1 /* 2 ----------------------------------------------------------------------------- 3 This source file is part of OGRE 4 (Object-oriented Graphics Rendering Engine) 5 For the latest info, see http://www.ogre3d.org 6 7 Copyright (c) 2000-2013 Torus Knot Software Ltd 8 Permission is hereby granted, free of charge, to any person obtaining a copy 9 of this software and associated documentation files (the "Software"), to deal 10 in the Software without restriction, including without limitation the rights 11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 copies of the Software, and to permit persons to whom the Software is 13 furnished to do so, subject to the following conditions: 14 15 The above copyright notice and this permission notice shall be included in 16 all copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 THE SOFTWARE. 25 ----------------------------------------------------------------------------- 26 */ 27 #ifndef _ShaderProgram_ 28 #define _ShaderProgram_ 29 30 #include "OgreShaderPrerequisites.h" 31 #include "OgreGpuProgram.h" 32 #include "OgreSingleton.h" 33 #include "OgreShaderParameter.h" 34 #include "OgreShaderFunction.h" 35 #include "OgreShaderFunctionAtom.h" 36 #include "OgreStringVector.h" 37 38 namespace Ogre { 39 namespace RTShader { 40 41 /** \addtogroup Core 42 * @{ 43 */ 44 /** \addtogroup RTShader 45 * @{ 46 */ 47 48 /** A class that represents a shader based program. 49 */ 50 class _OgreRTSSExport Program : public RTShaderSystemAlloc 51 { 52 53 // Interface. 54 public: 55 /** Get the type of this program. */ 56 GpuProgramType getType() const; 57 58 /** Resolve uniform auto constant parameter with associated real data of this program. 59 @param autoType The auto type of the desired parameter. 60 @param data The data to associate with the auto parameter. 61 @param size number of elements in the parameter. 62 Return parameter instance in case of that resolve operation succeeded. 63 */ 64 UniformParameterPtr resolveAutoParameterReal(GpuProgramParameters::AutoConstantType autoType, Real data, size_t size = 0); 65 66 /** Resolve uniform auto constant parameter with associated real data of this program. 67 @param autoType The auto type of the desired parameter. 68 @param type The desired data type of the auto parameter. 69 @param data The data to associate with the auto parameter. 70 @param size number of elements in the parameter. 71 Return parameter instance in case of that resolve operation succeeded. 72 */ 73 UniformParameterPtr resolveAutoParameterReal(GpuProgramParameters::AutoConstantType autoType, GpuConstantType type, Real data, size_t size = 0); 74 75 /** Resolve uniform auto constant parameter with associated int data of this program. 76 @param autoType The auto type of the desired parameter. 77 @param data The data to associate with the auto parameter. 78 @param size number of elements in the parameter. 79 Return parameter instance in case of that resolve operation succeeded. 80 */ 81 UniformParameterPtr resolveAutoParameterInt(GpuProgramParameters::AutoConstantType autoType, size_t data, size_t size = 0); 82 83 /** Resolve uniform auto constant parameter with associated int data of this program. 84 @param autoType The auto type of the desired parameter. 85 @param type The desired data type of the auto parameter. 86 @param data The data to associate with the auto parameter. 87 @param size number of elements in the parameter. 88 Return parameter instance in case of that resolve operation succeeded. 89 */ 90 UniformParameterPtr resolveAutoParameterInt(GpuProgramParameters::AutoConstantType autoType, GpuConstantType type, size_t data, size_t size = 0); 91 92 /** Resolve uniform parameter of this program. 93 @param type The type of the desired parameter. 94 @param index The index of the desired parameter. 95 @param suggestedName The suggested name for the parameter in case new one should be create. 96 @param variability How this parameter varies (bitwise combination of GpuProgramVariability). 97 @param size number of elements in the parameter. 98 Return parameter instance in case of that resolve operation succeeded. 99 @remarks Pass -1 as index parameter to create a new parameter with the desired type and index. 100 */ 101 UniformParameterPtr resolveParameter(GpuConstantType type, int index, uint16 variability, const String& suggestedName, size_t size = 0); 102 103 /** Get parameter by a given name. 104 @param name The name of the parameter to search for. 105 @remarks Return NULL if no matching parameter found. 106 */ 107 UniformParameterPtr getParameterByName(const String& name); 108 109 /** Get parameter by a given auto constant type. 110 @param autoType The auto type of the parameter to search for. 111 @remarks Return NULL if no matching parameter found. 112 */ 113 UniformParameterPtr getParameterByAutoType(GpuProgramParameters::AutoConstantType autoType); 114 115 /** Get parameter by a given type and index. 116 @param type The type of the parameter to search for. 117 @param index The index of the parameter to search for. 118 @remarks Return NULL if no matching parameter found. 119 */ 120 UniformParameterPtr getParameterByType(GpuConstantType type, int index); 121 122 /** Get the list of uniform parameters of this program. 123 */ getParameters()124 const UniformParameterList& getParameters() const { return mParameters; }; 125 126 /** Create new function in this program. Return the newly created function instance. 127 @param name The name of the function to create. 128 @param desc The description of the function. 129 */ 130 Function* createFunction(const String& name, const String& desc, const Function::FunctionType functionType); 131 132 /** Get a function by a given name. Return NULL if no matching function found. 133 @param name The name of the function to search for. 134 */ 135 Function* getFunctionByName(const String& name); 136 137 /** Get the function list of this program. 138 */ getFunctions()139 const ShaderFunctionList& getFunctions() const { return mFunctions; }; 140 141 /** Set the entry point function. 142 @param function The function that will use as entry point of this program. 143 */ setEntryPointFunction(Function * function)144 void setEntryPointFunction(Function* function) { mEntryPointFunction = function; } 145 146 /** Get the entry point function of this program.*/ getEntryPointFunction()147 Function* getEntryPointFunction() { return mEntryPointFunction; } 148 149 /** Add dependency for this program. Basically a filename that will be included in this 150 program and provide predefined shader functions code. 151 One should verify that the given library file he provides can be reached by the resource manager. 152 This step can be achieved using the ResourceGroupManager::addResourceLocation method. 153 */ 154 void addDependency(const String& libFileName); 155 156 /** Get the number of external libs this program depends on */ 157 size_t getDependencyCount() const; 158 159 /** Get the library name of the given index dependency. 160 @param index The index of the dependecy. 161 */ 162 const String& getDependency(unsigned int index) const; 163 164 165 /** Sets whether a vertex program includes the required instructions 166 to perform skeletal animation. 167 */ setSkeletalAnimationIncluded(bool value)168 void setSkeletalAnimationIncluded(bool value) { mSkeletalAnimation = value; } 169 170 /** Returns whether a vertex program includes the required instructions 171 to perform skeletal animation. 172 */ getSkeletalAnimationIncluded()173 bool getSkeletalAnimationIncluded() const { return mSkeletalAnimation; } 174 175 /** Tells Ogre whether auto-bound matrices should be sent in column or row-major order. 176 @remarks 177 This method has the same effect as column_major_matrices option used when declaring manually written hlsl program. 178 You want to use this method only when you use float3x4 type in a shader, e.g. for bone matrices. 179 In mentioned case you should call this method with false as parameter. 180 @par 181 For more detail see OGRE Manual, section <b>3.1.6 DirectX9 HLSL</b>. 182 @note 183 This setting has any effect only when the target language is HLSL. 184 @param value Should Ogre pass auto-bound matrices as column-major? The default is true. 185 */ setUseColumnMajorMatrices(bool value)186 void setUseColumnMajorMatrices(bool value) { mColumnMajorMatrices = value; } 187 188 /** Returns whether Ogre will pass auto-bound matrices as column-major. 189 @return 190 true, when the matrices will be passed in column-major order, false, when they will be passed as row-major. 191 */ getUseColumnMajorMatrices()192 bool getUseColumnMajorMatrices() const { return mColumnMajorMatrices; } 193 194 // Protected methods. 195 protected: 196 197 /** Class constructor. 198 @param type The type of this program. 199 */ 200 Program(GpuProgramType type); 201 202 /** Class destructor */ 203 ~Program(); 204 205 /** Destroy all parameters of this program. */ 206 void destroyParameters(); 207 208 /** Destroy all functions of this program. */ 209 void destroyFunctions(); 210 211 /** Add parameter to this program. */ 212 void addParameter(UniformParameterPtr parameter); 213 214 /** Remove parameter from this program. */ 215 void removeParameter(UniformParameterPtr parameter); 216 217 218 // Attributes. 219 protected: 220 // Program type. (Vertex, Fragment, Geometry). 221 GpuProgramType mType; 222 // Program uniform parameters. 223 UniformParameterList mParameters; 224 // Function list. 225 ShaderFunctionList mFunctions; 226 // Entry point function for this program. 227 Function* mEntryPointFunction; 228 // Program dependencies. 229 StringVector mDependencies; 230 // Skeletal animation calculation 231 bool mSkeletalAnimation; 232 // Whether to pass matrices as column-major. 233 bool mColumnMajorMatrices; 234 private: 235 friend class ProgramManager; 236 }; 237 238 /** @} */ 239 /** @} */ 240 241 } 242 } 243 244 #endif 245 246