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