Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(405)

Side by Side Diff: third_party/webgl/sdk/tests/conformance/ogles/ogles-utils.js

Issue 10399113: Roll webgl conformance tests to r17874: part 2, adding r17874 (Closed) Base URL: svn://chrome-svn/chrome/trunk/deps/
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 /*
2 ** Copyright (c) 2012 The Khronos Group Inc.
3 **
4 ** Permission is hereby granted, free of charge, to any person obtaining a
5 ** copy of this software and/or associated documentation files (the
6 ** "Materials"), to deal in the Materials without restriction, including
7 ** without limitation the rights to use, copy, modify, merge, publish,
8 ** distribute, sublicense, and/or sell copies of the Materials, and to
9 ** permit persons to whom the Materials are furnished to do so, subject to
10 ** the following conditions:
11 **
12 ** The above copyright notice and this permission notice shall be included
13 ** in all copies or substantial portions of the Materials.
14 **
15 ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
22 */
23 OpenGLESTestRunner = (function(){
24 var wtu = WebGLTestUtils;
25 var gl;
26
27 var HALF_GRID_MAX_SIZE = 32;
28 var KNOWN_ATTRIBS = [
29 "gtf_Vertex",
30 "gtf_Color"
31 ];
32
33 var GTFPIXELTOLERANCE = 24;
34 var GTFACCEPTABLEFAILURECONT = 10;
35 var GTFAMDPIXELTOLERANCE = 12;
36 var GTFSCORETOLERANCE = 0.65;
37 var GTFNCCTOLARANCEZERO = 0.25;
38 var GTFKERNALSIZE = 5;
39
40 function log(msg) {
41 // debug(msg);
42 }
43
44 function compareImages(refData, tstData, width, height, diff) {
45 function isPixelSame(offset) {
46 // First do simple check
47 if (Math.abs(refData[offset + 0] - tstData[offset + 0]) <= GTFPIXELTOLERANCE &&
48 Math.abs(refData[offset + 1] - tstData[offset + 1]) <= GTFPIXELTOLERANCE &&
49 Math.abs(refData[offset + 2] - tstData[offset + 2]) <= GTFPIXELTOLERANCE ) {
50 return true;
51 }
52
53 // TODO: Implement crazy check that's used in OpenGL ES 2.0 conformance test s.
54 // NOTE: on Desktop things seem to be working. Maybe the more complex check
55 // is needed for embedded systems?
56 return false;
57 }
58
59 var same = true;
60 for (var yy = 0; yy < height; ++yy) {
61 for (var xx = 0; xx < width; ++xx) {
62 var offset = (yy * width + xx) * 4;
63 var diffOffset = ((height - yy - 1) * width + xx) * 4;
64 diff[diffOffset + 0] = 0;
65 diff[diffOffset + 1] = 0;
66 diff[diffOffset + 2] = 0;
67 diff[diffOffset + 3] = 255;
68 if (!isPixelSame(offset)) {
69 diff[diffOffset] = 255;
70 if (same) {
71 same = false;
72 testFailed("pixel @ (" + xx + ", " + yy + " was [" +
73 tstData[offset + 0] + "," +
74 tstData[offset + 1] + "," +
75 tstData[offset + 2] + "," +
76 tstData[offset + 3] + "] expected [" +
77 refData[offset + 0] + "," +
78 refData[offset + 1] + "," +
79 refData[offset + 2] + "," +
80 refData[offset + 3] + "]")
81 }
82 }
83 }
84 }
85 return same;
86 }
87
88 function persp(fovy, aspect, n, f) {
89 var dz = f - n;
90 var rad = fovy / 2.0 * 3.14159265 / 180;
91
92 var s = Math.sin(rad);
93 if (dz == 0 || s == 0 || aspect == 0)
94 return;
95
96 var cot = Math.cos(rad) / s;
97
98 return [
99 cot / aspect,
100 0.0,
101 0.0,
102 0.0,
103
104 0.0,
105 cot,
106 0.0,
107 0.0,
108
109 0.0,
110 0.0,
111 -(f + n) / dz,
112 -1.0,
113
114 0.0,
115 0.0,
116 -2.0 * f * n / dz,
117 0.0
118 ];
119 }
120
121 function setAttribs(attribs, buffers) {
122 for (var name in attribs) {
123 var buffer = buffers[name];
124 if (!buffer) {
125 testFailed("no buffer for attrib:" + name);
126 continue;
127 }
128 var loc = attribs[name];
129 log("setup attrib: " + loc + " as " + name);
130 var buf = gl.createBuffer();
131 gl.bindBuffer(gl.ARRAY_BUFFER, buf);
132 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(buffer.data), gl.STATIC_DRAW );
133 gl.enableVertexAttribArray(loc);
134 gl.vertexAttribPointer(loc, buffer.numComponents, gl.FLOAT, false, 0, 0);
135 }
136 }
137
138 function drawSquare(attribs) {
139 var buffers = {
140 "gtf_Vertex": {
141 data: [
142 1.0, -1.0, -2.0,
143 1.0, 1.0, -2.0,
144 -1.0, -1.0, -2.0,
145 -1.0, 1.0, -2.0
146 ],
147 numComponents: 3
148 },
149 "gtf_Color": {
150 data: [
151 0.5, 1.0, 0.0,
152 0.0, 1.0, 1.0,
153 1.0, 0.0, 0.0,
154 0.5, 0.0, 1.0
155 ],
156 numComponents: 3,
157 },
158 "gtf_SecondaryColor": {
159 data: [
160 0.5, 0.0, 1.0,
161 1.0, 0.0, 0.0,
162 0.0, 1.0, 1.0,
163 0.5, 1.0, 0.0
164 ],
165 numComponents: 3,
166 },
167 "gtf_Normal": {
168 data: [
169 0.5, 0.0, 1.0,
170 1.0, 0.0, 0.0,
171 0.0, 1.0, 1.0,
172 0.5, 1.0, 0.0
173 ],
174 numComponents: 3,
175 },
176 "gtf_MultiTexCoord0": {
177 data: [
178 1.0, 0.0,
179 1.0, 1.0,
180 0.0, 0.0,
181 0.0, 1.0
182 ],
183 numComponents: 2,
184 },
185 "gtf_FogCoord": {
186 data: [
187 0.0,
188 1.0,
189 0.0,
190 1.0
191 ],
192 numComponents: 1,
193 }
194 };
195 setAttribs(attribs, buffers);
196 gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
197 }
198
199 function drawFrontBackSquare(attribs) {
200 var front = {
201 "gtf_Vertex": {
202 data: [
203 1.0, -1.0, -2.0,
204 1.0, 0.0, -2.0,
205 -1.0, -1.0, -2.0,
206 -1.0, 0.0, -2.0
207 ],
208 numComponents: 3
209 },
210 "gtf_Color": {
211 data: [
212 0.0, 1.0, 0.0,
213 0.0, 1.0, 0.0,
214 0.0, 1.0, 0.0,
215 0.0, 1.0, 0.0
216 ],
217 numComponents: 3,
218 },
219 "gtf_MultiTexCoord0": {
220 data: [
221 1.0, 0.0,
222 1.0, 0.5,
223 0.0, 0.0,
224 0.0, 0.5
225 ],
226 numComponents: 2,
227 }
228 };
229 setAttribs(attribs, front);
230 gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
231
232 var back = {
233 "gtf_Vertex": {
234 data: [
235 1.0, 1.0, -2.0,
236 1.0, 0.0, -2.0,
237 -1.0, 1.0, -2.0,
238 -1.0, 0.0, -2.0
239 ],
240 numComponents: 3
241 },
242 "gtf_Color": {
243 data: [
244 1.0, 0.0, 0.0,
245 1.0, 0.0, 0.0,
246 1.0, 0.0, 0.0,
247 1.0, 0.0, 0.0
248 ],
249 numComponents: 3,
250 },
251 "gtf_MultiTexCoord0": {
252 data: [
253 1.0, 0.1,
254 1.0, 0.5,
255 0.0, 0.1,
256 0.0, 0.5
257 ],
258 numComponents: 2,
259 }
260 };
261 setAttribs(attribs, back);
262 gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
263 }
264
265 function drawGrid(attribs, width, height) {
266 var n = Math.min(Math.floor(Math.max(width, height) / 4), HALF_GRID_MAX_SIZE);
267
268 var numVertices = (n + n) * (n + n) * 6;
269
270 var gridVertices = [];
271 var gridColors = [];
272 var gridSecColors = [];
273 var gridNormals = [];
274 var gridFogCoords = [];
275 var gridTexCoords0 = [];
276
277 var currentVertex = 0;
278 var currentColor = 0;
279 var currentSecColor = 0;
280 var currentTexCoord0 = 0;
281 var currentNormal = 0;
282 var currentFogCoord = 0;
283
284 var z = -2.0;
285 for(var i = -n; i < n; ++i)
286 {
287 var x1 = i / n;
288 var x2 = (i + 1) / n;
289 for(var j = -n; j < n; ++j)
290 {
291 var y1 = j / n;
292 var y2 = (j + 1) / n;
293
294 // VERTEX 0
295 gridVertices[currentVertex++] = x1;
296 gridVertices[currentVertex++] = y1;
297 gridVertices[currentVertex++] = z;
298 gridColors[currentColor++] = 1.0 - (x1 + y1 + 2.0) / 4.0;
299 gridColors[currentColor++] = (x1 + 1.0) / 2.0;
300 gridColors[currentColor++] = (y1 + 1.0) / 2.0;
301 gridSecColors[currentSecColor++] = 1.0 - (x2 + y2 + 2.0) / 4.0;
302 gridSecColors[currentSecColor++] = (x2 + 1.0) / 2.0;
303 gridSecColors[currentSecColor++] = (y2 + 1.0) / 2.0;
304 gridTexCoords0[currentTexCoord0++] = (x1 + 1.0) / 2.0;
305 gridTexCoords0[currentTexCoord0++] = (y1 + 1.0) / 2.0;
306 gridNormals[currentNormal++] = 1.0 - (x2 + y2 + 2.0) / 4.0;
307 gridNormals[currentNormal++] = (x2 + 1.0) / 2.0;
308 gridNormals[currentNormal++] = (y2 + 1.0) / 2.0;
309 gridFogCoords[currentFogCoord++] = (y1 + 1.0) / 2.0;
310
311 // VERTEX 1
312 gridVertices[currentVertex++] = x2;
313 gridVertices[currentVertex++] = y1;
314 gridVertices[currentVertex++] = z;
315 gridColors[currentColor++] = 1.0 - (x2 + y1 + 2.0) / 4.0;
316 gridColors[currentColor++] = (x2 + 1.0) / 2.0;
317 gridColors[currentColor++] = (y1 + 1.0) / 2.0;
318 gridSecColors[currentSecColor++] = 1.0 - (x1 + y2 + 2.0) / 4.0;
319 gridSecColors[currentSecColor++] = (x1 + 1.0) / 2.0;
320 gridSecColors[currentSecColor++] = (y2 + 1.0) / 2.0;
321 gridTexCoords0[currentTexCoord0++] = (x2 + 1.0) / 2.0;
322 gridTexCoords0[currentTexCoord0++] = (y1 + 1.0) / 2.0;
323 gridNormals[currentNormal++] = 1.0 - (x1 + y2 + 2.0) / 4.0;
324 gridNormals[currentNormal++] = (x1 + 1.0) / 2.0;
325 gridNormals[currentNormal++] = (y2 + 1.0) / 2.0;
326 gridFogCoords[currentFogCoord++] = (y1 + 1.0) / 2.0;
327
328 // VERTEX 2
329 gridVertices[currentVertex++] = x2;
330 gridVertices[currentVertex++] = y2;
331 gridVertices[currentVertex++] = z;
332 gridColors[currentColor++] = 1.0 - (x2 + y2 + 2.0) / 4.0;
333 gridColors[currentColor++] = (x2 + 1.0) / 2.0;
334 gridColors[currentColor++] = (y2 + 1.0) / 2.0;
335 gridSecColors[currentSecColor++] = 1.0 - (x1 + y1 + 2.0) / 4.0;
336 gridSecColors[currentSecColor++] = (x1 + 1.0) / 2.0;
337 gridSecColors[currentSecColor++] = (y1 + 1.0) / 2.0;
338 gridTexCoords0[currentTexCoord0++] = (x2 + 1.0) / 2.0;
339 gridTexCoords0[currentTexCoord0++] = (y2 + 1.0) / 2.0;
340 gridNormals[currentNormal++] = 1.0 - (x1 + y1 + 2.0) / 4.0;
341 gridNormals[currentNormal++] = (x1 + 1.0) / 2.0;
342 gridNormals[currentNormal++] = (y1 + 1.0) / 2.0;
343 gridFogCoords[currentFogCoord++] = (y2 + 1.0) / 2.0;
344
345 // VERTEX 2
346 gridVertices[currentVertex++] = x2;
347 gridVertices[currentVertex++] = y2;
348 gridVertices[currentVertex++] = z;
349 gridColors[currentColor++] = 1.0 - (x2 + y2 + 2.0) / 4.0;
350 gridColors[currentColor++] = (x2 + 1.0) / 2.0;
351 gridColors[currentColor++] = (y2 + 1.0) / 2.0;
352 gridSecColors[currentSecColor++] = 1.0 - (x1 + y1 + 2.0) / 4.0;
353 gridSecColors[currentSecColor++] = (x1 + 1.0) / 2.0;
354 gridSecColors[currentSecColor++] = (y1 + 1.0) / 2.0;
355 gridTexCoords0[currentTexCoord0++] = (x2 + 1.0) / 2.0;
356 gridTexCoords0[currentTexCoord0++] = (y2 + 1.0) / 2.0;
357 gridNormals[currentNormal++] = 1.0 - (x1 + y1 + 2.0) / 4.0;
358 gridNormals[currentNormal++] = (x1 + 1.0) / 2.0;
359 gridNormals[currentNormal++] = (y1 + 1.0) / 2.0;
360 gridFogCoords[currentFogCoord++] = (y2 + 1.0) / 2.0;
361
362 // VERTEX 3
363 gridVertices[currentVertex++] = x1;
364 gridVertices[currentVertex++] = y2;
365 gridVertices[currentVertex++] = z;
366 gridColors[currentColor++] = 1.0 - (x1 + y2 + 2.0) / 4.0;
367 gridColors[currentColor++] = (x1 + 1.0) / 2.0;
368 gridColors[currentColor++] = (y2 + 1.0) / 2.0;
369 gridSecColors[currentSecColor++] = 1.0 - (x2 + y1 + 2.0) / 4.0;
370 gridSecColors[currentSecColor++] = (x2 + 1.0) / 2.0;
371 gridSecColors[currentSecColor++] = (y1 + 1.0) / 2.0;
372 gridTexCoords0[currentTexCoord0++] = (x1 + 1.0) / 2.0;
373 gridTexCoords0[currentTexCoord0++] = (y2 + 1.0) / 2.0;
374 gridNormals[currentNormal++] = 1.0 - (x2 + y1 + 2.0) / 4.0;
375 gridNormals[currentNormal++] = (x2 + 1.0) / 2.0;
376 gridNormals[currentNormal++] = (y1 + 1.0) / 2.0;
377 gridFogCoords[currentFogCoord++] = (y2 + 1.0) / 2.0;
378
379 // VERTEX 0
380 gridVertices[currentVertex++] = x1;
381 gridVertices[currentVertex++] = y1;
382 gridVertices[currentVertex++] = z;
383 gridColors[currentColor++] = 1.0 - (x1 + y1 + 2.0) / 4.0;
384 gridColors[currentColor++] = (x1 + 1.0) / 2.0;
385 gridColors[currentColor++] = (y1 + 1.0) / 2.0;
386 gridSecColors[currentSecColor++] = 1.0 - (x2 + y2 + 2.0) / 4.0;
387 gridSecColors[currentSecColor++] = (x2 + 1.0) / 2.0;
388 gridSecColors[currentSecColor++] = (y2 + 1.0) / 2.0;
389 gridTexCoords0[currentTexCoord0++] = (x1 + 1.0) / 2.0;
390 gridTexCoords0[currentTexCoord0++] = (y1 + 1.0) / 2.0;
391 gridNormals[currentNormal++] = 1.0 - (x2 + y2 + 2.0) / 4.0;
392 gridNormals[currentNormal++] = (x2 + 1.0) / 2.0;
393 gridNormals[currentNormal++] = (y2 + 1.0) / 2.0;
394 gridFogCoords[currentFogCoord++] = (y1 + 1.0) / 2.0;
395 }
396 }
397
398 var buffers = {
399 "gtf_Vertex": { data: gridVertices, numComponents: 3 },
400 "gtf_Color": { data: gridColors, numComponents: 3 },
401 "gtf_SecondaryColor": { data: gridSecColors, numComponents: 3 },
402 "gtf_Normal": { data: gridNormals, numComponents: 3 },
403 "gtf_FogCoord": { data: gridFogCoords, numComponents: 1 },
404 "gtf_MultiTexCoord0": { data: gridTexCoords0, numComponents: 2 }
405 };
406 setAttribs(attribs, buffers);
407 gl.drawArrays(gl.TRIANGLES, 0, numVertices);
408 }
409
410 var MODEL_FUNCS = {
411 square: drawSquare,
412 frontbacksquare: drawFrontBackSquare,
413 grid: drawGrid
414 };
415
416 function drawWithProgram(program, programInfo, test) {
417 gl.useProgram(program);
418 var attribs = { };
419
420 var numAttribs = gl.getProgramParameter(program, gl.ACTIVE_ATTRIBUTES);
421 for (var ii = 0; ii < numAttribs; ++ii) {
422 var info = gl.getActiveAttrib(program, ii);
423 var name = info.name;
424 var location = gl.getAttribLocation(program, name);
425 attribs[name] = location;
426
427 if (KNOWN_ATTRIBS.indexOf(name) < 0) {
428 testFailed("unknown attrib:" + name)
429 }
430 }
431
432 var uniforms = { };
433 var numUniforms = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);
434 for (var ii = 0; ii < numUniforms; ++ii) {
435 var info = gl.getActiveUniform(program, ii);
436 var name = info.name;
437 if (name.match(/\[0\]$/)) {
438 name = name.substr(0, name.length - 3);
439 }
440 var location = gl.getUniformLocation(program, name);
441 uniforms[name] = {location: location};
442 }
443
444 var getUniformLocation = function(name) {
445 var uniform = uniforms[name];
446 if (uniform) {
447 uniform.used = true;
448 return uniform.location;
449 }
450 return null;
451 }
452
453 // Set known uniforms
454 var loc = getUniformLocation("gtf_ModelViewProjectionMatrix");
455 if (loc) {
456 gl.uniformMatrix4fv(
457 loc,
458 false,
459 persp(60, 1, 1, 30));
460 }
461 var loc = getUniformLocation("viewportwidth");
462 if (loc) {
463 gl.uniform1f(loc, gl.canvas.width);
464 }
465 var loc = getUniformLocation("viewportheight");
466 if (loc) {
467 gl.uniform1f(loc, gl.canvas.height);
468 }
469
470 // Set test specific uniforms
471 for (var name in programInfo.uniforms) {
472 var location = getUniformLocation(name);
473 if (!location) {
474 testFailed("uniform not found: " + name);
475 continue;
476 }
477 var uniform = programInfo.uniforms[name];
478 var type = uniform.type;
479 var value = uniform.value;
480 var transpose = uniform.transpose;
481 if (transpose !== undefined) {
482 log("gl." + type + '("' + name + '", ' + transpose + ", " + value + ")");
483 gl[type](location, transpose, value);
484 } else if (!type.match("v$")) {
485 var args = [location];
486 for (var ii = 0; ii < value.length; ++ii) {
487 args.push(value[ii]);
488 }
489 gl[type].apply(gl, args);
490 log("gl." + type + '("' + name + '", ' + args.slice(1) + ")");
491 } else {
492 log("gl." + type + '("' + name + '", ' + value + ")");
493 gl[type](location, value);
494 }
495 var err = gl.getError();
496 if (err != gl.NO_ERROR) {
497 testFailed(wtu.glEnumToString(gl, err) + " generated setting uniform: " + name);
498 }
499 }
500
501 // Check for unset uniforms
502 for (var name in uniforms) {
503 var uniform = uniforms[name];
504 if (!uniform.used) {
505 testFailed("uniform " + name + " never set");
506 }
507 }
508
509
510 for (var state in test.state) {
511 var fields = test.state[state];
512 switch (state) {
513 case 'depthrange':
514 gl.depthRange(fields.near, fields.far);
515 break;
516 default:
517 testFailed("unknown state: " + state)
518 }
519 }
520
521 gl.clearColor(0, 0, 0, 0);
522 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
523
524 var model = test.model || "square";
525 var fn = MODEL_FUNCS[model];
526 if (!fn) {
527 testFailed("unknown model type: " + model)
528 } else {
529 log("draw as: " + model)
530 fn(attribs, gl.canvas.width, gl.canvas.height);
531 }
532
533 var pixels = new Uint8Array(gl.canvas.width * gl.canvas.height * 4);
534 gl.readPixels(0, 0, gl.canvas.width, gl.canvas.height, gl.RGBA, gl.UNSIGNED_BY TE, pixels);
535 return {
536 width: gl.canvas.width,
537 height: gl.canvas.height,
538 pixels: pixels,
539 img: wtu.makeImage(gl.canvas)
540 };
541 }
542
543 function runProgram(programInfo, test, label, callback) {
544 var shaders = [];
545 var source = [];
546 var count = 0;
547
548 function loadShader(path, type, index) {
549 wtu.loadTextFileAsync(path, function(success, text) {
550 addShader(success, text, type, path, index);
551 });
552 }
553
554 function addShader(success, text, type, path, index) {
555 ++count;
556 if (!success) {
557 testFailed("could not load: " + path);
558 } else {
559 var shader = wtu.loadShader(gl, text, type);
560 shaders.push(shader);
561 source[index] = text;
562 }
563 if (count == 2) {
564 var result;
565 if (shaders.length == 2) {
566 debug("");
567 var console = document.getElementById("console");
568 wtu.addShaderSource(
569 console, label + " vertex shader", source[0], programInfo.vertexShad er);
570 wtu.addShaderSource(
571 console, label + " fragment shader", source[1], programInfo.fragment Shader);
572 var program = wtu.createProgram(gl, shaders[0], shaders[1]);
573 result = drawWithProgram(program, programInfo, test);
574 }
575 callback(result);
576 }
577 }
578
579 loadShader(programInfo.vertexShader, gl.VERTEX_SHADER, 0);
580 loadShader(programInfo.fragmentShader, gl.FRAGMENT_SHADER, 1);
581 }
582
583 function compareResults(expected, actual) {
584 var width = expected.width;
585 var height = expected.height;
586 var canvas = document.createElement("canvas");
587 canvas.width = width;
588 canvas.height = height;
589 var ctx = canvas.getContext("2d");
590 var imgData = ctx.getImageData(0, 0, width, height);
591 var tolerance = 0;
592
593 var expData = expected.pixels;
594 var actData = actual.pixels;
595
596 var same = compareImages(expData, actData, width, height, imgData.data);
597
598 var console = document.getElementById("console");
599 var diffImg = null;
600 if (!same) {
601 ctx.putImageData(imgData, 0, 0);
602 diffImg = wtu.makeImage(canvas);
603 }
604
605 var div = document.createElement("div");
606 div.className = "testimages";
607 wtu.insertImage(div, "reference", expected.img);
608 wtu.insertImage(div, "test", actual.img);
609 if (diffImg) {
610 wtu.insertImage(div, "diff", diffImg);
611 }
612 div.appendChild(document.createElement('br'));
613
614 console.appendChild(div);
615
616 if (!same) {
617 testFailed("images are different");
618 } else {
619 testPassed("images are the same");
620 }
621
622 console.appendChild(document.createElement('hr'));
623 }
624
625 function runCompareTest(test, callback) {
626 debug("");
627 debug("test: " + test.name);
628 var results = [];
629 var count = 0;
630
631 function storeResults(index) {
632 return function(result) {
633 results[index] = result;
634 ++count;
635 if (count == 2) {
636 compareResults(results[0], results[1]);
637 glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");
638 callback();
639 }
640 }
641 }
642
643 runProgram(test.referenceProgram, test, "reference", storeResults(0));
644 runProgram(test.testProgram, test, "test", storeResults(1));
645 }
646
647 function runBuildTest(test, callback) {
648 debug("");
649 debug("test: " + test.name);
650
651 var shaders = [null, null];
652 var source = ["",""];
653 var success = [undefined, undefined];
654 var count = 0;
655
656 function loadShader(path, type, index) {
657 if (path == "empty") {
658 shaders[index] = gl.createShader();
659 success[index] = true;
660 source[index] = "/* empty */";
661 attachAndLink();
662 } else {
663 wtu.loadTextFileAsync(path, function(loadSuccess, text) {
664 if (!loadSuccess) {
665 success[index] = false;
666 source[index] = "/* could not load */";
667 testFailed("could not load:" + path);
668 } else {
669 source[index] = text;
670 shaders[index] = wtu.loadShader(gl, text, type, function(index) {
671 return function(msg) {
672 success[index] = false
673 }
674 }(index));
675 if (success[index] === undefined) {
676 success[index] = true;
677 }
678 }
679 attachAndLink();
680 });
681 }
682 }
683
684 function attachAndLink() {
685 ++count;
686 if (count == 2) {
687 debug("");
688 var c = document.getElementById("console");
689 wtu.addShaderSource(
690 c, "vertex shader", source[0], test.testProgram.vertexShader);
691 debug("compile: " + (success[0] ? "success" : "fail"));
692 wtu.addShaderSource(
693 c, "fragment shader", source[1], test.testProgram.fragmentShader);
694 debug("compile: " + (success[1] ? "success" : "fail"));
695 compileSuccess = (success[0] && success[1]);
696 if (!test.compstat) {
697 if (compileSuccess) {
698 testFailed("expected compile failure but was successful");
699 } else {
700 testPassed("expected compile failure and it failed");
701 }
702 } else {
703 if (compileSuccess) {
704 testPassed("expected compile success and it was successful");
705 } else {
706 testFailed("expected compile success but it failed");
707 }
708 var linkSuccess = true;
709 var program = wtu.createProgram(gl, shaders[0], shaders[1], function() {
710 linkSuccess = false;
711 });
712 if (linkSuccess !== test.linkstat) {
713 testFailed("expected link to " + (test.linkstat ? "succeed" : "fail")) ;
714 } else {
715 testPassed("shaders compiled and linked as expected.");
716 }
717 }
718 callback();
719 }
720 }
721
722 loadShader(test.testProgram.vertexShader, gl.VERTEX_SHADER, 0);
723 loadShader(test.testProgram.fragmentShader, gl.FRAGMENT_SHADER, 1);
724 }
725
726 var testPatterns = {
727 compare: runCompareTest,
728 build: runBuildTest,
729
730 dummy: null // just here to mark the end
731 };
732
733 function LogGLCall(functionName, args) {
734 console.log("gl." + functionName + "(" +
735 WebGLDebugUtils.glFunctionArgsToString(functionName, args) + ")");
736 }
737
738 // Runs the tests async since they will load shaders.
739 function run(obj) {
740 description();
741
742 var canvas = document.getElementById("example");
743 gl = wtu.create3DContext(canvas);
744 if (window.WebGLDebugUtils) {
745 gl = WebGLDebugUtils.makeDebugContext(gl, undefined, LogGLCall);
746 }
747 if (!gl) {
748 testFailed("context does not exist");
749 finishTest();
750 return;
751 }
752
753 if (gl.canvas.width != 500 || gl.canvas.height != 500) {
754 testFailed("canvas must be 500x500 pixels: Several shaders are hard coded to this size.");
755 }
756
757 var tests = obj.tests;
758 var ndx = 0;
759
760 function runNextTest() {
761 if (ndx < tests.length) {
762 var test = tests[ndx++];
763 var fn = testPatterns[test.pattern];
764 if (!fn) {
765 testFailed("test pattern: " + test.pattern + " not supoprted")
766 runNextTest();
767 } else {
768 fn(test, runNextTest);
769 }
770 } else {
771 finishTest();
772 }
773 }
774 runNextTest();
775 }
776
777 return {
778 run: run,
779 };
780 }());
781
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698