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