Blender V5.3
overlay_sculpt.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "BKE_attribute.hh"
12#include "BKE_curves.hh"
13#include "BKE_mesh.hh"
14#include "BKE_paint.hh"
15#include "BKE_paint_bvh.hh"
16#include "BKE_subdiv_ccg.hh"
18
19#include "DRW_render.hh"
20#include "bmesh.hh"
21
22#include "draw_cache_impl.hh"
23#include "draw_sculpt.hh"
24
25#include "overlay_base.hh"
26
27namespace blender::draw::overlay {
28
35
36 private:
37 PassSimple sculpt_mask_ = {"SculptMaskAndFaceSet"};
38 PassSimple::Sub *mesh_ps_ = nullptr;
39 PassSimple::Sub *curves_ps_ = nullptr;
40
41 PassSimple sculpt_curve_cage_ = {"SculptCage"};
42
43 bool show_curves_cage_ = false;
44 bool show_face_set_ = false;
45 bool show_mask_ = false;
46
47 public:
48 void begin_sync(Resources &res, const State &state) final
49 {
50 show_curves_cage_ = state.show_sculpt_curves_cage();
51 show_face_set_ = state.show_sculpt_face_sets();
52 show_mask_ = state.show_sculpt_mask();
53
54 enabled_ = state.is_space_v3d() && !state.is_wire() && !res.is_selection() &&
55 !state.is_depth_only_drawing &&
57 (show_curves_cage_ || show_face_set_ || show_mask_);
58
59 if (!enabled_) {
60 /* Not used. But release the data. */
61 sculpt_mask_.init();
62 sculpt_curve_cage_.init();
63 return;
64 }
65
66 float curve_cage_opacity = show_curves_cage_ ? state.overlay.sculpt_curves_cage_opacity : 0.0f;
67 float face_set_opacity = show_face_set_ ? state.overlay.sculpt_mode_face_sets_opacity : 0.0f;
68 float mask_opacity = show_mask_ ? state.overlay.sculpt_mode_mask_opacity : 0.0f;
69
70 {
71 sculpt_mask_.init();
72 sculpt_mask_.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
73 sculpt_mask_.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
74 {
75 auto &sub = sculpt_mask_.sub("Mesh");
77 state.clipping_plane_count);
78 sub.shader_set(res.shaders->sculpt_mesh.get());
79 sub.push_constant("mask_opacity", mask_opacity);
80 sub.push_constant("face_sets_opacity", face_set_opacity);
81 mesh_ps_ = ⊂
82 }
83 {
84 auto &sub = sculpt_mask_.sub("Curves");
86 state.clipping_plane_count);
87 sub.shader_set(res.shaders->sculpt_curves.get());
88 sub.push_constant("selection_opacity", mask_opacity);
89 curves_ps_ = ⊂
90 }
91 }
92 {
93 auto &pass = sculpt_curve_cage_;
94 pass.init();
96 state.clipping_plane_count);
97 pass.shader_set(res.shaders->sculpt_curves_cage.get());
98 pass.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
99 pass.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
100 pass.push_constant("opacity", curve_cage_opacity);
101 }
102 }
103
104 void object_sync(Manager &manager,
105 const ObjectRef &ob_ref,
106 Resources & /*res*/,
107 const State &state) final
108 {
109 if (!enabled_) {
110 return;
111 }
112
113 switch (ob_ref.object->type) {
114 case OB_MESH:
115 mesh_sync(manager, ob_ref, state);
116 break;
117 case OB_CURVES:
118 curves_sync(manager, ob_ref, state);
119 break;
120 default:
121 break;
122 }
123 }
124
125 void curves_sync(Manager &manager, const ObjectRef &ob_ref, const State &state)
126 {
128
129 /* As an optimization, draw nothing if everything is selected. */
130 if (show_mask_ && !everything_selected(curves)) {
131 /* Retrieve the location of the texture. */
132 bool is_point_domain;
133 bool is_valid;
135 &curves, ".selection", is_point_domain, is_valid);
136 if (is_valid) {
137 /* Evaluate curves and their attributes if necessary. */
138 const char *error = nullptr;
139 /* The error string will always have been printed by the engine already.
140 * No need to display it twice. */
141 gpu::Batch *geometry = curves_sub_pass_setup(
142 *curves_ps_, state.scene, ob_ref.object, error);
143 if (select_attr_buf.get()) {
144 ResourceHandleRange handle = manager.unique_handle(ob_ref);
145
146 curves_ps_->push_constant("is_point_domain", is_point_domain);
147 curves_ps_->bind_texture("selection_tx", select_attr_buf);
148 curves_ps_->draw(geometry, handle);
149 }
150 }
151 }
152
153 if (show_curves_cage_) {
154 ResourceHandleRange handle = manager.unique_handle(ob_ref);
155
157 sculpt_curve_cage_.draw(geometry, handle);
158 }
159 }
160
161 void mesh_sync(Manager &manager, const ObjectRef &ob_ref, const State &state)
162 {
163 if (!show_face_set_ && !show_mask_) {
164 /* Nothing to display. */
165 return;
166 }
167
168 const SculptSession *sculpt_session = ob_ref.object->runtime->sculpt_session;
169 if (sculpt_session == nullptr) {
170 return;
171 }
172
174 if (!pbvh) {
175 /* It is possible to have SculptSession without pbvh::Tree. This happens, for example, when
176 * toggling object mode to sculpt then to edit mode. */
177 return;
178 }
179
180 /* Using the original object/geometry is necessary because we skip depsgraph updates in sculpt
181 * mode to improve performance. This means the evaluated mesh doesn't have the latest face set,
182 * visibility, and mask data. */
183 Object *object_orig = DEG_get_original(ob_ref.object);
184 if (!object_orig) {
186 return;
187 }
188
189 switch (pbvh->type()) {
191 const Mesh &mesh = DRW_object_get_data_for_drawing<Mesh>(*object_orig);
192 if (!mesh.attributes().contains(".sculpt_face_set") &&
193 !mesh.attributes().contains(".sculpt_mask"))
194 {
195 return;
196 }
197 break;
198 }
200 const SubdivCCG &subdiv_ccg = *sculpt_session->subdiv_ccg;
201 const Mesh &base_mesh = DRW_object_get_data_for_drawing<Mesh>(*object_orig);
202 if (subdiv_ccg.masks.is_empty() && !base_mesh.attributes().contains(".sculpt_face_set")) {
203 return;
204 }
205 break;
206 }
208 const BMesh &bm = *sculpt_session->bm;
209 if (!CustomData_has_layer_named(&bm.pdata, CD_PROP_FLOAT, ".sculpt_face_set") &&
211 {
212 return;
213 }
214 break;
215 }
216 }
217
218 const bool use_pbvh = BKE_sculptsession_use_pbvh_draw(ob_ref.object, state.rv3d);
219 if (use_pbvh) {
220 ResourceHandleRange handle = manager.unique_handle_for_sculpt(ob_ref);
221
222 SculptBatchFeature sculpt_batch_features_ = (show_face_set_ ? SCULPT_BATCH_FACE_SET :
224 (show_mask_ ? SCULPT_BATCH_MASK :
226
227 for (SculptBatch &batch : sculpt_batches_get(ob_ref.object, sculpt_batch_features_)) {
228 mesh_ps_->draw(batch.batch, handle);
229 }
230 }
231 else {
232 ResourceHandleRange handle = manager.unique_handle(ob_ref);
233
235 gpu::Batch *sculpt_overlays = DRW_mesh_batch_cache_get_sculpt_overlays(mesh);
236 mesh_ps_->draw(sculpt_overlays, handle);
237 }
238 }
239
240 void draw_line(Framebuffer &framebuffer, Manager &manager, View &view) final
241 {
242 if (!enabled_) {
243 return;
244 }
245 GPU_framebuffer_bind(framebuffer);
246 manager.submit(sculpt_curve_cage_, view);
247 }
248
249 void draw_on_render(gpu::FrameBuffer *framebuffer, Manager &manager, View &view) final
250 {
251 if (!enabled_) {
252 return;
253 }
254 GPU_framebuffer_bind(framebuffer);
255 manager.submit(sculpt_mask_, view);
256 }
257
258 private:
259 bool everything_selected(const blender::Curves &curves_id)
260 {
261 const bke::CurvesGeometry &curves = curves_id.geometry.wrap();
262 const VArray<bool> selection = *curves.attributes().lookup_or_default<bool>(
263 ".selection", bke::AttrDomain::Point, true);
264 return selection.is_single() && selection.get_internal_single();
265 }
266};
267
268} // namespace blender::draw::overlay
Low-level operations for curves.
A BVH for high poly meshes.
#define BLI_assert_unreachable()
#define ELEM(...)
bool is_empty() const
Definition BLI_array.hh:275
ResourceHandleRange unique_handle_for_sculpt(const ObjectRef &ref)
ResourceHandleRange unique_handle(const ObjectRef &ref)
detail::PassBase< command::DrawCommandBuf > Sub
Definition draw_pass.hh:509
void object_sync(Manager &manager, const ObjectRef &ob_ref, Resources &, const State &state) final
void mesh_sync(Manager &manager, const ObjectRef &ob_ref, const State &state)
void draw_line(Framebuffer &framebuffer, Manager &manager, View &view) final
void begin_sync(Resources &res, const State &state) final
void curves_sync(Manager &manager, const ObjectRef &ob_ref, const State &state)
void draw_on_render(gpu::FrameBuffer *framebuffer, Manager &manager, View &view) final
#define DRW_CLIPPING_UBO_SLOT
#define OVERLAY_GLOBALS_SLOT
const int state
pbvh::Tree * pbvh_get(Object &object)
detail::Pass< command::DrawCommandBuf > PassSimple
gpu::Batch * curves_sub_pass_setup(PassMain::Sub &ps, const Scene *scene, Object *ob, const char *&r_error, GPUMaterial *gpu_material=nullptr)
Vector< SculptBatch > sculpt_batches_get(const Object *ob, SculptBatchFeature features)
gpu::Batch * DRW_curves_batch_cache_get_sculpt_curves_cage(Curves *curves)
gpu::Batch * DRW_mesh_batch_cache_get_sculpt_overlays(Mesh &mesh)
gpu::VertBufPtr & DRW_curves_texture_for_evaluated_attribute(Curves *curves, StringRef name, bool &r_is_point_domain, bool &r_valid_attribute)
std::unique_ptr< gpu::VertBuf, gpu::VertBufDeleter > VertBufPtr
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
T & DRW_object_get_data_for_drawing(const Object &object)
@ OB_MODE_SCULPT_CURVES
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
@ DRW_STATE_BLEND_ALPHA
Definition draw_state.hh:57
@ DRW_STATE_BLEND_MUL
Definition draw_state.hh:62
@ DRW_STATE_DEPTH_LESS_EQUAL
Definition draw_state.hh:40
T * DEG_get_original(T *id)
bool BKE_sculptsession_use_pbvh_draw(const Object *ob, const RegionView3D *rv3d)
bool CustomData_has_layer_named(const CustomData *data, eCustomDataType type, StringRef name)
CustomData pdata
CustomData vdata
bke::ObjectRuntime * runtime
Array< float > masks