Sparkle 0.0.1
Loading...
Searching...
No Matches
spk_uniform_object.hpp
1#pragma once
2
3#include "structure/design_pattern/spk_activable_object.hpp"
4#include "structure/design_pattern/spk_synchronizable_object.hpp"
5#include "structure/math/spk_matrix.hpp"
6#include "structure/math/spk_vector2.hpp"
7#include "structure/math/spk_vector3.hpp"
8#include "structure/math/spk_vector4.hpp"
9#include "structure/opengl/spk_opengl_includes.hpp"
10#include <cmath>
11#include <cstdint>
12#include <stdexcept>
13#include <type_traits>
14#include <utility>
15#include <variant>
16
17namespace spk
18{
19 namespace OpenGL
20 {
21 namespace detail
22 {
23 template <typename T>
24 struct IsSupportedUniformTypeChecker : std::false_type
25 {
26 };
27
28 template <>
29 struct IsSupportedUniformTypeChecker<float_t> : std::true_type
30 {
31 };
32 template <>
33 struct IsSupportedUniformTypeChecker<int32_t> : std::true_type
34 {
35 };
36 template <>
37 struct IsSupportedUniformTypeChecker<uint32_t> : std::true_type
38 {
39 };
40 template <>
41 struct IsSupportedUniformTypeChecker<bool> : std::true_type
42 {
43 };
44 template <>
45 struct IsSupportedUniformTypeChecker<spk::Vector2> : std::true_type
46 {
47 };
48 template <>
49 struct IsSupportedUniformTypeChecker<spk::Vector2Int> : std::true_type
50 {
51 };
52 template <>
53 struct IsSupportedUniformTypeChecker<spk::Vector2UInt> : std::true_type
54 {
55 };
56 template <>
57 struct IsSupportedUniformTypeChecker<spk::Vector3> : std::true_type
58 {
59 };
60 template <>
61 struct IsSupportedUniformTypeChecker<spk::Vector3Int> : std::true_type
62 {
63 };
64 template <>
65 struct IsSupportedUniformTypeChecker<spk::Vector3UInt> : std::true_type
66 {
67 };
68 template <>
69 struct IsSupportedUniformTypeChecker<spk::Vector4> : std::true_type
70 {
71 };
72 template <>
73 struct IsSupportedUniformTypeChecker<spk::Vector4Int> : std::true_type
74 {
75 };
76 template <>
77 struct IsSupportedUniformTypeChecker<spk::Vector4UInt> : std::true_type
78 {
79 };
80 template <>
81 struct IsSupportedUniformTypeChecker<spk::Matrix2x2> : std::true_type
82 {
83 };
84 template <>
85 struct IsSupportedUniformTypeChecker<spk::Matrix3x3> : std::true_type
86 {
87 };
88 template <>
89 struct IsSupportedUniformTypeChecker<spk::Matrix4x4> : std::true_type
90 {
91 };
92
93 template <typename TType>
94 inline constexpr bool IsSupportedUniformType = IsSupportedUniformTypeChecker<std::decay_t<TType>>::value;
95 }
96
102 {
103 private:
104 using ValueStorage = std::variant<
105 std::monostate,
106 float_t,
107 int32_t,
108 uint32_t,
109 bool,
110 spk::Vector2,
111 spk::Vector2Int,
112 spk::Vector2UInt,
113 spk::Vector3,
114 spk::Vector3Int,
115 spk::Vector3UInt,
116 spk::Vector4,
117 spk::Vector4Int,
118 spk::Vector4UInt,
119 spk::Matrix2x2,
120 spk::Matrix3x3,
121 spk::Matrix4x4>;
122
123 int32_t _uniformLocation = -1;
124 ValueStorage _value = std::monostate{};
125
126 void _applyValue(const std::monostate &);
127 void _applyValue(const float_t &p_value);
128 void _applyValue(const int32_t &p_value);
129 void _applyValue(const uint32_t &p_value);
130 void _applyValue(const bool &p_value);
131 void _applyValue(const spk::Vector2 &p_value);
132 void _applyValue(const spk::Vector2Int &p_value);
133 void _applyValue(const spk::Vector2UInt &p_value);
134 void _applyValue(const spk::Vector3 &p_value);
135 void _applyValue(const spk::Vector3Int &p_value);
136 void _applyValue(const spk::Vector3UInt &p_value);
137 void _applyValue(const spk::Vector4 &p_value);
138 void _applyValue(const spk::Vector4Int &p_value);
139 void _applyValue(const spk::Vector4UInt &p_value);
140 void _applyValue(const spk::Matrix2x2 &p_value);
141 void _applyValue(const spk::Matrix3x3 &p_value);
142 void _applyValue(const spk::Matrix4x4 &p_value);
143
144 void _onSynchronize() override;
145 void _registerCallbacks();
146
147 public:
152 explicit UniformObject(GLint p_uniformLocation = -1);
157 UniformObject(const UniformObject &p_other);
163 UniformObject &operator=(const UniformObject &p_other);
164
165 template <typename TType>
171 void set(TType &&p_value)
172 {
173 using Decayed = std::decay_t<TType>;
174 static_assert(detail::IsSupportedUniformType<Decayed>, "Unsupported uniform type for UniformObject");
175
176 _value = std::forward<TType>(p_value);
178 }
179
180 template <typename TType>
186 const TType &get() const
187 {
188 return (std::get<TType>(_value));
189 }
190
195 bool hasValue() const;
196 };
197 }
198}
Stateful helper toggling between activated/deactivated states.
Definition spk_activable_object.hpp:20
void set(TType &&p_value)
Sets the uniform value and schedules synchronization.
Definition spk_uniform_object.hpp:171
bool hasValue() const
Indicates if a value is currently set.
Definition spk_uniform_object.cpp:162
UniformObject & operator=(const UniformObject &p_other)
Copies uniform location and stored value.
Definition spk_uniform_object.cpp:140
UniformObject(GLint p_uniformLocation=-1)
Creates a uniform wrapper for a specific location.
Definition spk_uniform_object.cpp:113
const TType & get() const
Retrieves the stored uniform value.
Definition spk_uniform_object.hpp:186
Base for objects requiring deferred synchronization hooks.
Definition spk_synchronizable_object.hpp:10
void requestSynchronization() noexcept
Marks the object as needing synchronization.
Definition spk_synchronizable_object.hpp:23
Definition spk_uniform_object.hpp:25