variant_converters.h 9.9 KB

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