rendering_device_graph.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. /**************************************************************************/
  2. /* rendering_device_graph.h */
  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. #ifndef RENDERING_DEVICE_GRAPH_H
  31. #define RENDERING_DEVICE_GRAPH_H
  32. #include "core/object/worker_thread_pool.h"
  33. #include "rendering_device_commons.h"
  34. #include "rendering_device_driver.h"
  35. // Buffer barriers have not shown any significant improvement or shown to be
  36. // even detrimental to performance. However, there are currently some known
  37. // cases where using them can solve problems that using singular memory
  38. // barriers does not, probably due to driver issues (see comment on PR #84976
  39. // https://github.com/godotengine/godot/pull/84976#issuecomment-1878566830).
  40. #define USE_BUFFER_BARRIERS 1
  41. class RenderingDeviceGraph {
  42. public:
  43. struct ComputeListInstruction {
  44. enum Type {
  45. TYPE_NONE,
  46. TYPE_BIND_PIPELINE,
  47. TYPE_BIND_UNIFORM_SET,
  48. TYPE_DISPATCH,
  49. TYPE_DISPATCH_INDIRECT,
  50. TYPE_SET_PUSH_CONSTANT,
  51. TYPE_UNIFORM_SET_PREPARE_FOR_USE
  52. };
  53. Type type = TYPE_NONE;
  54. };
  55. struct DrawListInstruction {
  56. enum Type {
  57. TYPE_NONE,
  58. TYPE_BIND_INDEX_BUFFER,
  59. TYPE_BIND_PIPELINE,
  60. TYPE_BIND_UNIFORM_SET,
  61. TYPE_BIND_VERTEX_BUFFERS,
  62. TYPE_CLEAR_ATTACHMENTS,
  63. TYPE_DRAW,
  64. TYPE_DRAW_INDEXED,
  65. TYPE_EXECUTE_COMMANDS,
  66. TYPE_NEXT_SUBPASS,
  67. TYPE_SET_BLEND_CONSTANTS,
  68. TYPE_SET_LINE_WIDTH,
  69. TYPE_SET_PUSH_CONSTANT,
  70. TYPE_SET_SCISSOR,
  71. TYPE_SET_VIEWPORT,
  72. TYPE_UNIFORM_SET_PREPARE_FOR_USE
  73. };
  74. Type type = TYPE_NONE;
  75. };
  76. struct RecordedCommand {
  77. enum Type {
  78. TYPE_NONE,
  79. TYPE_BUFFER_CLEAR,
  80. TYPE_BUFFER_COPY,
  81. TYPE_BUFFER_GET_DATA,
  82. TYPE_BUFFER_UPDATE,
  83. TYPE_COMPUTE_LIST,
  84. TYPE_DRAW_LIST,
  85. TYPE_TEXTURE_CLEAR,
  86. TYPE_TEXTURE_COPY,
  87. TYPE_TEXTURE_GET_DATA,
  88. TYPE_TEXTURE_RESOLVE,
  89. TYPE_TEXTURE_UPDATE,
  90. TYPE_CAPTURE_TIMESTAMP,
  91. TYPE_MAX
  92. };
  93. Type type = TYPE_NONE;
  94. int32_t adjacent_command_list_index = -1;
  95. RDD::MemoryBarrier memory_barrier;
  96. int32_t normalization_barrier_index = -1;
  97. int normalization_barrier_count = 0;
  98. int32_t transition_barrier_index = -1;
  99. int32_t transition_barrier_count = 0;
  100. #if USE_BUFFER_BARRIERS
  101. int32_t buffer_barrier_index = -1;
  102. int32_t buffer_barrier_count = 0;
  103. #endif
  104. int32_t label_index = -1;
  105. BitField<RDD::PipelineStageBits> previous_stages;
  106. BitField<RDD::PipelineStageBits> next_stages;
  107. BitField<RDD::PipelineStageBits> self_stages;
  108. };
  109. struct RecordedBufferCopy {
  110. RDD::BufferID source;
  111. RDD::BufferCopyRegion region;
  112. };
  113. struct RecordedBufferToTextureCopy {
  114. RDD::BufferID from_buffer;
  115. RDD::BufferTextureCopyRegion region;
  116. };
  117. enum ResourceUsage {
  118. RESOURCE_USAGE_NONE,
  119. RESOURCE_USAGE_COPY_FROM,
  120. RESOURCE_USAGE_COPY_TO,
  121. RESOURCE_USAGE_RESOLVE_FROM,
  122. RESOURCE_USAGE_RESOLVE_TO,
  123. RESOURCE_USAGE_UNIFORM_BUFFER_READ,
  124. RESOURCE_USAGE_INDIRECT_BUFFER_READ,
  125. RESOURCE_USAGE_TEXTURE_BUFFER_READ,
  126. RESOURCE_USAGE_TEXTURE_BUFFER_READ_WRITE,
  127. RESOURCE_USAGE_STORAGE_BUFFER_READ,
  128. RESOURCE_USAGE_STORAGE_BUFFER_READ_WRITE,
  129. RESOURCE_USAGE_VERTEX_BUFFER_READ,
  130. RESOURCE_USAGE_INDEX_BUFFER_READ,
  131. RESOURCE_USAGE_TEXTURE_SAMPLE,
  132. RESOURCE_USAGE_STORAGE_IMAGE_READ,
  133. RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE,
  134. RESOURCE_USAGE_ATTACHMENT_COLOR_READ_WRITE,
  135. RESOURCE_USAGE_ATTACHMENT_DEPTH_STENCIL_READ_WRITE
  136. };
  137. struct ResourceTracker {
  138. uint32_t reference_count = 0;
  139. int64_t command_frame = -1;
  140. int32_t read_full_command_list_index = -1;
  141. int32_t read_slice_command_list_index = -1;
  142. int32_t write_command_or_list_index = -1;
  143. int32_t draw_list_index = -1;
  144. ResourceUsage draw_list_usage = RESOURCE_USAGE_NONE;
  145. int32_t compute_list_index = -1;
  146. ResourceUsage compute_list_usage = RESOURCE_USAGE_NONE;
  147. ResourceUsage usage = RESOURCE_USAGE_NONE;
  148. BitField<RDD::BarrierAccessBits> usage_access;
  149. RDD::BufferID buffer_driver_id;
  150. RDD::TextureID texture_driver_id;
  151. RDD::TextureSubresourceRange texture_subresources;
  152. uint32_t texture_usage = 0;
  153. int32_t texture_slice_command_index = -1;
  154. ResourceTracker *parent = nullptr;
  155. ResourceTracker *dirty_shared_list = nullptr;
  156. ResourceTracker *next_shared = nullptr;
  157. Rect2i texture_slice_or_dirty_rect;
  158. bool in_parent_dirty_list = false;
  159. bool write_command_list_enabled = false;
  160. _FORCE_INLINE_ void reset_if_outdated(int64_t new_command_frame) {
  161. if (new_command_frame != command_frame) {
  162. usage_access.clear();
  163. command_frame = new_command_frame;
  164. read_full_command_list_index = -1;
  165. read_slice_command_list_index = -1;
  166. write_command_or_list_index = -1;
  167. draw_list_index = -1;
  168. compute_list_index = -1;
  169. texture_slice_command_index = -1;
  170. write_command_list_enabled = false;
  171. }
  172. }
  173. };
  174. struct CommandBufferPool {
  175. // Provided by RenderingDevice.
  176. RDD::CommandPoolID pool;
  177. // Created internally by RenderingDeviceGraph.
  178. LocalVector<RDD::CommandBufferID> buffers;
  179. LocalVector<RDD::SemaphoreID> semaphores;
  180. uint32_t buffers_used = 0;
  181. };
  182. struct WorkaroundsState {
  183. bool draw_list_found = false;
  184. };
  185. private:
  186. struct InstructionList {
  187. LocalVector<uint8_t> data;
  188. LocalVector<ResourceTracker *> command_trackers;
  189. LocalVector<ResourceUsage> command_tracker_usages;
  190. BitField<RDD::PipelineStageBits> stages;
  191. int32_t index = 0;
  192. void clear() {
  193. data.clear();
  194. command_trackers.clear();
  195. command_tracker_usages.clear();
  196. stages.clear();
  197. }
  198. };
  199. struct ComputeInstructionList : InstructionList {
  200. // No extra contents.
  201. };
  202. struct DrawInstructionList : InstructionList {
  203. RDD::RenderPassID render_pass;
  204. RDD::FramebufferID framebuffer;
  205. Rect2i region;
  206. LocalVector<RDD::RenderPassClearValue> clear_values;
  207. };
  208. struct RecordedCommandSort {
  209. uint32_t level = 0;
  210. uint32_t priority = 0;
  211. int32_t index = -1;
  212. RecordedCommandSort() = default;
  213. bool operator<(const RecordedCommandSort &p_other) const {
  214. if (level < p_other.level) {
  215. return true;
  216. } else if (level > p_other.level) {
  217. return false;
  218. }
  219. if (priority < p_other.priority) {
  220. return true;
  221. } else if (priority > p_other.priority) {
  222. return false;
  223. }
  224. return index < p_other.index;
  225. }
  226. };
  227. struct RecordedCommandListNode {
  228. int32_t command_index = -1;
  229. int32_t next_list_index = -1;
  230. };
  231. struct RecordedSliceListNode {
  232. int32_t command_index = -1;
  233. int32_t next_list_index = -1;
  234. Rect2i subresources;
  235. };
  236. struct RecordedBufferClearCommand : RecordedCommand {
  237. RDD::BufferID buffer;
  238. uint32_t offset = 0;
  239. uint32_t size = 0;
  240. };
  241. struct RecordedBufferCopyCommand : RecordedCommand {
  242. RDD::BufferID source;
  243. RDD::BufferID destination;
  244. RDD::BufferCopyRegion region;
  245. };
  246. struct RecordedBufferGetDataCommand : RecordedCommand {
  247. RDD::BufferID source;
  248. RDD::BufferID destination;
  249. RDD::BufferCopyRegion region;
  250. };
  251. struct RecordedBufferUpdateCommand : RecordedCommand {
  252. RDD::BufferID destination;
  253. uint32_t buffer_copies_count = 0;
  254. _FORCE_INLINE_ RecordedBufferCopy *buffer_copies() {
  255. return reinterpret_cast<RecordedBufferCopy *>(&this[1]);
  256. }
  257. _FORCE_INLINE_ const RecordedBufferCopy *buffer_copies() const {
  258. return reinterpret_cast<const RecordedBufferCopy *>(&this[1]);
  259. }
  260. };
  261. struct RecordedComputeListCommand : RecordedCommand {
  262. uint32_t instruction_data_size = 0;
  263. _FORCE_INLINE_ uint8_t *instruction_data() {
  264. return reinterpret_cast<uint8_t *>(&this[1]);
  265. }
  266. _FORCE_INLINE_ const uint8_t *instruction_data() const {
  267. return reinterpret_cast<const uint8_t *>(&this[1]);
  268. }
  269. };
  270. struct RecordedDrawListCommand : RecordedCommand {
  271. uint32_t instruction_data_size = 0;
  272. RDD::RenderPassID render_pass;
  273. RDD::FramebufferID framebuffer;
  274. RDD::CommandBufferType command_buffer_type;
  275. Rect2i region;
  276. uint32_t clear_values_count = 0;
  277. _FORCE_INLINE_ RDD::RenderPassClearValue *clear_values() {
  278. return reinterpret_cast<RDD::RenderPassClearValue *>(&this[1]);
  279. }
  280. _FORCE_INLINE_ const RDD::RenderPassClearValue *clear_values() const {
  281. return reinterpret_cast<const RDD::RenderPassClearValue *>(&this[1]);
  282. }
  283. _FORCE_INLINE_ uint8_t *instruction_data() {
  284. return reinterpret_cast<uint8_t *>(&clear_values()[clear_values_count]);
  285. }
  286. _FORCE_INLINE_ const uint8_t *instruction_data() const {
  287. return reinterpret_cast<const uint8_t *>(&clear_values()[clear_values_count]);
  288. }
  289. };
  290. struct RecordedTextureClearCommand : RecordedCommand {
  291. RDD::TextureID texture;
  292. RDD::TextureSubresourceRange range;
  293. Color color;
  294. };
  295. struct RecordedTextureCopyCommand : RecordedCommand {
  296. RDD::TextureID from_texture;
  297. RDD::TextureID to_texture;
  298. uint32_t texture_copy_regions_count = 0;
  299. _FORCE_INLINE_ RDD::TextureCopyRegion *texture_copy_regions() {
  300. return reinterpret_cast<RDD::TextureCopyRegion *>(&this[1]);
  301. }
  302. _FORCE_INLINE_ const RDD::TextureCopyRegion *texture_copy_regions() const {
  303. return reinterpret_cast<const RDD::TextureCopyRegion *>(&this[1]);
  304. }
  305. };
  306. struct RecordedTextureGetDataCommand : RecordedCommand {
  307. RDD::TextureID from_texture;
  308. RDD::BufferID to_buffer;
  309. uint32_t buffer_texture_copy_regions_count = 0;
  310. _FORCE_INLINE_ RDD::BufferTextureCopyRegion *buffer_texture_copy_regions() {
  311. return reinterpret_cast<RDD::BufferTextureCopyRegion *>(&this[1]);
  312. }
  313. _FORCE_INLINE_ const RDD::BufferTextureCopyRegion *buffer_texture_copy_regions() const {
  314. return reinterpret_cast<const RDD::BufferTextureCopyRegion *>(&this[1]);
  315. }
  316. };
  317. struct RecordedTextureResolveCommand : RecordedCommand {
  318. RDD::TextureID from_texture;
  319. RDD::TextureID to_texture;
  320. uint32_t src_layer = 0;
  321. uint32_t src_mipmap = 0;
  322. uint32_t dst_layer = 0;
  323. uint32_t dst_mipmap = 0;
  324. };
  325. struct RecordedTextureUpdateCommand : RecordedCommand {
  326. RDD::TextureID to_texture;
  327. uint32_t buffer_to_texture_copies_count = 0;
  328. _FORCE_INLINE_ RecordedBufferToTextureCopy *buffer_to_texture_copies() {
  329. return reinterpret_cast<RecordedBufferToTextureCopy *>(&this[1]);
  330. }
  331. _FORCE_INLINE_ const RecordedBufferToTextureCopy *buffer_to_texture_copies() const {
  332. return reinterpret_cast<const RecordedBufferToTextureCopy *>(&this[1]);
  333. }
  334. };
  335. struct RecordedCaptureTimestampCommand : RecordedCommand {
  336. RDD::QueryPoolID pool;
  337. uint32_t index = 0;
  338. };
  339. struct DrawListBindIndexBufferInstruction : DrawListInstruction {
  340. RDD::BufferID buffer;
  341. RenderingDeviceCommons::IndexBufferFormat format;
  342. uint32_t offset = 0;
  343. };
  344. struct DrawListBindPipelineInstruction : DrawListInstruction {
  345. RDD::PipelineID pipeline;
  346. };
  347. struct DrawListBindUniformSetInstruction : DrawListInstruction {
  348. RDD::UniformSetID uniform_set;
  349. RDD::ShaderID shader;
  350. uint32_t set_index = 0;
  351. };
  352. struct DrawListBindVertexBuffersInstruction : DrawListInstruction {
  353. uint32_t vertex_buffers_count = 0;
  354. _FORCE_INLINE_ RDD::BufferID *vertex_buffers() {
  355. return reinterpret_cast<RDD::BufferID *>(&this[1]);
  356. }
  357. _FORCE_INLINE_ const RDD::BufferID *vertex_buffers() const {
  358. return reinterpret_cast<const RDD::BufferID *>(&this[1]);
  359. }
  360. _FORCE_INLINE_ uint64_t *vertex_buffer_offsets() {
  361. return reinterpret_cast<uint64_t *>(&vertex_buffers()[vertex_buffers_count]);
  362. }
  363. _FORCE_INLINE_ const uint64_t *vertex_buffer_offsets() const {
  364. return reinterpret_cast<const uint64_t *>(&vertex_buffers()[vertex_buffers_count]);
  365. }
  366. };
  367. struct DrawListClearAttachmentsInstruction : DrawListInstruction {
  368. uint32_t attachments_clear_count = 0;
  369. uint32_t attachments_clear_rect_count = 0;
  370. _FORCE_INLINE_ RDD::AttachmentClear *attachments_clear() {
  371. return reinterpret_cast<RDD::AttachmentClear *>(&this[1]);
  372. }
  373. _FORCE_INLINE_ const RDD::AttachmentClear *attachments_clear() const {
  374. return reinterpret_cast<const RDD::AttachmentClear *>(&this[1]);
  375. }
  376. _FORCE_INLINE_ Rect2i *attachments_clear_rect() {
  377. return reinterpret_cast<Rect2i *>(&attachments_clear()[attachments_clear_count]);
  378. }
  379. _FORCE_INLINE_ const Rect2i *attachments_clear_rect() const {
  380. return reinterpret_cast<const Rect2i *>(&attachments_clear()[attachments_clear_count]);
  381. }
  382. };
  383. struct DrawListDrawInstruction : DrawListInstruction {
  384. uint32_t vertex_count = 0;
  385. uint32_t instance_count = 0;
  386. };
  387. struct DrawListDrawIndexedInstruction : DrawListInstruction {
  388. uint32_t index_count = 0;
  389. uint32_t instance_count = 0;
  390. uint32_t first_index = 0;
  391. };
  392. struct DrawListEndRenderPassInstruction : DrawListInstruction {
  393. // No contents.
  394. };
  395. struct DrawListExecuteCommandsInstruction : DrawListInstruction {
  396. RDD::CommandBufferID command_buffer;
  397. };
  398. struct DrawListSetPushConstantInstruction : DrawListInstruction {
  399. uint32_t size = 0;
  400. RDD::ShaderID shader;
  401. _FORCE_INLINE_ uint8_t *data() {
  402. return reinterpret_cast<uint8_t *>(&this[1]);
  403. }
  404. _FORCE_INLINE_ const uint8_t *data() const {
  405. return reinterpret_cast<const uint8_t *>(&this[1]);
  406. }
  407. };
  408. struct DrawListNextSubpassInstruction : DrawListInstruction {
  409. RDD::CommandBufferType command_buffer_type;
  410. };
  411. struct DrawListSetBlendConstantsInstruction : DrawListInstruction {
  412. Color color;
  413. };
  414. struct DrawListSetLineWidthInstruction : DrawListInstruction {
  415. float width;
  416. };
  417. struct DrawListSetScissorInstruction : DrawListInstruction {
  418. Rect2i rect;
  419. };
  420. struct DrawListSetViewportInstruction : DrawListInstruction {
  421. Rect2i rect;
  422. };
  423. struct DrawListUniformSetPrepareForUseInstruction : DrawListInstruction {
  424. RDD::UniformSetID uniform_set;
  425. RDD::ShaderID shader;
  426. uint32_t set_index = 0;
  427. };
  428. struct ComputeListBindPipelineInstruction : ComputeListInstruction {
  429. RDD::PipelineID pipeline;
  430. };
  431. struct ComputeListBindUniformSetInstruction : ComputeListInstruction {
  432. RDD::UniformSetID uniform_set;
  433. RDD::ShaderID shader;
  434. uint32_t set_index = 0;
  435. };
  436. struct ComputeListDispatchInstruction : ComputeListInstruction {
  437. uint32_t x_groups = 0;
  438. uint32_t y_groups = 0;
  439. uint32_t z_groups = 0;
  440. };
  441. struct ComputeListDispatchIndirectInstruction : ComputeListInstruction {
  442. RDD::BufferID buffer;
  443. uint32_t offset = 0;
  444. };
  445. struct ComputeListSetPushConstantInstruction : ComputeListInstruction {
  446. uint32_t size = 0;
  447. RDD::ShaderID shader;
  448. _FORCE_INLINE_ uint8_t *data() {
  449. return reinterpret_cast<uint8_t *>(&this[1]);
  450. }
  451. _FORCE_INLINE_ const uint8_t *data() const {
  452. return reinterpret_cast<const uint8_t *>(&this[1]);
  453. }
  454. };
  455. struct ComputeListUniformSetPrepareForUseInstruction : ComputeListInstruction {
  456. RDD::UniformSetID uniform_set;
  457. RDD::ShaderID shader;
  458. uint32_t set_index = 0;
  459. };
  460. struct BarrierGroup {
  461. BitField<RDD::PipelineStageBits> src_stages;
  462. BitField<RDD::PipelineStageBits> dst_stages;
  463. RDD::MemoryBarrier memory_barrier;
  464. LocalVector<RDD::TextureBarrier> normalization_barriers;
  465. LocalVector<RDD::TextureBarrier> transition_barriers;
  466. #if USE_BUFFER_BARRIERS
  467. LocalVector<RDD::BufferBarrier> buffer_barriers;
  468. #endif
  469. void clear() {
  470. src_stages.clear();
  471. dst_stages.clear();
  472. memory_barrier.src_access.clear();
  473. memory_barrier.dst_access.clear();
  474. normalization_barriers.clear();
  475. transition_barriers.clear();
  476. #if USE_BUFFER_BARRIERS
  477. buffer_barriers.clear();
  478. #endif
  479. }
  480. };
  481. struct SecondaryCommandBuffer {
  482. LocalVector<uint8_t> instruction_data;
  483. RDD::CommandBufferID command_buffer;
  484. RDD::CommandPoolID command_pool;
  485. RDD::RenderPassID render_pass;
  486. RDD::FramebufferID framebuffer;
  487. WorkerThreadPool::TaskID task;
  488. };
  489. struct Frame {
  490. TightLocalVector<SecondaryCommandBuffer> secondary_command_buffers;
  491. uint32_t secondary_command_buffers_used = 0;
  492. };
  493. RDD *driver = nullptr;
  494. RenderingContextDriver::Device device;
  495. int64_t tracking_frame = 0;
  496. LocalVector<uint8_t> command_data;
  497. LocalVector<uint32_t> command_data_offsets;
  498. LocalVector<RDD::TextureBarrier> command_normalization_barriers;
  499. LocalVector<RDD::TextureBarrier> command_transition_barriers;
  500. LocalVector<RDD::BufferBarrier> command_buffer_barriers;
  501. LocalVector<char> command_label_chars;
  502. LocalVector<Color> command_label_colors;
  503. LocalVector<uint32_t> command_label_offsets;
  504. int32_t command_label_index = -1;
  505. DrawInstructionList draw_instruction_list;
  506. ComputeInstructionList compute_instruction_list;
  507. uint32_t command_count = 0;
  508. uint32_t command_label_count = 0;
  509. LocalVector<RecordedCommandListNode> command_list_nodes;
  510. LocalVector<RecordedSliceListNode> read_slice_list_nodes;
  511. LocalVector<RecordedSliceListNode> write_slice_list_nodes;
  512. int32_t command_timestamp_index = -1;
  513. int32_t command_synchronization_index = -1;
  514. bool command_synchronization_pending = false;
  515. BarrierGroup barrier_group;
  516. bool driver_honors_barriers : 1;
  517. bool driver_clears_with_copy_engine : 1;
  518. WorkaroundsState workarounds_state;
  519. TightLocalVector<Frame> frames;
  520. uint32_t frame = 0;
  521. #ifdef DEV_ENABLED
  522. RBMap<ResourceTracker *, uint32_t> write_dependency_counters;
  523. #endif
  524. static bool _is_write_usage(ResourceUsage p_usage);
  525. static RDD::TextureLayout _usage_to_image_layout(ResourceUsage p_usage);
  526. static RDD::BarrierAccessBits _usage_to_access_bits(ResourceUsage p_usage);
  527. int32_t _add_to_command_list(int32_t p_command_index, int32_t p_list_index);
  528. void _add_adjacent_command(int32_t p_previous_command_index, int32_t p_command_index, RecordedCommand *r_command);
  529. int32_t _add_to_slice_read_list(int32_t p_command_index, Rect2i p_subresources, int32_t p_list_index);
  530. int32_t _add_to_write_list(int32_t p_command_index, Rect2i p_subresources, int32_t p_list_index);
  531. RecordedCommand *_allocate_command(uint32_t p_command_size, int32_t &r_command_index);
  532. DrawListInstruction *_allocate_draw_list_instruction(uint32_t p_instruction_size);
  533. ComputeListInstruction *_allocate_compute_list_instruction(uint32_t p_instruction_size);
  534. void _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);
  535. void _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);
  536. #if USE_BUFFER_BARRIERS
  537. void _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);
  538. #endif
  539. void _run_compute_list_command(RDD::CommandBufferID p_command_buffer, const uint8_t *p_instruction_data, uint32_t p_instruction_data_size);
  540. void _run_draw_list_command(RDD::CommandBufferID p_command_buffer, const uint8_t *p_instruction_data, uint32_t p_instruction_data_size);
  541. void _run_secondary_command_buffer_task(const SecondaryCommandBuffer *p_secondary);
  542. void _wait_for_secondary_command_buffer_tasks();
  543. void _run_render_commands(int32_t p_level, const RecordedCommandSort *p_sorted_commands, uint32_t p_sorted_commands_count, RDD::CommandBufferID &r_command_buffer, CommandBufferPool &r_command_buffer_pool, int32_t &r_current_label_index, int32_t &r_current_label_level);
  544. void _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);
  545. void _boost_priority_for_render_commands(RecordedCommandSort *p_sorted_commands, uint32_t p_sorted_commands_count, uint32_t &r_boosted_priority);
  546. void _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);
  547. void _print_render_commands(const RecordedCommandSort *p_sorted_commands, uint32_t p_sorted_commands_count);
  548. void _print_draw_list(const uint8_t *p_instruction_data, uint32_t p_instruction_data_size);
  549. void _print_compute_list(const uint8_t *p_instruction_data, uint32_t p_instruction_data_size);
  550. public:
  551. RenderingDeviceGraph();
  552. ~RenderingDeviceGraph();
  553. void initialize(RDD *p_driver, RenderingContextDriver::Device p_device, uint32_t p_frame_count, RDD::CommandQueueFamilyID p_secondary_command_queue_family, uint32_t p_secondary_command_buffers_per_frame);
  554. void finalize();
  555. void begin();
  556. void add_buffer_clear(RDD::BufferID p_dst, ResourceTracker *p_dst_tracker, uint32_t p_offset, uint32_t p_size);
  557. void add_buffer_copy(RDD::BufferID p_src, ResourceTracker *p_src_tracker, RDD::BufferID p_dst, ResourceTracker *p_dst_tracker, RDD::BufferCopyRegion p_region);
  558. void add_buffer_get_data(RDD::BufferID p_src, ResourceTracker *p_src_tracker, RDD::BufferID p_dst, RDD::BufferCopyRegion p_region);
  559. void add_buffer_update(RDD::BufferID p_dst, ResourceTracker *p_dst_tracker, VectorView<RecordedBufferCopy> p_buffer_copies);
  560. void add_compute_list_begin();
  561. void add_compute_list_bind_pipeline(RDD::PipelineID p_pipeline);
  562. void add_compute_list_bind_uniform_set(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index);
  563. void add_compute_list_dispatch(uint32_t p_x_groups, uint32_t p_y_groups, uint32_t p_z_groups);
  564. void add_compute_list_dispatch_indirect(RDD::BufferID p_buffer, uint32_t p_offset);
  565. void add_compute_list_set_push_constant(RDD::ShaderID p_shader, const void *p_data, uint32_t p_data_size);
  566. void add_compute_list_uniform_set_prepare_for_use(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index);
  567. void add_compute_list_usage(ResourceTracker *p_tracker, ResourceUsage p_usage);
  568. void add_compute_list_usages(VectorView<ResourceTracker *> p_trackers, VectorView<ResourceUsage> p_usages);
  569. void add_compute_list_end();
  570. void 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);
  571. void add_draw_list_bind_index_buffer(RDD::BufferID p_buffer, RDD::IndexBufferFormat p_format, uint32_t p_offset);
  572. void add_draw_list_bind_pipeline(RDD::PipelineID p_pipeline, BitField<RDD::PipelineStageBits> p_pipeline_stage_bits);
  573. void add_draw_list_bind_uniform_set(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index);
  574. void add_draw_list_bind_vertex_buffers(VectorView<RDD::BufferID> p_vertex_buffers, VectorView<uint64_t> p_vertex_buffer_offsets);
  575. void add_draw_list_clear_attachments(VectorView<RDD::AttachmentClear> p_attachments_clear, VectorView<Rect2i> p_attachments_clear_rect);
  576. void add_draw_list_draw(uint32_t p_vertex_count, uint32_t p_instance_count);
  577. void add_draw_list_draw_indexed(uint32_t p_index_count, uint32_t p_instance_count, uint32_t p_first_index);
  578. void add_draw_list_execute_commands(RDD::CommandBufferID p_command_buffer);
  579. void add_draw_list_next_subpass(RDD::CommandBufferType p_command_buffer_type);
  580. void add_draw_list_set_blend_constants(const Color &p_color);
  581. void add_draw_list_set_line_width(float p_width);
  582. void add_draw_list_set_push_constant(RDD::ShaderID p_shader, const void *p_data, uint32_t p_data_size);
  583. void add_draw_list_set_scissor(Rect2i p_rect);
  584. void add_draw_list_set_viewport(Rect2i p_rect);
  585. void add_draw_list_uniform_set_prepare_for_use(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index);
  586. void add_draw_list_usage(ResourceTracker *p_tracker, ResourceUsage p_usage);
  587. void add_draw_list_usages(VectorView<ResourceTracker *> p_trackers, VectorView<ResourceUsage> p_usages);
  588. void add_draw_list_end();
  589. void add_texture_clear(RDD::TextureID p_dst, ResourceTracker *p_dst_tracker, const Color &p_color, const RDD::TextureSubresourceRange &p_range);
  590. void add_texture_copy(RDD::TextureID p_src, ResourceTracker *p_src_tracker, RDD::TextureID p_dst, ResourceTracker *p_dst_tracker, VectorView<RDD::TextureCopyRegion> p_texture_copy_regions);
  591. void add_texture_get_data(RDD::TextureID p_src, ResourceTracker *p_src_tracker, RDD::BufferID p_dst, VectorView<RDD::BufferTextureCopyRegion> p_buffer_texture_copy_regions, ResourceTracker *p_dst_tracker = nullptr);
  592. void 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);
  593. void add_texture_update(RDD::TextureID p_dst, ResourceTracker *p_dst_tracker, VectorView<RecordedBufferToTextureCopy> p_buffer_copies, VectorView<ResourceTracker *> p_buffer_trackers = VectorView<ResourceTracker *>());
  594. void add_capture_timestamp(RDD::QueryPoolID p_query_pool, uint32_t p_index);
  595. void add_synchronization();
  596. void begin_label(const String &p_label_name, const Color &p_color);
  597. void end_label();
  598. void end(bool p_reorder_commands, bool p_full_barriers, RDD::CommandBufferID &r_command_buffer, CommandBufferPool &r_command_buffer_pool);
  599. static ResourceTracker *resource_tracker_create();
  600. static void resource_tracker_free(ResourceTracker *tracker);
  601. };
  602. using RDG = RenderingDeviceGraph;
  603. #endif // RENDERING_DEVICE_GRAPH_H