Coverage Report

Created: 2025-11-25 06:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ogre/Components/RTShaderSystem/include/OgreShaderParameter.h
Line
Count
Source
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 _ShaderParameter_
28
#define _ShaderParameter_
29
30
#include "OgreShaderPrerequisites.h"
31
#include "OgreVector.h"
32
#include "OgreMatrix4.h"
33
#include "OgreGpuProgramParams.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 shader based program parameter.
46
*/
47
class _OgreRTSSExport Parameter : public RTShaderSystemAlloc
48
{
49
public:
50
    // Shader parameter semantic.
51
    enum Semantic
52
    {
53
        /// Unknown semantic
54
        SPS_UNKNOWN = 0,
55
        /// Position
56
        SPS_POSITION = 1,
57
        /// Blending weights
58
        SPS_BLEND_WEIGHTS = 2,
59
        /// Blending indices
60
        SPS_BLEND_INDICES = 3,
61
        /// Normal, 3 reals per vertex
62
        SPS_NORMAL = 4,
63
        /// General floating point color.
64
        SPS_COLOR = 5,      
65
        /// Texture coordinates
66
        SPS_TEXTURE_COORDINATES = 7,
67
        /// Binormal (Y axis if normal is Z)
68
        SPS_BINORMAL = 8,
69
        /// Tangent (X axis if normal is Z)
70
        SPS_TANGENT = 9,
71
        /// VFACE
72
        SPS_FRONT_FACING,
73
        /// SV_RenderTargetArrayIndex
74
        SPS_LAYER
75
    };
76
77
    /** Shader parameter content
78
     * 
79
     * used to resolve Parameters across different SubRenderState instances
80
     * Think of it as Semantic extended to the actual parameter content.
81
     */ 
82
    enum Content
83
    {
84
        /// Unknown content
85
        SPC_UNKNOWN,
86
87
        /// Position in object space
88
        SPC_POSITION_OBJECT_SPACE,
89
90
        /// Position in world space
91
        SPC_POSITION_WORLD_SPACE,
92
93
        /// Position in view space
94
        SPC_POSITION_VIEW_SPACE,
95
96
        /// Position in projective space
97
        SPC_POSITION_PROJECTIVE_SPACE,
98
99
        /// Position in light space index 0-7
100
        SPC_POSITION_LIGHT_SPACE0,
101
        SPC_POSITION_LIGHT_SPACE1,
102
        SPC_POSITION_LIGHT_SPACE2,
103
        SPC_POSITION_LIGHT_SPACE3,
104
        SPC_POSITION_LIGHT_SPACE4,
105
        SPC_POSITION_LIGHT_SPACE5,
106
        SPC_POSITION_LIGHT_SPACE6,
107
        SPC_POSITION_LIGHT_SPACE7,
108
109
        /// Normal in object space
110
        SPC_NORMAL_OBJECT_SPACE,
111
112
        /// Normal in world space
113
        SPC_NORMAL_WORLD_SPACE,
114
115
        /// Normal in view space
116
        SPC_NORMAL_VIEW_SPACE,
117
118
        /// View vector in object space
119
        SPC_POSTOCAMERA_OBJECT_SPACE,
120
121
        /// View vector in world space
122
        SPC_POSTOCAMERA_WORLD_SPACE,
123
124
        /// View vector in view space
125
        SPC_POSTOCAMERA_VIEW_SPACE,
126
127
        /// Blending weights
128
        SPC_BLEND_WEIGHTS,
129
130
        /// Blending indices
131
        SPC_BLEND_INDICES,
132
        
133
        /// Tangent in object space
134
        SPC_TANGENT_OBJECT_SPACE,
135
136
        /// Tangent in world space
137
        SPC_TANGENT_WORLD_SPACE,
138
139
        /// Tangent in view space
140
        SPC_TANGENT_VIEW_SPACE,
141
142
        /// Binormal in object space
143
        SPC_BINORMAL_OBJECT_SPACE,
144
145
        /// Binormal in world space
146
        SPC_BINORMAL_WORLD_SPACE,
147
148
        /// Binormal in view space
149
        SPC_BINORMAL_VIEW_SPACE,
150
151
        /// Diffuse color
152
        SPC_COLOR_DIFFUSE,
153
154
        /// Specular color
155
        SPC_COLOR_SPECULAR,
156
157
        /// Depth in world space
158
        SPC_DEPTH_WORLD_SPACE,
159
160
        /// Depth in view space
161
        SPC_DEPTH_VIEW_SPACE,
162
163
        /// Depth in projective space
164
        SPC_DEPTH_PROJECTIVE_SPACE,
165
166
        /// Texture coordinate set index 0-7
167
        SPC_TEXTURE_COORDINATE0,        
168
        SPC_TEXTURE_COORDINATE1,        
169
        SPC_TEXTURE_COORDINATE2,        
170
        SPC_TEXTURE_COORDINATE3,    
171
        SPC_TEXTURE_COORDINATE4,
172
        SPC_TEXTURE_COORDINATE5,
173
        SPC_TEXTURE_COORDINATE6,
174
        SPC_TEXTURE_COORDINATE7,
175
    
176
        /// point sprite coordinates
177
        SPC_POINTSPRITE_COORDINATE,
178
179
        /// point sprite size
180
        SPC_POINTSPRITE_SIZE,
181
182
        /// gl_FrontFacing
183
        SPC_FRONT_FACING,
184
185
        /// gl_Layer
186
        SPC_LAYER,
187
188
        /// Reserved custom content range to be used by user custom shader extensions.
189
        SPC_CUSTOM_CONTENT_BEGIN    = 1000,
190
        SPC_CUSTOM_CONTENT_END      = 2000
191
    };
192
193
// Interface.
194
public:
195
    /** */
196
    Parameter();
197
198
    /** Class constructor.
199
    @param type The type of this parameter.
200
    @param name The name of this parameter.
201
    @param semantic The semantic of this parameter.
202
    @param index The index of this parameter.
203
    @param content The content of this parameter.
204
    @param size
205
    */
206
    Parameter(GpuConstantType type, const String& name, const Semantic& semantic, int index, int content,
207
              size_t size = 0);
208
209
    /** Class destructor */
210
0
    virtual ~Parameter() {};
211
212
    /** Get the name of this parameter. */
213
0
    const String& getName() const { return mName; }
214
215
    /// internal function for aliasing to GLSL builtins e.g. gl_Position
216
    void _rename(const String& newName, bool onlyLocal = false)
217
0
    {
218
0
        if(onlyLocal)
219
0
            mBindName = mName;
220
0
        mName = newName;
221
0
    }
222
223
    /** Get the type of this parameter. */
224
0
    GpuConstantType getType() const { return mType; }
225
226
    /** Get the semantic of this parameter. */
227
0
    const Semantic& getSemantic() const { return mSemantic; }
228
229
    /** Get the index of this parameter. */
230
0
    int getIndex() const { return mIndex; } 
231
232
    /** Return the content of this parameter. */
233
0
    int getContent() const { return mContent; }
234
235
    /** Returns true if this instance is a ConstParameter otherwise false. */
236
0
    virtual bool isConstParameter() const { return false; }
237
238
    /** Returns the string representation of this parameter. */
239
0
    virtual String toString() const { return mName; }
240
    
241
    /** Returns Whether this parameter is an array. */
242
0
    bool isArray() const { return mSize > 0; }
243
244
    /** Returns the number of elements in the parameter (for arrays). */
245
0
    size_t getSize() const { return mSize; }
246
    
247
    /** Sets the number of elements in the parameter (for arrays). */
248
0
    void setSize(size_t size) { mSize = size; }
249
250
    /// track whether this was used
251
0
    void setUsed(bool used) { mUsed = used; }
252
0
    bool isUsed() { return mUsed; }
253
254
    /// Is highp needed when using GLSL ES
255
0
    bool isHighP() const { return mIsHighP; }
256
0
    void setHighP(bool highP) { mIsHighP = highP; }
257
258
0
    const String& getStructType() const { return mStructType; }
259
0
    void setStructType(const String& structType) { mStructType = structType; }
260
261
// Attributes.
262
protected:
263
    // Name of this parameter.
264
    String mName;
265
266
    // only used for local renaming
267
    String mBindName;
268
269
    // Type of this parameter.
270
    GpuConstantType mType;
271
272
    // Type, if this is a struct
273
    String mStructType;
274
275
    // Semantic of this parameter.
276
    Semantic mSemantic;
277
    // Index of this parameter.
278
    int mIndex;
279
    // The content of this parameter.
280
    int mContent;
281
    // Number of elements in the parameter (for arrays)
282
    size_t mSize;
283
    
284
    bool mUsed;
285
    bool mIsHighP;
286
};
287
288
typedef ShaderParameterList::iterator           ShaderParameterIterator;
289
typedef ShaderParameterList::const_iterator     ShaderParameterConstIterator;
290
291
/** Uniform parameter class. Allow fast access to GPU parameter updates.
292
*/
293
class _OgreRTSSExport UniformParameter : public Parameter
294
{
295
public:
296
297
    /** Class constructor.
298
    @param type The type of this parameter.
299
    @param name The name of this parameter.
300
    @param semantic The semantic of this parameter.
301
    @param index The index of this parameter.
302
    @param content The content of this parameter.
303
    @param variability How this parameter varies (bitwise combination of GpuProgramVariability).
304
    @param size number of elements in the parameter.    
305
    */
306
    UniformParameter(GpuConstantType type, const String& name, 
307
        const Semantic& semantic, int index, 
308
        int content,
309
        uint16 variability, size_t size);
310
311
    /** Class constructor.
312
    @param autoType The auto type of this parameter.
313
    @param fAutoConstantData The real data for this auto constant parameter.    
314
    @param size number of elements in the parameter.    
315
    */
316
    UniformParameter(GpuProgramParameters::AutoConstantType autoType, float fAutoConstantData, size_t size);
317
    
318
    /** Class constructor.
319
    @param autoType The auto type of this parameter.
320
    @param fAutoConstantData The real data for this auto constant parameter.    
321
    @param size number of elements in the parameter.
322
    @param type The desired data type of this auto constant parameter.
323
    */
324
    UniformParameter(GpuProgramParameters::AutoConstantType autoType, float fAutoConstantData, size_t size, GpuConstantType type);
325
326
    /** Class constructor.
327
    @param autoType The auto type of this parameter.
328
    @param nAutoConstantData The int data for this auto constant parameter. 
329
    @param size number of elements in the parameter.    
330
    */
331
    UniformParameter(GpuProgramParameters::AutoConstantType autoType, uint32 nAutoConstantData, size_t size);
332
    
333
    /** Class constructor.
334
    @param autoType The auto type of this parameter.
335
    @param nAutoConstantData The int data for this auto constant parameter. 
336
    @param size number of elements in the parameter.
337
    @param type The desired data type of this auto constant parameter.
338
    */
339
    UniformParameter(GpuProgramParameters::AutoConstantType autoType, uint32 nAutoConstantData, size_t size, GpuConstantType type);
340
341
    
342
    /** Get auto constant int data of this parameter, in case it is auto constant parameter. */
343
0
    uint32 getAutoConstantIntData() const { return isArray() ? static_cast<uint32>(getSize()) : mAutoConstantIntData; }
344
345
    /** Get auto constant real data of this parameter, in case it is auto constant parameter. */
346
0
    float getAutoConstantRealData() const { return mAutoConstantRealData; }
347
348
    /** Return true if this parameter is a floating point type, false otherwise. */
349
0
    bool isFloat() const { return GpuConstantDefinition::isFloat(mType); }
350
351
    /** Return true if this parameter is a texture sampler type, false otherwise. */
352
0
    bool isSampler() const { return GpuConstantDefinition::isSampler(mType); }
353
354
    /** Return true if this parameter is an auto constant parameter, false otherwise. */
355
0
    bool isAutoConstantParameter() const { return mIsAutoConstantReal || mIsAutoConstantInt; }
356
357
    /** Return true if this parameter an auto constant with int data type, false otherwise. */
358
0
    bool isAutoConstantIntParameter() const { return mIsAutoConstantInt; }
359
360
    /** Return true if this parameter an auto constant with real data type, false otherwise. */
361
0
    bool isAutoConstantRealParameter() const { return mIsAutoConstantReal; }
362
363
    /** Return the auto constant type of this parameter. */
364
0
    GpuProgramParameters::AutoConstantType getAutoConstantType  () const { return mAutoConstantType; }
365
366
    /** Return the variability of this parameter. */
367
0
    uint16 getVariability() const { return mVariability; }
368
369
    /** Bind this parameter to the corresponding GPU parameter. */
370
    void bind(GpuProgramParametersSharedPtr paramsPtr);
371
372
public:
373
374
    /** Update the GPU parameter with the given value. */   
375
    void setGpuParameter(int val)
376
0
    { 
377
0
        if (mParamsPtr != NULL)
378
0
        {
379
0
            mParamsPtr->_writeRawConstant(mPhysicalIndex, val);
380
0
        }
381
0
    }
382
383
    /** Update the GPU parameter with the given value. */   
384
    void setGpuParameter(Real val)  
385
0
    { 
386
0
        if (mParamsPtr != NULL)
387
0
        {
388
0
            mParamsPtr->_writeRawConstant(mPhysicalIndex, val);
389
0
        }
390
0
    }
391
392
    /** Update the GPU parameter with the given value. */   
393
    void setGpuParameter(const ColourValue& val)  
394
0
    { 
395
0
        if (mParamsPtr != NULL)
396
0
        {
397
0
            mParamsPtr->_writeRawConstant(mPhysicalIndex, val);
398
0
        }
399
0
    }
400
401
    /** Update the GPU parameter with the given value. */   
402
    void setGpuParameter(const Vector2& val)  
403
0
    { 
404
0
        if (mParamsPtr != NULL)
405
0
        {
406
0
            mParamsPtr->_writeRawConstants(mPhysicalIndex, val.ptr(), 2);
407
0
        }
408
0
    }
409
    
410
    /** Update the GPU parameter with the given value. */   
411
    void setGpuParameter(const Vector3& val)  
412
0
    { 
413
0
        if (mParamsPtr != NULL)
414
0
        {
415
0
            mParamsPtr->_writeRawConstant(mPhysicalIndex, val);
416
0
        }
417
0
    }
418
419
    /** Update the GPU parameter with the given value. */   
420
    void setGpuParameter(const Vector4& val)  
421
0
    { 
422
0
        if (mParamsPtr != NULL)
423
0
        {
424
0
            mParamsPtr->_writeRawConstant(mPhysicalIndex, val);     
425
0
        }
426
0
    }
427
428
    void setGpuParameter(const Matrix3& val)
429
0
    {
430
0
        if (mParamsPtr == NULL) return;
431
0
432
0
        if(mElementSize == 9) // check if tight packing is supported
433
0
        {
434
0
            mParamsPtr->_writeRawConstant(mPhysicalIndex, val, 9);
435
0
        }
436
0
        else
437
0
        {
438
0
            mParamsPtr->_writeRawConstant(mPhysicalIndex, Matrix4(val), mElementSize);
439
0
        }
440
0
    }
441
442
    /** Update the GPU parameter with the given value. */   
443
    void setGpuParameter(const Matrix4& val)  
444
0
    { 
445
0
        if (mParamsPtr != NULL)
446
0
        {
447
0
            mParamsPtr->_writeRawConstant(mPhysicalIndex, val, 16);
448
0
        }
449
0
    }
450
451
    /** Update the GPU parameter with the given value. */   
452
    void setGpuParameter(const float *val, size_t count, size_t multiple = 4)  
453
0
    { 
454
0
        if (mParamsPtr != NULL)
455
0
        {
456
0
            mParamsPtr->_writeRawConstants(mPhysicalIndex, val, count * multiple);
457
0
        }
458
0
    }
459
460
    /** Update the GPU parameter with the given value. */   
461
    void setGpuParameter(const double *val, size_t count, size_t multiple = 4)  
462
0
    { 
463
0
        if (mParamsPtr != NULL)
464
0
        {
465
0
            mParamsPtr->_writeRawConstants(mPhysicalIndex, val, count * multiple);
466
0
        }
467
0
    }
468
469
    /** Update the GPU parameter with the given value. */   
470
    void setGpuParameter(const int *val, size_t count, size_t multiple = 4)  
471
0
    { 
472
0
        if (mParamsPtr != NULL)
473
0
        {
474
0
            mParamsPtr->_writeRawConstants(mPhysicalIndex, val, count * multiple);
475
0
        }
476
0
    }
477
478
    /// light index or array size
479
    void updateExtraInfo(uint32 data)
480
0
    {
481
0
        if (!mParamsPtr)
482
0
            return;
483
0
484
0
        mParamsPtr->_setRawAutoConstant(mPhysicalIndex, mAutoConstantType, data, mVariability,
485
0
                                        mElementSize);
486
0
    }
487
488
private:
489
    // Is it auto constant real based parameter.
490
    bool mIsAutoConstantReal;
491
    // Is it auto constant int based parameter.
492
    bool mIsAutoConstantInt;
493
    GpuProgramParameters::AutoConstantType  mAutoConstantType;      // The auto constant type of this parameter.
494
    union
495
    {
496
        // Auto constant int data.
497
        uint32 mAutoConstantIntData;
498
        // Auto constant real data.
499
        float mAutoConstantRealData;
500
    };      
501
    // How this parameter varies (bitwise combination of GpuProgramVariability).
502
    uint16 mVariability;
503
    // The actual GPU parameters pointer.
504
    GpuProgramParameters* mParamsPtr;
505
    // The physical index of this parameter in the GPU program.
506
    size_t mPhysicalIndex;
507
    // The size of this parameter in the GPU program
508
    uint8 mElementSize;
509
};
510
511
typedef std::vector<UniformParameterPtr>       UniformParameterList;
512
typedef UniformParameterList::iterator          UniformParameterIterator;
513
typedef UniformParameterList::const_iterator    UniformParameterConstIterator;
514
515
/** Helper template which is the base for our ConstParameters
516
*/
517
template <class valueType>
518
class ConstParameter : public Parameter
519
{
520
public:
521
522
    ConstParameter( valueType val, 
523
        GpuConstantType type, 
524
        const Semantic& semantic,  
525
        const Content& content) 
526
        : Parameter(type, "Constant", semantic, 0, content)
527
    {
528
        mValue = val;
529
    }
530
531
    virtual             ~ConstParameter     () {}
532
533
    /** Returns the native value of this parameter. (for example a Vector3) */
534
    const valueType& getValue() const { return mValue; }
535
536
    /** 
537
    @see Parameter::isConstParameter.
538
    */
539
    bool isConstParameter() const override { return true; }
540
541
    /** 
542
    @see Parameter::toString.
543
    */
544
    String toString() const override = 0;
545
546
protected:
547
    valueType mValue;
548
};
549
550
/** Helper utility class that creates common parameters.
551
*/
552
class _OgreRTSSExport ParameterFactory
553
{
554
555
    // Interface.
556
public:
557
558
    static ParameterPtr createInPosition(int index, int content = Parameter::SPC_POSITION_OBJECT_SPACE);
559
    static ParameterPtr createOutPosition(int index);
560
561
    static ParameterPtr createInNormal(int index);
562
    static ParameterPtr createInWeights(int index);
563
    static ParameterPtr createInIndices(int index);
564
    static ParameterPtr createOutNormal(int index);
565
    static ParameterPtr createInBiNormal(int index);
566
    static ParameterPtr createOutBiNormal(int index);
567
    static ParameterPtr createInTangent(int index);
568
    static ParameterPtr createOutTangent(int index);
569
    static ParameterPtr createInColor(int index);
570
    static ParameterPtr createOutColor(int index);
571
572
    static ParameterPtr createInTexcoord(GpuConstantType type, int index, int content);
573
    static ParameterPtr createOutTexcoord(GpuConstantType type, int index, int content);
574
575
    static ParameterPtr createConstParam(const Vector2& val);
576
    static ParameterPtr createConstParam(const Vector3& val);
577
    static ParameterPtr createConstParam(const Vector4& val);
578
    static ParameterPtr createConstParam(float val);
579
580
    static UniformParameterPtr createSampler(GpuConstantType type, int index);
581
    static UniformParameterPtr createSampler1D(int index);
582
    static UniformParameterPtr createSampler2D(int index);
583
    static UniformParameterPtr createSampler2DArray(int index);
584
    static UniformParameterPtr createSampler3D(int index);
585
    static UniformParameterPtr createSamplerCUBE(int index);    
586
587
    static UniformParameterPtr createUniform(GpuConstantType type, int index, uint16 variability, const String& suggestedName, size_t size);
588
};
589
590
591
592
/** @} */
593
/** @} */
594
595
}
596
}
597
598
#endif