Skip to content

Commit

Permalink
Merge pull request #591 from fuzhenn/uniform-array
Browse files Browse the repository at this point in the history
Simpler way to set array type uniform values
  • Loading branch information
mikolalysenko authored Feb 24, 2021
2 parents 399197e + 917582f commit 8f318f3
Show file tree
Hide file tree
Showing 4 changed files with 290 additions and 41 deletions.
117 changes: 82 additions & 35 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -2575,12 +2575,25 @@ module.exports = function reglCore (
var shared = env.shared
var GL = shared.gl

var definedArrUniforms = {}
var infix
for (var i = 0; i < uniforms.length; ++i) {
var uniform = uniforms[i]
var name = uniform.name
var type = uniform.info.type
var size = uniform.info.size
var arg = args.uniforms[name]
if (size > 1) {
// either foo[n] or foos, avoid define both
if (!arg) {
continue
}
var arrUniformName = name.replace('[0]', '')
if (definedArrUniforms[arrUniformName]) {
continue
}
definedArrUniforms[arrUniformName] = 1
}
var UNIFORM = env.link(uniform)
var LOCATION = UNIFORM + '.location'

Expand Down Expand Up @@ -2634,74 +2647,99 @@ module.exports = function reglCore (
} else {
switch (type) {
case GL_FLOAT:
check.commandType(value, 'number', 'uniform ' + name, env.commandStr)
if (size === 1) {
check.commandType(value, 'number', 'uniform ' + name, env.commandStr)
} else {
check.command(
isArrayLike(value) && (value.length === size),
'uniform ' + name, env.commandStr)
}
infix = '1f'
break
case GL_FLOAT_VEC2:
check.command(
isArrayLike(value) && value.length === 2,
isArrayLike(value) && (value.length && value.length % 2 === 0 && value.length <= size * 2),
'uniform ' + name, env.commandStr)
infix = '2f'
break
case GL_FLOAT_VEC3:
check.command(
isArrayLike(value) && value.length === 3,
isArrayLike(value) && (value.length && value.length % 3 === 0 && value.length <= size * 3),
'uniform ' + name, env.commandStr)
infix = '3f'
break
case GL_FLOAT_VEC4:
check.command(
isArrayLike(value) && value.length === 4,
isArrayLike(value) && (value.length && value.length % 4 === 0 && value.length <= size * 4),
'uniform ' + name, env.commandStr)
infix = '4f'
break
case GL_BOOL:
check.commandType(value, 'boolean', 'uniform ' + name, env.commandStr)
if (size === 1) {
check.commandType(value, 'boolean', 'uniform ' + name, env.commandStr)
} else {
check.command(
isArrayLike(value) && (value.length === size),
'uniform ' + name, env.commandStr)
}
infix = '1i'
break
case GL_INT:
check.commandType(value, 'number', 'uniform ' + name, env.commandStr)
if (size === 1) {
check.commandType(value, 'number', 'uniform ' + name, env.commandStr)
} else {
check.command(
isArrayLike(value) && (value.length === size),
'uniform ' + name, env.commandStr)
}
infix = '1i'
break
case GL_BOOL_VEC2:
check.command(
isArrayLike(value) && value.length === 2,
isArrayLike(value) && (value.length && value.length % 2 === 0 && value.length <= size * 2),
'uniform ' + name, env.commandStr)
infix = '2i'
break
case GL_INT_VEC2:
check.command(
isArrayLike(value) && value.length === 2,
isArrayLike(value) && (value.length && value.length % 2 === 0 && value.length <= size * 2),
'uniform ' + name, env.commandStr)
infix = '2i'
break
case GL_BOOL_VEC3:
check.command(
isArrayLike(value) && value.length === 3,
isArrayLike(value) && (value.length && value.length % 3 === 0 && value.length <= size * 3),
'uniform ' + name, env.commandStr)
infix = '3i'
break
case GL_INT_VEC3:
check.command(
isArrayLike(value) && value.length === 3,
isArrayLike(value) && (value.length && value.length % 3 === 0 && value.length <= size * 3),
'uniform ' + name, env.commandStr)
infix = '3i'
break
case GL_BOOL_VEC4:
check.command(
isArrayLike(value) && value.length === 4,
isArrayLike(value) && (value.length && value.length % 4 === 0 && value.length <= size * 4),
'uniform ' + name, env.commandStr)
infix = '4i'
break
case GL_INT_VEC4:
check.command(
isArrayLike(value) && value.length === 4,
isArrayLike(value) && (value.length && value.length % 4 === 0 && value.length <= size * 4),
'uniform ' + name, env.commandStr)
infix = '4i'
break
}
if (size > 1) {
infix += 'v'
value = env.global.def('[' +
Array.prototype.slice.call(value) + ']')
} else {
value = isArrayLike(value) ? Array.prototype.slice.call(value) : value
}
scope(GL, '.uniform', infix, '(', LOCATION, ',',
isArrayLike(value) ? Array.prototype.slice.call(value) : value,
value,
');')
}
continue
Expand Down Expand Up @@ -2736,20 +2774,24 @@ module.exports = function reglCore (
'bad data or missing for uniform "' + name + '". ' + message)
}

function checkType (type) {
check(!Array.isArray(VALUE), 'must not specify an array type for uniform')
function checkType (type, size) {
if (size === 1) {
check(!Array.isArray(VALUE), 'must not specify an array type for uniform')
}
emitCheck(
'typeof ' + VALUE + '==="' + type + '"',
'Array.isArray(' + VALUE + ') && typeof ' + VALUE + '[0]===" ' + type + '"' +
' || typeof ' + VALUE + '==="' + type + '"',
'invalid type, expected ' + type)
}

function checkVector (n, type) {
function checkVector (n, type, size) {
if (Array.isArray(VALUE)) {
check(VALUE.length === n, 'must have length ' + n)
check(VALUE.length && VALUE.length % n === 0 && VALUE.length <= n * size, 'must have length of ' + (size === 1 ? '' : 'n * ') + n)
} else {
emitCheck(
shared.isArrayLike + '(' + VALUE + ')&&' + VALUE + '.length===' + n,
'invalid vector, should have length ' + n, env.commandStr)
shared.isArrayLike + '(' + VALUE + ')&&' + VALUE + '.length && ' + VALUE + '.length % ' + n + ' === 0' +
' && ' + VALUE + '.length<=' + n * size,
'invalid vector, should have length of ' + (size === 1 ? '' : 'n * ') + n, env.commandStr)
}
}

Expand All @@ -2764,49 +2806,49 @@ module.exports = function reglCore (

switch (type) {
case GL_INT:
checkType('number')
checkType('number', size)
break
case GL_INT_VEC2:
checkVector(2, 'number')
checkVector(2, 'number', size)
break
case GL_INT_VEC3:
checkVector(3, 'number')
checkVector(3, 'number', size)
break
case GL_INT_VEC4:
checkVector(4, 'number')
checkVector(4, 'number', size)
break
case GL_FLOAT:
checkType('number')
checkType('number', size)
break
case GL_FLOAT_VEC2:
checkVector(2, 'number')
checkVector(2, 'number', size)
break
case GL_FLOAT_VEC3:
checkVector(3, 'number')
checkVector(3, 'number', size)
break
case GL_FLOAT_VEC4:
checkVector(4, 'number')
checkVector(4, 'number', size)
break
case GL_BOOL:
checkType('boolean')
checkType('boolean', size)
break
case GL_BOOL_VEC2:
checkVector(2, 'boolean')
checkVector(2, 'boolean', size)
break
case GL_BOOL_VEC3:
checkVector(3, 'boolean')
checkVector(3, 'boolean', size)
break
case GL_BOOL_VEC4:
checkVector(4, 'boolean')
checkVector(4, 'boolean', size)
break
case GL_FLOAT_MAT2:
checkVector(4, 'number')
checkVector(4, 'number', size)
break
case GL_FLOAT_MAT3:
checkVector(9, 'number')
checkVector(9, 'number', size)
break
case GL_FLOAT_MAT4:
checkVector(16, 'number')
checkVector(16, 'number', size)
break
case GL_SAMPLER_2D:
checkTexture(GL_TEXTURE_2D)
Expand Down Expand Up @@ -2881,6 +2923,11 @@ module.exports = function reglCore (
break
}

if (infix.indexOf('Matrix') === -1 && size > 1) {
infix += 'v'
unroll = 1
}

if (infix.charAt(0) === 'M') {
scope(GL, '.uniform', infix, '(', LOCATION, ',')
var matSize = Math.pow(type - GL_FLOAT_MAT2 + 2, 2)
Expand Down
15 changes: 9 additions & 6 deletions lib/shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,16 @@ module.exports = function wrapShaderState (gl, stringStore, stats, config) {
gl.getUniformLocation(program, name),
info))
}
} else {
insertActiveInfo(uniforms, new ActiveInfo(
info.name,
stringStore.id(info.name),
gl.getUniformLocation(program, info.name),
info))
}
var uniName = info.name
if (info.size > 1) {
uniName = uniName.replace('[0]', '')
}
insertActiveInfo(uniforms, new ActiveInfo(
uniName,
stringStore.id(uniName),
gl.getUniformLocation(program, uniName),
info))
}
}

Expand Down
1 change: 1 addition & 0 deletions test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ function updateDOM () {
}

require('./index')
require('./uniform-array')
Loading

0 comments on commit 8f318f3

Please sign in to comment.