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_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
00120         };
00122         class AutoConstantEntry
00123         {
00124         public:
00126             AutoConstantType paramType;
00128             size_t index;
00130             size_t data;
00131 
00132             AutoConstantEntry(AutoConstantType theType, size_t theIndex, size_t theData)
00133                 : paramType(theType), index(theIndex), data(theData) {}
00134 
00135         };
00140         struct RealConstantEntry
00141         {
00142             float val[4];
00143             bool isSet;
00144             RealConstantEntry() : isSet(false) {}
00145         };
00150         struct IntConstantEntry
00151         {
00152             int val[4];
00153             bool isSet;
00154             IntConstantEntry() : isSet(false) {}
00155         };
00156     protected:
00157         // Constant lists
00158         typedef std::vector<RealConstantEntry> RealConstantList;
00159         typedef std::vector<IntConstantEntry> IntConstantList;
00160         // Auto parameter storage
00161         typedef std::vector<AutoConstantEntry> AutoConstantList;
00163         RealConstantList mRealConstants;
00165         IntConstantList mIntConstants;
00167         AutoConstantList mAutoConstants;
00169         typedef std::map<String, size_t> ParamNameMap;
00170         ParamNameMap mParamNameMap;
00172         bool mTransposeMatrices;
00174         bool mAutoAddParamName;
00175 
00176     public:
00177         GpuProgramParameters();
00178         ~GpuProgramParameters() {}
00179 
00185         void setConstant(size_t index, const Vector4& vec);
00193         void setConstant(size_t index, const Vector3& vec);
00200         void setConstant(size_t index, const Matrix4& m);
00208         void setConstant(size_t index, const Matrix4* m, size_t numEntries);
00215         void setConstant(size_t index, const float *val, size_t count);
00222         void setConstant(size_t index, const double *val, size_t count);
00228         void setConstant(size_t index, const ColourValue& colour);
00229         
00244         void setConstant(size_t index, const int *val, size_t count);
00245 
00247         void resetRealConstants(void) { mRealConstants.clear(); }
00249         void resetIntConstants(void) { mIntConstants.clear(); }
00250 
00251         typedef ConstVectorIterator<RealConstantList> RealConstantIterator;
00252         typedef ConstVectorIterator<IntConstantList> IntConstantIterator;
00254         RealConstantIterator getRealConstantIterator(void) const;
00256         IntConstantIterator getIntConstantIterator(void) const;
00257 
00262         RealConstantEntry* getRealConstantEntry(const size_t index);
00267         IntConstantEntry* getIntConstantEntry(const size_t index);
00268         
00272         RealConstantEntry* getNamedRealConstantEntry(const String& name);
00276         IntConstantEntry* getNamedIntConstantEntry(const String& name);
00278         size_t getRealConstantCount(void) const { return mRealConstants.size(); }
00280         size_t getIntConstantCount(void) const { return mIntConstants.size(); }
00282         bool hasRealConstantParams(void) const { return !(mRealConstants.empty()); }
00284         bool hasIntConstantParams(void) const { return !(mIntConstants.empty()); }
00285 
00299         void setAutoConstant(size_t index, AutoConstantType acType, size_t extraInfo = 0);
00304         void setConstantFromTime(size_t index, Real factor);
00305 
00307         void clearAutoConstants(void);
00308         typedef ConstVectorIterator<AutoConstantList> AutoConstantIterator;
00310         AutoConstantIterator getAutoConstantIterator(void) const;
00312         bool hasAutoConstants(void) const { return !(mAutoConstants.empty()); }
00314         void _updateAutoParamsNoLights(const AutoParamDataSource& source);
00316         void _updateAutoParamsLightsOnly(const AutoParamDataSource& source);
00317 
00329         void setAutoAddParamName(bool state) { mAutoAddParamName = state; }
00330 
00350         void setNamedConstant(const String& name, Real val);
00370         void setNamedConstant(const String& name, int val);
00375         void setNamedConstant(const String& name, const Vector4& vec);
00388         void setNamedConstant(const String& name, const Vector3& vec);
00393         void setNamedConstant(const String& name, const Matrix4& m);
00401         void setNamedConstant(const String& name, const Matrix4* m, size_t numEntries);
00422         void setNamedConstant(const String& name, const float *val, size_t count);
00443         void setNamedConstant(const String& name, const double *val, size_t count);
00448         void setNamedConstant(const String& name, const ColourValue& colour);
00449         
00470         void setNamedConstant(const String& name, const int *val, size_t count);
00471 
00486         void setNamedAutoConstant(const String& name, AutoConstantType acType, size_t extraInfo = 0);
00487 
00495         void setNamedConstantFromTime(const String& name, Real factor);
00497         void _mapParameterNameToIndex(const String& name, size_t index);
00498 
00500         size_t getParamIndex(const String& name);
00501 
00502 
00510         void setTransposeMatrices(bool val) { mTransposeMatrices = val; } 
00512         bool getTransposeMatrices(void) const { return mTransposeMatrices; } 
00513 
00517         void copyConstantsFrom(const GpuProgramParameters& source);
00518         
00519     };
00520 
00522     typedef SharedPtr<GpuProgramParameters> GpuProgramParametersSharedPtr;
00523 
00524     // Forward declaration 
00525     class GpuProgramPtr;
00526 
00536     class _OgreExport GpuProgram : public Resource
00537     {
00538     protected:
00540         class _OgreExport CmdType : public ParamCommand
00541         {
00542         public:
00543             String doGet(const void* target) const;
00544             void doSet(void* target, const String& val);
00545         };
00546         class _OgreExport CmdSyntax : public ParamCommand
00547         {
00548         public:
00549             String doGet(const void* target) const;
00550             void doSet(void* target, const String& val);
00551         };
00552         class _OgreExport CmdSkeletal : public ParamCommand
00553         {
00554         public:
00555             String doGet(const void* target) const;
00556             void doSet(void* target, const String& val);
00557         };
00558         // Command object for setting / getting parameters
00559         static CmdType msTypeCmd;
00560         static CmdSyntax msSyntaxCmd;
00561         static CmdSkeletal msSkeletalCmd;
00562     
00564         GpuProgramType mType;
00566         String mFilename;
00568         String mSource;
00570         bool mLoadFromFile;
00572         String mSyntaxCode;
00574         bool mSkeletalAnimation;
00576         GpuProgramParametersSharedPtr mDefaultParams;
00578         bool mPassSurfaceAndLightStates;
00579 
00588         void setupBaseParamDictionary(void);
00589 
00591         size_t calculateSize(void) const { return 0; } // TODO 
00592 
00594         void loadImpl(void);
00595     public:
00596 
00597         GpuProgram(ResourceManager* creator, const String& name, ResourceHandle handle,
00598             const String& group, bool isManual = false, ManualResourceLoader* loader = 0);
00599 
00600         virtual ~GpuProgram() {}
00601 
00606         virtual void setSourceFile(const String& filename);
00607 
00612         virtual void setSource(const String& source);
00613 
00615         virtual const String& getSyntaxCode(void) const { return mSyntaxCode; }
00616 
00618         virtual void setSyntaxCode(const String& syntax);
00619 
00621         virtual const String& getSourceFile(void) const { return mFilename; }
00623         virtual const String& getSource(void) const { return mSource; }
00625         virtual void setType(GpuProgramType t);
00627         virtual GpuProgramType getType(void) const { return mType; }
00628 
00633         virtual GpuProgram* _getBindingDelegate(void) { return this; }
00634 
00636         virtual bool isSupported(void) const;
00637 
00645         virtual GpuProgramParametersSharedPtr createParameters(void);
00646 
00653         virtual void setSkeletalAnimationIncluded(bool included) 
00654         { mSkeletalAnimation = included; }
00655 
00662         virtual bool isSkeletalAnimationIncluded(void) const { return mSkeletalAnimation; }
00663 
00674         virtual GpuProgramParametersSharedPtr getDefaultParameters(void);
00675 
00684         virtual void setSurfaceAndPassLightStates(bool state)
00685             { mPassSurfaceAndLightStates = state; }
00686 
00690         virtual bool getPassSurfaceAndLightStates(void) const { return mPassSurfaceAndLightStates; }
00691 
00692     protected:
00694         virtual void loadFromSource(void) = 0;
00695 
00696     };
00697 
00698 
00705     class _OgreExport GpuProgramPtr : public SharedPtr<GpuProgram> 
00706     {
00707     public:
00708         GpuProgramPtr() : SharedPtr<GpuProgram>() {}
00709         explicit GpuProgramPtr(GpuProgram* rep) : SharedPtr<GpuProgram>(rep) {}
00710         GpuProgramPtr(const GpuProgramPtr& r) : SharedPtr<GpuProgram>(r) {} 
00711         GpuProgramPtr(const ResourcePtr& r) : SharedPtr<GpuProgram>()
00712         {
00713             // lock & copy other mutex pointer
00714             OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
00715             OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
00716             pRep = static_cast<GpuProgram*>(r.getPointer());
00717             pUseCount = r.useCountPointer();
00718             if (pUseCount)
00719             {
00720                 ++(*pUseCount);
00721             }
00722         }
00723 
00725         GpuProgramPtr& operator=(const ResourcePtr& r)
00726         {
00727             if (pRep == static_cast<GpuProgram*>(r.getPointer()))
00728                 return *this;
00729             release();
00730             // lock & copy other mutex pointer
00731             OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
00732             OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
00733             pRep = static_cast<GpuProgram*>(r.getPointer());
00734             pUseCount = r.useCountPointer();
00735             if (pUseCount)
00736             {
00737                 ++(*pUseCount);
00738             }
00739             return *this;
00740         }
00742         GpuProgramPtr& operator=(const HighLevelGpuProgramPtr& r);
00743     };
00744 }
00745 
00746 #endif

Copyright © 2000-2005 by The OGRE Team
Last modified Wed Feb 23 00:19:07 2005