Debian Codegraph @

Sources/nCine/Graphics/RHI/RSX/RsxShaderUniforms.h

raw
1 #pragma once
2
3 #include "RsxUniformCache.h"
4 #include "../RhiFwd.h"
5
6 #include <cstdint>
7
8 #include <Containers/SmallVector.h>
9
10 using namespace Death::Containers;
11
12 namespace nCine::RHI::RSX
13 {
14 class RsxShaderProgram;
15
16 /**
17 @brief Manages the loose-uniform caches of a program (aliased as `RHI::ShaderUniforms`)
18
19 Owns a @ref RsxUniformCache for every active uniform of a program that is not part of a block,
20 distributes a shared host data buffer across them, and on @ref CommitUniforms() publishes every dirty
21 value to the program. The managed set can be restricted to an include-only or exclude list of
22 null-separated names.
23 */
24 class RsxShaderUniforms
25 {
26 public:
27 // The caches are stored in a flat container whose value type is the cache itself (the name lives
28 // inside each cache's uniform), so range-iteration yields `RsxUniformCache&` directly — matching
29 // the OpenGL backend's `GetAllUniforms()` iteration semantics that the render batcher relies on.
30 using UniformHashMapType = SmallVector<RsxUniformCache, 0>;
31
32 RsxShaderUniforms();
33 explicit RsxShaderUniforms(RsxShaderProgram* shaderProgram);
34 RsxShaderUniforms(RsxShaderProgram* shaderProgram, const char* includeOnly, const char* exclude);
35
36 inline void SetProgram(RsxShaderProgram* shaderProgram) {
37 SetProgram(shaderProgram, nullptr, nullptr);
38 }
39 void SetProgram(RsxShaderProgram* shaderProgram, const char* includeOnly, const char* exclude);
40 void SetUniformsDataPointer(std::uint8_t* dataPointer);
41 void SetDirty(bool isDirty);
42
43 inline std::uint32_t GetUniformCount() const {
44 return std::uint32_t(_uniformCaches.size());
45 }
46 bool HasUniform(const char* name) const;
47 RsxUniformCache* GetUniform(const char* name);
48 /** @brief Returns the container of all managed uniform caches (range-iterates as `RsxUniformCache&`) */
49 inline const UniformHashMapType& GetAllUniforms() const {
50 return _uniformCaches;
51 }
52 inline void MarkDirty() {
53 _maybeDirty = true;
54 }
55 void CommitUniforms();
56
57 private:
58 RsxShaderProgram* _shaderProgram;
59 bool _maybeDirty;
60
61 UniformHashMapType _uniformCaches;
62
63 void ImportUniforms(const char* includeOnly, const char* exclude);
64 };
65
66 /**
67 @brief Manages the uniform-block caches of a program (aliased as `RHI::ShaderUniformBlocks`)
68
69 Owns a @ref RsxUniformBlockCache per active block, distributes a shared host data buffer across
70 them, copies the block contents into a suballocated range of the streaming uniform buffer on @ref
71 CommitUniformBlocks(), and on @ref Bind() forwards each range to the device.
72 */
73 class RsxShaderUniformBlocks
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 `RsxUniformBlockCache&` directly, matching the OpenGL backend.
78 using UniformHashMapType = SmallVector<RsxUniformBlockCache, 0>;
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 RsxShaderUniformBlocks();
87 explicit RsxShaderUniformBlocks(RsxShaderProgram* shaderProgram);
88 RsxShaderUniformBlocks(RsxShaderProgram* shaderProgram, const char* includeOnly, const char* exclude);
89
90 inline void SetProgram(RsxShaderProgram* shaderProgram) {
91 SetProgram(shaderProgram, nullptr, nullptr);
92 }
93 void SetProgram(RsxShaderProgram* 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 RsxUniformBlockCache* GetUniformBlock(const char* name);
101 /** @brief Returns the container of all managed block caches (range-iterates as `RsxUniformBlockCache&`) */
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 RsxShaderProgram* _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 }