LCOV - code coverage report
Current view: top level - source/renderer/backend/gl - ShaderProgram.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 517 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 26 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "ShaderProgram.h"
      21             : 
      22             : #include "graphics/PreprocessorWrapper.h"
      23             : #include "graphics/ShaderDefines.h"
      24             : #include "lib/config2.h"
      25             : #include "lib/path.h"
      26             : #include "lib/utf8.h"
      27             : #include "ps/CLogger.h"
      28             : #include "ps/Errors.h"
      29             : #include "ps/Filesystem.h"
      30             : #include "ps/Profiler2.h"
      31             : #include "ps/XMB/XMBData.h"
      32             : #include "ps/XMB/XMBStorage.h"
      33             : #include "ps/XML/Xeromyces.h"
      34             : #include "ps/algorithm.h"
      35             : #include "renderer/backend/PipelineState.h"
      36             : #include "renderer/backend/gl/Buffer.h"
      37             : #include "renderer/backend/gl/DeviceCommandContext.h"
      38             : 
      39             : #include <algorithm>
      40             : #include <cstddef>
      41             : #include <string>
      42             : #include <utility>
      43             : 
      44             : #define USE_SHADER_XML_VALIDATION 1
      45             : 
      46             : #if USE_SHADER_XML_VALIDATION
      47             : #include "ps/XML/XMLWriter.h"
      48             : #endif
      49             : 
      50             : namespace Renderer
      51             : {
      52             : 
      53             : namespace Backend
      54             : {
      55             : 
      56             : namespace GL
      57             : {
      58             : 
      59             : namespace
      60             : {
      61             : 
      62             : struct Binding
      63             : {
      64             :     Binding(int a, int b) : first(a), second(b) { }
      65             : 
      66             :     Binding() : first(-1), second(-1) { }
      67             : 
      68             :     /**
      69             :         * Returns whether this uniform attribute is active in the shader.
      70             :         * If not then there's no point calling Uniform() to set its value.
      71             :         */
      72             :     bool Active() const { return first != -1 || second != -1; }
      73             : 
      74             :     int first;
      75             :     int second;
      76             : };
      77             : 
      78           0 : int GetStreamMask(const VertexAttributeStream stream)
      79             : {
      80           0 :     return 1 << static_cast<int>(stream);
      81             : }
      82             : 
      83           0 : GLint GLSizeFromFormat(const Format format)
      84             : {
      85           0 :     GLint size = 1;
      86           0 :     if (format == Renderer::Backend::Format::R32_SFLOAT ||
      87           0 :         format == Renderer::Backend::Format::R16_SINT ||
      88             :         format == Renderer::Backend::Format::R16_SFLOAT)
      89             :         size = 1;
      90             :     else if (
      91             :         format == Renderer::Backend::Format::R8G8_UNORM ||
      92             :         format == Renderer::Backend::Format::R8G8_UINT ||
      93             :         format == Renderer::Backend::Format::R16G16_SINT ||
      94             :         format == Renderer::Backend::Format::R16G16_SFLOAT ||
      95             :         format == Renderer::Backend::Format::R32G32_SFLOAT)
      96             :         size = 2;
      97             :     else if (
      98             :         format == Renderer::Backend::Format::R16G16B16_SFLOAT ||
      99             :         format == Renderer::Backend::Format::R32G32B32_SFLOAT)
     100             :         size = 3;
     101             :     else if (
     102             :         format == Renderer::Backend::Format::R16G16B16A16_SFLOAT ||
     103             :         format == Renderer::Backend::Format::R32G32B32A32_SFLOAT ||
     104             :         format == Renderer::Backend::Format::R8G8B8A8_UNORM ||
     105             :         format == Renderer::Backend::Format::R8G8B8A8_UINT)
     106             :         size = 4;
     107             :     else
     108           0 :         debug_warn("Unsupported format.");
     109           0 :     return size;
     110             : }
     111             : 
     112           0 : GLenum GLTypeFromFormat(const Format format)
     113             : {
     114           0 :     GLenum type = GL_FLOAT;
     115           0 :     if (format == Renderer::Backend::Format::R32_SFLOAT ||
     116             :         format == Renderer::Backend::Format::R32G32_SFLOAT ||
     117           0 :         format == Renderer::Backend::Format::R32G32B32_SFLOAT ||
     118             :         format == Renderer::Backend::Format::R32G32B32A32_SFLOAT)
     119             :         type = GL_FLOAT;
     120             : #if !CONFIG2_GLES
     121           0 :     else if (format == Renderer::Backend::Format::R16_SFLOAT ||
     122           0 :         format == Renderer::Backend::Format::R16G16_SFLOAT ||
     123             :         format == Renderer::Backend::Format::R16G16B16_SFLOAT ||
     124             :         format == Renderer::Backend::Format::R16G16B16A16_SFLOAT)
     125             :         type = GL_HALF_FLOAT;
     126             : #endif
     127             :     else if (
     128             :         format == Renderer::Backend::Format::R16_SINT ||
     129             :         format == Renderer::Backend::Format::R16G16_SINT)
     130             :         type = GL_SHORT;
     131             :     else if (
     132             :         format == Renderer::Backend::Format::R8G8_UNORM ||
     133             :         format == Renderer::Backend::Format::R8G8_UINT ||
     134             :         format == Renderer::Backend::Format::R8G8B8A8_UNORM ||
     135             :         format == Renderer::Backend::Format::R8G8B8A8_UINT)
     136             :         type = GL_UNSIGNED_BYTE;
     137             :     else
     138           0 :         debug_warn("Unsupported format.");
     139           0 :     return type;
     140             : }
     141             : 
     142           0 : GLboolean NormalizedFromFormat(const Format format)
     143             : {
     144           0 :     switch (format)
     145             :     {
     146             :     case Format::R8G8_UNORM:
     147             :     case Format::R8G8B8_UNORM:
     148             :     case Format::R8G8B8A8_UNORM:
     149             :     case Format::R16_UNORM:
     150             :     case Format::R16G16_UNORM:
     151             :         return GL_TRUE;
     152           0 :     default:
     153           0 :         break;
     154             :     }
     155           0 :     return GL_FALSE;
     156             : }
     157             : 
     158           0 : int GetAttributeLocationFromStream(
     159             :     CDevice* device, const VertexAttributeStream stream)
     160             : {
     161             :     // Old mapping makes sense only if we have an old/low-end hardware. Else we
     162             :     // need to use sequential numbering to fix #3054. We use presence of
     163             :     // compute shaders as a check that the hardware has universal CUs.
     164           0 :     if (device->GetCapabilities().computeShaders)
     165             :     {
     166           0 :         return static_cast<int>(stream);
     167             :     }
     168             :     else
     169             :     {
     170             :         // Map known semantics onto the attribute locations documented by NVIDIA:
     171             :         //  https://download.nvidia.com/developer/Papers/2005/OpenGL_2.0/NVIDIA_OpenGL_2.0_Support.pdf
     172             :         //  https://developer.download.nvidia.com/opengl/glsl/glsl_release_notes.pdf
     173           0 :         switch (stream)
     174             :         {
     175             :         case VertexAttributeStream::POSITION: return 0;
     176             :         case VertexAttributeStream::NORMAL: return 2;
     177           0 :         case VertexAttributeStream::COLOR: return 3;
     178           0 :         case VertexAttributeStream::UV0: return 8;
     179           0 :         case VertexAttributeStream::UV1: return 9;
     180           0 :         case VertexAttributeStream::UV2: return 10;
     181           0 :         case VertexAttributeStream::UV3: return 11;
     182           0 :         case VertexAttributeStream::UV4: return 12;
     183           0 :         case VertexAttributeStream::UV5: return 13;
     184           0 :         case VertexAttributeStream::UV6: return 14;
     185           0 :         case VertexAttributeStream::UV7: return 15;
     186             :         }
     187             :     }
     188             : 
     189           0 :     debug_warn("Invalid attribute semantics");
     190             :     return 0;
     191             : }
     192             : 
     193           0 : bool PreprocessShaderFile(
     194             :     const CShaderDefines& defines, const VfsPath& path, const char* stage,
     195             :     CStr& source, std::vector<VfsPath>& fileDependencies)
     196             : {
     197           0 :     CVFSFile file;
     198           0 :     if (file.Load(g_VFS, path) != PSRETURN_OK)
     199             :     {
     200           0 :         LOGERROR("Failed to load shader file: '{}'", path.string8());
     201           0 :         return false;
     202             :     }
     203             : 
     204           0 :     CPreprocessorWrapper preprocessor(
     205           0 :         [&fileDependencies](const CStr& includePath, CStr& out) -> bool
     206             :         {
     207           0 :             const VfsPath includeFilePath{L"shaders/glsl/" + wstring_from_utf8(includePath)};
     208             :             // Add dependencies anyway to reload the shader when the file is
     209             :             // appeared.
     210           0 :             fileDependencies.push_back(includeFilePath);
     211           0 :             CVFSFile includeFile;
     212           0 :             if (includeFile.Load(g_VFS, includeFilePath) != PSRETURN_OK)
     213             :             {
     214           0 :                 LOGERROR("Failed to load shader include file: '{}'", includeFilePath.string8());
     215           0 :                 return false;
     216             :             }
     217           0 :             out = includeFile.GetAsString();
     218           0 :             return true;
     219           0 :         });
     220           0 :     preprocessor.AddDefines(defines);
     221           0 :     preprocessor.AddDefine(stage, "1");
     222             : 
     223             : #if CONFIG2_GLES
     224             :     // GLES defines the macro "GL_ES" in its GLSL preprocessor,
     225             :     // but since we run our own preprocessor first, we need to explicitly
     226             :     // define it here
     227             :     preprocessor.AddDefine("GL_ES", "1");
     228             : #endif
     229             : 
     230           0 :     source = preprocessor.Preprocess(file.GetAsString());
     231             : 
     232           0 :     return true;
     233           0 : }
     234             : 
     235           0 : bool CompileGLSL(GLuint shader, const VfsPath& file, const CStr& code)
     236             : {
     237           0 :     const char* codeString = code.c_str();
     238           0 :     GLint codeLength = code.length();
     239           0 :     glShaderSource(shader, 1, &codeString, &codeLength);
     240             : 
     241           0 :     glCompileShader(shader);
     242             : 
     243           0 :     GLint ok = 0;
     244           0 :     glGetShaderiv(shader, GL_COMPILE_STATUS, &ok);
     245             : 
     246           0 :     GLint length = 0;
     247           0 :     glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
     248             : 
     249             :     // Apparently sometimes GL_INFO_LOG_LENGTH is incorrectly reported as 0
     250             :     // (http://code.google.com/p/android/issues/detail?id=9953)
     251           0 :     if (!ok && length == 0)
     252           0 :         length = 4096;
     253             : 
     254           0 :     if (length > 1)
     255             :     {
     256           0 :         std::unique_ptr<char[]> infolog = std::make_unique<char[]>(length);
     257           0 :         glGetShaderInfoLog(shader, length, nullptr, infolog.get());
     258             : 
     259           0 :         if (ok)
     260           0 :             LOGMESSAGE("Info when compiling shader '{}':\n{}", file.string8(), infolog.get());
     261             :         else
     262           0 :             LOGERROR("Failed to compile shader '{}':\n{}", file.string8(), infolog.get());
     263           0 :     }
     264             : 
     265           0 :     return ok;
     266             : }
     267             : 
     268             : } // anonymous namespace
     269             : 
     270           0 : IDevice* CVertexInputLayout::GetDevice()
     271             : {
     272           0 :     return m_Device;
     273             : }
     274             : 
     275           0 : CShaderProgram::CShaderProgram(
     276             :     CDevice* device, const CStr& name,
     277             :     const VfsPath& programPath, std::span<const std::tuple<VfsPath, GLenum>> shaderStages,
     278             :     const CShaderDefines& defines,
     279             :     const std::map<CStrIntern, int>& vertexAttribs,
     280           0 :     int streamflags) :
     281           0 :     m_StreamFlags(streamflags), m_ValidStreams(0),
     282           0 :     m_Device(device), m_Name(name),
     283           0 :     m_VertexAttribs(vertexAttribs)
     284             : {
     285           0 :     for (std::map<CStrIntern, int>::iterator it = m_VertexAttribs.begin(); it != m_VertexAttribs.end(); ++it)
     286           0 :         m_ActiveVertexAttributes.emplace_back(it->second);
     287           0 :     std::sort(m_ActiveVertexAttributes.begin(), m_ActiveVertexAttributes.end());
     288             : 
     289           0 :     m_Program = 0;
     290           0 :     m_FileDependencies = {programPath};
     291           0 :     for (const auto& shaderStage : shaderStages)
     292           0 :         m_FileDependencies.emplace_back(std::get<0>(shaderStage));
     293             : 
     294             :     // TODO: replace by scoped bind.
     295           0 :     m_Device->GetActiveCommandContext()->SetGraphicsPipelineState(
     296           0 :         MakeDefaultGraphicsPipelineStateDesc());
     297             : 
     298           0 :     std::vector<VfsPath> newFileDependencies = {programPath};
     299           0 :     for (const auto& [path, type] : shaderStages)
     300             :     {
     301           0 :         GLuint shader = glCreateShader(type);
     302           0 :         newFileDependencies.emplace_back(path);
     303             : #if !CONFIG2_GLES
     304           0 :         if (m_Device->GetCapabilities().debugLabels)
     305           0 :             glObjectLabel(GL_SHADER, shader, -1, path.string8().c_str());
     306             : #endif
     307           0 :         m_ShaderStages.emplace_back(type, shader);
     308           0 :         const char* stageDefine = "STAGE_UNDEFINED";
     309           0 :         switch (type)
     310             :         {
     311           0 :         case GL_VERTEX_SHADER:
     312           0 :             stageDefine = "STAGE_VERTEX";
     313           0 :             break;
     314           0 :         case GL_FRAGMENT_SHADER:
     315           0 :             stageDefine = "STAGE_FRAGMENT";
     316           0 :             break;
     317             : #if !CONFIG2_GLES
     318           0 :         case GL_COMPUTE_SHADER:
     319           0 :             stageDefine = "STAGE_COMPUTE";
     320           0 :             break;
     321             : #endif
     322             :         default:
     323             :             break;
     324             :         }
     325           0 :         CStr source;
     326           0 :         if (!PreprocessShaderFile(defines, path, stageDefine, source, newFileDependencies))
     327           0 :             return;
     328           0 :         if (source.empty())
     329             :         {
     330           0 :             LOGERROR("Failed to preprocess shader: '{}'", path.string8());
     331           0 :             return;
     332             :         }
     333             : #if CONFIG2_GLES
     334             :         // Ugly hack to replace desktop GLSL 1.10/1.20 with GLSL ES 1.00,
     335             :         // and also to set default float precision for fragment shaders
     336             :         PS::ReplaceSubrange(source, "#version 110\n", "#version 100\nprecision highp float;\n");
     337             :         PS::ReplaceSubrange(source, "#version 110\r\n", "#version 100\nprecision highp float;\n");
     338             :         PS::ReplaceSubrange(source, "#version 120\n", "#version 100\nprecision highp float;\n");
     339             :         PS::ReplaceSubrange(source, "#version 120\r\n", "#version 100\nprecision highp float;\n");
     340             : #endif
     341           0 :         if (!CompileGLSL(shader, path, source))
     342             :             return;
     343             :     }
     344             : 
     345           0 :     m_FileDependencies = std::move(newFileDependencies);
     346             : 
     347           0 :     if (!Link(programPath))
     348             :         return;
     349           0 : }
     350             : 
     351           0 : CShaderProgram::~CShaderProgram()
     352             : {
     353           0 :     if (m_Program)
     354           0 :         glDeleteProgram(m_Program);
     355             : 
     356           0 :     for (ShaderStage& stage : m_ShaderStages)
     357           0 :         glDeleteShader(stage.shader);
     358           0 : }
     359             : 
     360           0 : bool CShaderProgram::Link(const VfsPath& path)
     361             : {
     362           0 :     ENSURE(!m_Program);
     363           0 :     m_Program = glCreateProgram();
     364             : 
     365             : #if !CONFIG2_GLES
     366           0 :     if (m_Device->GetCapabilities().debugLabels)
     367             :     {
     368           0 :         glObjectLabel(GL_PROGRAM, m_Program, -1, m_Name.c_str());
     369             :     }
     370             : #endif
     371             : 
     372           0 :     for (ShaderStage& stage : m_ShaderStages)
     373             :     {
     374           0 :         glAttachShader(m_Program, stage.shader);
     375             :     }
     376             : 
     377             :     // Set up the attribute bindings explicitly, since apparently drivers
     378             :     // don't always pick the most efficient bindings automatically,
     379             :     // and also this lets us hardcode indexes into VertexPointer etc
     380           0 :     for (std::map<CStrIntern, int>::iterator it = m_VertexAttribs.begin(); it != m_VertexAttribs.end(); ++it)
     381           0 :         glBindAttribLocation(m_Program, it->second, it->first.c_str());
     382             : 
     383           0 :     glLinkProgram(m_Program);
     384             : 
     385           0 :     GLint ok = 0;
     386           0 :     glGetProgramiv(m_Program, GL_LINK_STATUS, &ok);
     387             : 
     388           0 :     GLint length = 0;
     389           0 :     glGetProgramiv(m_Program, GL_INFO_LOG_LENGTH, &length);
     390             : 
     391           0 :     if (!ok && length == 0)
     392           0 :         length = 4096;
     393             : 
     394           0 :     if (length > 1)
     395             :     {
     396           0 :         char* infolog = new char[length];
     397           0 :         glGetProgramInfoLog(m_Program, length, NULL, infolog);
     398             : 
     399           0 :         if (ok)
     400           0 :             LOGMESSAGE("Info when linking program '{}':\n{}", path.string8(), infolog);
     401             :         else
     402           0 :             LOGERROR("Failed to link program '{}':\n{}", path.string8(), infolog);
     403             : 
     404           0 :         delete[] infolog;
     405             :     }
     406             : 
     407           0 :     if (!ok)
     408             :         return false;
     409             : 
     410           0 :     Bind(nullptr);
     411             : 
     412             :     // Reorder sampler units to decrease redundant texture unit changes when
     413             :     // samplers bound in a different order.
     414           0 :     const std::unordered_map<CStrIntern, int> requiredUnits =
     415             :     {
     416           0 :         {CStrIntern("baseTex"), 0},
     417           0 :         {CStrIntern("normTex"), 1},
     418           0 :         {CStrIntern("specTex"), 2},
     419           0 :         {CStrIntern("aoTex"), 3},
     420           0 :         {CStrIntern("shadowTex"), 4},
     421           0 :         {CStrIntern("losTex"), 5},
     422           0 :     };
     423             : 
     424           0 :     std::vector<uint8_t> occupiedUnits;
     425             : 
     426             : #if !CONFIG2_GLES
     427           0 :     const bool isStorageSupported{m_Device->GetCapabilities().storage};
     428           0 :     if (isStorageSupported)
     429             :     {
     430           0 :         constexpr GLint maxBlockNameLength{128};
     431           0 :         char name[maxBlockNameLength];
     432             : 
     433           0 :         GLint maxUniformBlockNameLength{0};
     434           0 :         glGetProgramInterfaceiv(m_Program, GL_UNIFORM_BLOCK, GL_MAX_NAME_LENGTH, &maxUniformBlockNameLength);
     435             : 
     436           0 :         GLint numberOfActiveUniformBlocks{0};
     437           0 :         glGetProgramInterfaceiv(m_Program, GL_UNIFORM_BLOCK, GL_ACTIVE_RESOURCES, &numberOfActiveUniformBlocks);
     438             :         // Currently we support the only one uniform buffer per shader.
     439           0 :         if (numberOfActiveUniformBlocks == 1)
     440             :         {
     441           0 :             GLsizei length{0};
     442           0 :             glGetProgramResourceName(m_Program, GL_UNIFORM_BLOCK, 0, maxBlockNameLength, &length, name);
     443             : 
     444           0 :             const GLuint location{glGetProgramResourceIndex(m_Program, GL_UNIFORM_BLOCK, name)};
     445           0 :             glUniformBlockBinding(m_Program, location, location);
     446             : 
     447           0 :             m_UniformBufferLocation = location;
     448             :         }
     449             : 
     450           0 :         GLint maxStorageNameLength{0};
     451           0 :         glGetProgramInterfaceiv(m_Program, GL_SHADER_STORAGE_BLOCK, GL_MAX_NAME_LENGTH, &maxStorageNameLength);
     452           0 :         ENSURE(maxStorageNameLength <= maxBlockNameLength);
     453           0 :         GLint numberOfActiveStorages{0};
     454           0 :         glGetProgramInterfaceiv(m_Program, GL_SHADER_STORAGE_BLOCK, GL_ACTIVE_RESOURCES, &numberOfActiveStorages);
     455           0 :         for (GLint index{0}; index < numberOfActiveStorages; ++index)
     456             :         {
     457           0 :             GLsizei length{0};
     458           0 :             glGetProgramResourceName(m_Program, GL_SHADER_STORAGE_BLOCK, index, maxBlockNameLength, &length, name);
     459             : 
     460           0 :             const GLuint location{glGetProgramResourceIndex(m_Program, GL_SHADER_STORAGE_BLOCK, name)};
     461           0 :             glShaderStorageBlockBinding(m_Program, location, location);
     462             : 
     463           0 :             const CStrIntern nameIntern(name);
     464             : 
     465           0 :             m_BindingSlotsMapping[nameIntern] = m_BindingSlots.size();
     466           0 :             BindingSlot bindingSlot{};
     467           0 :             bindingSlot.name = nameIntern;
     468           0 :             bindingSlot.location = location;
     469           0 :             bindingSlot.isStorageBuffer = true;
     470           0 :             m_BindingSlots.emplace_back(std::move(bindingSlot));
     471             :         }
     472             :     }
     473             : #endif
     474             : 
     475           0 :     GLint numUniforms = 0;
     476           0 :     glGetProgramiv(m_Program, GL_ACTIVE_UNIFORMS, &numUniforms);
     477           0 :     for (GLint i = 0; i < numUniforms; ++i)
     478             :     {
     479             :         // TODO: use GL_ACTIVE_UNIFORM_MAX_LENGTH for the size.
     480           0 :         char name[256] = {0};
     481           0 :         GLsizei nameLength = 0;
     482           0 :         GLint size = 0;
     483           0 :         GLenum type = 0;
     484           0 :         glGetActiveUniform(m_Program, i, ARRAY_SIZE(name), &nameLength, &size, &type, name);
     485             : 
     486           0 :         const GLint location = glGetUniformLocation(m_Program, name);
     487             : 
     488             :         // OpenGL specification is a bit vague about a name returned by glGetActiveUniform.
     489             :         // NVIDIA drivers return uniform name with "[0]", Intel Windows drivers without;
     490           0 :         while (nameLength > 3 &&
     491           0 :             name[nameLength - 3] == '[' &&
     492           0 :             name[nameLength - 2] == '0' &&
     493           0 :             name[nameLength - 1] == ']')
     494             :         {
     495           0 :             nameLength -= 3;
     496             :         }
     497           0 :         name[nameLength] = 0;
     498             : 
     499           0 :         const CStrIntern nameIntern(name);
     500             : 
     501           0 :         m_BindingSlotsMapping[nameIntern] = m_BindingSlots.size();
     502           0 :         BindingSlot bindingSlot{};
     503           0 :         bindingSlot.name = nameIntern;
     504           0 :         bindingSlot.location = location;
     505           0 :         bindingSlot.size = size;
     506           0 :         bindingSlot.type = type;
     507           0 :         bindingSlot.isTexture = false;
     508           0 :         bindingSlot.isStorageBuffer = false;
     509             : 
     510             : #define CASE(TYPE, ELEMENT_TYPE, ELEMENT_COUNT) \
     511             :             case GL_ ## TYPE: \
     512             :                 bindingSlot.elementType = GL_ ## ELEMENT_TYPE; \
     513             :                 bindingSlot.elementCount = ELEMENT_COUNT; \
     514             :                 break;
     515             : 
     516           0 :         switch (type)
     517             :         {
     518           0 :             CASE(FLOAT, FLOAT, 1);
     519           0 :             CASE(FLOAT_VEC2, FLOAT, 2);
     520           0 :             CASE(FLOAT_VEC3, FLOAT, 3);
     521           0 :             CASE(FLOAT_VEC4, FLOAT, 4);
     522           0 :             CASE(INT, INT, 1);
     523           0 :             CASE(FLOAT_MAT2, FLOAT, 4);
     524           0 :             CASE(FLOAT_MAT3, FLOAT, 9);
     525           0 :             CASE(FLOAT_MAT4, FLOAT, 16);
     526             : #if !CONFIG2_GLES // GL ES 2.0 doesn't support non-square matrices.
     527           0 :             CASE(FLOAT_MAT2x3, FLOAT, 6);
     528           0 :             CASE(FLOAT_MAT2x4, FLOAT, 8);
     529           0 :             CASE(FLOAT_MAT3x2, FLOAT, 6);
     530           0 :             CASE(FLOAT_MAT3x4, FLOAT, 12);
     531           0 :             CASE(FLOAT_MAT4x2, FLOAT, 8);
     532           0 :             CASE(FLOAT_MAT4x3, FLOAT, 12);
     533             : #endif
     534             :         }
     535             : #undef CASE
     536             : 
     537             :         // Assign sampler uniforms to sequential texture units.
     538           0 :         switch (type)
     539             :         {
     540           0 :         case GL_SAMPLER_2D:
     541           0 :             bindingSlot.elementType = GL_TEXTURE_2D;
     542           0 :             bindingSlot.isTexture = true;
     543           0 :             break;
     544           0 :         case GL_SAMPLER_CUBE:
     545           0 :             bindingSlot.elementType = GL_TEXTURE_CUBE_MAP;
     546           0 :             bindingSlot.isTexture = true;
     547           0 :             break;
     548             : #if !CONFIG2_GLES
     549           0 :         case GL_SAMPLER_2D_SHADOW:
     550           0 :             bindingSlot.elementType = GL_TEXTURE_2D;
     551           0 :             bindingSlot.isTexture = true;
     552           0 :             break;
     553           0 :         case GL_IMAGE_2D:
     554           0 :             bindingSlot.elementType = GL_IMAGE_2D;
     555           0 :             bindingSlot.isTexture = true;
     556           0 :             m_HasImageUniforms = true;
     557           0 :             break;
     558             : #endif
     559             :         default:
     560             :             break;
     561             :         }
     562             : 
     563           0 :         if (bindingSlot.isTexture)
     564             :         {
     565           0 :             const auto it = requiredUnits.find(nameIntern);
     566           0 :             const int unit = it == requiredUnits.end() ? -1 : it->second;
     567           0 :             bindingSlot.elementCount = unit;
     568           0 :             if (unit != -1)
     569             :             {
     570           0 :                 if (unit >= static_cast<int>(occupiedUnits.size()))
     571           0 :                     occupiedUnits.resize(unit + 1);
     572           0 :                 occupiedUnits[unit] = true;
     573             :             }
     574             :         }
     575             : 
     576           0 :         if (bindingSlot.elementType == 0)
     577             :         {
     578           0 :             LOGERROR("CShaderProgram::Link: unsupported uniform type: 0x{:04x}", type);
     579             :         }
     580             : 
     581             : #if !CONFIG2_GLES
     582           0 :         if (isStorageSupported)
     583             :         {
     584           0 :             GLuint uniformIndex{0};
     585           0 :             const GLchar* nameToQuery{name};
     586           0 :             glGetUniformIndices(m_Program, 1, &nameToQuery, &uniformIndex);
     587             : 
     588           0 :             GLint uniformOffset{0};
     589           0 :             glGetActiveUniformsiv(m_Program, 1, &uniformIndex, GL_UNIFORM_OFFSET, &uniformOffset);
     590             : 
     591             :             // According to the OpenGL spec:
     592             :             //  https://registry.khronos.org/OpenGL-Refpages/es3/html/glGetActiveUniformsiv.xhtml
     593             :             //  For uniforms in the default uniform block, -1 will be returned.
     594           0 :             if (uniformOffset >= 0)
     595             :             {
     596           0 :                 const uint32_t sizeInBytes{static_cast<uint32_t>(bindingSlot.size * bindingSlot.elementCount * sizeof(float))};
     597           0 :                 m_UniformBufferSize = std::max(m_UniformBufferSize, uniformOffset + sizeInBytes);
     598           0 :                 bindingSlot.location = -1;
     599           0 :                 bindingSlot.offset = uniformOffset;
     600             :             }
     601             :         }
     602             : #endif
     603             : 
     604           0 :         m_BindingSlots.emplace_back(std::move(bindingSlot));
     605             :     }
     606             : 
     607           0 :     for (BindingSlot& bindingSlot : m_BindingSlots)
     608             :     {
     609           0 :         if (!bindingSlot.isTexture)
     610           0 :             continue;
     611           0 :         if (bindingSlot.elementCount == -1)
     612             :         {
     613             :             // We need to find a minimal available unit.
     614             :             int unit = 0;
     615           0 :             while (unit < static_cast<int>(occupiedUnits.size()) && occupiedUnits[unit])
     616           0 :                 ++unit;
     617           0 :             if (unit >= static_cast<int>(occupiedUnits.size()))
     618           0 :                 occupiedUnits.resize(unit + 1);
     619           0 :             occupiedUnits[unit] = true;
     620           0 :             bindingSlot.elementCount = unit;
     621             :         }
     622             :         // Link uniform to unit.
     623           0 :         glUniform1i(bindingSlot.location, bindingSlot.elementCount);
     624             :     }
     625             : 
     626           0 :     if (m_UniformBufferSize > 0 && m_UniformBufferLocation != -1)
     627             :     {
     628           0 :         m_UniformBuffer = m_Device->CreateBuffer(
     629             :             "ShaderProgramUniformBuffer", IBuffer::Type::UNIFORM, m_UniformBufferSize,
     630           0 :             IBuffer::Usage::DYNAMIC | IBuffer::Usage::TRANSFER_DST);
     631             :     }
     632             : 
     633             :     // TODO: verify that we're not using more samplers than is supported
     634             : 
     635           0 :     Unbind();
     636             : 
     637           0 :     return true;
     638             : }
     639             : 
     640           0 : void CShaderProgram::Bind(CShaderProgram* previousShaderProgram)
     641             : {
     642           0 :     ENSURE(this != previousShaderProgram);
     643             : 
     644           0 :     glUseProgram(m_Program);
     645             : #if !CONFIG2_GLES
     646           0 :     if (m_UniformBuffer)
     647           0 :         glBindBufferBase(GL_UNIFORM_BUFFER, m_UniformBufferLocation, m_UniformBuffer->As<CBuffer>()->GetHandle());
     648             : #endif
     649             : 
     650           0 :     if (previousShaderProgram)
     651             :     {
     652           0 :         std::vector<int>::iterator itPrevious = previousShaderProgram->m_ActiveVertexAttributes.begin();
     653           0 :         std::vector<int>::iterator itNext = m_ActiveVertexAttributes.begin();
     654           0 :         while (
     655           0 :             itPrevious != previousShaderProgram->m_ActiveVertexAttributes.end() ||
     656           0 :             itNext != m_ActiveVertexAttributes.end())
     657             :         {
     658           0 :             if (itPrevious != previousShaderProgram->m_ActiveVertexAttributes.end() &&
     659           0 :                 itNext != m_ActiveVertexAttributes.end())
     660             :             {
     661           0 :                 if (*itPrevious == *itNext)
     662             :                 {
     663           0 :                     ++itPrevious;
     664           0 :                     ++itNext;
     665             :                 }
     666           0 :                 else if (*itPrevious < *itNext)
     667             :                 {
     668           0 :                     glDisableVertexAttribArray(*itPrevious);
     669           0 :                     ++itPrevious;
     670             :                 }
     671           0 :                 else if (*itPrevious > *itNext)
     672             :                 {
     673           0 :                     glEnableVertexAttribArray(*itNext);
     674           0 :                     ++itNext;
     675             :                 }
     676             :             }
     677           0 :             else if (itPrevious != previousShaderProgram->m_ActiveVertexAttributes.end())
     678             :             {
     679           0 :                 glDisableVertexAttribArray(*itPrevious);
     680           0 :                 ++itPrevious;
     681             :             }
     682           0 :             else if (itNext != m_ActiveVertexAttributes.end())
     683             :             {
     684           0 :                 glEnableVertexAttribArray(*itNext);
     685           0 :                 ++itNext;
     686             :             }
     687             :         }
     688             :     }
     689             :     else
     690             :     {
     691           0 :         for (const int index : m_ActiveVertexAttributes)
     692           0 :             glEnableVertexAttribArray(index);
     693             :     }
     694             : 
     695           0 :     m_ValidStreams = 0;
     696           0 : }
     697             : 
     698           0 : void CShaderProgram::Unbind()
     699             : {
     700           0 :     glUseProgram(0);
     701             : 
     702           0 :     for (const int index : m_ActiveVertexAttributes)
     703           0 :         glDisableVertexAttribArray(index);
     704           0 : }
     705             : 
     706           0 : int32_t CShaderProgram::GetBindingSlot(const CStrIntern name) const
     707             : {
     708           0 :     auto it = m_BindingSlotsMapping.find(name);
     709           0 :     return it == m_BindingSlotsMapping.end() ? -1 : it->second;
     710             : }
     711             : 
     712           0 : CShaderProgram::TextureUnit CShaderProgram::GetTextureUnit(const int32_t bindingSlot)
     713             : {
     714           0 :     if (bindingSlot < 0 || bindingSlot >= static_cast<int32_t>(m_BindingSlots.size()))
     715           0 :         return { 0, 0, 0 };
     716           0 :     TextureUnit textureUnit;
     717           0 :     textureUnit.type = m_BindingSlots[bindingSlot].type;
     718           0 :     textureUnit.target = m_BindingSlots[bindingSlot].elementType;
     719           0 :     textureUnit.unit = m_BindingSlots[bindingSlot].elementCount;
     720           0 :     return textureUnit;
     721             : }
     722             : 
     723           0 : GLuint CShaderProgram::GetStorageBuffer(const int32_t bindingSlot)
     724             : {
     725           0 :     if (bindingSlot < 0 || bindingSlot >= static_cast<int32_t>(m_BindingSlots.size()))
     726             :         return 0;
     727           0 :     if (!m_BindingSlots[bindingSlot].isStorageBuffer)
     728           0 :         LOGERROR("CShaderProgramGLSL::GetStorageBuffer(): Invalid slot (expected storage buffer): "
     729             :             "'{}'", m_BindingSlots[bindingSlot].name.string());
     730           0 :     return m_BindingSlots[bindingSlot].location;
     731             : }
     732             : 
     733           0 : void CShaderProgram::SetUniform(const int32_t bindingSlot, const float value)
     734             : {
     735           0 :     if (bindingSlot < 0 || bindingSlot >= static_cast<int32_t>(m_BindingSlots.size()))
     736             :         return;
     737           0 :     if (m_BindingSlots[bindingSlot].type != GL_FLOAT ||
     738           0 :         m_BindingSlots[bindingSlot].size != 1)
     739             :     {
     740           0 :         LOGERROR("CShaderProgramGLSL::SetUniform(): Invalid uniform type (expected float) '{}'",
     741             :             m_BindingSlots[bindingSlot].name.string());
     742           0 :         return;
     743             :     }
     744           0 :     glUniform1f(m_BindingSlots[bindingSlot].location, value);
     745             : }
     746             : 
     747           0 : void CShaderProgram::SetUniform(
     748             :     const int32_t bindingSlot,
     749             :     const float valueX, const float valueY)
     750             : {
     751           0 :     if (bindingSlot < 0 || bindingSlot >= static_cast<int32_t>(m_BindingSlots.size()))
     752             :         return;
     753           0 :     if (m_BindingSlots[bindingSlot].type != GL_FLOAT_VEC2 ||
     754           0 :         m_BindingSlots[bindingSlot].size != 1)
     755             :     {
     756           0 :         LOGERROR("CShaderProgramGLSL::SetUniform(): Invalid uniform type (expected vec2) '{}'",
     757             :             m_BindingSlots[bindingSlot].name.string());
     758           0 :         return;
     759             :     }
     760           0 :     glUniform2f(m_BindingSlots[bindingSlot].location, valueX, valueY);
     761             : }
     762             : 
     763           0 : void CShaderProgram::SetUniform(
     764             :     const int32_t bindingSlot,
     765             :     const float valueX, const float valueY, const float valueZ)
     766             : {
     767           0 :     if (bindingSlot < 0 || bindingSlot >= static_cast<int32_t>(m_BindingSlots.size()))
     768             :         return;
     769           0 :     if (m_BindingSlots[bindingSlot].type != GL_FLOAT_VEC3 ||
     770           0 :         m_BindingSlots[bindingSlot].size != 1)
     771             :     {
     772           0 :         LOGERROR("CShaderProgramGLSL::SetUniform(): Invalid uniform type (expected vec3) '{}'",
     773             :             m_BindingSlots[bindingSlot].name.string());
     774           0 :         return;
     775             :     }
     776           0 :     glUniform3f(m_BindingSlots[bindingSlot].location, valueX, valueY, valueZ);
     777             : }
     778             : 
     779           0 : void CShaderProgram::SetUniform(
     780             :     const int32_t bindingSlot,
     781             :     const float valueX, const float valueY,
     782             :     const float valueZ, const float valueW)
     783             : {
     784           0 :     if (bindingSlot < 0 || bindingSlot >= static_cast<int32_t>(m_BindingSlots.size()))
     785             :         return;
     786           0 :     if (m_BindingSlots[bindingSlot].type != GL_FLOAT_VEC4 ||
     787           0 :         m_BindingSlots[bindingSlot].size != 1)
     788             :     {
     789           0 :         LOGERROR("CShaderProgram::SetUniform(): Invalid uniform type (expected vec4) '{}'",
     790             :             m_BindingSlots[bindingSlot].name.string());
     791           0 :         return;
     792             :     }
     793           0 :     glUniform4f(m_BindingSlots[bindingSlot].location, valueX, valueY, valueZ, valueW);
     794             : }
     795             : 
     796           0 : void CShaderProgram::SetUniform(
     797             :     const int32_t bindingSlot, std::span<const float> values)
     798             : {
     799           0 :     if (bindingSlot < 0 || bindingSlot >= static_cast<int32_t>(m_BindingSlots.size()))
     800             :         return;
     801           0 :     if (m_BindingSlots[bindingSlot].elementType != GL_FLOAT)
     802             :     {
     803           0 :         LOGERROR("CShaderProgram::SetUniform(): Invalid uniform element type (expected float) '{}'",
     804             :             m_BindingSlots[bindingSlot].name.string());
     805           0 :         return;
     806             :     }
     807           0 :     if (m_BindingSlots[bindingSlot].size == 1 && m_BindingSlots[bindingSlot].elementCount > static_cast<GLint>(values.size()))
     808             :     {
     809           0 :         LOGERROR(
     810             :             "CShaderProgram::SetUniform(): Invalid uniform element count (expected: {} passed: {}) "
     811             :                 "'{}'", m_BindingSlots[bindingSlot].elementCount, values.size(),
     812             :                 m_BindingSlots[bindingSlot].name.string());
     813           0 :         return;
     814             :     }
     815           0 :     const GLint location = m_BindingSlots[bindingSlot].location;
     816           0 :     const GLenum type = m_BindingSlots[bindingSlot].type;
     817             : 
     818           0 :     if (location == -1)
     819             :     {
     820           0 :         const uint32_t sizeInBytes{
     821           0 :             static_cast<uint32_t>(m_BindingSlots[bindingSlot].size * m_BindingSlots[bindingSlot].elementCount * sizeof(float))};
     822           0 :         const uint32_t dataSizeToUpload{std::min(
     823           0 :             static_cast<uint32_t>(values.size() * sizeof(float)), sizeInBytes)};
     824           0 :         m_Device->GetActiveCommandContext()->UploadBufferRegion(
     825           0 :             m_UniformBuffer.get(), values.data(), m_BindingSlots[bindingSlot].offset, dataSizeToUpload);
     826           0 :         return;
     827             :     }
     828             : 
     829           0 :     if (type == GL_FLOAT)
     830           0 :         glUniform1fv(location, 1, values.data());
     831           0 :     else if (type == GL_FLOAT_VEC2)
     832           0 :         glUniform2fv(location, 1, values.data());
     833           0 :     else if (type == GL_FLOAT_VEC3)
     834           0 :         glUniform3fv(location, 1, values.data());
     835           0 :     else if (type == GL_FLOAT_VEC4)
     836           0 :         glUniform4fv(location, 1, values.data());
     837           0 :     else if (type == GL_FLOAT_MAT4)
     838             :     {
     839             :         // For case of array of matrices we might pass less number of matrices.
     840           0 :         const GLint size = std::min(
     841           0 :             m_BindingSlots[bindingSlot].size, static_cast<GLint>(values.size() / 16));
     842           0 :         glUniformMatrix4fv(location, size, GL_FALSE, values.data());
     843             :     }
     844             :     else
     845           0 :         LOGERROR("CShaderProgram::SetUniform(): Invalid uniform type (expected float, vec2, vec3, "
     846             :             "vec4, mat4) '{}'", m_BindingSlots[bindingSlot].name.string());
     847             : }
     848             : 
     849           0 : void CShaderProgram::VertexAttribPointer(
     850             :     const VertexAttributeStream stream, const Format format,
     851             :     const uint32_t offset, const uint32_t stride,
     852             :     [[maybe_unused]] const VertexAttributeRate rate, const void* data)
     853             : {
     854           0 :     const int attributeLocation = GetAttributeLocationFromStream(m_Device, stream);
     855           0 :     std::vector<int>::const_iterator it =
     856           0 :         std::lower_bound(m_ActiveVertexAttributes.begin(), m_ActiveVertexAttributes.end(), attributeLocation);
     857           0 :     if (it == m_ActiveVertexAttributes.end() || *it != attributeLocation)
     858           0 :         return;
     859           0 :     const GLint size = GLSizeFromFormat(format);
     860           0 :     const GLenum type = GLTypeFromFormat(format);
     861           0 :     const GLboolean normalized = NormalizedFromFormat(format);
     862           0 :     glVertexAttribPointer(
     863             :         attributeLocation, size, type, normalized, stride,
     864           0 :         static_cast<const std::uint8_t*>(data) + offset);
     865             : #if CONFIG2_GLES
     866             :     ENSURE(!m_Device->GetCapabilities().instancing);
     867             : #else
     868           0 :     if (rate == VertexAttributeRate::PER_INSTANCE)
     869           0 :         ENSURE(m_Device->GetCapabilities().instancing);
     870           0 :     if (m_Device->GetCapabilities().instancing)
     871             :     {
     872           0 :         glVertexAttribDivisorARB(attributeLocation, rate == VertexAttributeRate::PER_INSTANCE ? 1 : 0);
     873             :     }
     874             : #endif
     875           0 :     m_ValidStreams |= GetStreamMask(stream);
     876             : }
     877             : 
     878           0 : std::vector<VfsPath> CShaderProgram::GetFileDependencies() const
     879             : {
     880           0 :     return m_FileDependencies;
     881             : }
     882             : 
     883             : // static
     884           0 : std::unique_ptr<CShaderProgram> CShaderProgram::Create(CDevice* device, const CStr& name, const CShaderDefines& baseDefines)
     885             : {
     886           0 :     PROFILE2("loading shader");
     887           0 :     PROFILE2_ATTR("name: %s", name.c_str());
     888             : 
     889           0 :     VfsPath xmlFilename = L"shaders/" + wstring_from_utf8(name) + L".xml";
     890             : 
     891           0 :     CXeromyces XeroFile;
     892           0 :     PSRETURN ret = XeroFile.Load(g_VFS, xmlFilename);
     893           0 :     if (ret != PSRETURN_OK)
     894           0 :         return nullptr;
     895             : 
     896             : #if USE_SHADER_XML_VALIDATION
     897           0 :     {
     898             :         // Serialize the XMB data and pass it to the validator
     899           0 :         XMLWriter_File shaderFile;
     900           0 :         shaderFile.SetPrettyPrint(false);
     901           0 :         shaderFile.XMB(XeroFile);
     902           0 :         if (!g_Xeromyces.ValidateEncoded("shader", name, shaderFile.GetOutput()))
     903           0 :             return nullptr;
     904           0 :     }
     905             : #endif
     906             : 
     907             :     // Define all the elements and attributes used in the XML file
     908             : #define EL(x) int el_##x = XeroFile.GetElementID(#x)
     909             : #define AT(x) int at_##x = XeroFile.GetAttributeID(#x)
     910           0 :     EL(compute);
     911           0 :     EL(define);
     912           0 :     EL(fragment);
     913           0 :     EL(stream);
     914           0 :     EL(uniform);
     915           0 :     EL(vertex);
     916           0 :     AT(attribute);
     917           0 :     AT(file);
     918           0 :     AT(if);
     919           0 :     AT(loc);
     920           0 :     AT(name);
     921           0 :     AT(type);
     922           0 :     AT(value);
     923             : #undef AT
     924             : #undef EL
     925             : 
     926           0 :     CPreprocessorWrapper preprocessor;
     927           0 :     preprocessor.AddDefines(baseDefines);
     928             : 
     929           0 :     XMBElement root = XeroFile.GetRoot();
     930             : 
     931           0 :     VfsPath vertexFile;
     932           0 :     VfsPath fragmentFile;
     933           0 :     CShaderDefines defines = baseDefines;
     934           0 :     std::map<CStrIntern, std::pair<CStr, int>> vertexUniforms;
     935           0 :     std::map<CStrIntern, std::pair<CStr, int>> fragmentUniforms;
     936           0 :     std::map<CStrIntern, int> vertexAttribs;
     937           0 :     int streamFlags = 0;
     938             : 
     939           0 :     VfsPath computeFile;
     940             : 
     941           0 :     for (XMBElement child : root.GetChildNodes())
     942             :     {
     943           0 :         if (child.GetNodeName() == el_define)
     944             :         {
     945           0 :             defines.Add(CStrIntern(child.GetAttributes().GetNamedItem(at_name)), CStrIntern(child.GetAttributes().GetNamedItem(at_value)));
     946             :         }
     947           0 :         else if (child.GetNodeName() == el_vertex)
     948             :         {
     949           0 :             vertexFile = L"shaders/" + child.GetAttributes().GetNamedItem(at_file).FromUTF8();
     950             : 
     951           0 :             for (XMBElement param : child.GetChildNodes())
     952             :             {
     953           0 :                 XMBAttributeList attributes = param.GetAttributes();
     954             : 
     955           0 :                 CStr cond = attributes.GetNamedItem(at_if);
     956           0 :                 if (!cond.empty() && !preprocessor.TestConditional(cond))
     957           0 :                     continue;
     958             : 
     959           0 :                 if (param.GetNodeName() == el_uniform)
     960             :                 {
     961           0 :                     vertexUniforms[CStrIntern(attributes.GetNamedItem(at_name))] =
     962           0 :                         std::make_pair(attributes.GetNamedItem(at_type), attributes.GetNamedItem(at_loc).ToInt());
     963             :                 }
     964           0 :                 else if (param.GetNodeName() == el_stream)
     965             :                 {
     966           0 :                     const CStr streamName = attributes.GetNamedItem(at_name);
     967           0 :                     const CStr attributeName = attributes.GetNamedItem(at_attribute);
     968           0 :                     if (attributeName.empty())
     969             :                     {
     970           0 :                         LOGERROR("Empty attribute name in vertex shader description '{}'",
     971             :                             vertexFile.string8());
     972             :                     }
     973             : 
     974           0 :                     VertexAttributeStream stream =
     975             :                         VertexAttributeStream::UV7;
     976           0 :                     if (streamName == "pos")
     977             :                         stream = VertexAttributeStream::POSITION;
     978           0 :                     else if (streamName == "normal")
     979             :                         stream = VertexAttributeStream::NORMAL;
     980           0 :                     else if (streamName == "color")
     981             :                         stream = VertexAttributeStream::COLOR;
     982           0 :                     else if (streamName == "uv0")
     983             :                         stream = VertexAttributeStream::UV0;
     984           0 :                     else if (streamName == "uv1")
     985             :                         stream = VertexAttributeStream::UV1;
     986           0 :                     else if (streamName == "uv2")
     987             :                         stream = VertexAttributeStream::UV2;
     988           0 :                     else if (streamName == "uv3")
     989             :                         stream = VertexAttributeStream::UV3;
     990           0 :                     else if (streamName == "uv4")
     991             :                         stream = VertexAttributeStream::UV4;
     992           0 :                     else if (streamName == "uv5")
     993             :                         stream = VertexAttributeStream::UV5;
     994           0 :                     else if (streamName == "uv6")
     995             :                         stream = VertexAttributeStream::UV6;
     996           0 :                     else if (streamName == "uv7")
     997             :                         stream = VertexAttributeStream::UV7;
     998             :                     else
     999             :                     {
    1000           0 :                         LOGERROR("Unknown stream '{}' in vertex shader description '{}'",
    1001             :                             streamName, vertexFile.string8());
    1002             :                     }
    1003             : 
    1004           0 :                     const int attributeLocation = GetAttributeLocationFromStream(device, stream);
    1005           0 :                     vertexAttribs[CStrIntern(attributeName)] = attributeLocation;
    1006           0 :                     streamFlags |= GetStreamMask(stream);
    1007           0 :                 }
    1008           0 :             }
    1009             :         }
    1010           0 :         else if (child.GetNodeName() == el_fragment)
    1011             :         {
    1012           0 :             fragmentFile = L"shaders/" + child.GetAttributes().GetNamedItem(at_file).FromUTF8();
    1013             : 
    1014           0 :             for (XMBElement param : child.GetChildNodes())
    1015             :             {
    1016           0 :                 XMBAttributeList attributes = param.GetAttributes();
    1017             : 
    1018           0 :                 CStr cond = attributes.GetNamedItem(at_if);
    1019           0 :                 if (!cond.empty() && !preprocessor.TestConditional(cond))
    1020           0 :                     continue;
    1021             : 
    1022           0 :                 if (param.GetNodeName() == el_uniform)
    1023             :                 {
    1024           0 :                     fragmentUniforms[CStrIntern(attributes.GetNamedItem(at_name))] =
    1025           0 :                         std::make_pair(attributes.GetNamedItem(at_type), attributes.GetNamedItem(at_loc).ToInt());
    1026             :                 }
    1027           0 :             }
    1028             :         }
    1029           0 :         else if (child.GetNodeName() == el_compute)
    1030             :         {
    1031           0 :             computeFile = L"shaders/" + child.GetAttributes().GetNamedItem(at_file).FromUTF8();
    1032             :         }
    1033             :     }
    1034             : 
    1035             : #if !CONFIG2_GLES
    1036           0 :     if (!computeFile.empty())
    1037             :     {
    1038           0 :         ENSURE(streamFlags == 0);
    1039           0 :         ENSURE(vertexAttribs.empty());
    1040             :     }
    1041           0 :     const PS::StaticVector<std::tuple<VfsPath, GLenum>, 2> shaderStages{computeFile.empty()
    1042           0 :         ? PS::StaticVector<std::tuple<VfsPath, GLenum>, 2>{{vertexFile, GL_VERTEX_SHADER}, {fragmentFile, GL_FRAGMENT_SHADER}}
    1043           0 :         : PS::StaticVector<std::tuple<VfsPath, GLenum>, 2>{{computeFile, GL_COMPUTE_SHADER}}};
    1044             : #else
    1045             :     const PS::StaticVector<std::tuple<VfsPath, GLenum>, 2> shaderStages{{{vertexFile, GL_VERTEX_SHADER}, {fragmentFile, GL_FRAGMENT_SHADER}}};
    1046             : #endif
    1047             : 
    1048           0 :     return std::unique_ptr<CShaderProgram>(new CShaderProgram(
    1049             :         device, name, xmlFilename, shaderStages, defines,
    1050           0 :         vertexAttribs, streamFlags));
    1051           0 : }
    1052             : 
    1053           0 : bool CShaderProgram::IsStreamActive(const VertexAttributeStream stream) const
    1054             : {
    1055           0 :     return (m_StreamFlags & GetStreamMask(stream)) != 0;
    1056             : }
    1057             : 
    1058           0 : void CShaderProgram::AssertPointersBound()
    1059             : {
    1060           0 :     ENSURE((m_StreamFlags & ~m_ValidStreams) == 0);
    1061           0 : }
    1062             : 
    1063             : } // namespace GL
    1064             : 
    1065             : } // namespace Backend
    1066             : 
    1067             : } // namespace Renderer

Generated by: LCOV version 1.16