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