Blender V5.3
extract_mesh_vbo_pos.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#include "BLI_math_vector_c.hh"
11
12#include "extract_mesh.hh"
13
14#include "draw_subdivision.hh"
15
16namespace blender::draw {
17
19 MutableSpan<float3> vert_data,
20 MutableSpan<float3> loose_data)
21{
22 MutableSpan loose_edge_data = loose_data.take_front(mr.loose_edges.size() * 2);
23 MutableSpan loose_vert_data = loose_data.take_back(mr.loose_verts.size());
25 mr.vert_positions.size_in_bytes() * 2 + loose_data.size_in_bytes(), [&]() {
26 array_utils::copy(mr.vert_positions, vert_data);
27 extract_mesh_loose_edge_data(mr.vert_positions, mr.edges, mr.loose_edges, loose_edge_data);
28 array_utils::gather(mr.vert_positions, mr.loose_verts, loose_vert_data);
29 });
30}
31
33 MutableSpan<float3> vert_data,
34 MutableSpan<float3> loose_data)
35{
36 const BMesh &bm = *mr.bm;
37 MutableSpan loose_edge_data = loose_data.take_front(mr.loose_edges.size() * 2);
38 MutableSpan loose_vert_data = loose_data.take_back(mr.loose_verts.size());
39
40 threading::parallel_for(IndexRange(bm.totvert), 4096, [&](const IndexRange range) {
41 for (const int i : range) {
42 vert_data[i] = bm_vert_co_get(mr, BM_vert_at_index(&const_cast<BMesh &>(bm), i));
43 }
44 });
45
46 mr.loose_edges.foreach_index(
47 [&](const int i, const int pos) {
48 const BMEdge &edge = *BM_edge_at_index(&const_cast<BMesh &>(bm), i);
49 loose_edge_data[pos * 2 + 0] = bm_vert_co_get(mr, edge.v1);
50 loose_edge_data[pos * 2 + 1] = bm_vert_co_get(mr, edge.v2);
51 },
53
54 mr.loose_verts.foreach_index(
55 [&](const int i, const int pos) {
56 const BMVert &vert = *BM_vert_at_index(&const_cast<BMesh &>(bm), i);
57 loose_vert_data[pos] = bm_vert_co_get(mr, &vert);
58 },
60}
61
63{
64 static const GPUVertFormat format = GPU_vertformat_from_attribute(
65 "pos", gpu::VertAttrType::SFLOAT_32_32_32);
66
67 gpu::VertBufPtr vert_vbo = gpu::VertBufPtr(GPU_vertbuf_create_with_format(format));
68 GPU_vertbuf_data_alloc(*vert_vbo, mr.verts_num);
69 Array<float3> loose_data(mr.loose_indices_num);
71 extract_vert_positions_mesh(mr, vert_vbo->data<float3>(), loose_data);
72 }
73 else {
74 extract_vert_positions_bm(mr, vert_vbo->data<float3>(), loose_data);
75 }
76
79 gather_vert_to_corner_gpu(mr, cache, *vert_vbo, *vbo);
80 loose_data_upload(mr, loose_data.as_span(), *vbo);
81 return vbo;
82}
83
84static void extract_loose_positions_subdiv(const DRWSubdivCache &subdiv_cache,
85 const MeshRenderData &mr,
86 gpu::VertBuf &vbo)
87{
88 const IndexMask &loose_verts = mr.loose_verts;
89 const int loose_edges_num = mr.loose_edges.size();
90 if (loose_verts.is_empty() && loose_edges_num == 0) {
91 return;
92 }
93
94 /* Make sure buffer is active for sending loose data. */
95 GPU_vertbuf_use(&vbo);
96
97 const int resolution = subdiv_cache.resolution;
98 const Span<float3> cached_positions = subdiv_cache.loose_edge_positions;
99 const int verts_per_edge = subdiv_verts_per_coarse_edge(subdiv_cache);
100 const int edges_per_edge = subdiv_edges_per_coarse_edge(subdiv_cache);
101
102 const int loose_geom_start = subdiv_cache.num_subdiv_loops;
103
104 float3 edge_data[2];
105 for (const int i : IndexRange(loose_edges_num)) {
106 const int edge_offset = loose_geom_start + i * verts_per_edge;
107 const Span<float3> positions = cached_positions.slice(i * resolution, resolution);
108 for (const int edge : IndexRange(edges_per_edge)) {
109 edge_data[0] = positions[edge + 0];
110 edge_data[1] = positions[edge + 1];
112 &vbo, (edge_offset + edge * 2) * sizeof(float3), sizeof(float3) * 2, &edge_data);
113 }
114 }
115
116 const int loose_verts_start = loose_geom_start + loose_edges_num * verts_per_edge;
117 const Span<float3> positions = mr.vert_positions;
118 loose_verts.foreach_index([&](const int i, const int pos) {
120 &vbo, (loose_verts_start + pos) * sizeof(float3), sizeof(float3), &positions[i]);
121 });
122}
123
125 const MeshRenderData &mr,
126 gpu::VertBufPtr *orco_vbo)
127{
128 static const GPUVertFormat format = GPU_vertformat_from_attribute(
129 "pos", gpu::VertAttrType::SFLOAT_32_32_32);
131 GPU_vertbuf_create_on_device(format, subdiv_full_vbo_size(mr, subdiv_cache)));
132
133 if (subdiv_cache.num_subdiv_loops == 0) {
134 extract_loose_positions_subdiv(subdiv_cache, mr, *vbo);
135 return vbo;
136 }
137
138 if (orco_vbo) {
139 /* FIXME(fclem): We use the last component as a way to differentiate from generic vertex
140 * attributes. This is a substantial waste of video-ram and should be done another way.
141 * Unfortunately, at the time of writing, I did not found any other "non disruptive"
142 * alternative. */
143 static const GPUVertFormat format = GPU_vertformat_from_attribute(
144 "orco", gpu::VertAttrType::SFLOAT_32_32_32_32);
145 *orco_vbo = gpu::VertBufPtr(
146 GPU_vertbuf_create_on_device(format, subdiv_cache.num_subdiv_loops));
147 }
148
149 draw_subdiv_extract_pos(subdiv_cache, vbo.get(), orco_vbo ? orco_vbo->get() : nullptr);
150
151 extract_loose_positions_subdiv(subdiv_cache, mr, *vbo);
152 return vbo;
153}
154
155} // namespace blender::draw
Span< T > as_span() const
Definition BLI_array.hh:254
constexpr IndexRange()=default
constexpr MutableSpan take_back(const int64_t n) const
Definition BLI_span.hh:646
constexpr int64_t size_in_bytes() const
Definition BLI_span.hh:507
constexpr MutableSpan take_front(const int64_t n) const
Definition BLI_span.hh:635
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
Extraction of Mesh data into VBO to feed to GPU.
static void extract_vert_positions_bm(const MeshRenderData &mr, MutableSpan< float3 > vert_data, MutableSpan< float3 > loose_data)
int subdiv_verts_per_coarse_edge(const DRWSubdivCache &cache)
int subdiv_edges_per_coarse_edge(const DRWSubdivCache &cache)
static void extract_vert_positions_mesh(const MeshRenderData &mr, MutableSpan< float3 > vert_data, MutableSpan< float3 > loose_data)
static void extract_loose_positions_subdiv(const DRWSubdivCache &subdiv_cache, const MeshRenderData &mr, gpu::VertBuf &vbo)
void draw_subdiv_extract_pos(const DRWSubdivCache &cache, gpu::VertBuf *pos, gpu::VertBuf *orco)
gpu::VertBufPtr extract_positions_subdiv(const DRWSubdivCache &subdiv_cache, const MeshRenderData &mr, gpu::VertBufPtr *orco_vbo)
int subdiv_full_vbo_size(const MeshRenderData &mr, const DRWSubdivCache &cache)
gpu::VertBufPtr extract_positions(const MeshRenderData &mr, MeshBufferCache &cache)
void gather_vert_to_corner_gpu(const MeshRenderData &mr, MeshBufferCache &cache, gpu::VertBuf &src, gpu::VertBuf &dst)
void loose_data_upload(const MeshRenderData &mr, const Span< T > loose_data, gpu::VertBuf &dst)
BLI_INLINE const float * bm_vert_co_get(const MeshRenderData &mr, const BMVert *eve)
constexpr ParallelGrainSize grain_size(int grain_size)
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
void memory_bandwidth_bound_task(const int64_t approximate_bytes_touched, const Function &function)
Definition BLI_task.hh:251
BLI_INLINE BMEdge * BM_edge_at_index(BMesh *bm, const int index)
gpu::VertBuf * GPU_vertbuf_create_on_device(const GPUVertFormat &format, uint v_len)
GPUVertFormat GPU_vertformat_from_attribute(StringRef name, gpu::VertAttrType type)
void GPU_vertbuf_use(gpu::VertBuf *)
void GPU_vertbuf_update_sub(gpu::VertBuf *verts, uint start, uint len, const void *data)
BLI_INLINE BMVert * BM_vert_at_index(BMesh *bm, const int index)
void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len)
VecBase< float, 3 > float3