Fix T57633: Particle texture update problem
[blender.git] / source / blender / gpu / intern / gpu_shader.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2005 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Brecht Van Lommel.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/gpu/intern/gpu_shader.c
29  *  \ingroup gpu
30  */
31
32 #include "MEM_guardedalloc.h"
33
34 #include "BLI_utildefines.h"
35 #include "BLI_math_base.h"
36 #include "BLI_math_vector.h"
37 #include "BLI_path_util.h"
38 #include "BLI_string.h"
39
40 #include "BKE_appdir.h"
41 #include "BKE_global.h"
42
43 #include "DNA_space_types.h"
44
45 #include "GPU_extensions.h"
46 #include "GPU_context.h"
47 #include "GPU_matrix.h"
48 #include "GPU_shader.h"
49 #include "GPU_texture.h"
50 #include "GPU_uniformbuffer.h"
51
52 #include "gpu_shader_private.h"
53
54 /* Adjust these constants as needed. */
55 #define MAX_DEFINE_LENGTH 256
56 #define MAX_EXT_DEFINE_LENGTH 256
57
58 /* Non-generated shaders */
59 extern char datatoc_gpu_shader_depth_only_frag_glsl[];
60 extern char datatoc_gpu_shader_uniform_color_frag_glsl[];
61 extern char datatoc_gpu_shader_checker_frag_glsl[];
62 extern char datatoc_gpu_shader_diag_stripes_frag_glsl[];
63 extern char datatoc_gpu_shader_simple_lighting_frag_glsl[];
64 extern char datatoc_gpu_shader_simple_lighting_flat_color_frag_glsl[];
65 extern char datatoc_gpu_shader_simple_lighting_smooth_color_frag_glsl[];
66 extern char datatoc_gpu_shader_simple_lighting_smooth_color_alpha_frag_glsl[];
67 extern char datatoc_gpu_shader_flat_color_frag_glsl[];
68 extern char datatoc_gpu_shader_flat_color_alpha_test_0_frag_glsl[];
69 extern char datatoc_gpu_shader_flat_id_frag_glsl[];
70 extern char datatoc_gpu_shader_2D_area_borders_vert_glsl[];
71 extern char datatoc_gpu_shader_2D_area_borders_frag_glsl[];
72 extern char datatoc_gpu_shader_2D_vert_glsl[];
73 extern char datatoc_gpu_shader_2D_flat_color_vert_glsl[];
74 extern char datatoc_gpu_shader_2D_smooth_color_uniform_alpha_vert_glsl[];
75 extern char datatoc_gpu_shader_2D_smooth_color_vert_glsl[];
76 extern char datatoc_gpu_shader_2D_smooth_color_frag_glsl[];
77 extern char datatoc_gpu_shader_2D_smooth_color_dithered_frag_glsl[];
78 extern char datatoc_gpu_shader_2D_image_vert_glsl[];
79 extern char datatoc_gpu_shader_2D_image_rect_vert_glsl[];
80 extern char datatoc_gpu_shader_2D_image_multi_rect_vert_glsl[];
81 extern char datatoc_gpu_shader_2D_widget_base_vert_glsl[];
82 extern char datatoc_gpu_shader_2D_widget_base_frag_glsl[];
83 extern char datatoc_gpu_shader_2D_widget_shadow_vert_glsl[];
84 extern char datatoc_gpu_shader_2D_widget_shadow_frag_glsl[];
85 extern char datatoc_gpu_shader_2D_nodelink_frag_glsl[];
86 extern char datatoc_gpu_shader_2D_nodelink_vert_glsl[];
87
88 extern char datatoc_gpu_shader_3D_image_vert_glsl[];
89 extern char datatoc_gpu_shader_image_frag_glsl[];
90 extern char datatoc_gpu_shader_image_linear_frag_glsl[];
91 extern char datatoc_gpu_shader_image_color_frag_glsl[];
92 extern char datatoc_gpu_shader_image_desaturate_frag_glsl[];
93 extern char datatoc_gpu_shader_image_varying_color_frag_glsl[];
94 extern char datatoc_gpu_shader_image_alpha_color_frag_glsl[];
95 extern char datatoc_gpu_shader_image_shuffle_color_frag_glsl[];
96 extern char datatoc_gpu_shader_image_interlace_frag_glsl[];
97 extern char datatoc_gpu_shader_image_mask_uniform_color_frag_glsl[];
98 extern char datatoc_gpu_shader_image_modulate_alpha_frag_glsl[];
99 extern char datatoc_gpu_shader_image_depth_linear_frag_glsl[];
100 extern char datatoc_gpu_shader_image_depth_copy_frag_glsl[];
101 extern char datatoc_gpu_shader_image_multisample_resolve_frag_glsl[];
102 extern char datatoc_gpu_shader_3D_vert_glsl[];
103 extern char datatoc_gpu_shader_3D_normal_vert_glsl[];
104 extern char datatoc_gpu_shader_3D_flat_color_vert_glsl[];
105 extern char datatoc_gpu_shader_3D_smooth_color_vert_glsl[];
106 extern char datatoc_gpu_shader_3D_normal_flat_color_vert_glsl[];
107 extern char datatoc_gpu_shader_3D_normal_smooth_color_vert_glsl[];
108 extern char datatoc_gpu_shader_3D_smooth_color_frag_glsl[];
109 extern char datatoc_gpu_shader_3D_passthrough_vert_glsl[];
110 extern char datatoc_gpu_shader_3D_clipped_uniform_color_vert_glsl[];
111
112 extern char datatoc_gpu_shader_instance_vert_glsl[];
113 extern char datatoc_gpu_shader_instance_variying_size_variying_color_vert_glsl[];
114 extern char datatoc_gpu_shader_instance_variying_size_variying_id_vert_glsl[];
115 extern char datatoc_gpu_shader_instance_objectspace_variying_color_vert_glsl[];
116 extern char datatoc_gpu_shader_instance_screenspace_variying_color_vert_glsl[];
117 extern char datatoc_gpu_shader_instance_screen_aligned_vert_glsl[];
118 extern char datatoc_gpu_shader_instance_camera_vert_glsl[];
119 extern char datatoc_gpu_shader_instance_distance_line_vert_glsl[];
120 extern char datatoc_gpu_shader_instance_edges_variying_color_geom_glsl[];
121 extern char datatoc_gpu_shader_instance_edges_variying_color_vert_glsl[];
122 extern char datatoc_gpu_shader_instance_mball_handles_vert_glsl[];
123
124 extern char datatoc_gpu_shader_3D_groundpoint_vert_glsl[];
125 extern char datatoc_gpu_shader_3D_groundline_geom_glsl[];
126
127 extern char datatoc_gpu_shader_point_uniform_color_frag_glsl[];
128 extern char datatoc_gpu_shader_point_uniform_color_aa_frag_glsl[];
129 extern char datatoc_gpu_shader_point_uniform_color_outline_aa_frag_glsl[];
130 extern char datatoc_gpu_shader_point_varying_color_outline_aa_frag_glsl[];
131 extern char datatoc_gpu_shader_point_varying_color_varying_outline_aa_frag_glsl[];
132 extern char datatoc_gpu_shader_point_varying_color_frag_glsl[];
133 extern char datatoc_gpu_shader_3D_point_fixed_size_varying_color_vert_glsl[];
134 extern char datatoc_gpu_shader_3D_point_varying_size_vert_glsl[];
135 extern char datatoc_gpu_shader_3D_point_varying_size_varying_color_vert_glsl[];
136 extern char datatoc_gpu_shader_3D_point_uniform_size_aa_vert_glsl[];
137 extern char datatoc_gpu_shader_3D_point_uniform_size_outline_aa_vert_glsl[];
138 extern char datatoc_gpu_shader_2D_point_varying_size_varying_color_vert_glsl[];
139 extern char datatoc_gpu_shader_2D_point_uniform_size_aa_vert_glsl[];
140 extern char datatoc_gpu_shader_2D_point_uniform_size_outline_aa_vert_glsl[];
141 extern char datatoc_gpu_shader_2D_point_uniform_size_varying_color_outline_aa_vert_glsl[];
142
143 extern char datatoc_gpu_shader_2D_edituvs_points_vert_glsl[];
144 extern char datatoc_gpu_shader_2D_edituvs_facedots_vert_glsl[];
145 extern char datatoc_gpu_shader_2D_edituvs_edges_vert_glsl[];
146 extern char datatoc_gpu_shader_2D_edituvs_faces_vert_glsl[];
147 extern char datatoc_gpu_shader_2D_edituvs_stretch_vert_glsl[];
148
149 extern char datatoc_gpu_shader_2D_line_dashed_uniform_color_vert_glsl[];
150 extern char datatoc_gpu_shader_2D_line_dashed_frag_glsl[];
151 extern char datatoc_gpu_shader_2D_line_dashed_geom_glsl[];
152 extern char datatoc_gpu_shader_3D_line_dashed_uniform_color_legacy_vert_glsl[];
153 extern char datatoc_gpu_shader_3D_line_dashed_uniform_color_vert_glsl[];
154
155 extern char datatoc_gpu_shader_edges_front_back_persp_vert_glsl[];
156 extern char datatoc_gpu_shader_edges_front_back_persp_geom_glsl[];
157 extern char datatoc_gpu_shader_edges_front_back_persp_legacy_vert_glsl[];
158 extern char datatoc_gpu_shader_edges_front_back_ortho_vert_glsl[];
159 extern char datatoc_gpu_shader_edges_overlay_vert_glsl[];
160 extern char datatoc_gpu_shader_edges_overlay_geom_glsl[];
161 extern char datatoc_gpu_shader_edges_overlay_simple_geom_glsl[];
162 extern char datatoc_gpu_shader_edges_overlay_frag_glsl[];
163 extern char datatoc_gpu_shader_text_vert_glsl[];
164 extern char datatoc_gpu_shader_text_geom_glsl[];
165 extern char datatoc_gpu_shader_text_frag_glsl[];
166 extern char datatoc_gpu_shader_text_simple_vert_glsl[];
167 extern char datatoc_gpu_shader_text_simple_geom_glsl[];
168 extern char datatoc_gpu_shader_keyframe_diamond_vert_glsl[];
169 extern char datatoc_gpu_shader_keyframe_diamond_frag_glsl[];
170
171 extern char datatoc_gpu_shader_gpencil_stroke_vert_glsl[];
172 extern char datatoc_gpu_shader_gpencil_stroke_frag_glsl[];
173 extern char datatoc_gpu_shader_gpencil_stroke_geom_glsl[];
174
175 extern char datatoc_gpu_shader_gpencil_fill_vert_glsl[];
176 extern char datatoc_gpu_shader_gpencil_fill_frag_glsl[];
177
178 /* cache of built-in shaders (each is created on first use) */
179 static GPUShader *builtin_shaders[GPU_NUM_BUILTIN_SHADERS] = { NULL };
180
181 #ifndef NDEBUG
182 static uint g_shaderid = 0;
183 #endif
184
185 typedef struct {
186         const char *vert;
187         const char *frag;
188         const char *geom; /* geometry stage runs between vert & frag, but is less common, so it goes last */
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 GPUShaderTFBType 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[5];
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++] = 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++] = standard_extensions;
420                 source[num_source++] = standard_defines;
421
422                 if (defines) source[num_source++] = defines;
423                 if (libcode) source[num_source++] = libcode;
424                 source[num_source++] = fragcode;
425
426                 gpu_dump_shaders(source, num_source, DEBUG_SHADER_FRAGMENT);
427
428                 glAttachShader(shader->program, shader->fragment);
429                 glShaderSource(shader->fragment, num_source, source, NULL);
430
431                 glCompileShader(shader->fragment);
432                 glGetShaderiv(shader->fragment, GL_COMPILE_STATUS, &status);
433
434                 if (!status) {
435                         glGetShaderInfoLog(shader->fragment, sizeof(log), &length, log);
436                         shader_print_errors("compile", log, source, num_source);
437
438                         GPU_shader_free(shader);
439                         return NULL;
440                 }
441         }
442
443         if (geocode) {
444                 const char *source[6];
445                 int num_source = 0;
446
447                 source[num_source++] = gpu_shader_version();
448                 source[num_source++] = standard_extensions;
449                 source[num_source++] = standard_defines;
450
451                 if (defines) source[num_source++] = defines;
452                 source[num_source++] = geocode;
453
454                 gpu_dump_shaders(source, num_source, DEBUG_SHADER_GEOMETRY);
455
456                 glAttachShader(shader->program, shader->geometry);
457                 glShaderSource(shader->geometry, num_source, source, NULL);
458
459                 glCompileShader(shader->geometry);
460                 glGetShaderiv(shader->geometry, GL_COMPILE_STATUS, &status);
461
462                 if (!status) {
463                         glGetShaderInfoLog(shader->geometry, sizeof(log), &length, log);
464                         shader_print_errors("compile", log, source, num_source);
465
466                         GPU_shader_free(shader);
467                         return NULL;
468                 }
469         }
470
471         if (tf_names != NULL) {
472                 glTransformFeedbackVaryings(shader->program, tf_count, tf_names, GL_INTERLEAVED_ATTRIBS);
473                 /* Primitive type must be setup */
474                 BLI_assert(tf_type != GPU_SHADER_TFB_NONE);
475                 shader->feedback_transform_type = tf_type;
476         }
477
478         glLinkProgram(shader->program);
479         glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
480         if (!status) {
481                 glGetProgramInfoLog(shader->program, sizeof(log), &length, log);
482                 /* print attached shaders in pipeline order */
483                 if (vertexcode) shader_print_errors("linking", log, &vertexcode, 1);
484                 if (geocode) shader_print_errors("linking", log, &geocode, 1);
485                 if (libcode) shader_print_errors("linking", log, &libcode, 1);
486                 if (fragcode) shader_print_errors("linking", log, &fragcode, 1);
487
488                 GPU_shader_free(shader);
489                 return NULL;
490         }
491
492         shader->interface = GPU_shaderinterface_create(shader->program);
493
494         return shader;
495 }
496
497 #undef DEBUG_SHADER_GEOMETRY
498 #undef DEBUG_SHADER_FRAGMENT
499 #undef DEBUG_SHADER_VERTEX
500 #undef DEBUG_SHADER_NONE
501
502 void GPU_shader_bind(GPUShader *shader)
503 {
504         BLI_assert(shader && shader->program);
505
506         glUseProgram(shader->program);
507         GPU_matrix_bind(shader->interface);
508 }
509
510 void GPU_shader_unbind(void)
511 {
512         glUseProgram(0);
513 }
514
515 bool GPU_shader_transform_feedback_enable(GPUShader *shader, uint vbo_id)
516 {
517         if (shader->feedback_transform_type == GPU_SHADER_TFB_NONE) {
518                 return false;
519         }
520
521         glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, vbo_id);
522
523         switch (shader->feedback_transform_type) {
524                 case GPU_SHADER_TFB_POINTS:    glBeginTransformFeedback(GL_POINTS);    return true;
525                 case GPU_SHADER_TFB_LINES:     glBeginTransformFeedback(GL_LINES);     return true;
526                 case GPU_SHADER_TFB_TRIANGLES: glBeginTransformFeedback(GL_TRIANGLES); return true;
527                 default: return false;
528         }
529 }
530
531 void GPU_shader_transform_feedback_disable(GPUShader *UNUSED(shader))
532 {
533         glEndTransformFeedback();
534 }
535
536 void GPU_shader_free(GPUShader *shader)
537 {
538 #if 0 /* Would be nice to have, but for now the Deferred compilation
539        * does not have a GPUContext. */
540         BLI_assert(GPU_context_active_get() != NULL);
541 #endif
542         BLI_assert(shader);
543
544         if (shader->vertex)
545                 glDeleteShader(shader->vertex);
546         if (shader->geometry)
547                 glDeleteShader(shader->geometry);
548         if (shader->fragment)
549                 glDeleteShader(shader->fragment);
550         if (shader->program)
551                 glDeleteProgram(shader->program);
552
553         if (shader->interface)
554                 GPU_shaderinterface_discard(shader->interface);
555
556         MEM_freeN(shader);
557 }
558
559 int GPU_shader_get_uniform(GPUShader *shader, const char *name)
560 {
561         BLI_assert(shader && shader->program);
562         const GPUShaderInput *uniform = GPU_shaderinterface_uniform(shader->interface, name);
563         return uniform ? uniform->location : -1;
564 }
565
566 int GPU_shader_get_builtin_uniform(GPUShader *shader, int builtin)
567 {
568         BLI_assert(shader && shader->program);
569         const GPUShaderInput *uniform = GPU_shaderinterface_uniform_builtin(shader->interface, builtin);
570         return uniform ? uniform->location : -1;
571 }
572
573 int GPU_shader_get_uniform_block(GPUShader *shader, const char *name)
574 {
575         BLI_assert(shader && shader->program);
576
577         const GPUShaderInput *ubo = GPU_shaderinterface_ubo(shader->interface, name);
578         return ubo ? ubo->location : -1;
579 }
580
581 void *GPU_shader_get_interface(GPUShader *shader)
582 {
583         return shader->interface;
584 }
585
586 /* Clement : Temp */
587 int GPU_shader_get_program(GPUShader *shader)
588 {
589         return (int)shader->program;
590 }
591
592 void GPU_shader_uniform_float(GPUShader *UNUSED(shader), int location, float value)
593 {
594         if (location == -1)
595                 return;
596
597         glUniform1f(location, value);
598 }
599
600 void GPU_shader_uniform_vector(GPUShader *UNUSED(shader), int location, int length, int arraysize, const float *value)
601 {
602         if (location == -1 || value == NULL)
603                 return;
604
605         if (length == 1) glUniform1fv(location, arraysize, value);
606         else if (length == 2) glUniform2fv(location, arraysize, value);
607         else if (length == 3) glUniform3fv(location, arraysize, value);
608         else if (length == 4) glUniform4fv(location, arraysize, value);
609         else if (length == 9) glUniformMatrix3fv(location, arraysize, 0, value);
610         else if (length == 16) glUniformMatrix4fv(location, arraysize, 0, value);
611 }
612
613 void GPU_shader_uniform_vector_int(GPUShader *UNUSED(shader), int location, int length, int arraysize, const int *value)
614 {
615         if (location == -1)
616                 return;
617
618         if (length == 1) glUniform1iv(location, arraysize, value);
619         else if (length == 2) glUniform2iv(location, arraysize, value);
620         else if (length == 3) glUniform3iv(location, arraysize, value);
621         else if (length == 4) glUniform4iv(location, arraysize, value);
622 }
623
624 void GPU_shader_uniform_int(GPUShader *UNUSED(shader), int location, int value)
625 {
626         if (location == -1)
627                 return;
628
629         glUniform1i(location, value);
630 }
631
632 void GPU_shader_uniform_buffer(GPUShader *shader, int location, GPUUniformBuffer *ubo)
633 {
634         int bindpoint = GPU_uniformbuffer_bindpoint(ubo);
635
636         if (location == -1) {
637                 return;
638         }
639
640         glUniformBlockBinding(shader->program, location, bindpoint);
641 }
642
643 void GPU_shader_uniform_texture(GPUShader *UNUSED(shader), int location, GPUTexture *tex)
644 {
645         int number = GPU_texture_bound_number(tex);
646
647         if (number == -1) {
648                 fprintf(stderr, "Texture is not bound.\n");
649                 BLI_assert(0);
650                 return;
651         }
652
653         if (location == -1)
654                 return;
655
656         glUniform1i(location, number);
657 }
658
659 int GPU_shader_get_attribute(GPUShader *shader, const char *name)
660 {
661         BLI_assert(shader && shader->program);
662         const GPUShaderInput *attrib = GPU_shaderinterface_attr(shader->interface, name);
663         return attrib ? attrib->location : -1;
664 }
665
666 static const GPUShaderStages builtin_shader_stages[GPU_NUM_BUILTIN_SHADERS] = {
667         [GPU_SHADER_TEXT] =
668                 { datatoc_gpu_shader_text_vert_glsl,
669                   datatoc_gpu_shader_text_frag_glsl,
670                   datatoc_gpu_shader_text_geom_glsl },
671         [GPU_SHADER_TEXT_SIMPLE] =
672                 { datatoc_gpu_shader_text_simple_vert_glsl,
673                   datatoc_gpu_shader_text_frag_glsl,
674                   datatoc_gpu_shader_text_simple_geom_glsl },
675         [GPU_SHADER_KEYFRAME_DIAMOND] =
676                 { datatoc_gpu_shader_keyframe_diamond_vert_glsl,
677                   datatoc_gpu_shader_keyframe_diamond_frag_glsl },
678         [GPU_SHADER_EDGES_FRONT_BACK_PERSP] =
679                 { datatoc_gpu_shader_edges_front_back_persp_vert_glsl,
680                   datatoc_gpu_shader_flat_color_frag_glsl,               /*  this version is     */
681                   datatoc_gpu_shader_edges_front_back_persp_geom_glsl }, /*  magical but slooow  */
682         [GPU_SHADER_EDGES_FRONT_BACK_ORTHO] =
683                 { datatoc_gpu_shader_edges_front_back_ortho_vert_glsl,
684                  datatoc_gpu_shader_flat_color_frag_glsl },
685         [GPU_SHADER_EDGES_OVERLAY_SIMPLE] =
686                 { datatoc_gpu_shader_3D_vert_glsl,
687                   datatoc_gpu_shader_edges_overlay_frag_glsl,
688                   datatoc_gpu_shader_edges_overlay_simple_geom_glsl },
689         [GPU_SHADER_EDGES_OVERLAY] =
690                 { datatoc_gpu_shader_edges_overlay_vert_glsl,
691                   datatoc_gpu_shader_edges_overlay_frag_glsl,
692                   datatoc_gpu_shader_edges_overlay_geom_glsl },
693         [GPU_SHADER_SIMPLE_LIGHTING] =
694                 { datatoc_gpu_shader_3D_normal_vert_glsl,
695                   datatoc_gpu_shader_simple_lighting_frag_glsl },
696         /* Use 'USE_FLAT_NORMAL' to make flat shader from smooth  */
697         [GPU_SHADER_SIMPLE_LIGHTING_FLAT_COLOR] =
698                 { datatoc_gpu_shader_3D_normal_smooth_color_vert_glsl,
699                   datatoc_gpu_shader_simple_lighting_smooth_color_frag_glsl },
700         [GPU_SHADER_SIMPLE_LIGHTING_SMOOTH_COLOR] =
701                 { datatoc_gpu_shader_3D_normal_smooth_color_vert_glsl,
702                   datatoc_gpu_shader_simple_lighting_smooth_color_frag_glsl },
703         [GPU_SHADER_SIMPLE_LIGHTING_SMOOTH_COLOR_ALPHA] =
704                 { datatoc_gpu_shader_3D_normal_smooth_color_vert_glsl,
705                   datatoc_gpu_shader_simple_lighting_smooth_color_alpha_frag_glsl },
706
707         [GPU_SHADER_2D_IMAGE_MASK_UNIFORM_COLOR] =
708                 { datatoc_gpu_shader_3D_image_vert_glsl,
709                   datatoc_gpu_shader_image_mask_uniform_color_frag_glsl },
710         [GPU_SHADER_3D_IMAGE_MODULATE_ALPHA] =
711                 { datatoc_gpu_shader_3D_image_vert_glsl,
712                   datatoc_gpu_shader_image_modulate_alpha_frag_glsl },
713         [GPU_SHADER_3D_IMAGE_DEPTH] =
714                 { datatoc_gpu_shader_3D_image_vert_glsl,
715                   datatoc_gpu_shader_image_depth_linear_frag_glsl },
716         [GPU_SHADER_3D_IMAGE_DEPTH_COPY] =
717                 { datatoc_gpu_shader_3D_image_vert_glsl,
718                   datatoc_gpu_shader_image_depth_copy_frag_glsl },
719         [GPU_SHADER_2D_IMAGE_MULTISAMPLE_2] =
720                 { datatoc_gpu_shader_2D_vert_glsl,
721                   datatoc_gpu_shader_image_multisample_resolve_frag_glsl },
722         [GPU_SHADER_2D_IMAGE_MULTISAMPLE_4] =
723                 { datatoc_gpu_shader_2D_vert_glsl,
724                   datatoc_gpu_shader_image_multisample_resolve_frag_glsl },
725         [GPU_SHADER_2D_IMAGE_MULTISAMPLE_8] =
726                 { datatoc_gpu_shader_2D_vert_glsl,
727                   datatoc_gpu_shader_image_multisample_resolve_frag_glsl },
728         [GPU_SHADER_2D_IMAGE_MULTISAMPLE_16] =
729                 { datatoc_gpu_shader_2D_vert_glsl,
730                   datatoc_gpu_shader_image_multisample_resolve_frag_glsl },
731         [GPU_SHADER_2D_IMAGE_MULTISAMPLE_2_DEPTH_TEST] =
732                 { datatoc_gpu_shader_2D_vert_glsl,
733                   datatoc_gpu_shader_image_multisample_resolve_frag_glsl },
734         [GPU_SHADER_2D_IMAGE_MULTISAMPLE_4_DEPTH_TEST] =
735                 { datatoc_gpu_shader_2D_vert_glsl,
736                   datatoc_gpu_shader_image_multisample_resolve_frag_glsl },
737         [GPU_SHADER_2D_IMAGE_MULTISAMPLE_8_DEPTH_TEST] =
738                 { datatoc_gpu_shader_2D_vert_glsl,
739                   datatoc_gpu_shader_image_multisample_resolve_frag_glsl },
740         [GPU_SHADER_2D_IMAGE_MULTISAMPLE_16_DEPTH_TEST] =
741                 { datatoc_gpu_shader_2D_vert_glsl,
742                   datatoc_gpu_shader_image_multisample_resolve_frag_glsl },
743
744         [GPU_SHADER_2D_IMAGE_INTERLACE] =
745                 { datatoc_gpu_shader_2D_image_vert_glsl,
746                   datatoc_gpu_shader_image_interlace_frag_glsl },
747         [GPU_SHADER_2D_CHECKER] =
748                 { datatoc_gpu_shader_2D_vert_glsl,
749                   datatoc_gpu_shader_checker_frag_glsl },
750
751         [GPU_SHADER_2D_DIAG_STRIPES] =
752                 { datatoc_gpu_shader_2D_vert_glsl,
753                   datatoc_gpu_shader_diag_stripes_frag_glsl },
754
755         [GPU_SHADER_2D_UNIFORM_COLOR] =
756                 { datatoc_gpu_shader_2D_vert_glsl,
757                   datatoc_gpu_shader_uniform_color_frag_glsl },
758         [GPU_SHADER_2D_FLAT_COLOR] =
759                 { datatoc_gpu_shader_2D_flat_color_vert_glsl,
760                   datatoc_gpu_shader_flat_color_frag_glsl },
761         [GPU_SHADER_2D_SMOOTH_COLOR] =
762                 { datatoc_gpu_shader_2D_smooth_color_vert_glsl,
763                   datatoc_gpu_shader_2D_smooth_color_frag_glsl },
764         [GPU_SHADER_2D_SMOOTH_COLOR_DITHER] =
765                 { datatoc_gpu_shader_2D_smooth_color_vert_glsl,
766                   datatoc_gpu_shader_2D_smooth_color_dithered_frag_glsl },
767         [GPU_SHADER_2D_IMAGE_LINEAR_TO_SRGB] =
768                 { datatoc_gpu_shader_2D_image_vert_glsl,
769                   datatoc_gpu_shader_image_linear_frag_glsl },
770         [GPU_SHADER_2D_IMAGE] =
771                 { datatoc_gpu_shader_2D_image_vert_glsl,
772                   datatoc_gpu_shader_image_frag_glsl },
773         [GPU_SHADER_2D_IMAGE_COLOR] =
774                 { datatoc_gpu_shader_2D_image_vert_glsl,
775                   datatoc_gpu_shader_image_color_frag_glsl },
776         [GPU_SHADER_2D_IMAGE_DESATURATE_COLOR] =
777                 { datatoc_gpu_shader_2D_image_vert_glsl,
778                   datatoc_gpu_shader_image_desaturate_frag_glsl },
779         [GPU_SHADER_2D_IMAGE_ALPHA_COLOR] =
780                 { datatoc_gpu_shader_2D_image_vert_glsl,
781                   datatoc_gpu_shader_image_alpha_color_frag_glsl },
782         [GPU_SHADER_2D_IMAGE_SHUFFLE_COLOR] =
783                 { datatoc_gpu_shader_2D_image_vert_glsl,
784                   datatoc_gpu_shader_image_shuffle_color_frag_glsl },
785         [GPU_SHADER_2D_IMAGE_RECT_COLOR] =
786                 { datatoc_gpu_shader_2D_image_rect_vert_glsl,
787                   datatoc_gpu_shader_image_color_frag_glsl },
788         [GPU_SHADER_2D_IMAGE_MULTI_RECT_COLOR] =
789                 { datatoc_gpu_shader_2D_image_multi_rect_vert_glsl,
790                   datatoc_gpu_shader_image_varying_color_frag_glsl },
791
792         [GPU_SHADER_3D_UNIFORM_COLOR] =
793                 { datatoc_gpu_shader_3D_vert_glsl,
794                   datatoc_gpu_shader_uniform_color_frag_glsl },
795         [GPU_SHADER_3D_UNIFORM_COLOR_U32] =
796                 { datatoc_gpu_shader_3D_vert_glsl,
797                   datatoc_gpu_shader_uniform_color_frag_glsl },
798         [GPU_SHADER_3D_FLAT_COLOR] =
799                 { datatoc_gpu_shader_3D_flat_color_vert_glsl,
800                   datatoc_gpu_shader_flat_color_frag_glsl },
801         [GPU_SHADER_3D_FLAT_COLOR_U32] =
802                 { datatoc_gpu_shader_3D_flat_color_vert_glsl,
803                   datatoc_gpu_shader_flat_color_frag_glsl },
804         [GPU_SHADER_3D_SMOOTH_COLOR] =
805                 { datatoc_gpu_shader_3D_smooth_color_vert_glsl,
806                   datatoc_gpu_shader_3D_smooth_color_frag_glsl },
807         [GPU_SHADER_3D_DEPTH_ONLY] =
808                 { datatoc_gpu_shader_3D_vert_glsl,
809                   datatoc_gpu_shader_depth_only_frag_glsl },
810         [GPU_SHADER_3D_CLIPPED_UNIFORM_COLOR] =
811                 { datatoc_gpu_shader_3D_clipped_uniform_color_vert_glsl,
812                   datatoc_gpu_shader_uniform_color_frag_glsl },
813
814         [GPU_SHADER_3D_GROUNDPOINT] =
815                 { datatoc_gpu_shader_3D_groundpoint_vert_glsl,
816                   datatoc_gpu_shader_point_uniform_color_frag_glsl },
817         [GPU_SHADER_3D_GROUNDLINE] =
818                 { datatoc_gpu_shader_3D_passthrough_vert_glsl,
819                   datatoc_gpu_shader_uniform_color_frag_glsl,
820                   datatoc_gpu_shader_3D_groundline_geom_glsl },
821
822         [GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR] =
823                 { datatoc_gpu_shader_2D_line_dashed_uniform_color_vert_glsl,
824                   datatoc_gpu_shader_2D_line_dashed_frag_glsl,
825                   datatoc_gpu_shader_2D_line_dashed_geom_glsl },
826         [GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR] =
827                 { datatoc_gpu_shader_3D_line_dashed_uniform_color_vert_glsl,
828                   datatoc_gpu_shader_2D_line_dashed_frag_glsl,
829                   datatoc_gpu_shader_2D_line_dashed_geom_glsl },
830
831         [GPU_SHADER_3D_OBJECTSPACE_SIMPLE_LIGHTING_VARIYING_COLOR] =
832                 { datatoc_gpu_shader_instance_objectspace_variying_color_vert_glsl,
833                   datatoc_gpu_shader_simple_lighting_frag_glsl},
834         [GPU_SHADER_3D_OBJECTSPACE_VARIYING_COLOR] =
835                 { datatoc_gpu_shader_instance_objectspace_variying_color_vert_glsl,
836                   datatoc_gpu_shader_flat_color_frag_glsl},
837         [GPU_SHADER_3D_SCREENSPACE_VARIYING_COLOR] =
838                 { datatoc_gpu_shader_instance_screenspace_variying_color_vert_glsl,
839                   datatoc_gpu_shader_flat_color_frag_glsl},
840         [GPU_SHADER_3D_INSTANCE_SCREEN_ALIGNED_AXIS] =
841                 { datatoc_gpu_shader_instance_screen_aligned_vert_glsl,
842                   datatoc_gpu_shader_flat_color_frag_glsl},
843         [GPU_SHADER_3D_INSTANCE_SCREEN_ALIGNED] =
844                 { datatoc_gpu_shader_instance_screen_aligned_vert_glsl,
845                   datatoc_gpu_shader_flat_color_frag_glsl},
846
847         [GPU_SHADER_CAMERA] =
848                 { datatoc_gpu_shader_instance_camera_vert_glsl,
849                   datatoc_gpu_shader_flat_color_frag_glsl},
850         [GPU_SHADER_DISTANCE_LINES] =
851                 { datatoc_gpu_shader_instance_distance_line_vert_glsl,
852                   datatoc_gpu_shader_flat_color_frag_glsl},
853
854         [GPU_SHADER_2D_POINT_FIXED_SIZE_UNIFORM_COLOR] =
855                 { datatoc_gpu_shader_2D_vert_glsl,
856                   datatoc_gpu_shader_point_uniform_color_frag_glsl },
857         [GPU_SHADER_2D_POINT_VARYING_SIZE_VARYING_COLOR] =
858                 { datatoc_gpu_shader_2D_point_varying_size_varying_color_vert_glsl,
859                   datatoc_gpu_shader_point_varying_color_frag_glsl },
860         [GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA] =
861                 { datatoc_gpu_shader_2D_point_uniform_size_aa_vert_glsl,
862                   datatoc_gpu_shader_point_uniform_color_aa_frag_glsl },
863         [GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA] =
864                 { datatoc_gpu_shader_2D_point_uniform_size_outline_aa_vert_glsl,
865                   datatoc_gpu_shader_point_uniform_color_outline_aa_frag_glsl },
866         [GPU_SHADER_2D_POINT_UNIFORM_SIZE_VARYING_COLOR_OUTLINE_AA] =
867                 { datatoc_gpu_shader_2D_point_uniform_size_varying_color_outline_aa_vert_glsl,
868                   datatoc_gpu_shader_point_varying_color_outline_aa_frag_glsl },
869         [GPU_SHADER_3D_POINT_FIXED_SIZE_UNIFORM_COLOR] =
870                 { datatoc_gpu_shader_3D_vert_glsl,
871                   datatoc_gpu_shader_point_uniform_color_frag_glsl },
872         [GPU_SHADER_3D_POINT_FIXED_SIZE_VARYING_COLOR] =
873                 { datatoc_gpu_shader_3D_point_fixed_size_varying_color_vert_glsl,
874                   datatoc_gpu_shader_point_varying_color_frag_glsl },
875         [GPU_SHADER_3D_POINT_VARYING_SIZE_UNIFORM_COLOR] =
876                 { datatoc_gpu_shader_3D_point_varying_size_vert_glsl,
877                   datatoc_gpu_shader_point_uniform_color_frag_glsl },
878         [GPU_SHADER_3D_POINT_VARYING_SIZE_VARYING_COLOR] =
879                 { datatoc_gpu_shader_3D_point_varying_size_varying_color_vert_glsl,
880                   datatoc_gpu_shader_point_varying_color_frag_glsl },
881         [GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA] =
882                 { datatoc_gpu_shader_3D_point_uniform_size_aa_vert_glsl,
883                   datatoc_gpu_shader_point_uniform_color_aa_frag_glsl },
884         [GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA] =
885                 { datatoc_gpu_shader_3D_point_uniform_size_outline_aa_vert_glsl,
886                   datatoc_gpu_shader_point_uniform_color_outline_aa_frag_glsl },
887
888         [GPU_SHADER_INSTANCE_UNIFORM_COLOR] =
889                 { datatoc_gpu_shader_instance_vert_glsl,
890                   datatoc_gpu_shader_uniform_color_frag_glsl },
891         [GPU_SHADER_INSTANCE_VARIYING_ID_VARIYING_SIZE] =
892                 { datatoc_gpu_shader_instance_variying_size_variying_id_vert_glsl,
893                   datatoc_gpu_shader_flat_id_frag_glsl },
894         [GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SIZE] =
895                 { datatoc_gpu_shader_instance_variying_size_variying_color_vert_glsl,
896                   datatoc_gpu_shader_flat_color_frag_glsl },
897         [GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SCALE] =
898                 { datatoc_gpu_shader_instance_variying_size_variying_color_vert_glsl,
899                   datatoc_gpu_shader_flat_color_frag_glsl },
900         [GPU_SHADER_INSTANCE_EDGES_VARIYING_COLOR] =
901                 { datatoc_gpu_shader_instance_edges_variying_color_vert_glsl,
902                   datatoc_gpu_shader_flat_color_frag_glsl,
903                   datatoc_gpu_shader_instance_edges_variying_color_geom_glsl},
904
905         [GPU_SHADER_2D_AREA_EDGES] =
906                 { datatoc_gpu_shader_2D_area_borders_vert_glsl,
907                   datatoc_gpu_shader_2D_area_borders_frag_glsl},
908         [GPU_SHADER_2D_WIDGET_BASE] =
909                 { datatoc_gpu_shader_2D_widget_base_vert_glsl,
910                   datatoc_gpu_shader_2D_widget_base_frag_glsl},
911         [GPU_SHADER_2D_WIDGET_BASE_INST] =
912                 { datatoc_gpu_shader_2D_widget_base_vert_glsl,
913                   datatoc_gpu_shader_2D_widget_base_frag_glsl},
914         [GPU_SHADER_2D_WIDGET_SHADOW] =
915                 { datatoc_gpu_shader_2D_widget_shadow_vert_glsl,
916                   datatoc_gpu_shader_2D_widget_shadow_frag_glsl },
917         [GPU_SHADER_2D_NODELINK] =
918                 { datatoc_gpu_shader_2D_nodelink_vert_glsl,
919                   datatoc_gpu_shader_2D_nodelink_frag_glsl },
920         [GPU_SHADER_2D_NODELINK_INST] =
921                 { datatoc_gpu_shader_2D_nodelink_vert_glsl,
922                   datatoc_gpu_shader_2D_nodelink_frag_glsl },
923
924         [GPU_SHADER_2D_UV_VERTS] =
925                 { datatoc_gpu_shader_2D_edituvs_points_vert_glsl,
926                   datatoc_gpu_shader_point_varying_color_varying_outline_aa_frag_glsl },
927         [GPU_SHADER_2D_UV_FACEDOTS] =
928                 { datatoc_gpu_shader_2D_edituvs_facedots_vert_glsl,
929                   datatoc_gpu_shader_point_varying_color_frag_glsl },
930         [GPU_SHADER_2D_UV_EDGES] =
931                 { datatoc_gpu_shader_2D_edituvs_edges_vert_glsl,
932                   datatoc_gpu_shader_flat_color_frag_glsl },
933         [GPU_SHADER_2D_UV_EDGES_SMOOTH] =
934                 { datatoc_gpu_shader_2D_edituvs_edges_vert_glsl,
935                   datatoc_gpu_shader_2D_smooth_color_frag_glsl },
936         [GPU_SHADER_2D_UV_FACES] =
937                 { datatoc_gpu_shader_2D_edituvs_faces_vert_glsl,
938                   datatoc_gpu_shader_flat_color_frag_glsl },
939         [GPU_SHADER_2D_UV_FACES_STRETCH] =
940                 { datatoc_gpu_shader_2D_edituvs_stretch_vert_glsl,
941                   datatoc_gpu_shader_2D_smooth_color_frag_glsl },
942
943         [GPU_SHADER_GPENCIL_STROKE] =
944                 { datatoc_gpu_shader_gpencil_stroke_vert_glsl,
945                   datatoc_gpu_shader_gpencil_stroke_frag_glsl,
946                   datatoc_gpu_shader_gpencil_stroke_geom_glsl },
947
948         [GPU_SHADER_GPENCIL_FILL] =
949                 { datatoc_gpu_shader_gpencil_fill_vert_glsl,
950                   datatoc_gpu_shader_gpencil_fill_frag_glsl },
951 };
952
953 /* just a few special cases */
954 static const char *gpu_shader_get_builtin_shader_defines(
955         GPUBuiltinShader shader)
956 {
957         switch (shader) {
958                 case GPU_SHADER_2D_IMAGE_MULTISAMPLE_2:
959                         return "#define SAMPLES 2\n";
960
961                 case GPU_SHADER_2D_IMAGE_MULTISAMPLE_2_DEPTH_TEST:
962                         return "#define SAMPLES 2\n"
963                                "#define USE_DEPTH\n";
964
965                 case GPU_SHADER_2D_IMAGE_MULTISAMPLE_4:
966                         return "#define SAMPLES 4\n";
967
968                 case GPU_SHADER_2D_IMAGE_MULTISAMPLE_4_DEPTH_TEST:
969                         return "#define SAMPLES 4\n"
970                                "#define USE_DEPTH\n";
971
972                 case GPU_SHADER_2D_IMAGE_MULTISAMPLE_8:
973                         return "#define SAMPLES 8\n";
974
975                 case GPU_SHADER_2D_IMAGE_MULTISAMPLE_8_DEPTH_TEST:
976                         return "#define SAMPLES 8\n"
977                                "#define USE_DEPTH\n";
978
979                 case GPU_SHADER_2D_IMAGE_MULTISAMPLE_16:
980                         return "#define SAMPLES 16\n";
981
982                 case GPU_SHADER_2D_IMAGE_MULTISAMPLE_16_DEPTH_TEST:
983                         return "#define SAMPLES 16\n"
984                                "#define USE_DEPTH\n";
985
986                 case GPU_SHADER_2D_WIDGET_BASE_INST:
987                 case GPU_SHADER_2D_NODELINK_INST:
988                         return "#define USE_INSTANCE\n";
989
990                 case GPU_SHADER_INSTANCE_VARIYING_ID_VARIYING_SIZE:
991                 case GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SIZE:
992                         return "#define UNIFORM_SCALE\n";
993
994                 case GPU_SHADER_3D_INSTANCE_SCREEN_ALIGNED_AXIS:
995                         return "#define AXIS_NAME\n";
996
997                 case GPU_SHADER_3D_OBJECTSPACE_SIMPLE_LIGHTING_VARIYING_COLOR:
998                         return "#define USE_INSTANCE_COLOR\n";
999
1000                 case GPU_SHADER_3D_FLAT_COLOR_U32:
1001                 case GPU_SHADER_3D_UNIFORM_COLOR_U32:
1002                         return "#define USE_COLOR_U32\n";
1003
1004                 case GPU_SHADER_SIMPLE_LIGHTING_FLAT_COLOR:
1005                         return "#define USE_FLAT_NORMAL\n";
1006
1007                 case GPU_SHADER_2D_UV_EDGES_SMOOTH:
1008                         return "#define SMOOTH_COLOR\n";
1009
1010                 default:
1011                         return NULL;
1012         }
1013 }
1014
1015 GPUShader *GPU_shader_get_builtin_shader(GPUBuiltinShader shader)
1016 {
1017         BLI_assert(shader != GPU_NUM_BUILTIN_SHADERS); /* don't be a troll */
1018
1019         if (builtin_shaders[shader] == NULL) {
1020                 const char *defines = gpu_shader_get_builtin_shader_defines(shader);
1021
1022                 const GPUShaderStages *stages = builtin_shader_stages + shader;
1023
1024                 if (shader == GPU_SHADER_EDGES_FRONT_BACK_PERSP && !GLEW_VERSION_3_2) {
1025                         /* TODO: remove after switch to core profile (maybe) */
1026                         static const GPUShaderStages legacy_fancy_edges = {
1027                                 datatoc_gpu_shader_edges_front_back_persp_legacy_vert_glsl,
1028                                 datatoc_gpu_shader_flat_color_alpha_test_0_frag_glsl,
1029                         };
1030                         stages = &legacy_fancy_edges;
1031                 }
1032
1033                 if (shader == GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR && !GLEW_VERSION_3_2) {
1034                         /* Dashed need geometry shader, which are not supported by legacy OpenGL, fallback to solid lines. */
1035                         /* TODO: remove after switch to core profile (maybe) */
1036                         static const GPUShaderStages legacy_dashed_lines = {
1037                                 datatoc_gpu_shader_3D_line_dashed_uniform_color_legacy_vert_glsl,
1038                                 datatoc_gpu_shader_2D_line_dashed_frag_glsl,
1039                         };
1040                         stages = &legacy_dashed_lines;
1041                 }
1042
1043                 /* common case */
1044                 builtin_shaders[shader] = GPU_shader_create(stages->vert, stages->frag, stages->geom, NULL, defines, __func__);
1045         }
1046
1047         return builtin_shaders[shader];
1048 }
1049
1050 void GPU_shader_get_builtin_shader_code(
1051         GPUBuiltinShader shader,
1052         const char **r_vert, const char **r_frag,
1053         const char **r_geom, const char **r_defines)
1054 {
1055         const GPUShaderStages *stages = builtin_shader_stages + shader;
1056         *r_vert = stages->vert;
1057         *r_frag = stages->frag;
1058         *r_geom = stages->geom;
1059         *r_defines = gpu_shader_get_builtin_shader_defines(shader);
1060 }
1061
1062 #define MAX_DEFINES 100
1063
1064 void GPU_shader_free_builtin_shaders(void)
1065 {
1066         for (int i = 0; i < GPU_NUM_BUILTIN_SHADERS; ++i) {
1067                 if (builtin_shaders[i]) {
1068                         GPU_shader_free(builtin_shaders[i]);
1069                         builtin_shaders[i] = NULL;
1070                 }
1071         }
1072 }