rendering_device_graph.cpp 105 KB

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