Blender V5.3
extract_mesh_vbo_attributes.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_string.hh"
10
11#include "BKE_attribute.hh"
13#include "BKE_attribute_math.hh"
14#include "BKE_mesh.hh"
15
16#include "attribute_convert.hh"
17#include "draw_attributes.hh"
18#include "draw_subdivision.hh"
19#include "extract_mesh.hh"
20
21#include "GPU_vertex_buffer.hh"
22
23namespace blender::draw {
24
25/* ---------------------------------------------------------------------- */
28
30 const StringRef name,
31 const bke::AttrType type)
32{
33 char attr_name[32], attr_safe_name[GPU_MAX_SAFE_ATTR_NAME];
34 GPU_vertformat_safe_attr_name(name, attr_safe_name, GPU_MAX_SAFE_ATTR_NAME);
35 /* Attributes use auto-name. */
36 SNPRINTF(attr_name, "a%s", attr_safe_name);
37
38 GPUVertFormat format = init_format_for_attribute(type, attr_name);
40
41 if (mr.active_color_name && name == mr.active_color_name) {
42 GPU_vertformat_alias_add(&format, "ac");
43 }
44 if (mr.default_color_name && name == mr.default_color_name) {
45 GPU_vertformat_alias_add(&format, "c");
46 }
47 return format;
48}
49
50static gpu::VertBufPtr vbo_create(const GPUVertFormat &format, const int size)
51{
52 gpu::VertBufPtr vbo = gpu::VertBufPtr(GPU_vertbuf_create_with_format(format));
53 GPU_vertbuf_data_alloc(*vbo, size);
54 return vbo;
55}
56
57static int domain_size(const MeshRenderData &mr, const bke::AttrDomain domain)
58{
59 switch (domain) {
61 return mr.verts_num;
63 return mr.edges_num;
65 return mr.faces_num;
67 return mr.corners_num;
68 default:
70 return 0;
71 }
72}
73
79 MeshBufferCache &cache,
80 const bke::AttrDomain domain,
81 gpu::VertBuf &src,
82 const GPUVertFormat &format)
83{
85 switch (domain) {
87 gather_vert_to_corner_gpu(mr, cache, src, *vbo);
88 break;
90 gather_edge_to_corner_gpu(mr, cache, src, *vbo);
91 break;
93 scatter_face_to_corner_gpu(mr, cache, src, *vbo);
94 break;
95 default:
97 }
98 return vbo;
99}
100
102static void extract_data_bmesh(const MeshRenderData &mr,
103 const BMDataLayerLookup &attr,
104 gpu::VertBuf &vbo)
105{
106 BMesh &bm = *mr.bm;
107 bke::attribute_math::to_static_type(attr.type, [&]<typename T>() {
108 using Converter = AttributeConverter<T>;
109 using VBOType = typename Converter::VBOType;
110 if constexpr (!std::is_void_v<VBOType>) {
111 MutableSpan data = vbo.data<VBOType>();
112 const auto convert = [&](const BMHeader &head) {
113 return Converter::convert(*static_cast<const T *>(POINTER_OFFSET(head.data, attr.offset)));
114 };
115 switch (attr.domain) {
116 case bke::AttrDomain::Point:
117 threading::parallel_for(IndexRange(bm.totvert), 4096, [&](const IndexRange range) {
118 for (const int i : range) {
119 data[i] = convert(BM_vert_at_index(&bm, i)->head);
120 }
121 });
122 break;
123 case bke::AttrDomain::Edge:
124 threading::parallel_for(IndexRange(bm.totedge), 4096, [&](const IndexRange range) {
125 for (const int i : range) {
126 data[i] = convert(BM_edge_at_index(&bm, i)->head);
127 }
128 });
129 break;
130 case bke::AttrDomain::Face:
131 threading::parallel_for(IndexRange(bm.totface), 4096, [&](const IndexRange range) {
132 for (const int i : range) {
133 data[i] = convert(BM_face_at_index(&bm, i)->head);
134 }
135 });
136 break;
137 case bke::AttrDomain::Corner:
138 threading::parallel_for(IndexRange(bm.totface), 2048, [&](const IndexRange range) {
139 for (const int face_index : range) {
140 const BMFace &face = *BM_face_at_index(&bm, face_index);
141 const BMLoop *loop = BM_FACE_FIRST_LOOP(&face);
142 for ([[maybe_unused]] const int i : IndexRange(face.len)) {
143 data[BM_elem_index_get(loop)] = convert(loop->head);
144 loop = loop->next;
145 }
146 }
147 });
148 break;
149 default:
150 BLI_assert_unreachable();
151 }
152 }
153 });
154}
155
157 MeshBufferCache &cache,
158 const BMDataLayerLookup &attr,
159 const GPUVertFormat &format)
160{
161 gpu::VertBufPtr src = vbo_create(format, domain_size(mr, attr.domain));
162 extract_data_bmesh(mr, attr, *src);
163 if (attr.domain == bke::AttrDomain::Corner) {
164 return src;
165 }
166 return copy_to_corners_gpu(mr, cache, attr.domain, *src, format);
167}
168
170 MeshBufferCache &cache,
171 const bke::GAttributeReader &attr,
172 const GPUVertFormat &format)
173{
174 if (attr.varray.is_single()) {
175 gpu::VertBufPtr vbo = vbo_create(format, mr.corners_num);
176 bke::attribute_math::to_static_type(attr.varray.type(), [&]<typename T>() {
177 const VArray<T> &src = attr.varray.typed<T>();
178 if constexpr (!std::is_void_v<typename AttributeConverter<T>::VBOType>) {
179 using Converter = AttributeConverter<T>;
180 using VBOType = typename Converter::VBOType;
181 MutableSpan data = vbo->data<VBOType>();
182 data.fill(Converter::convert(src.get_internal_single()));
183 }
184 });
185 return vbo;
186 }
187 gpu::VertBufPtr src = vbo_create(format, domain_size(mr, attr.domain));
189 if (attr.domain == bke::AttrDomain::Corner) {
190 return src;
191 }
192 return copy_to_corners_gpu(mr, cache, attr.domain, *src, format);
193}
194
196 MeshBufferCache &cache,
197 const StringRef name)
198{
200 const BMDataLayerLookup attr = BM_data_layer_lookup(*mr.bm, name);
201 if (!attr) {
202 return {};
203 }
204 return extract_attribute_data(mr, cache, attr, attribute_vbo_format(mr, name, attr.type));
205 }
206 const bke::AttributeAccessor attributes = mr.mesh->attributes();
207 const bke::GAttributeReader attr = attributes.lookup(name);
208 if (!attr) {
209 return {};
210 }
212 return extract_attribute_data(mr, cache, attr, attribute_vbo_format(mr, name, type));
213}
214
216 MeshBufferCache &cache,
217 const DRWSubdivCache &subdiv_cache,
218 const StringRef name)
219{
220 BLI_assert(mr.corners_num == subdiv_cache.mesh->corners_num);
221
222 /* Extract the attribute on the coarse face corners, to be interpolated to the subdivided face
223 * corners. The compute shader only expects floats. */
224 gpu::VertBufPtr coarse_vbo;
225 bke::AttrType type;
227 const BMDataLayerLookup attr = BM_data_layer_lookup(*mr.bm, name);
228 if (!attr) {
229 return {};
230 }
231 type = attr.type;
232 coarse_vbo = extract_attribute_data(mr, cache, attr, init_format_for_attribute(type, "data"));
233 }
234 else {
235 const bke::AttributeAccessor attributes = mr.mesh->attributes();
236 const bke::GAttributeReader attr = attributes.lookup(name);
237 if (!attr) {
238 return {};
239 }
241 coarse_vbo = extract_attribute_data(mr, cache, attr, init_format_for_attribute(type, "data"));
242 }
243
245 attribute_vbo_format(mr, name, type), subdiv_cache.num_subdiv_loops));
246
247 bke::attribute_math::to_static_type(type, [&]<typename T>() {
249 if constexpr (!std::is_void_v<typename Converter::VBOType>) {
251 *coarse_vbo,
252 *vbo,
253 Converter::gpu_component_type,
254 Converter::gpu_component_len,
255 0);
256 }
257 });
258
259 return vbo;
260}
261
263{
264 static const GPUVertFormat format = GPU_vertformat_from_attribute(
265 "attribute_value", gpu::VertAttrType::SFLOAT_32_32_32_32);
266 const StringRefNull attr_name = ".viewer";
267 const bke::AttributeAccessor attributes = mr.mesh->attributes();
268 bke::GAttributeReader attr = attributes.lookup(
269 attr_name, std::nullopt, bke::AttrType::ColorFloat);
270
271 /* The GPU edge to corner gather only uses one edge per corner. Interpolate on the CPU instead,
272 * mixing both face edges adjacent to each corner, so the value shows along the edge. */
273 if (attr && attr.domain == bke::AttrDomain::Edge) {
274 attr = attributes.lookup(attr_name, bke::AttrDomain::Corner, bke::AttrType::ColorFloat);
275 }
276
277 return extract_attribute_data(mr, cache, attr, format);
278}
279
281
282} // namespace blender::draw
#define SNPRINTF(dst, format,...)
#define BLI_assert_unreachable()
GAttributeReader lookup(const StringRef name) const
Utilities for rendering attributes.
Extraction of Mesh data into VBO to feed to GPU.
#define T
void to_static_type(const CPPType &cpp_type, Fn &&fn)
AttrType cpp_type_to_attribute_type(const CPPType &type)
void scatter_face_to_corner_gpu(const MeshRenderData &mr, MeshBufferCache &cache, gpu::VertBuf &src, gpu::VertBuf &dst)
static gpu::VertBufPtr copy_to_corners_gpu(const MeshRenderData &mr, MeshBufferCache &cache, const bke::AttrDomain domain, gpu::VertBuf &src, const GPUVertFormat &format)
static gpu::VertBufPtr vbo_create(const GPUVertFormat &format, const int size)
gpu::VertBufPtr extract_attribute_subdiv(const MeshRenderData &mr, MeshBufferCache &cache, const DRWSubdivCache &subdiv_cache, StringRef name)
static void extract_data_bmesh(const MeshRenderData &mr, const BMDataLayerLookup &attr, gpu::VertBuf &vbo)
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)
static gpu::VertBufPtr extract_attribute_data(const MeshRenderData &mr, MeshBufferCache &cache, const BMDataLayerLookup &attr, const GPUVertFormat &format)
void vertbuf_data_extract_direct(const GSpan attribute, gpu::VertBuf &vbo)
static GPUVertFormat attribute_vbo_format(const MeshRenderData &mr, const StringRef name, const bke::AttrType type)
void gather_edge_to_corner_gpu(const MeshRenderData &mr, MeshBufferCache &cache, gpu::VertBuf &src, gpu::VertBuf &dst)
GPUVertFormat init_format_for_attribute(const bke::AttrType data_type, const StringRef vbo_name)
void gather_vert_to_corner_gpu(const MeshRenderData &mr, MeshBufferCache &cache, gpu::VertBuf &src, gpu::VertBuf &dst)
gpu::VertBufPtr extract_attr_viewer(const MeshRenderData &mr, MeshBufferCache &cache)
gpu::VertBufPtr extract_attribute(const MeshRenderData &mr, MeshBufferCache &cache, StringRef name)
static int domain_size(const MeshRenderData &mr, const bke::AttrDomain domain)
std::unique_ptr< gpu::VertBuf, gpu::VertBufDeleter > VertBufPtr
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void GPU_vertformat_alias_add(GPUVertFormat *, StringRef alias)
gpu::VertBuf * GPU_vertbuf_create_on_device(const GPUVertFormat &format, uint v_len)
void GPU_vertformat_deinterleave(GPUVertFormat *format)
GPUVertFormat GPU_vertformat_from_attribute(StringRef name, gpu::VertAttrType type)
void GPU_vertformat_safe_attr_name(StringRef attr_name, char *r_safe_name, uint max_len)
BMDataLayerLookup BM_data_layer_lookup(const BMesh &bm, const StringRef name)
void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len)