Debian Codegraph @

Sources/nCine/Graphics/RHI/GL/GLUniformCache.cpp

raw
1 #include "GLUniformCache.h"
2 #include "GLUniform.h"
3 #include "../../../../Main.h"
4
5 #include <cstring> // for memcpy()
6
7 namespace nCine::RHI::GL
8 {
9 GLUniformCache::GLUniformCache()
10 : _uniform(nullptr), _dataPointer(nullptr), _isDirty(false)
11 {
12 }
13
14 GLUniformCache::GLUniformCache(const GLUniform* uniform)
15 : _uniform(uniform), _dataPointer(nullptr), _isDirty(false)
16 {
17 DEATH_ASSERT(uniform);
18 }
19
20 const GLfloat* GLUniformCache::GetFloatVector() const
21 {
22 DEATH_ASSERT(_uniform == nullptr || (_dataPointer != nullptr && CheckFloat()));
23 const GLfloat* vec = nullptr;
24
25 if (_dataPointer != nullptr) {
26 vec = reinterpret_cast<GLfloat*>(_dataPointer);
27 }
28 return vec;
29 }
30
31 GLfloat GLUniformCache::GetFloatValue(std::uint32_t index) const
32 {
33 DEATH_ASSERT(_uniform == nullptr || (_dataPointer != nullptr && CheckFloat() && _uniform->GetComponentCount() > index));
34
35 GLfloat value = 0.0f;
36
37 if (_dataPointer != nullptr) {
38 value = reinterpret_cast<const GLfloat*>(_dataPointer)[index];
39 }
40 return value;
41 }
42
43 const GLint* GLUniformCache::GetIntVector() const
44 {
45 DEATH_ASSERT(_uniform == nullptr || (_dataPointer != nullptr && CheckInt()));
46 const GLint* vec = nullptr;
47
48 if (_dataPointer != nullptr) {
49 vec = reinterpret_cast<GLint*>(_dataPointer);
50 }
51 return vec;
52 }
53
54 GLint GLUniformCache::GetIntValue(std::uint32_t index) const
55 {
56 DEATH_ASSERT(_uniform == nullptr || (_dataPointer != nullptr && CheckInt() && _uniform->GetComponentCount() > index));
57 GLint value = 0;
58
59 if (_dataPointer != nullptr) {
60 value = reinterpret_cast<const GLint*>(_dataPointer)[index];
61 }
62 return value;
63 }
64
65 bool GLUniformCache::SetFloatVector(const GLfloat* vec)
66 {
67 DEATH_ASSERT(_uniform == nullptr || _dataPointer != nullptr);
68 if (_uniform == nullptr || _dataPointer == nullptr || !CheckFloat()) {
69 return false;
70 }
71
72 _isDirty = true;
73 std::memcpy(_dataPointer, vec, sizeof(GLfloat) * _uniform->GetComponentCount());
74 return true;
75 }
76
77 bool GLUniformCache::SetFloatValue(GLfloat v0)
78 {
79 DEATH_ASSERT(_uniform == nullptr || _dataPointer != nullptr);
80 if (_uniform == nullptr || _dataPointer == nullptr || !CheckFloat() || !CheckComponents(1)) {
81 return false;
82 }
83
84 _isDirty = true;
85 GLfloat* data = reinterpret_cast<GLfloat*>(_dataPointer);
86 data[0] = v0;
87 return true;
88 }
89
90 bool GLUniformCache::SetFloatValue(GLfloat v0, GLfloat v1)
91 {
92 DEATH_ASSERT(_uniform == nullptr || _dataPointer != nullptr);
93 if (_uniform == nullptr || _dataPointer == nullptr || !CheckFloat() || !CheckComponents(2)) {
94 return false;
95 }
96
97 _isDirty = true;
98 GLfloat* data = reinterpret_cast<GLfloat*>(_dataPointer);
99 data[0] = v0;
100 data[1] = v1;
101 return true;
102 }
103
104 bool GLUniformCache::SetFloatValue(GLfloat v0, GLfloat v1, GLfloat v2)
105 {
106 DEATH_ASSERT(_uniform == nullptr || _dataPointer != nullptr);
107 if (_uniform == nullptr || _dataPointer == nullptr || !CheckFloat() || !CheckComponents(3)) {
108 return false;
109 }
110
111 _isDirty = true;
112 GLfloat* data = reinterpret_cast<GLfloat*>(_dataPointer);
113 data[0] = v0;
114 data[1] = v1;
115 data[2] = v2;
116 return true;
117 }
118
119 bool GLUniformCache::SetFloatValue(GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3)
120 {
121 DEATH_ASSERT(_uniform == nullptr || _dataPointer != nullptr);
122 if (_uniform == nullptr || _dataPointer == nullptr || !CheckFloat() || !CheckComponents(4)) {
123 return false;
124 }
125
126 _isDirty = true;
127 GLfloat* data = reinterpret_cast<GLfloat*>(_dataPointer);
128 data[0] = v0;
129 data[1] = v1;
130 data[2] = v2;
131 data[3] = v3;
132 return true;
133 }
134
135 bool GLUniformCache::SetIntVector(const GLint* vec)
136 {
137 DEATH_ASSERT(_uniform == nullptr || _dataPointer != nullptr);
138 if (_uniform == nullptr || _dataPointer == nullptr || !CheckInt()) {
139 return false;
140 }
141
142 _isDirty = true;
143 std::memcpy(_dataPointer, vec, sizeof(GLint) * _uniform->GetComponentCount());
144 return true;
145 }
146
147 bool GLUniformCache::SetIntValue(GLint v0)
148 {
149 DEATH_ASSERT(_uniform == nullptr || _dataPointer != nullptr);
150 if (_uniform == nullptr || _dataPointer == nullptr || !CheckInt() || !CheckComponents(1)) {
151 return false;
152 }
153
154 _isDirty = true;
155 GLint* data = reinterpret_cast<GLint*>(_dataPointer);
156 data[0] = v0;
157 return true;
158 }
159
160 bool GLUniformCache::SetIntValue(GLint v0, GLint v1)
161 {
162 DEATH_ASSERT(_uniform == nullptr || _dataPointer != nullptr);
163 if (_uniform == nullptr || _dataPointer == nullptr || !CheckInt() || !CheckComponents(2)) {
164 return false;
165 }
166
167 _isDirty = true;
168 GLint* data = reinterpret_cast<GLint*>(_dataPointer);
169 data[0] = v0;
170 data[1] = v1;
171 return true;
172 }
173
174 bool GLUniformCache::SetIntValue(GLint v0, GLint v1, GLint v2)
175 {
176 DEATH_ASSERT(_uniform == nullptr || _dataPointer != nullptr);
177 if (_uniform == nullptr || _dataPointer == nullptr || !CheckInt() || !CheckComponents(3)) {
178 return false;
179 }
180
181 _isDirty = true;
182 GLint* data = reinterpret_cast<GLint*>(_dataPointer);
183 data[0] = v0;
184 data[1] = v1;
185 data[2] = v2;
186 return true;
187 }
188
189 bool GLUniformCache::SetIntValue(GLint v0, GLint v1, GLint v2, GLint v3)
190 {
191 DEATH_ASSERT(_uniform == nullptr || _dataPointer != nullptr);
192 if (_uniform == nullptr || _dataPointer == nullptr || !CheckInt() || !CheckComponents(4)) {
193 return false;
194 }
195
196 _isDirty = true;
197 GLint* data = reinterpret_cast<GLint*>(_dataPointer);
198 data[0] = v0;
199 data[1] = v1;
200 data[2] = v2;
201 data[3] = v3;
202 return true;
203 }
204
205 bool GLUniformCache::CommitValue()
206 {
207 DEATH_ASSERT(_uniform == nullptr || _dataPointer != nullptr);
208 if (_uniform == nullptr || _dataPointer == nullptr || !_isDirty) {
209 return false;
210 }
211
212 // The uniform must not belong to any uniform block
213 DEATH_ASSERT(_uniform->GetBlockIndex() == -1);
214
215 const GLint location = _uniform->GetLocation();
216 switch (_uniform->GetGLType()) {
217 case GL_FLOAT:
218 glUniform1fv(location, 1, reinterpret_cast<const GLfloat*>(_dataPointer));
219 break;
220 case GL_FLOAT_VEC2:
221 glUniform2fv(location, 1, reinterpret_cast<const GLfloat*>(_dataPointer));
222 break;
223 case GL_FLOAT_VEC3:
224 glUniform3fv(location, 1, reinterpret_cast<const GLfloat*>(_dataPointer));
225 break;
226 case GL_FLOAT_VEC4:
227 glUniform4fv(location, 1, reinterpret_cast<const GLfloat*>(_dataPointer));
228 break;
229 case GL_INT:
230 glUniform1iv(location, 1, reinterpret_cast<const GLint*>(_dataPointer));
231 break;
232 case GL_INT_VEC2:
233 glUniform2iv(location, 1, reinterpret_cast<const GLint*>(_dataPointer));
234 break;
235 case GL_INT_VEC3:
236 glUniform3iv(location, 1, reinterpret_cast<const GLint*>(_dataPointer));
237 break;
238 case GL_INT_VEC4:
239 glUniform4iv(location, 1, reinterpret_cast<const GLint*>(_dataPointer));
240 break;
241 case GL_FLOAT_MAT2:
242 glUniformMatrix2fv(location, 1, GL_FALSE, reinterpret_cast<const GLfloat*>(_dataPointer));
243 break;
244 case GL_FLOAT_MAT3:
245 glUniformMatrix3fv(location, 1, GL_FALSE, reinterpret_cast<const GLfloat*>(_dataPointer));
246 break;
247 case GL_FLOAT_MAT4:
248 glUniformMatrix4fv(location, 1, GL_FALSE, reinterpret_cast<const GLfloat*>(_dataPointer));
249 break;
250 #if defined(RHI_GL_PROFILE_CORE) // not available in OpenGL ES
251 case GL_SAMPLER_1D:
252 #endif
253 case GL_SAMPLER_2D:
254 #if !defined(DEATH_TARGET_VITA) // GL_SAMPLER_3D is not declared by vitaGL
255 case GL_SAMPLER_3D:
256 #endif
257 case GL_SAMPLER_CUBE:
258 #if defined(RHI_GL_PROFILE_CORE) || GL_ES_VERSION_3_2
259 case GL_SAMPLER_BUFFER:
260 #endif
261 glUniform1iv(location, 1, reinterpret_cast<const GLint*>(_dataPointer));
262 break;
263 default:
264 LOGW("No available case to handle type: {}", _uniform->GetGLType());
265 break;
266 }
267
268 _isDirty = false;
269 return true;
270 }
271
272 bool GLUniformCache::CheckFloat() const
273 {
274 if (_uniform->GetBasicType() != GL_FLOAT) {
275 LOGE("Uniform \"{}\" is not floating point", _uniform->GetName());
276 return false;
277 } else {
278 return true;
279 }
280 }
281
282 bool GLUniformCache::CheckInt() const
283 {
284 if (_uniform->GetBasicType() != GL_INT &&
285 #if !defined(DEATH_TARGET_VITA) // vitaGL declares neither GL_BOOL nor GL_SAMPLER_3D
286 _uniform->GetBasicType() != GL_BOOL &&
287 #endif
288 #if defined(RHI_GL_PROFILE_CORE) // not available in OpenGL ES
289 _uniform->GetBasicType() != GL_SAMPLER_1D &&
290 #endif
291 _uniform->GetBasicType() != GL_SAMPLER_2D &&
292 #if !defined(DEATH_TARGET_VITA)
293 _uniform->GetBasicType() != GL_SAMPLER_3D &&
294 #endif
295 _uniform->GetBasicType() != GL_SAMPLER_CUBE
296 #if defined(RHI_GL_PROFILE_CORE) || GL_ES_VERSION_3_2
297 && _uniform->GetBasicType() != GL_SAMPLER_BUFFER
298 #endif
299 ) {
300 LOGE("Uniform \"{}\" is not integer", _uniform->GetName());
301 return false;
302 } else {
303 return true;
304 }
305 }
306
307 bool GLUniformCache::CheckComponents(std::uint32_t requiredComponents) const
308 {
309 if (_uniform->GetComponentCount() != requiredComponents) {
310 LOGE("Uniform \"{}\" has {} components, not {}", _uniform->GetName(), _uniform->GetComponentCount(), requiredComponents);
311 return false;
312 } else {
313 return true;
314 }
315 }
316 }