9e494cdad88c2f2795be1ac2ae9320956050ea84
[blender.git] / source / blender / draw / intern / draw_common.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  * Copyright 2016, Blender Foundation.
17  */
18
19 /** \file
20  * \ingroup draw
21  */
22
23 #include "DRW_render.h"
24
25 #include "GPU_shader.h"
26 #include "GPU_texture.h"
27
28 #include "UI_resources.h"
29
30 #include "BKE_object.h"
31 #include "BKE_global.h"
32 #include "BKE_colorband.h"
33
34 #include "BIF_glutil.h"
35
36 #include "draw_common.h"
37
38 #if 0
39 #  define UI_COLOR_RGB_FROM_U8(r, g, b, v4) \
40     ARRAY_SET_ITEMS(v4, (float)r / 255.0f, (float)g / 255.0f, (float)b / 255.0f, 1.0)
41 #endif
42 #define UI_COLOR_RGBA_FROM_U8(r, g, b, a, v4) \
43   ARRAY_SET_ITEMS(v4, (float)r / 255.0f, (float)g / 255.0f, (float)b / 255.0f, (float)a / 255.0f)
44
45 /* Colors & Constant */
46 struct DRW_Global G_draw = {{{0}}};
47
48 static bool weight_ramp_custom = false;
49 static ColorBand weight_ramp_copy;
50
51 static struct GPUTexture *DRW_create_weight_colorramp_texture(void);
52
53 void DRW_globals_update(void)
54 {
55   GlobalsUboStorage *gb = &G_draw.block;
56
57   UI_GetThemeColor4fv(TH_WIRE, gb->colorWire);
58   UI_GetThemeColor4fv(TH_WIRE_EDIT, gb->colorWireEdit);
59   UI_GetThemeColor4fv(TH_ACTIVE, gb->colorActive);
60   UI_GetThemeColor4fv(TH_SELECT, gb->colorSelect);
61   UI_COLOR_RGBA_FROM_U8(0x88, 0xFF, 0xFF, 155, gb->colorLibrarySelect);
62   UI_COLOR_RGBA_FROM_U8(0x55, 0xCC, 0xCC, 155, gb->colorLibrary);
63   UI_GetThemeColor4fv(TH_TRANSFORM, gb->colorTransform);
64   UI_GetThemeColor4fv(TH_LIGHT, gb->colorLight);
65   UI_GetThemeColor4fv(TH_SPEAKER, gb->colorSpeaker);
66   UI_GetThemeColor4fv(TH_CAMERA, gb->colorCamera);
67   UI_GetThemeColor4fv(TH_EMPTY, gb->colorEmpty);
68   UI_GetThemeColor4fv(TH_VERTEX, gb->colorVertex);
69   UI_GetThemeColor4fv(TH_VERTEX_SELECT, gb->colorVertexSelect);
70   UI_GetThemeColor4fv(TH_VERTEX_UNREFERENCED, gb->colorVertexUnreferenced);
71   UI_COLOR_RGBA_FROM_U8(0xB0, 0x00, 0xB0, 0xFF, gb->colorVertexMissingData);
72   UI_GetThemeColor4fv(TH_EDITMESH_ACTIVE, gb->colorEditMeshActive);
73   UI_GetThemeColor4fv(TH_EDGE_SELECT, gb->colorEdgeSelect);
74
75   UI_GetThemeColor4fv(TH_EDGE_SEAM, gb->colorEdgeSeam);
76   UI_GetThemeColor4fv(TH_EDGE_SHARP, gb->colorEdgeSharp);
77   UI_GetThemeColor4fv(TH_EDGE_CREASE, gb->colorEdgeCrease);
78   UI_GetThemeColor4fv(TH_EDGE_BEVEL, gb->colorEdgeBWeight);
79   UI_GetThemeColor4fv(TH_EDGE_FACESEL, gb->colorEdgeFaceSelect);
80   UI_GetThemeColor4fv(TH_FACE, gb->colorFace);
81   UI_GetThemeColor4fv(TH_FACE_SELECT, gb->colorFaceSelect);
82   UI_GetThemeColor4fv(TH_NORMAL, gb->colorNormal);
83   UI_GetThemeColor4fv(TH_VNORMAL, gb->colorVNormal);
84   UI_GetThemeColor4fv(TH_LNORMAL, gb->colorLNormal);
85   UI_GetThemeColor4fv(TH_FACE_DOT, gb->colorFaceDot);
86   UI_GetThemeColor4fv(TH_BACK, gb->colorBackground);
87
88   /* Custom median color to slightly affect the edit mesh colors. */
89   interp_v4_v4v4(gb->colorEditMeshMiddle, gb->colorVertexSelect, gb->colorWireEdit, 0.35f);
90   copy_v3_fl(
91       gb->colorEditMeshMiddle,
92       dot_v3v3(gb->colorEditMeshMiddle, (float[3]){0.3333f, 0.3333f, 0.3333f})); /* Desaturate */
93
94   interp_v4_v4v4(gb->colorDupliSelect, gb->colorBackground, gb->colorSelect, 0.5f);
95   /* Was 50% in 2.7x since the background was lighter making it easier to tell the color from
96    * black, with a darker background we need a more faded color. */
97   interp_v4_v4v4(gb->colorDupli, gb->colorBackground, gb->colorWire, 0.3f);
98
99 #ifdef WITH_FREESTYLE
100   UI_GetThemeColor4fv(TH_FREESTYLE_EDGE_MARK, gb->colorEdgeFreestyle);
101   UI_GetThemeColor4fv(TH_FREESTYLE_FACE_MARK, gb->colorFaceFreestyle);
102 #else
103   zero_v4(gb->colorEdgeFreestyle);
104   zero_v4(gb->colorFaceFreestyle);
105 #endif
106
107   /* Curve */
108   UI_GetThemeColor4fv(TH_HANDLE_FREE, gb->colorHandleFree);
109   UI_GetThemeColor4fv(TH_HANDLE_AUTO, gb->colorHandleAuto);
110   UI_GetThemeColor4fv(TH_HANDLE_VECT, gb->colorHandleVect);
111   UI_GetThemeColor4fv(TH_HANDLE_ALIGN, gb->colorHandleAlign);
112   UI_GetThemeColor4fv(TH_HANDLE_AUTOCLAMP, gb->colorHandleAutoclamp);
113   UI_GetThemeColor4fv(TH_HANDLE_SEL_FREE, gb->colorHandleSelFree);
114   UI_GetThemeColor4fv(TH_HANDLE_SEL_AUTO, gb->colorHandleSelAuto);
115   UI_GetThemeColor4fv(TH_HANDLE_SEL_VECT, gb->colorHandleSelVect);
116   UI_GetThemeColor4fv(TH_HANDLE_SEL_ALIGN, gb->colorHandleSelAlign);
117   UI_GetThemeColor4fv(TH_HANDLE_SEL_AUTOCLAMP, gb->colorHandleSelAutoclamp);
118   UI_GetThemeColor4fv(TH_NURB_ULINE, gb->colorNurbUline);
119   UI_GetThemeColor4fv(TH_NURB_VLINE, gb->colorNurbVline);
120   UI_GetThemeColor4fv(TH_NURB_SEL_ULINE, gb->colorNurbSelUline);
121   UI_GetThemeColor4fv(TH_NURB_SEL_VLINE, gb->colorNurbSelVline);
122   UI_GetThemeColor4fv(TH_ACTIVE_SPLINE, gb->colorActiveSpline);
123
124   UI_GetThemeColor4fv(TH_BONE_POSE, gb->colorBonePose);
125
126   UI_GetThemeColor4fv(TH_CFRAME, gb->colorCurrentFrame);
127
128   /* Grid */
129   UI_GetThemeColorShade4fv(TH_GRID, 10, gb->colorGrid);
130   /* emphasise division lines lighter instead of darker, if background is darker than grid */
131   UI_GetThemeColorShade4fv(
132       TH_GRID,
133       (gb->colorGrid[0] + gb->colorGrid[1] + gb->colorGrid[2] + 0.12f >
134        gb->colorBackground[0] + gb->colorBackground[1] + gb->colorBackground[2]) ?
135           20 :
136           -10,
137       gb->colorGridEmphasise);
138   /* Grid Axis */
139   UI_GetThemeColorBlendShade4fv(TH_GRID, TH_AXIS_X, 0.5f, -10, gb->colorGridAxisX);
140   UI_GetThemeColorBlendShade4fv(TH_GRID, TH_AXIS_Y, 0.5f, -10, gb->colorGridAxisY);
141   UI_GetThemeColorBlendShade4fv(TH_GRID, TH_AXIS_Z, 0.5f, -10, gb->colorGridAxisZ);
142
143   UI_GetThemeColorShadeAlpha4fv(TH_TRANSFORM, 0, -80, gb->colorDeselect);
144   UI_GetThemeColorShadeAlpha4fv(TH_WIRE, 0, -30, gb->colorOutline);
145   UI_GetThemeColorShadeAlpha4fv(TH_LIGHT, 0, 255, gb->colorLightNoAlpha);
146
147   gb->sizeLightCenter = (UI_GetThemeValuef(TH_OBCENTER_DIA) + 1.5f) * U.pixelsize;
148   gb->sizeLightCircle = U.pixelsize * 9.0f;
149   gb->sizeLightCircleShadow = gb->sizeLightCircle + U.pixelsize * 3.0f;
150
151   /* M_SQRT2 to be at least the same size of the old square */
152   gb->sizeVertex = U.pixelsize *
153                    (max_ff(1.0f, UI_GetThemeValuef(TH_VERTEX_SIZE) * (float)M_SQRT2 / 2.0f));
154   gb->sizeFaceDot = U.pixelsize * UI_GetThemeValuef(TH_FACEDOT_SIZE);
155   gb->sizeEdge = U.pixelsize * (1.0f / 2.0f); /* TODO Theme */
156   gb->sizeEdgeFix = U.pixelsize * (0.5f + 2.0f * (2.0f * (gb->sizeEdge * (float)M_SQRT1_2)));
157
158   /* Color management. */
159   if (!DRW_state_do_color_management()) {
160     float *color = gb->UBO_FIRST_COLOR;
161     do {
162       /* TODO more accurate transform. */
163       srgb_to_linearrgb_v4(color, color);
164       color += 4;
165     } while (color != gb->UBO_LAST_COLOR);
166   }
167
168   if (G_draw.block_ubo == NULL) {
169     G_draw.block_ubo = DRW_uniformbuffer_create(sizeof(GlobalsUboStorage), gb);
170   }
171
172   DRW_uniformbuffer_update(G_draw.block_ubo, gb);
173
174   if (!G_draw.ramp) {
175     ColorBand ramp = {0};
176     float *colors;
177     int col_size;
178
179     ramp.tot = 3;
180     ramp.data[0].a = 1.0f;
181     ramp.data[0].b = 1.0f;
182     ramp.data[0].pos = 0.0f;
183     ramp.data[1].a = 1.0f;
184     ramp.data[1].g = 1.0f;
185     ramp.data[1].pos = 0.5f;
186     ramp.data[2].a = 1.0f;
187     ramp.data[2].r = 1.0f;
188     ramp.data[2].pos = 1.0f;
189
190     BKE_colorband_evaluate_table_rgba(&ramp, &colors, &col_size);
191
192     G_draw.ramp = GPU_texture_create_1d(col_size, GPU_RGBA8, colors, NULL);
193
194     MEM_freeN(colors);
195   }
196
197   /* Weight Painting color ramp texture */
198   bool user_weight_ramp = (U.flag & USER_CUSTOM_RANGE) != 0;
199
200   if (weight_ramp_custom != user_weight_ramp ||
201       (user_weight_ramp && memcmp(&weight_ramp_copy, &U.coba_weight, sizeof(ColorBand)) != 0)) {
202     DRW_TEXTURE_FREE_SAFE(G_draw.weight_ramp);
203   }
204
205   if (G_draw.weight_ramp == NULL) {
206     weight_ramp_custom = user_weight_ramp;
207     memcpy(&weight_ramp_copy, &U.coba_weight, sizeof(ColorBand));
208
209     G_draw.weight_ramp = DRW_create_weight_colorramp_texture();
210   }
211 }
212
213 /* ********************************* SHGROUP ************************************* */
214
215 extern char datatoc_animviz_mpath_lines_vert_glsl[];
216 extern char datatoc_animviz_mpath_lines_geom_glsl[];
217 extern char datatoc_animviz_mpath_points_vert_glsl[];
218
219 extern char datatoc_volume_velocity_vert_glsl[];
220
221 extern char datatoc_armature_axes_vert_glsl[];
222 extern char datatoc_armature_sphere_solid_vert_glsl[];
223 extern char datatoc_armature_sphere_solid_frag_glsl[];
224 extern char datatoc_armature_sphere_outline_vert_glsl[];
225 extern char datatoc_armature_envelope_solid_vert_glsl[];
226 extern char datatoc_armature_envelope_solid_frag_glsl[];
227 extern char datatoc_armature_envelope_outline_vert_glsl[];
228 extern char datatoc_armature_envelope_distance_frag_glsl[];
229 extern char datatoc_armature_shape_solid_vert_glsl[];
230 extern char datatoc_armature_shape_solid_frag_glsl[];
231 extern char datatoc_armature_shape_outline_vert_glsl[];
232 extern char datatoc_armature_shape_outline_geom_glsl[];
233 extern char datatoc_armature_stick_vert_glsl[];
234 extern char datatoc_armature_stick_frag_glsl[];
235 extern char datatoc_armature_dof_vert_glsl[];
236
237 extern char datatoc_common_globals_lib_glsl[];
238 extern char datatoc_common_view_lib_glsl[];
239
240 extern char datatoc_gpu_shader_flat_color_frag_glsl[];
241 extern char datatoc_gpu_shader_3D_smooth_color_frag_glsl[];
242 extern char datatoc_gpu_shader_point_varying_color_frag_glsl[];
243
244 extern char datatoc_object_mball_handles_vert_glsl[];
245 extern char datatoc_object_empty_axes_vert_glsl[];
246
247 typedef struct COMMON_Shaders {
248   struct GPUShader *shape_outline;
249   struct GPUShader *shape_solid;
250   struct GPUShader *bone_axes;
251   struct GPUShader *bone_envelope;
252   struct GPUShader *bone_envelope_distance;
253   struct GPUShader *bone_envelope_outline;
254   struct GPUShader *bone_sphere;
255   struct GPUShader *bone_sphere_outline;
256   struct GPUShader *bone_stick;
257   struct GPUShader *bone_dofs;
258
259   struct GPUShader *mpath_line_sh;
260   struct GPUShader *mpath_points_sh;
261
262   struct GPUShader *volume_velocity_needle_sh;
263   struct GPUShader *volume_velocity_sh;
264   struct GPUShader *empty_axes_sh;
265
266   struct GPUShader *mball_handles;
267 } COMMON_Shaders;
268
269 static COMMON_Shaders g_shaders[GPU_SHADER_CFG_LEN] = {{NULL}};
270
271 static struct {
272   struct GPUVertFormat *instance_screenspace;
273   struct GPUVertFormat *instance_color;
274   struct GPUVertFormat *instance_screen_aligned;
275   struct GPUVertFormat *instance_scaled;
276   struct GPUVertFormat *instance_sized;
277   struct GPUVertFormat *instance_outline;
278   struct GPUVertFormat *instance;
279   struct GPUVertFormat *instance_camera;
280   struct GPUVertFormat *instance_distance_lines;
281   struct GPUVertFormat *instance_spot;
282   struct GPUVertFormat *instance_bone;
283   struct GPUVertFormat *instance_bone_dof;
284   struct GPUVertFormat *instance_bone_stick;
285   struct GPUVertFormat *instance_bone_outline;
286   struct GPUVertFormat *instance_bone_envelope;
287   struct GPUVertFormat *instance_bone_envelope_distance;
288   struct GPUVertFormat *instance_bone_envelope_outline;
289   struct GPUVertFormat *instance_mball_handles;
290   struct GPUVertFormat *pos_color;
291   struct GPUVertFormat *pos;
292 } g_formats = {NULL};
293
294 void DRW_globals_free(void)
295 {
296   struct GPUVertFormat **format = &g_formats.instance_screenspace;
297   for (int i = 0; i < sizeof(g_formats) / sizeof(void *); ++i, ++format) {
298     MEM_SAFE_FREE(*format);
299   }
300
301   for (int j = 0; j < GPU_SHADER_CFG_LEN; j++) {
302     struct GPUShader **shader = &g_shaders[j].shape_outline;
303     for (int i = 0; i < sizeof(g_shaders[j]) / sizeof(void *); ++i, ++shader) {
304       DRW_SHADER_FREE_SAFE(*shader);
305     }
306   }
307 }
308
309 struct DRWCallBuffer *buffer_dynlines_flat_color(DRWPass *pass, eGPUShaderConfig sh_cfg)
310 {
311   GPUShader *sh = GPU_shader_get_builtin_shader_with_config(GPU_SHADER_3D_FLAT_COLOR, sh_cfg);
312
313   DRW_shgroup_instance_format(g_formats.pos_color,
314                               {
315                                   {"pos", DRW_ATTR_FLOAT, 3},
316                                   {"color", DRW_ATTR_FLOAT, 4},
317                               });
318
319   DRWShadingGroup *grp = DRW_shgroup_create(sh, pass);
320   if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
321     DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
322   }
323   return DRW_shgroup_call_buffer(grp, g_formats.pos_color, GPU_PRIM_LINES);
324 }
325
326 struct DRWCallBuffer *buffer_dynlines_dashed_uniform_color(DRWPass *pass,
327                                                            const float color[4],
328                                                            eGPUShaderConfig sh_cfg)
329 {
330   GPUShader *sh = GPU_shader_get_builtin_shader_with_config(
331       GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR, sh_cfg);
332
333   static float dash_width = 6.0f;
334   static float dash_factor = 0.5f;
335
336   DRW_shgroup_instance_format(g_formats.pos, {{"pos", DRW_ATTR_FLOAT, 3}});
337
338   DRWShadingGroup *grp = DRW_shgroup_create(sh, pass);
339   DRW_shgroup_uniform_vec4(grp, "color", color, 1);
340   DRW_shgroup_uniform_vec2(grp, "viewport_size", DRW_viewport_size_get(), 1);
341   DRW_shgroup_uniform_float(grp, "dash_width", &dash_width, 1);
342   DRW_shgroup_uniform_float(grp, "dash_factor", &dash_factor, 1);
343   DRW_shgroup_uniform_int_copy(grp, "colors_len", 0); /* "simple" mode */
344   if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
345     DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
346   }
347   return DRW_shgroup_call_buffer(grp, g_formats.pos, GPU_PRIM_LINES);
348 }
349
350 struct DRWCallBuffer *buffer_dynpoints_uniform_color(DRWShadingGroup *grp)
351 {
352   DRW_shgroup_instance_format(g_formats.pos, {{"pos", DRW_ATTR_FLOAT, 3}});
353
354   return DRW_shgroup_call_buffer(grp, g_formats.pos, GPU_PRIM_POINTS);
355 }
356
357 struct DRWCallBuffer *buffer_groundlines_uniform_color(DRWPass *pass,
358                                                        const float color[4],
359                                                        eGPUShaderConfig sh_cfg)
360 {
361   GPUShader *sh = GPU_shader_get_builtin_shader_with_config(GPU_SHADER_3D_GROUNDLINE, sh_cfg);
362
363   DRW_shgroup_instance_format(g_formats.pos, {{"pos", DRW_ATTR_FLOAT, 3}});
364
365   DRWShadingGroup *grp = DRW_shgroup_create(sh, pass);
366   DRW_shgroup_uniform_vec4(grp, "color", color, 1);
367   if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
368     DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
369   }
370   return DRW_shgroup_call_buffer(grp, g_formats.pos, GPU_PRIM_POINTS);
371 }
372
373 struct DRWCallBuffer *buffer_groundpoints_uniform_color(DRWPass *pass,
374                                                         const float color[4],
375                                                         eGPUShaderConfig sh_cfg)
376 {
377   GPUShader *sh = GPU_shader_get_builtin_shader_with_config(GPU_SHADER_3D_GROUNDPOINT, sh_cfg);
378
379   DRW_shgroup_instance_format(g_formats.pos, {{"pos", DRW_ATTR_FLOAT, 3}});
380
381   DRWShadingGroup *grp = DRW_shgroup_create(sh, pass);
382   DRW_shgroup_uniform_vec4(grp, "color", color, 1);
383   if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
384     DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
385   }
386   return DRW_shgroup_call_buffer(grp, g_formats.pos, GPU_PRIM_POINTS);
387 }
388
389 struct DRWCallBuffer *buffer_instance_screenspace(DRWPass *pass,
390                                                   struct GPUBatch *geom,
391                                                   const float *size,
392                                                   eGPUShaderConfig sh_cfg)
393 {
394   GPUShader *sh = GPU_shader_get_builtin_shader_with_config(
395       GPU_SHADER_3D_SCREENSPACE_VARIYING_COLOR, sh_cfg);
396
397   DRW_shgroup_instance_format(g_formats.instance_screenspace,
398                               {
399                                   {"world_pos", DRW_ATTR_FLOAT, 3},
400                                   {"color", DRW_ATTR_FLOAT, 3},
401                               });
402
403   DRWShadingGroup *grp = DRW_shgroup_create(sh, pass);
404   DRW_shgroup_uniform_float(grp, "size", size, 1);
405   DRW_shgroup_uniform_float(grp, "pixel_size", DRW_viewport_pixelsize_get(), 1);
406   DRW_shgroup_uniform_vec3(grp, "screen_vecs[0]", DRW_viewport_screenvecs_get(), 2);
407   if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
408     DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
409   }
410   return DRW_shgroup_call_buffer_instance(grp, g_formats.instance_screenspace, geom);
411 }
412
413 struct DRWCallBuffer *buffer_instance_solid(DRWPass *pass, struct GPUBatch *geom)
414 {
415   static float light[3] = {0.0f, 0.0f, 1.0f};
416   GPUShader *sh = GPU_shader_get_builtin_shader(
417       GPU_SHADER_3D_OBJECTSPACE_SIMPLE_LIGHTING_VARIYING_COLOR);
418
419   DRW_shgroup_instance_format(g_formats.instance_color,
420                               {
421                                   {"InstanceModelMatrix", DRW_ATTR_FLOAT, 16},
422                                   {"color", DRW_ATTR_FLOAT, 4},
423                               });
424
425   DRWShadingGroup *grp = DRW_shgroup_create(sh, pass);
426   DRW_shgroup_uniform_vec3(grp, "light", light, 1);
427
428   return DRW_shgroup_call_buffer_instance(grp, g_formats.instance_color, geom);
429 }
430
431 struct DRWCallBuffer *buffer_instance_wire(DRWPass *pass, struct GPUBatch *geom)
432 {
433   GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_OBJECTSPACE_VARIYING_COLOR);
434
435   DRW_shgroup_instance_format(g_formats.instance_color,
436                               {
437                                   {"InstanceModelMatrix", DRW_ATTR_FLOAT, 16},
438                                   {"color", DRW_ATTR_FLOAT, 4},
439                               });
440
441   DRWShadingGroup *grp = DRW_shgroup_create(sh, pass);
442
443   return DRW_shgroup_call_buffer_instance(grp, g_formats.instance_color, geom);
444 }
445
446 struct DRWCallBuffer *buffer_instance_screen_aligned(DRWPass *pass,
447                                                      struct GPUBatch *geom,
448                                                      eGPUShaderConfig sh_cfg)
449 {
450   GPUShader *sh = GPU_shader_get_builtin_shader_with_config(GPU_SHADER_3D_INSTANCE_SCREEN_ALIGNED,
451                                                             sh_cfg);
452
453   DRW_shgroup_instance_format(g_formats.instance_screen_aligned,
454                               {
455                                   {"color", DRW_ATTR_FLOAT, 3},
456                                   {"size", DRW_ATTR_FLOAT, 1},
457                                   {"InstanceModelMatrix", DRW_ATTR_FLOAT, 16},
458                               });
459
460   DRWShadingGroup *grp = DRW_shgroup_create(sh, pass);
461   DRW_shgroup_uniform_vec3(grp, "screen_vecs[0]", DRW_viewport_screenvecs_get(), 2);
462   if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
463     DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
464   }
465   return DRW_shgroup_call_buffer_instance(grp, g_formats.instance_screen_aligned, geom);
466 }
467
468 struct DRWCallBuffer *buffer_instance_scaled(DRWPass *pass,
469                                              struct GPUBatch *geom,
470                                              eGPUShaderConfig sh_cfg)
471 {
472   GPUShader *sh_inst = GPU_shader_get_builtin_shader_with_config(
473       GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SCALE, sh_cfg);
474
475   DRW_shgroup_instance_format(g_formats.instance_scaled,
476                               {
477                                   {"color", DRW_ATTR_FLOAT, 3},
478                                   {"size", DRW_ATTR_FLOAT, 3},
479                                   {"InstanceModelMatrix", DRW_ATTR_FLOAT, 16},
480                               });
481
482   DRWShadingGroup *grp = DRW_shgroup_create(sh_inst, pass);
483   if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
484     DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
485   }
486   return DRW_shgroup_call_buffer_instance(grp, g_formats.instance_scaled, geom);
487 }
488
489 struct DRWCallBuffer *buffer_instance(DRWPass *pass,
490                                       struct GPUBatch *geom,
491                                       eGPUShaderConfig sh_cfg)
492 {
493   GPUShader *sh_inst = GPU_shader_get_builtin_shader_with_config(
494       GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SIZE, sh_cfg);
495
496   DRW_shgroup_instance_format(g_formats.instance_sized,
497                               {
498                                   {"color", DRW_ATTR_FLOAT, 4},
499                                   {"size", DRW_ATTR_FLOAT, 1},
500                                   {"InstanceModelMatrix", DRW_ATTR_FLOAT, 16},
501                               });
502
503   DRWShadingGroup *grp = DRW_shgroup_create(sh_inst, pass);
504   DRW_shgroup_state_disable(grp, DRW_STATE_BLEND_ALPHA);
505   if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
506     DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
507   }
508   return DRW_shgroup_call_buffer_instance(grp, g_formats.instance_sized, geom);
509 }
510
511 struct DRWCallBuffer *buffer_instance_alpha(DRWShadingGroup *grp, struct GPUBatch *geom)
512 {
513   DRW_shgroup_instance_format(g_formats.instance_sized,
514                               {
515                                   {"color", DRW_ATTR_FLOAT, 4},
516                                   {"size", DRW_ATTR_FLOAT, 1},
517                                   {"InstanceModelMatrix", DRW_ATTR_FLOAT, 16},
518                               });
519
520   return DRW_shgroup_call_buffer_instance(grp, g_formats.instance_sized, geom);
521 }
522
523 struct DRWCallBuffer *buffer_instance_empty_axes(DRWPass *pass,
524                                                  struct GPUBatch *geom,
525                                                  eGPUShaderConfig sh_cfg)
526 {
527   COMMON_Shaders *sh_data = &g_shaders[sh_cfg];
528   if (sh_data->empty_axes_sh == NULL) {
529     const GPUShaderConfigData *sh_cfg_data = &GPU_shader_cfg_data[sh_cfg];
530     sh_data->empty_axes_sh = GPU_shader_create_from_arrays({
531         .vert = (const char *[]){sh_cfg_data->lib, datatoc_object_empty_axes_vert_glsl, NULL},
532         .frag = (const char *[]){datatoc_gpu_shader_flat_color_frag_glsl, NULL},
533         .defs = (const char *[]){sh_cfg_data->def, NULL},
534     });
535   }
536
537   DRW_shgroup_instance_format(g_formats.instance_sized,
538                               {
539                                   {"color", DRW_ATTR_FLOAT, 3},
540                                   {"size", DRW_ATTR_FLOAT, 1},
541                                   {"InstanceModelMatrix", DRW_ATTR_FLOAT, 16},
542                               });
543
544   DRWShadingGroup *grp = DRW_shgroup_create(sh_data->empty_axes_sh, pass);
545   DRW_shgroup_uniform_vec3(grp, "screenVecs[0]", DRW_viewport_screenvecs_get(), 2);
546   if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
547     DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
548   }
549   return DRW_shgroup_call_buffer_instance(grp, g_formats.instance_sized, geom);
550 }
551
552 struct DRWCallBuffer *buffer_instance_outline(DRWPass *pass,
553                                               struct GPUBatch *geom,
554                                               const int *baseid,
555                                               eGPUShaderConfig sh_cfg)
556 {
557   GPUShader *sh_inst = GPU_shader_get_builtin_shader_with_config(
558       GPU_SHADER_INSTANCE_VARIYING_ID_VARIYING_SIZE, sh_cfg);
559
560   DRW_shgroup_instance_format(g_formats.instance_outline,
561                               {
562                                   {"callId", DRW_ATTR_INT, 1},
563                                   {"size", DRW_ATTR_FLOAT, 1},
564                                   {"InstanceModelMatrix", DRW_ATTR_FLOAT, 16},
565                               });
566
567   DRWShadingGroup *grp = DRW_shgroup_create(sh_inst, pass);
568   DRW_shgroup_uniform_int(grp, "baseId", baseid, 1);
569
570   if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
571     DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
572   }
573   return DRW_shgroup_call_buffer_instance(grp, g_formats.instance_outline, geom);
574 }
575
576 struct DRWCallBuffer *buffer_camera_instance(DRWPass *pass,
577                                              struct GPUBatch *geom,
578                                              eGPUShaderConfig sh_cfg)
579 {
580   GPUShader *sh_inst = GPU_shader_get_builtin_shader_with_config(GPU_SHADER_CAMERA, sh_cfg);
581
582   DRW_shgroup_instance_format(g_formats.instance_camera,
583                               {
584                                   {"color", DRW_ATTR_FLOAT, 3},
585                                   {"corners", DRW_ATTR_FLOAT, 8},
586                                   {"depth", DRW_ATTR_FLOAT, 1},
587                                   {"tria", DRW_ATTR_FLOAT, 4},
588                                   {"InstanceModelMatrix", DRW_ATTR_FLOAT, 16},
589                               });
590
591   DRWShadingGroup *grp = DRW_shgroup_create(sh_inst, pass);
592   if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
593     DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
594   }
595   return DRW_shgroup_call_buffer_instance(grp, g_formats.instance_camera, geom);
596 }
597
598 struct DRWCallBuffer *buffer_distance_lines_instance(DRWPass *pass,
599                                                      struct GPUBatch *geom,
600                                                      eGPUShaderConfig sh_cfg)
601 {
602   GPUShader *sh_inst = GPU_shader_get_builtin_shader_with_config(GPU_SHADER_DISTANCE_LINES,
603                                                                  sh_cfg);
604   static float point_size = 4.0f;
605
606   DRW_shgroup_instance_format(g_formats.instance_distance_lines,
607                               {
608                                   {"color", DRW_ATTR_FLOAT, 3},
609                                   {"start", DRW_ATTR_FLOAT, 1},
610                                   {"end", DRW_ATTR_FLOAT, 1},
611                                   {"InstanceModelMatrix", DRW_ATTR_FLOAT, 16},
612                               });
613
614   DRWShadingGroup *grp = DRW_shgroup_create(sh_inst, pass);
615   DRW_shgroup_uniform_float(grp, "size", &point_size, 1);
616   if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
617     DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
618   }
619   return DRW_shgroup_call_buffer_instance(grp, g_formats.instance_distance_lines, geom);
620 }
621
622 struct DRWCallBuffer *buffer_spot_instance(DRWPass *pass,
623                                            struct GPUBatch *geom,
624                                            eGPUShaderConfig sh_cfg)
625 {
626   GPUShader *sh_inst = GPU_shader_get_builtin_shader_with_config(
627       GPU_SHADER_INSTANCE_EDGES_VARIYING_COLOR, sh_cfg);
628   static const int True = true;
629   static const int False = false;
630
631   DRW_shgroup_instance_format(g_formats.instance_spot,
632                               {
633                                   {"color", DRW_ATTR_FLOAT, 3},
634                                   {"InstanceModelMatrix", DRW_ATTR_FLOAT, 16},
635                               });
636
637   DRWShadingGroup *grp = DRW_shgroup_create(sh_inst, pass);
638   DRW_shgroup_uniform_bool(grp, "drawFront", &False, 1);
639   DRW_shgroup_uniform_bool(grp, "drawBack", &False, 1);
640   DRW_shgroup_uniform_bool(grp, "drawSilhouette", &True, 1);
641   if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
642     DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
643   }
644   return DRW_shgroup_call_buffer_instance(grp, g_formats.instance_spot, geom);
645 }
646
647 struct DRWCallBuffer *buffer_instance_bone_axes(DRWPass *pass, eGPUShaderConfig sh_cfg)
648 {
649   COMMON_Shaders *sh_data = &g_shaders[sh_cfg];
650   if (sh_data->bone_axes == NULL) {
651     const GPUShaderConfigData *sh_cfg_data = &GPU_shader_cfg_data[sh_cfg];
652     sh_data->bone_axes = GPU_shader_create_from_arrays({
653         .vert = (const char *[]){sh_cfg_data->lib, datatoc_armature_axes_vert_glsl, NULL},
654         .frag = (const char *[]){datatoc_gpu_shader_flat_color_frag_glsl, NULL},
655         .defs = (const char *[]){sh_cfg_data->def, NULL},
656     });
657   }
658
659   DRW_shgroup_instance_format(g_formats.instance_color,
660                               {
661                                   {"InstanceModelMatrix", DRW_ATTR_FLOAT, 16},
662                                   {"color", DRW_ATTR_FLOAT, 4},
663                               });
664
665   DRWShadingGroup *grp = DRW_shgroup_create(sh_data->bone_axes, pass);
666   DRW_shgroup_uniform_vec3(grp, "screenVecs[0]", DRW_viewport_screenvecs_get(), 2);
667   if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
668     DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
669   }
670   return DRW_shgroup_call_buffer_instance(
671       grp, g_formats.instance_color, DRW_cache_bone_arrows_get());
672 }
673
674 struct DRWCallBuffer *buffer_instance_bone_envelope_outline(DRWPass *pass, eGPUShaderConfig sh_cfg)
675 {
676   COMMON_Shaders *sh_data = &g_shaders[sh_cfg];
677   if (sh_data->bone_envelope_outline == NULL) {
678     const GPUShaderConfigData *sh_cfg_data = &GPU_shader_cfg_data[sh_cfg];
679     sh_data->bone_envelope_outline = GPU_shader_create_from_arrays({
680         .vert =
681             (const char *[]){sh_cfg_data->lib, datatoc_armature_envelope_outline_vert_glsl, NULL},
682         .frag = (const char *[]){datatoc_gpu_shader_flat_color_frag_glsl, NULL},
683         .defs = (const char *[]){sh_cfg_data->def, NULL},
684     });
685   }
686
687   DRW_shgroup_instance_format(g_formats.instance_bone_envelope_outline,
688                               {
689                                   {"headSphere", DRW_ATTR_FLOAT, 4},
690                                   {"tailSphere", DRW_ATTR_FLOAT, 4},
691                                   {"outlineColorSize", DRW_ATTR_FLOAT, 4},
692                                   {"xAxis", DRW_ATTR_FLOAT, 3},
693                               });
694
695   DRWShadingGroup *grp = DRW_shgroup_create(sh_data->bone_envelope_outline, pass);
696   DRW_shgroup_uniform_vec2(grp, "viewportSize", DRW_viewport_size_get(), 1);
697   if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
698     DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
699   }
700   return DRW_shgroup_call_buffer_instance(
701       grp, g_formats.instance_bone_envelope_outline, DRW_cache_bone_envelope_outline_get());
702 }
703
704 struct DRWCallBuffer *buffer_instance_bone_envelope_distance(DRWPass *pass,
705                                                              eGPUShaderConfig sh_cfg)
706 {
707   COMMON_Shaders *sh_data = &g_shaders[sh_cfg];
708   if (sh_data->bone_envelope_distance == NULL) {
709     const GPUShaderConfigData *sh_cfg_data = &GPU_shader_cfg_data[sh_cfg];
710     sh_data->bone_envelope_distance = GPU_shader_create_from_arrays({
711         .vert =
712             (const char *[]){sh_cfg_data->lib, datatoc_armature_envelope_solid_vert_glsl, NULL},
713         .frag = (const char *[]){datatoc_armature_envelope_distance_frag_glsl, NULL},
714         .defs = (const char *[]){sh_cfg_data->def, NULL},
715     });
716   }
717
718   DRW_shgroup_instance_format(g_formats.instance_bone_envelope_distance,
719                               {
720                                   {"headSphere", DRW_ATTR_FLOAT, 4},
721                                   {"tailSphere", DRW_ATTR_FLOAT, 4},
722                                   {"xAxis", DRW_ATTR_FLOAT, 3},
723                               });
724
725   DRWShadingGroup *grp = DRW_shgroup_create(sh_data->bone_envelope_distance, pass);
726   if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
727     DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
728   }
729   return DRW_shgroup_call_buffer_instance(
730       grp, g_formats.instance_bone_envelope_distance, DRW_cache_bone_envelope_solid_get());
731 }
732
733 struct DRWCallBuffer *buffer_instance_bone_envelope_solid(DRWPass *pass,
734                                                           bool transp,
735                                                           eGPUShaderConfig sh_cfg)
736 {
737   COMMON_Shaders *sh_data = &g_shaders[sh_cfg];
738   if (sh_data->bone_envelope == NULL) {
739     const GPUShaderConfigData *sh_cfg_data = &GPU_shader_cfg_data[sh_cfg];
740     sh_data->bone_envelope = GPU_shader_create_from_arrays({
741         .vert =
742             (const char *[]){sh_cfg_data->lib, datatoc_armature_envelope_solid_vert_glsl, NULL},
743         .frag = (const char *[]){datatoc_armature_envelope_solid_frag_glsl, NULL},
744         .defs = (const char *[]){sh_cfg_data->def, NULL},
745     });
746   }
747
748   DRW_shgroup_instance_format(g_formats.instance_bone_envelope,
749                               {
750                                   {"headSphere", DRW_ATTR_FLOAT, 4},
751                                   {"tailSphere", DRW_ATTR_FLOAT, 4},
752                                   {"boneColor", DRW_ATTR_FLOAT, 3},
753                                   {"stateColor", DRW_ATTR_FLOAT, 3},
754                                   {"xAxis", DRW_ATTR_FLOAT, 3},
755                               });
756
757   DRWShadingGroup *grp = DRW_shgroup_create(sh_data->bone_envelope, pass);
758   /* We can have a lot of overdraw if we don't do this. Also envelope are not subject to
759    * inverted matrix. */
760   DRW_shgroup_state_enable(grp, DRW_STATE_CULL_BACK);
761   DRW_shgroup_uniform_float_copy(grp, "alpha", transp ? 0.6f : 1.0f);
762   if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
763     DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
764   }
765   return DRW_shgroup_call_buffer_instance(
766       grp, g_formats.instance_bone_envelope, DRW_cache_bone_envelope_solid_get());
767 }
768
769 struct DRWCallBuffer *buffer_instance_mball_handles(DRWPass *pass, eGPUShaderConfig sh_cfg)
770 {
771   COMMON_Shaders *sh_data = &g_shaders[sh_cfg];
772   if (sh_data->mball_handles == NULL) {
773     const GPUShaderConfigData *sh_cfg_data = &GPU_shader_cfg_data[sh_cfg];
774     sh_data->mball_handles = GPU_shader_create_from_arrays({
775         .vert = (const char *[]){sh_cfg_data->lib, datatoc_object_mball_handles_vert_glsl, NULL},
776         .frag = (const char *[]){datatoc_gpu_shader_flat_color_frag_glsl, NULL},
777         .defs = (const char *[]){sh_cfg_data->def, NULL},
778     });
779   }
780
781   DRW_shgroup_instance_format(g_formats.instance_mball_handles,
782                               {
783                                   {"ScaleTranslationMatrix", DRW_ATTR_FLOAT, 12},
784                                   {"radius", DRW_ATTR_FLOAT, 1},
785                                   {"color", DRW_ATTR_FLOAT, 3},
786                               });
787
788   DRWShadingGroup *grp = DRW_shgroup_create(sh_data->mball_handles, pass);
789   DRW_shgroup_uniform_vec3(grp, "screen_vecs[0]", DRW_viewport_screenvecs_get(), 2);
790   if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
791     DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
792   }
793   return DRW_shgroup_call_buffer_instance(
794       grp, g_formats.instance_mball_handles, DRW_cache_screenspace_circle_get());
795 }
796
797 /* Only works with batches with adjacency infos. */
798 struct DRWCallBuffer *buffer_instance_bone_shape_outline(DRWPass *pass,
799                                                          struct GPUBatch *geom,
800                                                          eGPUShaderConfig sh_cfg)
801 {
802   COMMON_Shaders *sh_data = &g_shaders[sh_cfg];
803   if (sh_data->shape_outline == NULL) {
804     const GPUShaderConfigData *sh_cfg_data = &GPU_shader_cfg_data[sh_cfg];
805     sh_data->shape_outline = GPU_shader_create_from_arrays({
806         .vert = (const char *[]){sh_cfg_data->lib,
807                                  datatoc_common_view_lib_glsl,
808                                  datatoc_armature_shape_outline_vert_glsl,
809                                  NULL},
810         .geom = (const char *[]){sh_cfg_data->lib,
811                                  datatoc_common_view_lib_glsl,
812                                  datatoc_armature_shape_outline_geom_glsl,
813                                  NULL},
814         .frag = (const char *[]){datatoc_gpu_shader_flat_color_frag_glsl, NULL},
815         .defs = (const char *[]){sh_cfg_data->def, NULL},
816     });
817   }
818
819   DRW_shgroup_instance_format(g_formats.instance_bone_outline,
820                               {
821                                   {"InstanceModelMatrix", DRW_ATTR_FLOAT, 16},
822                                   {"outlineColorSize", DRW_ATTR_FLOAT, 4},
823                               });
824
825   DRWShadingGroup *grp = DRW_shgroup_create(sh_data->shape_outline, pass);
826   DRW_shgroup_uniform_vec2(grp, "viewportSize", DRW_viewport_size_get(), 1);
827   if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
828     DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
829   }
830   return DRW_shgroup_call_buffer_instance(grp, g_formats.instance_bone_outline, geom);
831 }
832
833 struct DRWCallBuffer *buffer_instance_bone_shape_solid(DRWPass *pass,
834                                                        struct GPUBatch *geom,
835                                                        bool transp,
836                                                        eGPUShaderConfig sh_cfg)
837 {
838   COMMON_Shaders *sh_data = &g_shaders[sh_cfg];
839   if (sh_data->shape_solid == NULL) {
840     const GPUShaderConfigData *sh_cfg_data = &GPU_shader_cfg_data[sh_cfg];
841     sh_data->shape_solid = GPU_shader_create_from_arrays({
842         .vert = (const char *[]){sh_cfg_data->lib,
843                                  datatoc_common_view_lib_glsl,
844                                  datatoc_armature_shape_solid_vert_glsl,
845                                  NULL},
846         .frag = (const char *[]){datatoc_armature_shape_solid_frag_glsl, NULL},
847         .defs = (const char *[]){sh_cfg_data->def, NULL},
848     });
849   }
850
851   DRW_shgroup_instance_format(g_formats.instance_bone,
852                               {
853                                   {"InstanceModelMatrix", DRW_ATTR_FLOAT, 16},
854                                   {"boneColor", DRW_ATTR_FLOAT, 3},
855                                   {"stateColor", DRW_ATTR_FLOAT, 3},
856                               });
857
858   DRWShadingGroup *grp = DRW_shgroup_create(sh_data->shape_solid, pass);
859   DRW_shgroup_uniform_float_copy(grp, "alpha", transp ? 0.6f : 1.0f);
860   if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
861     DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
862   }
863   return DRW_shgroup_call_buffer_instance(grp, g_formats.instance_bone, geom);
864 }
865
866 struct DRWCallBuffer *buffer_instance_bone_sphere_solid(DRWPass *pass,
867                                                         bool transp,
868                                                         eGPUShaderConfig sh_cfg)
869 {
870   COMMON_Shaders *sh_data = &g_shaders[sh_cfg];
871   if (sh_data->bone_sphere == NULL) {
872     const GPUShaderConfigData *sh_cfg_data = &GPU_shader_cfg_data[sh_cfg];
873     sh_data->bone_sphere = GPU_shader_create_from_arrays({
874         .vert = (const char *[]){sh_cfg_data->lib, datatoc_armature_sphere_solid_vert_glsl, NULL},
875         .frag = (const char *[]){datatoc_armature_sphere_solid_frag_glsl, NULL},
876         .defs = (const char *[]){sh_cfg_data->def, NULL},
877     });
878   }
879
880   DRW_shgroup_instance_format(g_formats.instance_bone,
881                               {
882                                   {"InstanceModelMatrix", DRW_ATTR_FLOAT, 16},
883                                   {"boneColor", DRW_ATTR_FLOAT, 3},
884                                   {"stateColor", DRW_ATTR_FLOAT, 3},
885                               });
886
887   DRWShadingGroup *grp = DRW_shgroup_create(sh_data->bone_sphere, pass);
888   /* More transparent than the shape to be less distractive. */
889   DRW_shgroup_uniform_float_copy(grp, "alpha", transp ? 0.4f : 1.0f);
890   if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
891     DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
892   }
893   return DRW_shgroup_call_buffer_instance(
894       grp, g_formats.instance_bone, DRW_cache_bone_point_get());
895 }
896
897 struct DRWCallBuffer *buffer_instance_bone_sphere_outline(DRWPass *pass, eGPUShaderConfig sh_cfg)
898 {
899   COMMON_Shaders *sh_data = &g_shaders[sh_cfg];
900   if (sh_data->bone_sphere_outline == NULL) {
901     const GPUShaderConfigData *sh_cfg_data = &GPU_shader_cfg_data[sh_cfg];
902     sh_data->bone_sphere_outline = GPU_shader_create_from_arrays({
903         .vert =
904             (const char *[]){sh_cfg_data->lib, datatoc_armature_sphere_outline_vert_glsl, NULL},
905         .frag = (const char *[]){datatoc_gpu_shader_flat_color_frag_glsl, NULL},
906         .defs = (const char *[]){sh_cfg_data->def, NULL},
907     });
908   }
909
910   DRW_shgroup_instance_format(g_formats.instance_bone_outline,
911                               {
912                                   {"InstanceModelMatrix", DRW_ATTR_FLOAT, 16},
913                                   {"outlineColorSize", DRW_ATTR_FLOAT, 4},
914                               });
915
916   DRWShadingGroup *grp = DRW_shgroup_create(sh_data->bone_sphere_outline, pass);
917   DRW_shgroup_uniform_vec2(grp, "viewportSize", DRW_viewport_size_get(), 1);
918   if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
919     DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
920   }
921   return DRW_shgroup_call_buffer_instance(
922       grp, g_formats.instance_bone_outline, DRW_cache_bone_point_wire_outline_get());
923 }
924
925 struct DRWCallBuffer *buffer_instance_bone_stick(DRWPass *pass, eGPUShaderConfig sh_cfg)
926 {
927   COMMON_Shaders *sh_data = &g_shaders[sh_cfg];
928   if (sh_data->bone_stick == NULL) {
929     const GPUShaderConfigData *sh_cfg_data = &GPU_shader_cfg_data[sh_cfg];
930     sh_data->bone_stick = GPU_shader_create_from_arrays({
931         .vert = (const char *[]){sh_cfg_data->lib, datatoc_armature_stick_vert_glsl, NULL},
932         .frag = (const char *[]){datatoc_armature_stick_frag_glsl, NULL},
933         .defs = (const char *[]){sh_cfg_data->def, NULL},
934     });
935   }
936
937   DRW_shgroup_instance_format(
938       g_formats.instance_bone_stick,
939       {
940           {"boneStart", DRW_ATTR_FLOAT, 3},
941           {"boneEnd", DRW_ATTR_FLOAT, 3},
942           {"wireColor", DRW_ATTR_FLOAT, 4}, /* TODO port theses to uchar color */
943           {"boneColor", DRW_ATTR_FLOAT, 4},
944           {"headColor", DRW_ATTR_FLOAT, 4},
945           {"tailColor", DRW_ATTR_FLOAT, 4},
946       });
947
948   DRWShadingGroup *grp = DRW_shgroup_create(sh_data->bone_stick, pass);
949   DRW_shgroup_uniform_vec2(grp, "viewportSize", DRW_viewport_size_get(), 1);
950   DRW_shgroup_uniform_float_copy(grp, "stickSize", 5.0f * U.pixelsize);
951   if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
952     DRW_shgroup_state_enable(grp, DRW_STATE_CLIP_PLANES);
953   }
954   return DRW_shgroup_call_buffer_instance(
955       grp, g_formats.instance_bone_stick, DRW_cache_bone_stick_get());
956 }
957
958 struct DRWCallBuffer *buffer_instance_bone_dof(struct DRWPass *pass,
959                                                struct GPUBatch *geom,
960                                                bool blend)
961 {
962   COMMON_Shaders *sh_data = &g_shaders[GPU_SHADER_CFG_DEFAULT];
963   if (sh_data->bone_dofs == NULL) {
964     sh_data->bone_dofs = DRW_shader_create(
965         datatoc_armature_dof_vert_glsl, NULL, datatoc_gpu_shader_flat_color_frag_glsl, NULL);
966   }
967
968   DRW_shgroup_instance_format(g_formats.instance_bone_dof,
969                               {
970                                   {"InstanceModelMatrix", DRW_ATTR_FLOAT, 16},
971                                   {"color", DRW_ATTR_FLOAT, 4},
972                                   {"amin", DRW_ATTR_FLOAT, 2},
973                                   {"amax", DRW_ATTR_FLOAT, 2},
974                               });
975
976   DRWShadingGroup *grp = DRW_shgroup_create(sh_data->bone_dofs, pass);
977   if (blend) {
978     DRW_shgroup_state_enable(grp, DRW_STATE_BLEND_ALPHA);
979     DRW_shgroup_state_disable(grp, DRW_STATE_CULL_FRONT);
980   }
981   return DRW_shgroup_call_buffer_instance(grp, g_formats.instance_bone_dof, geom);
982 }
983
984 void empties_callbuffers_create(struct DRWPass *pass,
985                                 DRWEmptiesBufferList *buffers,
986                                 eGPUShaderConfig sh_cfg)
987 {
988   struct GPUBatch *geom;
989
990   geom = DRW_cache_plain_axes_get();
991   buffers->plain_axes = buffer_instance(pass, geom, sh_cfg);
992
993   geom = DRW_cache_empty_cube_get();
994   buffers->cube = buffer_instance(pass, geom, sh_cfg);
995
996   geom = DRW_cache_circle_get();
997   buffers->circle = buffer_instance(pass, geom, sh_cfg);
998
999   geom = DRW_cache_empty_sphere_get();
1000   buffers->sphere = buffer_instance(pass, geom, sh_cfg);
1001
1002   geom = DRW_cache_sphere_get();
1003   buffers->sphere_solid = buffer_instance_solid(pass, geom);
1004
1005   geom = DRW_cache_empty_cylinder_get();
1006   buffers->cylinder = buffer_instance(pass, geom, sh_cfg);
1007
1008   geom = DRW_cache_empty_capsule_cap_get();
1009   buffers->capsule_cap = buffer_instance(pass, geom, sh_cfg);
1010
1011   geom = DRW_cache_empty_capsule_body_get();
1012   buffers->capsule_body = buffer_instance(pass, geom, sh_cfg);
1013
1014   geom = DRW_cache_empty_cone_get();
1015   buffers->cone = buffer_instance(pass, geom, sh_cfg);
1016
1017   geom = DRW_cache_single_arrow_get();
1018   buffers->single_arrow = buffer_instance(pass, geom, sh_cfg);
1019
1020   geom = DRW_cache_single_line_get();
1021   buffers->single_arrow_line = buffer_instance(pass, geom, sh_cfg);
1022
1023   geom = DRW_cache_bone_arrows_get();
1024   buffers->empty_axes = buffer_instance_empty_axes(pass, geom, sh_cfg);
1025 }
1026
1027 struct GPUShader *mpath_line_shader_get(void)
1028 {
1029   COMMON_Shaders *sh_data = &g_shaders[GPU_SHADER_CFG_DEFAULT];
1030   if (sh_data->mpath_line_sh == NULL) {
1031     sh_data->mpath_line_sh = DRW_shader_create_with_lib(
1032         datatoc_animviz_mpath_lines_vert_glsl,
1033         datatoc_animviz_mpath_lines_geom_glsl,
1034         datatoc_gpu_shader_3D_smooth_color_frag_glsl,
1035         datatoc_common_globals_lib_glsl,
1036         NULL);
1037   }
1038   return sh_data->mpath_line_sh;
1039 }
1040
1041 struct GPUShader *mpath_points_shader_get(void)
1042 {
1043   COMMON_Shaders *sh_data = &g_shaders[GPU_SHADER_CFG_DEFAULT];
1044   if (sh_data->mpath_points_sh == NULL) {
1045     sh_data->mpath_points_sh = DRW_shader_create_with_lib(
1046         datatoc_animviz_mpath_points_vert_glsl,
1047         NULL,
1048         datatoc_gpu_shader_point_varying_color_frag_glsl,
1049         datatoc_common_globals_lib_glsl,
1050         NULL);
1051   }
1052   return sh_data->mpath_points_sh;
1053 }
1054
1055 struct GPUShader *volume_velocity_shader_get(bool use_needle)
1056 {
1057   COMMON_Shaders *sh_data = &g_shaders[GPU_SHADER_CFG_DEFAULT];
1058   if (use_needle) {
1059     if (sh_data->volume_velocity_needle_sh == NULL) {
1060       sh_data->volume_velocity_needle_sh = DRW_shader_create_with_lib(
1061           datatoc_volume_velocity_vert_glsl,
1062           NULL,
1063           datatoc_gpu_shader_flat_color_frag_glsl,
1064           datatoc_common_view_lib_glsl,
1065           "#define USE_NEEDLE");
1066     }
1067     return sh_data->volume_velocity_needle_sh;
1068   }
1069   else {
1070     if (sh_data->volume_velocity_sh == NULL) {
1071       sh_data->volume_velocity_sh = DRW_shader_create_with_lib(
1072           datatoc_volume_velocity_vert_glsl,
1073           NULL,
1074           datatoc_gpu_shader_flat_color_frag_glsl,
1075           datatoc_common_view_lib_glsl,
1076           NULL);
1077     }
1078     return sh_data->volume_velocity_sh;
1079   }
1080 }
1081
1082 DRWView *DRW_view_create_with_zoffset(const RegionView3D *rv3d, float offset)
1083 {
1084   /* Create view with depth offset */
1085   const DRWView *default_view = DRW_view_default_get();
1086   float viewmat[4][4], winmat[4][4];
1087   DRW_view_viewmat_get(default_view, viewmat, false);
1088   DRW_view_winmat_get(default_view, winmat, false);
1089
1090   float viewdist = rv3d->dist;
1091
1092   /* special exception for ortho camera (viewdist isnt used for perspective cameras) */
1093   if (rv3d->persp == RV3D_CAMOB && rv3d->is_persp == false) {
1094     viewdist = 1.0f / max_ff(fabsf(winmat[0][0]), fabsf(winmat[1][1]));
1095   }
1096
1097   winmat[3][2] -= bglPolygonOffsetCalc((float *)winmat, viewdist, offset);
1098
1099   return DRW_view_create_sub(default_view, viewmat, winmat);
1100 }
1101
1102 /* ******************************************** COLOR UTILS ************************************ */
1103
1104 /* TODO FINISH */
1105 /**
1106  * Get the wire color theme_id of an object based on it's state
1107  * \a r_color is a way to get a pointer to the static color var associated
1108  */
1109 int DRW_object_wire_theme_get(Object *ob, ViewLayer *view_layer, float **r_color)
1110 {
1111   const DRWContextState *draw_ctx = DRW_context_state_get();
1112   const bool is_edit = (draw_ctx->object_mode & OB_MODE_EDIT) && (ob->mode & OB_MODE_EDIT);
1113   const bool active = (view_layer->basact && view_layer->basact->object == ob);
1114   /* confusing logic here, there are 2 methods of setting the color
1115    * 'colortab[colindex]' and 'theme_id', colindex overrides theme_id.
1116    *
1117    * note: no theme yet for 'colindex' */
1118   int theme_id = is_edit ? TH_WIRE_EDIT : TH_WIRE;
1119
1120   if (is_edit) {
1121     /* fallback to TH_WIRE */
1122   }
1123   else if (((G.moving & G_TRANSFORM_OBJ) != 0) && ((ob->base_flag & BASE_SELECTED) != 0)) {
1124     theme_id = TH_TRANSFORM;
1125   }
1126   else {
1127     /* Sets the 'theme_id' or fallback to wire */
1128     if ((ob->base_flag & BASE_SELECTED) != 0) {
1129       theme_id = (active) ? TH_ACTIVE : TH_SELECT;
1130     }
1131     else {
1132       switch (ob->type) {
1133         case OB_LAMP:
1134           theme_id = TH_LIGHT;
1135           break;
1136         case OB_SPEAKER:
1137           theme_id = TH_SPEAKER;
1138           break;
1139         case OB_CAMERA:
1140           theme_id = TH_CAMERA;
1141           break;
1142         case OB_EMPTY:
1143           theme_id = TH_EMPTY;
1144           break;
1145         case OB_LIGHTPROBE:
1146           /* TODO add lightprobe color */
1147           theme_id = TH_EMPTY;
1148           break;
1149         default:
1150           /* fallback to TH_WIRE */
1151           break;
1152       }
1153     }
1154   }
1155
1156   if (r_color != NULL) {
1157     if (UNLIKELY(ob->base_flag & BASE_FROM_SET)) {
1158       *r_color = G_draw.block.colorDupli;
1159     }
1160     else if (UNLIKELY(ob->base_flag & BASE_FROM_DUPLI)) {
1161       switch (theme_id) {
1162         case TH_ACTIVE:
1163         case TH_SELECT:
1164           *r_color = G_draw.block.colorDupliSelect;
1165           break;
1166         case TH_TRANSFORM:
1167           *r_color = G_draw.block.colorTransform;
1168           break;
1169         default:
1170           *r_color = G_draw.block.colorDupli;
1171           break;
1172       }
1173     }
1174     else {
1175       switch (theme_id) {
1176         case TH_WIRE_EDIT:
1177           *r_color = G_draw.block.colorWireEdit;
1178           break;
1179         case TH_ACTIVE:
1180           *r_color = G_draw.block.colorActive;
1181           break;
1182         case TH_SELECT:
1183           *r_color = G_draw.block.colorSelect;
1184           break;
1185         case TH_TRANSFORM:
1186           *r_color = G_draw.block.colorTransform;
1187           break;
1188         case TH_SPEAKER:
1189           *r_color = G_draw.block.colorSpeaker;
1190           break;
1191         case TH_CAMERA:
1192           *r_color = G_draw.block.colorCamera;
1193           break;
1194         case TH_EMPTY:
1195           *r_color = G_draw.block.colorEmpty;
1196           break;
1197         case TH_LIGHT:
1198           *r_color = G_draw.block.colorLight;
1199           break;
1200         default:
1201           *r_color = G_draw.block.colorWire;
1202           break;
1203       }
1204     }
1205   }
1206
1207   return theme_id;
1208 }
1209
1210 /* XXX This is very stupid, better find something more general. */
1211 float *DRW_color_background_blend_get(int theme_id)
1212 {
1213   static float colors[11][4];
1214   float *ret;
1215
1216   switch (theme_id) {
1217     case TH_WIRE_EDIT:
1218       ret = colors[0];
1219       break;
1220     case TH_ACTIVE:
1221       ret = colors[1];
1222       break;
1223     case TH_SELECT:
1224       ret = colors[2];
1225       break;
1226     case TH_TRANSFORM:
1227       ret = colors[5];
1228       break;
1229     case TH_SPEAKER:
1230       ret = colors[6];
1231       break;
1232     case TH_CAMERA:
1233       ret = colors[7];
1234       break;
1235     case TH_EMPTY:
1236       ret = colors[8];
1237       break;
1238     case TH_LIGHT:
1239       ret = colors[9];
1240       break;
1241     default:
1242       ret = colors[10];
1243       break;
1244   }
1245
1246   UI_GetThemeColorBlendShade4fv(theme_id, TH_BACK, 0.5, 0, ret);
1247
1248   return ret;
1249 }
1250
1251 bool DRW_object_is_flat(Object *ob, int *r_axis)
1252 {
1253   float dim[3];
1254
1255   if (!ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL)) {
1256     /* Non-meshes object cannot be considered as flat. */
1257     return false;
1258   }
1259
1260   BKE_object_dimensions_get(ob, dim);
1261   if (dim[0] == 0.0f) {
1262     *r_axis = 0;
1263     return true;
1264   }
1265   else if (dim[1] == 0.0f) {
1266     *r_axis = 1;
1267     return true;
1268   }
1269   else if (dim[2] == 0.0f) {
1270     *r_axis = 2;
1271     return true;
1272   }
1273   return false;
1274 }
1275
1276 bool DRW_object_axis_orthogonal_to_view(Object *ob, int axis)
1277 {
1278   float ob_rot[3][3], invviewmat[4][4];
1279   DRW_view_viewmat_get(NULL, invviewmat, true);
1280   BKE_object_rot_to_mat3(ob, ob_rot, true);
1281   float dot = dot_v3v3(ob_rot[axis], invviewmat[2]);
1282   if (fabsf(dot) < 1e-3) {
1283     return true;
1284   }
1285
1286   return false;
1287 }
1288
1289 static void DRW_evaluate_weight_to_color(const float weight, float result[4])
1290 {
1291   if (U.flag & USER_CUSTOM_RANGE) {
1292     BKE_colorband_evaluate(&U.coba_weight, weight, result);
1293   }
1294   else {
1295     /* Use gamma correction to even out the color bands:
1296      * increasing widens yellow/cyan vs red/green/blue.
1297      * Gamma 1.0 produces the original 2.79 color ramp. */
1298     const float gamma = 1.5f;
1299     float hsv[3] = {(2.0f / 3.0f) * (1.0f - weight), 1.0f, pow(0.5f + 0.5f * weight, gamma)};
1300
1301     hsv_to_rgb_v(hsv, result);
1302
1303     for (int i = 0; i < 3; i++) {
1304       result[i] = pow(result[i], 1.0f / gamma);
1305     }
1306   }
1307 }
1308
1309 static GPUTexture *DRW_create_weight_colorramp_texture(void)
1310 {
1311   char error[256];
1312   float pixels[256][4];
1313   for (int i = 0; i < 256; i++) {
1314     DRW_evaluate_weight_to_color(i / 255.0f, pixels[i]);
1315     pixels[i][3] = 1.0f;
1316   }
1317
1318   return GPU_texture_create_1d(256, GPU_RGBA8, pixels[0], error);
1319 }