summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/LayoutTests/fast/canvas/webgl/texture-npot.html
blob: fde02fc65fdb13266aec79376fde03c2fa81c7f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>WebGL Non-Power of 2 texture conformance test.</title>
<link rel="stylesheet" href="../../js/resources/js-test-style.css"/>
<script src="../../js/resources/js-test-pre.js"></script>
<script src="resources/webgl-test.js"> </script>
<script src="resources/webgl-test-utils.js"> </script>
</head>
<body>
<canvas id="example" width="4" height="4" style="width: 40px; height: 30px;"></canvas>
<div id="description"></div>
<div id="console"></div>
<script id="vshader" type="x-shader/x-vertex">
attribute vec4 vPosition;
attribute vec2 texCoord0;
varying vec2 texCoord;
void main()
{
    gl_Position = vPosition;
    texCoord = texCoord0;
}
</script>

<script id="fshader" type="x-shader/x-fragment">
#ifdef GL_ES
precision mediump float;
#endif
uniform samplerCube tex;
varying vec2 texCoord;
void main()
{
    gl_FragColor = textureCube(tex, normalize(vec3(texCoord, 1)));
}
</script>
<script>
var wtu = WebGLTestUtils;
var canvas = document.getElementById("example");
var gl = wtu.create3DContext(canvas);
var program = wtu.setupTexturedQuad(gl);

glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup.");

var tex = gl.createTexture();

// Check that an NPOT texture not on level 0 generates INVALID_VALUE
wtu.fillTexture(gl, tex, 5, 3, [0, 192, 128, 255], 1);
glErrorShouldBe(gl, gl.INVALID_VALUE,
    "gl.texImage2D with NPOT texture with level > 0 should return INVALID_VALUE");

// Check that an NPOT texture on level 0 succeeds
wtu.fillTexture(gl, tex, 5, 3, [0, 192, 128, 255]);
glErrorShouldBe(gl, gl.NO_ERROR,
    "gl.texImage2D with NPOT texture at level 0 should succeed");

// Check that generateMipmap fails on NPOT
gl.generateMipmap(gl.TEXTURE_2D);
glErrorShouldBe(gl, gl.INVALID_OPERATION,
    "gl.generateMipmap with NPOT texture should return INVALID_OPERATION");

var loc = gl.getUniformLocation(program, "tex");
gl.uniform1i(loc, 0);

// Check that nothing is drawn if filtering is not correct for NPOT
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT);

wtu.drawQuad(gl);
wtu.checkCanvas(
    gl, [0, 0, 0, 255],
    "NPOT texture with TEXTURE_WRAP set to REPEAT should draw with 0,0,0,255");
glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup.");

gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_LINEAR);

wtu.drawQuad(gl);
wtu.checkCanvas(
    gl, [0, 0, 0, 255],
    "NPOT texture with TEXTURE_MIN_FILTER not NEAREST or LINEAR should draw with 0,0,0,255");
glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup.");

gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);

wtu.drawQuad(gl);
wtu.checkCanvas(
    gl, [0, 192, 128, 255],
    "NPOT texture with TEXTURE_MIN_FILTER set to LINEAR should draw.");

gl.copyTexImage2D(gl.TEXTURE_2D, 1, gl.RGBA, 0, 0, 5, 3, 0);
glErrorShouldBe(gl, gl.INVALID_VALUE,
    "copyTexImage2D with NPOT texture with level > 0 should return INVALID_VALUE.");

// Check that generateMipmap for an POT texture succeeds
wtu.fillTexture(gl, tex, 4, 4, [0, 192, 128, 255]);
gl.generateMipmap(gl.TEXTURE_2D);
glErrorShouldBe(gl, gl.NO_ERROR,
    "gl.texImage2D and gl.generateMipmap with POT texture at level 0 should succeed");

gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT);

wtu.drawQuad(gl);
wtu.checkCanvas(
    gl, [0, 192, 128, 255],
    "POT texture with TEXTURE_MIN_FILTER set to LINEAR_MIPMAP_LINEAR should draw.");
glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup.");

debug("");
debug("check using cubemap");
var program = wtu.setupProgram(
    gl,
    [wtu.loadShaderFromScript(gl, 'vshader', gl.VERTEX_SHADER),
     wtu.loadShaderFromScript(gl, 'fshader', gl.FRAGMENT_SHADER)],
    ['vPosition', 'texCoord0'], [0, 1]);
var tex = gl.createTexture();

// Check that an NPOT texture not on level 0 generates INVALID_VALUE
fillCubeTexture(gl, tex, 5, 3, [0, 192, 128, 255], 1);
glErrorShouldBe(gl, gl.INVALID_VALUE,
    "gl.texImage2D with NPOT texture with level > 0 should return INVALID_VALUE");

// Check that an NPOT texture on level 0 succeeds
fillCubeTexture(gl, tex, 5, 5, [0, 192, 128, 255]);
glErrorShouldBe(gl, gl.NO_ERROR,
    "gl.texImage2D with NPOT texture at level 0 should succeed");

// Check that generateMipmap fails on NPOT
gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
glErrorShouldBe(gl, gl.INVALID_OPERATION,
    "gl.generateMipmap with NPOT texture should return INVALID_OPERATION");

var loc = gl.getUniformLocation(program, "tex");
gl.uniform1i(loc, 0);

// Check that nothing is drawn if filtering is not correct for NPOT
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.REPEAT);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.REPEAT);

wtu.drawQuad(gl);
wtu.checkCanvas(
    gl, [0, 0, 0, 255],
    "NPOT cubemap with TEXTURE_WRAP set to REPEAT should draw with 0,0,0,255");
glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup.");

gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_LINEAR);

wtu.drawQuad(gl);
wtu.checkCanvas(
    gl, [0, 0, 0, 255],
    "NPOT cubemap with TEXTURE_MIN_FILTER not NEAREST or LINEAR should draw with 0,0,0,255");
glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup.");

gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.LINEAR);

wtu.drawQuad(gl);
wtu.checkCanvas(
    gl, [0, 192, 128, 255],
    "NPOT cubemap with TEXTURE_MIN_FILTER set to LINEAR should draw.");

// Check that an POT texture on level 0 succeeds
fillCubeTexture(gl, tex, 4, 4, [0, 192, 128, 255]);
glErrorShouldBe(gl, gl.NO_ERROR,
    "gl.texImage2D with POT texture at level 0 should succeed");

gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.REPEAT);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.REPEAT);

wtu.drawQuad(gl);
wtu.checkCanvas(
    gl, [0, 0, 0, 255],
    "POT cubemap with TEXTURE_MIN_FILTER set to LINEAR_MIPMAP_LINEAR but no mips draw with 0,0,0,255");

// Check that generateMipmap succeeds on POT
gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
glErrorShouldBe(gl, gl.NO_ERROR,
    "gl.generateMipmap with POT texture should return succeed");

wtu.drawQuad(gl);
wtu.checkCanvas(
    gl, [0, 192, 128, 255],
    "POT cubemap with TEXTURE_MIN_FILTER set to LINEAR_MIPMAP_LINEAR should draw.");

successfullyParsed = true;

function fillCubeTexture(gl, tex, width, height, color, opt_level) {
  opt_level = opt_level || 0;
  var canvas = document.createElement('canvas');
  canvas.width = width;
  canvas.height = height;
  var ctx2d = canvas.getContext('2d');
  ctx2d.fillStyle = "rgba(" + color[0] + "," + color[1] + "," + color[2] + "," + color[3] + ")";
  ctx2d.fillRect(0, 0, width, height);
  gl.bindTexture(gl.TEXTURE_CUBE_MAP, tex);
  var targets = [
    gl.TEXTURE_CUBE_MAP_POSITIVE_X,
    gl.TEXTURE_CUBE_MAP_NEGATIVE_X,
    gl.TEXTURE_CUBE_MAP_POSITIVE_Y,
    gl.TEXTURE_CUBE_MAP_NEGATIVE_Y,
    gl.TEXTURE_CUBE_MAP_POSITIVE_Z,
    gl.TEXTURE_CUBE_MAP_NEGATIVE_Z];
  for (var tt = 0; tt < targets.length; ++tt) {
    gl.texImage2D(
        targets[tt], opt_level, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, canvas);
  }
};

</script>
</body>
<script src="../../js/resources/js-test-post.js"></script>

<script>
</script>

</body>
</html>