-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoglBuffer.h
359 lines (317 loc) · 11.8 KB
/
oglBuffer.h
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#pragma once
#include "oglRely.h"
#include "SystemCommon/CopyEx.h"
#if COMMON_COMPILER_MSVC
# pragma warning(push)
# pragma warning(disable:4275 4251)
#endif
namespace oglu
{
class CtxFuncs;
class oglMapPtr;
class oglBuffer_;
using oglBuffer = std::shared_ptr<oglBuffer_>;
class oglPixelBuffer_;
using oglPBO = std::shared_ptr<oglPixelBuffer_>;
class oglArrayBuffer_;
using oglVBO = std::shared_ptr<oglArrayBuffer_>;
class oglTextureBuffer_;
using oglTBO = std::shared_ptr<oglTextureBuffer_>;
class oglUniformBuffer_;
using oglUBO = std::shared_ptr<oglUniformBuffer_>;
class oglElementBuffer_;
using oglEBO = std::shared_ptr<oglElementBuffer_>;
class oglIndirectBuffer_;
using oglIBO = std::shared_ptr<oglIndirectBuffer_>;
enum class BufferTypes : uint32_t
{
Array = 0x8892/*GL_ARRAY_BUFFER*/,
Element = 0x8893/*GL_ELEMENT_ARRAY_BUFFER*/,
Uniform = 0x8A11/*GL_UNIFORM_BUFFER*/,
ShaderStorage = 0x90D2/*GL_SHADER_STORAGE_BUFFER*/,
Pixel = 0x88EC/*GL_PIXEL_UNPACK_BUFFER*/,
Texture = 0x8C2A/*GL_TEXTURE_BUFFER*/,
Indirect = 0x8F3F/*GL_DRAW_INDIRECT_BUFFER*/
};
enum class BufferWriteMode : uint8_t
{
FREQ_MASK = 0x0f, ACCESS_MASK = 0xf0,
FREQ_STREAM = 0x01, FREQ_STATIC = 0x02, FREQ_DYNAMIC = 0x03,
ACCESS_DRAW = 0x10, ACCESS_READ = 0x20, ACCESS_COPY = 0x30,
StreamDraw = FREQ_STREAM | ACCESS_DRAW, StreamRead = FREQ_STREAM | ACCESS_READ, StreamCopy = FREQ_STREAM | ACCESS_COPY,
StaticDraw = FREQ_STATIC | ACCESS_DRAW, StaticRead = FREQ_STATIC | ACCESS_READ, StaticCopy = FREQ_STATIC | ACCESS_COPY,
DynamicDraw = FREQ_DYNAMIC | ACCESS_DRAW, DynamicRead = FREQ_DYNAMIC | ACCESS_READ, DynamicCopy = FREQ_DYNAMIC | ACCESS_COPY,
};
MAKE_ENUM_BITFIELD(BufferWriteMode)
enum class MapFlag : uint16_t
{
MapRead = 0x0001/*GL_MAP_READ_BIT*/, MapWrite = 0x0002/*GL_MAP_WRITE_BIT*/,
PersistentMap = 0x0040/*GL_MAP_PERSISTENT_BIT*/, CoherentMap = 0x0080/*GL_MAP_COHERENT_BIT*/,
DynamicStorage = 0x0100/*GL_DYNAMIC_STORAGE_BIT*/, ClientStorage = 0x0200/*GL_CLIENT_STORAGE_BIT*/,
InvalidateRange = 0x0004/*GL_MAP_INVALIDATE_RANGE_BIT*/, InvalidateAll = 0x0008/*GL_MAP_INVALIDATE_BUFFER_BIT*/,
ExplicitFlush = 0x0010/*GL_MAP_FLUSH_EXPLICIT_BIT*/, UnSynchronize = 0x0020/*GL_MAP_UNSYNCHRONIZED_BIT*/,
PrepareMask = MapRead | MapWrite | PersistentMap | CoherentMap | DynamicStorage | ClientStorage,
RangeMask = MapRead | MapWrite | PersistentMap | CoherentMap | InvalidateRange | InvalidateAll | ExplicitFlush | UnSynchronize
};
MAKE_ENUM_BITFIELD(MapFlag)
class OGLUAPI oglBuffer_ : public common::NonMovable, public std::enable_shared_from_this<oglBuffer_>, public detail::oglCtxObject<true>
{
friend class oglMapPtr;
friend class oglProgram_;
friend class ::oclu::GLInterop;
private:
class OGLUAPI oglMapPtr_ : public common::NonCopyable, public common::NonMovable
{
friend class oglBuffer_;
friend class oglMapPtr;
private:
oglBuffer_& Buffer;
common::span<std::byte> MemSpace;
MapFlag Flag;
public:
MAKE_ENABLER();
oglMapPtr_(oglBuffer_* buf, const MapFlag flags);
~oglMapPtr_();
};
protected:
std::u16string Name;
std::optional<oglMapPtr_> PersistentPtr;
size_t BufSize;
uint32_t BufferID;
const BufferTypes BufferType;
void bind() const noexcept;
void unbind() const noexcept;
oglBuffer_(const BufferTypes type) noexcept;
void PersistentMap(MapFlag flags);
void WriteSpan(const common::span<const std::byte> space, const BufferWriteMode mode = BufferWriteMode::StaticDraw);
template<typename T>
void WriteSpan(const common::span<T> space, const BufferWriteMode mode = BufferWriteMode::StaticDraw)
{
WriteSpan(common::as_bytes(space), mode);
}
template<typename T>
void WriteSpan(const T& cont, const BufferWriteMode mode = BufferWriteMode::StaticDraw)
{
WriteSpan(common::as_bytes(common::to_span(cont)), mode);
}
template<typename T>
void Write(const T& obj, const BufferWriteMode mode = BufferWriteMode::StaticDraw)
{
WriteSpan(common::span<const std::byte>(reinterpret_cast<const std::byte*>(&obj), sizeof(T)), mode);
}
public:
virtual ~oglBuffer_() noexcept;
void SetName(std::u16string name) noexcept;
[[nodiscard]] oglMapPtr Map(const MapFlag flags);
[[nodiscard]] common::span<std::byte> GetPersistentPtr() const;
[[nodiscard]] std::u16string_view GetName() const noexcept { return Name; }
};
class OGLUAPI oglPixelBuffer_ : public oglBuffer_
{
template<typename Base>
friend struct oglTexCommon_;
private:
MAKE_ENABLER();
oglPixelBuffer_() noexcept : oglBuffer_(BufferTypes::Pixel) { }
public:
virtual ~oglPixelBuffer_() noexcept override;
using oglBuffer_::WriteSpan;
//using oglBuffer_::Write;
[[nodiscard]] static oglPBO Create();
};
class OGLUAPI oglArrayBuffer_ : public oglBuffer_
{
friend class oglVAO_;
private:
MAKE_ENABLER();
oglArrayBuffer_() noexcept : oglBuffer_(BufferTypes::Array) { }
public:
virtual ~oglArrayBuffer_() noexcept override;
using oglBuffer_::WriteSpan;
//using oglBuffer_::Write;
[[nodiscard]] static oglVBO Create();
};
class OGLUAPI oglTextureBuffer_ : public oglBuffer_
{
friend class oglBufferTexture_;
private:
MAKE_ENABLER();
oglTextureBuffer_() noexcept;
public:
virtual ~oglTextureBuffer_() noexcept override;
using oglBuffer_::WriteSpan;
//using oglBuffer_::Write;
[[nodiscard]] static oglTBO Create();
};
class OGLUAPI oglUniformBuffer_ : public oglBuffer_
{
friend class detail::CachedResManager<oglUniformBuffer_>;
friend class oglProgram_;
friend class ProgDraw;
private:
MAKE_ENABLER();
oglUniformBuffer_(const size_t size) noexcept;
protected:
[[nodiscard]] static detail::ResourceBinder<oglUniformBuffer_>& GetUBOMan() noexcept;
void BindToUnit(const uint16_t pos) const;
public:
virtual ~oglUniformBuffer_() noexcept override;
[[nodiscard]] size_t Size() const { return BufSize; };
using oglBuffer_::WriteSpan;
//using oglBuffer_::Write;
[[nodiscard]] static oglUBO Create(const size_t size);
};
class OGLUAPI oglIndirectBuffer_ : public oglBuffer_
{
friend class oglVAO_;
friend class CtxFuncs;
private:
MAKE_ENABLER();
oglIndirectBuffer_() noexcept;
public:
struct DrawElementsIndirectCommand
{
uint32_t count;
uint32_t instanceCount;
uint32_t firstIndex;
uint32_t baseVertex;
uint32_t baseInstance;
};
struct DrawArraysIndirectCommand
{
uint32_t count;
uint32_t instanceCount;
uint32_t first;
uint32_t baseInstance;
};
protected:
std::variant<std::vector<DrawElementsIndirectCommand>, std::vector<DrawArraysIndirectCommand>> Commands;
int32_t Count = 0;
[[nodiscard]] bool IsIndexed() const;
public:
virtual ~oglIndirectBuffer_() noexcept override;
///<summary>Write indirect draw commands</summary>
///<param name="offsets">offsets</param>
///<param name="sizes">sizes</param>
///<param name="isIndexed">Indexed commands or not</param>
void WriteCommands(const std::vector<uint32_t>& offsets, const std::vector<uint32_t>& sizes, const bool isIndexed);
///<summary>Write indirect draw commands</summary>
///<param name="offset">offset</param>
///<param name="size">size</param>
///<param name="isIndexed">Indexed commands or not</param>
void WriteCommands(const uint32_t offset, const uint32_t size, const bool isIndexed);
[[nodiscard]] const std::vector<DrawElementsIndirectCommand>& GetElementCommands() const
{
return std::get<std::vector<DrawElementsIndirectCommand>>(Commands);
}
[[nodiscard]] const std::vector<DrawArraysIndirectCommand>& GetArrayCommands() const
{
return std::get<std::vector<DrawArraysIndirectCommand>>(Commands);
}
[[nodiscard]] static oglIBO Create();
};
class OGLUAPI oglElementBuffer_ : public oglBuffer_
{
friend class oglVAO_;
private:
MAKE_ENABLER();
oglElementBuffer_() noexcept;
protected:
uint32_t IndexType;
uint8_t IndexSize;
void SetSize(const uint8_t elesize);
public:
virtual ~oglElementBuffer_() noexcept override;
///<summary>Write index</summary>
///<param name="cont">index container</param>
///<param name="mode">buffer write mode</param>
template<typename T>
void Write(const T& cont, const BufferWriteMode mode = BufferWriteMode::StaticDraw)
{
const auto space = common::to_span(cont);
using EleType = typename decltype(space)::value_type;
static_assert(std::is_integral_v<EleType> && std::is_unsigned_v<EleType> && sizeof(EleType) <= 4,
"input type should be unsigned integeral type and no more than uint32_t");
SetSize(sizeof(EleType));
oglBuffer_::WriteSpan(space, mode);
}
///<summary>Compact and write index</summary>
///<param name="cont">index container</param>
///<param name="mode">buffer write mode</param>
template<typename T>
void WriteCompact(const T& cont, const BufferWriteMode mode = BufferWriteMode::StaticDraw)
{
const auto space = common::to_span(cont);
using EleType = typename decltype(space)::value_type;
static_assert(std::is_integral_v<EleType> && std::is_unsigned_v<EleType>,
"input type should be unsigned integeral type");
const auto ptr = space.data();
const auto count = space.size();
auto [minptr, maxptr] = std::minmax_element(ptr, ptr + count);
if (*minptr < 0)
COMMON_THROW(common::BaseException, u"element buffer cannot appear negatve value");
auto maxval = *maxptr;
if (maxval <= UINT8_MAX)
{
if constexpr (sizeof(T) == 1)
Write(space, mode);
else
{
common::AlignedBuffer newdata(count * 1);
common::CopyEx.TruncCopy(newdata.GetRawPtr<uint8_t>(), ptr, count);
SetSize(1);
oglBuffer_::WriteSpan(newdata, mode);
}
}
else if (maxval <= UINT16_MAX)
{
if constexpr(sizeof(T) == 2)
Write(space, mode);
else
{
common::AlignedBuffer newdata(count * 2);
common::CopyEx.TruncCopy(newdata.GetRawPtr<uint16_t>(), ptr, count);
SetSize(2);
oglBuffer_::WriteSpan(newdata, mode);
}
}
else if (maxval <= UINT32_MAX)
{
if constexpr(sizeof(T) == 4)
Write(space, mode);
else
{
std::vector<uint32_t> newdata;
newdata.reserve(count);
const auto *cur = ptr, *end = ptr + count;
while (cur != end)
newdata.push_back(static_cast<uint32_t>(*cur++));
SetSize(4);
oglBuffer_::WriteSpan(newdata, mode);
}
}
else
COMMON_THROW(common::BaseException, u"input should be no more than uint32_t");
}
[[nodiscard]] static oglEBO Create();
};
class OGLUAPI oglMapPtr
{
friend class oglBuffer_;
private:
oglBuffer Buf;
std::shared_ptr<const oglBuffer_::oglMapPtr_> Ptr;
oglMapPtr(oglBuffer&& buf, std::shared_ptr<const oglBuffer_::oglMapPtr_> ptr) : Buf(std::move(buf)), Ptr(std::move(ptr)) { }
public:
constexpr oglMapPtr() {}
[[nodiscard]] common::span<std::byte> Get() const noexcept;
template<typename T>
[[nodiscard]] common::span<T> AsType() const noexcept
{
return common::span<T>(reinterpret_cast<T*>(Get().data()), Get().size() / sizeof(T));
}
};
}
#if COMMON_COMPILER_MSVC
# pragma warning(pop)
#endif