Path: blob/master/servers/rendering/rendering_device_graph.cpp
10277 views
/**************************************************************************/1/* rendering_device_graph.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "rendering_device_graph.h"3132#define PRINT_RENDER_GRAPH 033#define FORCE_FULL_ACCESS_BITS 034#define PRINT_RESOURCE_TRACKER_TOTAL 035#define PRINT_COMMAND_RECORDING 03637RenderingDeviceGraph::RenderingDeviceGraph() {38driver_honors_barriers = false;39driver_clears_with_copy_engine = false;40}4142RenderingDeviceGraph::~RenderingDeviceGraph() {43}4445String RenderingDeviceGraph::_usage_to_string(ResourceUsage p_usage) {46switch (p_usage) {47case RESOURCE_USAGE_NONE:48return "None";49case RESOURCE_USAGE_COPY_FROM:50return "Copy From";51case RESOURCE_USAGE_COPY_TO:52return "Copy To";53case RESOURCE_USAGE_RESOLVE_FROM:54return "Resolve From";55case RESOURCE_USAGE_RESOLVE_TO:56return "Resolve To";57case RESOURCE_USAGE_UNIFORM_BUFFER_READ:58return "Uniform Buffer Read";59case RESOURCE_USAGE_INDIRECT_BUFFER_READ:60return "Indirect Buffer Read";61case RESOURCE_USAGE_TEXTURE_BUFFER_READ:62return "Texture Buffer Read";63case RESOURCE_USAGE_TEXTURE_BUFFER_READ_WRITE:64return "Texture Buffer Read Write";65case RESOURCE_USAGE_STORAGE_BUFFER_READ:66return "Storage Buffer Read";67case RESOURCE_USAGE_STORAGE_BUFFER_READ_WRITE:68return "Storage Buffer Read Write";69case RESOURCE_USAGE_VERTEX_BUFFER_READ:70return "Vertex Buffer Read";71case RESOURCE_USAGE_INDEX_BUFFER_READ:72return "Index Buffer Read";73case RESOURCE_USAGE_TEXTURE_SAMPLE:74return "Texture Sample";75case RESOURCE_USAGE_STORAGE_IMAGE_READ:76return "Storage Image Read";77case RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE:78return "Storage Image Read Write";79case RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE:80return "Attachment Color Read Write";81case RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE:82return "Attachment Depth Stencil Read Write";83case RESOURCE_USAGE_GENERAL:84return "General";85default:86ERR_FAIL_V_MSG("Invalid", vformat("Invalid resource usage %d.", p_usage));87}88}8990bool RenderingDeviceGraph::_is_write_usage(ResourceUsage p_usage) {91switch (p_usage) {92case RESOURCE_USAGE_COPY_FROM:93case RESOURCE_USAGE_RESOLVE_FROM:94case RESOURCE_USAGE_UNIFORM_BUFFER_READ:95case RESOURCE_USAGE_INDIRECT_BUFFER_READ:96case RESOURCE_USAGE_TEXTURE_BUFFER_READ:97case RESOURCE_USAGE_STORAGE_BUFFER_READ:98case RESOURCE_USAGE_VERTEX_BUFFER_READ:99case RESOURCE_USAGE_INDEX_BUFFER_READ:100case RESOURCE_USAGE_TEXTURE_SAMPLE:101case RESOURCE_USAGE_STORAGE_IMAGE_READ:102case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_SHADING_RATE_READ:103case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_DENSITY_MAP_READ:104return false;105case RESOURCE_USAGE_COPY_TO:106case RESOURCE_USAGE_RESOLVE_TO:107case RESOURCE_USAGE_TEXTURE_BUFFER_READ_WRITE:108case RESOURCE_USAGE_STORAGE_BUFFER_READ_WRITE:109case RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE:110case RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE:111case RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE:112case RESOURCE_USAGE_GENERAL:113return true;114default:115DEV_ASSERT(false && "Invalid resource tracker usage.");116return false;117}118}119120RDD::TextureLayout RenderingDeviceGraph::_usage_to_image_layout(ResourceUsage p_usage) {121switch (p_usage) {122case RESOURCE_USAGE_COPY_FROM:123return RDD::TEXTURE_LAYOUT_COPY_SRC_OPTIMAL;124case RESOURCE_USAGE_COPY_TO:125return RDD::TEXTURE_LAYOUT_COPY_DST_OPTIMAL;126case RESOURCE_USAGE_RESOLVE_FROM:127return RDD::TEXTURE_LAYOUT_RESOLVE_SRC_OPTIMAL;128case RESOURCE_USAGE_RESOLVE_TO:129return RDD::TEXTURE_LAYOUT_RESOLVE_DST_OPTIMAL;130case RESOURCE_USAGE_TEXTURE_SAMPLE:131return RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;132case RESOURCE_USAGE_STORAGE_IMAGE_READ:133case RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE:134return RDD::TEXTURE_LAYOUT_STORAGE_OPTIMAL;135case RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE:136return RDD::TEXTURE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;137case RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE:138return RDD::TEXTURE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;139case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_SHADING_RATE_READ:140return RDD::TEXTURE_LAYOUT_FRAGMENT_SHADING_RATE_ATTACHMENT_OPTIMAL;141case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_DENSITY_MAP_READ:142return RDD::TEXTURE_LAYOUT_FRAGMENT_DENSITY_MAP_ATTACHMENT_OPTIMAL;143case RESOURCE_USAGE_GENERAL:144return RDD::TEXTURE_LAYOUT_GENERAL;145case RESOURCE_USAGE_NONE:146return RDD::TEXTURE_LAYOUT_UNDEFINED;147default:148DEV_ASSERT(false && "Invalid resource tracker usage or not an image usage.");149return RDD::TEXTURE_LAYOUT_UNDEFINED;150}151}152153RDD::BarrierAccessBits RenderingDeviceGraph::_usage_to_access_bits(ResourceUsage p_usage) {154#if FORCE_FULL_ACCESS_BITS155return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_MEMORY_READ_BIT | RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT);156#else157switch (p_usage) {158case RESOURCE_USAGE_NONE:159return RDD::BarrierAccessBits(0);160case RESOURCE_USAGE_COPY_FROM:161return RDD::BARRIER_ACCESS_COPY_READ_BIT;162case RESOURCE_USAGE_COPY_TO:163return RDD::BARRIER_ACCESS_COPY_WRITE_BIT;164case RESOURCE_USAGE_RESOLVE_FROM:165return RDD::BARRIER_ACCESS_RESOLVE_READ_BIT;166case RESOURCE_USAGE_RESOLVE_TO:167return RDD::BARRIER_ACCESS_RESOLVE_WRITE_BIT;168case RESOURCE_USAGE_UNIFORM_BUFFER_READ:169return RDD::BARRIER_ACCESS_UNIFORM_READ_BIT;170case RESOURCE_USAGE_INDIRECT_BUFFER_READ:171return RDD::BARRIER_ACCESS_INDIRECT_COMMAND_READ_BIT;172case RESOURCE_USAGE_STORAGE_BUFFER_READ:173case RESOURCE_USAGE_STORAGE_IMAGE_READ:174case RESOURCE_USAGE_TEXTURE_BUFFER_READ:175case RESOURCE_USAGE_TEXTURE_SAMPLE:176return RDD::BARRIER_ACCESS_SHADER_READ_BIT;177case RESOURCE_USAGE_TEXTURE_BUFFER_READ_WRITE:178case RESOURCE_USAGE_STORAGE_BUFFER_READ_WRITE:179case RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE:180return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_SHADER_READ_BIT | RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);181case RESOURCE_USAGE_VERTEX_BUFFER_READ:182return RDD::BARRIER_ACCESS_VERTEX_ATTRIBUTE_READ_BIT;183case RESOURCE_USAGE_INDEX_BUFFER_READ:184return RDD::BARRIER_ACCESS_INDEX_READ_BIT;185case RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE:186return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_READ_BIT | RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_WRITE_BIT);187case RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE:188return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | RDD::BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT);189case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_SHADING_RATE_READ:190return RDD::BARRIER_ACCESS_FRAGMENT_SHADING_RATE_ATTACHMENT_READ_BIT;191case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_DENSITY_MAP_READ:192return RDD::BARRIER_ACCESS_FRAGMENT_DENSITY_MAP_ATTACHMENT_READ_BIT;193case RESOURCE_USAGE_GENERAL:194return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_MEMORY_READ_BIT | RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT);195default:196DEV_ASSERT(false && "Invalid usage.");197return RDD::BarrierAccessBits(0);198}199#endif200}201202bool RenderingDeviceGraph::_check_command_intersection(ResourceTracker *p_resource_tracker, int32_t p_previous_command_index, int32_t p_command_index) const {203if (p_resource_tracker->usage != RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE && p_resource_tracker->usage != RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE) {204// We don't check possible intersections for usages that aren't consecutive color or depth writes.205return true;206}207208const uint32_t previous_command_data_offset = command_data_offsets[p_previous_command_index];209const uint32_t current_command_data_offset = command_data_offsets[p_command_index];210const RecordedDrawListCommand &previous_draw_list_command = *reinterpret_cast<const RecordedDrawListCommand *>(&command_data[previous_command_data_offset]);211const RecordedDrawListCommand ¤t_draw_list_command = *reinterpret_cast<const RecordedDrawListCommand *>(&command_data[current_command_data_offset]);212if (previous_draw_list_command.type != RecordedCommand::TYPE_DRAW_LIST || current_draw_list_command.type != RecordedCommand::TYPE_DRAW_LIST) {213// We don't check possible intersections if both commands aren't draw lists.214return true;215}216217// We check if the region used by both draw lists have an intersection.218return previous_draw_list_command.region.intersects(current_draw_list_command.region);219}220221bool RenderingDeviceGraph::_check_command_partial_coverage(ResourceTracker *p_resource_tracker, int32_t p_command_index) const {222if (p_resource_tracker->usage != RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE && p_resource_tracker->usage != RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE) {223// We don't check for partial coverage in usages that aren't attachment writes.224return false;225}226227const uint32_t command_data_offset = command_data_offsets[p_command_index];228const RecordedDrawListCommand &draw_list_command = *reinterpret_cast<const RecordedDrawListCommand *>(&command_data[command_data_offset]);229if (draw_list_command.type != RecordedCommand::TYPE_DRAW_LIST) {230// We don't check for partial coverage on commands that aren't draw lists.231return false;232}233234Rect2i texture_region(Point2i(0, 0), p_resource_tracker->texture_size);235return !draw_list_command.region.encloses(texture_region);236}237238int32_t RenderingDeviceGraph::_add_to_command_list(int32_t p_command_index, int32_t p_list_index) {239DEV_ASSERT(p_command_index < int32_t(command_count));240DEV_ASSERT(p_list_index < int32_t(command_list_nodes.size()));241242int32_t next_index = int32_t(command_list_nodes.size());243command_list_nodes.resize(next_index + 1);244245RecordedCommandListNode &new_node = command_list_nodes[next_index];246new_node.command_index = p_command_index;247new_node.next_list_index = p_list_index;248return next_index;249}250251void RenderingDeviceGraph::_add_adjacent_command(int32_t p_previous_command_index, int32_t p_command_index, RecordedCommand *r_command) {252const uint32_t previous_command_data_offset = command_data_offsets[p_previous_command_index];253RecordedCommand &previous_command = *reinterpret_cast<RecordedCommand *>(&command_data[previous_command_data_offset]);254previous_command.adjacent_command_list_index = _add_to_command_list(p_command_index, previous_command.adjacent_command_list_index);255previous_command.next_stages = previous_command.next_stages | r_command->self_stages;256r_command->previous_stages = r_command->previous_stages | previous_command.self_stages;257}258259int32_t RenderingDeviceGraph::_add_to_slice_read_list(int32_t p_command_index, Rect2i p_subresources, int32_t p_list_index) {260DEV_ASSERT(p_command_index < int32_t(command_count));261DEV_ASSERT(p_list_index < int32_t(read_slice_list_nodes.size()));262263int32_t next_index = int32_t(read_slice_list_nodes.size());264read_slice_list_nodes.resize(next_index + 1);265266RecordedSliceListNode &new_node = read_slice_list_nodes[next_index];267new_node.command_index = p_command_index;268new_node.next_list_index = p_list_index;269new_node.subresources = p_subresources;270return next_index;271}272273int32_t RenderingDeviceGraph::_add_to_write_list(int32_t p_command_index, Rect2i p_subresources, int32_t p_list_index, bool p_partial_coverage) {274DEV_ASSERT(p_command_index < int32_t(command_count));275DEV_ASSERT(p_list_index < int32_t(write_slice_list_nodes.size()));276277int32_t next_index = int32_t(write_slice_list_nodes.size());278write_slice_list_nodes.resize(next_index + 1);279280RecordedSliceListNode &new_node = write_slice_list_nodes[next_index];281new_node.command_index = p_command_index;282new_node.next_list_index = p_list_index;283new_node.subresources = p_subresources;284new_node.partial_coverage = p_partial_coverage;285return next_index;286}287288// Ensures all commands are 8-byte aligned.289#define GRAPH_ALIGN(x) (((x) + 7u) & 0xFFFFFFF8u)290291RenderingDeviceGraph::RecordedCommand *RenderingDeviceGraph::_allocate_command(uint32_t p_command_size, int32_t &r_command_index) {292uint32_t command_data_offset = command_data.size();293command_data_offset = GRAPH_ALIGN(command_data_offset);294command_data_offsets.push_back(command_data_offset);295command_data.resize(command_data_offset + p_command_size);296r_command_index = command_count++;297RecordedCommand *new_command = reinterpret_cast<RecordedCommand *>(&command_data[command_data_offset]);298*new_command = RecordedCommand();299return new_command;300}301302RenderingDeviceGraph::DrawListInstruction *RenderingDeviceGraph::_allocate_draw_list_instruction(uint32_t p_instruction_size) {303uint32_t draw_list_data_offset = draw_instruction_list.data.size();304draw_list_data_offset = GRAPH_ALIGN(draw_list_data_offset);305draw_instruction_list.data.resize(draw_list_data_offset + p_instruction_size);306return reinterpret_cast<DrawListInstruction *>(&draw_instruction_list.data[draw_list_data_offset]);307}308309RenderingDeviceGraph::ComputeListInstruction *RenderingDeviceGraph::_allocate_compute_list_instruction(uint32_t p_instruction_size) {310uint32_t compute_list_data_offset = compute_instruction_list.data.size();311compute_list_data_offset = GRAPH_ALIGN(compute_list_data_offset);312compute_instruction_list.data.resize(compute_list_data_offset + p_instruction_size);313return reinterpret_cast<ComputeListInstruction *>(&compute_instruction_list.data[compute_list_data_offset]);314}315316void RenderingDeviceGraph::_check_discardable_attachment_dependency(ResourceTracker *p_resource_tracker, int32_t p_previous_command_index, int32_t p_command_index) {317if (!p_resource_tracker->is_discardable) {318return;319}320321// 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.322uint32_t command_offset = command_data_offsets[p_command_index];323RecordedDrawListCommand *draw_list_command = reinterpret_cast<RecordedDrawListCommand *>(&command_data[command_offset]);324if (draw_list_command->type == RecordedCommand::TYPE_DRAW_LIST) {325ResourceTracker **trackers = draw_list_command->trackers();326for (uint32_t i = 0; i < draw_list_command->trackers_count; i++) {327if (trackers[i] == p_resource_tracker && draw_list_command->load_ops()[i] == RDD::ATTACHMENT_LOAD_OP_CLEAR) {328return;329}330}331}332333// Check if the previous command is a draw list.334uint32_t previous_command_offset = command_data_offsets[p_previous_command_index];335RecordedDrawListCommand *previous_draw_list_command = reinterpret_cast<RecordedDrawListCommand *>(&command_data[previous_command_offset]);336if (previous_draw_list_command->type != RecordedCommand::TYPE_DRAW_LIST) {337return;338}339340// Search for the tracker inside the draw list command and modify the store operation accordingly.341ResourceTracker **trackers = previous_draw_list_command->trackers();342for (uint32_t i = 0; i < previous_draw_list_command->trackers_count; i++) {343if (trackers[i] == p_resource_tracker) {344previous_draw_list_command->store_ops()[i] = RDD::ATTACHMENT_STORE_OP_STORE;345return;346}347}348}349350void 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) {351// Assign the next stages derived from the stages the command requires first.352r_command->next_stages = r_command->self_stages;353354if (command_label_index >= 0) {355// If a label is active, tag the command with the label.356r_command->label_index = command_label_index;357}358359if (r_command->type == RecordedCommand::TYPE_CAPTURE_TIMESTAMP) {360// All previous commands starting from the previous timestamp should be adjacent to this command.361int32_t start_command_index = uint32_t(MAX(command_timestamp_index, 0));362for (int32_t i = start_command_index; i < p_command_index; i++) {363_add_adjacent_command(i, p_command_index, r_command);364}365366// Make this command the new active timestamp command.367command_timestamp_index = p_command_index;368} else if (command_timestamp_index >= 0) {369// Timestamp command should be adjacent to this command.370_add_adjacent_command(command_timestamp_index, p_command_index, r_command);371}372373if (command_synchronization_pending) {374// All previous commands should be adjacent to this command.375int32_t start_command_index = uint32_t(MAX(command_synchronization_index, 0));376for (int32_t i = start_command_index; i < p_command_index; i++) {377_add_adjacent_command(i, p_command_index, r_command);378}379380command_synchronization_index = p_command_index;381command_synchronization_pending = false;382} else if (command_synchronization_index >= 0) {383// Synchronization command should be adjacent to this command.384_add_adjacent_command(command_synchronization_index, p_command_index, r_command);385}386387for (uint32_t i = 0; i < p_resource_count; i++) {388ResourceTracker *resource_tracker = p_resource_trackers[i];389DEV_ASSERT(resource_tracker != nullptr);390391resource_tracker->reset_if_outdated(tracking_frame);392393const RDD::TextureSubresourceRange &subresources = resource_tracker->texture_subresources;394const Rect2i resource_tracker_rect(subresources.base_mipmap, subresources.base_layer, subresources.mipmap_count, subresources.layer_count);395Rect2i search_tracker_rect = resource_tracker_rect;396397ResourceUsage new_resource_usage = p_resource_usages[i];398bool write_usage = _is_write_usage(new_resource_usage);399BitField<RDD::BarrierAccessBits> new_usage_access = _usage_to_access_bits(new_resource_usage);400bool is_resource_a_slice = resource_tracker->parent != nullptr;401if (is_resource_a_slice) {402// This resource depends on a parent resource.403resource_tracker->parent->reset_if_outdated(tracking_frame);404405if (resource_tracker->texture_slice_command_index != p_command_index) {406// Indicate this slice has been used by this command.407resource_tracker->texture_slice_command_index = p_command_index;408}409410if (resource_tracker->parent->usage == RESOURCE_USAGE_NONE) {411if (resource_tracker->parent->texture_driver_id.id != 0) {412// If the resource is a texture, we transition it entirely to the layout determined by the first slice that uses it.413_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);414}415416// If the parent hasn't been used yet, we assign the usage of the slice to the entire resource.417resource_tracker->parent->usage = new_resource_usage;418419// Also assign the usage to the slice and consider it a write operation. Consider the parent's current usage access as its own.420resource_tracker->usage = new_resource_usage;421resource_tracker->usage_access = resource_tracker->parent->usage_access;422write_usage = true;423424// Indicate the area that should be tracked is the entire resource.425const RDD::TextureSubresourceRange &parent_subresources = resource_tracker->parent->texture_subresources;426search_tracker_rect = Rect2i(parent_subresources.base_mipmap, parent_subresources.base_layer, parent_subresources.mipmap_count, parent_subresources.layer_count);427} else if (resource_tracker->in_parent_dirty_list) {428if (resource_tracker->parent->usage == new_resource_usage) {429// The slice will be transitioned to the resource of the parent and can be deleted from the dirty list.430ResourceTracker *previous_tracker = nullptr;431ResourceTracker *current_tracker = resource_tracker->parent->dirty_shared_list;432bool initialized_dirty_rect = false;433while (current_tracker != nullptr) {434current_tracker->reset_if_outdated(tracking_frame);435436if (current_tracker == resource_tracker) {437current_tracker->in_parent_dirty_list = false;438439if (previous_tracker != nullptr) {440previous_tracker->next_shared = current_tracker->next_shared;441} else {442resource_tracker->parent->dirty_shared_list = current_tracker->next_shared;443}444445current_tracker = current_tracker->next_shared;446} else {447if (initialized_dirty_rect) {448resource_tracker->parent->texture_slice_or_dirty_rect = resource_tracker->parent->texture_slice_or_dirty_rect.merge(current_tracker->texture_slice_or_dirty_rect);449} else {450resource_tracker->parent->texture_slice_or_dirty_rect = current_tracker->texture_slice_or_dirty_rect;451initialized_dirty_rect = true;452}453454previous_tracker = current_tracker;455current_tracker = current_tracker->next_shared;456}457}458}459} else {460if (resource_tracker->parent->dirty_shared_list != nullptr && resource_tracker->parent->texture_slice_or_dirty_rect.intersects(resource_tracker->texture_slice_or_dirty_rect)) {461// There's an intersection with the current dirty area of the parent and the slice. We must verify if the intersection is against a slice462// 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.463ResourceTracker *previous_tracker = nullptr;464ResourceTracker *current_tracker = resource_tracker->parent->dirty_shared_list;465bool initialized_dirty_rect = false;466while (current_tracker != nullptr) {467current_tracker->reset_if_outdated(tracking_frame);468469if (current_tracker->texture_slice_or_dirty_rect.intersects(resource_tracker->texture_slice_or_dirty_rect)) {470if (current_tracker->command_frame == tracking_frame && current_tracker->texture_slice_command_index == p_command_index) {471ERR_FAIL_MSG("Texture slices that overlap can't be used in the same command.");472} else {473// Delete the slice from the dirty list and revert it to the usage of the parent.474if (current_tracker->texture_driver_id.id != 0) {475_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);476477// Merge the area of the slice with the current tracking area of the command and indicate it's a write usage as well.478search_tracker_rect = search_tracker_rect.merge(current_tracker->texture_slice_or_dirty_rect);479write_usage = true;480}481482current_tracker->in_parent_dirty_list = false;483484if (previous_tracker != nullptr) {485previous_tracker->next_shared = current_tracker->next_shared;486} else {487resource_tracker->parent->dirty_shared_list = current_tracker->next_shared;488}489490current_tracker = current_tracker->next_shared;491}492} else {493// Recalculate the dirty rect of the parent so the deleted slices are excluded.494if (initialized_dirty_rect) {495resource_tracker->parent->texture_slice_or_dirty_rect = resource_tracker->parent->texture_slice_or_dirty_rect.merge(current_tracker->texture_slice_or_dirty_rect);496} else {497resource_tracker->parent->texture_slice_or_dirty_rect = current_tracker->texture_slice_or_dirty_rect;498initialized_dirty_rect = true;499}500501previous_tracker = current_tracker;502current_tracker = current_tracker->next_shared;503}504}505}506507// 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.508resource_tracker->usage = resource_tracker->parent->usage;509resource_tracker->usage_access = resource_tracker->parent->usage_access;510511if (resource_tracker->usage != new_resource_usage) {512// Insert to the dirty list if the requested usage is different.513resource_tracker->next_shared = resource_tracker->parent->dirty_shared_list;514resource_tracker->parent->dirty_shared_list = resource_tracker;515resource_tracker->in_parent_dirty_list = true;516if (resource_tracker->parent->dirty_shared_list != nullptr) {517resource_tracker->parent->texture_slice_or_dirty_rect = resource_tracker->parent->texture_slice_or_dirty_rect.merge(resource_tracker->texture_slice_or_dirty_rect);518} else {519resource_tracker->parent->texture_slice_or_dirty_rect = resource_tracker->texture_slice_or_dirty_rect;520}521}522}523} else {524ResourceTracker *current_tracker = resource_tracker->dirty_shared_list;525if (current_tracker != nullptr) {526// Consider the usage as write if we must transition any of the slices.527write_usage = true;528}529530while (current_tracker != nullptr) {531current_tracker->reset_if_outdated(tracking_frame);532533if (current_tracker->texture_driver_id.id != 0) {534// Transition all slices to the layout of the parent resource.535_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);536}537538current_tracker->in_parent_dirty_list = false;539current_tracker = current_tracker->next_shared;540}541542resource_tracker->dirty_shared_list = nullptr;543}544545// Use the resource's parent tracker directly for all search operations.546bool resource_has_parent = resource_tracker->parent != nullptr;547ResourceTracker *search_tracker = resource_has_parent ? resource_tracker->parent : resource_tracker;548bool different_usage = resource_tracker->usage != new_resource_usage;549bool write_usage_after_write = (write_usage && search_tracker->write_command_or_list_index >= 0);550if (different_usage || write_usage_after_write) {551// 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.552if (resource_tracker->texture_driver_id.id != 0) {553if (resource_tracker->usage_access.is_empty()) {554// FIXME: If the tracker does not know the previous type of usage, assume the generic memory write one.555// Tracking access bits across texture slices can be tricky, so this failsafe can be removed once that's improved.556resource_tracker->usage_access = RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT;557}558559_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);560} else if (resource_tracker->buffer_driver_id.id != 0) {561#if USE_BUFFER_BARRIERS562_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);563#endif564// Memory barriers are pushed regardless of buffer barriers being used or not.565r_command->memory_barrier.src_access = r_command->memory_barrier.src_access | resource_tracker->usage_access;566r_command->memory_barrier.dst_access = r_command->memory_barrier.dst_access | new_usage_access;567} else {568DEV_ASSERT(false && "Resource tracker does not contain a valid buffer or texture ID.");569}570}571572// Always update the access of the tracker according to the latest usage.573resource_tracker->usage_access = new_usage_access;574575// Always accumulate the stages of the tracker with the commands that use it.576search_tracker->current_frame_stages = search_tracker->current_frame_stages | r_command->self_stages;577578if (!search_tracker->previous_frame_stages.is_empty()) {579// Add to the command the stages the tracker was used on in the previous frame.580r_command->previous_stages = r_command->previous_stages | search_tracker->previous_frame_stages;581search_tracker->previous_frame_stages.clear();582}583584if (different_usage) {585// 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.586// In the case of buffers however, this is not exactly necessary if the driver does not consider different buffer usages as different states.587write_usage = write_usage || bool(resource_tracker->texture_driver_id) || driver_buffers_require_transitions;588resource_tracker->usage = new_resource_usage;589}590591bool write_usage_has_partial_coverage = !different_usage && _check_command_partial_coverage(resource_tracker, p_command_index);592if (search_tracker->write_command_or_list_index >= 0) {593if (search_tracker->write_command_list_enabled) {594// Make this command adjacent to any commands that wrote to this resource and intersect with the slice if it applies.595// For buffers or textures that never use slices, this list will only be one element long at most.596int32_t previous_write_list_index = -1;597int32_t write_list_index = search_tracker->write_command_or_list_index;598while (write_list_index >= 0) {599const RecordedSliceListNode &write_list_node = write_slice_list_nodes[write_list_index];600if (!resource_has_parent || search_tracker_rect.intersects(write_list_node.subresources)) {601if (write_list_node.command_index == p_command_index) {602ERR_FAIL_COND_MSG(!resource_has_parent, "Command can't have itself as a dependency.");603} else if (!write_list_node.partial_coverage || _check_command_intersection(resource_tracker, write_list_node.command_index, p_command_index)) {604_check_discardable_attachment_dependency(search_tracker, write_list_node.command_index, p_command_index);605606// Command is dependent on this command. Add this command to the adjacency list of the write command.607_add_adjacent_command(write_list_node.command_index, p_command_index, r_command);608609if (resource_has_parent && write_usage && search_tracker_rect.encloses(write_list_node.subresources) && !write_usage_has_partial_coverage) {610// Eliminate redundant writes from the list.611if (previous_write_list_index >= 0) {612RecordedSliceListNode &previous_list_node = write_slice_list_nodes[previous_write_list_index];613previous_list_node.next_list_index = write_list_node.next_list_index;614} else {615search_tracker->write_command_or_list_index = write_list_node.next_list_index;616}617618write_list_index = write_list_node.next_list_index;619continue;620}621}622}623624previous_write_list_index = write_list_index;625write_list_index = write_list_node.next_list_index;626}627} else {628// The index is just the latest command index that wrote to the resource.629if (search_tracker->write_command_or_list_index == p_command_index) {630ERR_FAIL_MSG("Command can't have itself as a dependency.");631} else {632_check_discardable_attachment_dependency(search_tracker, search_tracker->write_command_or_list_index, p_command_index);633_add_adjacent_command(search_tracker->write_command_or_list_index, p_command_index, r_command);634}635}636}637638if (write_usage) {639bool use_write_list = resource_has_parent || write_usage_has_partial_coverage;640if (use_write_list) {641if (!search_tracker->write_command_list_enabled && search_tracker->write_command_or_list_index >= 0) {642// 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.643const RDD::TextureSubresourceRange &tracker_subresources = search_tracker->texture_subresources;644Rect2i tracker_rect(tracker_subresources.base_mipmap, tracker_subresources.base_layer, tracker_subresources.mipmap_count, tracker_subresources.layer_count);645search_tracker->write_command_or_list_index = _add_to_write_list(search_tracker->write_command_or_list_index, tracker_rect, -1, false);646}647648search_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);649search_tracker->write_command_list_enabled = true;650} else {651search_tracker->write_command_or_list_index = p_command_index;652search_tracker->write_command_list_enabled = false;653}654655// We add this command to the adjacency list of all commands that were reading from the entire resource.656int32_t read_full_command_list_index = search_tracker->read_full_command_list_index;657while (read_full_command_list_index >= 0) {658int32_t read_full_command_index = command_list_nodes[read_full_command_list_index].command_index;659int32_t read_full_next_index = command_list_nodes[read_full_command_list_index].next_list_index;660if (read_full_command_index == p_command_index) {661if (!resource_has_parent) {662// 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.663ERR_FAIL_MSG("Command can't have itself as a dependency.");664}665} else {666// Add this command to the adjacency list of each command that was reading this resource.667_add_adjacent_command(read_full_command_index, p_command_index, r_command);668}669670read_full_command_list_index = read_full_next_index;671}672673if (!use_write_list) {674// Clear the full list if this resource is not a slice.675search_tracker->read_full_command_list_index = -1;676}677678// We add this command to the adjacency list of all commands that were reading from resource slices.679int32_t previous_slice_command_list_index = -1;680int32_t read_slice_command_list_index = search_tracker->read_slice_command_list_index;681while (read_slice_command_list_index >= 0) {682const RecordedSliceListNode &read_list_node = read_slice_list_nodes[read_slice_command_list_index];683if (!use_write_list || search_tracker_rect.encloses(read_list_node.subresources)) {684if (previous_slice_command_list_index >= 0) {685// Erase this element and connect the previous one to the next element.686read_slice_list_nodes[previous_slice_command_list_index].next_list_index = read_list_node.next_list_index;687} else {688// Erase this element from the head of the list.689DEV_ASSERT(search_tracker->read_slice_command_list_index == read_slice_command_list_index);690search_tracker->read_slice_command_list_index = read_list_node.next_list_index;691}692693// Advance to the next element.694read_slice_command_list_index = read_list_node.next_list_index;695} else {696previous_slice_command_list_index = read_slice_command_list_index;697read_slice_command_list_index = read_list_node.next_list_index;698}699700if (!resource_has_parent || search_tracker_rect.intersects(read_list_node.subresources)) {701// Add this command to the adjacency list of each command that was reading this resource.702// We only add the dependency if there's an intersection between slices or this resource isn't a slice.703_add_adjacent_command(read_list_node.command_index, p_command_index, r_command);704}705}706} else if (resource_has_parent) {707// We add a read dependency to the tracker to indicate this command reads from the resource slice.708search_tracker->read_slice_command_list_index = _add_to_slice_read_list(p_command_index, resource_tracker_rect, search_tracker->read_slice_command_list_index);709} else {710// We add a read dependency to the tracker to indicate this command reads from the entire resource.711search_tracker->read_full_command_list_index = _add_to_command_list(p_command_index, search_tracker->read_full_command_list_index);712}713}714}715716void 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) {717if (!driver_honors_barriers) {718return;719}720721if (r_barrier_index < 0) {722r_barrier_index = r_barrier_vector.size();723}724725RDD::TextureBarrier texture_barrier;726texture_barrier.texture = p_texture_id;727texture_barrier.src_access = p_src_access;728texture_barrier.dst_access = p_dst_access;729texture_barrier.prev_layout = _usage_to_image_layout(p_prev_usage);730texture_barrier.next_layout = _usage_to_image_layout(p_next_usage);731texture_barrier.subresources = p_subresources;732r_barrier_vector.push_back(texture_barrier);733r_barrier_count++;734}735736#if USE_BUFFER_BARRIERS737void 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) {738if (!driver_honors_barriers) {739return;740}741742if (r_barrier_index < 0) {743r_barrier_index = command_buffer_barriers.size();744}745746RDD::BufferBarrier buffer_barrier;747buffer_barrier.buffer = p_buffer_id;748buffer_barrier.src_access = p_src_access;749buffer_barrier.dst_access = p_dst_access;750buffer_barrier.offset = 0;751buffer_barrier.size = RDD::BUFFER_WHOLE_SIZE;752command_buffer_barriers.push_back(buffer_barrier);753r_barrier_count++;754}755#endif756757void RenderingDeviceGraph::_run_compute_list_command(RDD::CommandBufferID p_command_buffer, const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {758uint32_t instruction_data_cursor = 0;759while (instruction_data_cursor < p_instruction_data_size) {760DEV_ASSERT((instruction_data_cursor + sizeof(ComputeListInstruction)) <= p_instruction_data_size);761762const ComputeListInstruction *instruction = reinterpret_cast<const ComputeListInstruction *>(&p_instruction_data[instruction_data_cursor]);763switch (instruction->type) {764case ComputeListInstruction::TYPE_BIND_PIPELINE: {765const ComputeListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const ComputeListBindPipelineInstruction *>(instruction);766driver->command_bind_compute_pipeline(p_command_buffer, bind_pipeline_instruction->pipeline);767instruction_data_cursor += sizeof(ComputeListBindPipelineInstruction);768} break;769case ComputeListInstruction::TYPE_BIND_UNIFORM_SETS: {770const ComputeListBindUniformSetsInstruction *bind_uniform_sets_instruction = reinterpret_cast<const ComputeListBindUniformSetsInstruction *>(instruction);771driver->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);772instruction_data_cursor += sizeof(ComputeListBindUniformSetsInstruction) + sizeof(RDD::UniformSetID) * bind_uniform_sets_instruction->set_count;773} break;774case ComputeListInstruction::TYPE_DISPATCH: {775const ComputeListDispatchInstruction *dispatch_instruction = reinterpret_cast<const ComputeListDispatchInstruction *>(instruction);776driver->command_compute_dispatch(p_command_buffer, dispatch_instruction->x_groups, dispatch_instruction->y_groups, dispatch_instruction->z_groups);777instruction_data_cursor += sizeof(ComputeListDispatchInstruction);778} break;779case ComputeListInstruction::TYPE_DISPATCH_INDIRECT: {780const ComputeListDispatchIndirectInstruction *dispatch_indirect_instruction = reinterpret_cast<const ComputeListDispatchIndirectInstruction *>(instruction);781driver->command_compute_dispatch_indirect(p_command_buffer, dispatch_indirect_instruction->buffer, dispatch_indirect_instruction->offset);782instruction_data_cursor += sizeof(ComputeListDispatchIndirectInstruction);783} break;784case ComputeListInstruction::TYPE_SET_PUSH_CONSTANT: {785const ComputeListSetPushConstantInstruction *set_push_constant_instruction = reinterpret_cast<const ComputeListSetPushConstantInstruction *>(instruction);786const VectorView push_constant_data_view(reinterpret_cast<const uint32_t *>(set_push_constant_instruction->data()), set_push_constant_instruction->size / sizeof(uint32_t));787driver->command_bind_push_constants(p_command_buffer, set_push_constant_instruction->shader, 0, push_constant_data_view);788instruction_data_cursor += sizeof(ComputeListSetPushConstantInstruction);789instruction_data_cursor += set_push_constant_instruction->size;790} break;791case ComputeListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE: {792const ComputeListUniformSetPrepareForUseInstruction *uniform_set_prepare_for_use_instruction = reinterpret_cast<const ComputeListUniformSetPrepareForUseInstruction *>(instruction);793driver->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);794instruction_data_cursor += sizeof(ComputeListUniformSetPrepareForUseInstruction);795} break;796default:797DEV_ASSERT(false && "Unknown compute list instruction type.");798return;799}800801instruction_data_cursor = GRAPH_ALIGN(instruction_data_cursor);802}803}804805void RenderingDeviceGraph::_get_draw_list_render_pass_and_framebuffer(const RecordedDrawListCommand *p_draw_list_command, RDD::RenderPassID &r_render_pass, RDD::FramebufferID &r_framebuffer) {806DEV_ASSERT(p_draw_list_command->trackers_count <= 21 && "Max number of attachments that can be encoded into the key.");807808// Build a unique key from the load and store ops for each attachment.809const RDD::AttachmentLoadOp *load_ops = p_draw_list_command->load_ops();810const RDD::AttachmentStoreOp *store_ops = p_draw_list_command->store_ops();811uint64_t key = 0;812for (uint32_t i = 0; i < p_draw_list_command->trackers_count; i++) {813key |= uint64_t(load_ops[i]) << (i * 3);814key |= uint64_t(store_ops[i]) << (i * 3 + 2);815}816817// Check the storage map if the render pass and the framebuffer needs to be created.818FramebufferCache *framebuffer_cache = p_draw_list_command->framebuffer_cache;819HashMap<uint64_t, FramebufferStorage>::Iterator it = framebuffer_cache->storage_map.find(key);820if (it == framebuffer_cache->storage_map.end()) {821FramebufferStorage storage;822VectorView<RDD::AttachmentLoadOp> load_ops_view(load_ops, p_draw_list_command->trackers_count);823VectorView<RDD::AttachmentStoreOp> store_ops_view(store_ops, p_draw_list_command->trackers_count);824storage.render_pass = render_pass_creation_function(driver, load_ops_view, store_ops_view, framebuffer_cache->render_pass_creation_user_data);825ERR_FAIL_COND(!storage.render_pass);826827storage.framebuffer = driver->framebuffer_create(storage.render_pass, framebuffer_cache->textures, framebuffer_cache->width, framebuffer_cache->height);828ERR_FAIL_COND(!storage.framebuffer);829830it = framebuffer_cache->storage_map.insert(key, storage);831}832833r_render_pass = it->value.render_pass;834r_framebuffer = it->value.framebuffer;835}836837void RenderingDeviceGraph::_run_draw_list_command(RDD::CommandBufferID p_command_buffer, const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {838uint32_t instruction_data_cursor = 0;839while (instruction_data_cursor < p_instruction_data_size) {840DEV_ASSERT((instruction_data_cursor + sizeof(DrawListInstruction)) <= p_instruction_data_size);841842const DrawListInstruction *instruction = reinterpret_cast<const DrawListInstruction *>(&p_instruction_data[instruction_data_cursor]);843switch (instruction->type) {844case DrawListInstruction::TYPE_BIND_INDEX_BUFFER: {845const DrawListBindIndexBufferInstruction *bind_index_buffer_instruction = reinterpret_cast<const DrawListBindIndexBufferInstruction *>(instruction);846driver->command_render_bind_index_buffer(p_command_buffer, bind_index_buffer_instruction->buffer, bind_index_buffer_instruction->format, bind_index_buffer_instruction->offset);847instruction_data_cursor += sizeof(DrawListBindIndexBufferInstruction);848} break;849case DrawListInstruction::TYPE_BIND_PIPELINE: {850const DrawListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const DrawListBindPipelineInstruction *>(instruction);851driver->command_bind_render_pipeline(p_command_buffer, bind_pipeline_instruction->pipeline);852instruction_data_cursor += sizeof(DrawListBindPipelineInstruction);853} break;854case DrawListInstruction::TYPE_BIND_UNIFORM_SETS: {855const DrawListBindUniformSetsInstruction *bind_uniform_sets_instruction = reinterpret_cast<const DrawListBindUniformSetsInstruction *>(instruction);856driver->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);857instruction_data_cursor += sizeof(DrawListBindUniformSetsInstruction) + sizeof(RDD::UniformSetID) * bind_uniform_sets_instruction->set_count;858} break;859case DrawListInstruction::TYPE_BIND_VERTEX_BUFFERS: {860const DrawListBindVertexBuffersInstruction *bind_vertex_buffers_instruction = reinterpret_cast<const DrawListBindVertexBuffersInstruction *>(instruction);861driver->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());862instruction_data_cursor += sizeof(DrawListBindVertexBuffersInstruction);863instruction_data_cursor += sizeof(RDD::BufferID) * bind_vertex_buffers_instruction->vertex_buffers_count;864instruction_data_cursor += sizeof(uint64_t) * bind_vertex_buffers_instruction->vertex_buffers_count;865} break;866case DrawListInstruction::TYPE_CLEAR_ATTACHMENTS: {867const DrawListClearAttachmentsInstruction *clear_attachments_instruction = reinterpret_cast<const DrawListClearAttachmentsInstruction *>(instruction);868const VectorView attachments_clear_view(clear_attachments_instruction->attachments_clear(), clear_attachments_instruction->attachments_clear_count);869const VectorView attachments_clear_rect_view(clear_attachments_instruction->attachments_clear_rect(), clear_attachments_instruction->attachments_clear_rect_count);870driver->command_render_clear_attachments(p_command_buffer, attachments_clear_view, attachments_clear_rect_view);871instruction_data_cursor += sizeof(DrawListClearAttachmentsInstruction);872instruction_data_cursor += sizeof(RDD::AttachmentClear) * clear_attachments_instruction->attachments_clear_count;873instruction_data_cursor += sizeof(Rect2i) * clear_attachments_instruction->attachments_clear_rect_count;874} break;875case DrawListInstruction::TYPE_DRAW: {876const DrawListDrawInstruction *draw_instruction = reinterpret_cast<const DrawListDrawInstruction *>(instruction);877driver->command_render_draw(p_command_buffer, draw_instruction->vertex_count, draw_instruction->instance_count, 0, 0);878instruction_data_cursor += sizeof(DrawListDrawInstruction);879} break;880case DrawListInstruction::TYPE_DRAW_INDEXED: {881const DrawListDrawIndexedInstruction *draw_indexed_instruction = reinterpret_cast<const DrawListDrawIndexedInstruction *>(instruction);882driver->command_render_draw_indexed(p_command_buffer, draw_indexed_instruction->index_count, draw_indexed_instruction->instance_count, draw_indexed_instruction->first_index, 0, 0);883instruction_data_cursor += sizeof(DrawListDrawIndexedInstruction);884} break;885case DrawListInstruction::TYPE_DRAW_INDIRECT: {886const DrawListDrawIndirectInstruction *draw_indirect_instruction = reinterpret_cast<const DrawListDrawIndirectInstruction *>(instruction);887driver->command_render_draw_indirect(p_command_buffer, draw_indirect_instruction->buffer, draw_indirect_instruction->offset, draw_indirect_instruction->draw_count, draw_indirect_instruction->stride);888instruction_data_cursor += sizeof(DrawListDrawIndirectInstruction);889} break;890case DrawListInstruction::TYPE_DRAW_INDEXED_INDIRECT: {891const DrawListDrawIndexedIndirectInstruction *draw_indexed_indirect_instruction = reinterpret_cast<const DrawListDrawIndexedIndirectInstruction *>(instruction);892driver->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);893instruction_data_cursor += sizeof(DrawListDrawIndexedIndirectInstruction);894} break;895case DrawListInstruction::TYPE_EXECUTE_COMMANDS: {896const DrawListExecuteCommandsInstruction *execute_commands_instruction = reinterpret_cast<const DrawListExecuteCommandsInstruction *>(instruction);897driver->command_buffer_execute_secondary(p_command_buffer, execute_commands_instruction->command_buffer);898instruction_data_cursor += sizeof(DrawListExecuteCommandsInstruction);899} break;900case DrawListInstruction::TYPE_NEXT_SUBPASS: {901const DrawListNextSubpassInstruction *next_subpass_instruction = reinterpret_cast<const DrawListNextSubpassInstruction *>(instruction);902driver->command_next_render_subpass(p_command_buffer, next_subpass_instruction->command_buffer_type);903instruction_data_cursor += sizeof(DrawListNextSubpassInstruction);904} break;905case DrawListInstruction::TYPE_SET_BLEND_CONSTANTS: {906const DrawListSetBlendConstantsInstruction *set_blend_constants_instruction = reinterpret_cast<const DrawListSetBlendConstantsInstruction *>(instruction);907driver->command_render_set_blend_constants(p_command_buffer, set_blend_constants_instruction->color);908instruction_data_cursor += sizeof(DrawListSetBlendConstantsInstruction);909} break;910case DrawListInstruction::TYPE_SET_LINE_WIDTH: {911const DrawListSetLineWidthInstruction *set_line_width_instruction = reinterpret_cast<const DrawListSetLineWidthInstruction *>(instruction);912driver->command_render_set_line_width(p_command_buffer, set_line_width_instruction->width);913instruction_data_cursor += sizeof(DrawListSetLineWidthInstruction);914} break;915case DrawListInstruction::TYPE_SET_PUSH_CONSTANT: {916const DrawListSetPushConstantInstruction *set_push_constant_instruction = reinterpret_cast<const DrawListSetPushConstantInstruction *>(instruction);917const VectorView push_constant_data_view(reinterpret_cast<const uint32_t *>(set_push_constant_instruction->data()), set_push_constant_instruction->size / sizeof(uint32_t));918driver->command_bind_push_constants(p_command_buffer, set_push_constant_instruction->shader, 0, push_constant_data_view);919instruction_data_cursor += sizeof(DrawListSetPushConstantInstruction);920instruction_data_cursor += set_push_constant_instruction->size;921} break;922case DrawListInstruction::TYPE_SET_SCISSOR: {923const DrawListSetScissorInstruction *set_scissor_instruction = reinterpret_cast<const DrawListSetScissorInstruction *>(instruction);924driver->command_render_set_scissor(p_command_buffer, set_scissor_instruction->rect);925instruction_data_cursor += sizeof(DrawListSetScissorInstruction);926} break;927case DrawListInstruction::TYPE_SET_VIEWPORT: {928const DrawListSetViewportInstruction *set_viewport_instruction = reinterpret_cast<const DrawListSetViewportInstruction *>(instruction);929driver->command_render_set_viewport(p_command_buffer, set_viewport_instruction->rect);930instruction_data_cursor += sizeof(DrawListSetViewportInstruction);931} break;932case DrawListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE: {933const DrawListUniformSetPrepareForUseInstruction *uniform_set_prepare_for_use_instruction = reinterpret_cast<const DrawListUniformSetPrepareForUseInstruction *>(instruction);934driver->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);935instruction_data_cursor += sizeof(DrawListUniformSetPrepareForUseInstruction);936} break;937default:938DEV_ASSERT(false && "Unknown draw list instruction type.");939return;940}941942instruction_data_cursor = GRAPH_ALIGN(instruction_data_cursor);943}944}945946void 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) {947DEV_ASSERT(p_attachment_operations.size() == p_attachment_clear_values.size());948949draw_instruction_list.clear();950draw_instruction_list.index++;951draw_instruction_list.framebuffer_cache = p_framebuffer_cache;952draw_instruction_list.render_pass = p_render_pass;953draw_instruction_list.framebuffer = p_framebuffer;954draw_instruction_list.region = p_region;955draw_instruction_list.stages = p_stages;956draw_instruction_list.attachment_operations.resize(p_attachment_operations.size());957draw_instruction_list.attachment_clear_values.resize(p_attachment_clear_values.size());958959for (uint32_t i = 0; i < p_attachment_operations.size(); i++) {960draw_instruction_list.attachment_operations[i] = p_attachment_operations[i];961draw_instruction_list.attachment_clear_values[i] = p_attachment_clear_values[i];962}963964draw_instruction_list.split_cmd_buffer = p_split_cmd_buffer;965966#if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)967draw_instruction_list.breadcrumb = p_breadcrumb;968#endif969}970971void RenderingDeviceGraph::_run_secondary_command_buffer_task(const SecondaryCommandBuffer *p_secondary) {972driver->command_buffer_begin_secondary(p_secondary->command_buffer, p_secondary->render_pass, 0, p_secondary->framebuffer);973_run_draw_list_command(p_secondary->command_buffer, p_secondary->instruction_data.ptr(), p_secondary->instruction_data.size());974driver->command_buffer_end(p_secondary->command_buffer);975}976977void RenderingDeviceGraph::_wait_for_secondary_command_buffer_tasks() {978for (uint32_t i = 0; i < frames[frame].secondary_command_buffers_used; i++) {979WorkerThreadPool::TaskID &task = frames[frame].secondary_command_buffers[i].task;980if (task != WorkerThreadPool::INVALID_TASK_ID) {981WorkerThreadPool::get_singleton()->wait_for_task_completion(task);982task = WorkerThreadPool::INVALID_TASK_ID;983}984}985}986987void 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) {988for (uint32_t i = 0; i < p_sorted_commands_count; i++) {989const uint32_t command_index = p_sorted_commands[i].index;990const uint32_t command_data_offset = command_data_offsets[command_index];991const RecordedCommand *command = reinterpret_cast<const RecordedCommand *>(&command_data[command_data_offset]);992_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);993994switch (command->type) {995case RecordedCommand::TYPE_BUFFER_CLEAR: {996const RecordedBufferClearCommand *buffer_clear_command = reinterpret_cast<const RecordedBufferClearCommand *>(command);997driver->command_clear_buffer(r_command_buffer, buffer_clear_command->buffer, buffer_clear_command->offset, buffer_clear_command->size);998} break;999case RecordedCommand::TYPE_BUFFER_COPY: {1000const RecordedBufferCopyCommand *buffer_copy_command = reinterpret_cast<const RecordedBufferCopyCommand *>(command);1001driver->command_copy_buffer(r_command_buffer, buffer_copy_command->source, buffer_copy_command->destination, buffer_copy_command->region);1002} break;1003case RecordedCommand::TYPE_BUFFER_GET_DATA: {1004const RecordedBufferGetDataCommand *buffer_get_data_command = reinterpret_cast<const RecordedBufferGetDataCommand *>(command);1005driver->command_copy_buffer(r_command_buffer, buffer_get_data_command->source, buffer_get_data_command->destination, buffer_get_data_command->region);1006} break;1007case RecordedCommand::TYPE_BUFFER_UPDATE: {1008const RecordedBufferUpdateCommand *buffer_update_command = reinterpret_cast<const RecordedBufferUpdateCommand *>(command);1009const RecordedBufferCopy *command_buffer_copies = buffer_update_command->buffer_copies();1010for (uint32_t j = 0; j < buffer_update_command->buffer_copies_count; j++) {1011driver->command_copy_buffer(r_command_buffer, command_buffer_copies[j].source, buffer_update_command->destination, command_buffer_copies[j].region);1012}1013} break;1014case RecordedCommand::TYPE_DRIVER_CALLBACK: {1015const RecordedDriverCallbackCommand *driver_callback_command = reinterpret_cast<const RecordedDriverCallbackCommand *>(command);1016driver_callback_command->callback(driver, r_command_buffer, driver_callback_command->userdata);1017} break;1018case RecordedCommand::TYPE_COMPUTE_LIST: {1019if (device.workarounds.avoid_compute_after_draw && workarounds_state.draw_list_found) {1020// Avoid compute after draw workaround. Refer to the comment that enables this in the Vulkan driver for more information.1021workarounds_state.draw_list_found = false;10221023// Create or reuse a command buffer and finish recording the current one.1024driver->command_buffer_end(r_command_buffer);10251026while (r_command_buffer_pool.buffers_used >= r_command_buffer_pool.buffers.size()) {1027RDD::CommandBufferID command_buffer = driver->command_buffer_create(r_command_buffer_pool.pool);1028RDD::SemaphoreID command_semaphore = driver->semaphore_create();1029r_command_buffer_pool.buffers.push_back(command_buffer);1030r_command_buffer_pool.semaphores.push_back(command_semaphore);1031}10321033// Start recording on the next usable command buffer from the pool.1034uint32_t command_buffer_index = r_command_buffer_pool.buffers_used++;1035r_command_buffer = r_command_buffer_pool.buffers[command_buffer_index];1036driver->command_buffer_begin(r_command_buffer);1037}10381039const RecordedComputeListCommand *compute_list_command = reinterpret_cast<const RecordedComputeListCommand *>(command);1040_run_compute_list_command(r_command_buffer, compute_list_command->instruction_data(), compute_list_command->instruction_data_size);1041} break;1042case RecordedCommand::TYPE_DRAW_LIST: {1043if (device.workarounds.avoid_compute_after_draw) {1044// Indicate that a draw list was encountered for the workaround.1045workarounds_state.draw_list_found = true;1046}10471048const RecordedDrawListCommand *draw_list_command = reinterpret_cast<const RecordedDrawListCommand *>(command);10491050if (draw_list_command->split_cmd_buffer) {1051// Create or reuse a command buffer and finish recording the current one.1052driver->command_buffer_end(r_command_buffer);10531054while (r_command_buffer_pool.buffers_used >= r_command_buffer_pool.buffers.size()) {1055RDD::CommandBufferID command_buffer = driver->command_buffer_create(r_command_buffer_pool.pool);1056RDD::SemaphoreID command_semaphore = driver->semaphore_create();1057r_command_buffer_pool.buffers.push_back(command_buffer);1058r_command_buffer_pool.semaphores.push_back(command_semaphore);1059}10601061// Start recording on the next usable command buffer from the pool.1062uint32_t command_buffer_index = r_command_buffer_pool.buffers_used++;1063r_command_buffer = r_command_buffer_pool.buffers[command_buffer_index];1064driver->command_buffer_begin(r_command_buffer);1065}10661067const VectorView clear_values(draw_list_command->clear_values(), draw_list_command->clear_values_count);1068#if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)1069driver->command_insert_breadcrumb(r_command_buffer, draw_list_command->breadcrumb);1070#endif1071RDD::RenderPassID render_pass;1072RDD::FramebufferID framebuffer;1073if (draw_list_command->framebuffer_cache != nullptr) {1074_get_draw_list_render_pass_and_framebuffer(draw_list_command, render_pass, framebuffer);1075} else {1076render_pass = draw_list_command->render_pass;1077framebuffer = draw_list_command->framebuffer;1078}10791080if (framebuffer && render_pass) {1081driver->command_begin_render_pass(r_command_buffer, render_pass, framebuffer, draw_list_command->command_buffer_type, draw_list_command->region, clear_values);1082_run_draw_list_command(r_command_buffer, draw_list_command->instruction_data(), draw_list_command->instruction_data_size);1083driver->command_end_render_pass(r_command_buffer);1084}1085} break;1086case RecordedCommand::TYPE_TEXTURE_CLEAR: {1087const RecordedTextureClearCommand *texture_clear_command = reinterpret_cast<const RecordedTextureClearCommand *>(command);1088driver->command_clear_color_texture(r_command_buffer, texture_clear_command->texture, RDD::TEXTURE_LAYOUT_COPY_DST_OPTIMAL, texture_clear_command->color, texture_clear_command->range);1089} break;1090case RecordedCommand::TYPE_TEXTURE_COPY: {1091const RecordedTextureCopyCommand *texture_copy_command = reinterpret_cast<const RecordedTextureCopyCommand *>(command);1092const VectorView<RDD::TextureCopyRegion> command_texture_copy_regions_view(texture_copy_command->texture_copy_regions(), texture_copy_command->texture_copy_regions_count);1093driver->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);1094} break;1095case RecordedCommand::TYPE_TEXTURE_GET_DATA: {1096const RecordedTextureGetDataCommand *texture_get_data_command = reinterpret_cast<const RecordedTextureGetDataCommand *>(command);1097const 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);1098driver->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);1099} break;1100case RecordedCommand::TYPE_TEXTURE_RESOLVE: {1101const RecordedTextureResolveCommand *texture_resolve_command = reinterpret_cast<const RecordedTextureResolveCommand *>(command);1102driver->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);1103} break;1104case RecordedCommand::TYPE_TEXTURE_UPDATE: {1105const RecordedTextureUpdateCommand *texture_update_command = reinterpret_cast<const RecordedTextureUpdateCommand *>(command);1106const RecordedBufferToTextureCopy *command_buffer_to_texture_copies = texture_update_command->buffer_to_texture_copies();1107for (uint32_t j = 0; j < texture_update_command->buffer_to_texture_copies_count; j++) {1108driver->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);1109}1110} break;1111case RecordedCommand::TYPE_CAPTURE_TIMESTAMP: {1112const RecordedCaptureTimestampCommand *texture_capture_timestamp_command = reinterpret_cast<const RecordedCaptureTimestampCommand *>(command);1113driver->command_timestamp_write(r_command_buffer, texture_capture_timestamp_command->pool, texture_capture_timestamp_command->index);1114} break;1115default: {1116DEV_ASSERT(false && "Unknown recorded command type.");1117return;1118}1119}1120}1121}11221123void 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) {1124if (command_label_count == 0) {1125// Ignore any label operations if no labels were pushed.1126return;1127}11281129if (p_ignore_previous_value || p_new_label_index != r_current_label_index || p_new_level != r_current_label_level) {1130if (!p_ignore_previous_value && (p_use_label_for_empty || r_current_label_index >= 0 || r_current_label_level >= 0)) {1131// End the current label.1132driver->command_end_label(p_command_buffer);1133}11341135String label_name;1136Color label_color;1137if (p_new_label_index >= 0) {1138const char *label_chars = &command_label_chars[command_label_offsets[p_new_label_index]];1139label_name.append_utf8(label_chars);1140label_color = command_label_colors[p_new_label_index];1141} else if (p_use_label_for_empty) {1142label_name = "Command graph";1143label_color = Color(1, 1, 1, 1);1144} else {1145return;1146}11471148// Add the level to the name.1149label_name += " (L" + itos(p_new_level) + ")";11501151if (p_sorted_commands != nullptr && p_sorted_commands_count > 0) {1152// Analyze the commands in the level that have the same label to detect what type of operations are performed.1153bool copy_commands = false;1154bool compute_commands = false;1155bool draw_commands = false;1156bool custom_commands = false;1157for (uint32_t i = 0; i < p_sorted_commands_count; i++) {1158const uint32_t command_index = p_sorted_commands[i].index;1159const uint32_t command_data_offset = command_data_offsets[command_index];1160const RecordedCommand *command = reinterpret_cast<RecordedCommand *>(&command_data[command_data_offset]);1161if (command->label_index != p_new_label_index) {1162break;1163}11641165switch (command->type) {1166case RecordedCommand::TYPE_BUFFER_CLEAR:1167case RecordedCommand::TYPE_BUFFER_COPY:1168case RecordedCommand::TYPE_BUFFER_GET_DATA:1169case RecordedCommand::TYPE_BUFFER_UPDATE:1170case RecordedCommand::TYPE_TEXTURE_CLEAR:1171case RecordedCommand::TYPE_TEXTURE_COPY:1172case RecordedCommand::TYPE_TEXTURE_GET_DATA:1173case RecordedCommand::TYPE_TEXTURE_RESOLVE:1174case RecordedCommand::TYPE_TEXTURE_UPDATE: {1175copy_commands = true;1176} break;1177case RecordedCommand::TYPE_COMPUTE_LIST: {1178compute_commands = true;1179} break;1180case RecordedCommand::TYPE_DRAW_LIST: {1181draw_commands = true;1182} break;1183case RecordedCommand::TYPE_DRIVER_CALLBACK: {1184custom_commands = true;1185} break;1186default: {1187// Ignore command.1188} break;1189}11901191if (copy_commands && compute_commands && draw_commands && custom_commands) {1192// There's no more command types to find.1193break;1194}1195}11961197if (copy_commands || compute_commands || draw_commands || custom_commands) {1198// Add the operations to the name.1199bool plus_after_copy = copy_commands && (compute_commands || draw_commands || custom_commands);1200bool plus_after_compute = compute_commands && (draw_commands || custom_commands);1201bool plus_after_draw = draw_commands && custom_commands;1202label_name += " (";1203label_name += copy_commands ? "Copy" : "";1204label_name += plus_after_copy ? "+" : "";1205label_name += compute_commands ? "Compute" : "";1206label_name += plus_after_compute ? "+" : "";1207label_name += draw_commands ? "Draw" : "";1208label_name += plus_after_draw ? "+" : "";1209label_name += custom_commands ? "Custom" : "";1210label_name += ")";1211}1212}12131214// Start the new label.1215CharString label_name_utf8 = label_name.utf8();1216driver->command_begin_label(p_command_buffer, label_name_utf8.get_data(), label_color);12171218r_current_label_index = p_new_label_index;1219r_current_label_level = p_new_level;1220}1221}12221223void RenderingDeviceGraph::_boost_priority_for_render_commands(RecordedCommandSort *p_sorted_commands, uint32_t p_sorted_commands_count, uint32_t &r_boosted_priority) {1224if (p_sorted_commands_count == 0) {1225return;1226}12271228const uint32_t boosted_priority_value = 0;1229if (r_boosted_priority > 0) {1230bool perform_sort = false;1231for (uint32_t j = 0; j < p_sorted_commands_count; j++) {1232if (p_sorted_commands[j].priority == r_boosted_priority) {1233p_sorted_commands[j].priority = boosted_priority_value;1234perform_sort = true;1235}1236}12371238if (perform_sort) {1239SortArray<RecordedCommandSort> command_sorter;1240command_sorter.sort(p_sorted_commands, p_sorted_commands_count);1241}1242}12431244if (p_sorted_commands[p_sorted_commands_count - 1].priority != boosted_priority_value) {1245r_boosted_priority = p_sorted_commands[p_sorted_commands_count - 1].priority;1246}1247}12481249void 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) {1250if (!driver_honors_barriers) {1251return;1252}12531254barrier_group.clear();1255barrier_group.src_stages = RDD::PIPELINE_STAGE_TOP_OF_PIPE_BIT;1256barrier_group.dst_stages = RDD::PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;12571258for (uint32_t i = 0; i < p_sorted_commands_count; i++) {1259const uint32_t command_index = p_sorted_commands[i].index;1260const uint32_t command_data_offset = command_data_offsets[command_index];1261const RecordedCommand *command = reinterpret_cast<RecordedCommand *>(&command_data[command_data_offset]);12621263#if PRINT_COMMAND_RECORDING1264print_line(vformat("Grouping barriers for #%d", command_index));1265#endif12661267// Merge command's stage bits with the barrier group.1268barrier_group.src_stages = barrier_group.src_stages | command->previous_stages;1269barrier_group.dst_stages = barrier_group.dst_stages | command->next_stages;12701271// Merge command's memory barrier bits with the barrier group.1272barrier_group.memory_barrier.src_access = barrier_group.memory_barrier.src_access | command->memory_barrier.src_access;1273barrier_group.memory_barrier.dst_access = barrier_group.memory_barrier.dst_access | command->memory_barrier.dst_access;12741275// Gather texture barriers.1276for (int32_t j = 0; j < command->normalization_barrier_count; j++) {1277const RDD::TextureBarrier &recorded_barrier = command_normalization_barriers[command->normalization_barrier_index + j];1278barrier_group.normalization_barriers.push_back(recorded_barrier);1279#if PRINT_COMMAND_RECORDING1280print_line(vformat("Normalization Barrier #%d", barrier_group.normalization_barriers.size() - 1));1281#endif1282}12831284for (int32_t j = 0; j < command->transition_barrier_count; j++) {1285const RDD::TextureBarrier &recorded_barrier = command_transition_barriers[command->transition_barrier_index + j];1286barrier_group.transition_barriers.push_back(recorded_barrier);1287#if PRINT_COMMAND_RECORDING1288print_line(vformat("Transition Barrier #%d", barrier_group.transition_barriers.size() - 1));1289#endif1290}12911292#if USE_BUFFER_BARRIERS1293// Gather buffer barriers.1294for (int32_t j = 0; j < command->buffer_barrier_count; j++) {1295const RDD::BufferBarrier &recorded_barrier = command_buffer_barriers[command->buffer_barrier_index + j];1296barrier_group.buffer_barriers.push_back(recorded_barrier);1297}1298#endif1299}13001301if (p_full_memory_barrier) {1302barrier_group.src_stages = RDD::PIPELINE_STAGE_ALL_COMMANDS_BIT;1303barrier_group.dst_stages = RDD::PIPELINE_STAGE_ALL_COMMANDS_BIT;1304barrier_group.memory_barrier.src_access = RDD::BARRIER_ACCESS_MEMORY_READ_BIT | RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT;1305barrier_group.memory_barrier.dst_access = RDD::BARRIER_ACCESS_MEMORY_READ_BIT | RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT;1306}13071308const bool is_memory_barrier_empty = barrier_group.memory_barrier.src_access.is_empty() && barrier_group.memory_barrier.dst_access.is_empty();1309const bool are_texture_barriers_empty = barrier_group.normalization_barriers.is_empty() && barrier_group.transition_barriers.is_empty();1310#if USE_BUFFER_BARRIERS1311const bool are_buffer_barriers_empty = barrier_group.buffer_barriers.is_empty();1312#else1313const bool are_buffer_barriers_empty = true;1314#endif1315if (is_memory_barrier_empty && are_texture_barriers_empty && are_buffer_barriers_empty) {1316// Commands don't require synchronization.1317return;1318}13191320const VectorView<RDD::MemoryBarrier> memory_barriers = !is_memory_barrier_empty ? barrier_group.memory_barrier : VectorView<RDD::MemoryBarrier>();1321const VectorView<RDD::TextureBarrier> texture_barriers = barrier_group.normalization_barriers.is_empty() ? barrier_group.transition_barriers : barrier_group.normalization_barriers;1322#if USE_BUFFER_BARRIERS1323const VectorView<RDD::BufferBarrier> buffer_barriers = !are_buffer_barriers_empty ? barrier_group.buffer_barriers : VectorView<RDD::BufferBarrier>();1324#else1325const VectorView<RDD::BufferBarrier> buffer_barriers = VectorView<RDD::BufferBarrier>();1326#endif13271328driver->command_pipeline_barrier(p_command_buffer, barrier_group.src_stages, barrier_group.dst_stages, memory_barriers, buffer_barriers, texture_barriers);13291330bool separate_texture_barriers = !barrier_group.normalization_barriers.is_empty() && !barrier_group.transition_barriers.is_empty();1331if (separate_texture_barriers) {1332driver->command_pipeline_barrier(p_command_buffer, barrier_group.src_stages, barrier_group.dst_stages, VectorView<RDD::MemoryBarrier>(), VectorView<RDD::BufferBarrier>(), barrier_group.transition_barriers);1333}1334}13351336void RenderingDeviceGraph::_print_render_commands(const RecordedCommandSort *p_sorted_commands, uint32_t p_sorted_commands_count) {1337for (uint32_t i = 0; i < p_sorted_commands_count; i++) {1338const uint32_t command_index = p_sorted_commands[i].index;1339const uint32_t command_level = p_sorted_commands[i].level;1340const uint32_t command_data_offset = command_data_offsets[command_index];1341const RecordedCommand *command = reinterpret_cast<RecordedCommand *>(&command_data[command_data_offset]);1342switch (command->type) {1343case RecordedCommand::TYPE_BUFFER_CLEAR: {1344const RecordedBufferClearCommand *buffer_clear_command = reinterpret_cast<const RecordedBufferClearCommand *>(command);1345print_line(command_index, "LEVEL", command_level, "BUFFER CLEAR DESTINATION", itos(buffer_clear_command->buffer.id));1346} break;1347case RecordedCommand::TYPE_BUFFER_COPY: {1348const RecordedBufferCopyCommand *buffer_copy_command = reinterpret_cast<const RecordedBufferCopyCommand *>(command);1349print_line(command_index, "LEVEL", command_level, "BUFFER COPY SOURCE", itos(buffer_copy_command->source.id), "DESTINATION", itos(buffer_copy_command->destination.id));1350} break;1351case RecordedCommand::TYPE_BUFFER_GET_DATA: {1352const RecordedBufferGetDataCommand *buffer_get_data_command = reinterpret_cast<const RecordedBufferGetDataCommand *>(command);1353print_line(command_index, "LEVEL", command_level, "BUFFER GET DATA DESTINATION", itos(buffer_get_data_command->destination.id));1354} break;1355case RecordedCommand::TYPE_BUFFER_UPDATE: {1356const RecordedBufferUpdateCommand *buffer_update_command = reinterpret_cast<const RecordedBufferUpdateCommand *>(command);1357print_line(command_index, "LEVEL", command_level, "BUFFER UPDATE DESTINATION", itos(buffer_update_command->destination.id), "COPIES", buffer_update_command->buffer_copies_count);1358} break;1359case RecordedCommand::TYPE_DRIVER_CALLBACK: {1360print_line(command_index, "LEVEL", command_level, "DRIVER CALLBACK");1361} break;1362case RecordedCommand::TYPE_COMPUTE_LIST: {1363const RecordedComputeListCommand *compute_list_command = reinterpret_cast<const RecordedComputeListCommand *>(command);1364print_line(command_index, "LEVEL", command_level, "COMPUTE LIST SIZE", compute_list_command->instruction_data_size);1365} break;1366case RecordedCommand::TYPE_DRAW_LIST: {1367const RecordedDrawListCommand *draw_list_command = reinterpret_cast<const RecordedDrawListCommand *>(command);1368print_line(command_index, "LEVEL", command_level, "DRAW LIST SIZE", draw_list_command->instruction_data_size);1369} break;1370case RecordedCommand::TYPE_TEXTURE_CLEAR: {1371const RecordedTextureClearCommand *texture_clear_command = reinterpret_cast<const RecordedTextureClearCommand *>(command);1372print_line(command_index, "LEVEL", command_level, "TEXTURE CLEAR", itos(texture_clear_command->texture.id), "COLOR", texture_clear_command->color);1373} break;1374case RecordedCommand::TYPE_TEXTURE_COPY: {1375const RecordedTextureCopyCommand *texture_copy_command = reinterpret_cast<const RecordedTextureCopyCommand *>(command);1376print_line(command_index, "LEVEL", command_level, "TEXTURE COPY FROM", itos(texture_copy_command->from_texture.id), "TO", itos(texture_copy_command->to_texture.id));1377} break;1378case RecordedCommand::TYPE_TEXTURE_GET_DATA: {1379print_line(command_index, "LEVEL", command_level, "TEXTURE GET DATA");1380} break;1381case RecordedCommand::TYPE_TEXTURE_RESOLVE: {1382const RecordedTextureResolveCommand *texture_resolve_command = reinterpret_cast<const RecordedTextureResolveCommand *>(command);1383print_line(command_index, "LEVEL", command_level, "TEXTURE RESOLVE FROM", itos(texture_resolve_command->from_texture.id), "TO", itos(texture_resolve_command->to_texture.id));1384} break;1385case RecordedCommand::TYPE_TEXTURE_UPDATE: {1386const RecordedTextureUpdateCommand *texture_update_command = reinterpret_cast<const RecordedTextureUpdateCommand *>(command);1387print_line(command_index, "LEVEL", command_level, "TEXTURE UPDATE TO", itos(texture_update_command->to_texture.id));1388} break;1389case RecordedCommand::TYPE_CAPTURE_TIMESTAMP: {1390const RecordedCaptureTimestampCommand *texture_capture_timestamp_command = reinterpret_cast<const RecordedCaptureTimestampCommand *>(command);1391print_line(command_index, "LEVEL", command_level, "CAPTURE TIMESTAMP POOL", itos(texture_capture_timestamp_command->pool.id), "INDEX", texture_capture_timestamp_command->index);1392} break;1393default:1394DEV_ASSERT(false && "Unknown recorded command type.");1395return;1396}1397}1398}13991400void RenderingDeviceGraph::_print_draw_list(const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {1401uint32_t instruction_data_cursor = 0;1402while (instruction_data_cursor < p_instruction_data_size) {1403DEV_ASSERT((instruction_data_cursor + sizeof(DrawListInstruction)) <= p_instruction_data_size);14041405const DrawListInstruction *instruction = reinterpret_cast<const DrawListInstruction *>(&p_instruction_data[instruction_data_cursor]);1406switch (instruction->type) {1407case DrawListInstruction::TYPE_BIND_INDEX_BUFFER: {1408const DrawListBindIndexBufferInstruction *bind_index_buffer_instruction = reinterpret_cast<const DrawListBindIndexBufferInstruction *>(instruction);1409print_line("\tBIND INDEX BUFFER ID", itos(bind_index_buffer_instruction->buffer.id), "FORMAT", bind_index_buffer_instruction->format, "OFFSET", bind_index_buffer_instruction->offset);1410instruction_data_cursor += sizeof(DrawListBindIndexBufferInstruction);1411} break;1412case DrawListInstruction::TYPE_BIND_PIPELINE: {1413const DrawListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const DrawListBindPipelineInstruction *>(instruction);1414print_line("\tBIND PIPELINE ID", itos(bind_pipeline_instruction->pipeline.id));1415instruction_data_cursor += sizeof(DrawListBindPipelineInstruction);1416} break;1417case DrawListInstruction::TYPE_BIND_UNIFORM_SETS: {1418const DrawListBindUniformSetsInstruction *bind_uniform_sets_instruction = reinterpret_cast<const DrawListBindUniformSetsInstruction *>(instruction);1419print_line("\tBIND UNIFORM SETS COUNT", bind_uniform_sets_instruction->set_count);1420for (uint32_t i = 0; i < bind_uniform_sets_instruction->set_count; i++) {1421print_line("\tBIND UNIFORM SET ID", itos(bind_uniform_sets_instruction->uniform_set_ids()[i].id), "START INDEX", bind_uniform_sets_instruction->first_set_index);1422}1423instruction_data_cursor += sizeof(DrawListBindUniformSetsInstruction) + sizeof(RDD::UniformSetID) * bind_uniform_sets_instruction->set_count;1424} break;1425case DrawListInstruction::TYPE_BIND_VERTEX_BUFFERS: {1426const DrawListBindVertexBuffersInstruction *bind_vertex_buffers_instruction = reinterpret_cast<const DrawListBindVertexBuffersInstruction *>(instruction);1427print_line("\tBIND VERTEX BUFFERS COUNT", bind_vertex_buffers_instruction->vertex_buffers_count);1428instruction_data_cursor += sizeof(DrawListBindVertexBuffersInstruction);1429instruction_data_cursor += sizeof(RDD::BufferID) * bind_vertex_buffers_instruction->vertex_buffers_count;1430instruction_data_cursor += sizeof(uint64_t) * bind_vertex_buffers_instruction->vertex_buffers_count;1431} break;1432case DrawListInstruction::TYPE_CLEAR_ATTACHMENTS: {1433const DrawListClearAttachmentsInstruction *clear_attachments_instruction = reinterpret_cast<const DrawListClearAttachmentsInstruction *>(instruction);1434print_line("\tATTACHMENTS CLEAR COUNT", clear_attachments_instruction->attachments_clear_count, "RECT COUNT", clear_attachments_instruction->attachments_clear_rect_count);1435instruction_data_cursor += sizeof(DrawListClearAttachmentsInstruction);1436instruction_data_cursor += sizeof(RDD::AttachmentClear) * clear_attachments_instruction->attachments_clear_count;1437instruction_data_cursor += sizeof(Rect2i) * clear_attachments_instruction->attachments_clear_rect_count;1438} break;1439case DrawListInstruction::TYPE_DRAW: {1440const DrawListDrawInstruction *draw_instruction = reinterpret_cast<const DrawListDrawInstruction *>(instruction);1441print_line("\tDRAW VERTICES", draw_instruction->vertex_count, "INSTANCES", draw_instruction->instance_count);1442instruction_data_cursor += sizeof(DrawListDrawInstruction);1443} break;1444case DrawListInstruction::TYPE_DRAW_INDEXED: {1445const DrawListDrawIndexedInstruction *draw_indexed_instruction = reinterpret_cast<const DrawListDrawIndexedInstruction *>(instruction);1446print_line("\tDRAW INDICES", draw_indexed_instruction->index_count, "INSTANCES", draw_indexed_instruction->instance_count, "FIRST INDEX", draw_indexed_instruction->first_index);1447instruction_data_cursor += sizeof(DrawListDrawIndexedInstruction);1448} break;1449case DrawListInstruction::TYPE_DRAW_INDIRECT: {1450const DrawListDrawIndirectInstruction *draw_indirect_instruction = reinterpret_cast<const DrawListDrawIndirectInstruction *>(instruction);1451print_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);1452instruction_data_cursor += sizeof(DrawListDrawIndirectInstruction);1453} break;1454case DrawListInstruction::TYPE_DRAW_INDEXED_INDIRECT: {1455const DrawListDrawIndexedIndirectInstruction *draw_indexed_indirect_instruction = reinterpret_cast<const DrawListDrawIndexedIndirectInstruction *>(instruction);1456print_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);1457instruction_data_cursor += sizeof(DrawListDrawIndexedIndirectInstruction);1458} break;1459case DrawListInstruction::TYPE_EXECUTE_COMMANDS: {1460print_line("\tEXECUTE COMMANDS");1461instruction_data_cursor += sizeof(DrawListExecuteCommandsInstruction);1462} break;1463case DrawListInstruction::TYPE_NEXT_SUBPASS: {1464print_line("\tNEXT SUBPASS");1465instruction_data_cursor += sizeof(DrawListNextSubpassInstruction);1466} break;1467case DrawListInstruction::TYPE_SET_BLEND_CONSTANTS: {1468const DrawListSetBlendConstantsInstruction *set_blend_constants_instruction = reinterpret_cast<const DrawListSetBlendConstantsInstruction *>(instruction);1469print_line("\tSET BLEND CONSTANTS COLOR", set_blend_constants_instruction->color);1470instruction_data_cursor += sizeof(DrawListSetBlendConstantsInstruction);1471} break;1472case DrawListInstruction::TYPE_SET_LINE_WIDTH: {1473const DrawListSetLineWidthInstruction *set_line_width_instruction = reinterpret_cast<const DrawListSetLineWidthInstruction *>(instruction);1474print_line("\tSET LINE WIDTH", set_line_width_instruction->width);1475instruction_data_cursor += sizeof(DrawListSetLineWidthInstruction);1476} break;1477case DrawListInstruction::TYPE_SET_PUSH_CONSTANT: {1478const DrawListSetPushConstantInstruction *set_push_constant_instruction = reinterpret_cast<const DrawListSetPushConstantInstruction *>(instruction);1479print_line("\tSET PUSH CONSTANT SIZE", set_push_constant_instruction->size);1480instruction_data_cursor += sizeof(DrawListSetPushConstantInstruction);1481instruction_data_cursor += set_push_constant_instruction->size;1482} break;1483case DrawListInstruction::TYPE_SET_SCISSOR: {1484const DrawListSetScissorInstruction *set_scissor_instruction = reinterpret_cast<const DrawListSetScissorInstruction *>(instruction);1485print_line("\tSET SCISSOR", set_scissor_instruction->rect);1486instruction_data_cursor += sizeof(DrawListSetScissorInstruction);1487} break;1488case DrawListInstruction::TYPE_SET_VIEWPORT: {1489const DrawListSetViewportInstruction *set_viewport_instruction = reinterpret_cast<const DrawListSetViewportInstruction *>(instruction);1490print_line("\tSET VIEWPORT", set_viewport_instruction->rect);1491instruction_data_cursor += sizeof(DrawListSetViewportInstruction);1492} break;1493case DrawListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE: {1494const DrawListUniformSetPrepareForUseInstruction *uniform_set_prepare_for_use_instruction = reinterpret_cast<const DrawListUniformSetPrepareForUseInstruction *>(instruction);1495print_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);1496instruction_data_cursor += sizeof(DrawListUniformSetPrepareForUseInstruction);1497} break;1498default:1499DEV_ASSERT(false && "Unknown draw list instruction type.");1500return;1501}15021503instruction_data_cursor = GRAPH_ALIGN(instruction_data_cursor);1504}1505}15061507void RenderingDeviceGraph::_print_compute_list(const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {1508uint32_t instruction_data_cursor = 0;1509while (instruction_data_cursor < p_instruction_data_size) {1510DEV_ASSERT((instruction_data_cursor + sizeof(ComputeListInstruction)) <= p_instruction_data_size);15111512const ComputeListInstruction *instruction = reinterpret_cast<const ComputeListInstruction *>(&p_instruction_data[instruction_data_cursor]);1513switch (instruction->type) {1514case ComputeListInstruction::TYPE_BIND_PIPELINE: {1515const ComputeListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const ComputeListBindPipelineInstruction *>(instruction);1516print_line("\tBIND PIPELINE ID", itos(bind_pipeline_instruction->pipeline.id));1517instruction_data_cursor += sizeof(ComputeListBindPipelineInstruction);1518} break;1519case ComputeListInstruction::TYPE_BIND_UNIFORM_SETS: {1520const ComputeListBindUniformSetsInstruction *bind_uniform_sets_instruction = reinterpret_cast<const ComputeListBindUniformSetsInstruction *>(instruction);1521print_line("\tBIND UNIFORM SETS COUNT", bind_uniform_sets_instruction->set_count);1522for (uint32_t i = 0; i < bind_uniform_sets_instruction->set_count; i++) {1523print_line("\tBIND UNIFORM SET ID", itos(bind_uniform_sets_instruction->uniform_set_ids()[i].id), "START INDEX", bind_uniform_sets_instruction->first_set_index);1524}1525instruction_data_cursor += sizeof(ComputeListBindUniformSetsInstruction) + sizeof(RDD::UniformSetID) * bind_uniform_sets_instruction->set_count;1526} break;1527case ComputeListInstruction::TYPE_DISPATCH: {1528const ComputeListDispatchInstruction *dispatch_instruction = reinterpret_cast<const ComputeListDispatchInstruction *>(instruction);1529print_line("\tDISPATCH", dispatch_instruction->x_groups, dispatch_instruction->y_groups, dispatch_instruction->z_groups);1530instruction_data_cursor += sizeof(ComputeListDispatchInstruction);1531} break;1532case ComputeListInstruction::TYPE_DISPATCH_INDIRECT: {1533const ComputeListDispatchIndirectInstruction *dispatch_indirect_instruction = reinterpret_cast<const ComputeListDispatchIndirectInstruction *>(instruction);1534print_line("\tDISPATCH INDIRECT BUFFER ID", itos(dispatch_indirect_instruction->buffer.id), "OFFSET", dispatch_indirect_instruction->offset);1535instruction_data_cursor += sizeof(ComputeListDispatchIndirectInstruction);1536} break;1537case ComputeListInstruction::TYPE_SET_PUSH_CONSTANT: {1538const ComputeListSetPushConstantInstruction *set_push_constant_instruction = reinterpret_cast<const ComputeListSetPushConstantInstruction *>(instruction);1539print_line("\tSET PUSH CONSTANT SIZE", set_push_constant_instruction->size);1540instruction_data_cursor += sizeof(ComputeListSetPushConstantInstruction);1541instruction_data_cursor += set_push_constant_instruction->size;1542} break;1543case ComputeListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE: {1544const ComputeListUniformSetPrepareForUseInstruction *uniform_set_prepare_for_use_instruction = reinterpret_cast<const ComputeListUniformSetPrepareForUseInstruction *>(instruction);1545print_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));1546instruction_data_cursor += sizeof(ComputeListUniformSetPrepareForUseInstruction);1547} break;1548default:1549DEV_ASSERT(false && "Unknown compute list instruction type.");1550return;1551}15521553instruction_data_cursor = GRAPH_ALIGN(instruction_data_cursor);1554}1555}15561557void 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) {1558DEV_ASSERT(p_driver != nullptr);1559DEV_ASSERT(p_render_pass_creation_function != nullptr);1560DEV_ASSERT(p_frame_count > 0);15611562driver = p_driver;1563device = p_device;1564render_pass_creation_function = p_render_pass_creation_function;1565frames.resize(p_frame_count);15661567for (uint32_t i = 0; i < p_frame_count; i++) {1568frames[i].secondary_command_buffers.resize(p_secondary_command_buffers_per_frame);15691570for (uint32_t j = 0; j < p_secondary_command_buffers_per_frame; j++) {1571SecondaryCommandBuffer &secondary = frames[i].secondary_command_buffers[j];1572secondary.command_pool = driver->command_pool_create(p_secondary_command_queue_family, RDD::COMMAND_BUFFER_TYPE_SECONDARY);1573secondary.command_buffer = driver->command_buffer_create(secondary.command_pool);1574secondary.task = WorkerThreadPool::INVALID_TASK_ID;1575}1576}15771578driver_honors_barriers = driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS);1579driver_clears_with_copy_engine = driver->api_trait_get(RDD::API_TRAIT_CLEARS_WITH_COPY_ENGINE);1580driver_buffers_require_transitions = driver->api_trait_get(RDD::API_TRAIT_BUFFERS_REQUIRE_TRANSITIONS);1581}15821583void RenderingDeviceGraph::finalize() {1584if (!frames.is_empty()) {1585_wait_for_secondary_command_buffer_tasks();1586}15871588for (Frame &f : frames) {1589for (SecondaryCommandBuffer &secondary : f.secondary_command_buffers) {1590if (secondary.command_pool.id != 0) {1591driver->command_pool_free(secondary.command_pool);1592}1593}1594}15951596frames.clear();1597}15981599void RenderingDeviceGraph::begin() {1600command_data.clear();1601command_data_offsets.clear();1602command_normalization_barriers.clear();1603command_transition_barriers.clear();1604command_buffer_barriers.clear();1605command_label_chars.clear();1606command_label_colors.clear();1607command_label_offsets.clear();1608command_list_nodes.clear();1609read_slice_list_nodes.clear();1610write_slice_list_nodes.clear();1611command_count = 0;1612command_label_count = 0;1613command_timestamp_index = -1;1614command_synchronization_index = -1;1615command_synchronization_pending = false;1616command_label_index = -1;1617frames[frame].secondary_command_buffers_used = 0;1618draw_instruction_list.index = 0;1619compute_instruction_list.index = 0;1620tracking_frame++;16211622#ifdef DEV_ENABLED1623write_dependency_counters.clear();1624#endif1625}16261627void RenderingDeviceGraph::add_buffer_clear(RDD::BufferID p_dst, ResourceTracker *p_dst_tracker, uint32_t p_offset, uint32_t p_size) {1628DEV_ASSERT(p_dst_tracker != nullptr);16291630int32_t command_index;1631RecordedBufferClearCommand *command = static_cast<RecordedBufferClearCommand *>(_allocate_command(sizeof(RecordedBufferClearCommand), command_index));1632command->type = RecordedCommand::TYPE_BUFFER_CLEAR;1633command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;1634command->buffer = p_dst;1635command->offset = p_offset;1636command->size = p_size;16371638ResourceUsage usage = RESOURCE_USAGE_COPY_TO;1639_add_command_to_graph(&p_dst_tracker, &usage, 1, command_index, command);1640}16411642void RenderingDeviceGraph::add_buffer_copy(RDD::BufferID p_src, ResourceTracker *p_src_tracker, RDD::BufferID p_dst, ResourceTracker *p_dst_tracker, RDD::BufferCopyRegion p_region) {1643// Source tracker is allowed to be null as it could be a read-only buffer.1644DEV_ASSERT(p_dst_tracker != nullptr);16451646int32_t command_index;1647RecordedBufferCopyCommand *command = static_cast<RecordedBufferCopyCommand *>(_allocate_command(sizeof(RecordedBufferCopyCommand), command_index));1648command->type = RecordedCommand::TYPE_BUFFER_COPY;1649command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;1650command->source = p_src;1651command->destination = p_dst;1652command->region = p_region;16531654ResourceTracker *trackers[2] = { p_dst_tracker, p_src_tracker };1655ResourceUsage usages[2] = { RESOURCE_USAGE_COPY_TO, RESOURCE_USAGE_COPY_FROM };1656_add_command_to_graph(trackers, usages, p_src_tracker != nullptr ? 2 : 1, command_index, command);1657}16581659void RenderingDeviceGraph::add_buffer_get_data(RDD::BufferID p_src, ResourceTracker *p_src_tracker, RDD::BufferID p_dst, RDD::BufferCopyRegion p_region) {1660// Source tracker is allowed to be null as it could be a read-only buffer.1661int32_t command_index;1662RecordedBufferGetDataCommand *command = static_cast<RecordedBufferGetDataCommand *>(_allocate_command(sizeof(RecordedBufferGetDataCommand), command_index));1663command->type = RecordedCommand::TYPE_BUFFER_GET_DATA;1664command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;1665command->source = p_src;1666command->destination = p_dst;1667command->region = p_region;16681669if (p_src_tracker != nullptr) {1670ResourceUsage usage = RESOURCE_USAGE_COPY_FROM;1671_add_command_to_graph(&p_src_tracker, &usage, 1, command_index, command);1672} else {1673_add_command_to_graph(nullptr, nullptr, 0, command_index, command);1674}1675}16761677void RenderingDeviceGraph::add_buffer_update(RDD::BufferID p_dst, ResourceTracker *p_dst_tracker, VectorView<RecordedBufferCopy> p_buffer_copies) {1678DEV_ASSERT(p_dst_tracker != nullptr);16791680size_t buffer_copies_size = p_buffer_copies.size() * sizeof(RecordedBufferCopy);1681uint64_t command_size = sizeof(RecordedBufferUpdateCommand) + buffer_copies_size;1682int32_t command_index;1683RecordedBufferUpdateCommand *command = static_cast<RecordedBufferUpdateCommand *>(_allocate_command(command_size, command_index));1684command->type = RecordedCommand::TYPE_BUFFER_UPDATE;1685command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;1686command->destination = p_dst;1687command->buffer_copies_count = p_buffer_copies.size();16881689RecordedBufferCopy *buffer_copies = command->buffer_copies();1690for (uint32_t i = 0; i < command->buffer_copies_count; i++) {1691buffer_copies[i] = p_buffer_copies[i];1692}16931694ResourceUsage buffer_usage = RESOURCE_USAGE_COPY_TO;1695_add_command_to_graph(&p_dst_tracker, &buffer_usage, 1, command_index, command);1696}16971698void RenderingDeviceGraph::add_driver_callback(RDD::DriverCallback p_callback, void *p_userdata, VectorView<ResourceTracker *> p_trackers, VectorView<RenderingDeviceGraph::ResourceUsage> p_usages) {1699DEV_ASSERT(p_trackers.size() == p_usages.size());17001701int32_t command_index;1702RecordedDriverCallbackCommand *command = static_cast<RecordedDriverCallbackCommand *>(_allocate_command(sizeof(RecordedDriverCallbackCommand), command_index));1703command->type = RecordedCommand::TYPE_DRIVER_CALLBACK;1704command->callback = p_callback;1705command->userdata = p_userdata;1706_add_command_to_graph((ResourceTracker **)p_trackers.ptr(), (ResourceUsage *)p_usages.ptr(), p_trackers.size(), command_index, command);1707}17081709void RenderingDeviceGraph::add_compute_list_begin(RDD::BreadcrumbMarker p_phase, uint32_t p_breadcrumb_data) {1710compute_instruction_list.clear();1711#if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)1712compute_instruction_list.breadcrumb = p_breadcrumb_data | (p_phase & ((1 << 16) - 1));1713#endif1714compute_instruction_list.index++;1715}17161717void RenderingDeviceGraph::add_compute_list_bind_pipeline(RDD::PipelineID p_pipeline) {1718ComputeListBindPipelineInstruction *instruction = reinterpret_cast<ComputeListBindPipelineInstruction *>(_allocate_compute_list_instruction(sizeof(ComputeListBindPipelineInstruction)));1719instruction->type = ComputeListInstruction::TYPE_BIND_PIPELINE;1720instruction->pipeline = p_pipeline;1721compute_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT);1722}17231724void RenderingDeviceGraph::add_compute_list_bind_uniform_set(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index) {1725add_compute_list_bind_uniform_sets(p_shader, VectorView(&p_uniform_set, 1), set_index, 1);1726}17271728void 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) {1729DEV_ASSERT(p_uniform_sets.size() >= p_set_count);17301731uint32_t instruction_size = sizeof(ComputeListBindUniformSetsInstruction) + sizeof(RDD::UniformSetID) * p_set_count;1732ComputeListBindUniformSetsInstruction *instruction = reinterpret_cast<ComputeListBindUniformSetsInstruction *>(_allocate_compute_list_instruction(instruction_size));1733instruction->type = ComputeListInstruction::TYPE_BIND_UNIFORM_SETS;1734instruction->shader = p_shader;1735instruction->first_set_index = p_first_set_index;1736instruction->set_count = p_set_count;17371738RDD::UniformSetID *ids = instruction->uniform_set_ids();1739for (uint32_t i = 0; i < p_set_count; i++) {1740ids[i] = p_uniform_sets[i];1741}1742}17431744void RenderingDeviceGraph::add_compute_list_dispatch(uint32_t p_x_groups, uint32_t p_y_groups, uint32_t p_z_groups) {1745ComputeListDispatchInstruction *instruction = reinterpret_cast<ComputeListDispatchInstruction *>(_allocate_compute_list_instruction(sizeof(ComputeListDispatchInstruction)));1746instruction->type = ComputeListInstruction::TYPE_DISPATCH;1747instruction->x_groups = p_x_groups;1748instruction->y_groups = p_y_groups;1749instruction->z_groups = p_z_groups;1750}17511752void RenderingDeviceGraph::add_compute_list_dispatch_indirect(RDD::BufferID p_buffer, uint32_t p_offset) {1753ComputeListDispatchIndirectInstruction *instruction = reinterpret_cast<ComputeListDispatchIndirectInstruction *>(_allocate_compute_list_instruction(sizeof(ComputeListDispatchIndirectInstruction)));1754instruction->type = ComputeListInstruction::TYPE_DISPATCH_INDIRECT;1755instruction->buffer = p_buffer;1756instruction->offset = p_offset;1757compute_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_DRAW_INDIRECT_BIT);1758}17591760void RenderingDeviceGraph::add_compute_list_set_push_constant(RDD::ShaderID p_shader, const void *p_data, uint32_t p_data_size) {1761uint32_t instruction_size = sizeof(ComputeListSetPushConstantInstruction) + p_data_size;1762ComputeListSetPushConstantInstruction *instruction = reinterpret_cast<ComputeListSetPushConstantInstruction *>(_allocate_compute_list_instruction(instruction_size));1763instruction->type = ComputeListInstruction::TYPE_SET_PUSH_CONSTANT;1764instruction->size = p_data_size;1765instruction->shader = p_shader;1766memcpy(instruction->data(), p_data, p_data_size);1767}17681769void RenderingDeviceGraph::add_compute_list_uniform_set_prepare_for_use(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index) {1770ComputeListUniformSetPrepareForUseInstruction *instruction = reinterpret_cast<ComputeListUniformSetPrepareForUseInstruction *>(_allocate_compute_list_instruction(sizeof(ComputeListUniformSetPrepareForUseInstruction)));1771instruction->type = ComputeListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE;1772instruction->shader = p_shader;1773instruction->uniform_set = p_uniform_set;1774instruction->set_index = set_index;1775}17761777void RenderingDeviceGraph::add_compute_list_usage(ResourceTracker *p_tracker, ResourceUsage p_usage) {1778DEV_ASSERT(p_tracker != nullptr);17791780p_tracker->reset_if_outdated(tracking_frame);17811782if (p_tracker->compute_list_index != compute_instruction_list.index) {1783compute_instruction_list.command_trackers.push_back(p_tracker);1784compute_instruction_list.command_tracker_usages.push_back(p_usage);1785p_tracker->compute_list_index = compute_instruction_list.index;1786p_tracker->compute_list_usage = p_usage;1787}1788#ifdef DEV_ENABLED1789else if (p_tracker->compute_list_usage != p_usage) {1790ERR_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)));1791}1792#endif1793}17941795void RenderingDeviceGraph::add_compute_list_usages(VectorView<ResourceTracker *> p_trackers, VectorView<ResourceUsage> p_usages) {1796DEV_ASSERT(p_trackers.size() == p_usages.size());17971798for (uint32_t i = 0; i < p_trackers.size(); i++) {1799add_compute_list_usage(p_trackers[i], p_usages[i]);1800}1801}18021803void RenderingDeviceGraph::add_compute_list_end() {1804int32_t command_index;1805uint32_t instruction_data_size = compute_instruction_list.data.size();1806uint32_t command_size = sizeof(RecordedComputeListCommand) + instruction_data_size;1807RecordedComputeListCommand *command = static_cast<RecordedComputeListCommand *>(_allocate_command(command_size, command_index));1808command->type = RecordedCommand::TYPE_COMPUTE_LIST;1809command->self_stages = compute_instruction_list.stages;1810command->instruction_data_size = instruction_data_size;1811memcpy(command->instruction_data(), compute_instruction_list.data.ptr(), instruction_data_size);1812_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);1813}18141815void 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) {1816_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);1817}18181819void 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) {1820_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);1821}18221823void RenderingDeviceGraph::add_draw_list_bind_index_buffer(RDD::BufferID p_buffer, RDD::IndexBufferFormat p_format, uint32_t p_offset) {1824DrawListBindIndexBufferInstruction *instruction = reinterpret_cast<DrawListBindIndexBufferInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListBindIndexBufferInstruction)));1825instruction->type = DrawListInstruction::TYPE_BIND_INDEX_BUFFER;1826instruction->buffer = p_buffer;1827instruction->format = p_format;1828instruction->offset = p_offset;18291830if (instruction->buffer.id != 0) {1831draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_VERTEX_INPUT_BIT);1832}1833}18341835void RenderingDeviceGraph::add_draw_list_bind_pipeline(RDD::PipelineID p_pipeline, BitField<RDD::PipelineStageBits> p_pipeline_stage_bits) {1836DrawListBindPipelineInstruction *instruction = reinterpret_cast<DrawListBindPipelineInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListBindPipelineInstruction)));1837instruction->type = DrawListInstruction::TYPE_BIND_PIPELINE;1838instruction->pipeline = p_pipeline;1839draw_instruction_list.stages = draw_instruction_list.stages | p_pipeline_stage_bits;1840}18411842void RenderingDeviceGraph::add_draw_list_bind_uniform_set(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index) {1843add_draw_list_bind_uniform_sets(p_shader, VectorView(&p_uniform_set, 1), set_index, 1);1844}18451846void 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) {1847DEV_ASSERT(p_uniform_sets.size() >= p_set_count);18481849uint32_t instruction_size = sizeof(DrawListBindUniformSetsInstruction) + sizeof(RDD::UniformSetID) * p_set_count;1850DrawListBindUniformSetsInstruction *instruction = reinterpret_cast<DrawListBindUniformSetsInstruction *>(_allocate_draw_list_instruction(instruction_size));1851instruction->type = DrawListInstruction::TYPE_BIND_UNIFORM_SETS;1852instruction->shader = p_shader;1853instruction->first_set_index = p_first_index;1854instruction->set_count = p_set_count;18551856for (uint32_t i = 0; i < p_set_count; i++) {1857instruction->uniform_set_ids()[i] = p_uniform_sets[i];1858}1859}18601861void RenderingDeviceGraph::add_draw_list_bind_vertex_buffers(VectorView<RDD::BufferID> p_vertex_buffers, VectorView<uint64_t> p_vertex_buffer_offsets) {1862DEV_ASSERT(p_vertex_buffers.size() == p_vertex_buffer_offsets.size());18631864uint32_t instruction_size = sizeof(DrawListBindVertexBuffersInstruction) + sizeof(RDD::BufferID) * p_vertex_buffers.size() + sizeof(uint64_t) * p_vertex_buffer_offsets.size();1865DrawListBindVertexBuffersInstruction *instruction = reinterpret_cast<DrawListBindVertexBuffersInstruction *>(_allocate_draw_list_instruction(instruction_size));1866instruction->type = DrawListInstruction::TYPE_BIND_VERTEX_BUFFERS;1867instruction->vertex_buffers_count = p_vertex_buffers.size();18681869RDD::BufferID *vertex_buffers = instruction->vertex_buffers();1870uint64_t *vertex_buffer_offsets = instruction->vertex_buffer_offsets();1871for (uint32_t i = 0; i < instruction->vertex_buffers_count; i++) {1872vertex_buffers[i] = p_vertex_buffers[i];1873vertex_buffer_offsets[i] = p_vertex_buffer_offsets[i];1874}18751876if (instruction->vertex_buffers_count > 0) {1877draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_VERTEX_INPUT_BIT);1878}1879}18801881void RenderingDeviceGraph::add_draw_list_clear_attachments(VectorView<RDD::AttachmentClear> p_attachments_clear, VectorView<Rect2i> p_attachments_clear_rect) {1882uint32_t instruction_size = sizeof(DrawListClearAttachmentsInstruction) + sizeof(RDD::AttachmentClear) * p_attachments_clear.size() + sizeof(Rect2i) * p_attachments_clear_rect.size();1883DrawListClearAttachmentsInstruction *instruction = reinterpret_cast<DrawListClearAttachmentsInstruction *>(_allocate_draw_list_instruction(instruction_size));1884instruction->type = DrawListInstruction::TYPE_CLEAR_ATTACHMENTS;1885instruction->attachments_clear_count = p_attachments_clear.size();1886instruction->attachments_clear_rect_count = p_attachments_clear_rect.size();18871888RDD::AttachmentClear *attachments_clear = instruction->attachments_clear();1889Rect2i *attachments_clear_rect = instruction->attachments_clear_rect();1890for (uint32_t i = 0; i < instruction->attachments_clear_count; i++) {1891attachments_clear[i] = p_attachments_clear[i];1892}18931894for (uint32_t i = 0; i < instruction->attachments_clear_rect_count; i++) {1895attachments_clear_rect[i] = p_attachments_clear_rect[i];1896}1897}18981899void RenderingDeviceGraph::add_draw_list_draw(uint32_t p_vertex_count, uint32_t p_instance_count) {1900DrawListDrawInstruction *instruction = reinterpret_cast<DrawListDrawInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListDrawInstruction)));1901instruction->type = DrawListInstruction::TYPE_DRAW;1902instruction->vertex_count = p_vertex_count;1903instruction->instance_count = p_instance_count;1904}19051906void RenderingDeviceGraph::add_draw_list_draw_indexed(uint32_t p_index_count, uint32_t p_instance_count, uint32_t p_first_index) {1907DrawListDrawIndexedInstruction *instruction = reinterpret_cast<DrawListDrawIndexedInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListDrawIndexedInstruction)));1908instruction->type = DrawListInstruction::TYPE_DRAW_INDEXED;1909instruction->index_count = p_index_count;1910instruction->instance_count = p_instance_count;1911instruction->first_index = p_first_index;1912}19131914void RenderingDeviceGraph::add_draw_list_draw_indirect(RDD::BufferID p_buffer, uint32_t p_offset, uint32_t p_draw_count, uint32_t p_stride) {1915DrawListDrawIndirectInstruction *instruction = reinterpret_cast<DrawListDrawIndirectInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListDrawIndirectInstruction)));1916instruction->type = DrawListInstruction::TYPE_DRAW_INDIRECT;1917instruction->buffer = p_buffer;1918instruction->offset = p_offset;1919instruction->draw_count = p_draw_count;1920instruction->stride = p_stride;1921draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_DRAW_INDIRECT_BIT);1922}19231924void RenderingDeviceGraph::add_draw_list_draw_indexed_indirect(RDD::BufferID p_buffer, uint32_t p_offset, uint32_t p_draw_count, uint32_t p_stride) {1925DrawListDrawIndexedIndirectInstruction *instruction = reinterpret_cast<DrawListDrawIndexedIndirectInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListDrawIndexedIndirectInstruction)));1926instruction->type = DrawListInstruction::TYPE_DRAW_INDEXED_INDIRECT;1927instruction->buffer = p_buffer;1928instruction->offset = p_offset;1929instruction->draw_count = p_draw_count;1930instruction->stride = p_stride;1931draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_DRAW_INDIRECT_BIT);1932}19331934void RenderingDeviceGraph::add_draw_list_execute_commands(RDD::CommandBufferID p_command_buffer) {1935DrawListExecuteCommandsInstruction *instruction = reinterpret_cast<DrawListExecuteCommandsInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListExecuteCommandsInstruction)));1936instruction->type = DrawListInstruction::TYPE_EXECUTE_COMMANDS;1937instruction->command_buffer = p_command_buffer;1938}19391940void RenderingDeviceGraph::add_draw_list_next_subpass(RDD::CommandBufferType p_command_buffer_type) {1941DrawListNextSubpassInstruction *instruction = reinterpret_cast<DrawListNextSubpassInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListNextSubpassInstruction)));1942instruction->type = DrawListInstruction::TYPE_NEXT_SUBPASS;1943instruction->command_buffer_type = p_command_buffer_type;1944}19451946void RenderingDeviceGraph::add_draw_list_set_blend_constants(const Color &p_color) {1947DrawListSetBlendConstantsInstruction *instruction = reinterpret_cast<DrawListSetBlendConstantsInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListSetBlendConstantsInstruction)));1948instruction->type = DrawListInstruction::TYPE_SET_BLEND_CONSTANTS;1949instruction->color = p_color;1950}19511952void RenderingDeviceGraph::add_draw_list_set_line_width(float p_width) {1953DrawListSetLineWidthInstruction *instruction = reinterpret_cast<DrawListSetLineWidthInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListSetLineWidthInstruction)));1954instruction->type = DrawListInstruction::TYPE_SET_LINE_WIDTH;1955instruction->width = p_width;1956}19571958void RenderingDeviceGraph::add_draw_list_set_push_constant(RDD::ShaderID p_shader, const void *p_data, uint32_t p_data_size) {1959uint32_t instruction_size = sizeof(DrawListSetPushConstantInstruction) + p_data_size;1960DrawListSetPushConstantInstruction *instruction = reinterpret_cast<DrawListSetPushConstantInstruction *>(_allocate_draw_list_instruction(instruction_size));1961instruction->type = DrawListInstruction::TYPE_SET_PUSH_CONSTANT;1962instruction->size = p_data_size;1963instruction->shader = p_shader;1964memcpy(instruction->data(), p_data, p_data_size);1965}19661967void RenderingDeviceGraph::add_draw_list_set_scissor(Rect2i p_rect) {1968DrawListSetScissorInstruction *instruction = reinterpret_cast<DrawListSetScissorInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListSetScissorInstruction)));1969instruction->type = DrawListInstruction::TYPE_SET_SCISSOR;1970instruction->rect = p_rect;1971}19721973void RenderingDeviceGraph::add_draw_list_set_viewport(Rect2i p_rect) {1974DrawListSetViewportInstruction *instruction = reinterpret_cast<DrawListSetViewportInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListSetViewportInstruction)));1975instruction->type = DrawListInstruction::TYPE_SET_VIEWPORT;1976instruction->rect = p_rect;1977}19781979void RenderingDeviceGraph::add_draw_list_uniform_set_prepare_for_use(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index) {1980DrawListUniformSetPrepareForUseInstruction *instruction = reinterpret_cast<DrawListUniformSetPrepareForUseInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListUniformSetPrepareForUseInstruction)));1981instruction->type = DrawListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE;1982instruction->shader = p_shader;1983instruction->uniform_set = p_uniform_set;1984instruction->set_index = set_index;1985}19861987void RenderingDeviceGraph::add_draw_list_usage(ResourceTracker *p_tracker, ResourceUsage p_usage) {1988p_tracker->reset_if_outdated(tracking_frame);19891990if (p_tracker->draw_list_index != draw_instruction_list.index) {1991draw_instruction_list.command_trackers.push_back(p_tracker);1992draw_instruction_list.command_tracker_usages.push_back(p_usage);1993p_tracker->draw_list_index = draw_instruction_list.index;1994p_tracker->draw_list_usage = p_usage;1995}1996#ifdef DEV_ENABLED1997else if (p_tracker->draw_list_usage != p_usage) {1998ERR_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)));1999}2000#endif2001}20022003void RenderingDeviceGraph::add_draw_list_usages(VectorView<ResourceTracker *> p_trackers, VectorView<ResourceUsage> p_usages) {2004DEV_ASSERT(p_trackers.size() == p_usages.size());20052006for (uint32_t i = 0; i < p_trackers.size(); i++) {2007add_draw_list_usage(p_trackers[i], p_usages[i]);2008}2009}20102011void RenderingDeviceGraph::add_draw_list_end() {2012FramebufferCache *framebuffer_cache = draw_instruction_list.framebuffer_cache;2013int32_t command_index;2014uint32_t clear_values_size = sizeof(RDD::RenderPassClearValue) * draw_instruction_list.attachment_clear_values.size();2015uint32_t trackers_count = framebuffer_cache != nullptr ? framebuffer_cache->trackers.size() : 0;2016uint32_t trackers_and_ops_size = (sizeof(ResourceTracker *) + sizeof(RDD::AttachmentLoadOp) + sizeof(RDD::AttachmentStoreOp)) * trackers_count;2017uint32_t instruction_data_size = draw_instruction_list.data.size();2018uint32_t command_size = sizeof(RecordedDrawListCommand) + clear_values_size + trackers_and_ops_size + instruction_data_size;2019RecordedDrawListCommand *command = static_cast<RecordedDrawListCommand *>(_allocate_command(command_size, command_index));2020command->type = RecordedCommand::TYPE_DRAW_LIST;2021command->self_stages = draw_instruction_list.stages;2022command->framebuffer_cache = framebuffer_cache;2023command->render_pass = draw_instruction_list.render_pass;2024command->framebuffer = draw_instruction_list.framebuffer;2025command->instruction_data_size = instruction_data_size;2026command->command_buffer_type = RDD::COMMAND_BUFFER_TYPE_PRIMARY;2027command->region = draw_instruction_list.region;2028#if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)2029command->breadcrumb = draw_instruction_list.breadcrumb;2030#endif2031command->split_cmd_buffer = draw_instruction_list.split_cmd_buffer;2032command->clear_values_count = draw_instruction_list.attachment_clear_values.size();2033command->trackers_count = trackers_count;20342035// 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.2036uint32_t attachment_op_count = draw_instruction_list.attachment_operations.size();2037ResourceTracker **trackers = command->trackers();2038RDD::AttachmentLoadOp *load_ops = command->load_ops();2039RDD::AttachmentStoreOp *store_ops = command->store_ops();2040for (uint32_t i = 0; i < command->trackers_count; i++) {2041ResourceTracker *resource_tracker = framebuffer_cache->trackers[i];2042if (resource_tracker != nullptr) {2043if (i < command->clear_values_count && i < attachment_op_count && draw_instruction_list.attachment_operations[i] == ATTACHMENT_OPERATION_CLEAR) {2044load_ops[i] = RDD::ATTACHMENT_LOAD_OP_CLEAR;2045} else if (i < attachment_op_count && draw_instruction_list.attachment_operations[i] == ATTACHMENT_OPERATION_IGNORE) {2046load_ops[i] = RDD::ATTACHMENT_LOAD_OP_DONT_CARE;2047} else if (resource_tracker->is_discardable) {2048bool resource_has_parent = resource_tracker->parent != nullptr;2049ResourceTracker *search_tracker = resource_has_parent ? resource_tracker->parent : resource_tracker;2050search_tracker->reset_if_outdated(tracking_frame);2051bool resource_was_modified_this_frame = search_tracker->write_command_or_list_index >= 0;2052load_ops[i] = resource_was_modified_this_frame ? RDD::ATTACHMENT_LOAD_OP_LOAD : RDD::ATTACHMENT_LOAD_OP_DONT_CARE;2053} else {2054load_ops[i] = RDD::ATTACHMENT_LOAD_OP_LOAD;2055}20562057store_ops[i] = resource_tracker->is_discardable ? RDD::ATTACHMENT_STORE_OP_DONT_CARE : RDD::ATTACHMENT_STORE_OP_STORE;2058} else {2059load_ops[i] = RDD::ATTACHMENT_LOAD_OP_DONT_CARE;2060store_ops[i] = RDD::ATTACHMENT_STORE_OP_DONT_CARE;2061}20622063trackers[i] = resource_tracker;2064}20652066RDD::RenderPassClearValue *clear_values = command->clear_values();2067for (uint32_t i = 0; i < command->clear_values_count; i++) {2068clear_values[i] = draw_instruction_list.attachment_clear_values[i];2069}20702071memcpy(command->instruction_data(), draw_instruction_list.data.ptr(), instruction_data_size);2072_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);2073}20742075void RenderingDeviceGraph::add_texture_clear(RDD::TextureID p_dst, ResourceTracker *p_dst_tracker, const Color &p_color, const RDD::TextureSubresourceRange &p_range) {2076DEV_ASSERT(p_dst_tracker != nullptr);20772078int32_t command_index;2079RecordedTextureClearCommand *command = static_cast<RecordedTextureClearCommand *>(_allocate_command(sizeof(RecordedTextureClearCommand), command_index));2080command->type = RecordedCommand::TYPE_TEXTURE_CLEAR;2081command->texture = p_dst;2082command->color = p_color;2083command->range = p_range;20842085ResourceUsage usage;2086if (driver_clears_with_copy_engine) {2087command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;2088usage = RESOURCE_USAGE_COPY_TO;2089} else {2090// If the driver is uncapable of using the copy engine for clearing the image (e.g. D3D12), we must either transition the2091// resource to a render target or a storage image as that's the only two ways it can perform the operation.2092if (p_dst_tracker->texture_usage & RDD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT) {2093command->self_stages = RDD::PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;2094usage = RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE;2095} else {2096command->self_stages = RDD::PIPELINE_STAGE_CLEAR_STORAGE_BIT;2097usage = RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE;2098}2099}21002101_add_command_to_graph(&p_dst_tracker, &usage, 1, command_index, command);2102}21032104void 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) {2105DEV_ASSERT(p_src_tracker != nullptr);2106DEV_ASSERT(p_dst_tracker != nullptr);21072108int32_t command_index;2109uint64_t command_size = sizeof(RecordedTextureCopyCommand) + p_texture_copy_regions.size() * sizeof(RDD::TextureCopyRegion);2110RecordedTextureCopyCommand *command = static_cast<RecordedTextureCopyCommand *>(_allocate_command(command_size, command_index));2111command->type = RecordedCommand::TYPE_TEXTURE_COPY;2112command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;2113command->from_texture = p_src;2114command->to_texture = p_dst;2115command->texture_copy_regions_count = p_texture_copy_regions.size();21162117RDD::TextureCopyRegion *texture_copy_regions = command->texture_copy_regions();2118for (uint32_t i = 0; i < command->texture_copy_regions_count; i++) {2119texture_copy_regions[i] = p_texture_copy_regions[i];2120}21212122ResourceTracker *trackers[2] = { p_dst_tracker, p_src_tracker };2123ResourceUsage usages[2] = { RESOURCE_USAGE_COPY_TO, RESOURCE_USAGE_COPY_FROM };2124_add_command_to_graph(trackers, usages, 2, command_index, command);2125}21262127void 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) {2128DEV_ASSERT(p_src_tracker != nullptr);21292130int32_t command_index;2131uint64_t command_size = sizeof(RecordedTextureGetDataCommand) + p_buffer_texture_copy_regions.size() * sizeof(RDD::BufferTextureCopyRegion);2132RecordedTextureGetDataCommand *command = static_cast<RecordedTextureGetDataCommand *>(_allocate_command(command_size, command_index));2133command->type = RecordedCommand::TYPE_TEXTURE_GET_DATA;2134command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;2135command->from_texture = p_src;2136command->to_buffer = p_dst;2137command->buffer_texture_copy_regions_count = p_buffer_texture_copy_regions.size();21382139RDD::BufferTextureCopyRegion *buffer_texture_copy_regions = command->buffer_texture_copy_regions();2140for (uint32_t i = 0; i < command->buffer_texture_copy_regions_count; i++) {2141buffer_texture_copy_regions[i] = p_buffer_texture_copy_regions[i];2142}21432144if (p_dst_tracker != nullptr) {2145// Add the optional destination tracker if it was provided.2146ResourceTracker *trackers[2] = { p_dst_tracker, p_src_tracker };2147ResourceUsage usages[2] = { RESOURCE_USAGE_COPY_TO, RESOURCE_USAGE_COPY_FROM };2148_add_command_to_graph(trackers, usages, 2, command_index, command);2149} else {2150ResourceUsage usage = RESOURCE_USAGE_COPY_FROM;2151_add_command_to_graph(&p_src_tracker, &usage, 1, command_index, command);2152}2153}21542155void 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) {2156DEV_ASSERT(p_src_tracker != nullptr);2157DEV_ASSERT(p_dst_tracker != nullptr);21582159int32_t command_index;2160RecordedTextureResolveCommand *command = static_cast<RecordedTextureResolveCommand *>(_allocate_command(sizeof(RecordedTextureResolveCommand), command_index));2161command->type = RecordedCommand::TYPE_TEXTURE_RESOLVE;2162command->self_stages = RDD::PIPELINE_STAGE_RESOLVE_BIT;2163command->from_texture = p_src;2164command->to_texture = p_dst;2165command->src_layer = p_src_layer;2166command->src_mipmap = p_src_mipmap;2167command->dst_layer = p_dst_layer;2168command->dst_mipmap = p_dst_mipmap;21692170ResourceTracker *trackers[2] = { p_dst_tracker, p_src_tracker };2171ResourceUsage usages[2] = { RESOURCE_USAGE_RESOLVE_TO, RESOURCE_USAGE_RESOLVE_FROM };2172_add_command_to_graph(trackers, usages, 2, command_index, command);2173}21742175void RenderingDeviceGraph::add_texture_update(RDD::TextureID p_dst, ResourceTracker *p_dst_tracker, VectorView<RecordedBufferToTextureCopy> p_buffer_copies, VectorView<ResourceTracker *> p_buffer_trackers) {2176DEV_ASSERT(p_dst_tracker != nullptr);21772178int32_t command_index;2179uint64_t command_size = sizeof(RecordedTextureUpdateCommand) + p_buffer_copies.size() * sizeof(RecordedBufferToTextureCopy);2180RecordedTextureUpdateCommand *command = static_cast<RecordedTextureUpdateCommand *>(_allocate_command(command_size, command_index));2181command->type = RecordedCommand::TYPE_TEXTURE_UPDATE;2182command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;2183command->to_texture = p_dst;2184command->buffer_to_texture_copies_count = p_buffer_copies.size();21852186RecordedBufferToTextureCopy *buffer_to_texture_copies = command->buffer_to_texture_copies();2187for (uint32_t i = 0; i < command->buffer_to_texture_copies_count; i++) {2188buffer_to_texture_copies[i] = p_buffer_copies[i];2189}21902191if (p_buffer_trackers.size() > 0) {2192// Add the optional buffer trackers if they were provided.2193thread_local LocalVector<ResourceTracker *> trackers;2194thread_local LocalVector<ResourceUsage> usages;2195trackers.clear();2196usages.clear();2197for (uint32_t i = 0; i < p_buffer_trackers.size(); i++) {2198trackers.push_back(p_buffer_trackers[i]);2199usages.push_back(RESOURCE_USAGE_COPY_FROM);2200}22012202trackers.push_back(p_dst_tracker);2203usages.push_back(RESOURCE_USAGE_COPY_TO);22042205_add_command_to_graph(trackers.ptr(), usages.ptr(), trackers.size(), command_index, command);2206} else {2207ResourceUsage usage = RESOURCE_USAGE_COPY_TO;2208_add_command_to_graph(&p_dst_tracker, &usage, 1, command_index, command);2209}2210}22112212void RenderingDeviceGraph::add_capture_timestamp(RDD::QueryPoolID p_query_pool, uint32_t p_index) {2213int32_t command_index;2214RecordedCaptureTimestampCommand *command = static_cast<RecordedCaptureTimestampCommand *>(_allocate_command(sizeof(RecordedCaptureTimestampCommand), command_index));2215command->type = RecordedCommand::TYPE_CAPTURE_TIMESTAMP;2216command->self_stages = 0;2217command->pool = p_query_pool;2218command->index = p_index;2219_add_command_to_graph(nullptr, nullptr, 0, command_index, command);2220}22212222void RenderingDeviceGraph::add_synchronization() {2223// Synchronization is only acknowledged if commands have been recorded on the graph already.2224if (command_count > 0) {2225command_synchronization_pending = true;2226}2227}22282229void RenderingDeviceGraph::begin_label(const Span<char> &p_label_name, const Color &p_color) {2230uint32_t command_label_offset = command_label_chars.size();2231int command_label_size = p_label_name.size();2232command_label_chars.resize(command_label_offset + command_label_size + 1);2233memcpy(&command_label_chars[command_label_offset], p_label_name.ptr(), command_label_size);2234command_label_chars[command_label_offset + command_label_size] = '\0';2235command_label_colors.push_back(p_color);2236command_label_offsets.push_back(command_label_offset);2237command_label_index = command_label_count;2238command_label_count++;2239}22402241void RenderingDeviceGraph::end_label() {2242command_label_index = -1;2243}22442245void RenderingDeviceGraph::end(bool p_reorder_commands, bool p_full_barriers, RDD::CommandBufferID &r_command_buffer, CommandBufferPool &r_command_buffer_pool) {2246if (command_count == 0) {2247// No commands have been logged, do nothing.2248return;2249}22502251thread_local LocalVector<RecordedCommandSort> commands_sorted;2252if (p_reorder_commands) {2253thread_local LocalVector<int64_t> command_stack;2254thread_local LocalVector<int32_t> sorted_command_indices;2255thread_local LocalVector<uint32_t> command_degrees;2256int32_t adjacency_list_index = 0;2257int32_t command_index;22582259// Count all the incoming connections to every node by traversing their adjacency list.2260command_degrees.resize(command_count);2261memset(command_degrees.ptr(), 0, sizeof(uint32_t) * command_degrees.size());2262for (uint32_t i = 0; i < command_count; i++) {2263const RecordedCommand &recorded_command = *reinterpret_cast<const RecordedCommand *>(&command_data[command_data_offsets[i]]);2264adjacency_list_index = recorded_command.adjacent_command_list_index;2265while (adjacency_list_index >= 0) {2266const RecordedCommandListNode &command_list_node = command_list_nodes[adjacency_list_index];2267DEV_ASSERT((command_list_node.command_index != int32_t(i)) && "Command can't have itself as a dependency.");2268command_degrees[command_list_node.command_index] += 1;2269adjacency_list_index = command_list_node.next_list_index;2270}2271}22722273// Push to the stack all nodes that have no incoming connections.2274command_stack.clear();2275for (uint32_t i = 0; i < command_count; i++) {2276if (command_degrees[i] == 0) {2277command_stack.push_back(i);2278}2279}22802281sorted_command_indices.clear();2282while (!command_stack.is_empty()) {2283// Pop command from the stack.2284command_index = command_stack[command_stack.size() - 1];2285command_stack.resize(command_stack.size() - 1);22862287// Add it to the sorted commands.2288sorted_command_indices.push_back(command_index);22892290// Search for its adjacents and lower their degree for every visit. If the degree reaches zero, we push the command to the stack.2291const uint32_t command_data_offset = command_data_offsets[command_index];2292const RecordedCommand &recorded_command = *reinterpret_cast<const RecordedCommand *>(&command_data[command_data_offset]);2293adjacency_list_index = recorded_command.adjacent_command_list_index;2294while (adjacency_list_index >= 0) {2295const RecordedCommandListNode &command_list_node = command_list_nodes[adjacency_list_index];2296uint32_t &command_degree = command_degrees[command_list_node.command_index];2297DEV_ASSERT(command_degree > 0);2298command_degree--;2299if (command_degree == 0) {2300command_stack.push_back(command_list_node.command_index);2301}23022303adjacency_list_index = command_list_node.next_list_index;2304}2305}23062307// Batch buffer, texture, draw lists and compute operations together.2308const uint32_t PriorityTable[RecordedCommand::TYPE_MAX] = {23090, // TYPE_NONE23101, // TYPE_BUFFER_CLEAR23111, // TYPE_BUFFER_COPY23121, // TYPE_BUFFER_GET_DATA23131, // TYPE_BUFFER_UPDATE23144, // TYPE_COMPUTE_LIST23153, // TYPE_DRAW_LIST23162, // TYPE_TEXTURE_CLEAR23172, // TYPE_TEXTURE_COPY23182, // TYPE_TEXTURE_GET_DATA23192, // TYPE_TEXTURE_RESOLVE23202, // TYPE_TEXTURE_UPDATE23212, // TYPE_CAPTURE_TIMESTAMP23225, // TYPE_DRIVER_CALLBACK2323};23242325commands_sorted.clear();2326commands_sorted.resize(command_count);23272328for (uint32_t i = 0; i < command_count; i++) {2329const int32_t sorted_command_index = sorted_command_indices[i];2330const uint32_t command_data_offset = command_data_offsets[sorted_command_index];2331const RecordedCommand recorded_command = *reinterpret_cast<const RecordedCommand *>(&command_data[command_data_offset]);2332const uint32_t next_command_level = commands_sorted[sorted_command_index].level + 1;2333adjacency_list_index = recorded_command.adjacent_command_list_index;2334while (adjacency_list_index >= 0) {2335const RecordedCommandListNode &command_list_node = command_list_nodes[adjacency_list_index];2336uint32_t &adjacent_command_level = commands_sorted[command_list_node.command_index].level;2337if (adjacent_command_level < next_command_level) {2338adjacent_command_level = next_command_level;2339}23402341adjacency_list_index = command_list_node.next_list_index;2342}23432344commands_sorted[sorted_command_index].index = sorted_command_index;2345commands_sorted[sorted_command_index].priority = PriorityTable[recorded_command.type];2346}2347} else {2348commands_sorted.clear();2349commands_sorted.resize(command_count);23502351for (uint32_t i = 0; i < command_count; i++) {2352commands_sorted[i].index = i;2353}2354}23552356_wait_for_secondary_command_buffer_tasks();23572358if (command_count > 0) {2359int32_t current_label_index = -1;2360int32_t current_label_level = -1;2361_run_label_command_change(r_command_buffer, -1, -1, true, true, nullptr, 0, current_label_index, current_label_level);23622363if (device.workarounds.avoid_compute_after_draw) {2364// Reset the state of the workaround.2365workarounds_state.draw_list_found = false;2366}23672368if (p_reorder_commands) {2369#if PRINT_RENDER_GRAPH2370print_line("BEFORE SORT");2371_print_render_commands(commands_sorted.ptr(), command_count);2372#endif23732374commands_sorted.sort();23752376#if PRINT_RENDER_GRAPH2377print_line("AFTER SORT");2378_print_render_commands(commands_sorted.ptr(), command_count);2379#endif23802381#if PRINT_COMMAND_RECORDING2382print_line(vformat("Recording %d commands", command_count));2383#endif23842385uint32_t boosted_priority = 0;2386uint32_t current_level = commands_sorted[0].level;2387uint32_t current_level_start = 0;2388for (uint32_t i = 0; i < command_count; i++) {2389if (current_level != commands_sorted[i].level) {2390RecordedCommandSort *level_command_ptr = &commands_sorted[current_level_start];2391uint32_t level_command_count = i - current_level_start;2392_boost_priority_for_render_commands(level_command_ptr, level_command_count, boosted_priority);2393_group_barriers_for_render_commands(r_command_buffer, level_command_ptr, level_command_count, p_full_barriers);2394_run_render_commands(current_level, level_command_ptr, level_command_count, r_command_buffer, r_command_buffer_pool, current_label_index, current_label_level);2395current_level = commands_sorted[i].level;2396current_level_start = i;2397}2398}23992400RecordedCommandSort *level_command_ptr = &commands_sorted[current_level_start];2401uint32_t level_command_count = command_count - current_level_start;2402_boost_priority_for_render_commands(level_command_ptr, level_command_count, boosted_priority);2403_group_barriers_for_render_commands(r_command_buffer, level_command_ptr, level_command_count, p_full_barriers);2404_run_render_commands(current_level, level_command_ptr, level_command_count, r_command_buffer, r_command_buffer_pool, current_label_index, current_label_level);24052406#if PRINT_RENDER_GRAPH2407print_line("COMMANDS", command_count, "LEVELS", current_level + 1);2408#endif2409} else {2410for (uint32_t i = 0; i < command_count; i++) {2411_group_barriers_for_render_commands(r_command_buffer, &commands_sorted[i], 1, p_full_barriers);2412_run_render_commands(i, &commands_sorted[i], 1, r_command_buffer, r_command_buffer_pool, current_label_index, current_label_level);2413}2414}24152416_run_label_command_change(r_command_buffer, -1, -1, false, false, nullptr, 0, current_label_index, current_label_level);24172418#if PRINT_COMMAND_RECORDING2419print_line(vformat("Recorded %d commands", command_count));2420#endif2421}24222423// 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.2424frame = (frame + 1) % frames.size();2425}24262427#if PRINT_RESOURCE_TRACKER_TOTAL2428static uint32_t resource_tracker_total = 0;2429#endif24302431RenderingDeviceGraph::ResourceTracker *RenderingDeviceGraph::resource_tracker_create() {2432#if PRINT_RESOURCE_TRACKER_TOTAL2433print_line("Resource trackers:", ++resource_tracker_total);2434#endif2435return memnew(ResourceTracker);2436}24372438void RenderingDeviceGraph::resource_tracker_free(ResourceTracker *p_tracker) {2439if (p_tracker == nullptr) {2440return;2441}24422443if (p_tracker->in_parent_dirty_list) {2444// Delete the tracker from the parent's dirty linked list.2445if (p_tracker->parent->dirty_shared_list == p_tracker) {2446p_tracker->parent->dirty_shared_list = p_tracker->next_shared;2447} else {2448ResourceTracker *node = p_tracker->parent->dirty_shared_list;2449while (node != nullptr) {2450if (node->next_shared == p_tracker) {2451node->next_shared = p_tracker->next_shared;2452node = nullptr;2453} else {2454node = node->next_shared;2455}2456}2457}2458}24592460memdelete(p_tracker);24612462#if PRINT_RESOURCE_TRACKER_TOTAL2463print_line("Resource trackers:", --resource_tracker_total);2464#endif2465}24662467RenderingDeviceGraph::FramebufferCache *RenderingDeviceGraph::framebuffer_cache_create() {2468return memnew(FramebufferCache);2469}24702471void RenderingDeviceGraph::framebuffer_cache_free(RDD *p_driver, FramebufferCache *p_cache) {2472DEV_ASSERT(p_driver != nullptr);24732474if (p_cache == nullptr) {2475return;2476}24772478for (KeyValue<uint64_t, FramebufferStorage> &E : p_cache->storage_map) {2479p_driver->framebuffer_free(E.value.framebuffer);2480p_driver->render_pass_free(E.value.render_pass);2481}24822483memdelete(p_cache);2484}248524862487