|
47 | 47 | var fbHasColor; |
48 | 48 | var fbHasDepth; |
49 | 49 | var fbHasStencil; |
| 50 | +var contextVersion = wtu.getDefault3DContextVersion(); |
50 | 51 |
|
51 | 52 | function init() |
52 | 53 | { |
|
55 | 56 | runTest(); |
56 | 57 | } |
57 | 58 |
|
| 59 | +var vertices = new Float32Array([ |
| 60 | + 1.0, 1.0, 0.0, |
| 61 | + -1.0, 1.0, 0.0, |
| 62 | + -1.0, -1.0, 0.0, |
| 63 | + 1.0, 1.0, 0.0, |
| 64 | + -1.0, -1.0, 0.0, |
| 65 | + 1.0, -1.0, 0.0]); |
| 66 | + |
| 67 | +var colors = new Uint8Array([ |
| 68 | + 255, 0, 0, 255, |
| 69 | + 255, 0, 0, 255, |
| 70 | + 255, 0, 0, 255, |
| 71 | + 255, 0, 0, 255, |
| 72 | + 255, 0, 0, 255, |
| 73 | + 255, 0, 0, 255]); |
| 74 | + |
| 75 | + |
58 | 76 | function getWebGL(canvasWidth, canvasHeight, contextAttribs, clearColor, clearDepth, clearStencil) |
59 | 77 | { |
60 | 78 | var canvas = document.createElement("canvas"); |
|
63 | 81 | canvas.width = canvasWidth; |
64 | 82 | canvas.height = canvasHeight; |
65 | 83 |
|
66 | | - gl = wtu.create3DContext(canvas, contextAttribs); |
| 84 | + gl = wtu.create3DContext(canvas, contextAttribs, contextVersion); |
67 | 85 | if (!gl) |
68 | 86 | return null; |
69 | 87 |
|
|
102 | 120 | } |
103 | 121 | } |
104 | 122 | gl.bindFramebuffer(gl.FRAMEBUFFER, null); |
105 | | - wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors"); |
106 | | - |
107 | | - return gl; |
108 | | -} |
109 | 123 |
|
110 | | -function drawAndReadPixel(gl, vertices, colors) |
111 | | -{ |
112 | 124 | var colorOffset = vertices.byteLength; |
113 | | - |
114 | 125 | var vbo = gl.createBuffer(); |
115 | 126 | gl.bindBuffer(gl.ARRAY_BUFFER, vbo); |
116 | 127 | gl.bufferData(gl.ARRAY_BUFFER, colorOffset + colors.byteLength, gl.STATIC_DRAW); |
117 | 128 | gl.bufferSubData(gl.ARRAY_BUFFER, 0, vertices); |
118 | 129 | gl.bufferSubData(gl.ARRAY_BUFFER, colorOffset, colors); |
119 | | - |
120 | 130 | gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0); |
121 | 131 | gl.enableVertexAttribArray(0); |
122 | 132 | gl.vertexAttribPointer(1, 4, gl.UNSIGNED_BYTE, true, 0, colorOffset); |
123 | 133 | gl.enableVertexAttribArray(1); |
124 | 134 |
|
125 | | - gl.drawArrays(gl.TRIANGLES, 0, vertices.length / 3); |
| 135 | + wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors"); |
| 136 | + return gl; |
126 | 137 | } |
127 | 138 |
|
128 | | -function testDefault() |
| 139 | +function draw(gl, verticesCount) |
129 | 140 | { |
130 | | - debug("Testing default attributes: { stencil:false }"); |
131 | | - shouldBeNonNull("gl = getWebGL(1, 1, null, [ 0, 0, 0, 0 ], 1, 0)"); |
132 | | - shouldBeFalse("gl.getContextAttributes().stencil"); |
133 | | - shouldBeTrue("gl.getParameter(gl.STENCIL_BITS) == 0"); |
| 141 | + verticesCount = verticesCount || vertices.length / 3; |
| 142 | + gl.drawArrays(gl.TRIANGLES, 0, verticesCount); |
134 | 143 | } |
135 | 144 |
|
136 | | -function testAlpha(alpha) |
| 145 | +function checkDraw(hasAlpha, hasStencil, hasDepth, hasAntialias) |
137 | 146 | { |
138 | | - debug("Testing alpha = " + alpha); |
139 | | - if (alpha) { |
140 | | - shouldBeNonNull("gl = getWebGL(1, 1, { alpha: true, depth: false, stencil: false, antialias: false }, [ 0, 0, 0, 0 ], 1, 0)"); |
141 | | - shouldBeTrue("gl.getParameter(gl.ALPHA_BITS) >= 8"); |
142 | | - } else { |
143 | | - shouldBeNonNull("gl = getWebGL(1, 1, { alpha: false, depth: false, stencil: false, antialias: false }, [ 0, 0, 0, 0 ], 1, 0)"); |
144 | | - shouldBeTrue("gl.getParameter(gl.ALPHA_BITS) == 0"); |
145 | | - } |
146 | | - shouldBeTrue("gl.getParameter(gl.RED_BITS) >= 8"); |
147 | | - shouldBeTrue("gl.getParameter(gl.GREEN_BITS) >= 8"); |
148 | | - shouldBeTrue("gl.getParameter(gl.BLUE_BITS) >= 8"); |
149 | | - shouldBeTrue("gl.getParameter(gl.DEPTH_BITS) == 0"); |
150 | | - shouldBeTrue("gl.getParameter(gl.STENCIL_BITS) == 0"); |
151 | | - |
152 | | - shouldBeNonNull("contextAttribs = gl.getContextAttributes()"); |
153 | | - shouldBeTrue("contextAttribs.alpha == " + alpha); |
| 147 | + let red = [255, 0, 0, 255 ]; |
| 148 | + let black = [0, 0, 0, hasAlpha ? 0 : 255 ]; |
| 149 | + debug(`Testing that stencil ${ hasStencil ? 'affects': 'does not affect'} the rendering.`); |
| 150 | + gl.stencilFunc(gl.NEVER, 1, 1); |
| 151 | + gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP); |
| 152 | + draw(gl); |
| 153 | + correctColor = hasStencil ? black : red; |
| 154 | + wtu.checkCanvas(gl, correctColor) |
| 155 | + gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT); |
| 156 | + wtu.checkCanvas(gl, black); |
| 157 | + gl.stencilFunc(gl.ALWAYS, 1, 1); |
154 | 158 |
|
155 | | - var correctColor = (contextAttribs.alpha ? [0, 0, 0, 0] : [0, 0, 0, 255]); |
156 | | - wtu.checkCanvasRect(gl, 0, 0, 1, 1, correctColor); |
| 159 | + debug(`Testing that depth ${ hasDepth ? 'affects': 'does not affect'} the rendering.`); |
| 160 | + gl.depthFunc(gl.NEVER); |
| 161 | + draw(gl); |
| 162 | + correctColor = hasDepth ? black : red; |
| 163 | + wtu.checkCanvas(gl, correctColor); |
| 164 | + gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT); |
| 165 | + wtu.checkCanvas(gl, black); |
| 166 | + gl.depthFunc(gl.ALWAYS); |
157 | 167 |
|
158 | | - if (fbHasColor) { |
159 | | - gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer); |
160 | | - gl.clearColor(0.5, 0.5, 0.5, 0.5); |
161 | | - gl.clear(gl.COLOR_BUFFER_BIT); |
162 | | - wtu.checkCanvasRect(gl, 0, 0, 1, 1, [127, 127, 127, 127], undefined, 1); |
163 | | - gl.bindFramebuffer(gl.FRAMEBUFFER, null); |
| 168 | + debug(`Testing that rendering is ${hasAntialias ? 'antialiased' : 'aliased'}.`); |
| 169 | + draw(gl, 3); |
| 170 | + let N = 2; |
| 171 | + let buf = new Uint8Array(N * N * 4); |
| 172 | + gl.readPixels(0, 0, N, N, gl.RGBA, gl.UNSIGNED_BYTE, buf); |
| 173 | + redChannels[0] = buf[4 * (N + 1)]; // (1, 1) |
| 174 | + redChannels[1] = buf[4 * N * (N - 1)]; // left top |
| 175 | + redChannels[2] = buf[4 * (N - 1)]; // right bottom |
| 176 | + shouldBe("redChannels[1]", "255"); |
| 177 | + shouldBe("redChannels[2]", "0"); |
| 178 | + if (hasAntialias) { |
| 179 | + shouldNotBe("redChannels[0]", "255"); |
| 180 | + shouldNotBe("redChannels[0]", "0"); |
| 181 | + } else { |
| 182 | + shouldBeTrue("redChannels[0] == 255 || redChannels[0] == 0"); |
164 | 183 | } |
| 184 | + gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT); |
| 185 | + wtu.checkCanvas(gl, black); |
| 186 | + |
| 187 | + debug("Testing that rendering works."); |
| 188 | + draw(gl); |
| 189 | + wtu.checkCanvas(gl, red); |
| 190 | + gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT); |
| 191 | + wtu.checkCanvas(gl, black); |
165 | 192 | } |
166 | 193 |
|
167 | | -function testDepth(depth) |
| 194 | +function testDefault() |
168 | 195 | { |
169 | | - debug("Testing depth = " + depth); |
170 | | - if (depth) { |
171 | | - shouldBeNonNull("gl = getWebGL(1, 1, { stencil: false, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)"); |
172 | | - shouldBeTrue("gl.getParameter(gl.DEPTH_BITS) >= 16"); |
173 | | - } else { |
174 | | - shouldBeNonNull("gl = getWebGL(1, 1, { depth: false, stencil: false, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)"); |
175 | | - shouldBeTrue("gl.getParameter(gl.DEPTH_BITS) == 0"); |
176 | | - } |
177 | | - shouldBeTrue("gl.getParameter(gl.RED_BITS) >= 8"); |
178 | | - shouldBeTrue("gl.getParameter(gl.GREEN_BITS) >= 8"); |
179 | | - shouldBeTrue("gl.getParameter(gl.BLUE_BITS) >= 8"); |
180 | | - shouldBeTrue("gl.getParameter(gl.ALPHA_BITS) >= 8"); |
| 196 | + debug("Testing default attributes: { stencil:false }"); |
| 197 | + shouldBeNonNull("gl = getWebGL(1, 1, null, [ 0, 0, 0, 0 ], 1, 0)"); |
| 198 | + shouldBeFalse("gl.getContextAttributes().stencil"); |
| 199 | + shouldBe("gl.getParameter(gl.STENCIL_BITS)", "0"); |
| 200 | +} |
181 | 201 |
|
| 202 | +function testAttributesAffectContext(alpha, stencil, depth, antialias) |
| 203 | +{ |
| 204 | + shouldBeNonNull(`gl = getWebGL(2, 2, { depth: ${depth}, stencil: ${stencil}, antialias: ${antialias}, alpha: ${alpha} }, [ 0, 0, 0, 0 ], 1, 0)`); |
182 | 205 | shouldBeNonNull("contextAttribs = gl.getContextAttributes()"); |
183 | 206 |
|
184 | | - gl.depthFunc(gl.NEVER); |
185 | | - |
186 | | - var vertices = new Float32Array([ |
187 | | - 1.0, 1.0, 0.0, |
188 | | - -1.0, 1.0, 0.0, |
189 | | - -1.0, -1.0, 0.0, |
190 | | - 1.0, 1.0, 0.0, |
191 | | - -1.0, -1.0, 0.0, |
192 | | - 1.0, -1.0, 0.0]); |
193 | | - var colors = new Uint8Array([ |
194 | | - 255, 0, 0, 255, |
195 | | - 255, 0, 0, 255, |
196 | | - 255, 0, 0, 255, |
197 | | - 255, 0, 0, 255, |
198 | | - 255, 0, 0, 255, |
199 | | - 255, 0, 0, 255]); |
200 | | - |
201 | | - drawAndReadPixel(gl, vertices, colors, 0, 0); |
202 | | - correctColor = (contextAttribs.depth ? [0, 0, 0, 255] : [255, 0, 0, 255]); |
203 | | - wtu.checkCanvasRect(gl, 0, 0, 1, 1, correctColor); |
204 | | - |
205 | | - if (fbHasDepth) { |
206 | | - gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer); |
207 | | - gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
208 | | - drawAndReadPixel(gl, vertices, colors, 0, 0); |
209 | | - wtu.checkCanvasRect(gl, 0, 0, 1, 1, [0, 0, 0, 255]); |
210 | | - gl.bindFramebuffer(gl.FRAMEBUFFER, null); |
| 207 | + shouldBeGreaterThanOrEqual("gl.getParameter(gl.RED_BITS)", "8"); |
| 208 | + shouldBeGreaterThanOrEqual("gl.getParameter(gl.GREEN_BITS)", "8"); |
| 209 | + shouldBeGreaterThanOrEqual("gl.getParameter(gl.BLUE_BITS)", "8"); |
| 210 | + |
| 211 | + shouldBe("contextAttribs.alpha", "" + alpha); |
| 212 | + if (contextVersion < 2) { |
| 213 | + if (!stencil) |
| 214 | + shouldBeFalse("contextAttribs.stencil"); |
| 215 | + else |
| 216 | + stencil = contextAttribs.stencil; |
| 217 | + if (!depth) |
| 218 | + shouldBeFalse("contextAttribs.depth"); |
| 219 | + else |
| 220 | + depth = contextAttribs.depth; |
| 221 | + if (!antialias) |
| 222 | + shouldBeFalse("contextAttribs.antialias"); |
| 223 | + else |
| 224 | + antialias = contextAttribs.antialias; |
| 225 | + } else { |
| 226 | + shouldBe("contextAttribs.stencil", "" + stencil); |
| 227 | + shouldBe("contextAttribs.depth", "" + depth); |
| 228 | + shouldBe("contextAttribs.antialias", "" + antialias); |
211 | 229 | } |
212 | | -} |
213 | 230 |
|
214 | | -function testStencilAndDepth(stencil, depth) |
215 | | -{ |
216 | | - debug("Testing stencil = " + stencil + ", depth = " + depth); |
217 | | - var creationString = |
218 | | - "gl = getWebGL(1, 1, { depth: " + depth + ", stencil: " + stencil + ", antialias: false }, [ 0, 0, 0, 1 ], 1, 0)"; |
219 | | - shouldBeNonNull(creationString); |
220 | | - |
221 | | - shouldBeTrue("gl.getParameter(gl.RED_BITS) >= 8"); |
222 | | - shouldBeTrue("gl.getParameter(gl.GREEN_BITS) >= 8"); |
223 | | - shouldBeTrue("gl.getParameter(gl.BLUE_BITS) >= 8"); |
224 | | - shouldBeTrue("gl.getParameter(gl.ALPHA_BITS) >= 8"); |
225 | | - if (depth) |
226 | | - shouldBeTrue("gl.getParameter(gl.DEPTH_BITS) >= 16"); |
| 231 | + if (alpha) |
| 232 | + shouldBeGreaterThanOrEqual("gl.getParameter(gl.ALPHA_BITS)", "8"); |
227 | 233 | else |
228 | | - shouldBeTrue("gl.getParameter(gl.DEPTH_BITS) == 0"); |
229 | | - |
| 234 | + shouldBe("gl.getParameter(gl.ALPHA_BITS)", "0"); |
230 | 235 | if (stencil) |
231 | | - shouldBeTrue("gl.getParameter(gl.STENCIL_BITS) >= 8"); |
| 236 | + shouldBeGreaterThanOrEqual("gl.getParameter(gl.STENCIL_BITS)", "8"); |
232 | 237 | else |
233 | | - shouldBeTrue("gl.getParameter(gl.STENCIL_BITS) == 0"); |
234 | | - |
235 | | - shouldBeNonNull("contextAttribs = gl.getContextAttributes()"); |
236 | | - if (!depth && contextAttribs.depth) { |
237 | | - testFailed("WebGL implementation provided a depth buffer when it should not have"); |
238 | | - } |
239 | | - if (!contextAttribs.depth) |
240 | | - depth = false; |
241 | | - if (!stencil && contextAttribs.stencil) { |
242 | | - testFailed("WebGL implementation provided a stencil buffer when it should not have"); |
243 | | - } |
244 | | - if (!contextAttribs.stencil) |
245 | | - stencil = false; |
| 238 | + shouldBe("gl.getParameter(gl.STENCIL_BITS)", "0"); |
| 239 | + if (depth) |
| 240 | + shouldBeGreaterThanOrEqual("gl.getParameter(gl.DEPTH_BITS)", "16"); |
| 241 | + else |
| 242 | + shouldBe("gl.getParameter(gl.DEPTH_BITS)", "0"); |
246 | 243 |
|
247 | | - gl.depthFunc(gl.ALWAYS); |
| 244 | + var correctColor = alpha ? [0, 0, 0, 0] : [0, 0, 0, 255]; |
| 245 | + wtu.checkCanvas(gl, correctColor); |
248 | 246 |
|
249 | | - gl.stencilFunc(gl.NEVER, 1, 1); |
250 | | - gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP); |
| 247 | + debug("Testing default framebuffer."); |
| 248 | + checkDraw(alpha, stencil, depth, antialias); |
251 | 249 |
|
252 | | - var vertices = new Float32Array([ |
253 | | - 1.0, 1.0, 0.0, |
254 | | - -1.0, 1.0, 0.0, |
255 | | - -1.0, -1.0, 0.0, |
256 | | - 1.0, 1.0, 0.0, |
257 | | - -1.0, -1.0, 0.0, |
258 | | - 1.0, -1.0, 0.0]); |
259 | | - var colors = new Uint8Array([ |
260 | | - 255, 0, 0, 255, |
261 | | - 255, 0, 0, 255, |
262 | | - 255, 0, 0, 255, |
263 | | - 255, 0, 0, 255, |
264 | | - 255, 0, 0, 255, |
265 | | - 255, 0, 0, 255]); |
266 | | - |
267 | | - drawAndReadPixel(gl, vertices, colors, 0, 0); |
268 | | - correctColor = (stencil ? [0, 0, 0, 255] : [255, 0, 0, 255]); |
269 | | - wtu.checkCanvasRect(gl, 0, 0, 1, 1, correctColor) |
270 | | - |
271 | | - if (fbHasStencil) { |
272 | | - gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer); |
273 | | - gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
274 | | - drawAndReadPixel(gl, vertices, colors, 0, 0); |
275 | | - wtu.checkCanvasRect(gl, 0, 0, 1, 1, [0, 0, 0, 255]); |
276 | | - gl.bindFramebuffer(gl.FRAMEBUFFER, null); |
| 250 | + if (fbHasColor) { |
| 251 | + debug("Testing bound framebuffer object."); |
| 252 | + gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer); |
| 253 | + gl.clearColor(0, 0, 0, 0); |
| 254 | + gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT); |
| 255 | + checkDraw(true, fbHasStencil, fbHasDepth, false); |
| 256 | + gl.bindFramebuffer(gl.FRAMEBUFFER, null); |
277 | 257 | } |
278 | 258 | } |
279 | 259 |
|
280 | | -function testAntialias(antialias) |
281 | | -{ |
282 | | - debug("Testing antialias = " + antialias); |
283 | | - // Both the width and height of canvas are N. |
284 | | - var N = 2; |
285 | | - if (antialias) |
286 | | - shouldBeNonNull("gl = getWebGL(" + N + ", " + N + ", { depth: false, stencil: false, alpha: false, antialias: true }, [ 0, 0, 0, 1 ], 1, 0)"); |
287 | | - else |
288 | | - shouldBeNonNull("gl = getWebGL(" + N + ", " + N + ", { depth: false, stencil: false, alpha: false, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)"); |
289 | | - shouldBeNonNull("contextAttribs = gl.getContextAttributes()"); |
290 | | - |
291 | | - var vertices = new Float32Array([ |
292 | | - 1.0, 1.0, 0.0, |
293 | | - -1.0, 1.0, 0.0, |
294 | | - -1.0, -1.0, 0.0]); |
295 | | - var colors = new Uint8Array([ |
296 | | - 255, 0, 0, 255, |
297 | | - 255, 0, 0, 255, |
298 | | - 255, 0, 0, 255]); |
299 | | - drawAndReadPixel(gl, vertices, colors, 0, 0); |
300 | | - var buf = new Uint8Array(N * N * 4); |
301 | | - gl.readPixels(0, 0, N, N, gl.RGBA, gl.UNSIGNED_BYTE, buf); |
302 | | - redChannels[0] = buf[4 * (N + 1)]; // (1, 1) |
303 | | - redChannels[1] = buf[4 * N * (N - 1)]; // left top |
304 | | - redChannels[2] = buf[4 * (N - 1)]; // right bottom |
305 | | - shouldBeTrue("redChannels[1] == 255 && redChannels[2] == 0"); |
306 | | - shouldBe("redChannels[0] != 255 && redChannels[0] != 0", "contextAttribs.antialias"); |
307 | | -} |
308 | | - |
309 | 260 | function runTest() |
310 | 261 | { |
311 | 262 | testDefault(); |
312 | | - testAlpha(true); |
313 | | - testAlpha(false); |
314 | | - testDepth(true); |
315 | | - testDepth(false); |
316 | | - testStencilAndDepth(true, false); |
317 | | - testStencilAndDepth(false, false); |
318 | | - testStencilAndDepth(true, true); |
319 | | - testStencilAndDepth(false, true); |
320 | | - testAntialias(true); |
321 | | - testAntialias(false); |
322 | | - |
| 263 | + let cases = [false, true]; |
| 264 | + for (let alpha of cases) { |
| 265 | + for (let stencil of cases) { |
| 266 | + for (let depth of cases) { |
| 267 | + for (let antialias of cases) { |
| 268 | + testAttributesAffectContext(alpha, stencil, depth, antialias); |
| 269 | + } |
| 270 | + } |
| 271 | + } |
| 272 | + } |
323 | 273 | finishTest(); |
324 | 274 | } |
325 | | - |
326 | 275 | </script> |
327 | 276 | </head> |
328 | 277 | <body onload="init()"> |
|
0 commit comments