Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

OgreGpuProgram.h

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://ogre.sourceforge.net/
00006 
00007 Copyright (c) 2000-2005 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 #ifndef __GpuProgram_H_
00026 #define __GpuProgram_H_
00027 
00028 // Precompiler options
00029 #include "OgrePrerequisites.h"
00030 #include "OgreResource.h"
00031 #include "OgreSharedPtr.h"
00032 #include "OgreIteratorWrappers.h"
00033 
00034 namespace Ogre {
00035 
00037     enum GpuProgramType
00038     {
00039         GPT_VERTEX_PROGRAM,
00040         GPT_FRAGMENT_PROGRAM
00041     };
00042 
00043     
00055     class _OgreExport GpuProgramParameters
00056     {
00057     public:
00061         enum AutoConstantType
00062         {
00064             ACT_WORLD_MATRIX,
00066             ACT_INVERSE_WORLD_MATRIX,
00070             ACT_TRANSPOSE_WORLD_MATRIX,
00072             ACT_INVERSE_TRANSPOSE_WORLD_MATRIX,
00073 
00074 
00076             ACT_WORLD_MATRIX_ARRAY_3x4,
00078             ACT_WORLD_MATRIX_ARRAY,
00079 
00080 
00082             ACT_VIEW_MATRIX,
00084             ACT_INVERSE_VIEW_MATRIX,
00088             ACT_TRANSPOSE_VIEW_MATRIX,
00092             ACT_INVERSE_TRANSPOSE_VIEW_MATRIX,
00093 
00094 
00096             ACT_PROJECTION_MATRIX,
00100             ACT_INVERSE_PROJECTION_MATRIX,
00104             ACT_TRANSPOSE_PROJECTION_MATRIX,
00108             ACT_INVERSE_TRANSPOSE_PROJECTION_MATRIX,
00109 
00110 
00112             ACT_VIEWPROJ_MATRIX,
00116             ACT_INVERSE_VIEWPROJ_MATRIX,
00120             ACT_TRANSPOSE_VIEWPROJ_MATRIX,
00124             ACT_INVERSE_TRANSPOSE_VIEWPROJ_MATRIX,
00125 
00126 
00128             ACT_WORLDVIEW_MATRIX,
00130             ACT_INVERSE_WORLDVIEW_MATRIX,
00134             ACT_TRANSPOSE_WORLDVIEW_MATRIX,
00136             ACT_INVERSE_TRANSPOSE_WORLDVIEW_MATRIX,
00138 
00139 
00141             ACT_WORLDVIEWPROJ_MATRIX,
00145             ACT_INVERSE_WORLDVIEWPROJ_MATRIX,
00149             ACT_TRANSPOSE_WORLDVIEWPROJ_MATRIX,
00153             ACT_INVERSE_TRANSPOSE_WORLDVIEWPROJ_MATRIX,
00154 
00155 
00157 
00160             ACT_RENDER_TARGET_FLIPPING,
00161 
00162 
00164             ACT_FOG_COLOUR,
00166             ACT_FOG_PARAMS,
00167 
00168 
00170             ACT_AMBIENT_LIGHT_COLOUR, 
00171 
00173             ACT_LIGHT_DIFFUSE_COLOUR,
00175             ACT_LIGHT_SPECULAR_COLOUR,
00177             ACT_LIGHT_ATTENUATION,
00179             ACT_LIGHT_POSITION,
00181             ACT_LIGHT_POSITION_OBJECT_SPACE,
00183             ACT_LIGHT_POSITION_VIEW_SPACE,
00185             ACT_LIGHT_DIRECTION,
00187             ACT_LIGHT_DIRECTION_OBJECT_SPACE,
00189             ACT_LIGHT_DIRECTION_VIEW_SPACE,
00194             ACT_LIGHT_DISTANCE_OBJECT_SPACE,
00196             ACT_LIGHT_POWER_SCALE,
00200             ACT_SHADOW_EXTRUSION_DISTANCE,
00202             ACT_CAMERA_POSITION,
00204             ACT_CAMERA_POSITION_OBJECT_SPACE,
00206             ACT_TEXTURE_VIEWPROJ_MATRIX,
00208             ACT_CUSTOM,
00211             ACT_TIME,
00215             ACT_TIME_0_X,
00217             ACT_COSTIME_0_X,
00219             ACT_SINTIME_0_X,
00221             ACT_TANTIME_0_X,
00225             ACT_TIME_0_X_PACKED,
00230             ACT_TIME_0_1,
00232             ACT_COSTIME_0_1,
00234             ACT_SINTIME_0_1,
00236             ACT_TANTIME_0_1,
00240             ACT_TIME_0_1_PACKED,
00245             ACT_TIME_0_2PI,
00247             ACT_COSTIME_0_2PI,
00249             ACT_SINTIME_0_2PI,
00251             ACT_TANTIME_0_2PI,
00255             ACT_TIME_0_2PI_PACKED,
00257             ACT_FRAME_TIME,
00259             ACT_FPS,
00261 
00264             ACT_VIEWPORT_WIDTH,
00268             ACT_VIEWPORT_HEIGHT,
00272             ACT_INVERSE_VIEWPORT_WIDTH,
00276             ACT_INVERSE_VIEWPORT_HEIGHT,
00280             ACT_VIEWPORT_SIZE,
00281 
00283 
00286             ACT_VIEW_DIRECTION,
00290             ACT_VIEW_SIDE_VECTOR,
00294             ACT_VIEW_UP_VECTOR,
00298             ACT_FOV,
00302             ACT_NEAR_CLIP_DISTANCE,
00306             ACT_FAR_CLIP_DISTANCE,
00307 
00311             ACT_PASS_NUMBER,
00312 
00317             ACT_PASS_ITERATION_NUMBER,
00318 
00319 
00323             ACT_ANIMATION_PARAMETRIC,
00324 
00325         };
00326 
00330         enum ACDataType {
00332             ACDT_NONE,
00334             ACDT_INT,
00336             ACDT_REAL
00337         };
00338 
00341         enum ElementType {
00342             ET_INT,
00343             ET_REAL
00344         };
00345 
00346         struct AutoConstantDefinition
00347         {
00348             AutoConstantType acType;
00349             String name;
00350             size_t elementCount;
00352             ElementType elementType;
00354             ACDataType dataType;
00355 
00356             AutoConstantDefinition(AutoConstantType _acType, const String& _name, 
00357                 size_t _elementCount, ElementType _elementType, 
00358                 ACDataType _dataType)
00359                 :acType(_acType), name(_name), elementCount(_elementCount), 
00360                 elementType(_elementType), dataType(_dataType)
00361             {
00362                 
00363             }
00364         };
00365 
00367         class _OgrePrivate AutoConstantEntry
00368         {
00369         public:
00371             AutoConstantType paramType;
00373             size_t index;
00375             union{
00376                 size_t data;
00377                 Real fData;
00378             };
00379 
00380             AutoConstantEntry(AutoConstantType theType, size_t theIndex, size_t theData)
00381                 : paramType(theType), index(theIndex), data(theData) {}
00382 
00383             AutoConstantEntry(AutoConstantType theType, size_t theIndex, Real theData)
00384                 : paramType(theType), index(theIndex), fData(theData) {}
00385 
00386         };
00391         struct RealConstantEntry
00392         {
00393             float val[4];
00394             bool isSet;
00395             RealConstantEntry() : isSet(false)  {}
00396         };
00401         struct IntConstantEntry
00402         {
00403             int val[4];
00404             bool isSet;
00405             IntConstantEntry() : isSet(false) {}
00406         };
00407 
00408         // nfz
00413         struct ConstantDefinition
00414         {
00415             String name;
00416             size_t entryIndex;
00417             size_t elementCount;
00418             size_t arraySize;
00419             ElementType elementType;
00420             size_t autoIndex;
00421             bool   isAllocated;
00422             bool   isAuto;
00423 
00424             ConstantDefinition()
00425                 : entryIndex(0)
00426                 , elementCount(0)
00427                 , arraySize(1)
00428                 , elementType(ET_INT)
00429                 , autoIndex(0)
00430                 , isAllocated(false)
00431                 , isAuto(false)
00432             {}
00433 
00434         };
00435 
00436     protected:
00437         static AutoConstantDefinition AutoConstantDictionary[];
00438         // Constant lists
00439         typedef std::vector<RealConstantEntry> RealConstantList;
00440         typedef std::vector<IntConstantEntry> IntConstantList;
00441         // Auto parameter storage
00442         typedef std::vector<AutoConstantEntry> AutoConstantList;
00443         // parameter dictionary container
00444         typedef std::vector<ConstantDefinition> ConstantDefinitionContainer;
00446         RealConstantList mRealConstants;
00448         IntConstantList mIntConstants;
00450         AutoConstantList mAutoConstants;
00452         ConstantDefinitionContainer mConstantDefinitions;
00454         typedef std::map<String, size_t> ParamNameMap;
00455         ParamNameMap mParamNameMap;
00457         bool mTransposeMatrices;
00459         bool mAutoAddParamName;
00461         RealConstantEntry* mActivePassIterationEntry;
00463         size_t mActivePassIterationEntryIndex;
00464 
00465 
00466     public:
00467         GpuProgramParameters();
00468         ~GpuProgramParameters() {}
00469 
00471         GpuProgramParameters(const GpuProgramParameters& oth);
00473         GpuProgramParameters& operator=(const GpuProgramParameters& oth);
00474 
00475 
00481         void setConstant(size_t index, const Vector4& vec);
00489         void setConstant(size_t index, Real val);
00497         void setConstant(size_t index, const Vector3& vec);
00504         void setConstant(size_t index, const Matrix4& m);
00512         void setConstant(size_t index, const Matrix4* m, size_t numEntries);
00519         void setConstant(size_t index, const float *val, size_t count);
00526         void setConstant(size_t index, const double *val, size_t count);
00532         void setConstant(size_t index, const ColourValue& colour);
00533         
00548         void setConstant(size_t index, const int *val, size_t count);
00549 
00551         void resetRealConstants(void) { mRealConstants.clear(); }
00553         void resetIntConstants(void) { mIntConstants.clear(); }
00554 
00555         typedef ConstVectorIterator<RealConstantList> RealConstantIterator;
00556         typedef ConstVectorIterator<IntConstantList> IntConstantIterator;
00558         RealConstantIterator getRealConstantIterator(void) const;
00560         IntConstantIterator getIntConstantIterator(void) const;
00561 
00566         RealConstantEntry* getRealConstantEntry(const size_t index);
00571         IntConstantEntry* getIntConstantEntry(const size_t index);
00572         
00576         RealConstantEntry* getNamedRealConstantEntry(const String& name);
00580         IntConstantEntry* getNamedIntConstantEntry(const String& name);
00582         size_t getRealConstantCount(void) const { return mRealConstants.size(); }
00584         size_t getIntConstantCount(void) const { return mIntConstants.size(); }
00586         bool hasRealConstantParams(void) const { return !(mRealConstants.empty()); }
00588         bool hasIntConstantParams(void) const { return !(mIntConstants.empty()); }
00589 
00603         void setAutoConstant(size_t index, AutoConstantType acType, size_t extraInfo = 0);
00604         void setAutoConstantReal(size_t index, AutoConstantType acType, Real rData);
00609         void setConstantFromTime(size_t index, Real factor);
00610 
00612         void clearAutoConstants(void);
00613         typedef ConstVectorIterator<AutoConstantList> AutoConstantIterator;
00615         AutoConstantIterator getAutoConstantIterator(void) const;
00617         size_t getAutoConstantCount(void) const { return mAutoConstants.size(); }
00622         AutoConstantEntry* getAutoConstantEntry(const size_t index);
00624         bool hasAutoConstants(void) const { return !(mAutoConstants.empty()); }
00626         void _updateAutoParamsNoLights(const AutoParamDataSource& source);
00628         void _updateAutoParamsLightsOnly(const AutoParamDataSource& source);
00629 
00641         void setAutoAddParamName(bool state) { mAutoAddParamName = state; }
00642 
00662         void setNamedConstant(const String& name, Real val);
00682         void setNamedConstant(const String& name, int val);
00687         void setNamedConstant(const String& name, const Vector4& vec);
00700         void setNamedConstant(const String& name, const Vector3& vec);
00705         void setNamedConstant(const String& name, const Matrix4& m);
00713         void setNamedConstant(const String& name, const Matrix4* m, size_t numEntries);
00734         void setNamedConstant(const String& name, const float *val, size_t count);
00755         void setNamedConstant(const String& name, const double *val, size_t count);
00760         void setNamedConstant(const String& name, const ColourValue& colour);
00761         
00782         void setNamedConstant(const String& name, const int *val, size_t count);
00783 
00798         void setNamedAutoConstant(const String& name, AutoConstantType acType, size_t extraInfo = 0);
00799 
00807         void setNamedConstantFromTime(const String& name, Real factor);
00809         void _mapParameterNameToIndex(const String& name, const size_t index );
00810 
00812         size_t getParamIndex(const String& name);
00813 
00814 
00822         void setTransposeMatrices(bool val) { mTransposeMatrices = val; } 
00824         bool getTransposeMatrices(void) const { return mTransposeMatrices; } 
00825 
00829         void copyConstantsFrom(const GpuProgramParameters& source);
00830 
00843         size_t addConstantDefinition(const String& name, const size_t index, 
00844             const size_t elementCount, const ElementType elementType);
00845 
00849         const ConstantDefinition* getConstantDefinition(const String& name) const;
00854         const ConstantDefinition* getConstantDefinition(const size_t idx) const;
00858         const ConstantDefinition* findMatchingConstantDefinition(const String& name, 
00859             const size_t entryIndex, const ElementType elementType) const;
00860 
00863         size_t getNumConstantDefinitions(void) const { return mConstantDefinitions.size(); }
00869         void setConstantDefinitionAutoState( const size_t index, 
00870             const bool isAuto, const size_t autoIndex );
00874         static const AutoConstantDefinition* getAutoConstantDefinition(const String& name);
00879         static const AutoConstantDefinition* getAutoConstantDefinition(const size_t idx);
00882         static size_t getNumAutoConstantDefinitions(void);
00885         void incPassIterationNumber(void);
00889         RealConstantEntry* getPassIterationEntry(void);
00893         size_t getPassIterationEntryIndex(void) const { return mActivePassIterationEntryIndex; }
00894     };
00895 
00897     typedef SharedPtr<GpuProgramParameters> GpuProgramParametersSharedPtr;
00898 
00899     // Forward declaration 
00900     class GpuProgramPtr;
00901 
00911     class _OgreExport GpuProgram : public Resource
00912     {
00913     protected:
00915         class _OgreExport CmdType : public ParamCommand
00916         {
00917         public:
00918             String doGet(const void* target) const;
00919             void doSet(void* target, const String& val);
00920         };
00921         class _OgreExport CmdSyntax : public ParamCommand
00922         {
00923         public:
00924             String doGet(const void* target) const;
00925             void doSet(void* target, const String& val);
00926         };
00927         class _OgreExport CmdSkeletal : public ParamCommand
00928         {
00929         public:
00930             String doGet(const void* target) const;
00931             void doSet(void* target, const String& val);
00932         };
00933         // Command object for setting / getting parameters
00934         static CmdType msTypeCmd;
00935         static CmdSyntax msSyntaxCmd;
00936         static CmdSkeletal msSkeletalCmd;
00937     
00939         GpuProgramType mType;
00941         String mFilename;
00943         String mSource;
00945         bool mLoadFromFile;
00947         String mSyntaxCode;
00949         bool mSkeletalAnimation;
00951         bool mMorphAnimation;
00953         ushort mPoseAnimation;
00955         GpuProgramParametersSharedPtr mDefaultParams;
00957         bool mPassSurfaceAndLightStates;
00958 
00967         void setupBaseParamDictionary(void);
00968 
00970         size_t calculateSize(void) const { return 0; } // TODO 
00971 
00973         void loadImpl(void);
00974     public:
00975 
00976         GpuProgram(ResourceManager* creator, const String& name, ResourceHandle handle,
00977             const String& group, bool isManual = false, ManualResourceLoader* loader = 0);
00978 
00979         virtual ~GpuProgram() {}
00980 
00985         virtual void setSourceFile(const String& filename);
00986 
00991         virtual void setSource(const String& source);
00992 
00994         virtual const String& getSyntaxCode(void) const { return mSyntaxCode; }
00995 
00997         virtual void setSyntaxCode(const String& syntax);
00998 
01000         virtual const String& getSourceFile(void) const { return mFilename; }
01002         virtual const String& getSource(void) const { return mSource; }
01004         virtual void setType(GpuProgramType t);
01006         virtual GpuProgramType getType(void) const { return mType; }
01007 
01012         virtual GpuProgram* _getBindingDelegate(void) { return this; }
01013 
01015         virtual bool isSupported(void) const;
01016 
01024         virtual GpuProgramParametersSharedPtr createParameters(void);
01025 
01032         virtual void setSkeletalAnimationIncluded(bool included) 
01033         { mSkeletalAnimation = included; }
01034 
01041         virtual bool isSkeletalAnimationIncluded(void) const { return mSkeletalAnimation; }
01042 
01049         virtual void setMorphAnimationIncluded(bool included) 
01050         { mMorphAnimation = included; }
01051 
01059         virtual void setPoseAnimationIncluded(ushort poseCount) 
01060         { mPoseAnimation = poseCount; }
01061 
01068         virtual bool isMorphAnimationIncluded(void) const { return mMorphAnimation; }
01069 
01076         virtual bool isPoseAnimationIncluded(void) const { return mPoseAnimation > 0; }
01080         virtual ushort getNumberOfPosesIncluded(void) const { return mPoseAnimation; }
01081 
01092         virtual GpuProgramParametersSharedPtr getDefaultParameters(void);
01093 
01096         virtual bool hasDefaultParameters(void) const { return !mDefaultParams.isNull(); }
01097 
01106         virtual void setSurfaceAndPassLightStates(bool state)
01107             { mPassSurfaceAndLightStates = state; }
01108 
01112         virtual bool getPassSurfaceAndLightStates(void) const { return mPassSurfaceAndLightStates; }
01113 
01117         virtual const String& getLanguage(void) const;
01118 
01119     protected:
01121         virtual void loadFromSource(void) = 0;
01122 
01123     };
01124 
01125 
01132     class _OgreExport GpuProgramPtr : public SharedPtr<GpuProgram> 
01133     {
01134     public:
01135         GpuProgramPtr() : SharedPtr<GpuProgram>() {}
01136         explicit GpuProgramPtr(GpuProgram* rep) : SharedPtr<GpuProgram>(rep) {}
01137         GpuProgramPtr(const GpuProgramPtr& r) : SharedPtr<GpuProgram>(r) {} 
01138         GpuProgramPtr(const ResourcePtr& r) : SharedPtr<GpuProgram>()
01139         {
01140             // lock & copy other mutex pointer
01141             OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
01142             OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
01143             pRep = static_cast<GpuProgram*>(r.getPointer());
01144             pUseCount = r.useCountPointer();
01145             if (pUseCount)
01146             {
01147                 ++(*pUseCount);
01148             }
01149         }
01150 
01152         GpuProgramPtr& operator=(const ResourcePtr& r)
01153         {
01154             if (pRep == static_cast<GpuProgram*>(r.getPointer()))
01155                 return *this;
01156             release();
01157             // lock & copy other mutex pointer
01158             OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
01159             OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
01160             pRep = static_cast<GpuProgram*>(r.getPointer());
01161             pUseCount = r.useCountPointer();
01162             if (pUseCount)
01163             {
01164                 ++(*pUseCount);
01165             }
01166             return *this;
01167         }
01169         GpuProgramPtr& operator=(const HighLevelGpuProgramPtr& r);
01170     };
01171 }
01172 
01173 #endif

Copyright © 2000-2005 by The OGRE Team
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Mar 12 14:37:42 2006