Coverage Report

Created: 2025-07-18 07:08

/src/ogre/Components/RTShaderSystem/include/OgreShaderFunction.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 _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
     * @param inout function argument
52
     */
53
    void callFunction(const char* name, const InOut& inout) const;
54
55
    /// @overload
56
    void callFunction(const char* name, const std::vector<Operand>& params) const;
57
    /// @overload
58
0
    void callFunction(const char* name, const In& arg, const Out& ret) const { callFunction(name, {arg, ret}); }
59
    /// @overload
60
    void callFunction(const char* name, const In& arg0, const In& arg1, const Out& ret) const
61
0
    {
62
0
        callFunction(name, {arg0, arg1, ret});
63
0
    }
64
65
    /// dst = texture(sampler, texcoord);
66
    void sampleTexture(const In& sampler, const In& texcoord, const Out& dst) const
67
0
    {
68
0
        sampleTexture({sampler, texcoord, dst});
69
0
    }
70
    /// @overload
71
    void sampleTexture(const std::vector<Operand>& params) const;
72
73
    /// to = from;
74
0
    void assign(const In& from, const Out& to) const { assign({from, to}); }
75
    /// @overload
76
    void assign(const std::vector<Operand>& params) const;
77
78
    /// dst = arg0 * arg1;
79
0
    void mul(const In& arg0, const In& arg1, const Out& dst) const { binaryOp('*', {arg0, arg1, dst}); }
80
81
    /// dst = arg0 / arg1;
82
0
    void div(const In& arg0, const In& arg1, const Out& dst) const { binaryOp('/', {arg0, arg1, dst}); }
83
84
    /// dst = arg0 - arg1;
85
0
    void sub(const In& arg0, const In& arg1, const Out& dst) const { binaryOp('-', {arg0, arg1, dst}); }
86
87
    /// dst = arg0 + arg1;
88
0
    void add(const In& arg0, const In& arg1, const Out& dst) const { binaryOp('+', {arg0, arg1, dst}); }
89
90
    /// dst = arg0 OP arg1;
91
    void binaryOp(char op, const std::vector<Operand>& params) const;
92
93
    /// shorthand for "dst = BUILTIN(args);"
94
    void callBuiltin(const char* name, const std::vector<Operand>& params) const;
95
    /// @overload
96
0
    void callBuiltin(const char* name, const In& arg, const Out& ret) const { callBuiltin(name, {arg, ret}); }
97
    /// @overload
98
    void callBuiltin(const char* name, const In& arg0, const In& arg1, const Out& ret) const
99
0
    {
100
0
        callBuiltin(name, {arg0, arg1, ret});
101
0
    }
102
    /// @overload
103
    void callBuiltin(const char* name, const In& arg0, const In& arg1, const In& arg2, const Out& ret) const
104
0
    {
105
0
        callBuiltin(name, {arg0, arg1, arg2, ret});
106
0
    }
107
private:
108
    uint32 mStage;
109
    Function* mParent;
110
0
    FunctionStageRef(uint32 stage, Function* parent) : mStage(stage), mParent(parent) {}
111
};
112
113
/** A class that represents a shader based program function.
114
*/
115
class _OgreRTSSExport Function : public RTShaderSystemAlloc
116
{
117
    friend class ProgramManager;
118
// Interface.
119
public:
120
    /// @deprecated
121
    ParameterPtr resolveInputParameter(Parameter::Semantic semantic, int index,  int content, GpuConstantType type);
122
123
    /** Resolve input parameter of this function
124
    @param content The content of the parameter.
125
    @param type The type of the desired parameter.
126
    @return parameter instance in case of that resolve operation succeeded.
127
    */
128
    ParameterPtr resolveInputParameter(int content, GpuConstantType type = GCT_UNKNOWN)
129
0
    {
130
0
        return resolveInputParameter(Parameter::SPS_UNKNOWN, 0, content, type);
131
0
    }
132
133
    /// resolve input parameter from previous output
134
    ParameterPtr resolveInputParameter(const ParameterPtr& out)
135
0
    {
136
0
        OgreAssert(out, "parameter must not be NULL");
137
0
        return resolveInputParameter(out->getSemantic(), out->getIndex(), out->getContent(), out->getType());
138
0
    }
139
140
    /**
141
     * get input parameter by content
142
     * @param content
143
     * @param type The type of the desired parameter.
144
     * @return parameter or NULL if not found
145
     */
146
    ParameterPtr getInputParameter(int content, GpuConstantType type = GCT_UNKNOWN)
147
0
    {
148
0
        return _getParameterByContent(mInputParameters, content, type);
149
0
    }
150
151
    /// @deprecated
152
    ParameterPtr resolveOutputParameter(Parameter::Semantic semantic, int index,  int content, GpuConstantType type);
153
154
    /** Resolve output parameter of this function
155
    @param content The content of the parameter.
156
    @param type The type of the desired parameter.
157
    @return parameter instance in case of that resolve operation succeeded.
158
    */
159
    ParameterPtr resolveOutputParameter(int content, GpuConstantType type = GCT_UNKNOWN)
160
0
    {
161
0
        return resolveOutputParameter(Parameter::SPS_UNKNOWN, 0, content, type);
162
0
    }
163
164
    /**
165
     * get output parameter by content
166
     * @param content
167
     * @param type The type of the desired parameter.
168
     * @return parameter or NULL if not found
169
     */
170
    ParameterPtr getOutputParameter(int content, GpuConstantType type = GCT_UNKNOWN)
171
0
    {
172
0
        return _getParameterByContent(mOutputParameters, content, type);
173
0
    }
174
175
    /** Resolve local parameter of this function
176
177
    local parameters do not have index or semantic.
178
    @param name The name of the parameter.
179
    @param type The type of the desired parameter.
180
    @param arraySize If > 0, the parameter will be an array of the given size.
181
    @return parameter instance in case of that resolve operation succeeded.
182
    */
183
    ParameterPtr resolveLocalParameter(GpuConstantType type, const String& name, size_t arraySize = 0);
184
185
    /** Resolve local parameter of this function
186
187
    local parameters do not have index or semantic.
188
    @param content The content of the parameter.
189
    @param type The type of the desired parameter.
190
    @return parameter instance in case of that resolve operation succeeded.
191
    */
192
    ParameterPtr resolveLocalParameter(int content, GpuConstantType type = GCT_UNKNOWN);
193
194
195
    ParameterPtr resolveLocalStructParameter(const String& type, const String& name);
196
197
    /**
198
     * get local parameter by content
199
     * @param content
200
     * @return parameter or NULL if not found
201
     */
202
    ParameterPtr getLocalParameter(int content)
203
0
    {
204
0
        return _getParameterByContent(mLocalParameters, content, GCT_UNKNOWN);
205
0
    }
206
    /// @overload
207
    ParameterPtr getLocalParameter(const String& name)
208
0
    {
209
0
        return _getParameterByName(mLocalParameters, name);
210
0
    }
211
212
    /** Return a list of input parameters. */
213
0
    const ShaderParameterList& getInputParameters() const { return mInputParameters; }  
214
215
    /** Return a list of output parameters. */
216
0
    const ShaderParameterList& getOutputParameters() const { return mOutputParameters; }
217
218
    /** Return a list of local parameters. */
219
0
    const ShaderParameterList& getLocalParameters() const { return mLocalParameters; }  
220
    
221
    /** Add a function atom instance to this function. 
222
    @param atomInstance The atom instance to add.
223
    */
224
    void addAtomInstance(FunctionAtom* atomInstance);
225
226
    /// get a @ref FFPShaderStage of this function
227
    FunctionStageRef getStage(uint32 s)
228
0
    {
229
0
        return FunctionStageRef(s, this);
230
0
    }
231
232
    /** Delete a function atom instance from this function. 
233
    @param atomInstance The atom instance to OGRE_DELETE.
234
    */
235
    bool deleteAtomInstance(FunctionAtom* atomInstance);
236
237
    /** Return list of atom instances composing this function. (Const version) */
238
    const FunctionAtomInstanceList& getAtomInstances();
239
240
    /** Add input parameter to this function. */
241
    void addInputParameter(ParameterPtr parameter);
242
243
    /** Add output parameter to this function. */
244
    void addOutputParameter(ParameterPtr parameter);
245
246
    /** Delete input parameter from this function. */
247
    void deleteInputParameter(ParameterPtr parameter);
248
249
    /** Delete output parameter from this function. */
250
    void deleteOutputParameter(ParameterPtr parameter);
251
252
    /** Delete all input parameters from this function. */
253
    void deleteAllInputParameters();
254
255
    /** Delete all output parameters from this function. */
256
    void deleteAllOutputParameters();
257
258
    explicit Function(GpuProgramType type = GPT_VERTEX_PROGRAM);
259
260
private:
261
262
    static ParameterPtr _getParameterByName(const ShaderParameterList& parameterList, const String& name);
263
    static ParameterPtr _getParameterBySemantic(const ShaderParameterList& parameterList, const Parameter::Semantic semantic, int index);
264
    static ParameterPtr _getParameterByContent(const ShaderParameterList& parameterList, int content, GpuConstantType type);
265
266
    /** Class destructor */
267
    ~Function();
268
269
    /** Add parameter to given list */
270
    void addParameter(ShaderParameterList& parameterList, ParameterPtr parameter);
271
272
    /** Delete parameter from a given list */
273
    void deleteParameter(ShaderParameterList& parameterList, ParameterPtr parameter);
274
275
    // Input parameters.
276
    ShaderParameterList mInputParameters;
277
    // Output parameters.
278
    ShaderParameterList mOutputParameters;
279
    // Local parameters.
280
    ShaderParameterList mLocalParameters;
281
    // Atom instances composing this function.
282
    std::map<size_t, FunctionAtomInstanceList> mAtomInstances;
283
    FunctionAtomInstanceList mSortedAtomInstances;
284
285
    GpuProgramType mType;
286
287
    friend class Program;
288
};
289
290
typedef std::vector<Function*>                     ShaderFunctionList;
291
typedef ShaderFunctionList::iterator                ShaderFunctionIterator;
292
typedef ShaderFunctionList::const_iterator          ShaderFunctionConstIterator;
293
294
/** @} */
295
/** @} */
296
297
}
298
}
299
300
#endif