rendering_device_graph.cpp 134 KB

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