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