copyTexSubImage2D.html (3923B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <!-- 6 Copyright (c) 2019 The Khronos Group Inc. 7 Use of this source code is governed by an MIT-style license that can be 8 found in the LICENSE.txt file. 9 --> 10 <link rel="stylesheet" type="text/css" href="../unit.css" /> 11 <script type="application/javascript" src="../unit.js"></script> 12 <script type="application/javascript" src="../util.js"></script> 13 <script type="application/javascript"> 14 15 Tests.startUnit = function () { 16 var canvas = document.getElementById('gl'); 17 var gl = wrapGLContext(getGLContext(canvas)); 18 return [gl]; 19 } 20 21 Tests.setup = function(gl) { 22 var tex = gl.createTexture(); 23 gl.bindTexture(gl.TEXTURE_2D, tex); 24 var texCubeMap = gl.createTexture(); 25 gl.bindTexture(gl.TEXTURE_CUBE_MAP, texCubeMap); 26 return [gl] 27 } 28 29 Tests.teardown = function(gl,tex,texCubeMap) { 30 gl.bindTexture(gl.TEXTURE_2D, null); 31 gl.bindTexture(gl.TEXTURE_CUBE_MAP, null); 32 gl.deleteTexture(tex); 33 gl.deleteTexture(texCubeMap); 34 } 35 36 37 Tests.testTexImage2D = function(gl) { 38 gl.copyTexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 0,0,16,16,0); 39 gl.copyTexSubImage2D(gl.TEXTURE_2D, 0, 0,0, 0,0,1,1); 40 gl.copyTexSubImage2D(gl.TEXTURE_2D, 0, 0,0, 0,0,2,1); 41 gl.copyTexSubImage2D(gl.TEXTURE_2D, 0, 0,0, 0,0,1,2); 42 gl.copyTexSubImage2D(gl.TEXTURE_2D, 0, 0,0, 0,0,16,16); 43 gl.copyTexSubImage2D(gl.TEXTURE_2D, 0, 0,0, 15,15,1,1); 44 gl.copyTexSubImage2D(gl.TEXTURE_2D, 0, 1,1, 0,0,15,15); 45 gl.copyTexSubImage2D(gl.TEXTURE_2D, 0, 15,15, 0,0,1,1); 46 var valid_targets = [ 47 gl.TEXTURE_2D, 48 gl.TEXTURE_CUBE_MAP_POSITIVE_X, 49 gl.TEXTURE_CUBE_MAP_NEGATIVE_X, 50 gl.TEXTURE_CUBE_MAP_POSITIVE_Y, 51 gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, 52 gl.TEXTURE_CUBE_MAP_POSITIVE_Z, 53 gl.TEXTURE_CUBE_MAP_NEGATIVE_Z 54 ]; 55 valid_targets.forEach(function(t) { 56 assertOk(function(){ 57 gl.copyTexImage2D(t, 0, gl.RGBA, 0,0,1,1,0); 58 gl.copyTexSubImage2D(t, 0, 0,0,0,0,1,1); 59 }); 60 }); 61 } 62 Tests.testRoundtrip = function(gl) { 63 var sh = new Filter(gl, 'identity-flip-vert', 'identity-frag'); 64 gl.clearColor(1.0, 0.0, 0.0, 1.0); 65 gl.clear(gl.COLOR_BUFFER_BIT); 66 var buf = new Uint8Array(4); 67 gl.readPixels(0,0,1,1,gl.RGBA, gl.UNSIGNED_BYTE, buf); 68 assertEquals([255,0,0,255], [buf[0], buf[1], buf[2], buf[3]]); 69 // red texture 70 gl.clearColor(0.0, 0.0, 0.0, 0.0); 71 gl.clear(gl.COLOR_BUFFER_BIT); 72 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, document.getElementById('gl')); 73 gl.clearColor(1.0, 0.0, 0.0, 1.0); 74 gl.clear(gl.COLOR_BUFFER_BIT); 75 gl.copyTexSubImage2D(gl.TEXTURE_2D, 0, 0,0,0,0,16,16); 76 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); 77 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); 78 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); 79 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); 80 gl.clearColor(0.0, 0.0, 1.0, 1.0); 81 gl.clear(gl.COLOR_BUFFER_BIT); 82 // blue framebuffer 83 gl.readPixels(0,0,1,1,gl.RGBA, gl.UNSIGNED_BYTE, buf); 84 assertEquals([0,0,255,255], [buf[0], buf[1], buf[2], buf[3]]); 85 sh.apply(); // paint with texture 86 gl.readPixels(0,0,1,1,gl.RGBA, gl.UNSIGNED_BYTE, buf); 87 assertEquals([255,0,0,255], [buf[0], buf[1], buf[2], buf[3]]); 88 sh.destroy(); 89 } 90 91 Tests.endUnit = function(gl) { 92 } 93 94 </script> 95 <script id="identity-flip-vert" type="x-shader/x-vertex"> 96 attribute vec3 Vertex; 97 attribute vec2 Tex; 98 99 varying vec4 texCoord0; 100 void main() 101 { 102 texCoord0 = vec4(Tex.s, 1.0-Tex.t, 0.0, 0.0); 103 gl_Position = vec4(Vertex, 1.0); 104 } 105 </script> 106 <script id="identity-frag" type="x-shader/x-fragment"> 107 precision mediump float; 108 109 uniform sampler2D Texture; 110 111 varying vec4 texCoord0; 112 void main() 113 { 114 vec4 c = texture2D(Texture, texCoord0.st); 115 gl_FragColor = c; 116 } 117 </script> 118 <style>canvas{ position:absolute; }</style> 119 </head><body> 120 <canvas id="gl" width="16" height="16"></canvas> 121 </body></html>