Path: blob/master/drivers/metal/rendering_context_driver_metal.h
21871 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#include <Metal/Metal.hpp>38#include <QuartzCore/QuartzCore.hpp>3940namespace MTL3 {41class MDCommandBuffer;42}4344class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) RenderingContextDriverMetal : public RenderingContextDriver {45bool capture_available = false;4647protected:48MTL::Device *metal_device = nullptr;49Device device; // There is only one device on Apple Silicon.5051public:52Error initialize() final override;53const Device &device_get(uint32_t p_device_index) const final override;54uint32_t device_get_count() const final override;55bool device_supports_present(uint32_t p_device_index, SurfaceID p_surface) const final override { return true; }56RenderingDeviceDriver *driver_create() final override;57void driver_free(RenderingDeviceDriver *p_driver) final override;58SurfaceID surface_create(const void *p_platform_data) final override;59void surface_set_size(SurfaceID p_surface, uint32_t p_width, uint32_t p_height) final override;60void surface_set_vsync_mode(SurfaceID p_surface, DisplayServer::VSyncMode p_vsync_mode) final override;61DisplayServer::VSyncMode surface_get_vsync_mode(SurfaceID p_surface) const final override;62virtual void surface_set_hdr_output_enabled(SurfaceID p_surface, bool p_enabled) final override;63virtual bool surface_get_hdr_output_enabled(SurfaceID p_surface) const final override;64virtual void surface_set_hdr_output_reference_luminance(SurfaceID p_surface, float p_reference_luminance) final override;65virtual float surface_get_hdr_output_reference_luminance(SurfaceID p_surface) const final override;66virtual void surface_set_hdr_output_max_luminance(SurfaceID p_surface, float p_max_luminance) final override;67virtual float surface_get_hdr_output_max_luminance(SurfaceID p_surface) const final override;68virtual void surface_set_hdr_output_linear_luminance_scale(SurfaceID p_surface, float p_linear_luminance_scale) final override;69virtual float surface_get_hdr_output_linear_luminance_scale(SurfaceID p_surface) const final override;70virtual float surface_get_hdr_output_max_value(SurfaceID p_surface) const final override;71uint32_t surface_get_width(SurfaceID p_surface) const final override;72uint32_t surface_get_height(SurfaceID p_surface) const final override;73void surface_set_needs_resize(SurfaceID p_surface, bool p_needs_resize) final override;74bool surface_get_needs_resize(SurfaceID p_surface) const final override;75void surface_destroy(SurfaceID p_surface) final override;76bool is_debug_utils_enabled() const final override { return capture_available; }7778#pragma mark - Metal-specific methods7980// Platform-specific data for the Windows embedded in this driver.81struct WindowPlatformData {82CA::MetalLayer *layer;83};8485class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) Surface {86protected:87MTL::Device *device;8889public:90uint32_t width = 0;91uint32_t height = 0;92DisplayServer::VSyncMode vsync_mode = DisplayServer::VSYNC_ENABLED;93bool needs_resize = false;94double present_minimum_duration = 0.0;9596bool hdr_output = false;97// BT.2408 recommendation of 203 nits for HDR Reference White, rounded to 20098// to be a more pleasant player-facing value.99float hdr_reference_luminance = 200.0f;100float hdr_max_luminance = 1000.0f;101float hdr_linear_luminance_scale = 100.0f;102103Surface(MTL::Device *p_device) :104device(p_device) {}105virtual ~Surface() = default;106107MTL::PixelFormat get_pixel_format() const { return MTL::PixelFormatBGRA8Unorm; }108virtual Error resize(uint32_t p_desired_framebuffer_count) = 0;109virtual RDD::FramebufferID acquire_next_frame_buffer() = 0;110virtual void present(MTL3::MDCommandBuffer *p_cmd_buffer) = 0;111virtual MTL::Drawable *next_drawable() = 0;112API_AVAILABLE(macos(26.0), ios(26.0))113virtual MTL::ResidencySet *get_residency_set() const = 0;114void set_max_fps(int p_max_fps) { present_minimum_duration = p_max_fps ? 1.0 / p_max_fps : 0.0; }115};116117MTL::Device *get_metal_device() const {118return metal_device;119}120121#pragma mark - Initialization122123RenderingContextDriverMetal();124~RenderingContextDriverMetal() override;125};126127#endif // METAL_ENABLED128129130