Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / webgl / src / sdk / tests / conformance / extensions / webgl-compressed-texture-s3tc.html
1 <!--
2
3 /*
4 ** Copyright (c) 2012 The Khronos Group Inc.
5 **
6 ** Permission is hereby granted, free of charge, to any person obtaining a
7 ** copy of this software and/or associated documentation files (the
8 ** "Materials"), to deal in the Materials without restriction, including
9 ** without limitation the rights to use, copy, modify, merge, publish,
10 ** distribute, sublicense, and/or sell copies of the Materials, and to
11 ** permit persons to whom the Materials are furnished to do so, subject to
12 ** the following conditions:
13 **
14 ** The above copyright notice and this permission notice shall be included
15 ** in all copies or substantial portions of the Materials.
16 **
17 ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
24 */
25
26 -->
27
28 <!DOCTYPE html>
29 <html>
30 <head>
31 <meta charset="utf-8">
32 <link rel="stylesheet" href="../../resources/js-test-style.css"/>
33 <script src="../../resources/js-test-pre.js"></script>
34 <script src="../resources/webgl-test-utils.js"></script>
35 <title>WebGL WEBGL_compressed_texture_s3tc Conformance Tests</title>
36 <style>
37 img {
38  border: 1px solid black;
39  margin-right: 1em;
40 }
41 .testimages {
42 }
43
44 .testimages br {
45   clear: both;
46 }
47
48 .testimages > div {
49   float: left;
50   margin: 1em;
51 }
52 </style>
53 </head>
54 <body>
55 <div id="description"></div>
56 <canvas id="canvas" width="8" height="8" style="width: 8px; height: 8px;"></canvas>
57 <div id="console"></div>
58 <script>
59 "use strict";
60 description("This test verifies the functionality of the WEBGL_compressed_texture_s3tc extension, if it is available.");
61
62 debug("");
63
64 var img_4x4_rgba_raw = new Uint8Array([
65     0xff,0x00,0x00,0x69,0x00,0xff,0x00,0xff,0xff,0x00,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff,0xff,0x00,0x00,0xff,0x00,0xff,0x00,0xff,0xff,0x00,0x00,0xff,0xff,0x00,0x00,0xff,0xff,0x00,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff,
66 ]);
67 var img_4x4_rgb_dxt1 = new Uint8Array([
68     0xe0,0x07,0x00,0xf8,0x11,0x10,0x15,0x00,
69 ]);
70 var img_4x4_rgba_dxt1 = new Uint8Array([
71     0xe0,0x07,0x00,0xf8,0x13,0x10,0x15,0x00,
72 ]);
73 var img_4x4_rgba_dxt3 = new Uint8Array([
74     0xf6,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0xf8,0xe0,0x07,0x44,0x45,0x40,0x55,
75 ]);
76 var img_4x4_rgba_dxt5 = new Uint8Array([
77     0xf6,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0xf8,0xe0,0x07,0x44,0x45,0x40,0x55,
78 ]);
79 var img_8x8_rgba_raw = new Uint8Array([
80     0xff,0x00,0x00,0x69,0x00,0xff,0x00,0xff,0xff,0x00,0x00,0xff,0x00,0xff,0x00,0xff,0xff,0xff,0x00,0xff,0x00,0x00,0xff,0xff,0xff,0xff,0x00,0xff,0x00,0x00,0xff,0xff,0x00,0xff,0x00,0x69,0x00,0xff,0x00,0xff,0xff,0x00,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0x00,0xff,0xff,0x00,0x00,0xff,0xff,0xff,0xff,0x00,0xff,0x00,0x00,0xff,0xff,0xff,0x00,0x00,0xff,0xff,0x00,0x00,0xff,0xff,0x00,0x00,0xff,0x00,0xff,0x00,0xff,0xff,0xff,0x00,0xff,0xff,0xff,0x00,0xff,0xff,0xff,0x00,0xff,0x00,0x00,0xff,0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0x00,0xff,0xff,0x00,0x00,0xff,0xff,0x00,0x00,0xff,0xff,0x00,0x00,0xff,0xff,0x00,0xff,0x00,0xff,0xff,0x00,0xff,0xff,0x00,0xff,0x00,0xff,0xff,0x00,0xff,0xff,0x00,0x00,0xff,0xff,0x00,0xff,0xff,0xff,0x00,0x00,0xff,0xff,0x00,0xff,0xff,0xff,0xff,0x00,0xff,0xff,0xff,0x00,0xff,0xff,0x00,0xff,0x00,0xff,0xff,0x00,0xff,0xff,0x00,0xff,0xff,0xff,0x00,0xff,0xff,0xff,0x00,0x00,0xff,0xff,0x00,0xff,0xff,0xff,0x00,0xff,0x00,0x69,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff,0xff,0x00,0xff,0xff,0x00,0x00,0xff,0xff,0x00,0x00,0xff,0xff,0x00,0x00,0xff,0xff,0x00,0xff,0xff,0xff,0xff,0x00,0xff,0x69,0xff,0x00,0xff,0xff,0xff,0x00,0xff,0xff,0xff,0x00,0xff,0xff,0x00,0xff,0xff,0xff,0x00,0xff,0xff,0xff,0x00,0xff,0xff,0xff,0x00,0xff,0xff,0xff,
81 ]);
82 var img_8x8_rgb_dxt1 = new Uint8Array([
83     0xe0,0x07,0x00,0xf8,0x11,0x10,0x15,0x00,0x1f,0x00,0xe0,0xff,0x11,0x10,0x15,0x00,0xe0,0x07,0x1f,0xf8,0x44,0x45,0x40,0x55,0x1f,0x00,0xff,0x07,0x44,0x45,0x40,0x55,
84 ]);
85 var img_8x8_rgba_dxt1 = new Uint8Array([
86     0xe0,0x07,0x00,0xf8,0x13,0x13,0x15,0x00,0x1f,0x00,0xe0,0xff,0x11,0x10,0x15,0x00,0xe0,0x07,0x1f,0xf8,0x44,0x45,0x43,0x57,0x1f,0x00,0xff,0x07,0x44,0x45,0x40,0x55,
87 ]);
88 var img_8x8_rgba_dxt3 = new Uint8Array([
89     0xf6,0xff,0xf6,0xff,0xff,0xff,0xff,0xff,0x00,0xf8,0xe0,0x07,0x44,0x45,0x40,0x55,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xe0,0xff,0x1f,0x00,0x44,0x45,0x40,0x55,0xff,0xff,0xff,0xff,0xf6,0xff,0xf6,0xff,0x1f,0xf8,0xe0,0x07,0x11,0x10,0x15,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x07,0x1f,0x00,0x11,0x10,0x15,0x00,
90 ]);
91 var img_8x8_rgba_dxt5 = new Uint8Array([
92     0xff,0x69,0x01,0x10,0x00,0x00,0x00,0x00,0x00,0xf8,0xe0,0x07,0x44,0x45,0x40,0x55,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0xe0,0xff,0x1f,0x00,0x44,0x45,0x40,0x55,0xff,0x69,0x00,0x00,0x00,0x01,0x10,0x00,0x1f,0xf8,0xe0,0x07,0x11,0x10,0x15,0x00,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x07,0x1f,0x00,0x11,0x10,0x15,0x00,
93 ]);
94
95 var wtu = WebGLTestUtils;
96 var canvas = document.getElementById("canvas");
97 var gl = wtu.create3DContext(canvas, {antialias: false});
98 var program = wtu.setupTexturedQuad(gl);
99 var ext = null;
100 var vao = null;
101 var validFormats = {
102     COMPRESSED_RGB_S3TC_DXT1_EXT        : 0x83F0,
103     COMPRESSED_RGBA_S3TC_DXT1_EXT       : 0x83F1,
104     COMPRESSED_RGBA_S3TC_DXT3_EXT       : 0x83F2,
105     COMPRESSED_RGBA_S3TC_DXT5_EXT       : 0x83F3,
106 };
107 var name;
108 var supportedFormats;
109
110 if (!gl) {
111     testFailed("WebGL context does not exist");
112 } else {
113     testPassed("WebGL context exists");
114
115     // Run tests with extension disabled
116     runTestDisabled();
117
118     // Query the extension and store globally so shouldBe can access it
119     ext = wtu.getExtensionWithKnownPrefixes(gl, "WEBGL_compressed_texture_s3tc");
120     if (!ext) {
121         testPassed("No WEBGL_compressed_texture_s3tc support -- this is legal");
122         runSupportedTest(false);
123     } else {
124         testPassed("Successfully enabled WEBGL_compressed_texture_s3tc extension");
125
126         runSupportedTest(true);
127         runTestExtension();
128     }
129 }
130
131 function runSupportedTest(extensionEnabled) {
132     var name = wtu.getSupportedExtensionWithKnownPrefixes(gl, "WEBGL_compressed_texture_s3tc");
133     if (name !== undefined) {
134         if (extensionEnabled) {
135             testPassed("WEBGL_compressed_texture_s3tc listed as supported and getExtension succeeded");
136         } else {
137             testFailed("WEBGL_compressed_texture_s3tc listed as supported but getExtension failed");
138         }
139     } else {
140         if (extensionEnabled) {
141             testFailed("WEBGL_compressed_texture_s3tc not listed as supported but getExtension succeeded");
142         } else {
143             testPassed("WEBGL_compressed_texture_s3tc not listed as supported and getExtension failed -- this is legal");
144         }
145     }
146 }
147
148
149 function runTestDisabled() {
150     debug("Testing binding enum with extension disabled");
151
152     shouldBe('gl.getParameter(gl.COMPRESSED_TEXTURE_FORMATS)', '[]');
153 }
154
155 function formatExists(format, supportedFormats) {
156     for (var ii = 0; ii < supportedFormats.length; ++ii) {
157         if (format == supportedFormats[ii]) {
158             testPassed("supported format " + formatToString(format) + " is exists");
159             return;
160         }
161     }
162     testFailed("supported format " + formatToString(format) + " does not exist");
163 }
164
165 function formatToString(format) {
166     for (var p in ext) {
167         if (ext[p] == format) {
168             return p;
169         }
170     }
171     return "0x" + format.toString(16);
172 }
173
174 function runTestExtension() {
175     debug("Testing WEBGL_compressed_texture_s3tc");
176
177     // check that all format enums exist.
178     for (name in validFormats) {
179         var expected = "0x" + validFormats[name].toString(16);
180         var actual = "ext['" + name + "']";
181         shouldBe(actual, expected);
182     }
183
184     supportedFormats = gl.getParameter(gl.COMPRESSED_TEXTURE_FORMATS);
185     // There should be exactly 4 formats
186     shouldBe("supportedFormats.length", "4");
187
188     // check that all 4 formats exist
189     for (var name in validFormats.length) {
190         formatExists(validFormats[name], supportedFormats);
191     }
192
193     // Test each format
194     testDXT1_RGB();
195     testDXT1_RGBA();
196     testDXT3_RGBA();
197     testDXT5_RGBA();
198 }
199
200 function testDXT1_RGB() {
201     var tests = [
202         {   width: 4,
203             height: 4,
204             channels: 3,
205             data: img_4x4_rgb_dxt1,
206             format: ext.COMPRESSED_RGB_S3TC_DXT1_EXT
207         },
208         {   width: 8,
209             height: 8,
210             channels: 3,
211             data: img_8x8_rgb_dxt1,
212             format: ext.COMPRESSED_RGB_S3TC_DXT1_EXT
213         }
214     ];
215     testDXTTextures(tests);
216 }
217
218 function testDXT1_RGBA() {
219     var tests = [
220         {   width: 4,
221             height: 4,
222             channels: 4,
223             data: img_4x4_rgba_dxt1,
224             format: ext.COMPRESSED_RGBA_S3TC_DXT1_EXT
225         },
226         {   width: 8,
227             height: 8,
228             channels: 4,
229             data: img_8x8_rgba_dxt1,
230             format: ext.COMPRESSED_RGBA_S3TC_DXT1_EXT
231         }
232     ];
233     testDXTTextures(tests);
234 }
235
236 function testDXT3_RGBA() {
237     var tests = [
238         {   width: 4,
239             height: 4,
240             channels: 4,
241             data: img_4x4_rgba_dxt3,
242             format: ext.COMPRESSED_RGBA_S3TC_DXT3_EXT
243         },
244         {   width: 8,
245             height: 8,
246             channels: 4,
247             data: img_8x8_rgba_dxt3,
248             format: ext.COMPRESSED_RGBA_S3TC_DXT3_EXT
249         }
250     ];
251     testDXTTextures(tests);
252 }
253
254 function testDXT5_RGBA() {
255     var tests = [
256         {   width: 4,
257             height: 4,
258             channels: 4,
259             data: img_4x4_rgba_dxt5,
260             format: ext.COMPRESSED_RGBA_S3TC_DXT5_EXT
261         },
262         {   width: 8,
263             height: 8,
264             channels: 4,
265             data: img_8x8_rgba_dxt5,
266             format: ext.COMPRESSED_RGBA_S3TC_DXT5_EXT
267         }
268     ];
269     testDXTTextures(tests);
270 }
271
272 function testDXTTextures(tests) {
273     debug("<hr/>");
274     for (var ii = 0; ii < tests.length; ++ii) {
275         testDXTTexture(tests[ii]);
276     }
277 }
278
279 function uncompressDXTBlock(
280     destBuffer, destX, destY, destWidth, src, srcOffset, format) {
281     function make565(src, offset) {
282         return src[offset + 0] + src[offset + 1] * 256;
283     }
284     function make8888From565(c) {
285         return [
286                 Math.floor(((c >> 11) & 0x1F) * 255 / 31),
287                 Math.floor(((c >>    5) & 0x3F) * 255 / 63),
288                 Math.floor(((c >>    0) & 0x1F) * 255 / 31),
289                 255
290             ];
291     }
292     function mix(mult, c0, c1, div) {
293         var r = [];
294         for (var ii = 0; ii < c0.length; ++ii) {
295             r[ii] = Math.floor((c0[ii] * mult + c1[ii]) / div);
296         }
297         return r;
298     }
299     var isDXT1 = format == ext.COMPRESSED_RGB_S3TC_DXT1_EXT ||
300                  format == ext.COMPRESSED_RGBA_S3TC_DXT1_EXT;
301     var colorOffset = srcOffset + (isDXT1 ? 0 : 8);
302     var color0 = make565(src, colorOffset + 0);
303     var color1 = make565(src, colorOffset + 2);
304     var c0gtc1 = color0 > color1 || !isDXT1;
305     var rgba0 = make8888From565(color0);
306     var rgba1 = make8888From565(color1);
307     var colors = [
308             rgba0,
309             rgba1,
310             c0gtc1 ? mix(2, rgba0, rgba1, 3) : mix(1, rgba0, rgba1, 2),
311             c0gtc1 ? mix(2, rgba1, rgba0, 3) : [0, 0, 0, 255]
312         ];
313
314     // yea I know there is a lot of math in this inner loop.
315     // so sue me.
316     for (var yy = 0; yy < 4; ++yy) {
317         var pixels = src[colorOffset + 4 + yy];
318         for (var xx = 0; xx < 4; ++xx) {
319             var dstOff = ((destY + yy) * destWidth + destX + xx) * 4;
320             var code = (pixels >> (xx * 2)) & 0x3;
321             var srcColor = colors[code];
322             var alpha;
323             switch (format) {
324             case ext.COMPRESSED_RGB_S3TC_DXT1_EXT:
325                 alpha = 255;
326                 break;
327             case ext.COMPRESSED_RGBA_S3TC_DXT1_EXT:
328                 alpha = (code == 3 && !c0gtc1) ? 0 : 255;
329                 break;
330             case ext.COMPRESSED_RGBA_S3TC_DXT3_EXT:
331                 {
332                     var alpha0 = src[srcOffset + yy * 2 + Math.floor(xx / 2)];
333                     var alpha1 = (alpha0 >> ((xx % 2) * 4)) & 0xF;
334                     alpha = alpha1 | (alpha1 << 4);
335                 }
336                 break;
337             case ext.COMPRESSED_RGBA_S3TC_DXT5_EXT:
338                 {
339                     var alpha0 = src[srcOffset + 0];
340                     var alpha1 = src[srcOffset + 1];
341                     var alphaOff = Math.floor(yy / 2) * 3 + 2;
342                     var alphaBits =
343                          src[srcOffset + alphaOff + 0] +
344                          src[srcOffset + alphaOff + 1] * 256 +
345                          src[srcOffset + alphaOff + 2] * 65536;
346                     var alphaShift = (yy % 2) * 12 + xx * 3;
347                     var alphaCode = (alphaBits >> alphaShift) & 0x7;
348                     if (alpha0 > alpha1) {
349                         switch (alphaCode) {
350                         case 0:
351                             alpha = alpha0;
352                             break;
353                         case 1:
354                             alpha = alpha1;
355                             break;
356                         default:
357                             alpha = ((8 - alphaCode) * alpha0 + (alphaCode - 1) * alpha1) / 7;
358                             break;
359                         }
360                     } else {
361                         switch (alphaCode) {
362                         case 0:
363                             alpha = alpha0;
364                             break;
365                         case 1:
366                             alpha = alpha1;
367                             break;
368                         case 6:
369                             alpha = 0;
370                             break;
371                         case 7:
372                             alpha = 255;
373                             break;
374                         default:
375                             alpha = ((6 - alphaCode) * alpha0 + (alphaCode - 1) * alpha1) / 5;
376                             break;
377                         }
378                     }
379                 }
380                 break;
381             default:
382                 throw "bad format";
383             }
384             destBuffer[dstOff + 0] = srcColor[0];
385             destBuffer[dstOff + 1] = srcColor[1];
386             destBuffer[dstOff + 2] = srcColor[2];
387             destBuffer[dstOff + 3] = alpha;
388         }
389     }
390 }
391
392 function getBlockSize(format) {
393   var isDXT1 = format == ext.COMPRESSED_RGB_S3TC_DXT1_EXT ||
394                format == ext.COMPRESSED_RGBA_S3TC_DXT1_EXT;
395   return isDXT1 ? 8 : 16;
396 }
397
398 function uncompressDXT(width, height, data, format) {
399     if (width % 4 || height % 4) throw "bad width or height";
400
401     var dest = new Uint8Array(width * height * 4);
402     var blocksAcross = width / 4;
403     var blocksDown = height / 4;
404     var blockSize = getBlockSize(format);
405     for (var yy = 0; yy < blocksDown; ++yy) {
406         for (var xx = 0; xx < blocksAcross; ++xx) {
407             uncompressDXTBlock(
408                 dest, xx * 4, yy * 4, width, data,
409                 (yy * blocksAcross + xx) * blockSize, format);
410         }
411     }
412     return dest;
413 }
414
415 function copyRect(data, srcX, srcY, dstX, dstY, width, height, stride) {
416   var bytesPerLine = width * 4;
417   var srcOffset = srcX * 4 + srcY * stride;
418   var dstOffset = dstX * 4 + dstY * stride;
419   for (; height > 0; --height) {
420     for (var ii = 0; ii < bytesPerLine; ++ii) {
421       data[dstOffset + ii] = data[srcOffset + ii];
422     }
423     srcOffset += stride;
424     dstOffset += stride;
425   }
426 }
427
428 function testDXTTexture(test) {
429     var data = new Uint8Array(test.data);
430     var width = test.width;
431     var height = test.height;
432     var format = test.format;
433
434     var uncompressedData = uncompressDXT(width, height, data, format);
435
436     canvas.width = width;
437     canvas.height = height;
438     gl.viewport(0, 0, width, height);
439     debug("testing " + formatToString(format) + " " + width + "x" + height);
440
441     var tex = gl.createTexture();
442     gl.bindTexture(gl.TEXTURE_2D, tex);
443     gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
444     gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
445     gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
446     gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
447     gl.compressedTexImage2D(gl.TEXTURE_2D, 0, format, width, height, 0, data);
448     wtu.glErrorShouldBe(gl, gl.NO_ERROR, "uploading compressed texture");
449     gl.generateMipmap(gl.TEXTURE_2D);
450     wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "trying to generate mipmaps from compressed texture");
451     wtu.clearAndDrawUnitQuad(gl);
452     compareRect(width, height, test.channels, width, height, uncompressedData, data, format);
453
454     gl.compressedTexImage2D(gl.TEXTURE_2D, 0, format, width, height, 1, data);
455     wtu.glErrorShouldBe(gl, gl.INVALID_VALUE, "non 0 border");
456
457     gl.compressedTexImage2D(gl.TEXTURE_2D, 0, format, width + 4, height, 0, data);
458     wtu.glErrorShouldBe(gl, gl.INVALID_VALUE, "data size does not match dimensions");
459     gl.compressedTexImage2D(gl.TEXTURE_2D, 0, format, width, height + 4, 0, data);
460     wtu.glErrorShouldBe(gl, gl.INVALID_VALUE, "data size does not match dimensions");
461     gl.compressedTexImage2D(gl.TEXTURE_2D, 0, format, width - 4, height, 0, data);
462     wtu.glErrorShouldBe(gl, gl.INVALID_VALUE, "data size does not match dimensions");
463     gl.compressedTexImage2D(gl.TEXTURE_2D, 0, format, width, height - 4, 0, data);
464     wtu.glErrorShouldBe(gl, gl.INVALID_VALUE, "data size does not match dimensions");
465
466     gl.compressedTexImage2D(gl.TEXTURE_2D, 0, format, width - 1, height, 0, data);
467     wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "invalid dimensions");
468     gl.compressedTexImage2D(gl.TEXTURE_2D, 0, format, width - 2, height, 0, data);
469     wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "invalid dimensions");
470     gl.compressedTexImage2D(gl.TEXTURE_2D, 0, format, width, height - 1, 0, data);
471     wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "invalid dimensions");
472     gl.compressedTexImage2D(gl.TEXTURE_2D, 0, format, width, height - 2, 0, data);
473     wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "invalid dimensions");
474
475     if (width == 4) {
476       gl.compressedTexImage2D(gl.TEXTURE_2D, 1, format, 1, height, 0, data);
477       wtu.glErrorShouldBe(gl, gl.NO_ERROR, "valid dimensions for level > 0");
478       gl.compressedTexImage2D(gl.TEXTURE_2D, 1, format, 2, height, 0, data);
479       wtu.glErrorShouldBe(gl, gl.NO_ERROR, "valid dimensions for level > 0");
480     }
481     if (height == 4) {
482       gl.compressedTexImage2D(gl.TEXTURE_2D, 1, format, width, 1, 0, data);
483       wtu.glErrorShouldBe(gl, gl.NO_ERROR, "valid dimensions for level > 0");
484       gl.compressedTexImage2D(gl.TEXTURE_2D, 1, format, width, 2, 0, data);
485       wtu.glErrorShouldBe(gl, gl.NO_ERROR, "valid dimensions for level > 0");
486     }
487
488     // pick a wrong format that uses the same amount of data.
489     var wrongFormat;
490     switch (format) {
491     case ext.COMPRESSED_RGB_S3TC_DXT1_EXT:
492       wrongFormat = ext.COMPRESSED_RGBA_S3TC_DXT1_EXT;
493       break;
494     case ext.COMPRESSED_RGBA_S3TC_DXT1_EXT:
495       wrongFormat = ext.COMPRESSED_RGB_S3TC_DXT1_EXT;
496       break;
497     case ext.COMPRESSED_RGBA_S3TC_DXT3_EXT:
498       wrongFormat = ext.COMPRESSED_RGBA_S3TC_DXT5_EXT;
499       break;
500     case ext.COMPRESSED_RGBA_S3TC_DXT5_EXT:
501       wrongFormat = ext.COMPRESSED_RGBA_S3TC_DXT3_EXT;
502       break;
503     }
504
505     gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, width, height, wrongFormat, data);
506     wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "format does not match");
507
508     gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, width + 4, height, format, data);
509     wtu.glErrorShouldBe(gl, gl.INVALID_VALUE, "data size does not match dimensions");
510     gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, width, height + 4, format, data);
511     wtu.glErrorShouldBe(gl, gl.INVALID_VALUE, "data size does not match dimensions");
512     gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, width - 4, height, format, data);
513     wtu.glErrorShouldBe(gl, gl.INVALID_VALUE, "data size does not match dimensions");
514     gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, width, height - 4, format, data);
515     wtu.glErrorShouldBe(gl, gl.INVALID_VALUE, "data size does not match dimensions");
516
517     gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, width - 1, height, format, data);
518     wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "invalid dimensions");
519     gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, width - 2, height, format, data);
520     wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "invalid dimensions");
521     gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, width, height - 1, format, data);
522     wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "invalid dimensions");
523     gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, width, height - 2, format, data);
524     wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "invalid dimensions");
525
526     var subData = new Uint8Array(data.buffer, 0, getBlockSize(format));
527
528     if (width == 8 && height == 8) {
529         gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 1, 0, 4, 4, format, subData);
530         wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "invalid offset");
531         gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0, 1, 4, 4, format, subData);
532         wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "invalid offset");
533     }
534
535     var stride = width * 4;
536     for (var yoff = 0; yoff < height; yoff += 4) {
537         for (var xoff = 0; xoff < width; xoff += 4) {
538             copyRect(uncompressedData, 0, 0, xoff, yoff, 4, 4, stride);
539             gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, xoff, yoff, 4, 4, format, subData);
540             wtu.glErrorShouldBe(gl, gl.NO_ERROR, "uploading compressed texture");
541             wtu.clearAndDrawUnitQuad(gl);
542             compareRect(width, height, test.channels, width, height, uncompressedData, data, format);
543         }
544     }
545 }
546
547 function insertImg(element, caption, img) {
548     var div = document.createElement("div");
549     div.appendChild(img);
550     var label = document.createElement("div");
551     label.appendChild(document.createTextNode(caption));
552     div.appendChild(label);
553     element.appendChild(div);
554 }
555
556 function makeImage(imageWidth, imageHeight, dataWidth, data, alpha) {
557     var scale = 8;
558     var c = document.createElement("canvas");
559     c.width = imageWidth * scale;
560     c.height = imageHeight * scale;
561     var ctx = c.getContext("2d");
562     for (var yy = 0; yy < imageHeight; ++yy) {
563         for (var xx = 0; xx < imageWidth; ++xx) {
564             var offset = (yy * dataWidth + xx) * 4;
565             ctx.fillStyle = "rgba(" +
566                     data[offset + 0] + "," +
567                     data[offset + 1] + "," +
568                     data[offset + 2] + "," +
569                     (alpha ? data[offset + 3] / 255 : 1) + ")";
570             ctx.fillRect(xx * scale, yy * scale, scale, scale);
571         }
572     }
573     return wtu.makeImageFromCanvas(c);
574 }
575 function compareRect(
576         actualWidth, actualHeight, actualChannels,
577         dataWidth, dataHeight, expectedData,
578         testData, testFormat) {
579     var actual = new Uint8Array(actualWidth * actualHeight * 4);
580     gl.readPixels(
581             0, 0, actualWidth, actualHeight, gl.RGBA, gl.UNSIGNED_BYTE, actual);
582
583     var div = document.createElement("div");
584     div.className = "testimages";
585     insertImg(div, "expected", makeImage(
586             actualWidth, actualHeight, dataWidth, expectedData,
587             actualChannels == 4));
588     insertImg(div, "actual", makeImage(
589             actualWidth, actualHeight, actualWidth, actual,
590             actualChannels == 4));
591     div.appendChild(document.createElement('br'));
592     document.getElementById("console").appendChild(div);
593
594     var failed = false;
595     for (var yy = 0; yy < actualHeight; ++yy) {
596         for (var xx = 0; xx < actualWidth; ++xx) {
597             var actualOffset = (yy * actualWidth + xx) * 4;
598             var expectedOffset = (yy * dataWidth + xx) * 4;
599             var expected = [
600                     expectedData[expectedOffset + 0],
601                     expectedData[expectedOffset + 1],
602                     expectedData[expectedOffset + 2],
603                     (actualChannels == 3 ? 255 : expectedData[expectedOffset + 3])
604             ];
605             for (var jj = 0; jj < 4; ++jj) {
606                 if (actual[actualOffset + jj] != expected[jj]) {
607                     failed = true;
608                     var was = actual[actualOffset + 0].toString();
609                     for (var j = 1; j < 4; ++j) {
610                         was += "," + actual[actualOffset + j];
611                     }
612                     testFailed('at (' + xx + ', ' + yy +
613                                          ') expected: ' + expected + ' was ' + was);
614                 }
615             }
616         }
617     }
618     if (!failed) {
619         testPassed("texture rendered correctly");
620     }
621 }
622
623 function testPVRTCTextures() {
624     testFailed("PVRTC test not yet implemented");
625 }
626
627 debug("");
628 var successfullyParsed = true;
629 </script>
630 <script src="../../resources/js-test-post.js"></script>
631
632 </body>
633 </html>