ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/shader.h
Revision: 1.4
Committed: Sat Oct 23 21:55:13 2004 UTC (19 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.3: +2 -14 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     };
21    
22     template<class A, class B>
23     struct sl_concat2
24     {
25     const A a; const B b;
26     sl_concat2 (const A &a, const B &b) : a(a), b(b) { }
27     void operator ()() const { a (); b (); }
28     };
29    
30     template<class A, class B, class C>
31     struct sl_concat3
32     {
33     const A a; const B b; const C c;
34     sl_concat3 (const A &a, const B &b, const C &c) : a(a), b(b), c(c) { }
35     void operator ()() const { a (); b (); c (); }
36     };
37    
38     template<class A, class B, class C, class D>
39     struct sl_concat4
40     {
41     const A a; const B b; const C c; const D d;
42     sl_concat4 (const A &a, const B &b, const C &c, const D &d) : a(a), b(b), c(c), d(d) { }
43     void operator ()() const { a (); b (); c (); d(); }
44     };
45    
46     class refcounted
47     {
48     template<class type> friend class ref;
49     mutable int refcnt;
50     void refcnt_inc () const { refcnt++; }
51     void refcnt_dec () const { if (!--refcnt) delete this; };
52     public:
53     refcounted () : refcnt(0) { }
54     virtual ~refcounted ();
55     };
56    
57     template<class type>
58     struct ref
59     {
60     type *p;
61    
62     ref ()
63     {
64     p = new type;
65     p->refcnt_inc ();
66     }
67    
68     ref (type &d)
69     {
70     d.refcnt_inc ();
71     p = &d;
72     }
73    
74     ref (const ref &r)
75     {
76     r.p->refcnt_inc ();
77     p = r.p;
78     }
79    
80     template<class type2>
81     ref (const ref<type2> &r)
82     {
83     r.p->refcnt_inc ();
84     p = r.p;
85     }
86    
87     ~ref ()
88     {
89     p->refcnt_dec ();
90     }
91    
92     ref &operator =(type &d)
93     {
94     d.refcnt_inc ();
95     p->refcnt_dec ();
96     p = &d;
97     return *this;
98     }
99    
100     type *operator ->() const { return p; }
101    
102     operator type &() const { return *p; }
103    
104     template<class type2>
105     bool operator ==(const ref<type2> b) const
106     {
107     return (void *)p == (void *)b.p;
108     }
109     };
110    
111     // ref with auto-construct
112     template<class type>
113     struct auto_ref0 : ref<type>
114     {
115     auto_ref0 () : ref<type> (*new type ()) { }
116     };
117    
118     template<class type, typename arg1>
119     struct auto_ref1 : ref<type>
120     {
121     auto_ref1 (arg1 a) : ref<type> (*new type (a)) { }
122     };
123    
124     template<class type>
125     struct auto_lvalue_ref0 : ref<type>
126     {
127     auto_lvalue_ref0 () : ref<type> (*new type ()) { }
128     template<typename expr>
129     const auto_lvalue_ref0 &operator =(const expr &e) const;
130     };
131    
132     template<class type, typename arg1>
133     struct auto_lvalue_ref1 : ref<type>
134     {
135     auto_lvalue_ref1 (arg1 a) : ref<type> (*new type (a)) { }
136     template<class expr>
137     const auto_lvalue_ref1 &operator =(const expr &e) const;
138     };
139    
140     extern const char str_float [];
141     extern const char str_vec2 [];
142     extern const char str_vec3 [];
143     extern const char str_vec4 [];
144     extern const char str_mat2 [];
145     extern const char str_mat3 [];
146     extern const char str_mat4 [];
147    
148     extern const char str_sampler_1d [];
149     extern const char str_sampler_1d_shadow [];
150     extern const char str_sampler_2d [];
151     extern const char str_sampler_2d_shadow [];
152     extern const char str_sampler_2d_rect [];
153     extern const char str_sampler_2d_rect_shadow [];
154     extern const char str_sampler_3d [];
155     extern const char str_sampler_3d_rect [];
156     extern const char str_sampler_cube [];
157    
158     typedef ref<struct var_i> var;
159     typedef ref<struct uniform_i> uniform;
160    
161     struct shader_builder
162     {
163     vector<uniform> refs; // uniform parameters
164     vector<var> temps; // temporary variables
165     vector<var> streams; // varying inputs & outputs
166    
167     ostringstream source;
168    
169     template<typename type>
170     shader_builder &operator <<(const type &t)
171     {
172     source << t;
173     return *this;
174     }
175     };
176    
177     struct fragment_i : refcounted
178     {
179     virtual void build (shader_builder &b) = 0;
180     };
181    
182     typedef ref<fragment_i> fragment;
183    
184     struct lvalue_i : fragment_i
185     {
186     };
187    
188     // a simple predeclared variable with unspecified type
189     struct glvar_i : lvalue_i
190     {
191     const char *name;
192     void build (shader_builder &b);
193     glvar_i (const char *name) : name (name) { }
194     };
195    
196     typedef auto_lvalue_ref1<glvar_i, const char *> glvar;
197    
198     struct var_i : lvalue_i
199     {
200     static unsigned int next_id;
201    
202     char name[NAMELEN];
203     const char *typestr;
204    
205     void build (shader_builder &b);
206     virtual void build_decl (ostringstream &b);
207    
208     var_i (const char *typestr);
209     ~var_i ();
210     };
211    
212     struct uniform_i : var_i
213     {
214     bool dirty;
215    
216     virtual void update () = 0;
217    
218     void build (shader_builder &b);
219     void build_decl (ostringstream &b);
220     uniform_i (const char *strtype);
221     };
222    
223     template<int dimension, typename gltype, const char *strtype, void (*upd)(CGparameter, const gltype *)>
224     struct uniform2_i : uniform_i
225     {
226     gltype data[dimension];
227    
228     void update ()
229     {
230     if (dirty)
231     {
232     //upd (param, data);
233     dirty = false;
234     }
235     }
236    
237     uniform2_i () : uniform_i (strtype) { }
238     };
239    
240     template<int dimension, const char *strtype, void (*update)(CGparameter, const GLfloat *)>
241     struct uniform_f_i : uniform2_i<dimension, GLfloat, strtype, update>
242     {
243     };
244    
245     struct uniform_1f_i : uniform_f_i<1, str_float, cgGLSetParameter1fv> {
246     void operator =(GLfloat v)
247     {
248     data[0] = v;
249     dirty = true;
250     }
251     };
252    
253     struct uniform_2f_i : uniform_f_i<2, str_vec2, cgGLSetParameter2fv> { };
254    
255     struct uniform_3f_i : uniform_f_i<3, str_vec3, cgGLSetParameter3fv> {
256     void operator =(const vec3 &v)
257     {
258     data[0] = v.x;
259     data[1] = v.y;
260     data[2] = v.z;
261     dirty = true;
262     }
263     };
264    
265     struct uniform_4f_i : uniform_f_i<4, str_vec4, cgGLSetParameter4fv> {
266     #if 0
267     void operator =(const gl::matrix &m)
268     {
269     memcpy (data, m.data, 16 * sizeof (GLfloat));
270     dirty = true;
271     }
272     #endif
273     };
274    
275     struct uniform_matrix_2f_i : uniform_f_i< 4, str_mat2, cgGLSetMatrixParameterfc> { };
276     struct uniform_matrix_3f_i : uniform_f_i< 9, str_mat3, cgGLSetMatrixParameterfc> { };
277     struct uniform_matrix_4f_i : uniform_f_i<16, str_mat4, cgGLSetMatrixParameterfc> { };
278    
279 root 1.2 template<class var_i>
280     struct var_ref : ref<var_i>
281 root 1.1 {
282 root 1.2 var_ref (const char *glname = 0)
283     : ref<var_i> (*new var_i)
284 root 1.1 {
285     if (glname)
286     strcpy ((*this)->name, glname);
287     }
288     };
289    
290 root 1.2 typedef var_ref<uniform_1f_i> uniform_1f;
291     typedef var_ref<uniform_2f_i> uniform_2f;
292     typedef var_ref<uniform_3f_i> uniform_3f;
293     typedef var_ref<uniform_4f_i> uniform_4f;
294     typedef var_ref<uniform_matrix_2f_i> uniform_matrix_2f;
295     typedef var_ref<uniform_matrix_3f_i> uniform_matrix_3f;
296     typedef var_ref<uniform_matrix_4f_i> uniform_matrix_4f;
297 root 1.1
298     struct stream_i : var_i
299     {
300     void build (shader_builder &b);
301     void build_decl (ostringstream &b);
302 root 1.4 stream_i (const char *strtype);
303 root 1.1 };
304    
305     template<int dimension, GLenum gltype, const char *strtype>
306     struct varying_i : stream_i
307     {
308 root 1.4 varying_i () : stream_i (strtype) { }
309 root 1.1
310     void set (GLsizei stride, GLvoid *ptr)
311     {
312     //cgGLSetParameterPointer (param, dimension, gltype, stride, ptr);
313     }
314     };
315    
316     template<int dimension, const char *strtype>
317     struct varying_f_i : varying_i<dimension, GL_FLOAT, strtype>
318     {
319     void set (const gl::vertex_buffer_object &vb, GLint offset)
320     {
321     varying_i<dimension, GL_FLOAT, strtype>::set (gl::format_stride (vb.format), (GLvoid *)(long)offset);
322     }
323     };
324    
325     struct varying_1f_i : varying_f_i<1, str_float>
326     {
327     };
328    
329     struct varying_2f_i : varying_f_i<2, str_vec2>
330     {
331     void set_t (const gl::vertex_buffer_object &vb)
332     {
333     set (vb, gl::format_offset_t (vb.format));
334     }
335     };
336    
337     struct varying_3f_i : varying_f_i<3, str_vec3>
338     {
339     void set_p (const gl::vertex_buffer_object &vb)
340     {
341     set (vb, gl::format_offset_p (vb.format));
342     }
343    
344     void set_n (const gl::vertex_buffer_object &vb)
345     {
346     set (vb, gl::format_offset_n (vb.format));
347     }
348     };
349    
350     struct varying_4f_i : varying_f_i<4, str_vec4>
351     {
352     };
353    
354 root 1.2 typedef var_ref<varying_1f_i> varying_1f;
355     typedef var_ref<varying_2f_i> varying_2f;
356     typedef var_ref<varying_3f_i> varying_3f;
357     typedef var_ref<varying_4f_i> varying_4f;
358 root 1.1
359     struct temporary_i : var_i
360     {
361     void build (shader_builder &b);
362    
363     temporary_i (const char *strtype);
364     };
365    
366     template<const char *strtype>
367     struct temp_ref : ref<temporary_i>
368     {
369     temp_ref ()
370     : ref<temporary_i> (*new temporary_i (strtype))
371     {
372     }
373    
374     template<typename expr>
375     const temp_ref &operator =(const expr &e) const;
376     };
377    
378     typedef temp_ref<str_float> temp_1f;
379     typedef temp_ref<str_vec2> temp_2f;
380     typedef temp_ref<str_vec3> temp_3f;
381     typedef temp_ref<str_vec4> temp_4f;
382     typedef temp_ref<str_mat2> temp_matrix_2f;
383     typedef temp_ref<str_mat3> temp_matrix_3f;
384     typedef temp_ref<str_mat4> temp_matrix_4f;
385    
386     template<GLenum gltype, const char *strtype>
387     struct sampler_i : uniform_i
388     {
389     GLuint texture;
390    
391     void update ()
392     {
393     if (dirty)
394     {
395     //cgGLSetTextureParameter (param, texture);
396     dirty = false;
397     }
398     }
399    
400     void begin ()
401     {
402     cgGLEnableTextureParameter (texture);
403     }
404    
405     sampler_i (GLuint texturename) : uniform_i (strtype), texture (texturename) { }
406     };
407    
408     struct sampler_1d_i : sampler_i<CG_SAMPLER1D, str_sampler_1d>
409     {
410     sampler_1d_i (GLuint texturename) : sampler_i<CG_SAMPLER1D, str_sampler_1d> (texturename) { }
411     };
412    
413     struct sampler_1d_shadow_i : sampler_i<CG_SAMPLER1D, str_sampler_1d_shadow>
414     {
415     sampler_1d_shadow_i (GLuint texturename) : sampler_i<CG_SAMPLER1D, str_sampler_1d_shadow> (texturename) { }
416     };
417    
418     struct sampler_2d_i : sampler_i<CG_SAMPLER2D, str_sampler_2d>
419     {
420     sampler_2d_i (GLuint texturename) : sampler_i<CG_SAMPLER2D, str_sampler_2d> (texturename) { }
421     };
422    
423     struct sampler_2d_shadow_i : sampler_i<CG_SAMPLER2D, str_sampler_2d_shadow>
424     {
425     sampler_2d_shadow_i (GLuint texturename) : sampler_i<CG_SAMPLER2D, str_sampler_2d_shadow> (texturename) { }
426     };
427    
428     struct sampler_2d_rect_i : sampler_i<CG_SAMPLERRECT, str_sampler_2d_rect>
429     {
430     sampler_2d_rect_i (GLuint texturename) : sampler_i<CG_SAMPLERRECT, str_sampler_2d_rect> (texturename) { }
431     };
432    
433     struct sampler_2d_rect_shadow_i : sampler_i<CG_SAMPLERRECT, str_sampler_2d_rect_shadow>
434     {
435     sampler_2d_rect_shadow_i (GLuint texturename) : sampler_i<CG_SAMPLERRECT, str_sampler_2d_rect_shadow> (texturename) { }
436     };
437    
438     struct sampler_3d_i : sampler_i<CG_SAMPLER3D, str_sampler_3d>
439     {
440     sampler_3d_i (GLuint texturename) : sampler_i<CG_SAMPLER3D, str_sampler_3d> (texturename) { }
441     };
442    
443     struct sampler_3d_rect_i : sampler_i<CG_SAMPLER3D, str_sampler_3d_rect>
444     {
445     sampler_3d_rect_i (GLuint texturename) : sampler_i<CG_SAMPLER3D, str_sampler_3d_rect> (texturename) { }
446     };
447    
448     struct sampler_cube_i : sampler_i<CG_SAMPLERCUBE, str_sampler_cube>
449     {
450     sampler_cube_i (GLuint texturename) : sampler_i<CG_SAMPLERCUBE, str_sampler_cube> (texturename) { }
451     };
452    
453     typedef auto_ref1<sampler_1d_i, GLuint> sampler_1d;
454     typedef auto_ref1<sampler_1d_shadow_i, GLuint> sampler_1d_shadow;
455     typedef auto_ref1<sampler_2d_i, GLuint> sampler_2d;
456     typedef auto_ref1<sampler_2d_shadow_i, GLuint> sampler_2d_shadow;
457     typedef auto_ref1<sampler_2d_rect_i, GLuint> sampler_2d_rect;
458     typedef auto_ref1<sampler_2d_rect_shadow_i, GLuint> sampler_2d_rect_shadow;
459     typedef auto_ref1<sampler_3d_i, GLuint> sampler_3d;
460     typedef auto_ref1<sampler_3d_rect_i, GLuint> sampler_3d_rect;
461     typedef auto_ref1<sampler_cube_i, GLuint> sampler_cube;
462    
463     struct fragment_const_string_i : fragment_i
464     {
465     const char *str;
466    
467     void build (shader_builder &b);
468    
469     fragment_const_string_i (const char *str) : str(str) { }
470     };
471    
472     typedef auto_ref1<fragment_const_string_i, const char *> fragment_const_string;
473    
474     struct fragment_string_i : fragment_i
475     {
476     char *str;
477    
478     void build (shader_builder &b);
479    
480     fragment_string_i (const char *str) : str (strdup (str)) { }
481     ~fragment_string_i ();
482     };
483    
484     struct fragment_vector_i : fragment_i, vector<fragment>
485     {
486     void build (shader_builder &b);
487    
488     template<class fragment_i>
489     void append (const ref<fragment_i> &f)
490     {
491     push_back (f);
492     }
493    
494     void append_const (const char *s)
495     {
496     push_back (*new fragment_const_string_i (s));
497     }
498    
499     void append_string (const char *s)
500     {
501     push_back (*new fragment_string_i (s));
502     }
503    
504     #if 0
505     fragment_vector_i &operator <<(statement_i &f)
506     {
507     push_back (*new fragment_const_string_i (" "));
508    
509     for (vector<fragment>::iterator i = f.begin (); i != f.end (); i++)
510     push_back (*i);
511    
512     push_back (*new fragment_const_string_i (";\n"));
513     return *this;
514     }
515     #endif
516     };
517    
518     typedef ref<fragment_vector_i> fragment_vector;
519    
520     struct shader_object_i : fragment_vector_i
521     {
522     GLenum type;
523     GLuint id; // GLhandleARB, but 2.0 will use uint
524    
525     string source ();
526     void compile ();
527     void start ();
528     void stop ();
529    
530     shader_object_i (GLenum type);
531     ~shader_object_i ();
532     };
533    
534     extern shader_object_i *cur; // record actions to this shader
535    
536     template<GLenum type>
537     struct shader_object : ref<shader_object_i>
538     {
539     shader_object ()
540     : ref<shader_object_i> (*new shader_object_i (type))
541     {
542     }
543     };
544    
545     typedef shader_object<GL_VERTEX_SHADER_ARB> vertex_shader;
546     typedef shader_object<GL_FRAGMENT_SHADER_ARB> fragment_shader;
547    
548 root 1.3 template<typename T>
549     struct sl_append
550     {
551     T t;
552    
553     sl_append (const T &t) : t(t) { }
554    
555     void operator ()() const
556     {
557     cur->push_back (t);
558     }
559     };
560    
561     template<int length>
562     struct sl_string
563     {
564     char str[length];
565    
566     void operator ()() const
567     {
568     cur->push_back (*new fragment_string_i (str));
569     }
570     };
571    
572     // only floats
573     struct sl_float
574     {
575     const GLfloat c;
576    
577     sl_float (GLfloat c) : c(c) { }
578    
579     void operator ()() const
580     {
581     char s[64];
582     sprintf (s, "%g", c);
583     cur->append_string (s);
584     }
585     };
586    
587     template<class A, class B>
588     inline sl_expr< sl_concat2<A, B> >
589     concat (const A &a, const B &b)
590     {
591     typedef sl_concat2<A, B> expr;
592     return sl_expr<expr> (expr (a, b));
593     }
594    
595     template<class A, class B, class C>
596     inline sl_expr< sl_concat3<A, B, C> >
597     concat (const A &a, const B &b, const C &c)
598     {
599     typedef sl_concat3<A, B, C> expr;
600     return sl_expr<expr> (expr (a, b, c));
601     }
602    
603     template<class A, class B, class C, class D>
604     inline sl_expr< sl_concat4<A, B, C, D> >
605     concat (const A &a, const B &b, const C &c, const D &d)
606     {
607     typedef sl_concat4<A, B, C, D> expr;
608     return sl_expr<expr> (expr (a, b, c, d));
609     }
610    
611     template<typename expr>
612     struct sl_convert
613     {
614     typedef sl_expr<expr> T;
615     static inline const T &convert (const T &e)
616     {
617     return e;
618     }
619     };
620    
621     template<>
622     struct sl_convert<GLfloat>
623     {
624     typedef sl_expr<sl_float> T;
625     static inline const T convert (GLfloat f)
626     {
627     return sl_float (f);
628     }
629     };
630    
631     template<>
632     struct sl_convert<vec3>
633     {
634     typedef sl_expr< sl_string<256> > T;
635     static inline const T convert (const vec3 &v)
636     {
637     sl_string<256> s;
638     sprintf (s.str, "vec3 (%g, %g, %g)", v.x, v.y, v.z);
639     return s;
640     }
641     };
642    
643     template<>
644     struct sl_convert<vec4>
645     {
646     typedef sl_expr< sl_string<256> > T;
647     static inline const T convert (const vec4 &v)
648     {
649     sl_string<256> s;
650     sprintf (s.str, "vec4 (%g, %g, %g, %g)", v.x, v.y, v.z, v.w);
651     return s;
652     }
653     };
654    
655     template<>
656     template<class V>
657     struct sl_convert< var_ref<V> >
658     {
659     typedef sl_expr< sl_append< var_ref<V> > > T;
660     static inline const T convert (const var_ref<V> &v)
661     {
662     return sl_append< var_ref<V> > (v);
663     }
664     };
665    
666     template<>
667     struct sl_convert<glvar>
668     {
669     typedef sl_expr< sl_append<glvar> > T;
670     static inline const T convert (const glvar &v)
671     {
672     return sl_append<glvar> (v);
673     }
674     };
675    
676     template<>
677     template<const char *strtype>
678     struct sl_convert< temp_ref<strtype> >
679     {
680     typedef sl_expr< sl_append< temp_ref<strtype> > > T;
681     static inline const T convert (const temp_ref<strtype> &v)
682     {
683     return sl_expr< sl_append< temp_ref<strtype> > >(
684     sl_append< temp_ref<strtype> > (v)
685     );
686     }
687     };
688    
689     template<class fragment, typename expr>
690     inline void sl_assign (const fragment &f, const expr &e)
691     {
692     cur->append_const (" ");
693     cur->append (f);
694     cur->append_const (" = ");
695     sl_convert<expr>::convert (e) ();
696     cur->append_const (";\n");
697     }
698    
699     template<class type>
700     template<typename expr>
701     inline const auto_lvalue_ref0<type> &auto_lvalue_ref0<type>::operator =(const expr &e) const
702     {
703     sl_assign (*this, e);
704     return *this;
705     }
706    
707     template<class type, typename arg1>
708     template<typename expr>
709     inline const auto_lvalue_ref1<type,arg1> &auto_lvalue_ref1<type,arg1>::operator =(const expr &e) const
710     {
711     sl_assign (*this, e);
712     return *this;
713     }
714    
715     template<const char *strtype>
716     template<typename expr>
717     inline const temp_ref<strtype> &temp_ref<strtype>::operator =(const expr &e) const
718     {
719     sl_assign (*this, e);
720     return *this;
721     }
722    
723     struct sl_append_const_string
724     {
725     fragment_const_string str;
726     sl_append_const_string (const char *s)
727     : str (s)
728     { }
729    
730     void operator ()() const
731     {
732     cur->push_back (str);
733     }
734     };
735    
736     # define SHADER_BINOP(op, str) \
737     extern const sl_append_const_string str_ ## str; \
738     template<typename A, typename B> \
739     inline const sl_expr< sl_concat3< typename sl_convert<A>::T, \
740     sl_append_const_string, \
741     typename sl_convert<B>::T > > \
742     operator op(const A &a, const B &b) \
743     { \
744     return concat (sl_convert<A>::convert (a), str_ ## str, sl_convert<B>::convert (b)); \
745     }
746    
747     SHADER_BINOP (+, plus);
748     SHADER_BINOP (-, minus);
749     SHADER_BINOP (*, mul);
750     SHADER_BINOP (/, div);
751    
752     # undef SHADER_BINOP
753    
754 root 1.1 void debdebdebdebug ();//D
755     }
756    
757     #endif
758