SceneGraph.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #include "anki/scene/SceneGraph.h"
  2. #include "anki/scene/Camera.h"
  3. #include "anki/util/Exception.h"
  4. #include "anki/core/ThreadPool.h"
  5. #include "anki/renderer/Renderer.h"
  6. namespace anki {
  7. //==============================================================================
  8. // Misc =
  9. //==============================================================================
  10. //==============================================================================
  11. struct UpdateMovablesJob: ThreadJob
  12. {
  13. SceneGraph::Types<SceneNode>::Iterator movablesBegin;
  14. U32 movablesCount;
  15. void operator()(U threadId, U threadsCount)
  16. {
  17. U64 start, end;
  18. choseStartEnd(threadId, threadsCount, movablesCount, start, end);
  19. for(U64 i = start; i < end; i++)
  20. {
  21. SceneNode* sn = *(movablesBegin + i);
  22. Movable* m = sn->getMovable();
  23. if(m)
  24. {
  25. m->update();
  26. }
  27. }
  28. }
  29. };
  30. //==============================================================================
  31. static void updateSceneNode(SceneNode& sn, F32 prevUpdateTime,
  32. F32 crntTime, SectorGroup& sectorGroup)
  33. {
  34. sn.frameUpdate(prevUpdateTime, crntTime, Timestamp::getTimestamp());
  35. // Do some spatial stuff
  36. Spatial* sp = sn.getSpatial();
  37. if(sp)
  38. {
  39. if(sp->getSpatialTimestamp() >= Timestamp::getTimestamp())
  40. {
  41. sp->update();
  42. //sectorGroup.placeSceneNode(&sn);
  43. }
  44. sp->resetFrame();
  45. }
  46. // Do some frustumable stuff
  47. Frustumable* fr = sn.getFrustumable();
  48. if(fr)
  49. {
  50. fr->resetFrame();
  51. }
  52. // Do some renderable stuff
  53. Renderable* r = sn.getRenderable();
  54. if(r)
  55. {
  56. r->resetFrame();
  57. }
  58. }
  59. //==============================================================================
  60. struct UpdateSceneNodesJob: ThreadJob
  61. {
  62. SceneGraph::Types<SceneNode>::Iterator sceneNodesBegin;
  63. U32 sceneNodesCount;
  64. F32 prevUpdateTime;
  65. F32 crntTime;
  66. SectorGroup* sectorGroup;
  67. void operator()(U threadId, U threadsCount)
  68. {
  69. U64 start, end;
  70. choseStartEnd(threadId, threadsCount, sceneNodesCount, start, end);
  71. for(U64 i = start; i < end; i++)
  72. {
  73. SceneNode* n = *(sceneNodesBegin + i);
  74. updateSceneNode(*n, prevUpdateTime, crntTime, *sectorGroup);
  75. }
  76. }
  77. };
  78. //==============================================================================
  79. // Scene =
  80. //==============================================================================
  81. //==============================================================================
  82. SceneGraph::SceneGraph()
  83. : alloc(ANKI_CFG_SCENE_ALLOCATOR_SIZE),
  84. frameAlloc(ANKI_CFG_SCENE_FRAME_ALLOCATOR_SIZE),
  85. nodes(alloc),
  86. sectorGroup(this),
  87. events(this)
  88. {
  89. nodes.reserve(ANKI_CFG_SCENE_NODES_AVERAGE_COUNT);
  90. ambientCol = Vec3(0.1, 0.05, 0.05) * 3;
  91. }
  92. //==============================================================================
  93. SceneGraph::~SceneGraph()
  94. {}
  95. //==============================================================================
  96. void SceneGraph::registerNode(SceneNode* node)
  97. {
  98. addC(nodes, node);
  99. addDict(nameToNode, node);
  100. }
  101. //==============================================================================
  102. void SceneGraph::unregisterNode(SceneNode* node)
  103. {
  104. removeC(nodes, node);
  105. removeDict(nameToNode, node);
  106. }
  107. //==============================================================================
  108. void SceneGraph::update(F32 prevUpdateTime, F32 crntTime, Renderer& renderer)
  109. {
  110. #if ANKI_CFG_SCENE_PROFILE
  111. HighRezTimer::Scalar startTime = HighRezTimer::getCurrentTime();
  112. #endif
  113. frameAlloc.reset();
  114. // XXX Do that in parallel
  115. physics.update(prevUpdateTime, crntTime);
  116. renderer.getTiler().updateTiles(*mainCam);
  117. events.updateAllEvents(prevUpdateTime, crntTime);
  118. #if 0
  119. // First do the movable updates
  120. for(SceneNode* n : nodes)
  121. {
  122. Movable* m = n->getMovable();
  123. if(m)
  124. {
  125. m->update();
  126. }
  127. }
  128. #else
  129. ThreadPool& threadPool = ThreadPoolSingleton::get();
  130. UpdateMovablesJob jobs[ThreadPool::MAX_THREADS];
  131. for(U i = 0; i < threadPool.getThreadsCount(); i++)
  132. {
  133. jobs[i].movablesBegin = nodes.begin();
  134. jobs[i].movablesCount = nodes.size();
  135. threadPool.assignNewJob(i, &jobs[i]);
  136. }
  137. threadPool.waitForAllJobsToFinish();
  138. #endif
  139. // Then the rest
  140. #if 0
  141. for(SceneNode* n : nodes)
  142. {
  143. updateSceneNode(*n, prevUpdateTime, crntTime, sectorGroup);
  144. }
  145. #else
  146. Array<UpdateSceneNodesJob, ThreadPool::MAX_THREADS> jobs2;
  147. for(U i = 0; i < threadPool.getThreadsCount(); i++)
  148. {
  149. UpdateSceneNodesJob& job = jobs2[i];
  150. job.sceneNodesBegin = nodes.begin();
  151. job.sceneNodesCount = nodes.size();
  152. job.prevUpdateTime = prevUpdateTime;
  153. job.crntTime = crntTime;
  154. job.sectorGroup = &sectorGroup;
  155. threadPool.assignNewJob(i, &job);
  156. }
  157. threadPool.waitForAllJobsToFinish();
  158. #endif
  159. doVisibilityTests(*mainCam, *this, renderer);
  160. /*sectorGroup.doVisibilityTests(*mainCam,
  161. VisibilityTest(VT_RENDERABLES | VT_LIGHTS), &r);*/
  162. #if 0
  163. for(SceneNode* n : nodes)
  164. {
  165. if(n->getSpatial()
  166. && n->getSpatial()->getSpatialLastUpdateFrame() == frame)
  167. {
  168. std::cout << "Spatial updated on: " << n->getName()
  169. << std::endl;
  170. }
  171. if(n->getMovable()
  172. && n->getMovable()->getMovableLastUpdateFrame() == frame)
  173. {
  174. std::cout << "Movable updated on: " << n->getName()
  175. << std::endl;
  176. }
  177. if(n->getFrustumable()
  178. && n->getFrustumable()->getFrustumableLastUpdateFrame() == frame)
  179. {
  180. std::cout << "Frustumable updated on: " << n->getName()
  181. << std::endl;
  182. }
  183. }
  184. #endif
  185. #if ANKI_CFG_SCENE_PROFILE
  186. timeForUpdates += HighRezTimer::getCurrentTime() - startTime;
  187. #endif
  188. }
  189. //==============================================================================
  190. SceneNode& SceneGraph::findSceneNode(const char* name)
  191. {
  192. ANKI_ASSERT(nameToNode.find(name) != nameToNode.end());
  193. return *(nameToNode.find(name)->second);
  194. }
  195. //==============================================================================
  196. SceneNode* SceneGraph::tryFindSceneNode(const char* name)
  197. {
  198. Types<SceneNode>::NameToItemMap::iterator it = nameToNode.find(name);
  199. return (it == nameToNode.end()) ? nullptr : it->second;
  200. }
  201. //==============================================================================
  202. void SceneGraph::printProfileInfo() const
  203. {
  204. #if ANKI_CFG_SCENE_PROFILE
  205. ANKI_LOGI("Scene times: " << timeForUpdates);
  206. #endif
  207. }
  208. } // end namespace anki