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