Unravel Engine C++ Reference
Loading...
Searching...
No Matches
gpu_program.h
Go to the documentation of this file.
1#pragma once
2#include <engine/engine_export.h>
3
5
6#include <graphics/program.h>
7#include <graphics/shader.h>
8#include <math/math.h>
9#include <unordered_set>
10
11namespace unravel
12{
17{
18public:
19 using ptr = std::shared_ptr<gpu_program>;
20 using wptr = std::weak_ptr<gpu_program>;
21 using uptr = std::unique_ptr<gpu_program>;
22
26 gpu_program() = default;
27
34
42
49 auto begin() -> bool;
50
54 void end();
55
65 void set_texture(std::uint8_t stage,
66 const hpp::string_view& sampler,
68 uint8_t attachment = 0,
69 std::uint32_t flags = std::numeric_limits<std::uint32_t>::max());
70
79 void set_texture(std::uint8_t stage,
80 const hpp::string_view& sampler,
81 const gfx::texture* texture,
82 std::uint32_t flags = std::numeric_limits<std::uint32_t>::max());
83
91 void set_uniform(const hpp::string_view& name, const void* value, std::uint16_t num = 1);
92
100 void set_uniform(const hpp::string_view& name, const math::vec4& value, std::uint16_t num = 1);
101
109 void set_uniform(const hpp::string_view& name, const math::vec3& value, std::uint16_t num = 1);
110
118 void set_uniform(const hpp::string_view& name, const math::vec2& value, std::uint16_t num = 1);
119
126 auto get_uniform(const hpp::string_view& name) -> gfx::program::uniform_ptr;
127
133 auto native_handle() const -> gfx::program::handle_type_t;
134
140 auto get_shaders() const -> const std::vector<asset_handle<gfx::shader>>&;
141
147 auto is_valid() const -> bool;
148
152 auto was_repopulated() const -> bool;
153
157 void populate();
158
164 void attach_shader(asset_handle<gfx::shader> shader);
165
166private:
167 std::vector<asset_handle<gfx::shader>> shaders_;
168 std::array<gfx::shader::ptr, 2> shaders_cached_{nullptr, nullptr};
169 std::shared_ptr<gfx::program> program_;
170 bool repopulated_ = false;
171};
172
177{
186 void cache_uniform(gpu_program* program, gfx::program::uniform_ptr& uniform, const hpp::string_view& name, gfx::uniform_type type, uint16_t num = 1)
187 {
188 program->begin();
189 uniform = program->get_uniform(name);
190
191 if(uniform == nullptr)
192 {
193 uniform = std::make_shared<gfx::uniform>(std::string(name), type, num);
194 }
195
196 program->end();
197 }
198};
199
200} // namespace unravel
201
202namespace gfx
203{
204
210void set_world_transform(const std::vector<math::transform::mat4_t>& matrices);
211void set_transform(const std::vector<math::transform::mat4_t>& matrices);
212
218void set_world_transform(const std::vector<math::transform>& matrices);
219void set_transform(const std::vector<math::transform>& matrices);
220
227void set_transform(const math::transform::mat4_t& matrix);
228
234void set_world_transform(const math::transform& matrix);
235void set_transform(const math::transform& matrix);
236
246void set_texture(const gfx::program::uniform_ptr& uniform,
247 std::uint8_t stage,
249 uint8_t attachment = 0,
250 std::uint32_t flags = std::numeric_limits<std::uint32_t>::max());
251
260void set_texture(const gfx::program::uniform_ptr& uniform,
261 std::uint8_t stage,
262 const gfx::texture::ptr& texture,
263 std::uint32_t flags = std::numeric_limits<std::uint32_t>::max());
264
274void set_texture(const gfx::program::uniform_ptr& uniform,
275 std::uint8_t stage,
277 uint8_t attachment = 0,
278 std::uint32_t flags = std::numeric_limits<std::uint32_t>::max());
279
288void set_texture(const gfx::program::uniform_ptr& uniform,
289 std::uint8_t stage,
290 const gfx::texture* texture,
291 std::uint32_t flags = std::numeric_limits<std::uint32_t>::max());
292
301void set_texture(const gfx::program::uniform_ptr& uniform,
302 std::uint8_t stage,
303 const asset_handle<gfx::texture>& texture,
304 std::uint32_t flags = std::numeric_limits<std::uint32_t>::max());
305
313void set_uniform(const gfx::program::uniform_ptr& uniform, const void* value, std::uint16_t num = 1);
314
322void set_uniform(const gfx::program::uniform_ptr& uniform, const math::mat4& value, std::uint16_t num = 1);
323
331void set_uniform(const gfx::program::uniform_ptr& uniform, const math::vec4& value, std::uint16_t num = 1);
332
340void set_uniform(const gfx::program::uniform_ptr& uniform, const math::vec3& value, std::uint16_t num = 1);
341
349void set_uniform(const gfx::program::uniform_ptr& uniform, const math::vec2& value, std::uint16_t num = 1);
350
351} // namespace gfx
General purpose transformation class designed to maintain each component of the transformation separa...
Definition transform.hpp:27
mat< 4, 4, T, Q > mat4_t
Definition transform.hpp:30
Class representing a GPU program.
Definition gpu_program.h:17
void set_uniform(const hpp::string_view &name, const math::vec2 &value, std::uint16_t num=1)
Sets a uniform value in the shader program.
gpu_program()=default
Default constructor.
std::shared_ptr< gpu_program > ptr
Definition gpu_program.h:19
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.
std::weak_ptr< gpu_program > wptr
Definition gpu_program.h:20
auto begin() -> bool
Begins usage of the program. Checks validity of attached shaders and recreates the internal program i...
void set_texture(std::uint8_t stage, const hpp::string_view &sampler, const gfx::texture *texture, std::uint32_t flags=std::numeric_limits< std::uint32_t >::max())
Sets the texture for a specific stage using a texture.
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.
std::unique_ptr< gpu_program > uptr
Definition gpu_program.h:21
void set_uniform(const hpp::string_view &name, const math::vec4 &value, std::uint16_t num=1)
Sets a uniform value in the shader program.
void set_uniform(const hpp::string_view &name, const math::vec3 &value, std::uint16_t num=1)
Sets a uniform value in the shader 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
texture_job_type type
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
bgfx::UniformType::Enum uniform_type
Definition graphics.h:25
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
Structure for caching uniforms.
void cache_uniform(gpu_program *program, gfx::program::uniform_ptr &uniform, const hpp::string_view &name, gfx::uniform_type type, uint16_t num=1)
Caches a uniform in the GPU program.
gfx::uniform_handle handle
Definition uniform.cpp:9