rendering_device_graph.h 32 KB

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