# OgreGpuProgram.h Source File - OGRE Documentation

# 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_WORLD_MATRIX_ARRAY_3x4,
    00068             ACT_WORLD_MATRIX_ARRAY,
    00070             ACT_VIEW_MATRIX,
    00072             ACT_PROJECTION_MATRIX,
    00074             ACT_VIEWPROJ_MATRIX,
    00076             ACT_WORLDVIEW_MATRIX,
    00078             ACT_WORLDVIEWPROJ_MATRIX,
    00080             ACT_INVERSE_WORLD_MATRIX,
    00082             ACT_INVERSE_VIEW_MATRIX,
    00084             ACT_INVERSE_WORLDVIEW_MATRIX,
    00086             ACT_INVERSETRANSPOSE_WORLD_MATRIX,
    00088             ACT_INVERSETRANSPOSE_WORLDVIEW_MATRIX,
    00090             ACT_LIGHT_DIFFUSE_COLOUR,
    00092             ACT_LIGHT_SPECULAR_COLOUR,
    00094             ACT_LIGHT_ATTENUATION,
    00096             ACT_LIGHT_POSITION,
    00098             ACT_LIGHT_DIRECTION,
    00100             ACT_LIGHT_POSITION_OBJECT_SPACE,
    00102             ACT_LIGHT_DIRECTION_OBJECT_SPACE,
    00107             ACT_LIGHT_DISTANCE_OBJECT_SPACE,
    00111             ACT_SHADOW_EXTRUSION_DISTANCE,
    00113             ACT_CAMERA_POSITION_OBJECT_SPACE,
    00115             ACT_AMBIENT_LIGHT_COLOUR,
    00117             ACT_TEXTURE_VIEWPROJ_MATRIX,
    00119             ACT_CUSTOM,
    00121             ACT_CAMERA_POSITION,
    00125             ACT_TIME_0_X,
    00127             ACT_COSTIME_0_X,
    00129             ACT_SINTIME_0_X,
    00131             ACT_TANTIME_0_X,
    00135             ACT_TIME_0_X_PACKED,
    00140             ACT_TIME_0_1,
    00142             ACT_COSTIME_0_1,
    00144             ACT_SINTIME_0_1,
    00146             ACT_TANTIME_0_1,
    00150             ACT_TIME_0_1_PACKED,
    00155             ACT_TIME_0_2PI,
    00157             ACT_COSTIME_0_2PI,
    00159             ACT_SINTIME_0_2PI,
    00161             ACT_TANTIME_0_2PI,
    00165             ACT_TIME_0_2PI_PACKED,
    00167             ACT_FPS,
    00169
    00172             ACT_VIEWPORT_WIDTH,
    00176             ACT_VIEWPORT_HEIGHT,
    00180             ACT_INVERSE_VIEWPORT_WIDTH,
    00184             ACT_INVERSE_VIEWPORT_HEIGHT,
    00185
    00187
    00190             ACT_VIEW_DIRECTION,
    00194             ACT_VIEW_SIDE_VECTOR,
    00198             ACT_VIEW_UP_VECTOR,
    00202             ACT_FOV,
    00206             ACT_NEAR_CLIP_DISTANCE,
    00210             ACT_FAR_CLIP_DISTANCE,
    00211
    00213
    00216             ACT_INVERSE_VIEWPROJ_MATRIX,
    00220             ACT_INVERSETRANSPOSE_VIEWPROJ_MATRIX,
    00224             ACT_TRANSPOSE_VIEWPROJ_MATRIX,
    00225
    00229             ACT_TRANSPOSE_VIEW_MATRIX,
    00233             ACT_INVERSETRANSPOSE_VIEW_MATRIX,
    00234
    00238             ACT_TRANSPOSE_PROJECTION_MATRIX,
    00242             ACT_INVERSE_PROJECTION_MATRIX,
    00246             ACT_INVERSETRANSPOSE_PROJECTION_MATRIX,
    00247
    00251             ACT_TRANSPOSE_WORLDVIEWPROJ_MATRIX,
    00255             ACT_INVERSE_WORLDVIEWPROJ_MATRIX,
    00259             ACT_INVERSETRANSPOSE_WORLDVIEWPROJ_MATRIX,
    00260
    00264             ACT_TRANSPOSE_WORLDVIEW_MATRIX,
    00268             ACT_INVERSE_TRANSPOSE_WORLDVIEW_MATRIX,
    00269
    00273             ACT_TRANSPOSE_WORLD_MATRIX,
    00277             ACT_INVERSE_TRANSPOSE_WORLD_MATRIX
    00278
    00279         };
    00281         class _OgrePrivate AutoConstantEntry
    00282         {
    00283         public:
    00285             AutoConstantType paramType;
    00287             size_t index;
    00289             union{
    00290                 size_t data;
    00291                 Real fData;
    00292             };
    00293
    00294             AutoConstantEntry(AutoConstantType theType, size_t theIndex, size_t theData)
    00295                 : paramType(theType), index(theIndex), data(theData) {}
    00296
    00297             AutoConstantEntry(AutoConstantType theType, size_t theIndex, Real theData)
    00298                 : paramType(theType), index(theIndex), fData(theData) {}
    00299
    00300         };
    00305         struct RealConstantEntry
    00306         {
    00307             float val[4];
    00308             bool isSet;
    00309             RealConstantEntry() : isSet(false) {}
    00310         };
    00315         struct IntConstantEntry
    00316         {
    00317             int val[4];
    00318             bool isSet;
    00319             IntConstantEntry() : isSet(false) {}
    00320         };
    00321     protected:
    00322         // Constant lists
    00323         typedef std::vector<RealConstantEntry> RealConstantList;
    00324         typedef std::vector<IntConstantEntry> IntConstantList;
    00325         // Auto parameter storage
    00326         typedef std::vector<AutoConstantEntry> AutoConstantList;
    00328         RealConstantList mRealConstants;
    00330         IntConstantList mIntConstants;
    00332         AutoConstantList mAutoConstants;
    00334         typedef std::map<String, size_t> ParamNameMap;
    00335         ParamNameMap mParamNameMap;
    00337         bool mTransposeMatrices;
    00339         bool mAutoAddParamName;
    00340
    00341     public:
    00342         GpuProgramParameters();
    00343         ~GpuProgramParameters() {}
    00344
    00350         void setConstant(size_t index, const Vector4& vec);
    00358         void setConstant(size_t index, Real val);
    00366         void setConstant(size_t index, const Vector3& vec);
    00373         void setConstant(size_t index, const Matrix4& m);
    00381         void setConstant(size_t index, const Matrix4* m, size_t numEntries);
    00388         void setConstant(size_t index, const float *val, size_t count);
    00395         void setConstant(size_t index, const double *val, size_t count);
    00401         void setConstant(size_t index, const ColourValue& colour);
    00402
    00417         void setConstant(size_t index, const int *val, size_t count);
    00418
    00420         void resetRealConstants(void) { mRealConstants.clear(); }
    00422         void resetIntConstants(void) { mIntConstants.clear(); }
    00423
    00424         typedef ConstVectorIterator<RealConstantList> RealConstantIterator;
    00425         typedef ConstVectorIterator<IntConstantList> IntConstantIterator;
    00427         RealConstantIterator getRealConstantIterator(void) const;
    00429         IntConstantIterator getIntConstantIterator(void) const;
    00430
    00435         RealConstantEntry* getRealConstantEntry(const size_t index);
    00440         IntConstantEntry* getIntConstantEntry(const size_t index);
    00441
    00445         RealConstantEntry* getNamedRealConstantEntry(const String& name);
    00449         IntConstantEntry* getNamedIntConstantEntry(const String& name);
    00451         size_t getRealConstantCount(void) const { return mRealConstants.size(); }
    00453         size_t getIntConstantCount(void) const { return mIntConstants.size(); }
    00455         bool hasRealConstantParams(void) const { return !(mRealConstants.empty()); }
    00457         bool hasIntConstantParams(void) const { return !(mIntConstants.empty()); }
    00458
    00472         void setAutoConstant(size_t index, AutoConstantType acType, size_t extraInfo = 0);
    00473         void setAutoConstantReal(size_t index, AutoConstantType acType, Real rData);
    00478         void setConstantFromTime(size_t index, Real factor);
    00479
    00481         void clearAutoConstants(void);
    00482         typedef ConstVectorIterator<AutoConstantList> AutoConstantIterator;
    00484         AutoConstantIterator getAutoConstantIterator(void) const;
    00486         bool hasAutoConstants(void) const { return !(mAutoConstants.empty()); }
    00488         void _updateAutoParamsNoLights(const AutoParamDataSource& source);
    00490         void _updateAutoParamsLightsOnly(const AutoParamDataSource& source);
    00491
    00503         void setAutoAddParamName(bool state) { mAutoAddParamName = state; }
    00504
    00524         void setNamedConstant(const String& name, Real val);
    00544         void setNamedConstant(const String& name, int val);
    00549         void setNamedConstant(const String& name, const Vector4& vec);
    00562         void setNamedConstant(const String& name, const Vector3& vec);
    00567         void setNamedConstant(const String& name, const Matrix4& m);
    00575         void setNamedConstant(const String& name, const Matrix4* m, size_t numEntries);
    00596         void setNamedConstant(const String& name, const float *val, size_t count);
    00617         void setNamedConstant(const String& name, const double *val, size_t count);
    00622         void setNamedConstant(const String& name, const ColourValue& colour);
    00623
    00644         void setNamedConstant(const String& name, const int *val, size_t count);
    00645
    00660         void setNamedAutoConstant(const String& name, AutoConstantType acType, size_t extraInfo = 0);
    00661
    00669         void setNamedConstantFromTime(const String& name, Real factor);
    00671         void _mapParameterNameToIndex(const String& name, size_t index);
    00672
    00674         size_t getParamIndex(const String& name);
    00675
    00676
    00684         void setTransposeMatrices(bool val) { mTransposeMatrices = val; }
    00686         bool getTransposeMatrices(void) const { return mTransposeMatrices; }
    00687
    00691         void copyConstantsFrom(const GpuProgramParameters& source);
    00692
    00693     };
    00694
    00696     typedef SharedPtr<GpuProgramParameters> GpuProgramParametersSharedPtr;
    00697
    00698     // Forward declaration
    00699     class GpuProgramPtr;
    00700
    00710     class _OgreExport GpuProgram : public Resource
    00711     {
    00712     protected:
    00714         class _OgreExport CmdType : public ParamCommand
    00715         {
    00716         public:
    00717             String doGet(const void* target) const;
    00718             void doSet(void* target, const String& val);
    00719         };
    00720         class _OgreExport CmdSyntax : public ParamCommand
    00721         {
    00722         public:
    00723             String doGet(const void* target) const;
    00724             void doSet(void* target, const String& val);
    00725         };
    00726         class _OgreExport CmdSkeletal : public ParamCommand
    00727         {
    00728         public:
    00729             String doGet(const void* target) const;
    00730             void doSet(void* target, const String& val);
    00731         };
    00732         // Command object for setting / getting parameters
    00733         static CmdType msTypeCmd;
    00734         static CmdSyntax msSyntaxCmd;
    00735         static CmdSkeletal msSkeletalCmd;
    00736
    00738         GpuProgramType mType;
    00740         String mFilename;
    00742         String mSource;
    00744         bool mLoadFromFile;
    00746         String mSyntaxCode;
    00748         bool mSkeletalAnimation;
    00750         GpuProgramParametersSharedPtr mDefaultParams;
    00752         bool mPassSurfaceAndLightStates;
    00753
    00762         void setupBaseParamDictionary(void);
    00763
    00765         size_t calculateSize(void) const { return 0; } // TODO
    00766
    00768         void loadImpl(void);
    00769     public:
    00770
    00771         GpuProgram(ResourceManager* creator, const String& name, ResourceHandle handle,
    00772             const String& group, bool isManual = false, ManualResourceLoader* loader = 0);
    00773
    00774         virtual ~GpuProgram() {}
    00775
    00780         virtual void setSourceFile(const String& filename);
    00781
    00786         virtual void setSource(const String& source);
    00787
    00789         virtual const String& getSyntaxCode(void) const { return mSyntaxCode; }
    00790
    00792         virtual void setSyntaxCode(const String& syntax);
    00793
    00795         virtual const String& getSourceFile(void) const { return mFilename; }
    00797         virtual const String& getSource(void) const { return mSource; }
    00799         virtual void setType(GpuProgramType t);
    00801         virtual GpuProgramType getType(void) const { return mType; }
    00802
    00807         virtual GpuProgram* _getBindingDelegate(void) { return this; }
    00808
    00810         virtual bool isSupported(void) const;
    00811
    00819         virtual GpuProgramParametersSharedPtr createParameters(void);
    00820
    00827         virtual void setSkeletalAnimationIncluded(bool included)
    00828         { mSkeletalAnimation = included; }
    00829
    00836         virtual bool isSkeletalAnimationIncluded(void) const { return mSkeletalAnimation; }
    00837
    00848         virtual GpuProgramParametersSharedPtr getDefaultParameters(void);
    00849
    00858         virtual void setSurfaceAndPassLightStates(bool state)
    00859             { mPassSurfaceAndLightStates = state; }
    00860
    00864         virtual bool getPassSurfaceAndLightStates(void) const { return mPassSurfaceAndLightStates; }
    00865
    00866     protected:
    00868         virtual void loadFromSource(void) = 0;
    00869
    00870     };
    00871
    00872
    00879     class _OgreExport GpuProgramPtr : public SharedPtr<GpuProgram>
    00880     {
    00881     public:
    00882         GpuProgramPtr() : SharedPtr<GpuProgram>() {}
    00883         explicit GpuProgramPtr(GpuProgram* rep) : SharedPtr<GpuProgram>(rep) {}
    00884         GpuProgramPtr(const GpuProgramPtr& r) : SharedPtr<GpuProgram>(r) {}
    00885         GpuProgramPtr(const ResourcePtr& r) : SharedPtr<GpuProgram>()
    00886         {
    00887             // lock & copy other mutex pointer
    00888             OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
    00889             OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
    00890             pRep = static_cast<GpuProgram*>(r.getPointer());
    00891             pUseCount = r.useCountPointer();
    00892             if (pUseCount)
    00893             {
    00894                 ++(*pUseCount);
    00895             }
    00896         }
    00897
    00899         GpuProgramPtr& operator=(const ResourcePtr& r)
    00900         {
    00901             if (pRep == static_cast<GpuProgram*>(r.getPointer()))
    00902                 return *this;
    00903             release();
    00904             // lock & copy other mutex pointer
    00905             OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
    00906             OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
    00907             pRep = static_cast<GpuProgram*>(r.getPointer());
    00908             pUseCount = r.useCountPointer();
    00909             if (pUseCount)
    00910             {
    00911                 ++(*pUseCount);
    00912             }
    00913             return *this;
    00914         }
    00916         GpuProgramPtr& operator=(const HighLevelGpuProgramPtr& r);
    00917     };
    00918 }
    00919
    00920 #endif

Copyright © 2000-2005 by The OGRE Team
Last modified Sun Sep 25 17:35:54 2005