Debian Codegraph @

drivers/metal/rendering_context_driver_metal.h

raw symbols SCIP
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
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; // There is only one device on Apple Silicon.
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 // Platform-specific data for the Windows embedded in this driver.
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 // METAL_ENABLED