copytexsubimage2d-subrects.html (4660B)
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>CopyTexSubImage2D partial destination texture test</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 <canvas id="canvas"></canvas> 19 <div id="console"></div> 20 21 <script> 22 "use strict"; 23 description("Verifies that copyTexSubImage2D redefining part of the destination texture works as expected."); 24 25 //// 26 27 var kWidth = 16; 28 var kHeight = 16; 29 30 //// 31 32 var wtu = WebGLTestUtils; 33 var canvas = document.getElementById("canvas"); 34 35 canvas.width = kWidth; 36 canvas.height = kHeight; 37 var gl = wtu.create3DContext(canvas); 38 39 //// 40 41 function clearTo(color) { 42 gl.clearColor(color[0], color[1], color[2], color[3]); 43 gl.clear(gl.COLOR_BUFFER_BIT); 44 } 45 46 function readInto(view) { 47 gl.readPixels(0, 0, kWidth, kHeight, gl.RGBA, gl.UNSIGNED_BYTE, 48 new Uint8Array(view.buffer)); 49 } 50 51 //// 52 53 function runTest() { 54 gl.enable(gl.SCISSOR_TEST); 55 56 gl.scissor(0, 0, kWidth/2, kHeight/2); 57 clearTo([1,0,0,1]); 58 gl.scissor(kWidth/2, 0, kWidth/2, kHeight/2); 59 clearTo([0,1,0,1]); 60 gl.scissor(0, kHeight/2, kWidth/2, kHeight/2); 61 clearTo([0,0,1,1]); 62 gl.scissor(kWidth/2, kHeight/2, kWidth/2, kHeight/2); 63 clearTo([0,1,1,1]); 64 65 var srcData = new Uint32Array(kWidth * kHeight); 66 readInto(srcData); 67 console.log('0x' + srcData[0].toString(16)); 68 69 //// 70 71 var dstTex = gl.createTexture(); 72 gl.bindTexture(gl.TEXTURE_2D, dstTex); 73 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, kWidth, kHeight, 74 0, gl.RGBA, gl.UNSIGNED_BYTE, null); // Uploads zeros. 75 var dstRefData = new Uint32Array(kWidth * kHeight); // Also cleared to zeros! 76 var dstTestData = new Uint32Array(kWidth * kHeight); 77 78 var dstFB = gl.createFramebuffer(); 79 gl.bindFramebuffer(gl.FRAMEBUFFER, dstFB); 80 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, 81 gl.TEXTURE_2D, dstTex, 0); 82 83 //// 84 85 function pixelPos(x, y) { 86 return y * kWidth + x; 87 } 88 89 function testCmd(tuple) { 90 var dstX0, dstY0, srcX0, srcY0, width, height; 91 [dstX0, dstY0, srcX0, srcY0, width, height] = tuple 92 debug("copyTexSubImage2D(" + 93 [dstX0+','+dstY0, srcX0+','+srcY0, width+','+height].join(', ') + 94 ")"); 95 96 // Test 97 gl.bindFramebuffer(gl.FRAMEBUFFER, null); 98 gl.copyTexSubImage2D(gl.TEXTURE_2D, 0, 99 dstX0, dstY0, srcX0, srcY0, width, height); 100 101 // Emulate for reference 102 for (var x = 0; x < width; x++) { 103 var srcX = srcX0 + x; 104 var dstX = dstX0 + x; 105 if (srcX < 0 || srcX >= kWidth || 106 dstX < 0 || dstX >= kWidth) 107 { 108 continue; 109 } 110 111 for (var y = 0; y < height; y++) { 112 var srcY = srcY0 + y; 113 var dstY = dstY0 + y; 114 if (srcY < 0 || srcY >= kHeight || 115 dstY < 0 || dstY >= kHeight) 116 { 117 continue; 118 } 119 120 121 var srcPos = pixelPos(srcX, srcY); 122 var dstPos = pixelPos(dstX, dstY); 123 dstRefData[dstPos] = srcData[srcPos]; 124 } 125 } 126 127 // Compare 128 gl.bindFramebuffer(gl.FRAMEBUFFER, dstFB); 129 readInto(dstTestData); 130 131 for (var x = 0; x < kWidth; x++) { 132 for (var y = 0; y < kHeight; y++) { 133 var pos = pixelPos(x, y); 134 var refPixel = dstRefData[pos]; 135 var testPixel = dstTestData[pos]; 136 137 //console.log([x, y].join(",") + ":", 138 // testPixel.toString(16), refPixel.toString(16)) 139 if (testPixel == refPixel) 140 continue; 141 142 testFailed("Mismatch at (" + [x, y].join(", ") + "): " + 143 " Should be 0x" + refPixel.toString(16) + 144 ", was 0x" + testPixel.toString(16)); 145 return false; 146 } 147 } 148 149 return true; 150 } 151 152 var tests = [ 153 [0,0, 0,0, 2,3], 154 [0,0, 5,8, 2,3], 155 [1,0, 0,0, 2,3], 156 [1,7, 0,0, 2,3], 157 ]; 158 159 tests.every(x => testCmd(x)); 160 } 161 162 runTest(); 163 164 debug(""); 165 var successfullyParsed = true; 166 </script> 167 <script src="../../../js/js-test-post.js"></script> 168 169 </body> 170 </html>