1 |
root |
1.1 |
#include "shader.h" |
2 |
|
|
#include "shader_vars.h" |
3 |
|
|
|
4 |
|
|
namespace shader { |
5 |
|
|
|
6 |
|
|
refcounted::~refcounted () |
7 |
|
|
{ |
8 |
|
|
#if 0 |
9 |
|
|
if (refcnt) |
10 |
|
|
abort (); |
11 |
|
|
#endif |
12 |
|
|
} |
13 |
|
|
|
14 |
root |
1.6 |
void refcounted::refcnt_dec () const |
15 |
|
|
{ |
16 |
|
|
if (!--refcnt) |
17 |
|
|
delete this; // quite a bit of code... |
18 |
|
|
} |
19 |
|
|
|
20 |
root |
1.1 |
const char str_float [] = "float"; |
21 |
|
|
const char str_vec2 [] = "vec2"; |
22 |
|
|
const char str_vec3 [] = "vec3"; |
23 |
|
|
const char str_vec4 [] = "vec4"; |
24 |
|
|
const char str_mat2 [] = "mat2"; |
25 |
|
|
const char str_mat3 [] = "mat3"; |
26 |
|
|
const char str_mat4 [] = "mat4"; |
27 |
|
|
|
28 |
|
|
const char str_sampler_1d [] = "sampler1D"; |
29 |
|
|
const char str_sampler_1d_shadow [] = "sampler1DShadow"; |
30 |
|
|
const char str_sampler_2d [] = "sampler2D"; |
31 |
|
|
const char str_sampler_2d_shadow [] = "sampler2DShadow"; |
32 |
|
|
const char str_sampler_2d_rect [] = "sampler2DRect"; |
33 |
|
|
const char str_sampler_2d_rect_shadow [] = "sampler2DRectShadow"; |
34 |
|
|
const char str_sampler_3d [] = "sampler3D"; |
35 |
|
|
const char str_sampler_3d_rect [] = "sampler3DRect"; |
36 |
|
|
const char str_sampler_cube [] = "samplerCube"; |
37 |
|
|
|
38 |
|
|
unsigned int var_i::next_id = 0; |
39 |
|
|
|
40 |
|
|
var_i::var_i (const char *typestr) |
41 |
|
|
: typestr (typestr) |
42 |
|
|
{ |
43 |
|
|
//param = cgCreateParameter (cg_context, cgtype); |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
var_i::~var_i () |
47 |
|
|
{ |
48 |
|
|
//cgDestroyParameter (param); |
49 |
|
|
} |
50 |
|
|
|
51 |
root |
1.4 |
stream_i::stream_i (const char *strtype) |
52 |
|
|
: var_i (strtype) |
53 |
|
|
{ |
54 |
|
|
sprintf (name, "V%lx_%d", ((long)this >> 4) & 0xfff, ++next_id); |
55 |
|
|
} |
56 |
|
|
|
57 |
root |
1.1 |
temporary_i::temporary_i (const char *strtype) |
58 |
|
|
: var_i (strtype) |
59 |
|
|
{ |
60 |
|
|
sprintf (name, "T%lx_%d", ((long)this >> 4) & 0xfff, ++next_id); |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
uniform_i::uniform_i (const char *strtype) |
64 |
|
|
: var_i (strtype), dirty (true) |
65 |
|
|
{ |
66 |
|
|
sprintf (name, "U%lx_%d", ((long)this >> 4) & 0xfff, ++next_id); |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
fragment_string_i::~fragment_string_i () |
70 |
|
|
{ |
71 |
|
|
free (str); |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
//////////////////////////////////////////////////////////////////////////// |
75 |
|
|
|
76 |
|
|
void var_i::build_decl (ostringstream &b) |
77 |
|
|
{ |
78 |
|
|
b << typestr << ' ' << name; |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
void uniform_i::build_decl (ostringstream &b) |
82 |
|
|
{ |
83 |
|
|
b << "uniform " << typestr << ' ' << name; |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
void stream_i::build_decl (ostringstream &b) |
87 |
|
|
{ |
88 |
|
|
b << typestr << ' ' << name; |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
//////////////////////////////////////////////////////////////////////////// |
92 |
|
|
|
93 |
|
|
void glvar_i::build (shader_builder &b) |
94 |
|
|
{ |
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 |
|
|
|
154 |
|
|
shader_object_i *cur = 0; |
155 |
|
|
|
156 |
|
|
shader_object_i::shader_object_i (GLenum type) |
157 |
|
|
: type (type) |
158 |
|
|
{ |
159 |
|
|
id = glCreateShaderObjectARB (type); |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
shader_object_i::~shader_object_i () |
163 |
|
|
{ |
164 |
|
|
glDeleteObjectARB (id); |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
void shader_object_i::start () |
168 |
|
|
{ |
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; |
183 |
|
|
|
184 |
|
|
for (vector<uniform>::iterator i = b.refs.begin (); i != b.refs.end (); i++) |
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"; |
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 (); |
212 |
|
|
} |
213 |
|
|
|
214 |
|
|
void shader_object_i::compile () |
215 |
|
|
{ |
216 |
|
|
string src = source (); |
217 |
|
|
const char *sptr = src.data (); |
218 |
|
|
const int slen = src.size (); |
219 |
|
|
|
220 |
|
|
printf ("SOURCE<%s>\n", src.c_str ()); |
221 |
|
|
abort (); |
222 |
|
|
glShaderSourceARB (id, 1, &sptr, &slen); |
223 |
|
|
glCompileShaderARB (id); |
224 |
|
|
|
225 |
|
|
GLint compiled; |
226 |
|
|
glGetObjectParameterivARB (id, GL_OBJECT_COMPILE_STATUS_ARB, &compiled); |
227 |
|
|
|
228 |
|
|
if (!compiled) |
229 |
|
|
{ |
230 |
|
|
char infolog[8192]; |
231 |
|
|
glGetInfoLogARB (id, 8192, NULL, infolog); |
232 |
|
|
printf ("SOURCE<%s>\n", src.c_str ()); |
233 |
|
|
printf ("INFOLOG<%s>\n", infolog); |
234 |
|
|
abort (); |
235 |
|
|
} |
236 |
|
|
} |
237 |
|
|
|
238 |
root |
1.7 |
void sl_func0::begin () const |
239 |
|
|
{ |
240 |
|
|
cur->append_string (name_par); |
241 |
|
|
} |
242 |
|
|
|
243 |
root |
1.8 |
void sl_func0::comma () const |
244 |
|
|
{ |
245 |
|
|
cur->append (str_comma); |
246 |
|
|
} |
247 |
|
|
|
248 |
root |
1.7 |
void sl_func0::end () const |
249 |
|
|
{ |
250 |
|
|
str_rpar (); |
251 |
|
|
} |
252 |
|
|
|
253 |
root |
1.6 |
void sl_float::operator ()() const |
254 |
|
|
{ |
255 |
|
|
char s[20]; |
256 |
|
|
sprintf (s, "%g", c); |
257 |
|
|
cur->append_string (s); |
258 |
|
|
} |
259 |
|
|
|
260 |
root |
1.9 |
const sl_convert< ::vec2 >::T sl_convert< ::vec2 >::convert (const ::vec2 &v) |
261 |
root |
1.6 |
{ |
262 |
|
|
sl_string<60> s; |
263 |
|
|
sprintf (s.str, "vec2 (%g, %g)", v.x, v.y); |
264 |
|
|
return s; |
265 |
|
|
} |
266 |
|
|
|
267 |
root |
1.9 |
const sl_convert< ::vec3 >::T sl_convert< ::vec3 >::convert (const ::vec3 &v) |
268 |
root |
1.6 |
{ |
269 |
|
|
sl_string<80> s; |
270 |
|
|
sprintf (s.str, "vec3 (%g, %g, %g)", v.x, v.y, v.z); |
271 |
|
|
return s; |
272 |
|
|
} |
273 |
|
|
|
274 |
root |
1.9 |
const sl_convert< ::vec4 >::T sl_convert< ::vec4 >::convert (const ::vec4 &v) |
275 |
root |
1.6 |
{ |
276 |
|
|
sl_string<100> s; |
277 |
|
|
sprintf (s.str, "vec4 (%g, %g, %g, %g)", v.x, v.y, v.z, v.w); |
278 |
|
|
return s; |
279 |
|
|
} |
280 |
|
|
|
281 |
root |
1.5 |
const fragment_const_string str_2sp (" "); |
282 |
|
|
const fragment_const_string str_equal (" = "); |
283 |
root |
1.8 |
const fragment_const_string str_comma (", "); |
284 |
root |
1.5 |
const fragment_const_string str_endl (";\n"); |
285 |
|
|
|
286 |
root |
1.1 |
const sl_append_const_string str_plus (" + "); |
287 |
|
|
const sl_append_const_string str_minus (" - "); |
288 |
|
|
const sl_append_const_string str_mul (" * "); |
289 |
|
|
const sl_append_const_string str_div (" / "); |
290 |
root |
1.9 |
const sl_append_const_string str_mod (" % "); |
291 |
root |
1.1 |
|
292 |
root |
1.5 |
const sl_append_const_string str_lpar ("("); |
293 |
root |
1.1 |
const sl_append_const_string str_rpar (")"); |
294 |
|
|
|
295 |
root |
1.5 |
void swizzle_mask (sl_string<7> &s, int mask) |
296 |
|
|
{ |
297 |
|
|
static const char channel[4] = { 'x', 'y', 'z', 'w' }; |
298 |
|
|
|
299 |
|
|
char *str = s.str; |
300 |
|
|
|
301 |
|
|
*str++ = ')'; |
302 |
|
|
*str++ = '.'; |
303 |
|
|
|
304 |
|
|
while (mask) |
305 |
|
|
{ |
306 |
|
|
int c = mask % 5; |
307 |
|
|
mask /= 5; |
308 |
|
|
|
309 |
|
|
if (c) |
310 |
|
|
*str++ = channel[c - 1]; |
311 |
|
|
} |
312 |
|
|
|
313 |
|
|
*str++ = 0; |
314 |
|
|
} |
315 |
|
|
|
316 |
root |
1.7 |
|
317 |
root |
1.1 |
void debdebdebdebug ()//D |
318 |
|
|
{ |
319 |
|
|
vertex_shader vsh; |
320 |
|
|
|
321 |
|
|
vsh->start (); |
322 |
|
|
|
323 |
|
|
temp_4f lightpos; |
324 |
|
|
temp_3f wpos; |
325 |
|
|
|
326 |
|
|
lightpos = vec4 (10, -10, 0, 1); |
327 |
root |
1.7 |
wpos = xyz (gl.model_view_matrix * vin.vertex); |
328 |
|
|
vout.position = gl.model_view_matrix_inverse_transpose * vin.vertex; |
329 |
|
|
vout.tex_coord[0] = vin.tex_coord[0]; |
330 |
root |
1.8 |
vout.tex_coord[1] = normalize (lightpos - wpos); |
331 |
|
|
vout.tex_coord[2] = normalize (wpos); |
332 |
|
|
vout.tex_coord[3] = normalize (xyz (gl.model_view_matrix_inverse_transpose) * vin.normal); |
333 |
|
|
vout.tex_coord[4] = normalize (xyz (gl.projection_matrix_inverse_transpose) - wpos); |
334 |
root |
1.1 |
|
335 |
|
|
vsh->end (); |
336 |
|
|
vsh->compile (); |
337 |
|
|
} |
338 |
|
|
|
339 |
|
|
} |
340 |
|
|
|