ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libgender/shader.h
Revision: 1.8
Committed: Sun Oct 24 00:56:04 2004 UTC (21 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.7: +5 -3 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #ifndef SHADER_H
2     #define SHADER_H
3    
4     #include <sstream>
5    
6     #include "opengl.h"
7     #include "util.h"
8    
9     namespace shader {
10    
11     using namespace std;
12    
13     const int NAMELEN = 32;
14    
15     template<class T>
16     struct sl_expr {
17     const T t;
18     sl_expr (const T &t) : t(t) { }
19     void operator ()() const { t (); }
20 root 1.5 template<typename expr>
21     const sl_expr &operator =(const expr &e) const;
22 root 1.1 };
23    
24     template<class A, class B>
25     struct sl_concat2
26     {
27     const A a; const B b;
28     sl_concat2 (const A &a, const B &b) : a(a), b(b) { }
29     void operator ()() const { a (); b (); }
30     };
31    
32     template<class A, class B, class C>
33     struct sl_concat3
34     {
35     const A a; const B b; const C c;
36     sl_concat3 (const A &a, const B &b, const C &c) : a(a), b(b), c(c) { }
37     void operator ()() const { a (); b (); c (); }
38     };
39    
40     template<class A, class B, class C, class D>
41     struct sl_concat4
42     {
43     const A a; const B b; const C c; const D d;
44     sl_concat4 (const A &a, const B &b, const C &c, const D &d) : a(a), b(b), c(c), d(d) { }
45     void operator ()() const { a (); b (); c (); d(); }
46     };
47    
48 root 1.7 struct sl_func0
49     {
50     const char *name_par;
51     sl_func0 (const char *name_par) : name_par(name_par) { }
52     void begin () const;
53 root 1.8 void comma () const;
54 root 1.7 void end () const;
55     void operator ()() const
56     {
57     begin ();
58     end ();
59     }
60     };
61    
62     template<class A>
63     struct sl_func1 : sl_func0
64     {
65     const A a;
66     sl_func1 (const char *name, const A &a) : sl_func0(name), a(a) { }
67     void operator ()() const { begin (); a (); end (); }
68     };
69    
70     template<class A, class B>
71     struct sl_func2 : sl_func0
72     {
73     const A a; const B b;
74     sl_func2 (const char *name, const A &a, const B &b) : sl_func0(name), a(a), b(b) { }
75 root 1.8 void operator ()() const { begin (); a (); comma (); b (); end (); }
76 root 1.7 };
77    
78     template<class A, class B, class C>
79     struct sl_func3 : sl_func0
80     {
81     const A a; const B b; const C c;
82     sl_func3 (const char *name, const A &a, const B &b, const C &c) : sl_func0(name), a(a), b(b), c(c) { }
83 root 1.8 void operator ()() const { begin (); a (); comma (); b (); comma (); c (); end (); }
84 root 1.7 };
85    
86     template<class A, class B, class C, class D>
87     struct sl_func4 : sl_func0
88     {
89     const A a; const B b; const C c; const D d;
90     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) { }
91 root 1.8 void operator ()() const { begin (); a (); comma (); b (); comma (); c (); comma (); d(); end (); }
92 root 1.7 };
93    
94 root 1.1 class refcounted
95     {
96     template<class type> friend class ref;
97     mutable int refcnt;
98     void refcnt_inc () const { refcnt++; }
99 root 1.6 void refcnt_dec () const;
100 root 1.1 public:
101     refcounted () : refcnt(0) { }
102     virtual ~refcounted ();
103     };
104    
105     template<class type>
106     struct ref
107     {
108     type *p;
109    
110     ref ()
111     {
112     p = new type;
113     p->refcnt_inc ();
114     }
115    
116     ref (type &d)
117     {
118     d.refcnt_inc ();
119     p = &d;
120     }
121    
122     ref (const ref &r)
123     {
124     r.p->refcnt_inc ();
125     p = r.p;
126     }
127    
128     template<class type2>
129     ref (const ref<type2> &r)
130     {
131     r.p->refcnt_inc ();
132     p = r.p;
133     }
134    
135     ~ref ()
136     {
137     p->refcnt_dec ();
138     }
139    
140     ref &operator =(type &d)
141     {
142     d.refcnt_inc ();
143     p->refcnt_dec ();
144     p = &d;
145     return *this;
146     }
147    
148     type *operator ->() const { return p; }
149    
150     operator type &() const { return *p; }
151    
152     template<class type2>
153     bool operator ==(const ref<type2> b) const
154     {
155     return (void *)p == (void *)b.p;
156     }
157     };
158    
159     // ref with auto-construct
160     template<class type>
161     struct auto_ref0 : ref<type>
162     {
163     auto_ref0 () : ref<type> (*new type ()) { }
164     };
165    
166     template<class type, typename arg1>
167     struct auto_ref1 : ref<type>
168     {
169     auto_ref1 (arg1 a) : ref<type> (*new type (a)) { }
170     };
171    
172     template<class type>
173     struct auto_lvalue_ref0 : ref<type>
174     {
175     auto_lvalue_ref0 () : ref<type> (*new type ()) { }
176     template<typename expr>
177     const auto_lvalue_ref0 &operator =(const expr &e) const;
178     };
179    
180     template<class type, typename arg1>
181     struct auto_lvalue_ref1 : ref<type>
182     {
183     auto_lvalue_ref1 (arg1 a) : ref<type> (*new type (a)) { }
184     template<class expr>
185     const auto_lvalue_ref1 &operator =(const expr &e) const;
186     };
187    
188     extern const char str_float [];
189     extern const char str_vec2 [];
190     extern const char str_vec3 [];
191     extern const char str_vec4 [];
192     extern const char str_mat2 [];
193     extern const char str_mat3 [];
194     extern const char str_mat4 [];
195    
196     extern const char str_sampler_1d [];
197     extern const char str_sampler_1d_shadow [];
198     extern const char str_sampler_2d [];
199     extern const char str_sampler_2d_shadow [];
200     extern const char str_sampler_2d_rect [];
201     extern const char str_sampler_2d_rect_shadow [];
202     extern const char str_sampler_3d [];
203     extern const char str_sampler_3d_rect [];
204     extern const char str_sampler_cube [];
205    
206     typedef ref<struct var_i> var;
207     typedef ref<struct uniform_i> uniform;
208    
209     struct shader_builder
210     {
211     vector<uniform> refs; // uniform parameters
212     vector<var> temps; // temporary variables
213     vector<var> streams; // varying inputs & outputs
214    
215     ostringstream source;
216    
217     template<typename type>
218     shader_builder &operator <<(const type &t)
219     {
220     source << t;
221     return *this;
222     }
223     };
224    
225     struct fragment_i : refcounted
226     {
227     virtual void build (shader_builder &b) = 0;
228     };
229    
230     typedef ref<fragment_i> fragment;
231    
232     struct lvalue_i : fragment_i
233     {
234     };
235    
236     // a simple predeclared variable with unspecified type
237     struct glvar_i : lvalue_i
238     {
239     const char *name;
240     void build (shader_builder &b);
241     glvar_i (const char *name) : name (name) { }
242     };
243    
244     typedef auto_lvalue_ref1<glvar_i, const char *> glvar;
245    
246     struct var_i : lvalue_i
247     {
248     static unsigned int next_id;
249    
250     char name[NAMELEN];
251     const char *typestr;
252    
253     void build (shader_builder &b);
254     virtual void build_decl (ostringstream &b);
255    
256     var_i (const char *typestr);
257     ~var_i ();
258     };
259    
260     struct uniform_i : var_i
261     {
262     bool dirty;
263    
264     virtual void update () = 0;
265    
266     void build (shader_builder &b);
267     void build_decl (ostringstream &b);
268     uniform_i (const char *strtype);
269     };
270    
271     template<int dimension, typename gltype, const char *strtype, void (*upd)(CGparameter, const gltype *)>
272     struct uniform2_i : uniform_i
273     {
274     gltype data[dimension];
275    
276     void update ()
277     {
278     if (dirty)
279     {
280     //upd (param, data);
281     dirty = false;
282     }
283     }
284    
285     uniform2_i () : uniform_i (strtype) { }
286     };
287    
288     template<int dimension, const char *strtype, void (*update)(CGparameter, const GLfloat *)>
289     struct uniform_f_i : uniform2_i<dimension, GLfloat, strtype, update>
290     {
291     };
292    
293     struct uniform_1f_i : uniform_f_i<1, str_float, cgGLSetParameter1fv> {
294     void operator =(GLfloat v)
295     {
296     data[0] = v;
297     dirty = true;
298     }
299     };
300    
301     struct uniform_2f_i : uniform_f_i<2, str_vec2, cgGLSetParameter2fv> { };
302    
303     struct uniform_3f_i : uniform_f_i<3, str_vec3, cgGLSetParameter3fv> {
304     void operator =(const vec3 &v)
305     {
306     data[0] = v.x;
307     data[1] = v.y;
308     data[2] = v.z;
309     dirty = true;
310     }
311     };
312    
313     struct uniform_4f_i : uniform_f_i<4, str_vec4, cgGLSetParameter4fv> {
314     #if 0
315     void operator =(const gl::matrix &m)
316     {
317     memcpy (data, m.data, 16 * sizeof (GLfloat));
318     dirty = true;
319     }
320     #endif
321     };
322    
323     struct uniform_matrix_2f_i : uniform_f_i< 4, str_mat2, cgGLSetMatrixParameterfc> { };
324     struct uniform_matrix_3f_i : uniform_f_i< 9, str_mat3, cgGLSetMatrixParameterfc> { };
325     struct uniform_matrix_4f_i : uniform_f_i<16, str_mat4, cgGLSetMatrixParameterfc> { };
326    
327 root 1.2 template<class var_i>
328     struct var_ref : ref<var_i>
329 root 1.1 {
330 root 1.2 var_ref (const char *glname = 0)
331     : ref<var_i> (*new var_i)
332 root 1.1 {
333     if (glname)
334     strcpy ((*this)->name, glname);
335     }
336     };
337    
338 root 1.2 typedef var_ref<uniform_1f_i> uniform_1f;
339     typedef var_ref<uniform_2f_i> uniform_2f;
340     typedef var_ref<uniform_3f_i> uniform_3f;
341     typedef var_ref<uniform_4f_i> uniform_4f;
342     typedef var_ref<uniform_matrix_2f_i> uniform_matrix_2f;
343     typedef var_ref<uniform_matrix_3f_i> uniform_matrix_3f;
344     typedef var_ref<uniform_matrix_4f_i> uniform_matrix_4f;
345 root 1.1
346     struct stream_i : var_i
347     {
348     void build (shader_builder &b);
349     void build_decl (ostringstream &b);
350 root 1.4 stream_i (const char *strtype);
351 root 1.1 };
352    
353     template<int dimension, GLenum gltype, const char *strtype>
354     struct varying_i : stream_i
355     {
356 root 1.4 varying_i () : stream_i (strtype) { }
357 root 1.1
358     void set (GLsizei stride, GLvoid *ptr)
359     {
360     //cgGLSetParameterPointer (param, dimension, gltype, stride, ptr);
361     }
362     };
363    
364     template<int dimension, const char *strtype>
365     struct varying_f_i : varying_i<dimension, GL_FLOAT, strtype>
366     {
367     void set (const gl::vertex_buffer_object &vb, GLint offset)
368     {
369     varying_i<dimension, GL_FLOAT, strtype>::set (gl::format_stride (vb.format), (GLvoid *)(long)offset);
370     }
371     };
372    
373     struct varying_1f_i : varying_f_i<1, str_float>
374     {
375     };
376    
377     struct varying_2f_i : varying_f_i<2, str_vec2>
378     {
379     void set_t (const gl::vertex_buffer_object &vb)
380     {
381     set (vb, gl::format_offset_t (vb.format));
382     }
383     };
384    
385     struct varying_3f_i : varying_f_i<3, str_vec3>
386     {
387     void set_p (const gl::vertex_buffer_object &vb)
388     {
389     set (vb, gl::format_offset_p (vb.format));
390     }
391    
392     void set_n (const gl::vertex_buffer_object &vb)
393     {
394     set (vb, gl::format_offset_n (vb.format));
395     }
396     };
397    
398     struct varying_4f_i : varying_f_i<4, str_vec4>
399     {
400     };
401    
402 root 1.2 typedef var_ref<varying_1f_i> varying_1f;
403     typedef var_ref<varying_2f_i> varying_2f;
404     typedef var_ref<varying_3f_i> varying_3f;
405     typedef var_ref<varying_4f_i> varying_4f;
406 root 1.1
407     struct temporary_i : var_i
408     {
409     void build (shader_builder &b);
410    
411     temporary_i (const char *strtype);
412     };
413    
414     template<const char *strtype>
415     struct temp_ref : ref<temporary_i>
416     {
417     temp_ref ()
418     : ref<temporary_i> (*new temporary_i (strtype))
419     {
420     }
421    
422     template<typename expr>
423     const temp_ref &operator =(const expr &e) const;
424     };
425    
426     typedef temp_ref<str_float> temp_1f;
427     typedef temp_ref<str_vec2> temp_2f;
428     typedef temp_ref<str_vec3> temp_3f;
429     typedef temp_ref<str_vec4> temp_4f;
430     typedef temp_ref<str_mat2> temp_matrix_2f;
431     typedef temp_ref<str_mat3> temp_matrix_3f;
432     typedef temp_ref<str_mat4> temp_matrix_4f;
433    
434     template<GLenum gltype, const char *strtype>
435     struct sampler_i : uniform_i
436     {
437     GLuint texture;
438    
439     void update ()
440     {
441     if (dirty)
442     {
443     //cgGLSetTextureParameter (param, texture);
444     dirty = false;
445     }
446     }
447    
448     void begin ()
449     {
450     cgGLEnableTextureParameter (texture);
451     }
452    
453     sampler_i (GLuint texturename) : uniform_i (strtype), texture (texturename) { }
454     };
455    
456     struct sampler_1d_i : sampler_i<CG_SAMPLER1D, str_sampler_1d>
457     {
458     sampler_1d_i (GLuint texturename) : sampler_i<CG_SAMPLER1D, str_sampler_1d> (texturename) { }
459     };
460    
461     struct sampler_1d_shadow_i : sampler_i<CG_SAMPLER1D, str_sampler_1d_shadow>
462     {
463     sampler_1d_shadow_i (GLuint texturename) : sampler_i<CG_SAMPLER1D, str_sampler_1d_shadow> (texturename) { }
464     };
465    
466     struct sampler_2d_i : sampler_i<CG_SAMPLER2D, str_sampler_2d>
467     {
468     sampler_2d_i (GLuint texturename) : sampler_i<CG_SAMPLER2D, str_sampler_2d> (texturename) { }
469     };
470    
471     struct sampler_2d_shadow_i : sampler_i<CG_SAMPLER2D, str_sampler_2d_shadow>
472     {
473     sampler_2d_shadow_i (GLuint texturename) : sampler_i<CG_SAMPLER2D, str_sampler_2d_shadow> (texturename) { }
474     };
475    
476     struct sampler_2d_rect_i : sampler_i<CG_SAMPLERRECT, str_sampler_2d_rect>
477     {
478     sampler_2d_rect_i (GLuint texturename) : sampler_i<CG_SAMPLERRECT, str_sampler_2d_rect> (texturename) { }
479     };
480    
481     struct sampler_2d_rect_shadow_i : sampler_i<CG_SAMPLERRECT, str_sampler_2d_rect_shadow>
482     {
483     sampler_2d_rect_shadow_i (GLuint texturename) : sampler_i<CG_SAMPLERRECT, str_sampler_2d_rect_shadow> (texturename) { }
484     };
485    
486     struct sampler_3d_i : sampler_i<CG_SAMPLER3D, str_sampler_3d>
487     {
488     sampler_3d_i (GLuint texturename) : sampler_i<CG_SAMPLER3D, str_sampler_3d> (texturename) { }
489     };
490    
491     struct sampler_3d_rect_i : sampler_i<CG_SAMPLER3D, str_sampler_3d_rect>
492     {
493     sampler_3d_rect_i (GLuint texturename) : sampler_i<CG_SAMPLER3D, str_sampler_3d_rect> (texturename) { }
494     };
495    
496     struct sampler_cube_i : sampler_i<CG_SAMPLERCUBE, str_sampler_cube>
497     {
498     sampler_cube_i (GLuint texturename) : sampler_i<CG_SAMPLERCUBE, str_sampler_cube> (texturename) { }
499     };
500    
501     typedef auto_ref1<sampler_1d_i, GLuint> sampler_1d;
502     typedef auto_ref1<sampler_1d_shadow_i, GLuint> sampler_1d_shadow;
503     typedef auto_ref1<sampler_2d_i, GLuint> sampler_2d;
504     typedef auto_ref1<sampler_2d_shadow_i, GLuint> sampler_2d_shadow;
505     typedef auto_ref1<sampler_2d_rect_i, GLuint> sampler_2d_rect;
506     typedef auto_ref1<sampler_2d_rect_shadow_i, GLuint> sampler_2d_rect_shadow;
507     typedef auto_ref1<sampler_3d_i, GLuint> sampler_3d;
508     typedef auto_ref1<sampler_3d_rect_i, GLuint> sampler_3d_rect;
509     typedef auto_ref1<sampler_cube_i, GLuint> sampler_cube;
510    
511     struct fragment_const_string_i : fragment_i
512     {
513     const char *str;
514    
515     void build (shader_builder &b);
516    
517     fragment_const_string_i (const char *str) : str(str) { }
518     };
519    
520     typedef auto_ref1<fragment_const_string_i, const char *> fragment_const_string;
521    
522     struct fragment_string_i : fragment_i
523     {
524     char *str;
525    
526     void build (shader_builder &b);
527    
528     fragment_string_i (const char *str) : str (strdup (str)) { }
529     ~fragment_string_i ();
530     };
531    
532     struct fragment_vector_i : fragment_i, vector<fragment>
533     {
534     void build (shader_builder &b);
535    
536     template<class fragment_i>
537     void append (const ref<fragment_i> &f)
538     {
539     push_back (f);
540     }
541    
542 root 1.5 template<class expr>
543     void append (const sl_expr<expr> &e)
544     {
545     e ();
546     }
547    
548 root 1.1 void append_const (const char *s)
549     {
550     push_back (*new fragment_const_string_i (s));
551     }
552    
553     void append_string (const char *s)
554     {
555     push_back (*new fragment_string_i (s));
556     }
557    
558     #if 0
559     fragment_vector_i &operator <<(statement_i &f)
560     {
561     push_back (*new fragment_const_string_i (" "));
562    
563     for (vector<fragment>::iterator i = f.begin (); i != f.end (); i++)
564     push_back (*i);
565    
566     push_back (*new fragment_const_string_i (";\n"));
567     return *this;
568     }
569     #endif
570     };
571    
572     typedef ref<fragment_vector_i> fragment_vector;
573    
574     struct shader_object_i : fragment_vector_i
575     {
576     GLenum type;
577     GLuint id; // GLhandleARB, but 2.0 will use uint
578    
579     string source ();
580     void compile ();
581     void start ();
582     void stop ();
583    
584     shader_object_i (GLenum type);
585     ~shader_object_i ();
586     };
587    
588     extern shader_object_i *cur; // record actions to this shader
589    
590     template<GLenum type>
591     struct shader_object : ref<shader_object_i>
592     {
593     shader_object ()
594     : ref<shader_object_i> (*new shader_object_i (type))
595     {
596     }
597     };
598    
599     typedef shader_object<GL_VERTEX_SHADER_ARB> vertex_shader;
600     typedef shader_object<GL_FRAGMENT_SHADER_ARB> fragment_shader;
601    
602 root 1.3 template<typename T>
603     struct sl_append
604     {
605     T t;
606    
607     sl_append (const T &t) : t(t) { }
608    
609     void operator ()() const
610     {
611     cur->push_back (t);
612     }
613     };
614    
615     template<int length>
616     struct sl_string
617     {
618     char str[length];
619    
620     void operator ()() const
621     {
622     cur->push_back (*new fragment_string_i (str));
623     }
624     };
625    
626     struct sl_float
627     {
628     const GLfloat c;
629    
630     sl_float (GLfloat c) : c(c) { }
631    
632 root 1.6 void operator ()() const;
633 root 1.3 };
634    
635     template<class A, class B>
636     inline sl_expr< sl_concat2<A, B> >
637     concat (const A &a, const B &b)
638     {
639     typedef sl_concat2<A, B> expr;
640     return sl_expr<expr> (expr (a, b));
641     }
642    
643     template<class A, class B, class C>
644     inline sl_expr< sl_concat3<A, B, C> >
645     concat (const A &a, const B &b, const C &c)
646     {
647     typedef sl_concat3<A, B, C> expr;
648     return sl_expr<expr> (expr (a, b, c));
649     }
650    
651     template<class A, class B, class C, class D>
652     inline sl_expr< sl_concat4<A, B, C, D> >
653     concat (const A &a, const B &b, const C &c, const D &d)
654     {
655     typedef sl_concat4<A, B, C, D> expr;
656     return sl_expr<expr> (expr (a, b, c, d));
657     }
658    
659     template<typename expr>
660     struct sl_convert
661     {
662     typedef sl_expr<expr> T;
663     static inline const T &convert (const T &e)
664     {
665     return e;
666     }
667     };
668    
669     template<>
670     struct sl_convert<GLfloat>
671     {
672     typedef sl_expr<sl_float> T;
673     static inline const T convert (GLfloat f)
674     {
675     return sl_float (f);
676     }
677     };
678    
679     template<>
680 root 1.5 struct sl_convert<GLdouble>
681     {
682     typedef sl_expr<sl_float> T;
683     static inline const T convert (GLdouble d)
684     {
685     return sl_float (d);
686     }
687     };
688    
689     template<>
690     struct sl_convert<GLint>
691     {
692     typedef sl_expr<sl_float> T;
693     static inline const T convert (GLint i)
694     {
695     return sl_float (i);
696     }
697     };
698    
699     template<>
700 root 1.6 struct sl_convert<vec2>
701     {
702     typedef sl_expr< sl_string<60> > T;
703     static const T convert (const vec2 &v);
704     };
705    
706     template<>
707 root 1.3 struct sl_convert<vec3>
708     {
709 root 1.6 typedef sl_expr< sl_string<80> > T;
710     static const T convert (const vec3 &v);
711 root 1.3 };
712    
713     template<>
714     struct sl_convert<vec4>
715     {
716 root 1.6 typedef sl_expr< sl_string<100> > T;
717     static const T convert (const vec4 &v);
718 root 1.3 };
719    
720     template<>
721     template<class V>
722     struct sl_convert< var_ref<V> >
723     {
724     typedef sl_expr< sl_append< var_ref<V> > > T;
725     static inline const T convert (const var_ref<V> &v)
726     {
727     return sl_append< var_ref<V> > (v);
728     }
729     };
730    
731     template<>
732     struct sl_convert<glvar>
733     {
734     typedef sl_expr< sl_append<glvar> > T;
735     static inline const T convert (const glvar &v)
736     {
737     return sl_append<glvar> (v);
738     }
739     };
740    
741     template<>
742     template<const char *strtype>
743     struct sl_convert< temp_ref<strtype> >
744     {
745     typedef sl_expr< sl_append< temp_ref<strtype> > > T;
746     static inline const T convert (const temp_ref<strtype> &v)
747     {
748     return sl_expr< sl_append< temp_ref<strtype> > >(
749     sl_append< temp_ref<strtype> > (v)
750     );
751     }
752     };
753    
754 root 1.5 extern const fragment_const_string str_2sp;
755     extern const fragment_const_string str_equal;
756 root 1.8 extern const fragment_const_string str_comma;
757 root 1.5 extern const fragment_const_string str_endl;
758    
759 root 1.3 template<class fragment, typename expr>
760     inline void sl_assign (const fragment &f, const expr &e)
761     {
762 root 1.5 cur->append (str_2sp);
763 root 1.3 cur->append (f);
764 root 1.5 cur->append (str_equal);
765 root 1.3 sl_convert<expr>::convert (e) ();
766 root 1.5 cur->append (str_endl);
767 root 1.3 }
768    
769     template<class type>
770     template<typename expr>
771     inline const auto_lvalue_ref0<type> &auto_lvalue_ref0<type>::operator =(const expr &e) const
772     {
773     sl_assign (*this, e);
774     return *this;
775     }
776    
777     template<class type, typename arg1>
778     template<typename expr>
779     inline const auto_lvalue_ref1<type,arg1> &auto_lvalue_ref1<type,arg1>::operator =(const expr &e) const
780     {
781     sl_assign (*this, e);
782     return *this;
783     }
784    
785 root 1.5 template<class T>
786     template<typename expr>
787     inline const sl_expr<T> &sl_expr<T>::operator =(const expr &e) const
788     {
789     sl_assign (*this, e);
790     return *this;
791     }
792    
793 root 1.3 template<const char *strtype>
794     template<typename expr>
795     inline const temp_ref<strtype> &temp_ref<strtype>::operator =(const expr &e) const
796     {
797     sl_assign (*this, e);
798     return *this;
799     }
800    
801     struct sl_append_const_string
802     {
803     fragment_const_string str;
804     sl_append_const_string (const char *s)
805     : str (s)
806     { }
807    
808     void operator ()() const
809     {
810     cur->push_back (str);
811     }
812     };
813    
814     # define SHADER_BINOP(op, str) \
815     extern const sl_append_const_string str_ ## str; \
816     template<typename A, typename B> \
817     inline const sl_expr< sl_concat3< typename sl_convert<A>::T, \
818     sl_append_const_string, \
819     typename sl_convert<B>::T > > \
820     operator op(const A &a, const B &b) \
821     { \
822     return concat (sl_convert<A>::convert (a), str_ ## str, sl_convert<B>::convert (b)); \
823     }
824    
825     SHADER_BINOP (+, plus);
826     SHADER_BINOP (-, minus);
827     SHADER_BINOP (*, mul);
828     SHADER_BINOP (/, div);
829    
830     # undef SHADER_BINOP
831    
832 root 1.5 void swizzle_mask (sl_string<7> &s, int mask);
833 root 1.6
834     extern const sl_append_const_string str_lpar;
835     extern const sl_append_const_string str_rpar;
836    
837     template<typename T>
838     inline const sl_expr< sl_concat3< sl_append_const_string,
839     typename sl_convert<T>::T,
840     sl_string<7>
841     > >
842     swizzle (const T &t, int a, int b = 0, int c = 0, int d = 0)
843     {
844     sl_string<7> s;
845     swizzle_mask (s, ((d * 5 + c) * 5 + b) * 5 + a);
846     return concat (str_lpar, sl_convert<T>::convert (t), s);
847     }
848    
849     # define SHADER_SWIZZLE_OP(abcd,type) \
850     template<typename T> \
851     inline const sl_expr< sl_concat3< sl_append_const_string, \
852     typename sl_convert<T>::T, \
853     sl_string<7> \
854     > > \
855     type (const T &t) \
856     { \
857     return swizzle (t, (abcd / 1000) % 5, (abcd / 100) % 5, (abcd / 10) % 5, (abcd / 1) % 5); \
858     }
859    
860     // brute force is lovely, ain't it?
861     SHADER_SWIZZLE_OP (1 , x ) SHADER_SWIZZLE_OP (11 , xx ) SHADER_SWIZZLE_OP (111 , xxx ) SHADER_SWIZZLE_OP (1111, xxxx)
862     SHADER_SWIZZLE_OP (1112, xxxy) SHADER_SWIZZLE_OP (1113, xxxz) SHADER_SWIZZLE_OP (1114, xxxw) SHADER_SWIZZLE_OP (112 , xxy )
863     SHADER_SWIZZLE_OP (1121, xxyx) SHADER_SWIZZLE_OP (1122, xxyy) SHADER_SWIZZLE_OP (1123, xxyz) SHADER_SWIZZLE_OP (1124, xxyw)
864     SHADER_SWIZZLE_OP (113 , xxz ) SHADER_SWIZZLE_OP (1131, xxzx) SHADER_SWIZZLE_OP (1132, xxzy) SHADER_SWIZZLE_OP (1133, xxzz)
865     SHADER_SWIZZLE_OP (1134, xxzw) SHADER_SWIZZLE_OP (114 , xxw ) SHADER_SWIZZLE_OP (1141, xxwx) SHADER_SWIZZLE_OP (1142, xxwy)
866     SHADER_SWIZZLE_OP (1143, xxwz) SHADER_SWIZZLE_OP (1144, xxww) SHADER_SWIZZLE_OP (12 , xy ) SHADER_SWIZZLE_OP (121 , xyx )
867     SHADER_SWIZZLE_OP (1211, xyxx) SHADER_SWIZZLE_OP (1212, xyxy) SHADER_SWIZZLE_OP (1213, xyxz) SHADER_SWIZZLE_OP (1214, xyxw)
868     SHADER_SWIZZLE_OP (122 , xyy ) SHADER_SWIZZLE_OP (1221, xyyx) SHADER_SWIZZLE_OP (1222, xyyy) SHADER_SWIZZLE_OP (1223, xyyz)
869     SHADER_SWIZZLE_OP (1224, xyyw) SHADER_SWIZZLE_OP (123 , xyz ) SHADER_SWIZZLE_OP (1231, xyzx) SHADER_SWIZZLE_OP (1232, xyzy)
870     SHADER_SWIZZLE_OP (1233, xyzz) SHADER_SWIZZLE_OP (1234, xyzw) SHADER_SWIZZLE_OP (124 , xyw ) SHADER_SWIZZLE_OP (1241, xywx)
871     SHADER_SWIZZLE_OP (1242, xywy) SHADER_SWIZZLE_OP (1243, xywz) SHADER_SWIZZLE_OP (1244, xyww) SHADER_SWIZZLE_OP (13 , xz )
872     SHADER_SWIZZLE_OP (131 , xzx ) SHADER_SWIZZLE_OP (1311, xzxx) SHADER_SWIZZLE_OP (1312, xzxy) SHADER_SWIZZLE_OP (1313, xzxz)
873     SHADER_SWIZZLE_OP (1314, xzxw) SHADER_SWIZZLE_OP (132 , xzy ) SHADER_SWIZZLE_OP (1321, xzyx) SHADER_SWIZZLE_OP (1322, xzyy)
874     SHADER_SWIZZLE_OP (1323, xzyz) SHADER_SWIZZLE_OP (1324, xzyw) SHADER_SWIZZLE_OP (133 , xzz ) SHADER_SWIZZLE_OP (1331, xzzx)
875     SHADER_SWIZZLE_OP (1332, xzzy) SHADER_SWIZZLE_OP (1333, xzzz) SHADER_SWIZZLE_OP (1334, xzzw) SHADER_SWIZZLE_OP (134 , xzw )
876     SHADER_SWIZZLE_OP (1341, xzwx) SHADER_SWIZZLE_OP (1342, xzwy) SHADER_SWIZZLE_OP (1343, xzwz) SHADER_SWIZZLE_OP (1344, xzww)
877     SHADER_SWIZZLE_OP (14 , xw ) SHADER_SWIZZLE_OP (141 , xwx ) SHADER_SWIZZLE_OP (1411, xwxx) SHADER_SWIZZLE_OP (1412, xwxy)
878     SHADER_SWIZZLE_OP (1413, xwxz) SHADER_SWIZZLE_OP (1414, xwxw) SHADER_SWIZZLE_OP (142 , xwy ) SHADER_SWIZZLE_OP (1421, xwyx)
879     SHADER_SWIZZLE_OP (1422, xwyy) SHADER_SWIZZLE_OP (1423, xwyz) SHADER_SWIZZLE_OP (1424, xwyw) SHADER_SWIZZLE_OP (143 , xwz )
880     SHADER_SWIZZLE_OP (1431, xwzx) SHADER_SWIZZLE_OP (1432, xwzy) SHADER_SWIZZLE_OP (1433, xwzz) SHADER_SWIZZLE_OP (1434, xwzw)
881     SHADER_SWIZZLE_OP (144 , xww ) SHADER_SWIZZLE_OP (1441, xwwx) SHADER_SWIZZLE_OP (1442, xwwy) SHADER_SWIZZLE_OP (1443, xwwz)
882     SHADER_SWIZZLE_OP (1444, xwww) SHADER_SWIZZLE_OP (2 , y ) SHADER_SWIZZLE_OP (21 , yx ) SHADER_SWIZZLE_OP (211 , yxx )
883     SHADER_SWIZZLE_OP (2111, yxxx) SHADER_SWIZZLE_OP (2112, yxxy) SHADER_SWIZZLE_OP (2113, yxxz) SHADER_SWIZZLE_OP (2114, yxxw)
884     SHADER_SWIZZLE_OP (212 , yxy ) SHADER_SWIZZLE_OP (2121, yxyx) SHADER_SWIZZLE_OP (2122, yxyy) SHADER_SWIZZLE_OP (2123, yxyz)
885     SHADER_SWIZZLE_OP (2124, yxyw) SHADER_SWIZZLE_OP (213 , yxz ) SHADER_SWIZZLE_OP (2131, yxzx) SHADER_SWIZZLE_OP (2132, yxzy)
886     SHADER_SWIZZLE_OP (2133, yxzz) SHADER_SWIZZLE_OP (2134, yxzw) SHADER_SWIZZLE_OP (214 , yxw ) SHADER_SWIZZLE_OP (2141, yxwx)
887     SHADER_SWIZZLE_OP (2142, yxwy) SHADER_SWIZZLE_OP (2143, yxwz) SHADER_SWIZZLE_OP (2144, yxww) SHADER_SWIZZLE_OP (22 , yy )
888     SHADER_SWIZZLE_OP (221 , yyx ) SHADER_SWIZZLE_OP (2211, yyxx) SHADER_SWIZZLE_OP (2212, yyxy) SHADER_SWIZZLE_OP (2213, yyxz)
889     SHADER_SWIZZLE_OP (2214, yyxw) SHADER_SWIZZLE_OP (222 , yyy ) SHADER_SWIZZLE_OP (2221, yyyx) SHADER_SWIZZLE_OP (2222, yyyy)
890     SHADER_SWIZZLE_OP (2223, yyyz) SHADER_SWIZZLE_OP (2224, yyyw) SHADER_SWIZZLE_OP (223 , yyz ) SHADER_SWIZZLE_OP (2231, yyzx)
891     SHADER_SWIZZLE_OP (2232, yyzy) SHADER_SWIZZLE_OP (2233, yyzz) SHADER_SWIZZLE_OP (2234, yyzw) SHADER_SWIZZLE_OP (224 , yyw )
892     SHADER_SWIZZLE_OP (2241, yywx) SHADER_SWIZZLE_OP (2242, yywy) SHADER_SWIZZLE_OP (2243, yywz) SHADER_SWIZZLE_OP (2244, yyww)
893     SHADER_SWIZZLE_OP (23 , yz ) SHADER_SWIZZLE_OP (231 , yzx ) SHADER_SWIZZLE_OP (2311, yzxx) SHADER_SWIZZLE_OP (2312, yzxy)
894     SHADER_SWIZZLE_OP (2313, yzxz) SHADER_SWIZZLE_OP (2314, yzxw) SHADER_SWIZZLE_OP (232 , yzy ) SHADER_SWIZZLE_OP (2321, yzyx)
895     SHADER_SWIZZLE_OP (2322, yzyy) SHADER_SWIZZLE_OP (2323, yzyz) SHADER_SWIZZLE_OP (2324, yzyw) SHADER_SWIZZLE_OP (233 , yzz )
896     SHADER_SWIZZLE_OP (2331, yzzx) SHADER_SWIZZLE_OP (2332, yzzy) SHADER_SWIZZLE_OP (2333, yzzz) SHADER_SWIZZLE_OP (2334, yzzw)
897     SHADER_SWIZZLE_OP (234 , yzw ) SHADER_SWIZZLE_OP (2341, yzwx) SHADER_SWIZZLE_OP (2342, yzwy) SHADER_SWIZZLE_OP (2343, yzwz)
898     SHADER_SWIZZLE_OP (2344, yzww) SHADER_SWIZZLE_OP (24 , yw ) SHADER_SWIZZLE_OP (241 , ywx ) SHADER_SWIZZLE_OP (2411, ywxx)
899     SHADER_SWIZZLE_OP (2412, ywxy) SHADER_SWIZZLE_OP (2413, ywxz) SHADER_SWIZZLE_OP (2414, ywxw) SHADER_SWIZZLE_OP (242 , ywy )
900     SHADER_SWIZZLE_OP (2421, ywyx) SHADER_SWIZZLE_OP (2422, ywyy) SHADER_SWIZZLE_OP (2423, ywyz) SHADER_SWIZZLE_OP (2424, ywyw)
901     SHADER_SWIZZLE_OP (243 , ywz ) SHADER_SWIZZLE_OP (2431, ywzx) SHADER_SWIZZLE_OP (2432, ywzy) SHADER_SWIZZLE_OP (2433, ywzz)
902     SHADER_SWIZZLE_OP (2434, ywzw) SHADER_SWIZZLE_OP (244 , yww ) SHADER_SWIZZLE_OP (2441, ywwx) SHADER_SWIZZLE_OP (2442, ywwy)
903     SHADER_SWIZZLE_OP (2443, ywwz) SHADER_SWIZZLE_OP (2444, ywww) SHADER_SWIZZLE_OP (3 , z ) SHADER_SWIZZLE_OP (31 , zx )
904     SHADER_SWIZZLE_OP (311 , zxx ) SHADER_SWIZZLE_OP (3111, zxxx) SHADER_SWIZZLE_OP (3112, zxxy) SHADER_SWIZZLE_OP (3113, zxxz)
905     SHADER_SWIZZLE_OP (3114, zxxw) SHADER_SWIZZLE_OP (312 , zxy ) SHADER_SWIZZLE_OP (3121, zxyx) SHADER_SWIZZLE_OP (3122, zxyy)
906     SHADER_SWIZZLE_OP (3123, zxyz) SHADER_SWIZZLE_OP (3124, zxyw) SHADER_SWIZZLE_OP (313 , zxz ) SHADER_SWIZZLE_OP (3131, zxzx)
907     SHADER_SWIZZLE_OP (3132, zxzy) SHADER_SWIZZLE_OP (3133, zxzz) SHADER_SWIZZLE_OP (3134, zxzw) SHADER_SWIZZLE_OP (314 , zxw )
908     SHADER_SWIZZLE_OP (3141, zxwx) SHADER_SWIZZLE_OP (3142, zxwy) SHADER_SWIZZLE_OP (3143, zxwz) SHADER_SWIZZLE_OP (3144, zxww)
909     SHADER_SWIZZLE_OP (32 , zy ) SHADER_SWIZZLE_OP (321 , zyx ) SHADER_SWIZZLE_OP (3211, zyxx) SHADER_SWIZZLE_OP (3212, zyxy)
910     SHADER_SWIZZLE_OP (3213, zyxz) SHADER_SWIZZLE_OP (3214, zyxw) SHADER_SWIZZLE_OP (322 , zyy ) SHADER_SWIZZLE_OP (3221, zyyx)
911     SHADER_SWIZZLE_OP (3222, zyyy) SHADER_SWIZZLE_OP (3223, zyyz) SHADER_SWIZZLE_OP (3224, zyyw) SHADER_SWIZZLE_OP (323 , zyz )
912     SHADER_SWIZZLE_OP (3231, zyzx) SHADER_SWIZZLE_OP (3232, zyzy) SHADER_SWIZZLE_OP (3233, zyzz) SHADER_SWIZZLE_OP (3234, zyzw)
913     SHADER_SWIZZLE_OP (324 , zyw ) SHADER_SWIZZLE_OP (3241, zywx) SHADER_SWIZZLE_OP (3242, zywy) SHADER_SWIZZLE_OP (3243, zywz)
914     SHADER_SWIZZLE_OP (3244, zyww) SHADER_SWIZZLE_OP (33 , zz ) SHADER_SWIZZLE_OP (331 , zzx ) SHADER_SWIZZLE_OP (3311, zzxx)
915     SHADER_SWIZZLE_OP (3312, zzxy) SHADER_SWIZZLE_OP (3313, zzxz) SHADER_SWIZZLE_OP (3314, zzxw) SHADER_SWIZZLE_OP (332 , zzy )
916     SHADER_SWIZZLE_OP (3321, zzyx) SHADER_SWIZZLE_OP (3322, zzyy) SHADER_SWIZZLE_OP (3323, zzyz) SHADER_SWIZZLE_OP (3324, zzyw)
917     SHADER_SWIZZLE_OP (333 , zzz ) SHADER_SWIZZLE_OP (3331, zzzx) SHADER_SWIZZLE_OP (3332, zzzy) SHADER_SWIZZLE_OP (3333, zzzz)
918     SHADER_SWIZZLE_OP (3334, zzzw) SHADER_SWIZZLE_OP (334 , zzw ) SHADER_SWIZZLE_OP (3341, zzwx) SHADER_SWIZZLE_OP (3342, zzwy)
919     SHADER_SWIZZLE_OP (3343, zzwz) SHADER_SWIZZLE_OP (3344, zzww) SHADER_SWIZZLE_OP (34 , zw ) SHADER_SWIZZLE_OP (341 , zwx )
920     SHADER_SWIZZLE_OP (3411, zwxx) SHADER_SWIZZLE_OP (3412, zwxy) SHADER_SWIZZLE_OP (3413, zwxz) SHADER_SWIZZLE_OP (3414, zwxw)
921     SHADER_SWIZZLE_OP (342 , zwy ) SHADER_SWIZZLE_OP (3421, zwyx) SHADER_SWIZZLE_OP (3422, zwyy) SHADER_SWIZZLE_OP (3423, zwyz)
922     SHADER_SWIZZLE_OP (3424, zwyw) SHADER_SWIZZLE_OP (343 , zwz ) SHADER_SWIZZLE_OP (3431, zwzx) SHADER_SWIZZLE_OP (3432, zwzy)
923     SHADER_SWIZZLE_OP (3433, zwzz) SHADER_SWIZZLE_OP (3434, zwzw) SHADER_SWIZZLE_OP (344 , zww ) SHADER_SWIZZLE_OP (3441, zwwx)
924     SHADER_SWIZZLE_OP (3442, zwwy) SHADER_SWIZZLE_OP (3443, zwwz) SHADER_SWIZZLE_OP (3444, zwww) SHADER_SWIZZLE_OP (4 , w )
925     SHADER_SWIZZLE_OP (41 , wx ) SHADER_SWIZZLE_OP (411 , wxx ) SHADER_SWIZZLE_OP (4111, wxxx) SHADER_SWIZZLE_OP (4112, wxxy)
926     SHADER_SWIZZLE_OP (4113, wxxz) SHADER_SWIZZLE_OP (4114, wxxw) SHADER_SWIZZLE_OP (412 , wxy ) SHADER_SWIZZLE_OP (4121, wxyx)
927     SHADER_SWIZZLE_OP (4122, wxyy) SHADER_SWIZZLE_OP (4123, wxyz) SHADER_SWIZZLE_OP (4124, wxyw) SHADER_SWIZZLE_OP (413 , wxz )
928     SHADER_SWIZZLE_OP (4131, wxzx) SHADER_SWIZZLE_OP (4132, wxzy) SHADER_SWIZZLE_OP (4133, wxzz) SHADER_SWIZZLE_OP (4134, wxzw)
929     SHADER_SWIZZLE_OP (414 , wxw ) SHADER_SWIZZLE_OP (4141, wxwx) SHADER_SWIZZLE_OP (4142, wxwy) SHADER_SWIZZLE_OP (4143, wxwz)
930     SHADER_SWIZZLE_OP (4144, wxww) SHADER_SWIZZLE_OP (42 , wy ) SHADER_SWIZZLE_OP (421 , wyx ) SHADER_SWIZZLE_OP (4211, wyxx)
931     SHADER_SWIZZLE_OP (4212, wyxy) SHADER_SWIZZLE_OP (4213, wyxz) SHADER_SWIZZLE_OP (4214, wyxw) SHADER_SWIZZLE_OP (422 , wyy )
932     SHADER_SWIZZLE_OP (4221, wyyx) SHADER_SWIZZLE_OP (4222, wyyy) SHADER_SWIZZLE_OP (4223, wyyz) SHADER_SWIZZLE_OP (4224, wyyw)
933     SHADER_SWIZZLE_OP (423 , wyz ) SHADER_SWIZZLE_OP (4231, wyzx) SHADER_SWIZZLE_OP (4232, wyzy) SHADER_SWIZZLE_OP (4233, wyzz)
934     SHADER_SWIZZLE_OP (4234, wyzw) SHADER_SWIZZLE_OP (424 , wyw ) SHADER_SWIZZLE_OP (4241, wywx) SHADER_SWIZZLE_OP (4242, wywy)
935     SHADER_SWIZZLE_OP (4243, wywz) SHADER_SWIZZLE_OP (4244, wyww) SHADER_SWIZZLE_OP (43 , wz ) SHADER_SWIZZLE_OP (431 , wzx )
936     SHADER_SWIZZLE_OP (4311, wzxx) SHADER_SWIZZLE_OP (4312, wzxy) SHADER_SWIZZLE_OP (4313, wzxz) SHADER_SWIZZLE_OP (4314, wzxw)
937     SHADER_SWIZZLE_OP (432 , wzy ) SHADER_SWIZZLE_OP (4321, wzyx) SHADER_SWIZZLE_OP (4322, wzyy) SHADER_SWIZZLE_OP (4323, wzyz)
938     SHADER_SWIZZLE_OP (4324, wzyw) SHADER_SWIZZLE_OP (433 , wzz ) SHADER_SWIZZLE_OP (4331, wzzx) SHADER_SWIZZLE_OP (4332, wzzy)
939     SHADER_SWIZZLE_OP (4333, wzzz) SHADER_SWIZZLE_OP (4334, wzzw) SHADER_SWIZZLE_OP (434 , wzw ) SHADER_SWIZZLE_OP (4341, wzwx)
940     SHADER_SWIZZLE_OP (4342, wzwy) SHADER_SWIZZLE_OP (4343, wzwz) SHADER_SWIZZLE_OP (4344, wzww) SHADER_SWIZZLE_OP (44 , ww )
941     SHADER_SWIZZLE_OP (441 , wwx ) SHADER_SWIZZLE_OP (4411, wwxx) SHADER_SWIZZLE_OP (4412, wwxy) SHADER_SWIZZLE_OP (4413, wwxz)
942     SHADER_SWIZZLE_OP (4414, wwxw) SHADER_SWIZZLE_OP (442 , wwy ) SHADER_SWIZZLE_OP (4421, wwyx) SHADER_SWIZZLE_OP (4422, wwyy)
943     SHADER_SWIZZLE_OP (4423, wwyz) SHADER_SWIZZLE_OP (4424, wwyw) SHADER_SWIZZLE_OP (443 , wwz ) SHADER_SWIZZLE_OP (4431, wwzx)
944     SHADER_SWIZZLE_OP (4432, wwzy) SHADER_SWIZZLE_OP (4433, wwzz) SHADER_SWIZZLE_OP (4434, wwzw) SHADER_SWIZZLE_OP (444 , www )
945     SHADER_SWIZZLE_OP (4441, wwwx) SHADER_SWIZZLE_OP (4442, wwwy) SHADER_SWIZZLE_OP (4443, wwwz) SHADER_SWIZZLE_OP (4444, wwww)
946    
947     # undef SHADER_SWIZZLE_OP
948    
949 root 1.1 void debdebdebdebug ();//D
950     }
951    
952     #endif
953