Blender V5.0
extract_mesh_vbo_vnor.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_array_utils.hh"
10
12
13#include "extract_mesh.hh"
14
15namespace blender::draw {
16
19{
20 MutableSpan corners_data = vbo_data.take_front(mr.corners_num);
21 MutableSpan loose_edge_data = vbo_data.slice(mr.corners_num, mr.loose_edges.size() * 2);
22 MutableSpan loose_vert_data = vbo_data.take_back(mr.loose_verts.size());
23
24 const Span<float3> vert_normals = mr.mesh->vert_normals();
25
26 Array<gpu::PackedNormal> converted(vert_normals.size());
27 convert_normals(vert_normals, converted.as_mutable_span());
28 array_utils::gather(converted.as_span(), mr.corner_verts, corners_data);
29 extract_mesh_loose_edge_data(converted.as_span(), mr.edges, mr.loose_edges, loose_edge_data);
30 array_utils::gather(converted.as_span(), mr.loose_verts, loose_vert_data);
31}
32
35{
36 const BMesh &bm = *mr.bm;
37 MutableSpan corners_data = vbo_data.take_front(mr.corners_num);
38 MutableSpan loose_edge_data = vbo_data.slice(mr.corners_num, mr.loose_edges.size() * 2);
39 MutableSpan loose_vert_data = vbo_data.take_back(mr.loose_verts.size());
40
41 threading::parallel_for(IndexRange(bm.totface), 2048, [&](const IndexRange range) {
42 for (const int face_index : range) {
43 const BMFace &face = *BM_face_at_index(&const_cast<BMesh &>(bm), face_index);
44 const BMLoop *loop = BM_FACE_FIRST_LOOP(&face);
45 for ([[maybe_unused]] const int i : IndexRange(face.len)) {
46 const int index = BM_elem_index_get(loop);
47 corners_data[index] = gpu::convert_normal<gpu::PackedNormal>(bm_vert_no_get(mr, loop->v));
48 loop = loop->next;
49 }
50 }
51 });
52
53 const Span<int> loose_edges = mr.loose_edges;
54 threading::parallel_for(loose_edges.index_range(), 4096, [&](const IndexRange range) {
55 for (const int i : range) {
56 const BMEdge &edge = *BM_edge_at_index(&const_cast<BMesh &>(bm), loose_edges[i]);
57 loose_edge_data[i * 2 + 0] = gpu::convert_normal<gpu::PackedNormal>(
58 bm_vert_no_get(mr, edge.v1));
59 loose_edge_data[i * 2 + 1] = gpu::convert_normal<gpu::PackedNormal>(
60 bm_vert_no_get(mr, edge.v2));
61 }
62 });
63
64 const Span<int> loose_verts = mr.loose_verts;
65 threading::parallel_for(loose_verts.index_range(), 2048, [&](const IndexRange range) {
66 for (const int i : range) {
67 const BMVert &vert = *BM_vert_at_index(&const_cast<BMesh &>(bm), loose_verts[i]);
68 loose_vert_data[i] = gpu::convert_normal<gpu::PackedNormal>(bm_vert_no_get(mr, &vert));
69 }
70 });
71}
72
74{
76 gpu::VertAttrType::SNORM_10_10_10_2);
77
80 MutableSpan vbo_data = vbo->data<gpu::PackedNormal>();
81
83 extract_vert_normals_mesh(mr, vbo_data);
84 }
85 else {
86 extract_vert_normals_bm(mr, vbo_data);
87 }
88 return vbo;
89}
90
91} // namespace blender::draw
static blender::gpu::VertBuf * GPU_vertbuf_create_with_format(const GPUVertFormat &format)
void GPU_vertbuf_data_alloc(blender::gpu::VertBuf &verts, uint v_len)
GPUVertFormat GPU_vertformat_from_attribute(blender::StringRef name, blender::gpu::VertAttrType type)
BMesh * bm
constexpr int64_t size() const
Definition BLI_span.hh:252
constexpr IndexRange index_range() const
Definition BLI_span.hh:401
Span< T > as_span() const
Definition BLI_array.hh:243
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:248
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:573
constexpr MutableSpan take_back(const int64_t n) const
Definition BLI_span.hh:640
constexpr T * data() const
Definition BLI_span.hh:539
constexpr MutableSpan take_front(const int64_t n) const
Definition BLI_span.hh:629
constexpr int64_t size() const
Definition BLI_span.hh:252
constexpr IndexRange index_range() const
Definition BLI_span.hh:401
Extraction of Mesh data into VBO to feed to GPU.
format
void gather(const GVArray &src, const IndexMask &indices, GMutableSpan dst, int64_t grain_size=4096)
gpu::VertBufPtr extract_vert_normals(const MeshRenderData &mr)
static void extract_vert_normals_bm(const MeshRenderData &mr, MutableSpan< GPUType > normals)
static void extract_vert_normals_mesh(const MeshRenderData &mr, MutableSpan< gpu::PackedNormal > vbo_data)
void extract_mesh_loose_edge_data(const Span< T > vert_data, const Span< int2 > edges, const Span< int > loose_edges, MutableSpan< T > gpu_data)
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:93