rendering_device_graph.h 33 KB

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