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