Path: blob/master/drivers/d3d12/rendering_context_driver_d3d12.cpp
9973 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")51GODOT_MSVC_WARNING_PUSH5253#include <dxcapi.h>5455GODOT_GCC_WARNING_POP56GODOT_CLANG_WARNING_POP5758#if !defined(_MSC_VER)59#include <guiddef.h>6061#include <dxguids.h>62#endif6364// Note: symbols are not available in MinGW and old MSVC import libraries.65// GUID values from https://github.com/microsoft/DirectX-Headers/blob/7a9f4d06911d30eecb56a4956dab29dcca2709ed/include/directx/d3d12.idl#L5877-L588166const GUID CLSID_D3D12DeviceFactoryGodot = { 0x114863bf, 0xc386, 0x4aee, { 0xb3, 0x9d, 0x8f, 0x0b, 0xbb, 0x06, 0x29, 0x55 } };67const GUID CLSID_D3D12DebugGodot = { 0xf2352aeb, 0xdd84, 0x49fe, { 0xb9, 0x7b, 0xa9, 0xdc, 0xfd, 0xcc, 0x1b, 0x4f } };68const GUID CLSID_D3D12SDKConfigurationGodot = { 0x7cda6aca, 0xa03e, 0x49c8, { 0x94, 0x58, 0x03, 0x34, 0xd2, 0x0e, 0x07, 0xce } };6970#ifdef PIX_ENABLED71#if defined(__GNUC__)72#define _MSC_VER 180073#endif74#define USE_PIX75#include "WinPixEventRuntime/pix3.h"76#if defined(__GNUC__)77#undef _MSC_VER78#endif79#endif8081RenderingContextDriverD3D12::RenderingContextDriverD3D12() {}8283RenderingContextDriverD3D12::~RenderingContextDriverD3D12() {84// Let's release manually everything that may still be holding85// onto the DLLs before freeing them.86device_factory.Reset();87dxgi_factory.Reset();8889if (lib_d3d12) {90FreeLibrary(lib_d3d12);91}92if (lib_dxgi) {93FreeLibrary(lib_dxgi);94}95#ifdef DCOMP_ENABLED96if (lib_dcomp) {97FreeLibrary(lib_dcomp);98}99#endif100}101102Error RenderingContextDriverD3D12::_init_device_factory() {103uint32_t agility_sdk_version = GLOBAL_GET("rendering/rendering_device/d3d12/agility_sdk_version");104String agility_sdk_path = String(".\\") + Engine::get_singleton()->get_architecture_name();105106lib_d3d12 = LoadLibraryW(L"D3D12.dll");107ERR_FAIL_NULL_V(lib_d3d12, ERR_CANT_CREATE);108109lib_dxgi = LoadLibraryW(L"DXGI.dll");110ERR_FAIL_NULL_V(lib_dxgi, ERR_CANT_CREATE);111112#ifdef DCOMP_ENABLED113lib_dcomp = LoadLibraryW(L"Dcomp.dll");114ERR_FAIL_NULL_V(lib_dcomp, ERR_CANT_CREATE);115#endif116117// Note: symbol is not available in MinGW import library.118PFN_D3D12_GET_INTERFACE d3d_D3D12GetInterface = (PFN_D3D12_GET_INTERFACE)(void *)GetProcAddress(lib_d3d12, "D3D12GetInterface");119if (!d3d_D3D12GetInterface) {120return OK; // Fallback to the system loader.121}122123ID3D12SDKConfiguration *sdk_config = nullptr;124if (SUCCEEDED(d3d_D3D12GetInterface(CLSID_D3D12SDKConfigurationGodot, IID_PPV_ARGS(&sdk_config)))) {125ID3D12SDKConfiguration1 *sdk_config1 = nullptr;126if (SUCCEEDED(sdk_config->QueryInterface(&sdk_config1))) {127if (SUCCEEDED(sdk_config1->CreateDeviceFactory(agility_sdk_version, agility_sdk_path.ascii().get_data(), IID_PPV_ARGS(device_factory.GetAddressOf())))) {128d3d_D3D12GetInterface(CLSID_D3D12DeviceFactoryGodot, IID_PPV_ARGS(device_factory.GetAddressOf()));129} else if (SUCCEEDED(sdk_config1->CreateDeviceFactory(agility_sdk_version, ".\\", IID_PPV_ARGS(device_factory.GetAddressOf())))) {130d3d_D3D12GetInterface(CLSID_D3D12DeviceFactoryGodot, IID_PPV_ARGS(device_factory.GetAddressOf()));131}132sdk_config1->Release();133}134sdk_config->Release();135}136return OK;137}138139Error RenderingContextDriverD3D12::_initialize_debug_layers() {140ComPtr<ID3D12Debug> debug_controller;141HRESULT res;142143if (device_factory) {144res = device_factory->GetConfigurationInterface(CLSID_D3D12DebugGodot, IID_PPV_ARGS(&debug_controller));145} else {146PFN_D3D12_GET_DEBUG_INTERFACE d3d_D3D12GetDebugInterface = (PFN_D3D12_GET_DEBUG_INTERFACE)(void *)GetProcAddress(lib_d3d12, "D3D12GetDebugInterface");147ERR_FAIL_NULL_V(d3d_D3D12GetDebugInterface, ERR_CANT_CREATE);148149res = d3d_D3D12GetDebugInterface(IID_PPV_ARGS(&debug_controller));150}151152ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_QUERY_FAILED);153debug_controller->EnableDebugLayer();154return OK;155}156157Error RenderingContextDriverD3D12::_initialize_devices() {158const UINT dxgi_factory_flags = use_validation_layers() ? DXGI_CREATE_FACTORY_DEBUG : 0;159160typedef HRESULT(WINAPI * PFN_DXGI_CREATE_DXGI_FACTORY2)(UINT, REFIID, void **);161PFN_DXGI_CREATE_DXGI_FACTORY2 dxgi_CreateDXGIFactory2 = (PFN_DXGI_CREATE_DXGI_FACTORY2)(void *)GetProcAddress(lib_dxgi, "CreateDXGIFactory2");162ERR_FAIL_NULL_V(dxgi_CreateDXGIFactory2, ERR_CANT_CREATE);163164HRESULT res = dxgi_CreateDXGIFactory2(dxgi_factory_flags, IID_PPV_ARGS(&dxgi_factory));165ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE);166167// Enumerate all possible adapters.168LocalVector<IDXGIAdapter1 *> adapters;169IDXGIAdapter1 *adapter = nullptr;170do {171adapter = create_adapter(adapters.size());172if (adapter != nullptr) {173adapters.push_back(adapter);174}175} while (adapter != nullptr);176177ERR_FAIL_COND_V_MSG(adapters.is_empty(), ERR_CANT_CREATE, "Adapters enumeration reported zero accessible devices.");178179// Fill the device descriptions with the adapters.180driver_devices.resize(adapters.size());181for (uint32_t i = 0; i < adapters.size(); ++i) {182DXGI_ADAPTER_DESC1 desc = {};183adapters[i]->GetDesc1(&desc);184185Device &device = driver_devices[i];186device.name = desc.Description;187device.vendor = desc.VendorId;188device.workarounds = Workarounds();189190if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) {191device.type = DEVICE_TYPE_CPU;192} else {193const bool has_dedicated_vram = desc.DedicatedVideoMemory > 0;194device.type = has_dedicated_vram ? DEVICE_TYPE_DISCRETE_GPU : DEVICE_TYPE_INTEGRATED_GPU;195}196}197198// Release all created adapters.199for (uint32_t i = 0; i < adapters.size(); ++i) {200adapters[i]->Release();201}202203ComPtr<IDXGIFactory5> factory_5;204dxgi_factory.As(&factory_5);205if (factory_5 != nullptr) {206// The type is important as in general, sizeof(bool) != sizeof(BOOL).207BOOL feature_supported = FALSE;208res = factory_5->CheckFeatureSupport(DXGI_FEATURE_PRESENT_ALLOW_TEARING, &feature_supported, sizeof(feature_supported));209if (SUCCEEDED(res)) {210tearing_supported = feature_supported;211} else {212ERR_PRINT("CheckFeatureSupport failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");213}214}215216return OK;217}218219bool RenderingContextDriverD3D12::use_validation_layers() const {220return Engine::get_singleton()->is_validation_layers_enabled();221}222223Error RenderingContextDriverD3D12::initialize() {224Error err = _init_device_factory();225ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);226227if (use_validation_layers()) {228err = _initialize_debug_layers();229ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);230}231232err = _initialize_devices();233ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);234235return OK;236}237238const RenderingContextDriver::Device &RenderingContextDriverD3D12::device_get(uint32_t p_device_index) const {239DEV_ASSERT(p_device_index < driver_devices.size());240return driver_devices[p_device_index];241}242243uint32_t RenderingContextDriverD3D12::device_get_count() const {244return driver_devices.size();245}246247bool RenderingContextDriverD3D12::device_supports_present(uint32_t p_device_index, SurfaceID p_surface) const {248// All devices should support presenting to any surface.249return true;250}251252RenderingDeviceDriver *RenderingContextDriverD3D12::driver_create() {253return memnew(RenderingDeviceDriverD3D12(this));254}255256void RenderingContextDriverD3D12::driver_free(RenderingDeviceDriver *p_driver) {257memdelete(p_driver);258}259260RenderingContextDriver::SurfaceID RenderingContextDriverD3D12::surface_create(const void *p_platform_data) {261const WindowPlatformData *wpd = (const WindowPlatformData *)(p_platform_data);262Surface *surface = memnew(Surface);263surface->hwnd = wpd->window;264return SurfaceID(surface);265}266267void RenderingContextDriverD3D12::surface_set_size(SurfaceID p_surface, uint32_t p_width, uint32_t p_height) {268Surface *surface = (Surface *)(p_surface);269surface->width = p_width;270surface->height = p_height;271surface->needs_resize = true;272}273274void RenderingContextDriverD3D12::surface_set_vsync_mode(SurfaceID p_surface, DisplayServer::VSyncMode p_vsync_mode) {275Surface *surface = (Surface *)(p_surface);276surface->vsync_mode = p_vsync_mode;277surface->needs_resize = true;278}279280DisplayServer::VSyncMode RenderingContextDriverD3D12::surface_get_vsync_mode(SurfaceID p_surface) const {281Surface *surface = (Surface *)(p_surface);282return surface->vsync_mode;283}284285uint32_t RenderingContextDriverD3D12::surface_get_width(SurfaceID p_surface) const {286Surface *surface = (Surface *)(p_surface);287return surface->width;288}289290uint32_t RenderingContextDriverD3D12::surface_get_height(SurfaceID p_surface) const {291Surface *surface = (Surface *)(p_surface);292return surface->height;293}294295void RenderingContextDriverD3D12::surface_set_needs_resize(SurfaceID p_surface, bool p_needs_resize) {296Surface *surface = (Surface *)(p_surface);297surface->needs_resize = p_needs_resize;298}299300bool RenderingContextDriverD3D12::surface_get_needs_resize(SurfaceID p_surface) const {301Surface *surface = (Surface *)(p_surface);302return surface->needs_resize;303}304305void RenderingContextDriverD3D12::surface_destroy(SurfaceID p_surface) {306Surface *surface = (Surface *)(p_surface);307memdelete(surface);308}309310bool RenderingContextDriverD3D12::is_debug_utils_enabled() const {311#ifdef PIX_ENABLED312return true;313#else314return false;315#endif316}317318IDXGIAdapter1 *RenderingContextDriverD3D12::create_adapter(uint32_t p_adapter_index) const {319ComPtr<IDXGIFactory6> factory_6;320dxgi_factory.As(&factory_6);321322// TODO: Use IDXCoreAdapterList, which gives more comprehensive information.323IDXGIAdapter1 *adapter = nullptr;324if (factory_6) {325if (factory_6->EnumAdapterByGpuPreference(p_adapter_index, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, IID_PPV_ARGS(&adapter)) == DXGI_ERROR_NOT_FOUND) {326return nullptr;327}328} else {329if (dxgi_factory->EnumAdapters1(p_adapter_index, &adapter) == DXGI_ERROR_NOT_FOUND) {330return nullptr;331}332}333334return adapter;335}336337ID3D12DeviceFactory *RenderingContextDriverD3D12::device_factory_get() const {338return device_factory.Get();339}340341IDXGIFactory2 *RenderingContextDriverD3D12::dxgi_factory_get() const {342return dxgi_factory.Get();343}344345bool RenderingContextDriverD3D12::get_tearing_supported() const {346return tearing_supported;347}348349350