GLSL Struct 1.4.0
glslstruct
Loading...
Searching...
No Matches
vec_data.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_DATA_HPP_
12 #define _GLSL_STRUCT_VEC_DATA_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/traits/concepts/vec_traits_concept.hpp>
21
22namespace glslstruct {
23 /**
24 * @brief glsl vec data container
25 * @ingroup glslstruct
26 */
27 class vec_data {
28 private:
29 /// @brief returns vector of data bytes of array of N values of type T
30 template<class T, size_t N>
31 [[nodiscard]] static _GLSL_STRUCT_CONSTEXPR20 std::vector<std::byte> _get_data(const std::array<T, N>& values) {
32 static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable to be treated as raw bytes!!");
33
34 if _GLSL_STRUCT_CONSTEXPR17 (std::is_same_v<T, bool>) {
35 std::vector<std::byte> data;
36 data.reserve(N * sizeof(int));
37
38 for (const auto& value : values) {
39 int iValue = static_cast<int>(value);
40 const auto* iValuePtr = reinterpret_cast<const std::byte*>(std::addressof(iValue));
41 data.insert(data.end(), iValuePtr, iValuePtr + sizeof(int));
42 }
43
44 return data;
45 }
46 else {
47 const auto* valuesPtr = reinterpret_cast<const std::byte*>(values.data());
48 return { valuesPtr, valuesPtr + (sizeof(T) * N) };
49 }
50 }
51
52 /// @brief returns array of N values of type T from glm::vec
53 template<class T, glm::length_t L, glm::qualifier Q>
54 [[nodiscard]] static _GLSL_STRUCT_CONSTEXPR17 std::array<T, L> _to_array(const glm::vec<L, T, Q>& value) {
55 std::array<T, L> array;
56 std::copy_n(glm::value_ptr(value), L, array.begin());
57 return array;
58 }
59
60 /// @brief returns array of N values of type T from mstd::vec
61 template<class T, size_t N>
62 [[nodiscard]] static _GLSL_STRUCT_CONSTEXPR17 std::array<T, N> _to_array(const mstd::vec<N, T>& value) {
63 std::array<T, N> array;
64 std::copy_n(static_cast<const T*>(value), N, array.begin());
65 return array;
66 }
67
68 /// @brief vector of data bytes
69 std::vector<std::byte> _data;
70
71 public:
72 /// @brief constructor which accepts raw data (not recommended to use for end user)
73 explicit vec_data(const std::vector<std::byte>& data);
74
75 /// @brief constructor for glm::vec
77 template<class T, glm::length_t L, glm::qualifier Q>
78 #else
79 template<class T, glm::length_t L, glm::qualifier Q,
80 std::enable_if_t<(mstd::is_same_type_in_v<T, bool, int, unsigned int, float, double> && mstd::is_in_range_v<L, 2, 4>),
81 bool> = true>
82 #endif
83 explicit vec_data(const glm::vec<L, T, Q>& value) _GLSL_STRUCT_REQUIRES((
84 mstd::is_same_type_in_v<T, bool, int, unsigned int, float, double> && mstd::is_in_range_v<L, 2, 4>
85 ))
86 : vec_data(_get_data(_to_array(value))) {
87 }
88
89 /// @brief constructor for mstd::vec
91 template<class T, size_t N>
92 #else
93 template<class T, size_t N,
94 std::enable_if_t<(mstd::is_same_type_in_v<T, bool, int, unsigned int, float, double> && mstd::is_in_range_v<N, 2, 4>),
95 bool> = true>
96 #endif
97 explicit vec_data(const mstd::vec<N, T>& value) _GLSL_STRUCT_REQUIRES((
98 mstd::is_same_type_in_v<T, bool, int, unsigned int, float, double> && mstd::is_in_range_v<N, 2, 4>
99 ))
100 : vec_data(_get_data(_to_array(value))) {
101 }
102
103 /// @brief default copy constructor
104 vec_data(const vec_data& other);
105 /// @brief default move constructor
106 vec_data(vec_data&& other) noexcept;
107
108 /// @brief default destructor
109 ~vec_data();
110
111 /// @brief default copy assign operator
112 vec_data& operator=(const vec_data& other);
113 /// @brief default move assign operator
114 vec_data& operator=(vec_data&& other) noexcept;
115
116 /// @brief returns value from this vec_data container using vec_traits conversion
118 template<utils::glsl_vec T>
119 #else
120 template<class T, std::enable_if_t<utils::is_glsl_vec_v<T>, bool> = true>
121 #endif
122 T get() const {
123 return vec_traits<T>::get_value(*this);
124 }
125
126 /// @brief returns value data in bytes
127 [[nodiscard]] const std::vector<std::byte>& data() const noexcept;
128 };
129} // namespace glslstruct
130
131 #endif
132#endif
#define _GLSL_STRUCT_CONSTEXPR20
constexpr keyword for c++20 and higher
Definition config.hpp:213
#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_REQUIRES(condition)
requires keyword for c++20 and higher
Definition config.hpp:214
#define _GLSL_STRUCT_CONSTEXPR17
constexpr for c++17 and higher
Definition config.hpp:196