RenderFlow.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 "RenderFlow.hpp"
  21. #include "NodeMemPool.hpp"
  22. #include "assembler/AssemblerSprite.hpp"
  23. #if USE_MIDDLEWARE
  24. #include "MiddlewareManager.h"
  25. #endif
  26. #define SUB_RENDER_THREAD_COUNT 1
  27. #define RENDER_THREAD_COUNT (SUB_RENDER_THREAD_COUNT + 1)
  28. RENDERER_BEGIN
  29. const uint32_t InitLevelCount = 3;
  30. const uint32_t InitLevelNodeCount = 100;
  31. const uint32_t LocalMat_Use_Thread_Unit_Count = 5;
  32. const uint32_t WorldMat_Use_Thread_Node_count = 500;
  33. RenderFlow* RenderFlow::_instance = nullptr;
  34. RenderFlow::RenderFlow(DeviceGraphics* device, Scene* scene, ForwardRenderer* forward)
  35. : _device(device)
  36. , _scene(scene)
  37. , _forward(forward)
  38. {
  39. _instance = this;
  40. _batcher = new ModelBatcher(this);
  41. #if SUB_RENDER_THREAD_COUNT > 0
  42. _paralleTask = new ParallelTask();
  43. _paralleTask->init(SUB_RENDER_THREAD_COUNT);
  44. for (uint32_t i = 0; i < SUB_RENDER_THREAD_COUNT; i++)
  45. {
  46. _paralleTask->pushTask(i, [this](int tid){
  47. switch(_parallelStage)
  48. {
  49. case ParallelStage::LOCAL_MAT:
  50. calculateLocalMatrix(tid);
  51. break;
  52. case ParallelStage::WORLD_MAT:
  53. calculateLevelWorldMatrix(tid, _parallelStage);
  54. break;
  55. default:
  56. break;
  57. }
  58. });
  59. }
  60. #endif
  61. _levelInfoArr.resize(InitLevelCount);
  62. for (auto i = 0; i < InitLevelCount; i++)
  63. {
  64. _levelInfoArr[i].reserve(InitLevelNodeCount);
  65. }
  66. }
  67. RenderFlow::~RenderFlow()
  68. {
  69. CC_SAFE_DELETE(_paralleTask);
  70. CC_SAFE_DELETE(_batcher);
  71. }
  72. void RenderFlow::removeNodeLevel(std::size_t level, cocos2d::Mat4* worldMat)
  73. {
  74. if (level >= _levelInfoArr.size()) return;
  75. auto& levelInfos = _levelInfoArr[level];
  76. for(auto it = levelInfos.begin(); it != levelInfos.end(); it++)
  77. {
  78. if (it->worldMat == worldMat)
  79. {
  80. levelInfos.erase(it);
  81. return;
  82. }
  83. }
  84. }
  85. void RenderFlow::insertNodeLevel(std::size_t level, const LevelInfo& levelInfo)
  86. {
  87. if (level >= _levelInfoArr.size())
  88. {
  89. _levelInfoArr.resize(level + 1);
  90. }
  91. auto& levelInfos = _levelInfoArr[level];
  92. levelInfos.push_back(levelInfo);
  93. }
  94. void RenderFlow::calculateLocalMatrix(int tid)
  95. {
  96. const uint16_t SPACE_FREE_FLAG = 0x0;
  97. cocos2d::Mat4 matTemp;
  98. NodeMemPool* instance = NodeMemPool::getInstance();
  99. CCASSERT(instance, "RenderFlow calculateLocalMatrix NodeMemPool is null");
  100. auto& commonList = instance->getCommonList();
  101. auto& nodePool = instance->getNodePool();
  102. UnitCommon* commonUnit = nullptr;
  103. uint16_t usingNum = 0;
  104. std::size_t contentNum = 0;
  105. Sign* signData = nullptr;
  106. UnitNode* nodeUnit = nullptr;
  107. uint32_t* dirty = nullptr;
  108. cocos2d::Mat4* localMat = nullptr;
  109. TRS* trs = nullptr;
  110. uint8_t* is3D = nullptr;
  111. cocos2d::Quaternion* quat = nullptr;
  112. float trsZ = 0.0f, trsSZ = 0.0f;
  113. std::size_t begin = 0, end = commonList.size();
  114. std::size_t fieldSize = end / RENDER_THREAD_COUNT;
  115. if (tid >= 0)
  116. {
  117. begin = tid * fieldSize;
  118. if (tid < RENDER_THREAD_COUNT - 1)
  119. {
  120. end = (tid + 1) * fieldSize;
  121. }
  122. }
  123. for(auto i = begin; i < end; i++)
  124. {
  125. commonUnit = commonList[i];
  126. if (!commonUnit) continue;
  127. usingNum = commonUnit->getUsingNum();
  128. if (usingNum == 0) continue;
  129. contentNum = commonUnit->getContentNum();
  130. signData = commonUnit->getSignData(0);
  131. nodeUnit = nodePool[commonUnit->unitID];
  132. dirty = nodeUnit->getDirty(0);
  133. localMat = nodeUnit->getLocalMat(0);
  134. trs = nodeUnit->getTRS(0);
  135. is3D = nodeUnit->getIs3D(0);
  136. NodeProxy** nodeProxy = (NodeProxy**)nodeUnit->getNode(0);
  137. for (auto j = 0; j < contentNum; j++, localMat ++, trs ++, is3D ++, signData++, dirty++, nodeProxy++)
  138. {
  139. if (signData->freeFlag == SPACE_FREE_FLAG) continue;
  140. // reset world transform changed flag
  141. *dirty &= ~(WORLD_TRANSFORM_CHANGED | NODE_OPACITY_CHANGED);
  142. if (!(*dirty & LOCAL_TRANSFORM)) continue;
  143. localMat->setIdentity();
  144. trsZ = *is3D ? trs->z : 0;
  145. localMat->translate(trs->x, trs->y, trsZ);
  146. quat = (cocos2d::Quaternion*)&(trs->qx);
  147. cocos2d::Mat4::createRotation(*quat, &matTemp);
  148. cocos2d::Mat4::multiply(*localMat, matTemp, localMat);
  149. trsSZ = *is3D ? trs->sz : 1;
  150. cocos2d::Mat4::createScale(trs->sx, trs->sy, trsSZ, &matTemp);
  151. cocos2d::Mat4::multiply(*localMat, matTemp, localMat);
  152. *dirty &= ~LOCAL_TRANSFORM;
  153. *dirty |= WORLD_TRANSFORM;
  154. }
  155. }
  156. }
  157. void RenderFlow::calculateLevelWorldMatrix(int tid, int stage)
  158. {
  159. if (_curLevel >= _levelInfoArr.size())
  160. {
  161. return;
  162. }
  163. auto& levelInfos = _levelInfoArr[_curLevel];
  164. std::size_t begin = 0, end = levelInfos.size();
  165. std::size_t fieldSize = end / RENDER_THREAD_COUNT;
  166. if (tid >= 0)
  167. {
  168. begin = tid * fieldSize;
  169. if (tid < RENDER_THREAD_COUNT - 1)
  170. {
  171. end = (tid + 1) * fieldSize;
  172. }
  173. }
  174. for(std::size_t index = begin; index < end; index++)
  175. {
  176. auto& info = levelInfos[index];
  177. auto dirty = info.dirty;
  178. auto parentDirty = info.parentDirty;
  179. auto selfWorldDirty = *dirty & WORLD_TRANSFORM;
  180. auto selfOpacityDirty = *dirty & OPACITY;
  181. if (parentDirty)
  182. {
  183. if ((*parentDirty & WORLD_TRANSFORM_CHANGED) || selfWorldDirty)
  184. {
  185. cocos2d::Mat4::multiply(*info.parentWorldMat, *info.localMat, info.worldMat);
  186. *dirty |= WORLD_TRANSFORM_CHANGED;
  187. *dirty &= ~WORLD_TRANSFORM;
  188. }
  189. if ((*parentDirty & NODE_OPACITY_CHANGED) || selfOpacityDirty)
  190. {
  191. *info.realOpacity = *info.opacity * *info.parentRealOpacity / 255.0f;
  192. *dirty |= NODE_OPACITY_CHANGED;
  193. *dirty &= ~OPACITY;
  194. }
  195. }
  196. else
  197. {
  198. if (selfWorldDirty)
  199. {
  200. *info.worldMat = *info.localMat;
  201. *dirty |= WORLD_TRANSFORM_CHANGED;
  202. *dirty &= ~WORLD_TRANSFORM;
  203. }
  204. if (selfOpacityDirty)
  205. {
  206. *info.realOpacity = *info.opacity;
  207. *dirty |= NODE_OPACITY_CHANGED;
  208. *dirty &= ~OPACITY;
  209. }
  210. }
  211. }
  212. }
  213. void RenderFlow::calculateWorldMatrix()
  214. {
  215. for(std::size_t level = 0, n = _levelInfoArr.size(); level < n; level++)
  216. {
  217. auto& levelInfos = _levelInfoArr[level];
  218. for(std::size_t index = 0, count = levelInfos.size(); index < count; index++)
  219. {
  220. auto& info = levelInfos[index];
  221. auto selfWorldDirty = *info.dirty & WORLD_TRANSFORM;
  222. auto selfOpacityDirty = *info.dirty & OPACITY;
  223. if (info.parentDirty)
  224. {
  225. if ((*info.parentDirty & WORLD_TRANSFORM_CHANGED) || selfWorldDirty)
  226. {
  227. cocos2d::Mat4::multiply(*info.parentWorldMat, *info.localMat, info.worldMat);
  228. *info.dirty |= WORLD_TRANSFORM_CHANGED;
  229. *info.dirty &= ~WORLD_TRANSFORM;
  230. }
  231. if ((*info.parentDirty & NODE_OPACITY_CHANGED) || selfOpacityDirty)
  232. {
  233. *info.realOpacity = *info.opacity * *info.parentRealOpacity / 255.0f;
  234. *info.dirty |= NODE_OPACITY_CHANGED;
  235. *info.dirty &= ~OPACITY;
  236. }
  237. }
  238. else
  239. {
  240. if (selfWorldDirty)
  241. {
  242. *info.worldMat = *info.localMat;
  243. *info.dirty |= WORLD_TRANSFORM_CHANGED;
  244. *info.dirty &= ~WORLD_TRANSFORM;
  245. }
  246. if (selfOpacityDirty)
  247. {
  248. *info.realOpacity = *info.opacity;
  249. *info.dirty |= NODE_OPACITY_CHANGED;
  250. *info.dirty &= ~OPACITY;
  251. }
  252. }
  253. }
  254. }
  255. }
  256. void RenderFlow::render(NodeProxy* scene, float deltaTime, Camera *camera)
  257. {
  258. if (scene != nullptr)
  259. {
  260. #if USE_MIDDLEWARE
  261. // udpate middleware before render
  262. middleware::MiddlewareManager::getInstance()->update(deltaTime);
  263. #endif
  264. #if SUB_RENDER_THREAD_COUNT > 0
  265. int mainThreadTid = RENDER_THREAD_COUNT - 1;
  266. NodeMemPool* instance = NodeMemPool::getInstance();
  267. auto& commonList = instance->getCommonList();
  268. if (commonList.size() < LocalMat_Use_Thread_Unit_Count)
  269. {
  270. _parallelStage = ParallelStage::NONE;
  271. calculateLocalMatrix();
  272. }
  273. else
  274. {
  275. _parallelStage = ParallelStage::LOCAL_MAT;
  276. _paralleTask->beginAllThreads();
  277. calculateLocalMatrix(mainThreadTid);
  278. _paralleTask->waitAllThreads();
  279. }
  280. _curLevel = 0;
  281. for(auto count = _levelInfoArr.size(); _curLevel < count; _curLevel++)
  282. {
  283. auto& levelInfos = _levelInfoArr[_curLevel];
  284. if (levelInfos.size() < WorldMat_Use_Thread_Node_count)
  285. {
  286. _parallelStage = ParallelStage::NONE;
  287. calculateLevelWorldMatrix();
  288. }
  289. else
  290. {
  291. _parallelStage = ParallelStage::WORLD_MAT;
  292. _paralleTask->beginAllThreads();
  293. calculateLevelWorldMatrix(mainThreadTid);
  294. _paralleTask->waitAllThreads();
  295. }
  296. }
  297. #else
  298. calculateLocalMatrix();
  299. calculateWorldMatrix();
  300. #endif
  301. _batcher->startBatch();
  302. #if USE_MIDDLEWARE
  303. // render middleware
  304. middleware::MiddlewareManager::getInstance()->render(deltaTime);
  305. #endif
  306. scene->resetGlobalRenderOrder();
  307. auto traverseHandle = scene->traverseHandle;
  308. traverseHandle(scene, _batcher, _scene);
  309. _batcher->terminateBatch();
  310. if (camera) {
  311. _forward->renderCamera(camera, _scene);
  312. }
  313. else {
  314. _forward->render(_scene, deltaTime);
  315. }
  316. }
  317. }
  318. void RenderFlow::visit(NodeProxy* rootNode)
  319. {
  320. NodeProxy::visit(rootNode, _batcher, _scene);
  321. }
  322. RENDERER_END