forked from Themaister/Granite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render_graph.hpp
888 lines (737 loc) · 23.1 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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
/* Copyright (c) 2017-2020 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_headers.hpp"
#include "device.hpp"
#include "stack_allocator.hpp"
#include "application_wsi_events.hpp"
#include "quirks.hpp"
namespace Granite
{
class RenderGraph;
class RenderPass;
enum SizeClass
{
Absolute,
SwapchainRelative,
InputRelative
};
enum RenderGraphQueueFlagBits
{
RENDER_GRAPH_QUEUE_GRAPHICS_BIT = 1 << 0,
RENDER_GRAPH_QUEUE_COMPUTE_BIT = 1 << 1,
RENDER_GRAPH_QUEUE_ASYNC_COMPUTE_BIT = 1 << 2,
RENDER_GRAPH_QUEUE_ASYNC_GRAPHICS_BIT = 1 << 3
};
using RenderGraphQueueFlags = uint32_t;
struct AttachmentInfo
{
SizeClass size_class = SizeClass::SwapchainRelative;
float size_x = 1.0f;
float size_y = 1.0f;
float size_z = 0.0f;
VkFormat format = VK_FORMAT_UNDEFINED;
std::string size_relative_name;
unsigned samples = 1;
unsigned levels = 1;
unsigned layers = 1;
VkImageUsageFlags aux_usage = 0;
bool persistent = true;
bool unorm_srgb_alias = false;
};
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 unorm_srgb = false;
bool persistent = true;
RenderGraphQueueFlags queues = 0;
VkImageUsageFlags image_usage = 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 == other.buffer_info &&
transient == other.transient &&
persistent == other.persistent &&
unorm_srgb == other.unorm_srgb;
// image_usage is deliberately not part of this test.
// queues is deliberately not part of this test.
}
bool operator!=(const ResourceDimensions &other) const
{
return !(*this == other);
}
bool uses_semaphore() const
{
// If more than one queue is used for a resource, we need to use semaphores.
auto physical_queues = queues;
// Regular compute uses regular graphics queue.
if (physical_queues & RENDER_GRAPH_QUEUE_COMPUTE_BIT)
physical_queues |= RENDER_GRAPH_QUEUE_GRAPHICS_BIT;
physical_queues &= ~RENDER_GRAPH_QUEUE_COMPUTE_BIT;
return (physical_queues & (physical_queues - 1)) != 0;
}
bool is_storage_image() const
{
return (image_usage & VK_IMAGE_USAGE_STORAGE_BIT) != 0;
}
bool is_buffer_like() const
{
return is_storage_image() || (buffer_info.size != 0);
}
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;
}
std::unordered_set<unsigned> &get_read_passes()
{
return read_in_passes;
}
std::unordered_set<unsigned> &get_write_passes()
{
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_)
{
name = name_;
}
const std::string &get_name() const
{
return name;
}
void add_queue(RenderGraphQueueFlagBits queue)
{
used_queues |= queue;
}
RenderGraphQueueFlags get_used_queues() const
{
return used_queues;
}
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;
RenderGraphQueueFlags used_queues = 0;
};
class RenderBufferResource : public RenderResource
{
public:
explicit RenderBufferResource(unsigned index_)
: RenderResource(RenderResource::Type::Buffer, index_)
{
}
void set_buffer_info(const BufferInfo &info_)
{
info = info_;
}
const BufferInfo &get_buffer_info() const
{
return info;
}
void add_buffer_usage(VkBufferUsageFlags flags)
{
buffer_usage |= flags;
}
VkBufferUsageFlags get_buffer_usage() const
{
return buffer_usage;
}
private:
BufferInfo info;
VkBufferUsageFlags buffer_usage = 0;
};
class RenderTextureResource : public RenderResource
{
public:
explicit RenderTextureResource(unsigned index_)
: RenderResource(RenderResource::Type::Texture, index_)
{
}
void set_attachment_info(const AttachmentInfo &info_)
{
info = info_;
}
const AttachmentInfo &get_attachment_info() const
{
return info;
}
AttachmentInfo &get_attachment_info()
{
return info;
}
void set_transient_state(bool enable)
{
transient = enable;
}
bool get_transient_state() const
{
return transient;
}
void add_image_usage(VkImageUsageFlags flags)
{
image_usage |= flags;
}
VkImageUsageFlags get_image_usage() const
{
return image_usage;
}
private:
AttachmentInfo info;
VkImageUsageFlags image_usage = 0;
bool transient = false;
};
class RenderPass
{
public:
RenderPass(RenderGraph &graph_, unsigned index_, RenderGraphQueueFlagBits queue_)
: graph(graph_), index(index_), queue(queue_)
{
}
enum { Unused = ~0u };
struct AccessedResource
{
VkPipelineStageFlags stages = 0;
VkAccessFlags access = 0;
VkImageLayout layout = VK_IMAGE_LAYOUT_UNDEFINED;
};
struct AccessedTextureResource : AccessedResource
{
RenderTextureResource *texture = nullptr;
};
struct AccessedBufferResource : AccessedResource
{
RenderBufferResource *buffer = nullptr;
};
RenderGraphQueueFlagBits get_queue() const
{
return queue;
}
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_attachment_input(const std::string &name);
RenderTextureResource &add_history_input(const std::string &name);
RenderTextureResource &add_texture_input(const std::string &name,
VkPipelineStageFlags stages = 0);
RenderTextureResource &add_blit_texture_read_only_input(const std::string &name);
RenderBufferResource &add_uniform_input(const std::string &name,
VkPipelineStageFlags stages = 0);
RenderBufferResource &add_storage_read_only_input(const std::string &name,
VkPipelineStageFlags stages = 0);
RenderBufferResource &add_storage_output(const std::string &name, const BufferInfo &info, const std::string &input = "");
RenderBufferResource &add_transfer_output(const std::string &name, const BufferInfo &info);
RenderTextureResource &add_storage_texture_output(const std::string &name, const AttachmentInfo &info, const std::string &input = "");
RenderTextureResource &add_blit_texture_output(const std::string &name, const AttachmentInfo &info, const std::string &input = "");
RenderBufferResource &add_vertex_buffer_input(const std::string &name);
RenderBufferResource &add_index_buffer_input(const std::string &name);
RenderBufferResource &add_indirect_buffer_input(const std::string &name);
void add_fake_resource_write_alias(const std::string &from, const std::string &to);
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_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_blit_texture_inputs() const
{
return blit_texture_inputs;
}
const std::vector<RenderTextureResource *> &get_blit_texture_outputs() const
{
return blit_texture_outputs;
}
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_storage_inputs() const
{
return storage_inputs;
}
const std::vector<RenderBufferResource *> &get_storage_outputs() const
{
return storage_outputs;
}
const std::vector<RenderBufferResource *> &get_transfer_outputs() const
{
return transfer_outputs;
}
const std::vector<AccessedTextureResource> &get_generic_texture_inputs() const
{
return generic_texture;
}
const std::vector<AccessedBufferResource> &get_generic_buffer_inputs() const
{
return generic_buffer;
}
const std::vector<std::pair<RenderTextureResource *, RenderTextureResource *>> &get_fake_resource_aliases() const
{
return fake_resource_alias;
}
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 may_not_need_render_pass() const
{
return bool(need_render_pass_cb);
}
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, unsigned layer)
{
if (build_render_pass_layered_cb)
build_render_pass_layered_cb(layer, cmd);
else if (build_render_pass_cb)
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_build_render_pass_layered(std::function<void (unsigned, Vulkan::CommandBuffer &)> func)
{
build_render_pass_layered_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)
{
pass_name = name;
}
const std::string &get_name() const
{
return pass_name;
}
private:
RenderGraph &graph;
unsigned index;
unsigned physical_pass = Unused;
RenderGraphQueueFlagBits queue;
std::function<void (Vulkan::CommandBuffer &)> build_render_pass_cb;
std::function<void (unsigned, Vulkan::CommandBuffer &)> build_render_pass_layered_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 *> storage_texture_inputs;
std::vector<RenderTextureResource *> storage_texture_outputs;
std::vector<RenderTextureResource *> blit_texture_inputs;
std::vector<RenderTextureResource *> blit_texture_outputs;
std::vector<RenderTextureResource *> attachments_inputs;
std::vector<RenderTextureResource *> history_inputs;
std::vector<RenderBufferResource *> storage_outputs;
std::vector<RenderBufferResource *> storage_inputs;
std::vector<RenderBufferResource *> transfer_outputs;
std::vector<AccessedTextureResource> generic_texture;
std::vector<AccessedBufferResource> generic_buffer;
RenderTextureResource *depth_stencil_input = nullptr;
RenderTextureResource *depth_stencil_output = nullptr;
std::vector<std::pair<RenderTextureResource *, RenderTextureResource *>> fake_resource_alias;
std::string pass_name;
RenderBufferResource &add_generic_buffer_input(const std::string &name,
VkPipelineStageFlags stages,
VkAccessFlags access,
VkBufferUsageFlags usage);
};
class RenderGraph : public Vulkan::NoCopyNoMove, public EventHandler
{
public:
RenderGraph();
void set_device(Vulkan::Device *device_)
{
device = device_;
}
Vulkan::Device &get_device()
{
assert(device);
return *device;
}
RenderPass &add_pass(const std::string &name, RenderGraphQueueFlagBits queue);
void set_backbuffer_source(const std::string &name);
void set_backbuffer_dimensions(const ResourceDimensions &dim)
{
swapchain_dimensions = dim;
}
const ResourceDimensions &get_backbuffer_dimensions() const
{
return swapchain_dimensions;
}
void enable_timestamps(bool enable);
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(index != RenderResource::Unused);
assert(physical_attachments[index]);
return *physical_attachments[index];
}
Vulkan::ImageView *get_physical_history_texture_resource(unsigned index)
{
assert(index != RenderResource::Unused);
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(index != RenderResource::Unused);
assert(physical_buffers[index]);
return *physical_buffers[index];
}
Vulkan::ImageView &get_physical_texture_resource(const RenderTextureResource &resource)
{
assert(resource.get_physical_index() != RenderResource::Unused);
return get_physical_texture_resource(resource.get_physical_index());
}
Vulkan::ImageView *maybe_get_physical_texture_resource(RenderTextureResource *resource)
{
if (resource && resource->get_physical_index() != RenderResource::Unused)
return &get_physical_texture_resource(*resource);
else
return nullptr;
}
Vulkan::ImageView *get_physical_history_texture_resource(const RenderTextureResource &resource)
{
return get_physical_history_texture_resource(resource.get_physical_index());
}
Vulkan::Buffer &get_physical_buffer_resource(const RenderBufferResource &resource)
{
assert(resource.get_physical_index() != RenderResource::Unused);
return get_physical_buffer_resource(resource.get_physical_index());
}
Vulkan::Buffer *maybe_get_physical_buffer_resource(RenderBufferResource *resource)
{
if (resource && resource->get_physical_index() != RenderResource::Unused)
return &get_physical_buffer_resource(*resource);
else
return nullptr;
}
// 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);
static inline RenderGraphQueueFlagBits get_default_post_graphics_queue()
{
if (Vulkan::ImplementationQuirks::get().use_async_compute_post &&
!Vulkan::ImplementationQuirks::get().render_graph_force_single_queue)
{
return RENDER_GRAPH_QUEUE_ASYNC_GRAPHICS_BIT;
}
else
{
return RENDER_GRAPH_QUEUE_GRAPHICS_BIT;
}
}
static inline RenderGraphQueueFlagBits get_default_compute_queue()
{
if (Vulkan::ImplementationQuirks::get().render_graph_force_single_queue)
return RENDER_GRAPH_QUEUE_COMPUTE_BIT;
else
return RENDER_GRAPH_QUEUE_ASYNC_COMPUTE_BIT;
}
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;
unsigned layers = 1;
};
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();
bool enabled_timestamps = false;
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] = {};
VkImageLayout layout = VK_IMAGE_LAYOUT_UNDEFINED;
};
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);
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);
};
}