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 | bool isAvailable = false; |
---|
27 | //Ogre::LogManager::getSingleton().logMessage("Coherent Hierarchical Culling"); |
---|
28 | |
---|
29 | //-- PART 1: process finished occlusion queries |
---|
30 | while (!mHierarchyInterface->GetQueue()->empty() || !queryQueue.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 | |
---|
118 | queryQueue.push(QueryPair(node, mHierarchyInterface-> |
---|
119 | IssueNodeOcclusionQuery(node, wasVisible))); |
---|
120 | } |
---|
121 | |
---|
122 | // always traverse a node if it was visible |
---|
123 | if (wasVisible) |
---|
124 | { |
---|
125 | mHierarchyInterface->TraverseNode(node); |
---|
126 | } |
---|
127 | } |
---|
128 | } |
---|
129 | } |
---|
130 | } |
---|
131 | //----------------------------------------------------------------------- |
---|
132 | void CoherentHierarchicalCullingManager::SetAssumedVisibility(const unsigned int assumedVisibility) |
---|
133 | { |
---|
134 | mAssumedVisibility = assumedVisibility; |
---|
135 | |
---|
136 | mThreshold = 0; |
---|
137 | |
---|
138 | if (mAssumedVisibility > 0) |
---|
139 | { |
---|
140 | mThreshold = RAND_MAX - RAND_MAX / mAssumedVisibility; |
---|
141 | } |
---|
142 | //std::stringstream d; d << "*** setting assumed vis: " << mAssumedVisibility; Ogre::LogManager::getSingleton().logMessage(d.str()); |
---|
143 | } |
---|
144 | //----------------------------------------------------------------------- |
---|
145 | inline bool CoherentHierarchicalCullingManager::DecideVisible(HierarchyNode *node) const |
---|
146 | { |
---|
147 | //bool result = rand() < mThreshold; std::stringstream d; d << "Assumed vis: " << mAssumedVisibility << ", result: " << result; |
---|
148 | //Ogre::LogManager::getSingleton().logMessage(d.str()); return result; |
---|
149 | |
---|
150 | return rand() < mThreshold; |
---|
151 | } |
---|
152 | //----------------------------------------------------------------------- |
---|
153 | inline void CoherentHierarchicalCullingManager::SkipQuery(HierarchyNode *node) const |
---|
154 | { |
---|
155 | // -- set node to be visible in this frame, then traverse it |
---|
156 | mHierarchyInterface->SetLastVisited(node, mHierarchyInterface->GetFrameId()); |
---|
157 | |
---|
158 | mHierarchyInterface->PullUpVisibility(node); |
---|
159 | mHierarchyInterface->TraverseNode(node); |
---|
160 | } |
---|
161 | |
---|
162 | } // namespace GtpVisibility |
---|