Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/metal/rendering_context_driver_metal.h
21871 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
#include <Metal/Metal.hpp>
39
#include <QuartzCore/QuartzCore.hpp>
40
41
namespace MTL3 {
42
class MDCommandBuffer;
43
}
44
45
class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) RenderingContextDriverMetal : public RenderingContextDriver {
46
bool capture_available = false;
47
48
protected:
49
MTL::Device *metal_device = nullptr;
50
Device device; // There is only one device on Apple Silicon.
51
52
public:
53
Error initialize() final override;
54
const Device &device_get(uint32_t p_device_index) const final override;
55
uint32_t device_get_count() const final override;
56
bool device_supports_present(uint32_t p_device_index, SurfaceID p_surface) const final override { return true; }
57
RenderingDeviceDriver *driver_create() final override;
58
void driver_free(RenderingDeviceDriver *p_driver) final override;
59
SurfaceID surface_create(const void *p_platform_data) final override;
60
void surface_set_size(SurfaceID p_surface, uint32_t p_width, uint32_t p_height) final override;
61
void surface_set_vsync_mode(SurfaceID p_surface, DisplayServer::VSyncMode p_vsync_mode) final override;
62
DisplayServer::VSyncMode surface_get_vsync_mode(SurfaceID p_surface) const final override;
63
virtual void surface_set_hdr_output_enabled(SurfaceID p_surface, bool p_enabled) final override;
64
virtual bool surface_get_hdr_output_enabled(SurfaceID p_surface) const final override;
65
virtual void surface_set_hdr_output_reference_luminance(SurfaceID p_surface, float p_reference_luminance) final override;
66
virtual float surface_get_hdr_output_reference_luminance(SurfaceID p_surface) const final override;
67
virtual void surface_set_hdr_output_max_luminance(SurfaceID p_surface, float p_max_luminance) final override;
68
virtual float surface_get_hdr_output_max_luminance(SurfaceID p_surface) const final override;
69
virtual void surface_set_hdr_output_linear_luminance_scale(SurfaceID p_surface, float p_linear_luminance_scale) final override;
70
virtual float surface_get_hdr_output_linear_luminance_scale(SurfaceID p_surface) const final override;
71
virtual float surface_get_hdr_output_max_value(SurfaceID p_surface) const final override;
72
uint32_t surface_get_width(SurfaceID p_surface) const final override;
73
uint32_t surface_get_height(SurfaceID p_surface) const final override;
74
void surface_set_needs_resize(SurfaceID p_surface, bool p_needs_resize) final override;
75
bool surface_get_needs_resize(SurfaceID p_surface) const final override;
76
void surface_destroy(SurfaceID p_surface) final override;
77
bool is_debug_utils_enabled() const final override { return capture_available; }
78
79
#pragma mark - Metal-specific methods
80
81
// Platform-specific data for the Windows embedded in this driver.
82
struct WindowPlatformData {
83
CA::MetalLayer *layer;
84
};
85
86
class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) Surface {
87
protected:
88
MTL::Device *device;
89
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
97
bool hdr_output = false;
98
// BT.2408 recommendation of 203 nits for HDR Reference White, rounded to 200
99
// to be a more pleasant player-facing value.
100
float hdr_reference_luminance = 200.0f;
101
float hdr_max_luminance = 1000.0f;
102
float hdr_linear_luminance_scale = 100.0f;
103
104
Surface(MTL::Device *p_device) :
105
device(p_device) {}
106
virtual ~Surface() = default;
107
108
MTL::PixelFormat get_pixel_format() const { return MTL::PixelFormatBGRA8Unorm; }
109
virtual Error resize(uint32_t p_desired_framebuffer_count) = 0;
110
virtual RDD::FramebufferID acquire_next_frame_buffer() = 0;
111
virtual void present(MTL3::MDCommandBuffer *p_cmd_buffer) = 0;
112
virtual MTL::Drawable *next_drawable() = 0;
113
API_AVAILABLE(macos(26.0), ios(26.0))
114
virtual MTL::ResidencySet *get_residency_set() const = 0;
115
void set_max_fps(int p_max_fps) { present_minimum_duration = p_max_fps ? 1.0 / p_max_fps : 0.0; }
116
};
117
118
MTL::Device *get_metal_device() const {
119
return metal_device;
120
}
121
122
#pragma mark - Initialization
123
124
RenderingContextDriverMetal();
125
~RenderingContextDriverMetal() override;
126
};
127
128
#endif // METAL_ENABLED
129
130