| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <array> |
| 7 |
#include <memory> |
| 8 |
|
| 9 |
#include "Common/CommonTypes.h" |
| 10 |
#include "Common/HRWrap.h" |
| 11 |
|
| 12 |
#include "VideoBackends/D3D12/Common.h" |
| 13 |
#include "VideoBackends/D3D12/D3D12StreamBuffer.h" |
| 14 |
#include "VideoBackends/D3D12/DescriptorAllocator.h" |
| 15 |
#include "VideoBackends/D3D12/DescriptorHeapManager.h" |
| 16 |
|
| 17 |
struct IDXGIFactory; |
| 18 |
|
| 19 |
namespace DX12 |
| 20 |
{ |
| 21 |
|
| 22 |
enum ROOT_PARAMETER |
| 23 |
{ |
| 24 |
ROOT_PARAMETER_PS_CBV, |
| 25 |
ROOT_PARAMETER_PS_SRV, |
| 26 |
ROOT_PARAMETER_PS_SAMPLERS, |
| 27 |
ROOT_PARAMETER_VS_CBV, |
| 28 |
ROOT_PARAMETER_VS_CBV2, |
| 29 |
ROOT_PARAMETER_VS_CUS_CBV, |
| 30 |
ROOT_PARAMETER_GS_CBV, |
| 31 |
ROOT_PARAMETER_VS_SRV, |
| 32 |
ROOT_PARAMETER_BASE_VERTEX_CONSTANT, |
| 33 |
ROOT_PARAMETER_PS_CUS_CBV, |
| 34 |
ROOT_PARAMETER_PS_UAV_OR_CBV2, |
| 35 |
ROOT_PARAMETER_PS_CBV2, |
| 36 |
NUM_ROOT_PARAMETERS |
| 37 |
}; |
| 38 |
|
| 39 |
enum CS_ROOT_PARAMETERS |
| 40 |
{ |
| 41 |
CS_ROOT_PARAMETER_CBV, |
| 42 |
CS_ROOT_PARAMETER_SRV, |
| 43 |
CS_ROOT_PARAMETER_SAMPLERS, |
| 44 |
CS_ROOT_PARAMETER_UAV, |
| 45 |
NUM_CS_ROOT_PARAMETERS, |
| 46 |
}; |
| 47 |
|
| 48 |
class DXContext |
| 49 |
{ |
| 50 |
public: |
| 51 |
~DXContext(); |
| 52 |
|
| 53 |
|
| 54 |
static std::vector<u32> GetAAModes(u32 adapter_index); |
| 55 |
|
| 56 |
|
| 57 |
static bool Create(u32 adapter_index, bool enable_debug_layer); |
| 58 |
|
| 59 |
|
| 60 |
static void Destroy(); |
| 61 |
|
| 62 |
IDXGIFactory* GetDXGIFactory() const { return m_dxgi_factory.Get(); } |
| 63 |
ID3D12Device* GetDevice() const { return m_device.Get(); } |
| 64 |
ID3D12CommandQueue* GetCommandQueue() const { return m_command_queue.Get(); } |
| 65 |
|
| 66 |
|
| 67 |
ID3D12GraphicsCommandList* GetCommandList() const |
| 68 |
{ |
| 69 |
return m_command_lists[m_current_command_list].command_list.Get(); |
| 70 |
} |
| 71 |
DescriptorAllocator* GetDescriptorAllocator() |
| 72 |
{ |
| 73 |
return &m_command_lists[m_current_command_list].descriptor_allocator; |
| 74 |
} |
| 75 |
SamplerAllocator* GetSamplerAllocator() |
| 76 |
{ |
| 77 |
return &m_command_lists[m_current_command_list].sampler_allocator; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
DescriptorHeapManager& GetDescriptorHeapManager() { return m_descriptor_heap_manager; } |
| 82 |
DescriptorHeapManager& GetRTVHeapManager() { return m_rtv_heap_manager; } |
| 83 |
DescriptorHeapManager& GetDSVHeapManager() { return m_dsv_heap_manager; } |
| 84 |
SamplerHeapManager& GetSamplerHeapManager() { return m_sampler_heap_manager; } |
| 85 |
ID3D12DescriptorHeap* const* GetGPUDescriptorHeaps() const |
| 86 |
{ |
| 87 |
return m_gpu_descriptor_heaps.data(); |
| 88 |
} |
| 89 |
u32 GetGPUDescriptorHeapCount() const { return static_cast<u32>(m_gpu_descriptor_heaps.size()); } |
| 90 |
const DescriptorHandle& GetNullSRVDescriptor() const { return m_null_srv_descriptor; } |
| 91 |
|
| 92 |
|
| 93 |
ID3D12RootSignature* GetGXRootSignature() const { return m_gx_root_signature.Get(); } |
| 94 |
ID3D12RootSignature* GetUtilityRootSignature() const { return m_utility_root_signature.Get(); } |
| 95 |
ID3D12RootSignature* GetComputeRootSignature() const { return m_compute_root_signature.Get(); } |
| 96 |
|
| 97 |
|
| 98 |
u64 GetCurrentFenceValue() const { return m_current_fence_value; } |
| 99 |
|
| 100 |
|
| 101 |
u64 GetCompletedFenceValue() const { return m_completed_fence_value; } |
| 102 |
|
| 103 |
|
| 104 |
StreamBuffer& GetTextureUploadBuffer() { return m_texture_upload_buffer; } |
| 105 |
|
| 106 |
|
| 107 |
D3D_FEATURE_LEVEL GetFeatureLevel() const { return m_feature_level; } |
| 108 |
|
| 109 |
|
| 110 |
bool SupportsTextureFormat(DXGI_FORMAT format); |
| 111 |
|
| 112 |
|
| 113 |
bool CreateGlobalResources(); |
| 114 |
|
| 115 |
|
| 116 |
void ExecuteCommandList(bool wait_for_completion); |
| 117 |
|
| 118 |
|
| 119 |
void WaitForFence(u64 fence); |
| 120 |
|
| 121 |
|
| 122 |
void DeferResourceDestruction(ID3D12Resource* resource); |
| 123 |
|
| 124 |
|
| 125 |
void DeferDescriptorDestruction(DescriptorHeapManager& manager, u32 index); |
| 126 |
|
| 127 |
|
| 128 |
void ResetSamplerAllocators(); |
| 129 |
|
| 130 |
|
| 131 |
void RecreateGXRootSignature(); |
| 132 |
|
| 133 |
private: |
| 134 |
|
| 135 |
static const u32 NUM_COMMAND_LISTS = 3; |
| 136 |
|
| 137 |
|
| 138 |
static const u32 TEXTURE_UPLOAD_BUFFER_SIZE = 32 * 1024 * 1024; |
| 139 |
|
| 140 |
struct CommandListResources |
| 141 |
{ |
| 142 |
ComPtr<ID3D12CommandAllocator> command_allocator; |
| 143 |
ComPtr<ID3D12GraphicsCommandList> command_list; |
| 144 |
DescriptorAllocator descriptor_allocator; |
| 145 |
SamplerAllocator sampler_allocator; |
| 146 |
std::vector<ID3D12Resource*> pending_resources; |
| 147 |
std::vector<std::pair<DescriptorHeapManager&, u32>> pending_descriptors; |
| 148 |
u64 ready_fence_value = 0; |
| 149 |
}; |
| 150 |
|
| 151 |
DXContext(); |
| 152 |
|
| 153 |
bool CreateDXGIFactory(bool enable_debug_layer); |
| 154 |
bool CreateDevice(u32 adapter_index, bool enable_debug_layer); |
| 155 |
bool CreateCommandQueue(); |
| 156 |
bool CreateFence(); |
| 157 |
bool CreateDescriptorHeaps(); |
| 158 |
bool CreateRootSignatures(); |
| 159 |
bool CreateGXRootSignature(); |
| 160 |
bool CreateUtilityRootSignature(); |
| 161 |
bool CreateComputeRootSignature(); |
| 162 |
bool CreateTextureUploadBuffer(); |
| 163 |
bool CreateCommandLists(); |
| 164 |
void MoveToNextCommandList(); |
| 165 |
void DestroyPendingResources(CommandListResources& cmdlist); |
| 166 |
|
| 167 |
ComPtr<IDXGIFactory> m_dxgi_factory; |
| 168 |
ComPtr<ID3D12Debug> m_debug_interface; |
| 169 |
ComPtr<ID3D12Device> m_device; |
| 170 |
ComPtr<ID3D12CommandQueue> m_command_queue; |
| 171 |
|
| 172 |
ComPtr<ID3D12Fence> m_fence = nullptr; |
| 173 |
HANDLE m_fence_event = {}; |
| 174 |
u32 m_current_fence_value = 0; |
| 175 |
u64 m_completed_fence_value = 0; |
| 176 |
|
| 177 |
std::array<CommandListResources, NUM_COMMAND_LISTS> m_command_lists; |
| 178 |
u32 m_current_command_list = NUM_COMMAND_LISTS - 1; |
| 179 |
|
| 180 |
DescriptorHeapManager m_descriptor_heap_manager; |
| 181 |
DescriptorHeapManager m_rtv_heap_manager; |
| 182 |
DescriptorHeapManager m_dsv_heap_manager; |
| 183 |
SamplerHeapManager m_sampler_heap_manager; |
| 184 |
std::array<ID3D12DescriptorHeap*, 2> m_gpu_descriptor_heaps = {}; |
| 185 |
DescriptorHandle m_null_srv_descriptor; |
| 186 |
D3D_FEATURE_LEVEL m_feature_level = D3D_FEATURE_LEVEL_11_0; |
| 187 |
|
| 188 |
ComPtr<ID3D12RootSignature> m_gx_root_signature; |
| 189 |
ComPtr<ID3D12RootSignature> m_utility_root_signature; |
| 190 |
ComPtr<ID3D12RootSignature> m_compute_root_signature; |
| 191 |
|
| 192 |
StreamBuffer m_texture_upload_buffer; |
| 193 |
}; |
| 194 |
|
| 195 |
extern std::unique_ptr<DXContext> g_dx_context; |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
struct DX12HRWrap |
| 201 |
{ |
| 202 |
constexpr explicit DX12HRWrap(HRESULT hr) : m_hr(hr) {} |
| 203 |
const HRESULT m_hr; |
| 204 |
}; |
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
template <> |
| 209 |
struct fmt::formatter<DX12::DX12HRWrap> |
| 210 |
{ |
| 211 |
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); } |
| 212 |
template <typename FormatContext> |
| 213 |
auto format(const DX12::DX12HRWrap& hr, FormatContext& ctx) const |
| 214 |
{ |
| 215 |
if (hr.m_hr == DXGI_ERROR_DEVICE_REMOVED && DX12::g_dx_context != nullptr && |
| 216 |
DX12::g_dx_context->GetDevice() != nullptr) |
| 217 |
{ |
| 218 |
return fmt::format_to( |
| 219 |
ctx.out(), "{}\nDevice removal reason: {}", Common::HRWrap(hr.m_hr), |
| 220 |
Common::HRWrap(DX12::g_dx_context->GetDevice()->GetDeviceRemovedReason())); |
| 221 |
} |
| 222 |
else |
| 223 |
{ |
| 224 |
return fmt::format_to(ctx.out(), "{}", Common::HRWrap(hr.m_hr)); |
| 225 |
} |
| 226 |
} |
| 227 |
}; |