Path: blob/master/servers/rendering/rendering_device_graph.cpp
20888 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 03637// Prints the total number of bytes used for draw lists in a frame.38#define PRINT_DRAW_LIST_STATS 03940RenderingDeviceGraph::RenderingDeviceGraph() {41driver_honors_barriers = false;42driver_clears_with_copy_engine = false;43}4445RenderingDeviceGraph::~RenderingDeviceGraph() {46}4748String RenderingDeviceGraph::_usage_to_string(ResourceUsage p_usage) {49switch (p_usage) {50case RESOURCE_USAGE_NONE:51return "None";52case RESOURCE_USAGE_COPY_FROM:53return "Copy From";54case RESOURCE_USAGE_COPY_TO:55return "Copy To";56case RESOURCE_USAGE_RESOLVE_FROM:57return "Resolve From";58case RESOURCE_USAGE_RESOLVE_TO:59return "Resolve To";60case RESOURCE_USAGE_UNIFORM_BUFFER_READ:61return "Uniform Buffer Read";62case RESOURCE_USAGE_INDIRECT_BUFFER_READ:63return "Indirect Buffer Read";64case RESOURCE_USAGE_TEXTURE_BUFFER_READ:65return "Texture Buffer Read";66case RESOURCE_USAGE_TEXTURE_BUFFER_READ_WRITE:67return "Texture Buffer Read Write";68case RESOURCE_USAGE_STORAGE_BUFFER_READ:69return "Storage Buffer Read";70case RESOURCE_USAGE_STORAGE_BUFFER_READ_WRITE:71return "Storage Buffer Read Write";72case RESOURCE_USAGE_VERTEX_BUFFER_READ:73return "Vertex Buffer Read";74case RESOURCE_USAGE_INDEX_BUFFER_READ:75return "Index Buffer Read";76case RESOURCE_USAGE_TEXTURE_SAMPLE:77return "Texture Sample";78case RESOURCE_USAGE_STORAGE_IMAGE_READ:79return "Storage Image Read";80case RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE:81return "Storage Image Read Write";82case RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE:83return "Attachment Color Read Write";84case RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE:85return "Attachment Depth Stencil Read Write";86case RESOURCE_USAGE_GENERAL:87return "General";88default:89ERR_FAIL_V_MSG("Invalid", vformat("Invalid resource usage %d.", p_usage));90}91}9293bool RenderingDeviceGraph::_is_write_usage(ResourceUsage p_usage) {94switch (p_usage) {95case RESOURCE_USAGE_COPY_FROM:96case RESOURCE_USAGE_RESOLVE_FROM:97case RESOURCE_USAGE_UNIFORM_BUFFER_READ:98case RESOURCE_USAGE_INDIRECT_BUFFER_READ:99case RESOURCE_USAGE_TEXTURE_BUFFER_READ:100case RESOURCE_USAGE_STORAGE_BUFFER_READ:101case RESOURCE_USAGE_VERTEX_BUFFER_READ:102case RESOURCE_USAGE_INDEX_BUFFER_READ:103case RESOURCE_USAGE_TEXTURE_SAMPLE:104case RESOURCE_USAGE_STORAGE_IMAGE_READ:105case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_SHADING_RATE_READ:106case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_DENSITY_MAP_READ:107case RESOURCE_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT:108case RESOURCE_USAGE_ACCELERATION_STRUCTURE_READ:109return false;110case RESOURCE_USAGE_COPY_TO:111case RESOURCE_USAGE_RESOLVE_TO:112case RESOURCE_USAGE_TEXTURE_BUFFER_READ_WRITE:113case RESOURCE_USAGE_STORAGE_BUFFER_READ_WRITE:114case RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE:115case RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE:116case RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE:117case RESOURCE_USAGE_GENERAL:118case RESOURCE_USAGE_ACCELERATION_STRUCTURE_READ_WRITE:119return true;120default:121DEV_ASSERT(false && "Invalid resource tracker usage.");122return false;123}124}125126RDD::TextureLayout RenderingDeviceGraph::_usage_to_image_layout(ResourceUsage p_usage) {127switch (p_usage) {128case RESOURCE_USAGE_COPY_FROM:129return RDD::TEXTURE_LAYOUT_COPY_SRC_OPTIMAL;130case RESOURCE_USAGE_COPY_TO:131return RDD::TEXTURE_LAYOUT_COPY_DST_OPTIMAL;132case RESOURCE_USAGE_RESOLVE_FROM:133return RDD::TEXTURE_LAYOUT_RESOLVE_SRC_OPTIMAL;134case RESOURCE_USAGE_RESOLVE_TO:135return RDD::TEXTURE_LAYOUT_RESOLVE_DST_OPTIMAL;136case RESOURCE_USAGE_TEXTURE_SAMPLE:137return RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;138case RESOURCE_USAGE_STORAGE_IMAGE_READ:139case RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE:140return RDD::TEXTURE_LAYOUT_STORAGE_OPTIMAL;141case RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE:142return RDD::TEXTURE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;143case RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE:144return RDD::TEXTURE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;145case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_SHADING_RATE_READ:146return RDD::TEXTURE_LAYOUT_FRAGMENT_SHADING_RATE_ATTACHMENT_OPTIMAL;147case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_DENSITY_MAP_READ:148return RDD::TEXTURE_LAYOUT_FRAGMENT_DENSITY_MAP_ATTACHMENT_OPTIMAL;149case RESOURCE_USAGE_GENERAL:150return RDD::TEXTURE_LAYOUT_GENERAL;151case RESOURCE_USAGE_NONE:152return RDD::TEXTURE_LAYOUT_UNDEFINED;153default:154DEV_ASSERT(false && "Invalid resource tracker usage or not an image usage.");155return RDD::TEXTURE_LAYOUT_UNDEFINED;156}157}158159RDD::BarrierAccessBits RenderingDeviceGraph::_usage_to_access_bits(ResourceUsage p_usage) {160#if FORCE_FULL_ACCESS_BITS161return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_MEMORY_READ_BIT | RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT);162#else163switch (p_usage) {164case RESOURCE_USAGE_NONE:165return RDD::BarrierAccessBits(0);166case RESOURCE_USAGE_COPY_FROM:167return RDD::BARRIER_ACCESS_COPY_READ_BIT;168case RESOURCE_USAGE_COPY_TO:169return RDD::BARRIER_ACCESS_COPY_WRITE_BIT;170case RESOURCE_USAGE_RESOLVE_FROM:171return RDD::BARRIER_ACCESS_RESOLVE_READ_BIT;172case RESOURCE_USAGE_RESOLVE_TO:173return RDD::BARRIER_ACCESS_RESOLVE_WRITE_BIT;174case RESOURCE_USAGE_UNIFORM_BUFFER_READ:175return RDD::BARRIER_ACCESS_UNIFORM_READ_BIT;176case RESOURCE_USAGE_INDIRECT_BUFFER_READ:177return RDD::BARRIER_ACCESS_INDIRECT_COMMAND_READ_BIT;178case RESOURCE_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT:179// Acceleration structure build inputs can be either storage buffers with vertices, indices, transforms, or180// other acceleration structures (BLAS)181return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_COPY_READ_BIT | RDD::BARRIER_ACCESS_ACCELERATION_STRUCTURE_READ_BIT);182case RESOURCE_USAGE_ACCELERATION_STRUCTURE_READ:183return RDD::BARRIER_ACCESS_ACCELERATION_STRUCTURE_READ_BIT;184case RESOURCE_USAGE_STORAGE_BUFFER_READ:185case RESOURCE_USAGE_STORAGE_IMAGE_READ:186case RESOURCE_USAGE_TEXTURE_BUFFER_READ:187case RESOURCE_USAGE_TEXTURE_SAMPLE:188return RDD::BARRIER_ACCESS_SHADER_READ_BIT;189case RESOURCE_USAGE_TEXTURE_BUFFER_READ_WRITE:190case RESOURCE_USAGE_STORAGE_BUFFER_READ_WRITE:191case RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE:192return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_SHADER_READ_BIT | RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);193case RESOURCE_USAGE_VERTEX_BUFFER_READ:194return RDD::BARRIER_ACCESS_VERTEX_ATTRIBUTE_READ_BIT;195case RESOURCE_USAGE_INDEX_BUFFER_READ:196return RDD::BARRIER_ACCESS_INDEX_READ_BIT;197case RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE:198return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_READ_BIT | RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_WRITE_BIT);199case RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE:200return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | RDD::BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT);201case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_SHADING_RATE_READ:202return RDD::BARRIER_ACCESS_FRAGMENT_SHADING_RATE_ATTACHMENT_READ_BIT;203case RESOURCE_USAGE_ATTACHMENT_FRAGMENT_DENSITY_MAP_READ:204return RDD::BARRIER_ACCESS_FRAGMENT_DENSITY_MAP_ATTACHMENT_READ_BIT;205case RESOURCE_USAGE_GENERAL:206return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_MEMORY_READ_BIT | RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT);207case RESOURCE_USAGE_ACCELERATION_STRUCTURE_READ_WRITE:208return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_ACCELERATION_STRUCTURE_READ_BIT | RDD::BARRIER_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT);209default:210DEV_ASSERT(false && "Invalid usage.");211return RDD::BarrierAccessBits(0);212}213#endif214}215216bool RenderingDeviceGraph::_check_command_intersection(ResourceTracker *p_resource_tracker, int32_t p_previous_command_index, int32_t p_command_index) const {217if (p_resource_tracker->usage != RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE && p_resource_tracker->usage != RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE) {218// We don't check possible intersections for usages that aren't consecutive color or depth writes.219return true;220}221222const uint32_t previous_command_data_offset = command_data_offsets[p_previous_command_index];223const uint32_t current_command_data_offset = command_data_offsets[p_command_index];224const RecordedDrawListCommand &previous_draw_list_command = *reinterpret_cast<const RecordedDrawListCommand *>(&command_data[previous_command_data_offset]);225const RecordedDrawListCommand ¤t_draw_list_command = *reinterpret_cast<const RecordedDrawListCommand *>(&command_data[current_command_data_offset]);226if (previous_draw_list_command.type != RecordedCommand::TYPE_DRAW_LIST || current_draw_list_command.type != RecordedCommand::TYPE_DRAW_LIST) {227// We don't check possible intersections if both commands aren't draw lists.228return true;229}230231// We check if the region used by both draw lists have an intersection.232return previous_draw_list_command.region.intersects(current_draw_list_command.region);233}234235bool RenderingDeviceGraph::_check_command_partial_coverage(ResourceTracker *p_resource_tracker, int32_t p_command_index) const {236if (p_resource_tracker->usage != RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE && p_resource_tracker->usage != RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE) {237// We don't check for partial coverage in usages that aren't attachment writes.238return false;239}240241const uint32_t command_data_offset = command_data_offsets[p_command_index];242const RecordedDrawListCommand &draw_list_command = *reinterpret_cast<const RecordedDrawListCommand *>(&command_data[command_data_offset]);243if (draw_list_command.type != RecordedCommand::TYPE_DRAW_LIST) {244// We don't check for partial coverage on commands that aren't draw lists.245return false;246}247248Rect2i texture_region(Point2i(0, 0), p_resource_tracker->texture_size);249return !draw_list_command.region.encloses(texture_region);250}251252int32_t RenderingDeviceGraph::_add_to_command_list(int32_t p_command_index, int32_t p_list_index) {253DEV_ASSERT(p_command_index < int32_t(command_count));254DEV_ASSERT(p_list_index < int32_t(command_list_nodes.size()));255256int32_t next_index = int32_t(command_list_nodes.size());257command_list_nodes.resize(next_index + 1);258259RecordedCommandListNode &new_node = command_list_nodes[next_index];260new_node.command_index = p_command_index;261new_node.next_list_index = p_list_index;262return next_index;263}264265void RenderingDeviceGraph::_add_adjacent_command(int32_t p_previous_command_index, int32_t p_command_index, RecordedCommand *r_command) {266const uint32_t previous_command_data_offset = command_data_offsets[p_previous_command_index];267RecordedCommand &previous_command = *reinterpret_cast<RecordedCommand *>(&command_data[previous_command_data_offset]);268previous_command.adjacent_command_list_index = _add_to_command_list(p_command_index, previous_command.adjacent_command_list_index);269previous_command.next_stages = previous_command.next_stages | r_command->self_stages;270r_command->previous_stages = r_command->previous_stages | previous_command.self_stages;271}272273int32_t RenderingDeviceGraph::_add_to_slice_read_list(int32_t p_command_index, Rect2i p_subresources, int32_t p_list_index) {274DEV_ASSERT(p_command_index < int32_t(command_count));275DEV_ASSERT(p_list_index < int32_t(read_slice_list_nodes.size()));276277int32_t next_index = int32_t(read_slice_list_nodes.size());278read_slice_list_nodes.resize(next_index + 1);279280RecordedSliceListNode &new_node = read_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;284return next_index;285}286287int32_t RenderingDeviceGraph::_add_to_write_list(int32_t p_command_index, Rect2i p_subresources, int32_t p_list_index, bool p_partial_coverage) {288DEV_ASSERT(p_command_index < int32_t(command_count));289DEV_ASSERT(p_list_index < int32_t(write_slice_list_nodes.size()));290291int32_t next_index = int32_t(write_slice_list_nodes.size());292write_slice_list_nodes.resize(next_index + 1);293294RecordedSliceListNode &new_node = write_slice_list_nodes[next_index];295new_node.command_index = p_command_index;296new_node.next_list_index = p_list_index;297new_node.subresources = p_subresources;298new_node.partial_coverage = p_partial_coverage;299return next_index;300}301302// Ensures all commands are 8-byte aligned.303#define GRAPH_ALIGN(x) (((x) + 7u) & 0xFFFFFFF8u)304305RenderingDeviceGraph::RecordedCommand *RenderingDeviceGraph::_allocate_command(uint32_t p_command_size, int32_t &r_command_index) {306uint32_t command_data_offset = command_data.size();307command_data_offset = GRAPH_ALIGN(command_data_offset);308command_data_offsets.push_back(command_data_offset);309command_data.resize(command_data_offset + p_command_size);310r_command_index = command_count++;311RecordedCommand *new_command = reinterpret_cast<RecordedCommand *>(&command_data[command_data_offset]);312*new_command = RecordedCommand();313return new_command;314}315316RenderingDeviceGraph::DrawListInstruction *RenderingDeviceGraph::_allocate_draw_list_instruction(uint32_t p_instruction_size) {317uint32_t draw_list_data_offset = draw_instruction_list.data.size();318draw_list_data_offset = GRAPH_ALIGN(draw_list_data_offset);319draw_instruction_list.data.resize(draw_list_data_offset + p_instruction_size);320return reinterpret_cast<DrawListInstruction *>(&draw_instruction_list.data[draw_list_data_offset]);321}322323RenderingDeviceGraph::ComputeListInstruction *RenderingDeviceGraph::_allocate_compute_list_instruction(uint32_t p_instruction_size) {324uint32_t compute_list_data_offset = compute_instruction_list.data.size();325compute_list_data_offset = GRAPH_ALIGN(compute_list_data_offset);326compute_instruction_list.data.resize(compute_list_data_offset + p_instruction_size);327return reinterpret_cast<ComputeListInstruction *>(&compute_instruction_list.data[compute_list_data_offset]);328}329330void RenderingDeviceGraph::_check_discardable_attachment_dependency(ResourceTracker *p_resource_tracker, int32_t p_previous_command_index, int32_t p_command_index) {331if (!p_resource_tracker->is_discardable) {332return;333}334335// 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.336uint32_t command_offset = command_data_offsets[p_command_index];337RecordedDrawListCommand *draw_list_command = reinterpret_cast<RecordedDrawListCommand *>(&command_data[command_offset]);338if (draw_list_command->type == RecordedCommand::TYPE_DRAW_LIST) {339ResourceTracker **trackers = draw_list_command->trackers();340for (uint32_t i = 0; i < draw_list_command->trackers_count; i++) {341if (trackers[i] == p_resource_tracker && draw_list_command->load_ops()[i] == RDD::ATTACHMENT_LOAD_OP_CLEAR) {342return;343}344}345}346347// Check if the previous command is a draw list.348uint32_t previous_command_offset = command_data_offsets[p_previous_command_index];349RecordedDrawListCommand *previous_draw_list_command = reinterpret_cast<RecordedDrawListCommand *>(&command_data[previous_command_offset]);350if (previous_draw_list_command->type != RecordedCommand::TYPE_DRAW_LIST) {351return;352}353354// Search for the tracker inside the draw list command and modify the store operation accordingly.355ResourceTracker **trackers = previous_draw_list_command->trackers();356for (uint32_t i = 0; i < previous_draw_list_command->trackers_count; i++) {357if (trackers[i] == p_resource_tracker) {358previous_draw_list_command->store_ops()[i] = RDD::ATTACHMENT_STORE_OP_STORE;359return;360}361}362}363364RenderingDeviceGraph::RaytracingListInstruction *RenderingDeviceGraph::_allocate_raytracing_list_instruction(uint32_t p_instruction_size) {365uint32_t raytracing_list_data_offset = raytracing_instruction_list.data.size();366raytracing_instruction_list.data.resize(raytracing_list_data_offset + p_instruction_size);367return reinterpret_cast<RaytracingListInstruction *>(&raytracing_instruction_list.data[raytracing_list_data_offset]);368}369370void 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) {371// Assign the next stages derived from the stages the command requires first.372r_command->next_stages = r_command->self_stages;373374if (command_label_index >= 0) {375// If a label is active, tag the command with the label.376r_command->label_index = command_label_index;377}378379if (r_command->type == RecordedCommand::TYPE_CAPTURE_TIMESTAMP) {380// All previous commands starting from the previous timestamp should be adjacent to this command.381int32_t start_command_index = uint32_t(MAX(command_timestamp_index, 0));382for (int32_t i = start_command_index; i < p_command_index; i++) {383_add_adjacent_command(i, p_command_index, r_command);384}385386// Make this command the new active timestamp command.387command_timestamp_index = p_command_index;388} else if (command_timestamp_index >= 0) {389// Timestamp command should be adjacent to this command.390_add_adjacent_command(command_timestamp_index, p_command_index, r_command);391}392393if (command_synchronization_pending) {394// All previous commands should be adjacent to this command.395int32_t start_command_index = uint32_t(MAX(command_synchronization_index, 0));396for (int32_t i = start_command_index; i < p_command_index; i++) {397_add_adjacent_command(i, p_command_index, r_command);398}399400command_synchronization_index = p_command_index;401command_synchronization_pending = false;402} else if (command_synchronization_index >= 0) {403// Synchronization command should be adjacent to this command.404_add_adjacent_command(command_synchronization_index, p_command_index, r_command);405}406407for (uint32_t i = 0; i < p_resource_count; i++) {408ResourceTracker *resource_tracker = p_resource_trackers[i];409DEV_ASSERT(resource_tracker != nullptr);410411resource_tracker->reset_if_outdated(tracking_frame);412413const RDD::TextureSubresourceRange &subresources = resource_tracker->texture_subresources;414const Rect2i resource_tracker_rect(subresources.base_mipmap, subresources.base_layer, subresources.mipmap_count, subresources.layer_count);415Rect2i search_tracker_rect = resource_tracker_rect;416417ResourceUsage new_resource_usage = p_resource_usages[i];418bool write_usage = _is_write_usage(new_resource_usage);419BitField<RDD::BarrierAccessBits> new_usage_access = _usage_to_access_bits(new_resource_usage);420bool is_resource_a_slice = resource_tracker->parent != nullptr;421if (is_resource_a_slice) {422// This resource depends on a parent resource.423resource_tracker->parent->reset_if_outdated(tracking_frame);424425if (resource_tracker->texture_slice_command_index != p_command_index) {426// Indicate this slice has been used by this command.427resource_tracker->texture_slice_command_index = p_command_index;428}429430if (resource_tracker->parent->usage == RESOURCE_USAGE_NONE) {431if (resource_tracker->parent->texture_driver_id.id != 0) {432// If the resource is a texture, we transition it entirely to the layout determined by the first slice that uses it.433_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);434}435436// If the parent hasn't been used yet, we assign the usage of the slice to the entire resource.437resource_tracker->parent->usage = new_resource_usage;438439// Also assign the usage to the slice and consider it a write operation. Consider the parent's current usage access as its own.440resource_tracker->usage = new_resource_usage;441resource_tracker->usage_access = resource_tracker->parent->usage_access;442write_usage = true;443444// Indicate the area that should be tracked is the entire resource.445const RDD::TextureSubresourceRange &parent_subresources = resource_tracker->parent->texture_subresources;446search_tracker_rect = Rect2i(parent_subresources.base_mipmap, parent_subresources.base_layer, parent_subresources.mipmap_count, parent_subresources.layer_count);447} else if (resource_tracker->in_parent_dirty_list) {448if (resource_tracker->parent->usage == new_resource_usage) {449// The slice will be transitioned to the resource of the parent and can be deleted from the dirty list.450ResourceTracker *previous_tracker = nullptr;451ResourceTracker *current_tracker = resource_tracker->parent->dirty_shared_list;452bool initialized_dirty_rect = false;453while (current_tracker != nullptr) {454current_tracker->reset_if_outdated(tracking_frame);455456if (current_tracker == resource_tracker) {457current_tracker->in_parent_dirty_list = false;458459if (previous_tracker != nullptr) {460previous_tracker->next_shared = current_tracker->next_shared;461} else {462resource_tracker->parent->dirty_shared_list = current_tracker->next_shared;463}464465current_tracker = current_tracker->next_shared;466} else {467if (initialized_dirty_rect) {468resource_tracker->parent->texture_slice_or_dirty_rect = resource_tracker->parent->texture_slice_or_dirty_rect.merge(current_tracker->texture_slice_or_dirty_rect);469} else {470resource_tracker->parent->texture_slice_or_dirty_rect = current_tracker->texture_slice_or_dirty_rect;471initialized_dirty_rect = true;472}473474previous_tracker = current_tracker;475current_tracker = current_tracker->next_shared;476}477}478}479} else {480if (resource_tracker->parent->dirty_shared_list != nullptr && resource_tracker->parent->texture_slice_or_dirty_rect.intersects(resource_tracker->texture_slice_or_dirty_rect)) {481// There's an intersection with the current dirty area of the parent and the slice. We must verify if the intersection is against a slice482// 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.483ResourceTracker *previous_tracker = nullptr;484ResourceTracker *current_tracker = resource_tracker->parent->dirty_shared_list;485bool initialized_dirty_rect = false;486while (current_tracker != nullptr) {487current_tracker->reset_if_outdated(tracking_frame);488489if (current_tracker->texture_slice_or_dirty_rect.intersects(resource_tracker->texture_slice_or_dirty_rect)) {490if (current_tracker->command_frame == tracking_frame && current_tracker->texture_slice_command_index == p_command_index) {491ERR_FAIL_MSG("Texture slices that overlap can't be used in the same command.");492} else {493// Delete the slice from the dirty list and revert it to the usage of the parent.494if (current_tracker->texture_driver_id.id != 0) {495_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);496497// Merge the area of the slice with the current tracking area of the command and indicate it's a write usage as well.498search_tracker_rect = search_tracker_rect.merge(current_tracker->texture_slice_or_dirty_rect);499write_usage = true;500}501502current_tracker->in_parent_dirty_list = false;503504if (previous_tracker != nullptr) {505previous_tracker->next_shared = current_tracker->next_shared;506} else {507resource_tracker->parent->dirty_shared_list = current_tracker->next_shared;508}509510current_tracker = current_tracker->next_shared;511}512} else {513// Recalculate the dirty rect of the parent so the deleted slices are excluded.514if (initialized_dirty_rect) {515resource_tracker->parent->texture_slice_or_dirty_rect = resource_tracker->parent->texture_slice_or_dirty_rect.merge(current_tracker->texture_slice_or_dirty_rect);516} else {517resource_tracker->parent->texture_slice_or_dirty_rect = current_tracker->texture_slice_or_dirty_rect;518initialized_dirty_rect = true;519}520521previous_tracker = current_tracker;522current_tracker = current_tracker->next_shared;523}524}525}526527// 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.528resource_tracker->usage = resource_tracker->parent->usage;529resource_tracker->usage_access = resource_tracker->parent->usage_access;530531if (resource_tracker->usage != new_resource_usage) {532// Insert to the dirty list if the requested usage is different.533resource_tracker->next_shared = resource_tracker->parent->dirty_shared_list;534resource_tracker->parent->dirty_shared_list = resource_tracker;535resource_tracker->in_parent_dirty_list = true;536if (resource_tracker->parent->dirty_shared_list != nullptr) {537resource_tracker->parent->texture_slice_or_dirty_rect = resource_tracker->parent->texture_slice_or_dirty_rect.merge(resource_tracker->texture_slice_or_dirty_rect);538} else {539resource_tracker->parent->texture_slice_or_dirty_rect = resource_tracker->texture_slice_or_dirty_rect;540}541}542}543} else {544ResourceTracker *current_tracker = resource_tracker->dirty_shared_list;545if (current_tracker != nullptr) {546// Consider the usage as write if we must transition any of the slices.547write_usage = true;548}549550while (current_tracker != nullptr) {551current_tracker->reset_if_outdated(tracking_frame);552553if (current_tracker->texture_driver_id.id != 0) {554// Transition all slices to the layout of the parent resource.555_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);556}557558current_tracker->in_parent_dirty_list = false;559current_tracker = current_tracker->next_shared;560}561562resource_tracker->dirty_shared_list = nullptr;563}564565// Use the resource's parent tracker directly for all search operations.566bool resource_has_parent = resource_tracker->parent != nullptr;567ResourceTracker *search_tracker = resource_has_parent ? resource_tracker->parent : resource_tracker;568bool different_usage = resource_tracker->usage != new_resource_usage;569bool write_usage_after_write = (write_usage && search_tracker->write_command_or_list_index >= 0);570if (different_usage || write_usage_after_write) {571// 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.572if (resource_tracker->texture_driver_id.id != 0) {573if (resource_tracker->usage_access.is_empty()) {574// FIXME: If the tracker does not know the previous type of usage, assume the generic memory write one.575// Tracking access bits across texture slices can be tricky, so this failsafe can be removed once that's improved.576resource_tracker->usage_access = RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT;577}578579_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);580} else if (resource_tracker->buffer_driver_id.id != 0) {581#if USE_BUFFER_BARRIERS582_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);583#endif584// Memory barriers are pushed regardless of buffer barriers being used or not.585r_command->memory_barrier.src_access = r_command->memory_barrier.src_access | resource_tracker->usage_access;586r_command->memory_barrier.dst_access = r_command->memory_barrier.dst_access | new_usage_access;587} else if (resource_tracker->acceleration_structure_driver_id.id != 0) {588// Make sure the acceleration structure has been built before accessing it from raytracing shaders.589_add_acceleration_structure_barrier_to_command(resource_tracker->acceleration_structure_driver_id, resource_tracker->usage_access, new_usage_access, command_acceleration_structure_barriers, r_command->acceleration_structure_barrier_index, r_command->acceleration_structure_barrier_count);590r_command->memory_barrier.src_access = r_command->memory_barrier.src_access | resource_tracker->usage_access;591r_command->memory_barrier.dst_access = r_command->memory_barrier.dst_access | new_usage_access;592} else {593DEV_ASSERT(false && "Resource tracker does not contain a valid buffer or texture ID.");594}595}596597// Always update the access of the tracker according to the latest usage.598resource_tracker->usage_access = new_usage_access;599600// Always accumulate the stages of the tracker with the commands that use it.601search_tracker->current_frame_stages = search_tracker->current_frame_stages | r_command->self_stages;602603if (!search_tracker->previous_frame_stages.is_empty()) {604// Add to the command the stages the tracker was used on in the previous frame.605r_command->previous_stages = r_command->previous_stages | search_tracker->previous_frame_stages;606search_tracker->previous_frame_stages.clear();607}608609if (different_usage) {610// 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.611// In the case of buffers however, this is not exactly necessary if the driver does not consider different buffer usages as different states.612write_usage = write_usage || bool(resource_tracker->texture_driver_id) || driver_buffers_require_transitions;613resource_tracker->usage = new_resource_usage;614}615616bool write_usage_has_partial_coverage = !different_usage && _check_command_partial_coverage(resource_tracker, p_command_index);617if (search_tracker->write_command_or_list_index >= 0) {618if (search_tracker->write_command_list_enabled) {619// Make this command adjacent to any commands that wrote to this resource and intersect with the slice if it applies.620// For buffers or textures that never use slices, this list will only be one element long at most.621int32_t previous_write_list_index = -1;622int32_t write_list_index = search_tracker->write_command_or_list_index;623while (write_list_index >= 0) {624const RecordedSliceListNode &write_list_node = write_slice_list_nodes[write_list_index];625if (!resource_has_parent || search_tracker_rect.intersects(write_list_node.subresources)) {626if (write_list_node.command_index == p_command_index) {627ERR_FAIL_COND_MSG(!resource_has_parent, "Command can't have itself as a dependency.");628} else if (!write_list_node.partial_coverage || _check_command_intersection(resource_tracker, write_list_node.command_index, p_command_index)) {629_check_discardable_attachment_dependency(search_tracker, write_list_node.command_index, p_command_index);630631// Command is dependent on this command. Add this command to the adjacency list of the write command.632_add_adjacent_command(write_list_node.command_index, p_command_index, r_command);633634if (resource_has_parent && write_usage && search_tracker_rect.encloses(write_list_node.subresources) && !write_usage_has_partial_coverage) {635// Eliminate redundant writes from the list.636if (previous_write_list_index >= 0) {637RecordedSliceListNode &previous_list_node = write_slice_list_nodes[previous_write_list_index];638previous_list_node.next_list_index = write_list_node.next_list_index;639} else {640search_tracker->write_command_or_list_index = write_list_node.next_list_index;641}642643write_list_index = write_list_node.next_list_index;644continue;645}646}647}648649previous_write_list_index = write_list_index;650write_list_index = write_list_node.next_list_index;651}652} else {653// The index is just the latest command index that wrote to the resource.654if (search_tracker->write_command_or_list_index == p_command_index) {655ERR_FAIL_MSG("Command can't have itself as a dependency.");656} else {657_check_discardable_attachment_dependency(search_tracker, search_tracker->write_command_or_list_index, p_command_index);658_add_adjacent_command(search_tracker->write_command_or_list_index, p_command_index, r_command);659}660}661}662663if (write_usage) {664bool use_write_list = resource_has_parent || write_usage_has_partial_coverage;665if (use_write_list) {666if (!search_tracker->write_command_list_enabled && search_tracker->write_command_or_list_index >= 0) {667// 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.668const RDD::TextureSubresourceRange &tracker_subresources = search_tracker->texture_subresources;669Rect2i tracker_rect(tracker_subresources.base_mipmap, tracker_subresources.base_layer, tracker_subresources.mipmap_count, tracker_subresources.layer_count);670search_tracker->write_command_or_list_index = _add_to_write_list(search_tracker->write_command_or_list_index, tracker_rect, -1, false);671}672673search_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);674search_tracker->write_command_list_enabled = true;675} else {676search_tracker->write_command_or_list_index = p_command_index;677search_tracker->write_command_list_enabled = false;678}679680// We add this command to the adjacency list of all commands that were reading from the entire resource.681int32_t read_full_command_list_index = search_tracker->read_full_command_list_index;682while (read_full_command_list_index >= 0) {683int32_t read_full_command_index = command_list_nodes[read_full_command_list_index].command_index;684int32_t read_full_next_index = command_list_nodes[read_full_command_list_index].next_list_index;685if (read_full_command_index == p_command_index) {686if (!resource_has_parent) {687// 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.688ERR_FAIL_MSG("Command can't have itself as a dependency.");689}690} else {691// Add this command to the adjacency list of each command that was reading this resource.692_add_adjacent_command(read_full_command_index, p_command_index, r_command);693}694695read_full_command_list_index = read_full_next_index;696}697698if (!use_write_list) {699// Clear the full list if this resource is not a slice.700search_tracker->read_full_command_list_index = -1;701}702703// We add this command to the adjacency list of all commands that were reading from resource slices.704int32_t previous_slice_command_list_index = -1;705int32_t read_slice_command_list_index = search_tracker->read_slice_command_list_index;706while (read_slice_command_list_index >= 0) {707const RecordedSliceListNode &read_list_node = read_slice_list_nodes[read_slice_command_list_index];708if (!use_write_list || search_tracker_rect.encloses(read_list_node.subresources)) {709if (previous_slice_command_list_index >= 0) {710// Erase this element and connect the previous one to the next element.711read_slice_list_nodes[previous_slice_command_list_index].next_list_index = read_list_node.next_list_index;712} else {713// Erase this element from the head of the list.714DEV_ASSERT(search_tracker->read_slice_command_list_index == read_slice_command_list_index);715search_tracker->read_slice_command_list_index = read_list_node.next_list_index;716}717718// Advance to the next element.719read_slice_command_list_index = read_list_node.next_list_index;720} else {721previous_slice_command_list_index = read_slice_command_list_index;722read_slice_command_list_index = read_list_node.next_list_index;723}724725if (!resource_has_parent || search_tracker_rect.intersects(read_list_node.subresources)) {726// Add this command to the adjacency list of each command that was reading this resource.727// We only add the dependency if there's an intersection between slices or this resource isn't a slice.728_add_adjacent_command(read_list_node.command_index, p_command_index, r_command);729}730}731} else if (resource_has_parent) {732// We add a read dependency to the tracker to indicate this command reads from the resource slice.733search_tracker->read_slice_command_list_index = _add_to_slice_read_list(p_command_index, resource_tracker_rect, search_tracker->read_slice_command_list_index);734} else {735// We add a read dependency to the tracker to indicate this command reads from the entire resource.736search_tracker->read_full_command_list_index = _add_to_command_list(p_command_index, search_tracker->read_full_command_list_index);737}738}739}740741void 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) {742if (!driver_honors_barriers) {743return;744}745746if (r_barrier_index < 0) {747r_barrier_index = r_barrier_vector.size();748}749750RDD::TextureBarrier texture_barrier;751texture_barrier.texture = p_texture_id;752texture_barrier.src_access = p_src_access;753texture_barrier.dst_access = p_dst_access;754texture_barrier.prev_layout = _usage_to_image_layout(p_prev_usage);755texture_barrier.next_layout = _usage_to_image_layout(p_next_usage);756texture_barrier.subresources = p_subresources;757r_barrier_vector.push_back(texture_barrier);758r_barrier_count++;759}760761#if USE_BUFFER_BARRIERS762void 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) {763if (!driver_honors_barriers) {764return;765}766767if (r_barrier_index < 0) {768r_barrier_index = command_buffer_barriers.size();769}770771RDD::BufferBarrier buffer_barrier;772buffer_barrier.buffer = p_buffer_id;773buffer_barrier.src_access = p_src_access;774buffer_barrier.dst_access = p_dst_access;775buffer_barrier.offset = 0;776buffer_barrier.size = RDD::BUFFER_WHOLE_SIZE;777command_buffer_barriers.push_back(buffer_barrier);778r_barrier_count++;779}780#endif781782void RenderingDeviceGraph::_add_acceleration_structure_barrier_to_command(RDD::AccelerationStructureID p_acceleration_structure_id, BitField<RDD::BarrierAccessBits> p_src_access, BitField<RDD::BarrierAccessBits> p_dst_access, LocalVector<RDD::AccelerationStructureBarrier> &r_barrier_vector, int32_t &r_barrier_index, int32_t &r_barrier_count) {783if (!driver_honors_barriers) {784return;785}786787if (r_barrier_index < 0) {788r_barrier_index = r_barrier_vector.size();789}790791RDD::AccelerationStructureBarrier accel_barrier;792accel_barrier.acceleration_structure = p_acceleration_structure_id;793accel_barrier.src_access = p_src_access;794accel_barrier.dst_access = p_dst_access;795accel_barrier.offset = 0;796accel_barrier.size = RDD::BUFFER_WHOLE_SIZE;797r_barrier_vector.push_back(accel_barrier);798r_barrier_count++;799}800801void RenderingDeviceGraph::_run_raytracing_list_command(RDD::CommandBufferID p_command_buffer, const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {802uint32_t instruction_data_cursor = 0;803while (instruction_data_cursor < p_instruction_data_size) {804DEV_ASSERT((instruction_data_cursor + sizeof(RaytracingListInstruction)) <= p_instruction_data_size);805806const RaytracingListInstruction *instruction = reinterpret_cast<const RaytracingListInstruction *>(&p_instruction_data[instruction_data_cursor]);807switch (instruction->type) {808case RaytracingListInstruction::TYPE_BIND_PIPELINE: {809const RaytracingListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const RaytracingListBindPipelineInstruction *>(instruction);810driver->command_bind_raytracing_pipeline(p_command_buffer, bind_pipeline_instruction->pipeline);811instruction_data_cursor += sizeof(RaytracingListBindPipelineInstruction);812} break;813case RaytracingListInstruction::TYPE_BIND_UNIFORM_SET: {814const RaytracingListBindUniformSetInstruction *bind_uniform_set_instruction = reinterpret_cast<const RaytracingListBindUniformSetInstruction *>(instruction);815driver->command_bind_raytracing_uniform_set(p_command_buffer, bind_uniform_set_instruction->uniform_set, bind_uniform_set_instruction->shader, bind_uniform_set_instruction->set_index);816instruction_data_cursor += sizeof(RaytracingListBindUniformSetInstruction);817} break;818case RaytracingListInstruction::TYPE_TRACE_RAYS: {819const RaytracingListTraceRaysInstruction *trace_rays_instruction = reinterpret_cast<const RaytracingListTraceRaysInstruction *>(instruction);820driver->command_trace_rays(p_command_buffer, trace_rays_instruction->width, trace_rays_instruction->height);821instruction_data_cursor += sizeof(RaytracingListTraceRaysInstruction);822} break;823case RaytracingListInstruction::TYPE_SET_PUSH_CONSTANT: {824const RaytracingListSetPushConstantInstruction *set_push_constant_instruction = reinterpret_cast<const RaytracingListSetPushConstantInstruction *>(instruction);825const VectorView push_constant_data_view(reinterpret_cast<const uint32_t *>(set_push_constant_instruction->data()), set_push_constant_instruction->size / sizeof(uint32_t));826driver->command_bind_push_constants(p_command_buffer, set_push_constant_instruction->shader, 0, push_constant_data_view);827instruction_data_cursor += sizeof(RaytracingListSetPushConstantInstruction);828instruction_data_cursor += set_push_constant_instruction->size;829} break;830case RaytracingListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE: {831const RaytracingListUniformSetPrepareForUseInstruction *uniform_set_prepare_for_use_instruction = reinterpret_cast<const RaytracingListUniformSetPrepareForUseInstruction *>(instruction);832driver->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);833instruction_data_cursor += sizeof(RaytracingListUniformSetPrepareForUseInstruction);834} break;835default:836DEV_ASSERT(false && "Unknown raytracing list instruction type.");837return;838}839}840}841842void RenderingDeviceGraph::_run_compute_list_command(RDD::CommandBufferID p_command_buffer, const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {843uint32_t instruction_data_cursor = 0;844while (instruction_data_cursor < p_instruction_data_size) {845DEV_ASSERT((instruction_data_cursor + sizeof(ComputeListInstruction)) <= p_instruction_data_size);846847const ComputeListInstruction *instruction = reinterpret_cast<const ComputeListInstruction *>(&p_instruction_data[instruction_data_cursor]);848switch (instruction->type) {849case ComputeListInstruction::TYPE_BIND_PIPELINE: {850const ComputeListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const ComputeListBindPipelineInstruction *>(instruction);851driver->command_bind_compute_pipeline(p_command_buffer, bind_pipeline_instruction->pipeline);852instruction_data_cursor += sizeof(ComputeListBindPipelineInstruction);853} break;854case ComputeListInstruction::TYPE_BIND_UNIFORM_SETS: {855const ComputeListBindUniformSetsInstruction *bind_uniform_sets_instruction = reinterpret_cast<const ComputeListBindUniformSetsInstruction *>(instruction);856driver->command_bind_compute_uniform_sets(p_command_buffer, VectorView<RDD::UniformSetID>(bind_uniform_sets_instruction->uniform_set_ids(), bind_uniform_sets_instruction->set_count), bind_uniform_sets_instruction->shader, bind_uniform_sets_instruction->first_set_index, bind_uniform_sets_instruction->set_count, bind_uniform_sets_instruction->dynamic_offsets_mask);857instruction_data_cursor += sizeof(ComputeListBindUniformSetsInstruction) + sizeof(RDD::UniformSetID) * bind_uniform_sets_instruction->set_count;858} break;859case ComputeListInstruction::TYPE_DISPATCH: {860const ComputeListDispatchInstruction *dispatch_instruction = reinterpret_cast<const ComputeListDispatchInstruction *>(instruction);861driver->command_compute_dispatch(p_command_buffer, dispatch_instruction->x_groups, dispatch_instruction->y_groups, dispatch_instruction->z_groups);862instruction_data_cursor += sizeof(ComputeListDispatchInstruction);863} break;864case ComputeListInstruction::TYPE_DISPATCH_INDIRECT: {865const ComputeListDispatchIndirectInstruction *dispatch_indirect_instruction = reinterpret_cast<const ComputeListDispatchIndirectInstruction *>(instruction);866driver->command_compute_dispatch_indirect(p_command_buffer, dispatch_indirect_instruction->buffer, dispatch_indirect_instruction->offset);867instruction_data_cursor += sizeof(ComputeListDispatchIndirectInstruction);868} break;869case ComputeListInstruction::TYPE_SET_PUSH_CONSTANT: {870const ComputeListSetPushConstantInstruction *set_push_constant_instruction = reinterpret_cast<const ComputeListSetPushConstantInstruction *>(instruction);871const VectorView push_constant_data_view(reinterpret_cast<const uint32_t *>(set_push_constant_instruction->data()), set_push_constant_instruction->size / sizeof(uint32_t));872driver->command_bind_push_constants(p_command_buffer, set_push_constant_instruction->shader, 0, push_constant_data_view);873instruction_data_cursor += sizeof(ComputeListSetPushConstantInstruction);874instruction_data_cursor += set_push_constant_instruction->size;875} break;876case ComputeListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE: {877const ComputeListUniformSetPrepareForUseInstruction *uniform_set_prepare_for_use_instruction = reinterpret_cast<const ComputeListUniformSetPrepareForUseInstruction *>(instruction);878driver->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);879instruction_data_cursor += sizeof(ComputeListUniformSetPrepareForUseInstruction);880} break;881default:882DEV_ASSERT(false && "Unknown compute list instruction type.");883return;884}885886instruction_data_cursor = GRAPH_ALIGN(instruction_data_cursor);887}888}889890void RenderingDeviceGraph::_get_draw_list_render_pass_and_framebuffer(const RecordedDrawListCommand *p_draw_list_command, RDD::RenderPassID &r_render_pass, RDD::FramebufferID &r_framebuffer) {891DEV_ASSERT(p_draw_list_command->trackers_count <= 21 && "Max number of attachments that can be encoded into the key.");892893// Build a unique key from the load and store ops for each attachment.894const RDD::AttachmentLoadOp *load_ops = p_draw_list_command->load_ops();895const RDD::AttachmentStoreOp *store_ops = p_draw_list_command->store_ops();896uint64_t key = 0;897for (uint32_t i = 0; i < p_draw_list_command->trackers_count; i++) {898key |= uint64_t(load_ops[i]) << (i * 3);899key |= uint64_t(store_ops[i]) << (i * 3 + 2);900}901902// Check the storage map if the render pass and the framebuffer needs to be created.903FramebufferCache *framebuffer_cache = p_draw_list_command->framebuffer_cache;904HashMap<uint64_t, FramebufferStorage>::Iterator it = framebuffer_cache->storage_map.find(key);905if (it == framebuffer_cache->storage_map.end()) {906FramebufferStorage storage;907VectorView<RDD::AttachmentLoadOp> load_ops_view(load_ops, p_draw_list_command->trackers_count);908VectorView<RDD::AttachmentStoreOp> store_ops_view(store_ops, p_draw_list_command->trackers_count);909storage.render_pass = render_pass_creation_function(driver, load_ops_view, store_ops_view, framebuffer_cache->render_pass_creation_user_data);910ERR_FAIL_COND(!storage.render_pass);911912storage.framebuffer = driver->framebuffer_create(storage.render_pass, framebuffer_cache->textures, framebuffer_cache->width, framebuffer_cache->height);913ERR_FAIL_COND(!storage.framebuffer);914915it = framebuffer_cache->storage_map.insert(key, storage);916}917918r_render_pass = it->value.render_pass;919r_framebuffer = it->value.framebuffer;920}921922#if PRINT_DRAW_LIST_STATS923static uint32_t draw_list_total_size = 0;924#endif925926void RenderingDeviceGraph::_run_draw_list_command(RDD::CommandBufferID p_command_buffer, const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {927#if PRINT_DRAW_LIST_STATS928draw_list_total_size += p_instruction_data_size;929#endif930931uint32_t instruction_data_cursor = 0;932while (instruction_data_cursor < p_instruction_data_size) {933DEV_ASSERT((instruction_data_cursor + sizeof(DrawListInstruction)) <= p_instruction_data_size);934935const DrawListInstruction *instruction = reinterpret_cast<const DrawListInstruction *>(&p_instruction_data[instruction_data_cursor]);936switch (instruction->type) {937case DrawListInstruction::TYPE_BIND_INDEX_BUFFER: {938const DrawListBindIndexBufferInstruction *bind_index_buffer_instruction = reinterpret_cast<const DrawListBindIndexBufferInstruction *>(instruction);939driver->command_render_bind_index_buffer(p_command_buffer, bind_index_buffer_instruction->buffer, bind_index_buffer_instruction->format, bind_index_buffer_instruction->offset);940instruction_data_cursor += sizeof(DrawListBindIndexBufferInstruction);941} break;942case DrawListInstruction::TYPE_BIND_PIPELINE: {943const DrawListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const DrawListBindPipelineInstruction *>(instruction);944driver->command_bind_render_pipeline(p_command_buffer, bind_pipeline_instruction->pipeline);945instruction_data_cursor += sizeof(DrawListBindPipelineInstruction);946} break;947case DrawListInstruction::TYPE_BIND_UNIFORM_SETS: {948const DrawListBindUniformSetsInstruction *bind_uniform_sets_instruction = reinterpret_cast<const DrawListBindUniformSetsInstruction *>(instruction);949driver->command_bind_render_uniform_sets(p_command_buffer, VectorView<RDD::UniformSetID>(bind_uniform_sets_instruction->uniform_set_ids(), bind_uniform_sets_instruction->set_count), bind_uniform_sets_instruction->shader, bind_uniform_sets_instruction->first_set_index, bind_uniform_sets_instruction->set_count, bind_uniform_sets_instruction->dynamic_offsets_mask);950instruction_data_cursor += sizeof(DrawListBindUniformSetsInstruction) + sizeof(RDD::UniformSetID) * bind_uniform_sets_instruction->set_count;951} break;952case DrawListInstruction::TYPE_BIND_VERTEX_BUFFERS: {953const DrawListBindVertexBuffersInstruction *bind_vertex_buffers_instruction = reinterpret_cast<const DrawListBindVertexBuffersInstruction *>(instruction);954driver->command_render_bind_vertex_buffers(p_command_buffer, bind_vertex_buffers_instruction->vertex_buffers_count, bind_vertex_buffers_instruction->vertex_buffers(), bind_vertex_buffers_instruction->vertex_buffer_offsets(), bind_vertex_buffers_instruction->dynamic_offsets_mask);955instruction_data_cursor += sizeof(DrawListBindVertexBuffersInstruction);956instruction_data_cursor += sizeof(RDD::BufferID) * bind_vertex_buffers_instruction->vertex_buffers_count;957instruction_data_cursor += sizeof(uint64_t) * bind_vertex_buffers_instruction->vertex_buffers_count;958} break;959case DrawListInstruction::TYPE_CLEAR_ATTACHMENTS: {960const DrawListClearAttachmentsInstruction *clear_attachments_instruction = reinterpret_cast<const DrawListClearAttachmentsInstruction *>(instruction);961const VectorView attachments_clear_view(clear_attachments_instruction->attachments_clear(), clear_attachments_instruction->attachments_clear_count);962const VectorView attachments_clear_rect_view(clear_attachments_instruction->attachments_clear_rect(), clear_attachments_instruction->attachments_clear_rect_count);963driver->command_render_clear_attachments(p_command_buffer, attachments_clear_view, attachments_clear_rect_view);964instruction_data_cursor += sizeof(DrawListClearAttachmentsInstruction);965instruction_data_cursor += sizeof(RDD::AttachmentClear) * clear_attachments_instruction->attachments_clear_count;966instruction_data_cursor += sizeof(Rect2i) * clear_attachments_instruction->attachments_clear_rect_count;967} break;968case DrawListInstruction::TYPE_DRAW: {969const DrawListDrawInstruction *draw_instruction = reinterpret_cast<const DrawListDrawInstruction *>(instruction);970driver->command_render_draw(p_command_buffer, draw_instruction->vertex_count, draw_instruction->instance_count, 0, 0);971instruction_data_cursor += sizeof(DrawListDrawInstruction);972} break;973case DrawListInstruction::TYPE_DRAW_INDEXED: {974const DrawListDrawIndexedInstruction *draw_indexed_instruction = reinterpret_cast<const DrawListDrawIndexedInstruction *>(instruction);975driver->command_render_draw_indexed(p_command_buffer, draw_indexed_instruction->index_count, draw_indexed_instruction->instance_count, draw_indexed_instruction->first_index, 0, 0);976instruction_data_cursor += sizeof(DrawListDrawIndexedInstruction);977} break;978case DrawListInstruction::TYPE_DRAW_INDIRECT: {979const DrawListDrawIndirectInstruction *draw_indirect_instruction = reinterpret_cast<const DrawListDrawIndirectInstruction *>(instruction);980driver->command_render_draw_indirect(p_command_buffer, draw_indirect_instruction->buffer, draw_indirect_instruction->offset, draw_indirect_instruction->draw_count, draw_indirect_instruction->stride);981instruction_data_cursor += sizeof(DrawListDrawIndirectInstruction);982} break;983case DrawListInstruction::TYPE_DRAW_INDEXED_INDIRECT: {984const DrawListDrawIndexedIndirectInstruction *draw_indexed_indirect_instruction = reinterpret_cast<const DrawListDrawIndexedIndirectInstruction *>(instruction);985driver->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);986instruction_data_cursor += sizeof(DrawListDrawIndexedIndirectInstruction);987} break;988case DrawListInstruction::TYPE_EXECUTE_COMMANDS: {989const DrawListExecuteCommandsInstruction *execute_commands_instruction = reinterpret_cast<const DrawListExecuteCommandsInstruction *>(instruction);990driver->command_buffer_execute_secondary(p_command_buffer, execute_commands_instruction->command_buffer);991instruction_data_cursor += sizeof(DrawListExecuteCommandsInstruction);992} break;993case DrawListInstruction::TYPE_NEXT_SUBPASS: {994const DrawListNextSubpassInstruction *next_subpass_instruction = reinterpret_cast<const DrawListNextSubpassInstruction *>(instruction);995driver->command_next_render_subpass(p_command_buffer, next_subpass_instruction->command_buffer_type);996instruction_data_cursor += sizeof(DrawListNextSubpassInstruction);997} break;998case DrawListInstruction::TYPE_SET_BLEND_CONSTANTS: {999const DrawListSetBlendConstantsInstruction *set_blend_constants_instruction = reinterpret_cast<const DrawListSetBlendConstantsInstruction *>(instruction);1000driver->command_render_set_blend_constants(p_command_buffer, set_blend_constants_instruction->color);1001instruction_data_cursor += sizeof(DrawListSetBlendConstantsInstruction);1002} break;1003case DrawListInstruction::TYPE_SET_LINE_WIDTH: {1004const DrawListSetLineWidthInstruction *set_line_width_instruction = reinterpret_cast<const DrawListSetLineWidthInstruction *>(instruction);1005driver->command_render_set_line_width(p_command_buffer, set_line_width_instruction->width);1006instruction_data_cursor += sizeof(DrawListSetLineWidthInstruction);1007} break;1008case DrawListInstruction::TYPE_SET_PUSH_CONSTANT: {1009const DrawListSetPushConstantInstruction *set_push_constant_instruction = reinterpret_cast<const DrawListSetPushConstantInstruction *>(instruction);1010const VectorView push_constant_data_view(reinterpret_cast<const uint32_t *>(set_push_constant_instruction->data()), set_push_constant_instruction->size / sizeof(uint32_t));1011driver->command_bind_push_constants(p_command_buffer, set_push_constant_instruction->shader, 0, push_constant_data_view);1012instruction_data_cursor += sizeof(DrawListSetPushConstantInstruction);1013instruction_data_cursor += set_push_constant_instruction->size;1014} break;1015case DrawListInstruction::TYPE_SET_SCISSOR: {1016const DrawListSetScissorInstruction *set_scissor_instruction = reinterpret_cast<const DrawListSetScissorInstruction *>(instruction);1017driver->command_render_set_scissor(p_command_buffer, set_scissor_instruction->rect);1018instruction_data_cursor += sizeof(DrawListSetScissorInstruction);1019} break;1020case DrawListInstruction::TYPE_SET_VIEWPORT: {1021const DrawListSetViewportInstruction *set_viewport_instruction = reinterpret_cast<const DrawListSetViewportInstruction *>(instruction);1022driver->command_render_set_viewport(p_command_buffer, set_viewport_instruction->rect);1023instruction_data_cursor += sizeof(DrawListSetViewportInstruction);1024} break;1025case DrawListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE: {1026const DrawListUniformSetPrepareForUseInstruction *uniform_set_prepare_for_use_instruction = reinterpret_cast<const DrawListUniformSetPrepareForUseInstruction *>(instruction);1027driver->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);1028instruction_data_cursor += sizeof(DrawListUniformSetPrepareForUseInstruction);1029} break;1030default:1031DEV_ASSERT(false && "Unknown draw list instruction type.");1032return;1033}10341035instruction_data_cursor = GRAPH_ALIGN(instruction_data_cursor);1036}1037}10381039void 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) {1040DEV_ASSERT(p_attachment_operations.size() == p_attachment_clear_values.size());10411042draw_instruction_list.clear();1043draw_instruction_list.index++;1044draw_instruction_list.framebuffer_cache = p_framebuffer_cache;1045draw_instruction_list.render_pass = p_render_pass;1046draw_instruction_list.framebuffer = p_framebuffer;1047draw_instruction_list.region = p_region;1048draw_instruction_list.stages = p_stages;1049draw_instruction_list.attachment_operations.resize(p_attachment_operations.size());1050draw_instruction_list.attachment_clear_values.resize(p_attachment_clear_values.size());10511052for (uint32_t i = 0; i < p_attachment_operations.size(); i++) {1053draw_instruction_list.attachment_operations[i] = p_attachment_operations[i];1054draw_instruction_list.attachment_clear_values[i] = p_attachment_clear_values[i];1055}10561057draw_instruction_list.split_cmd_buffer = p_split_cmd_buffer;10581059#if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)1060draw_instruction_list.breadcrumb = p_breadcrumb;1061#endif1062}10631064void RenderingDeviceGraph::_run_secondary_command_buffer_task(const SecondaryCommandBuffer *p_secondary) {1065driver->command_buffer_begin_secondary(p_secondary->command_buffer, p_secondary->render_pass, 0, p_secondary->framebuffer);1066_run_draw_list_command(p_secondary->command_buffer, p_secondary->instruction_data.ptr(), p_secondary->instruction_data.size());1067driver->command_buffer_end(p_secondary->command_buffer);1068}10691070void RenderingDeviceGraph::_wait_for_secondary_command_buffer_tasks() {1071for (uint32_t i = 0; i < frames[frame].secondary_command_buffers_used; i++) {1072WorkerThreadPool::TaskID &task = frames[frame].secondary_command_buffers[i].task;1073if (task != WorkerThreadPool::INVALID_TASK_ID) {1074WorkerThreadPool::get_singleton()->wait_for_task_completion(task);1075task = WorkerThreadPool::INVALID_TASK_ID;1076}1077}1078}10791080void 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) {1081for (uint32_t i = 0; i < p_sorted_commands_count; i++) {1082const uint32_t command_index = p_sorted_commands[i].index;1083const uint32_t command_data_offset = command_data_offsets[command_index];1084const RecordedCommand *command = reinterpret_cast<const RecordedCommand *>(&command_data[command_data_offset]);1085_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);10861087switch (command->type) {1088case RecordedCommand::TYPE_ACCELERATION_STRUCTURE_BUILD: {1089const RecordedAccelerationStructureBuildCommand *as_build_command = reinterpret_cast<const RecordedAccelerationStructureBuildCommand *>(command);1090driver->command_build_acceleration_structure(r_command_buffer, as_build_command->acceleration_structure, as_build_command->scratch_buffer);1091} break;1092case RecordedCommand::TYPE_BUFFER_CLEAR: {1093const RecordedBufferClearCommand *buffer_clear_command = reinterpret_cast<const RecordedBufferClearCommand *>(command);1094driver->command_clear_buffer(r_command_buffer, buffer_clear_command->buffer, buffer_clear_command->offset, buffer_clear_command->size);1095} break;1096case RecordedCommand::TYPE_BUFFER_COPY: {1097const RecordedBufferCopyCommand *buffer_copy_command = reinterpret_cast<const RecordedBufferCopyCommand *>(command);1098driver->command_copy_buffer(r_command_buffer, buffer_copy_command->source, buffer_copy_command->destination, buffer_copy_command->region);1099} break;1100case RecordedCommand::TYPE_BUFFER_GET_DATA: {1101const RecordedBufferGetDataCommand *buffer_get_data_command = reinterpret_cast<const RecordedBufferGetDataCommand *>(command);1102driver->command_copy_buffer(r_command_buffer, buffer_get_data_command->source, buffer_get_data_command->destination, buffer_get_data_command->region);1103} break;1104case RecordedCommand::TYPE_BUFFER_UPDATE: {1105const RecordedBufferUpdateCommand *buffer_update_command = reinterpret_cast<const RecordedBufferUpdateCommand *>(command);1106const RecordedBufferCopy *command_buffer_copies = buffer_update_command->buffer_copies();1107for (uint32_t j = 0; j < buffer_update_command->buffer_copies_count; j++) {1108driver->command_copy_buffer(r_command_buffer, command_buffer_copies[j].source, buffer_update_command->destination, command_buffer_copies[j].region);1109}1110} break;1111case RecordedCommand::TYPE_DRIVER_CALLBACK: {1112const RecordedDriverCallbackCommand *driver_callback_command = reinterpret_cast<const RecordedDriverCallbackCommand *>(command);1113driver_callback_command->callback(driver, r_command_buffer, driver_callback_command->userdata);1114} break;1115case RecordedCommand::TYPE_RAYTRACING_LIST: {1116const RecordedRaytracingListCommand *raytracing_list_command = reinterpret_cast<const RecordedRaytracingListCommand *>(command);1117_run_raytracing_list_command(r_command_buffer, raytracing_list_command->instruction_data(), raytracing_list_command->instruction_data_size);1118} break;1119case RecordedCommand::TYPE_COMPUTE_LIST: {1120if (device.workarounds.avoid_compute_after_draw && workarounds_state.draw_list_found) {1121// Avoid compute after draw workaround. Refer to the comment that enables this in the Vulkan driver for more information.1122workarounds_state.draw_list_found = false;11231124// Create or reuse a command buffer and finish recording the current one.1125driver->command_buffer_end(r_command_buffer);11261127while (r_command_buffer_pool.buffers_used >= r_command_buffer_pool.buffers.size()) {1128RDD::CommandBufferID command_buffer = driver->command_buffer_create(r_command_buffer_pool.pool);1129RDD::SemaphoreID command_semaphore = driver->semaphore_create();1130r_command_buffer_pool.buffers.push_back(command_buffer);1131r_command_buffer_pool.semaphores.push_back(command_semaphore);1132}11331134// Start recording on the next usable command buffer from the pool.1135uint32_t command_buffer_index = r_command_buffer_pool.buffers_used++;1136r_command_buffer = r_command_buffer_pool.buffers[command_buffer_index];1137driver->command_buffer_begin(r_command_buffer);1138}11391140const RecordedComputeListCommand *compute_list_command = reinterpret_cast<const RecordedComputeListCommand *>(command);1141_run_compute_list_command(r_command_buffer, compute_list_command->instruction_data(), compute_list_command->instruction_data_size);1142} break;1143case RecordedCommand::TYPE_DRAW_LIST: {1144if (device.workarounds.avoid_compute_after_draw) {1145// Indicate that a draw list was encountered for the workaround.1146workarounds_state.draw_list_found = true;1147}11481149const RecordedDrawListCommand *draw_list_command = reinterpret_cast<const RecordedDrawListCommand *>(command);11501151if (draw_list_command->split_cmd_buffer) {1152// Create or reuse a command buffer and finish recording the current one.1153driver->command_buffer_end(r_command_buffer);11541155while (r_command_buffer_pool.buffers_used >= r_command_buffer_pool.buffers.size()) {1156RDD::CommandBufferID command_buffer = driver->command_buffer_create(r_command_buffer_pool.pool);1157RDD::SemaphoreID command_semaphore = driver->semaphore_create();1158r_command_buffer_pool.buffers.push_back(command_buffer);1159r_command_buffer_pool.semaphores.push_back(command_semaphore);1160}11611162// Start recording on the next usable command buffer from the pool.1163uint32_t command_buffer_index = r_command_buffer_pool.buffers_used++;1164r_command_buffer = r_command_buffer_pool.buffers[command_buffer_index];1165driver->command_buffer_begin(r_command_buffer);1166}11671168const VectorView clear_values(draw_list_command->clear_values(), draw_list_command->clear_values_count);1169#if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)1170driver->command_insert_breadcrumb(r_command_buffer, draw_list_command->breadcrumb);1171#endif1172RDD::RenderPassID render_pass;1173RDD::FramebufferID framebuffer;1174if (draw_list_command->framebuffer_cache != nullptr) {1175_get_draw_list_render_pass_and_framebuffer(draw_list_command, render_pass, framebuffer);1176} else {1177render_pass = draw_list_command->render_pass;1178framebuffer = draw_list_command->framebuffer;1179}11801181if (framebuffer && render_pass) {1182driver->command_begin_render_pass(r_command_buffer, render_pass, framebuffer, draw_list_command->command_buffer_type, draw_list_command->region, clear_values);1183_run_draw_list_command(r_command_buffer, draw_list_command->instruction_data(), draw_list_command->instruction_data_size);1184driver->command_end_render_pass(r_command_buffer);1185}1186} break;1187case RecordedCommand::TYPE_TEXTURE_CLEAR_COLOR: {1188const RecordedTextureClearColorCommand *texture_clear_color_command = reinterpret_cast<const RecordedTextureClearColorCommand *>(command);1189driver->command_clear_color_texture(r_command_buffer, texture_clear_color_command->texture, RDD::TEXTURE_LAYOUT_COPY_DST_OPTIMAL, texture_clear_color_command->color, texture_clear_color_command->range);1190} break;1191case RecordedCommand::TYPE_TEXTURE_CLEAR_DEPTH_STENCIL: {1192const RecordedTextureClearDepthStencilCommand *texture_clear_depth_stencil_command = reinterpret_cast<const RecordedTextureClearDepthStencilCommand *>(command);1193driver->command_clear_depth_stencil_texture(r_command_buffer, texture_clear_depth_stencil_command->texture, RDD::TEXTURE_LAYOUT_COPY_DST_OPTIMAL, texture_clear_depth_stencil_command->depth, texture_clear_depth_stencil_command->stencil, texture_clear_depth_stencil_command->range);1194} break;1195case RecordedCommand::TYPE_TEXTURE_COPY: {1196const RecordedTextureCopyCommand *texture_copy_command = reinterpret_cast<const RecordedTextureCopyCommand *>(command);1197const VectorView<RDD::TextureCopyRegion> command_texture_copy_regions_view(texture_copy_command->texture_copy_regions(), texture_copy_command->texture_copy_regions_count);1198driver->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);1199} break;1200case RecordedCommand::TYPE_TEXTURE_GET_DATA: {1201const RecordedTextureGetDataCommand *texture_get_data_command = reinterpret_cast<const RecordedTextureGetDataCommand *>(command);1202const 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);1203driver->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);1204} break;1205case RecordedCommand::TYPE_TEXTURE_RESOLVE: {1206const RecordedTextureResolveCommand *texture_resolve_command = reinterpret_cast<const RecordedTextureResolveCommand *>(command);1207driver->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);1208} break;1209case RecordedCommand::TYPE_TEXTURE_UPDATE: {1210const RecordedTextureUpdateCommand *texture_update_command = reinterpret_cast<const RecordedTextureUpdateCommand *>(command);1211const RecordedBufferToTextureCopy *command_buffer_to_texture_copies = texture_update_command->buffer_to_texture_copies();1212for (uint32_t j = 0; j < texture_update_command->buffer_to_texture_copies_count; j++) {1213driver->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);1214}1215} break;1216case RecordedCommand::TYPE_CAPTURE_TIMESTAMP: {1217const RecordedCaptureTimestampCommand *texture_capture_timestamp_command = reinterpret_cast<const RecordedCaptureTimestampCommand *>(command);1218driver->command_timestamp_write(r_command_buffer, texture_capture_timestamp_command->pool, texture_capture_timestamp_command->index);1219} break;1220default: {1221DEV_ASSERT(false && "Unknown recorded command type.");1222return;1223}1224}1225}1226}12271228void 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) {1229if (command_label_count == 0) {1230// Ignore any label operations if no labels were pushed.1231return;1232}12331234if (p_ignore_previous_value || p_new_label_index != r_current_label_index || p_new_level != r_current_label_level) {1235if (!p_ignore_previous_value && (p_use_label_for_empty || r_current_label_index >= 0 || r_current_label_level >= 0)) {1236// End the current label.1237driver->command_end_label(p_command_buffer);1238}12391240String label_name;1241Color label_color;1242if (p_new_label_index >= 0) {1243const char *label_chars = &command_label_chars[command_label_offsets[p_new_label_index]];1244label_name.append_utf8(label_chars);1245label_color = command_label_colors[p_new_label_index];1246} else if (p_use_label_for_empty) {1247label_name = "Command Graph";1248label_color = Color(1, 1, 1, 1);1249} else {1250return;1251}12521253// Add the level to the name.1254label_name += " (L" + itos(p_new_level) + ")";12551256if (p_sorted_commands != nullptr && p_sorted_commands_count > 0) {1257// Analyze the commands in the level that have the same label to detect what type of operations are performed.1258bool copy_commands = false;1259bool compute_commands = false;1260bool draw_commands = false;1261bool custom_commands = false;1262for (uint32_t i = 0; i < p_sorted_commands_count; i++) {1263const uint32_t command_index = p_sorted_commands[i].index;1264const uint32_t command_data_offset = command_data_offsets[command_index];1265const RecordedCommand *command = reinterpret_cast<RecordedCommand *>(&command_data[command_data_offset]);1266if (command->label_index != p_new_label_index) {1267break;1268}12691270switch (command->type) {1271case RecordedCommand::TYPE_BUFFER_CLEAR:1272case RecordedCommand::TYPE_BUFFER_COPY:1273case RecordedCommand::TYPE_BUFFER_GET_DATA:1274case RecordedCommand::TYPE_BUFFER_UPDATE:1275case RecordedCommand::TYPE_TEXTURE_CLEAR_COLOR:1276case RecordedCommand::TYPE_TEXTURE_CLEAR_DEPTH_STENCIL:1277case RecordedCommand::TYPE_TEXTURE_COPY:1278case RecordedCommand::TYPE_TEXTURE_GET_DATA:1279case RecordedCommand::TYPE_TEXTURE_RESOLVE:1280case RecordedCommand::TYPE_TEXTURE_UPDATE: {1281copy_commands = true;1282} break;1283case RecordedCommand::TYPE_COMPUTE_LIST: {1284compute_commands = true;1285} break;1286case RecordedCommand::TYPE_DRAW_LIST: {1287draw_commands = true;1288} break;1289case RecordedCommand::TYPE_DRIVER_CALLBACK: {1290custom_commands = true;1291} break;1292default: {1293// Ignore command.1294} break;1295}12961297if (copy_commands && compute_commands && draw_commands && custom_commands) {1298// There's no more command types to find.1299break;1300}1301}13021303if (copy_commands || compute_commands || draw_commands || custom_commands) {1304// Add the operations to the name.1305bool plus_after_copy = copy_commands && (compute_commands || draw_commands || custom_commands);1306bool plus_after_compute = compute_commands && (draw_commands || custom_commands);1307bool plus_after_draw = draw_commands && custom_commands;1308label_name += " (";1309label_name += copy_commands ? "Copy" : "";1310label_name += plus_after_copy ? "+" : "";1311label_name += compute_commands ? "Compute" : "";1312label_name += plus_after_compute ? "+" : "";1313label_name += draw_commands ? "Draw" : "";1314label_name += plus_after_draw ? "+" : "";1315label_name += custom_commands ? "Custom" : "";1316label_name += ")";1317}1318}13191320// Start the new label.1321CharString label_name_utf8 = label_name.utf8();1322driver->command_begin_label(p_command_buffer, label_name_utf8.get_data(), label_color);13231324r_current_label_index = p_new_label_index;1325r_current_label_level = p_new_level;1326}1327}13281329void RenderingDeviceGraph::_boost_priority_for_render_commands(RecordedCommandSort *p_sorted_commands, uint32_t p_sorted_commands_count, uint32_t &r_boosted_priority) {1330if (p_sorted_commands_count == 0) {1331return;1332}13331334const uint32_t boosted_priority_value = 0;1335if (r_boosted_priority > 0) {1336bool perform_sort = false;1337for (uint32_t j = 0; j < p_sorted_commands_count; j++) {1338if (p_sorted_commands[j].priority == r_boosted_priority) {1339p_sorted_commands[j].priority = boosted_priority_value;1340perform_sort = true;1341}1342}13431344if (perform_sort) {1345SortArray<RecordedCommandSort> command_sorter;1346command_sorter.sort(p_sorted_commands, p_sorted_commands_count);1347}1348}13491350if (p_sorted_commands[p_sorted_commands_count - 1].priority != boosted_priority_value) {1351r_boosted_priority = p_sorted_commands[p_sorted_commands_count - 1].priority;1352}1353}13541355void 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) {1356if (!driver_honors_barriers) {1357return;1358}13591360barrier_group.clear();1361barrier_group.src_stages = RDD::PIPELINE_STAGE_TOP_OF_PIPE_BIT;1362barrier_group.dst_stages = RDD::PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;13631364for (uint32_t i = 0; i < p_sorted_commands_count; i++) {1365const uint32_t command_index = p_sorted_commands[i].index;1366const uint32_t command_data_offset = command_data_offsets[command_index];1367const RecordedCommand *command = reinterpret_cast<RecordedCommand *>(&command_data[command_data_offset]);13681369#if PRINT_COMMAND_RECORDING1370print_line(vformat("Grouping barriers for #%d", command_index));1371#endif13721373// Merge command's stage bits with the barrier group.1374barrier_group.src_stages = barrier_group.src_stages | command->previous_stages;1375barrier_group.dst_stages = barrier_group.dst_stages | command->next_stages;13761377// Merge command's memory barrier bits with the barrier group.1378barrier_group.memory_barrier.src_access = barrier_group.memory_barrier.src_access | command->memory_barrier.src_access;1379barrier_group.memory_barrier.dst_access = barrier_group.memory_barrier.dst_access | command->memory_barrier.dst_access;13801381// Gather texture barriers.1382for (int32_t j = 0; j < command->normalization_barrier_count; j++) {1383const RDD::TextureBarrier &recorded_barrier = command_normalization_barriers[command->normalization_barrier_index + j];1384barrier_group.normalization_barriers.push_back(recorded_barrier);1385#if PRINT_COMMAND_RECORDING1386print_line(vformat("Normalization Barrier #%d", barrier_group.normalization_barriers.size() - 1));1387#endif1388}13891390for (int32_t j = 0; j < command->transition_barrier_count; j++) {1391const RDD::TextureBarrier &recorded_barrier = command_transition_barriers[command->transition_barrier_index + j];1392barrier_group.transition_barriers.push_back(recorded_barrier);1393#if PRINT_COMMAND_RECORDING1394print_line(vformat("Transition Barrier #%d", barrier_group.transition_barriers.size() - 1));1395#endif1396}13971398#if USE_BUFFER_BARRIERS1399// Gather buffer barriers.1400for (int32_t j = 0; j < command->buffer_barrier_count; j++) {1401const RDD::BufferBarrier &recorded_barrier = command_buffer_barriers[command->buffer_barrier_index + j];1402barrier_group.buffer_barriers.push_back(recorded_barrier);1403}1404#endif14051406// Gather acceleration structure barriers.1407for (int32_t j = 0; j < command->acceleration_structure_barrier_count; j++) {1408const RDD::AccelerationStructureBarrier &recorded_barrier = command_acceleration_structure_barriers[command->acceleration_structure_barrier_index + j];1409barrier_group.acceleration_structure_barriers.push_back(recorded_barrier);1410}1411}14121413if (p_full_memory_barrier) {1414barrier_group.src_stages = RDD::PIPELINE_STAGE_ALL_COMMANDS_BIT;1415barrier_group.dst_stages = RDD::PIPELINE_STAGE_ALL_COMMANDS_BIT;1416barrier_group.memory_barrier.src_access = RDD::BARRIER_ACCESS_MEMORY_READ_BIT | RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT;1417barrier_group.memory_barrier.dst_access = RDD::BARRIER_ACCESS_MEMORY_READ_BIT | RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT;1418}14191420const bool is_memory_barrier_empty = barrier_group.memory_barrier.src_access.is_empty() && barrier_group.memory_barrier.dst_access.is_empty();1421const bool are_texture_barriers_empty = barrier_group.normalization_barriers.is_empty() && barrier_group.transition_barriers.is_empty();1422#if USE_BUFFER_BARRIERS1423const bool are_buffer_barriers_empty = barrier_group.buffer_barriers.is_empty();1424#else1425const bool are_buffer_barriers_empty = true;1426#endif1427const bool are_acceleration_structure_barriers_empty = barrier_group.acceleration_structure_barriers.is_empty();1428if (is_memory_barrier_empty && are_texture_barriers_empty && are_buffer_barriers_empty && are_acceleration_structure_barriers_empty) {1429// Commands don't require synchronization.1430return;1431}14321433const VectorView<RDD::MemoryAccessBarrier> memory_barriers = !is_memory_barrier_empty ? barrier_group.memory_barrier : VectorView<RDD::MemoryAccessBarrier>();1434const VectorView<RDD::TextureBarrier> texture_barriers = barrier_group.normalization_barriers.is_empty() ? barrier_group.transition_barriers : barrier_group.normalization_barriers;1435#if USE_BUFFER_BARRIERS1436const VectorView<RDD::BufferBarrier> buffer_barriers = !are_buffer_barriers_empty ? barrier_group.buffer_barriers : VectorView<RDD::BufferBarrier>();1437#else1438const VectorView<RDD::BufferBarrier> buffer_barriers = VectorView<RDD::BufferBarrier>();1439#endif1440const VectorView<RDD::AccelerationStructureBarrier> acceleration_structure_barriers = !are_acceleration_structure_barriers_empty ? barrier_group.acceleration_structure_barriers : VectorView<RDD::AccelerationStructureBarrier>();14411442driver->command_pipeline_barrier(p_command_buffer, barrier_group.src_stages, barrier_group.dst_stages, memory_barriers, buffer_barriers, texture_barriers, acceleration_structure_barriers);14431444bool separate_texture_barriers = !barrier_group.normalization_barriers.is_empty() && !barrier_group.transition_barriers.is_empty();1445if (separate_texture_barriers) {1446driver->command_pipeline_barrier(p_command_buffer, barrier_group.src_stages, barrier_group.dst_stages, VectorView<RDD::MemoryAccessBarrier>(), VectorView<RDD::BufferBarrier>(), barrier_group.transition_barriers, VectorView<RDD::AccelerationStructureBarrier>());1447}1448}14491450void RenderingDeviceGraph::_print_render_commands(const RecordedCommandSort *p_sorted_commands, uint32_t p_sorted_commands_count) {1451for (uint32_t i = 0; i < p_sorted_commands_count; i++) {1452const uint32_t command_index = p_sorted_commands[i].index;1453const uint32_t command_level = p_sorted_commands[i].level;1454const uint32_t command_data_offset = command_data_offsets[command_index];1455const RecordedCommand *command = reinterpret_cast<RecordedCommand *>(&command_data[command_data_offset]);1456switch (command->type) {1457case RecordedCommand::TYPE_BUFFER_CLEAR: {1458const RecordedBufferClearCommand *buffer_clear_command = reinterpret_cast<const RecordedBufferClearCommand *>(command);1459print_line(command_index, "LEVEL", command_level, "BUFFER CLEAR DESTINATION", itos(buffer_clear_command->buffer.id));1460} break;1461case RecordedCommand::TYPE_BUFFER_COPY: {1462const RecordedBufferCopyCommand *buffer_copy_command = reinterpret_cast<const RecordedBufferCopyCommand *>(command);1463print_line(command_index, "LEVEL", command_level, "BUFFER COPY SOURCE", itos(buffer_copy_command->source.id), "DESTINATION", itos(buffer_copy_command->destination.id));1464} break;1465case RecordedCommand::TYPE_BUFFER_GET_DATA: {1466const RecordedBufferGetDataCommand *buffer_get_data_command = reinterpret_cast<const RecordedBufferGetDataCommand *>(command);1467print_line(command_index, "LEVEL", command_level, "BUFFER GET DATA DESTINATION", itos(buffer_get_data_command->destination.id));1468} break;1469case RecordedCommand::TYPE_BUFFER_UPDATE: {1470const RecordedBufferUpdateCommand *buffer_update_command = reinterpret_cast<const RecordedBufferUpdateCommand *>(command);1471print_line(command_index, "LEVEL", command_level, "BUFFER UPDATE DESTINATION", itos(buffer_update_command->destination.id), "COPIES", buffer_update_command->buffer_copies_count);1472} break;1473case RecordedCommand::TYPE_DRIVER_CALLBACK: {1474print_line(command_index, "LEVEL", command_level, "DRIVER CALLBACK");1475} break;1476case RecordedCommand::TYPE_COMPUTE_LIST: {1477const RecordedComputeListCommand *compute_list_command = reinterpret_cast<const RecordedComputeListCommand *>(command);1478print_line(command_index, "LEVEL", command_level, "COMPUTE LIST SIZE", compute_list_command->instruction_data_size);1479} break;1480case RecordedCommand::TYPE_DRAW_LIST: {1481const RecordedDrawListCommand *draw_list_command = reinterpret_cast<const RecordedDrawListCommand *>(command);1482print_line(command_index, "LEVEL", command_level, "DRAW LIST SIZE", draw_list_command->instruction_data_size);1483} break;1484case RecordedCommand::TYPE_TEXTURE_CLEAR_COLOR: {1485const RecordedTextureClearColorCommand *texture_clear_color_command = reinterpret_cast<const RecordedTextureClearColorCommand *>(command);1486print_line(command_index, "LEVEL", command_level, "TEXTURE CLEAR COLOR", itos(texture_clear_color_command->texture.id), "COLOR", texture_clear_color_command->color);1487} break;1488case RecordedCommand::TYPE_TEXTURE_CLEAR_DEPTH_STENCIL: {1489const RecordedTextureClearDepthStencilCommand *texture_clear_depth_stencil_command = reinterpret_cast<const RecordedTextureClearDepthStencilCommand *>(command);1490print_line(command_index, "LEVEL", command_level, "TEXTURE CLEAR DEPTH STENCIL", itos(texture_clear_depth_stencil_command->texture.id), "DEPTH", rtos(texture_clear_depth_stencil_command->depth), "STENCIL", itos(texture_clear_depth_stencil_command->stencil));1491} break;1492case RecordedCommand::TYPE_TEXTURE_COPY: {1493const RecordedTextureCopyCommand *texture_copy_command = reinterpret_cast<const RecordedTextureCopyCommand *>(command);1494print_line(command_index, "LEVEL", command_level, "TEXTURE COPY FROM", itos(texture_copy_command->from_texture.id), "TO", itos(texture_copy_command->to_texture.id));1495} break;1496case RecordedCommand::TYPE_TEXTURE_GET_DATA: {1497print_line(command_index, "LEVEL", command_level, "TEXTURE GET DATA");1498} break;1499case RecordedCommand::TYPE_TEXTURE_RESOLVE: {1500const RecordedTextureResolveCommand *texture_resolve_command = reinterpret_cast<const RecordedTextureResolveCommand *>(command);1501print_line(command_index, "LEVEL", command_level, "TEXTURE RESOLVE FROM", itos(texture_resolve_command->from_texture.id), "TO", itos(texture_resolve_command->to_texture.id));1502} break;1503case RecordedCommand::TYPE_TEXTURE_UPDATE: {1504const RecordedTextureUpdateCommand *texture_update_command = reinterpret_cast<const RecordedTextureUpdateCommand *>(command);1505print_line(command_index, "LEVEL", command_level, "TEXTURE UPDATE TO", itos(texture_update_command->to_texture.id));1506} break;1507case RecordedCommand::TYPE_CAPTURE_TIMESTAMP: {1508const RecordedCaptureTimestampCommand *texture_capture_timestamp_command = reinterpret_cast<const RecordedCaptureTimestampCommand *>(command);1509print_line(command_index, "LEVEL", command_level, "CAPTURE TIMESTAMP POOL", itos(texture_capture_timestamp_command->pool.id), "INDEX", texture_capture_timestamp_command->index);1510} break;1511default:1512DEV_ASSERT(false && "Unknown recorded command type.");1513return;1514}1515}1516}15171518void RenderingDeviceGraph::_print_draw_list(const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {1519uint32_t instruction_data_cursor = 0;1520while (instruction_data_cursor < p_instruction_data_size) {1521DEV_ASSERT((instruction_data_cursor + sizeof(DrawListInstruction)) <= p_instruction_data_size);15221523const DrawListInstruction *instruction = reinterpret_cast<const DrawListInstruction *>(&p_instruction_data[instruction_data_cursor]);1524switch (instruction->type) {1525case DrawListInstruction::TYPE_BIND_INDEX_BUFFER: {1526const DrawListBindIndexBufferInstruction *bind_index_buffer_instruction = reinterpret_cast<const DrawListBindIndexBufferInstruction *>(instruction);1527print_line("\tBIND INDEX BUFFER ID", itos(bind_index_buffer_instruction->buffer.id), "FORMAT", bind_index_buffer_instruction->format, "OFFSET", bind_index_buffer_instruction->offset);1528instruction_data_cursor += sizeof(DrawListBindIndexBufferInstruction);1529} break;1530case DrawListInstruction::TYPE_BIND_PIPELINE: {1531const DrawListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const DrawListBindPipelineInstruction *>(instruction);1532print_line("\tBIND PIPELINE ID", itos(bind_pipeline_instruction->pipeline.id));1533instruction_data_cursor += sizeof(DrawListBindPipelineInstruction);1534} break;1535case DrawListInstruction::TYPE_BIND_UNIFORM_SETS: {1536const DrawListBindUniformSetsInstruction *bind_uniform_sets_instruction = reinterpret_cast<const DrawListBindUniformSetsInstruction *>(instruction);1537print_line("\tBIND UNIFORM SETS COUNT", bind_uniform_sets_instruction->set_count);1538for (uint32_t i = 0; i < bind_uniform_sets_instruction->set_count; i++) {1539print_line("\tBIND UNIFORM SET ID", itos(bind_uniform_sets_instruction->uniform_set_ids()[i].id), "START INDEX", bind_uniform_sets_instruction->first_set_index, "DYNAMIC_OFFSETS", bind_uniform_sets_instruction->dynamic_offsets_mask);1540}1541instruction_data_cursor += sizeof(DrawListBindUniformSetsInstruction) + sizeof(RDD::UniformSetID) * bind_uniform_sets_instruction->set_count;1542} break;1543case DrawListInstruction::TYPE_BIND_VERTEX_BUFFERS: {1544const DrawListBindVertexBuffersInstruction *bind_vertex_buffers_instruction = reinterpret_cast<const DrawListBindVertexBuffersInstruction *>(instruction);1545print_line("\tBIND VERTEX BUFFERS COUNT", bind_vertex_buffers_instruction->vertex_buffers_count);1546instruction_data_cursor += sizeof(DrawListBindVertexBuffersInstruction);1547instruction_data_cursor += sizeof(RDD::BufferID) * bind_vertex_buffers_instruction->vertex_buffers_count;1548instruction_data_cursor += sizeof(uint64_t) * bind_vertex_buffers_instruction->vertex_buffers_count;1549} break;1550case DrawListInstruction::TYPE_CLEAR_ATTACHMENTS: {1551const DrawListClearAttachmentsInstruction *clear_attachments_instruction = reinterpret_cast<const DrawListClearAttachmentsInstruction *>(instruction);1552print_line("\tATTACHMENTS CLEAR COUNT", clear_attachments_instruction->attachments_clear_count, "RECT COUNT", clear_attachments_instruction->attachments_clear_rect_count);1553instruction_data_cursor += sizeof(DrawListClearAttachmentsInstruction);1554instruction_data_cursor += sizeof(RDD::AttachmentClear) * clear_attachments_instruction->attachments_clear_count;1555instruction_data_cursor += sizeof(Rect2i) * clear_attachments_instruction->attachments_clear_rect_count;1556} break;1557case DrawListInstruction::TYPE_DRAW: {1558const DrawListDrawInstruction *draw_instruction = reinterpret_cast<const DrawListDrawInstruction *>(instruction);1559print_line("\tDRAW VERTICES", draw_instruction->vertex_count, "INSTANCES", draw_instruction->instance_count);1560instruction_data_cursor += sizeof(DrawListDrawInstruction);1561} break;1562case DrawListInstruction::TYPE_DRAW_INDEXED: {1563const DrawListDrawIndexedInstruction *draw_indexed_instruction = reinterpret_cast<const DrawListDrawIndexedInstruction *>(instruction);1564print_line("\tDRAW INDICES", draw_indexed_instruction->index_count, "INSTANCES", draw_indexed_instruction->instance_count, "FIRST INDEX", draw_indexed_instruction->first_index);1565instruction_data_cursor += sizeof(DrawListDrawIndexedInstruction);1566} break;1567case DrawListInstruction::TYPE_DRAW_INDIRECT: {1568const DrawListDrawIndirectInstruction *draw_indirect_instruction = reinterpret_cast<const DrawListDrawIndirectInstruction *>(instruction);1569print_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);1570instruction_data_cursor += sizeof(DrawListDrawIndirectInstruction);1571} break;1572case DrawListInstruction::TYPE_DRAW_INDEXED_INDIRECT: {1573const DrawListDrawIndexedIndirectInstruction *draw_indexed_indirect_instruction = reinterpret_cast<const DrawListDrawIndexedIndirectInstruction *>(instruction);1574print_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);1575instruction_data_cursor += sizeof(DrawListDrawIndexedIndirectInstruction);1576} break;1577case DrawListInstruction::TYPE_EXECUTE_COMMANDS: {1578print_line("\tEXECUTE COMMANDS");1579instruction_data_cursor += sizeof(DrawListExecuteCommandsInstruction);1580} break;1581case DrawListInstruction::TYPE_NEXT_SUBPASS: {1582print_line("\tNEXT SUBPASS");1583instruction_data_cursor += sizeof(DrawListNextSubpassInstruction);1584} break;1585case DrawListInstruction::TYPE_SET_BLEND_CONSTANTS: {1586const DrawListSetBlendConstantsInstruction *set_blend_constants_instruction = reinterpret_cast<const DrawListSetBlendConstantsInstruction *>(instruction);1587print_line("\tSET BLEND CONSTANTS COLOR", set_blend_constants_instruction->color);1588instruction_data_cursor += sizeof(DrawListSetBlendConstantsInstruction);1589} break;1590case DrawListInstruction::TYPE_SET_LINE_WIDTH: {1591const DrawListSetLineWidthInstruction *set_line_width_instruction = reinterpret_cast<const DrawListSetLineWidthInstruction *>(instruction);1592print_line("\tSET LINE WIDTH", set_line_width_instruction->width);1593instruction_data_cursor += sizeof(DrawListSetLineWidthInstruction);1594} break;1595case DrawListInstruction::TYPE_SET_PUSH_CONSTANT: {1596const DrawListSetPushConstantInstruction *set_push_constant_instruction = reinterpret_cast<const DrawListSetPushConstantInstruction *>(instruction);1597print_line("\tSET PUSH CONSTANT SIZE", set_push_constant_instruction->size);1598instruction_data_cursor += sizeof(DrawListSetPushConstantInstruction);1599instruction_data_cursor += set_push_constant_instruction->size;1600} break;1601case DrawListInstruction::TYPE_SET_SCISSOR: {1602const DrawListSetScissorInstruction *set_scissor_instruction = reinterpret_cast<const DrawListSetScissorInstruction *>(instruction);1603print_line("\tSET SCISSOR", set_scissor_instruction->rect);1604instruction_data_cursor += sizeof(DrawListSetScissorInstruction);1605} break;1606case DrawListInstruction::TYPE_SET_VIEWPORT: {1607const DrawListSetViewportInstruction *set_viewport_instruction = reinterpret_cast<const DrawListSetViewportInstruction *>(instruction);1608print_line("\tSET VIEWPORT", set_viewport_instruction->rect);1609instruction_data_cursor += sizeof(DrawListSetViewportInstruction);1610} break;1611case DrawListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE: {1612const DrawListUniformSetPrepareForUseInstruction *uniform_set_prepare_for_use_instruction = reinterpret_cast<const DrawListUniformSetPrepareForUseInstruction *>(instruction);1613print_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);1614instruction_data_cursor += sizeof(DrawListUniformSetPrepareForUseInstruction);1615} break;1616default:1617DEV_ASSERT(false && "Unknown draw list instruction type.");1618return;1619}16201621instruction_data_cursor = GRAPH_ALIGN(instruction_data_cursor);1622}1623}16241625void RenderingDeviceGraph::_print_raytracing_list(const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {1626uint32_t instruction_data_cursor = 0;1627while (instruction_data_cursor < p_instruction_data_size) {1628DEV_ASSERT((instruction_data_cursor + sizeof(RaytracingListInstruction)) <= p_instruction_data_size);16291630const RaytracingListInstruction *instruction = reinterpret_cast<const RaytracingListInstruction *>(&p_instruction_data[instruction_data_cursor]);1631switch (instruction->type) {1632case RaytracingListInstruction::TYPE_BIND_PIPELINE: {1633const RaytracingListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const RaytracingListBindPipelineInstruction *>(instruction);1634print_line("\tBIND PIPELINE ID", itos(bind_pipeline_instruction->pipeline.id));1635instruction_data_cursor += sizeof(RaytracingListBindPipelineInstruction);1636} break;1637case RaytracingListInstruction::TYPE_BIND_UNIFORM_SET: {1638const RaytracingListBindUniformSetInstruction *bind_uniform_set_instruction = reinterpret_cast<const RaytracingListBindUniformSetInstruction *>(instruction);1639print_line("\tBIND UNIFORM SET ID", itos(bind_uniform_set_instruction->uniform_set.id), "SHADER ID", itos(bind_uniform_set_instruction->shader.id));1640instruction_data_cursor += sizeof(RaytracingListBindUniformSetInstruction);1641} break;1642case RaytracingListInstruction::TYPE_SET_PUSH_CONSTANT: {1643const RaytracingListSetPushConstantInstruction *set_push_constant_instruction = reinterpret_cast<const RaytracingListSetPushConstantInstruction *>(instruction);1644print_line("\tSET PUSH CONSTANT SIZE", set_push_constant_instruction->size);1645instruction_data_cursor += sizeof(RaytracingListSetPushConstantInstruction);1646instruction_data_cursor += set_push_constant_instruction->size;1647} break;1648case RaytracingListInstruction::TYPE_TRACE_RAYS: {1649const RaytracingListTraceRaysInstruction *trace_rays_instruction = reinterpret_cast<const RaytracingListTraceRaysInstruction *>(instruction);1650print_line("\tTRACE RAYS WIDTH", itos(trace_rays_instruction->width), "HEIGHT", itos(trace_rays_instruction->height));1651instruction_data_cursor += sizeof(RaytracingListTraceRaysInstruction);1652} break;1653case RaytracingListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE: {1654const RaytracingListUniformSetPrepareForUseInstruction *uniform_set_prepare_for_use_instruction = reinterpret_cast<const RaytracingListUniformSetPrepareForUseInstruction *>(instruction);1655print_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));1656instruction_data_cursor += sizeof(RaytracingListUniformSetPrepareForUseInstruction);1657} break;1658default:1659DEV_ASSERT(false && "Unknown raytracing list instruction type.");1660return;1661}1662}1663}16641665void RenderingDeviceGraph::_print_compute_list(const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {1666uint32_t instruction_data_cursor = 0;1667while (instruction_data_cursor < p_instruction_data_size) {1668DEV_ASSERT((instruction_data_cursor + sizeof(ComputeListInstruction)) <= p_instruction_data_size);16691670const ComputeListInstruction *instruction = reinterpret_cast<const ComputeListInstruction *>(&p_instruction_data[instruction_data_cursor]);1671switch (instruction->type) {1672case ComputeListInstruction::TYPE_BIND_PIPELINE: {1673const ComputeListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const ComputeListBindPipelineInstruction *>(instruction);1674print_line("\tBIND PIPELINE ID", itos(bind_pipeline_instruction->pipeline.id));1675instruction_data_cursor += sizeof(ComputeListBindPipelineInstruction);1676} break;1677case ComputeListInstruction::TYPE_BIND_UNIFORM_SETS: {1678const ComputeListBindUniformSetsInstruction *bind_uniform_sets_instruction = reinterpret_cast<const ComputeListBindUniformSetsInstruction *>(instruction);1679print_line("\tBIND UNIFORM SETS COUNT", bind_uniform_sets_instruction->set_count);1680for (uint32_t i = 0; i < bind_uniform_sets_instruction->set_count; i++) {1681print_line("\tBIND UNIFORM SET ID", itos(bind_uniform_sets_instruction->uniform_set_ids()[i].id), "START INDEX", bind_uniform_sets_instruction->first_set_index, "DYNAMIC_OFFSETS", bind_uniform_sets_instruction->dynamic_offsets_mask);1682}1683instruction_data_cursor += sizeof(ComputeListBindUniformSetsInstruction) + sizeof(RDD::UniformSetID) * bind_uniform_sets_instruction->set_count;1684} break;1685case ComputeListInstruction::TYPE_DISPATCH: {1686const ComputeListDispatchInstruction *dispatch_instruction = reinterpret_cast<const ComputeListDispatchInstruction *>(instruction);1687print_line("\tDISPATCH", dispatch_instruction->x_groups, dispatch_instruction->y_groups, dispatch_instruction->z_groups);1688instruction_data_cursor += sizeof(ComputeListDispatchInstruction);1689} break;1690case ComputeListInstruction::TYPE_DISPATCH_INDIRECT: {1691const ComputeListDispatchIndirectInstruction *dispatch_indirect_instruction = reinterpret_cast<const ComputeListDispatchIndirectInstruction *>(instruction);1692print_line("\tDISPATCH INDIRECT BUFFER ID", itos(dispatch_indirect_instruction->buffer.id), "OFFSET", dispatch_indirect_instruction->offset);1693instruction_data_cursor += sizeof(ComputeListDispatchIndirectInstruction);1694} break;1695case ComputeListInstruction::TYPE_SET_PUSH_CONSTANT: {1696const ComputeListSetPushConstantInstruction *set_push_constant_instruction = reinterpret_cast<const ComputeListSetPushConstantInstruction *>(instruction);1697print_line("\tSET PUSH CONSTANT SIZE", set_push_constant_instruction->size);1698instruction_data_cursor += sizeof(ComputeListSetPushConstantInstruction);1699instruction_data_cursor += set_push_constant_instruction->size;1700} break;1701case ComputeListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE: {1702const ComputeListUniformSetPrepareForUseInstruction *uniform_set_prepare_for_use_instruction = reinterpret_cast<const ComputeListUniformSetPrepareForUseInstruction *>(instruction);1703print_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));1704instruction_data_cursor += sizeof(ComputeListUniformSetPrepareForUseInstruction);1705} break;1706default:1707DEV_ASSERT(false && "Unknown compute list instruction type.");1708return;1709}17101711instruction_data_cursor = GRAPH_ALIGN(instruction_data_cursor);1712}1713}17141715void 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) {1716DEV_ASSERT(p_driver != nullptr);1717DEV_ASSERT(p_render_pass_creation_function != nullptr);1718DEV_ASSERT(p_frame_count > 0);17191720driver = p_driver;1721device = p_device;1722render_pass_creation_function = p_render_pass_creation_function;1723frames.resize(p_frame_count);17241725for (uint32_t i = 0; i < p_frame_count; i++) {1726frames[i].secondary_command_buffers.resize(p_secondary_command_buffers_per_frame);17271728for (uint32_t j = 0; j < p_secondary_command_buffers_per_frame; j++) {1729SecondaryCommandBuffer &secondary = frames[i].secondary_command_buffers[j];1730secondary.command_pool = driver->command_pool_create(p_secondary_command_queue_family, RDD::COMMAND_BUFFER_TYPE_SECONDARY);1731secondary.command_buffer = driver->command_buffer_create(secondary.command_pool);1732secondary.task = WorkerThreadPool::INVALID_TASK_ID;1733}1734}17351736driver_honors_barriers = driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS);1737driver_clears_with_copy_engine = driver->api_trait_get(RDD::API_TRAIT_CLEARS_WITH_COPY_ENGINE);1738driver_buffers_require_transitions = driver->api_trait_get(RDD::API_TRAIT_BUFFERS_REQUIRE_TRANSITIONS);1739}17401741void RenderingDeviceGraph::finalize() {1742if (!frames.is_empty()) {1743_wait_for_secondary_command_buffer_tasks();1744}17451746for (Frame &f : frames) {1747for (SecondaryCommandBuffer &secondary : f.secondary_command_buffers) {1748if (secondary.command_pool.id != 0) {1749driver->command_pool_free(secondary.command_pool);1750}1751}1752}17531754frames.clear();1755}17561757void RenderingDeviceGraph::begin() {1758command_data.clear();1759command_data_offsets.clear();1760command_normalization_barriers.clear();1761command_transition_barriers.clear();1762command_buffer_barriers.clear();1763command_acceleration_structure_barriers.clear();1764command_label_chars.clear();1765command_label_colors.clear();1766command_label_offsets.clear();1767command_list_nodes.clear();1768read_slice_list_nodes.clear();1769write_slice_list_nodes.clear();1770command_count = 0;1771command_label_count = 0;1772command_timestamp_index = -1;1773command_synchronization_index = -1;1774command_synchronization_pending = false;1775command_label_index = -1;1776frames[frame].secondary_command_buffers_used = 0;1777draw_instruction_list.index = 0;1778compute_instruction_list.index = 0;1779tracking_frame++;17801781#ifdef DEV_ENABLED1782write_dependency_counters.clear();1783#endif1784}17851786void RenderingDeviceGraph::add_acceleration_structure_build(RDD::AccelerationStructureID p_acceleration_structure, RDD::BufferID p_scratch_buffer, ResourceTracker *p_dst_tracker, VectorView<ResourceTracker *> p_src_trackers) {1787int32_t command_index;1788RecordedAccelerationStructureBuildCommand *command = static_cast<RecordedAccelerationStructureBuildCommand *>(_allocate_command(sizeof(RecordedAccelerationStructureBuildCommand), command_index));1789command->type = RecordedCommand::TYPE_ACCELERATION_STRUCTURE_BUILD;1790command->self_stages = RDD::PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT;1791command->acceleration_structure = p_acceleration_structure;1792command->scratch_buffer = p_scratch_buffer;17931794thread_local LocalVector<ResourceTracker *> trackers;1795thread_local LocalVector<ResourceUsage> usages;17961797// Sources and destination.1798uint32_t resource_count = p_src_trackers.size() + 1;1799trackers.resize(resource_count);1800usages.resize(resource_count);18011802for (uint32_t i = 0; i < p_src_trackers.size(); ++i) {1803trackers[i] = p_src_trackers[i];1804usages[i] = RESOURCE_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT;1805}18061807trackers[resource_count - 1] = p_dst_tracker;1808usages[resource_count - 1] = RESOURCE_USAGE_ACCELERATION_STRUCTURE_READ_WRITE;18091810_add_command_to_graph(trackers.ptr(), usages.ptr(), usages.size(), command_index, command);1811}18121813void RenderingDeviceGraph::add_buffer_clear(RDD::BufferID p_dst, ResourceTracker *p_dst_tracker, uint32_t p_offset, uint32_t p_size) {1814DEV_ASSERT(p_dst_tracker != nullptr);18151816int32_t command_index;1817RecordedBufferClearCommand *command = static_cast<RecordedBufferClearCommand *>(_allocate_command(sizeof(RecordedBufferClearCommand), command_index));1818command->type = RecordedCommand::TYPE_BUFFER_CLEAR;1819command->buffer = p_dst;1820command->offset = p_offset;1821command->size = p_size;18221823ResourceUsage usage;1824if (driver_clears_with_copy_engine) {1825command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;1826usage = RESOURCE_USAGE_COPY_TO;1827} else {1828// If the driver is uncapable of using the copy engine for clearing the buffer (e.g. D3D12), we must transition it to storage buffer read/write usage.1829command->self_stages = RDD::PIPELINE_STAGE_CLEAR_STORAGE_BIT;1830usage = RESOURCE_USAGE_STORAGE_BUFFER_READ_WRITE;1831}18321833_add_command_to_graph(&p_dst_tracker, &usage, 1, command_index, command);1834}18351836void RenderingDeviceGraph::add_buffer_copy(RDD::BufferID p_src, ResourceTracker *p_src_tracker, RDD::BufferID p_dst, ResourceTracker *p_dst_tracker, RDD::BufferCopyRegion p_region) {1837// Source tracker is allowed to be null as it could be a read-only buffer.1838DEV_ASSERT(p_dst_tracker != nullptr);18391840int32_t command_index;1841RecordedBufferCopyCommand *command = static_cast<RecordedBufferCopyCommand *>(_allocate_command(sizeof(RecordedBufferCopyCommand), command_index));1842command->type = RecordedCommand::TYPE_BUFFER_COPY;1843command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;1844command->source = p_src;1845command->destination = p_dst;1846command->region = p_region;18471848ResourceTracker *trackers[2] = { p_dst_tracker, p_src_tracker };1849ResourceUsage usages[2] = { RESOURCE_USAGE_COPY_TO, RESOURCE_USAGE_COPY_FROM };1850_add_command_to_graph(trackers, usages, p_src_tracker != nullptr ? 2 : 1, command_index, command);1851}18521853void RenderingDeviceGraph::add_buffer_get_data(RDD::BufferID p_src, ResourceTracker *p_src_tracker, RDD::BufferID p_dst, RDD::BufferCopyRegion p_region) {1854// Source tracker is allowed to be null as it could be a read-only buffer.1855int32_t command_index;1856RecordedBufferGetDataCommand *command = static_cast<RecordedBufferGetDataCommand *>(_allocate_command(sizeof(RecordedBufferGetDataCommand), command_index));1857command->type = RecordedCommand::TYPE_BUFFER_GET_DATA;1858command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;1859command->source = p_src;1860command->destination = p_dst;1861command->region = p_region;18621863if (p_src_tracker != nullptr) {1864ResourceUsage usage = RESOURCE_USAGE_COPY_FROM;1865_add_command_to_graph(&p_src_tracker, &usage, 1, command_index, command);1866} else {1867_add_command_to_graph(nullptr, nullptr, 0, command_index, command);1868}1869}18701871void RenderingDeviceGraph::add_buffer_update(RDD::BufferID p_dst, ResourceTracker *p_dst_tracker, VectorView<RecordedBufferCopy> p_buffer_copies) {1872DEV_ASSERT(p_dst_tracker != nullptr);18731874size_t buffer_copies_size = p_buffer_copies.size() * sizeof(RecordedBufferCopy);1875uint64_t command_size = sizeof(RecordedBufferUpdateCommand) + buffer_copies_size;1876int32_t command_index;1877RecordedBufferUpdateCommand *command = static_cast<RecordedBufferUpdateCommand *>(_allocate_command(command_size, command_index));1878command->type = RecordedCommand::TYPE_BUFFER_UPDATE;1879command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;1880command->destination = p_dst;1881command->buffer_copies_count = p_buffer_copies.size();18821883RecordedBufferCopy *buffer_copies = command->buffer_copies();1884for (uint32_t i = 0; i < command->buffer_copies_count; i++) {1885buffer_copies[i] = p_buffer_copies[i];1886}18871888ResourceUsage buffer_usage = RESOURCE_USAGE_COPY_TO;1889_add_command_to_graph(&p_dst_tracker, &buffer_usage, 1, command_index, command);1890}18911892void RenderingDeviceGraph::add_driver_callback(RDD::DriverCallback p_callback, void *p_userdata, VectorView<ResourceTracker *> p_trackers, VectorView<RenderingDeviceGraph::ResourceUsage> p_usages) {1893DEV_ASSERT(p_trackers.size() == p_usages.size());18941895int32_t command_index;1896RecordedDriverCallbackCommand *command = static_cast<RecordedDriverCallbackCommand *>(_allocate_command(sizeof(RecordedDriverCallbackCommand), command_index));1897command->type = RecordedCommand::TYPE_DRIVER_CALLBACK;1898command->callback = p_callback;1899command->userdata = p_userdata;1900_add_command_to_graph((ResourceTracker **)p_trackers.ptr(), (ResourceUsage *)p_usages.ptr(), p_trackers.size(), command_index, command);1901}19021903void RenderingDeviceGraph::add_raytracing_list_begin() {1904raytracing_instruction_list.clear();1905raytracing_instruction_list.index++;1906}19071908void RenderingDeviceGraph::add_raytracing_list_bind_pipeline(RDD::RaytracingPipelineID p_pipeline) {1909RaytracingListBindPipelineInstruction *instruction = reinterpret_cast<RaytracingListBindPipelineInstruction *>(_allocate_raytracing_list_instruction(sizeof(RaytracingListBindPipelineInstruction)));1910instruction->type = RaytracingListInstruction::TYPE_BIND_PIPELINE;1911instruction->pipeline = p_pipeline;1912raytracing_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_RAY_TRACING_SHADER_BIT);1913}19141915void RenderingDeviceGraph::add_raytracing_list_bind_uniform_set(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index) {1916RaytracingListBindUniformSetInstruction *instruction = reinterpret_cast<RaytracingListBindUniformSetInstruction *>(_allocate_raytracing_list_instruction(sizeof(RaytracingListBindUniformSetInstruction)));1917instruction->type = RaytracingListInstruction::TYPE_BIND_UNIFORM_SET;1918instruction->shader = p_shader;1919instruction->uniform_set = p_uniform_set;1920instruction->set_index = set_index;1921}19221923void RenderingDeviceGraph::add_raytracing_list_set_push_constant(RDD::ShaderID p_shader, const void *p_data, uint32_t p_data_size) {1924uint32_t instruction_size = sizeof(RaytracingListSetPushConstantInstruction) + p_data_size;1925RaytracingListSetPushConstantInstruction *instruction = reinterpret_cast<RaytracingListSetPushConstantInstruction *>(_allocate_raytracing_list_instruction(instruction_size));1926instruction->type = RaytracingListInstruction::TYPE_SET_PUSH_CONSTANT;1927instruction->size = p_data_size;1928instruction->shader = p_shader;1929memcpy(instruction->data(), p_data, p_data_size);1930}19311932void RenderingDeviceGraph::add_raytracing_list_trace_rays(uint32_t p_width, uint32_t p_height) {1933RaytracingListTraceRaysInstruction *instruction = reinterpret_cast<RaytracingListTraceRaysInstruction *>(_allocate_raytracing_list_instruction(sizeof(RaytracingListTraceRaysInstruction)));1934instruction->type = RaytracingListInstruction::TYPE_TRACE_RAYS;1935instruction->width = p_width;1936instruction->height = p_height;1937}19381939void RenderingDeviceGraph::add_raytracing_list_uniform_set_prepare_for_use(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index) {1940RaytracingListUniformSetPrepareForUseInstruction *instruction = reinterpret_cast<RaytracingListUniformSetPrepareForUseInstruction *>(_allocate_raytracing_list_instruction(sizeof(RaytracingListUniformSetPrepareForUseInstruction)));1941instruction->type = RaytracingListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE;1942instruction->shader = p_shader;1943instruction->uniform_set = p_uniform_set;1944instruction->set_index = set_index;1945}19461947void RenderingDeviceGraph::add_raytracing_list_usage(ResourceTracker *p_tracker, ResourceUsage p_usage) {1948DEV_ASSERT(p_tracker != nullptr);19491950p_tracker->reset_if_outdated(tracking_frame);19511952if (p_tracker->raytracing_list_index != raytracing_instruction_list.index) {1953raytracing_instruction_list.command_trackers.push_back(p_tracker);1954raytracing_instruction_list.command_tracker_usages.push_back(p_usage);1955p_tracker->raytracing_list_index = raytracing_instruction_list.index;1956p_tracker->raytracing_list_usage = p_usage;1957}1958#ifdef DEV_ENABLED1959else if (p_tracker->raytracing_list_usage != p_usage) {1960ERR_FAIL_MSG(vformat("Tracker can't have more than one type of usage in the same raytracing list. Raytracing list usage is %d and the requested usage is %d.", p_tracker->raytracing_list_usage, p_usage));1961}1962#endif1963}19641965void RenderingDeviceGraph::add_raytracing_list_usages(VectorView<ResourceTracker *> p_trackers, VectorView<ResourceUsage> p_usages) {1966DEV_ASSERT(p_trackers.size() == p_usages.size());19671968for (uint32_t i = 0; i < p_trackers.size(); i++) {1969add_raytracing_list_usage(p_trackers[i], p_usages[i]);1970}1971}19721973void RenderingDeviceGraph::add_raytracing_list_end() {1974int32_t command_index;1975uint32_t instruction_data_size = raytracing_instruction_list.data.size();1976uint32_t command_size = sizeof(RecordedRaytracingListCommand) + instruction_data_size;1977RecordedRaytracingListCommand *command = static_cast<RecordedRaytracingListCommand *>(_allocate_command(command_size, command_index));1978command->type = RecordedCommand::TYPE_RAYTRACING_LIST;1979command->self_stages = raytracing_instruction_list.stages;1980command->instruction_data_size = instruction_data_size;1981memcpy(command->instruction_data(), raytracing_instruction_list.data.ptr(), instruction_data_size);1982_add_command_to_graph(raytracing_instruction_list.command_trackers.ptr(), raytracing_instruction_list.command_tracker_usages.ptr(), raytracing_instruction_list.command_trackers.size(), command_index, command);1983}19841985void RenderingDeviceGraph::add_compute_list_begin(RDD::BreadcrumbMarker p_phase, uint32_t p_breadcrumb_data) {1986compute_instruction_list.clear();1987#if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)1988compute_instruction_list.breadcrumb = p_breadcrumb_data | (p_phase & ((1 << 16) - 1));1989#endif1990compute_instruction_list.index++;1991}19921993void RenderingDeviceGraph::add_compute_list_bind_pipeline(RDD::PipelineID p_pipeline) {1994ComputeListBindPipelineInstruction *instruction = reinterpret_cast<ComputeListBindPipelineInstruction *>(_allocate_compute_list_instruction(sizeof(ComputeListBindPipelineInstruction)));1995instruction->type = ComputeListInstruction::TYPE_BIND_PIPELINE;1996instruction->pipeline = p_pipeline;1997compute_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT);1998}19992000void RenderingDeviceGraph::add_compute_list_bind_uniform_set(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index) {2001add_compute_list_bind_uniform_sets(p_shader, VectorView(&p_uniform_set, 1), set_index, 1);2002}20032004void 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) {2005DEV_ASSERT(p_uniform_sets.size() >= p_set_count);20062007uint32_t instruction_size = sizeof(ComputeListBindUniformSetsInstruction) + sizeof(RDD::UniformSetID) * p_set_count;2008ComputeListBindUniformSetsInstruction *instruction = reinterpret_cast<ComputeListBindUniformSetsInstruction *>(_allocate_compute_list_instruction(instruction_size));2009instruction->type = ComputeListInstruction::TYPE_BIND_UNIFORM_SETS;2010instruction->shader = p_shader;2011instruction->first_set_index = p_first_set_index;2012instruction->set_count = p_set_count;2013instruction->dynamic_offsets_mask = driver->uniform_sets_get_dynamic_offsets(p_uniform_sets, p_shader, p_first_set_index, p_set_count);20142015RDD::UniformSetID *ids = instruction->uniform_set_ids();2016for (uint32_t i = 0; i < p_set_count; i++) {2017ids[i] = p_uniform_sets[i];2018}2019}20202021void RenderingDeviceGraph::add_compute_list_dispatch(uint32_t p_x_groups, uint32_t p_y_groups, uint32_t p_z_groups) {2022ComputeListDispatchInstruction *instruction = reinterpret_cast<ComputeListDispatchInstruction *>(_allocate_compute_list_instruction(sizeof(ComputeListDispatchInstruction)));2023instruction->type = ComputeListInstruction::TYPE_DISPATCH;2024instruction->x_groups = p_x_groups;2025instruction->y_groups = p_y_groups;2026instruction->z_groups = p_z_groups;2027}20282029void RenderingDeviceGraph::add_compute_list_dispatch_indirect(RDD::BufferID p_buffer, uint32_t p_offset) {2030ComputeListDispatchIndirectInstruction *instruction = reinterpret_cast<ComputeListDispatchIndirectInstruction *>(_allocate_compute_list_instruction(sizeof(ComputeListDispatchIndirectInstruction)));2031instruction->type = ComputeListInstruction::TYPE_DISPATCH_INDIRECT;2032instruction->buffer = p_buffer;2033instruction->offset = p_offset;2034compute_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_DRAW_INDIRECT_BIT);2035}20362037void RenderingDeviceGraph::add_compute_list_set_push_constant(RDD::ShaderID p_shader, const void *p_data, uint32_t p_data_size) {2038uint32_t instruction_size = sizeof(ComputeListSetPushConstantInstruction) + p_data_size;2039ComputeListSetPushConstantInstruction *instruction = reinterpret_cast<ComputeListSetPushConstantInstruction *>(_allocate_compute_list_instruction(instruction_size));2040instruction->type = ComputeListInstruction::TYPE_SET_PUSH_CONSTANT;2041instruction->size = p_data_size;2042instruction->shader = p_shader;2043memcpy(instruction->data(), p_data, p_data_size);2044}20452046void RenderingDeviceGraph::add_compute_list_uniform_set_prepare_for_use(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index) {2047ComputeListUniformSetPrepareForUseInstruction *instruction = reinterpret_cast<ComputeListUniformSetPrepareForUseInstruction *>(_allocate_compute_list_instruction(sizeof(ComputeListUniformSetPrepareForUseInstruction)));2048instruction->type = ComputeListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE;2049instruction->shader = p_shader;2050instruction->uniform_set = p_uniform_set;2051instruction->set_index = set_index;2052}20532054void RenderingDeviceGraph::add_compute_list_usage(ResourceTracker *p_tracker, ResourceUsage p_usage) {2055DEV_ASSERT(p_tracker != nullptr);20562057p_tracker->reset_if_outdated(tracking_frame);20582059if (p_tracker->compute_list_index != compute_instruction_list.index) {2060compute_instruction_list.command_trackers.push_back(p_tracker);2061compute_instruction_list.command_tracker_usages.push_back(p_usage);2062p_tracker->compute_list_index = compute_instruction_list.index;2063p_tracker->compute_list_usage = p_usage;2064}2065#ifdef DEV_ENABLED2066else if (p_tracker->compute_list_usage != p_usage) {2067ERR_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)));2068}2069#endif2070}20712072void RenderingDeviceGraph::add_compute_list_usages(VectorView<ResourceTracker *> p_trackers, VectorView<ResourceUsage> p_usages) {2073DEV_ASSERT(p_trackers.size() == p_usages.size());20742075for (uint32_t i = 0; i < p_trackers.size(); i++) {2076add_compute_list_usage(p_trackers[i], p_usages[i]);2077}2078}20792080void RenderingDeviceGraph::add_compute_list_end() {2081int32_t command_index;2082uint32_t instruction_data_size = compute_instruction_list.data.size();2083uint32_t command_size = sizeof(RecordedComputeListCommand) + instruction_data_size;2084RecordedComputeListCommand *command = static_cast<RecordedComputeListCommand *>(_allocate_command(command_size, command_index));2085command->type = RecordedCommand::TYPE_COMPUTE_LIST;2086command->self_stages = compute_instruction_list.stages;2087command->instruction_data_size = instruction_data_size;2088memcpy(command->instruction_data(), compute_instruction_list.data.ptr(), instruction_data_size);2089_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);2090}20912092void 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) {2093_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);2094}20952096void 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) {2097_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);2098}20992100void RenderingDeviceGraph::add_draw_list_bind_index_buffer(RDD::BufferID p_buffer, RDD::IndexBufferFormat p_format, uint32_t p_offset) {2101DrawListBindIndexBufferInstruction *instruction = reinterpret_cast<DrawListBindIndexBufferInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListBindIndexBufferInstruction)));2102instruction->type = DrawListInstruction::TYPE_BIND_INDEX_BUFFER;2103instruction->buffer = p_buffer;2104instruction->format = p_format;2105instruction->offset = p_offset;21062107if (instruction->buffer.id != 0) {2108draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_VERTEX_INPUT_BIT);2109}2110}21112112void RenderingDeviceGraph::add_draw_list_bind_pipeline(RDD::PipelineID p_pipeline, BitField<RDD::PipelineStageBits> p_pipeline_stage_bits) {2113DrawListBindPipelineInstruction *instruction = reinterpret_cast<DrawListBindPipelineInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListBindPipelineInstruction)));2114instruction->type = DrawListInstruction::TYPE_BIND_PIPELINE;2115instruction->pipeline = p_pipeline;2116draw_instruction_list.stages = draw_instruction_list.stages | p_pipeline_stage_bits;2117}21182119void RenderingDeviceGraph::add_draw_list_bind_uniform_set(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index) {2120add_draw_list_bind_uniform_sets(p_shader, VectorView(&p_uniform_set, 1), set_index, 1);2121}21222123void 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) {2124DEV_ASSERT(p_uniform_sets.size() >= p_set_count);21252126uint32_t instruction_size = sizeof(DrawListBindUniformSetsInstruction) + sizeof(RDD::UniformSetID) * p_set_count;2127DrawListBindUniformSetsInstruction *instruction = reinterpret_cast<DrawListBindUniformSetsInstruction *>(_allocate_draw_list_instruction(instruction_size));2128instruction->type = DrawListInstruction::TYPE_BIND_UNIFORM_SETS;2129instruction->shader = p_shader;2130instruction->first_set_index = p_first_index;2131instruction->set_count = p_set_count;2132instruction->dynamic_offsets_mask = driver->uniform_sets_get_dynamic_offsets(p_uniform_sets, p_shader, p_first_index, p_set_count);21332134for (uint32_t i = 0; i < p_set_count; i++) {2135instruction->uniform_set_ids()[i] = p_uniform_sets[i];2136}2137}21382139void RenderingDeviceGraph::add_draw_list_bind_vertex_buffers(Span<RDD::BufferID> p_vertex_buffers, Span<uint64_t> p_vertex_buffer_offsets) {2140DEV_ASSERT(p_vertex_buffers.size() == p_vertex_buffer_offsets.size());21412142uint32_t instruction_size = sizeof(DrawListBindVertexBuffersInstruction) + sizeof(RDD::BufferID) * p_vertex_buffers.size() + sizeof(uint64_t) * p_vertex_buffer_offsets.size();2143DrawListBindVertexBuffersInstruction *instruction = reinterpret_cast<DrawListBindVertexBuffersInstruction *>(_allocate_draw_list_instruction(instruction_size));2144instruction->type = DrawListInstruction::TYPE_BIND_VERTEX_BUFFERS;2145instruction->vertex_buffers_count = p_vertex_buffers.size();2146instruction->dynamic_offsets_mask = driver->buffer_get_dynamic_offsets(p_vertex_buffers);21472148RDD::BufferID *vertex_buffers = instruction->vertex_buffers();2149uint64_t *vertex_buffer_offsets = instruction->vertex_buffer_offsets();2150for (uint32_t i = 0; i < instruction->vertex_buffers_count; i++) {2151vertex_buffers[i] = p_vertex_buffers[i];2152vertex_buffer_offsets[i] = p_vertex_buffer_offsets[i];2153}21542155if (instruction->vertex_buffers_count > 0) {2156draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_VERTEX_INPUT_BIT);2157}2158}21592160void RenderingDeviceGraph::add_draw_list_clear_attachments(VectorView<RDD::AttachmentClear> p_attachments_clear, VectorView<Rect2i> p_attachments_clear_rect) {2161uint32_t instruction_size = sizeof(DrawListClearAttachmentsInstruction) + sizeof(RDD::AttachmentClear) * p_attachments_clear.size() + sizeof(Rect2i) * p_attachments_clear_rect.size();2162DrawListClearAttachmentsInstruction *instruction = reinterpret_cast<DrawListClearAttachmentsInstruction *>(_allocate_draw_list_instruction(instruction_size));2163instruction->type = DrawListInstruction::TYPE_CLEAR_ATTACHMENTS;2164instruction->attachments_clear_count = p_attachments_clear.size();2165instruction->attachments_clear_rect_count = p_attachments_clear_rect.size();21662167RDD::AttachmentClear *attachments_clear = instruction->attachments_clear();2168Rect2i *attachments_clear_rect = instruction->attachments_clear_rect();2169for (uint32_t i = 0; i < instruction->attachments_clear_count; i++) {2170attachments_clear[i] = p_attachments_clear[i];2171}21722173for (uint32_t i = 0; i < instruction->attachments_clear_rect_count; i++) {2174attachments_clear_rect[i] = p_attachments_clear_rect[i];2175}2176}21772178void RenderingDeviceGraph::add_draw_list_draw(uint32_t p_vertex_count, uint32_t p_instance_count) {2179DrawListDrawInstruction *instruction = reinterpret_cast<DrawListDrawInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListDrawInstruction)));2180instruction->type = DrawListInstruction::TYPE_DRAW;2181instruction->vertex_count = p_vertex_count;2182instruction->instance_count = p_instance_count;2183}21842185void RenderingDeviceGraph::add_draw_list_draw_indexed(uint32_t p_index_count, uint32_t p_instance_count, uint32_t p_first_index) {2186DrawListDrawIndexedInstruction *instruction = reinterpret_cast<DrawListDrawIndexedInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListDrawIndexedInstruction)));2187instruction->type = DrawListInstruction::TYPE_DRAW_INDEXED;2188instruction->index_count = p_index_count;2189instruction->instance_count = p_instance_count;2190instruction->first_index = p_first_index;2191}21922193void RenderingDeviceGraph::add_draw_list_draw_indirect(RDD::BufferID p_buffer, uint32_t p_offset, uint32_t p_draw_count, uint32_t p_stride) {2194DrawListDrawIndirectInstruction *instruction = reinterpret_cast<DrawListDrawIndirectInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListDrawIndirectInstruction)));2195instruction->type = DrawListInstruction::TYPE_DRAW_INDIRECT;2196instruction->buffer = p_buffer;2197instruction->offset = p_offset;2198instruction->draw_count = p_draw_count;2199instruction->stride = p_stride;2200draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_DRAW_INDIRECT_BIT);2201}22022203void RenderingDeviceGraph::add_draw_list_draw_indexed_indirect(RDD::BufferID p_buffer, uint32_t p_offset, uint32_t p_draw_count, uint32_t p_stride) {2204DrawListDrawIndexedIndirectInstruction *instruction = reinterpret_cast<DrawListDrawIndexedIndirectInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListDrawIndexedIndirectInstruction)));2205instruction->type = DrawListInstruction::TYPE_DRAW_INDEXED_INDIRECT;2206instruction->buffer = p_buffer;2207instruction->offset = p_offset;2208instruction->draw_count = p_draw_count;2209instruction->stride = p_stride;2210draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_DRAW_INDIRECT_BIT);2211}22122213void RenderingDeviceGraph::add_draw_list_execute_commands(RDD::CommandBufferID p_command_buffer) {2214DrawListExecuteCommandsInstruction *instruction = reinterpret_cast<DrawListExecuteCommandsInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListExecuteCommandsInstruction)));2215instruction->type = DrawListInstruction::TYPE_EXECUTE_COMMANDS;2216instruction->command_buffer = p_command_buffer;2217}22182219void RenderingDeviceGraph::add_draw_list_next_subpass(RDD::CommandBufferType p_command_buffer_type) {2220DrawListNextSubpassInstruction *instruction = reinterpret_cast<DrawListNextSubpassInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListNextSubpassInstruction)));2221instruction->type = DrawListInstruction::TYPE_NEXT_SUBPASS;2222instruction->command_buffer_type = p_command_buffer_type;2223}22242225void RenderingDeviceGraph::add_draw_list_set_blend_constants(const Color &p_color) {2226DrawListSetBlendConstantsInstruction *instruction = reinterpret_cast<DrawListSetBlendConstantsInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListSetBlendConstantsInstruction)));2227instruction->type = DrawListInstruction::TYPE_SET_BLEND_CONSTANTS;2228instruction->color = p_color;2229}22302231void RenderingDeviceGraph::add_draw_list_set_line_width(float p_width) {2232DrawListSetLineWidthInstruction *instruction = reinterpret_cast<DrawListSetLineWidthInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListSetLineWidthInstruction)));2233instruction->type = DrawListInstruction::TYPE_SET_LINE_WIDTH;2234instruction->width = p_width;2235}22362237void RenderingDeviceGraph::add_draw_list_set_push_constant(RDD::ShaderID p_shader, const void *p_data, uint32_t p_data_size) {2238uint32_t instruction_size = sizeof(DrawListSetPushConstantInstruction) + p_data_size;2239DrawListSetPushConstantInstruction *instruction = reinterpret_cast<DrawListSetPushConstantInstruction *>(_allocate_draw_list_instruction(instruction_size));2240instruction->type = DrawListInstruction::TYPE_SET_PUSH_CONSTANT;2241instruction->size = p_data_size;2242instruction->shader = p_shader;2243memcpy(instruction->data(), p_data, p_data_size);2244}22452246void RenderingDeviceGraph::add_draw_list_set_scissor(Rect2i p_rect) {2247DrawListSetScissorInstruction *instruction = reinterpret_cast<DrawListSetScissorInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListSetScissorInstruction)));2248instruction->type = DrawListInstruction::TYPE_SET_SCISSOR;2249instruction->rect = p_rect;2250}22512252void RenderingDeviceGraph::add_draw_list_set_viewport(Rect2i p_rect) {2253DrawListSetViewportInstruction *instruction = reinterpret_cast<DrawListSetViewportInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListSetViewportInstruction)));2254instruction->type = DrawListInstruction::TYPE_SET_VIEWPORT;2255instruction->rect = p_rect;2256}22572258void RenderingDeviceGraph::add_draw_list_uniform_set_prepare_for_use(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index) {2259DrawListUniformSetPrepareForUseInstruction *instruction = reinterpret_cast<DrawListUniformSetPrepareForUseInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListUniformSetPrepareForUseInstruction)));2260instruction->type = DrawListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE;2261instruction->shader = p_shader;2262instruction->uniform_set = p_uniform_set;2263instruction->set_index = set_index;2264}22652266void RenderingDeviceGraph::add_draw_list_usage(ResourceTracker *p_tracker, ResourceUsage p_usage) {2267p_tracker->reset_if_outdated(tracking_frame);22682269if (p_tracker->draw_list_index != draw_instruction_list.index) {2270draw_instruction_list.command_trackers.push_back(p_tracker);2271draw_instruction_list.command_tracker_usages.push_back(p_usage);2272p_tracker->draw_list_index = draw_instruction_list.index;2273p_tracker->draw_list_usage = p_usage;2274}2275#ifdef DEV_ENABLED2276else if (p_tracker->draw_list_usage != p_usage) {2277ERR_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)));2278}2279#endif2280}22812282void RenderingDeviceGraph::add_draw_list_usages(VectorView<ResourceTracker *> p_trackers, VectorView<ResourceUsage> p_usages) {2283DEV_ASSERT(p_trackers.size() == p_usages.size());22842285for (uint32_t i = 0; i < p_trackers.size(); i++) {2286add_draw_list_usage(p_trackers[i], p_usages[i]);2287}2288}22892290void RenderingDeviceGraph::add_draw_list_end() {2291FramebufferCache *framebuffer_cache = draw_instruction_list.framebuffer_cache;2292int32_t command_index;2293uint32_t clear_values_size = sizeof(RDD::RenderPassClearValue) * draw_instruction_list.attachment_clear_values.size();2294uint32_t trackers_count = framebuffer_cache != nullptr ? framebuffer_cache->trackers.size() : 0;2295uint32_t trackers_and_ops_size = (sizeof(ResourceTracker *) + sizeof(RDD::AttachmentLoadOp) + sizeof(RDD::AttachmentStoreOp)) * trackers_count;2296uint32_t instruction_data_size = draw_instruction_list.data.size();2297uint32_t command_size = sizeof(RecordedDrawListCommand) + clear_values_size + trackers_and_ops_size + instruction_data_size;2298RecordedDrawListCommand *command = static_cast<RecordedDrawListCommand *>(_allocate_command(command_size, command_index));2299command->type = RecordedCommand::TYPE_DRAW_LIST;2300command->self_stages = draw_instruction_list.stages;2301command->framebuffer_cache = framebuffer_cache;2302command->render_pass = draw_instruction_list.render_pass;2303command->framebuffer = draw_instruction_list.framebuffer;2304command->instruction_data_size = instruction_data_size;2305command->command_buffer_type = RDD::COMMAND_BUFFER_TYPE_PRIMARY;2306command->region = draw_instruction_list.region;2307#if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)2308command->breadcrumb = draw_instruction_list.breadcrumb;2309#endif2310command->split_cmd_buffer = draw_instruction_list.split_cmd_buffer;2311command->clear_values_count = draw_instruction_list.attachment_clear_values.size();2312command->trackers_count = trackers_count;23132314// 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.2315uint32_t attachment_op_count = draw_instruction_list.attachment_operations.size();2316ResourceTracker **trackers = command->trackers();2317RDD::AttachmentLoadOp *load_ops = command->load_ops();2318RDD::AttachmentStoreOp *store_ops = command->store_ops();2319for (uint32_t i = 0; i < command->trackers_count; i++) {2320ResourceTracker *resource_tracker = framebuffer_cache->trackers[i];2321if (resource_tracker != nullptr) {2322if (i < command->clear_values_count && i < attachment_op_count && draw_instruction_list.attachment_operations[i] == ATTACHMENT_OPERATION_CLEAR) {2323load_ops[i] = RDD::ATTACHMENT_LOAD_OP_CLEAR;2324} else if (i < attachment_op_count && draw_instruction_list.attachment_operations[i] == ATTACHMENT_OPERATION_IGNORE) {2325load_ops[i] = RDD::ATTACHMENT_LOAD_OP_DONT_CARE;2326} else if (resource_tracker->is_discardable) {2327bool resource_has_parent = resource_tracker->parent != nullptr;2328ResourceTracker *search_tracker = resource_has_parent ? resource_tracker->parent : resource_tracker;2329search_tracker->reset_if_outdated(tracking_frame);2330bool resource_was_modified_this_frame = search_tracker->write_command_or_list_index >= 0;2331load_ops[i] = resource_was_modified_this_frame ? RDD::ATTACHMENT_LOAD_OP_LOAD : RDD::ATTACHMENT_LOAD_OP_DONT_CARE;2332} else {2333load_ops[i] = RDD::ATTACHMENT_LOAD_OP_LOAD;2334}23352336store_ops[i] = resource_tracker->is_discardable ? RDD::ATTACHMENT_STORE_OP_DONT_CARE : RDD::ATTACHMENT_STORE_OP_STORE;2337} else {2338load_ops[i] = RDD::ATTACHMENT_LOAD_OP_DONT_CARE;2339store_ops[i] = RDD::ATTACHMENT_STORE_OP_DONT_CARE;2340}23412342trackers[i] = resource_tracker;2343}23442345RDD::RenderPassClearValue *clear_values = command->clear_values();2346for (uint32_t i = 0; i < command->clear_values_count; i++) {2347clear_values[i] = draw_instruction_list.attachment_clear_values[i];2348}23492350memcpy(command->instruction_data(), draw_instruction_list.data.ptr(), instruction_data_size);2351_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);2352}23532354void RenderingDeviceGraph::add_texture_clear_color(RDD::TextureID p_dst, ResourceTracker *p_dst_tracker, const Color &p_color, const RDD::TextureSubresourceRange &p_range) {2355DEV_ASSERT(p_dst_tracker != nullptr);23562357int32_t command_index;2358RecordedTextureClearColorCommand *command = static_cast<RecordedTextureClearColorCommand *>(_allocate_command(sizeof(RecordedTextureClearColorCommand), command_index));2359command->type = RecordedCommand::TYPE_TEXTURE_CLEAR_COLOR;2360command->texture = p_dst;2361command->color = p_color;2362command->range = p_range;23632364ResourceUsage usage;2365if (driver_clears_with_copy_engine) {2366command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;2367usage = RESOURCE_USAGE_COPY_TO;2368} else {2369// If the driver is uncapable of using the copy engine for clearing the image (e.g. D3D12), we must either transition the2370// resource to a render target or a storage image as that's the only two ways it can perform the operation.2371if (p_dst_tracker->texture_usage & RDD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT) {2372command->self_stages = RDD::PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;2373usage = RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE;2374} else {2375command->self_stages = RDD::PIPELINE_STAGE_CLEAR_STORAGE_BIT;2376usage = RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE;2377}2378}23792380_add_command_to_graph(&p_dst_tracker, &usage, 1, command_index, command);2381}23822383void RenderingDeviceGraph::add_texture_clear_depth_stencil(RDD::TextureID p_dst, ResourceTracker *p_dst_tracker, float p_depth, uint8_t p_stencil, const RDD::TextureSubresourceRange &p_range) {2384DEV_ASSERT(p_dst_tracker != nullptr);23852386int32_t command_index;2387RecordedTextureClearDepthStencilCommand *command = static_cast<RecordedTextureClearDepthStencilCommand *>(_allocate_command(sizeof(RecordedTextureClearDepthStencilCommand), command_index));2388command->type = RecordedCommand::TYPE_TEXTURE_CLEAR_DEPTH_STENCIL;2389command->texture = p_dst;2390command->depth = p_depth;2391command->stencil = p_stencil;2392command->range = p_range;23932394ResourceUsage usage;2395if (driver_clears_with_copy_engine) {2396command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;2397usage = RESOURCE_USAGE_COPY_TO;2398} else {2399// If the driver is uncapable of using the copy engine for clearing the image (e.g. D3D12), we must transition the2400// resource to a depth stencil as that's the only way it can perform the operation.2401command->self_stages = RDD::PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | RDD::PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;2402usage = RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE;2403}24042405_add_command_to_graph(&p_dst_tracker, &usage, 1, command_index, command);2406}24072408void 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) {2409DEV_ASSERT(p_src_tracker != nullptr);2410DEV_ASSERT(p_dst_tracker != nullptr);24112412int32_t command_index;2413uint64_t command_size = sizeof(RecordedTextureCopyCommand) + p_texture_copy_regions.size() * sizeof(RDD::TextureCopyRegion);2414RecordedTextureCopyCommand *command = static_cast<RecordedTextureCopyCommand *>(_allocate_command(command_size, command_index));2415command->type = RecordedCommand::TYPE_TEXTURE_COPY;2416command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;2417command->from_texture = p_src;2418command->to_texture = p_dst;2419command->texture_copy_regions_count = p_texture_copy_regions.size();24202421RDD::TextureCopyRegion *texture_copy_regions = command->texture_copy_regions();2422for (uint32_t i = 0; i < command->texture_copy_regions_count; i++) {2423texture_copy_regions[i] = p_texture_copy_regions[i];2424}24252426ResourceTracker *trackers[2] = { p_dst_tracker, p_src_tracker };2427ResourceUsage usages[2] = { RESOURCE_USAGE_COPY_TO, RESOURCE_USAGE_COPY_FROM };2428_add_command_to_graph(trackers, usages, 2, command_index, command);2429}24302431void 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) {2432DEV_ASSERT(p_src_tracker != nullptr);24332434int32_t command_index;2435uint64_t command_size = sizeof(RecordedTextureGetDataCommand) + p_buffer_texture_copy_regions.size() * sizeof(RDD::BufferTextureCopyRegion);2436RecordedTextureGetDataCommand *command = static_cast<RecordedTextureGetDataCommand *>(_allocate_command(command_size, command_index));2437command->type = RecordedCommand::TYPE_TEXTURE_GET_DATA;2438command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;2439command->from_texture = p_src;2440command->to_buffer = p_dst;2441command->buffer_texture_copy_regions_count = p_buffer_texture_copy_regions.size();24422443RDD::BufferTextureCopyRegion *buffer_texture_copy_regions = command->buffer_texture_copy_regions();2444for (uint32_t i = 0; i < command->buffer_texture_copy_regions_count; i++) {2445buffer_texture_copy_regions[i] = p_buffer_texture_copy_regions[i];2446}24472448if (p_dst_tracker != nullptr) {2449// Add the optional destination tracker if it was provided.2450ResourceTracker *trackers[2] = { p_dst_tracker, p_src_tracker };2451ResourceUsage usages[2] = { RESOURCE_USAGE_COPY_TO, RESOURCE_USAGE_COPY_FROM };2452_add_command_to_graph(trackers, usages, 2, command_index, command);2453} else {2454ResourceUsage usage = RESOURCE_USAGE_COPY_FROM;2455_add_command_to_graph(&p_src_tracker, &usage, 1, command_index, command);2456}2457}24582459void 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) {2460DEV_ASSERT(p_src_tracker != nullptr);2461DEV_ASSERT(p_dst_tracker != nullptr);24622463int32_t command_index;2464RecordedTextureResolveCommand *command = static_cast<RecordedTextureResolveCommand *>(_allocate_command(sizeof(RecordedTextureResolveCommand), command_index));2465command->type = RecordedCommand::TYPE_TEXTURE_RESOLVE;2466command->self_stages = RDD::PIPELINE_STAGE_RESOLVE_BIT;2467command->from_texture = p_src;2468command->to_texture = p_dst;2469command->src_layer = p_src_layer;2470command->src_mipmap = p_src_mipmap;2471command->dst_layer = p_dst_layer;2472command->dst_mipmap = p_dst_mipmap;24732474ResourceTracker *trackers[2] = { p_dst_tracker, p_src_tracker };2475ResourceUsage usages[2] = { RESOURCE_USAGE_RESOLVE_TO, RESOURCE_USAGE_RESOLVE_FROM };2476_add_command_to_graph(trackers, usages, 2, command_index, command);2477}24782479void RenderingDeviceGraph::add_texture_update(RDD::TextureID p_dst, ResourceTracker *p_dst_tracker, VectorView<RecordedBufferToTextureCopy> p_buffer_copies, VectorView<ResourceTracker *> p_buffer_trackers) {2480DEV_ASSERT(p_dst_tracker != nullptr);24812482int32_t command_index;2483uint64_t command_size = sizeof(RecordedTextureUpdateCommand) + p_buffer_copies.size() * sizeof(RecordedBufferToTextureCopy);2484RecordedTextureUpdateCommand *command = static_cast<RecordedTextureUpdateCommand *>(_allocate_command(command_size, command_index));2485command->type = RecordedCommand::TYPE_TEXTURE_UPDATE;2486command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;2487command->to_texture = p_dst;2488command->buffer_to_texture_copies_count = p_buffer_copies.size();24892490RecordedBufferToTextureCopy *buffer_to_texture_copies = command->buffer_to_texture_copies();2491for (uint32_t i = 0; i < command->buffer_to_texture_copies_count; i++) {2492buffer_to_texture_copies[i] = p_buffer_copies[i];2493}24942495if (p_buffer_trackers.size() > 0) {2496// Add the optional buffer trackers if they were provided.2497thread_local LocalVector<ResourceTracker *> trackers;2498thread_local LocalVector<ResourceUsage> usages;2499trackers.clear();2500usages.clear();2501for (uint32_t i = 0; i < p_buffer_trackers.size(); i++) {2502trackers.push_back(p_buffer_trackers[i]);2503usages.push_back(RESOURCE_USAGE_COPY_FROM);2504}25052506trackers.push_back(p_dst_tracker);2507usages.push_back(RESOURCE_USAGE_COPY_TO);25082509_add_command_to_graph(trackers.ptr(), usages.ptr(), trackers.size(), command_index, command);2510} else {2511ResourceUsage usage = RESOURCE_USAGE_COPY_TO;2512_add_command_to_graph(&p_dst_tracker, &usage, 1, command_index, command);2513}2514}25152516void RenderingDeviceGraph::add_capture_timestamp(RDD::QueryPoolID p_query_pool, uint32_t p_index) {2517int32_t command_index;2518RecordedCaptureTimestampCommand *command = static_cast<RecordedCaptureTimestampCommand *>(_allocate_command(sizeof(RecordedCaptureTimestampCommand), command_index));2519command->type = RecordedCommand::TYPE_CAPTURE_TIMESTAMP;2520command->self_stages = 0;2521command->pool = p_query_pool;2522command->index = p_index;2523_add_command_to_graph(nullptr, nullptr, 0, command_index, command);2524}25252526void RenderingDeviceGraph::add_synchronization() {2527// Synchronization is only acknowledged if commands have been recorded on the graph already.2528if (command_count > 0) {2529command_synchronization_pending = true;2530}2531}25322533void RenderingDeviceGraph::begin_label(const Span<char> &p_label_name, const Color &p_color) {2534uint32_t command_label_offset = command_label_chars.size();2535int command_label_size = p_label_name.size();2536command_label_chars.resize(command_label_offset + command_label_size + 1);2537memcpy(&command_label_chars[command_label_offset], p_label_name.ptr(), command_label_size);2538command_label_chars[command_label_offset + command_label_size] = '\0';2539command_label_colors.push_back(p_color);2540command_label_offsets.push_back(command_label_offset);2541command_label_index = command_label_count;2542command_label_count++;2543}25442545void RenderingDeviceGraph::end_label() {2546command_label_index = -1;2547}25482549void RenderingDeviceGraph::end(bool p_reorder_commands, bool p_full_barriers, RDD::CommandBufferID &r_command_buffer, CommandBufferPool &r_command_buffer_pool) {2550if (command_count == 0) {2551// No commands have been logged, do nothing.2552return;2553}25542555thread_local LocalVector<RecordedCommandSort> commands_sorted;2556if (p_reorder_commands) {2557thread_local LocalVector<int64_t> command_stack;2558thread_local LocalVector<int32_t> sorted_command_indices;2559thread_local LocalVector<uint32_t> command_degrees;2560int32_t adjacency_list_index = 0;2561int32_t command_index;25622563// Count all the incoming connections to every node by traversing their adjacency list.2564command_degrees.resize(command_count);2565memset(command_degrees.ptr(), 0, sizeof(uint32_t) * command_degrees.size());2566for (uint32_t i = 0; i < command_count; i++) {2567const RecordedCommand &recorded_command = *reinterpret_cast<const RecordedCommand *>(&command_data[command_data_offsets[i]]);2568adjacency_list_index = recorded_command.adjacent_command_list_index;2569while (adjacency_list_index >= 0) {2570const RecordedCommandListNode &command_list_node = command_list_nodes[adjacency_list_index];2571DEV_ASSERT((command_list_node.command_index != int32_t(i)) && "Command can't have itself as a dependency.");2572command_degrees[command_list_node.command_index] += 1;2573adjacency_list_index = command_list_node.next_list_index;2574}2575}25762577// Push to the stack all nodes that have no incoming connections.2578command_stack.clear();2579for (uint32_t i = 0; i < command_count; i++) {2580if (command_degrees[i] == 0) {2581command_stack.push_back(i);2582}2583}25842585sorted_command_indices.clear();2586while (!command_stack.is_empty()) {2587// Pop command from the stack.2588command_index = command_stack[command_stack.size() - 1];2589command_stack.resize(command_stack.size() - 1);25902591// Add it to the sorted commands.2592sorted_command_indices.push_back(command_index);25932594// Search for its adjacents and lower their degree for every visit. If the degree reaches zero, we push the command to the stack.2595const uint32_t command_data_offset = command_data_offsets[command_index];2596const RecordedCommand &recorded_command = *reinterpret_cast<const RecordedCommand *>(&command_data[command_data_offset]);2597adjacency_list_index = recorded_command.adjacent_command_list_index;2598while (adjacency_list_index >= 0) {2599const RecordedCommandListNode &command_list_node = command_list_nodes[adjacency_list_index];2600uint32_t &command_degree = command_degrees[command_list_node.command_index];2601DEV_ASSERT(command_degree > 0);2602command_degree--;2603if (command_degree == 0) {2604command_stack.push_back(command_list_node.command_index);2605}26062607adjacency_list_index = command_list_node.next_list_index;2608}2609}26102611// Batch buffer, texture, draw lists and compute operations together.2612const uint32_t PriorityTable[RecordedCommand::TYPE_MAX] = {26130, // TYPE_NONE26141, // TYPE_BUFFER_CLEAR26151, // TYPE_BUFFER_COPY26161, // TYPE_BUFFER_GET_DATA26171, // TYPE_BUFFER_UPDATE26184, // TYPE_COMPUTE_LIST26193, // TYPE_DRAW_LIST26202, // TYPE_TEXTURE_CLEAR_COLOR26212, // TYPE_TEXTURE_CLEAR_DEPTH_STENCIL26222, // TYPE_TEXTURE_COPY26232, // TYPE_TEXTURE_GET_DATA26242, // TYPE_TEXTURE_RESOLVE26252, // TYPE_TEXTURE_UPDATE26262, // TYPE_CAPTURE_TIMESTAMP26275, // TYPE_DRIVER_CALLBACK2628};26292630commands_sorted.clear();2631commands_sorted.resize(command_count);26322633for (uint32_t i = 0; i < command_count; i++) {2634const int32_t sorted_command_index = sorted_command_indices[i];2635const uint32_t command_data_offset = command_data_offsets[sorted_command_index];2636const RecordedCommand recorded_command = *reinterpret_cast<const RecordedCommand *>(&command_data[command_data_offset]);2637const uint32_t next_command_level = commands_sorted[sorted_command_index].level + 1;2638adjacency_list_index = recorded_command.adjacent_command_list_index;2639while (adjacency_list_index >= 0) {2640const RecordedCommandListNode &command_list_node = command_list_nodes[adjacency_list_index];2641uint32_t &adjacent_command_level = commands_sorted[command_list_node.command_index].level;2642if (adjacent_command_level < next_command_level) {2643adjacent_command_level = next_command_level;2644}26452646adjacency_list_index = command_list_node.next_list_index;2647}26482649commands_sorted[sorted_command_index].index = sorted_command_index;2650commands_sorted[sorted_command_index].priority = PriorityTable[recorded_command.type];2651}2652} else {2653commands_sorted.clear();2654commands_sorted.resize(command_count);26552656for (uint32_t i = 0; i < command_count; i++) {2657commands_sorted[i].index = i;2658}2659}26602661_wait_for_secondary_command_buffer_tasks();26622663if (command_count > 0) {2664int32_t current_label_index = -1;2665int32_t current_label_level = -1;2666_run_label_command_change(r_command_buffer, -1, -1, true, true, nullptr, 0, current_label_index, current_label_level);26672668if (device.workarounds.avoid_compute_after_draw) {2669// Reset the state of the workaround.2670workarounds_state.draw_list_found = false;2671}26722673#if PRINT_DRAW_LIST_STATS2674draw_list_total_size = 0;2675#endif26762677if (p_reorder_commands) {2678#if PRINT_RENDER_GRAPH2679print_line("BEFORE SORT");2680_print_render_commands(commands_sorted.ptr(), command_count);2681#endif26822683commands_sorted.sort();26842685#if PRINT_RENDER_GRAPH2686print_line("AFTER SORT");2687_print_render_commands(commands_sorted.ptr(), command_count);2688#endif26892690#if PRINT_COMMAND_RECORDING2691print_line(vformat("Recording %d commands", command_count));2692#endif26932694uint32_t boosted_priority = 0;2695uint32_t current_level = commands_sorted[0].level;2696uint32_t current_level_start = 0;2697for (uint32_t i = 0; i < command_count; i++) {2698if (current_level != commands_sorted[i].level) {2699RecordedCommandSort *level_command_ptr = &commands_sorted[current_level_start];2700uint32_t level_command_count = i - current_level_start;2701_boost_priority_for_render_commands(level_command_ptr, level_command_count, boosted_priority);2702_group_barriers_for_render_commands(r_command_buffer, level_command_ptr, level_command_count, p_full_barriers);2703_run_render_commands(current_level, level_command_ptr, level_command_count, r_command_buffer, r_command_buffer_pool, current_label_index, current_label_level);2704current_level = commands_sorted[i].level;2705current_level_start = i;2706}2707}27082709RecordedCommandSort *level_command_ptr = &commands_sorted[current_level_start];2710uint32_t level_command_count = command_count - current_level_start;2711_boost_priority_for_render_commands(level_command_ptr, level_command_count, boosted_priority);2712_group_barriers_for_render_commands(r_command_buffer, level_command_ptr, level_command_count, p_full_barriers);2713_run_render_commands(current_level, level_command_ptr, level_command_count, r_command_buffer, r_command_buffer_pool, current_label_index, current_label_level);27142715#if PRINT_RENDER_GRAPH2716print_line("COMMANDS", command_count, "LEVELS", current_level + 1);2717#endif2718} else {2719for (uint32_t i = 0; i < command_count; i++) {2720_group_barriers_for_render_commands(r_command_buffer, &commands_sorted[i], 1, p_full_barriers);2721_run_render_commands(i, &commands_sorted[i], 1, r_command_buffer, r_command_buffer_pool, current_label_index, current_label_level);2722}2723}27242725_run_label_command_change(r_command_buffer, -1, -1, false, false, nullptr, 0, current_label_index, current_label_level);27262727#if PRINT_DRAW_LIST_STATS2728print_line(vformat("Draw list %d bytes", draw_list_total_size));2729#endif2730#if PRINT_COMMAND_RECORDING2731print_line(vformat("Recorded %d commands", command_count));2732#endif2733}27342735// 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.2736frame = (frame + 1) % frames.size();2737}27382739#if PRINT_RESOURCE_TRACKER_TOTAL2740static uint32_t resource_tracker_total = 0;2741#endif27422743RenderingDeviceGraph::ResourceTracker *RenderingDeviceGraph::resource_tracker_create() {2744#if PRINT_RESOURCE_TRACKER_TOTAL2745print_line("Resource trackers:", ++resource_tracker_total);2746#endif2747return memnew(ResourceTracker);2748}27492750void RenderingDeviceGraph::resource_tracker_free(ResourceTracker *p_tracker) {2751if (p_tracker == nullptr) {2752return;2753}27542755if (p_tracker->in_parent_dirty_list) {2756// Delete the tracker from the parent's dirty linked list.2757if (p_tracker->parent->dirty_shared_list == p_tracker) {2758p_tracker->parent->dirty_shared_list = p_tracker->next_shared;2759} else {2760ResourceTracker *node = p_tracker->parent->dirty_shared_list;2761while (node != nullptr) {2762if (node->next_shared == p_tracker) {2763node->next_shared = p_tracker->next_shared;2764node = nullptr;2765} else {2766node = node->next_shared;2767}2768}2769}2770}27712772memdelete(p_tracker);27732774#if PRINT_RESOURCE_TRACKER_TOTAL2775print_line("Resource trackers:", --resource_tracker_total);2776#endif2777}27782779RenderingDeviceGraph::FramebufferCache *RenderingDeviceGraph::framebuffer_cache_create() {2780return memnew(FramebufferCache);2781}27822783void RenderingDeviceGraph::framebuffer_cache_free(RDD *p_driver, FramebufferCache *p_cache) {2784DEV_ASSERT(p_driver != nullptr);27852786if (p_cache == nullptr) {2787return;2788}27892790for (KeyValue<uint64_t, FramebufferStorage> &E : p_cache->storage_map) {2791p_driver->framebuffer_free(E.value.framebuffer);2792p_driver->render_pass_free(E.value.render_pass);2793}27942795memdelete(p_cache);2796}279727982799