CCMeshCommand.cpp 10 KB

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