Skip to content

Commit 1648dc2

Browse files
fix: Uniforms
fix: simplify api a bit, move wrapper parts - bindPublics into utils.js - wrapContext into webgl-render-context.js, since that class basically owns the functionality of wrapContext fix: Simplify how colorAttachments are checked so all functionalities are the same fix: Remove framebuffer._attachmentsArray, no longer needed All unit tests (that my driver supports on OSX) are running and passing locally!
1 parent 795a749 commit 1648dc2

File tree

6 files changed

+121
-148
lines changed

6 files changed

+121
-148
lines changed

src/javascript/node-index.js

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

7-
const wrap = require('./wrap')
8-
97
let CONTEXT_COUNTER = 0
108

119
function flag (options, name, dflt) {
@@ -36,9 +34,9 @@ function createContext (width, height, options) {
3634
contextAttributes.premultipliedAlpha =
3735
contextAttributes.premultipliedAlpha && contextAttributes.alpha
3836

39-
let gl
37+
let ctx
4038
try {
41-
gl = new WebGLRenderingContext(
39+
ctx = new WebGLRenderingContext(
4240
1,
4341
1,
4442
contextAttributes.alpha,
@@ -50,82 +48,82 @@ function createContext (width, height, options) {
5048
contextAttributes.preferLowPowerToHighPerformance,
5149
contextAttributes.failIfMajorPerformanceCaveat)
5250
} catch (e) {}
53-
if (!gl) {
51+
if (!ctx) {
5452
return null
5553
}
5654

57-
gl.drawingBufferWidth = width
58-
gl.drawingBufferHeight = height
55+
ctx.drawingBufferWidth = width
56+
ctx.drawingBufferHeight = height
5957

60-
gl._ = CONTEXT_COUNTER++
58+
ctx._ = CONTEXT_COUNTER++
6159

62-
gl._contextAttributes = contextAttributes
60+
ctx._contextAttributes = contextAttributes
6361

64-
gl._extensions = {}
65-
gl._programs = {}
66-
gl._shaders = {}
67-
gl._buffers = {}
68-
gl._textures = {}
69-
gl._framebuffers = {}
70-
gl._renderbuffers = {}
62+
ctx._extensions = {}
63+
ctx._programs = {}
64+
ctx._shaders = {}
65+
ctx._buffers = {}
66+
ctx._textures = {}
67+
ctx._framebuffers = {}
68+
ctx._renderbuffers = {}
7169

72-
gl._activeProgram = null
73-
gl._activeFramebuffer = null
74-
gl._activeArrayBuffer = null
75-
gl._activeElementArrayBuffer = null
76-
gl._activeRenderbuffer = null
70+
ctx._activeProgram = null
71+
ctx._activeFramebuffer = null
72+
ctx._activeArrayBuffer = null
73+
ctx._activeElementArrayBuffer = null
74+
ctx._activeRenderbuffer = null
7775

7876
// Initialize texture units
79-
const numTextures = gl.getParameter(gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS)
80-
gl._textureUnits = new Array(numTextures)
77+
const numTextures = ctx.getParameter(ctx.MAX_COMBINED_TEXTURE_IMAGE_UNITS)
78+
ctx._textureUnits = new Array(numTextures)
8179
for (let i = 0; i < numTextures; ++i) {
82-
gl._textureUnits[i] = new WebGLTextureUnit(i)
80+
ctx._textureUnits[i] = new WebGLTextureUnit(i)
8381
}
84-
gl._activeTextureUnit = 0
85-
gl.activeTexture(gl.TEXTURE0)
82+
ctx._activeTextureUnit = 0
83+
ctx.activeTexture(ctx.TEXTURE0)
8684

87-
gl._errorStack = []
85+
ctx._errorStack = []
8886

8987
// Initialize vertex attributes
90-
var numAttribs = gl.getParameter(gl.MAX_VERTEX_ATTRIBS)
91-
gl._vertexAttribs = new Array(numAttribs)
88+
const numAttribs = ctx.getParameter(ctx.MAX_VERTEX_ATTRIBS)
89+
ctx._vertexAttribs = new Array(numAttribs)
9290
for (let i = 0; i < numAttribs; ++i) {
93-
gl._vertexAttribs[i] = new WebGLVertexAttribute(gl, i)
91+
ctx._vertexAttribs[i] = new WebGLVertexAttribute(ctx, i)
9492
}
9593

9694
// Store limits
97-
gl._maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE)
98-
gl._maxTextureLevel = bits.log2(bits.nextPow2(gl._maxTextureSize))
99-
gl._maxCubeMapSize = gl.getParameter(gl.MAX_CUBE_MAP_TEXTURE_SIZE)
100-
gl._maxCubeMapLevel = bits.log2(bits.nextPow2(gl._maxCubeMapSize))
95+
ctx._maxTextureSize = ctx.getParameter(ctx.MAX_TEXTURE_SIZE)
96+
ctx._maxTextureLevel = bits.log2(bits.nextPow2(ctx._maxTextureSize))
97+
ctx._maxCubeMapSize = ctx.getParameter(ctx.MAX_CUBE_MAP_TEXTURE_SIZE)
98+
ctx._maxCubeMapLevel = bits.log2(bits.nextPow2(ctx._maxCubeMapSize))
10199

102100
// Unpack alignment
103-
gl._unpackAlignment = 4
104-
gl._packAlignment = 4
101+
ctx._unpackAlignment = 4
102+
ctx._packAlignment = 4
105103

106104
// Allocate framebuffer
107-
gl._allocateDrawingBuffer(width, height)
105+
ctx._allocateDrawingBuffer(width, height)
108106

109-
const attrib0Buffer = gl.createBuffer()
110-
gl._attrib0Buffer = attrib0Buffer
107+
const attrib0Buffer = ctx.createBuffer()
108+
ctx._attrib0Buffer = attrib0Buffer
111109

112110
// Initialize defaults
113-
gl.bindBuffer(gl.ARRAY_BUFFER, null)
114-
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null)
115-
gl.bindFramebuffer(gl.FRAMEBUFFER, null)
116-
gl.bindRenderbuffer(gl.RENDERBUFFER, null)
111+
ctx.bindBuffer(ctx.ARRAY_BUFFER, null)
112+
ctx.bindBuffer(ctx.ELEMENT_ARRAY_BUFFER, null)
113+
ctx.bindFramebuffer(ctx.FRAMEBUFFER, null)
114+
ctx.bindRenderbuffer(ctx.RENDERBUFFER, null)
117115

118116
// Set viewport and scissor
119-
gl.viewport(0, 0, width, height)
120-
gl.scissor(0, 0, width, height)
117+
ctx.viewport(0, 0, width, height)
118+
ctx.scissor(0, 0, width, height)
121119

122120
// Clear buffers
123-
gl.clearDepth(1)
124-
gl.clearColor(0, 0, 0, 0)
125-
gl.clearStencil(0)
126-
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT)
121+
ctx.clearDepth(1)
122+
ctx.clearColor(0, 0, 0, 0)
123+
ctx.clearStencil(0)
124+
ctx.clear(ctx.COLOR_BUFFER_BIT | ctx.DEPTH_BUFFER_BIT | ctx.STENCIL_BUFFER_BIT)
127125

128-
return wrap(gl)
126+
return wrapContext(ctx)
129127
}
130128

131129
module.exports = createContext

src/javascript/utils.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@ const { gl } = require('./native-gl')
22

33
const { WebGLUniformLocation } = require('./webgl-uniform-location')
44

5+
function bindPublics (props, wrapper, privateInstance, privateMethods) {
6+
for (let i = 0; i < props.length; i++) {
7+
const prop = props[i]
8+
const value = privateInstance[prop]
9+
if (typeof value === 'function') {
10+
if (privateMethods.indexOf(prop) === -1) {
11+
wrapper[prop] = value.bind(privateInstance)
12+
}
13+
} else {
14+
if (prop[0] === '_' ||
15+
prop[0] === '0' ||
16+
prop[0] === '1') {
17+
continue
18+
}
19+
wrapper[prop] = value
20+
}
21+
}
22+
}
23+
524
function checkObject (object) {
625
return typeof object === 'object' ||
726
(object === void 0)
@@ -188,6 +207,7 @@ function validCubeTarget (target) {
188207
}
189208

190209
module.exports = {
210+
bindPublics,
191211
checkObject,
192212
isTypedArray,
193213
isValidString,

src/javascript/webgl-framebuffer.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class WebGLFramebuffer extends Linkable {
1111
this._height = 0
1212
this._status = null
1313

14-
this._attachmentsArray = []
1514
this._attachments = {}
1615
this._attachments[gl.COLOR_ATTACHMENT0] = null
1716
this._attachments[gl.DEPTH_ATTACHMENT] = null
@@ -94,7 +93,6 @@ class WebGLFramebuffer extends Linkable {
9493
return
9594
}
9695
this._attachments[attachment] = null
97-
this._attachmentsArray.splice(this._attachmentsArray.indexOf(attachment), 1)
9896
this._unlink(object)
9997
}
10098

@@ -110,7 +108,6 @@ class WebGLFramebuffer extends Linkable {
110108
}
111109

112110
this._attachments[attachment] = object
113-
this._attachmentsArray.push(object)
114111

115112
this._link(object)
116113
}

src/javascript/webgl-rendering-context.js

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const { getSTACKGLDestroyContext } = require('./extensions/stackgl-destroy-conte
99
const { getSTACKGLResizeDrawingBuffer } = require('./extensions/stackgl-resize-drawing-buffer')
1010
const { getWebGLDrawBuffers } = require('./extensions/webgl-draw-buffers')
1111
const {
12+
bindPublics,
1213
checkObject,
1314
checkUniform,
1415
formatSize,
@@ -56,6 +57,32 @@ const availableExtensions = {
5657
webgl_draw_buffers: getWebGLDrawBuffers
5758
}
5859

60+
const privateMethods = [
61+
'resize',
62+
'destroy'
63+
]
64+
65+
function wrapContext (ctx) {
66+
const wrapper = new WebGLRenderingContext()
67+
bindPublics(Object.keys(ctx), wrapper, ctx, privateMethods)
68+
bindPublics(Object.keys(ctx.constructor.prototype), wrapper, ctx, privateMethods)
69+
bindPublics(Object.getOwnPropertyNames(ctx), wrapper, ctx, privateMethods)
70+
bindPublics(Object.getOwnPropertyNames(ctx.constructor.prototype), wrapper, ctx, privateMethods)
71+
72+
Object.defineProperties(wrapper, {
73+
drawingBufferWidth: {
74+
get () { return ctx.drawingBufferWidth },
75+
set (value) { ctx.drawingBufferWidth = value }
76+
},
77+
drawingBufferHeight: {
78+
get () { return ctx.drawingBufferHeight },
79+
set (value) { ctx.drawingBufferHeight = value }
80+
}
81+
})
82+
83+
return wrapper
84+
}
85+
5986
// We need to wrap some of the native WebGL functions to handle certain error codes and check input values
6087
class WebGLRenderingContext extends NativeWebGLRenderingContext {
6188
_checkDimensions (
@@ -380,7 +407,7 @@ class WebGLRenderingContext extends NativeWebGLRenderingContext {
380407
}
381408

382409
_getColorAttachments () {
383-
return this._extensions.webgl_draw_buffers ? this._extensions.webgl_draw_buffers._ALL_ATTACHMENTS : DEFAULT_COLOR_ATTACHMENTS
410+
return this._extensions.webgl_draw_buffers ? this._extensions.webgl_draw_buffers._ALL_COLOR_ATTACHMENTS : DEFAULT_COLOR_ATTACHMENTS
384411
}
385412

386413
_getParameterDirect (pname) {
@@ -399,9 +426,7 @@ class WebGLRenderingContext extends NativeWebGLRenderingContext {
399426
}
400427

401428
_preCheckFramebufferStatus (framebuffer) {
402-
const { webgl_draw_buffers } = this._extensions // eslint-disable-line
403429
const attachments = framebuffer._attachments
404-
const attachmentsArray = framebuffer._attachmentsArray
405430
const width = []
406431
const height = []
407432
const depthAttachment = attachments[gl.DEPTH_ATTACHMENT]
@@ -413,13 +438,16 @@ class WebGLRenderingContext extends NativeWebGLRenderingContext {
413438
return gl.FRAMEBUFFER_UNSUPPORTED
414439
}
415440

416-
if (webgl_draw_buffers) { // eslint-disable-line
417-
for (let i = 0; i < attachmentsArray.length; i++) {
418-
if (!attachmentsArray[i]) {
419-
return gl.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT
420-
}
441+
const colorAttachments = this._getColorAttachments()
442+
let colorAttachmentCount = 0
443+
for (const attachmentEnum in attachments) {
444+
if (attachments[attachmentEnum] && colorAttachments.indexOf(attachmentEnum * 1) !== -1) {
445+
colorAttachmentCount++
421446
}
422447
}
448+
if (colorAttachmentCount === 0) {
449+
return gl.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT
450+
}
423451

424452
if (depthStencilAttachment instanceof WebGLTexture) {
425453
return gl.FRAMEBUFFER_UNSUPPORTED
@@ -452,7 +480,6 @@ class WebGLRenderingContext extends NativeWebGLRenderingContext {
452480
}
453481

454482
let colorAttached = false
455-
const colorAttachments = this._getColorAttachments()
456483
for (let i = 0; i < colorAttachments.length; ++i) {
457484
const colorAttachment = attachments[colorAttachments[i]]
458485
if (colorAttachment instanceof WebGLTexture) {
@@ -3495,7 +3522,7 @@ class WebGLRenderingContext extends NativeWebGLRenderingContext {
34953522
}
34963523
return
34973524
}
3498-
super.uniform1i(location._ | 0, value[0])
3525+
this.uniform1i(location, value[0])
34993526
}
35003527

35013528
uniform2f (location, v0, v1) {
@@ -3528,7 +3555,7 @@ class WebGLRenderingContext extends NativeWebGLRenderingContext {
35283555
}
35293556
return
35303557
}
3531-
super.uniform2i(location._ | 0, value[0], value[1])
3558+
this.uniform2i(location, value[0], value[1])
35323559
}
35333560

35343561
uniform3f (location, v0, v1, v2) {
@@ -3561,7 +3588,7 @@ class WebGLRenderingContext extends NativeWebGLRenderingContext {
35613588
}
35623589
return
35633590
}
3564-
super.uniform3i(location._ | 0, value[0], value[1], value[2])
3591+
this.uniform3i(location, value[0], value[1], value[2])
35653592
}
35663593

35673594
uniform4f (location, v0, v1, v2, v3) {
@@ -3594,7 +3621,7 @@ class WebGLRenderingContext extends NativeWebGLRenderingContext {
35943621
}
35953622
return
35963623
}
3597-
super.uniform4i(location._ | 0, value[0], value[1], value[2], value[3])
3624+
this.uniform4i(location, value[0], value[1], value[2], value[3])
35983625
}
35993626

36003627
_checkUniformMatrix (location, transpose, value, name, count) {
@@ -3739,4 +3766,4 @@ class WebGLRenderingContext extends NativeWebGLRenderingContext {
37393766
}
37403767
}
37413768

3742-
module.exports = { WebGLRenderingContext }
3769+
module.exports = { WebGLRenderingContext, wrapContext }

0 commit comments

Comments
 (0)