1 /****************************************************************************
  2  Copyright (c) 2010-2012 cocos2d-x.org
  3  Copyright (c) 2008-2010 Ricardo Quesada
  4  Copyright (c) 2011      Zynga Inc.
  5 
  6  http://www.cocos2d-x.org
  7 
  8  Permission is hereby granted, free of charge, to any person obtaining a copy
  9  of this software and associated documentation files (the "Software"), to deal
 10  in the Software without restriction, including without limitation the rights
 11  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 12  copies of the Software, and to permit persons to whom the Software is
 13  furnished to do so, subject to the following conditions:
 14 
 15  The above copyright notice and this permission notice shall be included in
 16  all copies or substantial portions of the Software.
 17 
 18  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 24  THE SOFTWARE.
 25  ****************************************************************************/
 26 
 27 cc._currentProjectionMatrix = -1;
 28 cc._vertexAttribPosition = false;
 29 cc._vertexAttribColor = false;
 30 cc._vertexAttribTexCoords = false;
 31 
 32 if (cc.ENABLE_GL_STATE_CACHE) {
 33     cc.MAX_ACTIVETEXTURE = 16;
 34 
 35     cc._currentShaderProgram = -1;
 36     cc._currentBoundTexture = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1];
 37     cc._blendingSource = -1;
 38     cc._blendingDest = -1;
 39     cc._GLServerState = 0;
 40     if(cc.TEXTURE_ATLAS_USE_VAO)
 41         cc._uVAO = 0;
 42 }
 43 
 44 // GL State Cache functions
 45 
 46 /**
 47  * Invalidates the GL state cache.<br/>
 48  * If CC_ENABLE_GL_STATE_CACHE it will reset the GL state cache.
 49  */
 50 cc.glInvalidateStateCache = function () {
 51     cc.kmGLFreeAll();
 52     cc._currentProjectionMatrix = -1;
 53     cc._vertexAttribPosition = false;
 54     cc._vertexAttribColor = false;
 55     cc._vertexAttribTexCoords = false;
 56     if (cc.ENABLE_GL_STATE_CACHE) {
 57         cc._currentShaderProgram = -1;
 58         for (var i = 0; i < cc.MAX_ACTIVETEXTURE; i++) {
 59             cc._currentBoundTexture[i] = -1;
 60         }
 61         cc._blendingSource = -1;
 62         cc._blendingDest = -1;
 63         cc._GLServerState = 0;
 64     }
 65 };
 66 
 67 /**
 68  * Uses the GL program in case program is different than the current one.<br/>
 69  * If CC_ENABLE_GL_STATE_CACHE is disabled, it will the glUseProgram() directly.
 70  * @param {WebGLProgram} program
 71  */
 72 cc.glUseProgram = function (program) {
 73     if (program !== cc._currentShaderProgram) {
 74         cc._currentShaderProgram = program;
 75         cc._renderContext.useProgram(program);
 76     }
 77 };
 78 
 79 if(!cc.ENABLE_GL_STATE_CACHE){
 80     cc.glUseProgram = function (program) {
 81         cc._renderContext.useProgram(program);
 82     }
 83 }
 84 
 85 /**
 86  * Deletes the GL program. If it is the one that is being used, it invalidates it.<br/>
 87  * If CC_ENABLE_GL_STATE_CACHE is disabled, it will the glDeleteProgram() directly.
 88  * @param {WebGLProgram} program
 89  */
 90 cc.glDeleteProgram = function (program) {
 91     if (cc.ENABLE_GL_STATE_CACHE) {
 92         if (program === cc._currentShaderProgram)
 93             cc._currentShaderProgram = -1;
 94     }
 95     gl.deleteProgram(program);
 96 };
 97 
 98 /**
 99  * Uses a blending function in case it not already used.<br/>
100  * If CC_ENABLE_GL_STATE_CACHE is disabled, it will the glBlendFunc() directly.
101  * @param {Number} sfactor
102  * @param {Number} dfactor
103  */
104 cc.glBlendFunc = function (sfactor, dfactor) {
105     if ((sfactor !== cc._blendingSource) || (dfactor !== cc._blendingDest)) {
106         cc._blendingSource = sfactor;
107         cc._blendingDest = dfactor;
108         cc.setBlending(sfactor, dfactor);
109     }
110 };
111 
112 cc.setBlending = function (sfactor, dfactor) {
113     var ctx = cc._renderContext;
114     if ((sfactor === ctx.ONE) && (dfactor === ctx.ZERO)) {
115         ctx.disable(ctx.BLEND);
116     } else {
117         ctx.enable(ctx.BLEND);
118         cc._renderContext.blendFunc(sfactor,dfactor);
119         //TODO need fix for WebGL
120         //ctx.blendFuncSeparate(ctx.SRC_ALPHA, dfactor, sfactor, dfactor);
121     }
122 };
123 
124 cc.glBlendFuncForParticle = function(sfactor, dfactor) {
125     if ((sfactor !== cc._blendingSource) || (dfactor !== cc._blendingDest)) {
126         cc._blendingSource = sfactor;
127         cc._blendingDest = dfactor;
128         var ctx = cc._renderContext;
129         if ((sfactor === ctx.ONE) && (dfactor === ctx.ZERO)) {
130             ctx.disable(ctx.BLEND);
131         } else {
132             ctx.enable(ctx.BLEND);
133             //TODO need fix for WebGL
134             ctx.blendFuncSeparate(ctx.SRC_ALPHA, dfactor, sfactor, dfactor);
135         }
136     }
137 };
138 
139 if(!cc.ENABLE_GL_STATE_CACHE){
140     cc.glBlendFunc = cc.setBlending;
141 };
142 
143 /**
144  * Resets the blending mode back to the cached state in case you used glBlendFuncSeparate() or glBlendEquation().<br/>
145  * If CC_ENABLE_GL_STATE_CACHE is disabled, it will just set the default blending mode using GL_FUNC_ADD.
146  */
147 cc.glBlendResetToCache = function () {
148     var ctx = cc._renderContext;
149     ctx.blendEquation(ctx.FUNC_ADD);
150     if (cc.ENABLE_GL_STATE_CACHE)
151         cc.setBlending(cc._blendingSource, cc._blendingDest);
152     else
153         cc.setBlending(ctx.BLEND_SRC, ctx.BLEND_DST);
154 };
155 
156 /**
157  * sets the projection matrix as dirty
158  */
159 cc.setProjectionMatrixDirty = function () {
160     cc._currentProjectionMatrix = -1;
161 };
162 
163 /**
164  * <p>
165  *    Will enable the vertex attribs that are passed as flags.  <br/>
166  *    Possible flags:                                           <br/>
167  *    cc.VERTEX_ATTRIB_FLAG_POSITION                             <br/>
168  *    cc.VERTEX_ATTRIB_FLAG_COLOR                                <br/>
169  *    cc.VERTEX_ATTRIB_FLAG_TEX_COORDS                            <br/>
170  *                                                              <br/>
171  *    These flags can be ORed. The flags that are not present, will be disabled.
172  * </p>
173  * @param {cc.VERTEX_ATTRIB_FLAG_POSITION | cc.VERTEX_ATTRIB_FLAG_COLOR | cc.VERTEX_ATTRIB_FLAG_TEX_OORDS} flags
174  */
175 cc.glEnableVertexAttribs = function (flags) {
176     /* Position */
177     var ctx = cc._renderContext;
178     var enablePosition = ( flags & cc.VERTEX_ATTRIB_FLAG_POSITION );
179     if (enablePosition !== cc._vertexAttribPosition) {
180         if (enablePosition)
181             ctx.enableVertexAttribArray(cc.VERTEX_ATTRIB_POSITION);
182         else
183             ctx.disableVertexAttribArray(cc.VERTEX_ATTRIB_POSITION);
184         cc._vertexAttribPosition = enablePosition;
185     }
186 
187     /* Color */
188     var enableColor = (flags & cc.VERTEX_ATTRIB_FLAG_COLOR);
189     if (enableColor !== cc._vertexAttribColor) {
190         if (enableColor)
191             ctx.enableVertexAttribArray(cc.VERTEX_ATTRIB_COLOR);
192         else
193             ctx.disableVertexAttribArray(cc.VERTEX_ATTRIB_COLOR);
194         cc._vertexAttribColor = enableColor;
195     }
196 
197     /* Tex Coords */
198     var enableTexCoords = (flags & cc.VERTEX_ATTRIB_FLAG_TEX_COORDS);
199     if (enableTexCoords !== cc._vertexAttribTexCoords) {
200         if (enableTexCoords)
201             ctx.enableVertexAttribArray(cc.VERTEX_ATTRIB_TEX_COORDS);
202         else
203             ctx.disableVertexAttribArray(cc.VERTEX_ATTRIB_TEX_COORDS);
204         cc._vertexAttribTexCoords = enableTexCoords;
205     }
206 };
207 
208 /**
209  * If the texture is not already bound, it binds it.<br/>
210  * If CC_ENABLE_GL_STATE_CACHE is disabled, it will call glBindTexture() directly.
211  * @param {cc.Texture2D} textureId
212  */
213 cc.glBindTexture2D = function (textureId) {
214     cc.glBindTexture2DN(0, textureId);
215 };
216 
217 /**
218  * If the texture is not already bound to a given unit, it binds it.<br/>
219  * If CC_ENABLE_GL_STATE_CACHE is disabled, it will call glBindTexture() directly.
220  * @param {Number} textureUnit
221  * @param {cc.Texture2D} textureId
222  */
223 cc.glBindTexture2DN = function (textureUnit, textureId) {
224     if (cc._currentBoundTexture[textureUnit] == textureId)
225         return;
226     cc._currentBoundTexture[textureUnit] = textureId;
227 
228     var ctx = cc._renderContext;
229     ctx.activeTexture(ctx.TEXTURE0 + textureUnit);
230     if(textureId)
231         ctx.bindTexture(ctx.TEXTURE_2D, textureId._webTextureObj);
232     else
233         ctx.bindTexture(ctx.TEXTURE_2D, null);
234 };
235 if (!cc.ENABLE_GL_STATE_CACHE){
236     cc.glBindTexture2DN = function (textureUnit, textureId) {
237         var ctx = cc._renderContext;
238         ctx.activeTexture(ctx.TEXTURE0 + textureUnit);
239         if(textureId)
240             ctx.bindTexture(ctx.TEXTURE_2D, textureId._webTextureObj);
241         else
242             ctx.bindTexture(ctx.TEXTURE_2D, null);
243     };
244 }
245 
246 /**
247  * It will delete a given texture. If the texture was bound, it will invalidate the cached. <br/>
248  * If CC_ENABLE_GL_STATE_CACHE is disabled, it will call glDeleteTextures() directly.
249  * @param {WebGLTexture} textureId
250  */
251 cc.glDeleteTexture = function (textureId) {
252     cc.glDeleteTextureN(0, textureId);
253 };
254 
255 /**
256  * It will delete a given texture. If the texture was bound, it will invalidate the cached for the given texture unit.<br/>
257  * If CC_ENABLE_GL_STATE_CACHE is disabled, it will call glDeleteTextures() directly.
258  * @param {Number} textureUnit
259  * @param {WebGLTexture} textureId
260  */
261 cc.glDeleteTextureN = function (textureUnit, textureId) {
262     if (cc.ENABLE_GL_STATE_CACHE) {
263         if (textureId == cc._currentBoundTexture[ textureUnit ])
264             cc._currentBoundTexture[ textureUnit ] = -1;
265     }
266     cc._renderContext.deleteTexture(textureId);
267 };
268 
269 /**
270  * If the vertex array is not already bound, it binds it.<br/>
271  * If CC_ENABLE_GL_STATE_CACHE is disabled, it will call glBindVertexArray() directly.
272  * @param vaoId
273  */
274 cc.glBindVAO = function (vaoId) {
275     if (!cc.TEXTURE_ATLAS_USE_VAO)
276         return;
277 
278     if (cc.ENABLE_GL_STATE_CACHE) {
279         if (cc._uVAO != vaoId) {
280             cc._uVAO = vaoId;
281             //TODO need fixed
282             //glBindVertexArray(vaoId);
283         }
284     } else {
285         //glBindVertexArray(vaoId);
286     }
287 };
288 
289 /**
290  * It will enable / disable the server side GL states.<br/>
291  * If CC_ENABLE_GL_STATE_CACHE is disabled, it will call glEnable() directly.
292  * @param {Number} flags
293  */
294 cc.glEnable = function (flags) {
295     if (cc.ENABLE_GL_STATE_CACHE) {
296         /*var enabled;
297 
298          */
299         /* GL_BLEND */
300         /*
301          if ((enabled = (flags & cc.GL_BLEND)) != (cc._GLServerState & cc.GL_BLEND)) {
302          if (enabled) {
303          cc._renderContext.enable(cc._renderContext.BLEND);
304          cc._GLServerState |= cc.GL_BLEND;
305          } else {
306          cc._renderContext.disable(cc._renderContext.BLEND);
307          cc._GLServerState &= ~cc.GL_BLEND;
308          }
309          }*/
310     } else {
311         /*if ((flags & cc.GL_BLEND))
312          cc._renderContext.enable(cc._renderContext.BLEND);
313          else
314          cc._renderContext.disable(cc._renderContext.BLEND);*/
315     }
316 };
317 
318