Blender V5.3
draw_pbvh.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11
13#include "BLI_map.hh"
14#include "BLI_math_geom_c.hh"
16#include "BLI_utildefines.hh"
17#include "BLI_vector.hh"
18
19#include "DNA_object_types.h"
20
21#include "BKE_attribute.hh"
23#include "BKE_attribute_math.hh"
24#include "BKE_customdata.hh"
25#include "BKE_mesh.hh"
26#include "BKE_paint.hh"
27#include "BKE_paint_bvh.hh"
28#include "BKE_subdiv_ccg.hh"
29#include "BKE_subdiv_eval.hh"
30
32
33#include "GPU_batch.hh"
34
35#include "DRW_engine.hh"
36#include "DRW_pbvh.hh"
37#include "DRW_render.hh"
38
39#include "PRF_profile.hh"
40
41#include "attribute_convert.hh"
42#include "bmesh.hh"
43
44namespace blender {
45
48 {
49 using namespace draw::pbvh;
50 if (const CustomRequest *request_type = std::get_if<CustomRequest>(&value)) {
51 return get_default_hash(*request_type);
52 }
53 const GenericRequest &attr = std::get<GenericRequest>(value);
54 return get_default_hash(attr);
55 }
56};
57
58namespace draw::pbvh {
59
64
80 : active_color(mesh.active_color_attribute),
81 default_color(mesh.default_color_attribute),
82 active_uv_map(mesh.active_uv_map_name()),
83 default_uv_map(mesh.default_uv_map_name()),
84 face_set_default(mesh.face_sets_color_default),
85 face_set_seed(mesh.face_sets_color_seed),
87 {
88 uv_map_names.add_multiple(mesh.uv_map_names().as_span().cast<StringRef>());
89 }
90};
91
96class DrawCacheImpl : public DrawCache {
97 struct AttributeData {
107 BitVector<> dirty_nodes;
112 void tag_dirty(const IndexMask &node_mask);
113 };
114
116 BitVector<> use_flat_layout_;
118 Array<int> material_indices_;
119
121 Vector<gpu::IndexBufPtr> lines_ibos_;
123 Vector<gpu::IndexBufPtr> lines_ibos_coarse_;
125 Vector<gpu::IndexBufPtr> tris_ibos_;
127 Vector<gpu::IndexBufPtr> tris_ibos_coarse_;
133
135 Vector<gpu::Batch *> lines_batches_;
137 Vector<gpu::Batch *> lines_batches_coarse_;
144
153 BitVector<> dirty_topology_;
154
155 public:
156 ~DrawCacheImpl() override;
157
158 void tag_positions_changed(const IndexMask &node_mask) override;
159 void tag_visibility_changed(const IndexMask &node_mask) override;
160 void tag_topology_changed(const IndexMask &node_mask) override;
161 void tag_face_sets_changed(const IndexMask &node_mask) override;
162 void tag_masks_changed(const IndexMask &node_mask) override;
163 void tag_attribute_changed(const IndexMask &node_mask, StringRef attribute_name) override;
164
166 const ViewportRequest &request,
167 const IndexMask &nodes_to_update) override;
168
170 const ViewportRequest &request,
171 const IndexMask &nodes_to_update) override;
172
173 Span<int> ensure_material_indices(const Object &object) override;
174
175 private:
180 void free_nodes_with_changed_topology(const bke::pbvh::Tree &pbvh);
181
182 BitSpan ensure_use_flat_layout(const Object &object, const OrigMeshData &orig_mesh_data);
183
184 Span<gpu::VertBufPtr> ensure_attribute_data(const Object &object,
185 const OrigMeshData &orig_mesh_data,
186 const AttributeRequest &attr,
187 const IndexMask &node_mask);
188
189 Span<gpu::IndexBufPtr> ensure_tri_indices(const Object &object,
190 const OrigMeshData &orig_mesh_data,
191 const IndexMask &node_mask,
192 bool coarse);
193
194 Span<gpu::IndexBufPtr> ensure_lines_indices(const Object &object,
195 const OrigMeshData &orig_mesh_data,
196 const IndexMask &node_mask,
197 bool coarse);
198};
199
200void DrawCacheImpl::AttributeData::tag_dirty(const IndexMask &node_mask)
201{
202 this->dirty_nodes.resize(std::max(this->dirty_nodes.size(), node_mask.min_array_size()), false);
203 node_mask.set_bits(this->dirty_nodes);
204}
205
207{
208 if (DrawCacheImpl::AttributeData *data = attribute_vbos_.lookup_ptr(CustomRequest::Position)) {
209 data->tag_dirty(node_mask);
210 }
211 if (DrawCacheImpl::AttributeData *data = attribute_vbos_.lookup_ptr(CustomRequest::Normal)) {
212 data->tag_dirty(node_mask);
213 }
214}
215
217{
218 dirty_topology_.resize(std::max(dirty_topology_.size(), node_mask.min_array_size()), false);
219 node_mask.set_bits(dirty_topology_);
220}
221
223{
224 /* Currently the only times where topology changes are for BMesh dynamic topology, where tagging
225 * a visibility update deletes all the GPU data anyway. */
226 this->tag_visibility_changed(node_mask);
227}
228
230{
231 if (DrawCacheImpl::AttributeData *data = attribute_vbos_.lookup_ptr(CustomRequest::FaceSet)) {
232 data->tag_dirty(node_mask);
233 }
234}
235
237{
238 if (DrawCacheImpl::AttributeData *data = attribute_vbos_.lookup_ptr(CustomRequest::Mask)) {
239 data->tag_dirty(node_mask);
240 }
241}
242
243void DrawCacheImpl::tag_attribute_changed(const IndexMask &node_mask, StringRef attribute_name)
244{
245 for (const auto &[data_request, data] : attribute_vbos_.items()) {
246 if (const GenericRequest *request = std::get_if<GenericRequest>(&data_request)) {
247 if (*request == attribute_name) {
248 data.tag_dirty(node_mask);
249 }
250 }
251 }
252}
253
254DrawCache &ensure_draw_data(std::unique_ptr<bke::pbvh::DrawCache> &ptr)
255{
256 if (!ptr) {
257 ptr = std::make_unique<DrawCacheImpl>();
258 }
259 return dynamic_cast<DrawCache &>(*ptr);
260}
261
263 const IndexMask &node_mask)
264{
265 IndexMaskMemory memory;
266 const IndexMask mask = IndexMask::from_intersection(node_mask, ibos.index_range(), memory);
267 mask.foreach_index([&](const int i) { ibos[i].reset(); });
268}
269
271 const IndexMask &node_mask)
272{
273 IndexMaskMemory memory;
274 const IndexMask mask = IndexMask::from_intersection(node_mask, vbos.index_range(), memory);
275 mask.foreach_index([&](const int i) { vbos[i].reset(); });
276}
277
279 const IndexMask &node_mask)
280{
281 IndexMaskMemory memory;
282 const IndexMask mask = IndexMask::from_intersection(node_mask, batches.index_range(), memory);
283 mask.foreach_index([&](const int i) { GPU_BATCH_DISCARD_SAFE(batches[i]); });
284}
285
287{
288 static const GPUVertFormat format = GPU_vertformat_from_attribute(
289 "pos", gpu::VertAttrType::SFLOAT_32_32_32);
290 return format;
291}
292
294{
295 static const GPUVertFormat format = GPU_vertformat_from_attribute(
296 "nor", gpu::VertAttrType::SNORM_16_16_16_16);
297 return format;
298}
299
301{
302 static const GPUVertFormat format = GPU_vertformat_from_attribute("msk",
303 gpu::VertAttrType::SFLOAT_32);
304 return format;
305}
306
308{
309 static const GPUVertFormat format = GPU_vertformat_from_attribute(
310 "fset", gpu::VertAttrType::UNORM_8_8_8_8);
311 return format;
312}
313
314static GPUVertFormat attribute_format(const OrigMeshData &orig_mesh_data,
315 const StringRef name,
316 const bke::AttrType data_type)
317{
318 GPUVertFormat format = init_format_for_attribute(data_type, "data");
319
320 bool is_render, is_active;
321 const char *prefix = "a";
322
324 prefix = "c";
325 is_active = orig_mesh_data.active_color == name;
326 is_render = orig_mesh_data.default_color == name;
327 }
328 if (data_type == bke::AttrType::Float2) {
329 prefix = "u";
330 is_active = orig_mesh_data.active_uv_map == name;
331 is_render = orig_mesh_data.default_uv_map == name;
332 }
333
334 DRW_cdlayer_attr_aliases_add(&format, prefix, data_type, name, is_render, is_active);
335 return format;
336}
337
339{
342 return short4(result.x, result.y, result.z, 0);
343}
344
345template<typename T>
347 const Span<int> corner_verts,
348 const Span<T> attribute,
349 const Span<int> face_indices,
350 gpu::VertBuf &vbo)
351{
354 using VBOType = typename Converter::VBOType;
355 VBOType *data = vbo.data<VBOType>().data();
356 for (const int face : face_indices) {
357 for (const int vert : corner_verts.slice(faces[face])) {
358 *data = Converter::convert(attribute[vert]);
359 data++;
360 }
361 }
362}
363
364template<typename T>
366 const Span<T> attribute,
367 const Span<int> face_indices,
368 gpu::VertBuf &vbo)
369{
372 using VBOType = typename Converter::VBOType;
373
374 VBOType *data = vbo.data<VBOType>().data();
375 for (const int face : face_indices) {
376 const int face_size = faces[face].size();
377 std::fill_n(data, face_size, Converter::convert(attribute[face]));
378 data += face_size;
379 }
380}
381
382template<typename T>
384 const Span<T> attribute,
385 const Span<int> face_indices,
386 gpu::VertBuf &vbo)
387{
390 using VBOType = typename Converter::VBOType;
391
392 VBOType *data = vbo.data<VBOType>().data();
393 for (const int face : face_indices) {
394 for (const int corner : faces[face]) {
395 *data = Converter::convert(attribute[corner]);
396 data++;
397 }
398 }
399}
400
401template<typename T> const T &bmesh_cd_vert_get(const BMVert &vert, const int offset)
402{
403 return *static_cast<const T *>(POINTER_OFFSET(vert.head.data, offset));
404}
405
406template<typename T> const T &bmesh_cd_loop_get(const BMLoop &loop, const int offset)
407{
408 return *static_cast<const T *>(POINTER_OFFSET(loop.head.data, offset));
409}
410
411template<typename T> const T &bmesh_cd_face_get(const BMFace &face, const int offset)
412{
413 return *static_cast<const T *>(POINTER_OFFSET(face.head.data, offset));
414}
415
416template<typename T>
417void extract_data_vert_bmesh(const Set<BMFace *, 0> &faces, const int cd_offset, gpu::VertBuf &vbo)
418{
421 using VBOType = typename Converter::VBOType;
422 VBOType *data = vbo.data<VBOType>().data();
423
424 for (const BMFace *face : faces) {
426 continue;
427 }
428 const BMLoop *l = face->l_first;
429 *data = Converter::convert(bmesh_cd_vert_get<T>(*l->prev->v, cd_offset));
430 data++;
431 *data = Converter::convert(bmesh_cd_vert_get<T>(*l->v, cd_offset));
432 data++;
433 *data = Converter::convert(bmesh_cd_vert_get<T>(*l->next->v, cd_offset));
434 data++;
435 }
436}
437
438template<typename T>
439void extract_data_face_bmesh(const Set<BMFace *, 0> &faces, const int cd_offset, gpu::VertBuf &vbo)
440{
443 using VBOType = typename Converter::VBOType;
444 VBOType *data = vbo.data<VBOType>().data();
445
446 for (const BMFace *face : faces) {
448 continue;
449 }
450 std::fill_n(data, 3, Converter::convert(bmesh_cd_face_get<T>(*face, cd_offset)));
451 data += 3;
452 }
453}
454
455template<typename T>
457 const int cd_offset,
458 gpu::VertBuf &vbo)
459{
462 using VBOType = typename Converter::VBOType;
463 VBOType *data = vbo.data<VBOType>().data();
464
465 for (const BMFace *face : faces) {
467 continue;
468 }
469 const BMLoop *l = face->l_first;
470 *data = Converter::convert(bmesh_cd_loop_get<T>(*l->prev, cd_offset));
471 data++;
472 *data = Converter::convert(bmesh_cd_loop_get<T>(*l, cd_offset));
473 data++;
474 *data = Converter::convert(bmesh_cd_loop_get<T>(*l->next, cd_offset));
475 data++;
476 }
477}
478
480{
481 return std::count_if(faces.begin(), faces.end(), [&](const BMFace *face) {
482 return !BM_elem_flag_test_bool(face, BM_ELEM_HIDDEN);
483 });
484}
485
487{
488 free_batches(lines_batches_, lines_batches_.index_range());
489 free_batches(lines_batches_coarse_, lines_batches_coarse_.index_range());
490 for (MutableSpan<gpu::Batch *> batches : tris_batches_.values()) {
491 free_batches(batches, batches.index_range());
492 }
493}
494
495void DrawCacheImpl::free_nodes_with_changed_topology(const bke::pbvh::Tree &pbvh)
496{
498 /* NOTE: Theoretically we shouldn't need to free batches with a changed triangle count, but
499 * currently it's the simplest way to reallocate all the GPU data while keeping everything in a
500 * consistent state. */
501 IndexMaskMemory memory;
502 const IndexMask nodes_to_free = IndexMask::from_bits(dirty_topology_, memory);
503 if (nodes_to_free.is_empty()) {
504 return;
505 }
506
507 dirty_topology_.clear_and_shrink();
508
509 free_ibos(lines_ibos_, nodes_to_free);
510 free_ibos(lines_ibos_coarse_, nodes_to_free);
511 free_ibos(tris_ibos_, nodes_to_free);
512 free_ibos(tris_ibos_coarse_, nodes_to_free);
513 if (pbvh.type() == bke::pbvh::Type::BMesh) {
514 /* For BMesh, VBOs are only filled with data for visible triangles, and topology can also
515 * completely change due to dynamic topology, so VBOs must be rebuilt from scratch. For other
516 * types, actual topology doesn't change, and visibility changes are accounted for by the index
517 * buffers. */
518 for (AttributeData &data : attribute_vbos_.values()) {
519 free_vbos(data.vbos, nodes_to_free);
520 }
521 }
522
523 free_batches(lines_batches_, nodes_to_free);
524 free_batches(lines_batches_coarse_, nodes_to_free);
525 for (MutableSpan<gpu::Batch *> batches : tris_batches_.values()) {
526 free_batches(batches, nodes_to_free);
527 }
528}
529
531 const GPUVertFormat &format,
532 const IndexMask &node_mask,
534{
538 node_mask.foreach_index(
539 [&](const int i) {
540 if (!vbos[i]) {
541 vbos[i] = gpu::VertBufPtr(GPU_vertbuf_create_with_format(format));
542 }
543 GPU_vertbuf_data_alloc(*vbos[i], nodes[i].corners_num());
544 },
546}
547
549 const GPUVertFormat &format,
550 const BitSpan use_flat_layout,
551 const IndexMask &node_mask,
553{
557 const SubdivCCG &subdiv_ccg = *object.runtime->sculpt_session->subdiv_ccg;
558 node_mask.foreach_index(
559 [&](const int i) {
560 if (!vbos[i]) {
561 vbos[i] = gpu::VertBufPtr(GPU_vertbuf_create_with_format(format));
562 }
563 const int verts_per_grid = use_flat_layout[i] ? square_i(subdiv_ccg.grid_size - 1) * 4 :
564 square_i(subdiv_ccg.grid_size);
565 const int verts_num = nodes[i].grids().size() * verts_per_grid;
566 GPU_vertbuf_data_alloc(*vbos[i], verts_num);
567 },
569}
570
572 const GPUVertFormat &format,
573 const IndexMask &node_mask,
575{
579 node_mask.foreach_index(
580 [&](const int i) {
581 if (!vbos[i]) {
582 vbos[i] = gpu::VertBufPtr(GPU_vertbuf_create_with_format(format));
583 }
585 &const_cast<bke::pbvh::BMeshNode &>(nodes[i]));
586 const int verts_num = count_visible_tris_bmesh(faces) * 3;
587 GPU_vertbuf_data_alloc(*vbos[i], verts_num);
588 },
590}
591
592static void update_positions_mesh(const Object &object,
593 const IndexMask &node_mask,
595{
600 const OffsetIndices<int> faces = mesh.faces();
601 const Span<int> corner_verts = mesh.corner_verts();
602 const Span<float3> vert_positions = bke::pbvh::vert_positions_eval_from_eval(object);
603 ensure_vbos_allocated_mesh(object, position_format(), node_mask, vbos);
604 node_mask.foreach_index(
605 [&](const int i) {
607 faces, corner_verts, vert_positions, nodes[i].faces(), *vbos[i]);
608 },
610}
611
612static void update_normals_mesh(const Object &object,
613 const IndexMask &node_mask,
615{
620 const OffsetIndices<int> faces = mesh.faces();
621 const Span<int> corner_verts = mesh.corner_verts();
622 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval_from_eval(object);
623 const Span<float3> face_normals = bke::pbvh::face_normals_eval_from_eval(object);
624 const bke::AttributeAccessor attributes = mesh.attributes();
625 const VArraySpan sharp_faces = *attributes.lookup<bool>("sharp_face", bke::AttrDomain::Face);
626 ensure_vbos_allocated_mesh(object, normal_format(), node_mask, vbos);
627 node_mask.foreach_index(
628 [&](const int i) {
629 short4 *data = vbos[i]->data<short4>().data();
630
631 for (const int face : nodes[i].faces()) {
632 if (!sharp_faces.is_empty() && sharp_faces[face]) {
633 const int face_size = faces[face].size();
634 std::fill_n(data, face_size, normal_float_to_short(face_normals[face]));
635 data += face_size;
636 }
637 else {
638 for (const int vert : corner_verts.slice(faces[face])) {
639 *data = normal_float_to_short(vert_normals[vert]);
640 data++;
641 }
642 }
643 }
644 },
646}
647
648BLI_NOINLINE static void update_masks_mesh(const Object &object,
649 const OrigMeshData &orig_mesh_data,
650 const IndexMask &node_mask,
652{
657 const OffsetIndices<int> faces = mesh.faces();
658 const Span<int> corner_verts = mesh.corner_verts();
659 const VArraySpan mask = *orig_mesh_data.attributes.lookup<float>(".sculpt_mask",
661 ensure_vbos_allocated_mesh(object, mask_format(), node_mask, vbos);
662 if (!mask.is_empty()) {
663 node_mask.foreach_index(
664 [&](const int i) {
665 float *data = vbos[i]->data<float>().data();
666 for (const int face : nodes[i].faces()) {
667 for (const int vert : corner_verts.slice(faces[face])) {
668 *data = mask[vert];
669 data++;
670 }
671 }
672 },
674 }
675 else {
676 node_mask.foreach_index([&](const int i) { vbos[i]->data<float>().fill(0.0f); },
678 }
679}
680
681BLI_NOINLINE static void update_face_sets_mesh(const Object &object,
682 const OrigMeshData &orig_mesh_data,
683 const IndexMask &node_mask,
685{
690 const OffsetIndices<int> faces = mesh.faces();
691 const int color_default = orig_mesh_data.face_set_default;
692 const int color_seed = orig_mesh_data.face_set_seed;
693 const VArraySpan face_sets = *orig_mesh_data.attributes.lookup<int>(".sculpt_face_set",
695 ensure_vbos_allocated_mesh(object, face_set_format(), node_mask, vbos);
696 if (!face_sets.is_empty()) {
697 node_mask.foreach_index(
698 [&](const int i) {
699 uchar4 *data = vbos[i]->data<uchar4>().data();
700 for (const int face : nodes[i].faces()) {
701 const int id = face_sets[face];
702
703 uchar4 fset_color(UCHAR_MAX);
704 if (id != color_default) {
705 BKE_paint_face_set_overlay_color_get(id, color_seed, fset_color);
706 }
707 else {
708 /* Skip for the default color face set to render it white. */
709 fset_color[0] = fset_color[1] = fset_color[2] = UCHAR_MAX;
710 }
711
712 const int face_size = faces[face].size();
713 std::fill_n(data, face_size, fset_color);
714 data += face_size;
715 }
716 },
718 }
719 else {
720 node_mask.foreach_index([&](const int i) { vbos[i]->data<uchar4>().fill(uchar4(255)); },
722 }
723}
724
726 const OrigMeshData &orig_mesh_data,
727 const IndexMask &node_mask,
728 const StringRef name,
730{
735 const OffsetIndices<int> faces = mesh.faces();
736 const Span<int> corner_verts = mesh.corner_verts();
737 const bke::AttributeAccessor attributes = orig_mesh_data.attributes;
738 const bke::GAttributeReader attr = attributes.lookup(name);
739 if (!attr || attr.domain == bke::AttrDomain::Edge) {
740 return;
741 }
744 object, attribute_format(orig_mesh_data, name, data_type), node_mask, vbos);
745 node_mask.foreach_index(
746 [&](const int i) {
747 bke::attribute_math::to_static_type(attr.varray.type(), [&]<typename T>() {
748 if constexpr (!std::is_void_v<typename AttributeConverter<T>::VBOType>) {
749 const VArraySpan<T> src = attr.varray.typed<T>();
750 switch (attr.domain) {
751 case bke::AttrDomain::Point:
752 extract_data_vert_mesh<T>(faces, corner_verts, src, nodes[i].faces(), *vbos[i]);
753 break;
754 case bke::AttrDomain::Face:
755 extract_data_face_mesh<T>(faces, src, nodes[i].faces(), *vbos[i]);
756 break;
757 case bke::AttrDomain::Corner:
758 extract_data_corner_mesh<T>(faces, src, nodes[i].faces(), *vbos[i]);
759 break;
760 default:
761 BLI_assert_unreachable();
762 }
763 }
764 });
765 },
767}
768
769BLI_NOINLINE static void fill_positions_grids(const Object &object,
770 const BitSpan use_flat_layout,
771 const IndexMask &node_mask,
773{
777 const SubdivCCG &subdiv_ccg = *object.runtime->sculpt_session->subdiv_ccg;
778 const Span<float3> positions = subdiv_ccg.positions;
779 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
780 ensure_vbos_allocated_grids(object, position_format(), use_flat_layout, node_mask, vbos);
781 node_mask.foreach_index(
782 [&](const int i) {
783 float3 *data = vbos[i]->data<float3>().data();
784 if (use_flat_layout[i]) {
785 const int grid_size_1 = key.grid_size - 1;
786 for (const int grid : nodes[i].grids()) {
787 const Span<float3> grid_positions = positions.slice(bke::ccg::grid_range(key, grid));
788 for (int y = 0; y < grid_size_1; y++) {
789 for (int x = 0; x < grid_size_1; x++) {
790 *data = grid_positions[CCG_grid_xy_to_index(key.grid_size, x, y)];
791 data++;
792 *data = grid_positions[CCG_grid_xy_to_index(key.grid_size, x + 1, y)];
793 data++;
794 *data = grid_positions[CCG_grid_xy_to_index(key.grid_size, x + 1, y + 1)];
795 data++;
796 *data = grid_positions[CCG_grid_xy_to_index(key.grid_size, x, y + 1)];
797 data++;
798 }
799 }
800 }
801 }
802 else {
803 for (const int grid : nodes[i].grids()) {
804 const Span<float3> grid_positions = positions.slice(bke::ccg::grid_range(key, grid));
805 std::copy_n(grid_positions.data(), grid_positions.size(), data);
806 data += grid_positions.size();
807 }
808 }
809 },
811}
812
813BLI_NOINLINE static void fill_normals_grids(const Object &object,
814 const OrigMeshData &orig_mesh_data,
815 const BitSpan use_flat_layout,
816 const IndexMask &node_mask,
818{
822 const SubdivCCG &subdiv_ccg = *object.runtime->sculpt_session->subdiv_ccg;
823 const Span<float3> positions = subdiv_ccg.positions;
824 const Span<float3> normals = subdiv_ccg.normals;
825 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
826 const Span<int> grid_to_face_map = subdiv_ccg.grid_to_face_map;
827 const bke::AttributeAccessor attributes = orig_mesh_data.attributes;
828 const VArraySpan sharp_faces = *attributes.lookup<bool>("sharp_face", bke::AttrDomain::Face);
829 ensure_vbos_allocated_grids(object, normal_format(), use_flat_layout, node_mask, vbos);
830 node_mask.foreach_index(
831 [&](const int i) {
832 short4 *data = vbos[i]->data<short4>().data();
833
834 if (use_flat_layout[i]) {
835 const int grid_size_1 = key.grid_size - 1;
836 for (const int grid : nodes[i].grids()) {
837 const Span<float3> grid_positions = positions.slice(bke::ccg::grid_range(key, grid));
838 const Span<float3> grid_normals = normals.slice(bke::ccg::grid_range(key, grid));
839 if (!sharp_faces.is_empty() && sharp_faces[grid_to_face_map[grid]]) {
840 for (int y = 0; y < grid_size_1; y++) {
841 for (int x = 0; x < grid_size_1; x++) {
842 float3 no;
844 grid_positions[CCG_grid_xy_to_index(key.grid_size, x, y + 1)],
845 grid_positions[CCG_grid_xy_to_index(key.grid_size, x + 1, y + 1)],
846 grid_positions[CCG_grid_xy_to_index(key.grid_size, x + 1, y)],
847 grid_positions[CCG_grid_xy_to_index(key.grid_size, x, y)]);
848 std::fill_n(data, 4, normal_float_to_short(no));
849 data += 4;
850 }
851 }
852 }
853 else {
854 for (int y = 0; y < grid_size_1; y++) {
855 for (int x = 0; x < grid_size_1; x++) {
856 std::fill_n(data,
857 4,
859 grid_normals[CCG_grid_xy_to_index(key.grid_size, x, y)]));
860 data += 4;
861 }
862 }
863 }
864 }
865 }
866 else {
867 /* The non-flat VBO layout does not support sharp faces. */
868 for (const int grid : nodes[i].grids()) {
869 for (const float3 &normal : normals.slice(bke::ccg::grid_range(key, grid))) {
870 *data = normal_float_to_short(normal);
871 data++;
872 }
873 }
874 }
875 },
877}
878
879BLI_NOINLINE static void fill_masks_grids(const Object &object,
880 const BitSpan use_flat_layout,
881 const IndexMask &node_mask,
883{
887 const SubdivCCG &subdiv_ccg = *object.runtime->sculpt_session->subdiv_ccg;
888 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
889 const Span<float> masks = subdiv_ccg.masks;
890 ensure_vbos_allocated_grids(object, mask_format(), use_flat_layout, node_mask, vbos);
891 if (!masks.is_empty()) {
892 node_mask.foreach_index(
893 [&](const int i) {
894 float *data = vbos[i]->data<float>().data();
895 if (use_flat_layout[i]) {
896 const int grid_size_1 = key.grid_size - 1;
897 for (const int grid : nodes[i].grids()) {
898 const Span<float> grid_masks = masks.slice(bke::ccg::grid_range(key, grid));
899 for (int y = 0; y < grid_size_1; y++) {
900 for (int x = 0; x < grid_size_1; x++) {
901 *data = grid_masks[CCG_grid_xy_to_index(key.grid_size, x, y)];
902 data++;
903 *data = grid_masks[CCG_grid_xy_to_index(key.grid_size, x + 1, y)];
904 data++;
905 *data = grid_masks[CCG_grid_xy_to_index(key.grid_size, x + 1, y + 1)];
906 data++;
907 *data = grid_masks[CCG_grid_xy_to_index(key.grid_size, x, y + 1)];
908 data++;
909 }
910 }
911 }
912 }
913 else {
914 for (const int grid : nodes[i].grids()) {
915 const Span<float> grid_masks = masks.slice(bke::ccg::grid_range(key, grid));
916 std::copy_n(grid_masks.data(), grid_masks.size(), data);
917 data += grid_masks.size();
918 }
919 }
920 },
922 }
923 else {
924 node_mask.foreach_index([&](const int i) { vbos[i]->data<float>().fill(0.0f); },
926 }
927}
928
929BLI_NOINLINE static void fill_face_sets_grids(const Object &object,
930 const OrigMeshData &orig_mesh_data,
931 const BitSpan use_flat_layout,
932 const IndexMask &node_mask,
934{
938 const SubdivCCG &subdiv_ccg = *object.runtime->sculpt_session->subdiv_ccg;
939 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
940 const int color_default = orig_mesh_data.face_set_default;
941 const int color_seed = orig_mesh_data.face_set_seed;
942 const Span<int> grid_to_face_map = subdiv_ccg.grid_to_face_map;
943 const bke::AttributeAccessor attributes = orig_mesh_data.attributes;
944 ensure_vbos_allocated_grids(object, face_set_format(), use_flat_layout, node_mask, vbos);
945 if (const VArray<int> face_sets = *attributes.lookup<int>(".sculpt_face_set",
947 {
948 const VArraySpan<int> face_sets_span(face_sets);
949 node_mask.foreach_index(
950 [&](const int i) {
951 const Span<int> grids = nodes[i].grids();
952 const int verts_per_grid = use_flat_layout[i] ? square_i(key.grid_size - 1) * 4 :
953 square_i(key.grid_size);
954 uchar4 *data = vbos[i]->data<uchar4>().data();
955 for (const int i : grids.index_range()) {
956 uchar4 color{UCHAR_MAX};
957 const int fset = face_sets[grid_to_face_map[grids[i]]];
958 if (fset != color_default) {
960 }
961
962 std::fill_n(data, verts_per_grid, color);
963 data += verts_per_grid;
964 }
965 },
967 }
968 else {
969 node_mask.foreach_index([&](const int i) { vbos[i]->data<uchar4>().fill(uchar4{UCHAR_MAX}); },
971 }
972}
973
974static void calc_quad_uv(const SubdivCCG &subdiv_ccg,
975 const int uv_map_index,
976 const float grid_size_1_inv,
977 const int ptex_face_index,
978 const int grid_start,
979 const int corner,
980 const int x,
981 const int y,
982 const int offset,
984{
985 const float grid_u = x * grid_size_1_inv;
986 const float grid_v = y * grid_size_1_inv;
987 float u;
988 float v;
989 bke::subdiv::rotate_grid_to_quad(corner, grid_u, grid_v, &u, &v);
991 subdiv_ccg.subdiv, uv_map_index, ptex_face_index, u, v, result[grid_start + offset]);
992}
993
994static void calc_non_quad_uv(const SubdivCCG &subdiv_ccg,
995 const int uv_map_index,
996 const float grid_size_1_inv,
997 const int ptex_face_index,
998 const int grid_start,
999 const int x,
1000 const int y,
1001 const int offset,
1003{
1004 const float u = 1.0f - (y * grid_size_1_inv);
1005 const float v = 1.0f - (x * grid_size_1_inv);
1007 subdiv_ccg.subdiv, uv_map_index, ptex_face_index, u, v, result[grid_start + offset]);
1008}
1009
1010static void calc_node_uvs_for_flat_layout(const SubdivCCG &subdiv_ccg,
1011 const int uv_map_index,
1012 Span<int> grids,
1014{
1015 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
1016 const int grid_size_1 = key.grid_size - 1;
1017 const int grid_area_1 = grid_size_1 * grid_size_1;
1018 const int verts_per_grid = grid_area_1 * 4;
1019
1020 const Span<int> face_ptex_offset = bke::subdiv::face_ptex_offset_get(subdiv_ccg.subdiv);
1021 const float grid_size_1_inv = 1.0f / (key.grid_size - 1);
1022
1023 threading::parallel_for(grids.index_range(), 1024, [&](const IndexRange range) {
1024 for (const int i : range) {
1025 const int grid = grids[i];
1026 const int face_index = subdiv_ccg.grid_to_face_map[grid];
1027 const IndexRange face = subdiv_ccg.faces[face_index];
1028 const int corner = grid - face.start();
1029 if (face.size() == 4) {
1030 const int ptex_face_index = face_ptex_offset[face_index];
1031 for (int y = 0; y < grid_size_1; y++) {
1032 for (int x = 0; x < grid_size_1; x++) {
1033 const int start = verts_per_grid * i + CCG_grid_xy_to_index(grid_size_1, x, y) * 4;
1034 calc_quad_uv(subdiv_ccg,
1035 uv_map_index,
1036 grid_size_1_inv,
1037 ptex_face_index,
1038 start,
1039 corner,
1040 x,
1041 y,
1042 0,
1043 result);
1044 calc_quad_uv(subdiv_ccg,
1045 uv_map_index,
1046 grid_size_1_inv,
1047 ptex_face_index,
1048 start,
1049 corner,
1050 x + 1,
1051 y,
1052 1,
1053 result);
1054 calc_quad_uv(subdiv_ccg,
1055 uv_map_index,
1056 grid_size_1_inv,
1057 ptex_face_index,
1058 start,
1059 corner,
1060 x + 1,
1061 y + 1,
1062 2,
1063 result);
1064 calc_quad_uv(subdiv_ccg,
1065 uv_map_index,
1066 grid_size_1_inv,
1067 ptex_face_index,
1068 start,
1069 corner,
1070 x,
1071 y + 1,
1072 3,
1073 result);
1074 }
1075 }
1076 }
1077 else {
1078 const int ptex_face_index = face_ptex_offset[face_index] + corner;
1079 for (int y = 0; y < grid_size_1; y++) {
1080 for (int x = 0; x < grid_size_1; x++) {
1081 const int start = verts_per_grid * i + CCG_grid_xy_to_index(grid_size_1, x, y) * 4;
1082 calc_non_quad_uv(subdiv_ccg,
1083 uv_map_index,
1084 grid_size_1_inv,
1085 ptex_face_index,
1086 start,
1087 x,
1088 y,
1089 0,
1090 result);
1091 calc_non_quad_uv(subdiv_ccg,
1092 uv_map_index,
1093 grid_size_1_inv,
1094 ptex_face_index,
1095 start,
1096 x + 1,
1097 y,
1098 1,
1099 result);
1100 calc_non_quad_uv(subdiv_ccg,
1101 uv_map_index,
1102 grid_size_1_inv,
1103 ptex_face_index,
1104 start,
1105 x + 1,
1106 y + 1,
1107 2,
1108 result);
1109 calc_non_quad_uv(subdiv_ccg,
1110 uv_map_index,
1111 grid_size_1_inv,
1112 ptex_face_index,
1113 start,
1114 x,
1115 y + 1,
1116 3,
1117 result);
1118 }
1119 }
1120 }
1121 }
1122 });
1123}
1124
1125static void calc_node_uvs_for_standard_layout(const SubdivCCG &subdiv_ccg,
1126 const int uv_map_index,
1127 Span<int> grids,
1129{
1130 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
1131 const int grid_size = key.grid_size;
1132 const int grid_area = key.grid_area;
1133
1134 const Span<int> face_ptex_offset = bke::subdiv::face_ptex_offset_get(subdiv_ccg.subdiv);
1135 const float grid_size_1_inv = 1.0f / (grid_size - 1);
1136 threading::parallel_for(grids.index_range(), 1024, [&](const IndexRange range) {
1137 for (const int i : range) {
1138 const int grid = grids[i];
1139 const int face_index = subdiv_ccg.grid_to_face_map[grid];
1140 const IndexRange face = subdiv_ccg.faces[face_index];
1141 const int corner = grid - face.start();
1142 if (face.size() == 4) {
1143 const int ptex_face_index = face_ptex_offset[face_index];
1144 for (int y = 0; y < grid_size; y++) {
1145 const float grid_v = y * grid_size_1_inv;
1146 for (int x = 0; x < grid_size; x++) {
1147 const float grid_u = x * grid_size_1_inv;
1148 float u;
1149 float v;
1150 bke::subdiv::rotate_grid_to_quad(corner, grid_u, grid_v, &u, &v);
1151 const int element = i * grid_area + CCG_grid_xy_to_index(grid_size, x, y);
1152 bke::subdiv::eval_face_varying(
1153 subdiv_ccg.subdiv, uv_map_index, ptex_face_index, u, v, result[element]);
1154 }
1155 }
1156 }
1157 else {
1158 const int ptex_face_index = face_ptex_offset[face_index] + corner;
1159 for (int y = 0; y < grid_size; y++) {
1160 const float u = 1.0f - (y * grid_size_1_inv);
1161 for (int x = 0; x < grid_size; x++) {
1162 const float v = 1.0f - (x * grid_size_1_inv);
1163 const int element = i * grid_area + CCG_grid_xy_to_index(grid_size, x, y);
1164 bke::subdiv::eval_face_varying(
1165 subdiv_ccg.subdiv, uv_map_index, ptex_face_index, u, v, result[element]);
1166 }
1167 }
1168 }
1169 }
1170 });
1171}
1172
1173static void calc_node_uvs(const SubdivCCG &subdiv_ccg,
1174 const int uv_map_index,
1176 const bool use_flat_layout,
1178{
1179 const Span<int> grids = node.grids();
1180 if (use_flat_layout) {
1181 calc_node_uvs_for_flat_layout(subdiv_ccg, uv_map_index, grids, result);
1182 }
1183 if (!use_flat_layout) {
1184 calc_node_uvs_for_standard_layout(subdiv_ccg, uv_map_index, grids, result);
1185 }
1186}
1187
1188BLI_NOINLINE static void fill_uvs_grids(const Object &object,
1189 const OrigMeshData &orig_mesh_data,
1190 const BitSpan use_flat_layout,
1191 const IndexMask &node_mask,
1192 const StringRef name,
1194{
1196 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
1198 const SubdivCCG &subdiv_ccg = *object.runtime->sculpt_session->subdiv_ccg;
1200 attribute_format(orig_mesh_data, name, bke::AttrType::Float2),
1201 use_flat_layout,
1202 node_mask,
1203 vbos);
1204 node_mask.foreach_index(
1205 [&](const int i) {
1206 float2 *data = vbos[i]->data<float2>().data();
1207
1208 const int uv_channel = orig_mesh_data.uv_map_names.index_of_try(name);
1209 if (uv_channel == -1) [[unlikely]] {
1210 BLI_assert_msg(uv_channel != -1,
1211 "Unable to find appropriate FVar channel for given UV map");
1212 std::fill_n(data, vbos[i]->data<float2>().size(), float2(0));
1213 }
1214 else {
1215 calc_node_uvs(subdiv_ccg,
1216 uv_channel,
1217 nodes[i],
1218 use_flat_layout[i],
1219 {data, vbos[i]->data<float2>().size()});
1220 }
1221 },
1223}
1224
1225static float4 quad_weights_from_grid(const int corner,
1226 const int x,
1227 const int y,
1228 const float grid_size_1_inv)
1229{
1230 const float grid_u = x * grid_size_1_inv;
1231 const float grid_v = y * grid_size_1_inv;
1232 float u;
1233 float v;
1234 bke::subdiv::rotate_grid_to_quad(corner, grid_u, grid_v, &u, &v);
1236}
1237
1239 const int y,
1240 const float grid_size_1_inv)
1241{
1242 const float u = 1.0f - (y * grid_size_1_inv);
1243 const float v = 1.0f - (x * grid_size_1_inv);
1244
1246}
1247
1248template<typename T>
1250 const Span<int> corner_verts,
1251 const Span<int> grids,
1252 const Span<T> src,
1253 gpu::VertBuf &vbo)
1254{
1256 using VBOType = typename Converter::VBOType;
1257
1258 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
1259 const int grid_size_1 = key.grid_size - 1;
1260 const int grid_area_1 = grid_size_1 * grid_size_1;
1261 const int verts_per_grid = grid_area_1 * 4;
1262 const float grid_size_1_inv = 1.0f / (key.grid_size - 1);
1263 MutableSpan<VBOType> dst = vbo.data<VBOType>();
1264
1265 threading::parallel_for(grids.index_range(), 1024, [&](const IndexRange range) {
1266 for (const int i : range) {
1267 const int grid = grids[i];
1268 const int face_index = subdiv_ccg.grid_to_face_map[grid];
1269 const IndexRange face = subdiv_ccg.faces[face_index];
1270 const int corner = grid - face.start();
1271 if (face.size() == 4) {
1272 Span<int> face_verts = corner_verts.slice(face);
1273 for (int y = 0; y < grid_size_1; y++) {
1274 for (int x = 0; x < grid_size_1; x++) {
1275 const int start = verts_per_grid * i + CCG_grid_xy_to_index(grid_size_1, x, y) * 4;
1276 dst[start] = Converter::convert(
1277 bke::attribute_math::mix4(quad_weights_from_grid(corner, x, y, grid_size_1_inv),
1278 src[face_verts[0]],
1279 src[face_verts[1]],
1280 src[face_verts[2]],
1281 src[face_verts[3]]));
1282 dst[start + 1] = Converter::convert(bke::attribute_math::mix4(
1283 quad_weights_from_grid(corner, x + 1, y, grid_size_1_inv),
1284 src[face_verts[0]],
1285 src[face_verts[1]],
1286 src[face_verts[2]],
1287 src[face_verts[3]]));
1288 dst[start + 2] = Converter::convert(bke::attribute_math::mix4(
1289 quad_weights_from_grid(corner, x + 1, y + 1, grid_size_1_inv),
1290 src[face_verts[0]],
1291 src[face_verts[1]],
1292 src[face_verts[2]],
1293 src[face_verts[3]]));
1294 dst[start + 3] = Converter::convert(bke::attribute_math::mix4(
1295 quad_weights_from_grid(corner, x, y + 1, grid_size_1_inv),
1296 src[face_verts[0]],
1297 src[face_verts[1]],
1298 src[face_verts[2]],
1299 src[face_verts[3]]));
1300 }
1301 }
1302 }
1303 else {
1304 const std::array<T, 4> values =
1305 bke::subdiv::interpolate_vert_data_for_subdivided_irregular_face<T>(
1306 corner_verts, face, grid, src);
1307 for (int y = 0; y < grid_size_1; y++) {
1308 for (int x = 0; x < grid_size_1; x++) {
1309 const int start = verts_per_grid * i + CCG_grid_xy_to_index(grid_size_1, x, y) * 4;
1310 dst[start] = Converter::convert(bke::attribute_math::mix4(
1311 quad_weights_from_irregular_corner(x, y, grid_size_1_inv),
1312 values[0],
1313 values[1],
1314 values[2],
1315 values[3]));
1316 dst[start + 1] = Converter::convert(bke::attribute_math::mix4(
1317 quad_weights_from_irregular_corner(x + 1, y, grid_size_1_inv),
1318 values[0],
1319 values[1],
1320 values[2],
1321 values[3]));
1322 dst[start + 2] = Converter::convert(bke::attribute_math::mix4(
1323 quad_weights_from_irregular_corner(x + 1, y + 1, grid_size_1_inv),
1324 values[0],
1325 values[1],
1326 values[2],
1327 values[3]));
1328 dst[start + 3] = Converter::convert(bke::attribute_math::mix4(
1329 quad_weights_from_irregular_corner(x, y + 1, grid_size_1_inv),
1330 values[0],
1331 values[1],
1332 values[2],
1333 values[3]));
1334 }
1335 }
1336 }
1337 }
1338 });
1339}
1340
1341template<typename T>
1343 const Span<int> corner_verts,
1344 const Span<int> grids,
1345 const Span<T> src,
1346 gpu::VertBuf &vbo)
1347{
1349 using VBOType = typename Converter::VBOType;
1350
1351 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
1352 const int grid_size = key.grid_size;
1353 const int grid_area = key.grid_area;
1354
1355 const float grid_size_1_inv = 1.0f / (grid_size - 1);
1356 MutableSpan<VBOType> dst = vbo.data<VBOType>();
1357 threading::parallel_for(grids.index_range(), 1024, [&](const IndexRange range) {
1358 for (const int i : range) {
1359 const int grid = grids[i];
1360 const int face_index = subdiv_ccg.grid_to_face_map[grid];
1361 const IndexRange face = subdiv_ccg.faces[face_index];
1362 const int corner = grid - face.start();
1363 if (face.size() == 4) {
1364 const Span<int> face_verts = corner_verts.slice(face);
1365 for (int y = 0; y < grid_size; y++) {
1366 for (int x = 0; x < grid_size; x++) {
1367 const int element = i * grid_area + CCG_grid_xy_to_index(grid_size, x, y);
1368 dst[element] = Converter::convert(
1369 bke::attribute_math::mix4(quad_weights_from_grid(corner, x, y, grid_size_1_inv),
1370 src[face_verts[0]],
1371 src[face_verts[1]],
1372 src[face_verts[2]],
1373 src[face_verts[3]]));
1374 }
1375 }
1376 }
1377 else {
1378 const std::array<T, 4> values =
1379 bke::subdiv::interpolate_vert_data_for_subdivided_irregular_face<T>(
1380 corner_verts, face, grid, src);
1381 for (int y = 0; y < grid_size; y++) {
1382 for (int x = 0; x < grid_size; x++) {
1383 const int element = i * grid_area + CCG_grid_xy_to_index(grid_size, x, y);
1384 dst[element] = Converter::convert(bke::attribute_math::mix4(
1385 quad_weights_from_irregular_corner(x, y, grid_size_1_inv),
1386 values[0],
1387 values[1],
1388 values[2],
1389 values[3]));
1390 }
1391 }
1392 }
1393 }
1394 });
1395}
1396
1398 const OrigMeshData &orig_mesh_data,
1399 const BitSpan &use_flat_layout,
1400 const IndexMask &node_mask,
1401 const StringRef name,
1403{
1405 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
1407 const SubdivCCG &subdiv_ccg = *object.runtime->sculpt_session->subdiv_ccg;
1408 const bke::AttributeAccessor attributes = orig_mesh_data.attributes;
1409 const GVArraySpan attr = *attributes.lookup(name);
1410
1412 const Span<int> corner_verts = mesh.corner_verts();
1413
1414 node_mask.foreach_index(
1415 [&](const int i) {
1416 bke::attribute_math::to_static_type(attr.type(), [&]<typename T>() {
1417 if constexpr (!std::is_void_v<typename AttributeConverter<T>::VBOType>) {
1418 if (use_flat_layout[i]) {
1419 interpolate_vert_data_for_flat_layout<T>(
1420 subdiv_ccg, corner_verts, nodes[i].grids(), attr.typed<T>(), *vbos[i]);
1421 }
1422 else {
1423 interpolate_vert_data_for_standard_layout<T>(
1424 subdiv_ccg, corner_verts, nodes[i].grids(), attr.typed<T>(), *vbos[i]);
1425 }
1426 }
1427 });
1428 },
1430}
1431
1432template<typename T>
1434 const Span<int> grids,
1435 const Span<T> src,
1436 gpu::VertBuf &vbo)
1437{
1439 using VBOType = typename Converter::VBOType;
1440
1441 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
1442 const int grid_size_1 = key.grid_size - 1;
1443 const int grid_area_1 = grid_size_1 * grid_size_1;
1444 const int verts_per_grid = grid_area_1 * 4;
1445 const float grid_size_1_inv = 1.0f / (key.grid_size - 1);
1446 MutableSpan<VBOType> dst = vbo.data<VBOType>();
1447
1448 threading::parallel_for(grids.index_range(), 1024, [&](const IndexRange range) {
1449 for (const int i : range) {
1450 const int grid = grids[i];
1451 const int face_index = subdiv_ccg.grid_to_face_map[grid];
1452 const IndexRange face = subdiv_ccg.faces[face_index];
1453 const int corner = grid - face.start();
1454 if (face.size() == 4) {
1455 for (int y = 0; y < grid_size_1; y++) {
1456 for (int x = 0; x < grid_size_1; x++) {
1457 const int start = verts_per_grid * i + CCG_grid_xy_to_index(grid_size_1, x, y) * 4;
1458 dst[start] = Converter::convert(
1459 bke::attribute_math::mix4(quad_weights_from_grid(corner, x, y, grid_size_1_inv),
1460 src[face[0]],
1461 src[face[1]],
1462 src[face[2]],
1463 src[face[3]]));
1464 dst[start + 1] = Converter::convert(bke::attribute_math::mix4(
1465 quad_weights_from_grid(corner, x + 1, y, grid_size_1_inv),
1466 src[face[0]],
1467 src[face[1]],
1468 src[face[2]],
1469 src[face[3]]));
1470 dst[start + 2] = Converter::convert(bke::attribute_math::mix4(
1471 quad_weights_from_grid(corner, x + 1, y + 1, grid_size_1_inv),
1472 src[face[0]],
1473 src[face[1]],
1474 src[face[2]],
1475 src[face[3]]));
1476 dst[start + 3] = Converter::convert(bke::attribute_math::mix4(
1477 quad_weights_from_grid(corner, x, y + 1, grid_size_1_inv),
1478 src[face[0]],
1479 src[face[1]],
1480 src[face[2]],
1481 src[face[3]]));
1482 }
1483 }
1484 }
1485 else {
1486 const std::array<T, 4> values =
1487 bke::subdiv::interpolate_corner_data_for_subdivided_irregular_face<T>(face, grid, src);
1488 for (int y = 0; y < grid_size_1; y++) {
1489 for (int x = 0; x < grid_size_1; x++) {
1490 const int start = verts_per_grid * i + CCG_grid_xy_to_index(grid_size_1, x, y) * 4;
1491 dst[start] = Converter::convert(bke::attribute_math::mix4(
1492 quad_weights_from_irregular_corner(x, y, grid_size_1_inv),
1493 values[0],
1494 values[1],
1495 values[2],
1496 values[3]));
1497 dst[start + 1] = Converter::convert(bke::attribute_math::mix4(
1498 quad_weights_from_irregular_corner(x + 1, y, grid_size_1_inv),
1499 values[0],
1500 values[1],
1501 values[2],
1502 values[3]));
1503 dst[start + 2] = Converter::convert(bke::attribute_math::mix4(
1504 quad_weights_from_irregular_corner(x + 1, y + 1, grid_size_1_inv),
1505 values[0],
1506 values[1],
1507 values[2],
1508 values[3]));
1509 dst[start + 3] = Converter::convert(bke::attribute_math::mix4(
1510 quad_weights_from_irregular_corner(x, y + 1, grid_size_1_inv),
1511 values[0],
1512 values[1],
1513 values[2],
1514 values[3]));
1515 }
1516 }
1517 }
1518 }
1519 });
1520}
1521
1522template<typename T>
1524 const Span<int> grids,
1525 const Span<T> src,
1526 gpu::VertBuf &vbo)
1527{
1529 using VBOType = typename Converter::VBOType;
1530
1531 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
1532 const int grid_size = key.grid_size;
1533 const int grid_area = key.grid_area;
1534
1535 const float grid_size_1_inv = 1.0f / (grid_size - 1);
1536 MutableSpan<VBOType> dst = vbo.data<VBOType>();
1537 threading::parallel_for(grids.index_range(), 1024, [&](const IndexRange range) {
1538 for (const int i : range) {
1539 const int grid = grids[i];
1540 const int face_index = subdiv_ccg.grid_to_face_map[grid];
1541 const IndexRange face = subdiv_ccg.faces[face_index];
1542 const int corner = grid - face.start();
1543 if (face.size() == 4) {
1544 for (int y = 0; y < grid_size; y++) {
1545 for (int x = 0; x < grid_size; x++) {
1546 const int element = i * grid_area + CCG_grid_xy_to_index(grid_size, x, y);
1547 dst[element] = Converter::convert(
1548 bke::attribute_math::mix4(quad_weights_from_grid(corner, x, y, grid_size_1_inv),
1549 src[face[0]],
1550 src[face[1]],
1551 src[face[2]],
1552 src[face[3]]));
1553 }
1554 }
1555 }
1556 else {
1557 const std::array<T, 4> values =
1558 bke::subdiv::interpolate_corner_data_for_subdivided_irregular_face<T>(face, grid, src);
1559 for (int y = 0; y < grid_size; y++) {
1560 for (int x = 0; x < grid_size; x++) {
1561 const int element = i * grid_area + CCG_grid_xy_to_index(grid_size, x, y);
1562 dst[element] = Converter::convert(bke::attribute_math::mix4(
1563 quad_weights_from_irregular_corner(x, y, grid_size_1_inv),
1564 values[0],
1565 values[1],
1566 values[2],
1567 values[3]));
1568 }
1569 }
1570 }
1571 }
1572 });
1573}
1574
1576 const Object &object,
1577 const OrigMeshData &orig_mesh_data,
1578 const BitSpan &use_flat_layout,
1579 const IndexMask &node_mask,
1580 const StringRef name,
1582{
1584 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
1586 const SubdivCCG &subdiv_ccg = *object.runtime->sculpt_session->subdiv_ccg;
1587 const bke::AttributeAccessor attributes = orig_mesh_data.attributes;
1588 const GVArraySpan attr = *attributes.lookup(name);
1589
1590 node_mask.foreach_index(
1591 [&](const int i) {
1592 bke::attribute_math::to_static_type(attr.type(), [&]<typename T>() {
1593 if constexpr (!std::is_void_v<typename AttributeConverter<T>::VBOType>) {
1594 if (use_flat_layout[i]) {
1595 interpolate_corner_data_for_flat_layout<T>(
1596 subdiv_ccg, nodes[i].grids(), attr.typed<T>(), *vbos[i]);
1597 }
1598 else {
1599 interpolate_corner_data_for_standard_layout<T>(
1600 subdiv_ccg, nodes[i].grids(), attr.typed<T>(), *vbos[i]);
1601 }
1602 }
1603 });
1604 },
1606}
1607
1608template<typename T>
1609static void fill_face_data_grids(const SubdivCCG &subdiv_ccg,
1610 const bool use_flat_layout,
1611 const Span<int> grids,
1612 const Span<T> src,
1613 gpu::VertBuf &vbo)
1614{
1616 using VBOType = typename Converter::VBOType;
1617
1618 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
1619 const int verts_per_grid = use_flat_layout ? (key.grid_size - 1) * (key.grid_size - 1) * 4 :
1620 key.grid_area;
1621 VBOType *dst = vbo.data<VBOType>().data();
1622 for (const int i : grids.index_range()) {
1623 const int face_index = subdiv_ccg.grid_to_face_map[grids[i]];
1624 std::fill_n(dst, verts_per_grid, Converter::convert(src[face_index]));
1625 dst += verts_per_grid;
1626 }
1627}
1628
1630 const OrigMeshData &orig_mesh_data,
1631 const BitSpan &use_flat_layout,
1632 const IndexMask &node_mask,
1633 const StringRef name,
1635{
1637 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
1639 const SubdivCCG &subdiv_ccg = *object.runtime->sculpt_session->subdiv_ccg;
1640 const bke::AttributeAccessor attributes = orig_mesh_data.attributes;
1641 const GVArraySpan attr = *attributes.lookup(name);
1642
1643 node_mask.foreach_index(
1644 [&](const int i) {
1645 bke::attribute_math::to_static_type(attr.type(), [&]<typename T>() {
1646 if constexpr (!std::is_void_v<typename AttributeConverter<T>::VBOType>) {
1647 fill_face_data_grids<T>(
1648 subdiv_ccg, use_flat_layout[i], nodes[i].grids(), attr.typed<T>(), *vbos[i]);
1649 }
1650 });
1651 },
1653}
1654
1656 const OrigMeshData &orig_mesh_data,
1657 const BitSpan use_flat_layout,
1658 const IndexMask &node_mask,
1659 const StringRef name,
1661{
1663 const bke::AttributeAccessor attributes = orig_mesh_data.attributes;
1664 const bke::GAttributeReader attr = attributes.lookup(name);
1665 if (attr.domain == bke::AttrDomain::Edge) {
1666 return;
1667 }
1668
1669 if (!attr) {
1671 attribute_format(orig_mesh_data, "Dummy", bke::AttrType::Float3),
1672 use_flat_layout,
1673 node_mask,
1674 vbos);
1675 node_mask.foreach_index([&](const int i) { vbos[i]->data<float3>().fill(float3(0.0f)); },
1677 return;
1678 }
1679
1680 const bke::AttrType data_type = bke::cpp_type_to_attribute_type(attr.varray.type());
1681
1682 if (attr.domain == bke::AttrDomain::Corner && data_type == bke::AttrType::Float2) {
1683 fill_uvs_grids(object, orig_mesh_data, use_flat_layout, node_mask, name, vbos);
1684 return;
1685 }
1686
1687 switch (attr.domain) {
1690 attribute_format(orig_mesh_data, name, data_type),
1691 use_flat_layout,
1692 node_mask,
1693 vbos);
1695 object, orig_mesh_data, use_flat_layout, node_mask, name, vbos);
1696 break;
1699 attribute_format(orig_mesh_data, name, data_type),
1700 use_flat_layout,
1701 node_mask,
1702 vbos);
1704 object, orig_mesh_data, use_flat_layout, node_mask, name, vbos);
1705 break;
1708 attribute_format(orig_mesh_data, name, data_type),
1709 use_flat_layout,
1710 node_mask,
1711 vbos);
1713 object, orig_mesh_data, use_flat_layout, node_mask, name, vbos);
1714 break;
1715 default:
1717 }
1718}
1719
1721 const IndexMask &node_mask,
1723{
1725 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
1727 ensure_vbos_allocated_bmesh(object, position_format(), node_mask, vbos);
1728 node_mask.foreach_index(
1729 [&](const int i) {
1730 float3 *data = vbos[i]->data<float3>().data();
1731 for (const BMFace *face :
1733 {
1734 if (BM_elem_flag_test(face, BM_ELEM_HIDDEN)) {
1735 continue;
1736 }
1737 const BMLoop *l = face->l_first;
1738 *data = l->prev->v->co;
1739 data++;
1740 *data = l->v->co;
1741 data++;
1742 *data = l->next->v->co;
1743 data++;
1744 }
1745 },
1747}
1748
1749BLI_NOINLINE static void update_normals_bmesh(const Object &object,
1750 const IndexMask &node_mask,
1752{
1754 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
1756 ensure_vbos_allocated_bmesh(object, normal_format(), node_mask, vbos);
1757 node_mask.foreach_index(
1758 [&](const int i) {
1759 short4 *data = vbos[i]->data<short4>().data();
1760 for (const BMFace *face :
1762 {
1763 if (BM_elem_flag_test(face, BM_ELEM_HIDDEN)) {
1764 continue;
1765 }
1766 if (BM_elem_flag_test(face, BM_ELEM_SMOOTH)) {
1767 const BMLoop *l = face->l_first;
1769 data++;
1771 data++;
1773 data++;
1774 }
1775 else {
1776 std::fill_n(data, 3, normal_float_to_short(face->no));
1777 data += 3;
1778 }
1779 }
1780 },
1782}
1783
1784BLI_NOINLINE static void update_masks_bmesh(const Object &object,
1785 const IndexMask &node_mask,
1787{
1789 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
1791 const BMesh &bm = *object.runtime->sculpt_session->bm;
1792 const int cd_offset = CustomData_get_offset_named(&bm.vdata, CD_PROP_FLOAT, ".sculpt_mask");
1793 ensure_vbos_allocated_bmesh(object, mask_format(), node_mask, vbos);
1794 if (cd_offset != -1) {
1795 node_mask.foreach_index(
1796 [&](const int i) {
1797 float *data = vbos[i]->data<float>().data();
1798 for (const BMFace *face :
1800 {
1801 if (BM_elem_flag_test(face, BM_ELEM_HIDDEN)) {
1802 continue;
1803 }
1804 const BMLoop *l = face->l_first;
1805 *data = bmesh_cd_vert_get<float>(*l->prev->v, cd_offset);
1806 data++;
1807 *data = bmesh_cd_vert_get<float>(*l->v, cd_offset);
1808 data++;
1809 *data = bmesh_cd_vert_get<float>(*l->next->v, cd_offset);
1810 data++;
1811 }
1812 },
1814 }
1815 else {
1816 node_mask.foreach_index([&](const int i) { vbos[i]->data<float>().fill(0.0f); },
1818 }
1819}
1820
1822 const OrigMeshData &orig_mesh_data,
1823 const IndexMask &node_mask,
1825{
1827 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
1829 const BMesh &bm = *object.runtime->sculpt_session->bm;
1830 const int color_default = orig_mesh_data.face_set_default;
1831 const int color_seed = orig_mesh_data.face_set_seed;
1832 const int offset = CustomData_get_offset_named(&bm.pdata, CD_PROP_INT32, ".sculpt_face_set");
1833 ensure_vbos_allocated_bmesh(object, face_set_format(), node_mask, vbos);
1834 if (offset != -1) {
1835 node_mask.foreach_index(
1836 [&](const int i) {
1837 uchar4 *data = vbos[i]->data<uchar4>().data();
1838 for (const BMFace *face :
1840 {
1841 if (BM_elem_flag_test(face, BM_ELEM_HIDDEN)) {
1842 continue;
1843 }
1844 uchar4 color{UCHAR_MAX};
1845 const int fset = bmesh_cd_face_get<int>(*face, offset);
1846 if (fset != color_default) {
1848 }
1849 std::fill_n(data, 3, color);
1850 data += 3;
1851 }
1852 },
1854 }
1855 else {
1856 node_mask.foreach_index([&](const int i) { vbos[i]->data<uchar4>().fill(uchar4(255)); },
1858 }
1859}
1860
1862 const OrigMeshData &orig_mesh_data,
1863 const IndexMask &node_mask,
1864 const StringRef name,
1866{
1868 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
1870 const BMesh &bm = *object.runtime->sculpt_session->bm;
1871 const BMDataLayerLookup attr = BM_data_layer_lookup(bm, name);
1872 if (attr.domain == bke::AttrDomain::Edge) {
1873 return;
1874 }
1875
1876 if (!attr) {
1878 object, attribute_format(orig_mesh_data, name, bke::AttrType::Float3), node_mask, vbos);
1879 node_mask.foreach_index([&](const int i) { vbos[i]->data<float3>().fill(float3(0.0f)); },
1881 return;
1882 }
1883
1885 object, attribute_format(orig_mesh_data, name, attr.type), node_mask, vbos);
1886 node_mask.foreach_index(
1887 [&](const int i) {
1888 bke::attribute_math::to_static_type(attr.type, [&]<typename T>() {
1889 const auto &faces = BKE_pbvh_bmesh_node_faces(
1890 &const_cast<bke::pbvh::BMeshNode &>(nodes[i]));
1891 if constexpr (!std::is_void_v<typename AttributeConverter<T>::VBOType>) {
1892 switch (attr.domain) {
1893 case bke::AttrDomain::Point:
1894 extract_data_vert_bmesh<T>(faces, attr.offset, *vbos[i]);
1895 break;
1896 case bke::AttrDomain::Face:
1897 extract_data_face_bmesh<T>(faces, attr.offset, *vbos[i]);
1898 break;
1899 case bke::AttrDomain::Corner:
1900 extract_data_corner_bmesh<T>(faces, attr.offset, *vbos[i]);
1901 break;
1902 default:
1903 BLI_assert_unreachable();
1904 }
1905 }
1906 });
1907 },
1909}
1910
1912 const Span<bool> hide_poly,
1913 const Span<int> face_indices)
1914{
1915 int corners_count = 0;
1916 for (const int face : face_indices) {
1917 if (!hide_poly.is_empty() && hide_poly[face]) {
1918 continue;
1919 }
1920 corners_count += faces[face].size();
1921 }
1922
1923 GPUIndexBufBuilder builder;
1924 GPU_indexbuf_init(&builder, GPU_PRIM_LINES, corners_count, INT_MAX);
1926
1927 int node_corner_offset = 0;
1928 int line_index = 0;
1929 for (const int face_index : face_indices) {
1930 const int face_size = faces[face_index].size();
1931 if (!hide_poly.is_empty() && hide_poly[face_index]) {
1932 node_corner_offset += face_size;
1933 continue;
1934 }
1935 for (const int i : IndexRange(face_size)) {
1936 const int next = (i == face_size - 1) ? 0 : i + 1;
1937 data[line_index] = uint2(i, next) + node_corner_offset;
1938 line_index++;
1939 }
1940
1941 node_corner_offset += face_size;
1942 }
1943
1944 return gpu::IndexBufPtr(GPU_indexbuf_build_ex(&builder, 0, node_corner_offset, false));
1945}
1946
1948 const int visible_faces_num)
1949{
1950 GPUIndexBufBuilder builder;
1951 GPU_indexbuf_init(&builder, GPU_PRIM_LINES, visible_faces_num * 3, INT_MAX);
1952
1954
1955 int line_index = 0;
1956 int vert_index = 0;
1957
1958 for (const BMFace *face : faces) {
1959 if (BM_elem_flag_test(face, BM_ELEM_HIDDEN)) {
1960 continue;
1961 }
1962
1963 data[line_index] = uint2(vert_index, vert_index + 1);
1964 line_index++;
1965 data[line_index] = uint2(vert_index + 1, vert_index + 2);
1966 line_index++;
1967 data[line_index] = uint2(vert_index + 2, vert_index);
1968 line_index++;
1969
1970 vert_index += 3;
1971 }
1972
1973 return gpu::IndexBufPtr(GPU_indexbuf_build_ex(&builder, 0, visible_faces_num * 3, false));
1974}
1975
1976static int create_tri_index_grids(const Span<int> grid_indices,
1977 const BitGroupVector<> &grid_hidden,
1978 const int gridsize,
1979 const int skip,
1980 const int totgrid,
1982{
1983 int tri_index = 0;
1984 int offset = 0;
1985 const int grid_vert_len = gridsize * gridsize;
1986 for (int i = 0; i < totgrid; i++, offset += grid_vert_len) {
1987 uint v0, v1, v2, v3;
1988
1989 const BoundedBitSpan gh = grid_hidden.is_empty() ? BoundedBitSpan() :
1990 grid_hidden[grid_indices[i]];
1991
1992 for (int y = 0; y < gridsize - skip; y += skip) {
1993 for (int x = 0; x < gridsize - skip; x += skip) {
1994 /* Skip hidden grid face */
1995 if (!gh.is_empty() && BKE_paint_is_grid_face_hidden(gh, gridsize, x, y)) {
1996 continue;
1997 }
1998 /* Indices in a Clockwise QUAD disposition. */
1999 v0 = offset + CCG_grid_xy_to_index(gridsize, x, y);
2000 v1 = offset + CCG_grid_xy_to_index(gridsize, x + skip, y);
2001 v2 = offset + CCG_grid_xy_to_index(gridsize, x + skip, y + skip);
2002 v3 = offset + CCG_grid_xy_to_index(gridsize, x, y + skip);
2003
2004 data[tri_index] = uint3(v0, v2, v1);
2005 tri_index++;
2006 data[tri_index] = uint3(v0, v3, v2);
2007 tri_index++;
2008 }
2009 }
2010 }
2011
2012 return tri_index;
2013}
2014
2015static int create_tri_index_grids_flat_layout(const Span<int> grid_indices,
2016 const BitGroupVector<> &grid_hidden,
2017 const int gridsize,
2018 const int skip,
2019 const int totgrid,
2021{
2022 int tri_index = 0;
2023 int offset = 0;
2024 const int grid_vert_len = square_uint(gridsize - 1) * 4;
2025 for (int i = 0; i < totgrid; i++, offset += grid_vert_len) {
2026 const BoundedBitSpan gh = grid_hidden.is_empty() ? BoundedBitSpan() :
2027 grid_hidden[grid_indices[i]];
2028
2029 uint v0, v1, v2, v3;
2030 for (int y = 0; y < gridsize - skip; y += skip) {
2031 for (int x = 0; x < gridsize - skip; x += skip) {
2032 /* Skip hidden grid face */
2033 if (!gh.is_empty() && BKE_paint_is_grid_face_hidden(gh, gridsize, x, y)) {
2034 continue;
2035 }
2036
2037 v0 = (y * (gridsize - 1) + x) * 4;
2038
2039 if (skip > 1) {
2040 v1 = (y * (gridsize - 1) + x + skip - 1) * 4;
2041 v2 = ((y + skip - 1) * (gridsize - 1) + x + skip - 1) * 4;
2042 v3 = ((y + skip - 1) * (gridsize - 1) + x) * 4;
2043 }
2044 else {
2045 v1 = v2 = v3 = v0;
2046 }
2047
2048 /* VBO data are in a Clockwise QUAD disposition. Note
2049 * that vertices might be in different quads if we're
2050 * building a coarse index buffer.
2051 */
2052 v0 += offset;
2053 v1 += offset + 1;
2054 v2 += offset + 2;
2055 v3 += offset + 3;
2056
2057 data[tri_index] = uint3(v0, v2, v1);
2058 tri_index++;
2059 data[tri_index] = uint3(v0, v3, v2);
2060 tri_index++;
2061 }
2062 }
2063 }
2064 return tri_index;
2065}
2066
2067static void create_lines_index_grids(const Span<int> grid_indices,
2068 int display_gridsize,
2069 const BitGroupVector<> &grid_hidden,
2070 const int gridsize,
2071 const int skip,
2072 const int totgrid,
2074{
2075 int line_index = 0;
2076 int offset = 0;
2077 const int grid_vert_len = gridsize * gridsize;
2078 for (int i = 0; i < totgrid; i++, offset += grid_vert_len) {
2079 uint v0, v1, v2, v3;
2080 bool grid_visible = false;
2081
2082 const BoundedBitSpan gh = grid_hidden.is_empty() ? BoundedBitSpan() :
2083 grid_hidden[grid_indices[i]];
2084
2085 for (int y = 0; y < gridsize - skip; y += skip) {
2086 for (int x = 0; x < gridsize - skip; x += skip) {
2087 /* Skip hidden grid face */
2088 if (!gh.is_empty() && BKE_paint_is_grid_face_hidden(gh, gridsize, x, y)) {
2089 continue;
2090 }
2091 /* Indices in a Clockwise QUAD disposition. */
2092 v0 = offset + CCG_grid_xy_to_index(gridsize, x, y);
2093 v1 = offset + CCG_grid_xy_to_index(gridsize, x + skip, y);
2094 v2 = offset + CCG_grid_xy_to_index(gridsize, x + skip, y + skip);
2095 v3 = offset + CCG_grid_xy_to_index(gridsize, x, y + skip);
2096
2097 data[line_index] = uint2(v0, v1);
2098 line_index++;
2099 data[line_index] = uint2(v0, v3);
2100 line_index++;
2101
2102 if (y / skip + 2 == display_gridsize) {
2103 data[line_index] = uint2(v2, v3);
2104 line_index++;
2105 }
2106 grid_visible = true;
2107 }
2108
2109 if (grid_visible) {
2110 data[line_index] = uint2(v1, v2);
2111 line_index++;
2112 }
2113 }
2114 }
2115}
2116
2118 int display_gridsize,
2119 const BitGroupVector<> &grid_hidden,
2120 const int gridsize,
2121 const int skip,
2122 const int totgrid,
2124{
2125 int line_index = 0;
2126 int offset = 0;
2127 const int grid_vert_len = square_uint(gridsize - 1) * 4;
2128 for (int i = 0; i < totgrid; i++, offset += grid_vert_len) {
2129 bool grid_visible = false;
2130 const BoundedBitSpan gh = grid_hidden.is_empty() ? BoundedBitSpan() :
2131 grid_hidden[grid_indices[i]];
2132
2133 uint v0, v1, v2, v3;
2134 for (int y = 0; y < gridsize - skip; y += skip) {
2135 for (int x = 0; x < gridsize - skip; x += skip) {
2136 /* Skip hidden grid face */
2137 if (!gh.is_empty() && BKE_paint_is_grid_face_hidden(gh, gridsize, x, y)) {
2138 continue;
2139 }
2140
2141 v0 = (y * (gridsize - 1) + x) * 4;
2142
2143 if (skip > 1) {
2144 v1 = (y * (gridsize - 1) + x + skip - 1) * 4;
2145 v2 = ((y + skip - 1) * (gridsize - 1) + x + skip - 1) * 4;
2146 v3 = ((y + skip - 1) * (gridsize - 1) + x) * 4;
2147 }
2148 else {
2149 v1 = v2 = v3 = v0;
2150 }
2151
2152 /* VBO data are in a Clockwise QUAD disposition. Note
2153 * that vertices might be in different quads if we're
2154 * building a coarse index buffer.
2155 */
2156 v0 += offset;
2157 v1 += offset + 1;
2158 v2 += offset + 2;
2159 v3 += offset + 3;
2160
2161 data[line_index] = uint2(v0, v1);
2162 line_index++;
2163 data[line_index] = uint2(v0, v3);
2164 line_index++;
2165
2166 if (y / skip + 2 == display_gridsize) {
2167 data[line_index] = uint2(v2, v3);
2168 line_index++;
2169 }
2170 grid_visible = true;
2171 }
2172
2173 if (grid_visible) {
2174 data[line_index] = uint2(v1, v2);
2175 line_index++;
2176 }
2177 }
2178 }
2179}
2180
2181static Array<int> calc_material_indices(const Object &object, const OrigMeshData &orig_mesh_data)
2182{
2183 const SculptSession &ss = *object.runtime->sculpt_session;
2184 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
2185 switch (pbvh.type()) {
2186 case bke::pbvh::Type::Mesh: {
2189 const bke::AttributeAccessor attributes = mesh.attributes();
2190 const VArray material_indices = *attributes.lookup<int>("material_index",
2192 if (!material_indices) {
2193 return {};
2194 }
2195 Array<int> node_materials(nodes.size());
2196 threading::parallel_for(nodes.index_range(), 64, [&](const IndexRange range) {
2197 for (const int i : range) {
2198 const Span<int> face_indices = nodes[i].faces();
2199 if (face_indices.is_empty()) {
2200 continue;
2201 }
2202 node_materials[i] = material_indices[face_indices.first()];
2203 }
2204 });
2205 return node_materials;
2206 }
2209 /* Use original mesh data because evaluated mesh is empty. */
2210 const bke::AttributeAccessor attributes = orig_mesh_data.attributes;
2211 const VArray material_indices = *attributes.lookup<int>("material_index",
2213 if (!material_indices) {
2214 return {};
2215 }
2216 Array<int> node_materials(nodes.size());
2217 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
2218 const Span<int> grid_faces = subdiv_ccg.grid_to_face_map;
2219 threading::parallel_for(nodes.index_range(), 64, [&](const IndexRange range) {
2220 for (const int i : range) {
2221 const Span<int> grids = nodes[i].grids();
2222 if (grids.is_empty()) {
2223 continue;
2224 }
2225 node_materials[i] = material_indices[grid_faces[grids.first()]];
2226 }
2227 });
2228 return node_materials;
2229 }
2231 return {};
2232 }
2234 return {};
2235}
2236
2237static BitVector<> calc_use_flat_layout(const Object &object, const OrigMeshData &orig_mesh_data)
2238{
2240 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
2241 switch (pbvh.type()) {
2243 /* NOTE: Theoretically it would be possible to used vertex indexed buffers if there are no
2244 * face corner attributes, sharp faces, or face sets. */
2245 return {};
2248 const bke::AttributeAccessor attributes = orig_mesh_data.attributes;
2249 const VArraySpan sharp_faces = *attributes.lookup<bool>("sharp_face", bke::AttrDomain::Face);
2250 if (sharp_faces.is_empty()) {
2251 return BitVector<>(nodes.size(), false);
2252 }
2253
2254 const SubdivCCG &subdiv_ccg = *object.runtime->sculpt_session->subdiv_ccg;
2255 const Span<int> grid_to_face_map = subdiv_ccg.grid_to_face_map;
2256
2257 /* Use boolean array instead of #BitVector for parallelized writing. */
2258 Array<bool> use_flat_layout(nodes.size());
2259 threading::parallel_for(nodes.index_range(), 4, [&](const IndexRange range) {
2260 for (const int i : range) {
2261 const Span<int> grids = nodes[i].grids();
2262 if (grids.is_empty()) {
2263 continue;
2264 }
2265 use_flat_layout[i] = std::any_of(grids.begin(), grids.end(), [&](const int grid) {
2266 return sharp_faces[grid_to_face_map[grid]];
2267 });
2268 }
2269 });
2270 return BitVector<>(use_flat_layout);
2271 }
2273 return {};
2274 }
2276 return {};
2277}
2278
2280 const Span<int3> corner_tris,
2281 const Span<bool> hide_poly,
2283{
2284 const Span<int> face_indices = node.faces();
2285 int tris_num = 0;
2286 if (hide_poly.is_empty()) {
2287 tris_num = poly_to_tri_count(face_indices.size(), node.corners_num());
2288 }
2289 else {
2290 for (const int face : face_indices) {
2291 if (hide_poly[face]) {
2292 continue;
2293 }
2294 tris_num += bke::mesh::face_triangles_num(faces[face].size());
2295 }
2296 }
2297
2298 GPUIndexBufBuilder builder;
2299 GPU_indexbuf_init(&builder, GPU_PRIM_TRIS, tris_num, INT_MAX);
2301
2302 int tri_index = 0;
2303 int node_corner_offset = 0;
2304 for (const int face_index : face_indices) {
2305 const IndexRange face = faces[face_index];
2306 if (!hide_poly.is_empty() && hide_poly[face_index]) {
2307 node_corner_offset += face.size();
2308 continue;
2309 }
2310 for (const int3 &tri : corner_tris.slice(bke::mesh::face_triangles_range(faces, face_index))) {
2311 for (int i : IndexRange(3)) {
2312 const int corner = tri[i];
2313 const int index_in_face = corner - face.first();
2314 data[tri_index][i] = node_corner_offset + index_in_face;
2315 }
2316 tri_index++;
2317 }
2318 node_corner_offset += face.size();
2319 }
2320
2321 return gpu::IndexBufPtr(GPU_indexbuf_build_ex(&builder, 0, node_corner_offset, false));
2322}
2323
2325 const BitGroupVector<> &grid_hidden,
2326 const bool do_coarse,
2327 const Span<int> grid_indices,
2328 const bool use_flat_layout)
2329{
2330 int gridsize = key.grid_size;
2331 int display_gridsize = gridsize;
2332 int totgrid = grid_indices.size();
2333 int skip = 1;
2334
2335 const int display_level = do_coarse ? 0 : key.level;
2336
2337 if (display_level < key.level) {
2338 display_gridsize = (1 << display_level) + 1;
2339 skip = 1 << (key.level - display_level - 1);
2340 }
2341
2342 uint visible_quad_len = bke::pbvh::count_grid_quads(
2343 grid_hidden, grid_indices, key.grid_size, display_gridsize);
2344
2345 GPUIndexBufBuilder builder;
2346 GPU_indexbuf_init(&builder, GPU_PRIM_TRIS, 2 * visible_quad_len, INT_MAX);
2347
2349
2350 int tri_count;
2351 if (use_flat_layout) {
2353 grid_indices, grid_hidden, gridsize, skip, totgrid, data);
2354 }
2355 else {
2356 tri_count = create_tri_index_grids(grid_indices, grid_hidden, gridsize, skip, totgrid, data);
2357 }
2358
2359 builder.index_len = tri_count * 3;
2360 builder.index_min = 0;
2361 builder.index_max = 6 * visible_quad_len;
2362 builder.uses_restart_indices = false;
2364 GPU_indexbuf_build_in_place(&builder, result.get());
2365 return result;
2366}
2367
2369 const BitGroupVector<> &grid_hidden,
2370 const bool do_coarse,
2371 const Span<int> grid_indices,
2372 const bool use_flat_layout)
2373{
2374 int gridsize = key.grid_size;
2375 int display_gridsize = gridsize;
2376 int totgrid = grid_indices.size();
2377 int skip = 1;
2378
2379 const int display_level = do_coarse ? 0 : key.level;
2380
2381 if (display_level < key.level) {
2382 display_gridsize = (1 << display_level) + 1;
2383 skip = 1 << (key.level - display_level - 1);
2384 }
2385
2386 GPUIndexBufBuilder builder;
2388 &builder, GPU_PRIM_LINES, 2 * totgrid * display_gridsize * (display_gridsize - 1), INT_MAX);
2389
2391 /* The buffer might contain hidden elements which are not initialized but still accounted. We
2392 * don't count them to skip from allocation, so must fill that gaps by 0 to hide redundant edges.
2393 */
2394 data.fill(uint2(0));
2395
2396 if (use_flat_layout) {
2398 grid_indices, display_gridsize, grid_hidden, gridsize, skip, totgrid, data);
2399 }
2400 else {
2402 grid_indices, display_gridsize, grid_hidden, gridsize, skip, totgrid, data);
2403 }
2404
2406 &builder, 0, 2 * totgrid * display_gridsize * (display_gridsize - 1), false));
2407}
2408
2409Span<gpu::IndexBufPtr> DrawCacheImpl::ensure_lines_indices(const Object &object,
2410 const OrigMeshData &orig_mesh_data,
2411 const IndexMask &node_mask,
2412 const bool coarse)
2413{
2414 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
2415 Vector<gpu::IndexBufPtr> &ibos = coarse ? lines_ibos_coarse_ : lines_ibos_;
2416 ibos.resize(pbvh.nodes_num());
2417
2418 IndexMaskMemory memory;
2419 const IndexMask nodes_to_calculate = IndexMask::from_predicate(
2420 node_mask, memory, [&](const int i) { return !ibos[i]; });
2421
2422 switch (pbvh.type()) {
2423 case bke::pbvh::Type::Mesh: {
2426 const OffsetIndices<int> faces = mesh.faces();
2427 const bke::AttributeAccessor attributes = orig_mesh_data.attributes;
2428 const VArraySpan hide_poly = *attributes.lookup<bool>(".hide_poly", bke::AttrDomain::Face);
2429 nodes_to_calculate.foreach_index(
2430 [&](const int i) {
2431 ibos[i] = create_lines_index_faces(faces, hide_poly, nodes[i].faces());
2432 },
2434 break;
2435 }
2437 const Span<bke::pbvh::GridsNode> nodes = pbvh.nodes<bke::pbvh::GridsNode>();
2438 nodes_to_calculate.foreach_index(
2439 [&](const int i) {
2440 const SubdivCCG &subdiv_ccg = *object.runtime->sculpt_session->subdiv_ccg;
2441 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
2442 ibos[i] = create_lines_index_grids(
2443 key, subdiv_ccg.grid_hidden, coarse, nodes[i].grids(), use_flat_layout_[i]);
2444 },
2446 break;
2447 }
2449 const Span<bke::pbvh::BMeshNode> nodes = pbvh.nodes<bke::pbvh::BMeshNode>();
2450 nodes_to_calculate.foreach_index(
2451 [&](const int i) {
2452 const Set<BMFace *, 0> &faces = BKE_pbvh_bmesh_node_faces(
2453 &const_cast<bke::pbvh::BMeshNode &>(nodes[i]));
2454 const int visible_faces_num = count_visible_tris_bmesh(faces);
2455 ibos[i] = create_lines_index_bmesh(faces, visible_faces_num);
2456 },
2458 break;
2459 }
2460 }
2461
2462 return ibos;
2463}
2464
2465BitSpan DrawCacheImpl::ensure_use_flat_layout(const Object &object,
2466 const OrigMeshData &orig_mesh_data)
2467{
2469 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
2470 if (use_flat_layout_.size() != pbvh.nodes_num()) {
2471 use_flat_layout_ = calc_use_flat_layout(object, orig_mesh_data);
2472 }
2473 return use_flat_layout_;
2474}
2475
2477 const IndexMask &node_mask)
2478{
2480 node_mask.foreach_index([&](const int i) { GPU_vertbuf_use(vbos[i].get()); });
2481}
2482
2483Span<gpu::VertBufPtr> DrawCacheImpl::ensure_attribute_data(const Object &object,
2484 const OrigMeshData &orig_mesh_data,
2485 const AttributeRequest &attr,
2486 const IndexMask &node_mask)
2487{
2489 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
2490 AttributeData &data = attribute_vbos_.lookup_or_add_default(attr);
2491 Vector<gpu::VertBufPtr> &vbos = data.vbos;
2492 vbos.resize(pbvh.nodes_num());
2493
2494 /* The nodes we recompute here are a combination of:
2495 * 1. null VBOs, which correspond to nodes that either haven't been drawn before, or have been
2496 * cleared completely by #free_nodes_with_changed_topology.
2497 * 2. Nodes that have been tagged dirty as their values are changed.
2498 * We also only process a subset of the nodes referenced by the caller, for example to only
2499 * recompute visible nodes. */
2500 IndexMaskMemory memory;
2501 const IndexMask empty_mask = IndexMask::from_predicate(
2502 node_mask, memory, [&](const int i) { return !vbos[i]; });
2503 const IndexMask dirty_mask = IndexMask::from_bits(
2504 node_mask.slice_content(data.dirty_nodes.index_range()), data.dirty_nodes, memory);
2505 const IndexMask mask = IndexMask::from_union(empty_mask, dirty_mask, memory);
2506
2507 switch (pbvh.type()) {
2508 case bke::pbvh::Type::Mesh: {
2509 if (const CustomRequest *request_type = std::get_if<CustomRequest>(&attr)) {
2510 switch (*request_type) {
2511 case CustomRequest::Position:
2512 update_positions_mesh(object, mask, vbos);
2513 break;
2514 case CustomRequest::Normal:
2515 update_normals_mesh(object, mask, vbos);
2516 break;
2517 case CustomRequest::Mask:
2518 update_masks_mesh(object, orig_mesh_data, mask, vbos);
2519 break;
2520 case CustomRequest::FaceSet:
2521 update_face_sets_mesh(object, orig_mesh_data, mask, vbos);
2522 break;
2523 }
2524 }
2525 else {
2527 object, orig_mesh_data, mask, std::get<GenericRequest>(attr), vbos);
2528 }
2529 break;
2530 }
2532 if (const CustomRequest *request_type = std::get_if<CustomRequest>(&attr)) {
2533 switch (*request_type) {
2534 case CustomRequest::Position:
2535 fill_positions_grids(object, use_flat_layout_, mask, vbos);
2536 break;
2537 case CustomRequest::Normal:
2538 fill_normals_grids(object, orig_mesh_data, use_flat_layout_, mask, vbos);
2539 break;
2540 case CustomRequest::Mask:
2541 fill_masks_grids(object, use_flat_layout_, mask, vbos);
2542 break;
2543 case CustomRequest::FaceSet:
2544 fill_face_sets_grids(object, orig_mesh_data, use_flat_layout_, mask, vbos);
2545 break;
2546 }
2547 }
2548 else {
2550 object, orig_mesh_data, use_flat_layout_, mask, std::get<GenericRequest>(attr), vbos);
2551 }
2552 break;
2553 }
2555 if (const CustomRequest *request_type = std::get_if<CustomRequest>(&attr)) {
2556 switch (*request_type) {
2557 case CustomRequest::Position:
2558 update_positions_bmesh(object, mask, vbos);
2559 break;
2560 case CustomRequest::Normal:
2561 update_normals_bmesh(object, mask, vbos);
2562 break;
2563 case CustomRequest::Mask:
2564 update_masks_bmesh(object, mask, vbos);
2565 break;
2566 case CustomRequest::FaceSet:
2567 update_face_sets_bmesh(object, orig_mesh_data, mask, vbos);
2568 break;
2569 }
2570 }
2571 else {
2573 object, orig_mesh_data, mask, std::get<GenericRequest>(attr), vbos);
2574 }
2575 break;
2576 }
2577 }
2578
2579 /* TODO: It would be good to deallocate the bit vector if all of the bits have been reset to
2580 * avoid unnecessary processing in subsequent redraws. */
2581 dirty_mask.foreach_index_optimized<int>([&](const int i) { data.dirty_nodes[i].reset(); });
2582
2583 flush_vbo_data(vbos, mask);
2584
2585 return vbos;
2586}
2587
2588Span<gpu::IndexBufPtr> DrawCacheImpl::ensure_tri_indices(const Object &object,
2589 const OrigMeshData &orig_mesh_data,
2590 const IndexMask &node_mask,
2591 const bool coarse)
2592{
2594 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
2595 switch (pbvh.type()) {
2596 case bke::pbvh::Type::Mesh: {
2597 const Span<bke::pbvh::MeshNode> nodes = pbvh.nodes<bke::pbvh::MeshNode>();
2598
2599 Vector<gpu::IndexBufPtr> &ibos = tris_ibos_;
2600 ibos.resize(nodes.size());
2601
2602 /* Whenever a node's visible triangle count has changed the index buffers are freed, so we
2603 * only recalculate null IBOs here. A new mask is recalculated for more even task
2604 * distribution between threads. */
2605 IndexMaskMemory memory;
2606 const IndexMask nodes_to_calculate = IndexMask::from_predicate(
2607 node_mask, memory, [&](const int i) { return !ibos[i]; });
2608
2609 const Mesh &mesh = DRW_object_get_data_for_drawing<Mesh>(object);
2610 const OffsetIndices<int> faces = mesh.faces();
2611 const Span<int3> corner_tris = mesh.corner_tris();
2612 const bke::AttributeAccessor attributes = orig_mesh_data.attributes;
2613 const VArraySpan hide_poly = *attributes.lookup<bool>(".hide_poly", bke::AttrDomain::Face);
2614 nodes_to_calculate.foreach_index(
2615 [&](const int i) {
2616 ibos[i] = create_tri_index_mesh(faces, corner_tris, hide_poly, nodes[i]);
2617 },
2619 return ibos;
2620 }
2622 /* Unlike the other geometry types, multires grids use indexed vertex buffers because when
2623 * there are no flat faces, vertices can be shared between neighboring quads. This results in
2624 * a 4x decrease in the amount of data uploaded. Theoretically it also means freeing VBOs
2625 * because of visibility changes is unnecessary.
2626 *
2627 * TODO: With the "flat layout" and no hidden faces, the index buffers are unnecessary, we
2628 * should avoid creating them in that case. */
2629 const Span<bke::pbvh::GridsNode> nodes = pbvh.nodes<bke::pbvh::GridsNode>();
2630
2631 Vector<gpu::IndexBufPtr> &ibos = coarse ? tris_ibos_coarse_ : tris_ibos_;
2632 ibos.resize(nodes.size());
2633
2634 /* Whenever a node's visible triangle count has changed the index buffers are freed, so we
2635 * only recalculate null IBOs here. A new mask is recalculated for more even task
2636 * distribution between threads. */
2637 IndexMaskMemory memory;
2638 const IndexMask nodes_to_calculate = IndexMask::from_predicate(
2639 node_mask, memory, [&](const int i) { return !ibos[i]; });
2640
2641 const SubdivCCG &subdiv_ccg = *object.runtime->sculpt_session->subdiv_ccg;
2642 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
2643
2644 nodes_to_calculate.foreach_index(
2645 [&](const int i) {
2646 ibos[i] = create_tri_index_grids(
2647 key, subdiv_ccg.grid_hidden, coarse, nodes[i].grids(), use_flat_layout_[i]);
2648 },
2650 return ibos;
2651 }
2653 return {};
2654 }
2656 return {};
2657}
2658
2660 const ViewportRequest &request,
2661 const IndexMask &nodes_to_update)
2662{
2664 const Object &object_orig = *DEG_get_original(&object);
2665 const OrigMeshData orig_mesh_data{*id_cast<const Mesh *>(object_orig.data)};
2666 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
2667
2668 this->ensure_use_flat_layout(object, orig_mesh_data);
2669 this->free_nodes_with_changed_topology(pbvh);
2670
2671 const Span<gpu::IndexBufPtr> ibos = this->ensure_tri_indices(
2672 object, orig_mesh_data, nodes_to_update, request.use_coarse_grids);
2673
2674 for (const AttributeRequest &attr : request.attributes) {
2675 this->ensure_attribute_data(object, orig_mesh_data, attr, nodes_to_update);
2676 }
2677
2678 /* Collect VBO spans in a different loop because #ensure_attribute_data invalidates the allocated
2679 * arrays when its map is changed. */
2681 for (const AttributeRequest &attr : request.attributes) {
2682 if (const AttributeData *attr_data = attribute_vbos_.lookup_ptr(attr)) {
2683 attr_vbos.append(attr_data->vbos);
2684 }
2685 }
2686
2687 /* Except for the first iteration of the draw loop, we only need to rebuild batches for nodes
2688 * with changed topology (visible triangle count). */
2689 Vector<gpu::Batch *> &batches = tris_batches_.lookup_or_add_default(request);
2690 batches.resize(pbvh.nodes_num(), nullptr);
2691 nodes_to_update.foreach_index(
2692 [&](const int i) {
2693 if (!batches[i]) {
2694 batches[i] = GPU_batch_create(
2695 GPU_PRIM_TRIS, nullptr, ibos.is_empty() ? nullptr : ibos[i].get());
2696 for (const Span<gpu::VertBufPtr> vbos : attr_vbos) {
2697 GPU_batch_vertbuf_add(batches[i], vbos[i].get(), false);
2698 }
2699 }
2700 },
2702
2703 return batches;
2704}
2705
2707 const ViewportRequest &request,
2708 const IndexMask &nodes_to_update)
2709{
2711 const Object &object_orig = *DEG_get_original(&object);
2712 const OrigMeshData orig_mesh_data(*id_cast<const Mesh *>(object_orig.data));
2713 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
2714
2715 this->ensure_use_flat_layout(object, orig_mesh_data);
2716 this->free_nodes_with_changed_topology(pbvh);
2717
2718 const Span<gpu::VertBufPtr> position = this->ensure_attribute_data(
2719 object, orig_mesh_data, CustomRequest::Position, nodes_to_update);
2720 const Span<gpu::IndexBufPtr> lines = this->ensure_lines_indices(
2721 object, orig_mesh_data, nodes_to_update, request.use_coarse_grids);
2722
2723 /* Except for the first iteration of the draw loop, we only need to rebuild batches for nodes
2724 * with changed topology (visible triangle count). */
2725 Vector<gpu::Batch *> &batches = request.use_coarse_grids ? lines_batches_coarse_ :
2726 lines_batches_;
2727 batches.resize(pbvh.nodes_num(), nullptr);
2728 nodes_to_update.foreach_index(
2729 [&](const int i) {
2730 if (!batches[i]) {
2731 batches[i] = GPU_batch_create(GPU_PRIM_LINES, nullptr, lines[i].get());
2732 GPU_batch_vertbuf_add(batches[i], position[i].get(), false);
2733 }
2734 },
2736
2737 return batches;
2738}
2739
2741{
2742 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
2743 const Object &object_orig = *DEG_get_original(&object);
2744 const OrigMeshData orig_mesh_data(*id_cast<const Mesh *>(object_orig.data));
2745 if (orig_mesh_data.attributes.contains("material_index") &&
2746 material_indices_.size() != pbvh.nodes_num())
2747 {
2748 material_indices_ = calc_material_indices(object, orig_mesh_data);
2749 }
2750 return material_indices_;
2751}
2752
2753} // namespace draw::pbvh
2754} // namespace blender
CustomData interface, see also DNA_customdata_types.h.
A BVH for high poly meshes.
Object is a sort of wrapper for general info.
#define GPU_batch_create(primitive_type, vertex_buf, index_buf)
Definition GPU_batch.hh:143
#define GPU_BATCH_DISCARD_SAFE(batch)
Definition GPU_batch.hh:199
#define PRF_scope(category)
#define BLI_assert_unreachable()
#define BLI_NOINLINE
#define POINTER_OFFSET(v, ofs)
#define BLI_assert_msg(a, msg)
#define BM_elem_flag_test(ele, hflag)
constexpr int64_t size() const
Definition BLI_span.hh:256
BitVector(Allocator allocator={}) noexcept
const CPPType & type() const
static IndexMask from_predicate(const IndexMask &universe, LinearAllocator<> &memory, Fn &&predicate, exec_mode::Mode mode=exec_mode::parallel)
static IndexMask from_union(const IndexMask &mask_a, const IndexMask &mask_b, LinearAllocator<> &memory)
static IndexMask from_bits(BitSpan bits, LinearAllocator<> &memory)
static IndexMask from_intersection(const IndexMask &mask_a, const IndexMask &mask_b, LinearAllocator<> &memory)
constexpr int64_t first() const
constexpr int64_t size() const
constexpr IndexRange()=default
ValueIterator values() const &
Definition BLI_map.hh:884
constexpr T * data() const
Definition BLI_span.hh:545
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
Iterator begin() const
Definition BLI_set.hh:499
Iterator end() const
Definition BLI_set.hh:509
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr const T * data() const
Definition BLI_span.hh:219
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr bool is_empty() const
Definition BLI_span.hh:264
void add_multiple(Span< Key > keys)
void append(const T &value)
void resize(const int64_t new_size)
void resize(const int64_t new_size_in_bits, const bool value=false)
bool contains(StringRef name) const
GAttributeReader lookup(const StringRef name) const
constexpr bool is_empty() const
void tag_visibility_changed(const IndexMask &node_mask) override
Definition draw_pbvh.cc:216
void tag_positions_changed(const IndexMask &node_mask) override
Definition draw_pbvh.cc:206
Span< int > ensure_material_indices(const Object &object) override
void tag_attribute_changed(const IndexMask &node_mask, StringRef attribute_name) override
Definition draw_pbvh.cc:243
Span< gpu::Batch * > ensure_tris_batches(const Object &object, const ViewportRequest &request, const IndexMask &nodes_to_update) override
void tag_masks_changed(const IndexMask &node_mask) override
Definition draw_pbvh.cc:236
void tag_topology_changed(const IndexMask &node_mask) override
Definition draw_pbvh.cc:222
void tag_face_sets_changed(const IndexMask &node_mask) override
Definition draw_pbvh.cc:229
Span< gpu::Batch * > ensure_lines_batches(const Object &object, const ViewportRequest &request, const IndexMask &nodes_to_update) override
MutableSpan< T > data()
IndexMask slice_content(IndexRange range) const
void set_bits(MutableBitSpan r_bits, int64_t offset=0) const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
#define INT_MAX
#define position
#define attribute(slot)
#define T
void to_static_type(const CPPType &cpp_type, Fn &&fn)
IndexRange grid_range(const int grid_area, const int grid)
int face_triangles_num(const int face_size)
Definition BKE_mesh.hh:350
IndexRange face_triangles_range(OffsetIndices< int > faces, int face_i)
Definition BKE_mesh.hh:359
pbvh::Tree * pbvh_get(Object &object)
Span< float3 > vert_normals_eval_from_eval(const Object &object_eval)
Definition pbvh.cc:958
int count_grid_quads(const BitGroupVector<> &grid_hidden, Span< int > grid_indices, int gridsize, int display_gridsize)
Definition pbvh.cc:1532
Span< float3 > vert_positions_eval_from_eval(const Object &object_eval)
Definition pbvh.cc:939
Span< float3 > face_normals_eval_from_eval(const Object &object_eval)
Definition pbvh.cc:965
BLI_INLINE float4 quad_weights_from_uv(float u, float v)
BLI_INLINE void rotate_grid_to_quad(int corner, float grid_u, float grid_v, float *r_quad_u, float *r_quad_v)
void eval_face_varying(Subdiv *subdiv, int face_varying_channel, int ptex_face_index, float u, float v, float2 &r_face_varying)
Span< int > face_ptex_offset_get(Subdiv *subdiv)
Definition subdiv.cc:216
std::optional< eCustomDataType > attr_type_to_custom_data_type(AttrType attr_type)
AttrType cpp_type_to_attribute_type(const CPPType &type)
void extract_data_vert_mesh(const OffsetIndices< int > faces, const Span< int > corner_verts, const Span< T > attribute, const Span< int > face_indices, gpu::VertBuf &vbo)
Definition draw_pbvh.cc:346
static BLI_NOINLINE void flush_vbo_data(const Span< gpu::VertBufPtr > vbos, const IndexMask &node_mask)
static BLI_NOINLINE void update_face_sets_bmesh(const Object &object, const OrigMeshData &orig_mesh_data, const IndexMask &node_mask, const MutableSpan< gpu::VertBufPtr > vbos)
static BLI_NOINLINE void fill_masks_grids(const Object &object, const BitSpan use_flat_layout, const IndexMask &node_mask, const MutableSpan< gpu::VertBufPtr > vbos)
Definition draw_pbvh.cc:879
static BLI_NOINLINE void ensure_vbos_allocated_bmesh(const Object &object, const GPUVertFormat &format, const IndexMask &node_mask, const MutableSpan< gpu::VertBufPtr > vbos)
Definition draw_pbvh.cc:571
static BLI_NOINLINE void fill_uvs_grids(const Object &object, const OrigMeshData &orig_mesh_data, const BitSpan use_flat_layout, const IndexMask &node_mask, const StringRef name, const MutableSpan< gpu::VertBufPtr > vbos)
static BLI_NOINLINE void update_positions_bmesh(const Object &object, const IndexMask &node_mask, const MutableSpan< gpu::VertBufPtr > vbos)
void extract_data_corner_mesh(const OffsetIndices< int > faces, const Span< T > attribute, const Span< int > face_indices, gpu::VertBuf &vbo)
Definition draw_pbvh.cc:383
static void interpolate_corner_data_for_flat_layout(const SubdivCCG &subdiv_ccg, const Span< int > grids, const Span< T > src, gpu::VertBuf &vbo)
static void update_normals_mesh(const Object &object, const IndexMask &node_mask, MutableSpan< gpu::VertBufPtr > vbos)
Definition draw_pbvh.cc:612
static void calc_node_uvs_for_standard_layout(const SubdivCCG &subdiv_ccg, const int uv_map_index, Span< int > grids, MutableSpan< float2 > result)
static int count_visible_tris_bmesh(const Set< BMFace *, 0 > &faces)
Definition draw_pbvh.cc:479
const T & bmesh_cd_loop_get(const BMLoop &loop, const int offset)
Definition draw_pbvh.cc:406
static gpu::IndexBufPtr create_lines_index_faces(const OffsetIndices< int > faces, const Span< bool > hide_poly, const Span< int > face_indices)
static BitVector calc_use_flat_layout(const Object &object, const OrigMeshData &orig_mesh_data)
static BLI_NOINLINE void update_generic_attribute_grids(const Object &object, const OrigMeshData &orig_mesh_data, const BitSpan use_flat_layout, const IndexMask &node_mask, const StringRef name, const MutableSpan< gpu::VertBufPtr > vbos)
static const GPUVertFormat & normal_format()
Definition draw_pbvh.cc:293
static const GPUVertFormat & face_set_format()
Definition draw_pbvh.cc:307
DrawCache & ensure_draw_data(std::unique_ptr< bke::pbvh::DrawCache > &ptr)
Definition draw_pbvh.cc:254
static const GPUVertFormat & position_format()
Definition draw_pbvh.cc:286
static BLI_NOINLINE void update_generic_attribute_mesh(const Object &object, const OrigMeshData &orig_mesh_data, const IndexMask &node_mask, const StringRef name, MutableSpan< gpu::VertBufPtr > vbos)
Definition draw_pbvh.cc:725
static BLI_NOINLINE void update_masks_bmesh(const Object &object, const IndexMask &node_mask, const MutableSpan< gpu::VertBufPtr > vbos)
static BLI_NOINLINE void calc_generic_vert_attribute_grids(const Object &object, const OrigMeshData &orig_mesh_data, const BitSpan &use_flat_layout, const IndexMask &node_mask, const StringRef name, const MutableSpan< gpu::VertBufPtr > vbos)
static BLI_NOINLINE void fill_generic_face_attribute_grids(const Object &object, const OrigMeshData &orig_mesh_data, const BitSpan &use_flat_layout, const IndexMask &node_mask, const StringRef name, const MutableSpan< gpu::VertBufPtr > vbos)
static float4 quad_weights_from_grid(const int corner, const int x, const int y, const float grid_size_1_inv)
static void create_lines_index_grids_flat_layout(const Span< int > grid_indices, int display_gridsize, const BitGroupVector<> &grid_hidden, const int gridsize, const int skip, const int totgrid, MutableSpan< uint2 > data)
const T & bmesh_cd_face_get(const BMFace &face, const int offset)
Definition draw_pbvh.cc:411
static void calc_node_uvs(const SubdivCCG &subdiv_ccg, const int uv_map_index, const bke::pbvh::GridsNode &node, const bool use_flat_layout, MutableSpan< float2 > result)
static void create_lines_index_grids(const Span< int > grid_indices, int display_gridsize, const BitGroupVector<> &grid_hidden, const int gridsize, const int skip, const int totgrid, MutableSpan< uint2 > data)
static BLI_NOINLINE void update_generic_attribute_bmesh(const Object &object, const OrigMeshData &orig_mesh_data, const IndexMask &node_mask, const StringRef name, const MutableSpan< gpu::VertBufPtr > vbos)
static void calc_quad_uv(const SubdivCCG &subdiv_ccg, const int uv_map_index, const float grid_size_1_inv, const int ptex_face_index, const int grid_start, const int corner, const int x, const int y, const int offset, MutableSpan< float2 > result)
Definition draw_pbvh.cc:974
static void interpolate_vert_data_for_standard_layout(const SubdivCCG &subdiv_ccg, const Span< int > corner_verts, const Span< int > grids, const Span< T > src, gpu::VertBuf &vbo)
static int create_tri_index_grids_flat_layout(const Span< int > grid_indices, const BitGroupVector<> &grid_hidden, const int gridsize, const int skip, const int totgrid, MutableSpan< uint3 > data)
void extract_data_face_bmesh(const Set< BMFace *, 0 > &faces, const int cd_offset, gpu::VertBuf &vbo)
Definition draw_pbvh.cc:439
static Array< int > calc_material_indices(const Object &object, const OrigMeshData &orig_mesh_data)
short4 normal_float_to_short(const float3 &value)
Definition draw_pbvh.cc:338
static BLI_NOINLINE void free_vbos(const MutableSpan< gpu::VertBufPtr > vbos, const IndexMask &node_mask)
Definition draw_pbvh.cc:270
static BLI_NOINLINE void fill_face_sets_grids(const Object &object, const OrigMeshData &orig_mesh_data, const BitSpan use_flat_layout, const IndexMask &node_mask, const MutableSpan< gpu::VertBufPtr > vbos)
Definition draw_pbvh.cc:929
static gpu::IndexBufPtr create_tri_index_mesh(const OffsetIndices< int > faces, const Span< int3 > corner_tris, const Span< bool > hide_poly, const bke::pbvh::MeshNode &node)
static BLI_NOINLINE void free_batches(const MutableSpan< gpu::Batch * > batches, const IndexMask &node_mask)
Definition draw_pbvh.cc:278
void extract_data_vert_bmesh(const Set< BMFace *, 0 > &faces, const int cd_offset, gpu::VertBuf &vbo)
Definition draw_pbvh.cc:417
void extract_data_face_mesh(const OffsetIndices< int > faces, const Span< T > attribute, const Span< int > face_indices, gpu::VertBuf &vbo)
Definition draw_pbvh.cc:365
static BLI_NOINLINE void update_masks_mesh(const Object &object, const OrigMeshData &orig_mesh_data, const IndexMask &node_mask, MutableSpan< gpu::VertBufPtr > vbos)
Definition draw_pbvh.cc:648
static void calc_non_quad_uv(const SubdivCCG &subdiv_ccg, const int uv_map_index, const float grid_size_1_inv, const int ptex_face_index, const int grid_start, const int x, const int y, const int offset, MutableSpan< float2 > result)
Definition draw_pbvh.cc:994
static BLI_NOINLINE void calc_generic_corner_attribute_grids(const Object &object, const OrigMeshData &orig_mesh_data, const BitSpan &use_flat_layout, const IndexMask &node_mask, const StringRef name, const MutableSpan< gpu::VertBufPtr > vbos)
static GPUVertFormat attribute_format(const OrigMeshData &orig_mesh_data, const StringRef name, const bke::AttrType data_type)
Definition draw_pbvh.cc:314
static void update_positions_mesh(const Object &object, const IndexMask &node_mask, MutableSpan< gpu::VertBufPtr > vbos)
Definition draw_pbvh.cc:592
static BLI_NOINLINE void update_normals_bmesh(const Object &object, const IndexMask &node_mask, const MutableSpan< gpu::VertBufPtr > vbos)
static void fill_face_data_grids(const SubdivCCG &subdiv_ccg, const bool use_flat_layout, const Span< int > grids, const Span< T > src, gpu::VertBuf &vbo)
static void interpolate_vert_data_for_flat_layout(const SubdivCCG &subdiv_ccg, const Span< int > corner_verts, const Span< int > grids, const Span< T > src, gpu::VertBuf &vbo)
void extract_data_corner_bmesh(const Set< BMFace *, 0 > &faces, const int cd_offset, gpu::VertBuf &vbo)
Definition draw_pbvh.cc:456
static void interpolate_corner_data_for_standard_layout(const SubdivCCG &subdiv_ccg, const Span< int > grids, const Span< T > src, gpu::VertBuf &vbo)
static BLI_NOINLINE void ensure_vbos_allocated_mesh(const Object &object, const GPUVertFormat &format, const IndexMask &node_mask, const MutableSpan< gpu::VertBufPtr > vbos)
Definition draw_pbvh.cc:530
static BLI_NOINLINE void fill_normals_grids(const Object &object, const OrigMeshData &orig_mesh_data, const BitSpan use_flat_layout, const IndexMask &node_mask, const MutableSpan< gpu::VertBufPtr > vbos)
Definition draw_pbvh.cc:813
static BLI_NOINLINE void ensure_vbos_allocated_grids(const Object &object, const GPUVertFormat &format, const BitSpan use_flat_layout, const IndexMask &node_mask, const MutableSpan< gpu::VertBufPtr > vbos)
Definition draw_pbvh.cc:548
static BLI_NOINLINE void fill_positions_grids(const Object &object, const BitSpan use_flat_layout, const IndexMask &node_mask, const MutableSpan< gpu::VertBufPtr > vbos)
Definition draw_pbvh.cc:769
static gpu::IndexBufPtr create_lines_index_bmesh(const Set< BMFace *, 0 > &faces, const int visible_faces_num)
static BLI_NOINLINE void free_ibos(const MutableSpan< gpu::IndexBufPtr > ibos, const IndexMask &node_mask)
Definition draw_pbvh.cc:262
static int create_tri_index_grids(const Span< int > grid_indices, const BitGroupVector<> &grid_hidden, const int gridsize, const int skip, const int totgrid, MutableSpan< uint3 > data)
const T & bmesh_cd_vert_get(const BMVert &vert, const int offset)
Definition draw_pbvh.cc:401
static float4 quad_weights_from_irregular_corner(const int x, const int y, const float grid_size_1_inv)
static const GPUVertFormat & mask_format()
Definition draw_pbvh.cc:300
std::string GenericRequest
Definition DRW_pbvh.hh:40
static void calc_node_uvs_for_flat_layout(const SubdivCCG &subdiv_ccg, const int uv_map_index, Span< int > grids, MutableSpan< float2 > result)
static BLI_NOINLINE void update_face_sets_mesh(const Object &object, const OrigMeshData &orig_mesh_data, const IndexMask &node_mask, MutableSpan< gpu::VertBufPtr > vbos)
Definition draw_pbvh.cc:681
std::variant< CustomRequest, GenericRequest > AttributeRequest
Definition DRW_pbvh.hh:49
GPUVertFormat init_format_for_attribute(const bke::AttrType data_type, const StringRef vbo_name)
void DRW_cdlayer_attr_aliases_add(GPUVertFormat *format, const char *base_name, bke::AttrType data_type, StringRef layer_name, bool is_active_render, bool is_active_layer)
constexpr ParallelGrainSize grain_size(int grain_size)
std::unique_ptr< IndexBuf, IndexBufDeleter > IndexBufPtr
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
BMesh Iterators.
VecBase< uint32_t, 2 > uint2
VecBase< int16_t, 3 > short3
VecBase< uint8_t, 4 > uchar4
unsigned __int64 uint64_t
Definition BKE_text.h:91
void GPU_indexbuf_init(GPUIndexBufBuilder *, GPUPrimType, uint prim_len, uint vertex_len)
const Set< BMFace *, 0 > & BKE_pbvh_bmesh_node_faces(bke::pbvh::BMeshNode *node)
unsigned int uint
Definition BKE_text.h:55
ATTR_WARN_UNUSED_RESULT const BMLoop * l
MINLINE int square_i(int a)
GPUVertFormat GPU_vertformat_from_attribute(StringRef name, gpu::VertAttrType type)
void GPU_vertbuf_use(gpu::VertBuf *)
VecBase< uint32_t, 3 > uint3
ATTR_WARN_UNUSED_RESULT const BMVert * v
bool BKE_paint_is_grid_face_hidden(BoundedBitSpan grid_hidden, int gridsize, int x, int y)
CCGKey BKE_subdiv_ccg_key_top_level(const SubdivCCG &subdiv_ccg)
MINLINE void normal_float_to_short_v3(short out[3], const float in[3])
VecBase< float, 4 > float4
VecBase< int16_t, 4 > short4
VecBase< float, 2 > float2
VecBase< int32_t, 3 > int3
T & DRW_object_get_data_for_drawing(const Object &object)
BMesh const char void * data
float normal_quad_v3(float n[3], const float v1[3], const float v2[3], const float v3[3], const float v4[3])
Definition math_geom.cc:62
int CustomData_get_offset_named(const CustomData *data, eCustomDataType type, StringRef name)
MutableSpan< uint32_t > GPU_indexbuf_get_data(GPUIndexBufBuilder *)
gpu::IndexBuf * GPU_indexbuf_calloc()
BMDataLayerLookup BM_data_layer_lookup(const BMesh &bm, const StringRef name)
eCustomDataMask CD_TYPE_AS_MASK(eCustomDataType type)
MINLINE int poly_to_tri_count(int poly_count, int corner_count)
int CCG_grid_xy_to_index(const int grid_size, const int x, const int y)
Definition BKE_ccg.hh:75
void GPU_indexbuf_build_in_place(GPUIndexBufBuilder *, gpu::IndexBuf *)
T * DEG_get_original(T *id)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
void BKE_paint_face_set_overlay_color_get(int face_set, int seed, uchar r_color[4])
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)
VecBase< float, 3 > float3
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252
int GPU_batch_vertbuf_add(gpu::Batch *batch, gpu::VertBuf *vertex_buf, bool own_vbo)
MINLINE unsigned int square_uint(unsigned int a)
struct BMLoop * next
struct BMLoop * prev
struct BMVert * v
CustomData pdata
CustomData vdata
uint64_t operator()(const draw::pbvh::AttributeRequest &value) const
Definition draw_pbvh.cc:47
bke::subdiv::Subdiv * subdiv
Span< int > grid_to_face_map
VectorSet< StringRef > uv_map_names
Definition draw_pbvh.cc:73
bke::AttributeAccessor attributes
Definition draw_pbvh.cc:78
Vector< AttributeRequest > attributes
Definition DRW_pbvh.hh:52