rendering_device_graph.h 33 KB

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