Skip to content
0.2.0-dev Download ZIP
127 lines ·6.3 KB Raw
1/**************************************************************************/
2/* rendering_context_driver_d3d12.h */
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#pragma once
32
33#include "drivers/d3d12/rendering_device_driver_d3d12.h"
34#include "servers/display/display_server_enums.h"
35#include "servers/rendering/rendering_context_driver.h"
36
37#include <drivers/d3d12/godot_d3dx12.h>
38
39#ifdef DCOMP_ENABLED
40#include <dcomp.h>
41#endif
42
43#include <wrl/client.h>
44
45#define ARRAY_SIZE(a) std_size(a)
46
47class RenderingContextDriverD3D12 : public RenderingContextDriver {
48 Microsoft::WRL::ComPtr<ID3D12DeviceFactory> device_factory;
49 Microsoft::WRL::ComPtr<IDXGIFactory2> dxgi_factory;
50 TightLocalVector<Device> driver_devices;
51 bool tearing_supported = false;
52
53 Error _init_device_factory();
54 Error _initialize_debug_layers();
55 Error _create_dxgi_factory();
56 Error _initialize_devices();
57
58public:
59 virtual Error initialize() override;
60 virtual const Device &device_get(uint32_t p_device_index) const override;
61 virtual uint32_t device_get_count() const override;
62 virtual bool device_supports_present(uint32_t p_device_index, SurfaceID p_surface) const override;
63 virtual RenderingDeviceDriver *driver_create() override;
64 virtual void driver_free(RenderingDeviceDriver *p_driver) override;
65 virtual SurfaceID surface_create(const void *p_platform_data) override;
66 virtual void surface_set_size(SurfaceID p_surface, uint32_t p_width, uint32_t p_height) override;
67 virtual void surface_set_vsync_mode(SurfaceID p_surface, DisplayServerEnums::VSyncMode p_vsync_mode) override;
68 virtual DisplayServerEnums::VSyncMode surface_get_vsync_mode(SurfaceID p_surface) const override;
69 virtual void surface_set_hdr_output_enabled(SurfaceID p_surface, bool p_enabled) override;
70 virtual bool surface_get_hdr_output_enabled(SurfaceID p_surface) const override;
71 virtual void surface_set_hdr_output_reference_luminance(SurfaceID p_surface, float p_reference_luminance) override;
72 virtual float surface_get_hdr_output_reference_luminance(SurfaceID p_surface) const override;
73 virtual void surface_set_hdr_output_max_luminance(SurfaceID p_surface, float p_max_luminance) override;
74 virtual float surface_get_hdr_output_max_luminance(SurfaceID p_surface) const override;
75 virtual void surface_set_hdr_output_linear_luminance_scale(SurfaceID p_surface, float p_linear_luminance_scale) override;
76 virtual float surface_get_hdr_output_linear_luminance_scale(SurfaceID p_surface) const override;
77 virtual float surface_get_hdr_output_max_value(SurfaceID p_surface) const override;
78 virtual uint32_t surface_get_width(SurfaceID p_surface) const override;
79 virtual uint32_t surface_get_height(SurfaceID p_surface) const override;
80 virtual void surface_set_needs_resize(SurfaceID p_surface, bool p_needs_resize) override;
81 virtual bool surface_get_needs_resize(SurfaceID p_surface) const override;
82 virtual void surface_destroy(SurfaceID p_surface) override;
83 virtual bool is_debug_utils_enabled() const override;
84
85 // Platform-specific data for the Windows embedded in this driver.
86 struct WindowPlatformData {
87 HWND window;
88 };
89
90 // D3D12-only methods.
91 struct Surface {
92 HWND hwnd = nullptr;
93 uint32_t width = 0;
94 uint32_t height = 0;
95 DisplayServerEnums::VSyncMode vsync_mode = DisplayServerEnums::VSYNC_ENABLED;
96 bool needs_resize = false;
97
98 bool hdr_output = false;
99 // BT.2408 recommendation of 203 nits for HDR Reference White, rounded to 200
100 // to be a more pleasant player-facing value. This value is used by Steam
101 // Deck and other Windows emulation that does not provide an SDRWhiteLevel.
102 float hdr_reference_luminance = 200.0f;
103 float hdr_max_luminance = 1000.0f;
104 float hdr_linear_luminance_scale = 80.0f;
105
106#ifdef DCOMP_ENABLED
107 Microsoft::WRL::ComPtr<IDCompositionDevice> composition_device;
108 Microsoft::WRL::ComPtr<IDCompositionTarget> composition_target;
109 Microsoft::WRL::ComPtr<IDCompositionVisual> composition_visual;
110#endif
111 };
112
113 HMODULE lib_d3d12 = nullptr;
114 HMODULE lib_dxgi = nullptr;
115#ifdef DCOMP_ENABLED
116 HMODULE lib_dcomp = nullptr;
117#endif
118
119 IDXGIAdapter1 *create_adapter(uint32_t p_adapter_index) const;
120 ID3D12DeviceFactory *device_factory_get() const;
121 IDXGIFactory2 *dxgi_factory_get();
122 bool get_tearing_supported() const;
123 bool use_validation_layers() const;
124
125 RenderingContextDriverD3D12();
126 virtual ~RenderingContextDriverD3D12() override;
127};