summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance2/extensions/nv-shader-noperspective-interpolation.html
blob: 2198c17b1a4e31d9a9960b02d009dc27a9f8dd62 (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
<!--
Copyright (c) 2023 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 NV_shader_noperspective_interpolation Conformance Tests</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 width="128" height="128" id="c"></canvas>
<div id="description"></div>
<div id="console"></div>
<script>
"use strict";
description("This test verifies the functionality of the NV_shader_noperspective_interpolation extension, if it is available.");

debug("");

var wtu = WebGLTestUtils;
var gl = wtu.create3DContext("c", null, 2);
var ext;

function runShaderTests(extensionEnabled) {
    debug("");
    debug("Testing various shader compiles with extension " + (extensionEnabled ? "enabled" : "disabled"));

    const macroVertex = `#version 300 es
        in vec4 vPosition;
        void main() {
        #ifdef GL_NV_shader_noperspective_interpolation
            gl_Position = vPosition;
        #else
            #error no GL_NV_shader_noperspective_interpolation;
        #endif
        }`;

    const macroFragment = `#version 300 es
        precision highp float;
        out vec4 my_FragColor;
        void main() {
        #ifdef GL_NV_shader_noperspective_interpolation
            my_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
        #else
            #error no GL_NV_shader_noperspective_interpolation;
        #endif
        }`;

    for (const shaders of [[wtu.simpleVertexShaderESSL300, macroFragment],
                           [macroVertex, wtu.simpleColorFragmentShaderESSL300]]) {
        // Expect the macro shader to succeed ONLY if enabled
        if (wtu.setupProgram(gl, shaders)) {
            if (extensionEnabled) {
                testPassed("Macro defined in shaders when extension is enabled");
            } else {
                testFailed("Macro defined in shaders when extension is disabled");
            }
        } else {
            if (extensionEnabled) {
                testFailed("Macro not defined in shaders when extension is enabled");
            } else {
                testPassed("Macro not defined in shaders when extension is disabled");
            }
        }
    }

    const missingVertex = `#version 300 es
        noperspective out float interpolant;
        in vec4 vPosition;
        void main() {
            gl_Position = vPosition;
        }`;

    const missingFragment = `#version 300 es
        precision highp float;
        noperspective in float interpolant;
        out vec4 my_FragColor;
        void main() {
            my_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
        }`;

    // Always expect the shader missing the #extension pragma to fail (whether enabled or not)
    for (const shaders of [[missingVertex, wtu.simpleColorFragmentShaderESSL300],
                           [wtu.simpleVertexShaderESSL300, missingFragment],
                           [missingVertex, missingFragment]]) {
        if (wtu.setupProgram(gl, shaders)) {
            testFailed("Noperspective interpolation qualifier allowed without #extension pragma");
        } else {
            testPassed("Noperspective interpolation qualifier disallowed without #extension pragma");
        }
    }

    const validVertex = `#version 300 es
        #extension GL_NV_shader_noperspective_interpolation : enable
        noperspective out float interpolant;
        in vec4 vPosition;
        void main() {
            gl_Position = vPosition;
        }`;

    const validFragment = `#version 300 es
        #extension GL_NV_shader_noperspective_interpolation : enable
        precision highp float;
        noperspective in float interpolant;
        out vec4 my_FragColor;
        void main() {
            my_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
        }`;

    // Try to compile a shader using a noperspective qualifier that should only succeed if enabled
    if (wtu.setupProgram(gl, [validVertex, validFragment])) {
        if (extensionEnabled) {
            testPassed("Noperspective interpolation qualifier compiled successfully when extension enabled");
        } else {
            testFailed("Noperspective interpolation qualifier compiled successfully when extension disabled");
        }
    } else {
        if (extensionEnabled) {
            testFailed("Noperspective interpolation qualifier failed to compile when extension enabled");
        } else {
            testPassed("Noperspective interpolation qualifier failed to compile when extension disabled");
        }
    }

    debug("");
}

function runInterpolationTest() {
    function draw(program, skew) {
        gl.useProgram(program);

        const posLoc = gl.getAttribLocation(program, "position");
        const colLoc = gl.getAttribLocation(program, "color");

        const buf = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, buf);
        gl.bufferData(
            gl.ARRAY_BUFFER,
            new Float32Array([
                -1.0, -1.0,        0.0, 1.0,
                +1.0, -1.0,        0.0, 1.0,
                 0.0, +1.0 * skew, 0.0, skew,

                 1.0, 0.0, 0.0, 1.0,
                 0.0, 1.0, 0.0, 1.0,
                 0.0, 0.0, 1.0, 1.0]),
            gl.STATIC_DRAW);

        gl.vertexAttribPointer(posLoc, 4, gl.FLOAT, false, 0, 0);
        gl.vertexAttribPointer(colLoc, 4, gl.FLOAT, false, 0, 48);
        gl.enableVertexAttribArray(posLoc);
        gl.enableVertexAttribArray(colLoc);
        gl.drawArrays(gl.TRIANGLES, 0, 3);
    }

    const vertexSmooth = `#version 300 es
        in vec4 position;
        in vec4 color;
        smooth out vec4 interp_color;
        void main() {
            gl_Position = position;
            interp_color = color;
        }`;

    const fragmentSmooth = `#version 300 es
        precision highp float;
        smooth in vec4 interp_color;
        out vec4 fragColor;
        void main() {
            fragColor = interp_color;
        }`;
    const programSmooth = wtu.setupProgram(gl, [vertexSmooth, fragmentSmooth]);

    debug("Get non-skewed value with smooth interpolation");
    gl.clearColor(0.0, 0.0, 0.0, 1.0);
    gl.clear(gl.COLOR_BUFFER_BIT);
    draw(programSmooth, 1.0);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");

    const smoothColor = new Uint8Array(4);
    gl.readPixels(64, 64, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, smoothColor);

    const vertexNoperspective = `#version 300 es
        #extension GL_NV_shader_noperspective_interpolation : require
        in vec4 position;
        in vec4 color;
        noperspective out vec4 interp_color;
        void main() {
            gl_Position = position;
            interp_color = color;
        }`;

    const fragmentNoperspective = `#version 300 es
        #extension GL_NV_shader_noperspective_interpolation : require
        precision highp float;
        noperspective in vec4 interp_color;
        out vec4 fragColor;
        void main() {
            fragColor = interp_color;
        }`;
    const programNoperspective = wtu.setupProgram(gl, [vertexNoperspective, fragmentNoperspective]);

    debug("");
    debug("Check non-skewed value with noperspective interpolation");
    gl.clear(gl.COLOR_BUFFER_BIT);
    draw(programNoperspective, 1.0);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
    wtu.checkCanvasRect(gl, 64, 64, 1, 1, smoothColor, "Non-skewed noperspective should match smooth");

    debug("");
    debug("Check skewed value with noperspective interpolation");
    gl.clear(gl.COLOR_BUFFER_BIT);
    draw(programNoperspective, 2.0);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
    wtu.checkCanvasRect(gl, 64, 64, 1, 1, smoothColor, "Skewed noperspective should match smooth");
}

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

    runShaderTests(false);

    ext = gl.getExtension("NV_shader_noperspective_interpolation");
    wtu.runExtensionSupportedTest(gl, "NV_shader_noperspective_interpolation", ext !== null);

    if (!ext) {
        testPassed("No NV_shader_noperspective_interpolation support -- this is legal");
    } else {
        testPassed("Successfully enabled NV_shader_noperspective_interpolation extension");
        runShaderTests(true);
        runInterpolationTest();
    }
}

runTest();

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