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