LCOV - code coverage report
Current view: top level - source/renderer/backend/gl - ShaderProgram.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 10 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 4 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             : #ifndef INCLUDED_RENDERER_BACKEND_GL_SHADERPROGRAM
      19             : #define INCLUDED_RENDERER_BACKEND_GL_SHADERPROGRAM
      20             : 
      21             : #include "lib/code_annotation.h"
      22             : #include "lib/debug.h"
      23             : #include "ps/containers/StaticVector.h"
      24             : #include "ps/CStr.h"
      25             : #include "ps/CStrIntern.h"
      26             : #include "renderer/backend/Format.h"
      27             : #include "renderer/backend/gl/Device.h"
      28             : #include "renderer/backend/IBuffer.h"
      29             : #include "renderer/backend/IShaderProgram.h"
      30             : 
      31             : #if CONFIG2_GLES
      32             : #include "external_libraries/opengles2_wrapper.h"
      33             : #else
      34             : #include <glad/gl.h>
      35             : #endif
      36             : 
      37             : #include <cstdint>
      38             : #include <map>
      39             : #include <memory>
      40             : #include <span>
      41             : #include <tuple>
      42             : #include <unordered_map>
      43             : #include <vector>
      44             : 
      45             : class CShaderDefines;
      46             : class CStrIntern;
      47             : namespace Renderer::Backend::GL { class CDevice; }
      48             : 
      49             : namespace Renderer
      50             : {
      51             : 
      52             : namespace Backend
      53             : {
      54             : 
      55             : namespace GL
      56             : {
      57             : 
      58             : class CVertexInputLayout : public IVertexInputLayout
      59             : {
      60             : public:
      61           0 :     CVertexInputLayout(CDevice* device, const std::span<const SVertexAttributeFormat> attributes)
      62           0 :         : m_Device(device), m_Attributes(attributes.begin(), attributes.end())
      63             :     {
      64           0 :         for (const SVertexAttributeFormat& attribute : m_Attributes)
      65             :         {
      66           0 :             ENSURE(attribute.format != Format::UNDEFINED);
      67           0 :             ENSURE(attribute.stride > 0);
      68             :         }
      69           0 :     }
      70             : 
      71           0 :     ~CVertexInputLayout() override = default;
      72             : 
      73             :     IDevice* GetDevice() override;
      74             : 
      75           0 :     const std::vector<SVertexAttributeFormat>& GetAttributes() const noexcept { return m_Attributes; }
      76             : 
      77             : private:
      78             :     CDevice* m_Device = nullptr;
      79             : 
      80             :     std::vector<SVertexAttributeFormat> m_Attributes;
      81             : };
      82             : 
      83             : /**
      84             :  * A compiled vertex+fragment shader program.
      85             :  *
      86             :  * Setting uniforms that the shader doesn't support is harmless.
      87             :  */
      88             : class CShaderProgram final : public IShaderProgram
      89             : {
      90             :     NONCOPYABLE(CShaderProgram);
      91             : 
      92             : public:
      93             :     typedef CStrIntern attrib_id_t;
      94             : 
      95             :     static std::unique_ptr<CShaderProgram> Create(
      96             :         CDevice* device, const CStr& name, const CShaderDefines& baseDefines);
      97             : 
      98             :     ~CShaderProgram() override;
      99             : 
     100             :     /**
     101             :      * Binds the shader into the GL context. Call this before calling Uniform()
     102             :      * or trying to render with it.
     103             :      */
     104             :     void Bind(CShaderProgram* previousShaderProgram);
     105             : 
     106             :     /**
     107             :      * Unbinds the shader from the GL context. Call this after rendering with it.
     108             :      */
     109             :     void Unbind();
     110             : 
     111             :     int32_t GetBindingSlot(const CStrIntern name) const override;
     112             : 
     113             :     struct TextureUnit
     114             :     {
     115             :         GLenum type;
     116             :         GLenum target;
     117             :         GLint unit;
     118             :     };
     119             :     TextureUnit GetTextureUnit(const int32_t bindingSlot);
     120             : 
     121             :     GLuint GetStorageBuffer(const int32_t bindingSlot);
     122             : 
     123             :     void SetUniform(
     124             :         const int32_t bindingSlot,
     125             :         const float value);
     126             :     void SetUniform(
     127             :         const int32_t bindingSlot,
     128             :         const float valueX, const float valueY);
     129             :     void SetUniform(
     130             :         const int32_t bindingSlot,
     131             :         const float valueX, const float valueY,
     132             :         const float valueZ);
     133             :     void SetUniform(
     134             :         const int32_t bindingSlot,
     135             :         const float valueX, const float valueY,
     136             :         const float valueZ, const float valueW);
     137             :     void SetUniform(
     138             :         const int32_t bindingSlot, std::span<const float> values);
     139             : 
     140             :     // Vertex attribute pointers (equivalent to glVertexPointer etc).
     141             :     void VertexAttribPointer(
     142             :         const VertexAttributeStream stream, const Format format,
     143             :         const uint32_t offset, const uint32_t stride,
     144             :         const VertexAttributeRate rate, const void* data);
     145             : 
     146             :     bool IsStreamActive(const VertexAttributeStream stream) const;
     147             : 
     148           0 :     bool HasImageUniforms() const { return m_HasImageUniforms; }
     149             : 
     150           0 :     IDevice* GetDevice() override { return m_Device; }
     151             : 
     152             :     std::vector<VfsPath> GetFileDependencies() const override;
     153             : 
     154             :     /**
     155             :      * Checks that all the required vertex attributes have been set.
     156             :      * Call this before calling Draw/DrawIndexed etc to avoid potential crashes.
     157             :      */
     158             :     void AssertPointersBound();
     159             : 
     160             : private:
     161             :     CShaderProgram(
     162             :         CDevice* device, const CStr& name, const VfsPath& programPath,
     163             :         std::span<const std::tuple<VfsPath, GLenum>> shaderStages,
     164             :         const CShaderDefines& defines,
     165             :         const std::map<CStrIntern, int>& vertexAttribs,
     166             :         int streamflags);
     167             : 
     168             :     bool Link(const VfsPath& path);
     169             : 
     170             :     int m_StreamFlags;
     171             : 
     172             :     int m_ValidStreams; // which streams have been specified via VertexPointer etc since the last Bind
     173             : 
     174             :     bool m_HasImageUniforms{false};
     175             : 
     176             :     struct ShaderStage
     177             :     {
     178             :         GLenum type;
     179             :         GLuint shader;
     180             :     };
     181             : 
     182             :     CDevice* m_Device{nullptr};
     183             : 
     184             :     CStr m_Name;
     185             :     std::vector<VfsPath> m_FileDependencies;
     186             : 
     187             :     std::map<CStrIntern, int> m_VertexAttribs;
     188             :     // Sorted list of active vertex attributes.
     189             :     std::vector<int> m_ActiveVertexAttributes;
     190             : 
     191             :     GLuint m_Program;
     192             :     // 5 = max(compute, vertex + tesselation (control + evaluation) + geometry + fragment).
     193             :     PS::StaticVector<ShaderStage, 5> m_ShaderStages;
     194             : 
     195             :     struct BindingSlot
     196             :     {
     197             :         CStrIntern name;
     198             :         GLint location;
     199             :         GLint offset;
     200             :         GLint size;
     201             :         GLenum type;
     202             :         GLenum elementType;
     203             :         GLint elementCount;
     204             :         bool isTexture;
     205             :         bool isStorageBuffer;
     206             :     };
     207             :     std::vector<BindingSlot> m_BindingSlots;
     208             :     std::unordered_map<CStrIntern, int32_t> m_BindingSlotsMapping;
     209             : 
     210             :     GLint m_UniformBufferLocation{-1};
     211             :     uint32_t m_UniformBufferSize{0};
     212             :     std::unique_ptr<IBuffer> m_UniformBuffer;
     213             : };
     214             : 
     215             : } // namespace GL
     216             : 
     217             : } // namespace Backend
     218             : 
     219             : } // namespace Renderer
     220             : 
     221             : #endif // INCLUDED_RENDERER_BACKEND_GL_SHADERPROGRAM

Generated by: LCOV version 1.16