StapleGL
Header-only C++20 OpenGL wrapper
|
Shader program class. More...
#include <shader.hpp>
Public Member Functions | |
shader_program ()=default | |
shader_program (std::string_view name, std::string_view path) noexcept | |
Construct a new shader program object from a name and a path. | |
shader_program (std::string_view name, std::initializer_list< std::pair< shader_type, std::string_view > > shaders) noexcept | |
Construct a new shader program object from a name and a list of shaders sources. | |
shader_program (std::string_view path) noexcept | |
Construct a new shader program object from a path. | |
shader_program (const shader_program &)=default | |
auto | operator= (const shader_program &) -> shader_program &=default |
shader_program (shader_program &&other) noexcept | |
auto | operator= (shader_program &&other) noexcept -> shader_program & |
~shader_program () | |
Destroy the shader program object. | |
void | bind () const |
Bind the shader program. | |
void | unbind () const |
Unbind the shader program. | |
void | upload_uniform1i (std::string_view name, int val) |
Upload an integer uniform to the shader program. | |
void | upload_uniform1f (std::string_view name, float val) |
Upload a float uniform to the shader program. | |
void | upload_uniform2f (std::string_view name, float val0, float val1) |
Upload a 2D float uniform to the shader program. | |
void | upload_uniform3f (std::string_view name, float val0, float val1, float val2) |
Upload a 3D float uniform to the shader program. | |
void | upload_uniform4f (std::string_view name, float val0, float val1, float val2, float val3) |
Upload a 4D float uniform to the shader program. | |
void | upload_uniform_mat4f (std::string_view name, float const *mat) |
Upload a 4x4 float matrix uniform to the shader program. | |
void | upload_uniform_mat3f (std::string_view name, float const *mat) |
Upload a 3x3 float matrix uniform to the shader program. | |
auto constexpr | program_id () const -> std::uint32_t |
Obtain the shader program id. | |
auto constexpr | name () const -> std::string |
Obtain the shader program name. | |
auto | operator[] (std::size_t index) -> shader & |
Obtain a reference to a shader in the shader program. | |
auto | operator[] (std::size_t index) const -> const shader & |
Obtain a const reference to a shader in the shader program. | |
Static Public Member Functions | |
static auto | is_valid (std::uint32_t id) -> bool |
Check if a shader program is valid. | |
Private Member Functions | |
auto | create_program () const -> std::uint32_t |
Create a program object. | |
auto | compile (shader_type shader_type, std::string_view source) const -> std::uint32_t |
Create a shader object. | |
auto | parse_shaders (std::string_view source) const -> std::vector< shader > |
Link the shader program. | |
auto | uniform_location (std::string_view name) -> int |
Obtain the location of a uniform in the shader program. | |
Static Private Member Functions | |
static constexpr auto | to_gl_type (shader_type shader_type) -> std::uint32_t |
Convert a shader type to its OpenGL equivalent. | |
static auto | string_to_shader_type (std::string_view str) -> std::optional< shader_type > |
Convert a string to a shader type. | |
Private Attributes | |
std::vector< shader > | m_shaders |
std::unordered_map< std::string_view, int > | m_uniform_cache |
std::uint32_t | m_id {} |
std::string | m_name |
Shader program class.
The shader program class is used to load and compile shaders, after compilation, it can also be used as an interface to each contained shader, for actions such as uploading uniforms.
Each shader program has it's own internal cache of uniform locations. this avoids expensive API calls on each uniform upload.
Definition at line 77 of file shader.hpp.
|
default |
|
inlinenoexcept |
Construct a new shader program object from a name and a path.
This constructor is to be used when the shader program is to be loaded from a file which contains a set of GLSL shaders, separated by the #type
tag.
name | Shader program name, for debugging purposes. |
path | Shader program path, currently it must be relative to the current working directory. |
Definition at line 340 of file shader.hpp.
|
inlinenoexcept |
Construct a new shader program object from a name and a list of shaders sources.
This constructor is to be used when the shader program is to be loaded from a list of shader sources available at compile time, with the sources present as strings in the code.
name | Shader name, for debugging purposes. |
shaders | A list of shader sources, each shader source is a pair of shader type and shader source. |
Definition at line 348 of file shader.hpp.
|
inlinenoexcept |
Construct a new shader program object from a path.
This constructor is to be used when the shader program is to be loaded from a file and we do not care about the name of the shader program.
path | Shader program path, currently it must be relative to the current working directory. |
Definition at line 357 of file shader.hpp.
|
default |
|
inlinenoexcept |
Definition at line 119 of file shader.hpp.
|
inline |
Destroy the shader program object.
Definition at line 362 of file shader.hpp.
|
inline |
Bind the shader program.
Definition at line 367 of file shader.hpp.
|
inlineprivate |
Create a shader object.
shader_type | The shader type. |
source | The shader source. |
Definition at line 489 of file shader.hpp.
|
inlineprivate |
Create a program object.
Definition at line 432 of file shader.hpp.
|
inlinestatic |
Check if a shader program is valid.
A shader program is valid if it is compiled and linked.
id | Shader program id. |
Definition at line 574 of file shader.hpp.
|
inlineconstexpr |
Obtain the shader program name.
Definition at line 417 of file shader.hpp.
|
default |
|
inlinenoexcept |
Definition at line 128 of file shader.hpp.
|
inline |
Obtain a reference to a shader in the shader program.
index | Shader index. |
Definition at line 422 of file shader.hpp.
|
inline |
Obtain a const reference to a shader in the shader program.
index | Shader index. |
Definition at line 427 of file shader.hpp.
|
inlineprivate |
Link the shader program.
This method acts on the provided source of shaders, it will pre-process the source, splitting the monolithic source into individual shader sources by scanning for the #type tag.
source | The shader program source. |
Definition at line 523 of file shader.hpp.
|
inlineconstexpr |
Obtain the shader program id.
Definition at line 412 of file shader.hpp.
|
inlinestaticprivate |
Convert a string to a shader type.
str | The string to convert. |
Definition at line 636 of file shader.hpp.
|
inlinestaticconstexprprivate |
Convert a shader type to its OpenGL equivalent.
shader_type | the shader type. |
Definition at line 614 of file shader.hpp.
|
inline |
Unbind the shader program.
Definition at line 372 of file shader.hpp.
|
inlineprivate |
Obtain the location of a uniform in the shader program.
name | Uniform name. |
Definition at line 557 of file shader.hpp.
|
inline |
Upload a float uniform to the shader program.
name | Uniform name. |
val | Uniform value. |
Definition at line 382 of file shader.hpp.
|
inline |
Upload an integer uniform to the shader program.
name | Uniform name. |
val | Uniform value. |
Definition at line 377 of file shader.hpp.
|
inline |
Upload a 2D float uniform to the shader program.
name | Uniform name. |
val0 | Uniform value. |
val1 | Uniform value. |
Definition at line 387 of file shader.hpp.
|
inline |
Upload a 3D float uniform to the shader program.
name | Uniform name. |
val0 | Uniform value. |
val1 | Uniform value. |
val2 | Uniform value. |
Definition at line 392 of file shader.hpp.
|
inline |
Upload a 4D float uniform to the shader program.
name | Uniform name. |
val0 | Uniform value. |
val1 | Uniform value. |
val2 | Uniform value. |
val3 | Uniform value. |
Definition at line 397 of file shader.hpp.
|
inline |
Upload a 3x3 float matrix uniform to the shader program.
name | Uniform name. |
mat | Contiguous span of 9 floats representing the matrix. |
Definition at line 407 of file shader.hpp.
|
inline |
Upload a 4x4 float matrix uniform to the shader program.
name | Uniform name. |
mat | Contiguous span of 16 floats representing the matrix. |
Definition at line 402 of file shader.hpp.
|
private |
Definition at line 330 of file shader.hpp.
|
private |
Definition at line 331 of file shader.hpp.
|
private |
Definition at line 328 of file shader.hpp.
|
private |
Definition at line 329 of file shader.hpp.