Debian Codegraph @

servers/rendering/rendering_device_graph.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* rendering_device_graph.cpp */
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 #include "rendering_device_graph.h"
32
33 #define PRINT_RENDER_GRAPH 0
34 #define FORCE_FULL_ACCESS_BITS 0
35 #define PRINT_RESOURCE_TRACKER_TOTAL 0
36 #define PRINT_COMMAND_RECORDING 0
37
38 // Prints the total number of bytes used for draw lists in a frame.
39 #define PRINT_DRAW_LIST_STATS 0
40
41 RenderingDeviceGraph::RenderingDeviceGraph() {
42 driver_honors_barriers = false;
43 driver_clears_with_copy_engine = false;
44 }
45
46 RenderingDeviceGraph::~RenderingDeviceGraph() {
47 }
48
49 String RenderingDeviceGraph::_usage_to_string(ResourceUsage p_usage) {
50 switch (p_usage) {
51 case RESOURCE_USAGE_NONE:
52 return "None";
53 case RESOURCE_USAGE_COPY_FROM:
54 return "Copy From";
55 case RESOURCE_USAGE_COPY_TO:
56 return "Copy To";
57 case RESOURCE_USAGE_RESOLVE_FROM:
58 return "Resolve From";
59 case RESOURCE_USAGE_RESOLVE_TO:
60 return "Resolve To";
61 case RESOURCE_USAGE_UNIFORM_BUFFER_READ:
62 return "Uniform Buffer Read";
63 case RESOURCE_USAGE_INDIRECT_BUFFER_READ:
64 return "Indirect Buffer Read";
65 case RESOURCE_USAGE_TEXTURE_BUFFER_READ:
66 return "Texture Buffer Read";
67 case RESOURCE_USAGE_TEXTURE_BUFFER_READ_WRITE:
68 return "Texture Buffer Read Write";
69 case RESOURCE_USAGE_STORAGE_BUFFER_READ:
70 return "Storage Buffer Read";
71 case RESOURCE_USAGE_STORAGE_BUFFER_READ_WRITE:
72 return "Storage Buffer Read Write";
73 case RESOURCE_USAGE_VERTEX_BUFFER_READ:
74 return "Vertex Buffer Read";
75 case RESOURCE_USAGE_INDEX_BUFFER_READ:
76 return "Index Buffer Read";
77 case RESOURCE_USAGE_TEXTURE_SAMPLE:
78 return "Texture Sample";
79 case RESOURCE_USAGE_STORAGE_IMAGE_READ:
80 return "Storage Image Read";
81 case RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE:
82 return "Storage Image Read Write";
83 case RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE:
84 return "Attachment Color Read Write";
85 case RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE:
86 return "Attachment Depth Stencil Read Write";
87 case RESOURCE_USAGE_GENERAL:
88 return "General";
89 default:
90 ERR_FAIL_V_MSG("Invalid", vformat("Invalid resource usage %d.", p_usage));
91 }
92 }
93
94 bool RenderingDeviceGraph::_is_write_usage(ResourceUsage p_usage) {
95 switch (p_usage) {
96 case RESOURCE_USAGE_COPY_FROM:
97 case RESOURCE_USAGE_RESOLVE_FROM:
98 case RESOURCE_USAGE_UNIFORM_BUFFER_READ:
99 case RESOURCE_USAGE_INDIRECT_BUFFER_READ:
100 case RESOURCE_USAGE_TEXTURE_BUFFER_READ:
101 case RESOURCE_USAGE_STORAGE_BUFFER_READ:
102 case RESOURCE_USAGE_VERTEX_BUFFER_READ:
103 case RESOURCE_USAGE_INDEX_BUFFER_READ:
104 case RESOURCE_USAGE_TEXTURE_SAMPLE:
105 case RESOURCE_USAGE_STORAGE_IMAGE_READ:
106 case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_SHADING_RATE_READ:
107 case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_DENSITY_MAP_READ:
108 return false;
109 case RESOURCE_USAGE_COPY_TO:
110 case RESOURCE_USAGE_RESOLVE_TO:
111 case RESOURCE_USAGE_TEXTURE_BUFFER_READ_WRITE:
112 case RESOURCE_USAGE_STORAGE_BUFFER_READ_WRITE:
113 case RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE:
114 case RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE:
115 case RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE:
116 case RESOURCE_USAGE_GENERAL:
117 return true;
118 default:
119 DEV_ASSERT(false && "Invalid resource tracker usage.");
120 return false;
121 }
122 }
123
124 RDD::TextureLayout RenderingDeviceGraph::_usage_to_image_layout(ResourceUsage p_usage) {
125 switch (p_usage) {
126 case RESOURCE_USAGE_COPY_FROM:
127 return RDD::TEXTURE_LAYOUT_COPY_SRC_OPTIMAL;
128 case RESOURCE_USAGE_COPY_TO:
129 return RDD::TEXTURE_LAYOUT_COPY_DST_OPTIMAL;
130 case RESOURCE_USAGE_RESOLVE_FROM:
131 return RDD::TEXTURE_LAYOUT_RESOLVE_SRC_OPTIMAL;
132 case RESOURCE_USAGE_RESOLVE_TO:
133 return RDD::TEXTURE_LAYOUT_RESOLVE_DST_OPTIMAL;
134 case RESOURCE_USAGE_TEXTURE_SAMPLE:
135 return RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
136 case RESOURCE_USAGE_STORAGE_IMAGE_READ:
137 case RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE:
138 return RDD::TEXTURE_LAYOUT_STORAGE_OPTIMAL;
139 case RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE:
140 return RDD::TEXTURE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
141 case RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE:
142 return RDD::TEXTURE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
143 case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_SHADING_RATE_READ:
144 return RDD::TEXTURE_LAYOUT_FRAGMENT_SHADING_RATE_ATTACHMENT_OPTIMAL;
145 case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_DENSITY_MAP_READ:
146 return RDD::TEXTURE_LAYOUT_FRAGMENT_DENSITY_MAP_ATTACHMENT_OPTIMAL;
147 case RESOURCE_USAGE_GENERAL:
148 return RDD::TEXTURE_LAYOUT_GENERAL;
149 case RESOURCE_USAGE_NONE:
150 return RDD::TEXTURE_LAYOUT_UNDEFINED;
151 default:
152 DEV_ASSERT(false && "Invalid resource tracker usage or not an image usage.");
153 return RDD::TEXTURE_LAYOUT_UNDEFINED;
154 }
155 }
156
157 RDD::BarrierAccessBits RenderingDeviceGraph::_usage_to_access_bits(ResourceUsage p_usage) {
158 #if FORCE_FULL_ACCESS_BITS
159 return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_MEMORY_READ_BIT | RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT);
160 #else
161 switch (p_usage) {
162 case RESOURCE_USAGE_NONE:
163 return RDD::BarrierAccessBits(0);
164 case RESOURCE_USAGE_COPY_FROM:
165 return RDD::BARRIER_ACCESS_COPY_READ_BIT;
166 case RESOURCE_USAGE_COPY_TO:
167 return RDD::BARRIER_ACCESS_COPY_WRITE_BIT;
168 case RESOURCE_USAGE_RESOLVE_FROM:
169 return RDD::BARRIER_ACCESS_RESOLVE_READ_BIT;
170 case RESOURCE_USAGE_RESOLVE_TO:
171 return RDD::BARRIER_ACCESS_RESOLVE_WRITE_BIT;
172 case RESOURCE_USAGE_UNIFORM_BUFFER_READ:
173 return RDD::BARRIER_ACCESS_UNIFORM_READ_BIT;
174 case RESOURCE_USAGE_INDIRECT_BUFFER_READ:
175 return RDD::BARRIER_ACCESS_INDIRECT_COMMAND_READ_BIT;
176 case RESOURCE_USAGE_STORAGE_BUFFER_READ:
177 case RESOURCE_USAGE_STORAGE_IMAGE_READ:
178 case RESOURCE_USAGE_TEXTURE_BUFFER_READ:
179 case RESOURCE_USAGE_TEXTURE_SAMPLE:
180 return RDD::BARRIER_ACCESS_SHADER_READ_BIT;
181 case RESOURCE_USAGE_TEXTURE_BUFFER_READ_WRITE:
182 case RESOURCE_USAGE_STORAGE_BUFFER_READ_WRITE:
183 case RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE:
184 return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_SHADER_READ_BIT | RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
185 case RESOURCE_USAGE_VERTEX_BUFFER_READ:
186 return RDD::BARRIER_ACCESS_VERTEX_ATTRIBUTE_READ_BIT;
187 case RESOURCE_USAGE_INDEX_BUFFER_READ:
188 return RDD::BARRIER_ACCESS_INDEX_READ_BIT;
189 case RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE:
190 return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_READ_BIT | RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_WRITE_BIT);
191 case RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE:
192 return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | RDD::BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT);
193 case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_SHADING_RATE_READ:
194 return RDD::BARRIER_ACCESS_FRAGMENT_SHADING_RATE_ATTACHMENT_READ_BIT;
195 case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_DENSITY_MAP_READ:
196 return RDD::BARRIER_ACCESS_FRAGMENT_DENSITY_MAP_ATTACHMENT_READ_BIT;
197 case RESOURCE_USAGE_GENERAL:
198 return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_MEMORY_READ_BIT | RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT);
199 default:
200 DEV_ASSERT(false && "Invalid usage.");
201 return RDD::BarrierAccessBits(0);
202 }
203 #endif
204 }
205
206 bool RenderingDeviceGraph::_check_command_intersection(ResourceTracker *p_resource_tracker, int32_t p_previous_command_index, int32_t p_command_index) const {
207 if (p_resource_tracker->usage != RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE && p_resource_tracker->usage != RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE) {
208 // We don't check possible intersections for usages that aren't consecutive color or depth writes.
209 return true;
210 }
211
212 const uint32_t previous_command_data_offset = command_data_offsets[p_previous_command_index];
213 const uint32_t current_command_data_offset = command_data_offsets[p_command_index];
214 const RecordedDrawListCommand &previous_draw_list_command = *reinterpret_cast<const RecordedDrawListCommand *>(&command_data[previous_command_data_offset]);
215 const RecordedDrawListCommand &current_draw_list_command = *reinterpret_cast<const RecordedDrawListCommand *>(&command_data[current_command_data_offset]);
216 if (previous_draw_list_command.type != RecordedCommand::TYPE_DRAW_LIST || current_draw_list_command.type != RecordedCommand::TYPE_DRAW_LIST) {
217 // We don't check possible intersections if both commands aren't draw lists.
218 return true;
219 }
220
221 // We check if the region used by both draw lists have an intersection.
222 return previous_draw_list_command.region.intersects(current_draw_list_command.region);
223 }
224
225 bool RenderingDeviceGraph::_check_command_partial_coverage(ResourceTracker *p_resource_tracker, int32_t p_command_index) const {
226 if (p_resource_tracker->usage != RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE && p_resource_tracker->usage != RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE) {
227 // We don't check for partial coverage in usages that aren't attachment writes.
228 return false;
229 }
230
231 const uint32_t command_data_offset = command_data_offsets[p_command_index];
232 const RecordedDrawListCommand &draw_list_command = *reinterpret_cast<const RecordedDrawListCommand *>(&command_data[command_data_offset]);
233 if (draw_list_command.type != RecordedCommand::TYPE_DRAW_LIST) {
234 // We don't check for partial coverage on commands that aren't draw lists.
235 return false;
236 }
237
238 Rect2i texture_region(Point2i(0, 0), p_resource_tracker->texture_size);
239 return !draw_list_command.region.encloses(texture_region);
240 }
241
242 int32_t RenderingDeviceGraph::_add_to_command_list(int32_t p_command_index, int32_t p_list_index) {
243 DEV_ASSERT(p_command_index < int32_t(command_count));
244 DEV_ASSERT(p_list_index < int32_t(command_list_nodes.size()));
245
246 int32_t next_index = int32_t(command_list_nodes.size());
247 command_list_nodes.resize(next_index + 1);
248
249 RecordedCommandListNode &new_node = command_list_nodes[next_index];
250 new_node.command_index = p_command_index;
251 new_node.next_list_index = p_list_index;
252 return next_index;
253 }
254
255 void RenderingDeviceGraph::_add_adjacent_command(int32_t p_previous_command_index, int32_t p_command_index, RecordedCommand *r_command) {
256 const uint32_t previous_command_data_offset = command_data_offsets[p_previous_command_index];
257 RecordedCommand &previous_command = *reinterpret_cast<RecordedCommand *>(&command_data[previous_command_data_offset]);
258 previous_command.adjacent_command_list_index = _add_to_command_list(p_command_index, previous_command.adjacent_command_list_index);
259 previous_command.next_stages = previous_command.next_stages | r_command->self_stages;
260 r_command->previous_stages = r_command->previous_stages | previous_command.self_stages;
261 }
262
263 int32_t RenderingDeviceGraph::_add_to_slice_read_list(int32_t p_command_index, Rect2i p_subresources, int32_t p_list_index) {
264 DEV_ASSERT(p_command_index < int32_t(command_count));
265 DEV_ASSERT(p_list_index < int32_t(read_slice_list_nodes.size()));
266
267 int32_t next_index = int32_t(read_slice_list_nodes.size());
268 read_slice_list_nodes.resize(next_index + 1);
269
270 RecordedSliceListNode &new_node = read_slice_list_nodes[next_index];
271 new_node.command_index = p_command_index;
272 new_node.next_list_index = p_list_index;
273 new_node.subresources = p_subresources;
274 return next_index;
275 }
276
277 int32_t RenderingDeviceGraph::_add_to_write_list(int32_t p_command_index, Rect2i p_subresources, int32_t p_list_index, bool p_partial_coverage) {
278 DEV_ASSERT(p_command_index < int32_t(command_count));
279 DEV_ASSERT(p_list_index < int32_t(write_slice_list_nodes.size()));
280
281 int32_t next_index = int32_t(write_slice_list_nodes.size());
282 write_slice_list_nodes.resize(next_index + 1);
283
284 RecordedSliceListNode &new_node = write_slice_list_nodes[next_index];
285 new_node.command_index = p_command_index;
286 new_node.next_list_index = p_list_index;
287 new_node.subresources = p_subresources;
288 new_node.partial_coverage = p_partial_coverage;
289 return next_index;
290 }
291
292 // Ensures all commands are 8-byte aligned.
293 #define GRAPH_ALIGN(x) (((x) + 7u) & 0xFFFFFFF8u)
294
295 RenderingDeviceGraph::RecordedCommand *RenderingDeviceGraph::_allocate_command(uint32_t p_command_size, int32_t &r_command_index) {
296 uint32_t command_data_offset = command_data.size();
297 command_data_offset = GRAPH_ALIGN(command_data_offset);
298 command_data_offsets.push_back(command_data_offset);
299 command_data.resize(command_data_offset + p_command_size);
300 r_command_index = command_count++;
301 RecordedCommand *new_command = reinterpret_cast<RecordedCommand *>(&command_data[command_data_offset]);
302 *new_command = RecordedCommand();
303 return new_command;
304 }
305
306 RenderingDeviceGraph::DrawListInstruction *RenderingDeviceGraph::_allocate_draw_list_instruction(uint32_t p_instruction_size) {
307 uint32_t draw_list_data_offset = draw_instruction_list.data.size();
308 draw_list_data_offset = GRAPH_ALIGN(draw_list_data_offset);
309 draw_instruction_list.data.resize(draw_list_data_offset + p_instruction_size);
310 return reinterpret_cast<DrawListInstruction *>(&draw_instruction_list.data[draw_list_data_offset]);
311 }
312
313 RenderingDeviceGraph::ComputeListInstruction *RenderingDeviceGraph::_allocate_compute_list_instruction(uint32_t p_instruction_size) {
314 uint32_t compute_list_data_offset = compute_instruction_list.data.size();
315 compute_list_data_offset = GRAPH_ALIGN(compute_list_data_offset);
316 compute_instruction_list.data.resize(compute_list_data_offset + p_instruction_size);
317 return reinterpret_cast<ComputeListInstruction *>(&compute_instruction_list.data[compute_list_data_offset]);
318 }
319
320 void RenderingDeviceGraph::_check_discardable_attachment_dependency(ResourceTracker *p_resource_tracker, int32_t p_previous_command_index, int32_t p_command_index) {
321 if (!p_resource_tracker->is_discardable) {
322 return;
323 }
324
325 // Check if the command is a a draw list that clears the attachment completely. If it is, we don't need to modify the previous draw list.
326 uint32_t command_offset = command_data_offsets[p_command_index];
327 RecordedDrawListCommand *draw_list_command = reinterpret_cast<RecordedDrawListCommand *>(&command_data[command_offset]);
328 if (draw_list_command->type == RecordedCommand::TYPE_DRAW_LIST) {
329 ResourceTracker **trackers = draw_list_command->trackers();
330 for (uint32_t i = 0; i < draw_list_command->trackers_count; i++) {
331 if (trackers[i] == p_resource_tracker && draw_list_command->load_ops()[i] == RDD::ATTACHMENT_LOAD_OP_CLEAR) {
332 return;
333 }
334 }
335 }
336
337 // Check if the previous command is a draw list.
338 uint32_t previous_command_offset = command_data_offsets[p_previous_command_index];
339 RecordedDrawListCommand *previous_draw_list_command = reinterpret_cast<RecordedDrawListCommand *>(&command_data[previous_command_offset]);
340 if (previous_draw_list_command->type != RecordedCommand::TYPE_DRAW_LIST) {
341 return;
342 }
343
344 // Search for the tracker inside the draw list command and modify the store operation accordingly.
345 ResourceTracker **trackers = previous_draw_list_command->trackers();
346 for (uint32_t i = 0; i < previous_draw_list_command->trackers_count; i++) {
347 if (trackers[i] == p_resource_tracker) {
348 previous_draw_list_command->store_ops()[i] = RDD::ATTACHMENT_STORE_OP_STORE;
349 return;
350 }
351 }
352 }
353
354 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) {
355 // Assign the next stages derived from the stages the command requires first.
356 r_command->next_stages = r_command->self_stages;
357
358 if (command_label_index >= 0) {
359 // If a label is active, tag the command with the label.
360 r_command->label_index = command_label_index;
361 }
362
363 if (r_command->type == RecordedCommand::TYPE_CAPTURE_TIMESTAMP) {
364 // All previous commands starting from the previous timestamp should be adjacent to this command.
365 int32_t start_command_index = uint32_t(MAX(command_timestamp_index, 0));
366 for (int32_t i = start_command_index; i < p_command_index; i++) {
367 _add_adjacent_command(i, p_command_index, r_command);
368 }
369
370 // Make this command the new active timestamp command.
371 command_timestamp_index = p_command_index;
372 } else if (command_timestamp_index >= 0) {
373 // Timestamp command should be adjacent to this command.
374 _add_adjacent_command(command_timestamp_index, p_command_index, r_command);
375 }
376
377 if (command_synchronization_pending) {
378 // All previous commands should be adjacent to this command.
379 int32_t start_command_index = uint32_t(MAX(command_synchronization_index, 0));
380 for (int32_t i = start_command_index; i < p_command_index; i++) {
381 _add_adjacent_command(i, p_command_index, r_command);
382 }
383
384 command_synchronization_index = p_command_index;
385 command_synchronization_pending = false;
386 } else if (command_synchronization_index >= 0) {
387 // Synchronization command should be adjacent to this command.
388 _add_adjacent_command(command_synchronization_index, p_command_index, r_command);
389 }
390
391 for (uint32_t i = 0; i < p_resource_count; i++) {
392 ResourceTracker *resource_tracker = p_resource_trackers[i];
393 DEV_ASSERT(resource_tracker != nullptr);
394
395 resource_tracker->reset_if_outdated(tracking_frame);
396
397 const RDD::TextureSubresourceRange &subresources = resource_tracker->texture_subresources;
398 const Rect2i resource_tracker_rect(subresources.base_mipmap, subresources.base_layer, subresources.mipmap_count, subresources.layer_count);
399 Rect2i search_tracker_rect = resource_tracker_rect;
400
401 ResourceUsage new_resource_usage = p_resource_usages[i];
402 bool write_usage = _is_write_usage(new_resource_usage);
403 BitField<RDD::BarrierAccessBits> new_usage_access = _usage_to_access_bits(new_resource_usage);
404 bool is_resource_a_slice = resource_tracker->parent != nullptr;
405 if (is_resource_a_slice) {
406 // This resource depends on a parent resource.
407 resource_tracker->parent->reset_if_outdated(tracking_frame);
408
409 if (resource_tracker->texture_slice_command_index != p_command_index) {
410 // Indicate this slice has been used by this command.
411 resource_tracker->texture_slice_command_index = p_command_index;
412 }
413
414 if (resource_tracker->parent->usage == RESOURCE_USAGE_NONE) {
415 if (resource_tracker->parent->texture_driver_id.id != 0) {
416 // If the resource is a texture, we transition it entirely to the layout determined by the first slice that uses it.
417 _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);
418 }
419
420 // If the parent hasn't been used yet, we assign the usage of the slice to the entire resource.
421 resource_tracker->parent->usage = new_resource_usage;
422
423 // Also assign the usage to the slice and consider it a write operation. Consider the parent's current usage access as its own.
424 resource_tracker->usage = new_resource_usage;
425 resource_tracker->usage_access = resource_tracker->parent->usage_access;
426 write_usage = true;
427
428 // Indicate the area that should be tracked is the entire resource.
429 const RDD::TextureSubresourceRange &parent_subresources = resource_tracker->parent->texture_subresources;
430 search_tracker_rect = Rect2i(parent_subresources.base_mipmap, parent_subresources.base_layer, parent_subresources.mipmap_count, parent_subresources.layer_count);
431 } else if (resource_tracker->in_parent_dirty_list) {
432 if (resource_tracker->parent->usage == new_resource_usage) {
433 // The slice will be transitioned to the resource of the parent and can be deleted from the dirty list.
434 ResourceTracker *previous_tracker = nullptr;
435 ResourceTracker *current_tracker = resource_tracker->parent->dirty_shared_list;
436 bool initialized_dirty_rect = false;
437 while (current_tracker != nullptr) {
438 current_tracker->reset_if_outdated(tracking_frame);
439
440 if (current_tracker == resource_tracker) {
441 current_tracker->in_parent_dirty_list = false;
442
443 if (previous_tracker != nullptr) {
444 previous_tracker->next_shared = current_tracker->next_shared;
445 } else {
446 resource_tracker->parent->dirty_shared_list = current_tracker->next_shared;
447 }
448
449 current_tracker = current_tracker->next_shared;
450 } else {
451 if (initialized_dirty_rect) {
452 resource_tracker->parent->texture_slice_or_dirty_rect = resource_tracker->parent->texture_slice_or_dirty_rect.merge(current_tracker->texture_slice_or_dirty_rect);
453 } else {
454 resource_tracker->parent->texture_slice_or_dirty_rect = current_tracker->texture_slice_or_dirty_rect;
455 initialized_dirty_rect = true;
456 }
457
458 previous_tracker = current_tracker;
459 current_tracker = current_tracker->next_shared;
460 }
461 }
462 }
463 } else {
464 if (resource_tracker->parent->dirty_shared_list != nullptr && resource_tracker->parent->texture_slice_or_dirty_rect.intersects(resource_tracker->texture_slice_or_dirty_rect)) {
465 // 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
466 // 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.
467 ResourceTracker *previous_tracker = nullptr;
468 ResourceTracker *current_tracker = resource_tracker->parent->dirty_shared_list;
469 bool initialized_dirty_rect = false;
470 while (current_tracker != nullptr) {
471 current_tracker->reset_if_outdated(tracking_frame);
472
473 if (current_tracker->texture_slice_or_dirty_rect.intersects(resource_tracker->texture_slice_or_dirty_rect)) {
474 if (current_tracker->command_frame == tracking_frame && current_tracker->texture_slice_command_index == p_command_index) {
475 ERR_FAIL_MSG("Texture slices that overlap can't be used in the same command.");
476 } else {
477 // Delete the slice from the dirty list and revert it to the usage of the parent.
478 if (current_tracker->texture_driver_id.id != 0) {
479 _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);
480
481 // Merge the area of the slice with the current tracking area of the command and indicate it's a write usage as well.
482 search_tracker_rect = search_tracker_rect.merge(current_tracker->texture_slice_or_dirty_rect);
483 write_usage = true;
484 }
485
486 current_tracker->in_parent_dirty_list = false;
487
488 if (previous_tracker != nullptr) {
489 previous_tracker->next_shared = current_tracker->next_shared;
490 } else {
491 resource_tracker->parent->dirty_shared_list = current_tracker->next_shared;
492 }
493
494 current_tracker = current_tracker->next_shared;
495 }
496 } else {
497 // Recalculate the dirty rect of the parent so the deleted slices are excluded.
498 if (initialized_dirty_rect) {
499 resource_tracker->parent->texture_slice_or_dirty_rect = resource_tracker->parent->texture_slice_or_dirty_rect.merge(current_tracker->texture_slice_or_dirty_rect);
500 } else {
501 resource_tracker->parent->texture_slice_or_dirty_rect = current_tracker->texture_slice_or_dirty_rect;
502 initialized_dirty_rect = true;
503 }
504
505 previous_tracker = current_tracker;
506 current_tracker = current_tracker->next_shared;
507 }
508 }
509 }
510
511 // 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.
512 resource_tracker->usage = resource_tracker->parent->usage;
513 resource_tracker->usage_access = resource_tracker->parent->usage_access;
514
515 if (resource_tracker->usage != new_resource_usage) {
516 // Insert to the dirty list if the requested usage is different.
517 resource_tracker->next_shared = resource_tracker->parent->dirty_shared_list;
518 resource_tracker->parent->dirty_shared_list = resource_tracker;
519 resource_tracker->in_parent_dirty_list = true;
520 if (resource_tracker->parent->dirty_shared_list != nullptr) {
521 resource_tracker->parent->texture_slice_or_dirty_rect = resource_tracker->parent->texture_slice_or_dirty_rect.merge(resource_tracker->texture_slice_or_dirty_rect);
522 } else {
523 resource_tracker->parent->texture_slice_or_dirty_rect = resource_tracker->texture_slice_or_dirty_rect;
524 }
525 }
526 }
527 } else {
528 ResourceTracker *current_tracker = resource_tracker->dirty_shared_list;
529 if (current_tracker != nullptr) {
530 // Consider the usage as write if we must transition any of the slices.
531 write_usage = true;
532 }
533
534 while (current_tracker != nullptr) {
535 current_tracker->reset_if_outdated(tracking_frame);
536
537 if (current_tracker->texture_driver_id.id != 0) {
538 // Transition all slices to the layout of the parent resource.
539 _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);
540 }
541
542 current_tracker->in_parent_dirty_list = false;
543 current_tracker = current_tracker->next_shared;
544 }
545
546 resource_tracker->dirty_shared_list = nullptr;
547 }
548
549 // Use the resource's parent tracker directly for all search operations.
550 bool resource_has_parent = resource_tracker->parent != nullptr;
551 ResourceTracker *search_tracker = resource_has_parent ? resource_tracker->parent : resource_tracker;
552 bool different_usage = resource_tracker->usage != new_resource_usage;
553 bool write_usage_after_write = (write_usage && search_tracker->write_command_or_list_index >= 0);
554 if (different_usage || write_usage_after_write) {
555 // 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.
556 if (resource_tracker->texture_driver_id.id != 0) {
557 if (resource_tracker->usage_access.is_empty()) {
558 // FIXME: If the tracker does not know the previous type of usage, assume the generic memory write one.
559 // Tracking access bits across texture slices can be tricky, so this failsafe can be removed once that's improved.
560 resource_tracker->usage_access = RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT;
561 }
562
563 _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);
564 } else if (resource_tracker->buffer_driver_id.id != 0) {
565 #if USE_BUFFER_BARRIERS
566 _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);
567 #endif
568 // Memory barriers are pushed regardless of buffer barriers being used or not.
569 r_command->memory_barrier.src_access = r_command->memory_barrier.src_access | resource_tracker->usage_access;
570 r_command->memory_barrier.dst_access = r_command->memory_barrier.dst_access | new_usage_access;
571 } else {
572 DEV_ASSERT(false && "Resource tracker does not contain a valid buffer or texture ID.");
573 }
574 }
575
576 // Always update the access of the tracker according to the latest usage.
577 resource_tracker->usage_access = new_usage_access;
578
579 // Always accumulate the stages of the tracker with the commands that use it.
580 search_tracker->current_frame_stages = search_tracker->current_frame_stages | r_command->self_stages;
581
582 if (!search_tracker->previous_frame_stages.is_empty()) {
583 // Add to the command the stages the tracker was used on in the previous frame.
584 r_command->previous_stages = r_command->previous_stages | search_tracker->previous_frame_stages;
585 search_tracker->previous_frame_stages.clear();
586 }
587
588 if (different_usage) {
589 // 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.
590 // In the case of buffers however, this is not exactly necessary if the driver does not consider different buffer usages as different states.
591 write_usage = write_usage || bool(resource_tracker->texture_driver_id) || driver_buffers_require_transitions;
592 resource_tracker->usage = new_resource_usage;
593 }
594
595 bool write_usage_has_partial_coverage = !different_usage && _check_command_partial_coverage(resource_tracker, p_command_index);
596 if (search_tracker->write_command_or_list_index >= 0) {
597 if (search_tracker->write_command_list_enabled) {
598 // Make this command adjacent to any commands that wrote to this resource and intersect with the slice if it applies.
599 // For buffers or textures that never use slices, this list will only be one element long at most.
600 int32_t previous_write_list_index = -1;
601 int32_t write_list_index = search_tracker->write_command_or_list_index;
602 while (write_list_index >= 0) {
603 const RecordedSliceListNode &write_list_node = write_slice_list_nodes[write_list_index];
604 if (!resource_has_parent || search_tracker_rect.intersects(write_list_node.subresources)) {
605 if (write_list_node.command_index == p_command_index) {
606 ERR_FAIL_COND_MSG(!resource_has_parent, "Command can't have itself as a dependency.");
607 } else if (!write_list_node.partial_coverage || _check_command_intersection(resource_tracker, write_list_node.command_index, p_command_index)) {
608 _check_discardable_attachment_dependency(search_tracker, write_list_node.command_index, p_command_index);
609
610 // Command is dependent on this command. Add this command to the adjacency list of the write command.
611 _add_adjacent_command(write_list_node.command_index, p_command_index, r_command);
612
613 if (resource_has_parent && write_usage && search_tracker_rect.encloses(write_list_node.subresources) && !write_usage_has_partial_coverage) {
614 // Eliminate redundant writes from the list.
615 if (previous_write_list_index >= 0) {
616 RecordedSliceListNode &previous_list_node = write_slice_list_nodes[previous_write_list_index];
617 previous_list_node.next_list_index = write_list_node.next_list_index;
618 } else {
619 search_tracker->write_command_or_list_index = write_list_node.next_list_index;
620 }
621
622 write_list_index = write_list_node.next_list_index;
623 continue;
624 }
625 }
626 }
627
628 previous_write_list_index = write_list_index;
629 write_list_index = write_list_node.next_list_index;
630 }
631 } else {
632 // The index is just the latest command index that wrote to the resource.
633 if (search_tracker->write_command_or_list_index == p_command_index) {
634 ERR_FAIL_MSG("Command can't have itself as a dependency.");
635 } else {
636 _check_discardable_attachment_dependency(search_tracker, search_tracker->write_command_or_list_index, p_command_index);
637 _add_adjacent_command(search_tracker->write_command_or_list_index, p_command_index, r_command);
638 }
639 }
640 }
641
642 if (write_usage) {
643 bool use_write_list = resource_has_parent || write_usage_has_partial_coverage;
644 if (use_write_list) {
645 if (!search_tracker->write_command_list_enabled && search_tracker->write_command_or_list_index >= 0) {
646 // 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.
647 const RDD::TextureSubresourceRange &tracker_subresources = search_tracker->texture_subresources;
648 Rect2i tracker_rect(tracker_subresources.base_mipmap, tracker_subresources.base_layer, tracker_subresources.mipmap_count, tracker_subresources.layer_count);
649 search_tracker->write_command_or_list_index = _add_to_write_list(search_tracker->write_command_or_list_index, tracker_rect, -1, false);
650 }
651
652 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);
653 search_tracker->write_command_list_enabled = true;
654 } else {
655 search_tracker->write_command_or_list_index = p_command_index;
656 search_tracker->write_command_list_enabled = false;
657 }
658
659 // We add this command to the adjacency list of all commands that were reading from the entire resource.
660 int32_t read_full_command_list_index = search_tracker->read_full_command_list_index;
661 while (read_full_command_list_index >= 0) {
662 int32_t read_full_command_index = command_list_nodes[read_full_command_list_index].command_index;
663 int32_t read_full_next_index = command_list_nodes[read_full_command_list_index].next_list_index;
664 if (read_full_command_index == p_command_index) {
665 if (!resource_has_parent) {
666 // 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.
667 ERR_FAIL_MSG("Command can't have itself as a dependency.");
668 }
669 } else {
670 // Add this command to the adjacency list of each command that was reading this resource.
671 _add_adjacent_command(read_full_command_index, p_command_index, r_command);
672 }
673
674 read_full_command_list_index = read_full_next_index;
675 }
676
677 if (!use_write_list) {
678 // Clear the full list if this resource is not a slice.
679 search_tracker->read_full_command_list_index = -1;
680 }
681
682 // We add this command to the adjacency list of all commands that were reading from resource slices.
683 int32_t previous_slice_command_list_index = -1;
684 int32_t read_slice_command_list_index = search_tracker->read_slice_command_list_index;
685 while (read_slice_command_list_index >= 0) {
686 const RecordedSliceListNode &read_list_node = read_slice_list_nodes[read_slice_command_list_index];
687 if (!use_write_list || search_tracker_rect.encloses(read_list_node.subresources)) {
688 if (previous_slice_command_list_index >= 0) {
689 // Erase this element and connect the previous one to the next element.
690 read_slice_list_nodes[previous_slice_command_list_index].next_list_index = read_list_node.next_list_index;
691 } else {
692 // Erase this element from the head of the list.
693 DEV_ASSERT(search_tracker->read_slice_command_list_index == read_slice_command_list_index);
694 search_tracker->read_slice_command_list_index = read_list_node.next_list_index;
695 }
696
697 // Advance to the next element.
698 read_slice_command_list_index = read_list_node.next_list_index;
699 } else {
700 previous_slice_command_list_index = read_slice_command_list_index;
701 read_slice_command_list_index = read_list_node.next_list_index;
702 }
703
704 if (!resource_has_parent || search_tracker_rect.intersects(read_list_node.subresources)) {
705 // Add this command to the adjacency list of each command that was reading this resource.
706 // We only add the dependency if there's an intersection between slices or this resource isn't a slice.
707 _add_adjacent_command(read_list_node.command_index, p_command_index, r_command);
708 }
709 }
710 } else if (resource_has_parent) {
711 // We add a read dependency to the tracker to indicate this command reads from the resource slice.
712 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);
713 } else {
714 // We add a read dependency to the tracker to indicate this command reads from the entire resource.
715 search_tracker->read_full_command_list_index = _add_to_command_list(p_command_index, search_tracker->read_full_command_list_index);
716 }
717 }
718 }
719
720 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) {
721 if (!driver_honors_barriers) {
722 return;
723 }
724
725 if (r_barrier_index < 0) {
726 r_barrier_index = r_barrier_vector.size();
727 }
728
729 RDD::TextureBarrier texture_barrier;
730 texture_barrier.texture = p_texture_id;
731 texture_barrier.src_access = p_src_access;
732 texture_barrier.dst_access = p_dst_access;
733 texture_barrier.prev_layout = _usage_to_image_layout(p_prev_usage);
734 texture_barrier.next_layout = _usage_to_image_layout(p_next_usage);
735 texture_barrier.subresources = p_subresources;
736 r_barrier_vector.push_back(texture_barrier);
737 r_barrier_count++;
738 }
739
740 #if USE_BUFFER_BARRIERS
741 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) {
742 if (!driver_honors_barriers) {
743 return;
744 }
745
746 if (r_barrier_index < 0) {
747 r_barrier_index = command_buffer_barriers.size();
748 }
749
750 RDD::BufferBarrier buffer_barrier;
751 buffer_barrier.buffer = p_buffer_id;
752 buffer_barrier.src_access = p_src_access;
753 buffer_barrier.dst_access = p_dst_access;
754 buffer_barrier.offset = 0;
755 buffer_barrier.size = RDD::BUFFER_WHOLE_SIZE;
756 command_buffer_barriers.push_back(buffer_barrier);
757 r_barrier_count++;
758 }
759 #endif
760
761 void RenderingDeviceGraph::_run_compute_list_command(RDD::CommandBufferID p_command_buffer, const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {
762 uint32_t instruction_data_cursor = 0;
763 while (instruction_data_cursor < p_instruction_data_size) {
764 DEV_ASSERT((instruction_data_cursor + sizeof(ComputeListInstruction)) <= p_instruction_data_size);
765
766 const ComputeListInstruction *instruction = reinterpret_cast<const ComputeListInstruction *>(&p_instruction_data[instruction_data_cursor]);
767 switch (instruction->type) {
768 case ComputeListInstruction::TYPE_BIND_PIPELINE: {
769 const ComputeListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const ComputeListBindPipelineInstruction *>(instruction);
770 driver->command_bind_compute_pipeline(p_command_buffer, bind_pipeline_instruction->pipeline);
771 instruction_data_cursor += sizeof(ComputeListBindPipelineInstruction);
772 } break;
773 case ComputeListInstruction::TYPE_BIND_UNIFORM_SETS: {
774 const ComputeListBindUniformSetsInstruction *bind_uniform_sets_instruction = reinterpret_cast<const ComputeListBindUniformSetsInstruction *>(instruction);
775 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);
776 instruction_data_cursor += sizeof(ComputeListBindUniformSetsInstruction) + sizeof(RDD::UniformSetID) * bind_uniform_sets_instruction->set_count;
777 } break;
778 case ComputeListInstruction::TYPE_DISPATCH: {
779 const ComputeListDispatchInstruction *dispatch_instruction = reinterpret_cast<const ComputeListDispatchInstruction *>(instruction);
780 driver->command_compute_dispatch(p_command_buffer, dispatch_instruction->x_groups, dispatch_instruction->y_groups, dispatch_instruction->z_groups);
781 instruction_data_cursor += sizeof(ComputeListDispatchInstruction);
782 } break;
783 case ComputeListInstruction::TYPE_DISPATCH_INDIRECT: {
784 const ComputeListDispatchIndirectInstruction *dispatch_indirect_instruction = reinterpret_cast<const ComputeListDispatchIndirectInstruction *>(instruction);
785 driver->command_compute_dispatch_indirect(p_command_buffer, dispatch_indirect_instruction->buffer, dispatch_indirect_instruction->offset);
786 instruction_data_cursor += sizeof(ComputeListDispatchIndirectInstruction);
787 } break;
788 case ComputeListInstruction::TYPE_SET_PUSH_CONSTANT: {
789 const ComputeListSetPushConstantInstruction *set_push_constant_instruction = reinterpret_cast<const ComputeListSetPushConstantInstruction *>(instruction);
790 const VectorView push_constant_data_view(reinterpret_cast<const uint32_t *>(set_push_constant_instruction->data()), set_push_constant_instruction->size / sizeof(uint32_t));
791 driver->command_bind_push_constants(p_command_buffer, set_push_constant_instruction->shader, 0, push_constant_data_view);
792 instruction_data_cursor += sizeof(ComputeListSetPushConstantInstruction);
793 instruction_data_cursor += set_push_constant_instruction->size;
794 } break;
795 case ComputeListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE: {
796 const ComputeListUniformSetPrepareForUseInstruction *uniform_set_prepare_for_use_instruction = reinterpret_cast<const ComputeListUniformSetPrepareForUseInstruction *>(instruction);
797 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);
798 instruction_data_cursor += sizeof(ComputeListUniformSetPrepareForUseInstruction);
799 } break;
800 default:
801 DEV_ASSERT(false && "Unknown compute list instruction type.");
802 return;
803 }
804
805 instruction_data_cursor = GRAPH_ALIGN(instruction_data_cursor);
806 }
807 }
808
809 void RenderingDeviceGraph::_get_draw_list_render_pass_and_framebuffer(const RecordedDrawListCommand *p_draw_list_command, RDD::RenderPassID &r_render_pass, RDD::FramebufferID &r_framebuffer) {
810 DEV_ASSERT(p_draw_list_command->trackers_count <= 21 && "Max number of attachments that can be encoded into the key.");
811
812 // Build a unique key from the load and store ops for each attachment.
813 const RDD::AttachmentLoadOp *load_ops = p_draw_list_command->load_ops();
814 const RDD::AttachmentStoreOp *store_ops = p_draw_list_command->store_ops();
815 uint64_t key = 0;
816 for (uint32_t i = 0; i < p_draw_list_command->trackers_count; i++) {
817 key |= uint64_t(load_ops[i]) << (i * 3);
818 key |= uint64_t(store_ops[i]) << (i * 3 + 2);
819 }
820
821 // Check the storage map if the render pass and the framebuffer needs to be created.
822 FramebufferCache *framebuffer_cache = p_draw_list_command->framebuffer_cache;
823 HashMap<uint64_t, FramebufferStorage>::Iterator it = framebuffer_cache->storage_map.find(key);
824 if (it == framebuffer_cache->storage_map.end()) {
825 FramebufferStorage storage;
826 VectorView<RDD::AttachmentLoadOp> load_ops_view(load_ops, p_draw_list_command->trackers_count);
827 VectorView<RDD::AttachmentStoreOp> store_ops_view(store_ops, p_draw_list_command->trackers_count);
828 storage.render_pass = render_pass_creation_function(driver, load_ops_view, store_ops_view, framebuffer_cache->render_pass_creation_user_data);
829 ERR_FAIL_COND(!storage.render_pass);
830
831 storage.framebuffer = driver->framebuffer_create(storage.render_pass, framebuffer_cache->textures, framebuffer_cache->width, framebuffer_cache->height);
832 ERR_FAIL_COND(!storage.framebuffer);
833
834 it = framebuffer_cache->storage_map.insert(key, storage);
835 }
836
837 r_render_pass = it->value.render_pass;
838 r_framebuffer = it->value.framebuffer;
839 }
840
841 #if PRINT_DRAW_LIST_STATS
842 static uint32_t draw_list_total_size = 0;
843 #endif
844
845 void RenderingDeviceGraph::_run_draw_list_command(RDD::CommandBufferID p_command_buffer, const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {
846 #if PRINT_DRAW_LIST_STATS
847 draw_list_total_size += p_instruction_data_size;
848 #endif
849
850 uint32_t instruction_data_cursor = 0;
851 while (instruction_data_cursor < p_instruction_data_size) {
852 DEV_ASSERT((instruction_data_cursor + sizeof(DrawListInstruction)) <= p_instruction_data_size);
853
854 const DrawListInstruction *instruction = reinterpret_cast<const DrawListInstruction *>(&p_instruction_data[instruction_data_cursor]);
855 switch (instruction->type) {
856 case DrawListInstruction::TYPE_BIND_INDEX_BUFFER: {
857 const DrawListBindIndexBufferInstruction *bind_index_buffer_instruction = reinterpret_cast<const DrawListBindIndexBufferInstruction *>(instruction);
858 driver->command_render_bind_index_buffer(p_command_buffer, bind_index_buffer_instruction->buffer, bind_index_buffer_instruction->format, bind_index_buffer_instruction->offset);
859 instruction_data_cursor += sizeof(DrawListBindIndexBufferInstruction);
860 } break;
861 case DrawListInstruction::TYPE_BIND_PIPELINE: {
862 const DrawListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const DrawListBindPipelineInstruction *>(instruction);
863 driver->command_bind_render_pipeline(p_command_buffer, bind_pipeline_instruction->pipeline);
864 instruction_data_cursor += sizeof(DrawListBindPipelineInstruction);
865 } break;
866 case DrawListInstruction::TYPE_BIND_UNIFORM_SETS: {
867 const DrawListBindUniformSetsInstruction *bind_uniform_sets_instruction = reinterpret_cast<const DrawListBindUniformSetsInstruction *>(instruction);
868 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);
869 instruction_data_cursor += sizeof(DrawListBindUniformSetsInstruction) + sizeof(RDD::UniformSetID) * bind_uniform_sets_instruction->set_count;
870 } break;
871 case DrawListInstruction::TYPE_BIND_VERTEX_BUFFERS: {
872 const DrawListBindVertexBuffersInstruction *bind_vertex_buffers_instruction = reinterpret_cast<const DrawListBindVertexBuffersInstruction *>(instruction);
873 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);
874 instruction_data_cursor += sizeof(DrawListBindVertexBuffersInstruction);
875 instruction_data_cursor += sizeof(RDD::BufferID) * bind_vertex_buffers_instruction->vertex_buffers_count;
876 instruction_data_cursor += sizeof(uint64_t) * bind_vertex_buffers_instruction->vertex_buffers_count;
877 } break;
878 case DrawListInstruction::TYPE_CLEAR_ATTACHMENTS: {
879 const DrawListClearAttachmentsInstruction *clear_attachments_instruction = reinterpret_cast<const DrawListClearAttachmentsInstruction *>(instruction);
880 const VectorView attachments_clear_view(clear_attachments_instruction->attachments_clear(), clear_attachments_instruction->attachments_clear_count);
881 const VectorView attachments_clear_rect_view(clear_attachments_instruction->attachments_clear_rect(), clear_attachments_instruction->attachments_clear_rect_count);
882 driver->command_render_clear_attachments(p_command_buffer, attachments_clear_view, attachments_clear_rect_view);
883 instruction_data_cursor += sizeof(DrawListClearAttachmentsInstruction);
884 instruction_data_cursor += sizeof(RDD::AttachmentClear) * clear_attachments_instruction->attachments_clear_count;
885 instruction_data_cursor += sizeof(Rect2i) * clear_attachments_instruction->attachments_clear_rect_count;
886 } break;
887 case DrawListInstruction::TYPE_DRAW: {
888 const DrawListDrawInstruction *draw_instruction = reinterpret_cast<const DrawListDrawInstruction *>(instruction);
889 driver->command_render_draw(p_command_buffer, draw_instruction->vertex_count, draw_instruction->instance_count, 0, 0);
890 instruction_data_cursor += sizeof(DrawListDrawInstruction);
891 } break;
892 case DrawListInstruction::TYPE_DRAW_INDEXED: {
893 const DrawListDrawIndexedInstruction *draw_indexed_instruction = reinterpret_cast<const DrawListDrawIndexedInstruction *>(instruction);
894 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);
895 instruction_data_cursor += sizeof(DrawListDrawIndexedInstruction);
896 } break;
897 case DrawListInstruction::TYPE_DRAW_INDIRECT: {
898 const DrawListDrawIndirectInstruction *draw_indirect_instruction = reinterpret_cast<const DrawListDrawIndirectInstruction *>(instruction);
899 driver->command_render_draw_indirect(p_command_buffer, draw_indirect_instruction->buffer, draw_indirect_instruction->offset, draw_indirect_instruction->draw_count, draw_indirect_instruction->stride);
900 instruction_data_cursor += sizeof(DrawListDrawIndirectInstruction);
901 } break;
902 case DrawListInstruction::TYPE_DRAW_INDEXED_INDIRECT: {
903 const DrawListDrawIndexedIndirectInstruction *draw_indexed_indirect_instruction = reinterpret_cast<const DrawListDrawIndexedIndirectInstruction *>(instruction);
904 driver->command_render_draw_indexed_indirect(p_command_buffer, draw_indexed_indirect_instruction->buffer, draw_indexed_indirect_instruction->offset, draw_indexed_indirect_instruction->draw_count, draw_indexed_indirect_instruction->stride);
905 instruction_data_cursor += sizeof(DrawListDrawIndexedIndirectInstruction);
906 } break;
907 case DrawListInstruction::TYPE_EXECUTE_COMMANDS: {
908 const DrawListExecuteCommandsInstruction *execute_commands_instruction = reinterpret_cast<const DrawListExecuteCommandsInstruction *>(instruction);
909 driver->command_buffer_execute_secondary(p_command_buffer, execute_commands_instruction->command_buffer);
910 instruction_data_cursor += sizeof(DrawListExecuteCommandsInstruction);
911 } break;
912 case DrawListInstruction::TYPE_NEXT_SUBPASS: {
913 const DrawListNextSubpassInstruction *next_subpass_instruction = reinterpret_cast<const DrawListNextSubpassInstruction *>(instruction);
914 driver->command_next_render_subpass(p_command_buffer, next_subpass_instruction->command_buffer_type);
915 instruction_data_cursor += sizeof(DrawListNextSubpassInstruction);
916 } break;
917 case DrawListInstruction::TYPE_SET_BLEND_CONSTANTS: {
918 const DrawListSetBlendConstantsInstruction *set_blend_constants_instruction = reinterpret_cast<const DrawListSetBlendConstantsInstruction *>(instruction);
919 driver->command_render_set_blend_constants(p_command_buffer, set_blend_constants_instruction->color);
920 instruction_data_cursor += sizeof(DrawListSetBlendConstantsInstruction);
921 } break;
922 case DrawListInstruction::TYPE_SET_LINE_WIDTH: {
923 const DrawListSetLineWidthInstruction *set_line_width_instruction = reinterpret_cast<const DrawListSetLineWidthInstruction *>(instruction);
924 driver->command_render_set_line_width(p_command_buffer, set_line_width_instruction->width);
925 instruction_data_cursor += sizeof(DrawListSetLineWidthInstruction);
926 } break;
927 case DrawListInstruction::TYPE_SET_PUSH_CONSTANT: {
928 const DrawListSetPushConstantInstruction *set_push_constant_instruction = reinterpret_cast<const DrawListSetPushConstantInstruction *>(instruction);
929 const VectorView push_constant_data_view(reinterpret_cast<const uint32_t *>(set_push_constant_instruction->data()), set_push_constant_instruction->size / sizeof(uint32_t));
930 driver->command_bind_push_constants(p_command_buffer, set_push_constant_instruction->shader, 0, push_constant_data_view);
931 instruction_data_cursor += sizeof(DrawListSetPushConstantInstruction);
932 instruction_data_cursor += set_push_constant_instruction->size;
933 } break;
934 case DrawListInstruction::TYPE_SET_SCISSOR: {
935 const DrawListSetScissorInstruction *set_scissor_instruction = reinterpret_cast<const DrawListSetScissorInstruction *>(instruction);
936 driver->command_render_set_scissor(p_command_buffer, set_scissor_instruction->rect);
937 instruction_data_cursor += sizeof(DrawListSetScissorInstruction);
938 } break;
939 case DrawListInstruction::TYPE_SET_VIEWPORT: {
940 const DrawListSetViewportInstruction *set_viewport_instruction = reinterpret_cast<const DrawListSetViewportInstruction *>(instruction);
941 driver->command_render_set_viewport(p_command_buffer, set_viewport_instruction->rect);
942 instruction_data_cursor += sizeof(DrawListSetViewportInstruction);
943 } break;
944 case DrawListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE: {
945 const DrawListUniformSetPrepareForUseInstruction *uniform_set_prepare_for_use_instruction = reinterpret_cast<const DrawListUniformSetPrepareForUseInstruction *>(instruction);
946 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);
947 instruction_data_cursor += sizeof(DrawListUniformSetPrepareForUseInstruction);
948 } break;
949 default:
950 DEV_ASSERT(false && "Unknown draw list instruction type.");
951 return;
952 }
953
954 instruction_data_cursor = GRAPH_ALIGN(instruction_data_cursor);
955 }
956 }
957
958 void RenderingDeviceGraph::_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) {
959 DEV_ASSERT(p_attachment_operations.size() == p_attachment_clear_values.size());
960
961 draw_instruction_list.clear();
962 draw_instruction_list.index++;
963 draw_instruction_list.framebuffer_cache = p_framebuffer_cache;
964 draw_instruction_list.render_pass = p_render_pass;
965 draw_instruction_list.framebuffer = p_framebuffer;
966 draw_instruction_list.region = p_region;
967 draw_instruction_list.stages = p_stages;
968 draw_instruction_list.attachment_operations.resize(p_attachment_operations.size());
969 draw_instruction_list.attachment_clear_values.resize(p_attachment_clear_values.size());
970
971 for (uint32_t i = 0; i < p_attachment_operations.size(); i++) {
972 draw_instruction_list.attachment_operations[i] = p_attachment_operations[i];
973 draw_instruction_list.attachment_clear_values[i] = p_attachment_clear_values[i];
974 }
975
976 draw_instruction_list.split_cmd_buffer = p_split_cmd_buffer;
977
978 #if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
979 draw_instruction_list.breadcrumb = p_breadcrumb;
980 #endif
981 }
982
983 void RenderingDeviceGraph::_run_secondary_command_buffer_task(const SecondaryCommandBuffer *p_secondary) {
984 driver->command_buffer_begin_secondary(p_secondary->command_buffer, p_secondary->render_pass, 0, p_secondary->framebuffer);
985 _run_draw_list_command(p_secondary->command_buffer, p_secondary->instruction_data.ptr(), p_secondary->instruction_data.size());
986 driver->command_buffer_end(p_secondary->command_buffer);
987 }
988
989 void RenderingDeviceGraph::_wait_for_secondary_command_buffer_tasks() {
990 for (uint32_t i = 0; i < frames[frame].secondary_command_buffers_used; i++) {
991 WorkerThreadPool::TaskID &task = frames[frame].secondary_command_buffers[i].task;
992 if (task != WorkerThreadPool::INVALID_TASK_ID) {
993 WorkerThreadPool::get_singleton()->wait_for_task_completion(task);
994 task = WorkerThreadPool::INVALID_TASK_ID;
995 }
996 }
997 }
998
999 void RenderingDeviceGraph::_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) {
1000 for (uint32_t i = 0; i < p_sorted_commands_count; i++) {
1001 const uint32_t command_index = p_sorted_commands[i].index;
1002 const uint32_t command_data_offset = command_data_offsets[command_index];
1003 const RecordedCommand *command = reinterpret_cast<const RecordedCommand *>(&command_data[command_data_offset]);
1004 _run_label_command_change(r_command_buffer, command->label_index, p_level, false, true, &p_sorted_commands[i], p_sorted_commands_count - i, r_current_label_index, r_current_label_level);
1005
1006 switch (command->type) {
1007 case RecordedCommand::TYPE_BUFFER_CLEAR: {
1008 const RecordedBufferClearCommand *buffer_clear_command = reinterpret_cast<const RecordedBufferClearCommand *>(command);
1009 driver->command_clear_buffer(r_command_buffer, buffer_clear_command->buffer, buffer_clear_command->offset, buffer_clear_command->size);
1010 } break;
1011 case RecordedCommand::TYPE_BUFFER_COPY: {
1012 const RecordedBufferCopyCommand *buffer_copy_command = reinterpret_cast<const RecordedBufferCopyCommand *>(command);
1013 driver->command_copy_buffer(r_command_buffer, buffer_copy_command->source, buffer_copy_command->destination, buffer_copy_command->region);
1014 } break;
1015 case RecordedCommand::TYPE_BUFFER_GET_DATA: {
1016 const RecordedBufferGetDataCommand *buffer_get_data_command = reinterpret_cast<const RecordedBufferGetDataCommand *>(command);
1017 driver->command_copy_buffer(r_command_buffer, buffer_get_data_command->source, buffer_get_data_command->destination, buffer_get_data_command->region);
1018 } break;
1019 case RecordedCommand::TYPE_BUFFER_UPDATE: {
1020 const RecordedBufferUpdateCommand *buffer_update_command = reinterpret_cast<const RecordedBufferUpdateCommand *>(command);
1021 const RecordedBufferCopy *command_buffer_copies = buffer_update_command->buffer_copies();
1022 for (uint32_t j = 0; j < buffer_update_command->buffer_copies_count; j++) {
1023 driver->command_copy_buffer(r_command_buffer, command_buffer_copies[j].source, buffer_update_command->destination, command_buffer_copies[j].region);
1024 }
1025 } break;
1026 case RecordedCommand::TYPE_DRIVER_CALLBACK: {
1027 const RecordedDriverCallbackCommand *driver_callback_command = reinterpret_cast<const RecordedDriverCallbackCommand *>(command);
1028 driver_callback_command->callback(driver, r_command_buffer, driver_callback_command->userdata);
1029 } break;
1030 case RecordedCommand::TYPE_COMPUTE_LIST: {
1031 if (device.workarounds.avoid_compute_after_draw && workarounds_state.draw_list_found) {
1032 // Avoid compute after draw workaround. Refer to the comment that enables this in the Vulkan driver for more information.
1033 workarounds_state.draw_list_found = false;
1034
1035 // Create or reuse a command buffer and finish recording the current one.
1036 driver->command_buffer_end(r_command_buffer);
1037
1038 while (r_command_buffer_pool.buffers_used >= r_command_buffer_pool.buffers.size()) {
1039 RDD::CommandBufferID command_buffer = driver->command_buffer_create(r_command_buffer_pool.pool);
1040 RDD::SemaphoreID command_semaphore = driver->semaphore_create();
1041 r_command_buffer_pool.buffers.push_back(command_buffer);
1042 r_command_buffer_pool.semaphores.push_back(command_semaphore);
1043 }
1044
1045 // Start recording on the next usable command buffer from the pool.
1046 uint32_t command_buffer_index = r_command_buffer_pool.buffers_used++;
1047 r_command_buffer = r_command_buffer_pool.buffers[command_buffer_index];
1048 driver->command_buffer_begin(r_command_buffer);
1049 }
1050
1051 const RecordedComputeListCommand *compute_list_command = reinterpret_cast<const RecordedComputeListCommand *>(command);
1052 _run_compute_list_command(r_command_buffer, compute_list_command->instruction_data(), compute_list_command->instruction_data_size);
1053 } break;
1054 case RecordedCommand::TYPE_DRAW_LIST: {
1055 if (device.workarounds.avoid_compute_after_draw) {
1056 // Indicate that a draw list was encountered for the workaround.
1057 workarounds_state.draw_list_found = true;
1058 }
1059
1060 const RecordedDrawListCommand *draw_list_command = reinterpret_cast<const RecordedDrawListCommand *>(command);
1061
1062 if (draw_list_command->split_cmd_buffer) {
1063 // Create or reuse a command buffer and finish recording the current one.
1064 driver->command_buffer_end(r_command_buffer);
1065
1066 while (r_command_buffer_pool.buffers_used >= r_command_buffer_pool.buffers.size()) {
1067 RDD::CommandBufferID command_buffer = driver->command_buffer_create(r_command_buffer_pool.pool);
1068 RDD::SemaphoreID command_semaphore = driver->semaphore_create();
1069 r_command_buffer_pool.buffers.push_back(command_buffer);
1070 r_command_buffer_pool.semaphores.push_back(command_semaphore);
1071 }
1072
1073 // Start recording on the next usable command buffer from the pool.
1074 uint32_t command_buffer_index = r_command_buffer_pool.buffers_used++;
1075 r_command_buffer = r_command_buffer_pool.buffers[command_buffer_index];
1076 driver->command_buffer_begin(r_command_buffer);
1077 }
1078
1079 const VectorView clear_values(draw_list_command->clear_values(), draw_list_command->clear_values_count);
1080 #if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
1081 driver->command_insert_breadcrumb(r_command_buffer, draw_list_command->breadcrumb);
1082 #endif
1083 RDD::RenderPassID render_pass;
1084 RDD::FramebufferID framebuffer;
1085 if (draw_list_command->framebuffer_cache != nullptr) {
1086 _get_draw_list_render_pass_and_framebuffer(draw_list_command, render_pass, framebuffer);
1087 } else {
1088 render_pass = draw_list_command->render_pass;
1089 framebuffer = draw_list_command->framebuffer;
1090 }
1091
1092 if (framebuffer && render_pass) {
1093 driver->command_begin_render_pass(r_command_buffer, render_pass, framebuffer, draw_list_command->command_buffer_type, draw_list_command->region, clear_values);
1094 _run_draw_list_command(r_command_buffer, draw_list_command->instruction_data(), draw_list_command->instruction_data_size);
1095 driver->command_end_render_pass(r_command_buffer);
1096 }
1097 } break;
1098 case RecordedCommand::TYPE_TEXTURE_CLEAR_COLOR: {
1099 const RecordedTextureClearColorCommand *texture_clear_color_command = reinterpret_cast<const RecordedTextureClearColorCommand *>(command);
1100 driver->command_clear_color_texture(r_command_buffer, texture_clear_color_command->texture, RDD::TEXTURE_LAYOUT_COPY_DST_OPTIMAL, texture_clear_color_command->color, texture_clear_color_command->range);
1101 } break;
1102 case RecordedCommand::TYPE_TEXTURE_CLEAR_DEPTH_STENCIL: {
1103 const RecordedTextureClearDepthStencilCommand *texture_clear_depth_stencil_command = reinterpret_cast<const RecordedTextureClearDepthStencilCommand *>(command);
1104 driver->command_clear_depth_stencil_texture(r_command_buffer, texture_clear_depth_stencil_command->texture, RDD::TEXTURE_LAYOUT_COPY_DST_OPTIMAL, texture_clear_depth_stencil_command->depth, texture_clear_depth_stencil_command->stencil, texture_clear_depth_stencil_command->range);
1105 } break;
1106 case RecordedCommand::TYPE_TEXTURE_COPY: {
1107 const RecordedTextureCopyCommand *texture_copy_command = reinterpret_cast<const RecordedTextureCopyCommand *>(command);
1108 const VectorView<RDD::TextureCopyRegion> command_texture_copy_regions_view(texture_copy_command->texture_copy_regions(), texture_copy_command->texture_copy_regions_count);
1109 driver->command_copy_texture(r_command_buffer, texture_copy_command->from_texture, RDD::TEXTURE_LAYOUT_COPY_SRC_OPTIMAL, texture_copy_command->to_texture, RDD::TEXTURE_LAYOUT_COPY_DST_OPTIMAL, command_texture_copy_regions_view);
1110 } break;
1111 case RecordedCommand::TYPE_TEXTURE_GET_DATA: {
1112 const RecordedTextureGetDataCommand *texture_get_data_command = reinterpret_cast<const RecordedTextureGetDataCommand *>(command);
1113 const VectorView<RDD::BufferTextureCopyRegion> command_buffer_texture_copy_regions_view(texture_get_data_command->buffer_texture_copy_regions(), texture_get_data_command->buffer_texture_copy_regions_count);
1114 driver->command_copy_texture_to_buffer(r_command_buffer, texture_get_data_command->from_texture, RDD::TEXTURE_LAYOUT_COPY_SRC_OPTIMAL, texture_get_data_command->to_buffer, command_buffer_texture_copy_regions_view);
1115 } break;
1116 case RecordedCommand::TYPE_TEXTURE_RESOLVE: {
1117 const RecordedTextureResolveCommand *texture_resolve_command = reinterpret_cast<const RecordedTextureResolveCommand *>(command);
1118 driver->command_resolve_texture(r_command_buffer, texture_resolve_command->from_texture, RDD::TEXTURE_LAYOUT_RESOLVE_SRC_OPTIMAL, texture_resolve_command->src_layer, texture_resolve_command->src_mipmap, texture_resolve_command->to_texture, RDD::TEXTURE_LAYOUT_RESOLVE_DST_OPTIMAL, texture_resolve_command->dst_layer, texture_resolve_command->dst_mipmap);
1119 } break;
1120 case RecordedCommand::TYPE_TEXTURE_UPDATE: {
1121 const RecordedTextureUpdateCommand *texture_update_command = reinterpret_cast<const RecordedTextureUpdateCommand *>(command);
1122 const RecordedBufferToTextureCopy *command_buffer_to_texture_copies = texture_update_command->buffer_to_texture_copies();
1123 for (uint32_t j = 0; j < texture_update_command->buffer_to_texture_copies_count; j++) {
1124 driver->command_copy_buffer_to_texture(r_command_buffer, command_buffer_to_texture_copies[j].from_buffer, texture_update_command->to_texture, RDD::TEXTURE_LAYOUT_COPY_DST_OPTIMAL, command_buffer_to_texture_copies[j].region);
1125 }
1126 } break;
1127 case RecordedCommand::TYPE_CAPTURE_TIMESTAMP: {
1128 const RecordedCaptureTimestampCommand *texture_capture_timestamp_command = reinterpret_cast<const RecordedCaptureTimestampCommand *>(command);
1129 driver->command_timestamp_write(r_command_buffer, texture_capture_timestamp_command->pool, texture_capture_timestamp_command->index);
1130 } break;
1131 default: {
1132 DEV_ASSERT(false && "Unknown recorded command type.");
1133 return;
1134 }
1135 }
1136 }
1137 }
1138
1139 void RenderingDeviceGraph::_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) {
1140 if (command_label_count == 0) {
1141 // Ignore any label operations if no labels were pushed.
1142 return;
1143 }
1144
1145 if (p_ignore_previous_value || p_new_label_index != r_current_label_index || p_new_level != r_current_label_level) {
1146 if (!p_ignore_previous_value && (p_use_label_for_empty || r_current_label_index >= 0 || r_current_label_level >= 0)) {
1147 // End the current label.
1148 driver->command_end_label(p_command_buffer);
1149 }
1150
1151 String label_name;
1152 Color label_color;
1153 if (p_new_label_index >= 0) {
1154 const char *label_chars = &command_label_chars[command_label_offsets[p_new_label_index]];
1155 label_name.append_utf8(label_chars);
1156 label_color = command_label_colors[p_new_label_index];
1157 } else if (p_use_label_for_empty) {
1158 label_name = "Command Graph";
1159 label_color = Color(1, 1, 1, 1);
1160 } else {
1161 return;
1162 }
1163
1164 // Add the level to the name.
1165 label_name += " (L" + itos(p_new_level) + ")";
1166
1167 if (p_sorted_commands != nullptr && p_sorted_commands_count > 0) {
1168 // Analyze the commands in the level that have the same label to detect what type of operations are performed.
1169 bool copy_commands = false;
1170 bool compute_commands = false;
1171 bool draw_commands = false;
1172 bool custom_commands = false;
1173 for (uint32_t i = 0; i < p_sorted_commands_count; i++) {
1174 const uint32_t command_index = p_sorted_commands[i].index;
1175 const uint32_t command_data_offset = command_data_offsets[command_index];
1176 const RecordedCommand *command = reinterpret_cast<RecordedCommand *>(&command_data[command_data_offset]);
1177 if (command->label_index != p_new_label_index) {
1178 break;
1179 }
1180
1181 switch (command->type) {
1182 case RecordedCommand::TYPE_BUFFER_CLEAR:
1183 case RecordedCommand::TYPE_BUFFER_COPY:
1184 case RecordedCommand::TYPE_BUFFER_GET_DATA:
1185 case RecordedCommand::TYPE_BUFFER_UPDATE:
1186 case RecordedCommand::TYPE_TEXTURE_CLEAR_COLOR:
1187 case RecordedCommand::TYPE_TEXTURE_CLEAR_DEPTH_STENCIL:
1188 case RecordedCommand::TYPE_TEXTURE_COPY:
1189 case RecordedCommand::TYPE_TEXTURE_GET_DATA:
1190 case RecordedCommand::TYPE_TEXTURE_RESOLVE:
1191 case RecordedCommand::TYPE_TEXTURE_UPDATE: {
1192 copy_commands = true;
1193 } break;
1194 case RecordedCommand::TYPE_COMPUTE_LIST: {
1195 compute_commands = true;
1196 } break;
1197 case RecordedCommand::TYPE_DRAW_LIST: {
1198 draw_commands = true;
1199 } break;
1200 case RecordedCommand::TYPE_DRIVER_CALLBACK: {
1201 custom_commands = true;
1202 } break;
1203 default: {
1204 // Ignore command.
1205 } break;
1206 }
1207
1208 if (copy_commands && compute_commands && draw_commands && custom_commands) {
1209 // There's no more command types to find.
1210 break;
1211 }
1212 }
1213
1214 if (copy_commands || compute_commands || draw_commands || custom_commands) {
1215 // Add the operations to the name.
1216 bool plus_after_copy = copy_commands && (compute_commands || draw_commands || custom_commands);
1217 bool plus_after_compute = compute_commands && (draw_commands || custom_commands);
1218 bool plus_after_draw = draw_commands && custom_commands;
1219 label_name += " (";
1220 label_name += copy_commands ? "Copy" : "";
1221 label_name += plus_after_copy ? "+" : "";
1222 label_name += compute_commands ? "Compute" : "";
1223 label_name += plus_after_compute ? "+" : "";
1224 label_name += draw_commands ? "Draw" : "";
1225 label_name += plus_after_draw ? "+" : "";
1226 label_name += custom_commands ? "Custom" : "";
1227 label_name += ")";
1228 }
1229 }
1230
1231 // Start the new label.
1232 CharString label_name_utf8 = label_name.utf8();
1233 driver->command_begin_label(p_command_buffer, label_name_utf8.get_data(), label_color);
1234
1235 r_current_label_index = p_new_label_index;
1236 r_current_label_level = p_new_level;
1237 }
1238 }
1239
1240 void RenderingDeviceGraph::_boost_priority_for_render_commands(RecordedCommandSort *p_sorted_commands, uint32_t p_sorted_commands_count, uint32_t &r_boosted_priority) {
1241 if (p_sorted_commands_count == 0) {
1242 return;
1243 }
1244
1245 const uint32_t boosted_priority_value = 0;
1246 if (r_boosted_priority > 0) {
1247 bool perform_sort = false;
1248 for (uint32_t j = 0; j < p_sorted_commands_count; j++) {
1249 if (p_sorted_commands[j].priority == r_boosted_priority) {
1250 p_sorted_commands[j].priority = boosted_priority_value;
1251 perform_sort = true;
1252 }
1253 }
1254
1255 if (perform_sort) {
1256 SortArray<RecordedCommandSort> command_sorter;
1257 command_sorter.sort(p_sorted_commands, p_sorted_commands_count);
1258 }
1259 }
1260
1261 if (p_sorted_commands[p_sorted_commands_count - 1].priority != boosted_priority_value) {
1262 r_boosted_priority = p_sorted_commands[p_sorted_commands_count - 1].priority;
1263 }
1264 }
1265
1266 void RenderingDeviceGraph::_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) {
1267 if (!driver_honors_barriers) {
1268 return;
1269 }
1270
1271 barrier_group.clear();
1272 barrier_group.src_stages = RDD::PIPELINE_STAGE_TOP_OF_PIPE_BIT;
1273 barrier_group.dst_stages = RDD::PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
1274
1275 for (uint32_t i = 0; i < p_sorted_commands_count; i++) {
1276 const uint32_t command_index = p_sorted_commands[i].index;
1277 const uint32_t command_data_offset = command_data_offsets[command_index];
1278 const RecordedCommand *command = reinterpret_cast<RecordedCommand *>(&command_data[command_data_offset]);
1279
1280 #if PRINT_COMMAND_RECORDING
1281 print_line(vformat("Grouping barriers for #%d", command_index));
1282 #endif
1283
1284 // Merge command's stage bits with the barrier group.
1285 barrier_group.src_stages = barrier_group.src_stages | command->previous_stages;
1286 barrier_group.dst_stages = barrier_group.dst_stages | command->next_stages;
1287
1288 // Merge command's memory barrier bits with the barrier group.
1289 barrier_group.memory_barrier.src_access = barrier_group.memory_barrier.src_access | command->memory_barrier.src_access;
1290 barrier_group.memory_barrier.dst_access = barrier_group.memory_barrier.dst_access | command->memory_barrier.dst_access;
1291
1292 // Gather texture barriers.
1293 for (int32_t j = 0; j < command->normalization_barrier_count; j++) {
1294 const RDD::TextureBarrier &recorded_barrier = command_normalization_barriers[command->normalization_barrier_index + j];
1295 barrier_group.normalization_barriers.push_back(recorded_barrier);
1296 #if PRINT_COMMAND_RECORDING
1297 print_line(vformat("Normalization Barrier #%d", barrier_group.normalization_barriers.size() - 1));
1298 #endif
1299 }
1300
1301 for (int32_t j = 0; j < command->transition_barrier_count; j++) {
1302 const RDD::TextureBarrier &recorded_barrier = command_transition_barriers[command->transition_barrier_index + j];
1303 barrier_group.transition_barriers.push_back(recorded_barrier);
1304 #if PRINT_COMMAND_RECORDING
1305 print_line(vformat("Transition Barrier #%d", barrier_group.transition_barriers.size() - 1));
1306 #endif
1307 }
1308
1309 #if USE_BUFFER_BARRIERS
1310 // Gather buffer barriers.
1311 for (int32_t j = 0; j < command->buffer_barrier_count; j++) {
1312 const RDD::BufferBarrier &recorded_barrier = command_buffer_barriers[command->buffer_barrier_index + j];
1313 barrier_group.buffer_barriers.push_back(recorded_barrier);
1314 }
1315 #endif
1316 }
1317
1318 if (p_full_memory_barrier) {
1319 barrier_group.src_stages = RDD::PIPELINE_STAGE_ALL_COMMANDS_BIT;
1320 barrier_group.dst_stages = RDD::PIPELINE_STAGE_ALL_COMMANDS_BIT;
1321 barrier_group.memory_barrier.src_access = RDD::BARRIER_ACCESS_MEMORY_READ_BIT | RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT;
1322 barrier_group.memory_barrier.dst_access = RDD::BARRIER_ACCESS_MEMORY_READ_BIT | RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT;
1323 }
1324
1325 const bool is_memory_barrier_empty = barrier_group.memory_barrier.src_access.is_empty() && barrier_group.memory_barrier.dst_access.is_empty();
1326 const bool are_texture_barriers_empty = barrier_group.normalization_barriers.is_empty() && barrier_group.transition_barriers.is_empty();
1327 #if USE_BUFFER_BARRIERS
1328 const bool are_buffer_barriers_empty = barrier_group.buffer_barriers.is_empty();
1329 #else
1330 const bool are_buffer_barriers_empty = true;
1331 #endif
1332 if (is_memory_barrier_empty && are_texture_barriers_empty && are_buffer_barriers_empty) {
1333 // Commands don't require synchronization.
1334 return;
1335 }
1336
1337 const VectorView<RDD::MemoryAccessBarrier> memory_barriers = !is_memory_barrier_empty ? barrier_group.memory_barrier : VectorView<RDD::MemoryAccessBarrier>();
1338 const VectorView<RDD::TextureBarrier> texture_barriers = barrier_group.normalization_barriers.is_empty() ? barrier_group.transition_barriers : barrier_group.normalization_barriers;
1339 #if USE_BUFFER_BARRIERS
1340 const VectorView<RDD::BufferBarrier> buffer_barriers = !are_buffer_barriers_empty ? barrier_group.buffer_barriers : VectorView<RDD::BufferBarrier>();
1341 #else
1342 const VectorView<RDD::BufferBarrier> buffer_barriers = VectorView<RDD::BufferBarrier>();
1343 #endif
1344
1345 driver->command_pipeline_barrier(p_command_buffer, barrier_group.src_stages, barrier_group.dst_stages, memory_barriers, buffer_barriers, texture_barriers);
1346
1347 bool separate_texture_barriers = !barrier_group.normalization_barriers.is_empty() && !barrier_group.transition_barriers.is_empty();
1348 if (separate_texture_barriers) {
1349 driver->command_pipeline_barrier(p_command_buffer, barrier_group.src_stages, barrier_group.dst_stages, VectorView<RDD::MemoryAccessBarrier>(), VectorView<RDD::BufferBarrier>(), barrier_group.transition_barriers);
1350 }
1351 }
1352
1353 void RenderingDeviceGraph::_print_render_commands(const RecordedCommandSort *p_sorted_commands, uint32_t p_sorted_commands_count) {
1354 for (uint32_t i = 0; i < p_sorted_commands_count; i++) {
1355 const uint32_t command_index = p_sorted_commands[i].index;
1356 const uint32_t command_level = p_sorted_commands[i].level;
1357 const uint32_t command_data_offset = command_data_offsets[command_index];
1358 const RecordedCommand *command = reinterpret_cast<RecordedCommand *>(&command_data[command_data_offset]);
1359 switch (command->type) {
1360 case RecordedCommand::TYPE_BUFFER_CLEAR: {
1361 const RecordedBufferClearCommand *buffer_clear_command = reinterpret_cast<const RecordedBufferClearCommand *>(command);
1362 print_line(command_index, "LEVEL", command_level, "BUFFER CLEAR DESTINATION", itos(buffer_clear_command->buffer.id));
1363 } break;
1364 case RecordedCommand::TYPE_BUFFER_COPY: {
1365 const RecordedBufferCopyCommand *buffer_copy_command = reinterpret_cast<const RecordedBufferCopyCommand *>(command);
1366 print_line(command_index, "LEVEL", command_level, "BUFFER COPY SOURCE", itos(buffer_copy_command->source.id), "DESTINATION", itos(buffer_copy_command->destination.id));
1367 } break;
1368 case RecordedCommand::TYPE_BUFFER_GET_DATA: {
1369 const RecordedBufferGetDataCommand *buffer_get_data_command = reinterpret_cast<const RecordedBufferGetDataCommand *>(command);
1370 print_line(command_index, "LEVEL", command_level, "BUFFER GET DATA DESTINATION", itos(buffer_get_data_command->destination.id));
1371 } break;
1372 case RecordedCommand::TYPE_BUFFER_UPDATE: {
1373 const RecordedBufferUpdateCommand *buffer_update_command = reinterpret_cast<const RecordedBufferUpdateCommand *>(command);
1374 print_line(command_index, "LEVEL", command_level, "BUFFER UPDATE DESTINATION", itos(buffer_update_command->destination.id), "COPIES", buffer_update_command->buffer_copies_count);
1375 } break;
1376 case RecordedCommand::TYPE_DRIVER_CALLBACK: {
1377 print_line(command_index, "LEVEL", command_level, "DRIVER CALLBACK");
1378 } break;
1379 case RecordedCommand::TYPE_COMPUTE_LIST: {
1380 const RecordedComputeListCommand *compute_list_command = reinterpret_cast<const RecordedComputeListCommand *>(command);
1381 print_line(command_index, "LEVEL", command_level, "COMPUTE LIST SIZE", compute_list_command->instruction_data_size);
1382 } break;
1383 case RecordedCommand::TYPE_DRAW_LIST: {
1384 const RecordedDrawListCommand *draw_list_command = reinterpret_cast<const RecordedDrawListCommand *>(command);
1385 print_line(command_index, "LEVEL", command_level, "DRAW LIST SIZE", draw_list_command->instruction_data_size);
1386 } break;
1387 case RecordedCommand::TYPE_TEXTURE_CLEAR_COLOR: {
1388 const RecordedTextureClearColorCommand *texture_clear_color_command = reinterpret_cast<const RecordedTextureClearColorCommand *>(command);
1389 print_line(command_index, "LEVEL", command_level, "TEXTURE CLEAR COLOR", itos(texture_clear_color_command->texture.id), "COLOR", texture_clear_color_command->color);
1390 } break;
1391 case RecordedCommand::TYPE_TEXTURE_CLEAR_DEPTH_STENCIL: {
1392 const RecordedTextureClearDepthStencilCommand *texture_clear_depth_stencil_command = reinterpret_cast<const RecordedTextureClearDepthStencilCommand *>(command);
1393 print_line(command_index, "LEVEL", command_level, "TEXTURE CLEAR DEPTH STENCIL", itos(texture_clear_depth_stencil_command->texture.id), "DEPTH", rtos(texture_clear_depth_stencil_command->depth), "STENCIL", itos(texture_clear_depth_stencil_command->stencil));
1394 } break;
1395 case RecordedCommand::TYPE_TEXTURE_COPY: {
1396 const RecordedTextureCopyCommand *texture_copy_command = reinterpret_cast<const RecordedTextureCopyCommand *>(command);
1397 print_line(command_index, "LEVEL", command_level, "TEXTURE COPY FROM", itos(texture_copy_command->from_texture.id), "TO", itos(texture_copy_command->to_texture.id));
1398 } break;
1399 case RecordedCommand::TYPE_TEXTURE_GET_DATA: {
1400 print_line(command_index, "LEVEL", command_level, "TEXTURE GET DATA");
1401 } break;
1402 case RecordedCommand::TYPE_TEXTURE_RESOLVE: {
1403 const RecordedTextureResolveCommand *texture_resolve_command = reinterpret_cast<const RecordedTextureResolveCommand *>(command);
1404 print_line(command_index, "LEVEL", command_level, "TEXTURE RESOLVE FROM", itos(texture_resolve_command->from_texture.id), "TO", itos(texture_resolve_command->to_texture.id));
1405 } break;
1406 case RecordedCommand::TYPE_TEXTURE_UPDATE: {
1407 const RecordedTextureUpdateCommand *texture_update_command = reinterpret_cast<const RecordedTextureUpdateCommand *>(command);
1408 print_line(command_index, "LEVEL", command_level, "TEXTURE UPDATE TO", itos(texture_update_command->to_texture.id));
1409 } break;
1410 case RecordedCommand::TYPE_CAPTURE_TIMESTAMP: {
1411 const RecordedCaptureTimestampCommand *texture_capture_timestamp_command = reinterpret_cast<const RecordedCaptureTimestampCommand *>(command);
1412 print_line(command_index, "LEVEL", command_level, "CAPTURE TIMESTAMP POOL", itos(texture_capture_timestamp_command->pool.id), "INDEX", texture_capture_timestamp_command->index);
1413 } break;
1414 default:
1415 DEV_ASSERT(false && "Unknown recorded command type.");
1416 return;
1417 }
1418 }
1419 }
1420
1421 void RenderingDeviceGraph::_print_draw_list(const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {
1422 uint32_t instruction_data_cursor = 0;
1423 while (instruction_data_cursor < p_instruction_data_size) {
1424 DEV_ASSERT((instruction_data_cursor + sizeof(DrawListInstruction)) <= p_instruction_data_size);
1425
1426 const DrawListInstruction *instruction = reinterpret_cast<const DrawListInstruction *>(&p_instruction_data[instruction_data_cursor]);
1427 switch (instruction->type) {
1428 case DrawListInstruction::TYPE_BIND_INDEX_BUFFER: {
1429 const DrawListBindIndexBufferInstruction *bind_index_buffer_instruction = reinterpret_cast<const DrawListBindIndexBufferInstruction *>(instruction);
1430 print_line("\tBIND INDEX BUFFER ID", itos(bind_index_buffer_instruction->buffer.id), "FORMAT", bind_index_buffer_instruction->format, "OFFSET", bind_index_buffer_instruction->offset);
1431 instruction_data_cursor += sizeof(DrawListBindIndexBufferInstruction);
1432 } break;
1433 case DrawListInstruction::TYPE_BIND_PIPELINE: {
1434 const DrawListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const DrawListBindPipelineInstruction *>(instruction);
1435 print_line("\tBIND PIPELINE ID", itos(bind_pipeline_instruction->pipeline.id));
1436 instruction_data_cursor += sizeof(DrawListBindPipelineInstruction);
1437 } break;
1438 case DrawListInstruction::TYPE_BIND_UNIFORM_SETS: {
1439 const DrawListBindUniformSetsInstruction *bind_uniform_sets_instruction = reinterpret_cast<const DrawListBindUniformSetsInstruction *>(instruction);
1440 print_line("\tBIND UNIFORM SETS COUNT", bind_uniform_sets_instruction->set_count);
1441 for (uint32_t i = 0; i < bind_uniform_sets_instruction->set_count; i++) {
1442 print_line("\tBIND UNIFORM SET ID", itos(bind_uniform_sets_instruction->uniform_set_ids()[i].id), "START INDEX", bind_uniform_sets_instruction->first_set_index, "DYNAMIC_OFFSETS", bind_uniform_sets_instruction->dynamic_offsets_mask);
1443 }
1444 instruction_data_cursor += sizeof(DrawListBindUniformSetsInstruction) + sizeof(RDD::UniformSetID) * bind_uniform_sets_instruction->set_count;
1445 } break;
1446 case DrawListInstruction::TYPE_BIND_VERTEX_BUFFERS: {
1447 const DrawListBindVertexBuffersInstruction *bind_vertex_buffers_instruction = reinterpret_cast<const DrawListBindVertexBuffersInstruction *>(instruction);
1448 print_line("\tBIND VERTEX BUFFERS COUNT", bind_vertex_buffers_instruction->vertex_buffers_count);
1449 instruction_data_cursor += sizeof(DrawListBindVertexBuffersInstruction);
1450 instruction_data_cursor += sizeof(RDD::BufferID) * bind_vertex_buffers_instruction->vertex_buffers_count;
1451 instruction_data_cursor += sizeof(uint64_t) * bind_vertex_buffers_instruction->vertex_buffers_count;
1452 } break;
1453 case DrawListInstruction::TYPE_CLEAR_ATTACHMENTS: {
1454 const DrawListClearAttachmentsInstruction *clear_attachments_instruction = reinterpret_cast<const DrawListClearAttachmentsInstruction *>(instruction);
1455 print_line("\tATTACHMENTS CLEAR COUNT", clear_attachments_instruction->attachments_clear_count, "RECT COUNT", clear_attachments_instruction->attachments_clear_rect_count);
1456 instruction_data_cursor += sizeof(DrawListClearAttachmentsInstruction);
1457 instruction_data_cursor += sizeof(RDD::AttachmentClear) * clear_attachments_instruction->attachments_clear_count;
1458 instruction_data_cursor += sizeof(Rect2i) * clear_attachments_instruction->attachments_clear_rect_count;
1459 } break;
1460 case DrawListInstruction::TYPE_DRAW: {
1461 const DrawListDrawInstruction *draw_instruction = reinterpret_cast<const DrawListDrawInstruction *>(instruction);
1462 print_line("\tDRAW VERTICES", draw_instruction->vertex_count, "INSTANCES", draw_instruction->instance_count);
1463 instruction_data_cursor += sizeof(DrawListDrawInstruction);
1464 } break;
1465 case DrawListInstruction::TYPE_DRAW_INDEXED: {
1466 const DrawListDrawIndexedInstruction *draw_indexed_instruction = reinterpret_cast<const DrawListDrawIndexedInstruction *>(instruction);
1467 print_line("\tDRAW INDICES", draw_indexed_instruction->index_count, "INSTANCES", draw_indexed_instruction->instance_count, "FIRST INDEX", draw_indexed_instruction->first_index);
1468 instruction_data_cursor += sizeof(DrawListDrawIndexedInstruction);
1469 } break;
1470 case DrawListInstruction::TYPE_DRAW_INDIRECT: {
1471 const DrawListDrawIndirectInstruction *draw_indirect_instruction = reinterpret_cast<const DrawListDrawIndirectInstruction *>(instruction);
1472 print_line("\tDRAW INDIRECT BUFFER ID", itos(draw_indirect_instruction->buffer.id), "OFFSET", draw_indirect_instruction->offset, "DRAW COUNT", draw_indirect_instruction->draw_count, "STRIDE", draw_indirect_instruction->stride);
1473 instruction_data_cursor += sizeof(DrawListDrawIndirectInstruction);
1474 } break;
1475 case DrawListInstruction::TYPE_DRAW_INDEXED_INDIRECT: {
1476 const DrawListDrawIndexedIndirectInstruction *draw_indexed_indirect_instruction = reinterpret_cast<const DrawListDrawIndexedIndirectInstruction *>(instruction);
1477 print_line("\tDRAW INDEXED INDIRECT BUFFER ID", itos(draw_indexed_indirect_instruction->buffer.id), "OFFSET", draw_indexed_indirect_instruction->offset, "DRAW COUNT", draw_indexed_indirect_instruction->draw_count, "STRIDE", draw_indexed_indirect_instruction->stride);
1478 instruction_data_cursor += sizeof(DrawListDrawIndexedIndirectInstruction);
1479 } break;
1480 case DrawListInstruction::TYPE_EXECUTE_COMMANDS: {
1481 print_line("\tEXECUTE COMMANDS");
1482 instruction_data_cursor += sizeof(DrawListExecuteCommandsInstruction);
1483 } break;
1484 case DrawListInstruction::TYPE_NEXT_SUBPASS: {
1485 print_line("\tNEXT SUBPASS");
1486 instruction_data_cursor += sizeof(DrawListNextSubpassInstruction);
1487 } break;
1488 case DrawListInstruction::TYPE_SET_BLEND_CONSTANTS: {
1489 const DrawListSetBlendConstantsInstruction *set_blend_constants_instruction = reinterpret_cast<const DrawListSetBlendConstantsInstruction *>(instruction);
1490 print_line("\tSET BLEND CONSTANTS COLOR", set_blend_constants_instruction->color);
1491 instruction_data_cursor += sizeof(DrawListSetBlendConstantsInstruction);
1492 } break;
1493 case DrawListInstruction::TYPE_SET_LINE_WIDTH: {
1494 const DrawListSetLineWidthInstruction *set_line_width_instruction = reinterpret_cast<const DrawListSetLineWidthInstruction *>(instruction);
1495 print_line("\tSET LINE WIDTH", set_line_width_instruction->width);
1496 instruction_data_cursor += sizeof(DrawListSetLineWidthInstruction);
1497 } break;
1498 case DrawListInstruction::TYPE_SET_PUSH_CONSTANT: {
1499 const DrawListSetPushConstantInstruction *set_push_constant_instruction = reinterpret_cast<const DrawListSetPushConstantInstruction *>(instruction);
1500 print_line("\tSET PUSH CONSTANT SIZE", set_push_constant_instruction->size);
1501 instruction_data_cursor += sizeof(DrawListSetPushConstantInstruction);
1502 instruction_data_cursor += set_push_constant_instruction->size;
1503 } break;
1504 case DrawListInstruction::TYPE_SET_SCISSOR: {
1505 const DrawListSetScissorInstruction *set_scissor_instruction = reinterpret_cast<const DrawListSetScissorInstruction *>(instruction);
1506 print_line("\tSET SCISSOR", set_scissor_instruction->rect);
1507 instruction_data_cursor += sizeof(DrawListSetScissorInstruction);
1508 } break;
1509 case DrawListInstruction::TYPE_SET_VIEWPORT: {
1510 const DrawListSetViewportInstruction *set_viewport_instruction = reinterpret_cast<const DrawListSetViewportInstruction *>(instruction);
1511 print_line("\tSET VIEWPORT", set_viewport_instruction->rect);
1512 instruction_data_cursor += sizeof(DrawListSetViewportInstruction);
1513 } break;
1514 case DrawListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE: {
1515 const DrawListUniformSetPrepareForUseInstruction *uniform_set_prepare_for_use_instruction = reinterpret_cast<const DrawListUniformSetPrepareForUseInstruction *>(instruction);
1516 print_line("\tUNIFORM SET PREPARE FOR USE ID", itos(uniform_set_prepare_for_use_instruction->uniform_set.id), "SHADER ID", itos(uniform_set_prepare_for_use_instruction->shader.id), "INDEX", uniform_set_prepare_for_use_instruction->set_index);
1517 instruction_data_cursor += sizeof(DrawListUniformSetPrepareForUseInstruction);
1518 } break;
1519 default:
1520 DEV_ASSERT(false && "Unknown draw list instruction type.");
1521 return;
1522 }
1523
1524 instruction_data_cursor = GRAPH_ALIGN(instruction_data_cursor);
1525 }
1526 }
1527
1528 void RenderingDeviceGraph::_print_compute_list(const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {
1529 uint32_t instruction_data_cursor = 0;
1530 while (instruction_data_cursor < p_instruction_data_size) {
1531 DEV_ASSERT((instruction_data_cursor + sizeof(ComputeListInstruction)) <= p_instruction_data_size);
1532
1533 const ComputeListInstruction *instruction = reinterpret_cast<const ComputeListInstruction *>(&p_instruction_data[instruction_data_cursor]);
1534 switch (instruction->type) {
1535 case ComputeListInstruction::TYPE_BIND_PIPELINE: {
1536 const ComputeListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const ComputeListBindPipelineInstruction *>(instruction);
1537 print_line("\tBIND PIPELINE ID", itos(bind_pipeline_instruction->pipeline.id));
1538 instruction_data_cursor += sizeof(ComputeListBindPipelineInstruction);
1539 } break;
1540 case ComputeListInstruction::TYPE_BIND_UNIFORM_SETS: {
1541 const ComputeListBindUniformSetsInstruction *bind_uniform_sets_instruction = reinterpret_cast<const ComputeListBindUniformSetsInstruction *>(instruction);
1542 print_line("\tBIND UNIFORM SETS COUNT", bind_uniform_sets_instruction->set_count);
1543 for (uint32_t i = 0; i < bind_uniform_sets_instruction->set_count; i++) {
1544 print_line("\tBIND UNIFORM SET ID", itos(bind_uniform_sets_instruction->uniform_set_ids()[i].id), "START INDEX", bind_uniform_sets_instruction->first_set_index, "DYNAMIC_OFFSETS", bind_uniform_sets_instruction->dynamic_offsets_mask);
1545 }
1546 instruction_data_cursor += sizeof(ComputeListBindUniformSetsInstruction) + sizeof(RDD::UniformSetID) * bind_uniform_sets_instruction->set_count;
1547 } break;
1548 case ComputeListInstruction::TYPE_DISPATCH: {
1549 const ComputeListDispatchInstruction *dispatch_instruction = reinterpret_cast<const ComputeListDispatchInstruction *>(instruction);
1550 print_line("\tDISPATCH", dispatch_instruction->x_groups, dispatch_instruction->y_groups, dispatch_instruction->z_groups);
1551 instruction_data_cursor += sizeof(ComputeListDispatchInstruction);
1552 } break;
1553 case ComputeListInstruction::TYPE_DISPATCH_INDIRECT: {
1554 const ComputeListDispatchIndirectInstruction *dispatch_indirect_instruction = reinterpret_cast<const ComputeListDispatchIndirectInstruction *>(instruction);
1555 print_line("\tDISPATCH INDIRECT BUFFER ID", itos(dispatch_indirect_instruction->buffer.id), "OFFSET", dispatch_indirect_instruction->offset);
1556 instruction_data_cursor += sizeof(ComputeListDispatchIndirectInstruction);
1557 } break;
1558 case ComputeListInstruction::TYPE_SET_PUSH_CONSTANT: {
1559 const ComputeListSetPushConstantInstruction *set_push_constant_instruction = reinterpret_cast<const ComputeListSetPushConstantInstruction *>(instruction);
1560 print_line("\tSET PUSH CONSTANT SIZE", set_push_constant_instruction->size);
1561 instruction_data_cursor += sizeof(ComputeListSetPushConstantInstruction);
1562 instruction_data_cursor += set_push_constant_instruction->size;
1563 } break;
1564 case ComputeListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE: {
1565 const ComputeListUniformSetPrepareForUseInstruction *uniform_set_prepare_for_use_instruction = reinterpret_cast<const ComputeListUniformSetPrepareForUseInstruction *>(instruction);
1566 print_line("\tUNIFORM SET PREPARE FOR USE ID", itos(uniform_set_prepare_for_use_instruction->uniform_set.id), "SHADER ID", itos(uniform_set_prepare_for_use_instruction->shader.id), "INDEX", itos(uniform_set_prepare_for_use_instruction->set_index));
1567 instruction_data_cursor += sizeof(ComputeListUniformSetPrepareForUseInstruction);
1568 } break;
1569 default:
1570 DEV_ASSERT(false && "Unknown compute list instruction type.");
1571 return;
1572 }
1573
1574 instruction_data_cursor = GRAPH_ALIGN(instruction_data_cursor);
1575 }
1576 }
1577
1578 void RenderingDeviceGraph::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) {
1579 DEV_ASSERT(p_driver != nullptr);
1580 DEV_ASSERT(p_render_pass_creation_function != nullptr);
1581 DEV_ASSERT(p_frame_count > 0);
1582
1583 driver = p_driver;
1584 device = p_device;
1585 render_pass_creation_function = p_render_pass_creation_function;
1586 frames.resize(p_frame_count);
1587
1588 for (uint32_t i = 0; i < p_frame_count; i++) {
1589 frames[i].secondary_command_buffers.resize(p_secondary_command_buffers_per_frame);
1590
1591 for (uint32_t j = 0; j < p_secondary_command_buffers_per_frame; j++) {
1592 SecondaryCommandBuffer &secondary = frames[i].secondary_command_buffers[j];
1593 secondary.command_pool = driver->command_pool_create(p_secondary_command_queue_family, RDD::COMMAND_BUFFER_TYPE_SECONDARY);
1594 secondary.command_buffer = driver->command_buffer_create(secondary.command_pool);
1595 secondary.task = WorkerThreadPool::INVALID_TASK_ID;
1596 }
1597 }
1598
1599 driver_honors_barriers = driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS);
1600 driver_clears_with_copy_engine = driver->api_trait_get(RDD::API_TRAIT_CLEARS_WITH_COPY_ENGINE);
1601 driver_buffers_require_transitions = driver->api_trait_get(RDD::API_TRAIT_BUFFERS_REQUIRE_TRANSITIONS);
1602 }
1603
1604 void RenderingDeviceGraph::finalize() {
1605 if (!frames.is_empty()) {
1606 _wait_for_secondary_command_buffer_tasks();
1607 }
1608
1609 for (Frame &f : frames) {
1610 for (SecondaryCommandBuffer &secondary : f.secondary_command_buffers) {
1611 if (secondary.command_pool.id != 0) {
1612 driver->command_pool_free(secondary.command_pool);
1613 }
1614 }
1615 }
1616
1617 frames.clear();
1618 }
1619
1620 void RenderingDeviceGraph::begin() {
1621 command_data.clear();
1622 command_data_offsets.clear();
1623 command_normalization_barriers.clear();
1624 command_transition_barriers.clear();
1625 command_buffer_barriers.clear();
1626 command_label_chars.clear();
1627 command_label_colors.clear();
1628 command_label_offsets.clear();
1629 command_list_nodes.clear();
1630 read_slice_list_nodes.clear();
1631 write_slice_list_nodes.clear();
1632 command_count = 0;
1633 command_label_count = 0;
1634 command_timestamp_index = -1;
1635 command_synchronization_index = -1;
1636 command_synchronization_pending = false;
1637 command_label_index = -1;
1638 frames[frame].secondary_command_buffers_used = 0;
1639 draw_instruction_list.index = 0;
1640 compute_instruction_list.index = 0;
1641 tracking_frame++;
1642
1643 #ifdef DEV_ENABLED
1644 write_dependency_counters.clear();
1645 #endif
1646 }
1647
1648 void RenderingDeviceGraph::add_buffer_clear(RDD::BufferID p_dst, ResourceTracker *p_dst_tracker, uint32_t p_offset, uint32_t p_size) {
1649 DEV_ASSERT(p_dst_tracker != nullptr);
1650
1651 int32_t command_index;
1652 RecordedBufferClearCommand *command = static_cast<RecordedBufferClearCommand *>(_allocate_command(sizeof(RecordedBufferClearCommand), command_index));
1653 command->type = RecordedCommand::TYPE_BUFFER_CLEAR;
1654 command->buffer = p_dst;
1655 command->offset = p_offset;
1656 command->size = p_size;
1657
1658 ResourceUsage usage;
1659 if (driver_clears_with_copy_engine) {
1660 command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;
1661 usage = RESOURCE_USAGE_COPY_TO;
1662 } else {
1663 // If the driver is uncapable of using the copy engine for clearing the buffer (e.g. D3D12), we must transition it to storage buffer read/write usage.
1664 command->self_stages = RDD::PIPELINE_STAGE_CLEAR_STORAGE_BIT;
1665 usage = RESOURCE_USAGE_STORAGE_BUFFER_READ_WRITE;
1666 }
1667
1668 _add_command_to_graph(&p_dst_tracker, &usage, 1, command_index, command);
1669 }
1670
1671 void RenderingDeviceGraph::add_buffer_copy(RDD::BufferID p_src, ResourceTracker *p_src_tracker, RDD::BufferID p_dst, ResourceTracker *p_dst_tracker, RDD::BufferCopyRegion p_region) {
1672 // Source tracker is allowed to be null as it could be a read-only buffer.
1673 DEV_ASSERT(p_dst_tracker != nullptr);
1674
1675 int32_t command_index;
1676 RecordedBufferCopyCommand *command = static_cast<RecordedBufferCopyCommand *>(_allocate_command(sizeof(RecordedBufferCopyCommand), command_index));
1677 command->type = RecordedCommand::TYPE_BUFFER_COPY;
1678 command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;
1679 command->source = p_src;
1680 command->destination = p_dst;
1681 command->region = p_region;
1682
1683 ResourceTracker *trackers[2] = { p_dst_tracker, p_src_tracker };
1684 ResourceUsage usages[2] = { RESOURCE_USAGE_COPY_TO, RESOURCE_USAGE_COPY_FROM };
1685 _add_command_to_graph(trackers, usages, p_src_tracker != nullptr ? 2 : 1, command_index, command);
1686 }
1687
1688 void RenderingDeviceGraph::add_buffer_get_data(RDD::BufferID p_src, ResourceTracker *p_src_tracker, RDD::BufferID p_dst, RDD::BufferCopyRegion p_region) {
1689 // Source tracker is allowed to be null as it could be a read-only buffer.
1690 int32_t command_index;
1691 RecordedBufferGetDataCommand *command = static_cast<RecordedBufferGetDataCommand *>(_allocate_command(sizeof(RecordedBufferGetDataCommand), command_index));
1692 command->type = RecordedCommand::TYPE_BUFFER_GET_DATA;
1693 command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;
1694 command->source = p_src;
1695 command->destination = p_dst;
1696 command->region = p_region;
1697
1698 if (p_src_tracker != nullptr) {
1699 ResourceUsage usage = RESOURCE_USAGE_COPY_FROM;
1700 _add_command_to_graph(&p_src_tracker, &usage, 1, command_index, command);
1701 } else {
1702 _add_command_to_graph(nullptr, nullptr, 0, command_index, command);
1703 }
1704 }
1705
1706 void RenderingDeviceGraph::add_buffer_update(RDD::BufferID p_dst, ResourceTracker *p_dst_tracker, VectorView<RecordedBufferCopy> p_buffer_copies) {
1707 DEV_ASSERT(p_dst_tracker != nullptr);
1708
1709 size_t buffer_copies_size = p_buffer_copies.size() * sizeof(RecordedBufferCopy);
1710 uint64_t command_size = sizeof(RecordedBufferUpdateCommand) + buffer_copies_size;
1711 int32_t command_index;
1712 RecordedBufferUpdateCommand *command = static_cast<RecordedBufferUpdateCommand *>(_allocate_command(command_size, command_index));
1713 command->type = RecordedCommand::TYPE_BUFFER_UPDATE;
1714 command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;
1715 command->destination = p_dst;
1716 command->buffer_copies_count = p_buffer_copies.size();
1717
1718 RecordedBufferCopy *buffer_copies = command->buffer_copies();
1719 for (uint32_t i = 0; i < command->buffer_copies_count; i++) {
1720 buffer_copies[i] = p_buffer_copies[i];
1721 }
1722
1723 ResourceUsage buffer_usage = RESOURCE_USAGE_COPY_TO;
1724 _add_command_to_graph(&p_dst_tracker, &buffer_usage, 1, command_index, command);
1725 }
1726
1727 void RenderingDeviceGraph::add_driver_callback(RDD::DriverCallback p_callback, void *p_userdata, VectorView<ResourceTracker *> p_trackers, VectorView<RenderingDeviceGraph::ResourceUsage> p_usages) {
1728 DEV_ASSERT(p_trackers.size() == p_usages.size());
1729
1730 int32_t command_index;
1731 RecordedDriverCallbackCommand *command = static_cast<RecordedDriverCallbackCommand *>(_allocate_command(sizeof(RecordedDriverCallbackCommand), command_index));
1732 command->type = RecordedCommand::TYPE_DRIVER_CALLBACK;
1733 command->callback = p_callback;
1734 command->userdata = p_userdata;
1735 _add_command_to_graph((ResourceTracker **)p_trackers.ptr(), (ResourceUsage *)p_usages.ptr(), p_trackers.size(), command_index, command);
1736 }
1737
1738 void RenderingDeviceGraph::add_compute_list_begin(RDD::BreadcrumbMarker p_phase, uint32_t p_breadcrumb_data) {
1739 compute_instruction_list.clear();
1740 #if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
1741 compute_instruction_list.breadcrumb = p_breadcrumb_data | (p_phase & ((1 << 16) - 1));
1742 #endif
1743 compute_instruction_list.index++;
1744 }
1745
1746 void RenderingDeviceGraph::add_compute_list_bind_pipeline(RDD::PipelineID p_pipeline) {
1747 ComputeListBindPipelineInstruction *instruction = reinterpret_cast<ComputeListBindPipelineInstruction *>(_allocate_compute_list_instruction(sizeof(ComputeListBindPipelineInstruction)));
1748 instruction->type = ComputeListInstruction::TYPE_BIND_PIPELINE;
1749 instruction->pipeline = p_pipeline;
1750 compute_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT);
1751 }
1752
1753 void RenderingDeviceGraph::add_compute_list_bind_uniform_set(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index) {
1754 add_compute_list_bind_uniform_sets(p_shader, VectorView(&p_uniform_set, 1), set_index, 1);
1755 }
1756
1757 void RenderingDeviceGraph::add_compute_list_bind_uniform_sets(RDD::ShaderID p_shader, VectorView<RDD::UniformSetID> p_uniform_sets, uint32_t p_first_set_index, uint32_t p_set_count) {
1758 DEV_ASSERT(p_uniform_sets.size() >= p_set_count);
1759
1760 uint32_t instruction_size = sizeof(ComputeListBindUniformSetsInstruction) + sizeof(RDD::UniformSetID) * p_set_count;
1761 ComputeListBindUniformSetsInstruction *instruction = reinterpret_cast<ComputeListBindUniformSetsInstruction *>(_allocate_compute_list_instruction(instruction_size));
1762 instruction->type = ComputeListInstruction::TYPE_BIND_UNIFORM_SETS;
1763 instruction->shader = p_shader;
1764 instruction->first_set_index = p_first_set_index;
1765 instruction->set_count = p_set_count;
1766 instruction->dynamic_offsets_mask = driver->uniform_sets_get_dynamic_offsets(p_uniform_sets, p_shader, p_first_set_index, p_set_count);
1767
1768 RDD::UniformSetID *ids = instruction->uniform_set_ids();
1769 for (uint32_t i = 0; i < p_set_count; i++) {
1770 ids[i] = p_uniform_sets[i];
1771 }
1772 }
1773
1774 void RenderingDeviceGraph::add_compute_list_dispatch(uint32_t p_x_groups, uint32_t p_y_groups, uint32_t p_z_groups) {
1775 ComputeListDispatchInstruction *instruction = reinterpret_cast<ComputeListDispatchInstruction *>(_allocate_compute_list_instruction(sizeof(ComputeListDispatchInstruction)));
1776 instruction->type = ComputeListInstruction::TYPE_DISPATCH;
1777 instruction->x_groups = p_x_groups;
1778 instruction->y_groups = p_y_groups;
1779 instruction->z_groups = p_z_groups;
1780 }
1781
1782 void RenderingDeviceGraph::add_compute_list_dispatch_indirect(RDD::BufferID p_buffer, uint32_t p_offset) {
1783 ComputeListDispatchIndirectInstruction *instruction = reinterpret_cast<ComputeListDispatchIndirectInstruction *>(_allocate_compute_list_instruction(sizeof(ComputeListDispatchIndirectInstruction)));
1784 instruction->type = ComputeListInstruction::TYPE_DISPATCH_INDIRECT;
1785 instruction->buffer = p_buffer;
1786 instruction->offset = p_offset;
1787 compute_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_DRAW_INDIRECT_BIT);
1788 }
1789
1790 void RenderingDeviceGraph::add_compute_list_set_push_constant(RDD::ShaderID p_shader, const void *p_data, uint32_t p_data_size) {
1791 uint32_t instruction_size = sizeof(ComputeListSetPushConstantInstruction) + p_data_size;
1792 ComputeListSetPushConstantInstruction *instruction = reinterpret_cast<ComputeListSetPushConstantInstruction *>(_allocate_compute_list_instruction(instruction_size));
1793 instruction->type = ComputeListInstruction::TYPE_SET_PUSH_CONSTANT;
1794 instruction->size = p_data_size;
1795 instruction->shader = p_shader;
1796 memcpy(instruction->data(), p_data, p_data_size);
1797 }
1798
1799 void RenderingDeviceGraph::add_compute_list_uniform_set_prepare_for_use(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index) {
1800 ComputeListUniformSetPrepareForUseInstruction *instruction = reinterpret_cast<ComputeListUniformSetPrepareForUseInstruction *>(_allocate_compute_list_instruction(sizeof(ComputeListUniformSetPrepareForUseInstruction)));
1801 instruction->type = ComputeListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE;
1802 instruction->shader = p_shader;
1803 instruction->uniform_set = p_uniform_set;
1804 instruction->set_index = set_index;
1805 }
1806
1807 void RenderingDeviceGraph::add_compute_list_usage(ResourceTracker *p_tracker, ResourceUsage p_usage) {
1808 DEV_ASSERT(p_tracker != nullptr);
1809
1810 p_tracker->reset_if_outdated(tracking_frame);
1811
1812 if (p_tracker->compute_list_index != compute_instruction_list.index) {
1813 compute_instruction_list.command_trackers.push_back(p_tracker);
1814 compute_instruction_list.command_tracker_usages.push_back(p_usage);
1815 p_tracker->compute_list_index = compute_instruction_list.index;
1816 p_tracker->compute_list_usage = p_usage;
1817 }
1818 #ifdef DEV_ENABLED
1819 else if (p_tracker->compute_list_usage != p_usage) {
1820 ERR_FAIL_MSG(vformat("Tracker can't have more than one type of usage in the same compute list. Compute list usage is %s and the requested usage is %s.", _usage_to_string(p_tracker->compute_list_usage), _usage_to_string(p_usage)));
1821 }
1822 #endif
1823 }
1824
1825 void RenderingDeviceGraph::add_compute_list_usages(VectorView<ResourceTracker *> p_trackers, VectorView<ResourceUsage> p_usages) {
1826 DEV_ASSERT(p_trackers.size() == p_usages.size());
1827
1828 for (uint32_t i = 0; i < p_trackers.size(); i++) {
1829 add_compute_list_usage(p_trackers[i], p_usages[i]);
1830 }
1831 }
1832
1833 void RenderingDeviceGraph::add_compute_list_end() {
1834 int32_t command_index;
1835 uint32_t instruction_data_size = compute_instruction_list.data.size();
1836 uint32_t command_size = sizeof(RecordedComputeListCommand) + instruction_data_size;
1837 RecordedComputeListCommand *command = static_cast<RecordedComputeListCommand *>(_allocate_command(command_size, command_index));
1838 command->type = RecordedCommand::TYPE_COMPUTE_LIST;
1839 command->self_stages = compute_instruction_list.stages;
1840 command->instruction_data_size = instruction_data_size;
1841 memcpy(command->instruction_data(), compute_instruction_list.data.ptr(), instruction_data_size);
1842 _add_command_to_graph(compute_instruction_list.command_trackers.ptr(), compute_instruction_list.command_tracker_usages.ptr(), compute_instruction_list.command_trackers.size(), command_index, command);
1843 }
1844
1845 void RenderingDeviceGraph::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, bool p_split_cmd_buffer) {
1846 _add_draw_list_begin(p_framebuffer_cache, RDD::RenderPassID(), RDD::FramebufferID(), p_region, p_attachment_operations, p_attachment_clear_values, p_stages, p_breadcrumb, p_split_cmd_buffer);
1847 }
1848
1849 void RenderingDeviceGraph::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, bool p_split_cmd_buffer) {
1850 _add_draw_list_begin(nullptr, p_render_pass, p_framebuffer, p_region, p_attachment_operations, p_attachment_clear_values, p_stages, p_breadcrumb, p_split_cmd_buffer);
1851 }
1852
1853 void RenderingDeviceGraph::add_draw_list_bind_index_buffer(RDD::BufferID p_buffer, RDD::IndexBufferFormat p_format, uint32_t p_offset) {
1854 DrawListBindIndexBufferInstruction *instruction = reinterpret_cast<DrawListBindIndexBufferInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListBindIndexBufferInstruction)));
1855 instruction->type = DrawListInstruction::TYPE_BIND_INDEX_BUFFER;
1856 instruction->buffer = p_buffer;
1857 instruction->format = p_format;
1858 instruction->offset = p_offset;
1859
1860 if (instruction->buffer.id != 0) {
1861 draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_VERTEX_INPUT_BIT);
1862 }
1863 }
1864
1865 void RenderingDeviceGraph::add_draw_list_bind_pipeline(RDD::PipelineID p_pipeline, BitField<RDD::PipelineStageBits> p_pipeline_stage_bits) {
1866 DrawListBindPipelineInstruction *instruction = reinterpret_cast<DrawListBindPipelineInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListBindPipelineInstruction)));
1867 instruction->type = DrawListInstruction::TYPE_BIND_PIPELINE;
1868 instruction->pipeline = p_pipeline;
1869 draw_instruction_list.stages = draw_instruction_list.stages | p_pipeline_stage_bits;
1870 }
1871
1872 void RenderingDeviceGraph::add_draw_list_bind_uniform_set(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index) {
1873 add_draw_list_bind_uniform_sets(p_shader, VectorView(&p_uniform_set, 1), set_index, 1);
1874 }
1875
1876 void RenderingDeviceGraph::add_draw_list_bind_uniform_sets(RDD::ShaderID p_shader, VectorView<RDD::UniformSetID> p_uniform_sets, uint32_t p_first_index, uint32_t p_set_count) {
1877 DEV_ASSERT(p_uniform_sets.size() >= p_set_count);
1878
1879 uint32_t instruction_size = sizeof(DrawListBindUniformSetsInstruction) + sizeof(RDD::UniformSetID) * p_set_count;
1880 DrawListBindUniformSetsInstruction *instruction = reinterpret_cast<DrawListBindUniformSetsInstruction *>(_allocate_draw_list_instruction(instruction_size));
1881 instruction->type = DrawListInstruction::TYPE_BIND_UNIFORM_SETS;
1882 instruction->shader = p_shader;
1883 instruction->first_set_index = p_first_index;
1884 instruction->set_count = p_set_count;
1885 instruction->dynamic_offsets_mask = driver->uniform_sets_get_dynamic_offsets(p_uniform_sets, p_shader, p_first_index, p_set_count);
1886
1887 for (uint32_t i = 0; i < p_set_count; i++) {
1888 instruction->uniform_set_ids()[i] = p_uniform_sets[i];
1889 }
1890 }
1891
1892 void RenderingDeviceGraph::add_draw_list_bind_vertex_buffers(Span<RDD::BufferID> p_vertex_buffers, Span<uint64_t> p_vertex_buffer_offsets) {
1893 DEV_ASSERT(p_vertex_buffers.size() == p_vertex_buffer_offsets.size());
1894
1895 uint32_t instruction_size = sizeof(DrawListBindVertexBuffersInstruction) + sizeof(RDD::BufferID) * p_vertex_buffers.size() + sizeof(uint64_t) * p_vertex_buffer_offsets.size();
1896 DrawListBindVertexBuffersInstruction *instruction = reinterpret_cast<DrawListBindVertexBuffersInstruction *>(_allocate_draw_list_instruction(instruction_size));
1897 instruction->type = DrawListInstruction::TYPE_BIND_VERTEX_BUFFERS;
1898 instruction->vertex_buffers_count = p_vertex_buffers.size();
1899 instruction->dynamic_offsets_mask = driver->buffer_get_dynamic_offsets(p_vertex_buffers);
1900
1901 RDD::BufferID *vertex_buffers = instruction->vertex_buffers();
1902 uint64_t *vertex_buffer_offsets = instruction->vertex_buffer_offsets();
1903 for (uint32_t i = 0; i < instruction->vertex_buffers_count; i++) {
1904 vertex_buffers[i] = p_vertex_buffers[i];
1905 vertex_buffer_offsets[i] = p_vertex_buffer_offsets[i];
1906 }
1907
1908 if (instruction->vertex_buffers_count > 0) {
1909 draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_VERTEX_INPUT_BIT);
1910 }
1911 }
1912
1913 void RenderingDeviceGraph::add_draw_list_clear_attachments(VectorView<RDD::AttachmentClear> p_attachments_clear, VectorView<Rect2i> p_attachments_clear_rect) {
1914 uint32_t instruction_size = sizeof(DrawListClearAttachmentsInstruction) + sizeof(RDD::AttachmentClear) * p_attachments_clear.size() + sizeof(Rect2i) * p_attachments_clear_rect.size();
1915 DrawListClearAttachmentsInstruction *instruction = reinterpret_cast<DrawListClearAttachmentsInstruction *>(_allocate_draw_list_instruction(instruction_size));
1916 instruction->type = DrawListInstruction::TYPE_CLEAR_ATTACHMENTS;
1917 instruction->attachments_clear_count = p_attachments_clear.size();
1918 instruction->attachments_clear_rect_count = p_attachments_clear_rect.size();
1919
1920 RDD::AttachmentClear *attachments_clear = instruction->attachments_clear();
1921 Rect2i *attachments_clear_rect = instruction->attachments_clear_rect();
1922 for (uint32_t i = 0; i < instruction->attachments_clear_count; i++) {
1923 attachments_clear[i] = p_attachments_clear[i];
1924 }
1925
1926 for (uint32_t i = 0; i < instruction->attachments_clear_rect_count; i++) {
1927 attachments_clear_rect[i] = p_attachments_clear_rect[i];
1928 }
1929 }
1930
1931 void RenderingDeviceGraph::add_draw_list_draw(uint32_t p_vertex_count, uint32_t p_instance_count) {
1932 DrawListDrawInstruction *instruction = reinterpret_cast<DrawListDrawInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListDrawInstruction)));
1933 instruction->type = DrawListInstruction::TYPE_DRAW;
1934 instruction->vertex_count = p_vertex_count;
1935 instruction->instance_count = p_instance_count;
1936 }
1937
1938 void RenderingDeviceGraph::add_draw_list_draw_indexed(uint32_t p_index_count, uint32_t p_instance_count, uint32_t p_first_index) {
1939 DrawListDrawIndexedInstruction *instruction = reinterpret_cast<DrawListDrawIndexedInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListDrawIndexedInstruction)));
1940 instruction->type = DrawListInstruction::TYPE_DRAW_INDEXED;
1941 instruction->index_count = p_index_count;
1942 instruction->instance_count = p_instance_count;
1943 instruction->first_index = p_first_index;
1944 }
1945
1946 void RenderingDeviceGraph::add_draw_list_draw_indirect(RDD::BufferID p_buffer, uint32_t p_offset, uint32_t p_draw_count, uint32_t p_stride) {
1947 DrawListDrawIndirectInstruction *instruction = reinterpret_cast<DrawListDrawIndirectInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListDrawIndirectInstruction)));
1948 instruction->type = DrawListInstruction::TYPE_DRAW_INDIRECT;
1949 instruction->buffer = p_buffer;
1950 instruction->offset = p_offset;
1951 instruction->draw_count = p_draw_count;
1952 instruction->stride = p_stride;
1953 draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_DRAW_INDIRECT_BIT);
1954 }
1955
1956 void RenderingDeviceGraph::add_draw_list_draw_indexed_indirect(RDD::BufferID p_buffer, uint32_t p_offset, uint32_t p_draw_count, uint32_t p_stride) {
1957 DrawListDrawIndexedIndirectInstruction *instruction = reinterpret_cast<DrawListDrawIndexedIndirectInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListDrawIndexedIndirectInstruction)));
1958 instruction->type = DrawListInstruction::TYPE_DRAW_INDEXED_INDIRECT;
1959 instruction->buffer = p_buffer;
1960 instruction->offset = p_offset;
1961 instruction->draw_count = p_draw_count;
1962 instruction->stride = p_stride;
1963 draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_DRAW_INDIRECT_BIT);
1964 }
1965
1966 void RenderingDeviceGraph::add_draw_list_execute_commands(RDD::CommandBufferID p_command_buffer) {
1967 DrawListExecuteCommandsInstruction *instruction = reinterpret_cast<DrawListExecuteCommandsInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListExecuteCommandsInstruction)));
1968 instruction->type = DrawListInstruction::TYPE_EXECUTE_COMMANDS;
1969 instruction->command_buffer = p_command_buffer;
1970 }
1971
1972 void RenderingDeviceGraph::add_draw_list_next_subpass(RDD::CommandBufferType p_command_buffer_type) {
1973 DrawListNextSubpassInstruction *instruction = reinterpret_cast<DrawListNextSubpassInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListNextSubpassInstruction)));
1974 instruction->type = DrawListInstruction::TYPE_NEXT_SUBPASS;
1975 instruction->command_buffer_type = p_command_buffer_type;
1976 }
1977
1978 void RenderingDeviceGraph::add_draw_list_set_blend_constants(const Color &p_color) {
1979 DrawListSetBlendConstantsInstruction *instruction = reinterpret_cast<DrawListSetBlendConstantsInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListSetBlendConstantsInstruction)));
1980 instruction->type = DrawListInstruction::TYPE_SET_BLEND_CONSTANTS;
1981 instruction->color = p_color;
1982 }
1983
1984 void RenderingDeviceGraph::add_draw_list_set_line_width(float p_width) {
1985 DrawListSetLineWidthInstruction *instruction = reinterpret_cast<DrawListSetLineWidthInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListSetLineWidthInstruction)));
1986 instruction->type = DrawListInstruction::TYPE_SET_LINE_WIDTH;
1987 instruction->width = p_width;
1988 }
1989
1990 void RenderingDeviceGraph::add_draw_list_set_push_constant(RDD::ShaderID p_shader, const void *p_data, uint32_t p_data_size) {
1991 uint32_t instruction_size = sizeof(DrawListSetPushConstantInstruction) + p_data_size;
1992 DrawListSetPushConstantInstruction *instruction = reinterpret_cast<DrawListSetPushConstantInstruction *>(_allocate_draw_list_instruction(instruction_size));
1993 instruction->type = DrawListInstruction::TYPE_SET_PUSH_CONSTANT;
1994 instruction->size = p_data_size;
1995 instruction->shader = p_shader;
1996 memcpy(instruction->data(), p_data, p_data_size);
1997 }
1998
1999 void RenderingDeviceGraph::add_draw_list_set_scissor(Rect2i p_rect) {
2000 DrawListSetScissorInstruction *instruction = reinterpret_cast<DrawListSetScissorInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListSetScissorInstruction)));
2001 instruction->type = DrawListInstruction::TYPE_SET_SCISSOR;
2002 instruction->rect = p_rect;
2003 }
2004
2005 void RenderingDeviceGraph::add_draw_list_set_viewport(Rect2i p_rect) {
2006 DrawListSetViewportInstruction *instruction = reinterpret_cast<DrawListSetViewportInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListSetViewportInstruction)));
2007 instruction->type = DrawListInstruction::TYPE_SET_VIEWPORT;
2008 instruction->rect = p_rect;
2009 }
2010
2011 void RenderingDeviceGraph::add_draw_list_uniform_set_prepare_for_use(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index) {
2012 DrawListUniformSetPrepareForUseInstruction *instruction = reinterpret_cast<DrawListUniformSetPrepareForUseInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListUniformSetPrepareForUseInstruction)));
2013 instruction->type = DrawListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE;
2014 instruction->shader = p_shader;
2015 instruction->uniform_set = p_uniform_set;
2016 instruction->set_index = set_index;
2017 }
2018
2019 void RenderingDeviceGraph::add_draw_list_usage(ResourceTracker *p_tracker, ResourceUsage p_usage) {
2020 p_tracker->reset_if_outdated(tracking_frame);
2021
2022 if (p_tracker->draw_list_index != draw_instruction_list.index) {
2023 draw_instruction_list.command_trackers.push_back(p_tracker);
2024 draw_instruction_list.command_tracker_usages.push_back(p_usage);
2025 p_tracker->draw_list_index = draw_instruction_list.index;
2026 p_tracker->draw_list_usage = p_usage;
2027 }
2028 #ifdef DEV_ENABLED
2029 else if (p_tracker->draw_list_usage != p_usage) {
2030 ERR_FAIL_MSG(vformat("Tracker can't have more than one type of usage in the same draw list. Draw list usage is %s and the requested usage is %s.", _usage_to_string(p_tracker->draw_list_usage), _usage_to_string(p_usage)));
2031 }
2032 #endif
2033 }
2034
2035 void RenderingDeviceGraph::add_draw_list_usages(VectorView<ResourceTracker *> p_trackers, VectorView<ResourceUsage> p_usages) {
2036 DEV_ASSERT(p_trackers.size() == p_usages.size());
2037
2038 for (uint32_t i = 0; i < p_trackers.size(); i++) {
2039 add_draw_list_usage(p_trackers[i], p_usages[i]);
2040 }
2041 }
2042
2043 void RenderingDeviceGraph::add_draw_list_end() {
2044 FramebufferCache *framebuffer_cache = draw_instruction_list.framebuffer_cache;
2045 int32_t command_index;
2046 uint32_t clear_values_size = sizeof(RDD::RenderPassClearValue) * draw_instruction_list.attachment_clear_values.size();
2047 uint32_t trackers_count = framebuffer_cache != nullptr ? framebuffer_cache->trackers.size() : 0;
2048 uint32_t trackers_and_ops_size = (sizeof(ResourceTracker *) + sizeof(RDD::AttachmentLoadOp) + sizeof(RDD::AttachmentStoreOp)) * trackers_count;
2049 uint32_t instruction_data_size = draw_instruction_list.data.size();
2050 uint32_t command_size = sizeof(RecordedDrawListCommand) + clear_values_size + trackers_and_ops_size + instruction_data_size;
2051 RecordedDrawListCommand *command = static_cast<RecordedDrawListCommand *>(_allocate_command(command_size, command_index));
2052 command->type = RecordedCommand::TYPE_DRAW_LIST;
2053 command->self_stages = draw_instruction_list.stages;
2054 command->framebuffer_cache = framebuffer_cache;
2055 command->render_pass = draw_instruction_list.render_pass;
2056 command->framebuffer = draw_instruction_list.framebuffer;
2057 command->instruction_data_size = instruction_data_size;
2058 command->command_buffer_type = RDD::COMMAND_BUFFER_TYPE_PRIMARY;
2059 command->region = draw_instruction_list.region;
2060 #if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
2061 command->breadcrumb = draw_instruction_list.breadcrumb;
2062 #endif
2063 command->split_cmd_buffer = draw_instruction_list.split_cmd_buffer;
2064 command->clear_values_count = draw_instruction_list.attachment_clear_values.size();
2065 command->trackers_count = trackers_count;
2066
2067 // Initialize the load and store operations to their default behaviors. The store behavior will be modified if a command depends on the result of this render pass.
2068 uint32_t attachment_op_count = draw_instruction_list.attachment_operations.size();
2069 ResourceTracker **trackers = command->trackers();
2070 RDD::AttachmentLoadOp *load_ops = command->load_ops();
2071 RDD::AttachmentStoreOp *store_ops = command->store_ops();
2072 for (uint32_t i = 0; i < command->trackers_count; i++) {
2073 ResourceTracker *resource_tracker = framebuffer_cache->trackers[i];
2074 if (resource_tracker != nullptr) {
2075 if (i < command->clear_values_count && i < attachment_op_count && draw_instruction_list.attachment_operations[i] == ATTACHMENT_OPERATION_CLEAR) {
2076 load_ops[i] = RDD::ATTACHMENT_LOAD_OP_CLEAR;
2077 } else if (i < attachment_op_count && draw_instruction_list.attachment_operations[i] == ATTACHMENT_OPERATION_IGNORE) {
2078 load_ops[i] = RDD::ATTACHMENT_LOAD_OP_DONT_CARE;
2079 } else if (resource_tracker->is_discardable) {
2080 bool resource_has_parent = resource_tracker->parent != nullptr;
2081 ResourceTracker *search_tracker = resource_has_parent ? resource_tracker->parent : resource_tracker;
2082 search_tracker->reset_if_outdated(tracking_frame);
2083 bool resource_was_modified_this_frame = search_tracker->write_command_or_list_index >= 0;
2084 load_ops[i] = resource_was_modified_this_frame ? RDD::ATTACHMENT_LOAD_OP_LOAD : RDD::ATTACHMENT_LOAD_OP_DONT_CARE;
2085 } else {
2086 load_ops[i] = RDD::ATTACHMENT_LOAD_OP_LOAD;
2087 }
2088
2089 store_ops[i] = resource_tracker->is_discardable ? RDD::ATTACHMENT_STORE_OP_DONT_CARE : RDD::ATTACHMENT_STORE_OP_STORE;
2090 } else {
2091 load_ops[i] = RDD::ATTACHMENT_LOAD_OP_DONT_CARE;
2092 store_ops[i] = RDD::ATTACHMENT_STORE_OP_DONT_CARE;
2093 }
2094
2095 trackers[i] = resource_tracker;
2096 }
2097
2098 RDD::RenderPassClearValue *clear_values = command->clear_values();
2099 for (uint32_t i = 0; i < command->clear_values_count; i++) {
2100 clear_values[i] = draw_instruction_list.attachment_clear_values[i];
2101 }
2102
2103 memcpy(command->instruction_data(), draw_instruction_list.data.ptr(), instruction_data_size);
2104 _add_command_to_graph(draw_instruction_list.command_trackers.ptr(), draw_instruction_list.command_tracker_usages.ptr(), draw_instruction_list.command_trackers.size(), command_index, command);
2105 }
2106
2107 void RenderingDeviceGraph::add_texture_clear_color(RDD::TextureID p_dst, ResourceTracker *p_dst_tracker, const Color &p_color, const RDD::TextureSubresourceRange &p_range) {
2108 DEV_ASSERT(p_dst_tracker != nullptr);
2109
2110 int32_t command_index;
2111 RecordedTextureClearColorCommand *command = static_cast<RecordedTextureClearColorCommand *>(_allocate_command(sizeof(RecordedTextureClearColorCommand), command_index));
2112 command->type = RecordedCommand::TYPE_TEXTURE_CLEAR_COLOR;
2113 command->texture = p_dst;
2114 command->color = p_color;
2115 command->range = p_range;
2116
2117 ResourceUsage usage;
2118 if (driver_clears_with_copy_engine) {
2119 command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;
2120 usage = RESOURCE_USAGE_COPY_TO;
2121 } else {
2122 // If the driver is uncapable of using the copy engine for clearing the image (e.g. D3D12), we must either transition the
2123 // resource to a render target or a storage image as that's the only two ways it can perform the operation.
2124 if (p_dst_tracker->texture_usage & RDD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT) {
2125 command->self_stages = RDD::PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
2126 usage = RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE;
2127 } else {
2128 command->self_stages = RDD::PIPELINE_STAGE_CLEAR_STORAGE_BIT;
2129 usage = RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE;
2130 }
2131 }
2132
2133 _add_command_to_graph(&p_dst_tracker, &usage, 1, command_index, command);
2134 }
2135
2136 void RenderingDeviceGraph::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) {
2137 DEV_ASSERT(p_dst_tracker != nullptr);
2138
2139 int32_t command_index;
2140 RecordedTextureClearDepthStencilCommand *command = static_cast<RecordedTextureClearDepthStencilCommand *>(_allocate_command(sizeof(RecordedTextureClearDepthStencilCommand), command_index));
2141 command->type = RecordedCommand::TYPE_TEXTURE_CLEAR_DEPTH_STENCIL;
2142 command->texture = p_dst;
2143 command->depth = p_depth;
2144 command->stencil = p_stencil;
2145 command->range = p_range;
2146
2147 ResourceUsage usage;
2148 if (driver_clears_with_copy_engine) {
2149 command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;
2150 usage = RESOURCE_USAGE_COPY_TO;
2151 } else {
2152 // If the driver is uncapable of using the copy engine for clearing the image (e.g. D3D12), we must transition the
2153 // resource to a depth stencil as that's the only way it can perform the operation.
2154 command->self_stages = RDD::PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | RDD::PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
2155 usage = RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE;
2156 }
2157
2158 _add_command_to_graph(&p_dst_tracker, &usage, 1, command_index, command);
2159 }
2160
2161 void RenderingDeviceGraph::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) {
2162 DEV_ASSERT(p_src_tracker != nullptr);
2163 DEV_ASSERT(p_dst_tracker != nullptr);
2164
2165 int32_t command_index;
2166 uint64_t command_size = sizeof(RecordedTextureCopyCommand) + p_texture_copy_regions.size() * sizeof(RDD::TextureCopyRegion);
2167 RecordedTextureCopyCommand *command = static_cast<RecordedTextureCopyCommand *>(_allocate_command(command_size, command_index));
2168 command->type = RecordedCommand::TYPE_TEXTURE_COPY;
2169 command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;
2170 command->from_texture = p_src;
2171 command->to_texture = p_dst;
2172 command->texture_copy_regions_count = p_texture_copy_regions.size();
2173
2174 RDD::TextureCopyRegion *texture_copy_regions = command->texture_copy_regions();
2175 for (uint32_t i = 0; i < command->texture_copy_regions_count; i++) {
2176 texture_copy_regions[i] = p_texture_copy_regions[i];
2177 }
2178
2179 ResourceTracker *trackers[2] = { p_dst_tracker, p_src_tracker };
2180 ResourceUsage usages[2] = { RESOURCE_USAGE_COPY_TO, RESOURCE_USAGE_COPY_FROM };
2181 _add_command_to_graph(trackers, usages, 2, command_index, command);
2182 }
2183
2184 void RenderingDeviceGraph::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) {
2185 DEV_ASSERT(p_src_tracker != nullptr);
2186
2187 int32_t command_index;
2188 uint64_t command_size = sizeof(RecordedTextureGetDataCommand) + p_buffer_texture_copy_regions.size() * sizeof(RDD::BufferTextureCopyRegion);
2189 RecordedTextureGetDataCommand *command = static_cast<RecordedTextureGetDataCommand *>(_allocate_command(command_size, command_index));
2190 command->type = RecordedCommand::TYPE_TEXTURE_GET_DATA;
2191 command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;
2192 command->from_texture = p_src;
2193 command->to_buffer = p_dst;
2194 command->buffer_texture_copy_regions_count = p_buffer_texture_copy_regions.size();
2195
2196 RDD::BufferTextureCopyRegion *buffer_texture_copy_regions = command->buffer_texture_copy_regions();
2197 for (uint32_t i = 0; i < command->buffer_texture_copy_regions_count; i++) {
2198 buffer_texture_copy_regions[i] = p_buffer_texture_copy_regions[i];
2199 }
2200
2201 if (p_dst_tracker != nullptr) {
2202 // Add the optional destination tracker if it was provided.
2203 ResourceTracker *trackers[2] = { p_dst_tracker, p_src_tracker };
2204 ResourceUsage usages[2] = { RESOURCE_USAGE_COPY_TO, RESOURCE_USAGE_COPY_FROM };
2205 _add_command_to_graph(trackers, usages, 2, command_index, command);
2206 } else {
2207 ResourceUsage usage = RESOURCE_USAGE_COPY_FROM;
2208 _add_command_to_graph(&p_src_tracker, &usage, 1, command_index, command);
2209 }
2210 }
2211
2212 void RenderingDeviceGraph::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) {
2213 DEV_ASSERT(p_src_tracker != nullptr);
2214 DEV_ASSERT(p_dst_tracker != nullptr);
2215
2216 int32_t command_index;
2217 RecordedTextureResolveCommand *command = static_cast<RecordedTextureResolveCommand *>(_allocate_command(sizeof(RecordedTextureResolveCommand), command_index));
2218 command->type = RecordedCommand::TYPE_TEXTURE_RESOLVE;
2219 command->self_stages = RDD::PIPELINE_STAGE_RESOLVE_BIT;
2220 command->from_texture = p_src;
2221 command->to_texture = p_dst;
2222 command->src_layer = p_src_layer;
2223 command->src_mipmap = p_src_mipmap;
2224 command->dst_layer = p_dst_layer;
2225 command->dst_mipmap = p_dst_mipmap;
2226
2227 ResourceTracker *trackers[2] = { p_dst_tracker, p_src_tracker };
2228 ResourceUsage usages[2] = { RESOURCE_USAGE_RESOLVE_TO, RESOURCE_USAGE_RESOLVE_FROM };
2229 _add_command_to_graph(trackers, usages, 2, command_index, command);
2230 }
2231
2232 void RenderingDeviceGraph::add_texture_update(RDD::TextureID p_dst, ResourceTracker *p_dst_tracker, VectorView<RecordedBufferToTextureCopy> p_buffer_copies, VectorView<ResourceTracker *> p_buffer_trackers) {
2233 DEV_ASSERT(p_dst_tracker != nullptr);
2234
2235 int32_t command_index;
2236 uint64_t command_size = sizeof(RecordedTextureUpdateCommand) + p_buffer_copies.size() * sizeof(RecordedBufferToTextureCopy);
2237 RecordedTextureUpdateCommand *command = static_cast<RecordedTextureUpdateCommand *>(_allocate_command(command_size, command_index));
2238 command->type = RecordedCommand::TYPE_TEXTURE_UPDATE;
2239 command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;
2240 command->to_texture = p_dst;
2241 command->buffer_to_texture_copies_count = p_buffer_copies.size();
2242
2243 RecordedBufferToTextureCopy *buffer_to_texture_copies = command->buffer_to_texture_copies();
2244 for (uint32_t i = 0; i < command->buffer_to_texture_copies_count; i++) {
2245 buffer_to_texture_copies[i] = p_buffer_copies[i];
2246 }
2247
2248 if (p_buffer_trackers.size() > 0) {
2249 // Add the optional buffer trackers if they were provided.
2250 thread_local LocalVector<ResourceTracker *> trackers;
2251 thread_local LocalVector<ResourceUsage> usages;
2252 trackers.clear();
2253 usages.clear();
2254 for (uint32_t i = 0; i < p_buffer_trackers.size(); i++) {
2255 trackers.push_back(p_buffer_trackers[i]);
2256 usages.push_back(RESOURCE_USAGE_COPY_FROM);
2257 }
2258
2259 trackers.push_back(p_dst_tracker);
2260 usages.push_back(RESOURCE_USAGE_COPY_TO);
2261
2262 _add_command_to_graph(trackers.ptr(), usages.ptr(), trackers.size(), command_index, command);
2263 } else {
2264 ResourceUsage usage = RESOURCE_USAGE_COPY_TO;
2265 _add_command_to_graph(&p_dst_tracker, &usage, 1, command_index, command);
2266 }
2267 }
2268
2269 void RenderingDeviceGraph::add_capture_timestamp(RDD::QueryPoolID p_query_pool, uint32_t p_index) {
2270 int32_t command_index;
2271 RecordedCaptureTimestampCommand *command = static_cast<RecordedCaptureTimestampCommand *>(_allocate_command(sizeof(RecordedCaptureTimestampCommand), command_index));
2272 command->type = RecordedCommand::TYPE_CAPTURE_TIMESTAMP;
2273 command->self_stages = 0;
2274 command->pool = p_query_pool;
2275 command->index = p_index;
2276 _add_command_to_graph(nullptr, nullptr, 0, command_index, command);
2277 }
2278
2279 void RenderingDeviceGraph::add_synchronization() {
2280 // Synchronization is only acknowledged if commands have been recorded on the graph already.
2281 if (command_count > 0) {
2282 command_synchronization_pending = true;
2283 }
2284 }
2285
2286 void RenderingDeviceGraph::begin_label(const Span<char> &p_label_name, const Color &p_color) {
2287 uint32_t command_label_offset = command_label_chars.size();
2288 int command_label_size = p_label_name.size();
2289 command_label_chars.resize(command_label_offset + command_label_size + 1);
2290 memcpy(&command_label_chars[command_label_offset], p_label_name.ptr(), command_label_size);
2291 command_label_chars[command_label_offset + command_label_size] = '\0';
2292 command_label_colors.push_back(p_color);
2293 command_label_offsets.push_back(command_label_offset);
2294 command_label_index = command_label_count;
2295 command_label_count++;
2296 }
2297
2298 void RenderingDeviceGraph::end_label() {
2299 command_label_index = -1;
2300 }
2301
2302 void RenderingDeviceGraph::end(bool p_reorder_commands, bool p_full_barriers, RDD::CommandBufferID &r_command_buffer, CommandBufferPool &r_command_buffer_pool) {
2303 if (command_count == 0) {
2304 // No commands have been logged, do nothing.
2305 return;
2306 }
2307
2308 thread_local LocalVector<RecordedCommandSort> commands_sorted;
2309 if (p_reorder_commands) {
2310 thread_local LocalVector<int64_t> command_stack;
2311 thread_local LocalVector<int32_t> sorted_command_indices;
2312 thread_local LocalVector<uint32_t> command_degrees;
2313 int32_t adjacency_list_index = 0;
2314 int32_t command_index;
2315
2316 // Count all the incoming connections to every node by traversing their adjacency list.
2317 command_degrees.resize(command_count);
2318 memset(command_degrees.ptr(), 0, sizeof(uint32_t) * command_degrees.size());
2319 for (uint32_t i = 0; i < command_count; i++) {
2320 const RecordedCommand &recorded_command = *reinterpret_cast<const RecordedCommand *>(&command_data[command_data_offsets[i]]);
2321 adjacency_list_index = recorded_command.adjacent_command_list_index;
2322 while (adjacency_list_index >= 0) {
2323 const RecordedCommandListNode &command_list_node = command_list_nodes[adjacency_list_index];
2324 DEV_ASSERT((command_list_node.command_index != int32_t(i)) && "Command can't have itself as a dependency.");
2325 command_degrees[command_list_node.command_index] += 1;
2326 adjacency_list_index = command_list_node.next_list_index;
2327 }
2328 }
2329
2330 // Push to the stack all nodes that have no incoming connections.
2331 command_stack.clear();
2332 for (uint32_t i = 0; i < command_count; i++) {
2333 if (command_degrees[i] == 0) {
2334 command_stack.push_back(i);
2335 }
2336 }
2337
2338 sorted_command_indices.clear();
2339 while (!command_stack.is_empty()) {
2340 // Pop command from the stack.
2341 command_index = command_stack[command_stack.size() - 1];
2342 command_stack.resize(command_stack.size() - 1);
2343
2344 // Add it to the sorted commands.
2345 sorted_command_indices.push_back(command_index);
2346
2347 // Search for its adjacents and lower their degree for every visit. If the degree reaches zero, we push the command to the stack.
2348 const uint32_t command_data_offset = command_data_offsets[command_index];
2349 const RecordedCommand &recorded_command = *reinterpret_cast<const RecordedCommand *>(&command_data[command_data_offset]);
2350 adjacency_list_index = recorded_command.adjacent_command_list_index;
2351 while (adjacency_list_index >= 0) {
2352 const RecordedCommandListNode &command_list_node = command_list_nodes[adjacency_list_index];
2353 uint32_t &command_degree = command_degrees[command_list_node.command_index];
2354 DEV_ASSERT(command_degree > 0);
2355 command_degree--;
2356 if (command_degree == 0) {
2357 command_stack.push_back(command_list_node.command_index);
2358 }
2359
2360 adjacency_list_index = command_list_node.next_list_index;
2361 }
2362 }
2363
2364 // Batch buffer, texture, draw lists and compute operations together.
2365 const uint32_t PriorityTable[RecordedCommand::TYPE_MAX] = {
2366 0, // TYPE_NONE
2367 1, // TYPE_BUFFER_CLEAR
2368 1, // TYPE_BUFFER_COPY
2369 1, // TYPE_BUFFER_GET_DATA
2370 1, // TYPE_BUFFER_UPDATE
2371 4, // TYPE_COMPUTE_LIST
2372 3, // TYPE_DRAW_LIST
2373 2, // TYPE_TEXTURE_CLEAR_COLOR
2374 2, // TYPE_TEXTURE_CLEAR_DEPTH_STENCIL
2375 2, // TYPE_TEXTURE_COPY
2376 2, // TYPE_TEXTURE_GET_DATA
2377 2, // TYPE_TEXTURE_RESOLVE
2378 2, // TYPE_TEXTURE_UPDATE
2379 2, // TYPE_CAPTURE_TIMESTAMP
2380 5, // TYPE_DRIVER_CALLBACK
2381 };
2382
2383 commands_sorted.clear();
2384 commands_sorted.resize(command_count);
2385
2386 for (uint32_t i = 0; i < command_count; i++) {
2387 const int32_t sorted_command_index = sorted_command_indices[i];
2388 const uint32_t command_data_offset = command_data_offsets[sorted_command_index];
2389 const RecordedCommand recorded_command = *reinterpret_cast<const RecordedCommand *>(&command_data[command_data_offset]);
2390 const uint32_t next_command_level = commands_sorted[sorted_command_index].level + 1;
2391 adjacency_list_index = recorded_command.adjacent_command_list_index;
2392 while (adjacency_list_index >= 0) {
2393 const RecordedCommandListNode &command_list_node = command_list_nodes[adjacency_list_index];
2394 uint32_t &adjacent_command_level = commands_sorted[command_list_node.command_index].level;
2395 if (adjacent_command_level < next_command_level) {
2396 adjacent_command_level = next_command_level;
2397 }
2398
2399 adjacency_list_index = command_list_node.next_list_index;
2400 }
2401
2402 commands_sorted[sorted_command_index].index = sorted_command_index;
2403 commands_sorted[sorted_command_index].priority = PriorityTable[recorded_command.type];
2404 }
2405 } else {
2406 commands_sorted.clear();
2407 commands_sorted.resize(command_count);
2408
2409 for (uint32_t i = 0; i < command_count; i++) {
2410 commands_sorted[i].index = i;
2411 }
2412 }
2413
2414 _wait_for_secondary_command_buffer_tasks();
2415
2416 if (command_count > 0) {
2417 int32_t current_label_index = -1;
2418 int32_t current_label_level = -1;
2419 _run_label_command_change(r_command_buffer, -1, -1, true, true, nullptr, 0, current_label_index, current_label_level);
2420
2421 if (device.workarounds.avoid_compute_after_draw) {
2422 // Reset the state of the workaround.
2423 workarounds_state.draw_list_found = false;
2424 }
2425
2426 #if PRINT_DRAW_LIST_STATS
2427 draw_list_total_size = 0;
2428 #endif
2429
2430 if (p_reorder_commands) {
2431 #if PRINT_RENDER_GRAPH
2432 print_line("BEFORE SORT");
2433 _print_render_commands(commands_sorted.ptr(), command_count);
2434 #endif
2435
2436 commands_sorted.sort();
2437
2438 #if PRINT_RENDER_GRAPH
2439 print_line("AFTER SORT");
2440 _print_render_commands(commands_sorted.ptr(), command_count);
2441 #endif
2442
2443 #if PRINT_COMMAND_RECORDING
2444 print_line(vformat("Recording %d commands", command_count));
2445 #endif
2446
2447 uint32_t boosted_priority = 0;
2448 uint32_t current_level = commands_sorted[0].level;
2449 uint32_t current_level_start = 0;
2450 for (uint32_t i = 0; i < command_count; i++) {
2451 if (current_level != commands_sorted[i].level) {
2452 RecordedCommandSort *level_command_ptr = &commands_sorted[current_level_start];
2453 uint32_t level_command_count = i - current_level_start;
2454 _boost_priority_for_render_commands(level_command_ptr, level_command_count, boosted_priority);
2455 _group_barriers_for_render_commands(r_command_buffer, level_command_ptr, level_command_count, p_full_barriers);
2456 _run_render_commands(current_level, level_command_ptr, level_command_count, r_command_buffer, r_command_buffer_pool, current_label_index, current_label_level);
2457 current_level = commands_sorted[i].level;
2458 current_level_start = i;
2459 }
2460 }
2461
2462 RecordedCommandSort *level_command_ptr = &commands_sorted[current_level_start];
2463 uint32_t level_command_count = command_count - current_level_start;
2464 _boost_priority_for_render_commands(level_command_ptr, level_command_count, boosted_priority);
2465 _group_barriers_for_render_commands(r_command_buffer, level_command_ptr, level_command_count, p_full_barriers);
2466 _run_render_commands(current_level, level_command_ptr, level_command_count, r_command_buffer, r_command_buffer_pool, current_label_index, current_label_level);
2467
2468 #if PRINT_RENDER_GRAPH
2469 print_line("COMMANDS", command_count, "LEVELS", current_level + 1);
2470 #endif
2471 } else {
2472 for (uint32_t i = 0; i < command_count; i++) {
2473 _group_barriers_for_render_commands(r_command_buffer, &commands_sorted[i], 1, p_full_barriers);
2474 _run_render_commands(i, &commands_sorted[i], 1, r_command_buffer, r_command_buffer_pool, current_label_index, current_label_level);
2475 }
2476 }
2477
2478 _run_label_command_change(r_command_buffer, -1, -1, false, false, nullptr, 0, current_label_index, current_label_level);
2479
2480 #if PRINT_DRAW_LIST_STATS
2481 print_line(vformat("Draw list %d bytes", draw_list_total_size));
2482 #endif
2483 #if PRINT_COMMAND_RECORDING
2484 print_line(vformat("Recorded %d commands", command_count));
2485 #endif
2486 }
2487
2488 // Advance the frame counter. It's not necessary to do this if no commands are recorded because that means no secondary command buffers were used.
2489 frame = (frame + 1) % frames.size();
2490 }
2491
2492 #if PRINT_RESOURCE_TRACKER_TOTAL
2493 static uint32_t resource_tracker_total = 0;
2494 #endif
2495
2496 RenderingDeviceGraph::ResourceTracker *RenderingDeviceGraph::resource_tracker_create() {
2497 #if PRINT_RESOURCE_TRACKER_TOTAL
2498 print_line("Resource trackers:", ++resource_tracker_total);
2499 #endif
2500 return memnew(ResourceTracker);
2501 }
2502
2503 void RenderingDeviceGraph::resource_tracker_free(ResourceTracker *p_tracker) {
2504 if (p_tracker == nullptr) {
2505 return;
2506 }
2507
2508 if (p_tracker->in_parent_dirty_list) {
2509 // Delete the tracker from the parent's dirty linked list.
2510 if (p_tracker->parent->dirty_shared_list == p_tracker) {
2511 p_tracker->parent->dirty_shared_list = p_tracker->next_shared;
2512 } else {
2513 ResourceTracker *node = p_tracker->parent->dirty_shared_list;
2514 while (node != nullptr) {
2515 if (node->next_shared == p_tracker) {
2516 node->next_shared = p_tracker->next_shared;
2517 node = nullptr;
2518 } else {
2519 node = node->next_shared;
2520 }
2521 }
2522 }
2523 }
2524
2525 memdelete(p_tracker);
2526
2527 #if PRINT_RESOURCE_TRACKER_TOTAL
2528 print_line("Resource trackers:", --resource_tracker_total);
2529 #endif
2530 }
2531
2532 RenderingDeviceGraph::FramebufferCache *RenderingDeviceGraph::framebuffer_cache_create() {
2533 return memnew(FramebufferCache);
2534 }
2535
2536 void RenderingDeviceGraph::framebuffer_cache_free(RDD *p_driver, FramebufferCache *p_cache) {
2537 DEV_ASSERT(p_driver != nullptr);
2538
2539 if (p_cache == nullptr) {
2540 return;
2541 }
2542
2543 for (KeyValue<uint64_t, FramebufferStorage> &E : p_cache->storage_map) {
2544 p_driver->framebuffer_free(E.value.framebuffer);
2545 p_driver->render_pass_free(E.value.render_pass);
2546 }
2547
2548 memdelete(p_cache);
2549 }