-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathrender_pipeline.cpp
More file actions
507 lines (433 loc) · 18.1 KB
/
Copy pathrender_pipeline.cpp
File metadata and controls
507 lines (433 loc) · 18.1 KB
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
////////////////////////////////////////////////////////////////////////////////
// Distributed under the Boost Software License, Version 1.0. //
// (See accompanying file LICENSE or copy at //
// https://www.boost.org/LICENSE_1_0.txt) //
////////////////////////////////////////////////////////////////////////////////
#include "graphics/render_pipeline.h"
#include <cstddef>
#include <memory>
#include <vector>
#include "core/error_handling.h"
#include "graphics/material_manager.h"
#include "graphics/mesh_manager.h"
#include "graphics/render_graph/binary_operator_node.h"
#include "graphics/render_graph/blur_node.h"
#include "graphics/render_graph/component_node.h"
#include "graphics/render_graph/conditional_node.h"
#include "graphics/render_graph/node.h"
#include "graphics/render_graph/post_processing/ambient_occlusion_node.h"
#include "graphics/render_graph/post_processing/anti_aliasing_node.h"
#include "graphics/render_graph/post_processing/colour_adjust_node.h"
#include "graphics/render_graph/render_graph.h"
#include "graphics/render_graph/sky_box_node.h"
#include "graphics/render_graph/texture_node.h"
#include "graphics/render_graph/value_node.h"
#include "graphics/render_pass.h"
#include "graphics/render_pipeline.h"
#include "graphics/render_target.h"
#include "graphics/render_target_manager.h"
#include "graphics/renderer.h"
#include "graphics/scene.h"
#include "graphics/single_entity.h"
#include "log/log.h"
namespace
{
/**
* Helper function to create and enqueue all commands for rendering a Scene with
* a given light type.
*
* @param scene
* Scene to render.
*
* @param light_type
* Type of light for the scene.
*
* @param has_normal_target
* Flag indicating if normals should be rendered.
*
* @param has_position_target
* Flag indicating if positions should be rendered.
*
* @param sky_box_render_graph
* Render graph for the sky box for this scene (if one is present), otherwise nullptr.
*
* @param cmd
* Command object to mutate and enqueue, this is passed in so it can be "pre-loaded" with the correct state.
*
* @param render_queue
* Queue to add render commands to.
*
* @param shadow_maps
* Map of directional lights to their associated shadow map render target.
*/
void encode_light_pass_commands(
iris::MaterialManager &material_manager,
const iris::Scene *scene,
iris::LightType light_type,
bool has_normal_target,
bool has_position_target,
const iris::RenderGraph *sky_box_render_graph,
iris::RenderCommand &cmd,
std::vector<iris::RenderCommand> &render_queue,
const std::unordered_map<iris::DirectionalLight *, iris::RenderTarget *> &shadow_maps)
{
for (const auto &[render_graph, render_entity] : scene->entities())
{
// if we have a sky box we only want to render it once on the ambient pass
if ((render_graph == sky_box_render_graph) && (light_type != iris::LightType::AMBIENT))
{
continue;
}
auto *material = material_manager.create(
render_graph,
render_entity.get(),
light_type,
cmd.render_pass()->colour_target != nullptr,
has_normal_target && (light_type == iris::LightType::AMBIENT),
has_position_target && (light_type == iris::LightType::AMBIENT),
render_entity->has_transparency());
cmd.set_material(material);
cmd.set_render_entity(render_entity.get());
// light specific draw commands
switch (light_type)
{
case iris::LightType::AMBIENT:
cmd.set_type(iris::RenderCommandType::DRAW);
cmd.set_light(scene->lighting_rig()->ambient_light.get());
render_queue.push_back(cmd);
break;
case iris::LightType::POINT:
// a draw command for each light
for (auto &light : scene->lighting_rig()->point_lights)
{
cmd.set_type(iris::RenderCommandType::DRAW);
cmd.set_light(light.get());
render_queue.push_back(cmd);
}
break;
case iris::LightType::DIRECTIONAL:
// a draw command for each light
for (auto &light : scene->lighting_rig()->directional_lights)
{
cmd.set_type(iris::RenderCommandType::DRAW);
cmd.set_light(light.get());
// set shadow map in render command
if (render_entity->receive_shadow())
{
auto *shadow_map = shadow_maps.count(light.get()) == 0u ? nullptr : shadow_maps.at(light.get());
cmd.set_shadow_map(shadow_map);
}
render_queue.push_back(cmd);
}
break;
}
}
}
}
namespace iris
{
RenderPipeline::RenderPipeline(
MaterialManager &material_manager,
MeshManager &mesh_manager,
RenderTargetManager &render_target_manager,
std::uint32_t width,
std::uint32_t height)
: material_manager_(material_manager)
, mesh_manager_(mesh_manager)
, render_target_manager_(render_target_manager)
, scenes_()
, render_graphs_()
, user_created_passes_()
, engine_created_passes_()
, render_passes_()
, sky_box_entities_()
, dirty_(false)
, width_(width)
, height_(height)
, cameras_()
, shadow_maps_()
{
}
RenderPipeline::~RenderPipeline() = default;
Scene *RenderPipeline::create_scene()
{
static RenderGraph default_render_graph{material_manager_.create_property_buffer()};
// using new to access private ctor
scenes_.push_back(std::unique_ptr<Scene>(new Scene{&default_render_graph, &dirty_}));
return scenes_.back().get();
}
RenderGraph *RenderPipeline::create_render_graph()
{
// using new to access private ctor
render_graphs_.push_back(std::unique_ptr<RenderGraph>(new RenderGraph{material_manager_.create_property_buffer()}));
return render_graphs_.back().get();
}
RenderPass *RenderPipeline::create_render_pass(Scene *scene)
{
// using new to access private ctor
auto pass = std::unique_ptr<RenderPass>(new RenderPass{});
pass->scene = scene;
user_created_passes_.push_back(std::move(pass));
return user_created_passes_.back().get();
}
std::vector<RenderCommand> RenderPipeline::build()
{
engine_created_passes_.clear();
sky_box_entities_.clear();
shadow_maps_.clear();
std::vector<RenderPass *> pre_process_passes{};
// for each shadow casting light create a render target for the shadow map
// and enqueue commands so they are rendered
for (const auto &pass : user_created_passes_)
{
for (const auto &light : pass->scene->lighting_rig()->directional_lights)
{
if (light->casts_shadows())
{
auto *rt = render_target_manager_.create(1024u, 1024u);
RenderPass *shadow_pass = create_engine_render_pass(pass->scene);
shadow_pass->post_processing_description = {};
shadow_pass->camera = std::addressof(light->shadow_camera());
shadow_pass->colour_target = rt;
shadow_pass->normal_target = nullptr;
shadow_pass->position_target = nullptr;
shadow_pass->depth_only = true;
shadow_pass->clear_colour = true;
shadow_pass->clear_depth = true;
pre_process_passes.push_back(shadow_pass);
shadow_maps_[light.get()] = rt;
}
}
// SSAO is a bit messy to integrate, we first need to create a pass to output all the required information
// such as screen space normals and positions and then add the pass to combine all this data into our
// occlusion texture later on we will then wire this into the normal lighting passes, due to how the render
// is setup it's not easy to just add the occlusion texture into the ambient light pass, so instead we do
// that whole calculation in the SSAO pass and use that instead of recalculating the ambient pass
if (const auto ssao = pass->post_processing_description.ambient_occlusion; ssao)
{
// add a pass to output the data we need
RenderPass *ao_data_pass = create_engine_render_pass(pass->scene);
ao_data_pass->post_processing_description = {};
ao_data_pass->colour_target = nullptr;
ao_data_pass->normal_target = render_target_manager_.create(width_, height_);
ao_data_pass->position_target = render_target_manager_.create(width_, height_);
ao_data_pass->depth_only = true;
ao_data_pass->clear_colour = true;
ao_data_pass->camera = pass->camera;
pre_process_passes.push_back(ao_data_pass);
auto *prev = pre_process_passes.back();
const auto prev_camera = prev->camera;
// add a pass to calculate ssao (combined with the ambient light pass)
auto *ao_target = add_pass(pre_process_passes, [prev, ssao](RenderGraph *rg, const RenderTarget *target) {
rg->set_render_node<AmbientOcclusionNode>(
rg->create<TextureNode>(target->colour_texture()),
rg->create<TextureNode>(prev->normal_target->colour_texture()),
rg->create<TextureNode>(prev->position_target->colour_texture()),
*ssao);
});
// ensure we render with the perspective camera not the orthographic camera that will be created for the
// new pass
cameras_.back() = *prev_camera;
prev = pre_process_passes.back();
prev->colour_target = pass->colour_target;
// fudge the colour target of the pre pass and the depth target of the ssao pass into one render target
pass->colour_target = render_target_manager_.create(prev->colour_target, ao_target);
pass->clear_colour = false;
pass->clear_depth = false;
}
// if a skybox has been added ensure it has an entity in the scene
if (pass->sky_box != nullptr)
{
auto *scene = pass->scene;
auto *sky_box_rg = create_render_graph();
sky_box_rg->set_render_node<SkyBoxNode>(pass->sky_box);
const auto [_, inserted] = sky_box_entities_.try_emplace(
pass.get(),
scene->create_entity_at_front<SingleEntity>(
sky_box_rg, mesh_manager_.cube({}), Transform({}, {}, {0.5f})));
expect(inserted, "sky box exists");
sky_box_entities_[pass.get()]->set_receive_shadow(false);
sky_box_render_graphs_[pass.get()] = sky_box_rg;
}
}
render_passes_ = pre_process_passes;
std::transform(
std::begin(user_created_passes_),
std::end(user_created_passes_),
std::back_insert_iterator(render_passes_),
[](const auto &element) { return element.get(); });
add_post_processing_passes();
return rebuild();
}
std::vector<RenderCommand> RenderPipeline::rebuild()
{
std::vector<RenderCommand> render_queue;
RenderCommand cmd{};
// convert each pass into a series of commands which will render it
for (auto *pass : render_passes_)
{
cmd.set_render_pass(pass);
cmd.set_type(RenderCommandType::PASS_START);
render_queue.push_back(cmd);
const auto has_normal_target = pass->normal_target != nullptr;
const auto has_position_target = pass->position_target != nullptr;
const auto *sky_box_rg = (sky_box_render_graphs_.contains(pass)) ? sky_box_render_graphs_[pass] : nullptr;
// encode ambient light pass unless we have used ssao (in which case this gets done by the ssao pass itself)
if (!pass->post_processing_description.ambient_occlusion)
{
encode_light_pass_commands(
material_manager_,
pass->scene,
LightType::AMBIENT,
has_normal_target,
has_position_target,
sky_box_rg,
cmd,
render_queue,
shadow_maps_);
}
if (!pass->depth_only)
{
// encode point lights if there are any
if (!pass->scene->lighting_rig()->point_lights.empty())
{
encode_light_pass_commands(
material_manager_,
pass->scene,
LightType::POINT,
has_normal_target,
has_position_target,
sky_box_rg,
cmd,
render_queue,
shadow_maps_);
}
// encode directional lights if there are any
if (!pass->scene->lighting_rig()->directional_lights.empty())
{
encode_light_pass_commands(
material_manager_,
pass->scene,
LightType::DIRECTIONAL,
has_normal_target,
has_position_target,
sky_box_rg,
cmd,
render_queue,
shadow_maps_);
}
}
cmd.set_type(RenderCommandType::PASS_END);
render_queue.push_back(cmd);
}
cmd.set_type(RenderCommandType::PRESENT);
render_queue.push_back(cmd);
return render_queue;
}
std::vector<RenderPass *> RenderPipeline::render_passes() const
{
return render_passes_;
}
bool RenderPipeline::is_dirty() const
{
return dirty_;
}
void RenderPipeline::clear_dirty_bit()
{
dirty_ = false;
}
RenderPass *RenderPipeline::create_engine_render_pass(Scene *scene)
{
// using new to access private ctor
auto pass = std::unique_ptr<RenderPass>(new RenderPass{});
pass->scene = scene;
engine_created_passes_.push_back(std::move(pass));
return engine_created_passes_.back().get();
}
const RenderTarget *RenderPipeline::add_pass(
std::vector<RenderPass *> &render_passes,
std::function<void(RenderGraph *, const RenderTarget *)> create_render_graph_callback)
{
// create new pass with
cameras_.push_back({CameraType::ORTHOGRAPHIC, width_, height_});
auto *camera = std::addressof(cameras_.back());
const auto *target = render_target_manager_.create(width_, height_);
auto *scene = create_scene();
auto *rg = create_render_graph();
create_render_graph_callback(rg, target);
scene->create_entity<SingleEntity>(
rg,
mesh_manager_.sprite({}),
Transform({}, {}, {static_cast<float>(width_), static_cast<float>(height_), 1. - 1}));
auto *pass = create_engine_render_pass(scene);
pass->camera = camera;
// wire up this pass
render_passes.back()->colour_target = target;
render_passes.push_back(pass);
return target;
}
void RenderPipeline::add_post_processing_passes()
{
// our class member render_passes_ contains all the user passes and *some* engine passes
// in order to add the rest we copy them one by one to a new collection and, for each one, add any additioanl
// post processing passes
// at the end we copy that back to the class member render_passes_
std::vector<RenderPass *> render_passes{};
for (auto *render_pass : render_passes_)
{
auto &last_pass = render_passes.emplace_back(render_pass);
const auto *input_target = last_pass->colour_target;
const auto description = render_pass->post_processing_description;
if (const auto bloom = description.bloom; bloom)
{
const auto *null_target = add_pass(render_passes, [](RenderGraph *rg, const RenderTarget *target) {
rg->render_node()->set_colour_input(rg->create<TextureNode>(target->colour_texture()));
});
add_pass(render_passes, [&bloom](RenderGraph *rg, const RenderTarget *target) {
rg->render_node()->set_colour_input(rg->create<ConditionalNode>(
rg->create<BinaryOperatorNode>(
rg->create<TextureNode>(target->colour_texture()),
rg->create<ValueNode<Colour>>(Colour{0.2126f, 0.7152f, 0.0722f, 0.0f}),
BinaryOperator::DOT),
rg->create<ValueNode<float>>(bloom->threshold),
rg->create<TextureNode>(target->colour_texture()),
rg->create<ValueNode<Colour>>(Colour{0.0f, 0.0f, 0.0f, 1.0f}),
ConditionalOperator::GREATER));
});
for (auto i = 0u; i < bloom->iterations; ++i)
{
add_pass(render_passes, [](RenderGraph *rg, const RenderTarget *target) {
rg->render_node()->set_colour_input(
rg->create<BlurNode>(rg->create<TextureNode>(target->colour_texture())));
});
}
add_pass(render_passes, [null_target](RenderGraph *rg, const RenderTarget *target) {
rg->render_node()->set_colour_input(rg->create<BinaryOperatorNode>(
rg->create<TextureNode>(null_target->colour_texture()),
rg->create<TextureNode>(target->colour_texture()),
BinaryOperator::ADD));
});
}
if (const auto colour_adjust = description.colour_adjust; colour_adjust)
{
add_pass(render_passes, [&colour_adjust](RenderGraph *rg, const RenderTarget *target) {
rg->set_render_node<ColourAdjustNode>(
rg->create<TextureNode>(target->colour_texture()), *colour_adjust);
});
}
if (description.anti_aliasing)
{
add_pass(render_passes, [](RenderGraph *rg, const RenderTarget *target) {
rg->set_render_node<AntiAliasingNode>(rg->create<TextureNode>(target->colour_texture()));
});
}
render_passes.back()->colour_target = input_target;
}
render_passes_ = render_passes;
}
Scene *RenderPipeline::scene(std::size_t index) const
{
ensure(index < scenes_.size(), "index out of bounds");
return scenes_[index].get();
}
}