#ifndef SHADER_H #define SHADER_H #include #include #include #include #include "opengl.h" #include "util.h" namespace shader { using namespace std; const int NAMELEN = 32; namespace compile { struct shader_builder { set seen; ostringstream global; ostringstream local; ostringstream code; bool first (const void *p); }; extern shader_builder *cur; } template struct sl_expr { const T &t; sl_expr (const T &t) : t(t) { } void operator ()() const { t (); } template const sl_expr &operator =(const expr &e) const; }; template struct sl_concat2 { const A a; const B b; sl_concat2 (const A &a, const B &b) : a(a), b(b) { } void operator ()() const { a (); b (); } }; template struct sl_concat3 { const A a; const B b; const C c; sl_concat3 (const A &a, const B &b, const C &c) : a(a), b(b), c(c) { } void operator ()() const { a (); b (); c (); } }; template struct sl_concat4 { const A a; const B b; const C c; const D d; sl_concat4 (const A &a, const B &b, const C &c, const D &d) : a(a), b(b), c(c), d(d) { } void operator ()() const { a (); b (); c (); d(); } }; struct sl_func0 { const char *name_par; sl_func0 (const char *name_par) : name_par(name_par) { } void begin () const { compile::cur->code << name_par; } void comma () const { compile::cur->code << ", "; } void end () const { compile::cur->code << ")"; } void operator ()() const { begin (); end (); } }; template struct sl_func1 : sl_func0 { const A a; sl_func1 (const char *name, const A &a) : sl_func0(name), a(a) { } void operator ()() const { begin (); a (); end (); } }; template struct sl_func2 : sl_func0 { const A a; const B b; sl_func2 (const char *name, const A &a, const B &b) : sl_func0(name), a(a), b(b) { } void operator ()() const { begin (); a (); comma (); b (); end (); } }; template struct sl_func3 : sl_func0 { const A a; const B b; const C c; sl_func3 (const char *name, const A &a, const B &b, const C &c) : sl_func0(name), a(a), b(b), c(c) { } void operator ()() const { begin (); a (); comma (); b (); comma (); c (); end (); } }; template struct sl_func4 : sl_func0 { const A a; const B b; const C c; const D d; sl_func4 (const char *name, const A &a, const B &b, const C &c, const D &d) : sl_func0(name), a(a), b(b), c(c), d(d) { } void operator ()() const { begin (); a (); comma (); b (); comma (); c (); comma (); d(); end (); } }; class refcounted { template friend class ref; mutable unsigned int refcnt; void refcnt_inc () const { refcnt++; } void refcnt_dec () const { if (!--refcnt) refcnt_destroy (); } void refcnt_destroy () const; public: refcounted () : refcnt(0) { } virtual ~refcounted (); }; template struct ref { type *p; ref () { p = new type; p->refcnt_inc (); } ref (type &d) { d.refcnt_inc (); p = &d; } ref (const ref &r) { r.p->refcnt_inc (); p = r.p; } template ref (const ref &r) { r.p->refcnt_inc (); p = r.p; } ~ref () { p->refcnt_dec (); } ref &operator =(type &d) { d.refcnt_inc (); p->refcnt_dec (); p = &d; return *this; } type *operator ->() const { return p; } operator type &() const { return *p; } template bool operator ==(const ref b) const { return (void *)p == (void *)b.p; } }; // ref with auto-construct template struct auto_ref0 : ref { auto_ref0 () : ref (*new type ()) { } }; template struct auto_ref1 : ref { auto_ref1 (arg1 a) : ref (*new type (a)) { } }; template struct auto_lvalue_ref0 : ref { auto_lvalue_ref0 () : ref (*new type ()) { } template const auto_lvalue_ref0 &operator =(const expr &e) const; }; template struct auto_lvalue_ref1 : ref { auto_lvalue_ref1 (arg1 a) : ref (*new type (a)) { } template const auto_lvalue_ref1 &operator =(const expr &e) const; }; extern const char str_float []; extern const char str_vec2 []; extern const char str_vec3 []; extern const char str_vec4 []; extern const char str_mat2 []; extern const char str_mat3 []; extern const char str_mat4 []; extern const char str_sampler_1d []; extern const char str_sampler_1d_shadow []; extern const char str_sampler_2d []; extern const char str_sampler_2d_shadow []; extern const char str_sampler_2d_rect []; extern const char str_sampler_2d_rect_shadow []; extern const char str_sampler_3d []; extern const char str_sampler_3d_rect []; extern const char str_sampler_cube []; typedef ref var; typedef ref uniform; struct fragment_i : refcounted { }; typedef ref fragment; struct lvalue_i : fragment_i { }; // a simple predeclared variable with unspecified type struct gluvar_i : lvalue_i { const char *name; void operator ()() const { compile::cur->code << name; } gluvar_i (const char *name) : name (name) { } }; typedef auto_lvalue_ref1 gluvar; struct var_i : lvalue_i { static unsigned int next_id; char name[NAMELEN]; const char *domainstr; const char *typestr; void operator ()() const; var_i (const char *domainstr, const char *typestr); ~var_i (); }; struct uniform_i : var_i { GLint location (); uniform_i (const char *strtype); }; template struct uniform2_i : uniform_i { gltype data[dimension]; uniform2_i () : uniform_i (strtype) { } }; template struct uniform_f_i : uniform2_i { }; struct uniform_1f_i : uniform_f_i<1, str_float> { void set (GLfloat v) { data[0] = v; GLint loc = location (); if (loc >= 0) // workaround for buggy drivers glUniform1fvARB (loc, 1, data); } }; struct uniform_2f_i : uniform_f_i<2, str_vec2> { void set (GLfloat x, GLfloat y) { data[0] = x; data[1] = y; GLint loc = location (); if (loc >= 0) // workaround for buggy drivers glUniform2fvARB (loc, 1, data); } void set (const vec2 &v) { set (v.x, v.y); } }; struct uniform_3f_i : uniform_f_i<3, str_vec3> { void set (GLfloat x, GLfloat y, GLfloat z) { data[0] = x; data[1] = y; data[2] = z; GLint loc = location (); if (loc >= 0) // workaround for buggy drivers glUniform3fvARB (loc, 1, data); } void set (const vec3 &v) { set (v.x, v.y, v.z); } }; struct uniform_4f_i : uniform_f_i<4, str_vec4> { void set (GLfloat x, GLfloat y, GLfloat z, GLfloat w) { data[0] = x; data[1] = y; data[2] = z; data[3] = w; GLint loc = location (); if (loc >= 0) // workaround for buggy drivers glUniform4fvARB (loc, 1, data); } void set (const vec4 &v) { set (v.x, v.y, v.z, v.w); } }; struct uniform_matrix_2f_i : uniform_f_i< 4, str_mat2> { void set (const GLfloat d[4]) { memcpy (data, d, 4 * sizeof (GLfloat)); GLint loc = location (); if (loc >= 0) // workaround for buggy drivers glUniformMatrix2fvARB (loc, 1, 0, data); } }; struct uniform_matrix_3f_i : uniform_f_i< 9, str_mat3> { void set (const GLfloat d[9]) { memcpy (data, d, 9 * sizeof (GLfloat)); GLint loc = location (); if (loc >= 0) // workaround for buggy drivers glUniformMatrix3fvARB (loc, 1, 0, data); } }; struct uniform_matrix_4f_i : uniform_f_i<16, str_mat4> { void set (const gl::matrix &m) { memcpy (data, m.data, 16 * sizeof (GLfloat)); GLint loc = location (); if (loc >= 0) // workaround for buggy drivers glUniformMatrix4fvARB (loc, 1, 0, data); } }; template struct var_ref : ref { var_ref (const char *glname = 0) : ref (*new var_i) { if (glname) strcpy ((*this)->name, glname); } }; typedef var_ref uniform_1f; typedef var_ref uniform_2f; typedef var_ref uniform_3f; typedef var_ref uniform_4f; typedef var_ref uniform_matrix_2f; typedef var_ref uniform_matrix_3f; typedef var_ref uniform_matrix_4f; #if 0 // to be used for attributes struct stream_i : var_i { stream_i (const char *strtype); }; template struct varying_i : stream_i { varying_i () : stream_i (strtype) { } void set (GLsizei stride, GLvoid *ptr) { //cgGLSetParameterPointer (param, dimension, gltype, stride, ptr); } }; template struct varying_f_i : varying_i { void set (const gl::vertex_buffer &vb, GLint offset) { varying_i::set (gl::format_stride (vb.format), (GLvoid *)(long)offset); } }; struct varying_1f_i : varying_f_i<1, str_float> { }; struct varying_2f_i : varying_f_i<2, str_vec2> { void set_t (const gl::vertex_buffer &vb) { set (vb, gl::format_offset_t (vb.format)); } }; struct varying_3f_i : varying_f_i<3, str_vec3> { void set_p (const gl::vertex_buffer &vb) { set (vb, gl::format_offset_p (vb.format)); } void set_n (const gl::vertex_buffer &vb) { set (vb, gl::format_offset_n (vb.format)); } }; struct varying_4f_i : varying_f_i<4, str_vec4> { }; typedef var_ref varying_1f; typedef var_ref varying_2f; typedef var_ref varying_3f; typedef var_ref varying_4f; #endif struct varying_i : var_i { varying_i (const char *strtype); }; struct temporary_i : var_i { temporary_i (const char *strtype); }; template struct glnvar_ref : ref { glnvar_ref () : ref (*new var_i (strtype)) { } #if 0 template glnvar_ref (const sl_expr &e) : ref (*new var_i (strtype)) { (*this) = e; } #endif template const glnvar_ref &operator =(const expr &e) const; }; typedef glnvar_ref temp_1f; typedef glnvar_ref temp_2f; typedef glnvar_ref temp_3f; typedef glnvar_ref temp_4f; typedef glnvar_ref temp_matrix_2f; typedef glnvar_ref temp_matrix_3f; typedef glnvar_ref temp_matrix_4f; typedef glnvar_ref varying_1f; typedef glnvar_ref varying_2f; typedef glnvar_ref varying_3f; typedef glnvar_ref varying_4f; typedef glnvar_ref varying_matrix_2f; typedef glnvar_ref varying_matrix_3f; typedef glnvar_ref varying_matrix_4f; struct texture_units { static int units[8]; static int unit_count; static GLenum get_texture_unit () { assert (unit_count); return units[--unit_count]; } static void put_texture_unit (GLenum unit) { assert (unit_count < 8); units[unit_count++] = unit; } }; template struct sampler_i : uniform_i, texture_units { int unit; GLuint name; void enable () { #if DEBUG assert (unit < 0); #endif GLint loc = location (); if (loc >= 0) { unit = get_texture_unit (); glActiveTexture (GL_TEXTURE0 + unit); glBindTexture (gltype, name); glUniform1iARB (loc, unit); } } void disable () { if (unit >= 0) { put_texture_unit (unit); unit = -1; } } template void set (const gl::vertex_buffer &vb, const vectype bufferclass::*vp) { glTexCoordPointer ( vector_traits::dimension (), gl::type_traits::basetype>::glenum (), sizeof (bufferclass), (GLvoid)(char bufferclass::*)vp ); } sampler_i (GLuint texture) : uniform_i (strtype) , name (texture) #if DEBUG , unit (-1) #endif { } }; struct sampler_1d_i : sampler_i { sampler_1d_i (GLuint texture) : sampler_i (texture) { } }; struct sampler_1d_shadow_i : sampler_i { sampler_1d_shadow_i (GLuint texture) : sampler_i (texture) { } }; struct sampler_2d_i : sampler_i { sampler_2d_i (GLuint texture) : sampler_i (texture) { } }; struct sampler_2d_shadow_i : sampler_i { sampler_2d_shadow_i (GLuint texture) : sampler_i (texture) { } }; struct sampler_2d_rect_i : sampler_i { sampler_2d_rect_i (GLuint texture) : sampler_i (texture) { } }; struct sampler_2d_rect_shadow_i : sampler_i { sampler_2d_rect_shadow_i (GLuint texture) : sampler_i (texture) { } }; struct sampler_3d_i : sampler_i { sampler_3d_i (GLuint texture) : sampler_i (texture) { } }; struct sampler_3d_rect_i : sampler_i { sampler_3d_rect_i (GLuint texture) : sampler_i (texture) { } }; struct sampler_cube_i : sampler_i { sampler_cube_i (GLuint texture) : sampler_i (texture) { } }; template struct sampler_ref : auto_ref1 { sampler_ref (GLuint texture) : auto_ref1 (texture) { } }; typedef sampler_ref sampler_1d; typedef sampler_ref sampler_1d_shadow; typedef sampler_ref sampler_2d; typedef sampler_ref sampler_2d_shadow; typedef sampler_ref sampler_2d_rect; typedef sampler_ref sampler_2d_rect_shadow; typedef sampler_ref sampler_3d; typedef sampler_ref sampler_3d_rect; typedef sampler_ref sampler_cube; struct shader_object_i : refcounted { GLenum type; GLuint id; // GLhandleARB, but 2.0 will use uint void start (); void stop (); shader_object_i (GLenum type); ~shader_object_i (); }; template struct shader_object : ref { shader_object () : ref (*new shader_object_i (type)) { } }; typedef shader_object vertex_shader; typedef shader_object fragment_shader; struct program_object_i : refcounted { static struct program_object_i *cur; // currently bound program GLuint id; vertex_shader vsh; fragment_shader fsh; map uloc; // uniform location program_object_i (); ~program_object_i (); void link (); void enable (); void disable (); }; typedef auto_ref0 program_object; template struct sl_string { char str[length]; void operator ()() const { compile::cur->code << str; } }; struct sl_float { const GLfloat c; sl_float (GLfloat c) : c(c) { } void operator ()() const { compile::cur->code << c; } }; template inline sl_expr< sl_concat2 > concat (const A &a, const B &b) { typedef sl_concat2 expr; return sl_expr (expr (a, b)); } template inline sl_expr< sl_concat3 > concat (const A &a, const B &b, const C &c) { typedef sl_concat3 expr; return sl_expr (expr (a, b, c)); } template inline sl_expr< sl_concat4 > concat (const A &a, const B &b, const C &c, const D &d) { typedef sl_concat4 expr; return sl_expr (expr (a, b, c, d)); } template struct sl_convert { typedef sl_expr T; static inline const T &convert (const T &e) { return e; } }; template<> struct sl_convert { typedef sl_expr T; static inline const T convert (GLfloat f) { return sl_float (f); } }; template<> struct sl_convert { typedef sl_expr T; static inline const T convert (GLdouble d) { return sl_float (d); } }; template<> struct sl_convert { typedef sl_expr T; static inline const T convert (GLint i) { return sl_float (i); } }; template<> struct sl_convert { static const sl_expr< sl_string<60> > convert (const vec2 &v); }; template<> struct sl_convert { static const sl_expr< sl_string<80> > convert (const vec3 &v); }; template<> struct sl_convert { static const sl_expr< sl_string<100> > convert (const vec4 &v); }; template<> template struct sl_convert< var_ref > { typedef sl_expr< var_i > T; static inline const T convert (const var_ref &v) { return T (*&v); } }; template<> template struct sl_convert< auto_lvalue_ref1 > { typedef sl_expr< gluvar_i > T; static inline const T convert (const auto_lvalue_ref1 &v) { return T (*&v); } }; template<> template struct sl_convert< glnvar_ref > { typedef sl_expr< var_i > T; static inline const T convert (const glnvar_ref &v) { return T (*&v); } }; template<> template struct sl_convert< sampler_ref > { typedef sl_expr< sampler_i > T; static inline const T convert (const sampler_ref &v) { return T (*&v); } }; template inline void sl_assign (const lvalue &l, const expr &e) { compile::cur->code << " "; sl_convert::convert (l) (); compile::cur->code << " = "; sl_convert::convert (e) (); compile::cur->code << ";\n"; } template template inline const auto_lvalue_ref0 &auto_lvalue_ref0::operator =(const expr &e) const { sl_assign (*this, e); return *this; } template template inline const auto_lvalue_ref1 &auto_lvalue_ref1::operator =(const expr &e) const { sl_assign (*this, e); return *this; } template template inline const sl_expr &sl_expr::operator =(const expr &e) const { sl_assign (*this, e); return *this; } template template inline const glnvar_ref &glnvar_ref::operator =(const expr &e) const { compile::sl_assign (*this, e); return *this; } namespace compile { using namespace shader; template inline const sl_expr< sl_func1::T> > operator +(const T &t) { return sl_func1::T> ("+(", sl_convert::convert (t)); } template inline const sl_expr< sl_func1::T> > operator -(const T &t) { return sl_func1::T> ("-(", sl_convert::convert (t)); } template struct sl_binop { const A a; const char *b; const C c; sl_binop (const A &a, const char *b, const C &c) : a(a), b(b), c(c) { } void operator ()() const { compile::cur->code << "("; a (); compile::cur->code << b; c (); compile::cur->code << ")"; } }; template struct sl_ternary { const A a; const B b; const C c; sl_ternary (const A &a, const B &b, const C &c) : a(a), b(b), c(c) { } void operator ()() const { compile::cur->code << "("; a (); compile::cur->code << ") ? ("; b (); compile::cur->code << ") : ("; c (); compile::cur->code << ")"; } }; # define SHADER_BINOP(op) \ template \ inline const sl_expr< sl_binop< typename sl_convert::T, \ typename sl_convert::T > > \ operator op(const A &a, const B &b) \ { \ return sl_binop< typename sl_convert::T, typename sl_convert::T > \ (sl_convert::convert (a), " " # op " ", sl_convert::convert (b)); \ } SHADER_BINOP (+); SHADER_BINOP (-); SHADER_BINOP (*); SHADER_BINOP (/); SHADER_BINOP (%); SHADER_BINOP (<); SHADER_BINOP (<=); SHADER_BINOP (==); SHADER_BINOP (!=); SHADER_BINOP (>=); SHADER_BINOP (>); # undef SHADER_BINOP template struct sl_swizzle { const A a; const char *swizzle; sl_swizzle (const A &a, const char *swizzle) : a(a), swizzle(swizzle) { } void operator ()() const { compile::cur->code << "("; a (); compile::cur->code << ")." << swizzle; } }; # define SHADER_SWIZZLE_OP(type) \ template \ inline const sl_expr< sl_swizzle::T> > \ type (const T &t) \ { \ return sl_swizzle::T> (sl_convert::convert (t), #type); \ } // brute force is lovely, ain't it? SHADER_SWIZZLE_OP (x ) SHADER_SWIZZLE_OP (xx ) SHADER_SWIZZLE_OP (xxx ) SHADER_SWIZZLE_OP (xxxx) SHADER_SWIZZLE_OP (xxxy) SHADER_SWIZZLE_OP (xxxz) SHADER_SWIZZLE_OP (xxxw) SHADER_SWIZZLE_OP (xxy ) SHADER_SWIZZLE_OP (xxyx) SHADER_SWIZZLE_OP (xxyy) SHADER_SWIZZLE_OP (xxyz) SHADER_SWIZZLE_OP (xxyw) SHADER_SWIZZLE_OP (xxz ) SHADER_SWIZZLE_OP (xxzx) SHADER_SWIZZLE_OP (xxzy) SHADER_SWIZZLE_OP (xxzz) SHADER_SWIZZLE_OP (xxzw) SHADER_SWIZZLE_OP (xxw ) SHADER_SWIZZLE_OP (xxwx) SHADER_SWIZZLE_OP (xxwy) SHADER_SWIZZLE_OP (xxwz) SHADER_SWIZZLE_OP (xxww) SHADER_SWIZZLE_OP (xy ) SHADER_SWIZZLE_OP (xyx ) SHADER_SWIZZLE_OP (xyxx) SHADER_SWIZZLE_OP (xyxy) SHADER_SWIZZLE_OP (xyxz) SHADER_SWIZZLE_OP (xyxw) SHADER_SWIZZLE_OP (xyy ) SHADER_SWIZZLE_OP (xyyx) SHADER_SWIZZLE_OP (xyyy) SHADER_SWIZZLE_OP (xyyz) SHADER_SWIZZLE_OP (xyyw) SHADER_SWIZZLE_OP (xyz ) SHADER_SWIZZLE_OP (xyzx) SHADER_SWIZZLE_OP (xyzy) SHADER_SWIZZLE_OP (xyzz) SHADER_SWIZZLE_OP (xyzw) SHADER_SWIZZLE_OP (xyw ) SHADER_SWIZZLE_OP (xywx) SHADER_SWIZZLE_OP (xywy) SHADER_SWIZZLE_OP (xywz) SHADER_SWIZZLE_OP (xyww) SHADER_SWIZZLE_OP (xz ) SHADER_SWIZZLE_OP (xzx ) SHADER_SWIZZLE_OP (xzxx) SHADER_SWIZZLE_OP (xzxy) SHADER_SWIZZLE_OP (xzxz) SHADER_SWIZZLE_OP (xzxw) SHADER_SWIZZLE_OP (xzy ) SHADER_SWIZZLE_OP (xzyx) SHADER_SWIZZLE_OP (xzyy) SHADER_SWIZZLE_OP (xzyz) SHADER_SWIZZLE_OP (xzyw) SHADER_SWIZZLE_OP (xzz ) SHADER_SWIZZLE_OP (xzzx) SHADER_SWIZZLE_OP (xzzy) SHADER_SWIZZLE_OP (xzzz) SHADER_SWIZZLE_OP (xzzw) SHADER_SWIZZLE_OP (xzw ) SHADER_SWIZZLE_OP (xzwx) SHADER_SWIZZLE_OP (xzwy) SHADER_SWIZZLE_OP (xzwz) SHADER_SWIZZLE_OP (xzww) SHADER_SWIZZLE_OP (xw ) SHADER_SWIZZLE_OP (xwx ) SHADER_SWIZZLE_OP (xwxx) SHADER_SWIZZLE_OP (xwxy) SHADER_SWIZZLE_OP (xwxz) SHADER_SWIZZLE_OP (xwxw) SHADER_SWIZZLE_OP (xwy ) SHADER_SWIZZLE_OP (xwyx) SHADER_SWIZZLE_OP (xwyy) SHADER_SWIZZLE_OP (xwyz) SHADER_SWIZZLE_OP (xwyw) SHADER_SWIZZLE_OP (xwz ) SHADER_SWIZZLE_OP (xwzx) SHADER_SWIZZLE_OP (xwzy) SHADER_SWIZZLE_OP (xwzz) SHADER_SWIZZLE_OP (xwzw) SHADER_SWIZZLE_OP (xww ) SHADER_SWIZZLE_OP (xwwx) SHADER_SWIZZLE_OP (xwwy) SHADER_SWIZZLE_OP (xwwz) SHADER_SWIZZLE_OP (xwww) SHADER_SWIZZLE_OP (y ) SHADER_SWIZZLE_OP (yx ) SHADER_SWIZZLE_OP (yxx ) SHADER_SWIZZLE_OP (yxxx) SHADER_SWIZZLE_OP (yxxy) SHADER_SWIZZLE_OP (yxxz) SHADER_SWIZZLE_OP (yxxw) SHADER_SWIZZLE_OP (yxy ) SHADER_SWIZZLE_OP (yxyx) SHADER_SWIZZLE_OP (yxyy) SHADER_SWIZZLE_OP (yxyz) SHADER_SWIZZLE_OP (yxyw) SHADER_SWIZZLE_OP (yxz ) SHADER_SWIZZLE_OP (yxzx) SHADER_SWIZZLE_OP (yxzy) SHADER_SWIZZLE_OP (yxzz) SHADER_SWIZZLE_OP (yxzw) SHADER_SWIZZLE_OP (yxw ) SHADER_SWIZZLE_OP (yxwx) SHADER_SWIZZLE_OP (yxwy) SHADER_SWIZZLE_OP (yxwz) SHADER_SWIZZLE_OP (yxww) SHADER_SWIZZLE_OP (yy ) SHADER_SWIZZLE_OP (yyx ) SHADER_SWIZZLE_OP (yyxx) SHADER_SWIZZLE_OP (yyxy) SHADER_SWIZZLE_OP (yyxz) SHADER_SWIZZLE_OP (yyxw) SHADER_SWIZZLE_OP (yyy ) SHADER_SWIZZLE_OP (yyyx) SHADER_SWIZZLE_OP (yyyy) SHADER_SWIZZLE_OP (yyyz) SHADER_SWIZZLE_OP (yyyw) SHADER_SWIZZLE_OP (yyz ) SHADER_SWIZZLE_OP (yyzx) SHADER_SWIZZLE_OP (yyzy) SHADER_SWIZZLE_OP (yyzz) SHADER_SWIZZLE_OP (yyzw) SHADER_SWIZZLE_OP (yyw ) SHADER_SWIZZLE_OP (yywx) SHADER_SWIZZLE_OP (yywy) SHADER_SWIZZLE_OP (yywz) SHADER_SWIZZLE_OP (yyww) SHADER_SWIZZLE_OP (yz ) SHADER_SWIZZLE_OP (yzx ) SHADER_SWIZZLE_OP (yzxx) SHADER_SWIZZLE_OP (yzxy) SHADER_SWIZZLE_OP (yzxz) SHADER_SWIZZLE_OP (yzxw) SHADER_SWIZZLE_OP (yzy ) SHADER_SWIZZLE_OP (yzyx) SHADER_SWIZZLE_OP (yzyy) SHADER_SWIZZLE_OP (yzyz) SHADER_SWIZZLE_OP (yzyw) SHADER_SWIZZLE_OP (yzz ) SHADER_SWIZZLE_OP (yzzx) SHADER_SWIZZLE_OP (yzzy) SHADER_SWIZZLE_OP (yzzz) SHADER_SWIZZLE_OP (yzzw) SHADER_SWIZZLE_OP (yzw ) SHADER_SWIZZLE_OP (yzwx) SHADER_SWIZZLE_OP (yzwy) SHADER_SWIZZLE_OP (yzwz) SHADER_SWIZZLE_OP (yzww) SHADER_SWIZZLE_OP (yw ) SHADER_SWIZZLE_OP (ywx ) SHADER_SWIZZLE_OP (ywxx) SHADER_SWIZZLE_OP (ywxy) SHADER_SWIZZLE_OP (ywxz) SHADER_SWIZZLE_OP (ywxw) SHADER_SWIZZLE_OP (ywy ) SHADER_SWIZZLE_OP (ywyx) SHADER_SWIZZLE_OP (ywyy) SHADER_SWIZZLE_OP (ywyz) SHADER_SWIZZLE_OP (ywyw) SHADER_SWIZZLE_OP (ywz ) SHADER_SWIZZLE_OP (ywzx) SHADER_SWIZZLE_OP (ywzy) SHADER_SWIZZLE_OP (ywzz) SHADER_SWIZZLE_OP (ywzw) SHADER_SWIZZLE_OP (yww ) SHADER_SWIZZLE_OP (ywwx) SHADER_SWIZZLE_OP (ywwy) SHADER_SWIZZLE_OP (ywwz) SHADER_SWIZZLE_OP (ywww) SHADER_SWIZZLE_OP (z ) SHADER_SWIZZLE_OP (zx ) SHADER_SWIZZLE_OP (zxx ) SHADER_SWIZZLE_OP (zxxx) SHADER_SWIZZLE_OP (zxxy) SHADER_SWIZZLE_OP (zxxz) SHADER_SWIZZLE_OP (zxxw) SHADER_SWIZZLE_OP (zxy ) SHADER_SWIZZLE_OP (zxyx) SHADER_SWIZZLE_OP (zxyy) SHADER_SWIZZLE_OP (zxyz) SHADER_SWIZZLE_OP (zxyw) SHADER_SWIZZLE_OP (zxz ) SHADER_SWIZZLE_OP (zxzx) SHADER_SWIZZLE_OP (zxzy) SHADER_SWIZZLE_OP (zxzz) SHADER_SWIZZLE_OP (zxzw) SHADER_SWIZZLE_OP (zxw ) SHADER_SWIZZLE_OP (zxwx) SHADER_SWIZZLE_OP (zxwy) SHADER_SWIZZLE_OP (zxwz) SHADER_SWIZZLE_OP (zxww) SHADER_SWIZZLE_OP (zy ) SHADER_SWIZZLE_OP (zyx ) SHADER_SWIZZLE_OP (zyxx) SHADER_SWIZZLE_OP (zyxy) SHADER_SWIZZLE_OP (zyxz) SHADER_SWIZZLE_OP (zyxw) SHADER_SWIZZLE_OP (zyy ) SHADER_SWIZZLE_OP (zyyx) SHADER_SWIZZLE_OP (zyyy) SHADER_SWIZZLE_OP (zyyz) SHADER_SWIZZLE_OP (zyyw) SHADER_SWIZZLE_OP (zyz ) SHADER_SWIZZLE_OP (zyzx) SHADER_SWIZZLE_OP (zyzy) SHADER_SWIZZLE_OP (zyzz) SHADER_SWIZZLE_OP (zyzw) SHADER_SWIZZLE_OP (zyw ) SHADER_SWIZZLE_OP (zywx) SHADER_SWIZZLE_OP (zywy) SHADER_SWIZZLE_OP (zywz) SHADER_SWIZZLE_OP (zyww) SHADER_SWIZZLE_OP (zz ) SHADER_SWIZZLE_OP (zzx ) SHADER_SWIZZLE_OP (zzxx) SHADER_SWIZZLE_OP (zzxy) SHADER_SWIZZLE_OP (zzxz) SHADER_SWIZZLE_OP (zzxw) SHADER_SWIZZLE_OP (zzy ) SHADER_SWIZZLE_OP (zzyx) SHADER_SWIZZLE_OP (zzyy) SHADER_SWIZZLE_OP (zzyz) SHADER_SWIZZLE_OP (zzyw) SHADER_SWIZZLE_OP (zzz ) SHADER_SWIZZLE_OP (zzzx) SHADER_SWIZZLE_OP (zzzy) SHADER_SWIZZLE_OP (zzzz) SHADER_SWIZZLE_OP (zzzw) SHADER_SWIZZLE_OP (zzw ) SHADER_SWIZZLE_OP (zzwx) SHADER_SWIZZLE_OP (zzwy) SHADER_SWIZZLE_OP (zzwz) SHADER_SWIZZLE_OP (zzww) SHADER_SWIZZLE_OP (zw ) SHADER_SWIZZLE_OP (zwx ) SHADER_SWIZZLE_OP (zwxx) SHADER_SWIZZLE_OP (zwxy) SHADER_SWIZZLE_OP (zwxz) SHADER_SWIZZLE_OP (zwxw) SHADER_SWIZZLE_OP (zwy ) SHADER_SWIZZLE_OP (zwyx) SHADER_SWIZZLE_OP (zwyy) SHADER_SWIZZLE_OP (zwyz) SHADER_SWIZZLE_OP (zwyw) SHADER_SWIZZLE_OP (zwz ) SHADER_SWIZZLE_OP (zwzx) SHADER_SWIZZLE_OP (zwzy) SHADER_SWIZZLE_OP (zwzz) SHADER_SWIZZLE_OP (zwzw) SHADER_SWIZZLE_OP (zww ) SHADER_SWIZZLE_OP (zwwx) SHADER_SWIZZLE_OP (zwwy) SHADER_SWIZZLE_OP (zwwz) SHADER_SWIZZLE_OP (zwww) SHADER_SWIZZLE_OP (w ) SHADER_SWIZZLE_OP (wx ) SHADER_SWIZZLE_OP (wxx ) SHADER_SWIZZLE_OP (wxxx) SHADER_SWIZZLE_OP (wxxy) SHADER_SWIZZLE_OP (wxxz) SHADER_SWIZZLE_OP (wxxw) SHADER_SWIZZLE_OP (wxy ) SHADER_SWIZZLE_OP (wxyx) SHADER_SWIZZLE_OP (wxyy) SHADER_SWIZZLE_OP (wxyz) SHADER_SWIZZLE_OP (wxyw) SHADER_SWIZZLE_OP (wxz ) SHADER_SWIZZLE_OP (wxzx) SHADER_SWIZZLE_OP (wxzy) SHADER_SWIZZLE_OP (wxzz) SHADER_SWIZZLE_OP (wxzw) SHADER_SWIZZLE_OP (wxw ) SHADER_SWIZZLE_OP (wxwx) SHADER_SWIZZLE_OP (wxwy) SHADER_SWIZZLE_OP (wxwz) SHADER_SWIZZLE_OP (wxww) SHADER_SWIZZLE_OP (wy ) SHADER_SWIZZLE_OP (wyx ) SHADER_SWIZZLE_OP (wyxx) SHADER_SWIZZLE_OP (wyxy) SHADER_SWIZZLE_OP (wyxz) SHADER_SWIZZLE_OP (wyxw) SHADER_SWIZZLE_OP (wyy ) SHADER_SWIZZLE_OP (wyyx) SHADER_SWIZZLE_OP (wyyy) SHADER_SWIZZLE_OP (wyyz) SHADER_SWIZZLE_OP (wyyw) SHADER_SWIZZLE_OP (wyz ) SHADER_SWIZZLE_OP (wyzx) SHADER_SWIZZLE_OP (wyzy) SHADER_SWIZZLE_OP (wyzz) SHADER_SWIZZLE_OP (wyzw) SHADER_SWIZZLE_OP (wyw ) SHADER_SWIZZLE_OP (wywx) SHADER_SWIZZLE_OP (wywy) SHADER_SWIZZLE_OP (wywz) SHADER_SWIZZLE_OP (wyww) SHADER_SWIZZLE_OP (wz ) SHADER_SWIZZLE_OP (wzx ) SHADER_SWIZZLE_OP (wzxx) SHADER_SWIZZLE_OP (wzxy) SHADER_SWIZZLE_OP (wzxz) SHADER_SWIZZLE_OP (wzxw) SHADER_SWIZZLE_OP (wzy ) SHADER_SWIZZLE_OP (wzyx) SHADER_SWIZZLE_OP (wzyy) SHADER_SWIZZLE_OP (wzyz) SHADER_SWIZZLE_OP (wzyw) SHADER_SWIZZLE_OP (wzz ) SHADER_SWIZZLE_OP (wzzx) SHADER_SWIZZLE_OP (wzzy) SHADER_SWIZZLE_OP (wzzz) SHADER_SWIZZLE_OP (wzzw) SHADER_SWIZZLE_OP (wzw ) SHADER_SWIZZLE_OP (wzwx) SHADER_SWIZZLE_OP (wzwy) SHADER_SWIZZLE_OP (wzwz) SHADER_SWIZZLE_OP (wzww) SHADER_SWIZZLE_OP (ww ) SHADER_SWIZZLE_OP (wwx ) SHADER_SWIZZLE_OP (wwxx) SHADER_SWIZZLE_OP (wwxy) SHADER_SWIZZLE_OP (wwxz) SHADER_SWIZZLE_OP (wwxw) SHADER_SWIZZLE_OP (wwy ) SHADER_SWIZZLE_OP (wwyx) SHADER_SWIZZLE_OP (wwyy) SHADER_SWIZZLE_OP (wwyz) SHADER_SWIZZLE_OP (wwyw) SHADER_SWIZZLE_OP (wwz ) SHADER_SWIZZLE_OP (wwzx) SHADER_SWIZZLE_OP (wwzy) SHADER_SWIZZLE_OP (wwzz) SHADER_SWIZZLE_OP (wwzw) SHADER_SWIZZLE_OP (www ) SHADER_SWIZZLE_OP (wwwx) SHADER_SWIZZLE_OP (wwwy) SHADER_SWIZZLE_OP (wwwz) SHADER_SWIZZLE_OP (wwww) # undef SHADER_SWIZZLE_OP # define SHADER_FUNC0_(name, glname) \ template \ inline const sl_expr \ name () \ { \ return sl_func0 (#glname " ("); \ } # define SHADER_FUNC0(name) SHADER_FUNC0_(name,name) # define SHADER_FUNC1_(name, glname) \ template \ inline const sl_expr< sl_func1::T> > \ name (const A &a) \ { \ return sl_func1::T> (#glname " (", sl_convert::convert (a));\ } # define SHADER_FUNC1(name) SHADER_FUNC1_(name,name) # define SHADER_FUNC2_(name, glname) \ template \ inline const sl_expr< sl_func2::T, typename sl_convert::T> > \ name (const A &a, const B &b) \ { \ return sl_func2::T, typename sl_convert::T> (#glname " (", sl_convert::convert (a), sl_convert::convert (b));\ } # define SHADER_FUNC2(name) SHADER_FUNC2_(name,name) # define SHADER_FUNC3_(name, glname) \ template \ inline const sl_expr< sl_func3::T, typename sl_convert::T, typename sl_convert::T> > \ name (const A &a, const B &b, const C &c) \ { \ return sl_func3::T, typename sl_convert::T, typename sl_convert::T> (#glname " (", sl_convert::convert (a), sl_convert::convert (b), sl_convert::convert (c));\ } # define SHADER_FUNC3(name) SHADER_FUNC3_(name,name) # define SHADER_FUNC4_(name, glname) \ template \ inline const sl_expr< sl_func4::T, typename sl_convert::T, typename sl_convert::T, typename sl_convert::T > > \ name (const A &a, const B &b, const C &c, const D &d) \ { \ return sl_func4::T, typename sl_convert::T, typename sl_convert::T, typename sl_convert::T> (#glname " (", sl_convert::convert (a), sl_convert::convert (b), sl_convert::convert (c), sl_convert::convert (d));\ } # define SHADER_FUNC4(name) SHADER_FUNC4_(name,name) SHADER_FUNC1 (abs) SHADER_FUNC1 (acos) SHADER_FUNC1 (all) SHADER_FUNC1 (any) SHADER_FUNC1 (asin) SHADER_FUNC1 (atan) SHADER_FUNC2 (atan) SHADER_FUNC1 (ceil) SHADER_FUNC3 (clamp) SHADER_FUNC1 (cos) SHADER_FUNC2 (cross) SHADER_FUNC1 (dFdx) SHADER_FUNC1 (dFdy) SHADER_FUNC1 (degrees) SHADER_FUNC2 (distance) SHADER_FUNC2 (dot) SHADER_FUNC2_(equal, equal) SHADER_FUNC1 (exp) SHADER_FUNC1 (exp2) SHADER_FUNC3 (faceforward) SHADER_FUNC1 (floor) SHADER_FUNC1 (fract) SHADER_FUNC0 (ftransform) SHADER_FUNC1 (fwidth) SHADER_FUNC2_(greater_than_equal, greaterThanEqual) SHADER_FUNC2_(greater_then, greaterThan) SHADER_FUNC1 (inversesqrt) SHADER_FUNC1 (length) SHADER_FUNC2_(less_than, lessThan) SHADER_FUNC2_(less_than_equal, lessThanEqual) SHADER_FUNC1 (log) SHADER_FUNC1 (log2) SHADER_FUNC2_(matrix_comp_mult, matrixCompMult) SHADER_FUNC2 (max) SHADER_FUNC2 (min) SHADER_FUNC3 (mix) SHADER_FUNC2 (mod) SHADER_FUNC1 (noise1) SHADER_FUNC1 (noise2) SHADER_FUNC1 (noise3) SHADER_FUNC1 (noise4) SHADER_FUNC1 (normalize) SHADER_FUNC1 (gl_not) // TODO SHADER_FUNC2_(notequal, notEqual) SHADER_FUNC2 (pow) SHADER_FUNC1 (radians) SHADER_FUNC2 (reflect) SHADER_FUNC3 (refract) SHADER_FUNC2_(shadow_1d, shadow1D) SHADER_FUNC3_(shadow_1d, shadow1D) SHADER_FUNC3_(shadow_1d_lod, shadow1DLod) SHADER_FUNC2_(shadow_1d_proj, shadow1DProj) SHADER_FUNC3_(shadow_1d_proj, shadow1DProj) SHADER_FUNC3_(shadow_1d_proj_lod, shadow1DProjLod) SHADER_FUNC2_(shadow_2d, shadow2D) SHADER_FUNC3_(shadow_2d, shadow2D) SHADER_FUNC3_(shadow_2d_lod, shadow2DLod) SHADER_FUNC2_(shadow_2d_proj, shadow2DProj) SHADER_FUNC3_(shadow_2d_proj, shadow2DProj) SHADER_FUNC3_(shadow_2d_proj_lod, shadow2DProjLod) SHADER_FUNC1 (sign) SHADER_FUNC1 (sin) SHADER_FUNC3 (smoothstep) SHADER_FUNC1 (sqrt) SHADER_FUNC2 (step) SHADER_FUNC1 (tan) SHADER_FUNC2_(texture_1d, texture1D) SHADER_FUNC3_(texture_1d, texture1D) SHADER_FUNC3_(texture_1d_lod, texture1DLod) SHADER_FUNC2_(texture_1d_proj, texture1DProj) SHADER_FUNC3_(texture_1d_proj, texture1DProj) SHADER_FUNC3_(texture_1d_proj_lod, texture1DProjLod) SHADER_FUNC2_(texture_2d, texture2D) SHADER_FUNC3_(texture_2d, texture2D) SHADER_FUNC3_(texture_2d_lod, texture2DLod) SHADER_FUNC2_(texture_2d_proj, texture2DProj) SHADER_FUNC3_(texture_2d_proj, texture2DProj) SHADER_FUNC3_(texture_2d_proj_lod, texture2DProjLod) SHADER_FUNC2_(texture_3d, texture3D) SHADER_FUNC3_(texture_3d, texture3D) SHADER_FUNC3_(texture_3d_lod, texture3DLod) SHADER_FUNC2_(texture_3d_proj, texture3DProj) SHADER_FUNC3_(texture_3d_proj, texture3DProj) SHADER_FUNC3_(texture_3d_proj_lod, texture3DProjLod) SHADER_FUNC2_(texture_cube, textureCube) SHADER_FUNC3_(texture_cube, textureCube) SHADER_FUNC3_(texture_cude_lod, textureCubeLod) SHADER_FUNC1 (vec2) SHADER_FUNC2 (vec2) SHADER_FUNC1 (vec3) SHADER_FUNC2 (vec3) SHADER_FUNC3 (vec3) SHADER_FUNC1 (vec4) SHADER_FUNC2 (vec4) SHADER_FUNC3 (vec4) SHADER_FUNC4 (vec4) SHADER_FUNC1 (mat2) SHADER_FUNC2 (mat2) SHADER_FUNC1 (mat3) SHADER_FUNC2 (mat3) SHADER_FUNC3 (mat3) SHADER_FUNC1 (mat4) SHADER_FUNC2 (mat4) SHADER_FUNC3 (mat4) SHADER_FUNC4 (mat4) # undef SHADER_FUNC0 # undef SHADER_FUNC0_ # undef SHADER_FUNC1 # undef SHADER_FUNC1_ # undef SHADER_FUNC2 # undef SHADER_FUNC2_ # undef SHADER_FUNC3 # undef SHADER_FUNC3_ # undef SHADER_FUNC4 # undef SHADER_FUNC5_ } #if 0 template inline const sl_expr< sl_ternary::T, typename sl_convert::T, typename sl_convert::T> > ifelse (const A &a, const B &b, const C &c) { return sl_ternary::T, typename sl_convert::T, typename sl_convert::T> (sl_convert::convert (a), sl_convert::convert (b), sl_convert::convert (c)); } #endif void debdebdebdebug ();//D } #endif #include "shader_vars.h"