Blender V5.3
extract_mesh_vbo_uv.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_array_utils.hh"
11#include "BLI_string_utf8.hh"
12
14#include "BKE_attribute.hh"
15
16#include "draw_subdivision.hh"
17#include "extract_mesh.hh"
18
19namespace blender::draw {
20
21/* Initialize the vertex format to be used for UVs. Return true if any UV layer is
22 * found, false otherwise. */
24 const MeshBatchCache &cache,
25 const Mesh &mesh,
26 const MeshExtractType extract_type)
27{
29
30 VectorSet<StringRef> uv_layers;
31 for (const StringRef name : cache.cd_used.uv.as_span().take_front(MAX_MTFACE)) {
32 uv_layers.add_new(name);
33 }
34
35 const StringRef active_name = mesh.active_or_default_uv_map_name();
36 const StringRef default_name = mesh.default_uv_map_name();
37
38 /* HACK to fix #68857 */
39 if (extract_type == MeshExtractType::BMesh && cache.cd_used.edit_uv == 1) {
40 if (!bke::attribute_name_is_anonymous(default_name)) {
41 uv_layers.add(default_name);
42 }
43 }
44
45 const StringRef stencil_name = [&]() -> StringRef {
46 StringRef name = mesh.stencil_uv_map_attribute;
47 if (name.is_empty()) {
48 name = mesh.active_uv_map_name();
49 }
50 return name;
51 }();
52
53 for (const StringRef name : uv_layers) {
54 char attr_name[32], attr_safe_name[GPU_MAX_SAFE_ATTR_NAME];
55 GPU_vertformat_safe_attr_name(name, attr_safe_name, GPU_MAX_SAFE_ATTR_NAME);
56 SNPRINTF_UTF8(attr_name, "a%s", attr_safe_name);
57 GPU_vertformat_attr_add(format, attr_name, gpu::VertAttrType::SFLOAT_32_32);
58 if (name == default_name) {
59 GPU_vertformat_alias_add(format, "a");
60 }
61 if (name == active_name) {
62 GPU_vertformat_alias_add(format, "au");
63 /* Alias to `pos` for edit uvs. */
64 GPU_vertformat_alias_add(format, "pos");
65 }
66 if (name == stencil_name) {
67 GPU_vertformat_alias_add(format, "mu");
68 }
69 }
70
71 if (format->attr_len == 0) {
72 GPU_vertformat_attr_add(format, "dummy", gpu::VertAttrType::SFLOAT_32_32);
73 }
74
75 return uv_layers;
76}
77
79{
80 GPUVertFormat format = {0};
81
82 int v_len = mr.corners_num;
84 &format, cache, *mr.mesh, mr.extract_type);
85 if (uv_layers.is_empty()) {
86 /* VBO will not be used, only allocate minimum of memory. */
87 v_len = 1;
88 }
89
90 gpu::VertBufPtr vbo = gpu::VertBufPtr(GPU_vertbuf_create_with_format(format));
91 GPU_vertbuf_data_alloc(*vbo, v_len);
92
93 MutableSpan<float2> uv_data = vbo->data<float2>();
95 if (mr.extract_type == MeshExtractType::BMesh) {
96 const BMesh &bm = *mr.bm;
97 for (const int i : uv_layers.index_range()) {
98 MutableSpan<float2> data = uv_data.slice(i * bm.totloop, bm.totloop);
99 const int offset = CustomData_get_offset_named(&bm.ldata, CD_PROP_FLOAT2, uv_layers[i]);
100 threading::parallel_for(IndexRange(bm.totface), 2048, [&](const IndexRange range) {
101 for (const int face_index : range) {
102 const BMFace &face = *BM_face_at_index(&const_cast<BMesh &>(bm), face_index);
103 const BMLoop *loop = BM_FACE_FIRST_LOOP(&face);
104 for ([[maybe_unused]] const int i : IndexRange(face.len)) {
105 const int index = BM_elem_index_get(loop);
106 data[index] = BM_ELEM_CD_GET_FLOAT_P(loop, offset);
107 loop = loop->next;
108 }
109 }
110 });
111 }
112 }
113 else {
114 const bke::AttributeAccessor attributes = mr.mesh->attributes();
115 for (const int i : uv_layers.index_range()) {
116 const VArray uv_map = *attributes.lookup_or_default<float2>(
117 uv_layers[i], bke::AttrDomain::Corner, float2(0));
118 array_utils::copy(uv_map, uv_data.slice(i * mr.corners_num, mr.corners_num));
119 }
120 }
121 });
122 return vbo;
123}
124
126{
127 const bke::AttributeAccessor attributes = mesh.attributes();
129 attributes.foreach_attribute([&](const bke::AttributeIter &iter) {
131 result.add(iter.name);
132 }
133 });
134 return result;
135}
136
138 const MeshBatchCache &cache)
139{
140 const Mesh *coarse_mesh = subdiv_cache.mesh;
141 GPUVertFormat format = {0};
142
144 &format, cache, *coarse_mesh, MeshExtractType::Mesh);
145
146 uint v_len = subdiv_cache.num_subdiv_loops;
147 if (uv_layers.is_empty()) {
148 /* TODO(kevindietrich): handle this more gracefully. */
149 v_len = 1;
150 }
151
153
154 if (uv_layers.is_empty()) {
155 return vbo;
156 }
157
158 const VectorSet<StringRef> all_uv_maps = all_uv_map_attributes(*coarse_mesh);
159
160 /* Index of the UV layer in the compact buffer. Used UV layers are stored in a single buffer. */
161 for (const int pack_layer_index : uv_layers.index_range()) {
162 const StringRef name = uv_layers[pack_layer_index];
163 /* This lookup may fail because some modifiers (e.g. Skin)
164 * do not generate UV maps. See: #151787. */
165 const int i = all_uv_maps.index_of_try(name);
166 if (i == -1) {
167 continue;
168 }
169 const int offset = int(subdiv_cache.num_subdiv_loops) * pack_layer_index;
170 draw_subdiv_extract_uvs(subdiv_cache, vbo.get(), i, offset);
171 }
172 return vbo;
173}
174
175} // namespace blender::draw
#define SNPRINTF_UTF8(dst, format,...)
#define MAX_MTFACE
constexpr T * data() const
Definition BLI_span.hh:545
constexpr int64_t size_in_bytes() const
Definition BLI_span.hh:507
int64_t index_of_try(const Key &key) const
bool add(const Key &key)
IndexRange index_range() const
Span< Key > as_span() const
void add_new(const Key &key)
void foreach_attribute(const FunctionRef< void(const AttributeIter &)> fn) const
constexpr bool is_empty() const
Extraction of Mesh data into VBO to feed to GPU.
bool attribute_name_is_anonymous(const StringRef name)
void draw_subdiv_extract_uvs(const DRWSubdivCache &cache, gpu::VertBuf *uvs, const int face_varying_channel, const int dst_offset)
static VectorSet< StringRef > mesh_extract_uv_format_init(GPUVertFormat *format, const MeshBatchCache &cache, const Mesh &mesh, const MeshExtractType extract_type)
static VectorSet< StringRef > all_uv_map_attributes(const Mesh &mesh)
gpu::VertBufPtr extract_uv_maps(const MeshRenderData &mr, const MeshBatchCache &cache)
gpu::VertBufPtr extract_uv_maps_subdiv(const DRWSubdivCache &subdiv_cache, const MeshBatchCache &cache)
std::unique_ptr< gpu::VertBuf, gpu::VertBufDeleter > VertBufPtr
void memory_bandwidth_bound_task(const int64_t approximate_bytes_touched, const Function &function)
Definition BLI_task.hh:251
void GPU_vertformat_alias_add(GPUVertFormat *, StringRef alias)
unsigned int uint
Definition BKE_text.h:55
gpu::VertBuf * GPU_vertbuf_create_on_device(const GPUVertFormat &format, uint v_len)
void GPU_vertformat_deinterleave(GPUVertFormat *format)
VecBase< float, 2 > float2
void GPU_vertformat_safe_attr_name(StringRef attr_name, char *r_safe_name, uint max_len)
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len)
VectorSet< std::string > uv