compressed-tex-image.js (6197B)
1 "use strict"; 2 description("This test ensures WebGL implementations correctly implement querying for compressed textures when extensions are disabled."); 3 4 debug(""); 5 6 const wtu = WebGLTestUtils; 7 const gl = wtu.create3DContext(null, undefined, contextVersion); 8 9 const COMPRESSED_RGB_PVRTC_4BPPV1_IMG = 0x8C00; 10 const COMPRESSED_RGBA_PVRTC_4BPPV1_IMG = 0x8C02; 11 12 let formats = null; 13 let ext; 14 15 if (!gl) { 16 testFailed("context does not exist"); 17 } else { 18 testPassed("context exists"); 19 20 var tex = gl.createTexture(); 21 gl.bindTexture(gl.TEXTURE_2D, tex); 22 wtu.shouldGenerateGLError(gl, [gl.INVALID_ENUM, gl.INVALID_OPERATION], 23 "gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 10, 10, COMPRESSED_RGB_PVRTC_4BPPV1_IMG, new Uint8Array(8));"); 24 25 wtu.shouldGenerateGLError(gl, gl.INVALID_ENUM, "gl.compressedTexImage2D(gl.TEXTURE_2D, 0, COMPRESSED_RGB_PVRTC_4BPPV1_IMG, 8, 8, 0, new Uint8Array(8))"); 26 wtu.shouldGenerateGLError(gl, gl.INVALID_ENUM, "gl.compressedTexImage2D(gl.TEXTURE_2D, 0, COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, 8, 8, 0, new Uint8Array(8))"); 27 28 wtu.shouldGenerateGLError(gl, gl.NO_ERROR, "formats = gl.getParameter(gl.COMPRESSED_TEXTURE_FORMATS)"); 29 shouldBeNonNull("formats"); 30 shouldBe("formats.length", "0"); 31 32 wtu.shouldGenerateGLError(gl, gl.NO_ERROR, "gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 4, 4, 0, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array(4*4*4));"); 33 wtu.shouldGenerateGLError(gl, gl.INVALID_ENUM, 34 "gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 4, 4, COMPRESSED_RGB_PVRTC_4BPPV1_IMG, new Uint8Array(8));"); 35 36 // Check too-many and too-few args. 37 38 wtu.shouldThrow(gl, false, "too many args", function() { 39 gl.compressedTexImage2D(gl.TEXTURE_2D, 0, COMPRESSED_RGB_PVRTC_4BPPV1_IMG, 4, 4, 0, new Uint8Array(8), null); 40 }); 41 wtu.shouldThrow(gl, TypeError, "too few args", function() { 42 gl.compressedTexImage2D(gl.TEXTURE_2D, 0, COMPRESSED_RGB_PVRTC_4BPPV1_IMG, 4, 4, 0); 43 }); 44 45 wtu.shouldThrow(gl, false, "too many args", function() { 46 gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 4, 4, COMPRESSED_RGB_PVRTC_4BPPV1_IMG, new Uint8Array(8), null); 47 }); 48 wtu.shouldThrow(gl, TypeError, "too few args", function() { 49 gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 4, 4, COMPRESSED_RGB_PVRTC_4BPPV1_IMG); 50 }); 51 52 // - 53 54 let pbo; 55 // WebGL 2.0 specific 56 if (gl.PIXEL_UNPACK_BUFFER) { 57 pbo = gl.createBuffer(); 58 } 59 60 gl.bindTexture(gl.TEXTURE_2D, tex); 61 62 function validateExt(extName, enumName, blockSize, blockByteSize, expectedSubImageError) { 63 debug('\n---------------------------'); 64 debug('\n' + extName); 65 ext = gl.getExtension(extName); 66 if (!ext) { 67 testPassed(`Optional ext ${extName} MAY be unsupported.`); 68 return; 69 } 70 testPassed(`Optional ext ${extName} is supported.`); 71 72 const data = new Uint8Array(blockByteSize); 73 74 const views = [ 75 data, 76 new Uint8ClampedArray(data.buffer), 77 new Int8Array(data.buffer), 78 new Uint16Array(data.buffer), 79 new Int16Array(data.buffer), 80 new Uint32Array(data.buffer), 81 new Int32Array(data.buffer), 82 new Float32Array(data.buffer), 83 new DataView(data.buffer), 84 ]; 85 if (window.SharedArrayBuffer) { 86 const sharedBuffer = new SharedArrayBuffer(blockByteSize); 87 views.push( 88 new Uint8Array(sharedBuffer), 89 new Uint8ClampedArray(sharedBuffer), 90 new DataView(sharedBuffer) 91 ); 92 } 93 94 for (const view of views) { 95 window.g_view = view; 96 debug(`\nfrom ${view.constructor.name} of ${view.buffer.constructor.name}`); 97 wtu.shouldGenerateGLError(gl, gl.NO_ERROR, 98 `gl.compressedTexImage2D(gl.TEXTURE_2D, 0, ext.${enumName}, ${blockSize},${blockSize}, 0, g_view)`); 99 100 wtu.shouldGenerateGLError(gl, expectedSubImageError, 101 `gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0,0, ${blockSize},${blockSize}, ext.${enumName}, g_view)`); 102 } 103 104 if (pbo) { 105 debug('\nfrom PBO'); 106 gl.bindBuffer(gl.PIXEL_UNPACK_BUFFER, pbo); 107 wtu.shouldGenerateGLError(gl, gl.NO_ERROR, 108 `gl.bufferData(gl.PIXEL_UNPACK_BUFFER, ${blockByteSize}*2, gl.STATIC_DRAW)`); 109 110 wtu.shouldGenerateGLError(gl, gl.NO_ERROR, 111 `gl.compressedTexImage2D(gl.TEXTURE_2D, 0, ext.${enumName}, ${blockSize},${blockSize}, 0, ${blockByteSize}, 0)`); 112 wtu.shouldGenerateGLError(gl, gl.NO_ERROR, 113 `gl.compressedTexImage2D(gl.TEXTURE_2D, 0, ext.${enumName}, ${blockSize},${blockSize}, 0, ${blockByteSize}, 1)`); 114 wtu.shouldGenerateGLError(gl, gl.NO_ERROR, 115 `gl.compressedTexImage2D(gl.TEXTURE_2D, 0, ext.${enumName}, ${blockSize},${blockSize}, 0, ${blockByteSize}, ${blockByteSize})`); 116 wtu.shouldGenerateGLError(gl, gl.INVALID_OPERATION, 117 `gl.compressedTexImage2D(gl.TEXTURE_2D, 0, ext.${enumName}, ${blockSize},${blockSize}, 0, ${blockByteSize}, ${blockByteSize+1})`); 118 119 wtu.shouldGenerateGLError(gl, expectedSubImageError, 120 `gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0,0, ${blockSize},${blockSize}, ext.${enumName}, ${blockByteSize}, 0)`); 121 wtu.shouldGenerateGLError(gl, expectedSubImageError, 122 `gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0,0, ${blockSize},${blockSize}, ext.${enumName}, ${blockByteSize}, 1)`); 123 wtu.shouldGenerateGLError(gl, expectedSubImageError, 124 `gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0,0, ${blockSize},${blockSize}, ext.${enumName}, ${blockByteSize}, ${blockByteSize})`); 125 wtu.shouldGenerateGLError(gl, gl.INVALID_OPERATION, 126 `gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0,0, ${blockSize},${blockSize}, ext.${enumName}, ${blockByteSize}, ${blockByteSize+1})`); 127 128 gl.bindBuffer(gl.PIXEL_UNPACK_BUFFER, null); 129 } 130 } 131 132 validateExt('WEBGL_compressed_texture_s3tc', 'COMPRESSED_RGBA_S3TC_DXT5_EXT', 4, 16, gl.NO_ERROR); 133 validateExt('WEBGL_compressed_texture_etc1', 'COMPRESSED_RGB_ETC1_WEBGL', 4, 8, gl.INVALID_OPERATION); 134 validateExt('WEBGL_compressed_texture_etc', 'COMPRESSED_RGBA8_ETC2_EAC', 4, 16, gl.NO_ERROR); 135 validateExt('WEBGL_compressed_texture_astc', 'COMPRESSED_RGBA_ASTC_4x4_KHR', 4, 16, gl.NO_ERROR); 136 } 137 138 var successfullyParsed = true;