AnimEngine
ShaderLibrary.hpp
1 #pragma once
2 #ifndef SHADERLIBRARY_H_
3 #define SHADERLIBRARY_H_
4 
5 #include <unordered_map>
6 #include <vector>
7 #include <string>
8 #include "common.h"
9 #include "Program.h"
10 
11 using namespace std;
12 
14 
15 public:
16  static ShaderLibrary& getInstance(){
17  static ShaderLibrary instance;
18  return(instance);
19  }
20 
21  // Add a shader to the library by name and pointer
22  void add(const string &name, Program* program);
23  void add(const char* name, Program* program){add(string(name), program);}
24 
25  bool loadFromJSONCollection(string path, bool silent = false);
26 
27  bool buildAndAdd(string name, const json& program_obj);
28  bool buildAndAdd(const char* const name, const json& program_obj) {return(buildAndAdd(string(name), program_obj));}
29 
30  bool buildAndAdd(string name, istream &vertex, istream &fragment);
31  bool buildAndAdd(const char* const name, istream &vertex, istream &fragment) {return(buildAndAdd(string(name), vertex, fragment));}
32  bool buildAndAdd(string name, const string &vpath, const string &fpath);
33  bool buildAndAdd(const char* const name, const string &vpath, const string &fpath) {return(buildAndAdd(string(name), vpath, fpath));}
34 
35  void printLibary() const;
36 
37  // Bind the program stored under the given name and mark it as active
38  void makeActive(const string &name);
39  void makeActive(const char* name){makeActive(string(name));}
40 
41  // Automatically swaps to and binds on the given program pointer. This enables calls to be made to the
42  // library during rendering that skip the overhead of the hash-table whilst avoiding redundant bind calls.
43  void fastActivate(Program* prog);
44 
45  Program& getActive();
46 
47  // Should only be used in combination with fastActivate()! Do not use to bind manually!
48  Program* getActivePtr();
49 
50  Program* operator[](const string& name) {return(getPtr(name));}
51  Program* operator[](const char* name) {return(getPtr(string(name)));}
52 
53  // Same as getActivePtr but for any program in the library. If the given name is not found returns NULL
54  Program* getPtr(const string &name);
55  Program* getPtr(const char* name){return(getPtr(string(name)));}
56 
57  void dumpBinary(const string& name);
58  void dumpBinary(const char* name){dumpBinary(string(name));}
59 
60  ShaderLibrary(ShaderLibrary const&) = delete;
61  void operator=(ShaderLibrary const&) = delete;
62 private:
63  ShaderLibrary();
64 
65  // Hash-table associative array linking GLSL programs to a simple name such as "blinn-phong"
66  // Most pointers should point to an element of 'localPrograms', but don't need to.
67  unordered_map<string, Program*> programs;
68 
69  // Collection of Program objects scoped within the class for the sake of unambiguous memory management.
70  vector<Program> localPrograms;
71 
72  // Always initialized "error shader" that can be fallen back on if another shader is missing or fails
73  Program fallback;
74 
75  // Pointer to active shader which is currently bound in OpenGL
76  Program* active;
77 
78  // Hardcoded fallback shadercode
79  constexpr const static char* errorvs = "#version 330 core\n"
80  "layout(location = 0) in vec4 vertPos;\n"
81  "\n"
82  "uniform mat4 P;\n"
83  "uniform mat4 V;\n"
84  "uniform mat4 M;\n"
85  "\n"
86  "void main()\n"
87  "{\n"
88  " gl_Position = P * V * M * vertPos;\n"
89  "}\n"
90  ;
91 
92  constexpr const static char* errorfs = "#version 330 core\n"
93  "\n"
94  "out vec4 color;\n"
95  "\n"
96  "void main()\n"
97  "{\n"
98  " vec3 col = vec3(1.0,0.0,1.0);\n"
99  " color = vec4(col, 1.0);\n"
100  "}\n"
101  ;
102 
103 };
104 
105 #endif
Definition: Program.h:15
Definition: ShaderLibrary.hpp:13