forked from godotengine/godot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrendering_device_graph.cpp
More file actions
2868 lines (2523 loc) · 159 KB
/
Copy pathrendering_device_graph.cpp
File metadata and controls
2868 lines (2523 loc) · 159 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
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
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************/
/* rendering_device_graph.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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. */
/**************************************************************************/
#include "rendering_device_graph.h"
#define PRINT_RENDER_GRAPH 0
#define FORCE_FULL_ACCESS_BITS 0
#define PRINT_RESOURCE_TRACKER_TOTAL 0
#define PRINT_COMMAND_RECORDING 0
// Prints the total number of bytes used for draw lists in a frame.
#define PRINT_DRAW_LIST_STATS 0
RenderingDeviceGraph::RenderingDeviceGraph() {
driver_honors_barriers = false;
driver_clears_with_copy_engine = false;
}
RenderingDeviceGraph::~RenderingDeviceGraph() {
}
String RenderingDeviceGraph::_usage_to_string(ResourceUsage p_usage) {
switch (p_usage) {
case RESOURCE_USAGE_NONE:
return "None";
case RESOURCE_USAGE_COPY_FROM:
return "Copy From";
case RESOURCE_USAGE_COPY_TO:
return "Copy To";
case RESOURCE_USAGE_RESOLVE_FROM:
return "Resolve From";
case RESOURCE_USAGE_RESOLVE_TO:
return "Resolve To";
case RESOURCE_USAGE_UNIFORM_BUFFER_READ:
return "Uniform Buffer Read";
case RESOURCE_USAGE_INDIRECT_BUFFER_READ:
return "Indirect Buffer Read";
case RESOURCE_USAGE_TEXTURE_BUFFER_READ:
return "Texture Buffer Read";
case RESOURCE_USAGE_TEXTURE_BUFFER_READ_WRITE:
return "Texture Buffer Read Write";
case RESOURCE_USAGE_STORAGE_BUFFER_READ:
return "Storage Buffer Read";
case RESOURCE_USAGE_STORAGE_BUFFER_READ_WRITE:
return "Storage Buffer Read Write";
case RESOURCE_USAGE_VERTEX_BUFFER_READ:
return "Vertex Buffer Read";
case RESOURCE_USAGE_INDEX_BUFFER_READ:
return "Index Buffer Read";
case RESOURCE_USAGE_TEXTURE_SAMPLE:
return "Texture Sample";
case RESOURCE_USAGE_STORAGE_IMAGE_READ:
return "Storage Image Read";
case RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE:
return "Storage Image Read Write";
case RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE:
return "Attachment Color Read Write";
case RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE:
return "Attachment Depth Stencil Read Write";
case RESOURCE_USAGE_GENERAL:
return "General";
default:
ERR_FAIL_V_MSG("Invalid", vformat("Invalid resource usage %d.", p_usage));
}
}
bool RenderingDeviceGraph::_is_write_usage(ResourceUsage p_usage) {
switch (p_usage) {
case RESOURCE_USAGE_COPY_FROM:
case RESOURCE_USAGE_RESOLVE_FROM:
case RESOURCE_USAGE_UNIFORM_BUFFER_READ:
case RESOURCE_USAGE_INDIRECT_BUFFER_READ:
case RESOURCE_USAGE_TEXTURE_BUFFER_READ:
case RESOURCE_USAGE_STORAGE_BUFFER_READ:
case RESOURCE_USAGE_VERTEX_BUFFER_READ:
case RESOURCE_USAGE_INDEX_BUFFER_READ:
case RESOURCE_USAGE_TEXTURE_SAMPLE:
case RESOURCE_USAGE_STORAGE_IMAGE_READ:
case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_SHADING_RATE_READ:
case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_DENSITY_MAP_READ:
case RESOURCE_USAGE_ATTACHMENT_RASTERIZATION_RATE_MAP_READ:
case RESOURCE_USAGE_ACCELERATION_STRUCTURE_READ:
return false;
case RESOURCE_USAGE_COPY_TO:
case RESOURCE_USAGE_RESOLVE_TO:
case RESOURCE_USAGE_TEXTURE_BUFFER_READ_WRITE:
case RESOURCE_USAGE_STORAGE_BUFFER_READ_WRITE:
case RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE:
case RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE:
case RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE:
case RESOURCE_USAGE_GENERAL:
case RESOURCE_USAGE_ACCELERATION_STRUCTURE_READ_WRITE:
return true;
default:
DEV_ASSERT(false && "Invalid resource tracker usage.");
return false;
}
}
RDD::TextureLayout RenderingDeviceGraph::_usage_to_image_layout(ResourceUsage p_usage) {
switch (p_usage) {
case RESOURCE_USAGE_COPY_FROM:
return RDD::TEXTURE_LAYOUT_COPY_SRC_OPTIMAL;
case RESOURCE_USAGE_COPY_TO:
return RDD::TEXTURE_LAYOUT_COPY_DST_OPTIMAL;
case RESOURCE_USAGE_RESOLVE_FROM:
return RDD::TEXTURE_LAYOUT_RESOLVE_SRC_OPTIMAL;
case RESOURCE_USAGE_RESOLVE_TO:
return RDD::TEXTURE_LAYOUT_RESOLVE_DST_OPTIMAL;
case RESOURCE_USAGE_TEXTURE_SAMPLE:
return RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
case RESOURCE_USAGE_STORAGE_IMAGE_READ:
case RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE:
return RDD::TEXTURE_LAYOUT_STORAGE_OPTIMAL;
case RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE:
return RDD::TEXTURE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
case RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE:
return RDD::TEXTURE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_SHADING_RATE_READ:
return RDD::TEXTURE_LAYOUT_FRAGMENT_SHADING_RATE_ATTACHMENT_OPTIMAL;
case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_DENSITY_MAP_READ:
return RDD::TEXTURE_LAYOUT_FRAGMENT_DENSITY_MAP_ATTACHMENT_OPTIMAL;
case RESOURCE_USAGE_ATTACHMENT_RASTERIZATION_RATE_MAP_READ:
// Rasterization rate map is not a real texture and it's readonly from shaders,
// so it doesn't need a texture layout
return RDD::TEXTURE_LAYOUT_UNDEFINED;
case RESOURCE_USAGE_GENERAL:
return RDD::TEXTURE_LAYOUT_GENERAL;
case RESOURCE_USAGE_NONE:
return RDD::TEXTURE_LAYOUT_UNDEFINED;
default:
DEV_ASSERT(false && "Invalid resource tracker usage or not an image usage.");
return RDD::TEXTURE_LAYOUT_UNDEFINED;
}
}
RDD::BarrierAccessBits RenderingDeviceGraph::_usage_to_access_bits(ResourceUsage p_usage) {
#if FORCE_FULL_ACCESS_BITS
return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_MEMORY_READ_BIT | RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT);
#else
switch (p_usage) {
case RESOURCE_USAGE_NONE:
return RDD::BarrierAccessBits(0);
case RESOURCE_USAGE_COPY_FROM:
return RDD::BARRIER_ACCESS_COPY_READ_BIT;
case RESOURCE_USAGE_COPY_TO:
return RDD::BARRIER_ACCESS_COPY_WRITE_BIT;
case RESOURCE_USAGE_RESOLVE_FROM:
return RDD::BARRIER_ACCESS_RESOLVE_READ_BIT;
case RESOURCE_USAGE_RESOLVE_TO:
return RDD::BARRIER_ACCESS_RESOLVE_WRITE_BIT;
case RESOURCE_USAGE_UNIFORM_BUFFER_READ:
return RDD::BARRIER_ACCESS_UNIFORM_READ_BIT;
case RESOURCE_USAGE_INDIRECT_BUFFER_READ:
return RDD::BARRIER_ACCESS_INDIRECT_COMMAND_READ_BIT;
case RESOURCE_USAGE_ACCELERATION_STRUCTURE_READ:
return RDD::BARRIER_ACCESS_ACCELERATION_STRUCTURE_READ_BIT;
case RESOURCE_USAGE_STORAGE_BUFFER_READ:
case RESOURCE_USAGE_STORAGE_IMAGE_READ:
case RESOURCE_USAGE_TEXTURE_BUFFER_READ:
case RESOURCE_USAGE_TEXTURE_SAMPLE:
return RDD::BARRIER_ACCESS_SHADER_READ_BIT;
case RESOURCE_USAGE_TEXTURE_BUFFER_READ_WRITE:
case RESOURCE_USAGE_STORAGE_BUFFER_READ_WRITE:
case RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE:
return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_SHADER_READ_BIT | RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
case RESOURCE_USAGE_VERTEX_BUFFER_READ:
return RDD::BARRIER_ACCESS_VERTEX_ATTRIBUTE_READ_BIT;
case RESOURCE_USAGE_INDEX_BUFFER_READ:
return RDD::BARRIER_ACCESS_INDEX_READ_BIT;
case RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE:
return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_READ_BIT | RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_WRITE_BIT);
case RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE:
return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | RDD::BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT);
case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_SHADING_RATE_READ:
return RDD::BARRIER_ACCESS_FRAGMENT_SHADING_RATE_ATTACHMENT_READ_BIT;
case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_DENSITY_MAP_READ:
return RDD::BARRIER_ACCESS_FRAGMENT_DENSITY_MAP_ATTACHMENT_READ_BIT;
case RESOURCE_USAGE_ATTACHMENT_RASTERIZATION_RATE_MAP_READ:
// Rasterization rate map is not a real texture and it's readonly from shaders,
// so it doesn't need barrier access attributes
return RDD::BarrierAccessBits(0);
case RESOURCE_USAGE_GENERAL:
return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_MEMORY_READ_BIT | RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT);
case RESOURCE_USAGE_ACCELERATION_STRUCTURE_READ_WRITE:
return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_ACCELERATION_STRUCTURE_READ_BIT | RDD::BARRIER_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT);
default:
DEV_ASSERT(false && "Invalid usage.");
return RDD::BarrierAccessBits(0);
}
#endif
}
bool RenderingDeviceGraph::_check_command_intersection(ResourceTracker *p_resource_tracker, int32_t p_previous_command_index, int32_t p_command_index) const {
if (p_resource_tracker->usage != RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE && p_resource_tracker->usage != RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE) {
// We don't check possible intersections for usages that aren't consecutive color or depth writes.
return true;
}
const uint32_t previous_command_data_offset = command_data_offsets[p_previous_command_index];
const uint32_t current_command_data_offset = command_data_offsets[p_command_index];
const RecordedDrawListCommand &previous_draw_list_command = *reinterpret_cast<const RecordedDrawListCommand *>(&command_data[previous_command_data_offset]);
const RecordedDrawListCommand ¤t_draw_list_command = *reinterpret_cast<const RecordedDrawListCommand *>(&command_data[current_command_data_offset]);
if (previous_draw_list_command.type != RecordedCommand::TYPE_DRAW_LIST || current_draw_list_command.type != RecordedCommand::TYPE_DRAW_LIST) {
// We don't check possible intersections if both commands aren't draw lists.
return true;
}
// We check if the region used by both draw lists have an intersection.
return previous_draw_list_command.region.intersects(current_draw_list_command.region);
}
bool RenderingDeviceGraph::_check_command_partial_coverage(ResourceTracker *p_resource_tracker, int32_t p_command_index) const {
if (p_resource_tracker->usage != RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE && p_resource_tracker->usage != RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE) {
// We don't check for partial coverage in usages that aren't attachment writes.
return false;
}
const uint32_t command_data_offset = command_data_offsets[p_command_index];
const RecordedDrawListCommand &draw_list_command = *reinterpret_cast<const RecordedDrawListCommand *>(&command_data[command_data_offset]);
if (draw_list_command.type != RecordedCommand::TYPE_DRAW_LIST) {
// We don't check for partial coverage on commands that aren't draw lists.
return false;
}
Rect2i texture_region(Point2i(0, 0), p_resource_tracker->texture_size);
return !draw_list_command.region.encloses(texture_region);
}
int32_t RenderingDeviceGraph::_add_to_command_list(int32_t p_command_index, int32_t p_list_index) {
DEV_ASSERT(p_command_index < int32_t(command_count));
DEV_ASSERT(p_list_index < int32_t(command_list_nodes.size()));
int32_t next_index = int32_t(command_list_nodes.size());
command_list_nodes.resize(next_index + 1);
RecordedCommandListNode &new_node = command_list_nodes[next_index];
new_node.command_index = p_command_index;
new_node.next_list_index = p_list_index;
return next_index;
}
void RenderingDeviceGraph::_add_adjacent_command(int32_t p_previous_command_index, int32_t p_command_index, RecordedCommand *r_command) {
const uint32_t previous_command_data_offset = command_data_offsets[p_previous_command_index];
RecordedCommand &previous_command = *reinterpret_cast<RecordedCommand *>(&command_data[previous_command_data_offset]);
previous_command.adjacent_command_list_index = _add_to_command_list(p_command_index, previous_command.adjacent_command_list_index);
previous_command.next_stages = previous_command.next_stages | r_command->self_stages;
r_command->previous_stages = r_command->previous_stages | previous_command.self_stages;
}
int32_t RenderingDeviceGraph::_add_to_slice_read_list(int32_t p_command_index, Rect2i p_subresources, int32_t p_list_index) {
DEV_ASSERT(p_command_index < int32_t(command_count));
DEV_ASSERT(p_list_index < int32_t(read_slice_list_nodes.size()));
int32_t next_index = int32_t(read_slice_list_nodes.size());
read_slice_list_nodes.resize(next_index + 1);
RecordedSliceListNode &new_node = read_slice_list_nodes[next_index];
new_node.command_index = p_command_index;
new_node.next_list_index = p_list_index;
new_node.subresources = p_subresources;
return next_index;
}
int32_t RenderingDeviceGraph::_add_to_write_list(int32_t p_command_index, Rect2i p_subresources, int32_t p_list_index, bool p_partial_coverage) {
DEV_ASSERT(p_command_index < int32_t(command_count));
DEV_ASSERT(p_list_index < int32_t(write_slice_list_nodes.size()));
int32_t next_index = int32_t(write_slice_list_nodes.size());
write_slice_list_nodes.resize(next_index + 1);
RecordedSliceListNode &new_node = write_slice_list_nodes[next_index];
new_node.command_index = p_command_index;
new_node.next_list_index = p_list_index;
new_node.subresources = p_subresources;
new_node.partial_coverage = p_partial_coverage;
return next_index;
}
// Ensures all commands are 8-byte aligned.
#define GRAPH_ALIGN(x) (((x) + 7u) & 0xFFFFFFF8u)
RenderingDeviceGraph::RecordedCommand *RenderingDeviceGraph::_allocate_command(uint32_t p_command_size, int32_t &r_command_index) {
uint32_t command_data_offset = command_data.size();
command_data_offset = GRAPH_ALIGN(command_data_offset);
command_data_offsets.push_back(command_data_offset);
command_data.resize(command_data_offset + p_command_size);
r_command_index = command_count++;
RecordedCommand *new_command = reinterpret_cast<RecordedCommand *>(&command_data[command_data_offset]);
*new_command = RecordedCommand();
return new_command;
}
RenderingDeviceGraph::DrawListInstruction *RenderingDeviceGraph::_allocate_draw_list_instruction(uint32_t p_instruction_size) {
uint32_t draw_list_data_offset = draw_instruction_list.data.size();
draw_list_data_offset = GRAPH_ALIGN(draw_list_data_offset);
draw_instruction_list.data.resize(draw_list_data_offset + p_instruction_size);
return reinterpret_cast<DrawListInstruction *>(&draw_instruction_list.data[draw_list_data_offset]);
}
RenderingDeviceGraph::ComputeListInstruction *RenderingDeviceGraph::_allocate_compute_list_instruction(uint32_t p_instruction_size) {
uint32_t compute_list_data_offset = compute_instruction_list.data.size();
compute_list_data_offset = GRAPH_ALIGN(compute_list_data_offset);
compute_instruction_list.data.resize(compute_list_data_offset + p_instruction_size);
return reinterpret_cast<ComputeListInstruction *>(&compute_instruction_list.data[compute_list_data_offset]);
}
void RenderingDeviceGraph::_check_discardable_attachment_dependency(ResourceTracker *p_resource_tracker, int32_t p_previous_command_index, int32_t p_command_index) {
if (!p_resource_tracker->is_discardable) {
return;
}
// Check if the command is a draw list that clears the attachment completely. If it is, we don't need to modify the previous draw list.
uint32_t command_offset = command_data_offsets[p_command_index];
RecordedDrawListCommand *draw_list_command = reinterpret_cast<RecordedDrawListCommand *>(&command_data[command_offset]);
if (draw_list_command->type == RecordedCommand::TYPE_DRAW_LIST) {
ResourceTracker **trackers = draw_list_command->trackers();
for (uint32_t i = 0; i < draw_list_command->trackers_count; i++) {
if (trackers[i] == p_resource_tracker && draw_list_command->load_ops()[i] == RDD::ATTACHMENT_LOAD_OP_CLEAR) {
return;
}
}
}
// Check if the previous command is a draw list.
uint32_t previous_command_offset = command_data_offsets[p_previous_command_index];
RecordedDrawListCommand *previous_draw_list_command = reinterpret_cast<RecordedDrawListCommand *>(&command_data[previous_command_offset]);
if (previous_draw_list_command->type != RecordedCommand::TYPE_DRAW_LIST) {
return;
}
// Search for the tracker inside the draw list command and modify the store operation accordingly.
ResourceTracker **trackers = previous_draw_list_command->trackers();
for (uint32_t i = 0; i < previous_draw_list_command->trackers_count; i++) {
if (trackers[i] == p_resource_tracker) {
previous_draw_list_command->store_ops()[i] = RDD::ATTACHMENT_STORE_OP_STORE;
return;
}
}
}
RenderingDeviceGraph::RaytracingListInstruction *RenderingDeviceGraph::_allocate_raytracing_list_instruction(uint32_t p_instruction_size) {
uint32_t raytracing_list_data_offset = raytracing_instruction_list.data.size();
raytracing_instruction_list.data.resize(raytracing_list_data_offset + p_instruction_size);
return reinterpret_cast<RaytracingListInstruction *>(&raytracing_instruction_list.data[raytracing_list_data_offset]);
}
void RenderingDeviceGraph::_add_command_to_graph(ResourceTracker **p_resource_trackers, ResourceUsage *p_resource_usages, uint32_t p_resource_count, int32_t p_command_index, RecordedCommand *r_command) {
// Assign the next stages derived from the stages the command requires first.
r_command->next_stages = r_command->self_stages;
if (command_label_index >= 0) {
// If a label is active, tag the command with the label.
r_command->label_index = command_label_index;
}
if (r_command->type == RecordedCommand::TYPE_CAPTURE_TIMESTAMP) {
// All previous commands starting from the previous timestamp should be adjacent to this command.
int32_t start_command_index = uint32_t(MAX(command_timestamp_index, 0));
for (int32_t i = start_command_index; i < p_command_index; i++) {
_add_adjacent_command(i, p_command_index, r_command);
}
// Make this command the new active timestamp command.
command_timestamp_index = p_command_index;
} else if (command_timestamp_index >= 0) {
// Timestamp command should be adjacent to this command.
_add_adjacent_command(command_timestamp_index, p_command_index, r_command);
}
if (command_synchronization_pending) {
// All previous commands should be adjacent to this command.
int32_t start_command_index = uint32_t(MAX(command_synchronization_index, 0));
for (int32_t i = start_command_index; i < p_command_index; i++) {
_add_adjacent_command(i, p_command_index, r_command);
}
command_synchronization_index = p_command_index;
command_synchronization_pending = false;
} else if (command_synchronization_index >= 0) {
// Synchronization command should be adjacent to this command.
_add_adjacent_command(command_synchronization_index, p_command_index, r_command);
}
for (uint32_t i = 0; i < p_resource_count; i++) {
ResourceTracker *resource_tracker = p_resource_trackers[i];
DEV_ASSERT(resource_tracker != nullptr);
resource_tracker->reset_if_outdated(tracking_frame);
resource_tracker->command_index = p_command_index;
resource_tracker->usage_index = i;
}
for (uint32_t i = 0; i < p_resource_count; i++) {
ResourceTracker *resource_tracker = p_resource_trackers[i];
const RDD::TextureSubresourceRange &subresources = resource_tracker->texture_subresources;
const Rect2i resource_tracker_rect(subresources.base_mipmap, subresources.base_layer, subresources.mipmap_count, subresources.layer_count);
Rect2i search_tracker_rect = resource_tracker_rect;
ResourceUsage new_resource_usage = p_resource_usages[i];
bool write_usage = _is_write_usage(new_resource_usage);
BitField<RDD::BarrierAccessBits> new_usage_access = _usage_to_access_bits(new_resource_usage);
bool is_resource_a_slice = resource_tracker->parent != nullptr;
if (is_resource_a_slice) {
// This resource depends on a parent resource.
resource_tracker->parent->reset_if_outdated(tracking_frame);
// Quit early if the parent is already in this command.
if (resource_tracker->parent->command_index == p_command_index) {
DEV_ASSERT(resource_tracker->parent->usage_index != UINT32_MAX);
ERR_FAIL_COND_MSG(p_resource_usages[resource_tracker->parent->usage_index] != new_resource_usage, "Using a full texture and its slices at the same time with different usages is not allowed.");
continue;
}
if (resource_tracker->texture_slice_command_index != p_command_index) {
// Indicate this slice has been used by this command.
resource_tracker->texture_slice_command_index = p_command_index;
}
if (resource_tracker->parent->usage == RESOURCE_USAGE_NONE) {
if (resource_tracker->parent->texture_driver_id.id != 0) {
// If the resource is a texture, we transition it entirely to the layout determined by the first slice that uses it.
_add_texture_barrier_to_command(resource_tracker->parent->texture_driver_id, RDD::BarrierAccessBits(0), new_usage_access, RDG::RESOURCE_USAGE_NONE, new_resource_usage, resource_tracker->parent->texture_subresources, command_normalization_barriers, r_command->normalization_barrier_index, r_command->normalization_barrier_count);
}
// If the parent hasn't been used yet, we assign the usage of the slice to the entire resource.
resource_tracker->parent->usage = new_resource_usage;
// Also assign the usage to the slice and consider it a write operation. Consider the parent's current usage access as its own.
resource_tracker->usage = new_resource_usage;
resource_tracker->usage_access = resource_tracker->parent->usage_access;
write_usage = true;
// Indicate the area that should be tracked is the entire resource.
const RDD::TextureSubresourceRange &parent_subresources = resource_tracker->parent->texture_subresources;
search_tracker_rect = Rect2i(parent_subresources.base_mipmap, parent_subresources.base_layer, parent_subresources.mipmap_count, parent_subresources.layer_count);
} else if (resource_tracker->in_parent_dirty_list) {
if (resource_tracker->parent->usage == new_resource_usage) {
// The slice will be transitioned to the resource of the parent and can be deleted from the dirty list.
ResourceTracker *previous_tracker = nullptr;
ResourceTracker *current_tracker = resource_tracker->parent->dirty_shared_list;
bool initialized_dirty_rect = false;
while (current_tracker != nullptr) {
current_tracker->reset_if_outdated(tracking_frame);
if (current_tracker == resource_tracker) {
current_tracker->in_parent_dirty_list = false;
if (previous_tracker != nullptr) {
previous_tracker->next_shared = current_tracker->next_shared;
} else {
resource_tracker->parent->dirty_shared_list = current_tracker->next_shared;
}
current_tracker = current_tracker->next_shared;
} else {
if (initialized_dirty_rect) {
resource_tracker->parent->texture_slice_or_dirty_rect = resource_tracker->parent->texture_slice_or_dirty_rect.merge(current_tracker->texture_slice_or_dirty_rect);
} else {
resource_tracker->parent->texture_slice_or_dirty_rect = current_tracker->texture_slice_or_dirty_rect;
initialized_dirty_rect = true;
}
previous_tracker = current_tracker;
current_tracker = current_tracker->next_shared;
}
}
}
} else {
if (resource_tracker->parent->dirty_shared_list != nullptr && resource_tracker->parent->texture_slice_or_dirty_rect.intersects(resource_tracker->texture_slice_or_dirty_rect)) {
// There's an intersection with the current dirty area of the parent and the slice. We must verify if the intersection is against a slice
// that was used in this command or not. Any slice we can find that wasn't used by this command must be reverted to the layout of the parent.
ResourceTracker *previous_tracker = nullptr;
ResourceTracker *current_tracker = resource_tracker->parent->dirty_shared_list;
bool initialized_dirty_rect = false;
while (current_tracker != nullptr) {
current_tracker->reset_if_outdated(tracking_frame);
if (current_tracker->texture_slice_or_dirty_rect.intersects(resource_tracker->texture_slice_or_dirty_rect)) {
if (current_tracker->command_frame == tracking_frame && current_tracker->texture_slice_command_index == p_command_index) {
ERR_FAIL_MSG("Texture slices that overlap can't be used in the same command.");
} else {
// Delete the slice from the dirty list and revert it to the usage of the parent.
if (current_tracker->texture_driver_id.id != 0) {
_add_texture_barrier_to_command(current_tracker->texture_driver_id, current_tracker->usage_access, new_usage_access, current_tracker->usage, resource_tracker->parent->usage, current_tracker->texture_subresources, command_normalization_barriers, r_command->normalization_barrier_index, r_command->normalization_barrier_count);
// Merge the area of the slice with the current tracking area of the command and indicate it's a write usage as well.
search_tracker_rect = search_tracker_rect.merge(current_tracker->texture_slice_or_dirty_rect);
write_usage = true;
}
current_tracker->in_parent_dirty_list = false;
if (previous_tracker != nullptr) {
previous_tracker->next_shared = current_tracker->next_shared;
} else {
resource_tracker->parent->dirty_shared_list = current_tracker->next_shared;
}
current_tracker = current_tracker->next_shared;
}
} else {
// Recalculate the dirty rect of the parent so the deleted slices are excluded.
if (initialized_dirty_rect) {
resource_tracker->parent->texture_slice_or_dirty_rect = resource_tracker->parent->texture_slice_or_dirty_rect.merge(current_tracker->texture_slice_or_dirty_rect);
} else {
resource_tracker->parent->texture_slice_or_dirty_rect = current_tracker->texture_slice_or_dirty_rect;
initialized_dirty_rect = true;
}
previous_tracker = current_tracker;
current_tracker = current_tracker->next_shared;
}
}
}
// If it wasn't in the list, assume the usage is the same as the parent. Consider the parent's current usage access as its own.
resource_tracker->usage = resource_tracker->parent->usage;
resource_tracker->usage_access = resource_tracker->parent->usage_access;
if (resource_tracker->usage != new_resource_usage) {
// Insert to the dirty list if the requested usage is different.
resource_tracker->next_shared = resource_tracker->parent->dirty_shared_list;
resource_tracker->parent->dirty_shared_list = resource_tracker;
resource_tracker->in_parent_dirty_list = true;
if (resource_tracker->parent->dirty_shared_list != nullptr) {
resource_tracker->parent->texture_slice_or_dirty_rect = resource_tracker->parent->texture_slice_or_dirty_rect.merge(resource_tracker->texture_slice_or_dirty_rect);
} else {
resource_tracker->parent->texture_slice_or_dirty_rect = resource_tracker->texture_slice_or_dirty_rect;
}
}
}
} else {
ResourceTracker *current_tracker = resource_tracker->dirty_shared_list;
if (current_tracker != nullptr) {
// Consider the usage as write if we must transition any of the slices.
write_usage = true;
}
while (current_tracker != nullptr) {
current_tracker->reset_if_outdated(tracking_frame);
if (current_tracker->texture_driver_id.id != 0) {
// Transition all slices to the layout of the parent resource.
_add_texture_barrier_to_command(current_tracker->texture_driver_id, current_tracker->usage_access, new_usage_access, current_tracker->usage, resource_tracker->usage, current_tracker->texture_subresources, command_normalization_barriers, r_command->normalization_barrier_index, r_command->normalization_barrier_count);
}
current_tracker->in_parent_dirty_list = false;
current_tracker = current_tracker->next_shared;
}
resource_tracker->dirty_shared_list = nullptr;
}
// Use the resource's parent tracker directly for all search operations.
bool resource_has_parent = resource_tracker->parent != nullptr;
ResourceTracker *search_tracker = resource_has_parent ? resource_tracker->parent : resource_tracker;
bool different_usage = resource_tracker->usage != new_resource_usage;
bool write_usage_after_write = (write_usage && search_tracker->write_command_or_list_index >= 0);
if (different_usage || write_usage_after_write) {
// A barrier must be pushed if the usage is different of it's a write usage and there was already a command that wrote to this resource previously.
if (resource_tracker->texture_driver_id.id != 0) {
if (resource_tracker->usage_access.is_empty()) {
// FIXME: If the tracker does not know the previous type of usage, assume the generic memory write one.
// Tracking access bits across texture slices can be tricky, so this failsafe can be removed once that's improved.
resource_tracker->usage_access = RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT;
}
_add_texture_barrier_to_command(resource_tracker->texture_driver_id, resource_tracker->usage_access, new_usage_access, resource_tracker->usage, new_resource_usage, resource_tracker->texture_subresources, command_transition_barriers, r_command->transition_barrier_index, r_command->transition_barrier_count);
} else if (resource_tracker->buffer_driver_id.id != 0) {
#if USE_BUFFER_BARRIERS
_add_buffer_barrier_to_command(resource_tracker->buffer_driver_id, resource_tracker->usage_access, new_usage_access, r_command->buffer_barrier_index, r_command->buffer_barrier_count);
#endif
// Memory barriers are pushed regardless of buffer barriers being used or not.
r_command->memory_barrier.src_access = r_command->memory_barrier.src_access | resource_tracker->usage_access;
r_command->memory_barrier.dst_access = r_command->memory_barrier.dst_access | new_usage_access;
} else if (resource_tracker->acceleration_structure_driver_id.id != 0) {
// Make sure the acceleration structure has been built before accessing it from raytracing shaders.
_add_acceleration_structure_barrier_to_command(resource_tracker->acceleration_structure_driver_id, resource_tracker->usage_access, new_usage_access, command_acceleration_structure_barriers, r_command->acceleration_structure_barrier_index, r_command->acceleration_structure_barrier_count);
r_command->memory_barrier.src_access = r_command->memory_barrier.src_access | resource_tracker->usage_access;
r_command->memory_barrier.dst_access = r_command->memory_barrier.dst_access | new_usage_access;
} else {
DEV_ASSERT(false && "Resource tracker does not contain a valid buffer or texture ID.");
}
}
// Always update the access of the tracker according to the latest usage.
resource_tracker->usage_access = new_usage_access;
// Always accumulate the stages of the tracker with the commands that use it.
search_tracker->current_frame_stages = search_tracker->current_frame_stages | r_command->self_stages;
if (!search_tracker->previous_frame_stages.is_empty()) {
// Add to the command the stages the tracker was used on in the previous frame.
r_command->previous_stages = r_command->previous_stages | search_tracker->previous_frame_stages;
search_tracker->previous_frame_stages.clear();
}
if (different_usage) {
// Even if the usage of the resource isn't a write usage explicitly, a different usage implies a transition and it should therefore be considered a write.
// In the case of buffers however, this is not exactly necessary if the driver does not consider different buffer usages as different states.
write_usage = write_usage || bool(resource_tracker->texture_driver_id) || driver_buffers_require_transitions;
resource_tracker->usage = new_resource_usage;
}
bool write_usage_has_partial_coverage = !different_usage && _check_command_partial_coverage(resource_tracker, p_command_index);
if (search_tracker->write_command_or_list_index >= 0) {
if (search_tracker->write_command_list_enabled) {
// Make this command adjacent to any commands that wrote to this resource and intersect with the slice if it applies.
// For buffers or textures that never use slices, this list will only be one element long at most.
int32_t previous_write_list_index = -1;
int32_t write_list_index = search_tracker->write_command_or_list_index;
while (write_list_index >= 0) {
const RecordedSliceListNode &write_list_node = write_slice_list_nodes[write_list_index];
if (!resource_has_parent || search_tracker_rect.intersects(write_list_node.subresources)) {
if (write_list_node.command_index == p_command_index) {
ERR_FAIL_COND_MSG(!resource_has_parent, "Command can't have itself as a dependency.");
} else if (!write_list_node.partial_coverage || _check_command_intersection(resource_tracker, write_list_node.command_index, p_command_index)) {
_check_discardable_attachment_dependency(search_tracker, write_list_node.command_index, p_command_index);
// Command is dependent on this command. Add this command to the adjacency list of the write command.
_add_adjacent_command(write_list_node.command_index, p_command_index, r_command);
if (resource_has_parent && write_usage && search_tracker_rect.encloses(write_list_node.subresources) && !write_usage_has_partial_coverage) {
// Eliminate redundant writes from the list.
if (previous_write_list_index >= 0) {
RecordedSliceListNode &previous_list_node = write_slice_list_nodes[previous_write_list_index];
previous_list_node.next_list_index = write_list_node.next_list_index;
} else {
search_tracker->write_command_or_list_index = write_list_node.next_list_index;
}
write_list_index = write_list_node.next_list_index;
continue;
}
}
}
previous_write_list_index = write_list_index;
write_list_index = write_list_node.next_list_index;
}
} else {
// The index is just the latest command index that wrote to the resource.
if (search_tracker->write_command_or_list_index == p_command_index) {
ERR_FAIL_MSG("Command can't have itself as a dependency.");
} else {
_check_discardable_attachment_dependency(search_tracker, search_tracker->write_command_or_list_index, p_command_index);
_add_adjacent_command(search_tracker->write_command_or_list_index, p_command_index, r_command);
}
}
}
if (write_usage) {
bool use_write_list = resource_has_parent || write_usage_has_partial_coverage;
if (use_write_list) {
if (!search_tracker->write_command_list_enabled && search_tracker->write_command_or_list_index >= 0) {
// Write command list was not being used but there was a write command recorded. Add a new node with the entire parent resource's subresources and the recorded command index to the list.
const RDD::TextureSubresourceRange &tracker_subresources = search_tracker->texture_subresources;
Rect2i tracker_rect(tracker_subresources.base_mipmap, tracker_subresources.base_layer, tracker_subresources.mipmap_count, tracker_subresources.layer_count);
search_tracker->write_command_or_list_index = _add_to_write_list(search_tracker->write_command_or_list_index, tracker_rect, -1, false);
}
search_tracker->write_command_or_list_index = _add_to_write_list(p_command_index, search_tracker_rect, search_tracker->write_command_or_list_index, write_usage_has_partial_coverage);
search_tracker->write_command_list_enabled = true;
} else {
search_tracker->write_command_or_list_index = p_command_index;
search_tracker->write_command_list_enabled = false;
}
// We add this command to the adjacency list of all commands that were reading from the entire resource.
int32_t read_full_command_list_index = search_tracker->read_full_command_list_index;
while (read_full_command_list_index >= 0) {
int32_t read_full_command_index = command_list_nodes[read_full_command_list_index].command_index;
int32_t read_full_next_index = command_list_nodes[read_full_command_list_index].next_list_index;
if (read_full_command_index == p_command_index) {
if (!resource_has_parent) {
// Only slices are allowed to be in different usages in the same command as they are guaranteed to have no overlap in the same command.
ERR_FAIL_MSG("Command can't have itself as a dependency.");
}
} else {
// Add this command to the adjacency list of each command that was reading this resource.
_add_adjacent_command(read_full_command_index, p_command_index, r_command);
}
read_full_command_list_index = read_full_next_index;
}
if (!use_write_list) {
// Clear the full list if this resource is not a slice.
search_tracker->read_full_command_list_index = -1;
}
// We add this command to the adjacency list of all commands that were reading from resource slices.
int32_t previous_slice_command_list_index = -1;
int32_t read_slice_command_list_index = search_tracker->read_slice_command_list_index;
while (read_slice_command_list_index >= 0) {
const RecordedSliceListNode &read_list_node = read_slice_list_nodes[read_slice_command_list_index];
if (!use_write_list || search_tracker_rect.encloses(read_list_node.subresources)) {
if (previous_slice_command_list_index >= 0) {
// Erase this element and connect the previous one to the next element.
read_slice_list_nodes[previous_slice_command_list_index].next_list_index = read_list_node.next_list_index;
} else {
// Erase this element from the head of the list.
DEV_ASSERT(search_tracker->read_slice_command_list_index == read_slice_command_list_index);
search_tracker->read_slice_command_list_index = read_list_node.next_list_index;
}
// Advance to the next element.
read_slice_command_list_index = read_list_node.next_list_index;
} else {
previous_slice_command_list_index = read_slice_command_list_index;
read_slice_command_list_index = read_list_node.next_list_index;
}
if (!resource_has_parent || search_tracker_rect.intersects(read_list_node.subresources)) {
// Add this command to the adjacency list of each command that was reading this resource.
// We only add the dependency if there's an intersection between slices or this resource isn't a slice.
_add_adjacent_command(read_list_node.command_index, p_command_index, r_command);
}
}
} else if (resource_has_parent) {
// We add a read dependency to the tracker to indicate this command reads from the resource slice.
search_tracker->read_slice_command_list_index = _add_to_slice_read_list(p_command_index, resource_tracker_rect, search_tracker->read_slice_command_list_index);
} else {
// We add a read dependency to the tracker to indicate this command reads from the entire resource.
search_tracker->read_full_command_list_index = _add_to_command_list(p_command_index, search_tracker->read_full_command_list_index);
}
}
}
void RenderingDeviceGraph::_add_texture_barrier_to_command(RDD::TextureID p_texture_id, BitField<RDD::BarrierAccessBits> p_src_access, BitField<RDD::BarrierAccessBits> p_dst_access, ResourceUsage p_prev_usage, ResourceUsage p_next_usage, RDD::TextureSubresourceRange p_subresources, LocalVector<RDD::TextureBarrier> &r_barrier_vector, int32_t &r_barrier_index, int32_t &r_barrier_count) {
if (!driver_honors_barriers) {
return;
}
if (r_barrier_index < 0) {
r_barrier_index = r_barrier_vector.size();
}
RDD::TextureBarrier texture_barrier;
texture_barrier.texture = p_texture_id;
texture_barrier.src_access = p_src_access;
texture_barrier.dst_access = p_dst_access;
texture_barrier.prev_layout = _usage_to_image_layout(p_prev_usage);
texture_barrier.next_layout = _usage_to_image_layout(p_next_usage);
texture_barrier.subresources = p_subresources;
r_barrier_vector.push_back(texture_barrier);
r_barrier_count++;
}
#if USE_BUFFER_BARRIERS
void RenderingDeviceGraph::_add_buffer_barrier_to_command(RDD::BufferID p_buffer_id, BitField<RDD::BarrierAccessBits> p_src_access, BitField<RDD::BarrierAccessBits> p_dst_access, int32_t &r_barrier_index, int32_t &r_barrier_count) {
if (!driver_honors_barriers) {
return;
}
if (r_barrier_index < 0) {
r_barrier_index = command_buffer_barriers.size();
}
RDD::BufferBarrier buffer_barrier;
buffer_barrier.buffer = p_buffer_id;
buffer_barrier.src_access = p_src_access;
buffer_barrier.dst_access = p_dst_access;
buffer_barrier.offset = 0;
buffer_barrier.size = RDD::BUFFER_WHOLE_SIZE;
command_buffer_barriers.push_back(buffer_barrier);
r_barrier_count++;
}
#endif
void RenderingDeviceGraph::_add_acceleration_structure_barrier_to_command(RDD::AccelerationStructureID p_acceleration_structure_id, BitField<RDD::BarrierAccessBits> p_src_access, BitField<RDD::BarrierAccessBits> p_dst_access, LocalVector<RDD::AccelerationStructureBarrier> &r_barrier_vector, int32_t &r_barrier_index, int32_t &r_barrier_count) {
if (!driver_honors_barriers) {
return;
}
if (r_barrier_index < 0) {
r_barrier_index = r_barrier_vector.size();
}
RDD::AccelerationStructureBarrier accel_barrier;
accel_barrier.acceleration_structure = p_acceleration_structure_id;
accel_barrier.src_access = p_src_access;
accel_barrier.dst_access = p_dst_access;
accel_barrier.offset = 0;
accel_barrier.size = RDD::BUFFER_WHOLE_SIZE;
r_barrier_vector.push_back(accel_barrier);
r_barrier_count++;
}
void RenderingDeviceGraph::_run_raytracing_list_command(RDD::CommandBufferID p_command_buffer, const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {
uint32_t instruction_data_cursor = 0;
while (instruction_data_cursor < p_instruction_data_size) {
DEV_ASSERT((instruction_data_cursor + sizeof(RaytracingListInstruction)) <= p_instruction_data_size);
const RaytracingListInstruction *instruction = reinterpret_cast<const RaytracingListInstruction *>(&p_instruction_data[instruction_data_cursor]);
switch (instruction->type) {
case RaytracingListInstruction::TYPE_BIND_PIPELINE: {
const RaytracingListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const RaytracingListBindPipelineInstruction *>(instruction);
driver->command_bind_raytracing_pipeline(p_command_buffer, bind_pipeline_instruction->pipeline);
instruction_data_cursor += sizeof(RaytracingListBindPipelineInstruction);
} break;
case RaytracingListInstruction::TYPE_BIND_UNIFORM_SET: {
const RaytracingListBindUniformSetInstruction *bind_uniform_set_instruction = reinterpret_cast<const RaytracingListBindUniformSetInstruction *>(instruction);
driver->command_bind_raytracing_uniform_set(p_command_buffer, bind_uniform_set_instruction->uniform_set, bind_uniform_set_instruction->shader, bind_uniform_set_instruction->set_index);
instruction_data_cursor += sizeof(RaytracingListBindUniformSetInstruction);
} break;
case RaytracingListInstruction::TYPE_TRACE_RAYS: {
const RaytracingListTraceRaysInstruction *trace_rays_instruction = reinterpret_cast<const RaytracingListTraceRaysInstruction *>(instruction);
driver->command_trace_rays(p_command_buffer, trace_rays_instruction->raygen_sbt, trace_rays_instruction->miss_sbt, trace_rays_instruction->hit_sbt, trace_rays_instruction->width, trace_rays_instruction->height, trace_rays_instruction->depth);
instruction_data_cursor += sizeof(RaytracingListTraceRaysInstruction);
} break;
case RaytracingListInstruction::TYPE_SET_PUSH_CONSTANT: {
const RaytracingListSetPushConstantInstruction *set_push_constant_instruction = reinterpret_cast<const RaytracingListSetPushConstantInstruction *>(instruction);
const VectorView push_constant_data_view(reinterpret_cast<const uint32_t *>(set_push_constant_instruction->data()), set_push_constant_instruction->size / sizeof(uint32_t));
driver->command_bind_push_constants(p_command_buffer, set_push_constant_instruction->shader, 0, push_constant_data_view);
instruction_data_cursor += sizeof(RaytracingListSetPushConstantInstruction);
instruction_data_cursor += set_push_constant_instruction->size;
} break;
case RaytracingListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE: {
const RaytracingListUniformSetPrepareForUseInstruction *uniform_set_prepare_for_use_instruction = reinterpret_cast<const RaytracingListUniformSetPrepareForUseInstruction *>(instruction);
driver->command_uniform_set_prepare_for_use(p_command_buffer, uniform_set_prepare_for_use_instruction->uniform_set, uniform_set_prepare_for_use_instruction->shader, uniform_set_prepare_for_use_instruction->set_index);
instruction_data_cursor += sizeof(RaytracingListUniformSetPrepareForUseInstruction);
} break;
default:
DEV_ASSERT(false && "Unknown raytracing list instruction type.");
return;
}
}
}
void RenderingDeviceGraph::_run_compute_list_command(RDD::CommandBufferID p_command_buffer, const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {
uint32_t instruction_data_cursor = 0;
while (instruction_data_cursor < p_instruction_data_size) {
DEV_ASSERT((instruction_data_cursor + sizeof(ComputeListInstruction)) <= p_instruction_data_size);
const ComputeListInstruction *instruction = reinterpret_cast<const ComputeListInstruction *>(&p_instruction_data[instruction_data_cursor]);
switch (instruction->type) {
case ComputeListInstruction::TYPE_BIND_PIPELINE: {
const ComputeListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const ComputeListBindPipelineInstruction *>(instruction);
driver->command_bind_compute_pipeline(p_command_buffer, bind_pipeline_instruction->pipeline);
instruction_data_cursor += sizeof(ComputeListBindPipelineInstruction);
} break;
case ComputeListInstruction::TYPE_BIND_UNIFORM_SETS: {
const ComputeListBindUniformSetsInstruction *bind_uniform_sets_instruction = reinterpret_cast<const ComputeListBindUniformSetsInstruction *>(instruction);
driver->command_bind_compute_uniform_sets(p_command_buffer, VectorView<RDD::UniformSetID>(bind_uniform_sets_instruction->uniform_set_ids(), bind_uniform_sets_instruction->set_count), bind_uniform_sets_instruction->shader, bind_uniform_sets_instruction->first_set_index, bind_uniform_sets_instruction->set_count, bind_uniform_sets_instruction->dynamic_offsets_mask);
instruction_data_cursor += sizeof(ComputeListBindUniformSetsInstruction) + sizeof(RDD::UniformSetID) * bind_uniform_sets_instruction->set_count;
} break;
case ComputeListInstruction::TYPE_DISPATCH: {
const ComputeListDispatchInstruction *dispatch_instruction = reinterpret_cast<const ComputeListDispatchInstruction *>(instruction);
driver->command_compute_dispatch(p_command_buffer, dispatch_instruction->x_groups, dispatch_instruction->y_groups, dispatch_instruction->z_groups);
instruction_data_cursor += sizeof(ComputeListDispatchInstruction);
} break;
case ComputeListInstruction::TYPE_DISPATCH_INDIRECT: {
const ComputeListDispatchIndirectInstruction *dispatch_indirect_instruction = reinterpret_cast<const ComputeListDispatchIndirectInstruction *>(instruction);
driver->command_compute_dispatch_indirect(p_command_buffer, dispatch_indirect_instruction->buffer, dispatch_indirect_instruction->offset);
instruction_data_cursor += sizeof(ComputeListDispatchIndirectInstruction);
} break;
case ComputeListInstruction::TYPE_SET_PUSH_CONSTANT: {
const ComputeListSetPushConstantInstruction *set_push_constant_instruction = reinterpret_cast<const ComputeListSetPushConstantInstruction *>(instruction);
const VectorView push_constant_data_view(reinterpret_cast<const uint32_t *>(set_push_constant_instruction->data()), set_push_constant_instruction->size / sizeof(uint32_t));
driver->command_bind_push_constants(p_command_buffer, set_push_constant_instruction->shader, 0, push_constant_data_view);
instruction_data_cursor += sizeof(ComputeListSetPushConstantInstruction);
instruction_data_cursor += set_push_constant_instruction->size;
} break;
case ComputeListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE: {
const ComputeListUniformSetPrepareForUseInstruction *uniform_set_prepare_for_use_instruction = reinterpret_cast<const ComputeListUniformSetPrepareForUseInstruction *>(instruction);
driver->command_uniform_set_prepare_for_use(p_command_buffer, uniform_set_prepare_for_use_instruction->uniform_set, uniform_set_prepare_for_use_instruction->shader, uniform_set_prepare_for_use_instruction->set_index);
instruction_data_cursor += sizeof(ComputeListUniformSetPrepareForUseInstruction);
} break;
default:
DEV_ASSERT(false && "Unknown compute list instruction type.");
return;
}
instruction_data_cursor = GRAPH_ALIGN(instruction_data_cursor);
}
}
void RenderingDeviceGraph::_get_draw_list_render_pass_and_framebuffer(const RecordedDrawListCommand *p_draw_list_command, RDD::RenderPassID &r_render_pass, RDD::FramebufferID &r_framebuffer) {
DEV_ASSERT(p_draw_list_command->trackers_count <= 21 && "Max number of attachments that can be encoded into the key.");
// Build a unique key from the load and store ops for each attachment.
const RDD::AttachmentLoadOp *load_ops = p_draw_list_command->load_ops();
const RDD::AttachmentStoreOp *store_ops = p_draw_list_command->store_ops();
uint64_t key = 0;
for (uint32_t i = 0; i < p_draw_list_command->trackers_count; i++) {
key |= uint64_t(load_ops[i]) << (i * 3);
key |= uint64_t(store_ops[i]) << (i * 3 + 2);
}
// Check the storage map if the render pass and the framebuffer needs to be created.
FramebufferCache *framebuffer_cache = p_draw_list_command->framebuffer_cache;
HashMap<uint64_t, FramebufferStorage>::Iterator it = framebuffer_cache->storage_map.find(key);
if (it == framebuffer_cache->storage_map.end()) {
FramebufferStorage storage;
VectorView<RDD::AttachmentLoadOp> load_ops_view(load_ops, p_draw_list_command->trackers_count);
VectorView<RDD::AttachmentStoreOp> store_ops_view(store_ops, p_draw_list_command->trackers_count);
storage.render_pass = render_pass_creation_function(driver, load_ops_view, store_ops_view, framebuffer_cache->render_pass_creation_user_data);
ERR_FAIL_COND(!storage.render_pass);
storage.framebuffer = driver->framebuffer_create(storage.render_pass, framebuffer_cache->textures, framebuffer_cache->width, framebuffer_cache->height);
ERR_FAIL_COND(!storage.framebuffer);
it = framebuffer_cache->storage_map.insert(key, storage);
}
r_render_pass = it->value.render_pass;
r_framebuffer = it->value.framebuffer;
}
#if PRINT_DRAW_LIST_STATS
static uint32_t draw_list_total_size = 0;
#endif
void RenderingDeviceGraph::_run_draw_list_command(RDD::CommandBufferID p_command_buffer, const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {
#if PRINT_DRAW_LIST_STATS
draw_list_total_size += p_instruction_data_size;
#endif
uint32_t instruction_data_cursor = 0;
while (instruction_data_cursor < p_instruction_data_size) {
DEV_ASSERT((instruction_data_cursor + sizeof(DrawListInstruction)) <= p_instruction_data_size);
const DrawListInstruction *instruction = reinterpret_cast<const DrawListInstruction *>(&p_instruction_data[instruction_data_cursor]);
switch (instruction->type) {
case DrawListInstruction::TYPE_BIND_INDEX_BUFFER: {
const DrawListBindIndexBufferInstruction *bind_index_buffer_instruction = reinterpret_cast<const DrawListBindIndexBufferInstruction *>(instruction);
driver->command_render_bind_index_buffer(p_command_buffer, bind_index_buffer_instruction->buffer, bind_index_buffer_instruction->format, bind_index_buffer_instruction->offset);
instruction_data_cursor += sizeof(DrawListBindIndexBufferInstruction);
} break;
case DrawListInstruction::TYPE_BIND_PIPELINE: {
const DrawListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const DrawListBindPipelineInstruction *>(instruction);
driver->command_bind_render_pipeline(p_command_buffer, bind_pipeline_instruction->pipeline);
instruction_data_cursor += sizeof(DrawListBindPipelineInstruction);
} break;
case DrawListInstruction::TYPE_BIND_UNIFORM_SETS: {
const DrawListBindUniformSetsInstruction *bind_uniform_sets_instruction = reinterpret_cast<const DrawListBindUniformSetsInstruction *>(instruction);
driver->command_bind_render_uniform_sets(p_command_buffer, VectorView<RDD::UniformSetID>(bind_uniform_sets_instruction->uniform_set_ids(), bind_uniform_sets_instruction->set_count), bind_uniform_sets_instruction->shader, bind_uniform_sets_instruction->first_set_index, bind_uniform_sets_instruction->set_count, bind_uniform_sets_instruction->dynamic_offsets_mask);
instruction_data_cursor += sizeof(DrawListBindUniformSetsInstruction) + sizeof(RDD::UniformSetID) * bind_uniform_sets_instruction->set_count;
} break;
case DrawListInstruction::TYPE_BIND_VERTEX_BUFFERS: {
const DrawListBindVertexBuffersInstruction *bind_vertex_buffers_instruction = reinterpret_cast<const DrawListBindVertexBuffersInstruction *>(instruction);
driver->command_render_bind_vertex_buffers(p_command_buffer, bind_vertex_buffers_instruction->vertex_buffers_count, bind_vertex_buffers_instruction->vertex_buffers(), bind_vertex_buffers_instruction->vertex_buffer_offsets(), bind_vertex_buffers_instruction->dynamic_offsets_mask);
instruction_data_cursor += sizeof(DrawListBindVertexBuffersInstruction);
instruction_data_cursor += sizeof(RDD::BufferID) * bind_vertex_buffers_instruction->vertex_buffers_count;
instruction_data_cursor += sizeof(uint64_t) * bind_vertex_buffers_instruction->vertex_buffers_count;
} break;
case DrawListInstruction::TYPE_CLEAR_ATTACHMENTS: {
const DrawListClearAttachmentsInstruction *clear_attachments_instruction = reinterpret_cast<const DrawListClearAttachmentsInstruction *>(instruction);
const VectorView attachments_clear_view(clear_attachments_instruction->attachments_clear(), clear_attachments_instruction->attachments_clear_count);
const VectorView attachments_clear_rect_view(clear_attachments_instruction->attachments_clear_rect(), clear_attachments_instruction->attachments_clear_rect_count);
driver->command_render_clear_attachments(p_command_buffer, attachments_clear_view, attachments_clear_rect_view);
instruction_data_cursor += sizeof(DrawListClearAttachmentsInstruction);
instruction_data_cursor += sizeof(RDD::AttachmentClear) * clear_attachments_instruction->attachments_clear_count;
instruction_data_cursor += sizeof(Rect2i) * clear_attachments_instruction->attachments_clear_rect_count;
} break;
case DrawListInstruction::TYPE_DRAW: {
const DrawListDrawInstruction *draw_instruction = reinterpret_cast<const DrawListDrawInstruction *>(instruction);
driver->command_render_draw(p_command_buffer, draw_instruction->vertex_count, draw_instruction->instance_count, 0, 0);
instruction_data_cursor += sizeof(DrawListDrawInstruction);
} break;
case DrawListInstruction::TYPE_DRAW_INDEXED: {
const DrawListDrawIndexedInstruction *draw_indexed_instruction = reinterpret_cast<const DrawListDrawIndexedInstruction *>(instruction);
driver->command_render_draw_indexed(p_command_buffer, draw_indexed_instruction->index_count, draw_indexed_instruction->instance_count, draw_indexed_instruction->first_index, 0, 0);
instruction_data_cursor += sizeof(DrawListDrawIndexedInstruction);
} break;
case DrawListInstruction::TYPE_DRAW_INDIRECT: {
const DrawListDrawIndirectInstruction *draw_indirect_instruction = reinterpret_cast<const DrawListDrawIndirectInstruction *>(instruction);