rendering_device_driver_d3d12.h 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. /**************************************************************************/
  2. /* rendering_device_driver_d3d12.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_DRIVER_D3D12_H
  31. #define RENDERING_DEVICE_DRIVER_D3D12_H
  32. #include "core/templates/hash_map.h"
  33. #include "core/templates/paged_allocator.h"
  34. #include "core/templates/self_list.h"
  35. #include "servers/rendering/rendering_device_driver.h"
  36. #ifndef _MSC_VER
  37. // Match current version used by MinGW, MSVC and Direct3D 12 headers use 500.
  38. #define __REQUIRED_RPCNDR_H_VERSION__ 475
  39. #endif
  40. #if defined(__GNUC__) && !defined(__clang__)
  41. #pragma GCC diagnostic push
  42. #pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
  43. #pragma GCC diagnostic ignored "-Wshadow"
  44. #pragma GCC diagnostic ignored "-Wswitch"
  45. #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
  46. #pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
  47. #elif defined(__clang__)
  48. #pragma clang diagnostic push
  49. #pragma clang diagnostic ignored "-Wnon-virtual-dtor"
  50. #pragma clang diagnostic ignored "-Wstring-plus-int"
  51. #pragma clang diagnostic ignored "-Wswitch"
  52. #pragma clang diagnostic ignored "-Wmissing-field-initializers"
  53. #pragma clang diagnostic ignored "-Wimplicit-fallthrough"
  54. #endif
  55. #include "d3dx12.h"
  56. #include <dxgi1_6.h>
  57. #define D3D12MA_D3D12_HEADERS_ALREADY_INCLUDED
  58. #include "D3D12MemAlloc.h"
  59. #include <wrl/client.h>
  60. #if defined(_MSC_VER) && defined(MemoryBarrier)
  61. // Annoying define from winnt.h. Reintroduced by some of the headers above.
  62. #undef MemoryBarrier
  63. #endif
  64. #if defined(__GNUC__) && !defined(__clang__)
  65. #pragma GCC diagnostic pop
  66. #elif defined(__clang__)
  67. #pragma clang diagnostic pop
  68. #endif
  69. using Microsoft::WRL::ComPtr;
  70. #define D3D12_BITCODE_OFFSETS_NUM_STAGES 3
  71. #ifdef DEV_ENABLED
  72. #define CUSTOM_INFO_QUEUE_ENABLED 0
  73. #endif
  74. class RenderingContextDriverD3D12;
  75. // Design principles:
  76. // - D3D12 structs are zero-initialized and fields not requiring a non-zero value are omitted (except in cases where expresivity reasons apply).
  77. class RenderingDeviceDriverD3D12 : public RenderingDeviceDriver {
  78. /*****************/
  79. /**** GENERIC ****/
  80. /*****************/
  81. struct D3D12Format {
  82. DXGI_FORMAT family = DXGI_FORMAT_UNKNOWN;
  83. DXGI_FORMAT general_format = DXGI_FORMAT_UNKNOWN;
  84. UINT swizzle = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
  85. DXGI_FORMAT dsv_format = DXGI_FORMAT_UNKNOWN;
  86. };
  87. static const D3D12Format RD_TO_D3D12_FORMAT[RDD::DATA_FORMAT_MAX];
  88. struct DeviceLimits {
  89. uint64_t max_srvs_per_shader_stage = 0;
  90. uint64_t max_cbvs_per_shader_stage = 0;
  91. uint64_t max_samplers_across_all_stages = 0;
  92. uint64_t max_uavs_across_all_stages = 0;
  93. uint64_t timestamp_frequency = 0;
  94. };
  95. struct SubgroupCapabilities {
  96. uint32_t size = 0;
  97. bool wave_ops_supported = false;
  98. uint32_t supported_stages_flags_rd() const;
  99. uint32_t supported_operations_flags_rd() const;
  100. };
  101. struct VRSCapabilities {
  102. bool draw_call_supported = false; // We can specify our fragment rate on a draw call level.
  103. bool primitive_supported = false; // We can specify our fragment rate on each drawcall.
  104. bool primitive_in_multiviewport = false;
  105. bool ss_image_supported = false; // We can provide a density map attachment on our framebuffer.
  106. uint32_t ss_image_tile_size = 0;
  107. uint32_t ss_max_fragment_size = 0;
  108. bool additional_rates_supported = false;
  109. };
  110. struct ShaderCapabilities {
  111. D3D_SHADER_MODEL shader_model = (D3D_SHADER_MODEL)0;
  112. bool native_16bit_ops = false;
  113. };
  114. struct StorageBufferCapabilities {
  115. bool storage_buffer_16_bit_access_is_supported = false;
  116. };
  117. struct FormatCapabilities {
  118. bool relaxed_casting_supported = false;
  119. };
  120. struct BarrierCapabilities {
  121. bool enhanced_barriers_supported = false;
  122. };
  123. struct MiscFeaturesSupport {
  124. bool depth_bounds_supported = false;
  125. };
  126. RenderingContextDriverD3D12 *context_driver = nullptr;
  127. RenderingContextDriver::Device context_device;
  128. ComPtr<IDXGIAdapter> adapter;
  129. DXGI_ADAPTER_DESC adapter_desc;
  130. ComPtr<ID3D12Device> device;
  131. DeviceLimits device_limits;
  132. RDD::Capabilities device_capabilities;
  133. uint32_t feature_level = 0; // Major * 10 + minor.
  134. SubgroupCapabilities subgroup_capabilities;
  135. RDD::MultiviewCapabilities multiview_capabilities;
  136. VRSCapabilities vrs_capabilities;
  137. ShaderCapabilities shader_capabilities;
  138. StorageBufferCapabilities storage_buffer_capabilities;
  139. FormatCapabilities format_capabilities;
  140. BarrierCapabilities barrier_capabilities;
  141. MiscFeaturesSupport misc_features_support;
  142. String pipeline_cache_id;
  143. class DescriptorsHeap {
  144. D3D12_DESCRIPTOR_HEAP_DESC desc = {};
  145. ComPtr<ID3D12DescriptorHeap> heap;
  146. uint32_t handle_size = 0;
  147. public:
  148. class Walker { // Texas Ranger.
  149. friend class DescriptorsHeap;
  150. uint32_t handle_size = 0;
  151. uint32_t handle_count = 0;
  152. D3D12_CPU_DESCRIPTOR_HANDLE first_cpu_handle = {};
  153. D3D12_GPU_DESCRIPTOR_HANDLE first_gpu_handle = {};
  154. uint32_t handle_index = 0;
  155. public:
  156. D3D12_CPU_DESCRIPTOR_HANDLE get_curr_cpu_handle();
  157. D3D12_GPU_DESCRIPTOR_HANDLE get_curr_gpu_handle();
  158. _FORCE_INLINE_ void rewind() { handle_index = 0; }
  159. void advance(uint32_t p_count = 1);
  160. uint32_t get_current_handle_index() const { return handle_index; }
  161. uint32_t get_free_handles() { return handle_count - handle_index; }
  162. bool is_at_eof() { return handle_index == handle_count; }
  163. };
  164. Error allocate(ID3D12Device *m_device, D3D12_DESCRIPTOR_HEAP_TYPE m_type, uint32_t m_descriptor_count, bool p_for_gpu);
  165. uint32_t get_descriptor_count() const { return desc.NumDescriptors; }
  166. ID3D12DescriptorHeap *get_heap() const { return heap.Get(); }
  167. Walker make_walker() const;
  168. };
  169. struct {
  170. ComPtr<ID3D12CommandSignature> draw;
  171. ComPtr<ID3D12CommandSignature> draw_indexed;
  172. ComPtr<ID3D12CommandSignature> dispatch;
  173. } indirect_cmd_signatures;
  174. static void STDMETHODCALLTYPE _debug_message_func(D3D12_MESSAGE_CATEGORY p_category, D3D12_MESSAGE_SEVERITY p_severity, D3D12_MESSAGE_ID p_id, LPCSTR p_description, void *p_context);
  175. void _set_object_name(ID3D12Object *p_object, String p_object_name);
  176. Error _initialize_device();
  177. Error _check_capabilities();
  178. Error _get_device_limits();
  179. Error _initialize_allocator();
  180. Error _initialize_frames(uint32_t p_frame_count);
  181. Error _initialize_command_signatures();
  182. public:
  183. Error initialize(uint32_t p_device_index, uint32_t p_frame_count) override final;
  184. private:
  185. /****************/
  186. /**** MEMORY ****/
  187. /****************/
  188. ComPtr<D3D12MA::Allocator> allocator;
  189. /******************/
  190. /**** RESOURCE ****/
  191. /******************/
  192. struct ResourceInfo {
  193. struct States {
  194. // As many subresources as mipmaps * layers; planes (for depth-stencil) are tracked together.
  195. TightLocalVector<D3D12_RESOURCE_STATES> subresource_states; // Used only if not a view.
  196. uint32_t last_batch_with_uav_barrier = 0;
  197. };
  198. ID3D12Resource *resource = nullptr; // Non-null even if not owned.
  199. struct {
  200. ComPtr<ID3D12Resource> resource;
  201. ComPtr<D3D12MA::Allocation> allocation;
  202. States states;
  203. } owner_info; // All empty if the resource is not owned.
  204. States *states_ptr = nullptr; // Own or from another if it doesn't own the D3D12 resource.
  205. };
  206. struct BarrierRequest {
  207. static const uint32_t MAX_GROUPS = 4;
  208. // Maybe this is too much data to have it locally. Benchmarking may reveal that
  209. // cache would be used better by having a maximum of local subresource masks and beyond
  210. // that have an allocated vector with the rest.
  211. static const uint32_t MAX_SUBRESOURCES = 4096;
  212. ID3D12Resource *dx_resource = nullptr;
  213. uint8_t subres_mask_qwords = 0;
  214. uint8_t planes = 0;
  215. struct Group {
  216. D3D12_RESOURCE_STATES states = {};
  217. static_assert(MAX_SUBRESOURCES % 64 == 0);
  218. uint64_t subres_mask[MAX_SUBRESOURCES / 64] = {};
  219. } groups[MAX_GROUPS];
  220. uint8_t groups_count = 0;
  221. static const D3D12_RESOURCE_STATES DELETED_GROUP = D3D12_RESOURCE_STATES(0xFFFFFFFFU);
  222. };
  223. struct CommandBufferInfo;
  224. void _resource_transition_batch(CommandBufferInfo *p_command_buffer, ResourceInfo *p_resource, uint32_t p_subresource, uint32_t p_num_planes, D3D12_RESOURCE_STATES p_new_state);
  225. void _resource_transitions_flush(CommandBufferInfo *p_command_buffer);
  226. /*****************/
  227. /**** BUFFERS ****/
  228. /*****************/
  229. struct BufferInfo : public ResourceInfo {
  230. DataFormat texel_format = DATA_FORMAT_MAX;
  231. uint64_t size = 0;
  232. struct {
  233. bool usable_as_uav : 1;
  234. } flags = {};
  235. };
  236. public:
  237. virtual BufferID buffer_create(uint64_t p_size, BitField<BufferUsageBits> p_usage, MemoryAllocationType p_allocation_type) override final;
  238. virtual bool buffer_set_texel_format(BufferID p_buffer, DataFormat p_format) override final;
  239. virtual void buffer_free(BufferID p_buffer) override final;
  240. virtual uint64_t buffer_get_allocation_size(BufferID p_buffer) override final;
  241. virtual uint8_t *buffer_map(BufferID p_buffer) override final;
  242. virtual void buffer_unmap(BufferID p_buffer) override final;
  243. virtual uint64_t buffer_get_device_address(BufferID p_buffer) override final;
  244. /*****************/
  245. /**** TEXTURE ****/
  246. /*****************/
  247. private:
  248. struct TextureInfo : public ResourceInfo {
  249. DataFormat format = DATA_FORMAT_MAX;
  250. CD3DX12_RESOURCE_DESC desc = {};
  251. uint32_t base_layer = 0;
  252. uint32_t layers = 0;
  253. uint32_t base_mip = 0;
  254. uint32_t mipmaps = 0;
  255. struct {
  256. D3D12_SHADER_RESOURCE_VIEW_DESC srv;
  257. D3D12_UNORDERED_ACCESS_VIEW_DESC uav;
  258. } view_descs = {};
  259. TextureInfo *main_texture = nullptr;
  260. UINT mapped_subresource = UINT_MAX;
  261. SelfList<TextureInfo> pending_clear{ this };
  262. };
  263. SelfList<TextureInfo>::List textures_pending_clear;
  264. HashMap<DXGI_FORMAT, uint32_t> format_sample_counts_mask_cache;
  265. Mutex format_sample_counts_mask_cache_mutex;
  266. uint32_t _find_max_common_supported_sample_count(VectorView<DXGI_FORMAT> p_formats);
  267. UINT _compute_component_mapping(const TextureView &p_view);
  268. UINT _compute_plane_slice(DataFormat p_format, BitField<TextureAspectBits> p_aspect_bits);
  269. UINT _compute_plane_slice(DataFormat p_format, TextureAspect p_aspect);
  270. UINT _compute_subresource_from_layers(TextureInfo *p_texture, const TextureSubresourceLayers &p_layers, uint32_t p_layer_offset);
  271. void _discard_texture_subresources(const TextureInfo *p_tex_info, const CommandBufferInfo *p_cmd_buf_info);
  272. protected:
  273. virtual bool _unordered_access_supported_by_format(DataFormat p_format);
  274. public:
  275. virtual TextureID texture_create(const TextureFormat &p_format, const TextureView &p_view) override final;
  276. virtual TextureID texture_create_from_extension(uint64_t p_native_texture, TextureType p_type, DataFormat p_format, uint32_t p_array_layers, bool p_depth_stencil) override final;
  277. virtual TextureID texture_create_shared(TextureID p_original_texture, const TextureView &p_view) override final;
  278. virtual TextureID texture_create_shared_from_slice(TextureID p_original_texture, const TextureView &p_view, TextureSliceType p_slice_type, uint32_t p_layer, uint32_t p_layers, uint32_t p_mipmap, uint32_t p_mipmaps) override final;
  279. virtual void texture_free(TextureID p_texture) override final;
  280. virtual uint64_t texture_get_allocation_size(TextureID p_texture) override final;
  281. virtual void texture_get_copyable_layout(TextureID p_texture, const TextureSubresource &p_subresource, TextureCopyableLayout *r_layout) override final;
  282. virtual uint8_t *texture_map(TextureID p_texture, const TextureSubresource &p_subresource) override final;
  283. virtual void texture_unmap(TextureID p_texture) override final;
  284. virtual BitField<TextureUsageBits> texture_get_usages_supported_by_format(DataFormat p_format, bool p_cpu_readable) override final;
  285. virtual bool texture_can_make_shared_with_format(TextureID p_texture, DataFormat p_format, bool &r_raw_reinterpretation) override final;
  286. private:
  287. TextureID _texture_create_shared_from_slice(TextureID p_original_texture, const TextureView &p_view, TextureSliceType p_slice_type, uint32_t p_layer, uint32_t p_layers, uint32_t p_mipmap, uint32_t p_mipmaps);
  288. public:
  289. /*****************/
  290. /**** SAMPLER ****/
  291. /*****************/
  292. private:
  293. LocalVector<D3D12_SAMPLER_DESC> samplers;
  294. public:
  295. virtual SamplerID sampler_create(const SamplerState &p_state) final override;
  296. virtual void sampler_free(SamplerID p_sampler) final override;
  297. virtual bool sampler_is_format_supported_for_filter(DataFormat p_format, SamplerFilter p_filter) override final;
  298. /**********************/
  299. /**** VERTEX ARRAY ****/
  300. /**********************/
  301. private:
  302. struct VertexFormatInfo {
  303. TightLocalVector<D3D12_INPUT_ELEMENT_DESC> input_elem_descs;
  304. TightLocalVector<UINT> vertex_buffer_strides;
  305. };
  306. public:
  307. virtual VertexFormatID vertex_format_create(VectorView<VertexAttribute> p_vertex_attribs) override final;
  308. virtual void vertex_format_free(VertexFormatID p_vertex_format) override final;
  309. /******************/
  310. /**** BARRIERS ****/
  311. /******************/
  312. virtual void command_pipeline_barrier(
  313. CommandBufferID p_cmd_buffer,
  314. BitField<PipelineStageBits> p_src_stages,
  315. BitField<PipelineStageBits> p_dst_stages,
  316. VectorView<RDD::MemoryBarrier> p_memory_barriers,
  317. VectorView<RDD::BufferBarrier> p_buffer_barriers,
  318. VectorView<RDD::TextureBarrier> p_texture_barriers) override final;
  319. private:
  320. /****************/
  321. /**** FENCES ****/
  322. /****************/
  323. struct FenceInfo {
  324. ComPtr<ID3D12Fence> d3d_fence = nullptr;
  325. HANDLE event_handle = nullptr;
  326. UINT64 fence_value = 0;
  327. };
  328. public:
  329. virtual FenceID fence_create() override;
  330. virtual Error fence_wait(FenceID p_fence) override;
  331. virtual void fence_free(FenceID p_fence) override;
  332. private:
  333. /********************/
  334. /**** SEMAPHORES ****/
  335. /********************/
  336. struct SemaphoreInfo {
  337. ComPtr<ID3D12Fence> d3d_fence = nullptr;
  338. UINT64 fence_value = 0;
  339. };
  340. virtual SemaphoreID semaphore_create() override;
  341. virtual void semaphore_free(SemaphoreID p_semaphore) override;
  342. /******************/
  343. /**** COMMANDS ****/
  344. /******************/
  345. // ----- QUEUE FAMILY -----
  346. virtual CommandQueueFamilyID command_queue_family_get(BitField<CommandQueueFamilyBits> p_cmd_queue_family_bits, RenderingContextDriver::SurfaceID p_surface = 0) override;
  347. private:
  348. // ----- QUEUE -----
  349. struct CommandQueueInfo {
  350. ComPtr<ID3D12CommandQueue> d3d_queue;
  351. };
  352. public:
  353. virtual CommandQueueID command_queue_create(CommandQueueFamilyID p_cmd_queue_family, bool p_identify_as_main_queue = false) override;
  354. virtual Error command_queue_execute_and_present(CommandQueueID p_cmd_queue, VectorView<SemaphoreID> p_wait_semaphores, VectorView<CommandBufferID> p_cmd_buffers, VectorView<SemaphoreID> p_cmd_semaphores, FenceID p_cmd_fence, VectorView<SwapChainID> p_swap_chains) override;
  355. virtual void command_queue_free(CommandQueueID p_cmd_queue) override;
  356. private:
  357. // ----- POOL -----
  358. struct CommandPoolInfo {
  359. CommandQueueFamilyID queue_family;
  360. CommandBufferType buffer_type = COMMAND_BUFFER_TYPE_PRIMARY;
  361. // Since there are no command pools in D3D12, we need to track the command buffers created by this pool
  362. // so that we can free them when the pool is freed.
  363. SelfList<CommandBufferInfo>::List command_buffers;
  364. };
  365. public:
  366. virtual CommandPoolID command_pool_create(CommandQueueFamilyID p_cmd_queue_family, CommandBufferType p_cmd_buffer_type) override final;
  367. virtual bool command_pool_reset(CommandPoolID p_cmd_pool) override final;
  368. virtual void command_pool_free(CommandPoolID p_cmd_pool) override final;
  369. // ----- BUFFER -----
  370. private:
  371. // Belongs to RENDERING-SUBPASS, but needed here.
  372. struct FramebufferInfo;
  373. struct RenderPassInfo;
  374. struct RenderPassState {
  375. uint32_t current_subpass = UINT32_MAX;
  376. const FramebufferInfo *fb_info = nullptr;
  377. const RenderPassInfo *pass_info = nullptr;
  378. CD3DX12_RECT region_rect = {};
  379. bool region_is_all = false;
  380. const VertexFormatInfo *vf_info = nullptr;
  381. D3D12_VERTEX_BUFFER_VIEW vertex_buffer_views[8] = {};
  382. uint32_t vertex_buffer_count = 0;
  383. };
  384. // Leveraging knowledge of actual usage and D3D12 specifics (namely, command lists from the same allocator
  385. // can't be freely begun and ended), an allocator per list works better.
  386. struct CommandBufferInfo {
  387. // Store a self list reference to be used by the command pool.
  388. SelfList<CommandBufferInfo> command_buffer_info_elem{ this };
  389. ComPtr<ID3D12CommandAllocator> cmd_allocator;
  390. ComPtr<ID3D12GraphicsCommandList> cmd_list;
  391. ID3D12PipelineState *graphics_pso = nullptr;
  392. ID3D12PipelineState *compute_pso = nullptr;
  393. uint32_t graphics_root_signature_crc = 0;
  394. uint32_t compute_root_signature_crc = 0;
  395. RenderPassState render_pass_state;
  396. bool descriptor_heaps_set = false;
  397. HashMap<ResourceInfo::States *, BarrierRequest> res_barriers_requests;
  398. LocalVector<D3D12_RESOURCE_BARRIER> res_barriers;
  399. uint32_t res_barriers_count = 0;
  400. uint32_t res_barriers_batch = 0;
  401. };
  402. public:
  403. virtual CommandBufferID command_buffer_create(CommandPoolID p_cmd_pool) override final;
  404. virtual bool command_buffer_begin(CommandBufferID p_cmd_buffer) override final;
  405. virtual bool command_buffer_begin_secondary(CommandBufferID p_cmd_buffer, RenderPassID p_render_pass, uint32_t p_subpass, FramebufferID p_framebuffer) override final;
  406. virtual void command_buffer_end(CommandBufferID p_cmd_buffer) override final;
  407. virtual void command_buffer_execute_secondary(CommandBufferID p_cmd_buffer, VectorView<CommandBufferID> p_secondary_cmd_buffers) override final;
  408. private:
  409. /********************/
  410. /**** SWAP CHAIN ****/
  411. /********************/
  412. struct SwapChain {
  413. ComPtr<IDXGISwapChain3> d3d_swap_chain;
  414. RenderingContextDriver::SurfaceID surface = RenderingContextDriver::SurfaceID();
  415. UINT present_flags = 0;
  416. UINT sync_interval = 1;
  417. UINT creation_flags = 0;
  418. RenderPassID render_pass;
  419. TightLocalVector<ID3D12Resource *> render_targets;
  420. TightLocalVector<TextureInfo> render_targets_info;
  421. TightLocalVector<FramebufferID> framebuffers;
  422. RDD::DataFormat data_format = DATA_FORMAT_MAX;
  423. };
  424. void _swap_chain_release(SwapChain *p_swap_chain);
  425. void _swap_chain_release_buffers(SwapChain *p_swap_chain);
  426. public:
  427. virtual SwapChainID swap_chain_create(RenderingContextDriver::SurfaceID p_surface) override;
  428. virtual Error swap_chain_resize(CommandQueueID p_cmd_queue, SwapChainID p_swap_chain, uint32_t p_desired_framebuffer_count) override;
  429. virtual FramebufferID swap_chain_acquire_framebuffer(CommandQueueID p_cmd_queue, SwapChainID p_swap_chain, bool &r_resize_required) override;
  430. virtual RenderPassID swap_chain_get_render_pass(SwapChainID p_swap_chain) override;
  431. virtual DataFormat swap_chain_get_format(SwapChainID p_swap_chain) override;
  432. virtual void swap_chain_free(SwapChainID p_swap_chain) override;
  433. /*********************/
  434. /**** FRAMEBUFFER ****/
  435. /*********************/
  436. private:
  437. struct FramebufferInfo {
  438. bool is_screen = false;
  439. Size2i size;
  440. TightLocalVector<uint32_t> attachments_handle_inds; // RTV heap index for color; DSV heap index for DSV.
  441. DescriptorsHeap rtv_heap;
  442. DescriptorsHeap dsv_heap; // Used only if not for screen and some depth-stencil attachments.
  443. TightLocalVector<TextureID> attachments; // Color and depth-stencil. Used if not screen.
  444. TextureID vrs_attachment;
  445. };
  446. D3D12_RENDER_TARGET_VIEW_DESC _make_rtv_for_texture(const TextureInfo *p_texture_info, uint32_t p_mipmap_offset, uint32_t p_layer_offset, uint32_t p_layers, bool p_add_bases = true);
  447. D3D12_UNORDERED_ACCESS_VIEW_DESC _make_ranged_uav_for_texture(const TextureInfo *p_texture_info, uint32_t p_mipmap_offset, uint32_t p_layer_offset, uint32_t p_layers, bool p_add_bases = true);
  448. D3D12_DEPTH_STENCIL_VIEW_DESC _make_dsv_for_texture(const TextureInfo *p_texture_info);
  449. FramebufferID _framebuffer_create(RenderPassID p_render_pass, VectorView<TextureID> p_attachments, uint32_t p_width, uint32_t p_height, bool p_is_screen);
  450. public:
  451. virtual FramebufferID framebuffer_create(RenderPassID p_render_pass, VectorView<TextureID> p_attachments, uint32_t p_width, uint32_t p_height) override final;
  452. virtual void framebuffer_free(FramebufferID p_framebuffer) override final;
  453. /****************/
  454. /**** SHADER ****/
  455. /****************/
  456. private:
  457. static const uint32_t ROOT_SIGNATURE_SIZE = 256;
  458. static const uint32_t PUSH_CONSTANT_SIZE = 128; // Mimicking Vulkan.
  459. enum {
  460. // We can only aim to set a maximum here, since depending on the shader
  461. // there may be more or less root signature free for descriptor tables.
  462. // Therefore, we'll have to rely on the final check at runtime, when building
  463. // the root signature structure for a given shader.
  464. // To be precise, these may be present or not, and their size vary statically:
  465. // - Push constant (we'll assume this is always present to avoid reserving much
  466. // more space for descriptor sets than needed for almost any imaginable case,
  467. // given that most shader templates feature push constants).
  468. // - NIR-DXIL runtime data.
  469. MAX_UNIFORM_SETS = (ROOT_SIGNATURE_SIZE - PUSH_CONSTANT_SIZE) / sizeof(uint32_t),
  470. };
  471. enum RootSignatureLocationType {
  472. RS_LOC_TYPE_RESOURCE,
  473. RS_LOC_TYPE_SAMPLER,
  474. };
  475. enum ResourceClass {
  476. RES_CLASS_INVALID,
  477. RES_CLASS_CBV,
  478. RES_CLASS_SRV,
  479. RES_CLASS_UAV,
  480. };
  481. struct ShaderBinary {
  482. // Version 1: Initial.
  483. // Version 2: 64-bit vertex input mask.
  484. // Version 3: Added SC stage mask.
  485. static const uint32_t VERSION = 3;
  486. // Phase 1: SPIR-V reflection, where the Vulkan/RD interface of the shader is discovered.
  487. // Phase 2: SPIR-V to DXIL translation, where the DXIL interface is discovered, which may have gaps due to optimizations.
  488. struct DataBinding {
  489. // - Phase 1.
  490. uint32_t type = 0;
  491. uint32_t binding = 0;
  492. uint32_t stages = 0;
  493. uint32_t length = 0; // Size of arrays (in total elements), or ubos (in bytes * total elements).
  494. uint32_t writable = 0;
  495. // - Phase 2.
  496. uint32_t res_class = 0;
  497. uint32_t has_sampler = 0;
  498. uint32_t dxil_stages = 0;
  499. struct RootSignatureLocation {
  500. uint32_t root_param_idx = UINT32_MAX; // UINT32_MAX if unused.
  501. uint32_t range_idx = UINT32_MAX; // UINT32_MAX if unused.
  502. };
  503. RootSignatureLocation root_sig_locations[2]; // Index is RootSignatureLocationType.
  504. // We need to sort these to fill the root signature locations properly.
  505. bool operator<(const DataBinding &p_other) const {
  506. return binding < p_other.binding;
  507. }
  508. };
  509. struct SpecializationConstant {
  510. // - Phase 1.
  511. uint32_t type = 0;
  512. uint32_t constant_id = 0;
  513. union {
  514. uint32_t int_value = 0;
  515. float float_value;
  516. bool bool_value;
  517. };
  518. uint32_t stage_flags = 0;
  519. // - Phase 2.
  520. uint64_t stages_bit_offsets[D3D12_BITCODE_OFFSETS_NUM_STAGES] = {};
  521. };
  522. struct Data {
  523. uint64_t vertex_input_mask = 0;
  524. uint32_t fragment_output_mask = 0;
  525. uint32_t specialization_constants_count = 0;
  526. uint32_t spirv_specialization_constants_ids_mask = 0;
  527. uint32_t is_compute = 0;
  528. uint32_t compute_local_size[3] = {};
  529. uint32_t set_count = 0;
  530. uint32_t push_constant_size = 0;
  531. uint32_t dxil_push_constant_stages = 0; // Phase 2.
  532. uint32_t nir_runtime_data_root_param_idx = 0; // Phase 2.
  533. uint32_t stage_count = 0;
  534. uint32_t shader_name_len = 0;
  535. uint32_t root_signature_len = 0;
  536. uint32_t root_signature_crc = 0;
  537. };
  538. };
  539. struct ShaderInfo {
  540. uint32_t dxil_push_constant_size = 0;
  541. uint32_t nir_runtime_data_root_param_idx = UINT32_MAX;
  542. bool is_compute = false;
  543. struct UniformBindingInfo {
  544. uint32_t stages = 0; // Actual shader stages using the uniform (0 if totally optimized out).
  545. ResourceClass res_class = RES_CLASS_INVALID;
  546. UniformType type = UNIFORM_TYPE_MAX;
  547. uint32_t length = UINT32_MAX;
  548. #ifdef DEV_ENABLED
  549. bool writable = false;
  550. #endif
  551. struct RootSignatureLocation {
  552. uint32_t root_param_idx = UINT32_MAX;
  553. uint32_t range_idx = UINT32_MAX;
  554. };
  555. struct {
  556. RootSignatureLocation resource;
  557. RootSignatureLocation sampler;
  558. } root_sig_locations;
  559. };
  560. struct UniformSet {
  561. TightLocalVector<UniformBindingInfo> bindings;
  562. struct {
  563. uint32_t resources = 0;
  564. uint32_t samplers = 0;
  565. } num_root_params;
  566. };
  567. TightLocalVector<UniformSet> sets;
  568. struct SpecializationConstant {
  569. uint32_t constant_id = UINT32_MAX;
  570. uint32_t int_value = UINT32_MAX;
  571. uint64_t stages_bit_offsets[D3D12_BITCODE_OFFSETS_NUM_STAGES] = {};
  572. };
  573. TightLocalVector<SpecializationConstant> specialization_constants;
  574. uint32_t spirv_specialization_constants_ids_mask = 0;
  575. HashMap<ShaderStage, Vector<uint8_t>> stages_bytecode;
  576. ComPtr<ID3D12RootSignature> root_signature;
  577. ComPtr<ID3D12RootSignatureDeserializer> root_signature_deserializer;
  578. const D3D12_ROOT_SIGNATURE_DESC *root_signature_desc = nullptr; // Owned by the deserializer.
  579. uint32_t root_signature_crc = 0;
  580. };
  581. uint32_t _shader_patch_dxil_specialization_constant(
  582. PipelineSpecializationConstantType p_type,
  583. const void *p_value,
  584. const uint64_t (&p_stages_bit_offsets)[D3D12_BITCODE_OFFSETS_NUM_STAGES],
  585. HashMap<ShaderStage, Vector<uint8_t>> &r_stages_bytecodes,
  586. bool p_is_first_patch);
  587. bool _shader_apply_specialization_constants(
  588. const ShaderInfo *p_shader_info,
  589. VectorView<PipelineSpecializationConstant> p_specialization_constants,
  590. HashMap<ShaderStage, Vector<uint8_t>> &r_final_stages_bytecode);
  591. void _shader_sign_dxil_bytecode(ShaderStage p_stage, Vector<uint8_t> &r_dxil_blob);
  592. public:
  593. virtual String shader_get_binary_cache_key() override final;
  594. virtual Vector<uint8_t> shader_compile_binary_from_spirv(VectorView<ShaderStageSPIRVData> p_spirv, const String &p_shader_name) override final;
  595. virtual ShaderID shader_create_from_bytecode(const Vector<uint8_t> &p_shader_binary, ShaderDescription &r_shader_desc, String &r_name, const Vector<ImmutableSampler> &p_immutable_samplers) override final;
  596. virtual uint32_t shader_get_layout_hash(ShaderID p_shader) override final;
  597. virtual void shader_free(ShaderID p_shader) override final;
  598. virtual void shader_destroy_modules(ShaderID p_shader) override final;
  599. /*********************/
  600. /**** UNIFORM SET ****/
  601. /*********************/
  602. private:
  603. struct RootDescriptorTable {
  604. uint32_t root_param_idx = UINT32_MAX;
  605. D3D12_GPU_DESCRIPTOR_HANDLE start_gpu_handle = {};
  606. };
  607. struct UniformSetInfo {
  608. struct {
  609. DescriptorsHeap resources;
  610. DescriptorsHeap samplers;
  611. } desc_heaps;
  612. struct StateRequirement {
  613. ResourceInfo *resource = nullptr;
  614. bool is_buffer = false;
  615. D3D12_RESOURCE_STATES states = {};
  616. uint64_t shader_uniform_idx_mask = 0;
  617. };
  618. TightLocalVector<StateRequirement> resource_states;
  619. struct RecentBind {
  620. uint64_t segment_serial = 0;
  621. uint32_t root_signature_crc = 0;
  622. struct {
  623. TightLocalVector<RootDescriptorTable> resources;
  624. TightLocalVector<RootDescriptorTable> samplers;
  625. } root_tables;
  626. int uses = 0;
  627. } recent_binds[4]; // A better amount may be empirically found.
  628. #ifdef DEV_ENABLED
  629. // Filthy, but useful for dev.
  630. struct ResourceDescInfo {
  631. D3D12_DESCRIPTOR_RANGE_TYPE type;
  632. D3D12_SRV_DIMENSION srv_dimension;
  633. };
  634. TightLocalVector<ResourceDescInfo> resources_desc_info;
  635. #endif
  636. };
  637. public:
  638. virtual UniformSetID uniform_set_create(VectorView<BoundUniform> p_uniforms, ShaderID p_shader, uint32_t p_set_index, int p_linear_pool_index) override final;
  639. virtual void uniform_set_free(UniformSetID p_uniform_set) override final;
  640. // ----- COMMANDS -----
  641. virtual void command_uniform_set_prepare_for_use(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) override final;
  642. private:
  643. void _command_check_descriptor_sets(CommandBufferID p_cmd_buffer);
  644. void _command_bind_uniform_set(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index, bool p_for_compute);
  645. void _command_bind_uniform_sets(CommandBufferID p_cmd_buffer, VectorView<UniformSetID> p_uniform_sets, ShaderID p_shader, uint32_t p_first_set_index, uint32_t p_set_count, bool p_for_compute);
  646. public:
  647. /******************/
  648. /**** TRANSFER ****/
  649. /******************/
  650. virtual void command_clear_buffer(CommandBufferID p_cmd_buffer, BufferID p_buffer, uint64_t p_offset, uint64_t p_size) override final;
  651. virtual void command_copy_buffer(CommandBufferID p_cmd_buffer, BufferID p_src_buffer, BufferID p_dst_buffer, VectorView<BufferCopyRegion> p_regions) override final;
  652. virtual void command_copy_texture(CommandBufferID p_cmd_buffer, TextureID p_src_texture, TextureLayout p_src_texture_layout, TextureID p_dst_texture, TextureLayout p_dst_texture_layout, VectorView<TextureCopyRegion> p_regions) override final;
  653. virtual void command_resolve_texture(CommandBufferID p_cmd_buffer, TextureID p_src_texture, TextureLayout p_src_texture_layout, uint32_t p_src_layer, uint32_t p_src_mipmap, TextureID p_dst_texture, TextureLayout p_dst_texture_layout, uint32_t p_dst_layer, uint32_t p_dst_mipmap) override final;
  654. virtual void command_clear_color_texture(CommandBufferID p_cmd_buffer, TextureID p_texture, TextureLayout p_texture_layout, const Color &p_color, const TextureSubresourceRange &p_subresources) override final;
  655. public:
  656. virtual void command_copy_buffer_to_texture(CommandBufferID p_cmd_buffer, BufferID p_src_buffer, TextureID p_dst_texture, TextureLayout p_dst_texture_layout, VectorView<BufferTextureCopyRegion> p_regions) override final;
  657. virtual void command_copy_texture_to_buffer(CommandBufferID p_cmd_buffer, TextureID p_src_texture, TextureLayout p_src_texture_layout, BufferID p_dst_buffer, VectorView<BufferTextureCopyRegion> p_regions) override final;
  658. /******************/
  659. /**** PIPELINE ****/
  660. /******************/
  661. struct RenderPipelineInfo {
  662. const VertexFormatInfo *vf_info = nullptr;
  663. struct {
  664. D3D12_PRIMITIVE_TOPOLOGY primitive_topology = {};
  665. Color blend_constant;
  666. float depth_bounds_min = 0.0f;
  667. float depth_bounds_max = 0.0f;
  668. uint32_t stencil_reference = 0;
  669. } dyn_params;
  670. };
  671. struct PipelineInfo {
  672. ID3D12PipelineState *pso = nullptr;
  673. const ShaderInfo *shader_info = nullptr;
  674. RenderPipelineInfo render_info;
  675. };
  676. virtual void pipeline_free(PipelineID p_pipeline) override final;
  677. public:
  678. // ----- BINDING -----
  679. virtual void command_bind_push_constants(CommandBufferID p_cmd_buffer, ShaderID p_shader, uint32_t p_dst_first_index, VectorView<uint32_t> p_data) override final;
  680. // ----- CACHE -----
  681. virtual bool pipeline_cache_create(const Vector<uint8_t> &p_data) override final;
  682. virtual void pipeline_cache_free() override final;
  683. virtual size_t pipeline_cache_query_size() override final;
  684. virtual Vector<uint8_t> pipeline_cache_serialize() override final;
  685. /*******************/
  686. /**** RENDERING ****/
  687. /*******************/
  688. // ----- SUBPASS -----
  689. private:
  690. struct RenderPassInfo {
  691. TightLocalVector<Attachment> attachments;
  692. TightLocalVector<Subpass> subpasses;
  693. uint32_t view_count = 0;
  694. uint32_t max_supported_sample_count = 0;
  695. };
  696. public:
  697. virtual RenderPassID render_pass_create(VectorView<Attachment> p_attachments, VectorView<Subpass> p_subpasses, VectorView<SubpassDependency> p_subpass_dependencies, uint32_t p_view_count) override final;
  698. virtual void render_pass_free(RenderPassID p_render_pass) override final;
  699. // ----- COMMANDS -----
  700. virtual void command_begin_render_pass(CommandBufferID p_cmd_buffer, RenderPassID p_render_pass, FramebufferID p_framebuffer, CommandBufferType p_cmd_buffer_type, const Rect2i &p_rect, VectorView<RenderPassClearValue> p_clear_values) override final;
  701. private:
  702. void _end_render_pass(CommandBufferID p_cmd_buffer);
  703. public:
  704. virtual void command_end_render_pass(CommandBufferID p_cmd_buffer) override final;
  705. virtual void command_next_render_subpass(CommandBufferID p_cmd_buffer, CommandBufferType p_cmd_buffer_type) override final;
  706. virtual void command_render_set_viewport(CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_viewports) override final;
  707. virtual void command_render_set_scissor(CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_scissors) override final;
  708. virtual void command_render_clear_attachments(CommandBufferID p_cmd_buffer, VectorView<AttachmentClear> p_attachment_clears, VectorView<Rect2i> p_rects) override final;
  709. // Binding.
  710. virtual void command_bind_render_pipeline(CommandBufferID p_cmd_buffer, PipelineID p_pipeline) override final;
  711. virtual void command_bind_render_uniform_set(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) override final;
  712. virtual void command_bind_render_uniform_sets(CommandBufferID p_cmd_buffer, VectorView<UniformSetID> p_uniform_sets, ShaderID p_shader, uint32_t p_first_set_index, uint32_t p_set_count) override final;
  713. // Drawing.
  714. virtual void command_render_draw(CommandBufferID p_cmd_buffer, uint32_t p_vertex_count, uint32_t p_instance_count, uint32_t p_base_vertex, uint32_t p_first_instance) override final;
  715. virtual void command_render_draw_indexed(CommandBufferID p_cmd_buffer, uint32_t p_index_count, uint32_t p_instance_count, uint32_t p_first_index, int32_t p_vertex_offset, uint32_t p_first_instance) override final;
  716. virtual void command_render_draw_indexed_indirect(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset, uint32_t p_draw_count, uint32_t p_stride) override final;
  717. virtual void command_render_draw_indexed_indirect_count(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset, BufferID p_count_buffer, uint64_t p_count_buffer_offset, uint32_t p_max_draw_count, uint32_t p_stride) override final;
  718. virtual void command_render_draw_indirect(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset, uint32_t p_draw_count, uint32_t p_stride) override final;
  719. virtual void command_render_draw_indirect_count(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset, BufferID p_count_buffer, uint64_t p_count_buffer_offset, uint32_t p_max_draw_count, uint32_t p_stride) override final;
  720. // Buffer binding.
  721. virtual void command_render_bind_vertex_buffers(CommandBufferID p_cmd_buffer, uint32_t p_binding_count, const BufferID *p_buffers, const uint64_t *p_offsets) override final;
  722. virtual void command_render_bind_index_buffer(CommandBufferID p_cmd_buffer, BufferID p_buffer, IndexBufferFormat p_format, uint64_t p_offset) override final;
  723. private:
  724. void _bind_vertex_buffers(CommandBufferInfo *p_cmd_buf_info);
  725. public:
  726. // Dynamic state.
  727. virtual void command_render_set_blend_constants(CommandBufferID p_cmd_buffer, const Color &p_constants) override final;
  728. virtual void command_render_set_line_width(CommandBufferID p_cmd_buffer, float p_width) override final;
  729. // ----- PIPELINE -----
  730. public:
  731. virtual PipelineID render_pipeline_create(
  732. ShaderID p_shader,
  733. VertexFormatID p_vertex_format,
  734. RenderPrimitive p_render_primitive,
  735. PipelineRasterizationState p_rasterization_state,
  736. PipelineMultisampleState p_multisample_state,
  737. PipelineDepthStencilState p_depth_stencil_state,
  738. PipelineColorBlendState p_blend_state,
  739. VectorView<int32_t> p_color_attachments,
  740. BitField<PipelineDynamicStateFlags> p_dynamic_state,
  741. RenderPassID p_render_pass,
  742. uint32_t p_render_subpass,
  743. VectorView<PipelineSpecializationConstant> p_specialization_constants) override final;
  744. /*****************/
  745. /**** COMPUTE ****/
  746. /*****************/
  747. // ----- COMMANDS -----
  748. // Binding.
  749. virtual void command_bind_compute_pipeline(CommandBufferID p_cmd_buffer, PipelineID p_pipeline) override final;
  750. virtual void command_bind_compute_uniform_set(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) override final;
  751. virtual void command_bind_compute_uniform_sets(CommandBufferID p_cmd_buffer, VectorView<UniformSetID> p_uniform_sets, ShaderID p_shader, uint32_t p_first_set_index, uint32_t p_set_count) override final;
  752. // Dispatching.
  753. virtual void command_compute_dispatch(CommandBufferID p_cmd_buffer, uint32_t p_x_groups, uint32_t p_y_groups, uint32_t p_z_groups) override final;
  754. virtual void command_compute_dispatch_indirect(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset) override final;
  755. // ----- PIPELINE -----
  756. virtual PipelineID compute_pipeline_create(ShaderID p_shader, VectorView<PipelineSpecializationConstant> p_specialization_constants) override final;
  757. /*****************/
  758. /**** QUERIES ****/
  759. /*****************/
  760. // ----- TIMESTAMP -----
  761. private:
  762. struct TimestampQueryPoolInfo {
  763. ComPtr<ID3D12QueryHeap> query_heap;
  764. uint32_t query_count = 0;
  765. ComPtr<D3D12MA::Allocation> results_buffer_allocation;
  766. };
  767. public:
  768. // Basic.
  769. virtual QueryPoolID timestamp_query_pool_create(uint32_t p_query_count) override final;
  770. virtual void timestamp_query_pool_free(QueryPoolID p_pool_id) override final;
  771. virtual void timestamp_query_pool_get_results(QueryPoolID p_pool_id, uint32_t p_query_count, uint64_t *r_results) override final;
  772. virtual uint64_t timestamp_query_result_to_time(uint64_t p_result) override final;
  773. // Commands.
  774. virtual void command_timestamp_query_pool_reset(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_query_count) override final;
  775. virtual void command_timestamp_write(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_index) override final;
  776. /****************/
  777. /**** LABELS ****/
  778. /****************/
  779. virtual void command_begin_label(CommandBufferID p_cmd_buffer, const char *p_label_name, const Color &p_color) override final;
  780. virtual void command_end_label(CommandBufferID p_cmd_buffer) override final;
  781. /****************/
  782. /**** DEBUG *****/
  783. /****************/
  784. virtual void command_insert_breadcrumb(CommandBufferID p_cmd_buffer, uint32_t p_data) override final;
  785. /********************/
  786. /**** SUBMISSION ****/
  787. /********************/
  788. private:
  789. struct FrameInfo {
  790. struct {
  791. DescriptorsHeap resources;
  792. DescriptorsHeap samplers;
  793. DescriptorsHeap aux;
  794. DescriptorsHeap rtv;
  795. } desc_heaps;
  796. struct {
  797. DescriptorsHeap::Walker resources;
  798. DescriptorsHeap::Walker samplers;
  799. DescriptorsHeap::Walker aux;
  800. DescriptorsHeap::Walker rtv;
  801. } desc_heap_walkers;
  802. struct {
  803. bool resources = false;
  804. bool samplers = false;
  805. bool aux = false;
  806. bool rtv = false;
  807. } desc_heaps_exhausted_reported;
  808. CD3DX12_CPU_DESCRIPTOR_HANDLE null_rtv_handle = {}; // For [[MANUAL_SUBPASSES]].
  809. uint32_t segment_serial = 0;
  810. #ifdef DEV_ENABLED
  811. uint32_t uniform_set_reused = 0;
  812. #endif
  813. };
  814. TightLocalVector<FrameInfo> frames;
  815. uint32_t frame_idx = 0;
  816. uint32_t frames_drawn = 0;
  817. uint32_t segment_serial = 0;
  818. bool segment_begun = false;
  819. HashMap<uint64_t, bool> has_comp_alpha;
  820. public:
  821. virtual void begin_segment(uint32_t p_frame_index, uint32_t p_frames_drawn) override final;
  822. virtual void end_segment() override final;
  823. /**************/
  824. /**** MISC ****/
  825. /**************/
  826. virtual void set_object_name(ObjectType p_type, ID p_driver_id, const String &p_name) override final;
  827. virtual uint64_t get_resource_native_handle(DriverResource p_type, ID p_driver_id) override final;
  828. virtual uint64_t get_total_memory_used() override final;
  829. virtual uint64_t get_lazily_memory_used() override final;
  830. virtual uint64_t limit_get(Limit p_limit) override final;
  831. virtual uint64_t api_trait_get(ApiTrait p_trait) override final;
  832. virtual bool has_feature(Features p_feature) override final;
  833. virtual const MultiviewCapabilities &get_multiview_capabilities() override final;
  834. virtual String get_api_name() const override final;
  835. virtual String get_api_version() const override final;
  836. virtual String get_pipeline_cache_uuid() const override final;
  837. virtual const Capabilities &get_capabilities() const override final;
  838. virtual bool is_composite_alpha_supported(CommandQueueID p_queue) const override final;
  839. static bool is_in_developer_mode();
  840. private:
  841. /*********************/
  842. /**** BOOKKEEPING ****/
  843. /*********************/
  844. using VersatileResource = VersatileResourceTemplate<
  845. BufferInfo,
  846. TextureInfo,
  847. TextureInfo,
  848. TextureInfo,
  849. VertexFormatInfo,
  850. CommandBufferInfo,
  851. FramebufferInfo,
  852. ShaderInfo,
  853. UniformSetInfo,
  854. RenderPassInfo,
  855. TimestampQueryPoolInfo>;
  856. PagedAllocator<VersatileResource, true> resources_allocator;
  857. /******************/
  858. public:
  859. RenderingDeviceDriverD3D12(RenderingContextDriverD3D12 *p_context_driver);
  860. virtual ~RenderingDeviceDriverD3D12();
  861. };
  862. #endif // RENDERING_DEVICE_DRIVER_D3D12_H