Debian Codegraph @

drivers/metal/metal_utils.h

raw symbols SCIP
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 /// 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 class Defer {
97 public:
98 Defer(std::function<void()> func) :
99 func_(func) {}
100 ~Defer() { func_(); }
101
102 private:
103 std::function<void()> func_;
104 };
105
106 #define CONCAT_INTERNAL(x, y) x##y
107 #define CONCAT(x, y) CONCAT_INTERNAL(x, y)
108 #define DEFER const Defer &CONCAT(defer__, __LINE__) = Defer
109
110 extern os_log_t LOG_DRIVER;
111 // Used for dynamic tracing.
112 extern os_log_t LOG_INTERVALS;
113
114 _FORCE_INLINE_ static constexpr uint32_t make_msl_version(uint32_t p_major, uint32_t p_minor = 0, uint32_t p_patch = 0) {
115 return (p_major * 10000) + (p_minor * 100) + p_patch;
116 }
117
118 _FORCE_INLINE_ static constexpr void parse_msl_version(uint32_t p_version, uint32_t &r_major, uint32_t &r_minor) {
119 r_major = p_version / 10000;
120 r_minor = (p_version % 10000) / 100;
121 }
122
123 constexpr uint32_t MSL_VERSION_23 = make_msl_version(2, 3);
124 constexpr uint32_t MSL_VERSION_24 = make_msl_version(2, 4);
125 constexpr uint32_t MSL_VERSION_30 = make_msl_version(3, 0);
126 constexpr uint32_t MSL_VERSION_31 = make_msl_version(3, 1);
127 constexpr uint32_t MSL_VERSION_32 = make_msl_version(3, 2);
128 constexpr uint32_t MSL_VERSION_40 = make_msl_version(4, 0);
129
130 /* MSL Language version table
131 *
132 * | Version | macOS | iOS |
133 * |---------|---------|---------|
134 * | 1.0 | | 9.0 |
135 * | 1.1 | 10.11 | 9.0 |
136 * | 1.2 | 10.12 | 10.0 |
137 * | 2.0 | 10.13 | 11.0 |
138 * | 2.1 | 10.14 | 12.0 |
139 * | 2.2 | 10.15 | 13.0 |
140 * | 2.3 | 11.0 | 14.0 |
141 * | 2.4 | 12.0 | 15.0 |
142 * | 3.0 | 13.0 | 16.0 |
143 * | 3.1 | 14.0 | 17.0 |
144 * | 3.2 | 15.0 | 18.0 |
145 * | 4.0 | 26.0 | 26.0 |
146 * |---------|---------|---------|
147 */