rev |
line source |
nuclear@15
|
1 #include <stdio.h>
|
nuclear@15
|
2 #include <stdlib.h>
|
nuclear@15
|
3 #include <string.h>
|
nuclear@15
|
4 #include <errno.h>
|
nuclear@15
|
5 #include <stdarg.h>
|
nuclear@15
|
6 #include <assert.h>
|
nuclear@15
|
7 #include "opengl.h"
|
nuclear@15
|
8
|
nuclear@15
|
9 #if defined(unix) || defined(__unix__)
|
nuclear@15
|
10 #include <unistd.h>
|
nuclear@15
|
11 #include <sys/stat.h>
|
nuclear@15
|
12 #endif /* unix */
|
nuclear@15
|
13
|
nuclear@15
|
14 #include "sdr.h"
|
nuclear@15
|
15
|
nuclear@15
|
16 static const char *sdrtypestr(unsigned int sdrtype);
|
nuclear@15
|
17
|
nuclear@15
|
18 unsigned int create_vertex_shader(const char *src)
|
nuclear@15
|
19 {
|
nuclear@15
|
20 return create_shader(src, GL_VERTEX_SHADER);
|
nuclear@15
|
21 }
|
nuclear@15
|
22
|
nuclear@15
|
23 unsigned int create_pixel_shader(const char *src)
|
nuclear@15
|
24 {
|
nuclear@15
|
25 return create_shader(src, GL_FRAGMENT_SHADER);
|
nuclear@15
|
26 }
|
nuclear@15
|
27
|
nuclear@15
|
28 unsigned int create_tessctl_shader(const char *src)
|
nuclear@15
|
29 {
|
nuclear@15
|
30 #ifdef GL_TESS_CONTROL_SHADER
|
nuclear@15
|
31 return create_shader(src, GL_TESS_CONTROL_SHADER);
|
nuclear@15
|
32 #else
|
nuclear@15
|
33 return 0;
|
nuclear@15
|
34 #endif
|
nuclear@15
|
35 }
|
nuclear@15
|
36
|
nuclear@15
|
37 unsigned int create_tesseval_shader(const char *src)
|
nuclear@15
|
38 {
|
nuclear@15
|
39 #ifdef GL_TESS_EVALUATION_SHADER
|
nuclear@15
|
40 return create_shader(src, GL_TESS_EVALUATION_SHADER);
|
nuclear@15
|
41 #else
|
nuclear@15
|
42 return 0;
|
nuclear@15
|
43 #endif
|
nuclear@15
|
44 }
|
nuclear@15
|
45
|
nuclear@15
|
46 unsigned int create_geometry_shader(const char *src)
|
nuclear@15
|
47 {
|
nuclear@15
|
48 #ifdef GL_GEOMETRY_SHADER
|
nuclear@15
|
49 return create_shader(src, GL_GEOMETRY_SHADER);
|
nuclear@15
|
50 #else
|
nuclear@15
|
51 return 0;
|
nuclear@15
|
52 #endif
|
nuclear@15
|
53 }
|
nuclear@15
|
54
|
nuclear@15
|
55 unsigned int create_shader(const char *src, unsigned int sdr_type)
|
nuclear@15
|
56 {
|
nuclear@15
|
57 unsigned int sdr;
|
nuclear@15
|
58 int success, info_len;
|
nuclear@15
|
59 char *info_str = 0;
|
nuclear@15
|
60 GLenum err;
|
nuclear@15
|
61
|
nuclear@15
|
62 sdr = glCreateShader(sdr_type);
|
nuclear@15
|
63 assert(glGetError() == GL_NO_ERROR);
|
nuclear@15
|
64 glShaderSource(sdr, 1, &src, 0);
|
nuclear@15
|
65 err = glGetError();
|
nuclear@15
|
66 assert(err == GL_NO_ERROR);
|
nuclear@15
|
67 glCompileShader(sdr);
|
nuclear@15
|
68 assert(glGetError() == GL_NO_ERROR);
|
nuclear@15
|
69
|
nuclear@15
|
70 glGetShaderiv(sdr, GL_COMPILE_STATUS, &success);
|
nuclear@15
|
71 assert(glGetError() == GL_NO_ERROR);
|
nuclear@15
|
72 glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &info_len);
|
nuclear@15
|
73 assert(glGetError() == GL_NO_ERROR);
|
nuclear@15
|
74
|
nuclear@15
|
75 if(info_len) {
|
nuclear@15
|
76 if((info_str = malloc(info_len + 1))) {
|
nuclear@15
|
77 glGetShaderInfoLog(sdr, info_len, 0, info_str);
|
nuclear@15
|
78 assert(glGetError() == GL_NO_ERROR);
|
nuclear@15
|
79 info_str[info_len] = 0;
|
nuclear@15
|
80 }
|
nuclear@15
|
81 }
|
nuclear@15
|
82
|
nuclear@15
|
83 if(success) {
|
nuclear@15
|
84 fprintf(stderr, info_str ? "done: %s\n" : "done\n", info_str);
|
nuclear@15
|
85 } else {
|
nuclear@15
|
86 fprintf(stderr, info_str ? "failed: %s\n" : "failed\n", info_str);
|
nuclear@15
|
87 glDeleteShader(sdr);
|
nuclear@15
|
88 sdr = 0;
|
nuclear@15
|
89 }
|
nuclear@15
|
90
|
nuclear@15
|
91 free(info_str);
|
nuclear@15
|
92 return sdr;
|
nuclear@15
|
93 }
|
nuclear@15
|
94
|
nuclear@15
|
95 void free_shader(unsigned int sdr)
|
nuclear@15
|
96 {
|
nuclear@15
|
97 glDeleteShader(sdr);
|
nuclear@15
|
98 }
|
nuclear@15
|
99
|
nuclear@15
|
100 unsigned int load_vertex_shader(const char *fname)
|
nuclear@15
|
101 {
|
nuclear@15
|
102 return load_shader(fname, GL_VERTEX_SHADER);
|
nuclear@15
|
103 }
|
nuclear@15
|
104
|
nuclear@15
|
105 unsigned int load_pixel_shader(const char *fname)
|
nuclear@15
|
106 {
|
nuclear@15
|
107 return load_shader(fname, GL_FRAGMENT_SHADER);
|
nuclear@15
|
108 }
|
nuclear@15
|
109
|
nuclear@15
|
110 unsigned int load_tessctl_shader(const char *fname)
|
nuclear@15
|
111 {
|
nuclear@15
|
112 #ifdef GL_TESS_CONTROL_SHADER
|
nuclear@15
|
113 return load_shader(fname, GL_TESS_CONTROL_SHADER);
|
nuclear@15
|
114 #else
|
nuclear@15
|
115 return 0;
|
nuclear@15
|
116 #endif
|
nuclear@15
|
117 }
|
nuclear@15
|
118
|
nuclear@15
|
119 unsigned int load_tesseval_shader(const char *fname)
|
nuclear@15
|
120 {
|
nuclear@15
|
121 #ifdef GL_TESS_EVALUATION_SHADER
|
nuclear@15
|
122 return load_shader(fname, GL_TESS_EVALUATION_SHADER);
|
nuclear@15
|
123 #else
|
nuclear@15
|
124 return 0;
|
nuclear@15
|
125 #endif
|
nuclear@15
|
126 }
|
nuclear@15
|
127
|
nuclear@15
|
128 unsigned int load_geometry_shader(const char *fname)
|
nuclear@15
|
129 {
|
nuclear@15
|
130 #ifdef GL_GEOMETRY_SHADER
|
nuclear@15
|
131 return load_shader(fname, GL_GEOMETRY_SHADER);
|
nuclear@15
|
132 #else
|
nuclear@15
|
133 return 0;
|
nuclear@15
|
134 #endif
|
nuclear@15
|
135 }
|
nuclear@15
|
136
|
nuclear@15
|
137 unsigned int load_shader(const char *fname, unsigned int sdr_type)
|
nuclear@15
|
138 {
|
nuclear@15
|
139 unsigned int sdr;
|
nuclear@15
|
140 size_t filesize;
|
nuclear@15
|
141 FILE *fp;
|
nuclear@15
|
142 char *src;
|
nuclear@15
|
143
|
nuclear@15
|
144 if(!(fp = fopen(fname, "rb"))) {
|
nuclear@15
|
145 fprintf(stderr, "failed to open shader %s: %s\n", fname, strerror(errno));
|
nuclear@15
|
146 return 0;
|
nuclear@15
|
147 }
|
nuclear@15
|
148
|
nuclear@15
|
149 fseek(fp, 0, SEEK_END);
|
nuclear@15
|
150 filesize = ftell(fp);
|
nuclear@15
|
151 fseek(fp, 0, SEEK_SET);
|
nuclear@15
|
152
|
nuclear@15
|
153 if(!(src = malloc(filesize + 1))) {
|
nuclear@15
|
154 fclose(fp);
|
nuclear@15
|
155 return 0;
|
nuclear@15
|
156 }
|
nuclear@15
|
157 fread(src, 1, filesize, fp);
|
nuclear@15
|
158 src[filesize] = 0;
|
nuclear@15
|
159 fclose(fp);
|
nuclear@15
|
160
|
nuclear@15
|
161 fprintf(stderr, "compiling %s shader: %s... ", sdrtypestr(sdr_type), fname);
|
nuclear@15
|
162 sdr = create_shader(src, sdr_type);
|
nuclear@15
|
163
|
nuclear@15
|
164 free(src);
|
nuclear@15
|
165 return sdr;
|
nuclear@15
|
166 }
|
nuclear@15
|
167
|
nuclear@15
|
168
|
nuclear@15
|
169 /* ---- gpu programs ---- */
|
nuclear@15
|
170
|
nuclear@15
|
171 unsigned int create_program(void)
|
nuclear@15
|
172 {
|
nuclear@15
|
173 unsigned int prog = glCreateProgram();
|
nuclear@15
|
174 assert(glGetError() == GL_NO_ERROR);
|
nuclear@15
|
175 return prog;
|
nuclear@15
|
176 }
|
nuclear@15
|
177
|
nuclear@15
|
178 unsigned int create_program_link(unsigned int sdr0, ...)
|
nuclear@15
|
179 {
|
nuclear@15
|
180 unsigned int prog, sdr;
|
nuclear@15
|
181 va_list ap;
|
nuclear@15
|
182
|
nuclear@15
|
183 if(!(prog = create_program())) {
|
nuclear@15
|
184 return 0;
|
nuclear@15
|
185 }
|
nuclear@15
|
186
|
nuclear@15
|
187 attach_shader(prog, sdr0);
|
nuclear@15
|
188 if(glGetError()) {
|
nuclear@15
|
189 return 0;
|
nuclear@15
|
190 }
|
nuclear@15
|
191
|
nuclear@15
|
192 va_start(ap, sdr0);
|
nuclear@15
|
193 while((sdr = va_arg(ap, unsigned int))) {
|
nuclear@15
|
194 attach_shader(prog, sdr);
|
nuclear@15
|
195 if(glGetError()) {
|
nuclear@15
|
196 return 0;
|
nuclear@15
|
197 }
|
nuclear@15
|
198 }
|
nuclear@15
|
199 va_end(ap);
|
nuclear@15
|
200
|
nuclear@15
|
201 if(link_program(prog) == -1) {
|
nuclear@15
|
202 free_program(prog);
|
nuclear@15
|
203 return 0;
|
nuclear@15
|
204 }
|
nuclear@15
|
205 return prog;
|
nuclear@15
|
206 }
|
nuclear@15
|
207
|
nuclear@15
|
208 unsigned int create_program_load(const char *vfile, const char *pfile)
|
nuclear@15
|
209 {
|
nuclear@15
|
210 unsigned int vs = 0, ps = 0;
|
nuclear@15
|
211
|
nuclear@15
|
212 if(vfile && *vfile && !(vs = load_vertex_shader(vfile))) {
|
nuclear@15
|
213 return 0;
|
nuclear@15
|
214 }
|
nuclear@15
|
215 if(pfile && *pfile && !(ps = load_pixel_shader(pfile))) {
|
nuclear@15
|
216 return 0;
|
nuclear@15
|
217 }
|
nuclear@15
|
218 return create_program_link(vs, ps, 0);
|
nuclear@15
|
219 }
|
nuclear@15
|
220
|
nuclear@15
|
221 void free_program(unsigned int sdr)
|
nuclear@15
|
222 {
|
nuclear@15
|
223 glDeleteProgram(sdr);
|
nuclear@15
|
224 }
|
nuclear@15
|
225
|
nuclear@15
|
226 void attach_shader(unsigned int prog, unsigned int sdr)
|
nuclear@15
|
227 {
|
nuclear@15
|
228 int err;
|
nuclear@15
|
229
|
nuclear@15
|
230 if(prog && sdr) {
|
nuclear@15
|
231 assert(glGetError() == GL_NO_ERROR);
|
nuclear@15
|
232 glAttachShader(prog, sdr);
|
nuclear@15
|
233 if((err = glGetError()) != GL_NO_ERROR) {
|
nuclear@15
|
234 fprintf(stderr, "failed to attach shader %u to program %u (err: 0x%x)\n", sdr, prog, err);
|
nuclear@15
|
235 abort();
|
nuclear@15
|
236 }
|
nuclear@15
|
237 }
|
nuclear@15
|
238 }
|
nuclear@15
|
239
|
nuclear@15
|
240 int link_program(unsigned int prog)
|
nuclear@15
|
241 {
|
nuclear@15
|
242 int linked, info_len, retval = 0;
|
nuclear@15
|
243 char *info_str = 0;
|
nuclear@15
|
244
|
nuclear@15
|
245 glLinkProgram(prog);
|
nuclear@15
|
246 assert(glGetError() == GL_NO_ERROR);
|
nuclear@15
|
247 glGetProgramiv(prog, GL_LINK_STATUS, &linked);
|
nuclear@15
|
248 assert(glGetError() == GL_NO_ERROR);
|
nuclear@15
|
249 glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &info_len);
|
nuclear@15
|
250 assert(glGetError() == GL_NO_ERROR);
|
nuclear@15
|
251
|
nuclear@15
|
252 if(info_len) {
|
nuclear@15
|
253 if((info_str = malloc(info_len + 1))) {
|
nuclear@15
|
254 glGetProgramInfoLog(prog, info_len, 0, info_str);
|
nuclear@15
|
255 assert(glGetError() == GL_NO_ERROR);
|
nuclear@15
|
256 info_str[info_len] = 0;
|
nuclear@15
|
257 }
|
nuclear@15
|
258 }
|
nuclear@15
|
259
|
nuclear@15
|
260 if(linked) {
|
nuclear@15
|
261 fprintf(stderr, info_str ? "linking done: %s\n" : "linking done\n", info_str);
|
nuclear@15
|
262 } else {
|
nuclear@15
|
263 fprintf(stderr, info_str ? "linking failed: %s\n" : "linking failed\n", info_str);
|
nuclear@15
|
264 retval = -1;
|
nuclear@15
|
265 }
|
nuclear@15
|
266
|
nuclear@15
|
267 free(info_str);
|
nuclear@15
|
268 return retval;
|
nuclear@15
|
269 }
|
nuclear@15
|
270
|
nuclear@15
|
271 int bind_program(unsigned int prog)
|
nuclear@15
|
272 {
|
nuclear@15
|
273 GLenum err;
|
nuclear@15
|
274
|
nuclear@15
|
275 glUseProgram(prog);
|
nuclear@15
|
276 if(prog && (err = glGetError()) != GL_NO_ERROR) {
|
nuclear@15
|
277 /* maybe the program is not linked, try linking first */
|
nuclear@15
|
278 if(err == GL_INVALID_OPERATION) {
|
nuclear@15
|
279 if(link_program(prog) == -1) {
|
nuclear@15
|
280 return -1;
|
nuclear@15
|
281 }
|
nuclear@15
|
282 glUseProgram(prog);
|
nuclear@15
|
283 return glGetError() == GL_NO_ERROR ? 0 : -1;
|
nuclear@15
|
284 }
|
nuclear@15
|
285 return -1;
|
nuclear@15
|
286 }
|
nuclear@15
|
287 return 0;
|
nuclear@15
|
288 }
|
nuclear@15
|
289
|
nuclear@15
|
290 /* ugly but I'm not going to write the same bloody code over and over */
|
nuclear@15
|
291 #define BEGIN_UNIFORM_CODE \
|
nuclear@15
|
292 int loc, curr_prog; \
|
nuclear@15
|
293 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); \
|
nuclear@15
|
294 if((unsigned int)curr_prog != prog && bind_program(prog) == -1) { \
|
nuclear@15
|
295 return -1; \
|
nuclear@15
|
296 } \
|
nuclear@15
|
297 if((loc = glGetUniformLocation(prog, name)) != -1)
|
nuclear@15
|
298
|
nuclear@15
|
299 #define END_UNIFORM_CODE \
|
nuclear@15
|
300 if((unsigned int)curr_prog != prog) { \
|
nuclear@15
|
301 bind_program(curr_prog); \
|
nuclear@15
|
302 } \
|
nuclear@15
|
303 return loc == -1 ? -1 : 0
|
nuclear@15
|
304
|
nuclear@15
|
305 int set_uniform_int(unsigned int prog, const char *name, int val)
|
nuclear@15
|
306 {
|
nuclear@15
|
307 BEGIN_UNIFORM_CODE {
|
nuclear@15
|
308 glUniform1i(loc, val);
|
nuclear@15
|
309 }
|
nuclear@15
|
310 END_UNIFORM_CODE;
|
nuclear@15
|
311 }
|
nuclear@15
|
312
|
nuclear@15
|
313 int set_uniform_float(unsigned int prog, const char *name, float val)
|
nuclear@15
|
314 {
|
nuclear@15
|
315 BEGIN_UNIFORM_CODE {
|
nuclear@15
|
316 glUniform1f(loc, val);
|
nuclear@15
|
317 }
|
nuclear@15
|
318 END_UNIFORM_CODE;
|
nuclear@15
|
319 }
|
nuclear@15
|
320
|
nuclear@15
|
321 int set_uniform_float2(unsigned int prog, const char *name, float x, float y)
|
nuclear@15
|
322 {
|
nuclear@15
|
323 BEGIN_UNIFORM_CODE {
|
nuclear@15
|
324 glUniform2f(loc, x, y);
|
nuclear@15
|
325 }
|
nuclear@15
|
326 END_UNIFORM_CODE;
|
nuclear@15
|
327 }
|
nuclear@15
|
328
|
nuclear@15
|
329 int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z)
|
nuclear@15
|
330 {
|
nuclear@15
|
331 BEGIN_UNIFORM_CODE {
|
nuclear@15
|
332 glUniform3f(loc, x, y, z);
|
nuclear@15
|
333 }
|
nuclear@15
|
334 END_UNIFORM_CODE;
|
nuclear@15
|
335 }
|
nuclear@15
|
336
|
nuclear@15
|
337 int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w)
|
nuclear@15
|
338 {
|
nuclear@15
|
339 BEGIN_UNIFORM_CODE {
|
nuclear@15
|
340 glUniform4f(loc, x, y, z, w);
|
nuclear@15
|
341 }
|
nuclear@15
|
342 END_UNIFORM_CODE;
|
nuclear@15
|
343 }
|
nuclear@15
|
344
|
nuclear@15
|
345 int set_uniform_matrix4(unsigned int prog, const char *name, float *mat)
|
nuclear@15
|
346 {
|
nuclear@15
|
347 BEGIN_UNIFORM_CODE {
|
nuclear@15
|
348 glUniformMatrix4fv(loc, 1, GL_FALSE, mat);
|
nuclear@15
|
349 }
|
nuclear@15
|
350 END_UNIFORM_CODE;
|
nuclear@15
|
351 }
|
nuclear@15
|
352
|
nuclear@15
|
353 int set_uniform_matrix4_transposed(unsigned int prog, const char *name, float *mat)
|
nuclear@15
|
354 {
|
nuclear@15
|
355 BEGIN_UNIFORM_CODE {
|
nuclear@15
|
356 glUniformMatrix4fv(loc, 1, GL_TRUE, mat);
|
nuclear@15
|
357 }
|
nuclear@15
|
358 END_UNIFORM_CODE;
|
nuclear@15
|
359 }
|
nuclear@15
|
360
|
nuclear@15
|
361 int get_attrib_loc(unsigned int prog, const char *name)
|
nuclear@15
|
362 {
|
nuclear@15
|
363 int loc, curr_prog;
|
nuclear@15
|
364
|
nuclear@15
|
365 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog);
|
nuclear@15
|
366 if((unsigned int)curr_prog != prog && bind_program(prog) == -1) {
|
nuclear@15
|
367 return -1;
|
nuclear@15
|
368 }
|
nuclear@15
|
369
|
nuclear@15
|
370 loc = glGetAttribLocation(prog, (char*)name);
|
nuclear@15
|
371
|
nuclear@15
|
372 if((unsigned int)curr_prog != prog) {
|
nuclear@15
|
373 bind_program(curr_prog);
|
nuclear@15
|
374 }
|
nuclear@15
|
375 return loc;
|
nuclear@15
|
376 }
|
nuclear@15
|
377
|
nuclear@15
|
378 void set_attrib_float3(int attr_loc, float x, float y, float z)
|
nuclear@15
|
379 {
|
nuclear@15
|
380 glVertexAttrib3f(attr_loc, x, y, z);
|
nuclear@15
|
381 }
|
nuclear@15
|
382
|
nuclear@15
|
383 static const char *sdrtypestr(unsigned int sdrtype)
|
nuclear@15
|
384 {
|
nuclear@15
|
385 switch(sdrtype) {
|
nuclear@15
|
386 case GL_VERTEX_SHADER:
|
nuclear@15
|
387 return "vertex";
|
nuclear@15
|
388 case GL_FRAGMENT_SHADER:
|
nuclear@15
|
389 return "pixel";
|
nuclear@15
|
390 #ifdef GL_TESS_CONTROL_SHADER
|
nuclear@15
|
391 case GL_TESS_CONTROL_SHADER:
|
nuclear@15
|
392 return "tessellation control";
|
nuclear@15
|
393 #endif
|
nuclear@15
|
394 #ifdef GL_TESS_EVALUATION_SHADER
|
nuclear@15
|
395 case GL_TESS_EVALUATION_SHADER:
|
nuclear@15
|
396 return "tessellation evaluation";
|
nuclear@15
|
397 #endif
|
nuclear@15
|
398 #ifdef GL_GEOMETRY_SHADER
|
nuclear@15
|
399 case GL_GEOMETRY_SHADER:
|
nuclear@15
|
400 return "geometry";
|
nuclear@15
|
401 #endif
|
nuclear@15
|
402
|
nuclear@15
|
403 default:
|
nuclear@15
|
404 break;
|
nuclear@15
|
405 }
|
nuclear@15
|
406 return "<unknown>";
|
nuclear@15
|
407 }
|