/**************************************************************************/1/* metal_utils.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#include <os/log.h>3334#include <functional>3536/// Godot limits the number of dynamic buffers to 8.37///38/// This is a minimum guarantee for Vulkan.39constexpr uint32_t MAX_DYNAMIC_BUFFERS = 8;4041// From rendering/rendering_device/vsync/frame_queue_size42static constexpr uint32_t MAX_FRAME_COUNT = 3;4344#pragma mark - Boolean flags4546namespace flags {4748/*! Sets the flags within the value parameter specified by the mask parameter. */49template <typename Tv, typename Tm>50void set(Tv &p_value, Tm p_mask) {51using T = std::underlying_type_t<Tv>;52p_value = static_cast<Tv>(static_cast<T>(p_value) | static_cast<T>(p_mask));53}5455/*! Clears the flags within the value parameter specified by the mask parameter. */56template <typename Tv, typename Tm>57void clear(Tv &p_value, Tm p_mask) {58using T = std::underlying_type_t<Tv>;59p_value = static_cast<Tv>(static_cast<T>(p_value) & ~static_cast<T>(p_mask));60}6162/*! Returns whether the specified value has any of the bits specified in mask set to 1. */63template <typename Tv, typename Tm>64static constexpr bool any(Tv p_value, const Tm p_mask) {65return ((p_value & p_mask) != 0);66}6768/*! Returns whether the specified value has all of the bits specified in mask set to 1. */69template <typename Tv, typename Tm>70static constexpr bool all(Tv p_value, const Tm p_mask) {71return ((p_value & p_mask) == p_mask);72}7374} //namespace flags7576#pragma mark - Alignment and Offsets7778static constexpr bool is_power_of_two(uint64_t p_value) {79return p_value && ((p_value & (p_value - 1)) == 0);80}8182static constexpr uint64_t round_up_to_alignment(uint64_t p_value, uint64_t p_alignment) {83DEV_ASSERT(is_power_of_two(p_alignment));8485if (p_alignment == 0) {86return p_value;87}8889uint64_t mask = p_alignment - 1;90uint64_t aligned_value = (p_value + mask) & ~mask;9192return aligned_value;93}9495template <typename F>96class Defer {97public:98explicit Defer(F &&f) :99func_(std::forward<F>(f)) {}100~Defer() { func_(); }101102// Non-copyable (correct RAII semantics)103Defer(const Defer &) = delete;104Defer &operator=(const Defer &) = delete;105106// Movable107Defer(Defer &&) = default;108Defer &operator=(Defer &&) = default;109110private:111F func_;112};113114// C++17 class template argument deduction.115template <typename F>116Defer(F &&) -> Defer<std::decay_t<F>>;117118#define CONCAT_INTERNAL(x, y) x##y119#define CONCAT(x, y) CONCAT_INTERNAL(x, y)120#define DEFER const auto &CONCAT(defer__, __LINE__) = Defer121122extern os_log_t LOG_DRIVER;123// Used for dynamic tracing.124extern os_log_t LOG_INTERVALS;125126_FORCE_INLINE_ static constexpr uint32_t make_msl_version(uint32_t p_major, uint32_t p_minor = 0, uint32_t p_patch = 0) {127return (p_major * 10000) + (p_minor * 100) + p_patch;128}129130_FORCE_INLINE_ static constexpr void parse_msl_version(uint32_t p_version, uint32_t &r_major, uint32_t &r_minor) {131r_major = p_version / 10000;132r_minor = (p_version % 10000) / 100;133}134135constexpr uint32_t MSL_VERSION_23 = make_msl_version(2, 3);136constexpr uint32_t MSL_VERSION_24 = make_msl_version(2, 4);137constexpr uint32_t MSL_VERSION_30 = make_msl_version(3, 0);138constexpr uint32_t MSL_VERSION_31 = make_msl_version(3, 1);139constexpr uint32_t MSL_VERSION_32 = make_msl_version(3, 2);140constexpr uint32_t MSL_VERSION_40 = make_msl_version(4, 0);141142/* MSL Language version table143*144* | Version | macOS | iOS |145* |---------|---------|---------|146* | 1.0 | | 9.0 |147* | 1.1 | 10.11 | 9.0 |148* | 1.2 | 10.12 | 10.0 |149* | 2.0 | 10.13 | 11.0 |150* | 2.1 | 10.14 | 12.0 |151* | 2.2 | 10.15 | 13.0 |152* | 2.3 | 11.0 | 14.0 |153* | 2.4 | 12.0 | 15.0 |154* | 3.0 | 13.0 | 16.0 |155* | 3.1 | 14.0 | 17.0 |156* | 3.2 | 15.0 | 18.0 |157* | 4.0 | 26.0 | 26.0 |158* |---------|---------|---------|159*/160161162