OLD | NEW |
(Empty) | |
| 1 /* |
| 2 ** Copyright (c) 2012 The Khronos Group Inc. |
| 3 ** |
| 4 ** Permission is hereby granted, free of charge, to any person obtaining a |
| 5 ** copy of this software and/or associated documentation files (the |
| 6 ** "Materials"), to deal in the Materials without restriction, including |
| 7 ** without limitation the rights to use, copy, modify, merge, publish, |
| 8 ** distribute, sublicense, and/or sell copies of the Materials, and to |
| 9 ** permit persons to whom the Materials are furnished to do so, subject to |
| 10 ** the following conditions: |
| 11 ** |
| 12 ** The above copyright notice and this permission notice shall be included |
| 13 ** in all copies or substantial portions of the Materials. |
| 14 ** |
| 15 ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. |
| 22 */ |
| 23 |
| 24 function generateTest(pixelFormat, pixelType, pathToTestRoot, prologue) { |
| 25 var wtu = WebGLTestUtils; |
| 26 var gl = null; |
| 27 var textureLoc = null; |
| 28 var successfullyParsed = false; |
| 29 var imgCanvas; |
| 30 var red = [255, 0, 0]; |
| 31 var green = [0, 255, 0]; |
| 32 |
| 33 var init = function() |
| 34 { |
| 35 if (window.initNonKhronosFramework) { |
| 36 window.initNonKhronosFramework(true); |
| 37 } |
| 38 |
| 39 description('Verify texImage2D and texSubImage2D code paths taking image
elements (' + pixelFormat + '/' + pixelType + ')'); |
| 40 |
| 41 gl = wtu.create3DContext("example"); |
| 42 |
| 43 if (!prologue(gl)) { |
| 44 finishTest(); |
| 45 return; |
| 46 } |
| 47 |
| 48 var program = wtu.setupTexturedQuad(gl); |
| 49 |
| 50 gl.clearColor(0,0,0,1); |
| 51 gl.clearDepth(1); |
| 52 |
| 53 textureLoc = gl.getUniformLocation(program, "tex"); |
| 54 |
| 55 wtu.loadTexture(gl, pathToTestRoot + "/resources/red-green.png", runTest
); |
| 56 } |
| 57 |
| 58 function runOneIteration(image, useTexSubImage2D, flipY, topColor, bottomCol
or) |
| 59 { |
| 60 debug('Testing ' + (useTexSubImage2D ? 'texSubImage2D' : 'texImage2D') + |
| 61 ' with flipY=' + flipY); |
| 62 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
| 63 // Disable any writes to the alpha channel |
| 64 gl.colorMask(1, 1, 1, 0); |
| 65 var texture = gl.createTexture(); |
| 66 // Bind the texture to texture unit 0 |
| 67 gl.bindTexture(gl.TEXTURE_2D, texture); |
| 68 // Set up texture parameters |
| 69 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); |
| 70 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); |
| 71 // Set up pixel store parameters |
| 72 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY); |
| 73 gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false); |
| 74 gl.pixelStorei(gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, gl.NONE); |
| 75 // Upload the image into the texture |
| 76 if (useTexSubImage2D) { |
| 77 // Initialize the texture to black first |
| 78 gl.texImage2D(gl.TEXTURE_2D, 0, gl[pixelFormat], image.width, image.
height, 0, |
| 79 gl[pixelFormat], gl[pixelType], null); |
| 80 gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl[pixelFormat], gl[pixelTy
pe], image); |
| 81 } else { |
| 82 gl.texImage2D(gl.TEXTURE_2D, 0, gl[pixelFormat], gl[pixelFormat], gl
[pixelType], image); |
| 83 } |
| 84 |
| 85 // Point the uniform sampler to texture unit 0 |
| 86 gl.uniform1i(textureLoc, 0); |
| 87 // Draw the triangles |
| 88 wtu.drawQuad(gl, [0, 0, 0, 255]); |
| 89 // Check a few pixels near the top and bottom and make sure they have |
| 90 // the right color. |
| 91 debug("Checking lower left corner"); |
| 92 wtu.checkCanvasRect(gl, 4, 4, 2, 2, bottomColor, |
| 93 "shouldBe " + bottomColor); |
| 94 debug("Checking upper left corner"); |
| 95 wtu.checkCanvasRect(gl, 4, gl.canvas.height - 8, 2, 2, topColor, |
| 96 "shouldBe " + topColor); |
| 97 } |
| 98 |
| 99 function runTestOnImage(image) { |
| 100 runOneIteration(image, false, true, red, green); |
| 101 runOneIteration(image, false, false, green, red); |
| 102 runOneIteration(image, true, true, red, green); |
| 103 runOneIteration(image, true, false, green, red); |
| 104 } |
| 105 |
| 106 function runTest(image) |
| 107 { |
| 108 runTestOnImage(image); |
| 109 |
| 110 imgCanvas = document.createElement("canvas"); |
| 111 imgCanvas.width = 1; |
| 112 imgCanvas.height = 2; |
| 113 var imgCtx = imgCanvas.getContext("2d"); |
| 114 var imgData = imgCtx.createImageData(1, 2); |
| 115 imgData.data[0] = red[0]; |
| 116 imgData.data[1] = red[1]; |
| 117 imgData.data[2] = red[2]; |
| 118 imgData.data[3] = 255; |
| 119 imgData.data[4] = green[0]; |
| 120 imgData.data[5] = green[1]; |
| 121 imgData.data[6] = green[2]; |
| 122 imgData.data[7] = 255; |
| 123 imgCtx.putImageData(imgData, 0, 0); |
| 124 |
| 125 // apparently Image is different than <img>. |
| 126 var newImage = new Image(); |
| 127 newImage.onload = function() { |
| 128 runTest2(newImage); |
| 129 }; |
| 130 newImage.src = imgCanvas.toDataURL(); |
| 131 } |
| 132 |
| 133 function runTest2(image) { |
| 134 runTestOnImage(image); |
| 135 |
| 136 var newImage = document.createElement("img"); |
| 137 newImage.onload = function() { |
| 138 runTest3(newImage); |
| 139 }; |
| 140 newImage.src = imgCanvas.toDataURL(); |
| 141 } |
| 142 |
| 143 function runTest3(image) { |
| 144 runTestOnImage(image); |
| 145 |
| 146 glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors"); |
| 147 finishTest(); |
| 148 } |
| 149 |
| 150 return init; |
| 151 } |
OLD | NEW |