content/canvas/test/webgl-conformance/conformance/context/context-attributes-alpha-depth-stencil-antialias.html

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

michael@0 1 <!--
michael@0 2 Copyright (c) 2011 The Chromium Authors. All rights reserved.
michael@0 3 Use of this source code is governed by a BSD-style license that can be
michael@0 4 found in the LICENSE file.
michael@0 5 -->
michael@0 6 <!DOCTYPE html>
michael@0 7 <html>
michael@0 8 <head>
michael@0 9 <meta charset="utf-8">
michael@0 10 <link rel="stylesheet" href="../../resources/js-test-style.css"/>
michael@0 11 <script src="../../resources/js-test-pre.js"></script>
michael@0 12 <script src="../resources/webgl-test.js"></script>
michael@0 13 <script id="vshader" type="x-shader/x-vertex">
michael@0 14 attribute vec3 pos;
michael@0 15 attribute vec4 colorIn;
michael@0 16 varying vec4 color;
michael@0 17
michael@0 18 void main()
michael@0 19 {
michael@0 20 color = colorIn;
michael@0 21 gl_Position = vec4(pos.xyz, 1.0);
michael@0 22 }
michael@0 23 </script>
michael@0 24
michael@0 25 <script id="fshader" type="x-shader/x-fragment">
michael@0 26 precision mediump float;
michael@0 27
michael@0 28 varying vec4 color;
michael@0 29
michael@0 30 void main()
michael@0 31 {
michael@0 32 gl_FragColor = color;
michael@0 33 }
michael@0 34 </script>
michael@0 35
michael@0 36 <script>
michael@0 37 var successfullyParsed = false;
michael@0 38
michael@0 39 // These four declarations need to be global for "shouldBe" to see them
michael@0 40 var webGL = null;
michael@0 41 var contextAttribs = null;
michael@0 42 var pixel = [0, 0, 0, 1];
michael@0 43 var correctColor = null;
michael@0 44 var value;
michael@0 45
michael@0 46 function init()
michael@0 47 {
michael@0 48 if (window.initNonKhronosFramework) {
michael@0 49 window.initNonKhronosFramework(true);
michael@0 50 }
michael@0 51
michael@0 52 description('Verify WebGLContextAttributes are working as specified, including alpha, depth, stencil, antialias, but not premultipliedAlpha');
michael@0 53
michael@0 54 runTest();
michael@0 55 }
michael@0 56
michael@0 57 function getWebGL(canvasWidth, canvasHeight, contextAttribs, clearColor, clearDepth, clearStencil)
michael@0 58 {
michael@0 59 var canvas = document.createElement("canvas");
michael@0 60 if (!canvas)
michael@0 61 return null;
michael@0 62 canvas.width = canvasWidth;
michael@0 63 canvas.height = canvasHeight;
michael@0 64
michael@0 65 var context = create3DContext(canvas, contextAttribs);
michael@0 66 if (!context)
michael@0 67 return null;
michael@0 68
michael@0 69 context.program = createProgram(context, "vshader", "fshader", ["pos", "colorIn"]);
michael@0 70 if (!context.program)
michael@0 71 return null;
michael@0 72
michael@0 73 context.useProgram(context.program);
michael@0 74
michael@0 75 context.enable(context.DEPTH_TEST);
michael@0 76 context.enable(context.STENCIL_TEST);
michael@0 77 context.disable(context.BLEND);
michael@0 78
michael@0 79 context.clearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
michael@0 80 context.clearDepth(clearDepth);
michael@0 81 context.clearStencil(clearStencil);
michael@0 82 context.clear(context.COLOR_BUFFER_BIT | context.DEPTH_BUFFER_BIT | context.STENCIL_BUFFER_BIT);
michael@0 83
michael@0 84 return context;
michael@0 85 }
michael@0 86
michael@0 87 function drawAndReadPixel(gl, vertices, colors, x, y)
michael@0 88 {
michael@0 89 var colorOffset = vertices.byteLength;
michael@0 90
michael@0 91 var vbo = gl.createBuffer();
michael@0 92 gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
michael@0 93 gl.bufferData(gl.ARRAY_BUFFER, colorOffset + colors.byteLength, gl.STATIC_DRAW);
michael@0 94 gl.bufferSubData(gl.ARRAY_BUFFER, 0, vertices);
michael@0 95 gl.bufferSubData(gl.ARRAY_BUFFER, colorOffset, colors);
michael@0 96
michael@0 97 gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
michael@0 98 gl.enableVertexAttribArray(0);
michael@0 99 gl.vertexAttribPointer(1, 4, gl.UNSIGNED_BYTE, true, 0, colorOffset);
michael@0 100 gl.enableVertexAttribArray(1);
michael@0 101
michael@0 102 gl.drawArrays(gl.TRIANGLES, 0, vertices.length / 3);
michael@0 103
michael@0 104 var buf = new Uint8Array(1 * 1 * 4);
michael@0 105 gl.readPixels(x, y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, buf);
michael@0 106 return buf;
michael@0 107 }
michael@0 108
michael@0 109 function testAlpha(alpha)
michael@0 110 {
michael@0 111 debug("Testing alpha = " + alpha);
michael@0 112 if (alpha)
michael@0 113 shouldBeNonNull("webGL = getWebGL(1, 1, { alpha: true, depth: false, stencil: false, antialias: false }, [ 0, 0, 0, 0 ], 1, 0)");
michael@0 114 else
michael@0 115 shouldBeNonNull("webGL = getWebGL(1, 1, { alpha: false, depth: false, stencil: false, antialias: false }, [ 0, 0, 0, 0 ], 1, 0)");
michael@0 116 shouldBeNonNull("contextAttribs = webGL.getContextAttributes()");
michael@0 117 shouldBeTrue("contextAttribs.alpha == " + alpha);
michael@0 118
michael@0 119 var buf = new Uint8Array(1 * 1 * 4);
michael@0 120 webGL.readPixels(0, 0, 1, 1, webGL.RGBA, webGL.UNSIGNED_BYTE, buf);
michael@0 121 pixel[0] = buf[0];
michael@0 122 pixel[1] = buf[1];
michael@0 123 pixel[2] = buf[2];
michael@0 124 pixel[3] = buf[3];
michael@0 125 correctColor = (contextAttribs.alpha ? [0, 0, 0, 0] : [0, 0, 0, 255]);
michael@0 126 shouldBe("pixel", "correctColor");
michael@0 127 }
michael@0 128
michael@0 129 function testDepth(depth)
michael@0 130 {
michael@0 131 debug("Testing depth = " + depth);
michael@0 132 if (depth)
michael@0 133 shouldBeNonNull("webGL = getWebGL(1, 1, { stencil: false, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)");
michael@0 134 else
michael@0 135 shouldBeNonNull("webGL = getWebGL(1, 1, { depth: false, stencil: false, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)");
michael@0 136 shouldBeNonNull("contextAttribs = webGL.getContextAttributes()");
michael@0 137
michael@0 138 webGL.depthFunc(webGL.NEVER);
michael@0 139
michael@0 140 var vertices = new Float32Array([
michael@0 141 1.0, 1.0, 0.0,
michael@0 142 -1.0, 1.0, 0.0,
michael@0 143 -1.0, -1.0, 0.0,
michael@0 144 1.0, 1.0, 0.0,
michael@0 145 -1.0, -1.0, 0.0,
michael@0 146 1.0, -1.0, 0.0]);
michael@0 147 var colors = new Uint8Array([
michael@0 148 255, 0, 0, 255,
michael@0 149 255, 0, 0, 255,
michael@0 150 255, 0, 0, 255,
michael@0 151 255, 0, 0, 255,
michael@0 152 255, 0, 0, 255,
michael@0 153 255, 0, 0, 255]);
michael@0 154
michael@0 155 var buf = drawAndReadPixel(webGL, vertices, colors, 0, 0);
michael@0 156 pixel[0] = buf[0];
michael@0 157 pixel[1] = buf[1];
michael@0 158 pixel[2] = buf[2];
michael@0 159 pixel[3] = buf[3];
michael@0 160 correctColor = (contextAttribs.depth ? [0, 0, 0, 255] : [255, 0, 0, 255]);
michael@0 161 shouldBe("pixel", "correctColor");
michael@0 162 }
michael@0 163
michael@0 164 function testStencil(stencil)
michael@0 165 {
michael@0 166 debug("Testing stencil = " + stencil);
michael@0 167 if (stencil)
michael@0 168 shouldBeNonNull("webGL = getWebGL(1, 1, { depth: false, stencil: true, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)");
michael@0 169 else
michael@0 170 shouldBeNonNull("webGL = getWebGL(1, 1, { depth: false, stencil: false, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)");
michael@0 171 shouldBeNonNull("contextAttribs = webGL.getContextAttributes()");
michael@0 172
michael@0 173 webGL.depthFunc(webGL.ALWAYS);
michael@0 174
michael@0 175 webGL.stencilFunc(webGL.NEVER, 1, 1);
michael@0 176 webGL.stencilOp(webGL.KEEP, webGL.KEEP, webGL.KEEP);
michael@0 177
michael@0 178 var vertices = new Float32Array([
michael@0 179 1.0, 1.0, 0.0,
michael@0 180 -1.0, 1.0, 0.0,
michael@0 181 -1.0, -1.0, 0.0,
michael@0 182 1.0, 1.0, 0.0,
michael@0 183 -1.0, -1.0, 0.0,
michael@0 184 1.0, -1.0, 0.0]);
michael@0 185 var colors = new Uint8Array([
michael@0 186 255, 0, 0, 255,
michael@0 187 255, 0, 0, 255,
michael@0 188 255, 0, 0, 255,
michael@0 189 255, 0, 0, 255,
michael@0 190 255, 0, 0, 255,
michael@0 191 255, 0, 0, 255]);
michael@0 192
michael@0 193 var buf = drawAndReadPixel(webGL, vertices, colors, 0, 0);
michael@0 194 pixel[0] = buf[0];
michael@0 195 pixel[1] = buf[1];
michael@0 196 pixel[2] = buf[2];
michael@0 197 pixel[3] = buf[3];
michael@0 198 correctColor = (contextAttribs.stencil ? [0, 0, 0, 255] : [255, 0, 0, 255]);
michael@0 199 shouldBe("pixel", "correctColor");
michael@0 200 }
michael@0 201
michael@0 202 function testAntialias(antialias)
michael@0 203 {
michael@0 204 debug("Testing antialias = " + antialias);
michael@0 205 if (antialias)
michael@0 206 shouldBeNonNull("webGL = getWebGL(2, 2, { depth: false, stencil: false, alpha: false, antialias: true }, [ 0, 0, 0, 1 ], 1, 0)");
michael@0 207 else
michael@0 208 shouldBeNonNull("webGL = getWebGL(2, 2, { depth: false, stencil: false, alpha: false, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)");
michael@0 209 shouldBeNonNull("contextAttribs = webGL.getContextAttributes()");
michael@0 210
michael@0 211 var vertices = new Float32Array([
michael@0 212 1.0, 1.0, 0.0,
michael@0 213 -1.0, 1.0, 0.0,
michael@0 214 -1.0, -1.0, 0.0]);
michael@0 215 var colors = new Uint8Array([
michael@0 216 255, 0, 0, 255,
michael@0 217 255, 0, 0, 255,
michael@0 218 255, 0, 0, 255]);
michael@0 219 var buf = drawAndReadPixel(webGL, vertices, colors, 0, 0);
michael@0 220 pixel[0] = buf[0];
michael@0 221 shouldBe("pixel[0] != 255 && pixel[0] != 0", "contextAttribs.antialias");
michael@0 222 }
michael@0 223
michael@0 224 function runTest()
michael@0 225 {
michael@0 226
michael@0 227 testAlpha(true);
michael@0 228 testAlpha(false);
michael@0 229 testDepth(true);
michael@0 230 testDepth(false);
michael@0 231 testStencil(true);
michael@0 232 testStencil(false);
michael@0 233 testAntialias(true);
michael@0 234 testAntialias(false);
michael@0 235
michael@0 236 finishTest()
michael@0 237 }
michael@0 238
michael@0 239 </script>
michael@0 240 </head>
michael@0 241 <body onload="init()">
michael@0 242 <div id="description"></div>
michael@0 243 <div id="console"></div>
michael@0 244 </body>
michael@0 245 </html>

mercurial