dungeon_crawler
view prototype/src/sdr.c @ 61:4472ef41a209
forgot an extra mrt uniform in deferred_omni.p.glsl
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 25 Sep 2012 06:37:56 +0300 |
parents | d98240a13793 |
children |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <assert.h>
6 #include <GL/glew.h>
8 #if defined(unix) || defined(__unix__)
9 #include <unistd.h>
10 #include <sys/stat.h>
11 #endif /* unix */
13 #include "sdr.h"
15 unsigned int create_vertex_shader(const char *src)
16 {
17 return create_shader(src, GL_VERTEX_SHADER);
18 }
20 unsigned int create_pixel_shader(const char *src)
21 {
22 return create_shader(src, GL_FRAGMENT_SHADER);
23 }
25 unsigned int create_shader(const char *src, unsigned int sdr_type)
26 {
27 unsigned int sdr;
28 int success, info_len;
29 char *info_str = 0;
30 GLenum err;
32 sdr = glCreateShader(sdr_type);
33 assert(glGetError() == GL_NO_ERROR);
34 glShaderSource(sdr, 1, &src, 0);
35 err = glGetError();
36 assert(err == GL_NO_ERROR);
37 glCompileShader(sdr);
38 assert(glGetError() == GL_NO_ERROR);
40 glGetShaderiv(sdr, GL_COMPILE_STATUS, &success);
41 assert(glGetError() == GL_NO_ERROR);
42 glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &info_len);
43 assert(glGetError() == GL_NO_ERROR);
45 if(info_len) {
46 if((info_str = malloc(info_len + 1))) {
47 glGetShaderInfoLog(sdr, info_len, 0, info_str);
48 assert(glGetError() == GL_NO_ERROR);
49 }
50 }
52 if(success) {
53 fprintf(stderr, info_str ? "done: %s\n" : "done\n", info_str);
54 } else {
55 fprintf(stderr, info_str ? "failed: %s\n" : "failed\n", info_str);
56 glDeleteShader(sdr);
57 sdr = 0;
58 }
60 free(info_str);
61 return sdr;
62 }
64 void free_shader(unsigned int sdr)
65 {
66 glDeleteShader(sdr);
67 }
69 unsigned int load_vertex_shader(const char *fname)
70 {
71 return load_shader(fname, GL_VERTEX_SHADER);
72 }
74 unsigned int load_pixel_shader(const char *fname)
75 {
76 return load_shader(fname, GL_FRAGMENT_SHADER);
77 }
79 unsigned int load_shader(const char *fname, unsigned int sdr_type)
80 {
81 #if defined(unix) || defined(__unix__)
82 struct stat st;
83 #endif
84 unsigned int sdr;
85 size_t filesize;
86 FILE *fp;
87 char *src;
89 if(!(fp = fopen(fname, "r"))) {
90 fprintf(stderr, "failed to open shader %s: %s\n", fname, strerror(errno));
91 return 0;
92 }
94 #if defined(unix) || defined(__unix__)
95 fstat(fileno(fp), &st);
96 filesize = st.st_size;
97 #else
98 fseek(fp, 0, SEEK_END);
99 filesize = ftell(fp);
100 fseek(fp, 0, SEEK_SET);
101 #endif /* unix */
103 if(!(src = malloc(filesize + 1))) {
104 fclose(fp);
105 return 0;
106 }
107 fread(src, 1, filesize, fp);
108 src[filesize] = 0;
109 fclose(fp);
111 fprintf(stderr, "compiling %s shader: %s... ", (sdr_type == GL_VERTEX_SHADER ? "vertex" : "pixel"), fname);
112 sdr = create_shader(src, sdr_type);
114 free(src);
115 return sdr;
116 }
119 unsigned int get_vertex_shader(const char *fname)
120 {
121 return get_shader(fname, GL_VERTEX_SHADER);
122 }
124 unsigned int get_pixel_shader(const char *fname)
125 {
126 return get_shader(fname, GL_FRAGMENT_SHADER);
127 }
129 unsigned int get_shader(const char *fname, unsigned int sdr_type)
130 {
131 unsigned int sdr;
132 if(!(sdr = load_shader(fname, sdr_type))) {
133 return 0;
134 }
135 return sdr;
136 }
139 /* ---- gpu programs ---- */
141 unsigned int create_program(void)
142 {
143 unsigned int prog = glCreateProgram();
144 assert(glGetError() == GL_NO_ERROR);
145 return prog;
146 }
148 unsigned int create_program_link(unsigned int vs, unsigned int ps)
149 {
150 unsigned int prog;
152 if(!(prog = create_program())) {
153 return 0;
154 }
156 if(vs) {
157 attach_shader(prog, vs);
158 assert(glGetError() == GL_NO_ERROR);
159 }
160 if(ps) {
161 attach_shader(prog, ps);
162 assert(glGetError() == GL_NO_ERROR);
163 }
165 if(link_program(prog) == -1) {
166 free_program(prog);
167 return 0;
168 }
169 return prog;
170 }
172 unsigned int create_program_load(const char *vfile, const char *pfile)
173 {
174 unsigned int vs = 0, ps = 0;
176 if(vfile && *vfile && !(vs = get_vertex_shader(vfile))) {
177 return 0;
178 }
179 if(pfile && *pfile && !(ps = get_pixel_shader(pfile))) {
180 return 0;
181 }
182 return create_program_link(vs, ps);
183 }
185 void free_program(unsigned int sdr)
186 {
187 glDeleteProgram(sdr);
188 }
190 void attach_shader(unsigned int prog, unsigned int sdr)
191 {
192 glAttachShader(prog, sdr);
193 assert(glGetError() == GL_NO_ERROR);
194 }
196 int link_program(unsigned int prog)
197 {
198 int linked, info_len, retval = 0;
199 char *info_str = 0;
201 glLinkProgram(prog);
202 assert(glGetError() == GL_NO_ERROR);
203 glGetProgramiv(prog, GL_LINK_STATUS, &linked);
204 assert(glGetError() == GL_NO_ERROR);
205 glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &info_len);
206 assert(glGetError() == GL_NO_ERROR);
208 if(info_len) {
209 if((info_str = malloc(info_len + 1))) {
210 glGetProgramInfoLog(prog, info_len, 0, info_str);
211 assert(glGetError() == GL_NO_ERROR);
212 }
213 }
215 if(linked) {
216 fprintf(stderr, info_str ? "linking done: %s\n" : "linking done\n", info_str);
217 } else {
218 fprintf(stderr, info_str ? "linking failed: %s\n" : "linking failed\n", info_str);
219 retval = -1;
220 }
222 free(info_str);
223 return retval;
224 }
226 int bind_program(unsigned int prog)
227 {
228 GLenum err;
230 glUseProgram(prog);
231 if(prog && (err = glGetError()) != GL_NO_ERROR) {
232 /* maybe the program is not linked, try linking first */
233 if(err == GL_INVALID_OPERATION) {
234 if(link_program(prog) == -1) {
235 return -1;
236 }
237 glUseProgram(prog);
238 return glGetError() == GL_NO_ERROR ? 0 : -1;
239 }
240 return -1;
241 }
242 return 0;
243 }
245 /* ugly but I'm not going to write the same bloody code over and over */
246 #define BEGIN_UNIFORM_CODE \
247 int loc, curr_prog; \
248 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); \
249 if((unsigned int)curr_prog != prog && bind_program(prog) == -1) { \
250 return -1; \
251 } \
252 if((loc = glGetUniformLocation(prog, name)) != -1)
254 #define END_UNIFORM_CODE \
255 if((unsigned int)curr_prog != prog) { \
256 bind_program(curr_prog); \
257 } \
258 return loc == -1 ? -1 : 0
260 int set_uniform_int(unsigned int prog, const char *name, int val)
261 {
262 BEGIN_UNIFORM_CODE {
263 glUniform1i(loc, val);
264 }
265 END_UNIFORM_CODE;
266 }
268 int set_uniform_float(unsigned int prog, const char *name, float val)
269 {
270 BEGIN_UNIFORM_CODE {
271 glUniform1f(loc, val);
272 }
273 END_UNIFORM_CODE;
274 }
276 int set_uniform_float2(unsigned int prog, const char *name, float x, float y)
277 {
278 BEGIN_UNIFORM_CODE {
279 glUniform2f(loc, x, y);
280 }
281 END_UNIFORM_CODE;
282 }
284 int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z)
285 {
286 BEGIN_UNIFORM_CODE {
287 glUniform3f(loc, x, y, z);
288 }
289 END_UNIFORM_CODE;
290 }
292 int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w)
293 {
294 BEGIN_UNIFORM_CODE {
295 glUniform4f(loc, x, y, z, w);
296 }
297 END_UNIFORM_CODE;
298 }
300 int set_uniform_matrix4(unsigned int prog, const char *name, float *mat)
301 {
302 BEGIN_UNIFORM_CODE {
303 glUniformMatrix4fv(loc, 1, GL_FALSE, mat);
304 }
305 END_UNIFORM_CODE;
306 }
308 int set_uniform_matrix4_transposed(unsigned int prog, const char *name, float *mat)
309 {
310 BEGIN_UNIFORM_CODE {
311 glUniformMatrix4fv(loc, 1, GL_TRUE, mat);
312 }
313 END_UNIFORM_CODE;
314 }
316 int get_attrib_loc(unsigned int prog, const char *name)
317 {
318 int loc, curr_prog;
320 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog);
321 if((unsigned int)curr_prog != prog && bind_program(prog) == -1) {
322 return -1;
323 }
325 loc = glGetAttribLocation(prog, (char*)name);
327 if((unsigned int)curr_prog != prog) {
328 bind_program(curr_prog);
329 }
330 return loc;
331 }
333 void set_attrib_float3(int attr_loc, float x, float y, float z)
334 {
335 glVertexAttrib3f(attr_loc, x, y, z);
336 }