content/canvas/test/webgl-conformance/conformance/extensions/webgl-depth-texture.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 <!--
michael@0 2
michael@0 3 /*
michael@0 4 ** Copyright (c) 2012 The Khronos Group Inc.
michael@0 5 **
michael@0 6 ** Permission is hereby granted, free of charge, to any person obtaining a
michael@0 7 ** copy of this software and/or associated documentation files (the
michael@0 8 ** "Materials"), to deal in the Materials without restriction, including
michael@0 9 ** without limitation the rights to use, copy, modify, merge, publish,
michael@0 10 ** distribute, sublicense, and/or sell copies of the Materials, and to
michael@0 11 ** permit persons to whom the Materials are furnished to do so, subject to
michael@0 12 ** the following conditions:
michael@0 13 **
michael@0 14 ** The above copyright notice and this permission notice shall be included
michael@0 15 ** in all copies or substantial portions of the Materials.
michael@0 16 **
michael@0 17 ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
michael@0 18 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
michael@0 19 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
michael@0 20 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
michael@0 21 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
michael@0 22 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
michael@0 23 ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
michael@0 24 */
michael@0 25
michael@0 26 -->
michael@0 27 <!DOCTYPE html>
michael@0 28 <html>
michael@0 29 <head>
michael@0 30 <meta charset="utf-8">
michael@0 31 <link rel="stylesheet" href="../../resources/js-test-style.css"/>
michael@0 32 <script src="../../resources/js-test-pre.js"></script>
michael@0 33 <script src="../resources/webgl-test.js"></script>
michael@0 34 <script src="../resources/webgl-test-utils.js"></script>
michael@0 35 <title>WebGL WEBGL_depth_texture Conformance Tests</title>
michael@0 36 </head>
michael@0 37 <body>
michael@0 38 <script id="vshader" type="x-shader/x-vertex">
michael@0 39 attribute vec4 a_position;
michael@0 40 void main()
michael@0 41 {
michael@0 42 gl_Position = a_position;
michael@0 43 }
michael@0 44 </script>
michael@0 45
michael@0 46 <script id="fshader" type="x-shader/x-fragment">
michael@0 47 precision mediump float;
michael@0 48 uniform sampler2D u_texture;
michael@0 49 uniform vec2 u_resolution;
michael@0 50 void main()
michael@0 51 {
michael@0 52 vec2 texcoord = gl_FragCoord.xy / u_resolution;
michael@0 53 gl_FragColor = texture2D(u_texture, texcoord);
michael@0 54 }
michael@0 55 </script>
michael@0 56 <div id="description"></div>
michael@0 57 <div id="console"></div>
michael@0 58 <canvas id="canvas" width="8" height="8" style="width: 8px; height: 8px;"></canvas>
michael@0 59 <script>
michael@0 60 if (window.initNonKhronosFramework) {
michael@0 61 window.initNonKhronosFramework(false);
michael@0 62 }
michael@0 63 description("This test verifies the functionality of the WEBGL_depth_texture extension, if it is available.");
michael@0 64
michael@0 65 debug("");
michael@0 66
michael@0 67 var wtu = WebGLTestUtils;
michael@0 68 var canvas = document.getElementById("canvas");
michael@0 69 var gl = wtu.create3DContext(canvas, {antialias: false});
michael@0 70 var program = wtu.setupTexturedQuad(gl);
michael@0 71 var ext = null;
michael@0 72 var vao = null;
michael@0 73 var tex;
michael@0 74 var name;
michael@0 75 var supportedFormats;
michael@0 76
michael@0 77 if (!gl) {
michael@0 78 testFailed("WebGL context does not exist");
michael@0 79 } else {
michael@0 80 testPassed("WebGL context exists");
michael@0 81
michael@0 82 // Run tests with extension disabled
michael@0 83 runTestDisabled();
michael@0 84
michael@0 85 // Query the extension and store globally so shouldBe can access it
michael@0 86 ext = wtu.getExtensionWithKnownPrefixes(gl, "WEBGL_depth_texture");
michael@0 87 if (!ext) {
michael@0 88 testPassed("No WEBGL_depth_texture support -- this is legal");
michael@0 89 runSupportedTest(false);
michael@0 90 } else {
michael@0 91 testPassed("Successfully enabled WEBGL_depth_texture extension");
michael@0 92
michael@0 93 runSupportedTest(true);
michael@0 94 runTestExtension();
michael@0 95 }
michael@0 96 }
michael@0 97
michael@0 98 function runSupportedTest(extensionEnabled) {
michael@0 99 var name = wtu.getSupportedExtensionWithKnownPrefixes(gl, "WEBGL_depth_texture");
michael@0 100 if (name !== undefined) {
michael@0 101 if (extensionEnabled) {
michael@0 102 testPassed("WEBGL_depth_texture listed as supported and getExtension succeeded");
michael@0 103 } else {
michael@0 104 testFailed("WEBGL_depth_texture listed as supported but getExtension failed");
michael@0 105 }
michael@0 106 } else {
michael@0 107 if (extensionEnabled) {
michael@0 108 testFailed("WEBGL_depth_texture not listed as supported but getExtension succeeded");
michael@0 109 } else {
michael@0 110 testPassed("WEBGL_depth_texture not listed as supported and getExtension failed -- this is legal");
michael@0 111 }
michael@0 112 }
michael@0 113 }
michael@0 114
michael@0 115
michael@0 116 function runTestDisabled() {
michael@0 117 debug("Testing binding enum with extension disabled");
michael@0 118
michael@0 119 var tex = gl.createTexture();
michael@0 120 gl.bindTexture(gl.TEXTURE_2D, tex);
michael@0 121 shouldGenerateGLError(gl, gl.INVALID_ENUM, 'gl.texImage2D(gl.TEXTURE_2D, 0, gl.DEPTH_COMPONENT, 1, 1, 0, gl.DEPTH_COMPONENT, gl.UNSIGNED_SHORT, null)');
michael@0 122 shouldGenerateGLError(gl, gl.INVALID_ENUM, 'gl.texImage2D(gl.TEXTURE_2D, 0, gl.DEPTH_COMPONENT, 1, 1, 0, gl.DEPTH_COMPONENT, gl.UNSIGNED_INT, null)');
michael@0 123 }
michael@0 124
michael@0 125
michael@0 126 function dumpIt(gl, res, msg) {
michael@0 127 return; // comment out to debug
michael@0 128 debug(msg);
michael@0 129 var actualPixels = new Uint8Array(res * res * 4);
michael@0 130 gl.readPixels(0, 0, res, res, gl.RGBA, gl.UNSIGNED_BYTE, actualPixels);
michael@0 131
michael@0 132 for (var yy = 0; yy < res; ++yy) {
michael@0 133 var strs = [];
michael@0 134 for (var xx = 0; xx < res; ++xx) {
michael@0 135 var actual = (yy * res + xx) * 4;
michael@0 136 strs.push("(" + actualPixels[actual] + "," + actualPixels[actual+1] + "," + actualPixels[actual + 2] + "," + actualPixels[actual + 3] + ")");
michael@0 137 }
michael@0 138 debug(strs.join(" "));
michael@0 139 }
michael@0 140 }
michael@0 141 function runTestExtension() {
michael@0 142 debug("Testing WEBGL_depth_texture");
michael@0 143
michael@0 144 var res = 8;
michael@0 145
michael@0 146 // make canvas for testing.
michael@0 147 canvas2 = document.createElement("canvas");
michael@0 148 canvas2.width = res;
michael@0 149 canvas2.height = res;
michael@0 150 var ctx = canvas2.getContext("2d");
michael@0 151 ctx.fillStyle = "blue";
michael@0 152 ctx.fillRect(0, 0, canvas2.width, canvas2.height);
michael@0 153
michael@0 154 var program = wtu.setupProgram(gl, ['vshader', 'fshader'], ['a_position']);
michael@0 155 gl.useProgram(program);
michael@0 156 gl.uniform2f(gl.getUniformLocation(program, "u_resolution"), res, res);
michael@0 157
michael@0 158 var buffer = gl.createBuffer();
michael@0 159 gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
michael@0 160 gl.bufferData(
michael@0 161 gl.ARRAY_BUFFER,
michael@0 162 new Float32Array(
michael@0 163 [ 1, 1, 1,
michael@0 164 -1, 1, 0,
michael@0 165 -1, -1, -1,
michael@0 166 1, 1, 1,
michael@0 167 -1, -1, -1,
michael@0 168 1, -1, 0,
michael@0 169 ]),
michael@0 170 gl.STATIC_DRAW);
michael@0 171 gl.enableVertexAttribArray(0);
michael@0 172 gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
michael@0 173
michael@0 174 var types = [
michael@0 175 {obj: 'gl', attachment: 'DEPTH_ATTACHMENT', format: 'DEPTH_COMPONENT', type: 'UNSIGNED_SHORT', data: 'new Uint16Array(1)' },
michael@0 176 {obj: 'gl', attachment: 'DEPTH_ATTACHMENT', format: 'DEPTH_COMPONENT', type: 'UNSIGNED_INT', data: 'new Uint32Array(1)' },
michael@0 177 {obj: 'ext', attachment: 'DEPTH_STENCIL_ATTACHMENT', format: 'DEPTH_STENCIL', type: 'UNSIGNED_INT_24_8_WEBGL', data: 'new Uint32Array(1)' }
michael@0 178 ];
michael@0 179
michael@0 180 for (var ii = 0; ii < types.length; ++ii) {
michael@0 181 var typeInfo = types[ii];
michael@0 182 var type = typeInfo.type;
michael@0 183 var typeStr = typeInfo.obj + '.' + type;
michael@0 184
michael@0 185 debug("");
michael@0 186 debug("testing: " + type);
michael@0 187
michael@0 188 // check that cubemaps are not allowed.
michael@0 189 var cubeTex = gl.createTexture();
michael@0 190 gl.bindTexture(gl.TEXTURE_CUBE_MAP, cubeTex);
michael@0 191 var targets = [
michael@0 192 'TEXTURE_CUBE_MAP_POSITIVE_X',
michael@0 193 'TEXTURE_CUBE_MAP_NEGATIVE_X',
michael@0 194 'TEXTURE_CUBE_MAP_POSITIVE_Y',
michael@0 195 'TEXTURE_CUBE_MAP_NEGATIVE_Y',
michael@0 196 'TEXTURE_CUBE_MAP_POSITIVE_Z',
michael@0 197 'TEXTURE_CUBE_MAP_NEGATIVE_Z'
michael@0 198 ];
michael@0 199 for (var tt = 0; tt < targets.length; ++tt) {
michael@0 200 shouldGenerateGLError(gl, gl.INVALID_OPERATION, 'gl.texImage2D(gl.' + targets[ii] + ', 1, gl.' + typeInfo.format + ', 1, 1, 0, gl.' + typeInfo.format + ', ' + typeStr + ', null)');
michael@0 201 }
michael@0 202
michael@0 203 // check 2d textures.
michael@0 204 tex = gl.createTexture();
michael@0 205 gl.bindTexture(gl.TEXTURE_2D, tex);
michael@0 206 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
michael@0 207 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
michael@0 208 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
michael@0 209 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
michael@0 210
michael@0 211 // test level > 0
michael@0 212 shouldGenerateGLError(gl, gl.INVALID_OPERATION, 'gl.texImage2D(gl.TEXTURE_2D, 1, gl.' + typeInfo.format + ', 1, 1, 0, gl.' + typeInfo.format + ', ' + typeStr + ', null)');
michael@0 213
michael@0 214 // test with data
michael@0 215 shouldGenerateGLError(gl, gl.INVALID_OPERATION, 'gl.texImage2D(gl.TEXTURE_2D, 0, gl.' + typeInfo.format + ', 1, 1, 0, gl.' + typeInfo.format + ', ' + typeStr + ', ' + typeInfo.data + ')');
michael@0 216
michael@0 217 // test with canvas
michael@0 218 shouldGenerateGLError(gl, [gl.INVALID_VALUE, gl.INVALID_ENUM, gl.INVALID_OPERATION], 'gl.texImage2D(gl.TEXTURE_2D, 0, gl.' + typeInfo.format + ', gl.' + typeInfo.format + ', ' + typeStr + ', canvas2)');
michael@0 219
michael@0 220 // test copyTexImage2D
michael@0 221 shouldGenerateGLError(gl, [gl.INVALID_ENUM, gl.INVALID_OPERATION], 'gl.copyTexImage2D(gl.TEXTURE_2D, 0, gl.' + typeInfo.format + ', 0, 0, 1, 1, 0)');
michael@0 222
michael@0 223 // test real thing
michael@0 224 shouldGenerateGLError(gl, gl.NO_ERROR, 'gl.texImage2D(gl.TEXTURE_2D, 0, gl.' + typeInfo.format + ', ' + res + ', ' + res + ', 0, gl.' + typeInfo.format + ', ' + typeStr + ', null)');
michael@0 225
michael@0 226 // test texSubImage2D
michael@0 227 shouldGenerateGLError(gl, gl.INVALID_OPERATION, 'gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 1, 1, gl.' + typeInfo.format + ', ' + typeStr + ', ' + typeInfo.data + ')');
michael@0 228
michael@0 229 // test copyTexSubImage2D
michael@0 230 shouldGenerateGLError(gl, gl.INVALID_OPERATION, 'gl.copyTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 0, 0, 1, 1)');
michael@0 231
michael@0 232 // test generateMipmap
michael@0 233 shouldGenerateGLError(gl, gl.INVALID_OPERATION, 'gl.generateMipmap(gl.TEXTURE_2D)');
michael@0 234
michael@0 235 var fbo = gl.createFramebuffer();
michael@0 236 gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
michael@0 237 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl[typeInfo.attachment], gl.TEXTURE_2D, tex, 0);
michael@0 238 // TODO: remove this check if the spec is updated to require these combinations to work.
michael@0 239 if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE)
michael@0 240 {
michael@0 241 // try adding a color buffer.
michael@0 242 var colorTex = gl.createTexture();
michael@0 243 gl.bindTexture(gl.TEXTURE_2D, colorTex);
michael@0 244 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
michael@0 245 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
michael@0 246 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
michael@0 247 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
michael@0 248 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, res, res, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
michael@0 249 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, colorTex, 0);
michael@0 250 shouldBe('gl.checkFramebufferStatus(gl.FRAMEBUFFER)', 'gl.FRAMEBUFFER_COMPLETE');
michael@0 251 }
michael@0 252
michael@0 253 // use the default texture to render with while we return to the depth texture.
michael@0 254 gl.bindTexture(gl.TEXTURE_2D, null);
michael@0 255
michael@0 256 // render the z-quad
michael@0 257 gl.enable(gl.DEPTH_TEST);
michael@0 258 gl.clearColor(1, 0, 0, 1);
michael@0 259 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
michael@0 260 gl.drawArrays(gl.TRIANGLES, 0, 6);
michael@0 261
michael@0 262 dumpIt(gl, res, "--first--");
michael@0 263
michael@0 264 // render the depth texture.
michael@0 265 gl.bindFramebuffer(gl.FRAMEBUFFER, null);
michael@0 266 gl.bindTexture(gl.TEXTURE_2D, tex);
michael@0 267 gl.clearColor(0, 0, 1, 1);
michael@0 268 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
michael@0 269 gl.drawArrays(gl.TRIANGLES, 0, 6);
michael@0 270
michael@0 271 var actualPixels = new Uint8Array(res * res * 4);
michael@0 272 gl.readPixels(0, 0, res, res, gl.RGBA, gl.UNSIGNED_BYTE, actualPixels);
michael@0 273
michael@0 274 dumpIt(gl, res, "--depth--");
michael@0 275
michael@0 276 // Check that each pixel's RGB are the same and that it's value is less
michael@0 277 // than the previous pixel in either direction. Basically verify we have a
michael@0 278 // gradient.
michael@0 279 var success = true;
michael@0 280 for (var yy = 0; yy < res; ++yy) {
michael@0 281 for (var xx = 0; xx < res; ++xx) {
michael@0 282 var actual = (yy * res + xx) * 4;
michael@0 283 var left = actual - 4;
michael@0 284 var down = actual - res * 4;
michael@0 285
michael@0 286 if (actualPixels[actual + 0] != actualPixels[actual + 1]) {
michael@0 287 testFailed('R != G');
michael@0 288 success = false;
michael@0 289 }
michael@0 290 if (actualPixels[actual + 0] != actualPixels[actual + 2]) {
michael@0 291 testFailed('R != B');
michael@0 292 success = false;
michael@0 293 }
michael@0 294 // ALPHA is implementation dependent
michael@0 295 if (actualPixels[actual + 3] != 0xFF && actualPixels[actual + 3] != actualPixels[actual + 0]) {
michael@0 296 testFailed('A != 255 && A != R');
michael@0 297 success = false;
michael@0 298 }
michael@0 299
michael@0 300 if (xx > 0) {
michael@0 301 if (actualPixels[actual] <= actualPixels[left]) {
michael@0 302 testFailed("actual(" + actualPixels[actual] + ") < left(" + actualPixels[left] + ")");
michael@0 303 success = false;
michael@0 304 }
michael@0 305 }
michael@0 306 if (yy > 0) {
michael@0 307 if (actualPixels[actual] <= actualPixels[down]) {
michael@0 308 testFailed("actual(" + actualPixels[actual] + ") < down(" + actualPixels[down] + ")");
michael@0 309 success = false;
michael@0 310 }
michael@0 311 }
michael@0 312 }
michael@0 313 }
michael@0 314
michael@0 315 // Check that bottom left corner is vastly different thatn top right.
michael@0 316 if (actualPixels[(res * res - 1) * 4] - actualPixels[0] < 0xC0) {
michael@0 317 testFailed("corners are not different enough");
michael@0 318 success = false;
michael@0 319 }
michael@0 320
michael@0 321 if (success) {
michael@0 322 testPassed("depth texture rendered correctly.");
michael@0 323 }
michael@0 324
michael@0 325 // check limitations
michael@0 326 gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
michael@0 327 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl[typeInfo.attachment], gl.TEXTURE_2D, null, 0);
michael@0 328 var badAttachment = typeInfo.attachment == 'DEPTH_ATTACHMENT' ? 'DEPTH_STENCIL_ATTACHMENT' : 'DEPTH_ATTACHMENT';
michael@0 329 shouldGenerateGLError(gl, gl.NO_ERROR, 'gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.' + badAttachment + ', gl.TEXTURE_2D, tex, 0)');
michael@0 330 shouldNotBe('gl.checkFramebufferStatus(gl.FRAMEBUFFER)', 'gl.FRAMEBUFFER_COMPLETE');
michael@0 331 shouldGenerateGLError(gl, gl.INVALID_FRAMEBUFFER_OPERATION, 'gl.clear(gl.DEPTH_BUFFER_BIT)');
michael@0 332 gl.bindFramebuffer(gl.FRAMEBUFFER, null);
michael@0 333 shouldBe('gl.getError()', 'gl.NO_ERROR');
michael@0 334 }
michael@0 335 }
michael@0 336
michael@0 337 debug("");
michael@0 338 successfullyParsed = true;
michael@0 339 </script>
michael@0 340 <script src="../../resources/js-test-post.js"></script>
michael@0 341 </body>
michael@0 342 </html>
michael@0 343

mercurial