1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/canvas/test/webgl-conformance/conformance/context/context-attributes-alpha-depth-stencil-antialias.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,245 @@ 1.4 +<!-- 1.5 +Copyright (c) 2011 The Chromium Authors. All rights reserved. 1.6 +Use of this source code is governed by a BSD-style license that can be 1.7 +found in the LICENSE file. 1.8 + --> 1.9 +<!DOCTYPE html> 1.10 +<html> 1.11 +<head> 1.12 +<meta charset="utf-8"> 1.13 +<link rel="stylesheet" href="../../resources/js-test-style.css"/> 1.14 +<script src="../../resources/js-test-pre.js"></script> 1.15 +<script src="../resources/webgl-test.js"></script> 1.16 +<script id="vshader" type="x-shader/x-vertex"> 1.17 +attribute vec3 pos; 1.18 +attribute vec4 colorIn; 1.19 +varying vec4 color; 1.20 + 1.21 +void main() 1.22 +{ 1.23 + color = colorIn; 1.24 + gl_Position = vec4(pos.xyz, 1.0); 1.25 +} 1.26 +</script> 1.27 + 1.28 +<script id="fshader" type="x-shader/x-fragment"> 1.29 +precision mediump float; 1.30 + 1.31 +varying vec4 color; 1.32 + 1.33 +void main() 1.34 +{ 1.35 + gl_FragColor = color; 1.36 +} 1.37 +</script> 1.38 + 1.39 +<script> 1.40 +var successfullyParsed = false; 1.41 + 1.42 +// These four declarations need to be global for "shouldBe" to see them 1.43 +var webGL = null; 1.44 +var contextAttribs = null; 1.45 +var pixel = [0, 0, 0, 1]; 1.46 +var correctColor = null; 1.47 +var value; 1.48 + 1.49 +function init() 1.50 +{ 1.51 + if (window.initNonKhronosFramework) { 1.52 + window.initNonKhronosFramework(true); 1.53 + } 1.54 + 1.55 + description('Verify WebGLContextAttributes are working as specified, including alpha, depth, stencil, antialias, but not premultipliedAlpha'); 1.56 + 1.57 + runTest(); 1.58 +} 1.59 + 1.60 +function getWebGL(canvasWidth, canvasHeight, contextAttribs, clearColor, clearDepth, clearStencil) 1.61 +{ 1.62 + var canvas = document.createElement("canvas"); 1.63 + if (!canvas) 1.64 + return null; 1.65 + canvas.width = canvasWidth; 1.66 + canvas.height = canvasHeight; 1.67 + 1.68 + var context = create3DContext(canvas, contextAttribs); 1.69 + if (!context) 1.70 + return null; 1.71 + 1.72 + context.program = createProgram(context, "vshader", "fshader", ["pos", "colorIn"]); 1.73 + if (!context.program) 1.74 + return null; 1.75 + 1.76 + context.useProgram(context.program); 1.77 + 1.78 + context.enable(context.DEPTH_TEST); 1.79 + context.enable(context.STENCIL_TEST); 1.80 + context.disable(context.BLEND); 1.81 + 1.82 + context.clearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]); 1.83 + context.clearDepth(clearDepth); 1.84 + context.clearStencil(clearStencil); 1.85 + context.clear(context.COLOR_BUFFER_BIT | context.DEPTH_BUFFER_BIT | context.STENCIL_BUFFER_BIT); 1.86 + 1.87 + return context; 1.88 +} 1.89 + 1.90 +function drawAndReadPixel(gl, vertices, colors, x, y) 1.91 +{ 1.92 + var colorOffset = vertices.byteLength; 1.93 + 1.94 + var vbo = gl.createBuffer(); 1.95 + gl.bindBuffer(gl.ARRAY_BUFFER, vbo); 1.96 + gl.bufferData(gl.ARRAY_BUFFER, colorOffset + colors.byteLength, gl.STATIC_DRAW); 1.97 + gl.bufferSubData(gl.ARRAY_BUFFER, 0, vertices); 1.98 + gl.bufferSubData(gl.ARRAY_BUFFER, colorOffset, colors); 1.99 + 1.100 + gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0); 1.101 + gl.enableVertexAttribArray(0); 1.102 + gl.vertexAttribPointer(1, 4, gl.UNSIGNED_BYTE, true, 0, colorOffset); 1.103 + gl.enableVertexAttribArray(1); 1.104 + 1.105 + gl.drawArrays(gl.TRIANGLES, 0, vertices.length / 3); 1.106 + 1.107 + var buf = new Uint8Array(1 * 1 * 4); 1.108 + gl.readPixels(x, y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, buf); 1.109 + return buf; 1.110 +} 1.111 + 1.112 +function testAlpha(alpha) 1.113 +{ 1.114 + debug("Testing alpha = " + alpha); 1.115 + if (alpha) 1.116 + shouldBeNonNull("webGL = getWebGL(1, 1, { alpha: true, depth: false, stencil: false, antialias: false }, [ 0, 0, 0, 0 ], 1, 0)"); 1.117 + else 1.118 + shouldBeNonNull("webGL = getWebGL(1, 1, { alpha: false, depth: false, stencil: false, antialias: false }, [ 0, 0, 0, 0 ], 1, 0)"); 1.119 + shouldBeNonNull("contextAttribs = webGL.getContextAttributes()"); 1.120 + shouldBeTrue("contextAttribs.alpha == " + alpha); 1.121 + 1.122 + var buf = new Uint8Array(1 * 1 * 4); 1.123 + webGL.readPixels(0, 0, 1, 1, webGL.RGBA, webGL.UNSIGNED_BYTE, buf); 1.124 + pixel[0] = buf[0]; 1.125 + pixel[1] = buf[1]; 1.126 + pixel[2] = buf[2]; 1.127 + pixel[3] = buf[3]; 1.128 + correctColor = (contextAttribs.alpha ? [0, 0, 0, 0] : [0, 0, 0, 255]); 1.129 + shouldBe("pixel", "correctColor"); 1.130 +} 1.131 + 1.132 +function testDepth(depth) 1.133 +{ 1.134 + debug("Testing depth = " + depth); 1.135 + if (depth) 1.136 + shouldBeNonNull("webGL = getWebGL(1, 1, { stencil: false, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)"); 1.137 + else 1.138 + shouldBeNonNull("webGL = getWebGL(1, 1, { depth: false, stencil: false, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)"); 1.139 + shouldBeNonNull("contextAttribs = webGL.getContextAttributes()"); 1.140 + 1.141 + webGL.depthFunc(webGL.NEVER); 1.142 + 1.143 + var vertices = new Float32Array([ 1.144 + 1.0, 1.0, 0.0, 1.145 + -1.0, 1.0, 0.0, 1.146 + -1.0, -1.0, 0.0, 1.147 + 1.0, 1.0, 0.0, 1.148 + -1.0, -1.0, 0.0, 1.149 + 1.0, -1.0, 0.0]); 1.150 + var colors = new Uint8Array([ 1.151 + 255, 0, 0, 255, 1.152 + 255, 0, 0, 255, 1.153 + 255, 0, 0, 255, 1.154 + 255, 0, 0, 255, 1.155 + 255, 0, 0, 255, 1.156 + 255, 0, 0, 255]); 1.157 + 1.158 + var buf = drawAndReadPixel(webGL, vertices, colors, 0, 0); 1.159 + pixel[0] = buf[0]; 1.160 + pixel[1] = buf[1]; 1.161 + pixel[2] = buf[2]; 1.162 + pixel[3] = buf[3]; 1.163 + correctColor = (contextAttribs.depth ? [0, 0, 0, 255] : [255, 0, 0, 255]); 1.164 + shouldBe("pixel", "correctColor"); 1.165 +} 1.166 + 1.167 +function testStencil(stencil) 1.168 +{ 1.169 + debug("Testing stencil = " + stencil); 1.170 + if (stencil) 1.171 + shouldBeNonNull("webGL = getWebGL(1, 1, { depth: false, stencil: true, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)"); 1.172 + else 1.173 + shouldBeNonNull("webGL = getWebGL(1, 1, { depth: false, stencil: false, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)"); 1.174 + shouldBeNonNull("contextAttribs = webGL.getContextAttributes()"); 1.175 + 1.176 + webGL.depthFunc(webGL.ALWAYS); 1.177 + 1.178 + webGL.stencilFunc(webGL.NEVER, 1, 1); 1.179 + webGL.stencilOp(webGL.KEEP, webGL.KEEP, webGL.KEEP); 1.180 + 1.181 + var vertices = new Float32Array([ 1.182 + 1.0, 1.0, 0.0, 1.183 + -1.0, 1.0, 0.0, 1.184 + -1.0, -1.0, 0.0, 1.185 + 1.0, 1.0, 0.0, 1.186 + -1.0, -1.0, 0.0, 1.187 + 1.0, -1.0, 0.0]); 1.188 + var colors = new Uint8Array([ 1.189 + 255, 0, 0, 255, 1.190 + 255, 0, 0, 255, 1.191 + 255, 0, 0, 255, 1.192 + 255, 0, 0, 255, 1.193 + 255, 0, 0, 255, 1.194 + 255, 0, 0, 255]); 1.195 + 1.196 + var buf = drawAndReadPixel(webGL, vertices, colors, 0, 0); 1.197 + pixel[0] = buf[0]; 1.198 + pixel[1] = buf[1]; 1.199 + pixel[2] = buf[2]; 1.200 + pixel[3] = buf[3]; 1.201 + correctColor = (contextAttribs.stencil ? [0, 0, 0, 255] : [255, 0, 0, 255]); 1.202 + shouldBe("pixel", "correctColor"); 1.203 +} 1.204 + 1.205 +function testAntialias(antialias) 1.206 +{ 1.207 + debug("Testing antialias = " + antialias); 1.208 + if (antialias) 1.209 + shouldBeNonNull("webGL = getWebGL(2, 2, { depth: false, stencil: false, alpha: false, antialias: true }, [ 0, 0, 0, 1 ], 1, 0)"); 1.210 + else 1.211 + shouldBeNonNull("webGL = getWebGL(2, 2, { depth: false, stencil: false, alpha: false, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)"); 1.212 + shouldBeNonNull("contextAttribs = webGL.getContextAttributes()"); 1.213 + 1.214 + var vertices = new Float32Array([ 1.215 + 1.0, 1.0, 0.0, 1.216 + -1.0, 1.0, 0.0, 1.217 + -1.0, -1.0, 0.0]); 1.218 + var colors = new Uint8Array([ 1.219 + 255, 0, 0, 255, 1.220 + 255, 0, 0, 255, 1.221 + 255, 0, 0, 255]); 1.222 + var buf = drawAndReadPixel(webGL, vertices, colors, 0, 0); 1.223 + pixel[0] = buf[0]; 1.224 + shouldBe("pixel[0] != 255 && pixel[0] != 0", "contextAttribs.antialias"); 1.225 +} 1.226 + 1.227 +function runTest() 1.228 +{ 1.229 + 1.230 + testAlpha(true); 1.231 + testAlpha(false); 1.232 + testDepth(true); 1.233 + testDepth(false); 1.234 + testStencil(true); 1.235 + testStencil(false); 1.236 + testAntialias(true); 1.237 + testAntialias(false); 1.238 + 1.239 + finishTest() 1.240 +} 1.241 + 1.242 +</script> 1.243 +</head> 1.244 +<body onload="init()"> 1.245 +<div id="description"></div> 1.246 +<div id="console"></div> 1.247 +</body> 1.248 +</html>