Blender V5.3
extract_mesh_ibo_lines.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 "GPU_index_buffer.hh"
12
13#include "extract_mesh.hh"
14
15#include "draw_cache_inline.hh"
16#include "draw_subdivision.hh"
17
18namespace blender::draw {
19
21 const IndexMask &mask,
22 IndexMaskMemory &memory)
23{
24 IndexMask visible = mask;
27 visible = IndexMask::from_bits(visible, visible_bits, memory);
28 }
29 if (!mr.hide_edge.is_empty()) {
30 visible = IndexMask::from_bools_inverse(visible, mr.hide_edge, memory);
31 }
32 if (mr.hide_unmapped_edges && mr.orig_index_edge != nullptr) {
33 const int *orig_index = mr.orig_index_edge;
34 static_assert(ORIGINDEX_NONE == -1);
35 visible = array_utils::indices_non_negative(visible, Span(orig_index, mr.edges_num), memory);
36 }
37 return visible;
38}
39
41 const IndexMask &visible,
43{
44 /* Vertices for loose edges are not shared in the GPU vertex buffers, so the indices are simply
45 * an increasing contiguous range. Ideally this would be generated on the GPU itself, or just
46 * unnecessary, but a large number of loose edges isn't expected to be a common performance
47 * bottleneck either. */
48 threading::memory_bandwidth_bound_task(data.size_in_bytes(), [&]() {
49 const uint loose_start = mr.corners_num;
50 if (visible.size() == mr.loose_edges.size()) {
51 array_utils::fill_index_range(data.cast<uint>(), loose_start);
52 }
53 else {
54 visible.foreach_index_optimized<int>(
55 [&](const int i, const int pos) {
56 data[pos] = loose_start + uint2(i * 2 + 0, i * 2 + 1);
57 },
59 }
60 });
61}
62
64{
65 /* Since we're building a mask of the full loose edge data arrays (i.e. rather than a mask
66 * of the mesh's edges) we need random access to the IndexMask indices. */
67 Array<int, 64> loose_edges(mr.loose_edges.size());
68 mr.loose_edges.to_indices(loose_edges.as_mutable_span());
69 IndexMask visible(loose_edges.size());
70 if (!mr.hide_edge.is_empty()) {
71 const Span<bool> hide_edge = mr.hide_edge;
73 visible, memory, [&](const int i) { return !hide_edge[loose_edges[i]]; });
74 }
75 if (mr.hide_unmapped_edges && mr.orig_index_edge != nullptr) {
76 const int *orig_index = mr.orig_index_edge;
77 visible = IndexMask::from_predicate(visible, memory, [&](const int64_t i) {
78 return orig_index[loose_edges[i]] != ORIGINDEX_NONE;
79 });
80 }
81 return visible;
82}
83
84static void extract_lines_mesh(const MeshRenderData &mr,
85 gpu::IndexBufPtr *lines,
86 gpu::IndexBufPtr *lines_loose,
87 bool &no_loose_wire)
88{
89 IndexMaskMemory memory;
90 const IndexMask visible_loose_edges = calc_visible_loose_edge_indices(mr, memory);
91 const int max_index = mr.corners_num + mr.loose_edges.size() * 2;
92
93 no_loose_wire = visible_loose_edges.is_empty();
94
95 if (lines_loose && !lines) {
96 GPUIndexBufBuilder builder;
97 GPU_indexbuf_init(&builder, GPU_PRIM_LINES, visible_loose_edges.size(), max_index);
99 fill_loose_lines_ibo(mr, visible_loose_edges, data);
100 *lines_loose = gpu::IndexBufPtr(GPU_indexbuf_build_ex(&builder, 0, max_index, false));
101 return;
102 }
103
104 const IndexMask non_loose_edges = mr.loose_edges.complement(IndexRange(mr.edges_num), memory);
105 const IndexMask visible_non_loose_edges = calc_mesh_edge_visibility(mr, non_loose_edges, memory);
106
107 GPUIndexBufBuilder builder;
108 GPU_indexbuf_init(&builder,
110 visible_non_loose_edges.size() + visible_loose_edges.size(),
111 max_index);
113
114 /* This code fills the index buffer in a non-deterministic way, with non-atomic access to `used`.
115 * This is okay because any of the possible face corner indices are correct, since they all
116 * correspond to the same #Mesh vertex. `used` exists here as a performance optimization to
117 * avoid writing to the VBO. */
118 const OffsetIndices faces = mr.faces;
119 const Span<int> corner_edges = mr.corner_edges;
120 if (visible_non_loose_edges.size() == mr.edges_num) {
121 /* All edges in the mesh are visible. The edges in the GPU buffer will have the same order as
122 * the mesh's edges, so any remapping is unnecessary. Use a boolean array to avoid writing to
123 * the same indices again multiple times from different threads. This is slightly beneficial
124 * because booleans are 8 times smaller than the `uint2` for each edge. */
125 Array<bool> used(mr.edges_num, false);
127 used.as_span().size_in_bytes() + data.size_in_bytes() + corner_edges.size_in_bytes(),
128 [&]() {
129 threading::parallel_for(faces.index_range(), 2048, [&](const IndexRange range) {
130 for (const int face_index : range) {
131 const IndexRange face = faces[face_index];
132 for (const int corner : face) {
133 const int edge = corner_edges[corner];
134 if (!used[edge]) {
135 used[edge] = true;
136 data[edge] = edge_from_corners(face, corner);
137 }
138 }
139 }
140 });
141 });
142 }
143 else {
144 Array<int> map(mr.edges_num, -1);
146 map.as_span().size_in_bytes() + data.size_in_bytes() + corner_edges.size_in_bytes(),
147 [&]() {
148 index_mask::build_reverse_map(visible_non_loose_edges, map.as_mutable_span());
149 threading::parallel_for(faces.index_range(), 2048, [&](const IndexRange range) {
150 for (const int face_index : range) {
151 const IndexRange face = faces[face_index];
152 for (const int corner : face) {
153 const int edge = corner_edges[corner];
154 const int vbo_index = map[edge];
155 if (vbo_index != -1) {
156 map[edge] = -1;
157 data[vbo_index] = edge_from_corners(face, corner);
158 }
159 }
160 }
161 });
162 });
163 }
164
165 fill_loose_lines_ibo(mr, visible_loose_edges, data.take_back(visible_loose_edges.size()));
166
167 *lines = gpu::IndexBufPtr(GPU_indexbuf_build_ex(&builder, 0, max_index, false));
168 if (lines_loose) {
169 *lines_loose = gpu::IndexBufPtr(GPU_indexbuf_create_subrange(
170 lines->get(), visible_non_loose_edges.size() * 2, visible_loose_edges.size() * 2));
171 }
172}
173
174static void extract_lines_bm(const MeshRenderData &mr,
175 gpu::IndexBufPtr *lines,
176 gpu::IndexBufPtr *lines_loose,
177 bool &no_loose_wire)
178{
179 const BMesh &bm = *mr.bm;
180 Array<int, 64> loose_edges(mr.loose_edges.size());
181 mr.loose_edges.to_indices(loose_edges.as_mutable_span());
182
183 IndexMaskMemory memory;
184 const IndexMask visible_loose_edges = IndexMask::from_predicate(
185 loose_edges.index_range(), memory, [&](const int i) {
186 const BMEdge &edge = *BM_edge_at_index(&const_cast<BMesh &>(bm), loose_edges[i]);
187 return !BM_elem_flag_test_bool(&edge, BM_ELEM_HIDDEN);
188 });
189 const int max_index = mr.corners_num + loose_edges.size() * 2;
190
191 no_loose_wire = visible_loose_edges.is_empty();
192
193 if (lines_loose && !lines) {
194 GPUIndexBufBuilder builder;
195 GPU_indexbuf_init(&builder, GPU_PRIM_LINES, visible_loose_edges.size(), max_index);
197 fill_loose_lines_ibo(mr, visible_loose_edges, data);
198 *lines_loose = gpu::IndexBufPtr(GPU_indexbuf_build_ex(&builder, 0, max_index, false));
199 return;
200 }
201
202 const IndexMask non_loose_edges = mr.loose_edges.complement(IndexRange(bm.totedge), memory);
203 const IndexMask visible_non_loose_edges = IndexMask::from_predicate(
204 non_loose_edges, memory, [&](const int i) {
205 const BMEdge &edge = *BM_edge_at_index(&const_cast<BMesh &>(bm), i);
207 });
208
209 GPUIndexBufBuilder builder;
210 GPU_indexbuf_init(&builder,
212 visible_non_loose_edges.size() + visible_loose_edges.size(),
213 max_index);
215
216 /* Make use of BMesh's edge to loop topology knowledge to iterate over edges instead of
217 * iterating over faces and defining edges implicitly as done in the #Mesh extraction. */
218 visible_non_loose_edges.foreach_index(
219 [&](const int i, const int pos) {
220 const BMEdge &edge = *BM_edge_at_index(&const_cast<BMesh &>(bm), i);
221 data[pos] = uint2(BM_elem_index_get(edge.l), BM_elem_index_get(edge.l->next));
222 },
224
225 fill_loose_lines_ibo(mr, visible_loose_edges, data.take_back(visible_loose_edges.size()));
226
227 *lines = gpu::IndexBufPtr(GPU_indexbuf_build_ex(&builder, 0, max_index, false));
228 if (lines_loose) {
230 lines->get(), visible_non_loose_edges.size() * 2, visible_loose_edges.size() * 2));
231 }
232}
233
235 gpu::IndexBufPtr *lines,
236 gpu::IndexBufPtr *lines_loose,
237 bool &no_loose_wire)
238{
240 extract_lines_mesh(mr, lines, lines_loose, no_loose_wire);
241 }
242 else {
243 extract_lines_bm(mr, lines, lines_loose, no_loose_wire);
244 }
245}
246
247static void extract_lines_loose_geom_subdiv(const DRWSubdivCache &subdiv_cache,
248 const MeshRenderData &mr,
249 const int edge_loose_offset,
250 gpu::IndexBuf *ibo)
251{
252 Array<int, 64> loose_edges(mr.loose_edges.size());
253 mr.loose_edges.to_indices(loose_edges.as_mutable_span());
254 if (loose_edges.is_empty()) {
255 return;
256 }
257 const int edges_per_edge = subdiv_edges_per_coarse_edge(subdiv_cache);
258 const int loose_edges_num = subdiv_loose_edges_num(mr, subdiv_cache);
259
260 /* Update flags for loose edges, points are already handled. */
261 static const GPUVertFormat format = GPU_vertformat_from_attribute("data",
262 gpu::VertAttrType::UINT_32);
263
265 GPU_vertbuf_init_with_format(*flags, format);
266
267 GPU_vertbuf_data_alloc(*flags, loose_edges_num);
268
269 MutableSpan<uint> flags_data = flags->data<uint>();
270
271 switch (mr.extract_type) {
273 const int *orig_index_edge = (mr.hide_unmapped_edges) ? mr.orig_index_edge : nullptr;
274 if (orig_index_edge == nullptr) {
275 const Span<bool> hide_edge = mr.hide_edge;
276 if (!hide_edge.is_empty()) {
277 for (const int i : loose_edges.index_range()) {
278 const bool value = hide_edge[loose_edges[i]];
279 flags_data.slice(i * edges_per_edge, edges_per_edge).fill(value);
280 }
281 }
282 else {
283 flags_data.fill(0);
284 }
285 }
286 else {
287 if (mr.bm) {
288 for (const int i : loose_edges.index_range()) {
289 const BMEdge *bm_edge = bm_original_edge_get(mr, loose_edges[i]);
290 const int value = (bm_edge) ? BM_elem_flag_test_bool(bm_edge, BM_ELEM_HIDDEN) : true;
291 flags_data.slice(i * edges_per_edge, edges_per_edge).fill(value);
292 }
293 }
294 else {
295 const Span<bool> hide_edge = mr.hide_edge;
296 if (!hide_edge.is_empty()) {
297 for (const int i : loose_edges.index_range()) {
298 const bool value = (orig_index_edge[loose_edges[i]] == ORIGINDEX_NONE) ?
299 false :
300 hide_edge[loose_edges[i]];
301 flags_data.slice(i * edges_per_edge, edges_per_edge).fill(value);
302 }
303 }
304 else {
305 flags_data.fill(0);
306 }
307 }
308 }
309 break;
310 }
312 BMesh *bm = mr.bm;
313 for (const int i : loose_edges.index_range()) {
314 const BMEdge *bm_edge = BM_edge_at_index(bm, loose_edges[i]);
315 const bool value = BM_elem_flag_test_bool(bm_edge, BM_ELEM_HIDDEN);
316 flags_data.slice(i * edges_per_edge, edges_per_edge).fill(value);
317 }
318 break;
319 }
320 }
321
323 subdiv_cache, ibo, flags, uint(edge_loose_offset), loose_edges_num);
324
325 GPU_vertbuf_discard(flags);
326}
327
328void extract_lines_subdiv(const DRWSubdivCache &subdiv_cache,
329 const MeshRenderData &mr,
330 gpu::IndexBufPtr *lines,
331 gpu::IndexBufPtr *lines_loose,
332 bool &no_loose_wire)
333{
334 const int loose_ibo_size = subdiv_loose_edges_num(mr, subdiv_cache) * 2;
335 no_loose_wire = loose_ibo_size == 0;
336
337 if (lines_loose && !lines) {
338 *lines_loose = gpu::IndexBufPtr(GPU_indexbuf_build_on_device(loose_ibo_size));
339 extract_lines_loose_geom_subdiv(subdiv_cache, mr, 0, lines_loose->get());
340 return;
341 }
342
343 const int non_loose_ibo_size = subdiv_cache.num_subdiv_loops * 2;
344
345 *lines = gpu::IndexBufPtr(GPU_indexbuf_build_on_device(non_loose_ibo_size + loose_ibo_size));
346 if (non_loose_ibo_size > 0) {
347 draw_subdiv_build_lines_buffer(subdiv_cache, lines->get());
348 }
349 extract_lines_loose_geom_subdiv(subdiv_cache, mr, non_loose_ibo_size, lines->get());
350
351 if (lines_loose) {
352 /* Multiply by 2 because these are edges indices. */
353 *lines_loose = gpu::IndexBufPtr(
354 GPU_indexbuf_create_subrange(lines->get(), non_loose_ibo_size, loose_ibo_size));
355 }
356}
357
358} // namespace blender::draw
#define ORIGINDEX_NONE
#define GPU_vertbuf_init_with_format(verts, format)
#define BM_elem_index_get(ele)
#define BM_elem_flag_test_bool(ele, hflag)
int64_t size() const
Definition BLI_array.hh:267
Span< T > as_span() const
Definition BLI_array.hh:254
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
IndexRange index_range() const
Definition BLI_array.hh:371
bool is_empty() const
Definition BLI_array.hh:275
static IndexMask from_predicate(const IndexMask &universe, LinearAllocator<> &memory, Fn &&predicate, exec_mode::Mode mode=exec_mode::parallel)
static IndexMask from_bits(BitSpan bits, LinearAllocator<> &memory)
static IndexMask from_bools_inverse(Span< bool > bools, LinearAllocator<> &memory)
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:579
constexpr void fill(const T &value) const
Definition BLI_span.hh:523
constexpr int64_t size_in_bytes() const
Definition BLI_span.hh:272
constexpr Span()=default
constexpr bool is_empty() const
Definition BLI_span.hh:264
constexpr bool is_empty() const
MutableSpan< T > data()
void to_indices(MutableSpan< T > r_indices) const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
IndexMask complement(const IndexMask &universe, LinearAllocator<> &memory) const
Extraction of Mesh data into VBO to feed to GPU.
IndexMask indices_non_negative(const IndexMask &universe, Span< int > values, LinearAllocator<> &memory)
static void fill_loose_lines_ibo(const MeshRenderData &mr, const IndexMask &visible, MutableSpan< uint2 > data)
static void extract_lines_bm(const MeshRenderData &mr, gpu::IndexBufPtr *lines, gpu::IndexBufPtr *lines_loose, bool &no_loose_wire)
int subdiv_loose_edges_num(const MeshRenderData &mr, const DRWSubdivCache &cache)
static IndexMask calc_mesh_edge_visibility(const MeshRenderData &mr, const IndexMask &mask, IndexMaskMemory &memory)
static void extract_lines_loose_geom_subdiv(const DRWSubdivCache &subdiv_cache, const MeshRenderData &mr, const int edge_loose_offset, gpu::IndexBuf *ibo)
BLI_INLINE BMEdge * bm_original_edge_get(const MeshRenderData &mr, int idx)
int subdiv_edges_per_coarse_edge(const DRWSubdivCache &cache)
static IndexMask calc_visible_loose_edge_indices(const MeshRenderData &mr, IndexMaskMemory &memory)
void draw_subdiv_build_lines_buffer(const DRWSubdivCache &cache, gpu::IndexBuf *lines_indices)
void extract_lines(const MeshRenderData &mr, gpu::IndexBufPtr *lines, gpu::IndexBufPtr *lines_loose, bool &no_loose_wire)
void draw_subdiv_build_lines_loose_buffer(const DRWSubdivCache &cache, gpu::IndexBuf *lines_indices, gpu::VertBuf *lines_flags, uint edge_loose_offset, uint num_loose_edges)
static void extract_lines_mesh(const MeshRenderData &mr, gpu::IndexBufPtr *lines, gpu::IndexBufPtr *lines_loose, bool &no_loose_wire)
void extract_lines_subdiv(const DRWSubdivCache &subdiv_cache, const MeshRenderData &mr, gpu::IndexBufPtr *lines, gpu::IndexBufPtr *lines_loose, bool &no_loose_wire)
constexpr ParallelGrainSize grain_size(int grain_size)
std::unique_ptr< IndexBuf, IndexBufDeleter > IndexBufPtr
void memory_bandwidth_bound_task(const int64_t approximate_bytes_touched, const Function &function)
Definition BLI_task.hh:251
VecBase< uint32_t, 2 > uint2
__int64 int64_t
Definition BKE_text.h:90
gpu::IndexBuf * GPU_indexbuf_build_on_device(uint index_len)
void GPU_indexbuf_init(GPUIndexBufBuilder *, GPUPrimType, uint prim_len, uint vertex_len)
unsigned int uint
Definition BKE_text.h:55
BLI_INLINE BMEdge * BM_edge_at_index(BMesh *bm, const int index)
GPUVertFormat GPU_vertformat_from_attribute(StringRef name, gpu::VertAttrType type)
BMesh const char void * data
gpu::VertBuf * GPU_vertbuf_calloc()
MutableSpan< uint32_t > GPU_indexbuf_get_data(GPUIndexBufBuilder *)
void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len)
gpu::IndexBuf * GPU_indexbuf_build_ex(GPUIndexBufBuilder *builder, uint index_min, uint index_max, bool uses_restart_indices)
gpu::IndexBuf * GPU_indexbuf_create_subrange(gpu::IndexBuf *elem_src, uint start, uint length)
void GPU_vertbuf_discard(gpu::VertBuf *)
PipelineCompute lines(lines_main)
PipelineCompute lines_loose(lines_loose_main)
struct BMLoop * l
struct BMLoop * next
bke::MeshRuntime * runtime
OffsetIndices< int > faces