rendering_context_driver_metal.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**************************************************************************/
  2. /* rendering_context_driver_metal.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_METAL_H
  31. #define RENDERING_CONTEXT_DRIVER_METAL_H
  32. #ifdef METAL_ENABLED
  33. #import "servers/rendering/rendering_context_driver.h"
  34. #import "servers/rendering/rendering_device_driver.h"
  35. #import <CoreGraphics/CGGeometry.h>
  36. #ifdef __OBJC__
  37. #import "metal_objects.h"
  38. #import <Metal/Metal.h>
  39. #import <QuartzCore/CALayer.h>
  40. @class CAMetalLayer;
  41. @protocol CAMetalDrawable;
  42. #else
  43. typedef enum MTLPixelFormat {
  44. MTLPixelFormatBGRA8Unorm = 80,
  45. } MTLPixelFormat;
  46. class MDCommandBuffer;
  47. #endif
  48. class PixelFormats;
  49. class MDResourceCache;
  50. class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) RenderingContextDriverMetal : public RenderingContextDriver {
  51. protected:
  52. #ifdef __OBJC__
  53. id<MTLDevice> metal_device = nullptr;
  54. #else
  55. void *metal_device = nullptr;
  56. #endif
  57. Device device; // There is only one device on Apple Silicon.
  58. public:
  59. Error initialize() final override;
  60. const Device &device_get(uint32_t p_device_index) const final override;
  61. uint32_t device_get_count() const final override;
  62. bool device_supports_present(uint32_t p_device_index, SurfaceID p_surface) const final override { return true; }
  63. RenderingDeviceDriver *driver_create() final override;
  64. void driver_free(RenderingDeviceDriver *p_driver) final override;
  65. SurfaceID surface_create(const void *p_platform_data) final override;
  66. void surface_set_size(SurfaceID p_surface, uint32_t p_width, uint32_t p_height) final override;
  67. void surface_set_vsync_mode(SurfaceID p_surface, DisplayServer::VSyncMode p_vsync_mode) final override;
  68. DisplayServer::VSyncMode surface_get_vsync_mode(SurfaceID p_surface) const final override;
  69. uint32_t surface_get_width(SurfaceID p_surface) const final override;
  70. uint32_t surface_get_height(SurfaceID p_surface) const final override;
  71. void surface_set_needs_resize(SurfaceID p_surface, bool p_needs_resize) final override;
  72. bool surface_get_needs_resize(SurfaceID p_surface) const final override;
  73. void surface_destroy(SurfaceID p_surface) final override;
  74. bool is_debug_utils_enabled() const final override { return true; }
  75. #pragma mark - Metal-specific methods
  76. // Platform-specific data for the Windows embedded in this driver.
  77. struct WindowPlatformData {
  78. #ifdef __OBJC__
  79. CAMetalLayer *__unsafe_unretained layer;
  80. #else
  81. void *layer;
  82. #endif
  83. };
  84. class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) Surface {
  85. protected:
  86. #ifdef __OBJC__
  87. id<MTLDevice> device;
  88. #else
  89. void *device;
  90. #endif
  91. public:
  92. uint32_t width = 0;
  93. uint32_t height = 0;
  94. DisplayServer::VSyncMode vsync_mode = DisplayServer::VSYNC_ENABLED;
  95. bool needs_resize = false;
  96. double present_minimum_duration = 0.0;
  97. Surface(
  98. #ifdef __OBJC__
  99. id<MTLDevice> p_device
  100. #else
  101. void *p_device
  102. #endif
  103. ) :
  104. device(p_device) {
  105. }
  106. virtual ~Surface() = default;
  107. MTLPixelFormat get_pixel_format() const { return MTLPixelFormatBGRA8Unorm; }
  108. virtual Error resize(uint32_t p_desired_framebuffer_count) = 0;
  109. virtual RDD::FramebufferID acquire_next_frame_buffer() = 0;
  110. virtual void present(MDCommandBuffer *p_cmd_buffer) = 0;
  111. void set_max_fps(int p_max_fps) { present_minimum_duration = p_max_fps ? 1.0 / p_max_fps : 0.0; }
  112. };
  113. #ifdef __OBJC__
  114. id<MTLDevice>
  115. #else
  116. void *
  117. #endif
  118. get_metal_device() const {
  119. return metal_device;
  120. }
  121. #pragma mark - Initialization
  122. RenderingContextDriverMetal();
  123. ~RenderingContextDriverMetal() override;
  124. };
  125. #endif // METAL_ENABLED
  126. #endif // RENDERING_CONTEXT_DRIVER_METAL_H