Blender V5.3
multires_reshape_smooth.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "multires_reshape.hh"
10
11#include "MEM_guardedalloc.h"
12
13#include "DNA_mesh_types.h"
14
15#include "BLI_function_ref.hh"
16#include "BLI_math_matrix.hh"
17#include "BLI_math_matrix_c.hh"
18#include "BLI_math_vector_c.hh"
19#include "BLI_task.hh"
20#include "BLI_utildefines.hh"
21
22#include "BKE_customdata.hh"
23#include "BKE_mesh.hh"
24#include "BKE_multires.hh"
25#include "BKE_subdiv.hh"
26#include "BKE_subdiv_eval.hh"
27#include "BKE_subdiv_foreach.hh"
28#include "BKE_subdiv_mesh.hh"
29
31#ifdef WITH_OPENSUBDIV
33#endif
35
36#include "atomic_ops.h"
37#include "subdiv_converter.hh"
38
39namespace blender {
40
41#ifdef WITH_OPENSUBDIV
42
43/* -------------------------------------------------------------------- */
46
47/* Surface refers to a simplified and lower-memory footprint representation of the limit surface.
48 *
49 * Used to store pre-calculated information which is expensive or impossible to evaluate when
50 * traversing the final limit surface. */
51
52struct SurfacePoint {
53 float3 P = float3(0.0f);
54 float3x3 tangent_matrix = float3x3::identity();
55};
56
57struct SurfaceGrid {
58 Array<SurfacePoint> points = {};
59};
60
61/* Geometry elements which are used to simplify creation of topology refiner at the sculpt level.
62 * Contains a limited subset of information needed to construct topology refiner. */
63
64struct Vertex {
65 /* All grid coordinates which the vertex corresponding to.
66 * For a vertices which are created from inner points of grids there is always one coordinate. */
67 Vector<GridCoord> grid_coords = {};
68};
69
70/* Storage of data which is linearly interpolated from the reshape level to the top level. */
71
72struct LinearGridElement {
73 float mask = 0.0f;
74};
75
76struct LinearGrid {
77 /* Span pointing to section of `elements_storage` in `LinearGrids` */
78 MutableSpan<LinearGridElement> elements = {};
79};
80
81struct LinearGrids {
82 int num_grids = 0;
83 int level = 0;
84
85 /* Cached size for the grid, for faster lookup. */
86 int grid_size;
87
88 /* Indexed by grid index. */
89 Array<LinearGrid> grids;
90
91 /* Elements for all grids are allocated in a single array, for the allocation performance. */
92 Array<LinearGridElement> elements_storage;
93};
94
95/* Context which holds all information needed during propagation and smoothing. */
96
97struct MultiresReshapeSmoothContext : NonCopyable, NonMovable {
98 const MultiresReshapeContext *reshape_context;
99
100 /* Geometry at a reshape multires level. */
101 struct {
102 Array<Vertex> vertices;
103 /* Sharpness of each vertex, including the infinite sharpness of vertices which are adjacent
104 * to a loose edge. See #OpenSubdiv_Converter::vert_sharpness. */
105 Array<float> vert_sharpness;
106
107 /* Maximum number of edges which might be stored in the edges array.
108 * Is calculated based on the number of edges in the base mesh and the subdivision level. */
109 int max_edges;
110
111 /* Sparse storage of edges. Will only include edges which have non-zero sharpness.
112 *
113 * NOTE: Different type from others to be able to easier use atomic ops. */
114 size_t num_edges;
115 Array<int2> edges;
116 /* Sharpness of each of the #num_edges edges above. */
117 Array<float> edge_sharpness;
118
119 /* Indexes into the geometry.vertices array */
120 Array<int> corner_verts;
121 /* The grid each corner was created for. All the corners of a face share the same grid. */
122 Array<int> corner_grid_indices;
123
124 /* Face topology of subdivision level. */
125 Array<int> face_offsets;
126
127 OffsetIndices<int> faces() const
128 {
129 return OffsetIndices<int>(face_offsets, offset_indices::NoSortCheck());
130 }
131 } geometry;
132
133 /* Grids of data which is linearly interpolated between grid elements at the reshape level.
134 * The data is actually stored as a delta, which is then to be added to the higher levels. */
135 LinearGrids linear_delta_grids;
136
137 /* From #Mesh::loose_edges(). May be empty. */
138 BitVector<> loose_base_edges = {};
139
140 /* Subdivision surface created for geometry at a reshape level. */
141 bke::subdiv::Subdiv *reshape_subdiv = nullptr;
142
143 /* Limit surface of the base mesh with original sculpt level details on it, subdivided up to the
144 * top level.
145 * Is used as a base point to calculate how much displacement has been made in the sculpt mode.
146 *
147 * NOTE: Referring to sculpt as it is the main user of this functionality and it is clear to
148 * understand what it actually means in a concrete example. This is a generic code which is also
149 * used by Subdivide operation, but the idea is exactly the same as propagation in the sculpt
150 * mode. */
151 Array<SurfaceGrid> base_surface_grids;
152
153 /* Defines how displacement is interpolated on the higher levels (for example, whether
154 * displacement is smoothed in Catmull-Clark mode or interpolated linearly preserving sharp edges
155 * of the current sculpt level).
156 *
157 * NOTE: Uses same enumerator type as Subdivide operator, since the values are the same and
158 * decoupling type just adds extra headache to convert one enumerator to another. */
159 MultiresSubdivideModeType smoothing_type;
160
161 MultiresReshapeSmoothContext(const MultiresReshapeContext *reshape_context,
162 const MultiresSubdivideModeType smoothing_type)
163 : reshape_context(reshape_context),
164 geometry(),
165 linear_delta_grids(),
166 smoothing_type(smoothing_type)
167 {
168 }
169 ~MultiresReshapeSmoothContext();
170};
171
172MultiresReshapeSmoothContext::~MultiresReshapeSmoothContext()
173{
174 if (this->reshape_subdiv == nullptr) {
175 return;
176 }
177 bke::subdiv::free(this->reshape_subdiv);
178}
179
181
182/* -------------------------------------------------------------------- */
185
186static void linear_grids_allocate(LinearGrids *linear_grids, int num_grids, int level)
187{
188 const size_t grid_size = bke::subdiv::grid_size_from_level(level);
189 const size_t grid_area = grid_size * grid_size;
190 const size_t num_grid_elements = num_grids * grid_area;
191
192 linear_grids->num_grids = num_grids;
193 linear_grids->level = level;
194 linear_grids->grid_size = grid_size;
195
196 linear_grids->grids.reinitialize(num_grids);
197 linear_grids->elements_storage.reinitialize(num_grid_elements);
198
199 for (int i = 0; i < num_grids; ++i) {
200 const size_t element_offset = grid_area * i;
201 linear_grids->grids[i].elements = linear_grids->elements_storage.as_mutable_span().slice(
202 element_offset, grid_area);
203 }
204}
205
206static LinearGridElement *linear_grid_element_get(LinearGrids *linear_grids,
207 const GridCoord *grid_coord)
208{
209 BLI_assert(grid_coord->grid_index >= 0);
210 BLI_assert(grid_coord->grid_index < linear_grids->num_grids);
211
212 const int grid_size = linear_grids->grid_size;
213
214 const int grid_x = lround(grid_coord->u * (grid_size - 1));
215 const int grid_y = lround(grid_coord->v * (grid_size - 1));
216 const int grid_element_index = grid_y * grid_size + grid_x;
217
218 LinearGrid *grid = &linear_grids->grids[grid_coord->grid_index];
219 return &grid->elements[grid_element_index];
220}
221
222static void linear_grid_element_init(LinearGridElement *linear_grid_element)
223{
224 linear_grid_element->mask = 0.0f;
225}
226
227/* result = a - b. */
228static void linear_grid_element_sub(LinearGridElement *result,
229 const LinearGridElement *a,
230 const LinearGridElement *b)
231{
232 result->mask = a->mask - b->mask;
233}
234
235static void linear_grid_element_interpolate(LinearGridElement *result,
236 const LinearGridElement elements[4],
237 const float weights[4])
238{
239 result->mask = elements[0].mask * weights[0] + elements[1].mask * weights[1] +
240 elements[2].mask * weights[2] + elements[3].mask * weights[3];
241}
242
244
245/* -------------------------------------------------------------------- */
248
249static void base_surface_grids_allocate(MultiresReshapeSmoothContext *reshape_smooth_context)
250{
251 const MultiresReshapeContext *reshape_context = reshape_smooth_context->reshape_context;
252
253 const int num_grids = reshape_context->num_grids;
254 const int grid_size = reshape_context->top.grid_size;
255 const int grid_area = grid_size * grid_size;
256
257 reshape_smooth_context->base_surface_grids.reinitialize(num_grids);
258
259 for (const int i : reshape_smooth_context->base_surface_grids.index_range()) {
260 reshape_smooth_context->base_surface_grids[i].points.reinitialize(grid_area);
261 }
262}
263
264static SurfacePoint *base_surface_grids_read(MultiresReshapeSmoothContext *reshape_smooth_context,
265 const GridCoord *grid_coord)
266{
267 const MultiresReshapeContext *reshape_context = reshape_smooth_context->reshape_context;
268
269 const int grid_index = grid_coord->grid_index;
270 const int grid_size = reshape_context->top.grid_size;
271 const int grid_x = lround(grid_coord->u * (grid_size - 1));
272 const int grid_y = lround(grid_coord->v * (grid_size - 1));
273 const int grid_element_index = grid_y * grid_size + grid_x;
274
275 SurfaceGrid *surface_grid = &reshape_smooth_context->base_surface_grids[grid_index];
276 return &surface_grid->points[grid_element_index];
277}
278
279static void base_surface_grids_write(MultiresReshapeSmoothContext *reshape_smooth_context,
280 const GridCoord *grid_coord,
281 const float3 &P,
282 const float3x3 &tangent_matrix)
283{
284 SurfacePoint *point = base_surface_grids_read(reshape_smooth_context, grid_coord);
285 point->P = P;
286 point->tangent_matrix = tangent_matrix;
287}
288
290
291/* -------------------------------------------------------------------- */
294
295/* Find grid index which given face was created for. */
296static int get_face_grid_index(const MultiresReshapeSmoothContext *reshape_smooth_context,
297 const IndexRange face)
298{
299 const Span<int> corner_grid_indices = reshape_smooth_context->geometry.corner_grid_indices;
300 const int grid_index = corner_grid_indices[face.start()];
301
302# ifndef NDEBUG
303 for (const int corner_index : face) {
304 BLI_assert(corner_grid_indices[corner_index] == grid_index);
305 }
306# endif
307
308 return grid_index;
309}
310
311static std::optional<GridCoord> vert_grid_coord_with_grid_index(const Vertex *vert,
312 const int grid_index)
313{
314 for (const int i : vert->grid_coords.index_range()) {
315 if (vert->grid_coords[i].grid_index == grid_index) {
316 return vert->grid_coords[i];
317 }
318 }
319 return std::nullopt;
320}
321
322/* Get grid coordinates which correspond to corners of the given face.
323 * All the grid coordinates will be from the same grid index. */
324static std::array<std::optional<GridCoord>, 4> grid_coords_from_face_verts(
325 MultiresReshapeSmoothContext *reshape_smooth_context, const IndexRange face)
326{
327 std::array<std::optional<GridCoord>, 4> result;
328 BLI_assert(face.size() == 4);
329
330 const int grid_index = get_face_grid_index(reshape_smooth_context, face);
331 BLI_assert(grid_index != -1);
332
333 for (const int i : face.index_range()) {
334 const int corner_index = face[i];
335 const int vert_index = reshape_smooth_context->geometry.corner_verts[corner_index];
336 result[i] = vert_grid_coord_with_grid_index(
337 &reshape_smooth_context->geometry.vertices[vert_index], grid_index);
338 BLI_assert(result[i].has_value());
339 }
340 return result;
341}
342
347static float lerp_f(float t, float a, float b)
348{
349 return (a + t * (b - a));
350}
351
352static GridCoord interpolate_grid_coord(Span<std::optional<GridCoord>> face_grid_coords,
353 const float u,
354 const float v)
355{
356 /*
357 * v
358 * ^
359 * | (3) -------- (2)
360 * | | |
361 * | | |
362 * | | |
363 * | | |
364 * | (0) -------- (1)
365 * *--------------------------> u
366 */
368
369 const float u01 = lerp_f(u, face_grid_coords[0]->u, face_grid_coords[1]->u);
370 const float u32 = lerp_f(u, face_grid_coords[3]->u, face_grid_coords[2]->u);
371
372 const float v03 = lerp_f(v, face_grid_coords[0]->v, face_grid_coords[3]->v);
373 const float v12 = lerp_f(v, face_grid_coords[1]->v, face_grid_coords[2]->v);
374
375 result.grid_index = face_grid_coords[0]->grid_index;
376 result.u = lerp_f(v, u01, u32);
377 result.v = lerp_f(u, v03, v12);
378
379 return result;
380}
381
382static void foreach_toplevel_grid_coord(
383 MultiresReshapeSmoothContext *reshape_smooth_context,
384 FunctionRef<void(const PTexCoord *, const GridCoord *)> callback)
385{
386 const MultiresReshapeContext *reshape_context = reshape_smooth_context->reshape_context;
387 const int level_difference = (reshape_context->top.level - reshape_context->reshape.level);
388
389 const int inner_grid_size = (1 << level_difference) + 1;
390 const float inner_grid_size_1_inv = 1.0f / float(inner_grid_size - 1);
391
392 const OffsetIndices<int> faces = reshape_smooth_context->geometry.faces();
393 threading::parallel_for(faces.index_range(), 1, [&](const IndexRange range) {
394 for (const int face_index : range) {
395 const IndexRange face = faces[face_index];
396 std::array<std::optional<GridCoord>, 4> face_grid_coords = grid_coords_from_face_verts(
397 reshape_smooth_context, face);
398
399 for (int y = 0; y < inner_grid_size; ++y) {
400 const float ptex_v = float(y) * inner_grid_size_1_inv;
401 for (int x = 0; x < inner_grid_size; ++x) {
402 const float ptex_u = float(x) * inner_grid_size_1_inv;
403
404 PTexCoord ptex_coord;
405 ptex_coord.ptex_face_index = face_index;
406 ptex_coord.u = ptex_u;
407 ptex_coord.v = ptex_v;
408
409 const GridCoord grid_coord = interpolate_grid_coord(
410 Span(face_grid_coords), ptex_u, ptex_v);
411
412 callback(&ptex_coord, &grid_coord);
413 }
414 }
415 }
416 });
417}
418
420
421/* -------------------------------------------------------------------- */
427
428static int get_reshape_level_resolution(const MultiresReshapeContext *reshape_context)
429{
430 return (1 << reshape_context->reshape.level) + 1;
431}
432
433static bool is_crease_supported(const MultiresReshapeSmoothContext *reshape_smooth_context)
434{
435 return !ELEM(reshape_smooth_context->smoothing_type,
436 MultiresSubdivideModeType::Linear,
437 MultiresSubdivideModeType::Simple);
438}
439
440/* Get crease which will be used for communication to OpenSubdiv topology.
441 * Note that simple subdivision treats all base edges as infinitely sharp. */
442static float get_effective_crease(const MultiresReshapeSmoothContext *reshape_smooth_context,
443 const int base_edge_index)
444{
445 if (!is_crease_supported(reshape_smooth_context)) {
446 return 1.0f;
447 }
448 if (reshape_smooth_context->reshape_context->cd_edge_crease.is_empty()) {
449 return 0.0f;
450 }
451 return reshape_smooth_context->reshape_context->cd_edge_crease[base_edge_index];
452}
453
454static float get_effective_crease_float(const MultiresReshapeSmoothContext *reshape_smooth_context,
455 const float crease)
456{
457 if (!is_crease_supported(reshape_smooth_context)) {
458 return 1.0f;
459 }
460 return crease;
461}
462
463static bool foreach_topology_info(const bke::subdiv::ForeachContext *foreach_context,
464 const int num_vertices,
465 const int num_edges,
466 const int num_loops,
467 const int num_faces,
468 const Span<int> /*subdiv_face_offset*/)
469{
470 MultiresReshapeSmoothContext *reshape_smooth_context =
471 static_cast<MultiresReshapeSmoothContext *>(foreach_context->user_data);
472 const int max_edges = reshape_smooth_context->smoothing_type ==
473 MultiresSubdivideModeType::Linear ?
474 num_edges :
475 reshape_smooth_context->geometry.max_edges;
476
477 reshape_smooth_context->geometry.vertices.reinitialize(num_vertices);
478 reshape_smooth_context->geometry.vert_sharpness = Array<float>(num_vertices, 0.0f);
479
480 reshape_smooth_context->geometry.max_edges = max_edges;
481 reshape_smooth_context->geometry.edges.reinitialize(max_edges);
482 reshape_smooth_context->geometry.edge_sharpness.reinitialize(max_edges);
483
484 reshape_smooth_context->geometry.corner_verts.reinitialize(num_loops);
485 reshape_smooth_context->geometry.corner_grid_indices.reinitialize(num_loops);
486
487 reshape_smooth_context->geometry.face_offsets.reinitialize(num_faces + 1);
488 offset_indices::fill_constant_group_size(4, 0, reshape_smooth_context->geometry.face_offsets);
489 reshape_smooth_context->geometry.face_offsets.last() = num_loops;
490
491 return true;
492}
493
494static void foreach_single_vert(const bke::subdiv::ForeachContext *foreach_context,
495 const GridCoord *grid_coord,
496 const int coarse_vert_index,
497 const int subdiv_vert_index)
498{
499 MultiresReshapeSmoothContext *reshape_smooth_context =
500 static_cast<MultiresReshapeSmoothContext *>(foreach_context->user_data);
501
502 BLI_assert(subdiv_vert_index < reshape_smooth_context->geometry.vertices.size());
503
504 Vertex *vert = &reshape_smooth_context->geometry.vertices[subdiv_vert_index];
505
506 vert->grid_coords.append(*grid_coord);
507
508 if (coarse_vert_index == -1) {
509 return;
510 }
511
512 const MultiresReshapeContext *reshape_context = reshape_smooth_context->reshape_context;
513 if (reshape_context->cd_vert_crease.is_empty()) {
514 return;
515 }
516
517 float crease = reshape_context->cd_vert_crease[coarse_vert_index];
518 if (crease == 0.0f) {
519 return;
520 }
521
522 crease = get_effective_crease_float(reshape_smooth_context, crease);
523 reshape_smooth_context->geometry.vert_sharpness[subdiv_vert_index] =
524 bke::subdiv::crease_to_sharpness(crease);
525}
526
527/* TODO(sergey): De-duplicate with similar function in multires_reshape_vertcos.cc */
528static void foreach_vert(const bke::subdiv::ForeachContext *foreach_context,
529 const PTexCoord *ptex_coord,
530 const int coarse_vert_index,
531 const int subdiv_vert_index)
532{
533 const MultiresReshapeSmoothContext *reshape_smooth_context =
534 static_cast<const MultiresReshapeSmoothContext *>(foreach_context->user_data);
535 const MultiresReshapeContext *reshape_context = reshape_smooth_context->reshape_context;
536
537 const GridCoord grid_coord = multires_reshape_ptex_coord_to_grid(reshape_context, ptex_coord);
538 const int face_index = multires_reshape_grid_to_face_index(reshape_context,
539 grid_coord.grid_index);
540
541 const int num_corners = reshape_context->base_faces[face_index].size();
542 const int start_grid_index = reshape_context->base_faces[face_index].start();
543 const int corner = grid_coord.grid_index - start_grid_index;
544
545 if (grid_coord.u == 0.0f && grid_coord.v == 0.0f) {
546 for (int current_corner = 0; current_corner < num_corners; ++current_corner) {
547 GridCoord corner_grid_coord = grid_coord;
548 corner_grid_coord.grid_index = start_grid_index + current_corner;
549 foreach_single_vert(
550 foreach_context, &corner_grid_coord, coarse_vert_index, subdiv_vert_index);
551 }
552 return;
553 }
554
555 foreach_single_vert(foreach_context, &grid_coord, coarse_vert_index, subdiv_vert_index);
556
557 if (grid_coord.u == 0.0f) {
558 GridCoord prev_grid_coord;
559 prev_grid_coord.grid_index = start_grid_index + ((corner + num_corners - 1) % num_corners);
560 prev_grid_coord.u = grid_coord.v;
561 prev_grid_coord.v = 0.0f;
562
563 foreach_single_vert(foreach_context, &prev_grid_coord, coarse_vert_index, subdiv_vert_index);
564 }
565
566 if (grid_coord.v == 0.0f) {
567 GridCoord next_grid_coord;
568 next_grid_coord.grid_index = start_grid_index + ((corner + 1) % num_corners);
569 next_grid_coord.u = 0.0f;
570 next_grid_coord.v = grid_coord.u;
571
572 foreach_single_vert(foreach_context, &next_grid_coord, coarse_vert_index, subdiv_vert_index);
573 }
574}
575
576static void foreach_vert_inner(const bke::subdiv::ForeachContext *foreach_context,
577 void * /*tls*/,
578 const int ptex_face_index,
579 const float ptex_face_u,
580 const float ptex_face_v,
581 const int /*coarse_face_index*/,
582 const int /*coarse_corner*/,
583 const int subdiv_vert_index)
584{
585 PTexCoord ptex_coord{};
586 ptex_coord.ptex_face_index = ptex_face_index;
587 ptex_coord.u = ptex_face_u;
588 ptex_coord.v = ptex_face_v;
589 foreach_vert(foreach_context, &ptex_coord, -1, subdiv_vert_index);
590}
591
592static void foreach_vert_every_corner(const bke::subdiv::ForeachContext *foreach_context,
593 void * /*tls_v*/,
594 const int ptex_face_index,
595 const float ptex_face_u,
596 const float ptex_face_v,
597 const int coarse_vert_index,
598 const int /*coarse_face_index*/,
599 const int /*coarse_face_corner*/,
600 const int subdiv_vert_index)
601{
602 PTexCoord ptex_coord{};
603 ptex_coord.ptex_face_index = ptex_face_index;
604 ptex_coord.u = ptex_face_u;
605 ptex_coord.v = ptex_face_v;
606 foreach_vert(foreach_context, &ptex_coord, coarse_vert_index, subdiv_vert_index);
607}
608
609static void foreach_vert_every_edge(const bke::subdiv::ForeachContext *foreach_context,
610 void * /*tls_v*/,
611 const int ptex_face_index,
612 const float ptex_face_u,
613 const float ptex_face_v,
614 const int /*coarse_edge_index*/,
615 const int /*coarse_face_index*/,
616 const int /*coarse_face_corner*/,
617 const int subdiv_vert_index)
618{
619 PTexCoord ptex_coord{};
620 ptex_coord.ptex_face_index = ptex_face_index;
621 ptex_coord.u = ptex_face_u;
622 ptex_coord.v = ptex_face_v;
623 foreach_vert(foreach_context, &ptex_coord, -1, subdiv_vert_index);
624}
625
626static void foreach_loop(const bke::subdiv::ForeachContext *foreach_context,
627 void * /*tls*/,
628 const int /*ptex_face_index*/,
629 const float /*ptex_face_u*/,
630 const float /*ptex_face_v*/,
631 const int /*coarse_loop_index*/,
632 const int coarse_face_index,
633 const int coarse_corner,
634 const int subdiv_loop_index,
635 const int subdiv_vert_index,
636 const int /*subdiv_edge_index*/)
637{
638 MultiresReshapeSmoothContext *reshape_smooth_context =
639 static_cast<MultiresReshapeSmoothContext *>(foreach_context->user_data);
640 const MultiresReshapeContext *reshape_context = reshape_smooth_context->reshape_context;
641
642 auto &geometry = reshape_smooth_context->geometry;
643 BLI_assert(subdiv_loop_index < geometry.corner_verts.size());
644
645 geometry.corner_verts[subdiv_loop_index] = subdiv_vert_index;
646
647 const int first_grid_index = reshape_context->base_faces[coarse_face_index].start();
648 geometry.corner_grid_indices[subdiv_loop_index] = first_grid_index + coarse_corner;
649}
650
651static void foreach_vert_of_loose_edge(const bke::subdiv::ForeachContext *foreach_context,
652 void * /*tls*/,
653 const int /*coarse_edge_index*/,
654 const float /*u*/,
655 const int vert_index)
656{
657 MultiresReshapeSmoothContext *reshape_smooth_context =
658 static_cast<MultiresReshapeSmoothContext *>(foreach_context->user_data);
659
660 /* NOTE: This runs in a separate pass after every other vertex callback, so it is fine to
661 * override the sharpness which may have been set from the vertex crease. */
662 if (!reshape_smooth_context->geometry.vertices[vert_index].grid_coords.is_empty()) {
663 reshape_smooth_context->geometry.vert_sharpness[vert_index] = OPENSUBDIV_SHARPNESS_INFINITE;
664 }
665}
666
667static void store_edge(MultiresReshapeSmoothContext *reshape_smooth_context,
668 const int subdiv_v1,
669 const int subdiv_v2,
670 const float crease)
671{
672 /* This is a bit overhead to use atomics in such a simple function called from many threads,
673 * but this allows to save quite measurable amount of memory. */
674 const int edge_index = atomic_fetch_and_add_z(&reshape_smooth_context->geometry.num_edges, 1);
675 BLI_assert(edge_index < reshape_smooth_context->geometry.max_edges);
676
677 reshape_smooth_context->geometry.edges[edge_index] = int2(subdiv_v1, subdiv_v2);
678 reshape_smooth_context->geometry.edge_sharpness[edge_index] = bke::subdiv::crease_to_sharpness(
679 crease);
680}
681
682static void foreach_edge(const bke::subdiv::ForeachContext *foreach_context,
683 void * /*tls*/,
684 const int coarse_edge_index,
685 const int /*subdiv_edge_index*/,
686 const bool is_loose,
687 const int subdiv_v1,
688 const int subdiv_v2)
689{
690 MultiresReshapeSmoothContext *reshape_smooth_context =
691 static_cast<MultiresReshapeSmoothContext *>(foreach_context->user_data);
692
693 if (reshape_smooth_context->smoothing_type == MultiresSubdivideModeType::Linear) {
694 if (!is_loose) {
695 store_edge(reshape_smooth_context, subdiv_v1, subdiv_v2, 1.0f);
696 }
697 return;
698 }
699
700 /* Ignore all inner face edges as they have sharpness of zero. */
701 if (coarse_edge_index == ORIGINDEX_NONE) {
702 return;
703 }
704 /* Ignore all loose edges as well, as they are not communicated to the OpenSubdiv. */
705 if (!reshape_smooth_context->loose_base_edges.is_empty()) {
706 if (reshape_smooth_context->loose_base_edges[coarse_edge_index]) {
707 return;
708 }
709 }
710 /* Edges without crease are to be ignored as well. */
711 const float crease = get_effective_crease(reshape_smooth_context, coarse_edge_index);
712 if (crease == 0.0f) {
713 return;
714 }
715 store_edge(reshape_smooth_context, subdiv_v1, subdiv_v2, crease);
716}
717
718static void geometry_init_loose_information(MultiresReshapeSmoothContext *reshape_smooth_context)
719{
720 const MultiresReshapeContext *reshape_context = reshape_smooth_context->reshape_context;
721 const Mesh *base_mesh = reshape_context->base_mesh;
722
723 const IndexMask &loose_edges = base_mesh->loose_edges();
724 if (!loose_edges.is_empty()) {
725 reshape_smooth_context->loose_base_edges.resize(base_mesh->edges_num);
726 loose_edges.to_bits(reshape_smooth_context->loose_base_edges);
727 }
728
729 IndexMaskMemory memory;
730 const IndexMask non_loose_edges = loose_edges.complement(IndexRange(base_mesh->edges_num),
731 memory);
732 int num_used_edges = 0;
733 non_loose_edges.foreach_index([&](const int edge) {
734 const float crease = get_effective_crease(reshape_smooth_context, edge);
735 if (crease == 0.0f) {
736 return;
737 }
738 num_used_edges++;
739 });
740
741 const int resolution = get_reshape_level_resolution(reshape_context);
742 const int num_subdiv_vertices_per_base_edge = resolution - 2;
743 reshape_smooth_context->geometry.max_edges = num_used_edges *
744 (num_subdiv_vertices_per_base_edge + 1);
745}
746
747static void geometry_create(MultiresReshapeSmoothContext *reshape_smooth_context)
748{
749 const MultiresReshapeContext *reshape_context = reshape_smooth_context->reshape_context;
750
751 bke::subdiv::ForeachContext foreach_context{};
752 foreach_context.topology_info = foreach_topology_info;
753 foreach_context.vert_inner = foreach_vert_inner;
754 foreach_context.vert_every_corner = foreach_vert_every_corner;
755 foreach_context.vert_every_edge = foreach_vert_every_edge;
756 foreach_context.loop = foreach_loop;
757 foreach_context.vert_of_loose_edge = foreach_vert_of_loose_edge;
758 foreach_context.edge = foreach_edge;
759 foreach_context.user_data = reshape_smooth_context;
760
761 geometry_init_loose_information(reshape_smooth_context);
762
763 bke::subdiv::ToMeshSettings mesh_settings;
764 mesh_settings.resolution = get_reshape_level_resolution(reshape_context);
765 mesh_settings.use_optimal_display = false;
766
767 /* TODO(sergey): Tell the foreach() to ignore loose vertices. */
768 bke::subdiv::foreach_subdiv_geometry(
769 reshape_context->subdiv, &foreach_context, &mesh_settings, reshape_context->base_mesh);
770}
771
773
774/* -------------------------------------------------------------------- */
777
778static bool specifies_full_topology(const OpenSubdiv_Converter * /*converter*/)
779{
780 return false;
781}
782
783static void converter_init(const MultiresReshapeSmoothContext *reshape_smooth_context,
784 OpenSubdiv_Converter *converter)
785{
786 const MultiresReshapeContext *reshape_context = reshape_smooth_context->reshape_context;
787 const bke::subdiv::Settings &settings = reshape_context->subdiv->settings;
788
789 converter->scheme_type = OSD_SCHEME_CATMARK;
791 bke::subdiv::converter_vtx_boundary_interpolation_from_settings(&settings));
793 bke::subdiv::converter_fvar_linear_from_settings(&settings));
794 converter->specifiesFullTopology = specifies_full_topology;
795
796 converter->verts_num = reshape_smooth_context->geometry.vertices.size();
797
798 /* Only the first #num_edges entries of the sparse edge storage were filled in. */
799 const int edges_num = int(reshape_smooth_context->geometry.num_edges);
800 converter->edges = reshape_smooth_context->geometry.edges.as_span()
801 .take_front(edges_num)
802 .cast<std::pair<int, int>>();
803 converter->face_offsets = reshape_smooth_context->geometry.face_offsets.as_span();
804 converter->corner_verts = reshape_smooth_context->geometry.corner_verts;
805 converter->edge_sharpness = reshape_smooth_context->geometry.edge_sharpness.as_span().take_front(
806 edges_num);
807 converter->vert_sharpness = reshape_smooth_context->geometry.vert_sharpness;
808
809 converter->getFaceEdges = nullptr;
810
811 converter->getNumEdgeFaces = nullptr;
812 converter->getEdgeFaces = nullptr;
813
814 converter->getNumVertexEdges = nullptr;
815 converter->getVertexEdges = nullptr;
816 converter->getNumVertexFaces = nullptr;
817 converter->getVertexFaces = nullptr;
818
819 converter->getNumUVLayers = nullptr;
820 converter->precalcUVLayer = nullptr;
821 converter->finishUVLayer = nullptr;
822 converter->getNumUVCoordinates = nullptr;
823 converter->getFaceCornerUVIndex = nullptr;
824
825 converter->freeUserData = nullptr;
826
827 converter->user_data = (void *)reshape_smooth_context;
828}
829
830/* Create subdiv descriptor created for topology at a reshape level. */
831static void reshape_subdiv_create(MultiresReshapeSmoothContext *reshape_smooth_context)
832{
833 const MultiresReshapeContext *reshape_context = reshape_smooth_context->reshape_context;
834 const bke::subdiv::Settings *settings = &reshape_context->subdiv->settings;
835
836 OpenSubdiv_Converter converter;
837 converter_init(reshape_smooth_context, &converter);
838
839 bke::subdiv::Subdiv *reshape_subdiv = bke::subdiv::new_from_converter(settings, &converter);
840
841 OpenSubdiv_EvaluatorSettings evaluator_settings = {0};
842 bke::subdiv::eval_begin(
843 reshape_subdiv, bke::subdiv::SUBDIV_EVALUATOR_TYPE_CPU, nullptr, &evaluator_settings);
844
845 reshape_smooth_context->reshape_subdiv = reshape_subdiv;
846
847 bke::subdiv::converter_free(&converter);
848}
849
850/* Callback to provide coarse position for subdivision surface topology at a reshape level. */
851using ReshapeSubdivCoarsePositionCb = void(
852 const MultiresReshapeSmoothContext *reshape_smooth_context, const Vertex *vert, float3 &r_P);
853
854/* Refine subdivision surface topology at a reshape level for new coarse vertices positions. */
855static void reshape_subdiv_refine(const MultiresReshapeSmoothContext *reshape_smooth_context,
856 ReshapeSubdivCoarsePositionCb coarse_position_cb)
857{
858 bke::subdiv::Subdiv *reshape_subdiv = reshape_smooth_context->reshape_subdiv;
859
860 /* TODO(sergey): For non-trivial coarse_position_cb we should multi-thread this loop. */
861
862 const int num_vertices = reshape_smooth_context->geometry.vertices.size();
863 for (int i = 0; i < num_vertices; ++i) {
864 const Vertex *vert = &reshape_smooth_context->geometry.vertices[i];
865 float3 P;
866 coarse_position_cb(reshape_smooth_context, vert, P);
867 reshape_subdiv->evaluator->eval_output->setCoarsePositions(P, i, 1);
868 }
869 reshape_subdiv->evaluator->eval_output->refine();
870}
871
872BLI_INLINE const GridCoord *reshape_subdiv_refine_vert_grid_coord(const Vertex *vert)
873{
874 if (vert->grid_coords.is_empty()) {
875 /* This is a loose vertex, the coordinate is not important. */
876 /* TODO(sergey): Once the subdiv_foreach() supports properly ignoring loose elements this
877 * should become an assert instead. */
878 return nullptr;
879 }
880 /* NOTE: All grid coordinates will point to the same object position, so can be simple and use
881 * first grid coordinate. */
882 return vert->grid_coords.data();
883}
884
885/* Version of reshape_subdiv_refine() which uses coarse position from original grids. */
886static void reshape_subdiv_refine_orig_P(
887 const MultiresReshapeSmoothContext *reshape_smooth_context, const Vertex *vert, float3 &r_P)
888{
889 const MultiresReshapeContext *reshape_context = reshape_smooth_context->reshape_context;
890 const GridCoord *grid_coord = reshape_subdiv_refine_vert_grid_coord(vert);
891
892 /* Check whether this is a loose vertex. */
893 if (grid_coord == nullptr) {
894 r_P = float3(0.0f);
895 return;
896 }
897
898 float3 limit_P;
899 float3x3 tangent_matrix;
901 reshape_context, grid_coord, limit_P, tangent_matrix);
902
903 const ReshapeConstGridElement orig_grid_element =
904 multires_reshape_orig_grid_element_for_grid_coord(reshape_context, grid_coord);
905
906 const float3 D = math::transform_direction(tangent_matrix, orig_grid_element.displacement);
907
908 r_P = limit_P + D;
909}
910static void reshape_subdiv_refine_orig(const MultiresReshapeSmoothContext *reshape_smooth_context)
911{
912 reshape_subdiv_refine(reshape_smooth_context, reshape_subdiv_refine_orig_P);
913}
914
915/* Version of reshape_subdiv_refine() which uses coarse position from final grids. */
916static void reshape_subdiv_refine_final_P(
917 const MultiresReshapeSmoothContext *reshape_smooth_context, const Vertex *vert, float3 &r_P)
918{
919 const MultiresReshapeContext *reshape_context = reshape_smooth_context->reshape_context;
920 const GridCoord *grid_coord = reshape_subdiv_refine_vert_grid_coord(vert);
921
922 /* Check whether this is a loose vertex. */
923 if (grid_coord == nullptr) {
924 r_P = float3(0.0f);
925 return;
926 }
927
928 const ReshapeGridElement grid_element = multires_reshape_grid_element_for_grid_coord(
929 reshape_context, grid_coord);
930
931 /* NOTE: At this point in reshape/propagate pipeline grid displacement is actually storing object
932 * vertices coordinates. */
933 r_P = *grid_element.displacement;
934}
935static void reshape_subdiv_refine_final(const MultiresReshapeSmoothContext *reshape_smooth_context)
936{
937 reshape_subdiv_refine(reshape_smooth_context, reshape_subdiv_refine_final_P);
938}
939
940static void reshape_subdiv_evaluate_limit_at_grid(
941 const MultiresReshapeSmoothContext *reshape_smooth_context,
942 const PTexCoord *ptex_coord,
943 const GridCoord *grid_coord,
944 float3 &limit_P,
945 float3x3 &r_tangent_matrix)
946{
947 const MultiresReshapeContext *reshape_context = reshape_smooth_context->reshape_context;
948
949 float3 dPdu;
950 float3 dPdv;
951 bke::subdiv::eval_limit_point_and_derivatives(reshape_smooth_context->reshape_subdiv,
952 ptex_coord->ptex_face_index,
953 ptex_coord->u,
954 ptex_coord->v,
955 limit_P,
956 dPdu,
957 dPdv);
958
959 const int face_index = multires_reshape_grid_to_face_index(reshape_context,
960 grid_coord->grid_index);
961 const int corner = multires_reshape_grid_to_corner(reshape_context, grid_coord->grid_index);
963 reshape_context, face_index, corner, dPdu, dPdv, r_tangent_matrix);
964}
965
967
968/* -------------------------------------------------------------------- */
971
972static LinearGridElement linear_grid_element_orig_get(
973 const MultiresReshapeSmoothContext *reshape_smooth_context, const GridCoord *grid_coord)
974{
975 const MultiresReshapeContext *reshape_context = reshape_smooth_context->reshape_context;
976 const ReshapeConstGridElement orig_grid_element =
977 multires_reshape_orig_grid_element_for_grid_coord(reshape_context, grid_coord);
978
979 LinearGridElement linear_grid_element;
980 linear_grid_element_init(&linear_grid_element);
981
982 linear_grid_element.mask = orig_grid_element.mask;
983
984 return linear_grid_element;
985}
986
987static LinearGridElement linear_grid_element_final_get(
988 const MultiresReshapeSmoothContext *reshape_smooth_context, const GridCoord *grid_coord)
989{
990 const MultiresReshapeContext *reshape_context = reshape_smooth_context->reshape_context;
991 const ReshapeGridElement final_grid_element = multires_reshape_grid_element_for_grid_coord(
992 reshape_context, grid_coord);
993
994 LinearGridElement linear_grid_element;
995 linear_grid_element_init(&linear_grid_element);
996
997 if (final_grid_element.mask != nullptr) {
998 linear_grid_element.mask = *final_grid_element.mask;
999 }
1000
1001 return linear_grid_element;
1002}
1003
1004/* Interpolate difference of the linear data.
1005 *
1006 * Will access final data and original data at the grid elements at the reshape level,
1007 * calculate difference between final and original, and linearly interpolate to get value at the
1008 * top level. */
1009static void linear_grid_element_delta_interpolate(
1010 const MultiresReshapeSmoothContext *reshape_smooth_context,
1011 const GridCoord *grid_coord,
1012 LinearGridElement *result)
1013{
1014 const MultiresReshapeContext *reshape_context = reshape_smooth_context->reshape_context;
1015
1016 const int reshape_level = reshape_context->reshape.level;
1017 const int reshape_level_grid_size = bke::subdiv::grid_size_from_level(reshape_level);
1018 const int reshape_level_grid_size_1 = reshape_level_grid_size - 1;
1019 const float reshape_level_grid_size_1_inv = 1.0f / float(reshape_level_grid_size_1);
1020
1021 const float x_f = grid_coord->u * reshape_level_grid_size_1;
1022 const float y_f = grid_coord->v * reshape_level_grid_size_1;
1023
1024 const int x_i = x_f;
1025 const int y_i = y_f;
1026 const int x_n_i = (x_i == reshape_level_grid_size - 1) ? (x_i) : (x_i + 1);
1027 const int y_n_i = (y_i == reshape_level_grid_size - 1) ? (y_i) : (y_i + 1);
1028
1029 const int corners_int_coords[4][2] = {{x_i, y_i}, {x_n_i, y_i}, {x_n_i, y_n_i}, {x_i, y_n_i}};
1030
1031 LinearGridElement corner_elements[4];
1032 for (int i = 0; i < 4; ++i) {
1033 GridCoord corner_grid_coord;
1034 corner_grid_coord.grid_index = grid_coord->grid_index;
1035 corner_grid_coord.u = corners_int_coords[i][0] * reshape_level_grid_size_1_inv;
1036 corner_grid_coord.v = corners_int_coords[i][1] * reshape_level_grid_size_1_inv;
1037
1038 const LinearGridElement orig_element = linear_grid_element_orig_get(reshape_smooth_context,
1039 &corner_grid_coord);
1040 const LinearGridElement final_element = linear_grid_element_final_get(reshape_smooth_context,
1041 &corner_grid_coord);
1042 linear_grid_element_sub(&corner_elements[i], &final_element, &orig_element);
1043 }
1044
1045 const float u = x_f - x_i;
1046 const float v = y_f - y_i;
1047 const float weights[4] = {(1.0f - u) * (1.0f - v), u * (1.0f - v), u * v, (1.0f - u) * v};
1048
1049 linear_grid_element_interpolate(result, corner_elements, weights);
1050}
1051
1052static void evaluate_linear_delta_grids(MultiresReshapeSmoothContext *reshape_smooth_context)
1053{
1054 const MultiresReshapeContext *reshape_context = reshape_smooth_context->reshape_context;
1055 const int num_grids = reshape_context->num_grids;
1056 const int top_level = reshape_context->top.level;
1057
1058 linear_grids_allocate(&reshape_smooth_context->linear_delta_grids, num_grids, top_level);
1059
1060 foreach_toplevel_grid_coord(reshape_smooth_context,
1061 [&](const PTexCoord * /*ptex_coord*/, const GridCoord *grid_coord) {
1062 LinearGridElement *linear_delta_element = linear_grid_element_get(
1063 &reshape_smooth_context->linear_delta_grids, grid_coord);
1064
1065 linear_grid_element_delta_interpolate(
1066 reshape_smooth_context, grid_coord, linear_delta_element);
1067 });
1068}
1069
1070static void propagate_linear_data_delta(MultiresReshapeSmoothContext *reshape_smooth_context,
1071 ReshapeGridElement *final_grid_element,
1072 const GridCoord *grid_coord)
1073{
1074 const MultiresReshapeContext *reshape_context = reshape_smooth_context->reshape_context;
1075
1076 LinearGridElement *linear_delta_element = linear_grid_element_get(
1077 &reshape_smooth_context->linear_delta_grids, grid_coord);
1078
1079 const ReshapeConstGridElement orig_grid_element =
1080 multires_reshape_orig_grid_element_for_grid_coord(reshape_context, grid_coord);
1081
1082 if (final_grid_element->mask != nullptr) {
1083 *final_grid_element->mask = clamp_f(
1084 orig_grid_element.mask + linear_delta_element->mask, 0.0f, 1.0f);
1085 }
1086}
1087
1089
1090/* -------------------------------------------------------------------- */
1093
1094static void evaluate_base_surface_grids(MultiresReshapeSmoothContext *reshape_smooth_context)
1095{
1096 foreach_toplevel_grid_coord(
1097 reshape_smooth_context, [&](const PTexCoord *ptex_coord, const GridCoord *grid_coord) {
1098 float3 limit_P;
1099 float3x3 tangent_matrix;
1100 reshape_subdiv_evaluate_limit_at_grid(
1101 reshape_smooth_context, ptex_coord, grid_coord, limit_P, tangent_matrix);
1102
1103 base_surface_grids_write(reshape_smooth_context, grid_coord, limit_P, tangent_matrix);
1104 });
1105}
1106
1108
1109/* -------------------------------------------------------------------- */
1112
1113/* Evaluate final position of the original (pre-sculpt-edit) point position at a given grid
1114 * coordinate. */
1115static void evaluate_final_original_point(
1116 const MultiresReshapeSmoothContext *reshape_smooth_context,
1117 const GridCoord *grid_coord,
1118 float3 &r_orig_final_P)
1119{
1120 const MultiresReshapeContext *reshape_context = reshape_smooth_context->reshape_context;
1121
1122 /* Element of an original MDISPS grid. */
1123 const ReshapeConstGridElement orig_grid_element =
1124 multires_reshape_orig_grid_element_for_grid_coord(reshape_context, grid_coord);
1125
1126 /* Limit surface of the base mesh. */
1127 float3 base_mesh_limit_P;
1128 float3x3 base_mesh_tangent_matrix;
1130 reshape_context, grid_coord, base_mesh_limit_P, base_mesh_tangent_matrix);
1131
1132 /* Convert original displacement from tangent space to object space. */
1133 const float3 orig_displacement = math::transform_direction(base_mesh_tangent_matrix,
1134 orig_grid_element.displacement);
1135
1136 /* Final point = limit surface + displacement. */
1137 r_orig_final_P = base_mesh_limit_P + orig_displacement;
1138}
1139
1140static void evaluate_higher_grid_positions_with_details(
1141 MultiresReshapeSmoothContext *reshape_smooth_context)
1142{
1143 const MultiresReshapeContext *reshape_context = reshape_smooth_context->reshape_context;
1144 foreach_toplevel_grid_coord(
1145 reshape_smooth_context, [&](const PTexCoord *ptex_coord, const GridCoord *grid_coord) {
1146 /* Position of the original vertex at top level. */
1147 float3 orig_final_P;
1148 evaluate_final_original_point(reshape_smooth_context, grid_coord, orig_final_P);
1149
1150 /* Original surface point on sculpt level (sculpt level before edits in sculpt mode). */
1151 const SurfacePoint *orig_sculpt_point = base_surface_grids_read(reshape_smooth_context,
1152 grid_coord);
1153
1154 /* Difference between original top level and original sculpt level in object space. */
1155 const float3 original_detail_delta = orig_final_P - orig_sculpt_point->P;
1156
1157 /* Difference between original top level and original sculpt level in tangent space of
1158 * original sculpt level. */
1159 const float3x3 original_sculpt_tangent_matrix_inv = math::invert(
1160 orig_sculpt_point->tangent_matrix);
1161 float3 original_detail_delta_tangent = math::transform_direction(
1162 original_sculpt_tangent_matrix_inv, original_detail_delta);
1163
1164 /* Limit surface of smoothed (subdivided) edited sculpt level. */
1165 float3 smooth_limit_P;
1166 float3x3 smooth_tangent_matrix;
1167 reshape_subdiv_evaluate_limit_at_grid(
1168 reshape_smooth_context, ptex_coord, grid_coord, smooth_limit_P, smooth_tangent_matrix);
1169
1170 /* Add original detail to the smoothed surface. */
1171 float3 smooth_delta = math::transform_direction(smooth_tangent_matrix,
1172 original_detail_delta_tangent);
1173
1174 /* Grid element of the result.
1175 *
1176 * NOTE: Displacement is storing object space coordinate. */
1177 ReshapeGridElement grid_element = multires_reshape_grid_element_for_grid_coord(
1178 reshape_context, grid_coord);
1179
1180 *grid_element.displacement = smooth_limit_P + smooth_delta;
1181
1182 /* Propagate non-coordinate data. */
1183 propagate_linear_data_delta(reshape_smooth_context, &grid_element, grid_coord);
1184 });
1185}
1186
1187static void evaluate_higher_grid_positions(MultiresReshapeSmoothContext *reshape_smooth_context)
1188{
1189 const MultiresReshapeContext *reshape_context = reshape_smooth_context->reshape_context;
1190 foreach_toplevel_grid_coord(
1191 reshape_smooth_context, [&](const PTexCoord *ptex_coord, const GridCoord *grid_coord) {
1192 bke::subdiv::Subdiv *reshape_subdiv = reshape_smooth_context->reshape_subdiv;
1193
1194 ReshapeGridElement grid_element = multires_reshape_grid_element_for_grid_coord(
1195 reshape_context, grid_coord);
1196
1197 /* Surface. */
1198 const float3 P = bke::subdiv::eval_limit_point(
1199 reshape_subdiv, ptex_coord->ptex_face_index, ptex_coord->u, ptex_coord->v);
1200
1201 *grid_element.displacement = P;
1202
1203 /* Propagate non-coordinate data. */
1204 propagate_linear_data_delta(reshape_smooth_context, &grid_element, grid_coord);
1205 });
1206}
1207
1208#endif
1209
1211
1212/* -------------------------------------------------------------------- */
1215
1217 const MultiresReshapeContext *reshape_context)
1218{
1219#ifdef WITH_OPENSUBDIV
1220 const int level_difference = (reshape_context->top.level - reshape_context->reshape.level);
1221 if (level_difference == 0) {
1222 /* Early output. */
1223 return;
1224 }
1225
1226 const MultiresSubdivideModeType smoothing_type = reshape_context->subdiv->settings.is_simple ?
1229 MultiresReshapeSmoothContext reshape_smooth_context(reshape_context, smoothing_type);
1230 geometry_create(&reshape_smooth_context);
1231 evaluate_linear_delta_grids(&reshape_smooth_context);
1232
1233 reshape_subdiv_create(&reshape_smooth_context);
1234
1235 base_surface_grids_allocate(&reshape_smooth_context);
1236 reshape_subdiv_refine_orig(&reshape_smooth_context);
1237 evaluate_base_surface_grids(&reshape_smooth_context);
1238
1239 reshape_subdiv_refine_final(&reshape_smooth_context);
1240 evaluate_higher_grid_positions_with_details(&reshape_smooth_context);
1241#else
1242 UNUSED_VARS(reshape_context);
1243#endif
1244}
1245
1247 const MultiresSubdivideModeType mode)
1248{
1249#ifdef WITH_OPENSUBDIV
1250 const int level_difference = (reshape_context->top.level - reshape_context->reshape.level);
1251 if (level_difference == 0) {
1252 /* Early output. */
1253 return;
1254 }
1255
1256 MultiresReshapeSmoothContext reshape_smooth_context(reshape_context, mode);
1257 geometry_create(&reshape_smooth_context);
1258 evaluate_linear_delta_grids(&reshape_smooth_context);
1259
1260 reshape_subdiv_create(&reshape_smooth_context);
1261
1262 reshape_subdiv_refine_final(&reshape_smooth_context);
1263 evaluate_higher_grid_positions(&reshape_smooth_context);
1264#else
1265 UNUSED_VARS(reshape_context, mode);
1266#endif
1267}
1268
1270
1271} // namespace blender
CustomData interface, see also DNA_customdata_types.h.
#define ORIGINDEX_NONE
#define D
Read Blender memory allocation and freeing.
Provides wrapper around system-specific atomic primitives, and some extensions (faked-atomic operatio...
ATOMIC_INLINE size_t atomic_fetch_and_add_z(size_t *p, size_t x)
#define UNUSED_VARS(...)
#define BLI_INLINE
#define BLI_assert(a)
#define ELEM(...)
MatBase< 3, 3 > float3x3
VecBase< float, 3 > float3
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
void free(Subdiv *subdiv)
Definition subdiv.cc:192
BLI_INLINE int grid_size_from_level(int level)
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
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void multires_reshape_smooth_object_grids(const MultiresReshapeContext *reshape_context, MultiresSubdivideModeType mode)
int multires_reshape_grid_to_face_index(const MultiresReshapeContext *reshape_context, int grid_index)
void multires_reshape_smooth_object_grids_with_details(const MultiresReshapeContext *reshape_context)
GridCoord multires_reshape_ptex_coord_to_grid(const MultiresReshapeContext *reshape_context, const PTexCoord *ptex_coord)
ATTR_WARN_UNUSED_RESULT const BMVert * v
int multires_reshape_grid_to_corner(const MultiresReshapeContext *reshape_context, int grid_index)
MultiresSubdivideModeType
void multires_reshape_tangent_matrix_for_corner(const MultiresReshapeContext *reshape_context, int face_index, int corner, const float3 &dPdu, const float3 &dPdv, float3x3 &r_tangent_matrix)
MatBase< float, 3, 3 > float3x3
ReshapeGridElement multires_reshape_grid_element_for_grid_coord(const MultiresReshapeContext *reshape_context, const GridCoord *grid_coord)
ReshapeConstGridElement multires_reshape_orig_grid_element_for_grid_coord(const MultiresReshapeContext *reshape_context, const GridCoord *grid_coord)
void multires_reshape_evaluate_base_mesh_limit_at_grid(const MultiresReshapeContext *reshape_context, const GridCoord *grid_coord, float3 &r_P, float3x3 &r_tangent_matrix)
VecBase< float, 3 > float3
MINLINE float clamp_f(float value, float min, float max)
void vert(const int vert_id, float4 &position)
OpenSubdiv_FVarLinearInterpolation
OpenSubdiv_VtxBoundaryInterpolation
@ OSD_SCHEME_CATMARK
constexpr float OPENSUBDIV_SHARPNESS_INFINITE
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
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)
struct blender::MultiresReshapeContext::@021252173372013124221256162374334156074326245244 top
struct blender::MultiresReshapeContext::@034344347246210140240253130013212240027344072112 reshape