goat3dgfx

view src/shader.h @ 34:3eb6c8f89fe1

merge
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Mar 2014 17:41:10 +0200
parents 6f82b9b6d6c3
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"
8 #include "dataset.h"
10 namespace goatgfx {
12 class ShaderProg;
15 void bind_shader(const ShaderProg *sdr);
16 const ShaderProg *get_current_shader();
19 class Shader {
20 private:
21 unsigned int sdr;
22 unsigned int type;
23 std::string name, src;
25 public:
26 Shader();
27 ~Shader();
29 unsigned int get_id() const;
31 void set_name(const char *name);
32 const char *get_name() const;
34 bool create(const char *src, unsigned int type);
35 void destroy();
37 bool load(const char *fname, unsigned int type);
40 // these functions are only meant to be used by the ShaderSet
41 static Shader *create_shader();
42 static bool load_shader(Shader *sdr, const char *fname);
43 static bool done_shader(Shader *sdr, unsigned int type);
44 static void destroy_shader(Shader *sdr);
45 };
47 #define VSDR(s) s, GL_VERTEX_SHADER
48 #define FSDR(s) s, GL_FRAGMENT_SHADER
49 #define PSDR(s) FSDR(s)
50 #define GSDR(s) s, GL_GEOMETRY_SHADER
51 #define TCSDR(s) s, GL_TESS_CONTROL_SHADER
52 #define TESDR(s) s, GL_TESS_EVALUATION_SHADER
54 class ShaderProg {
55 private:
56 unsigned int prog;
57 mutable bool must_link;
58 std::vector<Shader*> shaders;
60 struct StateLocCache { int sidx, loc; };
61 /** a cache of all st_ prefixed uniform locations and their corresponding
62 * index in the global uniform state vector (see unistate.h)
63 */
64 mutable std::vector<StateLocCache> stloc_cache;
66 void cache_state_uniforms() const;
67 void setup_state_uniforms() const;
69 public:
70 static ShaderProg *current;
72 ShaderProg();
73 ~ShaderProg();
75 /// returns the OpenGL object id for this shader program
76 unsigned int get_id() const;
78 /** takes a series of shaders, and constructs a program object by linking
79 * them together. Terminate with a null pointer (don't use 0!) */
80 bool create(Shader *sdr, ...);
81 /// same as above, but with a va_list instead of variable arguments.
82 bool create(Shader *sdr, va_list ap);
83 /** takes two shaders (vertex and pixel) and constructs a program object by
84 * linking them together. Either one can be null. */
85 bool create(Shader *vsdr, Shader *psdr);
87 /** takes a series of shader source/shader type pairs and constructs a program
88 * object by linking them together. Terminate with a null pointer (don't use 0!)
89 * You can use the VSDR, PSDR, GSDR, TCSDR, TESDR convenience macros for passing
90 * the pairs.
91 * Example: create(VSDR(vsrc0), VSDR(vsrc1), PSDR(psrc), NULL);
92 */
93 bool create(const char *src, unsigned int type, ...);
94 /// same as above, but with a va_list instead of variable arguments.
95 bool create(const char *src, unsigned int type, va_list ap);
96 /** takes two shaders source strings (vertex and pixel) and constructs
97 * a program object by linking them together. Either one can be null. */
98 bool create(const char *vsrc, const char *psrc);
100 void destroy();
102 /** takes a series of shader filename/shader type pairs, loads the shaders and
103 * constructs a program object by linking them together. Terminate with a null
104 * pointer (don't use 0!). You can use the VSDR, PSDR, GSDR, TCSDR, TESDR convenience
105 * macros for passing the pairs.
106 * Example: load(VSDR("vsdr1.glsl"), VSDR("vsdr2.glsl"), PSDR("pixel.glsl"), NULL);
107 */
108 bool load(const char *fname, unsigned int type, ...);
109 /// same as above, but with a va_list instead of variable arguments.
110 bool load(const char *fname, unsigned int type, va_list ap);
111 /** takes the filenames of two shader files (vertex and pixel), loads them and
112 * constructs a program object by linking them together. Either one can be null */
113 bool load(const char *vsrc, const char *psrc);
115 void add_shader(Shader *sdr);
116 bool link() const;
118 void bind() const;
120 int get_attrib_location(const char *name) const;
121 void set_attrib_location(const char *name, int loc) const;
123 int get_uniform_location(const char *name) const;
125 bool set_uniform(int loc, int val) const;
126 bool set_uniform(int loc, float val) const;
127 bool set_uniform(int loc, const Vector2 &v) const;
128 bool set_uniform(int loc, const Vector3 &v) const;
129 bool set_uniform(int loc, const Vector4 &v) const;
130 bool set_uniform(int loc, const Matrix3x3 &m) const;
131 bool set_uniform(int loc, const Matrix4x4 &m) const;
133 bool set_uniform(const char *name, int val) const;
134 bool set_uniform(const char *name, float val) const;
135 bool set_uniform(const char *name, const Vector2 &v) const;
136 bool set_uniform(const char *name, const Vector3 &v) const;
137 bool set_uniform(const char *name, const Vector4 &v) const;
138 bool set_uniform(const char *name, const Matrix3x3 &m) const;
139 bool set_uniform(const char *name, const Matrix4x4 &m) const;
141 friend void setup_unistate(const ShaderProg*);
142 };
144 class ShaderSet : public DataSet<Shader*> {
145 private:
146 unsigned int type;
148 public:
149 ShaderSet(unsigned int type);
150 };
152 } // namespace goatgfx
154 #endif // SHADER_H_