486858b9c8222ae5d9d633845953457f949c38c1
[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         if (GPU_bicubic_bump_support())
262                 strcat(defines, "#define BUMP_BICUBIC\n");
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,
276                        "struct VertexData {\n"
277                        "  vec4 position;\n"
278                        "  vec3 normal;\n"
279                        "  vec2 uv;"
280                        "};\n");
281         }
282 #else
283         UNUSED_VARS(use_opensubdiv);
284 #endif
285
286         return;
287 }
288
289 GPUShader *GPU_shader_create(
290         const char *vertexcode,
291         const char *fragcode,
292         const char *geocode,
293         const char *libcode,
294         const char *defines,
295         const char *shname)
296 {
297         return GPU_shader_create_ex(
298                 vertexcode,
299                 fragcode,
300                 geocode,
301                 libcode,
302                 defines,
303                 GPU_SHADER_FLAGS_NONE,
304                 GPU_SHADER_TFB_NONE,
305                 NULL,
306                 0,
307                 shname);
308 }
309
310 #define DEBUG_SHADER_NONE ""
311 #define DEBUG_SHADER_VERTEX "vert"
312 #define DEBUG_SHADER_FRAGMENT "frag"
313 #define DEBUG_SHADER_GEOMETRY "geom"
314
315 /**
316  * Dump GLSL shaders to disk
317  *
318  * This is used for profiling shader performance externally and debug if shader code is correct.
319  * If called with no code, it simply bumps the shader index, so different shaders for the same
320  * program share the same index.
321  */
322 static void gpu_dump_shaders(const char **code, const int num_shaders, const char *extension)
323 {
324         if ((G.debug & G_DEBUG_GPU_SHADERS) == 0) {
325                 return;
326         }
327
328         /* We use the same shader index for shaders in the same program.
329          * So we call this function once before calling for the invidual shaders. */
330         static int shader_index = 0;
331         if (code == NULL) {
332                 shader_index++;
333                 BLI_assert(STREQ(DEBUG_SHADER_NONE, extension));
334                 return;
335         }
336
337         /* Determine the full path of the new shader. */
338         char shader_path[FILE_MAX];
339
340         char file_name[512] = {'\0'};
341         sprintf(file_name, "%04d.%s", shader_index, extension);
342
343         BLI_join_dirfile(shader_path, sizeof(shader_path), BKE_tempdir_session(), file_name);
344
345         /* Write shader to disk. */
346         FILE *f = fopen(shader_path, "w");
347         if (f == NULL) {
348                 printf("Error writing to file: %s\n", shader_path);
349         }
350         for (int j = 0; j < num_shaders; j++) {
351                 fprintf(f, "%s", code[j]);
352         }
353         fclose(f);
354         printf("Shader file written to disk: %s\n", shader_path);
355 }
356
357 GPUShader *GPU_shader_create_ex(
358         const char *vertexcode,
359         const char *fragcode,
360         const char *geocode,
361         const char *libcode,
362         const char *defines,
363         const int flags,
364         const GPUShaderTFBType tf_type,
365         const char **tf_names,
366         const int tf_count,
367         const char *shname)
368 {
369 #ifdef WITH_OPENSUBDIV
370         bool use_opensubdiv = (flags & GPU_SHADER_FLAGS_SPECIAL_OPENSUBDIV) != 0;
371 #else
372         UNUSED_VARS(flags);
373         bool use_opensubdiv = false;
374 #endif
375         GLint status;
376         GLchar log[5000];
377         GLsizei length = 0;
378         GPUShader *shader;
379         char standard_defines[MAX_DEFINE_LENGTH] = "";
380         char standard_extensions[MAX_EXT_DEFINE_LENGTH] = "";
381
382         shader = MEM_callocN(sizeof(GPUShader), "GPUShader");
383         gpu_dump_shaders(NULL, 0, DEBUG_SHADER_NONE);
384
385 #ifndef NDEBUG
386         BLI_snprintf(shader->name, sizeof(shader->name), "%s_%u", shname, g_shaderid++);
387 #else
388         UNUSED_VARS(shname);
389 #endif
390
391         if (vertexcode)
392                 shader->vertex = glCreateShader(GL_VERTEX_SHADER);
393         if (fragcode)
394                 shader->fragment = glCreateShader(GL_FRAGMENT_SHADER);
395         if (geocode)
396                 shader->geometry = glCreateShader(GL_GEOMETRY_SHADER_EXT);
397
398         shader->program = glCreateProgram();
399
400         if (!shader->program ||
401             (vertexcode && !shader->vertex) ||
402             (fragcode && !shader->fragment) ||
403             (geocode && !shader->geometry))
404         {
405                 fprintf(stderr, "GPUShader, object creation failed.\n");
406                 GPU_shader_free(shader);
407                 return NULL;
408         }
409
410         gpu_shader_standard_defines(
411                 standard_defines,
412                 use_opensubdiv);
413         gpu_shader_standard_extensions(standard_extensions);
414
415         if (vertexcode) {
416                 const char *source[5];
417                 /* custom limit, may be too small, beware */
418                 int num_source = 0;
419
420                 source[num_source++] = gpu_shader_version();
421                 source[num_source++] = standard_extensions;
422                 source[num_source++] = standard_defines;
423
424                 if (defines) source[num_source++] = defines;
425                 source[num_source++] = vertexcode;
426
427                 gpu_dump_shaders(source, num_source, DEBUG_SHADER_VERTEX);
428
429                 glAttachShader(shader->program, shader->vertex);
430                 glShaderSource(shader->vertex, num_source, source, NULL);
431
432                 glCompileShader(shader->vertex);
433                 glGetShaderiv(shader->vertex, GL_COMPILE_STATUS, &status);
434
435                 if (!status) {
436                         glGetShaderInfoLog(shader->vertex, sizeof(log), &length, log);
437                         shader_print_errors("compile", log, source, num_source);
438
439                         GPU_shader_free(shader);
440                         return NULL;
441                 }
442         }
443
444         if (fragcode) {
445                 const char *source[7];
446                 int num_source = 0;
447
448                 source[num_source++] = gpu_shader_version();
449                 source[num_source++] = standard_extensions;
450                 source[num_source++] = standard_defines;
451
452 #ifdef WITH_OPENSUBDIV
453                 /* TODO(sergey): Move to fragment shader source code generation. */
454                 if (use_opensubdiv) {
455                         source[num_source++] = (
456                                 "#ifdef USE_OPENSUBDIV\n"
457                                 "in block {\n"
458                                 "       VertexData v;\n"
459                                 "} inpt;\n"
460                                 "#endif\n"
461                         );
462                 }
463 #endif
464
465                 if (defines) source[num_source++] = defines;
466                 if (libcode) source[num_source++] = libcode;
467                 source[num_source++] = fragcode;
468
469                 gpu_dump_shaders(source, num_source, DEBUG_SHADER_FRAGMENT);
470
471                 glAttachShader(shader->program, shader->fragment);
472                 glShaderSource(shader->fragment, num_source, source, NULL);
473
474                 glCompileShader(shader->fragment);
475                 glGetShaderiv(shader->fragment, GL_COMPILE_STATUS, &status);
476
477                 if (!status) {
478                         glGetShaderInfoLog(shader->fragment, sizeof(log), &length, log);
479                         shader_print_errors("compile", log, source, num_source);
480
481                         GPU_shader_free(shader);
482                         return NULL;
483                 }
484         }
485
486         if (geocode) {
487                 const char *source[6];
488                 int num_source = 0;
489
490                 source[num_source++] = gpu_shader_version();
491                 source[num_source++] = standard_extensions;
492                 source[num_source++] = standard_defines;
493
494                 if (defines) source[num_source++] = defines;
495                 source[num_source++] = geocode;
496
497                 gpu_dump_shaders(source, num_source, DEBUG_SHADER_GEOMETRY);
498
499                 glAttachShader(shader->program, shader->geometry);
500                 glShaderSource(shader->geometry, num_source, source, NULL);
501
502                 glCompileShader(shader->geometry);
503                 glGetShaderiv(shader->geometry, GL_COMPILE_STATUS, &status);
504
505                 if (!status) {
506                         glGetShaderInfoLog(shader->geometry, sizeof(log), &length, log);
507                         shader_print_errors("compile", log, source, num_source);
508
509                         GPU_shader_free(shader);
510                         return NULL;
511                 }
512         }
513
514 #ifdef WITH_OPENSUBDIV
515         if (use_opensubdiv) {
516                 glBindAttribLocation(shader->program, 0, "position");
517                 glBindAttribLocation(shader->program, 1, "normal");
518         }
519 #endif
520
521         if (tf_names != NULL) {
522                 glTransformFeedbackVaryings(shader->program, tf_count, tf_names, GL_INTERLEAVED_ATTRIBS);
523                 /* Primitive type must be setup */
524                 BLI_assert(tf_type != GPU_SHADER_TFB_NONE);
525                 shader->feedback_transform_type = tf_type;
526         }
527
528         glLinkProgram(shader->program);
529         glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
530         if (!status) {
531                 glGetProgramInfoLog(shader->program, sizeof(log), &length, log);
532                 /* print attached shaders in pipeline order */
533                 if (vertexcode) shader_print_errors("linking", log, &vertexcode, 1);
534                 if (geocode) shader_print_errors("linking", log, &geocode, 1);
535                 if (libcode) shader_print_errors("linking", log, &libcode, 1);
536                 if (fragcode) shader_print_errors("linking", log, &fragcode, 1);
537
538                 GPU_shader_free(shader);
539                 return NULL;
540         }
541
542         shader->interface = GPU_shaderinterface_create(shader->program);
543
544 #ifdef WITH_OPENSUBDIV
545         /* TODO(sergey): Find a better place for this. */
546         if (use_opensubdiv) {
547                 if (GLEW_VERSION_4_1) {
548                         glProgramUniform1i(
549                                 shader->program,
550                                 GPU_shaderinterface_uniform(shader->interface, "FVarDataOffsetBuffer")->location,
551                                 30);  /* GL_TEXTURE30 */
552
553                         glProgramUniform1i(
554                                 shader->program,
555                                 GPU_shaderinterface_uniform(shader->interface, "FVarDataBuffer")->location,
556                                 31);  /* GL_TEXTURE31 */
557                 }
558                 else {
559                         glUseProgram(shader->program);
560                         glUniform1i(GPU_shaderinterface_uniform(shader->interface, "FVarDataOffsetBuffer")->location, 30);
561                         glUniform1i(GPU_shaderinterface_uniform(shader->interface, "FVarDataBuffer")->location, 31);
562                         glUseProgram(0);
563                 }
564         }
565 #endif
566
567         return shader;
568 }
569
570 #undef DEBUG_SHADER_GEOMETRY
571 #undef DEBUG_SHADER_FRAGMENT
572 #undef DEBUG_SHADER_VERTEX
573 #undef DEBUG_SHADER_NONE
574
575 void GPU_shader_bind(GPUShader *shader)
576 {
577         BLI_assert(shader && shader->program);
578
579         glUseProgram(shader->program);
580         GPU_matrix_bind(shader->interface);
581 }
582
583 void GPU_shader_unbind(void)
584 {
585         glUseProgram(0);
586 }
587
588 bool GPU_shader_transform_feedback_enable(GPUShader *shader, unsigned int vbo_id)
589 {
590         if (shader->feedback_transform_type == GPU_SHADER_TFB_NONE) {
591                 return false;
592         }
593
594         glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, vbo_id);
595
596         switch (shader->feedback_transform_type) {
597                 case GPU_SHADER_TFB_POINTS:    glBeginTransformFeedback(GL_POINTS);    return true;
598                 case GPU_SHADER_TFB_LINES:     glBeginTransformFeedback(GL_LINES);     return true;
599                 case GPU_SHADER_TFB_TRIANGLES: glBeginTransformFeedback(GL_TRIANGLES); return true;
600                 default: return false;
601         }
602 }
603
604 void GPU_shader_transform_feedback_disable(GPUShader *UNUSED(shader))
605 {
606         glEndTransformFeedback();
607 }
608
609 void GPU_shader_free(GPUShader *shader)
610 {
611 #if 0 /* Would be nice to have, but for now the Deferred compilation
612        * does not have a GPUContext. */
613         BLI_assert(GPU_context_active_get() != NULL);
614 #endif
615         BLI_assert(shader);
616
617         if (shader->vertex)
618                 glDeleteShader(shader->vertex);
619         if (shader->geometry)
620                 glDeleteShader(shader->geometry);
621         if (shader->fragment)
622                 glDeleteShader(shader->fragment);
623         if (shader->program)
624                 glDeleteProgram(shader->program);
625
626         if (shader->interface)
627                 GPU_shaderinterface_discard(shader->interface);
628
629         MEM_freeN(shader);
630 }
631
632 int GPU_shader_get_uniform(GPUShader *shader, const char *name)
633 {
634         BLI_assert(shader && shader->program);
635         const GPUShaderInput *uniform = GPU_shaderinterface_uniform(shader->interface, name);
636         return uniform ? uniform->location : -1;
637 }
638
639 int GPU_shader_get_builtin_uniform(GPUShader *shader, int builtin)
640 {
641         BLI_assert(shader && shader->program);
642         const GPUShaderInput *uniform = GPU_shaderinterface_uniform_builtin(shader->interface, builtin);
643         return uniform ? uniform->location : -1;
644 }
645
646 int GPU_shader_get_uniform_block(GPUShader *shader, const char *name)
647 {
648         BLI_assert(shader && shader->program);
649
650         const GPUShaderInput *ubo = GPU_shaderinterface_ubo(shader->interface, name);
651         return ubo ? ubo->location : -1;
652 }
653
654 void *GPU_shader_get_interface(GPUShader *shader)
655 {
656         return shader->interface;
657 }
658
659 /* Clement : Temp */
660 int GPU_shader_get_program(GPUShader *shader)
661 {
662         return (int)shader->program;
663 }
664
665 void GPU_shader_uniform_vector(GPUShader *UNUSED(shader), int location, int length, int arraysize, const float *value)
666 {
667         if (location == -1 || value == NULL)
668                 return;
669
670         if (length == 1) glUniform1fv(location, arraysize, value);
671         else if (length == 2) glUniform2fv(location, arraysize, value);
672         else if (length == 3) glUniform3fv(location, arraysize, value);
673         else if (length == 4) glUniform4fv(location, arraysize, value);
674         else if (length == 9) glUniformMatrix3fv(location, arraysize, 0, value);
675         else if (length == 16) glUniformMatrix4fv(location, arraysize, 0, value);
676 }
677
678 void GPU_shader_uniform_vector_int(GPUShader *UNUSED(shader), int location, int length, int arraysize, const int *value)
679 {
680         if (location == -1)
681                 return;
682
683         if (length == 1) glUniform1iv(location, arraysize, value);
684         else if (length == 2) glUniform2iv(location, arraysize, value);
685         else if (length == 3) glUniform3iv(location, arraysize, value);
686         else if (length == 4) glUniform4iv(location, arraysize, value);
687 }
688
689 void GPU_shader_uniform_int(GPUShader *UNUSED(shader), int location, int value)
690 {
691         if (location == -1)
692                 return;
693
694         glUniform1i(location, value);
695 }
696
697 void GPU_shader_uniform_buffer(GPUShader *shader, int location, GPUUniformBuffer *ubo)
698 {
699         int bindpoint = GPU_uniformbuffer_bindpoint(ubo);
700
701         if (location == -1) {
702                 return;
703         }
704
705         glUniformBlockBinding(shader->program, location, bindpoint);
706 }
707
708 void GPU_shader_uniform_texture(GPUShader *UNUSED(shader), int location, GPUTexture *tex)
709 {
710         int number = GPU_texture_bound_number(tex);
711
712         if (number == -1) {
713                 fprintf(stderr, "Texture is not bound.\n");
714                 BLI_assert(0);
715                 return;
716         }
717
718         if (location == -1)
719                 return;
720
721         glUniform1i(location, number);
722 }
723
724 int GPU_shader_get_attribute(GPUShader *shader, const char *name)
725 {
726         BLI_assert(shader && shader->program);
727         const GPUShaderInput *attrib = GPU_shaderinterface_attr(shader->interface, name);
728         return attrib ? attrib->location : -1;
729 }
730
731 GPUShader *GPU_shader_get_builtin_shader(GPUBuiltinShader shader)
732 {
733         BLI_assert(shader != GPU_NUM_BUILTIN_SHADERS); /* don't be a troll */
734
735         static const GPUShaderStages builtin_shader_stages[GPU_NUM_BUILTIN_SHADERS] = {
736                 [GPU_SHADER_SMOKE] = { datatoc_gpu_shader_smoke_vert_glsl, datatoc_gpu_shader_smoke_frag_glsl },
737                 [GPU_SHADER_SMOKE_FIRE] = { datatoc_gpu_shader_smoke_vert_glsl, datatoc_gpu_shader_smoke_frag_glsl },
738                 [GPU_SHADER_SMOKE_COBA] = { datatoc_gpu_shader_smoke_vert_glsl, datatoc_gpu_shader_smoke_frag_glsl },
739
740                 [GPU_SHADER_TEXT] = { datatoc_gpu_shader_text_vert_glsl,
741                                       datatoc_gpu_shader_text_frag_glsl,
742                                       datatoc_gpu_shader_text_geom_glsl },
743                 [GPU_SHADER_TEXT_SIMPLE] = { datatoc_gpu_shader_text_simple_vert_glsl,
744                                              datatoc_gpu_shader_text_frag_glsl,
745                                              datatoc_gpu_shader_text_simple_geom_glsl },
746                 [GPU_SHADER_KEYFRAME_DIAMOND] = { datatoc_gpu_shader_keyframe_diamond_vert_glsl,
747                                                   datatoc_gpu_shader_keyframe_diamond_frag_glsl },
748                 [GPU_SHADER_EDGES_FRONT_BACK_PERSP] = { datatoc_gpu_shader_edges_front_back_persp_vert_glsl,
749                         /*  this version is     */      datatoc_gpu_shader_flat_color_frag_glsl,
750                        /*  magical but slooow  */       datatoc_gpu_shader_edges_front_back_persp_geom_glsl },
751                 [GPU_SHADER_EDGES_FRONT_BACK_ORTHO] = { datatoc_gpu_shader_edges_front_back_ortho_vert_glsl,
752                                                         datatoc_gpu_shader_flat_color_frag_glsl },
753                 [GPU_SHADER_EDGES_OVERLAY_SIMPLE] = { datatoc_gpu_shader_3D_vert_glsl, datatoc_gpu_shader_edges_overlay_frag_glsl,
754                                                       datatoc_gpu_shader_edges_overlay_simple_geom_glsl },
755                 [GPU_SHADER_EDGES_OVERLAY] = { datatoc_gpu_shader_edges_overlay_vert_glsl,
756                                                datatoc_gpu_shader_edges_overlay_frag_glsl,
757                                                datatoc_gpu_shader_edges_overlay_geom_glsl },
758                 [GPU_SHADER_SIMPLE_LIGHTING] = { datatoc_gpu_shader_3D_normal_vert_glsl, datatoc_gpu_shader_simple_lighting_frag_glsl },
759                 /* Use 'USE_FLAT_NORMAL' to make flat shader from smooth  */
760                 [GPU_SHADER_SIMPLE_LIGHTING_FLAT_COLOR] = { datatoc_gpu_shader_3D_normal_smooth_color_vert_glsl, datatoc_gpu_shader_simple_lighting_smooth_color_frag_glsl },
761                 [GPU_SHADER_SIMPLE_LIGHTING_SMOOTH_COLOR] = { datatoc_gpu_shader_3D_normal_smooth_color_vert_glsl, datatoc_gpu_shader_simple_lighting_smooth_color_frag_glsl },
762                 [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 },
763
764                 [GPU_SHADER_2D_IMAGE_MASK_UNIFORM_COLOR] = { datatoc_gpu_shader_3D_image_vert_glsl,
765                                                              datatoc_gpu_shader_image_mask_uniform_color_frag_glsl },
766                 [GPU_SHADER_3D_IMAGE_MODULATE_ALPHA] = { datatoc_gpu_shader_3D_image_vert_glsl,
767                                                          datatoc_gpu_shader_image_modulate_alpha_frag_glsl },
768                 [GPU_SHADER_3D_IMAGE_DEPTH] = { datatoc_gpu_shader_3D_image_vert_glsl,
769                                                 datatoc_gpu_shader_image_depth_linear_frag_glsl },
770                 [GPU_SHADER_3D_IMAGE_DEPTH_COPY] = { datatoc_gpu_shader_3D_image_vert_glsl,
771                                                      datatoc_gpu_shader_image_depth_copy_frag_glsl },
772                 [GPU_SHADER_2D_IMAGE_MULTISAMPLE_2] = { datatoc_gpu_shader_2D_vert_glsl, datatoc_gpu_shader_image_multisample_resolve_frag_glsl },
773                 [GPU_SHADER_2D_IMAGE_MULTISAMPLE_4] = { datatoc_gpu_shader_2D_vert_glsl, datatoc_gpu_shader_image_multisample_resolve_frag_glsl },
774                 [GPU_SHADER_2D_IMAGE_MULTISAMPLE_8] = { datatoc_gpu_shader_2D_vert_glsl, datatoc_gpu_shader_image_multisample_resolve_frag_glsl },
775                 [GPU_SHADER_2D_IMAGE_MULTISAMPLE_16] = { datatoc_gpu_shader_2D_vert_glsl, datatoc_gpu_shader_image_multisample_resolve_frag_glsl },
776                 [GPU_SHADER_2D_IMAGE_MULTISAMPLE_2_DEPTH_TEST] = { datatoc_gpu_shader_2D_vert_glsl, datatoc_gpu_shader_image_multisample_resolve_frag_glsl },
777                 [GPU_SHADER_2D_IMAGE_MULTISAMPLE_4_DEPTH_TEST] = { datatoc_gpu_shader_2D_vert_glsl, datatoc_gpu_shader_image_multisample_resolve_frag_glsl },
778                 [GPU_SHADER_2D_IMAGE_MULTISAMPLE_8_DEPTH_TEST] = { datatoc_gpu_shader_2D_vert_glsl, datatoc_gpu_shader_image_multisample_resolve_frag_glsl },
779                 [GPU_SHADER_2D_IMAGE_MULTISAMPLE_16_DEPTH_TEST] = { datatoc_gpu_shader_2D_vert_glsl, datatoc_gpu_shader_image_multisample_resolve_frag_glsl },
780
781                 [GPU_SHADER_2D_IMAGE_INTERLACE] = { datatoc_gpu_shader_2D_image_vert_glsl,
782                                                     datatoc_gpu_shader_image_interlace_frag_glsl },
783                 [GPU_SHADER_2D_CHECKER] = { datatoc_gpu_shader_2D_vert_glsl, datatoc_gpu_shader_checker_frag_glsl },
784
785                 [GPU_SHADER_2D_DIAG_STRIPES] = { datatoc_gpu_shader_2D_vert_glsl, datatoc_gpu_shader_diag_stripes_frag_glsl },
786
787                 [GPU_SHADER_2D_UNIFORM_COLOR] = { datatoc_gpu_shader_2D_vert_glsl, datatoc_gpu_shader_uniform_color_frag_glsl },
788                 [GPU_SHADER_2D_FLAT_COLOR] = { datatoc_gpu_shader_2D_flat_color_vert_glsl,
789                                                datatoc_gpu_shader_flat_color_frag_glsl },
790                 [GPU_SHADER_2D_SMOOTH_COLOR] = { datatoc_gpu_shader_2D_smooth_color_vert_glsl,
791                                                  datatoc_gpu_shader_2D_smooth_color_frag_glsl },
792                 [GPU_SHADER_2D_SMOOTH_COLOR_DITHER] = { datatoc_gpu_shader_2D_smooth_color_vert_glsl,
793                                                         datatoc_gpu_shader_2D_smooth_color_dithered_frag_glsl },
794                 [GPU_SHADER_2D_IMAGE_LINEAR_TO_SRGB] = { datatoc_gpu_shader_2D_image_vert_glsl,
795                                                          datatoc_gpu_shader_image_linear_frag_glsl },
796                 [GPU_SHADER_2D_IMAGE] = { datatoc_gpu_shader_2D_image_vert_glsl,
797                                           datatoc_gpu_shader_image_frag_glsl },
798                 [GPU_SHADER_2D_IMAGE_COLOR] = { datatoc_gpu_shader_2D_image_vert_glsl,
799                                                 datatoc_gpu_shader_image_color_frag_glsl },
800                 [GPU_SHADER_2D_IMAGE_DESATURATE_COLOR] = { datatoc_gpu_shader_2D_image_vert_glsl,
801                                                            datatoc_gpu_shader_image_desaturate_frag_glsl },
802                 [GPU_SHADER_2D_IMAGE_ALPHA_COLOR] = { datatoc_gpu_shader_2D_image_vert_glsl,
803                                                       datatoc_gpu_shader_image_alpha_color_frag_glsl },
804                 [GPU_SHADER_2D_IMAGE_SHUFFLE_COLOR] = { datatoc_gpu_shader_2D_image_vert_glsl,
805                                                         datatoc_gpu_shader_image_shuffle_color_frag_glsl },
806                 [GPU_SHADER_2D_IMAGE_RECT_COLOR] = { datatoc_gpu_shader_2D_image_rect_vert_glsl,
807                                                      datatoc_gpu_shader_image_color_frag_glsl },
808                 [GPU_SHADER_2D_IMAGE_MULTI_RECT_COLOR] = { datatoc_gpu_shader_2D_image_multi_rect_vert_glsl,
809                                                            datatoc_gpu_shader_image_varying_color_frag_glsl },
810
811                 [GPU_SHADER_3D_UNIFORM_COLOR] = { datatoc_gpu_shader_3D_vert_glsl, datatoc_gpu_shader_uniform_color_frag_glsl },
812                 [GPU_SHADER_3D_UNIFORM_COLOR_U32] = { datatoc_gpu_shader_3D_vert_glsl, datatoc_gpu_shader_uniform_color_frag_glsl },
813                 [GPU_SHADER_3D_FLAT_COLOR] = { datatoc_gpu_shader_3D_flat_color_vert_glsl,
814                                                datatoc_gpu_shader_flat_color_frag_glsl },
815                 [GPU_SHADER_3D_FLAT_COLOR_U32] = { datatoc_gpu_shader_3D_flat_color_vert_glsl,
816                                                    datatoc_gpu_shader_flat_color_frag_glsl },
817                 [GPU_SHADER_3D_SMOOTH_COLOR] = { datatoc_gpu_shader_3D_smooth_color_vert_glsl,
818                                                  datatoc_gpu_shader_3D_smooth_color_frag_glsl },
819                 [GPU_SHADER_3D_DEPTH_ONLY] = { datatoc_gpu_shader_3D_vert_glsl, datatoc_gpu_shader_depth_only_frag_glsl },
820                 [GPU_SHADER_3D_CLIPPED_UNIFORM_COLOR] = { datatoc_gpu_shader_3D_clipped_uniform_color_vert_glsl,
821                                                           datatoc_gpu_shader_uniform_color_frag_glsl },
822
823                 [GPU_SHADER_3D_GROUNDPOINT] = { datatoc_gpu_shader_3D_groundpoint_vert_glsl, datatoc_gpu_shader_point_uniform_color_frag_glsl },
824                 [GPU_SHADER_3D_GROUNDLINE] = { datatoc_gpu_shader_3D_passthrough_vert_glsl,
825                                                datatoc_gpu_shader_uniform_color_frag_glsl,
826                                                datatoc_gpu_shader_3D_groundline_geom_glsl },
827
828                 [GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR] = { datatoc_gpu_shader_2D_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                 [GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR] = { datatoc_gpu_shader_3D_line_dashed_uniform_color_vert_glsl,
832                                                               datatoc_gpu_shader_2D_line_dashed_frag_glsl,
833                                                               datatoc_gpu_shader_2D_line_dashed_geom_glsl },
834
835                 [GPU_SHADER_3D_OBJECTSPACE_SIMPLE_LIGHTING_VARIYING_COLOR] =
836                     { datatoc_gpu_shader_instance_objectspace_variying_color_vert_glsl,
837                       datatoc_gpu_shader_simple_lighting_frag_glsl},
838                 [GPU_SHADER_3D_OBJECTSPACE_VARIYING_COLOR] = { datatoc_gpu_shader_instance_objectspace_variying_color_vert_glsl,
839                                                                datatoc_gpu_shader_flat_color_frag_glsl},
840                 [GPU_SHADER_3D_SCREENSPACE_VARIYING_COLOR] = { datatoc_gpu_shader_instance_screenspace_variying_color_vert_glsl,
841                                                                datatoc_gpu_shader_flat_color_frag_glsl},
842                 [GPU_SHADER_3D_INSTANCE_SCREEN_ALIGNED_AXIS] = { datatoc_gpu_shader_instance_screen_aligned_vert_glsl,
843                                                                  datatoc_gpu_shader_flat_color_frag_glsl},
844                 [GPU_SHADER_3D_INSTANCE_SCREEN_ALIGNED] = { datatoc_gpu_shader_instance_screen_aligned_vert_glsl,
845                                                             datatoc_gpu_shader_flat_color_frag_glsl},
846
847                 [GPU_SHADER_CAMERA] = { datatoc_gpu_shader_instance_camera_vert_glsl,
848                                         datatoc_gpu_shader_flat_color_frag_glsl},
849                 [GPU_SHADER_DISTANCE_LINES] = { datatoc_gpu_shader_instance_distance_line_vert_glsl,
850                                                 datatoc_gpu_shader_flat_color_frag_glsl},
851
852                 [GPU_SHADER_2D_POINT_FIXED_SIZE_UNIFORM_COLOR] =
853                         { datatoc_gpu_shader_2D_vert_glsl, datatoc_gpu_shader_point_uniform_color_frag_glsl },
854                 [GPU_SHADER_2D_POINT_VARYING_SIZE_VARYING_COLOR] =
855                         { datatoc_gpu_shader_2D_point_varying_size_varying_color_vert_glsl,
856                           datatoc_gpu_shader_point_varying_color_frag_glsl },
857                 [GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA] =
858                         { datatoc_gpu_shader_2D_point_uniform_size_aa_vert_glsl,
859                           datatoc_gpu_shader_point_uniform_color_aa_frag_glsl },
860                 [GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA] =
861                         { datatoc_gpu_shader_2D_point_uniform_size_outline_aa_vert_glsl,
862                           datatoc_gpu_shader_point_uniform_color_outline_aa_frag_glsl },
863                 [GPU_SHADER_2D_POINT_UNIFORM_SIZE_VARYING_COLOR_OUTLINE_AA] =
864                         { datatoc_gpu_shader_2D_point_uniform_size_varying_color_outline_aa_vert_glsl,
865                           datatoc_gpu_shader_point_varying_color_outline_aa_frag_glsl },
866                 [GPU_SHADER_3D_POINT_FIXED_SIZE_UNIFORM_COLOR] = { datatoc_gpu_shader_3D_vert_glsl,
867                                                                    datatoc_gpu_shader_point_uniform_color_frag_glsl },
868                 [GPU_SHADER_3D_POINT_FIXED_SIZE_VARYING_COLOR] = { datatoc_gpu_shader_3D_point_fixed_size_varying_color_vert_glsl,
869                                                                    datatoc_gpu_shader_point_varying_color_frag_glsl },
870                 [GPU_SHADER_3D_POINT_VARYING_SIZE_UNIFORM_COLOR] = { datatoc_gpu_shader_3D_point_varying_size_vert_glsl,
871                                                                      datatoc_gpu_shader_point_uniform_color_frag_glsl },
872                 [GPU_SHADER_3D_POINT_VARYING_SIZE_VARYING_COLOR] =
873                         { datatoc_gpu_shader_3D_point_varying_size_varying_color_vert_glsl,
874                           datatoc_gpu_shader_point_varying_color_frag_glsl },
875                 [GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA] =
876                         { datatoc_gpu_shader_3D_point_uniform_size_aa_vert_glsl,
877                           datatoc_gpu_shader_point_uniform_color_aa_frag_glsl },
878                 [GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA] =
879                         { datatoc_gpu_shader_3D_point_uniform_size_outline_aa_vert_glsl,
880                           datatoc_gpu_shader_point_uniform_color_outline_aa_frag_glsl },
881
882                 [GPU_SHADER_INSTANCE_UNIFORM_COLOR] = { datatoc_gpu_shader_instance_vert_glsl, datatoc_gpu_shader_uniform_color_frag_glsl },
883                 [GPU_SHADER_INSTANCE_VARIYING_ID_VARIYING_SIZE] =
884                     { datatoc_gpu_shader_instance_variying_size_variying_id_vert_glsl,
885                       datatoc_gpu_shader_flat_id_frag_glsl },
886                 [GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SIZE] =
887                     { datatoc_gpu_shader_instance_variying_size_variying_color_vert_glsl,
888                       datatoc_gpu_shader_flat_color_frag_glsl },
889                 [GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SCALE] =
890                     { datatoc_gpu_shader_instance_variying_size_variying_color_vert_glsl,
891                       datatoc_gpu_shader_flat_color_frag_glsl },
892                 [GPU_SHADER_INSTANCE_EDGES_VARIYING_COLOR] = { datatoc_gpu_shader_instance_edges_variying_color_vert_glsl,
893                                                                datatoc_gpu_shader_flat_color_frag_glsl,
894                                                                datatoc_gpu_shader_instance_edges_variying_color_geom_glsl},
895
896                 [GPU_SHADER_2D_AREA_EDGES] = { datatoc_gpu_shader_2D_area_borders_vert_glsl,
897                                                datatoc_gpu_shader_uniform_color_frag_glsl},
898                 [GPU_SHADER_2D_WIDGET_BASE] = { datatoc_gpu_shader_2D_widget_base_vert_glsl,
899                                                 datatoc_gpu_shader_2D_widget_base_frag_glsl},
900                 [GPU_SHADER_2D_WIDGET_BASE_INST] = { datatoc_gpu_shader_2D_widget_base_vert_glsl,
901                                                      datatoc_gpu_shader_2D_widget_base_frag_glsl},
902                 [GPU_SHADER_2D_WIDGET_SHADOW] = { datatoc_gpu_shader_2D_widget_shadow_vert_glsl,
903                                                   datatoc_gpu_shader_2D_widget_shadow_frag_glsl },
904                 [GPU_SHADER_2D_NODELINK] = { datatoc_gpu_shader_2D_nodelink_vert_glsl,
905                                              datatoc_gpu_shader_2D_nodelink_frag_glsl },
906                 [GPU_SHADER_2D_NODELINK_INST] = { datatoc_gpu_shader_2D_nodelink_vert_glsl,
907                                                   datatoc_gpu_shader_2D_nodelink_frag_glsl },
908
909                 [GPU_SHADER_GPENCIL_STROKE] = { datatoc_gpu_shader_gpencil_stroke_vert_glsl,
910                                                 datatoc_gpu_shader_gpencil_stroke_frag_glsl,
911                                                 datatoc_gpu_shader_gpencil_stroke_geom_glsl },
912
913                 [GPU_SHADER_GPENCIL_FILL] = { datatoc_gpu_shader_gpencil_fill_vert_glsl,
914                                               datatoc_gpu_shader_gpencil_fill_frag_glsl },
915         };
916
917         if (builtin_shaders[shader] == NULL) {
918                 /* just a few special cases */
919                 const char *defines = NULL;
920                 switch (shader) {
921                         case GPU_SHADER_2D_IMAGE_MULTISAMPLE_2:
922                                 defines = "#define SAMPLES 2\n";
923                                 break;
924                         case GPU_SHADER_2D_IMAGE_MULTISAMPLE_2_DEPTH_TEST:
925                                 defines = "#define SAMPLES 2\n"
926                                           "#define USE_DEPTH\n";
927                                 break;
928                         case GPU_SHADER_2D_IMAGE_MULTISAMPLE_4:
929                                 defines = "#define SAMPLES 4\n";
930                                 break;
931                         case GPU_SHADER_2D_IMAGE_MULTISAMPLE_4_DEPTH_TEST:
932                                 defines = "#define SAMPLES 4\n"
933                                           "#define USE_DEPTH\n";
934                                 break;
935                         case GPU_SHADER_2D_IMAGE_MULTISAMPLE_8:
936                                 defines = "#define SAMPLES 8\n";
937                                 break;
938                         case GPU_SHADER_2D_IMAGE_MULTISAMPLE_8_DEPTH_TEST:
939                                 defines = "#define SAMPLES 8\n"
940                                           "#define USE_DEPTH\n";
941                                 break;
942                         case GPU_SHADER_2D_IMAGE_MULTISAMPLE_16:
943                                 defines = "#define SAMPLES 16\n";
944                                 break;
945                         case GPU_SHADER_2D_IMAGE_MULTISAMPLE_16_DEPTH_TEST:
946                                 defines = "#define SAMPLES 16\n"
947                                           "#define USE_DEPTH\n";
948                                 break;
949                         case GPU_SHADER_2D_WIDGET_BASE_INST:
950                         case GPU_SHADER_2D_NODELINK_INST:
951                                 defines = "#define USE_INSTANCE\n";
952                                 break;
953                         case GPU_SHADER_SMOKE_COBA:
954                                 defines = "#define USE_COBA\n";
955                                 break;
956                         case GPU_SHADER_INSTANCE_VARIYING_ID_VARIYING_SIZE:
957                         case GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SIZE:
958                                 defines = "#define UNIFORM_SCALE\n";
959                                 break;
960                         case GPU_SHADER_3D_INSTANCE_SCREEN_ALIGNED_AXIS:
961                                 defines = "#define AXIS_NAME\n";
962                                 break;
963                         case GPU_SHADER_3D_OBJECTSPACE_SIMPLE_LIGHTING_VARIYING_COLOR:
964                                 defines = "#define USE_INSTANCE_COLOR\n";
965                                 break;
966                         case GPU_SHADER_3D_FLAT_COLOR_U32:
967                         case GPU_SHADER_3D_UNIFORM_COLOR_U32:
968                                 defines = "#define USE_COLOR_U32\n";
969                                 break;
970                         case GPU_SHADER_SIMPLE_LIGHTING_FLAT_COLOR:
971                                 defines = "#define USE_FLAT_NORMAL\n";
972                                 break;
973                         default:
974                                 break;
975                 }
976
977                 const GPUShaderStages *stages = builtin_shader_stages + shader;
978
979                 if (shader == GPU_SHADER_EDGES_FRONT_BACK_PERSP && !GLEW_VERSION_3_2) {
980                         /* TODO: remove after switch to core profile (maybe) */
981                         static const GPUShaderStages legacy_fancy_edges = {
982                                 datatoc_gpu_shader_edges_front_back_persp_legacy_vert_glsl,
983                                 datatoc_gpu_shader_flat_color_alpha_test_0_frag_glsl,
984                         };
985                         stages = &legacy_fancy_edges;
986                 }
987
988                 if (shader == GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR && !GLEW_VERSION_3_2) {
989                         /* Dashed need geometry shader, which are not supported by legacy OpenGL, fallback to solid lines. */
990                         /* TODO: remove after switch to core profile (maybe) */
991                         static const GPUShaderStages legacy_dashed_lines = {
992                                 datatoc_gpu_shader_3D_line_dashed_uniform_color_legacy_vert_glsl,
993                                 datatoc_gpu_shader_2D_line_dashed_frag_glsl,
994                         };
995                         stages = &legacy_dashed_lines;
996                 }
997
998                 /* common case */
999                 builtin_shaders[shader] = GPU_shader_create(stages->vert, stages->frag, stages->geom, NULL, defines, __func__);
1000         }
1001
1002         return builtin_shaders[shader];
1003 }
1004
1005 #define MAX_DEFINES 100
1006
1007 void GPU_shader_free_builtin_shaders(void)
1008 {
1009         for (int i = 0; i < GPU_NUM_BUILTIN_SHADERS; ++i) {
1010                 if (builtin_shaders[i]) {
1011                         GPU_shader_free(builtin_shaders[i]);
1012                         builtin_shaders[i] = NULL;
1013                 }
1014         }
1015 }