conworlds
view src/sdr.c @ 6:3c36bc28c6c2
more stuff in the vr test
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 21 Aug 2014 01:08:03 +0300 |
parents | |
children |
line source
1 /*
2 Printblobs - halftoning display hack
3 Copyright (C) 2013 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <stdarg.h>
23 #include <assert.h>
24 #include <GL/glew.h>
26 #if defined(unix) || defined(__unix__)
27 #include <unistd.h>
28 #include <sys/stat.h>
29 #endif /* unix */
31 #include "sdr.h"
33 static const char *sdrtypestr(unsigned int sdrtype);
35 unsigned int create_vertex_shader(const char *src)
36 {
37 return create_shader(src, GL_VERTEX_SHADER);
38 }
40 unsigned int create_pixel_shader(const char *src)
41 {
42 return create_shader(src, GL_FRAGMENT_SHADER);
43 }
45 unsigned int create_tessctl_shader(const char *src)
46 {
47 return create_shader(src, GL_TESS_CONTROL_SHADER);
48 }
50 unsigned int create_tesseval_shader(const char *src)
51 {
52 return create_shader(src, GL_TESS_EVALUATION_SHADER);
53 }
55 unsigned int create_geometry_shader(const char *src)
56 {
57 return create_shader(src, GL_GEOMETRY_SHADER);
58 }
60 unsigned int create_shader(const char *src, unsigned int sdr_type)
61 {
62 unsigned int sdr;
63 int success, info_len;
64 char *info_str = 0;
65 GLenum err;
67 sdr = glCreateShader(sdr_type);
68 assert(glGetError() == GL_NO_ERROR);
69 glShaderSource(sdr, 1, &src, 0);
70 err = glGetError();
71 assert(err == GL_NO_ERROR);
72 glCompileShader(sdr);
73 assert(glGetError() == GL_NO_ERROR);
75 glGetShaderiv(sdr, GL_COMPILE_STATUS, &success);
76 assert(glGetError() == GL_NO_ERROR);
77 glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &info_len);
78 assert(glGetError() == GL_NO_ERROR);
80 if(info_len) {
81 if((info_str = malloc(info_len + 1))) {
82 glGetShaderInfoLog(sdr, info_len, 0, info_str);
83 assert(glGetError() == GL_NO_ERROR);
84 }
85 }
87 if(success) {
88 fprintf(stderr, info_str ? "done: %s\n" : "done\n", info_str);
89 } else {
90 fprintf(stderr, info_str ? "failed: %s\n" : "failed\n", info_str);
91 glDeleteShader(sdr);
92 sdr = 0;
93 }
95 free(info_str);
96 return sdr;
97 }
99 void free_shader(unsigned int sdr)
100 {
101 glDeleteShader(sdr);
102 }
104 unsigned int load_vertex_shader(const char *fname)
105 {
106 return load_shader(fname, GL_VERTEX_SHADER);
107 }
109 unsigned int load_pixel_shader(const char *fname)
110 {
111 return load_shader(fname, GL_FRAGMENT_SHADER);
112 }
114 unsigned int load_tessctl_shader(const char *fname)
115 {
116 return load_shader(fname, GL_TESS_CONTROL_SHADER);
117 }
119 unsigned int load_tesseval_shader(const char *fname)
120 {
121 return load_shader(fname, GL_TESS_EVALUATION_SHADER);
122 }
124 unsigned int load_geometry_shader(const char *fname)
125 {
126 return load_shader(fname, GL_GEOMETRY_SHADER);
127 }
129 unsigned int load_shader(const char *fname, unsigned int sdr_type)
130 {
131 #if defined(unix) || defined(__unix__)
132 struct stat st;
133 #endif
134 unsigned int sdr;
135 size_t filesize;
136 FILE *fp;
137 char *src;
139 if(!(fp = fopen(fname, "r"))) {
140 fprintf(stderr, "failed to open shader %s: %s\n", fname, strerror(errno));
141 return 0;
142 }
144 #if defined(unix) || defined(__unix__)
145 fstat(fileno(fp), &st);
146 filesize = st.st_size;
147 #else
148 fseek(fp, 0, SEEK_END);
149 filesize = ftell(fp);
150 fseek(fp, 0, SEEK_SET);
151 #endif /* unix */
153 if(!(src = malloc(filesize + 1))) {
154 fclose(fp);
155 return 0;
156 }
157 fread(src, 1, filesize, fp);
158 src[filesize] = 0;
159 fclose(fp);
161 fprintf(stderr, "compiling %s shader: %s... ", sdrtypestr(sdr_type), fname);
162 sdr = create_shader(src, sdr_type);
164 free(src);
165 return sdr;
166 }
169 unsigned int get_vertex_shader(const char *fname)
170 {
171 return get_shader(fname, GL_VERTEX_SHADER);
172 }
174 unsigned int get_pixel_shader(const char *fname)
175 {
176 return get_shader(fname, GL_FRAGMENT_SHADER);
177 }
179 unsigned int get_tessctl_shader(const char *fname)
180 {
181 return get_shader(fname, GL_TESS_CONTROL_SHADER);
182 }
184 unsigned int get_tesseval_shader(const char *fname)
185 {
186 return get_shader(fname, GL_TESS_EVALUATION_SHADER);
187 }
189 unsigned int get_geometry_shader(const char *fname)
190 {
191 return get_shader(fname, GL_GEOMETRY_SHADER);
192 }
194 unsigned int get_shader(const char *fname, unsigned int sdr_type)
195 {
196 unsigned int sdr;
197 if(!(sdr = load_shader(fname, sdr_type))) {
198 return 0;
199 }
200 return sdr;
201 }
204 /* ---- gpu programs ---- */
206 unsigned int create_program(void)
207 {
208 unsigned int prog = glCreateProgram();
209 assert(glGetError() == GL_NO_ERROR);
210 return prog;
211 }
213 unsigned int create_program_link(unsigned int sdr0, ...)
214 {
215 unsigned int prog, sdr;
216 va_list ap;
218 if(!(prog = create_program())) {
219 return 0;
220 }
222 attach_shader(prog, sdr0);
223 if(glGetError()) {
224 return 0;
225 }
227 va_start(ap, sdr0);
228 while((sdr = va_arg(ap, unsigned int))) {
229 attach_shader(prog, sdr);
230 if(glGetError()) {
231 return 0;
232 }
233 }
234 va_end(ap);
236 if(link_program(prog) == -1) {
237 free_program(prog);
238 return 0;
239 }
240 return prog;
241 }
243 unsigned int create_program_load(const char *vfile, const char *pfile)
244 {
245 unsigned int vs = 0, ps = 0;
247 if(vfile && *vfile && !(vs = get_vertex_shader(vfile))) {
248 return 0;
249 }
250 if(pfile && *pfile && !(ps = get_pixel_shader(pfile))) {
251 return 0;
252 }
253 return create_program_link(vs, ps, 0);
254 }
256 void free_program(unsigned int sdr)
257 {
258 glDeleteProgram(sdr);
259 }
261 void attach_shader(unsigned int prog, unsigned int sdr)
262 {
263 glAttachShader(prog, sdr);
264 assert(glGetError() == GL_NO_ERROR);
265 }
267 int link_program(unsigned int prog)
268 {
269 int linked, info_len, retval = 0;
270 char *info_str = 0;
272 glLinkProgram(prog);
273 assert(glGetError() == GL_NO_ERROR);
274 glGetProgramiv(prog, GL_LINK_STATUS, &linked);
275 assert(glGetError() == GL_NO_ERROR);
276 glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &info_len);
277 assert(glGetError() == GL_NO_ERROR);
279 if(info_len) {
280 if((info_str = malloc(info_len + 1))) {
281 glGetProgramInfoLog(prog, info_len, 0, info_str);
282 assert(glGetError() == GL_NO_ERROR);
283 }
284 }
286 if(linked) {
287 fprintf(stderr, info_str ? "linking done: %s\n" : "linking done\n", info_str);
288 } else {
289 fprintf(stderr, info_str ? "linking failed: %s\n" : "linking failed\n", info_str);
290 retval = -1;
291 }
293 free(info_str);
294 return retval;
295 }
297 int bind_program(unsigned int prog)
298 {
299 GLenum err;
301 glUseProgram(prog);
302 if(prog && (err = glGetError()) != GL_NO_ERROR) {
303 /* maybe the program is not linked, try linking first */
304 if(err == GL_INVALID_OPERATION) {
305 if(link_program(prog) == -1) {
306 return -1;
307 }
308 glUseProgram(prog);
309 return glGetError() == GL_NO_ERROR ? 0 : -1;
310 }
311 return -1;
312 }
313 return 0;
314 }
316 /* ugly but I'm not going to write the same bloody code over and over */
317 #define BEGIN_UNIFORM_CODE \
318 int loc, curr_prog; \
319 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); \
320 if((unsigned int)curr_prog != prog && bind_program(prog) == -1) { \
321 return -1; \
322 } \
323 if((loc = glGetUniformLocation(prog, name)) != -1)
325 #define END_UNIFORM_CODE \
326 if((unsigned int)curr_prog != prog) { \
327 bind_program(curr_prog); \
328 } \
329 return loc == -1 ? -1 : 0
331 int set_uniform_int(unsigned int prog, const char *name, int val)
332 {
333 BEGIN_UNIFORM_CODE {
334 glUniform1i(loc, val);
335 }
336 END_UNIFORM_CODE;
337 }
339 int set_uniform_float(unsigned int prog, const char *name, float val)
340 {
341 BEGIN_UNIFORM_CODE {
342 glUniform1f(loc, val);
343 }
344 END_UNIFORM_CODE;
345 }
347 int set_uniform_float2(unsigned int prog, const char *name, float x, float y)
348 {
349 BEGIN_UNIFORM_CODE {
350 glUniform2f(loc, x, y);
351 }
352 END_UNIFORM_CODE;
353 }
355 int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z)
356 {
357 BEGIN_UNIFORM_CODE {
358 glUniform3f(loc, x, y, z);
359 }
360 END_UNIFORM_CODE;
361 }
363 int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w)
364 {
365 BEGIN_UNIFORM_CODE {
366 glUniform4f(loc, x, y, z, w);
367 }
368 END_UNIFORM_CODE;
369 }
371 int set_uniform_matrix4(unsigned int prog, const char *name, float *mat)
372 {
373 BEGIN_UNIFORM_CODE {
374 glUniformMatrix4fv(loc, 1, GL_FALSE, mat);
375 }
376 END_UNIFORM_CODE;
377 }
379 int set_uniform_matrix4_transposed(unsigned int prog, const char *name, float *mat)
380 {
381 BEGIN_UNIFORM_CODE {
382 glUniformMatrix4fv(loc, 1, GL_TRUE, mat);
383 }
384 END_UNIFORM_CODE;
385 }
387 int get_attrib_loc(unsigned int prog, const char *name)
388 {
389 int loc, curr_prog;
391 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog);
392 if((unsigned int)curr_prog != prog && bind_program(prog) == -1) {
393 return -1;
394 }
396 loc = glGetAttribLocation(prog, (char*)name);
398 if((unsigned int)curr_prog != prog) {
399 bind_program(curr_prog);
400 }
401 return loc;
402 }
404 void set_attrib_float3(int attr_loc, float x, float y, float z)
405 {
406 glVertexAttrib3f(attr_loc, x, y, z);
407 }
409 static const char *sdrtypestr(unsigned int sdrtype)
410 {
411 switch(sdrtype) {
412 case GL_VERTEX_SHADER:
413 return "vertex";
414 case GL_FRAGMENT_SHADER:
415 return "pixel";
416 case GL_TESS_CONTROL_SHADER:
417 return "tessellation control";
418 case GL_TESS_EVALUATION_SHADER:
419 return "tessellation evaluation";
420 case GL_GEOMETRY_SHADER:
421 return "geometry";
423 default:
424 break;
425 }
426 return "<unknown>";
427 }