|
|
1 | #include <cassert> |
|
|
2 | |
1 | #include "shader.h" |
3 | #include "shader.h" |
2 | #include "shader_vars.h" |
4 | #include "shader_vars.h" |
3 | |
5 | |
|
|
6 | #include <sstream> |
|
|
7 | #include <iomanip> |
|
|
8 | |
4 | namespace shader { |
9 | namespace shader { |
5 | |
10 | |
|
|
11 | using namespace std; |
|
|
12 | |
6 | refcounted::~refcounted () |
13 | refcounted::~refcounted () |
7 | { |
14 | { |
8 | #if 1 |
|
|
9 | if (refcnt) |
15 | if (refcnt) |
10 | abort (); |
16 | abort (); |
11 | #endif |
|
|
12 | } |
17 | } |
13 | |
18 | |
14 | void refcounted::refcnt_dec () const |
19 | void refcounted::refcnt_dec () const |
15 | { |
20 | { |
16 | if (!--refcnt) |
21 | if (!--refcnt) |
… | |
… | |
49 | } |
54 | } |
50 | |
55 | |
51 | stream_i::stream_i (const char *strtype) |
56 | stream_i::stream_i (const char *strtype) |
52 | : var_i (strtype) |
57 | : var_i (strtype) |
53 | { |
58 | { |
54 | sprintf (name, "V%lx_%d", ((long)this >> 4) & 0xfff, ++next_id); |
59 | sprintf (name, "V%d", ++next_id); |
55 | } |
60 | } |
56 | |
61 | |
57 | temporary_i::temporary_i (const char *strtype) |
62 | temporary_i::temporary_i (const char *strtype) |
58 | : var_i (strtype) |
63 | : var_i (strtype) |
59 | { |
64 | { |
60 | sprintf (name, "T%lx_%d", ((long)this >> 4) & 0xfff, ++next_id); |
65 | sprintf (name, "T%d", ++next_id); |
61 | } |
66 | } |
62 | |
67 | |
63 | uniform_i::uniform_i (const char *strtype) |
68 | uniform_i::uniform_i (const char *strtype) |
64 | : var_i (strtype), dirty (true) |
69 | : var_i (strtype) |
65 | { |
70 | { |
66 | sprintf (name, "U%lx_%d", ((long)this >> 4) & 0xfff, ++next_id); |
71 | sprintf (name, "U%d", ++next_id); |
67 | } |
72 | } |
68 | |
73 | |
69 | fragment_string_i::~fragment_string_i () |
74 | fragment_string_i::~fragment_string_i () |
70 | { |
75 | { |
71 | free (str); |
76 | free (str); |
… | |
… | |
88 | b << typestr << ' ' << name; |
93 | b << typestr << ' ' << name; |
89 | } |
94 | } |
90 | |
95 | |
91 | //////////////////////////////////////////////////////////////////////////// |
96 | //////////////////////////////////////////////////////////////////////////// |
92 | |
97 | |
93 | void glvar_i::build (shader_builder &b) |
98 | void gluvar_i::build (shader_builder &b) |
94 | { |
99 | { |
95 | b << name; |
100 | b << name; |
96 | } |
101 | } |
97 | |
102 | |
98 | void var_i::build (shader_builder &b) |
103 | void var_i::build (shader_builder &b) |
… | |
… | |
149 | } |
154 | } |
150 | #endif |
155 | #endif |
151 | |
156 | |
152 | //////////////////////////////////////////////////////////////////////////// |
157 | //////////////////////////////////////////////////////////////////////////// |
153 | |
158 | |
|
|
159 | int texture_units::unit_count = 8; |
|
|
160 | GLenum texture_units::units[8] = |
|
|
161 | { |
|
|
162 | GL_TEXTURE7, GL_TEXTURE6, GL_TEXTURE5, GL_TEXTURE4, |
|
|
163 | GL_TEXTURE3, GL_TEXTURE2, GL_TEXTURE1, GL_TEXTURE0, |
|
|
164 | }; |
|
|
165 | |
154 | shader_object_i *cur = 0; |
166 | shader_object_i *cur = 0; |
155 | |
167 | |
156 | shader_object_i::shader_object_i (GLenum type) |
168 | shader_object_i::shader_object_i (GLenum type) |
157 | : type (type) |
169 | : type (type) |
158 | { |
170 | { |
159 | id = glCreateShaderObjectARB (type); |
171 | id = glCreateShaderObjectARB (type); |
|
|
172 | assert (id); |
160 | } |
173 | } |
161 | |
174 | |
162 | shader_object_i::~shader_object_i () |
175 | shader_object_i::~shader_object_i () |
163 | { |
176 | { |
164 | glDeleteObjectARB (id); |
177 | glDeleteObjectARB (id); |
… | |
… | |
209 | os << "}\n"; |
222 | os << "}\n"; |
210 | |
223 | |
211 | return os.str (); |
224 | return os.str (); |
212 | } |
225 | } |
213 | |
226 | |
|
|
227 | static string linify (const string &s) |
|
|
228 | { |
|
|
229 | ostringstream o; |
|
|
230 | |
|
|
231 | int b = 0, e; |
|
|
232 | int l = 1; |
|
|
233 | do { |
|
|
234 | o << setw (3) << l << ": "; |
|
|
235 | e = s.find ('\n', b); |
|
|
236 | if (e == string::npos) |
|
|
237 | e = s.size (); |
|
|
238 | |
|
|
239 | o << s.substr (b, e - b + 1); |
|
|
240 | b = e + 1; |
|
|
241 | l++; |
|
|
242 | } while (b < s.size ()); |
|
|
243 | |
|
|
244 | return o.str (); |
|
|
245 | } |
|
|
246 | |
214 | void shader_object_i::compile () |
247 | void shader_object_i::compile () |
215 | { |
248 | { |
216 | string src = source (); |
249 | string src = source (); |
217 | const char *sptr = src.data (); |
250 | const char *sptr = src.data (); |
218 | const int slen = src.size (); |
251 | const int slen = src.size (); |
219 | |
252 | |
220 | printf ("SOURCE<%s>\n", src.c_str ()); |
253 | printf ("%s\n", linify (src).c_str ()); |
221 | glShaderSourceARB (id, 1, &sptr, &slen); |
254 | glShaderSourceARB (id, 1, &sptr, &slen); |
222 | glCompileShaderARB (id); |
255 | glCompileShaderARB (id); |
223 | |
256 | |
224 | GLint compiled; |
257 | GLint compiled; |
225 | glGetObjectParameterivARB (id, GL_OBJECT_COMPILE_STATUS_ARB, &compiled); |
258 | glGetObjectParameterivARB (id, GL_OBJECT_COMPILE_STATUS_ARB, &compiled); |
226 | |
259 | |
227 | if (!compiled) |
260 | if (!compiled) |
228 | { |
261 | { |
229 | char infolog[8192]; |
262 | char infolog[8192]; |
230 | glGetInfoLogARB (id, 8192, NULL, infolog); |
263 | glGetInfoLogARB (id, 8192, NULL, infolog); |
231 | printf ("SOURCE<%s>\n", src.c_str ()); |
264 | printf ("%s\n", linify (src).c_str ()); |
232 | printf ("INFOLOG<%s>\n", infolog); |
265 | printf ("%s\n", infolog); |
233 | abort (); |
266 | abort (); |
234 | } |
267 | } |
|
|
268 | } |
|
|
269 | |
|
|
270 | //////////////////////////////////////////////////////////////////////////// |
|
|
271 | |
|
|
272 | program_object *program_object::cur = 0; |
|
|
273 | |
|
|
274 | GLint uniform_i::location () |
|
|
275 | { |
|
|
276 | assert (program_object::cur); |
|
|
277 | |
|
|
278 | GLint &rid = program_object::cur->uloc[this]; |
|
|
279 | |
|
|
280 | if (!rid) |
|
|
281 | rid = glGetUniformLocationARB (program_object::cur->id, name); |
|
|
282 | |
|
|
283 | return rid; |
|
|
284 | } |
|
|
285 | |
|
|
286 | program_object::program_object () |
|
|
287 | { |
|
|
288 | id = glCreateProgramObjectARB (); |
|
|
289 | assert (id); |
|
|
290 | |
|
|
291 | glAttachObjectARB (id, vsh->id); |
|
|
292 | glAttachObjectARB (id, fsh->id); |
|
|
293 | } |
|
|
294 | |
|
|
295 | program_object::~program_object () |
|
|
296 | { |
|
|
297 | glDeleteProgramsARB (1, &id); |
|
|
298 | } |
|
|
299 | |
|
|
300 | void program_object::link () |
|
|
301 | { |
|
|
302 | glLinkProgramARB (id); |
|
|
303 | |
|
|
304 | GLint linked; |
|
|
305 | glGetObjectParameterivARB (id, GL_OBJECT_LINK_STATUS_ARB, &linked); |
|
|
306 | |
|
|
307 | if (!linked) |
|
|
308 | { |
|
|
309 | char infolog[8192]; |
|
|
310 | glGetInfoLogARB (id, 8192, NULL, infolog); |
|
|
311 | printf ("LINK-INFOLOG<%s>\n", infolog); |
|
|
312 | abort (); |
|
|
313 | } |
|
|
314 | |
|
|
315 | uloc.clear (); |
235 | } |
316 | } |
236 | |
317 | |
237 | void sl_func0::begin () const |
318 | void sl_func0::begin () const |
238 | { |
319 | { |
239 | cur->append_string (name_par); |
320 | cur->append_string (name_par); |
… | |
… | |
313 | } |
394 | } |
314 | |
395 | |
315 | |
396 | |
316 | void debdebdebdebug ()//D |
397 | void debdebdebdebug ()//D |
317 | { |
398 | { |
|
|
399 | return; |
|
|
400 | |
318 | vertex_shader vsh; |
401 | vertex_shader vsh; |
319 | |
402 | |
320 | vsh->start (); |
403 | vsh->start (); |
321 | |
404 | |
322 | temp_4f lightpos; |
405 | temp_4f lightpos; |
323 | temp_3f wpos; |
406 | temp_4f wpos; |
324 | |
407 | |
325 | lightpos = vec4 (10, -10, 0, 1); |
408 | lightpos = vec4 (0, 10, 0, 1); |
326 | wpos = xyz (gl.model_view_matrix * vin.vertex); |
409 | wpos = gl.model_view_matrix * vin.vertex; |
327 | vout.position = gl.model_view_matrix_inverse_transpose * vin.vertex; |
410 | vout.position = vin.vertex * gl.model_view_matrix_inverse; |
328 | vout.tex_coord[0] = vin.tex_coord[0]; |
411 | vout.tex_coord[0] = vin.tex_coord[0]; |
329 | vout.tex_coord[1] = normalize (lightpos - wpos); |
412 | vout.tex_coord[1] = normalize (lightpos - wpos); |
330 | vout.tex_coord[2] = normalize (wpos); |
413 | vout.tex_coord[2] = normalize (wpos); |
331 | vout.tex_coord[3] = normalize (xyz (gl.model_view_matrix_inverse_transpose) * vin.normal); |
414 | //vout.tex_coord[3] = normalize (xyz (gl.model_view_matrix_inverse_transpose) * vin.normal); |
332 | vout.tex_coord[4] = normalize (xyz (gl.projection_matrix_inverse_transpose) - wpos); |
415 | //vout.tex_coord[4] = normalize (xyz (gl.projection_matrix_inverse_transpose) - wpos); |
333 | |
416 | |
334 | vsh->end (); |
417 | vsh->end (); |
335 | //vsh->compile (); |
418 | vsh->compile (); |
336 | |
419 | |
337 | fragment_shader fsh; |
420 | fragment_shader fsh; |
338 | |
421 | |
339 | fsh->start (); |
422 | fsh->start (); |
340 | |
423 | |