rendering_device_graph.cpp 134 KB

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