summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/textures/misc/texture-npot.html
blob: aa876bcb281db74521430afc6a699f255a019ebd (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<!--

/*
** Copyright (c) 2012 The Khronos Group Inc.
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and/or associated documentation files (the
** "Materials"), to deal in the Materials without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Materials, and to
** permit persons to whom the Materials are furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be included
** in all copies or substantial portions of the Materials.
**
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/

-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebGL Non-Power of 2 texture conformance test.</title>
<link rel="stylesheet" href="../../../resources/js-test-style.css"/>
<script src="../../../js/js-test-pre.js"></script>
<script src="../../../js/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">
precision mediump float;
uniform samplerCube tex;
varying vec2 texCoord;
void main()
{
    gl_FragColor = textureCube(tex, normalize(vec3(texCoord, 1)));
}
</script>
<script>
"use strict";
description(document.title);
var wtu = WebGLTestUtils;
var gl = wtu.create3DContext("example");
var program = wtu.setupTexturedQuad(gl);

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

var tests = [
  { format: gl.RGBA,
    type: gl.UNSIGNED_BYTE,
    color: [192, 0, 128, 64],
    expected: [192, 0, 128, 64],
    tolerance: 0,
  },
  { format: gl.RGB,
    type: gl.UNSIGNED_BYTE,
    color: [192, 0, 128],
    expected: [192, 0, 128, 255],
    tolerance: 0,
  },
  { format: gl.LUMINANCE,
    type: gl.UNSIGNED_BYTE,
    color: [192],
    expected: [192, 192, 192, 255],
    tolerance: 0,
  },
  { format: gl.ALPHA,
    type: gl.UNSIGNED_BYTE,
    color: [64],
    expected: [0, 0, 0, 64],
    tolerance: 0,
  },
  { format: gl.LUMINANCE_ALPHA,
    type: gl.UNSIGNED_BYTE,
    color: [192, 64],
    expected: [192, 192, 192, 64],
    tolerance: 0,
  },
  // { format: gl.RGBA,
  //   type: gl.UNSIGNED_SHORT_4_4_4_4,
  //   color: [0x48FC],
  //   expected: [0x44, 0x88, 0xFF, 0xCC],
  //   tolerance: 16,
  // },
  // { format: gl.RGBA,
  //   type: gl.UNSIGNED_SHORT_5_5_5_1,
  //   color: [0x8309],  // 10000 01000 00100 1
  //   expected: [128, 64, 32, 255],
  //   tolerance: 16,
  // },
  // { format: gl.RGB,
  //   type: gl.UNSIGNED_SHORT_5_6_5,
  //   color: [0x8404],  // 10000 010000 00100
  //   expected: [128, 64, 32],
  //   tolerance: 16,
  // },
];

tests.forEach(function(test) {
  debug("");
  debug("test " + wtu.glEnumToString(gl, test.format) + "/" + wtu.glEnumToString(gl, test.type));
  var tex = gl.createTexture();

  // Check that an NPOT texture not on level 0 generates INVALID_VALUE
  wtu.fillTexture(gl, tex, 5, 3, test.color, 1, test.format, test.type);
  wtu.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, test.color, 0, test.format, test.type);
  wtu.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);
  wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION,
      "gl.generateMipmap with NPOT texture should return INVALID_OPERATION");

  // 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.clearAndDrawUnitQuad(gl);
  wtu.checkCanvas(
      gl, [0, 0, 0, 255],
      "NPOT texture with TEXTURE_WRAP set to REPEAT should draw with 0,0,0,255");
  wtu.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.clearAndDrawUnitQuad(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");
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup.");

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

  wtu.clearAndDrawUnitQuad(gl);
  wtu.checkCanvas(
      gl, test.expected,
      "NPOT texture with TEXTURE_MIN_FILTER set to LINEAR should draw.");

  gl.copyTexImage2D(gl.TEXTURE_2D, 1, test.format, 0, 0, 5, 3, 0);
  wtu.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, test.color, 0, test.format);
  gl.generateMipmap(gl.TEXTURE_2D);
  wtu.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.clearAndDrawUnitQuad(gl);
  wtu.checkCanvas(
      gl, test.expected,
      "POT texture with TEXTURE_MIN_FILTER set to LINEAR_MIPMAP_LINEAR should draw.");
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup.");
});

var testCubemap = function(switchTextureUnitBeforeDraw) {
  debug("");
  var title = "check using cubemap";
  if (switchTextureUnitBeforeDraw) {
    title += " and switch texture unit before draw to check for Chromium bug";
  }
  debug(title);
  var program = wtu.setupProgram(
      gl, ['vshader', 'fshader'], ['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);
  wtu.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]);
  wtu.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);
  wtu.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);

  if (switchTextureUnitBeforeDraw) {
    debug("Switching active texture unit to gl.TEXTURE1");
    // Test for http://crbug.com/390514
    gl.activeTexture(gl.TEXTURE1);
  }

  wtu.clearAndDrawUnitQuad(gl);
  wtu.checkCanvas(
      gl, [0, 0, 0, 255],
      "NPOT cubemap with TEXTURE_WRAP set to REPEAT should draw with 0,0,0,255");

  if (switchTextureUnitBeforeDraw) {
    var error = gl.getError();
    if (error === gl.NO_ERROR) {
      testPassed("getError was expected value: NO_ERROR : Should be no errors from draw.");
    } else if (error === gl.INVALID_ENUM) {
      testFailed("getError returned INVALID_ENUM. Possibly Chromium bug where texture unit is set to 0 instead of GL_TEXTURE0.");
    } else {
      testFailed("Drawing resulted in error: " + wtu.glEnumToString(gl, error));
    }
    gl.activeTexture(gl.TEXTURE0);
  } else {
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from draw.");
  }

  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.clearAndDrawUnitQuad(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");
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup.");

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

  wtu.clearAndDrawUnitQuad(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]);
  wtu.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.clearAndDrawUnitQuad(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);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR,
      "gl.generateMipmap with POT texture should return succeed");

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

testCubemap(false);
testCubemap(true);

var 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>
<script src="../../../js/js-test-post.js"></script>

</body>
</html>