tex-image-and-sub-image-3d-with-svg-image.js (4117B)
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 texImage3D and texSubImage3D 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 default: 35 break; 36 } 37 38 gl.clearColor(0,0,0,1); 39 gl.clearDepth(1); 40 41 wtu.loadTexture(gl, resourcePath + "red-green.svg", runTest); 42 } 43 44 function runOneIteration(image, flipY, topColor, bottomColor, bindingTarget, program) 45 { 46 debug('Testing ' + ' with flipY=' + flipY + ' bindingTarget=' + 47 (bindingTarget == gl.TEXTURE_3D ? 'TEXTURE_3D' : 'TEXTURE_2D_ARRAY')); 48 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); 49 // Disable any writes to the alpha channel 50 gl.colorMask(1, 1, 1, 0); 51 var texture = gl.createTexture(); 52 // Bind the texture to texture unit 0 53 gl.bindTexture(bindingTarget, texture); 54 // Set up texture parameters 55 gl.texParameteri(bindingTarget, gl.TEXTURE_MIN_FILTER, gl.NEAREST); 56 gl.texParameteri(bindingTarget, gl.TEXTURE_MAG_FILTER, gl.NEAREST); 57 // Set up pixel store parameters 58 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY); 59 gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false); 60 wtu.failIfGLError(gl, 'gl.pixelStorei(gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, gl.NONE);'); 61 // Upload the image into the texture 62 // Initialize the texture to black first 63 gl.texImage3D(bindingTarget, 0, gl[internalFormat], image.width, image.height, 1 /* depth */, 0, 64 gl[pixelFormat], gl[pixelType], null); 65 gl.texSubImage3D(bindingTarget, 0, 0, 0, 0, image.width, image.height, 1 /* depth */, 66 gl[pixelFormat], gl[pixelType], image); 67 68 // Draw the triangles 69 wtu.clearAndDrawUnitQuad(gl, [0, 0, 0, 255]); 70 // Check a few pixels near the top and bottom and make sure they have 71 // the right color. 72 debug("Checking lower left corner"); 73 wtu.checkCanvasRect(gl, 4, 4, 2, 2, bottomColor, 74 "shouldBe " + bottomColor); 75 debug("Checking upper left corner"); 76 wtu.checkCanvasRect(gl, 4, gl.canvas.height - 8, 2, 2, topColor, 77 "shouldBe " + topColor); 78 } 79 80 function runTest(image) 81 { 82 var program = tiu.setupTexturedQuadWith3D(gl, internalFormat); 83 runTestOnBindingTarget(image, gl.TEXTURE_3D, program); 84 program = tiu.setupTexturedQuadWith2DArray(gl, internalFormat); 85 runTestOnBindingTarget(image, gl.TEXTURE_2D_ARRAY, program); 86 87 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors"); 88 finishTest(); 89 } 90 91 function runTestOnBindingTarget(image, bindingTarget, program) { 92 var cases = [ 93 { flipY: true, topColor: redColor, bottomColor: greenColor }, 94 { flipY: false, topColor: greenColor, bottomColor: redColor }, 95 ]; 96 for (var i in cases) { 97 runOneIteration(image, cases[i].flipY, 98 cases[i].topColor, cases[i].bottomColor, 99 bindingTarget, program); 100 } 101 } 102 103 return init; 104 }