vrshoot

view src/shader.h @ 0:b2f14e535253

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