blob: d6401eb4246fb73f0d7bd22af6571c87c403f094 [file] [log] [blame]
bfulgham@apple.comf519b7f2016-03-23 19:21:11 +00001<!DOCTYPE html>
2<html>
3<head>
4<title>WebGL Non-Power of 2 texture conformance test.</title>
5<script src="../../../resources/js-test.js"></script>
6<script src="resources/webgl-test.js"> </script>
7<script src="resources/webgl-test-utils.js"> </script>
8</head>
9<body>
10<canvas id="example" width="4" height="4" style="width: 40px; height: 30px;"></canvas>
11<div id="description"></div>
12<div id="console"></div>
13<script id="vshader" type="x-shader/x-vertex">
14attribute vec4 vPosition;
15attribute vec2 texCoord0;
16varying vec2 texCoord;
17void main()
18{
19 gl_Position = vPosition;
20 texCoord = texCoord0;
21}
22</script>
23
24<script id="fshader" type="x-shader/x-fragment">
25#ifdef GL_ES
26precision mediump float;
27#endif
28uniform samplerCube tex;
29varying vec2 texCoord;
30void main()
31{
32 gl_FragColor = textureCube(tex, normalize(vec3(texCoord, 1)));
33}
34</script>
35<script>
36if (window.internals)
37 window.internals.settings.setWebGLErrorsToConsoleEnabled(false);
38
39var wtu = WebGLTestUtils;
40var canvas = document.getElementById("example");
41var gl = wtu.create3DContext(canvas);
42var program = wtu.setupTexturedQuad(gl);
43
44glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup.");
45
46// Make 3 textures by using 3 active texture units.
47var textures = [];
48for (var ii = 0; ii < 3; ++ii) {
49 var tex = gl.createTexture();
50 gl.activeTexture(gl.TEXTURE0 + ii);
51 gl.bindTexture(gl.TEXTURE_2D, tex);
52 textures[ii] = tex;
53}
54shouldBe("gl.getError()", "gl.NO_ERROR");
55
56// Alternate POT and NPOT textures, ending with NPOT.
57//
58// 1. Check that an NPOT texture not on level 0 generates INVALID_VALUE (alternating, ending in NPOT texture)
59gl.activeTexture(gl.TEXTURE0 + 0);
60wtu.fillTexture(gl, textures[0], 5, 3, [0, 192, 128, 255], 1);
61glErrorShouldBe(gl, gl.INVALID_VALUE, "gl.texImage2D[0] with NPOT texture with level > 0 should return INVALID_VALUE");
62
63gl.activeTexture(gl.TEXTURE0 + 1);
64wtu.fillTexture(gl, textures[1], 4, 4, [0, 192, 128, 255], 1);
65glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D[1] with POT texture with level > 0 should succeed");
66
67gl.activeTexture(gl.TEXTURE0 + 2);
68wtu.fillTexture(gl, textures[2], 5, 3, [0, 192, 128, 255], 1);
69glErrorShouldBe(gl, gl.INVALID_VALUE, "gl.texImage2D[2] with NPOT texture with level > 0 should return INVALID_VALUE");
70
71// 2. Check that an NPOT texture not on level 0 generates INVALID_VALUE (alternating, ending in POT texture)
72gl.activeTexture(gl.TEXTURE0 + 0);
73wtu.fillTexture(gl, textures[0], 4, 4, [0, 192, 128, 255], 1);
74glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D[0] with POT texture with level > 0 should succeed");
75
76gl.activeTexture(gl.TEXTURE0 + 1);
77wtu.fillTexture(gl, textures[1], 5, 3, [0, 192, 128, 255], 1);
78glErrorShouldBe(gl, gl.INVALID_VALUE, "gl.texImage2D[1] with POT texture with level > 0 should return INVALID_VALUE");
79
80gl.activeTexture(gl.TEXTURE0 + 2);
81wtu.fillTexture(gl, textures[2], 4, 4, [0, 192, 128, 255], 1);
82glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D[2] with POT texture with level > 0 should succeed");
83
84// 3. Check that an NPOT texture on level 0 succeeds (alternating, ending in NPOT texture)
85gl.activeTexture(gl.TEXTURE0 + 0);
86wtu.fillTexture(gl, textures[0], 5, 3, [0, 192, 128, 255]);
87glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D[0] with NPOT texture at level 0 should succeed");
88
89gl.activeTexture(gl.TEXTURE0 + 1);
90wtu.fillTexture(gl, textures[1], 4, 4, [0, 192, 128, 255]);
91glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D[1] with POT texture at level 0 should succeed");
92
93gl.activeTexture(gl.TEXTURE0 + 2);
94wtu.fillTexture(gl, textures[2], 5, 3, [0, 192, 128, 255]);
95glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D[2] with NPOT texture at level 0 should succeed");
96
97// 4. Check that an NPOT texture on level 0 succeeds (alternating, ending in POT texture)
98gl.activeTexture(gl.TEXTURE0 + 0);
99wtu.fillTexture(gl, textures[0], 4, 4, [0, 192, 128, 255]);
100glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D[0] with POT texture at level 0 should succeed");
101
102gl.activeTexture(gl.TEXTURE0 + 1);
103wtu.fillTexture(gl, textures[1], 5, 3, [0, 192, 128, 255]);
104glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D[1] with NPOT texture at level 0 should succeed");
105
106gl.activeTexture(gl.TEXTURE0 + 2);
107wtu.fillTexture(gl, textures[2], 4, 4, [0, 192, 128, 255]);
108glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D[2] with POT texture at level 0 should succeed");
109
110debug("");
111debug("check using cubemap");
112var program = wtu.setupProgram(
113 gl,
114 [wtu.loadShaderFromScript(gl, 'vshader', gl.VERTEX_SHADER),
115 wtu.loadShaderFromScript(gl, 'fshader', gl.FRAGMENT_SHADER)],
116 ['vPosition', 'texCoord0'], [0, 1]);
117
118// Make 3 textures by using 3 active texture units.
119var cubeTextures = [];
120for (var ii = 0; ii < 3; ++ii) {
121 var tex = gl.createTexture();
122 gl.activeTexture(gl.TEXTURE0 + ii);
123 gl.bindTexture(gl.TEXTURE_CUBE_MAP, tex);
124 cubeTextures[ii] = tex;
125}
126shouldBe("gl.getError()", "gl.NO_ERROR");
127
128// 5. Check that an NPOT texture not on level 0 generates INVALID_VALUE (alternating, ending in NPOT texture)
129gl.activeTexture(gl.TEXTURE0 + 0);
130fillCubeTexture(gl, cubeTextures[0], 5, 3, [0, 192, 128, 255], 1);
131glErrorShouldBe(gl, gl.INVALID_VALUE, "gl.texImage2D with NPOT texture with level > 0 should return INVALID_VALUE");
132
133gl.activeTexture(gl.TEXTURE0 + 1);
134fillCubeTexture(gl, cubeTextures[1], 4, 4, [0, 192, 128, 255], 1);
135glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D with POT texture with level > 0 should succeed");
136
137gl.activeTexture(gl.TEXTURE0 + 2);
138fillCubeTexture(gl, cubeTextures[2], 5, 3, [0, 192, 128, 255], 1);
139glErrorShouldBe(gl, gl.INVALID_VALUE, "gl.texImage2D with NPOT texture with level > 0 should return INVALID_VALUE");
140
141// 6. Check that an NPOT texture not on level 0 generates INVALID_VALUE (alternating, ending in POT texture)
142gl.activeTexture(gl.TEXTURE0 + 0);
143fillCubeTexture(gl, cubeTextures[0], 4, 4, [0, 192, 128, 255], 1);
144glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D with POT texture with level > 0 should succeed");
145
146gl.activeTexture(gl.TEXTURE0 + 1);
147fillCubeTexture(gl, cubeTextures[1], 5, 3, [0, 192, 128, 255], 1);
148glErrorShouldBe(gl, gl.INVALID_VALUE, "gl.texImage2D with NPOT texture with level > 0 should return INVALID_VALUE");
149
150gl.activeTexture(gl.TEXTURE0 + 2);
151fillCubeTexture(gl, cubeTextures[2], 4, 4, [0, 192, 128, 255], 1);
152glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D with POT texture with level > 0 should succeed");
153
154// 7. Check that an NPOT texture on level 0 succeeds (alternating, ending in NPOT texture)
155gl.activeTexture(gl.TEXTURE0 + 0);
156fillCubeTexture(gl, cubeTextures[0], 5, 5, [0, 192, 128, 255]);
157glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D with NPOT texture at level 0 should succeed");
158
159gl.activeTexture(gl.TEXTURE0 + 1);
160fillCubeTexture(gl, cubeTextures[1], 4, 4, [0, 192, 128, 255]);
161glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D with POT texture at level 0 should succeed");
162
163gl.activeTexture(gl.TEXTURE0 + 2);
164fillCubeTexture(gl, cubeTextures[2], 5, 5, [0, 192, 128, 255]);
165glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D with NPOT texture at level 0 should succeed");
166
167// 8. Check that an NPOT texture on level 0 succeeds (alternating, ending in POT texture)
168gl.activeTexture(gl.TEXTURE0 + 0);
169fillCubeTexture(gl, cubeTextures[0], 4, 4, [0, 192, 128, 255]);
170glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D with POT texture at level 0 should succeed");
171
172gl.activeTexture(gl.TEXTURE0 + 1);
173fillCubeTexture(gl, cubeTextures[1], 5, 5, [0, 192, 128, 255]);
174glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D with NPOT texture at level 0 should succeed");
175
176gl.activeTexture(gl.TEXTURE0 + 2);
177fillCubeTexture(gl, cubeTextures[2], 4, 4, [0, 192, 128, 255]);
178glErrorShouldBe(gl, gl.NO_ERROR, "gl.texImage2D with POT texture at level 0 should succeed");
179
180function fillCubeTexture(gl, tex, width, height, color, opt_level) {
181 opt_level = opt_level || 0;
182 var canvas = document.createElement('canvas');
183 canvas.width = width;
184 canvas.height = height;
185 var ctx2d = canvas.getContext('2d');
186 ctx2d.fillStyle = "rgba(" + color[0] + "," + color[1] + "," + color[2] + "," + color[3] + ")";
187 ctx2d.fillRect(0, 0, width, height);
188 gl.bindTexture(gl.TEXTURE_CUBE_MAP, tex);
189 var targets = [
190 gl.TEXTURE_CUBE_MAP_POSITIVE_X,
191 gl.TEXTURE_CUBE_MAP_NEGATIVE_X,
192 gl.TEXTURE_CUBE_MAP_POSITIVE_Y,
193 gl.TEXTURE_CUBE_MAP_NEGATIVE_Y,
194 gl.TEXTURE_CUBE_MAP_POSITIVE_Z,
195 gl.TEXTURE_CUBE_MAP_NEGATIVE_Z];
196 for (var tt = 0; tt < targets.length; ++tt) {
197 gl.texImage2D(
198 targets[tt], opt_level, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, canvas);
199 }
200};
201
202</script>
203</body>
204</html>
205