Coverage Report

Created: 2025-07-11 06:36

/src/ogre/Components/RTShaderSystem/include/OgreShaderSubRenderState.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 _ShaderSubRenderState_
28
#define _ShaderSubRenderState_
29
30
#include "OgreShaderPrerequisites.h"
31
#include "OgreShaderScriptTranslator.h"
32
33
namespace Ogre {
34
namespace RTShader {
35
36
/** \addtogroup Optional
37
*  @{
38
*/
39
/** \addtogroup RTShader
40
*  @{
41
*/
42
43
typedef SharedPtr<SubRenderStateAccessor>   SubRenderStateAccessorPtr; 
44
45
/// Computes the position of the vertex in projection space, allows using instancing
46
_OgreRTSSExport extern const String SRS_TRANSFORM;
47
/// Enables vertex-colors, always required.
48
_OgreRTSSExport extern const String SRS_VERTEX_COLOUR;
49
/// Computes FFP texture transform and blending based on the assigned textures
50
_OgreRTSSExport extern const String SRS_TEXTURING;
51
/// Adds per-vertex (FFP) lighting calulations (Gouraud Shading)
52
_OgreRTSSExport extern const String SRS_PER_VERTEX_LIGHTING;
53
/// Modulates the color of a pixel based on the scene or object fog parameters
54
_OgreRTSSExport extern const String SRS_FOG;
55
/// Enables alpha-testing
56
_OgreRTSSExport extern const String SRS_ALPHA_TEST;
57
/// @deprecated use #SRS_SHADOW_MAPPING
58
_OgreRTSSExport extern const String SRS_INTEGRATED_PSSM3;
59
/// shadow mapping receiver with multiple lights or single PSSM light with up to 3 custom splits.
60
_OgreRTSSExport extern const String SRS_SHADOW_MAPPING;
61
/// Adds per-pixel lighting calulations (Phong Shading)
62
_OgreRTSSExport extern const String SRS_PER_PIXEL_LIGHTING;
63
/// Use metal roughness parametrisation for lighting calulations
64
_OgreRTSSExport extern const String SRS_COOK_TORRANCE_LIGHTING;
65
/// Adds calculations for indirect image based lighting (IBL)
66
_OgreRTSSExport extern const String SRS_IMAGE_BASED_LIGHTING;
67
/// Use a normal map to derive normals
68
_OgreRTSSExport extern const String SRS_NORMALMAP;
69
/// Redirects rendering results into intermediate buffers for the WBOIT algorithm
70
_OgreRTSSExport extern const String SRS_WBOIT;
71
/// Redirects rendering results into gbuffers for e.g. deferred shading.
72
_OgreRTSSExport extern const String SRS_GBUFFER;
73
/// enable [triplanar texture mapping](https://www.volume-gfx.com/volume-rendering/triplanar-texturing/)
74
_OgreRTSSExport extern const String SRS_TRIPLANAR_TEXTURING;
75
/// Apply photoshop-like blend effects to texture layers
76
_OgreRTSSExport extern const String SRS_LAYERED_BLENDING;
77
/// Include skinning calculations for Skeletal Animation in the shader to move computations to the GPU
78
_OgreRTSSExport extern const String SRS_HARDWARE_SKINNING;
79
80
/** This class is the base interface of sub part from a shader based rendering pipeline.
81
* All sub parts implementations should derive from it and implement the needed methods.
82
A simple example of sub class of this interface will be the transform sub state of the
83
fixed pipeline.
84
*/
85
class _OgreRTSSExport SubRenderState : public RTShaderSystemAlloc
86
{
87
88
// Interface.
89
public:
90
91
    /** Class default constructor */
92
    SubRenderState();
93
94
    /** Class destructor */
95
    virtual ~SubRenderState();
96
97
98
    /** Get the type of this sub render state.
99
100
    The type string should be unique among all registered sub render states.    
101
    */
102
    virtual const String& getType() const = 0;
103
104
105
    /** Get the execution order of this sub render state.
106
107
    The return value should be synchronized with the predefined values
108
    of the FFPShaderStage enum.
109
    */
110
    virtual int getExecutionOrder() const = 0;
111
112
113
    /** Copy details from a given sub render state to this one.
114
    @param rhs the source sub state to copy from.   
115
    */
116
    virtual void copyFrom(const SubRenderState& rhs) = 0;
117
118
    /** Operator = declaration. Assign the given source sub state to this sub state.
119
    @param rhs the source sub state to copy from.   
120
    */
121
    SubRenderState& operator=   (const SubRenderState& rhs);
122
123
    /** Create sub programs that represents this sub render state as part of a program set.
124
    The given program set contains CPU programs that represents a vertex shader and pixel shader.
125
    One should use these program class API to create a representation of the sub state he wished to
126
    implement.
127
    @param programSet container class of CPU and GPU programs that this sub state will affect on.
128
    */
129
    virtual bool createCpuSubPrograms(ProgramSet* programSet);
130
131
    /** Update GPU programs parameters before a rendering operation occurs.
132
    This method is called in the context of SceneManager::renderSingle object via the RenderObjectListener interface and
133
    lets this sub render state instance opportunity to update custom GPU program parameters before the rendering action occurs.
134
    @see RenderObjectListener::notifyRenderSingleObject.
135
    @param rend The renderable that is about to be rendered.
136
    @param pass The pass that used for this rendering.
137
    @param source The auto parameter source.
138
    @param pLightList The light list used in the current rendering context.
139
    */
140
0
    virtual void updateGpuProgramsParams(Renderable* rend, const Pass* pass,  const AutoParamDataSource* source,  const LightList* pLightList) { }
141
142
    /** Called before adding this sub render state to the given render state.
143
    Allows this sub render state class to configure specific parameters depending on source pass or parent render state.
144
    Return of false value will cause canceling the add operation.
145
    @param renderState The target render state container this sub render state is about to be added.    
146
    @param srcPass The source pass.
147
    @param dstPass The destination pass.
148
    */
149
0
    virtual bool preAddToRenderState(const RenderState* renderState, Pass* srcPass, Pass* dstPass) { return true; }
150
151
    /** Return the accessor object to this sub render state.
152
    @see SubRenderStateAccessor.
153
    */
154
    SubRenderStateAccessorPtr getAccessor();
155
156
    /** Return the accessor object to this sub render state.
157
    @see SubRenderStateAccessor.
158
    */
159
    SubRenderStateAccessorPtr getAccessor() const;
160
161
    /// generic set method for parameters that connot be derived in @ref preAddToRenderState
162
0
    virtual bool setParameter(const String& name, const String& value) { return false; }
163
164
    /// @overload
165
0
    bool setParameter(const String& name, const char* value) { return setParameter(name, String(value)); }
166
167
    /// @overload
168
    virtual void setParameter(const String& name, const Any& value)
169
0
    {
170
0
        OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, name+": unsupported any type");
171
0
    }
172
173
// Protected methods
174
protected:
175
176
    /** Resolve parameters that this sub render state requires. 
177
    @param programSet container class of CPU and GPU programs that this sub state will affect on.
178
    @remarks Internal method called in the context of SubRenderState::createCpuSubPrograms implementation.
179
    */
180
    virtual bool resolveParameters(ProgramSet* programSet); 
181
182
    /** Resolve dependencies that this sub render state requires.   
183
    @param programSet container class of CPU and GPU programs that this sub state will affect on.
184
    @remarks Internal method called in the context of SubRenderState::createCpuSubPrograms implementation.
185
    */
186
    virtual bool resolveDependencies(ProgramSet* programSet);
187
188
    /** Add function invocation that this sub render state requires.    
189
    @param programSet container class of CPU and GPU programs that this sub state will affect on.
190
    @remarks Internal method called in the context of SubRenderState::createCpuSubPrograms implementation.
191
    */
192
    virtual bool addFunctionInvocations(ProgramSet* programSet);
193
194
// Attributes.
195
private:    
196
    // The accessor of this instance.
197
    mutable SubRenderStateAccessorPtr mThisAccessor;
198
    // The accessor of the source instance which used as base to create this instance.
199
    SubRenderStateAccessorPtr mOtherAccessor;
200
    
201
};
202
203
typedef std::vector<SubRenderState*>               SubRenderStateList;
204
typedef SubRenderStateList::iterator                SubRenderStateListIterator;
205
typedef SubRenderStateList::const_iterator          SubRenderStateListConstIterator;
206
207
typedef std::set<SubRenderState*>                  SubRenderStateSet;
208
typedef SubRenderStateSet::iterator                 SubRenderStateSetIterator;
209
typedef SubRenderStateSet::const_iterator           SubRenderStateSetConstIterator;
210
211
212
/** This class uses as accessor from a template SubRenderState to all of its instances that
213
created based on it. Since SubRenderState that added as templates to a RenderState are not directly used by the
214
system this class enable accessing the used instances.
215
A common usage will be add a SubRenderState to certain pass, obtain accessor and then call a method on the instanced SubRenderState
216
that will trigger some GPU uniform parameter updates.
217
*/
218
class _OgreRTSSExport SubRenderStateAccessor
219
{
220
public:
221
    /** Add SubRenderState instance to this accessor.
222
    */
223
    void addSubRenderStateInstance(SubRenderState* subRenderState) const 
224
0
    {
225
0
        mSubRenderStateInstancesSet.insert(subRenderState);
226
0
    }
227
228
    /** Remove SubRenderState instance to this accessor.
229
    */
230
    void removeSubRenderStateInstance(SubRenderState* subRenderState) const
231
0
    {
232
0
        SubRenderStateSetIterator itFind = mSubRenderStateInstancesSet.find(subRenderState);
233
0
234
0
        if (itFind != mSubRenderStateInstancesSet.end())
235
0
        {
236
0
            mSubRenderStateInstancesSet.erase(itFind);
237
0
        }
238
0
    }
239
240
    /** Return a set of all instances of the template SubRenderState. */
241
0
    SubRenderStateSet& getSubRenderStateInstanceSet() { return mSubRenderStateInstancesSet; }
242
243
    /** Return a set of all instances of the template SubRenderState. (const version). */
244
0
    const SubRenderStateSet& getSubRenderStateInstanceSet() const { return mSubRenderStateInstancesSet; }
245
246
protected:
247
    /** Construct SubRenderState accessor based on the given template SubRenderState.
248
    */
249
0
    SubRenderStateAccessor(const SubRenderState* templateSubRenderState) : mTemplateSubRenderState(templateSubRenderState) {}
250
251
252
protected:
253
    const SubRenderState* mTemplateSubRenderState;
254
    mutable SubRenderStateSet mSubRenderStateInstancesSet;
255
256
private:
257
    friend class SubRenderState;
258
259
};
260
261
262
/** Abstract factory interface for creating SubRenderState implementation instances.
263
264
Plugins or 3rd party applications can add new types of sub render states to the 
265
RTShader System by creating subclasses of the SubRenderState class. 
266
Because multiple instances of these sub class may be required, 
267
a factory class to manage the instances is also required. 
268
@par
269
SubRenderStateFactory subclasses must allow the creation and destruction of SubRenderState
270
subclasses. They must also be registered with the ShaderGenerator::addSubRenderStateFactory. 
271
All factories have a type which identifies them and the sub class of SubRenderState they creates.
272
*/
273
class _OgreRTSSExport SubRenderStateFactory : public RTShaderSystemAlloc
274
{
275
276
public:
277
0
    SubRenderStateFactory() {}
278
    virtual ~SubRenderStateFactory();
279
280
    /** Get the type of this sub render state factory.
281
282
    The type string should be the same as the type of the SubRenderState sub class it is going to create.   
283
    @see SubRenderState::getType.
284
    */
285
    virtual const String& getType() const = 0;
286
    
287
    /** Create an instance of the SubRenderState sub class it suppose to create.    
288
    */
289
    virtual SubRenderState* createInstance() OGRE_NODISCARD;
290
291
    /** Create an instance of the SubRenderState based on script properties.    
292
    This method is called in the context of script parsing and let this factory
293
    the opportunity to create custom SubRenderState instances based on extended script properties.
294
    @param prop The property
295
    @param pass The pass that is the parent context of this node.
296
    @param translator The translator instance holding existing scripts.
297
    */
298
    virtual SubRenderState* createInstance(const ScriptProperty& prop, Pass* pass,
299
                                           SGScriptTranslator* translator) OGRE_NODISCARD
300
0
    {
301
0
        return NULL;
302
0
    }
303
304
    /// @deprecated
305
    virtual SubRenderState* createInstance(ScriptCompiler* compiler, PropertyAbstractNode* prop, Pass* pass, SGScriptTranslator* translator) OGRE_NODISCARD 
306
0
    { return NULL; }
307
308
    /** Create an instance of the SubRenderState based on script properties.    
309
    This method is called in the context of script parsing and let this factory
310
    the opportunity to create custom SubRenderState instances based on extended script properties.
311
    @param prop The property
312
    @param texState The pass that is the parent context of this node.
313
    @param translator The translator instance holding existing scripts.
314
    */
315
    virtual SubRenderState* createInstance(const ScriptProperty& prop, TextureUnitState* texState,
316
                                           SGScriptTranslator* translator) OGRE_NODISCARD
317
0
    {
318
0
        return NULL;
319
0
    }
320
321
    /// @deprecated
322
    virtual SubRenderState* createInstance(ScriptCompiler* compiler, PropertyAbstractNode* prop, TextureUnitState* texState, SGScriptTranslator* translator) OGRE_NODISCARD 
323
0
    { return NULL; }
324
325
    /** Retrieve the previous instance the SRS in the script translator or
326
    * create a new instance if not found 
327
    @param translator The translator instance holding existing scripts.
328
    */
329
    virtual SubRenderState* createOrRetrieveInstance(SGScriptTranslator* translator);
330
331
    /** Destroy the given instance. 
332
    @param subRenderState The instance to destroy.
333
    */
334
    virtual void destroyInstance(SubRenderState* subRenderState);
335
336
    /** Destroy all the instances that created by this factory.     
337
    */
338
    virtual void destroyAllInstances();
339
340
    /** Write the given sub-render state instance using the material serializer.
341
    This method is called in the context of material serialization. It is useful for integrating into
342
    bigger context of material exporters from various environment that want to take advantage of the RT Shader System.
343
    Sub classes of this interface should override in case they need to write custom properties into the script context.
344
    @param ser The material serializer instance.
345
    @param subRenderState The sub render state instance to write down.
346
    @param srcPass The source pass.
347
    @param dstPass The generated shader based pass.
348
    */
349
0
    virtual void writeInstance(MaterialSerializer* ser, SubRenderState* subRenderState, Pass* srcPass, Pass* dstPass) {}
350
351
    /** Write the given sub-render state instance using the material serializer.
352
    This method is called in the context of material serialization. It is useful for integrating into
353
    bigger context of material exporters from various environment that want to take advantage of the RT Shader System.
354
    Sub classes of this interface should override in case they need to write custom properties into the script context.
355
    @param ser The material serializer instance.
356
    @param subRenderState The sub render state instance to write down.
357
    @param srcTextureUnit The source texture unit state.
358
    @param dstTextureUnit The generated shader based texture unit state.
359
    */
360
0
    virtual void writeInstance(MaterialSerializer* ser, SubRenderState* subRenderState, const TextureUnitState* srcTextureUnit, const TextureUnitState* dstTextureUnit) {}
361
protected:
362
    /** Create instance implementation method. Each sub class of this interface
363
    must implement this method in which it will allocate the specific sub class of 
364
    the SubRenderState.
365
    */
366
    virtual SubRenderState* createInstanceImpl() = 0;
367
368
// Attributes.
369
protected:
370
    // List of all sub render states instances this factory created.
371
    SubRenderStateSet mSubRenderStateList;
372
};
373
374
/** @} */
375
/** @} */
376
377
}
378
}
379
380
#endif
381