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