--debug-gpu-shader: Dump GLSL shaders to disk
[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_utildefines.h"
31 #include "BLI_math_base.h"
32 #include "BLI_math_vector.h"
33 #include "BLI_path_util.h"
34
35 #include "BKE_appdir.h"
36 #include "BKE_global.h"
37
38 #include "DNA_space_types.h"
39
40 #include "GPU_compositing.h"
41 #include "GPU_extensions.h"
42 #include "GPU_matrix.h"
43 #include "GPU_shader.h"
44 #include "GPU_texture.h"
45 #include "GPU_uniformbuffer.h"
46
47 #include "gpu_shader_private.h"
48
49 /* Adjust these constants as needed. */
50 #define MAX_DEFINE_LENGTH 256
51 #define MAX_EXT_DEFINE_LENGTH 256
52
53 /* Non-generated shaders */
54 extern char datatoc_gpu_shader_depth_only_frag_glsl[];
55 extern char datatoc_gpu_shader_uniform_color_frag_glsl[];
56 extern char datatoc_gpu_shader_checker_frag_glsl[];
57 extern char datatoc_gpu_shader_diag_stripes_frag_glsl[];
58 extern char datatoc_gpu_shader_simple_lighting_frag_glsl[];
59 extern char datatoc_gpu_shader_simple_lighting_flat_color_frag_glsl[];
60 extern char datatoc_gpu_shader_simple_lighting_smooth_color_frag_glsl[];
61 extern char datatoc_gpu_shader_simple_lighting_smooth_color_alpha_frag_glsl[];
62 extern char datatoc_gpu_shader_flat_color_frag_glsl[];
63 extern char datatoc_gpu_shader_flat_color_alpha_test_0_frag_glsl[];
64 extern char datatoc_gpu_shader_2D_vert_glsl[];
65 extern char datatoc_gpu_shader_2D_flat_color_vert_glsl[];
66 extern char datatoc_gpu_shader_2D_smooth_color_vert_glsl[];
67 extern char datatoc_gpu_shader_2D_smooth_color_frag_glsl[];
68 extern char datatoc_gpu_shader_2D_image_vert_glsl[];
69
70 extern char datatoc_gpu_shader_3D_image_vert_glsl[];
71 extern char datatoc_gpu_shader_image_linear_frag_glsl[];
72 extern char datatoc_gpu_shader_image_color_frag_glsl[];
73 extern char datatoc_gpu_shader_image_alpha_color_frag_glsl[];
74 extern char datatoc_gpu_shader_image_shuffle_color_frag_glsl[];
75 extern char datatoc_gpu_shader_image_interlace_frag_glsl[];
76 extern char datatoc_gpu_shader_image_mask_uniform_color_frag_glsl[];
77 extern char datatoc_gpu_shader_image_modulate_alpha_frag_glsl[];
78 extern char datatoc_gpu_shader_image_rect_modulate_alpha_frag_glsl[];
79 extern char datatoc_gpu_shader_image_depth_linear_frag_glsl[];
80 extern char datatoc_gpu_shader_3D_vert_glsl[];
81 extern char datatoc_gpu_shader_3D_normal_vert_glsl[];
82 extern char datatoc_gpu_shader_3D_flat_color_vert_glsl[];
83 extern char datatoc_gpu_shader_3D_smooth_color_vert_glsl[];
84 extern char datatoc_gpu_shader_3D_normal_flat_color_vert_glsl[];
85 extern char datatoc_gpu_shader_3D_normal_smooth_color_vert_glsl[];
86 extern char datatoc_gpu_shader_3D_smooth_color_frag_glsl[];
87 extern char datatoc_gpu_shader_3D_passthrough_vert_glsl[];
88 extern char datatoc_gpu_shader_3D_clipped_uniform_color_vert_glsl[];
89
90 extern char datatoc_gpu_shader_instance_vert_glsl[];
91 extern char datatoc_gpu_shader_instance_variying_size_variying_color_vert_glsl[];
92 extern char datatoc_gpu_shader_instance_objectspace_variying_color_vert_glsl[];
93 extern char datatoc_gpu_shader_instance_screenspace_variying_color_vert_glsl[];
94 extern char datatoc_gpu_shader_instance_screen_aligned_vert_glsl[];
95 extern char datatoc_gpu_shader_instance_camera_vert_glsl[];
96 extern char datatoc_gpu_shader_instance_distance_line_vert_glsl[];
97 extern char datatoc_gpu_shader_instance_edges_variying_color_geom_glsl[];
98 extern char datatoc_gpu_shader_instance_edges_variying_color_vert_glsl[];
99 extern char datatoc_gpu_shader_instance_bone_envelope_solid_vert_glsl[];
100 extern char datatoc_gpu_shader_instance_bone_envelope_wire_vert_glsl[];
101
102 extern char datatoc_gpu_shader_3D_groundpoint_vert_glsl[];
103 extern char datatoc_gpu_shader_3D_groundline_geom_glsl[];
104
105 extern char datatoc_gpu_shader_point_uniform_color_frag_glsl[];
106 extern char datatoc_gpu_shader_point_uniform_color_aa_frag_glsl[];
107 extern char datatoc_gpu_shader_point_uniform_color_outline_aa_frag_glsl[];
108 extern char datatoc_gpu_shader_point_varying_color_outline_aa_frag_glsl[];
109 extern char datatoc_gpu_shader_point_varying_color_frag_glsl[];
110 extern char datatoc_gpu_shader_3D_point_fixed_size_varying_color_vert_glsl[];
111 extern char datatoc_gpu_shader_3D_point_varying_size_vert_glsl[];
112 extern char datatoc_gpu_shader_3D_point_varying_size_varying_color_vert_glsl[];
113 extern char datatoc_gpu_shader_3D_point_uniform_size_aa_vert_glsl[];
114 extern char datatoc_gpu_shader_3D_point_uniform_size_outline_aa_vert_glsl[];
115 extern char datatoc_gpu_shader_2D_point_varying_size_varying_color_vert_glsl[];
116 extern char datatoc_gpu_shader_2D_point_uniform_size_aa_vert_glsl[];
117 extern char datatoc_gpu_shader_2D_point_uniform_size_outline_aa_vert_glsl[];
118 extern char datatoc_gpu_shader_2D_point_uniform_size_varying_color_outline_aa_vert_glsl[];
119
120 extern char datatoc_gpu_shader_2D_line_dashed_uniform_color_vert_glsl[];
121 extern char datatoc_gpu_shader_2D_line_dashed_frag_glsl[];
122 extern char datatoc_gpu_shader_2D_line_dashed_geom_glsl[];
123 extern char datatoc_gpu_shader_3D_line_dashed_uniform_color_legacy_vert_glsl[];
124 extern char datatoc_gpu_shader_3D_line_dashed_uniform_color_vert_glsl[];
125
126 extern char datatoc_gpu_shader_edges_front_back_persp_vert_glsl[];
127 extern char datatoc_gpu_shader_edges_front_back_persp_geom_glsl[];
128 extern char datatoc_gpu_shader_edges_front_back_persp_legacy_vert_glsl[];
129 extern char datatoc_gpu_shader_edges_front_back_ortho_vert_glsl[];
130 extern char datatoc_gpu_shader_edges_overlay_vert_glsl[];
131 extern char datatoc_gpu_shader_edges_overlay_geom_glsl[];
132 extern char datatoc_gpu_shader_edges_overlay_simple_geom_glsl[];
133 extern char datatoc_gpu_shader_edges_overlay_frag_glsl[];
134 extern char datatoc_gpu_shader_text_vert_glsl[];
135 extern char datatoc_gpu_shader_text_frag_glsl[];
136 extern char datatoc_gpu_shader_keyframe_diamond_vert_glsl[];
137 extern char datatoc_gpu_shader_keyframe_diamond_frag_glsl[];
138
139 extern char datatoc_gpu_shader_fire_frag_glsl[];
140 extern char datatoc_gpu_shader_smoke_vert_glsl[];
141 extern char datatoc_gpu_shader_smoke_frag_glsl[];
142 extern char datatoc_gpu_shader_vsm_store_vert_glsl[];
143 extern char datatoc_gpu_shader_vsm_store_frag_glsl[];
144 extern char datatoc_gpu_shader_sep_gaussian_blur_vert_glsl[];
145 extern char datatoc_gpu_shader_sep_gaussian_blur_frag_glsl[];
146 extern char datatoc_gpu_shader_fullscreen_vert_glsl[];
147 extern char datatoc_gpu_shader_fx_ssao_frag_glsl[];
148 extern char datatoc_gpu_shader_fx_dof_frag_glsl[];
149 extern char datatoc_gpu_shader_fx_dof_vert_glsl[];
150 extern char datatoc_gpu_shader_fx_dof_hq_frag_glsl[];
151 extern char datatoc_gpu_shader_fx_dof_hq_vert_glsl[];
152 extern char datatoc_gpu_shader_fx_dof_hq_geo_glsl[];
153 extern char datatoc_gpu_shader_fx_depth_resolve_glsl[];
154 extern char datatoc_gpu_shader_fx_lib_glsl[];
155
156 /* cache of built-in shaders (each is created on first use) */
157 static GPUShader *builtin_shaders[GPU_NUM_BUILTIN_SHADERS] = { NULL };
158
159 /* cache for shader fx. Those can exist in combinations so store them here */
160 static GPUShader *fx_shaders[MAX_FX_SHADERS * 2] = { NULL };
161
162 typedef struct {
163         const char *vert;
164         const char *frag;
165         const char *geom; /* geometry stage runs between vert & frag, but is less common, so it goes last */
166 } GPUShaderStages;
167
168 static void shader_print_errors(const char *task, const char *log, const char **code, int totcode)
169 {
170         int line = 1;
171
172         fprintf(stderr, "GPUShader: %s error:\n", task);
173
174         for (int i = 0; i < totcode; i++) {
175                 const char *c, *pos, *end = code[i] + strlen(code[i]);
176
177                 if (G.debug & G_DEBUG) {
178                         fprintf(stderr, "===== shader string %d ====\n", i + 1);
179
180                         c = code[i];
181                         while ((c < end) && (pos = strchr(c, '\n'))) {
182                                 fprintf(stderr, "%2d  ", line);
183                                 fwrite(c, (pos + 1) - c, 1, stderr);
184                                 c = pos + 1;
185                                 line++;
186                         }
187                         
188                         fprintf(stderr, "%s", c);
189                 }
190         }
191         
192         fprintf(stderr, "%s\n", log);
193 }
194
195 static const char *gpu_shader_version(void)
196 {
197         return "#version 330\n";
198 }
199
200 static void gpu_shader_standard_extensions(char defines[MAX_EXT_DEFINE_LENGTH])
201 {
202         /* enable extensions for features that are not part of our base GLSL version
203          * don't use an extension for something already available!
204          */
205
206         if (GLEW_ARB_texture_query_lod) {
207                 /* a #version 400 feature, but we use #version 330 maximum so use extension */
208                 strcat(defines, "#extension GL_ARB_texture_query_lod: enable\n");
209         }
210 }
211
212 static void gpu_shader_standard_defines(char defines[MAX_DEFINE_LENGTH],
213                                         bool use_opensubdiv,
214                                         bool use_new_shading)
215 {
216         /* some useful defines to detect GPU type */
217         if (GPU_type_matches(GPU_DEVICE_ATI, GPU_OS_ANY, GPU_DRIVER_ANY)) {
218                 strcat(defines, "#define GPU_ATI\n");
219                 if (GLEW_VERSION_3_0) {
220                         /* TODO(merwin): revisit this version check; GLEW_VERSION_3_0 means GL 3.0 or newer */
221                         strcat(defines, "#define CLIP_WORKAROUND\n");
222                 }
223         }
224         else if (GPU_type_matches(GPU_DEVICE_NVIDIA, GPU_OS_ANY, GPU_DRIVER_ANY))
225                 strcat(defines, "#define GPU_NVIDIA\n");
226         else if (GPU_type_matches(GPU_DEVICE_INTEL, GPU_OS_ANY, GPU_DRIVER_ANY))
227                 strcat(defines, "#define GPU_INTEL\n");
228
229         if (GPU_bicubic_bump_support())
230                 strcat(defines, "#define BUMP_BICUBIC\n");
231
232 #ifdef WITH_OPENSUBDIV
233         /* TODO(sergey): Check whether we actually compiling shader for
234          * the OpenSubdiv mesh.
235          */
236         if (use_opensubdiv) {
237                 strcat(defines, "#define USE_OPENSUBDIV\n");
238
239                 /* TODO(sergey): not strictly speaking a define, but this is
240                  * a global typedef which we don't have better place to define
241                  * in yet.
242                  */
243                 strcat(defines, "struct VertexData {\n"
244                                 "  vec4 position;\n"
245                                 "  vec3 normal;\n"
246                                 "  vec2 uv;"
247                                 "};\n");
248         }
249 #else
250         UNUSED_VARS(use_opensubdiv);
251 #endif
252
253         if (use_new_shading) {
254                 strcat(defines, "#define USE_NEW_SHADING\n");
255         }
256
257         return;
258 }
259
260 GPUShader *GPU_shader_create(const char *vertexcode,
261                              const char *fragcode,
262                              const char *geocode,
263                              const char *libcode,
264                              const char *defines)
265 {
266         return GPU_shader_create_ex(vertexcode,
267                                     fragcode,
268                                     geocode,
269                                     libcode,
270                                     defines,
271                                     GPU_SHADER_FLAGS_NONE);
272 }
273
274 #define DEBUG_SHADER_NONE ""
275 #define DEBUG_SHADER_VERTEX "vert"
276 #define DEBUG_SHADER_FRAGMENT "frag"
277 #define DEBUG_SHADER_GEOMETRY "geom"
278
279 /**
280  * Dump GLSL shaders to disk
281  *
282  * This is used for profiling shader performance externally and debug if shader code is correct.
283  * If called with no code, it simply bumps the shader index, so different shaders for the same
284  * program share the same index.
285  */
286 static void gpu_dump_shaders(const char **code, const int num_shaders, const char *extension)
287 {
288         if ((G.debug & G_DEBUG_GPU_SHADERS) == 0) {
289                 return;
290         }
291
292         /* We use the same shader index for shaders in the same program.
293          * So we call this function once before calling for the invidual shaders. */
294         static int shader_index = 0;
295         if (code == NULL) {
296                 shader_index++;
297                 BLI_assert(STREQ(DEBUG_SHADER_NONE, extension));
298                 return;
299         }
300
301         /* Determine the full path of the new shader. */
302         char shader_path[FILE_MAX];
303
304         char file_name[512] = {'\0'};
305         sprintf(file_name, "%04d.%s", shader_index, extension);
306
307         BLI_join_dirfile(shader_path, sizeof(shader_path), BKE_tempdir_session(), file_name);
308
309         /* Write shader to disk. */
310         FILE *f = fopen(shader_path, "w");
311         if (f == NULL) {
312                 printf("Error writing to file: %s\n", shader_path);
313         }
314         for (int j = 0; j < num_shaders; j++) {
315                 fprintf(f, "%s", code[j]);
316         }
317         fclose(f);
318         printf("Shader file written to disk: %s\n", shader_path);
319 }
320
321 GPUShader *GPU_shader_create_ex(const char *vertexcode,
322                                 const char *fragcode,
323                                 const char *geocode,
324                                 const char *libcode,
325                                 const char *defines,
326                                 const int flags)
327 {
328 #ifdef WITH_OPENSUBDIV
329         bool use_opensubdiv = (flags & GPU_SHADER_FLAGS_SPECIAL_OPENSUBDIV) != 0;
330 #else
331         UNUSED_VARS(flags);
332         bool use_opensubdiv = false;
333 #endif
334         GLint status;
335         GLchar log[5000];
336         GLsizei length = 0;
337         GPUShader *shader;
338         char standard_defines[MAX_DEFINE_LENGTH] = "";
339         char standard_extensions[MAX_EXT_DEFINE_LENGTH] = "";
340
341         shader = MEM_callocN(sizeof(GPUShader), "GPUShader");
342         gpu_dump_shaders(NULL, 0, DEBUG_SHADER_NONE);
343
344         if (vertexcode)
345                 shader->vertex = glCreateShader(GL_VERTEX_SHADER);
346         if (fragcode)
347                 shader->fragment = glCreateShader(GL_FRAGMENT_SHADER);
348         if (geocode)
349                 shader->geometry = glCreateShader(GL_GEOMETRY_SHADER_EXT);
350
351         shader->program = glCreateProgram();
352
353         if (!shader->program ||
354             (vertexcode && !shader->vertex) ||
355             (fragcode && !shader->fragment) ||
356             (geocode && !shader->geometry))
357         {
358                 fprintf(stderr, "GPUShader, object creation failed.\n");
359                 GPU_shader_free(shader);
360                 return NULL;
361         }
362
363         gpu_shader_standard_defines(standard_defines,
364                                     use_opensubdiv,
365                                     (flags & GPU_SHADER_FLAGS_NEW_SHADING) != 0);
366         gpu_shader_standard_extensions(standard_extensions);
367
368         if (vertexcode) {
369                 const char *source[5];
370                 /* custom limit, may be too small, beware */
371                 int num_source = 0;
372
373                 source[num_source++] = gpu_shader_version();
374                 source[num_source++] = standard_extensions;
375                 source[num_source++] = standard_defines;
376
377                 if (defines) source[num_source++] = defines;
378                 source[num_source++] = vertexcode;
379
380                 gpu_dump_shaders(source, num_source, DEBUG_SHADER_VERTEX);
381
382                 glAttachShader(shader->program, shader->vertex);
383                 glShaderSource(shader->vertex, num_source, source, NULL);
384
385                 glCompileShader(shader->vertex);
386                 glGetShaderiv(shader->vertex, GL_COMPILE_STATUS, &status);
387
388                 if (!status) {
389                         glGetShaderInfoLog(shader->vertex, sizeof(log), &length, log);
390                         shader_print_errors("compile", log, source, num_source);
391
392                         GPU_shader_free(shader);
393                         return NULL;
394                 }
395         }
396
397         if (fragcode) {
398                 const char *source[7];
399                 int num_source = 0;
400
401                 source[num_source++] = gpu_shader_version();
402                 source[num_source++] = standard_extensions;
403                 source[num_source++] = standard_defines;
404
405 #ifdef WITH_OPENSUBDIV
406                 /* TODO(sergey): Move to fragment shader source code generation. */
407                 if (use_opensubdiv) {
408                         source[num_source++] =
409                                 "#ifdef USE_OPENSUBDIV\n"
410                                 "in block {\n"
411                                 "       VertexData v;\n"
412                                 "} inpt;\n"
413                                 "#endif\n";
414                 }
415 #endif
416
417                 if (defines) source[num_source++] = defines;
418                 if (libcode) source[num_source++] = libcode;
419                 source[num_source++] = fragcode;
420
421                 gpu_dump_shaders(source, num_source, DEBUG_SHADER_FRAGMENT);
422
423                 glAttachShader(shader->program, shader->fragment);
424                 glShaderSource(shader->fragment, num_source, source, NULL);
425
426                 glCompileShader(shader->fragment);
427                 glGetShaderiv(shader->fragment, GL_COMPILE_STATUS, &status);
428
429                 if (!status) {
430                         glGetShaderInfoLog(shader->fragment, sizeof(log), &length, log);
431                         shader_print_errors("compile", log, source, num_source);
432
433                         GPU_shader_free(shader);
434                         return NULL;
435                 }
436         }
437
438         if (geocode) {
439                 const char *source[6];
440                 int num_source = 0;
441
442                 source[num_source++] = gpu_shader_version();
443                 source[num_source++] = standard_extensions;
444                 source[num_source++] = standard_defines;
445
446                 if (defines) source[num_source++] = defines;
447                 source[num_source++] = geocode;
448
449                 gpu_dump_shaders(source, num_source, DEBUG_SHADER_GEOMETRY);
450
451                 glAttachShader(shader->program, shader->geometry);
452                 glShaderSource(shader->geometry, num_source, source, NULL);
453
454                 glCompileShader(shader->geometry);
455                 glGetShaderiv(shader->geometry, GL_COMPILE_STATUS, &status);
456
457                 if (!status) {
458                         glGetShaderInfoLog(shader->geometry, sizeof(log), &length, log);
459                         shader_print_errors("compile", log, source, num_source);
460
461                         GPU_shader_free(shader);
462                         return NULL;
463                 }
464         }
465
466 #ifdef WITH_OPENSUBDIV
467         if (use_opensubdiv) {
468                 glBindAttribLocation(shader->program, 0, "position");
469                 glBindAttribLocation(shader->program, 1, "normal");
470         }
471 #endif
472
473         glLinkProgram(shader->program);
474         glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
475         if (!status) {
476                 glGetProgramInfoLog(shader->program, sizeof(log), &length, log);
477                 /* print attached shaders in pipeline order */
478                 if (vertexcode) shader_print_errors("linking", log, &vertexcode, 1);
479                 if (geocode) shader_print_errors("linking", log, &geocode, 1);
480                 if (libcode) shader_print_errors("linking", log, &libcode, 1);
481                 if (fragcode) shader_print_errors("linking", log, &fragcode, 1);
482
483                 GPU_shader_free(shader);
484                 return NULL;
485         }
486
487         shader->interface = GWN_shaderinterface_create(shader->program);
488
489 #ifdef WITH_OPENSUBDIV
490         /* TODO(sergey): Find a better place for this. */
491         if (use_opensubdiv) {
492                 if (GLEW_VERSION_4_1) {
493                         glProgramUniform1i(shader->program,
494                                            GWN_shaderinterface_uniform(shader->interface, "FVarDataOffsetBuffer")->location,
495                                            30);  /* GL_TEXTURE30 */
496
497                         glProgramUniform1i(shader->program,
498                                            GWN_shaderinterface_uniform(shader->interface, "FVarDataBuffer")->location,
499                                            31);  /* GL_TEXTURE31 */
500                 }
501                 else {
502                         glUseProgram(shader->program);
503                         glUniform1i(GWN_shaderinterface_uniform(shader->interface, "FVarDataOffsetBuffer")->location, 30);
504                         glUniform1i(GWN_shaderinterface_uniform(shader->interface, "FVarDataBuffer")->location, 31);
505                         glUseProgram(0);
506                 }
507         }
508 #endif
509
510         return shader;
511 }
512
513 #undef DEBUG_SHADER_GEOMETRY
514 #undef DEBUG_SHADER_FRAGMENT
515 #undef DEBUG_SHADER_VERTEX
516 #undef DEBUG_SHADER_NONE
517
518 void GPU_shader_bind(GPUShader *shader)
519 {
520         BLI_assert(shader && shader->program);
521
522         glUseProgram(shader->program);
523         gpuBindMatrices(shader->interface);
524 }
525
526 void GPU_shader_unbind(void)
527 {
528         glUseProgram(0);
529 }
530
531 void GPU_shader_free(GPUShader *shader)
532 {
533         BLI_assert(shader);
534
535         if (shader->vertex)
536                 glDeleteShader(shader->vertex);
537         if (shader->geometry)
538                 glDeleteShader(shader->geometry);
539         if (shader->fragment)
540                 glDeleteShader(shader->fragment);
541         if (shader->program)
542                 glDeleteProgram(shader->program);
543
544         if (shader->uniform_interface)
545                 MEM_freeN(shader->uniform_interface);
546
547         if (shader->interface)
548                 GWN_shaderinterface_discard(shader->interface);
549
550         MEM_freeN(shader);
551 }
552
553 int GPU_shader_get_uniform(GPUShader *shader, const char *name)
554 {
555         BLI_assert(shader && shader->program);
556         const Gwn_ShaderInput *uniform = GWN_shaderinterface_uniform(shader->interface, name);
557         return uniform ? uniform->location : -1;
558 }
559
560 int GPU_shader_get_uniform_block(GPUShader *shader, const char *name)
561 {
562         BLI_assert(shader && shader->program);
563
564         return glGetUniformBlockIndex(shader->program, name);
565 }
566
567 void *GPU_fx_shader_get_interface(GPUShader *shader)
568 {
569         return shader->uniform_interface;
570 }
571
572 void *GPU_shader_get_interface(GPUShader *shader)
573 {
574         return shader->interface;
575 }
576
577 /* Clement : Temp */
578 int GPU_shader_get_program(GPUShader *shader)
579 {
580         return (int)shader->program;
581 }
582
583 void GPU_fx_shader_set_interface(GPUShader *shader, void *interface)
584 {
585         shader->uniform_interface = interface;
586 }
587
588 void GPU_shader_uniform_vector(GPUShader *UNUSED(shader), int location, int length, int arraysize, const float *value)
589 {
590         if (location == -1 || value == NULL)
591                 return;
592
593         if (length == 1) glUniform1fv(location, arraysize, value);
594         else if (length == 2) glUniform2fv(location, arraysize, value);
595         else if (length == 3) glUniform3fv(location, arraysize, value);
596         else if (length == 4) glUniform4fv(location, arraysize, value);
597         else if (length == 9) glUniformMatrix3fv(location, arraysize, 0, value);
598         else if (length == 16) glUniformMatrix4fv(location, arraysize, 0, value);
599 }
600
601 void GPU_shader_uniform_vector_int(GPUShader *UNUSED(shader), int location, int length, int arraysize, const int *value)
602 {
603         if (location == -1)
604                 return;
605
606         if (length == 1) glUniform1iv(location, arraysize, value);
607         else if (length == 2) glUniform2iv(location, arraysize, value);
608         else if (length == 3) glUniform3iv(location, arraysize, value);
609         else if (length == 4) glUniform4iv(location, arraysize, value);
610 }
611
612 void GPU_shader_uniform_int(GPUShader *UNUSED(shader), int location, int value)
613 {
614         if (location == -1)
615                 return;
616
617         glUniform1i(location, value);
618 }
619
620 void GPU_shader_uniform_buffer(GPUShader *shader, int location, GPUUniformBuffer *ubo)
621 {
622         int bindpoint = GPU_uniformbuffer_bindpoint(ubo);
623
624         if (location == -1) {
625                 return;
626         }
627
628         glUniformBlockBinding(shader->program, location, bindpoint);
629 }
630
631 void GPU_shader_uniform_texture(GPUShader *UNUSED(shader), int location, GPUTexture *tex)
632 {
633         int number = GPU_texture_bound_number(tex);
634         int bindcode = GPU_texture_opengl_bindcode(tex);
635         int target = GPU_texture_target(tex);
636
637         if (number >= GPU_max_textures()) {
638                 fprintf(stderr, "Not enough texture slots.\n");
639                 return;
640         }
641                 
642         if (number == -1)
643                 return;
644
645         if (location == -1)
646                 return;
647
648         if (number != 0)
649                 glActiveTexture(GL_TEXTURE0 + number);
650
651         if (bindcode != 0)
652                 glBindTexture(target, bindcode);
653         else
654                 GPU_invalid_tex_bind(target);
655
656         glUniform1i(location, number);
657
658         if (number != 0)
659                 glActiveTexture(GL_TEXTURE0);
660 }
661
662 int GPU_shader_get_attribute(GPUShader *shader, const char *name)
663 {
664         BLI_assert(shader && shader->program);
665         const Gwn_ShaderInput *attrib = GWN_shaderinterface_attr(shader->interface, name);
666         return attrib ? attrib->location : -1;
667 }
668
669 GPUShader *GPU_shader_get_builtin_shader(GPUBuiltinShader shader)
670 {
671         BLI_assert(shader != GPU_NUM_BUILTIN_SHADERS); /* don't be a troll */
672
673         static const GPUShaderStages builtin_shader_stages[GPU_NUM_BUILTIN_SHADERS] = {
674                 [GPU_SHADER_VSM_STORE] = { datatoc_gpu_shader_vsm_store_vert_glsl, datatoc_gpu_shader_vsm_store_frag_glsl },
675                 [GPU_SHADER_SEP_GAUSSIAN_BLUR] = { datatoc_gpu_shader_sep_gaussian_blur_vert_glsl,
676                                                    datatoc_gpu_shader_sep_gaussian_blur_frag_glsl },
677                 [GPU_SHADER_SMOKE] = { datatoc_gpu_shader_smoke_vert_glsl, datatoc_gpu_shader_smoke_frag_glsl },
678                 [GPU_SHADER_SMOKE_FIRE] = { datatoc_gpu_shader_smoke_vert_glsl, datatoc_gpu_shader_smoke_frag_glsl },
679                 [GPU_SHADER_SMOKE_COBA] = { datatoc_gpu_shader_smoke_vert_glsl, datatoc_gpu_shader_smoke_frag_glsl },
680
681                 [GPU_SHADER_TEXT] = { datatoc_gpu_shader_text_vert_glsl, datatoc_gpu_shader_text_frag_glsl },
682                 [GPU_SHADER_KEYFRAME_DIAMOND] = { datatoc_gpu_shader_keyframe_diamond_vert_glsl,
683                                                   datatoc_gpu_shader_keyframe_diamond_frag_glsl },
684                 [GPU_SHADER_EDGES_FRONT_BACK_PERSP] = { datatoc_gpu_shader_edges_front_back_persp_vert_glsl,
685                         /*  this version is     */      datatoc_gpu_shader_flat_color_frag_glsl,
686                        /*  magical but slooow  */       datatoc_gpu_shader_edges_front_back_persp_geom_glsl },
687                 [GPU_SHADER_EDGES_FRONT_BACK_ORTHO] = { datatoc_gpu_shader_edges_front_back_ortho_vert_glsl,
688                                                         datatoc_gpu_shader_flat_color_frag_glsl },
689                 [GPU_SHADER_EDGES_OVERLAY_SIMPLE] = { datatoc_gpu_shader_3D_vert_glsl, datatoc_gpu_shader_edges_overlay_frag_glsl,
690                                                       datatoc_gpu_shader_edges_overlay_simple_geom_glsl },
691                 [GPU_SHADER_EDGES_OVERLAY] = { datatoc_gpu_shader_edges_overlay_vert_glsl,
692                                                datatoc_gpu_shader_edges_overlay_frag_glsl,
693                                                datatoc_gpu_shader_edges_overlay_geom_glsl },
694                 [GPU_SHADER_SIMPLE_LIGHTING] = { datatoc_gpu_shader_3D_normal_vert_glsl, datatoc_gpu_shader_simple_lighting_frag_glsl },
695                 /* Use 'USE_FLAT_NORMAL' to make flat shader from smooth  */
696                 [GPU_SHADER_SIMPLE_LIGHTING_FLAT_COLOR] = { datatoc_gpu_shader_3D_normal_smooth_color_vert_glsl, datatoc_gpu_shader_simple_lighting_smooth_color_frag_glsl },
697                 [GPU_SHADER_SIMPLE_LIGHTING_SMOOTH_COLOR] = { datatoc_gpu_shader_3D_normal_smooth_color_vert_glsl, datatoc_gpu_shader_simple_lighting_smooth_color_frag_glsl },
698                 [GPU_SHADER_SIMPLE_LIGHTING_SMOOTH_COLOR_ALPHA] = { datatoc_gpu_shader_3D_normal_smooth_color_vert_glsl, datatoc_gpu_shader_simple_lighting_smooth_color_alpha_frag_glsl },
699
700                 [GPU_SHADER_2D_IMAGE_MASK_UNIFORM_COLOR] = { datatoc_gpu_shader_3D_image_vert_glsl,
701                                                              datatoc_gpu_shader_image_mask_uniform_color_frag_glsl },
702                 [GPU_SHADER_3D_IMAGE_MODULATE_ALPHA] = { datatoc_gpu_shader_3D_image_vert_glsl,
703                                                          datatoc_gpu_shader_image_modulate_alpha_frag_glsl },
704                 [GPU_SHADER_3D_IMAGE_RECT_MODULATE_ALPHA] = { datatoc_gpu_shader_3D_image_vert_glsl,
705                                                               datatoc_gpu_shader_image_rect_modulate_alpha_frag_glsl },
706                 [GPU_SHADER_3D_IMAGE_DEPTH] = { datatoc_gpu_shader_3D_image_vert_glsl,
707                                                 datatoc_gpu_shader_image_depth_linear_frag_glsl },
708
709                 [GPU_SHADER_2D_IMAGE_INTERLACE] = { datatoc_gpu_shader_2D_image_vert_glsl,
710                                                     datatoc_gpu_shader_image_interlace_frag_glsl },
711                 [GPU_SHADER_2D_CHECKER] = { datatoc_gpu_shader_2D_vert_glsl, datatoc_gpu_shader_checker_frag_glsl },
712
713                 [GPU_SHADER_2D_DIAG_STRIPES] = { datatoc_gpu_shader_2D_vert_glsl, datatoc_gpu_shader_diag_stripes_frag_glsl },
714
715                 [GPU_SHADER_2D_UNIFORM_COLOR] = { datatoc_gpu_shader_2D_vert_glsl, datatoc_gpu_shader_uniform_color_frag_glsl },
716                 [GPU_SHADER_2D_FLAT_COLOR] = { datatoc_gpu_shader_2D_flat_color_vert_glsl,
717                                                datatoc_gpu_shader_flat_color_frag_glsl },
718                 [GPU_SHADER_2D_SMOOTH_COLOR] = { datatoc_gpu_shader_2D_smooth_color_vert_glsl,
719                                                  datatoc_gpu_shader_2D_smooth_color_frag_glsl },
720                 [GPU_SHADER_2D_IMAGE_LINEAR_TO_SRGB] = { datatoc_gpu_shader_2D_image_vert_glsl,
721                                                          datatoc_gpu_shader_image_linear_frag_glsl },
722                 [GPU_SHADER_2D_IMAGE_COLOR] = { datatoc_gpu_shader_2D_image_vert_glsl,
723                                                 datatoc_gpu_shader_image_color_frag_glsl },
724                 [GPU_SHADER_2D_IMAGE_ALPHA_COLOR] = { datatoc_gpu_shader_2D_image_vert_glsl,
725                                                       datatoc_gpu_shader_image_alpha_color_frag_glsl },
726                 [GPU_SHADER_2D_IMAGE_SHUFFLE_COLOR] = { datatoc_gpu_shader_2D_image_vert_glsl,
727                                                         datatoc_gpu_shader_image_shuffle_color_frag_glsl },
728                 [GPU_SHADER_3D_UNIFORM_COLOR] = { datatoc_gpu_shader_3D_vert_glsl, datatoc_gpu_shader_uniform_color_frag_glsl },
729             [GPU_SHADER_3D_UNIFORM_COLOR_U32] = { datatoc_gpu_shader_3D_vert_glsl, datatoc_gpu_shader_uniform_color_frag_glsl },
730                 [GPU_SHADER_3D_FLAT_COLOR] = { datatoc_gpu_shader_3D_flat_color_vert_glsl,
731                                                datatoc_gpu_shader_flat_color_frag_glsl },
732                 [GPU_SHADER_3D_FLAT_COLOR_U32] = { datatoc_gpu_shader_3D_flat_color_vert_glsl,
733                                                    datatoc_gpu_shader_flat_color_frag_glsl },
734                 [GPU_SHADER_3D_SMOOTH_COLOR] = { datatoc_gpu_shader_3D_smooth_color_vert_glsl,
735                                                  datatoc_gpu_shader_3D_smooth_color_frag_glsl },
736                 [GPU_SHADER_3D_DEPTH_ONLY] = { datatoc_gpu_shader_3D_vert_glsl, datatoc_gpu_shader_depth_only_frag_glsl },
737                 [GPU_SHADER_3D_CLIPPED_UNIFORM_COLOR] = { datatoc_gpu_shader_3D_clipped_uniform_color_vert_glsl,
738                                                           datatoc_gpu_shader_uniform_color_frag_glsl },
739
740                 [GPU_SHADER_3D_GROUNDPOINT] = { datatoc_gpu_shader_3D_groundpoint_vert_glsl, datatoc_gpu_shader_point_uniform_color_frag_glsl },
741                 [GPU_SHADER_3D_GROUNDLINE] = { datatoc_gpu_shader_3D_passthrough_vert_glsl,
742                                                datatoc_gpu_shader_uniform_color_frag_glsl,
743                                                datatoc_gpu_shader_3D_groundline_geom_glsl },
744
745                 [GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR] = { datatoc_gpu_shader_2D_line_dashed_uniform_color_vert_glsl,
746                                                               datatoc_gpu_shader_2D_line_dashed_frag_glsl,
747                                                               datatoc_gpu_shader_2D_line_dashed_geom_glsl },
748                 [GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR] = { datatoc_gpu_shader_3D_line_dashed_uniform_color_vert_glsl,
749                                                               datatoc_gpu_shader_2D_line_dashed_frag_glsl,
750                                                               datatoc_gpu_shader_2D_line_dashed_geom_glsl },
751
752                 [GPU_SHADER_3D_OBJECTSPACE_SIMPLE_LIGHTING_VARIYING_COLOR] =
753                     { datatoc_gpu_shader_instance_objectspace_variying_color_vert_glsl,
754                       datatoc_gpu_shader_simple_lighting_frag_glsl},
755                 [GPU_SHADER_3D_OBJECTSPACE_VARIYING_COLOR] = { datatoc_gpu_shader_instance_objectspace_variying_color_vert_glsl,
756                                                                datatoc_gpu_shader_flat_color_frag_glsl},
757                 [GPU_SHADER_3D_SCREENSPACE_VARIYING_COLOR] = { datatoc_gpu_shader_instance_screenspace_variying_color_vert_glsl,
758                                                                datatoc_gpu_shader_flat_color_frag_glsl},
759                 [GPU_SHADER_3D_INSTANCE_SCREEN_ALIGNED_AXIS] = { datatoc_gpu_shader_instance_screen_aligned_vert_glsl,
760                                                                  datatoc_gpu_shader_flat_color_frag_glsl},
761                 [GPU_SHADER_3D_INSTANCE_SCREEN_ALIGNED] = { datatoc_gpu_shader_instance_screen_aligned_vert_glsl,
762                                                             datatoc_gpu_shader_flat_color_frag_glsl},
763
764                 [GPU_SHADER_CAMERA] = { datatoc_gpu_shader_instance_camera_vert_glsl,
765                                         datatoc_gpu_shader_flat_color_frag_glsl},
766                 [GPU_SHADER_DISTANCE_LINES] = { datatoc_gpu_shader_instance_distance_line_vert_glsl,
767                                                 datatoc_gpu_shader_flat_color_frag_glsl},
768
769                 [GPU_SHADER_2D_POINT_FIXED_SIZE_UNIFORM_COLOR] =
770                         { datatoc_gpu_shader_2D_vert_glsl, datatoc_gpu_shader_point_uniform_color_frag_glsl },
771                 [GPU_SHADER_2D_POINT_VARYING_SIZE_VARYING_COLOR] =
772                         { datatoc_gpu_shader_2D_point_varying_size_varying_color_vert_glsl,
773                           datatoc_gpu_shader_point_varying_color_frag_glsl },
774                 [GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA] =
775                         { datatoc_gpu_shader_2D_point_uniform_size_aa_vert_glsl,
776                           datatoc_gpu_shader_point_uniform_color_aa_frag_glsl },
777                 [GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA] =
778                         { datatoc_gpu_shader_2D_point_uniform_size_outline_aa_vert_glsl,
779                           datatoc_gpu_shader_point_uniform_color_outline_aa_frag_glsl },
780                 [GPU_SHADER_2D_POINT_UNIFORM_SIZE_VARYING_COLOR_OUTLINE_AA] =
781                         { datatoc_gpu_shader_2D_point_uniform_size_varying_color_outline_aa_vert_glsl,
782                           datatoc_gpu_shader_point_varying_color_outline_aa_frag_glsl },
783                 [GPU_SHADER_3D_POINT_FIXED_SIZE_UNIFORM_COLOR] = { datatoc_gpu_shader_3D_vert_glsl,
784                                                                    datatoc_gpu_shader_point_uniform_color_frag_glsl },
785                 [GPU_SHADER_3D_POINT_FIXED_SIZE_VARYING_COLOR] = { datatoc_gpu_shader_3D_point_fixed_size_varying_color_vert_glsl,
786                                                                    datatoc_gpu_shader_point_varying_color_frag_glsl },
787                 [GPU_SHADER_3D_POINT_VARYING_SIZE_UNIFORM_COLOR] = { datatoc_gpu_shader_3D_point_varying_size_vert_glsl,
788                                                                      datatoc_gpu_shader_point_uniform_color_frag_glsl },
789                 [GPU_SHADER_3D_POINT_VARYING_SIZE_VARYING_COLOR] =
790                         { datatoc_gpu_shader_3D_point_varying_size_varying_color_vert_glsl,
791                           datatoc_gpu_shader_point_varying_color_frag_glsl },
792                 [GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA] =
793                         { datatoc_gpu_shader_3D_point_uniform_size_aa_vert_glsl,
794                           datatoc_gpu_shader_point_uniform_color_aa_frag_glsl },
795                 [GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA] =
796                         { datatoc_gpu_shader_3D_point_uniform_size_outline_aa_vert_glsl,
797                           datatoc_gpu_shader_point_uniform_color_outline_aa_frag_glsl },
798
799                 [GPU_SHADER_INSTANCE_UNIFORM_COLOR] = { datatoc_gpu_shader_instance_vert_glsl, datatoc_gpu_shader_uniform_color_frag_glsl },
800                 [GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SIZE] = 
801                     { datatoc_gpu_shader_instance_variying_size_variying_color_vert_glsl,
802                       datatoc_gpu_shader_flat_color_frag_glsl },
803                 [GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SCALE] =
804                     { datatoc_gpu_shader_instance_variying_size_variying_color_vert_glsl,
805                       datatoc_gpu_shader_flat_color_frag_glsl },
806                 [GPU_SHADER_INSTANCE_EDGES_VARIYING_COLOR] = { datatoc_gpu_shader_instance_edges_variying_color_vert_glsl,
807                                                                datatoc_gpu_shader_flat_color_frag_glsl,
808                                                                datatoc_gpu_shader_instance_edges_variying_color_geom_glsl},
809
810                 [GPU_SHADER_3D_INSTANCE_BONE_ENVELOPE_SOLID] = { datatoc_gpu_shader_instance_bone_envelope_solid_vert_glsl,
811                                                                  datatoc_gpu_shader_simple_lighting_frag_glsl },
812                 [GPU_SHADER_3D_INSTANCE_BONE_ENVELOPE_WIRE] = { datatoc_gpu_shader_instance_bone_envelope_wire_vert_glsl,
813                                                                 datatoc_gpu_shader_flat_color_frag_glsl },
814         };
815
816         if (builtin_shaders[shader] == NULL) {
817                 /* just a few special cases */
818                 const char *defines = NULL;
819                 switch (shader) {
820                         case GPU_SHADER_SMOKE_COBA:
821                                 defines = "#define USE_COBA;\n";
822                                 break;
823                         case GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SIZE:
824                                 defines = "#define UNIFORM_SCALE;\n";
825                                 break;
826                         case GPU_SHADER_3D_INSTANCE_SCREEN_ALIGNED_AXIS:
827                                 defines = "#define AXIS_NAME;\n";
828                                 break;
829                         case GPU_SHADER_3D_OBJECTSPACE_SIMPLE_LIGHTING_VARIYING_COLOR:
830                         case GPU_SHADER_3D_INSTANCE_BONE_ENVELOPE_SOLID:
831                                 defines = "#define USE_INSTANCE_COLOR;\n";
832                                 break;
833                         case GPU_SHADER_3D_FLAT_COLOR_U32:
834                         case GPU_SHADER_3D_UNIFORM_COLOR_U32:
835                                 defines = "#define USE_COLOR_U32;\n";
836                                 break;
837                         case GPU_SHADER_SIMPLE_LIGHTING_FLAT_COLOR:
838                                 defines = "#define USE_FLAT_NORMAL;\n";
839                                 break;
840                         default:
841                                 break;
842                 }
843
844                 const GPUShaderStages *stages = builtin_shader_stages + shader;
845
846                 if (shader == GPU_SHADER_EDGES_FRONT_BACK_PERSP && !GLEW_VERSION_3_2) {
847                         /* TODO: remove after switch to core profile (maybe) */
848                         static const GPUShaderStages legacy_fancy_edges =
849                                 { datatoc_gpu_shader_edges_front_back_persp_legacy_vert_glsl,
850                                   datatoc_gpu_shader_flat_color_alpha_test_0_frag_glsl };
851                         stages = &legacy_fancy_edges;
852                 }
853
854                 if (shader == GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR && !GLEW_VERSION_3_2) {
855                         /* Dashed need geometry shader, which are not supported by legacy OpenGL, fallback to solid lines. */
856                         /* TODO: remove after switch to core profile (maybe) */
857                         static const GPUShaderStages legacy_dashed_lines = { datatoc_gpu_shader_3D_line_dashed_uniform_color_legacy_vert_glsl,
858                                                                              datatoc_gpu_shader_2D_line_dashed_frag_glsl };
859                         stages = &legacy_dashed_lines;
860                 }
861
862                 /* common case */
863                 builtin_shaders[shader] = GPU_shader_create(stages->vert, stages->frag, stages->geom, NULL, defines);
864         }
865
866         return builtin_shaders[shader];
867 }
868
869 #define MAX_DEFINES 100
870
871 GPUShader *GPU_shader_get_builtin_fx_shader(int effect, bool persp)
872 {
873         int offset;
874         char defines[MAX_DEFINES] = "";
875         /* avoid shaders out of range */
876         if (effect >= MAX_FX_SHADERS)
877                 return NULL;
878
879         offset = 2 * effect;
880
881         if (persp) {
882                 offset += 1;
883                 strcat(defines, "#define PERSP_MATRIX\n");
884         }
885
886         if (!fx_shaders[offset]) {
887                 GPUShader *shader = NULL;
888
889                 switch (effect) {
890                         case GPU_SHADER_FX_SSAO:
891                                 shader = GPU_shader_create(datatoc_gpu_shader_fullscreen_vert_glsl, datatoc_gpu_shader_fx_ssao_frag_glsl, NULL, datatoc_gpu_shader_fx_lib_glsl, defines);
892                                 break;
893
894                         case GPU_SHADER_FX_DEPTH_OF_FIELD_PASS_ONE:
895                                 strcat(defines, "#define FIRST_PASS\n");
896                                 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);
897                                 break;
898
899                         case GPU_SHADER_FX_DEPTH_OF_FIELD_PASS_TWO:
900                                 strcat(defines, "#define SECOND_PASS\n");
901                                 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);
902                                 break;
903
904                         case GPU_SHADER_FX_DEPTH_OF_FIELD_PASS_THREE:
905                                 strcat(defines, "#define THIRD_PASS\n");
906                                 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);
907                                 break;
908
909                         case GPU_SHADER_FX_DEPTH_OF_FIELD_PASS_FOUR:
910                                 strcat(defines, "#define FOURTH_PASS\n");
911                                 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);
912                                 break;
913
914                         case GPU_SHADER_FX_DEPTH_OF_FIELD_PASS_FIVE:
915                                 strcat(defines, "#define FIFTH_PASS\n");
916                                 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);
917                                 break;
918
919                         case GPU_SHADER_FX_DEPTH_OF_FIELD_HQ_PASS_ONE:
920                                 strcat(defines, "#define FIRST_PASS\n");
921                                 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);
922                                 break;
923
924                         case GPU_SHADER_FX_DEPTH_OF_FIELD_HQ_PASS_TWO:
925                                 strcat(defines, "#define SECOND_PASS\n");
926                                 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, defines);
927                                 break;
928
929                         case GPU_SHADER_FX_DEPTH_OF_FIELD_HQ_PASS_THREE:
930                                 strcat(defines, "#define THIRD_PASS\n");
931                                 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);
932                                 break;
933
934                         case GPU_SHADER_FX_DEPTH_RESOLVE:
935                                 shader = GPU_shader_create(datatoc_gpu_shader_fullscreen_vert_glsl, datatoc_gpu_shader_fx_depth_resolve_glsl, NULL, NULL, defines);
936                                 break;
937                 }
938
939                 fx_shaders[offset] = shader;
940                 GPU_fx_shader_init_interface(shader, effect);
941         }
942
943         return fx_shaders[offset];
944 }
945
946
947 void GPU_shader_free_builtin_shaders(void)
948 {
949         for (int i = 0; i < GPU_NUM_BUILTIN_SHADERS; ++i) {
950                 if (builtin_shaders[i]) {
951                         GPU_shader_free(builtin_shaders[i]);
952                         builtin_shaders[i] = NULL;
953                 }
954         }
955
956         for (int i = 0; i < 2 * MAX_FX_SHADERS; ++i) {
957                 if (fx_shaders[i]) {
958                         GPU_shader_free(fx_shaders[i]);
959                         fx_shaders[i] = NULL;
960                 }
961         }
962 }