rendering_device_driver_d3d12.h 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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. /*****************/
  244. /**** TEXTURE ****/
  245. /*****************/
  246. private:
  247. struct TextureInfo : public ResourceInfo {
  248. DataFormat format = DATA_FORMAT_MAX;
  249. CD3DX12_RESOURCE_DESC desc = {};
  250. uint32_t base_layer = 0;
  251. uint32_t layers = 0;
  252. uint32_t base_mip = 0;
  253. uint32_t mipmaps = 0;
  254. struct {
  255. D3D12_SHADER_RESOURCE_VIEW_DESC srv;
  256. D3D12_UNORDERED_ACCESS_VIEW_DESC uav;
  257. } view_descs = {};
  258. TextureInfo *main_texture = nullptr;
  259. UINT mapped_subresource = UINT_MAX;
  260. SelfList<TextureInfo> pending_clear{ this };
  261. };
  262. SelfList<TextureInfo>::List textures_pending_clear;
  263. HashMap<DXGI_FORMAT, uint32_t> format_sample_counts_mask_cache;
  264. Mutex format_sample_counts_mask_cache_mutex;
  265. uint32_t _find_max_common_supported_sample_count(VectorView<DXGI_FORMAT> p_formats);
  266. UINT _compute_component_mapping(const TextureView &p_view);
  267. UINT _compute_plane_slice(DataFormat p_format, BitField<TextureAspectBits> p_aspect_bits);
  268. UINT _compute_plane_slice(DataFormat p_format, TextureAspect p_aspect);
  269. UINT _compute_subresource_from_layers(TextureInfo *p_texture, const TextureSubresourceLayers &p_layers, uint32_t p_layer_offset);
  270. void _discard_texture_subresources(const TextureInfo *p_tex_info, const CommandBufferInfo *p_cmd_buf_info);
  271. protected:
  272. virtual bool _unordered_access_supported_by_format(DataFormat p_format);
  273. public:
  274. virtual TextureID texture_create(const TextureFormat &p_format, const TextureView &p_view) override final;
  275. 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;
  276. virtual TextureID texture_create_shared(TextureID p_original_texture, const TextureView &p_view) override final;
  277. 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;
  278. virtual void texture_free(TextureID p_texture) override final;
  279. virtual uint64_t texture_get_allocation_size(TextureID p_texture) override final;
  280. virtual void texture_get_copyable_layout(TextureID p_texture, const TextureSubresource &p_subresource, TextureCopyableLayout *r_layout) override final;
  281. virtual uint8_t *texture_map(TextureID p_texture, const TextureSubresource &p_subresource) override final;
  282. virtual void texture_unmap(TextureID p_texture) override final;
  283. virtual BitField<TextureUsageBits> texture_get_usages_supported_by_format(DataFormat p_format, bool p_cpu_readable) override final;
  284. virtual bool texture_can_make_shared_with_format(TextureID p_texture, DataFormat p_format, bool &r_raw_reinterpretation) override final;
  285. private:
  286. 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);
  287. public:
  288. /*****************/
  289. /**** SAMPLER ****/
  290. /*****************/
  291. private:
  292. LocalVector<D3D12_SAMPLER_DESC> samplers;
  293. public:
  294. virtual SamplerID sampler_create(const SamplerState &p_state) final override;
  295. virtual void sampler_free(SamplerID p_sampler) final override;
  296. virtual bool sampler_is_format_supported_for_filter(DataFormat p_format, SamplerFilter p_filter) override final;
  297. /**********************/
  298. /**** VERTEX ARRAY ****/
  299. /**********************/
  300. private:
  301. struct VertexFormatInfo {
  302. TightLocalVector<D3D12_INPUT_ELEMENT_DESC> input_elem_descs;
  303. TightLocalVector<UINT> vertex_buffer_strides;
  304. };
  305. public:
  306. virtual VertexFormatID vertex_format_create(VectorView<VertexAttribute> p_vertex_attribs) override final;
  307. virtual void vertex_format_free(VertexFormatID p_vertex_format) override final;
  308. /******************/
  309. /**** BARRIERS ****/
  310. /******************/
  311. virtual void command_pipeline_barrier(
  312. CommandBufferID p_cmd_buffer,
  313. BitField<PipelineStageBits> p_src_stages,
  314. BitField<PipelineStageBits> p_dst_stages,
  315. VectorView<RDD::MemoryBarrier> p_memory_barriers,
  316. VectorView<RDD::BufferBarrier> p_buffer_barriers,
  317. VectorView<RDD::TextureBarrier> p_texture_barriers) override final;
  318. private:
  319. /****************/
  320. /**** FENCES ****/
  321. /****************/
  322. struct FenceInfo {
  323. ComPtr<ID3D12Fence> d3d_fence = nullptr;
  324. HANDLE event_handle = nullptr;
  325. UINT64 fence_value = 0;
  326. };
  327. public:
  328. virtual FenceID fence_create() override;
  329. virtual Error fence_wait(FenceID p_fence) override;
  330. virtual void fence_free(FenceID p_fence) override;
  331. private:
  332. /********************/
  333. /**** SEMAPHORES ****/
  334. /********************/
  335. struct SemaphoreInfo {
  336. ComPtr<ID3D12Fence> d3d_fence = nullptr;
  337. UINT64 fence_value = 0;
  338. };
  339. virtual SemaphoreID semaphore_create() override;
  340. virtual void semaphore_free(SemaphoreID p_semaphore) override;
  341. /******************/
  342. /**** COMMANDS ****/
  343. /******************/
  344. // ----- QUEUE FAMILY -----
  345. virtual CommandQueueFamilyID command_queue_family_get(BitField<CommandQueueFamilyBits> p_cmd_queue_family_bits, RenderingContextDriver::SurfaceID p_surface = 0) override;
  346. private:
  347. // ----- QUEUE -----
  348. struct CommandQueueInfo {
  349. ComPtr<ID3D12CommandQueue> d3d_queue;
  350. };
  351. public:
  352. virtual CommandQueueID command_queue_create(CommandQueueFamilyID p_cmd_queue_family, bool p_identify_as_main_queue = false) override;
  353. 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;
  354. virtual void command_queue_free(CommandQueueID p_cmd_queue) override;
  355. private:
  356. // ----- POOL -----
  357. struct CommandPoolInfo {
  358. CommandQueueFamilyID queue_family;
  359. CommandBufferType buffer_type = COMMAND_BUFFER_TYPE_PRIMARY;
  360. };
  361. public:
  362. virtual CommandPoolID command_pool_create(CommandQueueFamilyID p_cmd_queue_family, CommandBufferType p_cmd_buffer_type) override final;
  363. virtual void command_pool_free(CommandPoolID p_cmd_pool) override final;
  364. // ----- BUFFER -----
  365. private:
  366. // Belongs to RENDERING-SUBPASS, but needed here.
  367. struct FramebufferInfo;
  368. struct RenderPassInfo;
  369. struct RenderPassState {
  370. uint32_t current_subpass = UINT32_MAX;
  371. const FramebufferInfo *fb_info = nullptr;
  372. const RenderPassInfo *pass_info = nullptr;
  373. CD3DX12_RECT region_rect = {};
  374. bool region_is_all = false;
  375. const VertexFormatInfo *vf_info = nullptr;
  376. D3D12_VERTEX_BUFFER_VIEW vertex_buffer_views[8] = {};
  377. uint32_t vertex_buffer_count = 0;
  378. };
  379. // Leveraging knowledge of actual usage and D3D12 specifics (namely, command lists from the same allocator
  380. // can't be freely begun and ended), an allocator per list works better.
  381. struct CommandBufferInfo {
  382. ComPtr<ID3D12CommandAllocator> cmd_allocator;
  383. ComPtr<ID3D12GraphicsCommandList> cmd_list;
  384. ID3D12PipelineState *graphics_pso = nullptr;
  385. ID3D12PipelineState *compute_pso = nullptr;
  386. uint32_t graphics_root_signature_crc = 0;
  387. uint32_t compute_root_signature_crc = 0;
  388. RenderPassState render_pass_state;
  389. bool descriptor_heaps_set = false;
  390. HashMap<ResourceInfo::States *, BarrierRequest> res_barriers_requests;
  391. LocalVector<D3D12_RESOURCE_BARRIER> res_barriers;
  392. uint32_t res_barriers_count = 0;
  393. uint32_t res_barriers_batch = 0;
  394. };
  395. public:
  396. virtual CommandBufferID command_buffer_create(CommandPoolID p_cmd_pool) override final;
  397. virtual bool command_buffer_begin(CommandBufferID p_cmd_buffer) override final;
  398. virtual bool command_buffer_begin_secondary(CommandBufferID p_cmd_buffer, RenderPassID p_render_pass, uint32_t p_subpass, FramebufferID p_framebuffer) override final;
  399. virtual void command_buffer_end(CommandBufferID p_cmd_buffer) override final;
  400. virtual void command_buffer_execute_secondary(CommandBufferID p_cmd_buffer, VectorView<CommandBufferID> p_secondary_cmd_buffers) override final;
  401. private:
  402. /********************/
  403. /**** SWAP CHAIN ****/
  404. /********************/
  405. struct SwapChain {
  406. ComPtr<IDXGISwapChain3> d3d_swap_chain;
  407. RenderingContextDriver::SurfaceID surface = RenderingContextDriver::SurfaceID();
  408. UINT present_flags = 0;
  409. UINT sync_interval = 1;
  410. UINT creation_flags = 0;
  411. RenderPassID render_pass;
  412. TightLocalVector<ID3D12Resource *> render_targets;
  413. TightLocalVector<TextureInfo> render_targets_info;
  414. TightLocalVector<FramebufferID> framebuffers;
  415. RDD::DataFormat data_format = DATA_FORMAT_MAX;
  416. };
  417. void _swap_chain_release(SwapChain *p_swap_chain);
  418. void _swap_chain_release_buffers(SwapChain *p_swap_chain);
  419. public:
  420. virtual SwapChainID swap_chain_create(RenderingContextDriver::SurfaceID p_surface) override;
  421. virtual Error swap_chain_resize(CommandQueueID p_cmd_queue, SwapChainID p_swap_chain, uint32_t p_desired_framebuffer_count) override;
  422. virtual FramebufferID swap_chain_acquire_framebuffer(CommandQueueID p_cmd_queue, SwapChainID p_swap_chain, bool &r_resize_required) override;
  423. virtual RenderPassID swap_chain_get_render_pass(SwapChainID p_swap_chain) override;
  424. virtual DataFormat swap_chain_get_format(SwapChainID p_swap_chain) override;
  425. virtual void swap_chain_free(SwapChainID p_swap_chain) override;
  426. /*********************/
  427. /**** FRAMEBUFFER ****/
  428. /*********************/
  429. private:
  430. struct FramebufferInfo {
  431. bool is_screen = false;
  432. Size2i size;
  433. TightLocalVector<uint32_t> attachments_handle_inds; // RTV heap index for color; DSV heap index for DSV.
  434. DescriptorsHeap rtv_heap;
  435. DescriptorsHeap dsv_heap; // Used only if not for screen and some depth-stencil attachments.
  436. TightLocalVector<TextureID> attachments; // Color and depth-stencil. Used if not screen.
  437. TextureID vrs_attachment;
  438. };
  439. 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);
  440. 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);
  441. D3D12_DEPTH_STENCIL_VIEW_DESC _make_dsv_for_texture(const TextureInfo *p_texture_info);
  442. FramebufferID _framebuffer_create(RenderPassID p_render_pass, VectorView<TextureID> p_attachments, uint32_t p_width, uint32_t p_height, bool p_is_screen);
  443. public:
  444. virtual FramebufferID framebuffer_create(RenderPassID p_render_pass, VectorView<TextureID> p_attachments, uint32_t p_width, uint32_t p_height) override final;
  445. virtual void framebuffer_free(FramebufferID p_framebuffer) override final;
  446. /****************/
  447. /**** SHADER ****/
  448. /****************/
  449. private:
  450. static const uint32_t ROOT_SIGNATURE_SIZE = 256;
  451. static const uint32_t PUSH_CONSTANT_SIZE = 128; // Mimicking Vulkan.
  452. enum {
  453. // We can only aim to set a maximum here, since depending on the shader
  454. // there may be more or less root signature free for descriptor tables.
  455. // Therefore, we'll have to rely on the final check at runtime, when building
  456. // the root signature structure for a given shader.
  457. // To be precise, these may be present or not, and their size vary statically:
  458. // - Push constant (we'll assume this is always present to avoid reserving much
  459. // more space for descriptor sets than needed for almost any imaginable case,
  460. // given that most shader templates feature push constants).
  461. // - NIR-DXIL runtime data.
  462. MAX_UNIFORM_SETS = (ROOT_SIGNATURE_SIZE - PUSH_CONSTANT_SIZE) / sizeof(uint32_t),
  463. };
  464. enum RootSignatureLocationType {
  465. RS_LOC_TYPE_RESOURCE,
  466. RS_LOC_TYPE_SAMPLER,
  467. };
  468. enum ResourceClass {
  469. RES_CLASS_INVALID,
  470. RES_CLASS_CBV,
  471. RES_CLASS_SRV,
  472. RES_CLASS_UAV,
  473. };
  474. struct ShaderBinary {
  475. // Version 1: Initial.
  476. // Version 2: 64-bit vertex input mask.
  477. // Version 3: Added SC stage mask.
  478. static const uint32_t VERSION = 3;
  479. // Phase 1: SPIR-V reflection, where the Vulkan/RD interface of the shader is discovered.
  480. // Phase 2: SPIR-V to DXIL translation, where the DXIL interface is discovered, which may have gaps due to optimizations.
  481. struct DataBinding {
  482. // - Phase 1.
  483. uint32_t type = 0;
  484. uint32_t binding = 0;
  485. uint32_t stages = 0;
  486. uint32_t length = 0; // Size of arrays (in total elements), or ubos (in bytes * total elements).
  487. uint32_t writable = 0;
  488. // - Phase 2.
  489. uint32_t res_class = 0;
  490. uint32_t has_sampler = 0;
  491. uint32_t dxil_stages = 0;
  492. struct RootSignatureLocation {
  493. uint32_t root_param_idx = UINT32_MAX; // UINT32_MAX if unused.
  494. uint32_t range_idx = UINT32_MAX; // UINT32_MAX if unused.
  495. };
  496. RootSignatureLocation root_sig_locations[2]; // Index is RootSignatureLocationType.
  497. // We need to sort these to fill the root signature locations properly.
  498. bool operator<(const DataBinding &p_other) const {
  499. return binding < p_other.binding;
  500. }
  501. };
  502. struct SpecializationConstant {
  503. // - Phase 1.
  504. uint32_t type = 0;
  505. uint32_t constant_id = 0;
  506. union {
  507. uint32_t int_value = 0;
  508. float float_value;
  509. bool bool_value;
  510. };
  511. uint32_t stage_flags = 0;
  512. // - Phase 2.
  513. uint64_t stages_bit_offsets[D3D12_BITCODE_OFFSETS_NUM_STAGES] = {};
  514. };
  515. struct Data {
  516. uint64_t vertex_input_mask = 0;
  517. uint32_t fragment_output_mask = 0;
  518. uint32_t specialization_constants_count = 0;
  519. uint32_t spirv_specialization_constants_ids_mask = 0;
  520. uint32_t is_compute = 0;
  521. uint32_t compute_local_size[3] = {};
  522. uint32_t set_count = 0;
  523. uint32_t push_constant_size = 0;
  524. uint32_t dxil_push_constant_stages = 0; // Phase 2.
  525. uint32_t nir_runtime_data_root_param_idx = 0; // Phase 2.
  526. uint32_t stage_count = 0;
  527. uint32_t shader_name_len = 0;
  528. uint32_t root_signature_len = 0;
  529. uint32_t root_signature_crc = 0;
  530. };
  531. };
  532. struct ShaderInfo {
  533. uint32_t dxil_push_constant_size = 0;
  534. uint32_t nir_runtime_data_root_param_idx = UINT32_MAX;
  535. bool is_compute = false;
  536. struct UniformBindingInfo {
  537. uint32_t stages = 0; // Actual shader stages using the uniform (0 if totally optimized out).
  538. ResourceClass res_class = RES_CLASS_INVALID;
  539. UniformType type = UNIFORM_TYPE_MAX;
  540. uint32_t length = UINT32_MAX;
  541. #ifdef DEV_ENABLED
  542. bool writable = false;
  543. #endif
  544. struct RootSignatureLocation {
  545. uint32_t root_param_idx = UINT32_MAX;
  546. uint32_t range_idx = UINT32_MAX;
  547. };
  548. struct {
  549. RootSignatureLocation resource;
  550. RootSignatureLocation sampler;
  551. } root_sig_locations;
  552. };
  553. struct UniformSet {
  554. TightLocalVector<UniformBindingInfo> bindings;
  555. struct {
  556. uint32_t resources = 0;
  557. uint32_t samplers = 0;
  558. } num_root_params;
  559. };
  560. TightLocalVector<UniformSet> sets;
  561. struct SpecializationConstant {
  562. uint32_t constant_id = UINT32_MAX;
  563. uint32_t int_value = UINT32_MAX;
  564. uint64_t stages_bit_offsets[D3D12_BITCODE_OFFSETS_NUM_STAGES] = {};
  565. };
  566. TightLocalVector<SpecializationConstant> specialization_constants;
  567. uint32_t spirv_specialization_constants_ids_mask = 0;
  568. HashMap<ShaderStage, Vector<uint8_t>> stages_bytecode;
  569. ComPtr<ID3D12RootSignature> root_signature;
  570. ComPtr<ID3D12RootSignatureDeserializer> root_signature_deserializer;
  571. const D3D12_ROOT_SIGNATURE_DESC *root_signature_desc = nullptr; // Owned by the deserializer.
  572. uint32_t root_signature_crc = 0;
  573. };
  574. uint32_t _shader_patch_dxil_specialization_constant(
  575. PipelineSpecializationConstantType p_type,
  576. const void *p_value,
  577. const uint64_t (&p_stages_bit_offsets)[D3D12_BITCODE_OFFSETS_NUM_STAGES],
  578. HashMap<ShaderStage, Vector<uint8_t>> &r_stages_bytecodes,
  579. bool p_is_first_patch);
  580. bool _shader_apply_specialization_constants(
  581. const ShaderInfo *p_shader_info,
  582. VectorView<PipelineSpecializationConstant> p_specialization_constants,
  583. HashMap<ShaderStage, Vector<uint8_t>> &r_final_stages_bytecode);
  584. void _shader_sign_dxil_bytecode(ShaderStage p_stage, Vector<uint8_t> &r_dxil_blob);
  585. public:
  586. virtual String shader_get_binary_cache_key() override final;
  587. virtual Vector<uint8_t> shader_compile_binary_from_spirv(VectorView<ShaderStageSPIRVData> p_spirv, const String &p_shader_name) override final;
  588. virtual ShaderID shader_create_from_bytecode(const Vector<uint8_t> &p_shader_binary, ShaderDescription &r_shader_desc, String &r_name) override final;
  589. virtual uint32_t shader_get_layout_hash(ShaderID p_shader) override final;
  590. virtual void shader_free(ShaderID p_shader) override final;
  591. virtual void shader_destroy_modules(ShaderID p_shader) override final;
  592. /*********************/
  593. /**** UNIFORM SET ****/
  594. /*********************/
  595. private:
  596. struct RootDescriptorTable {
  597. uint32_t root_param_idx = UINT32_MAX;
  598. D3D12_GPU_DESCRIPTOR_HANDLE start_gpu_handle = {};
  599. };
  600. struct UniformSetInfo {
  601. struct {
  602. DescriptorsHeap resources;
  603. DescriptorsHeap samplers;
  604. } desc_heaps;
  605. struct StateRequirement {
  606. ResourceInfo *resource = nullptr;
  607. bool is_buffer = false;
  608. D3D12_RESOURCE_STATES states = {};
  609. uint64_t shader_uniform_idx_mask = 0;
  610. };
  611. TightLocalVector<StateRequirement> resource_states;
  612. struct RecentBind {
  613. uint64_t segment_serial = 0;
  614. uint32_t root_signature_crc = 0;
  615. struct {
  616. TightLocalVector<RootDescriptorTable> resources;
  617. TightLocalVector<RootDescriptorTable> samplers;
  618. } root_tables;
  619. int uses = 0;
  620. } recent_binds[4]; // A better amount may be empirically found.
  621. #ifdef DEV_ENABLED
  622. // Filthy, but useful for dev.
  623. struct ResourceDescInfo {
  624. D3D12_DESCRIPTOR_RANGE_TYPE type;
  625. D3D12_SRV_DIMENSION srv_dimension;
  626. };
  627. TightLocalVector<ResourceDescInfo> resources_desc_info;
  628. #endif
  629. };
  630. public:
  631. virtual UniformSetID uniform_set_create(VectorView<BoundUniform> p_uniforms, ShaderID p_shader, uint32_t p_set_index) override final;
  632. virtual void uniform_set_free(UniformSetID p_uniform_set) override final;
  633. // ----- COMMANDS -----
  634. 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;
  635. private:
  636. void _command_check_descriptor_sets(CommandBufferID p_cmd_buffer);
  637. 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);
  638. public:
  639. /******************/
  640. /**** TRANSFER ****/
  641. /******************/
  642. virtual void command_clear_buffer(CommandBufferID p_cmd_buffer, BufferID p_buffer, uint64_t p_offset, uint64_t p_size) override final;
  643. virtual void command_copy_buffer(CommandBufferID p_cmd_buffer, BufferID p_src_buffer, BufferID p_dst_buffer, VectorView<BufferCopyRegion> p_regions) override final;
  644. 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;
  645. 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;
  646. 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;
  647. public:
  648. 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;
  649. 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;
  650. /******************/
  651. /**** PIPELINE ****/
  652. /******************/
  653. struct RenderPipelineInfo {
  654. const VertexFormatInfo *vf_info = nullptr;
  655. struct {
  656. D3D12_PRIMITIVE_TOPOLOGY primitive_topology = {};
  657. Color blend_constant;
  658. float depth_bounds_min = 0.0f;
  659. float depth_bounds_max = 0.0f;
  660. uint32_t stencil_reference = 0;
  661. } dyn_params;
  662. };
  663. struct PipelineInfo {
  664. ID3D12PipelineState *pso = nullptr;
  665. const ShaderInfo *shader_info = nullptr;
  666. RenderPipelineInfo render_info;
  667. };
  668. virtual void pipeline_free(PipelineID p_pipeline) override final;
  669. public:
  670. // ----- BINDING -----
  671. 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;
  672. // ----- CACHE -----
  673. virtual bool pipeline_cache_create(const Vector<uint8_t> &p_data) override final;
  674. virtual void pipeline_cache_free() override final;
  675. virtual size_t pipeline_cache_query_size() override final;
  676. virtual Vector<uint8_t> pipeline_cache_serialize() override final;
  677. /*******************/
  678. /**** RENDERING ****/
  679. /*******************/
  680. // ----- SUBPASS -----
  681. private:
  682. struct RenderPassInfo {
  683. TightLocalVector<Attachment> attachments;
  684. TightLocalVector<Subpass> subpasses;
  685. uint32_t view_count = 0;
  686. uint32_t max_supported_sample_count = 0;
  687. };
  688. public:
  689. 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;
  690. virtual void render_pass_free(RenderPassID p_render_pass) override final;
  691. // ----- COMMANDS -----
  692. 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;
  693. private:
  694. void _end_render_pass(CommandBufferID p_cmd_buffer);
  695. public:
  696. virtual void command_end_render_pass(CommandBufferID p_cmd_buffer) override final;
  697. virtual void command_next_render_subpass(CommandBufferID p_cmd_buffer, CommandBufferType p_cmd_buffer_type) override final;
  698. virtual void command_render_set_viewport(CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_viewports) override final;
  699. virtual void command_render_set_scissor(CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_scissors) override final;
  700. virtual void command_render_clear_attachments(CommandBufferID p_cmd_buffer, VectorView<AttachmentClear> p_attachment_clears, VectorView<Rect2i> p_rects) override final;
  701. // Binding.
  702. virtual void command_bind_render_pipeline(CommandBufferID p_cmd_buffer, PipelineID p_pipeline) override final;
  703. 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;
  704. // Drawing.
  705. 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;
  706. 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;
  707. 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;
  708. 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;
  709. 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;
  710. 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;
  711. // Buffer binding.
  712. 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;
  713. virtual void command_render_bind_index_buffer(CommandBufferID p_cmd_buffer, BufferID p_buffer, IndexBufferFormat p_format, uint64_t p_offset) override final;
  714. private:
  715. void _bind_vertex_buffers(CommandBufferInfo *p_cmd_buf_info);
  716. public:
  717. // Dynamic state.
  718. virtual void command_render_set_blend_constants(CommandBufferID p_cmd_buffer, const Color &p_constants) override final;
  719. virtual void command_render_set_line_width(CommandBufferID p_cmd_buffer, float p_width) override final;
  720. // ----- PIPELINE -----
  721. public:
  722. virtual PipelineID render_pipeline_create(
  723. ShaderID p_shader,
  724. VertexFormatID p_vertex_format,
  725. RenderPrimitive p_render_primitive,
  726. PipelineRasterizationState p_rasterization_state,
  727. PipelineMultisampleState p_multisample_state,
  728. PipelineDepthStencilState p_depth_stencil_state,
  729. PipelineColorBlendState p_blend_state,
  730. VectorView<int32_t> p_color_attachments,
  731. BitField<PipelineDynamicStateFlags> p_dynamic_state,
  732. RenderPassID p_render_pass,
  733. uint32_t p_render_subpass,
  734. VectorView<PipelineSpecializationConstant> p_specialization_constants) override final;
  735. /*****************/
  736. /**** COMPUTE ****/
  737. /*****************/
  738. // ----- COMMANDS -----
  739. // Binding.
  740. virtual void command_bind_compute_pipeline(CommandBufferID p_cmd_buffer, PipelineID p_pipeline) override final;
  741. 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;
  742. // Dispatching.
  743. 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;
  744. virtual void command_compute_dispatch_indirect(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset) override final;
  745. // ----- PIPELINE -----
  746. virtual PipelineID compute_pipeline_create(ShaderID p_shader, VectorView<PipelineSpecializationConstant> p_specialization_constants) override final;
  747. /*****************/
  748. /**** QUERIES ****/
  749. /*****************/
  750. // ----- TIMESTAMP -----
  751. private:
  752. struct TimestampQueryPoolInfo {
  753. ComPtr<ID3D12QueryHeap> query_heap;
  754. uint32_t query_count = 0;
  755. ComPtr<D3D12MA::Allocation> results_buffer_allocation;
  756. };
  757. public:
  758. // Basic.
  759. virtual QueryPoolID timestamp_query_pool_create(uint32_t p_query_count) override final;
  760. virtual void timestamp_query_pool_free(QueryPoolID p_pool_id) override final;
  761. virtual void timestamp_query_pool_get_results(QueryPoolID p_pool_id, uint32_t p_query_count, uint64_t *r_results) override final;
  762. virtual uint64_t timestamp_query_result_to_time(uint64_t p_result) override final;
  763. // Commands.
  764. virtual void command_timestamp_query_pool_reset(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_query_count) override final;
  765. virtual void command_timestamp_write(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_index) override final;
  766. /****************/
  767. /**** LABELS ****/
  768. /****************/
  769. virtual void command_begin_label(CommandBufferID p_cmd_buffer, const char *p_label_name, const Color &p_color) override final;
  770. virtual void command_end_label(CommandBufferID p_cmd_buffer) override final;
  771. /****************/
  772. /**** DEBUG *****/
  773. /****************/
  774. virtual void command_insert_breadcrumb(CommandBufferID p_cmd_buffer, uint32_t p_data) override final;
  775. /********************/
  776. /**** SUBMISSION ****/
  777. /********************/
  778. private:
  779. struct FrameInfo {
  780. struct {
  781. DescriptorsHeap resources;
  782. DescriptorsHeap samplers;
  783. DescriptorsHeap aux;
  784. DescriptorsHeap rtv;
  785. } desc_heaps;
  786. struct {
  787. DescriptorsHeap::Walker resources;
  788. DescriptorsHeap::Walker samplers;
  789. DescriptorsHeap::Walker aux;
  790. DescriptorsHeap::Walker rtv;
  791. } desc_heap_walkers;
  792. struct {
  793. bool resources = false;
  794. bool samplers = false;
  795. bool aux = false;
  796. bool rtv = false;
  797. } desc_heaps_exhausted_reported;
  798. CD3DX12_CPU_DESCRIPTOR_HANDLE null_rtv_handle = {}; // For [[MANUAL_SUBPASSES]].
  799. uint32_t segment_serial = 0;
  800. #ifdef DEV_ENABLED
  801. uint32_t uniform_set_reused = 0;
  802. #endif
  803. };
  804. TightLocalVector<FrameInfo> frames;
  805. uint32_t frame_idx = 0;
  806. uint32_t frames_drawn = 0;
  807. uint32_t segment_serial = 0;
  808. bool segment_begun = false;
  809. HashMap<uint64_t, bool> has_comp_alpha;
  810. public:
  811. virtual void begin_segment(uint32_t p_frame_index, uint32_t p_frames_drawn) override final;
  812. virtual void end_segment() override final;
  813. /**************/
  814. /**** MISC ****/
  815. /**************/
  816. virtual void set_object_name(ObjectType p_type, ID p_driver_id, const String &p_name) override final;
  817. virtual uint64_t get_resource_native_handle(DriverResource p_type, ID p_driver_id) override final;
  818. virtual uint64_t get_total_memory_used() override final;
  819. virtual uint64_t limit_get(Limit p_limit) override final;
  820. virtual uint64_t api_trait_get(ApiTrait p_trait) override final;
  821. virtual bool has_feature(Features p_feature) override final;
  822. virtual const MultiviewCapabilities &get_multiview_capabilities() override final;
  823. virtual String get_api_name() const override final;
  824. virtual String get_api_version() const override final;
  825. virtual String get_pipeline_cache_uuid() const override final;
  826. virtual const Capabilities &get_capabilities() const override final;
  827. virtual bool is_composite_alpha_supported(CommandQueueID p_queue) const override final;
  828. static bool is_in_developer_mode();
  829. private:
  830. /*********************/
  831. /**** BOOKKEEPING ****/
  832. /*********************/
  833. using VersatileResource = VersatileResourceTemplate<
  834. BufferInfo,
  835. TextureInfo,
  836. TextureInfo,
  837. TextureInfo,
  838. VertexFormatInfo,
  839. CommandBufferInfo,
  840. FramebufferInfo,
  841. ShaderInfo,
  842. UniformSetInfo,
  843. RenderPassInfo,
  844. TimestampQueryPoolInfo>;
  845. PagedAllocator<VersatileResource, true> resources_allocator;
  846. /******************/
  847. public:
  848. RenderingDeviceDriverD3D12(RenderingContextDriverD3D12 *p_context_driver);
  849. virtual ~RenderingDeviceDriverD3D12();
  850. };
  851. #endif // RENDERING_DEVICE_DRIVER_D3D12_H