Blender V5.3
select_engine.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "DNA_userdef_types.h"
12
13#include "BKE_editmesh.hh"
14#include "BKE_mesh_types.hh"
15#include "BLI_math_matrix_c.hh"
16
17#include "BLT_translation.hh"
18
20#include "DRW_render.hh"
21#include "ED_view3d.hh"
22
23#include "RE_engine.h"
24
25#include "DRW_engine.hh"
26#include "DRW_select_buffer.hh"
27
28#include "draw_cache_impl.hh"
29#include "draw_common_c.hh"
31#include "draw_manager.hh"
32#include "draw_pass.hh"
33#include "draw_view_data.hh"
34
36
37#include "select_engine.hh"
38
39namespace blender {
40
42
43/* -------------------------------------------------------------------- */
46
47#define USE_CAGE_OCCLUSION
48
49struct Instance : public DrawEngine {
50 private:
51 PassSimple depth_only_ps = {"depth_only_ps"};
52 PassSimple::Sub *depth_only = nullptr;
53 PassSimple::Sub *depth_occlude = nullptr;
54
55 PassSimple select_edge_ps = {"select_id_edge_ps"};
56 PassSimple::Sub *select_edge = nullptr;
57
58 PassSimple select_id_vert_ps = {"select_id_vert_ps"};
59 PassSimple::Sub *select_vert = nullptr;
60
61 PassSimple select_face_ps = {"select_id_face_ps"};
62 PassSimple::Sub *select_face_uniform = nullptr;
63 PassSimple::Sub *select_face_flat = nullptr;
64
65 View view_faces = {"view_faces"};
66 View view_edges = {"view_edges"};
67 View view_verts = {"view_verts"};
68
69 UniformArrayBuffer<float4, 6> clip_planes_buf;
70
71 const DRWContext *draw_ctx = nullptr;
72
73 public:
92
94 {
95 return "SelectID";
96 }
97
98 void init() final
99 {
100 this->draw_ctx = DRW_context_get();
101 StaticData &e_data = StaticData::get();
102 GPUShaderConfig sh_cfg = RV3D_CLIPPING_ENABLED(draw_ctx->v3d, draw_ctx->rv3d) ?
105
106 StaticData::Shaders *sh_data = &e_data.sh_data[sh_cfg];
107
108 /* Prepass */
109 if (!sh_data->select_id_flat) {
111 sh_cfg == GPU_SHADER_CFG_CLIPPED ? "select_id_flat_clipped" : "select_id_flat");
112 }
113 if (!sh_data->select_id_uniform) {
115 sh_cfg == GPU_SHADER_CFG_CLIPPED ? "select_id_uniform_clipped" : "select_id_uniform");
116 }
117 }
118
120 {
121 StaticData &e_data = StaticData::get();
122 GPUShaderConfig sh_cfg = RV3D_CLIPPING_ENABLED(draw_ctx->v3d, draw_ctx->rv3d) ?
125
126 StaticData::Shaders *sh = &e_data.sh_data[sh_cfg];
127
128 if (e_data.context.select_mode == -1) {
129 e_data.context.select_mode = get_object_select_mode(draw_ctx->scene, draw_ctx->obact);
130 BLI_assert(e_data.context.select_mode != 0);
131 }
132
134 if (RV3D_CLIPPING_ENABLED(draw_ctx->v3d, draw_ctx->rv3d)) {
136 }
137
138 if (draw_ctx->v3d->shading.flag & V3D_SHADING_BACKFACE_CULLING &&
139 draw_ctx->v3d->shading.type == OB_SOLID)
140 {
142 }
143
144 bool retopology_occlusion = RETOPOLOGY_ENABLED(draw_ctx->v3d) && !XRAY_ENABLED(draw_ctx->v3d);
145 float retopology_offset = RETOPOLOGY_OFFSET(draw_ctx->v3d);
146
147 for (int i : IndexRange(6)) {
148 clip_planes_buf[i] = float4(0);
149 }
150
151 /* Note there might be less than 6 planes, but we always compute the 6 of them for simplicity.
152 */
153 int clipping_plane_count = RV3D_CLIPPING_ENABLED(draw_ctx->v3d, draw_ctx->rv3d) ? 6 : 0;
154 int plane_len = min((RV3D_LOCK_FLAGS(draw_ctx->rv3d) & RV3D_BOXCLIP) ? 4 : 6,
155 clipping_plane_count);
156
157 for (auto i : IndexRange(plane_len)) {
158 clip_planes_buf[i] = draw_ctx->rv3d->clip[i];
159 }
160
161 clip_planes_buf.push_update();
162
163 {
164 depth_only_ps.init();
165 depth_only_ps.state_set(state, clipping_plane_count);
166 depth_only_ps.bind_ubo(DRW_CLIPPING_UBO_SLOT, clip_planes_buf);
167 depth_only = nullptr;
168 depth_occlude = nullptr;
169 {
170 auto &sub = depth_only_ps.sub("DepthOnly");
171 sub.shader_set(sh->select_id_uniform);
172 sub.push_constant("retopology_offset", retopology_offset);
173 sub.push_constant("select_id", 0);
174 depth_only = &sub;
175 }
176 if (retopology_occlusion) {
177 auto &sub = depth_only_ps.sub("Occlusion");
178 sub.shader_set(sh->select_id_uniform);
179 sub.push_constant("retopology_offset", 0.0f);
180 sub.push_constant("select_id", 0);
181 depth_occlude = &sub;
182 }
183
184 select_face_ps.init();
185 select_face_ps.state_set(state, clipping_plane_count);
186 select_face_ps.bind_ubo(DRW_CLIPPING_UBO_SLOT, clip_planes_buf);
187 select_face_uniform = nullptr;
188 select_face_flat = nullptr;
189 if (e_data.context.select_mode & SCE_SELECT_FACE) {
190 auto &sub = select_face_ps.sub("Face");
191 const float vertex_size = U.pixelsize * draw::overlay::Resources::vertex_size_get();
192 sub.shader_set(sh->select_id_flat);
193 sub.push_constant("vertex_size", float(2 * vertex_size));
194 sub.push_constant("retopology_offset", retopology_offset);
195 select_face_flat = &sub;
196 }
197 else {
198 auto &sub = select_face_ps.sub("FaceNoSelect");
199 sub.shader_set(sh->select_id_uniform);
200 sub.push_constant("select_id", 0);
201 sub.push_constant("retopology_offset", retopology_offset);
202 select_face_uniform = &sub;
203 }
204
205 select_edge_ps.init();
206 select_edge_ps.bind_ubo(DRW_CLIPPING_UBO_SLOT, clip_planes_buf);
207 select_edge = nullptr;
208 if (e_data.context.select_mode & SCE_SELECT_EDGE) {
209 auto &sub = select_edge_ps.sub("Sub");
210 sub.state_set(state | DRW_STATE_FIRST_VERTEX_CONVENTION, clipping_plane_count);
211 sub.shader_set(sh->select_id_flat);
212 sub.push_constant("retopology_offset", retopology_offset);
213 select_edge = &sub;
214 }
215
216 select_id_vert_ps.init();
217 select_id_vert_ps.bind_ubo(DRW_CLIPPING_UBO_SLOT, clip_planes_buf);
218 select_vert = nullptr;
220 const float vertex_size = U.pixelsize * draw::overlay::Resources::vertex_size_get();
221 auto &sub = select_id_vert_ps.sub("Sub");
222 sub.state_set(state, clipping_plane_count);
223 sub.shader_set(sh->select_id_flat);
224 sub.push_constant("vertex_size", float(2 * vertex_size));
225 sub.push_constant("retopology_offset", retopology_offset);
226 select_vert = &sub;
227 }
228 }
229
230 e_data.context.elem_ranges.clear();
231
232 e_data.context.persmat = float4x4(draw_ctx->rv3d->persmat);
233 e_data.context.max_index_drawn_len = 1;
234 framebuffer_setup();
236 GPU_framebuffer_clear_color_depth(e_data.framebuffer_select_id, {0.0, 0.0, 0.0, 0.0}, 1.0f);
237 }
238
240 const BMesh *bm,
241 ResourceHandleRange res_handle,
242 short select_mode,
243 bool draw_facedot,
244 const uint initial_index)
245 {
246 using namespace blender::draw;
248
249 ElemIndexRanges ranges{};
250 ranges.total = IndexRange::from_begin_size(initial_index, 0);
251
253
254 if (select_mode & SCE_SELECT_FACE) {
255 ranges.face = alloc_range(ranges.total, bm->totface);
256
258 PassSimple::Sub *face_sub = select_face_flat;
259 face_sub->push_constant("offset", int(ranges.face.start()));
260 face_sub->draw(geom_faces, res_handle);
261
262 if (draw_facedot) {
263 gpu::Batch *geom_facedots = DRW_mesh_batch_cache_get_facedots_with_select_id(mesh);
264 face_sub->draw(geom_facedots, res_handle);
265 }
266 }
267 else {
268 if (ob->dt >= OB_SOLID) {
269#ifdef USE_CAGE_OCCLUSION
271#else
272 gpu::Batch *geom_faces = DRW_mesh_batch_cache_get_surface(mesh);
273#endif
274 select_face_uniform->draw(geom_faces, res_handle);
275 }
276 }
277
278 /* Unlike faces, only draw edges if edge select mode. */
279 if (select_mode & SCE_SELECT_EDGE) {
280 ranges.edge = alloc_range(ranges.total, bm->totedge);
281
282 gpu::Batch *geom_edges = DRW_mesh_batch_cache_get_edges_with_select_id(mesh);
283 select_edge->push_constant("offset", int(ranges.edge.start()));
284 select_edge->draw(geom_edges, res_handle);
285 }
286
287 /* Unlike faces, only verts if vert select mode. */
288 if (select_mode & SCE_SELECT_VERTEX) {
289 ranges.vert = alloc_range(ranges.total, bm->totvert);
290
291 gpu::Batch *geom_verts = DRW_mesh_batch_cache_get_verts_with_select_id(mesh);
292 select_vert->push_constant("offset", int(ranges.vert.start()));
293 select_vert->draw(geom_verts, res_handle);
294 }
295 return ranges;
296 }
297
299 ResourceHandleRange res_handle,
300 short select_mode,
301 const uint initial_index)
302 {
303 using namespace blender::draw;
305
306 ElemIndexRanges ranges{};
307 ranges.total = IndexRange::from_begin_size(initial_index, 0);
308
310 if (select_mode & SCE_SELECT_FACE) {
311 ranges.face = alloc_range(ranges.total, mesh.faces_num);
312
313 select_face_flat->push_constant("offset", int(ranges.face.start()));
314 select_face_flat->draw(geom_faces, res_handle);
315 }
316 else {
317 /* Only draw faces to mask out verts, we don't want their selection ID's. */
318 select_face_uniform->draw(geom_faces, res_handle);
319 }
320
321 if (select_mode & SCE_SELECT_EDGE) {
322 ranges.edge = alloc_range(ranges.total, mesh.edges_num);
323
324 gpu::Batch *geom_edges = DRW_mesh_batch_cache_get_edges_with_select_id(mesh);
325 select_edge->push_constant("offset", int(ranges.edge.start()));
326 select_edge->draw(geom_edges, res_handle);
327 }
328
329 if (select_mode & SCE_SELECT_VERTEX) {
330 ranges.vert = alloc_range(ranges.total, mesh.verts_num);
331
332 gpu::Batch *geom_verts = DRW_mesh_batch_cache_get_verts_with_select_id(mesh);
333 select_vert->push_constant("offset", int(ranges.vert.start()));
334 select_vert->draw(geom_verts, res_handle);
335 }
336
337 return ranges;
338 }
339
341 View3D *v3d, Object *ob, ResourceHandleRange res_handle, short select_mode, uint index_start)
342 {
343 BLI_assert_msg(index_start > 0, "Index 0 is reserved for no selection");
344
345 switch (ob->type) {
346 case OB_MESH: {
347 const bool is_editmode = ob->mode == OB_MODE_EDIT;
348 /* NOTE: it's important to get the edit-mesh before modifiers have been applied
349 * because the evaluated mesh may not have an edit-mesh, see #138715.
350 * Match edit-mesh access from #mesh_render_data_create. */
351 const Mesh *orig_edit_mesh = is_editmode ? BKE_object_get_pre_modified_mesh(ob) : nullptr;
352 const BMesh *bm = orig_edit_mesh ? BKE_editmesh_bmesh_get(orig_edit_mesh) : nullptr;
353
354 if (bm) {
355 bool draw_facedot = check_ob_drawface_dot(select_mode, v3d, eDrawType(ob->dt));
356 return edit_mesh_sync(ob, bm, res_handle, select_mode, draw_facedot, index_start);
357 }
358 return mesh_sync(ob, res_handle, select_mode, index_start);
359 }
360 case OB_CURVES_LEGACY:
361 case OB_SURF:
362 break;
363 default:
364 break;
365 }
367 return ElemIndexRanges{};
368 }
369
370 void object_sync(ObjectRef &ob_ref, Manager &manager) final
371 {
372 Object *ob = ob_ref.object;
373 StaticData &e_data = StaticData::get();
374 SELECTID_Context &sel_ctx = e_data.context;
375
376 if (!sel_ctx.objects.contains(ob)) {
377 if (ob->dt >= OB_SOLID) {
378 /* This object is not selectable. It is here to participate in occlusion.
379 * This is the case in retopology mode. */
380 gpu::Batch *geom_faces = DRW_mesh_batch_cache_get_surface(
382
383 depth_occlude->draw(geom_faces, manager.unique_handle(ob_ref));
384 }
385 return;
386 }
387
388 /* Only sync selectable object once.
389 * This can happen in retopology mode where there is two sync loop. */
390 sel_ctx.elem_ranges.lookup_or_add_cb(ob, [&]() {
391 ResourceHandleRange res_handle = manager.unique_handle(ob_ref);
392 ElemIndexRanges elem_ranges = object_sync(
393 draw_ctx->v3d, ob, res_handle, sel_ctx.select_mode, sel_ctx.max_index_drawn_len);
394 sel_ctx.max_index_drawn_len = elem_ranges.total.one_after_last();
395 return elem_ranges;
396 });
397 }
398
399 void end_sync() final {}
400
401 void draw(Manager &manager) final
402 {
403 StaticData &e_data = StaticData::get();
404
406 {
407 View::OffsetData offset_data(*draw_ctx->rv3d);
408 /* Create view with depth offset */
409 const View &view = View::default_get();
410 view_faces.sync(view.viewmat(), view.winmat());
411 view_edges.sync(view.viewmat(), offset_data.winmat_polygon_offset(view.winmat(), 1.0f));
412 view_verts.sync(view.viewmat(), offset_data.winmat_polygon_offset(view.winmat(), 1.1f));
413 }
414
415 {
416 DefaultFramebufferList *dfbl = draw_ctx->viewport_framebuffer_list_get();
419 manager.submit(depth_only_ps, view_faces);
420 }
421
422 /* Setup framebuffer */
424
425 manager.submit(select_face_ps, view_faces);
426
427 if (e_data.context.select_mode & SCE_SELECT_EDGE) {
428 manager.submit(select_edge_ps, view_edges);
429 }
430
432 manager.submit(select_id_vert_ps, view_verts);
433 }
435 }
436
437 private:
438 void framebuffer_setup()
439 {
440 StaticData &e_data = StaticData::get();
442 int size[2];
443 size[0] = GPU_texture_width(dtxl->depth);
444 size[1] = GPU_texture_height(dtxl->depth);
445
446 if (e_data.framebuffer_select_id == nullptr) {
447 e_data.framebuffer_select_id = GPU_framebuffer_create("framebuffer_select_id");
448 }
449
450 if ((e_data.texture_u32 != nullptr) && ((GPU_texture_width(e_data.texture_u32) != size[0]) ||
451 (GPU_texture_height(e_data.texture_u32) != size[1])))
452 {
453 GPU_texture_free(e_data.texture_u32);
454 e_data.texture_u32 = nullptr;
455 }
456
457 /* Make sure the depth texture is attached.
458 * It may disappear when loading another Blender session. */
459 GPU_framebuffer_texture_attach(e_data.framebuffer_select_id, dtxl->depth, 0, 0);
460
461 if (e_data.texture_u32 == nullptr) {
463 e_data.texture_u32 = GPU_texture_create_2d(
464 "select_buf_ids", size[0], size[1], 1, gpu::TextureFormat::UINT_32, usage, nullptr);
465 GPU_framebuffer_texture_attach(e_data.framebuffer_select_id, e_data.texture_u32, 0, 0);
466
467 GPU_framebuffer_check_valid(e_data.framebuffer_select_id, nullptr);
468 }
469 }
470
471 short get_object_select_mode(Scene *scene, Object *ob)
472 {
473 short select_mode = 0;
475 /* In order to sample flat colors for vertex weights / texture-paint / vertex-paint
476 * we need to be in SCE_SELECT_FACE mode so select_cache_init() correctly sets up
477 * a shgroup with select_id_flat.
478 * Note this is not working correctly for vertex-paint (yet), but has been discussed
479 * in #66645 and there is a solution by @mano-wii in P1032.
480 * So OB_MODE_VERTEX_PAINT is already included here [required for P1032 I guess]. */
481 Mesh *me_orig = id_cast<Mesh *>(DEG_get_original(ob)->data);
482 if (me_orig->editflag & ME_EDIT_PAINT_VERT_SEL) {
483 select_mode = SCE_SELECT_VERTEX;
484 }
485 else {
486 select_mode = SCE_SELECT_FACE;
487 }
488 }
489 else {
490 select_mode = scene->toolsettings->selectmode;
491 }
492
493 return select_mode;
494 }
495
496 bool check_ob_drawface_dot(short select_mode, const View3D *v3d, eDrawType dt)
497 {
498 if (select_mode & SCE_SELECT_FACE) {
499 if ((dt < OB_SOLID) || XRAY_FLAG_ENABLED(v3d)) {
500 return true;
501 }
503 return true;
504 }
505 }
506 return false;
507 }
508
509 /* Return a new range if size `n` after `total_range` and grow `total_range` by the same amount.
510 */
511 IndexRange alloc_range(IndexRange &total_range, uint size)
512 {
513 const IndexRange indices = total_range.after(size);
514 total_range = IndexRange::from_begin_size(total_range.start(), total_range.size() + size);
515 return indices;
516 }
517};
518
520{
521 return new Instance();
522}
523
525{
527 for (int sh_data_index = 0; sh_data_index < ARRAY_SIZE(e_data.sh_data); sh_data_index++) {
528 Instance::StaticData::Shaders *sh_data = &e_data.sh_data[sh_data_index];
531 }
532
535}
536
537} // namespace draw::edit_select
538
540
541/* -------------------------------------------------------------------- */
544
545using namespace blender::draw::edit_select;
546
552
558
564
566
567} // namespace blender
#define final(a, b, c)
Definition BLI_hash_c.hh:21
#define RV3D_LOCK_FLAGS(rv3d)
#define RV3D_CLIPPING_ENABLED(v3d, rv3d)
#define XRAY_ENABLED(v3d)
#define RETOPOLOGY_ENABLED(v3d)
#define RETOPOLOGY_OFFSET(v3d)
#define XRAY_FLAG_ENABLED(v3d)
#define GPU_FRAMEBUFFER_FREE_SAFE(fb)
#define GPU_SHADER_FREE_SAFE(shader)
#define GPU_SHADER_CFG_LEN
#define GPU_TEXTURE_FREE_SAFE(texture)
#define BLI_assert_unreachable()
#define BLI_assert_msg(a, msg)
constexpr int64_t one_after_last() const
constexpr int64_t size() const
constexpr IndexRange()=default
static constexpr IndexRange from_begin_size(const int64_t begin, const int64_t size)
constexpr IndexRange after(int64_t n) const
constexpr int64_t start() const
const float4x4 & winmat(int view_id=0) const
Definition draw_view.hh:148
static View & default_get()
Definition draw_view.cc:317
const float4x4 & viewmat(int view_id=0) const
Definition draw_view.hh:136
void push_constant(const char *name, const float &data)
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
#define DRW_CLIPPING_UBO_SLOT
#define DRW_STATE_DEFAULT
Definition draw_state.hh:79
const int state
gpu::Batch * DRW_mesh_batch_cache_get_facedots_with_select_id(Mesh &mesh)
detail::Pass< command::DrawCommandBuf > PassSimple
gpu::Batch * DRW_mesh_batch_cache_get_surface(Mesh &mesh)
gpu::Batch * DRW_mesh_batch_cache_get_triangles_with_select_id(Mesh &mesh)
gpu::Batch * DRW_mesh_batch_cache_get_verts_with_select_id(Mesh &mesh)
gpu::Batch * DRW_mesh_batch_cache_get_edges_with_select_id(Mesh &mesh)
BMesh Iterators.
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
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned int uint
Definition BKE_text.h:55
bool GPU_framebuffer_check_valid(gpu::FrameBuffer *fb, char err_out[256])
void BM_mesh_elem_table_ensure(BMesh *bm, const char htype)
gpu::Shader * GPU_shader_create_from_info_name(const char *info_name)
gpu::FrameBuffer * DRW_engine_select_framebuffer_get()
MatBase< float, 4, 4 > float4x4
@ V3D_SHADING_BACKFACE_CULLING
VecBase< float, 4 > float4
@ ME_EDIT_PAINT_VERT_SEL
@ GPU_TEXTURE_USAGE_SHADER_READ
@ GPU_TEXTURE_USAGE_ATTACHMENT
T & DRW_object_get_data_for_drawing(const Object &object)
@ OB_MODE_TEXTURE_PAINT
BMesh const char void * data
int GPU_texture_width(const gpu::Texture *texture)
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
@ DRW_STATE_CULL_BACK
Definition draw_state.hh:45
@ DRW_STATE_FIRST_VERTEX_CONVENTION
Definition draw_state.hh:73
@ DRW_STATE_CLIP_PLANES
Definition draw_state.hh:72
void DRW_submission_end()
const DRWContext * DRW_context_get()
gpu::FrameBuffer * GPU_framebuffer_create(const char *name)
void GPU_framebuffer_clear_depth(gpu::FrameBuffer *fb, float clear_depth)
void GPU_framebuffer_texture_attach(gpu::FrameBuffer *fb, gpu::Texture *texture, int slot, int mip)
T * DEG_get_original(T *id)
int GPU_texture_height(const gpu::Texture *texture)
void GPU_texture_free(gpu::Texture *texture)
void DRW_submission_start()
gpu::Texture * DRW_engine_select_texture_get()
const BMesh * BKE_editmesh_bmesh_get(const Object *ob)
Definition editmesh.cc:65
@ V3D_OVERLAY_EDIT_FACE_DOT
SELECTID_Context * DRW_select_engine_context_get()
void GPU_framebuffer_clear_color_depth(gpu::FrameBuffer *fb, const double4 clear_col, float clear_depth)
gpu::Texture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
#define min(a, b)
Definition sort.cc:38
DefaultTextureList * viewport_texture_list_get() const
Map< Object *, ElemIndexRanges > elem_ranges
eView3DOverlay_EditFlag edit_flag
View3DOverlay overlay
float4x4 winmat_polygon_offset(const float4x4 &winmat, float offset)
Definition draw_view.hh:208
struct blender::draw::edit_select::Instance::StaticData::Shaders sh_data[GPU_SHADER_CFG_LEN]
void object_sync(ObjectRef &ob_ref, Manager &manager) final
ElemIndexRanges object_sync(View3D *v3d, Object *ob, ResourceHandleRange res_handle, short select_mode, uint index_start)
ElemIndexRanges edit_mesh_sync(Object *ob, const BMesh *bm, ResourceHandleRange res_handle, short select_mode, bool draw_facedot, const uint initial_index)
void draw(Manager &manager) final
ElemIndexRanges mesh_sync(Object *ob, ResourceHandleRange res_handle, short select_mode, const uint initial_index)