rendering_device_graph.cpp 114 KB

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