1 | #include "RenderTraverser.h"
|
---|
2 | #include "glInterface.h"
|
---|
3 | #include "Camera.h"
|
---|
4 | #include "SceneEntity.h"
|
---|
5 | #include "RenderState.h"
|
---|
6 | #include "Geometry.h"
|
---|
7 | #include "Timer/PerfTimer.h"
|
---|
8 |
|
---|
9 |
|
---|
10 | using namespace std;
|
---|
11 |
|
---|
12 |
|
---|
13 | namespace CHCDemoEngine
|
---|
14 | {
|
---|
15 |
|
---|
16 |
|
---|
17 | void TraversalStatistics::Reset()
|
---|
18 | {
|
---|
19 | mNumTraversedNodes = 0;
|
---|
20 | mNumQueryCulledNodes = 0;
|
---|
21 | mNumFrustumCulledNodes = 0;
|
---|
22 |
|
---|
23 | mNumRenderedGeometry = 0;
|
---|
24 | mNumRenderedTriangles = 0;
|
---|
25 | mNumRenderedNodes = 0;
|
---|
26 |
|
---|
27 | mNumIssuedQueries = 0;
|
---|
28 | mNumStateChanges = 0;
|
---|
29 | mNumBatches = 0;
|
---|
30 |
|
---|
31 | mWaitTime = 0;
|
---|
32 | mQueryTime = 0;
|
---|
33 | mRestTime = 0;
|
---|
34 | }
|
---|
35 |
|
---|
36 |
|
---|
37 |
|
---|
38 | /******************************************************/
|
---|
39 | /* RenderTraverser implementation */
|
---|
40 | /******************************************************/
|
---|
41 |
|
---|
42 |
|
---|
43 | RenderTraverser::RenderTraverser():
|
---|
44 | mVisibilityThreshold(0),
|
---|
45 | mBvh(NULL),
|
---|
46 | mUseOptimization(false),
|
---|
47 | mFrameId(-1),
|
---|
48 | mUseRenderQueue(false),
|
---|
49 | mAssumedVisibleFrames(10),
|
---|
50 | mMaxBatchSize(50),
|
---|
51 | mUseTightBounds(false),
|
---|
52 | mShowBounds(false),
|
---|
53 | mRenderQueue(NULL),
|
---|
54 | mMaxVisibleDistance(.0f),
|
---|
55 | mRenderDynamicObjects(true)
|
---|
56 | {
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | RenderTraverser::~RenderTraverser()
|
---|
61 | {
|
---|
62 | mQueryHandler.DestroyQueries();
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | void RenderTraverser::EnqueueNode(BvhNode *node)
|
---|
67 | {
|
---|
68 | mBvh->UpdateDistance(node);
|
---|
69 | mDistanceQueue.push(node);
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | void RenderTraverser::TraverseNode(BvhNode *node)
|
---|
74 | {
|
---|
75 | ++ mStats.mNumTraversedNodes;
|
---|
76 |
|
---|
77 | if (node->IsVirtualLeaf())
|
---|
78 | {
|
---|
79 | RenderNode(node);
|
---|
80 |
|
---|
81 | if (mShowBounds)
|
---|
82 | mBvh->RenderBoundsForViz(node, mRenderState, mUseTightBounds);
|
---|
83 | }
|
---|
84 | else
|
---|
85 | {
|
---|
86 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
87 |
|
---|
88 | EnqueueNode(interior->GetFront());
|
---|
89 | EnqueueNode(interior->GetBack());
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | void RenderTraverser::RenderNode(BvhNode *node)
|
---|
95 | {
|
---|
96 | // test if node was already rendered in this frame
|
---|
97 | if (node->GetLastRenderedFrame() != mFrameId)
|
---|
98 | {
|
---|
99 | if (mRenderState->SetMode(RenderState::RENDER))
|
---|
100 | ++ mStats.mNumStateChanges;
|
---|
101 |
|
---|
102 | node->SetLastRenderedFrame(mFrameId);
|
---|
103 |
|
---|
104 | int geometrySize;
|
---|
105 | SceneEntity **entities = mBvh->GetGeometry(node, geometrySize);
|
---|
106 |
|
---|
107 | mStats.mNumRenderedGeometry += geometrySize;
|
---|
108 | ++ mStats.mNumRenderedNodes;
|
---|
109 |
|
---|
110 | for (int i = 0; i < geometrySize; ++ i)
|
---|
111 | {
|
---|
112 | SceneEntity *ent = entities[i];
|
---|
113 |
|
---|
114 | mStats.mNumRenderedTriangles += ent->CountNumTriangles();
|
---|
115 |
|
---|
116 | if (mUseRenderQueue)
|
---|
117 | mRenderQueue->Enqueue(ent);
|
---|
118 | else
|
---|
119 | ent->Render(mRenderState);
|
---|
120 |
|
---|
121 | // store the visible entities for rendering in the second pass
|
---|
122 | if (mUseDepthPass) mVisibleObjects.push_back(ent);
|
---|
123 | }
|
---|
124 |
|
---|
125 | // store the max distance in the scene for later use
|
---|
126 | float maxDist = mBvh->CalcMaxDistance(node);
|
---|
127 |
|
---|
128 | if (maxDist > mMaxVisibleDistance)
|
---|
129 | mMaxVisibleDistance = maxDist;
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | void RenderTraverser::SetHierarchy(Bvh *bvh)
|
---|
135 | {
|
---|
136 | mBvh = bvh;
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | void RenderTraverser::SetRenderState(RenderState *state)
|
---|
141 | {
|
---|
142 | mRenderState = state;
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | void RenderTraverser::RenderScene()
|
---|
147 | {
|
---|
148 | mVisibleObjects.clear();
|
---|
149 | mMaxVisibleDistance = .0f;
|
---|
150 |
|
---|
151 | ++ mFrameId;
|
---|
152 |
|
---|
153 | mBvh->InitFrame(mCamera, mRenderState);
|
---|
154 |
|
---|
155 | mStats.Reset();
|
---|
156 | mQueryHandler.ResetQueries();
|
---|
157 |
|
---|
158 | // add root node to queue
|
---|
159 | if (mRenderDynamicObjects)
|
---|
160 | EnqueueNode(mBvh->GetRoot());
|
---|
161 | else
|
---|
162 | EnqueueNode(mBvh->GetStaticRoot());
|
---|
163 |
|
---|
164 |
|
---|
165 |
|
---|
166 | ///////////
|
---|
167 | //-- the actual rendering algorithm
|
---|
168 |
|
---|
169 | Traverse();
|
---|
170 |
|
---|
171 | // render the contents of the render queue
|
---|
172 | if (mUseRenderQueue) ApplyRenderQueue();
|
---|
173 |
|
---|
174 | // reset the render state
|
---|
175 | mRenderState->Reset();
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | void RenderTraverser::SetUseRenderQueue(bool useRenderQueue)
|
---|
180 | {
|
---|
181 | mUseRenderQueue = useRenderQueue;
|
---|
182 | //std::cout << "using render queue: " << mUseRenderQueue << std::endl;
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 | void RenderTraverser::SetUseDepthPass(bool b)
|
---|
187 | {
|
---|
188 | mUseDepthPass = b;
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | void RenderTraverser::SetVisibilityThreshold(int threshold)
|
---|
193 | {
|
---|
194 | mVisibilityThreshold = threshold;
|
---|
195 | }
|
---|
196 |
|
---|
197 |
|
---|
198 | void RenderTraverser::SetUseOptimization(bool useOptimization)
|
---|
199 | {
|
---|
200 | mUseOptimization = useOptimization;
|
---|
201 | //cout << "using optimization: " << mUseOptimization << endl;
|
---|
202 | }
|
---|
203 |
|
---|
204 |
|
---|
205 | void RenderTraverser::SetAssumedVisibleFrames(int assumedVisibleFrames)
|
---|
206 | {
|
---|
207 | mAssumedVisibleFrames = assumedVisibleFrames;
|
---|
208 | }
|
---|
209 |
|
---|
210 |
|
---|
211 | void RenderTraverser::SetMaxBatchSize(int batchSize)
|
---|
212 | {
|
---|
213 | mMaxBatchSize = batchSize;
|
---|
214 | }
|
---|
215 |
|
---|
216 |
|
---|
217 | void RenderTraverser::SetUseMultiQueries(bool useMultiQueries)
|
---|
218 | {
|
---|
219 | mUseMultiQueries = useMultiQueries;
|
---|
220 | //cout << "using multiqueries: " << mUseMultiQueries << endl;
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 | void RenderTraverser::SetShowBounds(bool showBounds)
|
---|
225 | {
|
---|
226 | mShowBounds = showBounds;
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 | void RenderTraverser::SetUseTightBounds(bool useTightBounds)
|
---|
231 | {
|
---|
232 | mUseTightBounds = useTightBounds;
|
---|
233 | //cout << "using tight bounds: " << useTightBounds << endl;
|
---|
234 | }
|
---|
235 |
|
---|
236 |
|
---|
237 | OcclusionQuery *RenderTraverser::IssueOcclusionQuery(BvhNode *node)
|
---|
238 | {
|
---|
239 | OcclusionQuery *query = mQueryHandler.RequestQuery();
|
---|
240 | query->AddNode(node);
|
---|
241 |
|
---|
242 | IssueOcclusionQuery(*query);
|
---|
243 |
|
---|
244 | return query;
|
---|
245 | }
|
---|
246 |
|
---|
247 |
|
---|
248 | void RenderTraverser::IssueOcclusionQuery(const OcclusionQuery &query)
|
---|
249 | {
|
---|
250 | ++ mStats.mNumIssuedQueries;
|
---|
251 |
|
---|
252 | // render pending objects before changing to query mode
|
---|
253 | if (mUseRenderQueue && (mRenderState->GetMode() == RenderState::RENDER))
|
---|
254 | {
|
---|
255 | ApplyRenderQueue();
|
---|
256 | }
|
---|
257 |
|
---|
258 | query.BeginQuery();
|
---|
259 |
|
---|
260 | // change to query mode and render box
|
---|
261 | if (mRenderState->SetMode(RenderState::QUERY))
|
---|
262 | {
|
---|
263 | ++ mStats.mNumStateChanges;
|
---|
264 | }
|
---|
265 |
|
---|
266 | mBvh->RenderBounds(query.GetNodes(), mRenderState, mUseTightBounds);
|
---|
267 |
|
---|
268 | query.EndQuery();
|
---|
269 | }
|
---|
270 |
|
---|
271 |
|
---|
272 | void RenderTraverser::ApplyRenderQueue()
|
---|
273 | {
|
---|
274 | if (mRenderState->SetMode(RenderState::RENDER))
|
---|
275 | ++ mStats.mNumStateChanges;
|
---|
276 |
|
---|
277 | if (mRenderQueue->GetSize() > 0)
|
---|
278 | {
|
---|
279 | ++ mStats.mNumBatches;
|
---|
280 | mRenderQueue->Apply();
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 |
|
---|
285 | void RenderTraverser::SetRenderDynamicObjects(bool dynamic)
|
---|
286 | {
|
---|
287 | mRenderDynamicObjects = dynamic;
|
---|
288 | }
|
---|
289 |
|
---|
290 | }
|
---|