Cleanup: add trailing commas to structs
[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         copy_boundary_displacement(
319                 ctx, coarse_poly, face_corner, grid_x, grid_y,
320                 displacement_grid, grid_paint_mask);
321 }
322
323 /* =============================================================================
324  * Helpers to propagate displacement to higher levels.
325  */
326
327 typedef struct MultiresPropagateData {
328         /* Number of displacement grids. */
329         int num_grids;
330         /* Resolution level up to which displacement is known. */
331         int reshape_level;
332         /* Resolution up to which propagation is happening, affecting all the
333          * levels in [reshape_level + 1, top_level].
334          */
335         int top_level;
336         /* Grid sizes at the corresponding levels. */
337         int reshape_grid_size;
338         int top_grid_size;
339         /* Keys to access CCG at different levels. */
340         CCGKey reshape_level_key;
341         CCGKey top_level_key;
342         /* Original grid data, before any updates for reshape.
343          * Contains data at the reshape_level resolution level.
344          */
345         CCGElem **orig_grids_data;
346         /* Custom data layers from a coarse mesh. */
347         MDisps *mdisps;
348         GridPaintMask *grid_paint_mask;
349 } MultiresPropagateData;
350
351 static CCGElem **allocate_grids(CCGKey *key, int num_grids)
352 {
353         CCGElem **grids = MEM_calloc_arrayN(
354                 num_grids, sizeof(CCGElem *), "reshape grids*");
355         for (int grid_index = 0; grid_index < num_grids; grid_index++) {
356                 grids[grid_index] = MEM_calloc_arrayN(
357                         key->elem_size,
358                         key->grid_area,
359                         "reshape orig_grids_data elems");
360         }
361         return grids;
362 }
363
364 static void free_grids(CCGElem **grids, int num_grids)
365 {
366         if (grids == NULL) {
367                 return;
368         }
369         for (int grid_index = 0; grid_index < num_grids; grid_index++) {
370                 MEM_freeN(grids[grid_index]);
371         }
372         MEM_freeN(grids);
373 }
374
375 /* Initialize element sizes and offsets. */
376 static void multires_reshape_init_key_layers(
377         CCGKey *key,
378         const MultiresPropagateData *data)
379 {
380         key->elem_size = 3 * sizeof(float);
381         if (data->grid_paint_mask != NULL) {
382                 key->mask_offset = 3 * sizeof(float);
383                 key->elem_size += sizeof(float);
384                 key->has_mask = true;
385         }
386         else {
387                 key->mask_offset = -1;
388                 key->has_mask = false;
389         }
390         /* We never have normals in original grids. */
391         key->normal_offset = -1;
392         key->has_normals = false;
393 }
394
395 /* Initialize key used to access reshape grids at given level. */
396 static void multires_reshape_init_level_key(
397         CCGKey *key,
398         const MultiresPropagateData *data,
399         const int level)
400 {
401         key->level = level;
402         /* Init layers. */
403         multires_reshape_init_key_layers(key, data);
404         /* By default, only 3 floats for coordinate, */
405         key->grid_size = BKE_subdiv_grid_size_from_level(key->level);
406         key->grid_area = key->grid_size * key->grid_size;
407         key->grid_bytes = key->elem_size * key->grid_area;
408 }
409
410 static void multires_reshape_store_original_grids(
411         MultiresPropagateData *data)
412 {
413         const int num_grids = data->num_grids;
414         /* Original data to be backed up. */
415         const MDisps *mdisps = data->mdisps;
416         const GridPaintMask *grid_paint_mask = data->grid_paint_mask;
417         /* Allocate grids for backup. */
418         CCGKey *orig_key = &data->reshape_level_key;
419         CCGElem **orig_grids_data = allocate_grids(orig_key, num_grids);
420         /* Fill in grids. */
421         const int orig_grid_size = data->reshape_grid_size;
422         const int top_grid_size = data->top_grid_size;
423         const int skip = (top_grid_size - 1) / (orig_grid_size - 1);
424         for (int grid_index = 0; grid_index < num_grids; grid_index++) {
425                 CCGElem *orig_grid = orig_grids_data[grid_index];
426                 for (int y = 0; y < orig_grid_size; y++) {
427                         const int top_y = y * skip;
428                         for (int x = 0; x < orig_grid_size; x++) {
429                                 const int top_x = x * skip;
430                                 const int top_index = top_y * top_grid_size + top_x;
431                                 memcpy(CCG_grid_elem_co(orig_key, orig_grid, x, y),
432                                        mdisps[grid_index].disps[top_index],
433                                        sizeof(float) * 3);
434                                 if (orig_key->has_mask) {
435                                         *CCG_grid_elem_mask(orig_key, orig_grid, x, y) =
436                                                 grid_paint_mask[grid_index].data[top_index];
437                                 }
438                         }
439                 }
440         }
441         /* Store in the context. */
442         data->orig_grids_data = orig_grids_data;
443 }
444
445 static void multires_reshape_propagate_prepare(
446         MultiresPropagateData *data,
447         Mesh *coarse_mesh,
448         const int reshape_level,
449         const int top_level)
450 {
451         BLI_assert(reshape_level <= top_level);
452         memset(data, 0, sizeof(*data));
453         data->num_grids = coarse_mesh->totloop;
454         data->reshape_level = reshape_level;
455         data->top_level = top_level;
456         if (reshape_level == top_level) {
457                 /* Nothing to do, reshape will happen on the whole grid content. */
458                 return;
459         }
460         data->mdisps = CustomData_get_layer(&coarse_mesh->ldata, CD_MDISPS);
461         data->grid_paint_mask =
462                 CustomData_get_layer(&coarse_mesh->ldata, CD_GRID_PAINT_MASK);
463         data->top_grid_size = BKE_subdiv_grid_size_from_level(top_level);
464         data->reshape_grid_size = BKE_subdiv_grid_size_from_level(reshape_level);
465         /* Initialize keys to access CCG at different levels. */
466         multires_reshape_init_level_key(
467                 &data->reshape_level_key, data, data->reshape_level);
468         multires_reshape_init_level_key(
469                 &data->top_level_key, data, data->top_level);
470         /* Make a copy of grids before reshaping, so we can calculate deltas
471          * later on.
472          */
473         multires_reshape_store_original_grids(data);
474 }
475
476 static void multires_reshape_propagate_prepare_from_mmd(
477         MultiresPropagateData *data,
478         struct Depsgraph *depsgraph,
479         Object *object,
480         const MultiresModifierData *mmd,
481         const int top_level,
482         const bool use_render_params)
483 {
484     /* TODO(sergey): Find mode reliable way of getting current level. */
485         Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
486         Mesh *mesh = object->data;
487         const int level = multires_get_level(
488                 scene_eval, object, mmd, use_render_params, true);
489         multires_reshape_propagate_prepare(data, mesh, level, top_level);
490 }
491
492 /* Calculate delta of changed reshape level data layers. Delta goes to a
493  * grids at top level (meaning, the result grids are only partially filled
494  * in).
495  */
496 static void multires_reshape_calculate_delta(
497         MultiresPropagateData *data,
498         CCGElem **delta_grids_data)
499 {
500         const int num_grids = data->num_grids;
501         /* At this point those custom data layers has updated data for the
502          * level we are propagating from.
503          */
504         const MDisps *mdisps = data->mdisps;
505         const GridPaintMask *grid_paint_mask = data->grid_paint_mask;
506         CCGKey *reshape_key = &data->reshape_level_key;
507         CCGKey *delta_level_key = &data->top_level_key;
508         /* Calculate delta. */
509         const int top_grid_size = data->top_grid_size;
510         const int reshape_grid_size = data->reshape_grid_size;
511         const int delta_grid_size = data->top_grid_size;
512         const int skip = (top_grid_size - 1) / (reshape_grid_size - 1);
513         for (int grid_index = 0; grid_index < num_grids; grid_index++) {
514                 /*const*/ CCGElem *orig_grid = data->orig_grids_data[grid_index];
515                 CCGElem *delta_grid = delta_grids_data[grid_index];
516                 for (int y = 0; y < reshape_grid_size; y++) {
517                         const int top_y = y * skip;
518                         for (int x = 0; x < reshape_grid_size; x++) {
519                                 const int top_x = x * skip;
520                                 const int top_index = top_y * delta_grid_size + top_x;
521                                 sub_v3_v3v3(
522                                         CCG_grid_elem_co(
523                                                 delta_level_key, delta_grid, top_x, top_y),
524                                         mdisps[grid_index].disps[top_index],
525                                         CCG_grid_elem_co(reshape_key, orig_grid, x, y));
526                                 if (delta_level_key->has_mask) {
527                                         const float old_mask_value = *CCG_grid_elem_mask(
528                                                 reshape_key, orig_grid, x, y);
529                                         const float new_mask_value =
530                                                 grid_paint_mask[grid_index].data[top_index];
531                                         *CCG_grid_elem_mask(
532                                                 delta_level_key, delta_grid, top_x, top_y) =
533                                                         new_mask_value - old_mask_value;
534                                 }
535                         }
536                 }
537         }
538 }
539
540 /* Makes it so delta is propagated onto all the higher levels, but is also
541  * that this delta is smoothed in a way that it does not cause artifacts on
542  * boundaries.
543  */
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          */
683         MDisps *mdisps = data->mdisps;
684         GridPaintMask *grid_paint_mask = data->grid_paint_mask;
685         CCGKey *orig_key = &data->reshape_level_key;
686         CCGKey *delta_level_key = &data->top_level_key;
687         CCGElem **orig_grids_data = data->orig_grids_data;
688         const int orig_grid_size = data->reshape_grid_size;
689         const int top_grid_size = data->top_grid_size;
690         const int skip = (top_grid_size - 1) / (orig_grid_size - 1);
691         /* Restore grid values at the reshape level. Those values are to be changed
692          * to the accommodate for the smooth delta.
693          */
694         for (int grid_index = 0; grid_index < num_grids; grid_index++) {
695                 CCGElem *orig_grid = orig_grids_data[grid_index];
696                 for (int y = 0; y < orig_grid_size; y++) {
697                         const int top_y = y * skip;
698                         for (int x = 0; x < orig_grid_size; x++) {
699                                 const int top_x = x * skip;
700                                 const int top_index = top_y * top_grid_size + top_x;
701                                 copy_v3_v3(mdisps[grid_index].disps[top_index],
702                                        CCG_grid_elem_co(orig_key, orig_grid, x, y));
703                                 if (grid_paint_mask != NULL) {
704                                         grid_paint_mask[grid_index].data[top_index] =
705                                                 *CCG_grid_elem_mask(orig_key, orig_grid, x, y);
706                                 }
707                         }
708                 }
709         }
710         /* Add smoothed delta to all the levels. */
711         for (int grid_index = 0; grid_index < num_grids; grid_index++) {
712                 CCGElem *delta_grid = delta_grids_data[grid_index];
713                 for (int y = 0; y < top_grid_size; y++) {
714                         for (int x = 0; x < top_grid_size; x++) {
715                                 const int top_index = y * top_grid_size + x;
716                                 add_v3_v3(mdisps[grid_index].disps[top_index],
717                                        CCG_grid_elem_co(delta_level_key, delta_grid, x, y));
718                                 if (delta_level_key->has_mask) {
719                                         grid_paint_mask[grid_index].data[top_index] +=
720                                                *CCG_grid_elem_mask(
721                                                        delta_level_key, delta_grid, x, y);
722                                 }
723                         }
724                 }
725         }
726 }
727
728 static void multires_reshape_propagate(MultiresPropagateData *data)
729 {
730         if (data->reshape_level == data->top_level) {
731                 return;
732         }
733         const int num_grids = data->num_grids;
734         /* Calculate delta made at the reshape level. */
735         CCGKey *delta_level_key = &data->top_level_key;
736         CCGElem **delta_grids_data = allocate_grids(delta_level_key, num_grids);
737         multires_reshape_calculate_delta(data, delta_grids_data);
738         /* Propagate deltas to the higher levels. */
739         multires_reshape_propagate_and_smooth_delta(data, delta_grids_data);
740         /* Finally, apply smoothed deltas. */
741         multires_reshape_propagate_apply_delta(data, delta_grids_data);
742         /* Cleanup. */
743         free_grids(delta_grids_data, num_grids);
744 }
745
746 static void multires_reshape_propagate_free(MultiresPropagateData *data)
747 {
748         free_grids(data->orig_grids_data, data->num_grids);
749 }
750
751 /* =============================================================================
752  * Reshape from deformed vertex coordinates.
753  */
754
755 typedef struct MultiresReshapeFromDeformedVertsContext {
756         MultiresReshapeContext reshape_ctx;
757         const float (*deformed_verts)[3];
758         int num_deformed_verts;
759 } MultiresReshapeFromDeformedVertsContext;
760
761 static bool multires_reshape_topology_info(
762         const SubdivForeachContext *foreach_context,
763         const int num_vertices,
764         const int UNUSED(num_edges),
765         const int UNUSED(num_loops),
766         const int UNUSED(num_polygons))
767 {
768         MultiresReshapeFromDeformedVertsContext *ctx = foreach_context->user_data;
769         if (num_vertices != ctx->num_deformed_verts) {
770                 return false;
771         }
772         return true;
773 }
774
775 static void multires_reshape_vertex(
776         MultiresReshapeFromDeformedVertsContext *ctx,
777         const int ptex_face_index,
778         const float u, const float v,
779         const int coarse_poly_index,
780         const int coarse_corner,
781         const int subdiv_vertex_index)
782 {
783         const float *final_P = ctx->deformed_verts[subdiv_vertex_index];
784         multires_reshape_vertex_from_final_data(
785                 &ctx->reshape_ctx,
786                 ptex_face_index, u, v,
787                 coarse_poly_index,
788                 coarse_corner,
789                 final_P, 0.0f);
790 }
791
792 static void multires_reshape_vertex_inner(
793         const SubdivForeachContext *foreach_context,
794         void *UNUSED(tls_v),
795         const int ptex_face_index,
796         const float u, const float v,
797         const int coarse_poly_index,
798         const int coarse_corner,
799         const int subdiv_vertex_index)
800 {
801         MultiresReshapeFromDeformedVertsContext *ctx = foreach_context->user_data;
802         multires_reshape_vertex(
803                 ctx,
804                 ptex_face_index, u, v,
805                 coarse_poly_index,
806                 coarse_corner,
807                 subdiv_vertex_index);
808 }
809
810 static void multires_reshape_vertex_every_corner(
811         const struct SubdivForeachContext *foreach_context,
812         void *UNUSED(tls_v),
813         const int ptex_face_index,
814         const float u, const float v,
815         const int UNUSED(coarse_vertex_index),
816         const int coarse_poly_index,
817         const int coarse_corner,
818         const int subdiv_vertex_index)
819 {
820         MultiresReshapeFromDeformedVertsContext *ctx = foreach_context->user_data;
821         multires_reshape_vertex(
822                 ctx,
823                 ptex_face_index, u, v,
824                 coarse_poly_index,
825                 coarse_corner,
826                 subdiv_vertex_index);
827 }
828
829 static void multires_reshape_vertex_every_edge(
830         const struct SubdivForeachContext *foreach_context,
831         void *UNUSED(tls_v),
832         const int ptex_face_index,
833         const float u, const float v,
834         const int UNUSED(coarse_edge_index),
835         const int coarse_poly_index,
836         const int coarse_corner,
837         const int subdiv_vertex_index)
838 {
839         MultiresReshapeFromDeformedVertsContext *ctx = foreach_context->user_data;
840         multires_reshape_vertex(
841                 ctx,
842                 ptex_face_index, u, v,
843                 coarse_poly_index,
844                 coarse_corner,
845                 subdiv_vertex_index);
846 }
847
848 static Subdiv *multires_create_subdiv_for_reshape(
849         struct Depsgraph *depsgraph,
850         /*const*/ Object *object,
851         const MultiresModifierData *mmd)
852 {
853         Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
854         Object *object_eval = DEG_get_evaluated_object(depsgraph, object);
855         Mesh *deformed_mesh = mesh_get_eval_deform(
856                 depsgraph, scene_eval, object_eval, CD_MASK_BAREMESH);
857         SubdivSettings subdiv_settings;
858         BKE_multires_subdiv_settings_init(&subdiv_settings, mmd);
859         Subdiv *subdiv = BKE_subdiv_new_from_mesh(&subdiv_settings, deformed_mesh);
860         if (!BKE_subdiv_eval_update_from_mesh(subdiv, deformed_mesh)) {
861                 BKE_subdiv_free(subdiv);
862                 return NULL;
863         }
864         return subdiv;
865 }
866
867 static bool multires_reshape_from_vertcos(
868         struct Depsgraph *depsgraph,
869         Object *object,
870         const MultiresModifierData *mmd,
871         const float (*deformed_verts)[3],
872         const int num_deformed_verts,
873         const bool use_render_params)
874 {
875         Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
876         Mesh *coarse_mesh = object->data;
877         MDisps *mdisps = CustomData_get_layer(&coarse_mesh->ldata, CD_MDISPS);
878         /* Pick maximum between multires level and dispalcement level.
879          * This is because mesh can be used by objects with multires at different
880          * levels.
881          *
882          * TODO(sergey): At this point it should be possible to always use
883          * mdisps->level.
884          */
885         const int top_level = max_ii(mmd->totlvl, mdisps->level);
886         /* Make sure displacement grids are ready. */
887         multires_reshape_ensure_grids(coarse_mesh, top_level);
888         /* Construct context. */
889         MultiresReshapeFromDeformedVertsContext reshape_deformed_verts_ctx = {
890                 .reshape_ctx = {
891                         .coarse_mesh = coarse_mesh,
892                         .mdisps = mdisps,
893                         .grid_paint_mask = NULL,
894                         .top_grid_size = BKE_subdiv_grid_size_from_level(top_level),
895                         .top_level = top_level,
896                 },
897                 .deformed_verts = deformed_verts,
898                 .num_deformed_verts = num_deformed_verts,
899         };
900         SubdivForeachContext foreach_context = {
901                 .topology_info = multires_reshape_topology_info,
902                 .vertex_inner = multires_reshape_vertex_inner,
903                 .vertex_every_edge = multires_reshape_vertex_every_edge,
904                 .vertex_every_corner = multires_reshape_vertex_every_corner,
905                 .user_data = &reshape_deformed_verts_ctx,
906         };
907         /* Initialize subdivision surface. */
908         Subdiv *subdiv = multires_create_subdiv_for_reshape(depsgraph, object, mmd);
909         if (subdiv == NULL) {
910                 return false;
911         }
912         reshape_deformed_verts_ctx.reshape_ctx.subdiv = subdiv;
913         /* Initialize mesh rasterization settings. */
914         SubdivToMeshSettings mesh_settings;
915         BKE_multires_subdiv_mesh_settings_init(
916         &mesh_settings, scene_eval, object, mmd, use_render_params, true);
917         /* Initialize propagation to higher levels. */
918         MultiresPropagateData propagate_data;
919         multires_reshape_propagate_prepare_from_mmd(
920         &propagate_data, depsgraph, object, mmd, top_level, use_render_params);
921         /* Run all the callbacks. */
922         BKE_subdiv_foreach_subdiv_geometry(
923                 subdiv,
924                 &foreach_context,
925                 &mesh_settings,
926                 coarse_mesh);
927         BKE_subdiv_free(subdiv);
928         /* Update higher levels if needed. */
929         multires_reshape_propagate(&propagate_data);
930         multires_reshape_propagate_free(&propagate_data);
931         return true;
932 }
933
934 /* =============================================================================
935  * Reshape from object.
936  */
937
938 /* Returns truth on success, false otherwise.
939  *
940  * This function might fail in cases like source and destination not having
941  * matched amount of vertices.
942  */
943 bool multiresModifier_reshapeFromObject(
944         struct Depsgraph *depsgraph,
945         MultiresModifierData *mmd,
946         Object *dst,
947         Object *src)
948 {
949         /* Would be cool to support this eventually, but it is very tricky to match
950          * vertices order even for meshes, when mixing meshes and other objects it's
951          * even more tricky.
952          */
953         if (src->type != OB_MESH) {
954                 return false;
955         }
956         MultiresModifierData reshape_mmd;
957         multires_reshape_init_mmd(&reshape_mmd, mmd);
958         /* Get evaluated vertices locations to reshape to. */
959         Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
960         Object *src_eval = DEG_get_evaluated_object(depsgraph, src);
961         Mesh *src_mesh_eval = mesh_get_eval_final(
962                 depsgraph, scene_eval, src_eval, CD_MASK_BAREMESH);
963         int num_deformed_verts;
964         float (*deformed_verts)[3] = BKE_mesh_vertexCos_get(
965                 src_mesh_eval, &num_deformed_verts);
966         bool result = multires_reshape_from_vertcos(
967                 depsgraph,
968                 dst,
969                 &reshape_mmd,
970                 deformed_verts,
971                 num_deformed_verts,
972                 false);
973         MEM_freeN(deformed_verts);
974         return result;
975 }
976
977 /* =============================================================================
978  * Reshape from modifier.
979  */
980
981 bool multiresModifier_reshapeFromDeformModifier(
982         struct Depsgraph *depsgraph,
983         MultiresModifierData *mmd,
984         Object *object,
985         ModifierData *md)
986 {
987         MultiresModifierData highest_mmd;
988         /* It is possible that the current subdivision level of multires is lower
989          * that it's maximum possible one (i.e., viewport is set to a lower level
990          * for the performance purposes). But even then, we want all the multires
991          * levels to be reshaped. Most accurate way to do so is to ignore all
992          * simplifications and calculate deformation modifier for the highest
993          * possible multires level.
994          * Alternative would be propagate displacement from current level to a
995          * higher ones, but that is likely to cause artifacts.
996          */
997         multires_reshape_init_mmd_top_level(&highest_mmd, mmd);
998         Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
999         /* Perform sanity checks and early output. */
1000         if (multires_get_level(
1001                     scene_eval, object, &highest_mmd, false, true) == 0)
1002         {
1003                 return false;
1004         }
1005         /* Create mesh for the multires, ignoring any further modifiers (leading
1006          * deformation modifiers will be applied though).
1007          */
1008         Mesh *multires_mesh = BKE_multires_create_mesh(
1009                 depsgraph, scene_eval, &highest_mmd, object);
1010         int num_deformed_verts;
1011         float (*deformed_verts)[3] = BKE_mesh_vertexCos_get(
1012                 multires_mesh, &num_deformed_verts);
1013         /* Apply deformation modifier on the multires, */
1014         const ModifierEvalContext modifier_ctx = {
1015                 .depsgraph = depsgraph,
1016                 .object = object,
1017                 .flag = MOD_APPLY_USECACHE | MOD_APPLY_IGNORE_SIMPLIFY,
1018         };
1019         modwrap_deformVerts(
1020                 md, &modifier_ctx, multires_mesh, deformed_verts,
1021                 multires_mesh->totvert);
1022         BKE_id_free(NULL, multires_mesh);
1023         /* Reshaping */
1024         bool result = multires_reshape_from_vertcos(
1025                 depsgraph,
1026                 object,
1027                 &highest_mmd,
1028                 deformed_verts,
1029                 num_deformed_verts,
1030                 false);
1031         /* Cleanup */
1032         MEM_freeN(deformed_verts);
1033         return result;
1034 }
1035
1036 /* =============================================================================
1037  * Reshape from grids.
1038  */
1039
1040 typedef struct ReshapeFromCCGTaskData {
1041         MultiresReshapeContext reshape_ctx;
1042         int *face_ptex_offset;
1043         const CCGKey *key;
1044         /*const*/ CCGElem **grids;
1045 } ReshapeFromCCGTaskData;
1046
1047 static void reshape_from_ccg_regular_face(ReshapeFromCCGTaskData *data,
1048                                           const MPoly *coarse_poly)
1049 {
1050         const CCGKey *key = data->key;
1051         /*const*/ CCGElem **grids = data->grids;
1052         const Mesh *coarse_mesh = data->reshape_ctx.coarse_mesh;
1053         const MPoly *coarse_mpoly = coarse_mesh->mpoly;
1054         const int key_grid_size = key->grid_size;
1055         const int key_grid_size_1 = key_grid_size - 1;
1056         const int resolution = 2 * key_grid_size - 1;
1057         const float resolution_1_inv = 1.0f / (float)(resolution - 1);
1058         const int coarse_poly_index = coarse_poly - coarse_mpoly;
1059         const int ptex_face_index = data->face_ptex_offset[coarse_poly_index];
1060         for (int y = 0; y < resolution; y++) {
1061                 const float v = y * resolution_1_inv;
1062                 for (int x = 0; x < resolution; x++) {
1063                         const float u = x * resolution_1_inv;
1064                         float corner_u, corner_v;
1065                         float grid_u, grid_v;
1066                         const int face_corner = BKE_subdiv_rotate_quad_to_corner(
1067                                 u, v, &corner_u, &corner_v);
1068                         BKE_subdiv_ptex_face_uv_to_grid_uv(
1069                                 corner_u, corner_v, &grid_u, &grid_v);
1070                         /*const*/ CCGElem *grid =
1071                                 grids[coarse_poly->loopstart + face_corner];
1072                         /*const*/ CCGElem *grid_element = CCG_grid_elem(
1073                                 key,
1074                                 grid,
1075                                 key_grid_size_1 * grid_u,
1076                                 key_grid_size_1 * grid_v);
1077                         const float *final_P = CCG_elem_co(key, grid_element);
1078                         float final_mask = 0.0f;
1079                         if (key->has_mask) {
1080                                 final_mask = *CCG_elem_mask(key, grid_element);
1081                         }
1082                         multires_reshape_vertex_from_final_data(
1083                                 &data->reshape_ctx,
1084                                 ptex_face_index,
1085                                 u, v,
1086                                 coarse_poly_index,
1087                                 0,
1088                                 final_P, final_mask);
1089                 }
1090         }
1091 }
1092
1093 static void reshape_from_ccg_special_face(ReshapeFromCCGTaskData *data,
1094                                           const MPoly *coarse_poly)
1095 {
1096         const CCGKey *key = data->key;
1097         /*const*/ CCGElem **grids = data->grids;
1098         const Mesh *coarse_mesh = data->reshape_ctx.coarse_mesh;
1099         const MPoly *coarse_mpoly = coarse_mesh->mpoly;
1100         const int key_grid_size = key->grid_size;
1101         const int key_grid_size_1 = key_grid_size - 1;
1102         const int resolution = key_grid_size;
1103         const float resolution_1_inv = 1.0f / (float)(resolution - 1);
1104         const int coarse_poly_index = coarse_poly - coarse_mpoly;
1105         const int ptex_face_index = data->face_ptex_offset[coarse_poly_index];
1106         for (int corner = 0; corner < coarse_poly->totloop; corner++) {
1107                 for (int y = 0; y < resolution; y++) {
1108                         const float v = y * resolution_1_inv;
1109                         for (int x = 0; x < resolution; x++) {
1110                                 const float u = x * resolution_1_inv;
1111                                 float grid_u, grid_v;
1112                                 BKE_subdiv_ptex_face_uv_to_grid_uv(u, v, &grid_u, &grid_v);
1113                                 /*const*/ CCGElem *grid =
1114                                         grids[coarse_poly->loopstart + corner];
1115                                 /*const*/ CCGElem *grid_element = CCG_grid_elem(
1116                                         key,
1117                                         grid,
1118                                         key_grid_size_1 * grid_u,
1119                                         key_grid_size_1 * grid_v);
1120                                 const float *final_P = CCG_elem_co(key, grid_element);
1121                                 float final_mask = 0.0f;
1122                                 if (key->has_mask) {
1123                                         final_mask = *CCG_elem_mask(key, grid_element);
1124                                 }
1125                                 multires_reshape_vertex_from_final_data(
1126                                         &data->reshape_ctx,
1127                                         ptex_face_index + corner,
1128                                         u, v,
1129                                         coarse_poly_index,
1130                                         corner,
1131                                         final_P, final_mask);
1132                         }
1133                 }
1134         }
1135 }
1136
1137 static void reshape_from_ccg_task(
1138         void *__restrict userdata,
1139         const int coarse_poly_index,
1140         const ParallelRangeTLS *__restrict UNUSED(tls))
1141 {
1142         ReshapeFromCCGTaskData *data = userdata;
1143         const Mesh *coarse_mesh = data->reshape_ctx.coarse_mesh;
1144         const MPoly *coarse_mpoly = coarse_mesh->mpoly;
1145         const MPoly *coarse_poly = &coarse_mpoly[coarse_poly_index];
1146         if (coarse_poly->totloop == 4) {
1147                 reshape_from_ccg_regular_face(data, coarse_poly);
1148         }
1149         else {
1150                 reshape_from_ccg_special_face(data, coarse_poly);
1151         }
1152 }
1153
1154 bool multiresModifier_reshapeFromCCG(
1155         const int tot_level,
1156         Mesh *coarse_mesh,
1157         SubdivCCG *subdiv_ccg)
1158 {
1159         CCGKey key;
1160         BKE_subdiv_ccg_key_top_level(&key, subdiv_ccg);
1161         /* Sanity checks. */
1162         if (coarse_mesh->totloop != subdiv_ccg->num_grids) {
1163                 /* Grids are supposed to eb created for each face-cornder (aka loop). */
1164                 return false;
1165         }
1166         MDisps *mdisps = CustomData_get_layer(&coarse_mesh->ldata, CD_MDISPS);
1167         if (mdisps == NULL) {
1168                 /* Multires displacement has been removed before current changes were
1169                  * applies to all the levels. */
1170                 return false;
1171         }
1172         GridPaintMask *grid_paint_mask =
1173                 CustomData_get_layer(&coarse_mesh->ldata, CD_GRID_PAINT_MASK);
1174         Subdiv *subdiv = subdiv_ccg->subdiv;
1175         /* Pick maximum between multires level and dispalcement level.
1176          * This is because mesh can be used by objects with multires at different
1177          * levels.
1178          *
1179          * TODO(sergey): At this point it should be possible to always use
1180          * mdisps->level.
1181          */
1182         const int top_level = max_ii(tot_level, mdisps->level);
1183         /* Make sure displacement grids are ready. */
1184         multires_reshape_ensure_grids(coarse_mesh, top_level);
1185         /* Construct context. */
1186         ReshapeFromCCGTaskData data = {
1187                 .reshape_ctx = {
1188                         .subdiv = subdiv,
1189                         .coarse_mesh = coarse_mesh,
1190                         .mdisps  = mdisps,
1191                         .grid_paint_mask = grid_paint_mask,
1192                         .top_grid_size = BKE_subdiv_grid_size_from_level(top_level),
1193                         .top_level = top_level,
1194                 },
1195                 .face_ptex_offset = BKE_subdiv_face_ptex_offset_get(subdiv),
1196                 .key = &key,
1197                 .grids = subdiv_ccg->grids,
1198         };
1199         /* Initialize propagation to higher levels. */
1200         MultiresPropagateData propagate_data;
1201         multires_reshape_propagate_prepare(
1202                 &propagate_data, coarse_mesh, key.level, top_level);
1203         /* Threaded grids iteration. */
1204         ParallelRangeSettings parallel_range_settings;
1205         BLI_parallel_range_settings_defaults(&parallel_range_settings);
1206         BLI_task_parallel_range(0, coarse_mesh->totpoly,
1207                                 &data,
1208                                 reshape_from_ccg_task,
1209                                 &parallel_range_settings);
1210         /* Update higher levels if needed. */
1211         multires_reshape_propagate(&propagate_data);
1212         multires_reshape_propagate_free(&propagate_data);
1213         return true;
1214 }