1#include "shader_library.h"
6void shader_library::clear(context& ctx) {
7 for(
auto& entry : shaders)
8 entry.second.prog.destruct(ctx);
13bool shader_library::add(
const std::string& name,
const std::string& file,
const shader_compile_options& options) {
14 if(shaders.find(name) == shaders.end()) {
17 info.options = options;
18 shaders.insert({ name, info });
24bool shader_library::contains(
const std::string& name)
const {
25 return shaders.find(name) != shaders.end();
28shader_program& shader_library::get(
const std::string& name) {
29 return get_shader_info(name).prog;
33 return get_shader_info(name).options.defines;
36shader_compile_options& shader_library::get_compile_options(
const std::string& name) {
37 return get_shader_info(name).options;
40bool shader_library::load(context& ctx, shader_program& prog,
const std::string& name,
const shader_compile_options& options,
const std::string& where) {
44 const std::string function_context = where ==
"" ?
"shader_library::load_shader()" : where;
46 if(!prog.is_created()) {
47 bool from_program_file = name.length() > 4 && name.substr(name.length() - 5) ==
".glpr";
49 if(from_program_file) {
50 if(!prog.build_program(ctx, name, options,
true)) {
51 std::cerr <<
"ERROR in " << function_context <<
" ... could not build shader program " << name << std::endl;
55 if(!prog.build_files(ctx, name, options,
true)) {
56 std::cerr <<
"ERROR in " << function_context <<
" ... could not build shader files " << name << std::endl;
64bool shader_library::load(context& ctx, shader_program& prog,
const std::string& name,
const std::string& where) {
65 return load(ctx, prog, name, {}, where);
68bool shader_library::load_all(context& ctx,
const std::string& where) {
70 for(
auto& entry : shaders) {
71 shader_info& info = entry.second;
72 success &= load(ctx, info.prog, info.filename, info.options, where);
77bool shader_library::reload(context& ctx,
const std::string& name,
const shader_compile_options& options,
const std::string& where) {
78 auto it = shaders.find(name);
79 if(it != shaders.end()) {
80 shader_info& info = (*it).second;
81 info.options = options;
82 return load(ctx, info.prog, info.filename, info.options, where);
87shader_library::shader_info& shader_library::get_shader_info(
const std::string& name) {
88 if(shaders.find(name) != shaders.end()) {
89 return shaders.at(name);
91 std::cerr <<
"Error: shader_library::get shader with name " << name <<
" not found!" << std::endl;
std::map< std::string, std::string > shader_define_map
typedef for shader define map data structure