Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
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 <cstdint>
30#include <cassert>
31#include <limits>
32#include <string>
33#include <vector>
34#include <initializer_list>
35#include <iostream>
36
37#include <jau/functional.hpp>
38#include <jau/math/mat4f.hpp>
39
40namespace jau::math::util {
41
42 /** \addtogroup Math
43 *
44 * @{
45 */
46
47 /**
48 * Specific data synchronization action implemented by the data provider
49 * to update the buffer with the underlying data before usage, e.g. uploading the `GLUniformData` data to the GPU.
50 */
52
53 /** Plain function pointer type matching sync_action_t */
54 typedef void(*sync_action_fptr)();
55
56 /**
57 * Convenient tuple of a sync_action_t and data buffer.
58 *
59 * sync_action_t is used to update the data buffer in case it is derived
60 * or must be otherwise transported, defined by the data provider.
61 */
62 class SyncBuffer {
63 public:
64 virtual ~SyncBuffer() noexcept = default;
65
66 /** Return the defined sync_action_t */
67 virtual sync_action_t& action() noexcept = 0;
68
69 /** Return the underlying data buffer. */
70 virtual const void* buffer() const noexcept = 0;
71
72 /** Returns element size in bytes */
73 virtual size_t elementSize() const noexcept = 0;
74
75 /** Returns element count, total byte size = elementSize() * elementCount() */
76 virtual size_t elementCount() const noexcept = 0;
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
87 /** SyncBuffer interface with a single underlying Matrix4. */
88 template<typename Value_type,
89 std::enable_if_t<std::is_floating_point_v<Value_type>, bool> = true>
90 class SyncMatrix4 : public SyncBuffer {
91 public:
94
95 /** Return the underlying Mat4, used to synchronize via action() to the buffer(). */
96 virtual const Mat4& matrix() const noexcept = 0;
97
98 /** Return the underlying float data buffer. */
99 const value_type* floats() const noexcept { return matrix().cbegin(); }
100
101 const void* buffer() const noexcept override { return floats(); }
102 size_t elementSize() const noexcept override { return sizeof(float); }
103 size_t elementCount() const noexcept override { return 16; }
104 };
106
107
108 /** SyncBuffer interface with multiple underlying Matrix4. */
109 template<typename Value_type,
110 std::enable_if_t<std::is_floating_point_v<Value_type>, bool> = true>
111 class SyncMatrices4 : public SyncBuffer {
112 public:
115
116 /** Return the underlying Mat4 pointer, used to synchronize via action() to the buffer(). */
117 virtual const Mat4* matrices() const noexcept = 0;
118 /** Return the number of Mat4 referenced by matrices() */
119 virtual size_t matrixCount() const noexcept = 0;
120
121 const float* floats() const noexcept { return matrices()[0].cbegin(); }
122 const void* buffer() const noexcept override { return floats(); }
123 size_t elementSize() const noexcept override { return sizeof(float); }
124 size_t elementCount() const noexcept override { return 16 * matrixCount(); }
125 };
127
128 /**@}*/
129
130 } // namespace jau::math::util
131
132 #endif // JAU_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:112
constexpr const_iterator cbegin() const noexcept
Definition: mat4f.hpp:312
Convenient tuple of a sync_action_t and data buffer.
Definition: syncbuffer.hpp:62
virtual sync_action_t & action() noexcept=0
Return the defined sync_action_t.
virtual ~SyncBuffer() noexcept=default
virtual const void * buffer() const noexcept=0
Return the underlying data buffer.
SyncBuffer & sync() noexcept
Synchronizes the underlying data before usage.
Definition: syncbuffer.hpp:84
virtual size_t elementSize() const noexcept=0
Returns element size in bytes.
virtual size_t elementCount() const noexcept=0
Returns element count, total byte size = elementSize() * elementCount()
SyncBuffer interface with multiple underlying Matrix4.
Definition: syncbuffer.hpp:111
size_t elementSize() const noexcept override
Returns element size in bytes.
Definition: syncbuffer.hpp:123
const void * buffer() const noexcept override
Return the underlying data buffer.
Definition: syncbuffer.hpp:122
virtual size_t matrixCount() const noexcept=0
Return the number of Mat4 referenced by matrices()
const float * floats() const noexcept
Definition: syncbuffer.hpp:121
virtual const Mat4 * matrices() const noexcept=0
Return the underlying Mat4 pointer, used to synchronize via action() to the buffer().
size_t elementCount() const noexcept override
Returns element count, total byte size = elementSize() * elementCount()
Definition: syncbuffer.hpp:124
Matrix4< value_type, std::is_floating_point_v< Value_type > > Mat4
Definition: syncbuffer.hpp:114
SyncBuffer interface with a single underlying Matrix4.
Definition: syncbuffer.hpp:90
size_t elementCount() const noexcept override
Returns element count, total byte size = elementSize() * elementCount()
Definition: syncbuffer.hpp:103
virtual const Mat4 & matrix() const noexcept=0
Return the underlying Mat4, used to synchronize via action() to the buffer().
const void * buffer() const noexcept override
Return the underlying data buffer.
Definition: syncbuffer.hpp:101
const value_type * floats() const noexcept
Return the underlying float data buffer.
Definition: syncbuffer.hpp:99
Matrix4< value_type, std::is_floating_point_v< Value_type > > Mat4
Definition: syncbuffer.hpp:93
size_t elementSize() const noexcept override
Returns element size in bytes.
Definition: syncbuffer.hpp:102
SyncMatrices4< float > SyncMats4f
Definition: syncbuffer.hpp:126
jau::function< void()> sync_action_t
Specific data synchronization action implemented by the data provider to update the buffer with the u...
Definition: syncbuffer.hpp:51
void(* sync_action_fptr)()
Plain function pointer type matching sync_action_t.
Definition: syncbuffer.hpp:54
SyncMatrix4< float > SyncMat4f
Definition: syncbuffer.hpp:105
uint8_t Value_type