|
|
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 |
17 | } |
|
|
18 | |
|
|
19 | void refcounted::refcnt_destroy () const |
|
|
20 | { |
|
|
21 | if (!--refcnt) |
|
|
22 | delete this; // quite a bit of code... |
12 | } |
23 | } |
13 | |
24 | |
14 | const char str_float [] = "float"; |
25 | const char str_float [] = "float"; |
15 | const char str_vec2 [] = "vec2"; |
26 | const char str_vec2 [] = "vec2"; |
16 | const char str_vec3 [] = "vec3"; |
27 | const char str_vec3 [] = "vec3"; |
… | |
… | |
29 | const char str_sampler_3d_rect [] = "sampler3DRect"; |
40 | const char str_sampler_3d_rect [] = "sampler3DRect"; |
30 | const char str_sampler_cube [] = "samplerCube"; |
41 | const char str_sampler_cube [] = "samplerCube"; |
31 | |
42 | |
32 | unsigned int var_i::next_id = 0; |
43 | unsigned int var_i::next_id = 0; |
33 | |
44 | |
34 | var_i::var_i (const char *typestr) |
45 | var_i::var_i (const char *domainstr, const char *typestr) |
35 | : typestr (typestr) |
46 | : domainstr (domainstr), typestr (typestr) |
36 | { |
47 | { |
37 | //param = cgCreateParameter (cg_context, cgtype); |
|
|
38 | } |
48 | } |
39 | |
49 | |
40 | var_i::~var_i () |
50 | var_i::~var_i () |
41 | { |
51 | { |
42 | //cgDestroyParameter (param); |
|
|
43 | } |
52 | } |
44 | |
53 | |
|
|
54 | #if 0 |
45 | stream_i::stream_i (const char *strtype) |
55 | stream_i::stream_i (const char *strtype) |
46 | : var_i (strtype) |
56 | : var_i (strtype) |
47 | { |
57 | { |
48 | sprintf (name, "V%lx_%d", ((long)this >> 4) & 0xfff, ++next_id); |
58 | sprintf (name, "V%d", ++next_id); |
49 | } |
59 | } |
|
|
60 | #endif |
50 | |
61 | |
51 | temporary_i::temporary_i (const char *strtype) |
62 | temporary_i::temporary_i (const char *strtype) |
52 | : var_i (strtype) |
63 | : var_i (0, strtype) |
53 | { |
64 | { |
54 | 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); |
55 | } |
72 | } |
56 | |
73 | |
57 | uniform_i::uniform_i (const char *strtype) |
74 | uniform_i::uniform_i (const char *strtype) |
58 | : var_i (strtype), dirty (true) |
75 | : var_i ("uniform", strtype) |
59 | { |
76 | { |
60 | sprintf (name, "U%lx_%d", ((long)this >> 4) & 0xfff, ++next_id); |
77 | sprintf (name, "U%d", ++next_id); |
61 | } |
|
|
62 | |
|
|
63 | fragment_string_i::~fragment_string_i () |
|
|
64 | { |
|
|
65 | free (str); |
|
|
66 | } |
78 | } |
67 | |
79 | |
68 | //////////////////////////////////////////////////////////////////////////// |
80 | //////////////////////////////////////////////////////////////////////////// |
69 | |
81 | |
70 | void var_i::build_decl (ostringstream &b) |
82 | void var_i::operator ()() const |
71 | { |
83 | { |
72 | b << typestr << ' ' << name; |
84 | shader_builder::cur->code << name; |
73 | } |
|
|
74 | |
85 | |
75 | void uniform_i::build_decl (ostringstream &b) |
86 | if (shader_builder::cur->first (this)) |
76 | { |
87 | { |
77 | b << "uniform " << typestr << ' ' << name; |
88 | if (domainstr) |
78 | } |
89 | shader_builder::cur->global << domainstr << ' ' << typestr << ' ' << name << ";\n"; |
79 | |
90 | else |
80 | void stream_i::build_decl (ostringstream &b) |
91 | shader_builder::cur->local << " " << typestr << ' ' << name << ";\n"; |
81 | { |
92 | } |
82 | b << typestr << ' ' << name; |
|
|
83 | } |
93 | } |
84 | |
94 | |
85 | //////////////////////////////////////////////////////////////////////////// |
95 | //////////////////////////////////////////////////////////////////////////// |
86 | |
96 | |
87 | void glvar_i::build (shader_builder &b) |
97 | int texture_units::unit_count = 8; |
88 | { |
98 | int texture_units::units[8] = { 7, 6, 5, 4, 3, 2, 1, 0 }; |
89 | b << name; |
|
|
90 | } |
|
|
91 | |
|
|
92 | void var_i::build (shader_builder &b) |
|
|
93 | { |
|
|
94 | b << name; |
|
|
95 | } |
|
|
96 | |
|
|
97 | void uniform_i::build (shader_builder &b) |
|
|
98 | { |
|
|
99 | var_i::build (b); |
|
|
100 | |
|
|
101 | if (find (b.refs.begin (), b.refs.end (), uniform (*this)) == b.refs.end ()) |
|
|
102 | b.refs.push_back (*this); |
|
|
103 | } |
|
|
104 | |
|
|
105 | void stream_i::build (shader_builder &b) |
|
|
106 | { |
|
|
107 | var_i::build (b); |
|
|
108 | |
|
|
109 | if (find (b.streams.begin (), b.streams.end (), var (*this)) == b.streams.end ()) |
|
|
110 | b.streams.push_back (*this); |
|
|
111 | } |
|
|
112 | |
|
|
113 | void temporary_i::build (shader_builder &b) |
|
|
114 | { |
|
|
115 | var_i::build (b); |
|
|
116 | |
|
|
117 | if (find (b.temps.begin (), b.temps.end (), var (*this)) == b.temps.end ()) |
|
|
118 | b.temps.push_back (*this); |
|
|
119 | } |
|
|
120 | |
|
|
121 | void fragment_vector_i::build (shader_builder &b) |
|
|
122 | { |
|
|
123 | for (vector<fragment>::iterator i = begin (); i != end (); i++) |
|
|
124 | (*i)->build (b); |
|
|
125 | } |
|
|
126 | |
|
|
127 | void fragment_const_string_i::build (shader_builder &b) |
|
|
128 | { |
|
|
129 | b << str; |
|
|
130 | } |
|
|
131 | |
|
|
132 | void fragment_string_i::build (shader_builder &b) |
|
|
133 | { |
|
|
134 | b << str; |
|
|
135 | } |
|
|
136 | |
|
|
137 | #if 0 |
|
|
138 | void statement_i::build (shader_builder &b) |
|
|
139 | { |
|
|
140 | b << " "; |
|
|
141 | fragment_vector_i::build (b); |
|
|
142 | b << ";\n"; |
|
|
143 | } |
|
|
144 | #endif |
|
|
145 | |
|
|
146 | //////////////////////////////////////////////////////////////////////////// |
|
|
147 | |
99 | |
148 | shader_object_i *cur = 0; |
100 | shader_object_i *cur = 0; |
149 | |
101 | |
150 | shader_object_i::shader_object_i (GLenum type) |
102 | shader_object_i::shader_object_i (GLenum type) |
151 | : type (type) |
103 | : type (type) |
152 | { |
104 | { |
153 | id = glCreateShaderObjectARB (type); |
105 | id = glCreateShaderObjectARB (type); |
|
|
106 | assert (id); |
154 | } |
107 | } |
155 | |
108 | |
156 | shader_object_i::~shader_object_i () |
109 | shader_object_i::~shader_object_i () |
157 | { |
110 | { |
158 | glDeleteObjectARB (id); |
111 | glDeleteObjectARB (id); |
159 | } |
112 | } |
160 | |
113 | |
161 | 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 | //////////////////////////////////////////////////////////////////////////// |
162 | { |
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 | |
163 | 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? |
164 | cur = this; |
185 | cur = this; |
|
|
186 | } |
165 | } |
187 | } |
166 | |
188 | |
167 | void shader_object_i::stop () |
189 | void program_object_i::disable () |
168 | { |
190 | { |
|
|
191 | //TODO: clear samplers here? |
|
|
192 | glUseProgramObjectARB (0); |
169 | cur = 0; |
193 | cur = 0; |
170 | } |
194 | } |
171 | |
195 | |
172 | string shader_object_i::source () |
196 | static map<string, program_object> progcache; |
|
|
197 | |
|
|
198 | program_object get_program (const string &vsh, const string &fsh) |
173 | { |
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 | { |
174 | shader_builder b; |
266 | cur = new shader_builder; |
175 | build (b); |
267 | cur->code << scientific; |
|
|
268 | } |
|
|
269 | |
|
|
270 | string shader_builder::stop () |
|
|
271 | { |
176 | ostringstream os; |
272 | ostringstream os; |
177 | |
273 | |
178 | for (vector<uniform>::iterator i = b.refs.begin (); i != b.refs.end (); i++) |
274 | os << cur->global.str () |
179 | { |
|
|
180 | (*i)->build_decl (os); |
|
|
181 | os << ";\n"; |
|
|
182 | } |
|
|
183 | |
|
|
184 | // not neccessary right now, as GLSL is rich on predefinitions |
|
|
185 | for (vector<var>::iterator i = b.streams.begin (); i != b.streams.end (); i++) |
|
|
186 | { |
|
|
187 | os << "attribute "; |
|
|
188 | (*i)->build_decl (os); |
|
|
189 | os << ";\n"; |
|
|
190 | } |
|
|
191 | |
|
|
192 | os << "\nvoid main (void)\n{\n"; |
275 | << "\nvoid main (void)\n" |
193 | |
|
|
194 | for (vector<var>::iterator i = b.temps.begin (); i != b.temps.end (); i++) |
|
|
195 | { |
|
|
196 | os << " "; |
|
|
197 | (*i)->build_decl (os); |
|
|
198 | os << ";\n"; |
276 | << "{\n" |
199 | } |
277 | << cur->local.str () |
200 | |
|
|
201 | os << "\n"; |
278 | << "\n" |
202 | os << b.source.str (); |
279 | << cur->code.str () |
203 | os << "}\n"; |
280 | << "}\n"; |
|
|
281 | |
|
|
282 | delete cur; |
|
|
283 | cur = 0; |
204 | |
284 | |
205 | return os.str (); |
285 | return os.str (); |
206 | } |
286 | } |
207 | |
287 | |
208 | void shader_object_i::compile () |
288 | void shader_object_i::compile (const string &source) |
209 | { |
289 | { |
210 | string src = source (); |
|
|
211 | const char *sptr = src.data (); |
290 | const char *sptr = source.data (); |
212 | const int slen = src.size (); |
291 | const int slen = source.size (); |
213 | |
292 | |
214 | printf ("SOURCE<%s>\n", src.c_str ()); |
293 | printf ("%s\n", linify (source).c_str ()); |
215 | abort (); |
|
|
216 | glShaderSourceARB (id, 1, &sptr, &slen); |
294 | glShaderSourceARB (id, 1, &sptr, &slen); |
217 | glCompileShaderARB (id); |
295 | glCompileShaderARB (id); |
218 | |
296 | |
219 | GLint compiled; |
297 | GLint compiled; |
220 | glGetObjectParameterivARB (id, GL_OBJECT_COMPILE_STATUS_ARB, &compiled); |
298 | glGetObjectParameterivARB (id, GL_OBJECT_COMPILE_STATUS_ARB, &compiled); |
221 | |
299 | |
222 | if (!compiled) |
300 | if (!compiled) |
223 | { |
301 | { |
224 | char infolog[8192]; |
302 | char infolog[8192]; |
225 | glGetInfoLogARB (id, 8192, NULL, infolog); |
303 | glGetInfoLogARB (id, 8192, NULL, infolog); |
226 | printf ("SOURCE<%s>\n", src.c_str ()); |
304 | printf ("%s\n", linify (source).c_str ()); |
227 | printf ("INFOLOG<%s>\n", infolog); |
305 | printf ("%s\n", infolog); |
228 | abort (); |
306 | abort (); |
229 | } |
307 | } |
230 | } |
308 | } |
231 | |
309 | |
232 | const sl_append_const_string str_plus (" + "); |
|
|
233 | const sl_append_const_string str_minus (" - "); |
|
|
234 | const sl_append_const_string str_mul (" * "); |
|
|
235 | const sl_append_const_string str_div (" / "); |
|
|
236 | |
|
|
237 | const sl_append_const_string str_rpar (")"); |
|
|
238 | |
|
|
239 | void debdebdebdebug ()//D |
|
|
240 | { |
|
|
241 | vertex_shader vsh; |
|
|
242 | fragment_shader fsh; |
|
|
243 | temp_4f t1, t2; |
|
|
244 | sampler_2d s2d (1); |
|
|
245 | |
|
|
246 | vsh->start (); |
|
|
247 | |
|
|
248 | temp_4f lightpos; |
|
|
249 | temp_3f wpos; |
|
|
250 | |
|
|
251 | lightpos = vec4 (10, -10, 0, 1); |
|
|
252 | wpos = gl.model_view_matrix_inverse_transpose * vin.normal; |
|
|
253 | vout.position = wpos; |
|
|
254 | |
|
|
255 | vsh->end (); |
|
|
256 | vsh->compile (); |
|
|
257 | } |
|
|
258 | |
|
|
259 | } |
310 | } |
260 | |
311 | |