/src/ogre/Components/RTShaderSystem/include/OgreShaderFunctionAtom.h
Line | Count | Source (jump to first uncovered line) |
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 _ShaderFunctionAtom_ |
28 | | #define _ShaderFunctionAtom_ |
29 | | |
30 | | #include "OgreShaderPrerequisites.h" |
31 | | #include "OgreStringVector.h" |
32 | | #include "OgreGpuProgramParams.h" |
33 | | #include "OgreShaderParameter.h" |
34 | | |
35 | | namespace Ogre { |
36 | | namespace RTShader { |
37 | | |
38 | | /** \addtogroup Optional |
39 | | * @{ |
40 | | */ |
41 | | /** \addtogroup RTShader |
42 | | * @{ |
43 | | */ |
44 | | |
45 | | /** A class that represents a function operand (its the combination of a parameter the in/out semantic and the used fields) |
46 | | */ |
47 | | class _OgreRTSSExport Operand : public RTShaderSystemAlloc |
48 | | { |
49 | | public: |
50 | | |
51 | | // InOut semantic |
52 | | enum OpSemantic |
53 | | { |
54 | | /// The parameter is a input parameter |
55 | | OPS_IN, |
56 | | /// The parameter is a output parameter |
57 | | OPS_OUT, |
58 | | /// The parameter is a input/output parameter |
59 | | OPS_INOUT |
60 | | }; |
61 | | |
62 | | // Used field mask |
63 | | enum OpMask : uchar |
64 | | { |
65 | | OPM_NONE = 0, |
66 | | OPM_X = 0x0001, |
67 | | OPM_Y = 0x0002, |
68 | | OPM_Z = 0x0004, |
69 | | OPM_W = 0x0008, |
70 | | OPM_XY = OPM_X | OPM_Y, |
71 | | OPM_XZ = OPM_X | OPM_Z, |
72 | | OPM_XW = OPM_X | OPM_W, |
73 | | OPM_YZ = OPM_Y | OPM_Z, |
74 | | OPM_YW = OPM_Y | OPM_W, |
75 | | OPM_ZW = OPM_Z | OPM_W, |
76 | | OPM_XYZ = OPM_X | OPM_Y | OPM_Z, |
77 | | OPM_XYW = OPM_X | OPM_Y | OPM_W, |
78 | | OPM_XZW = OPM_X | OPM_Z | OPM_W, |
79 | | OPM_YZW = OPM_Y | OPM_Z | OPM_W, |
80 | | OPM_XYZW = OPM_X | OPM_Y | OPM_Z | OPM_W, |
81 | | OPM_ALL = OPM_XYZW |
82 | | }; |
83 | | |
84 | | /** Class constructor |
85 | | @param parameter A function parameter. |
86 | | @param opSemantic The in/out semantic of the parameter. |
87 | | @param opMask The field mask of the parameter. |
88 | | @param indirectionLevel |
89 | | */ |
90 | | Operand(ParameterPtr parameter, OpSemantic opSemantic, OpMask opMask = OPM_ALL, ushort indirectionLevel = 0); |
91 | | |
92 | | /** Copy constructor */ |
93 | | Operand(const Operand& rhs); |
94 | | |
95 | | /** Copy the given Operand to this Operand. |
96 | | @param rhs The other Operand to copy to this state. |
97 | | */ |
98 | | Operand& operator= (const Operand & rhs); |
99 | | |
100 | | /** Class destructor */ |
101 | | ~Operand(); |
102 | | |
103 | | /** Returns the parameter object as weak reference */ |
104 | 0 | const ParameterPtr& getParameter() const { return mParameter; } |
105 | | |
106 | | /** Returns true if not all fields used. (usage is described through semantic)*/ |
107 | 0 | bool hasFreeFields() const { return mMask != OPM_ALL; } |
108 | | |
109 | | /** Returns the mask bitfield. */ |
110 | 0 | OpMask getMask() const { return mMask; } |
111 | | |
112 | 0 | Operand& x() { return mask(OPM_X); } |
113 | 0 | Operand& y() { return mask(OPM_Y); } |
114 | 0 | Operand& z() { return mask(OPM_Z); } |
115 | 0 | Operand& w() { return mask(OPM_W); } |
116 | 0 | Operand& xy() { return mask(OPM_XY); } |
117 | 0 | Operand& xyz() { return mask(OPM_XYZ); } |
118 | | |
119 | | Operand& mask(OpMask opMask) |
120 | 0 | { |
121 | 0 | mMask = opMask; |
122 | 0 | return *this; |
123 | 0 | } |
124 | | |
125 | | /// automatically set swizzle to match parameter arity |
126 | | void setMaskToParamType(); |
127 | | |
128 | | /** Returns the operand semantic (do we read/write or both with the parameter). */ |
129 | 0 | OpSemantic getSemantic() const { return mSemantic; } |
130 | | |
131 | | /** Returns the level of indirection. |
132 | | The greater the indirection level the more the parameter needs to be nested in brackets. |
133 | | For example given 4 parameters x1...x4 with the indirections levels 0,1,1,2 |
134 | | respectively. The parameters should form the following string: x1[x2][x3[x4]]. |
135 | | */ |
136 | 0 | ushort getIndirectionLevel() const { return mIndirectionLevel; } |
137 | | |
138 | | /** write the parameter name and the usage mask like this 'color.xyz' */ |
139 | | void write(std::ostream& os) const; |
140 | | |
141 | | /** Return the float count of the given mask. */ |
142 | | static int getFloatCount(int mask); |
143 | | protected: |
144 | | /// The parameter being carried by the operand |
145 | | ParameterPtr mParameter; |
146 | | /// Tells if the parameter is of type input,output or both |
147 | | OpSemantic mSemantic; |
148 | | /// Which part of the parameter should be passed (x,y,z,w) |
149 | | OpMask mMask; |
150 | | /// The level of indirection. @see getIndirectionLevel |
151 | | ushort mIndirectionLevel; |
152 | | }; |
153 | | |
154 | | struct _OgreRTSSExport In : Operand |
155 | | { |
156 | 0 | In(const Operand& rhs) : Operand(rhs) { OgreAssert(mSemantic == OPS_IN, "invalid semantic"); } |
157 | 0 | In(ParameterPtr p) : Operand(p, OPS_IN) {} |
158 | 0 | In(UniformParameterPtr p) : Operand(p, OPS_IN) {} |
159 | | |
160 | | // implicitly construct const params |
161 | 0 | In(float f) : Operand(ParameterFactory::createConstParam(f), OPS_IN) {} |
162 | 0 | In(const Vector2& v) : Operand(ParameterFactory::createConstParam(v), OPS_IN) {} |
163 | 0 | In(const Vector3& v) : Operand(ParameterFactory::createConstParam(v), OPS_IN) {} |
164 | 0 | In(const Vector4& v) : Operand(ParameterFactory::createConstParam(v), OPS_IN) {} |
165 | | }; |
166 | | |
167 | | struct _OgreRTSSExport Out : Operand |
168 | | { |
169 | 0 | Out(const Operand& rhs) : Operand(rhs) { OgreAssert(mSemantic == OPS_OUT, "invalid semantic"); } |
170 | 0 | Out(ParameterPtr p) : Operand(p, OPS_OUT) {} |
171 | 0 | Out(UniformParameterPtr p) : Operand(p, OPS_OUT) {} |
172 | | }; |
173 | | |
174 | | struct _OgreRTSSExport InOut : Operand |
175 | | { |
176 | 0 | InOut(const Operand& rhs) : Operand(rhs) { OgreAssert(mSemantic == OPS_INOUT, "invalid semantic"); } |
177 | 0 | InOut(ParameterPtr p) : Operand(p, OPS_INOUT) {} |
178 | 0 | InOut(UniformParameterPtr p) : Operand(p, OPS_INOUT) {} |
179 | | }; |
180 | | |
181 | | /// shorthand for operator[] on preceding operand. e.g. myArray[p] |
182 | | struct _OgreRTSSExport At : Operand |
183 | | { |
184 | 0 | At(ParameterPtr p) : Operand(p, OPS_IN, OPM_ALL, 1) {} |
185 | 0 | At(int f) : Operand(ParameterFactory::createConstParam(static_cast<float>(f)), OPS_IN, OPM_ALL, 1) {} |
186 | | }; |
187 | | |
188 | | /** A class that represents an atomic code section of shader based program function. |
189 | | */ |
190 | | class _OgreRTSSExport FunctionAtom : public RTShaderSystemAlloc |
191 | | { |
192 | | // Interface. |
193 | | public: |
194 | | typedef std::vector<Operand> OperandVector; |
195 | | |
196 | | /** Class default destructor. */ |
197 | 0 | virtual ~FunctionAtom() {} |
198 | | |
199 | | /** Get the group execution order of this function atom. */ |
200 | | int getGroupExecutionOrder() const; |
201 | | |
202 | | /** Get a list of parameters this function invocation will use in the function call as arguments. */ |
203 | 0 | OperandVector& getOperandList() { return mOperands; } |
204 | | |
205 | | /** Push a new operand (on the end) to the function. |
206 | | @param parameter A function parameter. |
207 | | @param opSemantic The in/out semantic of the parameter. |
208 | | @param opMask The field mask of the parameter. |
209 | | @param indirectionLevel The level of nesting inside brackets |
210 | | */ |
211 | | void pushOperand(ParameterPtr parameter, Operand::OpSemantic opSemantic, Operand::OpMask opMask = Operand::OPM_ALL, int indirectionLevel = 0); |
212 | | |
213 | | void setOperands(const OperandVector& ops); |
214 | | |
215 | | /** Abstract method that writes a source code to the given output stream in the target shader language. */ |
216 | | virtual void writeSourceCode(std::ostream& os, const String& targetLanguage) const = 0; |
217 | | |
218 | | // Attributes. |
219 | | protected: |
220 | | /** Class default constructor. */ |
221 | | FunctionAtom(); |
222 | | |
223 | | void writeOperands(std::ostream& os, OperandVector::const_iterator begin, OperandVector::const_iterator end) const; |
224 | | |
225 | | // The owner group execution order. |
226 | | int mGroupExecutionOrder; |
227 | | OperandVector mOperands; |
228 | | String mFunctionName; |
229 | | }; |
230 | | |
231 | | /** A class that represents function invocation code from shader based program function. |
232 | | */ |
233 | | class _OgreRTSSExport FunctionInvocation : public FunctionAtom |
234 | | { |
235 | | // Interface. |
236 | | public: |
237 | | /** Class constructor |
238 | | @param functionName The name of the function to invoke. |
239 | | @param groupOrder The group order of this invocation. |
240 | | @param returnType The return type of the used function. |
241 | | */ |
242 | | FunctionInvocation(const String& functionName, int groupOrder, const String& returnType = "void"); |
243 | | |
244 | | /** Copy constructor */ |
245 | | FunctionInvocation(const FunctionInvocation& rhs); |
246 | | |
247 | | /** |
248 | | @see FunctionAtom::writeSourceCode |
249 | | */ |
250 | | void writeSourceCode(std::ostream& os, const String& targetLanguage) const override; |
251 | | |
252 | | /** Return the function name */ |
253 | 0 | const String& getFunctionName() const { return mFunctionName; } |
254 | | |
255 | | /** Return the return type */ |
256 | 0 | const String& getReturnType() const { return mReturnType; } |
257 | | |
258 | | /** Determines if the current object is equal to the compared one. */ |
259 | | bool operator == ( const FunctionInvocation& rhs ) const; |
260 | | |
261 | | /** Determines if the current object is not equal to the compared one. */ |
262 | | bool operator != ( const FunctionInvocation& rhs ) const; |
263 | | |
264 | | /** Determines if the current object is less than the compared one. */ |
265 | | bool operator < ( const FunctionInvocation& rhs ) const; |
266 | | |
267 | | /** Comparator function to be used for sorting. |
268 | | Implemented as a struct to make it easier for the compiler to inline |
269 | | */ |
270 | | struct FunctionInvocationLessThan |
271 | | { |
272 | | bool operator()(FunctionInvocation const& lhs, FunctionInvocation const& rhs) const; |
273 | | }; |
274 | | |
275 | | /** Comparator function to be used for comparisons. |
276 | | Implemented as a struct to make it easier for the compiler to inline |
277 | | */ |
278 | | struct FunctionInvocationCompare |
279 | | { |
280 | | bool operator()(FunctionInvocation const& lhs, FunctionInvocation const& rhs) const; |
281 | | }; |
282 | | |
283 | | private: |
284 | 0 | FunctionInvocation() {} |
285 | | |
286 | | String mReturnType; |
287 | | }; |
288 | | |
289 | | /// shorthand for "lhs = rhs;" insted of using FFP_Assign(rhs, lhs) |
290 | | class _OgreRTSSExport AssignmentAtom : public FunctionAtom |
291 | | { |
292 | | public: |
293 | 0 | explicit AssignmentAtom(int groupOrder) { mGroupExecutionOrder = groupOrder; } |
294 | | /// @note the argument order is reversed comered to all other function invocations |
295 | | AssignmentAtom(const Out& lhs, const In& rhs, int groupOrder); |
296 | | void writeSourceCode(std::ostream& os, const String& targetLanguage) const override; |
297 | | }; |
298 | | |
299 | | /// shorthand for "dst = texture(sampler, uv);" instead of using FFP_SampleTexture |
300 | | class _OgreRTSSExport SampleTextureAtom : public FunctionAtom |
301 | | { |
302 | | public: |
303 | 0 | explicit SampleTextureAtom(int groupOrder) { mGroupExecutionOrder = groupOrder; } |
304 | | SampleTextureAtom(const In& sampler, const In& texcoord, const Out& dst, int groupOrder); |
305 | | void writeSourceCode(std::ostream& os, const String& targetLanguage) const override; |
306 | | }; |
307 | | |
308 | | /// shorthand for "dst = a OP b;" |
309 | | class _OgreRTSSExport BinaryOpAtom : public FunctionAtom |
310 | | { |
311 | | char mOp; |
312 | | public: |
313 | 0 | BinaryOpAtom(char op, int groupOrder) : mOp(op) { mGroupExecutionOrder = groupOrder; } |
314 | | BinaryOpAtom(char op, const In& a, const In& b, const Out& dst, int groupOrder); |
315 | | void writeSourceCode(std::ostream& os, const String& targetLanguage) const override; |
316 | | }; |
317 | | |
318 | | /// shorthand for "dst = BUILTIN(args);" |
319 | | class _OgreRTSSExport BuiltinFunctionAtom : public FunctionAtom |
320 | | { |
321 | | public: |
322 | | BuiltinFunctionAtom(const char* builtin, int groupOrder) |
323 | 0 | { |
324 | 0 | mFunctionName = builtin; |
325 | 0 | mGroupExecutionOrder = groupOrder; |
326 | 0 | } |
327 | | void writeSourceCode(std::ostream& os, const String& targetLanguage) const override; |
328 | | }; |
329 | | |
330 | | typedef std::vector<FunctionAtom*> FunctionAtomInstanceList; |
331 | | typedef FunctionAtomInstanceList::iterator FunctionAtomInstanceIterator; |
332 | | typedef FunctionAtomInstanceList::const_iterator FunctionAtomInstanceConstIterator; |
333 | | |
334 | | /** @} */ |
335 | | /** @} */ |
336 | | |
337 | | } |
338 | | } |
339 | | |
340 | | #endif |