rendering_context_driver_d3d12.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #pragma once
  31. #include "core/os/mutex.h"
  32. #include "core/string/ustring.h"
  33. #include "core/templates/rid_owner.h"
  34. #include "rendering_device_driver_d3d12.h"
  35. #include "servers/display_server.h"
  36. #include "servers/rendering/rendering_context_driver.h"
  37. #if defined(__GNUC__) && !defined(__clang__)
  38. #pragma GCC diagnostic push
  39. #pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
  40. #pragma GCC diagnostic ignored "-Wshadow"
  41. #pragma GCC diagnostic ignored "-Wswitch"
  42. #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
  43. #pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
  44. #elif defined(__clang__)
  45. #pragma clang diagnostic push
  46. #pragma clang diagnostic ignored "-Wnon-virtual-dtor"
  47. #pragma clang diagnostic ignored "-Wstring-plus-int"
  48. #pragma clang diagnostic ignored "-Wswitch"
  49. #pragma clang diagnostic ignored "-Wmissing-field-initializers"
  50. #pragma clang diagnostic ignored "-Wimplicit-fallthrough"
  51. #endif
  52. #if defined(AS)
  53. #undef AS
  54. #endif
  55. #if (WINVER < _WIN32_WINNT_WIN8) && defined(_MSC_VER)
  56. #pragma push_macro("NTDDI_VERSION")
  57. #pragma push_macro("WINVER")
  58. #undef NTDDI_VERSION
  59. #undef WINVER
  60. #define NTDDI_VERSION NTDDI_WIN8
  61. #define WINVER _WIN32_WINNT_WIN8
  62. #include <dcomp.h>
  63. #pragma pop_macro("WINVER")
  64. #pragma pop_macro("NTDDI_VERSION")
  65. #else
  66. #include <dcomp.h>
  67. #endif
  68. #include "d3dx12.h"
  69. #include <dxgi1_6.h>
  70. #include <wrl/client.h>
  71. #if defined(__GNUC__) && !defined(__clang__)
  72. #pragma GCC diagnostic pop
  73. #elif defined(__clang__)
  74. #pragma clang diagnostic pop
  75. #endif
  76. using Microsoft::WRL::ComPtr;
  77. #define ARRAY_SIZE(a) std::size(a)
  78. class RenderingContextDriverD3D12 : public RenderingContextDriver {
  79. ComPtr<ID3D12DeviceFactory> device_factory;
  80. ComPtr<IDXGIFactory2> dxgi_factory;
  81. TightLocalVector<Device> driver_devices;
  82. bool tearing_supported = false;
  83. Error _init_device_factory();
  84. Error _initialize_debug_layers();
  85. Error _initialize_devices();
  86. public:
  87. virtual Error initialize() override;
  88. virtual const Device &device_get(uint32_t p_device_index) const override;
  89. virtual uint32_t device_get_count() const override;
  90. virtual bool device_supports_present(uint32_t p_device_index, SurfaceID p_surface) const override;
  91. virtual RenderingDeviceDriver *driver_create() override;
  92. virtual void driver_free(RenderingDeviceDriver *p_driver) override;
  93. virtual SurfaceID surface_create(const void *p_platform_data) override;
  94. virtual void surface_set_size(SurfaceID p_surface, uint32_t p_width, uint32_t p_height) override;
  95. virtual void surface_set_vsync_mode(SurfaceID p_surface, DisplayServer::VSyncMode p_vsync_mode) override;
  96. virtual DisplayServer::VSyncMode surface_get_vsync_mode(SurfaceID p_surface) const override;
  97. virtual uint32_t surface_get_width(SurfaceID p_surface) const override;
  98. virtual uint32_t surface_get_height(SurfaceID p_surface) const override;
  99. virtual void surface_set_needs_resize(SurfaceID p_surface, bool p_needs_resize) override;
  100. virtual bool surface_get_needs_resize(SurfaceID p_surface) const override;
  101. virtual void surface_destroy(SurfaceID p_surface) override;
  102. virtual bool is_debug_utils_enabled() const override;
  103. // Platform-specific data for the Windows embedded in this driver.
  104. struct WindowPlatformData {
  105. HWND window;
  106. };
  107. // D3D12-only methods.
  108. struct Surface {
  109. HWND hwnd = nullptr;
  110. uint32_t width = 0;
  111. uint32_t height = 0;
  112. DisplayServer::VSyncMode vsync_mode = DisplayServer::VSYNC_ENABLED;
  113. bool needs_resize = false;
  114. ComPtr<IDCompositionDevice> composition_device;
  115. ComPtr<IDCompositionTarget> composition_target;
  116. ComPtr<IDCompositionVisual> composition_visual;
  117. };
  118. HMODULE lib_d3d12 = nullptr;
  119. HMODULE lib_dxgi = nullptr;
  120. HMODULE lib_dcomp = nullptr;
  121. IDXGIAdapter1 *create_adapter(uint32_t p_adapter_index) const;
  122. ID3D12DeviceFactory *device_factory_get() const;
  123. IDXGIFactory2 *dxgi_factory_get() const;
  124. bool get_tearing_supported() const;
  125. bool use_validation_layers() const;
  126. RenderingContextDriverD3D12();
  127. virtual ~RenderingContextDriverD3D12() override;
  128. };