1 |
root |
1.12 |
#include <cassert> |
2 |
|
|
|
3 |
root |
1.1 |
#include "shader.h" |
4 |
|
|
#include "shader_vars.h" |
5 |
|
|
|
6 |
root |
1.17 |
#include <sstream> |
7 |
|
|
#include <iomanip> |
8 |
|
|
|
9 |
root |
1.18 |
namespace shader { |
10 |
root |
1.17 |
|
11 |
root |
1.18 |
using namespace std; |
12 |
root |
1.1 |
|
13 |
|
|
refcounted::~refcounted () |
14 |
|
|
{ |
15 |
|
|
if (refcnt) |
16 |
|
|
abort (); |
17 |
|
|
} |
18 |
|
|
|
19 |
root |
1.6 |
void refcounted::refcnt_dec () const |
20 |
|
|
{ |
21 |
|
|
if (!--refcnt) |
22 |
|
|
delete this; // quite a bit of code... |
23 |
|
|
} |
24 |
|
|
|
25 |
root |
1.1 |
const char str_float [] = "float"; |
26 |
|
|
const char str_vec2 [] = "vec2"; |
27 |
|
|
const char str_vec3 [] = "vec3"; |
28 |
|
|
const char str_vec4 [] = "vec4"; |
29 |
|
|
const char str_mat2 [] = "mat2"; |
30 |
|
|
const char str_mat3 [] = "mat3"; |
31 |
|
|
const char str_mat4 [] = "mat4"; |
32 |
|
|
|
33 |
|
|
const char str_sampler_1d [] = "sampler1D"; |
34 |
|
|
const char str_sampler_1d_shadow [] = "sampler1DShadow"; |
35 |
|
|
const char str_sampler_2d [] = "sampler2D"; |
36 |
|
|
const char str_sampler_2d_shadow [] = "sampler2DShadow"; |
37 |
|
|
const char str_sampler_2d_rect [] = "sampler2DRect"; |
38 |
|
|
const char str_sampler_2d_rect_shadow [] = "sampler2DRectShadow"; |
39 |
|
|
const char str_sampler_3d [] = "sampler3D"; |
40 |
|
|
const char str_sampler_3d_rect [] = "sampler3DRect"; |
41 |
|
|
const char str_sampler_cube [] = "samplerCube"; |
42 |
|
|
|
43 |
|
|
unsigned int var_i::next_id = 0; |
44 |
|
|
|
45 |
|
|
var_i::var_i (const char *typestr) |
46 |
|
|
: typestr (typestr) |
47 |
|
|
{ |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
var_i::~var_i () |
51 |
|
|
{ |
52 |
|
|
} |
53 |
|
|
|
54 |
root |
1.24 |
#if 0 |
55 |
root |
1.4 |
stream_i::stream_i (const char *strtype) |
56 |
|
|
: var_i (strtype) |
57 |
|
|
{ |
58 |
root |
1.19 |
sprintf (name, "V%d", ++next_id); |
59 |
root |
1.4 |
} |
60 |
root |
1.24 |
#endif |
61 |
root |
1.4 |
|
62 |
root |
1.1 |
temporary_i::temporary_i (const char *strtype) |
63 |
|
|
: var_i (strtype) |
64 |
|
|
{ |
65 |
root |
1.19 |
sprintf (name, "T%d", ++next_id); |
66 |
root |
1.1 |
} |
67 |
|
|
|
68 |
root |
1.24 |
varying_i::varying_i (const char *strtype) |
69 |
|
|
: var_i (strtype) |
70 |
|
|
{ |
71 |
|
|
sprintf (name, "V%d", ++next_id); |
72 |
|
|
} |
73 |
|
|
|
74 |
root |
1.1 |
uniform_i::uniform_i (const char *strtype) |
75 |
root |
1.19 |
: var_i (strtype) |
76 |
root |
1.1 |
{ |
77 |
root |
1.19 |
sprintf (name, "U%d", ++next_id); |
78 |
root |
1.1 |
} |
79 |
|
|
|
80 |
|
|
fragment_string_i::~fragment_string_i () |
81 |
|
|
{ |
82 |
|
|
free (str); |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
//////////////////////////////////////////////////////////////////////////// |
86 |
|
|
|
87 |
|
|
void var_i::build_decl (ostringstream &b) |
88 |
|
|
{ |
89 |
|
|
b << typestr << ' ' << name; |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
void uniform_i::build_decl (ostringstream &b) |
93 |
|
|
{ |
94 |
|
|
b << "uniform " << typestr << ' ' << name; |
95 |
|
|
} |
96 |
|
|
|
97 |
root |
1.24 |
void varying_i::build_decl (ostringstream &b) |
98 |
root |
1.1 |
{ |
99 |
root |
1.24 |
b << "varying " << typestr << ' ' << name; |
100 |
root |
1.1 |
} |
101 |
|
|
|
102 |
|
|
//////////////////////////////////////////////////////////////////////////// |
103 |
|
|
|
104 |
root |
1.16 |
void gluvar_i::build (shader_builder &b) |
105 |
root |
1.1 |
{ |
106 |
|
|
b << name; |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
void var_i::build (shader_builder &b) |
110 |
|
|
{ |
111 |
|
|
b << name; |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
void uniform_i::build (shader_builder &b) |
115 |
|
|
{ |
116 |
|
|
var_i::build (b); |
117 |
|
|
|
118 |
|
|
if (find (b.refs.begin (), b.refs.end (), uniform (*this)) == b.refs.end ()) |
119 |
|
|
b.refs.push_back (*this); |
120 |
|
|
} |
121 |
|
|
|
122 |
root |
1.24 |
#if 0 |
123 |
root |
1.1 |
void stream_i::build (shader_builder &b) |
124 |
|
|
{ |
125 |
|
|
var_i::build (b); |
126 |
|
|
|
127 |
root |
1.22 |
#if 0 |
128 |
root |
1.1 |
if (find (b.streams.begin (), b.streams.end (), var (*this)) == b.streams.end ()) |
129 |
|
|
b.streams.push_back (*this); |
130 |
root |
1.22 |
#endif |
131 |
root |
1.1 |
} |
132 |
root |
1.24 |
#endif |
133 |
root |
1.1 |
|
134 |
|
|
void temporary_i::build (shader_builder &b) |
135 |
|
|
{ |
136 |
|
|
var_i::build (b); |
137 |
|
|
|
138 |
|
|
if (find (b.temps.begin (), b.temps.end (), var (*this)) == b.temps.end ()) |
139 |
|
|
b.temps.push_back (*this); |
140 |
|
|
} |
141 |
|
|
|
142 |
root |
1.24 |
void varying_i::build (shader_builder &b) |
143 |
|
|
{ |
144 |
|
|
var_i::build (b); |
145 |
|
|
|
146 |
|
|
if (find (b.refs.begin (), b.refs.end (), var (*this)) == b.refs.end ()) |
147 |
|
|
b.refs.push_back (*this); |
148 |
|
|
} |
149 |
|
|
|
150 |
root |
1.1 |
void fragment_vector_i::build (shader_builder &b) |
151 |
|
|
{ |
152 |
|
|
for (vector<fragment>::iterator i = begin (); i != end (); i++) |
153 |
|
|
(*i)->build (b); |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
void fragment_const_string_i::build (shader_builder &b) |
157 |
|
|
{ |
158 |
|
|
b << str; |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
void fragment_string_i::build (shader_builder &b) |
162 |
|
|
{ |
163 |
|
|
b << str; |
164 |
|
|
} |
165 |
|
|
|
166 |
|
|
#if 0 |
167 |
|
|
void statement_i::build (shader_builder &b) |
168 |
|
|
{ |
169 |
|
|
b << " "; |
170 |
|
|
fragment_vector_i::build (b); |
171 |
|
|
b << ";\n"; |
172 |
|
|
} |
173 |
|
|
#endif |
174 |
|
|
|
175 |
|
|
//////////////////////////////////////////////////////////////////////////// |
176 |
|
|
|
177 |
root |
1.20 |
int texture_units::unit_count = 8; |
178 |
root |
1.21 |
int texture_units::units[8] = { 7, 6, 5, 4, 3, 2, 1, 0 }; |
179 |
root |
1.20 |
|
180 |
root |
1.1 |
shader_object_i *cur = 0; |
181 |
|
|
|
182 |
|
|
shader_object_i::shader_object_i (GLenum type) |
183 |
|
|
: type (type) |
184 |
|
|
{ |
185 |
|
|
id = glCreateShaderObjectARB (type); |
186 |
root |
1.12 |
assert (id); |
187 |
root |
1.1 |
} |
188 |
|
|
|
189 |
|
|
shader_object_i::~shader_object_i () |
190 |
|
|
{ |
191 |
|
|
glDeleteObjectARB (id); |
192 |
|
|
} |
193 |
|
|
|
194 |
|
|
void shader_object_i::start () |
195 |
|
|
{ |
196 |
|
|
clear (); |
197 |
|
|
cur = this; |
198 |
|
|
} |
199 |
|
|
|
200 |
|
|
void shader_object_i::stop () |
201 |
|
|
{ |
202 |
|
|
cur = 0; |
203 |
|
|
} |
204 |
|
|
|
205 |
|
|
string shader_object_i::source () |
206 |
|
|
{ |
207 |
|
|
shader_builder b; |
208 |
|
|
build (b); |
209 |
|
|
ostringstream os; |
210 |
|
|
|
211 |
root |
1.24 |
for (vector<var>::iterator i = b.refs.begin (); i != b.refs.end (); i++) |
212 |
root |
1.1 |
{ |
213 |
|
|
(*i)->build_decl (os); |
214 |
|
|
os << ";\n"; |
215 |
|
|
} |
216 |
|
|
|
217 |
root |
1.22 |
#if 0 |
218 |
root |
1.1 |
// not neccessary right now, as GLSL is rich on predefinitions |
219 |
|
|
for (vector<var>::iterator i = b.streams.begin (); i != b.streams.end (); i++) |
220 |
|
|
{ |
221 |
|
|
os << "attribute "; |
222 |
|
|
(*i)->build_decl (os); |
223 |
|
|
os << ";\n"; |
224 |
|
|
} |
225 |
root |
1.22 |
#endif |
226 |
root |
1.1 |
|
227 |
|
|
os << "\nvoid main (void)\n{\n"; |
228 |
|
|
|
229 |
|
|
for (vector<var>::iterator i = b.temps.begin (); i != b.temps.end (); i++) |
230 |
|
|
{ |
231 |
|
|
os << " "; |
232 |
|
|
(*i)->build_decl (os); |
233 |
|
|
os << ";\n"; |
234 |
|
|
} |
235 |
|
|
|
236 |
|
|
os << "\n"; |
237 |
|
|
os << b.source.str (); |
238 |
|
|
os << "}\n"; |
239 |
|
|
|
240 |
|
|
return os.str (); |
241 |
|
|
} |
242 |
|
|
|
243 |
root |
1.17 |
static string linify (const string &s) |
244 |
|
|
{ |
245 |
|
|
ostringstream o; |
246 |
|
|
|
247 |
|
|
int b = 0, e; |
248 |
|
|
int l = 1; |
249 |
|
|
do { |
250 |
|
|
o << setw (3) << l << ": "; |
251 |
|
|
e = s.find ('\n', b); |
252 |
|
|
if (e == string::npos) |
253 |
|
|
e = s.size (); |
254 |
|
|
|
255 |
|
|
o << s.substr (b, e - b + 1); |
256 |
|
|
b = e + 1; |
257 |
|
|
l++; |
258 |
|
|
} while (b < s.size ()); |
259 |
|
|
|
260 |
|
|
return o.str (); |
261 |
|
|
} |
262 |
|
|
|
263 |
root |
1.1 |
void shader_object_i::compile () |
264 |
|
|
{ |
265 |
|
|
string src = source (); |
266 |
|
|
const char *sptr = src.data (); |
267 |
|
|
const int slen = src.size (); |
268 |
|
|
|
269 |
root |
1.17 |
printf ("%s\n", linify (src).c_str ()); |
270 |
root |
1.1 |
glShaderSourceARB (id, 1, &sptr, &slen); |
271 |
|
|
glCompileShaderARB (id); |
272 |
|
|
|
273 |
|
|
GLint compiled; |
274 |
|
|
glGetObjectParameterivARB (id, GL_OBJECT_COMPILE_STATUS_ARB, &compiled); |
275 |
|
|
|
276 |
|
|
if (!compiled) |
277 |
|
|
{ |
278 |
|
|
char infolog[8192]; |
279 |
|
|
glGetInfoLogARB (id, 8192, NULL, infolog); |
280 |
root |
1.17 |
printf ("%s\n", linify (src).c_str ()); |
281 |
|
|
printf ("%s\n", infolog); |
282 |
root |
1.12 |
abort (); |
283 |
|
|
} |
284 |
|
|
} |
285 |
|
|
|
286 |
root |
1.19 |
//////////////////////////////////////////////////////////////////////////// |
287 |
|
|
|
288 |
|
|
program_object *program_object::cur = 0; |
289 |
|
|
|
290 |
|
|
GLint uniform_i::location () |
291 |
|
|
{ |
292 |
|
|
assert (program_object::cur); |
293 |
|
|
|
294 |
|
|
GLint &rid = program_object::cur->uloc[this]; |
295 |
|
|
|
296 |
|
|
if (!rid) |
297 |
|
|
rid = glGetUniformLocationARB (program_object::cur->id, name); |
298 |
|
|
|
299 |
|
|
return rid; |
300 |
|
|
} |
301 |
|
|
|
302 |
root |
1.12 |
program_object::program_object () |
303 |
|
|
{ |
304 |
|
|
id = glCreateProgramObjectARB (); |
305 |
|
|
assert (id); |
306 |
root |
1.15 |
|
307 |
|
|
glAttachObjectARB (id, vsh->id); |
308 |
|
|
glAttachObjectARB (id, fsh->id); |
309 |
root |
1.12 |
} |
310 |
|
|
|
311 |
|
|
program_object::~program_object () |
312 |
|
|
{ |
313 |
|
|
glDeleteProgramsARB (1, &id); |
314 |
|
|
} |
315 |
|
|
|
316 |
|
|
void program_object::link () |
317 |
|
|
{ |
318 |
|
|
glLinkProgramARB (id); |
319 |
|
|
|
320 |
|
|
GLint linked; |
321 |
|
|
glGetObjectParameterivARB (id, GL_OBJECT_LINK_STATUS_ARB, &linked); |
322 |
|
|
|
323 |
|
|
if (!linked) |
324 |
|
|
{ |
325 |
|
|
char infolog[8192]; |
326 |
|
|
glGetInfoLogARB (id, 8192, NULL, infolog); |
327 |
|
|
printf ("LINK-INFOLOG<%s>\n", infolog); |
328 |
root |
1.1 |
abort (); |
329 |
|
|
} |
330 |
root |
1.19 |
|
331 |
|
|
uloc.clear (); |
332 |
root |
1.22 |
} |
333 |
|
|
|
334 |
|
|
void program_object::enable () |
335 |
|
|
{ |
336 |
|
|
glUseProgramObjectARB (id); |
337 |
|
|
//TODO: set samplers here |
338 |
|
|
cur = this; |
339 |
|
|
} |
340 |
|
|
|
341 |
|
|
void program_object::disable () |
342 |
|
|
{ |
343 |
|
|
//TODO: clear samplers here |
344 |
|
|
glUseProgramObjectARB (0); |
345 |
|
|
cur = 0; |
346 |
root |
1.1 |
} |
347 |
|
|
|
348 |
root |
1.7 |
void sl_func0::begin () const |
349 |
|
|
{ |
350 |
|
|
cur->append_string (name_par); |
351 |
|
|
} |
352 |
|
|
|
353 |
root |
1.8 |
void sl_func0::comma () const |
354 |
|
|
{ |
355 |
root |
1.23 |
cur->append (compile::str_comma); |
356 |
root |
1.8 |
} |
357 |
|
|
|
358 |
root |
1.7 |
void sl_func0::end () const |
359 |
|
|
{ |
360 |
root |
1.25 |
cur->append_const (")"); |
361 |
root |
1.7 |
} |
362 |
|
|
|
363 |
root |
1.6 |
void sl_float::operator ()() const |
364 |
|
|
{ |
365 |
|
|
char s[20]; |
366 |
|
|
sprintf (s, "%g", c); |
367 |
|
|
cur->append_string (s); |
368 |
|
|
} |
369 |
|
|
|
370 |
root |
1.9 |
const sl_convert< ::vec2 >::T sl_convert< ::vec2 >::convert (const ::vec2 &v) |
371 |
root |
1.6 |
{ |
372 |
|
|
sl_string<60> s; |
373 |
|
|
sprintf (s.str, "vec2 (%g, %g)", v.x, v.y); |
374 |
|
|
return s; |
375 |
|
|
} |
376 |
|
|
|
377 |
root |
1.9 |
const sl_convert< ::vec3 >::T sl_convert< ::vec3 >::convert (const ::vec3 &v) |
378 |
root |
1.6 |
{ |
379 |
|
|
sl_string<80> s; |
380 |
|
|
sprintf (s.str, "vec3 (%g, %g, %g)", v.x, v.y, v.z); |
381 |
|
|
return s; |
382 |
|
|
} |
383 |
|
|
|
384 |
root |
1.9 |
const sl_convert< ::vec4 >::T sl_convert< ::vec4 >::convert (const ::vec4 &v) |
385 |
root |
1.6 |
{ |
386 |
|
|
sl_string<100> s; |
387 |
|
|
sprintf (s.str, "vec4 (%g, %g, %g, %g)", v.x, v.y, v.z, v.w); |
388 |
|
|
return s; |
389 |
|
|
} |
390 |
|
|
|
391 |
root |
1.23 |
namespace compile { |
392 |
root |
1.5 |
|
393 |
root |
1.23 |
const fragment_const_string str_2sp (" "); |
394 |
|
|
const fragment_const_string str_equal (" = "); |
395 |
|
|
const fragment_const_string str_comma (", "); |
396 |
|
|
const fragment_const_string str_endl (";\n"); |
397 |
root |
1.5 |
} |
398 |
|
|
|
399 |
root |
1.1 |
void debdebdebdebug ()//D |
400 |
|
|
{ |
401 |
root |
1.20 |
return; |
402 |
|
|
|
403 |
root |
1.23 |
using namespace compile; |
404 |
|
|
|
405 |
root |
1.1 |
vertex_shader vsh; |
406 |
|
|
|
407 |
|
|
vsh->start (); |
408 |
|
|
|
409 |
|
|
temp_4f lightpos; |
410 |
root |
1.18 |
temp_4f wpos; |
411 |
root |
1.1 |
|
412 |
root |
1.13 |
lightpos = vec4 (0, 10, 0, 1); |
413 |
root |
1.23 |
wpos = model_view_matrix * vin.vertex; |
414 |
|
|
vout.position = vin.vertex * model_view_matrix_inverse; |
415 |
root |
1.7 |
vout.tex_coord[0] = vin.tex_coord[0]; |
416 |
root |
1.18 |
vout.tex_coord[1] = normalize (lightpos - wpos); |
417 |
|
|
vout.tex_coord[2] = normalize (wpos); |
418 |
root |
1.23 |
//vout.tex_coord[3] = normalize (xyz (model_view_matrix_inverse_transpose) * vin.normal); |
419 |
|
|
//vout.tex_coord[4] = normalize (xyz (projection_matrix_inverse_transpose) - wpos); |
420 |
root |
1.1 |
|
421 |
|
|
vsh->end (); |
422 |
root |
1.13 |
vsh->compile (); |
423 |
root |
1.10 |
|
424 |
|
|
fragment_shader fsh; |
425 |
|
|
|
426 |
|
|
fsh->start (); |
427 |
|
|
|
428 |
root |
1.11 |
xyz (fout.frag_color) = noise3 (x (fin.frag_coord) * y (fin.frag_coord)); |
429 |
|
|
|
430 |
root |
1.10 |
temp_1f spec_expon; |
431 |
|
|
spec_expon = 200; |
432 |
|
|
|
433 |
|
|
fsh->end (); |
434 |
|
|
fsh->compile (); |
435 |
|
|
|
436 |
root |
1.11 |
//abort (); |
437 |
root |
1.1 |
} |
438 |
|
|
|
439 |
|
|
} |
440 |
|
|
|