Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
8 changes: 8 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
29 changes: 29 additions & 0 deletions src/javascript/extensions/webgl-provoking-vertex.js
Original file line number Diff line number Diff line change
@@ -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 }
4 changes: 3 additions & 1 deletion src/javascript/webgl-rendering-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/native/bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions src/native/webgl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -2692,6 +2693,12 @@ GL_METHOD(DeleteVertexArrayOES) {
glDeleteVertexArraysOES(1, &array);
}

GL_METHOD(ProvokingVertexANGLE) {
GL_BOILERPLATE;

glProvokingVertexANGLE(Nan::To<int32_t>(info[0]).ToChecked());
}

GL_METHOD(IsVertexArrayOES) {
GL_BOILERPLATE;

Expand Down
1 change: 1 addition & 0 deletions src/native/webgl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions test/types/node.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
96 changes: 96 additions & 0 deletions test/webgl-provoking-vertex.js
Original file line number Diff line number Diff line change
@@ -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()
})