Fix GLSL shader failing to compile on OSX
[blender.git] / source / blender / gpu / intern / gpu_shader.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2005 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Brecht Van Lommel.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include "MEM_guardedalloc.h"
29
30 #include "BLI_blenlib.h"
31 #include "BLI_utildefines.h"
32 #include "BLI_math_base.h"
33 #include "BLI_math_vector.h"
34
35 #include "BKE_global.h"
36
37 #include "GPU_compositing.h"
38 #include "GPU_debug.h"
39 #include "GPU_extensions.h"
40 #include "GPU_glew.h"
41 #include "GPU_shader.h"
42 #include "GPU_texture.h"
43
44 /* TODO(sergey): Find better default values for this constants. */
45 #define MAX_DEFINE_LENGTH 1024
46 #define MAX_EXT_DEFINE_LENGTH 1024
47
48 /* Non-generated shaders */
49 extern char datatoc_gpu_shader_smoke_vert_glsl[];
50 extern char datatoc_gpu_shader_smoke_frag_glsl[];
51 extern char datatoc_gpu_shader_vsm_store_vert_glsl[];
52 extern char datatoc_gpu_shader_vsm_store_frag_glsl[];
53 extern char datatoc_gpu_shader_sep_gaussian_blur_vert_glsl[];
54 extern char datatoc_gpu_shader_sep_gaussian_blur_frag_glsl[];
55 extern char datatoc_gpu_shader_fx_vert_glsl[];
56 extern char datatoc_gpu_shader_fx_ssao_frag_glsl[];
57 extern char datatoc_gpu_shader_fx_dof_frag_glsl[];
58 extern char datatoc_gpu_shader_fx_dof_vert_glsl[];
59 extern char datatoc_gpu_shader_fx_dof_hq_frag_glsl[];
60 extern char datatoc_gpu_shader_fx_dof_hq_vert_glsl[];
61 extern char datatoc_gpu_shader_fx_dof_hq_geo_glsl[];
62 extern char datatoc_gpu_shader_fx_depth_resolve_glsl[];
63 extern char datatoc_gpu_shader_fx_lib_glsl[];
64
65 static struct GPUShadersGlobal {
66         struct {
67                 GPUShader *vsm_store;
68                 GPUShader *sep_gaussian_blur;
69                 GPUShader *smoke;
70                 GPUShader *smoke_fire;
71                 /* cache for shader fx. Those can exist in combinations so store them here */
72                 GPUShader *fx_shaders[MAX_FX_SHADERS * 2];
73         } shaders;
74 } GG = {{NULL}};
75
76 /* GPUShader */
77
78 struct GPUShader {
79         GLuint program;  /* handle for full program (links shader stages below) */
80
81         GLuint vertex;   /* handle for vertex shader */
82         GLuint geometry; /* handle for geometry shader */
83         GLuint fragment; /* handle for fragment shader */
84
85         int totattrib;   /* total number of attributes */
86         int uniforms;    /* required uniforms */
87
88         void *uniform_interface; /* cached uniform interface for shader. Data depends on shader */
89 };
90
91 static void shader_print_errors(const char *task, const char *log, const char **code, int totcode)
92 {
93         int i;
94         int line = 1;
95
96         fprintf(stderr, "GPUShader: %s error:\n", task);
97
98         for (i = 0; i < totcode; i++) {
99                 const char *c, *pos, *end = code[i] + strlen(code[i]);
100
101                 if (G.debug & G_DEBUG) {
102                         fprintf(stderr, "===== shader string %d ====\n", i + 1);
103
104                         c = code[i];
105                         while ((c < end) && (pos = strchr(c, '\n'))) {
106                                 fprintf(stderr, "%2d  ", line);
107                                 fwrite(c, (pos + 1) - c, 1, stderr);
108                                 c = pos + 1;
109                                 line++;
110                         }
111                         
112                         fprintf(stderr, "%s", c);
113                 }
114         }
115         
116         fprintf(stderr, "%s\n", log);
117 }
118
119 static const char *gpu_shader_version(void)
120 {
121         if (GLEW_VERSION_3_2) {
122                 if (GLEW_ARB_compatibility) {
123                         return "#version 150 compatibility\n";
124                         /* highest version that is widely supported
125                          * gives us native geometry shaders!
126                          * use compatibility profile so we can continue using builtin shader input/output names
127                          */
128                 }
129                 else {
130                         return "#version 130\n";
131                         /* latest version that is compatible with existing shaders */
132                 }
133         }
134         else if (GLEW_VERSION_3_1) {
135                 if (GLEW_ARB_compatibility) {
136                         return "#version 140\n";
137                         /* also need the ARB_compatibility extension, handled below */
138                 }
139                 else {
140                         return "#version 130\n";
141                         /* latest version that is compatible with existing shaders */
142                 }
143         }
144         else if (GLEW_VERSION_3_0) {
145                 return "#version 130\n";
146                 /* GLSL 1.3 has modern syntax/keywords/datatypes so use if available
147                  * older features are deprecated but still available without compatibility extension or profile
148                  */
149         }
150         else {
151                 return "#version 120\n";
152                 /* minimum supported */
153         }
154 }
155
156
157 static void gpu_shader_standard_extensions(char defines[MAX_EXT_DEFINE_LENGTH], bool use_geometry_shader)
158 {
159         /* enable extensions for features that are not part of our base GLSL version
160          * don't use an extension for something already available!
161          */
162
163         if (GLEW_ARB_texture_query_lod) {
164                 /* a #version 400 feature, but we use #version 150 maximum so use extension */
165                 strcat(defines, "#extension GL_ARB_texture_query_lod: enable\n");
166         }
167
168         if (use_geometry_shader && GPU_geometry_shader_support_via_extension()) {
169                 strcat(defines, "#extension GL_EXT_geometry_shader4: enable\n");
170         }
171
172         if (GLEW_VERSION_3_1 && !GLEW_VERSION_3_2 && GLEW_ARB_compatibility) {
173                 strcat(defines, "#extension GL_ARB_compatibility: enable\n");
174         }
175
176         if (!GLEW_VERSION_3_1) {
177                 if (GLEW_ARB_draw_instanced) {
178                         strcat(defines, "#extension GL_ARB_draw_instanced: enable\n");
179                 }
180
181                 if (!GLEW_VERSION_3_0 && GLEW_EXT_gpu_shader4) {
182                         strcat(defines, "#extension GL_EXT_gpu_shader4: enable\n");
183                         /* TODO: maybe require this? shaders become so much nicer */
184                 }
185         }
186 }
187
188 static void gpu_shader_standard_defines(char defines[MAX_DEFINE_LENGTH],
189                                         bool use_opensubdiv,
190                                         bool use_new_shading)
191 {
192         /* some useful defines to detect GPU type */
193         if (GPU_type_matches(GPU_DEVICE_ATI, GPU_OS_ANY, GPU_DRIVER_ANY)) {
194                 strcat(defines, "#define GPU_ATI\n");
195                 if (GLEW_VERSION_3_0) {
196                         /* TODO(merwin): revisit this version check; GLEW_VERSION_3_0 means GL 3.0 or newer */
197                         strcat(defines, "#define CLIP_WORKAROUND\n");
198                 }
199         }
200         else if (GPU_type_matches(GPU_DEVICE_NVIDIA, GPU_OS_ANY, GPU_DRIVER_ANY))
201                 strcat(defines, "#define GPU_NVIDIA\n");
202         else if (GPU_type_matches(GPU_DEVICE_INTEL, GPU_OS_ANY, GPU_DRIVER_ANY))
203                 strcat(defines, "#define GPU_INTEL\n");
204
205         if (GPU_bicubic_bump_support())
206                 strcat(defines, "#define BUMP_BICUBIC\n");
207
208         if (GLEW_VERSION_3_0) {
209                 strcat(defines, "#define BIT_OPERATIONS\n");
210         }
211
212 #ifdef WITH_OPENSUBDIV
213         /* TODO(sergey): Check whether we actually compiling shader for
214          * the OpenSubdiv mesh.
215          */
216         if (use_opensubdiv) {
217                 strcat(defines, "#define USE_OPENSUBDIV\n");
218
219                 /* TODO(sergey): not strictly speaking a define, but this is
220                  * a global typedef which we don't have better place to define
221                  * in yet.
222                  */
223                 strcat(defines, "struct VertexData {\n"
224                                 "  vec4 position;\n"
225                                 "  vec3 normal;\n"
226                                 "  vec2 uv;"
227                                 "};\n");
228         }
229 #else
230         UNUSED_VARS(use_opensubdiv);
231 #endif
232
233         if (use_new_shading) {
234                 strcat(defines, "#define USE_NEW_SHADING\n");
235         }
236
237         return;
238 }
239
240 GPUShader *GPU_shader_create(const char *vertexcode,
241                              const char *fragcode,
242                              const char *geocode,
243                              const char *libcode,
244                              const char *defines,
245                              int input,
246                              int output,
247                              int number)
248 {
249         return GPU_shader_create_ex(vertexcode,
250                                     fragcode,
251                                     geocode,
252                                     libcode,
253                                     defines,
254                                     input,
255                                     output,
256                                     number,
257                                     GPU_SHADER_FLAGS_NONE);
258 }
259
260 GPUShader *GPU_shader_create_ex(const char *vertexcode,
261                                 const char *fragcode,
262                                 const char *geocode,
263                                 const char *libcode,
264                                 const char *defines,
265                                 int input,
266                                 int output,
267                                 int number,
268                                 const int flags)
269 {
270 #ifdef WITH_OPENSUBDIV
271         /* TODO(sergey): used to add #version 150 to the geometry shader.
272          * Could safely be renamed to "use_geometry_code" since it's very
273          * likely any of geometry code will want to use GLSL 1.5.
274          */
275         bool use_opensubdiv = (flags & GPU_SHADER_FLAGS_SPECIAL_OPENSUBDIV) != 0;
276 #else
277         UNUSED_VARS(flags);
278         bool use_opensubdiv = false;
279 #endif
280         GLint status;
281         GLchar log[5000];
282         GLsizei length = 0;
283         GPUShader *shader;
284         char standard_defines[MAX_DEFINE_LENGTH] = "";
285         char standard_extensions[MAX_EXT_DEFINE_LENGTH] = "";
286
287         if (geocode && !GPU_geometry_shader_support())
288                 return NULL;
289
290         shader = MEM_callocN(sizeof(GPUShader), "GPUShader");
291
292         if (vertexcode)
293                 shader->vertex = glCreateShader(GL_VERTEX_SHADER);
294         if (fragcode)
295                 shader->fragment = glCreateShader(GL_FRAGMENT_SHADER);
296         if (geocode)
297                 shader->geometry = glCreateShader(GL_GEOMETRY_SHADER_EXT);
298
299         shader->program = glCreateProgram();
300
301         if (!shader->program ||
302             (vertexcode && !shader->vertex) ||
303             (fragcode && !shader->fragment) ||
304             (geocode && !shader->geometry))
305         {
306                 fprintf(stderr, "GPUShader, object creation failed.\n");
307                 GPU_shader_free(shader);
308                 return NULL;
309         }
310
311         gpu_shader_standard_defines(standard_defines,
312                                     use_opensubdiv,
313                                     (flags & GPU_SHADER_FLAGS_NEW_SHADING) != 0);
314         gpu_shader_standard_extensions(standard_extensions, geocode != NULL);
315
316         if (vertexcode) {
317                 const char *source[5];
318                 /* custom limit, may be too small, beware */
319                 int num_source = 0;
320
321                 source[num_source++] = gpu_shader_version();
322                 source[num_source++] = standard_extensions;
323                 source[num_source++] = standard_defines;
324
325                 if (defines) source[num_source++] = defines;
326                 source[num_source++] = vertexcode;
327
328                 glAttachShader(shader->program, shader->vertex);
329                 glShaderSource(shader->vertex, num_source, source, NULL);
330
331                 glCompileShader(shader->vertex);
332                 glGetShaderiv(shader->vertex, GL_COMPILE_STATUS, &status);
333
334                 if (!status) {
335                         glGetShaderInfoLog(shader->vertex, sizeof(log), &length, log);
336                         shader_print_errors("compile", log, source, num_source);
337
338                         GPU_shader_free(shader);
339                         return NULL;
340                 }
341         }
342
343         if (fragcode) {
344                 const char *source[7];
345                 int num_source = 0;
346
347                 source[num_source++] = gpu_shader_version();
348                 source[num_source++] = standard_extensions;
349                 source[num_source++] = standard_defines;
350
351 #ifdef WITH_OPENSUBDIV
352                 /* TODO(sergey): Move to fragment shader source code generation. */
353                 if (use_opensubdiv) {
354                         source[num_source++] =
355                                 "#ifdef USE_OPENSUBDIV\n"
356                                 "in block {\n"
357                                 "       VertexData v;\n"
358                                 "} inpt;\n"
359                                 "#endif\n";
360                 }
361 #endif
362
363                 if (defines) source[num_source++] = defines;
364                 if (libcode) source[num_source++] = libcode;
365                 source[num_source++] = fragcode;
366
367                 glAttachShader(shader->program, shader->fragment);
368                 glShaderSource(shader->fragment, num_source, source, NULL);
369
370                 glCompileShader(shader->fragment);
371                 glGetShaderiv(shader->fragment, GL_COMPILE_STATUS, &status);
372
373                 if (!status) {
374                         glGetShaderInfoLog(shader->fragment, sizeof(log), &length, log);
375                         shader_print_errors("compile", log, source, num_source);
376
377                         GPU_shader_free(shader);
378                         return NULL;
379                 }
380         }
381
382         if (geocode) {
383                 const char *source[6];
384                 int num_source = 0;
385
386                 source[num_source++] = gpu_shader_version();
387                 source[num_source++] = standard_extensions;
388                 source[num_source++] = standard_defines;
389
390                 if (defines) source[num_source++] = defines;
391                 source[num_source++] = geocode;
392
393                 glAttachShader(shader->program, shader->geometry);
394                 glShaderSource(shader->geometry, num_source, source, NULL);
395
396                 glCompileShader(shader->geometry);
397                 glGetShaderiv(shader->geometry, GL_COMPILE_STATUS, &status);
398
399                 if (!status) {
400                         glGetShaderInfoLog(shader->geometry, sizeof(log), &length, log);
401                         shader_print_errors("compile", log, source, num_source);
402
403                         GPU_shader_free(shader);
404                         return NULL;
405                 }
406                 
407                 if (!use_opensubdiv) {
408                         GPU_shader_geometry_stage_primitive_io(shader, input, output, number);
409                 }
410         }
411
412 #ifdef WITH_OPENSUBDIV
413         if (use_opensubdiv) {
414                 glBindAttribLocation(shader->program, 0, "position");
415                 glBindAttribLocation(shader->program, 1, "normal");
416                 GPU_shader_geometry_stage_primitive_io(shader,
417                                                        GL_LINES_ADJACENCY_EXT,
418                                                        GL_TRIANGLE_STRIP,
419                                                        4);
420         }
421 #endif
422
423         glLinkProgram(shader->program);
424         glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
425         if (!status) {
426                 glGetProgramInfoLog(shader->program, sizeof(log), &length, log);
427                 /* print attached shaders in pipeline order */
428                 if (vertexcode) shader_print_errors("linking", log, &vertexcode, 1);
429                 if (geocode) shader_print_errors("linking", log, &geocode, 1);
430                 if (libcode) shader_print_errors("linking", log, &libcode, 1);
431                 if (fragcode) shader_print_errors("linking", log, &fragcode, 1);
432
433                 GPU_shader_free(shader);
434                 return NULL;
435         }
436
437 #ifdef WITH_OPENSUBDIV
438         /* TODO(sergey): Find a better place for this. */
439         if (use_opensubdiv && GLEW_VERSION_4_1) {
440                 glProgramUniform1i(shader->program,
441                                    glGetUniformLocation(shader->program, "FVarDataBuffer"),
442                                    31);  /* GL_TEXTURE31 */
443         }
444 #endif
445
446         return shader;
447 }
448
449 void GPU_shader_bind(GPUShader *shader)
450 {
451         GPU_ASSERT_NO_GL_ERRORS("Pre Shader Bind");
452         glUseProgram(shader->program);
453         GPU_ASSERT_NO_GL_ERRORS("Post Shader Bind");
454 }
455
456 void GPU_shader_unbind(void)
457 {
458         GPU_ASSERT_NO_GL_ERRORS("Pre Shader Unbind");
459         glUseProgram(0);
460         GPU_ASSERT_NO_GL_ERRORS("Post Shader Unbind");
461 }
462
463 void GPU_shader_free(GPUShader *shader)
464 {
465         if (shader->vertex)
466                 glDeleteShader(shader->vertex);
467         if (shader->geometry)
468                 glDeleteShader(shader->geometry);
469         if (shader->fragment)
470                 glDeleteShader(shader->fragment);
471         if (shader->program)
472                 glDeleteProgram(shader->program);
473
474         if (shader->uniform_interface)
475                 MEM_freeN(shader->uniform_interface);
476
477         MEM_freeN(shader);
478 }
479
480 int GPU_shader_get_uniform(GPUShader *shader, const char *name)
481 {
482         return glGetUniformLocation(shader->program, name);
483 }
484
485 void *GPU_shader_get_interface(GPUShader *shader)
486 {
487         return shader->uniform_interface;
488 }
489
490 void GPU_shader_set_interface(GPUShader *shader, void *interface)
491 {
492         shader->uniform_interface = interface;
493 }
494
495 void GPU_shader_uniform_vector(GPUShader *UNUSED(shader), int location, int length, int arraysize, const float *value)
496 {
497         if (location == -1 || value == NULL)
498                 return;
499
500         GPU_ASSERT_NO_GL_ERRORS("Pre Uniform Vector");
501
502         if (length == 1) glUniform1fv(location, arraysize, value);
503         else if (length == 2) glUniform2fv(location, arraysize, value);
504         else if (length == 3) glUniform3fv(location, arraysize, value);
505         else if (length == 4) glUniform4fv(location, arraysize, value);
506         else if (length == 9) glUniformMatrix3fv(location, arraysize, 0, value);
507         else if (length == 16) glUniformMatrix4fv(location, arraysize, 0, value);
508
509         GPU_ASSERT_NO_GL_ERRORS("Post Uniform Vector");
510 }
511
512 void GPU_shader_uniform_vector_int(GPUShader *UNUSED(shader), int location, int length, int arraysize, const int *value)
513 {
514         if (location == -1)
515                 return;
516
517         GPU_ASSERT_NO_GL_ERRORS("Pre Uniform Vector");
518
519         if (length == 1) glUniform1iv(location, arraysize, value);
520         else if (length == 2) glUniform2iv(location, arraysize, value);
521         else if (length == 3) glUniform3iv(location, arraysize, value);
522         else if (length == 4) glUniform4iv(location, arraysize, value);
523
524         GPU_ASSERT_NO_GL_ERRORS("Post Uniform Vector");
525 }
526
527 void GPU_shader_uniform_int(GPUShader *UNUSED(shader), int location, int value)
528 {
529         if (location == -1)
530                 return;
531
532         GPU_CHECK_ERRORS_AROUND(glUniform1i(location, value));
533 }
534
535 void GPU_shader_geometry_stage_primitive_io(GPUShader *shader, int input, int output, int number)
536 {
537         if (GPU_geometry_shader_support_via_extension()) {
538                 /* geometry shaders must provide this info themselves for #version 150 and up */
539                 glProgramParameteriEXT(shader->program, GL_GEOMETRY_INPUT_TYPE_EXT, input);
540                 glProgramParameteriEXT(shader->program, GL_GEOMETRY_OUTPUT_TYPE_EXT, output);
541                 glProgramParameteriEXT(shader->program, GL_GEOMETRY_VERTICES_OUT_EXT, number);
542         }
543 }
544
545 void GPU_shader_uniform_texture(GPUShader *UNUSED(shader), int location, GPUTexture *tex)
546 {
547         GLenum arbnumber;
548         int number = GPU_texture_bound_number(tex);
549         int bindcode = GPU_texture_opengl_bindcode(tex);
550         int target = GPU_texture_target(tex);
551
552         if (number >= GPU_max_textures()) {
553                 fprintf(stderr, "Not enough texture slots.\n");
554                 return;
555         }
556                 
557         if (number == -1)
558                 return;
559
560         if (location == -1)
561                 return;
562
563         GPU_ASSERT_NO_GL_ERRORS("Pre Uniform Texture");
564
565         arbnumber = (GLenum)((GLuint)GL_TEXTURE0 + number);
566
567         if (number != 0) glActiveTexture(arbnumber);
568         if (bindcode != 0)
569                 glBindTexture(target, bindcode);
570         else
571                 GPU_invalid_tex_bind(target);
572         glUniform1i(location, number);
573         glEnable(target);
574         if (number != 0) glActiveTexture(GL_TEXTURE0);
575
576         GPU_ASSERT_NO_GL_ERRORS("Post Uniform Texture");
577 }
578
579 int GPU_shader_get_attribute(GPUShader *shader, const char *name)
580 {
581         int index;
582         
583         GPU_CHECK_ERRORS_AROUND(index = glGetAttribLocation(shader->program, name));
584
585         return index;
586 }
587
588 GPUShader *GPU_shader_get_builtin_shader(GPUBuiltinShader shader)
589 {
590         GPUShader *retval = NULL;
591
592         switch (shader) {
593                 case GPU_SHADER_VSM_STORE:
594                         if (!GG.shaders.vsm_store)
595                                 GG.shaders.vsm_store = GPU_shader_create(
596                                         datatoc_gpu_shader_vsm_store_vert_glsl, datatoc_gpu_shader_vsm_store_frag_glsl,
597                                         NULL, NULL, NULL, 0, 0, 0);
598                         retval = GG.shaders.vsm_store;
599                         break;
600                 case GPU_SHADER_SEP_GAUSSIAN_BLUR:
601                         if (!GG.shaders.sep_gaussian_blur)
602                                 GG.shaders.sep_gaussian_blur = GPU_shader_create(
603                                         datatoc_gpu_shader_sep_gaussian_blur_vert_glsl,
604                                         datatoc_gpu_shader_sep_gaussian_blur_frag_glsl,
605                                         NULL, NULL, NULL, 0, 0, 0);
606                         retval = GG.shaders.sep_gaussian_blur;
607                         break;
608                 case GPU_SHADER_SMOKE:
609                         if (!GG.shaders.smoke)
610                                 GG.shaders.smoke = GPU_shader_create(
611                                         datatoc_gpu_shader_smoke_vert_glsl, datatoc_gpu_shader_smoke_frag_glsl,
612                                         NULL, NULL, NULL, 0, 0, 0);
613                         retval = GG.shaders.smoke;
614                         break;
615                 case GPU_SHADER_SMOKE_FIRE:
616                         if (!GG.shaders.smoke_fire)
617                                 GG.shaders.smoke_fire = GPU_shader_create(
618                                         datatoc_gpu_shader_smoke_vert_glsl, datatoc_gpu_shader_smoke_frag_glsl,
619                                         NULL, NULL, "#define USE_FIRE;\n", 0, 0, 0);
620                         retval = GG.shaders.smoke_fire;
621                         break;
622         }
623
624         if (retval == NULL)
625                 printf("Unable to create a GPUShader for builtin shader: %u\n", shader);
626
627         return retval;
628 }
629
630 #define MAX_DEFINES 100
631
632 GPUShader *GPU_shader_get_builtin_fx_shader(int effect, bool persp)
633 {
634         int offset;
635         char defines[MAX_DEFINES] = "";
636         /* avoid shaders out of range */
637         if (effect >= MAX_FX_SHADERS)
638                 return NULL;
639
640         offset = 2 * effect;
641
642         if (persp) {
643                 offset += 1;
644                 strcat(defines, "#define PERSP_MATRIX\n");
645         }
646
647         if (!GG.shaders.fx_shaders[offset]) {
648                 GPUShader *shader = NULL;
649
650                 switch (effect) {
651                         case GPU_SHADER_FX_SSAO:
652                                 shader = GPU_shader_create(datatoc_gpu_shader_fx_vert_glsl, datatoc_gpu_shader_fx_ssao_frag_glsl, NULL, datatoc_gpu_shader_fx_lib_glsl, defines, 0, 0, 0);
653                                 break;
654
655                         case GPU_SHADER_FX_DEPTH_OF_FIELD_PASS_ONE:
656                                 strcat(defines, "#define FIRST_PASS\n");
657                                 shader = GPU_shader_create(datatoc_gpu_shader_fx_dof_vert_glsl, datatoc_gpu_shader_fx_dof_frag_glsl, NULL, datatoc_gpu_shader_fx_lib_glsl, defines, 0, 0, 0);
658                                 break;
659
660                         case GPU_SHADER_FX_DEPTH_OF_FIELD_PASS_TWO:
661                                 strcat(defines, "#define SECOND_PASS\n");
662                                 shader = GPU_shader_create(datatoc_gpu_shader_fx_dof_vert_glsl, datatoc_gpu_shader_fx_dof_frag_glsl, NULL, datatoc_gpu_shader_fx_lib_glsl, defines, 0, 0, 0);
663                                 break;
664
665                         case GPU_SHADER_FX_DEPTH_OF_FIELD_PASS_THREE:
666                                 strcat(defines, "#define THIRD_PASS\n");
667                                 shader = GPU_shader_create(datatoc_gpu_shader_fx_dof_vert_glsl, datatoc_gpu_shader_fx_dof_frag_glsl, NULL, datatoc_gpu_shader_fx_lib_glsl, defines, 0, 0, 0);
668                                 break;
669
670                         case GPU_SHADER_FX_DEPTH_OF_FIELD_PASS_FOUR:
671                                 strcat(defines, "#define FOURTH_PASS\n");
672                                 shader = GPU_shader_create(datatoc_gpu_shader_fx_dof_vert_glsl, datatoc_gpu_shader_fx_dof_frag_glsl, NULL, datatoc_gpu_shader_fx_lib_glsl, defines, 0, 0, 0);
673                                 break;
674
675                         case GPU_SHADER_FX_DEPTH_OF_FIELD_PASS_FIVE:
676                                 strcat(defines, "#define FIFTH_PASS\n");
677                                 shader = GPU_shader_create(datatoc_gpu_shader_fx_dof_vert_glsl, datatoc_gpu_shader_fx_dof_frag_glsl, NULL, datatoc_gpu_shader_fx_lib_glsl, defines, 0, 0, 0);
678                                 break;
679
680                         case GPU_SHADER_FX_DEPTH_OF_FIELD_HQ_PASS_ONE:
681                                 strcat(defines, "#define FIRST_PASS\n");
682                                 shader = GPU_shader_create(datatoc_gpu_shader_fx_dof_hq_vert_glsl, datatoc_gpu_shader_fx_dof_hq_frag_glsl, NULL, datatoc_gpu_shader_fx_lib_glsl, defines, 0, 0, 0);
683                                 break;
684
685                         case GPU_SHADER_FX_DEPTH_OF_FIELD_HQ_PASS_TWO:
686                                 strcat(defines, "#define SECOND_PASS\n");
687                                 shader = GPU_shader_create(datatoc_gpu_shader_fx_dof_hq_vert_glsl, datatoc_gpu_shader_fx_dof_hq_frag_glsl, datatoc_gpu_shader_fx_dof_hq_geo_glsl, datatoc_gpu_shader_fx_lib_glsl,
688                                                            defines, GL_POINTS, GL_TRIANGLE_STRIP, 4);
689                                 break;
690
691                         case GPU_SHADER_FX_DEPTH_OF_FIELD_HQ_PASS_THREE:
692                                 strcat(defines, "#define THIRD_PASS\n");
693                                 shader = GPU_shader_create(datatoc_gpu_shader_fx_dof_hq_vert_glsl, datatoc_gpu_shader_fx_dof_hq_frag_glsl, NULL, datatoc_gpu_shader_fx_lib_glsl, defines, 0, 0, 0);
694                                 break;
695
696                         case GPU_SHADER_FX_DEPTH_RESOLVE:
697                                 shader = GPU_shader_create(datatoc_gpu_shader_fx_vert_glsl, datatoc_gpu_shader_fx_depth_resolve_glsl, NULL, NULL, defines, 0, 0, 0);
698                                 break;
699                 }
700
701                 GG.shaders.fx_shaders[offset] = shader;
702                 GPU_fx_shader_init_interface(shader, effect);
703         }
704
705         return GG.shaders.fx_shaders[offset];
706 }
707
708
709 void GPU_shader_free_builtin_shaders(void)
710 {
711         int i;
712
713         if (GG.shaders.vsm_store) {
714                 GPU_shader_free(GG.shaders.vsm_store);
715                 GG.shaders.vsm_store = NULL;
716         }
717
718         if (GG.shaders.sep_gaussian_blur) {
719                 GPU_shader_free(GG.shaders.sep_gaussian_blur);
720                 GG.shaders.sep_gaussian_blur = NULL;
721         }
722
723         if (GG.shaders.smoke) {
724                 GPU_shader_free(GG.shaders.smoke);
725                 GG.shaders.smoke = NULL;
726         }
727
728         if (GG.shaders.smoke_fire) {
729                 GPU_shader_free(GG.shaders.smoke_fire);
730                 GG.shaders.smoke_fire = NULL;
731         }
732
733         for (i = 0; i < 2 * MAX_FX_SHADERS; ++i) {
734                 if (GG.shaders.fx_shaders[i]) {
735                         GPU_shader_free(GG.shaders.fx_shaders[i]);
736                         GG.shaders.fx_shaders[i] = NULL;
737                 }
738         }
739 }
740
741