rendering_device_graph.cpp 110 KB

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