gwnavruntime/database/databaseupdatemanager.h Source File

databaseupdatemanager.h
Go to the documentation of this file.
1 /*
2 * Copyright 2016 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 #pragma once
8 
15 
16 namespace Kaim
17 {
18 
19 class World;
20 class Database;
21 class TagVolume;
22 
23 class DynamicNavMeshQuery;
24 class MakeNavFloorStitchQuery;
25 class QueryCounterOnDone;
26 typedef Collection<Ptr<TagVolume>, MemStat_NavData> TagVolumeCollection;
27 class NavCell;
28 
29 enum DatabaseUpdateStatus
30 {
31  DatabaseUpdateManagerIdle = 0,
32 
33  UpdatingNavDataInDatabases_ProcessingNavData = 1,
34  UpdatingNavDataInDatabases_WaitingDynamicNavMeshQueries = 2,
35  UpdatingNavDataInDatabases_PerformingStitchOneToOne = 3,
36  UpdatingNavDataInDatabases_WaitingRunTimeStitchQueries = 4,
37  UpdatingNavDataInDatabases_FinalizeNavDataUpdate = 5,
38 
39  UpdatingTagVolumeIntegration_ProcessingTagVolumes = 6,
40  UpdatingTagVolumeIntegration_WaitingDynamicNavMeshQueries = 7,
41  UpdatingTagVolumeIntegration_ProcessingDynNavMeshResults = 8,
42  UpdatingTagVolumeIntegration_WaitingRunTimeStitchQueries = 9,
43  UpdatingTagVolumeIntegration_FinalizeTagVolumeUpdate = 10,
44 
45  CancellingTagVolumeIntegration = 11,
46 
47  DatabaseUpdateStatus_Count = 12
48 };
49 
50 class DatabaseUpdateManager
51 {
52  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_DynamicNavMesh)
53 
54 public:
55  explicit DatabaseUpdateManager(World* world);
56  ~DatabaseUpdateManager();
57 
58  void Update();
59 
60  void NavDataAddRemoveUpdateImmediate(KyUInt32 databaseIdx = KyUInt32MAXVAL);
61 
62  // called from Generator, Database::Clear()<=BUG?
63  void TagVolumeIntegrationUpdateImmediate();
64 
65  // called from World::ClearAllDatabases(), World::ClearDatabase(KyUInt32), Database::RemoveAllNavDataImmediate(), Database::UpdateImmediate()<=BUG?
66  void CompleteAddRemoveNavDataOrCancelTagVolumeIntegration();
67 
68  // call from World::ForceClearWorkingMemory(), World::RemoveAllTagVolumes()
69  void CancelTagVolumeIntegrationUpdate();
70 
71  void ToggleDebugRender(bool toggle);
72 
73  bool IsIdle() const; // not IsDoingNavDataAddRemoveUpdate() and not IsDoingTagVolumeIntegrationUpdate()
74  bool IsDoingNavDataAddRemoveUpdate() const;
75  bool IsDoingTagVolumeIntegrationUpdate() const;
76 
77  DatabaseUpdateStatus GetStatus() const { return m_status; }
78  void SetStatus(DatabaseUpdateStatus status) { m_status = status; } // internal, used by the NavMeshElementManager
79 
80  bool HasDynamicNavMeshQueriesInProcess() const { return m_dynamicNavmeshQueriesInProcess != 0; } // used in unit-tests only
81  bool HasNavMeshStitchQueriesInProcess() const { return m_stitchQueriesInProcess != 0; } // used in unit-tests only
82 
83  KyInt32 GetDynamicNavMeshQueriesInProcess() const { return m_dynamicNavmeshQueriesInProcess; } // used in VisualDebug only
84  KyInt32 GetNavMeshStitchQueriesInProcess() const { return m_stitchQueriesInProcess; } // used in VisualDebug only
85 
86  // called in FinalizeNavDataAddRemoveUpdate(), FinalizeTagVolumeIntegrationUpdate(), CancelTagVolumeIntegrationUpdate()
87  // and in NavigationLab::LabVisualDebugFramesManager() (<- that's why it's public)
88  void Clear();
89 
90  // called in LabVisualDebugFramesManager::UpdateNavCellInDatabase() because the Lab reconstruct the database "by hand" from
91  // the navcells sent by the visualdebug. Note that the UpdateFromBunchOfCells should be part of the DatabaseUpdateManager class
92  void PushStitchQuery(KyUInt32 databseIndex, const Kaim::Ptr<Kaim::MakeNavFloorStitchQuery>& query);
93  void ExpandCellBoxOfUpdateInDatabase(KyUInt32 databaseIndex, const Kaim::CellPos& cellPos);
94 
95 private:
96  friend class NavCellGrid; // CreateDynamicNavMeshQuery CreateMakeNavFloorStitchQuery
97  // GetNumberOfWaitingDynamicNavMeshQueries GetNumberOfWaitingRunTimeStitchQueries
98 
99  friend class Database; // IsThereWaitingTagVolumesInDatabase EnlargeCellBoxAccordingToWaitingTagVolumes SpatializeWaitingTagVolumesInNavCellGrid
100 
101  void ForceUpdateToCompleteSynchronously();
102 
103  void UpdateFrameIdxInAllDatabases();
104  void ClearChangeInAllDatabases();
105  void PerformNextUpdateStepAccordingToStatus(QueryProcessMode processMode); // Immediate or Async
106 
107  // AddRemoveNavData phase
108  bool IsThereNavDataAddRemovePending() const;
109  void StartNavDataAddRemoveUpdate(QueryProcessMode processMode);
110  void PerformStitchOneToOne(QueryProcessMode processMode);
111  void FinalizeNavDataAddRemoveUpdate();
112 
113  // TagVolumeIntegration phase
114  bool IsThereTagVolumeIntegrationPending() const;
115  void StartTagVolumeIntegrationUpdate(QueryProcessMode processMode);
116  void CreateDynamicNavMeshQueriesForDatabase(KyUInt32 databaseIdx, QueryProcessMode queryProcessMode);
117  void ProcessDynamicNavMeshQueryResults(QueryProcessMode queryProcessMode);
118  void FinalizeTagVolumeIntegrationUpdate();
119 
120  void CreateMakeNavFloorStitchQuery(Database* database, NavCell* navCell, QueryProcessMode queryProcessMode);
121  void CreateDynamicNavMeshQuery(Database* database, NavCell* navCell,
122  const TagVolumeCollection* integratedTagVolumesAtCellPos, const TagVolumeCollection* newTagVolumes, QueryProcessMode queryProcessMode);
123 
124  KyUInt32 GetNumberOfWaitingDynamicNavMeshQueries() const { return m_dynamicNavmeshQueriesInProcess; }
125  KyUInt32 GetNumberOfWaitingRunTimeStitchQueries() const { return m_stitchQueriesInProcess; }
126 
127  void RunSynchronouslyAllNonFinishedQueries(); // cancel all stitch and tagvolume queries ansd launch them again
128 
129  // Call by the database when NavMesh are added in case of the Database was Empty.
130  bool IsThereWaitingTagVolumesInDatabase(KyUInt32 databaseIdx);
131  void EnlargeCellBoxAccordingToWaitingTagVolumes(KyUInt32 databaseIdx, CellBox& currentNavMeshUpdateCellBox);
132  void SpatializeWaitingTagVolumesInNavCellGrid(KyUInt32 databaseIdx);
133 
134  void RenderDisplayLists();
135  void ClearDebugRender();
136 
137  void DebugTagVolumeInUpdateDisplayList();
138 
139  // TagVolumeIntegration phase
140  void TakeSnapshotOfTagVolumesToIntegrateAndToRemove();
141  void ComputeCellBoxOfTagvolumeToIntegratePerDatabase();
142  void MarkAllCellsConcernedByTagVolumeIntegrationUpdate(TagVolume* tagVolume);
143  void MarkAllCellsConcernedByUpdateInDatabase(Database* database, const CellBox& tagVolumeCellBox);
144 
145  void PushNewIntegratedTagVolumeInCellBox(TagVolume* tagVolume, Database* database, const CellBox& cellBoxInDatabase);
146  void RemoveDeIntegratedTagVolumeFromCellBox(TagVolume* tagVolume, Database* database, const CellBox& cellBoxInDatabase);
147 
148  void PostProcessIntegratedTagVolumes();
149  void PostProcessDeIntegratedTagVolumes();
150 
151  void CancelAllQueries();
152 
153  void PrintErrorMessageForFaillingDynamicNavMeshQueries();
154 
155 private:
156  World* m_world;
157 
158  DatabaseUpdateStatus m_status;
159 
160  KyArray<CellBox, MemStat_DynamicNavMesh> m_cellBoxOfUpdateInDatabase; // indexed on databaseIdx
161  KyArray<CellBox, MemStat_DynamicNavMesh> m_cellBoxOfTagvolumeToIntegrate; // indexed on databaseIdx
162 
163  KyArray<Collection<Ptr<TagVolume>, MemStat_DynamicNavMesh>, MemStat_DynamicNavMesh> m_waitingTagVolumesPerDatabase; // indexed on databaseIdx
164 
165  KyArray<KyArray<Ptr<DynamicNavMeshQuery>, MemStat_DynamicNavMesh>, MemStat_DynamicNavMesh> m_dynNavMeshQueriesPerDatabase; // indexed on databaseIdx
166  KyArray<KyArray<Ptr<MakeNavFloorStitchQuery>, MemStat_DynamicNavMesh>, MemStat_DynamicNavMesh> m_stitchQueriesPerDatabase; // indexed on databaseIdx
167 
168  std::atomic<int> m_dynamicNavmeshQueriesInProcess;
169  std::atomic<int> m_stitchQueriesInProcess;
170 
171  Ptr<QueryCounterOnDone> m_OnDoneForDynamicNavMeshQueries;
172  Ptr<QueryCounterOnDone> m_OnDoneForRunTimeStitchQueries;
173 
174  TagVolumeCollection m_tagVolumesInDeIntegration;
175  TagVolumeCollection m_tagVolumesInIntegration;
176 
177  // for visualDebugManager (debug)
178  bool m_debugDisplayListsEnabled;
179  KyArray<DisplayList*> m_debugDisplayLists;
180 };
181 
182 
183 KY_INLINE bool DatabaseUpdateManager::IsIdle() const { return m_status == DatabaseUpdateManagerIdle; }
184 
185 KY_INLINE bool DatabaseUpdateManager::IsDoingNavDataAddRemoveUpdate() const
186 {
187  return m_status >= UpdatingNavDataInDatabases_ProcessingNavData && m_status <= UpdatingNavDataInDatabases_FinalizeNavDataUpdate;
188 }
189 
190 KY_INLINE bool DatabaseUpdateManager::IsDoingTagVolumeIntegrationUpdate() const
191 {
192  return m_status >= UpdatingTagVolumeIntegration_ProcessingTagVolumes && m_status <= UpdatingTagVolumeIntegration_FinalizeTagVolumeUpdate;
193 }
194 
195 KY_INLINE bool DatabaseUpdateManager::IsThereWaitingTagVolumesInDatabase(KyUInt32 databaseIdx)
196 {
197  return m_waitingTagVolumesPerDatabase[databaseIdx].GetCount() != 0;
198 }
199 
200 const char* GetDatabaseUpdateStatusString(DatabaseUpdateStatus status);
201 
202 } // namespace Kaim
203 
Box2i CellBox
A type that represents a bounding box around cells in a 2D grid.
Definition: navmeshtypes.h:31
std::uint32_t KyUInt32
uint32_t
Definition: types.h:29
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:132
2d vector using KyInt32
Definition: vec2i.h:18
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
std::int32_t KyInt32
int32_t
Definition: types.h:24
#define KyUInt32MAXVAL
KyUInt32 max value
Definition: types.h:68