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 | |
---|
24 | //Ogre::LogManager::getSingleton().logMessage("Batched Culling"); |
---|
25 | |
---|
26 | PendingQueue pendingQueue; |
---|
27 | |
---|
28 | //-- PART 1: process finished occlusion queries |
---|
29 | while (!mHierarchyInterface->GetQueue()->empty() || !queryQueue.empty() ||!pendingQueue.empty()) |
---|
30 | { |
---|
31 | while (!queryQueue.empty() && |
---|
32 | queryQueue.front().second->GetQueryResult(visiblePixels, |
---|
33 | mHierarchyInterface->GetQueue()->empty())) |
---|
34 | { |
---|
35 | HierarchyNode *node = queryQueue.front().first; |
---|
36 | |
---|
37 | queryQueue.pop(); |
---|
38 | |
---|
39 | if (visiblePixels > mVisibilityThreshold) |
---|
40 | { |
---|
41 | // ensure that we only traverse once if geometry in node |
---|
42 | if (!mHierarchyInterface->IsNodeVisible(node)) |
---|
43 | mHierarchyInterface->TraverseNode(node); |
---|
44 | |
---|
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 | |
---|
134 | // issue rest of queries |
---|
135 | IssueMultipleQueries(pendingQueue, queryQueue); |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | void BatchedQueriesCullingManager::IssueMultipleQueries(PendingQueue &pendingQueue, |
---|
140 | QueryQueue &queryQueue) |
---|
141 | { |
---|
142 | while (!pendingQueue.empty()) |
---|
143 | { |
---|
144 | HierarchyNode *node = pendingQueue.front().first; |
---|
145 | const bool wasVisible = pendingQueue.front().second; |
---|
146 | |
---|
147 | pendingQueue.pop(); |
---|
148 | |
---|
149 | queryQueue.push(QueryPair(node, mHierarchyInterface-> |
---|
150 | IssueNodeOcclusionQuery(node, wasVisible))); |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | void BatchedQueriesCullingManager::SetMaxPending(int maxPending) |
---|
155 | { |
---|
156 | mMaxPending = maxPending; |
---|
157 | } |
---|
158 | |
---|
159 | } // namespace GtpVisibility |
---|