summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/js/tests/webgl-compressed-texture-size-limit.js
blob: 6fad5520f2e6ea22f40aee941be39ae6faa42a5d (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
/*
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.
*/

'use strict';

var runCompressedTextureSizeLimitTest = function(maxArrayBufferSizeBytes, positiveCubeMapMaxSize) {

  function numLevelsFromSize(size) {
    var levels = 0;
    while ((size >> levels) > 0) {
      ++levels;
    }
    return levels;
  }

  // More formats can be added here when more texture compression extensions are enabled in WebGL.
  var validFormats = {
      COMPRESSED_RGB_S3TC_DXT1_EXT        : 0x83F0,
      COMPRESSED_RGBA_S3TC_DXT1_EXT       : 0x83F1,
      COMPRESSED_RGBA_S3TC_DXT3_EXT       : 0x83F2,
      COMPRESSED_RGBA_S3TC_DXT5_EXT       : 0x83F3,
  };

  // format specific restrictions for COMPRESSED_RGB_S3TC_DXT1_EXT and COMPRESSED_RGBA_S3TC_DXT1_EXT
  // on the byteLength of the ArrayBufferView, pixels
  function func1 (width, height)
  {
      return Math.floor((width + 3) / 4) * Math.floor((height + 3) / 4) * 8;
  }

  // format specific restrictions for COMPRESSED_RGBA_S3TC_DXT3_EXT and COMPRESSED_RGBA_S3TC_DXT5_EXT
  // on the byteLength of the ArrayBufferView, pixels
  function func2 (width, height)
  {
      return Math.floor((width + 3) / 4) * Math.floor((height + 3) / 4) * 16;
  }

  var wtu = WebGLTestUtils;
  var gl = wtu.create3DContext("example");
  var tests = [
    // More tests can be added here when more texture compression extensions are enabled in WebGL.
    // Level 0 image width and height must be a multiple of the sizeStep.
    { extension: "WEBGL_compressed_texture_s3tc", format: validFormats.COMPRESSED_RGB_S3TC_DXT1_EXT, dataType: Uint8Array, func: func1, sizeStep: 4},
    { extension: "WEBGL_compressed_texture_s3tc", format: validFormats.COMPRESSED_RGBA_S3TC_DXT1_EXT, dataType: Uint8Array, func: func1, sizeStep: 4},
    { extension: "WEBGL_compressed_texture_s3tc", format: validFormats.COMPRESSED_RGBA_S3TC_DXT3_EXT, dataType: Uint8Array, func: func2, sizeStep: 4},
    { extension: "WEBGL_compressed_texture_s3tc", format: validFormats.COMPRESSED_RGBA_S3TC_DXT5_EXT, dataType: Uint8Array, func: func2, sizeStep: 4},
  ];

  // Note: We expressly only use 2 textures because first a texture will be defined
  // using all mip levels of 1 format, then for a moment it will have mixed formats which
  // may uncover bugs.
  var targets = [
    { target: gl.TEXTURE_2D,
      maxSize: gl.getParameter(gl.MAX_TEXTURE_SIZE),
      tex: gl.createTexture(),
      targets: [gl.TEXTURE_2D]
    },
    { target: gl.TEXTURE_CUBE_MAP,
      maxSize: gl.getParameter(gl.MAX_CUBE_MAP_TEXTURE_SIZE),
      tex: gl.createTexture(),
      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
      ]
    }
  ];

  function getSharedArrayBufferSize() {
    var sharedArrayBufferSize = 0;
    for (var tt = 0; tt < tests.length; ++tt) {
      var test = tests[tt];
      for (var trg = 0; trg < targets.length; ++trg) {
        var t = targets[trg];
        var bufferSizeNeeded;
        if (t.target === gl.TEXTURE_CUBE_MAP) {
          var positiveTestSize = Math.min(2048, t.maxSize);
          bufferSizeNeeded = test.func(positiveTestSize, positiveTestSize);
        } else {
          bufferSizeNeeded = test.func(t.maxSize, test.sizeStep);
        }
        if (bufferSizeNeeded > sharedArrayBufferSize) {
          sharedArrayBufferSize = bufferSizeNeeded;
        }
        bufferSizeNeeded = test.func(t.maxSize + test.sizeStep, t.maxSize + test.sizeStep);
        // ArrayBuffers can be at most 4GB (minus 1 byte).
        if (bufferSizeNeeded > sharedArrayBufferSize && bufferSizeNeeded <= maxArrayBufferSizeBytes) {
          sharedArrayBufferSize = bufferSizeNeeded;
        }
      }
    }
    return sharedArrayBufferSize;
  }

  // Share an ArrayBuffer among tests to avoid too many large allocations
  var sharedArrayBuffer = new ArrayBuffer(getSharedArrayBufferSize());

  gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);

  var trg = 0;
  var tt = 0;
  runNextTest();

  function runNextTest() {
    var t = targets[trg];

    if (tt == 0) {
      var tex = t.tex;
      gl.bindTexture(t.target, tex);

      debug("");
      debug("max size for " + wtu.glEnumToString(gl, t.target) + ": " + t.maxSize);
    }

    var test = tests[tt];
    testFormatType(t, test);
    ++tt;
    if (tt == tests.length) {
      tt = 0;
      ++trg;
      if (trg == targets.length) {
        finishTest();
        return;
      }
    }
    wtu.dispatchPromise(runNextTest);
  }

  function testFormatType(t, test) {
    var positiveTestSize = t.maxSize;
    var positiveTestOtherDimension = test.sizeStep;
    if (t.target === gl.TEXTURE_CUBE_MAP) {
      // Can't always test the maximum size since that can cause OOM:
      positiveTestSize = Math.min(positiveCubeMapMaxSize, t.maxSize);
      // Cube map textures need to be square:
      positiveTestOtherDimension = positiveTestSize;
    }
    var positiveTestLevels = numLevelsFromSize(positiveTestSize);
    var numLevels = numLevelsFromSize(t.maxSize);
    debug("");
    debug("num levels: " + numLevels + ", levels used in positive test: " + positiveTestLevels);

    debug("");

    // Query the extension and store globally so shouldBe can access it
    var ext = wtu.getExtensionWithKnownPrefixes(gl, test.extension);
    if (ext) {

      testPassed("Successfully enabled " + test.extension + " extension");

      for (var j = 0; j < t.targets.length; ++j) {
        var target = t.targets[j];
        debug("");
        debug(wtu.glEnumToString(gl, target) + " " + wtu.glEnumToString(ext, test.format));

        // positive test
        var size = positiveTestSize;
        var otherDimension = positiveTestOtherDimension;
        for (var i = 0; i < positiveTestLevels; i++) {
          var pixels = new test.dataType(sharedArrayBuffer, 0, test.func(size, otherDimension));
          gl.compressedTexImage2D(target, i, test.format, size, otherDimension, 0, pixels);
          wtu.glErrorShouldBe(gl, gl.NO_ERROR, "uploading compressed texture should generate NO_ERROR."
              + "level is " + i + ", size is " + size + "x" + otherDimension);
          size /= 2;
          otherDimension /= 2;
          if (otherDimension < 1) {
              otherDimension = 1;
          }
        }

        var numLevels = numLevelsFromSize(t.maxSize);

        // out of bounds tests

        // width or height out of bounds
        if (t.target != gl.TEXTURE_CUBE_MAP) {
            // only width out of bounds
            var wideAndShortDataSize = test.func(t.maxSize + test.sizeStep, test.sizeStep);
            var pixelsNegativeTest1 = new test.dataType(sharedArrayBuffer, 0, wideAndShortDataSize);
            gl.compressedTexImage2D(target, 0, test.format, t.maxSize + test.sizeStep, test.sizeStep, 0, pixelsNegativeTest1);
            wtu.glErrorShouldBe(gl, gl.INVALID_VALUE, "width out of bounds: should generate INVALID_VALUE."
                + " level is 0, size is " + (t.maxSize + test.sizeStep) + "x" + (test.sizeStep));

            // only height out of bounds
            var narrowAndTallDataSize = test.func(test.sizeStep, t.maxSize + test.sizeStep);
            var pixelsNegativeTest1 = new test.dataType(sharedArrayBuffer, 0, narrowAndTallDataSize);
            gl.compressedTexImage2D(target, 0, test.format, test.sizeStep, t.maxSize + test.sizeStep, 0, pixelsNegativeTest1);
            wtu.glErrorShouldBe(gl, gl.INVALID_VALUE, "height out of bounds: should generate INVALID_VALUE."
                + " level is 0, size is " + (test.sizeStep) + "x" + (t.maxSize + test.sizeStep));
        }

        // both width and height out of the maximum bounds simultaneously
        var squareDataSize = test.func(t.maxSize + test.sizeStep, t.maxSize + test.sizeStep);
        // this check assumes that each element is 1 byte
        if (squareDataSize > sharedArrayBuffer.byteLength) {
          testPassed("Unable to test square texture larger than maximum size due to ArrayBuffer size limitations -- this is legal");
        } else {
          var pixelsNegativeTest1 = new test.dataType(sharedArrayBuffer, 0, squareDataSize);
          gl.compressedTexImage2D(target, 0, test.format, t.maxSize + test.sizeStep, t.maxSize + test.sizeStep, 0, pixelsNegativeTest1);
          wtu.glErrorShouldBe(gl, gl.INVALID_VALUE, "width and height out of bounds: should generate INVALID_VALUE."
              + " level is 0, size is " + (t.maxSize + test.sizeStep) + "x" + (t.maxSize + test.sizeStep));
        }

        // level out of bounds
        var pixelsNegativeTest2 = new test.dataType(sharedArrayBuffer, 0, test.func(256, 256));
        gl.compressedTexImage2D(target, numLevels, test.format, 256, 256, 0, pixelsNegativeTest2);
        wtu.glErrorShouldBe(gl, gl.INVALID_VALUE, "level out of bounds: should generate INVALID_VALUE."
            + " level is " + numLevels + ", size is 256x256");
        //width and height out of bounds for specified level
        gl.compressedTexImage2D(target, numLevels - 1, test.format, 256, 256, 0, pixelsNegativeTest2);
        wtu.glErrorShouldBe(gl, gl.INVALID_VALUE, "width or height out of bounds for specified level: should generate INVALID_VALUE."
            + " level is " + (numLevels - 1) + ", size is 256x256");
      }
    }
    else {
      testPassed("No " + test.extension + " extension support -- this is legal");
    }
  }

};