stereoplay
view src/sdr.c @ 1:b50fd6f24975
implemented pause
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 06 Mar 2011 20:53:30 +0200 |
parents | |
children | acf3d25f23cb |
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__) || defined(__APPLE__)
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__) || defined(__APPLE__)
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 0
133 if((res = get_resource(sdrman, fname))) {
134 /* TODO: validate that the shader matches sdr_type? */
135 return (uintptr_t)res;
136 }
137 #endif
139 if(!(sdr = load_shader(fname, sdr_type))) {
140 return 0;
141 }
142 add_shader(fname, sdr);
143 return sdr;
144 }
146 int add_shader(const char *fname, unsigned int sdr)
147 {
148 return 0;/*add_resource(sdrman, fname, (void*)(uintptr_t)sdr);*/
149 }
151 int remove_shader(const char *fname)
152 {
153 return 0;/*remove_resource(sdrman, fname);*/
154 }
157 /* ---- gpu programs ---- */
159 unsigned int create_program(void)
160 {
161 unsigned int prog = glCreateProgram();
162 assert(glGetError() == GL_NO_ERROR);
163 return prog;
164 }
166 unsigned int create_program_link(unsigned int vs, unsigned int ps)
167 {
168 unsigned int prog;
169 if(!vs && !ps) {
170 return 0;
171 }
173 if(!(prog = create_program())) {
174 return 0;
175 }
177 if(vs) {
178 attach_shader(prog, vs);
179 assert(glGetError() == GL_NO_ERROR);
180 }
181 if(ps) {
182 attach_shader(prog, ps);
183 assert(glGetError() == GL_NO_ERROR);
184 }
186 if(link_program(prog) == -1) {
187 free_program(prog);
188 return 0;
189 }
190 return prog;
191 }
193 unsigned int create_program_load(const char *vfile, const char *pfile)
194 {
195 unsigned int vs = 0, ps = 0;
197 if(vfile) {
198 if(!(vs = get_vertex_shader(vfile))) {
199 return 0;
200 }
201 }
202 if(pfile) {
203 if(!(ps = get_pixel_shader(pfile))) {
204 return 0;
205 }
206 }
207 return create_program_link(vs, ps);
208 }
210 void free_program(unsigned int sdr)
211 {
212 glDeleteProgram(sdr);
213 }
215 void attach_shader(unsigned int prog, unsigned int sdr)
216 {
217 glAttachShader(prog, sdr);
218 assert(glGetError() == GL_NO_ERROR);
219 }
221 int link_program(unsigned int prog)
222 {
223 int linked, info_len, retval = 0;
224 char *info_str = 0;
226 glLinkProgram(prog);
227 assert(glGetError() == GL_NO_ERROR);
228 glGetProgramiv(prog, GL_LINK_STATUS, &linked);
229 assert(glGetError() == GL_NO_ERROR);
230 glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &info_len);
231 assert(glGetError() == GL_NO_ERROR);
233 if(info_len) {
234 if((info_str = malloc(info_len + 1))) {
235 glGetProgramInfoLog(prog, info_len, 0, info_str);
236 assert(glGetError() == GL_NO_ERROR);
237 }
238 }
240 if(linked) {
241 fprintf(stderr, info_str ? "linking done: %s\n" : "linking done\n", info_str);
242 } else {
243 fprintf(stderr, info_str ? "linking failed: %s\n" : "linking failed\n", info_str);
244 retval = -1;
245 }
247 free(info_str);
248 return retval;
249 }
251 int bind_program(unsigned int prog)
252 {
253 GLenum err;
255 glUseProgram(prog);
256 if(prog && (err = glGetError()) != GL_NO_ERROR) {
257 /* maybe the program is not linked, try to link first */
258 if(err == GL_INVALID_OPERATION) {
259 if(link_program(prog) == -1) {
260 return -1;
261 }
262 glUseProgram(prog);
263 return glGetError() == GL_NO_ERROR ? 0 : -1;
264 }
265 return -1;
266 }
267 return 0;
268 }
270 /* ugly but I'm not going to write the same bloody code over and over */
271 #define BEGIN_UNIFORM_CODE \
272 int loc, curr_prog; \
273 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); \
274 if(curr_prog != prog && bind_program(prog) == -1) { \
275 return -1; \
276 } \
277 if((loc = glGetUniformLocation(prog, name)) != -1)
279 #define END_UNIFORM_CODE \
280 if(curr_prog != prog) { \
281 bind_program(curr_prog); \
282 } \
283 return loc == -1 ? -1 : 0
285 int set_uniform_int(unsigned int prog, const char *name, int val)
286 {
287 BEGIN_UNIFORM_CODE {
288 glUniform1i(loc, val);
289 }
290 END_UNIFORM_CODE;
291 }
293 int set_uniform_float(unsigned int prog, const char *name, float val)
294 {
295 BEGIN_UNIFORM_CODE {
296 glUniform1f(loc, val);
297 }
298 END_UNIFORM_CODE;
299 }