Skip to content

Commit

Permalink
support rendering to windows
Browse files Browse the repository at this point in the history
  • Loading branch information
kmamal committed Jan 19, 2025
1 parent c16e31e commit 457afd2
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 17 deletions.
12 changes: 9 additions & 3 deletions src/javascript/node-index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable */

const bits = require('bit-twiddle')
const { WebGLContextAttributes } = require('./webgl-context-attributes')
const { WebGLRenderingContext, wrapContext } = require('./webgl-rendering-context')
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
14 changes: 9 additions & 5 deletions src/javascript/webgl-rendering-context.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable */

const bits = require('bit-twiddle')
const tokenize = require('glsl-tokenizer/string')
const HEADLESS_VERSION = require('../../package.json').version
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 2 additions & 0 deletions src/native/bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
44 changes: 36 additions & 8 deletions src/native/webgl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -258,6 +275,10 @@ GL_METHOD(DisposeAll) {
GL_METHOD(New) {
Nan::HandleScope();

EGLNativeWindowType* window = info[10]->IsUndefined()
? nullptr
: *Nan::TypedArrayContents<EGLNativeWindowType>(info[10]);

WebGLRenderingContext* instance = new WebGLRenderingContext(
Nan::To<int32_t>(info[0]).ToChecked() //Width
, Nan::To<int32_t>(info[1]).ToChecked() //Height
Expand All @@ -269,6 +290,7 @@ GL_METHOD(New) {
, (Nan::To<bool>(info[7]).ToChecked()) //preserve drawing buffer
, (Nan::To<bool>(info[8]).ToChecked()) //low power
, (Nan::To<bool>(info[9]).ToChecked()) //fail if crap
, window
);

if(instance->state != GLCONTEXT_STATE_OK){
Expand All @@ -286,6 +308,12 @@ GL_METHOD(Destroy) {
inst->dispose();
}

GL_METHOD(Swap) {
GL_BOILERPLATE;

inst->swap();
}

GL_METHOD(Uniform1f) {
GL_BOILERPLATE;

Expand Down Expand Up @@ -1639,7 +1667,7 @@ GL_METHOD(GetTexParameter) {

GLenum target = Nan::To<int32_t>(info[0]).ToChecked();
GLenum pname = Nan::To<int32_t>(info[1]).ToChecked();

if (pname == GL_TEXTURE_MAX_ANISOTROPY_EXT) {
GLfloat param_value = 0;
(inst->glGetTexParameterfv)(target, pname, &param_value);
Expand Down
7 changes: 6 additions & 1 deletion src/native/webgl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 457afd2

Please sign in to comment.