-
Notifications
You must be signed in to change notification settings - Fork 0
/
vgl_render_queue.hpp
624 lines (559 loc) · 18.4 KB
/
vgl_render_queue.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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
#ifndef VGL_RENDER_QUEUE_HPP
#define VGL_RENDER_QUEUE_HPP
#include "vgl_font.hpp"
#include "vgl_frame_buffer.hpp"
#include "vgl_mesh.hpp"
#include "vgl_packed_data_reader.hpp"
namespace vgl
{
/// Render command queue contains precalculated rendering commands to send meshes to GPU.
///
/// Only objects with no constructors may be added to the rendering queue.
class RenderQueue
{
private:
/// Internal state for render operation.
class RenderState
{
private:
/// Render command function type.
using RenderCommandFunc = void (*)(RenderState&, PackedDataReader&);
private:
/// Current projection matrix.
const mat4* VGL_VOLUNTARY_MEMBER_VALUE(m_projection_matrix, nullptr);
/// Current camera depth range.
const vec2* VGL_VOLUNTARY_MEMBER_VALUE(m_projection_range, nullptr);
/// Current camera matrix.
const mat4* VGL_VOLUNTARY_MEMBER_VALUE(m_camera_matrix, nullptr);
/// Current projection camera matrix.
const mat4* VGL_VOLUNTARY_MEMBER_VALUE(m_projection_camera_matrix, nullptr);
/// Current modelview matrix.
const mat4* VGL_VOLUNTARY_MEMBER_VALUE(m_modelview_matrix, nullptr);
/// Current normal matrix.
const mat3* VGL_VOLUNTARY_MEMBER_VALUE(m_normal_matrix, nullptr);
/// Current camera modelview matrix.
const mat4* VGL_VOLUNTARY_MEMBER_VALUE(m_camera_modelview_matrix, nullptr);
/// Current projection camera modelview matrix.
const mat4* VGL_VOLUNTARY_MEMBER_VALUE(m_projection_camera_modelview_matrix, nullptr);
/// Current camera position.
const vec3* VGL_VOLUNTARY_MEMBER_VALUE(m_camera_position, nullptr);
/// Current program.
GlslProgram* VGL_VOLUNTARY_MEMBER_VALUE(m_program, nullptr);
/// Current mesh.
Mesh* m_mesh = nullptr;
/// Current texture unit.
unsigned m_texture_unit = 0;
/// Current persistent texture unit.
unsigned m_texture_unit_persistent = 0;
public:
/// Constructor.
explicit RenderState() = default;
private:
/// Apply a mesh.
///
/// Done when encountering a new render value type
/// Clears the mesh state.
void applyMesh()
{
if(!m_mesh)
{
return;
}
#if defined(USE_LD)
if(!m_program)
{
VGL_THROW_RUNTIME_ERROR("cannot apply mesh before a program has been set");
}
if(!m_projection_matrix)
{
VGL_THROW_RUNTIME_ERROR("cannot apply mesh before view settings have been read");
}
VGL_ASSERT(m_projection_range);
VGL_ASSERT(m_camera_matrix);
VGL_ASSERT(m_camera_position);
#endif
// Apply projection uniforms based on semantic only.
m_program->uniform(UniformSemantic::PROJECTION_MATRIX, *m_projection_matrix);
m_program->uniform(UniformSemantic::PROJECTION_RANGE, *m_projection_range);
m_program->uniform(UniformSemantic::CAMERA_MATRIX, *m_camera_matrix);
m_program->uniform(UniformSemantic::PROJECTION_CAMERA_MATRIX, *m_projection_camera_matrix);
m_program->uniform(UniformSemantic::MODELVIEW_MATRIX, *m_modelview_matrix);
m_program->uniform(UniformSemantic::NORMAL_MATRIX, *m_normal_matrix);
m_program->uniform(UniformSemantic::CAMERA_MODELVIEW_MATRIX, *m_camera_modelview_matrix);
m_program->uniform(UniformSemantic::PROJECTION_CAMERA_MODELVIEW_MATRIX, *m_projection_camera_modelview_matrix);
m_program->uniform(UniformSemantic::CAMERA_POSITION, *m_camera_position);
m_mesh->draw(*m_program);
m_mesh = nullptr;
m_texture_unit = 0;
}
/// Get uniform location from packed data reader.
///
/// \param iter Read iterator.
/// \return Uniform location.
GLint getUniformLocation(PackedDataReader& iter)
{
#if defined(USE_LD)
if(!m_program)
{
VGL_THROW_RUNTIME_ERROR("cannot get uniform locations before a program has been read");
}
#endif
string_view name = iter.read<string_view>();
UniformSemantic semantic = iter.read<UniformSemantic>();
if(semantic != UniformSemantic::NONE)
{
return m_program->getUniformLocation(semantic);
}
return m_program->getUniformLocation(name);
}
public:
/// Blend mode command.
///
/// \param iter Packed data iterator.
void commandBlend(PackedDataReader& iter)
{
OperationMode mode = static_cast<OperationMode>(iter.read<int>());
blend_mode(mode);
}
/// Clear command.
///
/// \param iter Packed data iterator.
void commandClear(PackedDataReader& iter)
{
optional<uvec4> color;
if(iter.read<int>())
{
color = iter.read<uvec4>();
}
optional<float> depth;
if(iter.read<int>())
{
depth = iter.read<float>();
}
#if !defined(VGL_DISABLE_STENCIL)
optional<uint8_t> stencil;
if(iter.read<int>())
{
stencil = static_cast<uint8_t>(iter.read<int>());
}
#endif
clear_buffers(color, depth
#if !defined(VGL_DISABLE_STENCIL)
, stencil
#endif
);
}
/// Cull face command.
///
/// \param iter Packed data iterator.
void commandCullFace(PackedDataReader& iter)
{
GLenum mode = static_cast<GLenum>(iter.read<int>());
cull_face(mode);
}
/// Depth test command.
///
/// \param iter Packed data iterator.
void commandDepthTest(PackedDataReader& iter)
{
GLenum mode = static_cast<GLenum>(iter.read<int>());
bool write = static_cast<bool>(iter.read<int>());
depth_test(mode);
depth_write(write);
}
/// FBO command.
///
/// \param iter Packed data iterator.
void commandFbo(PackedDataReader& iter)
{
applyMesh();
FrameBuffer* fbo = iter.read<FrameBuffer*>();
fbo->bind();
}
/// Mesh command.
///
/// \param iter Packed data iterator.
void commandMesh(PackedDataReader& iter)
{
applyMesh();
m_mesh = iter.read<Mesh*>();
m_modelview_matrix = &(iter.read<mat4>());
m_normal_matrix = &(iter.read<mat3>());
m_camera_modelview_matrix = &(iter.read<mat4>());
m_projection_camera_modelview_matrix = &(iter.read<mat4>());
}
/// Program command.
///
/// \param iter Packed data iterator.
void commandProgram(PackedDataReader& iter)
{
applyMesh();
m_program = iter.read<GlslProgram*>();
m_program->bind();
m_texture_unit_persistent = 0;
}
/// Uniform (texture) command.
///
/// \param iter Packed data iterator.
void commandUniformTexture(PackedDataReader& iter)
{
GLint location = getUniformLocation(iter);
Texture* tex = iter.read<Texture*>();
GlslProgram::applyUniform(location, *tex, m_texture_unit_persistent + m_texture_unit);
++m_texture_unit;
}
/// Uniform (texture, persistent) command.
///
/// \param iter Packed data iterator.
void commandUniformTexturePersistent(PackedDataReader& iter)
{
GLint location = getUniformLocation(iter);
Texture* tex = iter.read<Texture*>();
GlslProgram::applyUniform(location, *tex, m_texture_unit_persistent);
++m_texture_unit_persistent;
}
/// View command.
///
/// \param iter Packed data iterator.
void commandView(PackedDataReader& iter)
{
applyMesh();
m_projection_matrix = &(iter.read<mat4>());
m_projection_range = &(iter.read<vec2>());
m_camera_matrix = &(iter.read<mat4>());
m_projection_camera_matrix = &(iter.read<mat4>());
m_camera_position = &(iter.read<vec3>());
}
/// Render using the packed data reader.
///
/// \param op Render queue to render.
void draw(const RenderQueue& op)
{
/// Packed data reader.
PackedDataReader iter(op.m_data);
for(;;)
{
// Abort if no data remaining.
if(iter.remaining() < sizeof(void*))
{
break;
}
// Take an action based on value type extracted.
RenderCommandFunc fptr = reinterpret_cast<RenderCommandFunc>(iter.read<void*>());
fptr(*this, iter);
}
// Apply last mesh that may be remaining.
applyMesh();
}
public:
/// Uniform command.
///
/// \param iter Read iterator.
template<typename T> void commandUniform(PackedDataReader& iter)
{
GLint location = getUniformLocation(iter);
unsigned count = static_cast<unsigned>(iter.read<int>());
GlslProgram::applyUniform(location, &(iter.read<T>(count)), count);
}
};
private:
/// Rendering data.
PackedData m_data;
/// Current camera settings.
#if defined(USE_LD)
optional<mat4> m_camera_matrix;
#else
mat4 m_camera_matrix;
#endif
/// Current projection + camera settings.
#if defined(USE_LD)
optional<mat4> m_projection_camera_matrix;
#else
mat4 m_projection_camera_matrix;
#endif
/// Current camera position.
#if defined(USE_LD)
optional<vec3> m_camera_position;
#else
vec3 m_camera_position;
#endif
public:
/// Constructor.
constexpr explicit RenderQueue() noexcept = default;
private:
/// Push texture uniform.
///
/// \param name Name of the uniform.
/// \param value Texture value.
void pushUniformTexture(string_view name, UniformSemantic semantic, const Texture2D& value)
{
pushCommand<&RenderState::commandUniformTexture>();
m_data.push(name);
m_data.push(static_cast<int>(semantic));
m_data.push(&value);
}
/// Push persistent texture uniform.
///
/// \param name Name of the uniform.
/// \param value Texture value.
void pushUniformTexturePersistent(string_view name, UniformSemantic semantic, const Texture2D& value)
{
pushCommand<&RenderState::commandUniformTexturePersistent>();
m_data.push(name);
m_data.push(static_cast<int>(semantic));
m_data.push(&value);
}
private:
/// Push uniform.
///
/// \param name Name of the uniform.
/// \param semantic Uniform semantic.
/// \param ptr Pointer to uniform value array.
/// \param count Number of uniforms to push.
template<typename T> void pushUniform(string_view name, UniformSemantic semantic, const T* ptr, unsigned count)
{
pushCommand<&RenderState::commandUniform<T>>();
m_data.push(name);
m_data.push(static_cast<int>(semantic));
m_data.push(static_cast<int>(count));
for(unsigned ii = 0; (ii < count); ++ii)
{
m_data.push(ptr[ii]);
}
}
public:
/// Clear the render queue.
///
/// After clearing, the queue is ready to receive settings for a new frame.
void clear()
{
m_data.clear();
#if defined(USE_LD)
m_camera_matrix = nullopt;
m_projection_camera_matrix = nullopt;
m_camera_position = nullopt;
#endif
}
/// Render the contents in the queue.
void draw() const
{
RenderState state;
state.draw(*this);
}
/// Push fbo switch.
///
/// \param op Framebuffer ptr.
void push(const FrameBuffer& op)
{
pushCommand<&RenderState::commandFbo>();
m_data.push(&op);
}
/// Push mesh render.
///
/// \param msh Mesh to render.
/// \param modelview Mesh modelview matrix.
void push(const Mesh& msh, const mat4& modelview)
{
pushCommand<&RenderState::commandMesh>();
m_data.push(&msh);
m_data.push(modelview);
m_data.push(normalify(modelview));
#if defined(USE_LD)
m_data.push((*m_camera_matrix) * modelview);
m_data.push((*m_projection_camera_matrix) * modelview);
#else
m_data.push(m_camera_matrix * modelview);
m_data.push(m_projection_camera_matrix * modelview);
#endif
}
/// Push program switch.
///
/// \param op Program to use starting from this point.
void push(const GlslProgram& op)
{
pushCommand<&RenderState::commandProgram>();
m_data.push(&op);
}
/// Push view settings switch.
///
/// \param proj Projection matrix.
/// \param range Projection depth range.
/// \param cam Camera matrix.
void push(const mat4& proj, const vec2 range, const mat4& cam)
{
mat4 camera = viewify(cam);
m_camera_matrix = camera;
#if defined(USE_LD)
m_projection_camera_matrix = proj * (*m_camera_matrix);
#else
m_projection_camera_matrix = proj * m_camera_matrix;
#endif
m_camera_position = cam.getTranslation();
pushCommand<&RenderState::commandView>();
m_data.push(proj);
m_data.push(range);
#if defined(USE_LD)
m_data.push(*m_camera_matrix);
m_data.push(*m_projection_camera_matrix);
m_data.push(*m_camera_position);
#else
m_data.push(m_camera_matrix);
m_data.push(m_projection_camera_matrix);
m_data.push(m_camera_position);
#endif
}
/// Push blend mode switch.
///
/// \param op Blend mode.
void pushBlend(OperationMode op)
{
pushCommand<&RenderState::commandBlend>();
m_data.push(static_cast<int>(op));
}
/// Push clear.
///
/// \param color Optional clear color.
/// \param depth Optional clear depth.
/// \param stencil Optional clear stencil.
void pushClear(optional<uvec4> color, optional<float> depth
#if !defined(VGL_DISABLE_STENCIL)
, optional<uint8_t> stencil = nullopt
#endif
)
{
pushCommand<&RenderState::commandClear>();
{
int color_enable = static_cast<int>(static_cast<bool>(color));
if(color_enable)
{
m_data.push(static_cast<int>(1));
m_data.push(*color);
}
}
{
int depth_enable = static_cast<int>(static_cast<bool>(depth));
m_data.push(depth_enable);
if(depth_enable)
{
m_data.push(*depth);
}
}
#if !defined(VGL_DISABLE_STENCIL)
{
int stencil_enable = static_cast<int>(static_cast<bool>(stencil));
m_data.push(stencil_enable);
if(stencil)
{
m_data.push(static_cast<int>(*stencil));
}
}
#endif
}
/// Push cull mode switch.
///
/// \param op Cull mode.
void pushCull(GLenum op)
{
pushCommand<&RenderState::commandCullFace>();
m_data.push(static_cast<int>(op));
}
/// Push depth test switch.
///
/// \param mode Depth test mode.
/// \param write Depth write enabled.
void pushDepth(GLenum mode
#if !defined(VGL_DISABLE_DEPTH_WRITE)
, bool write = true
#endif
)
{
pushCommand<&RenderState::commandDepthTest>();
m_data.push(static_cast<int>(mode));
#if !defined(VGL_DISABLE_DEPTH_WRITE)
m_data.push(static_cast<int>(write));
#endif
}
/// Push texture uniform.
///
/// \param name Name of the uniform.
/// \param value Texture value.
void push(string_view name, const Texture2D& value)
{
pushUniformTexture(name, UniformSemantic::NONE, value);
}
/// Push texture uniform.
///
/// \param name Name of the uniform.
/// \param value Texture value.
void push(UniformSemantic semantic, const Texture2D& value)
{
pushUniformTexture(string_view(), semantic, value);
}
/// Push persistent texture uniform.
///
/// \param name Name of the uniform.
/// \param value Texture value.
void pushPersistent(string_view name, const Texture2D& value)
{
pushUniformTexturePersistent(name, UniformSemantic::NONE, value);
}
/// Push persistent texture uniform.
///
/// \param name Name of the uniform.
/// \param value Texture value.
void pushPersistent(UniformSemantic semantic, const Texture2D& value)
{
pushUniformTexturePersistent(string_view(), semantic, value);
}
public:
/// Push uniform.
///
/// \param name Name of the uniform.
/// \param value Value of the uniform.
template<typename T> void push(string_view name, const T& value)
{
pushUniform(name, UniformSemantic::NONE, &value, 1);
}
/// Push uniform.
///
/// \param semantic Uniform semantic.
/// \param value Value of the uniform.
template<typename T> void push(UniformSemantic semantic, const T& value)
{
pushUniform(string_view(), semantic, &value, 1);
}
/// Push uniform array.
///
/// \param name Name of the uniform.
/// \param ptr Pointer to uniform array.
/// \param count Number of matrices.
template<typename T> void push(string_view name, const T* ptr, unsigned count)
{
pushUniform(name, UniformSemantic::NONE, ptr, count);
}
/// Push uniform array.
///
/// \param semantic Uniform semantic.
/// \param ptr Pointer to uniform array.
/// \param count Number of matrices.
template<typename T> void push(UniformSemantic semantic, const T* ptr, unsigned count)
{
pushUniform(string_view(), semantic, ptr, count);
}
/// Templated push of a command.
template<void (RenderState::*F)(PackedDataReader&)> void pushCommand()
{
m_data.push(reinterpret_cast<void*>(commandFunc<F>));
}
private:
/// Command function template.
///
/// \param state Render state.
/// \param iter Read iterator.
template<void (RenderState::*F)(PackedDataReader&)> static void commandFunc(RenderState& state, PackedDataReader& iter)
{
(state.*F)(iter);
}
};
}
#endif