-
Notifications
You must be signed in to change notification settings - Fork 933
/
ubo.hpp
160 lines (136 loc) · 4.36 KB
/
ubo.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#ifndef COMPONENTS_STD140_UBO_H
#define COMPONENTS_STD140_UBO_H
#include <osg/Matrixf>
#include <osg/Vec2f>
#include <osg/Vec4f>
#include <array>
#include <cstdint>
#include <cstring>
#include <string>
#include <string_view>
#include <tuple>
namespace std140
{
struct Mat4
{
using Value = osg::Matrixf;
Value mValue;
static constexpr size_t sAlign = sizeof(Value);
static constexpr std::string_view sTypeName = "mat4";
};
struct Vec4
{
using Value = osg::Vec4f;
Value mValue;
static constexpr size_t sAlign = sizeof(Value);
static constexpr std::string_view sTypeName = "vec4";
};
struct Vec3
{
using Value = osg::Vec3f;
Value mValue;
static constexpr std::size_t sAlign = 4 * sizeof(osg::Vec3f::value_type);
static constexpr std::string_view sTypeName = "vec3";
};
struct Vec2
{
using Value = osg::Vec2f;
Value mValue;
static constexpr std::size_t sAlign = sizeof(Value);
static constexpr std::string_view sTypeName = "vec2";
};
struct Float
{
using Value = float;
Value mValue;
static constexpr std::size_t sAlign = sizeof(Value);
static constexpr std::string_view sTypeName = "float";
};
struct Int
{
using Value = std::int32_t;
Value mValue;
static constexpr std::size_t sAlign = sizeof(Value);
static constexpr std::string_view sTypeName = "int";
};
struct UInt
{
using Value = std::uint32_t;
Value mValue;
static constexpr std::size_t sAlign = sizeof(Value);
static constexpr std::string_view sTypeName = "uint";
};
struct Bool
{
using Value = std::int32_t;
Value mValue;
static constexpr std::size_t sAlign = sizeof(Value);
static constexpr std::string_view sTypeName = "bool";
};
template <class... CArgs>
class UBO
{
private:
template <typename T, typename... Args>
struct contains : std::bool_constant<(std::is_base_of_v<Args, T> || ...)>
{
};
static_assert((contains<CArgs, Mat4, Vec4, Vec3, Vec2, Float, Int, UInt, Bool>() && ...));
static constexpr size_t roundUpRemainder(size_t x, size_t multiple)
{
size_t remainder = x % multiple;
if (remainder == 0)
return 0;
return multiple - remainder;
}
template <class T>
static constexpr std::size_t getOffset()
{
bool found = false;
std::size_t size = 0;
((found = found || std::is_same_v<T, CArgs>,
size += (found ? 0 : sizeof(typename CArgs::Value) + roundUpRemainder(size, CArgs::sAlign))),
...);
return size + roundUpRemainder(size, T::sAlign);
}
public:
static constexpr size_t getGPUSize()
{
std::size_t size = 0;
((size += (sizeof(typename CArgs::Value) + roundUpRemainder(size, CArgs::sAlign))), ...);
return size;
}
static std::string getDefinition(const std::string& name)
{
std::string structDefinition = "struct " + name + " {\n";
((structDefinition += (" " + std::string(CArgs::sTypeName) + " " + std::string(CArgs::sName) + ";\n")),
...);
return structDefinition + "};";
}
using BufferType = std::array<char, getGPUSize()>;
using TupleType = std::tuple<CArgs...>;
template <class T>
typename T::Value& get()
{
return std::get<T>(mData).mValue;
}
template <class T>
const typename T::Value& get() const
{
return std::get<T>(mData).mValue;
}
void copyTo(BufferType& buffer) const
{
const auto copy = [&](const auto& v) {
static_assert(std::is_standard_layout_v<std::decay_t<decltype(v.mValue)>>);
constexpr std::size_t offset = getOffset<std::decay_t<decltype(v)>>();
std::memcpy(buffer.data() + offset, &v.mValue, sizeof(v.mValue));
};
std::apply([&](const auto&... v) { (copy(v), ...); }, mData);
}
const auto& getData() const { return mData; }
private:
std::tuple<CArgs...> mData;
};
}
#endif