ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/shader.h
Revision: 1.18
Committed: Fri Oct 29 17:21:54 2004 UTC (19 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.17: +27 -7 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #ifndef SHADER_H
2     #define SHADER_H
3    
4 root 1.17 #include <cassert>
5    
6 root 1.16 #include <map>
7 root 1.1 #include <sstream>
8    
9     #include "opengl.h"
10     #include "util.h"
11    
12     namespace shader {
13    
14     using namespace std;
15    
16     const int NAMELEN = 32;
17    
18     template<class T>
19     struct sl_expr {
20     const T t;
21     sl_expr (const T &t) : t(t) { }
22     void operator ()() const { t (); }
23 root 1.5 template<typename expr>
24     const sl_expr &operator =(const expr &e) const;
25 root 1.1 };
26    
27     template<class A, class B>
28     struct sl_concat2
29     {
30     const A a; const B b;
31     sl_concat2 (const A &a, const B &b) : a(a), b(b) { }
32     void operator ()() const { a (); b (); }
33     };
34    
35     template<class A, class B, class C>
36     struct sl_concat3
37     {
38     const A a; const B b; const C c;
39     sl_concat3 (const A &a, const B &b, const C &c) : a(a), b(b), c(c) { }
40     void operator ()() const { a (); b (); c (); }
41     };
42    
43     template<class A, class B, class C, class D>
44     struct sl_concat4
45     {
46     const A a; const B b; const C c; const D d;
47     sl_concat4 (const A &a, const B &b, const C &c, const D &d) : a(a), b(b), c(c), d(d) { }
48     void operator ()() const { a (); b (); c (); d(); }
49     };
50    
51 root 1.7 struct sl_func0
52     {
53     const char *name_par;
54     sl_func0 (const char *name_par) : name_par(name_par) { }
55     void begin () const;
56 root 1.8 void comma () const;
57 root 1.7 void end () const;
58     void operator ()() const
59     {
60     begin ();
61     end ();
62     }
63     };
64    
65     template<class A>
66     struct sl_func1 : sl_func0
67     {
68     const A a;
69     sl_func1 (const char *name, const A &a) : sl_func0(name), a(a) { }
70     void operator ()() const { begin (); a (); end (); }
71     };
72    
73     template<class A, class B>
74     struct sl_func2 : sl_func0
75     {
76     const A a; const B b;
77     sl_func2 (const char *name, const A &a, const B &b) : sl_func0(name), a(a), b(b) { }
78 root 1.8 void operator ()() const { begin (); a (); comma (); b (); end (); }
79 root 1.7 };
80    
81     template<class A, class B, class C>
82     struct sl_func3 : sl_func0
83     {
84     const A a; const B b; const C c;
85     sl_func3 (const char *name, const A &a, const B &b, const C &c) : sl_func0(name), a(a), b(b), c(c) { }
86 root 1.8 void operator ()() const { begin (); a (); comma (); b (); comma (); c (); end (); }
87 root 1.7 };
88    
89     template<class A, class B, class C, class D>
90     struct sl_func4 : sl_func0
91     {
92     const A a; const B b; const C c; const D d;
93     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) { }
94 root 1.8 void operator ()() const { begin (); a (); comma (); b (); comma (); c (); comma (); d(); end (); }
95 root 1.7 };
96    
97 root 1.1 class refcounted
98     {
99     template<class type> friend class ref;
100     mutable int refcnt;
101     void refcnt_inc () const { refcnt++; }
102 root 1.6 void refcnt_dec () const;
103 root 1.1 public:
104     refcounted () : refcnt(0) { }
105     virtual ~refcounted ();
106     };
107    
108     template<class type>
109     struct ref
110     {
111     type *p;
112    
113     ref ()
114     {
115     p = new type;
116     p->refcnt_inc ();
117     }
118    
119     ref (type &d)
120     {
121     d.refcnt_inc ();
122     p = &d;
123     }
124    
125     ref (const ref &r)
126     {
127     r.p->refcnt_inc ();
128     p = r.p;
129     }
130    
131     template<class type2>
132     ref (const ref<type2> &r)
133     {
134     r.p->refcnt_inc ();
135     p = r.p;
136     }
137    
138     ~ref ()
139     {
140     p->refcnt_dec ();
141     }
142    
143     ref &operator =(type &d)
144     {
145     d.refcnt_inc ();
146     p->refcnt_dec ();
147     p = &d;
148     return *this;
149     }
150    
151     type *operator ->() const { return p; }
152    
153     operator type &() const { return *p; }
154    
155     template<class type2>
156     bool operator ==(const ref<type2> b) const
157     {
158     return (void *)p == (void *)b.p;
159     }
160     };
161    
162     // ref with auto-construct
163     template<class type>
164     struct auto_ref0 : ref<type>
165     {
166     auto_ref0 () : ref<type> (*new type ()) { }
167     };
168    
169     template<class type, typename arg1>
170     struct auto_ref1 : ref<type>
171     {
172     auto_ref1 (arg1 a) : ref<type> (*new type (a)) { }
173     };
174    
175     template<class type>
176     struct auto_lvalue_ref0 : ref<type>
177     {
178     auto_lvalue_ref0 () : ref<type> (*new type ()) { }
179     template<typename expr>
180     const auto_lvalue_ref0 &operator =(const expr &e) const;
181     };
182    
183     template<class type, typename arg1>
184     struct auto_lvalue_ref1 : ref<type>
185     {
186     auto_lvalue_ref1 (arg1 a) : ref<type> (*new type (a)) { }
187     template<class expr>
188     const auto_lvalue_ref1 &operator =(const expr &e) const;
189     };
190    
191     extern const char str_float [];
192     extern const char str_vec2 [];
193     extern const char str_vec3 [];
194     extern const char str_vec4 [];
195     extern const char str_mat2 [];
196     extern const char str_mat3 [];
197     extern const char str_mat4 [];
198    
199     extern const char str_sampler_1d [];
200     extern const char str_sampler_1d_shadow [];
201     extern const char str_sampler_2d [];
202     extern const char str_sampler_2d_shadow [];
203     extern const char str_sampler_2d_rect [];
204     extern const char str_sampler_2d_rect_shadow [];
205     extern const char str_sampler_3d [];
206     extern const char str_sampler_3d_rect [];
207     extern const char str_sampler_cube [];
208    
209     typedef ref<struct var_i> var;
210     typedef ref<struct uniform_i> uniform;
211    
212     struct shader_builder
213     {
214     vector<uniform> refs; // uniform parameters
215     vector<var> temps; // temporary variables
216     vector<var> streams; // varying inputs & outputs
217    
218     ostringstream source;
219    
220     template<typename type>
221     shader_builder &operator <<(const type &t)
222     {
223     source << t;
224     return *this;
225     }
226     };
227    
228     struct fragment_i : refcounted
229     {
230     virtual void build (shader_builder &b) = 0;
231     };
232    
233     typedef ref<fragment_i> fragment;
234    
235     struct lvalue_i : fragment_i
236     {
237     };
238    
239     // a simple predeclared variable with unspecified type
240 root 1.14 struct gluvar_i : lvalue_i
241 root 1.1 {
242     const char *name;
243     void build (shader_builder &b);
244 root 1.14 gluvar_i (const char *name) : name (name) { }
245 root 1.1 };
246    
247 root 1.14 typedef auto_lvalue_ref1<gluvar_i, const char *> gluvar;
248 root 1.1
249     struct var_i : lvalue_i
250     {
251     static unsigned int next_id;
252    
253     char name[NAMELEN];
254     const char *typestr;
255    
256     void build (shader_builder &b);
257     virtual void build_decl (ostringstream &b);
258    
259     var_i (const char *typestr);
260     ~var_i ();
261     };
262    
263     struct uniform_i : var_i
264     {
265     void build (shader_builder &b);
266     void build_decl (ostringstream &b);
267 root 1.13
268 root 1.16 GLint location ();
269 root 1.13
270 root 1.1 uniform_i (const char *strtype);
271     };
272    
273 root 1.16 template<int dimension, typename gltype, const char *strtype>
274 root 1.1 struct uniform2_i : uniform_i
275     {
276     gltype data[dimension];
277    
278 root 1.16 uniform2_i () : uniform_i (strtype) { }
279     };
280    
281     template<int dimension, const char *strtype>
282     struct uniform_f_i : uniform2_i<dimension, GLfloat, strtype>
283     {
284     };
285    
286     struct uniform_1f_i : uniform_f_i<1, str_float> {
287     void set (GLfloat v)
288 root 1.1 {
289 root 1.16 data[0] = v;
290 root 1.18
291     GLint loc = location ();
292     if (loc >= 0) // workaround for buggy drivers
293     glUniform1fvARB (loc, 1, data);
294 root 1.16 }
295     };
296 root 1.13
297 root 1.16 struct uniform_2f_i : uniform_f_i<2, str_vec2> {
298     void set (GLfloat x, GLfloat y)
299     {
300     data[0] = x;
301     data[1] = y;
302 root 1.18
303     GLint loc = location ();
304     if (loc >= 0) // workaround for buggy drivers
305     glUniform2fvARB (loc, 1, data);
306 root 1.1 }
307    
308 root 1.16 void set (const vec2 &v) { set (v.x, v.y); }
309 root 1.1 };
310    
311 root 1.16 struct uniform_3f_i : uniform_f_i<3, str_vec3> {
312     void set (GLfloat x, GLfloat y, GLfloat z)
313     {
314     data[0] = x;
315     data[1] = y;
316     data[2] = z;
317 root 1.18
318     GLint loc = location ();
319     if (loc >= 0) // workaround for buggy drivers
320     glUniform3fvARB (loc, 1, data);
321 root 1.16 }
322    
323     void set (const vec3 &v) { set (v.x, v.y, v.z); }
324 root 1.1 };
325    
326 root 1.16 struct uniform_4f_i : uniform_f_i<4, str_vec4> {
327     void set (GLfloat x, GLfloat y, GLfloat z, GLfloat w)
328 root 1.1 {
329 root 1.16 data[0] = x;
330     data[1] = y;
331     data[2] = z;
332     data[3] = w;
333    
334 root 1.18 GLint loc = location ();
335     if (loc >= 0) // workaround for buggy drivers
336     glUniform4fvARB (loc, 1, data);
337 root 1.1 }
338 root 1.16
339     void set (const vec4 &v) { set (v.x, v.y, v.z, v.w); }
340 root 1.1 };
341    
342 root 1.16 struct uniform_matrix_2f_i : uniform_f_i< 4, str_mat2>
343     {
344     void set (const GLfloat d[4])
345     {
346     memcpy (data, d, 4 * sizeof (GLfloat));
347 root 1.18
348     GLint loc = location ();
349     if (loc >= 0) // workaround for buggy drivers
350     glUniformMatrix2fvARB (loc, 1, 0, data);
351 root 1.16 }
352     };
353 root 1.1
354 root 1.16 struct uniform_matrix_3f_i : uniform_f_i< 9, str_mat3>
355     {
356     void set (const GLfloat d[9])
357 root 1.1 {
358 root 1.16 memcpy (data, d, 9 * sizeof (GLfloat));
359 root 1.18
360     GLint loc = location ();
361     if (loc >= 0) // workaround for buggy drivers
362     glUniformMatrix3fvARB (loc, 1, 0, data);
363 root 1.1 }
364     };
365    
366 root 1.16 struct uniform_matrix_4f_i : uniform_f_i<16, str_mat4>
367     {
368     void set (const gl::matrix &m)
369 root 1.1 {
370     memcpy (data, m.data, 16 * sizeof (GLfloat));
371 root 1.18
372     GLint loc = location ();
373     if (loc >= 0) // workaround for buggy drivers
374     glUniformMatrix4fvARB (loc, 1, 0, data);
375 root 1.1 }
376     };
377    
378 root 1.2 template<class var_i>
379     struct var_ref : ref<var_i>
380 root 1.1 {
381 root 1.2 var_ref (const char *glname = 0)
382     : ref<var_i> (*new var_i)
383 root 1.1 {
384     if (glname)
385     strcpy ((*this)->name, glname);
386     }
387     };
388    
389 root 1.2 typedef var_ref<uniform_1f_i> uniform_1f;
390     typedef var_ref<uniform_2f_i> uniform_2f;
391     typedef var_ref<uniform_3f_i> uniform_3f;
392     typedef var_ref<uniform_4f_i> uniform_4f;
393     typedef var_ref<uniform_matrix_2f_i> uniform_matrix_2f;
394     typedef var_ref<uniform_matrix_3f_i> uniform_matrix_3f;
395     typedef var_ref<uniform_matrix_4f_i> uniform_matrix_4f;
396 root 1.1
397     struct stream_i : var_i
398     {
399     void build (shader_builder &b);
400     void build_decl (ostringstream &b);
401 root 1.4 stream_i (const char *strtype);
402 root 1.1 };
403    
404     template<int dimension, GLenum gltype, const char *strtype>
405     struct varying_i : stream_i
406     {
407 root 1.4 varying_i () : stream_i (strtype) { }
408 root 1.1
409     void set (GLsizei stride, GLvoid *ptr)
410     {
411     //cgGLSetParameterPointer (param, dimension, gltype, stride, ptr);
412     }
413     };
414    
415     template<int dimension, const char *strtype>
416     struct varying_f_i : varying_i<dimension, GL_FLOAT, strtype>
417     {
418     void set (const gl::vertex_buffer_object &vb, GLint offset)
419     {
420     varying_i<dimension, GL_FLOAT, strtype>::set (gl::format_stride (vb.format), (GLvoid *)(long)offset);
421     }
422     };
423    
424     struct varying_1f_i : varying_f_i<1, str_float>
425     {
426     };
427    
428     struct varying_2f_i : varying_f_i<2, str_vec2>
429     {
430     void set_t (const gl::vertex_buffer_object &vb)
431     {
432     set (vb, gl::format_offset_t (vb.format));
433     }
434     };
435    
436     struct varying_3f_i : varying_f_i<3, str_vec3>
437     {
438     void set_p (const gl::vertex_buffer_object &vb)
439     {
440     set (vb, gl::format_offset_p (vb.format));
441     }
442    
443     void set_n (const gl::vertex_buffer_object &vb)
444     {
445     set (vb, gl::format_offset_n (vb.format));
446     }
447     };
448    
449     struct varying_4f_i : varying_f_i<4, str_vec4>
450     {
451     };
452    
453 root 1.2 typedef var_ref<varying_1f_i> varying_1f;
454     typedef var_ref<varying_2f_i> varying_2f;
455     typedef var_ref<varying_3f_i> varying_3f;
456     typedef var_ref<varying_4f_i> varying_4f;
457 root 1.1
458     struct temporary_i : var_i
459     {
460     void build (shader_builder &b);
461    
462     temporary_i (const char *strtype);
463     };
464    
465     template<const char *strtype>
466     struct temp_ref : ref<temporary_i>
467     {
468     temp_ref ()
469     : ref<temporary_i> (*new temporary_i (strtype))
470     {
471     }
472    
473 root 1.10 #if 0
474     template<typename expr>
475     temp_ref (const expr &e)
476     : ref<temporary_i> (*new temporary_i (strtype))
477     {
478     (*this) = e;
479     }
480     #endif
481    
482 root 1.1 template<typename expr>
483     const temp_ref &operator =(const expr &e) const;
484     };
485    
486     typedef temp_ref<str_float> temp_1f;
487     typedef temp_ref<str_vec2> temp_2f;
488     typedef temp_ref<str_vec3> temp_3f;
489     typedef temp_ref<str_vec4> temp_4f;
490     typedef temp_ref<str_mat2> temp_matrix_2f;
491     typedef temp_ref<str_mat3> temp_matrix_3f;
492     typedef temp_ref<str_mat4> temp_matrix_4f;
493    
494 root 1.17 struct texture_units
495     {
496     static GLenum units[8];
497     static int unit_count;
498    
499     static GLenum get_texture_unit ()
500     {
501     assert (unit_count);
502     return units[--unit_count];
503     }
504    
505     static void put_texture_unit (GLenum unit)
506     {
507     assert (unit_count < 8);
508     units[unit_count++] = unit;
509     }
510     };
511    
512 root 1.1 template<GLenum gltype, const char *strtype>
513 root 1.17 struct sampler_i : uniform_i, texture_units
514 root 1.1 {
515 root 1.17 GLenum unit;
516     GLuint name;
517 root 1.1
518     void update ()
519     {
520     if (dirty)
521     {
522     //cgGLSetTextureParameter (param, texture);
523     dirty = false;
524     }
525     }
526    
527 root 1.17 void enable ()
528     {
529     #if DEBUG
530     assert (unit < GL_TEXTURE0);
531     #endif
532    
533     unit = get_texture_unit ();
534     glActiveTexture (unit);
535     glBindTexture (gltype, name);
536     }
537    
538     void disable ()
539 root 1.1 {
540 root 1.17 put_texture_unit (unit);
541     #if DEBUG
542     assert (unit >= GL_TEXTURE0);
543     unit = GL_ZERO;
544     #endif
545 root 1.1 }
546    
547 root 1.17 template<class bufferclass, class vectype>
548     void set (const gl::vertex_buffer_object &vb, const vectype bufferclass::*vp)
549     {
550     glTexCoordPointer (
551     vector_traits<vectype>::dimension (),
552     gltype_traits<typename vector_traits<vectype>::basetype>::gltype,
553     sizeof (bufferclass),
554     (GLvoid)(char bufferclass::*)vp
555     );
556     }
557    
558     sampler_i (GLuint texture)
559     : uniform_i (strtype)
560     , name (texture)
561     #if DEBUG
562     , unit (GL_ZERO)
563     #endif
564     {
565     }
566     };
567    
568     struct sampler_1d_i : sampler_i<GL_TEXTURE_1D, str_sampler_1d>
569     {
570     sampler_1d_i (GLuint texture) : sampler_i<GL_TEXTURE_1D, str_sampler_1d> (texture) { }
571 root 1.1 };
572    
573 root 1.17 struct sampler_1d_shadow_i : sampler_i<GL_TEXTURE_1D, str_sampler_1d_shadow>
574 root 1.1 {
575 root 1.17 sampler_1d_shadow_i (GLuint texture) : sampler_i<GL_TEXTURE_1D, str_sampler_1d_shadow> (texture) { }
576 root 1.1 };
577    
578 root 1.17 struct sampler_2d_i : sampler_i<GL_TEXTURE_2D, str_sampler_2d>
579 root 1.1 {
580 root 1.17 sampler_2d_i (GLuint texture) : sampler_i<GL_TEXTURE_2D, str_sampler_2d> (texture) { }
581 root 1.1 };
582    
583 root 1.17 struct sampler_2d_shadow_i : sampler_i<GL_TEXTURE_2D, str_sampler_2d_shadow>
584 root 1.1 {
585 root 1.17 sampler_2d_shadow_i (GLuint texture) : sampler_i<GL_TEXTURE_2D, str_sampler_2d_shadow> (texture) { }
586 root 1.1 };
587    
588 root 1.17 struct sampler_2d_rect_i : sampler_i<GL_TEXTURE_2D, str_sampler_2d_rect>
589 root 1.1 {
590 root 1.17 sampler_2d_rect_i (GLuint texture) : sampler_i<GL_TEXTURE_2D, str_sampler_2d_rect> (texture) { }
591 root 1.1 };
592    
593 root 1.17 struct sampler_2d_rect_shadow_i : sampler_i<GL_TEXTURE_2D, str_sampler_2d_rect_shadow>
594 root 1.1 {
595 root 1.17 sampler_2d_rect_shadow_i (GLuint texture) : sampler_i<GL_TEXTURE_2D, str_sampler_2d_rect_shadow> (texture) { }
596 root 1.1 };
597    
598 root 1.17 struct sampler_3d_i : sampler_i<GL_TEXTURE_3D, str_sampler_3d>
599 root 1.1 {
600 root 1.17 sampler_3d_i (GLuint texture) : sampler_i<GL_TEXTURE_3D, str_sampler_3d> (texture) { }
601 root 1.1 };
602    
603 root 1.17 struct sampler_3d_rect_i : sampler_i<GL_TEXTURE_3D, str_sampler_3d_rect>
604 root 1.1 {
605 root 1.17 sampler_3d_rect_i (GLuint texture) : sampler_i<GL_TEXTURE_3D, str_sampler_3d_rect> (texture) { }
606 root 1.1 };
607    
608 root 1.17 struct sampler_cube_i : sampler_i<GL_TEXTURE_CUBE_MAP, str_sampler_cube>
609 root 1.1 {
610 root 1.17 sampler_cube_i (GLuint texture) : sampler_i<GL_TEXTURE_CUBE_MAP, str_sampler_cube> (texture) { }
611 root 1.1 };
612    
613 root 1.17 template<class sampler_i>
614     struct sampler_ref : auto_ref1<sampler_i, GLuint>
615 root 1.1 {
616 root 1.17 sampler_ref (GLuint texture)
617     : auto_ref1<sampler_i, GLuint> (texture)
618     {
619     }
620 root 1.1 };
621    
622 root 1.17 typedef sampler_ref<sampler_1d_i> sampler_1d;
623     typedef sampler_ref<sampler_1d_shadow_i> sampler_1d_shadow;
624     typedef sampler_ref<sampler_2d_i> sampler_2d;
625     typedef sampler_ref<sampler_2d_shadow_i> sampler_2d_shadow;
626     typedef sampler_ref<sampler_2d_rect_i> sampler_2d_rect;
627     typedef sampler_ref<sampler_2d_rect_shadow_i> sampler_2d_rect_shadow;
628     typedef sampler_ref<sampler_3d_i> sampler_3d;
629     typedef sampler_ref<sampler_3d_rect_i> sampler_3d_rect;
630     typedef sampler_ref<sampler_cube_i> sampler_cube;
631 root 1.1
632     struct fragment_const_string_i : fragment_i
633     {
634     const char *str;
635    
636     void build (shader_builder &b);
637    
638     fragment_const_string_i (const char *str) : str(str) { }
639     };
640    
641     typedef auto_ref1<fragment_const_string_i, const char *> fragment_const_string;
642    
643     struct fragment_string_i : fragment_i
644     {
645     char *str;
646    
647     void build (shader_builder &b);
648    
649     fragment_string_i (const char *str) : str (strdup (str)) { }
650     ~fragment_string_i ();
651     };
652    
653     struct fragment_vector_i : fragment_i, vector<fragment>
654     {
655     void build (shader_builder &b);
656    
657     template<class fragment_i>
658     void append (const ref<fragment_i> &f)
659     {
660     push_back (f);
661     }
662    
663 root 1.5 template<class expr>
664     void append (const sl_expr<expr> &e)
665     {
666     e ();
667     }
668    
669 root 1.1 void append_const (const char *s)
670     {
671     push_back (*new fragment_const_string_i (s));
672     }
673    
674     void append_string (const char *s)
675     {
676     push_back (*new fragment_string_i (s));
677     }
678     };
679    
680     typedef ref<fragment_vector_i> fragment_vector;
681    
682     struct shader_object_i : fragment_vector_i
683     {
684     GLenum type;
685     GLuint id; // GLhandleARB, but 2.0 will use uint
686    
687     string source ();
688     void compile ();
689     void start ();
690     void stop ();
691    
692     shader_object_i (GLenum type);
693     ~shader_object_i ();
694     };
695    
696     extern shader_object_i *cur; // record actions to this shader
697    
698     template<GLenum type>
699     struct shader_object : ref<shader_object_i>
700     {
701     shader_object ()
702     : ref<shader_object_i> (*new shader_object_i (type))
703     {
704     }
705     };
706    
707     typedef shader_object<GL_VERTEX_SHADER_ARB> vertex_shader;
708     typedef shader_object<GL_FRAGMENT_SHADER_ARB> fragment_shader;
709    
710 root 1.11 struct program_object
711     {
712 root 1.16 static struct program_object *cur; // currently bound program
713    
714 root 1.11 GLuint id;
715    
716     vertex_shader vsh;
717     fragment_shader fsh;
718    
719 root 1.16 map<uniform_i *,GLint> uloc; // uniform location
720    
721 root 1.11 program_object ();
722     ~program_object ();
723    
724     void link ();
725 root 1.16
726 root 1.17 void enable ()
727 root 1.16 {
728     glUseProgramObjectARB (id);
729     cur = this;
730     }
731    
732 root 1.17 void disable ()
733 root 1.16 {
734     glUseProgramObjectARB (0);
735     cur = 0;
736     }
737 root 1.11 };
738    
739 root 1.3 template<typename T>
740     struct sl_append
741     {
742     T t;
743    
744     sl_append (const T &t) : t(t) { }
745    
746     void operator ()() const
747     {
748     cur->push_back (t);
749     }
750     };
751    
752     template<int length>
753     struct sl_string
754     {
755     char str[length];
756    
757     void operator ()() const
758     {
759     cur->push_back (*new fragment_string_i (str));
760     }
761     };
762    
763     struct sl_float
764     {
765     const GLfloat c;
766    
767     sl_float (GLfloat c) : c(c) { }
768    
769 root 1.6 void operator ()() const;
770 root 1.3 };
771    
772     template<class A, class B>
773     inline sl_expr< sl_concat2<A, B> >
774     concat (const A &a, const B &b)
775     {
776     typedef sl_concat2<A, B> expr;
777     return sl_expr<expr> (expr (a, b));
778     }
779    
780     template<class A, class B, class C>
781     inline sl_expr< sl_concat3<A, B, C> >
782     concat (const A &a, const B &b, const C &c)
783     {
784     typedef sl_concat3<A, B, C> expr;
785     return sl_expr<expr> (expr (a, b, c));
786     }
787    
788     template<class A, class B, class C, class D>
789     inline sl_expr< sl_concat4<A, B, C, D> >
790     concat (const A &a, const B &b, const C &c, const D &d)
791     {
792     typedef sl_concat4<A, B, C, D> expr;
793     return sl_expr<expr> (expr (a, b, c, d));
794     }
795    
796     template<typename expr>
797     struct sl_convert
798     {
799     typedef sl_expr<expr> T;
800     static inline const T &convert (const T &e)
801     {
802     return e;
803     }
804     };
805    
806     template<>
807     struct sl_convert<GLfloat>
808     {
809     typedef sl_expr<sl_float> T;
810     static inline const T convert (GLfloat f)
811     {
812     return sl_float (f);
813     }
814     };
815    
816     template<>
817 root 1.5 struct sl_convert<GLdouble>
818     {
819     typedef sl_expr<sl_float> T;
820     static inline const T convert (GLdouble d)
821     {
822     return sl_float (d);
823     }
824     };
825    
826     template<>
827     struct sl_convert<GLint>
828     {
829     typedef sl_expr<sl_float> T;
830     static inline const T convert (GLint i)
831     {
832     return sl_float (i);
833     }
834     };
835    
836     template<>
837 root 1.6 struct sl_convert<vec2>
838     {
839     typedef sl_expr< sl_string<60> > T;
840     static const T convert (const vec2 &v);
841     };
842    
843     template<>
844 root 1.3 struct sl_convert<vec3>
845     {
846 root 1.6 typedef sl_expr< sl_string<80> > T;
847     static const T convert (const vec3 &v);
848 root 1.3 };
849    
850     template<>
851     struct sl_convert<vec4>
852     {
853 root 1.6 typedef sl_expr< sl_string<100> > T;
854     static const T convert (const vec4 &v);
855 root 1.3 };
856    
857     template<>
858     template<class V>
859     struct sl_convert< var_ref<V> >
860     {
861     typedef sl_expr< sl_append< var_ref<V> > > T;
862     static inline const T convert (const var_ref<V> &v)
863     {
864     return sl_append< var_ref<V> > (v);
865     }
866     };
867    
868     template<>
869 root 1.14 struct sl_convert<gluvar>
870 root 1.3 {
871 root 1.14 typedef sl_expr< sl_append<gluvar> > T;
872     static inline const T convert (const gluvar &v)
873 root 1.3 {
874 root 1.14 return sl_append<gluvar> (v);
875 root 1.3 }
876     };
877    
878     template<>
879     template<const char *strtype>
880     struct sl_convert< temp_ref<strtype> >
881     {
882     typedef sl_expr< sl_append< temp_ref<strtype> > > T;
883     static inline const T convert (const temp_ref<strtype> &v)
884     {
885     return sl_expr< sl_append< temp_ref<strtype> > >(
886     sl_append< temp_ref<strtype> > (v)
887     );
888     }
889     };
890    
891 root 1.17 template<>
892     template<class sampler_i>
893     struct sl_convert< sampler_ref<sampler_i> >
894     {
895     typedef sl_expr< sl_append< sampler_ref<sampler_i> > > T;
896     static inline const T convert (const sampler_ref<sampler_i> &v)
897     {
898     return sl_append< sampler_ref<sampler_i> > (v);
899     }
900     };
901    
902 root 1.5 extern const fragment_const_string str_2sp;
903     extern const fragment_const_string str_equal;
904 root 1.8 extern const fragment_const_string str_comma;
905 root 1.5 extern const fragment_const_string str_endl;
906    
907 root 1.3 template<class fragment, typename expr>
908     inline void sl_assign (const fragment &f, const expr &e)
909     {
910 root 1.5 cur->append (str_2sp);
911 root 1.3 cur->append (f);
912 root 1.5 cur->append (str_equal);
913 root 1.3 sl_convert<expr>::convert (e) ();
914 root 1.5 cur->append (str_endl);
915 root 1.3 }
916    
917     template<class type>
918     template<typename expr>
919     inline const auto_lvalue_ref0<type> &auto_lvalue_ref0<type>::operator =(const expr &e) const
920     {
921     sl_assign (*this, e);
922     return *this;
923     }
924    
925     template<class type, typename arg1>
926     template<typename expr>
927     inline const auto_lvalue_ref1<type,arg1> &auto_lvalue_ref1<type,arg1>::operator =(const expr &e) const
928     {
929     sl_assign (*this, e);
930     return *this;
931     }
932    
933 root 1.5 template<class T>
934     template<typename expr>
935     inline const sl_expr<T> &sl_expr<T>::operator =(const expr &e) const
936     {
937     sl_assign (*this, e);
938     return *this;
939     }
940    
941 root 1.3 template<const char *strtype>
942     template<typename expr>
943     inline const temp_ref<strtype> &temp_ref<strtype>::operator =(const expr &e) const
944     {
945     sl_assign (*this, e);
946     return *this;
947     }
948    
949     struct sl_append_const_string
950     {
951     fragment_const_string str;
952     sl_append_const_string (const char *s)
953     : str (s)
954     { }
955    
956     void operator ()() const
957     {
958     cur->push_back (str);
959     }
960     };
961    
962 root 1.12 extern const sl_append_const_string str_lpar;
963     extern const sl_append_const_string str_rpar;
964    
965     template<class A, class B, class C>
966     struct sl_binop
967     {
968     const A a; const B b; const C c;
969     sl_binop (const A &a, const B &b, const C &c) : a(a), b(b), c(c) { }
970     void operator ()() const { str_lpar (); a (); b (); c (); str_rpar (); }
971     };
972    
973 root 1.3 # define SHADER_BINOP(op, str) \
974     extern const sl_append_const_string str_ ## str; \
975     template<typename A, typename B> \
976 root 1.12 inline const sl_expr< sl_binop< typename sl_convert<A>::T, \
977     sl_append_const_string, \
978     typename sl_convert<B>::T > > \
979 root 1.3 operator op(const A &a, const B &b) \
980     { \
981 root 1.12 return sl_binop< typename sl_convert<A>::T, sl_append_const_string, typename sl_convert<B>::T > \
982     (sl_convert<A>::convert (a), str_ ## str, sl_convert<B>::convert (b)); \
983 root 1.3 }
984    
985     SHADER_BINOP (+, plus);
986     SHADER_BINOP (-, minus);
987     SHADER_BINOP (*, mul);
988     SHADER_BINOP (/, div);
989 root 1.12 SHADER_BINOP (%, mod);
990 root 1.3
991     # undef SHADER_BINOP
992    
993 root 1.5 void swizzle_mask (sl_string<7> &s, int mask);
994 root 1.6
995     template<typename T>
996     inline const sl_expr< sl_concat3< sl_append_const_string,
997     typename sl_convert<T>::T,
998     sl_string<7>
999     > >
1000     swizzle (const T &t, int a, int b = 0, int c = 0, int d = 0)
1001     {
1002     sl_string<7> s;
1003     swizzle_mask (s, ((d * 5 + c) * 5 + b) * 5 + a);
1004     return concat (str_lpar, sl_convert<T>::convert (t), s);
1005     }
1006    
1007     # define SHADER_SWIZZLE_OP(abcd,type) \
1008     template<typename T> \
1009     inline const sl_expr< sl_concat3< sl_append_const_string, \
1010     typename sl_convert<T>::T, \
1011     sl_string<7> \
1012     > > \
1013     type (const T &t) \
1014     { \
1015     return swizzle (t, (abcd / 1000) % 5, (abcd / 100) % 5, (abcd / 10) % 5, (abcd / 1) % 5); \
1016     }
1017    
1018     // brute force is lovely, ain't it?
1019     SHADER_SWIZZLE_OP (1 , x ) SHADER_SWIZZLE_OP (11 , xx ) SHADER_SWIZZLE_OP (111 , xxx ) SHADER_SWIZZLE_OP (1111, xxxx)
1020     SHADER_SWIZZLE_OP (1112, xxxy) SHADER_SWIZZLE_OP (1113, xxxz) SHADER_SWIZZLE_OP (1114, xxxw) SHADER_SWIZZLE_OP (112 , xxy )
1021     SHADER_SWIZZLE_OP (1121, xxyx) SHADER_SWIZZLE_OP (1122, xxyy) SHADER_SWIZZLE_OP (1123, xxyz) SHADER_SWIZZLE_OP (1124, xxyw)
1022     SHADER_SWIZZLE_OP (113 , xxz ) SHADER_SWIZZLE_OP (1131, xxzx) SHADER_SWIZZLE_OP (1132, xxzy) SHADER_SWIZZLE_OP (1133, xxzz)
1023     SHADER_SWIZZLE_OP (1134, xxzw) SHADER_SWIZZLE_OP (114 , xxw ) SHADER_SWIZZLE_OP (1141, xxwx) SHADER_SWIZZLE_OP (1142, xxwy)
1024     SHADER_SWIZZLE_OP (1143, xxwz) SHADER_SWIZZLE_OP (1144, xxww) SHADER_SWIZZLE_OP (12 , xy ) SHADER_SWIZZLE_OP (121 , xyx )
1025     SHADER_SWIZZLE_OP (1211, xyxx) SHADER_SWIZZLE_OP (1212, xyxy) SHADER_SWIZZLE_OP (1213, xyxz) SHADER_SWIZZLE_OP (1214, xyxw)
1026     SHADER_SWIZZLE_OP (122 , xyy ) SHADER_SWIZZLE_OP (1221, xyyx) SHADER_SWIZZLE_OP (1222, xyyy) SHADER_SWIZZLE_OP (1223, xyyz)
1027     SHADER_SWIZZLE_OP (1224, xyyw) SHADER_SWIZZLE_OP (123 , xyz ) SHADER_SWIZZLE_OP (1231, xyzx) SHADER_SWIZZLE_OP (1232, xyzy)
1028     SHADER_SWIZZLE_OP (1233, xyzz) SHADER_SWIZZLE_OP (1234, xyzw) SHADER_SWIZZLE_OP (124 , xyw ) SHADER_SWIZZLE_OP (1241, xywx)
1029     SHADER_SWIZZLE_OP (1242, xywy) SHADER_SWIZZLE_OP (1243, xywz) SHADER_SWIZZLE_OP (1244, xyww) SHADER_SWIZZLE_OP (13 , xz )
1030     SHADER_SWIZZLE_OP (131 , xzx ) SHADER_SWIZZLE_OP (1311, xzxx) SHADER_SWIZZLE_OP (1312, xzxy) SHADER_SWIZZLE_OP (1313, xzxz)
1031     SHADER_SWIZZLE_OP (1314, xzxw) SHADER_SWIZZLE_OP (132 , xzy ) SHADER_SWIZZLE_OP (1321, xzyx) SHADER_SWIZZLE_OP (1322, xzyy)
1032     SHADER_SWIZZLE_OP (1323, xzyz) SHADER_SWIZZLE_OP (1324, xzyw) SHADER_SWIZZLE_OP (133 , xzz ) SHADER_SWIZZLE_OP (1331, xzzx)
1033     SHADER_SWIZZLE_OP (1332, xzzy) SHADER_SWIZZLE_OP (1333, xzzz) SHADER_SWIZZLE_OP (1334, xzzw) SHADER_SWIZZLE_OP (134 , xzw )
1034     SHADER_SWIZZLE_OP (1341, xzwx) SHADER_SWIZZLE_OP (1342, xzwy) SHADER_SWIZZLE_OP (1343, xzwz) SHADER_SWIZZLE_OP (1344, xzww)
1035     SHADER_SWIZZLE_OP (14 , xw ) SHADER_SWIZZLE_OP (141 , xwx ) SHADER_SWIZZLE_OP (1411, xwxx) SHADER_SWIZZLE_OP (1412, xwxy)
1036     SHADER_SWIZZLE_OP (1413, xwxz) SHADER_SWIZZLE_OP (1414, xwxw) SHADER_SWIZZLE_OP (142 , xwy ) SHADER_SWIZZLE_OP (1421, xwyx)
1037     SHADER_SWIZZLE_OP (1422, xwyy) SHADER_SWIZZLE_OP (1423, xwyz) SHADER_SWIZZLE_OP (1424, xwyw) SHADER_SWIZZLE_OP (143 , xwz )
1038     SHADER_SWIZZLE_OP (1431, xwzx) SHADER_SWIZZLE_OP (1432, xwzy) SHADER_SWIZZLE_OP (1433, xwzz) SHADER_SWIZZLE_OP (1434, xwzw)
1039     SHADER_SWIZZLE_OP (144 , xww ) SHADER_SWIZZLE_OP (1441, xwwx) SHADER_SWIZZLE_OP (1442, xwwy) SHADER_SWIZZLE_OP (1443, xwwz)
1040     SHADER_SWIZZLE_OP (1444, xwww) SHADER_SWIZZLE_OP (2 , y ) SHADER_SWIZZLE_OP (21 , yx ) SHADER_SWIZZLE_OP (211 , yxx )
1041     SHADER_SWIZZLE_OP (2111, yxxx) SHADER_SWIZZLE_OP (2112, yxxy) SHADER_SWIZZLE_OP (2113, yxxz) SHADER_SWIZZLE_OP (2114, yxxw)
1042     SHADER_SWIZZLE_OP (212 , yxy ) SHADER_SWIZZLE_OP (2121, yxyx) SHADER_SWIZZLE_OP (2122, yxyy) SHADER_SWIZZLE_OP (2123, yxyz)
1043     SHADER_SWIZZLE_OP (2124, yxyw) SHADER_SWIZZLE_OP (213 , yxz ) SHADER_SWIZZLE_OP (2131, yxzx) SHADER_SWIZZLE_OP (2132, yxzy)
1044     SHADER_SWIZZLE_OP (2133, yxzz) SHADER_SWIZZLE_OP (2134, yxzw) SHADER_SWIZZLE_OP (214 , yxw ) SHADER_SWIZZLE_OP (2141, yxwx)
1045     SHADER_SWIZZLE_OP (2142, yxwy) SHADER_SWIZZLE_OP (2143, yxwz) SHADER_SWIZZLE_OP (2144, yxww) SHADER_SWIZZLE_OP (22 , yy )
1046     SHADER_SWIZZLE_OP (221 , yyx ) SHADER_SWIZZLE_OP (2211, yyxx) SHADER_SWIZZLE_OP (2212, yyxy) SHADER_SWIZZLE_OP (2213, yyxz)
1047     SHADER_SWIZZLE_OP (2214, yyxw) SHADER_SWIZZLE_OP (222 , yyy ) SHADER_SWIZZLE_OP (2221, yyyx) SHADER_SWIZZLE_OP (2222, yyyy)
1048     SHADER_SWIZZLE_OP (2223, yyyz) SHADER_SWIZZLE_OP (2224, yyyw) SHADER_SWIZZLE_OP (223 , yyz ) SHADER_SWIZZLE_OP (2231, yyzx)
1049     SHADER_SWIZZLE_OP (2232, yyzy) SHADER_SWIZZLE_OP (2233, yyzz) SHADER_SWIZZLE_OP (2234, yyzw) SHADER_SWIZZLE_OP (224 , yyw )
1050     SHADER_SWIZZLE_OP (2241, yywx) SHADER_SWIZZLE_OP (2242, yywy) SHADER_SWIZZLE_OP (2243, yywz) SHADER_SWIZZLE_OP (2244, yyww)
1051     SHADER_SWIZZLE_OP (23 , yz ) SHADER_SWIZZLE_OP (231 , yzx ) SHADER_SWIZZLE_OP (2311, yzxx) SHADER_SWIZZLE_OP (2312, yzxy)
1052     SHADER_SWIZZLE_OP (2313, yzxz) SHADER_SWIZZLE_OP (2314, yzxw) SHADER_SWIZZLE_OP (232 , yzy ) SHADER_SWIZZLE_OP (2321, yzyx)
1053     SHADER_SWIZZLE_OP (2322, yzyy) SHADER_SWIZZLE_OP (2323, yzyz) SHADER_SWIZZLE_OP (2324, yzyw) SHADER_SWIZZLE_OP (233 , yzz )
1054     SHADER_SWIZZLE_OP (2331, yzzx) SHADER_SWIZZLE_OP (2332, yzzy) SHADER_SWIZZLE_OP (2333, yzzz) SHADER_SWIZZLE_OP (2334, yzzw)
1055     SHADER_SWIZZLE_OP (234 , yzw ) SHADER_SWIZZLE_OP (2341, yzwx) SHADER_SWIZZLE_OP (2342, yzwy) SHADER_SWIZZLE_OP (2343, yzwz)
1056     SHADER_SWIZZLE_OP (2344, yzww) SHADER_SWIZZLE_OP (24 , yw ) SHADER_SWIZZLE_OP (241 , ywx ) SHADER_SWIZZLE_OP (2411, ywxx)
1057     SHADER_SWIZZLE_OP (2412, ywxy) SHADER_SWIZZLE_OP (2413, ywxz) SHADER_SWIZZLE_OP (2414, ywxw) SHADER_SWIZZLE_OP (242 , ywy )
1058     SHADER_SWIZZLE_OP (2421, ywyx) SHADER_SWIZZLE_OP (2422, ywyy) SHADER_SWIZZLE_OP (2423, ywyz) SHADER_SWIZZLE_OP (2424, ywyw)
1059     SHADER_SWIZZLE_OP (243 , ywz ) SHADER_SWIZZLE_OP (2431, ywzx) SHADER_SWIZZLE_OP (2432, ywzy) SHADER_SWIZZLE_OP (2433, ywzz)
1060     SHADER_SWIZZLE_OP (2434, ywzw) SHADER_SWIZZLE_OP (244 , yww ) SHADER_SWIZZLE_OP (2441, ywwx) SHADER_SWIZZLE_OP (2442, ywwy)
1061     SHADER_SWIZZLE_OP (2443, ywwz) SHADER_SWIZZLE_OP (2444, ywww) SHADER_SWIZZLE_OP (3 , z ) SHADER_SWIZZLE_OP (31 , zx )
1062     SHADER_SWIZZLE_OP (311 , zxx ) SHADER_SWIZZLE_OP (3111, zxxx) SHADER_SWIZZLE_OP (3112, zxxy) SHADER_SWIZZLE_OP (3113, zxxz)
1063     SHADER_SWIZZLE_OP (3114, zxxw) SHADER_SWIZZLE_OP (312 , zxy ) SHADER_SWIZZLE_OP (3121, zxyx) SHADER_SWIZZLE_OP (3122, zxyy)
1064     SHADER_SWIZZLE_OP (3123, zxyz) SHADER_SWIZZLE_OP (3124, zxyw) SHADER_SWIZZLE_OP (313 , zxz ) SHADER_SWIZZLE_OP (3131, zxzx)
1065     SHADER_SWIZZLE_OP (3132, zxzy) SHADER_SWIZZLE_OP (3133, zxzz) SHADER_SWIZZLE_OP (3134, zxzw) SHADER_SWIZZLE_OP (314 , zxw )
1066     SHADER_SWIZZLE_OP (3141, zxwx) SHADER_SWIZZLE_OP (3142, zxwy) SHADER_SWIZZLE_OP (3143, zxwz) SHADER_SWIZZLE_OP (3144, zxww)
1067     SHADER_SWIZZLE_OP (32 , zy ) SHADER_SWIZZLE_OP (321 , zyx ) SHADER_SWIZZLE_OP (3211, zyxx) SHADER_SWIZZLE_OP (3212, zyxy)
1068     SHADER_SWIZZLE_OP (3213, zyxz) SHADER_SWIZZLE_OP (3214, zyxw) SHADER_SWIZZLE_OP (322 , zyy ) SHADER_SWIZZLE_OP (3221, zyyx)
1069     SHADER_SWIZZLE_OP (3222, zyyy) SHADER_SWIZZLE_OP (3223, zyyz) SHADER_SWIZZLE_OP (3224, zyyw) SHADER_SWIZZLE_OP (323 , zyz )
1070     SHADER_SWIZZLE_OP (3231, zyzx) SHADER_SWIZZLE_OP (3232, zyzy) SHADER_SWIZZLE_OP (3233, zyzz) SHADER_SWIZZLE_OP (3234, zyzw)
1071     SHADER_SWIZZLE_OP (324 , zyw ) SHADER_SWIZZLE_OP (3241, zywx) SHADER_SWIZZLE_OP (3242, zywy) SHADER_SWIZZLE_OP (3243, zywz)
1072     SHADER_SWIZZLE_OP (3244, zyww) SHADER_SWIZZLE_OP (33 , zz ) SHADER_SWIZZLE_OP (331 , zzx ) SHADER_SWIZZLE_OP (3311, zzxx)
1073     SHADER_SWIZZLE_OP (3312, zzxy) SHADER_SWIZZLE_OP (3313, zzxz) SHADER_SWIZZLE_OP (3314, zzxw) SHADER_SWIZZLE_OP (332 , zzy )
1074     SHADER_SWIZZLE_OP (3321, zzyx) SHADER_SWIZZLE_OP (3322, zzyy) SHADER_SWIZZLE_OP (3323, zzyz) SHADER_SWIZZLE_OP (3324, zzyw)
1075     SHADER_SWIZZLE_OP (333 , zzz ) SHADER_SWIZZLE_OP (3331, zzzx) SHADER_SWIZZLE_OP (3332, zzzy) SHADER_SWIZZLE_OP (3333, zzzz)
1076     SHADER_SWIZZLE_OP (3334, zzzw) SHADER_SWIZZLE_OP (334 , zzw ) SHADER_SWIZZLE_OP (3341, zzwx) SHADER_SWIZZLE_OP (3342, zzwy)
1077     SHADER_SWIZZLE_OP (3343, zzwz) SHADER_SWIZZLE_OP (3344, zzww) SHADER_SWIZZLE_OP (34 , zw ) SHADER_SWIZZLE_OP (341 , zwx )
1078     SHADER_SWIZZLE_OP (3411, zwxx) SHADER_SWIZZLE_OP (3412, zwxy) SHADER_SWIZZLE_OP (3413, zwxz) SHADER_SWIZZLE_OP (3414, zwxw)
1079     SHADER_SWIZZLE_OP (342 , zwy ) SHADER_SWIZZLE_OP (3421, zwyx) SHADER_SWIZZLE_OP (3422, zwyy) SHADER_SWIZZLE_OP (3423, zwyz)
1080     SHADER_SWIZZLE_OP (3424, zwyw) SHADER_SWIZZLE_OP (343 , zwz ) SHADER_SWIZZLE_OP (3431, zwzx) SHADER_SWIZZLE_OP (3432, zwzy)
1081     SHADER_SWIZZLE_OP (3433, zwzz) SHADER_SWIZZLE_OP (3434, zwzw) SHADER_SWIZZLE_OP (344 , zww ) SHADER_SWIZZLE_OP (3441, zwwx)
1082     SHADER_SWIZZLE_OP (3442, zwwy) SHADER_SWIZZLE_OP (3443, zwwz) SHADER_SWIZZLE_OP (3444, zwww) SHADER_SWIZZLE_OP (4 , w )
1083     SHADER_SWIZZLE_OP (41 , wx ) SHADER_SWIZZLE_OP (411 , wxx ) SHADER_SWIZZLE_OP (4111, wxxx) SHADER_SWIZZLE_OP (4112, wxxy)
1084     SHADER_SWIZZLE_OP (4113, wxxz) SHADER_SWIZZLE_OP (4114, wxxw) SHADER_SWIZZLE_OP (412 , wxy ) SHADER_SWIZZLE_OP (4121, wxyx)
1085     SHADER_SWIZZLE_OP (4122, wxyy) SHADER_SWIZZLE_OP (4123, wxyz) SHADER_SWIZZLE_OP (4124, wxyw) SHADER_SWIZZLE_OP (413 , wxz )
1086     SHADER_SWIZZLE_OP (4131, wxzx) SHADER_SWIZZLE_OP (4132, wxzy) SHADER_SWIZZLE_OP (4133, wxzz) SHADER_SWIZZLE_OP (4134, wxzw)
1087     SHADER_SWIZZLE_OP (414 , wxw ) SHADER_SWIZZLE_OP (4141, wxwx) SHADER_SWIZZLE_OP (4142, wxwy) SHADER_SWIZZLE_OP (4143, wxwz)
1088     SHADER_SWIZZLE_OP (4144, wxww) SHADER_SWIZZLE_OP (42 , wy ) SHADER_SWIZZLE_OP (421 , wyx ) SHADER_SWIZZLE_OP (4211, wyxx)
1089     SHADER_SWIZZLE_OP (4212, wyxy) SHADER_SWIZZLE_OP (4213, wyxz) SHADER_SWIZZLE_OP (4214, wyxw) SHADER_SWIZZLE_OP (422 , wyy )
1090     SHADER_SWIZZLE_OP (4221, wyyx) SHADER_SWIZZLE_OP (4222, wyyy) SHADER_SWIZZLE_OP (4223, wyyz) SHADER_SWIZZLE_OP (4224, wyyw)
1091     SHADER_SWIZZLE_OP (423 , wyz ) SHADER_SWIZZLE_OP (4231, wyzx) SHADER_SWIZZLE_OP (4232, wyzy) SHADER_SWIZZLE_OP (4233, wyzz)
1092     SHADER_SWIZZLE_OP (4234, wyzw) SHADER_SWIZZLE_OP (424 , wyw ) SHADER_SWIZZLE_OP (4241, wywx) SHADER_SWIZZLE_OP (4242, wywy)
1093     SHADER_SWIZZLE_OP (4243, wywz) SHADER_SWIZZLE_OP (4244, wyww) SHADER_SWIZZLE_OP (43 , wz ) SHADER_SWIZZLE_OP (431 , wzx )
1094     SHADER_SWIZZLE_OP (4311, wzxx) SHADER_SWIZZLE_OP (4312, wzxy) SHADER_SWIZZLE_OP (4313, wzxz) SHADER_SWIZZLE_OP (4314, wzxw)
1095     SHADER_SWIZZLE_OP (432 , wzy ) SHADER_SWIZZLE_OP (4321, wzyx) SHADER_SWIZZLE_OP (4322, wzyy) SHADER_SWIZZLE_OP (4323, wzyz)
1096     SHADER_SWIZZLE_OP (4324, wzyw) SHADER_SWIZZLE_OP (433 , wzz ) SHADER_SWIZZLE_OP (4331, wzzx) SHADER_SWIZZLE_OP (4332, wzzy)
1097     SHADER_SWIZZLE_OP (4333, wzzz) SHADER_SWIZZLE_OP (4334, wzzw) SHADER_SWIZZLE_OP (434 , wzw ) SHADER_SWIZZLE_OP (4341, wzwx)
1098     SHADER_SWIZZLE_OP (4342, wzwy) SHADER_SWIZZLE_OP (4343, wzwz) SHADER_SWIZZLE_OP (4344, wzww) SHADER_SWIZZLE_OP (44 , ww )
1099     SHADER_SWIZZLE_OP (441 , wwx ) SHADER_SWIZZLE_OP (4411, wwxx) SHADER_SWIZZLE_OP (4412, wwxy) SHADER_SWIZZLE_OP (4413, wwxz)
1100     SHADER_SWIZZLE_OP (4414, wwxw) SHADER_SWIZZLE_OP (442 , wwy ) SHADER_SWIZZLE_OP (4421, wwyx) SHADER_SWIZZLE_OP (4422, wwyy)
1101     SHADER_SWIZZLE_OP (4423, wwyz) SHADER_SWIZZLE_OP (4424, wwyw) SHADER_SWIZZLE_OP (443 , wwz ) SHADER_SWIZZLE_OP (4431, wwzx)
1102     SHADER_SWIZZLE_OP (4432, wwzy) SHADER_SWIZZLE_OP (4433, wwzz) SHADER_SWIZZLE_OP (4434, wwzw) SHADER_SWIZZLE_OP (444 , www )
1103     SHADER_SWIZZLE_OP (4441, wwwx) SHADER_SWIZZLE_OP (4442, wwwy) SHADER_SWIZZLE_OP (4443, wwwz) SHADER_SWIZZLE_OP (4444, wwww)
1104    
1105     # undef SHADER_SWIZZLE_OP
1106 root 1.9
1107     # define SHADER_FUNC0_(name, glname) \
1108     template<typename A> \
1109     inline const sl_expr<sl_func0> \
1110     name () \
1111     { \
1112     return sl_func0 (#glname " ("); \
1113     }
1114    
1115     # define SHADER_FUNC0(name) SHADER_FUNC0_(name,name)
1116    
1117     # define SHADER_FUNC1_(name, glname) \
1118     template<typename A> \
1119     inline const sl_expr< sl_func1<typename sl_convert<A>::T> > \
1120     name (const A &a) \
1121     { \
1122     return sl_func1<typename sl_convert<A>::T> (#glname " (", sl_convert<A>::convert (a));\
1123     }
1124    
1125     # define SHADER_FUNC1(name) SHADER_FUNC1_(name,name)
1126    
1127     # define SHADER_FUNC2_(name, glname) \
1128     template<typename A, typename B> \
1129     inline const sl_expr< sl_func2<typename sl_convert<A>::T, typename sl_convert<B>::T> > \
1130     name (const A &a, const B &b) \
1131     { \
1132     return sl_func2<typename sl_convert<A>::T, typename sl_convert<B>::T> (#glname " (", sl_convert<A>::convert (a), sl_convert<B>::convert (b));\
1133     }
1134    
1135     # define SHADER_FUNC2(name) SHADER_FUNC2_(name,name)
1136    
1137     # define SHADER_FUNC3_(name, glname) \
1138     template<typename A, typename B, typename C> \
1139     inline const sl_expr< sl_func3<typename sl_convert<A>::T, typename sl_convert<B>::T, typename sl_convert<C>::T> > \
1140     name (const A &a, const B &b, const C &c) \
1141     { \
1142     return sl_func3<typename sl_convert<A>::T, typename sl_convert<B>::T, typename sl_convert<C>::T> (#glname " (", sl_convert<A>::convert (a), sl_convert<B>::convert (b), sl_convert<C>::convert (c));\
1143     }
1144    
1145     # define SHADER_FUNC3(name) SHADER_FUNC3_(name,name)
1146    
1147     # define SHADER_FUNC4_(name, glname) \
1148     template<typename A, typename B, typename C, typename D> \
1149     inline const sl_expr< sl_func4<typename sl_convert<A>::T, typename sl_convert<B>::T, typename sl_convert<C>::T, typename sl_convert<D>::T > > \
1150     name (const A &a, const B &b, const C &c, const D &d) \
1151     { \
1152     return sl_func4<typename sl_convert<A>::T, typename sl_convert<B>::T, typename sl_convert<C>::T, typename sl_convert<D>::T> (#glname " (", sl_convert<A>::convert (a), sl_convert<B>::convert (b), sl_convert<C>::convert (c), sl_convert<D>::convert (d));\
1153     }
1154    
1155     # define SHADER_FUNC4(name) SHADER_FUNC4_(name,name)
1156    
1157     SHADER_FUNC1 (abs)
1158     SHADER_FUNC1 (acos)
1159     SHADER_FUNC1 (all)
1160     SHADER_FUNC1 (any)
1161     SHADER_FUNC1 (asin)
1162     SHADER_FUNC1 (atan)
1163     SHADER_FUNC2 (atan)
1164     SHADER_FUNC1 (ceil)
1165     SHADER_FUNC3 (clamp)
1166     SHADER_FUNC1 (cos)
1167     SHADER_FUNC2 (cross)
1168     SHADER_FUNC1 (dFdx)
1169     SHADER_FUNC1 (dFdy)
1170     SHADER_FUNC1 (degrees)
1171     SHADER_FUNC2 (distance)
1172     SHADER_FUNC2 (dot)
1173     SHADER_FUNC2_(equal, equal)
1174     SHADER_FUNC1 (exp)
1175     SHADER_FUNC1 (exp2)
1176     SHADER_FUNC3 (faceforward)
1177     SHADER_FUNC1 (floor)
1178     SHADER_FUNC1 (fract)
1179     SHADER_FUNC0 (ftransform)
1180     SHADER_FUNC1 (fwidth)
1181     SHADER_FUNC2_(greater_than_equal, greaterThanEqual)
1182     SHADER_FUNC2_(greater_then, greaterThan)
1183     SHADER_FUNC1 (inversesqrt)
1184     SHADER_FUNC1 (length)
1185     SHADER_FUNC2_(less_than, lessThan)
1186     SHADER_FUNC2_(less_than_equal, lessThanEqual)
1187     SHADER_FUNC1 (log)
1188     SHADER_FUNC1 (log2)
1189     SHADER_FUNC2_(matrix_comp_mult, matrixCompMult)
1190     SHADER_FUNC2 (max)
1191     SHADER_FUNC2 (min)
1192     SHADER_FUNC3 (mix)
1193     SHADER_FUNC2 (mod)
1194     SHADER_FUNC1 (noise1)
1195     SHADER_FUNC1 (noise2)
1196     SHADER_FUNC1 (noise3)
1197     SHADER_FUNC1 (noise4)
1198     SHADER_FUNC1 (normalize)
1199     SHADER_FUNC1 (gl_not) // TODO
1200     SHADER_FUNC2_(notequal, notEqual)
1201     SHADER_FUNC2 (pow)
1202     SHADER_FUNC1 (radians)
1203     SHADER_FUNC2 (reflect)
1204     SHADER_FUNC3 (refract)
1205     SHADER_FUNC2_(shadow_1d, shadow1D)
1206     SHADER_FUNC3_(shadow_1d, shadow1D)
1207     SHADER_FUNC3_(shadow_1d_lod, shadow1DLod)
1208     SHADER_FUNC2_(shadow_1d_proj, shadow1DProj)
1209     SHADER_FUNC3_(shadow_1d_proj, shadow1DProj)
1210     SHADER_FUNC3_(shadow_1d_proj_lod, shadow1DProjLod)
1211     SHADER_FUNC2_(shadow_2d, shadow2D)
1212     SHADER_FUNC3_(shadow_2d, shadow2D)
1213     SHADER_FUNC3_(shadow_2d_lod, shadow2DLod)
1214     SHADER_FUNC2_(shadow_2d_proj, shadow2DProj)
1215     SHADER_FUNC3_(shadow_2d_proj, shadow2DProj)
1216     SHADER_FUNC3_(shadow_2d_proj_lod, shadow2DProjLod)
1217     SHADER_FUNC1 (sign)
1218     SHADER_FUNC1 (sin)
1219     SHADER_FUNC3 (smoothstep)
1220     SHADER_FUNC1 (sqrt)
1221     SHADER_FUNC2 (step)
1222     SHADER_FUNC1 (tan)
1223     SHADER_FUNC2_(texture_1d, texture1D)
1224     SHADER_FUNC3_(texture_1d, texture1D)
1225     SHADER_FUNC3_(texture_1d_lod, texture1DLod)
1226     SHADER_FUNC2_(texture_1d_proj, texture1DProj)
1227     SHADER_FUNC3_(texture_1d_proj, texture1DProj)
1228     SHADER_FUNC3_(texture_1d_proj_lod, texture1DProjLod)
1229     SHADER_FUNC2_(texture_2d, texture2D)
1230     SHADER_FUNC3_(texture_2d, texture2D)
1231     SHADER_FUNC3_(texture_2d_lod, texture2DLod)
1232     SHADER_FUNC2_(texture_2d_proj, texture2DProj)
1233     SHADER_FUNC3_(texture_2d_proj, texture2DProj)
1234     SHADER_FUNC3_(texture_2d_proj_lod, texture2DProjLod)
1235     SHADER_FUNC2_(texture_3d, texture3D)
1236     SHADER_FUNC3_(texture_3d, texture3D)
1237     SHADER_FUNC3_(texture_3d_lod, texture3DLod)
1238     SHADER_FUNC2_(texture_3d_proj, texture3DProj)
1239     SHADER_FUNC3_(texture_3d_proj, texture3DProj)
1240     SHADER_FUNC3_(texture_3d_proj_lod, texture3DProjLod)
1241     SHADER_FUNC2_(texture_cube, textureCube)
1242     SHADER_FUNC3_(texture_cube, textureCube)
1243     SHADER_FUNC3_(texture_cude_lod, textureCubeLod)
1244     SHADER_FUNC1 (vec2) SHADER_FUNC2 (vec2)
1245     SHADER_FUNC1 (vec3) SHADER_FUNC2 (vec3) SHADER_FUNC3 (vec3)
1246     SHADER_FUNC1 (vec4) SHADER_FUNC2 (vec4) SHADER_FUNC3 (vec4) SHADER_FUNC4 (vec4)
1247     SHADER_FUNC1 (mat2) SHADER_FUNC2 (mat2)
1248     SHADER_FUNC1 (mat3) SHADER_FUNC2 (mat3) SHADER_FUNC3 (mat3)
1249     SHADER_FUNC1 (mat4) SHADER_FUNC2 (mat4) SHADER_FUNC3 (mat4) SHADER_FUNC4 (mat4)
1250    
1251     # undef SHADER_FUNC0
1252     # undef SHADER_FUNC0_
1253     # undef SHADER_FUNC1
1254     # undef SHADER_FUNC1_
1255     # undef SHADER_FUNC2
1256     # undef SHADER_FUNC2_
1257     # undef SHADER_FUNC3
1258     # undef SHADER_FUNC3_
1259     # undef SHADER_FUNC4
1260     # undef SHADER_FUNC5_
1261 root 1.6
1262 root 1.1 void debdebdebdebug ();//D
1263     }
1264    
1265     #endif
1266 root 1.17
1267     #include "shader_vars.h"
1268    
1269 root 1.1