ext-texture-norm16.html (8578B)
1 <!-- 2 Copyright (c) 2019 The Khronos Group Inc. 3 Use of this source code is governed by an MIT-style license that can be 4 found in the LICENSE.txt file. 5 --> 6 7 <!DOCTYPE html> 8 <html> 9 <head> 10 <meta charset="utf-8"> 11 <title>WebGL EXT_texture_norm16 Conformance Tests</title> 12 <LINK rel="stylesheet" href="../../resources/js-test-style.css"/> 13 <script src="../../js/js-test-pre.js"></script> 14 <script src="../../js/webgl-test-utils.js"></script> 15 </head> 16 <body> 17 <div id="description"></div> 18 <div id="console"></div> 19 <script> 20 "use strict"; 21 description("This test verifies the functionality of the EXT_texture_norm16 extension, if it is available."); 22 23 debug(""); 24 25 var wtu = WebGLTestUtils; 26 var gl = wtu.create3DContext(null, null, 2); 27 var ext; 28 29 var formats = null; 30 var textures; 31 var fbos; 32 var renderbuffer; 33 var readbackBuf = new Uint16Array(4); 34 35 function generateFormatColor(format, value, alpha) { 36 alpha = alpha !== undefined ? alpha : 255; 37 switch(format) { 38 case gl.RED: 39 return [value, 0, 0, alpha]; 40 case gl.RG: 41 return [value, value, 0, alpha]; 42 case gl.RGB: 43 return [value, value, value, alpha]; 44 case gl.RGBA: 45 return [value, value, value, value]; 46 default: 47 wtu.error("Unreachable: Unknown format."); 48 return null; 49 } 50 } 51 52 function testNorm16Texture(internalFormat, format, type, error="NO_ERROR") { 53 debug(`\ntestNorm16Texture(${[].slice.call(arguments).join(", ")})`); 54 let pixelValue; 55 let expectedValue; 56 let imageData; 57 58 switch(gl[type]) { 59 case gl.SHORT: 60 pixelValue = 0x7fff; 61 expectedValue = 0xff; 62 imageData = new Int16Array(4).fill(pixelValue); 63 break; 64 case gl.UNSIGNED_SHORT: 65 pixelValue = 0x6a35; 66 expectedValue = pixelValue >> 8; 67 imageData = new Uint16Array(4).fill(pixelValue); 68 break; 69 default: 70 wtu.error("Unreachable: Unknown texture type."); 71 break; 72 } 73 74 // Texture sampled from 75 gl.activeTexture(gl.TEXTURE0); 76 gl.bindTexture(gl.TEXTURE_2D, textures[0]); 77 gl.texImage2D(gl.TEXTURE_2D, 0, ext[internalFormat] || gl[internalFormat], 78 1, 1, 0, gl[format], gl[type], imageData); 79 80 wtu.glErrorShouldBe(gl, gl[error], `texImage should generate error:${error}`); 81 if (gl[error]) return; 82 83 gl.drawArrays(gl.TRIANGLES, 0, 6); 84 85 // Read back as gl.UNSIGNED_BYTE 86 wtu.checkCanvasRect(gl, 0, 0, 1, 1, generateFormatColor(gl[format], expectedValue)); 87 } 88 89 function testNorm16Render(interalFormat, format, type, tolerance) { 90 // Only UNSIGNED_SHORT are renderable 91 let pixelValue = 0x6a35; 92 let expectedValue = pixelValue; 93 let imageData = new Uint16Array(4).fill(pixelValue); 94 95 // Render to fbo texture attachment test 96 gl.bindTexture(gl.TEXTURE_2D, textures[1]); 97 gl.texImage2D(gl.TEXTURE_2D, 0, interalFormat, 1, 1, 0, format, type, null); 98 99 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "rtt bindings succeed"); 100 101 gl.bindFramebuffer(gl.FRAMEBUFFER, fbos[0]); 102 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, textures[1], 0); 103 104 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "fbo bindings succeed"); 105 106 gl.activeTexture(gl.TEXTURE0); 107 gl.bindTexture(gl.TEXTURE_2D, textures[0]); 108 gl.texImage2D(gl.TEXTURE_2D, 0, interalFormat, 1, 1, 0, format, type, imageData); 109 110 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "texture bindings succeed"); 111 112 gl.drawArrays(gl.TRIANGLES, 0, 6); 113 114 wtu.checkCanvasRect(gl, 0, 0, 1, 1, generateFormatColor(format, expectedValue, 0xffff), undefined, tolerance, readbackBuf, type); 115 116 // Renderbuffer test 117 gl.bindFramebuffer(gl.FRAMEBUFFER, fbos[1]); 118 gl.bindRenderbuffer(gl.RENDERBUFFER, renderbuffer); 119 gl.renderbufferStorage(gl.RENDERBUFFER, interalFormat, 1, 1); 120 gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, 121 renderbuffer); 122 gl.bindRenderbuffer(gl.RENDERBUFFER, null); 123 124 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "renderbuffer bindings succeed"); 125 126 gl.clearColor(1, 1, 1, 1); 127 gl.clear(gl.COLOR_BUFFER_BIT); 128 129 wtu.checkCanvasRect(gl, 0, 0, 1, 1, generateFormatColor(format, 0xffff, 0xffff), undefined, tolerance, readbackBuf, type); 130 131 // Copy from renderbuffer to textures[1] test 132 gl.bindTexture(gl.TEXTURE_2D, textures[1]); 133 gl.copyTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 0, 0, 1, 1); 134 135 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "copy succeed"); 136 137 gl.bindFramebuffer(gl.FRAMEBUFFER, fbos[0]); 138 wtu.checkCanvasRect(gl, 0, 0, 1, 1, generateFormatColor(format, 0xffff, 0xffff), undefined, tolerance, readbackBuf, type); 139 140 gl.bindFramebuffer(gl.FRAMEBUFFER, null); 141 gl.bindTexture(gl.TEXTURE_2D, null); 142 } 143 144 function testExtFormatUnrenderable(internalFormatName, format, type) { 145 gl.bindTexture(gl.TEXTURE_2D, textures[1]); 146 gl.texImage2D(gl.TEXTURE_2D, 0, ext[internalFormatName], 1, 1, 0, format, type, null); 147 148 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "texture definition succeeded"); 149 150 gl.bindFramebuffer(gl.FRAMEBUFFER, fbos[0]); 151 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, textures[1], 0); 152 153 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "fbo binding succeeded"); 154 155 wtu.framebufferStatusShouldBe(gl, gl.FRAMEBUFFER, [ gl.FRAMEBUFFER_INCOMPLETE_ATTACHMENT, gl.FRAMEBUFFER_UNSUPPORTED ], 156 `framebuffer should not be complete with ${internalFormatName} texture attached`); 157 } 158 159 function runInternalFormatQueryTest() 160 { 161 debug(""); 162 debug("testing the internal format query"); 163 164 var maxSamples = gl.getParameter(gl.MAX_SAMPLES); 165 const formats = [ext.R16_EXT, ext.RG16_EXT, ext.RGBA16_EXT]; 166 for (const format of formats) { 167 var samples = gl.getInternalformatParameter(gl.RENDERBUFFER, format, gl.SAMPLES); 168 if (samples == null || samples.length == 0 || samples[0] < maxSamples) { 169 testFailed("the maximum value in SAMPLES should be at least " + maxSamples); 170 return; 171 } 172 } 173 testPassed("Internal format query succeeded"); 174 } 175 176 function runTestExtension() { 177 textures = [gl.createTexture(), gl.createTexture()]; 178 fbos = [gl.createFramebuffer(), gl.createFramebuffer()]; 179 renderbuffer = gl.createRenderbuffer(); 180 181 for (let i = 0; i < 2; i++) { 182 gl.bindTexture(gl.TEXTURE_2D, textures[i]); 183 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); 184 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); 185 } 186 187 gl.bindTexture(gl.TEXTURE_2D, null); 188 189 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "texture and framebuffer setup succeed"); 190 191 let program300 = wtu.setupSimpleTextureProgramESSL300(gl); 192 let program100 = wtu.setupTexturedQuad(gl, 0, 1, wtu.simpleHighPrecisionTextureFragmentShader); 193 194 debug(""); 195 debug("Texture creation"); 196 testNorm16Texture("R16_EXT", "RED", "UNSIGNED_SHORT"); 197 testNorm16Texture("RG16_EXT", "RG", "UNSIGNED_SHORT"); 198 testNorm16Texture("RGB16_EXT", "RGB", "UNSIGNED_SHORT"); 199 testNorm16Texture("RGBA16_EXT", "RGBA", "UNSIGNED_SHORT"); 200 testNorm16Texture("R16_SNORM_EXT", "RED", "SHORT"); 201 testNorm16Texture("RG16_SNORM_EXT", "RG", "SHORT"); 202 testNorm16Texture("RGB16_SNORM_EXT", "RGB", "SHORT"); 203 testNorm16Texture("RGBA16_SNORM_EXT", "RGBA", "SHORT"); 204 205 testNorm16Texture("RGBA", "RGBA", "UNSIGNED_SHORT", "INVALID_OPERATION"); 206 testNorm16Texture("RGBA", "RGBA", "SHORT", "INVALID_OPERATION"); 207 208 debug(""); 209 debug("Texture renderability"); 210 211 testNorm16Render(ext.R16_EXT, gl.RED, gl.UNSIGNED_SHORT, 8); 212 testNorm16Render(ext.RG16_EXT, gl.RG, gl.UNSIGNED_SHORT, 8); 213 testNorm16Render(ext.RGBA16_EXT, gl.RGBA, gl.UNSIGNED_SHORT, 8); 214 215 gl.useProgram(program300); 216 217 testNorm16Render(ext.R16_EXT, gl.RED, gl.UNSIGNED_SHORT, 0); 218 testNorm16Render(ext.RG16_EXT, gl.RG, gl.UNSIGNED_SHORT, 0); 219 testExtFormatUnrenderable("RGB16_EXT", gl.RGB, gl.UNSIGNED_SHORT); 220 testNorm16Render(ext.RGBA16_EXT, gl.RGBA, gl.UNSIGNED_SHORT, 0); 221 222 testExtFormatUnrenderable("R16_SNORM_EXT", gl.RED, gl.SHORT); 223 testExtFormatUnrenderable("RG16_SNORM_EXT", gl.RG, gl.SHORT); 224 testExtFormatUnrenderable("RGB16_SNORM_EXT", gl.RGB, gl.SHORT); 225 testExtFormatUnrenderable("RGBA16_SNORM_EXT", gl.RGBA, gl.SHORT); 226 }; 227 228 function runTest() { 229 if (!gl) { 230 testFailed("context does not exist"); 231 } else { 232 testPassed("context exists"); 233 234 ext = gl.getExtension("EXT_texture_norm16"); 235 236 wtu.runExtensionSupportedTest(gl, "EXT_texture_norm16", ext !== null); 237 238 if (ext !== null) { 239 runInternalFormatQueryTest(); 240 runTestExtension(); 241 } else { 242 testPassed("No EXT_texture_norm16 support -- this is legal"); 243 } 244 } 245 } 246 247 runTest(); 248 249 var successfullyParsed = true; 250 </script> 251 <script src="../../js/js-test-post.js"></script> 252 </body> 253 </html>