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#ifndef VARIANT_CONVERTERS_H
32#define VARIANT_CONVERTERS_H
33
34#include "core/error/error_macros.h"
35#include "core/variant/array.h"
36#include "core/variant/variant.h"
37
38#include <initializer_list>
39#include <type_traits>
40
41template <typename T>
42struct VariantConverterStd140 {
43 // Generic base template for all Vector2/3/4(i) classes.
44 static constexpr int Elements = T::AXIS_COUNT;
45
46 template <typename P>
47 static void convert(const T &p_v, P *p_write, bool p_compact) {
48 for (int i = 0; i < Elements; i++) {
49 p_write[i] = p_v[i];
50 }
51 }
52};
53
54template <>
55struct VariantConverterStd140<float> {
56 static constexpr int Elements = 1;
57
58 template <typename P>
59 static void convert(float p_v, P *p_write, bool p_compact) {
60 p_write[0] = p_v;
61 }
62};
63
64template <>
65struct VariantConverterStd140<int32_t> {
66 static constexpr int Elements = 1;
67
68 template <typename P>
69 static void convert(int32_t p_v, P *p_write, bool p_compact) {
70 p_write[0] = p_v;
71 }
72};
73
74template <>
75struct VariantConverterStd140<uint32_t> {
76 static constexpr int Elements = 1;
77
78 template <typename P>
79 static void convert(uint32_t p_v, P *p_write, bool p_compact) {
80 p_write[0] = p_v;
81 }
82};
83
84template <>
85struct VariantConverterStd140<Basis> {
86 static constexpr int Elements = 9;
87
88 template <typename P>
89 static void convert(const Basis &p_v, P *p_write, bool p_compact) {
90 // Basis can have compact 9 floats or std140 layout 12 floats.
91 int i = 0;
92
93 p_write[i++] = p_v.rows[0][0];
94 p_write[i++] = p_v.rows[1][0];
95 p_write[i++] = p_v.rows[2][0];
96 if (!p_compact) {
97 p_write[i++] = 0;
98 }
99
100 p_write[i++] = p_v.rows[0][1];
101 p_write[i++] = p_v.rows[1][1];
102 p_write[i++] = p_v.rows[2][1];
103 if (!p_compact) {
104 p_write[i++] = 0;
105 }
106
107 p_write[i++] = p_v.rows[0][2];
108 p_write[i++] = p_v.rows[1][2];
109 p_write[i++] = p_v.rows[2][2];
110 if (!p_compact) {
111 p_write[i++] = 0;
112 }
113 }
114};
115
116template <>
117struct VariantConverterStd140<Transform2D> {
118 static constexpr int Elements = 12;
119
120 template <typename P>
121 static void convert(const Transform2D &p_v, P *p_write, bool p_compact) {
122 p_write[0] = p_v.columns[0][0];
123 p_write[1] = p_v.columns[0][1];
124 p_write[2] = 0;
125 p_write[3] = 0;
126
127 p_write[4] = p_v.columns[1][0];
128 p_write[5] = p_v.columns[1][1];
129 p_write[6] = 0;
130 p_write[7] = 0;
131
132 p_write[8] = p_v.columns[2][0];
133 p_write[9] = p_v.columns[2][1];
134 p_write[10] = 1;
135 p_write[11] = 0;
136 }
137};
138
139template <>
140struct VariantConverterStd140<Transform3D> {
141 static constexpr int Elements = 16;
142
143 template <typename P>
144 static void convert(const Transform3D &p_v, P *p_write, bool p_compact) {
145 p_write[0] = p_v.basis.rows[0][0];
146 p_write[1] = p_v.basis.rows[1][0];
147 p_write[2] = p_v.basis.rows[2][0];
148 p_write[3] = 0;
149
150 p_write[4] = p_v.basis.rows[0][1];
151 p_write[5] = p_v.basis.rows[1][1];
152 p_write[6] = p_v.basis.rows[2][1];
153 p_write[7] = 0;
154
155 p_write[8] = p_v.basis.rows[0][2];
156 p_write[9] = p_v.basis.rows[1][2];
157 p_write[10] = p_v.basis.rows[2][2];
158 p_write[11] = 0;
159
160 p_write[12] = p_v.origin.x;
161 p_write[13] = p_v.origin.y;
162 p_write[14] = p_v.origin.z;
163 p_write[15] = 1;
164 }
165};
166
167template <>
168struct VariantConverterStd140<Projection> {
169 static constexpr int Elements = 16;
170
171 template <typename P>
172 static void convert(const Projection &p_v, P *p_write, bool p_compact) {
173 for (int i = 0; i < 4; i++) {
174 for (int j = 0; j < 4; j++) {
175 p_write[i * 4 + j] = p_v.columns[i][j];
176 }
177 }
178 }
179};
180
181template <typename T, typename P>
182T construct_vector(const std::initializer_list<P> &values) {
183 T vector{};
184 int index = 0;
185 for (P v : values) {
186 vector[index++] = v;
187 if (index >= T::AXIS_COUNT) {
188 break;
189 }
190 }
191 return vector;
192}
193
194// Compatibility converter, tries to convert certain Variant types into a Vector2/3/4(i).
195
196template <typename T>
197T convert_to_vector(const Variant &p_variant, bool p_linear_color = false) {
198 const Variant::Type type = p_variant.get_type();
199
200 if (type == Variant::QUATERNION) {
201 Quaternion quat = p_variant;
202 return construct_vector<T>({ quat.x, quat.y, quat.z, quat.w });
203 } else if (type == Variant::PLANE) {
204 Plane p = p_variant;
205 return construct_vector<T>({ p.normal.x, p.normal.y, p.normal.z, p.d });
206 } else if (type == Variant::RECT2 || type == Variant::RECT2I) {
207 Rect2 r = p_variant;
208 return construct_vector<T>({ r.position.x, r.position.y, r.size.x, r.size.y });
209 } else if (type == Variant::COLOR) {
210 Color c = p_variant;
211 if (p_linear_color) {
212 c = c.srgb_to_linear();
213 }
214 return construct_vector<T>({ c.r, c.g, c.b, c.a });
215 } else if (p_variant.is_array()) {
216 const Array &array = p_variant;
217 const int size = MIN(array.size(), T::AXIS_COUNT);
218 T vector{};
219 for (int i = 0; i < size; i++) {
220 vector[i] = array.get(i);
221 }
222 return vector;
223 }
224
225 return p_variant; // Default Variant conversion, covers all Vector2/3/4(i) types.
226}
227
228inline bool is_number_array(const Array &p_array) {
229 const int size = p_array.size();
230 for (int i = 0; i < size; i++) {
231 if (!p_array.get(i).is_num()) {
232 return false;
233 }
234 }
235 return true;
236}
237
238inline bool is_convertible_array(Variant::Type type) {
239 return type == Variant::ARRAY ||
240 type == Variant::PACKED_VECTOR2_ARRAY ||
241 type == Variant::PACKED_VECTOR3_ARRAY ||
242 type == Variant::PACKED_COLOR_ARRAY;
243}
244
245template <class, class = void>
246struct is_vector_type : std::false_type {};
247
248template <class T>
249struct is_vector_type<T, std::void_t<decltype(T::AXIS_COUNT)>> : std::true_type {};
250
251template <typename T, typename P>
252void 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
256template <typename T, typename P>
257Vector<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<T>::value) {
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
297template <typename T, typename From, typename To>
298void 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}
321
322#endif // VARIANT_CONVERTERS_H
323