[2258] | 1 | #include "RandomUpdateCullingManager.h" |
---|
[2280] | 2 | #include <time.h> |
---|
| 3 | #include "CullingLogManager.h" |
---|
[2292] | 4 | #include <vector> |
---|
| 5 | //#include <iostream>
|
---|
| 6 | #include <sstream> |
---|
[2258] | 7 | |
---|
[2280] | 8 | |
---|
[2292] | 9 | using namespace std; |
---|
| 10 | |
---|
| 11 | |
---|
[2258] | 12 | namespace GtpVisibility { |
---|
| 13 | |
---|
[2287] | 14 | const static int R_CANDIDATES = 1; |
---|
[2455] | 15 | |
---|
[2258] | 16 | //----------------------------------------------------------------------- |
---|
[2455] | 17 | RandomUpdateCullingManager::RandomUpdateCullingManager(): mRandomCandidates(R_CANDIDATES) |
---|
[2258] | 18 | { |
---|
| 19 | } |
---|
| 20 | //----------------------------------------------------------------------- |
---|
[2455] | 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) |
---|
[2258] | 27 | { |
---|
[2455] | 28 | mRandomCandidates = std::max(randomCandidates, (unsigned int)1); |
---|
[2258] | 29 | } |
---|
| 30 | //----------------------------------------------------------------------- |
---|
| 31 | void RandomUpdateCullingManager::RenderScene() |
---|
| 32 | { |
---|
[2292] | 33 | if (0) CullingLogManager::GetSingleton()->LogMessage("ruc"); |
---|
[2287] | 34 | |
---|
[2258] | 35 | QueryQueue queryQueue; |
---|
| 36 | unsigned int visiblePixels = 0; |
---|
| 37 | |
---|
| 38 | ///////////// |
---|
| 39 | //-- PART 1: process finished occlusion queries |
---|
[2287] | 40 | |
---|
[2258] | 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 |
---|
[2259] | 58 | if (!mHierarchyInterface->IsNodeVisible(node) && |
---|
| 59 | !mHierarchyInterface->IsNodeFullyVisible(node)) |
---|
[2258] | 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 | { |
---|
[2287] | 89 | ++ mNumFrustumCulledNodes; |
---|
| 90 | |
---|
[2258] | 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 | { |
---|
[2502] | 103 | // fully visible subtree => render all in one batch |
---|
[2258] | 104 | if (mHierarchyInterface->IsNodeFullyVisible(node)) |
---|
[2281] | 105 | { |
---|
[2532] | 106 | int nodesTested = 0; |
---|
| 107 | |
---|
[2455] | 108 | // use different algorithm for finding random candidates |
---|
[2532] | 109 | #if 1 |
---|
[2292] | 110 | HierarchyNodeContainer mynodes; |
---|
| 111 | mHierarchyInterface->CollectLeaves(node, mynodes); |
---|
[2258] | 112 | |
---|
[2455] | 113 | const int p = mRandomCandidates * RAND_MAX / (int)mynodes.size(); |
---|
| 114 | |
---|
[2292] | 115 | HierarchyNodeContainer::const_iterator nit, nit_end = mynodes.end(); |
---|
[2283] | 116 | |
---|
[2292] | 117 | for (nit = mynodes.begin(); nit != nit_end; ++ nit) |
---|
| 118 | { |
---|
| 119 | HierarchyNode *leaf = *nit; |
---|
[2455] | 120 | |
---|
| 121 | if (rand() > p) |
---|
| 122 | continue; |
---|
| 123 | |
---|
[2532] | 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 | |
---|
[2455] | 131 | ++ nodesTested; |
---|
[2532] | 132 | |
---|
[2292] | 133 | mHierarchyInterface->SetNodeVisible(leaf, false); |
---|
| 134 | |
---|
[2502] | 135 | // update node's visited flag: this is important as we are not testing |
---|
| 136 | // all nodes in the hierarchy in this mode |
---|
[2306] | 137 | mHierarchyInterface->PullUpLastVisited(leaf, mHierarchyInterface->GetFrameId()); |
---|
[2502] | 138 | // issue the query |
---|
[2455] | 139 | mHierarchyInterface->IssueNodeOcclusionQuery(node, mTestGeometryForVisibleLeaves); |
---|
[2292] | 140 | } |
---|
[2532] | 141 | |
---|
[2455] | 142 | //std::stringstream d; d << "rc: " << mRandomCandidates << " tested: " << nodesTested << " of " << (int)mynodes.size(); |
---|
| 143 | //CullingLogManager::GetSingleton()->LogMessage(d.str()); |
---|
[2292] | 144 | |
---|
[2532] | 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 | |
---|
[2455] | 167 | mHierarchyInterface->RenderNodeRecursive(node); |
---|
[2532] | 168 | |
---|
| 169 | // bail out here, recursion has finished |
---|
[2258] | 170 | continue; |
---|
| 171 | } |
---|
| 172 | |
---|
[2332] | 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 |
---|
[2497] | 178 | const bool skipQuery = false; |
---|
| 179 | //wasVisible && mHierarchyInterface->HasGeometry(node); |
---|
[2332] | 180 | |
---|
| 181 | if (skipQuery) |
---|
| 182 | { |
---|
| 183 | SkipQuery(node); |
---|
| 184 | continue; |
---|
| 185 | } |
---|
| 186 | |
---|
[2258] | 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 |
---|
[2306] | 192 | // set visible if geometry in node => we only traverse the node once |
---|
[2258] | 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 | |
---|
[2455] | 204 | const bool testGeometry = wasVisible && mHierarchyInterface->IsLeaf(node) && mTestGeometryForVisibleLeaves; |
---|
| 205 | |
---|
[2258] | 206 | queryQueue.push(QueryPair(node, mHierarchyInterface-> |
---|
[2455] | 207 | IssueNodeOcclusionQuery(node, testGeometry))); |
---|
[2258] | 208 | } |
---|
| 209 | |
---|
| 210 | // always traverse a node if it was visible |
---|
| 211 | if (wasVisible) |
---|
| 212 | { |
---|
| 213 | mHierarchyInterface->TraverseNode(node); |
---|
| 214 | } |
---|
| 215 | } |
---|
| 216 | } |
---|
| 217 | } |
---|
[2292] | 218 | |
---|
| 219 | // update the fully visible classifications |
---|
| 220 | mHierarchyInterface->DetermineFullVisibility(mHierarchyInterface->GetHierarchyRoot()); |
---|
[2258] | 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 | } |
---|
[2455] | 231 | //----------------------------------------------------------------------- |
---|
| 232 | void RandomUpdateCullingManager::SetTestGeometryForVisibleLeaves(const bool testGeometry)
|
---|
| 233 | {
|
---|
| 234 | mTestGeometryForVisibleLeaves = testGeometry;
|
---|
| 235 | }
|
---|
| 236 | //-----------------------------------------------------------------------
|
---|
| 237 | bool RandomUpdateCullingManager::GetTestGeometryForVisibleLeaves()
|
---|
| 238 | {
|
---|
| 239 | return mTestGeometryForVisibleLeaves;
|
---|
| 240 | } |
---|
[2258] | 241 | |
---|
| 242 | } // namespace GtpVisibility |
---|