Debian Codegraph @

Sources/nCine/Graphics/RHI/RSX/RsxUniformCache.cpp

raw
1 #include "RsxUniformCache.h"
2 #include "RsxShaderProgram.h"
3
4 #include <cstring>
5
6 namespace nCine::RHI::RSX
7 {
8 const float* RsxUniformCache::GetFloatVector() const
9 {
10 return (_dataPointer != nullptr ? reinterpret_cast<const float*>(_dataPointer) : nullptr);
11 }
12
13 float RsxUniformCache::GetFloatValue(std::uint32_t index) const
14 {
15 return (_dataPointer != nullptr ? reinterpret_cast<const float*>(_dataPointer)[index] : 0.0f);
16 }
17
18 const std::int32_t* RsxUniformCache::GetIntVector() const
19 {
20 return (_dataPointer != nullptr ? reinterpret_cast<const std::int32_t*>(_dataPointer) : nullptr);
21 }
22
23 std::int32_t RsxUniformCache::GetIntValue(std::uint32_t index) const
24 {
25 return (_dataPointer != nullptr ? reinterpret_cast<const std::int32_t*>(_dataPointer)[index] : 0);
26 }
27
28 bool RsxUniformCache::SetFloatVector(const float* vec)
29 {
30 if (_uniform == nullptr || _dataPointer == nullptr || !CheckFloat()) {
31 return false;
32 }
33 _isDirty = true;
34 std::memcpy(_dataPointer, vec, sizeof(float) * _uniform->GetComponentCount());
35 return true;
36 }
37
38 bool RsxUniformCache::SetFloatValue(float v0)
39 {
40 if (_uniform == nullptr || _dataPointer == nullptr || !CheckFloat() || !CheckComponents(1)) {
41 return false;
42 }
43 _isDirty = true;
44 reinterpret_cast<float*>(_dataPointer)[0] = v0;
45 return true;
46 }
47
48 bool RsxUniformCache::SetFloatValue(float v0, float v1)
49 {
50 if (_uniform == nullptr || _dataPointer == nullptr || !CheckFloat() || !CheckComponents(2)) {
51 return false;
52 }
53 _isDirty = true;
54 float* data = reinterpret_cast<float*>(_dataPointer);
55 data[0] = v0;
56 data[1] = v1;
57 return true;
58 }
59
60 bool RsxUniformCache::SetFloatValue(float v0, float v1, float v2)
61 {
62 if (_uniform == nullptr || _dataPointer == nullptr || !CheckFloat() || !CheckComponents(3)) {
63 return false;
64 }
65 _isDirty = true;
66 float* data = reinterpret_cast<float*>(_dataPointer);
67 data[0] = v0;
68 data[1] = v1;
69 data[2] = v2;
70 return true;
71 }
72
73 bool RsxUniformCache::SetFloatValue(float v0, float v1, float v2, float v3)
74 {
75 if (_uniform == nullptr || _dataPointer == nullptr || !CheckFloat() || !CheckComponents(4)) {
76 return false;
77 }
78 _isDirty = true;
79 float* data = reinterpret_cast<float*>(_dataPointer);
80 data[0] = v0;
81 data[1] = v1;
82 data[2] = v2;
83 data[3] = v3;
84 return true;
85 }
86
87 bool RsxUniformCache::SetIntVector(const std::int32_t* vec)
88 {
89 if (_uniform == nullptr || _dataPointer == nullptr || !CheckInt()) {
90 return false;
91 }
92 _isDirty = true;
93 std::memcpy(_dataPointer, vec, sizeof(std::int32_t) * _uniform->GetComponentCount());
94 return true;
95 }
96
97 bool RsxUniformCache::SetIntValue(std::int32_t v0)
98 {
99 if (_uniform == nullptr || _dataPointer == nullptr || !CheckInt() || !CheckComponents(1)) {
100 return false;
101 }
102 _isDirty = true;
103 reinterpret_cast<std::int32_t*>(_dataPointer)[0] = v0;
104 return true;
105 }
106
107 bool RsxUniformCache::SetIntValue(std::int32_t v0, std::int32_t v1)
108 {
109 if (_uniform == nullptr || _dataPointer == nullptr || !CheckInt() || !CheckComponents(2)) {
110 return false;
111 }
112 _isDirty = true;
113 std::int32_t* data = reinterpret_cast<std::int32_t*>(_dataPointer);
114 data[0] = v0;
115 data[1] = v1;
116 return true;
117 }
118
119 bool RsxUniformCache::SetIntValue(std::int32_t v0, std::int32_t v1, std::int32_t v2)
120 {
121 if (_uniform == nullptr || _dataPointer == nullptr || !CheckInt() || !CheckComponents(3)) {
122 return false;
123 }
124 _isDirty = true;
125 std::int32_t* data = reinterpret_cast<std::int32_t*>(_dataPointer);
126 data[0] = v0;
127 data[1] = v1;
128 data[2] = v2;
129 return true;
130 }
131
132 bool RsxUniformCache::SetIntValue(std::int32_t v0, std::int32_t v1, std::int32_t v2, std::int32_t v3)
133 {
134 if (_uniform == nullptr || _dataPointer == nullptr || !CheckInt() || !CheckComponents(4)) {
135 return false;
136 }
137 _isDirty = true;
138 std::int32_t* data = reinterpret_cast<std::int32_t*>(_dataPointer);
139 data[0] = v0;
140 data[1] = v1;
141 data[2] = v2;
142 data[3] = v3;
143 return true;
144 }
145
146 bool RsxUniformCache::CommitValue()
147 {
148 if (_uniform == nullptr || _dataPointer == nullptr || !_isDirty) {
149 return false;
150 }
151
152 // A uniform inside a block is uploaded as part of the whole block, not individually
153 if (_uniform->GetBlockIndex() == -1 && _uniform->GetOwner() != nullptr) {
154 _uniform->GetOwner()->SetResolvedUniform(_uniform->GetName(), _dataPointer);
155 }
156
157 _isDirty = false;
158 return true;
159 }
160
161 bool RsxUniformCache::CheckFloat() const
162 {
163 return (_uniform != nullptr && _uniform->IsFloat());
164 }
165
166 bool RsxUniformCache::CheckInt() const
167 {
168 return (_uniform != nullptr && !_uniform->IsFloat());
169 }
170
171 bool RsxUniformCache::CheckComponents(std::uint32_t requiredComponents) const
172 {
173 return (_uniform != nullptr && _uniform->GetComponentCount() == requiredComponents);
174 }
175
176 // -------------------------------------------------------------------------------------------------
177
178 RsxUniformBlockCache::RsxUniformBlockCache(RsxUniformBlock* uniformBlock)
179 : _uniformBlock(uniformBlock), _dataPointer(nullptr), _usedSize(0)
180 {
181 _usedSize = uniformBlock->GetSize();
182 _uniformCaches.reserve(uniformBlock->_members.size());
183 for (RsxUniform& member : uniformBlock->_members) {
184 _uniformCaches.push_back({member.GetName(), RsxUniformCache(&member)});
185 }
186 }
187
188 std::uint32_t RsxUniformBlockCache::GetIndex() const
189 {
190 return (_uniformBlock != nullptr ? _uniformBlock->GetIndex() : 0);
191 }
192
193 std::int32_t RsxUniformBlockCache::GetBindingIndex() const
194 {
195 return (_uniformBlock != nullptr ? _uniformBlock->GetBindingIndex() : 0);
196 }
197
198 std::int32_t RsxUniformBlockCache::GetSize() const
199 {
200 return (_uniformBlock != nullptr ? _uniformBlock->GetSize() : 0);
201 }
202
203 std::uint8_t RsxUniformBlockCache::GetAlignAmount() const
204 {
205 return (_uniformBlock != nullptr ? _uniformBlock->GetAlignAmount() : 0);
206 }
207
208 void RsxUniformBlockCache::SetDataPointer(std::uint8_t* dataPointer)
209 {
210 _dataPointer = dataPointer;
211 for (NamedCache& named : _uniformCaches) {
212 named.Cache.SetDataPointer(_dataPointer + named.Cache.GetUniform()->GetOffset());
213 }
214 }
215
216 void RsxUniformBlockCache::SetUsedSize(std::int32_t usedSize)
217 {
218 if (usedSize >= 0) {
219 _usedSize = usedSize;
220 }
221 }
222
223 bool RsxUniformBlockCache::CopyData(std::uint32_t destIndex, const std::uint8_t* src, std::uint32_t numBytes)
224 {
225 if (destIndex + numBytes > std::uint32_t(GetSize()) || numBytes == 0 || src == nullptr || _dataPointer == nullptr) {
226 return false;
227 }
228 std::memcpy(&_dataPointer[destIndex], src, numBytes);
229 return true;
230 }
231
232 RsxUniformCache* RsxUniformBlockCache::GetUniform(StringView name)
233 {
234 for (NamedCache& named : _uniformCaches) {
235 if (name == named.Name) {
236 return &named.Cache;
237 }
238 }
239 return nullptr;
240 }
241
242 void RsxUniformBlockCache::SetBlockBinding(std::int32_t blockBinding)
243 {
244 if (_uniformBlock != nullptr) {
245 _uniformBlock->SetBlockBinding(blockBinding);
246 }
247 }
248 }