rendering_device_driver_d3d12.h 41 KB

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