Blender V5.3
mesh_runtime.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_array_utils.hh"
10#include "BLI_math_geom_c.hh"
11
13#include "BKE_bvhutils.hh"
14#include "BKE_customdata.hh"
15#include "BKE_editmesh_cache.hh"
16#include "BKE_lib_id.hh"
17#include "BKE_mesh.hh"
18#include "BKE_mesh_mapping.hh"
19#include "BKE_mesh_runtime.hh"
20#include "BKE_shrinkwrap.hh"
21#include "BKE_subdiv_ccg.hh"
22
23namespace blender {
24
25/* -------------------------------------------------------------------- */
28
29namespace bke {
30
31static void free_mesh_eval(MeshRuntime &mesh_runtime)
32{
33 if (mesh_runtime.mesh_eval != nullptr) {
34 BKE_id_free(nullptr, mesh_runtime.mesh_eval);
35 mesh_runtime.mesh_eval = nullptr;
36 }
37}
38
39static void free_batch_cache(MeshRuntime &mesh_runtime)
40{
41 if (mesh_runtime.batch_cache) {
43 mesh_runtime.batch_cache = nullptr;
44 }
45}
46
47static void free_bvh_caches(MeshRuntime &mesh_runtime)
48{
49 mesh_runtime.bvh_cache_edges.tag_dirty();
50 mesh_runtime.bvh_cache_faces.tag_dirty();
51 mesh_runtime.bvh_cache_corner_tris.tag_dirty();
52 mesh_runtime.bvh_cache_corner_tris_no_hidden.tag_dirty();
53 mesh_runtime.bvh_cache_loose_verts.tag_dirty();
54 mesh_runtime.bvh_cache_loose_verts_no_hidden.tag_dirty();
55 mesh_runtime.bvh_cache_loose_edges.tag_dirty();
56 mesh_runtime.bvh_cache_loose_edges_no_hidden.tag_dirty();
57 mesh_runtime.bvh_embree_tris_cache.tag_dirty();
58 mesh_runtime.bvh_embree_verts_cache.tag_dirty();
59 mesh_runtime.bvh_embree_edges_cache.tag_dirty();
60}
61
62MeshRuntime::MeshRuntime() = default;
63
69
70static void set_bools(MutableSpan<bool> bools, const Span<int> indices_to_set)
71{
72 threading::memory_bandwidth_bound_task(indices_to_set.size(), [&]() {
73 threading::parallel_for(indices_to_set.index_range(), 8192, [&](const IndexRange range) {
74 for (const int i : range) {
75 bools[indices_to_set[i]] = true;
76 }
77 });
78 });
79}
80
85{
86 if (!mesh.runtime->loose_edges_cache.is_cached() || !mesh.loose_edges().is_empty()) {
87 return;
88 }
89 if (!mesh.runtime->loose_verts_cache.is_cached() || !mesh.loose_verts().is_empty()) {
90 return;
91 }
92 mesh.runtime->verts_no_face_cache.ensure([&](LooseGeomCache &r_data) {
93 r_data.allocator.~LinearAllocator<>();
94 new (&r_data.allocator) LinearAllocator<>();
95 r_data.mask = {};
96 });
97}
98
99} // namespace bke
100
101Span<int> Mesh::corner_to_face_map() const
102{
103 this->runtime->corner_to_face_map_cache.ensure([&](Array<int> &r_data) {
104 const OffsetIndices faces = this->faces();
105 r_data = bke::mesh::build_corner_to_face_map(faces);
106 });
107 return this->runtime->corner_to_face_map_cache.data();
108}
109
110OffsetIndices<int> Mesh::vert_to_face_map_offsets() const
111{
112 this->runtime->vert_to_face_offset_cache.ensure([&](Array<int> &r_data) {
113 r_data = Array<int>(this->verts_num + 1, 0);
114 offset_indices::build_reverse_offsets(this->corner_verts(), r_data);
115 });
116 return OffsetIndices<int>(this->runtime->vert_to_face_offset_cache.data());
117}
118
119GroupedSpan<int> Mesh::vert_to_face_map() const
120{
121 const OffsetIndices offsets = this->vert_to_face_map_offsets();
122 this->runtime->vert_to_face_map_cache.ensure([&](Array<int> &r_data) {
123 r_data.reinitialize(this->corners_num);
124 if (this->runtime->vert_to_corner_map_cache.is_cached() &&
125 this->runtime->corner_to_face_map_cache.is_cached())
126 {
127 /* The vertex to face cache can be built from the vertex to face corner
128 * and face corner to face maps if they are both already cached. */
129 array_utils::gather(this->runtime->corner_to_face_map_cache.data().as_span(),
130 this->runtime->vert_to_corner_map_cache.data().as_span(),
131 r_data.as_mutable_span());
132 }
133 else {
134 bke::mesh::build_vert_to_face_indices(this->faces(), this->corner_verts(), offsets, r_data);
135 }
136 });
137 return {offsets, this->runtime->vert_to_face_map_cache.data()};
138}
139
140GroupedSpan<int> Mesh::vert_to_corner_map() const
141{
142 const OffsetIndices offsets = this->vert_to_face_map_offsets();
143 this->runtime->vert_to_corner_map_cache.ensure([&](Array<int> &r_data) {
144 r_data = bke::mesh::build_vert_to_corner_indices(this->corner_verts(), offsets);
145 });
146 return {offsets, this->runtime->vert_to_corner_map_cache.data()};
147}
148
149GroupedSpan<int> Mesh::edge_to_corner_map() const
150{
151 this->runtime->edge_to_corner_offset_cache.ensure([&](Array<int> &r_data) {
152 r_data = Array<int>(this->edges_num + 1, 0);
153 offset_indices::build_reverse_offsets(this->corner_edges(), r_data);
154 });
155 const OffsetIndices<int> offsets(this->runtime->edge_to_corner_offset_cache.data());
156 this->runtime->edge_to_corner_map_cache.ensure([&](Array<int> &r_data) {
157 r_data.reinitialize(offsets.total_size());
158 offset_indices::reverse_indices_in_groups(this->corner_edges(), offsets, r_data);
159 });
160 return {offsets, this->runtime->edge_to_corner_map_cache.data()};
161}
162
163const IndexMask &Mesh::loose_verts() const
164{
165 using namespace blender::bke;
166 this->runtime->loose_verts_cache.ensure([&](LooseGeomCache &r_data) {
167 Array<bool, 256> bools(this->verts_num, false);
168 set_bools(bools, this->edges().cast<int>());
169 r_data.allocator.~LinearAllocator<>();
170 new (&r_data.allocator) LinearAllocator<>();
171 r_data.mask = IndexMask::from_bools_inverse(bools, r_data.allocator);
172 });
173 return this->runtime->loose_verts_cache.data().mask;
174}
175
176const IndexMask &Mesh::verts_no_face() const
177{
178 using namespace blender::bke;
179 this->runtime->verts_no_face_cache.ensure([&](LooseGeomCache &r_data) {
180 Array<bool, 256> bools(this->verts_num, false);
181 set_bools(bools, this->corner_verts());
182 r_data.allocator.~LinearAllocator<>();
183 new (&r_data.allocator) LinearAllocator<>();
184 r_data.mask = IndexMask::from_bools_inverse(bools, r_data.allocator);
185 });
186 return this->runtime->verts_no_face_cache.data().mask;
187}
188
189bool Mesh::no_overlapping_topology() const
190{
191 return this->flag & ME_NO_OVERLAPPING_TOPOLOGY;
192}
193
194const IndexMask &Mesh::loose_edges() const
195{
196 using namespace blender::bke;
197 this->runtime->loose_edges_cache.ensure([&](LooseGeomCache &r_data) {
198 Array<bool, 256> bools(this->edges_num, false);
199 set_bools(bools, this->corner_edges());
200 r_data.allocator.~LinearAllocator<>();
201 new (&r_data.allocator) LinearAllocator<>();
202 r_data.mask = IndexMask::from_bools_inverse(bools, r_data.allocator);
203 });
204 return this->runtime->loose_edges_cache.data().mask;
205}
206
207void Mesh::tag_loose_verts_none() const
208{
209 using namespace blender::bke;
210 this->runtime->loose_verts_cache.ensure([&](LooseGeomCache &r_data) {
211 r_data.allocator.~LinearAllocator<>();
212 new (&r_data.allocator) LinearAllocator<>();
213 r_data.mask = {};
214 });
216}
217
218void Mesh::tag_loose_edges_none() const
219{
220 using namespace blender::bke;
221 this->runtime->loose_edges_cache.ensure([&](LooseGeomCache &r_data) {
222 r_data.allocator.~LinearAllocator<>();
223 new (&r_data.allocator) LinearAllocator<>();
224 r_data.mask = {};
225 });
227}
228
229void Mesh::tag_overlapping_none()
230{
231 using namespace blender::bke;
233}
234
235namespace bke {
236
238{
239 this->frozen = true;
240 this->dirty_while_frozen = false;
241}
242
244{
245 this->frozen = false;
246 if (this->dirty_while_frozen) {
247 this->data.tag_dirty();
248 }
249 this->dirty_while_frozen = false;
250}
251
253{
254 if (this->frozen) {
255 this->dirty_while_frozen = true;
256 }
257 else {
258 this->data.tag_dirty();
259 }
260}
261
262} // namespace bke
263
264Span<int3> Mesh::corner_tris() const
265{
266 this->runtime->corner_tris_cache.data.ensure([&](Array<int3> &r_data) {
267 const Span<float3> positions = this->vert_positions();
268 const OffsetIndices faces = this->faces();
269 const Span<int> corner_verts = this->corner_verts();
270
271 r_data.reinitialize(poly_to_tri_count(faces.size(), corner_verts.size()));
272
274 bke::mesh::corner_tris_calc(positions, faces, corner_verts, r_data);
275 }
276 else {
278 positions, faces, corner_verts, this->face_normals(), r_data);
279 }
280 });
281
282 return this->runtime->corner_tris_cache.data.data();
283}
284
285Span<int> Mesh::corner_tri_faces() const
286{
287 this->runtime->corner_tri_faces_cache.ensure([&](Array<int> &r_data) {
288 const OffsetIndices faces = this->faces();
289 r_data.reinitialize(poly_to_tri_count(faces.size(), this->corners_num));
290 bke::mesh::corner_tris_calc_face_indices(faces, r_data);
291 });
292 return this->runtime->corner_tri_faces_cache.data();
293}
294
296{
297 /* Allow returning the size without calculating the cache. */
298 return poly_to_tri_count(mesh->faces_num, mesh->corners_num);
299}
300
302{
303 if (!mesh->runtime->edit_data) {
304 mesh->runtime->edit_data = std::make_unique<bke::EditMeshData>();
305 }
306}
307
309{
310 using namespace blender::bke;
311 free_mesh_eval(*mesh->runtime);
312 free_batch_cache(*mesh->runtime);
313 mesh->runtime->edit_data.reset();
315}
316
318{
319 /* Tagging shared caches dirty will free the allocated data if there is only one user. */
320 free_bvh_caches(*mesh->runtime);
321 mesh->runtime->subdiv_ccg.reset();
322 mesh->runtime->bounds_cache.tag_dirty();
323 mesh->runtime->vert_to_face_offset_cache.tag_dirty();
324 mesh->runtime->vert_to_face_map_cache.tag_dirty();
325 mesh->runtime->vert_to_corner_map_cache.tag_dirty();
326 mesh->runtime->corner_to_face_map_cache.tag_dirty();
327 mesh->runtime->edge_to_corner_offset_cache.tag_dirty();
328 mesh->runtime->edge_to_corner_map_cache.tag_dirty();
329 mesh->runtime->vert_normals_cache.tag_dirty();
330 mesh->runtime->vert_normals_true_cache.tag_dirty();
331 mesh->runtime->face_normals_cache.tag_dirty();
332 mesh->runtime->face_normals_true_cache.tag_dirty();
333 mesh->runtime->corner_normals_cache.tag_dirty();
334 mesh->runtime->loose_edges_cache.tag_dirty();
335 mesh->runtime->loose_verts_cache.tag_dirty();
336 mesh->runtime->verts_no_face_cache.tag_dirty();
337 mesh->runtime->corner_tris_cache.data.tag_dirty();
338 mesh->runtime->corner_tri_faces_cache.tag_dirty();
339 mesh->runtime->shrinkwrap_boundary_cache.tag_dirty();
340 mesh->runtime->max_material_index.tag_dirty();
341 mesh->runtime->subsurf_face_dot_tags.clear_and_shrink();
342 mesh->runtime->subsurf_optimal_display_edges.clear_and_shrink();
343 mesh->runtime->spatial_groups.reset();
345}
346
347void Mesh::tag_edges_split()
348{
349 /* Triangulation didn't change because vertex positions and loop vertex indices didn't change. */
350 free_bvh_caches(*this->runtime);
351 this->runtime->vert_normals_cache.tag_dirty();
352 this->runtime->corner_normals_cache.tag_dirty();
353 this->runtime->subdiv_ccg.reset();
354 this->runtime->vert_to_face_offset_cache.tag_dirty();
355 this->runtime->vert_to_face_map_cache.tag_dirty();
356 this->runtime->vert_to_corner_map_cache.tag_dirty();
357 this->runtime->edge_to_corner_offset_cache.tag_dirty();
358 this->runtime->edge_to_corner_map_cache.tag_dirty();
359 if (this->runtime->loose_edges_cache.is_cached() &&
360 !this->runtime->loose_edges_cache.data().mask.is_empty())
361 {
362 this->runtime->loose_edges_cache.tag_dirty();
363 }
364 if (this->runtime->loose_verts_cache.is_cached() &&
365 !this->runtime->loose_verts_cache.data().mask.is_empty())
366 {
367 this->runtime->loose_verts_cache.tag_dirty();
368 }
369 if (this->runtime->verts_no_face_cache.is_cached() &&
370 !this->runtime->verts_no_face_cache.data().mask.is_empty())
371 {
372 this->runtime->verts_no_face_cache.tag_dirty();
373 }
374 this->runtime->subsurf_face_dot_tags.clear_and_shrink();
375 this->runtime->subsurf_optimal_display_edges.clear_and_shrink();
376 this->runtime->shrinkwrap_boundary_cache.tag_dirty();
377}
378
379void Mesh::tag_sharpness_changed()
380{
381 this->runtime->vert_normals_cache.tag_dirty();
382 this->runtime->face_normals_cache.tag_dirty();
383 this->runtime->corner_normals_cache.tag_dirty();
384}
385
386void Mesh::tag_custom_normals_changed()
387{
388 this->runtime->vert_normals_cache.tag_dirty();
389 this->runtime->face_normals_cache.tag_dirty();
390 this->runtime->corner_normals_cache.tag_dirty();
391}
392
393void Mesh::tag_face_winding_changed()
394{
395 this->runtime->vert_normals_cache.tag_dirty();
396 this->runtime->face_normals_cache.tag_dirty();
397 this->runtime->vert_normals_true_cache.tag_dirty();
398 this->runtime->face_normals_true_cache.tag_dirty();
399 this->runtime->corner_normals_cache.tag_dirty();
400 this->runtime->vert_to_corner_map_cache.tag_dirty();
401 /* Reversing faces swaps edges stored for each of its corners, but the number of corners using
402 * each edge doesn't change, so only the indices are recalculated, not the offsets. */
403 this->runtime->edge_to_corner_map_cache.tag_dirty();
404 this->runtime->shrinkwrap_boundary_cache.tag_dirty();
405}
406
407void Mesh::tag_positions_changed()
408{
409 this->runtime->vert_normals_cache.tag_dirty();
410 this->runtime->face_normals_cache.tag_dirty();
411 this->runtime->vert_normals_true_cache.tag_dirty();
412 this->runtime->face_normals_true_cache.tag_dirty();
413 this->runtime->corner_normals_cache.tag_dirty();
414 this->runtime->shrinkwrap_boundary_cache.tag_dirty();
415 this->tag_positions_changed_no_normals();
416}
417
418void Mesh::tag_positions_changed_no_normals()
419{
420 free_bvh_caches(*this->runtime);
421 this->runtime->corner_tris_cache.tag_dirty();
422 this->runtime->bounds_cache.tag_dirty();
423 this->runtime->shrinkwrap_boundary_cache.tag_dirty();
424}
425
426void Mesh::tag_positions_changed_uniformly()
427{
428 /* The normals and triangulation didn't change, since all verts moved by the same amount. */
429 free_bvh_caches(*this->runtime);
430 this->runtime->bounds_cache.tag_dirty();
431}
432
433void Mesh::tag_topology_changed()
434{
436}
437
438void Mesh::tag_visibility_changed()
439{
440 this->runtime->bvh_cache_corner_tris_no_hidden.tag_dirty();
441 this->runtime->bvh_cache_loose_verts_no_hidden.tag_dirty();
442 this->runtime->bvh_cache_loose_edges_no_hidden.tag_dirty();
443}
444
445void Mesh::tag_material_index_changed()
446{
447 this->runtime->max_material_index.tag_dirty();
448}
449
451
452/* -------------------------------------------------------------------- */
455
456/* Draw Engine */
457
460
462{
463 if (mesh->runtime->batch_cache) {
465 }
466
467 /* Also tag batch cache for subdivided mesh, if it exists this will be
468 * the mesh that is actually being drawn. */
469 Mesh *mesh_eval = mesh->runtime->mesh_eval;
470 if (mesh_eval && mesh_eval->runtime->batch_cache) {
471 BKE_mesh_batch_cache_dirty_tag_cb(mesh_eval, mode);
472 }
473}
478
480
481} // namespace blender
CustomData interface, see also DNA_customdata_types.h.
constexpr const T * data() const
Definition BLI_span.hh:219
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
constexpr const T * data() const
Definition BLI_span.hh:219
constexpr int64_t size() const
Definition BLI_span.hh:256
ccl_device_inline int4 cast(const float4 a)
Definition math_float4.h:32
void corner_tris_calc(Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, MutableSpan< int3 > corner_tris)
void corner_tris_calc_with_normals(Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, Span< float3 > face_normals, MutableSpan< int3 > corner_tris)
static void try_tag_verts_no_face_none(const Mesh &mesh)
static void set_bools(MutableSpan< bool > bools, const Span< int > indices_to_set)
static void free_bvh_caches(MeshRuntime &mesh_runtime)
static void free_batch_cache(MeshRuntime &mesh_runtime)
static void free_mesh_eval(MeshRuntime &mesh_runtime)
constexpr int poly_to_tri_count(const int poly_count, const int corner_count)
void memory_bandwidth_bound_task(const int64_t approximate_bytes_touched, const Function &function)
Definition BLI_task.hh:251
BMesh Iterators.
void BKE_mesh_batch_cache_dirty_tag(Mesh *mesh, eMeshBatchDirtyMode mode)
void BKE_mesh_runtime_clear_cache(Mesh *mesh)
int BKE_mesh_runtime_corner_tris_len(const Mesh *mesh)
MINLINE int poly_to_tri_count(int poly_count, int corner_count)
@ ME_NO_OVERLAPPING_TOPOLOGY
@ ME_FLAG_UV_SELECT_SYNC_VALID
void BKE_id_free(Main *bmain, void *idv)
eMeshBatchDirtyMode
Definition BKE_mesh.h:47
void(*) BKE_mesh_batch_cache_free_cb(draw::MeshBatchCache *batch_cache)
void BKE_mesh_batch_cache_free(draw::MeshBatchCache *batch_cache)
void BKE_mesh_runtime_ensure_edit_data(Mesh *mesh)
bool BKE_mesh_face_normals_are_dirty(const Mesh *mesh)
void BKE_mesh_runtime_clear_geometry(Mesh *mesh)
void(*) BKE_mesh_batch_cache_dirty_tag_cb(Mesh *mesh, eMeshBatchDirtyMode mode)
bke::MeshRuntime * runtime
eMesh_Flag flag
SharedCache< std::unique_ptr< BVHTree, BVHTreeDeleter > > bvh_cache_loose_verts
SharedCache< std::unique_ptr< BVHTree, BVHTreeDeleter > > bvh_cache_loose_edges_no_hidden
SharedCache< bke::bvh::Tree > bvh_embree_tris_cache
SharedCache< std::unique_ptr< BVHTree, BVHTreeDeleter > > bvh_cache_corner_tris
SharedCache< bke::bvh::Tree > bvh_embree_edges_cache
SharedCache< std::unique_ptr< BVHTree, BVHTreeDeleter > > bvh_cache_edges
SharedCache< std::unique_ptr< BVHTree, BVHTreeDeleter > > bvh_cache_loose_verts_no_hidden
SharedCache< bke::bvh::Tree > bvh_embree_verts_cache
SharedCache< std::unique_ptr< BVHTree, BVHTreeDeleter > > bvh_cache_loose_edges
SharedCache< std::unique_ptr< BVHTree, BVHTreeDeleter > > bvh_cache_faces
draw::MeshBatchCache * batch_cache
SharedCache< std::unique_ptr< BVHTree, BVHTreeDeleter > > bvh_cache_corner_tris_no_hidden
SharedCache< Array< int3 > > data