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 |
assert (unit < 0); |
527 |
GLint loc = location (); |
528 |
|
529 |
if (loc >= 0) |
530 |
{ |
531 |
unit = get_texture_unit (); |
532 |
glActiveTexture (GL_TEXTURE0 + unit); |
533 |
glBindTexture (gltype, name); |
534 |
glUniform1iARB (loc, unit); |
535 |
} |
536 |
} |
537 |
|
538 |
void disable () |
539 |
{ |
540 |
if (unit >= 0) |
541 |
{ |
542 |
put_texture_unit (unit); |
543 |
unit = -1; |
544 |
} |
545 |
} |
546 |
|
547 |
template<class bufferclass, class vectype> |
548 |
void set (const gl::vertex_buffer &vb, const vectype bufferclass::*vp) |
549 |
{ |
550 |
glTexCoordPointer ( |
551 |
vector_traits<vectype>::dimension (), |
552 |
gl::type_traits<typename vector_traits<vectype>::basetype>::glenum (), |
553 |
sizeof (bufferclass), |
554 |
(GLvoid)(char bufferclass::*)vp |
555 |
); |
556 |
} |
557 |
|
558 |
sampler_i (GLuint texture) |
559 |
: uniform_i (strtype) |
560 |
, name (texture) |
561 |
, unit (-1) |
562 |
{ |
563 |
} |
564 |
}; |
565 |
|
566 |
struct sampler_1d_i : sampler_i<GL_TEXTURE_1D, str_sampler_1d> |
567 |
{ |
568 |
sampler_1d_i (GLuint texture) : sampler_i<GL_TEXTURE_1D, str_sampler_1d> (texture) { } |
569 |
}; |
570 |
|
571 |
struct sampler_1d_shadow_i : sampler_i<GL_TEXTURE_1D, str_sampler_1d_shadow> |
572 |
{ |
573 |
sampler_1d_shadow_i (GLuint texture) : sampler_i<GL_TEXTURE_1D, str_sampler_1d_shadow> (texture) { } |
574 |
}; |
575 |
|
576 |
struct sampler_2d_i : sampler_i<GL_TEXTURE_2D, str_sampler_2d> |
577 |
{ |
578 |
sampler_2d_i (GLuint texture) : sampler_i<GL_TEXTURE_2D, str_sampler_2d> (texture) { } |
579 |
}; |
580 |
|
581 |
struct sampler_2d_shadow_i : sampler_i<GL_TEXTURE_2D, str_sampler_2d_shadow> |
582 |
{ |
583 |
sampler_2d_shadow_i (GLuint texture) : sampler_i<GL_TEXTURE_2D, str_sampler_2d_shadow> (texture) { } |
584 |
}; |
585 |
|
586 |
struct sampler_2d_rect_i : sampler_i<GL_TEXTURE_2D, str_sampler_2d_rect> |
587 |
{ |
588 |
sampler_2d_rect_i (GLuint texture) : sampler_i<GL_TEXTURE_2D, str_sampler_2d_rect> (texture) { } |
589 |
}; |
590 |
|
591 |
struct sampler_2d_rect_shadow_i : sampler_i<GL_TEXTURE_2D, str_sampler_2d_rect_shadow> |
592 |
{ |
593 |
sampler_2d_rect_shadow_i (GLuint texture) : sampler_i<GL_TEXTURE_2D, str_sampler_2d_rect_shadow> (texture) { } |
594 |
}; |
595 |
|
596 |
struct sampler_3d_i : sampler_i<GL_TEXTURE_3D, str_sampler_3d> |
597 |
{ |
598 |
sampler_3d_i (GLuint texture) : sampler_i<GL_TEXTURE_3D, str_sampler_3d> (texture) { } |
599 |
}; |
600 |
|
601 |
struct sampler_3d_rect_i : sampler_i<GL_TEXTURE_3D, str_sampler_3d_rect> |
602 |
{ |
603 |
sampler_3d_rect_i (GLuint texture) : sampler_i<GL_TEXTURE_3D, str_sampler_3d_rect> (texture) { } |
604 |
}; |
605 |
|
606 |
struct sampler_cube_i : sampler_i<GL_TEXTURE_CUBE_MAP, str_sampler_cube> |
607 |
{ |
608 |
sampler_cube_i (GLuint texture) : sampler_i<GL_TEXTURE_CUBE_MAP, str_sampler_cube> (texture) { } |
609 |
}; |
610 |
|
611 |
template<class sampler_i> |
612 |
struct sampler_ref : auto_ref1<sampler_i, GLuint> |
613 |
{ |
614 |
sampler_ref (GLuint texture) |
615 |
: auto_ref1<sampler_i, GLuint> (texture) |
616 |
{ |
617 |
} |
618 |
}; |
619 |
|
620 |
typedef sampler_ref<sampler_1d_i> sampler_1d; |
621 |
typedef sampler_ref<sampler_1d_shadow_i> sampler_1d_shadow; |
622 |
typedef sampler_ref<sampler_2d_i> sampler_2d; |
623 |
typedef sampler_ref<sampler_2d_shadow_i> sampler_2d_shadow; |
624 |
typedef sampler_ref<sampler_2d_rect_i> sampler_2d_rect; |
625 |
typedef sampler_ref<sampler_2d_rect_shadow_i> sampler_2d_rect_shadow; |
626 |
typedef sampler_ref<sampler_3d_i> sampler_3d; |
627 |
typedef sampler_ref<sampler_3d_rect_i> sampler_3d_rect; |
628 |
typedef sampler_ref<sampler_cube_i> sampler_cube; |
629 |
|
630 |
struct shader_object_i : refcounted |
631 |
{ |
632 |
GLenum type; |
633 |
GLuint id; // GLhandleARB, but 2.0 will use uint |
634 |
|
635 |
void compile (const string &source); |
636 |
|
637 |
shader_object_i (GLenum type); |
638 |
~shader_object_i (); |
639 |
}; |
640 |
|
641 |
template<GLenum type> |
642 |
struct shader_object : ref<shader_object_i> |
643 |
{ |
644 |
shader_object () |
645 |
: ref<shader_object_i> (*new shader_object_i (type)) |
646 |
{ |
647 |
} |
648 |
}; |
649 |
|
650 |
typedef shader_object<GL_VERTEX_SHADER_ARB> vertex_shader; |
651 |
typedef shader_object<GL_FRAGMENT_SHADER_ARB> fragment_shader; |
652 |
|
653 |
struct program_object_i : refcounted |
654 |
{ |
655 |
static struct program_object_i *cur; // currently bound program |
656 |
|
657 |
GLuint id; |
658 |
|
659 |
map<uniform_i *,GLint> uloc; // uniform location |
660 |
|
661 |
program_object_i (); |
662 |
~program_object_i (); |
663 |
|
664 |
void attach (const ref<shader_object_i> &shader) |
665 |
{ |
666 |
glAttachObjectARB (id, shader->id); |
667 |
} |
668 |
|
669 |
void link (); |
670 |
|
671 |
void enable (); |
672 |
void disable (); |
673 |
}; |
674 |
|
675 |
typedef auto_ref0<program_object_i> program_object; |
676 |
|
677 |
// create a new program or return a cached one |
678 |
program_object get_program (const string &vsh, const string &fsh); |
679 |
|
680 |
template<int length> |
681 |
struct sl_string |
682 |
{ |
683 |
char str[length]; |
684 |
|
685 |
void operator ()() const |
686 |
{ |
687 |
shader_builder::cur->code << str; |
688 |
} |
689 |
}; |
690 |
|
691 |
struct sl_float |
692 |
{ |
693 |
const GLfloat c; |
694 |
|
695 |
sl_float (GLfloat c) : c(c) { } |
696 |
|
697 |
void operator ()() const |
698 |
{ |
699 |
shader_builder::cur->code << c; |
700 |
} |
701 |
}; |
702 |
|
703 |
template<typename expr> |
704 |
struct sl_convert |
705 |
{ |
706 |
typedef sl_expr<expr> T; |
707 |
static inline const T &convert (const T &e) |
708 |
{ |
709 |
return e; |
710 |
} |
711 |
}; |
712 |
|
713 |
template<> |
714 |
struct sl_convert<GLfloat> |
715 |
{ |
716 |
typedef sl_expr<sl_float> T; |
717 |
static inline const T convert (GLfloat f) |
718 |
{ |
719 |
return sl_float (f); |
720 |
} |
721 |
}; |
722 |
|
723 |
template<> |
724 |
struct sl_convert<GLdouble> |
725 |
{ |
726 |
typedef sl_expr<sl_float> T; |
727 |
static inline const T convert (GLdouble d) |
728 |
{ |
729 |
return sl_float (d); |
730 |
} |
731 |
}; |
732 |
|
733 |
template<> |
734 |
struct sl_convert<GLint> |
735 |
{ |
736 |
typedef sl_expr<sl_float> T; |
737 |
static inline const T convert (GLint i) |
738 |
{ |
739 |
return sl_float (i); |
740 |
} |
741 |
}; |
742 |
|
743 |
template<> |
744 |
struct sl_convert<vec2> |
745 |
{ |
746 |
static const sl_expr< sl_string<60> > convert (const vec2 &v); |
747 |
}; |
748 |
|
749 |
template<> |
750 |
struct sl_convert<vec3> |
751 |
{ |
752 |
static const sl_expr< sl_string<80> > convert (const vec3 &v); |
753 |
}; |
754 |
|
755 |
template<> |
756 |
struct sl_convert<vec4> |
757 |
{ |
758 |
static const sl_expr< sl_string<100> > convert (const vec4 &v); |
759 |
}; |
760 |
|
761 |
template<class ref> |
762 |
struct sl_ref |
763 |
{ |
764 |
ref r; |
765 |
sl_ref (const ref &r) : r(r) { } |
766 |
void operator ()() const { r->operator ()(); } |
767 |
}; |
768 |
|
769 |
template<> |
770 |
template<class var_i> |
771 |
struct sl_convert< var_ref<var_i> > |
772 |
{ |
773 |
typedef sl_expr< sl_ref< var_ref<var_i> > > T; |
774 |
static inline const T convert (const var_ref<var_i> &v) |
775 |
{ |
776 |
return T (v); |
777 |
} |
778 |
}; |
779 |
|
780 |
template<> |
781 |
template<class gluvar_i> |
782 |
struct sl_convert< auto_lvalue_ref1<gluvar_i, const char *> > |
783 |
{ |
784 |
typedef sl_expr< sl_ref< auto_lvalue_ref1<gluvar_i, const char *> > > T; |
785 |
static inline const T convert (const auto_lvalue_ref1<gluvar_i, const char *> &v) |
786 |
{ |
787 |
return T (v); |
788 |
} |
789 |
}; |
790 |
|
791 |
template<> |
792 |
template<const char *strtype, class var_i> |
793 |
struct sl_convert< glnvar_ref<strtype, var_i> > |
794 |
{ |
795 |
typedef sl_expr< sl_ref< glnvar_ref<strtype, var_i> > > T; |
796 |
static inline const T convert (const glnvar_ref<strtype, var_i> &v) |
797 |
{ |
798 |
return T (v); |
799 |
} |
800 |
}; |
801 |
|
802 |
template<> |
803 |
template<class sampler_i> |
804 |
struct sl_convert< sampler_ref<sampler_i> > |
805 |
{ |
806 |
typedef sl_expr< sl_ref< sampler_ref<sampler_i> > > T; |
807 |
static inline const T convert (const sampler_ref<sampler_i> &v) |
808 |
{ |
809 |
return T (v); |
810 |
} |
811 |
}; |
812 |
|
813 |
template<class lvalue, typename expr> |
814 |
inline void sl_assign (const lvalue &l, const expr &e) |
815 |
{ |
816 |
shader_builder::cur->code << " "; |
817 |
sl_convert<lvalue>::convert (l) (); |
818 |
shader_builder::cur->code << " = "; |
819 |
sl_convert<expr>::convert (e) (); |
820 |
shader_builder::cur->code << ";\n"; |
821 |
} |
822 |
|
823 |
template<class type> |
824 |
template<typename expr> |
825 |
inline const auto_lvalue_ref0<type> &auto_lvalue_ref0<type>::operator =(const expr &e) const |
826 |
{ |
827 |
sl_assign (*this, e); |
828 |
return *this; |
829 |
} |
830 |
|
831 |
template<class type, typename arg1> |
832 |
template<typename expr> |
833 |
inline const auto_lvalue_ref1<type,arg1> &auto_lvalue_ref1<type,arg1>::operator =(const expr &e) const |
834 |
{ |
835 |
sl_assign (*this, e); |
836 |
return *this; |
837 |
} |
838 |
|
839 |
template<class T> |
840 |
template<typename expr> |
841 |
inline const sl_expr<T> &sl_expr<T>::operator =(const expr &e) const |
842 |
{ |
843 |
sl_assign (*this, e); |
844 |
return *this; |
845 |
} |
846 |
|
847 |
template<const char *strtype, class var_i> |
848 |
template<typename expr> |
849 |
inline const glnvar_ref<strtype, var_i> &glnvar_ref<strtype, var_i>::operator =(const expr &e) const |
850 |
{ |
851 |
sl_assign (*this, e); |
852 |
return *this; |
853 |
} |
854 |
|
855 |
namespace compile { |
856 |
using namespace shader; |
857 |
|
858 |
template<typename T> |
859 |
inline const sl_expr< sl_func1<typename sl_convert<T>::T> > |
860 |
operator +(const T &t) |
861 |
{ |
862 |
return sl_func1<typename sl_convert<T>::T> ("+(", sl_convert<T>::convert (t)); |
863 |
} |
864 |
|
865 |
template<typename T> |
866 |
inline const sl_expr< sl_func1<typename sl_convert<T>::T> > |
867 |
operator -(const T &t) |
868 |
{ |
869 |
return sl_func1<typename sl_convert<T>::T> ("-(", sl_convert<T>::convert (t)); |
870 |
} |
871 |
|
872 |
template<class A, class C> |
873 |
struct sl_binop |
874 |
{ |
875 |
const A a; const char *b; const C c; |
876 |
sl_binop (const A &a, const char *b, const C &c) : a(a), b(b), c(c) { } |
877 |
void operator ()() const |
878 |
{ |
879 |
shader_builder::cur->code << "("; |
880 |
a (); |
881 |
shader_builder::cur->code << b; |
882 |
c (); |
883 |
shader_builder::cur->code << ")"; |
884 |
} |
885 |
}; |
886 |
|
887 |
# define SHADER_BINOP(op) \ |
888 |
template<typename A, typename B> \ |
889 |
inline const sl_expr< sl_binop< typename sl_convert<A>::T, \ |
890 |
typename sl_convert<B>::T > > \ |
891 |
operator op(const A &a, const B &b) \ |
892 |
{ \ |
893 |
return sl_binop< typename sl_convert<A>::T, typename sl_convert<B>::T > \ |
894 |
(sl_convert<A>::convert (a), " " # op " ", sl_convert<B>::convert (b)); \ |
895 |
} |
896 |
|
897 |
SHADER_BINOP (+); |
898 |
SHADER_BINOP (-); |
899 |
SHADER_BINOP (*); |
900 |
SHADER_BINOP (/); |
901 |
SHADER_BINOP (%); |
902 |
|
903 |
SHADER_BINOP (<); |
904 |
SHADER_BINOP (<=); |
905 |
SHADER_BINOP (==); |
906 |
SHADER_BINOP (!=); |
907 |
SHADER_BINOP (>=); |
908 |
SHADER_BINOP (>); |
909 |
|
910 |
# undef SHADER_BINOP |
911 |
|
912 |
template<class A> |
913 |
struct sl_swizzle |
914 |
{ |
915 |
const A a; |
916 |
const char *swizzle; |
917 |
sl_swizzle (const A &a, const char *swizzle) : a(a), swizzle(swizzle) { } |
918 |
void operator ()() const |
919 |
{ |
920 |
shader_builder::cur->code << "("; |
921 |
a (); |
922 |
shader_builder::cur->code << ")." << swizzle; |
923 |
} |
924 |
}; |
925 |
|
926 |
# define SHADER_SWIZZLE_OP(type) \ |
927 |
template<typename T> \ |
928 |
inline const sl_expr< sl_swizzle<typename sl_convert<T>::T> > \ |
929 |
type (const T &t) \ |
930 |
{ \ |
931 |
return sl_swizzle<typename sl_convert<T>::T> (sl_convert<T>::convert (t), #type); \ |
932 |
} |
933 |
|
934 |
// brute force is lovely, ain't it? |
935 |
SHADER_SWIZZLE_OP (x ) SHADER_SWIZZLE_OP (xx ) SHADER_SWIZZLE_OP (xxx ) SHADER_SWIZZLE_OP (xxxx) |
936 |
SHADER_SWIZZLE_OP (xxxy) SHADER_SWIZZLE_OP (xxxz) SHADER_SWIZZLE_OP (xxxw) SHADER_SWIZZLE_OP (xxy ) |
937 |
SHADER_SWIZZLE_OP (xxyx) SHADER_SWIZZLE_OP (xxyy) SHADER_SWIZZLE_OP (xxyz) SHADER_SWIZZLE_OP (xxyw) |
938 |
SHADER_SWIZZLE_OP (xxz ) SHADER_SWIZZLE_OP (xxzx) SHADER_SWIZZLE_OP (xxzy) SHADER_SWIZZLE_OP (xxzz) |
939 |
SHADER_SWIZZLE_OP (xxzw) SHADER_SWIZZLE_OP (xxw ) SHADER_SWIZZLE_OP (xxwx) SHADER_SWIZZLE_OP (xxwy) |
940 |
SHADER_SWIZZLE_OP (xxwz) SHADER_SWIZZLE_OP (xxww) SHADER_SWIZZLE_OP (xy ) SHADER_SWIZZLE_OP (xyx ) |
941 |
SHADER_SWIZZLE_OP (xyxx) SHADER_SWIZZLE_OP (xyxy) SHADER_SWIZZLE_OP (xyxz) SHADER_SWIZZLE_OP (xyxw) |
942 |
SHADER_SWIZZLE_OP (xyy ) SHADER_SWIZZLE_OP (xyyx) SHADER_SWIZZLE_OP (xyyy) SHADER_SWIZZLE_OP (xyyz) |
943 |
SHADER_SWIZZLE_OP (xyyw) SHADER_SWIZZLE_OP (xyz ) SHADER_SWIZZLE_OP (xyzx) SHADER_SWIZZLE_OP (xyzy) |
944 |
SHADER_SWIZZLE_OP (xyzz) SHADER_SWIZZLE_OP (xyzw) SHADER_SWIZZLE_OP (xyw ) SHADER_SWIZZLE_OP (xywx) |
945 |
SHADER_SWIZZLE_OP (xywy) SHADER_SWIZZLE_OP (xywz) SHADER_SWIZZLE_OP (xyww) SHADER_SWIZZLE_OP (xz ) |
946 |
SHADER_SWIZZLE_OP (xzx ) SHADER_SWIZZLE_OP (xzxx) SHADER_SWIZZLE_OP (xzxy) SHADER_SWIZZLE_OP (xzxz) |
947 |
SHADER_SWIZZLE_OP (xzxw) SHADER_SWIZZLE_OP (xzy ) SHADER_SWIZZLE_OP (xzyx) SHADER_SWIZZLE_OP (xzyy) |
948 |
SHADER_SWIZZLE_OP (xzyz) SHADER_SWIZZLE_OP (xzyw) SHADER_SWIZZLE_OP (xzz ) SHADER_SWIZZLE_OP (xzzx) |
949 |
SHADER_SWIZZLE_OP (xzzy) SHADER_SWIZZLE_OP (xzzz) SHADER_SWIZZLE_OP (xzzw) SHADER_SWIZZLE_OP (xzw ) |
950 |
SHADER_SWIZZLE_OP (xzwx) SHADER_SWIZZLE_OP (xzwy) SHADER_SWIZZLE_OP (xzwz) SHADER_SWIZZLE_OP (xzww) |
951 |
SHADER_SWIZZLE_OP (xw ) SHADER_SWIZZLE_OP (xwx ) SHADER_SWIZZLE_OP (xwxx) SHADER_SWIZZLE_OP (xwxy) |
952 |
SHADER_SWIZZLE_OP (xwxz) SHADER_SWIZZLE_OP (xwxw) SHADER_SWIZZLE_OP (xwy ) SHADER_SWIZZLE_OP (xwyx) |
953 |
SHADER_SWIZZLE_OP (xwyy) SHADER_SWIZZLE_OP (xwyz) SHADER_SWIZZLE_OP (xwyw) SHADER_SWIZZLE_OP (xwz ) |
954 |
SHADER_SWIZZLE_OP (xwzx) SHADER_SWIZZLE_OP (xwzy) SHADER_SWIZZLE_OP (xwzz) SHADER_SWIZZLE_OP (xwzw) |
955 |
SHADER_SWIZZLE_OP (xww ) SHADER_SWIZZLE_OP (xwwx) SHADER_SWIZZLE_OP (xwwy) SHADER_SWIZZLE_OP (xwwz) |
956 |
SHADER_SWIZZLE_OP (xwww) SHADER_SWIZZLE_OP (y ) SHADER_SWIZZLE_OP (yx ) SHADER_SWIZZLE_OP (yxx ) |
957 |
SHADER_SWIZZLE_OP (yxxx) SHADER_SWIZZLE_OP (yxxy) SHADER_SWIZZLE_OP (yxxz) SHADER_SWIZZLE_OP (yxxw) |
958 |
SHADER_SWIZZLE_OP (yxy ) SHADER_SWIZZLE_OP (yxyx) SHADER_SWIZZLE_OP (yxyy) SHADER_SWIZZLE_OP (yxyz) |
959 |
SHADER_SWIZZLE_OP (yxyw) SHADER_SWIZZLE_OP (yxz ) SHADER_SWIZZLE_OP (yxzx) SHADER_SWIZZLE_OP (yxzy) |
960 |
SHADER_SWIZZLE_OP (yxzz) SHADER_SWIZZLE_OP (yxzw) SHADER_SWIZZLE_OP (yxw ) SHADER_SWIZZLE_OP (yxwx) |
961 |
SHADER_SWIZZLE_OP (yxwy) SHADER_SWIZZLE_OP (yxwz) SHADER_SWIZZLE_OP (yxww) SHADER_SWIZZLE_OP (yy ) |
962 |
SHADER_SWIZZLE_OP (yyx ) SHADER_SWIZZLE_OP (yyxx) SHADER_SWIZZLE_OP (yyxy) SHADER_SWIZZLE_OP (yyxz) |
963 |
SHADER_SWIZZLE_OP (yyxw) SHADER_SWIZZLE_OP (yyy ) SHADER_SWIZZLE_OP (yyyx) SHADER_SWIZZLE_OP (yyyy) |
964 |
SHADER_SWIZZLE_OP (yyyz) SHADER_SWIZZLE_OP (yyyw) SHADER_SWIZZLE_OP (yyz ) SHADER_SWIZZLE_OP (yyzx) |
965 |
SHADER_SWIZZLE_OP (yyzy) SHADER_SWIZZLE_OP (yyzz) SHADER_SWIZZLE_OP (yyzw) SHADER_SWIZZLE_OP (yyw ) |
966 |
SHADER_SWIZZLE_OP (yywx) SHADER_SWIZZLE_OP (yywy) SHADER_SWIZZLE_OP (yywz) SHADER_SWIZZLE_OP (yyww) |
967 |
SHADER_SWIZZLE_OP (yz ) SHADER_SWIZZLE_OP (yzx ) SHADER_SWIZZLE_OP (yzxx) SHADER_SWIZZLE_OP (yzxy) |
968 |
SHADER_SWIZZLE_OP (yzxz) SHADER_SWIZZLE_OP (yzxw) SHADER_SWIZZLE_OP (yzy ) SHADER_SWIZZLE_OP (yzyx) |
969 |
SHADER_SWIZZLE_OP (yzyy) SHADER_SWIZZLE_OP (yzyz) SHADER_SWIZZLE_OP (yzyw) SHADER_SWIZZLE_OP (yzz ) |
970 |
SHADER_SWIZZLE_OP (yzzx) SHADER_SWIZZLE_OP (yzzy) SHADER_SWIZZLE_OP (yzzz) SHADER_SWIZZLE_OP (yzzw) |
971 |
SHADER_SWIZZLE_OP (yzw ) SHADER_SWIZZLE_OP (yzwx) SHADER_SWIZZLE_OP (yzwy) SHADER_SWIZZLE_OP (yzwz) |
972 |
SHADER_SWIZZLE_OP (yzww) SHADER_SWIZZLE_OP (yw ) SHADER_SWIZZLE_OP (ywx ) SHADER_SWIZZLE_OP (ywxx) |
973 |
SHADER_SWIZZLE_OP (ywxy) SHADER_SWIZZLE_OP (ywxz) SHADER_SWIZZLE_OP (ywxw) SHADER_SWIZZLE_OP (ywy ) |
974 |
SHADER_SWIZZLE_OP (ywyx) SHADER_SWIZZLE_OP (ywyy) SHADER_SWIZZLE_OP (ywyz) SHADER_SWIZZLE_OP (ywyw) |
975 |
SHADER_SWIZZLE_OP (ywz ) SHADER_SWIZZLE_OP (ywzx) SHADER_SWIZZLE_OP (ywzy) SHADER_SWIZZLE_OP (ywzz) |
976 |
SHADER_SWIZZLE_OP (ywzw) SHADER_SWIZZLE_OP (yww ) SHADER_SWIZZLE_OP (ywwx) SHADER_SWIZZLE_OP (ywwy) |
977 |
SHADER_SWIZZLE_OP (ywwz) SHADER_SWIZZLE_OP (ywww) SHADER_SWIZZLE_OP (z ) SHADER_SWIZZLE_OP (zx ) |
978 |
SHADER_SWIZZLE_OP (zxx ) SHADER_SWIZZLE_OP (zxxx) SHADER_SWIZZLE_OP (zxxy) SHADER_SWIZZLE_OP (zxxz) |
979 |
SHADER_SWIZZLE_OP (zxxw) SHADER_SWIZZLE_OP (zxy ) SHADER_SWIZZLE_OP (zxyx) SHADER_SWIZZLE_OP (zxyy) |
980 |
SHADER_SWIZZLE_OP (zxyz) SHADER_SWIZZLE_OP (zxyw) SHADER_SWIZZLE_OP (zxz ) SHADER_SWIZZLE_OP (zxzx) |
981 |
SHADER_SWIZZLE_OP (zxzy) SHADER_SWIZZLE_OP (zxzz) SHADER_SWIZZLE_OP (zxzw) SHADER_SWIZZLE_OP (zxw ) |
982 |
SHADER_SWIZZLE_OP (zxwx) SHADER_SWIZZLE_OP (zxwy) SHADER_SWIZZLE_OP (zxwz) SHADER_SWIZZLE_OP (zxww) |
983 |
SHADER_SWIZZLE_OP (zy ) SHADER_SWIZZLE_OP (zyx ) SHADER_SWIZZLE_OP (zyxx) SHADER_SWIZZLE_OP (zyxy) |
984 |
SHADER_SWIZZLE_OP (zyxz) SHADER_SWIZZLE_OP (zyxw) SHADER_SWIZZLE_OP (zyy ) SHADER_SWIZZLE_OP (zyyx) |
985 |
SHADER_SWIZZLE_OP (zyyy) SHADER_SWIZZLE_OP (zyyz) SHADER_SWIZZLE_OP (zyyw) SHADER_SWIZZLE_OP (zyz ) |
986 |
SHADER_SWIZZLE_OP (zyzx) SHADER_SWIZZLE_OP (zyzy) SHADER_SWIZZLE_OP (zyzz) SHADER_SWIZZLE_OP (zyzw) |
987 |
SHADER_SWIZZLE_OP (zyw ) SHADER_SWIZZLE_OP (zywx) SHADER_SWIZZLE_OP (zywy) SHADER_SWIZZLE_OP (zywz) |
988 |
SHADER_SWIZZLE_OP (zyww) SHADER_SWIZZLE_OP (zz ) SHADER_SWIZZLE_OP (zzx ) SHADER_SWIZZLE_OP (zzxx) |
989 |
SHADER_SWIZZLE_OP (zzxy) SHADER_SWIZZLE_OP (zzxz) SHADER_SWIZZLE_OP (zzxw) SHADER_SWIZZLE_OP (zzy ) |
990 |
SHADER_SWIZZLE_OP (zzyx) SHADER_SWIZZLE_OP (zzyy) SHADER_SWIZZLE_OP (zzyz) SHADER_SWIZZLE_OP (zzyw) |
991 |
SHADER_SWIZZLE_OP (zzz ) SHADER_SWIZZLE_OP (zzzx) SHADER_SWIZZLE_OP (zzzy) SHADER_SWIZZLE_OP (zzzz) |
992 |
SHADER_SWIZZLE_OP (zzzw) SHADER_SWIZZLE_OP (zzw ) SHADER_SWIZZLE_OP (zzwx) SHADER_SWIZZLE_OP (zzwy) |
993 |
SHADER_SWIZZLE_OP (zzwz) SHADER_SWIZZLE_OP (zzww) SHADER_SWIZZLE_OP (zw ) SHADER_SWIZZLE_OP (zwx ) |
994 |
SHADER_SWIZZLE_OP (zwxx) SHADER_SWIZZLE_OP (zwxy) SHADER_SWIZZLE_OP (zwxz) SHADER_SWIZZLE_OP (zwxw) |
995 |
SHADER_SWIZZLE_OP (zwy ) SHADER_SWIZZLE_OP (zwyx) SHADER_SWIZZLE_OP (zwyy) SHADER_SWIZZLE_OP (zwyz) |
996 |
SHADER_SWIZZLE_OP (zwyw) SHADER_SWIZZLE_OP (zwz ) SHADER_SWIZZLE_OP (zwzx) SHADER_SWIZZLE_OP (zwzy) |
997 |
SHADER_SWIZZLE_OP (zwzz) SHADER_SWIZZLE_OP (zwzw) SHADER_SWIZZLE_OP (zww ) SHADER_SWIZZLE_OP (zwwx) |
998 |
SHADER_SWIZZLE_OP (zwwy) SHADER_SWIZZLE_OP (zwwz) SHADER_SWIZZLE_OP (zwww) SHADER_SWIZZLE_OP (w ) |
999 |
SHADER_SWIZZLE_OP (wx ) SHADER_SWIZZLE_OP (wxx ) SHADER_SWIZZLE_OP (wxxx) SHADER_SWIZZLE_OP (wxxy) |
1000 |
SHADER_SWIZZLE_OP (wxxz) SHADER_SWIZZLE_OP (wxxw) SHADER_SWIZZLE_OP (wxy ) SHADER_SWIZZLE_OP (wxyx) |
1001 |
SHADER_SWIZZLE_OP (wxyy) SHADER_SWIZZLE_OP (wxyz) SHADER_SWIZZLE_OP (wxyw) SHADER_SWIZZLE_OP (wxz ) |
1002 |
SHADER_SWIZZLE_OP (wxzx) SHADER_SWIZZLE_OP (wxzy) SHADER_SWIZZLE_OP (wxzz) SHADER_SWIZZLE_OP (wxzw) |
1003 |
SHADER_SWIZZLE_OP (wxw ) SHADER_SWIZZLE_OP (wxwx) SHADER_SWIZZLE_OP (wxwy) SHADER_SWIZZLE_OP (wxwz) |
1004 |
SHADER_SWIZZLE_OP (wxww) SHADER_SWIZZLE_OP (wy ) SHADER_SWIZZLE_OP (wyx ) SHADER_SWIZZLE_OP (wyxx) |
1005 |
SHADER_SWIZZLE_OP (wyxy) SHADER_SWIZZLE_OP (wyxz) SHADER_SWIZZLE_OP (wyxw) SHADER_SWIZZLE_OP (wyy ) |
1006 |
SHADER_SWIZZLE_OP (wyyx) SHADER_SWIZZLE_OP (wyyy) SHADER_SWIZZLE_OP (wyyz) SHADER_SWIZZLE_OP (wyyw) |
1007 |
SHADER_SWIZZLE_OP (wyz ) SHADER_SWIZZLE_OP (wyzx) SHADER_SWIZZLE_OP (wyzy) SHADER_SWIZZLE_OP (wyzz) |
1008 |
SHADER_SWIZZLE_OP (wyzw) SHADER_SWIZZLE_OP (wyw ) SHADER_SWIZZLE_OP (wywx) SHADER_SWIZZLE_OP (wywy) |
1009 |
SHADER_SWIZZLE_OP (wywz) SHADER_SWIZZLE_OP (wyww) SHADER_SWIZZLE_OP (wz ) SHADER_SWIZZLE_OP (wzx ) |
1010 |
SHADER_SWIZZLE_OP (wzxx) SHADER_SWIZZLE_OP (wzxy) SHADER_SWIZZLE_OP (wzxz) SHADER_SWIZZLE_OP (wzxw) |
1011 |
SHADER_SWIZZLE_OP (wzy ) SHADER_SWIZZLE_OP (wzyx) SHADER_SWIZZLE_OP (wzyy) SHADER_SWIZZLE_OP (wzyz) |
1012 |
SHADER_SWIZZLE_OP (wzyw) SHADER_SWIZZLE_OP (wzz ) SHADER_SWIZZLE_OP (wzzx) SHADER_SWIZZLE_OP (wzzy) |
1013 |
SHADER_SWIZZLE_OP (wzzz) SHADER_SWIZZLE_OP (wzzw) SHADER_SWIZZLE_OP (wzw ) SHADER_SWIZZLE_OP (wzwx) |
1014 |
SHADER_SWIZZLE_OP (wzwy) SHADER_SWIZZLE_OP (wzwz) SHADER_SWIZZLE_OP (wzww) SHADER_SWIZZLE_OP (ww ) |
1015 |
SHADER_SWIZZLE_OP (wwx ) SHADER_SWIZZLE_OP (wwxx) SHADER_SWIZZLE_OP (wwxy) SHADER_SWIZZLE_OP (wwxz) |
1016 |
SHADER_SWIZZLE_OP (wwxw) SHADER_SWIZZLE_OP (wwy ) SHADER_SWIZZLE_OP (wwyx) SHADER_SWIZZLE_OP (wwyy) |
1017 |
SHADER_SWIZZLE_OP (wwyz) SHADER_SWIZZLE_OP (wwyw) SHADER_SWIZZLE_OP (wwz ) SHADER_SWIZZLE_OP (wwzx) |
1018 |
SHADER_SWIZZLE_OP (wwzy) SHADER_SWIZZLE_OP (wwzz) SHADER_SWIZZLE_OP (wwzw) SHADER_SWIZZLE_OP (www ) |
1019 |
SHADER_SWIZZLE_OP (wwwx) SHADER_SWIZZLE_OP (wwwy) SHADER_SWIZZLE_OP (wwwz) SHADER_SWIZZLE_OP (wwww) |
1020 |
|
1021 |
# undef SHADER_SWIZZLE_OP |
1022 |
|
1023 |
# define SHADER_FUNC0_(name, glname) \ |
1024 |
inline const sl_expr<sl_func0> \ |
1025 |
name () \ |
1026 |
{ \ |
1027 |
return sl_func0 (#glname " ("); \ |
1028 |
} |
1029 |
|
1030 |
# define SHADER_FUNC0(name) SHADER_FUNC0_(name,name) |
1031 |
|
1032 |
# define SHADER_FUNC1_(name, glname) \ |
1033 |
template<typename A> \ |
1034 |
inline const sl_expr< sl_func1<typename sl_convert<A>::T> > \ |
1035 |
name (const A &a) \ |
1036 |
{ \ |
1037 |
return sl_func1<typename sl_convert<A>::T> (#glname " (", sl_convert<A>::convert (a));\ |
1038 |
} |
1039 |
|
1040 |
# define SHADER_FUNC1(name) SHADER_FUNC1_(name,name) |
1041 |
|
1042 |
# define SHADER_FUNC2_(name, glname) \ |
1043 |
template<typename A, typename B> \ |
1044 |
inline const sl_expr< sl_func2<typename sl_convert<A>::T, typename sl_convert<B>::T> > \ |
1045 |
name (const A &a, const B &b) \ |
1046 |
{ \ |
1047 |
return sl_func2<typename sl_convert<A>::T, typename sl_convert<B>::T> (#glname " (", sl_convert<A>::convert (a), sl_convert<B>::convert (b));\ |
1048 |
} |
1049 |
|
1050 |
# define SHADER_FUNC2(name) SHADER_FUNC2_(name,name) |
1051 |
|
1052 |
# define SHADER_FUNC3_(name, glname) \ |
1053 |
template<typename A, typename B, typename C> \ |
1054 |
inline const sl_expr< sl_func3<typename sl_convert<A>::T, typename sl_convert<B>::T, typename sl_convert<C>::T> > \ |
1055 |
name (const A &a, const B &b, const C &c) \ |
1056 |
{ \ |
1057 |
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));\ |
1058 |
} |
1059 |
|
1060 |
# define SHADER_FUNC3(name) SHADER_FUNC3_(name,name) |
1061 |
|
1062 |
# define SHADER_FUNC4_(name, glname) \ |
1063 |
template<typename A, typename B, typename C, typename D> \ |
1064 |
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 > > \ |
1065 |
name (const A &a, const B &b, const C &c, const D &d) \ |
1066 |
{ \ |
1067 |
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));\ |
1068 |
} |
1069 |
|
1070 |
# define SHADER_FUNC4(name) SHADER_FUNC4_(name,name) |
1071 |
|
1072 |
SHADER_FUNC1 (abs) |
1073 |
SHADER_FUNC1 (acos) |
1074 |
SHADER_FUNC1 (all) |
1075 |
SHADER_FUNC1 (any) |
1076 |
SHADER_FUNC1 (asin) |
1077 |
SHADER_FUNC1 (atan) |
1078 |
SHADER_FUNC2 (atan) |
1079 |
SHADER_FUNC1 (ceil) |
1080 |
SHADER_FUNC3 (clamp) |
1081 |
SHADER_FUNC1 (cos) |
1082 |
SHADER_FUNC2 (cross) |
1083 |
SHADER_FUNC1 (dFdx) |
1084 |
SHADER_FUNC1 (dFdy) |
1085 |
SHADER_FUNC1 (degrees) |
1086 |
SHADER_FUNC2 (distance) |
1087 |
SHADER_FUNC2 (dot) |
1088 |
SHADER_FUNC2_(equal, equal) |
1089 |
SHADER_FUNC1 (exp) |
1090 |
SHADER_FUNC1 (exp2) |
1091 |
SHADER_FUNC3 (faceforward) |
1092 |
SHADER_FUNC1 (floor) |
1093 |
SHADER_FUNC1 (fract) |
1094 |
SHADER_FUNC0 (ftransform) |
1095 |
SHADER_FUNC1 (fwidth) |
1096 |
SHADER_FUNC2_(greater_than_equal, greaterThanEqual) |
1097 |
SHADER_FUNC2_(greater_then, greaterThan) |
1098 |
SHADER_FUNC1 (inversesqrt) |
1099 |
SHADER_FUNC1 (length) |
1100 |
SHADER_FUNC2_(less_than, lessThan) |
1101 |
SHADER_FUNC2_(less_than_equal, lessThanEqual) |
1102 |
SHADER_FUNC1 (log) |
1103 |
SHADER_FUNC1 (log2) |
1104 |
SHADER_FUNC2_(matrix_comp_mult, matrixCompMult) |
1105 |
SHADER_FUNC2 (max) |
1106 |
SHADER_FUNC2 (min) |
1107 |
SHADER_FUNC3 (mix) |
1108 |
SHADER_FUNC2 (mod) |
1109 |
SHADER_FUNC1 (noise1) |
1110 |
SHADER_FUNC1 (noise2) |
1111 |
SHADER_FUNC1 (noise3) |
1112 |
SHADER_FUNC1 (noise4) |
1113 |
SHADER_FUNC1 (normalize) |
1114 |
SHADER_FUNC1 (gl_not) // TODO |
1115 |
SHADER_FUNC2_(notequal, notEqual) |
1116 |
SHADER_FUNC2 (pow) |
1117 |
SHADER_FUNC1 (radians) |
1118 |
SHADER_FUNC2 (reflect) |
1119 |
SHADER_FUNC3 (refract) |
1120 |
SHADER_FUNC2_(shadow_1d, shadow1D) |
1121 |
SHADER_FUNC3_(shadow_1d, shadow1D) |
1122 |
SHADER_FUNC3_(shadow_1d_lod, shadow1DLod) |
1123 |
SHADER_FUNC2_(shadow_1d_proj, shadow1DProj) |
1124 |
SHADER_FUNC3_(shadow_1d_proj, shadow1DProj) |
1125 |
SHADER_FUNC3_(shadow_1d_proj_lod, shadow1DProjLod) |
1126 |
SHADER_FUNC2_(shadow_2d, shadow2D) |
1127 |
SHADER_FUNC3_(shadow_2d, shadow2D) |
1128 |
SHADER_FUNC3_(shadow_2d_lod, shadow2DLod) |
1129 |
SHADER_FUNC2_(shadow_2d_proj, shadow2DProj) |
1130 |
SHADER_FUNC3_(shadow_2d_proj, shadow2DProj) |
1131 |
SHADER_FUNC3_(shadow_2d_proj_lod, shadow2DProjLod) |
1132 |
SHADER_FUNC1 (sign) |
1133 |
SHADER_FUNC1 (sin) |
1134 |
SHADER_FUNC3 (smoothstep) |
1135 |
SHADER_FUNC1 (sqrt) |
1136 |
SHADER_FUNC2 (step) |
1137 |
SHADER_FUNC1 (tan) |
1138 |
SHADER_FUNC2_(texture_1d, texture1D) |
1139 |
SHADER_FUNC3_(texture_1d, texture1D) |
1140 |
SHADER_FUNC3_(texture_1d_lod, texture1DLod) |
1141 |
SHADER_FUNC2_(texture_1d_proj, texture1DProj) |
1142 |
SHADER_FUNC3_(texture_1d_proj, texture1DProj) |
1143 |
SHADER_FUNC3_(texture_1d_proj_lod, texture1DProjLod) |
1144 |
SHADER_FUNC2_(texture_2d, texture2D) |
1145 |
SHADER_FUNC3_(texture_2d, texture2D) |
1146 |
SHADER_FUNC3_(texture_2d_lod, texture2DLod) |
1147 |
SHADER_FUNC2_(texture_2d_proj, texture2DProj) |
1148 |
SHADER_FUNC3_(texture_2d_proj, texture2DProj) |
1149 |
SHADER_FUNC3_(texture_2d_proj_lod, texture2DProjLod) |
1150 |
SHADER_FUNC2_(texture_3d, texture3D) |
1151 |
SHADER_FUNC3_(texture_3d, texture3D) |
1152 |
SHADER_FUNC3_(texture_3d_lod, texture3DLod) |
1153 |
SHADER_FUNC2_(texture_3d_proj, texture3DProj) |
1154 |
SHADER_FUNC3_(texture_3d_proj, texture3DProj) |
1155 |
SHADER_FUNC3_(texture_3d_proj_lod, texture3DProjLod) |
1156 |
SHADER_FUNC2_(texture_cube, textureCube) |
1157 |
SHADER_FUNC3_(texture_cube, textureCube) |
1158 |
SHADER_FUNC3_(texture_cude_lod, textureCubeLod) |
1159 |
SHADER_FUNC1 (vec2) SHADER_FUNC2 (vec2) |
1160 |
SHADER_FUNC1 (vec3) SHADER_FUNC2 (vec3) SHADER_FUNC3 (vec3) |
1161 |
SHADER_FUNC1 (vec4) SHADER_FUNC2 (vec4) SHADER_FUNC3 (vec4) SHADER_FUNC4 (vec4) |
1162 |
SHADER_FUNC1 (mat2) SHADER_FUNC2 (mat2) |
1163 |
SHADER_FUNC1 (mat3) SHADER_FUNC2 (mat3) SHADER_FUNC3 (mat3) |
1164 |
SHADER_FUNC1 (mat4) SHADER_FUNC2 (mat4) SHADER_FUNC3 (mat4) SHADER_FUNC4 (mat4) |
1165 |
|
1166 |
# undef SHADER_FUNC0 |
1167 |
# undef SHADER_FUNC0_ |
1168 |
# undef SHADER_FUNC1 |
1169 |
# undef SHADER_FUNC1_ |
1170 |
# undef SHADER_FUNC2 |
1171 |
# undef SHADER_FUNC2_ |
1172 |
# undef SHADER_FUNC3 |
1173 |
# undef SHADER_FUNC3_ |
1174 |
# undef SHADER_FUNC4 |
1175 |
# undef SHADER_FUNC5_ |
1176 |
|
1177 |
template<class A, class B, class C> |
1178 |
struct sl_ifelse |
1179 |
{ |
1180 |
const A a; const B b; const C c; |
1181 |
sl_ifelse (const A &a, const B &b, const C &c) : a(a), b(b), c(c) { } |
1182 |
void operator ()() const |
1183 |
{ |
1184 |
shader_builder::cur->code << "("; |
1185 |
a (); |
1186 |
shader_builder::cur->code << ") ? ("; |
1187 |
b (); |
1188 |
shader_builder::cur->code << ") : ("; |
1189 |
c (); |
1190 |
shader_builder::cur->code << ")"; |
1191 |
} |
1192 |
}; |
1193 |
|
1194 |
template<typename A, typename B, typename C> |
1195 |
inline const sl_expr< sl_ifelse<typename sl_convert<A>::T, typename sl_convert<B>::T, typename sl_convert<C>::T> > |
1196 |
ifelse (const A &a, const B &b, const C &c) |
1197 |
{ |
1198 |
return sl_ifelse<typename sl_convert<A>::T, typename sl_convert<B>::T, typename sl_convert<C>::T> |
1199 |
(sl_convert<A>::convert (a), sl_convert<B>::convert (b), sl_convert<C>::convert (c)); |
1200 |
} |
1201 |
|
1202 |
} |
1203 |
} |
1204 |
|
1205 |
#endif |
1206 |
|
1207 |
#include "shader_vars.h" |
1208 |
|
1209 |
|