SharedStateManager 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #ifndef OSGDB_SHAREDSTATEMANAGER
  14. #define OSGDB_SHAREDSTATEMANAGER 1
  15. #include <osg/NodeVisitor>
  16. #include <osg/Geode>
  17. #include <osgDB/Export>
  18. #include <OpenThreads/Mutex>
  19. #include <set>
  20. namespace osgDB {
  21. class OSGDB_EXPORT SharedStateManager : public osg::NodeVisitor
  22. {
  23. public:
  24. enum ShareMode
  25. {
  26. SHARE_NONE = 0,
  27. SHARE_STATIC_TEXTURES = 1<<0,
  28. SHARE_UNSPECIFIED_TEXTURES = 1<<1,
  29. SHARE_DYNAMIC_TEXTURES = 1<<2,
  30. SHARE_STATIC_STATESETS = 1<<3,
  31. SHARE_UNSPECIFIED_STATESETS = 1<<4,
  32. SHARE_DYNAMIC_STATESETS = 1<<5,
  33. SHARE_TEXTURES = SHARE_STATIC_TEXTURES | SHARE_UNSPECIFIED_TEXTURES,
  34. SHARE_STATESETS = SHARE_STATIC_STATESETS | SHARE_UNSPECIFIED_STATESETS,
  35. SHARE_ALL = SHARE_TEXTURES |
  36. SHARE_STATESETS
  37. };
  38. SharedStateManager(unsigned int mode = SHARE_ALL);
  39. META_NodeVisitor(osgDB, SharedStateManager)
  40. void setShareMode(unsigned int mode);
  41. unsigned int getShareMode() { return _shareMode; }
  42. // Call right after each unload and before Registry cache prune.
  43. void prune();
  44. // Call right after each load
  45. void share(osg::Node *node, OpenThreads::Mutex *mt=0);
  46. void apply(osg::Node& node);
  47. // Answers the question "Will this state set be eliminated by
  48. // the SharedStateManager because an equivalent one has been
  49. // seen already?" Safe to call from the pager thread.
  50. bool isShared(osg::StateSet* stateSet);
  51. bool isShared(osg::Texture* texture);
  52. void releaseGLObjects(osg::State* state ) const;
  53. protected:
  54. inline bool shareTexture(osg::Object::DataVariance variance)
  55. {
  56. return _shareTexture[variance];
  57. }
  58. inline bool shareStateSet(osg::Object::DataVariance variance)
  59. {
  60. return _shareStateSet[variance];
  61. }
  62. void process(osg::StateSet* ss, osg::Object* parent);
  63. osg::StateAttribute *find(osg::StateAttribute *sa);
  64. osg::StateSet *find(osg::StateSet *ss);
  65. void setStateSet(osg::StateSet* ss, osg::Object* object);
  66. void shareTextures(osg::StateSet* ss);
  67. struct CompareStateAttributes
  68. {
  69. bool operator()(const osg::ref_ptr<osg::StateAttribute>& lhs,
  70. const osg::ref_ptr<osg::StateAttribute>& rhs) const
  71. {
  72. return *lhs < *rhs;
  73. }
  74. };
  75. struct CompareStateSets
  76. {
  77. bool operator()(const osg::ref_ptr<osg::StateSet>& lhs,
  78. const osg::ref_ptr<osg::StateSet>& rhs) const
  79. {
  80. return lhs->compare(*rhs, true) < 0;
  81. }
  82. };
  83. // Lists of shared objects
  84. typedef std::set< osg::ref_ptr<osg::StateAttribute>, CompareStateAttributes > TextureSet;
  85. TextureSet _sharedTextureList;
  86. typedef std::set< osg::ref_ptr<osg::StateSet>, CompareStateSets > StateSetSet;
  87. StateSetSet _sharedStateSetList;
  88. // Temporary lists just to avoid unnecessary find calls
  89. typedef std::pair<osg::StateAttribute*, bool> TextureSharePair;
  90. typedef std::map<osg::StateAttribute*, TextureSharePair> TextureTextureSharePairMap;
  91. TextureTextureSharePairMap tmpSharedTextureList;
  92. typedef std::pair<osg::StateSet*, bool> StateSetSharePair;
  93. typedef std::map<osg::StateSet*, StateSetSharePair> StateSetStateSetSharePairMap;
  94. StateSetStateSetSharePairMap tmpSharedStateSetList;
  95. unsigned int _shareMode;
  96. bool _shareTexture[3];
  97. bool _shareStateSet[3];
  98. // Share connection mutex
  99. OpenThreads::Mutex *_mutex;
  100. // Mutex for doing isShared queries from other threads
  101. mutable OpenThreads::Mutex _listMutex;
  102. };
  103. }
  104. #endif