gdk-graphics  1b6d84e044c2c953fd7c9501e628a67e80f4da0d
webgl1es2_shader_program.h
1 // © 2019 Joseph Cameron - All Rights Reserved
2 
3 #ifndef GDK_GFX_WEBGL1ES2_SHADER_PROGRAM_H
4 #define GDK_GFX_WEBGL1ES2_SHADER_PROGRAM_H
5 
6 #include <gdk/graphics_types.h>
7 #include <gdk/opengl.h>
8 #include <gdk/shader_program.h>
9 #include <gdk/webgl1es2_texture.h>
10 #include <jfc/shared_proxy_ptr.h>
11 #include <jfc/unique_handle.h>
12 
13 #include <array>
14 #include <optional>
15 #include <string>
16 #include <unordered_map>
17 
18 namespace gdk
19 {
22  //TODO: consider a "pipeline" abstraction, which specifies all fixed options, then takes a program to specify programmable ones.
23  //TODO: pipline abstraction is required to properly support blend-based transparencies, since frag order matters when blending.
25  {
26  public:
29  {
30  GLint location;
31  GLenum type;
32  //TODO this is not component count. this is number of types (vec2 is 1 not 2)
33  GLint count;
34  };
35 
38  {
39  GLint location;
40  GLenum type;
41  GLint size;
42  };
43 
46  using active_attribute_collection_type = std::unordered_map<std::string, active_attribute_info>;
49  using active_uniform_collection_type = std::unordered_map<std::string, active_uniform_info>;
50 
52  using integer2_uniform_type = std::array<GLint, 2>;
54  using integer3_uniform_type = std::array<GLint, 3>;
56  using integer4_uniform_type = std::array<GLint, 4>;
57 
58  public:
60  std::optional<active_attribute_info> tryGetActiveAttribute(
61  const std::string &aAttributeName) const;
62 
64  bool try_set_uniform(const std::string &aName, const GLfloat aValue) const;
66  bool try_set_uniform(const std::string &aName,
67  const graphics_vector2_type &aValue) const;
69  bool try_set_uniform(const std::string &aName,
70  const graphics_vector3_type &aValue) const;
72  bool try_set_uniform(const std::string &aName,
73  const graphics_vector4_type &aValue) const;
75  bool try_set_uniform(const std::string &aName,
76  const std::vector<GLfloat> &avalue) const;
78  bool try_set_uniform(const std::string &aName,
79  const std::vector<graphics_vector2_type> &avalue) const;
81  bool try_set_uniform(const std::string &aName,
82  const std::vector<graphics_vector3_type> &avalue) const;
84  bool try_set_uniform(const std::string &aName,
85  const std::vector<graphics_vector4_type> &avalue) const;
86 
88  bool try_set_uniform(const std::string &aName, const GLint aValue) const;
90  bool try_set_uniform(const std::string aName, const integer2_uniform_type &aValue) const;
92  bool try_set_uniform(const std::string aName, const integer3_uniform_type &aValue) const;
94  bool try_set_uniform(const std::string aName, const integer4_uniform_type &aValue) const;
95 
97  bool try_set_uniform(const std::string &aName, const std::vector<GLint> &aValue) const;
99  bool try_set_uniform(const std::string &aName, const std::vector<integer2_uniform_type> &aValue) const;
101  bool try_set_uniform(const std::string &aName, const std::vector<integer3_uniform_type> &aValue) const;
103  bool try_set_uniform(const std::string &aName, const std::vector<integer4_uniform_type> &aValue) const;
104 
106  //bool try_set_uniform(const std::string &aName, const bool &aValue) const;
108  //bool try_set_uniform(const std::string &aName, const boolean2_uniform_type &a) const;
110  //bool try_set_uniform(const std::string &aName, const boolean3_uniform_type &a) const;
112  //bool try_set_uniform(const std::string &aName, const boolean4_uniform_type &a) const;
113 
115  //bool try_set_uniform(const std::string &aName, const std::vector<bool> &aValue) const;
117  //bool try_set_uniform(const std::string>&aName, const std::vector<boolean2_uniform_type> &a) const;
119  //bool try_set_uniform(const std::string>&aName, const std::vector<boolean3_uniform_type> &a) const;
121  //bool try_set_uniform(const std::string>&aName, const std::vector<boolean4_uniform_type> &a) const;
122 
123  /*//! assign a mat2x2 uniform from a mat2x2
124  bool try_set_uniform(const std::string &aName, const graphics_mat2x2_type &avalue) const;
126  bool try_set_uniform(const std::string &aName, const std::vector<graphics_mat2x2_type> &avalue) const;
127 
129  bool try_set_uniform(const std::string &aName, const graphics_mat3x3_type &avalue) const;
131  bool try_set_uniform(const std::string &aName, const std::vector<graphics_mat3x3_type> &avalue) const;*/
132 
134  bool try_set_uniform(const std::string &aName, const graphics_mat4x4_type &avalue) const;
136  bool try_set_uniform(const std::string &aName, const std::vector<graphics_mat4x4_type> &avalue) const;
137 
138  //TODO: texture needs to support more than tex2d!
140  bool try_set_uniform(const std::string &aName, const gdk::webgl1es2_texture &aTexture) const;
141 
142  //TODO: texture needs to support more than tex2d!
144  //bool try_set_uniform(const std::string &aName, const gdk::texture &aTexture) const;
145 
148  void useProgram() const;
149 
151  bool operator==(const webgl1es2_shader_program &) const;
153  bool operator!=(const webgl1es2_shader_program &) const;
154 
159 
161  webgl1es2_shader_program(std::string aVertexSource, std::string aFragmentSource);
162 
164  static const jfc::shared_proxy_ptr<gdk::webgl1es2_shader_program> PinkShaderOfDeath;
165 
168  static const jfc::shared_proxy_ptr<gdk::webgl1es2_shader_program> AlphaCutOff;
169 
174  static const size_t MAX_TEXTURE_UNITS = 8;
175 
176  private:
178  jfc::unique_handle<GLuint> m_VertexShaderHandle;
179 
181  jfc::unique_handle<GLuint> m_FragmentShaderHandle;
182 
184  jfc::unique_handle<GLuint> m_ProgramHandle;
185 
187  active_attribute_collection_type m_ActiveAttributes;
188 
190  active_uniform_collection_type m_ActiveUniforms;
191  };
192 }
193 
194 #endif
195 
void useProgram() const
bind an array of textures to the context then assign them to texture uniforms
std::unordered_map< std::string, active_uniform_info > active_uniform_collection_type
Definition: webgl1es2_shader_program.h:49
GLint location
location of the uniform e.g: 1
Definition: webgl1es2_shader_program.h:39
Specifies drawing behaviours at the programmable stages in the graphics pipeline. ...
Definition: shader_program.h:9
bool operator==(const webgl1es2_shader_program &) const
equality semantics
webgl1es2_shader_program & operator=(webgl1es2_shader_program &&)=default
move semantics
GLint size
size of the uniform e.g: 1
Definition: webgl1es2_shader_program.h:41
index, size, type of an active attribute used in the shader program
Definition: webgl1es2_shader_program.h:28
std::array< GLint, 4 > integer4_uniform_type
type alias used for setting int4 uniform value
Definition: webgl1es2_shader_program.h:56
Definition: camera.h:9
a texture generally represents the color of a surface
Definition: webgl1es2_texture.h:18
static const jfc::shared_proxy_ptr< gdk::webgl1es2_shader_program > AlphaCutOff
Definition: webgl1es2_shader_program.h:168
index. size. type o an active uniform used in the shader program
Definition: webgl1es2_shader_program.h:37
static const size_t MAX_TEXTURE_UNITS
Definition: webgl1es2_shader_program.h:174
GLint count
number of components in the attribute, e.g: 2
Definition: webgl1es2_shader_program.h:33
Specifies drawing behaviours at the two programmable stages in the OpenGL ES 2.0/WebGL 1...
Definition: webgl1es2_shader_program.h:24
GLenum type
type of the uniform e.g: Texture
Definition: webgl1es2_shader_program.h:40
bool try_set_uniform(const std::string &aName, const GLfloat aValue) const
assign a float1 uniform from a float
std::array< GLint, 3 > integer3_uniform_type
type alias used for setting int3 uniform value
Definition: webgl1es2_shader_program.h:54
std::optional< active_attribute_info > tryGetActiveAttribute(const std::string &aAttributeName) const
returns a nonnull optional to an attribute info if one with the given name exists ...
GLenum type
type of the attribute&#39;s components, e.g: float
Definition: webgl1es2_shader_program.h:31
static const jfc::shared_proxy_ptr< gdk::webgl1es2_shader_program > PinkShaderOfDeath
shader useful for indicating some kind of failure. Performs MVP mul then colors all frags bright pink...
Definition: webgl1es2_shader_program.h:164
bool operator!=(const webgl1es2_shader_program &) const
equality semantics
webgl1es2_shader_program(webgl1es2_shader_program &&)=default
move semantics
GLint location
location of the attribute
Definition: webgl1es2_shader_program.h:30
std::unordered_map< std::string, active_attribute_info > active_attribute_collection_type
Definition: webgl1es2_shader_program.h:46
std::array< GLint, 2 > integer2_uniform_type
type alias used for setting int2 uniform value
Definition: webgl1es2_shader_program.h:52