OLD | NEW |
(Empty) | |
| 1 <!-- |
| 2 |
| 3 /* |
| 4 ** Copyright (c) 2012 The Khronos Group Inc. |
| 5 ** |
| 6 ** Permission is hereby granted, free of charge, to any person obtaining a |
| 7 ** copy of this software and/or associated documentation files (the |
| 8 ** "Materials"), to deal in the Materials without restriction, including |
| 9 ** without limitation the rights to use, copy, modify, merge, publish, |
| 10 ** distribute, sublicense, and/or sell copies of the Materials, and to |
| 11 ** permit persons to whom the Materials are furnished to do so, subject to |
| 12 ** the following conditions: |
| 13 ** |
| 14 ** The above copyright notice and this permission notice shall be included |
| 15 ** in all copies or substantial portions of the Materials. |
| 16 ** |
| 17 ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 20 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 21 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 22 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 23 ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. |
| 24 */ |
| 25 |
| 26 --> |
| 27 |
| 28 <!DOCTYPE html> |
| 29 <html> |
| 30 <head> |
| 31 <meta charset="utf-8"> |
| 32 <link rel="stylesheet" href="../../resources/js-test-style.css"/> |
| 33 <script src="../../resources/js-test-pre.js"></script> |
| 34 <script src="../resources/webgl-test.js"></script> |
| 35 <script src="../resources/webgl-test-utils.js"></script> |
| 36 </head> |
| 37 <body> |
| 38 <canvas id="example" width="1px" height="2px"></canvas> |
| 39 <div id="description"></div> |
| 40 <div id="console"></div> |
| 41 <script> |
| 42 description('Verifies texImage2D and texSubImage2D code paths taking ArrayBuffer
View'); |
| 43 |
| 44 var wtu = WebGLTestUtils; |
| 45 var gl = null; |
| 46 var textureLoc = null; |
| 47 var successfullyParsed = false; |
| 48 |
| 49 function generateRGBAData(type, unpackAlignment) |
| 50 { |
| 51 var sourceData = [ 255, 0, 0, 255, |
| 52 0, 255, 0, 0 ]; |
| 53 switch (type) { |
| 54 case gl.UNSIGNED_BYTE: { |
| 55 var rowWidth = Math.max(unpackAlignment, 4); |
| 56 var data = new Uint8Array(2 * rowWidth); |
| 57 for (var y = 0; y < 2; ++y) { |
| 58 var index = y * rowWidth; |
| 59 for (var element = 0; element < 4; ++element) { |
| 60 data[index + element] = sourceData[4 * y + element]; |
| 61 } |
| 62 } |
| 63 return data; |
| 64 } |
| 65 case gl.UNSIGNED_SHORT_4_4_4_4: { |
| 66 var rowWidth = Math.max(unpackAlignment, 2) / 2; |
| 67 var data = new Uint16Array(2 * rowWidth); |
| 68 for (var y = 0; y < 2; ++y) { |
| 69 var index = y * rowWidth; |
| 70 data[index] = (((sourceData[4 * y] & 0xF0) << 8) |
| 71 | ((sourceData[4 * y + 1] & 0xF0) << 4) |
| 72 | (sourceData[4 * y + 2] & 0xF0) |
| 73 | (sourceData[4 * y + 3] >> 4)); |
| 74 } |
| 75 return data; |
| 76 } |
| 77 case gl.UNSIGNED_SHORT_5_5_5_1: { |
| 78 var rowWidth = Math.max(unpackAlignment, 2) / 2; |
| 79 var data = new Uint16Array(2 * rowWidth); |
| 80 for (var y = 0; y < 2; ++y) { |
| 81 var index = y * rowWidth; |
| 82 data[index] = (((sourceData[4 * y] & 0xF8) << 8) |
| 83 | ((sourceData[4 * y + 1] & 0xF8) << 3) |
| 84 | ((sourceData[4 * y + 2] & 0xF8) >> 2) |
| 85 | (sourceData[4 * y + 3] >> 7)); |
| 86 } |
| 87 return data; |
| 88 } |
| 89 } |
| 90 } |
| 91 |
| 92 function typeToString(type) |
| 93 { |
| 94 switch (type) { |
| 95 case gl.UNSIGNED_BYTE: return 'UNSIGNED_BYTE'; |
| 96 case gl.UNSIGNED_SHORT_5_5_5_1: return 'UNSIGNED_SHORT_5_5_5_1'; |
| 97 case gl.UNSIGNED_SHORT_4_4_4_4: return 'UNSIGNED_SHORT_4_4_4_4'; |
| 98 } |
| 99 return 'Unknown type ' + type; |
| 100 } |
| 101 |
| 102 function runOneIteration(useTexSubImage2D, type, unpackAlignment, flipY, premult
iplyAlpha, topColor, bottomColor) |
| 103 { |
| 104 debug('Testing ' + (useTexSubImage2D ? 'texSubImage2D' : 'texImage2D') + |
| 105 ' with type=' + typeToString(type) + |
| 106 ', unpackAlignment=' + unpackAlignment + |
| 107 ', flipY=' + flipY + ', premultiplyAlpha=' + premultiplyAlpha); |
| 108 gl.colorMask(true, true, true, true); |
| 109 gl.clearColor(0, 0, 0, 1.0); |
| 110 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
| 111 // Enable writes to the RGB channels |
| 112 gl.colorMask(true, true, true, false); |
| 113 var texture = gl.createTexture(); |
| 114 // Bind the texture to texture unit 0 |
| 115 gl.bindTexture(gl.TEXTURE_2D, texture); |
| 116 // Set up texture parameters |
| 117 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); |
| 118 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); |
| 119 // Set up pixel store parameters |
| 120 gl.pixelStorei(gl.UNPACK_ALIGNMENT, unpackAlignment); |
| 121 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY); |
| 122 gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, premultiplyAlpha); |
| 123 // Generate the data |
| 124 var data = generateRGBAData(type, unpackAlignment); |
| 125 if (gl.getError() != gl.NO_ERROR) |
| 126 testFailed("GL error before texture upload"); |
| 127 // Upload the image into the texture |
| 128 if (useTexSubImage2D) { |
| 129 // Initialize the texture to black first |
| 130 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 2, 0, |
| 131 gl.RGBA, type, null); |
| 132 if (gl.getError() != gl.NO_ERROR) |
| 133 testFailed("GL error after texImage2D(null)"); |
| 134 gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 1, 2, gl.RGBA, type, data); |
| 135 if (gl.getError() != gl.NO_ERROR) |
| 136 testFailed("GL error after texSubImage2D"); |
| 137 } else { |
| 138 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 2, 0, gl.RGBA, type, data); |
| 139 if (gl.getError() != gl.NO_ERROR) |
| 140 testFailed("GL error after texImage2D"); |
| 141 } |
| 142 |
| 143 // Point the uniform sampler to texture unit 0 |
| 144 gl.uniform1i(textureLoc, 0); |
| 145 // Draw the triangles |
| 146 wtu.drawQuad(gl, [0, 0, 0, 255]); |
| 147 |
| 148 // Check the top pixel and bottom pixel and make sure they have |
| 149 // the right color. |
| 150 debug("Checking bottom pixel"); |
| 151 wtu.checkCanvasRect(gl, 0, 0, 1, 1, bottomColor, "shouldBe " + bottomColor); |
| 152 debug("Checking top pixel"); |
| 153 wtu.checkCanvasRect(gl, 0, 1, 1, 1, topColor, "shouldBe " + topColor); |
| 154 } |
| 155 |
| 156 function runTest() |
| 157 { |
| 158 var red = [255, 0, 0, 255]; |
| 159 var green = [0, 255, 0, 255]; |
| 160 var redPremultiplyAlpha = [255, 0, 0, 255]; |
| 161 var greenPremultiplyAlpha = [0, 0, 0, 255]; |
| 162 |
| 163 var types = [ gl.UNSIGNED_BYTE, gl.UNSIGNED_SHORT_5_5_5_1, gl.UNSIGNED_SHORT
_4_4_4_4 ]; |
| 164 var unpackAlignments = [ 1, 2, 4, 8 ]; |
| 165 |
| 166 for (var i = 0; i < types.length; ++i) { |
| 167 var type = types[i]; |
| 168 for (var j = 0; j < unpackAlignments.length; ++j) { |
| 169 var unpackAlignment = unpackAlignments[j]; |
| 170 runOneIteration(false, type, unpackAlignment, true, false, |
| 171 red, green); |
| 172 runOneIteration(false, type, unpackAlignment, false, false, |
| 173 green, red); |
| 174 runOneIteration(false, type, unpackAlignment, true, true, |
| 175 redPremultiplyAlpha, greenPremultiplyAlpha); |
| 176 runOneIteration(false, type, unpackAlignment, false, true, |
| 177 greenPremultiplyAlpha, redPremultiplyAlpha); |
| 178 runOneIteration(true, type, unpackAlignment, true, false, |
| 179 red, green); |
| 180 runOneIteration(true, type, unpackAlignment, false, false, |
| 181 green, red); |
| 182 runOneIteration(true, type, unpackAlignment, true, true, |
| 183 redPremultiplyAlpha, greenPremultiplyAlpha); |
| 184 runOneIteration(true, type, unpackAlignment, false, true, |
| 185 greenPremultiplyAlpha, redPremultiplyAlpha); |
| 186 } |
| 187 } |
| 188 |
| 189 } |
| 190 |
| 191 gl = wtu.create3DContext("example"); |
| 192 var program = wtu.setupTexturedQuad(gl); |
| 193 gl.disable(gl.BLEND); |
| 194 |
| 195 gl.clearColor(0,0,0,1); |
| 196 gl.clearDepth(1); |
| 197 |
| 198 textureLoc = gl.getUniformLocation(program, "tex"); |
| 199 |
| 200 runTest(); |
| 201 glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors"); |
| 202 successfullyParsed = true; |
| 203 </script> |
| 204 <script src="../../resources/js-test-post.js"></script> |
| 205 </body> |
| 206 </html> |
OLD | NEW |