Blender V5.3
draw_cache_extract_mesh_render_data.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
10
11#include "BLI_array.hh"
12#include "BLI_array_utils.hh"
14#include "BLI_math_geom_c.hh"
15#include "BLI_task.hh"
16#include "BLI_virtual_array.hh"
17
18#include "BKE_attribute.hh"
19#include "BKE_editmesh.hh"
20#include "BKE_editmesh_cache.hh"
21#include "BKE_material.hh"
22#include "BKE_mesh.hh"
23#include "BKE_object.hh"
24#include "BKE_object_types.hh"
25
26#include "ED_mesh.hh"
27
28#include "DRW_render.hh"
29
31
32/* ---------------------------------------------------------------------- */
35
36namespace blender::draw {
37
39{
41 cache.loose_geom.verts = mr.mesh->loose_verts();
42 cache.loose_geom.edges = mr.mesh->loose_edges();
43 }
44 else {
45 BMesh &bm = *mr.bm;
46 /* Use LinearAllocator instead of IndexMaskMemory to avoid over-allocation. */
47 cache.loose_geom.allocator = std::make_unique<LinearAllocator<>>();
49 IndexRange(bm.totvert), *cache.loose_geom.allocator, [&](const int i) {
50 return BM_vert_at_index(&bm, i)->e == nullptr;
51 });
53 IndexRange(bm.totedge), *cache.loose_geom.allocator, [&](const int i) {
54 return BM_edge_at_index(&bm, i)->l == nullptr;
55 });
56 }
57}
58
60{
61 /* Early exit: Are loose geometry already available.
62 * Only checking for loose verts as loose edges and verts are calculated at the same time. */
63 if (!cache.loose_geom.verts.is_empty()) {
64 return;
65 }
67}
68
79
81
82/* ---------------------------------------------------------------------- */
87
90{
91 threading::parallel_for(IndexRange(bm.totface), 1024, [&](const IndexRange range) {
92 Array<int> &tri_counts = all_tri_counts.local();
93 const short last_index = tri_counts.size() - 1;
94 for (const int i : range) {
95 const BMFace &face = *BM_face_at_index(&const_cast<BMesh &>(bm), i);
96 if (!BM_elem_flag_test(&face, BM_ELEM_HIDDEN)) {
97 const short mat = std::clamp<short>(face.mat_nr, 0, last_index);
98 tri_counts[mat] += face.len - 2;
99 }
100 }
101 });
102}
103
106{
107 const OffsetIndices faces = mr.faces;
108 const Span<bool> hide_poly = mr.hide_poly;
109 const Span material_indices = mr.material_indices;
110 if (material_indices.is_empty()) {
111 if (!hide_poly.is_empty()) {
112 all_tri_counts.local().first() = threading::parallel_reduce(
113 faces.index_range(),
114 4096,
115 0,
116 [&](const IndexRange range, int count) {
117 for (const int face : range) {
118 if (!hide_poly[face]) {
119 count += bke::mesh::face_triangles_num(faces[face].size());
120 }
121 }
122 return count;
123 },
124 std::plus<>());
125 }
126 else {
127 all_tri_counts.local().first() = poly_to_tri_count(mr.faces_num, mr.corners_num);
128 }
129 return;
130 }
131
132 threading::parallel_for(material_indices.index_range(), 1024, [&](const IndexRange range) {
133 Array<int> &tri_counts = all_tri_counts.local();
134 const int last_index = tri_counts.size() - 1;
135 if (!hide_poly.is_empty()) {
136 for (const int i : range) {
137 if (!hide_poly[i]) {
138 const int mat = std::clamp(material_indices[i], 0, last_index);
139 tri_counts[mat] += bke::mesh::face_triangles_num(faces[i].size());
140 }
141 }
142 }
143 else {
144 for (const int i : range) {
145 const int mat = std::clamp(material_indices[i], 0, last_index);
146 tri_counts[mat] += bke::mesh::face_triangles_num(faces[i].size());
147 }
148 }
149 });
150}
151
152/* Count how many triangles for each material. */
154{
156 [&]() { return Array<int>(mr.materials_num, 0); });
157
159 accumululate_material_counts_bm(*mr.bm, all_tri_counts);
160 }
161 else {
162 accumululate_material_counts_mesh(mr, all_tri_counts);
163 }
164
165 Array<int> &tris_num_by_material = all_tri_counts.local();
166 for (const Array<int> &counts : all_tri_counts) {
167 if (&counts != &tris_num_by_material) {
168 for (const int i : tris_num_by_material.index_range()) {
169 tris_num_by_material[i] += counts[i];
170 }
171 }
172 }
173 return std::move(tris_num_by_material);
174}
175
177 MutableSpan<int> material_tri_starts)
178{
179 BMesh &bm = *mr.bm;
180 Array<int> face_tri_offsets(bm.totface);
181#ifndef NDEBUG
182 face_tri_offsets.fill(-1);
183#endif
184
185 const int mat_last = mr.materials_num - 1;
186 BMIter iter;
187 BMFace *face;
188 int i;
189 BM_ITER_MESH_INDEX (face, &iter, &bm, BM_FACES_OF_MESH, i) {
191 continue;
192 }
193 const int mat = std::clamp(int(face->mat_nr), 0, mat_last);
194 face_tri_offsets[i] = material_tri_starts[mat];
195 material_tri_starts[mat] += face->len - 2;
196 }
197
198 return face_tri_offsets;
199}
200
201static bool mesh_is_single_material(const OffsetIndices<int> material_tri_starts)
202{
203 const int used_materials = std::count_if(
204 material_tri_starts.index_range().begin(),
205 material_tri_starts.index_range().end(),
206 [&](const int i) { return material_tri_starts[i].size() > 0; });
207 return used_materials == 1;
208}
209
210static std::optional<Array<int>> calc_face_tri_starts_mesh(const MeshRenderData &mr,
211 MutableSpan<int> material_tri_starts)
212{
213 const bool single_material = mesh_is_single_material(material_tri_starts.as_span());
214 if (single_material && mr.hide_poly.is_empty()) {
215 return std::nullopt;
216 }
217
218 const OffsetIndices faces = mr.faces;
219 const Span<bool> hide_poly = mr.hide_poly;
220
221 Array<int> face_tri_offsets(faces.size());
222#ifndef NDEBUG
223 face_tri_offsets.fill(-1);
224#endif
225
226 if (single_material) {
227 int offset = 0;
228 for (const int face : faces.index_range()) {
229 if (hide_poly[face]) {
230 continue;
231 }
232 face_tri_offsets[face] = offset;
233 offset += bke::mesh::face_triangles_num(faces[face].size());
234 }
235 return face_tri_offsets;
236 }
237
238 const Span<int> material_indices = mr.material_indices;
239 const int mat_last = mr.materials_num - 1;
240 for (const int face : faces.index_range()) {
241 if (!hide_poly.is_empty() && hide_poly[face]) {
242 continue;
243 }
244 const int mat = std::clamp(material_indices[face], 0, mat_last);
245 face_tri_offsets[face] = material_tri_starts[mat];
246 material_tri_starts[mat] += bke::mesh::face_triangles_num(faces[face].size());
247 }
248
249 return face_tri_offsets;
250}
251
253{
254 SortedFaceData cache;
256 const Span<int> tris_num_by_material = cache.tris_num_by_material;
257
258 Array<int, 32> material_tri_starts(mr.materials_num + 1);
259 material_tri_starts.as_mutable_span().drop_back(1).copy_from(tris_num_by_material);
261 cache.visible_tris_num = material_tri_starts.last();
262
263 /* Sort per material. */
265 cache.face_tri_offsets = calc_face_tri_starts_bmesh(mr, material_tri_starts);
266 }
267 else {
268 cache.face_tri_offsets = calc_face_tri_starts_mesh(mr, material_tri_starts);
269 }
270 return cache;
271}
272
274 MeshBufferCache &cache)
275{
276 if (cache.face_sorted.visible_tris_num == 0) {
278 }
279 return cache.face_sorted;
280}
281
283
284/* ---------------------------------------------------------------------- */
287
288static bool bm_edge_is_sharp(const BMEdge *const &edge)
289{
290 return !BM_elem_flag_test(edge, BM_ELEM_SMOOTH);
291}
292
293static bool bm_face_is_sharp(const BMFace *const &face)
294{
295 return !BM_elem_flag_test(face, BM_ELEM_SMOOTH);
296}
297
303{
304 if (bm->totface == 0) {
306 }
307
308 if (CustomData_has_layer_named(&bm->vdata, CD_PROP_FLOAT3, "custom_normal")) {
310 }
311 if (CustomData_has_layer_named(&bm->pdata, CD_PROP_FLOAT3, "custom_normal")) {
313 }
314 if (CustomData_has_layer_named(&bm->ldata, CD_PROP_FLOAT3, "custom_normal")) {
316 }
317 if (CustomData_has_layer_named(&bm->ldata, CD_PROP_INT16_2D, "custom_normal")) {
319 }
320
322 const VArray<bool> sharp_faces =
324 Span(bm->ftable, bm->totface));
325 const array_utils::BooleanMix face_mix = array_utils::booleans_mix_calc(sharp_faces);
326 if (face_mix == array_utils::BooleanMix::AllTrue) {
328 }
329
331 const VArray<bool> sharp_edges =
333 Span(bm->etable, bm->totedge));
334 const array_utils::BooleanMix edge_mix = array_utils::booleans_mix_calc(sharp_edges);
335 if (edge_mix == array_utils::BooleanMix::AllTrue) {
337 }
338
339 if (edge_mix == array_utils::BooleanMix::AllFalse &&
341 {
343 }
344
346}
347
349{
351 mr.corner_normals = mr.mesh->corner_normals();
352 }
353 else {
356 {
357 /* If there are free custom normals they should be used directly. */
358 mr.bm_loop_normals = {};
359 return;
360 }
362 const int clnors_offset = CustomData_get_offset_named(
363 &mr.bm->ldata, CD_PROP_INT16_2D, "custom_normal");
368 true,
370 nullptr,
371 nullptr,
372 clnors_offset,
373 false);
374 }
375}
376
378{
380 /* Eager calculation of face normals can reduce waiting on the lazy cache's lock. */
381 mr.face_normals = mr.mesh->face_normals();
382 }
383 else {
384 /* Use #BMFace.no. */
385 }
386}
387
389 const Object &object,
390 const Mesh &mesh)
391{
392 const Mesh &mesh_final = editmesh_final_or_this(object, mesh);
394 mesh_final.default_color_attribute;
396}
397
398static const Mesh *mesh_get_original_mesh(const Object &object)
399{
400 BLI_assert(object.type == OB_MESH);
401 if (const ID *data_orig = object.runtime->data_orig) {
402 if (data_orig->id_type() == ID_ME) {
403 return id_cast<const Mesh *>(data_orig);
404 }
405 }
406 return nullptr;
407}
408
410 Mesh &mesh,
411 const bool is_editmode,
412 const bool is_paint_mode,
413 const bool do_final,
414 const bool do_uvedit,
415 const bool use_hide,
416 const ToolSettings *ts)
417{
418 MeshRenderData mr{};
419 mr.toolsettings = ts;
421
422 mr.use_hide = use_hide;
423
424 const Mesh *orig_mesh = mesh_get_original_mesh(object);
425 if (BMEditMesh *edit_mesh = orig_mesh ? orig_mesh->runtime->edit_mesh.get() : nullptr) {
426 const Mesh *eval_cage = DRW_object_get_editmesh_cage_for_drawing(object);
427
428 mr.bm = const_cast<BMesh *>(BKE_editmesh_bmesh_get(orig_mesh));
429 mr.edit_bmesh = edit_mesh;
430 mr.mesh = (do_final) ? &mesh : eval_cage;
431 mr.edit_data = is_editmode ? mr.mesh->runtime->edit_data.get() : nullptr;
432
433 /* If there is no distinct cage, hide unmapped edges that can't be selected. */
434 mr.hide_unmapped_edges = !do_final || &mesh == eval_cage;
435
437 &mr.bm->vdata, CD_PROP_FLOAT3, "custom_normal");
439 &mr.bm->pdata, CD_PROP_FLOAT3, "custom_normal");
441 &mr.bm->ldata, CD_PROP_FLOAT3, "custom_normal");
442
443 if (bke::EditMeshData *emd = mr.edit_data) {
444 if (!emd->vert_positions.is_empty()) {
446 if (mr.bm_free_normal_offset_vert == -1) {
448 }
449 if (mr.bm_free_normal_offset_face == -1) {
451 }
452 }
453 }
454
455 int bm_ensure_types = BM_VERT | BM_EDGE | BM_LOOP | BM_FACE;
456
457 BM_mesh_elem_index_ensure(mr.bm, bm_ensure_types);
458 BM_mesh_elem_table_ensure(mr.bm, bm_ensure_types & ~BM_LOOP);
459
460 mr.efa_act_uv = EDBM_uv_active_face_get(const_cast<Mesh *>(mr.mesh), false, false);
461 mr.efa_act = BM_mesh_active_face_get(mr.bm, false, true);
464
468 &mr.bm->edata, CD_PROP_FLOAT, "bevel_weight_edge");
469#ifdef WITH_FREESTYLE
471 &mr.bm->edata, CD_PROP_BOOL, "freestyle_edge");
473 &mr.bm->pdata, CD_PROP_BOOL, "freestyle_face");
474#endif
475
476 /* Use bmesh directly when the object is unchanged by any modifiers. For non-final UVs, always
477 * use original bmesh since the UV editor does not support using the cage mesh with deformed
478 * coordinates. */
479 if ((mr.mesh->runtime->is_original_bmesh &&
481 (do_uvedit && !do_final))
482 {
484 }
485 else {
487
488 /* Use mapping from final to original mesh when the object is in edit mode. */
489 if (is_editmode && do_final) {
490 mr.orig_index_vert = static_cast<const int *>(
492 mr.orig_index_edge = static_cast<const int *>(
494 mr.orig_index_face = static_cast<const int *>(
496 }
497 else {
498 mr.orig_index_vert = nullptr;
499 mr.orig_index_edge = nullptr;
500 mr.orig_index_face = nullptr;
501 }
502 }
503 }
504 else {
505 mr.mesh = &mesh;
506 mr.edit_bmesh = nullptr;
508 mr.hide_unmapped_edges = false;
509
510 if (is_paint_mode && mr.mesh) {
511 mr.orig_index_vert = static_cast<const int *>(
513 mr.orig_index_edge = static_cast<const int *>(
515 mr.orig_index_face = static_cast<const int *>(
517 }
518 else {
519 mr.orig_index_vert = nullptr;
520 mr.orig_index_edge = nullptr;
521 mr.orig_index_face = nullptr;
522 }
523 }
524
526 mr.verts_num = mr.mesh->verts_num;
527 mr.edges_num = mr.mesh->edges_num;
528 mr.faces_num = mr.mesh->faces_num;
531
532 mr.vert_positions = mr.mesh->vert_positions();
533 mr.edges = mr.mesh->edges();
534 mr.faces = mr.mesh->faces();
535 mr.corner_verts = mr.mesh->corner_verts();
536 mr.corner_edges = mr.mesh->corner_edges();
537
538 mr.orig_index_vert = static_cast<const int *>(
540 mr.orig_index_edge = static_cast<const int *>(
542 mr.orig_index_face = static_cast<const int *>(
544
545 mr.normals_domain = mr.mesh->normals_domain();
546
547 const bke::AttributeAccessor attributes = mr.mesh->attributes();
548
549 mr.material_indices = *attributes.lookup<int>("material_index", bke::AttrDomain::Face);
550
551 if (is_editmode || is_paint_mode) {
552 if (use_hide) {
553 mr.hide_vert = *attributes.lookup<bool>(".hide_vert", bke::AttrDomain::Point);
554 mr.hide_edge = *attributes.lookup<bool>(".hide_edge", bke::AttrDomain::Edge);
555 mr.hide_poly = *attributes.lookup<bool>(".hide_poly", bke::AttrDomain::Face);
556 }
557
558 mr.select_vert = *attributes.lookup<bool>(".select_vert", bke::AttrDomain::Point);
559 mr.select_edge = *attributes.lookup<bool>(".select_edge", bke::AttrDomain::Edge);
560 mr.select_poly = *attributes.lookup<bool>(".select_poly", bke::AttrDomain::Face);
561 }
562
563 mr.sharp_faces = *attributes.lookup<bool>("sharp_face", bke::AttrDomain::Face);
564 }
565 else {
566 BMesh *bm = mr.bm;
567
568 mr.verts_num = bm->totvert;
569 mr.edges_num = bm->totedge;
570 mr.faces_num = bm->totface;
571 mr.corners_num = bm->totloop;
573
575 }
576
577 retrieve_active_attribute_names(mr, object, *mr.mesh);
578
579 return mr;
580}
581
583
584} // namespace blender::draw
General operations, lookup, etc. for materials.
General operations, lookup, etc. for blender objects.
#define BM_elem_flag_test(ele, hflag)
#define BM_ITER_MESH_INDEX(ele, iter, bm, itype, indexvar)
constexpr Span()=default
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
const T & last(const int64_t n=0) const
Definition BLI_array.hh:307
IndexRange index_range() const
Definition BLI_array.hh:371
void fill(const T &value) const
Definition BLI_array.hh:283
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
static IndexMask from_predicate(const IndexMask &universe, LinearAllocator<> &memory, Fn &&predicate, exec_mode::Mode mode=exec_mode::parallel)
constexpr Iterator end() const
constexpr IndexRange()=default
constexpr Iterator begin() const
constexpr Span< T > as_span() const
Definition BLI_span.hh:667
constexpr bool is_empty() const
Definition BLI_span.hh:264
static VArray from_derived_span(Span< StructT > values)
GAttributeReader lookup(const StringRef name) const
constexpr bool is_empty() const
Extraction of Mesh data into VBO to feed to GPU.
BooleanMix booleans_mix_calc(const VArray< bool > &varray, IndexRange range_to_check)
int face_triangles_num(const int face_size)
Definition BKE_mesh.hh:350
static Array< int > calc_face_tri_starts_bmesh(const MeshRenderData &mr, MutableSpan< int > material_tri_starts)
void mesh_render_data_update_face_normals(MeshRenderData &mr)
static Array< int > mesh_render_data_mat_tri_len_build(const MeshRenderData &mr)
static bool mesh_is_single_material(const OffsetIndices< int > material_tri_starts)
static void mesh_render_data_loose_geom_ensure(const MeshRenderData &mr, MeshBufferCache &cache)
static bke::MeshNormalDomain bmesh_normals_domain(BMesh *bm)
static void accumululate_material_counts_mesh(const MeshRenderData &mr, threading::EnumerableThreadSpecific< Array< int > > &all_tri_counts)
static void accumululate_material_counts_bm(const BMesh &bm, threading::EnumerableThreadSpecific< Array< int > > &all_tri_counts)
static void retrieve_active_attribute_names(MeshRenderData &mr, const Object &object, const Mesh &mesh)
static std::optional< Array< int > > calc_face_tri_starts_mesh(const MeshRenderData &mr, MutableSpan< int > material_tri_starts)
const Mesh & editmesh_final_or_this(const Object &object, const Mesh &mesh)
void mesh_render_data_update_corner_normals(MeshRenderData &mr)
static const Mesh * mesh_get_original_mesh(const Object &object)
const SortedFaceData & mesh_render_data_faces_sorted_ensure(const MeshRenderData &mr, MeshBufferCache &cache)
MeshRenderData mesh_render_data_create(Object &object, Mesh &mesh, const bool is_editmode, const bool is_paint_mode, const bool do_final, const bool do_uvedit, const bool use_hide, const ToolSettings *ts)
void mesh_render_data_update_loose_geom(MeshRenderData &mr, MeshBufferCache &cache)
static void mesh_render_data_loose_geom_build(const MeshRenderData &mr, MeshBufferCache &cache)
static SortedFaceData mesh_render_data_faces_sorted_build(const MeshRenderData &mr)
static bool bm_face_is_sharp(const BMFace *const &face)
static bool bm_edge_is_sharp(const BMEdge *const &edge)
OffsetIndices< int > accumulate_counts_to_offsets(MutableSpan< int > counts_to_offsets, int start_offset=0)
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
Value parallel_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
Definition BLI_task.hh:127
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
Span< float3 > BKE_editmesh_cache_ensure_vert_normals(BMesh &bm, bke::EditMeshData &emd)
void BM_mesh_elem_index_ensure(BMesh *bm, const char htype)
int BKE_object_material_used_with_fallback_eval(const Object &ob)
void BM_mesh_elem_table_ensure(BMesh *bm, const char htype)
void BM_loops_calc_normal_vcos(BMesh *bm, const Span< float3 > vcos, const Span< float3 > vnos, const Span< float3 > fnos, const bool use_split_normals, MutableSpan< float3 > r_lnos, MLoopNorSpaceArray *r_lnors_spacearr, short(*clnors_data)[2], const int cd_loop_clnors_offset, const bool do_rebuild)
BMesh Compute Loop Normals from/to external data.
BMEdge * BM_mesh_active_edge_get(BMesh *bm)
int CustomData_get_offset_named(const CustomData *data, eCustomDataType type, StringRef name)
const void * CustomData_get_layer(const CustomData *data, eCustomDataType type)
MINLINE int poly_to_tri_count(int poly_count, int corner_count)
BMFace * EDBM_uv_active_face_get(Mesh *mesh, bool sloppy, bool selected)
BMFace * BM_mesh_active_face_get(BMesh *bm, const bool is_sloppy, const bool is_selected)
const Mesh * DRW_object_get_editmesh_cage_for_drawing(const Object &object)
const BMesh * BKE_editmesh_bmesh_get(const Object *ob)
Definition editmesh.cc:65
Span< float3 > BKE_editmesh_cache_ensure_face_normals(BMesh &bm, bke::EditMeshData &emd)
@ ME_WRAPPER_TYPE_BMESH
bool CustomData_has_layer_named(const CustomData *data, eCustomDataType type, StringRef name)
BMVert * BM_mesh_active_vert_get(BMesh *bm)
BMEdge ** etable
CustomData pdata
CustomData vdata
CustomData edata
CustomData ldata
BMFace ** ftable
bke::MeshRuntime * runtime
CustomData face_data
char * active_color_attribute
CustomData edge_data
char * default_color_attribute
CustomData vert_data
std::shared_ptr< BMEditMesh > edit_mesh
std::unique_ptr< EditMeshData > edit_data
eMeshWrapperType wrapper_type
std::unique_ptr< LinearAllocator<> > allocator
bke::EditMeshData * edit_data
const ToolSettings * toolsettings
VArraySpan< bool > select_vert
VArraySpan< bool > sharp_faces
VArraySpan< bool > select_poly
VArraySpan< bool > select_edge
VArraySpan< int > material_indices
OffsetIndices< int > faces
bke::MeshNormalDomain normals_domain
std::optional< Array< int > > face_tri_offsets