1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "WebGLContext.h"
7 #include "ClientWebGLExtensions.h"
9 #include "mozilla/dom/BindingDeclarations.h"
10 #include "mozilla/dom/ToJSValue.h"
11 #include "mozilla/EnumeratedRange.h"
12 #include "mozilla/StaticPrefs_webgl.h"
14 #include "WebGLContextUtils.h"
15 #include "WebGLExtensions.h"
19 const char* GetExtensionName(const WebGLExtensionID ext
) {
20 static EnumeratedArray
<WebGLExtensionID
, WebGLExtensionID::Max
, const char*>
21 sExtensionNamesEnumeratedArray
;
22 static bool initialized
= false;
27 #define WEBGL_EXTENSION_IDENTIFIER(x) \
28 sExtensionNamesEnumeratedArray[WebGLExtensionID::x] = #x;
30 WEBGL_EXTENSION_IDENTIFIER(ANGLE_instanced_arrays
)
31 WEBGL_EXTENSION_IDENTIFIER(EXT_blend_minmax
)
32 WEBGL_EXTENSION_IDENTIFIER(EXT_color_buffer_float
)
33 WEBGL_EXTENSION_IDENTIFIER(EXT_color_buffer_half_float
)
34 WEBGL_EXTENSION_IDENTIFIER(EXT_disjoint_timer_query
)
35 WEBGL_EXTENSION_IDENTIFIER(EXT_float_blend
)
36 WEBGL_EXTENSION_IDENTIFIER(EXT_frag_depth
)
37 WEBGL_EXTENSION_IDENTIFIER(EXT_shader_texture_lod
)
38 WEBGL_EXTENSION_IDENTIFIER(EXT_sRGB
)
39 WEBGL_EXTENSION_IDENTIFIER(EXT_texture_compression_bptc
)
40 WEBGL_EXTENSION_IDENTIFIER(EXT_texture_compression_rgtc
)
41 WEBGL_EXTENSION_IDENTIFIER(EXT_texture_filter_anisotropic
)
42 WEBGL_EXTENSION_IDENTIFIER(EXT_texture_norm16
)
43 WEBGL_EXTENSION_IDENTIFIER(MOZ_debug
)
44 WEBGL_EXTENSION_IDENTIFIER(OES_draw_buffers_indexed
)
45 WEBGL_EXTENSION_IDENTIFIER(OES_element_index_uint
)
46 WEBGL_EXTENSION_IDENTIFIER(OES_fbo_render_mipmap
)
47 WEBGL_EXTENSION_IDENTIFIER(OES_standard_derivatives
)
48 WEBGL_EXTENSION_IDENTIFIER(OES_texture_float
)
49 WEBGL_EXTENSION_IDENTIFIER(OES_texture_float_linear
)
50 WEBGL_EXTENSION_IDENTIFIER(OES_texture_half_float
)
51 WEBGL_EXTENSION_IDENTIFIER(OES_texture_half_float_linear
)
52 WEBGL_EXTENSION_IDENTIFIER(OES_vertex_array_object
)
53 WEBGL_EXTENSION_IDENTIFIER(OVR_multiview2
)
54 WEBGL_EXTENSION_IDENTIFIER(WEBGL_color_buffer_float
)
55 WEBGL_EXTENSION_IDENTIFIER(WEBGL_compressed_texture_astc
)
56 WEBGL_EXTENSION_IDENTIFIER(WEBGL_compressed_texture_etc
)
57 WEBGL_EXTENSION_IDENTIFIER(WEBGL_compressed_texture_etc1
)
58 WEBGL_EXTENSION_IDENTIFIER(WEBGL_compressed_texture_pvrtc
)
59 WEBGL_EXTENSION_IDENTIFIER(WEBGL_compressed_texture_s3tc
)
60 WEBGL_EXTENSION_IDENTIFIER(WEBGL_compressed_texture_s3tc_srgb
)
61 WEBGL_EXTENSION_IDENTIFIER(WEBGL_debug_renderer_info
)
62 WEBGL_EXTENSION_IDENTIFIER(WEBGL_debug_shaders
)
63 WEBGL_EXTENSION_IDENTIFIER(WEBGL_depth_texture
)
64 WEBGL_EXTENSION_IDENTIFIER(WEBGL_draw_buffers
)
65 WEBGL_EXTENSION_IDENTIFIER(WEBGL_explicit_present
)
66 WEBGL_EXTENSION_IDENTIFIER(WEBGL_lose_context
)
68 #undef WEBGL_EXTENSION_IDENTIFIER
71 return sExtensionNamesEnumeratedArray
[ext
];
74 // ----------------------------
77 void ClientWebGLContext::GetExtension(JSContext
* cx
, const nsAString
& wideName
,
78 JS::MutableHandle
<JSObject
*> retval
,
79 dom::CallerType callerType
,
82 const FuncScope
funcScope(*this, "getExtension");
83 if (IsContextLost()) return;
85 const auto name
= NS_ConvertUTF16toUTF8(wideName
);
87 auto ext
= WebGLExtensionID::Max
;
89 // step 1: figure what extension is wanted
90 for (const auto extension
: MakeEnumeratedRange(WebGLExtensionID::Max
)) {
91 const auto& curName
= GetExtensionName(extension
);
92 if (name
.Equals(curName
, nsCaseInsensitiveCStringComparator
)) {
98 if (ext
== WebGLExtensionID::Max
) return;
100 RefPtr
<ClientWebGLExtensionBase
> extObj
;
101 if (ext
== WebGLExtensionID::WEBGL_lose_context
) {
102 extObj
= mExtLoseContext
;
104 extObj
= GetExtension(ext
, callerType
);
108 // Ugh, this would be easier returning `any` than `object`.
109 JS::Rooted
<JS::Value
> v(cx
);
110 MOZ_ALWAYS_TRUE(dom::ToJSValue(cx
, extObj
, &v
));
112 retval
.set(&v
.toObject());
116 RefPtr
<ClientWebGLExtensionBase
> ClientWebGLContext::GetExtension(
117 const WebGLExtensionID ext
, const dom::CallerType callerType
) {
118 if (ext
== WebGLExtensionID::WEBGL_lose_context
) {
120 return mExtLoseContext
;
123 if (!mNotLost
) return nullptr;
125 if (!IsSupported(ext
, callerType
)) return nullptr;
127 auto& extSlot
= mNotLost
->extensions
[UnderlyingValue(ext
)];
128 if (MOZ_UNLIKELY(!extSlot
)) {
129 extSlot
= [&]() -> RefPtr
<ClientWebGLExtensionBase
> {
132 case WebGLExtensionID::ANGLE_instanced_arrays
:
133 return new ClientWebGLExtensionInstancedArrays(*this);
136 case WebGLExtensionID::EXT_blend_minmax
:
137 return new ClientWebGLExtensionBlendMinMax(*this);
138 case WebGLExtensionID::EXT_color_buffer_float
:
139 return new ClientWebGLExtensionEXTColorBufferFloat(*this);
140 case WebGLExtensionID::EXT_color_buffer_half_float
:
141 return new ClientWebGLExtensionColorBufferHalfFloat(*this);
142 case WebGLExtensionID::EXT_disjoint_timer_query
:
143 return new ClientWebGLExtensionDisjointTimerQuery(*this);
144 case WebGLExtensionID::EXT_float_blend
:
145 return new ClientWebGLExtensionFloatBlend(*this);
146 case WebGLExtensionID::EXT_frag_depth
:
147 return new ClientWebGLExtensionFragDepth(*this);
148 case WebGLExtensionID::EXT_shader_texture_lod
:
149 return new ClientWebGLExtensionShaderTextureLod(*this);
150 case WebGLExtensionID::EXT_sRGB
:
151 return new ClientWebGLExtensionSRGB(*this);
152 case WebGLExtensionID::EXT_texture_compression_bptc
:
153 return new ClientWebGLExtensionCompressedTextureBPTC(*this);
154 case WebGLExtensionID::EXT_texture_compression_rgtc
:
155 return new ClientWebGLExtensionCompressedTextureRGTC(*this);
156 case WebGLExtensionID::EXT_texture_filter_anisotropic
:
157 return new ClientWebGLExtensionTextureFilterAnisotropic(*this);
158 case WebGLExtensionID::EXT_texture_norm16
:
159 return new ClientWebGLExtensionTextureNorm16(*this);
162 case WebGLExtensionID::MOZ_debug
:
163 return new ClientWebGLExtensionMOZDebug(*this);
166 case WebGLExtensionID::OES_draw_buffers_indexed
:
167 return new ClientWebGLExtensionDrawBuffersIndexed(*this);
168 case WebGLExtensionID::OES_element_index_uint
:
169 return new ClientWebGLExtensionElementIndexUint(*this);
170 case WebGLExtensionID::OES_fbo_render_mipmap
:
171 return new ClientWebGLExtensionFBORenderMipmap(*this);
172 case WebGLExtensionID::OES_standard_derivatives
:
173 return new ClientWebGLExtensionStandardDerivatives(*this);
174 case WebGLExtensionID::OES_texture_float
:
175 return new ClientWebGLExtensionTextureFloat(*this);
176 case WebGLExtensionID::OES_texture_float_linear
:
177 return new ClientWebGLExtensionTextureFloatLinear(*this);
178 case WebGLExtensionID::OES_texture_half_float
:
179 return new ClientWebGLExtensionTextureHalfFloat(*this);
180 case WebGLExtensionID::OES_texture_half_float_linear
:
181 return new ClientWebGLExtensionTextureHalfFloatLinear(*this);
182 case WebGLExtensionID::OES_vertex_array_object
:
183 return new ClientWebGLExtensionVertexArray(*this);
186 case WebGLExtensionID::OVR_multiview2
:
187 return new ClientWebGLExtensionMultiview(*this);
190 case WebGLExtensionID::WEBGL_color_buffer_float
:
191 return new ClientWebGLExtensionColorBufferFloat(*this);
192 case WebGLExtensionID::WEBGL_compressed_texture_astc
:
193 return new ClientWebGLExtensionCompressedTextureASTC(*this);
194 case WebGLExtensionID::WEBGL_compressed_texture_etc
:
195 return new ClientWebGLExtensionCompressedTextureES3(*this);
196 case WebGLExtensionID::WEBGL_compressed_texture_etc1
:
197 return new ClientWebGLExtensionCompressedTextureETC1(*this);
198 case WebGLExtensionID::WEBGL_compressed_texture_pvrtc
:
199 return new ClientWebGLExtensionCompressedTexturePVRTC(*this);
200 case WebGLExtensionID::WEBGL_compressed_texture_s3tc
:
201 return new ClientWebGLExtensionCompressedTextureS3TC(*this);
202 case WebGLExtensionID::WEBGL_compressed_texture_s3tc_srgb
:
203 return new ClientWebGLExtensionCompressedTextureS3TC_SRGB(*this);
204 case WebGLExtensionID::WEBGL_debug_renderer_info
: {
205 if (callerType
!= dom::CallerType::System
) {
207 "WEBGL_debug_renderer_info is deprecated in Firefox and will "
208 "be removed. Please use RENDERER.");
210 return new ClientWebGLExtensionDebugRendererInfo(*this);
212 case WebGLExtensionID::WEBGL_debug_shaders
:
213 return new ClientWebGLExtensionDebugShaders(*this);
214 case WebGLExtensionID::WEBGL_depth_texture
:
215 return new ClientWebGLExtensionDepthTexture(*this);
216 case WebGLExtensionID::WEBGL_draw_buffers
:
217 return new ClientWebGLExtensionDrawBuffers(*this);
218 case WebGLExtensionID::WEBGL_explicit_present
:
219 return new ClientWebGLExtensionExplicitPresent(*this);
221 case WebGLExtensionID::WEBGL_lose_context
:
222 case WebGLExtensionID::Max
:
225 MOZ_CRASH("illegal extension enum");
228 RequestExtension(ext
);
234 // ----------------------------
237 bool WebGLContext::IsExtensionSupported(WebGLExtensionID ext
) const {
239 case WebGLExtensionID::MOZ_debug
:
240 case WebGLExtensionID::WEBGL_debug_renderer_info
:
241 case WebGLExtensionID::WEBGL_debug_shaders
:
242 case WebGLExtensionID::WEBGL_lose_context
:
246 // In alphabetical order
248 case WebGLExtensionID::ANGLE_instanced_arrays
:
249 return WebGLExtensionInstancedArrays::IsSupported(this);
252 case WebGLExtensionID::EXT_blend_minmax
:
253 return WebGLExtensionBlendMinMax::IsSupported(this);
255 case WebGLExtensionID::EXT_color_buffer_float
:
256 return WebGLExtensionEXTColorBufferFloat::IsSupported(this);
258 case WebGLExtensionID::EXT_color_buffer_half_float
:
259 return WebGLExtensionColorBufferHalfFloat::IsSupported(this);
261 case WebGLExtensionID::EXT_disjoint_timer_query
:
262 return WebGLExtensionDisjointTimerQuery::IsSupported(this);
264 case WebGLExtensionID::EXT_float_blend
:
265 return WebGLExtensionFloatBlend::IsSupported(this);
267 case WebGLExtensionID::EXT_frag_depth
:
268 return WebGLExtensionFragDepth::IsSupported(this);
270 case WebGLExtensionID::EXT_shader_texture_lod
:
271 return WebGLExtensionShaderTextureLod::IsSupported(this);
273 case WebGLExtensionID::EXT_sRGB
:
274 return WebGLExtensionSRGB::IsSupported(this);
276 case WebGLExtensionID::EXT_texture_compression_bptc
:
277 return WebGLExtensionCompressedTextureBPTC::IsSupported(this);
279 case WebGLExtensionID::EXT_texture_compression_rgtc
:
280 return WebGLExtensionCompressedTextureRGTC::IsSupported(this);
282 case WebGLExtensionID::EXT_texture_filter_anisotropic
:
283 return gl
->IsExtensionSupported(
284 gl::GLContext::EXT_texture_filter_anisotropic
);
286 case WebGLExtensionID::EXT_texture_norm16
:
287 return WebGLExtensionTextureNorm16::IsSupported(this);
290 case WebGLExtensionID::OES_draw_buffers_indexed
:
291 if (!IsWebGL2()) return false;
292 return gl
->IsSupported(gl::GLFeature::draw_buffers_indexed
) &&
293 gl
->IsSupported(gl::GLFeature::get_integer_indexed
);
295 case WebGLExtensionID::OES_element_index_uint
:
296 if (IsWebGL2()) return false;
297 return gl
->IsSupported(gl::GLFeature::element_index_uint
);
299 case WebGLExtensionID::OES_fbo_render_mipmap
:
300 return WebGLExtensionFBORenderMipmap::IsSupported(this);
302 case WebGLExtensionID::OES_standard_derivatives
:
303 if (IsWebGL2()) return false;
304 return gl
->IsSupported(gl::GLFeature::standard_derivatives
);
306 case WebGLExtensionID::OES_texture_float
:
307 return WebGLExtensionTextureFloat::IsSupported(this);
309 case WebGLExtensionID::OES_texture_float_linear
:
310 return gl
->IsSupported(gl::GLFeature::texture_float_linear
);
312 case WebGLExtensionID::OES_texture_half_float
:
313 return WebGLExtensionTextureHalfFloat::IsSupported(this);
315 case WebGLExtensionID::OES_texture_half_float_linear
:
316 if (IsWebGL2()) return false;
317 return gl
->IsSupported(gl::GLFeature::texture_half_float_linear
);
319 case WebGLExtensionID::OES_vertex_array_object
:
320 return !IsWebGL2(); // Always supported in webgl1.
323 case WebGLExtensionID::OVR_multiview2
:
324 return WebGLExtensionMultiview::IsSupported(this);
327 case WebGLExtensionID::WEBGL_color_buffer_float
:
328 return WebGLExtensionColorBufferFloat::IsSupported(this);
330 case WebGLExtensionID::WEBGL_compressed_texture_astc
:
331 return WebGLExtensionCompressedTextureASTC::IsSupported(this);
333 case WebGLExtensionID::WEBGL_compressed_texture_etc
:
334 return gl
->IsSupported(gl::GLFeature::ES3_compatibility
) &&
337 case WebGLExtensionID::WEBGL_compressed_texture_etc1
:
338 return gl
->IsExtensionSupported(
339 gl::GLContext::OES_compressed_ETC1_RGB8_texture
) &&
342 case WebGLExtensionID::WEBGL_compressed_texture_pvrtc
:
343 return gl
->IsExtensionSupported(
344 gl::GLContext::IMG_texture_compression_pvrtc
);
346 case WebGLExtensionID::WEBGL_compressed_texture_s3tc
:
347 return WebGLExtensionCompressedTextureS3TC::IsSupported(this);
349 case WebGLExtensionID::WEBGL_compressed_texture_s3tc_srgb
:
350 return WebGLExtensionCompressedTextureS3TC_SRGB::IsSupported(this);
352 case WebGLExtensionID::WEBGL_depth_texture
:
353 return WebGLExtensionDepthTexture::IsSupported(this);
355 case WebGLExtensionID::WEBGL_draw_buffers
:
356 return WebGLExtensionDrawBuffers::IsSupported(this);
358 case WebGLExtensionID::WEBGL_explicit_present
:
359 return WebGLExtensionExplicitPresent::IsSupported(this);
361 case WebGLExtensionID::Max
:
368 bool WebGLContext::IsExtensionExplicit(const WebGLExtensionID ext
) const {
369 return mExtensions
[ext
] && mExtensions
[ext
]->IsExplicit();
372 void WebGLContext::WarnIfImplicit(const WebGLExtensionID ext
) const {
373 const auto& extension
= mExtensions
[ext
];
374 if (!extension
|| extension
->IsExplicit()) return;
377 "Using format enabled by implicitly enabled extension: %s. "
378 "For maximal portability enable it explicitly.",
379 GetExtensionName(ext
));
382 void WebGLContext::RequestExtension(const WebGLExtensionID ext
,
383 const bool explicitly
) {
384 const auto& limits
= Limits();
385 if (!limits
.supportedExtensions
[ext
]) return;
387 auto& slot
= mExtensions
[ext
];
390 case WebGLExtensionID::ANGLE_instanced_arrays
:
391 slot
.reset(new WebGLExtensionInstancedArrays(this));
395 case WebGLExtensionID::EXT_blend_minmax
:
396 slot
.reset(new WebGLExtensionBlendMinMax(this));
398 case WebGLExtensionID::EXT_color_buffer_float
:
399 slot
.reset(new WebGLExtensionEXTColorBufferFloat(this));
401 case WebGLExtensionID::EXT_color_buffer_half_float
:
402 slot
.reset(new WebGLExtensionColorBufferHalfFloat(this));
404 case WebGLExtensionID::EXT_disjoint_timer_query
:
405 slot
.reset(new WebGLExtensionDisjointTimerQuery(this));
407 case WebGLExtensionID::EXT_float_blend
:
408 slot
.reset(new WebGLExtensionFloatBlend(this));
410 case WebGLExtensionID::EXT_frag_depth
:
411 slot
.reset(new WebGLExtensionFragDepth(this));
413 case WebGLExtensionID::EXT_shader_texture_lod
:
414 slot
.reset(new WebGLExtensionShaderTextureLod(this));
416 case WebGLExtensionID::EXT_sRGB
:
417 slot
.reset(new WebGLExtensionSRGB(this));
419 case WebGLExtensionID::EXT_texture_compression_bptc
:
420 slot
.reset(new WebGLExtensionCompressedTextureBPTC(this));
422 case WebGLExtensionID::EXT_texture_compression_rgtc
:
423 slot
.reset(new WebGLExtensionCompressedTextureRGTC(this));
425 case WebGLExtensionID::EXT_texture_filter_anisotropic
:
426 slot
.reset(new WebGLExtensionTextureFilterAnisotropic(this));
428 case WebGLExtensionID::EXT_texture_norm16
:
429 slot
.reset(new WebGLExtensionTextureNorm16(this));
433 case WebGLExtensionID::MOZ_debug
:
434 slot
.reset(new WebGLExtensionMOZDebug(this));
438 case WebGLExtensionID::OES_draw_buffers_indexed
:
439 slot
.reset(new WebGLExtensionDrawBuffersIndexed(this));
441 case WebGLExtensionID::OES_element_index_uint
:
442 slot
.reset(new WebGLExtensionElementIndexUint(this));
444 case WebGLExtensionID::OES_fbo_render_mipmap
:
445 slot
.reset(new WebGLExtensionFBORenderMipmap(this));
447 case WebGLExtensionID::OES_standard_derivatives
:
448 slot
.reset(new WebGLExtensionStandardDerivatives(this));
450 case WebGLExtensionID::OES_texture_float
:
451 slot
.reset(new WebGLExtensionTextureFloat(this));
453 case WebGLExtensionID::OES_texture_float_linear
:
454 slot
.reset(new WebGLExtensionTextureFloatLinear(this));
456 case WebGLExtensionID::OES_texture_half_float
:
457 slot
.reset(new WebGLExtensionTextureHalfFloat(this));
459 case WebGLExtensionID::OES_texture_half_float_linear
:
460 slot
.reset(new WebGLExtensionTextureHalfFloatLinear(this));
462 case WebGLExtensionID::OES_vertex_array_object
:
463 slot
.reset(new WebGLExtensionVertexArray(this));
467 case WebGLExtensionID::OVR_multiview2
:
468 slot
.reset(new WebGLExtensionMultiview(this));
472 case WebGLExtensionID::WEBGL_color_buffer_float
:
473 slot
.reset(new WebGLExtensionColorBufferFloat(this));
475 case WebGLExtensionID::WEBGL_compressed_texture_astc
:
476 slot
.reset(new WebGLExtensionCompressedTextureASTC(this));
478 case WebGLExtensionID::WEBGL_compressed_texture_etc
:
479 slot
.reset(new WebGLExtensionCompressedTextureES3(this));
481 case WebGLExtensionID::WEBGL_compressed_texture_etc1
:
482 slot
.reset(new WebGLExtensionCompressedTextureETC1(this));
484 case WebGLExtensionID::WEBGL_compressed_texture_pvrtc
:
485 slot
.reset(new WebGLExtensionCompressedTexturePVRTC(this));
487 case WebGLExtensionID::WEBGL_compressed_texture_s3tc
:
488 slot
.reset(new WebGLExtensionCompressedTextureS3TC(this));
490 case WebGLExtensionID::WEBGL_compressed_texture_s3tc_srgb
:
491 slot
.reset(new WebGLExtensionCompressedTextureS3TC_SRGB(this));
493 case WebGLExtensionID::WEBGL_debug_renderer_info
:
494 slot
.reset(new WebGLExtensionDebugRendererInfo(this));
496 case WebGLExtensionID::WEBGL_debug_shaders
:
497 slot
.reset(new WebGLExtensionDebugShaders(this));
499 case WebGLExtensionID::WEBGL_depth_texture
:
500 slot
.reset(new WebGLExtensionDepthTexture(this));
502 case WebGLExtensionID::WEBGL_draw_buffers
:
503 slot
.reset(new WebGLExtensionDrawBuffers(this));
505 case WebGLExtensionID::WEBGL_explicit_present
:
506 slot
.reset(new WebGLExtensionExplicitPresent(this));
508 case WebGLExtensionID::WEBGL_lose_context
:
509 slot
.reset(new WebGLExtensionLoseContext(this));
512 case WebGLExtensionID::Max
:
516 const auto& obj
= slot
;
518 if (explicitly
&& !obj
->IsExplicit()) {
522 // Also enable implied extensions.
524 case WebGLExtensionID::EXT_color_buffer_float
:
525 RequestExtension(WebGLExtensionID::EXT_float_blend
, false);
528 case WebGLExtensionID::OES_texture_float
:
529 RequestExtension(WebGLExtensionID::EXT_float_blend
, false);
530 RequestExtension(WebGLExtensionID::WEBGL_color_buffer_float
, false);
533 case WebGLExtensionID::OES_texture_half_float
:
534 RequestExtension(WebGLExtensionID::EXT_color_buffer_half_float
, false);
537 case WebGLExtensionID::WEBGL_color_buffer_float
:
538 RequestExtension(WebGLExtensionID::EXT_float_blend
, false);
546 } // namespace mozilla