Blender V5.3
overlay_curve.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_attribute.hh"
12
13#include "DNA_curves_types.h"
14
15#include "GPU_capabilities.hh"
16
17#include "draw_cache.hh"
18#include "draw_cache_impl.hh"
19
20#include "overlay_base.hh"
21
22namespace blender::draw::overlay {
23
27class Curves : Overlay {
28 private:
29 PassSimple edit_curves_ps_ = {"Curve Edit"};
30 PassSimple::Sub *edit_curves_lines_ = nullptr;
31
32 PassSimple edit_curves_handles_ps_ = {"Curve Edit Handles"};
33 PassSimple::Sub *edit_curves_points_ = nullptr;
34 PassSimple::Sub *edit_curves_handles_ = nullptr;
35
36 PassSimple edit_legacy_curve_ps_ = {"Legacy Curve Edit"};
37 PassSimple::Sub *edit_legacy_curve_wires_ = nullptr;
38 PassSimple::Sub *edit_legacy_curve_normals_ = nullptr;
39
40 PassSimple edit_legacy_curve_handles_ps_ = {"Legacy Curve Edit Handles"};
41 PassSimple::Sub *edit_legacy_curve_points_ = nullptr;
42 PassSimple::Sub *edit_legacy_curve_handles_ = nullptr;
43
44 PassSimple edit_legacy_surface_handles_ps = {"Surface Edit"};
45 PassSimple::Sub *edit_legacy_surface_handles_ = nullptr;
46 /* Handles that are below the geometry and are rendered with lower alpha. */
47 PassSimple::Sub *edit_legacy_surface_xray_handles_ = nullptr;
48
49 /* TODO(fclem): This is quite wasteful and expensive, prefer in shader Z modification like the
50 * retopology offset. */
51 View view_edit_cage = {"view_edit_cage"};
52 View::OffsetData offset_data_;
53
54 public:
55 void begin_sync(Resources &res, const State &state) final
56 {
57 enabled_ = state.is_space_v3d();
58
59 if (!enabled_) {
60 return;
61 }
62
63 offset_data_ = state.offset_data_get();
64
65 {
66 auto &pass = edit_curves_ps_;
67 pass.init();
68 pass.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
69 pass.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
70 {
71 auto &sub = pass.sub("Lines");
74 state.clipping_plane_count);
75 sub.shader_set(res.shaders->curve_edit_line.get());
76 sub.bind_texture("weight_tx", &res.weight_ramp_tx);
77 sub.push_constant("use_weight", false);
78 sub.push_constant("use_grease_pencil", false);
79 sub.push_constant("skip_unselected", false);
80 edit_curves_lines_ = ⊂
81 }
82 }
83
84 {
85 auto &pass = edit_curves_handles_ps_;
86 pass.init();
87 pass.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
88 pass.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
90 drw_state |= state.xray_flag_enabled ? DRW_STATE_DEPTH_ALWAYS : DRW_STATE_DEPTH_LESS_EQUAL;
91 {
92 auto &sub = pass.sub("Handles");
93 sub.state_set(drw_state, state.clipping_plane_count);
94 sub.shader_set(res.shaders->curve_edit_handles.get());
95 sub.push_constant("curve_handle_display", int(state.overlay.handle_display));
96 edit_curves_handles_ = ⊂
97 }
98 {
99 auto &sub = pass.sub("Points");
100 sub.state_set(drw_state, state.clipping_plane_count);
101 sub.shader_set(res.shaders->curve_edit_points.get());
102 sub.bind_texture("weight_tx", &res.weight_ramp_tx);
103 sub.push_constant("use_weight", false);
104 sub.push_constant("use_grease_pencil", false);
105 sub.push_constant("do_stroke_endpoints", false);
106 sub.push_constant("curve_handle_display", int(state.overlay.handle_display));
107 edit_curves_points_ = ⊂
108 }
109 }
110
111 const bool show_normals = (state.overlay.edit_flag & V3D_OVERLAY_EDIT_CU_NORMALS);
112 const bool use_hq_normals = (state.scene->r.perf_flag & SCE_PERF_HQ_NORMALS) ||
114
115 {
116 auto &pass = edit_legacy_curve_ps_;
117 pass.init();
118 pass.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
119 pass.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
120 {
121 auto &sub = pass.sub("Wires");
123 state.clipping_plane_count);
124 sub.shader_set(res.shaders->legacy_curve_edit_wires.get());
125 sub.push_constant("normal_size", 0.0f);
126 edit_legacy_curve_wires_ = ⊂
127 }
128 if (show_normals) {
129 auto &sub = pass.sub("Normals");
131 state.clipping_plane_count);
132 sub.shader_set(res.shaders->legacy_curve_edit_normals.get());
133 sub.push_constant("normal_size", state.overlay.normals_length);
134 sub.push_constant("use_hq_normals", use_hq_normals);
135 edit_legacy_curve_normals_ = ⊂
136 }
137 else {
138 edit_legacy_curve_normals_ = nullptr;
139 }
140 }
141
142 {
143 auto &pass = edit_legacy_curve_handles_ps_;
144 pass.init();
145 pass.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
146 pass.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
147 {
148 auto &sub = pass.sub("Handles");
149 sub.state_set(DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_ALPHA, state.clipping_plane_count);
150 sub.shader_set(res.shaders->legacy_curve_edit_handles.get());
151 sub.push_constant("show_curve_handles", state.overlay.handle_display != CURVE_HANDLE_NONE);
152 sub.push_constant("curve_handle_display", int(state.overlay.handle_display));
153 sub.push_constant("alpha", 1.0f);
154 edit_legacy_curve_handles_ = ⊂
155 }
156 /* Points need to be rendered after handles. */
157 {
158 auto &sub = pass.sub("Points");
159 sub.state_set(DRW_STATE_WRITE_COLOR, state.clipping_plane_count);
160 sub.shader_set(res.shaders->legacy_curve_edit_points.get());
161 sub.push_constant("show_curve_handles", state.overlay.handle_display != CURVE_HANDLE_NONE);
162 sub.push_constant("curve_handle_display", int(state.overlay.handle_display));
163 sub.push_constant("use_grease_pencil", false);
164 sub.push_constant("do_stroke_endpoints", false);
165 edit_legacy_curve_points_ = ⊂
166 }
167 }
168
169 {
170 auto &pass = edit_legacy_surface_handles_ps;
171 pass.init();
172 pass.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
173 pass.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
174
175 auto create_sub = [&](const char *name, DRWState drw_state, float alpha) {
176 auto &sub = pass.sub(name);
177 sub.state_set(drw_state, state.clipping_plane_count);
178 sub.shader_set(res.shaders->legacy_curve_edit_handles.get());
179 sub.push_constant("show_curve_handles", state.overlay.handle_display != CURVE_HANDLE_NONE);
180 sub.push_constant("curve_handle_display", int(state.overlay.handle_display));
181 sub.push_constant("alpha", alpha);
182 return ⊂
183 };
184
187 edit_legacy_surface_xray_handles_ = create_sub("SurfaceXrayHandles", state_xray, 0.2f);
188
191 edit_legacy_surface_handles_ = create_sub("SurfaceHandles", state_front, 1.0f);
192 }
193 }
194
196 const ObjectRef &ob_ref,
197 Resources & /*res*/,
198 const State & /*state*/) final
199 {
200 if (!enabled_) {
201 return;
202 }
203
204 Object *ob = ob_ref.object;
206 const bool show_points = bke::AttrDomain(curves.selection_domain) == bke::AttrDomain::Point;
207
208 if (show_points) {
210 edit_curves_points_->draw(geom, manager.unique_handle(ob_ref));
211 }
212 {
214 edit_curves_handles_->draw_expand(geom, GPU_PRIM_TRIS, 8, 1, manager.unique_handle(ob_ref));
215 }
216 {
218 edit_curves_lines_->draw(geom, manager.unique_handle(ob_ref));
219 }
220 }
221
222 /* Used for legacy curves. */
223 void edit_object_sync_legacy(Manager &manager, const ObjectRef &ob_ref, Resources & /*res*/)
224 {
225 if (!enabled_) {
226 return;
227 }
228
229 ResourceHandleRange res_handle = manager.unique_handle(ob_ref);
230
231 Object *ob = ob_ref.object;
233
234 if (ob->type == OB_CURVES_LEGACY) {
235 gpu::Batch *geom = DRW_cache_curve_edge_wire_get(ob);
236 edit_legacy_curve_wires_->draw(geom, res_handle);
237 }
238 if (edit_legacy_curve_normals_ && (curve.flag & CU_3D)) {
239 gpu::Batch *geom = DRW_cache_curve_edge_normal_get(ob);
240 edit_legacy_curve_normals_->draw_expand(geom, GPU_PRIM_LINES, 2, 1, res_handle);
241 }
242 {
243 gpu::Batch *geom = DRW_cache_curve_edge_overlay_get(ob);
244 if (ob->type == OB_CURVES_LEGACY) {
245 edit_legacy_curve_handles_->draw_expand(geom, GPU_PRIM_TRIS, 8, 1, res_handle);
246 }
247 else {
248 edit_legacy_surface_xray_handles_->draw_expand(geom, GPU_PRIM_TRIS, 8, 1, res_handle);
249 edit_legacy_surface_handles_->draw_expand(geom, GPU_PRIM_TRIS, 8, 1, res_handle);
250 }
251 }
252 {
253 gpu::Batch *geom = DRW_cache_curve_vert_overlay_get(ob);
254 edit_legacy_curve_points_->draw(geom, res_handle);
255 }
256 }
257
258 void draw_line(Framebuffer &framebuffer, Manager &manager, View &view) final
259 {
260 if (!enabled_) {
261 return;
262 }
263
264 view_edit_cage.sync(view.viewmat(), offset_data_.winmat_polygon_offset(view.winmat(), 0.5f));
265
266 GPU_framebuffer_bind(framebuffer);
267 manager.submit(edit_legacy_curve_ps_, view);
268 manager.submit(edit_curves_ps_, view);
269 manager.submit(edit_curves_handles_ps_, view_edit_cage);
270 manager.submit(edit_legacy_surface_handles_ps, view);
271 }
272
273 void draw_color_only(Framebuffer &framebuffer, Manager &manager, View &view) final
274 {
275 if (!enabled_) {
276 return;
277 }
278
279 view_edit_cage.sync(view.viewmat(), offset_data_.winmat_polygon_offset(view.winmat(), 0.5f));
280
281 GPU_framebuffer_bind(framebuffer);
282 manager.submit(edit_legacy_curve_handles_ps_, view);
283 }
284};
285
286} // namespace blender::draw::overlay
ResourceHandleRange unique_handle(const ObjectRef &ref)
detail::PassBase< command::DrawCommandBuf > Sub
Definition draw_pass.hh:509
void edit_object_sync_legacy(Manager &manager, const ObjectRef &ob_ref, Resources &)
void begin_sync(Resources &res, const State &state) final
void draw_line(Framebuffer &framebuffer, Manager &manager, View &view) final
void draw_color_only(Framebuffer &framebuffer, Manager &manager, View &view) final
void edit_object_sync(Manager &manager, const ObjectRef &ob_ref, Resources &, const State &) final
#define DRW_CLIPPING_UBO_SLOT
#define OVERLAY_GLOBALS_SLOT
const int state
gpu::Batch * DRW_curves_batch_cache_get_edit_points(Curves *curves)
detail::Pass< command::DrawCommandBuf > PassSimple
gpu::Batch * DRW_curves_batch_cache_get_edit_curves_handles(Curves *curves)
gpu::Batch * DRW_cache_curve_edge_overlay_get(Object *ob)
gpu::Batch * DRW_cache_curve_edge_wire_get(Object *ob)
gpu::Batch * DRW_cache_curve_edge_normal_get(Object *ob)
gpu::Batch * DRW_curves_batch_cache_get_edit_curves_lines(Curves *curves)
gpu::Batch * DRW_cache_curve_vert_overlay_get(Object *ob)
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
bool GPU_use_hq_normals_workaround()
T & DRW_object_get_data_for_drawing(const Object &object)
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
@ DRW_STATE_BLEND_ALPHA
Definition draw_state.hh:57
@ DRW_STATE_WRITE_DEPTH
Definition draw_state.hh:31
@ DRW_STATE_DEPTH_ALWAYS
Definition draw_state.hh:38
@ DRW_STATE_DEPTH_LESS_EQUAL
Definition draw_state.hh:40
@ DRW_STATE_DEPTH_GREATER
Definition draw_state.hh:42
@ V3D_OVERLAY_EDIT_CU_NORMALS