rendering_device_graph.cpp 113 KB

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