openscenegraph
Optimizer
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#ifndef OSGUTIL_OPTIMIZER
15#define OSGUTIL_OPTIMIZER
16
17#include <osg/NodeVisitor>
18#include <osg/Matrix>
19#include <osg/Geometry>
20#include <osg/Transform>
21#include <osg/Texture2D>
22
23#include <osgUtil/Export>
24
25#include <set>
26
27namespace osgUtil {
28
29// forward declare
30class Optimizer;
31
32/** Helper base class for implementing Optimizer techniques.*/
33class OSGUTIL_EXPORT BaseOptimizerVisitor : public osg::NodeVisitor
34{
35 public:
36
37 BaseOptimizerVisitor(Optimizer* optimizer, unsigned int operation):
38 osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
39 _optimizer(optimizer),
40 _operationType(operation)
41 {
42 setNodeMaskOverride(0xffffffff);
43 }
44
45 inline bool isOperationPermissibleForObject(const osg::StateSet* object) const;
46 inline bool isOperationPermissibleForObject(const osg::StateAttribute* object) const;
47 inline bool isOperationPermissibleForObject(const osg::Drawable* object) const;
48 inline bool isOperationPermissibleForObject(const osg::Node* object) const;
49
50 protected:
51
52 Optimizer* _optimizer;
53 unsigned int _operationType;
54};
55
56/** Traverses scene graph to improve efficiency. See OptimizationOptions.
57 * For example of usage see examples/osgimpostor or osgviewer.
58 */
59
60class OSGUTIL_EXPORT Optimizer
61{
62
63 public:
64
65 Optimizer() {}
66 virtual ~Optimizer() {}
67
68 enum OptimizationOptions
69 {
70 FLATTEN_STATIC_TRANSFORMS = (1 << 0),
71 REMOVE_REDUNDANT_NODES = (1 << 1),
72 REMOVE_LOADED_PROXY_NODES = (1 << 2),
73 COMBINE_ADJACENT_LODS = (1 << 3),
74 SHARE_DUPLICATE_STATE = (1 << 4),
75 MERGE_GEOMETRY = (1 << 5),
76 CHECK_GEOMETRY = (1 << 6), // deprecated, currently no-op
77 MAKE_FAST_GEOMETRY = (1 << 7),
78 SPATIALIZE_GROUPS = (1 << 8),
79 COPY_SHARED_NODES = (1 << 9),
80 TRISTRIP_GEOMETRY = (1 << 10),
81 TESSELLATE_GEOMETRY = (1 << 11),
82 OPTIMIZE_TEXTURE_SETTINGS = (1 << 12),
83 MERGE_GEODES = (1 << 13),
84 FLATTEN_BILLBOARDS = (1 << 14),
85 TEXTURE_ATLAS_BUILDER = (1 << 15),
86 STATIC_OBJECT_DETECTION = (1 << 16),
87 FLATTEN_STATIC_TRANSFORMS_DUPLICATING_SHARED_SUBGRAPHS = (1 << 17),
88 INDEX_MESH = (1 << 18),
89 VERTEX_POSTTRANSFORM = (1 << 19),
90 VERTEX_PRETRANSFORM = (1 << 20),
91 BUFFER_OBJECT_SETTINGS = (1 << 21),
92 DEFAULT_OPTIMIZATIONS = FLATTEN_STATIC_TRANSFORMS |
93 REMOVE_REDUNDANT_NODES |
94 REMOVE_LOADED_PROXY_NODES |
95 COMBINE_ADJACENT_LODS |
96 SHARE_DUPLICATE_STATE |
97 MERGE_GEOMETRY |
98 MAKE_FAST_GEOMETRY |
99 CHECK_GEOMETRY |
100 OPTIMIZE_TEXTURE_SETTINGS |
101 STATIC_OBJECT_DETECTION,
102 ALL_OPTIMIZATIONS = FLATTEN_STATIC_TRANSFORMS_DUPLICATING_SHARED_SUBGRAPHS |
103 REMOVE_REDUNDANT_NODES |
104 REMOVE_LOADED_PROXY_NODES |
105 COMBINE_ADJACENT_LODS |
106 SHARE_DUPLICATE_STATE |
107 MERGE_GEODES |
108 MERGE_GEOMETRY |
109 MAKE_FAST_GEOMETRY |
110 CHECK_GEOMETRY |
111 SPATIALIZE_GROUPS |
112 COPY_SHARED_NODES |
113 TRISTRIP_GEOMETRY |
114 OPTIMIZE_TEXTURE_SETTINGS |
115 TEXTURE_ATLAS_BUILDER |
116 STATIC_OBJECT_DETECTION |
117 BUFFER_OBJECT_SETTINGS
118 };
119
120 /** Reset internal data to initial state - the getPermissibleOptionsMap is cleared.*/
121 void reset();
122
123 /** Traverse the node and its subgraph with a series of optimization
124 * visitors, specified by the OptimizationOptions.*/
125 void optimize(osg::Node* node);
126
127 template<class T> void optimize(const osg::ref_ptr<T>& node) { optimize(node.get()); }
128
129 /** Traverse the node and its subgraph with a series of optimization
130 * visitors, specified by the OptimizationOptions.*/
131 virtual void optimize(osg::Node* node, unsigned int options);
132
133 template<class T> void optimize(const osg::ref_ptr<T>& node, unsigned int options) { optimize(node.get(), options); }
134
135
136 /** Callback for customizing what operations are permitted on objects in the scene graph.*/
137 struct IsOperationPermissibleForObjectCallback : public osg::Referenced
138 {
139 virtual bool isOperationPermissibleForObjectImplementation(const Optimizer* optimizer, const osg::StateSet* stateset,unsigned int option) const
140 {
141 return optimizer->isOperationPermissibleForObjectImplementation(stateset,option);
142 }
143
144 virtual bool isOperationPermissibleForObjectImplementation(const Optimizer* optimizer, const osg::StateAttribute* attribute,unsigned int option) const
145 {
146 return optimizer->isOperationPermissibleForObjectImplementation(attribute,option);
147 }
148
149 virtual bool isOperationPermissibleForObjectImplementation(const Optimizer* optimizer, const osg::Drawable* drawable,unsigned int option) const
150 {
151 return optimizer->isOperationPermissibleForObjectImplementation(drawable,option);
152 }
153
154 virtual bool isOperationPermissibleForObjectImplementation(const Optimizer* optimizer, const osg::Node* node,unsigned int option) const
155 {
156 return optimizer->isOperationPermissibleForObjectImplementation(node,option);
157 }
158
159 };
160
161 /** Set the callback for customizing what operations are permitted on objects in the scene graph.*/
162 void setIsOperationPermissibleForObjectCallback(IsOperationPermissibleForObjectCallback* callback) { _isOperationPermissibleForObjectCallback=callback; }
163
164 /** Get the callback for customizing what operations are permitted on objects in the scene graph.*/
165 IsOperationPermissibleForObjectCallback* getIsOperationPermissibleForObjectCallback() { return _isOperationPermissibleForObjectCallback.get(); }
166
167 /** Get the callback for customizing what operations are permitted on objects in the scene graph.*/
168 const IsOperationPermissibleForObjectCallback* getIsOperationPermissibleForObjectCallback() const { return _isOperationPermissibleForObjectCallback.get(); }
169
170
171 inline void setPermissibleOptimizationsForObject(const osg::Object* object, unsigned int options)
172 {
173 _permissibleOptimizationsMap[object] = options;
174 }
175
176 inline unsigned int getPermissibleOptimizationsForObject(const osg::Object* object) const
177 {
178 PermissibleOptimizationsMap::const_iterator itr = _permissibleOptimizationsMap.find(object);
179 if (itr!=_permissibleOptimizationsMap.end()) return itr->second;
180 else return 0xffffffff;
181 }
182
183
184 inline bool isOperationPermissibleForObject(const osg::StateSet* object, unsigned int option) const
185 {
186 if (_isOperationPermissibleForObjectCallback.valid())
187 return _isOperationPermissibleForObjectCallback->isOperationPermissibleForObjectImplementation(this,object,option);
188 else
189 return isOperationPermissibleForObjectImplementation(object,option);
190 }
191
192 inline bool isOperationPermissibleForObject(const osg::StateAttribute* object, unsigned int option) const
193 {
194 if (_isOperationPermissibleForObjectCallback.valid())
195 return _isOperationPermissibleForObjectCallback->isOperationPermissibleForObjectImplementation(this,object,option);
196 else
197 return isOperationPermissibleForObjectImplementation(object,option);
198 }
199
200 inline bool isOperationPermissibleForObject(const osg::Drawable* object, unsigned int option) const
201 {
202 if (_isOperationPermissibleForObjectCallback.valid())
203 return _isOperationPermissibleForObjectCallback->isOperationPermissibleForObjectImplementation(this,object,option);
204 else
205 return isOperationPermissibleForObjectImplementation(object,option);
206 }
207
208 inline bool isOperationPermissibleForObject(const osg::Node* object, unsigned int option) const
209 {
210 if (_isOperationPermissibleForObjectCallback.valid())
211 return _isOperationPermissibleForObjectCallback->isOperationPermissibleForObjectImplementation(this,object,option);
212 else
213 return isOperationPermissibleForObjectImplementation(object,option);
214 }
215
216 bool isOperationPermissibleForObjectImplementation(const osg::StateSet* stateset, unsigned int option) const
217 {
218 return (option & getPermissibleOptimizationsForObject(stateset))!=0;
219 }
220
221 bool isOperationPermissibleForObjectImplementation(const osg::StateAttribute* attribute, unsigned int option) const
222 {
223 return (option & getPermissibleOptimizationsForObject(attribute))!=0;
224 }
225
226 bool isOperationPermissibleForObjectImplementation(const osg::Drawable* drawable, unsigned int option) const
227 {
228 if (option & (REMOVE_REDUNDANT_NODES|MERGE_GEOMETRY))
229 {
230 if (drawable->getUserData()) return false;
231 if (drawable->getUpdateCallback()) return false;
232 if (drawable->getEventCallback()) return false;
233 if (drawable->getCullCallback()) return false;
234 }
235 return (option & getPermissibleOptimizationsForObject(drawable))!=0;
236 }
237
238 bool isOperationPermissibleForObjectImplementation(const osg::Node* node, unsigned int option) const
239 {
240 if (option & (REMOVE_REDUNDANT_NODES|COMBINE_ADJACENT_LODS|FLATTEN_STATIC_TRANSFORMS))
241 {
242 if (node->getUserData()) return false;
243 if (node->getUpdateCallback()) return false;
244 if (node->getEventCallback()) return false;
245 if (node->getCullCallback()) return false;
246 if (node->getNumDescriptions()>0) return false;
247 if (node->getStateSet()) return false;
248 if (node->getNodeMask()!=0xffffffff) return false;
249 // if (!node->getName().empty()) return false;
250 }
251
252 return (option & getPermissibleOptimizationsForObject(node))!=0;
253 }
254
255 protected:
256
257 osg::ref_ptr<IsOperationPermissibleForObjectCallback> _isOperationPermissibleForObjectCallback;
258
259 typedef std::map<const osg::Object*,unsigned int> PermissibleOptimizationsMap;
260 PermissibleOptimizationsMap _permissibleOptimizationsMap;
261
262 public:
263
264 /** Flatten Static Transform nodes by applying their transform to the
265 * geometry on the leaves of the scene graph, then removing the
266 * now redundant transforms. Static transformed subgraphs that have multiple
267 * parental paths above them are not flattened, if you require this then
268 * the subgraphs have to be duplicated - for this use the
269 * FlattenStaticTransformsDuplicatingSharedSubgraphsVisitor. */
270 class OSGUTIL_EXPORT FlattenStaticTransformsVisitor : public BaseOptimizerVisitor
271 {
272 public:
273
274 FlattenStaticTransformsVisitor(Optimizer* optimizer=0):
275 BaseOptimizerVisitor(optimizer, FLATTEN_STATIC_TRANSFORMS) {}
276
277 virtual void apply(osg::Node& geode);
278 virtual void apply(osg::Drawable& drawable);
279 virtual void apply(osg::Billboard& geode);
280 virtual void apply(osg::ProxyNode& node);
281 virtual void apply(osg::PagedLOD& node);
282 virtual void apply(osg::Transform& transform);
283
284 bool removeTransforms(osg::Node* nodeWeCannotRemove);
285
286 protected:
287
288 typedef std::vector<osg::Transform*> TransformStack;
289 typedef std::set<osg::Drawable*> DrawableSet;
290 typedef std::set<osg::Billboard*> BillboardSet;
291 typedef std::set<osg::Node* > NodeSet;
292 typedef std::set<osg::Transform*> TransformSet;
293
294 TransformStack _transformStack;
295 NodeSet _excludedNodeSet;
296 DrawableSet _drawableSet;
297 BillboardSet _billboardSet;
298 TransformSet _transformSet;
299 };
300
301 /** FlattenStaticTransformsDuplicatingSharedSubgraphsVisitor is similar
302 * to FlattenStaticTransformsVisitor in that it is designed to remove static transforms
303 * from the scene graph, pushing down the transforms to the geometry leaves of the scene graph,
304 * but with the difference that any subgraphs that are shared between different transforms
305 * are duplicated and flattened individually. This results in more static transforms
306 * being removed, but also means that more data is generated, and as a result may
307 * not always be the most appropriate flatten visitor to use.*/
308 class OSGUTIL_EXPORT FlattenStaticTransformsDuplicatingSharedSubgraphsVisitor : public BaseOptimizerVisitor
309 {
310 public:
311
312 FlattenStaticTransformsDuplicatingSharedSubgraphsVisitor(Optimizer* optimizer=0):
313 BaseOptimizerVisitor(optimizer, FLATTEN_STATIC_TRANSFORMS_DUPLICATING_SHARED_SUBGRAPHS) {}
314
315 virtual void reset();
316
317 virtual void apply(osg::Group& group);
318 virtual void apply(osg::Transform& transform);
319 virtual void apply(osg::LOD& lod);
320 virtual void apply(osg::Geode& geode);
321 virtual void apply(osg::Billboard& billboard);
322
323 protected:
324
325 void transformGeode(osg::Geode& geode);
326 void transformDrawable(osg::Drawable& drawable);
327 void transformBillboard(osg::Billboard& billboard);
328
329 std::vector<osg::Matrix> _matrixStack;
330
331 };
332
333 /** Combine Static Transform nodes that sit above one another.*/
334 class OSGUTIL_EXPORT CombineStaticTransformsVisitor : public BaseOptimizerVisitor
335 {
336 public:
337
338 CombineStaticTransformsVisitor(Optimizer* optimizer=0):
339 BaseOptimizerVisitor(optimizer, FLATTEN_STATIC_TRANSFORMS) {}
340
341 virtual void apply(osg::MatrixTransform& transform);
342
343 bool removeTransforms(osg::Node* nodeWeCannotRemove);
344
345 protected:
346
347 typedef std::set<osg::MatrixTransform*> TransformSet;
348 TransformSet _transformSet;
349 };
350
351 /** Remove rendundant nodes, such as groups with one single child.*/
352 class OSGUTIL_EXPORT RemoveEmptyNodesVisitor : public BaseOptimizerVisitor
353 {
354 public:
355
356
357 typedef std::set<osg::Node*> NodeList;
358 NodeList _redundantNodeList;
359
360 RemoveEmptyNodesVisitor(Optimizer* optimizer=0):
361 BaseOptimizerVisitor(optimizer, REMOVE_REDUNDANT_NODES) {}
362
363 virtual void apply(osg::Group& group);
364
365 void removeEmptyNodes();
366
367 };
368
369 /** Remove redundant nodes, such as groups with one single child.*/
370 class OSGUTIL_EXPORT RemoveRedundantNodesVisitor : public BaseOptimizerVisitor
371 {
372 public:
373
374 typedef std::set<osg::Node*> NodeList;
375 NodeList _redundantNodeList;
376
377 RemoveRedundantNodesVisitor(Optimizer* optimizer=0):
378 BaseOptimizerVisitor(optimizer, REMOVE_REDUNDANT_NODES) {}
379
380 virtual void apply(osg::Group& group);
381 virtual void apply(osg::Transform& transform);
382
383 bool isOperationPermissible(osg::Node& node);
384
385 void removeRedundantNodes();
386
387 };
388
389 /** Remove loaded proxy nodes.*/
390 class OSGUTIL_EXPORT RemoveLoadedProxyNodesVisitor : public BaseOptimizerVisitor
391 {
392 public:
393
394 typedef std::set<osg::Node*> NodeList;
395 NodeList _redundantNodeList;
396
397 RemoveLoadedProxyNodesVisitor(Optimizer* optimizer=0):
398 BaseOptimizerVisitor(optimizer, REMOVE_LOADED_PROXY_NODES) {}
399
400 virtual void apply(osg::ProxyNode& group);
401
402 void removeRedundantNodes();
403
404 };
405
406 /** Tessellate all Geometries, to remove POLYGONS.*/
407 class OSGUTIL_EXPORT TessellateVisitor : public BaseOptimizerVisitor
408 {
409 public:
410
411 typedef std::set<osg::Group*> GroupList;
412 GroupList _groupList;
413
414 TessellateVisitor(Optimizer* optimizer=0):
415 BaseOptimizerVisitor(optimizer, TESSELLATE_GEOMETRY) {}
416
417 virtual void apply(osg::Geometry& geom);
418
419 };
420
421 /** Optimize the LOD groups, by combining adjacent LOD's which have
422 * complementary ranges.*/
423 class OSGUTIL_EXPORT CombineLODsVisitor : public BaseOptimizerVisitor
424 {
425 public:
426
427 typedef std::set<osg::Group*> GroupList;
428 GroupList _groupList;
429
430 CombineLODsVisitor(Optimizer* optimizer=0):
431 BaseOptimizerVisitor(optimizer, COMBINE_ADJACENT_LODS) {}
432
433 virtual void apply(osg::LOD& lod);
434
435 void combineLODs();
436
437 };
438
439 /** Optimize State in the scene graph by removing duplicate state,
440 * replacing it with shared instances, both for StateAttributes,
441 * and whole StateSets.*/
442 class OSGUTIL_EXPORT StateVisitor : public BaseOptimizerVisitor
443 {
444 public:
445
446 /// default to traversing all children.
447 StateVisitor(bool combineDynamicState,
448 bool combineStaticState,
449 bool combineUnspecifiedState,
450 Optimizer* optimizer=0):
451 BaseOptimizerVisitor(optimizer, SHARE_DUPLICATE_STATE)
452 {
453 _optimize[osg::Object::DYNAMIC] = combineDynamicState;
454 _optimize[osg::Object::STATIC] = combineStaticState;
455 _optimize[osg::Object::UNSPECIFIED] = combineUnspecifiedState;
456 }
457
458 /** empty visitor, make it ready for next traversal.*/
459 virtual void reset();
460
461 virtual void apply(osg::Node& node);
462
463 void optimize();
464
465 protected:
466
467 void addStateSet(osg::StateSet* stateset, osg::Node* node);
468
469 inline bool optimize(osg::Object::DataVariance variance)
470 {
471 return _optimize[variance];
472 }
473
474 typedef std::set<osg::Node*> NodeSet;
475 typedef std::map<osg::StateSet*, NodeSet> StateSetMap;
476
477 // note, one element for DYNAMIC, STATIC and UNSPECIFIED
478 bool _optimize[3];
479
480 StateSetMap _statesets;
481
482 };
483
484 /** Combine geodes
485 */
486 class OSGUTIL_EXPORT MergeGeodesVisitor : public BaseOptimizerVisitor
487 {
488 public:
489
490 /// default to traversing all children.
491 MergeGeodesVisitor(Optimizer* optimizer=0):
492 BaseOptimizerVisitor(optimizer, MERGE_GEODES) {}
493
494 virtual void apply(osg::Group& group);
495
496 bool mergeGeodes(osg::Group& group);
497
498 protected:
499
500 bool mergeGeode(osg::Geode& lhs, osg::Geode& rhs);
501
502 };
503
504 class OSGUTIL_EXPORT MakeFastGeometryVisitor : public BaseOptimizerVisitor
505 {
506 public:
507
508 /// default to traversing all children.
509 MakeFastGeometryVisitor(Optimizer* optimizer=0):
510 BaseOptimizerVisitor(optimizer, MAKE_FAST_GEOMETRY) {}
511
512 virtual void apply(osg::Geometry& geom);
513
514 };
515
516 class OSGUTIL_EXPORT MergeGeometryVisitor : public BaseOptimizerVisitor
517 {
518 public:
519
520 /// default to traversing all children.
521 MergeGeometryVisitor(Optimizer* optimizer=0) :
522 BaseOptimizerVisitor(optimizer, MERGE_GEOMETRY),
523 _targetMaximumNumberOfVertices(10000) {}
524
525 void setTargetMaximumNumberOfVertices(unsigned int num)
526 {
527 _targetMaximumNumberOfVertices = num;
528 }
529
530 unsigned int getTargetMaximumNumberOfVertices() const
531 {
532 return _targetMaximumNumberOfVertices;
533 }
534
535 virtual void apply(osg::Group& group) { mergeGroup(group); traverse(group); }
536 virtual void apply(osg::Billboard&) { /* don't do anything*/ }
537
538 bool mergeGroup(osg::Group& group);
539
540 static bool geometryContainsSharedArrays(osg::Geometry& geom);
541
542 static bool mergeGeometry(osg::Geometry& lhs,osg::Geometry& rhs);
543
544 static bool mergePrimitive(osg::DrawArrays& lhs,osg::DrawArrays& rhs);
545 static bool mergePrimitive(osg::DrawArrayLengths& lhs,osg::DrawArrayLengths& rhs);
546 static bool mergePrimitive(osg::DrawElementsUByte& lhs,osg::DrawElementsUByte& rhs);
547 static bool mergePrimitive(osg::DrawElementsUShort& lhs,osg::DrawElementsUShort& rhs);
548 static bool mergePrimitive(osg::DrawElementsUInt& lhs,osg::DrawElementsUInt& rhs);
549
550 protected:
551
552 unsigned int _targetMaximumNumberOfVertices;
553
554 };
555
556 /** Spatialize scene into a balanced quad/oct tree.*/
557 class OSGUTIL_EXPORT SpatializeGroupsVisitor : public BaseOptimizerVisitor
558 {
559 public:
560
561 SpatializeGroupsVisitor(Optimizer* optimizer=0):
562 BaseOptimizerVisitor(optimizer, SPATIALIZE_GROUPS) {}
563
564 virtual void apply(osg::Group& group);
565 virtual void apply(osg::Geode& geode);
566
567 bool divide(unsigned int maxNumTreesPerCell=8);
568
569 bool divide(osg::Group* group, unsigned int maxNumTreesPerCell);
570 bool divide(osg::Geode* geode, unsigned int maxNumTreesPerCell);
571
572 typedef std::set<osg::Group*> GroupsToDivideList;
573 GroupsToDivideList _groupsToDivideList;
574
575 typedef std::set<osg::Geode*> GeodesToDivideList;
576 GeodesToDivideList _geodesToDivideList;
577 };
578
579 /** Copy any shared subgraphs, enabling flattening of static transforms.*/
580 class OSGUTIL_EXPORT CopySharedSubgraphsVisitor : public BaseOptimizerVisitor
581 {
582 public:
583
584 CopySharedSubgraphsVisitor(Optimizer* optimizer=0):
585 BaseOptimizerVisitor(optimizer, COPY_SHARED_NODES) {}
586
587 virtual void apply(osg::Node& node);
588
589 void copySharedNodes();
590
591 typedef std::set<osg::Node*> SharedNodeList;
592 SharedNodeList _sharedNodeList;
593
594 };
595
596
597 /** For all textures apply settings.*/
598 class OSGUTIL_EXPORT TextureVisitor : public BaseOptimizerVisitor
599 {
600 public:
601
602 TextureVisitor(bool changeAutoUnRef, bool valueAutoUnRef,
603 bool changeClientImageStorage, bool valueClientImageStorage,
604 bool changeAnisotropy, float valueAnisotropy,
605 Optimizer* optimizer=0):
606 BaseOptimizerVisitor(optimizer, OPTIMIZE_TEXTURE_SETTINGS),
607 _changeAutoUnRef(changeAutoUnRef), _valueAutoUnRef(valueAutoUnRef),
608 _changeClientImageStorage(changeClientImageStorage), _valueClientImageStorage(valueClientImageStorage),
609 _changeAnisotropy(changeAnisotropy), _valueAnisotropy(valueAnisotropy) {}
610
611 virtual void apply(osg::Node& node);
612
613 void apply(osg::StateSet& stateset);
614 void apply(osg::Texture& texture);
615
616 bool _changeAutoUnRef, _valueAutoUnRef;
617 bool _changeClientImageStorage, _valueClientImageStorage;
618 bool _changeAnisotropy;
619 float _valueAnisotropy;
620
621 };
622
623 /** Flatten MatrixTransform/Billboard pairs.*/
624 class OSGUTIL_EXPORT FlattenBillboardVisitor : public BaseOptimizerVisitor
625 {
626 public:
627 FlattenBillboardVisitor(Optimizer* optimizer=0):
628 BaseOptimizerVisitor(optimizer, FLATTEN_BILLBOARDS) {}
629
630 typedef std::vector<osg::NodePath> NodePathList;
631 typedef std::map<osg::Billboard*, NodePathList > BillboardNodePathMap;
632
633 virtual void reset();
634
635 virtual void apply(osg::Billboard& billboard);
636
637 void process();
638
639 BillboardNodePathMap _billboards;
640
641 };
642
643 /** Texture Atlas Builder creates a set of textures/images which each contain multiple images.
644 * Texture Atlas' are used to make it possible to use much wider batching of data. */
645 class OSGUTIL_EXPORT TextureAtlasBuilder
646 {
647 public:
648 TextureAtlasBuilder();
649
650 void reset();
651
652 void setMaximumAtlasSize(int width, int height);
653
654 int getMaximumAtlasWidth() const { return _maximumAtlasWidth; }
655 int getMaximumAtlasHeight() const { return _maximumAtlasHeight; }
656
657 void setMargin(int margin);
658 int getMargin() const { return _margin; }
659
660 void addSource(const osg::Image* image);
661 void addSource(const osg::Texture2D* texture);
662
663 unsigned int getNumSources() const { return _sourceList.size(); }
664 const osg::Image* getSourceImage(unsigned int i) { return _sourceList[i]->_image.get(); }
665 const osg::Texture2D* getSourceTexture(unsigned int i) { return _sourceList[i]->_texture.get(); }
666
667 void buildAtlas();
668 osg::Image* getImageAtlas(unsigned int i);
669 osg::Texture2D* getTextureAtlas(unsigned int i);
670 osg::Matrix getTextureMatrix(unsigned int i);
671
672 osg::Image* getImageAtlas(const osg::Image* image);
673 osg::Texture2D* getTextureAtlas(const osg::Image* image);
674 osg::Matrix getTextureMatrix(const osg::Image* image);
675
676 osg::Image* getImageAtlas(const osg::Texture2D* textue);
677 osg::Texture2D* getTextureAtlas(const osg::Texture2D* texture);
678 osg::Matrix getTextureMatrix(const osg::Texture2D* texture);
679
680 protected:
681
682 int _maximumAtlasWidth;
683 int _maximumAtlasHeight;
684 int _margin;
685
686
687 // forward declare
688 class Atlas;
689
690 class Source : public osg::Referenced
691 {
692 public:
693 Source():
694 _x(0),_y(0),_atlas(0) {}
695
696 Source(const osg::Image* image):
697 _x(0),_y(0),_atlas(0),_image(image) {}
698
699 Source(const osg::Texture2D* texture):
700 _x(0),_y(0),_atlas(0),_texture(texture) { if (texture) _image = texture->getImage(); }
701
702 int _x;
703 int _y;
704 Atlas* _atlas;
705
706 osg::ref_ptr<const osg::Image> _image;
707 osg::ref_ptr<const osg::Texture2D> _texture;
708
709 bool suitableForAtlas(int maximumAtlasWidth, int maximumAtlasHeight, int margin);
710 osg::Matrix computeTextureMatrix() const;
711
712
713 protected:
714
715 virtual ~Source() {}
716 };
717
718 typedef std::vector< osg::ref_ptr<Source> > SourceList;
719
720 class Atlas : public osg::Referenced
721 {
722 public:
723 Atlas(int width, int height, int margin):
724 _maximumAtlasWidth(width),
725 _maximumAtlasHeight(height),
726 _margin(margin),
727 _x(0),
728 _y(0),
729 _width(0),
730 _height(0),
731 _indexFirstOfRow(0){}
732
733 int _maximumAtlasWidth;
734 int _maximumAtlasHeight;
735 int _margin;
736
737 osg::ref_ptr<osg::Texture2D> _texture;
738 osg::ref_ptr<osg::Image> _image;
739
740 SourceList _sourceList;
741
742 int _x;
743 int _y;
744 int _width;
745 int _height;
746 unsigned int _indexFirstOfRow; ///< Contain the index of the first element of the last row.
747 enum FitsIn
748 {
749 DOES_NOT_FIT_IN_ANY_ROW,
750 FITS_IN_CURRENT_ROW,
751 IN_NEXT_ROW
752 };
753 FitsIn doesSourceFit(Source* source);
754 bool addSource(Source* source);
755 void clampToNearestPowerOfTwoSize();
756 void copySources();
757
758 protected:
759 virtual ~Atlas() {}
760 };
761
762 typedef std::vector< osg::ref_ptr<Atlas> > AtlasList;
763
764 Source* getSource(const osg::Image* image);
765 Source* getSource(const osg::Texture2D* texture);
766
767 SourceList _sourceList;
768 AtlasList _atlasList;
769 private:
770 struct CompareSrc
771 {
772 bool operator()(osg::ref_ptr<Source> src1, osg::ref_ptr<Source> src2) const
773 {
774 return src1->_image->t() > src2->_image->t();
775 }
776 };
777 void completeRow(unsigned int indexAtlas);
778 };
779
780
781 /** Optimize texture usage in the scene graph by combining textures into texture atlas
782 * Use of texture atlas cuts down on the number of separate states in the scene, reducing
783 * state changes and improving the chances of using larger batches of geometry.*/
784 class OSGUTIL_EXPORT TextureAtlasVisitor : public BaseOptimizerVisitor
785 {
786 public:
787
788 /// default to traversing all children.
789 TextureAtlasVisitor(Optimizer* optimizer=0):
790 BaseOptimizerVisitor(optimizer, TEXTURE_ATLAS_BUILDER) {}
791
792
793 TextureAtlasBuilder& getTextureAtlasBuilder() { return _builder; }
794
795 /** empty visitor, make it ready for next traversal.*/
796 virtual void reset();
797
798 virtual void apply(osg::Node& node);
799 virtual void apply(osg::Drawable& node);
800
801 void optimize();
802
803 protected:
804
805 bool pushStateSet(osg::StateSet* stateset);
806 void popStateSet();
807
808 typedef std::set<osg::Drawable*> Drawables;
809 typedef std::map<osg::StateSet*, Drawables> StateSetMap;
810 typedef std::set<osg::Texture2D*> Textures;
811 typedef std::vector<osg::StateSet*> StateSetStack;
812
813 TextureAtlasBuilder _builder;
814
815 StateSetMap _statesetMap;
816 StateSetStack _statesetStack;
817 Textures _textures;
818
819 };
820
821 /** Optimize the setting of StateSet and Geometry objects in scene so that they have a STATIC DataVariance
822 * when they don't have any callbacks associated with them. */
823 class OSGUTIL_EXPORT StaticObjectDetectionVisitor : public BaseOptimizerVisitor
824 {
825 public:
826
827 /// default to traversing all children.
828 StaticObjectDetectionVisitor(Optimizer* optimizer=0):
829 BaseOptimizerVisitor(optimizer, STATIC_OBJECT_DETECTION) {}
830
831 virtual void apply(osg::Node& node);
832 virtual void apply(osg::Drawable& drawable);
833
834 protected:
835
836 void applyStateSet(osg::StateSet& stateset);
837
838 };
839
840 /** For all geometry apply settings.*/
841 class OSGUTIL_EXPORT BufferObjectVisitor : public BaseOptimizerVisitor
842 {
843 public:
844
845 BufferObjectVisitor(bool changeVBO, bool valueVBO,
846 bool changeVertexArrayObject, bool valueVertexArrayObject,
847 bool changeDisplayList, bool valueDisplayList,
848 Optimizer* optimizer=0):
849 BaseOptimizerVisitor(optimizer, BUFFER_OBJECT_SETTINGS),
850 _changeVertexBufferObject(changeVBO), _valueVertexBufferObject(valueVBO),
851 _changeVertexArrayObject(changeVertexArrayObject), _valueVertexArrayObject(valueVertexArrayObject),
852 _changeDisplayList(changeDisplayList), _valueDisplayList(valueDisplayList) {}
853
854 virtual void apply(osg::Geometry& geometry);
855
856 bool _changeVertexBufferObject, _valueVertexBufferObject;
857 bool _changeVertexArrayObject, _valueVertexArrayObject;
858 bool _changeDisplayList, _valueDisplayList;
859
860 };
861};
862
863inline bool BaseOptimizerVisitor::isOperationPermissibleForObject(const osg::StateSet* object) const
864{
865 return _optimizer ? _optimizer->isOperationPermissibleForObject(object,_operationType) : true;
866}
867
868inline bool BaseOptimizerVisitor::isOperationPermissibleForObject(const osg::StateAttribute* object) const
869{
870 return _optimizer ? _optimizer->isOperationPermissibleForObject(object,_operationType) : true;
871}
872
873inline bool BaseOptimizerVisitor::isOperationPermissibleForObject(const osg::Drawable* object) const
874{
875 return _optimizer ? _optimizer->isOperationPermissibleForObject(object,_operationType) : true;
876}
877
878inline bool BaseOptimizerVisitor::isOperationPermissibleForObject(const osg::Node* object) const
879{
880 return _optimizer ? _optimizer->isOperationPermissibleForObject(object,_operationType) : true;
881}
882
883}
884
885#endif