| 1 | /**************************************************************************/ |
| 2 | /* rendering_context_driver_d3d12.cpp */ |
| 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 | |
| 31 | #include "rendering_context_driver_d3d12.h" |
| 32 | |
| 33 | #include "d3d12_hooks.h" |
| 34 | |
| 35 | #include "core/config/engine.h" |
| 36 | #include "core/config/project_settings.h" |
| 37 | #include "core/string/ustring.h" |
| 38 | #include "core/templates/local_vector.h" |
| 39 | #include "core/version.h" |
| 40 | #include "servers/rendering/rendering_device.h" |
| 41 | |
| 42 | GODOT_GCC_WARNING_PUSH |
| 43 | GODOT_GCC_WARNING_IGNORE("-Wmissing-field-initializers") |
| 44 | GODOT_GCC_WARNING_IGNORE("-Wnon-virtual-dtor") |
| 45 | GODOT_GCC_WARNING_IGNORE("-Wshadow") |
| 46 | GODOT_GCC_WARNING_IGNORE("-Wswitch") |
| 47 | GODOT_CLANG_WARNING_PUSH |
| 48 | GODOT_CLANG_WARNING_IGNORE("-Wmissing-field-initializers") |
| 49 | GODOT_CLANG_WARNING_IGNORE("-Wnon-virtual-dtor") |
| 50 | GODOT_CLANG_WARNING_IGNORE("-Wstring-plus-int") |
| 51 | GODOT_CLANG_WARNING_IGNORE("-Wswitch") |
| 52 | |
| 53 | #include <dxcapi.h> |
| 54 | #include <dxgi1_6.h> |
| 55 | |
| 56 | GODOT_GCC_WARNING_POP |
| 57 | GODOT_CLANG_WARNING_POP |
| 58 | |
| 59 | #if !defined(_MSC_VER) |
| 60 | #include <guiddef.h> |
| 61 | |
| 62 | #include <thirdparty/directx_headers/include/dxguids/dxguids.h> |
| 63 | #endif |
| 64 | |
| 65 | using Microsoft::WRL::ComPtr; |
| 66 | |
| 67 | // Note: symbols are not available in MinGW and old MSVC import libraries. |
| 68 | // GUID values from https://github.com/microsoft/DirectX-Headers/blob/7a9f4d06911d30eecb56a4956dab29dcca2709ed/include/directx/d3d12.idl#L5877-L5881 |
| 69 | const GUID CLSID_D3D12DebugGodot = { 0xf2352aeb, 0xdd84, 0x49fe, { 0xb9, 0x7b, 0xa9, 0xdc, 0xfd, 0xcc, 0x1b, 0x4f } }; |
| 70 | const GUID CLSID_D3D12SDKConfigurationGodot = { 0x7cda6aca, 0xa03e, 0x49c8, { 0x94, 0x58, 0x03, 0x34, 0xd2, 0x0e, 0x07, 0xce } }; |
| 71 | |
| 72 | #ifdef PIX_ENABLED |
| 73 | #if defined(__GNUC__) |
| 74 | #define _MSC_VER 1800 |
| 75 | #endif |
| 76 | #define USE_PIX |
| 77 | #include <WinPixEventRuntime/pix3.h> |
| 78 | #if defined(__GNUC__) |
| 79 | #undef _MSC_VER |
| 80 | #endif |
| 81 | #endif |
| 82 | |
| 83 | RenderingContextDriverD3D12::RenderingContextDriverD3D12() {} |
| 84 | |
| 85 | RenderingContextDriverD3D12::~RenderingContextDriverD3D12() { |
| 86 | // Let's release manually everything that may still be holding |
| 87 | // onto the DLLs before freeing them. |
| 88 | device_factory.Reset(); |
| 89 | dxgi_factory.Reset(); |
| 90 | |
| 91 | if (lib_d3d12) { |
| 92 | FreeLibrary(lib_d3d12); |
| 93 | } |
| 94 | if (lib_dxgi) { |
| 95 | FreeLibrary(lib_dxgi); |
| 96 | } |
| 97 | #ifdef DCOMP_ENABLED |
| 98 | if (lib_dcomp) { |
| 99 | FreeLibrary(lib_dcomp); |
| 100 | } |
| 101 | #endif |
| 102 | } |
| 103 | |
| 104 | Error RenderingContextDriverD3D12::_init_device_factory() { |
| 105 | uint32_t agility_sdk_version = GLOBAL_GET("rendering/rendering_device/d3d12/agility_sdk_version"); |
| 106 | String agility_sdk_path = String(".\\") + Engine::get_singleton()->get_architecture_name(); |
| 107 | |
| 108 | lib_d3d12 = LoadLibraryW(L"D3D12.dll"); |
| 109 | ERR_FAIL_NULL_V(lib_d3d12, ERR_CANT_CREATE); |
| 110 | |
| 111 | lib_dxgi = LoadLibraryW(L"DXGI.dll"); |
| 112 | ERR_FAIL_NULL_V(lib_dxgi, ERR_CANT_CREATE); |
| 113 | |
| 114 | #ifdef DCOMP_ENABLED |
| 115 | lib_dcomp = LoadLibraryW(L"Dcomp.dll"); |
| 116 | ERR_FAIL_NULL_V(lib_dcomp, ERR_CANT_CREATE); |
| 117 | #endif |
| 118 | |
| 119 | // Note: symbol is not available in MinGW import library. |
| 120 | PFN_D3D12_GET_INTERFACE d3d_D3D12GetInterface = (PFN_D3D12_GET_INTERFACE)(void *)GetProcAddress(lib_d3d12, "D3D12GetInterface"); |
| 121 | if (!d3d_D3D12GetInterface) { |
| 122 | return OK; // Fallback to the system loader. |
| 123 | } |
| 124 | |
| 125 | ComPtr<ID3D12SDKConfiguration1> sdk_config; |
| 126 | HRESULT hr = d3d_D3D12GetInterface(CLSID_D3D12SDKConfigurationGodot, IID_PPV_ARGS(sdk_config.GetAddressOf())); |
| 127 | if (SUCCEEDED(hr)) { |
| 128 | hr = sdk_config->CreateDeviceFactory(agility_sdk_version, agility_sdk_path.ascii().get_data(), IID_PPV_ARGS(device_factory.GetAddressOf())); |
| 129 | if (FAILED(hr)) { |
| 130 | sdk_config->CreateDeviceFactory(agility_sdk_version, ".\\", IID_PPV_ARGS(device_factory.GetAddressOf())); |
| 131 | } |
| 132 | // If both calls failed, device factory is going to be nullptr, and D3D12CreateDevice is going to be used as fallback. |
| 133 | } |
| 134 | return OK; |
| 135 | } |
| 136 | |
| 137 | Error RenderingContextDriverD3D12::_initialize_debug_layers() { |
| 138 | ComPtr<ID3D12Debug> debug_controller; |
| 139 | HRESULT res; |
| 140 | |
| 141 | if (device_factory) { |
| 142 | res = device_factory->GetConfigurationInterface(CLSID_D3D12DebugGodot, IID_PPV_ARGS(&debug_controller)); |
| 143 | } else { |
| 144 | PFN_D3D12_GET_DEBUG_INTERFACE d3d_D3D12GetDebugInterface = (PFN_D3D12_GET_DEBUG_INTERFACE)(void *)GetProcAddress(lib_d3d12, "D3D12GetDebugInterface"); |
| 145 | ERR_FAIL_NULL_V(d3d_D3D12GetDebugInterface, ERR_CANT_CREATE); |
| 146 | |
| 147 | res = d3d_D3D12GetDebugInterface(IID_PPV_ARGS(&debug_controller)); |
| 148 | } |
| 149 | |
| 150 | ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_QUERY_FAILED); |
| 151 | debug_controller->EnableDebugLayer(); |
| 152 | return OK; |
| 153 | } |
| 154 | |
| 155 | Error RenderingContextDriverD3D12::_initialize_devices() { |
| 156 | const UINT dxgi_factory_flags = use_validation_layers() ? DXGI_CREATE_FACTORY_DEBUG : 0; |
| 157 | |
| 158 | typedef HRESULT(WINAPI * PFN_DXGI_CREATE_DXGI_FACTORY2)(UINT, REFIID, void **); |
| 159 | PFN_DXGI_CREATE_DXGI_FACTORY2 dxgi_CreateDXGIFactory2 = (PFN_DXGI_CREATE_DXGI_FACTORY2)(void *)GetProcAddress(lib_dxgi, "CreateDXGIFactory2"); |
| 160 | ERR_FAIL_NULL_V(dxgi_CreateDXGIFactory2, ERR_CANT_CREATE); |
| 161 | |
| 162 | HRESULT res = dxgi_CreateDXGIFactory2(dxgi_factory_flags, IID_PPV_ARGS(&dxgi_factory)); |
| 163 | ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE); |
| 164 | |
| 165 | // Enumerate all possible adapters. |
| 166 | LocalVector<IDXGIAdapter1 *> adapters; |
| 167 | IDXGIAdapter1 *adapter = nullptr; |
| 168 | do { |
| 169 | adapter = create_adapter(adapters.size()); |
| 170 | if (adapter != nullptr) { |
| 171 | adapters.push_back(adapter); |
| 172 | } |
| 173 | } while (adapter != nullptr); |
| 174 | |
| 175 | ERR_FAIL_COND_V_MSG(adapters.is_empty(), ERR_CANT_CREATE, "Adapters enumeration reported zero accessible devices."); |
| 176 | |
| 177 | // Fill the device descriptions with the adapters. |
| 178 | driver_devices.resize(adapters.size()); |
| 179 | for (uint32_t i = 0; i < adapters.size(); ++i) { |
| 180 | DXGI_ADAPTER_DESC1 desc = {}; |
| 181 | adapters[i]->GetDesc1(&desc); |
| 182 | |
| 183 | Device &device = driver_devices[i]; |
| 184 | device.name = desc.Description; |
| 185 | device.vendor = desc.VendorId; |
| 186 | device.workarounds = Workarounds(); |
| 187 | |
| 188 | if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) { |
| 189 | device.type = DEVICE_TYPE_CPU; |
| 190 | } else { |
| 191 | const bool has_dedicated_vram = desc.DedicatedVideoMemory > 0; |
| 192 | device.type = has_dedicated_vram ? DEVICE_TYPE_DISCRETE_GPU : DEVICE_TYPE_INTEGRATED_GPU; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // Release all created adapters. |
| 197 | for (uint32_t i = 0; i < adapters.size(); ++i) { |
| 198 | adapters[i]->Release(); |
| 199 | } |
| 200 | |
| 201 | ComPtr<IDXGIFactory5> factory_5; |
| 202 | dxgi_factory.As(&factory_5); |
| 203 | if (factory_5 != nullptr) { |
| 204 | // The type is important as in general, sizeof(bool) != sizeof(BOOL). |
| 205 | BOOL feature_supported = FALSE; |
| 206 | res = factory_5->CheckFeatureSupport(DXGI_FEATURE_PRESENT_ALLOW_TEARING, &feature_supported, sizeof(feature_supported)); |
| 207 | if (SUCCEEDED(res)) { |
| 208 | tearing_supported = feature_supported; |
| 209 | } else { |
| 210 | ERR_PRINT("CheckFeatureSupport failed with error " + vformat("0x%08ux", (uint64_t)res) + "."); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | return OK; |
| 215 | } |
| 216 | |
| 217 | bool RenderingContextDriverD3D12::use_validation_layers() const { |
| 218 | return Engine::get_singleton()->is_validation_layers_enabled(); |
| 219 | } |
| 220 | |
| 221 | Error RenderingContextDriverD3D12::initialize() { |
| 222 | Error err = _init_device_factory(); |
| 223 | ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE); |
| 224 | |
| 225 | if (use_validation_layers()) { |
| 226 | err = _initialize_debug_layers(); |
| 227 | ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE); |
| 228 | } |
| 229 | |
| 230 | err = _initialize_devices(); |
| 231 | ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE); |
| 232 | |
| 233 | return OK; |
| 234 | } |
| 235 | |
| 236 | const RenderingContextDriver::Device &RenderingContextDriverD3D12::device_get(uint32_t p_device_index) const { |
| 237 | DEV_ASSERT(p_device_index < driver_devices.size()); |
| 238 | return driver_devices[p_device_index]; |
| 239 | } |
| 240 | |
| 241 | uint32_t RenderingContextDriverD3D12::device_get_count() const { |
| 242 | return driver_devices.size(); |
| 243 | } |
| 244 | |
| 245 | bool RenderingContextDriverD3D12::device_supports_present(uint32_t p_device_index, SurfaceID p_surface) const { |
| 246 | // All devices should support presenting to any surface. |
| 247 | return true; |
| 248 | } |
| 249 | |
| 250 | RenderingDeviceDriver *RenderingContextDriverD3D12::driver_create() { |
| 251 | return memnew(RenderingDeviceDriverD3D12(this)); |
| 252 | } |
| 253 | |
| 254 | void RenderingContextDriverD3D12::driver_free(RenderingDeviceDriver *p_driver) { |
| 255 | memdelete(p_driver); |
| 256 | } |
| 257 | |
| 258 | RenderingContextDriver::SurfaceID RenderingContextDriverD3D12::surface_create(const void *p_platform_data) { |
| 259 | const WindowPlatformData *wpd = (const WindowPlatformData *)(p_platform_data); |
| 260 | Surface *surface = memnew(Surface); |
| 261 | surface->hwnd = wpd->window; |
| 262 | return SurfaceID(surface); |
| 263 | } |
| 264 | |
| 265 | void RenderingContextDriverD3D12::surface_set_size(SurfaceID p_surface, uint32_t p_width, uint32_t p_height) { |
| 266 | Surface *surface = (Surface *)(p_surface); |
| 267 | surface->width = p_width; |
| 268 | surface->height = p_height; |
| 269 | surface->needs_resize = true; |
| 270 | } |
| 271 | |
| 272 | void RenderingContextDriverD3D12::surface_set_vsync_mode(SurfaceID p_surface, DisplayServer::VSyncMode p_vsync_mode) { |
| 273 | Surface *surface = (Surface *)(p_surface); |
| 274 | surface->vsync_mode = p_vsync_mode; |
| 275 | surface->needs_resize = true; |
| 276 | } |
| 277 | |
| 278 | DisplayServer::VSyncMode RenderingContextDriverD3D12::surface_get_vsync_mode(SurfaceID p_surface) const { |
| 279 | Surface *surface = (Surface *)(p_surface); |
| 280 | return surface->vsync_mode; |
| 281 | } |
| 282 | |
| 283 | uint32_t RenderingContextDriverD3D12::surface_get_width(SurfaceID p_surface) const { |
| 284 | Surface *surface = (Surface *)(p_surface); |
| 285 | return surface->width; |
| 286 | } |
| 287 | |
| 288 | uint32_t RenderingContextDriverD3D12::surface_get_height(SurfaceID p_surface) const { |
| 289 | Surface *surface = (Surface *)(p_surface); |
| 290 | return surface->height; |
| 291 | } |
| 292 | |
| 293 | void RenderingContextDriverD3D12::surface_set_needs_resize(SurfaceID p_surface, bool p_needs_resize) { |
| 294 | Surface *surface = (Surface *)(p_surface); |
| 295 | surface->needs_resize = p_needs_resize; |
| 296 | } |
| 297 | |
| 298 | bool RenderingContextDriverD3D12::surface_get_needs_resize(SurfaceID p_surface) const { |
| 299 | Surface *surface = (Surface *)(p_surface); |
| 300 | return surface->needs_resize; |
| 301 | } |
| 302 | |
| 303 | void RenderingContextDriverD3D12::surface_destroy(SurfaceID p_surface) { |
| 304 | Surface *surface = (Surface *)(p_surface); |
| 305 | memdelete(surface); |
| 306 | } |
| 307 | |
| 308 | bool RenderingContextDriverD3D12::is_debug_utils_enabled() const { |
| 309 | #ifdef PIX_ENABLED |
| 310 | return true; |
| 311 | #else |
| 312 | return false; |
| 313 | #endif |
| 314 | } |
| 315 | |
| 316 | IDXGIAdapter1 *RenderingContextDriverD3D12::create_adapter(uint32_t p_adapter_index) const { |
| 317 | ComPtr<IDXGIFactory6> factory_6; |
| 318 | dxgi_factory.As(&factory_6); |
| 319 | |
| 320 | // TODO: Use IDXCoreAdapterList, which gives more comprehensive information. |
| 321 | IDXGIAdapter1 *adapter = nullptr; |
| 322 | if (factory_6) { |
| 323 | if (factory_6->EnumAdapterByGpuPreference(p_adapter_index, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, IID_PPV_ARGS(&adapter)) == DXGI_ERROR_NOT_FOUND) { |
| 324 | return nullptr; |
| 325 | } |
| 326 | } else { |
| 327 | if (dxgi_factory->EnumAdapters1(p_adapter_index, &adapter) == DXGI_ERROR_NOT_FOUND) { |
| 328 | return nullptr; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | return adapter; |
| 333 | } |
| 334 | |
| 335 | ID3D12DeviceFactory *RenderingContextDriverD3D12::device_factory_get() const { |
| 336 | return device_factory.Get(); |
| 337 | } |
| 338 | |
| 339 | IDXGIFactory2 *RenderingContextDriverD3D12::dxgi_factory_get() const { |
| 340 | return dxgi_factory.Get(); |
| 341 | } |
| 342 | |
| 343 | bool RenderingContextDriverD3D12::get_tearing_supported() const { |
| 344 | return tearing_supported; |
| 345 | } |