StapleGL
Header-only C++20 OpenGL wrapper
Loading...
Searching...
No Matches
staplegl::shader_program Class Reference

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< shaderm_shaders
 
std::unordered_map< std::string_view, int > m_uniform_cache
 
std::uint32_t m_id {}
 
std::string m_name
 

Detailed Description

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.

Note
if utilizing paths to load the shaders, it is important to note that they will be relative to the current working directory when running the program, as an advice, it is best to design your build system so that your shaders are packaged with the executable.
Examples
batches.cpp, sandbox.cpp, and teapot.cpp.

Definition at line 77 of file shader.hpp.

Constructor & Destructor Documentation

◆ shader_program() [1/6]

staplegl::shader_program::shader_program ( )
default

◆ shader_program() [2/6]

staplegl::shader_program::shader_program ( std::string_view  name,
std::string_view  path 
)
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.

Parameters
nameShader program name, for debugging purposes.
pathShader program path, currently it must be relative to the current working directory.

Definition at line 340 of file shader.hpp.

◆ shader_program() [3/6]

staplegl::shader_program::shader_program ( std::string_view  name,
std::initializer_list< std::pair< shader_type, std::string_view > >  shaders 
)
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.

Parameters
nameShader name, for debugging purposes.
shadersA list of shader sources, each shader source is a pair of shader type and shader source.

Definition at line 348 of file shader.hpp.

◆ shader_program() [4/6]

staplegl::shader_program::shader_program ( std::string_view  path)
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.

Note
This constructor automatically generates a name for the shader program by obtaining it from its path.
Parameters
pathShader program path, currently it must be relative to the current working directory.

Definition at line 357 of file shader.hpp.

◆ shader_program() [5/6]

staplegl::shader_program::shader_program ( const shader_program )
default

◆ shader_program() [6/6]

staplegl::shader_program::shader_program ( shader_program &&  other)
inlinenoexcept

Definition at line 119 of file shader.hpp.

◆ ~shader_program()

staplegl::shader_program::~shader_program ( )
inline

Destroy the shader program object.

Definition at line 362 of file shader.hpp.

Member Function Documentation

◆ bind()

void staplegl::shader_program::bind ( ) const
inline

Bind the shader program.

Examples
batches.cpp, and teapot.cpp.

Definition at line 367 of file shader.hpp.

◆ compile()

auto staplegl::shader_program::compile ( shader_type  shader_type,
std::string_view  source 
) const -> std::uint32_t
inlineprivate

Create a shader object.

Parameters
shader_typeThe shader type.
See also
staplegl::shader_type
Parameters
sourceThe shader source.
Returns
std::uint32_t, the shader object id.

Definition at line 489 of file shader.hpp.

◆ create_program()

auto staplegl::shader_program::create_program ( ) const -> std::uint32_t
inlineprivate

Create a program object.

Returns
std::uint32_t, the program object id.

Definition at line 432 of file shader.hpp.

◆ is_valid()

auto staplegl::shader_program::is_valid ( std::uint32_t  id) -> bool
inlinestatic

Check if a shader program is valid.

A shader program is valid if it is compiled and linked.

Parameters
idShader program id.
Returns
true, if the shader program is valid.
false, if the shader program is not valid.

Definition at line 574 of file shader.hpp.

◆ name()

constexpr auto staplegl::shader_program::name ( ) const -> std::string
inlineconstexpr

Obtain the shader program name.

Returns
std::string the shader program name.

Definition at line 417 of file shader.hpp.

◆ operator=() [1/2]

auto staplegl::shader_program::operator= ( const shader_program ) -> shader_program &=default
default

◆ operator=() [2/2]

auto staplegl::shader_program::operator= ( shader_program &&  other) -> shader_program&
inlinenoexcept

Definition at line 128 of file shader.hpp.

◆ operator[]() [1/2]

auto staplegl::shader_program::operator[] ( std::size_t  index) -> shader&
inline

Obtain a reference to a shader in the shader program.

Parameters
indexShader index.
See also
staplegl::shader
Returns
shader&, a reference to the shader.

Definition at line 422 of file shader.hpp.

◆ operator[]() [2/2]

auto staplegl::shader_program::operator[] ( std::size_t  index) const -> const shader&
inline

Obtain a const reference to a shader in the shader program.

Parameters
indexShader index.
See also
staplegl::shader
Returns
const shader&, a const reference to the shader.

Definition at line 427 of file shader.hpp.

◆ parse_shaders()

auto staplegl::shader_program::parse_shaders ( std::string_view  source) const -> std::vector<shader>
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.

Parameters
sourceThe shader program source.
See also
staplegl::shader_type
Warning
In the case of a failure in parsing, such as an invalid shader type, this method will return an empty vector.
Returns
std::vector<shader>. A vector of shaders.

Definition at line 523 of file shader.hpp.

◆ program_id()

constexpr auto staplegl::shader_program::program_id ( ) const -> std::uint32_t
inlineconstexpr

Obtain the shader program id.

Returns
std::uint32_t the shader program id in the OpenGL context.

Definition at line 412 of file shader.hpp.

◆ string_to_shader_type()

auto staplegl::shader_program::string_to_shader_type ( std::string_view  str) -> std::optional<shader_type>
inlinestaticprivate

Convert a string to a shader type.

Parameters
strThe string to convert.
Returns
shader_type, the shader type.

Definition at line 636 of file shader.hpp.

◆ to_gl_type()

constexpr auto staplegl::shader_program::to_gl_type ( shader_type  shader_type) -> std::uint32_t
inlinestaticconstexprprivate

Convert a shader type to its OpenGL equivalent.

See also
staplegl::shader_type
Parameters
shader_typethe shader type.
Returns
std::uint32_t, the OpenGL equivalent of the shader type, as an OpenGL enum.

Definition at line 614 of file shader.hpp.

◆ unbind()

void staplegl::shader_program::unbind ( ) const
inline

Unbind the shader program.

Definition at line 372 of file shader.hpp.

◆ uniform_location()

auto staplegl::shader_program::uniform_location ( std::string_view  name) -> int
inlineprivate

Obtain the location of a uniform in the shader program.

Parameters
nameUniform name.
Returns
int, the uniform location.

Definition at line 557 of file shader.hpp.

◆ upload_uniform1f()

void staplegl::shader_program::upload_uniform1f ( std::string_view  name,
float  val 
)
inline

Upload a float uniform to the shader program.

Parameters
nameUniform name.
valUniform value.

Definition at line 382 of file shader.hpp.

◆ upload_uniform1i()

void staplegl::shader_program::upload_uniform1i ( std::string_view  name,
int  val 
)
inline

Upload an integer uniform to the shader program.

Parameters
nameUniform name.
valUniform value.

Definition at line 377 of file shader.hpp.

◆ upload_uniform2f()

void staplegl::shader_program::upload_uniform2f ( std::string_view  name,
float  val0,
float  val1 
)
inline

Upload a 2D float uniform to the shader program.

Parameters
nameUniform name.
val0Uniform value.
val1Uniform value.

Definition at line 387 of file shader.hpp.

◆ upload_uniform3f()

void staplegl::shader_program::upload_uniform3f ( std::string_view  name,
float  val0,
float  val1,
float  val2 
)
inline

Upload a 3D float uniform to the shader program.

Parameters
nameUniform name.
val0Uniform value.
val1Uniform value.
val2Uniform value.

Definition at line 392 of file shader.hpp.

◆ upload_uniform4f()

void staplegl::shader_program::upload_uniform4f ( std::string_view  name,
float  val0,
float  val1,
float  val2,
float  val3 
)
inline

Upload a 4D float uniform to the shader program.

Parameters
nameUniform name.
val0Uniform value.
val1Uniform value.
val2Uniform value.
val3Uniform value.

Definition at line 397 of file shader.hpp.

◆ upload_uniform_mat3f()

void staplegl::shader_program::upload_uniform_mat3f ( std::string_view  name,
float const *  mat 
)
inline

Upload a 3x3 float matrix uniform to the shader program.

Parameters
nameUniform name.
matContiguous span of 9 floats representing the matrix.

Definition at line 407 of file shader.hpp.

◆ upload_uniform_mat4f()

void staplegl::shader_program::upload_uniform_mat4f ( std::string_view  name,
float const *  mat 
)
inline

Upload a 4x4 float matrix uniform to the shader program.

Parameters
nameUniform name.
matContiguous span of 16 floats representing the matrix.

Definition at line 402 of file shader.hpp.

Member Data Documentation

◆ m_id

std::uint32_t staplegl::shader_program::m_id {}
private

Definition at line 330 of file shader.hpp.

◆ m_name

std::string staplegl::shader_program::m_name
private

Definition at line 331 of file shader.hpp.

◆ m_shaders

std::vector<shader> staplegl::shader_program::m_shaders
private

Definition at line 328 of file shader.hpp.

◆ m_uniform_cache

std::unordered_map<std::string_view, int> staplegl::shader_program::m_uniform_cache
private

Definition at line 329 of file shader.hpp.


The documentation for this class was generated from the following file: