summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance2/textures/misc/tex-srgb-mipmap.html
blob: 9764987d8d439ecc138b5ec22c7a0594979a17cb (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
<!--
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 mipmap 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: 16px; height: 16px;"></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 sampler2D tex;
varying vec2 texCoord;
void main()
{
    gl_FragColor = texture2D(tex, texCoord);
}
</script>
<script>
"use strict";
var wtu = WebGLTestUtils;
var canvas = document.getElementById("example");
var gl = wtu.create3DContext(canvas, undefined, 2);

description("Test srgb emulation for generateMipmap.");
function generateMipmap()
{
    debug("Generate mipmaps for sRGB texture");

    wtu.setupUnitQuad(gl, 0, 1);
    var program = wtu.setupProgram(
        gl, ['vshader', 'fshader'], ['vPosition', 'texCoord0'], [0, 1]);

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

    var colors = {
        blank: [0, 0, 0, 0],
        srgba: [0, 63, 127, 255],
    };

    var texLoc = gl.getUniformLocation(program, "tex");
    gl.uniform1i(texLoc, 0);

    var width = 128;
    var height = 128;
    canvas.width = width;
    canvas.height = height;
    gl.viewport(0, 0, width, height);

    var srgbTex = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, srgbTex);
    // Set full texture as srgba color first.
    wtu.fillTexture(gl, srgbTex, width, height, colors['srgba'], 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.SRGB8_ALPHA8);
    // Set up-left region of the texture as red color.
    // In order to make sure bi-linear interpolation operates on different colors, red region
    // is 1 pixel smaller than a quarter of the full texture on each side.
    var redWidth = width / 2 - 1;
    var redHeight = height / 2 - 1;
    var buf = new Uint8Array(redWidth * redHeight * 4);
    for (var i = 0; i < redWidth * redHeight; i++) {
        buf[4 * i + 0] = 255;
        buf[4 * i + 1] = 0;
        buf[4 * i + 2] = 0;
        buf[4 * i + 3] = 255;
    }
    gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, redWidth, redHeight, gl.RGBA, gl.UNSIGNED_BYTE, buf);
    gl.generateMipmap(gl.TEXTURE_2D);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_NEAREST);

    // Decode the srgba texture to a linear texture which will be used as reference.
    var linearTex = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, linearTex);
    wtu.fillTexture(gl, linearTex, width, height, wtu.sRGBToLinear(colors['srgba']), 0, gl.RGBA, gl.UNSIGNED_BYTE);
    // Set up-left region of the texture as red color.
    // In order to make sure bi-linear interpolation operates on different colors, red region
    // is 1 pixel smaller than a quarter of the full texture on each side.
    for (var i = 0; i < redWidth * redHeight; i++) {
        buf[4 * i + 0] = 255;
        buf[4 * i + 1] = 0;
        buf[4 * i + 2] = 0;
        buf[4 * i + 3] = 255;
    }
    gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, redWidth, redHeight, gl.RGBA, gl.UNSIGNED_BYTE, buf);
    gl.generateMipmap(gl.TEXTURE_2D);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_NEAREST);

    // Change canvas to a small size.
    width = 64;
    height = 64;
    canvas.width = width;
    canvas.height = height;
    gl.viewport(0, 0, width, height);

    // Draw with srgb texture and linear texture respectively.
    gl.bindTexture(gl.TEXTURE_2D, srgbTex);
    wtu.clearAndDrawUnitQuad(gl);
    var result = new Uint8Array(width * height * 4);
    gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, result);
    gl.bindTexture(gl.TEXTURE_2D, linearTex);
    wtu.clearAndDrawUnitQuad(gl);
    var reference = new Uint8Array(width * height * 4);
    gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, reference);

    gl.deleteTexture(srgbTex);
    gl.deleteTexture(linearTex);

    var tolerance = 7;
    var diff = new Uint8Array(width * height * 4);
    var failed = wtu.comparePixels(result, reference, tolerance, diff);
    if (failed) {
        testFailed("Generate wrong mipmaps for sRGB texture.");
        wtu.displayImageDiff(result, reference, diff, width, height);
    } else {
        testPassed("Generate correct mipmaps for sRGB texture.");
    }
}

function generateMipmap_immutableTexture()
{
    debug("Generate mipmaps for immutable texture.");
    var tex = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, tex);
    gl.texStorage2D(gl.TEXTURE_2D, Math.log2(canvas.width), gl.SRGB8_ALPHA8, canvas.width, canvas.height);
    gl.generateMipmap(gl.TEXTURE_2D);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "GenerateMipmap should succeed.");

    gl.deleteTexture(tex);
}

function generateMipmap_widthHeightNotEqual()
{
    debug("Generate mipmaps when width and height are not equal.");
    var tex = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, tex);
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.SRGB8_ALPHA8, 64, 32, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
    gl.generateMipmap(gl.TEXTURE_2D);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "GenerateMipmap should succeed.");

    gl.deleteTexture(tex);
}

function generateMipmap_maxLevelLessThanFullMipmapLevel()
{
    debug("Generate mipmaps when maxLevel is less than full mipmap level.");

    wtu.setupUnitQuad(gl, 0, 1);
    var program = wtu.setupProgram(
        gl, ['vshader', 'fshader'], ['vPosition', 'texCoord0'], [0, 1]);

    var colors = [0, 63, 127, 255];

    var texLoc = gl.getUniformLocation(program, "tex");
    gl.uniform1i(texLoc, 0);

    var width = 16;
    var height = 16;
    canvas.width = width;
    canvas.height = height;
    gl.viewport(0, 0, width, height);

    var srgbTex = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, srgbTex);
    wtu.fillTexture(gl, srgbTex, width, height, colors, 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.SRGB8_ALPHA8);

    // Set max level, check if the max level mipmap is generated.
    var max_level = 3;
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAX_LEVEL, max_level);
    gl.generateMipmap(gl.TEXTURE_2D);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_NEAREST);

    width >>= max_level;
    height >>= max_level;
    canvas.width = width;
    canvas.height = height;
    gl.viewport(0, 0, width, height);

    gl.bindTexture(gl.TEXTURE_2D, srgbTex);
    wtu.clearAndDrawUnitQuad(gl);

    var reference = wtu.sRGBToLinear(colors);
    var msg;
    wtu.checkCanvasRect(gl, 0, 0, width, height, reference, msg, [1,1,1,1]);

    gl.deleteTexture(srgbTex);
}

generateMipmap();
generateMipmap_immutableTexture();
generateMipmap_widthHeightNotEqual();
generateMipmap_maxLevelLessThanFullMipmapLevel();

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

</body>
</html>