Blender V5.3
extract_mesh_vbo_weights.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 "BLI_array_utils.hh"
12
13#include "BKE_deform.hh"
14
15#include "draw_subdivision.hh"
16#include "extract_mesh.hh"
17
18namespace blender::draw {
19
20static float evaluate_vertex_weight(const MDeformVert *dvert, const DRW_MeshWeightState *wstate)
21{
22 /* Error state. */
23 if ((wstate->defgroup_active < 0) && (wstate->defgroup_len > 0)) {
24 return -2.0f;
25 }
26
27 float input = 0.0f;
29 /* Multi-Paint feature */
30 bool is_normalized = (wstate->flags & (DRW_MESH_WEIGHT_STATE_AUTO_NORMALIZE |
33 wstate->defgroup_len,
34 wstate->defgroup_sel,
35 wstate->defgroup_sel_count,
36 is_normalized);
37 /* make it black if the selected groups have no weight on a vertex */
38 if (input == 0.0f) {
39 return -1.0f;
40 }
41 }
42 else {
43 /* default, non tricky behavior */
44 input = BKE_defvert_find_weight(dvert, wstate->defgroup_active);
45
46 if (input == 0.0f) {
47 switch (wstate->alert_mode) {
49 return -1.0f;
50 break;
52 if (BKE_defvert_is_weight_zero(dvert, wstate->defgroup_len)) {
53 return -1.0f;
54 }
55 break;
56 }
57 }
58 }
59
60 /* Lock-Relative: display the fraction of current weight vs total unlocked weight. */
63 input, dvert, wstate->defgroup_len, wstate->defgroup_locked, wstate->defgroup_unlocked);
64 }
65
66 CLAMP(input, 0.0f, 1.0f);
67 return input;
68}
69
71 const DRW_MeshWeightState &weight_state,
72 MutableSpan<float> vbo_data)
73{
74 const Mesh &mesh = *mr.mesh;
75 const Span<MDeformVert> dverts = mesh.deform_verts();
76 if (dverts.is_empty()) {
77 vbo_data.fill(weight_state.alert_mode == OB_DRAW_GROUPUSER_NONE ? 0.0f : -1.0f);
78 return;
79 }
80
81 threading::parallel_for(vbo_data.index_range(), 1024, [&](const IndexRange range) {
82 for (const int vert : range) {
83 vbo_data[vert] = evaluate_vertex_weight(&dverts[vert], &weight_state);
84 }
85 });
86}
87
88static void extract_weights_bm(const MeshRenderData &mr,
89 const DRW_MeshWeightState &weight_state,
90 MutableSpan<float> vbo_data)
91{
92 const BMesh &bm = *mr.bm;
93 const int offset = CustomData_get_offset(&bm.vdata, CD_MDEFORMVERT);
94 if (offset == -1) {
95 vbo_data.fill(weight_state.alert_mode == OB_DRAW_GROUPUSER_NONE ? 0.0f : -1.0f);
96 return;
97 }
98
99 threading::parallel_for(IndexRange(bm.totvert), 2048, [&](const IndexRange range) {
100 for (const int vert_index : range) {
101 const BMVert &vert = *BM_vert_at_index(&const_cast<BMesh &>(bm), vert_index);
102 vbo_data[vert_index] = evaluate_vertex_weight(
103 static_cast<const MDeformVert *>(BM_ELEM_CD_GET_VOID_P(&vert, offset)), &weight_state);
104 }
105 });
106}
107
109 const MeshBatchCache &batch_cache,
110 MeshBufferCache &cache)
111{
112 static GPUVertFormat format = GPU_vertformat_from_attribute("weight",
113 gpu::VertAttrType::SFLOAT_32);
114
115 gpu::VertBufPtr vert_vbo = gpu::VertBufPtr(GPU_vertbuf_create_with_format(format));
116 GPU_vertbuf_data_alloc(*vert_vbo, mr.verts_num);
117 MutableSpan<float> vbo_data = vert_vbo->data<float>();
118
119 const DRW_MeshWeightState &weight_state = batch_cache.weight_state;
120 if (weight_state.defgroup_active == -1) {
121 vbo_data.fill(weight_state.alert_mode == OB_DRAW_GROUPUSER_NONE ? 0.0f : -1.0f);
122 }
123 else {
125 extract_weights_mesh(mr, weight_state, vbo_data);
126 }
127 else {
128 extract_weights_bm(mr, weight_state, vbo_data);
129 }
130 }
131
133
134 gather_vert_to_corner_gpu(mr, cache, *vert_vbo, *vbo);
135
136 return vbo;
137}
138
140 const DRWSubdivCache &subdiv_cache,
141 const MeshBatchCache &batch_cache,
142 MeshBufferCache &cache)
143{
144 static GPUVertFormat format = GPU_vertformat_from_attribute("weight",
145 gpu::VertAttrType::SFLOAT_32);
146
148 GPU_vertbuf_create_on_device(format, subdiv_cache.num_subdiv_loops));
149
150 gpu::VertBufPtr coarse_weights = extract_weights(mr, batch_cache, cache);
151 draw_subdiv_interp_custom_data(subdiv_cache, *coarse_weights, *vbo, GPU_COMP_F32, 1, 0);
152 return vbo;
153}
154
155} // namespace blender::draw
support for deformation groups and hooks.
#define CLAMP(a, b, c)
constexpr T * data() const
Definition BLI_span.hh:545
constexpr void fill(const T &value) const
Definition BLI_span.hh:523
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
constexpr bool is_empty() const
Definition BLI_span.hh:264
Extraction of Mesh data into VBO to feed to GPU.
void draw_subdiv_interp_custom_data(const DRWSubdivCache &cache, gpu::VertBuf &src_data, gpu::VertBuf &dst_data, GPUVertCompType comp_type, int dimensions, int dst_offset)
gpu::VertBufPtr extract_weights_subdiv(const MeshRenderData &mr, const DRWSubdivCache &subdiv_cache, const MeshBatchCache &batch_cache, MeshBufferCache &cache)
static float evaluate_vertex_weight(const MDeformVert *dvert, const DRW_MeshWeightState *wstate)
static void extract_weights_bm(const MeshRenderData &mr, const DRW_MeshWeightState &weight_state, MutableSpan< float > vbo_data)
gpu::VertBufPtr extract_weights(const MeshRenderData &mr, const MeshBatchCache &batch_cache, MeshBufferCache &cache)
void gather_vert_to_corner_gpu(const MeshRenderData &mr, MeshBufferCache &cache, gpu::VertBuf &src, gpu::VertBuf &dst)
static void extract_weights_mesh(const MeshRenderData &mr, const DRW_MeshWeightState &weight_state, MutableSpan< float > vbo_data)
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
int CustomData_get_offset(const CustomData *data, eCustomDataType type)
bool BKE_defvert_is_weight_zero(const MDeformVert *dvert, int defgroup_tot)
Definition deform.cc:941
gpu::VertBuf * GPU_vertbuf_create_on_device(const GPUVertFormat &format, uint v_len)
GPUVertFormat GPU_vertformat_from_attribute(StringRef name, gpu::VertAttrType type)
float BKE_defvert_find_weight(const MDeformVert *dvert, int defgroup)
Definition deform.cc:779
float BKE_defvert_lock_relative_weight(float weight, const MDeformVert *dv, int defbase_num, const bool *defbase_locked, const bool *defbase_unlocked)
Definition deform.cc:1022
void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len)
@ OB_DRAW_GROUPUSER_ACTIVE
float BKE_defvert_multipaint_collective_weight(const MDeformVert *dv, int defbase_num, const bool *defbase_sel, int defbase_sel_num, bool is_normalized)
Definition deform.cc:977
CustomData vdata