|
|
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 0 |
|
|
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_destroy () const |
15 | { |
20 | { |
16 | if (!--refcnt) |
21 | if (!--refcnt) |
17 | delete this; // quite a bit of code... |
22 | delete this; // quite a bit of code... |
18 | } |
23 | } |
19 | |
24 | |
… | |
… | |
35 | const char str_sampler_3d_rect [] = "sampler3DRect"; |
40 | const char str_sampler_3d_rect [] = "sampler3DRect"; |
36 | const char str_sampler_cube [] = "samplerCube"; |
41 | const char str_sampler_cube [] = "samplerCube"; |
37 | |
42 | |
38 | unsigned int var_i::next_id = 0; |
43 | unsigned int var_i::next_id = 0; |
39 | |
44 | |
40 | var_i::var_i (const char *typestr) |
45 | var_i::var_i (const char *domainstr, const char *typestr) |
41 | : typestr (typestr) |
46 | : domainstr (domainstr), typestr (typestr) |
42 | { |
47 | { |
43 | //param = cgCreateParameter (cg_context, cgtype); |
|
|
44 | } |
48 | } |
45 | |
49 | |
46 | var_i::~var_i () |
50 | var_i::~var_i () |
47 | { |
51 | { |
48 | //cgDestroyParameter (param); |
|
|
49 | } |
52 | } |
50 | |
53 | |
|
|
54 | #if 0 |
51 | stream_i::stream_i (const char *strtype) |
55 | stream_i::stream_i (const char *strtype) |
52 | : var_i (strtype) |
56 | : var_i (strtype) |
53 | { |
57 | { |
54 | sprintf (name, "V%lx_%d", ((long)this >> 4) & 0xfff, ++next_id); |
58 | sprintf (name, "V%d", ++next_id); |
55 | } |
59 | } |
|
|
60 | #endif |
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 (0, strtype) |
59 | { |
64 | { |
60 | sprintf (name, "T%lx_%d", ((long)this >> 4) & 0xfff, ++next_id); |
65 | sprintf (name, "T%d", ++next_id); |
|
|
66 | } |
|
|
67 | |
|
|
68 | varying_i::varying_i (const char *strtype) |
|
|
69 | : var_i ("varying", strtype) |
|
|
70 | { |
|
|
71 | sprintf (name, "V%d", ++next_id); |
61 | } |
72 | } |
62 | |
73 | |
63 | uniform_i::uniform_i (const char *strtype) |
74 | uniform_i::uniform_i (const char *strtype) |
64 | : var_i (strtype), dirty (true) |
75 | : var_i ("uniform", strtype) |
65 | { |
76 | { |
66 | sprintf (name, "U%lx_%d", ((long)this >> 4) & 0xfff, ++next_id); |
77 | sprintf (name, "U%d", ++next_id); |
67 | } |
|
|
68 | |
|
|
69 | fragment_string_i::~fragment_string_i () |
|
|
70 | { |
|
|
71 | free (str); |
|
|
72 | } |
78 | } |
73 | |
79 | |
74 | //////////////////////////////////////////////////////////////////////////// |
80 | //////////////////////////////////////////////////////////////////////////// |
75 | |
81 | |
76 | void var_i::build_decl (ostringstream &b) |
82 | void var_i::operator ()() const |
77 | { |
83 | { |
78 | b << typestr << ' ' << name; |
84 | shader_builder::cur->code << name; |
79 | } |
|
|
80 | |
85 | |
81 | void uniform_i::build_decl (ostringstream &b) |
86 | if (shader_builder::cur->first (this)) |
82 | { |
87 | { |
83 | b << "uniform " << typestr << ' ' << name; |
88 | if (domainstr) |
84 | } |
89 | shader_builder::cur->global << domainstr << ' ' << typestr << ' ' << name << ";\n"; |
85 | |
90 | else |
86 | void stream_i::build_decl (ostringstream &b) |
91 | shader_builder::cur->local << " " << typestr << ' ' << name << ";\n"; |
87 | { |
92 | } |
88 | b << typestr << ' ' << name; |
|
|
89 | } |
93 | } |
90 | |
94 | |
91 | //////////////////////////////////////////////////////////////////////////// |
95 | //////////////////////////////////////////////////////////////////////////// |
92 | |
96 | |
93 | void glvar_i::build (shader_builder &b) |
97 | int texture_units::unit_count = 8; |
94 | { |
98 | int texture_units::units[8] = { 7, 6, 5, 4, 3, 2, 1, 0 }; |
95 | b << name; |
|
|
96 | } |
|
|
97 | |
|
|
98 | void var_i::build (shader_builder &b) |
|
|
99 | { |
|
|
100 | b << name; |
|
|
101 | } |
|
|
102 | |
|
|
103 | void uniform_i::build (shader_builder &b) |
|
|
104 | { |
|
|
105 | var_i::build (b); |
|
|
106 | |
|
|
107 | if (find (b.refs.begin (), b.refs.end (), uniform (*this)) == b.refs.end ()) |
|
|
108 | b.refs.push_back (*this); |
|
|
109 | } |
|
|
110 | |
|
|
111 | void stream_i::build (shader_builder &b) |
|
|
112 | { |
|
|
113 | var_i::build (b); |
|
|
114 | |
|
|
115 | if (find (b.streams.begin (), b.streams.end (), var (*this)) == b.streams.end ()) |
|
|
116 | b.streams.push_back (*this); |
|
|
117 | } |
|
|
118 | |
|
|
119 | void temporary_i::build (shader_builder &b) |
|
|
120 | { |
|
|
121 | var_i::build (b); |
|
|
122 | |
|
|
123 | if (find (b.temps.begin (), b.temps.end (), var (*this)) == b.temps.end ()) |
|
|
124 | b.temps.push_back (*this); |
|
|
125 | } |
|
|
126 | |
|
|
127 | void fragment_vector_i::build (shader_builder &b) |
|
|
128 | { |
|
|
129 | for (vector<fragment>::iterator i = begin (); i != end (); i++) |
|
|
130 | (*i)->build (b); |
|
|
131 | } |
|
|
132 | |
|
|
133 | void fragment_const_string_i::build (shader_builder &b) |
|
|
134 | { |
|
|
135 | b << str; |
|
|
136 | } |
|
|
137 | |
|
|
138 | void fragment_string_i::build (shader_builder &b) |
|
|
139 | { |
|
|
140 | b << str; |
|
|
141 | } |
|
|
142 | |
|
|
143 | #if 0 |
|
|
144 | void statement_i::build (shader_builder &b) |
|
|
145 | { |
|
|
146 | b << " "; |
|
|
147 | fragment_vector_i::build (b); |
|
|
148 | b << ";\n"; |
|
|
149 | } |
|
|
150 | #endif |
|
|
151 | |
|
|
152 | //////////////////////////////////////////////////////////////////////////// |
|
|
153 | |
99 | |
154 | shader_object_i *cur = 0; |
100 | shader_object_i *cur = 0; |
155 | |
101 | |
156 | shader_object_i::shader_object_i (GLenum type) |
102 | shader_object_i::shader_object_i (GLenum type) |
157 | : type (type) |
103 | : type (type) |
158 | { |
104 | { |
159 | id = glCreateShaderObjectARB (type); |
105 | id = glCreateShaderObjectARB (type); |
|
|
106 | assert (id); |
160 | } |
107 | } |
161 | |
108 | |
162 | shader_object_i::~shader_object_i () |
109 | shader_object_i::~shader_object_i () |
163 | { |
110 | { |
164 | glDeleteObjectARB (id); |
111 | glDeleteObjectARB (id); |
165 | } |
112 | } |
166 | |
113 | |
167 | void shader_object_i::start () |
114 | static string linify (const string &s) |
|
|
115 | { |
|
|
116 | ostringstream o; |
|
|
117 | |
|
|
118 | int b = 0, e; |
|
|
119 | int l = 1; |
|
|
120 | do { |
|
|
121 | o << setw (3) << l << ": "; |
|
|
122 | e = s.find ('\n', b); |
|
|
123 | if (e == string::npos) |
|
|
124 | e = s.size (); |
|
|
125 | |
|
|
126 | o << s.substr (b, e - b + 1); |
|
|
127 | b = e + 1; |
|
|
128 | l++; |
|
|
129 | } while (b < s.size ()); |
|
|
130 | |
|
|
131 | return o.str (); |
|
|
132 | } |
|
|
133 | |
|
|
134 | //////////////////////////////////////////////////////////////////////////// |
168 | { |
135 | |
|
|
136 | GLint uniform_i::location () |
|
|
137 | { |
|
|
138 | assert (program_object_i::cur); |
|
|
139 | |
|
|
140 | GLint &rid = program_object_i::cur->uloc[this]; |
|
|
141 | |
|
|
142 | if (!rid) |
|
|
143 | rid = glGetUniformLocationARB (program_object_i::cur->id, name); |
|
|
144 | |
|
|
145 | return rid; |
|
|
146 | } |
|
|
147 | |
|
|
148 | program_object_i *program_object_i::cur; |
|
|
149 | |
|
|
150 | program_object_i::program_object_i () |
|
|
151 | { |
|
|
152 | id = glCreateProgramObjectARB (); |
|
|
153 | assert (id); |
|
|
154 | } |
|
|
155 | |
|
|
156 | program_object_i::~program_object_i () |
|
|
157 | { |
|
|
158 | glDeleteProgramsARB (1, &id); |
|
|
159 | } |
|
|
160 | |
|
|
161 | void program_object_i::link () |
|
|
162 | { |
|
|
163 | glLinkProgramARB (id); |
|
|
164 | |
|
|
165 | GLint linked; |
|
|
166 | glGetObjectParameterivARB (id, GL_OBJECT_LINK_STATUS_ARB, &linked); |
|
|
167 | |
|
|
168 | if (!linked) |
|
|
169 | { |
|
|
170 | char infolog[8192]; |
|
|
171 | glGetInfoLogARB (id, 8192, NULL, infolog); |
|
|
172 | printf ("LINK-INFOLOG<%s>\n", infolog); |
|
|
173 | abort (); |
|
|
174 | } |
|
|
175 | |
169 | clear (); |
176 | uloc.clear (); |
|
|
177 | } |
|
|
178 | |
|
|
179 | void program_object_i::enable () |
|
|
180 | { |
|
|
181 | if (this != cur) |
|
|
182 | { |
|
|
183 | glUseProgramObjectARB (id); |
|
|
184 | //TODO: set samplers here? |
170 | cur = this; |
185 | cur = this; |
|
|
186 | } |
171 | } |
187 | } |
172 | |
188 | |
173 | void shader_object_i::stop () |
189 | void program_object_i::disable () |
174 | { |
190 | { |
|
|
191 | //TODO: clear samplers here? |
|
|
192 | glUseProgramObjectARB (0); |
175 | cur = 0; |
193 | cur = 0; |
176 | } |
194 | } |
177 | |
195 | |
178 | string shader_object_i::source () |
196 | static map<string, program_object> progcache; |
|
|
197 | |
|
|
198 | program_object get_program (const string &vsh, const string &fsh) |
179 | { |
199 | { |
|
|
200 | string idx = vsh + "\0" + fsh; |
|
|
201 | |
|
|
202 | map<string, program_object>::iterator i = progcache.find (idx); |
|
|
203 | |
|
|
204 | if (i != progcache.end ()) |
|
|
205 | return i->second; |
|
|
206 | |
|
|
207 | program_object p; |
|
|
208 | |
|
|
209 | if (vsh.size ()) |
|
|
210 | { |
|
|
211 | vertex_shader sh; |
|
|
212 | p->attach (sh); |
|
|
213 | sh->compile (vsh); |
|
|
214 | } |
|
|
215 | |
|
|
216 | if (fsh.size ()) |
|
|
217 | { |
|
|
218 | fragment_shader sh; |
|
|
219 | p->attach (sh); |
|
|
220 | sh->compile (fsh); |
|
|
221 | } |
|
|
222 | |
|
|
223 | p->link (); |
|
|
224 | |
|
|
225 | progcache.insert (pair<string, program_object> (idx, p)); |
|
|
226 | |
|
|
227 | return p; |
|
|
228 | } |
|
|
229 | |
|
|
230 | const sl_expr< sl_string<60> > sl_convert< ::vec2 >::convert (const ::vec2 &v) |
|
|
231 | { |
|
|
232 | sl_string<60> s; |
|
|
233 | sprintf (s.str, "vec2 (%e, %e)", v.x, v.y); |
|
|
234 | return s; |
|
|
235 | } |
|
|
236 | |
|
|
237 | const sl_expr< sl_string<80> > sl_convert< ::vec3 >::convert (const ::vec3 &v) |
|
|
238 | { |
|
|
239 | sl_string<80> s; |
|
|
240 | sprintf (s.str, "vec3 (%e, %e, %e)", v.x, v.y, v.z); |
|
|
241 | return s; |
|
|
242 | } |
|
|
243 | |
|
|
244 | const sl_expr< sl_string<100> > sl_convert< ::vec4 >::convert (const ::vec4 &v) |
|
|
245 | { |
|
|
246 | sl_string<100> s; |
|
|
247 | sprintf (s.str, "vec4 (%e, %e, %e, %e)", v.x, v.y, v.z, v.w); |
|
|
248 | return s; |
|
|
249 | } |
|
|
250 | |
|
|
251 | shader_builder *shader_builder::cur = 0; |
|
|
252 | |
|
|
253 | bool shader_builder::first (const void *p) |
|
|
254 | { |
|
|
255 | if (seen.find (p) == seen.end ()) |
|
|
256 | { |
|
|
257 | seen.insert (p); |
|
|
258 | return true; |
|
|
259 | } |
|
|
260 | |
|
|
261 | return false; |
|
|
262 | } |
|
|
263 | |
|
|
264 | void shader_builder::start () |
|
|
265 | { |
180 | shader_builder b; |
266 | cur = new shader_builder; |
181 | build (b); |
267 | cur->code << scientific; |
|
|
268 | } |
|
|
269 | |
|
|
270 | string shader_builder::stop () |
|
|
271 | { |
182 | ostringstream os; |
272 | ostringstream os; |
183 | |
273 | |
184 | for (vector<uniform>::iterator i = b.refs.begin (); i != b.refs.end (); i++) |
274 | os << cur->global.str () |
185 | { |
|
|
186 | (*i)->build_decl (os); |
|
|
187 | os << ";\n"; |
|
|
188 | } |
|
|
189 | |
|
|
190 | // not neccessary right now, as GLSL is rich on predefinitions |
|
|
191 | for (vector<var>::iterator i = b.streams.begin (); i != b.streams.end (); i++) |
|
|
192 | { |
|
|
193 | os << "attribute "; |
|
|
194 | (*i)->build_decl (os); |
|
|
195 | os << ";\n"; |
|
|
196 | } |
|
|
197 | |
|
|
198 | os << "\nvoid main (void)\n{\n"; |
275 | << "\nvoid main (void)\n" |
199 | |
|
|
200 | for (vector<var>::iterator i = b.temps.begin (); i != b.temps.end (); i++) |
|
|
201 | { |
|
|
202 | os << " "; |
|
|
203 | (*i)->build_decl (os); |
|
|
204 | os << ";\n"; |
276 | << "{\n" |
205 | } |
277 | << cur->local.str () |
206 | |
|
|
207 | os << "\n"; |
278 | << "\n" |
208 | os << b.source.str (); |
279 | << cur->code.str () |
209 | os << "}\n"; |
280 | << "}\n"; |
|
|
281 | |
|
|
282 | delete cur; |
|
|
283 | cur = 0; |
210 | |
284 | |
211 | return os.str (); |
285 | return os.str (); |
212 | } |
286 | } |
213 | |
287 | |
214 | void shader_object_i::compile () |
288 | void shader_object_i::compile (const string &source) |
215 | { |
289 | { |
216 | string src = source (); |
|
|
217 | const char *sptr = src.data (); |
290 | const char *sptr = source.data (); |
218 | const int slen = src.size (); |
291 | const int slen = source.size (); |
219 | |
292 | |
220 | printf ("SOURCE<%s>\n", src.c_str ()); |
293 | printf ("%s\n", linify (source).c_str ()); |
221 | glShaderSourceARB (id, 1, &sptr, &slen); |
294 | glShaderSourceARB (id, 1, &sptr, &slen); |
222 | glCompileShaderARB (id); |
295 | glCompileShaderARB (id); |
223 | |
296 | |
224 | GLint compiled; |
297 | GLint compiled; |
225 | glGetObjectParameterivARB (id, GL_OBJECT_COMPILE_STATUS_ARB, &compiled); |
298 | glGetObjectParameterivARB (id, GL_OBJECT_COMPILE_STATUS_ARB, &compiled); |
226 | |
299 | |
227 | if (!compiled) |
300 | if (!compiled) |
228 | { |
301 | { |
229 | char infolog[8192]; |
302 | char infolog[8192]; |
230 | glGetInfoLogARB (id, 8192, NULL, infolog); |
303 | glGetInfoLogARB (id, 8192, NULL, infolog); |
231 | printf ("SOURCE<%s>\n", src.c_str ()); |
304 | printf ("%s\n", linify (source).c_str ()); |
232 | printf ("INFOLOG<%s>\n", infolog); |
305 | printf ("%s\n", infolog); |
233 | abort (); |
306 | abort (); |
234 | } |
307 | } |
235 | } |
308 | } |
236 | |
309 | |
237 | void sl_func0::begin () const |
|
|
238 | { |
|
|
239 | cur->append_string (name_par); |
|
|
240 | } |
|
|
241 | |
|
|
242 | void sl_func0::comma () const |
|
|
243 | { |
|
|
244 | cur->append (str_comma); |
|
|
245 | } |
|
|
246 | |
|
|
247 | void sl_func0::end () const |
|
|
248 | { |
|
|
249 | str_rpar (); |
|
|
250 | } |
|
|
251 | |
|
|
252 | void sl_float::operator ()() const |
|
|
253 | { |
|
|
254 | char s[20]; |
|
|
255 | sprintf (s, "%g", c); |
|
|
256 | cur->append_string (s); |
|
|
257 | } |
|
|
258 | |
|
|
259 | const sl_convert< ::vec2 >::T sl_convert< ::vec2 >::convert (const ::vec2 &v) |
|
|
260 | { |
|
|
261 | sl_string<60> s; |
|
|
262 | sprintf (s.str, "vec2 (%g, %g)", v.x, v.y); |
|
|
263 | return s; |
|
|
264 | } |
|
|
265 | |
|
|
266 | const sl_convert< ::vec3 >::T sl_convert< ::vec3 >::convert (const ::vec3 &v) |
|
|
267 | { |
|
|
268 | sl_string<80> s; |
|
|
269 | sprintf (s.str, "vec3 (%g, %g, %g)", v.x, v.y, v.z); |
|
|
270 | return s; |
|
|
271 | } |
|
|
272 | |
|
|
273 | const sl_convert< ::vec4 >::T sl_convert< ::vec4 >::convert (const ::vec4 &v) |
|
|
274 | { |
|
|
275 | sl_string<100> s; |
|
|
276 | sprintf (s.str, "vec4 (%g, %g, %g, %g)", v.x, v.y, v.z, v.w); |
|
|
277 | return s; |
|
|
278 | } |
|
|
279 | |
|
|
280 | const fragment_const_string str_2sp (" "); |
|
|
281 | const fragment_const_string str_equal (" = "); |
|
|
282 | const fragment_const_string str_comma (", "); |
|
|
283 | const fragment_const_string str_endl (";\n"); |
|
|
284 | |
|
|
285 | const sl_append_const_string str_plus (" + "); |
|
|
286 | const sl_append_const_string str_minus (" - "); |
|
|
287 | const sl_append_const_string str_mul (" * "); |
|
|
288 | const sl_append_const_string str_div (" / "); |
|
|
289 | const sl_append_const_string str_mod (" % "); |
|
|
290 | |
|
|
291 | const sl_append_const_string str_lpar ("("); |
|
|
292 | const sl_append_const_string str_rpar (")"); |
|
|
293 | |
|
|
294 | void swizzle_mask (sl_string<7> &s, int mask) |
|
|
295 | { |
|
|
296 | static const char channel[4] = { 'x', 'y', 'z', 'w' }; |
|
|
297 | |
|
|
298 | char *str = s.str; |
|
|
299 | |
|
|
300 | *str++ = ')'; |
|
|
301 | *str++ = '.'; |
|
|
302 | |
|
|
303 | while (mask) |
|
|
304 | { |
|
|
305 | int c = mask % 5; |
|
|
306 | mask /= 5; |
|
|
307 | |
|
|
308 | if (c) |
|
|
309 | *str++ = channel[c - 1]; |
|
|
310 | } |
|
|
311 | |
|
|
312 | *str++ = 0; |
|
|
313 | } |
|
|
314 | |
|
|
315 | |
|
|
316 | void debdebdebdebug ()//D |
|
|
317 | { |
|
|
318 | vertex_shader vsh; |
|
|
319 | |
|
|
320 | vsh->start (); |
|
|
321 | |
|
|
322 | temp_4f lightpos; |
|
|
323 | temp_3f wpos; |
|
|
324 | |
|
|
325 | lightpos = vec4 (10, -10, 0, 1); |
|
|
326 | wpos = xyz (gl.model_view_matrix * vin.vertex); |
|
|
327 | vout.position = gl.model_view_matrix_inverse_transpose * vin.vertex; |
|
|
328 | vout.tex_coord[0] = vin.tex_coord[0]; |
|
|
329 | vout.tex_coord[1] = normalize (lightpos - wpos); |
|
|
330 | vout.tex_coord[2] = normalize (wpos); |
|
|
331 | 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); |
|
|
333 | |
|
|
334 | vsh->end (); |
|
|
335 | vsh->compile (); |
|
|
336 | |
|
|
337 | fragment_shader fsh; |
|
|
338 | |
|
|
339 | fsh->start (); |
|
|
340 | |
|
|
341 | temp_1f spec_expon; |
|
|
342 | spec_expon = 200; |
|
|
343 | |
|
|
344 | fsh->end (); |
|
|
345 | fsh->compile (); |
|
|
346 | |
|
|
347 | abort (); |
|
|
348 | } |
|
|
349 | |
|
|
350 | } |
310 | } |
351 | |
311 | |