CCMeshCommand.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /****************************************************************************
  2. Copyright (c) 2013-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "renderer/CCMeshCommand.h"
  21. #include "base/ccMacros.h"
  22. #include "base/CCConfiguration.h"
  23. #include "base/CCDirector.h"
  24. #include "base/CCEventCustom.h"
  25. #include "base/CCEventListenerCustom.h"
  26. #include "base/CCEventDispatcher.h"
  27. #include "base/CCEventType.h"
  28. #include "2d/CCLight.h"
  29. #include "renderer/ccGLStateCache.h"
  30. #include "renderer/CCGLProgramState.h"
  31. #include "renderer/CCRenderer.h"
  32. #include "renderer/CCTextureAtlas.h"
  33. #include "renderer/CCTexture2D.h"
  34. #include "renderer/CCTechnique.h"
  35. #include "renderer/CCMaterial.h"
  36. #include "renderer/CCPass.h"
  37. #include "xxhash.h"
  38. NS_CC_BEGIN
  39. MeshCommand::MeshCommand()
  40. : _displayColor(1.0f, 1.0f, 1.0f, 1.0f)
  41. , _matrixPalette(nullptr)
  42. , _matrixPaletteSize(0)
  43. , _materialID(0)
  44. , _vao(0)
  45. , _material(nullptr)
  46. , _glProgramState(nullptr)
  47. , _stateBlock(nullptr)
  48. , _textureID(0)
  49. {
  50. _type = RenderCommand::Type::MESH_COMMAND;
  51. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  52. // listen the event that renderer was recreated on Android/WP8
  53. _rendererRecreatedListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, CC_CALLBACK_1(MeshCommand::listenRendererRecreated, this));
  54. Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_rendererRecreatedListener, -1);
  55. #endif
  56. }
  57. void MeshCommand::init(float globalZOrder,
  58. Material* material,
  59. GLuint vertexBuffer,
  60. GLuint indexBuffer,
  61. GLenum primitive,
  62. GLenum indexFormat,
  63. ssize_t indexCount,
  64. const cocos2d::Mat4 &mv,
  65. uint32_t flags)
  66. {
  67. CCASSERT(material, "material cannot be null");
  68. RenderCommand::init(globalZOrder, mv, flags);
  69. _globalOrder = globalZOrder;
  70. _material = material;
  71. _vertexBuffer = vertexBuffer;
  72. _indexBuffer = indexBuffer;
  73. _primitive = primitive;
  74. _indexFormat = indexFormat;
  75. _indexCount = indexCount;
  76. _mv.set(mv);
  77. _is3D = true;
  78. }
  79. void MeshCommand::init(float globalZOrder,
  80. GLuint textureID,
  81. GLProgramState* glProgramState,
  82. RenderState::StateBlock* stateBlock,
  83. GLuint vertexBuffer,
  84. GLuint indexBuffer,
  85. GLenum primitive,
  86. GLenum indexFormat,
  87. ssize_t indexCount,
  88. const cocos2d::Mat4& mv,
  89. uint32_t flags)
  90. {
  91. CCASSERT(glProgramState, "GLProgramState cannot be null");
  92. CCASSERT(stateBlock, "StateBlock cannot be null");
  93. CCASSERT(!_material, "cannot init with GLProgramState if previously inited without GLProgramState");
  94. RenderCommand::init(globalZOrder, mv, flags);
  95. _globalOrder = globalZOrder;
  96. _textureID = textureID;
  97. // weak ref
  98. _glProgramState = glProgramState;
  99. _stateBlock = stateBlock;
  100. _vertexBuffer = vertexBuffer;
  101. _indexBuffer = indexBuffer;
  102. _primitive = primitive;
  103. _indexFormat = indexFormat;
  104. _indexCount = indexCount;
  105. _mv.set(mv);
  106. _is3D = true;
  107. }
  108. void MeshCommand::setDisplayColor(const Vec4& color)
  109. {
  110. CCASSERT(!_material, "If using material, you should set the color as a uniform: use u_color");
  111. _displayColor = color;
  112. }
  113. void MeshCommand::setMatrixPalette(const Vec4* matrixPalette)
  114. {
  115. CCASSERT(!_material, "If using material, you should set the color as a uniform: use u_matrixPalette");
  116. _matrixPalette = matrixPalette;
  117. }
  118. void MeshCommand::setMatrixPaletteSize(int size)
  119. {
  120. CCASSERT(!_material, "If using material, you should set the color as a uniform: use u_matrixPalette with its size");
  121. _matrixPaletteSize = size;
  122. }
  123. MeshCommand::~MeshCommand()
  124. {
  125. releaseVAO();
  126. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  127. Director::getInstance()->getEventDispatcher()->removeEventListener(_rendererRecreatedListener);
  128. #endif
  129. }
  130. void MeshCommand::applyRenderState()
  131. {
  132. CCASSERT(!_material, "Must not be called when using materials");
  133. CCASSERT(_stateBlock, "StateBlock must be non null");
  134. // blend and texture
  135. GL::bindTexture2D(_textureID);
  136. _stateBlock->bind();
  137. }
  138. void MeshCommand::genMaterialID(GLuint texID, void* glProgramState, GLuint vertexBuffer, GLuint indexBuffer, BlendFunc blend)
  139. {
  140. int intArray[7] = {0};
  141. intArray[0] = (int)texID;
  142. *(int**)&intArray[1] = (int*) glProgramState;
  143. intArray[3] = (int) vertexBuffer;
  144. intArray[4] = (int) indexBuffer;
  145. intArray[5] = (int) blend.src;
  146. intArray[6] = (int) blend.dst;
  147. _materialID = XXH32((const void*)intArray, sizeof(intArray), 0);
  148. }
  149. uint32_t MeshCommand::getMaterialID() const
  150. {
  151. return _materialID;
  152. }
  153. void MeshCommand::preBatchDraw()
  154. {
  155. // Do nothing if using material since each pass needs to bind its own VAO
  156. if (!_material)
  157. {
  158. if (Configuration::getInstance()->supportsShareableVAO() && _vao == 0)
  159. buildVAO();
  160. if (_vao)
  161. {
  162. GL::bindVAO(_vao);
  163. }
  164. else
  165. {
  166. glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
  167. // FIXME: Assumes that all the passes in the Material share the same Vertex Attribs
  168. GLProgramState* programState = _material
  169. ? _material->_currentTechnique->_passes.at(0)->getGLProgramState()
  170. : _glProgramState;
  171. programState->applyAttributes();
  172. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
  173. }
  174. }
  175. }
  176. void MeshCommand::batchDraw()
  177. {
  178. if (_material)
  179. {
  180. for(const auto& pass: _material->_currentTechnique->_passes)
  181. {
  182. pass->bind(_mv);
  183. glDrawElements(_primitive, (GLsizei)_indexCount, _indexFormat, 0);
  184. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _indexCount);
  185. pass->unbind();
  186. }
  187. }
  188. else
  189. {
  190. _glProgramState->applyGLProgram(_mv);
  191. // set render state
  192. applyRenderState();
  193. // Draw
  194. glDrawElements(_primitive, (GLsizei)_indexCount, _indexFormat, 0);
  195. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _indexCount);
  196. }
  197. }
  198. void MeshCommand::postBatchDraw()
  199. {
  200. // when using material, unbind is after draw
  201. if (!_material)
  202. {
  203. if (_vao)
  204. {
  205. GL::bindVAO(0);
  206. }
  207. else
  208. {
  209. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  210. glBindBuffer(GL_ARRAY_BUFFER, 0);
  211. }
  212. // restore the default state since we don't know
  213. // if the next command will need the default state or not
  214. RenderState::StateBlock::restore(0);
  215. }
  216. }
  217. void MeshCommand::execute()
  218. {
  219. // Draw without VAO
  220. glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
  221. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
  222. if (_material)
  223. {
  224. for(const auto& pass: _material->_currentTechnique->_passes)
  225. {
  226. pass->bind(_mv, true);
  227. glDrawElements(_primitive, (GLsizei)_indexCount, _indexFormat, 0);
  228. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _indexCount);
  229. pass->unbind();
  230. }
  231. }
  232. else
  233. {
  234. // set render state
  235. _glProgramState->apply(_mv);
  236. applyRenderState();
  237. // Draw
  238. glDrawElements(_primitive, (GLsizei)_indexCount, _indexFormat, 0);
  239. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _indexCount);
  240. }
  241. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  242. glBindBuffer(GL_ARRAY_BUFFER, 0);
  243. }
  244. void MeshCommand::buildVAO()
  245. {
  246. // FIXME: Assumes that all the passes in the Material share the same Vertex Attribs
  247. GLProgramState* programState = (_material != nullptr)
  248. ? _material->_currentTechnique->_passes.at(0)->getGLProgramState()
  249. : _glProgramState;
  250. releaseVAO();
  251. glGenVertexArrays(1, &_vao);
  252. GL::bindVAO(_vao);
  253. glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
  254. auto flags = programState->getVertexAttribsFlags();
  255. for (int i = 0; flags > 0; i++) {
  256. int flag = 1 << i;
  257. if (flag & flags)
  258. glEnableVertexAttribArray(i);
  259. flags &= ~flag;
  260. }
  261. programState->applyAttributes(false);
  262. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
  263. GL::bindVAO(0);
  264. glBindBuffer(GL_ARRAY_BUFFER, 0);
  265. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  266. }
  267. void MeshCommand::releaseVAO()
  268. {
  269. if (_vao)
  270. {
  271. glDeleteVertexArrays(1, &_vao);
  272. _vao = 0;
  273. GL::bindVAO(0);
  274. }
  275. }
  276. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  277. void MeshCommand::listenRendererRecreated(EventCustom* event)
  278. {
  279. _vao = 0;
  280. }
  281. #endif
  282. NS_CC_END