3
0

Culling.cpp 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <Atom/RPI.Public/AuxGeom/AuxGeomDraw.h>
  9. #include <Atom/RPI.Public/AuxGeom/AuxGeomFeatureProcessorInterface.h>
  10. #include <Atom/RPI.Public/Culling.h>
  11. #include <Atom/RPI.Public/Model/ModelLodUtils.h>
  12. #include <Atom/RPI.Public/RPISystemInterface.h>
  13. #include <Atom/RPI.Public/RenderPipeline.h>
  14. #include <Atom/RPI.Public/Scene.h>
  15. #include <Atom/RPI.Public/View.h>
  16. #include <AzCore/Casting/numeric_cast.h>
  17. #include <AzCore/Jobs/Job.h>
  18. #include <AzCore/Jobs/JobFunction.h>
  19. #include <AzCore/Math/MatrixUtils.h>
  20. #include <AzCore/Math/ShapeIntersection.h>
  21. #include <AzCore/Task/TaskGraph.h>
  22. #include <AzCore/std/parallel/lock.h>
  23. #include <AzCore/std/smart_ptr/unique_ptr.h>
  24. #include <AzFramework/Visibility/OcclusionBus.h>
  25. #include <Atom_RPI_Traits_Platform.h>
  26. #if AZ_TRAIT_MASKED_OCCLUSION_CULLING_SUPPORTED
  27. #include <MaskedOcclusionCulling/MaskedOcclusionCulling.h>
  28. #endif
  29. //Enables more inner-loop profiling scopes (can create high overhead in telemetry if there are many-many objects in a scene)
  30. //#define AZ_CULL_PROFILE_DETAILED
  31. //Enables more detailed profiling descriptions within the culling system, but adds some performance overhead.
  32. //Enable this to more easily see which jobs are associated with which view.
  33. //#define AZ_CULL_PROFILE_VERBOSE
  34. namespace AZ
  35. {
  36. namespace RPI
  37. {
  38. // Entry work lists
  39. AZ_CVAR(bool, r_useEntryWorkListsForCulling, false, nullptr, AZ::ConsoleFunctorFlags::Null, "Use entity work lists instead of node work lists for job distribution");
  40. AZ_CVAR(uint32_t, r_numEntriesPerCullingJob, 750, nullptr, AZ::ConsoleFunctorFlags::Null, "Controls amount of entries to collect for jobs when using entry work lists");
  41. // Node work lists using entry count
  42. AZ_CVAR(bool, r_useEntryCountForNodeJobs, true, nullptr, AZ::ConsoleFunctorFlags::Null, "Use entity count instead of node count when checking whether to spawn job for node work list");
  43. AZ_CVAR(uint32_t, r_maxNodesWhenUsingEntryCount, 100, nullptr, AZ::ConsoleFunctorFlags::Null, "Controls max amount of nodes to collect when using entry count");
  44. // Node work lists using node count
  45. AZ_CVAR(uint32_t, r_numNodesPerCullingJob, 25, nullptr, AZ::ConsoleFunctorFlags::Null, "Controls amount of nodes to collect for jobs when not using the entry count");
  46. // This value dictates the amount to extrude the octree node OBB when doing a frustum intersection test against the camera frustum to help cut draw calls for shadow cascade passes.
  47. // Default is set to -1 as this is optimization needs to be triggered by the content developer by setting a reasonable non-negative value applicable for their content.
  48. AZ_CVAR(int, r_shadowCascadeExtrusionAmount, -1, nullptr, AZ::ConsoleFunctorFlags::Null, "The amount of meters to extrude the Obb towards light direction when doing frustum overlap test against camera frustum");
  49. #ifdef AZ_CULL_DEBUG_ENABLED
  50. void DebugDrawWorldCoordinateAxes(AuxGeomDraw* auxGeom)
  51. {
  52. auxGeom->DrawCylinder(Vector3(.5, .0, .0), Vector3(1, 0, 0), 0.02f, 1.0f, Colors::Red, AuxGeomDraw::DrawStyle::Solid, AuxGeomDraw::DepthTest::Off);
  53. auxGeom->DrawCylinder(Vector3(.0, .5, .0), Vector3(0, 1, 0), 0.02f, 1.0f, Colors::Green, AuxGeomDraw::DrawStyle::Solid, AuxGeomDraw::DepthTest::Off);
  54. auxGeom->DrawCylinder(Vector3(.0, .0, .5), Vector3(0, 0, 1), 0.02f, 1.0f, Colors::Blue, AuxGeomDraw::DrawStyle::Solid, AuxGeomDraw::DepthTest::Off);
  55. Vector3 axisVerts[] =
  56. {
  57. Vector3(0.f, 0.f , 0.f), Vector3(10000.f, 0.f, 0.f),
  58. Vector3(0.f, 0.f , 0.f), Vector3(0.f, 10000.f, 0.f),
  59. Vector3(0.f, 0.f , 0.f), Vector3(0.f, 0.f, 10000.f)
  60. };
  61. Color colors[] =
  62. {
  63. Colors::Red, Colors::Red,
  64. Colors::Green, Colors::Green,
  65. Colors::Blue, Colors::Blue
  66. };
  67. AuxGeomDraw::AuxGeomDynamicDrawArguments lineArgs;
  68. lineArgs.m_verts = axisVerts;
  69. lineArgs.m_vertCount = 6;
  70. lineArgs.m_colors = colors;
  71. lineArgs.m_colorCount = lineArgs.m_vertCount;
  72. lineArgs.m_depthTest = AuxGeomDraw::DepthTest::Off;
  73. auxGeom->DrawLines(lineArgs);
  74. }
  75. #endif //AZ_CULL_DEBUG_ENABLED
  76. CullingDebugContext::~CullingDebugContext()
  77. {
  78. AZStd::lock_guard<AZStd::mutex> lock(m_perViewCullStatsMutex);
  79. for (auto& iter : m_perViewCullStats)
  80. {
  81. delete iter.second;
  82. iter.second = nullptr;
  83. }
  84. }
  85. CullingDebugContext::CullStats& CullingDebugContext::GetCullStatsForView(View* view)
  86. {
  87. AZStd::lock_guard<AZStd::mutex> lock(m_perViewCullStatsMutex);
  88. auto iter = m_perViewCullStats.find(view);
  89. if (iter != m_perViewCullStats.end())
  90. {
  91. AZ_Assert(iter->second->m_name == view->GetName(), "stored view name does not match");
  92. return *iter->second;
  93. }
  94. else
  95. {
  96. m_perViewCullStats[view] = aznew CullStats(view->GetName());
  97. return *m_perViewCullStats[view];
  98. }
  99. }
  100. void CullingDebugContext::ResetCullStats()
  101. {
  102. m_numCullablesInScene = 0;
  103. AZStd::lock_guard<AZStd::mutex> lockCullStats(m_perViewCullStatsMutex);
  104. for (auto& cullStatsPair : m_perViewCullStats)
  105. {
  106. cullStatsPair.second->Reset();
  107. }
  108. }
  109. void CullingScene::RegisterOrUpdateCullable(Cullable& cullable)
  110. {
  111. // Multiple threads can call RegisterOrUpdateCullable at the same time
  112. // since the underlying visScene is thread safe, but if you're inserting or
  113. // updating between BeginCulling and EndCulling, you'll get non-deterministic
  114. // results depending on a race condition if you happen to update before or after
  115. // the culling system starts Enumerating, so use soft_lock_shared here
  116. m_cullDataConcurrencyCheck.soft_lock_shared();
  117. m_visScene->InsertOrUpdateEntry(cullable.m_cullData.m_visibilityEntry);
  118. m_cullDataConcurrencyCheck.soft_unlock_shared();
  119. }
  120. void CullingScene::UnregisterCullable(Cullable& cullable)
  121. {
  122. // Multiple threads can call RegisterOrUpdateCullable at the same time
  123. // since the underlying visScene is thread safe, but if you're inserting or
  124. // updating between BeginCulling and EndCulling, you'll get non-deterministic
  125. // results depending on a race condition if you happen to update before or after
  126. // the culling system starts Enumerating, so use soft_lock_shared here
  127. m_cullDataConcurrencyCheck.soft_lock_shared();
  128. m_visScene->RemoveEntry(cullable.m_cullData.m_visibilityEntry);
  129. m_cullDataConcurrencyCheck.soft_unlock_shared();
  130. }
  131. uint32_t CullingScene::GetNumCullables() const
  132. {
  133. return m_visScene->GetEntryCount();
  134. }
  135. CullingDebugContext& CullingScene::GetDebugContext()
  136. {
  137. return m_debugCtx;
  138. }
  139. const AzFramework::IVisibilityScene* CullingScene::GetVisibilityScene() const
  140. {
  141. return m_visScene;
  142. }
  143. // Search for and return the entity context ID associated with the scene and connected to OcclusionRequestBus. If there is no
  144. // matching scene, return a null ID.
  145. static AzFramework::EntityContextId GetEntityContextIdForOcclusion(const AZ::RPI::Scene* scene)
  146. {
  147. // Active RPI scenes are registered with the AzFramework::SceneSystem using unique names.
  148. auto sceneSystem = AzFramework::SceneSystemInterface::Get();
  149. AZ_Assert(sceneSystem, "Attempting to retrieve the entity context ID for a scene before the scene system interface is ready.");
  150. AzFramework::EntityContextId resultId = AzFramework::EntityContextId::CreateNull();
  151. // Enumerate all scenes registered with the AzFramework::SceneSystem
  152. sceneSystem->IterateActiveScenes(
  153. [&](const AZStd::shared_ptr<AzFramework::Scene>& azScene)
  154. {
  155. // AzFramework::Scene uses "subsystems" bind arbitrary data. This is generally used to maintain an association between
  156. // AzFramework::Scene and AZ::RPI::Scene. We search for the AzFramework::Scene scene with a subsystem matching the input
  157. // scene pointer.
  158. AZ::RPI::ScenePtr* rpiScene = azScene->FindSubsystemInScene<AZ::RPI::ScenePtr>();
  159. if (rpiScene && (*rpiScene).get() == scene)
  160. {
  161. // Each scene should only be bound to one entity context for entities that will appear in the scene.
  162. AzFramework::EntityContext** entityContext =
  163. azScene->FindSubsystemInScene<AzFramework::EntityContext::SceneStorageType>();
  164. if (entityContext)
  165. {
  166. // Return if the entity context is valid and connected to OcclusionRequestBus
  167. const AzFramework::EntityContextId contextId = (*entityContext)->GetContextId();
  168. if (AzFramework::OcclusionRequestBus::HasHandlers(contextId))
  169. {
  170. resultId = contextId;
  171. return false; // Result found, returning
  172. }
  173. }
  174. }
  175. return true; // No match, continuing to search for containing scene.
  176. });
  177. return resultId;
  178. }
  179. struct WorklistData
  180. {
  181. CullingDebugContext* m_debugCtx = nullptr;
  182. const Scene* m_scene = nullptr;
  183. AzFramework::EntityContextId m_sceneEntityContextId;
  184. View* m_view = nullptr;
  185. Frustum m_frustum;
  186. Frustum m_cameraFrustum;
  187. Frustum m_excludeFrustum;
  188. AZ::Job* m_parentJob = nullptr;
  189. AZ::TaskGraphEvent* m_taskGraphEvent = nullptr;
  190. bool m_hasExcludeFrustum = false;
  191. bool m_applyCameraFrustumIntersectionTest = false;
  192. #ifdef AZ_CULL_DEBUG_ENABLED
  193. AuxGeomDrawPtr GetAuxGeomPtr()
  194. {
  195. if (m_debugCtx->m_debugDraw && (m_view->GetName() == m_debugCtx->m_currentViewSelectionName))
  196. {
  197. return AuxGeomFeatureProcessorInterface::GetDrawQueueForScene(m_scene);
  198. }
  199. return nullptr;
  200. }
  201. #endif
  202. };
  203. static AZStd::shared_ptr<WorklistData> MakeWorklistData(
  204. CullingDebugContext& debugCtx,
  205. const Scene& scene,
  206. View& view,
  207. Frustum& frustum,
  208. AZ::Job* parentJob,
  209. AZ::TaskGraphEvent* taskGraphEvent)
  210. {
  211. AZStd::shared_ptr<WorklistData> worklistData = AZStd::make_shared<WorklistData>();
  212. worklistData->m_debugCtx = &debugCtx;
  213. worklistData->m_scene = &scene;
  214. worklistData->m_sceneEntityContextId = GetEntityContextIdForOcclusion(&scene);
  215. worklistData->m_view = &view;
  216. worklistData->m_frustum = frustum;
  217. worklistData->m_parentJob = parentJob;
  218. worklistData->m_taskGraphEvent = taskGraphEvent;
  219. return worklistData;
  220. }
  221. // Used to accumulate NodeData into lists to be handed off to jobs for processing
  222. struct WorkListType
  223. {
  224. void Init()
  225. {
  226. m_entryCount = 0;
  227. u32 reserveCount = r_useEntryCountForNodeJobs ? r_maxNodesWhenUsingEntryCount : r_numNodesPerCullingJob;
  228. m_nodes.reserve(reserveCount);
  229. }
  230. u32 m_entryCount = 0;
  231. AZStd::vector<AzFramework::IVisibilityScene::NodeData> m_nodes;
  232. };
  233. // Used to accumulate VisibilityEntry into lists to be handed off to jobs for processing
  234. struct EntryListType
  235. {
  236. AZStd::vector<AzFramework::VisibilityEntry*> m_entries;
  237. };
  238. static bool TestOcclusionCulling(
  239. const AZStd::shared_ptr<WorklistData>& worklistData, const AzFramework::VisibilityEntry* visibleEntry);
  240. static void ProcessEntrylist(
  241. const AZStd::shared_ptr<WorklistData>& worklistData,
  242. const AZStd::vector<AzFramework::VisibilityEntry*>& entries,
  243. bool parentNodeContainedInFrustum = false,
  244. s32 startIdx = 0,
  245. s32 endIdx = -1)
  246. {
  247. #ifdef AZ_CULL_DEBUG_ENABLED
  248. // These variable are only used for the gathering of debug information.
  249. uint32_t numDrawPackets = 0;
  250. uint32_t numVisibleCullables = 0;
  251. #endif
  252. endIdx = (endIdx == -1) ? s32(entries.size()) : endIdx;
  253. for (s32 i = startIdx; i < endIdx; ++i)
  254. {
  255. AzFramework::VisibilityEntry* visibleEntry = entries[i];
  256. if (visibleEntry->m_typeFlags & AzFramework::VisibilityEntry::TYPE_RPI_Cullable ||
  257. visibleEntry->m_typeFlags & AzFramework::VisibilityEntry::TYPE_RPI_VisibleObjectList)
  258. {
  259. Cullable* c = static_cast<Cullable*>(visibleEntry->m_userData);
  260. if ((c->m_cullData.m_drawListMask & worklistData->m_view->GetDrawListMask()).none() ||
  261. c->m_cullData.m_hideFlags & worklistData->m_view->GetUsageFlags() ||
  262. c->m_isHidden)
  263. {
  264. continue;
  265. }
  266. if (!parentNodeContainedInFrustum)
  267. {
  268. IntersectResult res = ShapeIntersection::Classify(worklistData->m_frustum, c->m_cullData.m_boundingSphere);
  269. bool entryInFrustum = (res != IntersectResult::Exterior) && (res == IntersectResult::Interior || ShapeIntersection::Overlaps(worklistData->m_frustum, c->m_cullData.m_boundingObb));
  270. if (!entryInFrustum)
  271. {
  272. continue;
  273. }
  274. }
  275. if (worklistData->m_hasExcludeFrustum &&
  276. ShapeIntersection::Classify(worklistData->m_excludeFrustum, c->m_cullData.m_boundingSphere) == IntersectResult::Interior)
  277. {
  278. // Skip item contained in exclude frustum.
  279. continue;
  280. }
  281. if (TestOcclusionCulling(worklistData, visibleEntry))
  282. {
  283. // There are ways to write this without [[maybe_unused]], but they are brittle.
  284. // For example, using #else could cause a bug where the function's parameter
  285. // is changed in #ifdef but not in #else.
  286. [[maybe_unused]] const uint32_t drawPacketCount = AddLodDataToView(
  287. c->m_cullData.m_boundingSphere.GetCenter(), c->m_lodData, *worklistData->m_view, visibleEntry->m_typeFlags);
  288. c->m_isVisible = true;
  289. worklistData->m_view->ApplyFlags(c->m_flags);
  290. #ifdef AZ_CULL_DEBUG_ENABLED
  291. ++numVisibleCullables;
  292. numDrawPackets += drawPacketCount;
  293. #endif
  294. }
  295. }
  296. }
  297. #ifdef AZ_CULL_DEBUG_ENABLED
  298. AuxGeomDrawPtr auxGeomPtr = worklistData->GetAuxGeomPtr();
  299. if (auxGeomPtr)
  300. {
  301. //Draw bounds on individual objects
  302. if (worklistData->m_debugCtx->m_drawBoundingBoxes || worklistData->m_debugCtx->m_drawBoundingSpheres || worklistData->m_debugCtx->m_drawLodRadii)
  303. {
  304. for (AzFramework::VisibilityEntry* visibleEntry : entries)
  305. {
  306. if (visibleEntry->m_typeFlags & AzFramework::VisibilityEntry::TYPE_RPI_Cullable ||
  307. visibleEntry->m_typeFlags & AzFramework::VisibilityEntry::TYPE_RPI_VisibleObjectList)
  308. {
  309. Cullable* c = static_cast<Cullable*>(visibleEntry->m_userData);
  310. if (worklistData->m_debugCtx->m_drawBoundingBoxes)
  311. {
  312. auxGeomPtr->DrawObb(c->m_cullData.m_boundingObb, Matrix3x4::Identity(),
  313. parentNodeContainedInFrustum ? Colors::Lime : Colors::Yellow, AuxGeomDraw::DrawStyle::Line);
  314. }
  315. if (worklistData->m_debugCtx->m_drawBoundingSpheres)
  316. {
  317. auxGeomPtr->DrawSphere(c->m_cullData.m_boundingSphere.GetCenter(), c->m_cullData.m_boundingSphere.GetRadius(),
  318. Color(0.5f, 0.5f, 0.5f, 0.3f), AuxGeomDraw::DrawStyle::Shaded);
  319. }
  320. if (worklistData->m_debugCtx->m_drawLodRadii)
  321. {
  322. auxGeomPtr->DrawSphere(c->m_cullData.m_boundingSphere.GetCenter(),
  323. c->m_lodData.m_lodSelectionRadius,
  324. Color(1.0f, 0.5f, 0.0f, 0.3f), RPI::AuxGeomDraw::DrawStyle::Shaded);
  325. }
  326. }
  327. }
  328. }
  329. }
  330. if (worklistData->m_debugCtx->m_enableStats)
  331. {
  332. CullingDebugContext::CullStats& cullStats = worklistData->m_debugCtx->GetCullStatsForView(worklistData->m_view);
  333. //no need for mutex here since these are all atomics
  334. cullStats.m_numVisibleDrawPackets += numDrawPackets;
  335. cullStats.m_numVisibleCullables += numVisibleCullables;
  336. ++cullStats.m_numJobs;
  337. }
  338. #endif
  339. }
  340. static void ProcessVisibilityNode(const AZStd::shared_ptr<WorklistData>& worklistData, const AzFramework::IVisibilityScene::NodeData& nodeData)
  341. {
  342. bool nodeIsContainedInFrustum = !worklistData->m_debugCtx->m_enableFrustumCulling || ShapeIntersection::Contains(worklistData->m_frustum, nodeData.m_bounds);
  343. s32 startIdx = 0, size = s32(nodeData.m_entries.size());
  344. const AZStd::vector<AzFramework::VisibilityEntry*>& entries = nodeData.m_entries;
  345. if (worklistData->m_taskGraphEvent)
  346. {
  347. static const AZ::TaskDescriptor descriptor{ "AZ::RPI::ProcessWorklist", "Graphics" };
  348. AZ::TaskGraph taskGraph{ "ProcessCullableEntries" };
  349. AZ::TaskGraphEvent taskGraphEvent{ "ProcessCullableEntries Wait" };
  350. while (r_useEntryCountForNodeJobs && (size - startIdx) > s32(r_numEntriesPerCullingJob))
  351. {
  352. taskGraph.AddTask(descriptor, [=, &entries]() -> void
  353. {
  354. ProcessEntrylist(worklistData, entries, nodeIsContainedInFrustum, startIdx, startIdx + r_numEntriesPerCullingJob);
  355. });
  356. startIdx += s32(r_numEntriesPerCullingJob);
  357. }
  358. if (!taskGraph.IsEmpty())
  359. {
  360. taskGraph.Detach();
  361. taskGraph.Submit(worklistData->m_taskGraphEvent);
  362. }
  363. ProcessEntrylist(worklistData, nodeData.m_entries, nodeIsContainedInFrustum, startIdx, size);
  364. }
  365. else // Use job system
  366. {
  367. while (r_useEntryCountForNodeJobs && (size - startIdx) > s32(r_numEntriesPerCullingJob))
  368. {
  369. auto processEntries = [=, &entries]() -> void
  370. {
  371. ProcessEntrylist(worklistData, entries, nodeIsContainedInFrustum, startIdx, startIdx + r_numEntriesPerCullingJob);
  372. };
  373. AZ::Job* job = AZ::CreateJobFunction(AZStd::move(processEntries), true);
  374. worklistData->m_parentJob->SetContinuation(job);
  375. job->Start();
  376. startIdx += s32(r_numEntriesPerCullingJob);
  377. }
  378. ProcessEntrylist(worklistData, nodeData.m_entries, nodeIsContainedInFrustum, startIdx, size);
  379. }
  380. #ifdef AZ_CULL_DEBUG_ENABLED
  381. //Draw the node bounds
  382. // "Fully visible" nodes are nodes that are fully inside the frustum. "Partially visible" nodes intersect the edges of the frustum.
  383. // Since the nodes of an octree have lots of overlapping boxes with coplanar edges, it's easier to view these separately, so
  384. // we have a few debug booleans to toggle which ones to draw.
  385. AuxGeomDrawPtr auxGeomPtr = worklistData->GetAuxGeomPtr();
  386. if (auxGeomPtr)
  387. {
  388. if (nodeIsContainedInFrustum && worklistData->m_debugCtx->m_drawFullyVisibleNodes)
  389. {
  390. auxGeomPtr->DrawAabb(nodeData.m_bounds, Colors::Lime, RPI::AuxGeomDraw::DrawStyle::Line, RPI::AuxGeomDraw::DepthTest::Off);
  391. }
  392. else if (!nodeIsContainedInFrustum && worklistData->m_debugCtx->m_drawPartiallyVisibleNodes)
  393. {
  394. auxGeomPtr->DrawAabb(nodeData.m_bounds, Colors::Yellow, RPI::AuxGeomDraw::DrawStyle::Line, RPI::AuxGeomDraw::DepthTest::Off);
  395. }
  396. }
  397. #endif
  398. }
  399. static void ProcessWorklist(const AZStd::shared_ptr<WorklistData>& worklistData, const WorkListType& worklist)
  400. {
  401. AZ_PROFILE_SCOPE(RPI, "Culling: ProcessWorklist");
  402. AZ_Assert(worklist.m_nodes.size() > 0, "Received empty worklist in ProcessWorklist");
  403. for (const AzFramework::IVisibilityScene::NodeData& nodeData : worklist.m_nodes)
  404. {
  405. ProcessVisibilityNode(worklistData, nodeData);
  406. }
  407. }
  408. static bool TestOcclusionCulling(
  409. const AZStd::shared_ptr<WorklistData>& worklistData, const AzFramework::VisibilityEntry* visibleEntry)
  410. {
  411. #ifdef AZ_CULL_PROFILE_VERBOSE
  412. AZ_PROFILE_SCOPE(RPI, "TestOcclusionCulling");
  413. #endif
  414. if (visibleEntry->m_boundingVolume.Contains(worklistData->m_view->GetCameraTransform().GetTranslation()))
  415. {
  416. // camera is inside bounding volume
  417. return true;
  418. }
  419. // Perform occlusion tests using OcclusionRequestBus if it is connected to the entity context ID for this scene.
  420. if (!worklistData->m_sceneEntityContextId.IsNull())
  421. {
  422. bool result = true;
  423. AzFramework::OcclusionRequestBus::EventResult(
  424. result,
  425. worklistData->m_sceneEntityContextId,
  426. &AzFramework::OcclusionRequestBus::Events::IsAabbVisibleInOcclusionView,
  427. worklistData->m_view->GetName(),
  428. visibleEntry->m_boundingVolume);
  429. // Return immediately to bypass MaskedOcclusionCulling
  430. return result;
  431. }
  432. #if AZ_TRAIT_MASKED_OCCLUSION_CULLING_SUPPORTED
  433. MaskedOcclusionCulling* maskedOcclusionCulling = worklistData->m_view->GetMaskedOcclusionCulling();
  434. if (!maskedOcclusionCulling || !worklistData->m_view->GetMaskedOcclusionCullingDirty())
  435. {
  436. return true;
  437. }
  438. const Vector3& minBound = visibleEntry->m_boundingVolume.GetMin();
  439. const Vector3& maxBound = visibleEntry->m_boundingVolume.GetMax();
  440. // compute bounding volume corners
  441. Vector4 corners[8];
  442. corners[0] = worklistData->m_view->GetWorldToClipMatrix() * Vector4(minBound.GetX(), minBound.GetY(), minBound.GetZ(), 1.0f);
  443. corners[1] = worklistData->m_view->GetWorldToClipMatrix() * Vector4(minBound.GetX(), minBound.GetY(), maxBound.GetZ(), 1.0f);
  444. corners[2] = worklistData->m_view->GetWorldToClipMatrix() * Vector4(maxBound.GetX(), minBound.GetY(), maxBound.GetZ(), 1.0f);
  445. corners[3] = worklistData->m_view->GetWorldToClipMatrix() * Vector4(maxBound.GetX(), minBound.GetY(), minBound.GetZ(), 1.0f);
  446. corners[4] = worklistData->m_view->GetWorldToClipMatrix() * Vector4(minBound.GetX(), maxBound.GetY(), minBound.GetZ(), 1.0f);
  447. corners[5] = worklistData->m_view->GetWorldToClipMatrix() * Vector4(minBound.GetX(), maxBound.GetY(), maxBound.GetZ(), 1.0f);
  448. corners[6] = worklistData->m_view->GetWorldToClipMatrix() * Vector4(maxBound.GetX(), maxBound.GetY(), maxBound.GetZ(), 1.0f);
  449. corners[7] = worklistData->m_view->GetWorldToClipMatrix() * Vector4(maxBound.GetX(), maxBound.GetY(), minBound.GetZ(), 1.0f);
  450. // find min clip-space depth and NDC min/max
  451. float minDepth = FLT_MAX;
  452. float ndcMinX = FLT_MAX;
  453. float ndcMinY = FLT_MAX;
  454. float ndcMaxX = -FLT_MAX;
  455. float ndcMaxY = -FLT_MAX;
  456. for (uint32_t index = 0; index < 8; ++index)
  457. {
  458. minDepth = AZStd::min(minDepth, corners[index].GetW());
  459. if (minDepth < 0.00000001f)
  460. {
  461. return true;
  462. }
  463. // convert to NDC
  464. corners[index] /= corners[index].GetW();
  465. ndcMinX = AZStd::min(ndcMinX, corners[index].GetX());
  466. ndcMinY = AZStd::min(ndcMinY, corners[index].GetY());
  467. ndcMaxX = AZStd::max(ndcMaxX, corners[index].GetX());
  468. ndcMaxY = AZStd::max(ndcMaxY, corners[index].GetY());
  469. }
  470. // test against the occlusion buffer, which contains only the manually placed occlusion planes
  471. if (maskedOcclusionCulling->TestRect(ndcMinX, ndcMinY, ndcMaxX, ndcMaxY, minDepth) !=
  472. MaskedOcclusionCulling::CullingResult::VISIBLE)
  473. {
  474. return false;
  475. }
  476. #endif
  477. return true;
  478. }
  479. void CullingScene::ProcessCullablesCommon(
  480. const Scene& scene,
  481. View& view,
  482. AZ::Frustum& frustum [[maybe_unused]])
  483. {
  484. AZ_PROFILE_SCOPE(RPI, "CullingScene::ProcessCullablesCommon() - %s", view.GetName().GetCStr());
  485. #ifdef AZ_CULL_DEBUG_ENABLED
  486. if (m_debugCtx.m_freezeFrustums)
  487. {
  488. AZStd::lock_guard<AZStd::mutex> lock(m_debugCtx.m_frozenFrustumsMutex);
  489. auto iter = m_debugCtx.m_frozenFrustums.find(&view);
  490. if (iter != m_debugCtx.m_frozenFrustums.end())
  491. {
  492. frustum = iter->second;
  493. }
  494. }
  495. if (m_debugCtx.m_debugDraw && m_debugCtx.m_drawViewFrustum && view.GetName() == m_debugCtx.m_currentViewSelectionName)
  496. {
  497. AuxGeomDrawPtr auxGeomPtr = AuxGeomFeatureProcessorInterface::GetDrawQueueForScene(&scene);
  498. if (auxGeomPtr)
  499. {
  500. auxGeomPtr->DrawFrustum(frustum, AZ::Colors::White);
  501. }
  502. }
  503. if (m_debugCtx.m_enableStats)
  504. {
  505. CullingDebugContext::CullStats& cullStats = m_debugCtx.GetCullStatsForView(&view);
  506. cullStats.m_cameraViewToWorld = view.GetViewToWorldMatrix();
  507. }
  508. #endif //AZ_CULL_DEBUG_ENABLED
  509. // If connected, update the occlusion views for this scene and view combination.
  510. if (const auto& entityContextId = GetEntityContextIdForOcclusion(&scene); !entityContextId.IsNull())
  511. {
  512. AzFramework::OcclusionRequestBus::Event(
  513. entityContextId,
  514. &AzFramework::OcclusionRequestBus::Events::UpdateOcclusionView,
  515. view.GetName(),
  516. view.GetCameraTransform().GetTranslation(),
  517. view.GetWorldToClipMatrix());
  518. // Return immediately to bypass MaskedOcclusionCulling
  519. return;
  520. }
  521. #if AZ_TRAIT_MASKED_OCCLUSION_CULLING_SUPPORTED
  522. // setup occlusion culling, if necessary
  523. MaskedOcclusionCulling* maskedOcclusionCulling = view.GetMaskedOcclusionCulling();
  524. if (maskedOcclusionCulling && !m_occlusionPlanes.empty())
  525. {
  526. // frustum cull occlusion planes
  527. using VisibleOcclusionPlane = AZStd::pair<OcclusionPlane, float>;
  528. AZStd::vector<VisibleOcclusionPlane> visibleOccluders;
  529. visibleOccluders.reserve(m_occlusionPlanes.size());
  530. for (const auto& occlusionPlane : m_occlusionPlanes)
  531. {
  532. if (ShapeIntersection::Overlaps(frustum, occlusionPlane.m_aabb))
  533. {
  534. // occluder is visible, compute view space distance and add to list
  535. float depth = (view.GetWorldToViewMatrix() * occlusionPlane.m_aabb.GetMin()).GetZ();
  536. depth = AZStd::min(depth, (view.GetWorldToViewMatrix() * occlusionPlane.m_aabb.GetMax()).GetZ());
  537. visibleOccluders.emplace_back(occlusionPlane, depth);
  538. }
  539. }
  540. // sort the occlusion planes by view space distance, front-to-back
  541. AZStd::sort(visibleOccluders.begin(), visibleOccluders.end(), [](const VisibleOcclusionPlane& LHS, const VisibleOcclusionPlane& RHS)
  542. {
  543. return LHS.second > RHS.second;
  544. });
  545. bool anyVisible = false;
  546. for (const VisibleOcclusionPlane& occlusionPlane : visibleOccluders)
  547. {
  548. // convert to clip-space
  549. const Vector4 projectedBL = view.GetWorldToClipMatrix() * Vector4(occlusionPlane.first.m_cornerBL);
  550. const Vector4 projectedTL = view.GetWorldToClipMatrix() * Vector4(occlusionPlane.first.m_cornerTL);
  551. const Vector4 projectedTR = view.GetWorldToClipMatrix() * Vector4(occlusionPlane.first.m_cornerTR);
  552. const Vector4 projectedBR = view.GetWorldToClipMatrix() * Vector4(occlusionPlane.first.m_cornerBR);
  553. // store to float array
  554. float verts[16];
  555. projectedBL.StoreToFloat4(&verts[0]);
  556. projectedTL.StoreToFloat4(&verts[4]);
  557. projectedTR.StoreToFloat4(&verts[8]);
  558. projectedBR.StoreToFloat4(&verts[12]);
  559. static constexpr const uint32_t indices[6] = { 0, 1, 2, 2, 3, 0 };
  560. // render into the occlusion buffer, specifying BACKFACE_NONE so it functions as a double-sided occluder
  561. if (static_cast<MaskedOcclusionCulling*>(maskedOcclusionCulling)
  562. ->RenderTriangles(verts, indices, 2, nullptr, MaskedOcclusionCulling::BACKFACE_NONE) ==
  563. MaskedOcclusionCulling::CullingResult::VISIBLE)
  564. {
  565. anyVisible = true;
  566. }
  567. }
  568. if (anyVisible)
  569. {
  570. view.SetMaskedOcclusionCullingDirty(true);
  571. }
  572. }
  573. #endif
  574. }
  575. void CullingScene::ProcessCullables(const Scene& scene, View& view, AZ::Job* parentJob, AZ::TaskGraph* taskGraph, AZ::TaskGraphEvent* taskGraphEvent)
  576. {
  577. AZ_PROFILE_SCOPE(RPI, "CullingScene::ProcessCullables() - %s", view.GetName().GetCStr());
  578. AZ_Assert(parentJob != nullptr || taskGraph != nullptr, "ProcessCullables must have either a valid parent job or a valid task graph");
  579. const Matrix4x4& worldToClip = view.GetWorldToClipMatrix();
  580. AZ::Frustum frustum = Frustum::CreateFromMatrixColumnMajor(worldToClip);
  581. ProcessCullablesCommon(scene, view, frustum);
  582. AZStd::shared_ptr<WorkListType> worklist = AZStd::make_shared<WorkListType>();
  583. worklist->Init();
  584. AZStd::shared_ptr<WorklistData> worklistData = MakeWorklistData(m_debugCtx, scene, view, frustum, parentJob, taskGraphEvent);
  585. static const AZ::TaskDescriptor descriptor{ "AZ::RPI::ProcessWorklist", "Graphics" };
  586. if (const Matrix4x4* worldToClipExclude = view.GetWorldToClipExcludeMatrix())
  587. {
  588. worklistData->m_hasExcludeFrustum = true;
  589. worklistData->m_excludeFrustum = Frustum::CreateFromMatrixColumnMajor(*worldToClipExclude);
  590. // Get the render pipeline associated with the shadow pass of the given view
  591. RenderPipelinePtr renderPipeline = scene.GetRenderPipeline(view.GetShadowPassRenderPipelineId());
  592. //Only apply this optimization if you only have one view available.
  593. if (renderPipeline && renderPipeline->GetViews(renderPipeline->GetMainViewTag()).size() == 1)
  594. {
  595. RPI::ViewPtr cameraView = renderPipeline->GetDefaultView();
  596. const Matrix4x4& cameraWorldToClip = cameraView->GetWorldToClipMatrix();
  597. worklistData->m_cameraFrustum = Frustum::CreateFromMatrixColumnMajor(cameraWorldToClip);
  598. worklistData->m_applyCameraFrustumIntersectionTest = true;
  599. }
  600. }
  601. auto nodeVisitorLambda = [worklistData, taskGraph, parentJob, &worklist](const AzFramework::IVisibilityScene::NodeData& nodeData) -> void
  602. {
  603. // For shadow cascades that are greater than index 0 we can do another check to see if we can reject any Octree node that do not
  604. // intersect with the camera frustum. We do this by checking for an overlap between the camera frustum and the Obb created
  605. // from the node's AABB but rotated and extended towards light direction. This optimization is only activated when someone sets
  606. // a non-negative extrusion value (i.e r_shadowCascadeExtrusionAmount) for their given content.
  607. if (r_shadowCascadeExtrusionAmount >= 0 && worklistData->m_applyCameraFrustumIntersectionTest && worklistData->m_hasExcludeFrustum)
  608. {
  609. // Build an Obb from the Octree node's aabb
  610. AZ::Obb extrudedBounds = AZ::Obb::CreateFromAabb(nodeData.m_bounds);
  611. // Rotate the Obb in the direction of the light
  612. AZ::Quaternion directionalLightRot = worklistData->m_view->GetCameraTransform().GetRotation();
  613. extrudedBounds.SetRotation(directionalLightRot);
  614. AZ::Vector3 halfLength = 0.5f * nodeData.m_bounds.GetExtents();
  615. // After converting AABB to OBB we apply a rotation and this can incorrectly fail intersection test. If you have an OBB cube built from an octree node,
  616. // rotating it can cause it to not encapsulate meshes it encapsulated beforehand. The type of shape we want here is essentially a capsule that starts from the
  617. // light and wraps the aabb of the octree node cube and extends towards light direction. This capsule's diameter needs to the size of the body diagonal
  618. // of the cube. Since using capsule shape will make intersection test expensive we simply expand the Obb to have each side be at least the size of the body diagonal
  619. // which is sqrt(3) * side size. Hence we expand the Obb by 73%. Since this is half length, we expand it by 73% / 2, or 36.5%.
  620. halfLength *= Vector3(1.365f);
  621. // Next we extrude the Obb in the direction of the light in order to ensure we capture meshes that are behind the camera but cast a shadow within it's frustum
  622. halfLength.SetY(halfLength.GetY() + r_shadowCascadeExtrusionAmount);
  623. extrudedBounds.SetHalfLengths(halfLength);
  624. if (!AZ::ShapeIntersection::Overlaps(worklistData->m_cameraFrustum, extrudedBounds))
  625. {
  626. return;
  627. }
  628. }
  629. auto entriesInNode = nodeData.m_entries.size();
  630. AZ_Assert(entriesInNode > 0, "should not get called with 0 entries");
  631. // Check job spawn condition for entries
  632. bool spawnJob = r_useEntryCountForNodeJobs && (worklist->m_entryCount > 0) &&
  633. ((worklist->m_entryCount + entriesInNode) > r_numEntriesPerCullingJob);
  634. // Check job spawn condition for nodes
  635. spawnJob = spawnJob || (worklist->m_nodes.size() == worklist->m_nodes.capacity());
  636. if (spawnJob)
  637. {
  638. // capture worklistData & worklist by value
  639. auto processWorklist = [worklistData, worklist]()
  640. {
  641. ProcessWorklist(worklistData, *worklist);
  642. };
  643. if (taskGraph != nullptr)
  644. {
  645. taskGraph->AddTask(descriptor, [worklistData, worklist]()
  646. {
  647. ProcessWorklist(worklistData, *worklist);
  648. });
  649. }
  650. else
  651. {
  652. //Kick off a job to process the (full) worklist
  653. AZ::Job* job = AZ::CreateJobFunction(processWorklist, true);
  654. parentJob->SetContinuation(job);
  655. job->Start();
  656. }
  657. worklist = AZStd::make_shared<WorkListType>();
  658. worklist->Init();
  659. }
  660. worklist->m_nodes.emplace_back(AZStd::move(nodeData));
  661. worklist->m_entryCount += u32(entriesInNode);
  662. };
  663. if (m_debugCtx.m_enableFrustumCulling)
  664. {
  665. if (worklistData->m_hasExcludeFrustum)
  666. {
  667. m_visScene->Enumerate(frustum, worklistData->m_excludeFrustum, nodeVisitorLambda);
  668. }
  669. else
  670. {
  671. m_visScene->Enumerate(frustum, nodeVisitorLambda);
  672. }
  673. }
  674. else
  675. {
  676. m_visScene->EnumerateNoCull(nodeVisitorLambda);
  677. }
  678. if (worklist->m_nodes.size() > 0)
  679. {
  680. // capture worklistData & worklist by value
  681. auto processWorklist = [worklistData, worklist]()
  682. {
  683. ProcessWorklist(worklistData, *worklist);
  684. };
  685. if (taskGraph != nullptr)
  686. {
  687. taskGraph->AddTask(descriptor, AZStd::move(processWorklist));
  688. }
  689. else
  690. {
  691. //Kick off a job to process the (full) worklist
  692. AZ::Job* job = AZ::CreateJobFunction(AZStd::move(processWorklist), true);
  693. parentJob->SetContinuation(job);
  694. job->Start();
  695. }
  696. }
  697. }
  698. // Fastest of the three functions: ProcessCullablesJobsEntries, ProcessCullablesJobsNodes, ProcessCullablesTG
  699. void CullingScene::ProcessCullablesJobsEntries(const Scene& scene, View& view, AZ::Job* parentJob)
  700. {
  701. AZ_PROFILE_SCOPE(RPI, "CullingScene::ProcessCullablesJobsEntries() - %s", view.GetName().GetCStr());
  702. const Matrix4x4& worldToClip = view.GetWorldToClipMatrix();
  703. AZ::Frustum frustum = Frustum::CreateFromMatrixColumnMajor(worldToClip);
  704. ProcessCullablesCommon(scene, view, frustum);
  705. // Note 1: Cannot do unique_ptr here because compilation error (auto-deletes function from lambda which the job code complains about)
  706. // Note 2: Having this be a pointer (even a shared pointer) is faster than just having this live on the stack like:
  707. // EntryListType entryList;
  708. // Why isn't immediately clear (did profile several times and noticed the difference of ~0.2-0.3ms, seems making it a stack variable
  709. // increases the runtime for this function, which runs on a single thread and spawns other jobs).
  710. AZStd::shared_ptr<EntryListType> entryList = AZStd::make_shared<EntryListType>();
  711. entryList->m_entries.reserve(r_numEntriesPerCullingJob);
  712. AZStd::shared_ptr<WorklistData> worklistData = MakeWorklistData(m_debugCtx, scene, view, frustum, parentJob, nullptr);
  713. if (const Matrix4x4* worldToClipExclude = view.GetWorldToClipExcludeMatrix())
  714. {
  715. worklistData->m_hasExcludeFrustum = true;
  716. worklistData->m_excludeFrustum = Frustum::CreateFromMatrixColumnMajor(*worldToClipExclude);
  717. }
  718. auto nodeVisitorLambda = [worklistData, parentJob, &entryList](const AzFramework::IVisibilityScene::NodeData& nodeData) -> void
  719. {
  720. AZ_Assert(nodeData.m_entries.size() > 0, "should not get called with 0 entries");
  721. AZ_Assert(entryList->m_entries.size() < entryList->m_entries.capacity(), "we should always have room to push a node on the queue");
  722. u32 remainingCount = u32(nodeData.m_entries.size());
  723. u32 current = 0;
  724. while (remainingCount > 0)
  725. {
  726. u32 availableCount = u32(entryList->m_entries.capacity() - entryList->m_entries.size());
  727. u32 addCount = AZStd::min(availableCount, remainingCount);
  728. for (u32 i = 0; i < addCount; ++i)
  729. {
  730. entryList->m_entries.push_back(nodeData.m_entries[current++]);
  731. }
  732. remainingCount -= addCount;
  733. if (entryList->m_entries.size() == entryList->m_entries.capacity())
  734. {
  735. auto processWorklist = [worklistData, entryList = AZStd::move(entryList)]()
  736. {
  737. ProcessEntrylist(worklistData, entryList->m_entries);
  738. };
  739. AZ::Job* job = AZ::CreateJobFunction(processWorklist, true);
  740. entryList = AZStd::make_shared<EntryListType>();
  741. entryList->m_entries.reserve(r_numEntriesPerCullingJob);
  742. parentJob->SetContinuation(job);
  743. job->Start();
  744. }
  745. }
  746. };
  747. if (m_debugCtx.m_enableFrustumCulling)
  748. {
  749. m_visScene->Enumerate(frustum, nodeVisitorLambda);
  750. }
  751. else
  752. {
  753. m_visScene->EnumerateNoCull(nodeVisitorLambda);
  754. }
  755. if (entryList->m_entries.size() > 0)
  756. {
  757. auto processWorklist = [worklistData, entryList = AZStd::move(entryList)]()
  758. {
  759. ProcessEntrylist(worklistData, entryList->m_entries);
  760. };
  761. AZ::Job* job = AZ::CreateJobFunction(processWorklist, true);
  762. parentJob->SetContinuation(job);
  763. job->Start();
  764. }
  765. }
  766. void CullingScene::ProcessCullablesJobs(const Scene& scene, View& view, AZ::Job& parentJob)
  767. {
  768. if (r_useEntryWorkListsForCulling)
  769. {
  770. ProcessCullablesJobsEntries(scene, view, &parentJob);
  771. }
  772. else
  773. {
  774. ProcessCullables(scene, view, &parentJob, nullptr);
  775. }
  776. }
  777. void CullingScene::ProcessCullablesTG(const Scene& scene, View& view, AZ::TaskGraph& taskGraph, AZ::TaskGraphEvent& taskGraphEvent)
  778. {
  779. ProcessCullables(scene, view, nullptr, &taskGraph, &taskGraphEvent);
  780. }
  781. uint32_t AddLodDataToView(
  782. const Vector3& pos, const Cullable::LodData& lodData, RPI::View& view, AzFramework::VisibilityEntry::TypeFlags typeFlags)
  783. {
  784. #ifdef AZ_CULL_PROFILE_DETAILED
  785. AZ_PROFILE_SCOPE(RPI, "AddLodDataToView");
  786. #endif
  787. uint32_t numVisibleDrawPackets = 0;
  788. auto addLodToDrawPacket = [&](const Cullable::LodData::Lod& lod)
  789. {
  790. #ifdef AZ_CULL_PROFILE_VERBOSE
  791. AZ_PROFILE_SCOPE(RPI, "add draw packets: %zu", lod.m_drawPackets.size());
  792. #endif
  793. numVisibleDrawPackets += static_cast<uint32_t>(lod.m_drawPackets.size()); //don't want to pay the cost of aznumeric_cast<> here so using static_cast<> instead
  794. if (typeFlags & AzFramework::VisibilityEntry::TYPE_RPI_VisibleObjectList)
  795. {
  796. view.AddVisibleObject(lod.m_visibleObjectUserData, pos);
  797. }
  798. else if (typeFlags & AzFramework::VisibilityEntry::TYPE_RPI_Cullable)
  799. {
  800. for (const RHI::DrawPacket* drawPacket : lod.m_drawPackets)
  801. {
  802. view.AddDrawPacket(drawPacket, pos);
  803. }
  804. }
  805. else
  806. {
  807. AZ_Assert(false, "Invalid cullable type flags.")
  808. }
  809. };
  810. switch (lodData.m_lodConfiguration.m_lodType)
  811. {
  812. case Cullable::LodType::SpecificLod:
  813. if (lodData.m_lodConfiguration.m_lodOverride < lodData.m_lods.size())
  814. {
  815. addLodToDrawPacket(
  816. lodData.m_lods.at(lodData.m_lodConfiguration.m_lodOverride));
  817. }
  818. break;
  819. case Cullable::LodType::ScreenCoverage:
  820. default:
  821. {
  822. const Matrix4x4& viewToClip = view.GetViewToClipMatrix();
  823. // the [1][1] element of a perspective projection matrix stores cot(FovY/2) (equal to
  824. // 2*nearPlaneDistance/nearPlaneHeight), which is used to determine the (vertical) projected size in screen space
  825. const float yScale = viewToClip.GetElement(1, 1);
  826. const bool isPerspective = viewToClip.GetElement(3, 3) == 0.f;
  827. const Vector3 cameraPos = view.GetViewToWorldMatrix().GetTranslation();
  828. const float approxScreenPercentage =
  829. ModelLodUtils::ApproxScreenPercentage(pos, lodData.m_lodSelectionRadius, cameraPos, yScale, isPerspective);
  830. for (uint32_t lodIndex = 0; lodIndex < static_cast<uint32_t>(lodData.m_lods.size()); ++lodIndex)
  831. {
  832. const Cullable::LodData::Lod& lod = lodData.m_lods[lodIndex];
  833. // Note that this supports overlapping lod ranges (to support cross-fading lods, for example)
  834. if (approxScreenPercentage >= lod.m_screenCoverageMin && approxScreenPercentage <= lod.m_screenCoverageMax)
  835. {
  836. addLodToDrawPacket(lod);
  837. }
  838. }
  839. break;
  840. }
  841. }
  842. return numVisibleDrawPackets;
  843. }
  844. void CullingScene::Activate(const Scene* parentScene)
  845. {
  846. m_parentScene = parentScene;
  847. m_visScene = parentScene->GetVisibilityScene();
  848. m_taskGraphActive = AZ::Interface<AZ::TaskGraphActiveInterface>::Get();
  849. if (auto* console = AZ::Interface<AZ::IConsole>::Get(); console != nullptr)
  850. {
  851. // Start with default value
  852. int shadowCascadeExtrusionAmount = r_shadowCascadeExtrusionAmount;
  853. // Get the cvar value from settings registry
  854. console->GetCvarValue("r_shadowCascadeExtrusionAmount", shadowCascadeExtrusionAmount);
  855. // push the cvars value so anything in this dll can access it directly.
  856. console->PerformCommand(
  857. AZStd::string::format("r_shadowCascadeExtrusionAmount %i", shadowCascadeExtrusionAmount).c_str());
  858. }
  859. #ifdef AZ_CULL_DEBUG_ENABLED
  860. AZ_Assert(CountObjectsInScene() == 0, "The culling system should start with 0 entries in this scene.");
  861. #endif
  862. }
  863. void CullingScene::Deactivate()
  864. {
  865. #ifdef AZ_CULL_DEBUG_ENABLED
  866. AZ_Assert(CountObjectsInScene() == 0, "All culling entries must be removed from the scene before shutdown.");
  867. #endif
  868. m_visScene = nullptr;
  869. }
  870. void CullingScene::BeginCullingTaskGraph(const Scene& scene, AZStd::span<const ViewPtr> views)
  871. {
  872. AZ::TaskGraph taskGraph{ "RPI::Culling" };
  873. AZ::TaskDescriptor beginCullingDescriptor{ "RPI_CullingScene_BeginCullingView", "Graphics" };
  874. const auto& entityContextId = GetEntityContextIdForOcclusion(&scene);
  875. for (auto& view : views)
  876. {
  877. taskGraph.AddTask(
  878. beginCullingDescriptor,
  879. [&]()
  880. {
  881. AZ_PROFILE_SCOPE(RPI, "CullingScene: BeginCullingTaskGraph");
  882. view->BeginCulling();
  883. AzFramework::OcclusionRequestBus::Event(
  884. entityContextId, &AzFramework::OcclusionRequestBus::Events::CreateOcclusionView, view->GetName());
  885. });
  886. }
  887. if (!taskGraph.IsEmpty())
  888. {
  889. AZ::TaskGraphEvent waitForCompletion{ "RPI::Culling Wait" };
  890. taskGraph.Submit(&waitForCompletion);
  891. waitForCompletion.Wait();
  892. }
  893. }
  894. void CullingScene::BeginCullingJobs(const Scene& scene, AZStd::span<const ViewPtr> views)
  895. {
  896. AZ::JobCompletion beginCullingCompletion;
  897. const auto& entityContextId = GetEntityContextIdForOcclusion(&scene);
  898. for (auto& view : views)
  899. {
  900. const auto cullingLambda = [&]()
  901. {
  902. AZ_PROFILE_SCOPE(RPI, "CullingScene: BeginCullingJob");
  903. view->BeginCulling();
  904. AzFramework::OcclusionRequestBus::Event(
  905. entityContextId, &AzFramework::OcclusionRequestBus::Events::CreateOcclusionView, view->GetName());
  906. };
  907. AZ::Job* cullingJob = AZ::CreateJobFunction(AZStd::move(cullingLambda), true, nullptr);
  908. cullingJob->SetDependent(&beginCullingCompletion);
  909. cullingJob->Start();
  910. }
  911. beginCullingCompletion.StartAndWaitForCompletion();
  912. }
  913. void CullingScene::BeginCulling(const Scene& scene, AZStd::span<const ViewPtr> views)
  914. {
  915. AZ_PROFILE_SCOPE(RPI, "CullingScene: BeginCulling");
  916. m_cullDataConcurrencyCheck.soft_lock();
  917. m_debugCtx.ResetCullStats();
  918. m_debugCtx.m_numCullablesInScene = GetNumCullables();
  919. m_taskGraphActive = AZ::Interface<AZ::TaskGraphActiveInterface>::Get();
  920. // Remove any debug artifacts from the previous occlusion culling session.
  921. const auto& entityContextId = GetEntityContextIdForOcclusion(&scene);
  922. AzFramework::OcclusionRequestBus::Event(
  923. entityContextId, &AzFramework::OcclusionRequestBus::Events::ClearOcclusionViewDebugInfo);
  924. if (views.size() == 1) // avoid job overhead when only 1 job
  925. {
  926. views[0]->BeginCulling();
  927. AzFramework::OcclusionRequestBus::Event(
  928. entityContextId, &AzFramework::OcclusionRequestBus::Events::CreateOcclusionView, views[0]->GetName());
  929. }
  930. else if (m_taskGraphActive && m_taskGraphActive->IsTaskGraphActive())
  931. {
  932. BeginCullingTaskGraph(scene, views);
  933. }
  934. else
  935. {
  936. BeginCullingJobs(scene, views);
  937. }
  938. #ifdef AZ_CULL_DEBUG_ENABLED
  939. AuxGeomDrawPtr auxGeom;
  940. if (m_debugCtx.m_debugDraw)
  941. {
  942. auxGeom = AuxGeomFeatureProcessorInterface::GetDrawQueueForScene(m_parentScene);
  943. AZ_Assert(auxGeom, "Invalid AuxGeomFeatureProcessorInterface");
  944. if (m_debugCtx.m_drawWorldCoordinateAxes)
  945. {
  946. DebugDrawWorldCoordinateAxes(auxGeom.get());
  947. }
  948. }
  949. {
  950. AZStd::lock_guard<AZStd::mutex> lockFrozenFrustums(m_debugCtx.m_frozenFrustumsMutex);
  951. if (m_debugCtx.m_freezeFrustums)
  952. {
  953. for (const ViewPtr& viewPtr : views)
  954. {
  955. auto iter = m_debugCtx.m_frozenFrustums.find(viewPtr.get());
  956. if (iter == m_debugCtx.m_frozenFrustums.end())
  957. {
  958. const Matrix4x4& worldToClip = viewPtr->GetWorldToClipMatrix();
  959. Frustum frustum = Frustum::CreateFromMatrixColumnMajor(worldToClip, Frustum::ReverseDepth::True);
  960. m_debugCtx.m_frozenFrustums.insert({ viewPtr.get(), frustum });
  961. }
  962. }
  963. }
  964. else if(m_debugCtx.m_frozenFrustums.size() > 0)
  965. {
  966. m_debugCtx.m_frozenFrustums.clear();
  967. }
  968. }
  969. #endif
  970. }
  971. void CullingScene::EndCulling(const Scene& scene, AZStd::span<const ViewPtr> views)
  972. {
  973. m_cullDataConcurrencyCheck.soft_unlock();
  974. // When culling has completed, destroy all of the occlusion views.
  975. if (const auto& entityContextId = GetEntityContextIdForOcclusion(&scene); !entityContextId.IsNull())
  976. {
  977. for (auto& view : views)
  978. {
  979. AzFramework::OcclusionRequestBus::Event(
  980. entityContextId, &AzFramework::OcclusionRequestBus::Events::DestroyOcclusionView, view->GetName());
  981. }
  982. }
  983. }
  984. size_t CullingScene::CountObjectsInScene()
  985. {
  986. size_t numObjects = 0;
  987. m_visScene->EnumerateNoCull(
  988. [&numObjects](const AzFramework::IVisibilityScene::NodeData& nodeData)
  989. {
  990. for (AzFramework::VisibilityEntry* visibleEntry : nodeData.m_entries)
  991. {
  992. if (visibleEntry->m_typeFlags & AzFramework::VisibilityEntry::TYPE_RPI_Cullable ||
  993. visibleEntry->m_typeFlags & AzFramework::VisibilityEntry::TYPE_RPI_VisibleObjectList)
  994. {
  995. ++numObjects;
  996. }
  997. }
  998. }
  999. );
  1000. return numObjects;
  1001. }
  1002. } // namespace RPI
  1003. } // namespace AZ