summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/extensions/webgl-compressed-texture-pvrtc.html
blob: 465f44a9cdac10df13db230ea1274764a1d6320b (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
<!--
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">
<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>
<title>WebGL WEBGL_compressed_texture_pvrtc Conformance Tests</title>
<style>
img {
 border: 1px solid black;
 margin-right: 1em;
}
.testimages {
}

.testimages br {
  clear: both;
}

.testimages > div {
  float: left;
  margin: 1em;
}
</style>
</head>
<body>
<div id="description"></div>
<canvas id="canvas" width="8" height="8" style="width: 8px; height: 8px;"></canvas>
<div id="console"></div>
<script>
"use strict";
description("This test verifies the functionality of the WEBGL_compressed_texture_pvrtc extension, if it is available.");

debug("");

var pvrtc_4x4_2bpp = new Uint8Array([
  0x77, 0x22, 0x77, 0x22, 0xbb, 0x2b, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]);

var pvrtc_4x4_4bpp = new Uint8Array([
  0x1b, 0x1b, 0x1b, 0x1b, 0xba, 0x2b, 0x00, 0x80, 0x1b, 0x1b, 0x1b, 0x1b, 0xba, 0x2b, 0x00, 0x80,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]);

var pvrtc_4x4_rgba_decoded = new Uint8Array([
  0x00, 0x00, 0x00, 0xff, 0x46, 0x46, 0x46, 0xb8, 0x76, 0x76, 0x71, 0x8a, 0xbd, 0xbd, 0xba, 0x44,
  0x00, 0x00, 0x00, 0xff, 0x46, 0x46, 0x43, 0xb8, 0x76, 0x76, 0x71, 0x8a, 0xbd, 0xbd, 0xb5, 0x44,
  0x00, 0x00, 0x00, 0xff, 0x46, 0x46, 0x43, 0xb8, 0x76, 0x76, 0x71, 0x8a, 0xbd, 0xbd, 0xb5, 0x44,
  0x00, 0x00, 0x00, 0xff, 0x46, 0x46, 0x46, 0xb8, 0x76, 0x76, 0x71, 0x8a, 0xbd, 0xbd, 0xb7, 0x44,
]);

var pvrtc_4x4_rgb_decoded = new Uint8Array([
  0x00, 0x00, 0x00, 0xff, 0x46, 0x46, 0x46, 0xff, 0x76, 0x76, 0x71, 0xff, 0xbd, 0xbd, 0xba, 0xff,
  0x00, 0x00, 0x00, 0xff, 0x46, 0x46, 0x43, 0xff, 0x76, 0x76, 0x71, 0xff, 0xbd, 0xbd, 0xb5, 0xff,
  0x00, 0x00, 0x00, 0xff, 0x46, 0x46, 0x43, 0xff, 0x76, 0x76, 0x71, 0xff, 0xbd, 0xbd, 0xb5, 0xff,
  0x00, 0x00, 0x00, 0xff, 0x46, 0x46, 0x46, 0xff, 0x76, 0x76, 0x71, 0xff, 0xbd, 0xbd, 0xb7, 0xff,
]);

var wtu = WebGLTestUtils;
var contextVersion = wtu.getDefault3DContextVersion();
var canvas = document.getElementById("canvas");
var gl = wtu.create3DContext(canvas, {antialias: false});
var program = wtu.setupTexturedQuad(gl);
var ext = null;
var vao = null;
var validFormats = {
    COMPRESSED_RGB_PVRTC_4BPPV1_IMG      : 0x8C00,
    COMPRESSED_RGB_PVRTC_2BPPV1_IMG      : 0x8C01,
    COMPRESSED_RGBA_PVRTC_4BPPV1_IMG     : 0x8C02,
    COMPRESSED_RGBA_PVRTC_2BPPV1_IMG     : 0x8C03,
};
var name;
var supportedFormats;

if (!gl) {
    testFailed("WebGL context does not exist");
} else {
    testPassed("WebGL context exists");

    // Run tests with extension disabled
    runTestDisabled();

    // Query the extension and store globally so shouldBe can access it
    ext = wtu.getExtensionWithKnownPrefixes(gl, "WEBGL_compressed_texture_pvrtc");
    if (!ext) {
        testPassed("No WEBGL_compressed_texture_pvrtc support -- this is legal");
        runSupportedTest(false);
    } else {
        testPassed("Successfully enabled WEBGL_compressed_texture_pvrtc extension");

        runSupportedTest(true);
        runTestExtension();
    }
}

function runSupportedTest(extensionEnabled) {
    var name = wtu.getSupportedExtensionWithKnownPrefixes(gl, "WEBGL_compressed_texture_pvrtc");
    if (name !== undefined) {
        if (extensionEnabled) {
            testPassed("WEBGL_compressed_texture_pvrtc listed as supported and getExtension succeeded");
        } else {
            testFailed("WEBGL_compressed_texture_pvrtc listed as supported but getExtension failed");
        }
    } else {
        if (extensionEnabled) {
            testFailed("WEBGL_compressed_texture_pvrtc not listed as supported but getExtension succeeded");
        } else {
            testPassed("WEBGL_compressed_texture_pvrtc not listed as supported and getExtension failed -- this is legal");
        }
    }
}


function runTestDisabled() {
    debug("Testing binding enum with extension disabled");

    supportedFormats = gl.getParameter(gl.COMPRESSED_TEXTURE_FORMATS);
    shouldBe("supportedFormats", "[]");
}

function formatExists(format, supportedFormats) {
    for (var ii = 0; ii < supportedFormats.length; ++ii) {
        if (format == supportedFormats[ii]) {
            testPassed("supported format " + formatToString(format) + " is exists");
            return;
        }
    }
    testFailed("supported format " + formatToString(format) + " does not exist");
}

function formatToString(format) {
    for (var p in ext) {
        if (ext[p] == format) {
            return p;
        }
    }
    return "0x" + format.toString(16);
}

function runTestExtension() {
    debug("Testing WEBGL_compressed_texture_pvrtc");

    // check that all format enums exist.
    for (name in validFormats) {
        var expected = "0x" + validFormats[name].toString(16);
        var actual = "ext['" + name + "']";
        shouldBe(actual, expected);
    }

    supportedFormats = gl.getParameter(gl.COMPRESSED_TEXTURE_FORMATS);
    // There should be exactly 4 formats for both WebGL 1.0 and WebGL 2.0.
    shouldBe("supportedFormats.length", "4");

    // check that all 4 formats exist
    for (var name in validFormats.length) {
        formatExists(validFormats[name], supportedFormats);
    }

    // Test each format
    testPVRTC_RGBA_2BPP();
    testPVRTC_RGB_2BPP();
    testPVRTC_RGBA_4BPP();
    testPVRTC_RGB_4BPP();
}

function testPVRTC_RGBA_2BPP() {
    var tests = [
        {   width: 4,
            height: 4,
            channels: 4,
            data: pvrtc_4x4_2bpp,
            raw: pvrtc_4x4_rgba_decoded,
            format: ext.COMPRESSED_RGBA_PVRTC_2BPPV1_IMG
        }
    ];
    testPVRTCTextures(tests);
}

function testPVRTC_RGB_2BPP() {
    var tests = [
        {   width: 4,
            height: 4,
            channels: 4,
            data: pvrtc_4x4_2bpp,
            raw: pvrtc_4x4_rgb_decoded,
            format: ext.COMPRESSED_RGB_PVRTC_2BPPV1_IMG
        }
    ];
    testPVRTCTextures(tests);
}

function testPVRTC_RGBA_4BPP() {
    var tests = [
        {   width: 4,
            height: 4,
            channels: 4,
            data: pvrtc_4x4_4bpp,
            raw: pvrtc_4x4_rgba_decoded,
            format: ext.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG
        }
    ];
    testPVRTCTextures(tests);
}

function testPVRTC_RGB_4BPP() {
    var tests = [
        {   width: 4,
            height: 4,
            channels: 4,
            data: pvrtc_4x4_4bpp,
            raw: pvrtc_4x4_rgb_decoded,
            format: ext.COMPRESSED_RGB_PVRTC_4BPPV1_IMG
        }
    ];
    testPVRTCTextures(tests);
}

function testPVRTCTextures(tests) {
    debug("<hr/>");
    for (var ii = 0; ii < tests.length; ++ii) {
        testPVRTCTexture(tests[ii]);
    }
}

function testPVRTCTexture(test) {
    var data = new Uint8Array(test.data);
    var width = test.width;
    var height = test.height;
    var format = test.format;
    var uncompressedData = test.raw;

    canvas.width = width;
    canvas.height = height;
    gl.viewport(0, 0, width, height);
    debug("testing " + formatToString(format) + " " + width + "x" + height);

    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);
    gl.compressedTexImage2D(gl.TEXTURE_2D, 0, format, width, height, 0, data);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "uploading compressed texture");
    gl.generateMipmap(gl.TEXTURE_2D);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "trying to generate mipmaps from compressed texture");
    wtu.clearAndDrawUnitQuad(gl);
    compareRect(width, height, test.channels, width, height, uncompressedData, data, format, undefined, "NEAREST");
    // Test again with linear filtering.
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
    wtu.clearAndDrawUnitQuad(gl);
    compareRect(width, height, test.channels, width, height, uncompressedData, data, format, undefined, "LINEAR");

    gl.compressedTexImage2D(gl.TEXTURE_2D, 0, format, width, height, 1, data);
    wtu.glErrorShouldBe(gl, gl.INVALID_VALUE, "non 0 border");

    gl.compressedTexImage2D(gl.TEXTURE_2D, 0, format, width - 1, height, 0, data);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "invalid dimensions");
    gl.compressedTexImage2D(gl.TEXTURE_2D, 0, format, width - 2, height, 0, data);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "invalid dimensions");
    gl.compressedTexImage2D(gl.TEXTURE_2D, 0, format, width, height - 1, 0, data);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "invalid dimensions");
    gl.compressedTexImage2D(gl.TEXTURE_2D, 0, format, width, height - 2, 0, data);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "invalid dimensions");

    gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, width, height, format, data);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "compressedTexSubImage2D allowed for reloading of complete textures");

    gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, width - 2, height, format, data);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "compressedTexSubImage2D not allowed for partial texture updates");
    gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, width, height - 2, format, data);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "compressedTexSubImage2D not allowed for partial texture updates");
    gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 2, 0, width - 2, height, format, data);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "compressedTexSubImage2D not allowed for partial texture updates");
    gl.compressedTexSubImage2D(gl.TEXTURE_2D, 0, 0, 2, width, height - 2, format, data);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "compressedTexSubImage2D not allowed for partial texture updates");
}

function insertImg(element, caption, img) {
    var div = document.createElement("div");
    div.appendChild(img);
    var label = document.createElement("div");
    label.appendChild(document.createTextNode(caption));
    div.appendChild(label);
    element.appendChild(div);
}

function makeImage(imageWidth, imageHeight, dataWidth, data, alpha) {
    var scale = 8;
    var c = document.createElement("canvas");
    c.width = imageWidth * scale;
    c.height = imageHeight * scale;
    var ctx = c.getContext("2d");
    for (var yy = 0; yy < imageHeight; ++yy) {
        for (var xx = 0; xx < imageWidth; ++xx) {
            var offset = (yy * dataWidth + xx) * 4;
            ctx.fillStyle = "rgba(" +
                    data[offset + 0] + "," +
                    data[offset + 1] + "," +
                    data[offset + 2] + "," +
                    (alpha ? data[offset + 3] / 255 : 1) + ")";
            ctx.fillRect(xx * scale, yy * scale, scale, scale);
        }
    }
    return wtu.makeImageFromCanvas(c);
}
function compareRect(
        actualWidth, actualHeight, actualChannels,
        dataWidth, dataHeight, expectedData,
        testData, testFormat, tolerance, filteringMode) {
    if(typeof(tolerance) == 'undefined') { tolerance = 5; }
    var actual = new Uint8Array(actualWidth * actualHeight * 4);
    gl.readPixels(
            0, 0, actualWidth, actualHeight, gl.RGBA, gl.UNSIGNED_BYTE, actual);

    var div = document.createElement("div");
    div.className = "testimages";
    insertImg(div, "expected", makeImage(
            actualWidth, actualHeight, dataWidth, expectedData,
            actualChannels == 4));
    insertImg(div, "actual", makeImage(
            actualWidth, actualHeight, actualWidth, actual,
            actualChannels == 4));
    div.appendChild(document.createElement('br'));
    document.getElementById("console").appendChild(div);

    var failed = false;
    for (var yy = 0; yy < actualHeight; ++yy) {
        for (var xx = 0; xx < actualWidth; ++xx) {
            var actualOffset = (yy * actualWidth + xx) * 4;
            var expectedOffset = (yy * dataWidth + xx) * 4;
            var expected = [
                    expectedData[expectedOffset + 0],
                    expectedData[expectedOffset + 1],
                    expectedData[expectedOffset + 2],
                    (actualChannels == 3 ? 255 : expectedData[expectedOffset + 3])
            ];
            for (var jj = 0; jj < 4; ++jj) {
                if (Math.abs(actual[actualOffset + jj] - expected[jj]) > tolerance) {
                    failed = true;
                    var was = actual[actualOffset + 0].toString();
                    for (var j = 1; j < 4; ++j) {
                        was += "," + actual[actualOffset + j];
                    }
                    testFailed('at (' + xx + ', ' + yy +
                                         ') expected: ' + expected + ' was ' + was);
                }
            }
        }
    }
    if (!failed) {
        testPassed("texture rendered correctly with " + filteringMode + " filtering");
    }
}

debug("");
var successfullyParsed = true;
</script>
<script src="../../js/js-test-post.js"></script>

</body>
</html>