jaulib v1.3.8
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 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 JAU_MATH_UTIL_SYNCBUFFER_HPP_
13#define JAU_MATH_UTIL_SYNCBUFFER_HPP_
14
15#include <cmath>
16#include <cstdarg>
17#include <cassert>
18
19#include <jau/float_types.hpp>
20#include <jau/functional.hpp>
21#include <jau/math/mat4f.hpp>
22
23namespace jau::math::util {
24
25 /** \addtogroup Math
26 *
27 * @{
28 */
29
30 /**
31 * Specific data synchronization action implemented by the data provider
32 * to update the buffer with the underlying data before usage, e.g. uploading the `GLUniformData` data to the GPU.
33 */
35
36 /** Plain function pointer type matching sync_action_t */
37 typedef void(*sync_action_fptr)();
38
39 /**
40 * Convenient tuple of a sync_action_t and data buffer.
41 *
42 * sync_action_t is used to update the data buffer in case it is derived
43 * or must be otherwise transported, defined by the data provider.
44 */
45 class SyncBuffer {
46 public:
47 virtual ~SyncBuffer() noexcept = default;
48
49 /** Return the defined sync_action_t */
50 virtual sync_action_t& action() noexcept = 0;
51
52 /** Return the underlying data buffer as bytes w/o sync action(). */
53 virtual const void* data() const noexcept = 0;
54
55 /** Return the underlying data buffer as bytes after invoking sync action(). */
56 const void* syncedData() noexcept { action()(); return data(); }
57
58 /** Returns type signature of implementing class's stored component value type. */
59 virtual const jau::type_info& compSignature() const noexcept = 0;
60
61 /** The component's size in bytes */
62 // virtual size_t bytesPerComp() const noexcept = 0;
63
64 /** The number of components per element */
65 // virtual size_t compsPerElem() const noexcept = 0;
66
67 /** Returns element count. One element consist compsPerElem() components. */
68 // virtual size_t elementCount() const noexcept = 0;
69
70 /** Returns the byte size of all elements, i.e. elementCount() * compsPerElem() * bytesPerComp(). */
71 // virtual size_t byteCount() const noexcept = 0;
72
73 /**
74 * Synchronizes the underlying data before usage.
75 * <p>
76 * Convenient shortcut for action()() plus chaining.
77 * </p>
78 */
79 SyncBuffer& sync() noexcept { action()(); return *this; }
80
81 virtual std::string toString() const = 0;
82 };
83
84 /** SyncBuffer interface with a single underlying Matrix4. */
85 template<typename Value_type,
86 std::enable_if_t<std::is_floating_point_v<Value_type>, bool> = true>
87 class SyncMatrix4 : public SyncBuffer {
88 public:
91
92 /** Return the underlying Mat4, used to synchronize via action() to the buffer(). */
93 virtual const Mat4& matrix() const noexcept = 0;
94
95 /** Return the underlying float data buffer. */
96 const value_type* floats() const noexcept { return matrix().cbegin(); }
97
98 const void* data() const noexcept override { return floats(); }
99
100 const jau::type_info& compSignature() const noexcept override { return jau::static_ctti<value_type>(); }
101 /** The component's size in bytes */
102 size_t bytesPerComp() const noexcept { return sizeof(value_type); }
103 /** The number of components per element */
104 size_t compsPerElem() const noexcept { return 16; }
105 /** Returns element count. One element consist compsPerElem() components. */
106 size_t elementCount() const noexcept { return 1; }
107 /** Returns the byte size of all elements, i.e. elementCount() * compsPerElem() * bytesPerComp(). */
108 size_t byteCount() const noexcept { return 1 * 16 * bytesPerComp(); }
109
110 std::string toString() const override {
111 return std::string("SyncMatrix4[").append(compSignature().name())
112 .append(", count ").append(std::to_string(elementCount()))
113 .append(" elem x ").append(std::to_string(compsPerElem()))
114 .append(" comp x ").append(std::to_string(bytesPerComp()))
115 .append(" = ").append(std::to_string(byteCount())).append(" bytes]");
116 }
117 };
119
120
121 /** SyncBuffer interface with multiple underlying Matrix4. */
122 template<typename Value_type,
123 std::enable_if_t<std::is_floating_point_v<Value_type>, bool> = true>
124 class SyncMatrices4 : public SyncBuffer {
125 public:
128
129 /** Return the underlying Mat4 pointer, used to synchronize via action() to the buffer(). */
130 virtual const Mat4* matrices() const noexcept = 0;
131 /** Return the number of Mat4 referenced by matrices() */
132 virtual size_t matrixCount() const noexcept = 0;
133
134 /** Return the underlying float data buffer. */
135 const value_type* floats() const noexcept { return matrices()[0].cbegin(); }
136
137 const void* data() const noexcept override { return floats(); }
138
139 const jau::type_info& compSignature() const noexcept override { return jau::static_ctti<value_type>(); }
140 /** The component's size in bytes */
141 size_t bytesPerComp() const noexcept { return sizeof(value_type); }
142 /** The number of components per element */
143 size_t compsPerElem() const noexcept { return 16; }
144 /** Returns element count. One element consist compsPerElem() components. */
145 size_t elementCount() const noexcept { return matrixCount(); }
146 /** Returns the byte size of all elements, i.e. elementCount() * compsPerElem() * bytesPerComp(). */
147 size_t byteCount() const noexcept { return matrixCount() * 16 * bytesPerComp(); }
148
149 std::string toString() const override {
150 return std::string("SyncMatrices4[").append(compSignature().name())
151 .append(", count ").append(std::to_string(elementCount()))
152 .append(" elem x ").append(std::to_string(compsPerElem()))
153 .append(" comp x ").append(std::to_string(bytesPerComp()))
154 .append(" = ").append(std::to_string(byteCount())).append(" bytes]");
155 }
156
157 };
159
160 /**@}*/
161
162 } // namespace jau::math::util
163
164 #endif // JAU_MATH_UTIL_SYNCBUFFER_HPP_
Class template jau::function is a general-purpose static-polymorphic function wrapper.
Basic 4x4 value_type matrix implementation using fields for intensive use-cases (host operations).
Definition mat4f.hpp:101
constexpr const_iterator cbegin() const noexcept
Definition mat4f.hpp:301
Convenient tuple of a sync_action_t and data buffer.
virtual sync_action_t & action() noexcept=0
Return the defined sync_action_t.
const void * syncedData() noexcept
Return the underlying data buffer as bytes after invoking sync action().
virtual const jau::type_info & compSignature() const noexcept=0
Returns type signature of implementing class's stored component value type.
virtual ~SyncBuffer() noexcept=default
SyncBuffer & sync() noexcept
The component's size in bytes.
virtual std::string toString() const =0
virtual const void * data() const noexcept=0
Return the underlying data buffer as bytes w/o sync action().
SyncBuffer interface with multiple underlying Matrix4.
size_t byteCount() const noexcept
Returns the byte size of all elements, i.e.
const void * data() const noexcept override
Return the underlying data buffer as bytes w/o sync action().
size_t bytesPerComp() const noexcept
The component's size in 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
std::string toString() const override
const jau::type_info & compSignature() const noexcept override
Returns type signature of implementing class's stored component value type.
size_t compsPerElem() const noexcept
The number of components per element.
size_t elementCount() const noexcept
Returns element count.
SyncBuffer interface with a single underlying Matrix4.
Matrix4< value_type, std::is_floating_point_v< value_type > > Mat4
size_t byteCount() const noexcept
Returns the byte size of all elements, i.e.
virtual const Mat4 & matrix() const noexcept=0
Return the underlying Mat4, used to synchronize via action() to the buffer().
std::string toString() const override
size_t compsPerElem() const noexcept
The number of components per element.
size_t bytesPerComp() const noexcept
The component's size in bytes.
const void * data() const noexcept override
Return the underlying data buffer as bytes w/o sync action().
const jau::type_info & compSignature() const noexcept override
Returns type signature of implementing class's stored component value type.
size_t elementCount() const noexcept
Returns element count.
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
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
uint8_t Value_type