| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#ifdef METAL_ENABLED |
| 34 |
|
| 35 |
#include "servers/rendering/rendering_context_driver.h" |
| 36 |
#include "servers/rendering/rendering_device_driver.h" |
| 37 |
|
| 38 |
#import <CoreGraphics/CGGeometry.h> |
| 39 |
|
| 40 |
#ifdef __OBJC__ |
| 41 |
#import "metal_objects.h" |
| 42 |
|
| 43 |
#import <Metal/Metal.h> |
| 44 |
#import <QuartzCore/CALayer.h> |
| 45 |
|
| 46 |
@class CAMetalLayer; |
| 47 |
@protocol CAMetalDrawable; |
| 48 |
#else |
| 49 |
typedef enum MTLPixelFormat { |
| 50 |
MTLPixelFormatBGRA8Unorm = 80, |
| 51 |
} MTLPixelFormat; |
| 52 |
class MDCommandBuffer; |
| 53 |
#endif |
| 54 |
|
| 55 |
class PixelFormats; |
| 56 |
|
| 57 |
#ifdef __OBJC__ |
| 58 |
#define METAL_DEVICE id<MTLDevice> |
| 59 |
#define METAL_DRAWABLE id<MTLDrawable> |
| 60 |
#define METAL_LAYER CAMetalLayer *__unsafe_unretained |
| 61 |
#define METAL_RESIDENCY_SET id<MTLResidencySet> |
| 62 |
#else |
| 63 |
#define METAL_DEVICE void * |
| 64 |
#define METAL_DRAWABLE void * |
| 65 |
#define METAL_LAYER void * |
| 66 |
#define METAL_RESIDENCY_SET void * |
| 67 |
#endif |
| 68 |
|
| 69 |
class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) RenderingContextDriverMetal : public RenderingContextDriver { |
| 70 |
bool capture_available = false; |
| 71 |
|
| 72 |
protected: |
| 73 |
METAL_DEVICE metal_device = nullptr; |
| 74 |
Device device; |
| 75 |
|
| 76 |
public: |
| 77 |
Error initialize() final override; |
| 78 |
const Device &device_get(uint32_t p_device_index) const final override; |
| 79 |
uint32_t device_get_count() const final override; |
| 80 |
bool device_supports_present(uint32_t p_device_index, SurfaceID p_surface) const final override { return true; } |
| 81 |
RenderingDeviceDriver *driver_create() final override; |
| 82 |
void driver_free(RenderingDeviceDriver *p_driver) final override; |
| 83 |
SurfaceID surface_create(const void *p_platform_data) final override; |
| 84 |
void surface_set_size(SurfaceID p_surface, uint32_t p_width, uint32_t p_height) final override; |
| 85 |
void surface_set_vsync_mode(SurfaceID p_surface, DisplayServer::VSyncMode p_vsync_mode) final override; |
| 86 |
DisplayServer::VSyncMode surface_get_vsync_mode(SurfaceID p_surface) const final override; |
| 87 |
uint32_t surface_get_width(SurfaceID p_surface) const final override; |
| 88 |
uint32_t surface_get_height(SurfaceID p_surface) const final override; |
| 89 |
void surface_set_needs_resize(SurfaceID p_surface, bool p_needs_resize) final override; |
| 90 |
bool surface_get_needs_resize(SurfaceID p_surface) const final override; |
| 91 |
void surface_destroy(SurfaceID p_surface) final override; |
| 92 |
bool is_debug_utils_enabled() const final override { return capture_available; } |
| 93 |
|
| 94 |
#pragma mark - Metal-specific methods |
| 95 |
|
| 96 |
|
| 97 |
struct WindowPlatformData { |
| 98 |
METAL_LAYER layer; |
| 99 |
}; |
| 100 |
|
| 101 |
class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) Surface { |
| 102 |
protected: |
| 103 |
METAL_DEVICE device; |
| 104 |
|
| 105 |
public: |
| 106 |
uint32_t width = 0; |
| 107 |
uint32_t height = 0; |
| 108 |
DisplayServer::VSyncMode vsync_mode = DisplayServer::VSYNC_ENABLED; |
| 109 |
bool needs_resize = false; |
| 110 |
double present_minimum_duration = 0.0; |
| 111 |
|
| 112 |
Surface(METAL_DEVICE p_device) : |
| 113 |
device(p_device) {} |
| 114 |
virtual ~Surface() = default; |
| 115 |
|
| 116 |
MTLPixelFormat get_pixel_format() const { return MTLPixelFormatBGRA8Unorm; } |
| 117 |
virtual Error resize(uint32_t p_desired_framebuffer_count) = 0; |
| 118 |
virtual RDD::FramebufferID acquire_next_frame_buffer() = 0; |
| 119 |
virtual void present(MDCommandBuffer *p_cmd_buffer) = 0; |
| 120 |
void set_max_fps(int p_max_fps) { present_minimum_duration = p_max_fps ? 1.0 / p_max_fps : 0.0; } |
| 121 |
}; |
| 122 |
|
| 123 |
METAL_DEVICE get_metal_device() const { |
| 124 |
return metal_device; |
| 125 |
} |
| 126 |
|
| 127 |
#pragma mark - Initialization |
| 128 |
|
| 129 |
RenderingContextDriverMetal(); |
| 130 |
~RenderingContextDriverMetal() override; |
| 131 |
}; |
| 132 |
|
| 133 |
#endif |