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 | mHierarchyInterface->PullUpVisibility(node); |
---|
45 | } |
---|
46 | else |
---|
47 | { |
---|
48 | mHierarchyInterface->SetNodeVisible(node, false); |
---|
49 | |
---|
50 | ++ mNumQueryCulledNodes; |
---|
51 | |
---|
52 | if (mVisualizeCulledNodes) |
---|
53 | { |
---|
54 | mHierarchyInterface->VisualizeCulledNode(node, QUERY_CULLED); |
---|
55 | } |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | //-- PART 2: hierarchical traversal |
---|
60 | if (!mHierarchyInterface->GetQueue()->empty()) |
---|
61 | { |
---|
62 | HierarchyNode *node = mHierarchyInterface->GetQueue()->top(); |
---|
63 | mHierarchyInterface->GetQueue()->pop(); |
---|
64 | |
---|
65 | bool intersects = false; |
---|
66 | |
---|
67 | if (!mHierarchyInterface->CheckFrustumVisible(node, intersects)) |
---|
68 | { |
---|
69 | ++ mNumFrustumCulledNodes; |
---|
70 | |
---|
71 | if (mVisualizeCulledNodes) |
---|
72 | { |
---|
73 | mHierarchyInterface->VisualizeCulledNode(node, FRUSTUM_CULLED); |
---|
74 | } |
---|
75 | } |
---|
76 | // -- if node intersects near plane, skip query because wrong results possible |
---|
77 | else if (intersects) |
---|
78 | { |
---|
79 | SkipQuery(node); |
---|
80 | } |
---|
81 | else |
---|
82 | { |
---|
83 | // identify previously visible nodes |
---|
84 | bool wasVisible = mHierarchyInterface->IsNodeVisible(node) && |
---|
85 | (mHierarchyInterface->LastVisited(node) == mHierarchyInterface->GetFrameId() - 1); |
---|
86 | |
---|
87 | // if we assume node to be visible in this frame => skip query |
---|
88 | bool skipQuery = wasVisible && (mAssumedVisibility > 0) && |
---|
89 | DecideVisible(node) && mHierarchyInterface->HasGeometry(node); |
---|
90 | |
---|
91 | //if (mHierarchyInterface->LastVisited(node) >= mHierarchyInterface->GetFrameId()) |
---|
92 | // Ogre::LogManager::getSingleton().logMessage("error"); |
---|
93 | |
---|
94 | if (skipQuery) |
---|
95 | { |
---|
96 | SkipQuery(node); |
---|
97 | |
---|
98 | continue; |
---|
99 | } |
---|
100 | |
---|
101 | // identify nodes that we cannot skip queries for |
---|
102 | // geometry not only in leaves => test for renderable geometry |
---|
103 | bool issueQuery = !wasVisible || mHierarchyInterface->HasGeometry(node); |
---|
104 | |
---|
105 | // reset node's visibility classification |
---|
106 | // set visibe if geometry in node so we only traverse once |
---|
107 | mHierarchyInterface->SetNodeVisible(node, wasVisible && issueQuery); |
---|
108 | |
---|
109 | // update node's visited flag |
---|
110 | mHierarchyInterface->SetLastVisited(node, mHierarchyInterface->GetFrameId()); |
---|
111 | |
---|
112 | // skip testing previously visible nodes without geometry |
---|
113 | if (issueQuery) |
---|
114 | { |
---|
115 | ++ mNumQueriesIssued; |
---|
116 | // add to the pending queue instead of immediate query |
---|
117 | if ((int)pendingQueue.size() < mMaxPending) |
---|
118 | pendingQueue.push(PendingQuery(node, wasVisible)); |
---|
119 | else |
---|
120 | { |
---|
121 | IssueMultipleQueries(pendingQueue, queryQueue); |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | // always traverse a node if it was visible |
---|
126 | if (wasVisible) |
---|
127 | { |
---|
128 | mHierarchyInterface->TraverseNode(node); |
---|
129 | } |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | // issue 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 |
---|