/**************************************************************************/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#import <os/log.h>3334#import <functional>3536#pragma mark - Boolean flags3738namespace flags {3940/*! Sets the flags within the value parameter specified by the mask parameter. */41template <typename Tv, typename Tm>42void set(Tv &p_value, Tm p_mask) {43using T = std::underlying_type_t<Tv>;44p_value = static_cast<Tv>(static_cast<T>(p_value) | static_cast<T>(p_mask));45}4647/*! Clears the flags within the value parameter specified by the mask parameter. */48template <typename Tv, typename Tm>49void clear(Tv &p_value, Tm p_mask) {50using T = std::underlying_type_t<Tv>;51p_value = static_cast<Tv>(static_cast<T>(p_value) & ~static_cast<T>(p_mask));52}5354/*! Returns whether the specified value has any of the bits specified in mask set to 1. */55template <typename Tv, typename Tm>56static constexpr bool any(Tv p_value, const Tm p_mask) {57return ((p_value & p_mask) != 0);58}5960/*! Returns whether the specified value has all of the bits specified in mask set to 1. */61template <typename Tv, typename Tm>62static constexpr bool all(Tv p_value, const Tm p_mask) {63return ((p_value & p_mask) == p_mask);64}6566} //namespace flags6768#pragma mark - Alignment and Offsets6970static constexpr bool is_power_of_two(uint64_t p_value) {71return p_value && ((p_value & (p_value - 1)) == 0);72}7374static constexpr uint64_t round_up_to_alignment(uint64_t p_value, uint64_t p_alignment) {75DEV_ASSERT(is_power_of_two(p_alignment));7677if (p_alignment == 0) {78return p_value;79}8081uint64_t mask = p_alignment - 1;82uint64_t aligned_value = (p_value + mask) & ~mask;8384return aligned_value;85}8687class Defer {88public:89Defer(std::function<void()> func) :90func_(func) {}91~Defer() { func_(); }9293private:94std::function<void()> func_;95};9697#define CONCAT_INTERNAL(x, y) x##y98#define CONCAT(x, y) CONCAT_INTERNAL(x, y)99#define DEFER const Defer &CONCAT(defer__, __LINE__) = Defer100101extern os_log_t LOG_DRIVER;102// Used for dynamic tracing.103extern os_log_t LOG_INTERVALS;104105_FORCE_INLINE_ static uint32_t make_msl_version(uint32_t major, uint32_t minor = 0, uint32_t patch = 0) {106return (major * 10000) + (minor * 100) + patch;107}108109110