rendering_device_graph.cpp 121 KB

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