summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance2/extensions/webgl-provoking-vertex.html
blob: 3737409b3a9632992939483809a9704cfea4c531 (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
<!--
Copyright (c) 2022 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 WEBGL_provoking_vertex 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="16" height="16" id="c"></canvas>
<div id="description"></div>
<div id="console"></div>
<script>
"use strict";
description("This test verifies the functionality of the WEBGL_provoking_vertex extension, if it is available.");

debug("");

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

function runTestNoExtension() {
    debug("");
    debug("Check getParameter without the extension");
    shouldBeNull("gl.getParameter(0x8E4F /* PROVOKING_VERTEX_WEBGL */)");
    wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "parameter unknown without enabling the extension");
    debug("");
}

function runTestExtension() {
    debug("");
    debug("Check enums");
    shouldBe("ext.FIRST_VERTEX_CONVENTION_WEBGL", "0x8E4D");
    shouldBe("ext.LAST_VERTEX_CONVENTION_WEBGL", "0x8E4E");
    shouldBe("ext.PROVOKING_VERTEX_WEBGL", "0x8E4F");

    debug("");
    debug("Check default state");
    shouldBe("gl.getParameter(ext.PROVOKING_VERTEX_WEBGL)", "ext.LAST_VERTEX_CONVENTION_WEBGL");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "parameter known with the extension enabled");

    debug("");
    debug("Check state updates");
    ext.provokingVertexWEBGL(ext.FIRST_VERTEX_CONVENTION_WEBGL);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "provokingVertexWEBGL(ext.FIRST_VERTEX_CONVENTION_WEBGL) generates no errors");
    shouldBe("gl.getParameter(ext.PROVOKING_VERTEX_WEBGL)", "ext.FIRST_VERTEX_CONVENTION_WEBGL");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);
    ext.provokingVertexWEBGL(ext.LAST_VERTEX_CONVENTION_WEBGL);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "provokingVertexWEBGL(ext.LAST_VERTEX_CONVENTION_WEBGL) generates no errors");
    shouldBe("gl.getParameter(ext.PROVOKING_VERTEX_WEBGL)", "ext.LAST_VERTEX_CONVENTION_WEBGL");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);

    debug("");
    debug("Check invalid provoking vertex mode");
    ext.provokingVertexWEBGL(ext.FIRST_VERTEX_CONVENTION_WEBGL);
    ext.provokingVertexWEBGL(ext.PROVOKING_VERTEX_WEBGL);
    wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "invalid provoking mode generates an error");
    shouldBe("gl.getParameter(ext.PROVOKING_VERTEX_WEBGL)", "ext.FIRST_VERTEX_CONVENTION_WEBGL");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);

    debug("");
    debug("Check provoking vertex operation");

    const vs = `#version 300 es
      in int intAttrib;
      in vec2 position;
      flat out int attrib;
      void main() {
        gl_Position = vec4(position, 0, 1);
        attrib = intAttrib;
      }`;

    const fs = `#version 300 es
      flat in int attrib;
      out int fragColor;
      void main() {
        fragColor = attrib;
      }`;

    const program = wtu.setupProgram(gl, [vs, fs]);
    gl.useProgram(program);

    const tex = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, tex);
    gl.texStorage2D(gl.TEXTURE_2D, 1, gl.R32I, 16, 16);

    const fb = gl.createFramebuffer();
    gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
    gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0);

    const vb = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, vb);
    const buf = new ArrayBuffer(36);
    new Float32Array(buf, 0, 6).set([-1.0, -1.0, 3.0, -1.0, -1.0, 3.0]);
    new Int32Array(buf, 24, 3).set([1, 2, 3]);
    gl.bufferData(gl.ARRAY_BUFFER, buf, gl.STATIC_DRAW);

    const positionLocation = gl.getAttribLocation(program, "position");
    gl.enableVertexAttribArray(positionLocation);
    gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);

    const intAttribLocation = gl.getAttribLocation(program, "intAttrib");
    gl.enableVertexAttribArray(intAttribLocation);
    gl.vertexAttribIPointer(intAttribLocation, 1, gl.INT, 0, 24);

    const pixel = new Int32Array(4);

    ext.provokingVertexWEBGL(ext.LAST_VERTEX_CONVENTION_WEBGL);
    gl.clearBufferiv(gl.COLOR, 0, new Int32Array(4));
    gl.drawArrays(gl.TRIANGLES, 0, 3);
    gl.readPixels(0, 0, 1, 1, gl.RGBA_INTEGER, gl.INT, pixel);

    if (pixel[0] == 3) {
        testPassed("Correct last provoking vertex");
    } else {
        testFailed("Incorrect last provoking vertex");
    }

    ext.provokingVertexWEBGL(ext.FIRST_VERTEX_CONVENTION_WEBGL);
    gl.clearBufferiv(gl.COLOR, 0, new Int32Array(4));
    gl.drawArrays(gl.TRIANGLES, 0, 3);
    gl.readPixels(0, 0, 1, 1, gl.RGBA_INTEGER, gl.INT, pixel);

    if (pixel[0] == 1) {
        testPassed("Correct first provoking vertex");
    } else {
        testFailed("Incorrect first provoking vertex");
    }
}

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

    runTestNoExtension();

    ext = gl.getExtension("WEBGL_provoking_vertex");

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

    if (ext !== null) {
      runTestExtension();
    } else {
      testPassed("No WEBGL_provoking_vertex support -- this is legal");
    }
  }
}

runTest();

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