| OLD | NEW |
| (Empty) |
| 1 <!-- | |
| 2 Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 3 Use of this source code is governed by a BSD-style license that can be | |
| 4 found in the LICENSE file. | |
| 5 --> | |
| 6 <!DOCTYPE html> | |
| 7 <html> | |
| 8 <head> | |
| 9 <meta charset="utf-8"> | |
| 10 <link rel="stylesheet" href="../resources/js-test-style.css"/> | |
| 11 <script src="../resources/js-test-pre.js"></script> | |
| 12 <script src="resources/webgl-test.js"></script> | |
| 13 <script src="resources/webgl-test-utils.js"></script> | |
| 14 </head> | |
| 15 <body> | |
| 16 <canvas id="example" width="1px" height="2px"></canvas> | |
| 17 <div id="description"></div> | |
| 18 <div id="console"></div> | |
| 19 <script> | |
| 20 description('Verifies texImage2D and texSubImage2D code paths taking ArrayBuffer
View'); | |
| 21 | |
| 22 var wtu = WebGLTestUtils; | |
| 23 var gl = null; | |
| 24 var textureLoc = null; | |
| 25 var successfullyParsed = false; | |
| 26 | |
| 27 // These two declarations need to be global for "shouldBe" to see them | |
| 28 var buf = null; | |
| 29 var idx = 0; | |
| 30 var pixel = [0, 0, 0, 1]; | |
| 31 var correctColor = null; | |
| 32 | |
| 33 function generateRGBAData(type, unpackAlignment) | |
| 34 { | |
| 35 var sourceData = [ 255, 0, 0, 255, | |
| 36 0, 255, 0, 0 ]; | |
| 37 switch (type) { | |
| 38 case gl.UNSIGNED_BYTE: { | |
| 39 var rowWidth = Math.max(unpackAlignment, 4); | |
| 40 var data = new Uint8Array(2 * rowWidth); | |
| 41 for (var y = 0; y < 2; ++y) { | |
| 42 var index = y * rowWidth; | |
| 43 for (var element = 0; element < 4; ++element) { | |
| 44 data[index + element] = sourceData[4 * y + element]; | |
| 45 } | |
| 46 } | |
| 47 return data; | |
| 48 } | |
| 49 case gl.UNSIGNED_SHORT_4_4_4_4: { | |
| 50 var rowWidth = Math.max(unpackAlignment, 2) / 2; | |
| 51 var data = new Uint16Array(2 * rowWidth); | |
| 52 for (var y = 0; y < 2; ++y) { | |
| 53 var index = y * rowWidth; | |
| 54 data[index] = (((sourceData[4 * y] & 0xF0) << 8) | |
| 55 | ((sourceData[4 * y + 1] & 0xF0) << 4) | |
| 56 | (sourceData[4 * y + 2] & 0xF0) | |
| 57 | (sourceData[4 * y + 3] >> 4)); | |
| 58 } | |
| 59 return data; | |
| 60 } | |
| 61 case gl.UNSIGNED_SHORT_5_5_5_1: { | |
| 62 var rowWidth = Math.max(unpackAlignment, 2) / 2; | |
| 63 var data = new Uint16Array(2 * rowWidth); | |
| 64 for (var y = 0; y < 2; ++y) { | |
| 65 var index = y * rowWidth; | |
| 66 data[index] = (((sourceData[4 * y] & 0xF8) << 8) | |
| 67 | ((sourceData[4 * y + 1] & 0xF8) << 3) | |
| 68 | ((sourceData[4 * y + 2] & 0xF8) >> 2) | |
| 69 | (sourceData[4 * y + 3] >> 7)); | |
| 70 } | |
| 71 return data; | |
| 72 } | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 function typeToString(type) | |
| 77 { | |
| 78 switch (type) { | |
| 79 case gl.UNSIGNED_BYTE: return 'UNSIGNED_BYTE'; | |
| 80 case gl.UNSIGNED_SHORT_5_5_5_1: return 'UNSIGNED_SHORT_5_5_5_1'; | |
| 81 case gl.UNSIGNED_SHORT_4_4_4_4: return 'UNSIGNED_SHORT_4_4_4_4'; | |
| 82 } | |
| 83 return 'Unknown type ' + type; | |
| 84 } | |
| 85 | |
| 86 function runOneIteration(useTexSubImage2D, type, unpackAlignment, flipY, premult
iplyAlpha, topColor, bottomColor) | |
| 87 { | |
| 88 debug('Testing ' + (useTexSubImage2D ? 'texSubImage2D' : 'texImage2D') + | |
| 89 ' with type=' + typeToString(type) + | |
| 90 ', unpackAlignment=' + unpackAlignment + | |
| 91 ', flipY=' + flipY + ', premultiplyAlpha=' + premultiplyAlpha); | |
| 92 gl.colorMask(true, true, true, true); | |
| 93 gl.clearColor(0, 0, 0, 1.0); | |
| 94 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); | |
| 95 // Enable writes to the RGB channels | |
| 96 gl.colorMask(true, true, true, false); | |
| 97 var texture = gl.createTexture(); | |
| 98 // Bind the texture to texture unit 0 | |
| 99 gl.bindTexture(gl.TEXTURE_2D, texture); | |
| 100 // Set up texture parameters | |
| 101 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); | |
| 102 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); | |
| 103 // Set up pixel store parameters | |
| 104 gl.pixelStorei(gl.UNPACK_ALIGNMENT, unpackAlignment); | |
| 105 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY); | |
| 106 gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, premultiplyAlpha); | |
| 107 // Generate the data | |
| 108 var data = generateRGBAData(type, unpackAlignment); | |
| 109 if (gl.getError() != gl.NO_ERROR) | |
| 110 testFailed("GL error before texture upload"); | |
| 111 // Upload the image into the texture | |
| 112 if (useTexSubImage2D) { | |
| 113 // Initialize the texture to black first | |
| 114 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 2, 0, | |
| 115 gl.RGBA, type, null); | |
| 116 if (gl.getError() != gl.NO_ERROR) | |
| 117 testFailed("GL error after texImage2D(null)"); | |
| 118 gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 1, 2, gl.RGBA, type, data); | |
| 119 if (gl.getError() != gl.NO_ERROR) | |
| 120 testFailed("GL error after texSubImage2D"); | |
| 121 } else { | |
| 122 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 2, 0, gl.RGBA, type, data); | |
| 123 if (gl.getError() != gl.NO_ERROR) | |
| 124 testFailed("GL error after texImage2D"); | |
| 125 } | |
| 126 | |
| 127 // Point the uniform sampler to texture unit 0 | |
| 128 gl.uniform1i(textureLoc, 0); | |
| 129 // Draw the triangles | |
| 130 wtu.drawQuad(gl, [0, 0, 0, 255]); | |
| 131 | |
| 132 // Read back the rendering results | |
| 133 buf = new Uint8Array(1 * 2 * 4); | |
| 134 gl.readPixels(0, 0, 1, 2, gl.RGBA, gl.UNSIGNED_BYTE, buf); | |
| 135 // Check the top pixel and bottom pixel and make sure they have | |
| 136 // the right color. | |
| 137 debug("Checking bottom pixel"); | |
| 138 wtu.checkCanvasRect(gl, 0, 0, 1, 1, bottomColor, "shouldBe " + bottomColor); | |
| 139 debug("Checking top pixel"); | |
| 140 wtu.checkCanvasRect(gl, 0, 1, 1, 1, topColor, "shouldBe " + topColor); | |
| 141 } | |
| 142 | |
| 143 function runTest() | |
| 144 { | |
| 145 var red = [255, 0, 0, 255]; | |
| 146 var green = [0, 255, 0, 255]; | |
| 147 var redPremultiplyAlpha = [255, 0, 0, 255]; | |
| 148 var greenPremultiplyAlpha = [0, 0, 0, 255]; | |
| 149 | |
| 150 var types = [ gl.UNSIGNED_BYTE, gl.UNSIGNED_SHORT_5_5_5_1, gl.UNSIGNED_SHORT
_4_4_4_4 ]; | |
| 151 var unpackAlignments = [ 1, 2, 4, 8 ]; | |
| 152 | |
| 153 for (var i = 0; i < types.length; ++i) { | |
| 154 var type = types[i]; | |
| 155 for (var j = 0; j < unpackAlignments.length; ++j) { | |
| 156 var unpackAlignment = unpackAlignments[j]; | |
| 157 runOneIteration(false, type, unpackAlignment, true, false, | |
| 158 red, green); | |
| 159 runOneIteration(false, type, unpackAlignment, false, false, | |
| 160 green, red); | |
| 161 runOneIteration(false, type, unpackAlignment, true, true, | |
| 162 redPremultiplyAlpha, greenPremultiplyAlpha); | |
| 163 runOneIteration(false, type, unpackAlignment, false, true, | |
| 164 greenPremultiplyAlpha, redPremultiplyAlpha); | |
| 165 runOneIteration(true, type, unpackAlignment, true, false, | |
| 166 red, green); | |
| 167 runOneIteration(true, type, unpackAlignment, false, false, | |
| 168 green, red); | |
| 169 runOneIteration(true, type, unpackAlignment, true, true, | |
| 170 redPremultiplyAlpha, greenPremultiplyAlpha); | |
| 171 runOneIteration(true, type, unpackAlignment, false, true, | |
| 172 greenPremultiplyAlpha, redPremultiplyAlpha); | |
| 173 } | |
| 174 } | |
| 175 | |
| 176 } | |
| 177 | |
| 178 var canvas = document.getElementById("example"); | |
| 179 gl = wtu.create3DContext(canvas); | |
| 180 var program = wtu.setupTexturedQuad(gl); | |
| 181 gl.disable(gl.BLEND); | |
| 182 | |
| 183 gl.clearColor(0,0,0,1); | |
| 184 gl.clearDepth(1); | |
| 185 | |
| 186 textureLoc = gl.getUniformLocation(program, "tex"); | |
| 187 | |
| 188 runTest(); | |
| 189 glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors"); | |
| 190 successfullyParsed = true; | |
| 191 </script> | |
| 192 <script src="../resources/js-test-post.js"></script> | |
| 193 </body> | |
| 194 </html> | |
| OLD | NEW |