rendering_context_driver_metal.h 5.4 KB

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