webgl-provoking-vertex.html (5496B)
1 <!-- 2 Copyright (c) 2022 The Khronos Group Inc. 3 Use of this source code is governed by an MIT-style license that can be 4 found in the LICENSE.txt file. 5 --> 6 7 <!DOCTYPE html> 8 <html> 9 <head> 10 <meta charset="utf-8"> 11 <title>WebGL WEBGL_provoking_vertex Conformance Tests</title> 12 <LINK rel="stylesheet" href="../../resources/js-test-style.css"/> 13 <script src="../../js/js-test-pre.js"></script> 14 <script src="../../js/webgl-test-utils.js"></script> 15 </head> 16 <body> 17 <canvas width="16" height="16" id="c"></canvas> 18 <div id="description"></div> 19 <div id="console"></div> 20 <script> 21 "use strict"; 22 description("This test verifies the functionality of the WEBGL_provoking_vertex extension, if it is available."); 23 24 debug(""); 25 26 var wtu = WebGLTestUtils; 27 var gl = wtu.create3DContext("c", null, 2); 28 var ext; 29 30 function runTestNoExtension() { 31 debug(""); 32 debug("Check getParameter without the extension"); 33 shouldBeNull("gl.getParameter(0x8E4F /* PROVOKING_VERTEX_WEBGL */)"); 34 wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "parameter unknown without enabling the extension"); 35 debug(""); 36 } 37 38 function runTestExtension() { 39 debug(""); 40 debug("Check enums"); 41 shouldBe("ext.FIRST_VERTEX_CONVENTION_WEBGL", "0x8E4D"); 42 shouldBe("ext.LAST_VERTEX_CONVENTION_WEBGL", "0x8E4E"); 43 shouldBe("ext.PROVOKING_VERTEX_WEBGL", "0x8E4F"); 44 45 debug(""); 46 debug("Check default state"); 47 shouldBe("gl.getParameter(ext.PROVOKING_VERTEX_WEBGL)", "ext.LAST_VERTEX_CONVENTION_WEBGL"); 48 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "parameter known with the extension enabled"); 49 50 debug(""); 51 debug("Check state updates"); 52 ext.provokingVertexWEBGL(ext.FIRST_VERTEX_CONVENTION_WEBGL); 53 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "provokingVertexWEBGL(ext.FIRST_VERTEX_CONVENTION_WEBGL) generates no errors"); 54 shouldBe("gl.getParameter(ext.PROVOKING_VERTEX_WEBGL)", "ext.FIRST_VERTEX_CONVENTION_WEBGL"); 55 wtu.glErrorShouldBe(gl, gl.NO_ERROR); 56 ext.provokingVertexWEBGL(ext.LAST_VERTEX_CONVENTION_WEBGL); 57 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "provokingVertexWEBGL(ext.LAST_VERTEX_CONVENTION_WEBGL) generates no errors"); 58 shouldBe("gl.getParameter(ext.PROVOKING_VERTEX_WEBGL)", "ext.LAST_VERTEX_CONVENTION_WEBGL"); 59 wtu.glErrorShouldBe(gl, gl.NO_ERROR); 60 61 debug(""); 62 debug("Check invalid provoking vertex mode"); 63 ext.provokingVertexWEBGL(ext.FIRST_VERTEX_CONVENTION_WEBGL); 64 ext.provokingVertexWEBGL(ext.PROVOKING_VERTEX_WEBGL); 65 wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "invalid provoking mode generates an error"); 66 shouldBe("gl.getParameter(ext.PROVOKING_VERTEX_WEBGL)", "ext.FIRST_VERTEX_CONVENTION_WEBGL"); 67 wtu.glErrorShouldBe(gl, gl.NO_ERROR); 68 69 debug(""); 70 debug("Check provoking vertex operation"); 71 72 const vs = `#version 300 es 73 in int intAttrib; 74 in vec2 position; 75 flat out int attrib; 76 void main() { 77 gl_Position = vec4(position, 0, 1); 78 attrib = intAttrib; 79 }`; 80 81 const fs = `#version 300 es 82 flat in int attrib; 83 out int fragColor; 84 void main() { 85 fragColor = attrib; 86 }`; 87 88 const program = wtu.setupProgram(gl, [vs, fs]); 89 gl.useProgram(program); 90 91 const tex = gl.createTexture(); 92 gl.bindTexture(gl.TEXTURE_2D, tex); 93 gl.texStorage2D(gl.TEXTURE_2D, 1, gl.R32I, 16, 16); 94 95 const fb = gl.createFramebuffer(); 96 gl.bindFramebuffer(gl.FRAMEBUFFER, fb); 97 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0); 98 99 const vb = gl.createBuffer(); 100 gl.bindBuffer(gl.ARRAY_BUFFER, vb); 101 const buf = new ArrayBuffer(36); 102 new Float32Array(buf, 0, 6).set([-1.0, -1.0, 3.0, -1.0, -1.0, 3.0]); 103 new Int32Array(buf, 24, 3).set([1, 2, 3]); 104 gl.bufferData(gl.ARRAY_BUFFER, buf, gl.STATIC_DRAW); 105 106 const positionLocation = gl.getAttribLocation(program, "position"); 107 gl.enableVertexAttribArray(positionLocation); 108 gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0); 109 110 const intAttribLocation = gl.getAttribLocation(program, "intAttrib"); 111 gl.enableVertexAttribArray(intAttribLocation); 112 gl.vertexAttribIPointer(intAttribLocation, 1, gl.INT, 0, 24); 113 114 const pixel = new Int32Array(4); 115 116 ext.provokingVertexWEBGL(ext.LAST_VERTEX_CONVENTION_WEBGL); 117 gl.clearBufferiv(gl.COLOR, 0, new Int32Array(4)); 118 gl.drawArrays(gl.TRIANGLES, 0, 3); 119 gl.readPixels(0, 0, 1, 1, gl.RGBA_INTEGER, gl.INT, pixel); 120 121 if (pixel[0] == 3) { 122 testPassed("Correct last provoking vertex"); 123 } else { 124 testFailed("Incorrect last provoking vertex"); 125 } 126 127 ext.provokingVertexWEBGL(ext.FIRST_VERTEX_CONVENTION_WEBGL); 128 gl.clearBufferiv(gl.COLOR, 0, new Int32Array(4)); 129 gl.drawArrays(gl.TRIANGLES, 0, 3); 130 gl.readPixels(0, 0, 1, 1, gl.RGBA_INTEGER, gl.INT, pixel); 131 132 if (pixel[0] == 1) { 133 testPassed("Correct first provoking vertex"); 134 } else { 135 testFailed("Incorrect first provoking vertex"); 136 } 137 } 138 139 function runTest() { 140 if (!gl) { 141 testFailed("context does not exist"); 142 } else { 143 testPassed("context exists"); 144 145 runTestNoExtension(); 146 147 ext = gl.getExtension("WEBGL_provoking_vertex"); 148 149 wtu.runExtensionSupportedTest(gl, "WEBGL_provoking_vertex", ext !== null); 150 151 if (ext !== null) { 152 runTestExtension(); 153 } else { 154 testPassed("No WEBGL_provoking_vertex support -- this is legal"); 155 } 156 } 157 } 158 159 runTest(); 160 161 var successfullyParsed = true; 162 </script> 163 <script src="../../js/js-test-post.js"></script> 164 </body> 165 </html>