Blender V5.3
draw_cache_impl_lattice.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2017 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "MEM_guardedalloc.h"
12
13#include "BLI_utildefines.hh"
14
15#include "DNA_curve_types.h"
16#include "DNA_lattice_types.h"
17#include "DNA_meshdata_types.h"
18
19#include "BKE_deform.hh"
20#include "BKE_lattice.hh"
21
22#include "GPU_batch.hh"
23
24#include "draw_cache_impl.hh" /* own include */
25
26#define SELECT 1
27
28namespace blender::draw {
29
30static void lattice_batch_cache_clear(Lattice *lt);
31
32/* ---------------------------------------------------------------------- */
33/* Lattice Interface, direct access to basic data. */
34
35static int vert_len_calc(int u, int v, int w)
36{
37 if (u <= 0 || v <= 0 || w <= 0) {
38 return 0;
39 }
40 return u * v * w;
41}
42
43static int edge_len_calc(int u, int v, int w)
44{
45 if (u <= 0 || v <= 0 || w <= 0) {
46 return 0;
47 }
48 return (((((u - 1) * v) + ((v - 1) * u)) * w) + ((w - 1) * (u * v)));
49}
50
52{
53 if (lt->editlatt) {
54 lt = lt->editlatt->latt;
55 }
56
57 const int u = lt->pntsu;
58 const int v = lt->pntsv;
59 const int w = lt->pntsw;
60
61 if ((lt->flag & LT_OUTSIDE) == 0) {
62 return vert_len_calc(u, v, w);
63 }
64
65 /* TODO: remove internal coords. */
66 return vert_len_calc(u, v, w);
67}
68
70{
71 if (lt->editlatt) {
72 lt = lt->editlatt->latt;
73 }
74
75 const int u = lt->pntsu;
76 const int v = lt->pntsv;
77 const int w = lt->pntsw;
78
79 if ((lt->flag & LT_OUTSIDE) == 0) {
80 return edge_len_calc(u, v, w);
81 }
82
83 /* TODO: remove internal coords. */
84 return edge_len_calc(u, v, w);
85}
86
87/* ---------------------------------------------------------------------- */
88/* Lattice Interface, indirect, partially cached access to complex data. */
89
91 int types;
92
95
96 struct {
97 int u_len, v_len, w_len;
100
103
104 int actbp;
105
107};
108
109enum {
113};
114
116{
118 rdata->types = types;
119
120 if (lt->editlatt) {
121 EditLatt *editlatt = lt->editlatt;
122 lt = editlatt->latt;
123
124 rdata->edit_latt = editlatt;
125
126 rdata->dvert = lt->dvert;
127
128 if (types & (LR_DATATYPE_VERT)) {
130 }
131 if (types & (LR_DATATYPE_EDGE)) {
133 }
134 if (types & LR_DATATYPE_OVERLAY) {
135 rdata->actbp = lt->actbp;
136 }
137 }
138 else {
139 rdata->dvert = nullptr;
140
141 if (types & (LR_DATATYPE_VERT)) {
143 }
144 if (types & (LR_DATATYPE_EDGE)) {
146 /* No edge data. */
147 }
148 }
149
150 rdata->bp = lt->def;
151
152 rdata->dims.u_len = lt->pntsu;
153 rdata->dims.v_len = lt->pntsv;
154 rdata->dims.w_len = lt->pntsw;
155
156 rdata->show_only_outside = (lt->flag & LT_OUTSIDE) != 0;
157 rdata->actbp = lt->actbp;
158
159 return rdata;
160}
161
163{
164#if 0
165 if (rdata->loose_verts) {
166 MEM_delete(rdata->loose_verts);
167 }
168#endif
169 MEM_delete(rdata);
170}
171
173{
175 return rdata->vert_len;
176}
177
179{
181 return rdata->edge_len;
182}
183
185 const int vert_idx)
186{
188 return &rdata->bp[vert_idx];
189}
190
191/* ---------------------------------------------------------------------- */
192/* Lattice gpu::Batch Cache */
193
197
198 gpu::Batch *all_verts;
199 gpu::Batch *all_edges;
200
201 gpu::Batch *overlay_verts;
202
203 /* settings to determine if cache is invalid */
205
206 struct {
207 int u_len, v_len, w_len;
210
212};
213
214/* gpu::Batch cache management. */
215
217{
218 LatticeBatchCache *cache = lt->batch_cache;
219
220 if (cache == nullptr) {
221 return false;
222 }
223
224 if (cache->is_editmode != (lt->editlatt != nullptr)) {
225 return false;
226 }
227
228 if (cache->is_dirty) {
229 return false;
230 }
231
232 if ((cache->dims.u_len != lt->pntsu) || (cache->dims.v_len != lt->pntsv) ||
233 (cache->dims.w_len != lt->pntsw) ||
234 (cache->show_only_outside != ((lt->flag & LT_OUTSIDE) != 0)))
235 {
236 return false;
237 }
238
239 return true;
240}
241
243{
244 LatticeBatchCache *cache = lt->batch_cache;
245
246 if (!cache) {
247 lt->batch_cache = cache = MEM_new_zeroed<LatticeBatchCache>(__func__);
248 }
249 else {
250 memset(cache, 0, sizeof(*cache));
251 }
252
253 cache->dims.u_len = lt->pntsu;
254 cache->dims.v_len = lt->pntsv;
255 cache->dims.w_len = lt->pntsw;
256 cache->show_only_outside = (lt->flag & LT_OUTSIDE) != 0;
257
258 cache->is_editmode = lt->editlatt != nullptr;
259
260 cache->is_dirty = false;
261}
262
270
272{
273 return lt->batch_cache;
274}
275
277{
278 LatticeBatchCache *cache = lt->batch_cache;
279 if (cache == nullptr) {
280 return;
281 }
282 switch (mode) {
284 cache->is_dirty = true;
285 break;
287 /* TODO: Separate Flag VBO. */
289 break;
290 default:
291 BLI_assert(0);
292 }
293}
294
296{
297 LatticeBatchCache *cache = lt->batch_cache;
298 if (!cache) {
299 return;
300 }
301
305
308}
309
311{
313 MEM_SAFE_DELETE(lt->batch_cache);
314}
315
316/* gpu::Batch cache usage. */
318 LatticeBatchCache *cache,
319 bool use_weight,
320 const int actdef)
321{
323
324 if (cache->pos == nullptr) {
325 GPUVertFormat format = {0};
326 struct {
327 uint pos, col;
328 } attr_id;
329
330 attr_id.pos = GPU_vertformat_attr_add(&format, "pos", gpu::VertAttrType::SFLOAT_32_32_32);
331 if (use_weight) {
332 attr_id.col = GPU_vertformat_attr_add(&format, "weight", gpu::VertAttrType::SFLOAT_32);
333 }
334
335 const int vert_len = lattice_render_data_verts_len_get(rdata);
336
337 cache->pos = GPU_vertbuf_create_with_format(format);
338 GPU_vertbuf_data_alloc(*cache->pos, vert_len);
339 for (int i = 0; i < vert_len; i++) {
340 const BPoint *bp = lattice_render_data_vert_bpoint(rdata, i);
341 GPU_vertbuf_attr_set(cache->pos, attr_id.pos, i, bp->vec);
342
343 if (use_weight) {
344 const float no_active_weight = 666.0f;
345 float weight = (actdef > -1) ? BKE_defvert_find_weight(rdata->dvert + i, actdef) :
346 no_active_weight;
347 GPU_vertbuf_attr_set(cache->pos, attr_id.col, i, &weight);
348 }
349 }
350 }
351
352 return cache->pos;
353}
354
356 LatticeBatchCache *cache)
357{
359
360 if (cache->edges == nullptr) {
361 const int vert_len = lattice_render_data_verts_len_get(rdata);
362 const int edge_len = lattice_render_data_edges_len_get(rdata);
363
364 GPUIndexBufBuilder builder;
365 GPU_indexbuf_init(&builder, GPU_PRIM_LINES, edge_len, vert_len);
367 /* The buffer is allocated with **all** edges (see #lattice_render_edges_len_get()), but with
368 * the LT_OUTSIDE flag not all are drawn. So fill those gaps with zeros to hide redundant
369 * edges. */
370 data.fill(uint2(0));
371 int line_index = 0;
372
373#define LATT_INDEX(u, v, w) ((((w) * rdata->dims.v_len + (v)) * rdata->dims.u_len) + (u))
374
375 for (int w = 0; w < rdata->dims.w_len; w++) {
376 int wxt = ELEM(w, 0, rdata->dims.w_len - 1);
377 for (int v = 0; v < rdata->dims.v_len; v++) {
378 int vxt = ELEM(v, 0, rdata->dims.v_len - 1);
379 for (int u = 0; u < rdata->dims.u_len; u++) {
380 int uxt = ELEM(u, 0, rdata->dims.u_len - 1);
381
382 if (w && ((uxt || vxt) || !rdata->show_only_outside)) {
383 data[line_index++] = uint2(LATT_INDEX(u, v, w - 1), LATT_INDEX(u, v, w));
384 }
385 if (v && ((uxt || wxt) || !rdata->show_only_outside)) {
386 data[line_index++] = uint2(LATT_INDEX(u, v - 1, w), LATT_INDEX(u, v, w));
387 }
388 if (u && ((vxt || wxt) || !rdata->show_only_outside)) {
389 data[line_index++] = uint2(LATT_INDEX(u - 1, v, w), LATT_INDEX(u, v, w));
390 }
391 }
392 }
393 }
394
395#undef LATT_INDEX
396
397 if (rdata->show_only_outside) {
398 BLI_assert(line_index <= edge_len);
399 }
400 else {
401 BLI_assert(line_index == edge_len);
402 }
403
404 cache->edges = GPU_indexbuf_build_ex(&builder, 0, vert_len, false);
405 }
406
407 return cache->edges;
408}
409
411{
412 /* Since LR_DATATYPE_OVERLAY is slow to generate, generate them all at once */
414
417
418 if (cache->overlay_verts == nullptr) {
419 static struct {
420 uint pos, data;
421 } attr_id;
422 static const GPUVertFormat format = [&]() {
423 GPUVertFormat format{};
424 attr_id.pos = GPU_vertformat_attr_add(&format, "pos", gpu::VertAttrType::SFLOAT_32_32_32);
425 attr_id.data = GPU_vertformat_attr_add(&format, "data", gpu::VertAttrType::UINT_32);
426 return format;
427 }();
428
429 const int vert_len = lattice_render_data_verts_len_get(rdata);
430
431 gpu::VertBuf *vbo = GPU_vertbuf_create_with_format(format);
432 GPU_vertbuf_data_alloc(*vbo, vert_len);
433 for (int i = 0; i < vert_len; i++) {
434 const BPoint *bp = lattice_render_data_vert_bpoint(rdata, i);
435
436 uint32_t vflag = 0;
437 if (bp->f1 & SELECT) {
438 if (i == rdata->actbp) {
439 vflag |= VFLAG_VERT_ACTIVE;
440 }
441 else {
442 vflag |= VFLAG_VERT_SELECTED;
443 }
444 }
445
446 GPU_vertbuf_attr_set(vbo, attr_id.pos, i, bp->vec);
447 GPU_vertbuf_attr_set(vbo, attr_id.data, i, &vflag);
448 }
449
451 }
452
454}
455
456gpu::Batch *DRW_lattice_batch_cache_get_all_edges(Lattice *lt, bool use_weight, const int actdef)
457{
459
460 if (cache->all_edges == nullptr) {
461 /* create batch from Lattice */
463
466 lattice_batch_cache_get_pos(rdata, cache, use_weight, actdef),
467 lattice_batch_cache_get_edges(rdata, cache));
468
470 }
471
472 return cache->all_edges;
473}
474
476{
478
479 if (cache->all_verts == nullptr) {
481
483 GPU_PRIM_POINTS, lattice_batch_cache_get_pos(rdata, cache, false, -1), nullptr);
484
486 }
487
488 return cache->all_verts;
489}
490
492{
494
495 if (cache->overlay_verts == nullptr) {
497 }
498
499 return cache->overlay_verts;
500}
501
502} // namespace blender::draw
support for deformation groups and hooks.
#define SELECT
Definition BKE_mask.hh:19
#define GPU_batch_create(primitive_type, vertex_buf, index_buf)
Definition GPU_batch.hh:143
#define GPU_BATCH_DISCARD_SAFE(batch)
Definition GPU_batch.hh:199
#define GPU_INDEXBUF_DISCARD_SAFE(elem)
#define GPU_VERTBUF_DISCARD_SAFE(verts)
Read Blender memory allocation and freeing.
#define ELEM(...)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
MutableSpan< T > data()
CCL_NAMESPACE_BEGIN struct Options options
#define LATT_INDEX(u, v, w)
gpu::Batch * DRW_lattice_batch_cache_get_all_verts(Lattice *lt)
void DRW_lattice_batch_cache_validate(Lattice *lt)
static int edge_len_calc(int u, int v, int w)
gpu::Batch * DRW_lattice_batch_cache_get_all_edges(Lattice *lt, bool use_weight, int actdef)
void DRW_lattice_batch_cache_dirty_tag(Lattice *lt, int mode)
gpu::Batch * DRW_lattice_batch_cache_get_edit_verts(Lattice *lt)
void DRW_lattice_batch_cache_free(Lattice *lt)
static void lattice_batch_cache_init(Lattice *lt)
static void lattice_batch_cache_clear(Lattice *lt)
static gpu::IndexBuf * lattice_batch_cache_get_edges(LatticeRenderData *rdata, LatticeBatchCache *cache)
static int lattice_render_data_edges_len_get(const LatticeRenderData *rdata)
static const BPoint * lattice_render_data_vert_bpoint(const LatticeRenderData *rdata, const int vert_idx)
static LatticeBatchCache * lattice_batch_cache_get(Lattice *lt)
static int lattice_render_data_verts_len_get(const LatticeRenderData *rdata)
static LatticeRenderData * lattice_render_data_create(Lattice *lt, const int types)
static void lattice_batch_cache_create_overlay_batches(Lattice *lt)
static bool lattice_batch_cache_valid(Lattice *lt)
static void lattice_render_data_free(LatticeRenderData *rdata)
static gpu::VertBuf * lattice_batch_cache_get_pos(LatticeRenderData *rdata, LatticeBatchCache *cache, bool use_weight, const int actdef)
static int lattice_render_verts_len_get(Lattice *lt)
static int lattice_render_edges_len_get(Lattice *lt)
static int vert_len_calc(int u, int v, int w)
VecBase< uint32_t, 2 > uint2
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
gpu::Batch * GPU_batch_create_ex(GPUPrimType primitive_type, gpu::VertBuf *vertex_buf, gpu::IndexBuf *index_buf, GPUBatchFlag owns_flag)
Definition gpu_batch.cc:53
void GPU_indexbuf_init(GPUIndexBufBuilder *, GPUPrimType, uint prim_len, uint vertex_len)
unsigned int uint
Definition BKE_text.h:55
@ BKE_LATTICE_BATCH_DIRTY_ALL
@ BKE_LATTICE_BATCH_DIRTY_SELECT
ATTR_WARN_UNUSED_RESULT const BMVert * v
float BKE_defvert_find_weight(const MDeformVert *dvert, int defgroup)
Definition deform.cc:779
unsigned int uint32_t
Definition BKE_text.h:81
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
BMesh const char void * data
void GPU_vertbuf_attr_set(gpu::VertBuf *, uint a_idx, uint v_idx, const void *data)
MutableSpan< uint32_t > GPU_indexbuf_get_data(GPUIndexBufBuilder *)
@ GPU_BATCH_OWNS_VBO
Definition GPU_batch.hh:44
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len)
gpu::IndexBuf * GPU_indexbuf_build_ex(GPUIndexBufBuilder *builder, uint index_min, uint index_max, bool uses_restart_indices)
struct Lattice * latt
struct EditLatt * editlatt
draw::LatticeBatchCache * batch_cache
struct MDeformVert * dvert
struct BPoint * def
struct blender::draw::LatticeBatchCache::@154210265044357056026136226074313224321012116221 dims
struct blender::draw::LatticeRenderData::@212026052261026166321007063232025254241217305361 dims