| 1 | // Copyright 2010 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoBackends/D3D/D3DBase.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <array> |
| 8 | |
| 9 | #include "Common/CommonTypes.h" |
| 10 | #include "Common/DynamicLibrary.h" |
| 11 | #include "Common/Logging/Log.h" |
| 12 | #include "Common/MsgHandler.h" |
| 13 | #include "Core/Config/GraphicsSettings.h" |
| 14 | #include "Core/ConfigManager.h" |
| 15 | #include "VideoBackends/D3D/D3DState.h" |
| 16 | #include "VideoBackends/D3D/DXTexture.h" |
| 17 | #include "VideoBackends/D3DCommon/D3DCommon.h" |
| 18 | #include "VideoCommon/FramebufferManager.h" |
| 19 | #include "VideoCommon/VideoConfig.h" |
| 20 | |
| 21 | namespace DX11 |
| 22 | { |
| 23 | static Common::DynamicLibrary s_d3d11_library; |
| 24 | namespace D3D |
| 25 | { |
| 26 | ComPtr<IDXGIFactory> dxgi_factory; |
| 27 | ComPtr<ID3D11Device> device; |
| 28 | ComPtr<ID3D11Device1> device1; |
| 29 | ComPtr<ID3D11DeviceContext> context; |
| 30 | D3D_FEATURE_LEVEL feature_level; |
| 31 | |
| 32 | static ComPtr<ID3D11Debug> s_debug; |
| 33 | |
| 34 | constexpr std::array<D3D_FEATURE_LEVEL, 3> s_supported_feature_levels{ |
| 35 | D3D_FEATURE_LEVEL_11_0, |
| 36 | D3D_FEATURE_LEVEL_10_1, |
| 37 | D3D_FEATURE_LEVEL_10_0, |
| 38 | }; |
| 39 | |
| 40 | bool Create(u32 adapter_index, bool enable_debug_layer) |
| 41 | { |
| 42 | PFN_D3D11_CREATE_DEVICE d3d11_create_device; |
| 43 | if (!s_d3d11_library.Open("d3d11.dll") || |
| 44 | !s_d3d11_library.GetSymbol("D3D11CreateDevice", &d3d11_create_device)) |
| 45 | { |
| 46 | PanicAlertFmtT("Failed to load d3d11.dll"); |
| 47 | s_d3d11_library.Close(); |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | if (!D3DCommon::LoadLibraries()) |
| 52 | { |
| 53 | s_d3d11_library.Close(); |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | dxgi_factory = D3DCommon::CreateDXGIFactory(enable_debug_layer); |
| 58 | if (!dxgi_factory) |
| 59 | { |
| 60 | PanicAlertFmtT("Failed to create DXGI factory"); |
| 61 | D3DCommon::UnloadLibraries(); |
| 62 | s_d3d11_library.Close(); |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | ComPtr<IDXGIAdapter> adapter; |
| 67 | HRESULT hr = dxgi_factory->EnumAdapters(adapter_index, &adapter); |
| 68 | if (FAILED(hr)) |
| 69 | { |
| 70 | WARN_LOG_FMT(VIDEO, "Adapter {} not found, using default: {}", adapter_index, DX11HRWrap(hr)); |
| 71 | adapter = nullptr; |
| 72 | } |
| 73 | |
| 74 | // Creating debug devices can sometimes fail if the user doesn't have the correct |
| 75 | // version of the DirectX SDK. If it does, simply fallback to a non-debug device. |
| 76 | if (enable_debug_layer) |
| 77 | { |
| 78 | hr = d3d11_create_device(adapter.Get(), D3D_DRIVER_TYPE_UNKNOWN, nullptr, |
| 79 | D3D11_CREATE_DEVICE_DEBUG, s_supported_feature_levels.data(), |
| 80 | static_cast<UINT>(s_supported_feature_levels.size()), |
| 81 | D3D11_SDK_VERSION, &device, &feature_level, &context); |
| 82 | |
| 83 | // Debugbreak on D3D error |
| 84 | if (SUCCEEDED(hr) && SUCCEEDED(hr = device.As(&s_debug))) |
| 85 | { |
| 86 | ComPtr<ID3D11InfoQueue> info_queue; |
| 87 | if (SUCCEEDED(s_debug.As(&info_queue))) |
| 88 | { |
| 89 | info_queue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_CORRUPTION, true); |
| 90 | info_queue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_ERROR, true); |
| 91 | |
| 92 | D3D11_MESSAGE_ID hide[] = {D3D11_MESSAGE_ID_SETPRIVATEDATA_CHANGINGPARAMS}; |
| 93 | |
| 94 | D3D11_INFO_QUEUE_FILTER filter = {}; |
| 95 | filter.DenyList.NumIDs = sizeof(hide) / sizeof(D3D11_MESSAGE_ID); |
| 96 | filter.DenyList.pIDList = hide; |
| 97 | info_queue->AddStorageFilterEntries(&filter); |
| 98 | } |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | WARN_LOG_FMT(VIDEO, "Debug layer requested but not available: {}", DX11HRWrap(hr)); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (!enable_debug_layer || FAILED(hr)) |
| 107 | { |
| 108 | hr = d3d11_create_device(adapter.Get(), D3D_DRIVER_TYPE_UNKNOWN, nullptr, 0, |
| 109 | s_supported_feature_levels.data(), |
| 110 | static_cast<UINT>(s_supported_feature_levels.size()), |
| 111 | D3D11_SDK_VERSION, &device, &feature_level, &context); |
| 112 | } |
| 113 | |
| 114 | if (FAILED(hr)) |
| 115 | { |
| 116 | PanicAlertFmtT( |
| 117 | "Failed to initialize Direct3D.\nMake sure your video card supports at least D3D 10.0\n{0}", |
| 118 | DX11HRWrap(hr)); |
| 119 | dxgi_factory.Reset(); |
| 120 | D3DCommon::UnloadLibraries(); |
| 121 | s_d3d11_library.Close(); |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | hr = device.As(&device1); |
| 126 | if (FAILED(hr)) |
| 127 | { |
| 128 | WARN_LOG_FMT(VIDEO, |
| 129 | "Missing Direct3D 11.1 support. Logical operations will not be supported.\n{}", |
| 130 | DX11HRWrap(hr)); |
| 131 | } |
| 132 | |
| 133 | stateman = std::make_unique<StateManager>(); |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | void Destroy() |
| 138 | { |
| 139 | stateman.reset(); |
| 140 | |
| 141 | context->ClearState(); |
| 142 | context->Flush(); |
| 143 | |
| 144 | context.Reset(); |
| 145 | device1.Reset(); |
| 146 | |
| 147 | auto remaining_references = device.Reset(); |
| 148 | if (s_debug) |
| 149 | { |
| 150 | --remaining_references; // the debug interface increases the refcount of the device, subtract |
| 151 | // that. |
| 152 | if (remaining_references) |
| 153 | { |
| 154 | // print out alive objects, but only if we actually have pending references |
| 155 | // note this will also print out internal live objects to the debug console |
| 156 | s_debug->ReportLiveDeviceObjects(D3D11_RLDO_SUMMARY | D3D11_RLDO_DETAIL); |
| 157 | } |
| 158 | s_debug.Reset(); |
| 159 | } |
| 160 | |
| 161 | if (remaining_references) |
| 162 | ERROR_LOG_FMT(VIDEO, "Unreleased references: {}.", remaining_references); |
| 163 | else |
| 164 | NOTICE_LOG_FMT(VIDEO, "Successfully released all device references!"); |
| 165 | |
| 166 | dxgi_factory.Reset(); |
| 167 | D3DCommon::UnloadLibraries(); |
| 168 | s_d3d11_library.Close(); |
| 169 | } |
| 170 | |
| 171 | std::vector<u32> GetAAModes(u32 adapter_index) |
| 172 | { |
| 173 | // Use temporary device if we don't have one already. |
| 174 | Common::DynamicLibrary temp_lib; |
| 175 | ComPtr<ID3D11Device> temp_device = device; |
| 176 | D3D_FEATURE_LEVEL temp_feature_level = feature_level; |
| 177 | if (!temp_device) |
| 178 | { |
| 179 | ComPtr<IDXGIFactory> temp_dxgi_factory = D3DCommon::CreateDXGIFactory(false); |
| 180 | if (!temp_dxgi_factory) |
| 181 | return {}; |
| 182 | |
| 183 | ComPtr<IDXGIAdapter> adapter; |
| 184 | temp_dxgi_factory->EnumAdapters(adapter_index, &adapter); |
| 185 | |
| 186 | PFN_D3D11_CREATE_DEVICE d3d11_create_device; |
| 187 | if (!temp_lib.Open("d3d11.dll") || |
| 188 | !temp_lib.GetSymbol("D3D11CreateDevice", &d3d11_create_device)) |
| 189 | { |
| 190 | return {}; |
| 191 | } |
| 192 | |
| 193 | HRESULT hr = d3d11_create_device(adapter.Get(), D3D_DRIVER_TYPE_UNKNOWN, nullptr, 0, |
| 194 | s_supported_feature_levels.data(), |
| 195 | static_cast<UINT>(s_supported_feature_levels.size()), |
| 196 | D3D11_SDK_VERSION, &temp_device, &temp_feature_level, nullptr); |
| 197 | if (FAILED(hr)) |
| 198 | return {}; |
| 199 | } |
| 200 | |
| 201 | // NOTE: D3D 10.0 doesn't support multisampled resources which are bound as depth buffers AND |
| 202 | // shader resources. Thus, we can't have MSAA with 10.0 level hardware. |
| 203 | if (temp_feature_level == D3D_FEATURE_LEVEL_10_0) |
| 204 | return {}; |
| 205 | |
| 206 | const DXGI_FORMAT target_format = |
| 207 | D3DCommon::GetDXGIFormatForAbstractFormat(FramebufferManager::GetEFBColorFormat(), false); |
| 208 | std::vector<u32> aa_modes; |
| 209 | for (u32 samples = 1; samples <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; ++samples) |
| 210 | { |
| 211 | UINT quality_levels = 0; |
| 212 | if (SUCCEEDED( |
| 213 | temp_device->CheckMultisampleQualityLevels(target_format, samples, &quality_levels)) && |
| 214 | quality_levels > 0) |
| 215 | { |
| 216 | aa_modes.push_back(samples); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | return aa_modes; |
| 221 | } |
| 222 | |
| 223 | bool SupportsTextureFormat(DXGI_FORMAT format) |
| 224 | { |
| 225 | UINT support; |
| 226 | if (FAILED(device->CheckFormatSupport(format, &support))) |
| 227 | return false; |
| 228 | |
| 229 | return (support & D3D11_FORMAT_SUPPORT_TEXTURE2D) != 0; |
| 230 | } |
| 231 | |
| 232 | bool SupportsLogicOp(u32 adapter_index) |
| 233 | { |
| 234 | // Use temporary device if we don't have one already. |
| 235 | Common::DynamicLibrary temp_lib; |
| 236 | ComPtr<ID3D11Device1> temp_device1 = device1; |
| 237 | if (!device) |
| 238 | { |
| 239 | ComPtr<ID3D11Device> temp_device; |
| 240 | |
| 241 | ComPtr<IDXGIFactory> temp_dxgi_factory = D3DCommon::CreateDXGIFactory(false); |
| 242 | if (!temp_dxgi_factory) |
| 243 | return false; |
| 244 | |
| 245 | ComPtr<IDXGIAdapter> adapter; |
| 246 | temp_dxgi_factory->EnumAdapters(adapter_index, &adapter); |
| 247 | |
| 248 | PFN_D3D11_CREATE_DEVICE d3d11_create_device; |
| 249 | if (!temp_lib.Open("d3d11.dll") || |
| 250 | !temp_lib.GetSymbol("D3D11CreateDevice", &d3d11_create_device)) |
| 251 | { |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | HRESULT hr = d3d11_create_device(adapter.Get(), D3D_DRIVER_TYPE_UNKNOWN, nullptr, 0, |
| 256 | s_supported_feature_levels.data(), |
| 257 | static_cast<UINT>(s_supported_feature_levels.size()), |
| 258 | D3D11_SDK_VERSION, &temp_device, nullptr, nullptr); |
| 259 | if (FAILED(hr)) |
| 260 | return false; |
| 261 | |
| 262 | if (FAILED(temp_device.As(&temp_device1))) |
| 263 | return false; |
| 264 | } |
| 265 | |
| 266 | if (!temp_device1) |
| 267 | return false; |
| 268 | |
| 269 | D3D11_FEATURE_DATA_D3D11_OPTIONS options{}; |
| 270 | if (FAILED(temp_device1->CheckFeatureSupport(D3D11_FEATURE_D3D11_OPTIONS, &options, |
| 271 | sizeof(options)))) |
| 272 | { |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | return options.OutputMergerLogicOp != FALSE; |
| 277 | } |
| 278 | |
| 279 | } // namespace D3D |
| 280 | |
| 281 | } // namespace DX11 |