Skip to content

Commit b8a85ea

Browse files
committed
Refactor ShaderCache implementation
1 parent 9f78106 commit b8a85ea

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

cocos2d/shaders/CCShaderCache.js

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,25 @@ cc.shaderCache = /** @lends cc.shaderCache# */{
110110
*/
111111
TYPE_MAX: 11,
112112

113+
_keyMap: [
114+
cc.SHADER_POSITION_TEXTURECOLOR,
115+
cc.SHADER_POSITION_TEXTURECOLORALPHATEST,
116+
cc.SHADER_POSITION_COLOR,
117+
cc.SHADER_POSITION_TEXTURE,
118+
cc.SHADER_POSITION_TEXTURE_UCOLOR,
119+
cc.SHADER_POSITION_TEXTUREA8COLOR,
120+
cc.SHADER_POSITION_UCOLOR,
121+
cc.SHADER_POSITION_LENGTHTEXTURECOLOR,
122+
cc.SHADER_SPRITE_POSITION_TEXTURECOLOR,
123+
cc.SHADER_SPRITE_POSITION_TEXTURECOLORALPHATEST,
124+
cc.SHADER_SPRITE_POSITION_COLOR,
125+
cc.SHADER_SPRITE_POSITION_TEXTURECOLOR_GRAY
126+
],
127+
113128
_programs: {},
114129

115130
_init: function () {
116-
// this.loadDefaultShaders();
131+
this.loadDefaultShaders();
117132
return true;
118133
},
119134

@@ -200,6 +215,10 @@ cc.shaderCache = /** @lends cc.shaderCache# */{
200215
* loads the default shaders
201216
*/
202217
loadDefaultShaders: function () {
218+
for (var i = 0; i < this.TYPE_MAX; ++i) {
219+
var key = this._keyMap[i];
220+
this.programForKey(key);
221+
}
203222
},
204223

205224
/**
@@ -211,62 +230,62 @@ cc.shaderCache = /** @lends cc.shaderCache# */{
211230
// Position Texture Color shader
212231
var program = this.programForKey(cc.SHADER_POSITION_TEXTURECOLOR);
213232
program.reset();
214-
this._loadDefaultShader(program, this.TYPE_POSITION_TEXTURECOLOR);
233+
this._loadDefaultShader(program, cc.SHADER_POSITION_TEXTURECOLOR);
215234

216235
// Sprite Position Texture Color shader
217236
program = this.programForKey(cc.SHADER_SPRITE_POSITION_TEXTURECOLOR);
218237
program.reset();
219-
this._loadDefaultShader(program, this.TYPE_SPRITE_POSITION_TEXTURECOLOR);
238+
this._loadDefaultShader(program, cc.SHADER_SPRITE_POSITION_TEXTURECOLOR);
220239

221240
// Position Texture Color alpha test
222241
program = this.programForKey(cc.SHADER_POSITION_TEXTURECOLORALPHATEST);
223242
program.reset();
224-
this._loadDefaultShader(program, this.TYPE_POSITION_TEXTURECOLOR_ALPHATEST);
243+
this._loadDefaultShader(program, cc.SHADER_POSITION_TEXTURECOLORALPHATEST);
225244

226245
// Sprite Position Texture Color alpha shader
227246
program = this.programForKey(cc.SHADER_SPRITE_POSITION_TEXTURECOLORALPHATEST);
228247
program.reset();
229-
this._loadDefaultShader(program, this.TYPE_SPRITE_POSITION_TEXTURECOLOR_ALPHATEST);
248+
this._loadDefaultShader(program, cc.SHADER_SPRITE_POSITION_TEXTURECOLORALPHATEST);
230249

231250
//
232251
// Position, Color shader
233252
//
234253
program = this.programForKey(cc.SHADER_POSITION_COLOR);
235254
program.reset();
236-
this._loadDefaultShader(program, this.TYPE_POSITION_COLOR);
255+
this._loadDefaultShader(program, cc.SHADER_POSITION_COLOR);
237256

238257
//
239258
// Position Texture shader
240259
//
241260
program = this.programForKey(cc.SHADER_POSITION_TEXTURE);
242261
program.reset();
243-
this._loadDefaultShader(program, this.TYPE_POSITION_TEXTURE);
262+
this._loadDefaultShader(program, cc.SHADER_POSITION_TEXTURE);
244263

245264
//Position Texture Gray shader
246265
program = this.programForKey(cc.SHADER_SPRITE_POSITION_TEXTURE_COLOR_GRAY_FRAG);
247266
program.reset();
248-
this._loadDefaultShader(program, this.TYPE_SPRITE_POSITION_TEXTURECOLOR_GRAY);
267+
this._loadDefaultShader(program, cc.SHADER_SPRITE_POSITION_TEXTURE_COLOR_GRAY_FRAG);
249268

250269
//
251270
// Position, Texture attribs, 1 Color as uniform shader
252271
//
253272
program = this.programForKey(cc.SHADER_POSITION_TEXTURE_UCOLOR);
254273
program.reset();
255-
this._loadDefaultShader(program, this.TYPE_POSITION_TEXTURE_UCOLOR);
274+
this._loadDefaultShader(program, cc.SHADER_POSITION_TEXTURE_UCOLOR);
256275

257276
//
258277
// Position Texture A8 Color shader
259278
//
260279
program = this.programForKey(cc.SHADER_POSITION_TEXTUREA8COLOR);
261280
program.reset();
262-
this._loadDefaultShader(program, this.TYPE_POSITION_TEXTURE_A8COLOR);
281+
this._loadDefaultShader(program, cc.SHADER_POSITION_TEXTUREA8COLOR);
263282

264283
//
265284
// Position and 1 color passed as a uniform (to similate glColor4ub )
266285
//
267286
program = this.programForKey(cc.SHADER_POSITION_UCOLOR);
268287
program.reset();
269-
this._loadDefaultShader(program, this.TYPE_POSITION_UCOLOR);
288+
this._loadDefaultShader(program, cc.SHADER_POSITION_UCOLOR);
270289
},
271290

272291
/**

0 commit comments

Comments
 (0)