rendering_context_driver_d3d12.cpp 13 KB

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