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-2014 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 _ShaderProgramFunction_ 28 #define _ShaderProgramFunction_ 29 30 #include "OgreShaderPrerequisites.h" 31 #include "OgreShaderParameter.h" 32 #include "OgreShaderFunctionAtom.h" 33 34 namespace Ogre { 35 namespace RTShader { 36 37 /** \addtogroup Optional 38 * @{ 39 */ 40 /** \addtogroup RTShader 41 * @{ 42 */ 43 44 /// represents a @ref FFPShaderStage, part of a Function 45 class _OgreRTSSExport FunctionStageRef 46 { 47 friend class Function; 48 public: 49 /** call a library function 50 * @param name the function name 51 */ 52 void callFunction(const char* name, const InOut& inout) const; 53 54 /// @overload 55 void callFunction(const char* name, const std::vector<Operand>& params) const; 56 /// @overload callFunction(const char * name,const In & arg,const Out & ret)57 void callFunction(const char* name, const In& arg, const Out& ret) const { callFunction(name, {arg, ret}); } 58 /// @overload callFunction(const char * name,const In & arg0,const In & arg1,const Out & ret)59 void callFunction(const char* name, const In& arg0, const In& arg1, const Out& ret) const 60 { 61 callFunction(name, {arg0, arg1, ret}); 62 } 63 64 /// dst = texture(sampler, texcoord); sampleTexture(const In & sampler,const In & texcoord,const Out & dst)65 void sampleTexture(const In& sampler, const In& texcoord, const Out& dst) const 66 { 67 sampleTexture({sampler, texcoord, dst}); 68 } 69 /// @overload 70 void sampleTexture(const std::vector<Operand>& params) const; 71 72 /// to = from; assign(const In & from,const Out & to)73 void assign(const In& from, const Out& to) const { assign({from, to}); } 74 /// @overload 75 void assign(const std::vector<Operand>& params) const; 76 private: 77 size_t mStage; 78 Function* mParent; FunctionStageRef(size_t stage,Function * parent)79 FunctionStageRef(size_t stage, Function* parent) : mStage(stage), mParent(parent) {} 80 }; 81 82 /** A class that represents a shader based program function. 83 */ 84 class _OgreRTSSExport Function : public RTShaderSystemAlloc 85 { 86 friend ProgramManager; 87 // Interface. 88 public: 89 enum FunctionType 90 { 91 // internal function (default) 92 FFT_INTERNAL, 93 // Vertex program main 94 FFT_VS_MAIN, 95 // Pixel shader main 96 FFT_PS_MAIN 97 }; 98 99 /** Get the name of this function */ getName()100 const String& getName() const { return mName; } 101 102 /** Get the description of this function */ getDescription()103 const String& getDescription() const { return mDescription; } 104 105 /// @deprecated 106 ParameterPtr resolveInputParameter(Parameter::Semantic semantic, int index, const Parameter::Content content, GpuConstantType type); 107 108 /** Resolve input parameter of this function 109 @param content The content of the parameter. 110 @param type The type of the desired parameter. 111 @return parameter instance in case of that resolve operation succeeded. 112 */ 113 ParameterPtr resolveInputParameter(Parameter::Content content, GpuConstantType type = GCT_UNKNOWN) 114 { 115 return resolveInputParameter(Parameter::SPS_UNKNOWN, 0, content, type); 116 } 117 118 /// resolve input parameter from previous output resolveInputParameter(const ParameterPtr & out)119 ParameterPtr resolveInputParameter(const ParameterPtr& out) 120 { 121 OgreAssert(out, "parameter must not be NULL"); 122 return resolveInputParameter(out->getSemantic(), out->getIndex(), out->getContent(), out->getType()); 123 } 124 125 /** 126 * get input parameter by content 127 * @param content 128 * @return parameter or NULL if not found 129 */ 130 ParameterPtr getInputParameter(Parameter::Content content, GpuConstantType type = GCT_UNKNOWN) 131 { 132 return _getParameterByContent(mInputParameters, content, type); 133 } 134 135 /// @deprecated 136 ParameterPtr resolveOutputParameter(Parameter::Semantic semantic, int index, const Parameter::Content content, GpuConstantType type); 137 138 /** Resolve output parameter of this function 139 @param content The content of the parameter. 140 @param type The type of the desired parameter. 141 @return parameter instance in case of that resolve operation succeeded. 142 */ 143 ParameterPtr resolveOutputParameter(Parameter::Content content, GpuConstantType type = GCT_UNKNOWN) 144 { 145 return resolveOutputParameter(Parameter::SPS_UNKNOWN, 0, content, type); 146 } 147 148 /** 149 * get output parameter by content 150 * @param content 151 * @return parameter or NULL if not found 152 */ 153 ParameterPtr getOutputParameter(Parameter::Content content, GpuConstantType type = GCT_UNKNOWN) 154 { 155 return _getParameterByContent(mOutputParameters, content, type); 156 } 157 158 /// @deprecated local parameters do not have index or sematic. use resolveLocalParameter(const String&, GpuConstantType) 159 ParameterPtr resolveLocalParameter(Parameter::Semantic semantic, int index, const String& name, GpuConstantType type); 160 161 /** Resolve local parameter of this function 162 @param name The name of the parameter. 163 @param type The type of the desired parameter. 164 Return parameter instance in case of that resolve operation succeeded. 165 */ resolveLocalParameter(const String & name,GpuConstantType type)166 ParameterPtr resolveLocalParameter(const String& name, GpuConstantType type) 167 { 168 return resolveLocalParameter(Parameter::SPS_UNKNOWN, 0, name, type); 169 } 170 171 /// @deprecated local parameters do not have index or sematic. use resolveLocalParameter(const String&, GpuConstantType) 172 ParameterPtr resolveLocalParameter(Parameter::Semantic semantic, int index, const Parameter::Content content, GpuConstantType type); 173 174 /** Resolve local parameter of this function 175 @param content The content of the parameter. 176 @param type The type of the desired parameter. 177 Return parameter instance in case of that resolve operation succeeded. 178 */ 179 ParameterPtr resolveLocalParameter(Parameter::Content content, GpuConstantType type = GCT_UNKNOWN) 180 { 181 return resolveLocalParameter(Parameter::SPS_UNKNOWN, 0,content, type); 182 } 183 184 /** 185 * get local parameter by content 186 * @param content 187 * @return parameter or NULL if not found 188 */ getLocalParameter(Parameter::Content content)189 ParameterPtr getLocalParameter(Parameter::Content content) 190 { 191 return _getParameterByContent(mLocalParameters, content, GCT_UNKNOWN); 192 } 193 /// @overload getLocalParameter(const String & name)194 ParameterPtr getLocalParameter(const String& name) 195 { 196 return _getParameterByName(mLocalParameters, name); 197 } 198 199 /// @deprecated do not use getParameterByName(const ShaderParameterList & parameterList,const String & name)200 OGRE_DEPRECATED static ParameterPtr getParameterByName(const ShaderParameterList& parameterList, const String& name) 201 { 202 return _getParameterByName(parameterList, name); 203 } 204 /// @deprecated do not use getParameterBySemantic(const ShaderParameterList & parameterList,const Parameter::Semantic semantic,int index)205 OGRE_DEPRECATED static ParameterPtr getParameterBySemantic(const ShaderParameterList& parameterList, const Parameter::Semantic semantic, int index) 206 { 207 return _getParameterBySemantic(parameterList, semantic, index); 208 } 209 /// @deprecated use getInputParameter / getOutputParameter / getLocalParameter instead getParameterByContent(const ShaderParameterList & parameterList,const Parameter::Content content,GpuConstantType type)210 OGRE_DEPRECATED static ParameterPtr getParameterByContent(const ShaderParameterList& parameterList, const Parameter::Content content, GpuConstantType type) 211 { 212 return _getParameterByContent(parameterList, content, type); 213 } 214 215 /** Return a list of input parameters. */ getInputParameters()216 const ShaderParameterList& getInputParameters() const { return mInputParameters; } 217 218 /** Return a list of output parameters. */ getOutputParameters()219 const ShaderParameterList& getOutputParameters() const { return mOutputParameters; } 220 221 /** Return a list of local parameters. */ getLocalParameters()222 const ShaderParameterList& getLocalParameters() const { return mLocalParameters; } 223 224 /** Add a function atom instance to this function. 225 @param atomInstance The atom instance to add. 226 */ 227 void addAtomInstance(FunctionAtom* atomInstance); 228 229 /// @deprecated use FunctionStageRef::assign instead 230 OGRE_DEPRECATED void addAtomAssign(ParameterPtr lhs, ParameterPtr rhs, int groupOrder); 231 232 /// get a @ref FFPShaderStage of this function getStage(size_t s)233 FunctionStageRef getStage(size_t s) 234 { 235 return FunctionStageRef(s, this); 236 } 237 238 /** Delete a function atom instance from this function. 239 @param atomInstance The atom instance to OGRE_DELETE. 240 */ 241 bool deleteAtomInstance(FunctionAtom* atomInstance); 242 243 /** Return list of atom instances composing this function. (Const version) */ 244 const FunctionAtomInstanceList& getAtomInstances(); 245 246 /** Add input parameter to this function. */ 247 void addInputParameter(ParameterPtr parameter); 248 249 /** Add output parameter to this function. */ 250 void addOutputParameter(ParameterPtr parameter); 251 252 /** Delete input parameter from this function. */ 253 void deleteInputParameter(ParameterPtr parameter); 254 255 /** Delete output parameter from this function. */ 256 void deleteOutputParameter(ParameterPtr parameter); 257 258 /** Delete all input parameters from this function. */ 259 void deleteAllInputParameters(); 260 261 /** Delete all output parameters from this function. */ 262 void deleteAllOutputParameters(); 263 264 /** get function type. */ 265 FunctionType getFunctionType() const; 266 267 268 protected: 269 270 static ParameterPtr _getParameterByName(const ShaderParameterList& parameterList, const String& name); 271 static ParameterPtr _getParameterBySemantic(const ShaderParameterList& parameterList, const Parameter::Semantic semantic, int index); 272 static ParameterPtr _getParameterByContent(const ShaderParameterList& parameterList, const Parameter::Content content, GpuConstantType type); 273 274 275 /** Class constructor. 276 @param name The name of this function. 277 @param desc The description of this function. 278 @remarks This class is allocated via an instance of Program class. 279 */ 280 Function(const String& name, const String& desc, const FunctionType functionType); 281 282 /** Class destructor */ 283 ~Function(); 284 285 /** Add parameter to given list */ 286 void addParameter(ShaderParameterList& parameterList, ParameterPtr parameter); 287 288 /** Delete parameter from a given list */ 289 void deleteParameter(ShaderParameterList& parameterList, ParameterPtr parameter); 290 291 protected: 292 // Function name. 293 String mName; 294 // Function description. 295 String mDescription; 296 // Input parameters. 297 ShaderParameterList mInputParameters; 298 // Output parameters. 299 ShaderParameterList mOutputParameters; 300 // Local parameters. 301 ShaderParameterList mLocalParameters; 302 // Atom instances composing this function. 303 std::map<size_t, FunctionAtomInstanceList> mAtomInstances; 304 FunctionAtomInstanceList mSortedAtomInstances; 305 // Function type 306 FunctionType mFunctionType; 307 308 private: 309 friend class Program; 310 }; 311 312 typedef std::vector<Function*> ShaderFunctionList; 313 typedef ShaderFunctionList::iterator ShaderFunctionIterator; 314 typedef ShaderFunctionList::const_iterator ShaderFunctionConstIterator; 315 316 /** @} */ 317 /** @} */ 318 319 } 320 } 321 322 #endif 323