rendering_device_graph.cpp 131 KB

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