| 1 |
/**************************************************************************/ |
| 2 |
/* rendering_context_driver_metal.mm */ |
| 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 |
#import "rendering_context_driver_metal.h" |
| 32 |
|
| 33 |
#import "rendering_device_driver_metal.h" |
| 34 |
|
| 35 |
#include "core/templates/sort_array.h" |
| 36 |
|
| 37 |
#import <os/log.h> |
| 38 |
#import <os/signpost.h> |
| 39 |
|
| 40 |
#pragma mark - Logging |
| 41 |
|
| 42 |
os_log_t LOG_DRIVER; |
| 43 |
// Used for dynamic tracing. |
| 44 |
os_log_t LOG_INTERVALS; |
| 45 |
|
| 46 |
__attribute__((constructor)) static void InitializeLogging(void) { |
| 47 |
LOG_DRIVER = os_log_create("org.godotengine.godot.metal", OS_LOG_CATEGORY_POINTS_OF_INTEREST); |
| 48 |
LOG_INTERVALS = os_log_create("org.godotengine.godot.metal", "events"); |
| 49 |
} |
| 50 |
|
| 51 |
@protocol MTLDeviceEx <MTLDevice> |
| 52 |
#if TARGET_OS_OSX && __MAC_OS_X_VERSION_MAX_ALLOWED < 130300 |
| 53 |
- (void)setShouldMaximizeConcurrentCompilation:(BOOL)v; |
| 54 |
#endif |
| 55 |
@end |
| 56 |
|
| 57 |
RenderingContextDriverMetal::RenderingContextDriverMetal() { |
| 58 |
} |
| 59 |
|
| 60 |
RenderingContextDriverMetal::~RenderingContextDriverMetal() { |
| 61 |
} |
| 62 |
|
| 63 |
Error RenderingContextDriverMetal::initialize() { |
| 64 |
if (OS::get_singleton()->get_environment("MTL_CAPTURE_ENABLED") == "1") { |
| 65 |
capture_available = true; |
| 66 |
} |
| 67 |
|
| 68 |
metal_device = MTLCreateSystemDefaultDevice(); |
| 69 |
#if TARGET_OS_OSX |
| 70 |
if (@available(macOS 13.3, *)) { |
| 71 |
[id<MTLDeviceEx>(metal_device) setShouldMaximizeConcurrentCompilation:YES]; |
| 72 |
} |
| 73 |
#endif |
| 74 |
device.type = DEVICE_TYPE_INTEGRATED_GPU; |
| 75 |
device.vendor = Vendor::VENDOR_APPLE; |
| 76 |
device.workarounds = Workarounds(); |
| 77 |
|
| 78 |
MetalDeviceProperties props(metal_device); |
| 79 |
int version = (int)props.features.highestFamily - (int)MTLGPUFamilyApple1 + 1; |
| 80 |
device.name = vformat("%s (Apple%d)", metal_device.name.UTF8String, version); |
| 81 |
|
| 82 |
return OK; |
| 83 |
} |
| 84 |
|
| 85 |
const RenderingContextDriver::Device &RenderingContextDriverMetal::device_get(uint32_t p_device_index) const { |
| 86 |
DEV_ASSERT(p_device_index < 1); |
| 87 |
return device; |
| 88 |
} |
| 89 |
|
| 90 |
uint32_t RenderingContextDriverMetal::device_get_count() const { |
| 91 |
return 1; |
| 92 |
} |
| 93 |
|
| 94 |
RenderingDeviceDriver *RenderingContextDriverMetal::driver_create() { |
| 95 |
return memnew(RenderingDeviceDriverMetal(this)); |
| 96 |
} |
| 97 |
|
| 98 |
void RenderingContextDriverMetal::driver_free(RenderingDeviceDriver *p_driver) { |
| 99 |
memdelete(p_driver); |
| 100 |
} |
| 101 |
|
| 102 |
class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) SurfaceLayer : public RenderingContextDriverMetal::Surface { |
| 103 |
CAMetalLayer *__unsafe_unretained layer = nil; |
| 104 |
LocalVector<MDFrameBuffer> frame_buffers; |
| 105 |
LocalVector<id<MTLDrawable>> drawables; |
| 106 |
uint32_t rear = -1; |
| 107 |
uint32_t front = 0; |
| 108 |
uint32_t count = 0; |
| 109 |
|
| 110 |
public: |
| 111 |
SurfaceLayer(CAMetalLayer *p_layer, id<MTLDevice> p_device) : |
| 112 |
Surface(p_device), layer(p_layer) { |
| 113 |
layer.allowsNextDrawableTimeout = YES; |
| 114 |
layer.framebufferOnly = YES; |
| 115 |
layer.opaque = OS::get_singleton()->is_layered_allowed() ? NO : YES; |
| 116 |
layer.pixelFormat = get_pixel_format(); |
| 117 |
layer.device = p_device; |
| 118 |
} |
| 119 |
|
| 120 |
~SurfaceLayer() override { |
| 121 |
layer = nil; |
| 122 |
} |
| 123 |
|
| 124 |
Error resize(uint32_t p_desired_framebuffer_count) override final { |
| 125 |
if (width == 0 || height == 0) { |
| 126 |
// Very likely the window is minimized, don't create a swap chain. |
| 127 |
return ERR_SKIP; |
| 128 |
} |
| 129 |
|
| 130 |
CGSize drawableSize = CGSizeMake(width, height); |
| 131 |
CGSize current = layer.drawableSize; |
| 132 |
if (!CGSizeEqualToSize(current, drawableSize)) { |
| 133 |
layer.drawableSize = drawableSize; |
| 134 |
} |
| 135 |
|
| 136 |
// Metal supports a maximum of 3 drawables. |
| 137 |
p_desired_framebuffer_count = MIN(3U, p_desired_framebuffer_count); |
| 138 |
layer.maximumDrawableCount = p_desired_framebuffer_count; |
| 139 |
|
| 140 |
#if TARGET_OS_OSX |
| 141 |
// Display sync is only supported on macOS. |
| 142 |
switch (vsync_mode) { |
| 143 |
case DisplayServer::VSYNC_MAILBOX: |
| 144 |
case DisplayServer::VSYNC_ADAPTIVE: |
| 145 |
case DisplayServer::VSYNC_ENABLED: |
| 146 |
layer.displaySyncEnabled = YES; |
| 147 |
break; |
| 148 |
case DisplayServer::VSYNC_DISABLED: |
| 149 |
layer.displaySyncEnabled = NO; |
| 150 |
break; |
| 151 |
} |
| 152 |
#endif |
| 153 |
drawables.resize(p_desired_framebuffer_count); |
| 154 |
frame_buffers.resize(p_desired_framebuffer_count); |
| 155 |
for (uint32_t i = 0; i < p_desired_framebuffer_count; i++) { |
| 156 |
// Reserve space for the drawable texture. |
| 157 |
frame_buffers[i].set_texture_count(1); |
| 158 |
} |
| 159 |
|
| 160 |
return OK; |
| 161 |
} |
| 162 |
|
| 163 |
RDD::FramebufferID acquire_next_frame_buffer() override final { |
| 164 |
if (count == frame_buffers.size()) { |
| 165 |
return RDD::FramebufferID(); |
| 166 |
} |
| 167 |
|
| 168 |
rear = (rear + 1) % frame_buffers.size(); |
| 169 |
count++; |
| 170 |
|
| 171 |
MDFrameBuffer &frame_buffer = frame_buffers[rear]; |
| 172 |
frame_buffer.size = Size2i(width, height); |
| 173 |
|
| 174 |
id<CAMetalDrawable> drawable = layer.nextDrawable; |
| 175 |
ERR_FAIL_NULL_V_MSG(drawable, RDD::FramebufferID(), "no drawable available"); |
| 176 |
drawables[rear] = drawable; |
| 177 |
frame_buffer.set_texture(0, drawable.texture); |
| 178 |
|
| 179 |
return RDD::FramebufferID(&frame_buffer); |
| 180 |
} |
| 181 |
|
| 182 |
void present(MDCommandBuffer *p_cmd_buffer) override final { |
| 183 |
if (count == 0) { |
| 184 |
return; |
| 185 |
} |
| 186 |
|
| 187 |
// Release texture and drawable. |
| 188 |
frame_buffers[front].unset_texture(0); |
| 189 |
id<MTLDrawable> drawable = drawables[front]; |
| 190 |
drawables[front] = nil; |
| 191 |
|
| 192 |
count--; |
| 193 |
front = (front + 1) % frame_buffers.size(); |
| 194 |
|
| 195 |
if (vsync_mode != DisplayServer::VSYNC_DISABLED) { |
| 196 |
[p_cmd_buffer->get_command_buffer() presentDrawable:drawable afterMinimumDuration:present_minimum_duration]; |
| 197 |
} else { |
| 198 |
[p_cmd_buffer->get_command_buffer() presentDrawable:drawable]; |
| 199 |
} |
| 200 |
} |
| 201 |
}; |
| 202 |
|
| 203 |
class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) SurfaceOffscreen : public RenderingContextDriverMetal::Surface { |
| 204 |
int frame_buffer_size = 3; |
| 205 |
MDFrameBuffer *frame_buffers; |
| 206 |
LocalVector<id<MTLTexture>> textures; |
| 207 |
LocalVector<id<MTLDrawable>> drawables; |
| 208 |
|
| 209 |
int32_t rear = -1; |
| 210 |
std::atomic_int count; |
| 211 |
uint64_t target_time = 0; |
| 212 |
CAMetalLayer *layer; |
| 213 |
|
| 214 |
public: |
| 215 |
SurfaceOffscreen(CAMetalLayer *p_layer, id<MTLDevice> p_device) : |
| 216 |
Surface(p_device), layer(p_layer) { |
| 217 |
layer.allowsNextDrawableTimeout = YES; |
| 218 |
layer.framebufferOnly = YES; |
| 219 |
layer.opaque = OS::get_singleton()->is_layered_allowed() ? NO : YES; |
| 220 |
layer.pixelFormat = get_pixel_format(); |
| 221 |
layer.device = p_device; |
| 222 |
#if TARGET_OS_OSX |
| 223 |
layer.displaySyncEnabled = NO; |
| 224 |
#endif |
| 225 |
target_time = OS::get_singleton()->get_ticks_usec(); |
| 226 |
|
| 227 |
textures.resize(frame_buffer_size); |
| 228 |
drawables.resize(frame_buffer_size); |
| 229 |
|
| 230 |
frame_buffers = memnew_arr(MDFrameBuffer, frame_buffer_size); |
| 231 |
for (int i = 0; i < frame_buffer_size; i++) { |
| 232 |
frame_buffers[i].set_texture_count(1); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
~SurfaceOffscreen() override { |
| 237 |
memdelete_arr(frame_buffers); |
| 238 |
} |
| 239 |
|
| 240 |
Error resize(uint32_t p_desired_framebuffer_count) override final { |
| 241 |
if (width == 0 || height == 0) { |
| 242 |
// Very likely the window is minimized, don't create a swap chain. |
| 243 |
return ERR_SKIP; |
| 244 |
} |
| 245 |
|
| 246 |
CGSize drawableSize = CGSizeMake(width, height); |
| 247 |
CGSize current = layer.drawableSize; |
| 248 |
if (!CGSizeEqualToSize(current, drawableSize)) { |
| 249 |
layer.drawableSize = drawableSize; |
| 250 |
} |
| 251 |
|
| 252 |
return OK; |
| 253 |
} |
| 254 |
|
| 255 |
RDD::FramebufferID acquire_next_frame_buffer() override final { |
| 256 |
if (count.load(std::memory_order_relaxed) == 3) { |
| 257 |
// Wait for a frame to be presented. |
| 258 |
return RDD::FramebufferID(); |
| 259 |
} |
| 260 |
|
| 261 |
rear = (rear + 1) % 3; |
| 262 |
count.fetch_add(1, std::memory_order_relaxed); |
| 263 |
|
| 264 |
MDFrameBuffer &frame_buffer = frame_buffers[rear]; |
| 265 |
|
| 266 |
if (textures[rear] == nil || textures[rear].width != width || textures[rear].height != height) { |
| 267 |
MTLTextureDescriptor *texture_descriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:get_pixel_format() width:width height:height mipmapped:NO]; |
| 268 |
texture_descriptor.usage = MTLTextureUsageRenderTarget; |
| 269 |
texture_descriptor.hazardTrackingMode = MTLHazardTrackingModeUntracked; |
| 270 |
texture_descriptor.storageMode = MTLStorageModePrivate; |
| 271 |
textures[rear] = [device newTextureWithDescriptor:texture_descriptor]; |
| 272 |
} |
| 273 |
|
| 274 |
frame_buffer.size = Size2i(width, height); |
| 275 |
uint64_t now = OS::get_singleton()->get_ticks_usec(); |
| 276 |
if (now >= target_time) { |
| 277 |
target_time = now + 1'000'000; // 1 second into the future. |
| 278 |
id<CAMetalDrawable> drawable = layer.nextDrawable; |
| 279 |
ERR_FAIL_NULL_V_MSG(drawable, RDD::FramebufferID(), "no drawable available"); |
| 280 |
drawables[rear] = drawable; |
| 281 |
frame_buffer.set_texture(0, drawable.texture); |
| 282 |
} else { |
| 283 |
frame_buffer.set_texture(0, textures[rear]); |
| 284 |
} |
| 285 |
|
| 286 |
return RDD::FramebufferID(&frame_buffers[rear]); |
| 287 |
} |
| 288 |
|
| 289 |
void present(MDCommandBuffer *p_cmd_buffer) override final { |
| 290 |
MDFrameBuffer *frame_buffer = &frame_buffers[rear]; |
| 291 |
|
| 292 |
if (drawables[rear] != nil) { |
| 293 |
[p_cmd_buffer->get_command_buffer() presentDrawable:drawables[rear]]; |
| 294 |
drawables[rear] = nil; |
| 295 |
} |
| 296 |
|
| 297 |
[p_cmd_buffer->get_command_buffer() addScheduledHandler:^(id<MTLCommandBuffer> p_command_buffer) { |
| 298 |
frame_buffer->unset_texture(0); |
| 299 |
count.fetch_add(-1, std::memory_order_relaxed); |
| 300 |
}]; |
| 301 |
} |
| 302 |
}; |
| 303 |
|
| 304 |
RenderingContextDriver::SurfaceID RenderingContextDriverMetal::surface_create(const void *p_platform_data) { |
| 305 |
const WindowPlatformData *wpd = (const WindowPlatformData *)(p_platform_data); |
| 306 |
Surface *surface; |
| 307 |
if (String v = OS::get_singleton()->get_environment("GODOT_MTL_OFF_SCREEN"); v == U"1") { |
| 308 |
surface = memnew(SurfaceOffscreen(wpd->layer, metal_device)); |
| 309 |
} else { |
| 310 |
surface = memnew(SurfaceLayer(wpd->layer, metal_device)); |
| 311 |
} |
| 312 |
|
| 313 |
return SurfaceID(surface); |
| 314 |
} |
| 315 |
|
| 316 |
void RenderingContextDriverMetal::surface_set_size(SurfaceID p_surface, uint32_t p_width, uint32_t p_height) { |
| 317 |
Surface *surface = (Surface *)(p_surface); |
| 318 |
if (surface->width == p_width && surface->height == p_height) { |
| 319 |
return; |
| 320 |
} |
| 321 |
surface->width = p_width; |
| 322 |
surface->height = p_height; |
| 323 |
surface->needs_resize = true; |
| 324 |
} |
| 325 |
|
| 326 |
void RenderingContextDriverMetal::surface_set_vsync_mode(SurfaceID p_surface, DisplayServer::VSyncMode p_vsync_mode) { |
| 327 |
Surface *surface = (Surface *)(p_surface); |
| 328 |
if (surface->vsync_mode == p_vsync_mode) { |
| 329 |
return; |
| 330 |
} |
| 331 |
surface->vsync_mode = p_vsync_mode; |
| 332 |
surface->needs_resize = true; |
| 333 |
} |
| 334 |
|
| 335 |
DisplayServer::VSyncMode RenderingContextDriverMetal::surface_get_vsync_mode(SurfaceID p_surface) const { |
| 336 |
Surface *surface = (Surface *)(p_surface); |
| 337 |
return surface->vsync_mode; |
| 338 |
} |
| 339 |
|
| 340 |
uint32_t RenderingContextDriverMetal::surface_get_width(SurfaceID p_surface) const { |
| 341 |
Surface *surface = (Surface *)(p_surface); |
| 342 |
return surface->width; |
| 343 |
} |
| 344 |
|
| 345 |
uint32_t RenderingContextDriverMetal::surface_get_height(SurfaceID p_surface) const { |
| 346 |
Surface *surface = (Surface *)(p_surface); |
| 347 |
return surface->height; |
| 348 |
} |
| 349 |
|
| 350 |
void RenderingContextDriverMetal::surface_set_needs_resize(SurfaceID p_surface, bool p_needs_resize) { |
| 351 |
Surface *surface = (Surface *)(p_surface); |
| 352 |
surface->needs_resize = p_needs_resize; |
| 353 |
} |
| 354 |
|
| 355 |
bool RenderingContextDriverMetal::surface_get_needs_resize(SurfaceID p_surface) const { |
| 356 |
Surface *surface = (Surface *)(p_surface); |
| 357 |
return surface->needs_resize; |
| 358 |
} |
| 359 |
|
| 360 |
void RenderingContextDriverMetal::surface_destroy(SurfaceID p_surface) { |
| 361 |
Surface *surface = (Surface *)(p_surface); |
| 362 |
memdelete(surface); |
| 363 |
} |