Skip to content

Commit 1053247

Browse files
Merge pull request #22 from archmoj/no-new-fn
Converting gl-shader function constructors to normal functions
2 parents be0b583 + 3a80df3 commit 1053247

File tree

4 files changed

+111
-72
lines changed

4 files changed

+111
-72
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ logs
1212
results
1313

1414
npm-debug.log
15+
package-lock.json
1516
node_modules/*

lib/create-attributes.js

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,40 @@ Object.defineProperty(proto, 'location', {
5858
}
5959
})
6060

61+
var allFns = [
62+
function (gl, v, x0) {
63+
if (x0.length === undefined) {
64+
return gl.vertexAttrib1f(v, x0)
65+
} else {
66+
return gl.vertexAttrib1fv(v, x0)
67+
}
68+
},
69+
70+
function (gl, v, x0, x1) {
71+
if (x0.length === undefined) {
72+
return gl.vertexAttrib2f(v, x0, x1)
73+
} else {
74+
return gl.vertexAttrib2fv(v, x0)
75+
}
76+
},
77+
78+
function (gl, v, x0, x1, x2) {
79+
if (x0.length === undefined) {
80+
return gl.vertexAttrib3f(v, x0, x1, x2)
81+
} else {
82+
return gl.vertexAttrib3fv(v, x0)
83+
}
84+
},
85+
86+
function (gl, v, x0, x1, x2, x3) {
87+
if (x0.length === undefined) {
88+
return gl.vertexAttrib4f(v, x0, x1, x2, x3)
89+
} else {
90+
return gl.vertexAttrib4fv(v, x0)
91+
}
92+
}
93+
]
94+
6195
//Adds a vector attribute to obj
6296
function addVectorAttribute(
6397
gl
@@ -68,21 +102,7 @@ function addVectorAttribute(
68102
, obj
69103
, name) {
70104

71-
//Construct constant function
72-
var constFuncArgs = [ 'gl', 'v' ]
73-
var varNames = []
74-
for(var i=0; i<dimension; ++i) {
75-
constFuncArgs.push('x'+i)
76-
varNames.push('x'+i)
77-
}
78-
constFuncArgs.push(
79-
'if(x0.length===void 0){return gl.vertexAttrib' +
80-
dimension + 'f(v,' +
81-
varNames.join() +
82-
')}else{return gl.vertexAttrib' +
83-
dimension +
84-
'fv(v,x0)}')
85-
var constFunc = Function.apply(null, constFuncArgs)
105+
var constFunc = allFns[dimension]
86106

87107
//Create attribute wrapper
88108
var attr = new ShaderAttribute(

lib/create-uniforms.js

Lines changed: 74 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ module.exports = createUniformWrapper
77

88
//Binds a function and returns a value
99
function identity(x) {
10-
var c = new Function('y', 'return function(){return y}')
11-
return c(x)
10+
return function() {
11+
return x
12+
}
1213
}
1314

1415
function makeVector(length, fill) {
@@ -22,50 +23,83 @@ function makeVector(length, fill) {
2223
//Create shims for uniforms
2324
function createUniformWrapper(gl, wrapper, uniforms, locations) {
2425

25-
function makeGetter(index) {
26-
var proc = new Function(
27-
'gl'
28-
, 'wrapper'
29-
, 'locations'
30-
, 'return function(){return gl.getUniform(wrapper.program,locations[' + index + '])}')
31-
return proc(gl, wrapper, locations)
26+
function makeGetter(idx) {
27+
return function(gl, wrapper, locations) {
28+
return gl.getUniform(wrapper.program, locations[idx])
29+
}
3230
}
3331

34-
function makePropSetter(path, index, type) {
35-
switch(type) {
36-
case 'bool':
37-
case 'int':
38-
case 'sampler2D':
39-
case 'samplerCube':
40-
return 'gl.uniform1i(locations[' + index + '],obj' + path + ')'
41-
case 'float':
42-
return 'gl.uniform1f(locations[' + index + '],obj' + path + ')'
43-
default:
44-
var vidx = type.indexOf('vec')
45-
if(0 <= vidx && vidx <= 1 && type.length === 4 + vidx) {
46-
var d = type.charCodeAt(type.length-1) - 48
47-
if(d < 2 || d > 4) {
48-
throw new GLError('', 'Invalid data type')
32+
function makeSetter(type) {
33+
return function updateProperty(obj){
34+
var indices = enumerateIndices('', type)
35+
for(var i=0; i<indices.length; ++i) {
36+
var item = indices[i]
37+
var path = item[0]
38+
var idx = item[1]
39+
if(locations[idx]) {
40+
var objPath = obj
41+
if(typeof path === 'string' && (
42+
path.indexOf('.') === 0 ||
43+
path.indexOf('[') === 0
44+
)) {
45+
var key = path
46+
if(path.indexOf('.') === 0) {
47+
key = path.slice(1)
48+
}
49+
50+
if(key.indexOf(']') === key.length - 1) {
51+
var j = key.indexOf('[')
52+
var k1 = key.slice(0, j)
53+
var k2 = key.slice(j + 1, key.length - 1)
54+
objPath = k1? obj[k1][k2] : obj[k2]
55+
} else {
56+
objPath = obj[key]
57+
}
4958
}
50-
switch(type.charAt(0)) {
51-
case 'b':
52-
case 'i':
53-
return 'gl.uniform' + d + 'iv(locations[' + index + '],obj' + path + ')'
54-
case 'v':
55-
return 'gl.uniform' + d + 'fv(locations[' + index + '],obj' + path + ')'
59+
60+
var t = uniforms[idx].type
61+
var d
62+
switch(t) {
63+
case 'bool':
64+
case 'int':
65+
case 'sampler2D':
66+
case 'samplerCube':
67+
gl.uniform1i(locations[idx], objPath)
68+
break
69+
case 'float':
70+
gl.uniform1f(locations[idx], objPath)
71+
break
5672
default:
57-
throw new GLError('', 'Unrecognized data type for vector ' + name + ': ' + type)
58-
}
59-
} else if(type.indexOf('mat') === 0 && type.length === 4) {
60-
var d = type.charCodeAt(type.length-1) - 48
61-
if(d < 2 || d > 4) {
62-
throw new GLError('', 'Invalid uniform dimension type for matrix ' + name + ': ' + type)
73+
var vidx = t.indexOf('vec')
74+
if(0 <= vidx && vidx <= 1 && t.length === 4 + vidx) {
75+
d = t.charCodeAt(t.length-1) - 48
76+
if(d < 2 || d > 4) {
77+
throw new GLError('', 'Invalid data type')
78+
}
79+
switch(t.charAt(0)) {
80+
case 'b':
81+
case 'i':
82+
gl['uniform' + d + 'iv'](locations[idx], objPath)
83+
break
84+
case 'v':
85+
gl['uniform' + d + 'fv'](locations[idx], objPath)
86+
break
87+
default:
88+
throw new GLError('', 'Unrecognized data type for vector ' + name + ': ' + t)
89+
}
90+
} else if(t.indexOf('mat') === 0 && t.length === 4) {
91+
d = t.charCodeAt(t.length-1) - 48
92+
if(d < 2 || d > 4) {
93+
throw new GLError('', 'Invalid uniform dimension type for matrix ' + name + ': ' + t)
94+
}
95+
gl['uniformMatrix' + d + 'fv'](locations[idx], false, objPath)
96+
break
97+
} else {
98+
throw new GLError('', 'Unknown uniform data type for ' + name + ': ' + t)
99+
}
63100
}
64-
return 'gl.uniformMatrix' + d + 'fv(locations[' + index + '],false,obj' + path + ')'
65-
} else {
66-
throw new GLError('', 'Unknown uniform data type for ' + name + ': ' + type)
67101
}
68-
break
102+
}
69103
}
70104
}
71105

@@ -91,21 +125,6 @@ function createUniformWrapper(gl, wrapper, uniforms, locations) {
91125
return indices
92126
}
93127

94-
function makeSetter(type) {
95-
var code = [ 'return function updateProperty(obj){' ]
96-
var indices = enumerateIndices('', type)
97-
for(var i=0; i<indices.length; ++i) {
98-
var item = indices[i]
99-
var path = item[0]
100-
var idx = item[1]
101-
if(locations[idx]) {
102-
code.push(makePropSetter(path, idx, uniforms[idx].type))
103-
}
104-
}
105-
code.push('return obj}')
106-
var proc = new Function('gl', 'locations', code.join('\n'))
107-
return proc(gl, locations)
108-
}
109128

110129
function defaultValue(type) {
111130
switch(type) {
@@ -137,7 +156,6 @@ function createUniformWrapper(gl, wrapper, uniforms, locations) {
137156
} else {
138157
throw new GLError('', 'Unknown uniform data type for ' + name + ': ' + type)
139158
}
140-
break
141159
}
142160
}
143161

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gl-shader",
3-
"version": "4.2.1",
3+
"version": "4.3.0",
44
"description": "WebGL shader wrapper",
55
"main": "index.js",
66
"directories": {

0 commit comments

Comments
 (0)