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