UnitInOut.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_H_
00018 #define _C_UNIT_INOUT_H_
00019 
00020 
00021 //-------------------------------------------------------------------------
00022 // Includes
00023 //-------------------------------------------------------------------------
00024 #include <osgPPU/Export.h>
00025 #include <osgPPU/Unit.h>
00026 #include <osg/FrameBufferObject>
00027 
00028 #define OSGPPU_MIPMAP_LEVEL_UNIFORM "osgppu_MipmapLevel"
00029 #define OSGPPU_MIPMAP_LEVEL_NUM_UNIFORM "osgppu_MipmapLevelNum"
00030 #define OSGPPU_CUBEMAP_FACE_UNIFORM "osgppu_CubeMapFace"
00031 #define OSGPPU_3D_SLICE_NUMBER "osgppu_ZSliceNumber"
00032 #define OSGPPU_3D_SLICE_INDEX "osgppu_ZSliceIndex"
00033 
00034 namespace osgPPU
00035 {
00036     //! Compute output texture based on the assigned shaders and input data
00037     /**
00038     * InOut PPU, renders the content of input textures with applied shader 
00039     * to the output textures. Rendering is done in background, so no information
00040     * will leak to the frame buffer.
00041     **/
00042     class OSGPPU_EXPORT UnitInOut : public Unit {
00043         public:
00044             META_Node(osgPPU,UnitInOut);
00045         
00046             //! Mapping of MRT to ZSlice
00047             typedef std::map<unsigned int, unsigned int> OutputSliceMap;
00048 
00049             //! Create default ppfx 
00050             UnitInOut();
00051             UnitInOut(const UnitInOut&, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
00052             
00053             //! Release it and used memory
00054             virtual ~UnitInOut();
00055             
00056             //! Initialze the default Processor unit
00057             virtual void init();
00058             
00059             /**
00060             * Get framebuffer object used by this ppu. 
00061             **/
00062                         inline osg::FrameBufferObject* getFrameBufferObject() { return mFBO.get(); }
00063 
00064             /**
00065             * UnitInOut can also be used to bypass the input texture to the output
00066             * and perform rendering on it. This is different from the UnitBypass which
00067             * does not perform any rendering but bypasses the data. 
00068             * Specify here the index of the input unit,
00069             * to bypass the input to the output.
00070             * @param index Index of an input unit to bypass to output. Specify -1, to 
00071             * disable this feature.
00072             **/
00073             void setInputBypass(int index);
00074 
00075             /**
00076             * Get bypassed input texture index.
00077             **/
00078             inline int getInputBypass() const { return mBypassedInput; }
00079 
00080             /**
00081             * Set face index to use when rendering to a cubemap
00082             **/
00083             inline void setOutputFace(unsigned int face) { mOutputCubemapFace = face; dirty(); }
00084  
00085             /**
00086             * Get face index used when rendering to a cubemap
00087             **/
00088             inline unsigned int getOutputFace() const { return mOutputCubemapFace; }
00089 
00090             /**
00091             * Set slice index which is used to render the output to.
00092             * These settings have an effect only when using 3D textures as output.
00093             * The given slice will be defined for each MRT output.
00094             * @param slice Index of the slice (z-offset) to render the results to 
00095             * @param mrt MRT index of the output to be rendered to the given slice. This allows 
00096             * to specify which output to connect with which slice
00097             **/
00098             inline void setOutputZSlice(unsigned int slice, unsigned int mrt = 0) { mOutputZSlice[mrt] = slice; dirty(); }
00099 
00100             /**
00101             * Get Z slice which is used to render the output to if 3D
00102             * texture is used as output.
00103             **/
00104             inline unsigned int getOutputZSlice(unsigned int mrt) { return mOutputZSlice[mrt]; }
00105 
00106             /**
00107             * Get the mapping between MRT and Z-Offset slices. @see setOutputZSlice()
00108             **/
00109             inline const OutputSliceMap& getOutputZSliceMap() const { return mOutputZSlice; }
00110 
00111             /**
00112             * Specify the depth of the output texture when using a 3D or layered texture 
00113             * as output texture. Is a 2D texture is used as output, then this value specifies
00114             * the amount of MRT (multiple render targets) which has to be used by this unit.
00115             * 
00116             * NOTE: Currently there is no support to specify slices of different 3D or layered textures as MRTs
00117             **/
00118             void setOutputDepth(unsigned int depth);
00119 
00120             /**
00121             * Get depth of the output texture. The returned value is the value which 
00122             * you specify by the setOutputDepth() method and might be different to the 
00123             * real output texture depth. 
00124             **/
00125             inline unsigned int getOutputDepth() const { return mOutputDepth; }
00126 
00127             /**
00128             * Type of the texture. The types can be used to specify the type 
00129             * of the output texture of the UnitInOut units.
00130             **/
00131             enum TextureType
00132             {
00133                 //! Texture is a osg::Texture2D
00134                 TEXTURE_2D,
00135 
00136                 //! Texture is a osg::TextureCubeMap
00137                 TEXTURE_CUBEMAP,
00138 
00139                 //! 3D texture is used of the output
00140                 TEXTURE_3D,
00141 
00142                 //! Use 2D texture array
00143                 TEXTURE_2D_ARRAY,
00144 
00145                 //! Use a texture rectangle
00146                 TEXTURE_RECTANGLE
00147             };
00148 
00149             /**
00150             * Specify the type of the output texture. The next call on
00151             * getOrCreateOutputTexture() will generate the output texture of the given 
00152             * type if the output texture wasn't generated before. Use this
00153             * to implement units based not only on 2D textures.
00154             * 
00155             * NOTE: Since the output textures must be all of the same type, 
00156             * you have to take care about the correct output type. Default type is TEXTURE_2D.
00157             **/
00158             inline void setOutputTextureType(TextureType type) { mOutputType = type; dirty(); }
00159 
00160             /**
00161             * Get the type of the output texture. The type can be specified by 
00162             * the setOutputTextureType() method. The type must be equal for all output 
00163             * textures of the same unit.
00164             **/
00165             inline TextureType getOutputTextureType() const { return mOutputType; }
00166 
00167             /**
00168             * Set internal format which will be used by creating the textures. The format
00169             * specified here will be passed along to the osg::Texture::setInternalFormat()
00170             * method when creating output textures of a corresponding ppu.
00171             **/
00172             void setOutputInternalFormat(GLenum format);
00173     
00174             /**
00175             * Get internal format which is used by the output textures
00176             **/
00177             inline GLenum getOutputInternalFormat() const { return mOutputInternalFormat; }
00178 
00179             /**
00180             * Set an output texture.
00181             * @param outTex Texture used as output of this ppu 
00182             * @param mrt MRT (multiple rendering target) index of this output
00183             **/
00184             void setOutputTexture(osg::Texture* outTex, int mrt = 0);
00185 
00186             /**
00187             * Return output texture for the specified MRT index.
00188             * If no such exists, then it will be allocated.
00189             **/
00190             virtual osg::Texture* getOrCreateOutputTexture(int mrt = 0);
00191             
00192             /**
00193             * Set a MRT to texture map for output textures
00194             **/
00195             inline void setOutputTextureMap(const TextureMap& map) { mOutputTex = map; dirty();}
00196     
00197         protected:
00198 
00199             /**
00200             * Here the FBO will be applied, so that Unit can render its output to 
00201             * attached textures. If you overwrite this method in derived class,
00202             * so you have to take care about FBO handling.
00203             **/
00204             virtual bool  noticeBeginRendering (osg::RenderInfo&, const osg::Drawable* ) ;
00205 
00206             /**
00207             * Drawing is complete. So in our case, we will unbind used FBO and reset it to previous state
00208             * Derived methods has to take care about correct handling of FBOs!
00209             **/
00210             virtual void  noticeFinishRendering(osg::RenderInfo&, const osg::Drawable* );
00211         
00212             //! Viewport changed
00213             virtual void noticeChangeViewport();
00214     
00215             //! Reassign fbo if output textures changes
00216             virtual void assignOutputTexture();
00217 
00218             virtual void assignOutputPBO();
00219 
00220             //! Framebuffer object where results are written
00221                         osg::ref_ptr<osg::FrameBufferObject>    mFBO;    
00222 
00223             //! index of the bypassed input
00224             int mBypassedInput;
00225 
00226             //! Which face to render cubemaps to
00227             unsigned int mOutputCubemapFace;
00228 
00229             //! Slice index to which the output is rendered to
00230             OutputSliceMap mOutputZSlice;
00231 
00232             //! Depth of the 3D texture when used 
00233             unsigned int mOutputDepth;
00234 
00235             //! Output texture type
00236             TextureType mOutputType;
00237 
00238             //! Internal format of the output texture
00239             GLenum mOutputInternalFormat;
00240     };
00241 
00242 };
00243 
00244 #endif

Back to Homepage of osgPPU

Copyright (C) 2008 by Art Tevs (LGPL)