1 | #include "RandomUpdateCullingManager.h" |
---|
2 | #include <time.h> |
---|
3 | #include "CullingLogManager.h" |
---|
4 | #include <vector> |
---|
5 | //#include <iostream>
|
---|
6 | #include <sstream> |
---|
7 | |
---|
8 | |
---|
9 | using namespace std; |
---|
10 | |
---|
11 | |
---|
12 | namespace GtpVisibility { |
---|
13 | |
---|
14 | const static int R_CANDIDATES = 1; |
---|
15 | |
---|
16 | //----------------------------------------------------------------------- |
---|
17 | RandomUpdateCullingManager::RandomUpdateCullingManager(): mRandomCandidates(R_CANDIDATES) |
---|
18 | { |
---|
19 | } |
---|
20 | //----------------------------------------------------------------------- |
---|
21 | RandomUpdateCullingManager::RandomUpdateCullingManager(const unsigned int randomCandidates) |
---|
22 | { |
---|
23 | mRandomCandidates = std::max(randomCandidates, (unsigned int)1); |
---|
24 | } |
---|
25 | //----------------------------------------------------------------------- |
---|
26 | void RandomUpdateCullingManager::SetRandomCandidates(const unsigned int randomCandidates) |
---|
27 | { |
---|
28 | mRandomCandidates = std::max(randomCandidates, (unsigned int)1); |
---|
29 | } |
---|
30 | //----------------------------------------------------------------------- |
---|
31 | void RandomUpdateCullingManager::RenderScene() |
---|
32 | { |
---|
33 | if (0) CullingLogManager::GetSingleton()->LogMessage("ruc"); |
---|
34 | |
---|
35 | QueryQueue queryQueue; |
---|
36 | unsigned int visiblePixels = 0; |
---|
37 | |
---|
38 | ///////////// |
---|
39 | //-- PART 1: process finished occlusion queries |
---|
40 | |
---|
41 | while (!mHierarchyInterface->GetQueue()->empty() || !queryQueue.empty()) |
---|
42 | { |
---|
43 | bool resultAvailable = false; |
---|
44 | |
---|
45 | //-- only wait for result if there are no nodes to process |
---|
46 | while (!queryQueue.empty() && |
---|
47 | queryQueue.front().second->GetQueryResult(visiblePixels, |
---|
48 | mHierarchyInterface->GetQueue()->empty())) |
---|
49 | { |
---|
50 | HierarchyNode *node = queryQueue.front().first; |
---|
51 | |
---|
52 | queryQueue.pop(); |
---|
53 | |
---|
54 | if (visiblePixels > mVisibilityThreshold) |
---|
55 | { |
---|
56 | // in case geometry is in interior node: |
---|
57 | // ensure that we only traverse once |
---|
58 | if (!mHierarchyInterface->IsNodeVisible(node) && |
---|
59 | !mHierarchyInterface->IsNodeFullyVisible(node)) |
---|
60 | { |
---|
61 | mHierarchyInterface->TraverseNode(node); |
---|
62 | } |
---|
63 | |
---|
64 | mHierarchyInterface->PullUpVisibility(node); |
---|
65 | } |
---|
66 | else |
---|
67 | { |
---|
68 | mHierarchyInterface->SetNodeVisible(node, false); |
---|
69 | |
---|
70 | ++ mNumQueryCulledNodes; |
---|
71 | |
---|
72 | if (mVisualizeCulledNodes) |
---|
73 | { |
---|
74 | mHierarchyInterface->VisualizeCulledNode(node, QUERY_CULLED); |
---|
75 | } |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | //-- PART 2: hierarchical traversal |
---|
80 | if (!mHierarchyInterface->GetQueue()->empty()) |
---|
81 | { |
---|
82 | HierarchyNode *node = mHierarchyInterface->GetQueue()->top(); |
---|
83 | mHierarchyInterface->GetQueue()->pop(); |
---|
84 | |
---|
85 | bool intersects = false; |
---|
86 | |
---|
87 | if (!mHierarchyInterface->CheckFrustumVisible(node, intersects)) |
---|
88 | { |
---|
89 | ++ mNumFrustumCulledNodes; |
---|
90 | |
---|
91 | if (mVisualizeCulledNodes) |
---|
92 | { |
---|
93 | mHierarchyInterface->VisualizeCulledNode(node, FRUSTUM_CULLED); |
---|
94 | } |
---|
95 | } |
---|
96 | //-- if node intersects near plane, skip query because wrong results possible |
---|
97 | else if (intersects) |
---|
98 | { |
---|
99 | SkipQuery(node); |
---|
100 | } |
---|
101 | else |
---|
102 | { |
---|
103 | // fully visible subtree => render all in one batch |
---|
104 | if (mHierarchyInterface->IsNodeFullyVisible(node)) |
---|
105 | { |
---|
106 | int nodesTested = 0; |
---|
107 | |
---|
108 | // use different algorithm to find random candidates |
---|
109 | #if 1 |
---|
110 | HierarchyNodeContainer mynodes; |
---|
111 | mHierarchyInterface->CollectLeaves(node, mynodes); |
---|
112 | |
---|
113 | const int p = mRandomCandidates * RAND_MAX / (int)mynodes.size(); |
---|
114 | |
---|
115 | HierarchyNodeContainer::const_iterator nit, nit_end = mynodes.end(); |
---|
116 | |
---|
117 | for (nit = mynodes.begin(); nit != nit_end; ++ nit) |
---|
118 | { |
---|
119 | HierarchyNode *leaf = *nit; |
---|
120 | |
---|
121 | if (rand() > p) |
---|
122 | continue; |
---|
123 | |
---|
124 | bool intersects = false; |
---|
125 | const bool frustumCulled = !mHierarchyInterface->CheckFrustumVisible(leaf, intersects); |
---|
126 | |
---|
127 | // don't test in these cases ... |
---|
128 | if (frustumCulled || intersects) |
---|
129 | continue; |
---|
130 | |
---|
131 | ++ nodesTested; |
---|
132 | |
---|
133 | mHierarchyInterface->SetNodeVisible(leaf, false); |
---|
134 | |
---|
135 | // update node's visited flag: this is important as we are not testing |
---|
136 | // all nodes in the hierarchy in this mode |
---|
137 | mHierarchyInterface->PullUpLastVisited(leaf, mHierarchyInterface->GetFrameId()); |
---|
138 | // issue the query |
---|
139 | mHierarchyInterface->IssueNodeOcclusionQuery(node, mTestGeometryForVisibleLeaves); |
---|
140 | } |
---|
141 | |
---|
142 | //std::stringstream d; d << "rc: " << mRandomCandidates << " tested: " << nodesTested << " of " << (int)mynodes.size(); |
---|
143 | //CullingLogManager::GetSingleton()->LogMessage(d.str()); |
---|
144 | |
---|
145 | #else |
---|
146 | |
---|
147 | HierarchyNode *leaf = mHierarchyInterface->GetRandomLeaf(node); |
---|
148 | |
---|
149 | bool intersects = false; |
---|
150 | const bool frustumCulled = !mHierarchyInterface->CheckFrustumVisible(leaf, intersects); |
---|
151 | |
---|
152 | if (frustumCulled || intersects) // don't test in these cases ... |
---|
153 | continue; |
---|
154 | |
---|
155 | ++ nodesTested; |
---|
156 | |
---|
157 | mHierarchyInterface->SetNodeVisible(leaf, false); |
---|
158 | |
---|
159 | // update node's visited flag: this is important as we are not testing |
---|
160 | // all nodes in the hierarchy in this mode |
---|
161 | mHierarchyInterface->PullUpLastVisited(leaf, mHierarchyInterface->GetFrameId()); |
---|
162 | // issue the query |
---|
163 | mHierarchyInterface->IssueNodeOcclusionQuery(leaf, mTestGeometryForVisibleLeaves); |
---|
164 | |
---|
165 | #endif |
---|
166 | |
---|
167 | mHierarchyInterface->RenderNodeRecursive(node); |
---|
168 | |
---|
169 | // bail out here, recursion has finished |
---|
170 | continue; |
---|
171 | } |
---|
172 | |
---|
173 | // identify previously visible nodes |
---|
174 | const bool wasVisible = mHierarchyInterface->IsNodeVisible(node) && |
---|
175 | (mHierarchyInterface->LastVisited(node) == mHierarchyInterface->GetFrameId() - 1); |
---|
176 | |
---|
177 | // if we assume node to be visible in this frame => skip query |
---|
178 | const bool skipQuery = false; |
---|
179 | //wasVisible && mHierarchyInterface->HasGeometry(node); |
---|
180 | |
---|
181 | if (skipQuery) |
---|
182 | { |
---|
183 | SkipQuery(node); |
---|
184 | continue; |
---|
185 | } |
---|
186 | |
---|
187 | // identify nodes that we cannot skip queries for |
---|
188 | // geometry not only in leaves => test for renderable geometry |
---|
189 | const bool issueQuery = !wasVisible || mHierarchyInterface->HasGeometry(node); |
---|
190 | |
---|
191 | // reset node's visibility classification |
---|
192 | // set visible if geometry in node => we only traverse the node once |
---|
193 | mHierarchyInterface->SetNodeVisible(node, wasVisible && issueQuery); |
---|
194 | |
---|
195 | // update node's visited flag |
---|
196 | mHierarchyInterface->SetLastVisited(node, mHierarchyInterface->GetFrameId()); |
---|
197 | |
---|
198 | |
---|
199 | // skip testing previously visible nodes without geometry |
---|
200 | if (issueQuery) |
---|
201 | { |
---|
202 | ++ mNumQueriesIssued; |
---|
203 | |
---|
204 | const bool testGeometry = wasVisible && mHierarchyInterface->IsLeaf(node) && mTestGeometryForVisibleLeaves; |
---|
205 | |
---|
206 | queryQueue.push(QueryPair(node, mHierarchyInterface-> |
---|
207 | IssueNodeOcclusionQuery(node, testGeometry))); |
---|
208 | } |
---|
209 | |
---|
210 | // always traverse a node if it was visible |
---|
211 | if (wasVisible) |
---|
212 | { |
---|
213 | mHierarchyInterface->TraverseNode(node); |
---|
214 | } |
---|
215 | } |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | // update the fully visible classifications |
---|
220 | mHierarchyInterface->DetermineFullVisibility(mHierarchyInterface->GetHierarchyRoot()); |
---|
221 | } |
---|
222 | //----------------------------------------------------------------------- |
---|
223 | inline void RandomUpdateCullingManager::SkipQuery(HierarchyNode *node) const |
---|
224 | { |
---|
225 | // -- set node to be visible in this frame, then traverse it |
---|
226 | mHierarchyInterface->SetLastVisited(node, mHierarchyInterface->GetFrameId()); |
---|
227 | |
---|
228 | mHierarchyInterface->PullUpVisibility(node); |
---|
229 | mHierarchyInterface->TraverseNode(node); |
---|
230 | } |
---|
231 | //----------------------------------------------------------------------- |
---|
232 | void RandomUpdateCullingManager::SetTestGeometryForVisibleLeaves(const bool testGeometry)
|
---|
233 | {
|
---|
234 | mTestGeometryForVisibleLeaves = testGeometry;
|
---|
235 | }
|
---|
236 | //-----------------------------------------------------------------------
|
---|
237 | bool RandomUpdateCullingManager::GetTestGeometryForVisibleLeaves()
|
---|
238 | {
|
---|
239 | return mTestGeometryForVisibleLeaves;
|
---|
240 | } |
---|
241 | |
---|
242 | } // namespace GtpVisibility |
---|