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