… | |
… | |
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; |