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); 00605 00607 void clearAutoConstant(size_t index); 00608 00613 void setConstantFromTime(size_t index, Real factor); 00614 00616 void clearAutoConstants(void); 00617 typedef ConstVectorIterator<AutoConstantList> AutoConstantIterator; 00619 AutoConstantIterator getAutoConstantIterator(void) const; 00621 size_t getAutoConstantCount(void) const { return mAutoConstants.size(); } 00626 AutoConstantEntry* getAutoConstantEntry(const size_t index); 00628 bool hasAutoConstants(void) const { return !(mAutoConstants.empty()); } 00630 void _updateAutoParamsNoLights(const AutoParamDataSource& source); 00632 void _updateAutoParamsLightsOnly(const AutoParamDataSource& source); 00633 00645 void setAutoAddParamName(bool state) { mAutoAddParamName = state; } 00646 00666 void setNamedConstant(const String& name, Real val); 00686 void setNamedConstant(const String& name, int val); 00691 void setNamedConstant(const String& name, const Vector4& vec); 00704 void setNamedConstant(const String& name, const Vector3& vec); 00709 void setNamedConstant(const String& name, const Matrix4& m); 00717 void setNamedConstant(const String& name, const Matrix4* m, size_t numEntries); 00738 void setNamedConstant(const String& name, const float *val, size_t count); 00759 void setNamedConstant(const String& name, const double *val, size_t count); 00764 void setNamedConstant(const String& name, const ColourValue& colour); 00765 00786 void setNamedConstant(const String& name, const int *val, size_t count); 00787 00802 void setNamedAutoConstant(const String& name, AutoConstantType acType, size_t extraInfo = 0); 00803 00811 void setNamedConstantFromTime(const String& name, Real factor); 00812 00814 void clearNamedAutoConstant(const String& name); 00815 00817 void _mapParameterNameToIndex(const String& name, const size_t index ); 00818 00820 size_t getParamIndex(const String& name); 00821 00822 00830 void setTransposeMatrices(bool val) { mTransposeMatrices = val; } 00832 bool getTransposeMatrices(void) const { return mTransposeMatrices; } 00833 00837 void copyConstantsFrom(const GpuProgramParameters& source); 00838 00851 size_t addConstantDefinition(const String& name, const size_t index, 00852 const size_t elementCount, const ElementType elementType); 00853 00857 const ConstantDefinition* getConstantDefinition(const String& name) const; 00862 const ConstantDefinition* getConstantDefinition(const size_t idx) const; 00866 const ConstantDefinition* findMatchingConstantDefinition(const String& name, 00867 const size_t entryIndex, const ElementType elementType) const; 00868 00871 size_t getNumConstantDefinitions(void) const { return mConstantDefinitions.size(); } 00877 void setConstantDefinitionAutoState( const size_t index, 00878 const bool isAuto, const size_t autoIndex ); 00882 static const AutoConstantDefinition* getAutoConstantDefinition(const String& name); 00887 static const AutoConstantDefinition* getAutoConstantDefinition(const size_t idx); 00890 static size_t getNumAutoConstantDefinitions(void); 00893 void incPassIterationNumber(void); 00897 RealConstantEntry* getPassIterationEntry(void); 00901 size_t getPassIterationEntryIndex(void) const { return mActivePassIterationEntryIndex; } 00902 }; 00903 00905 typedef SharedPtr<GpuProgramParameters> GpuProgramParametersSharedPtr; 00906 00907 // Forward declaration 00908 class GpuProgramPtr; 00909 00919 class _OgreExport GpuProgram : public Resource 00920 { 00921 protected: 00923 class _OgreExport CmdType : public ParamCommand 00924 { 00925 public: 00926 String doGet(const void* target) const; 00927 void doSet(void* target, const String& val); 00928 }; 00929 class _OgreExport CmdSyntax : public ParamCommand 00930 { 00931 public: 00932 String doGet(const void* target) const; 00933 void doSet(void* target, const String& val); 00934 }; 00935 class _OgreExport CmdSkeletal : public ParamCommand 00936 { 00937 public: 00938 String doGet(const void* target) const; 00939 void doSet(void* target, const String& val); 00940 }; 00941 // Command object for setting / getting parameters 00942 static CmdType msTypeCmd; 00943 static CmdSyntax msSyntaxCmd; 00944 static CmdSkeletal msSkeletalCmd; 00945 00947 GpuProgramType mType; 00949 String mFilename; 00951 String mSource; 00953 bool mLoadFromFile; 00955 String mSyntaxCode; 00957 bool mSkeletalAnimation; 00959 bool mMorphAnimation; 00961 ushort mPoseAnimation; 00963 GpuProgramParametersSharedPtr mDefaultParams; 00965 bool mPassSurfaceAndLightStates; 00967 bool mCompileError; 00968 00977 void setupBaseParamDictionary(void); 00978 00980 size_t calculateSize(void) const { return 0; } // TODO 00981 00983 void loadImpl(void); 00984 public: 00985 00986 GpuProgram(ResourceManager* creator, const String& name, ResourceHandle handle, 00987 const String& group, bool isManual = false, ManualResourceLoader* loader = 0); 00988 00989 virtual ~GpuProgram() {} 00990 00995 virtual void setSourceFile(const String& filename); 00996 01001 virtual void setSource(const String& source); 01002 01004 virtual const String& getSyntaxCode(void) const { return mSyntaxCode; } 01005 01007 virtual void setSyntaxCode(const String& syntax); 01008 01010 virtual const String& getSourceFile(void) const { return mFilename; } 01012 virtual const String& getSource(void) const { return mSource; } 01014 virtual void setType(GpuProgramType t); 01016 virtual GpuProgramType getType(void) const { return mType; } 01017 01022 virtual GpuProgram* _getBindingDelegate(void) { return this; } 01023 01025 virtual bool isSupported(void) const; 01026 01034 virtual GpuProgramParametersSharedPtr createParameters(void); 01035 01042 virtual void setSkeletalAnimationIncluded(bool included) 01043 { mSkeletalAnimation = included; } 01044 01051 virtual bool isSkeletalAnimationIncluded(void) const { return mSkeletalAnimation; } 01052 01059 virtual void setMorphAnimationIncluded(bool included) 01060 { mMorphAnimation = included; } 01061 01069 virtual void setPoseAnimationIncluded(ushort poseCount) 01070 { mPoseAnimation = poseCount; } 01071 01078 virtual bool isMorphAnimationIncluded(void) const { return mMorphAnimation; } 01079 01086 virtual bool isPoseAnimationIncluded(void) const { return mPoseAnimation > 0; } 01090 virtual ushort getNumberOfPosesIncluded(void) const { return mPoseAnimation; } 01091 01102 virtual GpuProgramParametersSharedPtr getDefaultParameters(void); 01103 01106 virtual bool hasDefaultParameters(void) const { return !mDefaultParams.isNull(); } 01107 01116 virtual void setSurfaceAndPassLightStates(bool state) 01117 { mPassSurfaceAndLightStates = state; } 01118 01122 virtual bool getPassSurfaceAndLightStates(void) const { return mPassSurfaceAndLightStates; } 01123 01127 virtual const String& getLanguage(void) const; 01128 01131 virtual bool hasCompileError(void) const { return mCompileError; } 01132 01135 virtual void resetCompileError(void) { mCompileError = false; } 01136 protected: 01138 virtual void loadFromSource(void) = 0; 01139 01140 }; 01141 01142 01149 class _OgreExport GpuProgramPtr : public SharedPtr<GpuProgram> 01150 { 01151 public: 01152 GpuProgramPtr() : SharedPtr<GpuProgram>() {} 01153 explicit GpuProgramPtr(GpuProgram* rep) : SharedPtr<GpuProgram>(rep) {} 01154 GpuProgramPtr(const GpuProgramPtr& r) : SharedPtr<GpuProgram>(r) {} 01155 GpuProgramPtr(const ResourcePtr& r) : SharedPtr<GpuProgram>() 01156 { 01157 // lock & copy other mutex pointer 01158 OGRE_MUTEX_CONDITIONAL(r.OGRE_AUTO_MUTEX_NAME) 01159 { 01160 OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME) 01161 OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME) 01162 pRep = static_cast<GpuProgram*>(r.getPointer()); 01163 pUseCount = r.useCountPointer(); 01164 if (pUseCount) 01165 { 01166 ++(*pUseCount); 01167 } 01168 } 01169 } 01170 01172 GpuProgramPtr& operator=(const ResourcePtr& r) 01173 { 01174 if (pRep == static_cast<GpuProgram*>(r.getPointer())) 01175 return *this; 01176 release(); 01177 // lock & copy other mutex pointer 01178 OGRE_MUTEX_CONDITIONAL(r.OGRE_AUTO_MUTEX_NAME) 01179 { 01180 OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME) 01181 OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME) 01182 pRep = static_cast<GpuProgram*>(r.getPointer()); 01183 pUseCount = r.useCountPointer(); 01184 if (pUseCount) 01185 { 01186 ++(*pUseCount); 01187 } 01188 } 01189 return *this; 01190 } 01192 GpuProgramPtr& operator=(const HighLevelGpuProgramPtr& r); 01193 }; 01194 } 01195 01196 #endif
Copyright © 2000-2005 by The OGRE Team
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Jan 21 10:01:36 2007