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