|
|
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) |
168 | { |
115 | { |
169 | clear (); |
|
|
170 | cur = this; |
|
|
171 | } |
|
|
172 | |
|
|
173 | void shader_object_i::stop () |
|
|
174 | { |
|
|
175 | cur = 0; |
|
|
176 | } |
|
|
177 | |
|
|
178 | string shader_object_i::source () |
|
|
179 | { |
|
|
180 | shader_builder b; |
|
|
181 | build (b); |
|
|
182 | ostringstream os; |
116 | ostringstream o; |
183 | |
117 | |
184 | for (vector<uniform>::iterator i = b.refs.begin (); i != b.refs.end (); i++) |
118 | int b = 0, e; |
|
|
119 | int l = 1; |
185 | { |
120 | do { |
186 | (*i)->build_decl (os); |
121 | o << setw (3) << l << ": "; |
187 | os << ";\n"; |
122 | e = s.find ('\n', b); |
188 | } |
123 | if (e == string::npos) |
|
|
124 | e = s.size (); |
189 | |
125 | |
190 | // not neccessary right now, as GLSL is rich on predefinitions |
126 | o << s.substr (b, e - b + 1); |
191 | for (vector<var>::iterator i = b.streams.begin (); i != b.streams.end (); i++) |
127 | b = e + 1; |
192 | { |
128 | l++; |
193 | os << "attribute "; |
129 | } while (b < s.size ()); |
194 | (*i)->build_decl (os); |
|
|
195 | os << ";\n"; |
|
|
196 | } |
|
|
197 | |
130 | |
198 | os << "\nvoid main (void)\n{\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"; |
|
|
205 | } |
|
|
206 | |
|
|
207 | os << "\n"; |
|
|
208 | os << b.source.str (); |
|
|
209 | os << "}\n"; |
|
|
210 | |
|
|
211 | return os.str (); |
131 | return o.str (); |
|
|
132 | } |
|
|
133 | |
|
|
134 | //////////////////////////////////////////////////////////////////////////// |
212 | } |
135 | |
213 | |
136 | GLint uniform_i::location () |
214 | void shader_object_i::compile () |
|
|
215 | { |
137 | { |
216 | string src = source (); |
138 | assert (program_object_i::cur); |
217 | const char *sptr = src.data (); |
|
|
218 | const int slen = src.size (); |
|
|
219 | |
139 | |
220 | printf ("SOURCE<%s>\n", src.c_str ()); |
140 | GLint &rid = program_object_i::cur->uloc[this]; |
221 | abort (); |
|
|
222 | glShaderSourceARB (id, 1, &sptr, &slen); |
|
|
223 | glCompileShaderARB (id); |
|
|
224 | |
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 | glAttachObjectARB (id, vsh->id); |
|
|
156 | glAttachObjectARB (id, fsh->id); |
|
|
157 | } |
|
|
158 | |
|
|
159 | program_object_i::~program_object_i () |
|
|
160 | { |
|
|
161 | glDeleteProgramsARB (1, &id); |
|
|
162 | } |
|
|
163 | |
|
|
164 | void program_object_i::link () |
|
|
165 | { |
|
|
166 | glLinkProgramARB (id); |
|
|
167 | |
225 | GLint compiled; |
168 | GLint linked; |
226 | glGetObjectParameterivARB (id, GL_OBJECT_COMPILE_STATUS_ARB, &compiled); |
169 | glGetObjectParameterivARB (id, GL_OBJECT_LINK_STATUS_ARB, &linked); |
227 | |
170 | |
228 | if (!compiled) |
171 | if (!linked) |
229 | { |
172 | { |
230 | char infolog[8192]; |
173 | char infolog[8192]; |
231 | glGetInfoLogARB (id, 8192, NULL, infolog); |
174 | glGetInfoLogARB (id, 8192, NULL, infolog); |
232 | printf ("SOURCE<%s>\n", src.c_str ()); |
|
|
233 | printf ("INFOLOG<%s>\n", infolog); |
175 | printf ("LINK-INFOLOG<%s>\n", infolog); |
234 | abort (); |
176 | abort (); |
235 | } |
177 | } |
236 | } |
|
|
237 | |
178 | |
238 | void sl_func0::begin () const |
179 | uloc.clear (); |
239 | { |
|
|
240 | cur->append_string (name_par); |
|
|
241 | } |
180 | } |
242 | |
181 | |
243 | void sl_func0::end () const |
182 | void program_object_i::enable () |
244 | { |
183 | { |
245 | str_rpar (); |
184 | if (this != cur) |
|
|
185 | { |
|
|
186 | glUseProgramObjectARB (id); |
|
|
187 | //TODO: set samplers here? |
|
|
188 | cur = this; |
|
|
189 | } |
246 | } |
190 | } |
247 | |
191 | |
248 | void sl_float::operator ()() const |
192 | void program_object_i::disable () |
249 | { |
193 | { |
250 | char s[20]; |
194 | //TODO: clear samplers here? |
251 | sprintf (s, "%g", c); |
195 | glUseProgramObjectARB (0); |
252 | cur->append_string (s); |
196 | cur = 0; |
253 | } |
197 | } |
254 | |
198 | |
255 | const sl_convert<vec2>::T sl_convert<vec2>::convert (const vec2 &v) |
199 | static map<string, program_object> progcache; |
|
|
200 | |
|
|
201 | program_object get_program (const string &vsh, const string &fsh) |
|
|
202 | { |
|
|
203 | string idx = vsh + "\0" + fsh; |
|
|
204 | |
|
|
205 | map<string, program_object>::iterator i = progcache.find (idx); |
|
|
206 | |
|
|
207 | if (i != progcache.end ()) |
|
|
208 | return i->second; |
|
|
209 | |
|
|
210 | program_object p; |
|
|
211 | |
|
|
212 | p->vsh->compile (vsh); |
|
|
213 | p->fsh->compile (fsh); |
|
|
214 | p->link (); |
|
|
215 | |
|
|
216 | progcache.insert (pair<string, program_object> (idx, p)); |
|
|
217 | |
|
|
218 | return p; |
|
|
219 | } |
|
|
220 | |
|
|
221 | const sl_expr< sl_string<60> > sl_convert< ::vec2 >::convert (const ::vec2 &v) |
256 | { |
222 | { |
257 | sl_string<60> s; |
223 | sl_string<60> s; |
258 | sprintf (s.str, "vec2 (%g, %g)", v.x, v.y); |
224 | sprintf (s.str, "vec2 (%g, %g)", v.x, v.y); |
259 | return s; |
225 | return s; |
260 | } |
226 | } |
261 | |
227 | |
262 | const sl_convert<vec3>::T sl_convert<vec3>::convert (const vec3 &v) |
228 | const sl_expr< sl_string<80> > sl_convert< ::vec3 >::convert (const ::vec3 &v) |
263 | { |
229 | { |
264 | sl_string<80> s; |
230 | sl_string<80> s; |
265 | sprintf (s.str, "vec3 (%g, %g, %g)", v.x, v.y, v.z); |
231 | sprintf (s.str, "vec3 (%g, %g, %g)", v.x, v.y, v.z); |
266 | return s; |
232 | return s; |
267 | } |
233 | } |
268 | |
234 | |
269 | const sl_convert<vec4>::T sl_convert<vec4>::convert (const vec4 &v) |
235 | const sl_expr< sl_string<100> > sl_convert< ::vec4 >::convert (const ::vec4 &v) |
270 | { |
236 | { |
271 | sl_string<100> s; |
237 | sl_string<100> s; |
272 | sprintf (s.str, "vec4 (%g, %g, %g, %g)", v.x, v.y, v.z, v.w); |
238 | sprintf (s.str, "vec4 (%g, %g, %g, %g)", v.x, v.y, v.z, v.w); |
273 | return s; |
239 | return s; |
274 | } |
240 | } |
275 | |
241 | |
276 | const fragment_const_string str_2sp (" "); |
242 | shader_builder *shader_builder::cur = 0; |
277 | const fragment_const_string str_equal (" = "); |
|
|
278 | const fragment_const_string str_endl (";\n"); |
|
|
279 | |
243 | |
280 | const sl_append_const_string str_plus (" + "); |
244 | bool shader_builder::first (const void *p) |
281 | const sl_append_const_string str_minus (" - "); |
|
|
282 | const sl_append_const_string str_mul (" * "); |
|
|
283 | const sl_append_const_string str_div (" / "); |
|
|
284 | |
|
|
285 | const sl_append_const_string str_lpar ("("); |
|
|
286 | const sl_append_const_string str_rpar (")"); |
|
|
287 | |
|
|
288 | void swizzle_mask (sl_string<7> &s, int mask) |
|
|
289 | { |
245 | { |
290 | static const char channel[4] = { 'x', 'y', 'z', 'w' }; |
246 | if (seen.find (p) == seen.end ()) |
291 | |
|
|
292 | char *str = s.str; |
|
|
293 | |
|
|
294 | *str++ = ')'; |
|
|
295 | *str++ = '.'; |
|
|
296 | |
|
|
297 | while (mask) |
|
|
298 | { |
|
|
299 | int c = mask % 5; |
|
|
300 | mask /= 5; |
|
|
301 | |
|
|
302 | if (c) |
|
|
303 | *str++ = channel[c - 1]; |
|
|
304 | } |
247 | { |
|
|
248 | seen.insert (p); |
|
|
249 | return true; |
|
|
250 | } |
305 | |
251 | |
306 | *str++ = 0; |
252 | return false; |
307 | } |
253 | } |
308 | |
254 | |
309 | |
255 | void shader_builder::start () |
310 | template<typename T> |
|
|
311 | inline const sl_expr< sl_concat3< sl_append_const_string, |
|
|
312 | typename sl_convert<T>::T, |
|
|
313 | sl_string<7> |
|
|
314 | > > |
|
|
315 | swizzlx (const T &t, int a, int b = 0, int c = 0, int d = 0) |
|
|
316 | { |
256 | { |
317 | sl_string<7> s; |
257 | cur = new shader_builder; |
318 | swizzle_mask (s, ((d * 5 + c) * 5 + b) * 5 + a); |
|
|
319 | return concat (str_lpar, sl_convert<T>::convert (t), s); |
|
|
320 | } |
258 | } |
321 | |
259 | |
322 | void debdebdebdebug ()//D |
260 | string shader_builder::stop () |
323 | { |
261 | { |
324 | vertex_shader vsh; |
262 | ostringstream os; |
325 | fragment_shader fsh; |
|
|
326 | temp_4f t1, t2; |
|
|
327 | sampler_2d s2d (1); |
|
|
328 | |
263 | |
329 | vsh->start (); |
264 | os << cur->global.str () |
|
|
265 | << "\nvoid main (void)\n" |
|
|
266 | << "{\n" |
|
|
267 | << cur->local.str () |
|
|
268 | << "\n" |
|
|
269 | << cur->code.str () |
|
|
270 | << "}\n"; |
330 | |
271 | |
331 | temp_4f lightpos; |
272 | delete cur; |
332 | temp_3f wpos; |
273 | cur = 0; |
333 | |
274 | |
334 | lightpos = vec4 (10, -10, 0, 1); |
275 | return os.str (); |
335 | wpos = xyz (gl.model_view_matrix * vin.vertex); |
276 | } |
336 | vout.position = gl.model_view_matrix_inverse_transpose * vin.vertex; |
|
|
337 | vout.tex_coord[0] = vin.tex_coord[0]; |
|
|
338 | //vout.tex_coord[1] = normalize (lightpos - wpos); |
|
|
339 | |
277 | |
340 | z(wpos) = 5; |
278 | void shader_object_i::compile (const string &source) |
|
|
279 | { |
|
|
280 | const char *sptr = source.data (); |
|
|
281 | const int slen = source.size (); |
341 | |
282 | |
342 | vsh->end (); |
283 | printf ("%s\n", linify (source).c_str ()); |
343 | vsh->compile (); |
284 | glShaderSourceARB (id, 1, &sptr, &slen); |
|
|
285 | glCompileShaderARB (id); |
|
|
286 | |
|
|
287 | GLint compiled; |
|
|
288 | glGetObjectParameterivARB (id, GL_OBJECT_COMPILE_STATUS_ARB, &compiled); |
|
|
289 | |
|
|
290 | if (!compiled) |
|
|
291 | { |
|
|
292 | char infolog[8192]; |
|
|
293 | glGetInfoLogARB (id, 8192, NULL, infolog); |
|
|
294 | printf ("%s\n", linify (source).c_str ()); |
|
|
295 | printf ("%s\n", infolog); |
|
|
296 | abort (); |
|
|
297 | } |
344 | } |
298 | } |
345 | |
299 | |
346 | } |
300 | } |
347 | |
301 | |