UnitInOutModule.h

00001 /***************************************************************************
00002  *   Copyright (c) 2008   Art Tevs                                         *
00003  *                                                                         *
00004  *   This library is free software; you can redistribute it and/or modify  *
00005  *   it under the terms of the GNU Lesser General Public License as        *
00006  *   published by the Free Software Foundation; either version 3 of        *
00007  *   the License, or (at your option) any later version.                   *
00008  *                                                                         *
00009  *   This library is distributed in the hope that it will be useful,       *
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00012  *   GNU Lesse General Public License for more details.                    *
00013  *                                                                         *
00014  *   The full license is in LICENSE file included with this distribution.  *
00015  ***************************************************************************/
00016 
00017 #ifndef _C_UNIT_INOUT_MODULE_H_
00018 #define _C_UNIT_INOUT_MODULE_H_
00019 
00020 
00021 //-------------------------------------------------------------------------
00022 // Includes
00023 //-------------------------------------------------------------------------
00024 #include <osgPPU/Export.h>
00025 #include <osgPPU/UnitInOut.h>
00026 #include <osgDB/DynamicLibrary>
00027 
00028 //! Name of the function which is the entry point of the module 
00029 #define OSGPPU_MODULE_ENTRY osgppuInitModule
00030 #define OSGPPU_MODULE_ENTRY_STR "osgppuInitModule"
00031 #define OSGPPU_MODULE_RELEASE osgppuReleaseModule
00032 #define OSGPPU_MODULE_RELEASE_STR "osgppuReleaseModule"
00033 
00034 namespace osgPPU
00035 {
00036     //! Apply some loaded module on the input texture to compute the output
00037     /**
00038     * UnitInOutModule does load a module from a dynamic library which is capable
00039     * of doing processing operations on the input data. Such a module could be for 
00040     * example a cuda processing module which process the input by cuda.
00041     **/
00042     class OSGPPU_EXPORT UnitInOutModule : public UnitInOut
00043     {
00044         public:
00045             META_Node(osgPPU, UnitInOutModule);
00046         
00047             UnitInOutModule();
00048             UnitInOutModule(const UnitInOutModule&, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
00049             
00050             //! Release it and used memory
00051             virtual ~UnitInOutModule();
00052 
00053             /**
00054             * Interface class of a module which can be used with this unit to process the input 
00055             * data.
00056             **/
00057                         class Module// : public osg::Referenced
00058             {
00059                 public:
00060                     //! Default constructor of the module
00061                     Module(UnitInOutModule* parent) : _parent(parent) {}
00062 
00063                     virtual ~Module(){}
00064 
00065                     /**
00066                     * Let the module initiaize. Overwrite the method with proper initialization routines.
00067                     * @return If false is given back the module will not be used and will be unloaded.
00068                     **/
00069                     virtual bool init() {return true;}
00070 
00071                     //! This method will be called whenever the rendering has began. (@see noticeBeginRendering())
00072                     virtual bool beginAndProcess() { return false; }
00073 
00074                     //! End rendering (@see noticeFinishRendering())
00075                     virtual void end() { }
00076 
00077                 protected:
00078                     UnitInOutModule* _parent;
00079 
00080             };
00081 
00082             typedef bool (*OSGPPU_MODULE_ENTRY)(UnitInOutModule*);
00083                         typedef void (*OSGPPU_MODULE_RELEASE)(void);
00084 
00085             /**
00086             * Specify the file name of a dynamic libray containg the module.
00087             * A method "osgppuInitModule" has to be present in the library.
00088             * To the method pointer of this unit will be passed to let it
00089             * setup himself properly.
00090             * @return true if loading was successful
00091             **/
00092             virtual bool loadModule(const std::string& moduleFile);
00093 
00094             /**
00095             * Set module which will be used to process the input data.
00096             **/
00097             virtual void setModule(Module* module);
00098             
00099             //! Remove currently used module.
00100             virtual void removeModule();
00101 
00102             //! Get last loaded module file name
00103             const std::string& getModuleFile() const { return _moduleFile; }
00104 
00105             //! Get currently loaded module
00106             inline Module* getModule() { return _module; }
00107             inline const Module* getModule() const { return _module; }
00108 
00109                         void init();
00110 
00111         protected:
00112 
00113             //! Start cuda kernel running over the input textures
00114             virtual bool noticeBeginRendering(osg::RenderInfo &, const osg::Drawable* );
00115 
00116             //! Stop cuda kernel execution and write results to the output textures
00117             virtual void noticeFinishRendering(osg::RenderInfo &, const osg::Drawable* );
00118 
00119             bool  _moduleDirty;
00120             osg::ref_ptr<osgDB::DynamicLibrary> _moduleLib;
00121             std::string _moduleFile;
00122 
00123                         //! We do not handle the destruction and release of module, because it has to be made in the address space of loaded module
00124                         //osg::ref_ptr<Module> _module;
00125                         Module* _module;
00126     };
00127 
00128 };
00129 
00130 #endif

Back to Homepage of osgPPU

Copyright (C) 2008 by Art Tevs (LGPL)