tesspot

view src/sdr.c @ 2:178a9e3c3c8c

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