rendering_device_graph.cpp 135 KB

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