coeng

view src/shader.h @ 8:8cce82794f90

seems to work nicely
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 15 Feb 2015 05:14:20 +0200
parents
children
line source
1 #ifndef SHADER_H_
2 #define SHADER_H_
4 #include <vector>
5 #include <string>
6 #include "vmath/vmath.h"
7 #include "opengl.h"
9 class ShaderProg;
11 void bind_shader(const ShaderProg *sdr);
12 const ShaderProg *get_current_shader();
15 class Shader {
16 private:
17 unsigned int sdr;
18 unsigned int type;
19 std::string name, src;
21 public:
22 Shader();
23 ~Shader();
25 unsigned int get_id() const;
27 void set_name(const char *name);
28 const char *get_name() const;
30 bool create(const char *src, unsigned int type);
31 void destroy();
33 bool load(const char *fname, unsigned int type);
36 // these functions are only meant to be used by the ShaderSet
37 static Shader *create_shader();
38 static bool load_shader(Shader *sdr, const char *fname);
39 static bool done_shader(Shader *sdr, unsigned int type);
40 static void destroy_shader(Shader *sdr);
41 };
43 #define VSDR(s) s, GL_VERTEX_SHADER
44 #define FSDR(s) s, GL_FRAGMENT_SHADER
45 #define PSDR(s) FSDR(s)
46 #define GSDR(s) s, GL_GEOMETRY_SHADER
47 #define TCSDR(s) s, GL_TESS_CONTROL_SHADER
48 #define TESDR(s) s, GL_TESS_EVALUATION_SHADER
50 class ShaderProg {
51 private:
52 unsigned int prog;
53 mutable bool must_link;
54 std::vector<Shader*> shaders;
56 struct StateLocCache { int sidx, loc; };
57 /** a cache of all st_ prefixed uniform locations and their corresponding
58 * index in the global uniform state vector (see unistate.h)
59 */
60 mutable std::vector<StateLocCache> stloc_cache;
62 void cache_state_uniforms() const;
63 void setup_state_uniforms() const;
65 public:
66 static ShaderProg *current;
68 ShaderProg();
69 ~ShaderProg();
71 /// returns the OpenGL object id for this shader program
72 unsigned int get_id() const;
74 /** takes a series of shaders, and constructs a program object by linking
75 * them together. Terminate with a null pointer (don't use 0!) */
76 bool create(Shader *sdr, ...);
77 /// same as above, but with a va_list instead of variable arguments.
78 bool create(Shader *sdr, va_list ap);
79 /** takes two shaders (vertex and pixel) and constructs a program object by
80 * linking them together. Either one can be null. */
81 bool create(Shader *vsdr, Shader *psdr);
83 /** takes a series of shader source/shader type pairs and constructs a program
84 * object by linking them together. Terminate with a null pointer (don't use 0!)
85 * You can use the VSDR, PSDR, GSDR, TCSDR, TESDR convenience macros for passing
86 * the pairs.
87 * Example: create(VSDR(vsrc0), VSDR(vsrc1), PSDR(psrc), NULL);
88 */
89 bool create(const char *src, unsigned int type, ...);
90 /// same as above, but with a va_list instead of variable arguments.
91 bool create(const char *src, unsigned int type, va_list ap);
92 /** takes two shaders source strings (vertex and pixel) and constructs
93 * a program object by linking them together. Either one can be null. */
94 bool create(const char *vsrc, const char *psrc);
96 void destroy();
98 /** takes a series of shader filename/shader type pairs, loads the shaders and
99 * constructs a program object by linking them together. Terminate with a null
100 * pointer (don't use 0!). You can use the VSDR, PSDR, GSDR, TCSDR, TESDR convenience
101 * macros for passing the pairs.
102 * Example: load(VSDR("vsdr1.glsl"), VSDR("vsdr2.glsl"), PSDR("pixel.glsl"), NULL);
103 */
104 bool load(const char *fname, unsigned int type, ...);
105 /// same as above, but with a va_list instead of variable arguments.
106 bool load(const char *fname, unsigned int type, va_list ap);
107 /** takes the filenames of two shader files (vertex and pixel), loads them and
108 * constructs a program object by linking them together. Either one can be null */
109 bool load(const char *vsrc, const char *psrc);
111 void add_shader(Shader *sdr);
112 bool link() const;
114 void bind() const;
116 int get_attrib_location(const char *name) const;
117 void set_attrib_location(const char *name, int loc) const;
119 int get_uniform_location(const char *name) const;
121 bool set_uniform(int loc, int val) const;
122 bool set_uniform(int loc, float val) const;
123 bool set_uniform(int loc, const Vector2 &v) const;
124 bool set_uniform(int loc, const Vector3 &v) const;
125 bool set_uniform(int loc, const Vector4 &v) const;
126 bool set_uniform(int loc, const Matrix3x3 &m) const;
127 bool set_uniform(int loc, const Matrix4x4 &m) const;
129 bool set_uniform(const char *name, int val) const;
130 bool set_uniform(const char *name, float val) const;
131 bool set_uniform(const char *name, const Vector2 &v) const;
132 bool set_uniform(const char *name, const Vector3 &v) const;
133 bool set_uniform(const char *name, const Vector4 &v) const;
134 bool set_uniform(const char *name, const Matrix3x3 &m) const;
135 bool set_uniform(const char *name, const Matrix4x4 &m) const;
137 friend void setup_unistate(const ShaderProg*);
138 };
140 #endif // SHADER_H_