cgv
Loading...
Searching...
No Matches
shader_library.h
1#pragma once
2
3#include "context.h"
4#include "shader_program.h"
5
6#include "lib_begin.h"
7
8namespace cgv {
9namespace render {
10
12class CGV_API shader_library {
13protected:
14 struct shader_info {
15 std::string filename = "";
16 shader_program prog;
17 shader_define_map defines = {};
18 };
19
20 typedef std::map<std::string, shader_info> shader_lib_map;
21 shader_lib_map shaders;
22
23 shader_info& get_shader_info(const std::string& name) {
24
25 if(shaders.find(name) != shaders.end()) {
26 return shaders.at(name);
27 } else {
28 std::cerr << "Error: shader_library::get shader with name " << name << " not found!" << std::endl;
29 abort();
30 }
31 }
32
33public:
34 shader_library();
35 ~shader_library();
36
37 void clear(context& ctx);
38
39 bool add(const std::string& name, const std::string& file, const shader_define_map& defines = {});
40
41 shader_program& get(const std::string& name) {
42
43 return get_shader_info(name).prog;
44 }
45
46 shader_define_map& get_defines(const std::string& name) {
47
48 return get_shader_info(name).defines;
49 }
50
51 bool contains(const std::string& name) const {
52
53 return shaders.find(name) != shaders.end();
54 }
55
56 shader_lib_map::iterator begin() { return shaders.begin(); }
57 shader_lib_map::iterator end() { return shaders.end(); }
58
59 bool load_all(context& ctx, const std::string& where = "");
60
61 bool reload(context& ctx, const std::string& name, const shader_define_map& defines = {}, const std::string& where = "");
62
63 static bool load(context& ctx, shader_program& prog, const std::string& name, const bool reload = false, const std::string& where = "") {
64
65 return load(ctx, prog, name, shader_define_map(), reload, where);
66 }
67
68 static bool load(context& ctx, shader_program& prog, const std::string& name, const shader_define_map& defines, const bool reload = false, const std::string& where = "") {
69
70 if(prog.is_created()) {
71 if(reload) prog.destruct(ctx);
72 else return true;
73 }
74
75 std::string function_context = where == "" ? "shader_library::load_shader()" : where;
76
77 if(!prog.is_created()) {
78 bool from_program_file = name.length() > 4 && name.substr(name.length() - 5) == ".glpr";
79
80 if(from_program_file) {
81 if(!prog.build_program(ctx, name, true, defines)) {
82 std::cerr << "ERROR in " << function_context << " ... could not build shader program " << name << std::endl;
83 return false;
84 }
85 } else {
86 if(!prog.build_files(ctx, name, true, defines)) {
87 std::cerr << "ERROR in " << function_context << " ... could not build shader files " << name << std::endl;
88 return false;
89 }
90 }
91 }
92 return true;
93 }
94
95 bool reload_all(context& ctx, const std::string& where = "");
96};
97}
98}
99
100#include <cgv/config/lib_end.h>
provides a shader library that handles shader loading
a shader program combines several shader code fragments to a complete definition of the shading pipel...
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