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