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