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