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