cgv
Loading...
Searching...
No Matches
cgv::render::shader_compile_options Class Reference

Stores preprocessor options used for conditionally compiling shader programs. More...

#include <shader_code.h>

Public Types

using string_map = std::map< std::string, std::string >
 

Public Member Functions

bool empty () const
 Return true if no options are set.
 
const string_map & get_macros () const
 Return const reference to defined macros.
 
const string_map & get_snippets () const
 Return const reference to defined snippets.
 
void define_macro (const std::string &identifier)
 Define a macro as identifier and no replacement text.
 
template<typename T , typename std::enable_if< std::is_arithmetic_v< T >, bool >::type = true>
void define_macro (const std::string &identifier, const T &value)
 Define identifier as a macro with value as replacement text.
 
template<typename T , typename std::enable_if< std::is_enum_v< T >, bool >::type = true>
void define_macro (const std::string &identifier, const T &value)
 
void define_macro (const std::string &identifier, bool value)
 
void define_macro (const std::string &identifier, const std::string &value)
 
template<typename T , typename std::enable_if< std::is_arithmetic_v< T >, bool >::type = true>
void define_macro_if_not_default (const std::string &identifier, const T &value, const T &default_value)
 Conditionally define identifier as a macro with value as replacement text.
 
template<typename T , typename std::enable_if< std::is_enum_v< T >, bool >::type = true>
void define_macro_if_not_default (const std::string &identifier, const T &value, const T &default_value)
 
void define_macro_if_not_default (const std::string &identifier, bool value, bool default_value)
 
void define_macro_if_not_default (const std::string &identifier, const std::string &value, const std::string &default_value)
 
void define_macro_if_true (bool predicate, const std::string &identifier)
 Conditionally define identifier as a macro with replacement text 1.
 
void undefine_macro (const std::string &identifier)
 Remove (undefine) the macro with the given identifier.
 
void define_snippet (const std::string &identifier, const std::string &replacement_text)
 Define a text replacement snippet.
 
void undefine_snippet (const std::string &identifier)
 Remove (undefine) the snippet with the given identifier.
 
void extend (const shader_compile_options &other, bool overwrite)
 Extend options by content of other shader_compile_options via merging.
 
bool operator== (const shader_compile_options &other) const
 Compare two shader_compile_options for equality.
 
bool operator!= (const shader_compile_options &other) const
 Compare two shader_compile_options for inequality.
 

Detailed Description

Stores preprocessor options used for conditionally compiling shader programs.

Options include standard preprocessor defines and text replacement snippets.

Preprocessor macros will be handled as follows:

  • Existing macros in the source file that are defined in this class will have their values replaced.
  • Existing macros in the source file that are not defined in this class will be left untouched.
  • Macros not present in the source file but defined in this class will be added to the source before compilation.

Snippets will be handled as follows:

  • Snippets are text replacements similar to macros but offer more flexibility. A snippet replacement text supports multi-line strings without the need to escape newlines.
  • Snippet replacement locations in shader code are marked using special comments of form: //$cgv::<identifier> where <identifier> is a user-defined name.
  • The comment marking a snippet location is replaced with the content of a snippet whose id matches the given id.
  • If at least one snippet is given, the additional define <CGV_USE_SNIPPETS> is set internally before compilation. To prevent ill-formed shader code due to potential missing definitions before snippet replacement, i.e.before pre-processing, affected parts of code can be disabled by enclosing them in a define guard like so: #ifdef CGV_USE_SNIPPETS ...code relying on snippet content #endif

Definition at line 73 of file shader_code.h.

Member Typedef Documentation

◆ string_map

using cgv::render::shader_compile_options::string_map = std::map<std::string, std::string>

Definition at line 75 of file shader_code.h.

Member Function Documentation

◆ define_macro() [1/5]

void cgv::render::shader_compile_options::define_macro ( const std::string &  identifier)
inline

Define a macro as identifier and no replacement text.

Definition at line 93 of file shader_code.h.

Referenced by define_macro_if_true(), cgv::render::shader_program::extract_instances(), and cgv::plot::plot2d::init().

◆ define_macro() [2/5]

void cgv::render::shader_compile_options::define_macro ( const std::string &  identifier,
bool  value 
)
inline

Definition at line 118 of file shader_code.h.

◆ define_macro() [3/5]

void cgv::render::shader_compile_options::define_macro ( const std::string &  identifier,
const std::string &  value 
)
inline

Definition at line 122 of file shader_code.h.

◆ define_macro() [4/5]

template<typename T , typename std::enable_if< std::is_arithmetic_v< T >, bool >::type = true>
void cgv::render::shader_compile_options::define_macro ( const std::string &  identifier,
const T &  value 
)
inline

Define identifier as a macro with value as replacement text.

If value is of arithmetic type, it is converted to a string. If value is of boolean type, it is converted to 1 if true and 0 if false. If value is of enum type, it is first converted into its underlying arithmetic type.

An existing macro with the same identifier is overwritten with the new value.

Template Parameters
TThe value type.
Parameters
identifierThe macro name.
valueThe macro replacement value.

Definition at line 109 of file shader_code.h.

◆ define_macro() [5/5]

template<typename T , typename std::enable_if< std::is_enum_v< T >, bool >::type = true>
void cgv::render::shader_compile_options::define_macro ( const std::string &  identifier,
const T &  value 
)
inline

Definition at line 114 of file shader_code.h.

◆ define_macro_if_not_default() [1/4]

void cgv::render::shader_compile_options::define_macro_if_not_default ( const std::string &  identifier,
bool  value,
bool  default_value 
)
inline

Definition at line 157 of file shader_code.h.

◆ define_macro_if_not_default() [2/4]

void cgv::render::shader_compile_options::define_macro_if_not_default ( const std::string &  identifier,
const std::string &  value,
const std::string &  default_value 
)
inline

Definition at line 164 of file shader_code.h.

◆ define_macro_if_not_default() [3/4]

template<typename T , typename std::enable_if< std::is_arithmetic_v< T >, bool >::type = true>
void cgv::render::shader_compile_options::define_macro_if_not_default ( const std::string &  identifier,
const T &  value,
const T &  default_value 
)
inline

Conditionally define identifier as a macro with value as replacement text.

The macro is only defined if value is not equal to default_value (tested using operator !=). If value is equal to default_value and the macro is already defined it will be removed (undefined).

If value is of arithmetic type, it is converted to a string. If value is of boolean type, it is converted to 1 if true and 0 if false. If value is of enum type, it is first converted into its underlying arithmetic type.

An existing macro with the same identifier is overwritten with the new value.

Template Parameters
Tthe value type.
Parameters
identifierThe macro name.
valueThe macro replacement value.
defaultThe default value used for comparison.

Definition at line 142 of file shader_code.h.

Referenced by cgv::render::volume_renderer::update_shader_program_options().

◆ define_macro_if_not_default() [4/4]

template<typename T , typename std::enable_if< std::is_enum_v< T >, bool >::type = true>
void cgv::render::shader_compile_options::define_macro_if_not_default ( const std::string &  identifier,
const T &  value,
const T &  default_value 
)
inline

Definition at line 150 of file shader_code.h.

◆ define_macro_if_true()

void cgv::render::shader_compile_options::define_macro_if_true ( bool  predicate,
const std::string &  identifier 
)
inline

Conditionally define identifier as a macro with replacement text 1.

Will only define the macro if predicate is true. If predicate is false and the macro is already defined it will be removed (undefined).

Parameters
predicateThe condition.
identifierThe macro name.

Definition at line 178 of file shader_code.h.

References define_macro(), and undefine_macro().

Referenced by cgv::render::box_renderer::update_shader_program_options(), cgv::render::cone_renderer::update_shader_program_options(), and cgv::render::volume_renderer::update_shader_program_options().

◆ define_snippet()

void cgv::render::shader_compile_options::define_snippet ( const std::string &  identifier,
const std::string &  replacement_text 
)
inline

Define a text replacement snippet.

Parameters
identifierThe snippet name.
replacement_textThe snippet content.

Definition at line 194 of file shader_code.h.

◆ empty()

bool cgv::render::shader_compile_options::empty ( ) const
inline

Return true if no options are set.

Definition at line 78 of file shader_code.h.

◆ extend()

void cgv::render::shader_compile_options::extend ( const shader_compile_options other,
bool  overwrite 
)
inline

Extend options by content of other shader_compile_options via merging.

If overwrite is true, existing defines and snippets are overwritten with the content from other.

Parameters
otherThe other options.
overwriteIf true, existing options are overwritten with other.

Definition at line 210 of file shader_code.h.

◆ get_macros()

const string_map & cgv::render::shader_compile_options::get_macros ( ) const
inline

Return const reference to defined macros.

Definition at line 83 of file shader_code.h.

◆ get_snippets()

const string_map & cgv::render::shader_compile_options::get_snippets ( ) const
inline

Return const reference to defined snippets.

Definition at line 88 of file shader_code.h.

◆ operator!=()

bool cgv::render::shader_compile_options::operator!= ( const shader_compile_options other) const
inline

Compare two shader_compile_options for inequality.

Definition at line 229 of file shader_code.h.

◆ operator==()

bool cgv::render::shader_compile_options::operator== ( const shader_compile_options other) const
inline

Compare two shader_compile_options for equality.

Definition at line 224 of file shader_code.h.

◆ undefine_macro()

void cgv::render::shader_compile_options::undefine_macro ( const std::string &  identifier)
inline

Remove (undefine) the macro with the given identifier.

Parameters
identifierThe macro name.

Definition at line 187 of file shader_code.h.

Referenced by define_macro_if_true().

◆ undefine_snippet()

void cgv::render::shader_compile_options::undefine_snippet ( const std::string &  identifier)
inline

Remove (undefine) the snippet with the given identifier.

Parameters
identifierThe snippet name.

Definition at line 200 of file shader_code.h.


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