cgv
Loading...
Searching...
No Matches
shader_library.cxx
1#include "shader_library.h"
2
3namespace cgv {
4namespace render {
5
6void shader_library::clear(context& ctx) {
7 for(auto& entry : shaders)
8 entry.second.prog.destruct(ctx);
9
10 shaders.clear();
11}
12
13bool shader_library::add(const std::string& name, const std::string& file, const shader_compile_options& options) {
14 if(shaders.find(name) == shaders.end()) {
15 shader_info info;
16 info.filename = file;
17 info.options = options;
18 shaders.insert({ name, info });
19 return true;
20 }
21 return false;
22}
23
24bool shader_library::contains(const std::string& name) const {
25 return shaders.find(name) != shaders.end();
26}
27
28shader_program& shader_library::get(const std::string& name) {
29 return get_shader_info(name).prog;
30}
31
32shader_define_map& shader_library::get_defines(const std::string& name) {
33 return get_shader_info(name).options.defines;
34}
35
36shader_compile_options& shader_library::get_compile_options(const std::string& name) {
37 return get_shader_info(name).options;
38}
39
40bool shader_library::load(context& ctx, shader_program& prog, const std::string& name, const shader_compile_options& options, const std::string& where) {
41 if(prog.is_created())
42 prog.destruct(ctx);
43
44 const std::string function_context = where == "" ? "shader_library::load_shader()" : where;
45
46 if(!prog.is_created()) {
47 bool from_program_file = name.length() > 4 && name.substr(name.length() - 5) == ".glpr";
48
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;
52 return false;
53 }
54 } else {
55 if(!prog.build_files(ctx, name, options, true)) {
56 std::cerr << "ERROR in " << function_context << " ... could not build shader files " << name << std::endl;
57 return false;
58 }
59 }
60 }
61 return true;
62}
63
64bool shader_library::load(context& ctx, shader_program& prog, const std::string& name, const std::string& where) {
65 return load(ctx, prog, name, {}, where);
66}
67
68bool shader_library::load_all(context& ctx, const std::string& where) {
69 bool success = true;
70 for(auto& entry : shaders) {
71 shader_info& info = entry.second;
72 success &= load(ctx, info.prog, info.filename, info.options, where);
73 }
74 return success;
75}
76
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);
83 }
84 return false;
85}
86
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);
90 } else {
91 std::cerr << "Error: shader_library::get shader with name " << name << " not found!" << std::endl;
92 abort();
93 }
94}
95
96}
97}
std::map< std::string, std::string > shader_define_map
typedef for shader define map data structure
Definition shader_code.h:52
the cgv namespace
Definition print.h:11