rayfract
view src/sdr.c @ 0:09bb67c000bc
ray-fract repository
author | John Tsiombikas <nuclear@siggraph.org> |
---|---|
date | Thu, 21 Oct 2010 23:39:26 +0300 |
parents | |
children | 1496aae2e7d4 |
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 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;
170 if(!(prog = create_program())) {
171 return 0;
172 }
174 attach_shader(prog, vs);
175 assert(glGetError() == GL_NO_ERROR);
176 attach_shader(prog, ps);
177 assert(glGetError() == GL_NO_ERROR);
179 if(link_program(prog) == -1) {
180 free_program(prog);
181 return 0;
182 }
183 return prog;
184 }
186 unsigned int create_program_load(const char *vfile, const char *pfile)
187 {
188 unsigned int vs, ps;
190 if(!(vs = get_vertex_shader(vfile)) || !(ps = get_pixel_shader(pfile))) {
191 return 0;
192 }
193 return create_program_link(vs, ps);
194 }
196 void free_program(unsigned int sdr)
197 {
198 glDeleteProgram(sdr);
199 }
201 void attach_shader(unsigned int prog, unsigned int sdr)
202 {
203 glAttachShader(prog, sdr);
204 assert(glGetError() == GL_NO_ERROR);
205 }
207 int link_program(unsigned int prog)
208 {
209 int linked, info_len, retval = 0;
210 char *info_str = 0;
212 glLinkProgram(prog);
213 assert(glGetError() == GL_NO_ERROR);
214 glGetProgramiv(prog, GL_LINK_STATUS, &linked);
215 assert(glGetError() == GL_NO_ERROR);
216 glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &info_len);
217 assert(glGetError() == GL_NO_ERROR);
219 if(info_len) {
220 if((info_str = malloc(info_len + 1))) {
221 glGetProgramInfoLog(prog, info_len, 0, info_str);
222 assert(glGetError() == GL_NO_ERROR);
223 }
224 }
226 if(linked) {
227 fprintf(stderr, info_str ? "linking done: %s\n" : "linking done\n", info_str);
228 } else {
229 fprintf(stderr, info_str ? "linking failed: %s\n" : "linking failed\n", info_str);
230 retval = -1;
231 }
233 free(info_str);
234 return retval;
235 }
237 int bind_program(unsigned int prog)
238 {
239 GLenum err;
241 glUseProgram(prog);
242 if(prog && (err = glGetError()) != GL_NO_ERROR) {
243 /* maybe the program is not linked, try to link first */
244 if(err == GL_INVALID_OPERATION) {
245 if(link_program(prog) == -1) {
246 return -1;
247 }
248 glUseProgram(prog);
249 return glGetError() == GL_NO_ERROR ? 0 : -1;
250 }
251 return -1;
252 }
253 return 0;
254 }
256 /* ugly but I'm not going to write the same bloody code over and over */
257 #define BEGIN_UNIFORM_CODE \
258 int loc, curr_prog; \
259 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); \
260 if(curr_prog != prog && bind_program(prog) == -1) { \
261 return -1; \
262 } \
263 if((loc = glGetUniformLocation(prog, name)) != -1)
265 #define END_UNIFORM_CODE \
266 if(curr_prog != prog) { \
267 bind_program(curr_prog); \
268 } \
269 return loc == -1 ? -1 : 0
271 int set_uniform_int(unsigned int prog, const char *name, int val)
272 {
273 BEGIN_UNIFORM_CODE {
274 glUniform1i(loc, val);
275 }
276 END_UNIFORM_CODE;
277 }
279 int set_uniform_float(unsigned int prog, const char *name, float val)
280 {
281 BEGIN_UNIFORM_CODE {
282 glUniform1f(loc, val);
283 }
284 END_UNIFORM_CODE;
285 }
287 int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z)
288 {
289 BEGIN_UNIFORM_CODE {
290 glUniform3f(loc, x, y, z);
291 }
292 END_UNIFORM_CODE;
293 }
295 int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w)
296 {
297 BEGIN_UNIFORM_CODE {
298 glUniform4f(loc, x, y, z, w);
299 }
300 END_UNIFORM_CODE;
301 }
304 int get_attrib_loc(unsigned int prog, const char *name)
305 {
306 int loc, curr_prog;
308 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog);
309 if(curr_prog != prog && bind_program(prog) == -1) {
310 return -1;
311 }
313 loc = glGetAttribLocation(prog, (char*)name);
315 if(curr_prog != prog) {
316 bind_program(curr_prog);
317 }
318 return loc;
319 }
321 void set_attrib_float3(int attr_loc, float x, float y, float z)
322 {
323 glVertexAttrib3f(attr_loc, x, y, z);
324 }