2 Copyright (c) 2019 The Khronos Group Inc.
3 Use of this source code is governed by an MIT-style license that can be
4 found in the LICENSE.txt file.
10 <meta charset=
"utf-8">
11 <title>WebGL OES_texture_half_float Conformance Tests
</title>
12 <link rel=
"stylesheet" href=
"../../resources/js-test-style.css"/>
13 <script src=
"../../js/js-test-pre.js"></script>
14 <script src=
"../../js/webgl-test-utils.js"></script>
17 <div id=
"description"></div>
18 <canvas id=
"canvas" style=
"width: 50px; height: 50px;"> </canvas>
19 <canvas id=
"canvas2d" style=
"width: 50px; height: 50px;"> </canvas>
20 <div id=
"console"></div>
21 <script id=
"testFragmentShader" type=
"x-shader/x-fragment">
22 precision mediump float;
23 uniform sampler2D tex;
24 uniform vec4 subtractor;
25 varying vec2 texCoord;
28 vec4 color = texture2D(tex, texCoord);
29 if (abs(color.r - subtractor.r) +
30 abs(color.g - subtractor.g) +
31 abs(color.b - subtractor.b) +
32 abs(color.a - subtractor.a) <
8.0) {
33 gl_FragColor = vec4(
0.0,
1.0,
0.0,
1.0);
35 gl_FragColor = vec4(
1.0,
0.0,
0.0,
1.0);
39 <!-- Shaders for testing half-floating-point render targets -->
40 <script id=
"floatingPointFragmentShader" type=
"x-shader/x-fragment">
43 gl_FragColor = vec4(
10000.0,
10000.0,
10000.0,
10000.0);
48 description("This test verifies the functionality of OES_texture_half_float with null/non-null ArrayBufferView");
51 var wtu
= WebGLTestUtils
;
52 var canvas
= document
.getElementById("canvas");
53 var colorCanvas
= document
.getElementById("canvas2d");
54 colorCanvas
.width
= 2;
55 colorCanvas
.height
= 2;
56 var ctx
= colorCanvas
.getContext("2d");
57 ctx
.fillStyle
= "rgb(255,0,0)";
58 ctx
.fillRect(0, 0, 2, 2);
59 var gl
= wtu
.create3DContext(canvas
);
60 // This constant must be defined in order to run the texture creation test without the extension enabled.
61 var halfFloatOESEnum
= 0x8D61;
66 testFailed("WebGL context does not exists");
68 testPassed("WebGL context exists");
70 // Verify that allocation of texture fails if extension is not enabled
71 runTextureCreationTest(false);
72 ext
= gl
.getExtension("OES_texture_half_float")
74 testPassed("No OES_texture_half_float support. This is legal");
76 testPassed("Successfully enabled OES_texture_half_float extension");
78 var program
= wtu
.setupTexturedQuad(gl
);
80 // Check if creation of texture succeed's with various formats and null ArrayBufferView
82 { format
: gl
.RGBA
, expected
: [255, 0, 0, 255], },
83 { format
: gl
.RGB
, expected
: [255, 0, 0, 255], },
84 { format
: gl
.LUMINANCE
, expected
: [255, 255, 255, 255], },
85 { format
: gl
.ALPHA
, expected
: [ 0, 0, 0, 255], },
86 { format
: gl
.LUMINANCE_ALPHA
, expected
: [255, 255, 255, 255], },
88 formats
.forEach(function(f
) {
89 runTextureCreationTest(true, f
.format
, null, f
.expected
);
92 // Texture creation should fail when passed with non-null, non-Uint16 ArrayBufferView
93 formats
.forEach(function(f
) {
96 var format
= f
.format
;
99 var float32Data
= new Float32Array(width
* height
* getNumberOfChannels(format
));
100 for (var ii
= 0; ii
< float32Data
.length
; ii
++) {
101 float32Data
[ii
] = 1000;
103 runTextureCreationTest(true, format
, float32Data
, null);
106 var int16Data
= new Int16Array(width
* height
* getNumberOfChannels(format
));
107 for (var ii
= 0; ii
< int16Data
.length
; ii
++) {
108 int16Data
[ii
] = 1000;
110 runTextureCreationTest(true, format
, int16Data
, null);
113 // Test that Uint16 encoded half float values can be used as texture data.
115 // First test that values in the 0-1 range can be written and read.
116 var halfFloatOneThird
= 0x3555; // Half float 1/3
117 var uint16Formats
= [
118 { format
: gl
.RGBA
, expected
: [85, 85, 85, 85], },
119 { format
: gl
.RGB
, expected
: [85, 85, 85, 255], },
120 { format
: gl
.LUMINANCE
, expected
: [85, 85, 85, 255], },
121 { format
: gl
.ALPHA
, expected
: [ 0, 0, 0, 85], },
122 { format
: gl
.LUMINANCE_ALPHA
, expected
: [85, 85, 85, 85], },
125 uint16Formats
.forEach(function(f
) {
128 var format
= f
.format
;
130 var uint16Data
= new Uint16Array(width
* height
* getNumberOfChannels(format
));
131 for (var ii
= 0; ii
< uint16Data
.length
; ii
++) {
132 uint16Data
[ii
] = halfFloatOneThird
;
134 runTextureCreationTest(true, format
, uint16Data
, f
.expected
);
137 // Next check that values outside the 0-1 range can be written.
138 var halfFloatTenK
= 0x70E2; // Half float 10000
139 var uint16Formats2
= [
140 { format
: gl
.RGBA
, subtractor
: [10000, 10000, 10000, 10000], requireRenderable
: true},
141 { format
: gl
.RGB
, subtractor
: [10000, 10000, 10000, 1], requireRenderable
: false},
144 uint16Formats2
.forEach(function(f
) {
147 var format
= f
.format
;
149 var uint16Data
= new Uint16Array(width
* height
* getNumberOfChannels(format
));
150 for (var ii
= 0; ii
< uint16Data
.length
; ii
++) {
151 uint16Data
[ii
] = halfFloatTenK
;
153 runRenderTest(format
, f
.subtractor
, uint16Data
, f
.requireRenderable
);
158 var renderable
= isRenderable(gl
, ext
);
159 var renderableExtName
= "EXT_color_buffer_half_float";
160 var supported
= gl
.getSupportedExtensions().includes(renderableExtName
);
161 if (renderable
&& !supported
) {
162 testFailed("RGBA/HALF_FLOAT_OES is color renderable but " + renderableExtName
+ " not exposed");
163 } else if (supported
&& !renderable
) {
164 testFailed(renderableExtName
+ " is exposed but RGBA/HALF_FLOAT_OES is not color renderable");
167 runRenderTest(gl
.RGBA
, [10000, 10000, 10000, 10000], null, true);
168 runRenderTest(gl
.RGB
, [10000, 10000, 10000, 1], null, false);
169 runFramebufferTest();
173 // Check of getExtension() returns same object
174 runUniqueObjectTest();
178 function isRenderable(gl
, ext
) {
179 var tex
= gl
.createTexture();
180 gl
.bindTexture(gl
.TEXTURE_2D
, tex
);
181 gl
.texImage2D(gl
.TEXTURE_2D
, 0, gl
.RGBA
, 1, 1, 0, gl
.RGBA
, ext
.HALF_FLOAT_OES
, null);
183 var fb
= gl
.createFramebuffer();
184 gl
.bindFramebuffer(gl
.FRAMEBUFFER
, fb
);
185 gl
.framebufferTexture2D(gl
.FRAMEBUFFER
, gl
.COLOR_ATTACHMENT0
, gl
.TEXTURE_2D
, tex
, 0);
187 var status
= gl
.checkFramebufferStatus(gl
.FRAMEBUFFER
);
188 gl
.deleteFramebuffer(fb
);
189 gl
.deleteTexture(tex
);
191 return status
== gl
.FRAMEBUFFER_COMPLETE
;
194 function getNumberOfChannels(format
)
196 if (format
== gl
.RGBA
)
198 else if (format
== gl
.RGB
)
200 else if (format
== gl
.LUMINANCE
|| format
== gl
.ALPHA
)
202 else if (format
== gl
.LUMINANCE_ALPHA
)
206 function allocateTexture()
208 var texture
= gl
.createTexture();
209 gl
.bindTexture(gl
.TEXTURE_2D
, texture
);
210 gl
.texParameteri(gl
.TEXTURE_2D
, gl
.TEXTURE_MIN_FILTER
, gl
.NEAREST
);
211 gl
.texParameteri(gl
.TEXTURE_2D
, gl
.TEXTURE_MAG_FILTER
, gl
.NEAREST
);
212 gl
.texParameteri(gl
.TEXTURE_2D
, gl
.TEXTURE_WRAP_S
, gl
.CLAMP_TO_EDGE
);
213 gl
.texParameteri(gl
.TEXTURE_2D
, gl
.TEXTURE_WRAP_T
, gl
.CLAMP_TO_EDGE
);
214 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "texture parameter setup should succeed");
218 function runTextureCreationTest(extensionEnabled
, opt_format
, opt_data
, opt_expected
)
220 var format
= opt_format
|| gl
.RGBA
;
221 var data
= opt_data
|| null;
222 var expectSuccess
= true;
224 if (!extensionEnabled
|| !opt_expected
)
225 expectSuccess
= false;
226 debug("Testing texture creation with extension " + (extensionEnabled
? "enabled" : "disabled") +
227 ", format " + wtu
.glEnumToString(gl
, format
) + ", and data " + (data
? "non-null" : "null") +
228 ". Expect " + (expectSuccess
? "Success" : "Failure"));
230 var texture
= allocateTexture();
233 gl
.texImage2D(gl
.TEXTURE_2D
, 0, format
, width
, height
, 0, format
, halfFloatOESEnum
, data
);
234 if(!extensionEnabled
) {
235 wtu
.glErrorShouldBe(gl
, gl
.INVALID_ENUM
, "Half floating point texture must be disallowed if OES_texture_half_float isn't enabled");
237 } else if (!opt_expected
) {
238 wtu
.glErrorShouldBe(gl
, gl
.INVALID_OPERATION
, "Half floating point texture allocation must be disallowed when ArrayBufferView is not-null and not-Uint16");
241 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "Half floating point texture allocation should succeed if OES_texture_half_float is enabled");
244 gl
.texImage2D(gl
.TEXTURE_2D
, 0, format
, format
, halfFloatOESEnum
, colorCanvas
);
246 wtu
.clearAndDrawUnitQuad(gl
);
247 wtu
.checkCanvas(gl
, opt_expected
);
248 // Check that linear fails.
249 gl
.texParameteri(gl
.TEXTURE_2D
, gl
.TEXTURE_MAG_FILTER
, gl
.LINEAR
);
250 wtu
.clearAndDrawUnitQuad(gl
);
251 wtu
.checkCanvas(gl
, [0, 0, 0, 255], "should be black");
256 function checkRenderingResults()
258 wtu
.checkCanvas(gl
, [0, 255, 0, 255], "should be green");
261 function runRenderTest(format
, subtractor
, data
, requireRenderable
)
263 var formatString
= wtu
.glEnumToString(gl
, format
);
268 debug("Testing half floating point " + formatString
+ " render target");
270 debug("Testing half floating point " + formatString
+ " from a Uint16Array");
273 var texture
= allocateTexture();
277 gl
.texImage2D(gl
.TEXTURE_2D
, 0, format
, width
, height
, 0, format
, ext
.HALF_FLOAT_OES
, data
);
278 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "Half floating point texture allocation should succeed if OES_texture_half_float is enabled");
281 // Try to use this texture as render target
282 var fbo
= gl
.createFramebuffer();
283 gl
.bindFramebuffer(gl
.FRAMEBUFFER
, fbo
);
284 gl
.framebufferTexture2D(gl
.FRAMEBUFFER
, gl
.COLOR_ATTACHMENT0
, gl
.TEXTURE_2D
, texture
, 0);
285 gl
.bindTexture(gl
.TEXTURE_2D
, null);
287 // It is legal for a WebGL implementation exposing the OES_texture_half_float extension to
288 // support half floating point textures but not as attachments to framebuffer objects.
289 if (gl
.checkFramebufferStatus(gl
.FRAMEBUFFER
) != gl
.FRAMEBUFFER_COMPLETE
) {
290 if (requireRenderable
) {
291 testFailed(formatString
+ " render targets not supported.");
293 debug(formatString
+ " render targets not supported -- this is legal");
300 [wtu
.simpleVertexShader
, "floatingPointFragmentShader"],
303 wtu
.drawUnitQuad(gl
);
304 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "Rendering to half floating point texture should succeed");
307 // Now sample from the half floating-point texture and verify we got the correct values.
308 var texturedShaders
= [
309 wtu
.simpleTextureVertexShader
,
314 [wtu
.simpleTextureVertexShader
, "testFragmentShader"],
315 ['vPosition', 'texCoord0'],
317 var quadParameters
= wtu
.setupUnitQuad(gl
, 0, 1);
318 gl
.bindFramebuffer(gl
.FRAMEBUFFER
, null);
319 gl
.bindTexture(gl
.TEXTURE_2D
, texture
);
320 gl
.useProgram(testProgram
);
321 gl
.uniform4fv(gl
.getUniformLocation(testProgram
, "subtractor"), subtractor
);
322 wtu
.drawUnitQuad(gl
);
323 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "rendering from half floating point texture should succeed");
324 checkRenderingResults();
327 function runUniqueObjectTest()
330 debug("Testing that getExtension() returns the same object each time");
332 gl
.getExtension("OES_texture_half_float").myProperty
= 2;
333 webglHarnessCollectGarbage();
334 shouldBe('gl.getExtension("OES_texture_half_float").myProperty', '2');
337 // Make sure we can call readPixels with the passed in arrayBufferConstructor and that the color
338 // channels are the ones we expect. If there is a mismatch between the glType and arrayBuffer type,
340 function verifyReadPixelsColors(red
, green
, blue
, alpha
, alphaRGB
, glFormat
, glType
, arrayBufferConstructor
) {
341 var typeName
= wtu
.glEnumToString(gl
, glType
);
343 debug(wtu
.glEnumToString(gl
, glFormat
) + " framebuffer with " + typeName
+ " readback.");
345 var arrayBuffer
= new arrayBufferConstructor(4);
346 gl
.readPixels(0, 0, 1, 1, gl
.RGBA
, glType
, arrayBuffer
);
347 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "readPixels should return NO_ERROR when reading " + typeName
+ " data.");
349 assertMsg(arrayBuffer
[0] === red
, "Red channel should be " + red
+ " for " + typeName
+ " readPixels. Received: " + arrayBuffer
[0]);
350 assertMsg(arrayBuffer
[1] === green
, "Green channel should be " + green
+ " for " + typeName
+ " readPixels. Received: " + arrayBuffer
[1]);
351 assertMsg(arrayBuffer
[2] === blue
, "Blue channel should be " + blue
+ " for " + typeName
+ " readPixels. Received: " + arrayBuffer
[2]);
352 if (glFormat
=== gl
.RGBA
) {
353 assertMsg(arrayBuffer
[3] === alpha
, "Alpha channel should be " + alpha
+ " for " + typeName
+ " readPixels. Received: " + arrayBuffer
[3]);
354 } else if (glFormat
=== gl
.RGB
) {
355 assertMsg(arrayBuffer
[3] === alphaRGB
, "Alpha channel should be " + alphaRGB
+ " for " + typeName
+ " readPixels. Received: " + arrayBuffer
[3]);
358 // Make sure any arrayBuffer types that are not equal to arrayBufferConstructor fail readPixels.
359 if (arrayBufferConstructor
!== Uint8Array
) {
360 arrayBuffer
= new Uint8Array(4);
361 gl
.readPixels(0, 0, 1, 1, gl
.RGBA
, glType
, arrayBuffer
);
362 wtu
.glErrorShouldBe(gl
, gl
.INVALID_OPERATION
, "readPixels should return INVALID_OPERATION when reading mismatched types. " + Uint8Array
.toString());
364 if (arrayBufferConstructor
!== Float32Array
) {
365 arrayBuffer
= new Float32Array(4);
366 gl
.readPixels(0, 0, 1, 1, gl
.RGBA
, glType
, arrayBuffer
);
367 wtu
.glErrorShouldBe(gl
, gl
.INVALID_OPERATION
, "readPixels should return INVALID_OPERATION when reading mismatched types. " + Float32Array
.toString());
369 if (arrayBufferConstructor
!== Uint16Array
) {
370 arrayBuffer
= new Uint16Array(4);
371 gl
.readPixels(0, 0, 1, 1, gl
.RGBA
, glType
, arrayBuffer
);
372 wtu
.glErrorShouldBe(gl
, gl
.INVALID_OPERATION
, "readPixels should return INVALID_OPERATION when reading mismatched types. " + Uint16Array
.toString());
376 // Verify that half float textures attached to frame buffers function correctly with regard to framebuffer
377 // completness, IMPLEMENTATION_COLOR_READ_FORMAT/TYPE and readPixels
378 function runFramebufferTest() {
380 debug("Framebuffer Tests");
382 var texture
= allocateTexture();
383 gl
.bindTexture(gl
.TEXTURE_2D
, texture
);
385 var fbo
= gl
.createFramebuffer();
386 gl
.bindFramebuffer(gl
.FRAMEBUFFER
, fbo
);
387 gl
.framebufferTexture2D(gl
.FRAMEBUFFER
, gl
.COLOR_ATTACHMENT0
, gl
.TEXTURE_2D
, texture
, 0);
389 debug("Ensure non-color-renderable formats [LUMINANCE, LUMINANCE_ALPHA, ALPHA] fail.");
390 var arrayBufferFloatOutput
= new Float32Array(4); // 4 color channels
391 [gl
.LUMINANCE
, gl
.LUMINANCE_ALPHA
, gl
.ALPHA
].forEach(function(badFormat
) {
392 debug(wtu
.glEnumToString(gl
, badFormat
) + " framebuffer");
394 gl
.texImage2D(gl
.TEXTURE_2D
, 0, badFormat
, 1, 1, 0, badFormat
, ext
.HALF_FLOAT_OES
, null);
395 shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_INCOMPLETE_ATTACHMENT");
397 shouldBeNull("gl.getParameter(gl.IMPLEMENTATION_COLOR_READ_FORMAT)");
398 wtu
.glErrorShouldBe(gl
, gl
.INVALID_OPERATION
, "IMPLEMENTATION_COLOR_READ_FORMAT should fail for incomplete framebuffers.");
400 shouldBeNull("gl.getParameter(gl.IMPLEMENTATION_COLOR_READ_TYPE)");
401 wtu
.glErrorShouldBe(gl
, gl
.INVALID_OPERATION
, "IMPLEMENTATION_COLOR_READ_TYPE should fail for incomplete framebuffers.");
403 gl
.readPixels(0, 0, 1, 1, gl
.RGBA
, gl
.FLOAT
, arrayBufferFloatOutput
);
404 wtu
.glErrorShouldBe(gl
, gl
.INVALID_FRAMEBUFFER_OPERATION
, "readPixels should fail on incomplete framebuffers.");
408 debug("Ensure color renderable formats [RGBA, RGB] succeed.");
409 var arrayBufferHalfFloatInput
= new Uint16Array(4); // 4 color channels
410 arrayBufferHalfFloatInput
[0] = 0; // 0 in half float
411 arrayBufferHalfFloatInput
[1] = 0x3400; // 0.25 in half float
412 arrayBufferHalfFloatInput
[2] = 0x3800; // 0.50 in half float
413 arrayBufferHalfFloatInput
[3] = 0x3A00; // 0.75 in half float
415 [gl
.RGBA
, gl
.RGB
].forEach(function(goodFormat
) {
416 debug(wtu
.glEnumToString(gl
, goodFormat
) + " framebuffer tests");
419 gl
.texImage2D(gl
.TEXTURE_2D
, 0, goodFormat
, 1, 1, 0, goodFormat
, ext
.HALF_FLOAT_OES
, arrayBufferHalfFloatInput
);
420 if (gl
.checkFramebufferStatus(gl
.FRAMEBUFFER
) == gl
.FRAMEBUFFER_COMPLETE
) {
421 // Per the OES_color_buffer_half_float, RGBA/FLOAT should always succeed for readPixels
422 verifyReadPixelsColors(
432 var implementationColorReadFormat
= gl
.getParameter(gl
.IMPLEMENTATION_COLOR_READ_FORMAT
);
433 assertMsg(implementationColorReadFormat
=== gl
.RGBA
|| implementationColorReadFormat
=== gl
.RGB
,
434 "IMPLEMENTATION_COLOR_READ_FORMAT should be color renderable: RGBA or RGB. Received: " + wtu
.glEnumToString(gl
, implementationColorReadFormat
));
436 var implementationColorReadType
= gl
.getParameter(gl
.IMPLEMENTATION_COLOR_READ_TYPE
);
438 // There is nothing in the specifications that keeps the
439 // implementation color read format and type from being the
440 // same as the implicitly supported one. For this reason, keep
441 // gl.FLOAT as one of the valid options.
442 assertMsg(implementationColorReadType
=== gl
.UNSIGNED_BYTE
||
443 implementationColorReadType
=== gl
.FLOAT
||
444 implementationColorReadType
=== ext
.HALF_FLOAT_OES
||
445 implementationColorReadType
=== gl
.UNSIGNED_SHORT_4_4_4_4
||
446 implementationColorReadType
=== gl
.UNSIGNED_SHORT_5_5_5_1
||
447 implementationColorReadType
=== gl
.UNSIGNED_SHORT_5_6_5
,
448 "IMPLEMENTATION_COLOR_READ_TYPE must be one of UNSIGNED_BYTE, UNSIGNED_SHORT_4_4_4_4, UNSIGNED_SHORT_5_5_5_1, UNSIGNED_SHORT_5_6_5, FLOAT, or HALF_FLOAT_OES. " +
449 "Received: " + wtu
.glEnumToString(gl
, implementationColorReadType
));
451 // Test the RGBA/HALF_FLOAT_OES combination
452 if (implementationColorReadFormat
=== gl
.RGBA
&& implementationColorReadType
=== ext
.HALF_FLOAT_OES
) {
453 verifyReadPixelsColors(
469 var successfullyParsed
= true;
471 <script src=
"../../js/js-test-post.js"></script>