Debian Codegraph @

servers/rendering/rendering_device_graph.h

raw symbols SCIP
1 /**************************************************************************/
2 /* rendering_device_graph.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 #include "core/object/worker_thread_pool.h"
34 #include "rendering_device_commons.h"
35 #include "rendering_device_driver.h"
36
37 // Buffer barriers have not shown any significant improvement or shown to be
38 // even detrimental to performance. However, there are currently some known
39 // cases where using them can solve problems that using singular memory
40 // barriers does not, probably due to driver issues (see comment on PR #84976
41 // https://github.com/godotengine/godot/pull/84976#issuecomment-1878566830).
42
43 #define USE_BUFFER_BARRIERS 1
44
45 class RenderingDeviceGraph {
46 public:
47 struct ComputeListInstruction {
48 enum Type {
49 TYPE_NONE,
50 TYPE_BIND_PIPELINE,
51 TYPE_BIND_UNIFORM_SETS,
52 TYPE_DISPATCH,
53 TYPE_DISPATCH_INDIRECT,
54 TYPE_SET_PUSH_CONSTANT,
55 TYPE_UNIFORM_SET_PREPARE_FOR_USE
56 };
57
58 Type type = TYPE_NONE;
59 };
60
61 struct DrawListInstruction {
62 enum Type {
63 TYPE_NONE,
64 TYPE_BIND_INDEX_BUFFER,
65 TYPE_BIND_PIPELINE,
66 TYPE_BIND_UNIFORM_SETS,
67 TYPE_BIND_VERTEX_BUFFERS,
68 TYPE_CLEAR_ATTACHMENTS,
69 TYPE_DRAW,
70 TYPE_DRAW_INDEXED,
71 TYPE_DRAW_INDIRECT,
72 TYPE_DRAW_INDEXED_INDIRECT,
73 TYPE_EXECUTE_COMMANDS,
74 TYPE_NEXT_SUBPASS,
75 TYPE_SET_BLEND_CONSTANTS,
76 TYPE_SET_LINE_WIDTH,
77 TYPE_SET_PUSH_CONSTANT,
78 TYPE_SET_SCISSOR,
79 TYPE_SET_VIEWPORT,
80 TYPE_UNIFORM_SET_PREPARE_FOR_USE
81 };
82
83 Type type = TYPE_NONE;
84 };
85
86 struct RecordedCommand {
87 enum Type {
88 TYPE_NONE,
89 TYPE_BUFFER_CLEAR,
90 TYPE_BUFFER_COPY,
91 TYPE_BUFFER_GET_DATA,
92 TYPE_BUFFER_UPDATE,
93 TYPE_COMPUTE_LIST,
94 TYPE_DRAW_LIST,
95 TYPE_TEXTURE_CLEAR_COLOR,
96 TYPE_TEXTURE_CLEAR_DEPTH_STENCIL,
97 TYPE_TEXTURE_COPY,
98 TYPE_TEXTURE_GET_DATA,
99 TYPE_TEXTURE_RESOLVE,
100 TYPE_TEXTURE_UPDATE,
101 TYPE_CAPTURE_TIMESTAMP,
102 TYPE_DRIVER_CALLBACK,
103 TYPE_MAX
104 };
105
106 Type type = TYPE_NONE;
107 int32_t adjacent_command_list_index = -1;
108 RDD::MemoryAccessBarrier memory_barrier;
109 int32_t normalization_barrier_index = -1;
110 int normalization_barrier_count = 0;
111 int32_t transition_barrier_index = -1;
112 int32_t transition_barrier_count = 0;
113 #if USE_BUFFER_BARRIERS
114 int32_t buffer_barrier_index = -1;
115 int32_t buffer_barrier_count = 0;
116 #endif
117 int32_t label_index = -1;
118 BitField<RDD::PipelineStageBits> previous_stages = {};
119 BitField<RDD::PipelineStageBits> next_stages = {};
120 BitField<RDD::PipelineStageBits> self_stages = {};
121 };
122
123 struct RecordedBufferCopy {
124 RDD::BufferID source;
125 RDD::BufferCopyRegion region;
126 };
127
128 struct RecordedBufferToTextureCopy {
129 RDD::BufferID from_buffer;
130 RDD::BufferTextureCopyRegion region;
131 };
132
133 enum ResourceUsage {
134 RESOURCE_USAGE_NONE,
135 RESOURCE_USAGE_COPY_FROM,
136 RESOURCE_USAGE_COPY_TO,
137 RESOURCE_USAGE_RESOLVE_FROM,
138 RESOURCE_USAGE_RESOLVE_TO,
139 RESOURCE_USAGE_UNIFORM_BUFFER_READ,
140 RESOURCE_USAGE_INDIRECT_BUFFER_READ,
141 RESOURCE_USAGE_TEXTURE_BUFFER_READ,
142 RESOURCE_USAGE_TEXTURE_BUFFER_READ_WRITE,
143 RESOURCE_USAGE_STORAGE_BUFFER_READ,
144 RESOURCE_USAGE_STORAGE_BUFFER_READ_WRITE,
145 RESOURCE_USAGE_VERTEX_BUFFER_READ,
146 RESOURCE_USAGE_INDEX_BUFFER_READ,
147 RESOURCE_USAGE_TEXTURE_SAMPLE,
148 RESOURCE_USAGE_STORAGE_IMAGE_READ,
149 RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE,
150 RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE,
151 RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE,
152 RESOURCE_USAGE_ATTACHMENT_FRAGMENT_SHADING_RATE_READ,
153 RESOURCE_USAGE_ATTACHMENT_FRAGMENT_DENSITY_MAP_READ,
154 RESOURCE_USAGE_GENERAL,
155 RESOURCE_USAGE_MAX
156 };
157
158 struct ResourceTracker {
159 uint32_t reference_count = 0;
160 int64_t command_frame = -1;
161 BitField<RDD::PipelineStageBits> previous_frame_stages = {};
162 BitField<RDD::PipelineStageBits> current_frame_stages = {};
163 int32_t read_full_command_list_index = -1;
164 int32_t read_slice_command_list_index = -1;
165 int32_t write_command_or_list_index = -1;
166 int32_t draw_list_index = -1;
167 ResourceUsage draw_list_usage = RESOURCE_USAGE_NONE;
168 int32_t compute_list_index = -1;
169 ResourceUsage compute_list_usage = RESOURCE_USAGE_NONE;
170 ResourceUsage usage = RESOURCE_USAGE_NONE;
171 BitField<RDD::BarrierAccessBits> usage_access = {};
172 RDD::BufferID buffer_driver_id;
173 RDD::TextureID texture_driver_id;
174 RDD::TextureSubresourceRange texture_subresources;
175 Size2i texture_size;
176 uint32_t texture_usage = 0;
177 int32_t texture_slice_command_index = -1;
178 ResourceTracker *parent = nullptr;
179 ResourceTracker *dirty_shared_list = nullptr;
180 ResourceTracker *next_shared = nullptr;
181 Rect2i texture_slice_or_dirty_rect;
182 bool in_parent_dirty_list = false;
183 bool write_command_list_enabled = false;
184 bool is_discardable = false;
185
186 _FORCE_INLINE_ void reset_if_outdated(int64_t new_command_frame) {
187 if (new_command_frame != command_frame) {
188 command_frame = new_command_frame;
189 previous_frame_stages = current_frame_stages;
190 current_frame_stages.clear();
191 read_full_command_list_index = -1;
192 read_slice_command_list_index = -1;
193 write_command_or_list_index = -1;
194 draw_list_index = -1;
195 compute_list_index = -1;
196 texture_slice_command_index = -1;
197 write_command_list_enabled = false;
198 }
199 }
200 };
201
202 typedef RDD::RenderPassID (*RenderPassCreationFunction)(RenderingDeviceDriver *p_driver, VectorView<RDD::AttachmentLoadOp> p_load_ops, VectorView<RDD::AttachmentStoreOp> p_store_ops, void *p_user_data);
203
204 struct FramebufferStorage {
205 RDD::FramebufferID framebuffer;
206 RDD::RenderPassID render_pass;
207 };
208
209 struct FramebufferCache {
210 uint32_t width = 0;
211 uint32_t height = 0;
212 LocalVector<RDD::TextureID> textures;
213 LocalVector<ResourceTracker *> trackers;
214 HashMap<uint64_t, FramebufferStorage> storage_map;
215 void *render_pass_creation_user_data = nullptr;
216 };
217
218 struct CommandBufferPool {
219 // Provided by RenderingDevice.
220 RDD::CommandPoolID pool;
221
222 // Created internally by RenderingDeviceGraph.
223 LocalVector<RDD::CommandBufferID> buffers;
224 LocalVector<RDD::SemaphoreID> semaphores;
225 uint32_t buffers_used = 0;
226 };
227
228 struct WorkaroundsState {
229 bool draw_list_found = false;
230 };
231
232 enum AttachmentOperation {
233 // Loads or ignores if the attachment is discardable.
234 ATTACHMENT_OPERATION_DEFAULT,
235 // Clear the attachment to a value.
236 ATTACHMENT_OPERATION_CLEAR,
237 // Ignore any contents from the attachment.
238 ATTACHMENT_OPERATION_IGNORE,
239 };
240
241 private:
242 struct InstructionList {
243 LocalVector<uint8_t> data;
244 LocalVector<ResourceTracker *> command_trackers;
245 LocalVector<ResourceUsage> command_tracker_usages;
246 BitField<RDD::PipelineStageBits> stages = {};
247 int32_t index = 0;
248
249 void clear() {
250 data.clear();
251 command_trackers.clear();
252 command_tracker_usages.clear();
253 stages.clear();
254 }
255 };
256
257 struct ComputeInstructionList : InstructionList {
258 #if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
259 uint32_t breadcrumb;
260 #endif
261 };
262
263 struct DrawInstructionList : InstructionList {
264 FramebufferCache *framebuffer_cache = nullptr;
265 RDD::RenderPassID render_pass;
266 RDD::FramebufferID framebuffer;
267 Rect2i region;
268 LocalVector<AttachmentOperation> attachment_operations;
269 LocalVector<RDD::RenderPassClearValue> attachment_clear_values;
270
271 #if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
272 uint32_t breadcrumb;
273 #endif
274 bool split_cmd_buffer = false;
275 };
276
277 struct RecordedCommandSort {
278 uint32_t level = 0;
279 uint32_t priority = 0;
280 int32_t index = -1;
281
282 RecordedCommandSort() = default;
283
284 bool operator<(const RecordedCommandSort &p_other) const {
285 if (level < p_other.level) {
286 return true;
287 } else if (level > p_other.level) {
288 return false;
289 }
290
291 if (priority < p_other.priority) {
292 return true;
293 } else if (priority > p_other.priority) {
294 return false;
295 }
296
297 return index < p_other.index;
298 }
299 };
300
301 struct RecordedCommandListNode {
302 int32_t command_index = -1;
303 int32_t next_list_index = -1;
304 };
305
306 struct RecordedSliceListNode {
307 int32_t command_index = -1;
308 int32_t next_list_index = -1;
309 Rect2i subresources;
310 bool partial_coverage = false;
311 };
312
313 struct RecordedBufferClearCommand : RecordedCommand {
314 RDD::BufferID buffer;
315 uint32_t offset = 0;
316 uint32_t size = 0;
317 };
318
319 struct RecordedBufferCopyCommand : RecordedCommand {
320 RDD::BufferID source;
321 RDD::BufferID destination;
322 RDD::BufferCopyRegion region;
323 };
324
325 struct RecordedBufferGetDataCommand : RecordedCommand {
326 RDD::BufferID source;
327 RDD::BufferID destination;
328 RDD::BufferCopyRegion region;
329 };
330
331 struct RecordedBufferUpdateCommand : RecordedCommand {
332 RDD::BufferID destination;
333 uint32_t buffer_copies_count = 0;
334
335 _FORCE_INLINE_ RecordedBufferCopy *buffer_copies() {
336 return reinterpret_cast<RecordedBufferCopy *>(&this[1]);
337 }
338
339 _FORCE_INLINE_ const RecordedBufferCopy *buffer_copies() const {
340 return reinterpret_cast<const RecordedBufferCopy *>(&this[1]);
341 }
342 };
343
344 struct RecordedDriverCallbackCommand : RecordedCommand {
345 RDD::DriverCallback callback;
346 void *userdata = nullptr;
347 };
348
349 struct RecordedComputeListCommand : RecordedCommand {
350 uint32_t instruction_data_size = 0;
351 uint32_t breadcrumb = 0;
352
353 _FORCE_INLINE_ uint8_t *instruction_data() {
354 return reinterpret_cast<uint8_t *>(&this[1]);
355 }
356
357 _FORCE_INLINE_ const uint8_t *instruction_data() const {
358 return reinterpret_cast<const uint8_t *>(&this[1]);
359 }
360 };
361
362 struct RecordedDrawListCommand : RecordedCommand {
363 FramebufferCache *framebuffer_cache = nullptr;
364 RDD::FramebufferID framebuffer;
365 RDD::RenderPassID render_pass;
366 uint32_t instruction_data_size = 0;
367 RDD::CommandBufferType command_buffer_type;
368 Rect2i region;
369 uint32_t clear_values_count = 0;
370 uint32_t trackers_count = 0;
371
372 #if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
373 uint32_t breadcrumb = 0;
374 #endif
375 bool split_cmd_buffer = false;
376
377 _FORCE_INLINE_ RDD::RenderPassClearValue *clear_values() {
378 return reinterpret_cast<RDD::RenderPassClearValue *>(&this[1]);
379 }
380
381 _FORCE_INLINE_ const RDD::RenderPassClearValue *clear_values() const {
382 return reinterpret_cast<const RDD::RenderPassClearValue *>(&this[1]);
383 }
384
385 _FORCE_INLINE_ ResourceTracker **trackers() {
386 return reinterpret_cast<ResourceTracker **>(&clear_values()[clear_values_count]);
387 }
388
389 _FORCE_INLINE_ ResourceTracker *const *trackers() const {
390 return reinterpret_cast<ResourceTracker *const *>(&clear_values()[clear_values_count]);
391 }
392
393 _FORCE_INLINE_ RDD::AttachmentLoadOp *load_ops() {
394 return reinterpret_cast<RDD::AttachmentLoadOp *>(&trackers()[trackers_count]);
395 }
396
397 _FORCE_INLINE_ const RDD::AttachmentLoadOp *load_ops() const {
398 return reinterpret_cast<const RDD::AttachmentLoadOp *>(&trackers()[trackers_count]);
399 }
400
401 _FORCE_INLINE_ RDD::AttachmentStoreOp *store_ops() {
402 return reinterpret_cast<RDD::AttachmentStoreOp *>(&load_ops()[trackers_count]);
403 }
404
405 _FORCE_INLINE_ const RDD::AttachmentStoreOp *store_ops() const {
406 return reinterpret_cast<const RDD::AttachmentStoreOp *>(&load_ops()[trackers_count]);
407 }
408
409 _FORCE_INLINE_ uint8_t *instruction_data() {
410 return reinterpret_cast<uint8_t *>(&store_ops()[trackers_count]);
411 }
412
413 _FORCE_INLINE_ const uint8_t *instruction_data() const {
414 return reinterpret_cast<const uint8_t *>(&store_ops()[trackers_count]);
415 }
416 };
417
418 struct RecordedTextureClearColorCommand : RecordedCommand {
419 RDD::TextureID texture;
420 RDD::TextureSubresourceRange range;
421 Color color;
422 };
423
424 struct RecordedTextureClearDepthStencilCommand : RecordedCommand {
425 RDD::TextureID texture;
426 RDD::TextureSubresourceRange range;
427 float depth;
428 uint8_t stencil;
429 };
430
431 struct RecordedTextureCopyCommand : RecordedCommand {
432 RDD::TextureID from_texture;
433 RDD::TextureID to_texture;
434 uint32_t texture_copy_regions_count = 0;
435
436 _FORCE_INLINE_ RDD::TextureCopyRegion *texture_copy_regions() {
437 return reinterpret_cast<RDD::TextureCopyRegion *>(&this[1]);
438 }
439
440 _FORCE_INLINE_ const RDD::TextureCopyRegion *texture_copy_regions() const {
441 return reinterpret_cast<const RDD::TextureCopyRegion *>(&this[1]);
442 }
443 };
444
445 struct RecordedTextureGetDataCommand : RecordedCommand {
446 RDD::TextureID from_texture;
447 RDD::BufferID to_buffer;
448 uint32_t buffer_texture_copy_regions_count = 0;
449
450 _FORCE_INLINE_ RDD::BufferTextureCopyRegion *buffer_texture_copy_regions() {
451 return reinterpret_cast<RDD::BufferTextureCopyRegion *>(&this[1]);
452 }
453
454 _FORCE_INLINE_ const RDD::BufferTextureCopyRegion *buffer_texture_copy_regions() const {
455 return reinterpret_cast<const RDD::BufferTextureCopyRegion *>(&this[1]);
456 }
457 };
458
459 struct RecordedTextureResolveCommand : RecordedCommand {
460 RDD::TextureID from_texture;
461 RDD::TextureID to_texture;
462 uint32_t src_layer = 0;
463 uint32_t src_mipmap = 0;
464 uint32_t dst_layer = 0;
465 uint32_t dst_mipmap = 0;
466 };
467
468 struct RecordedTextureUpdateCommand : RecordedCommand {
469 RDD::TextureID to_texture;
470 uint32_t buffer_to_texture_copies_count = 0;
471
472 _FORCE_INLINE_ RecordedBufferToTextureCopy *buffer_to_texture_copies() {
473 return reinterpret_cast<RecordedBufferToTextureCopy *>(&this[1]);
474 }
475
476 _FORCE_INLINE_ const RecordedBufferToTextureCopy *buffer_to_texture_copies() const {
477 return reinterpret_cast<const RecordedBufferToTextureCopy *>(&this[1]);
478 }
479 };
480
481 struct RecordedCaptureTimestampCommand : RecordedCommand {
482 RDD::QueryPoolID pool;
483 uint32_t index = 0;
484 };
485
486 struct DrawListBindIndexBufferInstruction : DrawListInstruction {
487 RDD::BufferID buffer;
488 RenderingDeviceCommons::IndexBufferFormat format;
489 uint32_t offset = 0;
490 };
491
492 struct DrawListBindPipelineInstruction : DrawListInstruction {
493 RDD::PipelineID pipeline;
494 };
495
496 struct DrawListBindUniformSetsInstruction : DrawListInstruction {
497 RDD::ShaderID shader;
498 uint32_t first_set_index = 0;
499 uint32_t set_count = 0;
500 uint32_t dynamic_offsets_mask = 0u;
501
502 _FORCE_INLINE_ RDD::UniformSetID *uniform_set_ids() {
503 return reinterpret_cast<RDD::UniformSetID *>(&this[1]);
504 }
505
506 _FORCE_INLINE_ const RDD::UniformSetID *uniform_set_ids() const {
507 return reinterpret_cast<const RDD::UniformSetID *>(&this[1]);
508 }
509 };
510
511 struct DrawListBindVertexBuffersInstruction : DrawListInstruction {
512 uint32_t vertex_buffers_count = 0;
513 uint64_t dynamic_offsets_mask = 0;
514
515 _FORCE_INLINE_ RDD::BufferID *vertex_buffers() {
516 return reinterpret_cast<RDD::BufferID *>(&this[1]);
517 }
518
519 _FORCE_INLINE_ const RDD::BufferID *vertex_buffers() const {
520 return reinterpret_cast<const RDD::BufferID *>(&this[1]);
521 }
522
523 _FORCE_INLINE_ uint64_t *vertex_buffer_offsets() {
524 return reinterpret_cast<uint64_t *>(&vertex_buffers()[vertex_buffers_count]);
525 }
526
527 _FORCE_INLINE_ const uint64_t *vertex_buffer_offsets() const {
528 return reinterpret_cast<const uint64_t *>(&vertex_buffers()[vertex_buffers_count]);
529 }
530 };
531
532 struct DrawListClearAttachmentsInstruction : DrawListInstruction {
533 uint32_t attachments_clear_count = 0;
534 uint32_t attachments_clear_rect_count = 0;
535
536 _FORCE_INLINE_ RDD::AttachmentClear *attachments_clear() {
537 return reinterpret_cast<RDD::AttachmentClear *>(&this[1]);
538 }
539
540 _FORCE_INLINE_ const RDD::AttachmentClear *attachments_clear() const {
541 return reinterpret_cast<const RDD::AttachmentClear *>(&this[1]);
542 }
543
544 _FORCE_INLINE_ Rect2i *attachments_clear_rect() {
545 return reinterpret_cast<Rect2i *>(&attachments_clear()[attachments_clear_count]);
546 }
547
548 _FORCE_INLINE_ const Rect2i *attachments_clear_rect() const {
549 return reinterpret_cast<const Rect2i *>(&attachments_clear()[attachments_clear_count]);
550 }
551 };
552
553 struct DrawListDrawInstruction : DrawListInstruction {
554 uint32_t vertex_count = 0;
555 uint32_t instance_count = 0;
556 };
557
558 struct DrawListDrawIndexedInstruction : DrawListInstruction {
559 uint32_t index_count = 0;
560 uint32_t instance_count = 0;
561 uint32_t first_index = 0;
562 };
563
564 struct DrawListDrawIndirectInstruction : DrawListInstruction {
565 RDD::BufferID buffer;
566 uint32_t offset = 0;
567 uint32_t draw_count = 0;
568 uint32_t stride = 0;
569 };
570
571 struct DrawListDrawIndexedIndirectInstruction : DrawListInstruction {
572 RDD::BufferID buffer;
573 uint32_t offset = 0;
574 uint32_t draw_count = 0;
575 uint32_t stride = 0;
576 };
577
578 struct DrawListEndRenderPassInstruction : DrawListInstruction {
579 // No contents.
580 };
581
582 struct DrawListExecuteCommandsInstruction : DrawListInstruction {
583 RDD::CommandBufferID command_buffer;
584 };
585
586 struct DrawListSetPushConstantInstruction : DrawListInstruction {
587 uint32_t size = 0;
588 RDD::ShaderID shader;
589
590 _FORCE_INLINE_ uint8_t *data() {
591 return reinterpret_cast<uint8_t *>(&this[1]);
592 }
593
594 _FORCE_INLINE_ const uint8_t *data() const {
595 return reinterpret_cast<const uint8_t *>(&this[1]);
596 }
597 };
598
599 struct DrawListNextSubpassInstruction : DrawListInstruction {
600 RDD::CommandBufferType command_buffer_type;
601 };
602
603 struct DrawListSetBlendConstantsInstruction : DrawListInstruction {
604 Color color;
605 };
606
607 struct DrawListSetLineWidthInstruction : DrawListInstruction {
608 float width;
609 };
610
611 struct DrawListSetScissorInstruction : DrawListInstruction {
612 Rect2i rect;
613 };
614
615 struct DrawListSetViewportInstruction : DrawListInstruction {
616 Rect2i rect;
617 };
618
619 struct DrawListUniformSetPrepareForUseInstruction : DrawListInstruction {
620 RDD::UniformSetID uniform_set;
621 RDD::ShaderID shader;
622 uint32_t set_index = 0;
623 };
624
625 struct ComputeListBindPipelineInstruction : ComputeListInstruction {
626 RDD::PipelineID pipeline;
627 };
628
629 struct ComputeListBindUniformSetsInstruction : ComputeListInstruction {
630 RDD::ShaderID shader;
631 uint32_t first_set_index = 0;
632 uint32_t set_count = 0;
633 uint32_t dynamic_offsets_mask = 0u;
634
635 _FORCE_INLINE_ RDD::UniformSetID *uniform_set_ids() {
636 return reinterpret_cast<RDD::UniformSetID *>(&this[1]);
637 }
638
639 _FORCE_INLINE_ const RDD::UniformSetID *uniform_set_ids() const {
640 return reinterpret_cast<const RDD::UniformSetID *>(&this[1]);
641 }
642 };
643
644 struct ComputeListDispatchInstruction : ComputeListInstruction {
645 uint32_t x_groups = 0;
646 uint32_t y_groups = 0;
647 uint32_t z_groups = 0;
648 };
649
650 struct ComputeListDispatchIndirectInstruction : ComputeListInstruction {
651 RDD::BufferID buffer;
652 uint32_t offset = 0;
653 };
654
655 struct ComputeListSetPushConstantInstruction : ComputeListInstruction {
656 uint32_t size = 0;
657 RDD::ShaderID shader;
658
659 _FORCE_INLINE_ uint8_t *data() {
660 return reinterpret_cast<uint8_t *>(&this[1]);
661 }
662
663 _FORCE_INLINE_ const uint8_t *data() const {
664 return reinterpret_cast<const uint8_t *>(&this[1]);
665 }
666 };
667
668 struct ComputeListUniformSetPrepareForUseInstruction : ComputeListInstruction {
669 RDD::UniformSetID uniform_set;
670 RDD::ShaderID shader;
671 uint32_t set_index = 0;
672 };
673
674 struct BarrierGroup {
675 BitField<RDD::PipelineStageBits> src_stages = {};
676 BitField<RDD::PipelineStageBits> dst_stages = {};
677 RDD::MemoryAccessBarrier memory_barrier;
678 LocalVector<RDD::TextureBarrier> normalization_barriers;
679 LocalVector<RDD::TextureBarrier> transition_barriers;
680 #if USE_BUFFER_BARRIERS
681 LocalVector<RDD::BufferBarrier> buffer_barriers;
682 #endif
683
684 void clear() {
685 src_stages.clear();
686 dst_stages.clear();
687 memory_barrier.src_access.clear();
688 memory_barrier.dst_access.clear();
689 normalization_barriers.clear();
690 transition_barriers.clear();
691 #if USE_BUFFER_BARRIERS
692 buffer_barriers.clear();
693 #endif
694 }
695 };
696
697 struct SecondaryCommandBuffer {
698 LocalVector<uint8_t> instruction_data;
699 RDD::CommandBufferID command_buffer;
700 RDD::CommandPoolID command_pool;
701 RDD::RenderPassID render_pass;
702 RDD::FramebufferID framebuffer;
703 WorkerThreadPool::TaskID task;
704 };
705
706 struct Frame {
707 TightLocalVector<SecondaryCommandBuffer> secondary_command_buffers;
708 uint32_t secondary_command_buffers_used = 0;
709 };
710
711 RDD *driver = nullptr;
712 RenderingContextDriver::Device device;
713 RenderPassCreationFunction render_pass_creation_function = nullptr;
714 int64_t tracking_frame = 0;
715 LocalVector<uint8_t> command_data;
716 LocalVector<uint32_t> command_data_offsets;
717 LocalVector<RDD::TextureBarrier> command_normalization_barriers;
718 LocalVector<RDD::TextureBarrier> command_transition_barriers;
719 LocalVector<RDD::BufferBarrier> command_buffer_barriers;
720 LocalVector<char> command_label_chars;
721 LocalVector<Color> command_label_colors;
722 LocalVector<uint32_t> command_label_offsets;
723 int32_t command_label_index = -1;
724 DrawInstructionList draw_instruction_list;
725 ComputeInstructionList compute_instruction_list;
726 uint32_t command_count = 0;
727 uint32_t command_label_count = 0;
728 LocalVector<RecordedCommandListNode> command_list_nodes;
729 LocalVector<RecordedSliceListNode> read_slice_list_nodes;
730 LocalVector<RecordedSliceListNode> write_slice_list_nodes;
731 int32_t command_timestamp_index = -1;
732 int32_t command_synchronization_index = -1;
733 bool command_synchronization_pending = false;
734 BarrierGroup barrier_group;
735 bool driver_honors_barriers : 1;
736 bool driver_clears_with_copy_engine : 1;
737 bool driver_buffers_require_transitions : 1;
738 WorkaroundsState workarounds_state;
739 TightLocalVector<Frame> frames;
740 uint32_t frame = 0;
741
742 #ifdef DEV_ENABLED
743 RBMap<ResourceTracker *, uint32_t> write_dependency_counters;
744 #endif
745
746 static String _usage_to_string(ResourceUsage p_usage);
747 static bool _is_write_usage(ResourceUsage p_usage);
748 static RDD::TextureLayout _usage_to_image_layout(ResourceUsage p_usage);
749 static RDD::BarrierAccessBits _usage_to_access_bits(ResourceUsage p_usage);
750 bool _check_command_intersection(ResourceTracker *p_resource_tracker, int32_t p_previous_command_index, int32_t p_command_index) const;
751 bool _check_command_partial_coverage(ResourceTracker *p_resource_tracker, int32_t p_command_index) const;
752 int32_t _add_to_command_list(int32_t p_command_index, int32_t p_list_index);
753 void _add_adjacent_command(int32_t p_previous_command_index, int32_t p_command_index, RecordedCommand *r_command);
754 int32_t _add_to_slice_read_list(int32_t p_command_index, Rect2i p_subresources, int32_t p_list_index);
755 int32_t _add_to_write_list(int32_t p_command_index, Rect2i p_subresources, int32_t p_list_index, bool p_partial_coverage);
756 RecordedCommand *_allocate_command(uint32_t p_command_size, int32_t &r_command_index);
757 DrawListInstruction *_allocate_draw_list_instruction(uint32_t p_instruction_size);
758 ComputeListInstruction *_allocate_compute_list_instruction(uint32_t p_instruction_size);
759 void _check_discardable_attachment_dependency(ResourceTracker *p_resource_tracker, int32_t p_previous_command_index, int32_t p_command_index);
760 void _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);
761 void _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);
762 #if USE_BUFFER_BARRIERS
763 void _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);
764 #endif
765 void _run_compute_list_command(RDD::CommandBufferID p_command_buffer, const uint8_t *p_instruction_data, uint32_t p_instruction_data_size);
766 void _get_draw_list_render_pass_and_framebuffer(const RecordedDrawListCommand *p_draw_list_command, RDD::RenderPassID &r_render_pass, RDD::FramebufferID &r_framebuffer);
767 void _run_draw_list_command(RDD::CommandBufferID p_command_buffer, const uint8_t *p_instruction_data, uint32_t p_instruction_data_size);
768 void _add_draw_list_begin(FramebufferCache *p_framebuffer_cache, RDD::RenderPassID p_render_pass, RDD::FramebufferID p_framebuffer, Rect2i p_region, VectorView<AttachmentOperation> p_attachment_operations, VectorView<RDD::RenderPassClearValue> p_attachment_clear_values, BitField<RDD::PipelineStageBits> p_stages, uint32_t p_breadcrumb, bool p_split_cmd_buffer);
769 void _run_secondary_command_buffer_task(const SecondaryCommandBuffer *p_secondary);
770 void _wait_for_secondary_command_buffer_tasks();
771 void _run_render_commands(int32_t p_level, const RecordedCommandSort *p_sorted_commands, uint32_t p_sorted_commands_count, RDD::CommandBufferID &r_command_buffer, CommandBufferPool &r_command_buffer_pool, int32_t &r_current_label_index, int32_t &r_current_label_level);
772 void _run_label_command_change(RDD::CommandBufferID p_command_buffer, int32_t p_new_label_index, int32_t p_new_level, bool p_ignore_previous_value, bool p_use_label_for_empty, const RecordedCommandSort *p_sorted_commands, uint32_t p_sorted_commands_count, int32_t &r_current_label_index, int32_t &r_current_label_level);
773 void _boost_priority_for_render_commands(RecordedCommandSort *p_sorted_commands, uint32_t p_sorted_commands_count, uint32_t &r_boosted_priority);
774 void _group_barriers_for_render_commands(RDD::CommandBufferID p_command_buffer, const RecordedCommandSort *p_sorted_commands, uint32_t p_sorted_commands_count, bool p_full_memory_barrier);
775 void _print_render_commands(const RecordedCommandSort *p_sorted_commands, uint32_t p_sorted_commands_count);
776 void _print_draw_list(const uint8_t *p_instruction_data, uint32_t p_instruction_data_size);
777 void _print_compute_list(const uint8_t *p_instruction_data, uint32_t p_instruction_data_size);
778
779 public:
780 RenderingDeviceGraph();
781 ~RenderingDeviceGraph();
782 void initialize(RDD *p_driver, RenderingContextDriver::Device p_device, RenderPassCreationFunction p_render_pass_creation_function, uint32_t p_frame_count, RDD::CommandQueueFamilyID p_secondary_command_queue_family, uint32_t p_secondary_command_buffers_per_frame);
783 void finalize();
784 void begin();
785 void add_buffer_clear(RDD::BufferID p_dst, ResourceTracker *p_dst_tracker, uint32_t p_offset, uint32_t p_size);
786 void add_buffer_copy(RDD::BufferID p_src, ResourceTracker *p_src_tracker, RDD::BufferID p_dst, ResourceTracker *p_dst_tracker, RDD::BufferCopyRegion p_region);
787 void add_buffer_get_data(RDD::BufferID p_src, ResourceTracker *p_src_tracker, RDD::BufferID p_dst, RDD::BufferCopyRegion p_region);
788 void add_buffer_update(RDD::BufferID p_dst, ResourceTracker *p_dst_tracker, VectorView<RecordedBufferCopy> p_buffer_copies);
789 void add_driver_callback(RDD::DriverCallback p_callback, void *p_userdata, VectorView<ResourceTracker *> p_trackers, VectorView<ResourceUsage> p_usages);
790 void add_compute_list_begin(RDD::BreadcrumbMarker p_phase = RDD::BreadcrumbMarker::NONE, uint32_t p_breadcrumb_data = 0);
791 void add_compute_list_bind_pipeline(RDD::PipelineID p_pipeline);
792 void add_compute_list_bind_uniform_set(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index);
793 void add_compute_list_bind_uniform_sets(RDD::ShaderID p_shader, VectorView<RDD::UniformSetID> p_uniform_set, uint32_t p_first_set_index, uint32_t p_set_count);
794 void add_compute_list_dispatch(uint32_t p_x_groups, uint32_t p_y_groups, uint32_t p_z_groups);
795 void add_compute_list_dispatch_indirect(RDD::BufferID p_buffer, uint32_t p_offset);
796 void add_compute_list_set_push_constant(RDD::ShaderID p_shader, const void *p_data, uint32_t p_data_size);
797 void add_compute_list_uniform_set_prepare_for_use(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index);
798 void add_compute_list_usage(ResourceTracker *p_tracker, ResourceUsage p_usage);
799 void add_compute_list_usages(VectorView<ResourceTracker *> p_trackers, VectorView<ResourceUsage> p_usages);
800 void add_compute_list_end();
801 void add_draw_list_begin(FramebufferCache *p_framebuffer_cache, Rect2i p_region, VectorView<AttachmentOperation> p_attachment_operations, VectorView<RDD::RenderPassClearValue> p_attachment_clear_values, BitField<RDD::PipelineStageBits> p_stages, uint32_t p_breadcrumb = 0, bool p_split_cmd_buffer = false);
802 void add_draw_list_begin(RDD::RenderPassID p_render_pass, RDD::FramebufferID p_framebuffer, Rect2i p_region, VectorView<AttachmentOperation> p_attachment_operations, VectorView<RDD::RenderPassClearValue> p_attachment_clear_values, BitField<RDD::PipelineStageBits> p_stages, uint32_t p_breadcrumb = 0, bool p_split_cmd_buffer = false);
803 void add_draw_list_bind_index_buffer(RDD::BufferID p_buffer, RDD::IndexBufferFormat p_format, uint32_t p_offset);
804 void add_draw_list_bind_pipeline(RDD::PipelineID p_pipeline, BitField<RDD::PipelineStageBits> p_pipeline_stage_bits);
805 void add_draw_list_bind_uniform_set(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index);
806 void add_draw_list_bind_uniform_sets(RDD::ShaderID p_shader, VectorView<RDD::UniformSetID> p_uniform_set, uint32_t p_first_index, uint32_t p_set_count);
807 void add_draw_list_bind_vertex_buffers(Span<RDD::BufferID> p_vertex_buffers, Span<uint64_t> p_vertex_buffer_offsets);
808 void add_draw_list_clear_attachments(VectorView<RDD::AttachmentClear> p_attachments_clear, VectorView<Rect2i> p_attachments_clear_rect);
809 void add_draw_list_draw(uint32_t p_vertex_count, uint32_t p_instance_count);
810 void add_draw_list_draw_indexed(uint32_t p_index_count, uint32_t p_instance_count, uint32_t p_first_index);
811 void add_draw_list_draw_indirect(RDD::BufferID p_buffer, uint32_t p_offset, uint32_t p_draw_count, uint32_t p_stride);
812 void add_draw_list_draw_indexed_indirect(RDD::BufferID p_buffer, uint32_t p_offset, uint32_t p_draw_count, uint32_t p_stride);
813 void add_draw_list_execute_commands(RDD::CommandBufferID p_command_buffer);
814 void add_draw_list_next_subpass(RDD::CommandBufferType p_command_buffer_type);
815 void add_draw_list_set_blend_constants(const Color &p_color);
816 void add_draw_list_set_line_width(float p_width);
817 void add_draw_list_set_push_constant(RDD::ShaderID p_shader, const void *p_data, uint32_t p_data_size);
818 void add_draw_list_set_scissor(Rect2i p_rect);
819 void add_draw_list_set_viewport(Rect2i p_rect);
820 void add_draw_list_uniform_set_prepare_for_use(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index);
821 void add_draw_list_usage(ResourceTracker *p_tracker, ResourceUsage p_usage);
822 void add_draw_list_usages(VectorView<ResourceTracker *> p_trackers, VectorView<ResourceUsage> p_usages);
823 void add_draw_list_end();
824 void add_texture_clear_color(RDD::TextureID p_dst, ResourceTracker *p_dst_tracker, const Color &p_color, const RDD::TextureSubresourceRange &p_range);
825 void add_texture_clear_depth_stencil(RDD::TextureID p_dst, ResourceTracker *p_dst_tracker, float p_depth, uint8_t p_stencil, const RDD::TextureSubresourceRange &p_range);
826 void add_texture_copy(RDD::TextureID p_src, ResourceTracker *p_src_tracker, RDD::TextureID p_dst, ResourceTracker *p_dst_tracker, VectorView<RDD::TextureCopyRegion> p_texture_copy_regions);
827 void add_texture_get_data(RDD::TextureID p_src, ResourceTracker *p_src_tracker, RDD::BufferID p_dst, VectorView<RDD::BufferTextureCopyRegion> p_buffer_texture_copy_regions, ResourceTracker *p_dst_tracker = nullptr);
828 void add_texture_resolve(RDD::TextureID p_src, ResourceTracker *p_src_tracker, RDD::TextureID p_dst, ResourceTracker *p_dst_tracker, uint32_t p_src_layer, uint32_t p_src_mipmap, uint32_t p_dst_layer, uint32_t p_dst_mipmap);
829 void add_texture_update(RDD::TextureID p_dst, ResourceTracker *p_dst_tracker, VectorView<RecordedBufferToTextureCopy> p_buffer_copies, VectorView<ResourceTracker *> p_buffer_trackers = VectorView<ResourceTracker *>());
830 void add_capture_timestamp(RDD::QueryPoolID p_query_pool, uint32_t p_index);
831 void add_synchronization();
832 void begin_label(const Span<char> &p_label_name, const Color &p_color);
833 void end_label();
834 void end(bool p_reorder_commands, bool p_full_barriers, RDD::CommandBufferID &r_command_buffer, CommandBufferPool &r_command_buffer_pool);
835 static ResourceTracker *resource_tracker_create();
836 static void resource_tracker_free(ResourceTracker *p_tracker);
837 static FramebufferCache *framebuffer_cache_create();
838 static void framebuffer_cache_free(RDD *p_driver, FramebufferCache *p_cache);
839 };
840
841 using RDG = RenderingDeviceGraph;