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