Blender V5.3
extract_mesh_vbo_edit_data.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
11#include "extract_mesh.hh"
12
13#include "draw_cache_impl.hh"
14
15#include "draw_subdivision.hh"
16
17namespace blender::draw {
18
20 const BMEdge *eed,
21 EditLoopData &eattr)
22{
23 const ToolSettings *ts = mr.toolsettings;
24 const bool is_vertex_select_mode = (ts != nullptr) && (ts->selectmode & SCE_SELECT_VERTEX) != 0;
25 const bool is_face_only_select_mode = (ts != nullptr) && (ts->selectmode == SCE_SELECT_FACE);
26
27 if (eed == mr.eed_act) {
29 }
30 if (!is_vertex_select_mode && BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
32 }
33 if (is_vertex_select_mode && BM_elem_flag_test(eed->v1, BM_ELEM_SELECT) &&
35 {
38 }
40 eattr.e_flag |= VFLAG_EDGE_SEAM;
41 }
43 eattr.e_flag |= VFLAG_EDGE_SHARP;
44 }
45
46 /* Use active edge color for active face edges because
47 * specular highlights make it hard to see #55456#510873.
48 *
49 * This isn't ideal since it can't be used when mixing edge/face modes
50 * but it's still better than not being able to see the active face. */
51 if (is_face_only_select_mode) {
52 if (mr.efa_act != nullptr) {
53 if (BM_edge_in_face(eed, mr.efa_act)) {
55 }
56 }
57 }
58
59 /* Use half a byte for value range */
60 if (mr.edge_crease_ofs != -1) {
61 float crease = BM_ELEM_CD_GET_FLOAT(eed, mr.edge_crease_ofs);
62 if (crease > 0) {
63 eattr.crease = uchar(ceil(crease * 15.0f));
64 }
65 }
66 /* Use a byte for value range */
67 if (mr.bweight_ofs != -1) {
68 float bweight = BM_ELEM_CD_GET_FLOAT(eed, mr.bweight_ofs);
69 if (bweight > 0) {
70 eattr.bweight = uchar(bweight * 255.0f);
71 }
72 }
73#ifdef WITH_FREESTYLE
74 if (mr.freestyle_edge_ofs != -1) {
77 }
78 }
79#endif
80}
81
83 const BMVert *eve,
84 EditLoopData &eattr)
85{
86 if (eve == mr.eve_act) {
88 }
91 }
92 /* Use half a byte for value range */
93 if (mr.vert_crease_ofs != -1) {
94 float crease = BM_ELEM_CD_GET_FLOAT(eve, mr.vert_crease_ofs);
95 if (crease > 0) {
96 eattr.crease |= uchar(ceil(crease * 15.0f)) << 4;
97 }
98 }
99}
100
102{
103 static const GPUVertFormat format = []() {
104 GPUVertFormat format{};
105 /* WARNING: Adjust #EditLoopData struct accordingly. */
106 GPU_vertformat_attr_add(&format, "data", gpu::VertAttrType::UINT_8_8_8_8);
107 GPU_vertformat_alias_add(&format, "flag");
108 return format;
109 }();
110 return format;
111}
112
114{
115 MutableSpan corners_data = vbo_data.take_front(mr.corners_num);
116 MutableSpan loose_edge_data = vbo_data.slice(mr.corners_num, mr.loose_edges.size() * 2);
117 MutableSpan loose_vert_data = vbo_data.take_back(mr.loose_verts.size());
118
119 const BMUVOffsets uv_offsets_none = BMUVOFFSETS_NONE;
120 const OffsetIndices faces = mr.faces;
121 const Span<int> corner_verts = mr.corner_verts;
122 const Span<int> corner_edges = mr.corner_edges;
123 threading::parallel_for(faces.index_range(), 2048, [&](const IndexRange range) {
124 for (const int face : range) {
125 EditLoopData face_value = {};
126 if (const BMFace *bm_face = bm_original_face_get(mr, face)) {
127 mesh_render_data_face_flag(mr, bm_face, uv_offsets_none, face_value);
128 }
129 for (const int corner : faces[face]) {
130 EditLoopData &value = corners_data[corner];
131 value = face_value;
132 if (const BMVert *bm_vert = bm_original_vert_get(mr, corner_verts[corner])) {
133 mesh_render_data_vert_flag(mr, bm_vert, value);
134 }
135 if (const BMEdge *bm_edge = bm_original_edge_get(mr, corner_edges[corner])) {
136 mesh_render_data_edge_flag(mr, bm_edge, value);
137 }
138 }
139 }
140 });
141
142 const Span<int2> edges = mr.edges;
143 mr.loose_edges.foreach_index(
144 [&](const int edge_i, const int pos) {
145 EditLoopData &value_1 = loose_edge_data[pos * 2 + 0];
146 EditLoopData &value_2 = loose_edge_data[pos * 2 + 1];
147 if (const BMEdge *bm_edge = bm_original_edge_get(mr, edge_i)) {
148 value_1 = {};
149 mesh_render_data_edge_flag(mr, bm_edge, value_1);
150 value_2 = value_1;
151 }
152 else {
153 value_2 = value_1 = {};
154 }
155 const int2 edge = edges[edge_i];
156 if (const BMVert *bm_vert = bm_original_vert_get(mr, edge[0])) {
157 mesh_render_data_vert_flag(mr, bm_vert, value_1);
158 }
159 if (const BMVert *bm_vert = bm_original_vert_get(mr, edge[1])) {
160 mesh_render_data_vert_flag(mr, bm_vert, value_2);
161 }
162 },
164
165 mr.loose_verts.foreach_index(
166 [&](const int vert, const int pos) {
167 loose_vert_data[pos] = {};
168 if (const BMVert *eve = bm_original_vert_get(mr, vert)) {
169 mesh_render_data_vert_flag(mr, eve, loose_vert_data[pos]);
170 }
171 },
173}
174
176{
177 MutableSpan corners_data = vbo_data.take_front(mr.corners_num);
178 MutableSpan loose_edge_data = vbo_data.slice(mr.corners_num, mr.loose_edges.size() * 2);
179 MutableSpan loose_vert_data = vbo_data.take_back(mr.loose_verts.size());
180
181 const BMesh &bm = *mr.bm;
182 const BMUVOffsets uv_offsets_none = BMUVOFFSETS_NONE;
183
184 threading::parallel_for(IndexRange(bm.totface), 2048, [&](const IndexRange range) {
185 for (const int face_index : range) {
186 const BMFace &face = *BM_face_at_index(&const_cast<BMesh &>(bm), face_index);
187 EditLoopData face_value = {};
188 mesh_render_data_face_flag(mr, &face, uv_offsets_none, face_value);
189 const BMLoop *loop = BM_FACE_FIRST_LOOP(&face);
190 for ([[maybe_unused]] const int i : IndexRange(face.len)) {
191 const int index = BM_elem_index_get(loop);
192 EditLoopData &value = corners_data[index];
193 value = face_value;
194 mesh_render_data_edge_flag(mr, loop->e, value);
195 mesh_render_data_vert_flag(mr, loop->v, value);
196 loop = loop->next;
197 }
198 }
199 });
200
201 mr.loose_edges.foreach_index(
202 [&](const int edge_i, const int pos) {
203 EditLoopData &value_1 = loose_edge_data[pos * 2 + 0];
204 EditLoopData &value_2 = loose_edge_data[pos * 2 + 1];
205 const BMEdge &edge = *BM_edge_at_index(&const_cast<BMesh &>(bm), edge_i);
206 value_1 = {};
207 mesh_render_data_edge_flag(mr, &edge, value_1);
208 value_2 = value_1;
209 mesh_render_data_vert_flag(mr, edge.v1, value_1);
210 mesh_render_data_vert_flag(mr, edge.v2, value_2);
211 },
212 exec_mode::grain_size(2048));
213
214 mr.loose_verts.foreach_index(
215 [&](const int vert_i, const int pos) {
216 loose_vert_data[pos] = {};
217 const BMVert &vert = *BM_vert_at_index(&const_cast<BMesh &>(bm), vert_i);
218 mesh_render_data_vert_flag(mr, &vert, loose_vert_data[pos]);
219 },
220 exec_mode::grain_size(2048));
221}
222
224{
225 gpu::VertBufPtr vbo = gpu::VertBufPtr(GPU_vertbuf_create_with_format(get_edit_data_format()));
226 const int size = mr.corners_num + mr.loose_indices_num;
227 GPU_vertbuf_data_alloc(*vbo, size);
228 MutableSpan vbo_data = vbo->data<EditLoopData>();
230 extract_edit_data_mesh(mr, vbo_data);
231 }
232 else {
233 extract_edit_data_bm(mr, vbo_data);
234 }
235 return vbo;
236}
237
239 const DRWSubdivCache &subdiv_cache,
241{
242 const BMUVOffsets uv_offsets_none = BMUVOFFSETS_NONE;
243 const int corners_num = subdiv_cache.num_subdiv_loops;
244 const int loose_edges_num = mr.loose_edges.size();
245 const int verts_per_edge = subdiv_verts_per_coarse_edge(subdiv_cache);
246 const Span<int> subdiv_loop_face_index(subdiv_cache.subdiv_loop_face_index, corners_num);
247 const Span<int> subdiv_loop_vert_index = subdiv_cache.verts_orig_index->data<int>();
248 /* NOTE: #subdiv_loop_edge_index already has the origindex layer baked in. */
249 const Span<int> subdiv_loop_edge_index = subdiv_cache.edges_orig_index->data<int>();
250
251 MutableSpan corners_data = vbo_data.take_front(corners_num);
252 MutableSpan loose_edge_data = vbo_data.slice(corners_num, loose_edges_num * verts_per_edge);
253 MutableSpan loose_vert_data = vbo_data.take_back(mr.loose_verts.size());
254
255 threading::parallel_for(IndexRange(subdiv_cache.num_subdiv_quads), 2048, [&](IndexRange range) {
256 for (const int subdiv_quad : range) {
257 const int coarse_face = subdiv_loop_face_index[subdiv_quad * 4];
258
259 EditLoopData face_value = {};
260 if (const BMFace *bm_face = bm_original_face_get(mr, coarse_face)) {
261 mesh_render_data_face_flag(mr, bm_face, uv_offsets_none, face_value);
262 }
263 for (const int subdiv_corner : IndexRange(subdiv_quad * 4, 4)) {
264 EditLoopData &value = corners_data[subdiv_corner];
265 value = face_value;
266
267 const int vert_origindex = subdiv_loop_vert_index[subdiv_corner];
268 if (vert_origindex != -1) {
269 if (const BMVert *bm_vert = bm_original_vert_get(mr, vert_origindex)) {
270 mesh_render_data_vert_flag(mr, bm_vert, value);
271 }
272 }
273
274 const int edge_origindex = subdiv_loop_edge_index[subdiv_corner];
275 if (edge_origindex != -1) {
276 if (const BMEdge *bm_edge = BM_edge_at_index(mr.bm, edge_origindex)) {
277 mesh_render_data_edge_flag(mr, bm_edge, value);
278 }
279 }
280 }
281 }
282 });
283
284 const Span<int2> edges = mr.edges;
285 mr.loose_edges.foreach_index(
286 [&](const int edge_i, const int pos) {
287 MutableSpan<EditLoopData> data = loose_edge_data.slice(pos * verts_per_edge,
288 verts_per_edge);
289 if (const BMEdge *edge = bm_original_edge_get(mr, edge_i)) {
290 EditLoopData value{};
291 mesh_render_data_edge_flag(mr, edge, value);
292 data.fill(value);
293 }
294 else {
295 data.fill({});
296 }
297 const int2 edge = edges[edge_i];
298 if (const BMVert *bm_vert = bm_original_vert_get(mr, edge[0])) {
299 mesh_render_data_vert_flag(mr, bm_vert, data.first());
300 }
301 if (const BMVert *bm_vert = bm_original_vert_get(mr, edge[1])) {
302 mesh_render_data_vert_flag(mr, bm_vert, data.last());
303 }
304 },
305 exec_mode::grain_size(2048));
306
307 mr.loose_verts.foreach_index(
308 [&](const int vert, const int pos) {
309 loose_vert_data[pos] = {};
310 if (const BMVert *eve = bm_original_vert_get(mr, vert)) {
311 mesh_render_data_vert_flag(mr, eve, loose_vert_data[pos]);
312 }
313 },
314 exec_mode::grain_size(2048));
315}
316
318 const DRWSubdivCache &subdiv_cache,
320{
321 const BMUVOffsets uv_offsets_none = BMUVOFFSETS_NONE;
322 const int corners_num = subdiv_cache.num_subdiv_loops;
323 const int loose_edges_num = mr.loose_edges.size();
324 const int verts_per_edge = subdiv_verts_per_coarse_edge(subdiv_cache);
325 const Span<int> subdiv_loop_face_index(subdiv_cache.subdiv_loop_face_index, corners_num);
326 const Span<int> subdiv_loop_vert_index = subdiv_cache.verts_orig_index->data<int>();
327 const Span<int> subdiv_loop_edge_index = subdiv_cache.edges_orig_index->data<int>();
328
329 MutableSpan corners_data = vbo_data.take_front(corners_num);
330 MutableSpan loose_edge_data = vbo_data.slice(corners_num, loose_edges_num * verts_per_edge);
331 MutableSpan loose_vert_data = vbo_data.take_back(mr.loose_verts.size());
332
333 BMesh &bm = *mr.bm;
334 threading::parallel_for(IndexRange(subdiv_cache.num_subdiv_quads), 2048, [&](IndexRange range) {
335 for (const int subdiv_quad : range) {
336 const int coarse_face = subdiv_loop_face_index[subdiv_quad * 4];
337 const BMFace *bm_face = BM_face_at_index(&bm, coarse_face);
338
339 EditLoopData face_value = {};
340 mesh_render_data_face_flag(mr, bm_face, uv_offsets_none, face_value);
341
342 for (const int subdiv_corner : IndexRange(subdiv_quad * 4, 4)) {
343 EditLoopData &value = corners_data[subdiv_corner];
344 value = face_value;
345
346 const int vert_origindex = subdiv_loop_vert_index[subdiv_corner];
347 if (vert_origindex != -1) {
348 const BMVert *bm_vert = BM_vert_at_index(mr.bm, vert_origindex);
349 mesh_render_data_vert_flag(mr, bm_vert, value);
350 }
351
352 const int edge_origindex = subdiv_loop_edge_index[subdiv_corner];
353 if (edge_origindex != -1) {
354 const BMEdge *bm_edge = BM_edge_at_index(mr.bm, edge_origindex);
355 mesh_render_data_edge_flag(mr, bm_edge, value);
356 }
357 }
358 }
359 });
360
361 mr.loose_edges.foreach_index(
362 [&](const int edge_i, const int pos) {
363 MutableSpan<EditLoopData> data = loose_edge_data.slice(pos * verts_per_edge,
364 verts_per_edge);
365 const BMEdge *edge = BM_edge_at_index(&bm, edge_i);
366 EditLoopData value{};
367 mesh_render_data_edge_flag(mr, edge, value);
368 data.fill(value);
369 mesh_render_data_vert_flag(mr, edge->v1, data.first());
370 mesh_render_data_vert_flag(mr, edge->v2, data.last());
371 },
372 exec_mode::grain_size(2048));
373
374 mr.loose_verts.foreach_index(
375 [&](const int vert_i, const int pos) {
376 loose_vert_data[pos] = {};
377 const BMVert *vert = BM_vert_at_index(&bm, vert_i);
378 mesh_render_data_vert_flag(mr, vert, loose_vert_data[pos]);
379 },
380 exec_mode::grain_size(2048));
381}
382
384 const DRWSubdivCache &subdiv_cache)
385{
386 gpu::VertBufPtr vbo = gpu::VertBufPtr(GPU_vertbuf_create_with_format(get_edit_data_format()));
387 const int size = subdiv_full_vbo_size(mr, subdiv_cache);
388 GPU_vertbuf_data_alloc(*vbo, size);
389 MutableSpan vbo_data = vbo->data<EditLoopData>();
391 extract_edit_subdiv_data_mesh(mr, subdiv_cache, vbo_data);
392 }
393 else {
394 extract_edit_subdiv_data_bm(mr, subdiv_cache, vbo_data);
395 }
396 return vbo;
397}
398
399} // namespace blender::draw
#define BMUVOFFSETS_NONE
#define BM_ELEM_CD_GET_BOOL(ele, offset)
#define BM_ELEM_CD_GET_FLOAT(ele, offset)
#define BM_elem_flag_test(ele, hflag)
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:579
constexpr MutableSpan take_back(const int64_t n) const
Definition BLI_span.hh:646
constexpr T * data() const
Definition BLI_span.hh:545
constexpr MutableSpan take_front(const int64_t n) const
Definition BLI_span.hh:635
MutableSpan< T > data()
Extraction of Mesh data into VBO to feed to GPU.
#define ceil
BLI_INLINE BMVert * bm_original_vert_get(const MeshRenderData &mr, int idx)
gpu::VertBufPtr extract_edit_data_subdiv(const MeshRenderData &mr, const DRWSubdivCache &subdiv_cache)
static void extract_edit_subdiv_data_bm(const MeshRenderData &mr, const DRWSubdivCache &subdiv_cache, MutableSpan< EditLoopData > vbo_data)
int subdiv_verts_per_coarse_edge(const DRWSubdivCache &cache)
BLI_INLINE BMEdge * bm_original_edge_get(const MeshRenderData &mr, int idx)
gpu::VertBufPtr extract_edit_data(const MeshRenderData &mr)
static void mesh_render_data_edge_flag(const MeshRenderData &mr, const BMEdge *eed, EditLoopData &eattr)
static void extract_edit_data_mesh(const MeshRenderData &mr, MutableSpan< EditLoopData > vbo_data)
int subdiv_full_vbo_size(const MeshRenderData &mr, const DRWSubdivCache &cache)
static void extract_edit_data_bm(const MeshRenderData &mr, MutableSpan< EditLoopData > vbo_data)
static void extract_edit_subdiv_data_mesh(const MeshRenderData &mr, const DRWSubdivCache &subdiv_cache, MutableSpan< EditLoopData > vbo_data)
static void mesh_render_data_vert_flag(const MeshRenderData &mr, const BMVert *eve, EditLoopData &eattr)
static const GPUVertFormat & get_edit_data_format()
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
bool BM_edge_in_face(const BMEdge *e, const BMFace *f)
void GPU_vertformat_alias_add(GPUVertFormat *, StringRef alias)
BLI_INLINE BMEdge * BM_edge_at_index(BMesh *bm, const int index)
VecBase< int32_t, 2 > int2
unsigned char uchar
Definition BKE_text.h:58
BMesh const char void * data
BLI_INLINE BMVert * BM_vert_at_index(BMesh *bm, const int index)
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len)
void vert(const int vert_id, float4 &position)
const ToolSettings * toolsettings
OffsetIndices< int > faces