Blender V5.3
extract_mesh_vbo_paint_overlay_flag.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "extract_mesh.hh"
10
11#include "draw_subdivision.hh"
12
13namespace blender::draw {
14
16{
17 const bool use_face_select = (mr.mesh->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
18 Span<bool> selection;
20 selection = mr.select_poly;
21 }
22 else if (mr.mesh->editflag & ME_EDIT_PAINT_VERT_SEL) {
23 selection = mr.select_vert;
24 }
25 if (selection.is_empty() && mr.hide_poly.is_empty() && (!mr.edit_bmesh || !mr.orig_index_vert)) {
26 flags.fill(0);
27 return;
28 }
29 const OffsetIndices faces = mr.faces;
30 threading::parallel_for(faces.index_range(), 1024, [&](const IndexRange range) {
31 if (selection.is_empty()) {
32 flags.fill(0);
33 }
34 else {
35 if (use_face_select) {
36 for (const int face : range) {
37 flags.slice(faces[face]).fill(selection[face] ? 1 : 0);
38 }
39 }
40 else {
41 const Span<int> corner_verts = mr.corner_verts;
42 for (const int face : range) {
43 for (const int corner : faces[face]) {
44 flags[corner] = selection[corner_verts[corner]] ? 1 : 0;
45 }
46 }
47 }
48 }
49 if (!mr.hide_poly.is_empty()) {
50 const Span<bool> hide_poly = mr.hide_poly;
51 for (const int face : range) {
52 if (hide_poly[face]) {
53 flags.slice(faces[face]).fill(-1);
54 }
55 }
56 }
57 if (mr.edit_bmesh && mr.orig_index_vert) {
58 const Span<int> corner_verts = mr.corner_verts;
59 const Span<int> orig_indices(mr.orig_index_vert, mr.verts_num);
60 for (const int face : range) {
61 for (const int corner : faces[face]) {
62 if (orig_indices[corner_verts[corner]] == ORIGINDEX_NONE) {
63 flags[corner] = -1;
64 }
65 }
66 }
67 }
68 });
69}
70
72{
73 /* TODO: Return early if there are no hidden faces. */
74 const BMesh &bm = *mr.bm;
75 threading::parallel_for(IndexRange(bm.totface), 2048, [&](const IndexRange range) {
76 for (const int face_index : range) {
77 const BMFace &face = *BM_face_at_index(&const_cast<BMesh &>(bm), face_index);
78 if (BM_elem_flag_test(&face, BM_ELEM_HIDDEN)) {
79 const IndexRange face_range(BM_elem_index_get(BM_FACE_FIRST_LOOP(&face)), face.len);
80 flags.slice(face_range).fill(-1);
81 }
82 }
83 });
84}
85
87{
88 static const GPUVertFormat format = GPU_vertformat_from_attribute("paint_overlay_flag",
89 gpu::VertAttrType::SINT_32);
90 return format;
91}
92
94{
95 const int size = mr.corners_num + mr.loose_indices_num;
97 GPU_vertbuf_create_with_format(get_paint_overlay_flag_format()));
98 GPU_vertbuf_data_alloc(*vbo, size);
99 MutableSpan vbo_data = vbo->data<int>();
100 MutableSpan corners_data = vbo_data.take_front(mr.corners_num);
101 MutableSpan loose_data = vbo_data.take_back(mr.loose_indices_num);
102
104 extract_paint_overlay_flags(mr, corners_data);
105 }
106 else {
107 extract_edit_flags_bm(mr, corners_data);
108 }
109
110 loose_data.fill(0);
111 return vbo;
112}
113
114static void update_loose_flags(const MeshRenderData &mr,
115 const DRWSubdivCache &subdiv_cache,
116 gpu::VertBuf &flags)
117{
118 const int vbo_size = subdiv_full_vbo_size(mr, subdiv_cache);
119 const int loose_geom_start = subdiv_cache.num_subdiv_loops;
120
121 /* Push VBO content to the GPU and bind the VBO so that #GPU_vertbuf_update_sub can work. */
122 GPU_vertbuf_use(&flags);
123
124 /* Default to zeroed attribute. The overlay shader should expect this and render engines should
125 * never draw loose geometry. */
126 const int default_value = 0;
127 for (const int i : IndexRange::from_begin_end(loose_geom_start, vbo_size)) {
128 /* TODO(fclem): This has HORRENDOUS performance. Prefer clearing the buffer on device with
129 * something like glClearBufferSubData. */
130 GPU_vertbuf_update_sub(&flags, i * sizeof(int), sizeof(int), &default_value);
131 }
132}
133
135 const DRWSubdivCache &subdiv_cache)
136{
139
141
142 update_loose_flags(mr, subdiv_cache, *flags);
143 return flags;
144}
145
146} // namespace blender::draw
static constexpr IndexRange from_begin_end(const int64_t begin, const int64_t end)
constexpr MutableSpan take_back(const int64_t n) const
Definition BLI_span.hh:646
constexpr T * data() const
Definition BLI_span.hh:545
constexpr void fill(const T &value) const
Definition BLI_span.hh:523
constexpr MutableSpan take_front(const int64_t n) const
Definition BLI_span.hh:635
constexpr bool is_empty() const
Definition BLI_span.hh:264
constexpr bool is_empty() const
Extraction of Mesh data into VBO to feed to GPU.
static const GPUVertFormat & get_paint_overlay_flag_format()
void draw_subdiv_build_paint_overlay_flag_buffer(const DRWSubdivCache &cache, gpu::VertBuf &flags)
int subdiv_full_vbo_size(const MeshRenderData &mr, const DRWSubdivCache &cache)
static void extract_edit_flags_bm(const MeshRenderData &mr, MutableSpan< int > flags)
gpu::VertBufPtr extract_paint_overlay_flags_subdiv(const MeshRenderData &mr, const DRWSubdivCache &subdiv_cache)
gpu::VertBufPtr extract_paint_overlay_flags(const MeshRenderData &mr)
static void update_loose_flags(const MeshRenderData &mr, const DRWSubdivCache &subdiv_cache, gpu::VertBuf &flags)
std::unique_ptr< gpu::VertBuf, gpu::VertBufDeleter > VertBufPtr
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
gpu::VertBuf * GPU_vertbuf_create_on_device(const GPUVertFormat &format, uint v_len)
GPUVertFormat GPU_vertformat_from_attribute(StringRef name, gpu::VertAttrType type)
void GPU_vertbuf_use(gpu::VertBuf *)
@ ME_EDIT_PAINT_VERT_SEL
@ ME_EDIT_PAINT_FACE_SEL
void GPU_vertbuf_update_sub(gpu::VertBuf *verts, uint start, uint len, const void *data)
void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len)
eMesh_EditFlag editflag
VArraySpan< bool > select_vert
VArraySpan< bool > select_poly
OffsetIndices< int > faces