Skip to content

Converting gl-shader function constructors to normal functions #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ logs
results

npm-debug.log
package-lock.json
node_modules/*
50 changes: 35 additions & 15 deletions lib/create-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,40 @@ Object.defineProperty(proto, 'location', {
}
})

var allFns = [
function (gl, v, x0) {
if (x0.length === undefined) {
return gl.vertexAttrib1f(v, x0)
} else {
return gl.vertexAttrib1fv(v, x0)
}
},

function (gl, v, x0, x1) {
if (x0.length === undefined) {
return gl.vertexAttrib2f(v, x0, x1)
} else {
return gl.vertexAttrib2fv(v, x0)
}
},

function (gl, v, x0, x1, x2) {
if (x0.length === undefined) {
return gl.vertexAttrib3f(v, x0, x1, x2)
} else {
return gl.vertexAttrib3fv(v, x0)
}
},

function (gl, v, x0, x1, x2, x3) {
if (x0.length === undefined) {
return gl.vertexAttrib4f(v, x0, x1, x2, x3)
} else {
return gl.vertexAttrib4fv(v, x0)
}
}
]

//Adds a vector attribute to obj
function addVectorAttribute(
gl
Expand All @@ -68,21 +102,7 @@ function addVectorAttribute(
, obj
, name) {

//Construct constant function
var constFuncArgs = [ 'gl', 'v' ]
var varNames = []
for(var i=0; i<dimension; ++i) {
constFuncArgs.push('x'+i)
varNames.push('x'+i)
}
constFuncArgs.push(
'if(x0.length===void 0){return gl.vertexAttrib' +
dimension + 'f(v,' +
varNames.join() +
')}else{return gl.vertexAttrib' +
dimension +
'fv(v,x0)}')
var constFunc = Function.apply(null, constFuncArgs)
var constFunc = allFns[dimension]

//Create attribute wrapper
var attr = new ShaderAttribute(
Expand Down
130 changes: 74 additions & 56 deletions lib/create-uniforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ module.exports = createUniformWrapper

//Binds a function and returns a value
function identity(x) {
var c = new Function('y', 'return function(){return y}')
return c(x)
return function() {
return x
}
}

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

function makeGetter(index) {
var proc = new Function(
'gl'
, 'wrapper'
, 'locations'
, 'return function(){return gl.getUniform(wrapper.program,locations[' + index + '])}')
return proc(gl, wrapper, locations)
function makeGetter(idx) {
return function(gl, wrapper, locations) {
return gl.getUniform(wrapper.program, locations[idx])
}
}

function makePropSetter(path, index, type) {
switch(type) {
case 'bool':
case 'int':
case 'sampler2D':
case 'samplerCube':
return 'gl.uniform1i(locations[' + index + '],obj' + path + ')'
case 'float':
return 'gl.uniform1f(locations[' + index + '],obj' + path + ')'
default:
var vidx = type.indexOf('vec')
if(0 <= vidx && vidx <= 1 && type.length === 4 + vidx) {
var d = type.charCodeAt(type.length-1) - 48
if(d < 2 || d > 4) {
throw new GLError('', 'Invalid data type')
function makeSetter(type) {
return function updateProperty(obj){
var indices = enumerateIndices('', type)
for(var i=0; i<indices.length; ++i) {
var item = indices[i]
var path = item[0]
var idx = item[1]
if(locations[idx]) {
var objPath = obj
if(typeof path === 'string' && (
path.indexOf('.') === 0 ||
path.indexOf('[') === 0
)) {
var key = path
if(path.indexOf('.') === 0) {
key = path.slice(1)
}

if(key.indexOf(']') === key.length - 1) {
var j = key.indexOf('[')
var k1 = key.slice(0, j)
var k2 = key.slice(j + 1, key.length - 1)
objPath = k1? obj[k1][k2] : obj[k2]
} else {
objPath = obj[key]
}
}
switch(type.charAt(0)) {
case 'b':
case 'i':
return 'gl.uniform' + d + 'iv(locations[' + index + '],obj' + path + ')'
case 'v':
return 'gl.uniform' + d + 'fv(locations[' + index + '],obj' + path + ')'

var t = uniforms[idx].type
var d
switch(t) {
case 'bool':
case 'int':
case 'sampler2D':
case 'samplerCube':
gl.uniform1i(locations[idx], objPath)
break
case 'float':
gl.uniform1f(locations[idx], objPath)
break
default:
throw new GLError('', 'Unrecognized data type for vector ' + name + ': ' + type)
}
} else if(type.indexOf('mat') === 0 && type.length === 4) {
var d = type.charCodeAt(type.length-1) - 48
if(d < 2 || d > 4) {
throw new GLError('', 'Invalid uniform dimension type for matrix ' + name + ': ' + type)
var vidx = t.indexOf('vec')
if(0 <= vidx && vidx <= 1 && t.length === 4 + vidx) {
d = t.charCodeAt(t.length-1) - 48
if(d < 2 || d > 4) {
throw new GLError('', 'Invalid data type')
}
switch(t.charAt(0)) {
case 'b':
case 'i':
gl['uniform' + d + 'iv'](locations[idx], objPath)
break
case 'v':
gl['uniform' + d + 'fv'](locations[idx], objPath)
break
default:
throw new GLError('', 'Unrecognized data type for vector ' + name + ': ' + t)
}
} else if(t.indexOf('mat') === 0 && t.length === 4) {
d = t.charCodeAt(t.length-1) - 48
if(d < 2 || d > 4) {
throw new GLError('', 'Invalid uniform dimension type for matrix ' + name + ': ' + t)
}
gl['uniformMatrix' + d + 'fv'](locations[idx], false, objPath)
break
} else {
throw new GLError('', 'Unknown uniform data type for ' + name + ': ' + t)
}
}
return 'gl.uniformMatrix' + d + 'fv(locations[' + index + '],false,obj' + path + ')'
} else {
throw new GLError('', 'Unknown uniform data type for ' + name + ': ' + type)
}
break
}
}
}

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

function makeSetter(type) {
var code = [ 'return function updateProperty(obj){' ]
var indices = enumerateIndices('', type)
for(var i=0; i<indices.length; ++i) {
var item = indices[i]
var path = item[0]
var idx = item[1]
if(locations[idx]) {
code.push(makePropSetter(path, idx, uniforms[idx].type))
}
}
code.push('return obj}')
var proc = new Function('gl', 'locations', code.join('\n'))
return proc(gl, locations)
}

function defaultValue(type) {
switch(type) {
Expand Down Expand Up @@ -137,7 +156,6 @@ function createUniformWrapper(gl, wrapper, uniforms, locations) {
} else {
throw new GLError('', 'Unknown uniform data type for ' + name + ': ' + type)
}
break
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gl-shader",
"version": "4.2.1",
"version": "4.3.0",
"description": "WebGL shader wrapper",
"main": "index.js",
"directories": {
Expand Down