forked from Themaister/Granite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender_graph.hpp
701 lines (580 loc) · 17.3 KB
/
render_graph.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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
/* Copyright (c) 2017 Hans-Kristian Arntzen
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <vector>
#include <memory>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <string>
#include <functional>
#include "vulkan.hpp"
#include "device.hpp"
#include "stack_allocator.hpp"
#include "vulkan_events.hpp"
namespace Granite
{
class RenderGraph;
class RenderPass;
enum SizeClass
{
Absolute,
SwapchainRelative,
InputRelative
};
struct AttachmentInfo
{
SizeClass size_class = SizeClass::SwapchainRelative;
float size_x = 1.0f;
float size_y = 1.0f;
VkFormat format = VK_FORMAT_UNDEFINED;
std::string size_relative_name;
unsigned samples = 1;
unsigned levels = 1;
unsigned layers = 1;
bool persistent = true;
};
struct BufferInfo
{
VkDeviceSize size = 0;
VkBufferUsageFlags usage = 0;
bool persistent = true;
bool operator==(const BufferInfo &other) const
{
return size == other.size &&
usage == other.usage &&
persistent == other.persistent;
}
bool operator!=(const BufferInfo &other) const
{
return !(*this == other);
}
};
struct ResourceDimensions
{
VkFormat format = VK_FORMAT_UNDEFINED;
BufferInfo buffer_info;
unsigned width = 0;
unsigned height = 0;
unsigned depth = 1;
unsigned layers = 1;
unsigned levels = 1;
unsigned samples = 1;
bool transient = false;
bool persistent = true;
bool storage = false;
VkPipelineStageFlags stages = 0;
bool operator==(const ResourceDimensions &other) const
{
return format == other.format &&
width == other.width &&
height == other.height &&
depth == other.depth &&
layers == other.layers &&
levels == other.levels &&
buffer_info == buffer_info &&
transient == other.transient &&
persistent == other.persistent &&
storage == other.storage;
// stages is deliberately not part of this test.
}
bool operator!=(const ResourceDimensions &other) const
{
return !(*this == other);
}
bool uses_semaphore() const
{
static const VkPipelineStageFlags concurrent =
VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT |
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT;
return (stages & concurrent) == concurrent;
}
std::string name;
};
class RenderResource
{
public:
enum class Type
{
Buffer,
Texture
};
enum { Unused = ~0u };
RenderResource(Type type, unsigned index)
: resource_type(type), index(index)
{
}
virtual ~RenderResource() = default;
Type get_type() const
{
return resource_type;
}
void written_in_pass(unsigned index)
{
written_in_passes.insert(index);
}
void read_in_pass(unsigned index)
{
read_in_passes.insert(index);
}
const std::unordered_set<unsigned> &get_read_passes() const
{
return read_in_passes;
}
const std::unordered_set<unsigned> &get_write_passes() const
{
return written_in_passes;
}
unsigned get_index() const
{
return index;
}
void set_physical_index(unsigned index)
{
physical_index = index;
}
unsigned get_physical_index() const
{
return physical_index;
}
void set_name(const std::string &name)
{
this->name = name;
}
const std::string &get_name() const
{
return name;
}
void add_stages(VkPipelineStageFlags stages)
{
used_stages |= stages;
}
VkPipelineStageFlags get_used_stages() const
{
return used_stages;
}
private:
Type resource_type;
unsigned index;
unsigned physical_index = Unused;
std::unordered_set<unsigned> written_in_passes;
std::unordered_set<unsigned> read_in_passes;
std::string name;
VkPipelineStageFlags used_stages = 0;
};
class RenderBufferResource : public RenderResource
{
public:
RenderBufferResource(unsigned index)
: RenderResource(RenderResource::Type::Buffer, index)
{
}
void set_buffer_info(const BufferInfo &info)
{
this->info = info;
}
const BufferInfo &get_buffer_info() const
{
return info;
}
private:
BufferInfo info;
};
class RenderTextureResource : public RenderResource
{
public:
RenderTextureResource(unsigned index)
: RenderResource(RenderResource::Type::Texture, index)
{
}
void set_attachment_info(const AttachmentInfo &info)
{
this->info = info;
}
const AttachmentInfo &get_attachment_info() const
{
return info;
}
void set_transient_state(bool enable)
{
transient = enable;
}
bool get_transient_state() const
{
return transient;
}
void set_storage_state(bool enable)
{
storage = enable;
}
bool get_storage_state() const
{
return storage;
}
private:
AttachmentInfo info;
bool transient = false;
bool storage = false;
};
class RenderPass
{
public:
RenderPass(RenderGraph &graph, unsigned index, VkPipelineStageFlags stages)
: graph(graph), index(index), stages(stages)
{
}
enum { Unused = ~0u };
VkPipelineStageFlags get_stages() const
{
return stages;
}
RenderGraph &get_graph()
{
return graph;
}
unsigned get_index() const
{
return index;
}
RenderTextureResource &set_depth_stencil_input(const std::string &name);
RenderTextureResource &set_depth_stencil_output(const std::string &name, const AttachmentInfo &info);
RenderTextureResource &add_color_output(const std::string &name, const AttachmentInfo &info, const std::string &input = "");
RenderTextureResource &add_resolve_output(const std::string &name, const AttachmentInfo &info);
RenderTextureResource &add_texture_input(const std::string &name);
RenderTextureResource &add_attachment_input(const std::string &name);
RenderTextureResource &add_history_input(const std::string &name);
RenderBufferResource &add_uniform_input(const std::string &name);
RenderBufferResource &add_storage_output(const std::string &name, const BufferInfo &info, const std::string &input = "");
RenderBufferResource &add_storage_read_only_input(const std::string &name);
RenderTextureResource &add_storage_texture_output(const std::string &name, const AttachmentInfo &info, const std::string &input = "");
void set_texture_inputs(Vulkan::CommandBuffer &cmd, unsigned set, unsigned start_binding,
Vulkan::StockSampler sampler);
void make_color_input_scaled(unsigned index)
{
std::swap(color_scale_inputs[index], color_inputs[index]);
}
const std::vector<RenderTextureResource *> &get_color_outputs() const
{
return color_outputs;
}
const std::vector<RenderTextureResource *> &get_resolve_outputs() const
{
return resolve_outputs;
}
const std::vector<RenderTextureResource *> &get_color_inputs() const
{
return color_inputs;
}
const std::vector<RenderTextureResource *> &get_color_scale_inputs() const
{
return color_scale_inputs;
}
const std::vector<RenderTextureResource *> &get_texture_inputs() const
{
return texture_inputs;
}
const std::vector<RenderTextureResource *> &get_storage_texture_outputs() const
{
return storage_texture_outputs;
}
const std::vector<RenderTextureResource *> &get_storage_texture_inputs() const
{
return storage_texture_inputs;
}
const std::vector<RenderTextureResource *> &get_attachment_inputs() const
{
return attachments_inputs;
}
const std::vector<RenderTextureResource *> &get_history_inputs() const
{
return history_inputs;
}
const std::vector<RenderBufferResource *> &get_uniform_inputs() const
{
return uniform_inputs;
}
const std::vector<RenderBufferResource *> &get_storage_inputs() const
{
return storage_inputs;
}
const std::vector<RenderBufferResource *> &get_storage_read_inputs() const
{
return storage_read_inputs;
}
const std::vector<RenderBufferResource *> &get_storage_outputs() const
{
return storage_outputs;
}
RenderTextureResource *get_depth_stencil_input() const
{
return depth_stencil_input;
}
RenderTextureResource *get_depth_stencil_output() const
{
return depth_stencil_output;
}
unsigned get_physical_pass_index() const
{
return physical_pass;
}
void set_physical_pass_index(unsigned index)
{
physical_pass = index;
}
bool need_render_pass()
{
if (need_render_pass_cb)
return need_render_pass_cb();
else
return true;
}
bool get_clear_color(unsigned index, VkClearColorValue * value = nullptr)
{
if (get_clear_color_cb)
return get_clear_color_cb(index, value);
else
return false;
}
bool get_clear_depth_stencil(VkClearDepthStencilValue * value = nullptr)
{
if (get_clear_depth_stencil_cb)
return get_clear_depth_stencil_cb(value);
else
return false;
}
void build_render_pass(Vulkan::CommandBuffer &cmd)
{
build_render_pass_cb(cmd);
}
void set_need_render_pass(std::function<bool ()> func)
{
need_render_pass_cb = std::move(func);
}
void set_build_render_pass(std::function<void (Vulkan::CommandBuffer &)> func)
{
build_render_pass_cb = std::move(func);
}
void set_get_clear_depth_stencil(std::function<bool (VkClearDepthStencilValue *)> func)
{
get_clear_depth_stencil_cb = std::move(func);
}
void set_get_clear_color(std::function<bool (unsigned, VkClearColorValue *)> func)
{
get_clear_color_cb = std::move(func);
}
void set_name(const std::string &name)
{
this->name = name;
}
const std::string &get_name() const
{
return name;
}
private:
RenderGraph &graph;
unsigned index;
unsigned physical_pass = Unused;
VkPipelineStageFlags stages;
std::function<void (Vulkan::CommandBuffer &)> build_render_pass_cb;
std::function<bool ()> need_render_pass_cb;
std::function<bool (VkClearDepthStencilValue *)> get_clear_depth_stencil_cb;
std::function<bool (unsigned, VkClearColorValue *)> get_clear_color_cb;
std::vector<RenderTextureResource *> color_outputs;
std::vector<RenderTextureResource *> resolve_outputs;
std::vector<RenderTextureResource *> color_inputs;
std::vector<RenderTextureResource *> color_scale_inputs;
std::vector<RenderTextureResource *> texture_inputs;
std::vector<RenderTextureResource *> storage_texture_inputs;
std::vector<RenderTextureResource *> storage_texture_outputs;
std::vector<RenderTextureResource *> attachments_inputs;
std::vector<RenderTextureResource *> history_inputs;
std::vector<RenderBufferResource *> uniform_inputs;
std::vector<RenderBufferResource *> storage_outputs;
std::vector<RenderBufferResource *> storage_read_inputs;
std::vector<RenderBufferResource *> storage_inputs;
RenderTextureResource *depth_stencil_input = nullptr;
RenderTextureResource *depth_stencil_output = nullptr;
std::string name;
};
class RenderGraph : public Vulkan::NoCopyNoMove, public EventHandler
{
public:
RenderGraph();
void set_device(Vulkan::Device *device)
{
this->device = device;
}
Vulkan::Device &get_device()
{
assert(device);
return *device;
}
RenderPass &add_pass(const std::string &name, VkPipelineStageFlags stages);
void set_backbuffer_source(const std::string &name);
void set_backbuffer_dimensions(const ResourceDimensions &dim)
{
swapchain_dimensions = dim;
}
void bake();
void reset();
void log();
void setup_attachments(Vulkan::Device &device, Vulkan::ImageView *swapchain);
void enqueue_render_passes(Vulkan::Device &device);
RenderTextureResource &get_texture_resource(const std::string &name);
RenderBufferResource &get_buffer_resource(const std::string &name);
Vulkan::ImageView &get_physical_texture_resource(unsigned index)
{
assert(physical_attachments[index]);
return *physical_attachments[index];
}
Vulkan::ImageView *get_physical_history_texture_resource(unsigned index)
{
if (!physical_history_image_attachments[index])
return nullptr;
return &physical_history_image_attachments[index]->get_view();
}
Vulkan::Buffer &get_physical_buffer_resource(unsigned index)
{
assert(physical_buffers[index]);
return *physical_buffers[index];
}
// For keeping feed-back resources alive during rebaking.
Vulkan::BufferHandle consume_persistent_physical_buffer_resource(unsigned index) const;
void install_persistent_physical_buffer_resource(unsigned index, Vulkan::BufferHandle buffer);
// Utility to consume all physical buffer handles and install them.
std::vector<Vulkan::BufferHandle> consume_physical_buffers() const;
void install_physical_buffers(std::vector<Vulkan::BufferHandle> buffers);
private:
Vulkan::Device *device = nullptr;
std::vector<std::unique_ptr<RenderPass>> passes;
std::vector<std::unique_ptr<RenderResource>> resources;
std::unordered_map<std::string, unsigned> pass_to_index;
std::unordered_map<std::string, unsigned> resource_to_index;
std::string backbuffer_source;
std::vector<unsigned> pass_stack;
struct Barrier
{
unsigned resource_index;
VkImageLayout layout;
VkAccessFlags access;
VkPipelineStageFlags stages;
bool history;
};
struct Barriers
{
std::vector<Barrier> invalidate;
std::vector<Barrier> flush;
};
std::vector<Barriers> pass_barriers;
void filter_passes(std::vector<unsigned> &list);
void validate_passes();
void build_barriers();
ResourceDimensions get_resource_dimensions(const RenderBufferResource &resource) const;
ResourceDimensions get_resource_dimensions(const RenderTextureResource &resource) const;
ResourceDimensions swapchain_dimensions;
struct ColorClearRequest
{
RenderPass *pass;
VkClearColorValue *target;
unsigned index;
};
struct DepthClearRequest
{
RenderPass *pass;
VkClearDepthStencilValue *target;
};
struct ScaledClearRequests
{
unsigned target;
unsigned physical_resource;
};
struct MipmapRequests
{
unsigned physical_resource;
VkPipelineStageFlags stages;
VkAccessFlags access;
VkImageLayout layout;
};
struct PhysicalPass
{
std::vector<unsigned> passes;
std::vector<unsigned> discards;
std::vector<Barrier> invalidate;
std::vector<Barrier> flush;
std::vector<Barrier> history;
std::vector<std::pair<unsigned, unsigned>> alias_transfer;
Vulkan::RenderPassInfo render_pass_info;
std::vector<Vulkan::RenderPassInfo::Subpass> subpasses;
std::vector<unsigned> physical_color_attachments;
unsigned physical_depth_stencil_attachment = RenderResource::Unused;
std::vector<ColorClearRequest> color_clear_requests;
DepthClearRequest depth_clear_request;
std::vector<std::vector<ScaledClearRequests>> scaled_clear_requests;
std::vector<MipmapRequests> mipmap_requests;
};
std::vector<PhysicalPass> physical_passes;
void build_physical_passes();
void build_transients();
void build_physical_resources();
void build_physical_barriers();
void build_render_pass_info();
void build_aliases();
std::vector<ResourceDimensions> physical_dimensions;
std::vector<Vulkan::ImageView *> physical_attachments;
std::vector<Vulkan::BufferHandle> physical_buffers;
std::vector<Vulkan::ImageHandle> physical_image_attachments;
std::vector<Vulkan::ImageHandle> physical_history_image_attachments;
struct PipelineEvent
{
Vulkan::PipelineEvent event;
// Need two separate semaphores so we can wait in both queues independently.
// Waiting for a semaphore resets it.
Vulkan::Semaphore wait_graphics_semaphore;
Vulkan::Semaphore wait_compute_semaphore;
// Stages to wait for are stored inside the events.
VkAccessFlags to_flush_access = 0;
VkAccessFlags invalidated_in_stage[32] = {};
};
std::vector<PipelineEvent> physical_events;
std::vector<PipelineEvent> physical_history_events;
std::vector<bool> physical_image_has_history;
std::vector<unsigned> physical_aliases;
Vulkan::ImageView *swapchain_attachment = nullptr;
unsigned swapchain_physical_index = RenderResource::Unused;
void enqueue_scaled_requests(Vulkan::CommandBuffer &cmd, const std::vector<ScaledClearRequests> &requests);
void enqueue_mipmap_requests(Vulkan::CommandBuffer &cmd, const std::vector<MipmapRequests> &requests);
void on_swapchain_changed(const Vulkan::SwapchainParameterEvent &e);
void on_swapchain_destroyed(const Vulkan::SwapchainParameterEvent &e);
void on_device_created(const Vulkan::DeviceCreatedEvent &e);
void on_device_destroyed(const Vulkan::DeviceCreatedEvent &e);
void setup_physical_buffer(Vulkan::Device &device, unsigned attachment);
void setup_physical_image(Vulkan::Device &device, unsigned attachment, bool storage);
void depend_passes_recursive(const RenderPass &pass, const std::unordered_set<unsigned> &passes,
unsigned stack_count, bool no_check, bool ignore_self, bool merge_dependency);
void traverse_dependencies(const RenderPass &pass, unsigned stack_count);
std::vector<std::unordered_set<unsigned>> pass_dependencies;
std::vector<std::unordered_set<unsigned>> pass_merge_dependencies;
bool depends_on_pass(unsigned dst_pass, unsigned src_pass);
void reorder_passes(std::vector<unsigned> &passes);
static bool need_invalidate(const Barrier &barrier, const PipelineEvent &event);
};
}