Blender V5.0
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.h"
11
12#include "extract_mesh.hh"
13
14#include "draw_subdivision.hh"
15
16namespace blender::draw {
17
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
25 mr.vert_positions.size_in_bytes() + mr.corner_verts.size_in_bytes() +
26 vbo_data.size_in_bytes() + mr.loose_edges.size(),
27 [&]() {
28 array_utils::gather(mr.vert_positions, mr.corner_verts, corners_data);
29 extract_mesh_loose_edge_data(mr.vert_positions, mr.edges, mr.loose_edges, loose_edge_data);
30 array_utils::gather(mr.vert_positions, mr.loose_verts, loose_vert_data);
31 });
32}
33
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] = bm_vert_co_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] = bm_vert_co_get(mr, edge.v1);
58 loose_edge_data[i * 2 + 1] = bm_vert_co_get(mr, edge.v2);
59 }
60 });
61
62 const Span<int> loose_verts = mr.loose_verts;
63 threading::parallel_for(loose_verts.index_range(), 2048, [&](const IndexRange range) {
64 for (const int i : range) {
65 const BMVert &vert = *BM_vert_at_index(&const_cast<BMesh &>(bm), loose_verts[i]);
66 loose_vert_data[i] = bm_vert_co_get(mr, &vert);
67 }
68 });
69}
70
72{
74 "pos", gpu::VertAttrType::SFLOAT_32_32_32);
77
78 MutableSpan vbo_data = vbo->data<float3>();
80 extract_positions_mesh(mr, vbo_data);
81 }
82 else {
83 extract_positions_bm(mr, vbo_data);
84 }
85
86 return vbo;
87}
88
89static void extract_loose_positions_subdiv(const DRWSubdivCache &subdiv_cache,
90 const MeshRenderData &mr,
91 gpu::VertBuf &vbo)
92{
93 const Span<int> loose_verts = mr.loose_verts;
94 const int loose_edges_num = mr.loose_edges.size();
95 if (loose_verts.is_empty() && loose_edges_num == 0) {
96 return;
97 }
98
99 /* Make sure buffer is active for sending loose data. */
100 GPU_vertbuf_use(&vbo);
101
102 const int resolution = subdiv_cache.resolution;
103 const Span<float3> cached_positions = subdiv_cache.loose_edge_positions;
104 const int verts_per_edge = subdiv_verts_per_coarse_edge(subdiv_cache);
105 const int edges_per_edge = subdiv_edges_per_coarse_edge(subdiv_cache);
106
107 const int loose_geom_start = subdiv_cache.num_subdiv_loops;
108
109 float3 edge_data[2];
110 for (const int i : IndexRange(loose_edges_num)) {
111 const int edge_offset = loose_geom_start + i * verts_per_edge;
112 const Span<float3> positions = cached_positions.slice(i * resolution, resolution);
113 for (const int edge : IndexRange(edges_per_edge)) {
114 edge_data[0] = positions[edge + 0];
115 edge_data[1] = positions[edge + 1];
117 &vbo, (edge_offset + edge * 2) * sizeof(float3), sizeof(float3) * 2, &edge_data);
118 }
119 }
120
121 const int loose_verts_start = loose_geom_start + loose_edges_num * verts_per_edge;
122 const Span<float3> positions = mr.vert_positions;
123 for (const int i : loose_verts.index_range()) {
125 (loose_verts_start + i) * sizeof(float3),
126 sizeof(float3),
127 &positions[loose_verts[i]]);
128 }
129}
130
132 const MeshRenderData &mr,
133 gpu::VertBufPtr *orco_vbo)
134{
136 "pos", gpu::VertAttrType::SFLOAT_32_32_32);
139
140 if (subdiv_cache.num_subdiv_loops == 0) {
141 extract_loose_positions_subdiv(subdiv_cache, mr, *vbo);
142 return vbo;
143 }
144
145 if (orco_vbo) {
146 /* FIXME(fclem): We use the last component as a way to differentiate from generic vertex
147 * attributes. This is a substantial waste of video-ram and should be done another way.
148 * Unfortunately, at the time of writing, I did not found any other "non disruptive"
149 * alternative. */
151 "orco", gpu::VertAttrType::SFLOAT_32_32_32_32);
152 *orco_vbo = gpu::VertBufPtr(
154 }
155
156 draw_subdiv_extract_pos(subdiv_cache, vbo.get(), orco_vbo ? orco_vbo->get() : nullptr);
157
158 extract_loose_positions_subdiv(subdiv_cache, mr, *vbo);
159 return vbo;
160}
161
162} // namespace blender::draw
blender::gpu::VertBuf * GPU_vertbuf_create_on_device(const GPUVertFormat &format, uint v_len)
void GPU_vertbuf_use(blender::gpu::VertBuf *)
static blender::gpu::VertBuf * GPU_vertbuf_create_with_format(const GPUVertFormat &format)
void GPU_vertbuf_data_alloc(blender::gpu::VertBuf &verts, uint v_len)
void GPU_vertbuf_update_sub(blender::gpu::VertBuf *verts, uint start, uint len, const void *data)
GPUVertFormat GPU_vertformat_from_attribute(blender::StringRef name, blender::gpu::VertAttrType type)
BMesh * bm
constexpr int64_t size_in_bytes() const
Definition BLI_span.hh:268
constexpr int64_t size() const
Definition BLI_span.hh:252
constexpr IndexRange index_range() const
Definition BLI_span.hh:401
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 int64_t size_in_bytes() const
Definition BLI_span.hh:501
constexpr MutableSpan take_front(const int64_t n) const
Definition BLI_span.hh:629
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:137
constexpr IndexRange index_range() const
Definition BLI_span.hh:401
constexpr bool is_empty() const
Definition BLI_span.hh:260
Extraction of Mesh data into VBO to feed to GPU.
format
int subdiv_verts_per_coarse_edge(const DRWSubdivCache &cache)
int subdiv_edges_per_coarse_edge(const DRWSubdivCache &cache)
static void extract_positions_mesh(const MeshRenderData &mr, MutableSpan< float3 > vbo_data)
static void extract_positions_bm(const MeshRenderData &mr, MutableSpan< float3 > vbo_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)
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
void memory_bandwidth_bound_task(const int64_t approximate_bytes_touched, const Function &function)
Definition BLI_task.hh:265
VecBase< float, 3 > float3
i
Definition text_draw.cc:230