rendering_device_graph.cpp 109 KB

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