diff --git a/src/javascript/node-index.js b/src/javascript/node-index.js index 9f484eef..51d7f933 100644 --- a/src/javascript/node-index.js +++ b/src/javascript/node-index.js @@ -1,3 +1,5 @@ +/* eslint-disable */ + const bits = require('bit-twiddle') const { WebGLContextAttributes } = require('./webgl-context-attributes') const { WebGLRenderingContext, wrapContext } = require('./webgl-rendering-context') @@ -34,6 +36,9 @@ function createContext (width, height, options) { contextAttributes.premultipliedAlpha = contextAttributes.premultipliedAlpha && contextAttributes.alpha + const window = options && options.window + const hasWindow = Boolean(window) + let ctx try { ctx = new WebGLRenderingContext( @@ -46,7 +51,8 @@ function createContext (width, height, options) { contextAttributes.premultipliedAlpha, contextAttributes.preserveDrawingBuffer, contextAttributes.preferLowPowerToHighPerformance, - contextAttributes.failIfMajorPerformanceCaveat) + contextAttributes.failIfMajorPerformanceCaveat, + window) } catch (e) {} if (!ctx) { return null @@ -102,7 +108,7 @@ function createContext (width, height, options) { ctx._packAlignment = 4 // Allocate framebuffer - ctx._allocateDrawingBuffer(width, height) + ctx._allocateDrawingBuffer(width, height, hasWindow) const attrib0Buffer = ctx.createBuffer() ctx._attrib0Buffer = attrib0Buffer @@ -123,7 +129,7 @@ function createContext (width, height, options) { ctx.clearStencil(0) ctx.clear(ctx.COLOR_BUFFER_BIT | ctx.DEPTH_BUFFER_BIT | ctx.STENCIL_BUFFER_BIT) - return wrapContext(ctx) + return hasWindow ? ctx : wrapContext(ctx) } module.exports = createContext diff --git a/src/javascript/webgl-rendering-context.js b/src/javascript/webgl-rendering-context.js index 62a5b9a6..2eca2f24 100644 --- a/src/javascript/webgl-rendering-context.js +++ b/src/javascript/webgl-rendering-context.js @@ -1,3 +1,5 @@ +/* eslint-disable */ + const bits = require('bit-twiddle') const tokenize = require('glsl-tokenizer/string') const HEADLESS_VERSION = require('../../package.json').version @@ -3500,11 +3502,13 @@ class WebGLRenderingContext extends NativeWebGLRenderingContext { return super.viewport(x | 0, y | 0, width | 0, height | 0) } - _allocateDrawingBuffer (width, height) { - this._drawingBuffer = new WebGLDrawingBufferWrapper( - super.createFramebuffer(), - super.createTexture(), - super.createRenderbuffer()) + _allocateDrawingBuffer (width, height, hasWindow) { + this._drawingBuffer = hasWindow + ? new WebGLDrawingBufferWrapper(0, 0, 0) + : new WebGLDrawingBufferWrapper( + super.createFramebuffer(), + super.createTexture(), + super.createRenderbuffer()) this._resizeDrawingBuffer(width, height) } diff --git a/src/native/bindings.cc b/src/native/bindings.cc index 397abfc2..5f40cf2c 100644 --- a/src/native/bindings.cc +++ b/src/native/bindings.cc @@ -38,6 +38,8 @@ NAN_MODULE_INIT(Init) { JS_GL_METHOD("_drawElementsInstanced", DrawElementsInstanced); JS_GL_METHOD("_vertexAttribDivisor", VertexAttribDivisor); + JS_GL_METHOD("swap", Swap); + JS_GL_METHOD("getUniform", GetUniform); JS_GL_METHOD("uniform1f", Uniform1f); JS_GL_METHOD("uniform2f", Uniform2f); diff --git a/src/native/webgl.cc b/src/native/webgl.cc index e13245d9..bb1bea5b 100644 --- a/src/native/webgl.cc +++ b/src/native/webgl.cc @@ -38,7 +38,8 @@ WebGLRenderingContext::WebGLRenderingContext( , bool premultipliedAlpha , bool preserveDrawingBuffer , bool preferLowPowerToHighPerformance - , bool failIfMajorPerformanceCaveat) : + , bool failIfMajorPerformanceCaveat + , EGLNativeWindowType* window) : state(GLCONTEXT_STATE_INIT) , unpack_flip_y(false) , unpack_premultiply_alpha(false) @@ -100,12 +101,17 @@ WebGLRenderingContext::WebGLRenderingContext( return; } - EGLint surfaceAttribs[] = { - EGL_WIDTH, (EGLint)width - , EGL_HEIGHT, (EGLint)height - , EGL_NONE - }; - surface = eglCreatePbufferSurface(DISPLAY, config, surfaceAttribs); + if (window) { + surface = eglCreateWindowSurface(DISPLAY, config, *window, nullptr); + } else { + EGLint surfaceAttribs[] = { + EGL_WIDTH, (EGLint)width + , EGL_HEIGHT, (EGLint)height + , EGL_NONE + }; + surface = eglCreatePbufferSurface(DISPLAY, config, surfaceAttribs); + } + if (surface == EGL_NO_SURFACE) { state = GLCONTEXT_STATE_ERROR; return; @@ -146,6 +152,17 @@ WebGLRenderingContext::WebGLRenderingContext( } } +bool WebGLRenderingContext::swap() { + if (state != GLCONTEXT_STATE_OK) { + return false; + } + if (!eglSwapBuffers(DISPLAY, surface)) { + state = GLCONTEXT_STATE_ERROR; + return false; + } + return true; +} + bool WebGLRenderingContext::setActive() { if (state != GLCONTEXT_STATE_OK) { return false; @@ -258,6 +275,10 @@ GL_METHOD(DisposeAll) { GL_METHOD(New) { Nan::HandleScope(); + EGLNativeWindowType* window = info[10]->IsUndefined() + ? nullptr + : *Nan::TypedArrayContents(info[10]); + WebGLRenderingContext* instance = new WebGLRenderingContext( Nan::To(info[0]).ToChecked() //Width , Nan::To(info[1]).ToChecked() //Height @@ -269,6 +290,7 @@ GL_METHOD(New) { , (Nan::To(info[7]).ToChecked()) //preserve drawing buffer , (Nan::To(info[8]).ToChecked()) //low power , (Nan::To(info[9]).ToChecked()) //fail if crap + , window ); if(instance->state != GLCONTEXT_STATE_OK){ @@ -286,6 +308,12 @@ GL_METHOD(Destroy) { inst->dispose(); } +GL_METHOD(Swap) { + GL_BOILERPLATE; + + inst->swap(); +} + GL_METHOD(Uniform1f) { GL_BOILERPLATE; @@ -1639,7 +1667,7 @@ GL_METHOD(GetTexParameter) { GLenum target = Nan::To(info[0]).ToChecked(); GLenum pname = Nan::To(info[1]).ToChecked(); - + if (pname == GL_TEXTURE_MAX_ANISOTROPY_EXT) { GLfloat param_value = 0; (inst->glGetTexParameterfv)(target, pname, ¶m_value); diff --git a/src/native/webgl.h b/src/native/webgl.h index 2bc651c2..57168352 100644 --- a/src/native/webgl.h +++ b/src/native/webgl.h @@ -95,9 +95,12 @@ struct WebGLRenderingContext : public node::ObjectWrap { bool premultipliedAlpha, bool preserveDrawingBuffer, bool preferLowPowerToHighPerformance, - bool failIfMajorPerformanceCaveat); + bool failIfMajorPerformanceCaveat, + EGLNativeWindowType* window); virtual ~WebGLRenderingContext(); + bool swap(); + //Context validation static WebGLRenderingContext* ACTIVE; bool setActive(); @@ -128,6 +131,8 @@ struct WebGLRenderingContext : public node::ObjectWrap { static NAN_METHOD(New); static NAN_METHOD(Destroy); + static NAN_METHOD(Swap); + static NAN_METHOD(VertexAttribDivisor); static NAN_METHOD(DrawArraysInstanced); static NAN_METHOD(DrawElementsInstanced);