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