1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2021 by Blender Foundation.
* All rights reserved.
*/
/** \file
* \ingroup draw
*/
#include "extract_mesh.h"
#include "draw_cache_impl.h"
#include "draw_subdivision.h"
namespace blender::draw {
/* ---------------------------------------------------------------------- */
/** \name Extract Edit Mode Data / Flags
* \{ */
static void mesh_render_data_edge_flag(const MeshRenderData *mr,
const BMEdge *eed,
EditLoopData *eattr)
{
const ToolSettings *ts = mr->toolsettings;
const bool is_vertex_select_mode = (ts != nullptr) && (ts->selectmode & SCE_SELECT_VERTEX) != 0;
const bool is_face_only_select_mode = (ts != nullptr) && (ts->selectmode == SCE_SELECT_FACE);
if (eed == mr->eed_act) {
eattr->e_flag |= VFLAG_EDGE_ACTIVE;
}
if (!is_vertex_select_mode && BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
eattr->e_flag |= VFLAG_EDGE_SELECTED;
}
if (is_vertex_select_mode && BM_elem_flag_test(eed->v1, BM_ELEM_SELECT) &&
BM_elem_flag_test(eed->v2, BM_ELEM_SELECT)) {
eattr->e_flag |= VFLAG_EDGE_SELECTED;
eattr->e_flag |= VFLAG_VERT_SELECTED;
}
if (BM_elem_flag_test(eed, BM_ELEM_SEAM)) {
eattr->e_flag |= VFLAG_EDGE_SEAM;
}
if (!BM_elem_flag_test(eed, BM_ELEM_SMOOTH)) {
eattr->e_flag |= VFLAG_EDGE_SHARP;
}
/* Use active edge color for active face edges because
* specular highlights make it hard to see T55456#510873.
*
* This isn't ideal since it can't be used when mixing edge/face modes
* but it's still better than not being able to see the active face. */
if (is_face_only_select_mode) {
if (mr->efa_act != nullptr) {
if (BM_edge_in_face(eed, mr->efa_act)) {
eattr->e_flag |= VFLAG_EDGE_ACTIVE;
}
}
}
/* Use half a byte for value range */
if (mr->edge_crease_ofs != -1) {
float crease = BM_ELEM_CD_GET_FLOAT(eed, mr->edge_crease_ofs);
if (crease > 0) {
eattr->crease = (uchar)ceil(crease * 15.0f);
}
}
/* Use a byte for value range */
if (mr->bweight_ofs != -1) {
float bweight = BM_ELEM_CD_GET_FLOAT(eed, mr->bweight_ofs);
if (bweight > 0) {
eattr->bweight = (uchar)(bweight * 255.0f);
}
}
#ifdef WITH_FREESTYLE
if (mr->freestyle_edge_ofs != -1) {
const FreestyleEdge *fed = (const FreestyleEdge *)BM_ELEM_CD_GET_VOID_P(
eed, mr->freestyle_edge_ofs);
if (fed->flag & FREESTYLE_EDGE_MARK) {
eattr->e_flag |= VFLAG_EDGE_FREESTYLE;
}
}
#endif
}
static void mesh_render_data_vert_flag(const MeshRenderData *mr,
const BMVert *eve,
EditLoopData *eattr)
{
if (eve == mr->eve_act) {
eattr->e_flag |= VFLAG_VERT_ACTIVE;
}
if (BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
eattr->e_flag |= VFLAG_VERT_SELECTED;
}
/* Use half a byte for value range */
if (mr->vert_crease_ofs != -1) {
float crease = BM_ELEM_CD_GET_FLOAT(eve, mr->vert_crease_ofs);
if (crease > 0) {
eattr->crease |= (uchar)ceil(crease * 15.0f) << 4;
}
}
}
static GPUVertFormat *get_edit_data_format()
{
static GPUVertFormat format = {0};
if (format.attr_len == 0) {
/* WARNING: Adjust #EditLoopData struct accordingly. */
GPU_vertformat_attr_add(&format, "data", GPU_COMP_U8, 4, GPU_FETCH_INT);
GPU_vertformat_alias_add(&format, "flag");
}
return &format;
}
static void extract_edit_data_init(const MeshRenderData *mr,
struct MeshBatchCache *UNUSED(cache),
void *buf,
void *tls_data)
{
GPUVertBuf *vbo = static_cast<GPUVertBuf *>(buf);
GPUVertFormat *format = get_edit_data_format();
GPU_vertbuf_init_with_format(vbo, format);
GPU_vertbuf_data_alloc(vbo, mr->loop_len + mr->loop_loose_len);
EditLoopData *vbo_data = (EditLoopData *)GPU_vertbuf_get_data(vbo);
*(EditLoopData **)tls_data = vbo_data;
}
static void extract_edit_data_iter_poly_bm(const MeshRenderData *mr,
const BMFace *f,
const int UNUSED(f_index),
void *_data)
{
EditLoopData *vbo_data = *(EditLoopData **)_data;
BMLoop *l_iter, *l_first;
l_iter = l_first = BM_FACE_FIRST_LOOP(f);
do {
const int l_index = BM_elem_index_get(l_iter);
EditLoopData *data = vbo_data + l_index;
memset(data, 0x0, sizeof(*data));
mesh_render_data_face_flag(mr, f, -1, data);
mesh_render_data_edge_flag(mr, l_iter->e, data);
mesh_render_data_vert_flag(mr, l_iter->v, data);
} while ((l_iter = l_iter->next) != l_first);
}
static void extract_edit_data_iter_poly_mesh(const MeshRenderData *mr,
const MPoly *mp,
const int mp_index,
void *_data)
{
EditLoopData *vbo_data = *(EditLoopData **)_data;
const MLoop *mloop = mr->mloop;
const int ml_index_end = mp->loopstart + mp->totloop;
for (int ml_index = mp->loopstart; ml_index < ml_index_end; ml_index += 1) {
const MLoop *ml = &mloop[ml_index];
EditLoopData *data = vbo_data + ml_index;
memset(data, 0x0, sizeof(*data));
BMFace *efa = bm_original_face_get(mr, mp_index);
BMEdge *eed = bm_original_edge_get(mr, ml->e);
BMVert *eve = bm_original_vert_get(mr, ml->v);
if (efa) {
mesh_render_data_face_flag(mr, efa, -1, data);
}
if (eed) {
mesh_render_data_edge_flag(mr, eed, data);
}
if (eve) {
mesh_render_data_vert_flag(mr, eve, data);
}
}
}
static void extract_edit_data_iter_ledge_bm(const MeshRenderData *mr,
const BMEdge *eed,
const int ledge_index,
void *_data)
{
EditLoopData *vbo_data = *(EditLoopData **)_data;
EditLoopData *data = vbo_data + mr->loop_len + (ledge_index * 2);
memset(data, 0x0, sizeof(*data) * 2);
mesh_render_data_edge_flag(mr, eed, &data[0]);
data[1] = data[0];
mesh_render_data_vert_flag(mr, eed->v1, &data[0]);
mesh_render_data_vert_flag(mr, eed->v2, &data[1]);
}
static void extract_edit_data_iter_ledge_mesh(const MeshRenderData *mr,
const MEdge *med,
const int ledge_index,
void *_data)
{
EditLoopData *vbo_data = *(EditLoopData **)_data;
EditLoopData *data = vbo_data + mr->loop_len + ledge_index * 2;
memset(data, 0x0, sizeof(*data) * 2);
const int e_index = mr->ledges[ledge_index];
BMEdge *eed = bm_original_edge_get(mr, e_index);
BMVert *eve1 = bm_original_vert_get(mr, med->v1);
BMVert *eve2 = bm_original_vert_get(mr, med->v2);
if (eed) {
mesh_render_data_edge_flag(mr, eed, &data[0]);
data[1] = data[0];
}
if (eve1) {
mesh_render_data_vert_flag(mr, eve1, &data[0]);
}
if (eve2) {
mesh_render_data_vert_flag(mr, eve2, &data[1]);
}
}
static void extract_edit_data_iter_lvert_bm(const MeshRenderData *mr,
const BMVert *eve,
const int lvert_index,
void *_data)
{
EditLoopData *vbo_data = *(EditLoopData **)_data;
const int offset = mr->loop_len + (mr->edge_loose_len * 2);
EditLoopData *data = vbo_data + offset + lvert_index;
memset(data, 0x0, sizeof(*data));
mesh_render_data_vert_flag(mr, eve, data);
}
static void extract_edit_data_iter_lvert_mesh(const MeshRenderData *mr,
const MVert *UNUSED(mv),
const int lvert_index,
void *_data)
{
EditLoopData *vbo_data = *(EditLoopData **)_data;
const int offset = mr->loop_len + (mr->edge_loose_len * 2);
EditLoopData *data = vbo_data + offset + lvert_index;
memset(data, 0x0, sizeof(*data));
const int v_index = mr->lverts[lvert_index];
BMVert *eve = bm_original_vert_get(mr, v_index);
if (eve) {
mesh_render_data_vert_flag(mr, eve, data);
}
}
static void extract_edit_data_init_subdiv(const DRWSubdivCache *subdiv_cache,
const MeshRenderData *mr,
MeshBatchCache *UNUSED(cache),
void *buf,
void *data)
{
GPUVertBuf *vbo = static_cast<GPUVertBuf *>(buf);
GPU_vertbuf_init_with_format(vbo, get_edit_data_format());
GPU_vertbuf_data_alloc(vbo, subdiv_cache->num_subdiv_loops + mr->loop_loose_len);
EditLoopData *vbo_data = (EditLoopData *)GPU_vertbuf_get_data(vbo);
*(EditLoopData **)data = vbo_data;
}
static void extract_edit_data_iter_subdiv_bm(const DRWSubdivCache *subdiv_cache,
const MeshRenderData *mr,
void *_data,
uint subdiv_quad_index,
const BMFace *coarse_quad)
{
EditLoopData *vbo_data = *(EditLoopData **)_data;
int *subdiv_loop_vert_index = (int *)GPU_vertbuf_get_data(subdiv_cache->verts_orig_index);
int *subdiv_loop_edge_index = (int *)GPU_vertbuf_get_data(subdiv_cache->edges_orig_index);
uint start_loop_idx = subdiv_quad_index * 4;
uint end_loop_idx = (subdiv_quad_index + 1) * 4;
for (uint i = start_loop_idx; i < end_loop_idx; i++) {
const int vert_origindex = subdiv_loop_vert_index[i];
const int edge_origindex = subdiv_loop_edge_index[i];
EditLoopData *edit_loop_data = &vbo_data[i];
memset(edit_loop_data, 0, sizeof(EditLoopData));
if (vert_origindex != -1) {
const BMVert *eve = mr->v_origindex ? bm_original_vert_get(mr, vert_origindex) :
BM_vert_at_index(mr->bm, vert_origindex);
if (eve) {
mesh_render_data_vert_flag(mr, eve, edit_loop_data);
}
}
if (edge_origindex != -1) {
/* NOTE: #subdiv_loop_edge_index already has the origindex layer baked in. */
const BMEdge *eed = BM_edge_at_index(mr->bm, edge_origindex);
mesh_render_data_edge_flag(mr, eed, edit_loop_data);
}
/* coarse_quad can be null when called by the mesh iteration below. */
if (coarse_quad) {
/* The -1 parameter is for edit_uvs, which we don't do here. */
mesh_render_data_face_flag(mr, coarse_quad, -1, edit_loop_data);
}
}
}
static void extract_edit_data_iter_subdiv_mesh(const DRWSubdivCache *subdiv_cache,
const MeshRenderData *mr,
void *_data,
uint subdiv_quad_index,
const MPoly *coarse_quad)
{
const int coarse_quad_index = static_cast<int>(coarse_quad - mr->mpoly);
BMFace *coarse_quad_bm = bm_original_face_get(mr, coarse_quad_index);
extract_edit_data_iter_subdiv_bm(subdiv_cache, mr, _data, subdiv_quad_index, coarse_quad_bm);
}
static void extract_edit_data_loose_geom_subdiv(const DRWSubdivCache *subdiv_cache,
const MeshRenderData *mr,
const MeshExtractLooseGeom *loose_geom,
void *UNUSED(buffer),
void *_data)
{
if (loose_geom->edge_len == 0) {
return;
}
EditLoopData *vbo_data = *(EditLoopData **)_data;
for (int ledge_index = 0; ledge_index < loose_geom->edge_len; ledge_index++) {
const int offset = subdiv_cache->num_subdiv_loops + ledge_index * 2;
EditLoopData *data = &vbo_data[offset];
memset(data, 0, sizeof(EditLoopData));
const int edge_index = loose_geom->edges[ledge_index];
BMEdge *eed = mr->e_origindex ? bm_original_edge_get(mr, edge_index) :
BM_edge_at_index(mr->bm, edge_index);
mesh_render_data_edge_flag(mr, eed, &data[0]);
data[1] = data[0];
mesh_render_data_vert_flag(mr, eed->v1, &data[0]);
mesh_render_data_vert_flag(mr, eed->v2, &data[1]);
}
}
constexpr MeshExtract create_extractor_edit_data()
{
MeshExtract extractor = {nullptr};
extractor.init = extract_edit_data_init;
extractor.iter_poly_bm = extract_edit_data_iter_poly_bm;
extractor.iter_poly_mesh = extract_edit_data_iter_poly_mesh;
extractor.iter_ledge_bm = extract_edit_data_iter_ledge_bm;
extractor.iter_ledge_mesh = extract_edit_data_iter_ledge_mesh;
extractor.iter_lvert_bm = extract_edit_data_iter_lvert_bm;
extractor.iter_lvert_mesh = extract_edit_data_iter_lvert_mesh;
extractor.init_subdiv = extract_edit_data_init_subdiv;
extractor.iter_subdiv_bm = extract_edit_data_iter_subdiv_bm;
extractor.iter_subdiv_mesh = extract_edit_data_iter_subdiv_mesh;
extractor.iter_loose_geom_subdiv = extract_edit_data_loose_geom_subdiv;
extractor.data_type = MR_DATA_NONE;
extractor.data_size = sizeof(EditLoopData *);
extractor.use_threading = true;
extractor.mesh_buffer_offset = offsetof(MeshBufferList, vbo.edit_data);
return extractor;
}
/** \} */
} // namespace blender::draw
extern "C" {
const MeshExtract extract_edit_data = blender::draw::create_extractor_edit_data();
}
|