jaulib v1.3.6
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
syncbuffer.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2014-2024 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24#ifndef JAU_SYNCBUFFER_HPP_
25#define JAU_SYNCBUFFER_HPP_
26
27#include <cmath>
28#include <cstdarg>
29#include <cassert>
30
31#include <jau/float_types.hpp>
32#include <jau/functional.hpp>
33#include <jau/math/mat4f.hpp>
34
35namespace jau::math::util {
36
37 /** \addtogroup Math
38 *
39 * @{
40 */
41
42 /**
43 * Specific data synchronization action implemented by the data provider
44 * to update the buffer with the underlying data before usage, e.g. uploading the `GLUniformData` data to the GPU.
45 */
47
48 /** Plain function pointer type matching sync_action_t */
49 typedef void(*sync_action_fptr)();
50
51 /**
52 * Convenient tuple of a sync_action_t and data buffer.
53 *
54 * sync_action_t is used to update the data buffer in case it is derived
55 * or must be otherwise transported, defined by the data provider.
56 */
57 class SyncBuffer {
58 public:
59 virtual ~SyncBuffer() noexcept = default;
60
61 /** Returns type signature of implementing class's stored value type. */
62 virtual const jau::type_info& valueSignature() const noexcept = 0;
63
64 /** Return the defined sync_action_t */
65 virtual sync_action_t& action() noexcept = 0;
66
67 /** Return the underlying data buffer as bytes. */
68 virtual const void* data() const noexcept = 0;
69
70 /** Returns element size in bytes */
71 virtual size_t elementSize() const noexcept = 0;
72
73 /** Returns element count, total byte size = elementSize() * elementCount() */
74 virtual size_t elementCount() const noexcept = 0;
75
76 size_t byteSize() const noexcept { return elementSize() * elementCount(); }
77
78 /**
79 * Synchronizes the underlying data before usage.
80 * <p>
81 * Convenient shortcut for action()() plus chaining.
82 * </p>
83 */
84 SyncBuffer& sync() noexcept { action()(); return *this; }
85
86 std::string toString() const {
87 return std::string("SyncBuffer[").append(valueSignature().name())
88 .append(", count ").append(std::to_string(elementCount()))
89 .append(" x ").append(std::to_string(elementSize())).append("]");
90 }
91
92 /** Return the underlying data buffer as int8_t if valueSignature() matches, otherwise throw. */
93 const int8_t* data_i8() const {
94 const jau::type_info& t = valueSignature();
95 if( t == jau::int_ctti::i8() ) {
96 return reinterpret_cast<const int8_t*>(data());
97 } else {
98 throw jau::IllegalArgumentError("Storage type `"+t.name()+"`, not matching requested type `"+jau::int_ctti::i8().name()+"`", E_FILE_LINE);
99 }
100 }
101 /** Return the underlying data buffer as int32_t if valueSignature() matches, otherwise throw. */
102 const int32_t* data_i32() const {
103 const jau::type_info& t = valueSignature();
104 if( t == jau::int_ctti::i32() ) {
105 return reinterpret_cast<const int32_t*>(data());
106 } else {
107 throw jau::IllegalArgumentError("Storage type `"+t.name()+"`, not matching requested type `"+jau::int_ctti::i32().name()+"`", E_FILE_LINE);
108 }
109 }
110 /** Return the underlying data buffer as float32_t if valueSignature() matches, otherwise throw. */
111 const float32_t* data_f32() const {
112 const jau::type_info& t = valueSignature();
113 if( t == jau::float_ctti::f32() ) {
114 return reinterpret_cast<const float32_t*>(data());
115 } else {
116 throw jau::IllegalArgumentError("Storage type `"+t.name()+"`, not matching requested type `"+jau::float_ctti::f32().name()+"`", E_FILE_LINE);
117 }
118 }
119
120 };
121
122 /** SyncBuffer interface with a single underlying Matrix4. */
123 template<typename Value_type,
124 std::enable_if_t<std::is_floating_point_v<Value_type>, bool> = true>
125 class SyncMatrix4 : public SyncBuffer {
126 public:
129
130 /** Return the underlying Mat4, used to synchronize via action() to the buffer(). */
131 virtual const Mat4& matrix() const noexcept = 0;
132
133 /** Return the underlying float data buffer. */
134 const value_type* floats() const noexcept { return matrix().cbegin(); }
135
136 const jau::type_info& valueSignature() const noexcept override { return jau::static_ctti<value_type>(); }
137 const void* data() const noexcept override { return floats(); }
138 size_t elementSize() const noexcept override { return sizeof(float); }
139 size_t elementCount() const noexcept override { return 16; }
140 };
142
143
144 /** SyncBuffer interface with multiple underlying Matrix4. */
145 template<typename Value_type,
146 std::enable_if_t<std::is_floating_point_v<Value_type>, bool> = true>
147 class SyncMatrices4 : public SyncBuffer {
148 public:
151
152 /** Return the underlying Mat4 pointer, used to synchronize via action() to the buffer(). */
153 virtual const Mat4* matrices() const noexcept = 0;
154 /** Return the number of Mat4 referenced by matrices() */
155 virtual size_t matrixCount() const noexcept = 0;
156
157 /** Return the underlying float data buffer. */
158 const value_type* floats() const noexcept { return matrices()[0].cbegin(); }
159
160 const jau::type_info& valueSignature() const noexcept override { return jau::static_ctti<value_type>(); }
161 const void* data() const noexcept override { return floats(); }
162 size_t elementSize() const noexcept override { return sizeof(float); }
163 size_t elementCount() const noexcept override { return 16 * matrixCount(); }
164 };
166
167 /**@}*/
168
169 } // namespace jau::math::util
170
171 #endif // JAU_SYNCBUFFER_HPP_
#define E_FILE_LINE
static const jau::type_info & f32()
Class template jau::function is a general-purpose static-polymorphic function wrapper.
static const jau::type_info & i32()
static const jau::type_info & i8()
Basic 4x4 value_type matrix implementation using fields for intensive use-cases (host operations).
Definition mat4f.hpp:113
constexpr const_iterator cbegin() const noexcept
Definition mat4f.hpp:313
Convenient tuple of a sync_action_t and data buffer.
virtual const jau::type_info & valueSignature() const noexcept=0
Returns type signature of implementing class's stored value type.
virtual sync_action_t & action() noexcept=0
Return the defined sync_action_t.
virtual ~SyncBuffer() noexcept=default
const int32_t * data_i32() const
Return the underlying data buffer as int32_t if valueSignature() matches, otherwise throw.
std::string toString() const
SyncBuffer & sync() noexcept
Synchronizes the underlying data before usage.
const float32_t * data_f32() const
Return the underlying data buffer as float32_t if valueSignature() matches, otherwise throw.
const int8_t * data_i8() const
Return the underlying data buffer as int8_t if valueSignature() matches, otherwise throw.
virtual const void * data() const noexcept=0
Return the underlying data buffer as bytes.
virtual size_t elementSize() const noexcept=0
Returns element size in bytes.
size_t byteSize() const noexcept
virtual size_t elementCount() const noexcept=0
Returns element count, total byte size = elementSize() * elementCount()
SyncBuffer interface with multiple underlying Matrix4.
size_t elementSize() const noexcept override
Returns element size in bytes.
const void * data() const noexcept override
Return the underlying data buffer as bytes.
virtual const Mat4 * matrices() const noexcept=0
Return the underlying Mat4 pointer, used to synchronize via action() to the buffer().
Matrix4< value_type, std::is_floating_point_v< value_type > > Mat4
size_t elementCount() const noexcept override
Returns element count, total byte size = elementSize() * elementCount()
const jau::type_info & valueSignature() const noexcept override
Returns type signature of implementing class's stored value type.
SyncBuffer interface with a single underlying Matrix4.
Matrix4< value_type, std::is_floating_point_v< value_type > > Mat4
size_t elementCount() const noexcept override
Returns element count, total byte size = elementSize() * elementCount()
virtual const Mat4 & matrix() const noexcept=0
Return the underlying Mat4, used to synchronize via action() to the buffer().
const void * data() const noexcept override
Return the underlying data buffer as bytes.
const jau::type_info & valueSignature() const noexcept override
Returns type signature of implementing class's stored value type.
size_t elementSize() const noexcept override
Returns element size in bytes.
Generic type information using either Runtime type information (RTTI) or Compile time type informatio...
const jau::type_info & static_ctti() noexcept
Returns a static global reference of make_ctti<T>(true) w/ identity instance.
constexpr std::string_view name(const Bool v) noexcept
float float32_t
SyncMatrices4< float > SyncMats4f
jau::function< void()> sync_action_t
Specific data synchronization action implemented by the data provider to update the buffer with the u...
void(* sync_action_fptr)()
Plain function pointer type matching sync_action_t.
SyncMatrix4< float > SyncMat4f
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Definition backtrace.hpp:32
uint8_t Value_type