dolphin/Source/Core/VideoBackends/D3D/D3DBase.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

282 lines
8.2 KiB
C++
Raw Normal View History

// Copyright 2010 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "VideoBackends/D3D/D3DBase.h"
2015-12-12 12:00:08 +00:00
#include <algorithm>
#include <array>
2015-12-12 12:00:08 +00:00
#include "Common/CommonTypes.h"
2019-03-09 13:31:36 +00:00
#include "Common/DynamicLibrary.h"
#include "Common/Logging/Log.h"
#include "Common/MsgHandler.h"
#include "Core/Config/GraphicsSettings.h"
#include "Core/ConfigManager.h"
#include "VideoBackends/D3D/D3DState.h"
#include "VideoBackends/D3D/DXTexture.h"
#include "VideoBackends/D3DCommon/D3DCommon.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"
#include "VideoCommon/VideoConfig.h"
namespace DX11
{
2019-03-09 13:31:36 +00:00
static Common::DynamicLibrary s_d3d11_library;
namespace D3D
{
ComPtr<IDXGIFactory> dxgi_factory;
2019-03-09 13:31:36 +00:00
ComPtr<ID3D11Device> device;
ComPtr<ID3D11Device1> device1;
ComPtr<ID3D11DeviceContext> context;
D3D_FEATURE_LEVEL feature_level;
2019-03-09 13:31:36 +00:00
static ComPtr<ID3D11Debug> s_debug;
constexpr std::array<D3D_FEATURE_LEVEL, 3> s_supported_feature_levels{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
};
2019-03-09 13:31:36 +00:00
bool Create(u32 adapter_index, bool enable_debug_layer)
{
2019-03-09 13:31:36 +00:00
PFN_D3D11_CREATE_DEVICE d3d11_create_device;
if (!s_d3d11_library.Open("d3d11.dll") ||
!s_d3d11_library.GetSymbol("D3D11CreateDevice", &d3d11_create_device))
{
PanicAlertFmtT("Failed to load d3d11.dll");
2019-03-09 13:31:36 +00:00
s_d3d11_library.Close();
return false;
}
2019-03-09 13:31:36 +00:00
if (!D3DCommon::LoadLibraries())
{
2019-03-09 13:31:36 +00:00
s_d3d11_library.Close();
return false;
}
2019-03-09 13:31:36 +00:00
dxgi_factory = D3DCommon::CreateDXGIFactory(enable_debug_layer);
if (!dxgi_factory)
{
PanicAlertFmtT("Failed to create DXGI factory");
2019-03-09 13:31:36 +00:00
D3DCommon::UnloadLibraries();
s_d3d11_library.Close();
return false;
}
2019-03-09 13:31:36 +00:00
ComPtr<IDXGIAdapter> adapter;
HRESULT hr = dxgi_factory->EnumAdapters(adapter_index, adapter.GetAddressOf());
if (FAILED(hr))
{
WARN_LOG_FMT(VIDEO, "Adapter {} not found, using default: {}", adapter_index, DX11HRWrap(hr));
2019-03-09 13:31:36 +00:00
adapter = nullptr;
}
// Creating debug devices can sometimes fail if the user doesn't have the correct
// version of the DirectX SDK. If it does, simply fallback to a non-debug device.
2019-03-09 13:31:36 +00:00
if (enable_debug_layer)
{
hr = d3d11_create_device(
adapter.Get(), D3D_DRIVER_TYPE_UNKNOWN, nullptr, D3D11_CREATE_DEVICE_DEBUG,
s_supported_feature_levels.data(), static_cast<UINT>(s_supported_feature_levels.size()),
D3D11_SDK_VERSION, device.GetAddressOf(), &feature_level, context.GetAddressOf());
2015-02-09 12:00:42 +00:00
// Debugbreak on D3D error
if (SUCCEEDED(hr) && SUCCEEDED(hr = device.As(&s_debug)))
2015-02-09 12:00:42 +00:00
{
2019-03-09 13:31:36 +00:00
ComPtr<ID3D11InfoQueue> info_queue;
if (SUCCEEDED(s_debug.As(&info_queue)))
2015-02-09 12:00:42 +00:00
{
2019-03-09 13:31:36 +00:00
info_queue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_CORRUPTION, true);
info_queue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_ERROR, true);
2015-02-09 12:00:42 +00:00
D3D11_MESSAGE_ID hide[] = {D3D11_MESSAGE_ID_SETPRIVATEDATA_CHANGINGPARAMS};
2015-02-12 13:45:05 +00:00
D3D11_INFO_QUEUE_FILTER filter = {};
2015-02-09 12:00:42 +00:00
filter.DenyList.NumIDs = sizeof(hide) / sizeof(D3D11_MESSAGE_ID);
filter.DenyList.pIDList = hide;
2019-03-09 13:31:36 +00:00
info_queue->AddStorageFilterEntries(&filter);
2015-02-09 12:00:42 +00:00
}
}
2019-03-09 13:31:36 +00:00
else
{
WARN_LOG_FMT(VIDEO, "Debug layer requested but not available: {}", DX11HRWrap(hr));
2019-03-09 13:31:36 +00:00
}
}
2019-03-09 13:31:36 +00:00
if (!enable_debug_layer || FAILED(hr))
{
hr = d3d11_create_device(
adapter.Get(), D3D_DRIVER_TYPE_UNKNOWN, nullptr, 0, s_supported_feature_levels.data(),
static_cast<UINT>(s_supported_feature_levels.size()), D3D11_SDK_VERSION,
device.GetAddressOf(), &feature_level, context.GetAddressOf());
}
2019-03-09 13:31:36 +00:00
if (FAILED(hr))
{
PanicAlertFmtT(
"Failed to initialize Direct3D.\nMake sure your video card supports at least D3D 10.0\n{0}",
DX11HRWrap(hr));
dxgi_factory.Reset();
2019-03-09 13:31:36 +00:00
D3DCommon::UnloadLibraries();
s_d3d11_library.Close();
2019-03-09 13:31:36 +00:00
return false;
}
hr = device.As(&device1);
2017-09-03 06:33:47 +00:00
if (FAILED(hr))
{
WARN_LOG_FMT(VIDEO,
"Missing Direct3D 11.1 support. Logical operations will not be supported.\n{}",
DX11HRWrap(hr));
}
2017-09-03 06:33:47 +00:00
2019-03-29 09:55:00 +00:00
stateman = std::make_unique<StateManager>();
2019-03-09 13:31:36 +00:00
return true;
}
2019-03-09 13:31:36 +00:00
void Destroy()
{
2019-03-29 09:55:00 +00:00
stateman.reset();
context->ClearState();
2019-03-09 13:31:36 +00:00
context->Flush();
2019-03-09 13:31:36 +00:00
context.Reset();
device1.Reset();
2015-02-09 12:00:42 +00:00
2019-03-09 13:31:36 +00:00
auto remaining_references = device.Reset();
if (s_debug)
2015-02-09 12:00:42 +00:00
{
2019-03-09 13:31:36 +00:00
--remaining_references; // the debug interface increases the refcount of the device, subtract
// that.
if (remaining_references)
2015-02-09 12:00:42 +00:00
{
// print out alive objects, but only if we actually have pending references
// note this will also print out internal live objects to the debug console
s_debug->ReportLiveDeviceObjects(D3D11_RLDO_SUMMARY | D3D11_RLDO_DETAIL);
2015-02-09 12:00:42 +00:00
}
2019-03-09 13:31:36 +00:00
s_debug.Reset();
2015-02-09 12:00:42 +00:00
}
2019-03-09 13:31:36 +00:00
if (remaining_references)
ERROR_LOG_FMT(VIDEO, "Unreleased references: {}.", remaining_references);
else
NOTICE_LOG_FMT(VIDEO, "Successfully released all device references!");
dxgi_factory.Reset();
2019-03-09 13:31:36 +00:00
D3DCommon::UnloadLibraries();
s_d3d11_library.Close();
}
2019-03-09 13:31:36 +00:00
std::vector<u32> GetAAModes(u32 adapter_index)
{
2019-03-09 13:31:36 +00:00
// Use temporary device if we don't have one already.
Common::DynamicLibrary temp_lib;
ComPtr<ID3D11Device> temp_device = device;
D3D_FEATURE_LEVEL temp_feature_level = feature_level;
2019-03-09 13:31:36 +00:00
if (!temp_device)
{
ComPtr<IDXGIFactory> temp_dxgi_factory = D3DCommon::CreateDXGIFactory(false);
2019-03-09 13:31:36 +00:00
if (!temp_dxgi_factory)
return {};
2019-03-09 13:31:36 +00:00
ComPtr<IDXGIAdapter> adapter;
temp_dxgi_factory->EnumAdapters(adapter_index, adapter.GetAddressOf());
2019-03-09 13:31:36 +00:00
PFN_D3D11_CREATE_DEVICE d3d11_create_device;
if (!temp_lib.Open("d3d11.dll") ||
!temp_lib.GetSymbol("D3D11CreateDevice", &d3d11_create_device))
{
return {};
}
HRESULT hr = d3d11_create_device(
adapter.Get(), D3D_DRIVER_TYPE_UNKNOWN, nullptr, 0, s_supported_feature_levels.data(),
static_cast<UINT>(s_supported_feature_levels.size()), D3D11_SDK_VERSION,
temp_device.GetAddressOf(), &temp_feature_level, nullptr);
2019-03-09 13:31:36 +00:00
if (FAILED(hr))
return {};
}
2019-03-09 13:31:36 +00:00
// NOTE: D3D 10.0 doesn't support multisampled resources which are bound as depth buffers AND
// shader resources. Thus, we can't have MSAA with 10.0 level hardware.
if (temp_feature_level == D3D_FEATURE_LEVEL_10_0)
2019-03-09 13:31:36 +00:00
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-09 13:31:36 +00:00
std::vector<u32> aa_modes;
for (u32 samples = 1; samples <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; ++samples)
{
2019-03-09 13:31:36 +00:00
UINT quality_levels = 0;
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
if (SUCCEEDED(
temp_device->CheckMultisampleQualityLevels(target_format, samples, &quality_levels)) &&
2019-03-09 13:31:36 +00:00
quality_levels > 0)
{
aa_modes.push_back(samples);
}
}
2019-03-09 13:31:36 +00:00
return aa_modes;
}
bool SupportsTextureFormat(DXGI_FORMAT format)
{
UINT support;
if (FAILED(device->CheckFormatSupport(format, &support)))
return false;
return (support & D3D11_FORMAT_SUPPORT_TEXTURE2D) != 0;
}
bool SupportsLogicOp(u32 adapter_index)
{
// Use temporary device if we don't have one already.
Common::DynamicLibrary temp_lib;
ComPtr<ID3D11Device1> temp_device1 = device1;
if (!device)
{
ComPtr<ID3D11Device> temp_device;
ComPtr<IDXGIFactory> temp_dxgi_factory = D3DCommon::CreateDXGIFactory(false);
if (!temp_dxgi_factory)
return false;
ComPtr<IDXGIAdapter> adapter;
temp_dxgi_factory->EnumAdapters(adapter_index, adapter.GetAddressOf());
PFN_D3D11_CREATE_DEVICE d3d11_create_device;
if (!temp_lib.Open("d3d11.dll") ||
!temp_lib.GetSymbol("D3D11CreateDevice", &d3d11_create_device))
{
return false;
}
HRESULT hr = d3d11_create_device(
adapter.Get(), D3D_DRIVER_TYPE_UNKNOWN, nullptr, 0, s_supported_feature_levels.data(),
static_cast<UINT>(s_supported_feature_levels.size()), D3D11_SDK_VERSION,
temp_device.GetAddressOf(), nullptr, nullptr);
if (FAILED(hr))
return false;
if (FAILED(temp_device.As(&temp_device1)))
return false;
}
if (!temp_device1)
return false;
D3D11_FEATURE_DATA_D3D11_OPTIONS options{};
if (FAILED(temp_device1->CheckFeatureSupport(D3D11_FEATURE_D3D11_OPTIONS, &options,
sizeof(options))))
{
return false;
}
return options.OutputMergerLogicOp != FALSE;
}
} // namespace D3D
} // namespace DX11