CCGLStateCache.js 11 KB

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