rendering_device_graph.cpp 109 KB

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