Debian Codegraph @

Sources/nCine/Graphics/RHI/Vulkan/VulkanShaderUniforms.h

raw
1 #pragma once
2
3 #include "VulkanUniformCache.h"
4 #include "../RhiFwd.h"
5
6 #include <cstdint>
7 #include <vector>
8
9 namespace nCine::RHI::Vulkan
10 {
11 class VulkanShaderProgram;
12
13 /**
14 @brief Manages the loose-uniform caches of a program (aliased as `RHI::ShaderUniforms`)
15
16 Owns a @ref VulkanUniformCache for every active uniform of a program that is not part of a block,
17 distributes a shared host data buffer across them, and on @ref CommitUniforms() publishes every dirty
18 value to the program. The managed set can be restricted to an include-only or exclude list of
19 null-separated names.
20 */
21 class VulkanShaderUniforms
22 {
23 public:
24 // The caches are stored in a flat container whose value type is the cache itself (the name lives
25 // inside each cache's uniform), so range-iteration yields `VulkanUniformCache&` directly — matching
26 // the OpenGL backend's `GetAllUniforms()` iteration semantics that the render batcher relies on.
27 using UniformHashMapType = std::vector<VulkanUniformCache>;
28
29 VulkanShaderUniforms();
30 explicit VulkanShaderUniforms(VulkanShaderProgram* shaderProgram);
31 VulkanShaderUniforms(VulkanShaderProgram* shaderProgram, const char* includeOnly, const char* exclude);
32
33 inline void SetProgram(VulkanShaderProgram* shaderProgram) {
34 SetProgram(shaderProgram, nullptr, nullptr);
35 }
36 void SetProgram(VulkanShaderProgram* shaderProgram, const char* includeOnly, const char* exclude);
37 void SetUniformsDataPointer(std::uint8_t* dataPointer);
38 void SetDirty(bool isDirty);
39
40 inline std::uint32_t GetUniformCount() const {
41 return std::uint32_t(_uniformCaches.size());
42 }
43 bool HasUniform(const char* name) const;
44 VulkanUniformCache* GetUniform(const char* name);
45 /** @brief Returns the container of all managed uniform caches (range-iterates as `VulkanUniformCache&`) */
46 inline const UniformHashMapType& GetAllUniforms() const {
47 return _uniformCaches;
48 }
49 inline void MarkDirty() {
50 _maybeDirty = true;
51 }
52 void CommitUniforms();
53
54 private:
55 VulkanShaderProgram* _shaderProgram;
56 bool _maybeDirty;
57
58 UniformHashMapType _uniformCaches;
59
60 void ImportUniforms(const char* includeOnly, const char* exclude);
61 };
62
63 /**
64 @brief Manages the uniform-block caches of a program (aliased as `RHI::ShaderUniformBlocks`)
65
66 Owns a @ref VulkanUniformBlockCache per active block, distributes a shared host data buffer across
67 them, copies the block contents into a suballocated range of the streaming uniform buffer on @ref
68 CommitUniformBlocks(), and on @ref Bind() forwards each range to the device.
69 */
70 class VulkanShaderUniformBlocks
71 {
72 public:
73 // Flat container whose value type is the block cache itself (the name lives inside each cache's
74 // block), so range-iteration yields `VulkanUniformBlockCache&` directly, matching the OpenGL backend.
75 using UniformHashMapType = std::vector<VulkanUniformBlockCache>;
76
77 /** @brief Function that suballocates a range of the given size from the streaming uniform buffer */
78 using UniformRangeAllocator = RHI::BufferRange (*)(std::uint32_t bytes);
79
80 /** @brief Sets the allocator used by @ref CommitUniformBlocks() (registered by the pipeline at startup) */
81 static void SetUniformRangeAllocator(UniformRangeAllocator allocator);
82
83 VulkanShaderUniformBlocks();
84 explicit VulkanShaderUniformBlocks(VulkanShaderProgram* shaderProgram);
85 VulkanShaderUniformBlocks(VulkanShaderProgram* shaderProgram, const char* includeOnly, const char* exclude);
86
87 inline void SetProgram(VulkanShaderProgram* shaderProgram) {
88 SetProgram(shaderProgram, nullptr, nullptr);
89 }
90 void SetProgram(VulkanShaderProgram* shaderProgram, const char* includeOnly, const char* exclude);
91 void SetUniformsDataPointer(std::uint8_t* dataPointer);
92
93 inline std::uint32_t GetUniformBlockCount() const {
94 return std::uint32_t(_uniformBlockCaches.size());
95 }
96 bool HasUniformBlock(const char* name) const;
97 VulkanUniformBlockCache* GetUniformBlock(const char* name);
98 /** @brief Returns the container of all managed block caches (range-iterates as `VulkanUniformBlockCache&`) */
99 inline const UniformHashMapType& GetAllUniformBlocks() const {
100 return _uniformBlockCaches;
101 }
102 void CommitUniformBlocks();
103 void Bind();
104
105 private:
106 static UniformRangeAllocator _uniformRangeAllocator;
107
108 VulkanShaderProgram* _shaderProgram;
109 std::uint8_t* _dataPointer;
110 RHI::BufferRange _uboParams;
111
112 UniformHashMapType _uniformBlockCaches;
113
114 void ImportUniformBlocks(const char* includeOnly, const char* exclude);
115 };
116 }