Euphoria
shader.h
Go to the documentation of this file.
1 #pragma once
2 
3 
4 #include "base/vec2.h"
5 #include "base/vec3.h"
6 #include "base/vec4.h"
7 #include "base/rgb.h"
8 #include "base/mat4.h"
9 #include "base/rect.h"
10 #include "io/vfs_path.h"
11 
12 #include "render/gltypes.h"
13 #include "render/shaderattribute.h"
14 #include "render/shaderuniform.h"
15 
16 
17 namespace eu::io
18 {
19  struct FileSystem;
20 }
21 
22 
23 namespace eu::render
24 {
25  struct Texture2;
26 
27 
28  struct ShaderId
29  {
30  public:
31  ShaderId();
32  ~ShaderId();
33 
34  ShaderId(const ShaderId& other) = delete;
35  void operator=(const ShaderId&) = delete;
36  ShaderId(ShaderId&& other) = delete;
37  void operator=(ShaderId&&) = delete;
38 
39  [[nodiscard]] bool
40  is_currently_bound() const;
41 
42  [[nodiscard]] gl::Uint
43  get_id() const;
44 
45  private:
46  gl::Uint id;
47  };
48 
49 
50  struct ShaderProgram : public ShaderId
51  {
52  public:
53  ShaderProgram();
54  ~ShaderProgram() = default;
55 
56  ShaderProgram(const ShaderProgram& other) = delete;
57  void operator=(const ShaderProgram&) = delete;
58  ShaderProgram(ShaderProgram&& other) = delete;
59  void operator=(ShaderProgram&&) = delete;
60 
61  void add_attribute(const ShaderAttribute& attribute);
62 
63  bool load(io::FileSystem* fs, const io::FilePath& file_path);
64 
65  bool compile
66  (
67  const gl::Char* vertex_source,
68  const gl::Char* fragment_source,
69  const gl::Char* geometry_source = nullptr
70  );
71 
72  // uniform = shader global
73  ShaderUniform get_uniform(const std::string& name);
74 
75  void set_uniform(const ShaderUniform& attribute, gl::Int val);
76  void set_uniform(const ShaderUniform& attribute, float val);
77  void set_uniform(const ShaderUniform& attribute, const Rgb& val);
78  void set_uniform(const ShaderUniform& attribute, const Rgba& val);
79  void set_uniform(const ShaderUniform& attribute, const vec3f& val);
80  void set_uniform(const ShaderUniform& attribute, const vec4f& val);
81  void set_uniform(const ShaderUniform& attribute, const mat3f& val);
82  void set_uniform(const ShaderUniform& attribute, const mat4f& val);
83  void set_uniform(const ShaderUniform& attribute, const Rectf& val);
84 
85  // debug
86  [[nodiscard]] static const ShaderProgram*
88 
89  [[nodiscard]] const std::vector<ShaderAttribute>&
90  get_attributes() const;
91 
92  [[nodiscard]] const io::FilePath&
93  get_name() const;
94 
95  private:
96  [[nodiscard]] bool
97  has_bound_attribute(const ShaderAttribute& attribute) const;
98 
99  [[nodiscard]] bool
100  has_bound_uniform(const ShaderUniform& uniform) const;
101 
102  std::vector<ShaderAttribute> bound_attributes;
103  std::vector<ShaderUniform> bound_uniforms;
104  io::FilePath shader_name;
105  };
106 
107  void
108  use(const render::ShaderProgram* shader);
109 
110  void
112  (
113  Texture2* texture,
114  ShaderProgram* shader,
115  const ShaderUniform& attribute,
116  gl::Int index
117  );
118 
119 }
Definition: enum.h:8
char Char
Definition: gltypes.h:8
unsigned int Uint
Definition: gltypes.h:6
void use(const ShaderProgram *shader)
Definition: shader.cc:114
void bind_texture_to_shader(Texture2 *texture, ShaderProgram *shader, const ShaderUniform &attribute, gl::Int index)
Definition: shader.cc:488
Definition: rect.h:27
Definition: rgb.h:62
Definition: rgb.h:143
Definition: mat3.h:12
Definition: mat4.h:14
Represents a shader attribute like vertex, normal or uv coord.
bool is_currently_bound() const
Definition: shader.cc:107
void operator=(const ShaderId &)=delete
ShaderId(const ShaderId &other)=delete
ShaderId(ShaderId &&other)=delete
void operator=(ShaderId &&)=delete
gl::Uint get_id() const
Definition: shader.cc:100
ShaderProgram(const ShaderProgram &other)=delete
ShaderProgram(ShaderProgram &&other)=delete
void set_uniform(const ShaderUniform &attribute, gl::Int val)
Definition: shader.cc:292
bool load(io::FileSystem *fs, const io::FilePath &file_path)
Definition: shader.cc:398
bool compile(const gl::Char *vertex_source, const gl::Char *fragment_source, const gl::Char *geometry_source=nullptr)
Definition: shader.cc:194
void operator=(ShaderProgram &&)=delete
const io::FilePath & get_name() const
Definition: shader.cc:459
ShaderUniform get_uniform(const std::string &name)
Definition: shader.cc:262
void add_attribute(const ShaderAttribute &attribute)
Definition: shader.cc:184
static const ShaderProgram * get_current_bound_for_debug()
Definition: shader.cc:129
const std::vector< ShaderAttribute > & get_attributes() const
Definition: shader.cc:452
void operator=(const ShaderProgram &)=delete
Definition: vec3.h:48
Definition: vec4.h:14