Gamp v0.0.7-54-gccdc599
Gamp: Graphics, Audio, Multimedia and Processing
Loading...
Searching...
No Matches
GLUniformData.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright Gothel Software e.K.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * This Source Code Form is subject to the terms of the MIT License
8 * If a copy of the MIT was not distributed with this file,
9 * you can obtain one at https://opensource.org/license/mit/.
10 */
11
12#ifndef GAMP_GLSLUNIFORMDATA_HPP_
13#define GAMP_GLSLUNIFORMDATA_HPP_
14
15#include <cstddef>
16#include <memory>
17
18#include <jau/basic_types.hpp>
19#include <jau/cpp_lang_util.hpp>
20#include <jau/debug.hpp>
21#include <jau/float_types.hpp>
22#include <jau/io/file_util.hpp>
23#include <jau/io/io_util.hpp>
24#include <jau/math/vec4f.hpp>
25#include <jau/string_util.hpp>
27
28#include <gamp/Gamp.hpp>
33
34namespace gamp::render::gl::data {
35 using namespace gamp::render::gl;
36 using namespace jau::math::util;
37
38 /** \addtogroup Gamp_GLData
39 *
40 * @{
41 */
42
43 /**
44 * GLSL uniform data wrapper encapsulating data to be uploaded to the GPU as a uniform.
45 */
47 public:
48 virtual ~GLUniformData() noexcept = default;
49
50 /** Return the underlying data buffer pointer. */
51 virtual const void* data() const noexcept = 0;
52
53 /** Returns type signature of implementing class's stored component value type. */
54 const jau::type_info& compSignature() const noexcept { return m_signature; }
55
56 /** Return the uniform name as used in the shader */
57 const string_t& name() const noexcept { return m_name; }
58
59 /// Returns the uniform's location, -1 if no location has been retrieved or set
60 constexpr GLint location() const noexcept { return m_location; }
61
62 /**
63 * Sets the given location of the shader uniform.
64 * @return the given location
65 */
66 GLint setLocation(GLint location) noexcept { m_location=location; return location; }
67
68 /**
69 * Retrieves the location of the shader uniform with {@link #getName()} from the linked shader program.
70 * <p>
71 * No validation is performed within the implementation.
72 * </p>
73 * @param gl
74 * @param program
75 * @return &ge;0 denotes a valid uniform location as found and used in the given shader program.
76 * &lt;0 denotes an invalid location, i.e. not found or used in the given shader program.
77 */
78 int setLocation(const GL&, GLuint program) noexcept {
79 m_location = ::glGetUniformLocation(program, m_name.c_str());
80 return m_location;
81 }
82
83 /** Returns element count. One element consist compsPerElem() components. */
84 constexpr GLsizei count() const noexcept { return m_count; }
85 /** Returns component count per element, i.e. rows() * columns(). */
86 constexpr GLsizei components() const noexcept { return m_rows*m_columns; }
87 /** Returns row count, i.e. matrices usually have a row count > 1. */
88 constexpr GLsizei rows() const noexcept { return m_rows; }
89 /** Returns column count, i.e. matrices and vectors usually have a column count > 1. */
90 constexpr GLsizei columns() const noexcept { return m_columns; }
91 /** Return true if rows > 1 && columns > 1 */
92 constexpr bool isMatrix() const noexcept { return m_rows > 1 && m_columns > 1; }
93 /** Return true if rows == 1 && columns > 1 */
94 constexpr bool isVector() const noexcept { return m_rows == 1 && m_columns > 1; }
95 /** Return true if rows == 1 && columns == 1 */
96 constexpr bool isScalar() const noexcept { return m_rows == 1 && m_columns == 1; }
97
99 string_t sb("GLUniformData[");
100 sb.append(compSignature().name())
101 .append(", name ").append(m_name)
102 .append(", location ").append(std::to_string(m_location))
103 .append(", size ").append(std::to_string(m_rows)).append("x").append(std::to_string(m_columns))
104 .append(", count ").append(std::to_string(m_count))
105 .append(", data ");
106 if( isMatrix() ) {
108 sb.append("\n");
109 const GLfloat* d = reinterpret_cast<const GLfloat*>(data());
110 for( GLsizei i = 0; i < m_count; ++i ) {
111 if( i > 0 ) { sb.append("\n"); }
112 jau::mat_to_string<GLfloat>(sb, std::to_string(i)+": ", "%10.5f",
113 d+static_cast<ptrdiff_t>(i*m_rows*m_columns), m_rows, m_columns, false);
114 }
115 }
116 } else if( isVector() ) {
118 const GLfloat* d = reinterpret_cast<const GLfloat*>(data());
119 jau::row_to_string<GLfloat>(sb, "%10.5f", d, 1, m_columns, false, 0);
120 }
121 } else {
123 const GLfloat d = *reinterpret_cast<const GLfloat*>(data());
124 sb.append(std::to_string(d));
125 } else if( compSignature() == jau::int_ctti::i32() ) {
126 const GLint d = *reinterpret_cast<const GLint*>(data());
127 sb.append(std::to_string(d));
128 }
129 }
130 sb.append(" ]");
131 return sb;
132 }
133
134 GLUniformData(const GLUniformData&) = delete;
137
138 /** Sends the uniform data to the GPU, i.e. issues, [glUniform](https://docs.gl/es3/glUniform) */
139 void send(const GL& gl) const;
140
141 protected:
142 GLUniformData(const jau::type_info& sig, std::string_view name, GLint location,
143 GLsizei rows, GLsizei columns, size_t count)
144 : m_signature(sig), m_name(name), m_location(location),
145 m_rows(rows), m_columns(columns), m_count(castOrThrow<size_t, GLsizei>(count)) {}
146
147 private:
148 const jau::type_info& m_signature;
149 std::string m_name;
150 GLint m_location;
151 GLsizei m_rows, m_columns, m_count;
152 };
153 typedef std::shared_ptr<GLUniformData> GLUniformDataRef;
154
155 inline std::ostream& operator<<(std::ostream& out, const GLUniformData& v) {
156 return out << v.toString();
157 }
158
160 public:
162
163 private:
164 PMVMat4f& m_mat;
165 mutable SyncMats4f m_data;
166
167 public:
170 /*location=*/-1, /*rows=*/4, /*columns=*/4, /*count=*/data.matrixCount()),
171 m_mat(mat), m_data(data) {}
172
173 static std::shared_ptr<GLUniformSyncPMVMat4fExt> create(stringview_t name, PMVMat4f &mat, SyncMats4f &&data) {
174 return std::make_shared<GLUniformSyncPMVMat4fExt>(name, mat, std::move(data));
175 }
176 const void* data() const noexcept override { return m_data.syncedData(); }
177
178 constexpr const GLfloat* floats() const noexcept { return m_data.floats(); }
179
180 constexpr const PMVMat4f& pmv() const noexcept { return m_mat; }
181 constexpr PMVMat4f& pmv() noexcept { return m_mat; }
182 };
183 typedef std::shared_ptr<GLUniformSyncPMVMat4fExt> GLUniformSyncPMVMat4fExtRef;
184
186 public:
188 private:
189 PMVMat4f m_mat;
190 mutable SyncMats4f m_data;
191
192 public:
195 /*location=*/-1, /*rows=*/4, /*columns=*/4, /*count=*/mat.matrixCount()),
196 m_mat(mat), m_data(m_mat.makeSyncPMvReq()) {}
197
199 : GLUniformSyncPMVMat4f(name, PMVMat4f(derivedMatrices)) {}
200
201 static std::shared_ptr<GLUniformSyncPMVMat4f> create(stringview_t name, PMVData derivedMatrices=PMVData::none) {
202 return std::make_shared<GLUniformSyncPMVMat4f>(name, derivedMatrices);
203 }
204 const void* data() const noexcept override { return m_data.syncedData(); }
205
206 constexpr const GLfloat* floats() const noexcept { return m_data.floats(); }
207
208 constexpr const PMVMat4f& pmv() const noexcept { return m_mat; }
209 constexpr PMVMat4f& pmv() noexcept { return m_mat; }
210 };
211 typedef std::shared_ptr<GLUniformSyncPMVMat4f> GLUniformSyncPMVMat4fRef;
212
214 public:
216 private:
217 mutable SyncMats4f m_data;
218 public:
221 /*location=*/-1, /*rows=*/4, /*columns=*/4, /*count=*/data.matrixCount()),
222 m_data(data) {}
223
224 static std::shared_ptr<GLUniformSyncMatrices4f> create(stringview_t name, SyncMats4f &&data) {
225 return std::make_shared<GLUniformSyncMatrices4f>(name, std::move(data));
226 }
227 const void* data() const noexcept override { return m_data.syncedData(); }
228
229 constexpr const GLfloat* floats() const noexcept { return m_data.floats(); }
230 };
231 typedef std::shared_ptr<GLUniformSyncMatrices4f> GLUniformSyncMatrices4fRef;
232
234 private:
235 jau::math::Vec4f m_data;
236
237 public:
239 : GLUniformData(jau::float_ctti::f32(), name,
240 /*location=*/-1, /*rows=*/1, /*columns=*/4, /*count=*/1),
241 m_data(v) {}
242
243 static std::shared_ptr<GLUniformVec4f> create(stringview_t name, const jau::math::Vec4f& v) {
244 return std::make_shared<GLUniformVec4f>(name, v);
245 }
246 const void* data() const noexcept override { return m_data.cbegin(); }
247
248 constexpr const jau::math::Vec4f& vec4f() const noexcept { return m_data; }
249 constexpr jau::math::Vec4f& vec4f() noexcept { return m_data; }
250 };
251 typedef std::shared_ptr<GLUniformVec4f> GLUniformVec4fRef;
252
254 private:
255 jau::math::Vec3f m_data;
256
257 public:
259 : GLUniformData(jau::float_ctti::f32(), name,
260 /*location=*/-1, /*rows=*/1, /*columns=*/3, /*count=*/1),
261 m_data(v) {}
262
263 static std::shared_ptr<GLUniformVec3f> create(stringview_t name, const jau::math::Vec3f& v) {
264 return std::make_shared<GLUniformVec3f>(name, v);
265 }
266 const void* data() const noexcept override { return m_data.cbegin(); }
267
268 constexpr const jau::math::Vec3f& vec3f() const noexcept { return m_data; }
269 constexpr jau::math::Vec3f& vec3f() noexcept { return m_data; }
270 };
271 typedef std::shared_ptr<GLUniformVec3f> GLUniformVec3fRef;
272
274 private:
275 jau::math::Vec2f m_data;
276
277 public:
279 : GLUniformData(jau::float_ctti::f32(), name,
280 /*location=*/-1, /*rows=*/1, /*columns=*/2, /*count=*/1),
281 m_data(v) {}
282
283 static std::shared_ptr<GLUniformVec2f> create(stringview_t name, const jau::math::Vec2f& v) {
284 return std::make_shared<GLUniformVec2f>(name, v);
285 }
286 const void* data() const noexcept override { return m_data.cbegin(); }
287
288 constexpr const jau::math::Vec2f& vec2f() const noexcept { return m_data; }
289 constexpr jau::math::Vec2f& vec2f() noexcept { return m_data; }
290 };
291 typedef std::shared_ptr<GLUniformVec2f> GLUniformVec2fRef;
292
294 private:
295 float m_data;
296
297 public:
299 : GLUniformData(jau::float_ctti::f32(), name,
300 /*location=*/-1, /*rows=*/1, /*columns=*/1, /*count=*/1),
301 m_data(v) {}
302
303 static std::shared_ptr<GLUniformScalarF32> create(stringview_t name, float v) {
304 return std::make_shared<GLUniformScalarF32>(name, v);
305 }
306 const void* data() const noexcept override { return &m_data; }
307
308 constexpr float scalar() const noexcept { return m_data; }
309 constexpr float& scalar() noexcept { return m_data; }
310 };
311 typedef std::shared_ptr<GLUniformScalarF32> GLUniformScalarF32Ref;
312
313 /**@}*/
314}
315
316#endif // GAMP_GLSLUNIFORMDATA_HPP_
GLSL uniform data wrapper encapsulating data to be uploaded to the GPU as a uniform.
constexpr GLint location() const noexcept
Returns the uniform's location, -1 if no location has been retrieved or set.
GLUniformData(const jau::type_info &sig, std::string_view name, GLint location, GLsizei rows, GLsizei columns, size_t count)
int setLocation(const GL &, GLuint program) noexcept
Retrieves the location of the shader uniform with getName() from the linked shader program.
const string_t & name() const noexcept
Return the uniform name as used in the shader.
virtual const void * data() const noexcept=0
Return the underlying data buffer pointer.
constexpr GLsizei columns() const noexcept
Returns column count, i.e.
virtual ~GLUniformData() noexcept=default
GLUniformData(GLUniformData &&)=delete
GLUniformData & operator=(const GLUniformData &)=delete
constexpr GLsizei count() const noexcept
Returns element count.
constexpr bool isMatrix() const noexcept
Return true if rows > 1 && columns > 1.
constexpr GLsizei components() const noexcept
Returns component count per element, i.e.
GLint setLocation(GLint location) noexcept
Sets the given location of the shader uniform.
constexpr bool isScalar() const noexcept
Return true if rows == 1 && columns == 1.
constexpr GLsizei rows() const noexcept
Returns row count, i.e.
const jau::type_info & compSignature() const noexcept
Returns type signature of implementing class's stored component value type.
constexpr bool isVector() const noexcept
Return true if rows == 1 && columns > 1.
void send(const GL &gl) const
Sends the uniform data to the GPU, i.e.
Definition gamp_gl.cpp:330
GLUniformData(const GLUniformData &)=delete
static std::shared_ptr< GLUniformScalarF32 > create(stringview_t name, float v)
GLUniformScalarF32(stringview_t name, float v)
constexpr float scalar() const noexcept
const void * data() const noexcept override
Return the underlying data buffer pointer.
constexpr const GLfloat * floats() const noexcept
GLUniformSyncMatrices4f(stringview_t name, SyncMats4f &&data)
static std::shared_ptr< GLUniformSyncMatrices4f > create(stringview_t name, SyncMats4f &&data)
const void * data() const noexcept override
Return the underlying data buffer pointer.
static std::shared_ptr< GLUniformSyncPMVMat4fExt > create(stringview_t name, PMVMat4f &mat, SyncMats4f &&data)
constexpr const GLfloat * floats() const noexcept
const void * data() const noexcept override
Return the underlying data buffer pointer.
GLUniformSyncPMVMat4fExt(stringview_t name, PMVMat4f &mat, SyncMats4f &&data)
constexpr const PMVMat4f & pmv() const noexcept
static std::shared_ptr< GLUniformSyncPMVMat4f > create(stringview_t name, PMVData derivedMatrices=PMVData::none)
constexpr const GLfloat * floats() const noexcept
const void * data() const noexcept override
Return the underlying data buffer pointer.
GLUniformSyncPMVMat4f(stringview_t name, PMVMat4f &&mat)
constexpr const PMVMat4f & pmv() const noexcept
GLUniformSyncPMVMat4f(stringview_t name, PMVData derivedMatrices)
GLUniformVec2f(stringview_t name, const jau::math::Vec2f &v)
constexpr jau::math::Vec2f & vec2f() noexcept
static std::shared_ptr< GLUniformVec2f > create(stringview_t name, const jau::math::Vec2f &v)
constexpr const jau::math::Vec2f & vec2f() const noexcept
const void * data() const noexcept override
Return the underlying data buffer pointer.
static std::shared_ptr< GLUniformVec3f > create(stringview_t name, const jau::math::Vec3f &v)
constexpr jau::math::Vec3f & vec3f() noexcept
constexpr const jau::math::Vec3f & vec3f() const noexcept
const void * data() const noexcept override
Return the underlying data buffer pointer.
GLUniformVec3f(stringview_t name, const jau::math::Vec3f &v)
static std::shared_ptr< GLUniformVec4f > create(stringview_t name, const jau::math::Vec4f &v)
constexpr const jau::math::Vec4f & vec4f() const noexcept
GLUniformVec4f(stringview_t name, const jau::math::Vec4f &v)
const void * data() const noexcept override
Return the underlying data buffer pointer.
constexpr jau::math::Vec4f & vec4f() noexcept
static const jau::type_info & f32()
jau::float_32_t or just float
static const jau::type_info & i32()
int32_t
SyncBuffer interface with multiple underlying Matrix4.
Generic type information using either Runtime type information (RTTI) or Compile time type informatio...
std::ostream & operator<<(std::ostream &os, const T v)
std::string & row_to_string(std::string &sb, const std::string_view f, const T a[], const jau::nsize_t rows, const jau::nsize_t columns, const bool rowMajorOrder, const jau::nsize_t row) noexcept
Appends a row of floating points to the given string sb
std::string & mat_to_string(std::string &sb, const std::string &rowPrefix, const std::string_view f, const T a[], const jau::nsize_t rows, const jau::nsize_t columns, const bool rowMajorOrder) noexcept
Appends a matrix of floating points to the given string sb
std::shared_ptr< GLUniformSyncPMVMat4f > GLUniformSyncPMVMat4fRef
std::shared_ptr< GLUniformSyncMatrices4f > GLUniformSyncMatrices4fRef
std::shared_ptr< GLUniformData > GLUniformDataRef
std::shared_ptr< GLUniformScalarF32 > GLUniformScalarF32Ref
std::shared_ptr< GLUniformVec4f > GLUniformVec4fRef
std::shared_ptr< GLUniformVec3f > GLUniformVec3fRef
std::shared_ptr< GLUniformVec2f > GLUniformVec2fRef
std::shared_ptr< GLUniformSyncPMVMat4fExt > GLUniformSyncPMVMat4fExtRef
std::string_view stringview_t
U castOrThrow(T has)
Definition GampTypes.hpp:70
Vector4F< float > Vec4f
Definition vec4f.hpp:360
PMVData
PMVMatrix4 derived matrices and values.
Definition pmvmat4.hpp:57
Vector2F< float > Vec2f
Definition vec2f.hpp:404
Vector3F< float > Vec3f
Definition vec3f.hpp:422
PMVMatrix4< float > PMVMat4f
Definition pmvmat4.hpp:1463
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Definition backtrace.hpp:32