9b969c23ba6d2fa058ea1e88a34a2f0a5c87052c
[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 * (MAX2(ts.sizeVertex, 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 DRWShadingGroup *shgroup_dynlines_flat_color(DRWPass *pass)
302 {
303         GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_FLAT_COLOR);
304
305         DRW_shgroup_instance_format(g_formats.dynlines_color, {
306                 {"pos",       DRW_ATTRIB_FLOAT, 3},
307                 {"color",     DRW_ATTRIB_FLOAT, 4}
308         });
309
310         DRWShadingGroup *grp = DRW_shgroup_line_batch_create_with_format(sh, pass, g_formats.dynlines_color);
311
312         return grp;
313 }
314
315 DRWShadingGroup *shgroup_dynlines_dashed_uniform_color(DRWPass *pass, float color[4])
316 {
317         GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR);
318
319         static float dash_width = 6.0f;
320         static float dash_factor = 0.5f;
321         DRWShadingGroup *grp = DRW_shgroup_line_batch_create(sh, pass);
322         DRW_shgroup_uniform_vec4(grp, "color", color, 1);
323         DRW_shgroup_uniform_vec2(grp, "viewport_size", DRW_viewport_size_get(), 1);
324         DRW_shgroup_uniform_float(grp, "dash_width", &dash_width, 1);
325         DRW_shgroup_uniform_float(grp, "dash_factor", &dash_factor, 1);
326         DRW_shgroup_uniform_int_copy(grp, "colors_len", 0); /* "simple" mode */
327
328         return grp;
329 }
330
331 DRWShadingGroup *shgroup_dynpoints_uniform_color(DRWPass *pass, float color[4], float *size)
332 {
333         GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA);
334
335         DRWShadingGroup *grp = DRW_shgroup_point_batch_create(sh, pass);
336         DRW_shgroup_uniform_vec4(grp, "color", color, 1);
337         DRW_shgroup_uniform_float(grp, "size", size, 1);
338         DRW_shgroup_state_enable(grp, DRW_STATE_POINT);
339
340         return grp;
341 }
342
343 DRWShadingGroup *shgroup_groundlines_uniform_color(DRWPass *pass, float color[4])
344 {
345         GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_GROUNDLINE);
346
347         DRWShadingGroup *grp = DRW_shgroup_point_batch_create(sh, pass);
348         DRW_shgroup_uniform_vec4(grp, "color", color, 1);
349
350         return grp;
351 }
352
353 DRWShadingGroup *shgroup_groundpoints_uniform_color(DRWPass *pass, float color[4])
354 {
355         GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_GROUNDPOINT);
356
357         DRWShadingGroup *grp = DRW_shgroup_point_batch_create(sh, pass);
358         DRW_shgroup_uniform_vec4(grp, "color", color, 1);
359         DRW_shgroup_state_enable(grp, DRW_STATE_POINT);
360
361         return grp;
362 }
363
364 DRWShadingGroup *shgroup_instance_screenspace(DRWPass *pass, struct GPUBatch *geom, float *size)
365 {
366         GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_SCREENSPACE_VARIYING_COLOR);
367
368         DRW_shgroup_instance_format(g_formats.instance_screenspace, {
369                 {"world_pos", DRW_ATTRIB_FLOAT, 3},
370                 {"color",     DRW_ATTRIB_FLOAT, 3}
371         });
372
373         DRWShadingGroup *grp = DRW_shgroup_instance_create(sh, pass, geom, g_formats.instance_screenspace);
374         DRW_shgroup_uniform_float(grp, "size", size, 1);
375         DRW_shgroup_uniform_float(grp, "pixel_size", DRW_viewport_pixelsize_get(), 1);
376         DRW_shgroup_uniform_vec3(grp, "screen_vecs[0]", DRW_viewport_screenvecs_get(), 2);
377
378         return grp;
379 }
380
381 DRWShadingGroup *shgroup_instance_solid(DRWPass *pass, struct GPUBatch *geom)
382 {
383         static float light[3] = {0.0f, 0.0f, 1.0f};
384         GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_OBJECTSPACE_SIMPLE_LIGHTING_VARIYING_COLOR);
385
386         DRW_shgroup_instance_format(g_formats.instance_color, {
387                 {"InstanceModelMatrix", DRW_ATTRIB_FLOAT, 16},
388                 {"color",               DRW_ATTRIB_FLOAT, 4}
389         });
390
391         DRWShadingGroup *grp = DRW_shgroup_instance_create(sh, pass, geom, g_formats.instance_color);
392         DRW_shgroup_uniform_vec3(grp, "light", light, 1);
393
394         return grp;
395 }
396
397 DRWShadingGroup *shgroup_instance_wire(DRWPass *pass, struct GPUBatch *geom)
398 {
399         GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_OBJECTSPACE_VARIYING_COLOR);
400
401         DRW_shgroup_instance_format(g_formats.instance_color, {
402                 {"InstanceModelMatrix", DRW_ATTRIB_FLOAT, 16},
403                 {"color",               DRW_ATTRIB_FLOAT, 4}
404         });
405
406         DRWShadingGroup *grp = DRW_shgroup_instance_create(sh, pass, geom, g_formats.instance_color);
407
408         return grp;
409 }
410
411 DRWShadingGroup *shgroup_instance_screen_aligned(DRWPass *pass, struct GPUBatch *geom)
412 {
413         GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_INSTANCE_SCREEN_ALIGNED);
414
415         DRW_shgroup_instance_format(g_formats.instance_screen_aligned, {
416                 {"color",               DRW_ATTRIB_FLOAT, 3},
417                 {"size",                DRW_ATTRIB_FLOAT, 1},
418                 {"InstanceModelMatrix", DRW_ATTRIB_FLOAT, 16}
419         });
420
421         DRWShadingGroup *grp = DRW_shgroup_instance_create(sh, pass, geom, g_formats.instance_screen_aligned);
422         DRW_shgroup_uniform_vec3(grp, "screen_vecs[0]", DRW_viewport_screenvecs_get(), 2);
423
424         return grp;
425 }
426
427 DRWShadingGroup *shgroup_instance_scaled(DRWPass *pass, struct GPUBatch *geom)
428 {
429         GPUShader *sh_inst = GPU_shader_get_builtin_shader(GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SCALE);
430
431         DRW_shgroup_instance_format(g_formats.instance_scaled, {
432                 {"color",               DRW_ATTRIB_FLOAT, 3},
433                 {"size",                DRW_ATTRIB_FLOAT, 3},
434                 {"InstanceModelMatrix", DRW_ATTRIB_FLOAT, 16}
435         });
436
437         DRWShadingGroup *grp = DRW_shgroup_instance_create(sh_inst, pass, geom, g_formats.instance_scaled);
438
439         return grp;
440 }
441
442 DRWShadingGroup *shgroup_instance(DRWPass *pass, struct GPUBatch *geom)
443 {
444         GPUShader *sh_inst = GPU_shader_get_builtin_shader(GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SIZE);
445
446         DRW_shgroup_instance_format(g_formats.instance_sized, {
447                 {"color",               DRW_ATTRIB_FLOAT, 3},
448                 {"size",                DRW_ATTRIB_FLOAT, 1},
449                 {"InstanceModelMatrix", DRW_ATTRIB_FLOAT, 16}
450         });
451
452         DRWShadingGroup *grp = DRW_shgroup_instance_create(sh_inst, pass, geom, g_formats.instance_sized);
453         DRW_shgroup_uniform_float_copy(grp, "alpha", 1.0f);
454
455         return grp;
456 }
457
458 DRWShadingGroup *shgroup_instance_alpha(DRWPass *pass, struct GPUBatch *geom, float alpha)
459 {
460         GPUShader *sh_inst = GPU_shader_get_builtin_shader(GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SIZE);
461
462         DRW_shgroup_instance_format(g_formats.instance_sized, {
463                 {"color",               DRW_ATTRIB_FLOAT, 4},
464                 {"size",                DRW_ATTRIB_FLOAT, 1},
465                 {"InstanceModelMatrix", DRW_ATTRIB_FLOAT, 16}
466         });
467
468         DRWShadingGroup *grp = DRW_shgroup_instance_create(sh_inst, pass, geom, g_formats.instance_sized);
469         DRW_shgroup_uniform_float_copy(grp, "alpha", alpha);
470
471         return grp;
472 }
473
474 DRWShadingGroup *shgroup_instance_empty_axes(DRWPass *pass, struct GPUBatch *geom)
475 {
476         if (g_shaders.empty_axes_sh == NULL) {
477                 g_shaders.empty_axes_sh = DRW_shader_create(
478                         datatoc_object_empty_axes_vert_glsl, NULL,
479                         datatoc_gpu_shader_flat_color_frag_glsl, NULL);
480         }
481
482         DRW_shgroup_instance_format(g_formats.instance_sized, {
483                 {"color",               DRW_ATTRIB_FLOAT, 3},
484                 {"size",                DRW_ATTRIB_FLOAT, 1},
485                 {"InstanceModelMatrix", DRW_ATTRIB_FLOAT, 16}
486         });
487
488         DRWShadingGroup *grp = DRW_shgroup_instance_create(g_shaders.empty_axes_sh, pass, geom, g_formats.instance_sized);
489         DRW_shgroup_uniform_vec3(grp, "screenVecs[0]", DRW_viewport_screenvecs_get(), 2);
490
491         return grp;
492 }
493
494 DRWShadingGroup *shgroup_instance_outline(DRWPass *pass, struct GPUBatch *geom, int *baseid)
495 {
496         GPUShader *sh_inst = GPU_shader_get_builtin_shader(GPU_SHADER_INSTANCE_VARIYING_ID_VARIYING_SIZE);
497
498         DRW_shgroup_instance_format(g_formats.instance_outline, {
499                 {"callId",              DRW_ATTRIB_INT,   1},
500                 {"size",                DRW_ATTRIB_FLOAT, 1},
501                 {"InstanceModelMatrix", DRW_ATTRIB_FLOAT, 16}
502         });
503
504         DRWShadingGroup *grp = DRW_shgroup_instance_create(sh_inst, pass, geom, g_formats.instance_outline);
505         DRW_shgroup_uniform_int(grp, "baseId", baseid, 1);
506
507         return grp;
508 }
509
510 DRWShadingGroup *shgroup_camera_instance(DRWPass *pass, struct GPUBatch *geom)
511 {
512         GPUShader *sh_inst = GPU_shader_get_builtin_shader(GPU_SHADER_CAMERA);
513
514         DRW_shgroup_instance_format(g_formats.instance_camera, {
515                 {"color",               DRW_ATTRIB_FLOAT, 3},
516                 {"corners",             DRW_ATTRIB_FLOAT, 8},
517                 {"depth",               DRW_ATTRIB_FLOAT, 1},
518                 {"tria",                DRW_ATTRIB_FLOAT, 4},
519                 {"InstanceModelMatrix", DRW_ATTRIB_FLOAT, 16}
520         });
521
522         DRWShadingGroup *grp = DRW_shgroup_instance_create(sh_inst, pass, geom, g_formats.instance_camera);
523
524         return grp;
525 }
526
527 DRWShadingGroup *shgroup_distance_lines_instance(DRWPass *pass, struct GPUBatch *geom)
528 {
529         GPUShader *sh_inst = GPU_shader_get_builtin_shader(GPU_SHADER_DISTANCE_LINES);
530         static float point_size = 4.0f;
531
532         DRW_shgroup_instance_format(g_formats.instance_distance_lines, {
533                 {"color",               DRW_ATTRIB_FLOAT, 3},
534                 {"start",               DRW_ATTRIB_FLOAT, 1},
535                 {"end",                 DRW_ATTRIB_FLOAT, 1},
536                 {"InstanceModelMatrix", DRW_ATTRIB_FLOAT, 16}
537         });
538
539         DRWShadingGroup *grp = DRW_shgroup_instance_create(sh_inst, pass, geom, g_formats.instance_distance_lines);
540         DRW_shgroup_uniform_float(grp, "size", &point_size, 1);
541
542         return grp;
543 }
544
545 DRWShadingGroup *shgroup_spot_instance(DRWPass *pass, struct GPUBatch *geom)
546 {
547         GPUShader *sh_inst = GPU_shader_get_builtin_shader(GPU_SHADER_INSTANCE_EDGES_VARIYING_COLOR);
548         static const int True = true;
549         static const int False = false;
550
551         DRW_shgroup_instance_format(g_formats.instance_spot, {
552                 {"color",               DRW_ATTRIB_FLOAT, 3},
553                 {"InstanceModelMatrix", DRW_ATTRIB_FLOAT, 16}
554         });
555
556         DRWShadingGroup *grp = DRW_shgroup_instance_create(sh_inst, pass, geom, g_formats.instance_spot);
557         DRW_shgroup_uniform_bool(grp, "drawFront", &False, 1);
558         DRW_shgroup_uniform_bool(grp, "drawBack", &False, 1);
559         DRW_shgroup_uniform_bool(grp, "drawSilhouette", &True, 1);
560
561         return grp;
562 }
563
564 DRWShadingGroup *shgroup_instance_bone_axes(DRWPass *pass)
565 {
566         if (g_shaders.bone_axes == NULL) {
567                 g_shaders.bone_axes = DRW_shader_create(
568                             datatoc_armature_axes_vert_glsl, NULL,
569                             datatoc_gpu_shader_flat_color_frag_glsl, NULL);
570         }
571
572         DRW_shgroup_instance_format(g_formats.instance_color, {
573                 {"InstanceModelMatrix", DRW_ATTRIB_FLOAT, 16},
574                 {"color",               DRW_ATTRIB_FLOAT, 4}
575         });
576
577         DRWShadingGroup *grp = DRW_shgroup_instance_create(
578                 g_shaders.bone_axes,
579                 pass, DRW_cache_bone_arrows_get(),
580                 g_formats.instance_color);
581         DRW_shgroup_uniform_vec3(grp, "screenVecs[0]", DRW_viewport_screenvecs_get(), 2);
582
583         return grp;
584 }
585
586 DRWShadingGroup *shgroup_instance_bone_envelope_outline(DRWPass *pass)
587 {
588         if (g_shaders.bone_envelope_outline == NULL) {
589                 g_shaders.bone_envelope_outline = DRW_shader_create(
590                             datatoc_armature_envelope_outline_vert_glsl, NULL,
591                             datatoc_gpu_shader_flat_color_frag_glsl, NULL);
592         }
593
594         DRW_shgroup_instance_format(g_formats.instance_bone_envelope_outline, {
595                 {"headSphere",           DRW_ATTRIB_FLOAT, 4},
596                 {"tailSphere",           DRW_ATTRIB_FLOAT, 4},
597                 {"outlineColorSize",     DRW_ATTRIB_FLOAT, 4},
598                 {"xAxis",                DRW_ATTRIB_FLOAT, 3}
599         });
600
601         DRWShadingGroup *grp = DRW_shgroup_instance_create(
602                 g_shaders.bone_envelope_outline,
603                 pass, DRW_cache_bone_envelope_outline_get(),
604                 g_formats.instance_bone_envelope_outline);
605         DRW_shgroup_uniform_vec2(grp, "viewportSize", DRW_viewport_size_get(), 1);
606
607         return grp;
608 }
609
610 DRWShadingGroup *shgroup_instance_bone_envelope_distance(DRWPass *pass)
611 {
612         if (g_shaders.bone_envelope_distance == NULL) {
613                 g_shaders.bone_envelope_distance = DRW_shader_create(
614                             datatoc_armature_envelope_solid_vert_glsl, NULL,
615                             datatoc_armature_envelope_distance_frag_glsl, NULL);
616         }
617
618         DRW_shgroup_instance_format(g_formats.instance_bone_envelope_distance, {
619                 {"headSphere",           DRW_ATTRIB_FLOAT, 4},
620                 {"tailSphere",           DRW_ATTRIB_FLOAT, 4},
621                 {"xAxis",                DRW_ATTRIB_FLOAT, 3}
622         });
623
624         DRWShadingGroup *grp = DRW_shgroup_instance_create(
625                 g_shaders.bone_envelope_distance,
626                 pass, DRW_cache_bone_envelope_solid_get(),
627                 g_formats.instance_bone_envelope_distance);
628
629         return grp;
630 }
631
632 DRWShadingGroup *shgroup_instance_bone_envelope_solid(DRWPass *pass, bool transp)
633 {
634         if (g_shaders.bone_envelope == NULL) {
635                 g_shaders.bone_envelope = DRW_shader_create(
636                             datatoc_armature_envelope_solid_vert_glsl, NULL,
637                             datatoc_armature_envelope_solid_frag_glsl, "#define SMOOTH_ENVELOPE\n");
638         }
639
640         DRW_shgroup_instance_format(g_formats.instance_bone_envelope, {
641                 {"headSphere",           DRW_ATTRIB_FLOAT, 4},
642                 {"tailSphere",           DRW_ATTRIB_FLOAT, 4},
643                 {"boneColor",            DRW_ATTRIB_FLOAT, 3},
644                 {"stateColor",           DRW_ATTRIB_FLOAT, 3},
645                 {"xAxis",                DRW_ATTRIB_FLOAT, 3}
646         });
647
648         DRWShadingGroup *grp = DRW_shgroup_instance_create(
649                 g_shaders.bone_envelope,
650                 pass, DRW_cache_bone_envelope_solid_get(),
651                 g_formats.instance_bone_envelope);
652         DRW_shgroup_uniform_float_copy(grp, "alpha", transp ? 0.6f : 1.0f);
653
654         return grp;
655 }
656
657 DRWShadingGroup *shgroup_instance_mball_handles(DRWPass *pass)
658 {
659         if (g_shaders.mball_handles == NULL) {
660                 g_shaders.mball_handles = DRW_shader_create(
661                             datatoc_object_mball_handles_vert_glsl, NULL,
662                             datatoc_gpu_shader_flat_color_frag_glsl, NULL);
663         }
664
665         DRW_shgroup_instance_format(g_formats.instance_mball_handles, {
666                 {"ScaleTranslationMatrix",  DRW_ATTRIB_FLOAT, 12},
667                 {"radius",                  DRW_ATTRIB_FLOAT, 1},
668                 {"color",                   DRW_ATTRIB_FLOAT, 3}
669         });
670
671         DRWShadingGroup *grp = DRW_shgroup_instance_create(
672                 g_shaders.mball_handles, pass,
673                 DRW_cache_screenspace_circle_get(),
674                 g_formats.instance_mball_handles);
675         DRW_shgroup_uniform_vec3(grp, "screen_vecs[0]", DRW_viewport_screenvecs_get(), 2);
676
677         return grp;
678 }
679
680 /* Only works with batches with adjacency infos. */
681 DRWShadingGroup *shgroup_instance_bone_shape_outline(DRWPass *pass, struct GPUBatch *geom)
682 {
683         if (g_shaders.shape_outline == NULL) {
684                 g_shaders.shape_outline = DRW_shader_create(
685                             datatoc_armature_shape_outline_vert_glsl,
686                             datatoc_armature_shape_outline_geom_glsl,
687                             datatoc_gpu_shader_flat_color_frag_glsl,
688                             NULL);
689         }
690
691         DRW_shgroup_instance_format(g_formats.instance_bone_outline, {
692                 {"InstanceModelMatrix", DRW_ATTRIB_FLOAT, 16},
693                 {"outlineColorSize",    DRW_ATTRIB_FLOAT, 4}
694         });
695
696         DRWShadingGroup *grp = DRW_shgroup_instance_create(
697                 g_shaders.shape_outline,
698                 pass, geom, g_formats.instance_bone_outline);
699         DRW_shgroup_uniform_vec2(grp, "viewportSize", DRW_viewport_size_get(), 1);
700
701         return grp;
702 }
703
704 DRWShadingGroup *shgroup_instance_bone_shape_solid(DRWPass *pass, struct GPUBatch *geom, bool transp)
705 {
706         if (g_shaders.shape_solid == NULL) {
707                 g_shaders.shape_solid = DRW_shader_create(
708                             datatoc_armature_shape_solid_vert_glsl, NULL,
709                             datatoc_armature_shape_solid_frag_glsl, NULL);
710         }
711
712         DRW_shgroup_instance_format(g_formats.instance_bone, {
713                 {"InstanceModelMatrix", DRW_ATTRIB_FLOAT, 16},
714                 {"boneColor",            DRW_ATTRIB_FLOAT, 3},
715                 {"stateColor",           DRW_ATTRIB_FLOAT, 3}
716         });
717
718         DRWShadingGroup *grp = DRW_shgroup_instance_create(
719                 g_shaders.shape_solid,
720                 pass, geom, g_formats.instance_bone);
721         DRW_shgroup_uniform_float_copy(grp, "alpha", transp ? 0.6f : 1.0f);
722
723         return grp;
724 }
725
726 DRWShadingGroup *shgroup_instance_bone_sphere_solid(DRWPass *pass, bool transp)
727 {
728         if (g_shaders.bone_sphere == NULL) {
729                 g_shaders.bone_sphere = DRW_shader_create(
730                             datatoc_armature_sphere_solid_vert_glsl, NULL,
731                             datatoc_armature_sphere_solid_frag_glsl, NULL);
732         }
733
734         DRW_shgroup_instance_format(g_formats.instance_bone, {
735                 {"InstanceModelMatrix", DRW_ATTRIB_FLOAT, 16},
736                 {"boneColor",           DRW_ATTRIB_FLOAT, 3},
737                 {"stateColor",          DRW_ATTRIB_FLOAT, 3}
738         });
739
740         DRWShadingGroup *grp = DRW_shgroup_instance_create(
741                 g_shaders.bone_sphere,
742                 pass, DRW_cache_bone_point_get(), g_formats.instance_bone);
743         /* More transparent than the shape to be less distractive. */
744         DRW_shgroup_uniform_float_copy(grp, "alpha", transp ? 0.4f : 1.0f);
745
746         return grp;
747 }
748
749 DRWShadingGroup *shgroup_instance_bone_sphere_outline(DRWPass *pass)
750 {
751         if (g_shaders.bone_sphere_outline == NULL) {
752                 g_shaders.bone_sphere_outline = DRW_shader_create(
753                             datatoc_armature_sphere_outline_vert_glsl, NULL,
754                             datatoc_gpu_shader_flat_color_frag_glsl, NULL);
755         }
756
757         DRW_shgroup_instance_format(g_formats.instance_bone_outline, {
758                 {"InstanceModelMatrix", DRW_ATTRIB_FLOAT, 16},
759                 {"outlineColorSize",    DRW_ATTRIB_FLOAT, 4}
760         });
761
762         DRWShadingGroup *grp = DRW_shgroup_instance_create(
763                 g_shaders.bone_sphere_outline,
764                 pass, DRW_cache_bone_point_wire_outline_get(),
765                 g_formats.instance_bone_outline);
766         DRW_shgroup_uniform_vec2(grp, "viewportSize", DRW_viewport_size_get(), 1);
767
768         return grp;
769 }
770
771 DRWShadingGroup *shgroup_instance_bone_stick(DRWPass *pass)
772 {
773         if (g_shaders.bone_stick == NULL) {
774                 g_shaders.bone_stick = DRW_shader_create(
775                             datatoc_armature_stick_vert_glsl, NULL,
776                             datatoc_armature_stick_frag_glsl, NULL);
777         }
778
779         DRW_shgroup_instance_format(g_formats.instance_bone_stick, {
780                 {"boneStart", DRW_ATTRIB_FLOAT, 3},
781                 {"boneEnd",   DRW_ATTRIB_FLOAT, 3},
782                 {"wireColor", DRW_ATTRIB_FLOAT, 4}, /* TODO port theses to uchar color */
783                 {"boneColor", DRW_ATTRIB_FLOAT, 4},
784                 {"headColor", DRW_ATTRIB_FLOAT, 4},
785                 {"tailColor", DRW_ATTRIB_FLOAT, 4}
786         });
787
788         DRWShadingGroup *grp = DRW_shgroup_instance_create(
789                 g_shaders.bone_stick,
790                 pass, DRW_cache_bone_stick_get(),
791                 g_formats.instance_bone_stick);
792         DRW_shgroup_uniform_vec2(grp, "viewportSize", DRW_viewport_size_get(), 1);
793         DRW_shgroup_uniform_float_copy(grp, "stickSize", 5.0f * U.pixelsize);
794
795         return grp;
796 }
797
798 struct DRWShadingGroup *shgroup_instance_bone_dof(struct DRWPass *pass, struct GPUBatch *geom)
799 {
800         if (g_shaders.bone_dofs == NULL) {
801                 g_shaders.bone_dofs = DRW_shader_create(
802                             datatoc_armature_dof_vert_glsl, NULL,
803                             datatoc_gpu_shader_flat_color_frag_glsl, NULL);
804         }
805
806         DRW_shgroup_instance_format(g_formats.instance_bone_dof, {
807                 {"InstanceModelMatrix", DRW_ATTRIB_FLOAT, 16},
808                 {"color",               DRW_ATTRIB_FLOAT, 4},
809                 {"amin",                DRW_ATTRIB_FLOAT, 2},
810                 {"amax",                DRW_ATTRIB_FLOAT, 2},
811         });
812
813         DRWShadingGroup *grp = DRW_shgroup_instance_create(
814                 g_shaders.bone_dofs,
815                 pass, geom,
816                 g_formats.instance_bone_dof);
817
818         return grp;
819 }
820
821 struct GPUShader *mpath_line_shader_get(void)
822 {
823         if (g_shaders.mpath_line_sh == NULL) {
824                 g_shaders.mpath_line_sh = DRW_shader_create_with_lib(
825                         datatoc_animviz_mpath_lines_vert_glsl,
826                         datatoc_animviz_mpath_lines_geom_glsl,
827                         datatoc_gpu_shader_3D_smooth_color_frag_glsl,
828                         datatoc_common_globals_lib_glsl,
829                         NULL);
830         }
831         return g_shaders.mpath_line_sh;
832 }
833
834
835 struct GPUShader *mpath_points_shader_get(void)
836 {
837         if (g_shaders.mpath_points_sh == NULL) {
838                 g_shaders.mpath_points_sh = DRW_shader_create_with_lib(
839                         datatoc_animviz_mpath_points_vert_glsl,
840                         NULL,
841                         datatoc_gpu_shader_point_varying_color_frag_glsl,
842                         datatoc_common_globals_lib_glsl,
843                         NULL);
844         }
845         return g_shaders.mpath_points_sh;
846 }
847
848 struct GPUShader *volume_velocity_shader_get(bool use_needle)
849 {
850         if (use_needle) {
851                 if (g_shaders.volume_velocity_needle_sh == NULL) {
852                         g_shaders.volume_velocity_needle_sh = DRW_shader_create(
853                                 datatoc_volume_velocity_vert_glsl, NULL,
854                                 datatoc_gpu_shader_flat_color_frag_glsl, "#define USE_NEEDLE");
855                 }
856                 return g_shaders.volume_velocity_needle_sh;
857         }
858         else {
859                 if (g_shaders.volume_velocity_sh == NULL) {
860                         g_shaders.volume_velocity_sh = DRW_shader_create(
861                                 datatoc_volume_velocity_vert_glsl, NULL,
862                                 datatoc_gpu_shader_flat_color_frag_glsl, NULL);
863                 }
864                 return g_shaders.volume_velocity_sh;
865         }
866 }
867
868 /* ******************************************** COLOR UTILS *********************************************** */
869
870 /* TODO FINISH */
871 /**
872  * Get the wire color theme_id of an object based on it's state
873  * \a r_color is a way to get a pointer to the static color var associated
874  */
875 int DRW_object_wire_theme_get(Object *ob, ViewLayer *view_layer, float **r_color)
876 {
877         const DRWContextState *draw_ctx = DRW_context_state_get();
878         const bool is_edit = (draw_ctx->object_mode & OB_MODE_EDIT) && (ob->mode & OB_MODE_EDIT);
879         const bool active = (view_layer->basact && view_layer->basact->object == ob);
880         /* confusing logic here, there are 2 methods of setting the color
881          * 'colortab[colindex]' and 'theme_id', colindex overrides theme_id.
882          *
883          * note: no theme yet for 'colindex' */
884         int theme_id = is_edit ? TH_WIRE_EDIT : TH_WIRE;
885
886         if (is_edit) {
887                 /* fallback to TH_WIRE */
888         }
889         else if (((G.moving & G_TRANSFORM_OBJ) != 0) &&
890                  ((ob->base_flag & BASE_SELECTED) != 0))
891         {
892                 theme_id = TH_TRANSFORM;
893         }
894         else {
895                 /* Sets the 'theme_id' or fallback to wire */
896                 if ((ob->base_flag & BASE_SELECTED) != 0) {
897                         theme_id = (active) ? TH_ACTIVE : TH_SELECT;
898                 }
899                 else {
900                         if (ob->type == OB_LAMP) theme_id = TH_LAMP;
901                         else if (ob->type == OB_SPEAKER) theme_id = TH_SPEAKER;
902                         else if (ob->type == OB_CAMERA) theme_id = TH_CAMERA;
903                         else if (ob->type == OB_EMPTY) theme_id = TH_EMPTY;
904                         else if (ob->type == OB_LIGHTPROBE) theme_id = TH_EMPTY; /* TODO add lightprobe color */
905                         /* fallback to TH_WIRE */
906                 }
907         }
908
909         if (r_color != NULL) {
910                 if (UNLIKELY(ob->base_flag & BASE_FROM_SET)) {
911                         *r_color = ts.colorDupli;
912                 }
913                 else if (UNLIKELY(ob->base_flag & BASE_FROMDUPLI)) {
914                         switch (theme_id) {
915                                 case TH_ACTIVE:
916                                 case TH_SELECT:       *r_color = ts.colorDupliSelect; break;
917                                 case TH_TRANSFORM:    *r_color = ts.colorTransform; break;
918                                 default:              *r_color = ts.colorDupli; break;
919                         }
920                 }
921                 else {
922                         switch (theme_id) {
923                                 case TH_WIRE_EDIT:    *r_color = ts.colorWireEdit; break;
924                                 case TH_ACTIVE:       *r_color = ts.colorActive; break;
925                                 case TH_SELECT:       *r_color = ts.colorSelect; break;
926                                 case TH_TRANSFORM:    *r_color = ts.colorTransform; break;
927                                 case TH_SPEAKER:      *r_color = ts.colorSpeaker; break;
928                                 case TH_CAMERA:       *r_color = ts.colorCamera; break;
929                                 case TH_EMPTY:        *r_color = ts.colorEmpty; break;
930                                 case TH_LAMP:         *r_color = ts.colorLamp; break;
931                                 default:              *r_color = ts.colorWire; break;
932                         }
933                 }
934         }
935
936         return theme_id;
937 }
938
939 /* XXX This is very stupid, better find something more general. */
940 float *DRW_color_background_blend_get(int theme_id)
941 {
942         static float colors[11][4];
943         float *ret;
944
945         switch (theme_id) {
946                 case TH_WIRE_EDIT:    ret = colors[0]; break;
947                 case TH_ACTIVE:       ret = colors[1]; break;
948                 case TH_SELECT:       ret = colors[2]; break;
949                 case TH_TRANSFORM:    ret = colors[5]; break;
950                 case TH_SPEAKER:      ret = colors[6]; break;
951                 case TH_CAMERA:       ret = colors[7]; break;
952                 case TH_EMPTY:        ret = colors[8]; break;
953                 case TH_LAMP:         ret = colors[9]; break;
954                 default:              ret = colors[10]; break;
955         }
956
957         UI_GetThemeColorBlendShade4fv(theme_id, TH_BACK, 0.5, 0, ret);
958
959         return ret;
960 }
961
962
963 bool DRW_object_is_flat(Object *ob, int *axis)
964 {
965         float dim[3];
966
967         if (!ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL)) {
968                 /* Non-meshes object cannot be considered as flat. */
969                 return false;
970         }
971
972         BKE_object_dimensions_get(ob, dim);
973         if (dim[0] == 0.0f) {
974                 *axis = 0;
975                 return true;
976         }
977         else if (dim[1] == 0.0f) {
978                 *axis = 1;
979                 return true;
980         }
981         else if (dim[2] == 0.0f) {
982                 *axis = 2;
983                 return true;
984         }
985         return false;
986 }
987
988 bool DRW_object_axis_orthogonal_to_view(Object *ob, int axis)
989 {
990         float ob_rot[3][3], invviewmat[4][4];
991         DRW_viewport_matrix_get(invviewmat, DRW_MAT_VIEWINV);
992         BKE_object_rot_to_mat3(ob, ob_rot, true);
993         float dot = dot_v3v3(ob_rot[axis], invviewmat[2]);
994         if (fabsf(dot) < 1e-3) {
995                 return true;
996         }
997
998         return false;
999 }
1000
1001 static void DRW_evaluate_weight_to_color(const float weight, float result[4])
1002 {
1003         if (U.flag & USER_CUSTOM_RANGE) {
1004                 BKE_colorband_evaluate(&U.coba_weight, weight, result);
1005         }
1006         else {
1007                 /* Use gamma correction to even out the color bands:
1008                  * increasing widens yellow/cyan vs red/green/blue.
1009                  * Gamma 1.0 produces the original 2.79 color ramp. */
1010                 const float gamma = 1.5f;
1011                 float hsv[3] = {(2.0f / 3.0f) * (1.0f - weight), 1.0f, pow(0.5f + 0.5f * weight, gamma)};
1012
1013                 hsv_to_rgb_v(hsv, result);
1014
1015                 for (int i = 0; i < 3; i++) {
1016                         result[i] = pow(result[i], 1.0f / gamma);
1017                 }
1018         }
1019 }
1020
1021 static GPUTexture *DRW_create_weight_colorramp_texture(void)
1022 {
1023         char error[256];
1024         float pixels[256][4];
1025         for (int i = 0 ; i < 256 ; i ++) {
1026                 DRW_evaluate_weight_to_color(i / 255.0f, pixels[i]);
1027                 pixels[i][3] = 1.0f;
1028         }
1029
1030         return GPU_texture_create_1D(256, GPU_RGBA8, pixels[0], error);
1031 }