Blender V5.3
extract_mesh_vbo_select_idx.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_array_utils.hh"
10
11#include "draw_subdivision.hh"
12#include "extract_mesh.hh"
13
14namespace blender::draw {
15
16static gpu::VertBufPtr create_vbo(const int size)
17{
18 static GPUVertFormat format = GPU_vertformat_from_attribute("index", gpu::VertAttrType::SINT_32);
19 gpu::VertBufPtr vbo = gpu::VertBufPtr(GPU_vertbuf_create_with_format(format));
20 GPU_vertbuf_data_alloc(*vbo, size);
21 return vbo;
22}
23
24/* TODO: Use #glVertexID to get loop index and use the data structure on the CPU to retrieve the
25 * select element associated with this loop ID. This would remove the need for this separate
26 * index VBO's. We could upload the p/e/orig_index_vert as a buffer texture and sample it inside
27 * the shader to output original index. */
28
30{
31 MutableSpan corners_data = vbo_data.take_front(mr.corners_num);
32 MutableSpan loose_edge_data = vbo_data.slice(mr.corners_num, mr.loose_edges.size() * 2);
33 MutableSpan loose_vert_data = vbo_data.take_back(mr.loose_verts.size());
34
35 if (mr.orig_index_vert) {
36 const Span<int> orig_index_vert(mr.orig_index_vert, mr.verts_num);
37 array_utils::gather(orig_index_vert, mr.corner_verts, corners_data);
38 extract_mesh_loose_edge_data(orig_index_vert, mr.edges, mr.loose_edges, loose_edge_data);
39 array_utils::gather(orig_index_vert, mr.loose_verts, loose_vert_data);
40 }
41 else {
42 array_utils::copy(mr.corner_verts, corners_data);
43 const Span<int2> edges = mr.edges;
45 [&](const int i, const int pos) {
46 loose_edge_data[pos * 2 + 0] = edges[i][0];
47 loose_edge_data[pos * 2 + 1] = edges[i][1];
48 },
50 mr.loose_verts.to_indices(loose_vert_data);
51 }
52}
53
55{
56 MutableSpan corners_data = vbo_data.take_front(mr.corners_num);
57 MutableSpan loose_edge_data = vbo_data.slice(mr.corners_num, mr.loose_edges.size() * 2);
58 MutableSpan loose_vert_data = vbo_data.take_back(mr.loose_verts.size());
59
60 const BMesh &bm = *mr.bm;
61 threading::parallel_for(IndexRange(bm.totface), 2048, [&](const IndexRange range) {
62 for (const int face_index : range) {
63 const BMFace &face = *BM_face_at_index(&const_cast<BMesh &>(bm), face_index);
64 const BMLoop *loop = BM_FACE_FIRST_LOOP(&face);
65 for ([[maybe_unused]] const int i : IndexRange(face.len)) {
66 const int index = BM_elem_index_get(loop);
67 corners_data[index] = BM_elem_index_get(loop->v);
68 loop = loop->next;
69 }
70 }
71 });
72
73 mr.loose_edges.foreach_index(
74 [&](const int i, const int pos) {
75 const BMEdge &edge = *BM_edge_at_index(&const_cast<BMesh &>(bm), i);
76 loose_edge_data[pos * 2 + 0] = BM_elem_index_get(edge.v1);
77 loose_edge_data[pos * 2 + 1] = BM_elem_index_get(edge.v2);
78 },
80
81 mr.loose_verts.to_indices(loose_vert_data);
82}
83
85{
88 extract_vert_index_mesh(mr, vbo->data<int>());
89 }
90 else {
91 extract_vert_index_bm(mr, vbo->data<int>());
92 }
93 return vbo;
94}
95
97{
98 MutableSpan corners_data = vbo_data.take_front(mr.corners_num);
99 MutableSpan loose_edge_data = vbo_data.slice(mr.corners_num, mr.loose_edges.size() * 2);
100
101 if (mr.orig_index_edge) {
102 const Span<int> orig_index_edge(mr.orig_index_edge, mr.edges_num);
103 array_utils::gather(orig_index_edge, mr.corner_edges, corners_data);
105 [&](const int i, const int pos) {
106 loose_edge_data[pos * 2 + 0] = orig_index_edge[i];
107 loose_edge_data[pos * 2 + 1] = orig_index_edge[i];
108 },
110 }
111 else {
112 array_utils::copy(mr.corner_edges, corners_data);
114 [&](const int i, const int pos) {
115 loose_edge_data[pos * 2 + 0] = i;
116 loose_edge_data[pos * 2 + 1] = i;
117 },
119 }
120}
121
123{
124 MutableSpan corners_data = vbo_data.take_front(mr.corners_num);
125 MutableSpan loose_edge_data = vbo_data.slice(mr.corners_num, mr.loose_edges.size() * 2);
126
127 const BMesh &bm = *mr.bm;
128 threading::parallel_for(IndexRange(bm.totface), 2048, [&](const IndexRange range) {
129 for (const int face_index : range) {
130 const BMFace &face = *BM_face_at_index(&const_cast<BMesh &>(bm), face_index);
131 const BMLoop *loop = BM_FACE_FIRST_LOOP(&face);
132 for ([[maybe_unused]] const int i : IndexRange(face.len)) {
133 const int index = BM_elem_index_get(loop);
134 corners_data[index] = BM_elem_index_get(loop->e);
135 loop = loop->next;
136 }
137 }
138 });
139
140 mr.loose_edges.foreach_index_optimized<int>(
141 [&](const int i, const int pos) {
142 loose_edge_data[pos * 2 + 0] = i;
143 loose_edge_data[pos * 2 + 1] = i;
144 },
145 exec_mode::grain_size(4096));
146}
147
149{
152 extract_edge_index_mesh(mr, vbo->data<int>());
153 }
154 else {
155 extract_edge_index_bm(mr, vbo->data<int>());
156 }
157 return vbo;
158}
159
161{
162 const OffsetIndices faces = mr.faces;
163 if (mr.orig_index_face) {
164 const Span<int> orig_index_face(mr.orig_index_face, mr.edges_num);
165 threading::parallel_for(faces.index_range(), 2048, [&](const IndexRange range) {
166 for (const int face : range) {
167 vbo_data.slice(faces[face]).fill(orig_index_face[face]);
168 }
169 });
170 }
171 else {
172 offset_indices::build_reverse_map(faces, vbo_data);
173 }
174}
175
177{
178 const BMesh &bm = *mr.bm;
179 threading::parallel_for(IndexRange(bm.totface), 2048, [&](const IndexRange range) {
180 for (const int face_index : range) {
181 const BMFace &face = *BM_face_at_index(&const_cast<BMesh &>(bm), face_index);
182 const IndexRange face_range(BM_elem_index_get(BM_FACE_FIRST_LOOP(&face)), face.len);
183 vbo_data.slice(face_range).fill(face_index);
184 }
185 });
186}
187
189{
192 extract_face_index_mesh(mr, vbo->data<int>());
193 }
194 else {
195 extract_face_index_bm(mr, vbo->data<int>());
196 }
197 return vbo;
198}
199
201 const MeshRenderData &mr,
202 MutableSpan<int32_t> vbo_data)
203{
204 const IndexMask &loose_verts = mr.loose_verts;
205 const IndexMask &loose_edges = mr.loose_edges;
206 if (loose_edges.is_empty() && loose_verts.is_empty()) {
207 return;
208 }
209
210 const Span<int2> coarse_edges = mr.edges;
211 const int verts_per_edge = subdiv_verts_per_coarse_edge(subdiv_cache);
212 MutableSpan<int32_t> edge_data = vbo_data.slice(subdiv_cache.num_subdiv_loops,
213 loose_edges.size() * verts_per_edge);
214 loose_edges.foreach_index([&](const int i, const int pos) {
215 const int2 edge = coarse_edges[i];
216 MutableSpan data = edge_data.slice(pos * verts_per_edge, verts_per_edge);
217 data.first() = mr.orig_index_vert ? mr.orig_index_vert[edge[0]] : edge[0];
218 data.last() = mr.orig_index_vert ? mr.orig_index_vert[edge[1]] : edge[1];
219 });
220
221 MutableSpan<int32_t> loose_vert_data = vbo_data.take_back(loose_verts.size());
222 if (mr.orig_index_vert) {
223 array_utils::gather(Span(mr.orig_index_vert, mr.verts_num), loose_verts, loose_vert_data);
224 }
225 else {
226 loose_verts.to_indices(loose_vert_data);
227 }
228}
229
231 const MeshRenderData &mr)
232{
233 const int loose_len = subdiv_loose_edges_num(mr, subdiv_cache) * 2 + mr.loose_verts.size();
234 /* Each element points to an element in the `ibo.points`. */
236 subdiv_cache.verts_orig_index->data<int32_t>().data(),
237 subdiv_cache.num_subdiv_loops,
238 loose_len);
239 if (!mr.orig_index_vert) {
240 return vbo;
241 }
242
243 /* Remap the vertex indices to those pointed by the origin indices layer. At this point, the
244 * VBO data is a copy of #verts_orig_index which contains the coarse vertices indices, so
245 * the memory can both be accessed for lookup and immediately overwritten. */
246 MutableSpan vbo_data = vbo->data<int32_t>();
247 for (int i = 0; i < subdiv_cache.num_subdiv_loops; i++) {
248 if (vbo_data[i] == -1) {
249 continue;
250 }
251 vbo_data[i] = mr.orig_index_vert[vbo_data[i]];
252 }
253 extract_vert_idx_loose_geom_subdiv(subdiv_cache, mr, vbo_data);
254 return vbo;
255}
256
258 const MeshRenderData &mr,
259 MutableSpan<int32_t> vbo_data)
260{
261 const IndexMask &loose_edges = mr.loose_edges;
262 if (loose_edges.is_empty()) {
263 return;
264 }
265
266 const int verts_per_edge = subdiv_verts_per_coarse_edge(subdiv_cache);
267 MutableSpan data = vbo_data.slice(subdiv_cache.num_subdiv_loops,
268 loose_edges.size() * verts_per_edge);
269 loose_edges.foreach_index([&](const int edge, const int i) {
270 const int index = mr.orig_index_edge ? mr.orig_index_edge[edge] : edge;
271 data.slice(i * verts_per_edge, verts_per_edge).fill(index);
272 });
273}
274
276 const MeshRenderData &mr)
277{
279 subdiv_cache.edges_orig_index->data<int32_t>().data(),
280 subdiv_cache.num_subdiv_loops,
281 subdiv_loose_edges_num(mr, subdiv_cache) * 2);
282 extract_edge_idx_loose_geom_subdiv(subdiv_cache, mr, vbo->data<int32_t>());
283 return vbo;
284}
285
287 const MeshRenderData &mr)
288{
290 subdiv_cache.subdiv_loop_face_index, subdiv_cache.num_subdiv_loops, 0);
291
292 if (!mr.orig_index_face) {
293 return vbo;
294 }
295
296 /* Remap the face indices to those pointed by the origin indices layer. At this point, the
297 * VBO data is a copy of #subdiv_loop_face_index which contains the coarse face indices, so
298 * the memory can both be accessed for lookup and immediately overwritten. */
299 MutableSpan vbo_data = vbo->data<int32_t>();
300 for (int i = 0; i < subdiv_cache.num_subdiv_loops; i++) {
301 vbo_data[i] = mr.orig_index_face[vbo_data[i]];
302 }
303 return vbo;
304}
305
307{
310 if (mr.orig_index_face) {
311 const Span<int> orig_index_face(mr.orig_index_face, mr.faces_num);
312 array_utils::copy(orig_index_face, vbo->data<int>());
313 }
314 else {
315 array_utils::fill_index_range(vbo->data<int>());
316 }
317 }
318 else {
319 array_utils::fill_index_range(vbo->data<int>());
320 }
321 return vbo;
322}
323
324} // namespace blender::draw
#define BM_elem_index_get(ele)
constexpr Span()=default
constexpr IndexRange()=default
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:579
constexpr MutableSpan take_back(const int64_t n) const
Definition BLI_span.hh:646
constexpr T * data() const
Definition BLI_span.hh:545
constexpr MutableSpan take_front(const int64_t n) const
Definition BLI_span.hh:635
constexpr const T * data() const
Definition BLI_span.hh:219
MutableSpan< T > data()
void foreach_index_optimized(Fn &&fn, Mode mode=exec_mode::serial) const
void to_indices(MutableSpan< T > r_indices) const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
Extraction of Mesh data into VBO to feed to GPU.
void gather(const GVArray &src, const IndexMask &indices, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
void copy(const GVArray &src, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
void fill_index_range(MutableSpan< T > span, const T start=0)
void extract_mesh_loose_edge_data(const Span< T > vert_data, const Span< int2 > edges, const IndexMask &loose_edges, MutableSpan< T > gpu_data)
int subdiv_loose_edges_num(const MeshRenderData &mr, const DRWSubdivCache &cache)
static void extract_vert_index_bm(const MeshRenderData &mr, MutableSpan< int > vbo_data)
static void extract_face_index_bm(const MeshRenderData &mr, MutableSpan< int > vbo_data)
int subdiv_verts_per_coarse_edge(const DRWSubdivCache &cache)
gpu::VertBufPtr extract_edge_index(const MeshRenderData &mr)
gpu::VertBufPtr draw_subdiv_init_origindex_buffer(int32_t *vert_origindex, uint num_loops, uint loose_len)
gpu::VertBufPtr extract_vert_index_subdiv(const DRWSubdivCache &subdiv_cache, const MeshRenderData &mr)
gpu::VertBufPtr extract_face_dot_index(const MeshRenderData &mr)
static gpu::VertBufPtr create_vbo(const int size)
static void extract_edge_index_bm(const MeshRenderData &mr, MutableSpan< int > vbo_data)
gpu::VertBufPtr extract_face_index_subdiv(const DRWSubdivCache &subdiv_cache, const MeshRenderData &mr)
static void extract_vert_index_mesh(const MeshRenderData &mr, MutableSpan< int > vbo_data)
gpu::VertBufPtr extract_edge_index_subdiv(const DRWSubdivCache &subdiv_cache, const MeshRenderData &mr)
static void extract_edge_idx_loose_geom_subdiv(const DRWSubdivCache &subdiv_cache, const MeshRenderData &mr, MutableSpan< int32_t > vbo_data)
static void extract_edge_index_mesh(const MeshRenderData &mr, MutableSpan< int > vbo_data)
static void extract_vert_idx_loose_geom_subdiv(const DRWSubdivCache &subdiv_cache, const MeshRenderData &mr, MutableSpan< int32_t > vbo_data)
gpu::VertBufPtr extract_vert_index(const MeshRenderData &mr)
gpu::VertBufPtr extract_face_index(const MeshRenderData &mr)
static void extract_face_index_mesh(const MeshRenderData &mr, MutableSpan< int > vbo_data)
constexpr ParallelGrainSize grain_size(int grain_size)
std::unique_ptr< gpu::VertBuf, gpu::VertBufDeleter > VertBufPtr
void build_reverse_map(OffsetIndices< int > offsets, MutableSpan< int > r_map)
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
BLI_INLINE BMEdge * BM_edge_at_index(BMesh *bm, const int index)
GPUVertFormat GPU_vertformat_from_attribute(StringRef name, gpu::VertAttrType type)
VecBase< int32_t, 2 > int2
BMesh const char void * data
void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len)
signed int int32_t
Definition BKE_text.h:78
OffsetIndices< int > faces