Skip to content

Commit

Permalink
Merge pull request xeolabs#475 from tsherif/uniform-cache-fix
Browse files Browse the repository at this point in the history
Prevent uniform cache from referencing outside array
  • Loading branch information
xeolabs committed Apr 8, 2016
2 parents ff492fb + 81b5120 commit ee0c529
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
12 changes: 6 additions & 6 deletions src/core/display/programSourceFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -1075,9 +1075,9 @@ var SceneJS_ProgramSourceFactory = new (function () {
add("lightDist = length( SCENEJS_uLightPos" + i + " - SCENEJS_vWorldVertex.xyz);");

add("attenuation = 1.0 - (" +
" SCENEJS_uLightAttenuation" + i + "[0] + " +
" SCENEJS_uLightAttenuation" + i + "[1] * lightDist + " +
" SCENEJS_uLightAttenuation" + i + "[2] * lightDist * lightDist);");
" SCENEJS_uLightAttenuation" + i + ".x + " +
" SCENEJS_uLightAttenuation" + i + ".y * lightDist + " +
" SCENEJS_uLightAttenuation" + i + ".z * lightDist * lightDist);");

if (light.diffuse) {
add(" lightValue += dotN * SCENEJS_uLightColor" + i + " * attenuation;");
Expand Down Expand Up @@ -1130,9 +1130,9 @@ var SceneJS_ProgramSourceFactory = new (function () {
add("lightDist = length( SCENEJS_uLightPos" + i + " - SCENEJS_vWorldVertex.xyz);");

add("attenuation = 1.0 - (" +
" SCENEJS_uLightAttenuation" + i + "[0] + " +
" SCENEJS_uLightAttenuation" + i + "[1] * lightDist + " +
" SCENEJS_uLightAttenuation" + i + "[2] * lightDist * lightDist);");
" SCENEJS_uLightAttenuation" + i + ".x + " +
" SCENEJS_uLightAttenuation" + i + ".y * lightDist + " +
" SCENEJS_uLightAttenuation" + i + ".z * lightDist * lightDist);");

add("coneDiff = SCENEJS_uOuterCone" + i + " - SCENEJS_uInnerCone" + i + ";");

Expand Down
33 changes: 24 additions & 9 deletions src/core/webgl/uniform.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,41 @@ SceneJS._webgl.Uniform = function (gl, program, name, type, size, location, inde
};

} else if (type === gl.BOOL_VEC2) {
value = new Array(2);

func = function (v) {
if (value !== null && value[0] === v[0] && value[1] === v[1]) {
return;
}
value = v;
value[0] = v[0];
value[1] = v[1];
gl.uniform2iv(location, v);
};

} else if (type === gl.BOOL_VEC3) {
value = new Array(3);

func = function (v) {
if (value !== null && value[0] === v[0] && value[1] === v[1] && value[2] === v[2]) {
return;
}
value = v;
value[0] = v[0];
value[1] = v[1];
value[2] = v[2];
gl.uniform3iv(location, v);
};

} else if (type === gl.BOOL_VEC4) {
value = new Array(4);

func = function (v) {
if (value !== null && value[0] === v[0] && value[1] === v[1] && value[2] === v[2] && value[3] === v[3]) {
return;
}
value = v;
value[0] = v[0];
value[1] = v[1];
value[2] = v[2];
value[3] = v[3];
gl.uniform4iv(location, v);
};

Expand All @@ -55,32 +64,35 @@ SceneJS._webgl.Uniform = function (gl, program, name, type, size, location, inde
};

} else if (type === gl.INT_VEC2) {
value = new Uint32Array(2);

func = function (v) {
if (value !== null && value[0] === v[0] && value[1] === v[1]) {
return;
}
value = v;
value.set(v);
gl.uniform2iv(location, v);
};

} else if (type === gl.INT_VEC3) {
value = new Uint32Array(3);

func = function (v) {
if (value !== null && value[0] === v[0] && value[1] === v[1] && value[2] === v[2]) {
return;
}
value = v;
value.set(v);
gl.uniform3iv(location, v);
};

} else if (type === gl.INT_VEC4) {
value = new Uint32Array(4);

func = function (v) {
if (value !== null && value[0] === v[0] && value[1] === v[1] && value[2] === v[2] && value[3] === v[3]) {
return;
}
value = v;
value.set(v);
gl.uniform4iv(location, v);
};

Expand All @@ -95,32 +107,35 @@ SceneJS._webgl.Uniform = function (gl, program, name, type, size, location, inde
};

} else if (type === gl.FLOAT_VEC2) {
value = new Float32Array(2);

func = function (v) {
if (value !== null && value[0] === v[0] && value[1] === v[1]) {
return;
}
value = v;
value.set(v);
gl.uniform2fv(location, v);
};

} else if (type === gl.FLOAT_VEC3) {
value = new Float32Array(3);

func = function (v) {
if (value !== null && value[0] === v[0] && value[1] === v[1] && value[2] === v[2]) {
return;
}
value = v;
value.set(v);
gl.uniform3fv(location, v);
};

} else if (type === gl.FLOAT_VEC4) {
value = new Float32Array(4);

func = function (v) {
if (value !== null && value[0] === v[0] && value[1] === v[1] && value[2] === v[2] && value[3] === v[3]) {
return;
}
value = v;
value.set(v);
gl.uniform4fv(location, v);
};

Expand Down

0 comments on commit ee0c529

Please sign in to comment.