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