galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
galaxy::graphics::Shader Class Referencefinal

OpenGL Shader Program. More...

#include <Shader.hpp>

+ Collaboration diagram for galaxy::graphics::Shader:

Public Member Functions

 Shader ()
 Constructor.
 
 Shader (const nlohmann::json &json)
 JSON Constructor.
 
 Shader (Shader &&)
 Move constructor.
 
Shaderoperator= (Shader &&)
 Move assignment operator.
 
 ~Shader ()
 Destructor.
 
bool load (const std::string &file)
 Loads a combined shader.
 
bool load (const std::string &vertex, const std::string &frag)
 Loads an OpenGL shader.
 
bool parse (const std::string &src)
 Loads a combined raw shader.
 
bool parse (const std::string &vertex, const std::string &frag)
 Loads a raw shader.
 
void compile ()
 Compiles shader into GPU mem.
 
void destroy ()
 Destroys shader program.
 
void bind () const
 Make active shader.
 
void unbind () const
 Unbind.
 
template<typename... Uniforms>
void set_uniform (const std::string &name, const Uniforms &... args)
 Specialized variadic template for setting shader uniforms.
 
unsigned int id () const
 Get program id.
 
template<>
void set_uniform (const std::string &name)
 
template<>
void set_uniform (const std::string &name, const bool &a)
 
template<>
void set_uniform (const std::string &name, const int &a)
 
template<>
void set_uniform (const std::string &name, const int &a, const int &b)
 
template<>
void set_uniform (const std::string &name, const int &a, const int &b, const int &c)
 
template<>
void set_uniform (const std::string &name, const int &a, const int &b, const int &c, const int &d)
 
template<>
void set_uniform (const std::string &name, const unsigned int &a)
 
template<>
void set_uniform (const std::string &name, const unsigned int &a, const unsigned int &b)
 
template<>
void set_uniform (const std::string &name, const unsigned int &a, const unsigned int &b, const unsigned int &c)
 
template<>
void set_uniform (const std::string &name, const unsigned int &a, const unsigned int &b, const unsigned int &c, const unsigned int &d)
 
template<>
void set_uniform (const std::string &name, const float &a)
 
template<>
void set_uniform (const std::string &name, const float &a, const float &b)
 
template<>
void set_uniform (const std::string &name, const float &a, const float &b, const float &c)
 
template<>
void set_uniform (const std::string &name, const float &a, const float &b, const float &c, const float &d)
 
template<>
void set_uniform (const std::string &name, const double &a)
 
template<>
void set_uniform (const std::string &name, const double &a, const double &b)
 
template<>
void set_uniform (const std::string &name, const double &a, const double &b, const double &c)
 
template<>
void set_uniform (const std::string &name, const double &a, const double &b, const double &c, const double &d)
 

Private Member Functions

 Shader (const Shader &)=delete
 Copy constructor.
 
Shaderoperator= (const Shader &)=delete
 Copy assignment operator.
 
bool preprocess (const std::string &src)
 Extract source code from a combined shader.
 
int get_uniform_location (const std::string &name)
 Retrieves the location of a shader uniform.
 

Private Attributes

unsigned int m_id
 OpenGL handle.
 
ankerl::unordered_dense::map< std::string, int > m_cache
 Cache of uniforms for better performance.
 
entt::compressed_pair< std::string, std::string > m_src
 Shader source code.
 

Detailed Description

OpenGL Shader Program.

Currently only supports vertex and fragment shaders.

Definition at line 28 of file Shader.hpp.

Constructor & Destructor Documentation

◆ Shader() [1/4]

galaxy::graphics::Shader::Shader ( )

Constructor.

Definition at line 26 of file Shader.cpp.

◆ Shader() [2/4]

galaxy::graphics::Shader::Shader ( const nlohmann::json & json)

JSON Constructor.

Parameters
jsonJSON defining object.

Definition at line 31 of file Shader.cpp.

+ Here is the call graph for this function:

◆ Shader() [3/4]

galaxy::graphics::Shader::Shader ( Shader && s)

Move constructor.

Definition at line 44 of file Shader.cpp.

◆ ~Shader()

galaxy::graphics::Shader::~Shader ( )

Destructor.

Definition at line 77 of file Shader.cpp.

+ Here is the call graph for this function:

◆ Shader() [4/4]

galaxy::graphics::Shader::Shader ( const Shader & )
privatedelete

Copy constructor.

Member Function Documentation

◆ operator=() [1/2]

Shader & galaxy::graphics::Shader::operator= ( Shader && s)

Move assignment operator.

Definition at line 58 of file Shader.cpp.

◆ load() [1/2]

bool galaxy::graphics::Shader::load ( const std::string & file)
nodiscard

Loads a combined shader.

Parameters
filePath to a combined shader source.
Returns
True if successful.

Definition at line 82 of file Shader.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ load() [2/2]

bool galaxy::graphics::Shader::load ( const std::string & vertex,
const std::string & frag )
nodiscard

Loads an OpenGL shader.

Parameters
vertexVertex shader.
fragFragment shader.
Returns
True if successful.

Definition at line 90 of file Shader.cpp.

+ Here is the call graph for this function:

◆ parse() [1/2]

bool galaxy::graphics::Shader::parse ( const std::string & src)
nodiscard

Loads a combined raw shader.

Parameters
srcRaw shader code.
Returns
True if successful.

Definition at line 100 of file Shader.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse() [2/2]

bool galaxy::graphics::Shader::parse ( const std::string & vertex,
const std::string & frag )
nodiscard

Loads a raw shader.

Parameters
vertexRaw vertex shader code.
fragRaw fragment shader code.
Returns
True if successful.

Definition at line 105 of file Shader.cpp.

◆ compile()

void galaxy::graphics::Shader::compile ( )

Compiles shader into GPU mem.

Definition at line 130 of file Shader.cpp.

+ Here is the caller graph for this function:

◆ destroy()

void galaxy::graphics::Shader::destroy ( )

Destroys shader program.

Definition at line 219 of file Shader.cpp.

+ Here is the caller graph for this function:

◆ bind()

void galaxy::graphics::Shader::bind ( ) const

Make active shader.

Definition at line 227 of file Shader.cpp.

+ Here is the caller graph for this function:

◆ unbind()

void galaxy::graphics::Shader::unbind ( ) const

Unbind.

Definition at line 232 of file Shader.cpp.

◆ set_uniform() [1/19]

template<typename... Uniforms>
void galaxy::graphics::Shader::set_uniform ( const std::string & name,
const Uniforms &... args )

Specialized variadic template for setting shader uniforms.

Useful for i.e. 2f would be ("name", 1.0f, 2.0f) and 3i would be ("name", 1, 2, 3)

Parameters
nameThe name of the uniform to set.
argsVariable amount of arguments for the uniform.
+ Here is the caller graph for this function:

◆ id()

unsigned int galaxy::graphics::Shader::id ( ) const
nodiscard

Get program id.

Returns
unsigned int.

Definition at line 237 of file Shader.cpp.

◆ operator=() [2/2]

Shader & galaxy::graphics::Shader::operator= ( const Shader & )
privatedelete

Copy assignment operator.

◆ preprocess()

bool galaxy::graphics::Shader::preprocess ( const std::string & src)
nodiscardprivate

Extract source code from a combined shader.

Parameters
srcRaw source code.
Returns
True if parse was successful.

Definition at line 242 of file Shader.cpp.

+ Here is the caller graph for this function:

◆ get_uniform_location()

int galaxy::graphics::Shader::get_uniform_location ( const std::string & name)
nodiscardprivate

Retrieves the location of a shader uniform.

Parameters
nameShader uniform name.
Returns
Uniform location. -1 on error.

Definition at line 289 of file Shader.cpp.

+ Here is the caller graph for this function:

◆ set_uniform() [2/19]

template<>
void galaxy::graphics::Shader::set_uniform ( const std::string & name)
inline

Definition at line 189 of file Shader.hpp.

◆ set_uniform() [3/19]

template<>
void galaxy::graphics::Shader::set_uniform ( const std::string & name,
const bool & a )
inline

Definition at line 195 of file Shader.hpp.

+ Here is the call graph for this function:

◆ set_uniform() [4/19]

template<>
void galaxy::graphics::Shader::set_uniform ( const std::string & name,
const int & a )
inline

Definition at line 201 of file Shader.hpp.

+ Here is the call graph for this function:

◆ set_uniform() [5/19]

template<>
void galaxy::graphics::Shader::set_uniform ( const std::string & name,
const int & a,
const int & b )
inline

Definition at line 207 of file Shader.hpp.

+ Here is the call graph for this function:

◆ set_uniform() [6/19]

template<>
void galaxy::graphics::Shader::set_uniform ( const std::string & name,
const int & a,
const int & b,
const int & c )
inline

Definition at line 213 of file Shader.hpp.

+ Here is the call graph for this function:

◆ set_uniform() [7/19]

template<>
void galaxy::graphics::Shader::set_uniform ( const std::string & name,
const int & a,
const int & b,
const int & c,
const int & d )
inline

Definition at line 219 of file Shader.hpp.

+ Here is the call graph for this function:

◆ set_uniform() [8/19]

template<>
void galaxy::graphics::Shader::set_uniform ( const std::string & name,
const unsigned int & a )
inline

Definition at line 225 of file Shader.hpp.

+ Here is the call graph for this function:

◆ set_uniform() [9/19]

template<>
void galaxy::graphics::Shader::set_uniform ( const std::string & name,
const unsigned int & a,
const unsigned int & b )
inline

Definition at line 231 of file Shader.hpp.

+ Here is the call graph for this function:

◆ set_uniform() [10/19]

template<>
void galaxy::graphics::Shader::set_uniform ( const std::string & name,
const unsigned int & a,
const unsigned int & b,
const unsigned int & c )
inline

Definition at line 237 of file Shader.hpp.

+ Here is the call graph for this function:

◆ set_uniform() [11/19]

template<>
void galaxy::graphics::Shader::set_uniform ( const std::string & name,
const unsigned int & a,
const unsigned int & b,
const unsigned int & c,
const unsigned int & d )
inline

Definition at line 243 of file Shader.hpp.

+ Here is the call graph for this function:

◆ set_uniform() [12/19]

template<>
void galaxy::graphics::Shader::set_uniform ( const std::string & name,
const float & a )
inline

Definition at line 255 of file Shader.hpp.

+ Here is the call graph for this function:

◆ set_uniform() [13/19]

template<>
void galaxy::graphics::Shader::set_uniform ( const std::string & name,
const float & a,
const float & b )
inline

Definition at line 261 of file Shader.hpp.

+ Here is the call graph for this function:

◆ set_uniform() [14/19]

template<>
void galaxy::graphics::Shader::set_uniform ( const std::string & name,
const float & a,
const float & b,
const float & c )
inline

Definition at line 267 of file Shader.hpp.

+ Here is the call graph for this function:

◆ set_uniform() [15/19]

template<>
void galaxy::graphics::Shader::set_uniform ( const std::string & name,
const float & a,
const float & b,
const float & c,
const float & d )
inline

Definition at line 273 of file Shader.hpp.

+ Here is the call graph for this function:

◆ set_uniform() [16/19]

template<>
void galaxy::graphics::Shader::set_uniform ( const std::string & name,
const double & a )
inline

Definition at line 279 of file Shader.hpp.

+ Here is the call graph for this function:

◆ set_uniform() [17/19]

template<>
void galaxy::graphics::Shader::set_uniform ( const std::string & name,
const double & a,
const double & b )
inline

Definition at line 285 of file Shader.hpp.

+ Here is the call graph for this function:

◆ set_uniform() [18/19]

template<>
void galaxy::graphics::Shader::set_uniform ( const std::string & name,
const double & a,
const double & b,
const double & c )
inline

Definition at line 291 of file Shader.hpp.

+ Here is the call graph for this function:

◆ set_uniform() [19/19]

template<>
void galaxy::graphics::Shader::set_uniform ( const std::string & name,
const double & a,
const double & b,
const double & c,
const double & d )
inline

Definition at line 297 of file Shader.hpp.

+ Here is the call graph for this function:

Member Data Documentation

◆ m_id

unsigned int galaxy::graphics::Shader::m_id
private

OpenGL handle.

Definition at line 175 of file Shader.hpp.

◆ m_cache

ankerl::unordered_dense::map<std::string, int> galaxy::graphics::Shader::m_cache
private

Cache of uniforms for better performance.

Definition at line 180 of file Shader.hpp.

◆ m_src

entt::compressed_pair<std::string, std::string> galaxy::graphics::Shader::m_src
private

Shader source code.

Definition at line 185 of file Shader.hpp.


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