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