4 ** Copyright (c) 2012 The Khronos Group Inc.
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:
14 ** The above copyright notice and this permission notice shall be included
15 ** in all copies or substantial portions of the Materials.
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.
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>
38 border: 1px solid black;
55 <div id="description"></div>
56 <canvas id="canvas" width="8" height="8" style="width: 8px; height: 8px;"></canvas>
57 <div id="console"></div>
60 description("This test verifies the functionality of the WEBGL_compressed_texture_s3tc extension, if it is available.");
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,
67 var img_4x4_rgb_dxt1 = new Uint8Array([
68 0xe0,0x07,0x00,0xf8,0x11,0x10,0x15,0x00,
70 var img_4x4_rgba_dxt1 = new Uint8Array([
71 0xe0,0x07,0x00,0xf8,0x13,0x10,0x15,0x00,
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,
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,
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,
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,
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,
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,
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,
95 var wtu = WebGLTestUtils;
96 var canvas = document.getElementById("canvas");
97 var gl = wtu.create3DContext(canvas, {antialias: false});
98 var program = wtu.setupTexturedQuad(gl);
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,
108 var supportedFormats;
111 testFailed("WebGL context does not exist");
113 testPassed("WebGL context exists");
115 // Run tests with extension disabled
118 // Query the extension and store globally so shouldBe can access it
119 ext = wtu.getExtensionWithKnownPrefixes(gl, "WEBGL_compressed_texture_s3tc");
121 testPassed("No WEBGL_compressed_texture_s3tc support -- this is legal");
122 runSupportedTest(false);
124 testPassed("Successfully enabled WEBGL_compressed_texture_s3tc extension");
126 runSupportedTest(true);
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");
137 testFailed("WEBGL_compressed_texture_s3tc listed as supported but getExtension failed");
140 if (extensionEnabled) {
141 testFailed("WEBGL_compressed_texture_s3tc not listed as supported but getExtension succeeded");
143 testPassed("WEBGL_compressed_texture_s3tc not listed as supported and getExtension failed -- this is legal");
149 function runTestDisabled() {
150 debug("Testing binding enum with extension disabled");
152 shouldBe('gl.getParameter(gl.COMPRESSED_TEXTURE_FORMATS)', '[]');
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");
162 testFailed("supported format " + formatToString(format) + " does not exist");
165 function formatToString(format) {
167 if (ext[p] == format) {
171 return "0x" + format.toString(16);
174 function runTestExtension() {
175 debug("Testing WEBGL_compressed_texture_s3tc");
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);
184 supportedFormats = gl.getParameter(gl.COMPRESSED_TEXTURE_FORMATS);
185 // There should be exactly 4 formats
186 shouldBe("supportedFormats.length", "4");
188 // check that all 4 formats exist
189 for (var name in validFormats.length) {
190 formatExists(validFormats[name], supportedFormats);
200 function testDXT1_RGB() {
205 data: img_4x4_rgb_dxt1,
206 format: ext.COMPRESSED_RGB_S3TC_DXT1_EXT
211 data: img_8x8_rgb_dxt1,
212 format: ext.COMPRESSED_RGB_S3TC_DXT1_EXT
215 testDXTTextures(tests);
218 function testDXT1_RGBA() {
223 data: img_4x4_rgba_dxt1,
224 format: ext.COMPRESSED_RGBA_S3TC_DXT1_EXT
229 data: img_8x8_rgba_dxt1,
230 format: ext.COMPRESSED_RGBA_S3TC_DXT1_EXT
233 testDXTTextures(tests);
236 function testDXT3_RGBA() {
241 data: img_4x4_rgba_dxt3,
242 format: ext.COMPRESSED_RGBA_S3TC_DXT3_EXT
247 data: img_8x8_rgba_dxt3,
248 format: ext.COMPRESSED_RGBA_S3TC_DXT3_EXT
251 testDXTTextures(tests);
254 function testDXT5_RGBA() {
259 data: img_4x4_rgba_dxt5,
260 format: ext.COMPRESSED_RGBA_S3TC_DXT5_EXT
265 data: img_8x8_rgba_dxt5,
266 format: ext.COMPRESSED_RGBA_S3TC_DXT5_EXT
269 testDXTTextures(tests);
272 function testDXTTextures(tests) {
274 for (var ii = 0; ii < tests.length; ++ii) {
275 testDXTTexture(tests[ii]);
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;
284 function make8888From565(c) {
286 Math.floor(((c >> 11) & 0x1F) * 255 / 31),
287 Math.floor(((c >> 5) & 0x3F) * 255 / 63),
288 Math.floor(((c >> 0) & 0x1F) * 255 / 31),
292 function mix(mult, c0, c1, div) {
294 for (var ii = 0; ii < c0.length; ++ii) {
295 r[ii] = Math.floor((c0[ii] * mult + c1[ii]) / div);
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);
310 c0gtc1 ? mix(2, rgba0, rgba1, 3) : mix(1, rgba0, rgba1, 2),
311 c0gtc1 ? mix(2, rgba1, rgba0, 3) : [0, 0, 0, 255]
314 // yea I know there is a lot of math in this inner loop.
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];
324 case ext.COMPRESSED_RGB_S3TC_DXT1_EXT:
327 case ext.COMPRESSED_RGBA_S3TC_DXT1_EXT:
328 alpha = (code == 3 && !c0gtc1) ? 0 : 255;
330 case ext.COMPRESSED_RGBA_S3TC_DXT3_EXT:
332 var alpha0 = src[srcOffset + yy * 2 + Math.floor(xx / 2)];
333 var alpha1 = (alpha0 >> ((xx % 2) * 4)) & 0xF;
334 alpha = alpha1 | (alpha1 << 4);
337 case ext.COMPRESSED_RGBA_S3TC_DXT5_EXT:
339 var alpha0 = src[srcOffset + 0];
340 var alpha1 = src[srcOffset + 1];
341 var alphaOff = Math.floor(yy / 2) * 3 + 2;
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) {
357 alpha = ((8 - alphaCode) * alpha0 + (alphaCode - 1) * alpha1) / 7;
375 alpha = ((6 - alphaCode) * alpha0 + (alphaCode - 1) * alpha1) / 5;
384 destBuffer[dstOff + 0] = srcColor[0];
385 destBuffer[dstOff + 1] = srcColor[1];
386 destBuffer[dstOff + 2] = srcColor[2];
387 destBuffer[dstOff + 3] = alpha;
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;
398 function uncompressDXT(width, height, data, format) {
399 if (width % 4 || height % 4) throw "bad width or height";
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) {
408 dest, xx * 4, yy * 4, width, data,
409 (yy * blocksAcross + xx) * blockSize, format);
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];
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;
434 var uncompressedData = uncompressDXT(width, height, data, format);
436 canvas.width = width;
437 canvas.height = height;
438 gl.viewport(0, 0, width, height);
439 debug("testing " + formatToString(format) + " " + width + "x" + height);
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);
454 gl.compressedTexImage2D(gl.TEXTURE_2D, 0, format, width, height, 1, data);
455 wtu.glErrorShouldBe(gl, gl.INVALID_VALUE, "non 0 border");
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");
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");
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");
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");
488 // pick a wrong format that uses the same amount of data.
491 case ext.COMPRESSED_RGB_S3TC_DXT1_EXT:
492 wrongFormat = ext.COMPRESSED_RGBA_S3TC_DXT1_EXT;
494 case ext.COMPRESSED_RGBA_S3TC_DXT1_EXT:
495 wrongFormat = ext.COMPRESSED_RGB_S3TC_DXT1_EXT;
497 case ext.COMPRESSED_RGBA_S3TC_DXT3_EXT:
498 wrongFormat = ext.COMPRESSED_RGBA_S3TC_DXT5_EXT;
500 case ext.COMPRESSED_RGBA_S3TC_DXT5_EXT:
501 wrongFormat = ext.COMPRESSED_RGBA_S3TC_DXT3_EXT;
505 gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, width, height, wrongFormat, data);
506 wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "format does not match");
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");
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");
526 var subData = new Uint8Array(data.buffer, 0, getBlockSize(format));
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");
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);
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);
556 function makeImage(imageWidth, imageHeight, dataWidth, data, alpha) {
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);
573 return wtu.makeImageFromCanvas(c);
575 function compareRect(
576 actualWidth, actualHeight, actualChannels,
577 dataWidth, dataHeight, expectedData,
578 testData, testFormat) {
579 var actual = new Uint8Array(actualWidth * actualHeight * 4);
581 0, 0, actualWidth, actualHeight, gl.RGBA, gl.UNSIGNED_BYTE, actual);
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);
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;
600 expectedData[expectedOffset + 0],
601 expectedData[expectedOffset + 1],
602 expectedData[expectedOffset + 2],
603 (actualChannels == 3 ? 255 : expectedData[expectedOffset + 3])
605 for (var jj = 0; jj < 4; ++jj) {
606 if (actual[actualOffset + jj] != expected[jj]) {
608 var was = actual[actualOffset + 0].toString();
609 for (var j = 1; j < 4; ++j) {
610 was += "," + actual[actualOffset + j];
612 testFailed('at (' + xx + ', ' + yy +
613 ') expected: ' + expected + ' was ' + was);
619 testPassed("texture rendered correctly");
623 function testPVRTCTextures() {
624 testFailed("PVRTC test not yet implemented");
628 var successfullyParsed = true;
630 <script src="../../resources/js-test-post.js"></script>