1 |
#ifndef SHADER_H |
2 |
#define SHADER_H |
3 |
|
4 |
#include <cassert> |
5 |
|
6 |
#include <string> |
7 |
#include <set> |
8 |
#include <map> |
9 |
#include <sstream> |
10 |
|
11 |
#include "opengl.h" |
12 |
#include "util.h" |
13 |
|
14 |
namespace shader { |
15 |
|
16 |
using namespace std; |
17 |
|
18 |
const int NAMELEN = 32; |
19 |
|
20 |
struct shader_builder |
21 |
{ |
22 |
static struct shader_builder *cur; |
23 |
|
24 |
set<const void *> seen; |
25 |
|
26 |
ostringstream global; |
27 |
ostringstream local; |
28 |
ostringstream code; |
29 |
|
30 |
bool first (const void *p); |
31 |
|
32 |
static void start (); |
33 |
static string stop (); |
34 |
}; |
35 |
|
36 |
template<class T> |
37 |
struct sl_expr { |
38 |
const T t; |
39 |
sl_expr (const T &t) : t(t) { } |
40 |
void operator ()() const { t (); } |
41 |
template<typename expr> |
42 |
const sl_expr &operator =(const expr &e) const; |
43 |
}; |
44 |
|
45 |
struct sl_func0 |
46 |
{ |
47 |
const char *name_par; |
48 |
sl_func0 (const char *name_par) : name_par(name_par) { } |
49 |
|
50 |
void begin () const |
51 |
{ |
52 |
shader_builder::cur->code << name_par; |
53 |
} |
54 |
|
55 |
void comma () const |
56 |
{ |
57 |
shader_builder::cur->code << ", "; |
58 |
} |
59 |
|
60 |
void end () const |
61 |
{ |
62 |
shader_builder::cur->code << ")"; |
63 |
} |
64 |
|
65 |
void operator ()() const |
66 |
{ |
67 |
begin (); |
68 |
end (); |
69 |
} |
70 |
}; |
71 |
|
72 |
template<class A> |
73 |
struct sl_func1 : sl_func0 |
74 |
{ |
75 |
const A a; |
76 |
sl_func1 (const char *name, const A &a) : sl_func0(name), a(a) { } |
77 |
void operator ()() const { begin (); a (); end (); } |
78 |
}; |
79 |
|
80 |
template<class A, class B> |
81 |
struct sl_func2 : sl_func0 |
82 |
{ |
83 |
const A a; const B b; |
84 |
sl_func2 (const char *name, const A &a, const B &b) : sl_func0(name), a(a), b(b) { } |
85 |
void operator ()() const { begin (); a (); comma (); b (); end (); } |
86 |
}; |
87 |
|
88 |
template<class A, class B, class C> |
89 |
struct sl_func3 : sl_func0 |
90 |
{ |
91 |
const A a; const B b; const C c; |
92 |
sl_func3 (const char *name, const A &a, const B &b, const C &c) : sl_func0(name), a(a), b(b), c(c) { } |
93 |
void operator ()() const { begin (); a (); comma (); b (); comma (); c (); end (); } |
94 |
}; |
95 |
|
96 |
template<class A, class B, class C, class D> |
97 |
struct sl_func4 : sl_func0 |
98 |
{ |
99 |
const A a; const B b; const C c; const D d; |
100 |
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) { } |
101 |
void operator ()() const { begin (); a (); comma (); b (); comma (); c (); comma (); d(); end (); } |
102 |
}; |
103 |
|
104 |
class refcounted |
105 |
{ |
106 |
template<class type> friend class ref; |
107 |
mutable unsigned int refcnt; |
108 |
void refcnt_inc () const { refcnt++; } |
109 |
void refcnt_dec () const { if (!--refcnt) refcnt_destroy (); } |
110 |
void refcnt_destroy () const; |
111 |
public: |
112 |
refcounted () : refcnt(0) { } |
113 |
virtual ~refcounted (); |
114 |
}; |
115 |
|
116 |
template<class type> |
117 |
struct ref |
118 |
{ |
119 |
type *p; |
120 |
|
121 |
ref () |
122 |
{ |
123 |
p = new type; |
124 |
p->refcnt_inc (); |
125 |
} |
126 |
|
127 |
ref (type &d) |
128 |
{ |
129 |
d.refcnt_inc (); |
130 |
p = &d; |
131 |
} |
132 |
|
133 |
ref (const ref &r) |
134 |
{ |
135 |
r.p->refcnt_inc (); |
136 |
p = r.p; |
137 |
} |
138 |
|
139 |
template<class type2> |
140 |
ref (const ref<type2> &r) |
141 |
{ |
142 |
r.p->refcnt_inc (); |
143 |
p = r.p; |
144 |
} |
145 |
|
146 |
~ref () |
147 |
{ |
148 |
p->refcnt_dec (); |
149 |
} |
150 |
|
151 |
ref &operator =(type &d) |
152 |
{ |
153 |
d.refcnt_inc (); |
154 |
p->refcnt_dec (); |
155 |
p = &d; |
156 |
return *this; |
157 |
} |
158 |
|
159 |
type *operator ->() const { return p; } |
160 |
|
161 |
operator type &() const { return *p; } |
162 |
|
163 |
template<class type2> |
164 |
bool operator ==(const ref<type2> b) const |
165 |
{ |
166 |
return (void *)p == (void *)b.p; |
167 |
} |
168 |
}; |
169 |
|
170 |
// ref with auto-construct |
171 |
template<class type> |
172 |
struct auto_ref0 : ref<type> |
173 |
{ |
174 |
auto_ref0 () : ref<type> (*new type ()) { } |
175 |
}; |
176 |
|
177 |
template<class type, typename arg1> |
178 |
struct auto_ref1 : ref<type> |
179 |
{ |
180 |
auto_ref1 (arg1 a) : ref<type> (*new type (a)) { } |
181 |
}; |
182 |
|
183 |
template<class type> |
184 |
struct auto_lvalue_ref0 : ref<type> |
185 |
{ |
186 |
auto_lvalue_ref0 () : ref<type> (*new type ()) { } |
187 |
template<typename expr> |
188 |
const auto_lvalue_ref0 &operator =(const expr &e) const; |
189 |
}; |
190 |
|
191 |
template<class type, typename arg1> |
192 |
struct auto_lvalue_ref1 : ref<type> |
193 |
{ |
194 |
auto_lvalue_ref1 (arg1 a) : ref<type> (*new type (a)) { } |
195 |
template<class expr> |
196 |
const auto_lvalue_ref1 &operator =(const expr &e) const; |
197 |
}; |
198 |
|
199 |
extern const char str_float []; |
200 |
extern const char str_vec2 []; |
201 |
extern const char str_vec3 []; |
202 |
extern const char str_vec4 []; |
203 |
extern const char str_mat2 []; |
204 |
extern const char str_mat3 []; |
205 |
extern const char str_mat4 []; |
206 |
|
207 |
extern const char str_sampler_1d []; |
208 |
extern const char str_sampler_1d_shadow []; |
209 |
extern const char str_sampler_2d []; |
210 |
extern const char str_sampler_2d_shadow []; |
211 |
extern const char str_sampler_2d_rect []; |
212 |
extern const char str_sampler_2d_rect_shadow []; |
213 |
extern const char str_sampler_3d []; |
214 |
extern const char str_sampler_3d_rect []; |
215 |
extern const char str_sampler_cube []; |
216 |
|
217 |
typedef ref<struct var_i> var; |
218 |
typedef ref<struct uniform_i> uniform; |
219 |
|
220 |
struct fragment_i : refcounted |
221 |
{ |
222 |
}; |
223 |
|
224 |
typedef ref<fragment_i> fragment; |
225 |
|
226 |
struct lvalue_i : fragment_i |
227 |
{ |
228 |
}; |
229 |
|
230 |
// a simple predeclared variable with unspecified type |
231 |
struct gluvar_i : lvalue_i |
232 |
{ |
233 |
const char *name; |
234 |
|
235 |
void operator ()() const |
236 |
{ |
237 |
shader_builder::cur->code << name; |
238 |
} |
239 |
|
240 |
gluvar_i (const char *name) : name (name) { } |
241 |
}; |
242 |
|
243 |
typedef auto_lvalue_ref1<gluvar_i, const char *> gluvar; |
244 |
|
245 |
struct var_i : lvalue_i |
246 |
{ |
247 |
static unsigned int next_id; |
248 |
|
249 |
char name[NAMELEN]; |
250 |
const char *domainstr; |
251 |
const char *typestr; |
252 |
|
253 |
void operator ()() const; |
254 |
|
255 |
var_i (const char *domainstr, const char *typestr); |
256 |
~var_i (); |
257 |
}; |
258 |
|
259 |
struct uniform_i : var_i |
260 |
{ |
261 |
GLint location (); |
262 |
|
263 |
uniform_i (const char *strtype); |
264 |
}; |
265 |
|
266 |
template<int dimension, typename gltype, const char *strtype> |
267 |
struct uniform2_i : uniform_i |
268 |
{ |
269 |
gltype data[dimension]; |
270 |
|
271 |
uniform2_i () : uniform_i (strtype) { } |
272 |
}; |
273 |
|
274 |
template<int dimension, const char *strtype> |
275 |
struct uniform_f_i : uniform2_i<dimension, GLfloat, strtype> |
276 |
{ |
277 |
}; |
278 |
|
279 |
struct uniform_1f_i : uniform_f_i<1, str_float> { |
280 |
void set (GLfloat v) |
281 |
{ |
282 |
data[0] = v; |
283 |
|
284 |
GLint loc = location (); |
285 |
if (loc >= 0) // workaround for buggy drivers |
286 |
glUniform1fvARB (loc, 1, data); |
287 |
} |
288 |
}; |
289 |
|
290 |
struct uniform_2f_i : uniform_f_i<2, str_vec2> { |
291 |
void set (GLfloat x, GLfloat y) |
292 |
{ |
293 |
data[0] = x; |
294 |
data[1] = y; |
295 |
|
296 |
GLint loc = location (); |
297 |
if (loc >= 0) // workaround for buggy drivers |
298 |
glUniform2fvARB (loc, 1, data); |
299 |
} |
300 |
|
301 |
void set (const vec2 &v) { set (v.x, v.y); } |
302 |
}; |
303 |
|
304 |
struct uniform_3f_i : uniform_f_i<3, str_vec3> { |
305 |
void set (GLfloat x, GLfloat y, GLfloat z) |
306 |
{ |
307 |
data[0] = x; |
308 |
data[1] = y; |
309 |
data[2] = z; |
310 |
|
311 |
GLint loc = location (); |
312 |
if (loc >= 0) // workaround for buggy drivers |
313 |
glUniform3fvARB (loc, 1, data); |
314 |
} |
315 |
|
316 |
void set (const vec3 &v) { set (v.x, v.y, v.z); } |
317 |
}; |
318 |
|
319 |
struct uniform_4f_i : uniform_f_i<4, str_vec4> { |
320 |
void set (GLfloat x, GLfloat y, GLfloat z, GLfloat w) |
321 |
{ |
322 |
data[0] = x; |
323 |
data[1] = y; |
324 |
data[2] = z; |
325 |
data[3] = w; |
326 |
|
327 |
GLint loc = location (); |
328 |
if (loc >= 0) // workaround for buggy drivers |
329 |
glUniform4fvARB (loc, 1, data); |
330 |
} |
331 |
|
332 |
void set (const vec4 &v) { set (v.x, v.y, v.z, v.w); } |
333 |
}; |
334 |
|
335 |
struct uniform_matrix_2f_i : uniform_f_i< 4, str_mat2> |
336 |
{ |
337 |
void set (const GLfloat d[4]) |
338 |
{ |
339 |
memcpy (data, d, 4 * sizeof (GLfloat)); |
340 |
|
341 |
GLint loc = location (); |
342 |
if (loc >= 0) // workaround for buggy drivers |
343 |
glUniformMatrix2fvARB (loc, 1, 0, data); |
344 |
} |
345 |
}; |
346 |
|
347 |
struct uniform_matrix_3f_i : uniform_f_i< 9, str_mat3> |
348 |
{ |
349 |
void set (const GLfloat d[9]) |
350 |
{ |
351 |
memcpy (data, d, 9 * sizeof (GLfloat)); |
352 |
|
353 |
GLint loc = location (); |
354 |
if (loc >= 0) // workaround for buggy drivers |
355 |
glUniformMatrix3fvARB (loc, 1, 0, data); |
356 |
} |
357 |
}; |
358 |
|
359 |
struct uniform_matrix_4f_i : uniform_f_i<16, str_mat4> |
360 |
{ |
361 |
void set (const gl::matrix &m) |
362 |
{ |
363 |
memcpy (data, m.data, 16 * sizeof (GLfloat)); |
364 |
|
365 |
GLint loc = location (); |
366 |
if (loc >= 0) // workaround for buggy drivers |
367 |
glUniformMatrix4fvARB (loc, 1, 0, data); |
368 |
} |
369 |
}; |
370 |
|
371 |
template<class var_i> |
372 |
struct var_ref : ref<var_i> |
373 |
{ |
374 |
var_ref (const char *glname = 0) |
375 |
: ref<var_i> (*new var_i) |
376 |
{ |
377 |
if (glname) |
378 |
strcpy ((*this)->name, glname); |
379 |
} |
380 |
}; |
381 |
|
382 |
typedef var_ref<uniform_1f_i> uniform_1f; |
383 |
typedef var_ref<uniform_2f_i> uniform_2f; |
384 |
typedef var_ref<uniform_3f_i> uniform_3f; |
385 |
typedef var_ref<uniform_4f_i> uniform_4f; |
386 |
typedef var_ref<uniform_matrix_2f_i> uniform_matrix_2f; |
387 |
typedef var_ref<uniform_matrix_3f_i> uniform_matrix_3f; |
388 |
typedef var_ref<uniform_matrix_4f_i> uniform_matrix_4f; |
389 |
|
390 |
#if 0 |
391 |
// to be used for attributes |
392 |
|
393 |
struct stream_i : var_i |
394 |
{ |
395 |
stream_i (const char *strtype); |
396 |
}; |
397 |
|
398 |
template<int dimension, GLenum gltype, const char *strtype> |
399 |
struct varying_i : stream_i |
400 |
{ |
401 |
varying_i () : stream_i (strtype) { } |
402 |
|
403 |
void set (GLsizei stride, GLvoid *ptr) |
404 |
{ |
405 |
//cgGLSetParameterPointer (param, dimension, gltype, stride, ptr); |
406 |
} |
407 |
}; |
408 |
|
409 |
template<int dimension, const char *strtype> |
410 |
struct varying_f_i : varying_i<dimension, GL_FLOAT, strtype> |
411 |
{ |
412 |
void set (const gl::vertex_buffer &vb, GLint offset) |
413 |
{ |
414 |
varying_i<dimension, GL_FLOAT, strtype>::set (gl::format_stride (vb.format), (GLvoid *)(long)offset); |
415 |
} |
416 |
}; |
417 |
|
418 |
struct varying_1f_i : varying_f_i<1, str_float> |
419 |
{ |
420 |
}; |
421 |
|
422 |
struct varying_2f_i : varying_f_i<2, str_vec2> |
423 |
{ |
424 |
void set_t (const gl::vertex_buffer &vb) |
425 |
{ |
426 |
set (vb, gl::format_offset_t (vb.format)); |
427 |
} |
428 |
}; |
429 |
|
430 |
struct varying_3f_i : varying_f_i<3, str_vec3> |
431 |
{ |
432 |
void set_p (const gl::vertex_buffer &vb) |
433 |
{ |
434 |
set (vb, gl::format_offset_p (vb.format)); |
435 |
} |
436 |
|
437 |
void set_n (const gl::vertex_buffer &vb) |
438 |
{ |
439 |
set (vb, gl::format_offset_n (vb.format)); |
440 |
} |
441 |
}; |
442 |
|
443 |
struct varying_4f_i : varying_f_i<4, str_vec4> |
444 |
{ |
445 |
}; |
446 |
|
447 |
typedef var_ref<varying_1f_i> varying_1f; |
448 |
typedef var_ref<varying_2f_i> varying_2f; |
449 |
typedef var_ref<varying_3f_i> varying_3f; |
450 |
typedef var_ref<varying_4f_i> varying_4f; |
451 |
#endif |
452 |
|
453 |
struct varying_i : var_i |
454 |
{ |
455 |
varying_i (const char *strtype); |
456 |
}; |
457 |
|
458 |
struct temporary_i : var_i |
459 |
{ |
460 |
temporary_i (const char *strtype); |
461 |
}; |
462 |
|
463 |
template<const char *strtype, class var_i> |
464 |
struct glnvar_ref : ref<var_i> |
465 |
{ |
466 |
glnvar_ref () |
467 |
: ref<var_i> (*new var_i (strtype)) |
468 |
{ |
469 |
} |
470 |
|
471 |
#if 0 |
472 |
template<typename expr> |
473 |
glnvar_ref (const sl_expr<expr> &e) |
474 |
: ref<var_i> (*new var_i (strtype)) |
475 |
{ |
476 |
(*this) = e; |
477 |
} |
478 |
#endif |
479 |
|
480 |
template<typename expr> |
481 |
const glnvar_ref &operator =(const expr &e) const; |
482 |
}; |
483 |
|
484 |
typedef glnvar_ref<str_float, temporary_i> temp_1f; |
485 |
typedef glnvar_ref<str_vec2, temporary_i> temp_2f; |
486 |
typedef glnvar_ref<str_vec3, temporary_i> temp_3f; |
487 |
typedef glnvar_ref<str_vec4, temporary_i> temp_4f; |
488 |
typedef glnvar_ref<str_mat2, temporary_i> temp_matrix_2f; |
489 |
typedef glnvar_ref<str_mat3, temporary_i> temp_matrix_3f; |
490 |
typedef glnvar_ref<str_mat4, temporary_i> temp_matrix_4f; |
491 |
|
492 |
typedef glnvar_ref<str_float, varying_i> varying_1f; |
493 |
typedef glnvar_ref<str_vec2, varying_i> varying_2f; |
494 |
typedef glnvar_ref<str_vec3, varying_i> varying_3f; |
495 |
typedef glnvar_ref<str_vec4, varying_i> varying_4f; |
496 |
typedef glnvar_ref<str_mat2, varying_i> varying_matrix_2f; |
497 |
typedef glnvar_ref<str_mat3, varying_i> varying_matrix_3f; |
498 |
typedef glnvar_ref<str_mat4, varying_i> varying_matrix_4f; |
499 |
|
500 |
struct texture_units |
501 |
{ |
502 |
static int units[8]; |
503 |
static int unit_count; |
504 |
|
505 |
static GLenum get_texture_unit () |
506 |
{ |
507 |
assert (unit_count); |
508 |
return units[--unit_count]; |
509 |
} |
510 |
|
511 |
static void put_texture_unit (GLenum unit) |
512 |
{ |
513 |
assert (unit_count < 8); |
514 |
units[unit_count++] = unit; |
515 |
} |
516 |
}; |
517 |
|
518 |
template<GLenum gltype, const char *strtype> |
519 |
struct sampler_i : uniform_i, texture_units |
520 |
{ |
521 |
int unit; |
522 |
GLuint name; |
523 |
|
524 |
void enable () |
525 |
{ |
526 |
#if DEBUG |
527 |
assert (unit < 0); |
528 |
#endif |
529 |
GLint loc = location (); |
530 |
|
531 |
if (loc >= 0) |
532 |
{ |
533 |
unit = get_texture_unit (); |
534 |
glActiveTexture (GL_TEXTURE0 + unit); |
535 |
glBindTexture (gltype, name); |
536 |
glUniform1iARB (loc, unit); |
537 |
} |
538 |
} |
539 |
|
540 |
void disable () |
541 |
{ |
542 |
if (unit >= 0) |
543 |
{ |
544 |
put_texture_unit (unit); |
545 |
unit = -1; |
546 |
} |
547 |
} |
548 |
|
549 |
template<class bufferclass, class vectype> |
550 |
void set (const gl::vertex_buffer &vb, const vectype bufferclass::*vp) |
551 |
{ |
552 |
glTexCoordPointer ( |
553 |
vector_traits<vectype>::dimension (), |
554 |
gl::type_traits<typename vector_traits<vectype>::basetype>::glenum (), |
555 |
sizeof (bufferclass), |
556 |
(GLvoid)(char bufferclass::*)vp |
557 |
); |
558 |
} |
559 |
|
560 |
sampler_i (GLuint texture) |
561 |
: uniform_i (strtype) |
562 |
, name (texture) |
563 |
#if DEBUG |
564 |
, unit (-1) |
565 |
#endif |
566 |
{ |
567 |
} |
568 |
}; |
569 |
|
570 |
struct sampler_1d_i : sampler_i<GL_TEXTURE_1D, str_sampler_1d> |
571 |
{ |
572 |
sampler_1d_i (GLuint texture) : sampler_i<GL_TEXTURE_1D, str_sampler_1d> (texture) { } |
573 |
}; |
574 |
|
575 |
struct sampler_1d_shadow_i : sampler_i<GL_TEXTURE_1D, str_sampler_1d_shadow> |
576 |
{ |
577 |
sampler_1d_shadow_i (GLuint texture) : sampler_i<GL_TEXTURE_1D, str_sampler_1d_shadow> (texture) { } |
578 |
}; |
579 |
|
580 |
struct sampler_2d_i : sampler_i<GL_TEXTURE_2D, str_sampler_2d> |
581 |
{ |
582 |
sampler_2d_i (GLuint texture) : sampler_i<GL_TEXTURE_2D, str_sampler_2d> (texture) { } |
583 |
}; |
584 |
|
585 |
struct sampler_2d_shadow_i : sampler_i<GL_TEXTURE_2D, str_sampler_2d_shadow> |
586 |
{ |
587 |
sampler_2d_shadow_i (GLuint texture) : sampler_i<GL_TEXTURE_2D, str_sampler_2d_shadow> (texture) { } |
588 |
}; |
589 |
|
590 |
struct sampler_2d_rect_i : sampler_i<GL_TEXTURE_2D, str_sampler_2d_rect> |
591 |
{ |
592 |
sampler_2d_rect_i (GLuint texture) : sampler_i<GL_TEXTURE_2D, str_sampler_2d_rect> (texture) { } |
593 |
}; |
594 |
|
595 |
struct sampler_2d_rect_shadow_i : sampler_i<GL_TEXTURE_2D, str_sampler_2d_rect_shadow> |
596 |
{ |
597 |
sampler_2d_rect_shadow_i (GLuint texture) : sampler_i<GL_TEXTURE_2D, str_sampler_2d_rect_shadow> (texture) { } |
598 |
}; |
599 |
|
600 |
struct sampler_3d_i : sampler_i<GL_TEXTURE_3D, str_sampler_3d> |
601 |
{ |
602 |
sampler_3d_i (GLuint texture) : sampler_i<GL_TEXTURE_3D, str_sampler_3d> (texture) { } |
603 |
}; |
604 |
|
605 |
struct sampler_3d_rect_i : sampler_i<GL_TEXTURE_3D, str_sampler_3d_rect> |
606 |
{ |
607 |
sampler_3d_rect_i (GLuint texture) : sampler_i<GL_TEXTURE_3D, str_sampler_3d_rect> (texture) { } |
608 |
}; |
609 |
|
610 |
struct sampler_cube_i : sampler_i<GL_TEXTURE_CUBE_MAP, str_sampler_cube> |
611 |
{ |
612 |
sampler_cube_i (GLuint texture) : sampler_i<GL_TEXTURE_CUBE_MAP, str_sampler_cube> (texture) { } |
613 |
}; |
614 |
|
615 |
template<class sampler_i> |
616 |
struct sampler_ref : auto_ref1<sampler_i, GLuint> |
617 |
{ |
618 |
sampler_ref (GLuint texture) |
619 |
: auto_ref1<sampler_i, GLuint> (texture) |
620 |
{ |
621 |
} |
622 |
}; |
623 |
|
624 |
typedef sampler_ref<sampler_1d_i> sampler_1d; |
625 |
typedef sampler_ref<sampler_1d_shadow_i> sampler_1d_shadow; |
626 |
typedef sampler_ref<sampler_2d_i> sampler_2d; |
627 |
typedef sampler_ref<sampler_2d_shadow_i> sampler_2d_shadow; |
628 |
typedef sampler_ref<sampler_2d_rect_i> sampler_2d_rect; |
629 |
typedef sampler_ref<sampler_2d_rect_shadow_i> sampler_2d_rect_shadow; |
630 |
typedef sampler_ref<sampler_3d_i> sampler_3d; |
631 |
typedef sampler_ref<sampler_3d_rect_i> sampler_3d_rect; |
632 |
typedef sampler_ref<sampler_cube_i> sampler_cube; |
633 |
|
634 |
struct shader_object_i : refcounted |
635 |
{ |
636 |
GLenum type; |
637 |
GLuint id; // GLhandleARB, but 2.0 will use uint |
638 |
|
639 |
void compile (const string &source); |
640 |
|
641 |
shader_object_i (GLenum type); |
642 |
~shader_object_i (); |
643 |
}; |
644 |
|
645 |
template<GLenum type> |
646 |
struct shader_object : ref<shader_object_i> |
647 |
{ |
648 |
shader_object () |
649 |
: ref<shader_object_i> (*new shader_object_i (type)) |
650 |
{ |
651 |
} |
652 |
}; |
653 |
|
654 |
typedef shader_object<GL_VERTEX_SHADER_ARB> vertex_shader; |
655 |
typedef shader_object<GL_FRAGMENT_SHADER_ARB> fragment_shader; |
656 |
|
657 |
struct program_object_i : refcounted |
658 |
{ |
659 |
static struct program_object_i *cur; // currently bound program |
660 |
|
661 |
GLuint id; |
662 |
|
663 |
vertex_shader vsh; |
664 |
fragment_shader fsh; |
665 |
|
666 |
map<uniform_i *,GLint> uloc; // uniform location |
667 |
|
668 |
program_object_i (); |
669 |
~program_object_i (); |
670 |
|
671 |
void link (); |
672 |
|
673 |
void enable (); |
674 |
void disable (); |
675 |
}; |
676 |
|
677 |
typedef auto_ref0<program_object_i> program_object; |
678 |
|
679 |
// create a new program or return a cached one |
680 |
program_object get_program (const string &vsh, const string &fsh); |
681 |
|
682 |
template<int length> |
683 |
struct sl_string |
684 |
{ |
685 |
char str[length]; |
686 |
|
687 |
void operator ()() const |
688 |
{ |
689 |
shader_builder::cur->code << str; |
690 |
} |
691 |
}; |
692 |
|
693 |
struct sl_float |
694 |
{ |
695 |
const GLfloat c; |
696 |
|
697 |
sl_float (GLfloat c) : c(c) { } |
698 |
|
699 |
void operator ()() const |
700 |
{ |
701 |
shader_builder::cur->code << c; |
702 |
} |
703 |
}; |
704 |
|
705 |
template<typename expr> |
706 |
struct sl_convert |
707 |
{ |
708 |
typedef sl_expr<expr> T; |
709 |
static inline const T &convert (const T &e) |
710 |
{ |
711 |
return e; |
712 |
} |
713 |
}; |
714 |
|
715 |
template<> |
716 |
struct sl_convert<GLfloat> |
717 |
{ |
718 |
typedef sl_expr<sl_float> T; |
719 |
static inline const T convert (GLfloat f) |
720 |
{ |
721 |
return sl_float (f); |
722 |
} |
723 |
}; |
724 |
|
725 |
template<> |
726 |
struct sl_convert<GLdouble> |
727 |
{ |
728 |
typedef sl_expr<sl_float> T; |
729 |
static inline const T convert (GLdouble d) |
730 |
{ |
731 |
return sl_float (d); |
732 |
} |
733 |
}; |
734 |
|
735 |
template<> |
736 |
struct sl_convert<GLint> |
737 |
{ |
738 |
typedef sl_expr<sl_float> T; |
739 |
static inline const T convert (GLint i) |
740 |
{ |
741 |
return sl_float (i); |
742 |
} |
743 |
}; |
744 |
|
745 |
template<> |
746 |
struct sl_convert<vec2> |
747 |
{ |
748 |
static const sl_expr< sl_string<60> > convert (const vec2 &v); |
749 |
}; |
750 |
|
751 |
template<> |
752 |
struct sl_convert<vec3> |
753 |
{ |
754 |
static const sl_expr< sl_string<80> > convert (const vec3 &v); |
755 |
}; |
756 |
|
757 |
template<> |
758 |
struct sl_convert<vec4> |
759 |
{ |
760 |
static const sl_expr< sl_string<100> > convert (const vec4 &v); |
761 |
}; |
762 |
|
763 |
template<class ref> |
764 |
struct sl_ref |
765 |
{ |
766 |
ref r; |
767 |
sl_ref (const ref &r) : r(r) { } |
768 |
void operator ()() const { r->operator ()(); } |
769 |
}; |
770 |
|
771 |
template<> |
772 |
template<class var_i> |
773 |
struct sl_convert< var_ref<var_i> > |
774 |
{ |
775 |
typedef sl_expr< sl_ref< var_ref<var_i> > > T; |
776 |
static inline const T convert (const var_ref<var_i> &v) |
777 |
{ |
778 |
return T (v); |
779 |
} |
780 |
}; |
781 |
|
782 |
template<> |
783 |
template<class gluvar_i> |
784 |
struct sl_convert< auto_lvalue_ref1<gluvar_i, const char *> > |
785 |
{ |
786 |
typedef sl_expr< sl_ref< auto_lvalue_ref1<gluvar_i, const char *> > > T; |
787 |
static inline const T convert (const auto_lvalue_ref1<gluvar_i, const char *> &v) |
788 |
{ |
789 |
return T (v); |
790 |
} |
791 |
}; |
792 |
|
793 |
template<> |
794 |
template<const char *strtype, class var_i> |
795 |
struct sl_convert< glnvar_ref<strtype, var_i> > |
796 |
{ |
797 |
typedef sl_expr< sl_ref< glnvar_ref<strtype, var_i> > > T; |
798 |
static inline const T convert (const glnvar_ref<strtype, var_i> &v) |
799 |
{ |
800 |
return T (v); |
801 |
} |
802 |
}; |
803 |
|
804 |
template<> |
805 |
template<class sampler_i> |
806 |
struct sl_convert< sampler_ref<sampler_i> > |
807 |
{ |
808 |
typedef sl_expr< sl_ref< sampler_ref<sampler_i> > > T; |
809 |
static inline const T convert (const sampler_ref<sampler_i> &v) |
810 |
{ |
811 |
return T (v); |
812 |
} |
813 |
}; |
814 |
|
815 |
template<class lvalue, typename expr> |
816 |
inline void sl_assign (const lvalue &l, const expr &e) |
817 |
{ |
818 |
shader_builder::cur->code << " "; |
819 |
sl_convert<lvalue>::convert (l) (); |
820 |
shader_builder::cur->code << " = "; |
821 |
sl_convert<expr>::convert (e) (); |
822 |
shader_builder::cur->code << ";\n"; |
823 |
} |
824 |
|
825 |
template<class type> |
826 |
template<typename expr> |
827 |
inline const auto_lvalue_ref0<type> &auto_lvalue_ref0<type>::operator =(const expr &e) const |
828 |
{ |
829 |
sl_assign (*this, e); |
830 |
return *this; |
831 |
} |
832 |
|
833 |
template<class type, typename arg1> |
834 |
template<typename expr> |
835 |
inline const auto_lvalue_ref1<type,arg1> &auto_lvalue_ref1<type,arg1>::operator =(const expr &e) const |
836 |
{ |
837 |
sl_assign (*this, e); |
838 |
return *this; |
839 |
} |
840 |
|
841 |
template<class T> |
842 |
template<typename expr> |
843 |
inline const sl_expr<T> &sl_expr<T>::operator =(const expr &e) const |
844 |
{ |
845 |
sl_assign (*this, e); |
846 |
return *this; |
847 |
} |
848 |
|
849 |
template<const char *strtype, class var_i> |
850 |
template<typename expr> |
851 |
inline const glnvar_ref<strtype, var_i> &glnvar_ref<strtype, var_i>::operator =(const expr &e) const |
852 |
{ |
853 |
sl_assign (*this, e); |
854 |
return *this; |
855 |
} |
856 |
|
857 |
namespace compile { |
858 |
using namespace shader; |
859 |
|
860 |
template<typename T> |
861 |
inline const sl_expr< sl_func1<typename sl_convert<T>::T> > |
862 |
operator +(const T &t) |
863 |
{ |
864 |
return sl_func1<typename sl_convert<T>::T> ("+(", sl_convert<T>::convert (t)); |
865 |
} |
866 |
|
867 |
template<typename T> |
868 |
inline const sl_expr< sl_func1<typename sl_convert<T>::T> > |
869 |
operator -(const T &t) |
870 |
{ |
871 |
return sl_func1<typename sl_convert<T>::T> ("-(", sl_convert<T>::convert (t)); |
872 |
} |
873 |
|
874 |
template<class A, class C> |
875 |
struct sl_binop |
876 |
{ |
877 |
const A a; const char *b; const C c; |
878 |
sl_binop (const A &a, const char *b, const C &c) : a(a), b(b), c(c) { } |
879 |
void operator ()() const |
880 |
{ |
881 |
shader_builder::cur->code << "("; |
882 |
a (); |
883 |
shader_builder::cur->code << b; |
884 |
c (); |
885 |
shader_builder::cur->code << ")"; |
886 |
} |
887 |
}; |
888 |
|
889 |
template<class A, class B, class C> |
890 |
struct sl_ternary |
891 |
{ |
892 |
const A a; const B b; const C c; |
893 |
sl_ternary (const A &a, const B &b, const C &c) : a(a), b(b), c(c) { } |
894 |
void operator ()() const |
895 |
{ |
896 |
shader_builder::cur->code << "("; |
897 |
a (); |
898 |
shader_builder::cur->code << ") ? ("; |
899 |
b (); |
900 |
shader_builder::cur->code << ") : ("; |
901 |
c (); |
902 |
shader_builder::cur->code << ")"; |
903 |
} |
904 |
}; |
905 |
|
906 |
# define SHADER_BINOP(op) \ |
907 |
template<typename A, typename B> \ |
908 |
inline const sl_expr< sl_binop< typename sl_convert<A>::T, \ |
909 |
typename sl_convert<B>::T > > \ |
910 |
operator op(const A &a, const B &b) \ |
911 |
{ \ |
912 |
return sl_binop< typename sl_convert<A>::T, typename sl_convert<B>::T > \ |
913 |
(sl_convert<A>::convert (a), " " # op " ", sl_convert<B>::convert (b)); \ |
914 |
} |
915 |
|
916 |
SHADER_BINOP (+); |
917 |
SHADER_BINOP (-); |
918 |
SHADER_BINOP (*); |
919 |
SHADER_BINOP (/); |
920 |
SHADER_BINOP (%); |
921 |
|
922 |
SHADER_BINOP (<); |
923 |
SHADER_BINOP (<=); |
924 |
SHADER_BINOP (==); |
925 |
SHADER_BINOP (!=); |
926 |
SHADER_BINOP (>=); |
927 |
SHADER_BINOP (>); |
928 |
|
929 |
# undef SHADER_BINOP |
930 |
|
931 |
template<class A> |
932 |
struct sl_swizzle |
933 |
{ |
934 |
const A a; |
935 |
const char *swizzle; |
936 |
sl_swizzle (const A &a, const char *swizzle) : a(a), swizzle(swizzle) { } |
937 |
void operator ()() const |
938 |
{ |
939 |
shader_builder::cur->code << "("; |
940 |
a (); |
941 |
shader_builder::cur->code << ")." << swizzle; |
942 |
} |
943 |
}; |
944 |
|
945 |
# define SHADER_SWIZZLE_OP(type) \ |
946 |
template<typename T> \ |
947 |
inline const sl_expr< sl_swizzle<typename sl_convert<T>::T> > \ |
948 |
type (const T &t) \ |
949 |
{ \ |
950 |
return sl_swizzle<typename sl_convert<T>::T> (sl_convert<T>::convert (t), #type); \ |
951 |
} |
952 |
|
953 |
// brute force is lovely, ain't it? |
954 |
SHADER_SWIZZLE_OP (x ) SHADER_SWIZZLE_OP (xx ) SHADER_SWIZZLE_OP (xxx ) SHADER_SWIZZLE_OP (xxxx) |
955 |
SHADER_SWIZZLE_OP (xxxy) SHADER_SWIZZLE_OP (xxxz) SHADER_SWIZZLE_OP (xxxw) SHADER_SWIZZLE_OP (xxy ) |
956 |
SHADER_SWIZZLE_OP (xxyx) SHADER_SWIZZLE_OP (xxyy) SHADER_SWIZZLE_OP (xxyz) SHADER_SWIZZLE_OP (xxyw) |
957 |
SHADER_SWIZZLE_OP (xxz ) SHADER_SWIZZLE_OP (xxzx) SHADER_SWIZZLE_OP (xxzy) SHADER_SWIZZLE_OP (xxzz) |
958 |
SHADER_SWIZZLE_OP (xxzw) SHADER_SWIZZLE_OP (xxw ) SHADER_SWIZZLE_OP (xxwx) SHADER_SWIZZLE_OP (xxwy) |
959 |
SHADER_SWIZZLE_OP (xxwz) SHADER_SWIZZLE_OP (xxww) SHADER_SWIZZLE_OP (xy ) SHADER_SWIZZLE_OP (xyx ) |
960 |
SHADER_SWIZZLE_OP (xyxx) SHADER_SWIZZLE_OP (xyxy) SHADER_SWIZZLE_OP (xyxz) SHADER_SWIZZLE_OP (xyxw) |
961 |
SHADER_SWIZZLE_OP (xyy ) SHADER_SWIZZLE_OP (xyyx) SHADER_SWIZZLE_OP (xyyy) SHADER_SWIZZLE_OP (xyyz) |
962 |
SHADER_SWIZZLE_OP (xyyw) SHADER_SWIZZLE_OP (xyz ) SHADER_SWIZZLE_OP (xyzx) SHADER_SWIZZLE_OP (xyzy) |
963 |
SHADER_SWIZZLE_OP (xyzz) SHADER_SWIZZLE_OP (xyzw) SHADER_SWIZZLE_OP (xyw ) SHADER_SWIZZLE_OP (xywx) |
964 |
SHADER_SWIZZLE_OP (xywy) SHADER_SWIZZLE_OP (xywz) SHADER_SWIZZLE_OP (xyww) SHADER_SWIZZLE_OP (xz ) |
965 |
SHADER_SWIZZLE_OP (xzx ) SHADER_SWIZZLE_OP (xzxx) SHADER_SWIZZLE_OP (xzxy) SHADER_SWIZZLE_OP (xzxz) |
966 |
SHADER_SWIZZLE_OP (xzxw) SHADER_SWIZZLE_OP (xzy ) SHADER_SWIZZLE_OP (xzyx) SHADER_SWIZZLE_OP (xzyy) |
967 |
SHADER_SWIZZLE_OP (xzyz) SHADER_SWIZZLE_OP (xzyw) SHADER_SWIZZLE_OP (xzz ) SHADER_SWIZZLE_OP (xzzx) |
968 |
SHADER_SWIZZLE_OP (xzzy) SHADER_SWIZZLE_OP (xzzz) SHADER_SWIZZLE_OP (xzzw) SHADER_SWIZZLE_OP (xzw ) |
969 |
SHADER_SWIZZLE_OP (xzwx) SHADER_SWIZZLE_OP (xzwy) SHADER_SWIZZLE_OP (xzwz) SHADER_SWIZZLE_OP (xzww) |
970 |
SHADER_SWIZZLE_OP (xw ) SHADER_SWIZZLE_OP (xwx ) SHADER_SWIZZLE_OP (xwxx) SHADER_SWIZZLE_OP (xwxy) |
971 |
SHADER_SWIZZLE_OP (xwxz) SHADER_SWIZZLE_OP (xwxw) SHADER_SWIZZLE_OP (xwy ) SHADER_SWIZZLE_OP (xwyx) |
972 |
SHADER_SWIZZLE_OP (xwyy) SHADER_SWIZZLE_OP (xwyz) SHADER_SWIZZLE_OP (xwyw) SHADER_SWIZZLE_OP (xwz ) |
973 |
SHADER_SWIZZLE_OP (xwzx) SHADER_SWIZZLE_OP (xwzy) SHADER_SWIZZLE_OP (xwzz) SHADER_SWIZZLE_OP (xwzw) |
974 |
SHADER_SWIZZLE_OP (xww ) SHADER_SWIZZLE_OP (xwwx) SHADER_SWIZZLE_OP (xwwy) SHADER_SWIZZLE_OP (xwwz) |
975 |
SHADER_SWIZZLE_OP (xwww) SHADER_SWIZZLE_OP (y ) SHADER_SWIZZLE_OP (yx ) SHADER_SWIZZLE_OP (yxx ) |
976 |
SHADER_SWIZZLE_OP (yxxx) SHADER_SWIZZLE_OP (yxxy) SHADER_SWIZZLE_OP (yxxz) SHADER_SWIZZLE_OP (yxxw) |
977 |
SHADER_SWIZZLE_OP (yxy ) SHADER_SWIZZLE_OP (yxyx) SHADER_SWIZZLE_OP (yxyy) SHADER_SWIZZLE_OP (yxyz) |
978 |
SHADER_SWIZZLE_OP (yxyw) SHADER_SWIZZLE_OP (yxz ) SHADER_SWIZZLE_OP (yxzx) SHADER_SWIZZLE_OP (yxzy) |
979 |
SHADER_SWIZZLE_OP (yxzz) SHADER_SWIZZLE_OP (yxzw) SHADER_SWIZZLE_OP (yxw ) SHADER_SWIZZLE_OP (yxwx) |
980 |
SHADER_SWIZZLE_OP (yxwy) SHADER_SWIZZLE_OP (yxwz) SHADER_SWIZZLE_OP (yxww) SHADER_SWIZZLE_OP (yy ) |
981 |
SHADER_SWIZZLE_OP (yyx ) SHADER_SWIZZLE_OP (yyxx) SHADER_SWIZZLE_OP (yyxy) SHADER_SWIZZLE_OP (yyxz) |
982 |
SHADER_SWIZZLE_OP (yyxw) SHADER_SWIZZLE_OP (yyy ) SHADER_SWIZZLE_OP (yyyx) SHADER_SWIZZLE_OP (yyyy) |
983 |
SHADER_SWIZZLE_OP (yyyz) SHADER_SWIZZLE_OP (yyyw) SHADER_SWIZZLE_OP (yyz ) SHADER_SWIZZLE_OP (yyzx) |
984 |
SHADER_SWIZZLE_OP (yyzy) SHADER_SWIZZLE_OP (yyzz) SHADER_SWIZZLE_OP (yyzw) SHADER_SWIZZLE_OP (yyw ) |
985 |
SHADER_SWIZZLE_OP (yywx) SHADER_SWIZZLE_OP (yywy) SHADER_SWIZZLE_OP (yywz) SHADER_SWIZZLE_OP (yyww) |
986 |
SHADER_SWIZZLE_OP (yz ) SHADER_SWIZZLE_OP (yzx ) SHADER_SWIZZLE_OP (yzxx) SHADER_SWIZZLE_OP (yzxy) |
987 |
SHADER_SWIZZLE_OP (yzxz) SHADER_SWIZZLE_OP (yzxw) SHADER_SWIZZLE_OP (yzy ) SHADER_SWIZZLE_OP (yzyx) |
988 |
SHADER_SWIZZLE_OP (yzyy) SHADER_SWIZZLE_OP (yzyz) SHADER_SWIZZLE_OP (yzyw) SHADER_SWIZZLE_OP (yzz ) |
989 |
SHADER_SWIZZLE_OP (yzzx) SHADER_SWIZZLE_OP (yzzy) SHADER_SWIZZLE_OP (yzzz) SHADER_SWIZZLE_OP (yzzw) |
990 |
SHADER_SWIZZLE_OP (yzw ) SHADER_SWIZZLE_OP (yzwx) SHADER_SWIZZLE_OP (yzwy) SHADER_SWIZZLE_OP (yzwz) |
991 |
SHADER_SWIZZLE_OP (yzww) SHADER_SWIZZLE_OP (yw ) SHADER_SWIZZLE_OP (ywx ) SHADER_SWIZZLE_OP (ywxx) |
992 |
SHADER_SWIZZLE_OP (ywxy) SHADER_SWIZZLE_OP (ywxz) SHADER_SWIZZLE_OP (ywxw) SHADER_SWIZZLE_OP (ywy ) |
993 |
SHADER_SWIZZLE_OP (ywyx) SHADER_SWIZZLE_OP (ywyy) SHADER_SWIZZLE_OP (ywyz) SHADER_SWIZZLE_OP (ywyw) |
994 |
SHADER_SWIZZLE_OP (ywz ) SHADER_SWIZZLE_OP (ywzx) SHADER_SWIZZLE_OP (ywzy) SHADER_SWIZZLE_OP (ywzz) |
995 |
SHADER_SWIZZLE_OP (ywzw) SHADER_SWIZZLE_OP (yww ) SHADER_SWIZZLE_OP (ywwx) SHADER_SWIZZLE_OP (ywwy) |
996 |
SHADER_SWIZZLE_OP (ywwz) SHADER_SWIZZLE_OP (ywww) SHADER_SWIZZLE_OP (z ) SHADER_SWIZZLE_OP (zx ) |
997 |
SHADER_SWIZZLE_OP (zxx ) SHADER_SWIZZLE_OP (zxxx) SHADER_SWIZZLE_OP (zxxy) SHADER_SWIZZLE_OP (zxxz) |
998 |
SHADER_SWIZZLE_OP (zxxw) SHADER_SWIZZLE_OP (zxy ) SHADER_SWIZZLE_OP (zxyx) SHADER_SWIZZLE_OP (zxyy) |
999 |
SHADER_SWIZZLE_OP (zxyz) SHADER_SWIZZLE_OP (zxyw) SHADER_SWIZZLE_OP (zxz ) SHADER_SWIZZLE_OP (zxzx) |
1000 |
SHADER_SWIZZLE_OP (zxzy) SHADER_SWIZZLE_OP (zxzz) SHADER_SWIZZLE_OP (zxzw) SHADER_SWIZZLE_OP (zxw ) |
1001 |
SHADER_SWIZZLE_OP (zxwx) SHADER_SWIZZLE_OP (zxwy) SHADER_SWIZZLE_OP (zxwz) SHADER_SWIZZLE_OP (zxww) |
1002 |
SHADER_SWIZZLE_OP (zy ) SHADER_SWIZZLE_OP (zyx ) SHADER_SWIZZLE_OP (zyxx) SHADER_SWIZZLE_OP (zyxy) |
1003 |
SHADER_SWIZZLE_OP (zyxz) SHADER_SWIZZLE_OP (zyxw) SHADER_SWIZZLE_OP (zyy ) SHADER_SWIZZLE_OP (zyyx) |
1004 |
SHADER_SWIZZLE_OP (zyyy) SHADER_SWIZZLE_OP (zyyz) SHADER_SWIZZLE_OP (zyyw) SHADER_SWIZZLE_OP (zyz ) |
1005 |
SHADER_SWIZZLE_OP (zyzx) SHADER_SWIZZLE_OP (zyzy) SHADER_SWIZZLE_OP (zyzz) SHADER_SWIZZLE_OP (zyzw) |
1006 |
SHADER_SWIZZLE_OP (zyw ) SHADER_SWIZZLE_OP (zywx) SHADER_SWIZZLE_OP (zywy) SHADER_SWIZZLE_OP (zywz) |
1007 |
SHADER_SWIZZLE_OP (zyww) SHADER_SWIZZLE_OP (zz ) SHADER_SWIZZLE_OP (zzx ) SHADER_SWIZZLE_OP (zzxx) |
1008 |
SHADER_SWIZZLE_OP (zzxy) SHADER_SWIZZLE_OP (zzxz) SHADER_SWIZZLE_OP (zzxw) SHADER_SWIZZLE_OP (zzy ) |
1009 |
SHADER_SWIZZLE_OP (zzyx) SHADER_SWIZZLE_OP (zzyy) SHADER_SWIZZLE_OP (zzyz) SHADER_SWIZZLE_OP (zzyw) |
1010 |
SHADER_SWIZZLE_OP (zzz ) SHADER_SWIZZLE_OP (zzzx) SHADER_SWIZZLE_OP (zzzy) SHADER_SWIZZLE_OP (zzzz) |
1011 |
SHADER_SWIZZLE_OP (zzzw) SHADER_SWIZZLE_OP (zzw ) SHADER_SWIZZLE_OP (zzwx) SHADER_SWIZZLE_OP (zzwy) |
1012 |
SHADER_SWIZZLE_OP (zzwz) SHADER_SWIZZLE_OP (zzww) SHADER_SWIZZLE_OP (zw ) SHADER_SWIZZLE_OP (zwx ) |
1013 |
SHADER_SWIZZLE_OP (zwxx) SHADER_SWIZZLE_OP (zwxy) SHADER_SWIZZLE_OP (zwxz) SHADER_SWIZZLE_OP (zwxw) |
1014 |
SHADER_SWIZZLE_OP (zwy ) SHADER_SWIZZLE_OP (zwyx) SHADER_SWIZZLE_OP (zwyy) SHADER_SWIZZLE_OP (zwyz) |
1015 |
SHADER_SWIZZLE_OP (zwyw) SHADER_SWIZZLE_OP (zwz ) SHADER_SWIZZLE_OP (zwzx) SHADER_SWIZZLE_OP (zwzy) |
1016 |
SHADER_SWIZZLE_OP (zwzz) SHADER_SWIZZLE_OP (zwzw) SHADER_SWIZZLE_OP (zww ) SHADER_SWIZZLE_OP (zwwx) |
1017 |
SHADER_SWIZZLE_OP (zwwy) SHADER_SWIZZLE_OP (zwwz) SHADER_SWIZZLE_OP (zwww) SHADER_SWIZZLE_OP (w ) |
1018 |
SHADER_SWIZZLE_OP (wx ) SHADER_SWIZZLE_OP (wxx ) SHADER_SWIZZLE_OP (wxxx) SHADER_SWIZZLE_OP (wxxy) |
1019 |
SHADER_SWIZZLE_OP (wxxz) SHADER_SWIZZLE_OP (wxxw) SHADER_SWIZZLE_OP (wxy ) SHADER_SWIZZLE_OP (wxyx) |
1020 |
SHADER_SWIZZLE_OP (wxyy) SHADER_SWIZZLE_OP (wxyz) SHADER_SWIZZLE_OP (wxyw) SHADER_SWIZZLE_OP (wxz ) |
1021 |
SHADER_SWIZZLE_OP (wxzx) SHADER_SWIZZLE_OP (wxzy) SHADER_SWIZZLE_OP (wxzz) SHADER_SWIZZLE_OP (wxzw) |
1022 |
SHADER_SWIZZLE_OP (wxw ) SHADER_SWIZZLE_OP (wxwx) SHADER_SWIZZLE_OP (wxwy) SHADER_SWIZZLE_OP (wxwz) |
1023 |
SHADER_SWIZZLE_OP (wxww) SHADER_SWIZZLE_OP (wy ) SHADER_SWIZZLE_OP (wyx ) SHADER_SWIZZLE_OP (wyxx) |
1024 |
SHADER_SWIZZLE_OP (wyxy) SHADER_SWIZZLE_OP (wyxz) SHADER_SWIZZLE_OP (wyxw) SHADER_SWIZZLE_OP (wyy ) |
1025 |
SHADER_SWIZZLE_OP (wyyx) SHADER_SWIZZLE_OP (wyyy) SHADER_SWIZZLE_OP (wyyz) SHADER_SWIZZLE_OP (wyyw) |
1026 |
SHADER_SWIZZLE_OP (wyz ) SHADER_SWIZZLE_OP (wyzx) SHADER_SWIZZLE_OP (wyzy) SHADER_SWIZZLE_OP (wyzz) |
1027 |
SHADER_SWIZZLE_OP (wyzw) SHADER_SWIZZLE_OP (wyw ) SHADER_SWIZZLE_OP (wywx) SHADER_SWIZZLE_OP (wywy) |
1028 |
SHADER_SWIZZLE_OP (wywz) SHADER_SWIZZLE_OP (wyww) SHADER_SWIZZLE_OP (wz ) SHADER_SWIZZLE_OP (wzx ) |
1029 |
SHADER_SWIZZLE_OP (wzxx) SHADER_SWIZZLE_OP (wzxy) SHADER_SWIZZLE_OP (wzxz) SHADER_SWIZZLE_OP (wzxw) |
1030 |
SHADER_SWIZZLE_OP (wzy ) SHADER_SWIZZLE_OP (wzyx) SHADER_SWIZZLE_OP (wzyy) SHADER_SWIZZLE_OP (wzyz) |
1031 |
SHADER_SWIZZLE_OP (wzyw) SHADER_SWIZZLE_OP (wzz ) SHADER_SWIZZLE_OP (wzzx) SHADER_SWIZZLE_OP (wzzy) |
1032 |
SHADER_SWIZZLE_OP (wzzz) SHADER_SWIZZLE_OP (wzzw) SHADER_SWIZZLE_OP (wzw ) SHADER_SWIZZLE_OP (wzwx) |
1033 |
SHADER_SWIZZLE_OP (wzwy) SHADER_SWIZZLE_OP (wzwz) SHADER_SWIZZLE_OP (wzww) SHADER_SWIZZLE_OP (ww ) |
1034 |
SHADER_SWIZZLE_OP (wwx ) SHADER_SWIZZLE_OP (wwxx) SHADER_SWIZZLE_OP (wwxy) SHADER_SWIZZLE_OP (wwxz) |
1035 |
SHADER_SWIZZLE_OP (wwxw) SHADER_SWIZZLE_OP (wwy ) SHADER_SWIZZLE_OP (wwyx) SHADER_SWIZZLE_OP (wwyy) |
1036 |
SHADER_SWIZZLE_OP (wwyz) SHADER_SWIZZLE_OP (wwyw) SHADER_SWIZZLE_OP (wwz ) SHADER_SWIZZLE_OP (wwzx) |
1037 |
SHADER_SWIZZLE_OP (wwzy) SHADER_SWIZZLE_OP (wwzz) SHADER_SWIZZLE_OP (wwzw) SHADER_SWIZZLE_OP (www ) |
1038 |
SHADER_SWIZZLE_OP (wwwx) SHADER_SWIZZLE_OP (wwwy) SHADER_SWIZZLE_OP (wwwz) SHADER_SWIZZLE_OP (wwww) |
1039 |
|
1040 |
# undef SHADER_SWIZZLE_OP |
1041 |
|
1042 |
# define SHADER_FUNC0_(name, glname) \ |
1043 |
template<typename A> \ |
1044 |
inline const sl_expr<sl_func0> \ |
1045 |
name () \ |
1046 |
{ \ |
1047 |
return sl_func0 (#glname " ("); \ |
1048 |
} |
1049 |
|
1050 |
# define SHADER_FUNC0(name) SHADER_FUNC0_(name,name) |
1051 |
|
1052 |
# define SHADER_FUNC1_(name, glname) \ |
1053 |
template<typename A> \ |
1054 |
inline const sl_expr< sl_func1<typename sl_convert<A>::T> > \ |
1055 |
name (const A &a) \ |
1056 |
{ \ |
1057 |
return sl_func1<typename sl_convert<A>::T> (#glname " (", sl_convert<A>::convert (a));\ |
1058 |
} |
1059 |
|
1060 |
# define SHADER_FUNC1(name) SHADER_FUNC1_(name,name) |
1061 |
|
1062 |
# define SHADER_FUNC2_(name, glname) \ |
1063 |
template<typename A, typename B> \ |
1064 |
inline const sl_expr< sl_func2<typename sl_convert<A>::T, typename sl_convert<B>::T> > \ |
1065 |
name (const A &a, const B &b) \ |
1066 |
{ \ |
1067 |
return sl_func2<typename sl_convert<A>::T, typename sl_convert<B>::T> (#glname " (", sl_convert<A>::convert (a), sl_convert<B>::convert (b));\ |
1068 |
} |
1069 |
|
1070 |
# define SHADER_FUNC2(name) SHADER_FUNC2_(name,name) |
1071 |
|
1072 |
# define SHADER_FUNC3_(name, glname) \ |
1073 |
template<typename A, typename B, typename C> \ |
1074 |
inline const sl_expr< sl_func3<typename sl_convert<A>::T, typename sl_convert<B>::T, typename sl_convert<C>::T> > \ |
1075 |
name (const A &a, const B &b, const C &c) \ |
1076 |
{ \ |
1077 |
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));\ |
1078 |
} |
1079 |
|
1080 |
# define SHADER_FUNC3(name) SHADER_FUNC3_(name,name) |
1081 |
|
1082 |
# define SHADER_FUNC4_(name, glname) \ |
1083 |
template<typename A, typename B, typename C, typename D> \ |
1084 |
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 > > \ |
1085 |
name (const A &a, const B &b, const C &c, const D &d) \ |
1086 |
{ \ |
1087 |
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));\ |
1088 |
} |
1089 |
|
1090 |
# define SHADER_FUNC4(name) SHADER_FUNC4_(name,name) |
1091 |
|
1092 |
SHADER_FUNC1 (abs) |
1093 |
SHADER_FUNC1 (acos) |
1094 |
SHADER_FUNC1 (all) |
1095 |
SHADER_FUNC1 (any) |
1096 |
SHADER_FUNC1 (asin) |
1097 |
SHADER_FUNC1 (atan) |
1098 |
SHADER_FUNC2 (atan) |
1099 |
SHADER_FUNC1 (ceil) |
1100 |
SHADER_FUNC3 (clamp) |
1101 |
SHADER_FUNC1 (cos) |
1102 |
SHADER_FUNC2 (cross) |
1103 |
SHADER_FUNC1 (dFdx) |
1104 |
SHADER_FUNC1 (dFdy) |
1105 |
SHADER_FUNC1 (degrees) |
1106 |
SHADER_FUNC2 (distance) |
1107 |
SHADER_FUNC2 (dot) |
1108 |
SHADER_FUNC2_(equal, equal) |
1109 |
SHADER_FUNC1 (exp) |
1110 |
SHADER_FUNC1 (exp2) |
1111 |
SHADER_FUNC3 (faceforward) |
1112 |
SHADER_FUNC1 (floor) |
1113 |
SHADER_FUNC1 (fract) |
1114 |
SHADER_FUNC0 (ftransform) |
1115 |
SHADER_FUNC1 (fwidth) |
1116 |
SHADER_FUNC2_(greater_than_equal, greaterThanEqual) |
1117 |
SHADER_FUNC2_(greater_then, greaterThan) |
1118 |
SHADER_FUNC1 (inversesqrt) |
1119 |
SHADER_FUNC1 (length) |
1120 |
SHADER_FUNC2_(less_than, lessThan) |
1121 |
SHADER_FUNC2_(less_than_equal, lessThanEqual) |
1122 |
SHADER_FUNC1 (log) |
1123 |
SHADER_FUNC1 (log2) |
1124 |
SHADER_FUNC2_(matrix_comp_mult, matrixCompMult) |
1125 |
SHADER_FUNC2 (max) |
1126 |
SHADER_FUNC2 (min) |
1127 |
SHADER_FUNC3 (mix) |
1128 |
SHADER_FUNC2 (mod) |
1129 |
SHADER_FUNC1 (noise1) |
1130 |
SHADER_FUNC1 (noise2) |
1131 |
SHADER_FUNC1 (noise3) |
1132 |
SHADER_FUNC1 (noise4) |
1133 |
SHADER_FUNC1 (normalize) |
1134 |
SHADER_FUNC1 (gl_not) // TODO |
1135 |
SHADER_FUNC2_(notequal, notEqual) |
1136 |
SHADER_FUNC2 (pow) |
1137 |
SHADER_FUNC1 (radians) |
1138 |
SHADER_FUNC2 (reflect) |
1139 |
SHADER_FUNC3 (refract) |
1140 |
SHADER_FUNC2_(shadow_1d, shadow1D) |
1141 |
SHADER_FUNC3_(shadow_1d, shadow1D) |
1142 |
SHADER_FUNC3_(shadow_1d_lod, shadow1DLod) |
1143 |
SHADER_FUNC2_(shadow_1d_proj, shadow1DProj) |
1144 |
SHADER_FUNC3_(shadow_1d_proj, shadow1DProj) |
1145 |
SHADER_FUNC3_(shadow_1d_proj_lod, shadow1DProjLod) |
1146 |
SHADER_FUNC2_(shadow_2d, shadow2D) |
1147 |
SHADER_FUNC3_(shadow_2d, shadow2D) |
1148 |
SHADER_FUNC3_(shadow_2d_lod, shadow2DLod) |
1149 |
SHADER_FUNC2_(shadow_2d_proj, shadow2DProj) |
1150 |
SHADER_FUNC3_(shadow_2d_proj, shadow2DProj) |
1151 |
SHADER_FUNC3_(shadow_2d_proj_lod, shadow2DProjLod) |
1152 |
SHADER_FUNC1 (sign) |
1153 |
SHADER_FUNC1 (sin) |
1154 |
SHADER_FUNC3 (smoothstep) |
1155 |
SHADER_FUNC1 (sqrt) |
1156 |
SHADER_FUNC2 (step) |
1157 |
SHADER_FUNC1 (tan) |
1158 |
SHADER_FUNC2_(texture_1d, texture1D) |
1159 |
SHADER_FUNC3_(texture_1d, texture1D) |
1160 |
SHADER_FUNC3_(texture_1d_lod, texture1DLod) |
1161 |
SHADER_FUNC2_(texture_1d_proj, texture1DProj) |
1162 |
SHADER_FUNC3_(texture_1d_proj, texture1DProj) |
1163 |
SHADER_FUNC3_(texture_1d_proj_lod, texture1DProjLod) |
1164 |
SHADER_FUNC2_(texture_2d, texture2D) |
1165 |
SHADER_FUNC3_(texture_2d, texture2D) |
1166 |
SHADER_FUNC3_(texture_2d_lod, texture2DLod) |
1167 |
SHADER_FUNC2_(texture_2d_proj, texture2DProj) |
1168 |
SHADER_FUNC3_(texture_2d_proj, texture2DProj) |
1169 |
SHADER_FUNC3_(texture_2d_proj_lod, texture2DProjLod) |
1170 |
SHADER_FUNC2_(texture_3d, texture3D) |
1171 |
SHADER_FUNC3_(texture_3d, texture3D) |
1172 |
SHADER_FUNC3_(texture_3d_lod, texture3DLod) |
1173 |
SHADER_FUNC2_(texture_3d_proj, texture3DProj) |
1174 |
SHADER_FUNC3_(texture_3d_proj, texture3DProj) |
1175 |
SHADER_FUNC3_(texture_3d_proj_lod, texture3DProjLod) |
1176 |
SHADER_FUNC2_(texture_cube, textureCube) |
1177 |
SHADER_FUNC3_(texture_cube, textureCube) |
1178 |
SHADER_FUNC3_(texture_cude_lod, textureCubeLod) |
1179 |
SHADER_FUNC1 (vec2) SHADER_FUNC2 (vec2) |
1180 |
SHADER_FUNC1 (vec3) SHADER_FUNC2 (vec3) SHADER_FUNC3 (vec3) |
1181 |
SHADER_FUNC1 (vec4) SHADER_FUNC2 (vec4) SHADER_FUNC3 (vec4) SHADER_FUNC4 (vec4) |
1182 |
// floatx is out extension |
1183 |
SHADER_FUNC1 (float2) SHADER_FUNC2 (float2) |
1184 |
SHADER_FUNC1 (float3) SHADER_FUNC2 (float3) SHADER_FUNC3 (float3) |
1185 |
SHADER_FUNC1 (float4) SHADER_FUNC2 (float4) SHADER_FUNC3 (float4) SHADER_FUNC4 (float4) |
1186 |
SHADER_FUNC1 (mat2) SHADER_FUNC2 (mat2) |
1187 |
SHADER_FUNC1 (mat3) SHADER_FUNC2 (mat3) SHADER_FUNC3 (mat3) |
1188 |
SHADER_FUNC1 (mat4) SHADER_FUNC2 (mat4) SHADER_FUNC3 (mat4) SHADER_FUNC4 (mat4) |
1189 |
|
1190 |
# undef SHADER_FUNC0 |
1191 |
# undef SHADER_FUNC0_ |
1192 |
# undef SHADER_FUNC1 |
1193 |
# undef SHADER_FUNC1_ |
1194 |
# undef SHADER_FUNC2 |
1195 |
# undef SHADER_FUNC2_ |
1196 |
# undef SHADER_FUNC3 |
1197 |
# undef SHADER_FUNC3_ |
1198 |
# undef SHADER_FUNC4 |
1199 |
# undef SHADER_FUNC5_ |
1200 |
} |
1201 |
|
1202 |
#if 0 |
1203 |
template<typename A, typename B, typename C> |
1204 |
inline const sl_expr< sl_ternary<typename sl_convert<A>::T, typename sl_convert<B>::T, typename sl_convert<C>::T> > |
1205 |
ifelse (const A &a, const B &b, const C &c) |
1206 |
{ |
1207 |
return sl_ternary<typename sl_convert<A>::T, typename sl_convert<B>::T, typename sl_convert<C>::T> |
1208 |
(sl_convert<A>::convert (a), sl_convert<B>::convert (b), sl_convert<C>::convert (c)); |
1209 |
} |
1210 |
#endif |
1211 |
|
1212 |
} |
1213 |
|
1214 |
#endif |
1215 |
|
1216 |
#include "shader_vars.h" |
1217 |
|
1218 |
|