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