Antkeeper  0.0.1
Public Types | Public Member Functions | List of all members
gl::shader_template Class Reference

Template used to for generating one or more shader variants from a single source. More...

#include <shader-template.hpp>

Public Types

using dictionary_type = std::unordered_map< std::string, std::string >
 Container of definitions used to generate #pragma define <key> <value> directives. More...
 

Public Member Functions

 shader_template (text_file &&source_code, std::vector< std::shared_ptr< text_file >> &&include_files)
 Constructs a shader template and sets its source code. More...
 
constexpr shader_template () noexcept=default
 Constructs an empty shader template. More...
 
std::string configure (gl::shader_stage stage, const dictionary_type &definitions={}) const
 Configures shader object source code for a given shader stage and template dictionary. More...
 
std::unique_ptr< gl::shader_objectcompile (gl::shader_stage stage, const dictionary_type &definitions={}) const
 Configures and compiles a shader object. More...
 
std::unique_ptr< gl::shader_programbuild (const dictionary_type &definitions={}) const
 Configures and compiles shader objects, then links them into a shader program. More...
 
bool has_vertex_directive () const noexcept
 Returns true if the template source contains one or more #pragma vertex directive. More...
 
bool has_fragment_directive () const noexcept
 Returns true if the template source contains one or more #pragma fragment directive. More...
 
bool has_geometry_directive () const noexcept
 Returns true if the template source contains one or more #pragma geometry directive. More...
 
bool has_define_directive (const std::string &key) const
 Returns true if the template source contains one or more instance of #pragma define <key>. More...
 
constexpr std::size_t hash () const noexcept
 Returns a hash of the template source code. More...
 
 shader_template (const text_file &source_code)
 Constructs a shader template and sets its source code. More...
 
 shader_template (text_file &&source_code)
 Constructs a shader template and sets its source code. More...
 
void source (const text_file &source_code)
 Replaces the source code of the shader template. More...
 
void source (text_file &&source_code)
 Replaces the source code of the shader template. More...
 

Detailed Description

Template used to for generating one or more shader variants from a single source.

Shader templates support the following preprocessor directives:

See also
gl::shader_stage
gl::shader_object
gl::shader_program

Definition at line 49 of file shader-template.hpp.

Member Typedef Documentation

◆ dictionary_type

using gl::shader_template::dictionary_type = std::unordered_map<std::string, std::string>

Container of definitions used to generate #pragma define <key> <value> directives.

Definition at line 53 of file shader-template.hpp.

Constructor & Destructor Documentation

◆ shader_template() [1/4]

gl::shader_template::shader_template ( const text_file source_code)
explicit

Constructs a shader template and sets its source code.

Parameters
source_codeShader template source code.

Definition at line 33 of file shader-template.cpp.

◆ shader_template() [2/4]

gl::shader_template::shader_template ( text_file &&  source_code)
explicit

Constructs a shader template and sets its source code.

Parameters
source_codeShader template source code.

Definition at line 40 of file shader-template.cpp.

◆ shader_template() [3/4]

gl::shader_template::shader_template ( text_file &&  source_code,
std::vector< std::shared_ptr< text_file >> &&  include_files 
)

Constructs a shader template and sets its source code.

Parameters
source_codeShader template source code.
include_filesShader template include files.
Note
This constuctor is used to keep the loaded include files cached.

Definition at line 47 of file shader-template.cpp.

◆ shader_template() [4/4]

constexpr gl::shader_template::shader_template ( )
constexprdefaultnoexcept

Constructs an empty shader template.

Member Function Documentation

◆ build()

std::unique_ptr< gl::shader_program > gl::shader_template::build ( const dictionary_type definitions = {}) const

Configures and compiles shader objects, then links them into a shader program.

Shader object stages are determined according to the presence of #pragma <stage> directives.

Parameters
definitionsContainer of definitions used to replace #pragma define <key> <value> directives.
Returns
Linked shader program.
Exceptions
std::runtime_errorAny exceptions thrown by gl::shader_object or gl::shader_program.
See also
has_vertex_directive() const
has_fragment_directive() const
has_geometry_directive() const

Definition at line 104 of file shader-template.cpp.

◆ compile()

std::unique_ptr< gl::shader_object > gl::shader_template::compile ( gl::shader_stage  stage,
const dictionary_type definitions = {} 
) const

Configures and compiles a shader object.

Parameters
stageShader stage of the shader object to generate. Instances of #pragma <stage> in the template source will be replaced with #define __<STAGE>__.
definitionsContainer of definitions used to replace #pragma define <key> <value> directives.
Returns
Compiled shader object.
Exceptions
std::runtime_errorAny exceptions thrown by gl::shader_object.

Definition at line 87 of file shader-template.cpp.

◆ configure()

std::string gl::shader_template::configure ( gl::shader_stage  stage,
const dictionary_type definitions = {} 
) const

Configures shader object source code for a given shader stage and template dictionary.

Parameters
stageShader stage of the shader object to generate. Instances of #pragma <stage> in the template source will be replaced with #define __<STAGE>__.
definitionsContainer of definitions used to replace #pragma define <key> <value> directives.
Returns
Configured shader object source code.

Definition at line 71 of file shader-template.cpp.

◆ has_define_directive()

bool gl::shader_template::has_define_directive ( const std::string &  key) const

Returns true if the template source contains one or more instance of #pragma define <key>.

Parameters
keyDefinition key.

Definition at line 245 of file shader-template.cpp.

◆ has_fragment_directive()

bool gl::shader_template::has_fragment_directive ( ) const
inlinenoexcept

Returns true if the template source contains one or more #pragma fragment directive.

Definition at line 134 of file shader-template.hpp.

◆ has_geometry_directive()

bool gl::shader_template::has_geometry_directive ( ) const
inlinenoexcept

Returns true if the template source contains one or more #pragma geometry directive.

Definition at line 140 of file shader-template.hpp.

◆ has_vertex_directive()

bool gl::shader_template::has_vertex_directive ( ) const
inlinenoexcept

Returns true if the template source contains one or more #pragma vertex directive.

Definition at line 128 of file shader-template.hpp.

◆ hash()

constexpr std::size_t gl::shader_template::hash ( ) const
inlineconstexprnoexcept

Returns a hash of the template source code.

Definition at line 153 of file shader-template.hpp.

◆ source() [1/2]

void gl::shader_template::source ( const text_file source_code)

Replaces the source code of the shader template.

Parameters
source_codeShader template source code.

Definition at line 55 of file shader-template.cpp.

◆ source() [2/2]

void gl::shader_template::source ( text_file &&  source_code)

Replaces the source code of the shader template.

Parameters
source_codeShader template source code.

Definition at line 63 of file shader-template.cpp.


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