rendering_device_graph.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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_SETS,
  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_SETS,
  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. bool is_discardable = false;
  166. _FORCE_INLINE_ void reset_if_outdated(int64_t new_command_frame) {
  167. if (new_command_frame != command_frame) {
  168. command_frame = new_command_frame;
  169. previous_frame_stages = current_frame_stages;
  170. current_frame_stages.clear();
  171. read_full_command_list_index = -1;
  172. read_slice_command_list_index = -1;
  173. write_command_or_list_index = -1;
  174. draw_list_index = -1;
  175. compute_list_index = -1;
  176. texture_slice_command_index = -1;
  177. write_command_list_enabled = false;
  178. }
  179. }
  180. };
  181. typedef RDD::RenderPassID (*RenderPassCreationFunction)(RenderingDeviceDriver *p_driver, VectorView<RDD::AttachmentLoadOp> p_load_ops, VectorView<RDD::AttachmentStoreOp> p_store_ops, void *p_user_data);
  182. struct FramebufferStorage {
  183. RDD::FramebufferID framebuffer;
  184. RDD::RenderPassID render_pass;
  185. };
  186. struct FramebufferCache {
  187. uint32_t width = 0;
  188. uint32_t height = 0;
  189. LocalVector<RDD::TextureID> textures;
  190. LocalVector<ResourceTracker *> trackers;
  191. HashMap<uint64_t, FramebufferStorage> storage_map;
  192. void *render_pass_creation_user_data = nullptr;
  193. };
  194. struct CommandBufferPool {
  195. // Provided by RenderingDevice.
  196. RDD::CommandPoolID pool;
  197. // Created internally by RenderingDeviceGraph.
  198. LocalVector<RDD::CommandBufferID> buffers;
  199. LocalVector<RDD::SemaphoreID> semaphores;
  200. uint32_t buffers_used = 0;
  201. };
  202. struct WorkaroundsState {
  203. bool draw_list_found = false;
  204. };
  205. enum AttachmentOperation {
  206. // Loads or ignores if the attachment is discardable.
  207. ATTACHMENT_OPERATION_DEFAULT,
  208. // Clear the attachment to a value.
  209. ATTACHMENT_OPERATION_CLEAR,
  210. // Ignore any contents from the attachment.
  211. ATTACHMENT_OPERATION_IGNORE,
  212. };
  213. private:
  214. struct InstructionList {
  215. LocalVector<uint8_t> data;
  216. LocalVector<ResourceTracker *> command_trackers;
  217. LocalVector<ResourceUsage> command_tracker_usages;
  218. BitField<RDD::PipelineStageBits> stages;
  219. int32_t index = 0;
  220. void clear() {
  221. data.clear();
  222. command_trackers.clear();
  223. command_tracker_usages.clear();
  224. stages.clear();
  225. }
  226. };
  227. struct ComputeInstructionList : InstructionList {
  228. #if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
  229. uint32_t breadcrumb;
  230. #endif
  231. };
  232. struct DrawInstructionList : InstructionList {
  233. FramebufferCache *framebuffer_cache = nullptr;
  234. RDD::RenderPassID render_pass;
  235. RDD::FramebufferID framebuffer;
  236. Rect2i region;
  237. LocalVector<AttachmentOperation> attachment_operations;
  238. LocalVector<RDD::RenderPassClearValue> attachment_clear_values;
  239. #if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
  240. uint32_t breadcrumb;
  241. #endif
  242. bool split_cmd_buffer = false;
  243. };
  244. struct RecordedCommandSort {
  245. uint32_t level = 0;
  246. uint32_t priority = 0;
  247. int32_t index = -1;
  248. RecordedCommandSort() = default;
  249. bool operator<(const RecordedCommandSort &p_other) const {
  250. if (level < p_other.level) {
  251. return true;
  252. } else if (level > p_other.level) {
  253. return false;
  254. }
  255. if (priority < p_other.priority) {
  256. return true;
  257. } else if (priority > p_other.priority) {
  258. return false;
  259. }
  260. return index < p_other.index;
  261. }
  262. };
  263. struct RecordedCommandListNode {
  264. int32_t command_index = -1;
  265. int32_t next_list_index = -1;
  266. };
  267. struct RecordedSliceListNode {
  268. int32_t command_index = -1;
  269. int32_t next_list_index = -1;
  270. Rect2i subresources;
  271. bool partial_coverage = false;
  272. };
  273. struct RecordedBufferClearCommand : RecordedCommand {
  274. RDD::BufferID buffer;
  275. uint32_t offset = 0;
  276. uint32_t size = 0;
  277. };
  278. struct RecordedBufferCopyCommand : RecordedCommand {
  279. RDD::BufferID source;
  280. RDD::BufferID destination;
  281. RDD::BufferCopyRegion region;
  282. };
  283. struct RecordedBufferGetDataCommand : RecordedCommand {
  284. RDD::BufferID source;
  285. RDD::BufferID destination;
  286. RDD::BufferCopyRegion region;
  287. };
  288. struct RecordedBufferUpdateCommand : RecordedCommand {
  289. RDD::BufferID destination;
  290. uint32_t buffer_copies_count = 0;
  291. _FORCE_INLINE_ RecordedBufferCopy *buffer_copies() {
  292. return reinterpret_cast<RecordedBufferCopy *>(&this[1]);
  293. }
  294. _FORCE_INLINE_ const RecordedBufferCopy *buffer_copies() const {
  295. return reinterpret_cast<const RecordedBufferCopy *>(&this[1]);
  296. }
  297. };
  298. struct RecordedComputeListCommand : RecordedCommand {
  299. uint32_t instruction_data_size = 0;
  300. uint32_t breadcrumb = 0;
  301. _FORCE_INLINE_ uint8_t *instruction_data() {
  302. return reinterpret_cast<uint8_t *>(&this[1]);
  303. }
  304. _FORCE_INLINE_ const uint8_t *instruction_data() const {
  305. return reinterpret_cast<const uint8_t *>(&this[1]);
  306. }
  307. };
  308. struct RecordedDrawListCommand : RecordedCommand {
  309. FramebufferCache *framebuffer_cache = nullptr;
  310. RDD::FramebufferID framebuffer;
  311. RDD::RenderPassID render_pass;
  312. uint32_t instruction_data_size = 0;
  313. RDD::CommandBufferType command_buffer_type;
  314. Rect2i region;
  315. uint32_t clear_values_count = 0;
  316. uint32_t trackers_count = 0;
  317. #if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
  318. uint32_t breadcrumb = 0;
  319. #endif
  320. bool split_cmd_buffer = false;
  321. _FORCE_INLINE_ RDD::RenderPassClearValue *clear_values() {
  322. return reinterpret_cast<RDD::RenderPassClearValue *>(&this[1]);
  323. }
  324. _FORCE_INLINE_ const RDD::RenderPassClearValue *clear_values() const {
  325. return reinterpret_cast<const RDD::RenderPassClearValue *>(&this[1]);
  326. }
  327. _FORCE_INLINE_ ResourceTracker **trackers() {
  328. return reinterpret_cast<ResourceTracker **>(&clear_values()[clear_values_count]);
  329. }
  330. _FORCE_INLINE_ ResourceTracker *const *trackers() const {
  331. return reinterpret_cast<ResourceTracker *const *>(&clear_values()[clear_values_count]);
  332. }
  333. _FORCE_INLINE_ RDD::AttachmentLoadOp *load_ops() {
  334. return reinterpret_cast<RDD::AttachmentLoadOp *>(&trackers()[trackers_count]);
  335. }
  336. _FORCE_INLINE_ const RDD::AttachmentLoadOp *load_ops() const {
  337. return reinterpret_cast<const RDD::AttachmentLoadOp *>(&trackers()[trackers_count]);
  338. }
  339. _FORCE_INLINE_ RDD::AttachmentStoreOp *store_ops() {
  340. return reinterpret_cast<RDD::AttachmentStoreOp *>(&load_ops()[trackers_count]);
  341. }
  342. _FORCE_INLINE_ const RDD::AttachmentStoreOp *store_ops() const {
  343. return reinterpret_cast<const RDD::AttachmentStoreOp *>(&load_ops()[trackers_count]);
  344. }
  345. _FORCE_INLINE_ uint8_t *instruction_data() {
  346. return reinterpret_cast<uint8_t *>(&store_ops()[trackers_count]);
  347. }
  348. _FORCE_INLINE_ const uint8_t *instruction_data() const {
  349. return reinterpret_cast<const uint8_t *>(&store_ops()[trackers_count]);
  350. }
  351. };
  352. struct RecordedTextureClearCommand : RecordedCommand {
  353. RDD::TextureID texture;
  354. RDD::TextureSubresourceRange range;
  355. Color color;
  356. };
  357. struct RecordedTextureCopyCommand : RecordedCommand {
  358. RDD::TextureID from_texture;
  359. RDD::TextureID to_texture;
  360. uint32_t texture_copy_regions_count = 0;
  361. _FORCE_INLINE_ RDD::TextureCopyRegion *texture_copy_regions() {
  362. return reinterpret_cast<RDD::TextureCopyRegion *>(&this[1]);
  363. }
  364. _FORCE_INLINE_ const RDD::TextureCopyRegion *texture_copy_regions() const {
  365. return reinterpret_cast<const RDD::TextureCopyRegion *>(&this[1]);
  366. }
  367. };
  368. struct RecordedTextureGetDataCommand : RecordedCommand {
  369. RDD::TextureID from_texture;
  370. RDD::BufferID to_buffer;
  371. uint32_t buffer_texture_copy_regions_count = 0;
  372. _FORCE_INLINE_ RDD::BufferTextureCopyRegion *buffer_texture_copy_regions() {
  373. return reinterpret_cast<RDD::BufferTextureCopyRegion *>(&this[1]);
  374. }
  375. _FORCE_INLINE_ const RDD::BufferTextureCopyRegion *buffer_texture_copy_regions() const {
  376. return reinterpret_cast<const RDD::BufferTextureCopyRegion *>(&this[1]);
  377. }
  378. };
  379. struct RecordedTextureResolveCommand : RecordedCommand {
  380. RDD::TextureID from_texture;
  381. RDD::TextureID to_texture;
  382. uint32_t src_layer = 0;
  383. uint32_t src_mipmap = 0;
  384. uint32_t dst_layer = 0;
  385. uint32_t dst_mipmap = 0;
  386. };
  387. struct RecordedTextureUpdateCommand : RecordedCommand {
  388. RDD::TextureID to_texture;
  389. uint32_t buffer_to_texture_copies_count = 0;
  390. _FORCE_INLINE_ RecordedBufferToTextureCopy *buffer_to_texture_copies() {
  391. return reinterpret_cast<RecordedBufferToTextureCopy *>(&this[1]);
  392. }
  393. _FORCE_INLINE_ const RecordedBufferToTextureCopy *buffer_to_texture_copies() const {
  394. return reinterpret_cast<const RecordedBufferToTextureCopy *>(&this[1]);
  395. }
  396. };
  397. struct RecordedCaptureTimestampCommand : RecordedCommand {
  398. RDD::QueryPoolID pool;
  399. uint32_t index = 0;
  400. };
  401. struct DrawListBindIndexBufferInstruction : DrawListInstruction {
  402. RDD::BufferID buffer;
  403. RenderingDeviceCommons::IndexBufferFormat format;
  404. uint32_t offset = 0;
  405. };
  406. struct DrawListBindPipelineInstruction : DrawListInstruction {
  407. RDD::PipelineID pipeline;
  408. };
  409. struct DrawListBindUniformSetsInstruction : DrawListInstruction {
  410. RDD::ShaderID shader;
  411. uint32_t first_set_index = 0;
  412. uint32_t set_count = 0;
  413. _FORCE_INLINE_ RDD::UniformSetID *uniform_set_ids() {
  414. return reinterpret_cast<RDD::UniformSetID *>(&this[1]);
  415. }
  416. _FORCE_INLINE_ const RDD::UniformSetID *uniform_set_ids() const {
  417. return reinterpret_cast<const RDD::UniformSetID *>(&this[1]);
  418. }
  419. };
  420. struct DrawListBindVertexBuffersInstruction : DrawListInstruction {
  421. uint32_t vertex_buffers_count = 0;
  422. _FORCE_INLINE_ RDD::BufferID *vertex_buffers() {
  423. return reinterpret_cast<RDD::BufferID *>(&this[1]);
  424. }
  425. _FORCE_INLINE_ const RDD::BufferID *vertex_buffers() const {
  426. return reinterpret_cast<const RDD::BufferID *>(&this[1]);
  427. }
  428. _FORCE_INLINE_ uint64_t *vertex_buffer_offsets() {
  429. return reinterpret_cast<uint64_t *>(&vertex_buffers()[vertex_buffers_count]);
  430. }
  431. _FORCE_INLINE_ const uint64_t *vertex_buffer_offsets() const {
  432. return reinterpret_cast<const uint64_t *>(&vertex_buffers()[vertex_buffers_count]);
  433. }
  434. };
  435. struct DrawListClearAttachmentsInstruction : DrawListInstruction {
  436. uint32_t attachments_clear_count = 0;
  437. uint32_t attachments_clear_rect_count = 0;
  438. _FORCE_INLINE_ RDD::AttachmentClear *attachments_clear() {
  439. return reinterpret_cast<RDD::AttachmentClear *>(&this[1]);
  440. }
  441. _FORCE_INLINE_ const RDD::AttachmentClear *attachments_clear() const {
  442. return reinterpret_cast<const RDD::AttachmentClear *>(&this[1]);
  443. }
  444. _FORCE_INLINE_ Rect2i *attachments_clear_rect() {
  445. return reinterpret_cast<Rect2i *>(&attachments_clear()[attachments_clear_count]);
  446. }
  447. _FORCE_INLINE_ const Rect2i *attachments_clear_rect() const {
  448. return reinterpret_cast<const Rect2i *>(&attachments_clear()[attachments_clear_count]);
  449. }
  450. };
  451. struct DrawListDrawInstruction : DrawListInstruction {
  452. uint32_t vertex_count = 0;
  453. uint32_t instance_count = 0;
  454. };
  455. struct DrawListDrawIndexedInstruction : DrawListInstruction {
  456. uint32_t index_count = 0;
  457. uint32_t instance_count = 0;
  458. uint32_t first_index = 0;
  459. };
  460. struct DrawListDrawIndirectInstruction : DrawListInstruction {
  461. RDD::BufferID buffer;
  462. uint32_t offset = 0;
  463. uint32_t draw_count = 0;
  464. uint32_t stride = 0;
  465. };
  466. struct DrawListDrawIndexedIndirectInstruction : DrawListInstruction {
  467. RDD::BufferID buffer;
  468. uint32_t offset = 0;
  469. uint32_t draw_count = 0;
  470. uint32_t stride = 0;
  471. };
  472. struct DrawListEndRenderPassInstruction : DrawListInstruction {
  473. // No contents.
  474. };
  475. struct DrawListExecuteCommandsInstruction : DrawListInstruction {
  476. RDD::CommandBufferID command_buffer;
  477. };
  478. struct DrawListSetPushConstantInstruction : DrawListInstruction {
  479. uint32_t size = 0;
  480. RDD::ShaderID shader;
  481. _FORCE_INLINE_ uint8_t *data() {
  482. return reinterpret_cast<uint8_t *>(&this[1]);
  483. }
  484. _FORCE_INLINE_ const uint8_t *data() const {
  485. return reinterpret_cast<const uint8_t *>(&this[1]);
  486. }
  487. };
  488. struct DrawListNextSubpassInstruction : DrawListInstruction {
  489. RDD::CommandBufferType command_buffer_type;
  490. };
  491. struct DrawListSetBlendConstantsInstruction : DrawListInstruction {
  492. Color color;
  493. };
  494. struct DrawListSetLineWidthInstruction : DrawListInstruction {
  495. float width;
  496. };
  497. struct DrawListSetScissorInstruction : DrawListInstruction {
  498. Rect2i rect;
  499. };
  500. struct DrawListSetViewportInstruction : DrawListInstruction {
  501. Rect2i rect;
  502. };
  503. struct DrawListUniformSetPrepareForUseInstruction : DrawListInstruction {
  504. RDD::UniformSetID uniform_set;
  505. RDD::ShaderID shader;
  506. uint32_t set_index = 0;
  507. };
  508. struct ComputeListBindPipelineInstruction : ComputeListInstruction {
  509. RDD::PipelineID pipeline;
  510. };
  511. struct ComputeListBindUniformSetsInstruction : ComputeListInstruction {
  512. RDD::ShaderID shader;
  513. uint32_t first_set_index = 0;
  514. uint32_t set_count = 0;
  515. _FORCE_INLINE_ RDD::UniformSetID *uniform_set_ids() {
  516. return reinterpret_cast<RDD::UniformSetID *>(&this[1]);
  517. }
  518. _FORCE_INLINE_ const RDD::UniformSetID *uniform_set_ids() const {
  519. return reinterpret_cast<const RDD::UniformSetID *>(&this[1]);
  520. }
  521. };
  522. struct ComputeListDispatchInstruction : ComputeListInstruction {
  523. uint32_t x_groups = 0;
  524. uint32_t y_groups = 0;
  525. uint32_t z_groups = 0;
  526. };
  527. struct ComputeListDispatchIndirectInstruction : ComputeListInstruction {
  528. RDD::BufferID buffer;
  529. uint32_t offset = 0;
  530. };
  531. struct ComputeListSetPushConstantInstruction : ComputeListInstruction {
  532. uint32_t size = 0;
  533. RDD::ShaderID shader;
  534. _FORCE_INLINE_ uint8_t *data() {
  535. return reinterpret_cast<uint8_t *>(&this[1]);
  536. }
  537. _FORCE_INLINE_ const uint8_t *data() const {
  538. return reinterpret_cast<const uint8_t *>(&this[1]);
  539. }
  540. };
  541. struct ComputeListUniformSetPrepareForUseInstruction : ComputeListInstruction {
  542. RDD::UniformSetID uniform_set;
  543. RDD::ShaderID shader;
  544. uint32_t set_index = 0;
  545. };
  546. struct BarrierGroup {
  547. BitField<RDD::PipelineStageBits> src_stages;
  548. BitField<RDD::PipelineStageBits> dst_stages;
  549. RDD::MemoryBarrier memory_barrier;
  550. LocalVector<RDD::TextureBarrier> normalization_barriers;
  551. LocalVector<RDD::TextureBarrier> transition_barriers;
  552. #if USE_BUFFER_BARRIERS
  553. LocalVector<RDD::BufferBarrier> buffer_barriers;
  554. #endif
  555. void clear() {
  556. src_stages.clear();
  557. dst_stages.clear();
  558. memory_barrier.src_access.clear();
  559. memory_barrier.dst_access.clear();
  560. normalization_barriers.clear();
  561. transition_barriers.clear();
  562. #if USE_BUFFER_BARRIERS
  563. buffer_barriers.clear();
  564. #endif
  565. }
  566. };
  567. struct SecondaryCommandBuffer {
  568. LocalVector<uint8_t> instruction_data;
  569. RDD::CommandBufferID command_buffer;
  570. RDD::CommandPoolID command_pool;
  571. RDD::RenderPassID render_pass;
  572. RDD::FramebufferID framebuffer;
  573. WorkerThreadPool::TaskID task;
  574. };
  575. struct Frame {
  576. TightLocalVector<SecondaryCommandBuffer> secondary_command_buffers;
  577. uint32_t secondary_command_buffers_used = 0;
  578. };
  579. RDD *driver = nullptr;
  580. RenderingContextDriver::Device device;
  581. RenderPassCreationFunction render_pass_creation_function = nullptr;
  582. int64_t tracking_frame = 0;
  583. LocalVector<uint8_t> command_data;
  584. LocalVector<uint32_t> command_data_offsets;
  585. LocalVector<RDD::TextureBarrier> command_normalization_barriers;
  586. LocalVector<RDD::TextureBarrier> command_transition_barriers;
  587. LocalVector<RDD::BufferBarrier> command_buffer_barriers;
  588. LocalVector<char> command_label_chars;
  589. LocalVector<Color> command_label_colors;
  590. LocalVector<uint32_t> command_label_offsets;
  591. int32_t command_label_index = -1;
  592. DrawInstructionList draw_instruction_list;
  593. ComputeInstructionList compute_instruction_list;
  594. uint32_t command_count = 0;
  595. uint32_t command_label_count = 0;
  596. LocalVector<RecordedCommandListNode> command_list_nodes;
  597. LocalVector<RecordedSliceListNode> read_slice_list_nodes;
  598. LocalVector<RecordedSliceListNode> write_slice_list_nodes;
  599. int32_t command_timestamp_index = -1;
  600. int32_t command_synchronization_index = -1;
  601. bool command_synchronization_pending = false;
  602. BarrierGroup barrier_group;
  603. bool driver_honors_barriers : 1;
  604. bool driver_clears_with_copy_engine : 1;
  605. bool driver_buffers_require_transitions : 1;
  606. WorkaroundsState workarounds_state;
  607. TightLocalVector<Frame> frames;
  608. uint32_t frame = 0;
  609. #ifdef DEV_ENABLED
  610. RBMap<ResourceTracker *, uint32_t> write_dependency_counters;
  611. #endif
  612. static bool _is_write_usage(ResourceUsage p_usage);
  613. static RDD::TextureLayout _usage_to_image_layout(ResourceUsage p_usage);
  614. static RDD::BarrierAccessBits _usage_to_access_bits(ResourceUsage p_usage);
  615. bool _check_command_intersection(ResourceTracker *p_resource_tracker, int32_t p_previous_command_index, int32_t p_command_index) const;
  616. bool _check_command_partial_coverage(ResourceTracker *p_resource_tracker, int32_t p_command_index) const;
  617. int32_t _add_to_command_list(int32_t p_command_index, int32_t p_list_index);
  618. void _add_adjacent_command(int32_t p_previous_command_index, int32_t p_command_index, RecordedCommand *r_command);
  619. int32_t _add_to_slice_read_list(int32_t p_command_index, Rect2i p_subresources, int32_t p_list_index);
  620. int32_t _add_to_write_list(int32_t p_command_index, Rect2i p_subresources, int32_t p_list_index, bool p_partial_coverage);
  621. RecordedCommand *_allocate_command(uint32_t p_command_size, int32_t &r_command_index);
  622. DrawListInstruction *_allocate_draw_list_instruction(uint32_t p_instruction_size);
  623. ComputeListInstruction *_allocate_compute_list_instruction(uint32_t p_instruction_size);
  624. void _check_discardable_attachment_dependency(ResourceTracker *p_resource_tracker, int32_t p_previous_command_index, int32_t p_command_index);
  625. 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);
  626. 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);
  627. #if USE_BUFFER_BARRIERS
  628. 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);
  629. #endif
  630. void _run_compute_list_command(RDD::CommandBufferID p_command_buffer, const uint8_t *p_instruction_data, uint32_t p_instruction_data_size);
  631. void _get_draw_list_render_pass_and_framebuffer(const RecordedDrawListCommand *p_draw_list_command, RDD::RenderPassID &r_render_pass, RDD::FramebufferID &r_framebuffer);
  632. void _run_draw_list_command(RDD::CommandBufferID p_command_buffer, const uint8_t *p_instruction_data, uint32_t p_instruction_data_size);
  633. void _add_draw_list_begin(FramebufferCache *p_framebuffer_cache, RDD::RenderPassID p_render_pass, RDD::FramebufferID p_framebuffer, Rect2i p_region, VectorView<AttachmentOperation> p_attachment_operations, VectorView<RDD::RenderPassClearValue> p_attachment_clear_values, bool p_uses_color, bool p_uses_depth, uint32_t p_breadcrumb, bool p_split_cmd_buffer);
  634. void _run_secondary_command_buffer_task(const SecondaryCommandBuffer *p_secondary);
  635. void _wait_for_secondary_command_buffer_tasks();
  636. 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);
  637. 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);
  638. void _boost_priority_for_render_commands(RecordedCommandSort *p_sorted_commands, uint32_t p_sorted_commands_count, uint32_t &r_boosted_priority);
  639. 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);
  640. void _print_render_commands(const RecordedCommandSort *p_sorted_commands, uint32_t p_sorted_commands_count);
  641. void _print_draw_list(const uint8_t *p_instruction_data, uint32_t p_instruction_data_size);
  642. void _print_compute_list(const uint8_t *p_instruction_data, uint32_t p_instruction_data_size);
  643. public:
  644. RenderingDeviceGraph();
  645. ~RenderingDeviceGraph();
  646. void initialize(RDD *p_driver, RenderingContextDriver::Device p_device, RenderPassCreationFunction p_render_pass_creation_function, uint32_t p_frame_count, RDD::CommandQueueFamilyID p_secondary_command_queue_family, uint32_t p_secondary_command_buffers_per_frame);
  647. void finalize();
  648. void begin();
  649. void add_buffer_clear(RDD::BufferID p_dst, ResourceTracker *p_dst_tracker, uint32_t p_offset, uint32_t p_size);
  650. void add_buffer_copy(RDD::BufferID p_src, ResourceTracker *p_src_tracker, RDD::BufferID p_dst, ResourceTracker *p_dst_tracker, RDD::BufferCopyRegion p_region);
  651. void add_buffer_get_data(RDD::BufferID p_src, ResourceTracker *p_src_tracker, RDD::BufferID p_dst, RDD::BufferCopyRegion p_region);
  652. void add_buffer_update(RDD::BufferID p_dst, ResourceTracker *p_dst_tracker, VectorView<RecordedBufferCopy> p_buffer_copies);
  653. void add_compute_list_begin(RDD::BreadcrumbMarker p_phase = RDD::BreadcrumbMarker::NONE, uint32_t p_breadcrumb_data = 0);
  654. void add_compute_list_bind_pipeline(RDD::PipelineID p_pipeline);
  655. void add_compute_list_bind_uniform_set(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index);
  656. void add_compute_list_bind_uniform_sets(RDD::ShaderID p_shader, VectorView<RDD::UniformSetID> p_uniform_set, uint32_t p_first_set_index, uint32_t p_set_count);
  657. void add_compute_list_dispatch(uint32_t p_x_groups, uint32_t p_y_groups, uint32_t p_z_groups);
  658. void add_compute_list_dispatch_indirect(RDD::BufferID p_buffer, uint32_t p_offset);
  659. void add_compute_list_set_push_constant(RDD::ShaderID p_shader, const void *p_data, uint32_t p_data_size);
  660. void add_compute_list_uniform_set_prepare_for_use(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index);
  661. void add_compute_list_usage(ResourceTracker *p_tracker, ResourceUsage p_usage);
  662. void add_compute_list_usages(VectorView<ResourceTracker *> p_trackers, VectorView<ResourceUsage> p_usages);
  663. void add_compute_list_end();
  664. void add_draw_list_begin(FramebufferCache *p_framebuffer_cache, Rect2i p_region, VectorView<AttachmentOperation> p_attachment_operations, VectorView<RDD::RenderPassClearValue> p_attachment_clear_values, bool p_uses_color, bool p_uses_depth, uint32_t p_breadcrumb = 0, bool p_split_cmd_buffer = false);
  665. void add_draw_list_begin(RDD::RenderPassID p_render_pass, RDD::FramebufferID p_framebuffer, Rect2i p_region, VectorView<AttachmentOperation> p_attachment_operations, VectorView<RDD::RenderPassClearValue> p_attachment_clear_values, bool p_uses_color, bool p_uses_depth, uint32_t p_breadcrumb = 0, bool p_split_cmd_buffer = false);
  666. void add_draw_list_bind_index_buffer(RDD::BufferID p_buffer, RDD::IndexBufferFormat p_format, uint32_t p_offset);
  667. void add_draw_list_bind_pipeline(RDD::PipelineID p_pipeline, BitField<RDD::PipelineStageBits> p_pipeline_stage_bits);
  668. void add_draw_list_bind_uniform_set(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index);
  669. void add_draw_list_bind_uniform_sets(RDD::ShaderID p_shader, VectorView<RDD::UniformSetID> p_uniform_set, uint32_t p_first_index, uint32_t p_set_count);
  670. void add_draw_list_bind_vertex_buffers(VectorView<RDD::BufferID> p_vertex_buffers, VectorView<uint64_t> p_vertex_buffer_offsets);
  671. void add_draw_list_clear_attachments(VectorView<RDD::AttachmentClear> p_attachments_clear, VectorView<Rect2i> p_attachments_clear_rect);
  672. void add_draw_list_draw(uint32_t p_vertex_count, uint32_t p_instance_count);
  673. void add_draw_list_draw_indexed(uint32_t p_index_count, uint32_t p_instance_count, uint32_t p_first_index);
  674. void add_draw_list_draw_indirect(RDD::BufferID p_buffer, uint32_t p_offset, uint32_t p_draw_count, uint32_t p_stride);
  675. void add_draw_list_draw_indexed_indirect(RDD::BufferID p_buffer, uint32_t p_offset, uint32_t p_draw_count, uint32_t p_stride);
  676. void add_draw_list_execute_commands(RDD::CommandBufferID p_command_buffer);
  677. void add_draw_list_next_subpass(RDD::CommandBufferType p_command_buffer_type);
  678. void add_draw_list_set_blend_constants(const Color &p_color);
  679. void add_draw_list_set_line_width(float p_width);
  680. void add_draw_list_set_push_constant(RDD::ShaderID p_shader, const void *p_data, uint32_t p_data_size);
  681. void add_draw_list_set_scissor(Rect2i p_rect);
  682. void add_draw_list_set_viewport(Rect2i p_rect);
  683. void add_draw_list_uniform_set_prepare_for_use(RDD::ShaderID p_shader, RDD::UniformSetID p_uniform_set, uint32_t set_index);
  684. void add_draw_list_usage(ResourceTracker *p_tracker, ResourceUsage p_usage);
  685. void add_draw_list_usages(VectorView<ResourceTracker *> p_trackers, VectorView<ResourceUsage> p_usages);
  686. void add_draw_list_end();
  687. void add_texture_clear(RDD::TextureID p_dst, ResourceTracker *p_dst_tracker, const Color &p_color, const RDD::TextureSubresourceRange &p_range);
  688. 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);
  689. 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);
  690. 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);
  691. void add_texture_update(RDD::TextureID p_dst, ResourceTracker *p_dst_tracker, VectorView<RecordedBufferToTextureCopy> p_buffer_copies, VectorView<ResourceTracker *> p_buffer_trackers = VectorView<ResourceTracker *>());
  692. void add_capture_timestamp(RDD::QueryPoolID p_query_pool, uint32_t p_index);
  693. void add_synchronization();
  694. void begin_label(const String &p_label_name, const Color &p_color);
  695. void end_label();
  696. void end(bool p_reorder_commands, bool p_full_barriers, RDD::CommandBufferID &r_command_buffer, CommandBufferPool &r_command_buffer_pool);
  697. static ResourceTracker *resource_tracker_create();
  698. static void resource_tracker_free(ResourceTracker *p_tracker);
  699. static FramebufferCache *framebuffer_cache_create();
  700. static void framebuffer_cache_free(RDD *p_driver, FramebufferCache *p_cache);
  701. };
  702. using RDG = RenderingDeviceGraph;
  703. #endif // RENDERING_DEVICE_GRAPH_H