|
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 Non-Power of 2 texture 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="4" height="4" style="width: 40px; height: 30px;"></canvas> |
|
18 <div id="description"></div> |
|
19 <div id="console"></div> |
|
20 <script id="vshader" type="x-shader/x-vertex"> |
|
21 attribute vec4 vPosition; |
|
22 attribute vec2 texCoord0; |
|
23 varying vec2 texCoord; |
|
24 void main() |
|
25 { |
|
26 gl_Position = vPosition; |
|
27 texCoord = texCoord0; |
|
28 } |
|
29 </script> |
|
30 |
|
31 <script id="fshader" type="x-shader/x-fragment"> |
|
32 precision mediump float; |
|
33 uniform samplerCube tex; |
|
34 varying vec2 texCoord; |
|
35 void main() |
|
36 { |
|
37 gl_FragColor = textureCube(tex, normalize(vec3(texCoord, 1))); |
|
38 } |
|
39 </script> |
|
40 <script> |
|
41 description(document.title); |
|
42 var wtu = WebGLTestUtils; |
|
43 var gl = wtu.create3DContext("example"); |
|
44 var program = wtu.setupTexturedQuad(gl); |
|
45 |
|
46 glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup."); |
|
47 |
|
48 var tex = gl.createTexture(); |
|
49 |
|
50 // Check that an NPOT texture not on level 0 generates INVALID_VALUE |
|
51 wtu.fillTexture(gl, tex, 5, 3, [0, 192, 128, 255], 1); |
|
52 glErrorShouldBe(gl, gl.INVALID_VALUE, |
|
53 "gl.texImage2D with NPOT texture with level > 0 should return INVALID_VALUE"); |
|
54 |
|
55 // Check that an NPOT texture on level 0 succeeds |
|
56 wtu.fillTexture(gl, tex, 5, 3, [0, 192, 128, 255]); |
|
57 glErrorShouldBe(gl, gl.NO_ERROR, |
|
58 "gl.texImage2D with NPOT texture at level 0 should succeed"); |
|
59 |
|
60 // Check that generateMipmap fails on NPOT |
|
61 gl.generateMipmap(gl.TEXTURE_2D); |
|
62 glErrorShouldBe(gl, gl.INVALID_OPERATION, |
|
63 "gl.generateMipmap with NPOT texture should return INVALID_OPERATION"); |
|
64 |
|
65 var loc = gl.getUniformLocation(program, "tex"); |
|
66 gl.uniform1i(loc, 0); |
|
67 |
|
68 // Check that nothing is drawn if filtering is not correct for NPOT |
|
69 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); |
|
70 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); |
|
71 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT); |
|
72 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT); |
|
73 |
|
74 wtu.drawQuad(gl); |
|
75 wtu.checkCanvas( |
|
76 gl, [0, 0, 0, 255], |
|
77 "NPOT texture with TEXTURE_WRAP set to REPEAT should draw with 0,0,0,255"); |
|
78 glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup."); |
|
79 |
|
80 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); |
|
81 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); |
|
82 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_LINEAR); |
|
83 |
|
84 wtu.drawQuad(gl); |
|
85 wtu.checkCanvas( |
|
86 gl, [0, 0, 0, 255], |
|
87 "NPOT texture with TEXTURE_MIN_FILTER not NEAREST or LINEAR should draw with 0,0,0,255"); |
|
88 glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup."); |
|
89 |
|
90 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); |
|
91 |
|
92 wtu.drawQuad(gl); |
|
93 wtu.checkCanvas( |
|
94 gl, [0, 192, 128, 255], |
|
95 "NPOT texture with TEXTURE_MIN_FILTER set to LINEAR should draw."); |
|
96 |
|
97 gl.copyTexImage2D(gl.TEXTURE_2D, 1, gl.RGBA, 0, 0, 5, 3, 0); |
|
98 glErrorShouldBe(gl, gl.INVALID_VALUE, |
|
99 "copyTexImage2D with NPOT texture with level > 0 should return INVALID_VALUE."); |
|
100 |
|
101 // Check that generateMipmap for an POT texture succeeds |
|
102 wtu.fillTexture(gl, tex, 4, 4, [0, 192, 128, 255]); |
|
103 gl.generateMipmap(gl.TEXTURE_2D); |
|
104 glErrorShouldBe(gl, gl.NO_ERROR, |
|
105 "gl.texImage2D and gl.generateMipmap with POT texture at level 0 should succeed"); |
|
106 |
|
107 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR); |
|
108 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); |
|
109 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT); |
|
110 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT); |
|
111 |
|
112 wtu.drawQuad(gl); |
|
113 wtu.checkCanvas( |
|
114 gl, [0, 192, 128, 255], |
|
115 "POT texture with TEXTURE_MIN_FILTER set to LINEAR_MIPMAP_LINEAR should draw."); |
|
116 glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup."); |
|
117 |
|
118 debug(""); |
|
119 debug("check using cubemap"); |
|
120 var program = wtu.setupProgram( |
|
121 gl, ['vshader', 'fshader'], ['vPosition', 'texCoord0'], [0, 1]); |
|
122 var tex = gl.createTexture(); |
|
123 |
|
124 // Check that an NPOT texture not on level 0 generates INVALID_VALUE |
|
125 fillCubeTexture(gl, tex, 5, 3, [0, 192, 128, 255], 1); |
|
126 glErrorShouldBe(gl, gl.INVALID_VALUE, |
|
127 "gl.texImage2D with NPOT texture with level > 0 should return INVALID_VALUE"); |
|
128 |
|
129 // Check that an NPOT texture on level 0 succeeds |
|
130 fillCubeTexture(gl, tex, 5, 5, [0, 192, 128, 255]); |
|
131 glErrorShouldBe(gl, gl.NO_ERROR, |
|
132 "gl.texImage2D with NPOT texture at level 0 should succeed"); |
|
133 |
|
134 // Check that generateMipmap fails on NPOT |
|
135 gl.generateMipmap(gl.TEXTURE_CUBE_MAP); |
|
136 glErrorShouldBe(gl, gl.INVALID_OPERATION, |
|
137 "gl.generateMipmap with NPOT texture should return INVALID_OPERATION"); |
|
138 |
|
139 var loc = gl.getUniformLocation(program, "tex"); |
|
140 gl.uniform1i(loc, 0); |
|
141 |
|
142 // Check that nothing is drawn if filtering is not correct for NPOT |
|
143 gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.NEAREST); |
|
144 gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.NEAREST); |
|
145 gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.REPEAT); |
|
146 gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.REPEAT); |
|
147 |
|
148 wtu.drawQuad(gl); |
|
149 wtu.checkCanvas( |
|
150 gl, [0, 0, 0, 255], |
|
151 "NPOT cubemap with TEXTURE_WRAP set to REPEAT should draw with 0,0,0,255"); |
|
152 glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup."); |
|
153 |
|
154 gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); |
|
155 gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); |
|
156 gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_LINEAR); |
|
157 |
|
158 wtu.drawQuad(gl); |
|
159 wtu.checkCanvas( |
|
160 gl, [0, 0, 0, 255], |
|
161 "NPOT cubemap with TEXTURE_MIN_FILTER not NEAREST or LINEAR should draw with 0,0,0,255"); |
|
162 glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup."); |
|
163 |
|
164 gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.LINEAR); |
|
165 |
|
166 wtu.drawQuad(gl); |
|
167 wtu.checkCanvas( |
|
168 gl, [0, 192, 128, 255], |
|
169 "NPOT cubemap with TEXTURE_MIN_FILTER set to LINEAR should draw."); |
|
170 |
|
171 // Check that an POT texture on level 0 succeeds |
|
172 fillCubeTexture(gl, tex, 4, 4, [0, 192, 128, 255]); |
|
173 glErrorShouldBe(gl, gl.NO_ERROR, |
|
174 "gl.texImage2D with POT texture at level 0 should succeed"); |
|
175 |
|
176 gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR); |
|
177 gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR); |
|
178 gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.REPEAT); |
|
179 gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.REPEAT); |
|
180 |
|
181 wtu.drawQuad(gl); |
|
182 wtu.checkCanvas( |
|
183 gl, [0, 0, 0, 255], |
|
184 "POT cubemap with TEXTURE_MIN_FILTER set to LINEAR_MIPMAP_LINEAR but no mips draw with 0,0,0,255"); |
|
185 |
|
186 // Check that generateMipmap succeeds on POT |
|
187 gl.generateMipmap(gl.TEXTURE_CUBE_MAP); |
|
188 glErrorShouldBe(gl, gl.NO_ERROR, |
|
189 "gl.generateMipmap with POT texture should return succeed"); |
|
190 |
|
191 wtu.drawQuad(gl); |
|
192 wtu.checkCanvas( |
|
193 gl, [0, 192, 128, 255], |
|
194 "POT cubemap with TEXTURE_MIN_FILTER set to LINEAR_MIPMAP_LINEAR should draw."); |
|
195 |
|
196 successfullyParsed = true; |
|
197 |
|
198 function fillCubeTexture(gl, tex, width, height, color, opt_level) { |
|
199 opt_level = opt_level || 0; |
|
200 var canvas = document.createElement('canvas'); |
|
201 canvas.width = width; |
|
202 canvas.height = height; |
|
203 var ctx2d = canvas.getContext('2d'); |
|
204 ctx2d.fillStyle = "rgba(" + color[0] + "," + color[1] + "," + color[2] + "," + color[3] + ")"; |
|
205 ctx2d.fillRect(0, 0, width, height); |
|
206 gl.bindTexture(gl.TEXTURE_CUBE_MAP, tex); |
|
207 var targets = [ |
|
208 gl.TEXTURE_CUBE_MAP_POSITIVE_X, |
|
209 gl.TEXTURE_CUBE_MAP_NEGATIVE_X, |
|
210 gl.TEXTURE_CUBE_MAP_POSITIVE_Y, |
|
211 gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, |
|
212 gl.TEXTURE_CUBE_MAP_POSITIVE_Z, |
|
213 gl.TEXTURE_CUBE_MAP_NEGATIVE_Z]; |
|
214 for (var tt = 0; tt < targets.length; ++tt) { |
|
215 gl.texImage2D( |
|
216 targets[tt], opt_level, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, canvas); |
|
217 } |
|
218 }; |
|
219 |
|
220 </script> |
|
221 <script>finishTest();</script> |
|
222 |
|
223 </body> |
|
224 </html> |
|
225 |