File: GLUniformCache.cpp

package info (click to toggle)
jazz2-native 3.5.0-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 16,912 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (310 lines) | stat: -rw-r--r-- 8,615 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include "GLUniformCache.h"
#include "GLUniform.h"
#include "../../../Main.h"

#include <cstring> // for memcpy()

namespace nCine
{
	GLUniformCache::GLUniformCache()
		: uniform_(nullptr), dataPointer_(nullptr), isDirty_(false)
	{
	}

	GLUniformCache::GLUniformCache(const GLUniform* uniform)
		: uniform_(uniform), dataPointer_(nullptr), isDirty_(false)
	{
		DEATH_ASSERT(uniform);
	}

	const GLfloat* GLUniformCache::GetFloatVector() const
	{
		DEATH_ASSERT(uniform_ == nullptr || (dataPointer_ != nullptr && CheckFloat()));
		const GLfloat* vec = nullptr;

		if (dataPointer_ != nullptr) {
			vec = reinterpret_cast<GLfloat*>(dataPointer_);
		}
		return vec;
	}

	GLfloat GLUniformCache::GetFloatValue(std::uint32_t index) const
	{
		DEATH_ASSERT(uniform_ == nullptr || (dataPointer_ != nullptr && CheckFloat() && uniform_->GetComponentCount() > index));

		GLfloat value = 0.0f;

		if (dataPointer_ != nullptr) {
			value = static_cast<GLfloat>(*dataPointer_);
		}
		return value;
	}

	const GLint* GLUniformCache::GetIntVector() const
	{
		DEATH_ASSERT(uniform_ == nullptr || (dataPointer_ != nullptr && CheckInt()));
		const GLint* vec = nullptr;

		if (dataPointer_ != nullptr) {
			vec = reinterpret_cast<GLint*>(dataPointer_);
		}
		return vec;
	}

	GLint GLUniformCache::GetIntValue(std::uint32_t index) const
	{
		DEATH_ASSERT(uniform_ == nullptr || (dataPointer_ != nullptr && CheckInt() && uniform_->GetComponentCount() > index));
		GLint value = 0;

		if (dataPointer_ != nullptr) {
			value = static_cast<GLint>(*dataPointer_);
		}
		return value;
	}

	bool GLUniformCache::SetFloatVector(const GLfloat* vec)
	{
		DEATH_ASSERT(uniform_ == nullptr || dataPointer_ != nullptr);
		if (uniform_ == nullptr || dataPointer_ == nullptr || !CheckFloat()) {
			return false;
		}

		isDirty_ = true;
		std::memcpy(dataPointer_, vec, sizeof(GLfloat) * uniform_->GetComponentCount());
		return true;
	}

	bool GLUniformCache::SetFloatValue(GLfloat v0)
	{
		DEATH_ASSERT(uniform_ == nullptr || dataPointer_ != nullptr);
		if (uniform_ == nullptr || dataPointer_ == nullptr || !CheckFloat() || !CheckComponents(1)) {
			return false;
		}

		isDirty_ = true;
		GLfloat* data = reinterpret_cast<GLfloat*>(dataPointer_);
		data[0] = v0;
		return true;
	}

	bool GLUniformCache::SetFloatValue(GLfloat v0, GLfloat v1)
	{
		DEATH_ASSERT(uniform_ == nullptr || dataPointer_ != nullptr);
		if (uniform_ == nullptr || dataPointer_ == nullptr || !CheckFloat() || !CheckComponents(2)) {
			return false;
		}

		isDirty_ = true;
		GLfloat* data = reinterpret_cast<GLfloat*>(dataPointer_);
		data[0] = v0;
		data[1] = v1;
		return true;
	}

	bool GLUniformCache::SetFloatValue(GLfloat v0, GLfloat v1, GLfloat v2)
	{
		DEATH_ASSERT(uniform_ == nullptr || dataPointer_ != nullptr);
		if (uniform_ == nullptr || dataPointer_ == nullptr || !CheckFloat() || !CheckComponents(3)) {
			return false;
		}

		isDirty_ = true;
		GLfloat* data = reinterpret_cast<GLfloat*>(dataPointer_);
		data[0] = v0;
		data[1] = v1;
		data[2] = v2;
		return true;
	}

	bool GLUniformCache::SetFloatValue(GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3)
	{
		DEATH_ASSERT(uniform_ == nullptr || dataPointer_ != nullptr);
		if (uniform_ == nullptr || dataPointer_ == nullptr || !CheckFloat() || !CheckComponents(4)) {
			return false;
		}

		isDirty_ = true;
		GLfloat* data = reinterpret_cast<GLfloat*>(dataPointer_);
		data[0] = v0;
		data[1] = v1;
		data[2] = v2;
		data[3] = v3;
		return true;
	}

	bool GLUniformCache::SetIntVector(const GLint* vec)
	{
		DEATH_ASSERT(uniform_ == nullptr || dataPointer_ != nullptr);
		if (uniform_ == nullptr || dataPointer_ == nullptr || !CheckInt()) {
			return false;
		}

		isDirty_ = true;
		std::memcpy(dataPointer_, vec, sizeof(GLint) * uniform_->GetComponentCount());
		return true;
	}

	bool GLUniformCache::SetIntValue(GLint v0)
	{
		DEATH_ASSERT(uniform_ == nullptr || dataPointer_ != nullptr);
		if (uniform_ == nullptr || dataPointer_ == nullptr || !CheckInt() || !CheckComponents(1)) {
			return false;
		}

		isDirty_ = true;
		GLint* data = reinterpret_cast<GLint*>(dataPointer_);
		data[0] = v0;
		return true;
	}

	bool GLUniformCache::SetIntValue(GLint v0, GLint v1)
	{
		DEATH_ASSERT(uniform_ == nullptr || dataPointer_ != nullptr);
		if (uniform_ == nullptr || dataPointer_ == nullptr || !CheckInt() || !CheckComponents(2)) {
			return false;
		}

		isDirty_ = true;
		GLint* data = reinterpret_cast<GLint*>(dataPointer_);
		data[0] = v0;
		data[1] = v1;
		return true;
	}

	bool GLUniformCache::SetIntValue(GLint v0, GLint v1, GLint v2)
	{
		DEATH_ASSERT(uniform_ == nullptr || dataPointer_ != nullptr);
		if (uniform_ == nullptr || dataPointer_ == nullptr || !CheckInt() || !CheckComponents(3)) {
			return false;
		}

		isDirty_ = true;
		GLint* data = reinterpret_cast<GLint*>(dataPointer_);
		data[0] = v0;
		data[1] = v1;
		data[2] = v2;
		return true;
	}

	bool GLUniformCache::SetIntValue(GLint v0, GLint v1, GLint v2, GLint v3)
	{
		DEATH_ASSERT(uniform_ == nullptr || dataPointer_ != nullptr);
		if (uniform_ == nullptr || dataPointer_ == nullptr || !CheckInt() || !CheckComponents(4)) {
			return false;
		}

		isDirty_ = true;
		GLint* data = reinterpret_cast<GLint*>(dataPointer_);
		data[0] = v0;
		data[1] = v1;
		data[2] = v2;
		data[3] = v3;
		return true;
	}

	bool GLUniformCache::CommitValue()
	{
		DEATH_ASSERT(uniform_ == nullptr || dataPointer_ != nullptr);
		if (uniform_ == nullptr || dataPointer_ == nullptr || !isDirty_) {
			return false;
		}

		// The uniform must not belong to any uniform block
		DEATH_ASSERT(uniform_->GetBlockIndex() == -1);

		const GLint location = uniform_->GetLocation();
		switch (uniform_->GetType()) {
			case GL_FLOAT:
				glUniform1fv(location, 1, reinterpret_cast<const GLfloat*>(dataPointer_));
				break;
			case GL_FLOAT_VEC2:
				glUniform2fv(location, 1, reinterpret_cast<const GLfloat*>(dataPointer_));
				break;
			case GL_FLOAT_VEC3:
				glUniform3fv(location, 1, reinterpret_cast<const GLfloat*>(dataPointer_));
				break;
			case GL_FLOAT_VEC4:
				glUniform4fv(location, 1, reinterpret_cast<const GLfloat*>(dataPointer_));
				break;
			case GL_INT:
				glUniform1iv(location, 1, reinterpret_cast<const GLint*>(dataPointer_));
				break;
			case GL_INT_VEC2:
				glUniform2iv(location, 1, reinterpret_cast<const GLint*>(dataPointer_));
				break;
			case GL_INT_VEC3:
				glUniform3iv(location, 1, reinterpret_cast<const GLint*>(dataPointer_));
				break;
			case GL_INT_VEC4:
				glUniform4iv(location, 1, reinterpret_cast<const GLint*>(dataPointer_));
				break;
			case GL_FLOAT_MAT2:
				glUniformMatrix2fv(location, 1, GL_FALSE, reinterpret_cast<const GLfloat*>(dataPointer_));
				break;
			case GL_FLOAT_MAT3:
				glUniformMatrix3fv(location, 1, GL_FALSE, reinterpret_cast<const GLfloat*>(dataPointer_));
				break;
			case GL_FLOAT_MAT4:
				glUniformMatrix4fv(location, 1, GL_FALSE, reinterpret_cast<const GLfloat*>(dataPointer_));
				break;
#if !defined(WITH_OPENGLES) // not available in OpenGL ES
			case GL_SAMPLER_1D:
#endif
			case GL_SAMPLER_2D:
			case GL_SAMPLER_3D:
			case GL_SAMPLER_CUBE:
#if !defined(WITH_OPENGLES) || (defined(WITH_OPENGLES) && GL_ES_VERSION_3_2)
			case GL_SAMPLER_BUFFER:
#endif
				glUniform1iv(location, 1, reinterpret_cast<const GLint*>(dataPointer_));
				break;
			default:
				LOGW("No available case to handle type: {}", uniform_->GetType());
				break;
		}

		isDirty_ = false;
		return true;
	}

	bool GLUniformCache::CheckFloat() const
	{
		if (uniform_->GetBasicType() != GL_FLOAT) {
			LOGE("Uniform \"{}\" is not floating point", uniform_->GetName());
			return false;
		} else {
			return true;
		}
	}

	bool GLUniformCache::CheckInt() const
	{
		if (uniform_->GetBasicType() != GL_INT &&
			uniform_->GetBasicType() != GL_BOOL &&
#if !defined(WITH_OPENGLES) // not available in OpenGL ES
			uniform_->GetBasicType() != GL_SAMPLER_1D &&
#endif
			uniform_->GetBasicType() != GL_SAMPLER_2D &&
			uniform_->GetBasicType() != GL_SAMPLER_3D &&
			uniform_->GetBasicType() != GL_SAMPLER_CUBE
#if !defined(WITH_OPENGLES) || (defined(WITH_OPENGLES) && GL_ES_VERSION_3_2)
			&& uniform_->GetBasicType() != GL_SAMPLER_BUFFER
#endif
		) {
			LOGE("Uniform \"{}\" is not integer", uniform_->GetName());
			return false;
		} else {
			return true;
		}
	}

	bool GLUniformCache::CheckComponents(std::uint32_t requiredComponents) const
	{
		if (uniform_->GetComponentCount() != requiredComponents) {
			LOGE("Uniform \"{}\" has {} components, not {}", uniform_->GetName(), uniform_->GetComponentCount(), requiredComponents);
			return false;
		} else {
			return true;
		}
	}
}