goat3dgfx

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