goat3dgfx
diff src/shader.h @ 0:1873dfd13f2d
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 14 Nov 2013 05:27:09 +0200 |
parents | |
children | 7d6b667821cf |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/shader.h Thu Nov 14 05:27:09 2013 +0200 1.3 @@ -0,0 +1,135 @@ 1.4 +#ifndef SHADER_H_ 1.5 +#define SHADER_H_ 1.6 + 1.7 +#include <vector> 1.8 +#include "vmath/vmath.h" 1.9 +#include "opengl.h" 1.10 +#include "dataset.h" 1.11 + 1.12 +class Shader { 1.13 +private: 1.14 + unsigned int sdr; 1.15 + unsigned int type; 1.16 + char *name; 1.17 + 1.18 +public: 1.19 + Shader(); 1.20 + ~Shader(); 1.21 + 1.22 + unsigned int get_id() const; 1.23 + 1.24 + void set_name(const char *name); 1.25 + const char *get_name() const; 1.26 + 1.27 + bool create(const char *src, unsigned int type); 1.28 + void destroy(); 1.29 + 1.30 + bool load(const char *fname, unsigned int type); 1.31 +}; 1.32 + 1.33 +#define VSDR(s) s, GL_VERTEX_SHADER 1.34 +#define FSDR(s) s, GL_FRAGMENT_SHADER 1.35 +#define PSDR(s) FSDR(s) 1.36 +#define GSDR(s) s, GL_GEOMETRY_SHADER 1.37 +#define TCSDR(s) s, GL_TESS_CONTROL_SHADER 1.38 +#define TESDR(s) s, GL_TESS_EVALUATION_SHADER 1.39 + 1.40 +class ShaderProg { 1.41 +private: 1.42 + unsigned int prog; 1.43 + mutable bool must_link; 1.44 + std::vector<Shader*> shaders; 1.45 + 1.46 + struct StateLocCache { int sidx, loc; }; 1.47 + /** a cache of all st_ prefixed uniform locations and their corresponding 1.48 + * index in the global uniform state vector (see unistate.h) 1.49 + */ 1.50 + mutable std::vector<StateLocCache> stloc_cache; 1.51 + 1.52 + void cache_state_uniforms() const; 1.53 + void setup_state_uniforms() const; 1.54 + 1.55 +public: 1.56 + static ShaderProg *current; 1.57 + 1.58 + ShaderProg(); 1.59 + ~ShaderProg(); 1.60 + 1.61 + /// returns the OpenGL object id for this shader program 1.62 + unsigned int get_id() const; 1.63 + 1.64 + /** takes a series of shaders, and constructs a program object by linking 1.65 + * them together. Terminate with a null pointer (don't use 0!) */ 1.66 + bool create(Shader *sdr, ...); 1.67 + /// same as above, but with a va_list instead of variable arguments. 1.68 + bool create(Shader *sdr, va_list ap); 1.69 + /** takes two shaders (vertex and pixel) and constructs a program object by 1.70 + * linking them together. Either one can be null. */ 1.71 + bool create(Shader *vsdr, Shader *psdr); 1.72 + 1.73 + /** takes a series of shader source/shader type pairs and constructs a program 1.74 + * object by linking them together. Terminate with a null pointer (don't use 0!) 1.75 + * You can use the VSDR, PSDR, GSDR, TCSDR, TESDR convenience macros for passing 1.76 + * the pairs. 1.77 + * Example: create(VSDR(vsrc0), VSDR(vsrc1), PSDR(psrc), NULL); 1.78 + */ 1.79 + bool create(const char *src, unsigned int type, ...); 1.80 + /// same as above, but with a va_list instead of variable arguments. 1.81 + bool create(const char *src, unsigned int type, va_list ap); 1.82 + /** takes two shaders source strings (vertex and pixel) and constructs 1.83 + * a program object by linking them together. Either one can be null. */ 1.84 + bool create(const char *vsrc, const char *psrc); 1.85 + 1.86 + void destroy(); 1.87 + 1.88 + /** takes a series of shader filename/shader type pairs, loads the shaders and 1.89 + * constructs a program object by linking them together. Terminate with a null 1.90 + * pointer (don't use 0!). You can use the VSDR, PSDR, GSDR, TCSDR, TESDR convenience 1.91 + * macros for passing the pairs. 1.92 + * Example: load(VSDR("vsdr1.glsl"), VSDR("vsdr2.glsl"), PSDR("pixel.glsl"), NULL); 1.93 + */ 1.94 + bool load(const char *fname, unsigned int type, ...); 1.95 + /// same as above, but with a va_list instead of variable arguments. 1.96 + bool load(const char *fname, unsigned int type, va_list ap); 1.97 + /** takes the filenames of two shader files (vertex and pixel), loads them and 1.98 + * constructs a program object by linking them together. Either one can be null */ 1.99 + bool load(const char *vsrc, const char *psrc); 1.100 + 1.101 + void add_shader(Shader *sdr); 1.102 + bool link() const; 1.103 + 1.104 + void bind() const; 1.105 + 1.106 + int get_attrib_location(const char *name) const; 1.107 + void set_attrib_location(const char *name, int loc) const; 1.108 + 1.109 + int get_uniform_location(const char *name) const; 1.110 + 1.111 + bool set_uniform(int loc, int val) const; 1.112 + bool set_uniform(int loc, float val) const; 1.113 + bool set_uniform(int loc, const Vector2 &v) const; 1.114 + bool set_uniform(int loc, const Vector3 &v) const; 1.115 + bool set_uniform(int loc, const Vector4 &v) const; 1.116 + bool set_uniform(int loc, const Matrix3x3 &m) const; 1.117 + bool set_uniform(int loc, const Matrix4x4 &m) const; 1.118 + 1.119 + bool set_uniform(const char *name, int val) const; 1.120 + bool set_uniform(const char *name, float val) const; 1.121 + bool set_uniform(const char *name, const Vector2 &v) const; 1.122 + bool set_uniform(const char *name, const Vector3 &v) const; 1.123 + bool set_uniform(const char *name, const Vector4 &v) const; 1.124 + bool set_uniform(const char *name, const Matrix3x3 &m) const; 1.125 + bool set_uniform(const char *name, const Matrix4x4 &m) const; 1.126 + 1.127 + friend void setup_unistate(const ShaderProg*); 1.128 +}; 1.129 + 1.130 +class ShaderSet : public DataSet<Shader*> { 1.131 +private: 1.132 + unsigned int type; 1.133 + 1.134 +public: 1.135 + ShaderSet(unsigned int type); 1.136 +}; 1.137 + 1.138 +#endif // SHADER_H_