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.cpp
Go to the documentation of this file.
1
7
8#include <entt/locator/locator.hpp>
9
11
12#include "Shader.hpp"
13
14namespace galaxy
15{
16 Shader::Shader() noexcept
17 {
18 }
19
20 Shader::Shader(Shader&& s) noexcept
21 {
22 if (this->m_shader.id != 0)
23 {
24 ray::UnloadShader(this->m_shader);
25 this->m_cache.clear();
26 }
27
28 this->m_shader.id = s.m_shader.id;
29 this->m_shader.locs = s.m_shader.locs;
30 this->m_cache = std::move(s.m_cache);
31
32 s.m_shader = {0};
33 }
34
36 {
37 if (this != &s)
38 {
39 if (this->m_shader.id != 0)
40 {
41 ray::UnloadShader(this->m_shader);
42 this->m_cache.clear();
43 }
44
45 this->m_shader.id = s.m_shader.id;
46 this->m_shader.locs = s.m_shader.locs;
47 this->m_cache = std::move(s.m_cache);
48
49 s.m_shader = {0};
50 }
51
52 return *this;
53 }
54
55 Shader::~Shader() noexcept
56 {
57 if (m_shader.id != 0)
58 {
59 ray::UnloadShader(m_shader);
60 }
61 }
62
63 bool Shader::load(const std::string& file) noexcept
64 {
65 auto& fs = entt::locator<VirtualFileSystem>::value();
66
67 auto data = fs.read(file);
68 return parse(data);
69 }
70
71 bool Shader::load(const std::string& vertex, const std::string& frag) noexcept
72 {
73 auto& fs = entt::locator<VirtualFileSystem>::value();
74
75 auto v = fs.read(vertex);
76 auto f = fs.read(frag);
77
78 return parse(v, f);
79 }
80
81 bool Shader::parse(const std::string& src) noexcept
82 {
83 auto result = true;
84
85 if (!src.empty())
86 {
87 std::string vertex, fragment;
88
89 const auto token = "#type";
90 const auto len = std::strlen(token);
91
92 auto pos = src.find(token, 0);
93 while (pos != std::string::npos)
94 {
95 const auto eol = src.find_first_of("\r\n", pos);
96 const auto begin = pos + len + 1;
97
98 auto type = src.substr(begin, eol - begin);
99
100 if (type == "vertex" || type == "fragment")
101 {
102 const auto next_line = src.find_first_not_of("\r\n", eol);
103 pos = src.find(token, next_line);
104
105 if (type == "vertex")
106 {
107 vertex = (pos == std::string::npos) ? src.substr(next_line) : src.substr(next_line, pos - next_line);
108 }
109 else
110 {
111 fragment = (pos == std::string::npos) ? src.substr(next_line) : src.substr(next_line, pos - next_line);
112 }
113 }
114 else
115 {
116 GALAXY_LOG(GALAXY_ERROR, "Failed to parse shader type. Must be 'vertex' or 'fragment'.");
117 result = false;
118 }
119 }
120
121 if (result)
122 {
123 return parse(vertex, fragment);
124 }
125 }
126 else
127 {
128 GALAXY_LOG(GALAXY_ERROR, "Empty source code provided to shader.");
129 result = false;
130 }
131
132 return result;
133 }
134
135 bool Shader::parse(const std::string& vertex, const std::string& frag) noexcept
136 {
137 auto result = true;
138
139 if (vertex.empty())
140 {
141 GALAXY_LOG(GALAXY_ERROR, "Shader was passed an empty vertex shader.");
142 result = false;
143 }
144
145 if (frag.empty())
146 {
147 GALAXY_LOG(GALAXY_ERROR, "Shader was passed an empty fragment shader.");
148 result = false;
149 }
150
151 if (result)
152 {
153 m_shader = ray::LoadShaderFromMemory(vertex.c_str(), frag.c_str());
154 return ray::IsShaderValid(m_shader);
155 }
156
157 return result;
158 }
159
160 int Shader::get_uniform_location(const std::string& name)
161 {
162 if (!m_cache.contains(name))
163 {
164 m_cache[name] = ray::GetShaderLocation(m_shader, name.c_str());
165 }
166
167 return m_cache[name];
168 }
169} // namespace galaxy
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:29
#define GALAXY_ERROR
Definition Log.hpp:25
Shader() noexcept
Constructor.
Definition Shader.cpp:16
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=(Shader &&) noexcept
Move assignment operator.
Definition Shader.cpp:35
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.