ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/shader.h
(Generate patch)

Comparing libgender/shader.h (file contents):
Revision 1.11 by root, Sun Oct 24 02:13:43 2004 UTC vs.
Revision 1.14 by root, Sun Oct 24 21:16:41 2004 UTC

232 struct lvalue_i : fragment_i 232 struct lvalue_i : fragment_i
233 { 233 {
234 }; 234 };
235 235
236 // a simple predeclared variable with unspecified type 236 // a simple predeclared variable with unspecified type
237 struct glvar_i : lvalue_i 237 struct gluvar_i : lvalue_i
238 { 238 {
239 const char *name; 239 const char *name;
240 void build (shader_builder &b); 240 void build (shader_builder &b);
241 glvar_i (const char *name) : name (name) { } 241 gluvar_i (const char *name) : name (name) { }
242 }; 242 };
243 243
244 typedef auto_lvalue_ref1<glvar_i, const char *> glvar; 244 typedef auto_lvalue_ref1<gluvar_i, const char *> gluvar;
245 245
246 struct var_i : lvalue_i 246 struct var_i : lvalue_i
247 { 247 {
248 static unsigned int next_id; 248 static unsigned int next_id;
249 249
258 }; 258 };
259 259
260 struct uniform_i : var_i 260 struct uniform_i : var_i
261 { 261 {
262 bool dirty; 262 bool dirty;
263 GLint location;
263 264
264 virtual void update () = 0; 265 virtual void update () = 0;
265 266
266 void build (shader_builder &b); 267 void build (shader_builder &b);
267 void build_decl (ostringstream &b); 268 void build_decl (ostringstream &b);
269
270 void update_location (GLint program)
271 {
272 location = glGetUniformLocationARB (program, name);
273 }
274
268 uniform_i (const char *strtype); 275 uniform_i (const char *strtype);
269 }; 276 };
270 277
271 template<int dimension, typename gltype, const char *strtype, void (*upd)(CGparameter, const gltype *)> 278 template<int dimension, typename gltype, const char *strtype, void (*upd)(GLint, GLsizei, const gltype *)>
272 struct uniform2_i : uniform_i 279 struct uniform2_i : uniform_i
273 { 280 {
274 gltype data[dimension]; 281 gltype data[dimension];
275 282
276 void update () 283 void update ()
277 { 284 {
278 if (dirty) 285 if (dirty)
279 { 286 {
280 //upd (param, data); 287 if (location >= 0)
288 upd (location, dimension, data);
289
281 dirty = false; 290 dirty = false;
282 } 291 }
283 } 292 }
284 293
285 uniform2_i () : uniform_i (strtype) { } 294 uniform2_i () : uniform_i (strtype) { }
286 }; 295 };
287 296
288 template<int dimension, const char *strtype, void (*update)(CGparameter, const GLfloat *)> 297 template<int dimension, const char *strtype, void (*update)(GLint, GLsizei, const GLfloat *)>
289 struct uniform_f_i : uniform2_i<dimension, GLfloat, strtype, update> 298 struct uniform_f_i : uniform2_i<dimension, GLfloat, strtype, update>
290 { 299 {
291 }; 300 };
292 301
293 struct uniform_1f_i : uniform_f_i<1, str_float, cgGLSetParameter1fv> { 302 struct uniform_1f_i : uniform_f_i<1, str_float, glUniform1fvARB> {
294 void operator =(GLfloat v) 303 void operator =(GLfloat v)
295 { 304 {
296 data[0] = v; 305 data[0] = v;
297 dirty = true; 306 dirty = true;
298 } 307 }
299 }; 308 };
300 309
301 struct uniform_2f_i : uniform_f_i<2, str_vec2, cgGLSetParameter2fv> { }; 310 struct uniform_2f_i : uniform_f_i<2, str_vec2, glUniform2fvARB> { };
302 311
303 struct uniform_3f_i : uniform_f_i<3, str_vec3, cgGLSetParameter3fv> { 312 struct uniform_3f_i : uniform_f_i<3, str_vec3, glUniform3fvARB> {
304 void operator =(const vec3 &v) 313 void operator =(const vec3 &v)
305 { 314 {
306 data[0] = v.x; 315 data[0] = v.x;
307 data[1] = v.y; 316 data[1] = v.y;
308 data[2] = v.z; 317 data[2] = v.z;
309 dirty = true; 318 dirty = true;
310 } 319 }
311 }; 320 };
312 321
313 struct uniform_4f_i : uniform_f_i<4, str_vec4, cgGLSetParameter4fv> { 322 struct uniform_4f_i : uniform_f_i<4, str_vec4, glUniform4fvARB> {
314#if 0 323#if 0
315 void operator =(const gl::matrix &m) 324 void operator =(const gl::matrix &m)
316 { 325 {
317 memcpy (data, m.data, 16 * sizeof (GLfloat)); 326 memcpy (data, m.data, 16 * sizeof (GLfloat));
318 dirty = true; 327 dirty = true;
319 } 328 }
320#endif 329#endif
321 }; 330 };
322 331
332 inline void UniformMatrix2fv (GLint l, GLsizei s, const GLfloat *m) { glUniformMatrix2fvARB (l, s, 0, m); }
333 inline void UniformMatrix3fv (GLint l, GLsizei s, const GLfloat *m) { glUniformMatrix3fvARB (l, s, 0, m); }
334 inline void UniformMatrix4fv (GLint l, GLsizei s, const GLfloat *m) { glUniformMatrix4fvARB (l, s, 0, m); }
335
323 struct uniform_matrix_2f_i : uniform_f_i< 4, str_mat2, cgGLSetMatrixParameterfc> { }; 336 struct uniform_matrix_2f_i : uniform_f_i< 4, str_mat2, UniformMatrix2fv> { };
324 struct uniform_matrix_3f_i : uniform_f_i< 9, str_mat3, cgGLSetMatrixParameterfc> { }; 337 struct uniform_matrix_3f_i : uniform_f_i< 9, str_mat3, UniformMatrix3fv> { };
325 struct uniform_matrix_4f_i : uniform_f_i<16, str_mat4, cgGLSetMatrixParameterfc> { }; 338 struct uniform_matrix_4f_i : uniform_f_i<16, str_mat4, UniformMatrix4fv> { };
326 339
327 template<class var_i> 340 template<class var_i>
328 struct var_ref : ref<var_i> 341 struct var_ref : ref<var_i>
329 { 342 {
330 var_ref (const char *glname = 0) 343 var_ref (const char *glname = 0)
441 typedef temp_ref<str_mat4> temp_matrix_4f; 454 typedef temp_ref<str_mat4> temp_matrix_4f;
442 455
443 template<GLenum gltype, const char *strtype> 456 template<GLenum gltype, const char *strtype>
444 struct sampler_i : uniform_i 457 struct sampler_i : uniform_i
445 { 458 {
446 GLuint texture; 459 GLuint unit;
447 460
448 void update () 461 void update ()
449 { 462 {
450 if (dirty) 463 if (dirty)
451 { 464 {
454 } 467 }
455 } 468 }
456 469
457 void begin () 470 void begin ()
458 { 471 {
459 cgGLEnableTextureParameter (texture); 472 cgGLEnableTextureParameter (unit);
460 } 473 }
461 474
462 sampler_i (GLuint texturename) : uniform_i (strtype), texture (texturename) { } 475 sampler_i (GLuint textureunit) : uniform_i (strtype), unit (textureunit) { }
463 }; 476 };
464 477
465 struct sampler_1d_i : sampler_i<CG_SAMPLER1D, str_sampler_1d> 478 struct sampler_1d_i : sampler_i<CG_SAMPLER1D, str_sampler_1d>
466 { 479 {
467 sampler_1d_i (GLuint texturename) : sampler_i<CG_SAMPLER1D, str_sampler_1d> (texturename) { } 480 sampler_1d_i (GLuint textureunit) : sampler_i<CG_SAMPLER1D, str_sampler_1d> (textureunit) { }
468 }; 481 };
469 482
470 struct sampler_1d_shadow_i : sampler_i<CG_SAMPLER1D, str_sampler_1d_shadow> 483 struct sampler_1d_shadow_i : sampler_i<CG_SAMPLER1D, str_sampler_1d_shadow>
471 { 484 {
472 sampler_1d_shadow_i (GLuint texturename) : sampler_i<CG_SAMPLER1D, str_sampler_1d_shadow> (texturename) { } 485 sampler_1d_shadow_i (GLuint textureunit) : sampler_i<CG_SAMPLER1D, str_sampler_1d_shadow> (textureunit) { }
473 }; 486 };
474 487
475 struct sampler_2d_i : sampler_i<CG_SAMPLER2D, str_sampler_2d> 488 struct sampler_2d_i : sampler_i<CG_SAMPLER2D, str_sampler_2d>
476 { 489 {
477 sampler_2d_i (GLuint texturename) : sampler_i<CG_SAMPLER2D, str_sampler_2d> (texturename) { } 490 sampler_2d_i (GLuint textureunit) : sampler_i<CG_SAMPLER2D, str_sampler_2d> (textureunit) { }
478 }; 491 };
479 492
480 struct sampler_2d_shadow_i : sampler_i<CG_SAMPLER2D, str_sampler_2d_shadow> 493 struct sampler_2d_shadow_i : sampler_i<CG_SAMPLER2D, str_sampler_2d_shadow>
481 { 494 {
482 sampler_2d_shadow_i (GLuint texturename) : sampler_i<CG_SAMPLER2D, str_sampler_2d_shadow> (texturename) { } 495 sampler_2d_shadow_i (GLuint textureunit) : sampler_i<CG_SAMPLER2D, str_sampler_2d_shadow> (textureunit) { }
483 }; 496 };
484 497
485 struct sampler_2d_rect_i : sampler_i<CG_SAMPLERRECT, str_sampler_2d_rect> 498 struct sampler_2d_rect_i : sampler_i<CG_SAMPLERRECT, str_sampler_2d_rect>
486 { 499 {
487 sampler_2d_rect_i (GLuint texturename) : sampler_i<CG_SAMPLERRECT, str_sampler_2d_rect> (texturename) { } 500 sampler_2d_rect_i (GLuint textureunit) : sampler_i<CG_SAMPLERRECT, str_sampler_2d_rect> (textureunit) { }
488 }; 501 };
489 502
490 struct sampler_2d_rect_shadow_i : sampler_i<CG_SAMPLERRECT, str_sampler_2d_rect_shadow> 503 struct sampler_2d_rect_shadow_i : sampler_i<CG_SAMPLERRECT, str_sampler_2d_rect_shadow>
491 { 504 {
492 sampler_2d_rect_shadow_i (GLuint texturename) : sampler_i<CG_SAMPLERRECT, str_sampler_2d_rect_shadow> (texturename) { } 505 sampler_2d_rect_shadow_i (GLuint textureunit) : sampler_i<CG_SAMPLERRECT, str_sampler_2d_rect_shadow> (textureunit) { }
493 }; 506 };
494 507
495 struct sampler_3d_i : sampler_i<CG_SAMPLER3D, str_sampler_3d> 508 struct sampler_3d_i : sampler_i<CG_SAMPLER3D, str_sampler_3d>
496 { 509 {
497 sampler_3d_i (GLuint texturename) : sampler_i<CG_SAMPLER3D, str_sampler_3d> (texturename) { } 510 sampler_3d_i (GLuint textureunit) : sampler_i<CG_SAMPLER3D, str_sampler_3d> (textureunit) { }
498 }; 511 };
499 512
500 struct sampler_3d_rect_i : sampler_i<CG_SAMPLER3D, str_sampler_3d_rect> 513 struct sampler_3d_rect_i : sampler_i<CG_SAMPLER3D, str_sampler_3d_rect>
501 { 514 {
502 sampler_3d_rect_i (GLuint texturename) : sampler_i<CG_SAMPLER3D, str_sampler_3d_rect> (texturename) { } 515 sampler_3d_rect_i (GLuint textureunit) : sampler_i<CG_SAMPLER3D, str_sampler_3d_rect> (textureunit) { }
503 }; 516 };
504 517
505 struct sampler_cube_i : sampler_i<CG_SAMPLERCUBE, str_sampler_cube> 518 struct sampler_cube_i : sampler_i<CG_SAMPLERCUBE, str_sampler_cube>
506 { 519 {
507 sampler_cube_i (GLuint texturename) : sampler_i<CG_SAMPLERCUBE, str_sampler_cube> (texturename) { } 520 sampler_cube_i (GLuint textureunit) : sampler_i<CG_SAMPLERCUBE, str_sampler_cube> (textureunit) { }
508 }; 521 };
509 522
510 typedef auto_ref1<sampler_1d_i, GLuint> sampler_1d; 523 typedef auto_ref1<sampler_1d_i, GLuint> sampler_1d;
511 typedef auto_ref1<sampler_1d_shadow_i, GLuint> sampler_1d_shadow; 524 typedef auto_ref1<sampler_1d_shadow_i, GLuint> sampler_1d_shadow;
512 typedef auto_ref1<sampler_2d_i, GLuint> sampler_2d; 525 typedef auto_ref1<sampler_2d_i, GLuint> sampler_2d;
736 return sl_append< var_ref<V> > (v); 749 return sl_append< var_ref<V> > (v);
737 } 750 }
738 }; 751 };
739 752
740 template<> 753 template<>
741 struct sl_convert<glvar> 754 struct sl_convert<gluvar>
742 { 755 {
743 typedef sl_expr< sl_append<glvar> > T; 756 typedef sl_expr< sl_append<gluvar> > T;
744 static inline const T convert (const glvar &v) 757 static inline const T convert (const gluvar &v)
745 { 758 {
746 return sl_append<glvar> (v); 759 return sl_append<gluvar> (v);
747 } 760 }
748 }; 761 };
749 762
750 template<> 763 template<>
751 template<const char *strtype> 764 template<const char *strtype>
818 { 831 {
819 cur->push_back (str); 832 cur->push_back (str);
820 } 833 }
821 }; 834 };
822 835
836 extern const sl_append_const_string str_lpar;
837 extern const sl_append_const_string str_rpar;
838
839 template<class A, class B, class C>
840 struct sl_binop
841 {
842 const A a; const B b; const C c;
843 sl_binop (const A &a, const B &b, const C &c) : a(a), b(b), c(c) { }
844 void operator ()() const { str_lpar (); a (); b (); c (); str_rpar (); }
845 };
846
823# define SHADER_BINOP(op, str) \ 847# define SHADER_BINOP(op, str) \
824 extern const sl_append_const_string str_ ## str; \ 848 extern const sl_append_const_string str_ ## str; \
825 template<typename A, typename B> \ 849 template<typename A, typename B> \
826 inline const sl_expr< sl_concat3< typename sl_convert<A>::T, \ 850 inline const sl_expr< sl_binop< typename sl_convert<A>::T, \
827 sl_append_const_string, \ 851 sl_append_const_string, \
828 typename sl_convert<B>::T > > \ 852 typename sl_convert<B>::T > > \
829 operator op(const A &a, const B &b) \ 853 operator op(const A &a, const B &b) \
830 { \ 854 { \
855 return sl_binop< typename sl_convert<A>::T, sl_append_const_string, typename sl_convert<B>::T > \
831 return concat (sl_convert<A>::convert (a), str_ ## str, sl_convert<B>::convert (b)); \ 856 (sl_convert<A>::convert (a), str_ ## str, sl_convert<B>::convert (b)); \
832 } 857 }
833 858
834 SHADER_BINOP (+, plus); 859 SHADER_BINOP (+, plus);
835 SHADER_BINOP (-, minus); 860 SHADER_BINOP (-, minus);
836 SHADER_BINOP (*, mul); 861 SHADER_BINOP (*, mul);
837 SHADER_BINOP (/, div); 862 SHADER_BINOP (/, div);
838 SHADER_BINOP (%, div); 863 SHADER_BINOP (%, mod);
839 864
840# undef SHADER_BINOP 865# undef SHADER_BINOP
841 866
842 void swizzle_mask (sl_string<7> &s, int mask); 867 void swizzle_mask (sl_string<7> &s, int mask);
843
844 extern const sl_append_const_string str_lpar;
845 extern const sl_append_const_string str_rpar;
846 868
847 template<typename T> 869 template<typename T>
848 inline const sl_expr< sl_concat3< sl_append_const_string, 870 inline const sl_expr< sl_concat3< sl_append_const_string,
849 typename sl_convert<T>::T, 871 typename sl_convert<T>::T,
850 sl_string<7> 872 sl_string<7>

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines