Blender V5.3
multires_reshape_ccg.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 <algorithm>
12
13#include "BLI_task.hh"
14
15#include "BKE_ccg.hh"
16#include "BKE_subdiv_ccg.hh"
17
18namespace blender {
19
21 SubdivCCG *subdiv_ccg)
22{
23 const CCGKey reshape_level_key = BKE_subdiv_ccg_key(*subdiv_ccg, reshape_context->reshape.level);
24
25 const int reshape_grid_size = reshape_context->reshape.grid_size;
26 const float reshape_grid_size_1_inv = 1.0f / (float(reshape_grid_size) - 1.0f);
27
28 const Span<float3> positions = subdiv_ccg->positions;
29 const Span<float> masks = subdiv_ccg->masks;
30
31 /* NOTE: The sculpt mode might have SubdivCCG's data out of sync from what is stored in
32 * the original object. This happens in the following scenario:
33 *
34 * - User enters sculpt mode of the default cube object.
35 * - Sculpt mode creates new `layer`
36 * - User does some strokes.
37 * - User used undo until sculpt mode is exited.
38 *
39 * In an ideal world the sculpt mode will take care of keeping CustomData and CCG layers in
40 * sync by doing proper pushes to a local sculpt undo stack.
41 *
42 * Since the proper solution needs time to be implemented, consider the target object
43 * the source of truth of which data layers are to be updated during reshape. This means,
44 * for example, that if the undo system says object does not have paint mask layer, it is
45 * not to be updated.
46 *
47 * This is fragile logic, and is only working correctly because the code path is only
48 * used by sculpt changes. In other use cases the code might not catch inconsistency and
49 * silently make the wrong decision. */
50 /* NOTE: There is a known bug in Undo code that results in first Sculpt step
51 * after a Memfile one to never be undone (see #83806). This might be the root cause of
52 * this inconsistency. */
53
54 /* Each grid writes to its own displacement and mask grid, so there are no write conflicts.
55 * Balance the amount of work per task, since the work per grid depends on the grid size. */
56 const int grain_size = std::max(1, 2048 / (reshape_grid_size * reshape_grid_size));
57
59 IndexRange(subdiv_ccg->grids_num), grain_size, [&](const IndexRange range) {
60 for (const int grid_index : range) {
61 for (int y = 0; y < reshape_grid_size; ++y) {
62 const float v = float(y) * reshape_grid_size_1_inv;
63 for (int x = 0; x < reshape_grid_size; ++x) {
64 const float u = float(x) * reshape_grid_size_1_inv;
65 const int vert = bke::ccg::grid_xy_to_vert(reshape_level_key, grid_index, x, y);
66
67 GridCoord grid_coord;
68 grid_coord.grid_index = grid_index;
69 grid_coord.u = u;
70 grid_coord.v = v;
71
72 ReshapeGridElement grid_element = multires_reshape_grid_element_for_grid_coord(
73 reshape_context, &grid_coord);
74
75 BLI_assert(grid_element.displacement != nullptr);
76 *grid_element.displacement = positions[vert];
77
78 if (!masks.is_empty() && grid_element.mask != nullptr) {
79 *grid_element.mask = masks[vert];
80 }
81 }
82 }
83 }
84 });
85
86 return true;
87}
88
89} // namespace blender
constexpr IndexRange()=default
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.
CCGKey BKE_subdiv_ccg_key(const SubdivCCG &subdiv_ccg, int level)
bool multires_reshape_assign_final_coords_from_ccg(const MultiresReshapeContext *reshape_context, SubdivCCG *subdiv_ccg)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct blender::MultiresReshapeContext::@034344347246210140240253130013212240027344072112 reshape
Array< float3 > positions
Array< float > masks