LCOV - code coverage report
Current view: top level - source/renderer/backend/vulkan - ShaderProgram.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 22 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 3 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_VULKAN_SHADERPROGRAM
      19             : #define INCLUDED_RENDERER_BACKEND_VULKAN_SHADERPROGRAM
      20             : 
      21             : #include "lib/debug.h"
      22             : #include "lib/file/vfs/vfs_path.h"
      23             : #include "ps/CStr.h"
      24             : #include "ps/CStrIntern.h"
      25             : #include "renderer/backend/Format.h"
      26             : #include "renderer/backend/IShaderProgram.h"
      27             : #include "renderer/backend/vulkan/DescriptorManager.h"
      28             : #include "renderer/backend/vulkan/Mapping.h"
      29             : 
      30             : #include <array>
      31             : #include <cstddef>
      32             : #include <cstdint>
      33             : #include <glad/vulkan.h>
      34             : #include <memory>
      35             : #include <optional>
      36             : #include <span>
      37             : #include <unordered_map>
      38             : #include <utility>
      39             : #include <vector>
      40             : 
      41             : class CShaderDefines;
      42             : class CStr;
      43             : namespace Renderer::Backend::Vulkan { class CBuffer; }
      44             : namespace Renderer::Backend::Vulkan { class CDevice; }
      45             : namespace Renderer::Backend::Vulkan { class CRingCommandContext; }
      46             : namespace Renderer::Backend::Vulkan { class CTexture; }
      47             : 
      48             : namespace Renderer
      49             : {
      50             : 
      51             : namespace Backend
      52             : {
      53             : 
      54             : namespace Vulkan
      55             : {
      56             : 
      57             : class CVertexInputLayout : public IVertexInputLayout
      58             : {
      59             : public:
      60           0 :     CVertexInputLayout(CDevice* device, const std::span<const SVertexAttributeFormat> attributes)
      61           0 :         : m_Device(device), m_Attributes(attributes.begin(), attributes.end())
      62             :     {
      63           0 :         static uint32_t m_LastAvailableUID = 1;
      64           0 :         m_UID = m_LastAvailableUID++;
      65           0 :         for (const SVertexAttributeFormat& attribute : m_Attributes)
      66             :         {
      67           0 :             ENSURE(attribute.format != Format::UNDEFINED);
      68           0 :             ENSURE(attribute.stride > 0);
      69             : 
      70           0 :             VkFormatProperties formatProperties{};
      71           0 :             vkGetPhysicalDeviceFormatProperties(
      72           0 :                 m_Device->GetChoosenPhysicalDevice().device, Mapping::FromFormat(attribute.format), &formatProperties);
      73           0 :             ENSURE(formatProperties.bufferFeatures & VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT);
      74             :         }
      75           0 :     }
      76             : 
      77           0 :     ~CVertexInputLayout() override = default;
      78             : 
      79             :     IDevice* GetDevice() override;
      80             : 
      81           0 :     const std::vector<SVertexAttributeFormat>& GetAttributes() const noexcept { return m_Attributes; }
      82             : 
      83             :     using UID = uint32_t;
      84           0 :     UID GetUID() const { return m_UID; }
      85             : 
      86             : private:
      87             :     CDevice* m_Device = nullptr;
      88             : 
      89             :     UID m_UID = 0;
      90             : 
      91             :     std::vector<SVertexAttributeFormat> m_Attributes;
      92             : };
      93             : 
      94             : class CShaderProgram final : public IShaderProgram
      95             : {
      96             : public:
      97             :     ~CShaderProgram() override;
      98             : 
      99             :     IDevice* GetDevice() override;
     100             : 
     101             :     int32_t GetBindingSlot(const CStrIntern name) const override;
     102             : 
     103             :     std::vector<VfsPath> GetFileDependencies() const override;
     104             : 
     105             :     uint32_t GetStreamLocation(const VertexAttributeStream stream) const;
     106             : 
     107           0 :     const std::vector<VkPipelineShaderStageCreateInfo>& GetStages() const { return m_Stages; }
     108             : 
     109             :     void Bind();
     110             :     void Unbind();
     111             : 
     112             :     void PreDraw(CRingCommandContext& commandContext);
     113             :     void PreDispatch(CRingCommandContext& commandContext);
     114             :     void PostDispatch(CRingCommandContext& commandContext);
     115             : 
     116           0 :     VkPipelineLayout GetPipelineLayout() const { return m_PipelineLayout; }
     117           0 :     VkPipelineBindPoint GetPipelineBindPoint() const { return m_PipelineBindPoint; }
     118             : 
     119             :     void SetUniform(
     120             :         const int32_t bindingSlot,
     121             :         const float value);
     122             :     void SetUniform(
     123             :         const int32_t bindingSlot,
     124             :         const float valueX, const float valueY);
     125             :     void SetUniform(
     126             :         const int32_t bindingSlot,
     127             :         const float valueX, const float valueY,
     128             :         const float valueZ);
     129             :     void SetUniform(
     130             :         const int32_t bindingSlot,
     131             :         const float valueX, const float valueY,
     132             :         const float valueZ, const float valueW);
     133             :     void SetUniform(
     134             :         const int32_t bindingSlot, std::span<const float> values);
     135             : 
     136             :     void SetTexture(const int32_t bindingSlot, CTexture* texture);
     137             :     void SetStorageTexture(const int32_t bindingSlot, CTexture* texture);
     138             :     void SetStorageBuffer(const int32_t bindingSlot, CBuffer* buffer);
     139             : 
     140             :     // TODO: rename to something related to buffer.
     141           0 :     bool IsMaterialConstantsDataOutdated() const { return m_MaterialConstantsDataOutdated; }
     142           0 :     void UpdateMaterialConstantsData() { m_MaterialConstantsDataOutdated = false; }
     143           0 :     std::byte* GetMaterialConstantsData() const { return m_MaterialConstantsData.get(); }
     144           0 :     uint32_t GetMaterialConstantsDataSize() const { return m_MaterialConstantsDataSize; }
     145             : 
     146             : private:
     147             :     friend class CDevice;
     148             : 
     149             :     CShaderProgram();
     150             : 
     151             :     std::pair<std::byte*, uint32_t> GetUniformData(
     152             :         const int32_t bindingSlot, const uint32_t dataSize);
     153             : 
     154             :     static std::unique_ptr<CShaderProgram> Create(
     155             :         CDevice* device, const CStr& name, const CShaderDefines& defines);
     156             : 
     157             :     void BindOutdatedDescriptorSets(
     158             :         CRingCommandContext& commandContext);
     159             : 
     160             :     CDevice* m_Device = nullptr;
     161             : 
     162             :     std::vector<VkShaderModule> m_ShaderModules;
     163             :     std::vector<VkPipelineShaderStageCreateInfo> m_Stages;
     164             :     VkPipelineLayout m_PipelineLayout = VK_NULL_HANDLE;
     165             :     VkPipelineBindPoint m_PipelineBindPoint = VK_PIPELINE_BIND_POINT_MAX_ENUM;
     166             : 
     167             :     std::vector<VfsPath> m_FileDependencies;
     168             : 
     169             :     struct PushConstant
     170             :     {
     171             :         CStrIntern name;
     172             :         uint32_t offset;
     173             :         uint32_t size;
     174             :         VkShaderStageFlags stageFlags;
     175             :     };
     176             :     struct Uniform
     177             :     {
     178             :         CStrIntern name;
     179             :         uint32_t offset;
     180             :         uint32_t size;
     181             :     };
     182             :     std::unique_ptr<std::byte[]> m_MaterialConstantsData;
     183             :     uint32_t m_MaterialConstantsDataSize = 0;
     184             :     bool m_MaterialConstantsDataOutdated = false;
     185             :     std::array<std::byte, 128> m_PushConstantData;
     186             :     uint32_t m_PushConstantDataMask = 0;
     187             :     std::array<VkShaderStageFlags, 32> m_PushConstantDataFlags;
     188             :     std::vector<PushConstant> m_PushConstants;
     189             :     std::vector<Uniform> m_Uniforms;
     190             :     std::unordered_map<CStrIntern, uint32_t> m_UniformMapping;
     191             :     std::unordered_map<CStrIntern, uint32_t> m_PushConstantMapping;
     192             : 
     193             :     std::optional<CSingleTypeDescriptorSetBinding<CTexture>> m_TextureBinding;
     194             :     std::optional<CSingleTypeDescriptorSetBinding<CTexture>> m_StorageImageBinding;
     195             :     std::optional<CSingleTypeDescriptorSetBinding<CBuffer>> m_StorageBufferBinding;
     196             : 
     197             :     std::unordered_map<VertexAttributeStream, uint32_t> m_StreamLocations;
     198             : };
     199             : 
     200             : } // namespace Vulkan
     201             : 
     202             : } // namespace Backend
     203             : 
     204             : } // namespace Renderer
     205             : 
     206             : #endif // INCLUDED_RENDERER_BACKEND_VULKAN_SHADERPROGRAM

Generated by: LCOV version 1.16