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 _ShaderProgram_
28 #define _ShaderProgram_
29 
30 #include "OgreShaderPrerequisites.h"
31 #include "OgreGpuProgram.h"
32 #include "OgreSingleton.h"
33 #include "OgreShaderFunction.h"
34 #include "OgreStringVector.h"
35 
36 namespace Ogre {
37 namespace RTShader {
38 
39 /** \addtogroup Optional
40 *  @{
41 */
42 /** \addtogroup RTShader
43 *  @{
44 */
45 
46 /** A class that represents a shader based program.
47 */
48 class _OgreRTSSExport Program : public RTShaderSystemAlloc
49 {
50 
51 // Interface.
52 public:
53     /** Get the type of this program. */
54     GpuProgramType getType() const;
55 
56     /** Resolve uniform auto constant parameter with associated real data of this program.
57     @param autoType The auto type of the desired parameter.
58     @param data The data to associate with the auto parameter.
59     @param size number of elements in the parameter.
60     @return parameter instance in case of that resolve operation succeeded.
61     */
62     UniformParameterPtr resolveAutoParameterReal(GpuProgramParameters::AutoConstantType autoType, Real data, size_t size = 0);
63 
64     /** Resolve uniform auto constant parameter with associated real data of this program.
65     @param autoType The auto type of the desired parameter.
66     @param type The desired data type of the auto parameter.
67     @param data The data to associate with the auto parameter.
68     @param size number of elements in the parameter.
69     @return parameter instance in case of that resolve operation succeeded.
70     */
71     UniformParameterPtr resolveAutoParameterReal(GpuProgramParameters::AutoConstantType autoType, GpuConstantType type, Real data, size_t size = 0);
72 
73     /// @deprecated use resolveParameter(GpuProgramParameters::AutoConstantType, size_t)
74     OGRE_DEPRECATED UniformParameterPtr resolveAutoParameterInt(GpuProgramParameters::AutoConstantType autoType, size_t data, size_t size = 0);
75 
76     /** Resolve uniform auto constant parameter with associated int data of this program.
77     @param autoType The auto type of the desired parameter.
78     @param type The desired data type of the auto parameter.
79     @param data The data to associate with the auto parameter.
80     @param size number of elements in the parameter.
81     @return parameter instance in case of that resolve operation succeeded.
82     */
83     UniformParameterPtr resolveAutoParameterInt(GpuProgramParameters::AutoConstantType autoType, GpuConstantType type, size_t data, size_t size = 0);
84 
85     /** Resolve uniform parameter of this program.
86     @param type The type of the desired parameter.
87     @param index The index of the desired parameter.
88     @param suggestedName The suggested name for the parameter in case new one should be create.
89     @param variability How this parameter varies (bitwise combination of GpuProgramVariability).
90     @param size number of elements in the parameter.
91     @return parameter instance in case of that resolve operation succeeded.
92     @remarks Pass -1 as index parameter to create a new parameter with the desired type and index.
93     */
94     UniformParameterPtr resolveParameter(GpuConstantType type, int index, uint16 variability, const String& suggestedName, size_t size = 0);
95 
96     /** Resolve uniform auto constant parameter
97     @param autoType The auto type of the desired parameter
98     @param data The data to associate with the auto parameter.
99     @return parameter instance in case of that resolve operation succeeded.
100     */
101     UniformParameterPtr resolveParameter(GpuProgramParameters::AutoConstantType autoType, size_t data = 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     @param functionType
130     */
131     Function* createFunction(const String& name, const String& desc, const Function::FunctionType functionType);
132 
133     /** Get a function by a given name. Return NULL if no matching function found.
134     @param name The name of the function to search for.
135     */
136     Function* getFunctionByName(const String& name);
137 
138     /** Get the function list of this program.
139     */
getFunctions()140     const ShaderFunctionList& getFunctions() const { return mFunctions; };
141 
142     /** Set the entry point function.
143     @param function The function that will use as entry point of this program.
144     */
setEntryPointFunction(Function * function)145     void setEntryPointFunction(Function* function) { mEntryPointFunction = function; }
146 
147     /** Get the entry point function of this program.*/
getEntryPointFunction()148     Function* getEntryPointFunction()                    { return mEntryPointFunction; }
149 
150     /** Add dependency for this program. Basically a filename that will be included in this
151     program and provide predefined shader functions code.
152     One should verify that the given library file he provides can be reached by the resource manager.
153     This step can be achieved using the ResourceGroupManager::addResourceLocation method.
154     */
155     void addDependency(const String& libFileName);
156 
157     /** Get the number of external libs this program depends on */
158     size_t getDependencyCount() const;
159 
160     /** Get the library name of the given index dependency.
161     @param index The index of the dependecy.
162     */
163     const String& getDependency(unsigned int index) const;
164 
165 
166     /** Sets whether a vertex program includes the required instructions
167         to perform skeletal animation.
168     */
setSkeletalAnimationIncluded(bool value)169     void setSkeletalAnimationIncluded(bool value) { mSkeletalAnimation = value; }
170 
171     /** Returns whether a vertex program includes the required instructions
172         to perform skeletal animation.
173     */
getSkeletalAnimationIncluded()174     bool getSkeletalAnimationIncluded() const { return mSkeletalAnimation; }
175 
176     /** Tells Ogre whether auto-bound matrices should be sent in column or row-major order.
177     @remarks
178         This method has the same effect as column_major_matrices option used when declaring manually written hlsl program.
179         You want to use this method only when you use float3x4 type in a shader, e.g. for bone matrices.
180         In mentioned case you should call this method with false as parameter.
181     @par
182         For more detail see OGRE Manual, section <b>3.1.6 DirectX9 HLSL</b>.
183     @note
184         This setting has any effect only when the target language is HLSL.
185     @param value Should Ogre pass auto-bound matrices as column-major? The default is true.
186     */
setUseColumnMajorMatrices(bool value)187     void setUseColumnMajorMatrices(bool value) { mColumnMajorMatrices = value; }
188 
189     /** Returns whether Ogre will pass auto-bound matrices as column-major.
190     @return
191     true, when the matrices will be passed in column-major order, false, when they will be passed as row-major.
192     */
getUseColumnMajorMatrices()193     bool getUseColumnMajorMatrices() const { return mColumnMajorMatrices; }
194 
195     void addPreprocessorDefines(const String& defines);
196 
getPreprocessorDefines()197     const String& getPreprocessorDefines() const { return mPreprocessorDefines; }
198 
199 // Protected methods.
200 protected:
201 
202     /** Class constructor.
203     @param type The type of this program.
204     */
205     Program(GpuProgramType type);
206 
207     /** Class destructor */
208     ~Program();
209 
210     /** Destroy all parameters of this program. */
211     void destroyParameters();
212 
213     /** Destroy all functions of this program. */
214     void destroyFunctions();
215 
216     /** Add parameter to this program. */
217     void addParameter(UniformParameterPtr parameter);
218 
219     /** Remove parameter from this program. */
220     void removeParameter(UniformParameterPtr parameter);
221 // Attributes.
222 protected:
223     // Program type. (Vertex, Fragment, Geometry).
224     GpuProgramType mType;
225     // Program uniform parameters.
226     UniformParameterList mParameters;
227     // Function list.
228     ShaderFunctionList mFunctions;
229     // Entry point function for this program.
230     Function* mEntryPointFunction;
231     // Program dependencies.
232     StringVector mDependencies;
233     /// preprocessor definitions
234     String mPreprocessorDefines;
235     // Skeletal animation calculation
236     bool mSkeletalAnimation;
237     // Whether to pass matrices as column-major.
238     bool mColumnMajorMatrices;
239 private:
240     friend class ProgramManager;
241 };
242 
243 /** @} */
244 /** @} */
245 
246 }
247 }
248 
249 #endif
250 
251