VR-Forces 4.3 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DtOsgFileCache.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 2013 MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 
9 
10 #pragma once
11 #include <vrvOsg/vrvOsg.h>
13 #include <vrvCore/DtFileCache.h>
14 #include <vrvUtil/signalslib.h>
15 #include <osgDB/ReaderWriter>
16 
17 #include <boost/unordered_map.hpp>
18 #include <boost/unordered_set.hpp>
19 #include <osgDB/ReaderWriter>
20 #define _WINSOCKAPI_
21 #include <boost/thread/mutex.hpp>
22 #include <boost/signals2.hpp>
23 
24 namespace osg
25 {
26  class Drawable;
27  class GraphicsContext;
28  class Node;
29  class Texture;
30 }
31 
32 namespace makVrv
33 {
34  class DtMeifConverter;
35  class DtMetaFileManager;
36 
42  {
43  public:
44 
47  friend class DtOsgFileCacheCreator;
48 
50  virtual ~DtOsgFileCache();
51 
53  virtual void releaseResources()
54  {
55  releaseResources(0);
56  }
57 
59  virtual void releaseResources(osg::State* state);
60 
62  virtual void setFromRecord( const makArchives::DtCacheSettingsRecord& rec );
63 
67  static void optimizeNodeGeometry(osg::Node* node);
68 
72  osg::Node* readNodeFile( const std::string& inputFilename,
73  const LoadFileOptions& loadFileOptions);
74 
77  inline osg::ref_ptr<osg::Node> findNodeFile( const std::string& inputFilename )
78  {
79  return ts_findNodeFile(inputFilename);
80  }
81 
85  osg::Image* readImageFile( const std::string& inputFilename,
86  const LoadFileOptions& loadFileOptions);
87 
90  virtual osg::Texture* getTextureInstance(const std::string& filePath,
91  osg::Texture::FilterMode minFilter, osg::Texture::FilterMode magFilter,
92  osg::Texture::WrapMode s, osg::Texture::WrapMode t,
93  const LoadFileOptions& loadFileOptions);
94 
96  virtual void encodeOptionsString(const LoadFileOptions& loadFileOptions,
97  osgDB::ReaderWriter::Options& options) const;
98 
100  virtual void decodeOptions(const osgDB::ReaderWriter::Options& options,
101  LoadFileOptions& loadFileOptions) const;
102 
104  DtMetaFileManager* metaFileManager();
105 
110  boost::signals2::signal<void (osg::Node*, const std::string& origFileName,
111  const std::string& actualFileName)> signal_nodeFileLoaded;
112 
117  boost::signals2::signal<void (osg::Image*, const std::string& origFileName,
118  const std::string& actualFileName)> signal_imageFileLoaded;
119 
123  boost::signals2::signal<void (const std::string& filename,
124  osgDB::ReaderWriter::ReadResult&,const osgDB::ReaderWriter::Options*)>
126 
130  boost::signals2::signal<void (const std::string& filename,
131  osgDB::ReaderWriter::ReadResult&,const osgDB::ReaderWriter::Options*)>
133 
134  class TextureCollection : public osg::Referenced
135  {
136  public:
137  union KeyType
138  {
139  unsigned short key;
140  struct
141  {
142  unsigned int min : 4;
143  unsigned int max : 4;
144  unsigned int wrap_s : 4;
145  unsigned int wrap_t : 4;
146  };
147  };
148 
149  // \Returns trush if the entire texture collection should be flushed.
150  bool flush()
151  {
152  TextureList replacementList;
153 
154  TextureList::iterator i = myTextures.begin();
155  TextureList::iterator e = myTextures.end();
156  for(;i != e;++i)
157  {
158  if(i->second->referenceCount() > 1)
159  {
160  replacementList.push_back(*i);
161  }
162  }
163  if(replacementList.size() != myTextures.size())
164  {
165  myTextures.swap(replacementList);
166  }
167  return myTextures.size() == 0;
168  }
169 
170  void releaseGLObjects()
171  {
172  TextureList::iterator i = myTextures.begin();
173  TextureList::iterator e = myTextures.end();
174  for(;i != e;++i)
175  {
176  i->second->releaseGLObjects();
177  }
178  }
179 
180  bool findTexture(unsigned int minFilter, unsigned int magFilter,
181  unsigned int wrapS, unsigned wrapT, osg::ref_ptr<osg::Texture>& texture)
182  {
183  KeyType k;
184  k.min = ((minFilter ^ 0x2600) & 0x3) + ((minFilter ^ 0x2600) >> 7);
185  k.max = ((magFilter ^ 0x2600) & 0x3) + ((magFilter ^ 0x2600) >> 7);
186  k.wrap_s = (wrapS & 0x0F) | ((wrapS & 0x10) >> 3);
187  k.wrap_t = (wrapT & 0x0F) | ((wrapT & 0x10) >> 3);
188 
189  TextureList::iterator i = myTextures.begin();
190  TextureList::iterator e = myTextures.end();
191  for(;i != e;++i)
192  {
193  if(i->first.key == k.key)
194  {
195  texture = i->second;
196  return true;
197  }
198  }
199  return false;
200  }
201 
202  void addTexture(osg::Texture* texture)
203  {
204  KeyTexturePair pair;
205  pair.first.min = ((texture->getFilter(osg::Texture::MIN_FILTER) ^ 0x2600) & 0x3) +
206  ((texture->getFilter(osg::Texture::MIN_FILTER) ^ 0x2600) >> 7);
207 
208  pair.first.max = ((texture->getFilter(osg::Texture::MAG_FILTER) ^ 0x2600) & 0x3) +
209  ((texture->getFilter(osg::Texture::MAG_FILTER) ^ 0x2600) >> 7);
210 
211  pair.first.wrap_s = (texture->getWrap(osg::Texture::WRAP_S) & 0x0F) |
212  ((texture->getWrap(osg::Texture::WRAP_S) & 0x10) >> 3);
213 
214  pair.first.wrap_t = (texture->getWrap(osg::Texture::WRAP_S) & 0x0F) |
215  ((texture->getWrap(osg::Texture::WRAP_S) & 0x10) >> 3);
216  pair.second = texture;
217  myTextures.push_back(pair);
218  }
219 
220  typedef std::pair<KeyType,osg::ref_ptr<osg::Texture> > KeyTexturePair;
221  typedef std::vector<KeyTexturePair> TextureList;
223  };
224 
225  protected:
226 
227  friend class DtReadFileCallback;
228 
230  virtual osg::ref_ptr<osg::Node> ts_readNodeFile( const std::string& inputFilename,
231  const LoadFileOptions& loadFileOptions);
232 
234  virtual osg::ref_ptr<osg::Node> ts_findNodeFile( const std::string& inputFilename );
235 
237  virtual osg::ref_ptr<osg::Image> ts_readImageFile( const std::string& inputFilename,
238  const LoadFileOptions& loadFileOptions);
239 
241  void ts_encryptImage( const std::string& inputFileName,
242  const std::string& outputFilename, bool printToConsole);
243 
245  bool ts_resolveRelativeFilename( const LoadFileOptions& loadFileOptions,
246  std::string& absoluteInputFilename );
247 
249  virtual void ts_clearInstancingCache();
250 
252  virtual void ts_flushInstancingCache( bool multipass,
253  bool flushNodes, bool flushTextures, bool flushImages);
254 
256  DtOsgFileCache(DtDe& de);
257 
261  std::string ts_buildAcceleratedFilename( const std::string& filePathIn,
262  const std::string& cachedDir,
263  const std::string& cachedExtension) const;
264 
267  virtual bool save(const std::string& origFilename,
268  const std::string& cachedFilename, bool optimize);
269 
271  bool shouldSaveFile( const std::string& filename);
272 
274  bool saveToAcceleratedFile( const std::string& origFileName,
275  osg::Node* nodeRef, const std::string& cachedFilename,
276  bool writeExternalReferences);
277 
279  virtual void addMetaFlightOptions(std::string& optionsString);
280 
282  virtual bool needsShaderGeneration(const std::string& filename, const LoadFileOptions& options) const;
283 
284  protected:
285 
286  typedef boost::unordered_map<std::string,std::string> RelativePathCache;
287 
291 
293  unsigned int myMaxFlushNodeCount;
294  unsigned int myMaxFlushImageCount;
295 
296  boost::mutex myRelativePathMutex;
300  };
301 
305  {
306 
307  public:
308 
310  virtual DtFileCache* create(DtDe& de);
311 
312  };
313 }
314 
315 // Include the inl to solve a circular dependency problem for gcc
316 #include <vrvOsg/DtOsgReferencedCollection.inl>
317 

Document ID: Generated on Wed Mar 11 21:20:57 EDT 2015 from SVN revision 150940
Copyright © 2005-2014 VT MÄK. All Rights Reserved (www.mak.com)