# 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,
    00122             ACT_TIME,
    00126             ACT_TIME_0_X,
    00128             ACT_COSTIME_0_X,
    00130             ACT_SINTIME_0_X,
    00132             ACT_TANTIME_0_X,
    00136             ACT_TIME_0_X_PACKED,
    00141             ACT_TIME_0_1,
    00143             ACT_COSTIME_0_1,
    00145             ACT_SINTIME_0_1,
    00147             ACT_TANTIME_0_1,
    00151             ACT_TIME_0_1_PACKED,
    00156             ACT_TIME_0_2PI,
    00158             ACT_COSTIME_0_2PI,
    00160             ACT_SINTIME_0_2PI,
    00162             ACT_TANTIME_0_2PI,
    00166             ACT_TIME_0_2PI_PACKED,
    00168             ACT_FPS,
    00170
    00173             ACT_VIEWPORT_WIDTH,
    00177             ACT_VIEWPORT_HEIGHT,
    00181             ACT_INVERSE_VIEWPORT_WIDTH,
    00185             ACT_INVERSE_VIEWPORT_HEIGHT,
    00186
    00188
    00191             ACT_VIEW_DIRECTION,
    00195             ACT_VIEW_SIDE_VECTOR,
    00199             ACT_VIEW_UP_VECTOR,
    00203             ACT_FOV,
    00207             ACT_NEAR_CLIP_DISTANCE,
    00211             ACT_FAR_CLIP_DISTANCE,
    00212
    00214
    00217             ACT_INVERSE_VIEWPROJ_MATRIX,
    00221             ACT_INVERSETRANSPOSE_VIEWPROJ_MATRIX,
    00225             ACT_TRANSPOSE_VIEWPROJ_MATRIX,
    00226
    00230             ACT_TRANSPOSE_VIEW_MATRIX,
    00234             ACT_INVERSETRANSPOSE_VIEW_MATRIX,
    00235
    00239             ACT_TRANSPOSE_PROJECTION_MATRIX,
    00243             ACT_INVERSE_PROJECTION_MATRIX,
    00247             ACT_INVERSETRANSPOSE_PROJECTION_MATRIX,
    00248
    00252             ACT_TRANSPOSE_WORLDVIEWPROJ_MATRIX,
    00256             ACT_INVERSE_WORLDVIEWPROJ_MATRIX,
    00260             ACT_INVERSETRANSPOSE_WORLDVIEWPROJ_MATRIX,
    00261
    00265             ACT_TRANSPOSE_WORLDVIEW_MATRIX,
    00269             ACT_INVERSE_TRANSPOSE_WORLDVIEW_MATRIX,
    00270
    00274             ACT_TRANSPOSE_WORLD_MATRIX,
    00278             ACT_INVERSE_TRANSPOSE_WORLD_MATRIX
    00279
    00280         };
    00282         class _OgrePrivate AutoConstantEntry
    00283         {
    00284         public:
    00286             AutoConstantType paramType;
    00288             size_t index;
    00290             union{
    00291                 size_t data;
    00292                 Real fData;
    00293             };
    00294
    00295             AutoConstantEntry(AutoConstantType theType, size_t theIndex, size_t theData)
    00296                 : paramType(theType), index(theIndex), data(theData) {}
    00297
    00298             AutoConstantEntry(AutoConstantType theType, size_t theIndex, Real theData)
    00299                 : paramType(theType), index(theIndex), fData(theData) {}
    00300
    00301         };
    00306         struct RealConstantEntry
    00307         {
    00308             float val[4];
    00309             bool isSet;
    00310             RealConstantEntry() : isSet(false) {}
    00311         };
    00316         struct IntConstantEntry
    00317         {
    00318             int val[4];
    00319             bool isSet;
    00320             IntConstantEntry() : isSet(false) {}
    00321         };
    00322     protected:
    00323         // Constant lists
    00324         typedef std::vector<RealConstantEntry> RealConstantList;
    00325         typedef std::vector<IntConstantEntry> IntConstantList;
    00326         // Auto parameter storage
    00327         typedef std::vector<AutoConstantEntry> AutoConstantList;
    00329         RealConstantList mRealConstants;
    00331         IntConstantList mIntConstants;
    00333         AutoConstantList mAutoConstants;
    00335         typedef std::map<String, size_t> ParamNameMap;
    00336         ParamNameMap mParamNameMap;
    00338         bool mTransposeMatrices;
    00340         bool mAutoAddParamName;
    00341
    00342     public:
    00343         GpuProgramParameters();
    00344         ~GpuProgramParameters() {}
    00345
    00351         void setConstant(size_t index, const Vector4& vec);
    00359         void setConstant(size_t index, Real val);
    00367         void setConstant(size_t index, const Vector3& vec);
    00374         void setConstant(size_t index, const Matrix4& m);
    00382         void setConstant(size_t index, const Matrix4* m, size_t numEntries);
    00389         void setConstant(size_t index, const float *val, size_t count);
    00396         void setConstant(size_t index, const double *val, size_t count);
    00402         void setConstant(size_t index, const ColourValue& colour);
    00403
    00418         void setConstant(size_t index, const int *val, size_t count);
    00419
    00421         void resetRealConstants(void) { mRealConstants.clear(); }
    00423         void resetIntConstants(void) { mIntConstants.clear(); }
    00424
    00425         typedef ConstVectorIterator<RealConstantList> RealConstantIterator;
    00426         typedef ConstVectorIterator<IntConstantList> IntConstantIterator;
    00428         RealConstantIterator getRealConstantIterator(void) const;
    00430         IntConstantIterator getIntConstantIterator(void) const;
    00431
    00436         RealConstantEntry* getRealConstantEntry(const size_t index);
    00441         IntConstantEntry* getIntConstantEntry(const size_t index);
    00442
    00446         RealConstantEntry* getNamedRealConstantEntry(const String& name);
    00450         IntConstantEntry* getNamedIntConstantEntry(const String& name);
    00452         size_t getRealConstantCount(void) const { return mRealConstants.size(); }
    00454         size_t getIntConstantCount(void) const { return mIntConstants.size(); }
    00456         bool hasRealConstantParams(void) const { return !(mRealConstants.empty()); }
    00458         bool hasIntConstantParams(void) const { return !(mIntConstants.empty()); }
    00459
    00473         void setAutoConstant(size_t index, AutoConstantType acType, size_t extraInfo = 0);
    00474         void setAutoConstantReal(size_t index, AutoConstantType acType, Real rData);
    00479         void setConstantFromTime(size_t index, Real factor);
    00480
    00482         void clearAutoConstants(void);
    00483         typedef ConstVectorIterator<AutoConstantList> AutoConstantIterator;
    00485         AutoConstantIterator getAutoConstantIterator(void) const;
    00487         bool hasAutoConstants(void) const { return !(mAutoConstants.empty()); }
    00489         void _updateAutoParamsNoLights(const AutoParamDataSource& source);
    00491         void _updateAutoParamsLightsOnly(const AutoParamDataSource& source);
    00492
    00504         void setAutoAddParamName(bool state) { mAutoAddParamName = state; }
    00505
    00525         void setNamedConstant(const String& name, Real val);
    00545         void setNamedConstant(const String& name, int val);
    00550         void setNamedConstant(const String& name, const Vector4& vec);
    00563         void setNamedConstant(const String& name, const Vector3& vec);
    00568         void setNamedConstant(const String& name, const Matrix4& m);
    00576         void setNamedConstant(const String& name, const Matrix4* m, size_t numEntries);
    00597         void setNamedConstant(const String& name, const float *val, size_t count);
    00618         void setNamedConstant(const String& name, const double *val, size_t count);
    00623         void setNamedConstant(const String& name, const ColourValue& colour);
    00624
    00645         void setNamedConstant(const String& name, const int *val, size_t count);
    00646
    00661         void setNamedAutoConstant(const String& name, AutoConstantType acType, size_t extraInfo = 0);
    00662
    00670         void setNamedConstantFromTime(const String& name, Real factor);
    00672         void _mapParameterNameToIndex(const String& name, size_t index);
    00673
    00675         size_t getParamIndex(const String& name);
    00676
    00677
    00685         void setTransposeMatrices(bool val) { mTransposeMatrices = val; }
    00687         bool getTransposeMatrices(void) const { return mTransposeMatrices; }
    00688
    00692         void copyConstantsFrom(const GpuProgramParameters& source);
    00693
    00694     };
    00695
    00697     typedef SharedPtr<GpuProgramParameters> GpuProgramParametersSharedPtr;
    00698
    00699     // Forward declaration
    00700     class GpuProgramPtr;
    00701
    00711     class _OgreExport GpuProgram : public Resource
    00712     {
    00713     protected:
    00715         class _OgreExport CmdType : public ParamCommand
    00716         {
    00717         public:
    00718             String doGet(const void* target) const;
    00719             void doSet(void* target, const String& val);
    00720         };
    00721         class _OgreExport CmdSyntax : public ParamCommand
    00722         {
    00723         public:
    00724             String doGet(const void* target) const;
    00725             void doSet(void* target, const String& val);
    00726         };
    00727         class _OgreExport CmdSkeletal : public ParamCommand
    00728         {
    00729         public:
    00730             String doGet(const void* target) const;
    00731             void doSet(void* target, const String& val);
    00732         };
    00733         // Command object for setting / getting parameters
    00734         static CmdType msTypeCmd;
    00735         static CmdSyntax msSyntaxCmd;
    00736         static CmdSkeletal msSkeletalCmd;
    00737
    00739         GpuProgramType mType;
    00741         String mFilename;
    00743         String mSource;
    00745         bool mLoadFromFile;
    00747         String mSyntaxCode;
    00749         bool mSkeletalAnimation;
    00751         GpuProgramParametersSharedPtr mDefaultParams;
    00753         bool mPassSurfaceAndLightStates;
    00754
    00763         void setupBaseParamDictionary(void);
    00764
    00766         size_t calculateSize(void) const { return 0; } // TODO
    00767
    00769         void loadImpl(void);
    00770     public:
    00771
    00772         GpuProgram(ResourceManager* creator, const String& name, ResourceHandle handle,
    00773             const String& group, bool isManual = false, ManualResourceLoader* loader = 0);
    00774
    00775         virtual ~GpuProgram() {}
    00776
    00781         virtual void setSourceFile(const String& filename);
    00782
    00787         virtual void setSource(const String& source);
    00788
    00790         virtual const String& getSyntaxCode(void) const { return mSyntaxCode; }
    00791
    00793         virtual void setSyntaxCode(const String& syntax);
    00794
    00796         virtual const String& getSourceFile(void) const { return mFilename; }
    00798         virtual const String& getSource(void) const { return mSource; }
    00800         virtual void setType(GpuProgramType t);
    00802         virtual GpuProgramType getType(void) const { return mType; }
    00803
    00808         virtual GpuProgram* _getBindingDelegate(void) { return this; }
    00809
    00811         virtual bool isSupported(void) const;
    00812
    00820         virtual GpuProgramParametersSharedPtr createParameters(void);
    00821
    00828         virtual void setSkeletalAnimationIncluded(bool included)
    00829         { mSkeletalAnimation = included; }
    00830
    00837         virtual bool isSkeletalAnimationIncluded(void) const { return mSkeletalAnimation; }
    00838
    00849         virtual GpuProgramParametersSharedPtr getDefaultParameters(void);
    00850
    00859         virtual void setSurfaceAndPassLightStates(bool state)
    00860             { mPassSurfaceAndLightStates = state; }
    00861
    00865         virtual bool getPassSurfaceAndLightStates(void) const { return mPassSurfaceAndLightStates; }
    00866
    00867     protected:
    00869         virtual void loadFromSource(void) = 0;
    00870
    00871     };
    00872
    00873
    00880     class _OgreExport GpuProgramPtr : public SharedPtr<GpuProgram>
    00881     {
    00882     public:
    00883         GpuProgramPtr() : SharedPtr<GpuProgram>() {}
    00884         explicit GpuProgramPtr(GpuProgram* rep) : SharedPtr<GpuProgram>(rep) {}
    00885         GpuProgramPtr(const GpuProgramPtr& r) : SharedPtr<GpuProgram>(r) {}
    00886         GpuProgramPtr(const ResourcePtr& r) : SharedPtr<GpuProgram>()
    00887         {
    00888             // lock & copy other mutex pointer
    00889             OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
    00890             OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
    00891             pRep = static_cast<GpuProgram*>(r.getPointer());
    00892             pUseCount = r.useCountPointer();
    00893             if (pUseCount)
    00894             {
    00895                 ++(*pUseCount);
    00896             }
    00897         }
    00898
    00900         GpuProgramPtr& operator=(const ResourcePtr& r)
    00901         {
    00902             if (pRep == static_cast<GpuProgram*>(r.getPointer()))
    00903                 return *this;
    00904             release();
    00905             // lock & copy other mutex pointer
    00906             OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
    00907             OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
    00908             pRep = static_cast<GpuProgram*>(r.getPointer());
    00909             pUseCount = r.useCountPointer();
    00910             if (pUseCount)
    00911             {
    00912                 ++(*pUseCount);
    00913             }
    00914             return *this;
    00915         }
    00917         GpuProgramPtr& operator=(const HighLevelGpuProgramPtr& r);
    00918     };
    00919 }
    00920
    00921 #endif

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