Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / webgl / src / sdk / tests / conformance / extensions / oes-texture-float.html
1 <!--
2
3 /*
4 ** Copyright (c) 2012 The Khronos Group Inc.
5 **
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:
13 **
14 ** The above copyright notice and this permission notice shall be included
15 ** in all copies or substantial portions of the Materials.
16 **
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.
24 */
25
26 -->
27
28 <!DOCTYPE html>
29 <html>
30 <head>
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>
36 </head>
37 <body>
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;
47 void main()
48 {
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);
55     } else {
56         gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
57     }
58 }
59 </script>
60 <!-- Shaders for testing floating-point render targets -->
61 <script id="positionVertexShader" type="x-shader/x-vertex">
62 attribute vec4 vPosition;
63 void main()
64 {
65     gl_Position = vPosition;
66 }
67 </script>
68 <script id="floatingPointFragmentShader" type="x-shader/x-fragment">
69 void main()
70 {
71     gl_FragColor = vec4(10000.0, 10000.0, 10000.0, 10000.0);
72 }
73 </script>
74 <script>
75 "use strict";
76 description("This test verifies the functionality of the OES_texture_float extension, if it is available.");
77
78 debug("");
79
80 var wtu = WebGLTestUtils;
81 var canvas = document.getElementById("canvas");
82 var gl = wtu.create3DContext(canvas);
83
84 if (!gl) {
85   testFailed("WebGL context does not exist");
86 } else {
87   testPassed("WebGL context exists");
88
89   var texturedShaders = [
90       wtu.setupSimpleTextureVertexShader(gl),
91       "testFragmentShader"
92   ];
93   var testProgram =
94       wtu.setupProgram(gl,
95                        texturedShaders,
96                        ['vPosition', 'texCoord0'],
97                        [0, 1]);
98   var quadParameters = wtu.setupUnitQuad(gl, 0, 1);
99
100   // First verify that allocation of floating-point textures fails if
101   // the extension has not been enabled yet.
102   runTextureCreationTest(testProgram, false);
103
104   if (!gl.getExtension("OES_texture_float")) {
105       testPassed("No OES_texture_float support -- this is legal");
106   } else {
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();
119   }
120 }
121
122 function allocateTexture()
123 {
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");
131     return texture;
132 }
133
134 function checkRenderingResults()
135 {
136     wtu.checkCanvas(gl, [0, 255, 0, 255], "should be green");
137 }
138
139 function runTextureCreationTest(testProgram, extensionEnabled, opt_format, opt_numChannels, opt_subtractor)
140 {
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];
145
146     debug("");
147     debug("testing format: " + wtu.glEnumToString(gl, format) +
148           " expect:" + (extensionEnabled ? "success" : "failure"));
149
150     var texture = allocateTexture();
151     // Generate data.
152     var width = 2;
153     var height = 2;
154     var data = new Float32Array(width * height * numberOfChannels);
155     for (var ii = 0; ii < data.length; ++ii) {
156         data[ii] = 10000;
157     }
158     gl.texImage2D(gl.TEXTURE_2D, 0, format, width, height, 0, format, gl.FLOAT, data);
159     if (expectFailure) {
160         wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "floating-point texture allocation must be disallowed if OES_texture_float isn't enabled");
161         return;
162     } else {
163         wtu.glErrorShouldBe(gl, gl.NO_ERROR, "floating-point texture allocation should succeed if OES_texture_float is enabled");
164     }
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();
169
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");
174 }
175
176 function arrayToString(arr, size) {
177     var mySize;
178     if (!size)
179         mySize = arr.length;
180     else
181         mySize = size;
182     var out = "[";
183     for (var ii = 0; ii < mySize; ++ii) {
184         if (ii > 0) {
185             out += ", ";
186         }
187         out += arr[ii];
188     }
189     return out + "]";
190 }
191
192 function runRenderTargetAndReadbackTest(testProgram, format, numberOfChannels, subtractor, texSubImageCover)
193 {
194     var formatString = wtu.glEnumToString(gl, format);
195     debug("");
196     debug("testing floating-point " + formatString + " render target" + (texSubImageCover > 0 ? " after calling texSubImage" : ""));
197
198     var texture = allocateTexture();
199     var width = 2;
200     var height = 2;
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");
203
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");
213         return;
214     }
215
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");
225             return;
226         }
227     }
228
229     var renderProgram =
230         wtu.setupProgram(gl,
231                          ["positionVertexShader", "floatingPointFragmentShader"],
232                          ['vPosition'],
233                          [0]);
234     wtu.clearAndDrawUnitQuad(gl);
235     wtu.glErrorShouldBe(gl, gl.NO_ERROR, "rendering to floating-point texture should succeed");
236
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();
246
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");
258         var ok = true;
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) {
262                 ok = false;
263                 break;
264             }
265         }
266         if (ok) {
267             testPassed("readPixels of float-type data from floating-point renderbuffer succeeded");
268         } else {
269             testFailed("readPixels of float-type data from floating-point renderbuffer failed: expected "
270                        + arrayToString(subtractor, arraySize) + ", got " + arrayToString(buf));
271         }
272     }
273 }
274
275 function runUniqueObjectTest()
276 {
277     debug("");
278     debug("Testing that getExtension() returns the same object each time");
279     gl.getExtension("OES_texture_float").myProperty = 2;
280     gc();
281     shouldBe('gl.getExtension("OES_texture_float").myProperty', '2');
282 }
283
284
285 debug("");
286 var successfullyParsed = true;
287 </script>
288 <script src="../../resources/js-test-post.js"></script>
289
290 </body>
291 </html>