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

a shader code object holds a code fragment of a geometry vertex or fragment shader and can be added to a shader program. More...

#include <shader_code.h>

Inheritance diagram for cgv::render::shader_code:
cgv::render::render_component

Public Member Functions

 shader_code ()
 create shader a shader code object
 
 ~shader_code ()
 calls the destruct method
 
void destruct (const context &ctx)
 destruct shader code
 
bool read_code (const context &ctx, const std::string &file_name, ShaderType st=ST_DETECT, const shader_compile_options &options={})
 read shader code from file that is searched for with find_file.
 
bool set_code (const context &ctx, const std::string &source, ShaderType st)
 set shader code from string
 
ShaderType get_shader_type () const
 return the shader type of this code
 
bool compile (const context &ctx)
 compile attached source; returns true if successful
 
bool read_and_compile (const context &ctx, const std::string &file_name, ShaderType st=ST_DETECT, const shader_compile_options &options={}, bool show_error=true)
 read shader code with read_code and compile.
 
bool is_compiled () const
 return whether shader has been compiled successfully
 
- Public Member Functions inherited from cgv::render::render_component
 render_component ()
 initialize members
 
virtual bool is_created () const
 return whether component has been created
 
void put_id_void (void *ptr) const
 copy the rendering api specific id the component to the memory location of the given pointer.
 
template<typename T >
void put_id (T &id) const
 cast the refence to rendering api specific representation of component id to the specified type
 

Static Public Member Functions

template<typename T , typename std::enable_if<!std::is_enum< T >::value, bool >::type = true>
static void set_define (shader_define_map &defines, const std::string &name, const T &value, const T &default_value)
 
template<typename T , typename std::enable_if< std::is_enum< T >::value, bool >::type = true>
static void set_define (shader_define_map &defines, const std::string &name, const T &value, const T &default_value)
 
static void set_define (shader_define_map &defines, const std::string &name, const std::string &value, const std::string &default_value)
 
static void set_define (shader_define_map &defines, const std::string &name, bool value, bool default_value)
 
static void decode_if_base64 (std::string &content)
 decode a string if it is base64 encoded
 
static std::string find_file (const std::string &file_name, bool search_exhaustive=false)
 Find the full path to a shader by its file name.
 
static std::string get_last_error (const std::string &file_name, const std::string &last_error)
 format given last error in a way that developer environments can locate errors in the source file
 
static std::string read_code_file (const std::string &file_name, std::string *_last_error=0)
 read shader code from file and return string with content or empty string if read failed
 
static std::string retrieve_code (const std::string &file_name, bool use_cache, std::string *_last_error)
 retreive shader code either by reading the file from disk or from the cache if enabled
 
static ShaderType detect_shader_type (const std::string &file_name)
 detect the shader type from the extension of the given file_name, i.e.
 

Protected Attributes

ShaderType st
 store the shader type
 

Static Protected Attributes

static std::map< std::string, std::string > shader_file_name_map
 map that caches full shader file paths indexed by the shader file name
 
static bool shader_file_name_map_initialized = false
 whether the shader file name map is initialized
 
static std::map< std::string, std::string > code_cache
 map that caches shader file contents indexed by their file name
 

Additional Inherited Members

- Public Attributes inherited from cgv::render::render_component
void * handle
 
void * internal_format
 
void * user_data
 
const contextctx_ptr
 keep pointer to my context
 
std::string last_error
 a string that contains the last error
 

Detailed Description

a shader code object holds a code fragment of a geometry vertex or fragment shader and can be added to a shader program.

Definition at line 103 of file shader_code.h.

Constructor & Destructor Documentation

◆ shader_code()

cgv::render::shader_code::shader_code ( )

create shader a shader code object

Definition at line 157 of file shader_code.cxx.

References st.

◆ ~shader_code()

cgv::render::shader_code::~shader_code ( )

calls the destruct method

Definition at line 162 of file shader_code.cxx.

References cgv::render::render_component::ctx_ptr, and destruct().

Member Function Documentation

◆ compile()

bool cgv::render::shader_code::compile ( const context ctx)

compile attached source; returns true if successful

Definition at line 881 of file shader_code.cxx.

Referenced by cgv::render::shader_program::attach_code(), and read_and_compile().

◆ decode_if_base64()

void cgv::render::shader_code::decode_if_base64 ( std::string &  content)
static

decode a string if it is base64 encoded

Definition at line 69 of file shader_code.cxx.

References cgv::utils::decode_base64().

Referenced by cgv::render::shader_program::collect_program(), cgv::render::shader_program::open_program_file(), and read_code_file().

◆ destruct()

void cgv::render::shader_code::destruct ( const context ctx)

destruct shader code

destruct shader

Definition at line 172 of file shader_code.cxx.

Referenced by set_code(), and ~shader_code().

◆ detect_shader_type()

ShaderType cgv::render::shader_code::detect_shader_type ( const std::string &  file_name)
static

detect the shader type from the extension of the given file_name, i.e.

  • glvs ... ST_VERTEX
  • glgs ... ST_GEOMETRY
  • glfs ... ST_FRAGMENT
  • glcs ... ST_COMPUTE

Definition at line 279 of file shader_code.cxx.

References st, and cgv::utils::to_lower().

Referenced by read_code().

◆ find_file()

std::string cgv::render::shader_code::find_file ( const std::string &  file_name,
bool  search_exhaustive = false 
)
static

Find the full path to a shader by its file name.

Check if the file exists. If not, check if a resource file of this file_name has been registered. If not, search it in the shader_file_name_map that caches paths for file names based on a recursive search through the paths given in the shader_path of the shader_config. This avoids excessive queries to the file system. The shader_path is accessed by the get_shader_config() method. This path is initialized to the environment variable CGV_SHADER_PATH or empty if that is not defined. If search_exhaustive is true and the file has not been found yet, search recursively in the shader_path.

Parameters
file_nameThe shader file_name to search.
search_exhaustiveIf true, a full recursive search through the given shader_path will be performed if all other attempts have failed.
Returns
The full path of the shader file_name.

Definition at line 180 of file shader_code.cxx.

References cgv::data::ref_ptr< T, is_ref_counted >::empty(), cgv::base::action::end(), cgv::render::get_shader_config(), cgv::base::ref_prog_name(), cgv::base::ref_resource_file_map(), shader_file_name_map, and shader_file_name_map_initialized.

Referenced by cgv::render::shader_program::build_program(), cgv::render::shader_program::collect_file(), cgv::render::shader_program::collect_files(), cgv::render::shader_program::collect_program(), get_last_error(), cgv::render::shader_program::open_program_file(), and read_code_file().

◆ get_last_error()

std::string cgv::render::shader_code::get_last_error ( const std::string &  file_name,
const std::string &  last_error 
)
static

format given last error in a way that developer environments can locate errors in the source file

query the last error in a way that developer environments can locate errors in the source file

Definition at line 93 of file shader_code.cxx.

References find_file(), cgv::render::get_shader_config(), cgv::utils::is_integer(), cgv::render::render_component::last_error, cgv::utils::tokenizer::set_sep(), cgv::utils::tokenizer::set_ws(), cgv::utils::split_to_lines(), cgv::utils::to_string(), and cgv::utils::to_upper().

Referenced by read_and_compile().

◆ get_shader_type()

ShaderType cgv::render::shader_code::get_shader_type ( ) const

return the shader type of this code

Definition at line 443 of file shader_code.cxx.

References st.

Referenced by cgv::render::shader_program::attach_code(), and cgv::render::shader_program::detach_code().

◆ is_compiled()

bool cgv::render::shader_code::is_compiled ( ) const

return whether shader has been compiled successfully

Definition at line 908 of file shader_code.cxx.

Referenced by cgv::render::shader_program::attach_code().

◆ read_and_compile()

bool cgv::render::shader_code::read_and_compile ( const context ctx,
const std::string &  file_name,
ShaderType  st = ST_DETECT,
const shader_compile_options options = {},
bool  show_error = true 
)

read shader code with read_code and compile.

read shader code from file, compile and print error message if necessary

If show_error is true print error messages formated with the get_last_error method in case an error arose.

Definition at line 893 of file shader_code.cxx.

References compile(), get_last_error(), cgv::render::render_component::last_error, read_code(), and st.

Referenced by cgv::render::shader_program::attach_file().

◆ read_code()

bool cgv::render::shader_code::read_code ( const context ctx,
const std::string &  file_name,
ShaderType  st = ST_DETECT,
const shader_compile_options options = {} 
)

read shader code from file that is searched for with find_file.

read shader code from file

If the shader type defaults to ST_DETECT, the detect_shader_type() method is applied to the file name.

Definition at line 492 of file shader_code.cxx.

References detect_shader_type(), cgv::render::context::get_gpu_vendor_id(), cgv::render::context::is_shader_file_cache_enabled(), cgv::render::render_component::last_error, retrieve_code(), set_code(), and st.

Referenced by read_and_compile().

◆ read_code_file()

std::string cgv::render::shader_code::read_code_file ( const std::string &  file_name,
std::string *  _last_error = 0 
)
static

read shader code from file and return string with content or empty string if read failed

Definition at line 449 of file shader_code.cxx.

References decode_if_base64(), cgv::data::ref_ptr< T, is_ref_counted >::empty(), find_file(), cgv::render::get_shader_config(), cgv::render::render_component::last_error, and cgv::base::read_data_file().

Referenced by retrieve_code().

◆ retrieve_code()

std::string cgv::render::shader_code::retrieve_code ( const std::string &  file_name,
bool  use_cache,
std::string *  _last_error 
)
static

retreive shader code either by reading the file from disk or from the cache if enabled

Definition at line 257 of file shader_code.cxx.

References code_cache, cgv::base::action::end(), and read_code_file().

Referenced by read_code().

◆ set_code()

bool cgv::render::shader_code::set_code ( const context ctx,
const std::string &  source,
ShaderType  st 
)

set shader code from string

Definition at line 516 of file shader_code.cxx.

References cgv::render::render_component::ctx_ptr, destruct(), and st.

Referenced by cgv::render::shader_program::attach_code(), and read_code().

◆ set_define() [1/4]

static void cgv::render::shader_code::set_define ( shader_define_map defines,
const std::string &  name,
bool  value,
bool  default_value 
)
inlinestatic

Definition at line 129 of file shader_code.h.

◆ set_define() [2/4]

static void cgv::render::shader_code::set_define ( shader_define_map defines,
const std::string &  name,
const std::string &  value,
const std::string &  default_value 
)
inlinestatic

Definition at line 122 of file shader_code.h.

◆ set_define() [3/4]

template<typename T , typename std::enable_if<!std::is_enum< T >::value, bool >::type = true>
static void cgv::render::shader_code::set_define ( shader_define_map defines,
const std::string &  name,
const T &  value,
const T &  default_value 
)
inlinestatic

Definition at line 107 of file shader_code.h.

◆ set_define() [4/4]

template<typename T , typename std::enable_if< std::is_enum< T >::value, bool >::type = true>
static void cgv::render::shader_code::set_define ( shader_define_map defines,
const std::string &  name,
const T &  value,
const T &  default_value 
)
inlinestatic

Definition at line 115 of file shader_code.h.

Member Data Documentation

◆ code_cache

std::map< std::string, std::string > cgv::render::shader_code::code_cache
staticprotected

map that caches shader file contents indexed by their file name

Definition at line 142 of file shader_code.h.

Referenced by retrieve_code().

◆ shader_file_name_map

std::map< std::string, std::string > cgv::render::shader_code::shader_file_name_map
staticprotected

map that caches full shader file paths indexed by the shader file name

Definition at line 138 of file shader_code.h.

Referenced by find_file().

◆ shader_file_name_map_initialized

bool cgv::render::shader_code::shader_file_name_map_initialized = false
staticprotected

whether the shader file name map is initialized

Definition at line 140 of file shader_code.h.

Referenced by find_file().

◆ st

ShaderType cgv::render::shader_code::st
protected

store the shader type

Definition at line 145 of file shader_code.h.

Referenced by detect_shader_type(), get_shader_type(), read_and_compile(), read_code(), set_code(), and shader_code().


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