jaulib v1.4.1
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#include <jau/type_concepts.hpp>
23
24namespace jau::math::util {
25
26 /** \addtogroup Math
27 *
28 * @{
29 */
30
31 /**
32 * Specific data synchronization action implemented by the data provider
33 * to update the buffer with the underlying data before usage, e.g. uploading the `GLUniformData` data to the GPU.
34 */
36
37 /** Plain function pointer type matching sync_action_t */
38 typedef void(*sync_action_fptr)();
39
40 /**
41 * Convenient tuple of a sync_action_t and data buffer.
42 *
43 * sync_action_t is used to update the data buffer in case it is derived
44 * or must be otherwise transported, defined by the data provider.
45 */
46 class SyncBuffer {
47 public:
48 virtual ~SyncBuffer() noexcept = default;
49
50 /** Return the defined sync_action_t */
51 virtual sync_action_t& action() noexcept = 0;
52
53 /** Return the underlying data buffer as bytes w/o sync action(). */
54 virtual const void* data() const noexcept = 0;
55
56 /** Return the underlying data buffer as bytes after invoking sync action(). */
57 const void* syncedData() noexcept { action()(); return data(); }
58
59 /** Returns type signature of implementing class's stored component value type. */
60 virtual const jau::type_info& compSignature() const noexcept = 0;
61
62 /** The component's size in bytes */
63 // virtual size_t bytesPerComp() const noexcept = 0;
64
65 /** The number of components per element */
66 // virtual size_t compsPerElem() const noexcept = 0;
67
68 /** Returns element count. One element consist compsPerElem() components. */
69 // virtual size_t elementCount() const noexcept = 0;
70
71 /** Returns the byte size of all elements, i.e. elementCount() * compsPerElem() * bytesPerComp(). */
72 // virtual size_t byteCount() const noexcept = 0;
73
74 /**
75 * Synchronizes the underlying data before usage.
76 * <p>
77 * Convenient shortcut for action()() plus chaining.
78 * </p>
79 */
80 SyncBuffer& sync() noexcept { action()(); return *this; }
81
82 virtual std::string toString() const = 0;
83 };
84
85 /** SyncBuffer interface with multiple underlying Matrix4. */
86 template <jau::req::packed_floating_point Value_type>
87 class SyncMatrices4 : public SyncBuffer {
88 public:
91
92 private:
93 const Mat4 &m_mat;
94 size_t m_count;
95 sync_action_t m_sync;
96
97 public:
98 SyncMatrices4(const Mat4 &first, size_t count, sync_action_t s = sync_action_t())
99 : m_mat(first), m_count(count), m_sync(std::move(s)) {}
100
101 /** Return the underlying Mat4 pointer, used to synchronize via action() to the buffer(). */
102 constexpr const Mat4* matrices() const noexcept { return &m_mat; }
103 /** Return the number of Mat4 referenced by matrices() */
104 constexpr size_t matrixCount() const noexcept { return m_count; }
105
106 /** Return the underlying float data buffer. */
107 constexpr const value_type* floats() const noexcept { return m_mat.cbegin(); }
108
109 sync_action_t& action() noexcept override { return m_sync; }
110 const void* data() const noexcept override { return floats(); }
111
112 /** Returns the component's value_type signature */
113 const jau::type_info& compSignature() const noexcept override { return jau::static_ctti<value_type>(); }
114 /** The component's size in bytes */
115 constexpr size_t bytesPerComp() const noexcept { return sizeof(value_type); }
116 /** The number of components per element */
117 constexpr size_t compsPerElem() const noexcept { return 16; }
118 /** Returns element count. One element consist compsPerElem() components. */
119 constexpr size_t elementCount() const noexcept { return matrixCount(); }
120 /** Returns the byte size of all elements, i.e. elementCount() * compsPerElem() * bytesPerComp(). */
121 constexpr size_t byteCount() const noexcept { return matrixCount() * 16 * bytesPerComp(); }
122
123 std::string toString() const override {
124 return std::string("SyncMatrices4[").append(compSignature().name())
125 .append(", count ").append(std::to_string(elementCount()))
126 .append(" elem x ").append(std::to_string(compsPerElem()))
127 .append(" comp x ").append(std::to_string(bytesPerComp()))
128 .append(" = ").append(std::to_string(byteCount())).append(" bytes]");
129 }
130
131 };
133
134 /**@}*/
135
136 } // namespace jau::math::util
137
138 #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:100
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.
const jau::type_info & compSignature() const noexcept override
Returns the component's value_type signature.
const void * data() const noexcept override
Return the underlying data buffer as bytes w/o sync action().
constexpr size_t matrixCount() const noexcept
Return the number of Mat4 referenced by matrices()
constexpr size_t byteCount() const noexcept
Returns the byte size of all elements, i.e.
std::string toString() const override
constexpr size_t bytesPerComp() const noexcept
The component's size in bytes.
SyncMatrices4(const Mat4 &first, size_t count, sync_action_t s=sync_action_t())
sync_action_t & action() noexcept override
Return the defined sync_action_t.
constexpr const value_type * floats() const noexcept
Return the underlying float data buffer.
constexpr size_t elementCount() const noexcept
Returns element count.
constexpr const Mat4 * matrices() const noexcept
Return the underlying Mat4 pointer, used to synchronize via action() to the buffer().
constexpr size_t compsPerElem() const noexcept
The number of components per element.
Generic type information using either Runtime type information (RTTI) or Compile time type informatio...
consteval_cxx20 std::string_view name() noexcept
const jau::type_info & static_ctti() noexcept
Returns a static global reference of make_ctti<T>(true) w/ identity instance.
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.
STL namespace.
uint8_t Value_type