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