Blender V5.3
overlay_wireframe.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "BKE_paint.hh"
12#include "DNA_volume_types.h"
13
14#include "DRW_render.hh"
15#include "draw_common.hh"
16#include "draw_sculpt.hh"
17
18#include "overlay_base.hh"
19#include "overlay_mesh.hh"
20
21namespace blender::draw::overlay {
22
33 private:
34 PassMain wireframe_ps_ = {"Wireframe"};
35 struct ColoringPass {
36 PassMain::Sub *curves_ps_ = nullptr;
37 PassMain::Sub *mesh_ps_ = nullptr;
38 /* Variant for meshes that force drawing all edges. */
39 PassMain::Sub *mesh_all_edges_ps_ = nullptr;
40 PassMain::Sub *points_ps_ = nullptr;
41 PassMain::Sub *pointcloud_ps_ = nullptr;
42 PassMain::Sub *gsplat_ps_ = nullptr;
43 } colored, non_colored;
44
45 /* Copy of the depth buffer to be able to read it during wireframe rendering. */
46 TextureFromPool tmp_depth_tx_ = {"tmp_depth_tx"};
47 bool do_depth_copy_workaround_ = false;
48
49 /* Force display of wireframe on surface objects, regardless of the object display settings. */
50 bool show_wire_ = false;
51
52 public:
53 void begin_sync(Resources &res, const State &state) final
54 {
55 enabled_ = state.is_space_v3d() && (state.is_wireframe_mode || !state.hide_overlays);
56 if (!enabled_) {
57 return;
58 }
59
60 show_wire_ = state.is_wireframe_mode || state.show_wireframes();
61
62 const bool is_selection = res.is_selection();
63 const bool do_smooth_lines = (U.gpu_flag & USER_GPU_FLAG_OVERLAY_SMOOTH_WIRE) != 0;
64 const bool is_transform = (G.moving & G_TRANSFORM_OBJ) != 0;
65 const float wire_threshold = wire_discard_threshold_get(state.overlay.wireframe_threshold);
66
67 gpu::Texture **depth_tex = (state.xray_enabled) ? &res.depth_tx : &tmp_depth_tx_;
68 if (is_selection) {
69 depth_tex = &res.dummy_depth_tx;
70 }
71
72 /* Note: Depth buffer has different format when doing selection. Avoid copy in this case. */
73 do_depth_copy_workaround_ = !is_selection && (depth_tex == &tmp_depth_tx_);
74
75 {
76 auto &pass = wireframe_ps_;
77 pass.init();
78 pass.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
79 pass.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
82 state.clipping_plane_count);
83 res.select_bind(pass);
84
85 auto shader_pass =
86 [&](gpu::Shader *shader, const char *name, bool use_coloring, float wire_threshold) {
87 auto &sub = pass.sub(name);
88 if (res.shaders->wireframe_mesh.get() == shader) {
89 sub.specialize_constant(shader, "use_custom_depth_bias", do_smooth_lines);
90 }
91 sub.shader_set(shader);
92 sub.bind_texture("depth_tx", depth_tex);
93 sub.push_constant("wire_opacity", state.overlay.wireframe_opacity);
94 sub.push_constant("is_transform", is_transform);
95 sub.push_constant("color_type", state.v3d->shading.wire_color_type);
96 sub.push_constant("use_coloring", use_coloring);
97 sub.push_constant("wire_step_param", wire_threshold);
98 sub.push_constant("ndc_offset_factor", &state.ndc_offset_factor);
99 sub.push_constant("is_hair", false);
100 return ⊂
101 };
102
103 auto coloring_pass = [&](ColoringPass &ps, bool use_color) {
104 overlay::ShaderModule &sh = *res.shaders;
105 ps.mesh_ps_ = shader_pass(sh.wireframe_mesh.get(), "Mesh", use_color, wire_threshold);
106 ps.mesh_all_edges_ps_ = shader_pass(sh.wireframe_mesh.get(), "Wire", use_color, 1.0f);
107 ps.points_ps_ = shader_pass(sh.wireframe_points.get(), "Points", use_color, 1.0f);
108 ps.pointcloud_ps_ = shader_pass(
109 sh.wireframe_points_with_radius.get(), "PtCloud", use_color, 1.0f);
110 ps.gsplat_ps_ = shader_pass(sh.wireframe_gsplat.get(), "GSplat", use_color, 1.0f);
111 ps.curves_ps_ = shader_pass(sh.wireframe_curve.get(), "Curve", use_color, 1.0f);
112 };
113
114 coloring_pass(non_colored, false);
115 coloring_pass(colored, true);
116 }
117 }
118
119 void object_sync_ex(Manager &manager,
120 const ObjectRef &ob_ref,
121 Resources &res,
122 const State &state,
123 const bool in_edit_paint_mode,
124 const bool in_edit_mode)
125 {
126 if (!enabled_) {
127 return;
128 }
129
130 if (ob_ref.object->dt < OB_WIRE) {
131 return;
132 }
133
134 const bool all_edges = (ob_ref.object->dtx & OB_DRAW_ALL_EDGES) != 0;
135 const bool show_surface_wire = show_wire_ || (ob_ref.object->dtx & OB_DRAWWIRE) ||
136 (ob_ref.object->dt == OB_WIRE);
137
138 ColoringPass &coloring = in_edit_paint_mode ? non_colored : colored;
139 switch (ob_ref.object->type) {
140 case OB_CURVES_LEGACY: {
141 gpu::Batch *geom = DRW_cache_curve_edge_wire_get(ob_ref.object);
142 coloring.curves_ps_->draw(
143 geom, manager.unique_handle(ob_ref), res.select_id(ob_ref).get());
144 break;
145 }
146 case OB_FONT: {
147 gpu::Batch *geom = DRW_cache_text_edge_wire_get(ob_ref.object);
148 coloring.curves_ps_->draw(
149 geom, manager.unique_handle(ob_ref), res.select_id(ob_ref).get());
150 break;
151 }
152 case OB_SURF: {
153 gpu::Batch *geom = DRW_cache_surf_edge_wire_get(ob_ref.object);
154 coloring.curves_ps_->draw(
155 geom, manager.unique_handle(ob_ref), res.select_id(ob_ref).get());
156 break;
157 }
158 case OB_CURVES:
159 /* TODO(fclem): Not yet implemented. */
160 break;
161 case OB_GREASE_PENCIL: {
162 if (show_surface_wire) {
163 gpu::Batch *geom = DRW_cache_grease_pencil_face_wireframe_get(state.scene,
164 ob_ref.object);
165 coloring.curves_ps_->draw(
166 geom, manager.unique_handle(ob_ref), res.select_id(ob_ref).get());
167 }
168 break;
169 }
170 case OB_MESH: {
171 /* Force display in edit mode when overlay is off in wireframe mode (see #78484). */
172 const bool wireframe_no_overlay = state.hide_overlays && state.is_wireframe_mode;
173
174 /* In some cases the edit mode wireframe overlay is already drawn for the same edges.
175 * We want to avoid this redundant work and avoid Z-fighting, but detecting this case is
176 * relatively complicated. Whether edit mode draws edges on the evaluated mesh depends on
177 * whether there is a separate cage and whether there is a valid mapping between the
178 * evaluated and original edit mesh. */
179 const bool edit_wires_overlap_all = mesh_edit_wires_overlap(ob_ref, in_edit_mode);
180
181 const bool bypass_mode_check = wireframe_no_overlay || !edit_wires_overlap_all;
182
183 if (show_surface_wire) {
184 if (BKE_sculptsession_use_pbvh_draw(ob_ref.object, state.rv3d)) {
185 ResourceHandleRange handle = manager.unique_handle(ob_ref);
186
188 coloring.mesh_all_edges_ps_->draw(batch.batch, handle);
189 }
190 }
191 else if (!in_edit_mode || bypass_mode_check) {
192 /* Only draw the wireframe in edit mode if object has edit cage.
193 * Otherwise the wireframe will conflict with the edit cage drawing and produce
194 * unpleasant aliasing. */
195 gpu::Batch *geom = DRW_cache_mesh_face_wireframe_get(ob_ref.object);
196 (all_edges ? coloring.mesh_all_edges_ps_ : coloring.mesh_ps_)
197 ->draw(geom, manager.unique_handle(ob_ref), res.select_id(ob_ref).get());
198 }
199 }
200
201 /* Draw loose geometry. */
202 if (!in_edit_paint_mode || bypass_mode_check) {
204 gpu::Batch *geom;
205 if ((mesh.edges_num == 0) && (mesh.verts_num > 0)) {
207 coloring.points_ps_->draw(
208 geom, manager.unique_handle(ob_ref), res.select_id(ob_ref).get());
209 }
210 else if ((geom = DRW_cache_mesh_loose_edges_get(ob_ref.object))) {
211 coloring.mesh_all_edges_ps_->draw(
212 geom, manager.unique_handle(ob_ref), res.select_id(ob_ref).get());
213 }
214 }
215 break;
216 }
217 case OB_POINTCLOUD: {
218 if (show_surface_wire) {
219 ResourceHandleRange res_handle = manager.unique_handle(ob_ref);
220 gpu::Batch *geom_pointcloud = DRW_pointcloud_batch_cache_get_dots(ob_ref.object);
221 gpu::Batch *geom_gsplat = DRW_gsplat_batch_cache_get_dots(ob_ref.object);
222 if (geom_pointcloud) {
223 coloring.pointcloud_ps_->draw(
224 geom_pointcloud, manager.unique_handle(ob_ref), res.select_id(ob_ref).get());
225 }
226 if (geom_gsplat) {
227 draw::gsplat_sub_pass_setup(*coloring.gsplat_ps_, ob_ref, res_handle, nullptr);
228 coloring.gsplat_ps_->draw(
229 geom_gsplat, manager.unique_handle(ob_ref), res.select_id(ob_ref).get());
230 }
231 }
232 break;
233 }
234 case OB_VOLUME: {
235 if (show_surface_wire) {
236 gpu::Batch *geom = DRW_cache_volume_face_wireframe_get(ob_ref.object);
237 if (geom == nullptr) {
238 break;
239 }
240 if (DRW_object_get_data_for_drawing<Volume>(*ob_ref.object).display.wireframe_type ==
242 {
243 coloring.points_ps_->draw(
244 geom, manager.unique_handle(ob_ref), res.select_id(ob_ref).get());
245 }
246 else {
247 coloring.mesh_all_edges_ps_->draw(
248 geom, manager.unique_handle(ob_ref), res.select_id(ob_ref).get());
249 }
250 }
251 break;
252 }
253 default:
254 /* Would be good to have. */
255 // BLI_assert_unreachable();
256 break;
257 }
258 }
259
260 void pre_draw(Manager &manager, View &view) final
261 {
262 if (!enabled_) {
263 return;
264 }
265
266 manager.generate_commands(wireframe_ps_, view);
267 }
268
269 void copy_depth(TextureRef &depth_tx)
270 {
271 if (!enabled_ || !do_depth_copy_workaround_) {
272 return;
273 }
274
276 int2 render_size = int2(depth_tx.size());
277 tmp_depth_tx_.acquire_2d(render_size, gpu::TextureFormat::SFLOAT_32_DEPTH_UINT_8, usage);
278
279 /* WORKAROUND: Nasty framebuffer copy.
280 * We should find a way to have nice wireframe without this. */
281 GPU_texture_copy(tmp_depth_tx_, depth_tx);
282 }
283
284 void draw_line(Framebuffer &framebuffer, Manager &manager, View &view) final
285 {
286 if (!enabled_) {
287 return;
288 }
289
290 GPU_framebuffer_bind(framebuffer);
291 manager.submit_only(wireframe_ps_, view);
292
293 tmp_depth_tx_.release();
294 }
295
296 private:
297 float wire_discard_threshold_get(float threshold)
298 {
299 /* Use `sqrt` since the value stored in the edge is a variation of the cosine, so its square
300 * becomes more proportional with a variation of angle. */
301 threshold = sqrt(abs(threshold));
302 /* The maximum value (255 in the VBO) is used to force hide the edge. */
303 return math::interpolate(0.0f, 1.0f - (1.0f / 255.0f), threshold);
304 }
305
306 static bool mesh_edit_wires_overlap(const ObjectRef &ob_ref, const bool in_edit_mode)
307 {
308 if (!in_edit_mode) {
309 return false;
310 }
312 const Mesh *orig_edit_mesh = BKE_object_get_pre_modified_mesh(ob_ref.object);
313 const bool edit_mapping_valid = BKE_editmesh_eval_orig_map_available(mesh, orig_edit_mesh);
314 if (!edit_mapping_valid) {
315 /* The mesh edit mode overlay doesn't include wireframe for the evaluated mesh when it
316 * doesn't correspond with the original edit mesh. So the main wireframe overlay should draw
317 * wires for the evaluated mesh instead. */
318 return false;
319 }
321 /* If a cage exists, the edit overlay might not display every edge. */
322 return false;
323 }
324 /* The edit mode overlay displays all of the edges of the evaluated mesh; drawing the edges
325 * again would be redundant. */
326 return true;
327 }
328};
329
330} // namespace blender::draw::overlay
ResourceHandleRange unique_handle(const ObjectRef &ref)
int3 size(int miplvl=0) const
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::DrawMultiBuf > Sub
Definition draw_pass.hh:509
static bool mesh_has_edit_cage(const Object *ob)
void begin_sync(Resources &res, const State &state) final
void draw_line(Framebuffer &framebuffer, Manager &manager, View &view) final
void pre_draw(Manager &manager, View &view) final
void object_sync_ex(Manager &manager, const ObjectRef &ob_ref, Resources &res, const State &state, const bool in_edit_paint_mode, const bool in_edit_mode)
void copy_depth(TextureRef &depth_tx)
#define DRW_CLIPPING_UBO_SLOT
#define OVERLAY_GLOBALS_SLOT
#define abs
const int state
gpu::Batch * DRW_gsplat_batch_cache_get_dots(Object *ob)
gpu::Batch * DRW_cache_text_edge_wire_get(Object *ob)
gpu::Batch * DRW_cache_mesh_all_verts_get(Object *ob)
gpu::Batch * DRW_cache_grease_pencil_face_wireframe_get(const Scene *scene, Object *ob)
detail::Pass< command::DrawMultiBuf > PassMain
gpu::Batch * DRW_cache_mesh_loose_edges_get(Object *ob)
gpu::Batch * DRW_cache_mesh_face_wireframe_get(Object *ob)
Vector< SculptBatch > sculpt_batches_get(const Object *ob, SculptBatchFeature features)
gpu::Batch * DRW_pointcloud_batch_cache_get_dots(Object *ob)
gpu::Batch * DRW_cache_curve_edge_wire_get(Object *ob)
gpu::Batch * DRW_cache_surf_edge_wire_get(Object *ob)
gpu::Batch * DRW_cache_volume_face_wireframe_get(Object *ob)
gpu::Batch * gsplat_sub_pass_setup(PassMain::Sub &sub_ps, const ObjectRef &ob_ref, const ResourceHandleRange &res_handle, GPUMaterial *gpu_material=nullptr)
T interpolate(const T &a, const T &b, const FactorT &t)
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
UserDef U
Definition blender.cc:58
const Mesh * BKE_object_get_pre_modified_mesh(const Object *object)
Definition object.cc:4444
void GPU_texture_copy(gpu::Texture *dst, gpu::Texture *src)
Global G
Definition blender.cc:57
@ GPU_TEXTURE_USAGE_SHADER_READ
@ GPU_TEXTURE_USAGE_ATTACHMENT
T & DRW_object_get_data_for_drawing(const Object &object)
@ USER_GPU_FLAG_OVERLAY_SMOOTH_WIRE
sqrt(x)+1/max(0
@ DRW_STATE_FIRST_VERTEX_CONVENTION
Definition draw_state.hh:73
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
@ DRW_STATE_WRITE_DEPTH
Definition draw_state.hh:31
@ DRW_STATE_DEPTH_LESS_EQUAL
Definition draw_state.hh:40
bool BKE_editmesh_eval_orig_map_available(const Mesh &mesh_eval, const Mesh *mesh_orig)
Definition editmesh.cc:93
bool BKE_sculptsession_use_pbvh_draw(const Object *ob, const RegionView3D *rv3d)
@ VOLUME_WIREFRAME_POINTS
@ G_TRANSFORM_OBJ
eObject_DrawExtraFlag dtx
const ID select_id(const ObjectRef &ob_ref, uint sub_object_id=0)