vrshoot

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