Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/metal/metal_utils.h
21654 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
#include <os/log.h>
34
35
#include <functional>
36
37
/// Godot limits the number of dynamic buffers to 8.
38
///
39
/// This is a minimum guarantee for Vulkan.
40
constexpr uint32_t MAX_DYNAMIC_BUFFERS = 8;
41
42
// From rendering/rendering_device/vsync/frame_queue_size
43
static constexpr uint32_t MAX_FRAME_COUNT = 3;
44
45
#pragma mark - Boolean flags
46
47
namespace flags {
48
49
/*! Sets the flags within the value parameter specified by the mask parameter. */
50
template <typename Tv, typename Tm>
51
void set(Tv &p_value, Tm p_mask) {
52
using T = std::underlying_type_t<Tv>;
53
p_value = static_cast<Tv>(static_cast<T>(p_value) | static_cast<T>(p_mask));
54
}
55
56
/*! Clears the flags within the value parameter specified by the mask parameter. */
57
template <typename Tv, typename Tm>
58
void clear(Tv &p_value, Tm p_mask) {
59
using T = std::underlying_type_t<Tv>;
60
p_value = static_cast<Tv>(static_cast<T>(p_value) & ~static_cast<T>(p_mask));
61
}
62
63
/*! Returns whether the specified value has any of the bits specified in mask set to 1. */
64
template <typename Tv, typename Tm>
65
static constexpr bool any(Tv p_value, const Tm p_mask) {
66
return ((p_value & p_mask) != 0);
67
}
68
69
/*! Returns whether the specified value has all of the bits specified in mask set to 1. */
70
template <typename Tv, typename Tm>
71
static constexpr bool all(Tv p_value, const Tm p_mask) {
72
return ((p_value & p_mask) == p_mask);
73
}
74
75
} //namespace flags
76
77
#pragma mark - Alignment and Offsets
78
79
static constexpr bool is_power_of_two(uint64_t p_value) {
80
return p_value && ((p_value & (p_value - 1)) == 0);
81
}
82
83
static constexpr uint64_t round_up_to_alignment(uint64_t p_value, uint64_t p_alignment) {
84
DEV_ASSERT(is_power_of_two(p_alignment));
85
86
if (p_alignment == 0) {
87
return p_value;
88
}
89
90
uint64_t mask = p_alignment - 1;
91
uint64_t aligned_value = (p_value + mask) & ~mask;
92
93
return aligned_value;
94
}
95
96
template <typename F>
97
class Defer {
98
public:
99
explicit Defer(F &&f) :
100
func_(std::forward<F>(f)) {}
101
~Defer() { func_(); }
102
103
// Non-copyable (correct RAII semantics)
104
Defer(const Defer &) = delete;
105
Defer &operator=(const Defer &) = delete;
106
107
// Movable
108
Defer(Defer &&) = default;
109
Defer &operator=(Defer &&) = default;
110
111
private:
112
F func_;
113
};
114
115
// C++17 class template argument deduction.
116
template <typename F>
117
Defer(F &&) -> Defer<std::decay_t<F>>;
118
119
#define CONCAT_INTERNAL(x, y) x##y
120
#define CONCAT(x, y) CONCAT_INTERNAL(x, y)
121
#define DEFER const auto &CONCAT(defer__, __LINE__) = Defer
122
123
extern os_log_t LOG_DRIVER;
124
// Used for dynamic tracing.
125
extern os_log_t LOG_INTERVALS;
126
127
_FORCE_INLINE_ static constexpr uint32_t make_msl_version(uint32_t p_major, uint32_t p_minor = 0, uint32_t p_patch = 0) {
128
return (p_major * 10000) + (p_minor * 100) + p_patch;
129
}
130
131
_FORCE_INLINE_ static constexpr void parse_msl_version(uint32_t p_version, uint32_t &r_major, uint32_t &r_minor) {
132
r_major = p_version / 10000;
133
r_minor = (p_version % 10000) / 100;
134
}
135
136
constexpr uint32_t MSL_VERSION_23 = make_msl_version(2, 3);
137
constexpr uint32_t MSL_VERSION_24 = make_msl_version(2, 4);
138
constexpr uint32_t MSL_VERSION_30 = make_msl_version(3, 0);
139
constexpr uint32_t MSL_VERSION_31 = make_msl_version(3, 1);
140
constexpr uint32_t MSL_VERSION_32 = make_msl_version(3, 2);
141
constexpr uint32_t MSL_VERSION_40 = make_msl_version(4, 0);
142
143
/* MSL Language version table
144
*
145
* | Version | macOS | iOS |
146
* |---------|---------|---------|
147
* | 1.0 | | 9.0 |
148
* | 1.1 | 10.11 | 9.0 |
149
* | 1.2 | 10.12 | 10.0 |
150
* | 2.0 | 10.13 | 11.0 |
151
* | 2.1 | 10.14 | 12.0 |
152
* | 2.2 | 10.15 | 13.0 |
153
* | 2.3 | 11.0 | 14.0 |
154
* | 2.4 | 12.0 | 15.0 |
155
* | 3.0 | 13.0 | 16.0 |
156
* | 3.1 | 14.0 | 17.0 |
157
* | 3.2 | 15.0 | 18.0 |
158
* | 4.0 | 26.0 | 26.0 |
159
* |---------|---------|---------|
160
*/
161
162