1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
/*
* Copyright 2009, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// This file contains the definition of TreeTraveral.
#include "core/cross/tree_traversal.h"
#include "core/cross/shape.h"
#include "core/cross/draw_list.h"
#include "core/cross/transformation_context.h"
#include "core/cross/renderer.h"
#include "core/cross/error.h"
namespace o3d {
O3D_DEFN_CLASS(TreeTraversal, RenderNode);
const char* TreeTraversal::kTransformParamName =
O3D_STRING_CONSTANT("transform");
ObjectBase::Ref TreeTraversal::Create(ServiceLocator* service_locator) {
return ObjectBase::Ref(new TreeTraversal(service_locator));
}
TreeTraversal::TreeTraversal(ServiceLocator* service_locator)
: RenderNode(service_locator),
transformation_context_(service_locator->
GetService<TransformationContext>()) {
RegisterParamRef(kTransformParamName, &transform_param_);
}
void TreeTraversal::RegisterDrawList(
DrawList* draw_list,
DrawContext* draw_context,
bool reset) {
DCHECK(draw_list);
DCHECK(draw_context);
draw_list_draw_context_info_map_[DrawList::Ref(draw_list)] =
DrawContextInfo(draw_context, reset);
}
bool TreeTraversal::UnregisterDrawList(DrawList* draw_list) {
return draw_list_draw_context_info_map_.erase(DrawList::Ref(draw_list)) > 0;
}
void TreeTraversal::Render(RenderContext* render_context) {
// Reset the draw context infos array so we can rebuild it.
draw_context_infos_by_draw_list_global_index_.clear();
// Reset any DrawLists that need resetting and set the pass list flags.
DrawListDrawContextInfoMap::iterator end(
draw_list_draw_context_info_map_.end());
for (DrawListDrawContextInfoMap::iterator iter(
draw_list_draw_context_info_map_.begin());
iter != end;
++iter) {
DrawContextInfo& draw_context_info = iter->second;
draw_context_info.ResetCullDepth();
// Update the view projection matrix so we don't have to compute it more
// than once later.
draw_context_info.UpdateViewProjection();
// If we are supposed to reset the DrawList reset it.
DrawList* draw_list = iter->first;
if (draw_context_info.reset()) {
draw_list->Reset(draw_context_info.view(),
draw_context_info.projection());
}
// Add it to draw_context_infos_by_draw_list_global_index_ array.
unsigned global_index = draw_list->global_index();
if (global_index >= draw_context_infos_by_draw_list_global_index_.size()) {
draw_context_infos_by_draw_list_global_index_.insert(
draw_context_infos_by_draw_list_global_index_.end(),
global_index - draw_context_infos_by_draw_list_global_index_.size() + 1,
NULL);
}
draw_context_infos_by_draw_list_global_index_[global_index] =
&draw_context_info;
}
// At this point draw_context_infos_by_draw_list_global_index_ is big enough
// to so that there is a directly accessable entry for each DrawList that this
// TreeTraveral is filling out. In other words. If
// draw_context_infos_by_draw_list_global_index_[
// material->draw_list()->global_index()]
// is non NULL then we want the DrawElement using that material.
// Only after clearing any DrawLists do we bail out if there is no transform.
Transform* transform1 = transform();
if (!transform1) {
return;
}
// Return if this transform is not visible.
if (!transform1->visible()) {
return;
}
// Now walk ourselves and all our children.
WalkTransform(render_context,
transform1,
0,
static_cast<int>(draw_list_draw_context_info_map_.size()));
}
void TreeTraversal::SetStandardParameters(const Matrix4& world,
const Matrix4& world_view_projection,
DrawContextInfo* draw_context_info) {
transformation_context_->set_world(world);
transformation_context_->set_view(draw_context_info->view());
transformation_context_->set_projection(draw_context_info->projection());
transformation_context_->set_view_projection(
draw_context_info->view_projection());
transformation_context_->set_world_view_projection(world_view_projection);
}
void TreeTraversal::WalkTransform(RenderContext* render_context,
Transform* transform,
int depth,
int num_non_culled_draw_contexts) {
Renderer* renderer = render_context->renderer();
Matrix4 world = transform->world_matrix();
Matrix4 world_view_projection;
bool cull_depth_was_set = false;
renderer->IncrementTransformsProcessed();
// Attempt to cull this transform for each draw_context
DrawListDrawContextInfoMap::iterator end(
draw_list_draw_context_info_map_.end());
for (DrawListDrawContextInfoMap::iterator iter(
draw_list_draw_context_info_map_.begin());
iter != end;
++iter) {
DrawList* draw_list = iter->first;
bool have_world_view_projection = false;
DrawContextInfo* draw_context_info =
draw_context_infos_by_draw_list_global_index_[
draw_list->global_index()];
DLOG_ASSERT(draw_context_info);
// Before we cull, if the cull or bounding box params have input
// connections we need to setup the standard params.
if (transform->ParamsUsedByTreeTraversalHaveInputConnections()) {
Matrix4 world_view_projection =
draw_context_info->view_projection() * world;
have_world_view_projection = true;
SetStandardParameters(world, world_view_projection, draw_context_info);
}
if (transform->cull()) {
// Are we still processing this draw_context?
//
// The way this works is, draw_context_info->cull_depth is set to (-1) for
// each DrawContextInfo at the start. As we descend the tree, assuming
// transform->cull is true for a given transform, then if the cull_depth
// is still (-1) we need to check this transform to see if its boundingbox
// makes it culled. If that's true then we set the cull_depth to the depth
// in the tree at which it was culled. That means at any time
// draw_context_infos_by_draw_list_global_index[draw_list_global_index]->
// cull_depth() >= 0 means that draw list has already been completely
// culled.
//
// Each time we set a cull_depth we decrement
// num_non_culled_draw_contexts. If it goes to zero then there is no point
// going any deeper. The entire sub tree is culled for all draw_contexts.
//
// On the way back out IF the depth we are at matches a cull_depth then we
// can clear it back to -1. To help this process we keep a flag that
// whether or not we set 1 or more cull_depths in (cull_depth_was_set)
if (!draw_context_info->IsCulled()) {
if (!have_world_view_projection) {
world_view_projection = draw_context_info->view_projection() * world;
}
// NOTE: Computing the world view projection matrix this way means
// that no matter what, we only cull to that worldViewProjection.
// In other words the user can not supply is own funky
// worldViewProjection for culling using param binds where as he
// can supply one for rendering.
if (!transform->bounding_box().InFrustum(world_view_projection)) {
renderer->IncrementTransformsCulled();
--num_non_culled_draw_contexts;
draw_context_info->set_cull_depth(depth);
cull_depth_was_set = true;
}
}
}
}
if (num_non_culled_draw_contexts > 0) {
// Process Shapes
{
const ShapeRefArray& shapes = transform->GetShapeRefs();
ShapeRefArray::size_type size = shapes.size();
for (ShapeRefArray::size_type ii = 0; ii < size; ++ii) {
AddInstance(render_context, shapes[ii], transform, world);
}
}
// Process all the children
{
int children_depth = depth + 1;
const TransformRefArray& children = transform->GetChildrenRefs();
TransformRefArray::size_type size = children.size();
for (TransformRefArray::size_type ii = 0; ii < size; ++ii) {
if (children[ii]->visible()) {
WalkTransform(render_context,
children[ii],
children_depth,
num_non_culled_draw_contexts);
}
}
}
}
// Reset any cull_depths at our depth
if (cull_depth_was_set) {
DrawListDrawContextInfoMap::iterator end(
draw_list_draw_context_info_map_.end());
for (DrawListDrawContextInfoMap::iterator iter(
draw_list_draw_context_info_map_.begin());
iter != end;
++iter) {
DrawList* draw_list = iter->first;
DrawContextInfo* draw_context_info =
draw_context_infos_by_draw_list_global_index_[
draw_list->global_index()];
DLOG_ASSERT(draw_context_info);
if (draw_context_info->cull_depth() == depth) {
draw_context_info->ResetCullDepth();
++num_non_culled_draw_contexts;
}
}
}
}
void TreeTraversal::AddInstance(RenderContext* render_context,
Shape* shape,
Transform* override,
const Matrix4& world) {
Renderer* renderer = render_context->renderer();
const ElementRefArray& elements = shape->GetElementRefs();
ElementRefArray::size_type num_elements = elements.size();
for (ElementRefArray::size_type ii = 0; ii < num_elements; ++ii) {
Element* element = elements[ii];
const DrawElementRefArray& draw_elements =
element->GetDrawElementRefs();
DrawElementRefArray::const_iterator end(draw_elements.end());
for (DrawElementRefArray::const_iterator iter(draw_elements.begin());
iter != end;
++iter) {
renderer->IncrementDrawElementsProcessed();
DrawElement* draw_element = iter->Get();
// We MUST get the cache BEFORE culling so that we usually get the same
// cache for the same draw element.
ParamCache* param_cache =
override->param_cache_manager().GetNextCache(
render_context->renderer());
Material* material = draw_element->material();
if (!material) {
material = element->material();
}
if (!material) {
continue;
}
if (!material->effect()) {
continue;
}
DrawList* draw_list = material->draw_list();
if (!draw_list) {
continue;
}
// Is this pass list something we want?
if (draw_list->global_index() >=
draw_context_infos_by_draw_list_global_index_.size()) {
continue;
}
DrawContextInfo* draw_context_info =
draw_context_infos_by_draw_list_global_index_[
draw_list->global_index()];
if (draw_context_info && !draw_context_info->IsCulled()) {
Matrix4 world_view_projection(
draw_context_info->view_projection() * world);
// Yes it is. Should we attempt to cull it?
// Before we cull, if the cull or bounding box params have input
// connections we need to setup the standard params.
if (element->ParamsUsedByTreeTraversalHaveInputConnections()) {
SetStandardParameters(world,
world_view_projection,
draw_context_info);
}
if (element->cull()) {
// NOTE: Computing the world view projection matrix this way means
// that no matter what, we only cull to that worldViewProjection.
// In other words the user can not supply is own funky
// worldViewProjection for culling using param binds.
if (!element->bounding_box().InFrustum(world_view_projection)) {
renderer->IncrementDrawElementsCulled();
continue;
}
}
draw_list->AddDrawElement(draw_element,
element,
material,
override,
param_cache,
world,
world_view_projection);
}
}
}
}
void TreeTraversal::DrawContextInfo::UpdateViewProjection() {
view_ = draw_context_->view();
projection_ = draw_context_->projection();
view_projection_ = projection_ * view_;
}
} // namespace o3d
|