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 void populate();
153
159 void attach_shader(asset_handle<gfx::shader> shader);
160
161private:
162 std::vector<asset_handle<gfx::shader>> shaders_;
163 std::array<gfx::shader::ptr, 2> shaders_cached_{nullptr, nullptr};
164 std::shared_ptr<gfx::program> program_;
165};
166
171{
180 void cache_uniform(gpu_program* program, gfx::program::uniform_ptr& uniform, const hpp::string_view& name, gfx::uniform_type type, uint16_t num = 1)
181 {
182 program->begin();
183 uniform = program->get_uniform(name);
184
185 if(uniform == nullptr)
186 {
187 uniform = std::make_shared<gfx::uniform>(std::string(name), type, num);
188 }
189
190 program->end();
191 }
192};
193
194} // namespace unravel
195
196namespace gfx
197{
198
204void set_world_transform(const std::vector<math::transform::mat4_t>& matrices);
205void set_transform(const std::vector<math::transform::mat4_t>& matrices);
206
212void set_world_transform(const std::vector<math::transform>& matrices);
213void set_transform(const std::vector<math::transform>& matrices);
214
221void set_transform(const math::transform::mat4_t& matrix);
222
228void set_world_transform(const math::transform& matrix);
229void set_transform(const math::transform& matrix);
230
240void set_texture(const gfx::program::uniform_ptr& uniform,
241 std::uint8_t stage,
243 uint8_t attachment = 0,
244 std::uint32_t flags = std::numeric_limits<std::uint32_t>::max());
245
254void set_texture(const gfx::program::uniform_ptr& uniform,
255 std::uint8_t stage,
256 const gfx::texture::ptr& texture,
257 std::uint32_t flags = std::numeric_limits<std::uint32_t>::max());
258
268void set_texture(const gfx::program::uniform_ptr& uniform,
269 std::uint8_t stage,
271 uint8_t attachment = 0,
272 std::uint32_t flags = std::numeric_limits<std::uint32_t>::max());
273
282void set_texture(const gfx::program::uniform_ptr& uniform,
283 std::uint8_t stage,
284 const gfx::texture* texture,
285 std::uint32_t flags = std::numeric_limits<std::uint32_t>::max());
286
295void set_texture(const gfx::program::uniform_ptr& uniform,
296 std::uint8_t stage,
297 const asset_handle<gfx::texture>& texture,
298 std::uint32_t flags = std::numeric_limits<std::uint32_t>::max());
299
307void set_uniform(const gfx::program::uniform_ptr& uniform, const void* value, std::uint16_t num = 1);
308
316void set_uniform(const gfx::program::uniform_ptr& uniform, const math::mat4& value, std::uint16_t num = 1);
317
325void set_uniform(const gfx::program::uniform_ptr& uniform, const math::vec4& value, std::uint16_t num = 1);
326
334void set_uniform(const gfx::program::uniform_ptr& uniform, const math::vec3& value, std::uint16_t num = 1);
335
343void set_uniform(const gfx::program::uniform_ptr& uniform, const math::vec2& value, std::uint16_t num = 1);
344
345} // namespace gfx
manifold_type type
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.
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: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
bgfx::UniformType::Enum uniform_type
Definition graphics.h:25
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
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