Debian Codegraph @

servers/rendering/storage/variant_converters.h

raw symbols SCIP
1 /**************************************************************************/
2 /* variant_converters.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 "core/error/error_macros.h"
34 #include "core/variant/array.h"
35 #include "core/variant/variant.h"
36
37 #include <initializer_list>
38 #include <type_traits>
39
40 template <typename T>
41 struct VariantConverterStd140 {
42 // Generic base template for all Vector2/3/4(i) classes.
43 static constexpr int Elements = T::AXIS_COUNT;
44
45 template <typename P>
46 static void convert(const T &p_v, P *p_write, bool p_compact) {
47 for (int i = 0; i < Elements; i++) {
48 p_write[i] = p_v[i];
49 }
50 }
51 };
52
53 template <>
54 struct VariantConverterStd140<float> {
55 static constexpr int Elements = 1;
56
57 template <typename P>
58 static void convert(float p_v, P *p_write, bool p_compact) {
59 p_write[0] = p_v;
60 }
61 };
62
63 template <>
64 struct VariantConverterStd140<int32_t> {
65 static constexpr int Elements = 1;
66
67 template <typename P>
68 static void convert(int32_t p_v, P *p_write, bool p_compact) {
69 p_write[0] = p_v;
70 }
71 };
72
73 template <>
74 struct VariantConverterStd140<uint32_t> {
75 static constexpr int Elements = 1;
76
77 template <typename P>
78 static void convert(uint32_t p_v, P *p_write, bool p_compact) {
79 p_write[0] = p_v;
80 }
81 };
82
83 template <>
84 struct VariantConverterStd140<Basis> {
85 static constexpr int Elements = 9;
86
87 template <typename P>
88 static void convert(const Basis &p_v, P *p_write, bool p_compact) {
89 // Basis can have compact 9 floats or std140 layout 12 floats.
90 int i = 0;
91
92 p_write[i++] = p_v.rows[0][0];
93 p_write[i++] = p_v.rows[1][0];
94 p_write[i++] = p_v.rows[2][0];
95 if (!p_compact) {
96 p_write[i++] = 0;
97 }
98
99 p_write[i++] = p_v.rows[0][1];
100 p_write[i++] = p_v.rows[1][1];
101 p_write[i++] = p_v.rows[2][1];
102 if (!p_compact) {
103 p_write[i++] = 0;
104 }
105
106 p_write[i++] = p_v.rows[0][2];
107 p_write[i++] = p_v.rows[1][2];
108 p_write[i++] = p_v.rows[2][2];
109 if (!p_compact) {
110 p_write[i++] = 0;
111 }
112 }
113 };
114
115 template <>
116 struct VariantConverterStd140<Transform2D> {
117 static constexpr int Elements = 12;
118
119 template <typename P>
120 static void convert(const Transform2D &p_v, P *p_write, bool p_compact) {
121 p_write[0] = p_v.columns[0][0];
122 p_write[1] = p_v.columns[0][1];
123 p_write[2] = 0;
124 p_write[3] = 0;
125
126 p_write[4] = p_v.columns[1][0];
127 p_write[5] = p_v.columns[1][1];
128 p_write[6] = 0;
129 p_write[7] = 0;
130
131 p_write[8] = p_v.columns[2][0];
132 p_write[9] = p_v.columns[2][1];
133 p_write[10] = 1;
134 p_write[11] = 0;
135 }
136 };
137
138 template <>
139 struct VariantConverterStd140<Transform3D> {
140 static constexpr int Elements = 16;
141
142 template <typename P>
143 static void convert(const Transform3D &p_v, P *p_write, bool p_compact) {
144 p_write[0] = p_v.basis.rows[0][0];
145 p_write[1] = p_v.basis.rows[1][0];
146 p_write[2] = p_v.basis.rows[2][0];
147 p_write[3] = 0;
148
149 p_write[4] = p_v.basis.rows[0][1];
150 p_write[5] = p_v.basis.rows[1][1];
151 p_write[6] = p_v.basis.rows[2][1];
152 p_write[7] = 0;
153
154 p_write[8] = p_v.basis.rows[0][2];
155 p_write[9] = p_v.basis.rows[1][2];
156 p_write[10] = p_v.basis.rows[2][2];
157 p_write[11] = 0;
158
159 p_write[12] = p_v.origin.x;
160 p_write[13] = p_v.origin.y;
161 p_write[14] = p_v.origin.z;
162 p_write[15] = 1;
163 }
164 };
165
166 template <>
167 struct VariantConverterStd140<Projection> {
168 static constexpr int Elements = 16;
169
170 template <typename P>
171 static void convert(const Projection &p_v, P *p_write, bool p_compact) {
172 for (int i = 0; i < 4; i++) {
173 for (int j = 0; j < 4; j++) {
174 p_write[i * 4 + j] = p_v.columns[i][j];
175 }
176 }
177 }
178 };
179
180 template <typename T, typename P>
181 T construct_vector(const std::initializer_list<P> &values) {
182 T vector{};
183 int index = 0;
184 for (P v : values) {
185 vector[index++] = v;
186 if (index >= T::AXIS_COUNT) {
187 break;
188 }
189 }
190 return vector;
191 }
192
193 // Compatibility converter, tries to convert certain Variant types into a Vector2/3/4(i).
194
195 template <typename T>
196 T convert_to_vector(const Variant &p_variant, bool p_linear_color = false) {
197 const Variant::Type type = p_variant.get_type();
198
199 if (type == Variant::QUATERNION) {
200 Quaternion quat = p_variant;
201 return construct_vector<T>({ quat.x, quat.y, quat.z, quat.w });
202 } else if (type == Variant::PLANE) {
203 Plane p = p_variant;
204 return construct_vector<T>({ p.normal.x, p.normal.y, p.normal.z, p.d });
205 } else if (type == Variant::RECT2 || type == Variant::RECT2I) {
206 Rect2 r = p_variant;
207 return construct_vector<T>({ r.position.x, r.position.y, r.size.x, r.size.y });
208 } else if (type == Variant::COLOR) {
209 Color c = p_variant;
210 if (p_linear_color) {
211 c = c.srgb_to_linear();
212 }
213 return construct_vector<T>({ c.r, c.g, c.b, c.a });
214 } else if (p_variant.is_array()) {
215 const Array &array = p_variant;
216 const int size = MIN(array.size(), T::AXIS_COUNT);
217 T vector{};
218 for (int i = 0; i < size; i++) {
219 vector[i] = array.get(i);
220 }
221 return vector;
222 }
223
224 return p_variant; // Default Variant conversion, covers all Vector2/3/4(i) types.
225 }
226
227 inline bool is_number_array(const Array &p_array) {
228 const int size = p_array.size();
229 for (int i = 0; i < size; i++) {
230 if (!p_array.get(i).is_num()) {
231 return false;
232 }
233 }
234 return true;
235 }
236
237 inline bool is_convertible_array(Variant::Type type) {
238 return type == Variant::ARRAY ||
239 type == Variant::PACKED_VECTOR2_ARRAY ||
240 type == Variant::PACKED_VECTOR3_ARRAY ||
241 type == Variant::PACKED_COLOR_ARRAY ||
242 type == Variant::PACKED_VECTOR4_ARRAY;
243 }
244
245 template <typename, typename = void>
246 inline constexpr bool is_vector_type_v = false;
247
248 template <typename T>
249 inline constexpr bool is_vector_type_v<T, std::void_t<decltype(T::AXIS_COUNT)>> = true;
250
251 template <typename T, typename P>
252 void convert_item_std140(const T &p_item, P *p_write, bool p_compact = false) {
253 VariantConverterStd140<T>::template convert<P>(p_item, p_write, p_compact);
254 }
255
256 template <typename T, typename P>
257 Vector<P> convert_array_std140(const Variant &p_variant, [[maybe_unused]] bool p_linear_color = false) {
258 if (is_convertible_array(p_variant.get_type())) {
259 // Slow path, convert Variant arrays and some packed arrays manually into primitive types.
260 const Array &array = p_variant;
261 if (is_number_array(array)) {
262 // Already flattened and converted (or empty) array, usually coming from saved resources.
263 return p_variant;
264 }
265
266 const int items = array.size();
267 constexpr int elements = VariantConverterStd140<T>::Elements;
268
269 Vector<P> result;
270 result.resize(items * elements);
271 P *write = result.ptrw();
272
273 for (int i = 0; i < items; i++) {
274 const Variant &item = array.get(i);
275 P *offset = write + (i * elements);
276
277 if constexpr (is_vector_type_v<T>) {
278 const T &vec = convert_to_vector<T>(item, p_linear_color);
279 convert_item_std140<T, P>(vec, offset, true);
280 } else {
281 convert_item_std140<T, P>(item.operator T(), offset, true);
282 }
283 }
284 return result;
285
286 } else if (p_variant.is_array()) {
287 // Fast path, return the packed array directly.
288 return p_variant;
289 }
290
291 // Not an array type. Usually happens with uninitialized null shader resource parameters.
292 // Just return an empty array, uniforms will be default initialized later.
293
294 return Vector<P>();
295 }
296
297 template <typename T, typename From, typename To>
298 void write_array_std140(const Vector<From> &p_values, To *p_write, int p_array_size, int p_stride) {
299 constexpr int elements = VariantConverterStd140<T>::Elements;
300 const int src_count = p_values.size();
301 const int dst_count = elements * p_array_size;
302 const int stride_count = p_stride * p_array_size;
303 const From *read = p_values.ptr();
304 const T default_value{};
305
306 memset(p_write, 0, sizeof(To) * stride_count);
307
308 for (int i = 0, j = 0; i < dst_count; i += elements, j += p_stride) {
309 if (i + elements - 1 < src_count) {
310 // Only copy full items with all elements, no partial or missing data.
311 for (int e = 0; e < elements; e++) {
312 DEV_ASSERT(j + e < stride_count && i + e < src_count);
313 p_write[j + e] = read[i + e];
314 }
315 } else {
316 // If not enough source data was passed in, write default values.
317 convert_item_std140(default_value, p_write + j);
318 }
319 }
320 }