gdk-graphics 0b051eb9b5c1eaa0658babaa4463dd7a80aa9d2c
Loading...
Searching...
No Matches
webgl1es2_shader_program.h
1// © 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/unique_handle.h>
11#include <jfc/lazy_ptr.h>
12
13#include <array>
14#include <optional>
15#include <string>
16#include <string_view>
17#include <unordered_map>
18
19namespace gdk {
23 class webgl1es2_shader_program final : public shader_program {
24 public:
27 GLint location;
28 GLenum type;
29 GLint count;
30 };
31
34 GLint location;
35 GLenum type;
36 GLint size;
37 };
38
41 using active_attribute_collection_type = std::unordered_map<std::string, active_attribute_info>;
44 using active_uniform_collection_type = std::unordered_map<std::string, active_uniform_info>;
45
47 using integer2_uniform_type = graphics_intvector2_type;
49 using integer3_uniform_type = graphics_intvector3_type;
51 using integer4_uniform_type = graphics_intvector4_type;
52
53 public:
55 std::optional<active_attribute_info> tryGetActiveAttribute(
56 const std::string &aAttributeName) const;
57
59 bool try_set_uniform(const std::string &aName, const GLfloat aValue) const;
61 bool try_set_uniform(const std::string &aName, const graphics_vector2_type &aValue) const;
63 bool try_set_uniform(const std::string &aName, const graphics_vector3_type &aValue) const;
65 bool try_set_uniform(const std::string &aName, const graphics_vector4_type &aValue) const;
67 bool try_set_uniform(const std::string &aName, const std::vector<GLfloat> &avalue) const;
69 bool try_set_uniform(const std::string &aName, const std::vector<graphics_vector2_type> &avalue) const;
71 bool try_set_uniform(const std::string &aName, const std::vector<graphics_vector3_type> &avalue) const;
73 bool try_set_uniform(const std::string &aName, const std::vector<graphics_vector4_type> &avalue) const;
74
76 bool try_set_uniform(const std::string &aName, const GLint aValue) const;
78 bool try_set_uniform(const std::string aName, const integer2_uniform_type &aValue) const;
80 bool try_set_uniform(const std::string aName, const integer3_uniform_type &aValue) const;
82 bool try_set_uniform(const std::string aName, const integer4_uniform_type &aValue) const;
83
85 bool try_set_uniform(const std::string &aName, const std::vector<GLint> &aValue) const;
87 bool try_set_uniform(const std::string &aName, const std::vector<integer2_uniform_type> &aValue) const;
89 bool try_set_uniform(const std::string &aName, const std::vector<integer3_uniform_type> &aValue) const;
91 bool try_set_uniform(const std::string &aName, const std::vector<integer4_uniform_type> &aValue) const;
92
94 //bool try_set_uniform(const std::string &aName, const bool &aValue) const;
96 //bool try_set_uniform(const std::string &aName, const boolean2_uniform_type &a) const;
98 //bool try_set_uniform(const std::string &aName, const boolean3_uniform_type &a) const;
100 //bool try_set_uniform(const std::string &aName, const boolean4_uniform_type &a) const;
101
103 //bool try_set_uniform(const std::string &aName, const std::vector<bool> &aValue) const;
105 //bool try_set_uniform(const std::string>&aName, const std::vector<boolean2_uniform_type> &a) const;
107 //bool try_set_uniform(const std::string>&aName, const std::vector<boolean3_uniform_type> &a) const;
109 //bool try_set_uniform(const std::string>&aName, const std::vector<boolean4_uniform_type> &a) const;
110
111 /*//! assign a mat2x2 uniform from a mat2x2
112 bool try_set_uniform(const std::string &aName, const graphics_mat2x2_type &avalue) const;
114 bool try_set_uniform(const std::string &aName, const std::vector<graphics_mat2x2_type> &avalue) const;
115
117 bool try_set_uniform(const std::string &aName, const graphics_mat3x3_type &avalue) const;
119 bool try_set_uniform(const std::string &aName, const std::vector<graphics_mat3x3_type> &avalue) const;*/
120
122 bool try_set_uniform(const std::string &aName, const graphics_matrix4x4_type &avalue) const;
124 bool try_set_uniform(const std::string &aName, const std::vector<graphics_matrix4x4_type> &avalue) const;
125
126 //TODO: texture needs to support more than tex2d!
128 bool try_set_uniform(const std::string &aName, const gdk::webgl1es2_texture &aTexture) const;
129
130 //TODO: texture needs to support more than tex2d!
132 //bool try_set_uniform(const std::string &aName, const gdk::texture &aTexture) const;
133
136 void useProgram() const;
137
142
147
149 webgl1es2_shader_program(std::string aVertexSource, std::string aFragmentSource);
150
152 static const jfc::lazy_ptr<gdk::webgl1es2_shader_program> PinkShaderOfDeath;
153
156 static const jfc::lazy_ptr<gdk::webgl1es2_shader_program> AlphaCutOff;
157
158 static const size_t MAX_TEXTURE_UNITS();
159 static const size_t MAX_FRAGMENT_SHADER_INSTRUCTIONS();
160 static const size_t MAX_VERTEX_SHADER_INSTRUCTIONS();
161
162 private:
164 jfc::unique_handle<GLuint> m_VertexShaderHandle;
165
167 jfc::unique_handle<GLuint> m_FragmentShaderHandle;
168
170 jfc::unique_handle<GLuint> m_ProgramHandle;
171
173 active_attribute_collection_type m_ActiveAttributes;
174
176 active_uniform_collection_type m_ActiveUniforms;
177 };
178}
179
180#endif
181
std::unordered_map< std::string, active_uniform_info > active_uniform_collection_type
Definition webgl1es2_shader_program.h:44
bool try_set_uniform(const std::string aName, const integer3_uniform_type &aValue) const
assign a value to a integer3 uniform
bool try_set_uniform(const std::string &aName, const graphics_matrix4x4_type &avalue) const
assign a value to a bool uniform
bool try_set_uniform(const std::string &aName, const std::vector< graphics_matrix4x4_type > &avalue) const
assign a mat4x4 uniform array from a vector of mat4x4s
bool try_set_uniform(const std::string &aName, const GLfloat aValue) const
assign a float1 uniform from a float
bool try_set_uniform(const std::string &aName, const graphics_vector3_type &aValue) const
assign a float3 uniform from a 3 component vector
bool try_set_uniform(const std::string &aName, const graphics_vector4_type &aValue) const
assign a float4 uniform from a 4 component vector
webgl1es2_shader_program & operator=(webgl1es2_shader_program &&)=default
move semantics
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
bool try_set_uniform(const std::string &aName, const std::vector< integer3_uniform_type > &aValue) const
assign a value to a integer3 uniform array
bool try_set_uniform(const std::string &aName, const std::vector< GLint > &aValue) const
assign a value to a integer uniform array
bool try_set_uniform(const std::string &aName, const graphics_vector2_type &aValue) const
assign a float2 uniform from a 2 component vector
bool try_set_uniform(const std::string aName, const integer2_uniform_type &aValue) const
assign a value to a integer2 uniform
static const jfc::lazy_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:152
bool try_set_uniform(const std::string &aName, const std::vector< GLfloat > &avalue) const
assign a float uniform array from an array of floats
graphics_intvector2_type integer2_uniform_type
type alias used for setting int2 uniform value
Definition webgl1es2_shader_program.h:47
graphics_intvector4_type integer4_uniform_type
type alias used for setting int4 uniform value
Definition webgl1es2_shader_program.h:51
bool operator!=(const webgl1es2_shader_program &) const
equality semantics
std::unordered_map< std::string, active_attribute_info > active_attribute_collection_type
Definition webgl1es2_shader_program.h:41
bool try_set_uniform(const std::string &aName, const std::vector< integer2_uniform_type > &aValue) const
assign a value to a integer2 uniform array
bool try_set_uniform(const std::string aName, const integer4_uniform_type &aValue) const
assign a value to a integer4 uniform
bool operator==(const webgl1es2_shader_program &) const
equality semantics
void useProgram() const
bind an array of textures to the context then assign them to texture uniforms
bool try_set_uniform(const std::string &aName, const gdk::webgl1es2_texture &aTexture) const
bind a texture to the context then assign it to a texture uniform
bool try_set_uniform(const std::string &aName, const std::vector< integer4_uniform_type > &aValue) const
assign a value to a integer4 uniform array
bool try_set_uniform(const std::string &aName, const std::vector< graphics_vector3_type > &avalue) const
assign a value to a float3 uniform array from a vector of float3s
static const jfc::lazy_ptr< gdk::webgl1es2_shader_program > AlphaCutOff
Definition webgl1es2_shader_program.h:156
webgl1es2_shader_program(std::string aVertexSource, std::string aFragmentSource)
constructs a shader program with glsl source file contents for a vertex shader and a fragment shader
bool try_set_uniform(const std::string &aName, const GLint aValue) const
assign a value to a integer uniform
webgl1es2_shader_program(webgl1es2_shader_program &&)=default
move semantics
bool try_set_uniform(const std::string &aName, const std::vector< graphics_vector4_type > &avalue) const
assign a value to a float4 uniform array from a vector of float4s
graphics_intvector3_type integer3_uniform_type
type alias used for setting int3 uniform value
Definition webgl1es2_shader_program.h:49
bool try_set_uniform(const std::string &aName, const std::vector< graphics_vector2_type > &avalue) const
assign a float2 uniform array from an array of float2s
Definition webgl1es2_texture.h:19
index, size, type of an active attribute used in the shader program
Definition webgl1es2_shader_program.h:26
GLenum type
type of the attribute's components, e.g: float
Definition webgl1es2_shader_program.h:28
GLint count
number of attributes
Definition webgl1es2_shader_program.h:29
GLint location
location of the attribute
Definition webgl1es2_shader_program.h:27
index. size. type o an active uniform used in the shader program
Definition webgl1es2_shader_program.h:33
GLint size
size of the uniform e.g: 1
Definition webgl1es2_shader_program.h:36
GLenum type
type of the uniform e.g: Texture
Definition webgl1es2_shader_program.h:35
GLint location
location of the uniform e.g: 1
Definition webgl1es2_shader_program.h:34