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 <stdarg.h>
|
nuclear@0
|
6 #include <assert.h>
|
nuclear@0
|
7 #include "opengl.h"
|
nuclear@0
|
8
|
nuclear@0
|
9 #if defined(unix) || defined(__unix__)
|
nuclear@0
|
10 #include <unistd.h>
|
nuclear@0
|
11 #include <sys/stat.h>
|
nuclear@0
|
12 #endif /* unix */
|
nuclear@0
|
13
|
nuclear@0
|
14 #include "sdr.h"
|
nuclear@0
|
15
|
nuclear@0
|
16 static const char *sdrtypestr(unsigned int sdrtype);
|
nuclear@0
|
17 static int sdrtypeidx(unsigned int sdrtype);
|
nuclear@0
|
18
|
nuclear@0
|
19
|
nuclear@0
|
20 unsigned int create_vertex_shader(const char *src)
|
nuclear@0
|
21 {
|
nuclear@0
|
22 return create_shader(src, GL_VERTEX_SHADER);
|
nuclear@0
|
23 }
|
nuclear@0
|
24
|
nuclear@0
|
25 unsigned int create_pixel_shader(const char *src)
|
nuclear@0
|
26 {
|
nuclear@0
|
27 return create_shader(src, GL_FRAGMENT_SHADER);
|
nuclear@0
|
28 }
|
nuclear@0
|
29
|
nuclear@0
|
30 unsigned int create_tessctl_shader(const char *src)
|
nuclear@0
|
31 {
|
nuclear@0
|
32 #ifdef GL_TESS_CONTROL_SHADER
|
nuclear@0
|
33 return create_shader(src, GL_TESS_CONTROL_SHADER);
|
nuclear@0
|
34 #else
|
nuclear@0
|
35 return 0;
|
nuclear@0
|
36 #endif
|
nuclear@0
|
37 }
|
nuclear@0
|
38
|
nuclear@0
|
39 unsigned int create_tesseval_shader(const char *src)
|
nuclear@0
|
40 {
|
nuclear@0
|
41 #ifdef GL_TESS_EVALUATION_SHADER
|
nuclear@0
|
42 return create_shader(src, GL_TESS_EVALUATION_SHADER);
|
nuclear@0
|
43 #else
|
nuclear@0
|
44 return 0;
|
nuclear@0
|
45 #endif
|
nuclear@0
|
46 }
|
nuclear@0
|
47
|
nuclear@0
|
48 unsigned int create_geometry_shader(const char *src)
|
nuclear@0
|
49 {
|
nuclear@0
|
50 #ifdef GL_GEOMETRY_SHADER
|
nuclear@0
|
51 return create_shader(src, GL_GEOMETRY_SHADER);
|
nuclear@0
|
52 #else
|
nuclear@0
|
53 return 0;
|
nuclear@0
|
54 #endif
|
nuclear@0
|
55 }
|
nuclear@0
|
56
|
nuclear@0
|
57 unsigned int create_shader(const char *src, unsigned int sdr_type)
|
nuclear@0
|
58 {
|
nuclear@0
|
59 unsigned int sdr;
|
nuclear@0
|
60 int success, info_len;
|
nuclear@0
|
61 char *info_str = 0;
|
nuclear@0
|
62 const char *src_str[3], *header, *footer;
|
nuclear@0
|
63 int src_str_count = 0;
|
nuclear@0
|
64 GLenum err;
|
nuclear@0
|
65
|
nuclear@0
|
66 if((header = get_shader_header(sdr_type))) {
|
nuclear@0
|
67 src_str[src_str_count++] = header;
|
nuclear@0
|
68 }
|
nuclear@0
|
69 src_str[src_str_count++] = src;
|
nuclear@0
|
70 if((footer = get_shader_footer(sdr_type))) {
|
nuclear@0
|
71 src_str[src_str_count++] = footer;
|
nuclear@0
|
72 }
|
nuclear@0
|
73
|
nuclear@0
|
74 sdr = glCreateShader(sdr_type);
|
nuclear@0
|
75 assert(glGetError() == GL_NO_ERROR);
|
nuclear@0
|
76 glShaderSource(sdr, src_str_count, src_str, 0);
|
nuclear@0
|
77 err = glGetError();
|
nuclear@0
|
78 assert(err == GL_NO_ERROR);
|
nuclear@0
|
79 glCompileShader(sdr);
|
nuclear@0
|
80 assert(glGetError() == GL_NO_ERROR);
|
nuclear@0
|
81
|
nuclear@0
|
82 glGetShaderiv(sdr, GL_COMPILE_STATUS, &success);
|
nuclear@0
|
83 assert(glGetError() == GL_NO_ERROR);
|
nuclear@0
|
84 glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &info_len);
|
nuclear@0
|
85 assert(glGetError() == GL_NO_ERROR);
|
nuclear@0
|
86
|
nuclear@0
|
87 if(info_len) {
|
nuclear@0
|
88 if((info_str = malloc(info_len + 1))) {
|
nuclear@0
|
89 glGetShaderInfoLog(sdr, info_len, 0, info_str);
|
nuclear@0
|
90 assert(glGetError() == GL_NO_ERROR);
|
nuclear@0
|
91 info_str[info_len] = 0;
|
nuclear@0
|
92 }
|
nuclear@0
|
93 }
|
nuclear@0
|
94
|
nuclear@0
|
95 if(success) {
|
nuclear@0
|
96 fprintf(stderr, info_str ? "done: %s\n" : "done\n", info_str);
|
nuclear@0
|
97 } else {
|
nuclear@0
|
98 fprintf(stderr, info_str ? "failed: %s\n" : "failed\n", info_str);
|
nuclear@0
|
99 glDeleteShader(sdr);
|
nuclear@0
|
100 sdr = 0;
|
nuclear@0
|
101 }
|
nuclear@0
|
102
|
nuclear@0
|
103 free(info_str);
|
nuclear@0
|
104 return sdr;
|
nuclear@0
|
105 }
|
nuclear@0
|
106
|
nuclear@0
|
107 void free_shader(unsigned int sdr)
|
nuclear@0
|
108 {
|
nuclear@0
|
109 glDeleteShader(sdr);
|
nuclear@0
|
110 }
|
nuclear@0
|
111
|
nuclear@0
|
112 unsigned int load_vertex_shader(const char *fname)
|
nuclear@0
|
113 {
|
nuclear@0
|
114 return load_shader(fname, GL_VERTEX_SHADER);
|
nuclear@0
|
115 }
|
nuclear@0
|
116
|
nuclear@0
|
117 unsigned int load_pixel_shader(const char *fname)
|
nuclear@0
|
118 {
|
nuclear@0
|
119 return load_shader(fname, GL_FRAGMENT_SHADER);
|
nuclear@0
|
120 }
|
nuclear@0
|
121
|
nuclear@0
|
122 unsigned int load_tessctl_shader(const char *fname)
|
nuclear@0
|
123 {
|
nuclear@0
|
124 #ifdef GL_TESS_CONTROL_SHADER
|
nuclear@0
|
125 return load_shader(fname, GL_TESS_CONTROL_SHADER);
|
nuclear@0
|
126 #else
|
nuclear@0
|
127 return 0;
|
nuclear@0
|
128 #endif
|
nuclear@0
|
129 }
|
nuclear@0
|
130
|
nuclear@0
|
131 unsigned int load_tesseval_shader(const char *fname)
|
nuclear@0
|
132 {
|
nuclear@0
|
133 #ifdef GL_TESS_EVALUATION_SHADER
|
nuclear@0
|
134 return load_shader(fname, GL_TESS_EVALUATION_SHADER);
|
nuclear@0
|
135 #else
|
nuclear@0
|
136 return 0;
|
nuclear@0
|
137 #endif
|
nuclear@0
|
138 }
|
nuclear@0
|
139
|
nuclear@0
|
140 unsigned int load_geometry_shader(const char *fname)
|
nuclear@0
|
141 {
|
nuclear@0
|
142 #ifdef GL_GEOMETRY_SHADER
|
nuclear@0
|
143 return load_shader(fname, GL_GEOMETRY_SHADER);
|
nuclear@0
|
144 #else
|
nuclear@0
|
145 return 0;
|
nuclear@0
|
146 #endif
|
nuclear@0
|
147 }
|
nuclear@0
|
148
|
nuclear@0
|
149 unsigned int load_shader(const char *fname, unsigned int sdr_type)
|
nuclear@0
|
150 {
|
nuclear@0
|
151 unsigned int sdr;
|
nuclear@0
|
152 size_t filesize;
|
nuclear@0
|
153 FILE *fp;
|
nuclear@0
|
154 char *src;
|
nuclear@0
|
155
|
nuclear@0
|
156 if(!(fp = fopen(fname, "rb"))) {
|
nuclear@0
|
157 fprintf(stderr, "failed to open shader %s: %s\n", fname, strerror(errno));
|
nuclear@0
|
158 return 0;
|
nuclear@0
|
159 }
|
nuclear@0
|
160
|
nuclear@0
|
161 fseek(fp, 0, SEEK_END);
|
nuclear@0
|
162 filesize = ftell(fp);
|
nuclear@0
|
163 fseek(fp, 0, SEEK_SET);
|
nuclear@0
|
164
|
nuclear@0
|
165 if(!(src = malloc(filesize + 1))) {
|
nuclear@0
|
166 fclose(fp);
|
nuclear@0
|
167 return 0;
|
nuclear@0
|
168 }
|
nuclear@0
|
169 fread(src, 1, filesize, fp);
|
nuclear@0
|
170 src[filesize] = 0;
|
nuclear@0
|
171 fclose(fp);
|
nuclear@0
|
172
|
nuclear@0
|
173 fprintf(stderr, "compiling %s shader: %s... ", sdrtypestr(sdr_type), fname);
|
nuclear@0
|
174 sdr = create_shader(src, sdr_type);
|
nuclear@0
|
175
|
nuclear@0
|
176 free(src);
|
nuclear@0
|
177 return sdr;
|
nuclear@0
|
178 }
|
nuclear@0
|
179
|
nuclear@0
|
180
|
nuclear@0
|
181 /* ---- gpu programs ---- */
|
nuclear@0
|
182
|
nuclear@0
|
183 unsigned int create_program(void)
|
nuclear@0
|
184 {
|
nuclear@0
|
185 unsigned int prog = glCreateProgram();
|
nuclear@0
|
186 assert(glGetError() == GL_NO_ERROR);
|
nuclear@0
|
187 return prog;
|
nuclear@0
|
188 }
|
nuclear@0
|
189
|
nuclear@0
|
190 unsigned int create_program_link(unsigned int sdr0, ...)
|
nuclear@0
|
191 {
|
nuclear@0
|
192 unsigned int prog, sdr;
|
nuclear@0
|
193 va_list ap;
|
nuclear@0
|
194
|
nuclear@0
|
195 if(!(prog = create_program())) {
|
nuclear@0
|
196 return 0;
|
nuclear@0
|
197 }
|
nuclear@0
|
198
|
nuclear@0
|
199 attach_shader(prog, sdr0);
|
nuclear@0
|
200 if(glGetError()) {
|
nuclear@0
|
201 return 0;
|
nuclear@0
|
202 }
|
nuclear@0
|
203
|
nuclear@0
|
204 va_start(ap, sdr0);
|
nuclear@0
|
205 while((sdr = va_arg(ap, unsigned int))) {
|
nuclear@0
|
206 attach_shader(prog, sdr);
|
nuclear@0
|
207 if(glGetError()) {
|
nuclear@0
|
208 return 0;
|
nuclear@0
|
209 }
|
nuclear@0
|
210 }
|
nuclear@0
|
211 va_end(ap);
|
nuclear@0
|
212
|
nuclear@0
|
213 if(link_program(prog) == -1) {
|
nuclear@0
|
214 free_program(prog);
|
nuclear@0
|
215 return 0;
|
nuclear@0
|
216 }
|
nuclear@0
|
217 return prog;
|
nuclear@0
|
218 }
|
nuclear@0
|
219
|
nuclear@0
|
220 unsigned int create_program_load(const char *vfile, const char *pfile)
|
nuclear@0
|
221 {
|
nuclear@0
|
222 unsigned int vs = 0, ps = 0;
|
nuclear@0
|
223
|
nuclear@0
|
224 if(vfile && *vfile && !(vs = load_vertex_shader(vfile))) {
|
nuclear@0
|
225 return 0;
|
nuclear@0
|
226 }
|
nuclear@0
|
227 if(pfile && *pfile && !(ps = load_pixel_shader(pfile))) {
|
nuclear@0
|
228 return 0;
|
nuclear@0
|
229 }
|
nuclear@0
|
230 return create_program_link(vs, ps, 0);
|
nuclear@0
|
231 }
|
nuclear@0
|
232
|
nuclear@0
|
233 void free_program(unsigned int sdr)
|
nuclear@0
|
234 {
|
nuclear@0
|
235 glDeleteProgram(sdr);
|
nuclear@0
|
236 }
|
nuclear@0
|
237
|
nuclear@0
|
238 void attach_shader(unsigned int prog, unsigned int sdr)
|
nuclear@0
|
239 {
|
nuclear@0
|
240 int err;
|
nuclear@0
|
241
|
nuclear@0
|
242 if(prog && sdr) {
|
nuclear@0
|
243 assert(glGetError() == GL_NO_ERROR);
|
nuclear@0
|
244 glAttachShader(prog, sdr);
|
nuclear@0
|
245 if((err = glGetError()) != GL_NO_ERROR) {
|
nuclear@0
|
246 fprintf(stderr, "failed to attach shader %u to program %u (err: 0x%x)\n", sdr, prog, err);
|
nuclear@0
|
247 abort();
|
nuclear@0
|
248 }
|
nuclear@0
|
249 }
|
nuclear@0
|
250 }
|
nuclear@0
|
251
|
nuclear@0
|
252 int link_program(unsigned int prog)
|
nuclear@0
|
253 {
|
nuclear@0
|
254 int linked, info_len, retval = 0;
|
nuclear@0
|
255 char *info_str = 0;
|
nuclear@0
|
256
|
nuclear@0
|
257 glLinkProgram(prog);
|
nuclear@0
|
258 assert(glGetError() == GL_NO_ERROR);
|
nuclear@0
|
259 glGetProgramiv(prog, GL_LINK_STATUS, &linked);
|
nuclear@0
|
260 assert(glGetError() == GL_NO_ERROR);
|
nuclear@0
|
261 glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &info_len);
|
nuclear@0
|
262 assert(glGetError() == GL_NO_ERROR);
|
nuclear@0
|
263
|
nuclear@0
|
264 if(info_len) {
|
nuclear@0
|
265 if((info_str = malloc(info_len + 1))) {
|
nuclear@0
|
266 glGetProgramInfoLog(prog, info_len, 0, info_str);
|
nuclear@0
|
267 assert(glGetError() == GL_NO_ERROR);
|
nuclear@0
|
268 info_str[info_len] = 0;
|
nuclear@0
|
269 }
|
nuclear@0
|
270 }
|
nuclear@0
|
271
|
nuclear@0
|
272 if(linked) {
|
nuclear@0
|
273 fprintf(stderr, info_str ? "linking done: %s\n" : "linking done\n", info_str);
|
nuclear@0
|
274 } else {
|
nuclear@0
|
275 fprintf(stderr, info_str ? "linking failed: %s\n" : "linking failed\n", info_str);
|
nuclear@0
|
276 retval = -1;
|
nuclear@0
|
277 }
|
nuclear@0
|
278
|
nuclear@0
|
279 free(info_str);
|
nuclear@0
|
280 return retval;
|
nuclear@0
|
281 }
|
nuclear@0
|
282
|
nuclear@0
|
283 int bind_program(unsigned int prog)
|
nuclear@0
|
284 {
|
nuclear@0
|
285 GLenum err;
|
nuclear@0
|
286
|
nuclear@0
|
287 glUseProgram(prog);
|
nuclear@0
|
288 if(prog && (err = glGetError()) != GL_NO_ERROR) {
|
nuclear@0
|
289 /* maybe the program is not linked, try linking first */
|
nuclear@0
|
290 if(err == GL_INVALID_OPERATION) {
|
nuclear@0
|
291 if(link_program(prog) == -1) {
|
nuclear@0
|
292 return -1;
|
nuclear@0
|
293 }
|
nuclear@0
|
294 glUseProgram(prog);
|
nuclear@0
|
295 return glGetError() == GL_NO_ERROR ? 0 : -1;
|
nuclear@0
|
296 }
|
nuclear@0
|
297 return -1;
|
nuclear@0
|
298 }
|
nuclear@0
|
299 return 0;
|
nuclear@0
|
300 }
|
nuclear@0
|
301
|
nuclear@0
|
302 /* ugly but I'm not going to write the same bloody code over and over */
|
nuclear@0
|
303 #define BEGIN_UNIFORM_CODE \
|
nuclear@0
|
304 int loc, curr_prog; \
|
nuclear@0
|
305 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); \
|
nuclear@0
|
306 if((unsigned int)curr_prog != prog && bind_program(prog) == -1) { \
|
nuclear@0
|
307 return -1; \
|
nuclear@0
|
308 } \
|
nuclear@0
|
309 if((loc = glGetUniformLocation(prog, name)) != -1)
|
nuclear@0
|
310
|
nuclear@0
|
311 #define END_UNIFORM_CODE \
|
nuclear@0
|
312 if((unsigned int)curr_prog != prog) { \
|
nuclear@0
|
313 bind_program(curr_prog); \
|
nuclear@0
|
314 } \
|
nuclear@0
|
315 return loc == -1 ? -1 : 0
|
nuclear@0
|
316
|
nuclear@0
|
317 int get_uniform_loc(unsigned int prog, const char *name)
|
nuclear@0
|
318 {
|
nuclear@0
|
319 int loc, curr_prog;
|
nuclear@0
|
320 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog);
|
nuclear@0
|
321 if((unsigned int)curr_prog != prog && bind_program(prog) == -1) {
|
nuclear@0
|
322 return -1;
|
nuclear@0
|
323 }
|
nuclear@0
|
324 loc = glGetUniformLocation(prog, name);
|
nuclear@0
|
325 if((unsigned int)curr_prog != prog) {
|
nuclear@0
|
326 bind_program(curr_prog);
|
nuclear@0
|
327 }
|
nuclear@0
|
328 return loc;
|
nuclear@0
|
329 }
|
nuclear@0
|
330
|
nuclear@0
|
331 int set_uniform_int(unsigned int prog, const char *name, int val)
|
nuclear@0
|
332 {
|
nuclear@0
|
333 BEGIN_UNIFORM_CODE {
|
nuclear@0
|
334 glUniform1i(loc, val);
|
nuclear@0
|
335 }
|
nuclear@0
|
336 END_UNIFORM_CODE;
|
nuclear@0
|
337 }
|
nuclear@0
|
338
|
nuclear@0
|
339 int set_uniform_float(unsigned int prog, const char *name, float val)
|
nuclear@0
|
340 {
|
nuclear@0
|
341 BEGIN_UNIFORM_CODE {
|
nuclear@0
|
342 glUniform1f(loc, val);
|
nuclear@0
|
343 }
|
nuclear@0
|
344 END_UNIFORM_CODE;
|
nuclear@0
|
345 }
|
nuclear@0
|
346
|
nuclear@0
|
347 int set_uniform_float2(unsigned int prog, const char *name, float x, float y)
|
nuclear@0
|
348 {
|
nuclear@0
|
349 BEGIN_UNIFORM_CODE {
|
nuclear@0
|
350 glUniform2f(loc, x, y);
|
nuclear@0
|
351 }
|
nuclear@0
|
352 END_UNIFORM_CODE;
|
nuclear@0
|
353 }
|
nuclear@0
|
354
|
nuclear@0
|
355 int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z)
|
nuclear@0
|
356 {
|
nuclear@0
|
357 BEGIN_UNIFORM_CODE {
|
nuclear@0
|
358 glUniform3f(loc, x, y, z);
|
nuclear@0
|
359 }
|
nuclear@0
|
360 END_UNIFORM_CODE;
|
nuclear@0
|
361 }
|
nuclear@0
|
362
|
nuclear@0
|
363 int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w)
|
nuclear@0
|
364 {
|
nuclear@0
|
365 BEGIN_UNIFORM_CODE {
|
nuclear@0
|
366 glUniform4f(loc, x, y, z, w);
|
nuclear@0
|
367 }
|
nuclear@0
|
368 END_UNIFORM_CODE;
|
nuclear@0
|
369 }
|
nuclear@0
|
370
|
nuclear@0
|
371 int set_uniform_matrix4(unsigned int prog, const char *name, const float *mat)
|
nuclear@0
|
372 {
|
nuclear@0
|
373 BEGIN_UNIFORM_CODE {
|
nuclear@0
|
374 glUniformMatrix4fv(loc, 1, GL_FALSE, mat);
|
nuclear@0
|
375 }
|
nuclear@0
|
376 END_UNIFORM_CODE;
|
nuclear@0
|
377 }
|
nuclear@0
|
378
|
nuclear@0
|
379 int set_uniform_matrix4_transposed(unsigned int prog, const char *name, const float *mat)
|
nuclear@0
|
380 {
|
nuclear@0
|
381 BEGIN_UNIFORM_CODE {
|
nuclear@0
|
382 glUniformMatrix4fv(loc, 1, GL_TRUE, mat);
|
nuclear@0
|
383 }
|
nuclear@0
|
384 END_UNIFORM_CODE;
|
nuclear@0
|
385 }
|
nuclear@0
|
386
|
nuclear@0
|
387 int get_attrib_loc(unsigned int prog, const char *name)
|
nuclear@0
|
388 {
|
nuclear@0
|
389 int loc, curr_prog;
|
nuclear@0
|
390
|
nuclear@0
|
391 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog);
|
nuclear@0
|
392 if((unsigned int)curr_prog != prog && bind_program(prog) == -1) {
|
nuclear@0
|
393 return -1;
|
nuclear@0
|
394 }
|
nuclear@0
|
395
|
nuclear@0
|
396 loc = glGetAttribLocation(prog, (char*)name);
|
nuclear@0
|
397
|
nuclear@0
|
398 if((unsigned int)curr_prog != prog) {
|
nuclear@0
|
399 bind_program(curr_prog);
|
nuclear@0
|
400 }
|
nuclear@0
|
401 return loc;
|
nuclear@0
|
402 }
|
nuclear@0
|
403
|
nuclear@0
|
404 void set_attrib_float3(int attr_loc, float x, float y, float z)
|
nuclear@0
|
405 {
|
nuclear@0
|
406 glVertexAttrib3f(attr_loc, x, y, z);
|
nuclear@0
|
407 }
|
nuclear@0
|
408
|
nuclear@0
|
409 /* ---- shader composition ---- */
|
nuclear@0
|
410 struct string {
|
nuclear@0
|
411 char *text;
|
nuclear@0
|
412 int len;
|
nuclear@0
|
413 };
|
nuclear@0
|
414
|
nuclear@0
|
415 #define NUM_SHADER_TYPES 5
|
nuclear@0
|
416 static struct string header[NUM_SHADER_TYPES];
|
nuclear@0
|
417 static struct string footer[NUM_SHADER_TYPES];
|
nuclear@0
|
418
|
nuclear@0
|
419 static void clear_string(struct string *str)
|
nuclear@0
|
420 {
|
nuclear@0
|
421 free(str->text);
|
nuclear@0
|
422 str->text = 0;
|
nuclear@0
|
423 str->len = 0;
|
nuclear@0
|
424 }
|
nuclear@0
|
425
|
nuclear@0
|
426 static void append_string(struct string *str, const char *s)
|
nuclear@0
|
427 {
|
nuclear@0
|
428 int len, newlen;
|
nuclear@0
|
429 char *newstr;
|
nuclear@0
|
430
|
nuclear@0
|
431 if(!s || !*s) return;
|
nuclear@0
|
432
|
nuclear@0
|
433 len = strlen(s);
|
nuclear@0
|
434 newlen = str->len + len;
|
nuclear@0
|
435 if(!(newstr = malloc(newlen + 2))) { /* leave space for a possible newline */
|
nuclear@0
|
436 fprintf(stderr, "shader composition: failed to append string of size %d\n", len);
|
nuclear@0
|
437 abort();
|
nuclear@0
|
438 }
|
nuclear@0
|
439
|
nuclear@0
|
440 if(str->text) {
|
nuclear@0
|
441 memcpy(newstr, str->text, str->len);
|
nuclear@0
|
442 }
|
nuclear@0
|
443 memcpy(newstr + str->len, s, len + 1);
|
nuclear@0
|
444
|
nuclear@0
|
445 if(s[len - 1] != '\n') {
|
nuclear@0
|
446 newstr[newlen] = '\n';
|
nuclear@0
|
447 newstr[newlen + 1] = 0;
|
nuclear@0
|
448 }
|
nuclear@0
|
449
|
nuclear@0
|
450 free(str->text);
|
nuclear@0
|
451 str->text = newstr;
|
nuclear@0
|
452 str->len = newlen;
|
nuclear@0
|
453 }
|
nuclear@0
|
454
|
nuclear@0
|
455 void clear_shader_header(unsigned int type)
|
nuclear@0
|
456 {
|
nuclear@0
|
457 if(type) {
|
nuclear@0
|
458 int idx = sdrtypeidx(type);
|
nuclear@0
|
459 clear_string(&header[idx]);
|
nuclear@0
|
460 } else {
|
nuclear@0
|
461 int i;
|
nuclear@0
|
462 for(i=0; i<NUM_SHADER_TYPES; i++) {
|
nuclear@0
|
463 clear_string(&header[i]);
|
nuclear@0
|
464 }
|
nuclear@0
|
465 }
|
nuclear@0
|
466 }
|
nuclear@0
|
467
|
nuclear@0
|
468 void clear_shader_footer(unsigned int type)
|
nuclear@0
|
469 {
|
nuclear@0
|
470 if(type) {
|
nuclear@0
|
471 int idx = sdrtypeidx(type);
|
nuclear@0
|
472 clear_string(&footer[idx]);
|
nuclear@0
|
473 } else {
|
nuclear@0
|
474 int i;
|
nuclear@0
|
475 for(i=0; i<NUM_SHADER_TYPES; i++) {
|
nuclear@0
|
476 clear_string(&footer[i]);
|
nuclear@0
|
477 }
|
nuclear@0
|
478 }
|
nuclear@0
|
479 }
|
nuclear@0
|
480
|
nuclear@0
|
481 void add_shader_header(unsigned int type, const char *s)
|
nuclear@0
|
482 {
|
nuclear@0
|
483 if(type) {
|
nuclear@0
|
484 int idx = sdrtypeidx(type);
|
nuclear@0
|
485 append_string(&header[idx], s);
|
nuclear@0
|
486 } else {
|
nuclear@0
|
487 int i;
|
nuclear@0
|
488 for(i=0; i<NUM_SHADER_TYPES; i++) {
|
nuclear@0
|
489 append_string(&header[i], s);
|
nuclear@0
|
490 }
|
nuclear@0
|
491 }
|
nuclear@0
|
492 }
|
nuclear@0
|
493
|
nuclear@0
|
494 void add_shader_footer(unsigned int type, const char *s)
|
nuclear@0
|
495 {
|
nuclear@0
|
496 if(type) {
|
nuclear@0
|
497 int idx = sdrtypeidx(type);
|
nuclear@0
|
498 append_string(&footer[idx], s);
|
nuclear@0
|
499 } else {
|
nuclear@0
|
500 int i;
|
nuclear@0
|
501 for(i=0; i<NUM_SHADER_TYPES; i++) {
|
nuclear@0
|
502 append_string(&footer[i], s);
|
nuclear@0
|
503 }
|
nuclear@0
|
504 }
|
nuclear@0
|
505 }
|
nuclear@0
|
506
|
nuclear@0
|
507 const char *get_shader_header(unsigned int type)
|
nuclear@0
|
508 {
|
nuclear@0
|
509 int idx = sdrtypeidx(type);
|
nuclear@0
|
510 return header[idx].text;
|
nuclear@0
|
511 }
|
nuclear@0
|
512
|
nuclear@0
|
513 const char *get_shader_footer(unsigned int type)
|
nuclear@0
|
514 {
|
nuclear@0
|
515 int idx = sdrtypeidx(type);
|
nuclear@0
|
516 return footer[idx].text;
|
nuclear@0
|
517 }
|
nuclear@0
|
518
|
nuclear@0
|
519 static const char *sdrtypestr(unsigned int sdrtype)
|
nuclear@0
|
520 {
|
nuclear@0
|
521 switch(sdrtype) {
|
nuclear@0
|
522 case GL_VERTEX_SHADER:
|
nuclear@0
|
523 return "vertex";
|
nuclear@0
|
524 case GL_FRAGMENT_SHADER:
|
nuclear@0
|
525 return "pixel";
|
nuclear@0
|
526 #ifdef GL_TESS_CONTROL_SHADER
|
nuclear@0
|
527 case GL_TESS_CONTROL_SHADER:
|
nuclear@0
|
528 return "tessellation control";
|
nuclear@0
|
529 #endif
|
nuclear@0
|
530 #ifdef GL_TESS_EVALUATION_SHADER
|
nuclear@0
|
531 case GL_TESS_EVALUATION_SHADER:
|
nuclear@0
|
532 return "tessellation evaluation";
|
nuclear@0
|
533 #endif
|
nuclear@0
|
534 #ifdef GL_GEOMETRY_SHADER
|
nuclear@0
|
535 case GL_GEOMETRY_SHADER:
|
nuclear@0
|
536 return "geometry";
|
nuclear@0
|
537 #endif
|
nuclear@0
|
538
|
nuclear@0
|
539 default:
|
nuclear@0
|
540 break;
|
nuclear@0
|
541 }
|
nuclear@0
|
542 return "<unknown>";
|
nuclear@0
|
543 }
|
nuclear@0
|
544
|
nuclear@0
|
545 static int sdrtypeidx(unsigned int sdrtype)
|
nuclear@0
|
546 {
|
nuclear@0
|
547 switch(sdrtype) {
|
nuclear@0
|
548 case GL_VERTEX_SHADER:
|
nuclear@0
|
549 return 0;
|
nuclear@0
|
550 case GL_FRAGMENT_SHADER:
|
nuclear@0
|
551 return 1;
|
nuclear@0
|
552 case GL_TESS_CONTROL_SHADER:
|
nuclear@0
|
553 return 2;
|
nuclear@0
|
554 case GL_TESS_EVALUATION_SHADER:
|
nuclear@0
|
555 return 3;
|
nuclear@0
|
556 case GL_GEOMETRY_SHADER:
|
nuclear@0
|
557 return 4;
|
nuclear@0
|
558 default:
|
nuclear@0
|
559 break;
|
nuclear@0
|
560 }
|
nuclear@0
|
561 return 0;
|
nuclear@0
|
562 }
|