Skip to content

Commit d67a8b3

Browse files
committed
optimize uniform cache performance
1 parent 444c237 commit d67a8b3

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

Diff for: cocos2d/shaders/CCGLProgram.js

+18-8
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,18 @@ cc.GLProgram = cc.Class.extend(/** @lends cc.GLProgram# */{
4848

4949
var updated = false;
5050
var element = this._hashForUniforms[name];
51-
var args = Array.isArray(arguments[1]) ? arguments[1] :
52-
Array.prototype.slice.call(arguments, 1);
51+
var args;
52+
if (Array.isArray(arguments[1])) {
53+
args = arguments[1];
54+
} else {
55+
args = new Array(arguments.length - 1);
56+
for (var i = 1; i < arguments.length; i += 1) {
57+
args[i - 1] = arguments[i];
58+
}
59+
}
5360

5461
if (!element || element.length !== args.length) {
55-
this._hashForUniforms[name] = args.slice();
62+
this._hashForUniforms[name] = [].concat(args);
5663
updated = true;
5764
} else {
5865
for (var i = 0; i < args.length; i += 1) {
@@ -260,11 +267,14 @@ cc.GLProgram = cc.Class.extend(/** @lends cc.GLProgram# */{
260267
* cc.UNIFORM_SAMPLER
261268
*/
262269
updateUniforms: function () {
263-
[cc.UNIFORM_PMATRIX_S, cc.UNIFORM_MVMATRIX_S, cc.UNIFORM_MVPMATRIX_S,
264-
cc.UNIFORM_TIME_S, cc.UNIFORM_SINTIME_S, cc.UNIFORM_COSTIME_S,
265-
cc.UNIFORM_RANDOM01_S, cc.UNIFORM_SAMPLER_S].forEach(function(name) {
266-
this._addUniformLocation(name);
267-
}.bind(this));
270+
this._addUniformLocation(cc.UNIFORM_PMATRIX_S);
271+
this._addUniformLocation(cc.UNIFORM_MVMATRIX_S);
272+
this._addUniformLocation(cc.UNIFORM_MVPMATRIX_S);
273+
this._addUniformLocation(cc.UNIFORM_TIME_S);
274+
this._addUniformLocation(cc.UNIFORM_SINTIME_S);
275+
this._addUniformLocation(cc.UNIFORM_COSTIME_S);
276+
this._addUniformLocation(cc.UNIFORM_RANDOM01_S);
277+
this._addUniformLocation(cc.UNIFORM_SAMPLER_S);
268278
this._usesTime = (this._uniforms[cc.UNIFORM_TIME_S] != null || this._uniforms[cc.UNIFORM_SINTIME_S] != null || this._uniforms[cc.UNIFORM_COSTIME_S] != null);
269279

270280
this.use();

Diff for: cocos2d/shaders/CCGLProgramState.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ cc.GLProgramState.prototype = {
163163
this._glprogram._setUniformForMVPMatrixWithMat4(modelView);
164164
}
165165

166-
Object.values(this._uniforms).forEach(function(uniform) {
167-
uniform.apply();
168-
});
166+
for (var name in this._uniforms) {
167+
this._uniforms[name].apply();
168+
};
169169
},
170170

171171
setGLProgram: function setGLProgram(glprogram) {

0 commit comments

Comments
 (0)