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;
32shader_compile_options& shader_library::get_compile_options(
const std::string& name) {
33 return get_shader_info(name).options;
36bool shader_library::load(context& ctx, shader_program& prog,
const std::string& name,
const shader_compile_options& options,
const std::string& where) {
40 const std::string function_context = where ==
"" ?
"shader_library::load_shader()" : where;
42 if(!prog.is_created()) {
43 bool from_program_file = name.length() > 4 && name.substr(name.length() - 5) ==
".glpr";
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;
51 if(!prog.build_files(ctx, name, options,
true)) {
52 std::cerr <<
"ERROR in " << function_context <<
" ... could not build shader files " << name << std::endl;
60bool shader_library::load(context& ctx, shader_program& prog,
const std::string& name,
const std::string& where) {
61 return load(ctx, prog, name, {}, where);
64bool shader_library::load_all(context& ctx,
const std::string& where) {
66 for(
auto& entry : shaders) {
67 shader_info& info = entry.second;
68 success &= load(ctx, info.prog, info.filename, info.options, where);
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);
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);
87 std::cerr <<
"Error: shader_library::get shader with name " << name <<
" not found!" << std::endl;