-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathDatabasePager.h
More file actions
180 lines (129 loc) · 5.97 KB
/
DatabasePager.h
File metadata and controls
180 lines (129 loc) · 5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#pragma once
/* <editor-fold desc="MIT License">
Copyright(c) 2018 Robert Osfield
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
</editor-fold> */
#include <vsg/app/CompileManager.h>
#include <vsg/core/Inherit.h>
#include <vsg/core/observer_ptr.h>
#include <vsg/io/FileSystem.h>
#include <vsg/io/Options.h>
#include <vsg/nodes/PagedLOD.h>
#include <vsg/threading/DeleteQueue.h>
#include <vsg/utils/Instrumentation.h>
#include <condition_variable>
#include <list>
#include <thread>
namespace vsg
{
/// Class used by the DatabasePager to keep track of PagedLOD nodes
class CulledPagedLODs : public Inherit<Object, CulledPagedLODs>
{
public:
CulledPagedLODs()
{
highresCulled.reserve(512);
newHighresRequired.reserve(8);
}
void clear()
{
highresCulled.clear();
newHighresRequired.clear();
}
std::vector<const PagedLOD*> highresCulled;
std::vector<const PagedLOD*> newHighresRequired;
};
struct PagedLODContainer : public Inherit<Object, PagedLODContainer>
{
explicit PagedLODContainer(uint32_t maxNumPagedLOD = 10000);
struct List
{
uint32_t head = 0;
uint32_t tail = 0;
uint32_t count = 0;
std::string name;
};
struct Element
{
uint32_t previous = 0;
uint32_t next = 0;
ref_ptr<PagedLOD> plod;
List* list = nullptr;
};
using Elements = std::vector<Element>;
Elements elements;
List availableList;
List inactiveList;
List activeList;
void resize(uint32_t new_size);
void resize();
void inactive(const PagedLOD* plod);
void active(const PagedLOD* plod);
void remove(PagedLOD* plod);
void _move(const PagedLOD* plod, List* targetList);
bool check();
bool check(const List& list);
void print(std::ostream& out);
};
/// Thread safe queue for tracking PagedLOD that needs to be loaded, compiled or merged by the DatabasePager
class VSG_DECLSPEC DatabaseQueue : public Inherit<Object, DatabaseQueue>
{
public:
explicit DatabaseQueue(ref_ptr<ActivityStatus> status);
using Nodes = std::list<ref_ptr<PagedLOD>>;
ActivityStatus* getStatus() { return _status; }
const ActivityStatus* getStatus() const { return _status; }
void add(ref_ptr<PagedLOD> plod);
void add(ref_ptr<PagedLOD> plod, const CompileResult& cr);
/// prune entries older than specified frameCount
uint32_t prune(uint64_t frameCount);
ref_ptr<PagedLOD> take_when_available(uint64_t frameCount);
Nodes take_all(CompileResult& result);
protected:
virtual ~DatabaseQueue();
std::mutex _mutex;
std::condition_variable _cv;
Nodes _queue;
CompileResult _compileResult;
ref_ptr<ActivityStatus> _status;
};
VSG_type_name(vsg::DatabaseQueue);
/// Multi-threaded database pager for reading, compiling loaded PagedLOD subgraphs and updating the scene graph
/// with newly loaded subgraphs and pruning expired PagedLOD subgraphs
class VSG_DECLSPEC DatabasePager : public Inherit<Object, DatabasePager>
{
public:
DatabasePager();
DatabasePager(const DatabasePager&) = delete;
DatabasePager& operator=(const DatabasePager& rhs) = delete;
virtual void start(uint32_t numReadThreads = 4);
virtual void request(ref_ptr<PagedLOD> plod);
virtual void updateSceneGraph(ref_ptr<FrameStamp> frameStamp, CompileResult& cr);
ref_ptr<CompileManager> compileManager;
std::atomic_uint numActiveRequests{0};
std::atomic_uint64_t frameCount{0};
ref_ptr<CulledPagedLODs> culledPagedLODs;
/// for systems with smaller GPU memory limits you may need to reduce the targetMaxNumPagedLODWithHighResSubgraphs to keep memory usage within available limits.
uint32_t targetMaxNumPagedLODWithHighResSubgraphs = 1500;
/// number of frames before a PagedLOD with a failed load/compile is attempted to be loaded/compiled again.
uint64_t delayBeforeNextLoadAttempt = 60;
std::mutex pendingPagedLODMutex;
ref_ptr<PagedLODContainer> pagedLODContainer;
/// Hook for assigning Instrumentation to enable profiling
ref_ptr<Instrumentation> instrumentation;
/// assign Instrumentation to all CompileTraversal and their associated Context
void assignInstrumentation(ref_ptr<Instrumentation> in_instrumentation);
/// read and delete threads created by start()
std::list<std::thread> threads;
ref_ptr<ActivityStatus> status;
ref_ptr<DeleteQueue> deleteQueue;
protected:
virtual ~DatabasePager();
void requestDiscarded(PagedLOD* plod);
ref_ptr<DatabaseQueue> _requestQueue;
ref_ptr<DatabaseQueue> _toMergeQueue;
};
VSG_type_name(vsg::DatabasePager);
} // namespace vsg