summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/textures/misc/gl-teximage.html
blob: 897377534ce4dc9fefa383b4ff203c29be678b85 (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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<!--
Copyright (c) 2019 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebGL texImage2D 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="256" height="16" style="width: 256px; height: 48px;"></canvas>
<div id="description"></div>
<div id="console"></div>
<script>
"use strict";
enableJSTestPreVerboseLogging();
description("Test texImage2D conversions.");
var wtu = WebGLTestUtils;
var gl = wtu.create3DContext("example");
gl.disable(gl.DITHER);
var program = wtu.setupTexturedQuad(gl);
var successfullyParsed;

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

var imgURLs = [
  '../../../resources/1-channel.jpg',
  '../../../resources/gray-ramp-256-with-128-alpha.png',
  '../../../resources/gray-ramp-256.png',
  '../../../resources/gray-ramp-default-gamma.png',
  '../../../resources/gray-ramp-gamma0.1.png',
  '../../../resources/gray-ramp-gamma1.0.png',
  '../../../resources/gray-ramp-gamma2.0.png',
  '../../../resources/gray-ramp-gamma4.0.png',
  '../../../resources/gray-ramp-gamma9.0.png',
  '../../../resources/gray-ramp.png',
  '../../../resources/zero-alpha.png',
  '../../../resources/3x3.png',
  '../../../resources/blue-1x1.jpg',
  '../../../resources/red-indexed.png',
  '../../../resources/transparent-on-left-indexed.png',
  '../../../resources/green-2x2-16bit.png',
  '../../../resources/small-square-with-colorspin-profile.jpg',
  '../../../resources/small-square-with-colorspin-profile.png',
  '../../../resources/small-square-with-cie-rgb-profile.png',
  '../../../resources/small-square-with-colormatch-profile.png',
  '../../../resources/small-square-with-e-srgb-profile.png',
  '../../../resources/small-square-with-smpte-c-profile.png',
  '../../../resources/small-square-with-srgb-iec61966-2.1-profile.png'];


wtu.loadImagesAsync(imgURLs, runTests);

function runTests(imgs) {
  var loc = gl.getUniformLocation(program, "tex");
  gl.uniform1i(loc, 0);

  gl.disable(gl.BLEND);
  gl.disable(gl.DEPTH_TEST);

  var width = gl.canvas.width;
  var height = gl.canvas.height;

  function checkPixel(x, y, color) {
    wtu.checkCanvasRect(gl, x, y, 1, 1, color);
  }

  function checkPixelRange(x, y, color, allowedRange) {
    var msg = "pixel " + x + ", " + y + " should be within " +
              allowedRange + " units of " +
              color[0] + ", " +
              color[1] + ", " +
              color[2] + ", " +
              color[3];
    wtu.checkCanvasRect(gl, x, y, 1, 1, color, msg, allowedRange);
  }

  var tex = gl.createTexture();
  gl.bindTexture(gl.TEXTURE_2D, tex);
  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);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);

  var buf = new Uint8Array(width * height * 4);

  debug("");
  debug("check pixels are NOT pre-multiplied");
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE,
                imgs['../../../resources/zero-alpha.png']);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup");
  wtu.clearAndDrawUnitQuad(gl);

  var left = 0;
  var middle = Math.floor(width / 2);
  var right = width - 1;
  var bottom = 0;
  var center = Math.floor(height / 2);
  var top = height - 1;
  checkPixel(left,   top,    [  0,   0,   0, 255]);
  checkPixel(middle, top,    [255,   0, 255, 255]);
  checkPixel(right,  top,    [  0,   0, 255, 255]);
  checkPixel(left,   center, [128, 128, 128, 255]);
  checkPixel(middle, center, [255, 255, 255, 255]);
  checkPixel(right,  center, [  0, 255, 255, 255]);
  checkPixel(left,   bottom, [255,   0,   0, 255]);
  checkPixel(middle, bottom, [255, 255,   0, 255]);
  checkPixel(right,  bottom, [  0, 255,   0, 255]);

  debug("");
  debug("check quantization");
  var quantInfo = [
    {format: gl.RGBA, type: gl.UNSIGNED_BYTE,          counts: [256, 256, 256, 256]},
    {format: gl.RGBA, type: gl.UNSIGNED_SHORT_4_4_4_4, counts: [ 16,  16,  16,  16]},
    {format: gl.RGB,  type: gl.UNSIGNED_SHORT_5_6_5,   counts: [ 32,  64,  32,   1]},
    {format: gl.RGBA, type: gl.UNSIGNED_SHORT_5_5_5_1, counts: [ 32,  32,  32,   2]}];
  for (var qq = 0; qq < quantInfo.length; ++qq) {
    var info = quantInfo[qq];
    gl.texImage2D(
        gl.TEXTURE_2D, 0, info.format, info.format, info.type,
        imgs['../../../resources/gray-ramp-256.png']);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup.");
    wtu.clearAndDrawUnitQuad(gl);
    gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buf);
    var counts = [{ }, { }, { }, { }];
    var numUniqueValues = [0, 0, 0, 0];
    // Count the number of unique values in each channel.
    for (var ii = 0; ii < width * height * 4; ii += 4) {
      for (var jj = 0; jj < 4; ++jj) {
        var v = buf[ii + jj];
        if (!counts[jj][v]) {
          counts[jj][v] = 1;
          ++numUniqueValues[jj];
        } else {
          ++counts[jj][v];
        }
      }
    }
    for (var ii = 0; ii < 4; ++ii) {
      assertMsg(numUniqueValues[ii] == info.counts[ii],
                "There should be " + info.counts[ii] +
                " unique values in channel " + ii + ". Found " +
                numUniqueValues[ii]);
    }
  }

  debug("");
  debug("Check that gamma settings don't effect 8bit pngs");
  wtu.failIfGLError(gl, 'gl.pixelStorei(gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, gl.NONE);');
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE,
                imgs['../../../resources/gray-ramp-default-gamma.png']);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup.");
  wtu.clearAndDrawUnitQuad(gl);
  var ref = new Uint8Array(width * height * 4);
  gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, ref);

  var gammaImages = [
    '../../../resources/gray-ramp-gamma0.1.png',
    '../../../resources/gray-ramp-gamma1.0.png',
    '../../../resources/gray-ramp-gamma2.0.png',
    '../../../resources/gray-ramp-gamma4.0.png',
    '../../../resources/gray-ramp-gamma9.0.png'];
  for (var ii = 0; ii < gammaImages.length; ++ii) {
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE,
                  imgs[gammaImages[ii]]);
    wtu.clearAndDrawUnitQuad(gl);
    gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buf);
    var same = true;
    for (var jj = 0; jj < width * height * 4; ++jj) {
      if (buf[jj] != ref[jj]) {
        same = false;
        break;
      }
    }
    assertMsg(same, "pixels should be same regardless of gamma settings.");
  }

  debug("");
  debug("check pixels are UN pre-multiplied");
  for (var ii = 0; ii < 2; ++ii) {
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
    if (ii == 0) {
      var canvas2d = document.createElement("canvas");
      canvas2d.width = 256;
      canvas2d.height = 1;
      var ctx = canvas2d.getContext("2d");
      ctx.drawImage(imgs['../../../resources/gray-ramp-256-with-128-alpha.png'], 0, 0);
      gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, canvas2d);
    } else {
      gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE,
                    imgs['../../../resources/gray-ramp-256-with-128-alpha.png']);
    }
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup.");
    wtu.clearAndDrawUnitQuad(gl);
    var buf = new Uint8Array(width * height * 4);
    gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buf);
    var lt128Count = [0, 0, 0];
    var ge128Count = [0, 0, 0];
    for (var jj = 0; jj < width; ++jj) {
      var off = jj * 4;
      for (var cc = 0; cc < 3; ++cc) {
        if (buf[off + cc] < 128) {
          ++lt128Count[cc];
        } else {
          ++ge128Count[cc];
        }
      }
    }
    // Not sure the exact count here because gamma does effect drawing into the
    // canvas but it should be close to 50% so I'll pass 45%
    for (var jj = 0; jj < 3; ++jj) {
      assertMsg(ge128Count[jj] > 256 * 0.45,
                "Half the pixels in channel " + jj +
                " should be >= 128,128,128. found " +
                ((ge128Count[jj] / 256) * 100).toFixed() + "%");
      assertMsg(lt128Count[jj] > 256 * 0.45,
                "Half the pixels in channel " + jj +
                " should be < 128,128,128. found " +
                ((lt128Count[jj] / 256) * 100).toFixed() + "%");
    }
  }

  debug("");
  debug("check canvas pixels are UN pre-multiplied");
  var canvas2d = document.createElement("canvas");
  canvas2d.width = 1;
  canvas2d.height = 1;
  var ctx = canvas2d.getContext("2d");
  ctx.fillStyle ="rgba(255,255,255,0.5)";
  ctx.fillRect(0, 0, 256, 1);
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, canvas2d);
  wtu.clearAndDrawUnitQuad(gl);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup.");
  checkPixelRange(0, 0, [255, 255, 255, 127], 4);

  debug("");
  debug("check canvas pixels are pre-multiplied");
  gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, canvas2d);
  wtu.clearAndDrawUnitQuad(gl);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup.");
  checkPixelRange(0, 0, [127, 127, 127, 127], 4);


  debug("");
  debug("check pixels are pre-multiplied");
  gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
  // TODO(gman): use different texture that won't pass on failure
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE,
                imgs['../../../resources/zero-alpha.png']);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup");
  wtu.clearAndDrawUnitQuad(gl);
  gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buf);

  var same = true;
  for (var jj = 0; jj < width * height * 4; ++jj) {
    if (buf[jj] != 0) {
      same = false;
      break;
    }
  }
  assertMsg(same, "pixels should all be 0.");

  debug("");
  debug("check pixels are flipped");
  gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
  gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE,
                imgs['../../../resources/3x3.png']);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup");
  wtu.clearAndDrawUnitQuad(gl);

  checkPixel(left,   top,    [255,   0,   0, 255]);
  checkPixel(middle, top,    [255, 255,   0, 255]);
  checkPixel(right,  top,    [255,   0,   0, 255]);
  checkPixel(left,   center, [255,   0, 255, 255]);
  checkPixel(middle, center, [255,   0,   0, 255]);
  checkPixel(right,  center, [  0, 255,   0, 255]);
  checkPixel(left,   bottom, [  0,   0,   0, 255]);
  checkPixel(middle, bottom, [  0,   0, 255, 255]);
  checkPixel(right,  bottom, [255,   0,   0, 255]);

  debug("");
  debug("check uploading of images with no alpha channel works");
  gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
  gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE,
                imgs['../../../resources/blue-1x1.jpg']);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup");
  wtu.clearAndDrawUnitQuad(gl);
  checkPixelRange(middle, center, [   0,   0, 255, 255], 10);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors");

  debug("");
  debug("check uploading of 16-bit images");
  gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
  gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE,
                imgs['../../../resources/green-2x2-16bit.png']);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup");
  wtu.clearAndDrawUnitQuad(gl);
  checkPixelRange(middle, center, [   15, 121,   0, 255], 10);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors");

  debug("");
  debug("check uploading of images with ICC profiles");
  gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
  gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
  wtu.failIfGLError(gl, 'gl.pixelStorei(gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, gl.NONE);');

  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE,
                imgs['../../../resources/small-square-with-colorspin-profile.jpg']);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup");
  wtu.clearAndDrawUnitQuad(gl);
  // The image is red.  However, if we ignore the color profile, it is blue.
  checkPixelRange(middle, center, [ 0, 0, 255, 255], 10);

  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE,
                imgs['../../../resources/small-square-with-colorspin-profile.png']);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup");
  wtu.clearAndDrawUnitQuad(gl);
  // The image is red.  However, if we ignore the color profile, it is blue.
  checkPixelRange(middle, center, [ 0, 0, 255, 255], 10);

  var iccPNGs = [
    '../../../resources/small-square-with-cie-rgb-profile.png',
    '../../../resources/small-square-with-colormatch-profile.png',
    '../../../resources/small-square-with-e-srgb-profile.png',
    '../../../resources/small-square-with-smpte-c-profile.png',
    '../../../resources/small-square-with-srgb-iec61966-2.1-profile.png'];
  gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buf);
  for (var ii = 0; ii < iccPNGs.length; ++ii) {
    var buf2 = new Uint8Array(width * height * 4);
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE,
                  imgs[iccPNGs[ii]]);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup");
    wtu.clearAndDrawUnitQuad(gl);
    gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buf2);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors");
    var same = true;
    for (var jj = 0; jj < buf.length; ++jj) {
      if (buf[jj] != buf2[jj]) {
        same = false;
        break;
      }
    }
    assertMsg(same, "uploading PNGs with same data but various ICC profiles should generate the same results");
  }

  debug("");
  debug("check uploading of indexed PNG images");
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE,
                imgs['../../../resources/red-indexed.png']);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup");
  wtu.clearAndDrawUnitQuad(gl);
  // The image should be red.
  checkPixelRange(middle, center, [ 255, 0, 0, 255 ], 10);

  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup");
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE,
                imgs['../../../resources/transparent-on-left-indexed.png']);
  wtu.clearAndDrawUnitQuad(gl);
  wtu.checkCanvasRect(gl, 0, 0, 128, 16, [255, 0, 255, 0], "should be transparent purple");
  wtu.checkCanvasRect(gl, 128, 0,128, 16, [255, 255, 0, 255], "should be yellow");

  debug("");
  debug("check uploading of 1-channel JPG images");
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE,
                imgs['../../../resources/1-channel.jpg']);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup");
  wtu.clearAndDrawUnitQuad(gl);
  // The image should be gray.
  checkPixelRange(middle, center, [ 128, 128, 128, 255 ], 28);

  debug("")
  debug("check calling texImage2D with NULL clears the texture");
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB,
                imgs['../../../resources/red-indexed.png'].width,
                imgs['../../../resources/red-indexed.png'].height,
                0, gl.RGB, gl.UNSIGNED_BYTE, null);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup");
  wtu.clearAndDrawUnitQuad(gl);
  // The image should be white.
  checkPixelRange(middle, center, [ 0, 0, 0, 255 ], 10);

  debug("");
  debug("check zero size cases");
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.LUMINANCE, 2, 0, 0, gl.LUMINANCE, gl.UNSIGNED_BYTE, new Uint8Array());
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from zero sized textures");

  debug("");
  successfullyParsed = true;
  shouldBeTrue("successfullyParsed");
  debug('<br /><span class="pass">TEST COMPLETE</span>');
  notifyFinishedToHarness();
}
</script>
</body>
</html>