galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
Shader.hpp
Go to the documentation of this file.
1
7
8#ifndef GALAXY_GRAPHICS_SHADER_HPP_
9#define GALAXY_GRAPHICS_SHADER_HPP_
10
11#include <ankerl/unordered_dense.h>
12#include <Raylib.hpp>
13
15
16namespace galaxy
17{
23 class Shader final
24 {
25 public:
29 Shader() noexcept;
30
34 Shader(Shader&&) noexcept;
35
39 Shader& operator=(Shader&&) noexcept;
40
44 ~Shader() noexcept;
45
53 [[nodiscard]]
54 bool load(const std::string& file) noexcept;
55
64 [[nodiscard]]
65 bool load(const std::string& vertex, const std::string& frag) noexcept;
66
74 [[nodiscard]]
75 bool parse(const std::string& src) noexcept;
76
85 [[nodiscard]]
86 bool parse(const std::string& vertex, const std::string& frag) noexcept;
87
97 template<typename... Uniforms>
98 void set_uniform(const std::string& name, const Uniforms&... args);
99
100 private:
104 Shader(const Shader&) = delete;
105
109 Shader& operator=(const Shader&) = delete;
110
118 [[nodiscard]]
119 int get_uniform_location(const std::string& name);
120
121 private:
125 ray::Shader m_shader;
126
130 ankerl::unordered_dense::map<std::string, int> m_cache;
131 };
132
133 template<>
134 inline void Shader::set_uniform(const std::string& name)
135 {
136 GALAXY_LOG(GALAXY_ERROR, "Tried to set uniform without value: '{0}'.", name);
137 }
138
139 template<>
140 inline void Shader::set_uniform<bool>(const std::string& name, const bool& a)
141 {
142 ray::SetShaderValue(m_shader, get_uniform_location(name), &a, ray::SHADER_UNIFORM_INT);
143 }
144
145 template<>
146 inline void Shader::set_uniform<int>(const std::string& name, const int& a)
147 {
148 ray::SetShaderValue(m_shader, get_uniform_location(name), &a, ray::SHADER_UNIFORM_INT);
149 }
150
151 template<>
152 inline void Shader::set_uniform<unsigned int>(const std::string& name, const unsigned int& a)
153 {
154 ray::SetShaderValue(m_shader, get_uniform_location(name), &a, ray::SHADER_UNIFORM_UINT);
155 }
156
157 template<>
158 inline void Shader::set_uniform<float>(const std::string& name, const float& a)
159 {
160 ray::SetShaderValue(m_shader, get_uniform_location(name), &a, ray::SHADER_UNIFORM_FLOAT);
161 }
162
163 template<>
164 inline void Shader::set_uniform<ray::Vector2>(const std::string& name, const ray::Vector2& a)
165 {
166 ray::SetShaderValue(m_shader, get_uniform_location(name), &a, ray::SHADER_UNIFORM_VEC2);
167 }
168
169 template<>
170 inline void Shader::set_uniform<ray::Vector3>(const std::string& name, const ray::Vector3& a)
171 {
172 ray::SetShaderValue(m_shader, get_uniform_location(name), &a, ray::SHADER_UNIFORM_VEC3);
173 }
174
175 template<>
176 inline void Shader::set_uniform<ray::Vector4>(const std::string& name, const ray::Vector4& a)
177 {
178 ray::SetShaderValue(m_shader, get_uniform_location(name), &a, ray::SHADER_UNIFORM_VEC4);
179 }
180
181 template<>
182 inline void Shader::set_uniform<ray::Matrix>(const std::string& name, const ray::Matrix& a)
183 {
184 ray::SetShaderValueMatrix(m_shader, get_uniform_location(name), a);
185 }
186
187 template<>
188 inline void Shader::set_uniform<ray::Texture2D>(const std::string& name, const ray::Texture2D& a)
189 {
190 ray::SetShaderValueTexture(m_shader, get_uniform_location(name), a);
191 }
192} // namespace galaxy
193
194#endif
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:29
#define GALAXY_ERROR
Definition Log.hpp:25
Shader() noexcept
Constructor.
Definition Shader.cpp:16
Shader(const Shader &)=delete
Copy constructor.
ankerl::unordered_dense::map< std::string, int > m_cache
Cache of uniforms for better performance.
Definition Shader.hpp:130
int get_uniform_location(const std::string &name)
Retrieves the location of a shader uniform.
Definition Shader.cpp:160
~Shader() noexcept
Destructor.
Definition Shader.cpp:55
Shader & operator=(const Shader &)=delete
Copy assignment operator.
Shader & operator=(Shader &&) noexcept
Move assignment operator.
Definition Shader.cpp:35
void set_uniform(const std::string &name, const Uniforms &... args)
Specialized variadic template for setting shader uniforms.
ray::Shader m_shader
Handle.
Definition Shader.hpp:125
bool parse(const std::string &src) noexcept
Loads a combined raw shader.
Definition Shader.cpp:81
bool load(const std::string &file) noexcept
Loads a combined shader.
Definition Shader.cpp:63
Application.hpp galaxy.