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_compile_options& shader_library::get_compile_options(const std::string& name) {
33 return get_shader_info(name).options;
34}
35
36bool shader_library::load(context& ctx, shader_program& prog, const std::string& name, const shader_compile_options& options, const std::string& where) {
37 if(prog.is_created())
38 prog.destruct(ctx);
39
40 const std::string function_context = where == "" ? "shader_library::load_shader()" : where;
41
42 if(!prog.is_created()) {
43 bool from_program_file = name.length() > 4 && name.substr(name.length() - 5) == ".glpr";
44
45 if(from_program_file) {
46 if(!prog.build_program(ctx, name, options, true)) {
47 std::cerr << "ERROR in " << function_context << " ... could not build shader program " << name << std::endl;
48 return false;
49 }
50 } else {
51 if(!prog.build_files(ctx, name, options, true)) {
52 std::cerr << "ERROR in " << function_context << " ... could not build shader files " << name << std::endl;
53 return false;
54 }
55 }
56 }
57 return true;
58}
59
60bool shader_library::load(context& ctx, shader_program& prog, const std::string& name, const std::string& where) {
61 return load(ctx, prog, name, {}, where);
62}
63
64bool shader_library::load_all(context& ctx, const std::string& where) {
65 bool success = true;
66 for(auto& entry : shaders) {
67 shader_info& info = entry.second;
68 success &= load(ctx, info.prog, info.filename, info.options, where);
69 }
70 return success;
71}
72
73bool shader_library::reload(context& ctx, const std::string& name, const shader_compile_options& options, const std::string& where) {
74 auto it = shaders.find(name);
75 if(it != shaders.end()) {
76 shader_info& info = (*it).second;
77 info.options = options;
78 return load(ctx, info.prog, info.filename, info.options, where);
79 }
80 return false;
81}
82
83shader_library::shader_info& shader_library::get_shader_info(const std::string& name) {
84 if(shaders.find(name) != shaders.end()) {
85 return shaders.at(name);
86 } else {
87 std::cerr << "Error: shader_library::get shader with name " << name << " not found!" << std::endl;
88 abort();
89 }
90}
91
92}
93}
the cgv namespace
Definition print.h:11