copy-tex-image-2d-formats.html (4779B)
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 <title>Verify copyTexImage2D follows format restictions</title> 11 <meta charset="utf-8"> 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 <canvas id="canvas" width="2" height="2"></canvas> 19 <canvas id="canvasNoAlpha" width="2" height="2"></canvas> 20 <div id="console"></div> 21 <script> 22 "use strict"; 23 24 function getChannelsFromFormat(format) { 25 switch (gl[format]) { 26 case gl.ALPHA: 27 return 0x0001; 28 case gl.LUMINANCE: 29 case gl.RGB: 30 return 0x1110; 31 case gl.LUMINANCE_ALPHA: 32 case gl.RGBA: 33 return 0x1111; 34 default: 35 return 0; 36 } 37 } 38 39 var formats = [ 40 'ALPHA', 41 'LUMINANCE', 42 'LUMINANCE_ALPHA', 43 'RGB', 44 'RGBA' 45 ]; 46 47 var isRenderable = { 48 'ALPHA': false, 49 'LUMINANCE': false, 50 'LUMINANCE_ALPHA': false, 51 'RGB': true, 52 'RGBA': true 53 }; 54 55 var gl = null; 56 var wtu = WebGLTestUtils; 57 58 description(); 59 60 var canvas = document.getElementById("canvas"); 61 var canvasNoAlpha = document.getElementById("canvasNoAlpha"); 62 var gl = wtu.create3DContext(canvas, {alpha:true}); 63 var glNoAlpha = wtu.create3DContext(canvasNoAlpha, {alpha:false}); 64 65 debug("test with an RGBA backbuffer"); 66 var program = wtu.setupTexturedQuad(gl); 67 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "During Initialization"); 68 testFormats('RGBA'); 69 70 testBackbufferFormats(); 71 72 debug("test with an RGB backbuffer"); 73 var gl = glNoAlpha; 74 var program = wtu.setupTexturedQuad(gl); 75 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "During Initialization"); 76 testFormats('RGB'); 77 78 function testBackbufferFormats() { 79 var fbo = gl.createFramebuffer(); 80 gl.bindFramebuffer(gl.FRAMEBUFFER, fbo); 81 82 for (var ii = 0; ii < formats.length; ++ii) { 83 var backFormat = formats[ii]; 84 85 var tex = gl.createTexture(); 86 gl.bindTexture(gl.TEXTURE_2D, tex); 87 gl.texImage2D(gl.TEXTURE_2D, 0, gl[backFormat], 2, 2, 0, gl[backFormat], gl.UNSIGNED_BYTE, null); 88 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); 89 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); 90 91 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0); 92 var status = gl.checkFramebufferStatus(gl.FRAMEBUFFER); 93 94 debug(''); 95 if (status == gl.FRAMEBUFFER_COMPLETE) { 96 if (!isRenderable[backFormat]) { 97 testFailed('Creating framebuffer from ' + backFormat + ' texture succeeded even though it is not a renderable format'); 98 } else { 99 debug('test with ' + backFormat + ' fbo'); 100 testFormats(backFormat); 101 } 102 } else { 103 debug(backFormat + ' not supported as a renderbuffer attachment'); 104 } 105 } 106 } 107 108 function testFormats(backFormat) { 109 for (var ii = 0; ii < formats.length; ++ii) { 110 testCopyTexImage2D(backFormat, formats[ii]); 111 } 112 } 113 114 function toChannels(value) { 115 return ((value & 0x1000) ? 'R' : '_') + 116 ((value & 0x0100) ? 'G' : '_') + 117 ((value & 0x0010) ? 'B' : '_') + 118 ((value & 0x0001) ? 'A' : '_'); 119 } 120 121 function testCopyTexImage2D(backFormat, texFormat) { 122 var need = getChannelsFromFormat(texFormat); 123 var have = getChannelsFromFormat(backFormat); 124 var shouldPass = (need & have) == need; 125 126 //debug("need: " + toChannels(need)); 127 //debug("have: " + toChannels(have)); 128 //debug("both: " + toChannels(have & need)); 129 130 // clear backbuffer 131 gl.clearColor(0.25, 1, 0.75, 0.5); 132 gl.clear(gl.COLOR_BUFFER_BIT); 133 134 var texture = gl.createTexture(); 135 // Bind the texture to texture unit 0 136 gl.bindTexture(gl.TEXTURE_2D, texture); 137 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); 138 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); 139 140 gl.copyTexImage2D(gl.TEXTURE_2D, 0, gl[texFormat], 0, 0, 2, 2, 0); 141 if (!shouldPass) { 142 wtu.glErrorShouldBe( 143 gl, gl.INVALID_OPERATION, 144 "should not be able to copyTexImage2D " + texFormat + " from " + backFormat); 145 return; 146 } 147 148 wtu.glErrorShouldBe( 149 gl, gl.NO_ERROR, 150 "should be able to copyTexImage2D " + texFormat + " from " + backFormat); 151 152 // Draw 153 wtu.clearAndDrawUnitQuad(gl); 154 155 var expectedColors = { 156 'ALPHA': [0, 0, 0, 127], 157 'LUMINANCE': [64, 64, 64, 255], 158 'LUMINANCE_ALPHA': [64, 64, 64, 127], 159 'RGB': [64, 255, 191, 255], 160 'RGBA': [64, 255, 191, 127] 161 }; 162 163 var color = expectedColors[texFormat]; 164 165 wtu.checkCanvas(gl, color, "should be " + color, 16); 166 167 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors"); 168 } 169 var successfullyParsed = true; 170 </script> 171 <script src="../../../js/js-test-post.js"></script> 172 </body> 173 </html>