VR-Forces 4.3.1 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  class DtOsgShaderManager;
37 
43  {
44  public:
45 
48  friend class DtOsgFileCacheCreator;
49 
51  virtual ~DtOsgFileCache();
52 
54  virtual void releaseResources()
55  {
56  releaseResources(0);
57  }
58 
60  virtual void releaseResources(osg::State* state);
61 
63  virtual void setFromRecord( const makArchives::DtCacheSettingsRecord& rec );
64 
68  static void optimizeNodeGeometry(osg::Node* node);
69 
73  osg::Node* readNodeFile( const std::string& inputFilename,
74  const LoadFileOptions& loadFileOptions);
75 
78  inline osg::ref_ptr<osg::Node> findNodeFile( const std::string& inputFilename )
79  {
80  return ts_findNodeFile(inputFilename);
81  }
82 
86  osg::Image* readImageFile( const std::string& inputFilename,
87  const LoadFileOptions& loadFileOptions);
88 
91  virtual osg::Texture* getTextureInstance(const std::string& filePath,
92  osg::Texture::FilterMode minFilter, osg::Texture::FilterMode magFilter,
93  osg::Texture::WrapMode s, osg::Texture::WrapMode t,
94  const LoadFileOptions& loadFileOptions);
95 
97  virtual void encodeOptionsString(const LoadFileOptions& loadFileOptions,
98  osgDB::ReaderWriter::Options& options) const;
99 
101  virtual void decodeOptions(const osgDB::ReaderWriter::Options& options,
102  LoadFileOptions& loadFileOptions) const;
103 
105  DtMetaFileManager* metaFileManager();
106 
111  boost::signals2::signal<void (osg::Node*, const std::string& origFileName,
112  const std::string& actualFileName)> signal_nodeFileLoaded;
113 
118  boost::signals2::signal<void (osg::Image*, const std::string& origFileName,
119  const std::string& actualFileName)> signal_imageFileLoaded;
120 
124  boost::signals2::signal<void (const std::string& filename,
125  osgDB::ReaderWriter::ReadResult&,const osgDB::ReaderWriter::Options*)>
127 
131  boost::signals2::signal<void (const std::string& filename,
132  osgDB::ReaderWriter::ReadResult&,const osgDB::ReaderWriter::Options*)>
134 
135  class TextureCollection : public osg::Referenced
136  {
137  public:
138  union KeyType
139  {
140  unsigned short key;
141  struct
142  {
143  unsigned int min : 4;
144  unsigned int max : 4;
145  unsigned int wrap_s : 4;
146  unsigned int wrap_t : 4;
147  };
148  };
149 
150  // \Returns trush if the entire texture collection should be flushed.
151  bool flush()
152  {
153  TextureList replacementList;
154 
155  TextureList::iterator i = myTextures.begin();
156  TextureList::iterator e = myTextures.end();
157  for(;i != e;++i)
158  {
159  if(i->second->referenceCount() > 1)
160  {
161  replacementList.push_back(*i);
162  }
163  }
164  if(replacementList.size() != myTextures.size())
165  {
166  myTextures.swap(replacementList);
167  }
168  return myTextures.size() == 0;
169  }
170 
171  void releaseGLObjects()
172  {
173  TextureList::iterator i = myTextures.begin();
174  TextureList::iterator e = myTextures.end();
175  for(;i != e;++i)
176  {
177  i->second->releaseGLObjects();
178  }
179  }
180 
181  bool findTexture(unsigned int minFilter, unsigned int magFilter,
182  unsigned int wrapS, unsigned wrapT, osg::ref_ptr<osg::Texture>& texture)
183  {
184  KeyType k;
185  k.min = ((minFilter ^ 0x2600) & 0x3) + ((minFilter ^ 0x2600) >> 7);
186  k.max = ((magFilter ^ 0x2600) & 0x3) + ((magFilter ^ 0x2600) >> 7);
187  k.wrap_s = (wrapS & 0x0F) | ((wrapS & 0x10) >> 3);
188  k.wrap_t = (wrapT & 0x0F) | ((wrapT & 0x10) >> 3);
189 
190  TextureList::iterator i = myTextures.begin();
191  TextureList::iterator e = myTextures.end();
192  for(;i != e;++i)
193  {
194  if(i->first.key == k.key)
195  {
196  texture = i->second;
197  return true;
198  }
199  }
200  return false;
201  }
202 
203  void addTexture(osg::Texture* texture)
204  {
205  KeyTexturePair pair;
206 
207  pair.first.min = ((texture->getFilter(osg::Texture::MIN_FILTER) ^ 0x2600) & 0x3) +
208  ((texture->getFilter(osg::Texture::MIN_FILTER) ^ 0x2600) >> 7);
209 
210  pair.first.max = ((texture->getFilter(osg::Texture::MAG_FILTER) ^ 0x2600) & 0x3) +
211  ((texture->getFilter(osg::Texture::MAG_FILTER) ^ 0x2600) >> 7);
212 
213  pair.first.wrap_s = (texture->getWrap(osg::Texture::WRAP_S) & 0x0F) |
214  ((texture->getWrap(osg::Texture::WRAP_S) & 0x10) >> 3);
215 
216  pair.first.wrap_t = (texture->getWrap(osg::Texture::WRAP_T) & 0x0F) |
217  ((texture->getWrap(osg::Texture::WRAP_T) & 0x10) >> 3);
218  pair.second = texture;
219  myTextures.push_back(pair);
220  }
221 
222  typedef std::pair<KeyType,osg::ref_ptr<osg::Texture> > KeyTexturePair;
223  typedef std::vector<KeyTexturePair> TextureList;
225  };
226 
227  protected:
228 
229  friend class DtReadFileCallback;
230 
232  virtual osg::ref_ptr<osg::Node> ts_readNodeFile( const std::string& inputFilename,
233  const LoadFileOptions& loadFileOptions);
234 
236  virtual osg::ref_ptr<osg::Node> ts_findNodeFile( const std::string& inputFilename );
237 
239  virtual osg::ref_ptr<osg::Image> ts_readImageFile( const std::string& inputFilename,
240  const LoadFileOptions& loadFileOptions);
241 
243  void ts_encryptImage( const std::string& inputFileName,
244  const std::string& outputFilename, bool printToConsole);
245 
247  bool ts_resolveRelativeFilename( const LoadFileOptions& loadFileOptions,
248  std::string& absoluteInputFilename );
249 
251  virtual void ts_clearInstancingCache();
252 
254  virtual void ts_flushInstancingCache( bool multipass,
255  bool flushNodes, bool flushTextures, bool flushImages);
256 
258  DtOsgFileCache(DtDe& de);
259 
263  std::string ts_buildAcceleratedFilename( const std::string& filePathIn,
264  const std::string& cachedDir,
265  const std::string& cachedExtension) const;
266 
269  virtual bool save(const std::string& origFilename,
270  const std::string& cachedFilename, bool optimize);
271 
273  bool shouldSaveFile( const std::string& filename);
274 
276  bool saveToAcceleratedFile( const std::string& origFileName,
277  osg::Node* nodeRef, const std::string& cachedFilename,
278  bool writeExternalReferences);
279 
281  virtual void addMetaFlightOptions(std::string& optionsString);
282 
284  virtual bool needsShaderGeneration(const std::string& filename, const LoadFileOptions& options) const;
285 
287  virtual void updateGlobalOptions();
288 
290  virtual void slot_onDdsFlipChanged(bool flip);
291 
293  virtual void slot_instancingChanged(bool enabled);
294 
295 
296  protected:
297 
298  typedef boost::unordered_map<std::string,std::string> RelativePathCache;
299 
303 
305  unsigned int myMaxFlushNodeCount;
306  unsigned int myMaxFlushImageCount;
307 
308  boost::mutex myRelativePathMutex;
312 
314  };
315 
319  {
320 
321  public:
322 
324  virtual DtFileCache* create(DtDe& de);
325 
326  };
327 }
328 
329 // Include the inl to solve a circular dependency problem for gcc
330 #include <vrvOsg/DtOsgReferencedCollection.inl>
331 

Document ID: Generated on Wed Jul 29 00:55:00 EDT 2015 from SVN revision 155257
Copyright © 2005-2015 VT MÄK. All Rights Reserved (www.mak.com)