rendering_device_graph.cpp 134 KB

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