Skip to content

Commit 774e89a

Browse files
authored
Merge pull request #3416 from DavidDeSimone/program-state
Implementation of CCGLProgramState in cocos2d-html5
2 parents 2083141 + a442dd4 commit 774e89a

19 files changed

+411
-62
lines changed

cocos2d/core/CCDrawingPrimitivesWebGL.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ cc.DrawingPrimitiveWebGL = cc.Class.extend(/** @lends cc.DrawingPrimitiveWebGL#
8181
this.lazy_init();
8282

8383
var glContext = this._renderContext;
84-
this._shader.use();
84+
this._glProgramState.apply();
8585
this._shader.setUniformForModelViewAndProjectionMatrixWithMat4();
8686
glContext.enableVertexAttribArray(cc.VERTEX_ATTRIB_POSITION);
8787
this._shader.setUniformLocationWith4fv(this._colorLocation, this._colorArray);
@@ -110,7 +110,7 @@ cc.DrawingPrimitiveWebGL = cc.Class.extend(/** @lends cc.DrawingPrimitiveWebGL#
110110
this.lazy_init();
111111

112112
var glContext = this._renderContext;
113-
this._shader.use();
113+
this._glProgramState.apply();
114114
this._shader.setUniformForModelViewAndProjectionMatrixWithMat4();
115115
glContext.enableVertexAttribArray(cc.VERTEX_ATTRIB_POSITION);
116116
this._shader.setUniformLocationWith4fv(this._colorLocation, this._colorArray);
@@ -145,7 +145,7 @@ cc.DrawingPrimitiveWebGL = cc.Class.extend(/** @lends cc.DrawingPrimitiveWebGL#
145145
this.lazy_init();
146146

147147
var glContext = this._renderContext;
148-
this._shader.use();
148+
this._glProgramState.apply();
149149
this._shader.setUniformForModelViewAndProjectionMatrixWithMat4();
150150
glContext.enableVertexAttribArray(cc.VERTEX_ATTRIB_POSITION);
151151
this._shader.setUniformLocationWith4fv(this._colorLocation, this._colorArray);
@@ -200,7 +200,7 @@ cc.DrawingPrimitiveWebGL = cc.Class.extend(/** @lends cc.DrawingPrimitiveWebGL#
200200
this.lazy_init();
201201

202202
var glContext = this._renderContext;
203-
this._shader.use();
203+
this._glProgramState.apply();
204204
this._shader.setUniformForModelViewAndProjectionMatrixWithMat4();
205205
glContext.enableVertexAttribArray(cc.VERTEX_ATTRIB_POSITION);
206206
this._shader.setUniformLocationWith4fv(this._colorLocation, this._colorArray);
@@ -231,7 +231,7 @@ cc.DrawingPrimitiveWebGL = cc.Class.extend(/** @lends cc.DrawingPrimitiveWebGL#
231231
this.setDrawColor(color.r, color.g, color.b, color.a);
232232

233233
var glContext = this._renderContext;
234-
this._shader.use();
234+
this._glProgramState.apply();
235235
this._shader.setUniformForModelViewAndProjectionMatrixWithMat4();
236236
glContext.enableVertexAttribArray(cc.VERTEX_ATTRIB_POSITION);
237237
this._shader.setUniformLocationWith4fv(this._colorLocation, this._colorArray);
@@ -279,7 +279,7 @@ cc.DrawingPrimitiveWebGL = cc.Class.extend(/** @lends cc.DrawingPrimitiveWebGL#
279279
vertices[(segments + 1) * 2 + 1] = center.y;
280280

281281
var glContext = this._renderContext;
282-
this._shader.use();
282+
this._glProgramState.apply();
283283
this._shader.setUniformForModelViewAndProjectionMatrixWithMat4();
284284
glContext.enableVertexAttribArray(cc.VERTEX_ATTRIB_POSITION);
285285
this._shader.setUniformLocationWith4fv(this._colorLocation, this._colorArray);
@@ -317,7 +317,7 @@ cc.DrawingPrimitiveWebGL = cc.Class.extend(/** @lends cc.DrawingPrimitiveWebGL#
317317
vertices[segments * 2 + 1] = destination.y;
318318

319319
var glContext = this._renderContext;
320-
this._shader.use();
320+
this._glProgramState.apply();
321321
this._shader.setUniformForModelViewAndProjectionMatrixWithMat4();
322322
glContext.enableVertexAttribArray(cc.VERTEX_ATTRIB_POSITION);
323323
this._shader.setUniformLocationWith4fv(this._colorLocation, this._colorArray);
@@ -356,7 +356,7 @@ cc.DrawingPrimitiveWebGL = cc.Class.extend(/** @lends cc.DrawingPrimitiveWebGL#
356356
vertices[segments * 2 + 1] = destination.y;
357357

358358
var glContext = this._renderContext;
359-
this._shader.use();
359+
this._glProgramState.apply();
360360
this._shader.setUniformForModelViewAndProjectionMatrixWithMat4();
361361
glContext.enableVertexAttribArray(cc.VERTEX_ATTRIB_POSITION);
362362
this._shader.setUniformLocationWith4fv(this._colorLocation, this._colorArray);
@@ -416,7 +416,7 @@ cc.DrawingPrimitiveWebGL = cc.Class.extend(/** @lends cc.DrawingPrimitiveWebGL#
416416
}
417417

418418
var glContext = this._renderContext;
419-
this._shader.use();
419+
this._glProgramState.apply();
420420
this._shader.setUniformForModelViewAndProjectionMatrixWithMat4();
421421
glContext.enableVertexAttribArray(cc.VERTEX_ATTRIB_POSITION);
422422
this._shader.setUniformLocationWith4fv(this._colorLocation, this._colorArray);

cocos2d/core/base-nodes/CCAtlasNodeWebGLRenderCmd.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@
6868
this._matrix.mat[5] = wt.d;
6969
this._matrix.mat[13] = wt.ty;
7070

71-
this._shaderProgram.use();
72-
this._shaderProgram._setUniformForMVPMatrixWithMat4(this._matrix);
71+
this._glProgramState.apply(this._matrix);
7372

7473
cc.glBlendFunc(node._blendFunc.src, node._blendFunc.dst);
7574
if (this._uniformColor && this._colorF32Array) {

cocos2d/core/base-nodes/CCNode.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2240,6 +2240,14 @@ cc.Node = cc.Class.extend(/** @lends cc.Node# */{
22402240
this._renderCmd.setShaderProgram(newShaderProgram);
22412241
},
22422242

2243+
setGLProgramState: function (glProgramState) {
2244+
this._renderCmd.setGLProgramState(glProgramState);
2245+
},
2246+
2247+
getGLProgramState: function () {
2248+
return this._renderCmd.getGLProgramState();
2249+
},
2250+
22432251
/**
22442252
* Returns the state of OpenGL server side.
22452253
* @function

cocos2d/core/base-nodes/CCNodeCanvasRenderCmd.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,23 @@ cc.Node.RenderCmd.prototype = {
529529

530530
if (locFlag & dirtyFlags.orderDirty)
531531
this._dirtyFlag &= ~dirtyFlags.orderDirty;
532-
}
532+
},
533+
534+
setShaderProgram: function (shaderProgram) {
535+
//do nothing.
536+
},
537+
538+
getShaderProgram: function () {
539+
return null;
540+
},
541+
542+
getGLProgramState: function () {
543+
return null;
544+
},
545+
546+
setGLProgramState: function (glProgramState) {
547+
// do nothing
548+
},
533549
};
534550

535551
cc.Node.RenderCmd.prototype.originTransform = cc.Node.RenderCmd.prototype.transform;
@@ -623,14 +639,6 @@ cc.Node.RenderCmd.prototype._originSyncStatus = cc.Node.RenderCmd.prototype._syn
623639
}
624640
};
625641

626-
proto.setShaderProgram = function (shaderProgram) {
627-
//do nothing.
628-
};
629-
630-
proto.getShaderProgram = function () {
631-
return null;
632-
};
633-
634642
//util functions
635643
cc.Node.CanvasRenderCmd._getCompositeOperationByBlendFunc = function (blendFunc) {
636644
if (!blendFunc)

cocos2d/core/base-nodes/CCNodeWebGLRenderCmd.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
this._node = renderable;
2828
this._anchorPointInPoints = {x: 0, y: 0};
2929
this._displayedColor = cc.color(255, 255, 255, 255);
30-
this._shaderProgram = null;
30+
this._glProgramState = null;
3131
};
3232

3333
var proto = cc.Node.WebGLRenderCmd.prototype = Object.create(cc.Node.RenderCmd.prototype);
@@ -38,10 +38,26 @@
3838
};
3939

4040
proto.setShaderProgram = function (shaderProgram) {
41-
this._shaderProgram = shaderProgram;
41+
this._glProgramState = cc.GLProgramState.getOrCreateWithGLProgram(shaderProgram);
4242
};
4343

4444
proto.getShaderProgram = function () {
45-
return this._shaderProgram;
45+
return this._glProgramState ? this._glProgramState.getGLProgram() : null;
4646
};
47+
48+
proto.getGLProgramState = function () {
49+
return this._glProgramState;
50+
};
51+
52+
proto.setGLProgramState = function (glProgramState) {
53+
this._glProgramState = glProgramState;
54+
};
55+
56+
// Use a property getter/setter for backwards compatability, and
57+
// to ease the transition from using glPrograms directly, to
58+
// using glProgramStates.
59+
Object.defineProperty(proto, '_shaderProgram', {
60+
set: function (value) { this.setShaderProgram(value); },
61+
get: function () { return this.getShaderProgram(); }
62+
});
4763
})();

cocos2d/core/layers/CCLayerWebGLRenderCmd.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@
136136
this._dataDirty = false;
137137
}
138138

139-
this._shaderProgram.use();
140-
this._shaderProgram._setUniformForMVPMatrixWithMat4(this._matrix);
139+
this._glProgramState.apply(this._matrix);
141140
cc.glBlendFunc(node._blendFunc.src, node._blendFunc.dst);
142141

143142
gl.bindBuffer(gl.ARRAY_BUFFER, this._vertexBuffer);
@@ -302,8 +301,7 @@
302301
}
303302

304303
//draw gradient layer
305-
this._shaderProgram.use();
306-
this._shaderProgram._setUniformForMVPMatrixWithMat4(this._matrix);
304+
this._glProgramState.apply(this._matrix);
307305
cc.glBlendFunc(node._blendFunc.src, node._blendFunc.dst);
308306

309307
gl.bindBuffer(gl.ARRAY_BUFFER, this._vertexBuffer);

cocos2d/core/platform/CCMacro.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ cc.nodeDrawSetup = function (node) {
175175
//cc.glEnable(node._glServerState);
176176
if (node._shaderProgram) {
177177
//cc._renderContext.useProgram(node._shaderProgram._programObj);
178-
node._shaderProgram.use();
178+
node._glProgramState.apply();
179179
node._shaderProgram.setUniformForModelViewAndProjectionMatrixWithMat4();
180180
}
181181
};

cocos2d/core/renderer/RendererWebGL.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ var _batchedInfo = {
3333
blendSrc: null,
3434
// The batched blend destination, all batching element should have the same blend destination
3535
blendDst: null,
36-
// The batched shader, all batching element should have the same shader
37-
shader: null
36+
// The batched glProgramState, all batching element should have the same glProgramState
37+
glProgramState: null
3838
},
3939

4040
_batchBroken = false,
@@ -342,19 +342,19 @@ return {
342342
var texture = node._texture || (node._spriteFrame ? node._spriteFrame._texture : null);
343343
var blendSrc = node._blendFunc.src;
344344
var blendDst = node._blendFunc.dst;
345-
var shader = cmd._shaderProgram;
345+
var glProgramState = cmd._glProgramState;
346346
if (_batchBroken ||
347347
_batchedInfo.texture !== texture ||
348348
_batchedInfo.blendSrc !== blendSrc ||
349349
_batchedInfo.blendDst !== blendDst ||
350-
_batchedInfo.shader !== shader) {
350+
_batchedInfo.glProgramState !== glProgramState) {
351351
// Draw batched elements
352352
this._batchRendering();
353353
// Update _batchedInfo
354354
_batchedInfo.texture = texture;
355355
_batchedInfo.blendSrc = blendSrc;
356356
_batchedInfo.blendDst = blendDst;
357-
_batchedInfo.shader = shader;
357+
_batchedInfo.glProgramState = glProgramState;
358358
_batchBroken = false;
359359
}
360360

@@ -403,12 +403,12 @@ return {
403403

404404
var gl = cc._renderContext;
405405
var texture = _batchedInfo.texture;
406-
var shader = _batchedInfo.shader;
406+
var glProgramState = _batchedInfo.glProgramState;
407407
var uploadAll = _batchingSize > _maxVertexSize * 0.5;
408408

409-
if (shader) {
410-
shader.use();
411-
shader._updateProjectionUniform();
409+
if (glProgramState) {
410+
glProgramState.apply();
411+
glProgramState.getGLProgram()._updateProjectionUniform();
412412
}
413413

414414
cc.glBlendFunc(_batchedInfo.blendSrc, _batchedInfo.blendDst);

cocos2d/core/textures/TexturesWebGL.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,8 @@ cc._tmp.WebGLTexture2D = function () {
340340
point.x, height + point.y, 0.0,
341341
width + point.x, height + point.y, 0.0];
342342

343-
self._shaderProgram.use();
344-
self._shaderProgram.setUniformsForBuiltins();
343+
self._glProgramState.apply();
344+
self._glProgramState._glprogram.setUniformsForBuiltins();
345345

346346
cc.glBindTexture2D(self);
347347

@@ -370,8 +370,8 @@ cc._tmp.WebGLTexture2D = function () {
370370
rect.x, rect.y + rect.height, /*0.0,*/
371371
rect.x + rect.width, rect.y + rect.height /*0.0*/];
372372

373-
self._shaderProgram.use();
374-
self._shaderProgram.setUniformsForBuiltins();
373+
self._glProgramState.apply();
374+
self._glProgramState._glprogram.setUniformsForBuiltins();
375375

376376
cc.glBindTexture2D(self);
377377

cocos2d/effects/CCGrid.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ cc.GridBase = cc.Class.extend(/** @lends cc.GridBase# */{
3939
_step: null,
4040
_grabber: null,
4141
_isTextureFlipped: false,
42-
_shaderProgram: null,
42+
_glProgramState: null,
4343
_directorProjection: 0,
4444

4545
_dirty: false,
@@ -62,7 +62,7 @@ cc.GridBase = cc.Class.extend(/** @lends cc.GridBase# */{
6262
this._step = cc.p(0, 0);
6363
this._grabber = null;
6464
this._isTextureFlipped = false;
65-
this._shaderProgram = null;
65+
this._glProgramState = null;
6666
this._directorProjection = 0;
6767
this._dirty = false;
6868

@@ -227,7 +227,7 @@ cc.GridBase = cc.Class.extend(/** @lends cc.GridBase# */{
227227
if (!this._grabber)
228228
return false;
229229
this._grabber.grab(this._texture);
230-
this._shaderProgram = cc.shaderCache.programForKey(cc.SHADER_POSITION_TEXTURE);
230+
this._glProgramState = cc.GLProgramState.getOrCreateWithGLProgram(cc.shaderCache.programForKey(cc.SHADER_POSITION_TEXTURE));
231231
this.calculateVertexPoints();
232232
return true;
233233
},
@@ -443,9 +443,7 @@ cc.Grid3D = cc.GridBase.extend(/** @lends cc.Grid3D# */{
443443
this._matrix.mat[5] = wt.d;
444444
this._matrix.mat[13] = wt.ty;
445445

446-
this._shaderProgram.use();
447-
//this._shaderProgram.setUniformsForBuiltins();
448-
this._shaderProgram._setUniformForMVPMatrixWithMat4(this._matrix);
446+
this._glProgramState.apply(this._matrix);
449447

450448
var gl = cc._renderContext, locDirty = this._dirty;
451449

@@ -714,9 +712,7 @@ cc.TiledGrid3D = cc.GridBase.extend(/** @lends cc.TiledGrid3D# */{
714712
this._matrix.mat[5] = wt.d;
715713
this._matrix.mat[13] = wt.ty;
716714

717-
this._shaderProgram.use();
718-
this._shaderProgram._setUniformForMVPMatrixWithMat4(this._matrix);
719-
//this._shaderProgram.setUniformsForBuiltins();
715+
this._glProgramState.apply(this._matrix);
720716

721717
//
722718
// Attributes

cocos2d/motion-streak/CCMotionStreakWebGLRenderCmd.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ cc.MotionStreak.WebGLRenderCmd.prototype.rendering = function (ctx) {
4949
this._matrix.mat[5] = wt.d;
5050
this._matrix.mat[13] = wt.ty;
5151

52-
this._shaderProgram.use();
53-
this._shaderProgram._setUniformForMVPMatrixWithMat4(this._matrix);
52+
this._glProgramState.apply(this._matrix);
5453
cc.glBlendFunc(node._blendFunc.src, node._blendFunc.dst);
5554

5655
cc.glBindTexture2D(node.texture);

cocos2d/particle/CCParticleBatchNodeWebGLRenderCmd.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@
4949
this._matrix.mat[5] = wt.d;
5050
this._matrix.mat[13] = wt.ty;
5151

52-
this._shaderProgram.use();
53-
this._shaderProgram._setUniformForMVPMatrixWithMat4(this._matrix);
52+
this._glProgramState.apply(this._matrix);
5453
cc.glBlendFuncForParticle(_t._blendFunc.src, _t._blendFunc.dst);
5554
_t.textureAtlas.drawQuads();
5655
};

cocos2d/particle/CCParticleSystemWebGLRenderCmd.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,7 @@
204204
this._matrix.mat[5] = wt.d;
205205
this._matrix.mat[13] = wt.ty;
206206

207-
this._shaderProgram.use();
208-
this._shaderProgram._setUniformForMVPMatrixWithMat4(this._matrix); //;
207+
this._glProgramState.apply(this._matrix);
209208

210209
cc.glBindTexture2D(node._texture);
211210
cc.glBlendFuncForParticle(node._blendFunc.src, node._blendFunc.dst);

cocos2d/physics/CCPhysicsDebugNodeWebGLRenderCmd.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@
5454

5555
//cc.DrawNode.prototype.draw.call(node);
5656
cc.glBlendFunc(node._blendFunc.src, node._blendFunc.dst);
57-
this._shaderProgram.use();
58-
this._shaderProgram._setUniformForMVPMatrixWithMat4(this._matrix);
57+
this._glProgramState.apply(this._matrix);
5958
node._render();
6059

6160
node.clear();

cocos2d/progress-timer/CCProgressTimerWebGLRenderCmd.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
if (this._vertexDataCount === 0 || !node._sprite)
6565
return;
6666

67-
this._shaderProgram.use();
67+
this._glProgramState.apply();
6868
this._shaderProgram._updateProjectionUniform();
6969

7070
var blendFunc = node._sprite._blendFunc;

cocos2d/shaders/CCGLProgram.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ cc.GLProgram = cc.Class.extend(/** @lends cc.GLProgram# */{
231231
}
232232

233233
this._glContext.linkProgram(this._programObj);
234-
234+
235235
if (this._vertShader)
236236
this._glContext.deleteShader(this._vertShader);
237237
if (this._fragShader)

0 commit comments

Comments
 (0)