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