Debian Codegraph @

Sources/nCine/Graphics/RHI/GS/GsShaderUniforms.h

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