Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/metal/rendering_context_driver_metal.h
9973 views
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
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
class MDResourceCache;
57
58
class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) RenderingContextDriverMetal : public RenderingContextDriver {
59
bool capture_available = false;
60
61
protected:
62
#ifdef __OBJC__
63
id<MTLDevice> metal_device = nullptr;
64
#else
65
void *metal_device = nullptr;
66
#endif
67
Device device; // There is only one device on Apple Silicon.
68
69
public:
70
Error initialize() final override;
71
const Device &device_get(uint32_t p_device_index) const final override;
72
uint32_t device_get_count() const final override;
73
bool device_supports_present(uint32_t p_device_index, SurfaceID p_surface) const final override { return true; }
74
RenderingDeviceDriver *driver_create() final override;
75
void driver_free(RenderingDeviceDriver *p_driver) final override;
76
SurfaceID surface_create(const void *p_platform_data) final override;
77
void surface_set_size(SurfaceID p_surface, uint32_t p_width, uint32_t p_height) final override;
78
void surface_set_vsync_mode(SurfaceID p_surface, DisplayServer::VSyncMode p_vsync_mode) final override;
79
DisplayServer::VSyncMode surface_get_vsync_mode(SurfaceID p_surface) const final override;
80
uint32_t surface_get_width(SurfaceID p_surface) const final override;
81
uint32_t surface_get_height(SurfaceID p_surface) const final override;
82
void surface_set_needs_resize(SurfaceID p_surface, bool p_needs_resize) final override;
83
bool surface_get_needs_resize(SurfaceID p_surface) const final override;
84
void surface_destroy(SurfaceID p_surface) final override;
85
bool is_debug_utils_enabled() const final override { return capture_available; }
86
87
#pragma mark - Metal-specific methods
88
89
// Platform-specific data for the Windows embedded in this driver.
90
struct WindowPlatformData {
91
#ifdef __OBJC__
92
CAMetalLayer *__unsafe_unretained layer;
93
#else
94
void *layer;
95
#endif
96
};
97
98
class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) Surface {
99
protected:
100
#ifdef __OBJC__
101
id<MTLDevice> device;
102
#else
103
void *device;
104
#endif
105
106
public:
107
uint32_t width = 0;
108
uint32_t height = 0;
109
DisplayServer::VSyncMode vsync_mode = DisplayServer::VSYNC_ENABLED;
110
bool needs_resize = false;
111
double present_minimum_duration = 0.0;
112
113
Surface(
114
#ifdef __OBJC__
115
id<MTLDevice> p_device
116
#else
117
void *p_device
118
#endif
119
) :
120
device(p_device) {
121
}
122
virtual ~Surface() = default;
123
124
MTLPixelFormat get_pixel_format() const { return MTLPixelFormatBGRA8Unorm; }
125
virtual Error resize(uint32_t p_desired_framebuffer_count) = 0;
126
virtual RDD::FramebufferID acquire_next_frame_buffer() = 0;
127
virtual void present(MDCommandBuffer *p_cmd_buffer) = 0;
128
void set_max_fps(int p_max_fps) { present_minimum_duration = p_max_fps ? 1.0 / p_max_fps : 0.0; }
129
};
130
131
#ifdef __OBJC__
132
id<MTLDevice>
133
#else
134
void *
135
#endif
136
get_metal_device() const {
137
return metal_device;
138
}
139
140
#pragma mark - Initialization
141
142
RenderingContextDriverMetal();
143
~RenderingContextDriverMetal() override;
144
};
145
146
#endif // METAL_ENABLED
147
148