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