4 ** Copyright (c) 2012 The Khronos Group Inc.
6 ** Permission is hereby granted, free of charge, to any person obtaining a
7 ** copy of this software and/or associated documentation files (the
8 ** "Materials"), to deal in the Materials without restriction, including
9 ** without limitation the rights to use, copy, modify, merge, publish,
10 ** distribute, sublicense, and/or sell copies of the Materials, and to
11 ** permit persons to whom the Materials are furnished to do so, subject to
12 ** the following conditions:
14 ** The above copyright notice and this permission notice shall be included
15 ** in all copies or substantial portions of the Materials.
17 ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
31 <meta charset="utf-8">
32 <title>WebGL OES_texture_float Conformance Tests</title>
33 <link rel="stylesheet" href="../../resources/js-test-style.css"/>
34 <script src="../../resources/js-test-pre.js"></script>
35 <script src="../resources/webgl-test-utils.js"></script>
38 <div id="description"></div>
39 <canvas id="canvas" style="width: 50px; height: 50px;"> </canvas>
40 <div id="console"></div>
41 <!-- Shaders for testing floating-point textures -->
42 <script id="testFragmentShader" type="x-shader/x-fragment">
43 precision mediump float;
44 uniform sampler2D tex;
45 uniform vec4 subtractor;
46 varying vec2 texCoord;
49 vec4 color = texture2D(tex, texCoord);
50 if (abs(color.r - subtractor.r) +
51 abs(color.g - subtractor.g) +
52 abs(color.b - subtractor.b) +
53 abs(color.a - subtractor.a) < 8.0) {
54 gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
56 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
60 <!-- Shaders for testing floating-point render targets -->
61 <script id="positionVertexShader" type="x-shader/x-vertex">
62 attribute vec4 vPosition;
65 gl_Position = vPosition;
68 <script id="floatingPointFragmentShader" type="x-shader/x-fragment">
71 gl_FragColor = vec4(10000.0, 10000.0, 10000.0, 10000.0);
76 description("This test verifies the functionality of the OES_texture_float extension, if it is available.");
80 var wtu = WebGLTestUtils;
81 var canvas = document.getElementById("canvas");
82 var gl = wtu.create3DContext(canvas);
85 testFailed("WebGL context does not exist");
87 testPassed("WebGL context exists");
89 var texturedShaders = [
90 wtu.setupSimpleTextureVertexShader(gl),
96 ['vPosition', 'texCoord0'],
98 var quadParameters = wtu.setupUnitQuad(gl, 0, 1);
100 // First verify that allocation of floating-point textures fails if
101 // the extension has not been enabled yet.
102 runTextureCreationTest(testProgram, false);
104 if (!gl.getExtension("OES_texture_float")) {
105 testPassed("No OES_texture_float support -- this is legal");
107 testPassed("Successfully enabled OES_texture_float extension");
108 // If alpha value is missing from a texture it gets filled to 1 when sampling according to GLES2.0 table 3.12
109 runTextureCreationTest(testProgram, true, gl.RGBA, 4, [10000, 10000, 10000, 10000]);
110 runTextureCreationTest(testProgram, true, gl.RGB, 3, [10000, 10000, 10000, 1]);
111 runTextureCreationTest(testProgram, true, gl.LUMINANCE, 1, [10000, 10000, 10000, 1]);
112 runTextureCreationTest(testProgram, true, gl.ALPHA, 1, [0, 0, 0, 10000]);
113 runTextureCreationTest(testProgram, true, gl.LUMINANCE_ALPHA, 2, [10000, 10000, 10000, 10000]);
114 runRenderTargetAndReadbackTest(testProgram, gl.RGBA, 4, [10000, 10000, 10000, 10000], 0);
115 runRenderTargetAndReadbackTest(testProgram, gl.RGB, 3, [10000, 10000, 10000, 1], 0);
116 runRenderTargetAndReadbackTest(testProgram, gl.RGBA, 4, [10000, 10000, 10000, 10000], 1);
117 runRenderTargetAndReadbackTest(testProgram, gl.RGBA, 4, [10000, 10000, 10000, 10000], 0.5);
118 runUniqueObjectTest();
122 function allocateTexture()
124 var texture = gl.createTexture();
125 gl.bindTexture(gl.TEXTURE_2D, texture);
126 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
127 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
128 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
129 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
130 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "texture parameter setup should succeed");
134 function checkRenderingResults()
136 wtu.checkCanvas(gl, [0, 255, 0, 255], "should be green");
139 function runTextureCreationTest(testProgram, extensionEnabled, opt_format, opt_numChannels, opt_subtractor)
141 var format = opt_format || gl.RGBA;
142 var numberOfChannels = opt_numChannels || 4;
143 var expectFailure = !extensionEnabled;
144 var subtractor = opt_subtractor || [10000, 10000, 10000, 10000];
147 debug("testing format: " + wtu.glEnumToString(gl, format) +
148 " expect:" + (extensionEnabled ? "success" : "failure"));
150 var texture = allocateTexture();
154 var data = new Float32Array(width * height * numberOfChannels);
155 for (var ii = 0; ii < data.length; ++ii) {
158 gl.texImage2D(gl.TEXTURE_2D, 0, format, width, height, 0, format, gl.FLOAT, data);
160 wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "floating-point texture allocation must be disallowed if OES_texture_float isn't enabled");
163 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "floating-point texture allocation should succeed if OES_texture_float is enabled");
165 // Verify that the texture actually works for sampling and contains the expected data.
166 gl.uniform4fv(gl.getUniformLocation(testProgram, "subtractor"), subtractor);
167 wtu.clearAndDrawUnitQuad(gl);
168 checkRenderingResults();
170 // Check that linear fails.
171 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
172 wtu.clearAndDrawUnitQuad(gl);
173 wtu.checkCanvas(gl, [255, 0, 0, 255], "should be red");
176 function arrayToString(arr, size) {
183 for (var ii = 0; ii < mySize; ++ii) {
192 function runRenderTargetAndReadbackTest(testProgram, format, numberOfChannels, subtractor, texSubImageCover)
194 var formatString = wtu.glEnumToString(gl, format);
196 debug("testing floating-point " + formatString + " render target" + (texSubImageCover > 0 ? " after calling texSubImage" : ""));
198 var texture = allocateTexture();
201 gl.texImage2D(gl.TEXTURE_2D, 0, format, width, height, 0, format, gl.FLOAT, null);
202 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "floating-point texture allocation should succeed if OES_texture_float is enabled");
204 // Try to use this texture as a render target.
205 var fbo = gl.createFramebuffer();
206 gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
207 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
208 gl.bindTexture(gl.TEXTURE_2D, null);
209 // It is legal for a WebGL implementation exposing the OES_texture_float extension to
210 // support floating-point textures but not as attachments to framebuffer objects.
211 if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE) {
212 debug("floating-point " + formatString + " render target not supported -- this is legal");
216 if (texSubImageCover > 0) {
217 // Ensure that replacing the whole texture or a part of it with texSubImage2D doesn't affect renderability
218 gl.bindTexture(gl.TEXTURE_2D, texture);
219 var data = new Float32Array(width * height * numberOfChannels * texSubImageCover);
220 gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, width, height * texSubImageCover, format, gl.FLOAT, data);
221 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "texSubImage2D should succeed if OES_texture_float is enabled");
222 gl.bindTexture(gl.TEXTURE_2D, null);
223 if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE) {
224 testFailed("render target support changed after calling texSubImage2D");
231 ["positionVertexShader", "floatingPointFragmentShader"],
234 wtu.clearAndDrawUnitQuad(gl);
235 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "rendering to floating-point texture should succeed");
237 // Now sample from the floating-point texture and verify we got the correct values.
238 gl.bindFramebuffer(gl.FRAMEBUFFER, null);
239 gl.bindTexture(gl.TEXTURE_2D, texture);
240 gl.useProgram(testProgram);
241 gl.uniform1i(gl.getUniformLocation(testProgram, "tex"), 0);
242 gl.uniform4fv(gl.getUniformLocation(testProgram, "subtractor"), subtractor);
243 wtu.clearAndDrawUnitQuad(gl);
244 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "rendering from floating-point texture should succeed");
245 checkRenderingResults();
247 // Finally, if the implementation supports floating-point readback, verify it.
248 gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
249 var implFormat = gl.getParameter(gl.IMPLEMENTATION_COLOR_READ_FORMAT);
250 var implType = gl.getParameter(gl.IMPLEMENTATION_COLOR_READ_TYPE);
251 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "getParameter of IMPLEMENTATION_COLOR_READ_{FORMAT|TYPE} should succeed");
252 if ((implFormat == gl.RGBA || implFormat == gl.RGB) && implType == gl.FLOAT) {
253 debug("Checking readback of floating-point values");
254 var arraySize = (implFormat == gl.RGBA) ? 4 : 3
255 var buf = new Float32Array(arraySize);
256 gl.readPixels(0, 0, 1, 1, implFormat, implType , buf);
257 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "readPixels from floating-point renderbuffer should succeed");
259 var tolerance = 8.0; // TODO: factor this out from both this test and the subtractor shader above.
260 for (var ii = 0; ii < buf.length; ++ii) {
261 if (Math.abs(buf[ii] - subtractor[ii]) > tolerance) {
267 testPassed("readPixels of float-type data from floating-point renderbuffer succeeded");
269 testFailed("readPixels of float-type data from floating-point renderbuffer failed: expected "
270 + arrayToString(subtractor, arraySize) + ", got " + arrayToString(buf));
275 function runUniqueObjectTest()
278 debug("Testing that getExtension() returns the same object each time");
279 gl.getExtension("OES_texture_float").myProperty = 2;
281 shouldBe('gl.getExtension("OES_texture_float").myProperty', '2');
286 var successfullyParsed = true;
288 <script src="../../resources/js-test-post.js"></script>