rendering_device_graph.cpp 132 KB

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