Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/d3d12/rendering_context_driver_d3d12.cpp
22517 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
41
GODOT_GCC_WARNING_PUSH
42
GODOT_GCC_WARNING_IGNORE("-Wmissing-field-initializers")
43
GODOT_GCC_WARNING_IGNORE("-Wnon-virtual-dtor")
44
GODOT_GCC_WARNING_IGNORE("-Wshadow")
45
GODOT_GCC_WARNING_IGNORE("-Wswitch")
46
GODOT_CLANG_WARNING_PUSH
47
GODOT_CLANG_WARNING_IGNORE("-Wmissing-field-initializers")
48
GODOT_CLANG_WARNING_IGNORE("-Wnon-virtual-dtor")
49
GODOT_CLANG_WARNING_IGNORE("-Wstring-plus-int")
50
GODOT_CLANG_WARNING_IGNORE("-Wswitch")
51
52
#include <dxcapi.h>
53
#include <dxgi1_6.h>
54
55
GODOT_GCC_WARNING_POP
56
GODOT_CLANG_WARNING_POP
57
58
#if !defined(_MSC_VER)
59
#include <guiddef.h>
60
61
#include <thirdparty/directx_headers/include/dxguids/dxguids.h>
62
#endif
63
64
using Microsoft::WRL::ComPtr;
65
66
// 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-L5881
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
ComPtr<ID3D12SDKConfiguration1> sdk_config;
125
HRESULT hr = d3d_D3D12GetInterface(CLSID_D3D12SDKConfigurationGodot, IID_PPV_ARGS(sdk_config.GetAddressOf()));
126
if (SUCCEEDED(hr)) {
127
hr = sdk_config->CreateDeviceFactory(agility_sdk_version, agility_sdk_path.ascii().get_data(), IID_PPV_ARGS(device_factory.GetAddressOf()));
128
if (FAILED(hr)) {
129
sdk_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
}
133
return OK;
134
}
135
136
Error RenderingContextDriverD3D12::_initialize_debug_layers() {
137
ComPtr<ID3D12Debug> debug_controller;
138
HRESULT res;
139
140
if (device_factory) {
141
res = device_factory->GetConfigurationInterface(CLSID_D3D12DebugGodot, IID_PPV_ARGS(&debug_controller));
142
} else {
143
PFN_D3D12_GET_DEBUG_INTERFACE d3d_D3D12GetDebugInterface = (PFN_D3D12_GET_DEBUG_INTERFACE)(void *)GetProcAddress(lib_d3d12, "D3D12GetDebugInterface");
144
ERR_FAIL_NULL_V(d3d_D3D12GetDebugInterface, ERR_CANT_CREATE);
145
146
res = d3d_D3D12GetDebugInterface(IID_PPV_ARGS(&debug_controller));
147
}
148
149
ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_QUERY_FAILED);
150
debug_controller->EnableDebugLayer();
151
return OK;
152
}
153
154
Error RenderingContextDriverD3D12::_create_dxgi_factory() {
155
const UINT dxgi_factory_flags = use_validation_layers() ? DXGI_CREATE_FACTORY_DEBUG : 0;
156
157
typedef HRESULT(WINAPI * PFN_DXGI_CREATE_DXGI_FACTORY2)(UINT, REFIID, void **);
158
PFN_DXGI_CREATE_DXGI_FACTORY2 dxgi_CreateDXGIFactory2 = (PFN_DXGI_CREATE_DXGI_FACTORY2)(void *)GetProcAddress(lib_dxgi, "CreateDXGIFactory2");
159
ERR_FAIL_NULL_V(dxgi_CreateDXGIFactory2, ERR_CANT_CREATE);
160
161
HRESULT res = dxgi_CreateDXGIFactory2(dxgi_factory_flags, IID_PPV_ARGS(&dxgi_factory));
162
ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE);
163
164
return OK;
165
}
166
167
Error RenderingContextDriverD3D12::_initialize_devices() {
168
// Create the initial DXGI factory.
169
Error err = _create_dxgi_factory();
170
ERR_FAIL_COND_V(err != OK, err);
171
172
HRESULT res;
173
174
// Enumerate all possible adapters.
175
LocalVector<IDXGIAdapter1 *> adapters;
176
IDXGIAdapter1 *adapter = nullptr;
177
do {
178
adapter = create_adapter(adapters.size());
179
if (adapter != nullptr) {
180
adapters.push_back(adapter);
181
}
182
} while (adapter != nullptr);
183
184
ERR_FAIL_COND_V_MSG(adapters.is_empty(), ERR_CANT_CREATE, "Adapters enumeration reported zero accessible devices.");
185
186
// Fill the device descriptions with the adapters.
187
driver_devices.resize(adapters.size());
188
for (uint32_t i = 0; i < adapters.size(); ++i) {
189
DXGI_ADAPTER_DESC1 desc = {};
190
adapters[i]->GetDesc1(&desc);
191
192
Device &device = driver_devices[i];
193
device.name = desc.Description;
194
device.vendor = desc.VendorId;
195
device.workarounds = Workarounds();
196
197
if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) {
198
device.type = DEVICE_TYPE_CPU;
199
} else {
200
const bool has_dedicated_vram = desc.DedicatedVideoMemory > 0;
201
device.type = has_dedicated_vram ? DEVICE_TYPE_DISCRETE_GPU : DEVICE_TYPE_INTEGRATED_GPU;
202
}
203
}
204
205
// Release all created adapters.
206
for (uint32_t i = 0; i < adapters.size(); ++i) {
207
adapters[i]->Release();
208
}
209
210
ComPtr<IDXGIFactory5> factory_5;
211
dxgi_factory.As(&factory_5);
212
if (factory_5 != nullptr) {
213
// The type is important as in general, sizeof(bool) != sizeof(BOOL).
214
BOOL feature_supported = FALSE;
215
res = factory_5->CheckFeatureSupport(DXGI_FEATURE_PRESENT_ALLOW_TEARING, &feature_supported, sizeof(feature_supported));
216
if (SUCCEEDED(res)) {
217
tearing_supported = feature_supported;
218
} else {
219
ERR_PRINT("CheckFeatureSupport failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
220
}
221
}
222
223
return OK;
224
}
225
226
bool RenderingContextDriverD3D12::use_validation_layers() const {
227
return Engine::get_singleton()->is_validation_layers_enabled();
228
}
229
230
Error RenderingContextDriverD3D12::initialize() {
231
Error err = _init_device_factory();
232
ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
233
234
if (use_validation_layers()) {
235
err = _initialize_debug_layers();
236
ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
237
}
238
239
err = _initialize_devices();
240
ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
241
242
return OK;
243
}
244
245
const RenderingContextDriver::Device &RenderingContextDriverD3D12::device_get(uint32_t p_device_index) const {
246
DEV_ASSERT(p_device_index < driver_devices.size());
247
return driver_devices[p_device_index];
248
}
249
250
uint32_t RenderingContextDriverD3D12::device_get_count() const {
251
return driver_devices.size();
252
}
253
254
bool RenderingContextDriverD3D12::device_supports_present(uint32_t p_device_index, SurfaceID p_surface) const {
255
// All devices should support presenting to any surface.
256
return true;
257
}
258
259
RenderingDeviceDriver *RenderingContextDriverD3D12::driver_create() {
260
return memnew(RenderingDeviceDriverD3D12(this));
261
}
262
263
void RenderingContextDriverD3D12::driver_free(RenderingDeviceDriver *p_driver) {
264
memdelete(p_driver);
265
}
266
267
RenderingContextDriver::SurfaceID RenderingContextDriverD3D12::surface_create(const void *p_platform_data) {
268
const WindowPlatformData *wpd = (const WindowPlatformData *)(p_platform_data);
269
Surface *surface = memnew(Surface);
270
surface->hwnd = wpd->window;
271
return SurfaceID(surface);
272
}
273
274
void RenderingContextDriverD3D12::surface_set_size(SurfaceID p_surface, uint32_t p_width, uint32_t p_height) {
275
Surface *surface = (Surface *)(p_surface);
276
surface->width = p_width;
277
surface->height = p_height;
278
surface->needs_resize = true;
279
}
280
281
void RenderingContextDriverD3D12::surface_set_vsync_mode(SurfaceID p_surface, DisplayServer::VSyncMode p_vsync_mode) {
282
Surface *surface = (Surface *)(p_surface);
283
surface->vsync_mode = p_vsync_mode;
284
surface->needs_resize = true;
285
}
286
287
DisplayServer::VSyncMode RenderingContextDriverD3D12::surface_get_vsync_mode(SurfaceID p_surface) const {
288
Surface *surface = (Surface *)(p_surface);
289
return surface->vsync_mode;
290
}
291
292
void RenderingContextDriverD3D12::surface_set_hdr_output_enabled(SurfaceID p_surface, bool p_enabled) {
293
Surface *surface = (Surface *)(p_surface);
294
surface->hdr_output = p_enabled;
295
surface->needs_resize = true;
296
}
297
298
bool RenderingContextDriverD3D12::surface_get_hdr_output_enabled(SurfaceID p_surface) const {
299
Surface *surface = (Surface *)(p_surface);
300
return surface->hdr_output;
301
}
302
303
void RenderingContextDriverD3D12::surface_set_hdr_output_reference_luminance(SurfaceID p_surface, float p_reference_luminance) {
304
Surface *surface = (Surface *)(p_surface);
305
surface->hdr_reference_luminance = p_reference_luminance;
306
}
307
308
float RenderingContextDriverD3D12::surface_get_hdr_output_reference_luminance(SurfaceID p_surface) const {
309
Surface *surface = (Surface *)(p_surface);
310
return surface->hdr_reference_luminance;
311
}
312
313
void RenderingContextDriverD3D12::surface_set_hdr_output_max_luminance(SurfaceID p_surface, float p_max_luminance) {
314
Surface *surface = (Surface *)(p_surface);
315
surface->hdr_max_luminance = p_max_luminance;
316
}
317
318
float RenderingContextDriverD3D12::surface_get_hdr_output_max_luminance(SurfaceID p_surface) const {
319
Surface *surface = (Surface *)(p_surface);
320
return surface->hdr_max_luminance;
321
}
322
323
void RenderingContextDriverD3D12::surface_set_hdr_output_linear_luminance_scale(SurfaceID p_surface, float p_linear_luminance_scale) {
324
Surface *surface = (Surface *)(p_surface);
325
surface->hdr_linear_luminance_scale = p_linear_luminance_scale;
326
}
327
328
float RenderingContextDriverD3D12::surface_get_hdr_output_linear_luminance_scale(SurfaceID p_surface) const {
329
Surface *surface = (Surface *)(p_surface);
330
return surface->hdr_linear_luminance_scale;
331
}
332
333
float RenderingContextDriverD3D12::surface_get_hdr_output_max_value(SurfaceID p_surface) const {
334
Surface *surface = (Surface *)(p_surface);
335
return MAX(surface->hdr_max_luminance / MAX(surface->hdr_reference_luminance, 1.0f), 1.0f);
336
}
337
338
uint32_t RenderingContextDriverD3D12::surface_get_width(SurfaceID p_surface) const {
339
Surface *surface = (Surface *)(p_surface);
340
return surface->width;
341
}
342
343
uint32_t RenderingContextDriverD3D12::surface_get_height(SurfaceID p_surface) const {
344
Surface *surface = (Surface *)(p_surface);
345
return surface->height;
346
}
347
348
void RenderingContextDriverD3D12::surface_set_needs_resize(SurfaceID p_surface, bool p_needs_resize) {
349
Surface *surface = (Surface *)(p_surface);
350
surface->needs_resize = p_needs_resize;
351
}
352
353
bool RenderingContextDriverD3D12::surface_get_needs_resize(SurfaceID p_surface) const {
354
Surface *surface = (Surface *)(p_surface);
355
return surface->needs_resize;
356
}
357
358
void RenderingContextDriverD3D12::surface_destroy(SurfaceID p_surface) {
359
Surface *surface = (Surface *)(p_surface);
360
memdelete(surface);
361
}
362
363
bool RenderingContextDriverD3D12::is_debug_utils_enabled() const {
364
#ifdef PIX_ENABLED
365
return true;
366
#else
367
return false;
368
#endif
369
}
370
371
IDXGIAdapter1 *RenderingContextDriverD3D12::create_adapter(uint32_t p_adapter_index) const {
372
ComPtr<IDXGIFactory6> factory_6;
373
dxgi_factory.As(&factory_6);
374
375
// TODO: Use IDXCoreAdapterList, which gives more comprehensive information.
376
IDXGIAdapter1 *adapter = nullptr;
377
if (factory_6) {
378
if (factory_6->EnumAdapterByGpuPreference(p_adapter_index, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, IID_PPV_ARGS(&adapter)) == DXGI_ERROR_NOT_FOUND) {
379
return nullptr;
380
}
381
} else {
382
if (dxgi_factory->EnumAdapters1(p_adapter_index, &adapter) == DXGI_ERROR_NOT_FOUND) {
383
return nullptr;
384
}
385
}
386
387
return adapter;
388
}
389
390
ID3D12DeviceFactory *RenderingContextDriverD3D12::device_factory_get() const {
391
return device_factory.Get();
392
}
393
394
IDXGIFactory2 *RenderingContextDriverD3D12::dxgi_factory_get() {
395
// Check if the factory is still current. It can become invalid if displays change
396
if (dxgi_factory && !dxgi_factory->IsCurrent()) {
397
dxgi_factory.Reset();
398
_create_dxgi_factory();
399
}
400
return dxgi_factory.Get();
401
}
402
403
bool RenderingContextDriverD3D12::get_tearing_supported() const {
404
return tearing_supported;
405
}
406
407