Blender V5.3
subdiv_converter_mesh.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2018 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "subdiv_converter.hh"
10
11#include <cstring>
12
13#include "BLI_array_utils.hh"
14#include "BLI_task.hh"
15
16#include "BKE_attribute.hh"
17#include "BKE_customdata.hh"
18#include "BKE_mesh.hh"
19#include "BKE_mesh_mapping.hh"
20#include "BKE_subdiv.hh"
21
22#include "MEM_guardedalloc.h"
23
25
26namespace blender::bke::subdiv {
27
28/* Enable work-around for non-working CPU evaluator when using bilinear scheme.
29 * This forces Catmark scheme with all edges marked as infinitely sharp. */
30#define BUGGY_SIMPLE_SCHEME_WORKAROUND 1
31
34 const Mesh *mesh;
35 /* Edge vertices, either referencing Mesh::edges() or #edges_manifold. */
38
40 /* Vertex indices of face corners, using manifold vertex indices (see #manifold_vertex_index
41 * below). Either referencing of Mesh::corner_verts() #corner_verts_manifold. */
44
45 /* Sharpness of every manifold edge and vertex, or empty when nothing is sharp. See
46 * #OpenSubdiv_Converter::edge_sharpness. */
49
51
52 /* CustomData layer for vertex sharpnesses. */
54 /* CustomData layer for edge sharpness. */
56 /* Indexed by loop index, value denotes index of face-varying vertex
57 * which corresponds to the UV coordinate.
58 */
61 /* Indexed by coarse mesh elements, gives index of corresponding element
62 * with ignoring all non-manifold entities.
63 *
64 * NOTE: This isn't strictly speaking manifold, this is more like non-loose
65 * geometry index. As in, index of element as if there were no loose edges
66 * or vertices in the mesh.
67 *
68 * Left empty when the mapping is the identity for all elements that are kept, which is the
69 * case whenever the removed (loose) elements are already a contiguous run at the end of the
70 * array. See #initialize_manifold_index_map.
71 */
73 /* Reverse mapping to above. */
76 /* Number of non-loose elements. */
79};
80
82{
83#if BUGGY_SIMPLE_SCHEME_WORKAROUND
84 (void)settings;
85 return OSD_SCHEME_CATMARK;
86#else
87 if (settings.is_simple) {
89 }
90 else {
91 return OSD_SCHEME_CATMARK;
92 }
93#endif
94}
95
96static bool specifies_full_topology(const OpenSubdiv_Converter * /*converter*/)
97{
98 return false;
99}
100
101/* Both `original_to_manifold()` and `manifold_to_original()` treat an empty map as the identity
102 * mapping. This is the case whenever the elements not passed to OpenSubdiv (loose vertices or
103 * edges) are already a contiguous run at the end of the array, see
104 * #initialize_manifold_index_map. */
105
106static int original_to_manifold(const Array<int> &forward_map, const int original_index)
107{
108 return forward_map.is_empty() ? original_index : forward_map[original_index];
109}
110
111static int manifold_to_original(const Array<int> &reverse_map, const int manifold_index)
112{
113 return reverse_map.is_empty() ? manifold_index : reverse_map[manifold_index];
114}
115
116static int get_num_uv_layers(const OpenSubdiv_Converter *converter)
117{
118 ConverterStorage *storage = static_cast<ConverterStorage *>(converter->user_data);
119 return storage->uv_map_names.size();
120}
121
122static void precalc_uv_layer(const OpenSubdiv_Converter *converter, const int layer_index)
123{
124 ConverterStorage *storage = static_cast<ConverterStorage *>(converter->user_data);
125 const Mesh *mesh = storage->mesh;
126 const StringRef name = storage->uv_map_names[layer_index];
127 const bke::AttributeAccessor attributes = mesh->attributes();
128 const VArraySpan uv_map = *attributes.lookup<float2>(name, bke::AttrDomain::Corner);
129 const int num_vert = storage->num_manifold_vertices;
130 /* Initialize memory required for the operations. */
131 if (storage->loop_uv_indices == nullptr) {
132 storage->loop_uv_indices = MEM_new_array_uninitialized<int>(size_t(mesh->corners_num),
133 "loop uv vertex index");
134 }
136 storage->faces, storage->corner_verts, uv_map, num_vert, float2(STD_UV_CONNECT_LIMIT), true);
137 /* NOTE: First UV vertex is supposed to be always marked as separate. */
138 storage->num_uv_coordinates = -1;
139 for (int vertex_index = 0; vertex_index < num_vert; vertex_index++) {
140 const UvMapVert *uv_vert = BKE_mesh_uv_vert_map_get_vert(uv_vert_map, vertex_index);
141 while (uv_vert != nullptr) {
142 if (uv_vert->separate) {
143 storage->num_uv_coordinates++;
144 }
145 const IndexRange face = storage->faces[uv_vert->face_index];
146 const int global_loop_index = face.start() + uv_vert->loop_of_face_index;
147 storage->loop_uv_indices[global_loop_index] = storage->num_uv_coordinates;
148 uv_vert = uv_vert->next;
149 }
150 }
151 /* So far this value was used as a 0-based index, actual number of UV
152 * vertices is 1 more.
153 */
154 storage->num_uv_coordinates += 1;
155 BKE_mesh_uv_vert_map_free(uv_vert_map);
156}
157
158static void finish_uv_layer(const OpenSubdiv_Converter * /*converter*/) {}
159
160static int get_num_uvs(const OpenSubdiv_Converter *converter)
161{
162 ConverterStorage *storage = static_cast<ConverterStorage *>(converter->user_data);
163 return storage->num_uv_coordinates;
164}
165
167 const int face_index,
168 const int corner)
169{
170 ConverterStorage *storage = static_cast<ConverterStorage *>(converter->user_data);
171 const IndexRange face = storage->faces[face_index];
172 return storage->loop_uv_indices[face.start() + corner];
173}
174
175static void free_user_data(const OpenSubdiv_Converter *converter)
176{
177 ConverterStorage *user_data = static_cast<ConverterStorage *>(converter->user_data);
178 MEM_SAFE_DELETE(user_data->loop_uv_indices);
179 MEM_delete(user_data);
180}
181
183{
185
186 converter->getFaceEdges = nullptr;
187
188 converter->getNumEdgeFaces = nullptr;
189 converter->getEdgeFaces = nullptr;
190
191 converter->getNumVertexEdges = nullptr;
192 converter->getVertexEdges = nullptr;
193 converter->getNumVertexFaces = nullptr;
194 converter->getVertexFaces = nullptr;
195
197 converter->precalcUVLayer = precalc_uv_layer;
198 converter->finishUVLayer = finish_uv_layer;
199 converter->getNumUVCoordinates = get_num_uvs;
201
202 converter->freeUserData = free_user_data;
203}
204
205/* Whether `unused_mask` (e.g. loose vertices/edges) is empty, or is exactly a contiguous section
206 * of indices at the end. In that case every kept element already has the same index whether or not
207 * the unused elements are removed, so no remapping is necessary. */
208static bool unused_is_trailing_range(const IndexMask &unused_mask, const int num_elements)
209{
210 if (unused_mask.is_empty()) {
211 return true;
212 }
213 const std::optional<IndexRange> range = unused_mask.to_range();
214 return range.has_value() && range->one_after_last() == num_elements;
215}
216
217/* Builds the mapping between original and manifold (i.e. skipping `unused_mask`) indices for a
218 * set of coarse mesh elements (vertices or edges). Returns the number of manifold elements. */
219static int initialize_manifold_index_map(const IndexMask &unused_mask,
220 const int num_elements,
221 Array<int> *r_index_forward,
222 Array<int> *r_index_reverse)
223{
224 const int num_manifold_elements = num_elements - unused_mask.size();
225 if (unused_is_trailing_range(unused_mask, num_elements)) {
226 return num_manifold_elements;
227 }
228 IndexMaskMemory memory;
229 const IndexMask used_mask = unused_mask.complement(IndexRange(num_elements), memory);
230 if (r_index_forward != nullptr) {
231 r_index_forward->reinitialize(num_elements);
232 index_mask::build_reverse_map<int>(used_mask, *r_index_forward);
233 }
234 if (r_index_reverse != nullptr) {
235 r_index_reverse->reinitialize(num_manifold_elements);
236 used_mask.to_indices<int>(*r_index_reverse);
237 }
238 return num_manifold_elements;
239}
240
241/* Builds the manifold-space edges array exposed as `converter->edges`: for each manifold edge
242 * index, the pair of manifold vertex indices, skipping loose edges. */
244{
245 const Span<int2> mesh_edges = storage.mesh->edges();
246 if (storage.manifold_edge_to_orig.is_empty() && storage.manifold_verts.is_empty()) {
247 storage.edges = mesh_edges.take_front(storage.num_manifold_edges);
248 return;
249 }
250 storage.edges_manifold.reinitialize(storage.num_manifold_edges);
251 const MutableSpan<int2> edges_manifold = storage.edges_manifold;
252 threading::parallel_for(edges_manifold.index_range(), 4096, [&](const IndexRange range) {
253 for (const int manifold_edge_index : range) {
254 const int edge_index = manifold_to_original(storage.manifold_edge_to_orig,
255 manifold_edge_index);
256 const int2 &edge = mesh_edges[edge_index];
257 edges_manifold[manifold_edge_index] = int2(
258 original_to_manifold(storage.manifold_verts, edge[0]),
259 original_to_manifold(storage.manifold_verts, edge[1]));
260 }
261 });
262 storage.edges = storage.edges_manifold;
263}
264
266{
267 const Mesh *mesh = storage.mesh;
268 const IndexMask &loose_verts = mesh->verts_no_face();
269 const IndexMask &loose_edges = mesh->loose_edges();
270
271 storage.num_manifold_vertices = initialize_manifold_index_map(
272 loose_verts, mesh->verts_num, &storage.manifold_verts, &storage.manifold_vert_to_orig);
273 if (storage.manifold_verts.is_empty()) {
274 storage.corner_verts = mesh->corner_verts();
275 }
276 else {
277 const Span<int> mesh_corner_verts = mesh->corner_verts();
278 storage.corner_verts_manifold.reinitialize(mesh_corner_verts.size());
279 array_utils::gather(storage.manifold_verts.as_span(),
280 mesh_corner_verts,
281 storage.corner_verts_manifold.as_mutable_span());
282 storage.corner_verts = storage.corner_verts_manifold;
283 }
284
285 storage.num_manifold_edges = initialize_manifold_index_map(
286 loose_edges, mesh->edges_num, nullptr, &storage.manifold_edge_to_orig);
288}
289
290/* Fills `sharpness` with the sharpness of every manifold element, from the crease attribute of the
291 * original elements. `manifold_to_orig` is empty when it is the identity mapping, in which case
292 * the manifold elements are simply the first `sharpness.size()` original ones. */
293static void gather_manifold_sharpness(const Span<float> creases,
294 const Span<int> manifold_to_orig,
295 const MutableSpan<float> sharpness)
296{
297 if (manifold_to_orig.is_empty()) {
298 array_utils::copy(creases.take_front(sharpness.size()), sharpness);
299 }
300 else {
301 array_utils::gather(creases, manifold_to_orig, sharpness);
302 }
303 threading::parallel_for(sharpness.index_range(), 4096, [&](const IndexRange range) {
304 for (const int i : range) {
305 sharpness[i] = crease_to_sharpness(sharpness[i]);
306 }
307 });
308}
309
310/* Builds the sharpness of every manifold edge, exposed as `converter->edge_sharpness`. Left empty
311 * when no edge is sharp, which OpenSubdiv treats the same as all-zero sharpness. */
313{
314#if BUGGY_SIMPLE_SCHEME_WORKAROUND
315 if (storage.settings.is_simple) {
316 /* Emulate the bilinear scheme by making every edge infinitely sharp. */
317 storage.edge_sharpness = Array<float>(storage.num_manifold_edges,
319 return;
320 }
321#endif
322 if (storage.cd_edge_crease.is_empty()) {
323 return;
324 }
325 storage.edge_sharpness.reinitialize(storage.num_manifold_edges);
327 storage.cd_edge_crease, storage.manifold_edge_to_orig, storage.edge_sharpness);
328}
329
330/* Builds the sharpness of every manifold vertex, exposed as `converter->vert_sharpness`. Besides
331 * the vertex crease this includes the infinite sharpness given to vertices adjacent to a loose
332 * edge, since OpenSubdiv expects non-manifold vertices to be sharp. Left empty when no vertex is
333 * sharp. */
335{
336#if BUGGY_SIMPLE_SCHEME_WORKAROUND
337 if (storage.settings.is_simple) {
338 /* Emulate the bilinear scheme by making every vertex infinitely sharp. */
339 storage.vert_sharpness = Array<float>(storage.num_manifold_vertices,
341 return;
342 }
343#endif
344 const Mesh *mesh = storage.mesh;
345 const IndexMask &loose_edges = mesh->loose_edges();
346 if (storage.cd_vertex_crease.is_empty() && loose_edges.is_empty()) {
347 return;
348 }
349
350 storage.vert_sharpness.reinitialize(storage.num_manifold_vertices);
351 const MutableSpan<float> sharpness = storage.vert_sharpness;
352 if (storage.cd_vertex_crease.is_empty()) {
353 sharpness.fill(0.0f);
354 }
355 else {
356 gather_manifold_sharpness(storage.cd_vertex_crease, storage.manifold_vert_to_orig, sharpness);
357 }
358
359 if (loose_edges.is_empty()) {
360 return;
361 }
362
363 /* Vertices adjacent to a loose edge are infinitely sharp, which overrides the vertex crease.
364 * The map is indexed by the original vertex index, since such a vertex may itself be loose, in
365 * which case it is not one of the manifold vertices at all. */
366 BitVector<> infinite_sharp_verts(mesh->verts_num, false);
367 const Span<int2> edges = mesh->edges();
368 loose_edges.foreach_index([&](const int edge_index) {
369 const int2 edge = edges[edge_index];
370 infinite_sharp_verts[edge[0]].set();
371 infinite_sharp_verts[edge[1]].set();
372 });
373 threading::parallel_for(sharpness.index_range(), 4096, [&](const IndexRange range) {
374 for (const int manifold_vertex_index : range) {
375 const int vertex_index = manifold_to_original(storage.manifold_vert_to_orig,
376 manifold_vertex_index);
377 if (infinite_sharp_verts[vertex_index]) {
378 sharpness[manifold_vertex_index] = OPENSUBDIV_SHARPNESS_INFINITE;
379 }
380 }
381 });
382}
383
385 const Settings *settings,
386 const Mesh *mesh)
387{
388 ConverterStorage *user_data = MEM_new<ConverterStorage>(__func__);
389 user_data->settings = *settings;
390 user_data->mesh = mesh;
391 user_data->faces = mesh->faces();
392 if (settings->use_creases) {
393 const AttributeAccessor attributes = mesh->attributes();
394 user_data->cd_vertex_crease = *attributes.lookup<float>("crease_vert", AttrDomain::Point);
395 user_data->cd_edge_crease = *attributes.lookup<float>("crease_edge", AttrDomain::Edge);
396 }
397 user_data->uv_map_names = mesh->uv_map_names();
398 user_data->loop_uv_indices = nullptr;
399 initialize_manifold_indices(*user_data);
400 initialize_edge_sharpness(*user_data);
401 initialize_vert_sharpness(*user_data);
402 converter->user_data = user_data;
403}
404
406 const Settings *settings,
407 const Mesh *mesh)
408{
409 init_functions(converter);
410 init_user_data(converter, settings, mesh);
411 const auto &storage = *static_cast<ConverterStorage *>(converter->user_data);
412 converter->verts_num = storage.num_manifold_vertices;
413 converter->face_offsets = storage.mesh->face_offsets();
414 converter->edges = storage.edges.cast<std::pair<int, int>>();
415 converter->corner_verts = storage.corner_verts;
416 converter->edge_sharpness = storage.edge_sharpness;
417 converter->vert_sharpness = storage.vert_sharpness;
418 converter->scheme_type = get_scheme_type(*settings);
423}
424
425} // namespace blender::bke::subdiv
CustomData interface, see also DNA_customdata_types.h.
#define STD_UV_CONNECT_LIMIT
Read Blender memory allocation and freeing.
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
bool is_empty() const
Definition BLI_array.hh:275
constexpr IndexRange()=default
constexpr int64_t start() const
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr void fill(const T &value) const
Definition BLI_span.hh:523
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr Span take_front(int64_t n) const
Definition BLI_span.hh:197
constexpr bool is_empty() const
Definition BLI_span.hh:264
GAttributeReader lookup(const StringRef name) const
NonMovable(NonMovable &&other)=delete
std::optional< IndexRange > to_range() const
void to_indices(MutableSpan< T > r_indices) const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
IndexMask complement(const IndexMask &universe, LinearAllocator<> &memory) const
#define storage(slot, qualifiers)
void gather(const GVArray &src, const IndexMask &indices, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
void copy(const GVArray &src, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
static void init_functions(OpenSubdiv_Converter *converter)
static void free_user_data(const OpenSubdiv_Converter *converter)
void converter_init_for_mesh(OpenSubdiv_Converter *converter, const Settings *settings, const Mesh *mesh)
static int get_num_uvs(const OpenSubdiv_Converter *converter)
static int initialize_manifold_index_map(const IndexMask &unused_mask, const int num_elements, Array< int > *r_index_forward, Array< int > *r_index_reverse)
static void initialize_manifold_edges(ConverterStorage &storage)
int converter_fvar_linear_from_settings(const Settings *settings)
static int get_num_uv_layers(const OpenSubdiv_Converter *converter)
static bool specifies_full_topology(const OpenSubdiv_Converter *)
static void finish_uv_layer(const OpenSubdiv_Converter *)
int converter_vtx_boundary_interpolation_from_settings(const Settings *settings)
static int original_to_manifold(const Array< int > &forward_map, const int original_index)
static OpenSubdiv_SchemeType get_scheme_type(const Settings &settings)
static void gather_manifold_sharpness(const Span< float > creases, const Span< int > manifold_to_orig, const MutableSpan< float > sharpness)
static void initialize_edge_sharpness(ConverterStorage &storage)
static int get_face_corner_uv_index(const OpenSubdiv_Converter *converter, const int face_index, const int corner)
static bool unused_is_trailing_range(const IndexMask &unused_mask, const int num_elements)
static void initialize_manifold_indices(ConverterStorage &storage)
static void precalc_uv_layer(const OpenSubdiv_Converter *converter, const int layer_index)
static void init_user_data(OpenSubdiv_Converter *converter, const Settings *settings, const Mesh *mesh)
static int manifold_to_original(const Array< int > &reverse_map, const int manifold_index)
static void initialize_vert_sharpness(ConverterStorage &storage)
template void build_reverse_map< int >(const IndexMask &mask, MutableSpan< int > r_map)
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
void BKE_mesh_uv_vert_map_free(UvVertMap *vmap)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
UvMapVert * BKE_mesh_uv_vert_map_get_vert(UvVertMap *vmap, unsigned int v)
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
UvVertMap * BKE_mesh_uv_vert_map_create(OffsetIndices< int > faces, Span< int > corner_verts, Span< float2 > uv_map, int verts_num, const float2 &limit, bool use_winding)
OpenSubdiv_FVarLinearInterpolation
OpenSubdiv_VtxBoundaryInterpolation
OpenSubdiv_SchemeType
@ OSD_SCHEME_CATMARK
@ OSD_SCHEME_BILINEAR
constexpr float OPENSUBDIV_SHARPNESS_INFINITE
void(*) getFaceEdges(const OpenSubdiv_Converter *converter, const int face_index, int *face_edges)
OpenSubdiv_VtxBoundaryInterpolation vtx_boundary_interpolation
std::span< const float > edge_sharpness
OpenSubdiv_FVarLinearInterpolation fvar_linear_interpolation
std::span< const float > vert_sharpness
void(*) getVertexEdges(const OpenSubdiv_Converter *converter, const int vertex_index, int *vertex_edges)
int(*) getNumUVCoordinates(const OpenSubdiv_Converter *converter)
OpenSubdiv_SchemeType scheme_type
void(*) getVertexFaces(const OpenSubdiv_Converter *converter, const int vertex_index, int *vertex_faces)
int(*) getNumVertexFaces(const OpenSubdiv_Converter *converter, const int vertex_index)
std::span< const int > face_offsets
std::span< const std::pair< int, int > > edges
void(*) precalcUVLayer(const OpenSubdiv_Converter *converter, const int layer_index)
void(*) getEdgeFaces(const OpenSubdiv_Converter *converter, const int edge, int *edge_faces)
int(*) getNumVertexEdges(const OpenSubdiv_Converter *converter, const int vertex_index)
int(*) getNumUVLayers(const OpenSubdiv_Converter *converter)
std::span< const int > corner_verts
int(*) getFaceCornerUVIndex(const OpenSubdiv_Converter *converter, const int face_index, const int corner_index)
int(*) getNumEdgeFaces(const OpenSubdiv_Converter *converter, const int edge_index)
bool(*) specifiesFullTopology(const OpenSubdiv_Converter *converter)
void(*) finishUVLayer(const OpenSubdiv_Converter *converter)
void(*) freeUserData(const OpenSubdiv_Converter *converter)
unsigned short loop_of_face_index