Assembler.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /****************************************************************************
  2. Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
  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 "Assembler.hpp"
  21. #include "../NodeProxy.hpp"
  22. #include "../ModelBatcher.hpp"
  23. #include "../MeshBuffer.hpp"
  24. #include "../../renderer/Scene.h"
  25. #include "math/CCMath.h"
  26. #include "cocos/scripting/js-bindings/jswrapper/SeApi.h"
  27. #include "cocos/scripting/js-bindings/manual/jsb_conversions.hpp"
  28. #include "cocos/scripting/js-bindings/auto/jsb_renderer_auto.hpp"
  29. #include "../RenderFlow.hpp"
  30. RENDERER_BEGIN
  31. Assembler::IARenderData::IARenderData()
  32. {
  33. }
  34. Assembler::IARenderData::IARenderData(const IARenderData& o)
  35. {
  36. meshIndex = o.meshIndex;
  37. verticesStart = o.verticesStart;
  38. verticesCount = o.verticesCount;
  39. indicesStart = o.indicesStart;
  40. indicesCount = o.indicesCount;
  41. setEffect(o.getEffect());
  42. }
  43. Assembler::IARenderData::~IARenderData()
  44. {
  45. CC_SAFE_RELEASE(_effect);
  46. }
  47. void Assembler::IARenderData::setEffect(EffectVariant* effect)
  48. {
  49. if (effect == _effect) return;
  50. CC_SAFE_RELEASE(_effect);
  51. _effect = effect;
  52. CC_SAFE_RETAIN(_effect);
  53. }
  54. EffectVariant* Assembler::IARenderData::getEffect() const
  55. {
  56. return _effect;
  57. }
  58. Assembler::Assembler()
  59. {
  60. }
  61. Assembler::~Assembler()
  62. {
  63. CC_SAFE_RELEASE_NULL(_datas);
  64. CC_SAFE_RELEASE(_vfmt);
  65. }
  66. void Assembler::updateMeshIndex(std::size_t iaIndex, int meshIndex)
  67. {
  68. if (iaIndex >= _iaDatas.size())
  69. {
  70. _iaDatas.resize(iaIndex + 1);
  71. }
  72. IARenderData& ia = _iaDatas[iaIndex];
  73. ia.meshIndex = meshIndex;
  74. }
  75. void Assembler::updateIndicesRange(std::size_t iaIndex, int start, int count)
  76. {
  77. if (iaIndex >= _iaDatas.size())
  78. {
  79. _iaDatas.resize(iaIndex + 1);
  80. }
  81. IARenderData& ia = _iaDatas[iaIndex];
  82. ia.indicesStart = start;
  83. ia.indicesCount = count;
  84. }
  85. void Assembler::updateVerticesRange(std::size_t iaIndex, int start, int count)
  86. {
  87. if (iaIndex >= _iaDatas.size())
  88. {
  89. _iaDatas.resize(iaIndex + 1);
  90. }
  91. IARenderData& ia = _iaDatas[iaIndex];
  92. ia.verticesStart = start;
  93. ia.verticesCount = count;
  94. enableDirty(AssemblerBase::VERTICES_OPACITY_CHANGED);
  95. }
  96. void Assembler::updateEffect(std::size_t iaIndex, EffectVariant* effect)
  97. {
  98. if (iaIndex >= _iaDatas.size())
  99. {
  100. _iaDatas.resize(iaIndex + 1);
  101. }
  102. IARenderData& ia = _iaDatas[iaIndex];
  103. ia.setEffect(effect);
  104. }
  105. void Assembler::reset()
  106. {
  107. _iaDatas.clear();
  108. }
  109. void Assembler::handle(NodeProxy *node, ModelBatcher* batcher, Scene* scene)
  110. {
  111. batcher->commit(node, this, node->getCullingMask());
  112. }
  113. void Assembler::fillBuffers(NodeProxy* node, ModelBatcher* batcher, std::size_t index)
  114. {
  115. if(!_datas || !_vfmt)
  116. {
  117. return;
  118. }
  119. MeshBuffer* buffer = batcher->getBuffer(_vfmt);
  120. const IARenderData& ia = _iaDatas[index];
  121. std::size_t meshIndex = ia.meshIndex >= 0 ? ia.meshIndex : index;
  122. RenderData* data = _datas->getRenderData(meshIndex);
  123. if (!data)
  124. {
  125. return;
  126. }
  127. CCASSERT(data->getVBytes() % _bytesPerVertex == 0, "Assembler::fillBuffers vertices data doesn't follow vertex format");
  128. uint32_t vertexCount = ia.verticesCount >= 0 ? (uint32_t)ia.verticesCount : (uint32_t)data->getVBytes() / _bytesPerVertex;
  129. uint32_t indexCount = ia.indicesCount >= 0 ? (uint32_t)ia.indicesCount : (uint32_t)data->getIBytes() / sizeof(unsigned short);
  130. uint32_t vertexStart = (uint32_t)ia.verticesStart;
  131. // must retrieve offset before request
  132. auto& bufferOffset = buffer->request(vertexCount, indexCount);
  133. uint32_t vBufferOffset = bufferOffset.vByte / sizeof(float);
  134. uint32_t indexId = bufferOffset.index;
  135. uint32_t vertexId = bufferOffset.vertex;
  136. uint32_t vertexOffset = vertexId - vertexStart;
  137. uint32_t num = _vfPos->num;
  138. float* worldVerts = buffer->vData + vBufferOffset;
  139. memcpy(worldVerts, data->getVertices() + vertexStart * _bytesPerVertex, vertexCount * _bytesPerVertex);
  140. // Calculate vertices world positions
  141. if (!_useModel && !_ignoreWorldMatrix)
  142. {
  143. size_t dataPerVertex = _bytesPerVertex / sizeof(float);
  144. float* ptrPos = worldVerts + _posOffset;
  145. auto& worldMat = node->getWorldMatrix();
  146. switch (num) {
  147. // Vertex is X Y Z Format
  148. case 3:
  149. for (uint32_t i = 0; i < vertexCount; ++i)
  150. {
  151. ((cocos2d::Vec3*)ptrPos)->transformMat4(*((cocos2d::Vec3*)ptrPos), worldMat);
  152. ptrPos += dataPerVertex;
  153. }
  154. break;
  155. // Vertex is X Y Format
  156. case 2:
  157. for (uint32_t i = 0; i < vertexCount; ++i)
  158. {
  159. float z = ptrPos[2];
  160. ptrPos[2] = 0;
  161. worldMat.transformPoint((cocos2d::Vec3*)ptrPos);
  162. ptrPos[2] = z;
  163. ptrPos += dataPerVertex;
  164. }
  165. break;
  166. }
  167. }
  168. // Copy index buffer with vertex offset
  169. uint16_t* indices = (uint16_t*)data->getIndices();
  170. uint16_t* dst = buffer->iData;
  171. for (auto i = 0, j = ia.indicesStart; i < indexCount; ++i, ++j)
  172. {
  173. dst[indexId++] = vertexOffset + indices[j];
  174. }
  175. }
  176. void Assembler::setVertexFormat(VertexFormat* vfmt)
  177. {
  178. if (_vfmt == vfmt) return;
  179. CC_SAFE_RETAIN(vfmt);
  180. CC_SAFE_RELEASE(_vfmt);
  181. _vfmt = vfmt;
  182. if (_vfmt)
  183. {
  184. _bytesPerVertex = _vfmt->getBytes();
  185. _vfPos = _vfmt->getElement(ATTRIB_NAME_POSITION_HASH);
  186. _posOffset = _vfPos->offset / 4;
  187. _vfColor = _vfmt->getElement(ATTRIB_NAME_COLOR_HASH);
  188. if (_vfColor != nullptr)
  189. {
  190. _alphaOffset = _vfColor->offset + 3;
  191. }
  192. }
  193. }
  194. void Assembler::setRenderDataList(RenderDataList* datas)
  195. {
  196. if (_datas == datas) return;
  197. CC_SAFE_RELEASE(_datas);
  198. _datas = datas;
  199. CC_SAFE_RETAIN(_datas);
  200. }
  201. void Assembler::updateOpacity(std::size_t index, uint8_t opacity)
  202. {
  203. // has no color info in vertex buffer
  204. if(!_vfColor || !_datas || !_vfmt)
  205. {
  206. return;
  207. }
  208. const IARenderData& ia = _iaDatas[index];
  209. std::size_t meshIndex = ia.meshIndex >= 0 ? ia.meshIndex : index;
  210. RenderData* data = _datas->getRenderData(meshIndex);
  211. if (!data)
  212. {
  213. return;
  214. }
  215. CCASSERT(data->getVBytes() % _bytesPerVertex == 0, "Assembler::updateOpacity vertices data doesn't follow vertex format");
  216. uint32_t vertexCount = (uint32_t)data->getVBytes() / _bytesPerVertex;
  217. size_t dataPerVertex = _bytesPerVertex / sizeof(uint8_t);
  218. uint8_t* ptrAlpha = (uint8_t*)data->getVertices() + _alphaOffset;
  219. const Vector<Pass*>& passes = ia.getEffect()->getPasses();
  220. if (passes.at(0)->getBlendSrc() == BlendFactor::ONE)
  221. {
  222. float alpha = opacity / 255.0;
  223. for (uint32_t i = 0; i < vertexCount; ++i)
  224. {
  225. *(ptrAlpha-1) *= alpha;
  226. *(ptrAlpha-2) *= alpha;
  227. *(ptrAlpha-3) *= alpha;
  228. *ptrAlpha = opacity;
  229. ptrAlpha += dataPerVertex;
  230. }
  231. }
  232. else
  233. {
  234. for (uint32_t i = 0; i < vertexCount; ++i)
  235. {
  236. *ptrAlpha = opacity;
  237. ptrAlpha += dataPerVertex;
  238. }
  239. }
  240. *_dirty &= ~VERTICES_OPACITY_CHANGED;
  241. }
  242. RENDERER_END