gpuray_glsl

view src/glsdr.c @ 4:2ed3da7dc0bc

broken
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 10 Nov 2014 01:26:00 +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 "glsdr.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 sdr[2];
229 int i = 0;
231 if(vfile && *vfile && !(sdr[i++] = get_vertex_shader(vfile))) {
232 return 0;
233 }
234 if(pfile && *pfile && !(sdr[i++] = get_pixel_shader(pfile))) {
235 return 0;
236 }
237 return create_program_link(sdr[0], sdr[1], 0);
238 }
240 void free_program(unsigned int sdr)
241 {
242 glDeleteProgram(sdr);
243 }
245 void attach_shader(unsigned int prog, unsigned int sdr)
246 {
247 glAttachShader(prog, sdr);
248 assert(glGetError() == GL_NO_ERROR);
249 }
251 int link_program(unsigned int prog)
252 {
253 int linked, info_len, retval = 0;
254 char *info_str = 0;
256 glLinkProgram(prog);
257 assert(glGetError() == GL_NO_ERROR);
258 glGetProgramiv(prog, GL_LINK_STATUS, &linked);
259 assert(glGetError() == GL_NO_ERROR);
260 glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &info_len);
261 assert(glGetError() == GL_NO_ERROR);
263 if(info_len) {
264 if((info_str = malloc(info_len + 1))) {
265 glGetProgramInfoLog(prog, info_len, 0, info_str);
266 assert(glGetError() == GL_NO_ERROR);
267 }
268 }
270 if(linked) {
271 fprintf(stderr, info_str ? "linking done: %s\n" : "linking done\n", info_str);
272 } else {
273 fprintf(stderr, info_str ? "linking failed: %s\n" : "linking failed\n", info_str);
274 retval = -1;
275 }
277 free(info_str);
278 return retval;
279 }
281 int bind_program(unsigned int prog)
282 {
283 GLenum err;
285 glUseProgram(prog);
286 if(prog && (err = glGetError()) != GL_NO_ERROR) {
287 /* maybe the program is not linked, try linking first */
288 if(err == GL_INVALID_OPERATION) {
289 if(link_program(prog) == -1) {
290 return -1;
291 }
292 glUseProgram(prog);
293 return glGetError() == GL_NO_ERROR ? 0 : -1;
294 }
295 return -1;
296 }
297 return 0;
298 }
300 /* ugly but I'm not going to write the same bloody code over and over */
301 #define BEGIN_UNIFORM_CODE \
302 int loc, curr_prog; \
303 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); \
304 if((unsigned int)curr_prog != prog && bind_program(prog) == -1) { \
305 return -1; \
306 } \
307 if((loc = glGetUniformLocation(prog, name)) != -1)
309 #define END_UNIFORM_CODE \
310 if((unsigned int)curr_prog != prog) { \
311 bind_program(curr_prog); \
312 } \
313 return loc == -1 ? -1 : 0
315 int set_uniform_int(unsigned int prog, const char *name, int val)
316 {
317 BEGIN_UNIFORM_CODE {
318 glUniform1i(loc, val);
319 }
320 END_UNIFORM_CODE;
321 }
323 int set_uniform_float(unsigned int prog, const char *name, float val)
324 {
325 BEGIN_UNIFORM_CODE {
326 glUniform1f(loc, val);
327 }
328 END_UNIFORM_CODE;
329 }
331 int set_uniform_float2(unsigned int prog, const char *name, float x, float y)
332 {
333 BEGIN_UNIFORM_CODE {
334 glUniform2f(loc, x, y);
335 }
336 END_UNIFORM_CODE;
337 }
339 int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z)
340 {
341 BEGIN_UNIFORM_CODE {
342 glUniform3f(loc, x, y, z);
343 }
344 END_UNIFORM_CODE;
345 }
347 int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w)
348 {
349 BEGIN_UNIFORM_CODE {
350 glUniform4f(loc, x, y, z, w);
351 }
352 END_UNIFORM_CODE;
353 }
355 int set_uniform_matrix4(unsigned int prog, const char *name, float *mat)
356 {
357 BEGIN_UNIFORM_CODE {
358 glUniformMatrix4fv(loc, 1, GL_FALSE, mat);
359 }
360 END_UNIFORM_CODE;
361 }
363 int set_uniform_matrix4_transposed(unsigned int prog, const char *name, float *mat)
364 {
365 BEGIN_UNIFORM_CODE {
366 glUniformMatrix4fv(loc, 1, GL_TRUE, mat);
367 }
368 END_UNIFORM_CODE;
369 }
371 int get_attrib_loc(unsigned int prog, const char *name)
372 {
373 int loc, curr_prog;
375 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog);
376 if((unsigned int)curr_prog != prog && bind_program(prog) == -1) {
377 return -1;
378 }
380 loc = glGetAttribLocation(prog, (char*)name);
382 if((unsigned int)curr_prog != prog) {
383 bind_program(curr_prog);
384 }
385 return loc;
386 }
388 void set_attrib_float3(int attr_loc, float x, float y, float z)
389 {
390 glVertexAttrib3f(attr_loc, x, y, z);
391 }
393 static const char *sdrtypestr(unsigned int sdrtype)
394 {
395 switch(sdrtype) {
396 case GL_VERTEX_SHADER:
397 return "vertex";
398 case GL_FRAGMENT_SHADER:
399 return "pixel";
400 case GL_TESS_CONTROL_SHADER:
401 return "tessellation control";
402 case GL_TESS_EVALUATION_SHADER:
403 return "tessellation evaluation";
404 case GL_GEOMETRY_SHADER:
405 return "geometry";
407 default:
408 break;
409 }
410 return "<unknown>";
411 }