Skip to content

Commit

Permalink
Only update modified uniforms
Browse files Browse the repository at this point in the history
  • Loading branch information
stalgiag committed Nov 4, 2019
1 parent ab675cd commit 8686a4e
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/webgl/p5.Shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,19 @@ p5.Shader.prototype._loadUniforms = function() {
}
uniform.name = uniformName;
uniform.type = uniformInfo.type;
uniform._cachedData = undefined;
if (uniform.type === gl.SAMPLER_2D) {
uniform.samplerIndex = samplerIndex;
samplerIndex++;
this.samplers.push(uniform);
}
uniform.isArray =
uniform.type === gl.FLOAT_MAT3 ||
uniform.type === gl.FLOAT_MAT4 ||
uniform.type === gl.INT_VEC2 ||
uniform.type === gl.INT_VEC3 ||
uniform.type === gl.INT_VEC4;

this.uniforms[uniformName] = uniform;
}
this._loadedUniforms = true;
Expand All @@ -193,6 +201,7 @@ p5.Shader.prototype.compile = () => {
p5.Shader.prototype.bindShader = function() {
this.init();
if (!this._bound) {
// NEED TO MINIMIZE CALLS TO useProgram();
this.useProgram();
this._bound = true;

Expand Down Expand Up @@ -341,16 +350,29 @@ p5.Shader.prototype.useProgram = function() {
* canvas toggles between a circular gradient of orange and blue vertically. and a circular gradient of red and green moving horizontally when mouse is clicked/pressed.
*/
p5.Shader.prototype.setUniform = function(uniformName, data) {
//@todo update all current gl.uniformXX calls

const uniform = this.uniforms[uniformName];
if (!uniform) {
return;
}
const gl = this._renderer.GL;

if (uniform.isArray) {
if (
uniform._cachedData &&
this._renderer._arraysEqual(uniform._cachedData, data)
) {
return;
} else {
uniform._cachedData = data.slice(0);
}
} else if (uniform._cachedData && uniform._cachedData === data) {
return;
} else {
uniform._cachedData = data;
}

const location = uniform.location;

const gl = this._renderer.GL;
this.useProgram();

switch (uniform.type) {
Expand Down Expand Up @@ -521,4 +543,8 @@ p5.Shader.prototype.enableAttrib = function(
return this;
};

p5.Shader.prototype._isArray = function(uniform) {
return uniform;
};

export default p5.Shader;

0 comments on commit 8686a4e

Please sign in to comment.