goat3dgfx

annotate src/shader.h @ 2:7bd5ebec3b6f

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