Skip to content

Commit f5139a2

Browse files
feat: Refactor into classes, mostly working
fix: Some refactoring so class setup is less reliant on JS
1 parent 37e4c00 commit f5139a2

36 files changed

+4794
-4618
lines changed

binding.gyp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
'VERSION=1.0.0'
1414
],
1515
'sources': [
16-
'src/bindings.cc',
17-
'src/webgl.cc',
18-
'src/procs.cc'
16+
'src/native/bindings.cc',
17+
'src/native/webgl.cc',
18+
'src/native/procs.cc'
1919
],
2020
'include_dirs': [
2121
"<!(node -e \"require('nan')\")",

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
if (typeof WebGLRenderingContext !== 'undefined') {
2-
module.exports = require('./browser_index')
2+
module.exports = require('./src/javascript/browser-index')
33
} else {
4-
module.exports = require('./node_index')
4+
module.exports = require('./src/javascript/node-index')
55
}
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
'use strict'
2-
31
function createContext (width, height, options) {
42
width = width | 0
53
height = height | 0
64
if (!(width > 0 && height > 0)) {
75
return null
86
}
97

10-
var canvas = document.createElement('canvas')
8+
const canvas = document.createElement('canvas')
119
if (!canvas) {
1210
return null
1311
}
14-
var gl
12+
let gl
1513
canvas.width = width
1614
canvas.height = height
1715

@@ -25,24 +23,24 @@ function createContext (width, height, options) {
2523
}
2624
}
2725

28-
var _getExtension = gl.getExtension
29-
var extDestroy = {
26+
const _getExtension = gl.getExtension
27+
const extDestroy = {
3028
destroy: function () {
31-
var loseContext = _getExtension.call(gl, 'WEBGL_lose_context')
29+
const loseContext = _getExtension.call(gl, 'WEBGL_lose_context')
3230
if (loseContext) {
3331
loseContext.loseContext()
3432
}
3533
}
3634
}
3735

38-
var extResize = {
36+
const extResize = {
3937
resize: function (w, h) {
4038
canvas.width = w
4139
canvas.height = h
4240
}
4341
}
4442

45-
var _supportedExtensions = gl.getSupportedExtensions().slice()
43+
const _supportedExtensions = gl.getSupportedExtensions().slice()
4644
_supportedExtensions.push(
4745
'STACKGL_destroy_context',
4846
'STACKGL_resize_drawingbuffer')
@@ -51,7 +49,7 @@ function createContext (width, height, options) {
5149
}
5250

5351
gl.getExtension = function (extName) {
54-
var name = extName.toLowerCase()
52+
const name = extName.toLowerCase()
5553
if (name === 'stackgl_resize_drawingbuffer') {
5654
return extResize
5755
}
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
const { gl } = require('../native-gl')
2+
const { vertexCount } = require('../utils')
3+
4+
class ANGLEInstancedArrays {
5+
constructor (ctx) {
6+
this.VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE = 0x88fe
7+
this.ctx = ctx
8+
9+
this._drawArraysInstanced = gl._drawArraysInstanced.bind(ctx)
10+
this._drawElementsInstanced = gl._drawElementsInstanced.bind(ctx)
11+
this._vertexAttribDivisor = gl._vertexAttribDivisor.bind(ctx)
12+
}
13+
14+
drawArraysInstancedANGLE (mode, first, count, primCount) {
15+
const { ctx } = this
16+
mode |= 0
17+
first |= 0
18+
count |= 0
19+
primCount |= 0
20+
if (first < 0 || count < 0 || primCount < 0) {
21+
ctx.setError(gl.INVALID_VALUE)
22+
return
23+
}
24+
if (!ctx._checkStencilState()) {
25+
return
26+
}
27+
const reducedCount = vertexCount(mode, count)
28+
if (reducedCount < 0) {
29+
ctx.setError(gl.INVALID_ENUM)
30+
return
31+
}
32+
if (!ctx._framebufferOk()) {
33+
return
34+
}
35+
if (count === 0 || primCount === 0) {
36+
return
37+
}
38+
let maxIndex = first
39+
if (count > 0) {
40+
maxIndex = (count + first - 1) >>> 0
41+
}
42+
if (this.checkInstancedVertexAttribState(maxIndex, primCount)) {
43+
return this._drawArraysInstanced(mode, first, reducedCount, primCount)
44+
}
45+
}
46+
47+
drawElementsInstancedANGLE (mode, count, type, ioffset, primCount) {
48+
const { ctx } = this
49+
mode |= 0
50+
count |= 0
51+
type |= 0
52+
ioffset |= 0
53+
primCount |= 0
54+
55+
if (count < 0 || ioffset < 0 || primCount < 0) {
56+
ctx.setError(gl.INVALID_VALUE)
57+
return
58+
}
59+
60+
if (!ctx._checkStencilState()) {
61+
return
62+
}
63+
64+
const elementBuffer = ctx._activeElementArrayBuffer
65+
if (!elementBuffer) {
66+
ctx.setError(gl.INVALID_OPERATION)
67+
return
68+
}
69+
70+
// Unpack element data
71+
let elementData = null
72+
let offset = ioffset
73+
if (type === gl.UNSIGNED_SHORT) {
74+
if (offset % 2) {
75+
ctx.setError(gl.INVALID_OPERATION)
76+
return
77+
}
78+
offset >>= 1
79+
elementData = new Uint16Array(elementBuffer._elements.buffer)
80+
} else if (ctx._extensions.oes_element_index_uint && type === gl.UNSIGNED_INT) {
81+
if (offset % 4) {
82+
ctx.setError(gl.INVALID_OPERATION)
83+
return
84+
}
85+
offset >>= 2
86+
elementData = new Uint32Array(elementBuffer._elements.buffer)
87+
} else if (type === gl.UNSIGNED_BYTE) {
88+
elementData = elementBuffer._elements
89+
} else {
90+
ctx.setError(gl.INVALID_ENUM)
91+
return
92+
}
93+
94+
let reducedCount = count
95+
switch (mode) {
96+
case gl.TRIANGLES:
97+
if (count % 3) {
98+
reducedCount -= (count % 3)
99+
}
100+
break
101+
case gl.LINES:
102+
if (count % 2) {
103+
reducedCount -= (count % 2)
104+
}
105+
break
106+
case gl.POINTS:
107+
break
108+
case gl.LINE_LOOP:
109+
case gl.LINE_STRIP:
110+
if (count < 2) {
111+
ctx.setError(gl.INVALID_OPERATION)
112+
return
113+
}
114+
break
115+
case gl.TRIANGLE_FAN:
116+
case gl.TRIANGLE_STRIP:
117+
if (count < 3) {
118+
ctx.setError(gl.INVALID_OPERATION)
119+
return
120+
}
121+
break
122+
default:
123+
ctx.setError(gl.INVALID_ENUM)
124+
return
125+
}
126+
127+
if (!ctx._framebufferOk()) {
128+
return
129+
}
130+
131+
if (count === 0 || primCount === 0) {
132+
this.checkInstancedVertexAttribState(0, 0)
133+
return
134+
}
135+
136+
if ((count + offset) >>> 0 > elementData.length) {
137+
ctx.setError(gl.INVALID_OPERATION)
138+
return
139+
}
140+
141+
// Compute max index
142+
let maxIndex = -1
143+
for (let i = offset; i < offset + count; ++i) {
144+
maxIndex = Math.max(maxIndex, elementData[i])
145+
}
146+
147+
if (maxIndex < 0) {
148+
this.checkInstancedVertexAttribState(0, 0)
149+
return
150+
}
151+
152+
if (this.checkInstancedVertexAttribState(maxIndex, primCount)) {
153+
if (reducedCount > 0) {
154+
this._drawElementsInstanced(mode, reducedCount, type, ioffset, primCount)
155+
}
156+
}
157+
}
158+
159+
vertexAttribDivisorANGLE (index, divisor) {
160+
const { ctx } = this
161+
index |= 0
162+
divisor |= 0
163+
if (divisor < 0 ||
164+
index < 0 || index >= ctx._vertexAttribs.length) {
165+
ctx.setError(gl.INVALID_VALUE)
166+
return
167+
}
168+
const attrib = ctx._vertexAttribs[index]
169+
attrib._divisor = divisor
170+
this._vertexAttribDivisor(index, divisor)
171+
}
172+
173+
checkInstancedVertexAttribState (maxIndex, primCount) {
174+
const { ctx } = this
175+
const program = ctx._activeProgram
176+
if (!program) {
177+
ctx.setError(gl.INVALID_OPERATION)
178+
return false
179+
}
180+
181+
const attribs = ctx._vertexAttribs
182+
let hasZero = false
183+
for (let i = 0; i < attribs.length; ++i) {
184+
const attrib = attribs[i]
185+
if (attrib._isPointer) {
186+
const buffer = attrib._pointerBuffer
187+
if (program._attributes.indexOf(i) >= 0) {
188+
if (!buffer) {
189+
ctx.setError(gl.INVALID_OPERATION)
190+
return false
191+
}
192+
let maxByte = 0
193+
if (attrib._divisor === 0) {
194+
hasZero = true
195+
maxByte = attrib._pointerStride * maxIndex +
196+
attrib._pointerSize +
197+
attrib._pointerOffset
198+
} else {
199+
maxByte = attrib._pointerStride * (Math.ceil(primCount / attrib._divisor) - 1) +
200+
attrib._pointerSize +
201+
attrib._pointerOffset
202+
}
203+
if (maxByte > buffer._size) {
204+
ctx.setError(gl.INVALID_OPERATION)
205+
return false
206+
}
207+
}
208+
}
209+
}
210+
211+
if (!hasZero) {
212+
ctx.setError(gl.INVALID_OPERATION)
213+
return false
214+
}
215+
216+
return true
217+
}
218+
}
219+
220+
function getANGLEInstancedArrays (ctx) {
221+
return new ANGLEInstancedArrays(ctx)
222+
}
223+
224+
module.exports = { ANGLEInstancedArrays, getANGLEInstancedArrays }
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class OESElementIndexUint {}
2+
3+
function getOESElementIndexUint (context) {
4+
let result = null
5+
const exts = context.getSupportedExtensions()
6+
7+
if (exts && exts.indexOf('OES_element_index_uint') >= 0) {
8+
result = new OESElementIndexUint()
9+
}
10+
11+
return result
12+
}
13+
14+
module.exports = { getOESElementIndexUint, OESElementIndexUint }
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class OESTextureFloat {}
2+
3+
function getOESTextureFloat (context) {
4+
let result = null
5+
const exts = context.getSupportedExtensions()
6+
7+
if (exts && exts.indexOf('OES_texture_float') >= 0) {
8+
result = new OESTextureFloat()
9+
}
10+
11+
return result
12+
}
13+
14+
module.exports = { getOESTextureFloat, OESTextureFloat }
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class STACKGLDestroyContext {
2+
constructor (ctx) {
3+
this.destroy = ctx.destroy.bind(ctx)
4+
}
5+
}
6+
7+
function getSTACKGLDestroyContext (ctx) {
8+
return new STACKGLDestroyContext(ctx)
9+
}
10+
11+
module.exports = { getSTACKGLDestroyContext, STACKGLDestroyContext }
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class STACKGLResizeDrawingBuffer {
2+
constructor (ctx) {
3+
this.resize = ctx.resize.bind(ctx)
4+
}
5+
}
6+
7+
function getSTACKGLResizeDrawingBuffer (ctx) {
8+
return new STACKGLResizeDrawingBuffer(ctx)
9+
}
10+
11+
module.exports = { getSTACKGLResizeDrawingBuffer, STACKGLResizeDrawingBuffer }

0 commit comments

Comments
 (0)