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