Unravel Engine C++ Reference
Loading...
Searching...
No Matches
program.cpp
Go to the documentation of this file.
1#include "program.h"
2#include "frame_buffer.h"
3#include "shader.h"
4#include "texture.h"
5#include "uniform.h"
6
7namespace gfx
8{
9program::program(const shader& compute_shader)
10{
11 if(compute_shader.is_valid())
12 {
13 handle_ = create_program(compute_shader.native_handle());
14
15 for(auto& uniform : compute_shader.uniforms)
16 {
18 }
19 }
20
21 textures_uniforms.fill(nullptr);
22
23}
24
25program::program(const shader& vertex_shader, const shader& fragment_shader)
26{
27 if(vertex_shader.is_valid() && fragment_shader.is_valid())
28 {
29 handle_ = create_program(vertex_shader.native_handle(), fragment_shader.native_handle());
30
31 for(auto& uniform : vertex_shader.uniforms)
32 {
34 }
35
36 for(auto& uniform : fragment_shader.uniforms)
37 {
39 }
40 }
41
42 textures_uniforms.fill(nullptr);
43
44}
45
46void program::set_texture(uint8_t _stage,
47 const hpp::string_view& _sampler,
48 const gfx::frame_buffer* frameBuffer,
49 uint8_t _attachment /*= 0 */,
50 uint32_t _flags /*= std::numeric_limits<uint32_t>::max()*/)
51{
52 if(frameBuffer == nullptr)
53 {
54 return;
55 }
56
57 auto uniform = get_uniform(_sampler, _stage);
58
59 if(!uniform)
60 {
61 return;
62 }
63
64 gfx::set_texture(_stage, uniform->native_handle(), frameBuffer->get_texture(_attachment)->native_handle(), _flags);
65}
66
67void program::set_texture(uint8_t _stage,
68 const hpp::string_view& _sampler,
69 const gfx::texture* _texture,
70 uint32_t _flags /*= std::numeric_limits<uint32_t>::max()*/)
71{
72 if(_texture == nullptr)
73 {
74 return;
75 }
76
77 auto uniform = get_uniform(_sampler, _stage);
78
79 if(!uniform)
80 {
81 return;
82 }
83
84 gfx::set_texture(_stage, uniform->native_handle(), _texture->native_handle(), _flags);
85}
86
87void program::set_uniform(const hpp::string_view& _name, const void* _value, uint16_t _num)
88{
89 auto uniform = get_uniform(_name);
90
91 if(uniform)
92 {
93 gfx::set_uniform(uniform->native_handle(), _value, _num);
94 }
95}
96
97auto program::get_uniform(const hpp::string_view& _name, uint8_t stage) -> uniform_ptr
98{
99 uniform_ptr uniform = nullptr;
100
101 bool is_texture = stage != uint8_t(-1) && stage < textures_uniforms.size();
102
103 if(is_texture)
104 {
105 uniform = textures_uniforms[stage];
106 }
107
108 if(!uniform)
109 {
110 auto it = uniforms.find(_name);
111
112 if(it != uniforms.end())
113 {
114 uniform = it->second;
115
116 if(is_texture)
117 {
118 textures_uniforms[stage] = uniform;
119 }
120 }
121 }
122
123 return uniform;
124}
125} // namespace gfx
auto is_valid() const -> bool
Definition handle_impl.h:35
auto native_handle() const -> T
Definition handle_impl.h:40
program_handle create_program(shader_handle _vsh, shader_handle _fsh, bool _destroyShaders)
Definition graphics.cpp:472
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
auto get_texture(std::uint32_t index=0) const -> const gfx::texture::ptr &
std::map< std::string, uniform_ptr, std::less<> > uniforms
All uniforms for this program.
Definition program.h:98
std::array< uniform_ptr, 64 > textures_uniforms
Definition program.h:99
void set_texture(uint8_t _stage, const hpp::string_view &_sampler, const gfx::frame_buffer *_handle, uint8_t _attachment=0, uint32_t _flags=std::numeric_limits< uint32_t >::max())
Definition program.cpp:46
std::shared_ptr< uniform > uniform_ptr
Definition program.h:19
auto get_uniform(const hpp::string_view &_name, uint8_t stage=uint8_t(-1)) -> uniform_ptr
Definition program.cpp:97
program()=default
void set_uniform(const hpp::string_view &_name, const void *_value, uint16_t _num=1)
Definition program.cpp:87
std::vector< std::shared_ptr< uniform > > uniforms
Uniforms for this shader.
Definition shader.h:17
uniform_info info
Uniform info.
Definition uniform.h:78