Multires: Fix spike issues when sculpting on triangles
[blender.git] / source / blender / blenkernel / intern / multires_reshape.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software  Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2018 Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Sergey Sharybin.
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/blenkernel/intern/multires_reshape.c
27  *  \ingroup bke
28  */
29
30 #include "MEM_guardedalloc.h"
31
32 #include "DNA_mesh_types.h"
33 #include "DNA_meshdata_types.h"
34 #include "DNA_scene_types.h"
35
36 #include "BLI_utildefines.h"
37 #include "BLI_math_vector.h"
38 #include "BLI_task.h"
39
40 #include "BKE_ccg.h"
41 #include "BKE_library.h"
42 #include "BKE_mesh.h"
43 #include "BKE_mesh_runtime.h"
44 #include "BKE_modifier.h"
45 #include "BKE_multires.h"
46 #include "BKE_subdiv.h"
47 #include "BKE_subdiv_ccg.h"
48 #include "BKE_subdiv_eval.h"
49 #include "BKE_subdiv_foreach.h"
50 #include "BKE_subdiv_mesh.h"
51
52 #include "DEG_depsgraph_query.h"
53
54 static void multires_reshape_init_mmd(
55         MultiresModifierData *reshape_mmd,
56         const MultiresModifierData *mmd)
57 {
58         *reshape_mmd = *mmd;
59 }
60
61 static void multires_reshape_init_mmd_top_level(
62         MultiresModifierData *reshape_mmd,
63         const MultiresModifierData *mmd)
64 {
65         *reshape_mmd = *mmd;
66         reshape_mmd->lvl = reshape_mmd->totlvl;
67 }
68
69 /* =============================================================================
70  * General reshape implementation, reused by all particular cases.
71  */
72
73 typedef struct MultiresReshapeContext {
74         Subdiv *subdiv;
75         const Mesh *coarse_mesh;
76         MDisps *mdisps;
77         GridPaintMask *grid_paint_mask;
78         int top_grid_size;
79         int top_level;
80 } MultiresReshapeContext;
81
82 static void multires_reshape_allocate_displacement_grid(
83         MDisps *displacement_grid, const int level)
84 {
85         const int grid_size = BKE_subdiv_grid_size_from_level(level);
86         const int grid_area = grid_size * grid_size;
87         float (*disps)[3] = MEM_calloc_arrayN(
88                 grid_area, 3 * sizeof(float), "multires disps");
89         displacement_grid->disps = disps;
90         displacement_grid->totdisp = grid_area;
91         displacement_grid->level = level;
92 }
93
94 static void multires_reshape_ensure_displacement_grid(
95         MDisps *displacement_grid, const int level)
96 {
97         if (displacement_grid->disps != NULL) {
98                 return;
99         }
100         multires_reshape_allocate_displacement_grid(
101         displacement_grid, level);
102 }
103
104 static void multires_reshape_ensure_displacement_grids(
105         Mesh *mesh,
106         const int grid_level)
107 {
108         const int num_grids = mesh->totloop;
109         MDisps *mdisps = CustomData_get_layer(&mesh->ldata, CD_MDISPS);
110         for (int grid_index = 0; grid_index < num_grids; grid_index++) {
111                 multires_reshape_ensure_displacement_grid(
112                         &mdisps[grid_index], grid_level);
113         }
114 }
115
116 static void multires_reshape_ensure_mask_grids(Mesh *mesh, const int grid_level)
117 {
118         GridPaintMask *grid_paint_masks =
119                 CustomData_get_layer(&mesh->ldata, CD_GRID_PAINT_MASK);
120         if (grid_paint_masks == NULL) {
121                 return;
122         }
123         const int num_grids = mesh->totloop;
124         const int grid_size = BKE_subdiv_grid_size_from_level(grid_level);
125         const int grid_area = grid_size * grid_size;
126         for (int grid_index = 0; grid_index < num_grids; grid_index++) {
127                 GridPaintMask *grid_paint_mask = &grid_paint_masks[grid_index];
128                 if (grid_paint_mask->level == grid_level) {
129                         continue;
130                 }
131                 grid_paint_mask->level = grid_level;
132                 if (grid_paint_mask->data) {
133                         MEM_freeN(grid_paint_mask->data);
134                 }
135                 grid_paint_mask->data = MEM_calloc_arrayN(
136                         grid_area, sizeof(float), "gpm.data");
137         }
138 }
139
140 static void multires_reshape_ensure_grids(Mesh *mesh, const int grid_level)
141 {
142         multires_reshape_ensure_displacement_grids(mesh, grid_level);
143         multires_reshape_ensure_mask_grids(mesh, grid_level);
144 }
145
146 static void multires_reshape_vertex_copy_to_next(
147         MultiresReshapeContext *ctx,
148         const MPoly *coarse_poly,
149         const int current_corner,
150         const MDisps *current_displacement_grid,
151         const GridPaintMask *current_mask_grid,
152         const int current_grid_x, const int current_grid_y)
153 {
154         const int grid_size = ctx->top_grid_size;
155         const int next_current_corner = (current_corner + 1) % coarse_poly->totloop;
156         const int next_grid_x = 0;
157         const int next_grid_y = current_grid_x;
158         const int current_index = current_grid_y * grid_size + current_grid_x;
159         const int next_index = next_grid_y * grid_size + next_grid_x;
160         /* Copy displacement. */
161         MDisps *next_displacement_grid = &ctx->mdisps[
162                 coarse_poly->loopstart + next_current_corner];
163         float *next_displacement = next_displacement_grid->disps[next_index];
164         copy_v3_v3(next_displacement,
165                    current_displacement_grid->disps[current_index]);
166         SWAP(float, next_displacement[0], next_displacement[1]);
167         next_displacement[0] = -next_displacement[0];
168         /* Copy mask, if exists. */
169         if (current_mask_grid != NULL) {
170                 GridPaintMask *next_mask_grid = &ctx->grid_paint_mask[
171                         coarse_poly->loopstart + next_current_corner];
172                 next_mask_grid->data[next_index] =
173                         current_mask_grid->data[current_index];
174         }
175 }
176
177 static void multires_reshape_vertex_copy_to_prev(
178         MultiresReshapeContext *ctx,
179         const MPoly *coarse_poly,
180         const int current_corner,
181         const MDisps *current_displacement_grid,
182         const GridPaintMask *current_mask_grid,
183         const int current_grid_x, const int current_grid_y)
184 {
185         const int grid_size = ctx->top_grid_size;
186         const int prev_current_corner =
187                 (current_corner - 1 + coarse_poly->totloop) % coarse_poly->totloop;
188         const int prev_grid_x = current_grid_y;
189         const int prev_grid_y = 0;
190         const int current_index = current_grid_y * grid_size + current_grid_x;
191         const int prev_index = prev_grid_y * grid_size + prev_grid_x;
192         /* Copy displacement. */
193         MDisps *prev_displacement_grid = &ctx->mdisps[
194                 coarse_poly->loopstart + prev_current_corner];
195         float *prev_displacement = prev_displacement_grid->disps[prev_index];
196         copy_v3_v3(prev_displacement,
197                    current_displacement_grid->disps[current_index]);
198         SWAP(float, prev_displacement[0], prev_displacement[1]);
199         prev_displacement[1] = -prev_displacement[1];
200         /* Copy mask, if exists. */
201         if (current_mask_grid != NULL) {
202                 GridPaintMask *prev_mask_grid = &ctx->grid_paint_mask[
203                         coarse_poly->loopstart + prev_current_corner];
204                 prev_mask_grid->data[prev_index] =
205                         current_mask_grid->data[current_index];
206         }
207 }
208
209 static void copy_boundary_displacement(
210         MultiresReshapeContext *ctx,
211         const MPoly *coarse_poly,
212         const int corner,
213         const int grid_x, const int grid_y,
214         const MDisps *displacement_grid,
215         const GridPaintMask *mask_grid)
216 {
217         if (grid_x == 0 && grid_y == 0) {
218                 for (int i = 0; i < coarse_poly->totloop; i++) {
219                         const int current_face_corner =
220                                 (corner + i) % coarse_poly->totloop;
221                         const int grid_index = coarse_poly->loopstart + current_face_corner;
222                         MDisps *current_displacement_grid = &ctx->mdisps[grid_index];
223                         GridPaintMask *current_mask_grid =
224                                 mask_grid != NULL ? &ctx->grid_paint_mask[grid_index]
225                                                   : NULL;
226                         multires_reshape_vertex_copy_to_next(
227                                 ctx,
228                                 coarse_poly,
229                                 current_face_corner,
230                                 current_displacement_grid,
231                                 current_mask_grid,
232                                 0, 0);
233                 }
234         }
235         else if (grid_x == 0) {
236                 multires_reshape_vertex_copy_to_prev(
237                         ctx,
238                         coarse_poly,
239                         corner,
240                         displacement_grid,
241                         mask_grid,
242                         grid_x, grid_y);
243         }
244         else if (grid_y == 0) {
245                 multires_reshape_vertex_copy_to_next(
246                         ctx,
247                         coarse_poly,
248                         corner,
249                         displacement_grid,
250                         mask_grid,
251                         grid_x, grid_y);
252         }
253 }
254
255 static void multires_reshape_vertex_from_final_data(
256         MultiresReshapeContext *ctx,
257         const int ptex_face_index,
258         const float u, const float v,
259         const int coarse_poly_index,
260         const int coarse_corner,
261         const float final_P[3], const float final_mask)
262 {
263         Subdiv *subdiv = ctx->subdiv;
264         const int grid_size = ctx->top_grid_size;
265         const Mesh *coarse_mesh = ctx->coarse_mesh;
266         const MPoly *coarse_mpoly = coarse_mesh->mpoly;
267         const MPoly *coarse_poly = &coarse_mpoly[coarse_poly_index];
268         const int loop_index = coarse_poly->loopstart + coarse_corner;
269         /* Evaluate limit surface. */
270         float P[3], dPdu[3], dPdv[3];
271         BKE_subdiv_eval_limit_point_and_derivatives(
272                 subdiv, ptex_face_index, u, v, P, dPdu, dPdv);
273         /* Get coordinate and corner configuration. */
274         float grid_u, grid_v;
275         MDisps *displacement_grid;
276         GridPaintMask *grid_paint_mask = NULL;
277         int face_corner = coarse_corner;
278         int grid_corner = 0;
279         int grid_index;
280         if (coarse_poly->totloop == 4) {
281                 float corner_u, corner_v;
282                 face_corner = BKE_subdiv_rotate_quad_to_corner(
283                         u, v, &corner_u, &corner_v);
284                 grid_corner = face_corner;
285                 grid_index = loop_index + face_corner;
286                 BKE_subdiv_ptex_face_uv_to_grid_uv(
287                         corner_u, corner_v, &grid_u, &grid_v);
288         }
289         else {
290                 grid_index = loop_index;
291                 BKE_subdiv_ptex_face_uv_to_grid_uv(u, v, &grid_u, &grid_v);
292         }
293         displacement_grid = &ctx->mdisps[grid_index];
294         if (ctx->grid_paint_mask != NULL) {
295                 grid_paint_mask = &ctx->grid_paint_mask[grid_index];
296                 BLI_assert(grid_paint_mask->level == displacement_grid->level);
297         }
298         /* Convert object coordinate to a tangent space of displacement grid. */
299         float D[3];
300         sub_v3_v3v3(D, final_P, P);
301         float tangent_matrix[3][3];
302         BKE_multires_construct_tangent_matrix(
303                 tangent_matrix, dPdu, dPdv, grid_corner);
304         float inv_tangent_matrix[3][3];
305         invert_m3_m3(inv_tangent_matrix, tangent_matrix);
306         float tangent_D[3];
307         mul_v3_m3v3(tangent_D, inv_tangent_matrix, D);
308         /* Write tangent displacement. */
309         const int grid_x = (grid_u * (grid_size - 1) + 0.5f);
310         const int grid_y = (grid_v * (grid_size - 1) + 0.5f);
311         const int index = grid_y * grid_size + grid_x;
312         copy_v3_v3(displacement_grid->disps[index], tangent_D);
313         /* Write mask grid. */
314         if (grid_paint_mask != NULL) {
315                 grid_paint_mask->data[index] = final_mask;
316         }
317         /* Copy boundary to the next/previous grids.
318          *
319          * NOTE: Only do this for quads faces, since other ones will call reshape
320          * for every boundary vertex, ensuring proper continuity across boundaries.
321         */
322         if (coarse_poly->totloop == 4) {
323                 copy_boundary_displacement(
324                         ctx, coarse_poly, face_corner, grid_x, grid_y,
325                         displacement_grid, grid_paint_mask);
326         }
327 }
328
329 /* =============================================================================
330  * Helpers to propagate displacement to higher levels.
331  */
332
333 typedef struct MultiresPropagateData {
334         /* Number of displacement grids. */
335         int num_grids;
336         /* Resolution level up to which displacement is known. */
337         int reshape_level;
338         /* Resolution up to which propagation is happening, affecting all the
339          * levels in [reshape_level + 1, top_level]. */
340         int top_level;
341         /* Grid sizes at the corresponding levels. */
342         int reshape_grid_size;
343         int top_grid_size;
344         /* Keys to access CCG at different levels. */
345         CCGKey reshape_level_key;
346         CCGKey top_level_key;
347         /* Original grid data, before any updates for reshape.
348          * Contains data at the reshape_level resolution level. */
349         CCGElem **orig_grids_data;
350         /* Custom data layers from a coarse mesh. */
351         MDisps *mdisps;
352         GridPaintMask *grid_paint_mask;
353 } MultiresPropagateData;
354
355 static CCGElem **allocate_grids(CCGKey *key, int num_grids)
356 {
357         CCGElem **grids = MEM_calloc_arrayN(
358                 num_grids, sizeof(CCGElem *), "reshape grids*");
359         for (int grid_index = 0; grid_index < num_grids; grid_index++) {
360                 grids[grid_index] = MEM_calloc_arrayN(
361                         key->elem_size,
362                         key->grid_area,
363                         "reshape orig_grids_data elems");
364         }
365         return grids;
366 }
367
368 static void free_grids(CCGElem **grids, int num_grids)
369 {
370         if (grids == NULL) {
371                 return;
372         }
373         for (int grid_index = 0; grid_index < num_grids; grid_index++) {
374                 MEM_freeN(grids[grid_index]);
375         }
376         MEM_freeN(grids);
377 }
378
379 /* Initialize element sizes and offsets. */
380 static void multires_reshape_init_key_layers(
381         CCGKey *key,
382         const MultiresPropagateData *data)
383 {
384         key->elem_size = 3 * sizeof(float);
385         if (data->grid_paint_mask != NULL) {
386                 key->mask_offset = 3 * sizeof(float);
387                 key->elem_size += sizeof(float);
388                 key->has_mask = true;
389         }
390         else {
391                 key->mask_offset = -1;
392                 key->has_mask = false;
393         }
394         /* We never have normals in original grids. */
395         key->normal_offset = -1;
396         key->has_normals = false;
397 }
398
399 /* Initialize key used to access reshape grids at given level. */
400 static void multires_reshape_init_level_key(
401         CCGKey *key,
402         const MultiresPropagateData *data,
403         const int level)
404 {
405         key->level = level;
406         /* Init layers. */
407         multires_reshape_init_key_layers(key, data);
408         /* By default, only 3 floats for coordinate, */
409         key->grid_size = BKE_subdiv_grid_size_from_level(key->level);
410         key->grid_area = key->grid_size * key->grid_size;
411         key->grid_bytes = key->elem_size * key->grid_area;
412 }
413
414 static void multires_reshape_store_original_grids(
415         MultiresPropagateData *data)
416 {
417         const int num_grids = data->num_grids;
418         /* Original data to be backed up. */
419         const MDisps *mdisps = data->mdisps;
420         const GridPaintMask *grid_paint_mask = data->grid_paint_mask;
421         /* Allocate grids for backup. */
422         CCGKey *orig_key = &data->reshape_level_key;
423         CCGElem **orig_grids_data = allocate_grids(orig_key, num_grids);
424         /* Fill in grids. */
425         const int orig_grid_size = data->reshape_grid_size;
426         const int top_grid_size = data->top_grid_size;
427         const int skip = (top_grid_size - 1) / (orig_grid_size - 1);
428         for (int grid_index = 0; grid_index < num_grids; grid_index++) {
429                 CCGElem *orig_grid = orig_grids_data[grid_index];
430                 for (int y = 0; y < orig_grid_size; y++) {
431                         const int top_y = y * skip;
432                         for (int x = 0; x < orig_grid_size; x++) {
433                                 const int top_x = x * skip;
434                                 const int top_index = top_y * top_grid_size + top_x;
435                                 memcpy(CCG_grid_elem_co(orig_key, orig_grid, x, y),
436                                        mdisps[grid_index].disps[top_index],
437                                        sizeof(float) * 3);
438                                 if (orig_key->has_mask) {
439                                         *CCG_grid_elem_mask(orig_key, orig_grid, x, y) =
440                                                 grid_paint_mask[grid_index].data[top_index];
441                                 }
442                         }
443                 }
444         }
445         /* Store in the context. */
446         data->orig_grids_data = orig_grids_data;
447 }
448
449 static void multires_reshape_propagate_prepare(
450         MultiresPropagateData *data,
451         Mesh *coarse_mesh,
452         const int reshape_level,
453         const int top_level)
454 {
455         BLI_assert(reshape_level <= top_level);
456         memset(data, 0, sizeof(*data));
457         data->num_grids = coarse_mesh->totloop;
458         data->reshape_level = reshape_level;
459         data->top_level = top_level;
460         if (reshape_level == top_level) {
461                 /* Nothing to do, reshape will happen on the whole grid content. */
462                 return;
463         }
464         data->mdisps = CustomData_get_layer(&coarse_mesh->ldata, CD_MDISPS);
465         data->grid_paint_mask =
466                 CustomData_get_layer(&coarse_mesh->ldata, CD_GRID_PAINT_MASK);
467         data->top_grid_size = BKE_subdiv_grid_size_from_level(top_level);
468         data->reshape_grid_size = BKE_subdiv_grid_size_from_level(reshape_level);
469         /* Initialize keys to access CCG at different levels. */
470         multires_reshape_init_level_key(
471                 &data->reshape_level_key, data, data->reshape_level);
472         multires_reshape_init_level_key(
473                 &data->top_level_key, data, data->top_level);
474         /* Make a copy of grids before reshaping, so we can calculate deltas
475          * later on. */
476         multires_reshape_store_original_grids(data);
477 }
478
479 static void multires_reshape_propagate_prepare_from_mmd(
480         MultiresPropagateData *data,
481         struct Depsgraph *depsgraph,
482         Object *object,
483         const MultiresModifierData *mmd,
484         const int top_level,
485         const bool use_render_params)
486 {
487     /* TODO(sergey): Find mode reliable way of getting current level. */
488         Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
489         Mesh *mesh = object->data;
490         const int level = multires_get_level(
491                 scene_eval, object, mmd, use_render_params, true);
492         multires_reshape_propagate_prepare(data, mesh, level, top_level);
493 }
494
495 /* Calculate delta of changed reshape level data layers. Delta goes to a
496  * grids at top level (meaning, the result grids are only partially filled
497  * in). */
498 static void multires_reshape_calculate_delta(
499         MultiresPropagateData *data,
500         CCGElem **delta_grids_data)
501 {
502         const int num_grids = data->num_grids;
503         /* At this point those custom data layers has updated data for the
504          * level we are propagating from. */
505         const MDisps *mdisps = data->mdisps;
506         const GridPaintMask *grid_paint_mask = data->grid_paint_mask;
507         CCGKey *reshape_key = &data->reshape_level_key;
508         CCGKey *delta_level_key = &data->top_level_key;
509         /* Calculate delta. */
510         const int top_grid_size = data->top_grid_size;
511         const int reshape_grid_size = data->reshape_grid_size;
512         const int delta_grid_size = data->top_grid_size;
513         const int skip = (top_grid_size - 1) / (reshape_grid_size - 1);
514         for (int grid_index = 0; grid_index < num_grids; grid_index++) {
515                 /*const*/ CCGElem *orig_grid = data->orig_grids_data[grid_index];
516                 CCGElem *delta_grid = delta_grids_data[grid_index];
517                 for (int y = 0; y < reshape_grid_size; y++) {
518                         const int top_y = y * skip;
519                         for (int x = 0; x < reshape_grid_size; x++) {
520                                 const int top_x = x * skip;
521                                 const int top_index = top_y * delta_grid_size + top_x;
522                                 sub_v3_v3v3(
523                                         CCG_grid_elem_co(
524                                                 delta_level_key, delta_grid, top_x, top_y),
525                                         mdisps[grid_index].disps[top_index],
526                                         CCG_grid_elem_co(reshape_key, orig_grid, x, y));
527                                 if (delta_level_key->has_mask) {
528                                         const float old_mask_value = *CCG_grid_elem_mask(
529                                                 reshape_key, orig_grid, x, y);
530                                         const float new_mask_value =
531                                                 grid_paint_mask[grid_index].data[top_index];
532                                         *CCG_grid_elem_mask(
533                                                 delta_level_key, delta_grid, top_x, top_y) =
534                                                         new_mask_value - old_mask_value;
535                                 }
536                         }
537                 }
538         }
539 }
540
541 /* Makes it so delta is propagated onto all the higher levels, but is also
542  * that this delta is smoothed in a way that it does not cause artifacts on
543  * boundaries. */
544
545 typedef struct MultiresPropagateCornerData {
546         float coord_delta[3];
547         float mask_delta;
548 } MultiresPropagateCornerData;
549
550 BLI_INLINE void multires_reshape_propagate_init_patch_corners(
551         MultiresPropagateData *data,
552         CCGElem *delta_grid,
553         const int patch_x, const int patch_y,
554         MultiresPropagateCornerData r_corners[4])
555 {
556         CCGKey *delta_level_key = &data->top_level_key;
557         const int orig_grid_size = data->reshape_grid_size;
558         const int top_grid_size = data->top_grid_size;
559         const int skip = (top_grid_size - 1) / (orig_grid_size - 1);
560         const int x = patch_x * skip;
561         const int y = patch_y * skip;
562         /* Store coordinate deltas. */
563         copy_v3_v3(r_corners[0].coord_delta,
564                    CCG_grid_elem_co(delta_level_key, delta_grid, x, y));
565         copy_v3_v3(r_corners[1].coord_delta,
566                    CCG_grid_elem_co(delta_level_key, delta_grid, x + skip, y));
567         copy_v3_v3(r_corners[2].coord_delta,
568                    CCG_grid_elem_co(delta_level_key, delta_grid, x, y + skip));
569         copy_v3_v3(r_corners[3].coord_delta,
570                    CCG_grid_elem_co(delta_level_key, delta_grid,
571                                     x + skip, y + skip));
572         if (delta_level_key->has_mask) {
573                 r_corners[0].mask_delta =
574                            *CCG_grid_elem_mask(delta_level_key, delta_grid, x, y);
575                 r_corners[1].mask_delta =
576                            *CCG_grid_elem_mask(delta_level_key, delta_grid,
577                                                x + skip, y);
578                 r_corners[2].mask_delta =
579                            *CCG_grid_elem_mask(delta_level_key, delta_grid,
580                                                x, y + skip);
581                 r_corners[3].mask_delta =
582                            *CCG_grid_elem_mask(delta_level_key, delta_grid,
583                                                x + skip, y + skip);
584         }
585 }
586
587 BLI_INLINE void multires_reshape_propagate_interpolate_coord(
588         float delta[3],
589         const MultiresPropagateCornerData corners[4],
590         const float weights[4])
591 {
592         interp_v3_v3v3v3v3(
593                delta,
594                corners[0].coord_delta, corners[1].coord_delta,
595                corners[2].coord_delta, corners[3].coord_delta,
596                weights);
597 }
598
599 BLI_INLINE float multires_reshape_propagate_interpolate_mask(
600         const MultiresPropagateCornerData corners[4],
601         const float weights[4])
602 {
603         return corners[0].mask_delta * weights[0] +
604                corners[1].mask_delta * weights[1] +
605                corners[2].mask_delta * weights[2] +
606                corners[3].mask_delta * weights[3];
607 }
608
609 BLI_INLINE void multires_reshape_propagate_and_smooth_delta_grid_patch(
610         MultiresPropagateData *data,
611         CCGElem *delta_grid,
612         const int patch_x, const int patch_y)
613 {
614         CCGKey *delta_level_key = &data->top_level_key;
615         const int orig_grid_size = data->reshape_grid_size;
616         const int top_grid_size = data->top_grid_size;
617         const int skip = (top_grid_size - 1) / (orig_grid_size - 1);
618         const float skip_inv = 1.0f / (float)skip;
619         MultiresPropagateCornerData corners[4];
620         multires_reshape_propagate_init_patch_corners(
621                 data, delta_grid, patch_x, patch_y, corners);
622         const int start_x = patch_x * skip;
623         const int start_y = patch_y * skip;
624         for (int y = 0; y <= skip; y++) {
625                 const float v = (float)y * skip_inv;
626                 const int final_y = start_y + y;
627                 for (int x = 0; x <= skip; x++) {
628                         const float u = (float)x * skip_inv;
629                         const int final_x = start_x + x;
630                         const float linear_weights[4] = {(1.0f - u) * (1.0f - v),
631                                                          u * (1.0f - v),
632                                                          (1.0f - u) * v,
633                                                          u * v};
634                         multires_reshape_propagate_interpolate_coord(
635                                 CCG_grid_elem_co(delta_level_key, delta_grid,
636                                                  final_x, final_y),
637                                 corners,
638                                 linear_weights);
639                         if (delta_level_key->has_mask) {
640                                 float *mask = CCG_grid_elem_mask(delta_level_key, delta_grid,
641                                                                  final_x, final_y);
642                                 *mask = multires_reshape_propagate_interpolate_mask(
643                                         corners, linear_weights);
644                         }
645                 }
646         }
647 }
648
649 BLI_INLINE void multires_reshape_propagate_and_smooth_delta_grid(
650         MultiresPropagateData *data,
651         CCGElem *delta_grid)
652 {
653         const int orig_grid_size = data->reshape_grid_size;
654         for (int patch_y = 0; patch_y < orig_grid_size - 1; patch_y++) {
655                 for (int patch_x = 0; patch_x < orig_grid_size - 1; patch_x++) {
656                         multires_reshape_propagate_and_smooth_delta_grid_patch(
657                                 data, delta_grid, patch_x, patch_y);
658                 }
659         }
660 }
661
662 /* Entry point to propagate+smooth. */
663 static void multires_reshape_propagate_and_smooth_delta(
664         MultiresPropagateData *data,
665         CCGElem **delta_grids_data)
666 {
667         const int num_grids = data->num_grids;
668         for (int grid_index = 0; grid_index < num_grids; grid_index++) {
669                 CCGElem *delta_grid = delta_grids_data[grid_index];
670                 multires_reshape_propagate_and_smooth_delta_grid(data, delta_grid);
671         }
672 }
673
674 /* Apply smoothed deltas on the actual data layers. */
675 static void multires_reshape_propagate_apply_delta(
676         MultiresPropagateData *data,
677         CCGElem **delta_grids_data)
678 {
679         const int num_grids = data->num_grids;
680         /* At this point those custom data layers has updated data for the
681          * level we are propagating from. */
682         MDisps *mdisps = data->mdisps;
683         GridPaintMask *grid_paint_mask = data->grid_paint_mask;
684         CCGKey *orig_key = &data->reshape_level_key;
685         CCGKey *delta_level_key = &data->top_level_key;
686         CCGElem **orig_grids_data = data->orig_grids_data;
687         const int orig_grid_size = data->reshape_grid_size;
688         const int top_grid_size = data->top_grid_size;
689         const int skip = (top_grid_size - 1) / (orig_grid_size - 1);
690         /* Restore grid values at the reshape level. Those values are to be changed
691          * to the accommodate for the smooth delta. */
692         for (int grid_index = 0; grid_index < num_grids; grid_index++) {
693                 CCGElem *orig_grid = orig_grids_data[grid_index];
694                 for (int y = 0; y < orig_grid_size; y++) {
695                         const int top_y = y * skip;
696                         for (int x = 0; x < orig_grid_size; x++) {
697                                 const int top_x = x * skip;
698                                 const int top_index = top_y * top_grid_size + top_x;
699                                 copy_v3_v3(mdisps[grid_index].disps[top_index],
700                                        CCG_grid_elem_co(orig_key, orig_grid, x, y));
701                                 if (grid_paint_mask != NULL) {
702                                         grid_paint_mask[grid_index].data[top_index] =
703                                                 *CCG_grid_elem_mask(orig_key, orig_grid, x, y);
704                                 }
705                         }
706                 }
707         }
708         /* Add smoothed delta to all the levels. */
709         for (int grid_index = 0; grid_index < num_grids; grid_index++) {
710                 CCGElem *delta_grid = delta_grids_data[grid_index];
711                 for (int y = 0; y < top_grid_size; y++) {
712                         for (int x = 0; x < top_grid_size; x++) {
713                                 const int top_index = y * top_grid_size + x;
714                                 add_v3_v3(mdisps[grid_index].disps[top_index],
715                                        CCG_grid_elem_co(delta_level_key, delta_grid, x, y));
716                                 if (delta_level_key->has_mask) {
717                                         grid_paint_mask[grid_index].data[top_index] +=
718                                                *CCG_grid_elem_mask(
719                                                        delta_level_key, delta_grid, x, y);
720                                 }
721                         }
722                 }
723         }
724 }
725
726 static void multires_reshape_propagate(MultiresPropagateData *data)
727 {
728         if (data->reshape_level == data->top_level) {
729                 return;
730         }
731         const int num_grids = data->num_grids;
732         /* Calculate delta made at the reshape level. */
733         CCGKey *delta_level_key = &data->top_level_key;
734         CCGElem **delta_grids_data = allocate_grids(delta_level_key, num_grids);
735         multires_reshape_calculate_delta(data, delta_grids_data);
736         /* Propagate deltas to the higher levels. */
737         multires_reshape_propagate_and_smooth_delta(data, delta_grids_data);
738         /* Finally, apply smoothed deltas. */
739         multires_reshape_propagate_apply_delta(data, delta_grids_data);
740         /* Cleanup. */
741         free_grids(delta_grids_data, num_grids);
742 }
743
744 static void multires_reshape_propagate_free(MultiresPropagateData *data)
745 {
746         free_grids(data->orig_grids_data, data->num_grids);
747 }
748
749 /* =============================================================================
750  * Reshape from deformed vertex coordinates.
751  */
752
753 typedef struct MultiresReshapeFromDeformedVertsContext {
754         MultiresReshapeContext reshape_ctx;
755         const float (*deformed_verts)[3];
756         int num_deformed_verts;
757 } MultiresReshapeFromDeformedVertsContext;
758
759 static bool multires_reshape_topology_info(
760         const SubdivForeachContext *foreach_context,
761         const int num_vertices,
762         const int UNUSED(num_edges),
763         const int UNUSED(num_loops),
764         const int UNUSED(num_polygons))
765 {
766         MultiresReshapeFromDeformedVertsContext *ctx = foreach_context->user_data;
767         if (num_vertices != ctx->num_deformed_verts) {
768                 return false;
769         }
770         return true;
771 }
772
773 static void multires_reshape_vertex(
774         MultiresReshapeFromDeformedVertsContext *ctx,
775         const int ptex_face_index,
776         const float u, const float v,
777         const int coarse_poly_index,
778         const int coarse_corner,
779         const int subdiv_vertex_index)
780 {
781         const float *final_P = ctx->deformed_verts[subdiv_vertex_index];
782         multires_reshape_vertex_from_final_data(
783                 &ctx->reshape_ctx,
784                 ptex_face_index, u, v,
785                 coarse_poly_index,
786                 coarse_corner,
787                 final_P, 0.0f);
788 }
789
790 static void multires_reshape_vertex_inner(
791         const SubdivForeachContext *foreach_context,
792         void *UNUSED(tls_v),
793         const int ptex_face_index,
794         const float u, const float v,
795         const int coarse_poly_index,
796         const int coarse_corner,
797         const int subdiv_vertex_index)
798 {
799         MultiresReshapeFromDeformedVertsContext *ctx = foreach_context->user_data;
800         multires_reshape_vertex(
801                 ctx,
802                 ptex_face_index, u, v,
803                 coarse_poly_index,
804                 coarse_corner,
805                 subdiv_vertex_index);
806 }
807
808 static void multires_reshape_vertex_every_corner(
809         const struct SubdivForeachContext *foreach_context,
810         void *UNUSED(tls_v),
811         const int ptex_face_index,
812         const float u, const float v,
813         const int UNUSED(coarse_vertex_index),
814         const int coarse_poly_index,
815         const int coarse_corner,
816         const int subdiv_vertex_index)
817 {
818         MultiresReshapeFromDeformedVertsContext *ctx = foreach_context->user_data;
819         multires_reshape_vertex(
820                 ctx,
821                 ptex_face_index, u, v,
822                 coarse_poly_index,
823                 coarse_corner,
824                 subdiv_vertex_index);
825 }
826
827 static void multires_reshape_vertex_every_edge(
828         const struct SubdivForeachContext *foreach_context,
829         void *UNUSED(tls_v),
830         const int ptex_face_index,
831         const float u, const float v,
832         const int UNUSED(coarse_edge_index),
833         const int coarse_poly_index,
834         const int coarse_corner,
835         const int subdiv_vertex_index)
836 {
837         MultiresReshapeFromDeformedVertsContext *ctx = foreach_context->user_data;
838         multires_reshape_vertex(
839                 ctx,
840                 ptex_face_index, u, v,
841                 coarse_poly_index,
842                 coarse_corner,
843                 subdiv_vertex_index);
844 }
845
846 static Subdiv *multires_create_subdiv_for_reshape(
847         struct Depsgraph *depsgraph,
848         /*const*/ Object *object,
849         const MultiresModifierData *mmd)
850 {
851         Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
852         Object *object_eval = DEG_get_evaluated_object(depsgraph, object);
853         Mesh *deformed_mesh = mesh_get_eval_deform(
854                 depsgraph, scene_eval, object_eval, CD_MASK_BAREMESH);
855         SubdivSettings subdiv_settings;
856         BKE_multires_subdiv_settings_init(&subdiv_settings, mmd);
857         Subdiv *subdiv = BKE_subdiv_new_from_mesh(&subdiv_settings, deformed_mesh);
858         if (!BKE_subdiv_eval_update_from_mesh(subdiv, deformed_mesh)) {
859                 BKE_subdiv_free(subdiv);
860                 return NULL;
861         }
862         return subdiv;
863 }
864
865 static bool multires_reshape_from_vertcos(
866         struct Depsgraph *depsgraph,
867         Object *object,
868         const MultiresModifierData *mmd,
869         const float (*deformed_verts)[3],
870         const int num_deformed_verts,
871         const bool use_render_params)
872 {
873         Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
874         Mesh *coarse_mesh = object->data;
875         MDisps *mdisps = CustomData_get_layer(&coarse_mesh->ldata, CD_MDISPS);
876         /* Pick maximum between multires level and dispalcement level.
877          * This is because mesh can be used by objects with multires at different
878          * levels.
879          *
880          * TODO(sergey): At this point it should be possible to always use
881          * mdisps->level. */
882         const int top_level = max_ii(mmd->totlvl, mdisps->level);
883         /* Make sure displacement grids are ready. */
884         multires_reshape_ensure_grids(coarse_mesh, top_level);
885         /* Construct context. */
886         MultiresReshapeFromDeformedVertsContext reshape_deformed_verts_ctx = {
887                 .reshape_ctx = {
888                         .coarse_mesh = coarse_mesh,
889                         .mdisps = mdisps,
890                         .grid_paint_mask = NULL,
891                         .top_grid_size = BKE_subdiv_grid_size_from_level(top_level),
892                         .top_level = top_level,
893                 },
894                 .deformed_verts = deformed_verts,
895                 .num_deformed_verts = num_deformed_verts,
896         };
897         SubdivForeachContext foreach_context = {
898                 .topology_info = multires_reshape_topology_info,
899                 .vertex_inner = multires_reshape_vertex_inner,
900                 .vertex_every_edge = multires_reshape_vertex_every_edge,
901                 .vertex_every_corner = multires_reshape_vertex_every_corner,
902                 .user_data = &reshape_deformed_verts_ctx,
903         };
904         /* Initialize subdivision surface. */
905         Subdiv *subdiv = multires_create_subdiv_for_reshape(depsgraph, object, mmd);
906         if (subdiv == NULL) {
907                 return false;
908         }
909         reshape_deformed_verts_ctx.reshape_ctx.subdiv = subdiv;
910         /* Initialize mesh rasterization settings. */
911         SubdivToMeshSettings mesh_settings;
912         BKE_multires_subdiv_mesh_settings_init(
913         &mesh_settings, scene_eval, object, mmd, use_render_params, true);
914         /* Initialize propagation to higher levels. */
915         MultiresPropagateData propagate_data;
916         multires_reshape_propagate_prepare_from_mmd(
917         &propagate_data, depsgraph, object, mmd, top_level, use_render_params);
918         /* Run all the callbacks. */
919         BKE_subdiv_foreach_subdiv_geometry(
920                 subdiv,
921                 &foreach_context,
922                 &mesh_settings,
923                 coarse_mesh);
924         BKE_subdiv_free(subdiv);
925         /* Update higher levels if needed. */
926         multires_reshape_propagate(&propagate_data);
927         multires_reshape_propagate_free(&propagate_data);
928         return true;
929 }
930
931 /* =============================================================================
932  * Reshape from object.
933  */
934
935 /* Returns truth on success, false otherwise.
936  *
937  * This function might fail in cases like source and destination not having
938  * matched amount of vertices. */
939 bool multiresModifier_reshapeFromObject(
940         struct Depsgraph *depsgraph,
941         MultiresModifierData *mmd,
942         Object *dst,
943         Object *src)
944 {
945         /* Would be cool to support this eventually, but it is very tricky to match
946          * vertices order even for meshes, when mixing meshes and other objects it's
947          * even more tricky. */
948         if (src->type != OB_MESH) {
949                 return false;
950         }
951         MultiresModifierData reshape_mmd;
952         multires_reshape_init_mmd(&reshape_mmd, mmd);
953         /* Get evaluated vertices locations to reshape to. */
954         Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
955         Object *src_eval = DEG_get_evaluated_object(depsgraph, src);
956         Mesh *src_mesh_eval = mesh_get_eval_final(
957                 depsgraph, scene_eval, src_eval, CD_MASK_BAREMESH);
958         int num_deformed_verts;
959         float (*deformed_verts)[3] = BKE_mesh_vertexCos_get(
960                 src_mesh_eval, &num_deformed_verts);
961         bool result = multires_reshape_from_vertcos(
962                 depsgraph,
963                 dst,
964                 &reshape_mmd,
965                 deformed_verts,
966                 num_deformed_verts,
967                 false);
968         MEM_freeN(deformed_verts);
969         return result;
970 }
971
972 /* =============================================================================
973  * Reshape from modifier.
974  */
975
976 bool multiresModifier_reshapeFromDeformModifier(
977         struct Depsgraph *depsgraph,
978         MultiresModifierData *mmd,
979         Object *object,
980         ModifierData *md)
981 {
982         MultiresModifierData highest_mmd;
983         /* It is possible that the current subdivision level of multires is lower
984          * that it's maximum possible one (i.e., viewport is set to a lower level
985          * for the performance purposes). But even then, we want all the multires
986          * levels to be reshaped. Most accurate way to do so is to ignore all
987          * simplifications and calculate deformation modifier for the highest
988          * possible multires level.
989          * Alternative would be propagate displacement from current level to a
990          * higher ones, but that is likely to cause artifacts. */
991         multires_reshape_init_mmd_top_level(&highest_mmd, mmd);
992         Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
993         /* Perform sanity checks and early output. */
994         if (multires_get_level(
995                     scene_eval, object, &highest_mmd, false, true) == 0)
996         {
997                 return false;
998         }
999         /* Create mesh for the multires, ignoring any further modifiers (leading
1000          * deformation modifiers will be applied though). */
1001         Mesh *multires_mesh = BKE_multires_create_mesh(
1002                 depsgraph, scene_eval, &highest_mmd, object);
1003         int num_deformed_verts;
1004         float (*deformed_verts)[3] = BKE_mesh_vertexCos_get(
1005                 multires_mesh, &num_deformed_verts);
1006         /* Apply deformation modifier on the multires, */
1007         const ModifierEvalContext modifier_ctx = {
1008                 .depsgraph = depsgraph,
1009                 .object = object,
1010                 .flag = MOD_APPLY_USECACHE | MOD_APPLY_IGNORE_SIMPLIFY,
1011         };
1012         modwrap_deformVerts(
1013                 md, &modifier_ctx, multires_mesh, deformed_verts,
1014                 multires_mesh->totvert);
1015         BKE_id_free(NULL, multires_mesh);
1016         /* Reshaping */
1017         bool result = multires_reshape_from_vertcos(
1018                 depsgraph,
1019                 object,
1020                 &highest_mmd,
1021                 deformed_verts,
1022                 num_deformed_verts,
1023                 false);
1024         /* Cleanup */
1025         MEM_freeN(deformed_verts);
1026         return result;
1027 }
1028
1029 /* =============================================================================
1030  * Reshape from grids.
1031  */
1032
1033 typedef struct ReshapeFromCCGTaskData {
1034         MultiresReshapeContext reshape_ctx;
1035         int *face_ptex_offset;
1036         const CCGKey *key;
1037         /*const*/ CCGElem **grids;
1038 } ReshapeFromCCGTaskData;
1039
1040 static void reshape_from_ccg_regular_face(ReshapeFromCCGTaskData *data,
1041                                           const MPoly *coarse_poly)
1042 {
1043         const CCGKey *key = data->key;
1044         /*const*/ CCGElem **grids = data->grids;
1045         const Mesh *coarse_mesh = data->reshape_ctx.coarse_mesh;
1046         const MPoly *coarse_mpoly = coarse_mesh->mpoly;
1047         const int key_grid_size = key->grid_size;
1048         const int key_grid_size_1 = key_grid_size - 1;
1049         const int resolution = 2 * key_grid_size - 1;
1050         const float resolution_1_inv = 1.0f / (float)(resolution - 1);
1051         const int coarse_poly_index = coarse_poly - coarse_mpoly;
1052         const int ptex_face_index = data->face_ptex_offset[coarse_poly_index];
1053         for (int y = 0; y < resolution; y++) {
1054                 const float v = y * resolution_1_inv;
1055                 for (int x = 0; x < resolution; x++) {
1056                         const float u = x * resolution_1_inv;
1057                         float corner_u, corner_v;
1058                         float grid_u, grid_v;
1059                         const int face_corner = BKE_subdiv_rotate_quad_to_corner(
1060                                 u, v, &corner_u, &corner_v);
1061                         BKE_subdiv_ptex_face_uv_to_grid_uv(
1062                                 corner_u, corner_v, &grid_u, &grid_v);
1063                         /*const*/ CCGElem *grid =
1064                                 grids[coarse_poly->loopstart + face_corner];
1065                         /*const*/ CCGElem *grid_element = CCG_grid_elem(
1066                                 key,
1067                                 grid,
1068                                 key_grid_size_1 * grid_u,
1069                                 key_grid_size_1 * grid_v);
1070                         const float *final_P = CCG_elem_co(key, grid_element);
1071                         float final_mask = 0.0f;
1072                         if (key->has_mask) {
1073                                 final_mask = *CCG_elem_mask(key, grid_element);
1074                         }
1075                         multires_reshape_vertex_from_final_data(
1076                                 &data->reshape_ctx,
1077                                 ptex_face_index,
1078                                 u, v,
1079                                 coarse_poly_index,
1080                                 0,
1081                                 final_P, final_mask);
1082                 }
1083         }
1084 }
1085
1086 static void reshape_from_ccg_special_face(ReshapeFromCCGTaskData *data,
1087                                           const MPoly *coarse_poly)
1088 {
1089         const CCGKey *key = data->key;
1090         /*const*/ CCGElem **grids = data->grids;
1091         const Mesh *coarse_mesh = data->reshape_ctx.coarse_mesh;
1092         const MPoly *coarse_mpoly = coarse_mesh->mpoly;
1093         const int key_grid_size = key->grid_size;
1094         const int key_grid_size_1 = key_grid_size - 1;
1095         const int resolution = key_grid_size;
1096         const float resolution_1_inv = 1.0f / (float)(resolution - 1);
1097         const int coarse_poly_index = coarse_poly - coarse_mpoly;
1098         const int ptex_face_index = data->face_ptex_offset[coarse_poly_index];
1099         for (int corner = 0; corner < coarse_poly->totloop; corner++) {
1100                 for (int y = 0; y < resolution; y++) {
1101                         const float v = y * resolution_1_inv;
1102                         for (int x = 0; x < resolution; x++) {
1103                                 const float u = x * resolution_1_inv;
1104                                 float grid_u, grid_v;
1105                                 BKE_subdiv_ptex_face_uv_to_grid_uv(u, v, &grid_u, &grid_v);
1106                                 /*const*/ CCGElem *grid =
1107                                         grids[coarse_poly->loopstart + corner];
1108                                 /*const*/ CCGElem *grid_element = CCG_grid_elem(
1109                                         key,
1110                                         grid,
1111                                         key_grid_size_1 * grid_u,
1112                                         key_grid_size_1 * grid_v);
1113                                 const float *final_P = CCG_elem_co(key, grid_element);
1114                                 float final_mask = 0.0f;
1115                                 if (key->has_mask) {
1116                                         final_mask = *CCG_elem_mask(key, grid_element);
1117                                 }
1118                                 multires_reshape_vertex_from_final_data(
1119                                         &data->reshape_ctx,
1120                                         ptex_face_index + corner,
1121                                         u, v,
1122                                         coarse_poly_index,
1123                                         corner,
1124                                         final_P, final_mask);
1125                         }
1126                 }
1127         }
1128 }
1129
1130 static void reshape_from_ccg_task(
1131         void *__restrict userdata,
1132         const int coarse_poly_index,
1133         const ParallelRangeTLS *__restrict UNUSED(tls))
1134 {
1135         ReshapeFromCCGTaskData *data = userdata;
1136         const Mesh *coarse_mesh = data->reshape_ctx.coarse_mesh;
1137         const MPoly *coarse_mpoly = coarse_mesh->mpoly;
1138         const MPoly *coarse_poly = &coarse_mpoly[coarse_poly_index];
1139         if (coarse_poly->totloop == 4) {
1140                 reshape_from_ccg_regular_face(data, coarse_poly);
1141         }
1142         else {
1143                 reshape_from_ccg_special_face(data, coarse_poly);
1144         }
1145 }
1146
1147 bool multiresModifier_reshapeFromCCG(
1148         const int tot_level,
1149         Mesh *coarse_mesh,
1150         SubdivCCG *subdiv_ccg)
1151 {
1152         CCGKey key;
1153         BKE_subdiv_ccg_key_top_level(&key, subdiv_ccg);
1154         /* Sanity checks. */
1155         if (coarse_mesh->totloop != subdiv_ccg->num_grids) {
1156                 /* Grids are supposed to eb created for each face-cornder (aka loop). */
1157                 return false;
1158         }
1159         MDisps *mdisps = CustomData_get_layer(&coarse_mesh->ldata, CD_MDISPS);
1160         if (mdisps == NULL) {
1161                 /* Multires displacement has been removed before current changes were
1162                  * applies to all the levels. */
1163                 return false;
1164         }
1165         GridPaintMask *grid_paint_mask =
1166                 CustomData_get_layer(&coarse_mesh->ldata, CD_GRID_PAINT_MASK);
1167         Subdiv *subdiv = subdiv_ccg->subdiv;
1168         /* Pick maximum between multires level and dispalcement level.
1169          * This is because mesh can be used by objects with multires at different
1170          * levels.
1171          *
1172          * TODO(sergey): At this point it should be possible to always use
1173          * mdisps->level. */
1174         const int top_level = max_ii(tot_level, mdisps->level);
1175         /* Make sure displacement grids are ready. */
1176         multires_reshape_ensure_grids(coarse_mesh, top_level);
1177         /* Construct context. */
1178         ReshapeFromCCGTaskData data = {
1179                 .reshape_ctx = {
1180                         .subdiv = subdiv,
1181                         .coarse_mesh = coarse_mesh,
1182                         .mdisps  = mdisps,
1183                         .grid_paint_mask = grid_paint_mask,
1184                         .top_grid_size = BKE_subdiv_grid_size_from_level(top_level),
1185                         .top_level = top_level,
1186                 },
1187                 .face_ptex_offset = BKE_subdiv_face_ptex_offset_get(subdiv),
1188                 .key = &key,
1189                 .grids = subdiv_ccg->grids,
1190         };
1191         /* Initialize propagation to higher levels. */
1192         MultiresPropagateData propagate_data;
1193         multires_reshape_propagate_prepare(
1194                 &propagate_data, coarse_mesh, key.level, top_level);
1195         /* Threaded grids iteration. */
1196         ParallelRangeSettings parallel_range_settings;
1197         BLI_parallel_range_settings_defaults(&parallel_range_settings);
1198         BLI_task_parallel_range(0, coarse_mesh->totpoly,
1199                                 &data,
1200                                 reshape_from_ccg_task,
1201                                 &parallel_range_settings);
1202         /* Update higher levels if needed. */
1203         multires_reshape_propagate(&propagate_data);
1204         multires_reshape_propagate_free(&propagate_data);
1205         return true;
1206 }