[373] | 1 | #include "BatchedQueriesCullingManager.h" |
---|
| 2 | #include <OgreLogManager.h> |
---|
| 3 | |
---|
| 4 | namespace GtpVisibility { |
---|
| 5 | |
---|
| 6 | //----------------------------------------------------------------------- |
---|
| 7 | BatchedQueriesCullingManager::BatchedQueriesCullingManager(): |
---|
| 8 | mMaxPending(5) |
---|
| 9 | { |
---|
| 10 | } |
---|
| 11 | //----------------------------------------------------------------------- |
---|
| 12 | BatchedQueriesCullingManager::BatchedQueriesCullingManager(const unsigned int |
---|
| 13 | assumedVisibility): |
---|
| 14 | CoherentHierarchicalCullingManager(assumedVisibility), mMaxPending(5) |
---|
| 15 | { |
---|
| 16 | } |
---|
| 17 | |
---|
| 18 | //----------------------------------------------------------------------- |
---|
| 19 | void BatchedQueriesCullingManager::RenderScene() |
---|
| 20 | { |
---|
| 21 | QueryQueue queryQueue; |
---|
| 22 | unsigned int visiblePixels = 0; |
---|
| 23 | bool isAvailable = false; |
---|
| 24 | |
---|
| 25 | //Ogre::LogManager::getSingleton().logMessage("Batched Culling"); |
---|
| 26 | |
---|
| 27 | PendingQueue pendingQueue; |
---|
| 28 | |
---|
| 29 | //-- PART 1: process finished occlusion queries |
---|
| 30 | while (!mHierarchyInterface->GetQueue()->empty() || !queryQueue.empty() ||!pendingQueue.empty()) |
---|
| 31 | { |
---|
| 32 | while (!queryQueue.empty() && |
---|
| 33 | queryQueue.front().second->GetQueryResult(visiblePixels, |
---|
| 34 | mHierarchyInterface->GetQueue()->empty())) |
---|
| 35 | { |
---|
| 36 | HierarchyNode *node = queryQueue.front().first; |
---|
| 37 | |
---|
| 38 | queryQueue.pop(); |
---|
| 39 | |
---|
| 40 | if (visiblePixels > mVisibilityThreshold) |
---|
| 41 | { |
---|
| 42 | // ensure that we only traverse once if geometry in node |
---|
| 43 | if (!mHierarchyInterface->IsNodeVisible(node)) |
---|
| 44 | mHierarchyInterface->TraverseNode(node); |
---|
| 45 | mHierarchyInterface->PullUpVisibility(node); |
---|
| 46 | } |
---|
| 47 | else |
---|
| 48 | { |
---|
| 49 | mHierarchyInterface->SetNodeVisible(node, false); |
---|
| 50 | |
---|
| 51 | ++ mNumQueryCulledNodes; |
---|
| 52 | |
---|
| 53 | if (mVisualizeCulledNodes) |
---|
| 54 | { |
---|
| 55 | mHierarchyInterface->VisualizeCulledNode(node, QUERY_CULLED); |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | //-- PART 2: hierarchical traversal |
---|
| 61 | if (!mHierarchyInterface->GetQueue()->empty()) |
---|
| 62 | { |
---|
| 63 | HierarchyNode *node = mHierarchyInterface->GetQueue()->top(); |
---|
| 64 | mHierarchyInterface->GetQueue()->pop(); |
---|
| 65 | |
---|
| 66 | bool intersects = false; |
---|
| 67 | |
---|
| 68 | if (!mHierarchyInterface->CheckFrustumVisible(node, intersects)) |
---|
| 69 | { |
---|
| 70 | ++ mNumFrustumCulledNodes; |
---|
| 71 | |
---|
| 72 | if (mVisualizeCulledNodes) |
---|
| 73 | { |
---|
| 74 | mHierarchyInterface->VisualizeCulledNode(node, FRUSTUM_CULLED); |
---|
| 75 | } |
---|
| 76 | } |
---|
| 77 | // -- if node intersects near plane, skip query because wrong results possible |
---|
| 78 | else if (intersects) |
---|
| 79 | { |
---|
| 80 | SkipQuery(node); |
---|
| 81 | } |
---|
| 82 | else |
---|
| 83 | { |
---|
| 84 | // identify previously visible nodes |
---|
| 85 | bool wasVisible = mHierarchyInterface->IsNodeVisible(node) && |
---|
| 86 | (mHierarchyInterface->LastVisited(node) == mHierarchyInterface->GetFrameId() - 1); |
---|
| 87 | |
---|
| 88 | // if we assume node to be visible in this frame => skip query |
---|
| 89 | bool skipQuery = wasVisible && (mAssumedVisibility > 0) && |
---|
| 90 | DecideVisible(node) && mHierarchyInterface->HasGeometry(node); |
---|
| 91 | |
---|
| 92 | //if (mHierarchyInterface->LastVisited(node) >= mHierarchyInterface->GetFrameId()) |
---|
| 93 | // Ogre::LogManager::getSingleton().logMessage("error"); |
---|
| 94 | |
---|
| 95 | if (skipQuery) |
---|
| 96 | { |
---|
| 97 | SkipQuery(node); |
---|
| 98 | |
---|
| 99 | continue; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | // identify nodes that we cannot skip queries for |
---|
| 103 | // geometry not only in leaves => test for renderable geometry |
---|
| 104 | bool issueQuery = !wasVisible || mHierarchyInterface->HasGeometry(node); |
---|
| 105 | |
---|
| 106 | // reset node's visibility classification |
---|
| 107 | // set visibe if geometry in node so we only traverse once |
---|
| 108 | mHierarchyInterface->SetNodeVisible(node, wasVisible && issueQuery); |
---|
| 109 | |
---|
| 110 | // update node's visited flag |
---|
| 111 | mHierarchyInterface->SetLastVisited(node, mHierarchyInterface->GetFrameId()); |
---|
| 112 | |
---|
| 113 | // skip testing previously visible nodes without geometry |
---|
| 114 | if (issueQuery) |
---|
| 115 | { |
---|
| 116 | ++ mNumQueriesIssued; |
---|
| 117 | // add to the pending queue instead of immediate query
|
---|
| 118 | if ((int)pendingQueue.size() < mMaxPending)
|
---|
| 119 | pendingQueue.push(PendingQuery(node, wasVisible));
|
---|
| 120 | else
|
---|
| 121 | {
|
---|
| 122 | IssueMultipleQueries(pendingQueue, queryQueue);
|
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | // always traverse a node if it was visible |
---|
| 127 | if (wasVisible) |
---|
| 128 | { |
---|
| 129 | mHierarchyInterface->TraverseNode(node); |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | // add rest of queries |
---|
| 134 | IssueMultipleQueries(pendingQueue, queryQueue); |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | void BatchedQueriesCullingManager::IssueMultipleQueries(PendingQueue &pendingQueue, |
---|
| 139 | QueryQueue &queryQueue) |
---|
| 140 | { |
---|
| 141 | while (!pendingQueue.empty()) |
---|
| 142 | { |
---|
| 143 | HierarchyNode *node = pendingQueue.front().first; |
---|
| 144 | const bool wasVisible = pendingQueue.front().second; |
---|
| 145 | |
---|
| 146 | pendingQueue.pop(); |
---|
| 147 | |
---|
| 148 | queryQueue.push(QueryPair(node, mHierarchyInterface-> |
---|
| 149 | IssueNodeOcclusionQuery(node, wasVisible))); |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | void BatchedQueriesCullingManager::SetMaxPending(int maxPending) |
---|
| 154 | { |
---|
| 155 | mMaxPending = maxPending; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | } // namespace GtpVisibility |
---|