From 45094c8e3814ba2ecbb3ba5e1861e3c2abd10575 Mon Sep 17 00:00:00 2001 From: Peter Boyer Date: Tue, 17 Nov 2026 22:04:34 -0500 Subject: [PATCH] Support WEBGL_provoking_vertex getExtension('WEBGL_provoking_vertex') returned null, so flat varyings always came from the last vertex. headless-gl now offers it on WebGL2 contexts using ANGLE's GL_ANGLE_provoking_vertex, as Chrome does, and provokingVertexWEBGL(FIRST_VERTEX_CONVENTION_WEBGL) switches to the first vertex. getParameter(PROVOKING_VERTEX_WEBGL) returns the convention once the extension is enabled. The first vertex convention matters beyond correctness: under the last vertex convention, ANGLE's Metal backend rewrites the index buffer of each indexed draw whose program has flat varyings, and keeps the copies. index.d.ts declares the extension, since TypeScript's DOM types don't. test/webgl-provoking-vertex.js checks that WebGL1 doesn't offer it, that getParameter is rejected before it is enabled, and that a flat-shaded triangle takes its last vertex's color, then its first's once set, then its last's again. Co-Authored-By: Claude Opus 5.5 --- README.md | 1 + index.d.ts | 8 ++ .../extensions/webgl-provoking-vertex.js | 29 ++++++ src/javascript/webgl-rendering-context.js | 4 +- src/native/bindings.cc | 1 + src/native/webgl.cc | 7 ++ src/native/webgl.h | 1 + test/types/node.mts | 2 + test/webgl-provoking-vertex.js | 96 +++++++++++++++++++ 9 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 src/javascript/extensions/webgl-provoking-vertex.js create mode 100644 test/webgl-provoking-vertex.js diff --git a/README.md b/README.md index e1af5b36..54cf53df 100644 --- a/README.md +++ b/README.md @@ -288,6 +288,7 @@ Supported extensions by WebGL version. "Built in" means WebGL2 has the feature i | [`WEBGL_compressed_texture_s3tc_srgb`](https://www.khronos.org/registry/webgl/extensions/WEBGL_compressed_texture_s3tc_srgb/) | Yes | Yes | | [`WEBGL_debug_renderer_info`](https://www.khronos.org/registry/webgl/extensions/WEBGL_debug_renderer_info/) | Yes | Yes | | [`EXT_color_buffer_float`](https://www.khronos.org/registry/webgl/extensions/EXT_color_buffer_float/) | No | Yes | +| [`WEBGL_provoking_vertex`](https://www.khronos.org/registry/webgl/extensions/WEBGL_provoking_vertex/) | No | Yes | | [`WEBGL_color_buffer_float`](https://www.khronos.org/registry/webgl/extensions/WEBGL_color_buffer_float/) | Yes | Use `EXT_color_buffer_float` | | [`ANGLE_instanced_arrays`](https://www.khronos.org/registry/webgl/extensions/ANGLE_instanced_arrays/) | Yes | Built in | | [`EXT_blend_minmax`](https://www.khronos.org/registry/webgl/extensions/EXT_blend_minmax/) | Yes | Built in | diff --git a/index.d.ts b/index.d.ts index 5682978a..85752550 100644 --- a/index.d.ts +++ b/index.d.ts @@ -119,12 +119,20 @@ declare namespace createContext { readonly MIRROR_CLAMP_TO_EDGE_EXT: 0x8743; } + interface WEBGL_provoking_vertex { + readonly FIRST_VERTEX_CONVENTION_WEBGL: 0x8E4D; + readonly LAST_VERTEX_CONVENTION_WEBGL: 0x8E4E; + readonly PROVOKING_VERTEX_WEBGL: 0x8E4F; + provokingVertexWEBGL(provokeMode: GLenum): void; + } + /** * getExtension for the extensions headless-gl supports that TypeScript's DOM types don't declare yet. */ interface SupplementalExtensions { getExtension(extensionName: "EXT_depth_clamp"): EXT_depth_clamp | null; getExtension(extensionName: "EXT_texture_mirror_clamp_to_edge"): EXT_texture_mirror_clamp_to_edge | null; + getExtension(extensionName: "WEBGL_provoking_vertex"): WEBGL_provoking_vertex | null; } /** diff --git a/src/javascript/extensions/webgl-provoking-vertex.js b/src/javascript/extensions/webgl-provoking-vertex.js new file mode 100644 index 00000000..8564173b --- /dev/null +++ b/src/javascript/extensions/webgl-provoking-vertex.js @@ -0,0 +1,29 @@ +const { gl } = require('../native-gl') + +class WebGLProvokingVertex { + constructor (ctx) { + this.FIRST_VERTEX_CONVENTION_WEBGL = 0x8E4D + this.LAST_VERTEX_CONVENTION_WEBGL = 0x8E4E + this.PROVOKING_VERTEX_WEBGL = 0x8E4F + + this._ctx = ctx + } + + // ANGLE's GL_ANGLE_provoking_vertex sets INVALID_ENUM for any other mode, as the WebGL extension does. + provokingVertexWEBGL (provokeMode) { + gl.provokingVertexANGLE.call(this._ctx, provokeMode | 0) + } +} + +function getWebGLProvokingVertex (context) { + let result = null + const exts = context.getSupportedExtensions() + + if (exts && exts.indexOf('WEBGL_provoking_vertex') >= 0) { + result = new WebGLProvokingVertex(context) + } + + return result +} + +module.exports = { getWebGLProvokingVertex, WebGLProvokingVertex } diff --git a/src/javascript/webgl-rendering-context.js b/src/javascript/webgl-rendering-context.js index 52534a1c..8f0074dd 100644 --- a/src/javascript/webgl-rendering-context.js +++ b/src/javascript/webgl-rendering-context.js @@ -60,6 +60,7 @@ const { getWebGLCompressedTextureS3TC } = require('./extensions/webgl-compressed const { getWebGLCompressedTextureS3TCsRGB } = require('./extensions/webgl-compressed-texture-s3tc-srgb') const { getEXTTextureCompressionRGTC } = require('./extensions/ext-texture-compression-rgtc') const { getEXTTextureCompressionBPTC } = require('./extensions/ext-texture-compression-bptc') +const { getWebGLProvokingVertex } = require('./extensions/webgl-provoking-vertex') // The longest uniform and attribute locations, from the WebGL 1.0 and 2.0 specs. const WEBGL1_MAX_LOCATION_LENGTH = 256 @@ -154,7 +155,8 @@ const availableExtensions = { webgl_compressed_texture_s3tc: getWebGLCompressedTextureS3TC, webgl_compressed_texture_s3tc_srgb: getWebGLCompressedTextureS3TCsRGB, ext_texture_compression_rgtc: getEXTTextureCompressionRGTC, - ext_texture_compression_bptc: getEXTTextureCompressionBPTC + ext_texture_compression_bptc: getEXTTextureCompressionBPTC, + webgl_provoking_vertex: getWebGLProvokingVertex } // Creates the object that createContext returns: it has the constants and spec methods for the context's WebGL version, bound to the native context. diff --git a/src/native/bindings.cc b/src/native/bindings.cc index 04ba3c7c..74392c50 100644 --- a/src/native/bindings.cc +++ b/src/native/bindings.cc @@ -164,6 +164,7 @@ NAN_MODULE_INIT(Init) { JS_GL_METHOD("deleteVertexArrayOES", DeleteVertexArrayOES); JS_GL_METHOD("isVertexArrayOES", IsVertexArrayOES); JS_GL_METHOD("bindVertexArrayOES", BindVertexArrayOES); + JS_GL_METHOD("provokingVertexANGLE", ProvokingVertexANGLE); // WebGL 2.0 functions: JS_GL_METHOD("copyBufferSubData", CopyBufferSubData); diff --git a/src/native/webgl.cc b/src/native/webgl.cc index e0d1a0a3..192b7501 100644 --- a/src/native/webgl.cc +++ b/src/native/webgl.cc @@ -417,6 +417,7 @@ WebGLRenderingContext::WebGLRenderingContext(int width, int height, bool createW {"EXT_texture_compression_bptc", {"GL_EXT_texture_compression_bptc"}}); if (createWebGL2Context) { webGLToANGLEExtensions.insert({"EXT_color_buffer_float", {"GL_EXT_color_buffer_float"}}); + webGLToANGLEExtensions.insert({"WEBGL_provoking_vertex", {"GL_ANGLE_provoking_vertex"}}); webGLToANGLEExtensions.insert( {"EXT_color_buffer_half_float", {"GL_EXT_color_buffer_half_float"}}); } else { @@ -2692,6 +2693,12 @@ GL_METHOD(DeleteVertexArrayOES) { glDeleteVertexArraysOES(1, &array); } +GL_METHOD(ProvokingVertexANGLE) { + GL_BOILERPLATE; + + glProvokingVertexANGLE(Nan::To(info[0]).ToChecked()); +} + GL_METHOD(IsVertexArrayOES) { GL_BOILERPLATE; diff --git a/src/native/webgl.h b/src/native/webgl.h index 1cdc4674..e56e0b06 100644 --- a/src/native/webgl.h +++ b/src/native/webgl.h @@ -271,6 +271,7 @@ struct WebGLRenderingContext : public node::ObjectWrap { static NAN_METHOD(EXTWEBGL_draw_buffers); static NAN_METHOD(BindVertexArrayOES); + static NAN_METHOD(ProvokingVertexANGLE); static NAN_METHOD(CreateVertexArrayOES); static NAN_METHOD(DeleteVertexArrayOES); static NAN_METHOD(IsVertexArrayOES); diff --git a/test/types/node.mts b/test/types/node.mts index 71b2f0aa..fd6b8732 100644 --- a/test/types/node.mts +++ b/test/types/node.mts @@ -22,6 +22,8 @@ gl.createVertexArray() const gl2 = createGL(1, 1, { createWebGL2Context: true, alpha: false }) gl2.bindVertexArray(gl2.createVertexArray()) +const provokingVertex = gl2.getExtension('WEBGL_provoking_vertex') +provokingVertex?.provokingVertexWEBGL(provokingVertex.FIRST_VERTEX_CONVENTION_WEBGL) const texture3D: number = WebGL2RenderingContext.TEXTURE_3D const textureCube: number = WebGLRenderingContext.TEXTURE_CUBE_MAP // @ts-expect-error The classes have the constants but not the methods. diff --git a/test/webgl-provoking-vertex.js b/test/webgl-provoking-vertex.js new file mode 100644 index 00000000..57b032e3 --- /dev/null +++ b/test/webgl-provoking-vertex.js @@ -0,0 +1,96 @@ +'use strict' + +const tape = require('tape') +const createContext = require('../index') + +const PROVOKING_VERTEX_WEBGL = 0x8E4F + +// Draws one triangle over the pixel with a flat color: red at its first vertex, green at its second, blue at its last. +function drawFlatTriangle (gl) { + function compile (type, source) { + const shader = gl.createShader(type) + gl.shaderSource(shader, source) + gl.compileShader(shader) + return shader + } + const program = gl.createProgram() + gl.attachShader(program, compile(gl.VERTEX_SHADER, `#version 300 es +in vec2 position; +in vec3 color; +flat out vec3 vColor; +void main() { + vColor = color; + gl_Position = vec4(position, 0.0, 1.0); +}`)) + gl.attachShader(program, compile(gl.FRAGMENT_SHADER, `#version 300 es +precision mediump float; +flat in vec3 vColor; +out vec4 fragColor; +void main() { + fragColor = vec4(vColor, 1.0); +}`)) + gl.bindAttribLocation(program, 0, 'position') + gl.bindAttribLocation(program, 1, 'color') + gl.linkProgram(program) + gl.useProgram(program) + + const vertices = gl.createBuffer() + gl.bindBuffer(gl.ARRAY_BUFFER, vertices) + gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ + -1, -1, 1, 0, 0, + 3, -1, 0, 1, 0, + -1, 3, 0, 0, 1 + ]), gl.STATIC_DRAW) + gl.enableVertexAttribArray(0) + gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 20, 0) + gl.enableVertexAttribArray(1) + gl.vertexAttribPointer(1, 3, gl.FLOAT, false, 20, 8) + + gl.clearColor(0, 0, 0, 1) + gl.clear(gl.COLOR_BUFFER_BIT) + gl.drawArrays(gl.TRIANGLES, 0, 3) + + const pixel = new Uint8Array(4) + gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixel) + gl.deleteBuffer(vertices) + gl.deleteProgram(program) + return Array.from(pixel) +} + +tape('WEBGL_provoking_vertex: WebGL1', function (t) { + const gl = createContext(1, 1) + t.equal(gl.getExtension('WEBGL_provoking_vertex'), null, 'not available') + gl.getExtension('STACKGL_destroy_context').destroy() + t.end() +}) + +tape('WEBGL_provoking_vertex: WebGL2', function (t) { + const gl = createContext(1, 1, { createWebGL2Context: true }) + + t.equal(gl.getParameter(PROVOKING_VERTEX_WEBGL), null, 'getParameter returns null before it is enabled') + t.equal(gl.getError(), gl.INVALID_ENUM, 'getParameter sets INVALID_ENUM before it is enabled') + + const ext = gl.getExtension('WEBGL_provoking_vertex') + t.ok(ext, 'available') + t.equal(ext.FIRST_VERTEX_CONVENTION_WEBGL, 0x8E4D, 'FIRST_VERTEX_CONVENTION_WEBGL') + t.equal(ext.LAST_VERTEX_CONVENTION_WEBGL, 0x8E4E, 'LAST_VERTEX_CONVENTION_WEBGL') + t.equal(ext.PROVOKING_VERTEX_WEBGL, PROVOKING_VERTEX_WEBGL, 'PROVOKING_VERTEX_WEBGL') + + t.equal(gl.getParameter(ext.PROVOKING_VERTEX_WEBGL), ext.LAST_VERTEX_CONVENTION_WEBGL, 'the last vertex convention at first') + t.deepEqual(drawFlatTriangle(gl), [0, 0, 255, 255], 'flat varyings come from the last vertex') + + ext.provokingVertexWEBGL(ext.FIRST_VERTEX_CONVENTION_WEBGL) + t.equal(gl.getParameter(ext.PROVOKING_VERTEX_WEBGL), ext.FIRST_VERTEX_CONVENTION_WEBGL, 'getParameter returns the first vertex convention once set') + t.deepEqual(drawFlatTriangle(gl), [255, 0, 0, 255], 'flat varyings come from the first vertex') + + ext.provokingVertexWEBGL(gl.TRIANGLES) + t.equal(gl.getError(), gl.INVALID_ENUM, 'another mode sets INVALID_ENUM') + t.equal(gl.getParameter(ext.PROVOKING_VERTEX_WEBGL), ext.FIRST_VERTEX_CONVENTION_WEBGL, 'and leaves the convention as it was') + + ext.provokingVertexWEBGL(ext.LAST_VERTEX_CONVENTION_WEBGL) + t.deepEqual(drawFlatTriangle(gl), [0, 0, 255, 255], 'the last vertex convention once set back') + t.equal(gl.getError(), gl.NO_ERROR, 'no GL errors') + + gl.getExtension('STACKGL_destroy_context').destroy() + t.end() +})