Coverage Report

Created: 2026-02-14 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ogre/Components/RTShaderSystem/src/OgreShaderGLSLProgramWriter.cpp
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
28
#include "OgreShaderPrecompiledHeaders.h"
29
30
namespace Ogre {
31
namespace RTShader {
32
33
String GLSLProgramWriter::TargetLanguage = "glsl";
34
35
//-----------------------------------------------------------------------
36
0
GLSLProgramWriter::GLSLProgramWriter() : mGLSLVersion(100), mIsGLSLES(false), mIsVulkan(false)
37
0
{
38
0
    initializeStringMaps();
39
40
0
    auto* rs = Root::getSingleton().getRenderSystem();
41
0
    if (!rs)
42
0
        return;
43
44
0
    mGLSLVersion = rs->getNativeShadingLanguageVersion();
45
46
0
    if (rs->getCapabilities()->isShaderProfileSupported("spirv"))
47
0
    {
48
0
        mGLSLVersion = 460;
49
0
        mIsVulkan = true;
50
0
    }
51
52
0
    mIsGLSLES = rs->getCapabilities()->isShaderProfileSupported("glsles");
53
0
}
54
55
//-----------------------------------------------------------------------
56
GLSLProgramWriter::~GLSLProgramWriter()
57
0
{
58
59
0
}
60
61
//-----------------------------------------------------------------------
62
void GLSLProgramWriter::initializeStringMaps()
63
0
{
64
    // basic glsl types
65
0
    mGpuConstTypeMap[GCT_FLOAT1] = "float";
66
0
    mGpuConstTypeMap[GCT_FLOAT2] = "vec2";
67
0
    mGpuConstTypeMap[GCT_FLOAT3] = "vec3";
68
0
    mGpuConstTypeMap[GCT_FLOAT4] = "vec4";
69
0
    mGpuConstTypeMap[GCT_SAMPLER1D] = "sampler1D";
70
0
    mGpuConstTypeMap[GCT_SAMPLER2D] = "sampler2D";
71
0
    mGpuConstTypeMap[GCT_SAMPLER2DARRAY] = "sampler2DArray";
72
0
    mGpuConstTypeMap[GCT_SAMPLER3D] = "sampler3D";
73
0
    mGpuConstTypeMap[GCT_SAMPLERCUBE] = "samplerCube";
74
0
    mGpuConstTypeMap[GCT_SAMPLERCUBESHADOW] = "samplerCubeShadow";
75
0
    mGpuConstTypeMap[GCT_SAMPLER1DSHADOW] = "sampler1DShadow";
76
0
    mGpuConstTypeMap[GCT_SAMPLER2DSHADOW] = "sampler2DShadow";
77
0
    mGpuConstTypeMap[GCT_SAMPLER2DARRAYSHADOW] = "sampler2DArrayShadow";
78
0
    mGpuConstTypeMap[GCT_SAMPLER_EXTERNAL_OES] = "samplerExternalOES";
79
0
    mGpuConstTypeMap[GCT_MATRIX_2X2] = "mat2";
80
0
    mGpuConstTypeMap[GCT_MATRIX_2X3] = "mat2x3";
81
0
    mGpuConstTypeMap[GCT_MATRIX_2X4] = "mat2x4";
82
0
    mGpuConstTypeMap[GCT_MATRIX_3X2] = "mat3x2";
83
0
    mGpuConstTypeMap[GCT_MATRIX_3X3] = "mat3";
84
0
    mGpuConstTypeMap[GCT_MATRIX_3X4] = "mat3x4";
85
0
    mGpuConstTypeMap[GCT_MATRIX_4X2] = "mat4x2";
86
0
    mGpuConstTypeMap[GCT_MATRIX_4X3] = "mat4x3";
87
0
    mGpuConstTypeMap[GCT_MATRIX_4X4] = "mat4";
88
0
    mGpuConstTypeMap[GCT_INT1] = "int";
89
0
    mGpuConstTypeMap[GCT_INT2] = "ivec2";
90
0
    mGpuConstTypeMap[GCT_INT3] = "ivec3";
91
0
    mGpuConstTypeMap[GCT_INT4] = "ivec4";
92
0
    mGpuConstTypeMap[GCT_UINT1] = "uint";
93
0
    mGpuConstTypeMap[GCT_UINT2] = "uvec2";
94
0
    mGpuConstTypeMap[GCT_UINT3] = "uvec3";
95
0
    mGpuConstTypeMap[GCT_UINT4] = "uvec4";
96
97
    // Custom vertex attributes defined http://www.ogre3d.org/docs/manual/manual_21.html
98
0
    mParamSemanticToNameMap[Parameter::SPS_POSITION] = "vertex";
99
0
    mParamSemanticToNameMap[Parameter::SPS_NORMAL] = "normal";
100
0
    mParamSemanticToNameMap[Parameter::SPS_TANGENT] = "tangent";
101
0
    mParamSemanticToNameMap[Parameter::SPS_BINORMAL] = "binormal";
102
0
    mParamSemanticToNameMap[Parameter::SPS_BLEND_INDICES] = "blendIndices";
103
0
    mParamSemanticToNameMap[Parameter::SPS_BLEND_WEIGHTS] = "blendWeights";
104
105
0
    mParamSemanticToNameMap[Parameter::SPS_COLOR] = "colour";
106
0
}
107
108
//-----------------------------------------------------------------------
109
void GLSLProgramWriter::writeSourceCode(std::ostream& os, Program* program)
110
0
{
111
    // Write the current version (this force the driver to more fulfill the glsl standard)
112
0
    os << "OGRE_NATIVE_GLSL_VERSION_DIRECTIVE\n";
113
114
0
    for(const auto& p : program->getParameters())
115
0
    {
116
0
        if(p->getType() != GCT_SAMPLER_EXTERNAL_OES)
117
0
            continue;
118
0
        if(mGLSLVersion > 100)
119
0
            os << "#extension GL_OES_EGL_image_external_essl3 : require\n";
120
0
        else
121
0
            os << "#extension GL_OES_EGL_image_external : require\n";
122
123
0
        break;
124
0
    }
125
126
0
    if (program->getType() == GPT_VERTEX_PROGRAM && program->getMain()->getOutputParameter(Parameter::SPC_LAYER))
127
0
    {
128
0
        os << "#extension GL_ARB_shader_viewport_layer_array : require\n";
129
0
    }
130
131
    // Generate dependencies.
132
0
    writeProgramDependencies(os, program);
133
0
    os << std::endl;
134
135
0
    writeMainSourceCode(os, program);
136
0
}
137
138
void GLSLProgramWriter::writeUniformBlock(std::ostream& os, const String& name, int binding,
139
                                          const UniformParameterList& uniforms)
140
0
{
141
0
    os << "layout(binding = " << binding << ", row_major) uniform " << name << " {\n";
142
143
0
    for (const auto& uparam : uniforms)
144
0
    {
145
0
        if(uparam->getType() == GCT_MATRIX_3X4 || uparam->getType() == GCT_MATRIX_2X4)
146
0
            os << "layout(column_major) ";
147
0
        writeParameter(os, uparam);
148
0
        os << ";\n";
149
0
    }
150
151
0
    os << "};\n";
152
0
}
153
154
void GLSLProgramWriter::writeMainSourceCode(std::ostream& os, Program* program)
155
0
{
156
0
    GpuProgramType gpuType = program->getType();
157
0
    if(gpuType == GPT_GEOMETRY_PROGRAM)
158
0
    {
159
0
        OGRE_EXCEPT( Exception::ERR_NOT_IMPLEMENTED,
160
0
            "Geometry Program not supported in GLSL writer ",
161
0
            "GLSLProgramWriter::writeSourceCode" );
162
0
    }
163
164
0
    const UniformParameterList& parameterList = program->getParameters();
165
166
    // Generate global variable code.
167
0
    writeUniformParametersTitle(os, program);
168
0
    os << std::endl;
169
170
0
    auto* rs = Root::getSingleton().getRenderSystem();
171
0
    auto hasSSO = rs ? rs->getCapabilities()->hasCapability(RSC_SEPARATE_SHADER_OBJECTS) : false;
172
173
    // Write the uniforms
174
0
    UniformParameterList uniforms;
175
0
    for (const auto& param : parameterList)
176
0
    {
177
0
        if(!param->isSampler())
178
0
        {
179
0
            uniforms.push_back(param);
180
0
            continue;
181
0
        }
182
0
        writeSamplerParameter(os, param);
183
0
        os << ";" << std::endl;
184
0
    }
185
0
    if (mIsVulkan && !uniforms.empty())
186
0
    {
187
0
        writeUniformBlock(os, "OgreUniforms", gpuType, uniforms);
188
0
        uniforms.clear();
189
0
    }
190
191
0
    int uniformLoc = 0;
192
0
    for (const auto& uparam : uniforms)
193
0
    {
194
0
        if(mGLSLVersion >= 430 && hasSSO)
195
0
        {
196
0
            os << "layout(location = " << uniformLoc << ") ";
197
0
            auto esize = GpuConstantDefinition::getElementSize(uparam->getType(), true) / 4;
198
0
            uniformLoc += esize * std::max<int>(uparam->getSize(), 1);
199
0
        }
200
201
0
        os << "uniform\t";
202
0
        if(mIsGLSLES)
203
0
            os << "highp\t"; // force highp to avoid precision mismatch between VP/ FP
204
0
        writeParameter(os, uparam);
205
0
        os << ";\n";
206
0
    }
207
0
    os << std::endl;
208
209
0
    Function* curFunction = program->getMain();
210
0
    const ShaderParameterList& inParams = curFunction->getInputParameters();
211
212
0
    writeFunctionTitle(os, curFunction);
213
214
    // Write inout params and fill mInputToGLStatesMap
215
0
    writeInputParameters(os, curFunction, gpuType);
216
0
    writeOutParameters(os, curFunction, gpuType);
217
218
    // The function name must always main.
219
0
    os << "void main(void) {" << std::endl;
220
221
    // Write local parameters.
222
0
    for (const auto& p : curFunction->getLocalParameters())
223
0
    {
224
0
        os << "\t";
225
0
        writeParameter(os, p);
226
0
        os << ";" << std::endl;
227
0
    }
228
0
    os << std::endl;
229
230
0
    for (const auto& pFuncInvoc : curFunction->getAtomInstances())
231
0
    {
232
0
        redirectGlobalWrites(os, pFuncInvoc, inParams, parameterList);
233
0
        for (auto& operand : pFuncInvoc->getOperandList())
234
0
        {
235
0
            const ParameterPtr& param = operand.getParameter();
236
0
            if (gpuType != GPT_VERTEX_PROGRAM || param->getSemantic() != Parameter::SPS_TEXTURE_COORDINATES)
237
0
                continue;
238
239
0
            bool isInputParam = std::find(inParams.begin(), inParams.end(), param) != inParams.end();
240
241
            // Now that every texcoord is a vec4 (passed as vertex attributes) we
242
            // have to swizzle them according the desired type.
243
0
            if (isInputParam)
244
0
                operand.setMaskToParamType();
245
0
        }
246
247
0
        os << "\t";
248
0
        pFuncInvoc->writeSourceCode(os, getTargetLanguage());
249
0
        os << std::endl;
250
0
    }
251
0
    os << "}" << std::endl;
252
0
    os << std::endl;
253
0
}
254
255
//-----------------------------------------------------------------------
256
void GLSLProgramWriter::writeInputParameters(std::ostream& os, Function* function, GpuProgramType gpuType)
257
0
{
258
0
    int psInLocation = 0;
259
260
0
    for (const auto& p : function->getInputParameters())
261
0
    {
262
0
        auto paramContent = p->getContent();
263
0
        auto paramSemantic = p->getSemantic();
264
0
        const String& paramName = p->getName();
265
266
0
        if (gpuType == GPT_FRAGMENT_PROGRAM)
267
0
        {
268
0
            if(paramContent == Parameter::SPC_POINTSPRITE_COORDINATE)
269
0
            {
270
0
                p->_rename("gl_PointCoord");
271
0
                continue;
272
0
            }
273
0
            else if(paramContent == Parameter::SPC_POINTSPRITE_SIZE)
274
0
            {
275
                // injected by matchVStoPSInterface, but only available in VS
276
0
                continue;
277
0
            }
278
0
            else if(paramSemantic == Parameter::SPS_LAYER)
279
0
            {
280
0
                p->_rename("gl_Layer");
281
0
                continue;
282
0
            }
283
0
            else if(paramSemantic == Parameter::SPS_POSITION)
284
0
            {
285
0
                p->_rename("gl_FragCoord");
286
0
                continue;
287
0
            }
288
0
            else if(paramSemantic == Parameter::SPS_FRONT_FACING)
289
0
            {
290
0
                p->_rename("gl_FrontFacing");
291
0
                continue;
292
0
            }
293
294
0
            os << "IN(";
295
0
            if(p->isHighP())
296
0
                os << "f32"; // rely on unified shader vor f32vec4 etc.
297
0
            os << mGpuConstTypeMap[p->getType()];
298
0
            os << "\t";
299
0
            os << paramName;
300
0
            os << ", " << psInLocation++ << ")\n";
301
0
        }
302
0
        else if (gpuType == GPT_VERTEX_PROGRAM)
303
0
        {
304
            // Due the fact that glsl does not have register like cg we have to rename the params
305
0
            if(paramSemantic == Parameter::SPS_TEXTURE_COORDINATES)
306
0
                p->_rename(StringUtil::format("uv%d", p->getIndex()));
307
0
            else if(paramContent == Parameter::SPC_COLOR_SPECULAR)
308
0
                p->_rename("secondary_colour");
309
0
            else
310
0
                p->_rename(mParamSemanticToNameMap[paramSemantic]);
311
0
            os << "IN(";
312
            // all uv texcoords passed by ogre are at least vec4
313
0
            if ((paramSemantic == Parameter::SPS_TEXTURE_COORDINATES) && (p->getType() < GCT_FLOAT4))
314
0
            {
315
0
                os << "vec4";
316
0
            }
317
0
            else
318
0
            {
319
                // the gl rendersystems only pass float attributes
320
0
                GpuConstantType type = p->getType();
321
0
                if(!mIsVulkan && !GpuConstantDefinition::isFloat(type))
322
0
                    type = GpuConstantType(type & ~GpuConstantDefinition::getBaseType(type));
323
0
                os << mGpuConstTypeMap[type];
324
0
            }
325
0
            os << "\t";
326
0
            os << p->getName() << ", ";
327
0
            writeParameterSemantic(os, p);  // maps to location
328
0
            os << ")\n";
329
0
        }
330
0
    }
331
0
}
332
333
//-----------------------------------------------------------------------
334
void GLSLProgramWriter::writeOutParameters(std::ostream& os, Function* function, GpuProgramType gpuType)
335
0
{
336
0
    const ShaderParameterList& outParams = function->getOutputParameters();
337
0
    int vsOutLocation = 0;
338
339
0
    for (const auto& p : outParams)
340
0
    {
341
0
        if(gpuType == GPT_VERTEX_PROGRAM)
342
0
        {
343
            // GLSL vertex program has to write always gl_Position (but this is also deprecated after version 130)
344
0
            if(p->getSemantic() == Parameter::SPS_POSITION)
345
0
            {
346
0
                p->_rename("gl_Position");
347
0
            }
348
0
            else if(p->getContent() == Parameter::SPC_POINTSPRITE_SIZE)
349
0
            {
350
0
                p->_rename("gl_PointSize");
351
0
            }
352
0
            else if(p->getSemantic() == Parameter::SPS_LAYER)
353
0
            {
354
0
                p->_rename("gl_Layer");
355
0
            }
356
0
            else
357
0
            {
358
0
                os << "OUT(";
359
0
                writeParameter(os, p);
360
0
                os << ", " << vsOutLocation++ << ")\n";
361
0
            }
362
0
        }
363
0
        else if(gpuType == GPT_FRAGMENT_PROGRAM &&
364
0
                p->getSemantic() == Parameter::SPS_COLOR)
365
0
        {
366
0
            if(p->getIndex() == 0)
367
0
            {
368
                // handled by UnifiedShader
369
0
                p->_rename("gl_FragColor");
370
0
                continue;
371
0
            }
372
373
0
            os << "OUT(vec4\t" << p->getName() << ", " << p->getIndex() << ")\n";
374
0
        }
375
0
    }
376
0
}
377
//-----------------------------------------------------------------------
378
}
379
}