[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 | { |
---|
[2455] | 103 | // fully visible subtree => render all in one bathc |
---|
[2258] | 104 | if (mHierarchyInterface->IsNodeFullyVisible(node)) |
---|
[2281] | 105 | { |
---|
[2455] | 106 | // use different algorithm for finding random candidates |
---|
[2292] | 107 | HierarchyNodeContainer mynodes; |
---|
| 108 | mHierarchyInterface->CollectLeaves(node, mynodes); |
---|
[2258] | 109 | |
---|
[2455] | 110 | const int p = mRandomCandidates * RAND_MAX / (int)mynodes.size(); |
---|
| 111 | |
---|
[2292] | 112 | HierarchyNodeContainer::const_iterator nit, nit_end = mynodes.end(); |
---|
[2283] | 113 | |
---|
[2455] | 114 | int nodesTested = 0; |
---|
[2292] | 115 | for (nit = mynodes.begin(); nit != nit_end; ++ nit) |
---|
| 116 | { |
---|
| 117 | HierarchyNode *leaf = *nit; |
---|
[2455] | 118 | |
---|
| 119 | if (rand() > p) |
---|
| 120 | continue; |
---|
| 121 | |
---|
| 122 | ++ nodesTested; |
---|
| 123 | |
---|
[2292] | 124 | mHierarchyInterface->SetNodeVisible(leaf, false); |
---|
| 125 | |
---|
| 126 | // update node's visited flag |
---|
[2306] | 127 | mHierarchyInterface->PullUpLastVisited(leaf, mHierarchyInterface->GetFrameId()); |
---|
[2292] | 128 | |
---|
[2455] | 129 | const bool testGeometry =mTestGeometryForVisibleLeaves; |
---|
| 130 | |
---|
| 131 | mHierarchyInterface->IssueNodeOcclusionQuery(node, mTestGeometryForVisibleLeaves); |
---|
[2292] | 132 | } |
---|
[2455] | 133 | |
---|
| 134 | //std::stringstream d; d << "rc: " << mRandomCandidates << " tested: " << nodesTested << " of " << (int)mynodes.size(); |
---|
| 135 | //CullingLogManager::GetSingleton()->LogMessage(d.str()); |
---|
[2292] | 136 | |
---|
[2455] | 137 | mHierarchyInterface->RenderNodeRecursive(node); |
---|
[2258] | 138 | continue; |
---|
| 139 | } |
---|
| 140 | |
---|
[2332] | 141 | // identify previously visible nodes |
---|
| 142 | const bool wasVisible = mHierarchyInterface->IsNodeVisible(node) && |
---|
| 143 | (mHierarchyInterface->LastVisited(node) == mHierarchyInterface->GetFrameId() - 1); |
---|
| 144 | |
---|
| 145 | // if we assume node to be visible in this frame => skip query |
---|
[2455] | 146 | const bool skipQuery = false;//wasVisible && mHierarchyInterface->HasGeometry(node); |
---|
[2332] | 147 | |
---|
| 148 | if (skipQuery) |
---|
| 149 | { |
---|
| 150 | SkipQuery(node); |
---|
| 151 | continue; |
---|
| 152 | } |
---|
| 153 | |
---|
[2258] | 154 | // identify nodes that we cannot skip queries for |
---|
| 155 | // geometry not only in leaves => test for renderable geometry |
---|
| 156 | const bool issueQuery = !wasVisible || mHierarchyInterface->HasGeometry(node); |
---|
| 157 | |
---|
| 158 | // reset node's visibility classification |
---|
[2306] | 159 | // set visible if geometry in node => we only traverse the node once |
---|
[2258] | 160 | mHierarchyInterface->SetNodeVisible(node, wasVisible && issueQuery); |
---|
| 161 | |
---|
| 162 | // update node's visited flag |
---|
| 163 | mHierarchyInterface->SetLastVisited(node, mHierarchyInterface->GetFrameId()); |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | // skip testing previously visible nodes without geometry |
---|
| 167 | if (issueQuery) |
---|
| 168 | { |
---|
| 169 | ++ mNumQueriesIssued; |
---|
| 170 | |
---|
[2455] | 171 | const bool testGeometry = wasVisible && mHierarchyInterface->IsLeaf(node) && mTestGeometryForVisibleLeaves; |
---|
| 172 | |
---|
[2258] | 173 | queryQueue.push(QueryPair(node, mHierarchyInterface-> |
---|
[2455] | 174 | IssueNodeOcclusionQuery(node, testGeometry))); |
---|
[2258] | 175 | } |
---|
| 176 | |
---|
| 177 | // always traverse a node if it was visible |
---|
| 178 | if (wasVisible) |
---|
| 179 | { |
---|
| 180 | mHierarchyInterface->TraverseNode(node); |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | } |
---|
[2292] | 185 | |
---|
| 186 | // update the fully visible classifications |
---|
| 187 | mHierarchyInterface->DetermineFullVisibility(mHierarchyInterface->GetHierarchyRoot()); |
---|
[2258] | 188 | } |
---|
| 189 | //----------------------------------------------------------------------- |
---|
| 190 | inline void RandomUpdateCullingManager::SkipQuery(HierarchyNode *node) const |
---|
| 191 | { |
---|
| 192 | // -- set node to be visible in this frame, then traverse it |
---|
| 193 | mHierarchyInterface->SetLastVisited(node, mHierarchyInterface->GetFrameId()); |
---|
| 194 | |
---|
| 195 | mHierarchyInterface->PullUpVisibility(node); |
---|
| 196 | mHierarchyInterface->TraverseNode(node); |
---|
| 197 | } |
---|
[2455] | 198 | //----------------------------------------------------------------------- |
---|
| 199 | void RandomUpdateCullingManager::SetTestGeometryForVisibleLeaves(const bool testGeometry)
|
---|
| 200 | {
|
---|
| 201 | mTestGeometryForVisibleLeaves = testGeometry;
|
---|
| 202 | }
|
---|
| 203 | //-----------------------------------------------------------------------
|
---|
| 204 | bool RandomUpdateCullingManager::GetTestGeometryForVisibleLeaves()
|
---|
| 205 | {
|
---|
| 206 | return mTestGeometryForVisibleLeaves;
|
---|
| 207 | } |
---|
[2258] | 208 | |
---|
| 209 | } // namespace GtpVisibility |
---|