rendering_device_graph.cpp 120 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197
  1. /**************************************************************************/
  2. /* rendering_device_graph.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "rendering_device_graph.h"
  31. #define PRINT_RENDER_GRAPH 0
  32. #define FORCE_FULL_ACCESS_BITS 0
  33. #define PRINT_RESOURCE_TRACKER_TOTAL 0
  34. #define PRINT_COMMAND_RECORDING 0
  35. RenderingDeviceGraph::RenderingDeviceGraph() {
  36. driver_honors_barriers = false;
  37. driver_clears_with_copy_engine = false;
  38. }
  39. RenderingDeviceGraph::~RenderingDeviceGraph() {
  40. }
  41. bool RenderingDeviceGraph::_is_write_usage(ResourceUsage p_usage) {
  42. switch (p_usage) {
  43. case RESOURCE_USAGE_COPY_FROM:
  44. case RESOURCE_USAGE_RESOLVE_FROM:
  45. case RESOURCE_USAGE_UNIFORM_BUFFER_READ:
  46. case RESOURCE_USAGE_INDIRECT_BUFFER_READ:
  47. case RESOURCE_USAGE_TEXTURE_BUFFER_READ:
  48. case RESOURCE_USAGE_STORAGE_BUFFER_READ:
  49. case RESOURCE_USAGE_VERTEX_BUFFER_READ:
  50. case RESOURCE_USAGE_INDEX_BUFFER_READ:
  51. case RESOURCE_USAGE_TEXTURE_SAMPLE:
  52. case RESOURCE_USAGE_STORAGE_IMAGE_READ:
  53. return false;
  54. case RESOURCE_USAGE_COPY_TO:
  55. case RESOURCE_USAGE_RESOLVE_TO:
  56. case RESOURCE_USAGE_TEXTURE_BUFFER_READ_WRITE:
  57. case RESOURCE_USAGE_STORAGE_BUFFER_READ_WRITE:
  58. case RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE:
  59. case RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE:
  60. case RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE:
  61. return true;
  62. default:
  63. DEV_ASSERT(false && "Invalid resource tracker usage.");
  64. return false;
  65. }
  66. }
  67. RDD::TextureLayout RenderingDeviceGraph::_usage_to_image_layout(ResourceUsage p_usage) {
  68. switch (p_usage) {
  69. case RESOURCE_USAGE_COPY_FROM:
  70. return RDD::TEXTURE_LAYOUT_COPY_SRC_OPTIMAL;
  71. case RESOURCE_USAGE_COPY_TO:
  72. return RDD::TEXTURE_LAYOUT_COPY_DST_OPTIMAL;
  73. case RESOURCE_USAGE_RESOLVE_FROM:
  74. return RDD::TEXTURE_LAYOUT_RESOLVE_SRC_OPTIMAL;
  75. case RESOURCE_USAGE_RESOLVE_TO:
  76. return RDD::TEXTURE_LAYOUT_RESOLVE_DST_OPTIMAL;
  77. case RESOURCE_USAGE_TEXTURE_SAMPLE:
  78. return RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  79. case RESOURCE_USAGE_STORAGE_IMAGE_READ:
  80. case RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE:
  81. return RDD::TEXTURE_LAYOUT_STORAGE_OPTIMAL;
  82. case RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE:
  83. return RDD::TEXTURE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
  84. case RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE:
  85. return RDD::TEXTURE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
  86. case RESOURCE_USAGE_NONE:
  87. return RDD::TEXTURE_LAYOUT_UNDEFINED;
  88. default:
  89. DEV_ASSERT(false && "Invalid resource tracker usage or not an image usage.");
  90. return RDD::TEXTURE_LAYOUT_UNDEFINED;
  91. }
  92. }
  93. RDD::BarrierAccessBits RenderingDeviceGraph::_usage_to_access_bits(ResourceUsage p_usage) {
  94. #if FORCE_FULL_ACCESS_BITS
  95. return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_MEMORY_READ_BIT | RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT);
  96. #else
  97. switch (p_usage) {
  98. case RESOURCE_USAGE_NONE:
  99. return RDD::BarrierAccessBits(0);
  100. case RESOURCE_USAGE_COPY_FROM:
  101. return RDD::BARRIER_ACCESS_COPY_READ_BIT;
  102. case RESOURCE_USAGE_COPY_TO:
  103. return RDD::BARRIER_ACCESS_COPY_WRITE_BIT;
  104. case RESOURCE_USAGE_RESOLVE_FROM:
  105. return RDD::BARRIER_ACCESS_RESOLVE_READ_BIT;
  106. case RESOURCE_USAGE_RESOLVE_TO:
  107. return RDD::BARRIER_ACCESS_RESOLVE_WRITE_BIT;
  108. case RESOURCE_USAGE_UNIFORM_BUFFER_READ:
  109. return RDD::BARRIER_ACCESS_UNIFORM_READ_BIT;
  110. case RESOURCE_USAGE_INDIRECT_BUFFER_READ:
  111. return RDD::BARRIER_ACCESS_INDIRECT_COMMAND_READ_BIT;
  112. case RESOURCE_USAGE_STORAGE_BUFFER_READ:
  113. case RESOURCE_USAGE_STORAGE_IMAGE_READ:
  114. case RESOURCE_USAGE_TEXTURE_BUFFER_READ:
  115. case RESOURCE_USAGE_TEXTURE_SAMPLE:
  116. return RDD::BARRIER_ACCESS_SHADER_READ_BIT;
  117. case RESOURCE_USAGE_TEXTURE_BUFFER_READ_WRITE:
  118. case RESOURCE_USAGE_STORAGE_BUFFER_READ_WRITE:
  119. case RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE:
  120. return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_SHADER_READ_BIT | RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  121. case RESOURCE_USAGE_VERTEX_BUFFER_READ:
  122. return RDD::BARRIER_ACCESS_VERTEX_ATTRIBUTE_READ_BIT;
  123. case RESOURCE_USAGE_INDEX_BUFFER_READ:
  124. return RDD::BARRIER_ACCESS_INDEX_READ_BIT;
  125. case RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE:
  126. return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_READ_BIT | RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_WRITE_BIT);
  127. case RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE:
  128. return RDD::BarrierAccessBits(RDD::BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | RDD::BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT);
  129. default:
  130. DEV_ASSERT(false && "Invalid usage.");
  131. return RDD::BarrierAccessBits(0);
  132. }
  133. #endif
  134. }
  135. bool RenderingDeviceGraph::_check_command_intersection(ResourceTracker *p_resource_tracker, int32_t p_previous_command_index, int32_t p_command_index) const {
  136. if (p_resource_tracker->usage != RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE && p_resource_tracker->usage != RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE) {
  137. // We don't check possible intersections for usages that aren't consecutive color or depth writes.
  138. return true;
  139. }
  140. const uint32_t previous_command_data_offset = command_data_offsets[p_previous_command_index];
  141. const uint32_t current_command_data_offset = command_data_offsets[p_command_index];
  142. const RecordedDrawListCommand &previous_draw_list_command = *reinterpret_cast<const RecordedDrawListCommand *>(&command_data[previous_command_data_offset]);
  143. const RecordedDrawListCommand &current_draw_list_command = *reinterpret_cast<const RecordedDrawListCommand *>(&command_data[current_command_data_offset]);
  144. if (previous_draw_list_command.type != RecordedCommand::TYPE_DRAW_LIST || current_draw_list_command.type != RecordedCommand::TYPE_DRAW_LIST) {
  145. // We don't check possible intersections if both commands aren't draw lists.
  146. return true;
  147. }
  148. // We check if the region used by both draw lists have an intersection.
  149. return previous_draw_list_command.region.intersects(current_draw_list_command.region);
  150. }
  151. int32_t RenderingDeviceGraph::_add_to_command_list(int32_t p_command_index, int32_t p_list_index) {
  152. DEV_ASSERT(p_command_index < int32_t(command_count));
  153. DEV_ASSERT(p_list_index < int32_t(command_list_nodes.size()));
  154. int32_t next_index = int32_t(command_list_nodes.size());
  155. command_list_nodes.resize(next_index + 1);
  156. RecordedCommandListNode &new_node = command_list_nodes[next_index];
  157. new_node.command_index = p_command_index;
  158. new_node.next_list_index = p_list_index;
  159. return next_index;
  160. }
  161. void RenderingDeviceGraph::_add_adjacent_command(int32_t p_previous_command_index, int32_t p_command_index, RecordedCommand *r_command) {
  162. const uint32_t previous_command_data_offset = command_data_offsets[p_previous_command_index];
  163. RecordedCommand &previous_command = *reinterpret_cast<RecordedCommand *>(&command_data[previous_command_data_offset]);
  164. previous_command.adjacent_command_list_index = _add_to_command_list(p_command_index, previous_command.adjacent_command_list_index);
  165. previous_command.next_stages = previous_command.next_stages | r_command->self_stages;
  166. r_command->previous_stages = r_command->previous_stages | previous_command.self_stages;
  167. }
  168. int32_t RenderingDeviceGraph::_add_to_slice_read_list(int32_t p_command_index, Rect2i p_subresources, int32_t p_list_index) {
  169. DEV_ASSERT(p_command_index < int32_t(command_count));
  170. DEV_ASSERT(p_list_index < int32_t(read_slice_list_nodes.size()));
  171. int32_t next_index = int32_t(read_slice_list_nodes.size());
  172. read_slice_list_nodes.resize(next_index + 1);
  173. RecordedSliceListNode &new_node = read_slice_list_nodes[next_index];
  174. new_node.command_index = p_command_index;
  175. new_node.next_list_index = p_list_index;
  176. new_node.subresources = p_subresources;
  177. return next_index;
  178. }
  179. int32_t RenderingDeviceGraph::_add_to_write_list(int32_t p_command_index, Rect2i p_subresources, int32_t p_list_index) {
  180. DEV_ASSERT(p_command_index < int32_t(command_count));
  181. DEV_ASSERT(p_list_index < int32_t(write_slice_list_nodes.size()));
  182. int32_t next_index = int32_t(write_slice_list_nodes.size());
  183. write_slice_list_nodes.resize(next_index + 1);
  184. RecordedSliceListNode &new_node = write_slice_list_nodes[next_index];
  185. new_node.command_index = p_command_index;
  186. new_node.next_list_index = p_list_index;
  187. new_node.subresources = p_subresources;
  188. return next_index;
  189. }
  190. RenderingDeviceGraph::RecordedCommand *RenderingDeviceGraph::_allocate_command(uint32_t p_command_size, int32_t &r_command_index) {
  191. uint32_t command_data_offset = command_data.size();
  192. command_data_offsets.push_back(command_data_offset);
  193. command_data.resize(command_data_offset + p_command_size);
  194. r_command_index = command_count++;
  195. RecordedCommand *new_command = reinterpret_cast<RecordedCommand *>(&command_data[command_data_offset]);
  196. *new_command = RecordedCommand();
  197. return new_command;
  198. }
  199. RenderingDeviceGraph::DrawListInstruction *RenderingDeviceGraph::_allocate_draw_list_instruction(uint32_t p_instruction_size) {
  200. uint32_t draw_list_data_offset = draw_instruction_list.data.size();
  201. draw_instruction_list.data.resize(draw_list_data_offset + p_instruction_size);
  202. return reinterpret_cast<DrawListInstruction *>(&draw_instruction_list.data[draw_list_data_offset]);
  203. }
  204. RenderingDeviceGraph::ComputeListInstruction *RenderingDeviceGraph::_allocate_compute_list_instruction(uint32_t p_instruction_size) {
  205. uint32_t compute_list_data_offset = compute_instruction_list.data.size();
  206. compute_instruction_list.data.resize(compute_list_data_offset + p_instruction_size);
  207. return reinterpret_cast<ComputeListInstruction *>(&compute_instruction_list.data[compute_list_data_offset]);
  208. }
  209. void RenderingDeviceGraph::_add_command_to_graph(ResourceTracker **p_resource_trackers, ResourceUsage *p_resource_usages, uint32_t p_resource_count, int32_t p_command_index, RecordedCommand *r_command) {
  210. // Assign the next stages derived from the stages the command requires first.
  211. r_command->next_stages = r_command->self_stages;
  212. if (command_label_index >= 0) {
  213. // If a label is active, tag the command with the label.
  214. r_command->label_index = command_label_index;
  215. }
  216. if (r_command->type == RecordedCommand::TYPE_CAPTURE_TIMESTAMP) {
  217. // All previous commands starting from the previous timestamp should be adjacent to this command.
  218. int32_t start_command_index = uint32_t(MAX(command_timestamp_index, 0));
  219. for (int32_t i = start_command_index; i < p_command_index; i++) {
  220. _add_adjacent_command(i, p_command_index, r_command);
  221. }
  222. // Make this command the new active timestamp command.
  223. command_timestamp_index = p_command_index;
  224. } else if (command_timestamp_index >= 0) {
  225. // Timestamp command should be adjacent to this command.
  226. _add_adjacent_command(command_timestamp_index, p_command_index, r_command);
  227. }
  228. if (command_synchronization_pending) {
  229. // All previous commands should be adjacent to this command.
  230. int32_t start_command_index = uint32_t(MAX(command_synchronization_index, 0));
  231. for (int32_t i = start_command_index; i < p_command_index; i++) {
  232. _add_adjacent_command(i, p_command_index, r_command);
  233. }
  234. command_synchronization_index = p_command_index;
  235. command_synchronization_pending = false;
  236. } else if (command_synchronization_index >= 0) {
  237. // Synchronization command should be adjacent to this command.
  238. _add_adjacent_command(command_synchronization_index, p_command_index, r_command);
  239. }
  240. for (uint32_t i = 0; i < p_resource_count; i++) {
  241. ResourceTracker *resource_tracker = p_resource_trackers[i];
  242. DEV_ASSERT(resource_tracker != nullptr);
  243. resource_tracker->reset_if_outdated(tracking_frame);
  244. const RDD::TextureSubresourceRange &subresources = resource_tracker->texture_subresources;
  245. const Rect2i resource_tracker_rect(subresources.base_mipmap, subresources.base_layer, subresources.mipmap_count, subresources.layer_count);
  246. Rect2i search_tracker_rect = resource_tracker_rect;
  247. ResourceUsage new_resource_usage = p_resource_usages[i];
  248. bool write_usage = _is_write_usage(new_resource_usage);
  249. BitField<RDD::BarrierAccessBits> new_usage_access = _usage_to_access_bits(new_resource_usage);
  250. bool is_resource_a_slice = resource_tracker->parent != nullptr;
  251. if (is_resource_a_slice) {
  252. // This resource depends on a parent resource.
  253. resource_tracker->parent->reset_if_outdated(tracking_frame);
  254. if (resource_tracker->texture_slice_command_index != p_command_index) {
  255. // Indicate this slice has been used by this command.
  256. resource_tracker->texture_slice_command_index = p_command_index;
  257. }
  258. if (resource_tracker->parent->usage == RESOURCE_USAGE_NONE) {
  259. if (resource_tracker->parent->texture_driver_id.id != 0) {
  260. // If the resource is a texture, we transition it entirely to the layout determined by the first slice that uses it.
  261. _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);
  262. }
  263. // If the parent hasn't been used yet, we assign the usage of the slice to the entire resource.
  264. resource_tracker->parent->usage = new_resource_usage;
  265. // Also assign the usage to the slice and consider it a write operation. Consider the parent's current usage access as its own.
  266. resource_tracker->usage = new_resource_usage;
  267. resource_tracker->usage_access = resource_tracker->parent->usage_access;
  268. write_usage = true;
  269. // Indicate the area that should be tracked is the entire resource.
  270. const RDD::TextureSubresourceRange &parent_subresources = resource_tracker->parent->texture_subresources;
  271. search_tracker_rect = Rect2i(parent_subresources.base_mipmap, parent_subresources.base_layer, parent_subresources.mipmap_count, parent_subresources.layer_count);
  272. } else if (resource_tracker->in_parent_dirty_list) {
  273. if (resource_tracker->parent->usage == new_resource_usage) {
  274. // The slice will be transitioned to the resource of the parent and can be deleted from the dirty list.
  275. ResourceTracker *previous_tracker = nullptr;
  276. ResourceTracker *current_tracker = resource_tracker->parent->dirty_shared_list;
  277. bool initialized_dirty_rect = false;
  278. while (current_tracker != nullptr) {
  279. current_tracker->reset_if_outdated(tracking_frame);
  280. if (current_tracker == resource_tracker) {
  281. current_tracker->in_parent_dirty_list = false;
  282. if (previous_tracker != nullptr) {
  283. previous_tracker->next_shared = current_tracker->next_shared;
  284. } else {
  285. resource_tracker->parent->dirty_shared_list = current_tracker->next_shared;
  286. }
  287. current_tracker = current_tracker->next_shared;
  288. } else {
  289. if (initialized_dirty_rect) {
  290. resource_tracker->parent->texture_slice_or_dirty_rect = resource_tracker->parent->texture_slice_or_dirty_rect.merge(current_tracker->texture_slice_or_dirty_rect);
  291. } else {
  292. resource_tracker->parent->texture_slice_or_dirty_rect = current_tracker->texture_slice_or_dirty_rect;
  293. initialized_dirty_rect = true;
  294. }
  295. previous_tracker = current_tracker;
  296. current_tracker = current_tracker->next_shared;
  297. }
  298. }
  299. }
  300. } else {
  301. if (resource_tracker->parent->dirty_shared_list != nullptr && resource_tracker->parent->texture_slice_or_dirty_rect.intersects(resource_tracker->texture_slice_or_dirty_rect)) {
  302. // There's an intersection with the current dirty area of the parent and the slice. We must verify if the intersection is against a slice
  303. // 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.
  304. ResourceTracker *previous_tracker = nullptr;
  305. ResourceTracker *current_tracker = resource_tracker->parent->dirty_shared_list;
  306. bool initialized_dirty_rect = false;
  307. while (current_tracker != nullptr) {
  308. current_tracker->reset_if_outdated(tracking_frame);
  309. if (current_tracker->texture_slice_or_dirty_rect.intersects(resource_tracker->texture_slice_or_dirty_rect)) {
  310. if (current_tracker->command_frame == tracking_frame && current_tracker->texture_slice_command_index == p_command_index) {
  311. ERR_FAIL_MSG("Texture slices that overlap can't be used in the same command.");
  312. } else {
  313. // Delete the slice from the dirty list and revert it to the usage of the parent.
  314. if (current_tracker->texture_driver_id.id != 0) {
  315. _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);
  316. // Merge the area of the slice with the current tracking area of the command and indicate it's a write usage as well.
  317. search_tracker_rect = search_tracker_rect.merge(current_tracker->texture_slice_or_dirty_rect);
  318. write_usage = true;
  319. }
  320. current_tracker->in_parent_dirty_list = false;
  321. if (previous_tracker != nullptr) {
  322. previous_tracker->next_shared = current_tracker->next_shared;
  323. } else {
  324. resource_tracker->parent->dirty_shared_list = current_tracker->next_shared;
  325. }
  326. current_tracker = current_tracker->next_shared;
  327. }
  328. } else {
  329. // Recalculate the dirty rect of the parent so the deleted slices are excluded.
  330. if (initialized_dirty_rect) {
  331. resource_tracker->parent->texture_slice_or_dirty_rect = resource_tracker->parent->texture_slice_or_dirty_rect.merge(current_tracker->texture_slice_or_dirty_rect);
  332. } else {
  333. resource_tracker->parent->texture_slice_or_dirty_rect = current_tracker->texture_slice_or_dirty_rect;
  334. initialized_dirty_rect = true;
  335. }
  336. previous_tracker = current_tracker;
  337. current_tracker = current_tracker->next_shared;
  338. }
  339. }
  340. }
  341. // 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.
  342. resource_tracker->usage = resource_tracker->parent->usage;
  343. resource_tracker->usage_access = resource_tracker->parent->usage_access;
  344. if (resource_tracker->usage != new_resource_usage) {
  345. // Insert to the dirty list if the requested usage is different.
  346. resource_tracker->next_shared = resource_tracker->parent->dirty_shared_list;
  347. resource_tracker->parent->dirty_shared_list = resource_tracker;
  348. resource_tracker->in_parent_dirty_list = true;
  349. if (resource_tracker->parent->dirty_shared_list != nullptr) {
  350. resource_tracker->parent->texture_slice_or_dirty_rect = resource_tracker->parent->texture_slice_or_dirty_rect.merge(resource_tracker->texture_slice_or_dirty_rect);
  351. } else {
  352. resource_tracker->parent->texture_slice_or_dirty_rect = resource_tracker->texture_slice_or_dirty_rect;
  353. }
  354. }
  355. }
  356. } else {
  357. ResourceTracker *current_tracker = resource_tracker->dirty_shared_list;
  358. if (current_tracker != nullptr) {
  359. // Consider the usage as write if we must transition any of the slices.
  360. write_usage = true;
  361. }
  362. while (current_tracker != nullptr) {
  363. current_tracker->reset_if_outdated(tracking_frame);
  364. if (current_tracker->texture_driver_id.id != 0) {
  365. // Transition all slices to the layout of the parent resource.
  366. _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);
  367. }
  368. current_tracker->in_parent_dirty_list = false;
  369. current_tracker = current_tracker->next_shared;
  370. }
  371. resource_tracker->dirty_shared_list = nullptr;
  372. }
  373. // Use the resource's parent tracker directly for all search operations.
  374. bool resource_has_parent = resource_tracker->parent != nullptr;
  375. ResourceTracker *search_tracker = resource_has_parent ? resource_tracker->parent : resource_tracker;
  376. bool different_usage = resource_tracker->usage != new_resource_usage;
  377. bool write_usage_after_write = (write_usage && search_tracker->write_command_or_list_index >= 0);
  378. if (different_usage || write_usage_after_write) {
  379. // 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.
  380. if (resource_tracker->texture_driver_id.id != 0) {
  381. if (resource_tracker->usage_access.is_empty()) {
  382. // FIXME: If the tracker does not know the previous type of usage, assume the generic memory write one.
  383. // Tracking access bits across texture slices can be tricky, so this failsafe can be removed once that's improved.
  384. resource_tracker->usage_access = RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT;
  385. }
  386. _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);
  387. } else if (resource_tracker->buffer_driver_id.id != 0) {
  388. #if USE_BUFFER_BARRIERS
  389. _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);
  390. #endif
  391. // Memory barriers are pushed regardless of buffer barriers being used or not.
  392. r_command->memory_barrier.src_access = r_command->memory_barrier.src_access | resource_tracker->usage_access;
  393. r_command->memory_barrier.dst_access = r_command->memory_barrier.dst_access | new_usage_access;
  394. } else {
  395. DEV_ASSERT(false && "Resource tracker does not contain a valid buffer or texture ID.");
  396. }
  397. }
  398. // Always update the access of the tracker according to the latest usage.
  399. resource_tracker->usage_access = new_usage_access;
  400. // Always accumulate the stages of the tracker with the commands that use it.
  401. search_tracker->current_frame_stages = search_tracker->current_frame_stages | r_command->self_stages;
  402. if (!search_tracker->previous_frame_stages.is_empty()) {
  403. // Add to the command the stages the tracker was used on in the previous frame.
  404. r_command->previous_stages = r_command->previous_stages | search_tracker->previous_frame_stages;
  405. search_tracker->previous_frame_stages.clear();
  406. }
  407. if (different_usage) {
  408. // 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.
  409. // In the case of buffers however, this is not exactly necessary if the driver does not consider different buffer usages as different states.
  410. write_usage = write_usage || bool(resource_tracker->texture_driver_id) || driver_buffers_require_transitions;
  411. resource_tracker->usage = new_resource_usage;
  412. }
  413. bool command_intersection_failed = false;
  414. if (search_tracker->write_command_or_list_index >= 0) {
  415. if (search_tracker->write_command_list_enabled) {
  416. // Make this command adjacent to any commands that wrote to this resource and intersect with the slice if it applies.
  417. // For buffers or textures that never use slices, this list will only be one element long at most.
  418. int32_t previous_write_list_index = -1;
  419. int32_t write_list_index = search_tracker->write_command_or_list_index;
  420. while (write_list_index >= 0) {
  421. const RecordedSliceListNode &write_list_node = write_slice_list_nodes[write_list_index];
  422. if (!resource_has_parent || search_tracker_rect.intersects(write_list_node.subresources)) {
  423. if (write_list_node.command_index == p_command_index) {
  424. ERR_FAIL_COND_MSG(!resource_has_parent, "Command can't have itself as a dependency.");
  425. } else if (_check_command_intersection(resource_tracker, write_list_node.command_index, p_command_index)) {
  426. // Command is dependent on this command. Add this command to the adjacency list of the write command.
  427. _add_adjacent_command(write_list_node.command_index, p_command_index, r_command);
  428. if (resource_has_parent && write_usage && search_tracker_rect.encloses(write_list_node.subresources)) {
  429. // Eliminate redundant writes from the list.
  430. if (previous_write_list_index >= 0) {
  431. RecordedSliceListNode &previous_list_node = write_slice_list_nodes[previous_write_list_index];
  432. previous_list_node.next_list_index = write_list_node.next_list_index;
  433. } else {
  434. search_tracker->write_command_or_list_index = write_list_node.next_list_index;
  435. }
  436. write_list_index = write_list_node.next_list_index;
  437. continue;
  438. }
  439. } else {
  440. command_intersection_failed = true;
  441. }
  442. }
  443. previous_write_list_index = write_list_index;
  444. write_list_index = write_list_node.next_list_index;
  445. }
  446. } else {
  447. // The index is just the latest command index that wrote to the resource.
  448. if (search_tracker->write_command_or_list_index == p_command_index) {
  449. ERR_FAIL_MSG("Command can't have itself as a dependency.");
  450. } else if (_check_command_intersection(resource_tracker, search_tracker->write_command_or_list_index, p_command_index)) {
  451. _add_adjacent_command(search_tracker->write_command_or_list_index, p_command_index, r_command);
  452. } else {
  453. command_intersection_failed = true;
  454. }
  455. }
  456. }
  457. if (write_usage) {
  458. if (resource_has_parent || command_intersection_failed) {
  459. if (!search_tracker->write_command_list_enabled && search_tracker->write_command_or_list_index >= 0) {
  460. // 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.
  461. const RDD::TextureSubresourceRange &tracker_subresources = search_tracker->texture_subresources;
  462. Rect2i tracker_rect(tracker_subresources.base_mipmap, tracker_subresources.base_layer, tracker_subresources.mipmap_count, tracker_subresources.layer_count);
  463. search_tracker->write_command_or_list_index = _add_to_write_list(search_tracker->write_command_or_list_index, tracker_rect, -1);
  464. }
  465. search_tracker->write_command_or_list_index = _add_to_write_list(p_command_index, search_tracker_rect, search_tracker->write_command_or_list_index);
  466. search_tracker->write_command_list_enabled = true;
  467. } else {
  468. search_tracker->write_command_or_list_index = p_command_index;
  469. search_tracker->write_command_list_enabled = false;
  470. }
  471. // We add this command to the adjacency list of all commands that were reading from the entire resource.
  472. int32_t read_full_command_list_index = search_tracker->read_full_command_list_index;
  473. while (read_full_command_list_index >= 0) {
  474. int32_t read_full_command_index = command_list_nodes[read_full_command_list_index].command_index;
  475. int32_t read_full_next_index = command_list_nodes[read_full_command_list_index].next_list_index;
  476. if (read_full_command_index == p_command_index) {
  477. if (!resource_has_parent) {
  478. // 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.
  479. ERR_FAIL_MSG("Command can't have itself as a dependency.");
  480. }
  481. } else {
  482. // Add this command to the adjacency list of each command that was reading this resource.
  483. _add_adjacent_command(read_full_command_index, p_command_index, r_command);
  484. }
  485. read_full_command_list_index = read_full_next_index;
  486. }
  487. if (!resource_has_parent) {
  488. // Clear the full list if this resource is not a slice.
  489. search_tracker->read_full_command_list_index = -1;
  490. }
  491. // We add this command to the adjacency list of all commands that were reading from resource slices.
  492. int32_t previous_slice_command_list_index = -1;
  493. int32_t read_slice_command_list_index = search_tracker->read_slice_command_list_index;
  494. while (read_slice_command_list_index >= 0) {
  495. const RecordedSliceListNode &read_list_node = read_slice_list_nodes[read_slice_command_list_index];
  496. if (!resource_has_parent || search_tracker_rect.encloses(read_list_node.subresources)) {
  497. if (previous_slice_command_list_index >= 0) {
  498. // Erase this element and connect the previous one to the next element.
  499. read_slice_list_nodes[previous_slice_command_list_index].next_list_index = read_list_node.next_list_index;
  500. } else {
  501. // Erase this element from the head of the list.
  502. DEV_ASSERT(search_tracker->read_slice_command_list_index == read_slice_command_list_index);
  503. search_tracker->read_slice_command_list_index = read_list_node.next_list_index;
  504. }
  505. // Advance to the next element.
  506. read_slice_command_list_index = read_list_node.next_list_index;
  507. } else {
  508. previous_slice_command_list_index = read_slice_command_list_index;
  509. read_slice_command_list_index = read_list_node.next_list_index;
  510. }
  511. if (!resource_has_parent || search_tracker_rect.intersects(read_list_node.subresources)) {
  512. // Add this command to the adjacency list of each command that was reading this resource.
  513. // We only add the dependency if there's an intersection between slices or this resource isn't a slice.
  514. _add_adjacent_command(read_list_node.command_index, p_command_index, r_command);
  515. }
  516. }
  517. } else if (resource_has_parent) {
  518. // We add a read dependency to the tracker to indicate this command reads from the resource slice.
  519. search_tracker->read_slice_command_list_index = _add_to_slice_read_list(p_command_index, resource_tracker_rect, search_tracker->read_slice_command_list_index);
  520. } else {
  521. // We add a read dependency to the tracker to indicate this command reads from the entire resource.
  522. search_tracker->read_full_command_list_index = _add_to_command_list(p_command_index, search_tracker->read_full_command_list_index);
  523. }
  524. }
  525. }
  526. void RenderingDeviceGraph::_add_texture_barrier_to_command(RDD::TextureID p_texture_id, BitField<RDD::BarrierAccessBits> p_src_access, BitField<RDD::BarrierAccessBits> p_dst_access, ResourceUsage p_prev_usage, ResourceUsage p_next_usage, RDD::TextureSubresourceRange p_subresources, LocalVector<RDD::TextureBarrier> &r_barrier_vector, int32_t &r_barrier_index, int32_t &r_barrier_count) {
  527. if (!driver_honors_barriers) {
  528. return;
  529. }
  530. if (r_barrier_index < 0) {
  531. r_barrier_index = r_barrier_vector.size();
  532. }
  533. RDD::TextureBarrier texture_barrier;
  534. texture_barrier.texture = p_texture_id;
  535. texture_barrier.src_access = p_src_access;
  536. texture_barrier.dst_access = p_dst_access;
  537. texture_barrier.prev_layout = _usage_to_image_layout(p_prev_usage);
  538. texture_barrier.next_layout = _usage_to_image_layout(p_next_usage);
  539. texture_barrier.subresources = p_subresources;
  540. r_barrier_vector.push_back(texture_barrier);
  541. r_barrier_count++;
  542. }
  543. #if USE_BUFFER_BARRIERS
  544. void RenderingDeviceGraph::_add_buffer_barrier_to_command(RDD::BufferID p_buffer_id, BitField<RDD::BarrierAccessBits> p_src_access, BitField<RDD::BarrierAccessBits> p_dst_access, int32_t &r_barrier_index, int32_t &r_barrier_count) {
  545. if (!driver_honors_barriers) {
  546. return;
  547. }
  548. if (r_barrier_index < 0) {
  549. r_barrier_index = command_buffer_barriers.size();
  550. }
  551. RDD::BufferBarrier buffer_barrier;
  552. buffer_barrier.buffer = p_buffer_id;
  553. buffer_barrier.src_access = p_src_access;
  554. buffer_barrier.dst_access = p_dst_access;
  555. buffer_barrier.offset = 0;
  556. buffer_barrier.size = RDD::BUFFER_WHOLE_SIZE;
  557. command_buffer_barriers.push_back(buffer_barrier);
  558. r_barrier_count++;
  559. }
  560. #endif
  561. void RenderingDeviceGraph::_run_compute_list_command(RDD::CommandBufferID p_command_buffer, const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {
  562. uint32_t instruction_data_cursor = 0;
  563. while (instruction_data_cursor < p_instruction_data_size) {
  564. DEV_ASSERT((instruction_data_cursor + sizeof(ComputeListInstruction)) <= p_instruction_data_size);
  565. const ComputeListInstruction *instruction = reinterpret_cast<const ComputeListInstruction *>(&p_instruction_data[instruction_data_cursor]);
  566. switch (instruction->type) {
  567. case ComputeListInstruction::TYPE_BIND_PIPELINE: {
  568. const ComputeListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const ComputeListBindPipelineInstruction *>(instruction);
  569. driver->command_bind_compute_pipeline(p_command_buffer, bind_pipeline_instruction->pipeline);
  570. instruction_data_cursor += sizeof(ComputeListBindPipelineInstruction);
  571. } break;
  572. case ComputeListInstruction::TYPE_BIND_UNIFORM_SET: {
  573. const ComputeListBindUniformSetInstruction *bind_uniform_set_instruction = reinterpret_cast<const ComputeListBindUniformSetInstruction *>(instruction);
  574. driver->command_bind_compute_uniform_set(p_command_buffer, bind_uniform_set_instruction->uniform_set, bind_uniform_set_instruction->shader, bind_uniform_set_instruction->set_index);
  575. instruction_data_cursor += sizeof(ComputeListBindUniformSetInstruction);
  576. } break;
  577. case ComputeListInstruction::TYPE_DISPATCH: {
  578. const ComputeListDispatchInstruction *dispatch_instruction = reinterpret_cast<const ComputeListDispatchInstruction *>(instruction);
  579. driver->command_compute_dispatch(p_command_buffer, dispatch_instruction->x_groups, dispatch_instruction->y_groups, dispatch_instruction->z_groups);
  580. instruction_data_cursor += sizeof(ComputeListDispatchInstruction);
  581. } break;
  582. case ComputeListInstruction::TYPE_DISPATCH_INDIRECT: {
  583. const ComputeListDispatchIndirectInstruction *dispatch_indirect_instruction = reinterpret_cast<const ComputeListDispatchIndirectInstruction *>(instruction);
  584. driver->command_compute_dispatch_indirect(p_command_buffer, dispatch_indirect_instruction->buffer, dispatch_indirect_instruction->offset);
  585. instruction_data_cursor += sizeof(ComputeListDispatchIndirectInstruction);
  586. } break;
  587. case ComputeListInstruction::TYPE_SET_PUSH_CONSTANT: {
  588. const ComputeListSetPushConstantInstruction *set_push_constant_instruction = reinterpret_cast<const ComputeListSetPushConstantInstruction *>(instruction);
  589. const VectorView push_constant_data_view(reinterpret_cast<const uint32_t *>(set_push_constant_instruction->data()), set_push_constant_instruction->size / sizeof(uint32_t));
  590. driver->command_bind_push_constants(p_command_buffer, set_push_constant_instruction->shader, 0, push_constant_data_view);
  591. instruction_data_cursor += sizeof(ComputeListSetPushConstantInstruction);
  592. instruction_data_cursor += set_push_constant_instruction->size;
  593. } break;
  594. case ComputeListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE: {
  595. const ComputeListUniformSetPrepareForUseInstruction *uniform_set_prepare_for_use_instruction = reinterpret_cast<const ComputeListUniformSetPrepareForUseInstruction *>(instruction);
  596. driver->command_uniform_set_prepare_for_use(p_command_buffer, uniform_set_prepare_for_use_instruction->uniform_set, uniform_set_prepare_for_use_instruction->shader, uniform_set_prepare_for_use_instruction->set_index);
  597. instruction_data_cursor += sizeof(ComputeListUniformSetPrepareForUseInstruction);
  598. } break;
  599. default:
  600. DEV_ASSERT(false && "Unknown compute list instruction type.");
  601. return;
  602. }
  603. }
  604. }
  605. void RenderingDeviceGraph::_run_draw_list_command(RDD::CommandBufferID p_command_buffer, const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {
  606. uint32_t instruction_data_cursor = 0;
  607. while (instruction_data_cursor < p_instruction_data_size) {
  608. DEV_ASSERT((instruction_data_cursor + sizeof(DrawListInstruction)) <= p_instruction_data_size);
  609. const DrawListInstruction *instruction = reinterpret_cast<const DrawListInstruction *>(&p_instruction_data[instruction_data_cursor]);
  610. switch (instruction->type) {
  611. case DrawListInstruction::TYPE_BIND_INDEX_BUFFER: {
  612. const DrawListBindIndexBufferInstruction *bind_index_buffer_instruction = reinterpret_cast<const DrawListBindIndexBufferInstruction *>(instruction);
  613. driver->command_render_bind_index_buffer(p_command_buffer, bind_index_buffer_instruction->buffer, bind_index_buffer_instruction->format, bind_index_buffer_instruction->offset);
  614. instruction_data_cursor += sizeof(DrawListBindIndexBufferInstruction);
  615. } break;
  616. case DrawListInstruction::TYPE_BIND_PIPELINE: {
  617. const DrawListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const DrawListBindPipelineInstruction *>(instruction);
  618. driver->command_bind_render_pipeline(p_command_buffer, bind_pipeline_instruction->pipeline);
  619. instruction_data_cursor += sizeof(DrawListBindPipelineInstruction);
  620. } break;
  621. case DrawListInstruction::TYPE_BIND_UNIFORM_SET: {
  622. const DrawListBindUniformSetInstruction *bind_uniform_set_instruction = reinterpret_cast<const DrawListBindUniformSetInstruction *>(instruction);
  623. driver->command_bind_render_uniform_set(p_command_buffer, bind_uniform_set_instruction->uniform_set, bind_uniform_set_instruction->shader, bind_uniform_set_instruction->set_index);
  624. instruction_data_cursor += sizeof(DrawListBindUniformSetInstruction);
  625. } break;
  626. case DrawListInstruction::TYPE_BIND_VERTEX_BUFFERS: {
  627. const DrawListBindVertexBuffersInstruction *bind_vertex_buffers_instruction = reinterpret_cast<const DrawListBindVertexBuffersInstruction *>(instruction);
  628. driver->command_render_bind_vertex_buffers(p_command_buffer, bind_vertex_buffers_instruction->vertex_buffers_count, bind_vertex_buffers_instruction->vertex_buffers(), bind_vertex_buffers_instruction->vertex_buffer_offsets());
  629. instruction_data_cursor += sizeof(DrawListBindVertexBuffersInstruction);
  630. instruction_data_cursor += sizeof(RDD::BufferID) * bind_vertex_buffers_instruction->vertex_buffers_count;
  631. instruction_data_cursor += sizeof(uint64_t) * bind_vertex_buffers_instruction->vertex_buffers_count;
  632. } break;
  633. case DrawListInstruction::TYPE_CLEAR_ATTACHMENTS: {
  634. const DrawListClearAttachmentsInstruction *clear_attachments_instruction = reinterpret_cast<const DrawListClearAttachmentsInstruction *>(instruction);
  635. const VectorView attachments_clear_view(clear_attachments_instruction->attachments_clear(), clear_attachments_instruction->attachments_clear_count);
  636. const VectorView attachments_clear_rect_view(clear_attachments_instruction->attachments_clear_rect(), clear_attachments_instruction->attachments_clear_rect_count);
  637. driver->command_render_clear_attachments(p_command_buffer, attachments_clear_view, attachments_clear_rect_view);
  638. instruction_data_cursor += sizeof(DrawListClearAttachmentsInstruction);
  639. instruction_data_cursor += sizeof(RDD::AttachmentClear) * clear_attachments_instruction->attachments_clear_count;
  640. instruction_data_cursor += sizeof(Rect2i) * clear_attachments_instruction->attachments_clear_rect_count;
  641. } break;
  642. case DrawListInstruction::TYPE_DRAW: {
  643. const DrawListDrawInstruction *draw_instruction = reinterpret_cast<const DrawListDrawInstruction *>(instruction);
  644. driver->command_render_draw(p_command_buffer, draw_instruction->vertex_count, draw_instruction->instance_count, 0, 0);
  645. instruction_data_cursor += sizeof(DrawListDrawInstruction);
  646. } break;
  647. case DrawListInstruction::TYPE_DRAW_INDEXED: {
  648. const DrawListDrawIndexedInstruction *draw_indexed_instruction = reinterpret_cast<const DrawListDrawIndexedInstruction *>(instruction);
  649. driver->command_render_draw_indexed(p_command_buffer, draw_indexed_instruction->index_count, draw_indexed_instruction->instance_count, draw_indexed_instruction->first_index, 0, 0);
  650. instruction_data_cursor += sizeof(DrawListDrawIndexedInstruction);
  651. } break;
  652. case DrawListInstruction::TYPE_DRAW_INDIRECT: {
  653. const DrawListDrawIndirectInstruction *draw_indirect_instruction = reinterpret_cast<const DrawListDrawIndirectInstruction *>(instruction);
  654. driver->command_render_draw_indirect(p_command_buffer, draw_indirect_instruction->buffer, draw_indirect_instruction->offset, draw_indirect_instruction->draw_count, draw_indirect_instruction->stride);
  655. instruction_data_cursor += sizeof(DrawListDrawIndirectInstruction);
  656. } break;
  657. case DrawListInstruction::TYPE_DRAW_INDEXED_INDIRECT: {
  658. const DrawListDrawIndexedIndirectInstruction *draw_indexed_indirect_instruction = reinterpret_cast<const DrawListDrawIndexedIndirectInstruction *>(instruction);
  659. driver->command_render_draw_indexed_indirect(p_command_buffer, draw_indexed_indirect_instruction->buffer, draw_indexed_indirect_instruction->offset, draw_indexed_indirect_instruction->draw_count, draw_indexed_indirect_instruction->stride);
  660. instruction_data_cursor += sizeof(DrawListDrawIndexedIndirectInstruction);
  661. } break;
  662. case DrawListInstruction::TYPE_EXECUTE_COMMANDS: {
  663. const DrawListExecuteCommandsInstruction *execute_commands_instruction = reinterpret_cast<const DrawListExecuteCommandsInstruction *>(instruction);
  664. driver->command_buffer_execute_secondary(p_command_buffer, execute_commands_instruction->command_buffer);
  665. instruction_data_cursor += sizeof(DrawListExecuteCommandsInstruction);
  666. } break;
  667. case DrawListInstruction::TYPE_NEXT_SUBPASS: {
  668. const DrawListNextSubpassInstruction *next_subpass_instruction = reinterpret_cast<const DrawListNextSubpassInstruction *>(instruction);
  669. driver->command_next_render_subpass(p_command_buffer, next_subpass_instruction->command_buffer_type);
  670. instruction_data_cursor += sizeof(DrawListNextSubpassInstruction);
  671. } break;
  672. case DrawListInstruction::TYPE_SET_BLEND_CONSTANTS: {
  673. const DrawListSetBlendConstantsInstruction *set_blend_constants_instruction = reinterpret_cast<const DrawListSetBlendConstantsInstruction *>(instruction);
  674. driver->command_render_set_blend_constants(p_command_buffer, set_blend_constants_instruction->color);
  675. instruction_data_cursor += sizeof(DrawListSetBlendConstantsInstruction);
  676. } break;
  677. case DrawListInstruction::TYPE_SET_LINE_WIDTH: {
  678. const DrawListSetLineWidthInstruction *set_line_width_instruction = reinterpret_cast<const DrawListSetLineWidthInstruction *>(instruction);
  679. driver->command_render_set_line_width(p_command_buffer, set_line_width_instruction->width);
  680. instruction_data_cursor += sizeof(DrawListSetLineWidthInstruction);
  681. } break;
  682. case DrawListInstruction::TYPE_SET_PUSH_CONSTANT: {
  683. const DrawListSetPushConstantInstruction *set_push_constant_instruction = reinterpret_cast<const DrawListSetPushConstantInstruction *>(instruction);
  684. const VectorView push_constant_data_view(reinterpret_cast<const uint32_t *>(set_push_constant_instruction->data()), set_push_constant_instruction->size / sizeof(uint32_t));
  685. driver->command_bind_push_constants(p_command_buffer, set_push_constant_instruction->shader, 0, push_constant_data_view);
  686. instruction_data_cursor += sizeof(DrawListSetPushConstantInstruction);
  687. instruction_data_cursor += set_push_constant_instruction->size;
  688. } break;
  689. case DrawListInstruction::TYPE_SET_SCISSOR: {
  690. const DrawListSetScissorInstruction *set_scissor_instruction = reinterpret_cast<const DrawListSetScissorInstruction *>(instruction);
  691. driver->command_render_set_scissor(p_command_buffer, set_scissor_instruction->rect);
  692. instruction_data_cursor += sizeof(DrawListSetScissorInstruction);
  693. } break;
  694. case DrawListInstruction::TYPE_SET_VIEWPORT: {
  695. const DrawListSetViewportInstruction *set_viewport_instruction = reinterpret_cast<const DrawListSetViewportInstruction *>(instruction);
  696. driver->command_render_set_viewport(p_command_buffer, set_viewport_instruction->rect);
  697. instruction_data_cursor += sizeof(DrawListSetViewportInstruction);
  698. } break;
  699. case DrawListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE: {
  700. const DrawListUniformSetPrepareForUseInstruction *uniform_set_prepare_for_use_instruction = reinterpret_cast<const DrawListUniformSetPrepareForUseInstruction *>(instruction);
  701. driver->command_uniform_set_prepare_for_use(p_command_buffer, uniform_set_prepare_for_use_instruction->uniform_set, uniform_set_prepare_for_use_instruction->shader, uniform_set_prepare_for_use_instruction->set_index);
  702. instruction_data_cursor += sizeof(DrawListUniformSetPrepareForUseInstruction);
  703. } break;
  704. default:
  705. DEV_ASSERT(false && "Unknown draw list instruction type.");
  706. return;
  707. }
  708. }
  709. }
  710. void RenderingDeviceGraph::_run_secondary_command_buffer_task(const SecondaryCommandBuffer *p_secondary) {
  711. driver->command_buffer_begin_secondary(p_secondary->command_buffer, p_secondary->render_pass, 0, p_secondary->framebuffer);
  712. _run_draw_list_command(p_secondary->command_buffer, p_secondary->instruction_data.ptr(), p_secondary->instruction_data.size());
  713. driver->command_buffer_end(p_secondary->command_buffer);
  714. }
  715. void RenderingDeviceGraph::_wait_for_secondary_command_buffer_tasks() {
  716. for (uint32_t i = 0; i < frames[frame].secondary_command_buffers_used; i++) {
  717. WorkerThreadPool::TaskID &task = frames[frame].secondary_command_buffers[i].task;
  718. if (task != WorkerThreadPool::INVALID_TASK_ID) {
  719. WorkerThreadPool::get_singleton()->wait_for_task_completion(task);
  720. task = WorkerThreadPool::INVALID_TASK_ID;
  721. }
  722. }
  723. }
  724. void RenderingDeviceGraph::_run_render_commands(int32_t p_level, const RecordedCommandSort *p_sorted_commands, uint32_t p_sorted_commands_count, RDD::CommandBufferID &r_command_buffer, CommandBufferPool &r_command_buffer_pool, int32_t &r_current_label_index, int32_t &r_current_label_level) {
  725. for (uint32_t i = 0; i < p_sorted_commands_count; i++) {
  726. const uint32_t command_index = p_sorted_commands[i].index;
  727. const uint32_t command_data_offset = command_data_offsets[command_index];
  728. const RecordedCommand *command = reinterpret_cast<RecordedCommand *>(&command_data[command_data_offset]);
  729. _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);
  730. switch (command->type) {
  731. case RecordedCommand::TYPE_BUFFER_CLEAR: {
  732. const RecordedBufferClearCommand *buffer_clear_command = reinterpret_cast<const RecordedBufferClearCommand *>(command);
  733. driver->command_clear_buffer(r_command_buffer, buffer_clear_command->buffer, buffer_clear_command->offset, buffer_clear_command->size);
  734. } break;
  735. case RecordedCommand::TYPE_BUFFER_COPY: {
  736. const RecordedBufferCopyCommand *buffer_copy_command = reinterpret_cast<const RecordedBufferCopyCommand *>(command);
  737. driver->command_copy_buffer(r_command_buffer, buffer_copy_command->source, buffer_copy_command->destination, buffer_copy_command->region);
  738. } break;
  739. case RecordedCommand::TYPE_BUFFER_GET_DATA: {
  740. const RecordedBufferGetDataCommand *buffer_get_data_command = reinterpret_cast<const RecordedBufferGetDataCommand *>(command);
  741. driver->command_copy_buffer(r_command_buffer, buffer_get_data_command->source, buffer_get_data_command->destination, buffer_get_data_command->region);
  742. } break;
  743. case RecordedCommand::TYPE_BUFFER_UPDATE: {
  744. const RecordedBufferUpdateCommand *buffer_update_command = reinterpret_cast<const RecordedBufferUpdateCommand *>(command);
  745. const RecordedBufferCopy *command_buffer_copies = buffer_update_command->buffer_copies();
  746. for (uint32_t j = 0; j < buffer_update_command->buffer_copies_count; j++) {
  747. driver->command_copy_buffer(r_command_buffer, command_buffer_copies[j].source, buffer_update_command->destination, command_buffer_copies[j].region);
  748. }
  749. } break;
  750. case RecordedCommand::TYPE_COMPUTE_LIST: {
  751. if (device.workarounds.avoid_compute_after_draw && workarounds_state.draw_list_found) {
  752. // Avoid compute after draw workaround. Refer to the comment that enables this in the Vulkan driver for more information.
  753. workarounds_state.draw_list_found = false;
  754. // Create or reuse a command buffer and finish recording the current one.
  755. driver->command_buffer_end(r_command_buffer);
  756. while (r_command_buffer_pool.buffers_used >= r_command_buffer_pool.buffers.size()) {
  757. RDD::CommandBufferID command_buffer = driver->command_buffer_create(r_command_buffer_pool.pool);
  758. RDD::SemaphoreID command_semaphore = driver->semaphore_create();
  759. r_command_buffer_pool.buffers.push_back(command_buffer);
  760. r_command_buffer_pool.semaphores.push_back(command_semaphore);
  761. }
  762. // Start recording on the next usable command buffer from the pool.
  763. uint32_t command_buffer_index = r_command_buffer_pool.buffers_used++;
  764. r_command_buffer = r_command_buffer_pool.buffers[command_buffer_index];
  765. driver->command_buffer_begin(r_command_buffer);
  766. }
  767. const RecordedComputeListCommand *compute_list_command = reinterpret_cast<const RecordedComputeListCommand *>(command);
  768. _run_compute_list_command(r_command_buffer, compute_list_command->instruction_data(), compute_list_command->instruction_data_size);
  769. } break;
  770. case RecordedCommand::TYPE_DRAW_LIST: {
  771. if (device.workarounds.avoid_compute_after_draw) {
  772. // Indicate that a draw list was encountered for the workaround.
  773. workarounds_state.draw_list_found = true;
  774. }
  775. const RecordedDrawListCommand *draw_list_command = reinterpret_cast<const RecordedDrawListCommand *>(command);
  776. const VectorView clear_values(draw_list_command->clear_values(), draw_list_command->clear_values_count);
  777. #if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
  778. driver->command_insert_breadcrumb(r_command_buffer, draw_list_command->breadcrumb);
  779. #endif
  780. driver->command_begin_render_pass(r_command_buffer, draw_list_command->render_pass, draw_list_command->framebuffer, draw_list_command->command_buffer_type, draw_list_command->region, clear_values);
  781. _run_draw_list_command(r_command_buffer, draw_list_command->instruction_data(), draw_list_command->instruction_data_size);
  782. driver->command_end_render_pass(r_command_buffer);
  783. } break;
  784. case RecordedCommand::TYPE_TEXTURE_CLEAR: {
  785. const RecordedTextureClearCommand *texture_clear_command = reinterpret_cast<const RecordedTextureClearCommand *>(command);
  786. driver->command_clear_color_texture(r_command_buffer, texture_clear_command->texture, RDD::TEXTURE_LAYOUT_COPY_DST_OPTIMAL, texture_clear_command->color, texture_clear_command->range);
  787. } break;
  788. case RecordedCommand::TYPE_TEXTURE_COPY: {
  789. const RecordedTextureCopyCommand *texture_copy_command = reinterpret_cast<const RecordedTextureCopyCommand *>(command);
  790. const VectorView<RDD::TextureCopyRegion> command_texture_copy_regions_view(texture_copy_command->texture_copy_regions(), texture_copy_command->texture_copy_regions_count);
  791. driver->command_copy_texture(r_command_buffer, texture_copy_command->from_texture, RDD::TEXTURE_LAYOUT_COPY_SRC_OPTIMAL, texture_copy_command->to_texture, RDD::TEXTURE_LAYOUT_COPY_DST_OPTIMAL, command_texture_copy_regions_view);
  792. } break;
  793. case RecordedCommand::TYPE_TEXTURE_GET_DATA: {
  794. const RecordedTextureGetDataCommand *texture_get_data_command = reinterpret_cast<const RecordedTextureGetDataCommand *>(command);
  795. const VectorView<RDD::BufferTextureCopyRegion> command_buffer_texture_copy_regions_view(texture_get_data_command->buffer_texture_copy_regions(), texture_get_data_command->buffer_texture_copy_regions_count);
  796. driver->command_copy_texture_to_buffer(r_command_buffer, texture_get_data_command->from_texture, RDD::TEXTURE_LAYOUT_COPY_SRC_OPTIMAL, texture_get_data_command->to_buffer, command_buffer_texture_copy_regions_view);
  797. } break;
  798. case RecordedCommand::TYPE_TEXTURE_RESOLVE: {
  799. const RecordedTextureResolveCommand *texture_resolve_command = reinterpret_cast<const RecordedTextureResolveCommand *>(command);
  800. driver->command_resolve_texture(r_command_buffer, texture_resolve_command->from_texture, RDD::TEXTURE_LAYOUT_RESOLVE_SRC_OPTIMAL, texture_resolve_command->src_layer, texture_resolve_command->src_mipmap, texture_resolve_command->to_texture, RDD::TEXTURE_LAYOUT_RESOLVE_DST_OPTIMAL, texture_resolve_command->dst_layer, texture_resolve_command->dst_mipmap);
  801. } break;
  802. case RecordedCommand::TYPE_TEXTURE_UPDATE: {
  803. const RecordedTextureUpdateCommand *texture_update_command = reinterpret_cast<const RecordedTextureUpdateCommand *>(command);
  804. const RecordedBufferToTextureCopy *command_buffer_to_texture_copies = texture_update_command->buffer_to_texture_copies();
  805. for (uint32_t j = 0; j < texture_update_command->buffer_to_texture_copies_count; j++) {
  806. driver->command_copy_buffer_to_texture(r_command_buffer, command_buffer_to_texture_copies[j].from_buffer, texture_update_command->to_texture, RDD::TEXTURE_LAYOUT_COPY_DST_OPTIMAL, command_buffer_to_texture_copies[j].region);
  807. }
  808. } break;
  809. case RecordedCommand::TYPE_CAPTURE_TIMESTAMP: {
  810. const RecordedCaptureTimestampCommand *texture_capture_timestamp_command = reinterpret_cast<const RecordedCaptureTimestampCommand *>(command);
  811. driver->command_timestamp_write(r_command_buffer, texture_capture_timestamp_command->pool, texture_capture_timestamp_command->index);
  812. } break;
  813. default: {
  814. DEV_ASSERT(false && "Unknown recorded command type.");
  815. return;
  816. }
  817. }
  818. }
  819. }
  820. void RenderingDeviceGraph::_run_label_command_change(RDD::CommandBufferID p_command_buffer, int32_t p_new_label_index, int32_t p_new_level, bool p_ignore_previous_value, bool p_use_label_for_empty, const RecordedCommandSort *p_sorted_commands, uint32_t p_sorted_commands_count, int32_t &r_current_label_index, int32_t &r_current_label_level) {
  821. if (command_label_count == 0) {
  822. // Ignore any label operations if no labels were pushed.
  823. return;
  824. }
  825. if (p_ignore_previous_value || p_new_label_index != r_current_label_index || p_new_level != r_current_label_level) {
  826. if (!p_ignore_previous_value && (p_use_label_for_empty || r_current_label_index >= 0 || r_current_label_level >= 0)) {
  827. // End the current label.
  828. driver->command_end_label(p_command_buffer);
  829. }
  830. String label_name;
  831. Color label_color;
  832. if (p_new_label_index >= 0) {
  833. const char *label_chars = &command_label_chars[command_label_offsets[p_new_label_index]];
  834. label_name.parse_utf8(label_chars);
  835. label_color = command_label_colors[p_new_label_index];
  836. } else if (p_use_label_for_empty) {
  837. label_name = "Command graph";
  838. label_color = Color(1, 1, 1, 1);
  839. } else {
  840. return;
  841. }
  842. // Add the level to the name.
  843. label_name += " (L" + itos(p_new_level) + ")";
  844. if (p_sorted_commands != nullptr && p_sorted_commands_count > 0) {
  845. // Analyze the commands in the level that have the same label to detect what type of operations are performed.
  846. bool copy_commands = false;
  847. bool compute_commands = false;
  848. bool draw_commands = false;
  849. for (uint32_t i = 0; i < p_sorted_commands_count; i++) {
  850. const uint32_t command_index = p_sorted_commands[i].index;
  851. const uint32_t command_data_offset = command_data_offsets[command_index];
  852. const RecordedCommand *command = reinterpret_cast<RecordedCommand *>(&command_data[command_data_offset]);
  853. if (command->label_index != p_new_label_index) {
  854. break;
  855. }
  856. switch (command->type) {
  857. case RecordedCommand::TYPE_BUFFER_CLEAR:
  858. case RecordedCommand::TYPE_BUFFER_COPY:
  859. case RecordedCommand::TYPE_BUFFER_GET_DATA:
  860. case RecordedCommand::TYPE_BUFFER_UPDATE:
  861. case RecordedCommand::TYPE_TEXTURE_CLEAR:
  862. case RecordedCommand::TYPE_TEXTURE_COPY:
  863. case RecordedCommand::TYPE_TEXTURE_GET_DATA:
  864. case RecordedCommand::TYPE_TEXTURE_RESOLVE:
  865. case RecordedCommand::TYPE_TEXTURE_UPDATE: {
  866. copy_commands = true;
  867. } break;
  868. case RecordedCommand::TYPE_COMPUTE_LIST: {
  869. compute_commands = true;
  870. } break;
  871. case RecordedCommand::TYPE_DRAW_LIST: {
  872. draw_commands = true;
  873. } break;
  874. default: {
  875. // Ignore command.
  876. } break;
  877. }
  878. if (copy_commands && compute_commands && draw_commands) {
  879. // There's no more command types to find.
  880. break;
  881. }
  882. }
  883. if (copy_commands || compute_commands || draw_commands) {
  884. // Add the operations to the name.
  885. bool plus_after_copy = copy_commands && (compute_commands || draw_commands);
  886. bool plus_after_compute = compute_commands && draw_commands;
  887. label_name += " (";
  888. label_name += copy_commands ? "Copy" : "";
  889. label_name += plus_after_copy ? "+" : "";
  890. label_name += compute_commands ? "Compute" : "";
  891. label_name += plus_after_compute ? "+" : "";
  892. label_name += draw_commands ? "Draw" : "";
  893. label_name += ")";
  894. }
  895. }
  896. // Start the new label.
  897. CharString label_name_utf8 = label_name.utf8();
  898. driver->command_begin_label(p_command_buffer, label_name_utf8.get_data(), label_color);
  899. r_current_label_index = p_new_label_index;
  900. r_current_label_level = p_new_level;
  901. }
  902. }
  903. void RenderingDeviceGraph::_boost_priority_for_render_commands(RecordedCommandSort *p_sorted_commands, uint32_t p_sorted_commands_count, uint32_t &r_boosted_priority) {
  904. if (p_sorted_commands_count == 0) {
  905. return;
  906. }
  907. const uint32_t boosted_priority_value = 0;
  908. if (r_boosted_priority > 0) {
  909. bool perform_sort = false;
  910. for (uint32_t j = 0; j < p_sorted_commands_count; j++) {
  911. if (p_sorted_commands[j].priority == r_boosted_priority) {
  912. p_sorted_commands[j].priority = boosted_priority_value;
  913. perform_sort = true;
  914. }
  915. }
  916. if (perform_sort) {
  917. SortArray<RecordedCommandSort> command_sorter;
  918. command_sorter.sort(p_sorted_commands, p_sorted_commands_count);
  919. }
  920. }
  921. if (p_sorted_commands[p_sorted_commands_count - 1].priority != boosted_priority_value) {
  922. r_boosted_priority = p_sorted_commands[p_sorted_commands_count - 1].priority;
  923. }
  924. }
  925. void RenderingDeviceGraph::_group_barriers_for_render_commands(RDD::CommandBufferID p_command_buffer, const RecordedCommandSort *p_sorted_commands, uint32_t p_sorted_commands_count, bool p_full_memory_barrier) {
  926. if (!driver_honors_barriers) {
  927. return;
  928. }
  929. barrier_group.clear();
  930. barrier_group.src_stages = RDD::PIPELINE_STAGE_TOP_OF_PIPE_BIT;
  931. barrier_group.dst_stages = RDD::PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
  932. for (uint32_t i = 0; i < p_sorted_commands_count; i++) {
  933. const uint32_t command_index = p_sorted_commands[i].index;
  934. const uint32_t command_data_offset = command_data_offsets[command_index];
  935. const RecordedCommand *command = reinterpret_cast<RecordedCommand *>(&command_data[command_data_offset]);
  936. #if PRINT_COMMAND_RECORDING
  937. print_line(vformat("Grouping barriers for #%d", command_index));
  938. #endif
  939. // Merge command's stage bits with the barrier group.
  940. barrier_group.src_stages = barrier_group.src_stages | command->previous_stages;
  941. barrier_group.dst_stages = barrier_group.dst_stages | command->next_stages;
  942. // Merge command's memory barrier bits with the barrier group.
  943. barrier_group.memory_barrier.src_access = barrier_group.memory_barrier.src_access | command->memory_barrier.src_access;
  944. barrier_group.memory_barrier.dst_access = barrier_group.memory_barrier.dst_access | command->memory_barrier.dst_access;
  945. // Gather texture barriers.
  946. for (int32_t j = 0; j < command->normalization_barrier_count; j++) {
  947. const RDD::TextureBarrier &recorded_barrier = command_normalization_barriers[command->normalization_barrier_index + j];
  948. barrier_group.normalization_barriers.push_back(recorded_barrier);
  949. #if PRINT_COMMAND_RECORDING
  950. print_line(vformat("Normalization Barrier #%d", barrier_group.normalization_barriers.size() - 1));
  951. #endif
  952. }
  953. for (int32_t j = 0; j < command->transition_barrier_count; j++) {
  954. const RDD::TextureBarrier &recorded_barrier = command_transition_barriers[command->transition_barrier_index + j];
  955. barrier_group.transition_barriers.push_back(recorded_barrier);
  956. #if PRINT_COMMAND_RECORDING
  957. print_line(vformat("Transition Barrier #%d", barrier_group.transition_barriers.size() - 1));
  958. #endif
  959. }
  960. #if USE_BUFFER_BARRIERS
  961. // Gather buffer barriers.
  962. for (int32_t j = 0; j < command->buffer_barrier_count; j++) {
  963. const RDD::BufferBarrier &recorded_barrier = command_buffer_barriers[command->buffer_barrier_index + j];
  964. barrier_group.buffer_barriers.push_back(recorded_barrier);
  965. }
  966. #endif
  967. }
  968. if (p_full_memory_barrier) {
  969. barrier_group.src_stages = RDD::PIPELINE_STAGE_ALL_COMMANDS_BIT;
  970. barrier_group.dst_stages = RDD::PIPELINE_STAGE_ALL_COMMANDS_BIT;
  971. barrier_group.memory_barrier.src_access = RDD::BARRIER_ACCESS_MEMORY_READ_BIT | RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT;
  972. barrier_group.memory_barrier.dst_access = RDD::BARRIER_ACCESS_MEMORY_READ_BIT | RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT;
  973. }
  974. const bool is_memory_barrier_empty = barrier_group.memory_barrier.src_access.is_empty() && barrier_group.memory_barrier.dst_access.is_empty();
  975. const bool are_texture_barriers_empty = barrier_group.normalization_barriers.is_empty() && barrier_group.transition_barriers.is_empty();
  976. #if USE_BUFFER_BARRIERS
  977. const bool are_buffer_barriers_empty = barrier_group.buffer_barriers.is_empty();
  978. #else
  979. const bool are_buffer_barriers_empty = true;
  980. #endif
  981. if (is_memory_barrier_empty && are_texture_barriers_empty && are_buffer_barriers_empty) {
  982. // Commands don't require synchronization.
  983. return;
  984. }
  985. const VectorView<RDD::MemoryBarrier> memory_barriers = !is_memory_barrier_empty ? barrier_group.memory_barrier : VectorView<RDD::MemoryBarrier>();
  986. const VectorView<RDD::TextureBarrier> texture_barriers = barrier_group.normalization_barriers.is_empty() ? barrier_group.transition_barriers : barrier_group.normalization_barriers;
  987. #if USE_BUFFER_BARRIERS
  988. const VectorView<RDD::BufferBarrier> buffer_barriers = !are_buffer_barriers_empty ? barrier_group.buffer_barriers : VectorView<RDD::BufferBarrier>();
  989. #else
  990. const VectorView<RDD::BufferBarrier> buffer_barriers = VectorView<RDD::BufferBarrier>();
  991. #endif
  992. driver->command_pipeline_barrier(p_command_buffer, barrier_group.src_stages, barrier_group.dst_stages, memory_barriers, buffer_barriers, texture_barriers);
  993. bool separate_texture_barriers = !barrier_group.normalization_barriers.is_empty() && !barrier_group.transition_barriers.is_empty();
  994. if (separate_texture_barriers) {
  995. driver->command_pipeline_barrier(p_command_buffer, barrier_group.src_stages, barrier_group.dst_stages, VectorView<RDD::MemoryBarrier>(), VectorView<RDD::BufferBarrier>(), barrier_group.transition_barriers);
  996. }
  997. }
  998. void RenderingDeviceGraph::_print_render_commands(const RecordedCommandSort *p_sorted_commands, uint32_t p_sorted_commands_count) {
  999. for (uint32_t i = 0; i < p_sorted_commands_count; i++) {
  1000. const uint32_t command_index = p_sorted_commands[i].index;
  1001. const uint32_t command_level = p_sorted_commands[i].level;
  1002. const uint32_t command_data_offset = command_data_offsets[command_index];
  1003. const RecordedCommand *command = reinterpret_cast<RecordedCommand *>(&command_data[command_data_offset]);
  1004. switch (command->type) {
  1005. case RecordedCommand::TYPE_BUFFER_CLEAR: {
  1006. const RecordedBufferClearCommand *buffer_clear_command = reinterpret_cast<const RecordedBufferClearCommand *>(command);
  1007. print_line(command_index, "LEVEL", command_level, "BUFFER CLEAR DESTINATION", itos(buffer_clear_command->buffer.id));
  1008. } break;
  1009. case RecordedCommand::TYPE_BUFFER_COPY: {
  1010. const RecordedBufferCopyCommand *buffer_copy_command = reinterpret_cast<const RecordedBufferCopyCommand *>(command);
  1011. print_line(command_index, "LEVEL", command_level, "BUFFER COPY SOURCE", itos(buffer_copy_command->source.id), "DESTINATION", itos(buffer_copy_command->destination.id));
  1012. } break;
  1013. case RecordedCommand::TYPE_BUFFER_GET_DATA: {
  1014. const RecordedBufferGetDataCommand *buffer_get_data_command = reinterpret_cast<const RecordedBufferGetDataCommand *>(command);
  1015. print_line(command_index, "LEVEL", command_level, "BUFFER GET DATA DESTINATION", itos(buffer_get_data_command->destination.id));
  1016. } break;
  1017. case RecordedCommand::TYPE_BUFFER_UPDATE: {
  1018. const RecordedBufferUpdateCommand *buffer_update_command = reinterpret_cast<const RecordedBufferUpdateCommand *>(command);
  1019. print_line(command_index, "LEVEL", command_level, "BUFFER UPDATE DESTINATION", itos(buffer_update_command->destination.id), "COPIES", buffer_update_command->buffer_copies_count);
  1020. } break;
  1021. case RecordedCommand::TYPE_COMPUTE_LIST: {
  1022. const RecordedComputeListCommand *compute_list_command = reinterpret_cast<const RecordedComputeListCommand *>(command);
  1023. print_line(command_index, "LEVEL", command_level, "COMPUTE LIST SIZE", compute_list_command->instruction_data_size);
  1024. } break;
  1025. case RecordedCommand::TYPE_DRAW_LIST: {
  1026. const RecordedDrawListCommand *draw_list_command = reinterpret_cast<const RecordedDrawListCommand *>(command);
  1027. print_line(command_index, "LEVEL", command_level, "DRAW LIST SIZE", draw_list_command->instruction_data_size);
  1028. } break;
  1029. case RecordedCommand::TYPE_TEXTURE_CLEAR: {
  1030. const RecordedTextureClearCommand *texture_clear_command = reinterpret_cast<const RecordedTextureClearCommand *>(command);
  1031. print_line(command_index, "LEVEL", command_level, "TEXTURE CLEAR", itos(texture_clear_command->texture.id), "COLOR", texture_clear_command->color);
  1032. } break;
  1033. case RecordedCommand::TYPE_TEXTURE_COPY: {
  1034. const RecordedTextureCopyCommand *texture_copy_command = reinterpret_cast<const RecordedTextureCopyCommand *>(command);
  1035. print_line(command_index, "LEVEL", command_level, "TEXTURE COPY FROM", itos(texture_copy_command->from_texture.id), "TO", itos(texture_copy_command->to_texture.id));
  1036. } break;
  1037. case RecordedCommand::TYPE_TEXTURE_GET_DATA: {
  1038. print_line(command_index, "LEVEL", command_level, "TEXTURE GET DATA");
  1039. } break;
  1040. case RecordedCommand::TYPE_TEXTURE_RESOLVE: {
  1041. const RecordedTextureResolveCommand *texture_resolve_command = reinterpret_cast<const RecordedTextureResolveCommand *>(command);
  1042. print_line(command_index, "LEVEL", command_level, "TEXTURE RESOLVE FROM", itos(texture_resolve_command->from_texture.id), "TO", itos(texture_resolve_command->to_texture.id));
  1043. } break;
  1044. case RecordedCommand::TYPE_TEXTURE_UPDATE: {
  1045. const RecordedTextureUpdateCommand *texture_update_command = reinterpret_cast<const RecordedTextureUpdateCommand *>(command);
  1046. print_line(command_index, "LEVEL", command_level, "TEXTURE UPDATE TO", itos(texture_update_command->to_texture.id));
  1047. } break;
  1048. case RecordedCommand::TYPE_CAPTURE_TIMESTAMP: {
  1049. const RecordedCaptureTimestampCommand *texture_capture_timestamp_command = reinterpret_cast<const RecordedCaptureTimestampCommand *>(command);
  1050. print_line(command_index, "LEVEL", command_level, "CAPTURE TIMESTAMP POOL", itos(texture_capture_timestamp_command->pool.id), "INDEX", texture_capture_timestamp_command->index);
  1051. } break;
  1052. default:
  1053. DEV_ASSERT(false && "Unknown recorded command type.");
  1054. return;
  1055. }
  1056. }
  1057. }
  1058. void RenderingDeviceGraph::_print_draw_list(const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {
  1059. uint32_t instruction_data_cursor = 0;
  1060. while (instruction_data_cursor < p_instruction_data_size) {
  1061. DEV_ASSERT((instruction_data_cursor + sizeof(DrawListInstruction)) <= p_instruction_data_size);
  1062. const DrawListInstruction *instruction = reinterpret_cast<const DrawListInstruction *>(&p_instruction_data[instruction_data_cursor]);
  1063. switch (instruction->type) {
  1064. case DrawListInstruction::TYPE_BIND_INDEX_BUFFER: {
  1065. const DrawListBindIndexBufferInstruction *bind_index_buffer_instruction = reinterpret_cast<const DrawListBindIndexBufferInstruction *>(instruction);
  1066. print_line("\tBIND INDEX BUFFER ID", itos(bind_index_buffer_instruction->buffer.id), "FORMAT", bind_index_buffer_instruction->format, "OFFSET", bind_index_buffer_instruction->offset);
  1067. instruction_data_cursor += sizeof(DrawListBindIndexBufferInstruction);
  1068. } break;
  1069. case DrawListInstruction::TYPE_BIND_PIPELINE: {
  1070. const DrawListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const DrawListBindPipelineInstruction *>(instruction);
  1071. print_line("\tBIND PIPELINE ID", itos(bind_pipeline_instruction->pipeline.id));
  1072. instruction_data_cursor += sizeof(DrawListBindPipelineInstruction);
  1073. } break;
  1074. case DrawListInstruction::TYPE_BIND_UNIFORM_SET: {
  1075. const DrawListBindUniformSetInstruction *bind_uniform_set_instruction = reinterpret_cast<const DrawListBindUniformSetInstruction *>(instruction);
  1076. print_line("\tBIND UNIFORM SET ID", itos(bind_uniform_set_instruction->uniform_set.id), "SET INDEX", bind_uniform_set_instruction->set_index);
  1077. instruction_data_cursor += sizeof(DrawListBindUniformSetInstruction);
  1078. } break;
  1079. case DrawListInstruction::TYPE_BIND_VERTEX_BUFFERS: {
  1080. const DrawListBindVertexBuffersInstruction *bind_vertex_buffers_instruction = reinterpret_cast<const DrawListBindVertexBuffersInstruction *>(instruction);
  1081. print_line("\tBIND VERTEX BUFFERS COUNT", bind_vertex_buffers_instruction->vertex_buffers_count);
  1082. instruction_data_cursor += sizeof(DrawListBindVertexBuffersInstruction);
  1083. instruction_data_cursor += sizeof(RDD::BufferID) * bind_vertex_buffers_instruction->vertex_buffers_count;
  1084. instruction_data_cursor += sizeof(uint64_t) * bind_vertex_buffers_instruction->vertex_buffers_count;
  1085. } break;
  1086. case DrawListInstruction::TYPE_CLEAR_ATTACHMENTS: {
  1087. const DrawListClearAttachmentsInstruction *clear_attachments_instruction = reinterpret_cast<const DrawListClearAttachmentsInstruction *>(instruction);
  1088. print_line("\tATTACHMENTS CLEAR COUNT", clear_attachments_instruction->attachments_clear_count, "RECT COUNT", clear_attachments_instruction->attachments_clear_rect_count);
  1089. instruction_data_cursor += sizeof(DrawListClearAttachmentsInstruction);
  1090. instruction_data_cursor += sizeof(RDD::AttachmentClear) * clear_attachments_instruction->attachments_clear_count;
  1091. instruction_data_cursor += sizeof(Rect2i) * clear_attachments_instruction->attachments_clear_rect_count;
  1092. } break;
  1093. case DrawListInstruction::TYPE_DRAW: {
  1094. const DrawListDrawInstruction *draw_instruction = reinterpret_cast<const DrawListDrawInstruction *>(instruction);
  1095. print_line("\tDRAW VERTICES", draw_instruction->vertex_count, "INSTANCES", draw_instruction->instance_count);
  1096. instruction_data_cursor += sizeof(DrawListDrawInstruction);
  1097. } break;
  1098. case DrawListInstruction::TYPE_DRAW_INDEXED: {
  1099. const DrawListDrawIndexedInstruction *draw_indexed_instruction = reinterpret_cast<const DrawListDrawIndexedInstruction *>(instruction);
  1100. print_line("\tDRAW INDICES", draw_indexed_instruction->index_count, "INSTANCES", draw_indexed_instruction->instance_count, "FIRST INDEX", draw_indexed_instruction->first_index);
  1101. instruction_data_cursor += sizeof(DrawListDrawIndexedInstruction);
  1102. } break;
  1103. case DrawListInstruction::TYPE_DRAW_INDIRECT: {
  1104. const DrawListDrawIndirectInstruction *draw_indirect_instruction = reinterpret_cast<const DrawListDrawIndirectInstruction *>(instruction);
  1105. print_line("\tDRAW INDIRECT BUFFER ID", itos(draw_indirect_instruction->buffer.id), "OFFSET", draw_indirect_instruction->offset, "DRAW COUNT", draw_indirect_instruction->draw_count, "STRIDE", draw_indirect_instruction->stride);
  1106. instruction_data_cursor += sizeof(DrawListDrawIndirectInstruction);
  1107. } break;
  1108. case DrawListInstruction::TYPE_DRAW_INDEXED_INDIRECT: {
  1109. const DrawListDrawIndexedIndirectInstruction *draw_indexed_indirect_instruction = reinterpret_cast<const DrawListDrawIndexedIndirectInstruction *>(instruction);
  1110. print_line("\tDRAW INDEXED INDIRECT BUFFER ID", itos(draw_indexed_indirect_instruction->buffer.id), "OFFSET", draw_indexed_indirect_instruction->offset, "DRAW COUNT", draw_indexed_indirect_instruction->draw_count, "STRIDE", draw_indexed_indirect_instruction->stride);
  1111. instruction_data_cursor += sizeof(DrawListDrawIndexedIndirectInstruction);
  1112. } break;
  1113. case DrawListInstruction::TYPE_EXECUTE_COMMANDS: {
  1114. print_line("\tEXECUTE COMMANDS");
  1115. instruction_data_cursor += sizeof(DrawListExecuteCommandsInstruction);
  1116. } break;
  1117. case DrawListInstruction::TYPE_NEXT_SUBPASS: {
  1118. print_line("\tNEXT SUBPASS");
  1119. instruction_data_cursor += sizeof(DrawListNextSubpassInstruction);
  1120. } break;
  1121. case DrawListInstruction::TYPE_SET_BLEND_CONSTANTS: {
  1122. const DrawListSetBlendConstantsInstruction *set_blend_constants_instruction = reinterpret_cast<const DrawListSetBlendConstantsInstruction *>(instruction);
  1123. print_line("\tSET BLEND CONSTANTS COLOR", set_blend_constants_instruction->color);
  1124. instruction_data_cursor += sizeof(DrawListSetBlendConstantsInstruction);
  1125. } break;
  1126. case DrawListInstruction::TYPE_SET_LINE_WIDTH: {
  1127. const DrawListSetLineWidthInstruction *set_line_width_instruction = reinterpret_cast<const DrawListSetLineWidthInstruction *>(instruction);
  1128. print_line("\tSET LINE WIDTH", set_line_width_instruction->width);
  1129. instruction_data_cursor += sizeof(DrawListSetLineWidthInstruction);
  1130. } break;
  1131. case DrawListInstruction::TYPE_SET_PUSH_CONSTANT: {
  1132. const DrawListSetPushConstantInstruction *set_push_constant_instruction = reinterpret_cast<const DrawListSetPushConstantInstruction *>(instruction);
  1133. print_line("\tSET PUSH CONSTANT SIZE", set_push_constant_instruction->size);
  1134. instruction_data_cursor += sizeof(DrawListSetPushConstantInstruction);
  1135. instruction_data_cursor += set_push_constant_instruction->size;
  1136. } break;
  1137. case DrawListInstruction::TYPE_SET_SCISSOR: {
  1138. const DrawListSetScissorInstruction *set_scissor_instruction = reinterpret_cast<const DrawListSetScissorInstruction *>(instruction);
  1139. print_line("\tSET SCISSOR", set_scissor_instruction->rect);
  1140. instruction_data_cursor += sizeof(DrawListSetScissorInstruction);
  1141. } break;
  1142. case DrawListInstruction::TYPE_SET_VIEWPORT: {
  1143. const DrawListSetViewportInstruction *set_viewport_instruction = reinterpret_cast<const DrawListSetViewportInstruction *>(instruction);
  1144. print_line("\tSET VIEWPORT", set_viewport_instruction->rect);
  1145. instruction_data_cursor += sizeof(DrawListSetViewportInstruction);
  1146. } break;
  1147. case DrawListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE: {
  1148. const DrawListUniformSetPrepareForUseInstruction *uniform_set_prepare_for_use_instruction = reinterpret_cast<const DrawListUniformSetPrepareForUseInstruction *>(instruction);
  1149. print_line("\tUNIFORM SET PREPARE FOR USE ID", itos(uniform_set_prepare_for_use_instruction->uniform_set.id), "SHADER ID", itos(uniform_set_prepare_for_use_instruction->shader.id), "INDEX", uniform_set_prepare_for_use_instruction->set_index);
  1150. instruction_data_cursor += sizeof(DrawListUniformSetPrepareForUseInstruction);
  1151. } break;
  1152. default:
  1153. DEV_ASSERT(false && "Unknown draw list instruction type.");
  1154. return;
  1155. }
  1156. }
  1157. }
  1158. void RenderingDeviceGraph::_print_compute_list(const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {
  1159. uint32_t instruction_data_cursor = 0;
  1160. while (instruction_data_cursor < p_instruction_data_size) {
  1161. DEV_ASSERT((instruction_data_cursor + sizeof(ComputeListInstruction)) <= p_instruction_data_size);
  1162. const ComputeListInstruction *instruction = reinterpret_cast<const ComputeListInstruction *>(&p_instruction_data[instruction_data_cursor]);
  1163. switch (instruction->type) {
  1164. case ComputeListInstruction::TYPE_BIND_PIPELINE: {
  1165. const ComputeListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const ComputeListBindPipelineInstruction *>(instruction);
  1166. print_line("\tBIND PIPELINE ID", itos(bind_pipeline_instruction->pipeline.id));
  1167. instruction_data_cursor += sizeof(ComputeListBindPipelineInstruction);
  1168. } break;
  1169. case ComputeListInstruction::TYPE_BIND_UNIFORM_SET: {
  1170. const ComputeListBindUniformSetInstruction *bind_uniform_set_instruction = reinterpret_cast<const ComputeListBindUniformSetInstruction *>(instruction);
  1171. print_line("\tBIND UNIFORM SET ID", itos(bind_uniform_set_instruction->uniform_set.id), "SHADER ID", itos(bind_uniform_set_instruction->shader.id));
  1172. instruction_data_cursor += sizeof(ComputeListBindUniformSetInstruction);
  1173. } break;
  1174. case ComputeListInstruction::TYPE_DISPATCH: {
  1175. const ComputeListDispatchInstruction *dispatch_instruction = reinterpret_cast<const ComputeListDispatchInstruction *>(instruction);
  1176. print_line("\tDISPATCH", dispatch_instruction->x_groups, dispatch_instruction->y_groups, dispatch_instruction->z_groups);
  1177. instruction_data_cursor += sizeof(ComputeListDispatchInstruction);
  1178. } break;
  1179. case ComputeListInstruction::TYPE_DISPATCH_INDIRECT: {
  1180. const ComputeListDispatchIndirectInstruction *dispatch_indirect_instruction = reinterpret_cast<const ComputeListDispatchIndirectInstruction *>(instruction);
  1181. print_line("\tDISPATCH INDIRECT BUFFER ID", itos(dispatch_indirect_instruction->buffer.id), "OFFSET", dispatch_indirect_instruction->offset);
  1182. instruction_data_cursor += sizeof(ComputeListDispatchIndirectInstruction);
  1183. } break;
  1184. case ComputeListInstruction::TYPE_SET_PUSH_CONSTANT: {
  1185. const ComputeListSetPushConstantInstruction *set_push_constant_instruction = reinterpret_cast<const ComputeListSetPushConstantInstruction *>(instruction);
  1186. print_line("\tSET PUSH CONSTANT SIZE", set_push_constant_instruction->size);
  1187. instruction_data_cursor += sizeof(ComputeListSetPushConstantInstruction);
  1188. instruction_data_cursor += set_push_constant_instruction->size;
  1189. } break;
  1190. case ComputeListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE: {
  1191. const ComputeListUniformSetPrepareForUseInstruction *uniform_set_prepare_for_use_instruction = reinterpret_cast<const ComputeListUniformSetPrepareForUseInstruction *>(instruction);
  1192. print_line("\tUNIFORM SET PREPARE FOR USE ID", itos(uniform_set_prepare_for_use_instruction->uniform_set.id), "SHADER ID", itos(uniform_set_prepare_for_use_instruction->shader.id), "INDEX", itos(uniform_set_prepare_for_use_instruction->set_index));
  1193. instruction_data_cursor += sizeof(ComputeListUniformSetPrepareForUseInstruction);
  1194. } break;
  1195. default:
  1196. DEV_ASSERT(false && "Unknown compute list instruction type.");
  1197. return;
  1198. }
  1199. }
  1200. }
  1201. void RenderingDeviceGraph::initialize(RDD *p_driver, RenderingContextDriver::Device p_device, uint32_t p_frame_count, RDD::CommandQueueFamilyID p_secondary_command_queue_family, uint32_t p_secondary_command_buffers_per_frame) {
  1202. driver = p_driver;
  1203. device = p_device;
  1204. frames.resize(p_frame_count);
  1205. for (uint32_t i = 0; i < p_frame_count; i++) {
  1206. frames[i].secondary_command_buffers.resize(p_secondary_command_buffers_per_frame);
  1207. for (uint32_t j = 0; j < p_secondary_command_buffers_per_frame; j++) {
  1208. SecondaryCommandBuffer &secondary = frames[i].secondary_command_buffers[j];
  1209. secondary.command_pool = driver->command_pool_create(p_secondary_command_queue_family, RDD::COMMAND_BUFFER_TYPE_SECONDARY);
  1210. secondary.command_buffer = driver->command_buffer_create(secondary.command_pool);
  1211. secondary.task = WorkerThreadPool::INVALID_TASK_ID;
  1212. }
  1213. }
  1214. driver_honors_barriers = driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS);
  1215. driver_clears_with_copy_engine = driver->api_trait_get(RDD::API_TRAIT_CLEARS_WITH_COPY_ENGINE);
  1216. driver_buffers_require_transitions = driver->api_trait_get(RDD::API_TRAIT_BUFFERS_REQUIRE_TRANSITIONS);
  1217. }
  1218. void RenderingDeviceGraph::finalize() {
  1219. _wait_for_secondary_command_buffer_tasks();
  1220. for (Frame &f : frames) {
  1221. for (SecondaryCommandBuffer &secondary : f.secondary_command_buffers) {
  1222. if (secondary.command_pool.id != 0) {
  1223. driver->command_pool_free(secondary.command_pool);
  1224. }
  1225. }
  1226. }
  1227. frames.clear();
  1228. }
  1229. void RenderingDeviceGraph::begin() {
  1230. command_data.clear();
  1231. command_data_offsets.clear();
  1232. command_normalization_barriers.clear();
  1233. command_transition_barriers.clear();
  1234. command_buffer_barriers.clear();
  1235. command_label_chars.clear();
  1236. command_label_colors.clear();
  1237. command_label_offsets.clear();
  1238. command_list_nodes.clear();
  1239. read_slice_list_nodes.clear();
  1240. write_slice_list_nodes.clear();
  1241. command_count = 0;
  1242. command_label_count = 0;
  1243. command_timestamp_index = -1;
  1244. command_synchronization_index = -1;
  1245. command_synchronization_pending = false;
  1246. command_label_index = -1;
  1247. frames[frame].secondary_command_buffers_used = 0;
  1248. draw_instruction_list.index = 0;
  1249. compute_instruction_list.index = 0;
  1250. tracking_frame++;
  1251. #ifdef DEV_ENABLED
  1252. write_dependency_counters.clear();
  1253. #endif
  1254. }
  1255. void RenderingDeviceGraph::add_buffer_clear(RDD::BufferID p_dst, ResourceTracker *p_dst_tracker, uint32_t p_offset, uint32_t p_size) {
  1256. DEV_ASSERT(p_dst_tracker != nullptr);
  1257. int32_t command_index;
  1258. RecordedBufferClearCommand *command = static_cast<RecordedBufferClearCommand *>(_allocate_command(sizeof(RecordedBufferClearCommand), command_index));
  1259. command->type = RecordedCommand::TYPE_BUFFER_CLEAR;
  1260. command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;
  1261. command->buffer = p_dst;
  1262. command->offset = p_offset;
  1263. command->size = p_size;
  1264. ResourceUsage usage = RESOURCE_USAGE_COPY_TO;
  1265. _add_command_to_graph(&p_dst_tracker, &usage, 1, command_index, command);
  1266. }
  1267. void RenderingDeviceGraph::add_buffer_copy(RDD::BufferID p_src, ResourceTracker *p_src_tracker, RDD::BufferID p_dst, ResourceTracker *p_dst_tracker, RDD::BufferCopyRegion p_region) {
  1268. // Source tracker is allowed to be null as it could be a read-only buffer.
  1269. DEV_ASSERT(p_dst_tracker != nullptr);
  1270. int32_t command_index;
  1271. RecordedBufferCopyCommand *command = static_cast<RecordedBufferCopyCommand *>(_allocate_command(sizeof(RecordedBufferCopyCommand), command_index));
  1272. command->type = RecordedCommand::TYPE_BUFFER_COPY;
  1273. command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;
  1274. command->source = p_src;
  1275. command->destination = p_dst;
  1276. command->region = p_region;
  1277. ResourceTracker *trackers[2] = { p_dst_tracker, p_src_tracker };
  1278. ResourceUsage usages[2] = { RESOURCE_USAGE_COPY_TO, RESOURCE_USAGE_COPY_FROM };
  1279. _add_command_to_graph(trackers, usages, p_src_tracker != nullptr ? 2 : 1, command_index, command);
  1280. }
  1281. void RenderingDeviceGraph::add_buffer_get_data(RDD::BufferID p_src, ResourceTracker *p_src_tracker, RDD::BufferID p_dst, RDD::BufferCopyRegion p_region) {
  1282. // Source tracker is allowed to be null as it could be a read-only buffer.
  1283. int32_t command_index;
  1284. RecordedBufferGetDataCommand *command = static_cast<RecordedBufferGetDataCommand *>(_allocate_command(sizeof(RecordedBufferGetDataCommand), command_index));
  1285. command->type = RecordedCommand::TYPE_BUFFER_GET_DATA;
  1286. command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;
  1287. command->source = p_src;
  1288. command->destination = p_dst;
  1289. command->region = p_region;
  1290. if (p_src_tracker != nullptr) {
  1291. ResourceUsage usage = RESOURCE_USAGE_COPY_FROM;
  1292. _add_command_to_graph(&p_src_tracker, &usage, 1, command_index, command);
  1293. } else {
  1294. _add_command_to_graph(nullptr, nullptr, 0, command_index, command);
  1295. }
  1296. }
  1297. void RenderingDeviceGraph::add_buffer_update(RDD::BufferID p_dst, ResourceTracker *p_dst_tracker, VectorView<RecordedBufferCopy> p_buffer_copies) {
  1298. DEV_ASSERT(p_dst_tracker != nullptr);
  1299. size_t buffer_copies_size = p_buffer_copies.size() * sizeof(RecordedBufferCopy);
  1300. uint64_t command_size = sizeof(RecordedBufferUpdateCommand) + buffer_copies_size;
  1301. int32_t command_index;
  1302. RecordedBufferUpdateCommand *command = static_cast<RecordedBufferUpdateCommand *>(_allocate_command(command_size, command_index));
  1303. command->type = RecordedCommand::TYPE_BUFFER_UPDATE;
  1304. command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;
  1305. command->destination = p_dst;
  1306. command->buffer_copies_count = p_buffer_copies.size();
  1307. RecordedBufferCopy *buffer_copies = command->buffer_copies();
  1308. for (uint32_t i = 0; i < command->buffer_copies_count; i++) {
  1309. buffer_copies[i] = p_buffer_copies[i];
  1310. }
  1311. ResourceUsage buffer_usage = RESOURCE_USAGE_COPY_TO;
  1312. _add_command_to_graph(&p_dst_tracker, &buffer_usage, 1, command_index, command);
  1313. }
  1314. void RenderingDeviceGraph::add_compute_list_begin(RDD::BreadcrumbMarker p_phase, uint32_t p_breadcrumb_data) {
  1315. compute_instruction_list.clear();
  1316. #if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
  1317. compute_instruction_list.breadcrumb = p_breadcrumb_data | (p_phase & ((1 << 16) - 1));
  1318. #endif
  1319. compute_instruction_list.index++;
  1320. }
  1321. void RenderingDeviceGraph::add_compute_list_bind_pipeline(RDD::PipelineID p_pipeline) {
  1322. ComputeListBindPipelineInstruction *instruction = reinterpret_cast<ComputeListBindPipelineInstruction *>(_allocate_compute_list_instruction(sizeof(ComputeListBindPipelineInstruction)));
  1323. instruction->type = ComputeListInstruction::TYPE_BIND_PIPELINE;
  1324. instruction->pipeline = p_pipeline;
  1325. compute_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT);
  1326. }
  1327. void RenderingDeviceGraph::add_compute_list_bind_uniform_set(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index) {
  1328. ComputeListBindUniformSetInstruction *instruction = reinterpret_cast<ComputeListBindUniformSetInstruction *>(_allocate_compute_list_instruction(sizeof(ComputeListBindUniformSetInstruction)));
  1329. instruction->type = ComputeListInstruction::TYPE_BIND_UNIFORM_SET;
  1330. instruction->shader = p_shader;
  1331. instruction->uniform_set = p_uniform_set;
  1332. instruction->set_index = set_index;
  1333. }
  1334. void RenderingDeviceGraph::add_compute_list_dispatch(uint32_t p_x_groups, uint32_t p_y_groups, uint32_t p_z_groups) {
  1335. ComputeListDispatchInstruction *instruction = reinterpret_cast<ComputeListDispatchInstruction *>(_allocate_compute_list_instruction(sizeof(ComputeListDispatchInstruction)));
  1336. instruction->type = ComputeListInstruction::TYPE_DISPATCH;
  1337. instruction->x_groups = p_x_groups;
  1338. instruction->y_groups = p_y_groups;
  1339. instruction->z_groups = p_z_groups;
  1340. }
  1341. void RenderingDeviceGraph::add_compute_list_dispatch_indirect(RDD::BufferID p_buffer, uint32_t p_offset) {
  1342. ComputeListDispatchIndirectInstruction *instruction = reinterpret_cast<ComputeListDispatchIndirectInstruction *>(_allocate_compute_list_instruction(sizeof(ComputeListDispatchIndirectInstruction)));
  1343. instruction->type = ComputeListInstruction::TYPE_DISPATCH_INDIRECT;
  1344. instruction->buffer = p_buffer;
  1345. instruction->offset = p_offset;
  1346. compute_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_DRAW_INDIRECT_BIT);
  1347. }
  1348. void RenderingDeviceGraph::add_compute_list_set_push_constant(RDD::ShaderID p_shader, const void *p_data, uint32_t p_data_size) {
  1349. uint32_t instruction_size = sizeof(ComputeListSetPushConstantInstruction) + p_data_size;
  1350. ComputeListSetPushConstantInstruction *instruction = reinterpret_cast<ComputeListSetPushConstantInstruction *>(_allocate_compute_list_instruction(instruction_size));
  1351. instruction->type = ComputeListInstruction::TYPE_SET_PUSH_CONSTANT;
  1352. instruction->size = p_data_size;
  1353. instruction->shader = p_shader;
  1354. memcpy(instruction->data(), p_data, p_data_size);
  1355. }
  1356. void RenderingDeviceGraph::add_compute_list_uniform_set_prepare_for_use(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index) {
  1357. ComputeListUniformSetPrepareForUseInstruction *instruction = reinterpret_cast<ComputeListUniformSetPrepareForUseInstruction *>(_allocate_compute_list_instruction(sizeof(ComputeListUniformSetPrepareForUseInstruction)));
  1358. instruction->type = ComputeListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE;
  1359. instruction->shader = p_shader;
  1360. instruction->uniform_set = p_uniform_set;
  1361. instruction->set_index = set_index;
  1362. }
  1363. void RenderingDeviceGraph::add_compute_list_usage(ResourceTracker *p_tracker, ResourceUsage p_usage) {
  1364. DEV_ASSERT(p_tracker != nullptr);
  1365. p_tracker->reset_if_outdated(tracking_frame);
  1366. if (p_tracker->compute_list_index != compute_instruction_list.index) {
  1367. compute_instruction_list.command_trackers.push_back(p_tracker);
  1368. compute_instruction_list.command_tracker_usages.push_back(p_usage);
  1369. p_tracker->compute_list_index = compute_instruction_list.index;
  1370. p_tracker->compute_list_usage = p_usage;
  1371. }
  1372. #ifdef DEV_ENABLED
  1373. else if (p_tracker->compute_list_usage != p_usage) {
  1374. ERR_FAIL_MSG(vformat("Tracker can't have more than one type of usage in the same compute list. Compute list usage is %d and the requested usage is %d.", p_tracker->compute_list_usage, p_usage));
  1375. }
  1376. #endif
  1377. }
  1378. void RenderingDeviceGraph::add_compute_list_usages(VectorView<ResourceTracker *> p_trackers, VectorView<ResourceUsage> p_usages) {
  1379. DEV_ASSERT(p_trackers.size() == p_usages.size());
  1380. for (uint32_t i = 0; i < p_trackers.size(); i++) {
  1381. add_compute_list_usage(p_trackers[i], p_usages[i]);
  1382. }
  1383. }
  1384. void RenderingDeviceGraph::add_compute_list_end() {
  1385. int32_t command_index;
  1386. uint32_t instruction_data_size = compute_instruction_list.data.size();
  1387. uint32_t command_size = sizeof(RecordedComputeListCommand) + instruction_data_size;
  1388. RecordedComputeListCommand *command = static_cast<RecordedComputeListCommand *>(_allocate_command(command_size, command_index));
  1389. command->type = RecordedCommand::TYPE_COMPUTE_LIST;
  1390. command->self_stages = compute_instruction_list.stages;
  1391. command->instruction_data_size = instruction_data_size;
  1392. memcpy(command->instruction_data(), compute_instruction_list.data.ptr(), instruction_data_size);
  1393. _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);
  1394. }
  1395. void RenderingDeviceGraph::add_draw_list_begin(RDD::RenderPassID p_render_pass, RDD::FramebufferID p_framebuffer, Rect2i p_region, VectorView<RDD::RenderPassClearValue> p_clear_values, bool p_uses_color, bool p_uses_depth, uint32_t p_breadcrumb) {
  1396. draw_instruction_list.clear();
  1397. draw_instruction_list.index++;
  1398. draw_instruction_list.render_pass = p_render_pass;
  1399. draw_instruction_list.framebuffer = p_framebuffer;
  1400. draw_instruction_list.region = p_region;
  1401. #if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
  1402. draw_instruction_list.breadcrumb = p_breadcrumb;
  1403. #endif
  1404. draw_instruction_list.clear_values.resize(p_clear_values.size());
  1405. for (uint32_t i = 0; i < p_clear_values.size(); i++) {
  1406. draw_instruction_list.clear_values[i] = p_clear_values[i];
  1407. }
  1408. if (p_uses_color) {
  1409. draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT);
  1410. }
  1411. if (p_uses_depth) {
  1412. draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT);
  1413. draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT);
  1414. }
  1415. }
  1416. void RenderingDeviceGraph::add_draw_list_bind_index_buffer(RDD::BufferID p_buffer, RDD::IndexBufferFormat p_format, uint32_t p_offset) {
  1417. DrawListBindIndexBufferInstruction *instruction = reinterpret_cast<DrawListBindIndexBufferInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListBindIndexBufferInstruction)));
  1418. instruction->type = DrawListInstruction::TYPE_BIND_INDEX_BUFFER;
  1419. instruction->buffer = p_buffer;
  1420. instruction->format = p_format;
  1421. instruction->offset = p_offset;
  1422. if (instruction->buffer.id != 0) {
  1423. draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_VERTEX_INPUT_BIT);
  1424. }
  1425. }
  1426. void RenderingDeviceGraph::add_draw_list_bind_pipeline(RDD::PipelineID p_pipeline, BitField<RDD::PipelineStageBits> p_pipeline_stage_bits) {
  1427. DrawListBindPipelineInstruction *instruction = reinterpret_cast<DrawListBindPipelineInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListBindPipelineInstruction)));
  1428. instruction->type = DrawListInstruction::TYPE_BIND_PIPELINE;
  1429. instruction->pipeline = p_pipeline;
  1430. draw_instruction_list.stages = draw_instruction_list.stages | p_pipeline_stage_bits;
  1431. }
  1432. void RenderingDeviceGraph::add_draw_list_bind_uniform_set(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index) {
  1433. DrawListBindUniformSetInstruction *instruction = reinterpret_cast<DrawListBindUniformSetInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListBindUniformSetInstruction)));
  1434. instruction->type = DrawListInstruction::TYPE_BIND_UNIFORM_SET;
  1435. instruction->shader = p_shader;
  1436. instruction->uniform_set = p_uniform_set;
  1437. instruction->set_index = set_index;
  1438. }
  1439. void RenderingDeviceGraph::add_draw_list_bind_vertex_buffers(VectorView<RDD::BufferID> p_vertex_buffers, VectorView<uint64_t> p_vertex_buffer_offsets) {
  1440. DEV_ASSERT(p_vertex_buffers.size() == p_vertex_buffer_offsets.size());
  1441. uint32_t instruction_size = sizeof(DrawListBindVertexBuffersInstruction) + sizeof(RDD::BufferID) * p_vertex_buffers.size() + sizeof(uint64_t) * p_vertex_buffer_offsets.size();
  1442. DrawListBindVertexBuffersInstruction *instruction = reinterpret_cast<DrawListBindVertexBuffersInstruction *>(_allocate_draw_list_instruction(instruction_size));
  1443. instruction->type = DrawListInstruction::TYPE_BIND_VERTEX_BUFFERS;
  1444. instruction->vertex_buffers_count = p_vertex_buffers.size();
  1445. RDD::BufferID *vertex_buffers = instruction->vertex_buffers();
  1446. uint64_t *vertex_buffer_offsets = instruction->vertex_buffer_offsets();
  1447. for (uint32_t i = 0; i < instruction->vertex_buffers_count; i++) {
  1448. vertex_buffers[i] = p_vertex_buffers[i];
  1449. vertex_buffer_offsets[i] = p_vertex_buffer_offsets[i];
  1450. }
  1451. if (instruction->vertex_buffers_count > 0) {
  1452. draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_VERTEX_INPUT_BIT);
  1453. }
  1454. }
  1455. void RenderingDeviceGraph::add_draw_list_clear_attachments(VectorView<RDD::AttachmentClear> p_attachments_clear, VectorView<Rect2i> p_attachments_clear_rect) {
  1456. uint32_t instruction_size = sizeof(DrawListClearAttachmentsInstruction) + sizeof(RDD::AttachmentClear) * p_attachments_clear.size() + sizeof(Rect2i) * p_attachments_clear_rect.size();
  1457. DrawListClearAttachmentsInstruction *instruction = reinterpret_cast<DrawListClearAttachmentsInstruction *>(_allocate_draw_list_instruction(instruction_size));
  1458. instruction->type = DrawListInstruction::TYPE_CLEAR_ATTACHMENTS;
  1459. instruction->attachments_clear_count = p_attachments_clear.size();
  1460. instruction->attachments_clear_rect_count = p_attachments_clear_rect.size();
  1461. RDD::AttachmentClear *attachments_clear = instruction->attachments_clear();
  1462. Rect2i *attachments_clear_rect = instruction->attachments_clear_rect();
  1463. for (uint32_t i = 0; i < instruction->attachments_clear_count; i++) {
  1464. attachments_clear[i] = p_attachments_clear[i];
  1465. }
  1466. for (uint32_t i = 0; i < instruction->attachments_clear_rect_count; i++) {
  1467. attachments_clear_rect[i] = p_attachments_clear_rect[i];
  1468. }
  1469. }
  1470. void RenderingDeviceGraph::add_draw_list_draw(uint32_t p_vertex_count, uint32_t p_instance_count) {
  1471. DrawListDrawInstruction *instruction = reinterpret_cast<DrawListDrawInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListDrawInstruction)));
  1472. instruction->type = DrawListInstruction::TYPE_DRAW;
  1473. instruction->vertex_count = p_vertex_count;
  1474. instruction->instance_count = p_instance_count;
  1475. }
  1476. void RenderingDeviceGraph::add_draw_list_draw_indexed(uint32_t p_index_count, uint32_t p_instance_count, uint32_t p_first_index) {
  1477. DrawListDrawIndexedInstruction *instruction = reinterpret_cast<DrawListDrawIndexedInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListDrawIndexedInstruction)));
  1478. instruction->type = DrawListInstruction::TYPE_DRAW_INDEXED;
  1479. instruction->index_count = p_index_count;
  1480. instruction->instance_count = p_instance_count;
  1481. instruction->first_index = p_first_index;
  1482. }
  1483. void RenderingDeviceGraph::add_draw_list_draw_indirect(RDD::BufferID p_buffer, uint32_t p_offset, uint32_t p_draw_count, uint32_t p_stride) {
  1484. DrawListDrawIndirectInstruction *instruction = reinterpret_cast<DrawListDrawIndirectInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListDrawIndirectInstruction)));
  1485. instruction->type = DrawListInstruction::TYPE_DRAW_INDIRECT;
  1486. instruction->buffer = p_buffer;
  1487. instruction->offset = p_offset;
  1488. instruction->draw_count = p_draw_count;
  1489. instruction->stride = p_stride;
  1490. draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_DRAW_INDIRECT_BIT);
  1491. }
  1492. void RenderingDeviceGraph::add_draw_list_draw_indexed_indirect(RDD::BufferID p_buffer, uint32_t p_offset, uint32_t p_draw_count, uint32_t p_stride) {
  1493. DrawListDrawIndexedIndirectInstruction *instruction = reinterpret_cast<DrawListDrawIndexedIndirectInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListDrawIndexedIndirectInstruction)));
  1494. instruction->type = DrawListInstruction::TYPE_DRAW_INDEXED_INDIRECT;
  1495. instruction->buffer = p_buffer;
  1496. instruction->offset = p_offset;
  1497. instruction->draw_count = p_draw_count;
  1498. instruction->stride = p_stride;
  1499. draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_DRAW_INDIRECT_BIT);
  1500. }
  1501. void RenderingDeviceGraph::add_draw_list_execute_commands(RDD::CommandBufferID p_command_buffer) {
  1502. DrawListExecuteCommandsInstruction *instruction = reinterpret_cast<DrawListExecuteCommandsInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListExecuteCommandsInstruction)));
  1503. instruction->type = DrawListInstruction::TYPE_EXECUTE_COMMANDS;
  1504. instruction->command_buffer = p_command_buffer;
  1505. }
  1506. void RenderingDeviceGraph::add_draw_list_next_subpass(RDD::CommandBufferType p_command_buffer_type) {
  1507. DrawListNextSubpassInstruction *instruction = reinterpret_cast<DrawListNextSubpassInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListNextSubpassInstruction)));
  1508. instruction->type = DrawListInstruction::TYPE_NEXT_SUBPASS;
  1509. instruction->command_buffer_type = p_command_buffer_type;
  1510. }
  1511. void RenderingDeviceGraph::add_draw_list_set_blend_constants(const Color &p_color) {
  1512. DrawListSetBlendConstantsInstruction *instruction = reinterpret_cast<DrawListSetBlendConstantsInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListSetBlendConstantsInstruction)));
  1513. instruction->type = DrawListInstruction::TYPE_SET_BLEND_CONSTANTS;
  1514. instruction->color = p_color;
  1515. }
  1516. void RenderingDeviceGraph::add_draw_list_set_line_width(float p_width) {
  1517. DrawListSetLineWidthInstruction *instruction = reinterpret_cast<DrawListSetLineWidthInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListSetLineWidthInstruction)));
  1518. instruction->type = DrawListInstruction::TYPE_SET_LINE_WIDTH;
  1519. instruction->width = p_width;
  1520. }
  1521. void RenderingDeviceGraph::add_draw_list_set_push_constant(RDD::ShaderID p_shader, const void *p_data, uint32_t p_data_size) {
  1522. uint32_t instruction_size = sizeof(DrawListSetPushConstantInstruction) + p_data_size;
  1523. DrawListSetPushConstantInstruction *instruction = reinterpret_cast<DrawListSetPushConstantInstruction *>(_allocate_draw_list_instruction(instruction_size));
  1524. instruction->type = DrawListInstruction::TYPE_SET_PUSH_CONSTANT;
  1525. instruction->size = p_data_size;
  1526. instruction->shader = p_shader;
  1527. memcpy(instruction->data(), p_data, p_data_size);
  1528. }
  1529. void RenderingDeviceGraph::add_draw_list_set_scissor(Rect2i p_rect) {
  1530. DrawListSetScissorInstruction *instruction = reinterpret_cast<DrawListSetScissorInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListSetScissorInstruction)));
  1531. instruction->type = DrawListInstruction::TYPE_SET_SCISSOR;
  1532. instruction->rect = p_rect;
  1533. }
  1534. void RenderingDeviceGraph::add_draw_list_set_viewport(Rect2i p_rect) {
  1535. DrawListSetViewportInstruction *instruction = reinterpret_cast<DrawListSetViewportInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListSetViewportInstruction)));
  1536. instruction->type = DrawListInstruction::TYPE_SET_VIEWPORT;
  1537. instruction->rect = p_rect;
  1538. }
  1539. void RenderingDeviceGraph::add_draw_list_uniform_set_prepare_for_use(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index) {
  1540. DrawListUniformSetPrepareForUseInstruction *instruction = reinterpret_cast<DrawListUniformSetPrepareForUseInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListUniformSetPrepareForUseInstruction)));
  1541. instruction->type = DrawListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE;
  1542. instruction->shader = p_shader;
  1543. instruction->uniform_set = p_uniform_set;
  1544. instruction->set_index = set_index;
  1545. }
  1546. void RenderingDeviceGraph::add_draw_list_usage(ResourceTracker *p_tracker, ResourceUsage p_usage) {
  1547. p_tracker->reset_if_outdated(tracking_frame);
  1548. if (p_tracker->draw_list_index != draw_instruction_list.index) {
  1549. draw_instruction_list.command_trackers.push_back(p_tracker);
  1550. draw_instruction_list.command_tracker_usages.push_back(p_usage);
  1551. p_tracker->draw_list_index = draw_instruction_list.index;
  1552. p_tracker->draw_list_usage = p_usage;
  1553. }
  1554. #ifdef DEV_ENABLED
  1555. else if (p_tracker->draw_list_usage != p_usage) {
  1556. ERR_FAIL_MSG(vformat("Tracker can't have more than one type of usage in the same draw list. Draw list usage is %d and the requested usage is %d.", p_tracker->draw_list_usage, p_usage));
  1557. }
  1558. #endif
  1559. }
  1560. void RenderingDeviceGraph::add_draw_list_usages(VectorView<ResourceTracker *> p_trackers, VectorView<ResourceUsage> p_usages) {
  1561. DEV_ASSERT(p_trackers.size() == p_usages.size());
  1562. for (uint32_t i = 0; i < p_trackers.size(); i++) {
  1563. add_draw_list_usage(p_trackers[i], p_usages[i]);
  1564. }
  1565. }
  1566. void RenderingDeviceGraph::add_draw_list_end() {
  1567. // Arbitrary size threshold to evaluate if it'd be best to record the draw list on the background as a secondary buffer.
  1568. const uint32_t instruction_data_threshold_for_secondary = 16384;
  1569. RDD::CommandBufferType command_buffer_type;
  1570. uint32_t &secondary_buffers_used = frames[frame].secondary_command_buffers_used;
  1571. if (draw_instruction_list.data.size() > instruction_data_threshold_for_secondary && secondary_buffers_used < frames[frame].secondary_command_buffers.size()) {
  1572. // Copy the current instruction list data into another array that will be used by the secondary command buffer worker.
  1573. SecondaryCommandBuffer &secondary = frames[frame].secondary_command_buffers[secondary_buffers_used];
  1574. secondary.render_pass = draw_instruction_list.render_pass;
  1575. secondary.framebuffer = draw_instruction_list.framebuffer;
  1576. secondary.instruction_data.resize(draw_instruction_list.data.size());
  1577. memcpy(secondary.instruction_data.ptr(), draw_instruction_list.data.ptr(), draw_instruction_list.data.size());
  1578. // Run a background task for recording the secondary command buffer.
  1579. secondary.task = WorkerThreadPool::get_singleton()->add_template_task(this, &RenderingDeviceGraph::_run_secondary_command_buffer_task, &secondary, true);
  1580. // Clear the instruction list and add a single command for executing the secondary command buffer instead.
  1581. draw_instruction_list.data.clear();
  1582. add_draw_list_execute_commands(secondary.command_buffer);
  1583. secondary_buffers_used++;
  1584. command_buffer_type = RDD::COMMAND_BUFFER_TYPE_SECONDARY;
  1585. } else {
  1586. command_buffer_type = RDD::COMMAND_BUFFER_TYPE_PRIMARY;
  1587. }
  1588. int32_t command_index;
  1589. uint32_t clear_values_size = sizeof(RDD::RenderPassClearValue) * draw_instruction_list.clear_values.size();
  1590. uint32_t instruction_data_size = draw_instruction_list.data.size();
  1591. uint32_t command_size = sizeof(RecordedDrawListCommand) + clear_values_size + instruction_data_size;
  1592. RecordedDrawListCommand *command = static_cast<RecordedDrawListCommand *>(_allocate_command(command_size, command_index));
  1593. command->type = RecordedCommand::TYPE_DRAW_LIST;
  1594. command->self_stages = draw_instruction_list.stages;
  1595. command->instruction_data_size = instruction_data_size;
  1596. command->render_pass = draw_instruction_list.render_pass;
  1597. command->framebuffer = draw_instruction_list.framebuffer;
  1598. command->command_buffer_type = command_buffer_type;
  1599. command->region = draw_instruction_list.region;
  1600. #if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
  1601. command->breadcrumb = draw_instruction_list.breadcrumb;
  1602. #endif
  1603. command->clear_values_count = draw_instruction_list.clear_values.size();
  1604. RDD::RenderPassClearValue *clear_values = command->clear_values();
  1605. for (uint32_t i = 0; i < command->clear_values_count; i++) {
  1606. clear_values[i] = draw_instruction_list.clear_values[i];
  1607. }
  1608. memcpy(command->instruction_data(), draw_instruction_list.data.ptr(), instruction_data_size);
  1609. _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);
  1610. }
  1611. void RenderingDeviceGraph::add_texture_clear(RDD::TextureID p_dst, ResourceTracker *p_dst_tracker, const Color &p_color, const RDD::TextureSubresourceRange &p_range) {
  1612. DEV_ASSERT(p_dst_tracker != nullptr);
  1613. int32_t command_index;
  1614. RecordedTextureClearCommand *command = static_cast<RecordedTextureClearCommand *>(_allocate_command(sizeof(RecordedTextureClearCommand), command_index));
  1615. command->type = RecordedCommand::TYPE_TEXTURE_CLEAR;
  1616. command->texture = p_dst;
  1617. command->color = p_color;
  1618. command->range = p_range;
  1619. ResourceUsage usage;
  1620. if (driver_clears_with_copy_engine) {
  1621. command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;
  1622. usage = RESOURCE_USAGE_COPY_TO;
  1623. } else {
  1624. // If the driver is uncapable of using the copy engine for clearing the image (e.g. D3D12), we must either transition the
  1625. // resource to a render target or a storage image as that's the only two ways it can perform the operation.
  1626. if (p_dst_tracker->texture_usage & RDD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT) {
  1627. command->self_stages = RDD::PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
  1628. usage = RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE;
  1629. } else {
  1630. command->self_stages = RDD::PIPELINE_STAGE_CLEAR_STORAGE_BIT;
  1631. usage = RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE;
  1632. }
  1633. }
  1634. _add_command_to_graph(&p_dst_tracker, &usage, 1, command_index, command);
  1635. }
  1636. void RenderingDeviceGraph::add_texture_copy(RDD::TextureID p_src, ResourceTracker *p_src_tracker, RDD::TextureID p_dst, ResourceTracker *p_dst_tracker, VectorView<RDD::TextureCopyRegion> p_texture_copy_regions) {
  1637. DEV_ASSERT(p_src_tracker != nullptr);
  1638. DEV_ASSERT(p_dst_tracker != nullptr);
  1639. int32_t command_index;
  1640. uint64_t command_size = sizeof(RecordedTextureCopyCommand) + p_texture_copy_regions.size() * sizeof(RDD::TextureCopyRegion);
  1641. RecordedTextureCopyCommand *command = static_cast<RecordedTextureCopyCommand *>(_allocate_command(command_size, command_index));
  1642. command->type = RecordedCommand::TYPE_TEXTURE_COPY;
  1643. command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;
  1644. command->from_texture = p_src;
  1645. command->to_texture = p_dst;
  1646. command->texture_copy_regions_count = p_texture_copy_regions.size();
  1647. RDD::TextureCopyRegion *texture_copy_regions = command->texture_copy_regions();
  1648. for (uint32_t i = 0; i < command->texture_copy_regions_count; i++) {
  1649. texture_copy_regions[i] = p_texture_copy_regions[i];
  1650. }
  1651. ResourceTracker *trackers[2] = { p_dst_tracker, p_src_tracker };
  1652. ResourceUsage usages[2] = { RESOURCE_USAGE_COPY_TO, RESOURCE_USAGE_COPY_FROM };
  1653. _add_command_to_graph(trackers, usages, 2, command_index, command);
  1654. }
  1655. void RenderingDeviceGraph::add_texture_get_data(RDD::TextureID p_src, ResourceTracker *p_src_tracker, RDD::BufferID p_dst, VectorView<RDD::BufferTextureCopyRegion> p_buffer_texture_copy_regions, ResourceTracker *p_dst_tracker) {
  1656. DEV_ASSERT(p_src_tracker != nullptr);
  1657. int32_t command_index;
  1658. uint64_t command_size = sizeof(RecordedTextureGetDataCommand) + p_buffer_texture_copy_regions.size() * sizeof(RDD::BufferTextureCopyRegion);
  1659. RecordedTextureGetDataCommand *command = static_cast<RecordedTextureGetDataCommand *>(_allocate_command(command_size, command_index));
  1660. command->type = RecordedCommand::TYPE_TEXTURE_GET_DATA;
  1661. command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;
  1662. command->from_texture = p_src;
  1663. command->to_buffer = p_dst;
  1664. command->buffer_texture_copy_regions_count = p_buffer_texture_copy_regions.size();
  1665. RDD::BufferTextureCopyRegion *buffer_texture_copy_regions = command->buffer_texture_copy_regions();
  1666. for (uint32_t i = 0; i < command->buffer_texture_copy_regions_count; i++) {
  1667. buffer_texture_copy_regions[i] = p_buffer_texture_copy_regions[i];
  1668. }
  1669. if (p_dst_tracker != nullptr) {
  1670. // Add the optional destination tracker if it was provided.
  1671. ResourceTracker *trackers[2] = { p_dst_tracker, p_src_tracker };
  1672. ResourceUsage usages[2] = { RESOURCE_USAGE_COPY_TO, RESOURCE_USAGE_COPY_FROM };
  1673. _add_command_to_graph(trackers, usages, 2, command_index, command);
  1674. } else {
  1675. ResourceUsage usage = RESOURCE_USAGE_COPY_FROM;
  1676. _add_command_to_graph(&p_src_tracker, &usage, 1, command_index, command);
  1677. }
  1678. }
  1679. void RenderingDeviceGraph::add_texture_resolve(RDD::TextureID p_src, ResourceTracker *p_src_tracker, RDD::TextureID p_dst, ResourceTracker *p_dst_tracker, uint32_t p_src_layer, uint32_t p_src_mipmap, uint32_t p_dst_layer, uint32_t p_dst_mipmap) {
  1680. DEV_ASSERT(p_src_tracker != nullptr);
  1681. DEV_ASSERT(p_dst_tracker != nullptr);
  1682. int32_t command_index;
  1683. RecordedTextureResolveCommand *command = static_cast<RecordedTextureResolveCommand *>(_allocate_command(sizeof(RecordedTextureResolveCommand), command_index));
  1684. command->type = RecordedCommand::TYPE_TEXTURE_RESOLVE;
  1685. command->self_stages = RDD::PIPELINE_STAGE_RESOLVE_BIT;
  1686. command->from_texture = p_src;
  1687. command->to_texture = p_dst;
  1688. command->src_layer = p_src_layer;
  1689. command->src_mipmap = p_src_mipmap;
  1690. command->dst_layer = p_dst_layer;
  1691. command->dst_mipmap = p_dst_mipmap;
  1692. ResourceTracker *trackers[2] = { p_dst_tracker, p_src_tracker };
  1693. ResourceUsage usages[2] = { RESOURCE_USAGE_RESOLVE_TO, RESOURCE_USAGE_RESOLVE_FROM };
  1694. _add_command_to_graph(trackers, usages, 2, command_index, command);
  1695. }
  1696. void RenderingDeviceGraph::add_texture_update(RDD::TextureID p_dst, ResourceTracker *p_dst_tracker, VectorView<RecordedBufferToTextureCopy> p_buffer_copies, VectorView<ResourceTracker *> p_buffer_trackers) {
  1697. DEV_ASSERT(p_dst_tracker != nullptr);
  1698. int32_t command_index;
  1699. uint64_t command_size = sizeof(RecordedTextureUpdateCommand) + p_buffer_copies.size() * sizeof(RecordedBufferToTextureCopy);
  1700. RecordedTextureUpdateCommand *command = static_cast<RecordedTextureUpdateCommand *>(_allocate_command(command_size, command_index));
  1701. command->type = RecordedCommand::TYPE_TEXTURE_UPDATE;
  1702. command->self_stages = RDD::PIPELINE_STAGE_COPY_BIT;
  1703. command->to_texture = p_dst;
  1704. command->buffer_to_texture_copies_count = p_buffer_copies.size();
  1705. RecordedBufferToTextureCopy *buffer_to_texture_copies = command->buffer_to_texture_copies();
  1706. for (uint32_t i = 0; i < command->buffer_to_texture_copies_count; i++) {
  1707. buffer_to_texture_copies[i] = p_buffer_copies[i];
  1708. }
  1709. if (p_buffer_trackers.size() > 0) {
  1710. // Add the optional buffer trackers if they were provided.
  1711. thread_local LocalVector<ResourceTracker *> trackers;
  1712. thread_local LocalVector<ResourceUsage> usages;
  1713. trackers.clear();
  1714. usages.clear();
  1715. for (uint32_t i = 0; i < p_buffer_trackers.size(); i++) {
  1716. trackers.push_back(p_buffer_trackers[i]);
  1717. usages.push_back(RESOURCE_USAGE_COPY_FROM);
  1718. }
  1719. trackers.push_back(p_dst_tracker);
  1720. usages.push_back(RESOURCE_USAGE_COPY_TO);
  1721. _add_command_to_graph(trackers.ptr(), usages.ptr(), trackers.size(), command_index, command);
  1722. } else {
  1723. ResourceUsage usage = RESOURCE_USAGE_COPY_TO;
  1724. _add_command_to_graph(&p_dst_tracker, &usage, 1, command_index, command);
  1725. }
  1726. }
  1727. void RenderingDeviceGraph::add_capture_timestamp(RDD::QueryPoolID p_query_pool, uint32_t p_index) {
  1728. int32_t command_index;
  1729. RecordedCaptureTimestampCommand *command = static_cast<RecordedCaptureTimestampCommand *>(_allocate_command(sizeof(RecordedCaptureTimestampCommand), command_index));
  1730. command->type = RecordedCommand::TYPE_CAPTURE_TIMESTAMP;
  1731. command->self_stages = 0;
  1732. command->pool = p_query_pool;
  1733. command->index = p_index;
  1734. _add_command_to_graph(nullptr, nullptr, 0, command_index, command);
  1735. }
  1736. void RenderingDeviceGraph::add_synchronization() {
  1737. // Synchronization is only acknowledged if commands have been recorded on the graph already.
  1738. if (command_count > 0) {
  1739. command_synchronization_pending = true;
  1740. }
  1741. }
  1742. void RenderingDeviceGraph::begin_label(const String &p_label_name, const Color &p_color) {
  1743. uint32_t command_label_offset = command_label_chars.size();
  1744. PackedByteArray command_label_utf8 = p_label_name.to_utf8_buffer();
  1745. int command_label_utf8_size = command_label_utf8.size();
  1746. command_label_chars.resize(command_label_offset + command_label_utf8_size + 1);
  1747. memcpy(&command_label_chars[command_label_offset], command_label_utf8.ptr(), command_label_utf8.size());
  1748. command_label_chars[command_label_offset + command_label_utf8_size] = '\0';
  1749. command_label_colors.push_back(p_color);
  1750. command_label_offsets.push_back(command_label_offset);
  1751. command_label_index = command_label_count;
  1752. command_label_count++;
  1753. }
  1754. void RenderingDeviceGraph::end_label() {
  1755. command_label_index = -1;
  1756. }
  1757. void RenderingDeviceGraph::end(bool p_reorder_commands, bool p_full_barriers, RDD::CommandBufferID &r_command_buffer, CommandBufferPool &r_command_buffer_pool) {
  1758. if (command_count == 0) {
  1759. // No commands have been logged, do nothing.
  1760. return;
  1761. }
  1762. thread_local LocalVector<RecordedCommandSort> commands_sorted;
  1763. if (p_reorder_commands) {
  1764. thread_local LocalVector<int64_t> command_stack;
  1765. thread_local LocalVector<int32_t> sorted_command_indices;
  1766. thread_local LocalVector<uint32_t> command_degrees;
  1767. int32_t adjacency_list_index = 0;
  1768. int32_t command_index;
  1769. // Count all the incoming connections to every node by traversing their adjacency list.
  1770. command_degrees.resize(command_count);
  1771. memset(command_degrees.ptr(), 0, sizeof(uint32_t) * command_degrees.size());
  1772. for (uint32_t i = 0; i < command_count; i++) {
  1773. const RecordedCommand &recorded_command = *reinterpret_cast<const RecordedCommand *>(&command_data[command_data_offsets[i]]);
  1774. adjacency_list_index = recorded_command.adjacent_command_list_index;
  1775. while (adjacency_list_index >= 0) {
  1776. const RecordedCommandListNode &command_list_node = command_list_nodes[adjacency_list_index];
  1777. DEV_ASSERT((command_list_node.command_index != int32_t(i)) && "Command can't have itself as a dependency.");
  1778. command_degrees[command_list_node.command_index] += 1;
  1779. adjacency_list_index = command_list_node.next_list_index;
  1780. }
  1781. }
  1782. // Push to the stack all nodes that have no incoming connections.
  1783. command_stack.clear();
  1784. for (uint32_t i = 0; i < command_count; i++) {
  1785. if (command_degrees[i] == 0) {
  1786. command_stack.push_back(i);
  1787. }
  1788. }
  1789. sorted_command_indices.clear();
  1790. while (!command_stack.is_empty()) {
  1791. // Pop command from the stack.
  1792. command_index = command_stack[command_stack.size() - 1];
  1793. command_stack.resize(command_stack.size() - 1);
  1794. // Add it to the sorted commands.
  1795. sorted_command_indices.push_back(command_index);
  1796. // Search for its adjacents and lower their degree for every visit. If the degree reaches zero, we push the command to the stack.
  1797. const uint32_t command_data_offset = command_data_offsets[command_index];
  1798. const RecordedCommand &recorded_command = *reinterpret_cast<const RecordedCommand *>(&command_data[command_data_offset]);
  1799. adjacency_list_index = recorded_command.adjacent_command_list_index;
  1800. while (adjacency_list_index >= 0) {
  1801. const RecordedCommandListNode &command_list_node = command_list_nodes[adjacency_list_index];
  1802. uint32_t &command_degree = command_degrees[command_list_node.command_index];
  1803. DEV_ASSERT(command_degree > 0);
  1804. command_degree--;
  1805. if (command_degree == 0) {
  1806. command_stack.push_back(command_list_node.command_index);
  1807. }
  1808. adjacency_list_index = command_list_node.next_list_index;
  1809. }
  1810. }
  1811. // Batch buffer, texture, draw lists and compute operations together.
  1812. const uint32_t PriorityTable[RecordedCommand::TYPE_MAX] = {
  1813. 0, // TYPE_NONE
  1814. 1, // TYPE_BUFFER_CLEAR
  1815. 1, // TYPE_BUFFER_COPY
  1816. 1, // TYPE_BUFFER_GET_DATA
  1817. 1, // TYPE_BUFFER_UPDATE
  1818. 4, // TYPE_COMPUTE_LIST
  1819. 3, // TYPE_DRAW_LIST
  1820. 2, // TYPE_TEXTURE_CLEAR
  1821. 2, // TYPE_TEXTURE_COPY
  1822. 2, // TYPE_TEXTURE_GET_DATA
  1823. 2, // TYPE_TEXTURE_RESOLVE
  1824. 2, // TYPE_TEXTURE_UPDATE
  1825. 2, // TYPE_INSERT_BREADCRUMB
  1826. };
  1827. commands_sorted.clear();
  1828. commands_sorted.resize(command_count);
  1829. for (uint32_t i = 0; i < command_count; i++) {
  1830. const int32_t sorted_command_index = sorted_command_indices[i];
  1831. const uint32_t command_data_offset = command_data_offsets[sorted_command_index];
  1832. const RecordedCommand recorded_command = *reinterpret_cast<const RecordedCommand *>(&command_data[command_data_offset]);
  1833. const uint32_t next_command_level = commands_sorted[sorted_command_index].level + 1;
  1834. adjacency_list_index = recorded_command.adjacent_command_list_index;
  1835. while (adjacency_list_index >= 0) {
  1836. const RecordedCommandListNode &command_list_node = command_list_nodes[adjacency_list_index];
  1837. uint32_t &adjacent_command_level = commands_sorted[command_list_node.command_index].level;
  1838. if (adjacent_command_level < next_command_level) {
  1839. adjacent_command_level = next_command_level;
  1840. }
  1841. adjacency_list_index = command_list_node.next_list_index;
  1842. }
  1843. commands_sorted[sorted_command_index].index = sorted_command_index;
  1844. commands_sorted[sorted_command_index].priority = PriorityTable[recorded_command.type];
  1845. }
  1846. } else {
  1847. commands_sorted.clear();
  1848. commands_sorted.resize(command_count);
  1849. for (uint32_t i = 0; i < command_count; i++) {
  1850. commands_sorted[i].index = i;
  1851. }
  1852. }
  1853. _wait_for_secondary_command_buffer_tasks();
  1854. if (command_count > 0) {
  1855. int32_t current_label_index = -1;
  1856. int32_t current_label_level = -1;
  1857. _run_label_command_change(r_command_buffer, -1, -1, true, true, nullptr, 0, current_label_index, current_label_level);
  1858. if (device.workarounds.avoid_compute_after_draw) {
  1859. // Reset the state of the workaround.
  1860. workarounds_state.draw_list_found = false;
  1861. }
  1862. if (p_reorder_commands) {
  1863. #if PRINT_RENDER_GRAPH
  1864. print_line("BEFORE SORT");
  1865. _print_render_commands(commands_sorted.ptr(), command_count);
  1866. #endif
  1867. commands_sorted.sort();
  1868. #if PRINT_RENDER_GRAPH
  1869. print_line("AFTER SORT");
  1870. _print_render_commands(commands_sorted.ptr(), command_count);
  1871. #endif
  1872. #if PRINT_COMMAND_RECORDING
  1873. print_line(vformat("Recording %d commands", command_count));
  1874. #endif
  1875. uint32_t boosted_priority = 0;
  1876. uint32_t current_level = commands_sorted[0].level;
  1877. uint32_t current_level_start = 0;
  1878. for (uint32_t i = 0; i < command_count; i++) {
  1879. if (current_level != commands_sorted[i].level) {
  1880. RecordedCommandSort *level_command_ptr = &commands_sorted[current_level_start];
  1881. uint32_t level_command_count = i - current_level_start;
  1882. _boost_priority_for_render_commands(level_command_ptr, level_command_count, boosted_priority);
  1883. _group_barriers_for_render_commands(r_command_buffer, level_command_ptr, level_command_count, p_full_barriers);
  1884. _run_render_commands(current_level, level_command_ptr, level_command_count, r_command_buffer, r_command_buffer_pool, current_label_index, current_label_level);
  1885. current_level = commands_sorted[i].level;
  1886. current_level_start = i;
  1887. }
  1888. }
  1889. RecordedCommandSort *level_command_ptr = &commands_sorted[current_level_start];
  1890. uint32_t level_command_count = command_count - current_level_start;
  1891. _boost_priority_for_render_commands(level_command_ptr, level_command_count, boosted_priority);
  1892. _group_barriers_for_render_commands(r_command_buffer, level_command_ptr, level_command_count, p_full_barriers);
  1893. _run_render_commands(current_level, level_command_ptr, level_command_count, r_command_buffer, r_command_buffer_pool, current_label_index, current_label_level);
  1894. #if PRINT_RENDER_GRAPH
  1895. print_line("COMMANDS", command_count, "LEVELS", current_level + 1);
  1896. #endif
  1897. } else {
  1898. for (uint32_t i = 0; i < command_count; i++) {
  1899. _group_barriers_for_render_commands(r_command_buffer, &commands_sorted[i], 1, p_full_barriers);
  1900. _run_render_commands(i, &commands_sorted[i], 1, r_command_buffer, r_command_buffer_pool, current_label_index, current_label_level);
  1901. }
  1902. }
  1903. _run_label_command_change(r_command_buffer, -1, -1, false, false, nullptr, 0, current_label_index, current_label_level);
  1904. #if PRINT_COMMAND_RECORDING
  1905. print_line(vformat("Recorded %d commands", command_count));
  1906. #endif
  1907. }
  1908. // 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.
  1909. frame = (frame + 1) % frames.size();
  1910. }
  1911. #if PRINT_RESOURCE_TRACKER_TOTAL
  1912. static uint32_t resource_tracker_total = 0;
  1913. #endif
  1914. RenderingDeviceGraph::ResourceTracker *RenderingDeviceGraph::resource_tracker_create() {
  1915. #if PRINT_RESOURCE_TRACKER_TOTAL
  1916. print_line("Resource trackers:", ++resource_tracker_total);
  1917. #endif
  1918. return memnew(ResourceTracker);
  1919. }
  1920. void RenderingDeviceGraph::resource_tracker_free(ResourceTracker *tracker) {
  1921. if (tracker == nullptr) {
  1922. return;
  1923. }
  1924. if (tracker->in_parent_dirty_list) {
  1925. // Delete the tracker from the parent's dirty linked list.
  1926. if (tracker->parent->dirty_shared_list == tracker) {
  1927. tracker->parent->dirty_shared_list = tracker->next_shared;
  1928. } else {
  1929. ResourceTracker *node = tracker->parent->dirty_shared_list;
  1930. while (node != nullptr) {
  1931. if (node->next_shared == tracker) {
  1932. node->next_shared = tracker->next_shared;
  1933. node = nullptr;
  1934. } else {
  1935. node = node->next_shared;
  1936. }
  1937. }
  1938. }
  1939. }
  1940. memdelete(tracker);
  1941. #if PRINT_RESOURCE_TRACKER_TOTAL
  1942. print_line("Resource trackers:", --resource_tracker_total);
  1943. #endif
  1944. }