Skip to content

Commit a35eeba

Browse files
null77dhritzkiv
authored andcommitted
Initial support for creating WebGL 2 contexts.
This support is very incomplete and only works for a bare minimum of functionality. It hasn't been tested against the WebGL 2 CTS. To create a WebGL 2 context, use the "createWebGL2Context" attribute in the creation options struct. Implementation notes: - in native code, we use a single native class for both context types - we assign enum constants dynamically, so we replace gl.ENUM with this.ENUM in the context helper functions - only adds VAOs from the set of new WebGL 2 object types This contribution is funded by https://higharc.com/
1 parent e75142e commit a35eeba

11 files changed

+1936
-436
lines changed

index.d.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,19 @@ declare namespace createContext {
2626
declare function createContext(
2727
width: number,
2828
height: number,
29-
options?: WebGLContextAttributes,
29+
options?: WebGLContextAttributes & { createWebGL2Context?: false },
3030
): WebGLRenderingContext & createContext.StackGLExtension;
3131

32+
declare function createContext(
33+
width: number,
34+
height: number,
35+
options: WebGLContextAttributes & { createWebGL2Context: true }
36+
): WebGL2RenderingContext & createContext.StackGLExtension;
37+
38+
declare function createContext(
39+
width: number,
40+
height: number,
41+
options?: WebGLContextAttributes & { createWebGL2Context?: boolean }
42+
): (WebGLRenderingContext | WebGL2RenderingContext) & createContext.StackGLExtension;
43+
3244
export = createContext;

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ if (typeof WebGLRenderingContext !== 'undefined') {
44
module.exports = require('./src/javascript/node-index')
55
}
66
module.exports.WebGLRenderingContext = require('./src/javascript/webgl-rendering-context').WebGLRenderingContext
7+
module.exports.WebGL2RenderingContext = require('./src/javascript/webgl-rendering-context').WebGL2RenderingContext
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class EXTColorBufferFloat {}
2+
3+
function getEXTColorBufferFloat (context) {
4+
let result = null
5+
const exts = context.getSupportedExtensions()
6+
7+
if (exts && exts.indexOf('EXT_color_buffer_float') >= 0) {
8+
result = new EXTColorBufferFloat()
9+
}
10+
11+
return result
12+
}
13+
14+
module.exports = { getEXTColorBufferFloat, EXTColorBufferFloat }

src/javascript/node-index.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const bits = require('bit-twiddle')
22
const { WebGLContextAttributes } = require('./webgl-context-attributes')
3-
const { WebGLRenderingContext, wrapContext } = require('./webgl-rendering-context')
3+
const { WebGLRenderingContext, WebGL2RenderingContext, wrapContext } = require('./webgl-rendering-context')
44
const { WebGLTextureUnit } = require('./webgl-texture-unit')
55
const { WebGLVertexArrayObjectState, WebGLVertexArrayGlobalState } = require('./webgl-vertex-attribute')
66

@@ -28,15 +28,17 @@ function createContext (width, height, options) {
2828
flag(options, 'premultipliedAlpha', true),
2929
flag(options, 'preserveDrawingBuffer', false),
3030
flag(options, 'preferLowPowerToHighPerformance', false),
31-
flag(options, 'failIfMajorPerformanceCaveat', false))
31+
flag(options, 'failIfMajorPerformanceCaveat', false),
32+
flag(options, 'createWebGL2Context', false))
3233

3334
// Can only use premultipliedAlpha if alpha is set
3435
contextAttributes.premultipliedAlpha =
3536
contextAttributes.premultipliedAlpha && contextAttributes.alpha
3637

38+
const WebGLContext = contextAttributes.createWebGL2Context ? WebGL2RenderingContext : WebGLRenderingContext
3739
let ctx
3840
try {
39-
ctx = new WebGLRenderingContext(
41+
ctx = new WebGLContext(
4042
1,
4143
1,
4244
contextAttributes.alpha,
@@ -46,7 +48,8 @@ function createContext (width, height, options) {
4648
contextAttributes.premultipliedAlpha,
4749
contextAttributes.preserveDrawingBuffer,
4850
contextAttributes.preferLowPowerToHighPerformance,
49-
contextAttributes.failIfMajorPerformanceCaveat)
51+
contextAttributes.failIfMajorPerformanceCaveat,
52+
contextAttributes.createWebGL2Context)
5053
} catch (e) {}
5154
if (!ctx) {
5255
return null
@@ -73,11 +76,16 @@ function createContext (width, height, options) {
7376
ctx._checkStencil = false
7477
ctx._stencilState = true
7578

79+
if (contextAttributes.createWebGL2Context) {
80+
ctx._vaos = {}
81+
ctx._activeVertexArrayObject = null
82+
}
83+
7684
// Initialize texture units
7785
const numTextures = ctx.getParameter(ctx.MAX_COMBINED_TEXTURE_IMAGE_UNITS)
7886
ctx._textureUnits = new Array(numTextures)
7987
for (let i = 0; i < numTextures; ++i) {
80-
ctx._textureUnits[i] = new WebGLTextureUnit(i)
88+
ctx._textureUnits[i] = new WebGLTextureUnit(ctx, i)
8189
}
8290
ctx._activeTextureUnit = 0
8391
ctx.activeTexture(ctx.TEXTURE0)

src/javascript/webgl-context-attributes.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ class WebGLContextAttributes {
77
premultipliedAlpha,
88
preserveDrawingBuffer,
99
preferLowPowerToHighPerformance,
10-
failIfMajorPerformanceCaveat) {
10+
failIfMajorPerformanceCaveat,
11+
createWebGL2Context) {
1112
this.alpha = alpha
1213
this.depth = depth
1314
this.stencil = stencil
@@ -16,6 +17,7 @@ class WebGLContextAttributes {
1617
this.preserveDrawingBuffer = preserveDrawingBuffer
1718
this.preferLowPowerToHighPerformance = preferLowPowerToHighPerformance
1819
this.failIfMajorPerformanceCaveat = failIfMajorPerformanceCaveat
20+
this.createWebGL2Context = createWebGL2Context
1921
}
2022
}
2123

0 commit comments

Comments
 (0)