ccGLStateCache.cpp 7.3 KB

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