rendering_context_driver_d3d12.h 6.2 KB

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