Debian Codegraph @

Sources/nCine/Graphics/RHI/GX/GxShaderUniforms.h

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