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 |
|
|
const char str_float [] = "float"; |
15 |
|
|
const char str_vec2 [] = "vec2"; |
16 |
|
|
const char str_vec3 [] = "vec3"; |
17 |
|
|
const char str_vec4 [] = "vec4"; |
18 |
|
|
const char str_mat2 [] = "mat2"; |
19 |
|
|
const char str_mat3 [] = "mat3"; |
20 |
|
|
const char str_mat4 [] = "mat4"; |
21 |
|
|
|
22 |
|
|
const char str_sampler_1d [] = "sampler1D"; |
23 |
|
|
const char str_sampler_1d_shadow [] = "sampler1DShadow"; |
24 |
|
|
const char str_sampler_2d [] = "sampler2D"; |
25 |
|
|
const char str_sampler_2d_shadow [] = "sampler2DShadow"; |
26 |
|
|
const char str_sampler_2d_rect [] = "sampler2DRect"; |
27 |
|
|
const char str_sampler_2d_rect_shadow [] = "sampler2DRectShadow"; |
28 |
|
|
const char str_sampler_3d [] = "sampler3D"; |
29 |
|
|
const char str_sampler_3d_rect [] = "sampler3DRect"; |
30 |
|
|
const char str_sampler_cube [] = "samplerCube"; |
31 |
|
|
|
32 |
|
|
unsigned int var_i::next_id = 0; |
33 |
|
|
|
34 |
|
|
var_i::var_i (const char *typestr) |
35 |
|
|
: typestr (typestr) |
36 |
|
|
{ |
37 |
|
|
//param = cgCreateParameter (cg_context, cgtype); |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
var_i::~var_i () |
41 |
|
|
{ |
42 |
|
|
//cgDestroyParameter (param); |
43 |
|
|
} |
44 |
|
|
|
45 |
root |
1.4 |
stream_i::stream_i (const char *strtype) |
46 |
|
|
: var_i (strtype) |
47 |
|
|
{ |
48 |
|
|
sprintf (name, "V%lx_%d", ((long)this >> 4) & 0xfff, ++next_id); |
49 |
|
|
} |
50 |
|
|
|
51 |
root |
1.1 |
temporary_i::temporary_i (const char *strtype) |
52 |
|
|
: var_i (strtype) |
53 |
|
|
{ |
54 |
|
|
sprintf (name, "T%lx_%d", ((long)this >> 4) & 0xfff, ++next_id); |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
uniform_i::uniform_i (const char *strtype) |
58 |
|
|
: var_i (strtype), dirty (true) |
59 |
|
|
{ |
60 |
|
|
sprintf (name, "U%lx_%d", ((long)this >> 4) & 0xfff, ++next_id); |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
fragment_string_i::~fragment_string_i () |
64 |
|
|
{ |
65 |
|
|
free (str); |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
//////////////////////////////////////////////////////////////////////////// |
69 |
|
|
|
70 |
|
|
void var_i::build_decl (ostringstream &b) |
71 |
|
|
{ |
72 |
|
|
b << typestr << ' ' << name; |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
void uniform_i::build_decl (ostringstream &b) |
76 |
|
|
{ |
77 |
|
|
b << "uniform " << typestr << ' ' << name; |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
void stream_i::build_decl (ostringstream &b) |
81 |
|
|
{ |
82 |
|
|
b << typestr << ' ' << name; |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
//////////////////////////////////////////////////////////////////////////// |
86 |
|
|
|
87 |
|
|
void glvar_i::build (shader_builder &b) |
88 |
|
|
{ |
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 |
|
|
|
148 |
|
|
shader_object_i *cur = 0; |
149 |
|
|
|
150 |
|
|
shader_object_i::shader_object_i (GLenum type) |
151 |
|
|
: type (type) |
152 |
|
|
{ |
153 |
|
|
id = glCreateShaderObjectARB (type); |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
shader_object_i::~shader_object_i () |
157 |
|
|
{ |
158 |
|
|
glDeleteObjectARB (id); |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
void shader_object_i::start () |
162 |
|
|
{ |
163 |
|
|
clear (); |
164 |
|
|
cur = this; |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
void shader_object_i::stop () |
168 |
|
|
{ |
169 |
|
|
cur = 0; |
170 |
|
|
} |
171 |
|
|
|
172 |
|
|
string shader_object_i::source () |
173 |
|
|
{ |
174 |
|
|
shader_builder b; |
175 |
|
|
build (b); |
176 |
|
|
ostringstream os; |
177 |
|
|
|
178 |
|
|
for (vector<uniform>::iterator i = b.refs.begin (); i != b.refs.end (); i++) |
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"; |
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"; |
199 |
|
|
} |
200 |
|
|
|
201 |
|
|
os << "\n"; |
202 |
|
|
os << b.source.str (); |
203 |
|
|
os << "}\n"; |
204 |
|
|
|
205 |
|
|
return os.str (); |
206 |
|
|
} |
207 |
|
|
|
208 |
|
|
void shader_object_i::compile () |
209 |
|
|
{ |
210 |
|
|
string src = source (); |
211 |
|
|
const char *sptr = src.data (); |
212 |
|
|
const int slen = src.size (); |
213 |
|
|
|
214 |
|
|
printf ("SOURCE<%s>\n", src.c_str ()); |
215 |
|
|
abort (); |
216 |
|
|
glShaderSourceARB (id, 1, &sptr, &slen); |
217 |
|
|
glCompileShaderARB (id); |
218 |
|
|
|
219 |
|
|
GLint compiled; |
220 |
|
|
glGetObjectParameterivARB (id, GL_OBJECT_COMPILE_STATUS_ARB, &compiled); |
221 |
|
|
|
222 |
|
|
if (!compiled) |
223 |
|
|
{ |
224 |
|
|
char infolog[8192]; |
225 |
|
|
glGetInfoLogARB (id, 8192, NULL, infolog); |
226 |
|
|
printf ("SOURCE<%s>\n", src.c_str ()); |
227 |
|
|
printf ("INFOLOG<%s>\n", infolog); |
228 |
|
|
abort (); |
229 |
|
|
} |
230 |
|
|
} |
231 |
|
|
|
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 |
|
|
} |
260 |
|
|
|