1 |
root |
1.12 |
#include <cassert> |
2 |
|
|
|
3 |
root |
1.1 |
#include "shader.h" |
4 |
|
|
#include "shader_vars.h" |
5 |
|
|
|
6 |
root |
1.17 |
#include <sstream> |
7 |
|
|
#include <iomanip> |
8 |
|
|
|
9 |
root |
1.18 |
namespace shader { |
10 |
root |
1.17 |
|
11 |
root |
1.18 |
using namespace std; |
12 |
root |
1.1 |
|
13 |
|
|
refcounted::~refcounted () |
14 |
|
|
{ |
15 |
|
|
if (refcnt) |
16 |
|
|
abort (); |
17 |
|
|
} |
18 |
|
|
|
19 |
root |
1.26 |
void refcounted::refcnt_destroy () const |
20 |
root |
1.6 |
{ |
21 |
|
|
if (!--refcnt) |
22 |
|
|
delete this; // quite a bit of code... |
23 |
|
|
} |
24 |
|
|
|
25 |
root |
1.1 |
const char str_float [] = "float"; |
26 |
|
|
const char str_vec2 [] = "vec2"; |
27 |
|
|
const char str_vec3 [] = "vec3"; |
28 |
|
|
const char str_vec4 [] = "vec4"; |
29 |
|
|
const char str_mat2 [] = "mat2"; |
30 |
|
|
const char str_mat3 [] = "mat3"; |
31 |
|
|
const char str_mat4 [] = "mat4"; |
32 |
|
|
|
33 |
|
|
const char str_sampler_1d [] = "sampler1D"; |
34 |
|
|
const char str_sampler_1d_shadow [] = "sampler1DShadow"; |
35 |
|
|
const char str_sampler_2d [] = "sampler2D"; |
36 |
|
|
const char str_sampler_2d_shadow [] = "sampler2DShadow"; |
37 |
|
|
const char str_sampler_2d_rect [] = "sampler2DRect"; |
38 |
|
|
const char str_sampler_2d_rect_shadow [] = "sampler2DRectShadow"; |
39 |
|
|
const char str_sampler_3d [] = "sampler3D"; |
40 |
|
|
const char str_sampler_3d_rect [] = "sampler3DRect"; |
41 |
|
|
const char str_sampler_cube [] = "samplerCube"; |
42 |
|
|
|
43 |
|
|
unsigned int var_i::next_id = 0; |
44 |
|
|
|
45 |
root |
1.26 |
var_i::var_i (const char *domainstr, const char *typestr) |
46 |
|
|
: domainstr (domainstr), typestr (typestr) |
47 |
root |
1.1 |
{ |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
var_i::~var_i () |
51 |
|
|
{ |
52 |
|
|
} |
53 |
|
|
|
54 |
root |
1.24 |
#if 0 |
55 |
root |
1.4 |
stream_i::stream_i (const char *strtype) |
56 |
|
|
: var_i (strtype) |
57 |
|
|
{ |
58 |
root |
1.19 |
sprintf (name, "V%d", ++next_id); |
59 |
root |
1.4 |
} |
60 |
root |
1.24 |
#endif |
61 |
root |
1.4 |
|
62 |
root |
1.1 |
temporary_i::temporary_i (const char *strtype) |
63 |
root |
1.26 |
: var_i (0, strtype) |
64 |
root |
1.1 |
{ |
65 |
root |
1.19 |
sprintf (name, "T%d", ++next_id); |
66 |
root |
1.1 |
} |
67 |
|
|
|
68 |
root |
1.24 |
varying_i::varying_i (const char *strtype) |
69 |
root |
1.26 |
: var_i ("varying", strtype) |
70 |
root |
1.24 |
{ |
71 |
|
|
sprintf (name, "V%d", ++next_id); |
72 |
|
|
} |
73 |
|
|
|
74 |
root |
1.1 |
uniform_i::uniform_i (const char *strtype) |
75 |
root |
1.26 |
: var_i ("uniform", strtype) |
76 |
root |
1.1 |
{ |
77 |
root |
1.19 |
sprintf (name, "U%d", ++next_id); |
78 |
root |
1.1 |
} |
79 |
|
|
|
80 |
|
|
//////////////////////////////////////////////////////////////////////////// |
81 |
|
|
|
82 |
root |
1.26 |
void var_i::operator ()() const |
83 |
root |
1.1 |
{ |
84 |
root |
1.26 |
compile::cur->code << name; |
85 |
root |
1.1 |
|
86 |
root |
1.26 |
if (compile::cur->first (this)) |
87 |
|
|
{ |
88 |
|
|
if (domainstr) |
89 |
|
|
compile::cur->global << domainstr << ' ' << typestr << ' ' << name << ";\n"; |
90 |
|
|
else |
91 |
|
|
compile::cur->local << " " << typestr << ' ' << name << ";\n"; |
92 |
|
|
} |
93 |
root |
1.1 |
} |
94 |
|
|
|
95 |
|
|
//////////////////////////////////////////////////////////////////////////// |
96 |
|
|
|
97 |
root |
1.20 |
int texture_units::unit_count = 8; |
98 |
root |
1.21 |
int texture_units::units[8] = { 7, 6, 5, 4, 3, 2, 1, 0 }; |
99 |
root |
1.20 |
|
100 |
root |
1.1 |
shader_object_i *cur = 0; |
101 |
|
|
|
102 |
|
|
shader_object_i::shader_object_i (GLenum type) |
103 |
|
|
: type (type) |
104 |
|
|
{ |
105 |
|
|
id = glCreateShaderObjectARB (type); |
106 |
root |
1.12 |
assert (id); |
107 |
root |
1.1 |
} |
108 |
|
|
|
109 |
|
|
shader_object_i::~shader_object_i () |
110 |
|
|
{ |
111 |
|
|
glDeleteObjectARB (id); |
112 |
|
|
} |
113 |
|
|
|
114 |
root |
1.17 |
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 |
root |
1.26 |
void shader_object_i::start () |
135 |
|
|
{ |
136 |
|
|
compile::cur = new compile::shader_builder; |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
void shader_object_i::stop () |
140 |
root |
1.1 |
{ |
141 |
root |
1.26 |
compile::shader_builder &b = *compile::cur; |
142 |
|
|
|
143 |
|
|
ostringstream os; |
144 |
|
|
|
145 |
|
|
os << b.global.str () |
146 |
|
|
<< "\nvoid main (void)\n" |
147 |
|
|
<< "{\n" |
148 |
|
|
<< b.local.str () |
149 |
|
|
<< "\n" |
150 |
|
|
<< b.code.str () |
151 |
|
|
<< "}\n"; |
152 |
|
|
|
153 |
|
|
delete cur; |
154 |
|
|
cur = 0; |
155 |
|
|
|
156 |
|
|
string src = os.str (); |
157 |
root |
1.1 |
const char *sptr = src.data (); |
158 |
|
|
const int slen = src.size (); |
159 |
|
|
|
160 |
root |
1.17 |
printf ("%s\n", linify (src).c_str ()); |
161 |
root |
1.1 |
glShaderSourceARB (id, 1, &sptr, &slen); |
162 |
|
|
glCompileShaderARB (id); |
163 |
|
|
|
164 |
|
|
GLint compiled; |
165 |
|
|
glGetObjectParameterivARB (id, GL_OBJECT_COMPILE_STATUS_ARB, &compiled); |
166 |
|
|
|
167 |
|
|
if (!compiled) |
168 |
|
|
{ |
169 |
|
|
char infolog[8192]; |
170 |
|
|
glGetInfoLogARB (id, 8192, NULL, infolog); |
171 |
root |
1.17 |
printf ("%s\n", linify (src).c_str ()); |
172 |
|
|
printf ("%s\n", infolog); |
173 |
root |
1.12 |
abort (); |
174 |
|
|
} |
175 |
|
|
} |
176 |
|
|
|
177 |
root |
1.19 |
//////////////////////////////////////////////////////////////////////////// |
178 |
|
|
|
179 |
|
|
program_object *program_object::cur = 0; |
180 |
|
|
|
181 |
|
|
GLint uniform_i::location () |
182 |
|
|
{ |
183 |
|
|
assert (program_object::cur); |
184 |
|
|
|
185 |
|
|
GLint &rid = program_object::cur->uloc[this]; |
186 |
|
|
|
187 |
|
|
if (!rid) |
188 |
|
|
rid = glGetUniformLocationARB (program_object::cur->id, name); |
189 |
|
|
|
190 |
|
|
return rid; |
191 |
|
|
} |
192 |
|
|
|
193 |
root |
1.12 |
program_object::program_object () |
194 |
|
|
{ |
195 |
|
|
id = glCreateProgramObjectARB (); |
196 |
|
|
assert (id); |
197 |
root |
1.15 |
|
198 |
|
|
glAttachObjectARB (id, vsh->id); |
199 |
|
|
glAttachObjectARB (id, fsh->id); |
200 |
root |
1.12 |
} |
201 |
|
|
|
202 |
|
|
program_object::~program_object () |
203 |
|
|
{ |
204 |
|
|
glDeleteProgramsARB (1, &id); |
205 |
|
|
} |
206 |
|
|
|
207 |
|
|
void program_object::link () |
208 |
|
|
{ |
209 |
|
|
glLinkProgramARB (id); |
210 |
|
|
|
211 |
|
|
GLint linked; |
212 |
|
|
glGetObjectParameterivARB (id, GL_OBJECT_LINK_STATUS_ARB, &linked); |
213 |
|
|
|
214 |
|
|
if (!linked) |
215 |
|
|
{ |
216 |
|
|
char infolog[8192]; |
217 |
|
|
glGetInfoLogARB (id, 8192, NULL, infolog); |
218 |
|
|
printf ("LINK-INFOLOG<%s>\n", infolog); |
219 |
root |
1.1 |
abort (); |
220 |
|
|
} |
221 |
root |
1.19 |
|
222 |
|
|
uloc.clear (); |
223 |
root |
1.22 |
} |
224 |
|
|
|
225 |
|
|
void program_object::enable () |
226 |
|
|
{ |
227 |
|
|
glUseProgramObjectARB (id); |
228 |
|
|
//TODO: set samplers here |
229 |
|
|
cur = this; |
230 |
|
|
} |
231 |
|
|
|
232 |
|
|
void program_object::disable () |
233 |
|
|
{ |
234 |
|
|
//TODO: clear samplers here |
235 |
|
|
glUseProgramObjectARB (0); |
236 |
|
|
cur = 0; |
237 |
root |
1.1 |
} |
238 |
|
|
|
239 |
root |
1.26 |
const sl_expr< sl_string<60> > sl_convert< ::vec2 >::convert (const ::vec2 &v) |
240 |
root |
1.6 |
{ |
241 |
|
|
sl_string<60> s; |
242 |
|
|
sprintf (s.str, "vec2 (%g, %g)", v.x, v.y); |
243 |
|
|
return s; |
244 |
|
|
} |
245 |
|
|
|
246 |
root |
1.26 |
const sl_expr< sl_string<80> > sl_convert< ::vec3 >::convert (const ::vec3 &v) |
247 |
root |
1.6 |
{ |
248 |
|
|
sl_string<80> s; |
249 |
|
|
sprintf (s.str, "vec3 (%g, %g, %g)", v.x, v.y, v.z); |
250 |
|
|
return s; |
251 |
|
|
} |
252 |
|
|
|
253 |
root |
1.26 |
const sl_expr< sl_string<100> > sl_convert< ::vec4 >::convert (const ::vec4 &v) |
254 |
root |
1.6 |
{ |
255 |
|
|
sl_string<100> s; |
256 |
|
|
sprintf (s.str, "vec4 (%g, %g, %g, %g)", v.x, v.y, v.z, v.w); |
257 |
|
|
return s; |
258 |
|
|
} |
259 |
|
|
|
260 |
root |
1.23 |
namespace compile { |
261 |
root |
1.5 |
|
262 |
root |
1.26 |
shader_builder *cur = 0; |
263 |
|
|
|
264 |
|
|
bool shader_builder::first (const void *p) |
265 |
|
|
{ |
266 |
|
|
if (seen.find (p) == seen.end ()) |
267 |
|
|
{ |
268 |
|
|
seen.insert (p); |
269 |
|
|
return true; |
270 |
|
|
} |
271 |
|
|
|
272 |
|
|
return false; |
273 |
|
|
} |
274 |
root |
1.5 |
} |
275 |
|
|
|
276 |
root |
1.1 |
void debdebdebdebug ()//D |
277 |
|
|
{ |
278 |
root |
1.20 |
return; |
279 |
|
|
|
280 |
root |
1.23 |
using namespace compile; |
281 |
|
|
|
282 |
root |
1.1 |
vertex_shader vsh; |
283 |
|
|
|
284 |
|
|
vsh->start (); |
285 |
|
|
|
286 |
|
|
temp_4f lightpos; |
287 |
root |
1.18 |
temp_4f wpos; |
288 |
root |
1.1 |
|
289 |
root |
1.13 |
lightpos = vec4 (0, 10, 0, 1); |
290 |
root |
1.23 |
wpos = model_view_matrix * vin.vertex; |
291 |
|
|
vout.position = vin.vertex * model_view_matrix_inverse; |
292 |
root |
1.7 |
vout.tex_coord[0] = vin.tex_coord[0]; |
293 |
root |
1.18 |
vout.tex_coord[1] = normalize (lightpos - wpos); |
294 |
|
|
vout.tex_coord[2] = normalize (wpos); |
295 |
root |
1.23 |
//vout.tex_coord[3] = normalize (xyz (model_view_matrix_inverse_transpose) * vin.normal); |
296 |
|
|
//vout.tex_coord[4] = normalize (xyz (projection_matrix_inverse_transpose) - wpos); |
297 |
root |
1.1 |
|
298 |
root |
1.26 |
vsh->stop (); |
299 |
root |
1.10 |
|
300 |
|
|
fragment_shader fsh; |
301 |
|
|
|
302 |
|
|
fsh->start (); |
303 |
|
|
|
304 |
root |
1.11 |
xyz (fout.frag_color) = noise3 (x (fin.frag_coord) * y (fin.frag_coord)); |
305 |
|
|
|
306 |
root |
1.10 |
temp_1f spec_expon; |
307 |
|
|
spec_expon = 200; |
308 |
|
|
|
309 |
root |
1.26 |
fsh->stop (); |
310 |
root |
1.10 |
|
311 |
root |
1.11 |
//abort (); |
312 |
root |
1.1 |
} |
313 |
|
|
|
314 |
|
|
} |
315 |
|
|
|