texture-attachment-formats.html (5156B)
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 Texture Attachment Format 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 <canvas id="canvas" width="2" height="2" style="width: 100px; height:100px; border: 1px solid black;"> </canvas> 20 <script> 21 "use strict"; 22 description(); 23 24 var wtu = WebGLTestUtils; 25 var gl = wtu.create3DContext("canvas"); 26 if (!gl) { 27 testFailed("context does not exist"); 28 } else { 29 testPassed("context exists"); 30 31 debug(""); 32 debug("Checking texture formats."); 33 34 var numValidFormats = 0; 35 var clearColor = [0.25, 0.5, 0.75, 0.25]; 36 37 var floatToBits = function(value, bits) { 38 var range = (1 << bits) - 1; 39 var result = 0; 40 if (range > 0) { 41 result = Math.floor(Math.floor(value * range) * 255 / range); 42 } 43 44 //debug("v = " + value + ", bits = " + bits + ", range = " + range + ", result = " + result); 45 return result; 46 } 47 48 var testFormat = function(info) { 49 debug(""); 50 debug("testing: " + info.format + ", " + info.type); 51 52 var format = gl[info.format]; 53 var type = gl[info.type]; 54 55 gl.texImage2D(gl.TEXTURE_2D, 56 0, // level 57 format, // internalFormat 58 16, // width 59 16, // height 60 0, // border 61 format, // format 62 type, // type 63 null); // data 64 var fbStatus = gl.checkFramebufferStatus(gl.FRAMEBUFFER); 65 debug(wtu.glEnumToString(gl, fbStatus)); 66 if (fbStatus != gl.FRAMEBUFFER_COMPLETE) { 67 debug("format unsupported"); 68 if (info.mustBeFramebufferComplete) { 69 testFailed(info.format + " must be FRAMEBUFFER_COMPLETE"); 70 } 71 return; 72 } 73 74 ++numValidFormats; 75 76 var startExpected = [0, 0, 0, info.channels[3] < 0 ? 255 : 0]; 77 78 var expected = []; 79 var tolerance = []; 80 for (var ii = 0; ii < 4; ++ii) { 81 var color = 0; 82 var channel = info.channels[ii]; 83 if (channel < 0) { 84 color = ii < 3 ? 0 : 255 85 } else { 86 color = floatToBits(clearColor[channel], info.bits[ii]); 87 } 88 expected.push(color); 89 tolerance.push(channel < 0 ? 0 : (1 + (1 << (8 - info.bits[ii])))); 90 } 91 92 wtu.checkCanvas(gl, startExpected, "should be " + startExpected); 93 gl.clear(gl.COLOR_BUFFER_BIT); 94 wtu.checkCanvas(gl, expected, "should be " + expected + " with tolerance " + tolerance, tolerance); 95 } 96 97 var validFormats = [ 98 { format: 'RGBA', 99 type: 'UNSIGNED_BYTE', 100 channels: [0, 1, 2, 3], 101 bits: [8, 8, 8, 8], 102 mustBeFramebufferComplete: true 103 }, 104 { format: 'ALPHA', 105 type: 'UNSIGNED_BYTE', 106 channels: [-1, -1, -1, 3], 107 bits: [0, 0, 0, 8], 108 mustBeFramebufferComplete: false 109 }, 110 { format: 'RGB', 111 type: 'UNSIGNED_BYTE', 112 channels: [0, 1, 2, -1], 113 bits: [8, 8, 8, 0], 114 mustBeFramebufferComplete: false 115 }, 116 { format: 'RGB', 117 type: 'UNSIGNED_SHORT_5_6_5', 118 channels: [0, 1, 2, -1], 119 bits: [5, 6, 5, 0], 120 mustBeFramebufferComplete: false 121 }, 122 { format: 'RGBA', 123 type: 'UNSIGNED_SHORT_5_5_5_1', 124 channels: [0, 1, 2, 3], 125 bits: [5, 5, 5, 1], 126 mustBeFramebufferComplete: false 127 }, 128 { format: 'RGBA', 129 type: 'UNSIGNED_SHORT_4_4_4_4', 130 channels: [0, 1, 2, 3], 131 bits: [4, 4, 4, 4], 132 mustBeFramebufferComplete: false 133 }, 134 { format: 'LUMINANCE', 135 type: 'UNSIGNED_BYTE', 136 channels: [0, 0, 0, -1], 137 bits: [8, 8, 8, -1], 138 mustBeFramebufferComplete: false 139 }, 140 { format: 'LUMINANCE_ALPHA', 141 type: 'UNSIGNED_BYTE', 142 channels: [0, 0, 0, 3], 143 bits: [8, 8, 8, 8], 144 mustBeFramebufferComplete: false 145 } 146 ]; 147 148 gl.clearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]); 149 var fbo = gl.createFramebuffer(); 150 gl.bindFramebuffer(gl.FRAMEBUFFER, fbo); 151 var tex = gl.createTexture(); 152 gl.bindTexture(gl.TEXTURE_2D, tex); 153 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); 154 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); 155 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); 156 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); 157 gl.framebufferTexture2D( 158 gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0); 159 160 for (var ii = 0; ii < validFormats.length; ++ii) { 161 var info = validFormats[ii]; 162 testFormat(info); 163 } 164 165 debug(""); 166 shouldBeTrue("numValidFormats > 0"); 167 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors"); 168 } 169 170 debug(""); 171 var successfullyParsed = true; 172 </script> 173 <script src="../../../js/js-test-post.js"></script> 174 175 </body> 176 </html>