Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial support for creating WebGL 2 contexts. #310

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
14 changes: 13 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,19 @@ declare namespace createContext {
declare function createContext(
width: number,
height: number,
options?: WebGLContextAttributes,
options?: WebGLContextAttributes & { createWebGL2Context?: false },
): WebGLRenderingContext & createContext.StackGLExtension;

declare function createContext(
width: number,
height: number,
options: WebGLContextAttributes & { createWebGL2Context: true }
): WebGL2RenderingContext & createContext.StackGLExtension;

declare function createContext(
width: number,
height: number,
options?: WebGLContextAttributes & { createWebGL2Context?: boolean }
): (WebGLRenderingContext | WebGL2RenderingContext) & createContext.StackGLExtension;

export = createContext;
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ if (typeof WebGLRenderingContext !== 'undefined') {
module.exports = require('./src/javascript/node-index')
}
module.exports.WebGLRenderingContext = require('./src/javascript/webgl-rendering-context').WebGLRenderingContext
module.exports.WebGL2RenderingContext = require('./src/javascript/webgl-rendering-context').WebGL2RenderingContext
14 changes: 14 additions & 0 deletions src/javascript/extensions/ext-color-buffer-float.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class EXTColorBufferFloat {}

function getEXTColorBufferFloat (context) {
let result = null
const exts = context.getSupportedExtensions()

if (exts && exts.indexOf('EXT_color_buffer_float') >= 0) {
result = new EXTColorBufferFloat()
}

return result
}

module.exports = { getEXTColorBufferFloat, EXTColorBufferFloat }
16 changes: 12 additions & 4 deletions src/javascript/node-index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const bits = require('bit-twiddle')
const { WebGLContextAttributes } = require('./webgl-context-attributes')
const { WebGLRenderingContext, wrapContext } = require('./webgl-rendering-context')
const { WebGLRenderingContext, WebGL2RenderingContext, wrapContext } = require('./webgl-rendering-context')
const { WebGLTextureUnit } = require('./webgl-texture-unit')
const { WebGLVertexArrayObjectState, WebGLVertexArrayGlobalState } = require('./webgl-vertex-attribute')

Expand Down Expand Up @@ -28,15 +28,17 @@ function createContext (width, height, options) {
flag(options, 'premultipliedAlpha', true),
flag(options, 'preserveDrawingBuffer', false),
flag(options, 'preferLowPowerToHighPerformance', false),
flag(options, 'failIfMajorPerformanceCaveat', false))
flag(options, 'failIfMajorPerformanceCaveat', false),
flag(options, 'createWebGL2Context', false))

// Can only use premultipliedAlpha if alpha is set
contextAttributes.premultipliedAlpha =
contextAttributes.premultipliedAlpha && contextAttributes.alpha

const WebGLContext = contextAttributes.createWebGL2Context ? WebGL2RenderingContext : WebGLRenderingContext
let ctx
try {
ctx = new WebGLRenderingContext(
ctx = new WebGLContext(
1,
1,
contextAttributes.alpha,
Expand All @@ -46,7 +48,8 @@ function createContext (width, height, options) {
contextAttributes.premultipliedAlpha,
contextAttributes.preserveDrawingBuffer,
contextAttributes.preferLowPowerToHighPerformance,
contextAttributes.failIfMajorPerformanceCaveat)
contextAttributes.failIfMajorPerformanceCaveat,
contextAttributes.createWebGL2Context)
} catch (e) {}
if (!ctx) {
return null
Expand All @@ -73,6 +76,11 @@ function createContext (width, height, options) {
ctx._checkStencil = false
ctx._stencilState = true

if (contextAttributes.createWebGL2Context) {
ctx._vaos = {}
ctx._activeVertexArrayObject = null
}

// Initialize texture units
const numTextures = ctx.getParameter(ctx.MAX_COMBINED_TEXTURE_IMAGE_UNITS)
ctx._textureUnits = new Array(numTextures)
Expand Down
4 changes: 3 additions & 1 deletion src/javascript/webgl-context-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class WebGLContextAttributes {
premultipliedAlpha,
preserveDrawingBuffer,
preferLowPowerToHighPerformance,
failIfMajorPerformanceCaveat) {
failIfMajorPerformanceCaveat,
createWebGL2Context) {
this.alpha = alpha
this.depth = depth
this.stencil = stencil
Expand All @@ -16,6 +17,7 @@ class WebGLContextAttributes {
this.preserveDrawingBuffer = preserveDrawingBuffer
this.preferLowPowerToHighPerformance = preferLowPowerToHighPerformance
this.failIfMajorPerformanceCaveat = failIfMajorPerformanceCaveat
this.createWebGL2Context = createWebGL2Context
}
}

Expand Down
Loading