summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/textures/misc/texture-size.html
blob: 12d99ac89f2feab70ff39f118fe768cd5b3cdba6 (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
<!--
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 texture size 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="32" height="32" style="width: 40px; height: 40px;"></canvas>
<div id="description"></div>
<div id="console"></div>
<script id="vshader" type="x-shader/x-vertex">
attribute vec4 vPosition;
attribute vec3 texCoord0;
varying vec3 texCoord;
void main()
{
    gl_Position = vPosition;
    texCoord = texCoord0;
}
</script>

<script id="fshader" type="x-shader/x-fragment">
precision mediump float;
uniform samplerCube tex;
varying vec3 texCoord;
void main()
{
    gl_FragColor = textureCube(tex, normalize(texCoord));
}
</script>
<script>
"use strict";
description("Checks that various sizes of textures render")

var wtu = WebGLTestUtils;
var gl = wtu.create3DContext("example");
var program2D = wtu.setupTexturedQuad(gl);
var programCubeMap = wtu.setupProgram(
    gl, ['vshader', 'fshader'], ['vPosition', 'texCoord0'], [0, 1]);
gl.disable(gl.DEPTH_TEST);
gl.disable(gl.BLEND);
var tex = gl.createTexture();
var max2DSize = gl.getParameter(gl.MAX_TEXTURE_SIZE);
var maxCubeMapSize = gl.getParameter(gl.MAX_CUBE_MAP_TEXTURE_SIZE);
debug("MAX_TEXTURE_SIZE:" + max2DSize);
debug("MAX_CUBE_MAP_TEXTURE_SIZE:" + maxCubeMapSize);
// Assuming 2048x2048xRGBA (22meg with mips) will run on all WebGL platforms
var max2DSquareSize = Math.min(max2DSize, 2048);
// I'd prefer this to be 2048 but that's 16meg x 6 faces or 128meg (with mips)
// 1024 is 33.5 meg (with mips)
var maxCubeMapSize = Math.min(maxCubeMapSize, 1024);

var colors = [
  { name: "green", rgba: [0, 0, 255, 255] },
  { name: "red", rgba: [255, 0, 0, 255] },
  { name: "blue", rgba: [0, 255, 0, 255] },
  { name: "yellow", rgba: [255, 255, 0, 255] },
  { name: "magenta", rgba: [255, 0, 255, 255] },
  { name: "cyan", rgba: [0, 255, 255, 255] }
];

var count = 0;
var power = 0;
runTest();

function runTest() {
  function doTest() {
    var size = Math.pow(2, power);
    if (size > max2DSize) {
      return false;
    }
    gl.useProgram(program2D);
    if (!checkTexture(size, 1, false)) return false;
    if (!checkTexture(1, size, false)) return false;
    if (size <= max2DSquareSize) {
      if (!checkTexture(size, size, false)) {
        return false;
      }
    }
    if (size <= maxCubeMapSize) {
      gl.useProgram(programCubeMap);
      if (!checkTexture(size, size, true)) {
        return false;
      }
    }
    return true;
  }

  if (doTest()) {
    ++power;
    setTimeout(runTest, 0);
  } else {
    finishTest();
  }
}

function checkTexture(width, height, cubeMap) {
  debug("");
  count = (count + 1) % colors.length;
  var color = colors[count];
  var tex = gl.createTexture();
  var target = cubeMap ? gl.TEXTURE_CUBE_MAP : gl.TEXTURE_2D;
  var type = cubeMap ? "cube map" : "2D texture";
  debug("check " + width + ", " + height + " " + type);
  gl.bindTexture(target, tex);
  gl.texParameteri(target, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
  gl.texParameteri(target, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  gl.texParameteri(target, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  fillLevel(0, width, height, color.rgba, cubeMap);
  var err = gl.getError();
  if (err == gl.OUT_OF_MEMORY) {
    debug("out of memory");
    return false;
  }
  if (err != gl.NO_ERROR) {
    testFailed("unexpected gl error: " + wtu.glEnumToString(gl, err));
  }
  wtu.clearAndDrawUnitQuad(gl);
  var tolerance = 3;
  wtu.checkCanvas(gl, color.rgba,
      type + " of size " + width + "x" + height + " with no mips should draw with " + color.name,
      tolerance);
  count = (count + 1) % colors.length;
  color = colors[count];
  fillLevel(0, width, height, color.rgba, cubeMap);
  gl.generateMipmap(target);
  var err = gl.getError();
  if (err == gl.OUT_OF_MEMORY) {
    debug("out of memory");
    return false;
  }
  if (err != gl.NO_ERROR) {
    testFailed("unexpected gl error: " + wtu.glEnumToString(gl, err));
  }
  gl.texParameteri(target, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
  wtu.clearAndDrawUnitQuad(gl);
  wtu.checkCanvas(gl, color.rgba,
      type + " of size " + width + "x" + height + " with mips should draw with " + color.name,
      tolerance);

  count = (count + 1) % colors.length;
  color = colors[count];
  fillLevel(0, width, height, color.rgba, cubeMap, true);
  gl.generateMipmap(target);

  wtu.clearAndDrawUnitQuad(gl);
  wtu.checkCanvas(gl, color.rgba,
      type + " of size " + width + "x" + height + " with mips should draw with " + color.name,
      tolerance);

  gl.deleteTexture(tex);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors.");
  return true;
}

function fillLevel(level, width, height, color, opt_cubemap, opt_subTex) {
  var numPixels = width * height;
  var pixels = null;
  var largeDim = Math.max(width, height);
  var smallDim = Math.min(width, height);

  var pixelRow = new Uint8Array(largeDim * 4);
  for (var jj = 0; jj < largeDim; ++jj) {
    var off = jj * 4;
    pixelRow[off + 0] = color[0];
    pixelRow[off + 1] = color[1];
    pixelRow[off + 2] = color[2];
    pixelRow[off + 3] = color[3];
  }

  if (largeDim == numPixels) {
    pixels = pixelRow;
  } else {
    var pixels = new Uint8Array(numPixels * 4);
    for (var jj = 0; jj < smallDim; ++jj) {
      var off = jj * largeDim * 4;
      pixels.set(pixelRow, off);
    }
  }

  var targets = opt_cubemap ? [
    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] :
    [gl.TEXTURE_2D];

  for (var ii = 0; ii < targets.length; ++ii) {
    // debug(wtu.glEnumToString(gl, targets[ii]));
    var index = (ii + power) % targets.length;
    var target = targets[index];
    if (opt_subTex) {
      gl.texSubImage2D(
          target, level, 0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE,
          pixels);
    } else {
      gl.texImage2D(
          target, level, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE,
          pixels);
    }
  }
}
</script>
</body>
</html>