rendering_context_driver_d3d12.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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(AS)
  38. #undef AS
  39. #endif
  40. #ifdef DCOMP_ENABLED
  41. #include <dcomp.h>
  42. #endif
  43. #include <d3dx12.h>
  44. #include <dxgi1_6.h>
  45. #include <wrl/client.h>
  46. using Microsoft::WRL::ComPtr;
  47. #define ARRAY_SIZE(a) std::size(a)
  48. class RenderingContextDriverD3D12 : public RenderingContextDriver {
  49. ComPtr<ID3D12DeviceFactory> device_factory;
  50. ComPtr<IDXGIFactory2> dxgi_factory;
  51. TightLocalVector<Device> driver_devices;
  52. bool tearing_supported = false;
  53. Error _init_device_factory();
  54. Error _initialize_debug_layers();
  55. Error _initialize_devices();
  56. public:
  57. virtual Error initialize() override;
  58. virtual const Device &device_get(uint32_t p_device_index) const override;
  59. virtual uint32_t device_get_count() const override;
  60. virtual bool device_supports_present(uint32_t p_device_index, SurfaceID p_surface) const override;
  61. virtual RenderingDeviceDriver *driver_create() override;
  62. virtual void driver_free(RenderingDeviceDriver *p_driver) override;
  63. virtual SurfaceID surface_create(const void *p_platform_data) override;
  64. virtual void surface_set_size(SurfaceID p_surface, uint32_t p_width, uint32_t p_height) override;
  65. virtual void surface_set_vsync_mode(SurfaceID p_surface, DisplayServer::VSyncMode p_vsync_mode) override;
  66. virtual DisplayServer::VSyncMode surface_get_vsync_mode(SurfaceID p_surface) const override;
  67. virtual uint32_t surface_get_width(SurfaceID p_surface) const override;
  68. virtual uint32_t surface_get_height(SurfaceID p_surface) const override;
  69. virtual void surface_set_needs_resize(SurfaceID p_surface, bool p_needs_resize) override;
  70. virtual bool surface_get_needs_resize(SurfaceID p_surface) const override;
  71. virtual void surface_destroy(SurfaceID p_surface) override;
  72. virtual bool is_debug_utils_enabled() const override;
  73. // Platform-specific data for the Windows embedded in this driver.
  74. struct WindowPlatformData {
  75. HWND window;
  76. };
  77. // D3D12-only methods.
  78. struct Surface {
  79. HWND hwnd = nullptr;
  80. uint32_t width = 0;
  81. uint32_t height = 0;
  82. DisplayServer::VSyncMode vsync_mode = DisplayServer::VSYNC_ENABLED;
  83. bool needs_resize = false;
  84. #ifdef DCOMP_ENABLED
  85. ComPtr<IDCompositionDevice> composition_device;
  86. ComPtr<IDCompositionTarget> composition_target;
  87. ComPtr<IDCompositionVisual> composition_visual;
  88. #endif
  89. };
  90. HMODULE lib_d3d12 = nullptr;
  91. HMODULE lib_dxgi = nullptr;
  92. #ifdef DCOMP_ENABLED
  93. HMODULE lib_dcomp = nullptr;
  94. #endif
  95. IDXGIAdapter1 *create_adapter(uint32_t p_adapter_index) const;
  96. ID3D12DeviceFactory *device_factory_get() const;
  97. IDXGIFactory2 *dxgi_factory_get() const;
  98. bool get_tearing_supported() const;
  99. bool use_validation_layers() const;
  100. RenderingContextDriverD3D12();
  101. virtual ~RenderingContextDriverD3D12() override;
  102. };