rendering_device_graph.cpp 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020
  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 != 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 != 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 != 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. const RecordedCommandListNode &command_list_node = command_list_nodes[read_full_command_list_index];
  437. if (command_list_node.command_index == p_command_index) {
  438. if (!resource_has_parent) {
  439. // 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.
  440. ERR_FAIL_MSG("Command can't have itself as a dependency.");
  441. }
  442. } else {
  443. // Add this command to the adjacency list of each command that was reading this resource.
  444. _add_adjacent_command(command_list_node.command_index, p_command_index, r_command);
  445. }
  446. read_full_command_list_index = command_list_node.next_list_index;
  447. }
  448. if (!resource_has_parent) {
  449. // Clear the full list if this resource is not a slice.
  450. search_tracker->read_full_command_list_index = -1;
  451. }
  452. // We add this command to the adjacency list of all commands that were reading from resource slices.
  453. int32_t previous_slice_command_list_index = -1;
  454. int32_t read_slice_command_list_index = search_tracker->read_slice_command_list_index;
  455. while (read_slice_command_list_index >= 0) {
  456. const RecordedSliceListNode &read_list_node = read_slice_list_nodes[read_slice_command_list_index];
  457. if (!resource_has_parent || search_tracker_rect.encloses(read_list_node.subresources)) {
  458. if (previous_slice_command_list_index >= 0) {
  459. // Erase this element and connect the previous one to the next element.
  460. read_slice_list_nodes[previous_slice_command_list_index].next_list_index = read_list_node.next_list_index;
  461. } else {
  462. // Erase this element from the head of the list.
  463. DEV_ASSERT(search_tracker->read_slice_command_list_index == read_slice_command_list_index);
  464. search_tracker->read_slice_command_list_index = read_list_node.next_list_index;
  465. }
  466. // Advance to the next element.
  467. read_slice_command_list_index = read_list_node.next_list_index;
  468. } else {
  469. previous_slice_command_list_index = read_slice_command_list_index;
  470. read_slice_command_list_index = read_list_node.next_list_index;
  471. }
  472. if (!resource_has_parent || search_tracker_rect.intersects(read_list_node.subresources)) {
  473. // Add this command to the adjacency list of each command that was reading this resource.
  474. // We only add the dependency if there's an intersection between slices or this resource isn't a slice.
  475. _add_adjacent_command(read_list_node.command_index, p_command_index, r_command);
  476. }
  477. }
  478. } else if (resource_has_parent) {
  479. // We add a read dependency to the tracker to indicate this command reads from the resource slice.
  480. 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);
  481. } else {
  482. // We add a read dependency to the tracker to indicate this command reads from the entire resource.
  483. search_tracker->read_full_command_list_index = _add_to_command_list(p_command_index, search_tracker->read_full_command_list_index);
  484. }
  485. }
  486. }
  487. 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) {
  488. if (!driver_honors_barriers) {
  489. return;
  490. }
  491. if (r_barrier_index < 0) {
  492. r_barrier_index = r_barrier_vector.size();
  493. }
  494. RDD::TextureBarrier texture_barrier;
  495. texture_barrier.texture = p_texture_id;
  496. texture_barrier.src_access = p_src_access;
  497. texture_barrier.dst_access = p_dst_access;
  498. texture_barrier.prev_layout = _usage_to_image_layout(p_prev_usage);
  499. texture_barrier.next_layout = _usage_to_image_layout(p_next_usage);
  500. texture_barrier.subresources = p_subresources;
  501. r_barrier_vector.push_back(texture_barrier);
  502. r_barrier_count++;
  503. }
  504. #if USE_BUFFER_BARRIERS
  505. 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) {
  506. if (!driver_honors_barriers) {
  507. return;
  508. }
  509. if (r_barrier_index < 0) {
  510. r_barrier_index = command_buffer_barriers.size();
  511. }
  512. RDD::BufferBarrier buffer_barrier;
  513. buffer_barrier.buffer = p_buffer_id;
  514. buffer_barrier.src_access = p_src_access;
  515. buffer_barrier.dst_access = p_dst_access;
  516. buffer_barrier.offset = 0;
  517. buffer_barrier.size = RDD::BUFFER_WHOLE_SIZE;
  518. command_buffer_barriers.push_back(buffer_barrier);
  519. r_barrier_count++;
  520. }
  521. #endif
  522. void RenderingDeviceGraph::_run_compute_list_command(RDD::CommandBufferID p_command_buffer, const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {
  523. uint32_t instruction_data_cursor = 0;
  524. while (instruction_data_cursor < p_instruction_data_size) {
  525. DEV_ASSERT((instruction_data_cursor + sizeof(ComputeListInstruction)) <= p_instruction_data_size);
  526. const ComputeListInstruction *instruction = reinterpret_cast<const ComputeListInstruction *>(&p_instruction_data[instruction_data_cursor]);
  527. switch (instruction->type) {
  528. case ComputeListInstruction::TYPE_BIND_PIPELINE: {
  529. const ComputeListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const ComputeListBindPipelineInstruction *>(instruction);
  530. driver->command_bind_compute_pipeline(p_command_buffer, bind_pipeline_instruction->pipeline);
  531. instruction_data_cursor += sizeof(ComputeListBindPipelineInstruction);
  532. } break;
  533. case ComputeListInstruction::TYPE_BIND_UNIFORM_SET: {
  534. const ComputeListBindUniformSetInstruction *bind_uniform_set_instruction = reinterpret_cast<const ComputeListBindUniformSetInstruction *>(instruction);
  535. 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);
  536. instruction_data_cursor += sizeof(ComputeListBindUniformSetInstruction);
  537. } break;
  538. case ComputeListInstruction::TYPE_DISPATCH: {
  539. const ComputeListDispatchInstruction *dispatch_instruction = reinterpret_cast<const ComputeListDispatchInstruction *>(instruction);
  540. driver->command_compute_dispatch(p_command_buffer, dispatch_instruction->x_groups, dispatch_instruction->y_groups, dispatch_instruction->z_groups);
  541. instruction_data_cursor += sizeof(ComputeListDispatchInstruction);
  542. } break;
  543. case ComputeListInstruction::TYPE_DISPATCH_INDIRECT: {
  544. const ComputeListDispatchIndirectInstruction *dispatch_indirect_instruction = reinterpret_cast<const ComputeListDispatchIndirectInstruction *>(instruction);
  545. driver->command_compute_dispatch_indirect(p_command_buffer, dispatch_indirect_instruction->buffer, dispatch_indirect_instruction->offset);
  546. instruction_data_cursor += sizeof(ComputeListDispatchIndirectInstruction);
  547. } break;
  548. case ComputeListInstruction::TYPE_SET_PUSH_CONSTANT: {
  549. const ComputeListSetPushConstantInstruction *set_push_constant_instruction = reinterpret_cast<const ComputeListSetPushConstantInstruction *>(instruction);
  550. const VectorView push_constant_data_view(reinterpret_cast<const uint32_t *>(set_push_constant_instruction->data()), set_push_constant_instruction->size / sizeof(uint32_t));
  551. driver->command_bind_push_constants(p_command_buffer, set_push_constant_instruction->shader, 0, push_constant_data_view);
  552. instruction_data_cursor += sizeof(ComputeListSetPushConstantInstruction);
  553. instruction_data_cursor += set_push_constant_instruction->size;
  554. } break;
  555. case ComputeListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE: {
  556. const ComputeListUniformSetPrepareForUseInstruction *uniform_set_prepare_for_use_instruction = reinterpret_cast<const ComputeListUniformSetPrepareForUseInstruction *>(instruction);
  557. 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);
  558. instruction_data_cursor += sizeof(ComputeListUniformSetPrepareForUseInstruction);
  559. } break;
  560. default:
  561. DEV_ASSERT(false && "Unknown compute list instruction type.");
  562. return;
  563. }
  564. }
  565. }
  566. void RenderingDeviceGraph::_run_draw_list_command(RDD::CommandBufferID p_command_buffer, const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {
  567. uint32_t instruction_data_cursor = 0;
  568. while (instruction_data_cursor < p_instruction_data_size) {
  569. DEV_ASSERT((instruction_data_cursor + sizeof(DrawListInstruction)) <= p_instruction_data_size);
  570. const DrawListInstruction *instruction = reinterpret_cast<const DrawListInstruction *>(&p_instruction_data[instruction_data_cursor]);
  571. switch (instruction->type) {
  572. case DrawListInstruction::TYPE_BIND_INDEX_BUFFER: {
  573. const DrawListBindIndexBufferInstruction *bind_index_buffer_instruction = reinterpret_cast<const DrawListBindIndexBufferInstruction *>(instruction);
  574. driver->command_render_bind_index_buffer(p_command_buffer, bind_index_buffer_instruction->buffer, bind_index_buffer_instruction->format, bind_index_buffer_instruction->offset);
  575. instruction_data_cursor += sizeof(DrawListBindIndexBufferInstruction);
  576. } break;
  577. case DrawListInstruction::TYPE_BIND_PIPELINE: {
  578. const DrawListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const DrawListBindPipelineInstruction *>(instruction);
  579. driver->command_bind_render_pipeline(p_command_buffer, bind_pipeline_instruction->pipeline);
  580. instruction_data_cursor += sizeof(DrawListBindPipelineInstruction);
  581. } break;
  582. case DrawListInstruction::TYPE_BIND_UNIFORM_SET: {
  583. const DrawListBindUniformSetInstruction *bind_uniform_set_instruction = reinterpret_cast<const DrawListBindUniformSetInstruction *>(instruction);
  584. 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);
  585. instruction_data_cursor += sizeof(DrawListBindUniformSetInstruction);
  586. } break;
  587. case DrawListInstruction::TYPE_BIND_VERTEX_BUFFERS: {
  588. const DrawListBindVertexBuffersInstruction *bind_vertex_buffers_instruction = reinterpret_cast<const DrawListBindVertexBuffersInstruction *>(instruction);
  589. 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());
  590. instruction_data_cursor += sizeof(DrawListBindVertexBuffersInstruction);
  591. instruction_data_cursor += sizeof(RDD::BufferID) * bind_vertex_buffers_instruction->vertex_buffers_count;
  592. instruction_data_cursor += sizeof(uint64_t) * bind_vertex_buffers_instruction->vertex_buffers_count;
  593. } break;
  594. case DrawListInstruction::TYPE_CLEAR_ATTACHMENTS: {
  595. const DrawListClearAttachmentsInstruction *clear_attachments_instruction = reinterpret_cast<const DrawListClearAttachmentsInstruction *>(instruction);
  596. const VectorView attachments_clear_view(clear_attachments_instruction->attachments_clear(), clear_attachments_instruction->attachments_clear_count);
  597. const VectorView attachments_clear_rect_view(clear_attachments_instruction->attachments_clear_rect(), clear_attachments_instruction->attachments_clear_rect_count);
  598. driver->command_render_clear_attachments(p_command_buffer, attachments_clear_view, attachments_clear_rect_view);
  599. instruction_data_cursor += sizeof(DrawListClearAttachmentsInstruction);
  600. instruction_data_cursor += sizeof(RDD::AttachmentClear) * clear_attachments_instruction->attachments_clear_count;
  601. instruction_data_cursor += sizeof(Rect2i) * clear_attachments_instruction->attachments_clear_rect_count;
  602. } break;
  603. case DrawListInstruction::TYPE_DRAW: {
  604. const DrawListDrawInstruction *draw_instruction = reinterpret_cast<const DrawListDrawInstruction *>(instruction);
  605. driver->command_render_draw(p_command_buffer, draw_instruction->vertex_count, draw_instruction->instance_count, 0, 0);
  606. instruction_data_cursor += sizeof(DrawListDrawInstruction);
  607. } break;
  608. case DrawListInstruction::TYPE_DRAW_INDEXED: {
  609. const DrawListDrawIndexedInstruction *draw_indexed_instruction = reinterpret_cast<const DrawListDrawIndexedInstruction *>(instruction);
  610. 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);
  611. instruction_data_cursor += sizeof(DrawListDrawIndexedInstruction);
  612. } break;
  613. case DrawListInstruction::TYPE_EXECUTE_COMMANDS: {
  614. const DrawListExecuteCommandsInstruction *execute_commands_instruction = reinterpret_cast<const DrawListExecuteCommandsInstruction *>(instruction);
  615. driver->command_buffer_execute_secondary(p_command_buffer, execute_commands_instruction->command_buffer);
  616. instruction_data_cursor += sizeof(DrawListExecuteCommandsInstruction);
  617. } break;
  618. case DrawListInstruction::TYPE_NEXT_SUBPASS: {
  619. const DrawListNextSubpassInstruction *next_subpass_instruction = reinterpret_cast<const DrawListNextSubpassInstruction *>(instruction);
  620. driver->command_next_render_subpass(p_command_buffer, next_subpass_instruction->command_buffer_type);
  621. instruction_data_cursor += sizeof(DrawListNextSubpassInstruction);
  622. } break;
  623. case DrawListInstruction::TYPE_SET_BLEND_CONSTANTS: {
  624. const DrawListSetBlendConstantsInstruction *set_blend_constants_instruction = reinterpret_cast<const DrawListSetBlendConstantsInstruction *>(instruction);
  625. driver->command_render_set_blend_constants(p_command_buffer, set_blend_constants_instruction->color);
  626. instruction_data_cursor += sizeof(DrawListSetBlendConstantsInstruction);
  627. } break;
  628. case DrawListInstruction::TYPE_SET_LINE_WIDTH: {
  629. const DrawListSetLineWidthInstruction *set_line_width_instruction = reinterpret_cast<const DrawListSetLineWidthInstruction *>(instruction);
  630. driver->command_render_set_line_width(p_command_buffer, set_line_width_instruction->width);
  631. instruction_data_cursor += sizeof(DrawListSetLineWidthInstruction);
  632. } break;
  633. case DrawListInstruction::TYPE_SET_PUSH_CONSTANT: {
  634. const DrawListSetPushConstantInstruction *set_push_constant_instruction = reinterpret_cast<const DrawListSetPushConstantInstruction *>(instruction);
  635. const VectorView push_constant_data_view(reinterpret_cast<const uint32_t *>(set_push_constant_instruction->data()), set_push_constant_instruction->size / sizeof(uint32_t));
  636. driver->command_bind_push_constants(p_command_buffer, set_push_constant_instruction->shader, 0, push_constant_data_view);
  637. instruction_data_cursor += sizeof(DrawListSetPushConstantInstruction);
  638. instruction_data_cursor += set_push_constant_instruction->size;
  639. } break;
  640. case DrawListInstruction::TYPE_SET_SCISSOR: {
  641. const DrawListSetScissorInstruction *set_scissor_instruction = reinterpret_cast<const DrawListSetScissorInstruction *>(instruction);
  642. driver->command_render_set_scissor(p_command_buffer, set_scissor_instruction->rect);
  643. instruction_data_cursor += sizeof(DrawListSetScissorInstruction);
  644. } break;
  645. case DrawListInstruction::TYPE_SET_VIEWPORT: {
  646. const DrawListSetViewportInstruction *set_viewport_instruction = reinterpret_cast<const DrawListSetViewportInstruction *>(instruction);
  647. driver->command_render_set_viewport(p_command_buffer, set_viewport_instruction->rect);
  648. instruction_data_cursor += sizeof(DrawListSetViewportInstruction);
  649. } break;
  650. case DrawListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE: {
  651. const DrawListUniformSetPrepareForUseInstruction *uniform_set_prepare_for_use_instruction = reinterpret_cast<const DrawListUniformSetPrepareForUseInstruction *>(instruction);
  652. 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);
  653. instruction_data_cursor += sizeof(DrawListUniformSetPrepareForUseInstruction);
  654. } break;
  655. default:
  656. DEV_ASSERT(false && "Unknown draw list instruction type.");
  657. return;
  658. }
  659. }
  660. }
  661. void RenderingDeviceGraph::_run_secondary_command_buffer_task(const SecondaryCommandBuffer *p_secondary) {
  662. driver->command_buffer_begin_secondary(p_secondary->command_buffer, p_secondary->render_pass, 0, p_secondary->framebuffer);
  663. _run_draw_list_command(p_secondary->command_buffer, p_secondary->instruction_data.ptr(), p_secondary->instruction_data.size());
  664. driver->command_buffer_end(p_secondary->command_buffer);
  665. }
  666. void RenderingDeviceGraph::_wait_for_secondary_command_buffer_tasks() {
  667. for (uint32_t i = 0; i < frames[frame].secondary_command_buffers_used; i++) {
  668. WorkerThreadPool::TaskID &task = frames[frame].secondary_command_buffers[i].task;
  669. if (task != WorkerThreadPool::INVALID_TASK_ID) {
  670. WorkerThreadPool::get_singleton()->wait_for_task_completion(task);
  671. task = WorkerThreadPool::INVALID_TASK_ID;
  672. }
  673. }
  674. }
  675. 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) {
  676. for (uint32_t i = 0; i < p_sorted_commands_count; i++) {
  677. const uint32_t command_index = p_sorted_commands[i].index;
  678. const uint32_t command_data_offset = command_data_offsets[command_index];
  679. const RecordedCommand *command = reinterpret_cast<RecordedCommand *>(&command_data[command_data_offset]);
  680. _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);
  681. switch (command->type) {
  682. case RecordedCommand::TYPE_BUFFER_CLEAR: {
  683. const RecordedBufferClearCommand *buffer_clear_command = reinterpret_cast<const RecordedBufferClearCommand *>(command);
  684. driver->command_clear_buffer(p_command_buffer, buffer_clear_command->buffer, buffer_clear_command->offset, buffer_clear_command->size);
  685. } break;
  686. case RecordedCommand::TYPE_BUFFER_COPY: {
  687. const RecordedBufferCopyCommand *buffer_copy_command = reinterpret_cast<const RecordedBufferCopyCommand *>(command);
  688. driver->command_copy_buffer(p_command_buffer, buffer_copy_command->source, buffer_copy_command->destination, buffer_copy_command->region);
  689. } break;
  690. case RecordedCommand::TYPE_BUFFER_GET_DATA: {
  691. const RecordedBufferGetDataCommand *buffer_get_data_command = reinterpret_cast<const RecordedBufferGetDataCommand *>(command);
  692. driver->command_copy_buffer(p_command_buffer, buffer_get_data_command->source, buffer_get_data_command->destination, buffer_get_data_command->region);
  693. } break;
  694. case RecordedCommand::TYPE_BUFFER_UPDATE: {
  695. const RecordedBufferUpdateCommand *buffer_update_command = reinterpret_cast<const RecordedBufferUpdateCommand *>(command);
  696. const RecordedBufferCopy *command_buffer_copies = buffer_update_command->buffer_copies();
  697. for (uint32_t j = 0; j < buffer_update_command->buffer_copies_count; j++) {
  698. driver->command_copy_buffer(p_command_buffer, command_buffer_copies[j].source, buffer_update_command->destination, command_buffer_copies[j].region);
  699. }
  700. } break;
  701. case RecordedCommand::TYPE_COMPUTE_LIST: {
  702. const RecordedComputeListCommand *compute_list_command = reinterpret_cast<const RecordedComputeListCommand *>(command);
  703. _run_compute_list_command(p_command_buffer, compute_list_command->instruction_data(), compute_list_command->instruction_data_size);
  704. } break;
  705. case RecordedCommand::TYPE_DRAW_LIST: {
  706. const RecordedDrawListCommand *draw_list_command = reinterpret_cast<const RecordedDrawListCommand *>(command);
  707. const VectorView clear_values(draw_list_command->clear_values(), draw_list_command->clear_values_count);
  708. 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);
  709. _run_draw_list_command(p_command_buffer, draw_list_command->instruction_data(), draw_list_command->instruction_data_size);
  710. driver->command_end_render_pass(p_command_buffer);
  711. } break;
  712. case RecordedCommand::TYPE_TEXTURE_CLEAR: {
  713. const RecordedTextureClearCommand *texture_clear_command = reinterpret_cast<const RecordedTextureClearCommand *>(command);
  714. 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);
  715. } break;
  716. case RecordedCommand::TYPE_TEXTURE_COPY: {
  717. const RecordedTextureCopyCommand *texture_copy_command = reinterpret_cast<const RecordedTextureCopyCommand *>(command);
  718. 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);
  719. } break;
  720. case RecordedCommand::TYPE_TEXTURE_GET_DATA: {
  721. const RecordedTextureGetDataCommand *texture_get_data_command = reinterpret_cast<const RecordedTextureGetDataCommand *>(command);
  722. 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);
  723. 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);
  724. } break;
  725. case RecordedCommand::TYPE_TEXTURE_RESOLVE: {
  726. const RecordedTextureResolveCommand *texture_resolve_command = reinterpret_cast<const RecordedTextureResolveCommand *>(command);
  727. 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);
  728. } break;
  729. case RecordedCommand::TYPE_TEXTURE_UPDATE: {
  730. const RecordedTextureUpdateCommand *texture_update_command = reinterpret_cast<const RecordedTextureUpdateCommand *>(command);
  731. const RecordedBufferToTextureCopy *command_buffer_to_texture_copies = texture_update_command->buffer_to_texture_copies();
  732. for (uint32_t j = 0; j < texture_update_command->buffer_to_texture_copies_count; j++) {
  733. 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);
  734. }
  735. } break;
  736. case RecordedCommand::TYPE_CAPTURE_TIMESTAMP: {
  737. const RecordedCaptureTimestampCommand *texture_capture_timestamp_command = reinterpret_cast<const RecordedCaptureTimestampCommand *>(command);
  738. driver->command_timestamp_write(p_command_buffer, texture_capture_timestamp_command->pool, texture_capture_timestamp_command->index);
  739. } break;
  740. default: {
  741. DEV_ASSERT(false && "Unknown recorded command type.");
  742. return;
  743. }
  744. }
  745. }
  746. }
  747. 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) {
  748. if (command_label_count == 0) {
  749. // Ignore any label operations if no labels were pushed.
  750. return;
  751. }
  752. if (p_ignore_previous_value || p_new_label_index != r_current_label_index || p_new_level != r_current_label_level) {
  753. if (!p_ignore_previous_value && (p_use_label_for_empty || r_current_label_index >= 0)) {
  754. // End the current label.
  755. driver->command_end_label(p_command_buffer);
  756. }
  757. String label_name;
  758. Color label_color;
  759. if (p_new_label_index >= 0) {
  760. const char *label_chars = &command_label_chars[command_label_offsets[p_new_label_index]];
  761. label_name.parse_utf8(label_chars);
  762. label_color = command_label_colors[p_new_label_index];
  763. } else if (p_use_label_for_empty) {
  764. label_name = "Command graph";
  765. label_color = Color(1, 1, 1, 1);
  766. }
  767. // Add the level to the name.
  768. label_name += " (L" + itos(p_new_level) + ")";
  769. if (p_sorted_commands != nullptr && p_sorted_commands_count > 0) {
  770. // Analyze the commands in the level that have the same label to detect what type of operations are performed.
  771. bool copy_commands = false;
  772. bool compute_commands = false;
  773. bool draw_commands = false;
  774. for (uint32_t i = 0; i < p_sorted_commands_count; i++) {
  775. const uint32_t command_index = p_sorted_commands[i].index;
  776. const uint32_t command_data_offset = command_data_offsets[command_index];
  777. const RecordedCommand *command = reinterpret_cast<RecordedCommand *>(&command_data[command_data_offset]);
  778. if (command->label_index != p_new_label_index) {
  779. break;
  780. }
  781. switch (command->type) {
  782. case RecordedCommand::TYPE_BUFFER_CLEAR:
  783. case RecordedCommand::TYPE_BUFFER_COPY:
  784. case RecordedCommand::TYPE_BUFFER_GET_DATA:
  785. case RecordedCommand::TYPE_BUFFER_UPDATE:
  786. case RecordedCommand::TYPE_TEXTURE_CLEAR:
  787. case RecordedCommand::TYPE_TEXTURE_COPY:
  788. case RecordedCommand::TYPE_TEXTURE_GET_DATA:
  789. case RecordedCommand::TYPE_TEXTURE_RESOLVE:
  790. case RecordedCommand::TYPE_TEXTURE_UPDATE: {
  791. copy_commands = true;
  792. } break;
  793. case RecordedCommand::TYPE_COMPUTE_LIST: {
  794. compute_commands = true;
  795. } break;
  796. case RecordedCommand::TYPE_DRAW_LIST: {
  797. draw_commands = true;
  798. } break;
  799. default: {
  800. // Ignore command.
  801. } break;
  802. }
  803. if (copy_commands && compute_commands && draw_commands) {
  804. // There's no more command types to find.
  805. break;
  806. }
  807. }
  808. if (copy_commands || compute_commands || draw_commands) {
  809. // Add the operations to the name.
  810. bool plus_after_copy = copy_commands && (compute_commands || draw_commands);
  811. bool plus_after_compute = compute_commands && draw_commands;
  812. label_name += " (";
  813. label_name += copy_commands ? "Copy" : "";
  814. label_name += plus_after_copy ? "+" : "";
  815. label_name += compute_commands ? "Compute" : "";
  816. label_name += plus_after_compute ? "+" : "";
  817. label_name += draw_commands ? "Draw" : "";
  818. label_name += ")";
  819. }
  820. }
  821. // Start the new label.
  822. CharString label_name_utf8 = label_name.utf8();
  823. driver->command_begin_label(p_command_buffer, label_name_utf8.get_data(), label_color);
  824. r_current_label_index = p_new_label_index;
  825. r_current_label_level = p_new_level;
  826. }
  827. }
  828. void RenderingDeviceGraph::_boost_priority_for_render_commands(RecordedCommandSort *p_sorted_commands, uint32_t p_sorted_commands_count, uint32_t &r_boosted_priority) {
  829. if (p_sorted_commands_count == 0) {
  830. return;
  831. }
  832. const uint32_t boosted_priority_value = 0;
  833. if (r_boosted_priority > 0) {
  834. bool perform_sort = false;
  835. for (uint32_t j = 0; j < p_sorted_commands_count; j++) {
  836. if (p_sorted_commands[j].priority == r_boosted_priority) {
  837. p_sorted_commands[j].priority = boosted_priority_value;
  838. perform_sort = true;
  839. }
  840. }
  841. if (perform_sort) {
  842. SortArray<RecordedCommandSort> command_sorter;
  843. command_sorter.sort(p_sorted_commands, p_sorted_commands_count);
  844. }
  845. }
  846. if (p_sorted_commands[p_sorted_commands_count - 1].priority != boosted_priority_value) {
  847. r_boosted_priority = p_sorted_commands[p_sorted_commands_count - 1].priority;
  848. }
  849. }
  850. 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) {
  851. if (!driver_honors_barriers) {
  852. return;
  853. }
  854. barrier_group.clear();
  855. barrier_group.src_stages = RDD::PIPELINE_STAGE_TOP_OF_PIPE_BIT;
  856. barrier_group.dst_stages = RDD::PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
  857. for (uint32_t i = 0; i < p_sorted_commands_count; i++) {
  858. const uint32_t command_index = p_sorted_commands[i].index;
  859. const uint32_t command_data_offset = command_data_offsets[command_index];
  860. const RecordedCommand *command = reinterpret_cast<RecordedCommand *>(&command_data[command_data_offset]);
  861. #if PRINT_COMMAND_RECORDING
  862. print_line(vformat("Grouping barriers for #%d", command_index));
  863. #endif
  864. // Merge command's stage bits with the barrier group.
  865. barrier_group.src_stages = barrier_group.src_stages | command->previous_stages;
  866. barrier_group.dst_stages = barrier_group.dst_stages | command->next_stages;
  867. // Merge command's memory barrier bits with the barrier group.
  868. barrier_group.memory_barrier.src_access = barrier_group.memory_barrier.src_access | command->memory_barrier.src_access;
  869. barrier_group.memory_barrier.dst_access = barrier_group.memory_barrier.dst_access | command->memory_barrier.dst_access;
  870. // Gather texture barriers.
  871. for (int32_t j = 0; j < command->normalization_barrier_count; j++) {
  872. const RDD::TextureBarrier &recorded_barrier = command_normalization_barriers[command->normalization_barrier_index + j];
  873. barrier_group.normalization_barriers.push_back(recorded_barrier);
  874. #if PRINT_COMMAND_RECORDING
  875. print_line(vformat("Normalization Barrier #%d", barrier_group.normalization_barriers.size() - 1));
  876. #endif
  877. }
  878. for (int32_t j = 0; j < command->transition_barrier_count; j++) {
  879. const RDD::TextureBarrier &recorded_barrier = command_transition_barriers[command->transition_barrier_index + j];
  880. barrier_group.transition_barriers.push_back(recorded_barrier);
  881. #if PRINT_COMMAND_RECORDING
  882. print_line(vformat("Transition Barrier #%d", barrier_group.transition_barriers.size() - 1));
  883. #endif
  884. }
  885. #if USE_BUFFER_BARRIERS
  886. // Gather buffer barriers.
  887. for (int32_t j = 0; j < command->buffer_barrier_count; j++) {
  888. const RDD::BufferBarrier &recorded_barrier = command_buffer_barriers[command->buffer_barrier_index + j];
  889. barrier_group.buffer_barriers.push_back(recorded_barrier);
  890. }
  891. #endif
  892. }
  893. if (p_full_memory_barrier) {
  894. barrier_group.src_stages = RDD::PIPELINE_STAGE_ALL_COMMANDS_BIT;
  895. barrier_group.dst_stages = RDD::PIPELINE_STAGE_ALL_COMMANDS_BIT;
  896. barrier_group.memory_barrier.src_access = RDD::BARRIER_ACCESS_MEMORY_READ_BIT | RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT;
  897. barrier_group.memory_barrier.dst_access = RDD::BARRIER_ACCESS_MEMORY_READ_BIT | RDD::BARRIER_ACCESS_MEMORY_WRITE_BIT;
  898. }
  899. const bool is_memory_barrier_empty = barrier_group.memory_barrier.src_access.is_empty() && barrier_group.memory_barrier.dst_access.is_empty();
  900. const bool are_texture_barriers_empty = barrier_group.normalization_barriers.is_empty() && barrier_group.transition_barriers.is_empty();
  901. #if USE_BUFFER_BARRIERS
  902. const bool are_buffer_barriers_empty = barrier_group.buffer_barriers.is_empty();
  903. #else
  904. const bool are_buffer_barriers_empty = true;
  905. #endif
  906. if (is_memory_barrier_empty && are_texture_barriers_empty && are_buffer_barriers_empty) {
  907. // Commands don't require synchronization.
  908. return;
  909. }
  910. const VectorView<RDD::MemoryBarrier> memory_barriers = !is_memory_barrier_empty ? barrier_group.memory_barrier : VectorView<RDD::MemoryBarrier>();
  911. const VectorView<RDD::TextureBarrier> texture_barriers = barrier_group.normalization_barriers.is_empty() ? barrier_group.transition_barriers : barrier_group.normalization_barriers;
  912. #if USE_BUFFER_BARRIERS
  913. const VectorView<RDD::BufferBarrier> buffer_barriers = !are_buffer_barriers_empty ? barrier_group.buffer_barriers : VectorView<RDD::BufferBarrier>();
  914. #else
  915. const VectorView<RDD::BufferBarrier> buffer_barriers = VectorView<RDD::BufferBarrier>();
  916. #endif
  917. driver->command_pipeline_barrier(p_command_buffer, barrier_group.src_stages, barrier_group.dst_stages, memory_barriers, buffer_barriers, texture_barriers);
  918. bool separate_texture_barriers = !barrier_group.normalization_barriers.is_empty() && !barrier_group.transition_barriers.is_empty();
  919. if (separate_texture_barriers) {
  920. 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);
  921. }
  922. }
  923. void RenderingDeviceGraph::_print_render_commands(const RecordedCommandSort *p_sorted_commands, uint32_t p_sorted_commands_count) {
  924. for (uint32_t i = 0; i < p_sorted_commands_count; i++) {
  925. const uint32_t command_index = p_sorted_commands[i].index;
  926. const uint32_t command_level = p_sorted_commands[i].level;
  927. const uint32_t command_data_offset = command_data_offsets[command_index];
  928. const RecordedCommand *command = reinterpret_cast<RecordedCommand *>(&command_data[command_data_offset]);
  929. switch (command->type) {
  930. case RecordedCommand::TYPE_BUFFER_CLEAR: {
  931. const RecordedBufferClearCommand *buffer_clear_command = reinterpret_cast<const RecordedBufferClearCommand *>(command);
  932. print_line(command_index, "LEVEL", command_level, "BUFFER CLEAR DESTINATION", itos(buffer_clear_command->buffer.id));
  933. } break;
  934. case RecordedCommand::TYPE_BUFFER_COPY: {
  935. const RecordedBufferCopyCommand *buffer_copy_command = reinterpret_cast<const RecordedBufferCopyCommand *>(command);
  936. print_line(command_index, "LEVEL", command_level, "BUFFER COPY SOURCE", itos(buffer_copy_command->source.id), "DESTINATION", itos(buffer_copy_command->destination.id));
  937. } break;
  938. case RecordedCommand::TYPE_BUFFER_GET_DATA: {
  939. const RecordedBufferGetDataCommand *buffer_get_data_command = reinterpret_cast<const RecordedBufferGetDataCommand *>(command);
  940. print_line(command_index, "LEVEL", command_level, "BUFFER GET DATA DESTINATION", itos(buffer_get_data_command->destination.id));
  941. } break;
  942. case RecordedCommand::TYPE_BUFFER_UPDATE: {
  943. const RecordedBufferUpdateCommand *buffer_update_command = reinterpret_cast<const RecordedBufferUpdateCommand *>(command);
  944. print_line(command_index, "LEVEL", command_level, "BUFFER UPDATE DESTINATION", itos(buffer_update_command->destination.id), "COPIES", buffer_update_command->buffer_copies_count);
  945. } break;
  946. case RecordedCommand::TYPE_COMPUTE_LIST: {
  947. const RecordedComputeListCommand *compute_list_command = reinterpret_cast<const RecordedComputeListCommand *>(command);
  948. print_line(command_index, "LEVEL", command_level, "COMPUTE LIST SIZE", compute_list_command->instruction_data_size);
  949. } break;
  950. case RecordedCommand::TYPE_DRAW_LIST: {
  951. const RecordedDrawListCommand *draw_list_command = reinterpret_cast<const RecordedDrawListCommand *>(command);
  952. print_line(command_index, "LEVEL", command_level, "DRAW LIST SIZE", draw_list_command->instruction_data_size);
  953. } break;
  954. case RecordedCommand::TYPE_TEXTURE_CLEAR: {
  955. const RecordedTextureClearCommand *texture_clear_command = reinterpret_cast<const RecordedTextureClearCommand *>(command);
  956. print_line(command_index, "LEVEL", command_level, "TEXTURE CLEAR", itos(texture_clear_command->texture.id), "COLOR", texture_clear_command->color);
  957. } break;
  958. case RecordedCommand::TYPE_TEXTURE_COPY: {
  959. const RecordedTextureCopyCommand *texture_copy_command = reinterpret_cast<const RecordedTextureCopyCommand *>(command);
  960. 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));
  961. } break;
  962. case RecordedCommand::TYPE_TEXTURE_GET_DATA: {
  963. print_line(command_index, "LEVEL", command_level, "TEXTURE GET DATA");
  964. } break;
  965. case RecordedCommand::TYPE_TEXTURE_RESOLVE: {
  966. const RecordedTextureResolveCommand *texture_resolve_command = reinterpret_cast<const RecordedTextureResolveCommand *>(command);
  967. 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));
  968. } break;
  969. case RecordedCommand::TYPE_TEXTURE_UPDATE: {
  970. const RecordedTextureUpdateCommand *texture_update_command = reinterpret_cast<const RecordedTextureUpdateCommand *>(command);
  971. print_line(command_index, "LEVEL", command_level, "TEXTURE UPDATE TO", itos(texture_update_command->to_texture.id));
  972. } break;
  973. case RecordedCommand::TYPE_CAPTURE_TIMESTAMP: {
  974. const RecordedCaptureTimestampCommand *texture_capture_timestamp_command = reinterpret_cast<const RecordedCaptureTimestampCommand *>(command);
  975. print_line(command_index, "LEVEL", command_level, "CAPTURE TIMESTAMP POOL", itos(texture_capture_timestamp_command->pool.id), "INDEX", texture_capture_timestamp_command->index);
  976. } break;
  977. default:
  978. DEV_ASSERT(false && "Unknown recorded command type.");
  979. return;
  980. }
  981. }
  982. }
  983. void RenderingDeviceGraph::_print_draw_list(const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {
  984. uint32_t instruction_data_cursor = 0;
  985. while (instruction_data_cursor < p_instruction_data_size) {
  986. DEV_ASSERT((instruction_data_cursor + sizeof(DrawListInstruction)) <= p_instruction_data_size);
  987. const DrawListInstruction *instruction = reinterpret_cast<const DrawListInstruction *>(&p_instruction_data[instruction_data_cursor]);
  988. switch (instruction->type) {
  989. case DrawListInstruction::TYPE_BIND_INDEX_BUFFER: {
  990. const DrawListBindIndexBufferInstruction *bind_index_buffer_instruction = reinterpret_cast<const DrawListBindIndexBufferInstruction *>(instruction);
  991. 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);
  992. instruction_data_cursor += sizeof(DrawListBindIndexBufferInstruction);
  993. } break;
  994. case DrawListInstruction::TYPE_BIND_PIPELINE: {
  995. const DrawListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const DrawListBindPipelineInstruction *>(instruction);
  996. print_line("\tBIND PIPELINE ID", itos(bind_pipeline_instruction->pipeline.id));
  997. instruction_data_cursor += sizeof(DrawListBindPipelineInstruction);
  998. } break;
  999. case DrawListInstruction::TYPE_BIND_UNIFORM_SET: {
  1000. const DrawListBindUniformSetInstruction *bind_uniform_set_instruction = reinterpret_cast<const DrawListBindUniformSetInstruction *>(instruction);
  1001. print_line("\tBIND UNIFORM SET ID", itos(bind_uniform_set_instruction->uniform_set.id), "SET INDEX", bind_uniform_set_instruction->set_index);
  1002. instruction_data_cursor += sizeof(DrawListBindUniformSetInstruction);
  1003. } break;
  1004. case DrawListInstruction::TYPE_BIND_VERTEX_BUFFERS: {
  1005. const DrawListBindVertexBuffersInstruction *bind_vertex_buffers_instruction = reinterpret_cast<const DrawListBindVertexBuffersInstruction *>(instruction);
  1006. print_line("\tBIND VERTEX BUFFERS COUNT", bind_vertex_buffers_instruction->vertex_buffers_count);
  1007. instruction_data_cursor += sizeof(DrawListBindVertexBuffersInstruction);
  1008. instruction_data_cursor += sizeof(RDD::BufferID) * bind_vertex_buffers_instruction->vertex_buffers_count;
  1009. instruction_data_cursor += sizeof(uint64_t) * bind_vertex_buffers_instruction->vertex_buffers_count;
  1010. } break;
  1011. case DrawListInstruction::TYPE_CLEAR_ATTACHMENTS: {
  1012. const DrawListClearAttachmentsInstruction *clear_attachments_instruction = reinterpret_cast<const DrawListClearAttachmentsInstruction *>(instruction);
  1013. print_line("\tATTACHMENTS CLEAR COUNT", clear_attachments_instruction->attachments_clear_count, "RECT COUNT", clear_attachments_instruction->attachments_clear_rect_count);
  1014. instruction_data_cursor += sizeof(DrawListClearAttachmentsInstruction);
  1015. instruction_data_cursor += sizeof(RDD::AttachmentClear) * clear_attachments_instruction->attachments_clear_count;
  1016. instruction_data_cursor += sizeof(Rect2i) * clear_attachments_instruction->attachments_clear_rect_count;
  1017. } break;
  1018. case DrawListInstruction::TYPE_DRAW: {
  1019. const DrawListDrawInstruction *draw_instruction = reinterpret_cast<const DrawListDrawInstruction *>(instruction);
  1020. print_line("\tDRAW VERTICES", draw_instruction->vertex_count, "INSTANCES", draw_instruction->instance_count);
  1021. instruction_data_cursor += sizeof(DrawListDrawInstruction);
  1022. } break;
  1023. case DrawListInstruction::TYPE_DRAW_INDEXED: {
  1024. const DrawListDrawIndexedInstruction *draw_indexed_instruction = reinterpret_cast<const DrawListDrawIndexedInstruction *>(instruction);
  1025. print_line("\tDRAW INDICES", draw_indexed_instruction->index_count, "INSTANCES", draw_indexed_instruction->instance_count, "FIRST INDEX", draw_indexed_instruction->first_index);
  1026. instruction_data_cursor += sizeof(DrawListDrawIndexedInstruction);
  1027. } break;
  1028. case DrawListInstruction::TYPE_EXECUTE_COMMANDS: {
  1029. print_line("\tEXECUTE COMMANDS");
  1030. instruction_data_cursor += sizeof(DrawListExecuteCommandsInstruction);
  1031. } break;
  1032. case DrawListInstruction::TYPE_NEXT_SUBPASS: {
  1033. print_line("\tNEXT SUBPASS");
  1034. instruction_data_cursor += sizeof(DrawListNextSubpassInstruction);
  1035. } break;
  1036. case DrawListInstruction::TYPE_SET_BLEND_CONSTANTS: {
  1037. const DrawListSetBlendConstantsInstruction *set_blend_constants_instruction = reinterpret_cast<const DrawListSetBlendConstantsInstruction *>(instruction);
  1038. print_line("\tSET BLEND CONSTANTS COLOR", set_blend_constants_instruction->color);
  1039. instruction_data_cursor += sizeof(DrawListSetBlendConstantsInstruction);
  1040. } break;
  1041. case DrawListInstruction::TYPE_SET_LINE_WIDTH: {
  1042. const DrawListSetLineWidthInstruction *set_line_width_instruction = reinterpret_cast<const DrawListSetLineWidthInstruction *>(instruction);
  1043. print_line("\tSET LINE WIDTH", set_line_width_instruction->width);
  1044. instruction_data_cursor += sizeof(DrawListSetLineWidthInstruction);
  1045. } break;
  1046. case DrawListInstruction::TYPE_SET_PUSH_CONSTANT: {
  1047. const DrawListSetPushConstantInstruction *set_push_constant_instruction = reinterpret_cast<const DrawListSetPushConstantInstruction *>(instruction);
  1048. print_line("\tSET PUSH CONSTANT SIZE", set_push_constant_instruction->size);
  1049. instruction_data_cursor += sizeof(DrawListSetPushConstantInstruction);
  1050. instruction_data_cursor += set_push_constant_instruction->size;
  1051. } break;
  1052. case DrawListInstruction::TYPE_SET_SCISSOR: {
  1053. const DrawListSetScissorInstruction *set_scissor_instruction = reinterpret_cast<const DrawListSetScissorInstruction *>(instruction);
  1054. print_line("\tSET SCISSOR", set_scissor_instruction->rect);
  1055. instruction_data_cursor += sizeof(DrawListSetScissorInstruction);
  1056. } break;
  1057. case DrawListInstruction::TYPE_SET_VIEWPORT: {
  1058. const DrawListSetViewportInstruction *set_viewport_instruction = reinterpret_cast<const DrawListSetViewportInstruction *>(instruction);
  1059. print_line("\tSET VIEWPORT", set_viewport_instruction->rect);
  1060. instruction_data_cursor += sizeof(DrawListSetViewportInstruction);
  1061. } break;
  1062. case DrawListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE: {
  1063. const DrawListUniformSetPrepareForUseInstruction *uniform_set_prepare_for_use_instruction = reinterpret_cast<const DrawListUniformSetPrepareForUseInstruction *>(instruction);
  1064. 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);
  1065. instruction_data_cursor += sizeof(DrawListUniformSetPrepareForUseInstruction);
  1066. } break;
  1067. default:
  1068. DEV_ASSERT(false && "Unknown draw list instruction type.");
  1069. return;
  1070. }
  1071. }
  1072. }
  1073. void RenderingDeviceGraph::_print_compute_list(const uint8_t *p_instruction_data, uint32_t p_instruction_data_size) {
  1074. uint32_t instruction_data_cursor = 0;
  1075. while (instruction_data_cursor < p_instruction_data_size) {
  1076. DEV_ASSERT((instruction_data_cursor + sizeof(ComputeListInstruction)) <= p_instruction_data_size);
  1077. const ComputeListInstruction *instruction = reinterpret_cast<const ComputeListInstruction *>(&p_instruction_data[instruction_data_cursor]);
  1078. switch (instruction->type) {
  1079. case ComputeListInstruction::TYPE_BIND_PIPELINE: {
  1080. const ComputeListBindPipelineInstruction *bind_pipeline_instruction = reinterpret_cast<const ComputeListBindPipelineInstruction *>(instruction);
  1081. print_line("\tBIND PIPELINE ID", itos(bind_pipeline_instruction->pipeline.id));
  1082. instruction_data_cursor += sizeof(ComputeListBindPipelineInstruction);
  1083. } break;
  1084. case ComputeListInstruction::TYPE_BIND_UNIFORM_SET: {
  1085. const ComputeListBindUniformSetInstruction *bind_uniform_set_instruction = reinterpret_cast<const ComputeListBindUniformSetInstruction *>(instruction);
  1086. print_line("\tBIND UNIFORM SET ID", itos(bind_uniform_set_instruction->uniform_set.id), "SHADER ID", itos(bind_uniform_set_instruction->shader.id));
  1087. instruction_data_cursor += sizeof(ComputeListBindUniformSetInstruction);
  1088. } break;
  1089. case ComputeListInstruction::TYPE_DISPATCH: {
  1090. const ComputeListDispatchInstruction *dispatch_instruction = reinterpret_cast<const ComputeListDispatchInstruction *>(instruction);
  1091. print_line("\tDISPATCH", dispatch_instruction->x_groups, dispatch_instruction->y_groups, dispatch_instruction->z_groups);
  1092. instruction_data_cursor += sizeof(ComputeListDispatchInstruction);
  1093. } break;
  1094. case ComputeListInstruction::TYPE_DISPATCH_INDIRECT: {
  1095. const ComputeListDispatchIndirectInstruction *dispatch_indirect_instruction = reinterpret_cast<const ComputeListDispatchIndirectInstruction *>(instruction);
  1096. print_line("\tDISPATCH INDIRECT BUFFER ID", itos(dispatch_indirect_instruction->buffer.id), "OFFSET", dispatch_indirect_instruction->offset);
  1097. instruction_data_cursor += sizeof(ComputeListDispatchIndirectInstruction);
  1098. } break;
  1099. case ComputeListInstruction::TYPE_SET_PUSH_CONSTANT: {
  1100. const ComputeListSetPushConstantInstruction *set_push_constant_instruction = reinterpret_cast<const ComputeListSetPushConstantInstruction *>(instruction);
  1101. print_line("\tSET PUSH CONSTANT SIZE", set_push_constant_instruction->size);
  1102. instruction_data_cursor += sizeof(ComputeListSetPushConstantInstruction);
  1103. instruction_data_cursor += set_push_constant_instruction->size;
  1104. } break;
  1105. case ComputeListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE: {
  1106. const ComputeListUniformSetPrepareForUseInstruction *uniform_set_prepare_for_use_instruction = reinterpret_cast<const ComputeListUniformSetPrepareForUseInstruction *>(instruction);
  1107. 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));
  1108. instruction_data_cursor += sizeof(ComputeListUniformSetPrepareForUseInstruction);
  1109. } break;
  1110. default:
  1111. DEV_ASSERT(false && "Unknown compute list instruction type.");
  1112. return;
  1113. }
  1114. }
  1115. }
  1116. 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) {
  1117. driver = p_driver;
  1118. frames.resize(p_frame_count);
  1119. for (uint32_t i = 0; i < p_frame_count; i++) {
  1120. frames[i].secondary_command_buffers.resize(p_secondary_command_buffers_per_frame);
  1121. for (uint32_t j = 0; j < p_secondary_command_buffers_per_frame; j++) {
  1122. SecondaryCommandBuffer &secondary = frames[i].secondary_command_buffers[j];
  1123. secondary.command_pool = driver->command_pool_create(p_secondary_command_queue_family, RDD::COMMAND_BUFFER_TYPE_SECONDARY);
  1124. secondary.command_buffer = driver->command_buffer_create(secondary.command_pool);
  1125. secondary.task = WorkerThreadPool::INVALID_TASK_ID;
  1126. }
  1127. }
  1128. driver_honors_barriers = driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS);
  1129. }
  1130. void RenderingDeviceGraph::finalize() {
  1131. _wait_for_secondary_command_buffer_tasks();
  1132. for (Frame &f : frames) {
  1133. for (SecondaryCommandBuffer &secondary : f.secondary_command_buffers) {
  1134. if (secondary.command_pool.id != 0) {
  1135. driver->command_pool_free(secondary.command_pool);
  1136. }
  1137. }
  1138. }
  1139. frames.clear();
  1140. }
  1141. void RenderingDeviceGraph::begin() {
  1142. command_data.clear();
  1143. command_data_offsets.clear();
  1144. command_normalization_barriers.clear();
  1145. command_transition_barriers.clear();
  1146. command_buffer_barriers.clear();
  1147. command_label_chars.clear();
  1148. command_label_colors.clear();
  1149. command_label_offsets.clear();
  1150. command_list_nodes.clear();
  1151. read_slice_list_nodes.clear();
  1152. write_slice_list_nodes.clear();
  1153. command_count = 0;
  1154. command_label_count = 0;
  1155. command_timestamp_index = -1;
  1156. command_synchronization_index = -1;
  1157. command_synchronization_pending = false;
  1158. command_label_index = -1;
  1159. frames[frame].secondary_command_buffers_used = 0;
  1160. draw_instruction_list.index = 0;
  1161. compute_instruction_list.index = 0;
  1162. tracking_frame++;
  1163. #ifdef DEV_ENABLED
  1164. write_dependency_counters.clear();
  1165. #endif
  1166. }
  1167. void RenderingDeviceGraph::add_buffer_clear(RDD::BufferID p_dst, ResourceTracker *p_dst_tracker, uint32_t p_offset, uint32_t p_size) {
  1168. DEV_ASSERT(p_dst_tracker != nullptr);
  1169. int32_t command_index;
  1170. RecordedBufferClearCommand *command = static_cast<RecordedBufferClearCommand *>(_allocate_command(sizeof(RecordedBufferClearCommand), command_index));
  1171. command->type = RecordedCommand::TYPE_BUFFER_CLEAR;
  1172. command->self_stages = RDD::PIPELINE_STAGE_TRANSFER_BIT;
  1173. command->buffer = p_dst;
  1174. command->offset = p_offset;
  1175. command->size = p_size;
  1176. ResourceUsage usage = RESOURCE_USAGE_TRANSFER_TO;
  1177. _add_command_to_graph(&p_dst_tracker, &usage, 1, command_index, command);
  1178. }
  1179. 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) {
  1180. // Source tracker is allowed to be null as it could be a read-only buffer.
  1181. DEV_ASSERT(p_dst_tracker != nullptr);
  1182. int32_t command_index;
  1183. RecordedBufferCopyCommand *command = static_cast<RecordedBufferCopyCommand *>(_allocate_command(sizeof(RecordedBufferCopyCommand), command_index));
  1184. command->type = RecordedCommand::TYPE_BUFFER_COPY;
  1185. command->self_stages = RDD::PIPELINE_STAGE_TRANSFER_BIT;
  1186. command->source = p_src;
  1187. command->destination = p_dst;
  1188. command->region = p_region;
  1189. ResourceTracker *trackers[2] = { p_dst_tracker, p_src_tracker };
  1190. ResourceUsage usages[2] = { RESOURCE_USAGE_TRANSFER_TO, RESOURCE_USAGE_TRANSFER_FROM };
  1191. _add_command_to_graph(trackers, usages, p_src_tracker != nullptr ? 2 : 1, command_index, command);
  1192. }
  1193. void RenderingDeviceGraph::add_buffer_get_data(RDD::BufferID p_src, ResourceTracker *p_src_tracker, RDD::BufferID p_dst, RDD::BufferCopyRegion p_region) {
  1194. // Source tracker is allowed to be null as it could be a read-only buffer.
  1195. int32_t command_index;
  1196. RecordedBufferGetDataCommand *command = static_cast<RecordedBufferGetDataCommand *>(_allocate_command(sizeof(RecordedBufferGetDataCommand), command_index));
  1197. command->type = RecordedCommand::TYPE_BUFFER_GET_DATA;
  1198. command->self_stages = RDD::PIPELINE_STAGE_TRANSFER_BIT;
  1199. command->source = p_src;
  1200. command->destination = p_dst;
  1201. command->region = p_region;
  1202. if (p_src_tracker != nullptr) {
  1203. ResourceUsage usage = RESOURCE_USAGE_TRANSFER_FROM;
  1204. _add_command_to_graph(&p_src_tracker, &usage, 1, command_index, command);
  1205. } else {
  1206. _add_command_to_graph(nullptr, nullptr, 0, command_index, command);
  1207. }
  1208. }
  1209. void RenderingDeviceGraph::add_buffer_update(RDD::BufferID p_dst, ResourceTracker *p_dst_tracker, VectorView<RecordedBufferCopy> p_buffer_copies) {
  1210. DEV_ASSERT(p_dst_tracker != nullptr);
  1211. size_t buffer_copies_size = p_buffer_copies.size() * sizeof(RecordedBufferCopy);
  1212. uint64_t command_size = sizeof(RecordedBufferUpdateCommand) + buffer_copies_size;
  1213. int32_t command_index;
  1214. RecordedBufferUpdateCommand *command = static_cast<RecordedBufferUpdateCommand *>(_allocate_command(command_size, command_index));
  1215. command->type = RecordedCommand::TYPE_BUFFER_UPDATE;
  1216. command->self_stages = RDD::PIPELINE_STAGE_TRANSFER_BIT;
  1217. command->destination = p_dst;
  1218. command->buffer_copies_count = p_buffer_copies.size();
  1219. RecordedBufferCopy *buffer_copies = command->buffer_copies();
  1220. for (uint32_t i = 0; i < command->buffer_copies_count; i++) {
  1221. buffer_copies[i] = p_buffer_copies[i];
  1222. }
  1223. ResourceUsage buffer_usage = RESOURCE_USAGE_TRANSFER_TO;
  1224. _add_command_to_graph(&p_dst_tracker, &buffer_usage, 1, command_index, command);
  1225. }
  1226. void RenderingDeviceGraph::add_compute_list_begin() {
  1227. compute_instruction_list.clear();
  1228. compute_instruction_list.index++;
  1229. }
  1230. void RenderingDeviceGraph::add_compute_list_bind_pipeline(RDD::PipelineID p_pipeline) {
  1231. ComputeListBindPipelineInstruction *instruction = reinterpret_cast<ComputeListBindPipelineInstruction *>(_allocate_compute_list_instruction(sizeof(ComputeListBindPipelineInstruction)));
  1232. instruction->type = ComputeListInstruction::TYPE_BIND_PIPELINE;
  1233. instruction->pipeline = p_pipeline;
  1234. compute_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT);
  1235. }
  1236. void RenderingDeviceGraph::add_compute_list_bind_uniform_set(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index) {
  1237. ComputeListBindUniformSetInstruction *instruction = reinterpret_cast<ComputeListBindUniformSetInstruction *>(_allocate_compute_list_instruction(sizeof(ComputeListBindUniformSetInstruction)));
  1238. instruction->type = ComputeListInstruction::TYPE_BIND_UNIFORM_SET;
  1239. instruction->shader = p_shader;
  1240. instruction->uniform_set = p_uniform_set;
  1241. instruction->set_index = set_index;
  1242. }
  1243. void RenderingDeviceGraph::add_compute_list_dispatch(uint32_t p_x_groups, uint32_t p_y_groups, uint32_t p_z_groups) {
  1244. ComputeListDispatchInstruction *instruction = reinterpret_cast<ComputeListDispatchInstruction *>(_allocate_compute_list_instruction(sizeof(ComputeListDispatchInstruction)));
  1245. instruction->type = ComputeListInstruction::TYPE_DISPATCH;
  1246. instruction->x_groups = p_x_groups;
  1247. instruction->y_groups = p_y_groups;
  1248. instruction->z_groups = p_z_groups;
  1249. }
  1250. void RenderingDeviceGraph::add_compute_list_dispatch_indirect(RDD::BufferID p_buffer, uint32_t p_offset) {
  1251. ComputeListDispatchIndirectInstruction *instruction = reinterpret_cast<ComputeListDispatchIndirectInstruction *>(_allocate_compute_list_instruction(sizeof(ComputeListDispatchIndirectInstruction)));
  1252. instruction->type = ComputeListInstruction::TYPE_DISPATCH_INDIRECT;
  1253. instruction->buffer = p_buffer;
  1254. instruction->offset = p_offset;
  1255. compute_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_DRAW_INDIRECT_BIT);
  1256. }
  1257. void RenderingDeviceGraph::add_compute_list_set_push_constant(RDD::ShaderID p_shader, const void *p_data, uint32_t p_data_size) {
  1258. uint32_t instruction_size = sizeof(ComputeListSetPushConstantInstruction) + p_data_size;
  1259. ComputeListSetPushConstantInstruction *instruction = reinterpret_cast<ComputeListSetPushConstantInstruction *>(_allocate_compute_list_instruction(instruction_size));
  1260. instruction->type = ComputeListInstruction::TYPE_SET_PUSH_CONSTANT;
  1261. instruction->size = p_data_size;
  1262. instruction->shader = p_shader;
  1263. memcpy(instruction->data(), p_data, p_data_size);
  1264. }
  1265. void RenderingDeviceGraph::add_compute_list_uniform_set_prepare_for_use(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index) {
  1266. ComputeListUniformSetPrepareForUseInstruction *instruction = reinterpret_cast<ComputeListUniformSetPrepareForUseInstruction *>(_allocate_compute_list_instruction(sizeof(ComputeListUniformSetPrepareForUseInstruction)));
  1267. instruction->type = ComputeListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE;
  1268. instruction->shader = p_shader;
  1269. instruction->uniform_set = p_uniform_set;
  1270. instruction->set_index = set_index;
  1271. }
  1272. void RenderingDeviceGraph::add_compute_list_usage(ResourceTracker *p_tracker, ResourceUsage p_usage) {
  1273. DEV_ASSERT(p_tracker != nullptr);
  1274. p_tracker->reset_if_outdated(tracking_frame);
  1275. if (p_tracker->compute_list_index != compute_instruction_list.index) {
  1276. compute_instruction_list.command_trackers.push_back(p_tracker);
  1277. compute_instruction_list.command_tracker_usages.push_back(p_usage);
  1278. p_tracker->compute_list_index = compute_instruction_list.index;
  1279. p_tracker->compute_list_usage = p_usage;
  1280. }
  1281. #ifdef DEV_ENABLED
  1282. else if (p_tracker->compute_list_usage != p_usage) {
  1283. 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));
  1284. }
  1285. #endif
  1286. }
  1287. void RenderingDeviceGraph::add_compute_list_usages(VectorView<ResourceTracker *> p_trackers, VectorView<ResourceUsage> p_usages) {
  1288. DEV_ASSERT(p_trackers.size() == p_usages.size());
  1289. for (uint32_t i = 0; i < p_trackers.size(); i++) {
  1290. add_compute_list_usage(p_trackers[i], p_usages[i]);
  1291. }
  1292. }
  1293. void RenderingDeviceGraph::add_compute_list_end() {
  1294. int32_t command_index;
  1295. uint32_t instruction_data_size = compute_instruction_list.data.size();
  1296. uint32_t command_size = sizeof(RecordedComputeListCommand) + instruction_data_size;
  1297. RecordedComputeListCommand *command = static_cast<RecordedComputeListCommand *>(_allocate_command(command_size, command_index));
  1298. command->type = RecordedCommand::TYPE_COMPUTE_LIST;
  1299. command->self_stages = compute_instruction_list.stages;
  1300. command->instruction_data_size = instruction_data_size;
  1301. memcpy(command->instruction_data(), compute_instruction_list.data.ptr(), instruction_data_size);
  1302. _add_command_to_graph(compute_instruction_list.command_trackers.ptr(), compute_instruction_list.command_tracker_usages.ptr(), compute_instruction_list.command_trackers.size(), command_index, command);
  1303. }
  1304. void RenderingDeviceGraph::add_draw_list_begin(RDD::RenderPassID p_render_pass, RDD::FramebufferID p_framebuffer, Rect2i p_region, VectorView<RDD::RenderPassClearValue> p_clear_values, bool p_uses_color, bool p_uses_depth) {
  1305. draw_instruction_list.clear();
  1306. draw_instruction_list.index++;
  1307. draw_instruction_list.render_pass = p_render_pass;
  1308. draw_instruction_list.framebuffer = p_framebuffer;
  1309. draw_instruction_list.region = p_region;
  1310. draw_instruction_list.clear_values.resize(p_clear_values.size());
  1311. for (uint32_t i = 0; i < p_clear_values.size(); i++) {
  1312. draw_instruction_list.clear_values[i] = p_clear_values[i];
  1313. }
  1314. if (p_uses_color) {
  1315. draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT);
  1316. }
  1317. if (p_uses_depth) {
  1318. draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT);
  1319. draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT);
  1320. }
  1321. }
  1322. void RenderingDeviceGraph::add_draw_list_bind_index_buffer(RDD::BufferID p_buffer, RDD::IndexBufferFormat p_format, uint32_t p_offset) {
  1323. DrawListBindIndexBufferInstruction *instruction = reinterpret_cast<DrawListBindIndexBufferInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListBindIndexBufferInstruction)));
  1324. instruction->type = DrawListInstruction::TYPE_BIND_INDEX_BUFFER;
  1325. instruction->buffer = p_buffer;
  1326. instruction->format = p_format;
  1327. instruction->offset = p_offset;
  1328. if (instruction->buffer.id != 0) {
  1329. draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_VERTEX_INPUT_BIT);
  1330. }
  1331. }
  1332. void RenderingDeviceGraph::add_draw_list_bind_pipeline(RDD::PipelineID p_pipeline, BitField<RDD::PipelineStageBits> p_pipeline_stage_bits) {
  1333. DrawListBindPipelineInstruction *instruction = reinterpret_cast<DrawListBindPipelineInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListBindPipelineInstruction)));
  1334. instruction->type = DrawListInstruction::TYPE_BIND_PIPELINE;
  1335. instruction->pipeline = p_pipeline;
  1336. draw_instruction_list.stages = draw_instruction_list.stages | p_pipeline_stage_bits;
  1337. }
  1338. void RenderingDeviceGraph::add_draw_list_bind_uniform_set(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index) {
  1339. DrawListBindUniformSetInstruction *instruction = reinterpret_cast<DrawListBindUniformSetInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListBindUniformSetInstruction)));
  1340. instruction->type = DrawListInstruction::TYPE_BIND_UNIFORM_SET;
  1341. instruction->shader = p_shader;
  1342. instruction->uniform_set = p_uniform_set;
  1343. instruction->set_index = set_index;
  1344. }
  1345. void RenderingDeviceGraph::add_draw_list_bind_vertex_buffers(VectorView<RDD::BufferID> p_vertex_buffers, VectorView<uint64_t> p_vertex_buffer_offsets) {
  1346. DEV_ASSERT(p_vertex_buffers.size() == p_vertex_buffer_offsets.size());
  1347. uint32_t instruction_size = sizeof(DrawListBindVertexBuffersInstruction) + sizeof(RDD::BufferID) * p_vertex_buffers.size() + sizeof(uint64_t) * p_vertex_buffer_offsets.size();
  1348. DrawListBindVertexBuffersInstruction *instruction = reinterpret_cast<DrawListBindVertexBuffersInstruction *>(_allocate_draw_list_instruction(instruction_size));
  1349. instruction->type = DrawListInstruction::TYPE_BIND_VERTEX_BUFFERS;
  1350. instruction->vertex_buffers_count = p_vertex_buffers.size();
  1351. RDD::BufferID *vertex_buffers = instruction->vertex_buffers();
  1352. uint64_t *vertex_buffer_offsets = instruction->vertex_buffer_offsets();
  1353. for (uint32_t i = 0; i < instruction->vertex_buffers_count; i++) {
  1354. vertex_buffers[i] = p_vertex_buffers[i];
  1355. vertex_buffer_offsets[i] = p_vertex_buffer_offsets[i];
  1356. }
  1357. if (instruction->vertex_buffers_count > 0) {
  1358. draw_instruction_list.stages.set_flag(RDD::PIPELINE_STAGE_VERTEX_INPUT_BIT);
  1359. }
  1360. }
  1361. void RenderingDeviceGraph::add_draw_list_clear_attachments(VectorView<RDD::AttachmentClear> p_attachments_clear, VectorView<Rect2i> p_attachments_clear_rect) {
  1362. uint32_t instruction_size = sizeof(DrawListClearAttachmentsInstruction) + sizeof(RDD::AttachmentClear) * p_attachments_clear.size() + sizeof(Rect2i) * p_attachments_clear_rect.size();
  1363. DrawListClearAttachmentsInstruction *instruction = reinterpret_cast<DrawListClearAttachmentsInstruction *>(_allocate_draw_list_instruction(instruction_size));
  1364. instruction->type = DrawListInstruction::TYPE_CLEAR_ATTACHMENTS;
  1365. instruction->attachments_clear_count = p_attachments_clear.size();
  1366. instruction->attachments_clear_rect_count = p_attachments_clear_rect.size();
  1367. RDD::AttachmentClear *attachments_clear = instruction->attachments_clear();
  1368. Rect2i *attachments_clear_rect = instruction->attachments_clear_rect();
  1369. for (uint32_t i = 0; i < instruction->attachments_clear_count; i++) {
  1370. attachments_clear[i] = p_attachments_clear[i];
  1371. }
  1372. for (uint32_t i = 0; i < instruction->attachments_clear_rect_count; i++) {
  1373. attachments_clear_rect[i] = p_attachments_clear_rect[i];
  1374. }
  1375. }
  1376. void RenderingDeviceGraph::add_draw_list_draw(uint32_t p_vertex_count, uint32_t p_instance_count) {
  1377. DrawListDrawInstruction *instruction = reinterpret_cast<DrawListDrawInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListDrawInstruction)));
  1378. instruction->type = DrawListInstruction::TYPE_DRAW;
  1379. instruction->vertex_count = p_vertex_count;
  1380. instruction->instance_count = p_instance_count;
  1381. }
  1382. void RenderingDeviceGraph::add_draw_list_draw_indexed(uint32_t p_index_count, uint32_t p_instance_count, uint32_t p_first_index) {
  1383. DrawListDrawIndexedInstruction *instruction = reinterpret_cast<DrawListDrawIndexedInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListDrawIndexedInstruction)));
  1384. instruction->type = DrawListInstruction::TYPE_DRAW_INDEXED;
  1385. instruction->index_count = p_index_count;
  1386. instruction->instance_count = p_instance_count;
  1387. instruction->first_index = p_first_index;
  1388. }
  1389. void RenderingDeviceGraph::add_draw_list_execute_commands(RDD::CommandBufferID p_command_buffer) {
  1390. DrawListExecuteCommandsInstruction *instruction = reinterpret_cast<DrawListExecuteCommandsInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListExecuteCommandsInstruction)));
  1391. instruction->type = DrawListInstruction::TYPE_EXECUTE_COMMANDS;
  1392. instruction->command_buffer = p_command_buffer;
  1393. }
  1394. void RenderingDeviceGraph::add_draw_list_next_subpass(RDD::CommandBufferType p_command_buffer_type) {
  1395. DrawListNextSubpassInstruction *instruction = reinterpret_cast<DrawListNextSubpassInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListNextSubpassInstruction)));
  1396. instruction->type = DrawListInstruction::TYPE_NEXT_SUBPASS;
  1397. instruction->command_buffer_type = p_command_buffer_type;
  1398. }
  1399. void RenderingDeviceGraph::add_draw_list_set_blend_constants(const Color &p_color) {
  1400. DrawListSetBlendConstantsInstruction *instruction = reinterpret_cast<DrawListSetBlendConstantsInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListSetBlendConstantsInstruction)));
  1401. instruction->type = DrawListInstruction::TYPE_SET_BLEND_CONSTANTS;
  1402. instruction->color = p_color;
  1403. }
  1404. void RenderingDeviceGraph::add_draw_list_set_line_width(float p_width) {
  1405. DrawListSetLineWidthInstruction *instruction = reinterpret_cast<DrawListSetLineWidthInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListSetLineWidthInstruction)));
  1406. instruction->type = DrawListInstruction::TYPE_SET_LINE_WIDTH;
  1407. instruction->width = p_width;
  1408. }
  1409. void RenderingDeviceGraph::add_draw_list_set_push_constant(RDD::ShaderID p_shader, const void *p_data, uint32_t p_data_size) {
  1410. uint32_t instruction_size = sizeof(DrawListSetPushConstantInstruction) + p_data_size;
  1411. DrawListSetPushConstantInstruction *instruction = reinterpret_cast<DrawListSetPushConstantInstruction *>(_allocate_draw_list_instruction(instruction_size));
  1412. instruction->type = DrawListInstruction::TYPE_SET_PUSH_CONSTANT;
  1413. instruction->size = p_data_size;
  1414. instruction->shader = p_shader;
  1415. memcpy(instruction->data(), p_data, p_data_size);
  1416. }
  1417. void RenderingDeviceGraph::add_draw_list_set_scissor(Rect2i p_rect) {
  1418. DrawListSetScissorInstruction *instruction = reinterpret_cast<DrawListSetScissorInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListSetScissorInstruction)));
  1419. instruction->type = DrawListInstruction::TYPE_SET_SCISSOR;
  1420. instruction->rect = p_rect;
  1421. }
  1422. void RenderingDeviceGraph::add_draw_list_set_viewport(Rect2i p_rect) {
  1423. DrawListSetViewportInstruction *instruction = reinterpret_cast<DrawListSetViewportInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListSetViewportInstruction)));
  1424. instruction->type = DrawListInstruction::TYPE_SET_VIEWPORT;
  1425. instruction->rect = p_rect;
  1426. }
  1427. void RenderingDeviceGraph::add_draw_list_uniform_set_prepare_for_use(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index) {
  1428. DrawListUniformSetPrepareForUseInstruction *instruction = reinterpret_cast<DrawListUniformSetPrepareForUseInstruction *>(_allocate_draw_list_instruction(sizeof(DrawListUniformSetPrepareForUseInstruction)));
  1429. instruction->type = DrawListInstruction::TYPE_UNIFORM_SET_PREPARE_FOR_USE;
  1430. instruction->shader = p_shader;
  1431. instruction->uniform_set = p_uniform_set;
  1432. instruction->set_index = set_index;
  1433. }
  1434. void RenderingDeviceGraph::add_draw_list_usage(ResourceTracker *p_tracker, ResourceUsage p_usage) {
  1435. p_tracker->reset_if_outdated(tracking_frame);
  1436. if (p_tracker->draw_list_index != draw_instruction_list.index) {
  1437. draw_instruction_list.command_trackers.push_back(p_tracker);
  1438. draw_instruction_list.command_tracker_usages.push_back(p_usage);
  1439. p_tracker->draw_list_index = draw_instruction_list.index;
  1440. p_tracker->draw_list_usage = p_usage;
  1441. }
  1442. #ifdef DEV_ENABLED
  1443. else if (p_tracker->draw_list_usage != p_usage) {
  1444. 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));
  1445. }
  1446. #endif
  1447. }
  1448. void RenderingDeviceGraph::add_draw_list_usages(VectorView<ResourceTracker *> p_trackers, VectorView<ResourceUsage> p_usages) {
  1449. DEV_ASSERT(p_trackers.size() == p_usages.size());
  1450. for (uint32_t i = 0; i < p_trackers.size(); i++) {
  1451. add_draw_list_usage(p_trackers[i], p_usages[i]);
  1452. }
  1453. }
  1454. void RenderingDeviceGraph::add_draw_list_end() {
  1455. // Arbitrary size threshold to evaluate if it'd be best to record the draw list on the background as a secondary buffer.
  1456. const uint32_t instruction_data_threshold_for_secondary = 16384;
  1457. RDD::CommandBufferType command_buffer_type;
  1458. uint32_t &secondary_buffers_used = frames[frame].secondary_command_buffers_used;
  1459. if (draw_instruction_list.data.size() > instruction_data_threshold_for_secondary && secondary_buffers_used < frames[frame].secondary_command_buffers.size()) {
  1460. // Copy the current instruction list data into another array that will be used by the secondary command buffer worker.
  1461. SecondaryCommandBuffer &secondary = frames[frame].secondary_command_buffers[secondary_buffers_used];
  1462. secondary.render_pass = draw_instruction_list.render_pass;
  1463. secondary.framebuffer = draw_instruction_list.framebuffer;
  1464. secondary.instruction_data.resize(draw_instruction_list.data.size());
  1465. memcpy(secondary.instruction_data.ptr(), draw_instruction_list.data.ptr(), draw_instruction_list.data.size());
  1466. // Run a background task for recording the secondary command buffer.
  1467. secondary.task = WorkerThreadPool::get_singleton()->add_template_task(this, &RenderingDeviceGraph::_run_secondary_command_buffer_task, &secondary, true);
  1468. // Clear the instruction list and add a single command for executing the secondary command buffer instead.
  1469. draw_instruction_list.data.clear();
  1470. add_draw_list_execute_commands(secondary.command_buffer);
  1471. secondary_buffers_used++;
  1472. command_buffer_type = RDD::COMMAND_BUFFER_TYPE_SECONDARY;
  1473. } else {
  1474. command_buffer_type = RDD::COMMAND_BUFFER_TYPE_PRIMARY;
  1475. }
  1476. int32_t command_index;
  1477. uint32_t clear_values_size = sizeof(RDD::RenderPassClearValue) * draw_instruction_list.clear_values.size();
  1478. uint32_t instruction_data_size = draw_instruction_list.data.size();
  1479. uint32_t command_size = sizeof(RecordedDrawListCommand) + clear_values_size + instruction_data_size;
  1480. RecordedDrawListCommand *command = static_cast<RecordedDrawListCommand *>(_allocate_command(command_size, command_index));
  1481. command->type = RecordedCommand::TYPE_DRAW_LIST;
  1482. command->self_stages = draw_instruction_list.stages;
  1483. command->instruction_data_size = instruction_data_size;
  1484. command->render_pass = draw_instruction_list.render_pass;
  1485. command->framebuffer = draw_instruction_list.framebuffer;
  1486. command->command_buffer_type = command_buffer_type;
  1487. command->region = draw_instruction_list.region;
  1488. command->clear_values_count = draw_instruction_list.clear_values.size();
  1489. RDD::RenderPassClearValue *clear_values = command->clear_values();
  1490. for (uint32_t i = 0; i < command->clear_values_count; i++) {
  1491. clear_values[i] = draw_instruction_list.clear_values[i];
  1492. }
  1493. memcpy(command->instruction_data(), draw_instruction_list.data.ptr(), instruction_data_size);
  1494. _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);
  1495. }
  1496. void RenderingDeviceGraph::add_texture_clear(RDD::TextureID p_dst, ResourceTracker *p_dst_tracker, const Color &p_color, const RDD::TextureSubresourceRange &p_range) {
  1497. DEV_ASSERT(p_dst_tracker != nullptr);
  1498. int32_t command_index;
  1499. RecordedTextureClearCommand *command = static_cast<RecordedTextureClearCommand *>(_allocate_command(sizeof(RecordedTextureClearCommand), command_index));
  1500. command->type = RecordedCommand::TYPE_TEXTURE_CLEAR;
  1501. command->self_stages = RDD::PIPELINE_STAGE_TRANSFER_BIT;
  1502. command->texture = p_dst;
  1503. command->color = p_color;
  1504. command->range = p_range;
  1505. ResourceUsage usage = RESOURCE_USAGE_TRANSFER_TO;
  1506. _add_command_to_graph(&p_dst_tracker, &usage, 1, command_index, command);
  1507. }
  1508. 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) {
  1509. DEV_ASSERT(p_src_tracker != nullptr);
  1510. DEV_ASSERT(p_dst_tracker != nullptr);
  1511. int32_t command_index;
  1512. RecordedTextureCopyCommand *command = static_cast<RecordedTextureCopyCommand *>(_allocate_command(sizeof(RecordedTextureCopyCommand), command_index));
  1513. command->type = RecordedCommand::TYPE_TEXTURE_COPY;
  1514. command->self_stages = RDD::PIPELINE_STAGE_TRANSFER_BIT;
  1515. command->from_texture = p_src;
  1516. command->to_texture = p_dst;
  1517. command->region = p_region;
  1518. ResourceTracker *trackers[2] = { p_dst_tracker, p_src_tracker };
  1519. ResourceUsage usages[2] = { RESOURCE_USAGE_TRANSFER_TO, RESOURCE_USAGE_TRANSFER_FROM };
  1520. _add_command_to_graph(trackers, usages, 2, command_index, command);
  1521. }
  1522. 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) {
  1523. DEV_ASSERT(p_src_tracker != nullptr);
  1524. int32_t command_index;
  1525. uint64_t command_size = sizeof(RecordedTextureGetDataCommand) + p_buffer_texture_copy_regions.size() * sizeof(RDD::BufferTextureCopyRegion);
  1526. RecordedTextureGetDataCommand *command = static_cast<RecordedTextureGetDataCommand *>(_allocate_command(command_size, command_index));
  1527. command->type = RecordedCommand::TYPE_TEXTURE_GET_DATA;
  1528. command->self_stages = RDD::PIPELINE_STAGE_TRANSFER_BIT;
  1529. command->from_texture = p_src;
  1530. command->to_buffer = p_dst;
  1531. command->buffer_texture_copy_regions_count = p_buffer_texture_copy_regions.size();
  1532. RDD::BufferTextureCopyRegion *buffer_texture_copy_regions = command->buffer_texture_copy_regions();
  1533. for (uint32_t i = 0; i < command->buffer_texture_copy_regions_count; i++) {
  1534. buffer_texture_copy_regions[i] = p_buffer_texture_copy_regions[i];
  1535. }
  1536. ResourceUsage usage = RESOURCE_USAGE_TRANSFER_FROM;
  1537. _add_command_to_graph(&p_src_tracker, &usage, 1, command_index, command);
  1538. }
  1539. 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) {
  1540. DEV_ASSERT(p_src_tracker != nullptr);
  1541. DEV_ASSERT(p_dst_tracker != nullptr);
  1542. int32_t command_index;
  1543. RecordedTextureResolveCommand *command = static_cast<RecordedTextureResolveCommand *>(_allocate_command(sizeof(RecordedTextureResolveCommand), command_index));
  1544. command->type = RecordedCommand::TYPE_TEXTURE_RESOLVE;
  1545. command->self_stages = RDD::PIPELINE_STAGE_TRANSFER_BIT;
  1546. command->from_texture = p_src;
  1547. command->to_texture = p_dst;
  1548. command->src_layer = p_src_layer;
  1549. command->src_mipmap = p_src_mipmap;
  1550. command->dst_layer = p_dst_layer;
  1551. command->dst_mipmap = p_dst_mipmap;
  1552. ResourceTracker *trackers[2] = { p_dst_tracker, p_src_tracker };
  1553. ResourceUsage usages[2] = { RESOURCE_USAGE_TRANSFER_TO, RESOURCE_USAGE_TRANSFER_FROM };
  1554. _add_command_to_graph(trackers, usages, 2, command_index, command);
  1555. }
  1556. void RenderingDeviceGraph::add_texture_update(RDD::TextureID p_dst, ResourceTracker *p_dst_tracker, VectorView<RecordedBufferToTextureCopy> p_buffer_copies) {
  1557. DEV_ASSERT(p_dst_tracker != nullptr);
  1558. int32_t command_index;
  1559. uint64_t command_size = sizeof(RecordedTextureUpdateCommand) + p_buffer_copies.size() * sizeof(RecordedBufferToTextureCopy);
  1560. RecordedTextureUpdateCommand *command = static_cast<RecordedTextureUpdateCommand *>(_allocate_command(command_size, command_index));
  1561. command->type = RecordedCommand::TYPE_TEXTURE_UPDATE;
  1562. command->self_stages = RDD::PIPELINE_STAGE_TRANSFER_BIT;
  1563. command->to_texture = p_dst;
  1564. command->buffer_to_texture_copies_count = p_buffer_copies.size();
  1565. RecordedBufferToTextureCopy *buffer_to_texture_copies = command->buffer_to_texture_copies();
  1566. for (uint32_t i = 0; i < command->buffer_to_texture_copies_count; i++) {
  1567. buffer_to_texture_copies[i] = p_buffer_copies[i];
  1568. }
  1569. ResourceUsage usage = RESOURCE_USAGE_TRANSFER_TO;
  1570. _add_command_to_graph(&p_dst_tracker, &usage, 1, command_index, command);
  1571. }
  1572. void RenderingDeviceGraph::add_capture_timestamp(RDD::QueryPoolID p_query_pool, uint32_t p_index) {
  1573. int32_t command_index;
  1574. RecordedCaptureTimestampCommand *command = static_cast<RecordedCaptureTimestampCommand *>(_allocate_command(sizeof(RecordedCaptureTimestampCommand), command_index));
  1575. command->type = RecordedCommand::TYPE_CAPTURE_TIMESTAMP;
  1576. command->self_stages = 0;
  1577. command->pool = p_query_pool;
  1578. command->index = p_index;
  1579. _add_command_to_graph(nullptr, nullptr, 0, command_index, command);
  1580. }
  1581. void RenderingDeviceGraph::add_synchronization() {
  1582. // Synchronization is only acknowledged if commands have been recorded on the graph already.
  1583. if (command_count > 0) {
  1584. command_synchronization_pending = true;
  1585. }
  1586. }
  1587. void RenderingDeviceGraph::begin_label(const String &p_label_name, const Color &p_color) {
  1588. uint32_t command_label_offset = command_label_chars.size();
  1589. PackedByteArray command_label_utf8 = p_label_name.to_utf8_buffer();
  1590. int command_label_utf8_size = command_label_utf8.size();
  1591. command_label_chars.resize(command_label_offset + command_label_utf8_size + 1);
  1592. memcpy(&command_label_chars[command_label_offset], command_label_utf8.ptr(), command_label_utf8.size());
  1593. command_label_chars[command_label_offset + command_label_utf8_size] = '\0';
  1594. command_label_colors.push_back(p_color);
  1595. command_label_offsets.push_back(command_label_offset);
  1596. command_label_index = command_label_count;
  1597. command_label_count++;
  1598. }
  1599. void RenderingDeviceGraph::end_label() {
  1600. command_label_index = -1;
  1601. }
  1602. void RenderingDeviceGraph::end(RDD::CommandBufferID p_command_buffer, bool p_reorder_commands, bool p_full_barriers) {
  1603. if (command_count == 0) {
  1604. // No commands have been logged, do nothing.
  1605. return;
  1606. }
  1607. thread_local LocalVector<RecordedCommandSort> commands_sorted;
  1608. if (p_reorder_commands) {
  1609. thread_local LocalVector<int64_t> command_stack;
  1610. thread_local LocalVector<int32_t> sorted_command_indices;
  1611. thread_local LocalVector<uint32_t> command_degrees;
  1612. int32_t adjacency_list_index = 0;
  1613. int32_t command_index;
  1614. // Count all the incoming connections to every node by traversing their adjacency list.
  1615. command_degrees.resize(command_count);
  1616. memset(command_degrees.ptr(), 0, sizeof(uint32_t) * command_degrees.size());
  1617. for (uint32_t i = 0; i < command_count; i++) {
  1618. const RecordedCommand &recorded_command = *reinterpret_cast<const RecordedCommand *>(&command_data[command_data_offsets[i]]);
  1619. adjacency_list_index = recorded_command.adjacent_command_list_index;
  1620. while (adjacency_list_index >= 0) {
  1621. const RecordedCommandListNode &command_list_node = command_list_nodes[adjacency_list_index];
  1622. DEV_ASSERT((command_list_node.command_index != int32_t(i)) && "Command can't have itself as a dependency.");
  1623. command_degrees[command_list_node.command_index] += 1;
  1624. adjacency_list_index = command_list_node.next_list_index;
  1625. }
  1626. }
  1627. // Push to the stack all nodes that have no incoming connections.
  1628. command_stack.clear();
  1629. for (uint32_t i = 0; i < command_count; i++) {
  1630. if (command_degrees[i] == 0) {
  1631. command_stack.push_back(i);
  1632. }
  1633. }
  1634. sorted_command_indices.clear();
  1635. while (!command_stack.is_empty()) {
  1636. // Pop command from the stack.
  1637. command_index = command_stack[command_stack.size() - 1];
  1638. command_stack.resize(command_stack.size() - 1);
  1639. // Add it to the sorted commands.
  1640. sorted_command_indices.push_back(command_index);
  1641. // Search for its adjacents and lower their degree for every visit. If the degree reaches zero, we push the command to the stack.
  1642. const uint32_t command_data_offset = command_data_offsets[command_index];
  1643. const RecordedCommand &recorded_command = *reinterpret_cast<const RecordedCommand *>(&command_data[command_data_offset]);
  1644. adjacency_list_index = recorded_command.adjacent_command_list_index;
  1645. while (adjacency_list_index >= 0) {
  1646. const RecordedCommandListNode &command_list_node = command_list_nodes[adjacency_list_index];
  1647. uint32_t &command_degree = command_degrees[command_list_node.command_index];
  1648. DEV_ASSERT(command_degree > 0);
  1649. command_degree--;
  1650. if (command_degree == 0) {
  1651. command_stack.push_back(command_list_node.command_index);
  1652. }
  1653. adjacency_list_index = command_list_node.next_list_index;
  1654. }
  1655. }
  1656. // Batch buffer, texture, draw lists and compute operations together.
  1657. const uint32_t PriorityTable[RecordedCommand::TYPE_MAX] = {
  1658. 0, // TYPE_NONE
  1659. 1, // TYPE_BUFFER_CLEAR
  1660. 1, // TYPE_BUFFER_COPY
  1661. 1, // TYPE_BUFFER_GET_DATA
  1662. 1, // TYPE_BUFFER_UPDATE
  1663. 4, // TYPE_COMPUTE_LIST
  1664. 3, // TYPE_DRAW_LIST
  1665. 2, // TYPE_TEXTURE_CLEAR
  1666. 2, // TYPE_TEXTURE_COPY
  1667. 2, // TYPE_TEXTURE_GET_DATA
  1668. 2, // TYPE_TEXTURE_RESOLVE
  1669. 2, // TYPE_TEXTURE_UPDATE
  1670. };
  1671. commands_sorted.clear();
  1672. commands_sorted.resize(command_count);
  1673. for (uint32_t i = 0; i < command_count; i++) {
  1674. const int32_t sorted_command_index = sorted_command_indices[i];
  1675. const uint32_t command_data_offset = command_data_offsets[sorted_command_index];
  1676. const RecordedCommand recorded_command = *reinterpret_cast<const RecordedCommand *>(&command_data[command_data_offset]);
  1677. const uint32_t next_command_level = commands_sorted[sorted_command_index].level + 1;
  1678. adjacency_list_index = recorded_command.adjacent_command_list_index;
  1679. while (adjacency_list_index >= 0) {
  1680. const RecordedCommandListNode &command_list_node = command_list_nodes[adjacency_list_index];
  1681. uint32_t &adjacent_command_level = commands_sorted[command_list_node.command_index].level;
  1682. if (adjacent_command_level < next_command_level) {
  1683. adjacent_command_level = next_command_level;
  1684. }
  1685. adjacency_list_index = command_list_node.next_list_index;
  1686. }
  1687. commands_sorted[sorted_command_index].index = sorted_command_index;
  1688. commands_sorted[sorted_command_index].priority = PriorityTable[recorded_command.type];
  1689. }
  1690. } else {
  1691. commands_sorted.clear();
  1692. commands_sorted.resize(command_count);
  1693. for (uint32_t i = 0; i < command_count; i++) {
  1694. commands_sorted[i].index = i;
  1695. }
  1696. }
  1697. _wait_for_secondary_command_buffer_tasks();
  1698. if (command_count > 0) {
  1699. int32_t current_label_index = -1;
  1700. int32_t current_label_level = -1;
  1701. _run_label_command_change(p_command_buffer, -1, -1, true, true, nullptr, 0, current_label_index, current_label_level);
  1702. if (p_reorder_commands) {
  1703. #if PRINT_RENDER_GRAPH
  1704. print_line("BEFORE SORT");
  1705. _print_render_commands(commands_sorted.ptr(), command_count);
  1706. #endif
  1707. commands_sorted.sort();
  1708. #if PRINT_RENDER_GRAPH
  1709. print_line("AFTER SORT");
  1710. _print_render_commands(commands_sorted.ptr(), command_count);
  1711. #endif
  1712. #if PRINT_COMMAND_RECORDING
  1713. print_line(vformat("Recording %d commands", command_count));
  1714. #endif
  1715. uint32_t boosted_priority = 0;
  1716. uint32_t current_level = commands_sorted[0].level;
  1717. uint32_t current_level_start = 0;
  1718. for (uint32_t i = 0; i < command_count; i++) {
  1719. if (current_level != commands_sorted[i].level) {
  1720. RecordedCommandSort *level_command_ptr = &commands_sorted[current_level_start];
  1721. uint32_t level_command_count = i - current_level_start;
  1722. _boost_priority_for_render_commands(level_command_ptr, level_command_count, boosted_priority);
  1723. _group_barriers_for_render_commands(p_command_buffer, level_command_ptr, level_command_count, p_full_barriers);
  1724. _run_render_commands(p_command_buffer, current_level, level_command_ptr, level_command_count, current_label_index, current_label_level);
  1725. current_level = commands_sorted[i].level;
  1726. current_level_start = i;
  1727. }
  1728. }
  1729. RecordedCommandSort *level_command_ptr = &commands_sorted[current_level_start];
  1730. uint32_t level_command_count = command_count - current_level_start;
  1731. _boost_priority_for_render_commands(level_command_ptr, level_command_count, boosted_priority);
  1732. _group_barriers_for_render_commands(p_command_buffer, level_command_ptr, level_command_count, p_full_barriers);
  1733. _run_render_commands(p_command_buffer, current_level, level_command_ptr, level_command_count, current_label_index, current_label_level);
  1734. #if PRINT_RENDER_GRAPH
  1735. print_line("COMMANDS", command_count, "LEVELS", current_level + 1);
  1736. #endif
  1737. } else {
  1738. for (uint32_t i = 0; i < command_count; i++) {
  1739. _group_barriers_for_render_commands(p_command_buffer, &commands_sorted[i], 1, p_full_barriers);
  1740. _run_render_commands(p_command_buffer, i, &commands_sorted[i], 1, current_label_index, current_label_level);
  1741. }
  1742. }
  1743. _run_label_command_change(p_command_buffer, -1, -1, true, false, nullptr, 0, current_label_index, current_label_level);
  1744. #if PRINT_COMMAND_RECORDING
  1745. print_line(vformat("Recorded %d commands", command_count));
  1746. #endif
  1747. }
  1748. // 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.
  1749. frame = (frame + 1) % frames.size();
  1750. }
  1751. #if PRINT_RESOURCE_TRACKER_TOTAL
  1752. static uint32_t resource_tracker_total = 0;
  1753. #endif
  1754. RenderingDeviceGraph::ResourceTracker *RenderingDeviceGraph::resource_tracker_create() {
  1755. #if PRINT_RESOURCE_TRACKER_TOTAL
  1756. print_line("Resource trackers:", ++resource_tracker_total);
  1757. #endif
  1758. return memnew(ResourceTracker);
  1759. }
  1760. void RenderingDeviceGraph::resource_tracker_free(ResourceTracker *tracker) {
  1761. if (tracker == nullptr) {
  1762. return;
  1763. }
  1764. if (tracker->in_parent_dirty_list) {
  1765. // Delete the tracker from the parent's dirty linked list.
  1766. if (tracker->parent->dirty_shared_list == tracker) {
  1767. tracker->parent->dirty_shared_list = tracker->next_shared;
  1768. } else {
  1769. ResourceTracker *node = tracker->parent->dirty_shared_list;
  1770. while (node != nullptr) {
  1771. if (node->next_shared == tracker) {
  1772. node->next_shared = tracker->next_shared;
  1773. node = nullptr;
  1774. } else {
  1775. node = node->next_shared;
  1776. }
  1777. }
  1778. }
  1779. }
  1780. memdelete(tracker);
  1781. #if PRINT_RESOURCE_TRACKER_TOTAL
  1782. print_line("Resource trackers:", --resource_tracker_total);
  1783. #endif
  1784. }