|
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 <title>WebGL ActiveTexture BindTexture conformance test.</title> |
|
11 <link rel="stylesheet" href="../../resources/js-test-style.css"/> |
|
12 <script src="../../resources/js-test-pre.js"></script> |
|
13 <script src="../resources/webgl-test.js"> </script> |
|
14 <script src="../resources/webgl-test-utils.js"></script> |
|
15 </head> |
|
16 <body> |
|
17 <canvas id="example" width="2" height="2" style="width: 40px; height: 40px;"></canvas> |
|
18 <canvas id="canvas2d" width="1" height="1" style="width: 40px; height: 40px;"></canvas> |
|
19 <div id="description"></div> |
|
20 <div id="console"></div> |
|
21 <script id="vshader" type="x-shader/x-vertex"> |
|
22 uniform mat4 world; |
|
23 attribute vec3 vPosition; |
|
24 attribute vec2 texCoord0; |
|
25 varying vec2 texCoord; |
|
26 void main() |
|
27 { |
|
28 gl_Position = world * vec4(vPosition, 1); |
|
29 texCoord = texCoord0; |
|
30 } |
|
31 </script> |
|
32 <script> |
|
33 var gl; |
|
34 |
|
35 function init() |
|
36 { |
|
37 if (window.initNonKhronosFramework) { |
|
38 window.initNonKhronosFramework(false); |
|
39 } |
|
40 |
|
41 description( |
|
42 "Tests that glActiveTexture and glBindTexture work as expected" + |
|
43 "Specifically texture targets are per active texture unit."); |
|
44 |
|
45 var canvas2d = document.getElementById("canvas2d"); |
|
46 var ctx2d = canvas2d.getContext("2d"); |
|
47 |
|
48 var wtu = WebGLTestUtils; |
|
49 gl = wtu.create3DContext("example"); |
|
50 var program = wtu.setupProgram( |
|
51 gl, |
|
52 ["vshader", wtu.setupSimpleTextureFragmentShader(gl)], |
|
53 ['vPosition', 'texCoord0']); |
|
54 wtu.setupUnitQuad(gl); |
|
55 gl.disable(gl.DEPTH_TEST); |
|
56 gl.disable(gl.BLEND); |
|
57 glErrorShouldBe(gl, gl.NO_ERROR); |
|
58 |
|
59 var colors = [ |
|
60 [0,192,128,255], |
|
61 [128,64,255,255], |
|
62 [192,255,64,255], |
|
63 [200,0,255,255]]; |
|
64 |
|
65 // Make 4 textures by using 4 active texture units if available. |
|
66 var texunits = Math.min(colors.length, gl.getParameter(gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS)) |
|
67 var textures = []; |
|
68 for (var ii = 0; ii < texunits; ++ii) { |
|
69 var tex = gl.createTexture(); |
|
70 gl.activeTexture(gl.TEXTURE0 + ii); |
|
71 gl.bindTexture(gl.TEXTURE_2D, tex); |
|
72 textures[ii] = tex; |
|
73 } |
|
74 glErrorShouldBe(gl, gl.NO_ERROR); |
|
75 |
|
76 // now use each texture unit to write into the textures, |
|
77 for (var ii = 0; ii < texunits; ++ii) { |
|
78 var c = colors[ii]; |
|
79 ctx2d.fillStyle = |
|
80 "rgba(" + c[0] + "," + c[1] + "," + c[2] + "," + c[3] + ")"; |
|
81 ctx2d.fillRect(0, 0, 1, 1); |
|
82 gl.activeTexture(gl.TEXTURE0 + ii); |
|
83 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, canvas2d); |
|
84 } |
|
85 glErrorShouldBe(gl, gl.NO_ERROR); |
|
86 |
|
87 var textureLoc = gl.getUniformLocation(program, "tex"); |
|
88 var worldLoc = gl.getUniformLocation(program, "world"); |
|
89 glErrorShouldBe(gl, gl.NO_ERROR); |
|
90 |
|
91 gl.clearColor(1,0,0,1); |
|
92 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
|
93 |
|
94 for (var ii = 0; ii < texunits; ++ii) { |
|
95 var x = ii % 2; |
|
96 var y = Math.floor(ii / 2); |
|
97 gl.uniform1i(textureLoc, ii); |
|
98 gl.uniformMatrix4fv( |
|
99 worldLoc, false, |
|
100 [0.5, 0, 0, 0, |
|
101 0, 0.5, 0, 0, |
|
102 0, 0, 1, 0, |
|
103 -0.5 + x, -0.5 + y, 0, 1]); |
|
104 gl.drawArrays(gl.TRIANGLES, 0, 6); |
|
105 } |
|
106 glErrorShouldBe(gl, gl.NO_ERROR); |
|
107 |
|
108 for (var ii = 0; ii < texunits; ++ii) { |
|
109 var c = colors[ii]; |
|
110 var x = ii % 2; |
|
111 var y = Math.floor(ii / 2); |
|
112 var buf = new Uint8Array(4); |
|
113 gl.readPixels(x, y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, buf); |
|
114 var msg = 'expected:' + |
|
115 c[0] + ', ' + c[1] + ', ' + c[2] + ', ' + c[3] + ' found: ' + |
|
116 buf[0] + ', ' + |
|
117 buf[1] + ', ' + |
|
118 buf[2] + ', ' + |
|
119 buf[3]; |
|
120 if (buf[0] != c[0] || |
|
121 buf[1] != c[1] || |
|
122 buf[2] != c[2] || |
|
123 buf[3] != c[3]) |
|
124 testFailed(msg); |
|
125 else |
|
126 testPassed(msg); |
|
127 } |
|
128 } |
|
129 |
|
130 init(); |
|
131 successfullyParsed = true; |
|
132 </script> |
|
133 |
|
134 <script>finishTest();</script> |
|
135 |
|
136 </body> |
|
137 </html> |
|
138 |