# 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;
    00959         bool mCompileError;
    00960
    00969         void setupBaseParamDictionary(void);
    00970
    00972         size_t calculateSize(void) const { return 0; } // TODO
    00973
    00975         void loadImpl(void);
    00976     public:
    00977
    00978         GpuProgram(ResourceManager* creator, const String& name, ResourceHandle handle,
    00979             const String& group, bool isManual = false, ManualResourceLoader* loader = 0);
    00980
    00981         virtual ~GpuProgram() {}
    00982
    00987         virtual void setSourceFile(const String& filename);
    00988
    00993         virtual void setSource(const String& source);
    00994
    00996         virtual const String& getSyntaxCode(void) const { return mSyntaxCode; }
    00997
    00999         virtual void setSyntaxCode(const String& syntax);
    01000
    01002         virtual const String& getSourceFile(void) const { return mFilename; }
    01004         virtual const String& getSource(void) const { return mSource; }
    01006         virtual void setType(GpuProgramType t);
    01008         virtual GpuProgramType getType(void) const { return mType; }
    01009
    01014         virtual GpuProgram* _getBindingDelegate(void) { return this; }
    01015
    01017         virtual bool isSupported(void) const;
    01018
    01026         virtual GpuProgramParametersSharedPtr createParameters(void);
    01027
    01034         virtual void setSkeletalAnimationIncluded(bool included)
    01035         { mSkeletalAnimation = included; }
    01036
    01043         virtual bool isSkeletalAnimationIncluded(void) const { return mSkeletalAnimation; }
    01044
    01051         virtual void setMorphAnimationIncluded(bool included)
    01052         { mMorphAnimation = included; }
    01053
    01061         virtual void setPoseAnimationIncluded(ushort poseCount)
    01062         { mPoseAnimation = poseCount; }
    01063
    01070         virtual bool isMorphAnimationIncluded(void) const { return mMorphAnimation; }
    01071
    01078         virtual bool isPoseAnimationIncluded(void) const { return mPoseAnimation > 0; }
    01082         virtual ushort getNumberOfPosesIncluded(void) const { return mPoseAnimation; }
    01083
    01094         virtual GpuProgramParametersSharedPtr getDefaultParameters(void);
    01095
    01098         virtual bool hasDefaultParameters(void) const { return !mDefaultParams.isNull(); }
    01099
    01108         virtual void setSurfaceAndPassLightStates(bool state)
    01109             { mPassSurfaceAndLightStates = state; }
    01110
    01114         virtual bool getPassSurfaceAndLightStates(void) const { return mPassSurfaceAndLightStates; }
    01115
    01119         virtual const String& getLanguage(void) const;
    01120
    01123         virtual bool hasCompileError(void) const { return mCompileError; }
    01124
    01127         virtual void resetCompileError(void) { mCompileError = false; }
    01128     protected:
    01130         virtual void loadFromSource(void) = 0;
    01131
    01132     };
    01133
    01134
    01141     class _OgreExport GpuProgramPtr : public SharedPtr<GpuProgram>
    01142     {
    01143     public:
    01144         GpuProgramPtr() : SharedPtr<GpuProgram>() {}
    01145         explicit GpuProgramPtr(GpuProgram* rep) : SharedPtr<GpuProgram>(rep) {}
    01146         GpuProgramPtr(const GpuProgramPtr& r) : SharedPtr<GpuProgram>(r) {}
    01147         GpuProgramPtr(const ResourcePtr& r) : SharedPtr<GpuProgram>()
    01148         {
    01149             // lock & copy other mutex pointer
    01150             OGRE_MUTEX_CONDITIONAL(r.OGRE_AUTO_MUTEX_NAME)
    01151             {
    01152                 OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
    01153                 OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
    01154                 pRep = static_cast<GpuProgram*>(r.getPointer());
    01155                 pUseCount = r.useCountPointer();
    01156                 if (pUseCount)
    01157                 {
    01158                     ++(*pUseCount);
    01159                 }
    01160             }
    01161         }
    01162
    01164         GpuProgramPtr& operator=(const ResourcePtr& r)
    01165         {
    01166             if (pRep == static_cast<GpuProgram*>(r.getPointer()))
    01167                 return *this;
    01168             release();
    01169             // lock & copy other mutex pointer
    01170             OGRE_MUTEX_CONDITIONAL(r.OGRE_AUTO_MUTEX_NAME)
    01171             {
    01172                 OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
    01173                 OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
    01174                 pRep = static_cast<GpuProgram*>(r.getPointer());
    01175                 pUseCount = r.useCountPointer();
    01176                 if (pUseCount)
    01177                 {
    01178                     ++(*pUseCount);
    01179                 }
    01180             }
    01181             return *this;
    01182         }
    01184         GpuProgramPtr& operator=(const HighLevelGpuProgramPtr& r);
    01185     };
    01186 }
    01187
    01188 #endif

Copyright © 2000-2005 by The OGRE Team
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Sep 17 15:39:09 2006