CCGLStateCache.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2008-2010 Ricardo Quesada
  4. Copyright (c) 2011 Zynga 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. // ------------------- vertex attrib flags -----------------------------
  23. /**
  24. * @constant
  25. * @type {Number}
  26. */
  27. cc.VERTEX_ATTRIB_FLAG_NONE = 0;
  28. /**
  29. * @constant
  30. * @type {Number}
  31. */
  32. cc.VERTEX_ATTRIB_FLAG_POSITION = 1 << 0;
  33. /**
  34. * @constant
  35. * @type {Number}
  36. */
  37. cc.VERTEX_ATTRIB_FLAG_COLOR = 1 << 1;
  38. /**
  39. * @constant
  40. * @type {Number}
  41. */
  42. cc.VERTEX_ATTRIB_FLAG_TEX_COORDS = 1 << 2;
  43. /**
  44. * @constant
  45. * @type {Number}
  46. */
  47. cc.VERTEX_ATTRIB_FLAG_POS_COLOR_TEX = ( cc.VERTEX_ATTRIB_FLAG_POSITION | cc.VERTEX_ATTRIB_FLAG_COLOR | cc.VERTEX_ATTRIB_FLAG_TEX_COORDS );
  48. /**
  49. * GL server side states
  50. * @constant
  51. * @type {Number}
  52. */
  53. cc.GL_ALL = 0;
  54. cc._currentProjectionMatrix = -1;
  55. cc._vertexAttribPosition = false;
  56. cc._vertexAttribColor = false;
  57. cc._vertexAttribTexCoords = false;
  58. if (cc.ENABLE_GL_STATE_CACHE) {
  59. cc.MAX_ACTIVETEXTURE = 16;
  60. cc._currentShaderProgram = -1;
  61. cc._currentBoundTexture = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1];
  62. cc._blendingSource = -1;
  63. cc._blendingDest = -1;
  64. cc._GLServerState = 0;
  65. if(cc.TEXTURE_ATLAS_USE_VAO)
  66. cc._uVAO = 0;
  67. }
  68. // GL State Cache functions
  69. /**
  70. * Invalidates the GL state cache.<br/>
  71. * If CC_ENABLE_GL_STATE_CACHE it will reset the GL state cache.
  72. */
  73. cc.glInvalidateStateCache = function () {
  74. cc.kmGLFreeAll();
  75. cc._currentProjectionMatrix = -1;
  76. cc._vertexAttribPosition = false;
  77. cc._vertexAttribColor = false;
  78. cc._vertexAttribTexCoords = false;
  79. if (cc.ENABLE_GL_STATE_CACHE) {
  80. cc._currentShaderProgram = -1;
  81. for (var i = 0; i < cc.MAX_ACTIVETEXTURE; i++) {
  82. cc._currentBoundTexture[i] = -1;
  83. }
  84. cc._blendingSource = -1;
  85. cc._blendingDest = -1;
  86. cc._GLServerState = 0;
  87. }
  88. };
  89. /**
  90. * Uses the GL program in case program is different than the current one.<br/>
  91. * If CC_ENABLE_GL_STATE_CACHE is disabled, it will the glUseProgram() directly.
  92. * @param {WebGLProgram} program
  93. */
  94. cc.glUseProgram = function (program) {
  95. if (program !== cc._currentShaderProgram) {
  96. cc._currentShaderProgram = program;
  97. cc.renderContext.useProgram(program);
  98. }
  99. };
  100. if(!cc.ENABLE_GL_STATE_CACHE){
  101. cc.glUseProgram = function (program) {
  102. cc.renderContext.useProgram(program);
  103. }
  104. }
  105. /**
  106. * Deletes the GL program. If it is the one that is being used, it invalidates it.<br/>
  107. * If CC_ENABLE_GL_STATE_CACHE is disabled, it will the glDeleteProgram() directly.
  108. * @param {WebGLProgram} program
  109. */
  110. cc.glDeleteProgram = function (program) {
  111. if (cc.ENABLE_GL_STATE_CACHE) {
  112. if (program === cc._currentShaderProgram)
  113. cc._currentShaderProgram = -1;
  114. }
  115. gl.deleteProgram(program);
  116. };
  117. /**
  118. * Uses a blending function in case it not already used.<br/>
  119. * If CC_ENABLE_GL_STATE_CACHE is disabled, it will the glBlendFunc() directly.
  120. * @param {Number} sfactor
  121. * @param {Number} dfactor
  122. */
  123. cc.glBlendFunc = function (sfactor, dfactor) {
  124. if ((sfactor !== cc._blendingSource) || (dfactor !== cc._blendingDest)) {
  125. cc._blendingSource = sfactor;
  126. cc._blendingDest = dfactor;
  127. cc.setBlending(sfactor, dfactor);
  128. }
  129. };
  130. cc.setBlending = function (sfactor, dfactor) {
  131. var ctx = cc.renderContext;
  132. if ((sfactor === ctx.ONE) && (dfactor === ctx.ZERO)) {
  133. ctx.disable(ctx.BLEND);
  134. } else {
  135. ctx.enable(ctx.BLEND);
  136. cc.renderContext.blendFunc(sfactor,dfactor);
  137. //TODO need fix for WebGL
  138. //ctx.blendFuncSeparate(ctx.SRC_ALPHA, dfactor, sfactor, dfactor);
  139. }
  140. };
  141. cc.glBlendFuncForParticle = function(sfactor, dfactor) {
  142. if ((sfactor !== cc._blendingSource) || (dfactor !== cc._blendingDest)) {
  143. cc._blendingSource = sfactor;
  144. cc._blendingDest = dfactor;
  145. var ctx = cc.renderContext;
  146. if ((sfactor === ctx.ONE) && (dfactor === ctx.ZERO)) {
  147. ctx.disable(ctx.BLEND);
  148. } else {
  149. ctx.enable(ctx.BLEND);
  150. //TODO need fix for WebGL
  151. ctx.blendFuncSeparate(ctx.SRC_ALPHA, dfactor, sfactor, dfactor);
  152. }
  153. }
  154. };
  155. if(!cc.ENABLE_GL_STATE_CACHE){
  156. cc.glBlendFunc = cc.setBlending;
  157. };
  158. /**
  159. * Resets the blending mode back to the cached state in case you used glBlendFuncSeparate() or glBlendEquation().<br/>
  160. * If CC_ENABLE_GL_STATE_CACHE is disabled, it will just set the default blending mode using GL_FUNC_ADD.
  161. */
  162. cc.glBlendResetToCache = function () {
  163. var ctx = cc.renderContext;
  164. ctx.blendEquation(ctx.FUNC_ADD);
  165. if (cc.ENABLE_GL_STATE_CACHE)
  166. cc.setBlending(cc._blendingSource, cc._blendingDest);
  167. else
  168. cc.setBlending(ctx.BLEND_SRC, ctx.BLEND_DST);
  169. };
  170. /**
  171. * sets the projection matrix as dirty
  172. */
  173. cc.setProjectionMatrixDirty = function () {
  174. cc._currentProjectionMatrix = -1;
  175. };
  176. /**
  177. * <p>
  178. * Will enable the vertex attribs that are passed as flags. <br/>
  179. * Possible flags: <br/>
  180. * cc.VERTEX_ATTRIB_FLAG_POSITION <br/>
  181. * cc.VERTEX_ATTRIB_FLAG_COLOR <br/>
  182. * cc.VERTEX_ATTRIB_FLAG_TEX_COORDS <br/>
  183. * <br/>
  184. * These flags can be ORed. The flags that are not present, will be disabled.
  185. * </p>
  186. * @param {cc.VERTEX_ATTRIB_FLAG_POSITION | cc.VERTEX_ATTRIB_FLAG_COLOR | cc.VERTEX_ATTRIB_FLAG_TEX_OORDS} flags
  187. */
  188. cc.glEnableVertexAttribs = function (flags) {
  189. /* Position */
  190. var ctx = cc.renderContext;
  191. var enablePosition = ( flags & cc.VERTEX_ATTRIB_FLAG_POSITION );
  192. if (enablePosition !== cc._vertexAttribPosition) {
  193. if (enablePosition)
  194. ctx.enableVertexAttribArray(cc.VERTEX_ATTRIB_POSITION);
  195. else
  196. ctx.disableVertexAttribArray(cc.VERTEX_ATTRIB_POSITION);
  197. cc._vertexAttribPosition = enablePosition;
  198. }
  199. /* Color */
  200. var enableColor = (flags & cc.VERTEX_ATTRIB_FLAG_COLOR);
  201. if (enableColor !== cc._vertexAttribColor) {
  202. if (enableColor)
  203. ctx.enableVertexAttribArray(cc.VERTEX_ATTRIB_COLOR);
  204. else
  205. ctx.disableVertexAttribArray(cc.VERTEX_ATTRIB_COLOR);
  206. cc._vertexAttribColor = enableColor;
  207. }
  208. /* Tex Coords */
  209. var enableTexCoords = (flags & cc.VERTEX_ATTRIB_FLAG_TEX_COORDS);
  210. if (enableTexCoords !== cc._vertexAttribTexCoords) {
  211. if (enableTexCoords)
  212. ctx.enableVertexAttribArray(cc.VERTEX_ATTRIB_TEX_COORDS);
  213. else
  214. ctx.disableVertexAttribArray(cc.VERTEX_ATTRIB_TEX_COORDS);
  215. cc._vertexAttribTexCoords = enableTexCoords;
  216. }
  217. };
  218. /**
  219. * If the texture is not already bound, it binds it.<br/>
  220. * If CC_ENABLE_GL_STATE_CACHE is disabled, it will call glBindTexture() directly.
  221. * @param {cc.Texture2D} textureId
  222. */
  223. cc.glBindTexture2D = function (textureId) {
  224. cc.glBindTexture2DN(0, textureId);
  225. };
  226. /**
  227. * If the texture is not already bound to a given unit, it binds it.<br/>
  228. * If CC_ENABLE_GL_STATE_CACHE is disabled, it will call glBindTexture() directly.
  229. * @param {Number} textureUnit
  230. * @param {cc.Texture2D} textureId
  231. */
  232. cc.glBindTexture2DN = function (textureUnit, textureId) {
  233. if (cc._currentBoundTexture[textureUnit] == textureId)
  234. return;
  235. cc._currentBoundTexture[textureUnit] = textureId;
  236. var ctx = cc.renderContext;
  237. ctx.activeTexture(ctx.TEXTURE0 + textureUnit);
  238. if(textureId)
  239. ctx.bindTexture(ctx.TEXTURE_2D, textureId._webTextureObj);
  240. else
  241. ctx.bindTexture(ctx.TEXTURE_2D, null);
  242. };
  243. if (!cc.ENABLE_GL_STATE_CACHE){
  244. cc.glBindTexture2DN = function (textureUnit, textureId) {
  245. var ctx = cc.renderContext;
  246. ctx.activeTexture(ctx.TEXTURE0 + textureUnit);
  247. if(textureId)
  248. ctx.bindTexture(ctx.TEXTURE_2D, textureId._webTextureObj);
  249. else
  250. ctx.bindTexture(ctx.TEXTURE_2D, null);
  251. };
  252. }
  253. /**
  254. * It will delete a given texture. If the texture was bound, it will invalidate the cached. <br/>
  255. * If CC_ENABLE_GL_STATE_CACHE is disabled, it will call glDeleteTextures() directly.
  256. * @param {WebGLTexture} textureId
  257. */
  258. cc.glDeleteTexture = function (textureId) {
  259. cc.glDeleteTextureN(0, textureId);
  260. };
  261. /**
  262. * It will delete a given texture. If the texture was bound, it will invalidate the cached for the given texture unit.<br/>
  263. * If CC_ENABLE_GL_STATE_CACHE is disabled, it will call glDeleteTextures() directly.
  264. * @param {Number} textureUnit
  265. * @param {WebGLTexture} textureId
  266. */
  267. cc.glDeleteTextureN = function (textureUnit, textureId) {
  268. if (cc.ENABLE_GL_STATE_CACHE) {
  269. if (textureId == cc._currentBoundTexture[ textureUnit ])
  270. cc._currentBoundTexture[ textureUnit ] = -1;
  271. }
  272. cc.renderContext.deleteTexture(textureId);
  273. };
  274. /**
  275. * If the vertex array is not already bound, it binds it.<br/>
  276. * If CC_ENABLE_GL_STATE_CACHE is disabled, it will call glBindVertexArray() directly.
  277. * @param vaoId
  278. */
  279. cc.glBindVAO = function (vaoId) {
  280. if (!cc.TEXTURE_ATLAS_USE_VAO)
  281. return;
  282. if (cc.ENABLE_GL_STATE_CACHE) {
  283. if (cc._uVAO != vaoId) {
  284. cc._uVAO = vaoId;
  285. //TODO need fixed
  286. //glBindVertexArray(vaoId);
  287. }
  288. } else {
  289. //glBindVertexArray(vaoId);
  290. }
  291. };
  292. /**
  293. * It will enable / disable the server side GL states.<br/>
  294. * If CC_ENABLE_GL_STATE_CACHE is disabled, it will call glEnable() directly.
  295. * @param {Number} flags
  296. */
  297. cc.glEnable = function (flags) {
  298. if (cc.ENABLE_GL_STATE_CACHE) {
  299. /*var enabled;
  300. */
  301. /* GL_BLEND */
  302. /*
  303. if ((enabled = (flags & cc.GL_BLEND)) != (cc._GLServerState & cc.GL_BLEND)) {
  304. if (enabled) {
  305. cc.renderContext.enable(cc.renderContext.BLEND);
  306. cc._GLServerState |= cc.GL_BLEND;
  307. } else {
  308. cc.renderContext.disable(cc.renderContext.BLEND);
  309. cc._GLServerState &= ~cc.GL_BLEND;
  310. }
  311. }*/
  312. } else {
  313. /*if ((flags & cc.GL_BLEND))
  314. cc.renderContext.enable(cc.renderContext.BLEND);
  315. else
  316. cc.renderContext.disable(cc.renderContext.BLEND);*/
  317. }
  318. };