2 Copyright (c) 2019 The Khronos Group Inc.
3 Use of this source code is governed by an MIT-style license that can be
4 found in the LICENSE.txt file.
10 <meta charset=
"utf-8">
11 <title>WebGL ActiveTexture BindTexture conformance test #
2</title>
12 <link rel=
"stylesheet" href=
"../../../resources/js-test-style.css"/>
13 <script src=
"../../../js/js-test-pre.js"></script>
14 <script src=
"../../../js/webgl-test-utils.js"> </script>
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">
23 attribute vec3 vPosition;
24 attribute vec2 texCoord0;
25 varying vec2 texCoord;
28 gl_Position = world * vec4(vPosition,
1);
32 <script id=
"fshader2d" type=
"x-shader/x-fragment">
33 precision mediump float;
35 uniform sampler2D tex2d;
36 varying vec2 texCoord;
39 gl_FragColor = texture2D(tex2d, texCoord);
42 <script id=
"fshaderCube" type=
"x-shader/x-fragment">
43 precision mediump float;
45 uniform samplerCube texCube;
48 gl_FragColor = textureCube(texCube, vec3(
0,
1,
0));
57 "Tests that binding both TEXTURE_2D and TEXTURE_CUBE_MAP to the same " +
58 "active texture unit works as long as they are not used " +
59 "simultaneously in the same shader program.");
61 var canvas2d
= document
.getElementById("canvas2d");
62 var ctx2d
= canvas2d
.getContext("2d");
63 ctx2d
.globalCompositeOperation
= "copy";
65 var wtu
= WebGLTestUtils
;
66 var gl
= wtu
.create3DContext("example");
67 var program
= wtu
.setupProgram(
68 gl
, ["vshader", "fshader2d"], ["vPosition", "texCoord0"]);
70 var program2d
= program
;
71 var programCube
= wtu
.setupProgram(
72 gl
, ["vshader", "fshaderCube"], ["vPosition", "texCoord0"]);
74 var vertexObject
= gl
.createBuffer();
75 gl
.bindBuffer(gl
.ARRAY_BUFFER
, vertexObject
);
78 new Float32Array([-1, 1,0, 1,1,0, -1,-1,0,
79 -1,-1,0, 1,1,0, 1,-1,0]),
81 gl
.enableVertexAttribArray(0);
82 gl
.vertexAttribPointer(0, 3, gl
.FLOAT
, false, 0, 0);
84 var vertexObject
= gl
.createBuffer();
85 gl
.bindBuffer(gl
.ARRAY_BUFFER
, vertexObject
);
88 new Float32Array([ 0,0, 1,0, 0,1,
91 gl
.enableVertexAttribArray(1);
92 gl
.vertexAttribPointer(1, 2, gl
.FLOAT
, false, 0, 0);
94 // Make texture unit 1 active.
95 gl
.activeTexture(gl
.TEXTURE1
);
98 var tex2d
= gl
.createTexture();
99 gl
.bindTexture(gl
.TEXTURE_2D
, tex2d
);
100 ctx2d
.fillStyle
= "rgba(0, 0, 255, 255)";
101 ctx2d
.fillRect(0, 0, 1, 1);
102 gl
.texImage2D(gl
.TEXTURE_2D
, 0, gl
.RGBA
, gl
.RGBA
, gl
.UNSIGNED_BYTE
, canvas2d
);
104 // make a cube texture
105 var texCube
= gl
.createTexture();
106 gl
.bindTexture(gl
.TEXTURE_CUBE_MAP
, texCube
);
107 ctx2d
.fillStyle
= "rgba(255, 0, 255, 255)";
108 ctx2d
.fillRect(0, 0, 1, 1);
110 gl
.TEXTURE_CUBE_MAP_POSITIVE_X
,
111 gl
.TEXTURE_CUBE_MAP_NEGATIVE_X
,
112 gl
.TEXTURE_CUBE_MAP_POSITIVE_Y
,
113 gl
.TEXTURE_CUBE_MAP_NEGATIVE_Y
,
114 gl
.TEXTURE_CUBE_MAP_POSITIVE_Z
,
115 gl
.TEXTURE_CUBE_MAP_NEGATIVE_Z
];
116 for (var ii
= 0; ii
< targets
.length
; ++ii
) {
117 gl
.texImage2D(targets
[ii
], 0, gl
.RGBA
, gl
.RGBA
, gl
.UNSIGNED_BYTE
, canvas2d
);
120 // Setup program2d and programCube
121 var tex2dLoc
= gl
.getUniformLocation(program2d
, "tex2d");
122 var world2dLoc
= gl
.getUniformLocation(program2d
, "world");
123 var texCubeLoc
= gl
.getUniformLocation(programCube
, "texCube");
124 var worldCubeLoc
= gl
.getUniformLocation(programCube
, "world");
126 gl
.useProgram(program2d
);
127 gl
.uniform1i(tex2dLoc
, 1);
128 gl
.useProgram(programCube
);
129 gl
.uniform1i(texCubeLoc
, 1);
131 gl
.clearColor(1,0,0,1);
132 gl
.clear(gl
.COLOR_BUFFER_BIT
| gl
.DEPTH_BUFFER_BIT
);
134 var programs
= [program2d
, programCube
];
135 var worldLocs
= [world2dLoc
, worldCubeLoc
];
136 for (var ii
= 0; ii
< 4; ++ii
) {
138 var y
= Math
.floor(ii
/ 2);
139 gl
.useProgram(programs
[x
]);
145 -0.5 + x
, -0.5 + y
, 0, 1]);
146 gl
.drawArrays(gl
.TRIANGLES
, 0, 6);
155 for (var ii
= 0; ii
< colors
.length
; ++ii
) {
158 var y
= Math
.floor(ii
/ 2);
159 wtu
.checkCanvasRect(gl
, x
, y
, 1, 1, c
);
162 // Test that binding to one target does not affect the other
164 debug("Testing texture target binding/unbinding");
172 gl
.activeTexture(gl
.TEXTURE2
);
174 // Unbinding the TEXTURE_CUBE_MAP target should not affect the TEXTURE_2D target
175 gl
.clear(gl
.COLOR_BUFFER_BIT
| gl
.DEPTH_BUFFER_BIT
);
177 gl
.bindTexture(gl
.TEXTURE_2D
, tex2d
);
178 gl
.bindTexture(gl
.TEXTURE_CUBE_MAP
, texCube
);
179 gl
.bindTexture(gl
.TEXTURE_CUBE_MAP
, null);
181 gl
.useProgram(program2d
);
182 gl
.uniform1i(tex2dLoc
, 2);
183 gl
.uniformMatrix4fv(world2dLoc
, false, worldMatrix
);
184 gl
.drawArrays(gl
.TRIANGLES
, 0, 6);
186 wtu
.checkCanvasRect(gl
, 0, 0, 2, 2, [0,0,255,255]);
188 // Unbinding the TEXTURE_2D target should not affect the TEXTURE_CUBE_MAP target
189 gl
.clear(gl
.COLOR_BUFFER_BIT
| gl
.DEPTH_BUFFER_BIT
);
191 gl
.bindTexture(gl
.TEXTURE_CUBE_MAP
, texCube
);
192 gl
.bindTexture(gl
.TEXTURE_2D
, tex2d
);
193 gl
.bindTexture(gl
.TEXTURE_2D
, null);
195 gl
.useProgram(programCube
);
196 gl
.uniform1i(texCubeLoc
, 2);
197 gl
.uniformMatrix4fv(worldCubeLoc
, false, worldMatrix
);
198 gl
.drawArrays(gl
.TRIANGLES
, 0, 6);
200 wtu
.checkCanvasRect(gl
, 0, 0, 2, 2, [255,0,255,255]);
204 var successfullyParsed
= true;
206 <script src=
"../../../js/js-test-post.js"></script>