Plasma Engine  2.0
Loading...
Searching...
No Matches
SpatialSystem_RegularGrid.h
1#pragma once
2
3#include <Core/World/SpatialSystem.h>
4#include <Foundation/Containers/IdTable.h>
5#include <Foundation/SimdMath/SimdVec4i.h>
6#include <Foundation/Types/UniquePtr.h>
7
8namespace plInternal
9{
10 struct QueryHelper;
11}
12
14{
15 PL_ADD_DYNAMIC_REFLECTION(plSpatialSystem_RegularGrid, plSpatialSystem);
16
17public:
18 plSpatialSystem_RegularGrid(plUInt32 uiCellSize = 128);
20
22 plResult GetCellBoxForSpatialData(const plSpatialDataHandle& hData, plBoundingBox& out_boundingBox) const;
23
25 void GetAllCellBoxes(plDynamicArray<plBoundingBox>& out_boundingBoxes, plSpatialData::Category filterCategory = plInvalidSpatialDataCategory) const;
26
27private:
28 friend plInternal::QueryHelper;
29
30 // plSpatialSystem implementation
31 virtual void StartNewFrame() override;
32
33 plSpatialDataHandle CreateSpatialData(const plSimdBBoxSphere& bounds, plGameObject* pObject, plUInt32 uiCategoryBitmask, const plTagSet& tags) override;
34 plSpatialDataHandle CreateSpatialDataAlwaysVisible(plGameObject* pObject, plUInt32 uiCategoryBitmask, const plTagSet& tags) override;
35
36 void DeleteSpatialData(const plSpatialDataHandle& hData) override;
37
38 void UpdateSpatialDataBounds(const plSpatialDataHandle& hData, const plSimdBBoxSphere& bounds) override;
39 void UpdateSpatialDataObject(const plSpatialDataHandle& hData, plGameObject* pObject) override;
40
41 void FindObjectsInSphere(const plBoundingSphere& sphere, const QueryParams& queryParams, QueryCallback callback) const override;
42 void FindObjectsInBox(const plBoundingBox& box, const QueryParams& queryParams, QueryCallback callback) const override;
43
44 void FindVisibleObjects(const plFrustum& frustum, const QueryParams& queryParams, plDynamicArray<const plGameObject*>& out_Objects, plSpatialSystem::IsOccludedFunc IsOccluded, plVisibilityState visType) const override;
45
46 plVisibilityState GetVisibilityState(const plSpatialDataHandle& hData, plUInt32 uiNumFramesBeforeInvisible) const override;
47
48#if PL_ENABLED(PL_COMPILE_FOR_DEVELOPMENT)
49 virtual void GetInternalStats(plStringBuilder& sb) const override;
50#endif
51
52 plProxyAllocator m_AlignedAllocator;
53
54 plSimdVec4i m_vCellSize;
55 plSimdVec4f m_vOverlapSize;
56 plSimdFloat m_fInvCellSize;
57
58 enum
59 {
60 MAX_NUM_GRIDS = 63,
61 MAX_NUM_REGULAR_GRIDS = (sizeof(plSpatialData::Category::m_uiValue) * 8),
62 MAX_NUM_CACHED_GRIDS = MAX_NUM_GRIDS - MAX_NUM_REGULAR_GRIDS
63 };
64
65 struct Cell;
66 struct Grid;
68 plUInt32 m_uiFirstCachedGridIndex = MAX_NUM_GRIDS;
69
70 struct Data
71 {
72 PL_DECLARE_POD_TYPE();
73
74 plUInt64 m_uiGridBitmask : MAX_NUM_GRIDS;
75 plUInt64 m_uiAlwaysVisible : 1;
76 };
77
79
80 bool IsAlwaysVisibleData(const Data& data) const;
81
82 plSpatialDataHandle AddSpatialDataToGrids(const plSimdBBoxSphere& bounds, plGameObject* pObject, plUInt32 uiCategoryBitmask, const plTagSet& tags, bool bAlwaysVisible);
83
84 template <typename Functor>
85 void ForEachGrid(const Data& data, const plSpatialDataHandle& hData, Functor func) const;
86
87 struct Stats;
88 using CellCallback = plDelegate<plVisitorExecution::Enum(const Cell&, const QueryParams&, Stats&, void*, plVisibilityState)>;
89 void ForEachCellInBoxInMatchingGrids(const plSimdBBox& box, const QueryParams& queryParams, CellCallback noFilterCallback, CellCallback filterByTagsCallback, void* pUserData, plVisibilityState visType) const;
90
91 struct CacheCandidate
92 {
93 plTagSet m_IncludeTags;
94 plTagSet m_ExcludeTags;
95 plSpatialData::Category m_Category;
96 float m_fQueryCount = 0.0f;
97 float m_fFilteredRatio = 0.0f;
98 plUInt32 m_uiGridIndex = plInvalidIndex;
99 };
100
101 mutable plDynamicArray<CacheCandidate> m_CacheCandidates;
102 mutable plMutex m_CacheCandidatesMutex;
103
104 struct SortedCacheCandidate
105 {
106 plUInt32 m_uiIndex = 0;
107 float m_fScore = 0;
108
109 bool operator<(const SortedCacheCandidate& other) const
110 {
111 if (m_fScore != other.m_fScore)
112 return m_fScore > other.m_fScore; // higher score comes first
113
114 return m_uiIndex < other.m_uiIndex;
115 }
116 };
117
118 plDynamicArray<SortedCacheCandidate> m_SortedCacheCandidates;
119
120 void MigrateCachedGrid(plUInt32 uiCandidateIndex);
121 void MigrateSpatialData(plUInt32 uiTargetGridIndex, plUInt32 uiSourceGridIndex);
122
123 void RemoveCachedGrid(plUInt32 uiCandidateIndex);
124 void RemoveAllCachedGrids();
125
126 void UpdateCacheCandidate(const plTagSet* pIncludeTags, const plTagSet* pExcludeTags, plSpatialData::Category category, float filteredRatio) const;
127};
Definition DynamicArray.h:81
Represents the frustum of some camera and can be used for culling objects.
Definition Frustum.h:32
This class represents an object inside the world.
Definition GameObject.h:32
Definition IdTable.h:171
Provides a simple mechanism for mutual exclusion to prevent multiple threads from accessing a shared ...
Definition Mutex.h:13
Definition SimdBBox.h:6
Definition SimdBBoxSphere.h:6
Definition SimdFloat.h:7
A 4-component SIMD vector class.
Definition SimdVec4f.h:8
A SIMD 4-component vector class of signed 32b integers.
Definition SimdVec4i.h:9
Definition Declarations.h:353
Definition SpatialSystem_RegularGrid.h:14
Definition SpatialSystem.h:10
virtual plVisibilityState GetVisibilityState(const plSpatialDataHandle &hData, plUInt32 uiNumFramesBeforeInvisible) const =0
Retrieves a state describing how visible the object is.
plStringBuilder is a class that is meant for creating and modifying strings.
Definition StringBuilder.h:35
A dynamic collection of tags featuring fast lookups.
Definition TagSet.h:23
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54
Definition SpatialData.h:27
Definition SpatialSystem.h:47
Enum
Definition Declarations.h:344