ccGLStateCache.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /****************************************************************************
  2. Copyright (c) 2011 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2017 Chukong Technologies Inc.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #include "renderer/ccGLStateCache.h"
  24. #include "renderer/CCGLProgram.h"
  25. #include "renderer/CCRenderState.h"
  26. #include "base/CCDirector.h"
  27. #include "base/ccConfig.h"
  28. #include "base/CCConfiguration.h"
  29. NS_CC_BEGIN
  30. static const int MAX_ATTRIBUTES = 16;
  31. static const int MAX_ACTIVE_TEXTURE = 16;
  32. namespace
  33. {
  34. static GLuint s_currentProjectionMatrix = -1;
  35. static uint32_t s_attributeFlags = 0; // 32 attributes max
  36. #if CC_ENABLE_GL_STATE_CACHE
  37. static GLuint s_currentShaderProgram = -1;
  38. static GLuint s_currentBoundTexture[MAX_ACTIVE_TEXTURE] = {(GLuint)-1,(GLuint)-1,(GLuint)-1,(GLuint)-1, (GLuint)-1,(GLuint)-1,(GLuint)-1,(GLuint)-1, (GLuint)-1,(GLuint)-1,(GLuint)-1,(GLuint)-1, (GLuint)-1,(GLuint)-1,(GLuint)-1,(GLuint)-1, };
  39. static GLenum s_blendingSource = -1;
  40. static GLenum s_blendingDest = -1;
  41. static int s_GLServerState = 0;
  42. static GLuint s_VAO = 0;
  43. static GLenum s_activeTexture = -1;
  44. #endif // CC_ENABLE_GL_STATE_CACHE
  45. }
  46. // GL State Cache functions
  47. namespace GL {
  48. void invalidateStateCache( void )
  49. {
  50. Director::getInstance()->resetMatrixStack();
  51. s_currentProjectionMatrix = -1;
  52. s_attributeFlags = 0;
  53. #if CC_ENABLE_GL_STATE_CACHE
  54. s_currentShaderProgram = -1;
  55. for( int i=0; i < MAX_ACTIVE_TEXTURE; i++ )
  56. {
  57. s_currentBoundTexture[i] = -1;
  58. }
  59. s_blendingSource = -1;
  60. s_blendingDest = -1;
  61. s_GLServerState = 0;
  62. s_VAO = 0;
  63. #endif // CC_ENABLE_GL_STATE_CACHE
  64. }
  65. void deleteProgram( GLuint program )
  66. {
  67. #if CC_ENABLE_GL_STATE_CACHE
  68. if(program == s_currentShaderProgram)
  69. {
  70. s_currentShaderProgram = -1;
  71. }
  72. #endif // CC_ENABLE_GL_STATE_CACHE
  73. glDeleteProgram( program );
  74. }
  75. void useProgram( GLuint program )
  76. {
  77. #if CC_ENABLE_GL_STATE_CACHE
  78. if( program != s_currentShaderProgram ) {
  79. s_currentShaderProgram = program;
  80. glUseProgram(program);
  81. }
  82. #else
  83. glUseProgram(program);
  84. #endif // CC_ENABLE_GL_STATE_CACHE
  85. }
  86. static void SetBlending(GLenum sfactor, GLenum dfactor)
  87. {
  88. if (sfactor == GL_ONE && dfactor == GL_ZERO)
  89. {
  90. glDisable(GL_BLEND);
  91. RenderState::StateBlock::_defaultState->setBlend(false);
  92. }
  93. else
  94. {
  95. glEnable(GL_BLEND);
  96. glBlendFunc(sfactor, dfactor);
  97. RenderState::StateBlock::_defaultState->setBlend(true);
  98. RenderState::StateBlock::_defaultState->setBlendSrc((RenderState::Blend)sfactor);
  99. RenderState::StateBlock::_defaultState->setBlendDst((RenderState::Blend)dfactor);
  100. }
  101. }
  102. void blendFunc(GLenum sfactor, GLenum dfactor)
  103. {
  104. #if CC_ENABLE_GL_STATE_CACHE
  105. if (sfactor != s_blendingSource || dfactor != s_blendingDest)
  106. {
  107. s_blendingSource = sfactor;
  108. s_blendingDest = dfactor;
  109. SetBlending(sfactor, dfactor);
  110. }
  111. #else
  112. SetBlending( sfactor, dfactor );
  113. #endif // CC_ENABLE_GL_STATE_CACHE
  114. }
  115. void blendResetToCache(void)
  116. {
  117. glBlendEquation(GL_FUNC_ADD);
  118. #if CC_ENABLE_GL_STATE_CACHE
  119. SetBlending(s_blendingSource, s_blendingDest);
  120. #else
  121. SetBlending(CC_BLEND_SRC, CC_BLEND_DST);
  122. #endif // CC_ENABLE_GL_STATE_CACHE
  123. }
  124. void bindTexture2D(GLuint textureId)
  125. {
  126. GL::bindTexture2DN(0, textureId);
  127. }
  128. void bindTexture2D(Texture2D* texture)
  129. {
  130. GL::bindTexture2DN(0, texture->getName());
  131. auto alphaTexID = texture->getAlphaTextureName();
  132. if (alphaTexID > 0) {
  133. GL::bindTexture2DN(1, alphaTexID);
  134. }
  135. }
  136. void bindTexture2DN(GLuint textureUnit, GLuint textureId)
  137. {
  138. #if CC_ENABLE_GL_STATE_CACHE
  139. CCASSERT(textureUnit < MAX_ACTIVE_TEXTURE, "textureUnit is too big");
  140. if (s_currentBoundTexture[textureUnit] != textureId)
  141. {
  142. s_currentBoundTexture[textureUnit] = textureId;
  143. activeTexture(GL_TEXTURE0 + textureUnit);
  144. glBindTexture(GL_TEXTURE_2D, textureId);
  145. }
  146. #else
  147. glActiveTexture(GL_TEXTURE0 + textureUnit);
  148. glBindTexture(GL_TEXTURE_2D, textureId);
  149. #endif
  150. }
  151. void bindTextureN(GLuint textureUnit, GLuint textureId, GLuint textureType/* = GL_TEXTURE_2D*/)
  152. {
  153. #if CC_ENABLE_GL_STATE_CACHE
  154. CCASSERT(textureUnit < MAX_ACTIVE_TEXTURE, "textureUnit is too big");
  155. if (s_currentBoundTexture[textureUnit] != textureId)
  156. {
  157. s_currentBoundTexture[textureUnit] = textureId;
  158. activeTexture(GL_TEXTURE0 + textureUnit);
  159. glBindTexture(textureType, textureId);
  160. }
  161. #else
  162. glActiveTexture(GL_TEXTURE0 + textureUnit);
  163. glBindTexture(textureType, textureId);
  164. #endif
  165. }
  166. void deleteTexture(GLuint textureId)
  167. {
  168. #if CC_ENABLE_GL_STATE_CACHE
  169. for (size_t i = 0; i < MAX_ACTIVE_TEXTURE; ++i)
  170. {
  171. if (s_currentBoundTexture[i] == textureId)
  172. {
  173. s_currentBoundTexture[i] = -1;
  174. }
  175. }
  176. #endif // CC_ENABLE_GL_STATE_CACHE
  177. glDeleteTextures(1, &textureId);
  178. }
  179. void deleteTextureN(GLuint /*textureUnit*/, GLuint textureId)
  180. {
  181. deleteTexture(textureId);
  182. }
  183. void activeTexture(GLenum texture)
  184. {
  185. #if CC_ENABLE_GL_STATE_CACHE
  186. if(s_activeTexture != texture) {
  187. s_activeTexture = texture;
  188. glActiveTexture(s_activeTexture);
  189. }
  190. #else
  191. glActiveTexture(texture);
  192. #endif
  193. }
  194. void bindVAO(GLuint vaoId)
  195. {
  196. if (Configuration::getInstance()->supportsShareableVAO())
  197. {
  198. #if CC_ENABLE_GL_STATE_CACHE
  199. if (s_VAO != vaoId)
  200. {
  201. s_VAO = vaoId;
  202. glBindVertexArray(vaoId);
  203. }
  204. #else
  205. glBindVertexArray(vaoId);
  206. #endif // CC_ENABLE_GL_STATE_CACHE
  207. }
  208. }
  209. // GL Vertex Attrib functions
  210. void enableVertexAttribs(uint32_t flags)
  211. {
  212. bindVAO(0);
  213. // hardcoded!
  214. for(int i=0; i < MAX_ATTRIBUTES; i++) {
  215. unsigned int bit = 1 << i;
  216. //FIXME:Cache is disabled, try to enable cache as before
  217. bool enabled = (flags & bit) != 0;
  218. bool enabledBefore = (s_attributeFlags & bit) != 0;
  219. if(enabled != enabledBefore)
  220. {
  221. if( enabled )
  222. glEnableVertexAttribArray(i);
  223. else
  224. glDisableVertexAttribArray(i);
  225. }
  226. }
  227. s_attributeFlags = flags;
  228. }
  229. // GL Uniforms functions
  230. void setProjectionMatrixDirty( void )
  231. {
  232. s_currentProjectionMatrix = -1;
  233. }
  234. } // Namespace GL
  235. NS_CC_END