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