rendering_device_graph.cpp 127 KB

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