Blender V5.3
overlay_outline.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 "overlay_base.hh"
13
14#include "draw_common.hh"
15
16#include "DNA_userdef_types.h"
17
18namespace blender::draw::overlay {
19
25 private:
26 /* Simple render pass that renders an object ID pass. */
27 PassMain outline_prepass_ps_ = {"Prepass"};
28 PassMain::Sub *prepass_curves_ps_ = nullptr;
29 PassMain::Sub *prepass_pointcloud_ps_ = nullptr;
30 PassMain::Sub *prepass_gsplat_ps_ = nullptr;
31 PassMain::Sub *prepass_gpencil_ps_ = nullptr;
32 PassMain::Sub *prepass_mesh_ps_ = nullptr;
33 PassMain::Sub *prepass_volume_ps_ = nullptr;
34 PassMain::Sub *prepass_wire_ps_ = nullptr;
35 /* Detect edges inside the ID pass and output color for each of them. */
36 PassSimple outline_resolve_ps_ = {"Resolve"};
37
38 TextureFromPool object_id_tx_ = {"outline_ob_id_tx"};
39 TextureFromPool tmp_depth_tx_ = {"outline_depth_tx"};
40
41 Framebuffer prepass_fb_ = {"outline.prepass_fb"};
42
43 Vector<FlatObjectRef> flat_objects_;
44
45 PassMain outline_prepass_flat_ps_ = {"PrepassFlat"};
46
47 public:
48 void begin_sync(Resources &res, const State &state) final
49 {
50 enabled_ = !res.is_selection();
51 enabled_ &= state.v3d && (state.v3d_flag & V3D_SELECT_OUTLINE);
52
53 flat_objects_.clear();
54
55 if (!enabled_) {
56 return;
57 }
58
59 const float outline_width = ui::theme::get_value_f(TH_OUTLINE_WIDTH);
60 const bool do_smooth_lines = (U.gpu_flag & USER_GPU_FLAG_OVERLAY_SMOOTH_WIRE) != 0;
61 const bool do_expand = (U.pixelsize > 1.0) || (outline_width > 2.0f);
62 const bool is_transform = (G.moving & G_TRANSFORM_OBJ) != 0;
63
64 {
65 auto &pass = outline_prepass_ps_;
66 pass.init();
67 pass.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
68 pass.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
69 pass.framebuffer_set(&prepass_fb_);
70 pass.clear_color_depth_stencil(float4(0.0f), 1.0f, 0x0);
72 state.clipping_plane_count);
73 {
74 auto &sub = pass.sub("Curves");
75 sub.shader_set(res.shaders->outline_prepass_curves.get());
76 sub.push_constant("is_transform", is_transform);
77 prepass_curves_ps_ = &sub;
78 }
79 {
80 auto &sub = pass.sub("PointCloud");
81 sub.shader_set(res.shaders->outline_prepass_pointcloud.get());
82 sub.push_constant("is_transform", is_transform);
83 prepass_pointcloud_ps_ = &sub;
84 }
85 {
86 auto &sub = pass.sub("GSplat");
87 sub.shader_set(res.shaders->outline_prepass_gsplat.get());
88 sub.push_constant("is_transform", is_transform);
89 prepass_gsplat_ps_ = &sub;
90 }
91 {
92 auto &sub = pass.sub("GreasePencil");
93 sub.shader_set(res.shaders->outline_prepass_gpencil.get());
94 sub.push_constant("is_transform", is_transform);
95 prepass_gpencil_ps_ = &sub;
96 }
97 {
98 auto &sub = pass.sub("Mesh");
99 sub.shader_set(res.shaders->outline_prepass_mesh.get());
100 sub.push_constant("is_transform", is_transform);
101 prepass_mesh_ps_ = &sub;
102 }
103 {
104 auto &sub = pass.sub("Volume");
105 sub.shader_set(res.shaders->outline_prepass_mesh.get());
106 sub.push_constant("is_transform", is_transform);
107 prepass_volume_ps_ = &sub;
108 }
109 {
110 auto &sub = pass.sub("Wire");
111 sub.shader_set(res.shaders->outline_prepass_wire.get());
112 sub.push_constant("is_transform", is_transform);
113 prepass_wire_ps_ = &sub;
114 }
115 }
116 {
117 auto &pass = outline_resolve_ps_;
118 pass.init();
120 pass.shader_set(res.shaders->outline_detect.get());
121 /* Don't occlude the outline if in xray mode as it causes too much flickering. */
122 pass.push_constant("alpha_occlu", state.xray_enabled ? 1.0f : 0.35f);
123 pass.push_constant("do_thick_outlines", do_expand);
124 pass.push_constant("do_anti_aliasing", do_smooth_lines);
125 pass.push_constant("is_xray_wires", state.xray_enabled_and_not_wire);
126 pass.bind_texture("outline_id_tx", &object_id_tx_);
127 pass.bind_texture("scene_depth_tx", &res.depth_tx);
128 pass.bind_texture("outline_depth_tx", &tmp_depth_tx_);
129 pass.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
130 pass.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
131 pass.draw_procedural(GPU_PRIM_TRIS, 1, 3);
132 }
133 }
134
135 void object_sync(Manager &manager,
136 const ObjectRef &ob_ref,
137 Resources &res,
138 const State &state) final
139 {
140 if (!enabled_) {
141 return;
142 }
143
144 /* Outlines of bounding boxes are not drawn. */
145 if (ob_ref.object->dt == OB_BOUNDBOX) {
146 return;
147 }
148
149 gpu::Batch *geom;
150 switch (ob_ref.object->type) {
151 case OB_CURVES: {
152 const char *error = nullptr;
153 /* The error string will always have been printed by the engine already.
154 * No need to display it twice. */
155 geom = curves_sub_pass_setup(*prepass_curves_ps_, state.scene, ob_ref.object, error);
156 prepass_curves_ps_->draw(geom, manager.unique_handle(ob_ref));
157 break;
158 }
159 case OB_GREASE_PENCIL:
161 res, *prepass_gpencil_ps_, state.scene, ob_ref.object, manager.unique_handle(ob_ref));
162 break;
163 case OB_MESH:
164 if (state.xray_enabled_and_not_wire) {
165 geom = DRW_cache_mesh_edge_detection_get(ob_ref.object, nullptr);
166 prepass_wire_ps_->draw_expand(geom, GPU_PRIM_LINES, 1, 1, manager.unique_handle(ob_ref));
167 }
168 else {
169 geom = DRW_cache_mesh_surface_get(ob_ref.object);
170 prepass_mesh_ps_->draw(geom, manager.unique_handle(ob_ref));
171
172 /* Display flat object as a line when view is orthogonal to them.
173 * This fixes only the biggest case which is a plane in ortho view. */
174 int flat_axis = FlatObjectRef::flat_axis_index_get(ob_ref);
175 if (flat_axis != -1) {
176 geom = DRW_cache_mesh_edge_detection_get(ob_ref.object, nullptr);
177 flat_objects_.append({geom, manager.unique_handle(ob_ref), flat_axis});
178 }
179 }
180 break;
181 case OB_POINTCLOUD:
182 /* Looks bad in wireframe mode. Could be relaxed if we draw a wireframe of some sort in
183 * the future. */
184 if (!state.is_wireframe_mode) {
185 PassMain::Sub *ps = pointcloud_is_gsplat(ob_ref.object) ? prepass_gsplat_ps_ :
186 prepass_pointcloud_ps_;
187 ResourceHandleRange res_handle = manager.unique_handle(ob_ref);
188 geom = pointcloud_sub_pass_setup(*ps, ob_ref, res_handle);
189 ps->draw(geom, manager.unique_handle(ob_ref));
190 }
191 break;
192 case OB_VOLUME:
193 geom = DRW_cache_volume_selection_surface_get(ob_ref.object);
194 /* TODO(fclem): Get rid of these check and enforce correct API on the batch cache. */
195 if (geom) {
196 prepass_volume_ps_->draw(geom, manager.unique_handle(ob_ref));
197 }
198 break;
199 default:
200 break;
201 }
202 }
203
204 /* Flat objects outline workaround need to generate passes for each redraw. */
205 void flat_objects_pass_sync(Manager &manager, View &view, Resources &res, const State &state)
206 {
207 outline_prepass_flat_ps_.init();
208
209 if (!enabled_) {
210 return;
211 }
212
213 if (!view.is_persp()) {
214 const bool is_transform = (G.moving & G_TRANSFORM_OBJ) != 0;
215 /* Note: We need a dedicated pass since we have to populated it for each redraw. */
216 auto &pass = outline_prepass_flat_ps_;
217 pass.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
218 pass.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
219 pass.framebuffer_set(&prepass_fb_);
221 state.clipping_plane_count);
222 pass.shader_set(res.shaders->outline_prepass_wire.get());
223 pass.push_constant("is_transform", is_transform);
224
225 for (FlatObjectRef flag_ob_ref : flat_objects_) {
226 flag_ob_ref.if_flat_axis_orthogonal_to_view(
227 manager, view, [&](gpu::Batch *geom, ResourceID resource_id) {
228 pass.draw_expand(geom, GPU_PRIM_LINES, 1, 1, resource_id);
229 });
230 }
231 }
232 }
233
234 void pre_draw(Manager &manager, View &view) final
235 {
236 if (!enabled_) {
237 return;
238 }
239
240 manager.generate_commands(outline_prepass_ps_, view);
241 manager.generate_commands(outline_prepass_flat_ps_, view);
242 }
243
244 /* TODO(fclem): Remove dependency on Resources. */
245 void draw_line_only_ex(Framebuffer &framebuffer, Resources &res, Manager &manager, View &view)
246 {
247 if (!enabled_) {
248 return;
249 }
250
251 GPU_debug_group_begin("Outline");
252
253 int2 render_size = int2(res.depth_tx.size());
254
256 tmp_depth_tx_.acquire_2d(render_size, gpu::TextureFormat::SFLOAT_32_DEPTH_UINT_8, usage);
257 object_id_tx_.acquire_2d(render_size, gpu::TextureFormat::UINT_16, usage);
258
259 prepass_fb_.ensure(GPU_ATTACHMENT_TEXTURE(tmp_depth_tx_),
260 GPU_ATTACHMENT_TEXTURE(object_id_tx_));
261
262 manager.submit_only(outline_prepass_ps_, view);
263 manager.submit_only(outline_prepass_flat_ps_, view);
264
265 GPU_framebuffer_bind(framebuffer);
266 manager.submit(outline_resolve_ps_, view);
267
268 tmp_depth_tx_.release();
269 object_id_tx_.release();
270
272 }
273};
274
275} // namespace blender::draw::overlay
#define GPU_ATTACHMENT_TEXTURE(_texture)
void submit_only(PassMain &pass, View &view)
void submit(PassSimple &pass, View &view)
int3 size(int miplvl=0) const
bool is_persp(int view_id=0) const
Definition draw_view.hh:93
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 void draw_grease_pencil(Resources &res, PassMain::Sub &pass, const Scene *scene, Object *ob, ResourceHandleRange res_handle, select::ID select_id=select::SelectMap::select_invalid_id())
void object_sync(Manager &manager, const ObjectRef &ob_ref, Resources &res, const State &state) final
void flat_objects_pass_sync(Manager &manager, View &view, Resources &res, const State &state)
void pre_draw(Manager &manager, View &view) final
void begin_sync(Resources &res, const State &state) final
void draw_line_only_ex(Framebuffer &framebuffer, Resources &res, Manager &manager, View &view)
#define DRW_CLIPPING_UBO_SLOT
#define OVERLAY_GLOBALS_SLOT
const int state
detail::Pass< command::DrawCommandBuf > PassSimple
detail::Pass< command::DrawMultiBuf > PassMain
gpu::Batch * curves_sub_pass_setup(PassMain::Sub &ps, const Scene *scene, Object *ob, const char *&r_error, GPUMaterial *gpu_material=nullptr)
bool pointcloud_is_gsplat(Object *ob)
gpu::Batch * DRW_cache_mesh_edge_detection_get(Object *ob, bool *r_is_manifold)
gpu::Batch * pointcloud_sub_pass_setup(PassMain::Sub &sub_ps, const ObjectRef &ob_ref, const ResourceHandleRange &res_handle, GPUMaterial *gpu_material=nullptr)
gpu::Batch * DRW_cache_volume_selection_surface_get(Object *ob)
gpu::Batch * DRW_cache_mesh_surface_get(Object *ob)
float get_value_f(int colorid)
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
UserDef U
Definition blender.cc:58
Global G
Definition blender.cc:57
@ GPU_TEXTURE_USAGE_SHADER_READ
@ GPU_TEXTURE_USAGE_ATTACHMENT
@ USER_GPU_FLAG_OVERLAY_SMOOTH_WIRE
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
@ DRW_STATE_WRITE_DEPTH
Definition draw_state.hh:31
@ DRW_STATE_BLEND_ALPHA_PREMUL
Definition draw_state.hh:59
@ DRW_STATE_DEPTH_LESS_EQUAL
Definition draw_state.hh:40
void GPU_debug_group_end()
Definition gpu_debug.cc:34
@ G_TRANSFORM_OBJ
void GPU_debug_group_begin(const char *name)
Definition gpu_debug.cc:23
static int flat_axis_index_get(const ObjectRef &ob_ref)
draw::UniformArrayBuffer< float4, 6 > clip_planes_buf
draw::UniformBuffer< UniformData > globals_buf