2019-03-28 10:35:46 +00:00
|
|
|
// Copyright 2019 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2019-03-28 10:35:46 +00:00
|
|
|
|
2020-09-15 13:01:29 +00:00
|
|
|
#include "VideoBackends/D3D12/DX12Context.h"
|
2019-06-01 11:55:09 +00:00
|
|
|
|
2019-03-28 10:35:46 +00:00
|
|
|
#include <algorithm>
|
2019-06-01 11:55:09 +00:00
|
|
|
#include <array>
|
2019-03-28 10:35:46 +00:00
|
|
|
#include <dxgi1_2.h>
|
|
|
|
#include <queue>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "Common/Assert.h"
|
|
|
|
#include "Common/DynamicLibrary.h"
|
|
|
|
#include "Common/StringUtil.h"
|
2021-11-10 23:55:33 +00:00
|
|
|
|
2019-03-28 10:35:46 +00:00
|
|
|
#include "VideoBackends/D3D12/Common.h"
|
2020-09-15 13:01:29 +00:00
|
|
|
#include "VideoBackends/D3D12/D3D12StreamBuffer.h"
|
2019-03-28 10:35:46 +00:00
|
|
|
#include "VideoBackends/D3D12/DescriptorHeapManager.h"
|
Video: implement color correction to match the NTSC and PAL color spaces (and gamma) that GC and Wii targeted.
To further increase the accuracy of the post process phase, I've added (scRGB) HDR support, which is necessary
to fully display the PAL and NTSC-J color spaces, and also to improve the quality of post process texture samplings and
do them in linear space instead of gamma space (which is very important when playing at low resolutions).
For SDR, the quality is also slightly increased, at least if any post process runs, as the buffer is now
R10G10B10A2 (on Vulkan, DX11 and DX12) if supported; previously it was R8G8B8A8 but the alpha bits were wasted.
Gamma correction is arguably the most important thing as Dolphin on Windows outputted in "sRGB" (implicitly)
as that's what Windows expects by default, though sRGB gamma is very different from the gamma commonly used
by video standards dating to the pre HDR era (roughly gamma 2.35).
Additionally, the addition of HDR support (which is pretty straight forward and minimal), added support for
our own custom AutoHDR shaders, which would allow us to achieve decent looking HDR in Dolphin games without
having to use SpecialK or Windows 11 AutoHDR. Both of which don't necessarily play nice with older games
with strongly different and simpler lighting. HDR should also be supported in Linux.
Development of my own AutoHDR shader is almost complete and will come next.
This has been carefully tested and there should be no regression in any of the different features that Dolphin
offers, like multisampling, stereo rendering, other post processes, etc etc.
Fixes: https://bugs.dolphin-emu.org/issues/8941
Co-authored-by: EndlesslyFlowering <EndlesslyFlowering@protonmail.com>
Co-authored-by: Dogway <lin_ares@hotmail.com>
2023-06-10 08:48:05 +00:00
|
|
|
#include "VideoCommon/FramebufferManager.h"
|
2019-03-28 10:35:46 +00:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
|
|
|
|
|
|
|
namespace DX12
|
|
|
|
{
|
|
|
|
std::unique_ptr<DXContext> g_dx_context;
|
|
|
|
|
|
|
|
// Private D3D12 state
|
|
|
|
static Common::DynamicLibrary s_d3d12_library;
|
|
|
|
static PFN_D3D12_CREATE_DEVICE s_d3d12_create_device;
|
|
|
|
static PFN_D3D12_GET_DEBUG_INTERFACE s_d3d12_get_debug_interface;
|
|
|
|
static PFN_D3D12_SERIALIZE_ROOT_SIGNATURE s_d3d12_serialize_root_signature;
|
|
|
|
|
|
|
|
DXContext::DXContext() = default;
|
|
|
|
|
|
|
|
DXContext::~DXContext()
|
|
|
|
{
|
|
|
|
if (m_fence_event)
|
|
|
|
CloseHandle(m_fence_event);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<u32> DXContext::GetAAModes(u32 adapter_index)
|
|
|
|
{
|
|
|
|
// Use a temporary device if we aren't booting.
|
|
|
|
Common::DynamicLibrary temp_lib;
|
|
|
|
ComPtr<ID3D12Device> temp_device = g_dx_context ? g_dx_context->m_device : nullptr;
|
|
|
|
if (!temp_device)
|
|
|
|
{
|
2019-05-12 04:42:16 +00:00
|
|
|
ComPtr<IDXGIFactory> temp_dxgi_factory = D3DCommon::CreateDXGIFactory(false);
|
2019-03-28 10:35:46 +00:00
|
|
|
if (!temp_dxgi_factory)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
ComPtr<IDXGIAdapter> adapter;
|
2023-06-07 23:46:08 +00:00
|
|
|
HRESULT hr = temp_dxgi_factory->EnumAdapters(adapter_index, &adapter);
|
|
|
|
|
|
|
|
if (!SUCCEEDED(hr))
|
|
|
|
return {};
|
2019-03-28 10:35:46 +00:00
|
|
|
|
|
|
|
PFN_D3D12_CREATE_DEVICE d3d12_create_device;
|
|
|
|
if (!temp_lib.Open("d3d12.dll") ||
|
|
|
|
!temp_lib.GetSymbol("D3D12CreateDevice", &d3d12_create_device))
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-06-07 23:46:08 +00:00
|
|
|
hr = d3d12_create_device(adapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&temp_device));
|
2019-03-28 10:35:46 +00:00
|
|
|
if (!SUCCEEDED(hr))
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
Video: implement color correction to match the NTSC and PAL color spaces (and gamma) that GC and Wii targeted.
To further increase the accuracy of the post process phase, I've added (scRGB) HDR support, which is necessary
to fully display the PAL and NTSC-J color spaces, and also to improve the quality of post process texture samplings and
do them in linear space instead of gamma space (which is very important when playing at low resolutions).
For SDR, the quality is also slightly increased, at least if any post process runs, as the buffer is now
R10G10B10A2 (on Vulkan, DX11 and DX12) if supported; previously it was R8G8B8A8 but the alpha bits were wasted.
Gamma correction is arguably the most important thing as Dolphin on Windows outputted in "sRGB" (implicitly)
as that's what Windows expects by default, though sRGB gamma is very different from the gamma commonly used
by video standards dating to the pre HDR era (roughly gamma 2.35).
Additionally, the addition of HDR support (which is pretty straight forward and minimal), added support for
our own custom AutoHDR shaders, which would allow us to achieve decent looking HDR in Dolphin games without
having to use SpecialK or Windows 11 AutoHDR. Both of which don't necessarily play nice with older games
with strongly different and simpler lighting. HDR should also be supported in Linux.
Development of my own AutoHDR shader is almost complete and will come next.
This has been carefully tested and there should be no regression in any of the different features that Dolphin
offers, like multisampling, stereo rendering, other post processes, etc etc.
Fixes: https://bugs.dolphin-emu.org/issues/8941
Co-authored-by: EndlesslyFlowering <EndlesslyFlowering@protonmail.com>
Co-authored-by: Dogway <lin_ares@hotmail.com>
2023-06-10 08:48:05 +00:00
|
|
|
const DXGI_FORMAT target_format =
|
|
|
|
D3DCommon::GetDXGIFormatForAbstractFormat(FramebufferManager::GetEFBColorFormat(), false);
|
2019-03-28 10:35:46 +00:00
|
|
|
std::vector<u32> aa_modes;
|
|
|
|
for (u32 samples = 1; samples < D3D12_MAX_MULTISAMPLE_SAMPLE_COUNT; ++samples)
|
|
|
|
{
|
|
|
|
D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS multisample_quality_levels = {};
|
Video: implement color correction to match the NTSC and PAL color spaces (and gamma) that GC and Wii targeted.
To further increase the accuracy of the post process phase, I've added (scRGB) HDR support, which is necessary
to fully display the PAL and NTSC-J color spaces, and also to improve the quality of post process texture samplings and
do them in linear space instead of gamma space (which is very important when playing at low resolutions).
For SDR, the quality is also slightly increased, at least if any post process runs, as the buffer is now
R10G10B10A2 (on Vulkan, DX11 and DX12) if supported; previously it was R8G8B8A8 but the alpha bits were wasted.
Gamma correction is arguably the most important thing as Dolphin on Windows outputted in "sRGB" (implicitly)
as that's what Windows expects by default, though sRGB gamma is very different from the gamma commonly used
by video standards dating to the pre HDR era (roughly gamma 2.35).
Additionally, the addition of HDR support (which is pretty straight forward and minimal), added support for
our own custom AutoHDR shaders, which would allow us to achieve decent looking HDR in Dolphin games without
having to use SpecialK or Windows 11 AutoHDR. Both of which don't necessarily play nice with older games
with strongly different and simpler lighting. HDR should also be supported in Linux.
Development of my own AutoHDR shader is almost complete and will come next.
This has been carefully tested and there should be no regression in any of the different features that Dolphin
offers, like multisampling, stereo rendering, other post processes, etc etc.
Fixes: https://bugs.dolphin-emu.org/issues/8941
Co-authored-by: EndlesslyFlowering <EndlesslyFlowering@protonmail.com>
Co-authored-by: Dogway <lin_ares@hotmail.com>
2023-06-10 08:48:05 +00:00
|
|
|
multisample_quality_levels.Format = target_format;
|
2019-03-28 10:35:46 +00:00
|
|
|
multisample_quality_levels.SampleCount = samples;
|
|
|
|
|
|
|
|
temp_device->CheckFeatureSupport(D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS,
|
|
|
|
&multisample_quality_levels,
|
|
|
|
sizeof(multisample_quality_levels));
|
|
|
|
|
|
|
|
if (multisample_quality_levels.NumQualityLevels > 0)
|
|
|
|
aa_modes.push_back(samples);
|
|
|
|
}
|
|
|
|
|
|
|
|
return aa_modes;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DXContext::SupportsTextureFormat(DXGI_FORMAT format)
|
|
|
|
{
|
2023-07-22 00:09:40 +00:00
|
|
|
constexpr u32 required = D3D12_FORMAT_SUPPORT1_TEXTURE2D | D3D12_FORMAT_SUPPORT1_TEXTURECUBE |
|
|
|
|
D3D12_FORMAT_SUPPORT1_SHADER_SAMPLE;
|
2019-03-28 10:35:46 +00:00
|
|
|
|
|
|
|
D3D12_FEATURE_DATA_FORMAT_SUPPORT support = {format};
|
|
|
|
return SUCCEEDED(m_device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &support,
|
|
|
|
sizeof(support))) &&
|
|
|
|
(support.Support1 & required) == required;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DXContext::Create(u32 adapter_index, bool enable_debug_layer)
|
|
|
|
{
|
|
|
|
ASSERT(!g_dx_context);
|
|
|
|
if (!s_d3d12_library.Open("d3d12.dll") ||
|
|
|
|
!s_d3d12_library.GetSymbol("D3D12CreateDevice", &s_d3d12_create_device) ||
|
|
|
|
!s_d3d12_library.GetSymbol("D3D12GetDebugInterface", &s_d3d12_get_debug_interface) ||
|
|
|
|
!s_d3d12_library.GetSymbol("D3D12SerializeRootSignature", &s_d3d12_serialize_root_signature))
|
|
|
|
{
|
2020-12-02 18:17:27 +00:00
|
|
|
PanicAlertFmtT("d3d12.dll could not be loaded.");
|
2019-03-28 10:35:46 +00:00
|
|
|
s_d3d12_library.Close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!D3DCommon::LoadLibraries())
|
|
|
|
{
|
|
|
|
s_d3d12_library.Close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_dx_context.reset(new DXContext());
|
|
|
|
if (!g_dx_context->CreateDXGIFactory(enable_debug_layer) ||
|
|
|
|
!g_dx_context->CreateDevice(adapter_index, enable_debug_layer) ||
|
|
|
|
!g_dx_context->CreateCommandQueue() || !g_dx_context->CreateFence())
|
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DXContext::CreateGlobalResources()
|
|
|
|
{
|
|
|
|
return g_dx_context->CreateDescriptorHeaps() && g_dx_context->CreateRootSignatures() &&
|
|
|
|
g_dx_context->CreateTextureUploadBuffer() && g_dx_context->CreateCommandLists();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DXContext::Destroy()
|
|
|
|
{
|
|
|
|
if (g_dx_context)
|
|
|
|
g_dx_context.reset();
|
|
|
|
|
|
|
|
s_d3d12_serialize_root_signature = nullptr;
|
|
|
|
s_d3d12_get_debug_interface = nullptr;
|
|
|
|
s_d3d12_create_device = nullptr;
|
|
|
|
s_d3d12_library.Close();
|
|
|
|
D3DCommon::UnloadLibraries();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DXContext::CreateDXGIFactory(bool enable_debug_layer)
|
|
|
|
{
|
|
|
|
m_dxgi_factory = D3DCommon::CreateDXGIFactory(enable_debug_layer);
|
|
|
|
return m_dxgi_factory != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DXContext::CreateDevice(u32 adapter_index, bool enable_debug_layer)
|
|
|
|
{
|
|
|
|
ComPtr<IDXGIAdapter> adapter;
|
|
|
|
HRESULT hr = m_dxgi_factory->EnumAdapters(adapter_index, &adapter);
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2021-12-12 21:18:39 +00:00
|
|
|
ERROR_LOG_FMT(VIDEO, "Adapter {} not found, using default: {}", adapter_index, DX12HRWrap(hr));
|
2019-03-28 10:35:46 +00:00
|
|
|
adapter = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enabling the debug layer will fail if the Graphics Tools feature is not installed.
|
|
|
|
if (enable_debug_layer)
|
|
|
|
{
|
|
|
|
hr = s_d3d12_get_debug_interface(IID_PPV_ARGS(&m_debug_interface));
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
|
|
|
m_debug_interface->EnableDebugLayer();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-12 21:18:39 +00:00
|
|
|
ERROR_LOG_FMT(VIDEO, "Debug layer requested but not available: {}", DX12HRWrap(hr));
|
2019-03-28 10:35:46 +00:00
|
|
|
enable_debug_layer = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the actual device.
|
|
|
|
hr = s_d3d12_create_device(adapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&m_device));
|
2021-12-12 21:18:39 +00:00
|
|
|
ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to create D3D12 device: {}", DX12HRWrap(hr));
|
2019-03-28 10:35:46 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (enable_debug_layer)
|
|
|
|
{
|
|
|
|
ComPtr<ID3D12InfoQueue> info_queue;
|
2019-07-20 22:17:15 +00:00
|
|
|
if (SUCCEEDED(m_device.As(&info_queue)))
|
2019-03-28 10:35:46 +00:00
|
|
|
{
|
|
|
|
info_queue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_ERROR, TRUE);
|
|
|
|
info_queue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_WARNING, TRUE);
|
|
|
|
|
|
|
|
D3D12_INFO_QUEUE_FILTER filter = {};
|
2019-06-01 11:55:09 +00:00
|
|
|
std::array<D3D12_MESSAGE_ID, 5> id_list{
|
2019-03-28 10:35:46 +00:00
|
|
|
D3D12_MESSAGE_ID_CLEARRENDERTARGETVIEW_MISMATCHINGCLEARVALUE,
|
|
|
|
D3D12_MESSAGE_ID_CLEARDEPTHSTENCILVIEW_MISMATCHINGCLEARVALUE,
|
|
|
|
D3D12_MESSAGE_ID_CREATEGRAPHICSPIPELINESTATE_RENDERTARGETVIEW_NOT_SET,
|
|
|
|
D3D12_MESSAGE_ID_CREATEINPUTLAYOUT_TYPE_MISMATCH,
|
2019-06-01 11:55:09 +00:00
|
|
|
D3D12_MESSAGE_ID_DRAW_EMPTY_SCISSOR_RECTANGLE,
|
|
|
|
};
|
|
|
|
filter.DenyList.NumIDs = static_cast<UINT>(id_list.size());
|
|
|
|
filter.DenyList.pIDList = id_list.data();
|
2019-03-28 10:35:46 +00:00
|
|
|
info_queue->PushStorageFilter(&filter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DXContext::CreateCommandQueue()
|
|
|
|
{
|
|
|
|
const D3D12_COMMAND_QUEUE_DESC queue_desc = {D3D12_COMMAND_LIST_TYPE_DIRECT,
|
|
|
|
D3D12_COMMAND_QUEUE_PRIORITY_NORMAL,
|
|
|
|
D3D12_COMMAND_QUEUE_FLAG_NONE};
|
|
|
|
HRESULT hr = m_device->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&m_command_queue));
|
2021-12-12 21:18:39 +00:00
|
|
|
ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to create command queue: {}", DX12HRWrap(hr));
|
2019-03-28 10:35:46 +00:00
|
|
|
return SUCCEEDED(hr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DXContext::CreateFence()
|
|
|
|
{
|
|
|
|
HRESULT hr =
|
|
|
|
m_device->CreateFence(m_completed_fence_value, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&m_fence));
|
2021-12-12 21:18:39 +00:00
|
|
|
ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to create fence: {}", DX12HRWrap(hr));
|
2019-03-28 10:35:46 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_fence_event = CreateEvent(nullptr, FALSE, FALSE, nullptr);
|
2023-04-15 04:55:53 +00:00
|
|
|
ASSERT_MSG(VIDEO, m_fence_event != nullptr, "Failed to create fence event");
|
2019-03-28 10:35:46 +00:00
|
|
|
if (!m_fence_event)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DXContext::CreateDescriptorHeaps()
|
|
|
|
{
|
|
|
|
static constexpr size_t MAX_SRVS = 16384;
|
|
|
|
static constexpr size_t MAX_RTVS = 8192;
|
|
|
|
static constexpr size_t MAX_DSVS = 128;
|
|
|
|
static constexpr size_t MAX_SAMPLERS = 16384;
|
|
|
|
|
|
|
|
if (!m_descriptor_heap_manager.Create(m_device.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
|
|
|
|
MAX_SRVS) ||
|
|
|
|
!m_rtv_heap_manager.Create(m_device.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_RTV, MAX_RTVS) ||
|
|
|
|
!m_dsv_heap_manager.Create(m_device.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_DSV, MAX_DSVS) ||
|
|
|
|
!m_sampler_heap_manager.Create(m_device.Get(), MAX_SAMPLERS))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_gpu_descriptor_heaps[1] = m_sampler_heap_manager.GetDescriptorHeap();
|
|
|
|
|
|
|
|
// Allocate null SRV descriptor for unbound textures.
|
|
|
|
constexpr D3D12_SHADER_RESOURCE_VIEW_DESC null_srv_desc = {
|
|
|
|
DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_SRV_DIMENSION_TEXTURE2D,
|
|
|
|
D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING};
|
|
|
|
|
|
|
|
if (!m_descriptor_heap_manager.Allocate(&m_null_srv_descriptor))
|
|
|
|
{
|
2020-12-02 18:17:27 +00:00
|
|
|
PanicAlertFmt("Failed to allocate null descriptor");
|
2019-03-28 10:35:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_device->CreateShaderResourceView(nullptr, &null_srv_desc, m_null_srv_descriptor.cpu_handle);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-06-20 07:55:49 +00:00
|
|
|
static void SetRootParamConstant(D3D12_ROOT_PARAMETER* rp, u32 shader_reg, u32 num_values,
|
|
|
|
D3D12_SHADER_VISIBILITY visibility)
|
|
|
|
{
|
|
|
|
rp->ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS;
|
|
|
|
rp->Constants.Num32BitValues = num_values;
|
|
|
|
rp->Constants.ShaderRegister = shader_reg;
|
|
|
|
rp->Constants.RegisterSpace = 0;
|
|
|
|
rp->ShaderVisibility = visibility;
|
|
|
|
}
|
|
|
|
|
2019-03-28 10:35:46 +00:00
|
|
|
static void SetRootParamCBV(D3D12_ROOT_PARAMETER* rp, u32 shader_reg,
|
|
|
|
D3D12_SHADER_VISIBILITY visibility)
|
|
|
|
{
|
|
|
|
rp->ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
|
|
|
|
rp->Descriptor.ShaderRegister = shader_reg;
|
|
|
|
rp->Descriptor.RegisterSpace = 0;
|
|
|
|
rp->ShaderVisibility = visibility;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SetRootParamTable(D3D12_ROOT_PARAMETER* rp, D3D12_DESCRIPTOR_RANGE* dr,
|
|
|
|
D3D12_DESCRIPTOR_RANGE_TYPE rt, u32 start_shader_reg,
|
|
|
|
u32 num_shader_regs, D3D12_SHADER_VISIBILITY visibility)
|
|
|
|
{
|
|
|
|
dr->RangeType = rt;
|
|
|
|
dr->NumDescriptors = num_shader_regs;
|
|
|
|
dr->BaseShaderRegister = start_shader_reg;
|
|
|
|
dr->RegisterSpace = 0;
|
|
|
|
dr->OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
|
|
|
|
|
|
|
|
rp->ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
|
|
|
|
rp->DescriptorTable.pDescriptorRanges = dr;
|
|
|
|
rp->DescriptorTable.NumDescriptorRanges = 1;
|
|
|
|
rp->ShaderVisibility = visibility;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool BuildRootSignature(ID3D12Device* device, ID3D12RootSignature** sig_ptr,
|
|
|
|
const D3D12_ROOT_PARAMETER* params, u32 num_params)
|
|
|
|
{
|
|
|
|
D3D12_ROOT_SIGNATURE_DESC desc = {};
|
|
|
|
desc.pParameters = params;
|
|
|
|
desc.Flags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT |
|
|
|
|
D3D12_ROOT_SIGNATURE_FLAG_DENY_DOMAIN_SHADER_ROOT_ACCESS |
|
|
|
|
D3D12_ROOT_SIGNATURE_FLAG_DENY_HULL_SHADER_ROOT_ACCESS;
|
|
|
|
desc.NumParameters = num_params;
|
|
|
|
|
|
|
|
ComPtr<ID3DBlob> root_signature_blob;
|
|
|
|
ComPtr<ID3DBlob> root_signature_error_blob;
|
|
|
|
|
|
|
|
HRESULT hr = s_d3d12_serialize_root_signature(&desc, D3D_ROOT_SIGNATURE_VERSION_1,
|
|
|
|
&root_signature_blob, &root_signature_error_blob);
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2021-12-12 21:18:39 +00:00
|
|
|
PanicAlertFmt("Failed to serialize root signature: {}\n{}",
|
|
|
|
static_cast<const char*>(root_signature_error_blob->GetBufferPointer()),
|
|
|
|
DX12HRWrap(hr));
|
2019-03-28 10:35:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = device->CreateRootSignature(0, root_signature_blob->GetBufferPointer(),
|
|
|
|
root_signature_blob->GetBufferSize(), IID_PPV_ARGS(sig_ptr));
|
2021-12-12 21:18:39 +00:00
|
|
|
ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to create root signature: {}", DX12HRWrap(hr));
|
2019-03-28 10:35:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DXContext::CreateRootSignatures()
|
|
|
|
{
|
|
|
|
return CreateGXRootSignature() && CreateUtilityRootSignature() && CreateComputeRootSignature();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DXContext::CreateGXRootSignature()
|
|
|
|
{
|
|
|
|
// GX:
|
2022-05-04 05:41:34 +00:00
|
|
|
// - 3 constant buffers (bindings 0-2), 0/1 visible in PS, 2 visible in VS, 1 visible in GS.
|
2023-02-10 06:28:06 +00:00
|
|
|
// - VideoCommon::MAX_PIXEL_SHADER_SAMPLERS textures (visible in PS).
|
|
|
|
// - VideoCommon::MAX_PIXEL_SHADER_SAMPLERS samplers (visible in PS).
|
2019-03-28 10:35:46 +00:00
|
|
|
// - 1 UAV (visible in PS).
|
|
|
|
|
|
|
|
std::array<D3D12_ROOT_PARAMETER, NUM_ROOT_PARAMETERS> params;
|
|
|
|
std::array<D3D12_DESCRIPTOR_RANGE, NUM_ROOT_PARAMETERS> ranges;
|
|
|
|
u32 param_count = 0;
|
|
|
|
SetRootParamCBV(¶ms[param_count], 0, D3D12_SHADER_VISIBILITY_PIXEL);
|
|
|
|
param_count++;
|
|
|
|
SetRootParamTable(¶ms[param_count], &ranges[param_count], D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 0,
|
2023-02-10 06:28:06 +00:00
|
|
|
VideoCommon::MAX_PIXEL_SHADER_SAMPLERS, D3D12_SHADER_VISIBILITY_PIXEL);
|
2019-03-28 10:35:46 +00:00
|
|
|
param_count++;
|
|
|
|
SetRootParamTable(¶ms[param_count], &ranges[param_count], D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER,
|
2023-02-10 06:28:06 +00:00
|
|
|
0, VideoCommon::MAX_PIXEL_SHADER_SAMPLERS, D3D12_SHADER_VISIBILITY_PIXEL);
|
2019-03-28 10:35:46 +00:00
|
|
|
param_count++;
|
|
|
|
SetRootParamCBV(¶ms[param_count], 0, D3D12_SHADER_VISIBILITY_VERTEX);
|
|
|
|
param_count++;
|
2022-05-04 05:41:34 +00:00
|
|
|
SetRootParamCBV(¶ms[param_count], 1, D3D12_SHADER_VISIBILITY_VERTEX);
|
|
|
|
param_count++;
|
2022-10-01 03:12:08 +00:00
|
|
|
if (g_ActiveConfig.UseVSForLinePointExpand())
|
|
|
|
SetRootParamCBV(¶ms[param_count], 2, D3D12_SHADER_VISIBILITY_VERTEX);
|
|
|
|
else
|
|
|
|
SetRootParamCBV(¶ms[param_count], 0, D3D12_SHADER_VISIBILITY_GEOMETRY);
|
2019-03-28 10:35:46 +00:00
|
|
|
param_count++;
|
2022-06-20 07:55:49 +00:00
|
|
|
SetRootParamTable(¶ms[param_count], &ranges[param_count], D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 3,
|
|
|
|
1, D3D12_SHADER_VISIBILITY_VERTEX);
|
|
|
|
param_count++;
|
2022-07-23 05:47:04 +00:00
|
|
|
SetRootParamConstant(¶ms[param_count], 3, 1, D3D12_SHADER_VISIBILITY_VERTEX);
|
2022-06-20 07:55:49 +00:00
|
|
|
param_count++;
|
2019-03-28 10:35:46 +00:00
|
|
|
|
|
|
|
// Since these must be contiguous, pixel lighting goes to bbox if not enabled.
|
|
|
|
if (g_ActiveConfig.bBBoxEnable)
|
|
|
|
{
|
|
|
|
SetRootParamTable(¶ms[param_count], &ranges[param_count], D3D12_DESCRIPTOR_RANGE_TYPE_UAV,
|
|
|
|
2, 1, D3D12_SHADER_VISIBILITY_PIXEL);
|
|
|
|
param_count++;
|
|
|
|
}
|
|
|
|
if (g_ActiveConfig.bEnablePixelLighting)
|
|
|
|
{
|
|
|
|
SetRootParamCBV(¶ms[param_count], 1, D3D12_SHADER_VISIBILITY_PIXEL);
|
|
|
|
param_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return BuildRootSignature(m_device.Get(), &m_gx_root_signature, params.data(), param_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DXContext::CreateUtilityRootSignature()
|
|
|
|
{
|
|
|
|
// Utility:
|
|
|
|
// - 1 constant buffer (binding 0, visible in VS/PS).
|
|
|
|
// - 8 textures (visible in PS).
|
|
|
|
// - 8 samplers (visible in PS).
|
|
|
|
|
|
|
|
std::array<D3D12_ROOT_PARAMETER, NUM_ROOT_PARAMETERS> params;
|
|
|
|
std::array<D3D12_DESCRIPTOR_RANGE, NUM_ROOT_PARAMETERS> ranges;
|
|
|
|
SetRootParamCBV(¶ms[ROOT_PARAMETER_PS_CBV], 0, D3D12_SHADER_VISIBILITY_ALL);
|
|
|
|
SetRootParamTable(¶ms[ROOT_PARAMETER_PS_SRV], &ranges[ROOT_PARAMETER_PS_SRV],
|
|
|
|
D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 0, 8, D3D12_SHADER_VISIBILITY_PIXEL);
|
|
|
|
SetRootParamTable(¶ms[ROOT_PARAMETER_PS_SAMPLERS], &ranges[ROOT_PARAMETER_PS_SAMPLERS],
|
|
|
|
D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER, 0, 8, D3D12_SHADER_VISIBILITY_PIXEL);
|
|
|
|
return BuildRootSignature(m_device.Get(), &m_utility_root_signature, params.data(), 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DXContext::CreateComputeRootSignature()
|
|
|
|
{
|
|
|
|
// Compute:
|
|
|
|
// - 1 constant buffer (binding 0).
|
|
|
|
// - 8 textures.
|
|
|
|
// - 8 samplers.
|
|
|
|
// - 1 UAV.
|
|
|
|
|
|
|
|
std::array<D3D12_ROOT_PARAMETER, NUM_ROOT_PARAMETERS> params;
|
|
|
|
std::array<D3D12_DESCRIPTOR_RANGE, NUM_ROOT_PARAMETERS> ranges;
|
|
|
|
SetRootParamCBV(¶ms[CS_ROOT_PARAMETER_CBV], 0, D3D12_SHADER_VISIBILITY_ALL);
|
|
|
|
SetRootParamTable(¶ms[CS_ROOT_PARAMETER_SRV], &ranges[CS_ROOT_PARAMETER_CBV],
|
|
|
|
D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 0, 8, D3D12_SHADER_VISIBILITY_ALL);
|
|
|
|
SetRootParamTable(¶ms[CS_ROOT_PARAMETER_SAMPLERS], &ranges[CS_ROOT_PARAMETER_SAMPLERS],
|
|
|
|
D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER, 0, 8, D3D12_SHADER_VISIBILITY_ALL);
|
|
|
|
SetRootParamTable(¶ms[CS_ROOT_PARAMETER_UAV], &ranges[CS_ROOT_PARAMETER_UAV],
|
|
|
|
D3D12_DESCRIPTOR_RANGE_TYPE_UAV, 0, 1, D3D12_SHADER_VISIBILITY_ALL);
|
|
|
|
return BuildRootSignature(m_device.Get(), &m_compute_root_signature, params.data(), 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DXContext::CreateTextureUploadBuffer()
|
|
|
|
{
|
|
|
|
if (!m_texture_upload_buffer.AllocateBuffer(TEXTURE_UPLOAD_BUFFER_SIZE))
|
|
|
|
{
|
2020-12-02 18:17:27 +00:00
|
|
|
PanicAlertFmt("Failed to create texture upload buffer");
|
2019-03-28 10:35:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DXContext::CreateCommandLists()
|
|
|
|
{
|
|
|
|
static constexpr size_t MAX_DRAWS_PER_FRAME = 8192;
|
|
|
|
static constexpr size_t TEMPORARY_SLOTS = MAX_DRAWS_PER_FRAME * 8;
|
|
|
|
|
|
|
|
for (u32 i = 0; i < NUM_COMMAND_LISTS; i++)
|
|
|
|
{
|
|
|
|
CommandListResources& res = m_command_lists[i];
|
|
|
|
HRESULT hr = m_device->CreateCommandAllocator(
|
|
|
|
D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(res.command_allocator.GetAddressOf()));
|
2021-12-12 21:18:39 +00:00
|
|
|
ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to create command allocator: {}", DX12HRWrap(hr));
|
2019-03-28 10:35:46 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
hr = m_device->CreateCommandList(1, D3D12_COMMAND_LIST_TYPE_DIRECT, res.command_allocator.Get(),
|
|
|
|
nullptr, IID_PPV_ARGS(res.command_list.GetAddressOf()));
|
2021-12-12 21:18:39 +00:00
|
|
|
ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to create command list: {}", DX12HRWrap(hr));
|
2019-03-28 10:35:46 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the command list, since the first thing we do is reset them.
|
|
|
|
hr = res.command_list->Close();
|
2021-12-12 21:18:39 +00:00
|
|
|
ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Closing new command list failed: {}", DX12HRWrap(hr));
|
2019-03-28 10:35:46 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!res.descriptor_allocator.Create(m_device.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
|
|
|
|
TEMPORARY_SLOTS) ||
|
|
|
|
!res.sampler_allocator.Create(m_device.Get()))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MoveToNextCommandList();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DXContext::MoveToNextCommandList()
|
|
|
|
{
|
|
|
|
m_current_command_list = (m_current_command_list + 1) % NUM_COMMAND_LISTS;
|
|
|
|
m_current_fence_value++;
|
|
|
|
|
|
|
|
// We may have to wait if this command list hasn't finished on the GPU.
|
|
|
|
CommandListResources& res = m_command_lists[m_current_command_list];
|
|
|
|
WaitForFence(res.ready_fence_value);
|
|
|
|
|
|
|
|
// Begin command list.
|
|
|
|
res.command_allocator->Reset();
|
|
|
|
res.command_list->Reset(res.command_allocator.Get(), nullptr);
|
|
|
|
res.descriptor_allocator.Reset();
|
|
|
|
if (res.sampler_allocator.ShouldReset())
|
|
|
|
res.sampler_allocator.Reset();
|
|
|
|
m_gpu_descriptor_heaps[0] = res.descriptor_allocator.GetDescriptorHeap();
|
|
|
|
m_gpu_descriptor_heaps[1] = res.sampler_allocator.GetDescriptorHeap();
|
|
|
|
res.ready_fence_value = m_current_fence_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DXContext::ExecuteCommandList(bool wait_for_completion)
|
|
|
|
{
|
|
|
|
CommandListResources& res = m_command_lists[m_current_command_list];
|
|
|
|
|
|
|
|
// Close and queue command list.
|
|
|
|
HRESULT hr = res.command_list->Close();
|
2021-12-12 21:18:39 +00:00
|
|
|
ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to close command list: {}", DX12HRWrap(hr));
|
2019-06-01 11:55:09 +00:00
|
|
|
const std::array<ID3D12CommandList*, 1> execute_lists{res.command_list.Get()};
|
|
|
|
m_command_queue->ExecuteCommandLists(static_cast<UINT>(execute_lists.size()),
|
|
|
|
execute_lists.data());
|
2019-03-28 10:35:46 +00:00
|
|
|
|
|
|
|
// Update fence when GPU has completed.
|
|
|
|
hr = m_command_queue->Signal(m_fence.Get(), m_current_fence_value);
|
2021-11-10 23:55:33 +00:00
|
|
|
|
2021-12-12 21:18:39 +00:00
|
|
|
ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to signal fence: {}", DX12HRWrap(hr));
|
2019-03-28 10:35:46 +00:00
|
|
|
|
|
|
|
MoveToNextCommandList();
|
|
|
|
if (wait_for_completion)
|
|
|
|
WaitForFence(res.ready_fence_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DXContext::DeferResourceDestruction(ID3D12Resource* resource)
|
|
|
|
{
|
|
|
|
resource->AddRef();
|
|
|
|
m_command_lists[m_current_command_list].pending_resources.push_back(resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DXContext::DeferDescriptorDestruction(DescriptorHeapManager& manager, u32 index)
|
|
|
|
{
|
|
|
|
m_command_lists[m_current_command_list].pending_descriptors.emplace_back(manager, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DXContext::ResetSamplerAllocators()
|
|
|
|
{
|
|
|
|
for (CommandListResources& res : m_command_lists)
|
|
|
|
res.sampler_allocator.Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DXContext::RecreateGXRootSignature()
|
|
|
|
{
|
|
|
|
m_gx_root_signature.Reset();
|
|
|
|
if (!CreateGXRootSignature())
|
2020-12-02 18:17:27 +00:00
|
|
|
PanicAlertFmt("Failed to re-create GX root signature.");
|
2019-03-28 10:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DXContext::DestroyPendingResources(CommandListResources& cmdlist)
|
|
|
|
{
|
|
|
|
for (const auto& dd : cmdlist.pending_descriptors)
|
|
|
|
dd.first.Free(dd.second);
|
|
|
|
cmdlist.pending_descriptors.clear();
|
|
|
|
|
|
|
|
for (ID3D12Resource* res : cmdlist.pending_resources)
|
|
|
|
res->Release();
|
|
|
|
cmdlist.pending_resources.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DXContext::WaitForFence(u64 fence)
|
|
|
|
{
|
|
|
|
if (m_completed_fence_value >= fence)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Try non-blocking check.
|
|
|
|
m_completed_fence_value = m_fence->GetCompletedValue();
|
|
|
|
if (m_completed_fence_value < fence)
|
|
|
|
{
|
|
|
|
// Fall back to event.
|
|
|
|
HRESULT hr = m_fence->SetEventOnCompletion(fence, m_fence_event);
|
2021-12-12 21:18:39 +00:00
|
|
|
ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to set fence event on completion: {}", DX12HRWrap(hr));
|
2019-03-28 10:35:46 +00:00
|
|
|
WaitForSingleObject(m_fence_event, INFINITE);
|
|
|
|
m_completed_fence_value = m_fence->GetCompletedValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release resources for as many command lists which have completed.
|
|
|
|
u32 index = (m_current_command_list + 1) % NUM_COMMAND_LISTS;
|
|
|
|
for (u32 i = 0; i < NUM_COMMAND_LISTS; i++)
|
|
|
|
{
|
|
|
|
CommandListResources& res = m_command_lists[index];
|
|
|
|
if (m_completed_fence_value < res.ready_fence_value)
|
|
|
|
break;
|
|
|
|
|
|
|
|
DestroyPendingResources(res);
|
|
|
|
index = (index + 1) % NUM_COMMAND_LISTS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace DX12
|