-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoglVAO.cpp
328 lines (300 loc) · 11 KB
/
oglVAO.cpp
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
#include "oglPch.h"
#include "oglVAO.h"
#include "oglContext.h"
#include "oglProgram.h"
namespace oglu
{
using std::string;
using std::u16string;
using std::vector;
MAKE_ENABLER_IMPL(oglVAO_)
oglVAO_::VAOPrep::VAOPrep(oglVAO_* vao, const oglDrawProgram_& prog) noexcept :
VAO(vao), Prog(prog)
{
VAO->bind();
}
oglVAO_::VAOPrep::~VAOPrep()
{
if (VAO)
{
VAO->CheckCurrent();
oglVAO_::unbind();
}
}
GLint oglVAO_::VAOPrep::GetLoc(const std::string_view name) const noexcept
{
return Prog.InputRess.GetLocation(name);
}
void oglVAO_::VAOPrep::GetLocs(const common::span<const std::string_view> names, const common::span<GLint> idxs) const noexcept
{
size_t i = 0;
for (const auto& name : names)
idxs[i++] = Prog.InputRess.GetLocation(name);
}
static constexpr GLenum ParseVAValType(const VAValType type)
{
switch (type)
{
case VAValType::Double: return GL_DOUBLE;
case VAValType::Float: return GL_FLOAT;
case VAValType::Half: return GL_HALF_FLOAT;
case VAValType::U32: return GL_UNSIGNED_INT;
case VAValType::I32: return GL_INT;
case VAValType::U16: return GL_UNSIGNED_SHORT;
case VAValType::I16: return GL_SHORT;
case VAValType::U8: return GL_UNSIGNED_BYTE;
case VAValType::I8: return GL_BYTE;
case VAValType::U10_2: return GL_UNSIGNED_INT_2_10_10_10_REV;
case VAValType::I10_2: return GL_INT_2_10_10_10_REV;
case VAValType::UF11_10: return GL_UNSIGNED_INT_10F_11F_11F_REV;
default: return GL_INVALID_ENUM;
}
}
void oglVAO_::VAOPrep::SetInteger(const VAValType valType, const GLint attridx, const uint16_t stride, const uint8_t size, const size_t offset, GLuint divisor)
{
if (attridx != GLInvalidIndex)
{
CtxFunc->EnableVertexArrayAttrib(VAO->VAOId, attridx);//vertex attr index
CtxFunc->VertexAttribIPointer(attridx, size, ParseVAValType(valType), stride, offset);
if (divisor != 0)
CtxFunc->VertexAttribDivisor(attridx, divisor);
}
}
void oglVAO_::VAOPrep::SetFloat(const VAValType valType, const bool isNormalize, const GLint attridx, const uint16_t stride, const uint8_t size, const size_t offset, GLuint divisor)
{
if (attridx != GLInvalidIndex)
{
CtxFunc->EnableVertexArrayAttrib(VAO->VAOId, attridx);//vertex attr index
CtxFunc->VertexAttribPointer(attridx, size, ParseVAValType(valType), isNormalize, stride, offset);
if (divisor != 0)
CtxFunc->VertexAttribDivisor(attridx, divisor);
}
}
oglVAO_::VAOPrep& oglVAO_::VAOPrep::SetIndex(const oglEBO& ebo)
{
VAO->CheckCurrent();
ebo->bind();
VAO->IndexBuffer = ebo;
return *this;
}
oglVAO_::VAOPrep& oglVAO_::VAOPrep::SetDrawSize(const uint32_t offset, const uint32_t size)
{
VAO->CheckCurrent();
VAO->Count = (GLsizei)size;
if (VAO->IndexBuffer)
{
VAO->Method = DrawMethod::Index;
VAO->Offsets = (void*)uintptr_t(offset * VAO->IndexBuffer->IndexSize);
}
else
{
VAO->Method = DrawMethod::Array;
VAO->Offsets = (GLint)offset;
}
return *this;
}
oglVAO_::VAOPrep& oglVAO_::VAOPrep::SetDrawSizes(const vector<uint32_t>& offsets, const vector<uint32_t>& sizes)
{
VAO->CheckCurrent();
const auto count = offsets.size();
if (count != sizes.size())
COMMON_THROWEX(OGLException, OGLException::GLComponent::OGLU, u"offset and size should be of the same size.");
VAO->Count.emplace<vector<GLsizei>>(sizes.cbegin(), sizes.cend());
if (VAO->IndexBuffer)
{
VAO->Method = DrawMethod::Indexs;
const uint32_t idxSize = VAO->IndexBuffer->IndexSize;
VAO->Offsets = common::linq::FromIterable(offsets)
.Select([=](const uint32_t off) { return reinterpret_cast<const void*>(uintptr_t(off * idxSize)); })
.ToVector();
}
else
{
VAO->Method = DrawMethod::Arrays;
VAO->Offsets = common::linq::FromIterable(offsets)
.Select([](const uint32_t off) { return static_cast<GLint>(off); })
.ToVector();
}
return *this;
}
oglVAO_::VAOPrep& oglVAO_::VAOPrep::SetDrawSizeFrom(const oglIBO& ibo, GLint offset, GLsizei size)
{
if ((bool)VAO->IndexBuffer != ibo->IsIndexed())
COMMON_THROWEX(OGLException, OGLException::GLComponent::OGLU, u"Unmatched ebo state and ibo's target.");
if (offset > ibo->Count || offset < 0)
COMMON_THROWEX(OGLException, OGLException::GLComponent::OGLU, u"offset exceed ebo size.");
if (size == 0)
size = ibo->Count - offset;
else if (size + offset > ibo->Count)
COMMON_THROWEX(OGLException, OGLException::GLComponent::OGLU, u"draw size exceed ebo size.");
VAO->IndirectBuffer = ibo;
VAO->Method = ibo->IsIndexed() ? DrawMethod::IndirectIndexes : DrawMethod::IndirectArrays;
VAO->Count = size;
VAO->Offsets = offset;
return *this;
}
struct DRAWIDCtxConfig : public CtxResConfig<true, oglVBO>
{
oglVBO Construct() const
{
std::vector<uint32_t> ids(4096);
for (uint32_t i = 0; i < 4096; ++i)
ids[i] = i;
auto drawIdVBO = oglArrayBuffer_::Create();
drawIdVBO->SetName(u"ogluDrawID");
drawIdVBO->WriteSpan(ids);
oglLog().Success(u"new DrawIdVBO generated.\n");
return drawIdVBO;
}
};
static DRAWIDCtxConfig DRAWID_CTX_CFG;
oglVAO_::VAOPrep& oglVAO_::VAOPrep::SetDrawId(const GLint attridx)
{
VAO->CheckCurrent();
return SetInteger<int32_t>(oglContext_::CurrentContext()->GetOrCreate<true>(DRAWID_CTX_CFG), attridx, sizeof(int32_t), 1, 0, 1);
}
oglVAO_::VAOPrep& oglVAO_::VAOPrep::SetDrawId()
{
return SetDrawId(Prog.InputRess.GetLocation("@DrawID"));
}
void oglVAO_::bind() const noexcept
{
CheckCurrent();
CtxFunc->BindVertexArray(VAOId);
}
void oglVAO_::unbind() noexcept
{
CtxFunc->BindVertexArray(0);
}
oglVAO_::oglVAO_(const VAODrawMode mode) noexcept : VAOId(GL_INVALID_INDEX), DrawMode(mode)
{
CtxFunc->GenVertexArrays(1, &VAOId);
}
oglVAO_::~oglVAO_() noexcept
{
if (!EnsureValid()) return;
CtxFunc->DeleteVertexArrays(1, &VAOId);
}
oglVAO_::VAOPrep oglVAO_::Prepare(const std::shared_ptr<oglDrawProgram_>& prog) noexcept
{
CheckCurrent();
return VAOPrep(this, *prog);
}
void oglVAO_::SetName(std::u16string name) noexcept
{
Name = std::move(name);
CtxFunc->SetObjectLabel(GL_VERTEX_ARRAY, VAOId, Name);
}
void oglVAO_::RangeDraw(const uint32_t size, const uint32_t offset) const noexcept
{
CheckCurrent();
bind();
switch (Method)
{
case DrawMethod::Array:
CtxFunc->DrawArrays(common::enum_cast(DrawMode), offset, size);
break;
case DrawMethod::Index:
CtxFunc->DrawElements(common::enum_cast(DrawMode), size, IndexBuffer->IndexType, (const void*)intptr_t(offset * IndexBuffer->IndexSize));
break;
default:
oglLog().Error(u"Only array/index mode support ranged draw, this vao[{}] has mode [{}].\n", VAOId, (uint8_t)Method);
}
unbind();
}
void oglVAO_::Draw() const noexcept
{
CheckCurrent();
bind();
switch (Method)
{
case DrawMethod::Array:
CtxFunc->DrawArrays(common::enum_cast(DrawMode), std::get<GLint>(Offsets), std::get<GLsizei>(Count));
break;
case DrawMethod::Index:
CtxFunc->DrawElements(common::enum_cast(DrawMode), std::get<GLsizei>(Count), IndexBuffer->IndexType, std::get<const void*>(Offsets));
break;
case DrawMethod::Arrays:
{
const auto& sizes = std::get<vector<GLsizei>>(Count);
CtxFunc->MultiDrawArrays(common::enum_cast(DrawMode), std::get<vector<GLint>>(Offsets).data(), sizes.data(), (GLsizei)sizes.size());
} break;
case DrawMethod::Indexs:
{
const auto& sizes = std::get<vector<GLsizei>>(Count);
CtxFunc->MultiDrawElements(common::enum_cast(DrawMode), sizes.data(), IndexBuffer->IndexType, std::get<vector<const void*>>(Offsets).data(), (GLsizei)sizes.size());
//const auto& offsets = std::get<vector<const void*>>(Offsets).data();
//for (size_t i = 0; i < sizes.size(); ++i)
//{
// //glDrawElementsBaseVertex(common::enum_cast(DrawMode), sizes[i], IndexBuffer->IndexType, (void*)offsets[i], 0);
// //glDrawElementsInstancedBaseInstance(common::enum_cast(DrawMode), sizes[i], IndexBuffer->IndexType, (void*)offsets[i], 1, 0);
// glDrawElementsInstancedBaseVertexBaseInstance(common::enum_cast(DrawMode), sizes[i], IndexBuffer->IndexType, (void*)offsets[i], 1, 0, 0);
//}
} break;
case DrawMethod::IndirectArrays:
CtxFunc->MultiDrawArraysIndirect(common::enum_cast(DrawMode), *IndirectBuffer, std::get<GLint>(Offsets), std::get<GLsizei>(Count));
break;
case DrawMethod::IndirectIndexes:
CtxFunc->MultiDrawElementsIndirect(common::enum_cast(DrawMode), IndexBuffer->IndexType, *IndirectBuffer, std::get<GLint>(Offsets), std::get<GLsizei>(Count));
break;
case DrawMethod::UnPrepared:
oglLog().Error(u"drawing an unprepared VAO [{}]\n", VAOId);
break;
}
unbind();
}
void oglVAO_::InstanceDraw(const uint32_t count, const uint32_t base) const noexcept
{
CheckCurrent();
bind();
switch (Method)
{
case DrawMethod::Array:
CtxFunc->DrawArraysInstanced(common::enum_cast(DrawMode), std::get<GLint>(Offsets), std::get<GLsizei>(Count), count, base);
break;
case DrawMethod::Index:
CtxFunc->DrawElementsInstanced(common::enum_cast(DrawMode), std::get<GLsizei>(Count), IndexBuffer->IndexType, std::get<const void*>(Offsets), count, base);
break;
case DrawMethod::Arrays:
{
const auto& sizes = std::get<vector<GLsizei>>(Count);
const auto& offsets = std::get<vector<GLint>>(Offsets);
for (size_t i = 0; i < sizes.size(); i++)
{
CtxFunc->DrawArraysInstanced(common::enum_cast(DrawMode),
offsets[i], sizes[i], count, base);
}
} break;
case DrawMethod::Indexs:
{
const auto& sizes = std::get<vector<GLsizei>>(Count);
const auto& offsets = std::get<vector<const void*>>(Offsets);
for (size_t i = 0; i < sizes.size(); i++)
{
CtxFunc->DrawElementsInstanced(common::enum_cast(DrawMode),
sizes[i], IndexBuffer->IndexType, offsets[i], count, base);
}
} break;
case DrawMethod::IndirectArrays:
case DrawMethod::IndirectIndexes:
oglLog().Error(u"indirect draw not support when manually instance draw, this vao[{}] has mode [{}].\n", VAOId, (uint8_t)Method);
break;
case DrawMethod::UnPrepared:
oglLog().Error(u"drawing an unprepared VAO [{}]\n", VAOId);
break;
}
unbind();
}
void oglVAO_::Test() const noexcept
{
CheckCurrent();
bind();
BindingState state;
unbind();
oglLog().Debug(u"Current VAO[{}]'s binding: VAO[{}], VBO[{}], IBO[{}], EBO[{}]\n", VAOId, state.VAO, state.VBO, state.IBO, state.EBO);
}
oglVAO oglVAO_::Create(const VAODrawMode mode)
{
return MAKE_ENABLER_SHARED(oglVAO_, (mode));
}
}