Path: blob/master/drivers/metal/rendering_context_driver_metal.h
9973 views
/**************************************************************************/1/* rendering_context_driver_metal.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#ifdef METAL_ENABLED3334#include "servers/rendering/rendering_context_driver.h"35#include "servers/rendering/rendering_device_driver.h"3637#import <CoreGraphics/CGGeometry.h>3839#ifdef __OBJC__40#import "metal_objects.h"4142#import <Metal/Metal.h>43#import <QuartzCore/CALayer.h>4445@class CAMetalLayer;46@protocol CAMetalDrawable;47#else48typedef enum MTLPixelFormat {49MTLPixelFormatBGRA8Unorm = 80,50} MTLPixelFormat;51class MDCommandBuffer;52#endif5354class PixelFormats;55class MDResourceCache;5657class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) RenderingContextDriverMetal : public RenderingContextDriver {58bool capture_available = false;5960protected:61#ifdef __OBJC__62id<MTLDevice> metal_device = nullptr;63#else64void *metal_device = nullptr;65#endif66Device device; // There is only one device on Apple Silicon.6768public:69Error initialize() final override;70const Device &device_get(uint32_t p_device_index) const final override;71uint32_t device_get_count() const final override;72bool device_supports_present(uint32_t p_device_index, SurfaceID p_surface) const final override { return true; }73RenderingDeviceDriver *driver_create() final override;74void driver_free(RenderingDeviceDriver *p_driver) final override;75SurfaceID surface_create(const void *p_platform_data) final override;76void surface_set_size(SurfaceID p_surface, uint32_t p_width, uint32_t p_height) final override;77void surface_set_vsync_mode(SurfaceID p_surface, DisplayServer::VSyncMode p_vsync_mode) final override;78DisplayServer::VSyncMode surface_get_vsync_mode(SurfaceID p_surface) const final override;79uint32_t surface_get_width(SurfaceID p_surface) const final override;80uint32_t surface_get_height(SurfaceID p_surface) const final override;81void surface_set_needs_resize(SurfaceID p_surface, bool p_needs_resize) final override;82bool surface_get_needs_resize(SurfaceID p_surface) const final override;83void surface_destroy(SurfaceID p_surface) final override;84bool is_debug_utils_enabled() const final override { return capture_available; }8586#pragma mark - Metal-specific methods8788// Platform-specific data for the Windows embedded in this driver.89struct WindowPlatformData {90#ifdef __OBJC__91CAMetalLayer *__unsafe_unretained layer;92#else93void *layer;94#endif95};9697class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) Surface {98protected:99#ifdef __OBJC__100id<MTLDevice> device;101#else102void *device;103#endif104105public:106uint32_t width = 0;107uint32_t height = 0;108DisplayServer::VSyncMode vsync_mode = DisplayServer::VSYNC_ENABLED;109bool needs_resize = false;110double present_minimum_duration = 0.0;111112Surface(113#ifdef __OBJC__114id<MTLDevice> p_device115#else116void *p_device117#endif118) :119device(p_device) {120}121virtual ~Surface() = default;122123MTLPixelFormat get_pixel_format() const { return MTLPixelFormatBGRA8Unorm; }124virtual Error resize(uint32_t p_desired_framebuffer_count) = 0;125virtual RDD::FramebufferID acquire_next_frame_buffer() = 0;126virtual void present(MDCommandBuffer *p_cmd_buffer) = 0;127void set_max_fps(int p_max_fps) { present_minimum_duration = p_max_fps ? 1.0 / p_max_fps : 0.0; }128};129130#ifdef __OBJC__131id<MTLDevice>132#else133void *134#endif135get_metal_device() const {136return metal_device;137}138139#pragma mark - Initialization140141RenderingContextDriverMetal();142~RenderingContextDriverMetal() override;143};144145#endif // METAL_ENABLED146147148