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 }
144
145 return is_valid();
146}
147
149{
150}
151
152
153} // namespace unravel
154
155namespace gfx
156{
157
158void set_world_transform(const std::vector<math::transform::mat4_t>& matrices)
159{
160 if(matrices.empty())
161 {
162 return;
163 }
164
165 gfx::set_world_transform(matrices.data(), static_cast<std::uint16_t>(matrices.size()));
166}
167
168void set_world_transform(const std::vector<math::transform>& matrices)
169{
170 if(matrices.empty())
171 {
172 return;
173 }
174
175 using mat_type = math::transform::mat4_t;
176 std::vector<mat_type> mats;
177 mats.reserve(matrices.size());
178 for(const auto& m : matrices)
179 {
180 mats.emplace_back(m.get_matrix());
181 }
182
184}
185
187{
188 auto mat4 = (const void*)math::value_ptr(matrix);
190}
191
193{
195}
196
197void set_transform(const std::vector<math::transform::mat4_t>& matrices)
198{
199 if(matrices.empty())
200 {
201 return;
202 }
203
204 gfx::set_transform(matrices.data(), static_cast<std::uint16_t>(matrices.size()));
205}
206
207void set_transform(const std::vector<math::transform>& matrices)
208{
209 if(matrices.empty())
210 {
211 return;
212 }
213
214 using mat_type = math::transform::mat4_t;
215 std::vector<mat_type> mats;
216 mats.reserve(matrices.size());
217 for(const auto& m : matrices)
218 {
219 mats.emplace_back(m.get_matrix());
220 }
221
222 set_transform(mats);
223}
224
226{
227 auto mat4 = (const void*)math::value_ptr(matrix);
228 gfx::set_transform(mat4);
229}
230
232{
233 set_transform(matrix.get_matrix());
234}
235
237 std::uint8_t stage,
239 uint8_t attachment,
240 std::uint32_t flags)
241{
242 set_texture(uniform, stage, handle.get(), attachment, flags);
243}
244
246 std::uint8_t stage,
248 std::uint32_t flags)
249{
250 set_texture(uniform, stage, texture.get(), flags);
251}
252
254 std::uint8_t stage,
256 uint8_t attachment,
257 std::uint32_t flags)
258{
259 if(!uniform)
260 {
261 return;
262 }
263
264 uniform->set_texture(stage, handle, attachment, flags);
265}
266
268 std::uint8_t stage,
269 const gfx::texture* texture,
270 std::uint32_t flags)
271{
272 if(!uniform)
273 {
274 return;
275 }
276
277 uniform->set_texture(stage, texture, flags);
278}
279
281 std::uint8_t stage,
283 std::uint32_t flags)
284{
285 set_texture(uniform, stage, texture.get(), flags);
286}
287
288void set_uniform(const gfx::program::uniform_ptr& uniform, const void* value, std::uint16_t num)
289{
290 if(!uniform)
291 {
292 return;
293 }
294
295 uniform->set_uniform(value, num);
296}
297void set_uniform(const gfx::program::uniform_ptr& uniform, const math::mat4& value, std::uint16_t num)
298{
299 set_uniform(uniform, math::value_ptr(value), num);
300}
301void set_uniform(const gfx::program::uniform_ptr& uniform, const math::vec4& value, std::uint16_t num)
302{
303 set_uniform(uniform, math::value_ptr(value), num);
304}
305void set_uniform(const gfx::program::uniform_ptr& uniform, const math::vec3& value, std::uint16_t num)
306{
307 set_uniform(uniform, math::vec4(value, 0.0f), num);
308}
309void set_uniform(const gfx::program::uniform_ptr& uniform, const math::vec2& value, std::uint16_t num)
310{
311 set_uniform(uniform, math::vec4(value, 0.0f, 0.0f), num);
312}
313} // 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 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:27
bgfx::Attachment attachment
Definition graphics.h:27
void set_world_transform(const void *_mtx, uint16_t _num)
uint32_t set_transform(const void *_mtx, uint16_t _num)
Definition graphics.cpp:788
void set_uniform(uniform_handle _handle, const void *_value, uint16_t _num)
Definition graphics.cpp:803
void set_texture(uint8_t _stage, uniform_handle _sampler, texture_handle _handle, uint32_t _flags)
Definition graphics.cpp:889
Represents a handle to an asset, providing access and management functions.
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:91
void set_uniform(const void *_value, uint16_t _num=1)
Definition uniform.cpp:116
gfx::uniform_handle handle
Definition uniform.cpp:9