GLSL Struct 1.4.0
glslstruct
Loading...
Searching...
No Matches
vec_traits.hpp
1/*
2 * glslstruct - a C++ library designed to easily represent GLSL's Uniform Buffer Objects (UBOs) and Shader Storage Buffer Objects (SSBOs) in C++.
3 *
4 * Licensed under the BSD 3-Clause License with Attribution Requirement.
5 * See the LICENSE file for details: https://github.com/MAIPA01/glslstruct/blob/main/LICENSE
6 *
7 * Copyright (c) 2025, Patryk Antosik (MAIPA01)
8 */
9
10#pragma once
11#ifndef _GLSL_STRUCT_VEC_TRAITS_HPP
12 #define _GLSL_STRUCT_VEC_TRAITS_HPP
13
14 #include <glslstruct/config.hpp>
15
17_GLSL_STRUCT_ERROR("This is only available for c++17 and greater!");
18 #else
19
20 #include <glslstruct/type/data/vec_data.hpp>
21
22namespace glslstruct {
23 #pragma region DEFAULT_TRAITS
24
25 /**
26 * @brief vec_traits for glm::vec values
27 * @ingroup glslstruct
28 */
29 template<class T, glm::length_t L, glm::qualifier Q>
30 struct vec_traits<glm::vec<L, T, Q> > {
31 /// @brief returns length of glm::vec
32 static _GLSL_STRUCT_CONSTEXPR17 size_t get_length() noexcept { return L; }
33
34 /// @brief returns ValueType of glm::vec
35 static _GLSL_STRUCT_CONSTEXPR17 ValueType get_value_type() noexcept { return glslstruct::get_value_type<T>(); }
36
37 /// @brief returns vec_data of glm::vec
38 static vec_data get_data(const glm::vec<L, T, Q>& value) { return vec_data(value); }
39
40 /// @brief returns glm::vec value from vec_data
41 static glm::vec<L, T, Q> get_value(const vec_data& data) {
42 const std::vector<std::byte>& bytes = data.data();
43
44 if _GLSL_STRUCT_CONSTEXPR17 (std::is_same_v<T, bool>) {
45 const size_t maxSize = std::min(bytes.size(), sizeof(int) * L);
46
47 glm::vec<L, int, Q> value;
48 std::copy_n(bytes.begin(), maxSize, reinterpret_cast<std::byte*>(glm::value_ptr(value)));
49 return value > 0;
50 }
51 else {
52 const size_t maxSize = std::min(bytes.size(), sizeof(T) * L);
53
54 glm::vec<L, T, Q> value;
55 std::copy_n(bytes.begin(), maxSize, reinterpret_cast<std::byte*>(glm::value_ptr(value)));
56 return value;
57 }
58 }
59 };
60
61 /**
62 * @brief vec_traits for mstd::vec values
63 * @ingroup glslstruct
64 */
65 template<class T, size_t N>
66 struct vec_traits<mstd::vec<N, T> > {
67 /// @brief returns length of mstd::vec
68 static _GLSL_STRUCT_CONSTEXPR17 size_t get_length() noexcept { return N; }
69
70 /// @brief returns ValueType of mstd::vec
71 static _GLSL_STRUCT_CONSTEXPR17 ValueType get_value_type() noexcept { return glslstruct::get_value_type<T>(); }
72
73 /// @brief returns vec_data of mstd::vec
74 static vec_data get_data(const mstd::vec<N, T>& value) { return vec_data(value); }
75
76 /// @brief returns mstd::vec value from vec_data
77 static mstd::vec<N, T> get_value(const vec_data& data) {
78 const std::vector<std::byte>& bytes = data.data();
79
80 if _GLSL_STRUCT_CONSTEXPR17 (std::is_same_v<T, bool>) {
81 const size_t maxSize = std::min(bytes.size(), sizeof(int) * N);
82
83 mstd::vec<N, int> iValue;
84 std::copy_n(bytes.begin(), maxSize, reinterpret_cast<std::byte*>(static_cast<int*>(iValue)));
85
86 mstd::vec<N, T> value;
87 for (size_t i = 0; i < N; ++i) { value[i] = iValue[i] > 0; }
88 return value;
89 }
90 else {
91 const size_t maxSize = std::min(bytes.size(), sizeof(T) * N);
92
93 mstd::vec<N, T> value;
94 std::copy_n(bytes.begin(), maxSize, reinterpret_cast<std::byte*>(static_cast<T*>(value)));
95 return value;
96 }
97 }
98 };
99
100 #pragma endregion
101
102 #pragma region FUNCTIONS
103 /// @brief returns length of T based on vec_traits
105 template<utils::glsl_vec T>
106 #else
107 template<class T, std::enable_if_t<utils::is_glsl_vec_v<T>, bool> = true>
108 #endif
109 static inline size_t get_vec_length() noexcept {
110 return vec_traits<T>::get_length();
111 }
112
113 /// @brief returns ValueType of T based on vec_traits
115 template<utils::glsl_vec T>
116 #else
117 template<class T, std::enable_if_t<utils::is_glsl_vec_v<T>, bool> = true>
118 #endif
119 static inline ValueType get_vec_value_type() noexcept {
120 return vec_traits<T>::get_value_type();
121 }
122
123 /// @brief returns vec_data of T based on vec_traits
125 template<utils::glsl_vec T>
126 #else
127 template<class T, std::enable_if_t<utils::is_glsl_vec_v<T>, bool> = true>
128 #endif
129 static inline vec_data get_vec_data(const T& value) {
130 return vec_traits<T>::get_data(value);
131 }
132
133 /// @brief returns vec value of type T from vec_data based on vec_traits
135 template<utils::glsl_vec T>
136 #else
137 template<class T, std::enable_if_t<utils::is_glsl_vec_v<T>, bool> = true>
138 #endif
139 static inline T get_vec_value(const vec_data& data) {
140 return vec_traits<T>::get_value(data);
141 }
142
143 /// @brief returns glsl vec type string
144 std::string vec_to_string(ValueType valueType, size_t length);
145
146 /// @brief returns glsl vec type string from type T based on vec_traits
148 template<utils::glsl_vec T>
149 #else
150 template<class T, std::enable_if_t<utils::is_glsl_vec_v<T>, bool> = true>
151 #endif
152 static inline std::string vec_to_string() {
153 return vec_to_string(get_vec_value_type<T>(), get_vec_length<T>());
154 }
155
156 #pragma endregion
157} // namespace glslstruct
158
159 #endif
160#endif
#define _GLSL_STRUCT_HAS_CXX20
check if compiler has c++ version greater or equal to c++20 and if user enabled c++20 features using ...
Definition config.hpp:142
#define _GLSL_STRUCT_HAS_CXX17
check if compiler has c++ version greater or equal to c++17
Definition config.hpp:130
#define _GLSL_STRUCT_CONSTEXPR17
constexpr for c++17 and higher
Definition config.hpp:196