Blender V5.3
overlay_private.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "BKE_context.hh"
12#include "BKE_movieclip.hh"
13#include "BKE_object.hh"
14
15#include "BLI_function_ref.hh"
16
17#include "DNA_space_types.h"
18#include "DNA_workspace_types.h"
19#include "DNA_world_types.h"
20
21#include "GPU_matrix.hh"
22
23#include "DRW_gpu_wrapper.hh"
24#include "DRW_render.hh"
25#include "UI_resources.hh"
26#include "draw_manager.hh"
27#include "draw_pass.hh"
28#include "draw_view_data.hh"
30
33
34#include "draw_common.hh"
35
36namespace blender {
37
38template<> struct gpu::AttrType<VertexClass> {
39 static constexpr VertAttrType type = VertAttrType::SINT_32;
40};
41template<> struct gpu::AttrType<StickBoneFlag> {
42 static constexpr VertAttrType type = VertAttrType::SINT_32;
43};
44
45namespace draw::overlay {
46
48 /* Keep sync with bone instance vertex format (OVERLAY_InstanceFormats) */
49 union {
51 float mat[4][4];
52 struct {
53 float _pad0[3], color_hint_a;
54 float _pad1[3], color_hint_b;
55 float _pad2[3], color_a;
56 float _pad3[3], color_b;
57 };
58 struct {
59 float _pad00[3], amin_a;
60 float _pad01[3], amin_b;
61 float _pad02[3], amax_a;
62 float _pad03[3], amax_b;
63 };
64 };
65
66 BoneInstanceData() = default;
67
73 const float3 &pos,
74 const float radius,
75 const float color[4])
76
77 {
78 mat44[0] = ob_mat[0] * radius;
79 mat44[1] = ob_mat[1] * radius;
80 mat44[2] = ob_mat[2] * radius;
81 mat44[3] = float4(math::transform_point(ob_mat, pos), 0.0f);
83 }
84
85 BoneInstanceData(const float4x4 &bone_mat, const float4 &bone_color, const float4 &hint_color)
86 : mat44(bone_mat)
87 {
88 set_color(bone_color);
89 set_hint_color(hint_color);
90 };
91
92 BoneInstanceData(const float4x4 &bone_mat, const float4 &bone_color) : mat44(bone_mat)
93 {
94 set_color(bone_color);
95 };
96
97 void set_color(const float4 &bone_color)
98 {
99 /* Encoded color into 2 floats to be able to use the matrix to color the custom bones. */
100 color_a = encode_2f_to_float(bone_color[0], bone_color[1]);
101 color_b = encode_2f_to_float(bone_color[2], bone_color[3]);
102 }
103
104 void set_hint_color(const float4 &hint_color)
105 {
106 /* Encoded color into 2 floats to be able to use the matrix to color the custom bones. */
107 color_hint_a = encode_2f_to_float(hint_color[0], hint_color[1]);
108 color_hint_b = encode_2f_to_float(hint_color[2], hint_color[3]);
109 }
110
111 private:
112 /* Encode 2 units float with byte precision into a float. */
113 float encode_2f_to_float(float a, float b) const
114 {
115 /* NOTE: `b` can go up to 2. Needed to encode wire size. */
116 return float(int(clamp_f(a, 0.0f, 1.0f) * 255) | (int(clamp_f(b, 0.0f, 2.0f) * 255) << 8));
117 }
118};
119
121
124using draw::Texture;
126using draw::TextureRef;
127
128struct State {
129 Depsgraph *depsgraph = nullptr;
130 const ViewLayer *view_layer = nullptr;
131 const Scene *scene = nullptr;
132 const View3D *v3d = nullptr;
133 const SpaceLink *space_data = nullptr;
134 const ARegion *region = nullptr;
135 const RegionView3D *rv3d = nullptr;
136 const bToolRef *active_tool = nullptr;
137 DRWTextStore *dt = nullptr;
142 const Object *object_active = nullptr;
143 bool clear_in_front = false;
144 bool use_in_front = false;
145 bool is_wireframe_mode = false;
149 bool is_image_render = false;
153 bool skip_particles = false;
155 bool is_material_select = false;
157 bool draw_background = false;
161 bool vignette_enabled = false;
163 bool show_text = false;
164 bool hide_overlays = false;
165 bool xray_enabled = false;
168 bool xray_flag_enabled = false;
170 bool do_pose_xray = false;
172 bool do_pose_fade_geom = false;
173 float xray_opacity = 0.0f;
174 short v3d_flag = 0; /* TODO: move to #View3DOverlay. */
175 short v3d_gridflag = 0; /* TODO: move to #View3DOverlay. */
176 int cfra = 0;
180
182 bool is_image_valid = false;
186
188 {
189 if (rv3d == nullptr) {
190 return View::OffsetData();
191 }
192 return View::OffsetData(*rv3d);
193 }
194
195 /* Factor to use for wireframe offset.
196 * Result of GPU_polygon_offset_calc for the current view.
197 * Only valid at draw time, so use push constant reference instead of copy. */
198 float ndc_offset_factor = 0.0f;
199
201
203 bool is_solid() const
204 {
205 return xray_opacity == 1.0f;
206 }
207
208 bool is_xray() const
209 {
210 return (xray_opacity < 1.0f) && (xray_opacity > 0.0f);
211 }
212
213 bool is_wire() const
214 {
215 return xray_opacity == 0.0f;
216 }
217
218 bool is_space_v3d() const
219 {
220 return this->space_type == SPACE_VIEW3D;
221 }
222 bool is_space_image() const
223 {
224 return this->space_type == SPACE_IMAGE;
225 }
226 bool is_space_node() const
227 {
228 return this->space_type == SPACE_NODE;
229 }
230
231 bool show_extras() const
232 {
233 return (this->overlay.flag & V3D_OVERLAY_HIDE_OBJECT_XTRAS) == 0;
234 }
236 {
237 return (this->overlay.flag & V3D_OVERLAY_FACE_ORIENTATION);
238 }
240 {
241 return (this->overlay.flag & V3D_OVERLAY_BONE_SELECT);
242 }
243 bool show_wireframes() const
244 {
245 return (this->overlay.flag & V3D_OVERLAY_WIREFRAMES);
246 }
247 bool show_motion_paths() const
248 {
249 return (this->overlay.flag & V3D_OVERLAY_HIDE_MOTION_PATHS) == 0;
250 }
251 bool show_bones() const
252 {
253 return (this->overlay.flag & V3D_OVERLAY_HIDE_BONES) == 0;
254 }
256 {
257 return (this->overlay.flag & V3D_OVERLAY_HIDE_OBJECT_ORIGINS) == 0;
258 }
260 {
261 return (this->overlay.flag & V3D_OVERLAY_FADE_INACTIVE);
262 }
264 {
265 return (this->overlay.flag & V3D_OVERLAY_VIEWER_ATTRIBUTE);
266 }
268 {
269 return (this->overlay.flag & V3D_OVERLAY_VIEWER_ATTRIBUTE_TEXT);
270 }
271 bool show_sculpt_mask() const
272 {
273 return (this->overlay.flag & V3D_OVERLAY_SCULPT_SHOW_MASK);
274 }
276 {
277 return (this->overlay.flag & V3D_OVERLAY_SCULPT_SHOW_FACE_SETS);
278 }
280 {
281 return (this->overlay.flag & V3D_OVERLAY_SCULPT_CURVES_CAGE);
282 }
283 bool show_light_colors() const
284 {
285 return (this->overlay.flag & V3D_OVERLAY_SHOW_LIGHT_COLORS);
286 }
287};
288
289/* Matches Vertex Format. */
296
303
310
318
319/* TODO(fclem): Might be good to remove for simplicity. */
327
332 private:
333 struct BatchDeleter {
334 void operator()(gpu::Batch *shader)
335 {
337 }
338 };
339 using BatchPtr = std::unique_ptr<gpu::Batch, BatchDeleter>;
340
341 public:
342 BatchPtr bone_box;
348 BatchPtr bone_sphere;
350 BatchPtr bone_stick;
351
354
355 BatchPtr grid;
356 BatchPtr cube_solid;
357
359 BatchPtr cursor_lines;
360
361 BatchPtr quad_wire;
362 BatchPtr quad_solid;
363 BatchPtr plain_axes;
364 BatchPtr single_arrow;
365 BatchPtr cube;
366 BatchPtr circle;
367 BatchPtr empty_sphere;
368 BatchPtr empty_cone;
369 BatchPtr cylinder;
370 BatchPtr capsule_body;
371 BatchPtr capsule_cap;
372 BatchPtr arrows;
374
375 BatchPtr speaker;
376
378 BatchPtr camera_frame;
380 BatchPtr camera_tria;
381
384
386
387 BatchPtr ground_line;
388
389 /* Batch drawing a quad with coordinate [0..1] at 0.75 depth. */
390 BatchPtr image_quad;
391
401
402 BatchPtr field_force;
403 BatchPtr field_wind;
404 BatchPtr field_vortex;
405 BatchPtr field_curve;
409
413
414 ShapeCache();
415
416 private:
417 /* Caller gets ownership of the #gpu::VertBuf. */
418 template<typename T> gpu::VertBuf *vbo_from_vector(const Vector<T> &vector)
419 {
420 gpu::VertBuf *vbo = GPU_vertbuf_create_with_format(T::format());
421 GPU_vertbuf_data_alloc(*vbo, vector.size());
422 vbo->data<T>().copy_from(vector);
423 return vbo;
424 }
425};
426
428
432class ShaderModule {
433 private:
434 /* Allow StaticShaderCache access to the constructor. */
436
437 using StaticCache =
438 gpu::StaticShaderCache<ShaderModule>[2 /* Selection Instance. */][2 /* Clipping Enabled. */];
439
440 static StaticCache &get_static_cache()
441 {
443 static StaticCache static_cache;
444 return static_cache;
445 }
446
447 const SelectionType selection_type_;
452 const bool clipping_enabled_;
453
454 public:
456 StaticShader anti_aliasing = {"overlay_antialiasing_pipeline"};
457 StaticShader armature_degrees_of_freedom = shader_clippable("overlay_armature_dof");
458 StaticShader attribute_viewer_mesh = shader_clippable("overlay_viewer_attribute_mesh");
460 "overlay_viewer_attribute_pointcloud");
461 StaticShader attribute_viewer_gsplat = shader_clippable("overlay_viewer_attribute_gsplat");
462 StaticShader attribute_viewer_curve = shader_clippable("overlay_viewer_attribute_curve");
463 StaticShader attribute_viewer_curves = shader_clippable("overlay_viewer_attribute_curves");
464 StaticShader background_fill = {"overlay_background"};
465 StaticShader background_clip_bound = {"overlay_clipbound"};
466 StaticShader curve_edit_points = shader_clippable("overlay_edit_curves_point");
467 StaticShader curve_edit_line = shader_clippable("overlay_edit_particle_strand");
468 StaticShader curve_edit_handles = shader_clippable("overlay_edit_curves_handle");
469 StaticShader facing = shader_clippable("overlay_facing");
470 StaticShader grid = {"overlay_grid_next"};
471 StaticShader grid_background = {"overlay_grid_background"};
472 StaticShader grid_grease_pencil = shader_clippable("overlay_gpencil_canvas");
473 StaticShader grid_image = {"overlay_grid_image"};
474 StaticShader lattice_points = shader_clippable("overlay_edit_lattice_point");
475 StaticShader lattice_wire = shader_clippable("overlay_edit_lattice_wire");
476 StaticShader legacy_curve_edit_handles = shader_clippable("overlay_edit_curve_handle");
477 StaticShader legacy_curve_edit_normals = shader_clippable("overlay_edit_curve_normals");
478 StaticShader legacy_curve_edit_points = shader_clippable("overlay_edit_curve_point");
479 StaticShader legacy_curve_edit_wires = shader_clippable("overlay_edit_curve_wire");
480 StaticShader light_spot_cone = shader_clippable("overlay_extra_spot_cone");
481 StaticShader mesh_analysis = shader_clippable("overlay_edit_mesh_analysis");
482 StaticShader mesh_edit_depth = shader_clippable("overlay_edit_mesh_depth");
483 StaticShader mesh_edit_edge = shader_clippable("overlay_edit_mesh_edge");
484 StaticShader mesh_edit_face = shader_clippable("overlay_edit_mesh_face");
485 StaticShader mesh_edit_facedot = shader_clippable("overlay_edit_mesh_facedot");
486 StaticShader mesh_edit_vert = shader_clippable("overlay_edit_mesh_vert");
487 StaticShader mesh_edit_skin_root = shader_clippable("overlay_edit_mesh_skin_root");
488 StaticShader mesh_face_normal = shader_clippable("overlay_mesh_face_normal");
489 StaticShader mesh_face_normal_subdiv = shader_clippable("overlay_mesh_face_normal_subdiv");
490 StaticShader mesh_loop_normal = shader_clippable("overlay_mesh_loop_normal");
491 StaticShader mesh_loop_normal_subdiv = shader_clippable("overlay_mesh_loop_normal_subdiv");
492 StaticShader mesh_vert_normal = shader_clippable("overlay_mesh_vert_normal");
493 StaticShader mesh_vert_normal_subdiv = shader_clippable("overlay_mesh_vert_normal_subdiv");
494 StaticShader motion_path_line = shader_clippable("overlay_motion_path_line");
495 StaticShader motion_path_vert = shader_clippable("overlay_motion_path_point");
496 StaticShader outline_detect = {"overlay_outline_detect"};
497 StaticShader outline_prepass_curves = shader_clippable("overlay_outline_prepass_curves");
498 StaticShader outline_prepass_gpencil = shader_clippable("overlay_outline_prepass_gpencil");
499 StaticShader outline_prepass_gsplat = shader_clippable("overlay_outline_prepass_gsplat");
500 StaticShader outline_prepass_mesh = shader_clippable("overlay_outline_prepass_mesh");
501 StaticShader outline_prepass_pointcloud = shader_clippable("overlay_outline_prepass_pointcloud");
502 StaticShader outline_prepass_wire = shader_clippable("overlay_outline_prepass_wire");
503 StaticShader paint_region_edge = shader_clippable("overlay_paint_wire");
504 StaticShader paint_region_face = shader_clippable("overlay_paint_face");
505 StaticShader paint_region_vert = shader_clippable("overlay_paint_point");
506 StaticShader paint_texture = shader_clippable("overlay_paint_texture");
507 StaticShader paint_weight = shader_clippable("overlay_paint_weight");
508 /* TODO(fclem): Specialization constant. */
509 StaticShader paint_weight_fake_shading = shader_clippable("overlay_paint_weight_fake_shading");
510 StaticShader particle_edit_vert = shader_clippable("overlay_edit_particle_point");
511 StaticShader particle_edit_edge = shader_clippable("overlay_edit_particle_strand");
512 StaticShader gsplat_points = shader_clippable("overlay_edit_gsplat");
513 StaticShader pointcloud_points = shader_clippable("overlay_edit_pointcloud");
514 StaticShader sculpt_curves = shader_clippable("overlay_sculpt_curves_selection");
515 StaticShader sculpt_curves_cage = shader_clippable("overlay_sculpt_curves_cage");
516 StaticShader sculpt_mesh = shader_clippable("overlay_sculpt_mask");
517 StaticShader uniform_color = shader_clippable("overlay_uniform_color");
518 StaticShader uv_analysis_stretch_angle = {"overlay_edit_uv_stretching_angle"};
519 StaticShader uv_analysis_stretch_area = {"overlay_edit_uv_stretching_area"};
520 StaticShader uv_brush_stencil = {"overlay_edit_uv_stencil_image"};
521 StaticShader uv_edit_edge = {"overlay_edit_uv_edges"};
522 StaticShader uv_edit_face = {"overlay_edit_uv_faces"};
523 StaticShader uv_edit_facedot = {"overlay_edit_uv_face_dots"};
524 StaticShader uv_edit_vert = {"overlay_edit_uv_verts"};
525 StaticShader uv_image_borders = {"overlay_edit_uv_tiled_image_borders"};
526 StaticShader uv_paint_mask = {"overlay_edit_uv_mask_image"};
527 StaticShader uv_wireframe = {"overlay_wireframe_uv"};
528 StaticShader xray_fade = {"overlay_xray_fade_pipeline"};
529
531 StaticShader armature_envelope_fill = shader_selectable("overlay_armature_envelope_solid");
532 StaticShader armature_envelope_outline = shader_selectable("overlay_armature_envelope_outline");
533 StaticShader armature_shape_outline = shader_selectable("overlay_armature_shape_outline");
534 StaticShader armature_shape_fill = shader_selectable("overlay_armature_shape_solid");
535 StaticShader armature_shape_wire = shader_selectable("overlay_armature_shape_wire");
536 StaticShader armature_shape_wire_strip = shader_selectable("overlay_armature_shape_wire_strip");
537 StaticShader armature_sphere_outline = shader_selectable("overlay_armature_sphere_outline");
538 StaticShader armature_sphere_fill = shader_selectable("overlay_armature_sphere_solid");
539 StaticShader armature_stick = shader_selectable("overlay_armature_stick");
540 StaticShader armature_wire = shader_selectable("overlay_armature_wire");
541 StaticShader depth_curves = shader_selectable("overlay_depth_curves");
542 StaticShader depth_grease_pencil = shader_selectable("overlay_depth_gpencil");
543 StaticShader depth_mesh = shader_selectable("overlay_depth_mesh");
544 StaticShader depth_mesh_conservative = shader_selectable("overlay_depth_mesh_conservative");
545 StaticShader depth_pointcloud = shader_selectable("overlay_depth_pointcloud");
546 StaticShader depth_gsplat = shader_selectable("overlay_depth_gsplat");
547 StaticShader extra_shape = shader_selectable("overlay_extra");
548 StaticShader extra_point = shader_selectable("overlay_extra_point");
549 StaticShader extra_wire = shader_selectable("overlay_extra_wire");
550 StaticShader extra_wire_object = shader_selectable("overlay_extra_wire_object");
551 StaticShader extra_loose_points = shader_selectable("overlay_extra_loose_point");
552 StaticShader extra_grid = shader_selectable("overlay_extra_grid");
553 StaticShader extra_ground_line = shader_selectable("overlay_extra_groundline");
554 StaticShader image_plane = shader_selectable("overlay_image");
555 StaticShader image_plane_depth_bias = shader_selectable("overlay_image_depth_bias");
556 StaticShader particle_dot = shader_selectable("overlay_particle_dot");
557 StaticShader particle_shape = shader_selectable("overlay_particle_shape");
558 StaticShader particle_hair = shader_selectable("overlay_particle_hair");
559 StaticShader wireframe_mesh = shader_selectable("overlay_wireframe");
560 /* Draw objects without edges for the wireframe overlay. */
561 StaticShader wireframe_points = shader_selectable("overlay_wireframe_points");
563 "overlay_wireframe_points_with_radius");
564 StaticShader wireframe_curve = shader_selectable("overlay_wireframe_curve");
565 StaticShader wireframe_gsplat = shader_selectable("overlay_wireframe_gsplat");
566
567 StaticShader fluid_grid_lines_flags = shader_selectable_no_clip(
568 "overlay_volume_gridlines_flags");
569 StaticShader fluid_grid_lines_flat = shader_selectable_no_clip("overlay_volume_gridlines_flat");
570 StaticShader fluid_grid_lines_range = shader_selectable_no_clip(
571 "overlay_volume_gridlines_range");
572 StaticShader fluid_velocity_streamline = shader_selectable_no_clip(
573 "overlay_volume_velocity_streamline");
574 StaticShader fluid_velocity_mac = shader_selectable_no_clip("overlay_volume_velocity_mac");
575 StaticShader fluid_velocity_needle = shader_selectable_no_clip("overlay_volume_velocity_needle");
576
579 static ShaderModule &module_get(SelectionType selection_type, bool clipping_enabled);
580 static void module_free();
581
582 private:
583 ShaderModule(const SelectionType selection_type, const bool clipping_enabled)
584 : selection_type_(selection_type), clipping_enabled_(clipping_enabled) {};
585
586 StaticShader shader_clippable(const char *create_info_name);
587 StaticShader shader_selectable(const char *create_info_name);
588 StaticShader shader_selectable_no_clip(const char *create_info_name);
589};
590
592 /* Plane data to reference as push constant.
593 * Will be computed just before drawing. */
595 /* Center and size of the bounding box of the Grease Pencil object. */
597 /* Grease-pencil object resource handle. */
599};
600
603
604 /* Overlay Color. */
605 Framebuffer overlay_color_only_fb = {"overlay_color_only_fb"};
606 /* Overlay Color, Line Data. */
607 Framebuffer overlay_line_only_fb = {"overlay_line_only_fb"};
608 /* Depth, Overlay Color. */
609 Framebuffer overlay_fb = {"overlay_fb"};
610 /* Depth, Overlay Color, Line Data. */
611 Framebuffer overlay_line_fb = {"overlay_line_fb"};
612 /* Depth In-Front, Overlay Color. */
613 Framebuffer overlay_in_front_fb = {"overlay_in_front_fb"};
614 /* Depth In-Front, Overlay Color, Line Data. */
615 Framebuffer overlay_line_in_front_fb = {"overlay_line_in_front_fb"};
616
617 /* Output Color. */
618 Framebuffer overlay_output_color_only_fb = {"overlay_output_color_only_fb"};
619 /* Depth, Output Color. */
620 Framebuffer overlay_output_fb = {"overlay_output_fb"};
621
622 /* Render Frame-buffers. Only used for multiplicative blending on top of the render. */
623 /* TODO(fclem): Remove the usage of these somehow. This is against design. */
626
627 /* Target containing line direction and data for line expansion and anti-aliasing. */
629 /* Target containing overlay color before anti-aliasing. */
630 TextureFromPool overlay_tx = {"overlay_tx"};
631 /* Target containing depth of overlays when xray is enabled. */
632 TextureFromPool xray_depth_tx = {"xray_depth_tx"};
633 TextureFromPool xray_depth_in_front_tx = {"xray_depth_in_front_tx"};
634
635 /* Texture that are usually allocated inside. These are fallback when they aren't.
636 * They are then wrapped inside the #TextureRefs below. */
637 TextureFromPool depth_in_front_alloc_tx = {"overlay_depth_in_front_tx"};
638 TextureFromPool color_overlay_alloc_tx = {"overlay_color_overlay_alloc_tx"};
639 TextureFromPool color_render_alloc_tx = {"overlay_color_render_alloc_tx"};
640
641 /* 1px texture containing only maximum depth. To be used for fulfilling bindings when depth
642 * texture is not available or not needed. */
643 Texture dummy_depth_tx = {"dummy_depth_tx"};
644
645 /* Global vector for all grease pencil depth planes.
646 * Managed by the grease pencil overlay module.
647 * This is to avoid passing the grease pencil overlay class to other overlay and
648 * keep draw_grease_pencil as a static function.
649 * Memory is reference, so we have to use a container with fixed memory. */
652
656 /* Wrappers around #DefaultTextureList members. */
675
677 bool weight_ramp_custom = false;
680 Texture weight_ramp_tx = {"weight_ramp"};
681
683
685
686 Resources(const SelectionType selection_type_, const ShapeCache &shapes_)
687 : select::SelectMap(selection_type_), shapes(shapes_) {};
688
693
694 void update_theme_settings(const DRWContext *ctx, const State &state);
695 void update_clip_planes(const State &state);
696
697 void init(bool clipping_enabled)
698 {
774 }
775
777 {
778 SelectMap::begin_sync(clipping_plane_count);
780 }
781
782 void acquire(const DRWContext *draw_ctx, const State &state)
783 {
784 DefaultTextureList &viewport_textures = *draw_ctx->viewport_texture_list_get();
785 DefaultFramebufferList &viewport_framebuffers = *draw_ctx->viewport_framebuffer_list_get();
786 this->depth_tx.wrap(viewport_textures.depth);
787 this->depth_in_front_tx.wrap(viewport_textures.depth_in_front);
788 this->color_overlay_tx.wrap(viewport_textures.color_overlay);
789 this->color_render_tx.wrap(viewport_textures.color);
790
791 this->render_fb = viewport_framebuffers.default_fb;
792 this->render_in_front_fb = viewport_framebuffers.in_front_fb;
793
794 int2 render_size = int2(this->depth_tx.size());
795
796 if (state.xray_enabled) {
797 /* For X-ray we render the scene to a separate depth buffer. */
798 this->xray_depth_tx.acquire_2d(render_size, gpu::TextureFormat::SFLOAT_32_DEPTH_UINT_8);
799 this->depth_target_tx.wrap(this->xray_depth_tx);
800 /* TODO(fclem): Remove mandatory allocation. */
801 this->xray_depth_in_front_tx.acquire_2d(render_size,
802 gpu::TextureFormat::SFLOAT_32_DEPTH_UINT_8);
803 this->depth_target_in_front_tx.wrap(this->xray_depth_in_front_tx);
804 }
805 else {
806 /* TODO(fclem): Remove mandatory allocation. */
807 if (!this->depth_in_front_tx.is_valid()) {
808 this->depth_in_front_alloc_tx.acquire_2d(render_size,
809 gpu::TextureFormat::SFLOAT_32_DEPTH_UINT_8);
810 this->depth_in_front_tx.wrap(this->depth_in_front_alloc_tx);
811 }
812 this->depth_target_tx.wrap(this->depth_tx);
813 this->depth_target_in_front_tx.wrap(this->depth_in_front_tx);
814 }
815
816 /* TODO: Better semantics using a switch? */
817 if (!this->color_overlay_tx.is_valid()) {
818 /* Likely to be the selection case. Allocate dummy texture and bind only depth buffer. */
819 this->color_overlay_alloc_tx.acquire_2d(int2(1, 1), gpu::TextureFormat::SRGBA_8_8_8_8);
820 this->color_render_alloc_tx.acquire_2d(int2(1, 1), gpu::TextureFormat::SRGBA_8_8_8_8);
821
822 this->color_overlay_tx.wrap(this->color_overlay_alloc_tx);
823 this->color_render_tx.wrap(this->color_render_alloc_tx);
824
825 this->line_tx.acquire_2d(int2(1, 1), gpu::TextureFormat::UNORM_8_8);
826 this->overlay_tx.acquire_2d(int2(1, 1), gpu::TextureFormat::SRGBA_8_8_8_8);
827
828 this->overlay_fb.ensure(GPU_ATTACHMENT_TEXTURE(this->depth_target_tx));
829 this->overlay_line_fb.ensure(GPU_ATTACHMENT_TEXTURE(this->depth_target_tx));
830 this->overlay_in_front_fb.ensure(GPU_ATTACHMENT_TEXTURE(this->depth_target_tx));
831 this->overlay_line_in_front_fb.ensure(GPU_ATTACHMENT_TEXTURE(this->depth_target_tx));
832 }
833 else {
836 this->line_tx.acquire_2d(render_size, gpu::TextureFormat::UNORM_8_8, usage);
837 this->overlay_tx.acquire_2d(render_size, gpu::TextureFormat::SRGBA_8_8_8_8, usage);
838
839 this->overlay_fb.ensure(GPU_ATTACHMENT_TEXTURE(this->depth_target_tx),
840 GPU_ATTACHMENT_TEXTURE(this->overlay_tx));
841 this->overlay_line_fb.ensure(GPU_ATTACHMENT_TEXTURE(this->depth_target_tx),
842 GPU_ATTACHMENT_TEXTURE(this->overlay_tx),
843 GPU_ATTACHMENT_TEXTURE(this->line_tx));
844 this->overlay_in_front_fb.ensure(GPU_ATTACHMENT_TEXTURE(this->depth_target_in_front_tx),
845 GPU_ATTACHMENT_TEXTURE(this->overlay_tx));
846 this->overlay_line_in_front_fb.ensure(GPU_ATTACHMENT_TEXTURE(this->depth_target_in_front_tx),
847 GPU_ATTACHMENT_TEXTURE(this->overlay_tx),
848 GPU_ATTACHMENT_TEXTURE(this->line_tx));
849 }
850
851 this->overlay_line_only_fb.ensure(GPU_ATTACHMENT_NONE,
852 GPU_ATTACHMENT_TEXTURE(this->overlay_tx),
853 GPU_ATTACHMENT_TEXTURE(this->line_tx));
854 this->overlay_color_only_fb.ensure(GPU_ATTACHMENT_NONE,
855 GPU_ATTACHMENT_TEXTURE(this->overlay_tx));
856
857 this->overlay_output_color_only_fb.ensure(GPU_ATTACHMENT_NONE,
858 GPU_ATTACHMENT_TEXTURE(this->color_overlay_tx));
859 this->overlay_output_fb.ensure(GPU_ATTACHMENT_TEXTURE(this->depth_tx),
860 GPU_ATTACHMENT_TEXTURE(this->color_overlay_tx));
861 }
862
863 void release()
864 {
865 this->line_tx.release();
866 this->overlay_tx.release();
867 this->xray_depth_tx.release();
868 this->xray_depth_in_front_tx.release();
869 this->depth_in_front_alloc_tx.release();
870 this->color_overlay_alloc_tx.release();
871 this->color_render_alloc_tx.release();
873 }
874
876 {
877 const bool is_edit = (state.object_mode & OB_MODE_EDIT) &&
878 (ob_ref.object->mode & OB_MODE_EDIT);
879 const bool active = ob_ref.is_active(state.object_active);
880 const bool is_selected = ((ob_ref.object->base_flag & BASE_SELECTED) != 0);
881
882 /* Object in edit mode. */
883 if (is_edit) {
884 return TH_WIRE_EDIT;
885 }
886 /* Transformed object during operators. */
887 if (((G.moving & G_TRANSFORM_OBJ) != 0) && is_selected) {
888 return TH_TRANSFORM;
889 }
890 /* Sets the 'theme_id' or fall back to wire */
891 if ((ob_ref.object->base_flag & BASE_SELECTED) != 0) {
892 return (active) ? TH_ACTIVE : TH_SELECT;
893 }
894
895 switch (ob_ref.object->type) {
896 case OB_LAMP:
897 return TH_LIGHT;
898 case OB_SPEAKER:
899 return TH_SPEAKER;
900 case OB_CAMERA:
901 return TH_CAMERA;
902 case OB_LIGHTPROBE:
903 /* TODO: add light-probe color. Use empty color for now. */
904 case OB_EMPTY:
905 return TH_EMPTY;
906 default:
907 return (is_edit) ? TH_WIRE_EDIT : TH_WIRE;
908 }
909 }
910
911 const float4 &object_wire_color(const ObjectRef &ob_ref, ThemeColorID theme_id) const
912 {
913 if (ob_ref.object->base_flag & BASE_FROM_SET) [[unlikely]] {
914 return theme.colors.wire;
915 }
916 switch (theme_id) {
917 case TH_WIRE_EDIT:
918 return theme.colors.wire_edit;
919 case TH_ACTIVE:
920 return theme.colors.active_object;
921 case TH_SELECT:
922 return theme.colors.object_select;
923 case TH_TRANSFORM:
924 return theme.colors.transform;
925 case TH_SPEAKER:
926 return theme.colors.speaker;
927 case TH_CAMERA:
928 return theme.colors.camera;
929 case TH_EMPTY:
930 return theme.colors.empty;
931 case TH_LIGHT:
932 return theme.colors.light;
933 default:
934 return theme.colors.wire;
935 }
936 }
937
938 const float4 &object_wire_color(const ObjectRef &ob_ref, const State &state) const
939 {
940 ThemeColorID theme_id = object_wire_theme_id(ob_ref, state);
941 return object_wire_color(ob_ref, theme_id);
942 }
943
945 {
948 return color;
949 }
950
952 {
953 ThemeColorID theme_id = object_wire_theme_id(ob_ref, state);
954 return background_blend_color(theme_id);
955 }
956
958 {
959 if (state.v3d->shading.background_type == V3D_SHADING_BACKGROUND_WORLD) {
960 if (state.scene->world) {
961 return float4(float3(&state.scene->world->horr), 0.0f);
962 }
963 }
964 else if (state.v3d->shading.background_type == V3D_SHADING_BACKGROUND_VIEWPORT) {
965 return state.v3d->shading.background_color;
966 }
969 return color;
970 }
971
973 {
974 /* Free Movie clip textures after rendering */
975 for (MovieClip *clip : bg_movie_clips) {
977 }
978 bg_movie_clips.clear();
979 }
980
981 static float vertex_size_get()
982 {
983 /* M_SQRT2 to be at least the same size of the old square */
984 return max_ff(1.0f, ui::theme::get_value_f(TH_VERTEX_SIZE) * float(M_SQRT2) / 2.0f);
985 }
986
988
989 /* Returns true if drawing for any selection mode. */
990 bool is_selection() const
991 {
992 return this->selection_type != SelectionType::DISABLED;
993 }
994};
995
996/* List of flat objects draw-calls.
997 * In order to not loose selection display of flat objects view from the side,
998 * we store them in a list and add them to the pass just in time if their flat side is
999 * perpendicular to the view. */
1000/* Reference to a flat object.
1001 * Allow deferred rendering condition of flat object for special purpose. */
1003 gpu::Batch *geom;
1006
1007 /* Returns flat axis index if only one axis is flat. Returns -1 otherwise. */
1008 static int flat_axis_index_get(const ObjectRef &ob_ref)
1009 {
1010 BLI_assert(ELEM(ob_ref.object->type,
1011 OB_MESH,
1013 OB_SURF,
1014 OB_FONT,
1015 OB_CURVES,
1017 OB_VOLUME));
1018
1019 float3 dim;
1020 if (!ob_ref.is_dupli()) {
1021 BKE_object_dimensions_get(ob_ref.object, dim);
1022 }
1023 else {
1024 /* BKE_object_dimensions_get can't be used with dupli objects.
1025 * Just use mesh bounds instead of object bounds. */
1026 std::optional<Bounds<float3>> bounds = BKE_object_boundbox_get(ob_ref.object);
1027 if (!bounds) {
1028 return -1;
1029 }
1030 dim = bounds->size();
1031 }
1032
1033 /* Small epsilon relative to object size to handle float errors in flat axis detection after
1034 * rotation. See #139555. */
1035 const float max_dim = math::reduce_max(float3(dim));
1036 const float epsilon = max_dim * 1e-6f;
1037
1038 if (dim[0] <= epsilon) {
1039 return 0;
1040 }
1041 if (dim[1] <= epsilon) {
1042 return 1;
1043 }
1044 if (dim[2] <= epsilon) {
1045 return 2;
1046 }
1047 return -1;
1048 }
1049
1050 using Callback = FunctionRef<void(gpu::Batch *geom, ResourceID handle)>;
1051
1052 /* Execute callback for every handles that is orthogonal to the view.
1053 * Note: Only works in orthogonal view. */
1054 void if_flat_axis_orthogonal_to_view(Manager &manager, const View &view, Callback callback) const
1055 {
1056 for (ResourceID resource_id : handle.id_range()) {
1057 const float4x4 &object_to_world =
1058 manager.matrix_buf.current().get_or_resize(resource_id.index()).model;
1059
1060 float3 view_forward = view.forward();
1061 float3 axis_not_flat_a = (flattened_axis_id == 0) ? object_to_world.y_axis() :
1062 object_to_world.x_axis();
1063 float3 axis_not_flat_b = (flattened_axis_id == 1) ? object_to_world.z_axis() :
1064 object_to_world.y_axis();
1065 float3 axis_flat = math::cross(axis_not_flat_a, axis_not_flat_b);
1066
1067 if (math::abs(math::dot(view_forward, axis_flat)) < 1e-3f) {
1068 callback(geom, resource_id);
1069 }
1070 }
1071 }
1072};
1073
1077template<typename InstanceDataT> struct ShapeInstanceBuf : private select::SelectBuf {
1078
1080
1081 ShapeInstanceBuf(const SelectionType selection_type, const char *name = nullptr)
1083
1084 void clear()
1085 {
1086 this->select_clear();
1087 data_buf.clear();
1088 }
1089
1090 void append(const InstanceDataT &data, select::ID select_id)
1091 {
1092 this->select_append(select_id);
1094 }
1095
1096 void end_sync(PassSimple::Sub &pass, gpu::Batch *shape)
1097 {
1098 if (data_buf.is_empty()) {
1099 return;
1100 }
1101 this->select_bind(pass);
1103 pass.bind_ssbo("data_buf", &data_buf);
1104 pass.draw(shape, data_buf.size());
1105 }
1106
1108 gpu::Batch *shape,
1109 GPUPrimType primitive_type,
1110 uint primitive_len)
1111 {
1112 if (data_buf.is_empty()) {
1113 return;
1114 }
1115 this->select_bind(pass);
1117 pass.bind_ssbo("data_buf", &data_buf);
1118 pass.draw_expand(shape, primitive_type, primitive_len, data_buf.size());
1119 }
1120};
1121
1123 protected:
1126 int color_id = 0;
1127
1128 VertexPrimitiveBuf(const SelectionType selection_type, const char *name = nullptr)
1129 : select_buf(selection_type), data_buf(name) {};
1130
1131 void append(const float3 &position, const float4 &color)
1132 {
1133 data_buf.append({float4(position, 0.0f), color});
1134 }
1135
1136 void end_sync(PassSimple::Sub &pass, GPUPrimType primitive)
1137 {
1138 if (data_buf.is_empty()) {
1139 return;
1140 }
1141 select_buf.select_bind(pass);
1143 pass.bind_ssbo("data_buf", &data_buf);
1144 pass.push_constant("colorid", color_id);
1145 pass.draw_procedural(primitive, 1, data_buf.size());
1146 }
1147
1148 public:
1149 void clear()
1150 {
1152 data_buf.clear();
1153 color_id = 0;
1154 }
1155};
1156
1158
1159 public:
1160 PointPrimitiveBuf(const SelectionType selection_type, const char *name = nullptr)
1161 : VertexPrimitiveBuf(selection_type, name)
1162 {
1163 }
1164
1166 const float4 &color,
1168 {
1169 select_buf.select_append(select_id);
1171 }
1172
1173 void append(const float3 &position, const int color_id, select::ID select_id)
1174 {
1175 this->color_id = color_id;
1176 append(position, float4(), select_id);
1177 }
1178
1183};
1184
1186
1187 public:
1188 LinePrimitiveBuf(const SelectionType selection_type, const char *name = nullptr)
1189 : VertexPrimitiveBuf(selection_type, name)
1190 {
1191 }
1192
1193 void append(const float3 &start,
1194 const float3 &end,
1195 const float4 &color,
1197 {
1198 select_buf.select_append(select_id);
1201 }
1202
1203 void append(const float3 &start,
1204 const float3 &end,
1205 const int color_id,
1207 {
1208 this->color_id = color_id;
1209 append(start, end, float4(), select_id);
1210 }
1211
1216};
1217
1218/* Consider instance any object form a set or a dupli system.
1219 * This hides some overlay to avoid making the viewport unreadable. */
1220static inline bool is_from_dupli_or_set(const Object *ob)
1221{
1222 return ob->base_flag & (BASE_FROM_SET | BASE_FROM_DUPLI);
1223}
1224
1225/* Consider instance any object form a set or a dupli system.
1226 * This hides some overlay to avoid making the viewport unreadable. */
1227static inline bool is_from_dupli_or_set(const ObjectRef &ob_ref)
1228{
1229 return is_from_dupli_or_set(ob_ref.object);
1230}
1231
1232} // namespace draw::overlay
1233} // namespace blender
General operations, lookup, etc. for blender objects.
#define M_SQRT2
#define GPU_BATCH_DISCARD_SAFE(batch)
Definition GPU_batch.hh:199
#define GPU_ATTACHMENT_TEXTURE(_texture)
#define GPU_ATTACHMENT_NONE
#define ELEM(...)
SIMD_FORCE_INLINE btVector3 operator()(const btVector3 &x) const
Return the transform of the vector.
Definition btTransform.h:90
void ensure(GPUAttachment depth=GPU_ATTACHMENT_NONE, GPUAttachment color1=GPU_ATTACHMENT_NONE, GPUAttachment color2=GPU_ATTACHMENT_NONE, GPUAttachment color3=GPU_ATTACHMENT_NONE, GPUAttachment color4=GPU_ATTACHMENT_NONE, GPUAttachment color5=GPU_ATTACHMENT_NONE, GPUAttachment color6=GPU_ATTACHMENT_NONE, GPUAttachment color7=GPU_ATTACHMENT_NONE, GPUAttachment color8=GPU_ATTACHMENT_NONE)
SwapChain< ObjectMatricesBuf, 2 > matrix_buf
bool is_active(const Object *active_object) const
bool acquire_2d(int2 extent, gpu::TextureFormat format, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, int mip_len=1)
void wrap(gpu::Texture *tex)
int3 size(int miplvl=0) const
const float3 & forward(int view_id=0) const
Definition draw_view.hh:130
void draw_procedural(GPUPrimType primitive, uint instance_len, uint vertex_len, uint vertex_first=-1, ResourceIDRange res_id={}, uint custom_id=0)
Definition draw_pass.hh:976
void draw_expand(gpu::Batch *batch, GPUPrimType primitive_type, uint primitive_len, uint instance_len, uint vertex_len, uint vertex_first, ResourceIDRange res_id={}, uint custom_id=0)
Definition draw_pass.hh:938
void push_constant(const char *name, const float &data)
void bind_ssbo(const char *name, gpu::StorageBuf *buffer)
void draw(gpu::Batch *batch, uint instance_len=-1, uint vertex_len=-1, uint vertex_first=-1, ResourceIDRange res_id={}, uint custom_id=0)
Definition draw_pass.hh:906
detail::PassBase< command::DrawCommandBuf > Sub
Definition draw_pass.hh:509
static ShaderModule & module_get(SelectionType selection_type, bool clipping_enabled)
MutableSpan< T > data()
#define position
const int state
#define T
gpu::StaticShader StaticShader
select::SelectionType SelectionType
static bool is_from_dupli_or_set(const Object *ob)
T reduce_max(const VecBase< T, Size > &a)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
AxisSigned cross(const AxisSigned a, const AxisSigned b)
T abs(const T &a)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
void get_color_3fv(int colorid, float col[3])
float get_value_f(int colorid)
void get_color_blend_shade_4fv(int colorid1, int colorid2, float fac, int offset, float col[4])
BMesh Iterators.
@ V3D_OVERLAY_HIDE_OBJECT_ORIGINS
@ V3D_OVERLAY_SCULPT_SHOW_MASK
@ V3D_OVERLAY_FADE_INACTIVE
@ V3D_OVERLAY_VIEWER_ATTRIBUTE
@ V3D_OVERLAY_HIDE_MOTION_PATHS
@ V3D_OVERLAY_SCULPT_SHOW_FACE_SETS
@ V3D_OVERLAY_SCULPT_CURVES_CAGE
@ V3D_OVERLAY_VIEWER_ATTRIBUTE_TEXT
@ V3D_OVERLAY_HIDE_OBJECT_XTRAS
@ V3D_OVERLAY_SHOW_LIGHT_COLORS
@ V3D_OVERLAY_FACE_ORIENTATION
@ V3D_OVERLAY_BONE_SELECT
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
unsigned int uint
Definition BKE_text.h:55
@ CTX_MODE_EDIT_MESH
Global G
Definition blender.cc:57
@ GPU_TEXTURE_USAGE_SHADER_READ
@ GPU_TEXTURE_USAGE_SHADER_WRITE
@ GPU_TEXTURE_USAGE_ATTACHMENT
BMesh const char void * data
void BKE_object_dimensions_get(const Object *ob, float r_vec[3])
Definition object.cc:3799
@ V3D_SHADING_BACKGROUND_VIEWPORT
@ V3D_SHADING_BACKGROUND_WORLD
MINLINE float max_ff(float a, float b)
void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len)
@ G_TRANSFORM_OBJ
std::optional< Bounds< float3 > > BKE_object_boundbox_get(const Object *ob)
Definition object.cc:3726
void BKE_movieclip_free_gputexture(MovieClip *clip)
MINLINE float clamp_f(float value, float min, float max)
TokenType select(char char_value, char char_class, bool cond)
Definition lexit.cc:418
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
DefaultTextureList * viewport_texture_list_get() const
DefaultFramebufferList * viewport_framebuffer_list_get() const
eView3DOverlay_Flag flag
void set_color(const float4 &bone_color)
BoneInstanceData(const float4x4 &ob_mat, const float3 &pos, const float radius, const float color[4])
void set_hint_color(const float4 &hint_color)
BoneInstanceData(const float4x4 &bone_mat, const float4 &bone_color, const float4 &hint_color)
BoneInstanceData(const float4x4 &bone_mat, const float4 &bone_color)
FunctionRef< void(gpu::Batch *geom, ResourceID handle)> Callback
void if_flat_axis_orthogonal_to_view(Manager &manager, const View &view, Callback callback) const
static int flat_axis_index_get(const ObjectRef &ob_ref)
void append(const float3 &start, const float3 &end, const float4 &color, select::ID select_id=select::SelectMap::select_invalid_id())
void append(const float3 &start, const float3 &end, const int color_id, select::ID select_id=select::SelectMap::select_invalid_id())
LinePrimitiveBuf(const SelectionType selection_type, const char *name=nullptr)
PointPrimitiveBuf(const SelectionType selection_type, const char *name=nullptr)
void append(const float3 &position, const int color_id, select::ID select_id)
void append(const float3 &position, const float4 &color, select::ID select_id=select::SelectMap::select_invalid_id())
float4 background_color_get(const State &state)
const float4 & object_wire_color(const ObjectRef &ob_ref, ThemeColorID theme_id) const
const float4 & object_wire_color(const ObjectRef &ob_ref, const State &state) const
void begin_sync(int clipping_plane_count)
void acquire(const DRWContext *draw_ctx, const State &state)
void update_theme_settings(const DRWContext *ctx, const State &state)
detail::SubPassVector< GreasePencilDepthPlane, 16 > depth_planes
float4 object_background_blend_color(const ObjectRef &ob_ref, const State &state) const
draw::UniformArrayBuffer< float4, 6 > clip_planes_buf
void init(bool clipping_enabled)
float4 background_blend_color(ThemeColorID theme_id) const
ThemeColorID object_wire_theme_id(const ObjectRef &ob_ref, const State &state) const
draw::UniformBuffer< UniformData > globals_buf
Resources(const SelectionType selection_type_, const ShapeCache &shapes_)
void update_clip_planes(const State &state)
ShapeInstanceBuf(const SelectionType selection_type, const char *name=nullptr)
void end_sync(PassSimple::Sub &pass, gpu::Batch *shape)
void append(const InstanceDataT &data, select::ID select_id)
void end_sync(PassSimple::Sub &pass, gpu::Batch *shape, GPUPrimType primitive_type, uint primitive_len)
View::OffsetData offset_data_get() const
GPU_VERTEX_FORMAT_FUNC(VertShaded, pos, vclass, nor)
GPU_VERTEX_FORMAT_FUNC(VertexBone, pos, vclass)
void end_sync(PassSimple::Sub &pass, GPUPrimType primitive)
void append(const float3 &position, const float4 &color)
VertexPrimitiveBuf(const SelectionType selection_type, const char *name=nullptr)
StorageVectorBuffer< VertexData > data_buf
GPU_VERTEX_FORMAT_FUNC(VertexTriple, pos0, pos1, pos2)
GPU_VERTEX_FORMAT_FUNC(VertexWithColor, pos, color)
GPU_VERTEX_FORMAT_FUNC(Vertex, pos, vclass)
void select_bind(PassSimple::Sub &pass)
SelectBuf(const SelectionType selection_type)
SelectMap(const SelectionType selection_type)
static constexpr VertAttrType type
static constexpr VertAttrType type