tex-image-and-sub-image-2d-with-svg-image.js (5655B)
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 function generateTest(internalFormat, pixelFormat, pixelType, prologue, resourcePath, defaultContextVersion) { 8 var wtu = WebGLTestUtils; 9 var tiu = TexImageUtils; 10 var gl = null; 11 var successfullyParsed = false; 12 var imgCanvas; 13 var redColor = [255, 0, 0]; 14 var greenColor = [0, 255, 0]; 15 16 function init() 17 { 18 description('Verify texImage2D and texSubImage2D code paths taking SVG image elements (' + internalFormat + '/' + pixelFormat + '/' + pixelType + ')'); 19 20 // Set the default context version while still allowing the webglVersion URL query string to override it. 21 wtu.setDefault3DContextVersion(defaultContextVersion); 22 gl = wtu.create3DContext("example"); 23 24 if (!prologue(gl)) { 25 finishTest(); 26 return; 27 } 28 29 switch (gl[pixelFormat]) { 30 case gl.RED: 31 case gl.RED_INTEGER: 32 greenColor = [0, 0, 0]; 33 break; 34 case gl.LUMINANCE: 35 case gl.LUMINANCE_ALPHA: 36 redColor = [255, 255, 255]; 37 greenColor = [0, 0, 0]; 38 break; 39 case gl.ALPHA: 40 redColor = [0, 0, 0]; 41 greenColor = [0, 0, 0]; 42 break; 43 default: 44 break; 45 } 46 47 gl.clearColor(0,0,0,1); 48 gl.clearDepth(1); 49 50 wtu.loadTexture(gl, resourcePath + "red-green.svg", runTest); 51 } 52 53 function runOneIteration(image, useTexSubImage2D, flipY, topColor, bottomColor, bindingTarget, program) 54 { 55 debug('Testing ' + (useTexSubImage2D ? 'texSubImage2D' : 'texImage2D') + 56 ' with flipY=' + flipY + ' bindingTarget=' + 57 (bindingTarget == gl.TEXTURE_2D ? 'TEXTURE_2D' : 'TEXTURE_CUBE_MAP')); 58 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); 59 // Disable any writes to the alpha channel 60 gl.colorMask(1, 1, 1, 0); 61 var texture = gl.createTexture(); 62 // Bind the texture to texture unit 0 63 gl.bindTexture(bindingTarget, texture); 64 // Set up texture parameters 65 gl.texParameteri(bindingTarget, gl.TEXTURE_MIN_FILTER, gl.NEAREST); 66 gl.texParameteri(bindingTarget, gl.TEXTURE_MAG_FILTER, gl.NEAREST); 67 // Set up pixel store parameters 68 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY); 69 gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false); 70 wtu.failIfGLError(gl, 'gl.pixelStorei(gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, gl.NONE);'); 71 var targets = [gl.TEXTURE_2D]; 72 if (bindingTarget == gl.TEXTURE_CUBE_MAP) { 73 targets = [gl.TEXTURE_CUBE_MAP_POSITIVE_X, 74 gl.TEXTURE_CUBE_MAP_NEGATIVE_X, 75 gl.TEXTURE_CUBE_MAP_POSITIVE_Y, 76 gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, 77 gl.TEXTURE_CUBE_MAP_POSITIVE_Z, 78 gl.TEXTURE_CUBE_MAP_NEGATIVE_Z]; 79 } 80 // Upload the image into the texture 81 for (var tt = 0; tt < targets.length; ++tt) { 82 if (useTexSubImage2D) { 83 // Initialize the texture to black first 84 gl.texImage2D(targets[tt], 0, gl[internalFormat], image.width, image.height, 0, 85 gl[pixelFormat], gl[pixelType], null); 86 gl.texSubImage2D(targets[tt], 0, 0, 0, gl[pixelFormat], gl[pixelType], image); 87 } else { 88 gl.texImage2D(targets[tt], 0, gl[internalFormat], gl[pixelFormat], gl[pixelType], image); 89 } 90 } 91 92 var loc; 93 if (bindingTarget == gl.TEXTURE_CUBE_MAP) { 94 loc = gl.getUniformLocation(program, "face"); 95 } 96 97 for (var tt = 0; tt < targets.length; ++tt) { 98 if (bindingTarget == gl.TEXTURE_CUBE_MAP) { 99 gl.uniform1i(loc, targets[tt]); 100 } 101 // Draw the triangles 102 wtu.clearAndDrawUnitQuad(gl, [0, 0, 0, 255]); 103 // Check a few pixels near the top and bottom and make sure they have 104 // the right color. 105 debug("Checking lower left corner"); 106 wtu.checkCanvasRect(gl, 4, 4, 2, 2, bottomColor, 107 "shouldBe " + bottomColor); 108 debug("Checking upper left corner"); 109 wtu.checkCanvasRect(gl, 4, gl.canvas.height - 8, 2, 2, topColor, 110 "shouldBe " + topColor); 111 } 112 } 113 114 function runTest(image) 115 { 116 var program = tiu.setupTexturedQuad(gl, internalFormat); 117 runTestOnBindingTarget(image, gl.TEXTURE_2D, program); 118 program = tiu.setupTexturedQuadWithCubeMap(gl, internalFormat); 119 runTestOnBindingTarget(image, gl.TEXTURE_CUBE_MAP, program); 120 121 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors"); 122 finishTest(); 123 } 124 125 function runTestOnBindingTarget(image, bindingTarget, program) { 126 var cases = [ 127 { sub: false, flipY: true, topColor: redColor, bottomColor: greenColor }, 128 { sub: false, flipY: false, topColor: greenColor, bottomColor: redColor }, 129 { sub: true, flipY: true, topColor: redColor, bottomColor: greenColor }, 130 { sub: true, flipY: false, topColor: greenColor, bottomColor: redColor }, 131 ]; 132 for (var i in cases) { 133 runOneIteration(image, cases[i].sub, cases[i].flipY, 134 cases[i].topColor, cases[i].bottomColor, 135 bindingTarget, program); 136 } 137 } 138 139 return init; 140 }