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