rendering_device_graph.h 28 KB

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