content/canvas/test/webgl-conformance/conformance/extensions/webgl-compressed-texture-s3tc.html

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

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

mercurial