|
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 <script> |
|
15 var wtu = WebGLTestUtils; |
|
16 var gl = null; |
|
17 var textureLoc = null; |
|
18 var successfullyParsed = false; |
|
19 var imageData = null; |
|
20 |
|
21 function init() |
|
22 { |
|
23 if (window.initNonKhronosFramework) { |
|
24 window.initNonKhronosFramework(true); |
|
25 } |
|
26 |
|
27 description('Verify texImage2D and texSubImage2D code paths taking ImageData'); |
|
28 |
|
29 var canvas2d = document.getElementById("texcanvas"); |
|
30 var context2d = canvas2d.getContext("2d"); |
|
31 imageData = context2d.createImageData(1, 2); |
|
32 var data = imageData.data; |
|
33 data[0] = 255; |
|
34 data[1] = 0; |
|
35 data[2] = 0; |
|
36 data[3] = 255; |
|
37 data[4] = 0; |
|
38 data[5] = 255; |
|
39 data[6] = 0; |
|
40 data[7] = 0; |
|
41 |
|
42 wtu = WebGLTestUtils; |
|
43 var canvas = document.getElementById("example"); |
|
44 gl = wtu.create3DContext(canvas); |
|
45 var program = wtu.setupTexturedQuad(gl); |
|
46 gl.clearColor(0,0,0,1); |
|
47 gl.clearDepth(1); |
|
48 gl.disable(gl.BLEND); |
|
49 |
|
50 textureLoc = gl.getUniformLocation(program, "tex"); |
|
51 |
|
52 runTest(); |
|
53 } |
|
54 |
|
55 // These two declarations need to be global for "shouldBe" to see them |
|
56 var buf = null; |
|
57 var idx = 0; |
|
58 var pixel = [0, 0, 0, 1]; |
|
59 var correctColor = null; |
|
60 |
|
61 function runOneIteration(useTexSubImage2D, flipY, premultiplyAlpha, topColor, bottomColor) |
|
62 { |
|
63 debug('Testing ' + (useTexSubImage2D ? 'texSubImage2D' : 'texImage2D') + |
|
64 ' with flipY=' + flipY + ' and premultiplyAlpha=' + premultiplyAlpha); |
|
65 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
|
66 // Enable writes to the RGBA channels |
|
67 gl.colorMask(1, 1, 1, 0); |
|
68 var texture = gl.createTexture(); |
|
69 // Bind the texture to texture unit 0 |
|
70 gl.bindTexture(gl.TEXTURE_2D, texture); |
|
71 // Set up texture parameters |
|
72 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); |
|
73 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); |
|
74 // Set up pixel store parameters |
|
75 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY); |
|
76 gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, premultiplyAlpha); |
|
77 // Upload the image into the texture |
|
78 if (useTexSubImage2D) { |
|
79 // Initialize the texture to black first |
|
80 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 2, 0, |
|
81 gl.RGBA, gl.UNSIGNED_BYTE, null); |
|
82 gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, imageData); |
|
83 } else { |
|
84 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, imageData); |
|
85 } |
|
86 |
|
87 // Point the uniform sampler to texture unit 0 |
|
88 gl.uniform1i(textureLoc, 0); |
|
89 // Draw the triangles |
|
90 wtu.drawQuad(gl, [0, 0, 0, 255]); |
|
91 |
|
92 // Read back the rendering results |
|
93 buf = new Uint8Array(1 * 2 * 4); |
|
94 gl.readPixels(0, 0, 1, 2, gl.RGBA, gl.UNSIGNED_BYTE, buf); |
|
95 // Check the top pixel and bottom pixel and make sure they have |
|
96 // the right color. |
|
97 debug("Checking bottom pixel"); |
|
98 wtu.checkCanvasRect(gl, 0, 0, 1, 1, bottomColor, "shouldBe " + bottomColor); |
|
99 debug("Checking top pixel"); |
|
100 wtu.checkCanvasRect(gl, 0, 1, 1, 1, topColor, "shouldBe " + topColor); |
|
101 } |
|
102 |
|
103 function runTest() |
|
104 { |
|
105 var red = [255, 0, 0, 255]; |
|
106 var green = [0, 255, 0, 255]; |
|
107 var redPremultiplyAlpha = [255, 0, 0, 255]; |
|
108 var greenPremultiplyAlpha = [0, 0, 0, 255]; |
|
109 |
|
110 runOneIteration(false, true, false, |
|
111 red, green); |
|
112 runOneIteration(false, false, false, |
|
113 green, red); |
|
114 runOneIteration(false, true, true, |
|
115 redPremultiplyAlpha, greenPremultiplyAlpha); |
|
116 runOneIteration(false, false, true, |
|
117 greenPremultiplyAlpha, redPremultiplyAlpha); |
|
118 runOneIteration(true, true, false, |
|
119 red, green); |
|
120 runOneIteration(true, false, false, |
|
121 green, red); |
|
122 runOneIteration(true, true, true, |
|
123 redPremultiplyAlpha, greenPremultiplyAlpha); |
|
124 runOneIteration(true, false, true, |
|
125 greenPremultiplyAlpha, redPremultiplyAlpha); |
|
126 |
|
127 glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors"); |
|
128 finishTest(); |
|
129 } |
|
130 </script> |
|
131 </head> |
|
132 <body onload="init()"> |
|
133 <canvas id="texcanvas" width="1px" height="2px"></canvas> |
|
134 <canvas id="example" width="1px" height="2px"></canvas> |
|
135 <div id="description"></div> |
|
136 <div id="console"></div> |
|
137 </body> |
|
138 </html> |