Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

NBFSaver.h

00001 // Copyright NVIDIA Corporation 2002-2004
00002 // TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
00003 // *AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS
00004 // OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
00005 // AND FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS
00006 // BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES
00007 // WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,
00008 // BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY LOSS)
00009 // ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF NVIDIA HAS
00010 // BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES 
00011 
00012 #pragma once
00013 
00014 #include  "nvsg/PlugInterface.h"
00015 #include  "nvtraverser/Traverser.h"
00016 
00017 #include <map>
00018 #include <vector>
00019 
00020 #if defined(_WIN32)
00021 # ifdef NBFSAVER_EXPORTS
00022 #  define NBFSAVER_API __declspec(dllexport)
00023 # else
00024 #  define NBFSAVER_API __declspec(dllimport)
00025 # endif
00026 #else
00027 # define NBFSAVER_API
00028 #endif
00029 
00030 // exports required for a scene loader plug-in
00031 #if defined(LINUX)
00032 extern "C"
00033 {
00034 #endif
00035   NBFSAVER_API bool getPlugInterface(const nvutil::UPIID& piid, nvutil::PlugIn *& pi);
00036   NBFSAVER_API bool queryPlugInterfaceType(const nvutil::UPITID& pitid, std::vector<nvutil::UPIID>& piids);
00037 #if defined(LINUX)
00038 }
00039 #endif
00040 
00041 
00043 class NBFSaveTraverser : public nvtraverser::Traverser
00044 {
00045   public:
00047     NBFSaveTraverser();
00048 
00050     void preCalculateFileSize(const nvsg::ViewState * viewState, const nvsg::Scene * scene);
00051 
00052     // file mapping/unmapping
00053     enum
00054     {
00055       // proposed view granularity
00056       VIEWSIZE = 0x04000000 // 64 MB (should be no problem to map the size)
00057     };
00058 
00060     bool mapViewOfFile(const std::string& file);
00061 
00063     void unmapViewOfFile();
00064 
00065   protected:
00066 
00068 
00075     bool calculatingStorageRequirements() const;
00076 
00078 
00087     uint_t alloc( 
00088       void*& ptr       
00089     , size_t numBytes  
00090     );
00091 
00093 
00100     uint_t pseudoAlloc(
00101       size_t numBytes  
00102     );
00103 
00105     virtual void doApply( 
00106       const nvsg::ViewState *viewState     
00107     , const nvsg::Scene *scene             
00108     );
00109 
00110     // overloads to process concrete types for saving
00111     // ...Cameras
00112     virtual void handleJitterCamera(const nvsg::JitterCamera *p, const nvsg::Node *root);
00113     virtual void handleParallelCamera(const nvsg::ParallelCamera *p, const nvsg::Node *root);
00114     virtual void handlePerspectiveCamera(const nvsg::PerspectiveCamera *p, const nvsg::Node *root);
00115     virtual void handleStereoCamera(const nvsg::StereoCamera *p, const nvsg::Node *root);
00116     // ...Nodes
00117     virtual void handleGeoNode(const nvsg::GeoNode *p);
00118     virtual void handleGroup(const nvsg::Group *p);
00119     virtual void handleTransform(const nvsg::Transform *p);
00120     virtual void handleAnimatedTransform(const nvsg::AnimatedTransform *p);
00121     virtual void handleLOD(const nvsg::LOD *p);
00122     virtual void handleSwitch(const nvsg::Switch *p);
00123     virtual void handleDirectedLight(const nvsg::DirectedLight *p);
00124     virtual void handlePointLight(const nvsg::PointLight *p);
00125     virtual void handleSpotLight(const nvsg::SpotLight *p);
00126     // ...StateSet
00127     virtual void handleStateSet(const nvsg::StateSet *p);
00128     // ...StateAttribs
00129     virtual void handleCgFx(const nvsg::CgFx *p);
00130     virtual void handleFaceAttribute(const nvsg::FaceAttribute *p);
00131     virtual void handleMaterial(const nvsg::Material *p);
00132     virtual void handleTextureAttribute(const nvsg::TextureAttribute *p);
00133     virtual void handleTextureAttributeItem(const nvsg::TextureAttributeItem * p, size_t tu);
00134     // ...GeoSets
00135     virtual void handleTriangles(const nvsg::Triangles *p);
00136     virtual void handleAnimatedTriangles(const nvsg::AnimatedTriangles *p);
00137     virtual void handleSkinnedTriangles(const nvsg::SkinnedTriangles *p);
00138     virtual void handleTriStrips(const nvsg::TriStrips *p);
00139     virtual void handleQuads(const nvsg::Quads *p);
00140     virtual void handleAnimatedQuads(const nvsg::AnimatedQuads *p);
00141     virtual void handleQuadStrips(const nvsg::QuadStrips *p);
00142     // ... Animations 
00143     virtual void handleFramedTrafoAnimation(const nvsg::FramedAnimation<nvmath::Trafo> *p);
00144     virtual void handleFramedVNVectorAnimation(const nvsg::FramedAnimation<nvsg::VNVector> *p);
00145     virtual void handleLinearInterpolatedTrafoAnimation(const nvsg::LinearInterpolatedAnimation<nvmath::Trafo> *p);
00146     virtual void handleLinearInterpolatedVNVectorAnimation(const nvsg::LinearInterpolatedAnimation<nvsg::VNVector> *p);
00147 
00148   private: // api covering implementation details
00149 
00150     // operator() to process a Scene (not part of the public interface!)
00151     // returns the file offset to the allocated and initialized NBFScene object
00152     // behavior is undefined if p is an invalid pointer
00153     uint_t handleScene(const nvsg::Scene *p);
00154     // operator() to process a ViewState (not part of the public interface!)
00155     // returns the file offset to the allocated and initialized NBFViewState object
00156     // behavior is undefined if p is an invalid pointer
00157     uint_t handleViewState(const nvsg::ViewState *p);
00158 
00159     // write routines for non-concrete types
00160     // ... write tasks similar for all Objects
00161     void writeObject(const nvsg::Object* nvsgObjPtr, NBFObject * nbfObjPtr, uint_t objCode);
00162     // ... write task similar for Nodes
00163     void writeNode(const nvsg::Node* nvsgNodePtr, NBFNode * nbfNodePtr, uint_t objCode);
00164     // ... write task similar for Groups
00165     void writeGroup(const nvsg::Group * nvsgGrpPtr, NBFGroup * nbfGrpPtr, uint_t objCode);
00166     // ... write task similar for LightSources
00167     void writeLightSource(const nvsg::LightSource* nvsgLightSrcPtr, NBFLightSource * nbfLightSrcPtr, uint_t objCode);
00168     // ... write tasks similar for all GeoSets
00169     void writeGeoSet(const nvsg::GeoSet* nvsgGeoSetPtr, NBFGeoSet * nbfGeoSetPtr, uint_t objCode); 
00170     // ... write task similar for SimpleCameras
00171     void writeSimpleCamera(const nvsg::SimpleCamera * nvsgCamPtr, NBFSimpleCamera * nbfCamPtr, uint_t objCode);
00172     // ... write strips
00173     template<typename StripType>
00174     void writeStrips(const StripType * nvsgStripsPtr, NBFStrips * nbfStripsPtr, uint_t objCode);
00175     // .. write faces
00176     template<typename NVSGFaceType, typename NBFFaceType>
00177     void writeFaces(const NVSGFaceType * nvsgFaces, NBFFaceType * nbfFaces, size_t sizeOfFace, uint_t objCode);
00178     
00179     // shared objects processing 
00180     bool processSharedObject(const nvsg::Object * obj, uint_t objCode);
00181     
00182     // map an additional view of the file into our address space, 
00183     // large enough to hold 'minimumNumberOfBytesToMap' bytes
00184     bool remapViewOfFile(size_t minimumNumberOfBytesToMap);
00185 
00186     // map cnt * sizeof(Type) bytes from the mapped file 
00187     template <typename Type>
00188     uint_t alloc(Type *& objPtr, size_t cnt=1); 
00189 
00190     // pseudo allocation routines for calculation of storage requirements
00191     void pseudoAllocObject(const nvsg::Object* p);
00192     void pseudoAllocNode(const nvsg::Node* p);
00193     void pseudoAllocGroup(const nvsg::Group * p);
00194     void pseudoAllocLightSource(const nvsg::LightSource * p);
00195     void pseudoAllocGeoSet(const nvsg::GeoSet* p); 
00196     void pseudoAllocSimpleCamera(const nvsg::SimpleCamera * p);
00197     template<typename StripType>
00198     void pseudoAllocStrips(const StripType * p);
00199 
00200   private:
00201 
00202     bool        m_calculateStorageRequirements; // pre-calculate what the file size would be
00203     size_t      m_preCalculatedFileSize; // required file size in bytes
00204 #if defined(_WIN32)
00205     HANDLE      m_file;       // file handle returned by CreateFile
00206     HANDLE      m_fileMapping; // handle of a file mapping object returned by CreateFileMapping
00207 #elif defined(LINUX)
00208     int         m_file;       // file descriptor returned by open()
00209 #endif
00210     struct Mapping
00211     {
00212       ubyte_t * basePtr;
00213       size_t    size;    // needed with linux's unmap call only  
00214     };
00215     std::vector<Mapping> m_mappedViews; // collection of currently mapped views
00216     size_t      m_viewSize;   // size in bytes of the currently mapped file view
00217     size_t      m_allocationGranularity; // actual system's allocation granularity
00218     ubyte_t   * m_basePtr;    // points to the beginning of the current view of the file mapping
00219     ubyte_t   * m_filePtr;    // actual write position
00220     size_t      m_fileOffset; // actual file offset
00221     std::map<const nvsg::Object*, uint_t> m_objectOffsetMap; // mapping NVSG objects to the corresponding offsets in file mapping
00222     std::map<nvsg::DataID, uint_t> m_objectDataIDOffsetMap; // mapping object IDs of shared objects to corresponding offsets
00223 };
00224 
00226 class NBFSaver : public nvsg::SceneSaver
00227 {
00228 public :
00230   NBFSaver();
00231 
00233 
00234   void deleteThis( void ); 
00235 
00237 
00238   bool save( const nvsg::Scene &scene          
00239            , const nvsg::ViewState *viewState  
00240            , const std::string& filename      
00241            );
00242 
00243 protected:
00244   virtual ~NBFSaver();
00245 
00246 protected:
00247   NBFSaveTraverser * m_traverser; 
00248 };
00249 
00250 
00251 inline void NBFSaver::deleteThis( void )
00252 {
00253   delete this;
00254 }

Generated on Tue Mar 1 13:19:18 2005 for NVSGSDK by NVIDIA