Path: blob/master/drivers/d3d12/rendering_context_driver_d3d12.cpp
20952 views
/**************************************************************************/1/* rendering_context_driver_d3d12.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "rendering_context_driver_d3d12.h"3132#include "d3d12_hooks.h"3334#include "core/config/engine.h"35#include "core/config/project_settings.h"36#include "core/string/ustring.h"37#include "core/templates/local_vector.h"38#include "core/version.h"39#include "servers/rendering/rendering_device.h"4041GODOT_GCC_WARNING_PUSH42GODOT_GCC_WARNING_IGNORE("-Wmissing-field-initializers")43GODOT_GCC_WARNING_IGNORE("-Wnon-virtual-dtor")44GODOT_GCC_WARNING_IGNORE("-Wshadow")45GODOT_GCC_WARNING_IGNORE("-Wswitch")46GODOT_CLANG_WARNING_PUSH47GODOT_CLANG_WARNING_IGNORE("-Wmissing-field-initializers")48GODOT_CLANG_WARNING_IGNORE("-Wnon-virtual-dtor")49GODOT_CLANG_WARNING_IGNORE("-Wstring-plus-int")50GODOT_CLANG_WARNING_IGNORE("-Wswitch")5152#include <dxcapi.h>53#include <dxgi1_6.h>5455GODOT_GCC_WARNING_POP56GODOT_CLANG_WARNING_POP5758#if !defined(_MSC_VER)59#include <guiddef.h>6061#include <thirdparty/directx_headers/include/dxguids/dxguids.h>62#endif6364using Microsoft::WRL::ComPtr;6566// Note: symbols are not available in MinGW and old MSVC import libraries.67// GUID values from https://github.com/microsoft/DirectX-Headers/blob/7a9f4d06911d30eecb56a4956dab29dcca2709ed/include/directx/d3d12.idl#L5877-L588168const GUID CLSID_D3D12DebugGodot = { 0xf2352aeb, 0xdd84, 0x49fe, { 0xb9, 0x7b, 0xa9, 0xdc, 0xfd, 0xcc, 0x1b, 0x4f } };69const GUID CLSID_D3D12SDKConfigurationGodot = { 0x7cda6aca, 0xa03e, 0x49c8, { 0x94, 0x58, 0x03, 0x34, 0xd2, 0x0e, 0x07, 0xce } };7071#ifdef PIX_ENABLED72#if defined(__GNUC__)73#define _MSC_VER 180074#endif75#define USE_PIX76#include <WinPixEventRuntime/pix3.h>77#if defined(__GNUC__)78#undef _MSC_VER79#endif80#endif8182RenderingContextDriverD3D12::RenderingContextDriverD3D12() {}8384RenderingContextDriverD3D12::~RenderingContextDriverD3D12() {85// Let's release manually everything that may still be holding86// onto the DLLs before freeing them.87device_factory.Reset();88dxgi_factory.Reset();8990if (lib_d3d12) {91FreeLibrary(lib_d3d12);92}93if (lib_dxgi) {94FreeLibrary(lib_dxgi);95}96#ifdef DCOMP_ENABLED97if (lib_dcomp) {98FreeLibrary(lib_dcomp);99}100#endif101}102103Error RenderingContextDriverD3D12::_init_device_factory() {104uint32_t agility_sdk_version = GLOBAL_GET("rendering/rendering_device/d3d12/agility_sdk_version");105String agility_sdk_path = String(".\\") + Engine::get_singleton()->get_architecture_name();106107lib_d3d12 = LoadLibraryW(L"D3D12.dll");108ERR_FAIL_NULL_V(lib_d3d12, ERR_CANT_CREATE);109110lib_dxgi = LoadLibraryW(L"DXGI.dll");111ERR_FAIL_NULL_V(lib_dxgi, ERR_CANT_CREATE);112113#ifdef DCOMP_ENABLED114lib_dcomp = LoadLibraryW(L"Dcomp.dll");115ERR_FAIL_NULL_V(lib_dcomp, ERR_CANT_CREATE);116#endif117118// Note: symbol is not available in MinGW import library.119PFN_D3D12_GET_INTERFACE d3d_D3D12GetInterface = (PFN_D3D12_GET_INTERFACE)(void *)GetProcAddress(lib_d3d12, "D3D12GetInterface");120if (!d3d_D3D12GetInterface) {121return OK; // Fallback to the system loader.122}123124ComPtr<ID3D12SDKConfiguration1> sdk_config;125HRESULT hr = d3d_D3D12GetInterface(CLSID_D3D12SDKConfigurationGodot, IID_PPV_ARGS(sdk_config.GetAddressOf()));126if (SUCCEEDED(hr)) {127hr = sdk_config->CreateDeviceFactory(agility_sdk_version, agility_sdk_path.ascii().get_data(), IID_PPV_ARGS(device_factory.GetAddressOf()));128if (FAILED(hr)) {129sdk_config->CreateDeviceFactory(agility_sdk_version, ".\\", IID_PPV_ARGS(device_factory.GetAddressOf()));130}131// If both calls failed, device factory is going to be nullptr, and D3D12CreateDevice is going to be used as fallback.132}133return OK;134}135136Error RenderingContextDriverD3D12::_initialize_debug_layers() {137ComPtr<ID3D12Debug> debug_controller;138HRESULT res;139140if (device_factory) {141res = device_factory->GetConfigurationInterface(CLSID_D3D12DebugGodot, IID_PPV_ARGS(&debug_controller));142} else {143PFN_D3D12_GET_DEBUG_INTERFACE d3d_D3D12GetDebugInterface = (PFN_D3D12_GET_DEBUG_INTERFACE)(void *)GetProcAddress(lib_d3d12, "D3D12GetDebugInterface");144ERR_FAIL_NULL_V(d3d_D3D12GetDebugInterface, ERR_CANT_CREATE);145146res = d3d_D3D12GetDebugInterface(IID_PPV_ARGS(&debug_controller));147}148149ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_QUERY_FAILED);150debug_controller->EnableDebugLayer();151return OK;152}153154Error RenderingContextDriverD3D12::_initialize_devices() {155const UINT dxgi_factory_flags = use_validation_layers() ? DXGI_CREATE_FACTORY_DEBUG : 0;156157typedef HRESULT(WINAPI * PFN_DXGI_CREATE_DXGI_FACTORY2)(UINT, REFIID, void **);158PFN_DXGI_CREATE_DXGI_FACTORY2 dxgi_CreateDXGIFactory2 = (PFN_DXGI_CREATE_DXGI_FACTORY2)(void *)GetProcAddress(lib_dxgi, "CreateDXGIFactory2");159ERR_FAIL_NULL_V(dxgi_CreateDXGIFactory2, ERR_CANT_CREATE);160161HRESULT res = dxgi_CreateDXGIFactory2(dxgi_factory_flags, IID_PPV_ARGS(&dxgi_factory));162ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE);163164// Enumerate all possible adapters.165LocalVector<IDXGIAdapter1 *> adapters;166IDXGIAdapter1 *adapter = nullptr;167do {168adapter = create_adapter(adapters.size());169if (adapter != nullptr) {170adapters.push_back(adapter);171}172} while (adapter != nullptr);173174ERR_FAIL_COND_V_MSG(adapters.is_empty(), ERR_CANT_CREATE, "Adapters enumeration reported zero accessible devices.");175176// Fill the device descriptions with the adapters.177driver_devices.resize(adapters.size());178for (uint32_t i = 0; i < adapters.size(); ++i) {179DXGI_ADAPTER_DESC1 desc = {};180adapters[i]->GetDesc1(&desc);181182Device &device = driver_devices[i];183device.name = desc.Description;184device.vendor = desc.VendorId;185device.workarounds = Workarounds();186187if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) {188device.type = DEVICE_TYPE_CPU;189} else {190const bool has_dedicated_vram = desc.DedicatedVideoMemory > 0;191device.type = has_dedicated_vram ? DEVICE_TYPE_DISCRETE_GPU : DEVICE_TYPE_INTEGRATED_GPU;192}193}194195// Release all created adapters.196for (uint32_t i = 0; i < adapters.size(); ++i) {197adapters[i]->Release();198}199200ComPtr<IDXGIFactory5> factory_5;201dxgi_factory.As(&factory_5);202if (factory_5 != nullptr) {203// The type is important as in general, sizeof(bool) != sizeof(BOOL).204BOOL feature_supported = FALSE;205res = factory_5->CheckFeatureSupport(DXGI_FEATURE_PRESENT_ALLOW_TEARING, &feature_supported, sizeof(feature_supported));206if (SUCCEEDED(res)) {207tearing_supported = feature_supported;208} else {209ERR_PRINT("CheckFeatureSupport failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");210}211}212213return OK;214}215216bool RenderingContextDriverD3D12::use_validation_layers() const {217return Engine::get_singleton()->is_validation_layers_enabled();218}219220Error RenderingContextDriverD3D12::initialize() {221Error err = _init_device_factory();222ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);223224if (use_validation_layers()) {225err = _initialize_debug_layers();226ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);227}228229err = _initialize_devices();230ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);231232return OK;233}234235const RenderingContextDriver::Device &RenderingContextDriverD3D12::device_get(uint32_t p_device_index) const {236DEV_ASSERT(p_device_index < driver_devices.size());237return driver_devices[p_device_index];238}239240uint32_t RenderingContextDriverD3D12::device_get_count() const {241return driver_devices.size();242}243244bool RenderingContextDriverD3D12::device_supports_present(uint32_t p_device_index, SurfaceID p_surface) const {245// All devices should support presenting to any surface.246return true;247}248249RenderingDeviceDriver *RenderingContextDriverD3D12::driver_create() {250return memnew(RenderingDeviceDriverD3D12(this));251}252253void RenderingContextDriverD3D12::driver_free(RenderingDeviceDriver *p_driver) {254memdelete(p_driver);255}256257RenderingContextDriver::SurfaceID RenderingContextDriverD3D12::surface_create(const void *p_platform_data) {258const WindowPlatformData *wpd = (const WindowPlatformData *)(p_platform_data);259Surface *surface = memnew(Surface);260surface->hwnd = wpd->window;261return SurfaceID(surface);262}263264void RenderingContextDriverD3D12::surface_set_size(SurfaceID p_surface, uint32_t p_width, uint32_t p_height) {265Surface *surface = (Surface *)(p_surface);266surface->width = p_width;267surface->height = p_height;268surface->needs_resize = true;269}270271void RenderingContextDriverD3D12::surface_set_vsync_mode(SurfaceID p_surface, DisplayServer::VSyncMode p_vsync_mode) {272Surface *surface = (Surface *)(p_surface);273surface->vsync_mode = p_vsync_mode;274surface->needs_resize = true;275}276277DisplayServer::VSyncMode RenderingContextDriverD3D12::surface_get_vsync_mode(SurfaceID p_surface) const {278Surface *surface = (Surface *)(p_surface);279return surface->vsync_mode;280}281282uint32_t RenderingContextDriverD3D12::surface_get_width(SurfaceID p_surface) const {283Surface *surface = (Surface *)(p_surface);284return surface->width;285}286287uint32_t RenderingContextDriverD3D12::surface_get_height(SurfaceID p_surface) const {288Surface *surface = (Surface *)(p_surface);289return surface->height;290}291292void RenderingContextDriverD3D12::surface_set_needs_resize(SurfaceID p_surface, bool p_needs_resize) {293Surface *surface = (Surface *)(p_surface);294surface->needs_resize = p_needs_resize;295}296297bool RenderingContextDriverD3D12::surface_get_needs_resize(SurfaceID p_surface) const {298Surface *surface = (Surface *)(p_surface);299return surface->needs_resize;300}301302void RenderingContextDriverD3D12::surface_destroy(SurfaceID p_surface) {303Surface *surface = (Surface *)(p_surface);304memdelete(surface);305}306307bool RenderingContextDriverD3D12::is_debug_utils_enabled() const {308#ifdef PIX_ENABLED309return true;310#else311return false;312#endif313}314315IDXGIAdapter1 *RenderingContextDriverD3D12::create_adapter(uint32_t p_adapter_index) const {316ComPtr<IDXGIFactory6> factory_6;317dxgi_factory.As(&factory_6);318319// TODO: Use IDXCoreAdapterList, which gives more comprehensive information.320IDXGIAdapter1 *adapter = nullptr;321if (factory_6) {322if (factory_6->EnumAdapterByGpuPreference(p_adapter_index, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, IID_PPV_ARGS(&adapter)) == DXGI_ERROR_NOT_FOUND) {323return nullptr;324}325} else {326if (dxgi_factory->EnumAdapters1(p_adapter_index, &adapter) == DXGI_ERROR_NOT_FOUND) {327return nullptr;328}329}330331return adapter;332}333334ID3D12DeviceFactory *RenderingContextDriverD3D12::device_factory_get() const {335return device_factory.Get();336}337338IDXGIFactory2 *RenderingContextDriverD3D12::dxgi_factory_get() const {339return dxgi_factory.Get();340}341342bool RenderingContextDriverD3D12::get_tearing_supported() const {343return tearing_supported;344}345346347