4f6206bf11f0a532011eaf22d3fd86046e3d1998
[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
34 #include "BKE_global.h"
35
36 #include "GPU_compositing.h"
37 #include "GPU_debug.h"
38 #include "GPU_extensions.h"
39 #include "GPU_shader.h"
40 #include "GPU_texture.h"
41
42 #include "gpu_shader_private.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_depth_only_frag_glsl[];
50 extern char datatoc_gpu_shader_uniform_color_frag_glsl[];
51 extern char datatoc_gpu_shader_flat_color_frag_glsl[];
52 extern char datatoc_gpu_shader_flat_color_alpha_test_0_frag_glsl[];
53 extern char datatoc_gpu_shader_2D_vert_glsl[];
54 extern char datatoc_gpu_shader_2D_flat_color_vert_glsl[];
55 extern char datatoc_gpu_shader_2D_smooth_color_vert_glsl[];
56 extern char datatoc_gpu_shader_2D_smooth_color_frag_glsl[];
57 extern char datatoc_gpu_shader_3D_image_vert_glsl[];
58 extern char datatoc_gpu_shader_image_mask_uniform_color_frag_glsl[];
59 extern char datatoc_gpu_shader_image_modulate_alpha_frag_glsl[];
60 extern char datatoc_gpu_shader_image_rect_modulate_alpha_frag_glsl[];
61 extern char datatoc_gpu_shader_image_depth_linear_frag_glsl[];
62 extern char datatoc_gpu_shader_3D_vert_glsl[];
63 extern char datatoc_gpu_shader_3D_flat_color_vert_glsl[];
64 extern char datatoc_gpu_shader_3D_smooth_color_vert_glsl[];
65 extern char datatoc_gpu_shader_3D_smooth_color_frag_glsl[];
66
67 extern char datatoc_gpu_shader_point_uniform_color_frag_glsl[];
68 extern char datatoc_gpu_shader_point_uniform_color_smooth_frag_glsl[];
69 extern char datatoc_gpu_shader_point_uniform_color_outline_smooth_frag_glsl[];
70 extern char datatoc_gpu_shader_point_varying_color_outline_smooth_frag_glsl[];
71 extern char datatoc_gpu_shader_point_varying_color_frag_glsl[];
72 extern char datatoc_gpu_shader_3D_point_fixed_size_varying_color_vert_glsl[];
73 extern char datatoc_gpu_shader_3D_point_varying_size_vert_glsl[];
74 extern char datatoc_gpu_shader_3D_point_varying_size_varying_color_vert_glsl[];
75 extern char datatoc_gpu_shader_3D_point_uniform_size_smooth_vert_glsl[];
76 extern char datatoc_gpu_shader_3D_point_uniform_size_outline_smooth_vert_glsl[];
77 extern char datatoc_gpu_shader_2D_point_varying_size_varying_color_vert_glsl[];
78 extern char datatoc_gpu_shader_2D_point_uniform_size_smooth_vert_glsl[];
79 extern char datatoc_gpu_shader_2D_point_uniform_size_outline_smooth_vert_glsl[];
80 extern char datatoc_gpu_shader_2D_point_uniform_size_varying_color_outline_smooth_vert_glsl[];
81
82 extern char datatoc_gpu_shader_edges_front_back_persp_vert_glsl[];
83 extern char datatoc_gpu_shader_edges_front_back_persp_geom_glsl[];
84 extern char datatoc_gpu_shader_edges_front_back_persp_legacy_vert_glsl[];
85 extern char datatoc_gpu_shader_edges_front_back_ortho_vert_glsl[];
86 extern char datatoc_gpu_shader_edges_overlay_vert_glsl[];
87 extern char datatoc_gpu_shader_edges_overlay_geom_glsl[];
88 extern char datatoc_gpu_shader_edges_overlay_simple_geom_glsl[];
89 extern char datatoc_gpu_shader_edges_overlay_frag_glsl[];
90 extern char datatoc_gpu_shader_text_vert_glsl[];
91 extern char datatoc_gpu_shader_text_frag_glsl[];
92
93 extern char datatoc_gpu_shader_fire_frag_glsl[];
94 extern char datatoc_gpu_shader_smoke_vert_glsl[];
95 extern char datatoc_gpu_shader_smoke_frag_glsl[];
96 extern char datatoc_gpu_shader_vsm_store_vert_glsl[];
97 extern char datatoc_gpu_shader_vsm_store_frag_glsl[];
98 extern char datatoc_gpu_shader_sep_gaussian_blur_vert_glsl[];
99 extern char datatoc_gpu_shader_sep_gaussian_blur_frag_glsl[];
100 extern char datatoc_gpu_shader_fx_vert_glsl[];
101 extern char datatoc_gpu_shader_fx_ssao_frag_glsl[];
102 extern char datatoc_gpu_shader_fx_dof_frag_glsl[];
103 extern char datatoc_gpu_shader_fx_dof_vert_glsl[];
104 extern char datatoc_gpu_shader_fx_dof_hq_frag_glsl[];
105 extern char datatoc_gpu_shader_fx_dof_hq_vert_glsl[];
106 extern char datatoc_gpu_shader_fx_dof_hq_geo_glsl[];
107 extern char datatoc_gpu_shader_fx_depth_resolve_glsl[];
108 extern char datatoc_gpu_shader_fx_lib_glsl[];
109
110 static struct GPUShadersGlobal {
111         struct {
112                 GPUShader *vsm_store;
113                 GPUShader *sep_gaussian_blur;
114                 GPUShader *smoke;
115                 GPUShader *smoke_fire;
116                 GPUShader *smoke_coba;
117                 /* cache for shader fx. Those can exist in combinations so store them here */
118                 GPUShader *fx_shaders[MAX_FX_SHADERS * 2];
119                 /* specialized drawing */
120                 GPUShader *text;
121                 GPUShader *edges_front_back_persp;
122                 GPUShader *edges_front_back_ortho;
123                 GPUShader *edges_overlay_simple;
124                 GPUShader *edges_overlay;
125                 /* for drawing images */
126                 GPUShader *image_mask_uniform_color_2D;
127                 GPUShader *image_modulate_alpha_3D;
128                 GPUShader *image_rect_modulate_alpha_3D;
129                 GPUShader *image_depth_3D;
130                 /* for simple 2D drawing */
131                 GPUShader *uniform_color_2D;
132                 GPUShader *flat_color_2D;
133                 GPUShader *smooth_color_2D;
134                 /* for simple 3D drawing */
135                 GPUShader *uniform_color_3D;
136                 GPUShader *flat_color_3D;
137                 GPUShader *smooth_color_3D;
138                 GPUShader *depth_only_3D;
139                 /* points */
140                 GPUShader *point_fixed_size_uniform_color_2D;
141                 GPUShader *point_varying_size_varying_color_2D;
142                 GPUShader *point_uniform_size_uniform_color_smooth_2D;
143                 GPUShader *point_uniform_size_uniform_color_outline_smooth_2D;
144                 GPUShader *point_uniform_size_varying_color_outline_smooth_2D;
145                 GPUShader *point_fixed_size_uniform_color_3D;
146                 GPUShader *point_fixed_size_varying_color_3D;
147                 GPUShader *point_varying_size_uniform_color_3D;
148                 GPUShader *point_varying_size_varying_color_3D;
149                 GPUShader *point_uniform_size_uniform_color_smooth_3D;
150                 GPUShader *point_uniform_size_uniform_color_outline_smooth_3D;
151         } shaders;
152 } GG = {{NULL}};
153
154
155 static void shader_print_errors(const char *task, const char *log, const char **code, int totcode)
156 {
157         int line = 1;
158
159         fprintf(stderr, "GPUShader: %s error:\n", task);
160
161         for (int i = 0; i < totcode; i++) {
162                 const char *c, *pos, *end = code[i] + strlen(code[i]);
163
164                 if (G.debug & G_DEBUG) {
165                         fprintf(stderr, "===== shader string %d ====\n", i + 1);
166
167                         c = code[i];
168                         while ((c < end) && (pos = strchr(c, '\n'))) {
169                                 fprintf(stderr, "%2d  ", line);
170                                 fwrite(c, (pos + 1) - c, 1, stderr);
171                                 c = pos + 1;
172                                 line++;
173                         }
174                         
175                         fprintf(stderr, "%s", c);
176                 }
177         }
178         
179         fprintf(stderr, "%s\n", log);
180 }
181
182 static const char *gpu_shader_version(void)
183 {
184         if (GLEW_VERSION_3_3) {
185                 if (GPU_legacy_support()) {
186                         return "#version 330 compatibility\n";
187                         /* highest version that is widely supported
188                          * gives us native geometry shaders!
189                          * use compatibility profile so we can continue using builtin shader input/output names
190                          */
191                 }
192                 else {
193                         return "#version 130\n";
194                         /* latest version that is compatible with existing shaders */
195                 }
196         }
197         else if (GLEW_VERSION_3_0) {
198                 return "#version 130\n";
199                 /* GLSL 1.3 has modern syntax/keywords/datatypes so use if available
200                  * older features are deprecated but still available without compatibility extension or profile
201                  */
202         }
203         else {
204                 return "#version 120\n";
205                 /* minimum supported */
206         }
207 }
208
209
210 static void gpu_shader_standard_extensions(char defines[MAX_EXT_DEFINE_LENGTH], bool use_geometry_shader)
211 {
212         /* enable extensions for features that are not part of our base GLSL version
213          * don't use an extension for something already available!
214          */
215
216         if (GLEW_ARB_texture_query_lod) {
217                 /* a #version 400 feature, but we use #version 150 maximum so use extension */
218                 strcat(defines, "#extension GL_ARB_texture_query_lod: enable\n");
219         }
220
221         if (use_geometry_shader && GPU_geometry_shader_support_via_extension()) {
222                 strcat(defines, "#extension GL_EXT_geometry_shader4: enable\n");
223         }
224
225         if (GLEW_VERSION_3_1 && !GLEW_VERSION_3_2 && GLEW_ARB_compatibility) {
226                 strcat(defines, "#extension GL_ARB_compatibility: enable\n");
227         }
228
229         if (!GLEW_VERSION_3_1) {
230                 if (GLEW_ARB_draw_instanced) {
231                         strcat(defines, "#extension GL_ARB_draw_instanced: enable\n");
232                 }
233
234                 if (!GLEW_VERSION_3_0) {
235                         strcat(defines, "#extension GL_EXT_gpu_shader4: require\n");
236                 }
237         }
238 }
239
240 static void gpu_shader_standard_defines(char defines[MAX_DEFINE_LENGTH],
241                                         bool use_opensubdiv,
242                                         bool use_new_shading)
243 {
244         /* some useful defines to detect GPU type */
245         if (GPU_type_matches(GPU_DEVICE_ATI, GPU_OS_ANY, GPU_DRIVER_ANY)) {
246                 strcat(defines, "#define GPU_ATI\n");
247                 if (GLEW_VERSION_3_0) {
248                         /* TODO(merwin): revisit this version check; GLEW_VERSION_3_0 means GL 3.0 or newer */
249                         strcat(defines, "#define CLIP_WORKAROUND\n");
250                 }
251         }
252         else if (GPU_type_matches(GPU_DEVICE_NVIDIA, GPU_OS_ANY, GPU_DRIVER_ANY))
253                 strcat(defines, "#define GPU_NVIDIA\n");
254         else if (GPU_type_matches(GPU_DEVICE_INTEL, GPU_OS_ANY, GPU_DRIVER_ANY))
255                 strcat(defines, "#define GPU_INTEL\n");
256
257         if (GPU_bicubic_bump_support())
258                 strcat(defines, "#define BUMP_BICUBIC\n");
259
260         if (GLEW_VERSION_3_0) {
261                 strcat(defines, "#define BIT_OPERATIONS\n");
262         }
263
264 #ifdef WITH_OPENSUBDIV
265         /* TODO(sergey): Check whether we actually compiling shader for
266          * the OpenSubdiv mesh.
267          */
268         if (use_opensubdiv) {
269                 strcat(defines, "#define USE_OPENSUBDIV\n");
270
271                 /* TODO(sergey): not strictly speaking a define, but this is
272                  * a global typedef which we don't have better place to define
273                  * in yet.
274                  */
275                 strcat(defines, "struct VertexData {\n"
276                                 "  vec4 position;\n"
277                                 "  vec3 normal;\n"
278                                 "  vec2 uv;"
279                                 "};\n");
280         }
281 #else
282         UNUSED_VARS(use_opensubdiv);
283 #endif
284
285         if (use_new_shading) {
286                 strcat(defines, "#define USE_NEW_SHADING\n");
287         }
288
289         return;
290 }
291
292 GPUShader *GPU_shader_create(const char *vertexcode,
293                              const char *fragcode,
294                              const char *geocode,
295                              const char *libcode,
296                              const char *defines,
297                              int input,
298                              int output,
299                              int number)
300 {
301         return GPU_shader_create_ex(vertexcode,
302                                     fragcode,
303                                     geocode,
304                                     libcode,
305                                     defines,
306                                     input,
307                                     output,
308                                     number,
309                                     GPU_SHADER_FLAGS_NONE);
310 }
311
312 GPUShader *GPU_shader_create_ex(const char *vertexcode,
313                                 const char *fragcode,
314                                 const char *geocode,
315                                 const char *libcode,
316                                 const char *defines,
317                                 int input,
318                                 int output,
319                                 int number,
320                                 const int flags)
321 {
322 #ifdef WITH_OPENSUBDIV
323         /* TODO(sergey): used to add #version 150 to the geometry shader.
324          * Could safely be renamed to "use_geometry_code" since it's very
325          * likely any of geometry code will want to use GLSL 1.5.
326          */
327         bool use_opensubdiv = (flags & GPU_SHADER_FLAGS_SPECIAL_OPENSUBDIV) != 0;
328 #else
329         UNUSED_VARS(flags);
330         bool use_opensubdiv = false;
331 #endif
332         GLint status;
333         GLchar log[5000];
334         GLsizei length = 0;
335         GPUShader *shader;
336         char standard_defines[MAX_DEFINE_LENGTH] = "";
337         char standard_extensions[MAX_EXT_DEFINE_LENGTH] = "";
338
339         if (geocode && !GPU_geometry_shader_support())
340                 return NULL;
341
342         shader = MEM_callocN(sizeof(GPUShader), "GPUShader");
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, geocode != NULL);
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                 glAttachShader(shader->program, shader->vertex);
381                 glShaderSource(shader->vertex, num_source, source, NULL);
382
383                 glCompileShader(shader->vertex);
384                 glGetShaderiv(shader->vertex, GL_COMPILE_STATUS, &status);
385
386                 if (!status) {
387                         glGetShaderInfoLog(shader->vertex, sizeof(log), &length, log);
388                         shader_print_errors("compile", log, source, num_source);
389
390                         GPU_shader_free(shader);
391                         return NULL;
392                 }
393         }
394
395         if (fragcode) {
396                 const char *source[7];
397                 int num_source = 0;
398
399                 source[num_source++] = gpu_shader_version();
400                 source[num_source++] = standard_extensions;
401                 source[num_source++] = standard_defines;
402
403 #ifdef WITH_OPENSUBDIV
404                 /* TODO(sergey): Move to fragment shader source code generation. */
405                 if (use_opensubdiv) {
406                         source[num_source++] =
407                                 "#ifdef USE_OPENSUBDIV\n"
408                                 "in block {\n"
409                                 "       VertexData v;\n"
410                                 "} inpt;\n"
411                                 "#endif\n";
412                 }
413 #endif
414
415                 if (defines) source[num_source++] = defines;
416                 if (libcode) source[num_source++] = libcode;
417                 source[num_source++] = fragcode;
418
419                 glAttachShader(shader->program, shader->fragment);
420                 glShaderSource(shader->fragment, num_source, source, NULL);
421
422                 glCompileShader(shader->fragment);
423                 glGetShaderiv(shader->fragment, GL_COMPILE_STATUS, &status);
424
425                 if (!status) {
426                         glGetShaderInfoLog(shader->fragment, sizeof(log), &length, log);
427                         shader_print_errors("compile", log, source, num_source);
428
429                         GPU_shader_free(shader);
430                         return NULL;
431                 }
432         }
433
434         if (geocode) {
435                 const char *source[6];
436                 int num_source = 0;
437
438                 source[num_source++] = gpu_shader_version();
439                 source[num_source++] = standard_extensions;
440                 source[num_source++] = standard_defines;
441
442                 if (defines) source[num_source++] = defines;
443                 source[num_source++] = geocode;
444
445                 glAttachShader(shader->program, shader->geometry);
446                 glShaderSource(shader->geometry, num_source, source, NULL);
447
448                 glCompileShader(shader->geometry);
449                 glGetShaderiv(shader->geometry, GL_COMPILE_STATUS, &status);
450
451                 if (!status) {
452                         glGetShaderInfoLog(shader->geometry, sizeof(log), &length, log);
453                         shader_print_errors("compile", log, source, num_source);
454
455                         GPU_shader_free(shader);
456                         return NULL;
457                 }
458                 
459                 if (!use_opensubdiv) {
460                         GPU_shader_geometry_stage_primitive_io(shader, input, output, number);
461                 }
462         }
463
464 #ifdef WITH_OPENSUBDIV
465         if (use_opensubdiv) {
466                 glBindAttribLocation(shader->program, 0, "position");
467                 glBindAttribLocation(shader->program, 1, "normal");
468                 GPU_shader_geometry_stage_primitive_io(shader,
469                                                        GL_LINES_ADJACENCY_EXT,
470                                                        GL_TRIANGLE_STRIP,
471                                                        4);
472         }
473 #endif
474
475         glLinkProgram(shader->program);
476         glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
477         if (!status) {
478                 glGetProgramInfoLog(shader->program, sizeof(log), &length, log);
479                 /* print attached shaders in pipeline order */
480                 if (vertexcode) shader_print_errors("linking", log, &vertexcode, 1);
481                 if (geocode) shader_print_errors("linking", log, &geocode, 1);
482                 if (libcode) shader_print_errors("linking", log, &libcode, 1);
483                 if (fragcode) shader_print_errors("linking", log, &fragcode, 1);
484
485                 GPU_shader_free(shader);
486                 return NULL;
487         }
488
489 #ifdef WITH_OPENSUBDIV
490         /* TODO(sergey): Find a better place for this. */
491         if (use_opensubdiv && GLEW_VERSION_4_1) {
492                 glProgramUniform1i(shader->program,
493                                    glGetUniformLocation(shader->program, "FVarDataOffsetBuffer"),
494                                    30);  /* GL_TEXTURE30 */
495
496                 glProgramUniform1i(shader->program,
497                                    glGetUniformLocation(shader->program, "FVarDataBuffer"),
498                                    31);  /* GL_TEXTURE31 */
499         }
500 #endif
501
502         return shader;
503 }
504
505 void GPU_shader_bind(GPUShader *shader)
506 {
507         BLI_assert(shader && shader->program);
508
509         glUseProgram(shader->program);
510 }
511
512 void GPU_shader_unbind(void)
513 {
514         glUseProgram(0);
515 }
516
517 void GPU_shader_free(GPUShader *shader)
518 {
519         BLI_assert(shader);
520
521         if (shader->vertex)
522                 glDeleteShader(shader->vertex);
523         if (shader->geometry)
524                 glDeleteShader(shader->geometry);
525         if (shader->fragment)
526                 glDeleteShader(shader->fragment);
527         if (shader->program)
528                 glDeleteProgram(shader->program);
529
530         if (shader->uniform_interface)
531                 MEM_freeN(shader->uniform_interface);
532
533         MEM_freeN(shader);
534 }
535
536 int GPU_shader_get_uniform(GPUShader *shader, const char *name)
537 {
538         BLI_assert(shader && shader->program);
539
540         return glGetUniformLocation(shader->program, name);
541 }
542
543 void *GPU_shader_get_interface(GPUShader *shader)
544 {
545         return shader->uniform_interface;
546 }
547
548 void GPU_shader_set_interface(GPUShader *shader, void *interface)
549 {
550         shader->uniform_interface = interface;
551 }
552
553 void GPU_shader_uniform_vector(GPUShader *UNUSED(shader), int location, int length, int arraysize, const float *value)
554 {
555         if (location == -1 || value == NULL)
556                 return;
557
558         if (length == 1) glUniform1fv(location, arraysize, value);
559         else if (length == 2) glUniform2fv(location, arraysize, value);
560         else if (length == 3) glUniform3fv(location, arraysize, value);
561         else if (length == 4) glUniform4fv(location, arraysize, value);
562         else if (length == 9) glUniformMatrix3fv(location, arraysize, 0, value);
563         else if (length == 16) glUniformMatrix4fv(location, arraysize, 0, value);
564 }
565
566 void GPU_shader_uniform_vector_int(GPUShader *UNUSED(shader), int location, int length, int arraysize, const int *value)
567 {
568         if (location == -1)
569                 return;
570
571         if (length == 1) glUniform1iv(location, arraysize, value);
572         else if (length == 2) glUniform2iv(location, arraysize, value);
573         else if (length == 3) glUniform3iv(location, arraysize, value);
574         else if (length == 4) glUniform4iv(location, arraysize, value);
575 }
576
577 void GPU_shader_uniform_int(GPUShader *UNUSED(shader), int location, int value)
578 {
579         if (location == -1)
580                 return;
581
582         glUniform1i(location, value);
583 }
584
585 void GPU_shader_geometry_stage_primitive_io(GPUShader *shader, int input, int output, int number)
586 {
587         if (GPU_geometry_shader_support_via_extension()) {
588                 /* geometry shaders must provide this info themselves for #version 150 and up */
589                 glProgramParameteriEXT(shader->program, GL_GEOMETRY_INPUT_TYPE_EXT, input);
590                 glProgramParameteriEXT(shader->program, GL_GEOMETRY_OUTPUT_TYPE_EXT, output);
591                 glProgramParameteriEXT(shader->program, GL_GEOMETRY_VERTICES_OUT_EXT, number);
592         }
593 }
594
595 void GPU_shader_uniform_texture(GPUShader *UNUSED(shader), int location, GPUTexture *tex)
596 {
597         int number = GPU_texture_bound_number(tex);
598         int bindcode = GPU_texture_opengl_bindcode(tex);
599         int target = GPU_texture_target(tex);
600
601         if (number >= GPU_max_textures()) {
602                 fprintf(stderr, "Not enough texture slots.\n");
603                 return;
604         }
605                 
606         if (number == -1)
607                 return;
608
609         if (location == -1)
610                 return;
611
612         if (number != 0)
613                 glActiveTexture(GL_TEXTURE0 + number);
614
615         if (bindcode != 0)
616                 glBindTexture(target, bindcode);
617         else
618                 GPU_invalid_tex_bind(target);
619
620         glUniform1i(location, number);
621
622         if (number != 0)
623                 glActiveTexture(GL_TEXTURE0);
624 }
625
626 int GPU_shader_get_attribute(GPUShader *shader, const char *name)
627 {
628         BLI_assert(shader && shader->program);
629
630         return glGetAttribLocation(shader->program, name);
631 }
632
633 GPUShader *GPU_shader_get_builtin_shader(GPUBuiltinShader shader)
634 {
635         GPUShader *retval = NULL;
636
637         switch (shader) {
638                 case GPU_SHADER_VSM_STORE:
639                         if (!GG.shaders.vsm_store)
640                                 GG.shaders.vsm_store = GPU_shader_create(
641                                         datatoc_gpu_shader_vsm_store_vert_glsl, datatoc_gpu_shader_vsm_store_frag_glsl,
642                                         NULL, NULL, NULL, 0, 0, 0);
643                         retval = GG.shaders.vsm_store;
644                         break;
645                 case GPU_SHADER_SEP_GAUSSIAN_BLUR:
646                         if (!GG.shaders.sep_gaussian_blur)
647                                 GG.shaders.sep_gaussian_blur = GPU_shader_create(
648                                         datatoc_gpu_shader_sep_gaussian_blur_vert_glsl,
649                                         datatoc_gpu_shader_sep_gaussian_blur_frag_glsl,
650                                         NULL, NULL, NULL, 0, 0, 0);
651                         retval = GG.shaders.sep_gaussian_blur;
652                         break;
653                 case GPU_SHADER_SMOKE:
654                         if (!GG.shaders.smoke)
655                                 GG.shaders.smoke = GPU_shader_create(
656                                         datatoc_gpu_shader_smoke_vert_glsl, datatoc_gpu_shader_smoke_frag_glsl,
657                                         NULL, NULL, NULL, 0, 0, 0);
658                         retval = GG.shaders.smoke;
659                         break;
660                 case GPU_SHADER_SMOKE_FIRE:
661                         if (!GG.shaders.smoke_fire)
662                                 GG.shaders.smoke_fire = GPU_shader_create(
663                                         datatoc_gpu_shader_smoke_vert_glsl, datatoc_gpu_shader_smoke_frag_glsl,
664                                         NULL, NULL, NULL, 0, 0, 0);
665                         retval = GG.shaders.smoke_fire;
666                         break;
667                 case GPU_SHADER_SMOKE_COBA:
668                         if (!GG.shaders.smoke_coba)
669                                 GG.shaders.smoke_coba = GPU_shader_create(
670                                         datatoc_gpu_shader_smoke_vert_glsl, datatoc_gpu_shader_smoke_frag_glsl,
671                                         NULL, NULL, "#define USE_COBA;\n", 0, 0, 0);
672                         retval = GG.shaders.smoke_coba;
673                         break;
674                 case GPU_SHADER_TEXT:
675                         if (!GG.shaders.text)
676                                 GG.shaders.text = GPU_shader_create(
677                                         datatoc_gpu_shader_text_vert_glsl,
678                                         datatoc_gpu_shader_text_frag_glsl,
679                                         NULL, NULL, NULL, 0, 0, 0);
680                         retval = GG.shaders.text;
681                         break;
682                 case GPU_SHADER_EDGES_FRONT_BACK_PERSP:
683                         if (!GG.shaders.edges_front_back_persp)
684                                 if (GLEW_VERSION_3_2) {
685                                         /* this version is magical but slooow */
686                                         GG.shaders.edges_front_back_persp = GPU_shader_create(
687                                                 datatoc_gpu_shader_edges_front_back_persp_vert_glsl,
688                                                 datatoc_gpu_shader_flat_color_frag_glsl,
689                                                 datatoc_gpu_shader_edges_front_back_persp_geom_glsl,
690                                                 NULL, NULL, 0, 0, 0);
691                                 }
692                                 else {
693                                         GG.shaders.edges_front_back_persp = GPU_shader_create(
694                                                 datatoc_gpu_shader_edges_front_back_persp_legacy_vert_glsl,
695                                                 datatoc_gpu_shader_flat_color_alpha_test_0_frag_glsl,
696                                                 NULL, NULL, NULL, 0, 0, 0);
697                                 }
698                         retval = GG.shaders.edges_front_back_persp;
699                         break;
700                 case GPU_SHADER_EDGES_FRONT_BACK_ORTHO:
701                         if (!GG.shaders.edges_front_back_ortho)
702                                 GG.shaders.edges_front_back_ortho = GPU_shader_create(
703                                         datatoc_gpu_shader_edges_front_back_ortho_vert_glsl,
704                                         datatoc_gpu_shader_flat_color_frag_glsl,
705                                         NULL, NULL, NULL, 0, 0, 0);
706                         retval = GG.shaders.edges_front_back_ortho;
707                         break;
708                 case GPU_SHADER_EDGES_OVERLAY_SIMPLE:
709                         if (!GG.shaders.edges_overlay_simple)
710                                 GG.shaders.edges_overlay_simple = GPU_shader_create(
711                                         datatoc_gpu_shader_3D_vert_glsl,
712                                         datatoc_gpu_shader_edges_overlay_frag_glsl,
713                                         datatoc_gpu_shader_edges_overlay_simple_geom_glsl,
714                                         NULL, NULL, 0, 0, 0);
715                         retval = GG.shaders.edges_overlay_simple;
716                         break;
717                 case GPU_SHADER_EDGES_OVERLAY:
718                         if (!GG.shaders.edges_overlay)
719                                 GG.shaders.edges_overlay = GPU_shader_create(
720                                         datatoc_gpu_shader_edges_overlay_vert_glsl,
721                                         datatoc_gpu_shader_edges_overlay_frag_glsl,
722                                         datatoc_gpu_shader_edges_overlay_geom_glsl,
723                                         NULL, NULL, 0, 0, 0);
724                         retval = GG.shaders.edges_overlay;
725                         break;
726                 case GPU_SHADER_2D_IMAGE_MASK_UNIFORM_COLOR:
727                         if (!GG.shaders.image_mask_uniform_color_2D)
728                                 GG.shaders.image_mask_uniform_color_2D = GPU_shader_create(
729                                         datatoc_gpu_shader_3D_image_vert_glsl,
730                                         datatoc_gpu_shader_image_mask_uniform_color_frag_glsl,
731                                         NULL, NULL, NULL, 0, 0, 0);
732                         retval = GG.shaders.image_mask_uniform_color_2D;
733                         break;
734                 case GPU_SHADER_3D_IMAGE_MODULATE_ALPHA:
735                         if (!GG.shaders.image_modulate_alpha_3D)
736                                 GG.shaders.image_modulate_alpha_3D = GPU_shader_create(
737                                         datatoc_gpu_shader_3D_image_vert_glsl,
738                                         datatoc_gpu_shader_image_modulate_alpha_frag_glsl,
739                                         NULL, NULL, NULL, 0, 0, 0);
740                         retval = GG.shaders.image_modulate_alpha_3D;
741                         break;
742                 case GPU_SHADER_3D_IMAGE_RECT_MODULATE_ALPHA:
743                         if (!GG.shaders.image_rect_modulate_alpha_3D)
744                                 GG.shaders.image_rect_modulate_alpha_3D = GPU_shader_create(
745                                 datatoc_gpu_shader_3D_image_vert_glsl,
746                                 datatoc_gpu_shader_image_rect_modulate_alpha_frag_glsl,
747                                 NULL, NULL, NULL, 0, 0, 0);
748                         retval = GG.shaders.image_rect_modulate_alpha_3D;
749                         break;
750                 case GPU_SHADER_3D_IMAGE_DEPTH:
751                         if (!GG.shaders.image_depth_3D)
752                                 GG.shaders.image_depth_3D = GPU_shader_create(
753                                         datatoc_gpu_shader_3D_image_vert_glsl,
754                                         datatoc_gpu_shader_image_depth_linear_frag_glsl,
755                                         NULL, NULL, NULL, 0, 0, 0);
756                         retval = GG.shaders.image_depth_3D;
757                         break;
758                 case GPU_SHADER_2D_UNIFORM_COLOR:
759                         if (!GG.shaders.uniform_color_2D)
760                                 GG.shaders.uniform_color_2D = GPU_shader_create(
761                                         datatoc_gpu_shader_2D_vert_glsl,
762                                         datatoc_gpu_shader_uniform_color_frag_glsl,
763                                         NULL, NULL, NULL, 0, 0, 0);
764                         retval = GG.shaders.uniform_color_2D;
765                         break;
766                 case GPU_SHADER_2D_FLAT_COLOR:
767                         if (!GG.shaders.flat_color_2D)
768                                 GG.shaders.flat_color_2D = GPU_shader_create(
769                                         datatoc_gpu_shader_2D_flat_color_vert_glsl,
770                                         datatoc_gpu_shader_flat_color_frag_glsl,
771                                         NULL, NULL, NULL, 0, 0, 0);
772                         retval = GG.shaders.flat_color_2D;
773                         break;
774                 case GPU_SHADER_2D_SMOOTH_COLOR:
775                         if (!GG.shaders.smooth_color_2D)
776                                 GG.shaders.smooth_color_2D = GPU_shader_create(
777                                         datatoc_gpu_shader_2D_smooth_color_vert_glsl,
778                                         datatoc_gpu_shader_2D_smooth_color_frag_glsl,
779                                         NULL, NULL, NULL, 0, 0, 0);
780                         retval = GG.shaders.smooth_color_2D;
781                         break;
782                 case GPU_SHADER_3D_UNIFORM_COLOR:
783                         if (!GG.shaders.uniform_color_3D)
784                                 GG.shaders.uniform_color_3D = GPU_shader_create(
785                                         datatoc_gpu_shader_3D_vert_glsl,
786                                         datatoc_gpu_shader_uniform_color_frag_glsl,
787                                         NULL, NULL, NULL, 0, 0, 0);
788                         retval = GG.shaders.uniform_color_3D;
789                         break;
790                 case GPU_SHADER_3D_FLAT_COLOR:
791                         if (!GG.shaders.flat_color_3D)
792                                 GG.shaders.flat_color_3D = GPU_shader_create(
793                                         datatoc_gpu_shader_3D_flat_color_vert_glsl,
794                                         datatoc_gpu_shader_flat_color_frag_glsl,
795                                         NULL, NULL, NULL, 0, 0, 0);
796                         retval = GG.shaders.flat_color_3D;
797                         break;
798                 case GPU_SHADER_3D_SMOOTH_COLOR:
799                         if (!GG.shaders.smooth_color_3D)
800                                 GG.shaders.smooth_color_3D = GPU_shader_create(
801                                         datatoc_gpu_shader_3D_smooth_color_vert_glsl,
802                                         datatoc_gpu_shader_3D_smooth_color_frag_glsl,
803                                         NULL, NULL, NULL, 0, 0, 0);
804                         retval = GG.shaders.smooth_color_3D;
805                         break;
806                 case GPU_SHADER_3D_DEPTH_ONLY:
807                         if (!GG.shaders.depth_only_3D)
808                                 GG.shaders.depth_only_3D = GPU_shader_create(
809                                         datatoc_gpu_shader_3D_vert_glsl,
810                                         datatoc_gpu_shader_depth_only_frag_glsl,
811                                         NULL, NULL, NULL, 0, 0, 0);
812                         retval = GG.shaders.depth_only_3D;
813                         break;
814                 case GPU_SHADER_2D_POINT_FIXED_SIZE_UNIFORM_COLOR:
815                         if (!GG.shaders.point_fixed_size_uniform_color_2D)
816                                 GG.shaders.point_fixed_size_uniform_color_2D = GPU_shader_create(
817                                         datatoc_gpu_shader_2D_vert_glsl,
818                                         datatoc_gpu_shader_point_uniform_color_frag_glsl,
819                                         NULL, NULL, NULL, 0, 0, 0);
820                         retval = GG.shaders.point_fixed_size_uniform_color_2D;
821                         break;
822                 case GPU_SHADER_2D_POINT_VARYING_SIZE_VARYING_COLOR:
823                         if (!GG.shaders.point_varying_size_varying_color_2D)
824                                 GG.shaders.point_varying_size_varying_color_2D = GPU_shader_create(
825                                         datatoc_gpu_shader_2D_point_varying_size_varying_color_vert_glsl,
826                                         datatoc_gpu_shader_point_varying_color_frag_glsl,
827                                         NULL, NULL, NULL, 0, 0, 0);
828                         retval = GG.shaders.point_varying_size_varying_color_2D;
829                         break;
830                 case GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_SMOOTH:
831                         if (!GG.shaders.point_uniform_size_uniform_color_smooth_2D)
832                                 GG.shaders.point_uniform_size_uniform_color_smooth_2D = GPU_shader_create(
833                                         datatoc_gpu_shader_2D_point_uniform_size_smooth_vert_glsl,
834                                         datatoc_gpu_shader_point_uniform_color_smooth_frag_glsl,
835                                         NULL, NULL, NULL, 0, 0, 0);
836                         retval = GG.shaders.point_uniform_size_uniform_color_smooth_2D;
837                         break;
838                 case GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_SMOOTH:
839                         if (!GG.shaders.point_uniform_size_uniform_color_outline_smooth_2D)
840                                 GG.shaders.point_uniform_size_uniform_color_outline_smooth_2D = GPU_shader_create(
841                                         datatoc_gpu_shader_2D_point_uniform_size_outline_smooth_vert_glsl,
842                                         datatoc_gpu_shader_point_uniform_color_outline_smooth_frag_glsl,
843                                         NULL, NULL, NULL, 0, 0, 0);
844                         retval = GG.shaders.point_uniform_size_uniform_color_outline_smooth_2D;
845                         break;
846                 case GPU_SHADER_2D_POINT_UNIFORM_SIZE_VARYING_COLOR_OUTLINE_SMOOTH:
847                         if (!GG.shaders.point_uniform_size_varying_color_outline_smooth_2D)
848                                 GG.shaders.point_uniform_size_varying_color_outline_smooth_2D = GPU_shader_create(
849                                         datatoc_gpu_shader_2D_point_uniform_size_varying_color_outline_smooth_vert_glsl,
850                                         datatoc_gpu_shader_point_varying_color_outline_smooth_frag_glsl,
851                                         NULL, NULL, NULL, 0, 0, 0);
852                         retval = GG.shaders.point_uniform_size_varying_color_outline_smooth_2D;
853                         break;
854                 case GPU_SHADER_3D_POINT_FIXED_SIZE_UNIFORM_COLOR:
855                         if (!GG.shaders.point_fixed_size_uniform_color_3D)
856                                 GG.shaders.point_fixed_size_uniform_color_3D = GPU_shader_create(
857                                         datatoc_gpu_shader_3D_vert_glsl,
858                                         datatoc_gpu_shader_point_uniform_color_frag_glsl,
859                                         NULL, NULL, NULL, 0, 0, 0);
860                         retval = GG.shaders.point_fixed_size_uniform_color_3D;
861                         break;
862                 case GPU_SHADER_3D_POINT_FIXED_SIZE_VARYING_COLOR:
863                         if (!GG.shaders.point_fixed_size_varying_color_3D)
864                                 GG.shaders.point_fixed_size_varying_color_3D = GPU_shader_create(
865                                         datatoc_gpu_shader_3D_point_fixed_size_varying_color_vert_glsl,
866                                         datatoc_gpu_shader_point_varying_color_frag_glsl,
867                                         NULL, NULL, NULL, 0, 0, 0);
868                         retval = GG.shaders.point_fixed_size_varying_color_3D;
869                         break;
870                 case GPU_SHADER_3D_POINT_VARYING_SIZE_UNIFORM_COLOR:
871                         if (!GG.shaders.point_varying_size_uniform_color_3D)
872                                 GG.shaders.point_varying_size_uniform_color_3D = GPU_shader_create(
873                                         datatoc_gpu_shader_3D_point_varying_size_vert_glsl,
874                                         datatoc_gpu_shader_point_uniform_color_frag_glsl,
875                                         NULL, NULL, NULL, 0, 0, 0);
876                         retval = GG.shaders.point_varying_size_uniform_color_3D;
877                         break;
878                 case GPU_SHADER_3D_POINT_VARYING_SIZE_VARYING_COLOR:
879                         if (!GG.shaders.point_varying_size_varying_color_3D)
880                                 GG.shaders.point_varying_size_varying_color_3D = GPU_shader_create(
881                                         datatoc_gpu_shader_3D_point_varying_size_varying_color_vert_glsl,
882                                         datatoc_gpu_shader_point_varying_color_frag_glsl,
883                                         NULL, NULL, NULL, 0, 0, 0);
884                         retval = GG.shaders.point_varying_size_varying_color_3D;
885                         break;
886                 case GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_SMOOTH:
887                         if (!GG.shaders.point_uniform_size_uniform_color_smooth_3D)
888                                 GG.shaders.point_uniform_size_uniform_color_smooth_3D = GPU_shader_create(
889                                         datatoc_gpu_shader_3D_point_uniform_size_smooth_vert_glsl,
890                                         datatoc_gpu_shader_point_uniform_color_smooth_frag_glsl,
891                                         NULL, NULL, NULL, 0, 0, 0);
892                         retval = GG.shaders.point_uniform_size_uniform_color_smooth_3D;
893                         break;
894                 case GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_SMOOTH:
895                         if (!GG.shaders.point_uniform_size_uniform_color_outline_smooth_3D)
896                                 GG.shaders.point_uniform_size_uniform_color_outline_smooth_3D = GPU_shader_create(
897                                         datatoc_gpu_shader_3D_point_uniform_size_outline_smooth_vert_glsl,
898                                         datatoc_gpu_shader_point_uniform_color_outline_smooth_frag_glsl,
899                                         NULL, NULL, NULL, 0, 0, 0);
900                         retval = GG.shaders.point_uniform_size_uniform_color_outline_smooth_3D;
901                         break;
902         }
903
904         if (retval == NULL)
905                 printf("Unable to create a GPUShader for builtin shader: %u\n", shader);
906
907         return retval;
908 }
909
910 #define MAX_DEFINES 100
911
912 GPUShader *GPU_shader_get_builtin_fx_shader(int effect, bool persp)
913 {
914         int offset;
915         char defines[MAX_DEFINES] = "";
916         /* avoid shaders out of range */
917         if (effect >= MAX_FX_SHADERS)
918                 return NULL;
919
920         offset = 2 * effect;
921
922         if (persp) {
923                 offset += 1;
924                 strcat(defines, "#define PERSP_MATRIX\n");
925         }
926
927         if (!GG.shaders.fx_shaders[offset]) {
928                 GPUShader *shader = NULL;
929
930                 switch (effect) {
931                         case GPU_SHADER_FX_SSAO:
932                                 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);
933                                 break;
934
935                         case GPU_SHADER_FX_DEPTH_OF_FIELD_PASS_ONE:
936                                 strcat(defines, "#define FIRST_PASS\n");
937                                 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);
938                                 break;
939
940                         case GPU_SHADER_FX_DEPTH_OF_FIELD_PASS_TWO:
941                                 strcat(defines, "#define SECOND_PASS\n");
942                                 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);
943                                 break;
944
945                         case GPU_SHADER_FX_DEPTH_OF_FIELD_PASS_THREE:
946                                 strcat(defines, "#define THIRD_PASS\n");
947                                 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);
948                                 break;
949
950                         case GPU_SHADER_FX_DEPTH_OF_FIELD_PASS_FOUR:
951                                 strcat(defines, "#define FOURTH_PASS\n");
952                                 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);
953                                 break;
954
955                         case GPU_SHADER_FX_DEPTH_OF_FIELD_PASS_FIVE:
956                                 strcat(defines, "#define FIFTH_PASS\n");
957                                 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);
958                                 break;
959
960                         case GPU_SHADER_FX_DEPTH_OF_FIELD_HQ_PASS_ONE:
961                                 strcat(defines, "#define FIRST_PASS\n");
962                                 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);
963                                 break;
964
965                         case GPU_SHADER_FX_DEPTH_OF_FIELD_HQ_PASS_TWO:
966                                 strcat(defines, "#define SECOND_PASS\n");
967                                 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,
968                                                            defines, GL_POINTS, GL_TRIANGLE_STRIP, 4);
969                                 break;
970
971                         case GPU_SHADER_FX_DEPTH_OF_FIELD_HQ_PASS_THREE:
972                                 strcat(defines, "#define THIRD_PASS\n");
973                                 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);
974                                 break;
975
976                         case GPU_SHADER_FX_DEPTH_RESOLVE:
977                                 shader = GPU_shader_create(datatoc_gpu_shader_fx_vert_glsl, datatoc_gpu_shader_fx_depth_resolve_glsl, NULL, NULL, defines, 0, 0, 0);
978                                 break;
979                 }
980
981                 GG.shaders.fx_shaders[offset] = shader;
982                 GPU_fx_shader_init_interface(shader, effect);
983         }
984
985         return GG.shaders.fx_shaders[offset];
986 }
987
988
989 void GPU_shader_free_builtin_shaders(void)
990 {
991         if (GG.shaders.vsm_store) {
992                 GPU_shader_free(GG.shaders.vsm_store);
993                 GG.shaders.vsm_store = NULL;
994         }
995
996         if (GG.shaders.sep_gaussian_blur) {
997                 GPU_shader_free(GG.shaders.sep_gaussian_blur);
998                 GG.shaders.sep_gaussian_blur = NULL;
999         }
1000
1001         if (GG.shaders.smoke) {
1002                 GPU_shader_free(GG.shaders.smoke);
1003                 GG.shaders.smoke = NULL;
1004         }
1005
1006         if (GG.shaders.smoke_fire) {
1007                 GPU_shader_free(GG.shaders.smoke_fire);
1008                 GG.shaders.smoke_fire = NULL;
1009         }
1010
1011         if (GG.shaders.smoke_coba) {
1012                 GPU_shader_free(GG.shaders.smoke_coba);
1013                 GG.shaders.smoke_coba = NULL;
1014         }
1015
1016         if (GG.shaders.text) {
1017                 GPU_shader_free(GG.shaders.text);
1018                 GG.shaders.text = NULL;
1019         }
1020
1021         if (GG.shaders.edges_front_back_persp) {
1022                 GPU_shader_free(GG.shaders.edges_front_back_persp);
1023                 GG.shaders.edges_front_back_persp = NULL;
1024         }
1025
1026         if (GG.shaders.edges_front_back_ortho) {
1027                 GPU_shader_free(GG.shaders.edges_front_back_ortho);
1028                 GG.shaders.edges_front_back_ortho = NULL;
1029         }
1030
1031         if (GG.shaders.edges_overlay_simple) {
1032                 GPU_shader_free(GG.shaders.edges_overlay_simple);
1033                 GG.shaders.edges_overlay_simple = NULL;
1034         }
1035
1036         if (GG.shaders.edges_overlay) {
1037                 GPU_shader_free(GG.shaders.edges_overlay);
1038                 GG.shaders.edges_overlay = NULL;
1039         }
1040
1041         if (GG.shaders.image_mask_uniform_color_2D) {
1042                 GPU_shader_free(GG.shaders.image_mask_uniform_color_2D);
1043                 GG.shaders.image_mask_uniform_color_2D = NULL;
1044         }
1045
1046         if (GG.shaders.image_modulate_alpha_3D) {
1047                 GPU_shader_free(GG.shaders.image_modulate_alpha_3D);
1048                 GG.shaders.image_modulate_alpha_3D = NULL;
1049         }
1050
1051         if (GG.shaders.image_rect_modulate_alpha_3D) {
1052                 GPU_shader_free(GG.shaders.image_rect_modulate_alpha_3D);
1053                 GG.shaders.image_rect_modulate_alpha_3D = NULL;
1054         }
1055
1056         if (GG.shaders.image_depth_3D) {
1057                 GPU_shader_free(GG.shaders.image_depth_3D);
1058                 GG.shaders.image_depth_3D = NULL;
1059         }
1060
1061         if (GG.shaders.uniform_color_2D) {
1062                 GPU_shader_free(GG.shaders.uniform_color_2D);
1063                 GG.shaders.uniform_color_2D = NULL;
1064         }
1065
1066         if (GG.shaders.flat_color_2D) {
1067                 GPU_shader_free(GG.shaders.flat_color_2D);
1068                 GG.shaders.flat_color_2D = NULL;
1069         }
1070
1071         if (GG.shaders.smooth_color_2D) {
1072                 GPU_shader_free(GG.shaders.smooth_color_2D);
1073                 GG.shaders.smooth_color_2D = NULL;
1074         }
1075
1076         if (GG.shaders.uniform_color_3D) {
1077                 GPU_shader_free(GG.shaders.uniform_color_3D);
1078                 GG.shaders.uniform_color_3D = NULL;
1079         }
1080
1081         if (GG.shaders.flat_color_3D) {
1082                 GPU_shader_free(GG.shaders.flat_color_3D);
1083                 GG.shaders.flat_color_3D = NULL;
1084         }
1085
1086         if (GG.shaders.smooth_color_3D) {
1087                 GPU_shader_free(GG.shaders.smooth_color_3D);
1088                 GG.shaders.smooth_color_3D = NULL;
1089         }
1090
1091         if (GG.shaders.depth_only_3D) {
1092                 GPU_shader_free(GG.shaders.depth_only_3D);
1093                 GG.shaders.depth_only_3D = NULL;
1094         }
1095
1096         if (GG.shaders.point_fixed_size_uniform_color_2D) {
1097                 GPU_shader_free(GG.shaders.point_fixed_size_uniform_color_2D);
1098                 GG.shaders.point_fixed_size_uniform_color_2D = NULL;
1099         }
1100
1101         if (GG.shaders.point_varying_size_varying_color_2D) {
1102                 GPU_shader_free(GG.shaders.point_varying_size_varying_color_2D);
1103                 GG.shaders.point_varying_size_varying_color_2D = NULL;
1104         }
1105
1106         if (GG.shaders.point_uniform_size_uniform_color_smooth_2D) {
1107                 GPU_shader_free(GG.shaders.point_uniform_size_uniform_color_smooth_2D);
1108                 GG.shaders.point_uniform_size_uniform_color_smooth_2D = NULL;
1109         }
1110
1111         if (GG.shaders.point_uniform_size_uniform_color_outline_smooth_2D) {
1112                 GPU_shader_free(GG.shaders.point_uniform_size_uniform_color_outline_smooth_2D);
1113                 GG.shaders.point_uniform_size_uniform_color_outline_smooth_2D = NULL;
1114         }
1115
1116         if (GG.shaders.point_uniform_size_varying_color_outline_smooth_2D) {
1117                 GPU_shader_free(GG.shaders.point_uniform_size_varying_color_outline_smooth_2D);
1118                 GG.shaders.point_uniform_size_varying_color_outline_smooth_2D = NULL;
1119         }
1120
1121         if (GG.shaders.point_fixed_size_uniform_color_3D) {
1122                 GPU_shader_free(GG.shaders.point_fixed_size_uniform_color_3D);
1123                 GG.shaders.point_fixed_size_uniform_color_3D = NULL;
1124         }
1125
1126         if (GG.shaders.point_fixed_size_varying_color_3D) {
1127                 GPU_shader_free(GG.shaders.point_fixed_size_varying_color_3D);
1128                 GG.shaders.point_fixed_size_varying_color_3D = NULL;
1129         }
1130
1131         if (GG.shaders.point_varying_size_uniform_color_3D) {
1132                 GPU_shader_free(GG.shaders.point_varying_size_uniform_color_3D);
1133                 GG.shaders.point_varying_size_uniform_color_3D = NULL;
1134         }
1135
1136         if (GG.shaders.point_varying_size_varying_color_3D) {
1137                 GPU_shader_free(GG.shaders.point_varying_size_varying_color_3D);
1138                 GG.shaders.point_varying_size_varying_color_3D = NULL;
1139         }
1140
1141         if (GG.shaders.point_uniform_size_uniform_color_smooth_3D) {
1142                 GPU_shader_free(GG.shaders.point_uniform_size_uniform_color_smooth_3D);
1143                 GG.shaders.point_uniform_size_uniform_color_smooth_3D = NULL;
1144         }
1145
1146         if (GG.shaders.point_uniform_size_uniform_color_outline_smooth_3D) {
1147                 GPU_shader_free(GG.shaders.point_uniform_size_uniform_color_outline_smooth_3D);
1148                 GG.shaders.point_uniform_size_uniform_color_outline_smooth_3D = NULL;
1149         }
1150
1151         for (int i = 0; i < 2 * MAX_FX_SHADERS; ++i) {
1152                 if (GG.shaders.fx_shaders[i]) {
1153                         GPU_shader_free(GG.shaders.fx_shaders[i]);
1154                         GG.shaders.fx_shaders[i] = NULL;
1155                 }
1156         }
1157 }