1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "WebGLExtensions.h"
8 #include "mozilla/dom/WebGLRenderingContextBinding.h"
9 #include "WebGLContext.h"
10 #include "WebGLFormats.h"
14 WebGLExtensionTextureHalfFloat::WebGLExtensionTextureHalfFloat(
16 : WebGLExtensionBase(webgl
) {
17 auto& fua
= webgl
->mFormatUsage
;
18 gl::GLContext
* gl
= webgl
->GL();
20 webgl::PackingInfo pi
;
21 webgl::DriverUnpackInfo dui
;
22 const GLint
* swizzle
= nullptr;
24 const auto fnAdd
= [&](webgl::EffectiveFormat effFormat
) {
25 MOZ_ASSERT_IF(swizzle
, gl
->IsSupported(gl::GLFeature::texture_swizzle
));
27 auto usage
= fua
->EditUsage(effFormat
);
28 usage
->textureSwizzleRGBA
= swizzle
;
29 fua
->AddTexUnpack(usage
, pi
, dui
);
31 fua
->AllowUnsizedTexFormat(pi
, usage
);
34 bool useSizedFormats
= true;
35 const bool hasSizedLegacyFormats
= gl
->IsCompatibilityProfile();
36 if (gl
->IsGLES() && gl
->Version() < 300) {
37 useSizedFormats
= false;
40 GLenum driverUnpackType
= LOCAL_GL_HALF_FLOAT
;
41 if (!gl
->IsSupported(gl::GLFeature::texture_half_float
)) {
42 MOZ_ASSERT(gl
->IsExtensionSupported(gl::GLContext::OES_texture_half_float
));
43 driverUnpackType
= LOCAL_GL_HALF_FLOAT_OES
;
48 pi
= {LOCAL_GL_RGBA
, LOCAL_GL_HALF_FLOAT_OES
};
49 dui
= {pi
.format
, pi
.format
, driverUnpackType
};
51 if (useSizedFormats
) {
52 dui
.internalFormat
= LOCAL_GL_RGBA16F
;
54 fnAdd(webgl::EffectiveFormat::RGBA16F
);
58 pi
= {LOCAL_GL_RGB
, LOCAL_GL_HALF_FLOAT_OES
};
59 dui
= {pi
.format
, pi
.format
, driverUnpackType
};
61 if (useSizedFormats
) {
62 dui
.internalFormat
= LOCAL_GL_RGB16F
;
64 fnAdd(webgl::EffectiveFormat::RGB16F
);
68 pi
= {LOCAL_GL_LUMINANCE
, LOCAL_GL_HALF_FLOAT_OES
};
69 dui
= {pi
.format
, pi
.format
, driverUnpackType
};
71 if (useSizedFormats
) {
72 if (hasSizedLegacyFormats
) {
73 dui
.internalFormat
= LOCAL_GL_LUMINANCE16F_ARB
;
75 dui
.internalFormat
= LOCAL_GL_R16F
;
76 dui
.unpackFormat
= LOCAL_GL_RED
;
77 swizzle
= webgl::FormatUsageInfo::kLuminanceSwizzleRGBA
;
80 fnAdd(webgl::EffectiveFormat::Luminance16F
);
84 pi
= {LOCAL_GL_ALPHA
, LOCAL_GL_HALF_FLOAT_OES
};
85 dui
= {pi
.format
, pi
.format
, driverUnpackType
};
87 if (useSizedFormats
) {
88 if (hasSizedLegacyFormats
) {
89 dui
.internalFormat
= LOCAL_GL_ALPHA16F_ARB
;
91 dui
.internalFormat
= LOCAL_GL_R16F
;
92 dui
.unpackFormat
= LOCAL_GL_RED
;
93 swizzle
= webgl::FormatUsageInfo::kAlphaSwizzleRGBA
;
96 fnAdd(webgl::EffectiveFormat::Alpha16F
);
100 pi
= {LOCAL_GL_LUMINANCE_ALPHA
, LOCAL_GL_HALF_FLOAT_OES
};
101 dui
= {pi
.format
, pi
.format
, driverUnpackType
};
103 if (useSizedFormats
) {
104 if (hasSizedLegacyFormats
) {
105 dui
.internalFormat
= LOCAL_GL_LUMINANCE_ALPHA16F_ARB
;
107 dui
.internalFormat
= LOCAL_GL_RG16F
;
108 dui
.unpackFormat
= LOCAL_GL_RG
;
109 swizzle
= webgl::FormatUsageInfo::kLumAlphaSwizzleRGBA
;
112 fnAdd(webgl::EffectiveFormat::Luminance16FAlpha16F
);
115 bool WebGLExtensionTextureHalfFloat::IsSupported(const WebGLContext
* webgl
) {
116 if (webgl
->IsWebGL2()) return false;
118 gl::GLContext
* gl
= webgl
->GL();
119 if (!gl
->IsSupported(gl::GLFeature::texture_half_float
) &&
120 !gl
->IsExtensionSupported(gl::GLContext::OES_texture_half_float
)) {
124 const bool needsSwizzle
= gl
->IsCoreProfile();
125 const bool hasSwizzle
= gl
->IsSupported(gl::GLFeature::texture_swizzle
);
126 if (needsSwizzle
&& !hasSwizzle
) return false;
131 } // namespace mozilla