Skip to content

Commit

Permalink
Revert "Simpler way to set array type uniform values"
Browse files Browse the repository at this point in the history
  • Loading branch information
mikolalysenko authored Mar 10, 2021
1 parent 8ee9cd5 commit 821ad00
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 290 deletions.
117 changes: 35 additions & 82 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -2560,25 +2560,12 @@ 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 @@ -2632,99 +2619,74 @@ module.exports = function reglCore (
} else {
switch (type) {
case GL_FLOAT:
if (size === 1) {
check.commandType(value, 'number', 'uniform ' + name, env.commandStr)
} else {
check.command(
isArrayLike(value) && (value.length === size),
'uniform ' + name, env.commandStr)
}
check.commandType(value, 'number', 'uniform ' + name, env.commandStr)
infix = '1f'
break
case GL_FLOAT_VEC2:
check.command(
isArrayLike(value) && (value.length && value.length % 2 === 0 && value.length <= size * 2),
isArrayLike(value) && value.length === 2,
'uniform ' + name, env.commandStr)
infix = '2f'
break
case GL_FLOAT_VEC3:
check.command(
isArrayLike(value) && (value.length && value.length % 3 === 0 && value.length <= size * 3),
isArrayLike(value) && value.length === 3,
'uniform ' + name, env.commandStr)
infix = '3f'
break
case GL_FLOAT_VEC4:
check.command(
isArrayLike(value) && (value.length && value.length % 4 === 0 && value.length <= size * 4),
isArrayLike(value) && value.length === 4,
'uniform ' + name, env.commandStr)
infix = '4f'
break
case GL_BOOL:
if (size === 1) {
check.commandType(value, 'boolean', 'uniform ' + name, env.commandStr)
} else {
check.command(
isArrayLike(value) && (value.length === size),
'uniform ' + name, env.commandStr)
}
check.commandType(value, 'boolean', 'uniform ' + name, env.commandStr)
infix = '1i'
break
case GL_INT:
if (size === 1) {
check.commandType(value, 'number', 'uniform ' + name, env.commandStr)
} else {
check.command(
isArrayLike(value) && (value.length === size),
'uniform ' + name, env.commandStr)
}
check.commandType(value, 'number', 'uniform ' + name, env.commandStr)
infix = '1i'
break
case GL_BOOL_VEC2:
check.command(
isArrayLike(value) && (value.length && value.length % 2 === 0 && value.length <= size * 2),
isArrayLike(value) && value.length === 2,
'uniform ' + name, env.commandStr)
infix = '2i'
break
case GL_INT_VEC2:
check.command(
isArrayLike(value) && (value.length && value.length % 2 === 0 && value.length <= size * 2),
isArrayLike(value) && value.length === 2,
'uniform ' + name, env.commandStr)
infix = '2i'
break
case GL_BOOL_VEC3:
check.command(
isArrayLike(value) && (value.length && value.length % 3 === 0 && value.length <= size * 3),
isArrayLike(value) && value.length === 3,
'uniform ' + name, env.commandStr)
infix = '3i'
break
case GL_INT_VEC3:
check.command(
isArrayLike(value) && (value.length && value.length % 3 === 0 && value.length <= size * 3),
isArrayLike(value) && value.length === 3,
'uniform ' + name, env.commandStr)
infix = '3i'
break
case GL_BOOL_VEC4:
check.command(
isArrayLike(value) && (value.length && value.length % 4 === 0 && value.length <= size * 4),
isArrayLike(value) && value.length === 4,
'uniform ' + name, env.commandStr)
infix = '4i'
break
case GL_INT_VEC4:
check.command(
isArrayLike(value) && (value.length && value.length % 4 === 0 && value.length <= size * 4),
isArrayLike(value) && value.length === 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, ',',
value,
isArrayLike(value) ? Array.prototype.slice.call(value) : value,
');')
}
continue
Expand Down Expand Up @@ -2759,24 +2721,20 @@ module.exports = function reglCore (
'bad data or missing for uniform "' + name + '". ' + message)
}

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

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

Expand All @@ -2791,49 +2749,49 @@ module.exports = function reglCore (

switch (type) {
case GL_INT:
checkType('number', size)
checkType('number')
break
case GL_INT_VEC2:
checkVector(2, 'number', size)
checkVector(2, 'number')
break
case GL_INT_VEC3:
checkVector(3, 'number', size)
checkVector(3, 'number')
break
case GL_INT_VEC4:
checkVector(4, 'number', size)
checkVector(4, 'number')
break
case GL_FLOAT:
checkType('number', size)
checkType('number')
break
case GL_FLOAT_VEC2:
checkVector(2, 'number', size)
checkVector(2, 'number')
break
case GL_FLOAT_VEC3:
checkVector(3, 'number', size)
checkVector(3, 'number')
break
case GL_FLOAT_VEC4:
checkVector(4, 'number', size)
checkVector(4, 'number')
break
case GL_BOOL:
checkType('boolean', size)
checkType('boolean')
break
case GL_BOOL_VEC2:
checkVector(2, 'boolean', size)
checkVector(2, 'boolean')
break
case GL_BOOL_VEC3:
checkVector(3, 'boolean', size)
checkVector(3, 'boolean')
break
case GL_BOOL_VEC4:
checkVector(4, 'boolean', size)
checkVector(4, 'boolean')
break
case GL_FLOAT_MAT2:
checkVector(4, 'number', size)
checkVector(4, 'number')
break
case GL_FLOAT_MAT3:
checkVector(9, 'number', size)
checkVector(9, 'number')
break
case GL_FLOAT_MAT4:
checkVector(16, 'number', size)
checkVector(16, 'number')
break
case GL_SAMPLER_2D:
checkTexture(GL_TEXTURE_2D)
Expand Down Expand Up @@ -2908,11 +2866,6 @@ 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: 6 additions & 9 deletions lib/shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,13 @@ 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: 0 additions & 1 deletion test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,3 @@ function updateDOM () {
}

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

0 comments on commit 821ad00

Please sign in to comment.