Debian Codegraph @

Sources/nCine/Graphics/RHI/GXM/GxmUniformCache.h

raw
1 #pragma once
2
3 #include "GxmShaderTypes.h"
4
5 #include <cstdint>
6
7 #include <Containers/SmallVector.h>
8 #include <Containers/String.h>
9 #include <Containers/StringView.h>
10
11 using namespace Death::Containers;
12
13 namespace nCine::RHI::GXM
14 {
15 /**
16 @brief Caches the host-side value of a single uniform (aliased as `RHI::UniformCache`)
17
18 Mirrors the OpenGL backend's uniform cache: it holds a pointer into a shared host data buffer and the
19 `Set*` methods that write values into it, plus a dirty flag. For a loose uniform, @ref CommitValue()
20 publishes the value pointer to the owning shader program. For a uniform living inside a block,
21 committing is a no-op — the whole block is published by @ref GxmShaderUniformBlocks.
22 */
23 class GxmUniformCache
24 {
25 public:
26 GxmUniformCache()
27 : _uniform(nullptr), _dataPointer(nullptr), _isDirty(false) {}
28 explicit GxmUniformCache(const GxmUniform* uniform)
29 : _uniform(uniform), _dataPointer(nullptr), _isDirty(false) {}
30
31 inline const GxmUniform* GetUniform() const {
32 return _uniform;
33 }
34 inline std::uint8_t* GetDataPointer() const {
35 return _dataPointer;
36 }
37 inline void SetDataPointer(std::uint8_t* dataPointer) {
38 _dataPointer = dataPointer;
39 }
40
41 const float* GetFloatVector() const;
42 float GetFloatValue(std::uint32_t index) const;
43 const std::int32_t* GetIntVector() const;
44 std::int32_t GetIntValue(std::uint32_t index) const;
45
46 bool SetFloatVector(const float* vec);
47 bool SetFloatValue(float v0);
48 bool SetFloatValue(float v0, float v1);
49 bool SetFloatValue(float v0, float v1, float v2);
50 bool SetFloatValue(float v0, float v1, float v2, float v3);
51 bool SetIntVector(const std::int32_t* vec);
52 bool SetIntValue(std::int32_t v0);
53 bool SetIntValue(std::int32_t v0, std::int32_t v1);
54 bool SetIntValue(std::int32_t v0, std::int32_t v1, std::int32_t v2);
55 bool SetIntValue(std::int32_t v0, std::int32_t v1, std::int32_t v2, std::int32_t v3);
56
57 inline bool IsDirty() const {
58 return _isDirty;
59 }
60 inline void SetDirty(bool isDirty) {
61 _isDirty = isDirty;
62 }
63 /** @brief Publishes a dirty loose-uniform value to the owning program; a no-op for block members */
64 bool CommitValue();
65
66 private:
67 const GxmUniform* _uniform;
68 std::uint8_t* _dataPointer;
69 bool _isDirty;
70
71 bool CheckFloat() const;
72 bool CheckInt() const;
73 bool CheckComponents(std::uint32_t requiredComponents) const;
74 };
75
76 /**
77 @brief Caches the contents of a uniform block (aliased as `RHI::UniformBlockCache`)
78
79 Holds a pointer into a shared host data buffer mirroring the block's std140 layout and a @ref
80 GxmUniformCache for each member, each pointing at its member offset within that buffer. The block is
81 uploaded as one contiguous range by @ref GxmShaderUniformBlocks.
82 */
83 class GxmUniformBlockCache
84 {
85 public:
86 GxmUniformBlockCache()
87 : _uniformBlock(nullptr), _dataPointer(nullptr), _usedSize(0) {}
88 explicit GxmUniformBlockCache(GxmUniformBlock* uniformBlock);
89
90 inline const GxmUniformBlock* uniformBlock() const {
91 return _uniformBlock;
92 }
93 std::uint32_t GetIndex() const;
94 std::int32_t GetBindingIndex() const;
95 std::int32_t GetSize() const;
96 std::uint8_t GetAlignAmount() const;
97
98 inline std::uint8_t* GetDataPointer() {
99 return _dataPointer;
100 }
101 inline const std::uint8_t* GetDataPointer() const {
102 return _dataPointer;
103 }
104 void SetDataPointer(std::uint8_t* dataPointer);
105
106 inline std::int32_t usedSize() const {
107 return _usedSize;
108 }
109 void SetUsedSize(std::int32_t usedSize);
110
111 bool CopyData(std::uint32_t destIndex, const std::uint8_t* src, std::uint32_t numBytes);
112 inline bool CopyData(const std::uint8_t* src) {
113 return CopyData(0, src, std::uint32_t(_usedSize));
114 }
115
116 /** @brief Returns the member uniform cache with the specified name, or `nullptr` if not found */
117 GxmUniformCache* GetUniform(StringView name);
118 void SetBlockBinding(std::int32_t blockBinding);
119
120 private:
121 GxmUniformBlock* _uniformBlock;
122 std::uint8_t* _dataPointer;
123 std::int32_t _usedSize;
124
125 struct NamedCache
126 {
127 String Name;
128 GxmUniformCache Cache;
129 };
130 SmallVector<NamedCache, 0> _uniformCaches;
131 };
132 }