Unravel Engine C++ Reference
Loading...
Searching...
No Matches
gpu_program.cpp
Go to the documentation of this file.
1#include "gpu_program.h"
2#include <algorithm>
3
4namespace unravel
5{
7{
8 attach_shader(std::move(compute_shader));
9 populate();
10}
11
13{
14 attach_shader(std::move(vertex_shader));
15 attach_shader(std::move(fragment_shader));
16 populate();
17}
18
20{
21 if(!shader)
22 {
23 shaders_.push_back(shader);
24 return;
25 }
26
27 shaders_.emplace_back(std::move(shader));
28}
29
31{
32 bool all_valid = std::all_of(std::begin(shaders_),
33 std::end(shaders_),
34 [](auto& shader)
35 {
36 return shader && shader.get()->is_valid();
37 });
38
39 if(all_valid)
40 {
41 if(program_)
42 {
43 program_.reset();
44 }
45 if(shaders_.size() == 1)
46 {
47 const auto& compute_shader = shaders_[0];
48 program_ = std::make_shared<gfx::program>(*compute_shader.get());
49 }
50 else if(shaders_.size() == 2)
51 {
52 const auto& vertex_shader = shaders_[0];
53 const auto& fragment_shader = shaders_[1];
54 program_ = std::make_shared<gfx::program>(*vertex_shader.get(), *fragment_shader.get());
55 }
56
57 for(std::size_t i = 0; i < shaders_.size(); ++i)
58 {
59 const auto& shader = shaders_[i];
60 shaders_cached_[i] = shader.get();
61 }
62 }
63}
64
65void gpu_program::set_texture(uint8_t stage,
66 const hpp::string_view& sampler,
67 const gfx::frame_buffer* fbo,
68 uint8_t attachment,
69 uint32_t flags)
70{
71 program_->set_texture(stage, sampler, fbo, attachment, flags);
72}
73
74void gpu_program::set_texture(uint8_t stage,
75 const hpp::string_view& sampler,
76 const gfx::texture* texture,
77 uint32_t flags)
78{
79 program_->set_texture(stage, sampler, texture, flags);
80}
81
82void gpu_program::set_uniform(const hpp::string_view& name, const void* value, uint16_t num)
83{
84 program_->set_uniform(name, value, num);
85}
86
87void gpu_program::set_uniform(const hpp::string_view& name, const math::vec4& value, uint16_t num)
88{
89 set_uniform(name, math::value_ptr(value), num);
90}
91
92void gpu_program::set_uniform(const hpp::string_view& name, const math::vec3& value, uint16_t num)
93{
94 set_uniform(name, math::vec4(value, 0.0f), num);
95}
96
97void gpu_program::set_uniform(const hpp::string_view& name, const math::vec2& value, uint16_t num)
98{
99 set_uniform(name, math::vec4(value, 0.0f, 0.0f), num);
100}
101
103{
104 return program_->get_uniform(name);
105}
106
107auto gpu_program::native_handle() const -> gfx::program::handle_type_t
108{
109 return program_->native_handle();
110}
111
112auto gpu_program::get_shaders() const -> const std::vector<asset_handle<gfx::shader>>&
113{
114 return shaders_;
115}
116
117auto gpu_program::is_valid() const -> bool
118{
119 return program_ && program_->is_valid();
120}
121
122auto gpu_program::begin() -> bool
123{
124 bool repopulate = false;
125 for(std::size_t i = 0; i < shaders_.size(); ++i)
126 {
127 auto shader_ptr = shaders_[i];
128 if(!shader_ptr)
129 {
130 continue;
131 }
132
133 if(shaders_cached_[i] != shader_ptr.get())
134 {
135 repopulate = true;
136 break;
137 }
138 }
139
140 if(repopulate)
141 {
142 populate();
143 repopulated_ = true;
144 }
145 else
146 {
147 repopulated_ = false;
148 }
149
150 return is_valid();
151}
152
154{
155 return repopulated_;
156}
157
159{
160}
161
162
163} // namespace unravel
164
165namespace gfx
166{
167
168void set_world_transform(const std::vector<math::transform::mat4_t>& matrices)
169{
170 if(matrices.empty())
171 {
172 return;
173 }
174
175 gfx::set_world_transform(matrices.data(), static_cast<std::uint16_t>(matrices.size()));
176}
177
178void set_world_transform(const std::vector<math::transform>& matrices)
179{
180 if(matrices.empty())
181 {
182 return;
183 }
184
185 using mat_type = math::transform::mat4_t;
186 std::vector<mat_type> mats;
187 mats.reserve(matrices.size());
188 for(const auto& m : matrices)
189 {
190 mats.emplace_back(m.get_matrix());
191 }
192
194}
195
197{
198 auto mat4 = (const void*)math::value_ptr(matrix);
200}
201
203{
205}
206
207void set_transform(const std::vector<math::transform::mat4_t>& matrices)
208{
209 if(matrices.empty())
210 {
211 return;
212 }
213
214 gfx::set_transform(matrices.data(), static_cast<std::uint16_t>(matrices.size()));
215}
216
217void set_transform(const std::vector<math::transform>& matrices)
218{
219 if(matrices.empty())
220 {
221 return;
222 }
223
224 using mat_type = math::transform::mat4_t;
225 std::vector<mat_type> mats;
226 mats.reserve(matrices.size());
227 for(const auto& m : matrices)
228 {
229 mats.emplace_back(m.get_matrix());
230 }
231
232 set_transform(mats);
233}
234
236{
237 auto mat4 = (const void*)math::value_ptr(matrix);
238 gfx::set_transform(mat4);
239}
240
242{
243 set_transform(matrix.get_matrix());
244}
245
247 std::uint8_t stage,
249 uint8_t attachment,
250 std::uint32_t flags)
251{
252 set_texture(uniform, stage, handle.get(), attachment, flags);
253}
254
256 std::uint8_t stage,
258 std::uint32_t flags)
259{
260 set_texture(uniform, stage, texture.get(), flags);
261}
262
264 std::uint8_t stage,
266 uint8_t attachment,
267 std::uint32_t flags)
268{
269 if(!uniform)
270 {
271 return;
272 }
273
274 uniform->set_texture(stage, handle, attachment, flags);
275}
276
278 std::uint8_t stage,
279 const gfx::texture* texture,
280 std::uint32_t flags)
281{
282 if(!uniform)
283 {
284 return;
285 }
286
287 uniform->set_texture(stage, texture, flags);
288}
289
291 std::uint8_t stage,
293 std::uint32_t flags)
294{
295 set_texture(uniform, stage, texture.get(), flags);
296}
297
298void set_uniform(const gfx::program::uniform_ptr& uniform, const void* value, std::uint16_t num)
299{
300 if(!uniform)
301 {
302 return;
303 }
304
305 uniform->set_uniform(value, num);
306}
307void set_uniform(const gfx::program::uniform_ptr& uniform, const math::mat4& value, std::uint16_t num)
308{
309 set_uniform(uniform, math::value_ptr(value), num);
310}
311void set_uniform(const gfx::program::uniform_ptr& uniform, const math::vec4& value, std::uint16_t num)
312{
313 set_uniform(uniform, math::value_ptr(value), num);
314}
315void set_uniform(const gfx::program::uniform_ptr& uniform, const math::vec3& value, std::uint16_t num)
316{
317 set_uniform(uniform, math::vec4(value, 0.0f), num);
318}
319void set_uniform(const gfx::program::uniform_ptr& uniform, const math::vec2& value, std::uint16_t num)
320{
321 set_uniform(uniform, math::vec4(value, 0.0f, 0.0f), num);
322}
323} // namespace gfx
General purpose transformation class designed to maintain each component of the transformation separa...
Definition transform.hpp:27
auto get_matrix() const noexcept -> const mat4_t &
Get the transform matrix.
mat< 4, 4, T, Q > mat4_t
Definition transform.hpp:30
gpu_program()=default
Default constructor.
void attach_shader(asset_handle< gfx::shader > shader)
Attaches a shader to the GPU program.
auto was_repopulated() const -> bool
True when begin() just rebuilt the program after a shader asset reload.
auto begin() -> bool
Begins usage of the program. Checks validity of attached shaders and recreates the internal program i...
void populate()
Populates the GPU program.
void set_texture(std::uint8_t stage, const hpp::string_view &sampler, const gfx::frame_buffer *handle, uint8_t attachment=0, std::uint32_t flags=std::numeric_limits< std::uint32_t >::max())
Sets the texture for a specific stage using a frame buffer.
void set_uniform(const hpp::string_view &name, const void *value, std::uint16_t num=1)
Sets a uniform value in the shader program.
auto is_valid() const -> bool
Checks if the GPU program is valid.
void end()
Indicates the end of working with a program.
auto native_handle() const -> gfx::program::handle_type_t
Retrieves the native handle of the internal shader program.
auto get_uniform(const hpp::string_view &name) -> gfx::program::uniform_ptr
Retrieves a uniform from the shader program.
auto get_shaders() const -> const std::vector< asset_handle< gfx::shader > > &
Retrieves the shader assets that created the shader program.
std::string name
Definition hub.cpp:33
bgfx::Attachment attachment
Definition graphics.h:28
void set_world_transform(const void *_mtx, uint16_t _num)
uint32_t set_transform(const void *_mtx, uint16_t _num)
Definition graphics.cpp:962
void set_uniform(uniform_handle _handle, const void *_value, uint16_t _num)
Definition graphics.cpp:977
void set_texture(uint8_t _stage, uniform_handle _sampler, texture_handle _handle, uint32_t _flags)
Hash specialization for batch_key to enable use in std::unordered_map.
Thread-safe handle to an asset.
std::shared_ptr< uniform > uniform_ptr
Definition program.h:19
void set_texture(uint8_t _stage, const gfx::frame_buffer *_handle, uint8_t _attachment=0, uint32_t _flags=std::numeric_limits< uint32_t >::max())
Definition uniform.cpp:99
void set_uniform(const void *_value, uint16_t _num=1)
Definition uniform.cpp:124
gfx::uniform_handle handle
Definition uniform.cpp:9