Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/metal/metal_utils.h
9973 views
1
/**************************************************************************/
2
/* metal_utils.h */
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
#pragma once
32
33
#import <os/log.h>
34
35
#import <functional>
36
37
#pragma mark - Boolean flags
38
39
namespace flags {
40
41
/*! Sets the flags within the value parameter specified by the mask parameter. */
42
template <typename Tv, typename Tm>
43
void set(Tv &p_value, Tm p_mask) {
44
using T = std::underlying_type_t<Tv>;
45
p_value = static_cast<Tv>(static_cast<T>(p_value) | static_cast<T>(p_mask));
46
}
47
48
/*! Clears the flags within the value parameter specified by the mask parameter. */
49
template <typename Tv, typename Tm>
50
void clear(Tv &p_value, Tm p_mask) {
51
using T = std::underlying_type_t<Tv>;
52
p_value = static_cast<Tv>(static_cast<T>(p_value) & ~static_cast<T>(p_mask));
53
}
54
55
/*! Returns whether the specified value has any of the bits specified in mask set to 1. */
56
template <typename Tv, typename Tm>
57
static constexpr bool any(Tv p_value, const Tm p_mask) {
58
return ((p_value & p_mask) != 0);
59
}
60
61
/*! Returns whether the specified value has all of the bits specified in mask set to 1. */
62
template <typename Tv, typename Tm>
63
static constexpr bool all(Tv p_value, const Tm p_mask) {
64
return ((p_value & p_mask) == p_mask);
65
}
66
67
} //namespace flags
68
69
#pragma mark - Alignment and Offsets
70
71
static constexpr bool is_power_of_two(uint64_t p_value) {
72
return p_value && ((p_value & (p_value - 1)) == 0);
73
}
74
75
static constexpr uint64_t round_up_to_alignment(uint64_t p_value, uint64_t p_alignment) {
76
DEV_ASSERT(is_power_of_two(p_alignment));
77
78
if (p_alignment == 0) {
79
return p_value;
80
}
81
82
uint64_t mask = p_alignment - 1;
83
uint64_t aligned_value = (p_value + mask) & ~mask;
84
85
return aligned_value;
86
}
87
88
class Defer {
89
public:
90
Defer(std::function<void()> func) :
91
func_(func) {}
92
~Defer() { func_(); }
93
94
private:
95
std::function<void()> func_;
96
};
97
98
#define CONCAT_INTERNAL(x, y) x##y
99
#define CONCAT(x, y) CONCAT_INTERNAL(x, y)
100
#define DEFER const Defer &CONCAT(defer__, __LINE__) = Defer
101
102
extern os_log_t LOG_DRIVER;
103
// Used for dynamic tracing.
104
extern os_log_t LOG_INTERVALS;
105
106
_FORCE_INLINE_ static uint32_t make_msl_version(uint32_t major, uint32_t minor = 0, uint32_t patch = 0) {
107
return (major * 10000) + (minor * 100) + patch;
108
}
109
110