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