Skip to content

Commit 4ed7dc5

Browse files
author
David DeSimone
committed
Porting CCGLProgramState to cocos2d-html5. This will now give the user the ability to bind multiple texture uniforms to a single shader program. It will also allow users to have late-bound uniforms via a callback.
1 parent 6404318 commit 4ed7dc5

19 files changed

+417
-59
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
@@ -2228,6 +2228,14 @@ cc.Node = cc.Class.extend(/** @lends cc.Node# */{
22282228
this._renderCmd.setShaderProgram(newShaderProgram);
22292229
},
22302230

2231+
setGLProgramState: function (glProgramState) {
2232+
this._renderCmd.setGLProgramState(glProgramState);
2233+
},
2234+
2235+
getGLProgramState: function () {
2236+
return this._renderCmd.getGLProgramState();
2237+
},
2238+
22312239
/**
22322240
* Returns the state of OpenGL server side.
22332241
* @function

cocos2d/core/base-nodes/CCNodeCanvasRenderCmd.js

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

514514
if (locFlag & dirtyFlags.orderDirty)
515515
this._dirtyFlag &= ~dirtyFlags.orderDirty;
516-
}
516+
},
517+
518+
setShaderProgram: function (shaderProgram) {
519+
//do nothing.
520+
},
521+
522+
getShaderProgram: function () {
523+
return null;
524+
},
525+
526+
getGLProgramState: function () {
527+
return null;
528+
},
529+
530+
setGLProgramState: function (glProgramState) {
531+
// do nothing
532+
},
517533
};
518534

519535
cc.Node.RenderCmd.prototype.originTransform = cc.Node.RenderCmd.prototype.transform;
@@ -607,14 +623,6 @@ cc.Node.RenderCmd.prototype._originSyncStatus = cc.Node.RenderCmd.prototype._syn
607623
}
608624
};
609625

610-
proto.setShaderProgram = function (shaderProgram) {
611-
//do nothing.
612-
};
613-
614-
proto.getShaderProgram = function () {
615-
return null;
616-
};
617-
618626
//util functions
619627
cc.Node.CanvasRenderCmd._getCompositeOperationByBlendFunc = function (blendFunc) {
620628
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 () { this.setShaderProgram.apply(this, arguments); },
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
@@ -97,8 +97,7 @@
9797
this._matrix.mat[5] = wt.d;
9898
this._matrix.mat[13] = wt.ty;
9999

100-
this._shaderProgram.use();
101-
this._shaderProgram._setUniformForMVPMatrixWithMat4(this._matrix);
100+
this._glProgramState.apply(this._matrix);
102101
context.enableVertexAttribArray(cc.VERTEX_ATTRIB_POSITION);
103102
context.enableVertexAttribArray(cc.VERTEX_ATTRIB_COLOR);
104103
cc.glBlendFunc(node._blendFunc.src, node._blendFunc.dst);
@@ -311,8 +310,7 @@
311310
this._matrix.mat[13] = wt.ty;
312311

313312
//draw gradient layer
314-
this._shaderProgram.use();
315-
this._shaderProgram._setUniformForMVPMatrixWithMat4(this._matrix);
313+
this._glProgramState.apply(this._matrix);
316314
context.enableVertexAttribArray(cc.VERTEX_ATTRIB_POSITION);
317315
context.enableVertexAttribArray(cc.VERTEX_ATTRIB_COLOR);
318316
cc.glBlendFunc(node._blendFunc.src, node._blendFunc.dst);

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
_quadIndexBuffer = null,
@@ -280,18 +280,18 @@ return {
280280
}
281281
var blendSrc = node._blendFunc.src;
282282
var blendDst = node._blendFunc.dst;
283-
var shader = cmd._shaderProgram;
283+
var glProgramState = cmd._glProgramState;
284284
if (_batchedInfo.texture !== texture ||
285285
_batchedInfo.blendSrc !== blendSrc ||
286286
_batchedInfo.blendDst !== blendDst ||
287-
_batchedInfo.shader !== shader) {
287+
_batchedInfo.glProgramState !== glProgramState) {
288288
// Draw batched elements
289289
this._batchRendering();
290290
// Update _batchedInfo
291291
_batchedInfo.texture = texture;
292292
_batchedInfo.blendSrc = blendSrc;
293293
_batchedInfo.blendDst = blendDst;
294-
_batchedInfo.shader = shader;
294+
_batchedInfo.glProgramState = glProgramState;
295295
}
296296

297297
// Upload vertex data
@@ -308,12 +308,12 @@ return {
308308

309309
var gl = cc._renderContext;
310310
var texture = _batchedInfo.texture;
311-
var shader = _batchedInfo.shader;
311+
var glProgramState = _batchedInfo.glProgramState;
312312
var count = _batchingSize / 4;
313313

314-
if (shader) {
315-
shader.use();
316-
shader._updateProjectionUniform();
314+
if (glProgramState) {
315+
glProgramState.apply();
316+
glProgramState.getGLProgram()._updateProjectionUniform();
317317
}
318318

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

cocos2d/core/textures/TexturesWebGL.js

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

342-
self._shaderProgram.use();
343-
self._shaderProgram.setUniformsForBuiltins();
342+
self._glProgramState.apply();
344343

345344
cc.glBindTexture2D(self);
346345

@@ -369,8 +368,7 @@ cc._tmp.WebGLTexture2D = function () {
369368
rect.x, rect.y + rect.height, /*0.0,*/
370369
rect.x + rect.width, rect.y + rect.height /*0.0*/];
371370

372-
self._shaderProgram.use();
373-
self._shaderProgram.setUniformsForBuiltins();
371+
self._glProgramState.apply();
374372

375373
cc.glBindTexture2D(self);
376374

cocos2d/effects/CCGrid.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)