Blender  V2.93
multires.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2007 by Nicholas Bishop
17  * All rights reserved.
18  */
19 
24 #include "MEM_guardedalloc.h"
25 
26 /* for reading old multires */
27 #define DNA_DEPRECATED_ALLOW
28 
29 #include "DNA_mesh_types.h"
30 #include "DNA_meshdata_types.h"
31 #include "DNA_object_types.h"
32 #include "DNA_scene_types.h"
33 
34 #include "BLI_bitmap.h"
35 #include "BLI_blenlib.h"
36 #include "BLI_math.h"
37 #include "BLI_task.h"
38 #include "BLI_utildefines.h"
39 
40 #include "BKE_ccg.h"
41 #include "BKE_cdderivedmesh.h"
42 #include "BKE_editmesh.h"
43 #include "BKE_mesh.h"
44 #include "BKE_mesh_mapping.h"
45 #include "BKE_mesh_runtime.h"
46 #include "BKE_modifier.h"
47 #include "BKE_multires.h"
48 #include "BKE_paint.h"
49 #include "BKE_pbvh.h"
50 #include "BKE_scene.h"
51 #include "BKE_subdiv_ccg.h"
52 #include "BKE_subsurf.h"
53 
54 #include "BKE_object.h"
55 
56 #include "CCGSubSurf.h"
57 
58 #include "DEG_depsgraph_query.h"
59 
60 #include "multires_reshape.h"
61 
62 #include <math.h>
63 #include <string.h>
64 
65 /* MULTIRES MODIFIER */
66 static const int multires_grid_tot[] = {
67  0, 4, 9, 25, 81, 289, 1089, 4225, 16641, 66049, 263169, 1050625, 4198401, 16785409};
68 static const int multires_side_tot[] = {
69  0, 2, 3, 5, 9, 17, 33, 65, 129, 257, 513, 1025, 2049, 4097};
70 
71 /* See multiresModifier_disp_run for description of each operation */
72 typedef enum {
76 } DispOp;
77 
78 static void multiresModifier_disp_run(
79  DerivedMesh *dm, Mesh *me, DerivedMesh *dm2, DispOp op, CCGElem **oldGridData, int totlvl);
80 
84 {
85  if (me->edit_mesh) {
86  BMEditMesh *em = me->edit_mesh;
87  /* CustomData_external_remove is used here only to mark layer
88  * as non-external for further free-ing, so zero element count
89  * looks safer than em->totface */
91 
92  if (CustomData_has_layer(&em->bm->ldata, CD_MDISPS)) {
93  BM_data_layer_free(em->bm, &em->bm->ldata, CD_MDISPS);
94  }
95 
98  }
99  }
100  else {
103 
105  }
106 }
107 
110  int lo_level,
111  int hi_level,
112 
113  /* assumed to be at hi_level (or null) */
114  const BLI_bitmap *prev_hidden)
115 {
116  BLI_bitmap *subd;
117  int hi_gridsize = BKE_ccg_gridsize(hi_level);
118  int lo_gridsize = BKE_ccg_gridsize(lo_level);
119  int yh, xh, xl, yl, xo, yo, hi_ndx;
120  int offset, factor;
121 
122  BLI_assert(lo_level <= hi_level);
123 
124  /* fast case */
125  if (lo_level == hi_level) {
126  return MEM_dupallocN(lo_hidden);
127  }
128 
129  subd = BLI_BITMAP_NEW(square_i(hi_gridsize), "MDisps.hidden upsample");
130 
131  factor = BKE_ccg_factor(lo_level, hi_level);
132  offset = 1 << (hi_level - lo_level - 1);
133 
134  /* low-res blocks */
135  for (yl = 0; yl < lo_gridsize; yl++) {
136  for (xl = 0; xl < lo_gridsize; xl++) {
137  int lo_val = BLI_BITMAP_TEST(lo_hidden, yl * lo_gridsize + xl);
138 
139  /* high-res blocks */
140  for (yo = -offset; yo <= offset; yo++) {
141  yh = yl * factor + yo;
142  if (yh < 0 || yh >= hi_gridsize) {
143  continue;
144  }
145 
146  for (xo = -offset; xo <= offset; xo++) {
147  xh = xl * factor + xo;
148  if (xh < 0 || xh >= hi_gridsize) {
149  continue;
150  }
151 
152  hi_ndx = yh * hi_gridsize + xh;
153 
154  if (prev_hidden) {
155  /* If prev_hidden is available, copy it to
156  * subd, except when the equivalent element in
157  * lo_hidden is different */
158  if (lo_val != prev_hidden[hi_ndx]) {
159  BLI_BITMAP_SET(subd, hi_ndx, lo_val);
160  }
161  else {
162  BLI_BITMAP_SET(subd, hi_ndx, prev_hidden[hi_ndx]);
163  }
164  }
165  else {
166  BLI_BITMAP_SET(subd, hi_ndx, lo_val);
167  }
168  }
169  }
170  }
171  }
172 
173  return subd;
174 }
175 
177  int old_level,
178  int new_level)
179 {
180  BLI_bitmap *new_hidden;
181  int new_gridsize = BKE_ccg_gridsize(new_level);
182  int old_gridsize = BKE_ccg_gridsize(old_level);
183  int x, y, factor, old_value;
184 
185  BLI_assert(new_level <= old_level);
186  factor = BKE_ccg_factor(new_level, old_level);
187  new_hidden = BLI_BITMAP_NEW(square_i(new_gridsize), "downsample hidden");
188 
189  for (y = 0; y < new_gridsize; y++) {
190  for (x = 0; x < new_gridsize; x++) {
191  old_value = BLI_BITMAP_TEST(old_hidden, factor * y * old_gridsize + x * factor);
192 
193  BLI_BITMAP_SET(new_hidden, y * new_gridsize + x, old_value);
194  }
195  }
196 
197  return new_hidden;
198 }
199 
200 static void multires_output_hidden_to_ccgdm(CCGDerivedMesh *ccgdm, Mesh *me, int level)
201 {
202  const MDisps *mdisps = CustomData_get_layer(&me->ldata, CD_MDISPS);
203  BLI_bitmap **grid_hidden = ccgdm->gridHidden;
204  int *gridOffset;
205  int i, j;
206 
207  gridOffset = ccgdm->dm.getGridOffset(&ccgdm->dm);
208 
209  for (i = 0; i < me->totpoly; i++) {
210  for (j = 0; j < me->mpoly[i].totloop; j++) {
211  int g = gridOffset[i] + j;
212  const MDisps *md = &mdisps[g];
213  BLI_bitmap *gh = md->hidden;
214 
215  if (gh) {
216  grid_hidden[g] = multires_mdisps_downsample_hidden(gh, md->level, level);
217  }
218  }
219  }
220 }
221 
222 /* subdivide mdisps.hidden if needed (assumes that md.level reflects
223  * the current level of md.hidden) */
224 static void multires_mdisps_subdivide_hidden(MDisps *md, int new_level)
225 {
226  BLI_bitmap *subd;
227 
228  BLI_assert(md->hidden);
229 
230  /* nothing to do if already subdivided enough */
231  if (md->level >= new_level) {
232  return;
233  }
234 
235  subd = multires_mdisps_upsample_hidden(md->hidden, md->level, new_level, NULL);
236 
237  /* swap in the subdivided data */
238  MEM_freeN(md->hidden);
239  md->hidden = subd;
240 }
241 
243  Object *object,
245 {
246  Object *object_eval = DEG_get_evaluated_object(depsgraph, object);
247  Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
248  Mesh *deformed_mesh = mesh_get_eval_deform(
249  depsgraph, scene_eval, object_eval, &CD_MASK_BAREMESH);
250  ModifierEvalContext modifier_ctx = {
251  .depsgraph = depsgraph,
252  .object = object_eval,
254  };
255 
257  Mesh *result = mti->modifyMesh(&mmd->modifier, &modifier_ctx, deformed_mesh);
258 
259  if (result == deformed_mesh) {
260  result = BKE_mesh_copy_for_eval(deformed_mesh, true);
261  }
262  return result;
263 }
264 
266  struct Object *object,
267  struct MultiresModifierData *mmd,
268  int *r_num_deformed_verts))[3]
269 {
270  Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
271  Object *object_eval = DEG_get_evaluated_object(depsgraph, object);
272 
273  Object object_for_eval = *object_eval;
274  object_for_eval.data = object->data;
275  object_for_eval.sculpt = NULL;
276 
277  const bool use_render = (DEG_get_mode(depsgraph) == DAG_EVAL_RENDER);
278  ModifierEvalContext mesh_eval_context = {depsgraph, &object_for_eval, 0};
279  if (use_render) {
280  mesh_eval_context.flag |= MOD_APPLY_RENDER;
281  }
282  const int required_mode = use_render ? eModifierMode_Render : eModifierMode_Realtime;
283 
284  VirtualModifierData virtual_modifier_data;
285  ModifierData *first_md = BKE_modifiers_get_virtual_modifierlist(&object_for_eval,
286  &virtual_modifier_data);
287 
288  Mesh *base_mesh = object->data;
289 
290  int num_deformed_verts;
291  float(*deformed_verts)[3] = BKE_mesh_vert_coords_alloc(base_mesh, &num_deformed_verts);
292 
293  for (ModifierData *md = first_md; md != NULL; md = md->next) {
294  const ModifierTypeInfo *mti = BKE_modifier_get_info(md->type);
295 
296  if (md == &mmd->modifier) {
297  break;
298  }
299 
300  if (!BKE_modifier_is_enabled(scene_eval, md, required_mode)) {
301  continue;
302  }
303 
304  if (mti->type != eModifierTypeType_OnlyDeform) {
305  break;
306  }
307 
309  md, &mesh_eval_context, base_mesh, deformed_verts, num_deformed_verts);
310  }
311 
312  if (r_num_deformed_verts != NULL) {
313  *r_num_deformed_verts = num_deformed_verts;
314  }
315  return deformed_verts;
316 }
317 
319 {
320  ModifierData *md;
321 
322  for (md = lastmd; md; md = md->prev) {
323  if (md->type == eModifierType_Multires) {
325  return (MultiresModifierData *)md;
326  }
327  }
328  }
329 
330  return NULL;
331 }
332 
333 /* used for applying scale on mdisps layer and syncing subdivide levels when joining objects
334  * use_first - return first multires modifier if all multires'es are disabled
335  */
337 {
338  ModifierData *md;
339  MultiresModifierData *mmd = NULL, *firstmmd = NULL;
340 
341  /* find first active multires modifier */
342  for (md = ob->modifiers.first; md; md = md->next) {
343  if (md->type == eModifierType_Multires) {
344  if (!firstmmd) {
345  firstmmd = (MultiresModifierData *)md;
346  }
347 
349  mmd = (MultiresModifierData *)md;
350  break;
351  }
352  }
353  }
354 
355  if (!mmd && use_first) {
356  /* active multires have not been found
357  * try to use first one */
358  return firstmmd;
359  }
360 
361  return mmd;
362 }
363 
365  const Object *ob,
366  const MultiresModifierData *mmd,
367  bool render,
368  bool ignore_simplify)
369 {
370  if (render) {
371  return (scene != NULL) ? get_render_subsurf_level(&scene->r, mmd->renderlvl, true) :
372  mmd->renderlvl;
373  }
374  if (ob->mode == OB_MODE_SCULPT) {
375  return mmd->sculptlvl;
376  }
377  if (ignore_simplify) {
378  return mmd->lvl;
379  }
380 
381  return (scene != NULL) ? get_render_subsurf_level(&scene->r, mmd->lvl, false) : mmd->lvl;
382 }
383 
385 {
386  mmd->totlvl = lvl;
387 
388  if (ob->mode != OB_MODE_SCULPT) {
389  mmd->lvl = CLAMPIS(MAX2(mmd->lvl, lvl), 0, mmd->totlvl);
390  }
391 
392  mmd->sculptlvl = CLAMPIS(MAX2(mmd->sculptlvl, lvl), 0, mmd->totlvl);
393  mmd->renderlvl = CLAMPIS(MAX2(mmd->renderlvl, lvl), 0, mmd->totlvl);
394 }
395 
397 {
398  if (flags & MULTIRES_COORDS_MODIFIED) {
399  subdiv_ccg->dirty.coords = true;
400  }
401  if (flags & MULTIRES_HIDDEN_MODIFIED) {
402  subdiv_ccg->dirty.hidden = true;
403  }
404 }
405 
407 {
408  if (object == NULL) {
409  return;
410  }
411  /* NOTE: CCG live inside of evaluated object.
412  *
413  * While this is a bit weird to tag the only one, this is how other areas were built
414  * historically: they are tagging multires for update and then rely on object re-evaluation to
415  * do an actual update.
416  *
417  * In a longer term maybe special dependency graph tag can help sanitizing this a bit. */
418  Object *object_eval = DEG_get_evaluated_object(depsgraph, object);
419  Mesh *mesh = object_eval->data;
420  SubdivCCG *subdiv_ccg = mesh->runtime.subdiv_ccg;
421  if (subdiv_ccg == NULL) {
422  return;
423  }
424  multires_ccg_mark_as_modified(subdiv_ccg, flags);
425 }
426 
428 {
429  if (object == NULL || object->sculpt == NULL || object->sculpt->pbvh == NULL) {
430  return;
431  }
432 
433  SculptSession *sculpt_session = object->sculpt;
434  if (BKE_pbvh_type(sculpt_session->pbvh) != PBVH_GRIDS || !sculpt_session->multires.active ||
435  sculpt_session->multires.modifier == NULL) {
436  return;
437  }
438 
439  SubdivCCG *subdiv_ccg = sculpt_session->subdiv_ccg;
440  if (subdiv_ccg == NULL) {
441  return;
442  }
443 
444  if (!subdiv_ccg->dirty.coords && !subdiv_ccg->dirty.hidden) {
445  return;
446  }
447 
448  Mesh *mesh = object->data;
450  sculpt_session->multires.modifier->totlvl, mesh, sculpt_session->subdiv_ccg);
451 
452  subdiv_ccg->dirty.coords = false;
453  subdiv_ccg->dirty.hidden = false;
454 }
455 
457 {
459 
460  if (object == NULL || object->sculpt == NULL) {
461  return;
462  }
463 
464  SculptSession *ss = object->sculpt;
465 
466  if (ss->pbvh != NULL) {
467  BKE_pbvh_free(ss->pbvh);
468  object->sculpt->pbvh = NULL;
469  }
470 
471  if (ss->pmap != NULL) {
472  MEM_freeN(ss->pmap);
473  ss->pmap = NULL;
474  }
475 
476  if (ss->pmap_mem != NULL) {
477  MEM_freeN(ss->pmap_mem);
478  ss->pmap_mem = NULL;
479  }
480 }
481 
483 {
484  Mesh *mesh = BKE_mesh_from_object(object);
485 
488 }
489 
490 /* reset the multires levels to match the number of mdisps */
492 {
493  Mesh *me = ob->data;
494  MDisps *mdisp, *md;
495  int i, j, totlvl = 0;
496 
497  mdisp = CustomData_get_layer(&me->ldata, CD_MDISPS);
498 
499  for (i = 0; i < me->totpoly; i++) {
500  md = mdisp + me->mpoly[i].loopstart;
501 
502  for (j = 0; j < me->mpoly[i].totloop; j++, md++) {
503  if (md->totdisp == 0) {
504  continue;
505  }
506 
507  while (1) {
508  int side = (1 << (totlvl - 1)) + 1;
509  int lvl_totdisp = side * side;
510  if (md->totdisp == lvl_totdisp) {
511  break;
512  }
513  if (md->totdisp < lvl_totdisp) {
514  totlvl--;
515  }
516  else {
517  totlvl++;
518  }
519  }
520 
521  break;
522  }
523  }
524 
525  return totlvl;
526 }
527 
528 /* reset the multires levels to match the number of mdisps */
530 {
531  Mesh *me = ob->data;
532  MDisps *mdisp;
533 
534  if (me->edit_mesh) {
536  }
537  else {
538  mdisp = CustomData_get_layer(&me->ldata, CD_MDISPS);
539  }
540 
541  if (mdisp) {
542  mmd->totlvl = get_levels_from_disps(ob);
543  mmd->lvl = MIN2(mmd->sculptlvl, mmd->totlvl);
544  mmd->sculptlvl = MIN2(mmd->sculptlvl, mmd->totlvl);
545  mmd->renderlvl = MIN2(mmd->renderlvl, mmd->totlvl);
546  }
547 }
548 
549 static void multires_set_tot_mdisps(Mesh *me, int lvl)
550 {
551  MDisps *mdisps = CustomData_get_layer(&me->ldata, CD_MDISPS);
552  int i;
553 
554  if (mdisps) {
555  for (i = 0; i < me->totloop; i++, mdisps++) {
556  mdisps->totdisp = multires_grid_tot[lvl];
557  mdisps->level = lvl;
558  }
559  }
560 }
561 
562 static void multires_reallocate_mdisps(int totloop, MDisps *mdisps, int lvl)
563 {
564  int i;
565 
566  /* reallocate displacements to be filled in */
567  for (i = 0; i < totloop; i++) {
568  int totdisp = multires_grid_tot[lvl];
569  float(*disps)[3] = MEM_calloc_arrayN(totdisp, sizeof(float[3]), "multires disps");
570 
571  if (mdisps[i].disps) {
572  MEM_freeN(mdisps[i].disps);
573  }
574 
575  if (mdisps[i].level && mdisps[i].hidden) {
576  multires_mdisps_subdivide_hidden(&mdisps[i], lvl);
577  }
578 
579  mdisps[i].disps = disps;
580  mdisps[i].totdisp = totdisp;
581  mdisps[i].level = lvl;
582  }
583 }
584 
585 static void multires_copy_grid(float (*gridA)[3], float (*gridB)[3], int sizeA, int sizeB)
586 {
587  int x, y, j, skip;
588 
589  if (sizeA > sizeB) {
590  skip = (sizeA - 1) / (sizeB - 1);
591 
592  for (j = 0, y = 0; y < sizeB; y++) {
593  for (x = 0; x < sizeB; x++, j++) {
594  copy_v3_v3(gridA[y * skip * sizeA + x * skip], gridB[j]);
595  }
596  }
597  }
598  else {
599  skip = (sizeB - 1) / (sizeA - 1);
600 
601  for (j = 0, y = 0; y < sizeA; y++) {
602  for (x = 0; x < sizeA; x++, j++) {
603  copy_v3_v3(gridA[j], gridB[y * skip * sizeB + x * skip]);
604  }
605  }
606  }
607 }
608 
609 static void multires_copy_dm_grid(CCGElem *gridA, CCGElem *gridB, CCGKey *keyA, CCGKey *keyB)
610 {
611  int x, y, j, skip;
612 
613  if (keyA->grid_size > keyB->grid_size) {
614  skip = (keyA->grid_size - 1) / (keyB->grid_size - 1);
615 
616  for (j = 0, y = 0; y < keyB->grid_size; y++) {
617  for (x = 0; x < keyB->grid_size; x++, j++) {
618  memcpy(CCG_elem_offset_co(keyA, gridA, y * skip * keyA->grid_size + x * skip),
619  CCG_elem_offset_co(keyB, gridB, j),
620  keyA->elem_size);
621  }
622  }
623  }
624  else {
625  skip = (keyB->grid_size - 1) / (keyA->grid_size - 1);
626 
627  for (j = 0, y = 0; y < keyA->grid_size; y++) {
628  for (x = 0; x < keyA->grid_size; x++, j++) {
629  memcpy(CCG_elem_offset_co(keyA, gridA, j),
630  CCG_elem_offset_co(keyB, gridB, y * skip * keyB->grid_size + x * skip),
631  keyA->elem_size);
632  }
633  }
634  }
635 }
636 
637 /* Reallocate gpm->data at a lower resolution and copy values over
638  * from the original high-resolution data */
640 {
641  if (level < gpm->level) {
642  int gridsize = BKE_ccg_gridsize(level);
643  float *data = MEM_calloc_arrayN(
644  square_i(gridsize), sizeof(float), "multires_grid_paint_mask_downsample");
645  int x, y;
646 
647  for (y = 0; y < gridsize; y++) {
648  for (x = 0; x < gridsize; x++) {
649  data[y * gridsize + x] = paint_grid_paint_mask(gpm, level, x, y);
650  }
651  }
652 
653  MEM_freeN(gpm->data);
654  gpm->data = data;
655  gpm->level = level;
656  }
657 }
658 
659 static void multires_del_higher(MultiresModifierData *mmd, Object *ob, int lvl)
660 {
661  Mesh *me = (Mesh *)ob->data;
662  int levels = mmd->totlvl - lvl;
663  MDisps *mdisps;
664  GridPaintMask *gpm;
665 
668  mdisps = CustomData_get_layer(&me->ldata, CD_MDISPS);
670 
672 
673  if (mdisps && levels > 0) {
674  if (lvl > 0) {
675  /* MLoop *ml = me->mloop; */ /*UNUSED*/
676  int nsize = multires_side_tot[lvl];
677  int hsize = multires_side_tot[mmd->totlvl];
678  int i, j;
679 
680  for (i = 0; i < me->totpoly; i++) {
681  for (j = 0; j < me->mpoly[i].totloop; j++) {
682  int g = me->mpoly[i].loopstart + j;
683  MDisps *mdisp = &mdisps[g];
684  float(*disps)[3], (*ndisps)[3], (*hdisps)[3];
685  int totdisp = multires_grid_tot[lvl];
686 
687  disps = MEM_calloc_arrayN(totdisp, sizeof(float[3]), "multires disps");
688 
689  if (mdisp->disps != NULL) {
690  ndisps = disps;
691  hdisps = mdisp->disps;
692 
693  multires_copy_grid(ndisps, hdisps, nsize, hsize);
694  if (mdisp->hidden) {
695  BLI_bitmap *gh = multires_mdisps_downsample_hidden(mdisp->hidden, mdisp->level, lvl);
696  MEM_freeN(mdisp->hidden);
697  mdisp->hidden = gh;
698  }
699 
700  MEM_freeN(mdisp->disps);
701  }
702 
703  mdisp->disps = disps;
704  mdisp->totdisp = totdisp;
705  mdisp->level = lvl;
706 
707  if (gpm) {
709  }
710  }
711  }
712  }
713  else {
715  }
716  }
717 
718  multires_set_tot_level(ob, mmd, lvl);
719 }
720 
721 /* (direction = 1) for delete higher, (direction = 0) for lower (not implemented yet) */
723  Scene *scene,
724  Object *ob,
725  int direction)
726 {
727  Mesh *me = BKE_mesh_from_object(ob);
728  int lvl = multires_get_level(scene, ob, mmd, false, true);
729  int levels = mmd->totlvl - lvl;
730  MDisps *mdisps;
731 
734  mdisps = CustomData_get_layer(&me->ldata, CD_MDISPS);
735 
737 
738  if (mdisps && levels > 0 && direction == 1) {
739  multires_del_higher(mmd, ob, lvl);
740  }
741 
742  multires_set_tot_level(ob, mmd, lvl);
743 }
744 
746  Object *ob,
747  DerivedMesh *dm,
748  int lvl,
749  int totlvl,
750  bool alloc_paint_mask,
751  int flags)
752 {
753  MultiresModifierData mmd = {{NULL}};
754 
755  mmd.lvl = lvl;
756  mmd.sculptlvl = lvl;
757  mmd.renderlvl = lvl;
758  mmd.totlvl = totlvl;
759 
760  flags |= MULTIRES_USE_LOCAL_MMD;
761  if (alloc_paint_mask) {
762  flags |= MULTIRES_ALLOC_PAINT_MASK;
763  }
764 
765  return multires_make_derived_from_derived(dm, &mmd, scene, ob, flags);
766 }
767 
769  Object *ob,
770  DerivedMesh *dm,
771  int lvl,
772  bool is_simple,
773  bool is_optimal,
774  bool is_plain_uv,
775  bool alloc_paint_mask,
776  bool for_render,
777  SubsurfFlags flags)
778 {
779  SubsurfModifierData smd = {{NULL}};
780 
781  smd.levels = smd.renderLevels = lvl;
782  smd.quality = 3;
783  if (!is_plain_uv) {
785  }
786  else {
788  }
789  if (is_simple) {
791  }
792  if (is_optimal) {
794  }
795 
796  if (ob->mode & OB_MODE_EDIT) {
797  flags |= SUBSURF_IN_EDIT_MODE;
798  }
799  if (alloc_paint_mask) {
800  flags |= SUBSURF_ALLOC_PAINT_MASK;
801  }
802  if (for_render) {
803  flags |= SUBSURF_USE_RENDER_PARAMS;
804  }
805 
806  return subsurf_make_derived_from_derived(dm, &smd, scene, NULL, flags);
807 }
808 
809 static void grid_tangent(const CCGKey *key, int x, int y, int axis, CCGElem *grid, float t[3])
810 {
811  if (axis == 0) {
812  if (x == key->grid_size - 1) {
813  if (y == key->grid_size - 1) {
814  sub_v3_v3v3(
815  t, CCG_grid_elem_co(key, grid, x, y - 1), CCG_grid_elem_co(key, grid, x - 1, y - 1));
816  }
817  else {
818  sub_v3_v3v3(t, CCG_grid_elem_co(key, grid, x, y), CCG_grid_elem_co(key, grid, x - 1, y));
819  }
820  }
821  else {
822  sub_v3_v3v3(t, CCG_grid_elem_co(key, grid, x + 1, y), CCG_grid_elem_co(key, grid, x, y));
823  }
824  }
825  else if (axis == 1) {
826  if (y == key->grid_size - 1) {
827  if (x == key->grid_size - 1) {
828  sub_v3_v3v3(
829  t, CCG_grid_elem_co(key, grid, x - 1, y), CCG_grid_elem_co(key, grid, x - 1, (y - 1)));
830  }
831  else {
832  sub_v3_v3v3(t, CCG_grid_elem_co(key, grid, x, y), CCG_grid_elem_co(key, grid, x, (y - 1)));
833  }
834  }
835  else {
836  sub_v3_v3v3(t, CCG_grid_elem_co(key, grid, x, (y + 1)), CCG_grid_elem_co(key, grid, x, y));
837  }
838  }
839 }
840 
841 /* Construct 3x3 tangent-space matrix in 'mat' */
842 static void grid_tangent_matrix(float mat[3][3], const CCGKey *key, int x, int y, CCGElem *grid)
843 {
844  grid_tangent(key, x, y, 0, grid, mat[0]);
845  normalize_v3(mat[0]);
846 
847  grid_tangent(key, x, y, 1, grid, mat[1]);
848  normalize_v3(mat[1]);
849 
850  copy_v3_v3(mat[2], CCG_grid_elem_no(key, grid, x, y));
851 }
852 
853 typedef struct MultiresThreadedData {
863  float (*smat)[3];
865 
866 static void multires_disp_run_cb(void *__restrict userdata,
867  const int pidx,
868  const TaskParallelTLS *__restrict UNUSED(tls))
869 {
870  MultiresThreadedData *tdata = userdata;
871 
872  DispOp op = tdata->op;
873  CCGElem **gridData = tdata->gridData;
874  CCGElem **subGridData = tdata->subGridData;
875  CCGKey *key = tdata->key;
876  MPoly *mpoly = tdata->mpoly;
877  MDisps *mdisps = tdata->mdisps;
878  GridPaintMask *grid_paint_mask = tdata->grid_paint_mask;
879  int *gridOffset = tdata->gridOffset;
880  int gridSize = tdata->gridSize;
881  int dGridSize = tdata->dGridSize;
882  int dSkip = tdata->dSkip;
883 
884  const int numVerts = mpoly[pidx].totloop;
885  int S, x, y, gIndex = gridOffset[pidx];
886 
887  for (S = 0; S < numVerts; S++, gIndex++) {
888  GridPaintMask *gpm = grid_paint_mask ? &grid_paint_mask[gIndex] : NULL;
889  MDisps *mdisp = &mdisps[mpoly[pidx].loopstart + S];
890  CCGElem *grid = gridData[gIndex];
891  CCGElem *subgrid = subGridData[gIndex];
892  float(*dispgrid)[3] = NULL;
893 
894  dispgrid = mdisp->disps;
895 
896  /* if needed, reallocate multires paint mask */
897  if (gpm && gpm->level < key->level) {
898  gpm->level = key->level;
899  if (gpm->data) {
900  MEM_freeN(gpm->data);
901  }
902  gpm->data = MEM_calloc_arrayN(key->grid_area, sizeof(float), "gpm.data");
903  }
904 
905  for (y = 0; y < gridSize; y++) {
906  for (x = 0; x < gridSize; x++) {
907  float *co = CCG_grid_elem_co(key, grid, x, y);
908  float *sco = CCG_grid_elem_co(key, subgrid, x, y);
909  float *data = dispgrid[dGridSize * y * dSkip + x * dSkip];
910  float mat[3][3], disp[3], d[3], mask;
911 
912  /* construct tangent space matrix */
913  grid_tangent_matrix(mat, key, x, y, subgrid);
914 
915  switch (op) {
916  case APPLY_DISPLACEMENTS:
917  /* Convert displacement to object space
918  * and add to grid points */
919  mul_v3_m3v3(disp, mat, data);
920  add_v3_v3v3(co, sco, disp);
921  break;
922  case CALC_DISPLACEMENTS:
923  /* Calculate displacement between new and old
924  * grid points and convert to tangent space */
925  sub_v3_v3v3(disp, co, sco);
926  invert_m3(mat);
927  mul_v3_m3v3(data, mat, disp);
928  break;
929  case ADD_DISPLACEMENTS:
930  /* Convert subdivided displacements to tangent
931  * space and add to the original displacements */
932  invert_m3(mat);
933  mul_v3_m3v3(d, mat, co);
934  add_v3_v3(data, d);
935  break;
936  }
937 
938  if (gpm) {
939  switch (op) {
940  case APPLY_DISPLACEMENTS:
941  /* Copy mask from gpm to DM */
942  *CCG_grid_elem_mask(key, grid, x, y) = paint_grid_paint_mask(gpm, key->level, x, y);
943  break;
944  case CALC_DISPLACEMENTS:
945  /* Copy mask from DM to gpm */
946  mask = *CCG_grid_elem_mask(key, grid, x, y);
947  gpm->data[y * gridSize + x] = CLAMPIS(mask, 0, 1);
948  break;
949  case ADD_DISPLACEMENTS:
950  /* Add mask displacement to gpm */
951  gpm->data[y * gridSize + x] += *CCG_grid_elem_mask(key, grid, x, y);
952  break;
953  }
954  }
955  }
956  }
957  }
958 }
959 
960 /* XXX WARNING: subsurf elements from dm and oldGridData *must* be of the same format (size),
961  * because this code uses CCGKey's info from dm to access oldGridData's normals
962  * (through the call to grid_tangent_matrix())! */
964  DerivedMesh *dm, Mesh *me, DerivedMesh *dm2, DispOp op, CCGElem **oldGridData, int totlvl)
965 {
966  CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm;
967  CCGElem **gridData, **subGridData;
968  CCGKey key;
969  MPoly *mpoly = me->mpoly;
970  MDisps *mdisps = CustomData_get_layer(&me->ldata, CD_MDISPS);
971  GridPaintMask *grid_paint_mask = NULL;
972  int *gridOffset;
973  int i, gridSize, dGridSize, dSkip;
974  int totloop, totpoly;
975 
976  /* this happens in the dm made by bmesh_mdisps_space_set */
977  if (dm2 && CustomData_has_layer(&dm2->loopData, CD_MDISPS)) {
978  mpoly = CustomData_get_layer(&dm2->polyData, CD_MPOLY);
979  mdisps = CustomData_get_layer(&dm2->loopData, CD_MDISPS);
980  totloop = dm2->numLoopData;
981  totpoly = dm2->numPolyData;
982  }
983  else {
984  totloop = me->totloop;
985  totpoly = me->totpoly;
986  }
987 
988  if (!mdisps) {
989  if (op == CALC_DISPLACEMENTS) {
991  }
992  else {
993  return;
994  }
995  }
996 
997  /*numGrids = dm->getNumGrids(dm);*/ /*UNUSED*/
998  gridSize = dm->getGridSize(dm);
999  gridData = dm->getGridData(dm);
1000  gridOffset = dm->getGridOffset(dm);
1001  dm->getGridKey(dm, &key);
1002  subGridData = (oldGridData) ? oldGridData : gridData;
1003 
1004  dGridSize = multires_side_tot[totlvl];
1005  dSkip = (dGridSize - 1) / (gridSize - 1);
1006 
1007  /* multires paint masks */
1008  if (key.has_mask) {
1009  grid_paint_mask = CustomData_get_layer(&me->ldata, CD_GRID_PAINT_MASK);
1010  }
1011 
1012  /* when adding new faces in edit mode, need to allocate disps */
1013  for (i = 0; i < totloop; i++) {
1014  if (mdisps[i].disps == NULL) {
1015  multires_reallocate_mdisps(totloop, mdisps, totlvl);
1016  break;
1017  }
1018  }
1019 
1020  TaskParallelSettings settings;
1023 
1025  .op = op,
1026  .gridData = gridData,
1027  .subGridData = subGridData,
1028  .key = &key,
1029  .mpoly = mpoly,
1030  .mdisps = mdisps,
1031  .grid_paint_mask = grid_paint_mask,
1032  .gridOffset = gridOffset,
1033  .gridSize = gridSize,
1034  .dGridSize = dGridSize,
1035  .dSkip = dSkip,
1036  };
1037 
1038  BLI_task_parallel_range(0, totpoly, &data, multires_disp_run_cb, &settings);
1039 
1040  if (op == APPLY_DISPLACEMENTS) {
1041  ccgSubSurf_stitchFaces(ccgdm->ss, 0, NULL, 0);
1042  ccgSubSurf_updateNormals(ccgdm->ss, NULL, 0);
1043  }
1044 }
1045 
1047 {
1048  CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm;
1049  Object *ob;
1050  Mesh *me;
1051  MDisps *mdisps;
1052  MultiresModifierData *mmd;
1053 
1054  ob = ccgdm->multires.ob;
1055  me = ccgdm->multires.ob->data;
1056  mmd = ccgdm->multires.mmd;
1057  multires_set_tot_mdisps(me, mmd->totlvl);
1059  mdisps = CustomData_get_layer(&me->ldata, CD_MDISPS);
1060 
1061  if (mdisps) {
1062  int lvl = ccgdm->multires.lvl;
1063  int totlvl = ccgdm->multires.totlvl;
1064 
1065  if (lvl < totlvl) {
1066  DerivedMesh *lowdm, *cddm, *highdm;
1067  CCGElem **highGridData, **lowGridData, **subGridData, **gridData, *diffGrid;
1068  CCGKey highGridKey, lowGridKey;
1069  CCGSubSurf *ss;
1070  int i, j, numGrids, highGridSize, lowGridSize;
1071  const bool has_mask = CustomData_has_layer(&me->ldata, CD_GRID_PAINT_MASK);
1072 
1073  /* Create subsurf DM from original mesh at high level. */
1074  /* TODO: use mesh_deform_eval when sculpting on deformed mesh. */
1075  cddm = CDDM_from_mesh(me);
1077 
1078  highdm = subsurf_dm_create_local(scene,
1079  ob,
1080  cddm,
1081  totlvl,
1082  false,
1083  0,
1085  has_mask,
1086  false,
1088  ss = ((CCGDerivedMesh *)highdm)->ss;
1089 
1090  /* create multires DM from original mesh and displacements */
1091  lowdm = multires_dm_create_local(
1092  scene, ob, cddm, lvl, totlvl, has_mask, MULTIRES_IGNORE_SIMPLIFY);
1093  cddm->release(cddm);
1094 
1095  /* gather grid data */
1096  numGrids = highdm->getNumGrids(highdm);
1097  highGridSize = highdm->getGridSize(highdm);
1098  highGridData = highdm->getGridData(highdm);
1099  highdm->getGridKey(highdm, &highGridKey);
1100  lowGridSize = lowdm->getGridSize(lowdm);
1101  lowGridData = lowdm->getGridData(lowdm);
1102  lowdm->getGridKey(lowdm, &lowGridKey);
1103  gridData = dm->getGridData(dm);
1104 
1105  BLI_assert(highGridKey.elem_size == lowGridKey.elem_size);
1106 
1107  subGridData = MEM_calloc_arrayN(numGrids, sizeof(CCGElem *), "subGridData*");
1108  diffGrid = MEM_calloc_arrayN(lowGridKey.elem_size, lowGridSize * lowGridSize, "diff");
1109 
1110  for (i = 0; i < numGrids; i++) {
1111  /* backup subsurf grids */
1112  subGridData[i] = MEM_calloc_arrayN(
1113  highGridKey.elem_size, highGridSize * highGridSize, "subGridData");
1114  memcpy(
1115  subGridData[i], highGridData[i], highGridKey.elem_size * highGridSize * highGridSize);
1116 
1117  /* write difference of subsurf and displaced low level into high subsurf */
1118  for (j = 0; j < lowGridSize * lowGridSize; j++) {
1119  sub_v4_v4v4(CCG_elem_offset_co(&lowGridKey, diffGrid, j),
1120  CCG_elem_offset_co(&lowGridKey, gridData[i], j),
1121  CCG_elem_offset_co(&lowGridKey, lowGridData[i], j));
1122  }
1123 
1124  multires_copy_dm_grid(highGridData[i], diffGrid, &highGridKey, &lowGridKey);
1125  }
1126 
1127  /* lower level dm no longer needed at this point */
1128  MEM_freeN(diffGrid);
1129  lowdm->release(lowdm);
1130 
1131  /* subsurf higher levels again with difference of coordinates */
1132  ccgSubSurf_updateFromFaces(ss, lvl, NULL, 0);
1133  ccgSubSurf_updateLevels(ss, lvl, NULL, 0);
1134 
1135  /* add to displacements */
1136  multiresModifier_disp_run(highdm, me, NULL, ADD_DISPLACEMENTS, subGridData, mmd->totlvl);
1137 
1138  /* free */
1139  highdm->release(highdm);
1140  for (i = 0; i < numGrids; i++) {
1141  MEM_freeN(subGridData[i]);
1142  }
1143  MEM_freeN(subGridData);
1144  }
1145  else {
1146  DerivedMesh *cddm, *subdm;
1147  const bool has_mask = CustomData_has_layer(&me->ldata, CD_GRID_PAINT_MASK);
1148 
1149  /* TODO: use mesh_deform_eval when sculpting on deformed mesh. */
1150  cddm = CDDM_from_mesh(me);
1152 
1154  ob,
1155  cddm,
1156  mmd->totlvl,
1157  false,
1158  0,
1160  has_mask,
1161  false,
1163  cddm->release(cddm);
1164 
1166  dm, me, NULL, CALC_DISPLACEMENTS, subdm->getGridData(subdm), mmd->totlvl);
1167 
1168  subdm->release(subdm);
1169  }
1170  }
1171 }
1172 
1174 {
1175  CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm;
1176  BLI_bitmap **grid_hidden = ccgdm->gridHidden;
1177  Mesh *me = ccgdm->multires.ob->data;
1178  MDisps *mdisps = CustomData_get_layer(&me->ldata, CD_MDISPS);
1179  int totlvl = ccgdm->multires.totlvl;
1180  int lvl = ccgdm->multires.lvl;
1181 
1182  if (mdisps) {
1183  int i;
1184 
1185  for (i = 0; i < me->totloop; i++) {
1186  MDisps *md = &mdisps[i];
1187  BLI_bitmap *gh = grid_hidden[i];
1188 
1189  if (!gh && md->hidden) {
1190  MEM_freeN(md->hidden);
1191  md->hidden = NULL;
1192  }
1193  else if (gh) {
1194  gh = multires_mdisps_upsample_hidden(gh, lvl, totlvl, md->hidden);
1195  if (md->hidden) {
1196  MEM_freeN(md->hidden);
1197  }
1198 
1199  md->hidden = gh;
1200  }
1201  }
1202  }
1203 }
1204 
1206 {
1207  if (ob == NULL) {
1208  return;
1209  }
1210  SculptSession *sculpt_session = ob->sculpt;
1211  if (sculpt_session == NULL) {
1212  return;
1213  }
1214  PBVH *pbvh = sculpt_session->pbvh;
1215  SubdivCCG *subdiv_ccg = sculpt_session->subdiv_ccg;
1216  if (pbvh == NULL || subdiv_ccg == NULL) {
1217  return;
1218  }
1220  /* NOTE: Currently CCG does not keep track of faces, making it impossible
1221  * to use BKE_pbvh_get_grid_updates().
1222  */
1223  CCGFace **faces;
1224  int num_faces;
1225  BKE_pbvh_get_grid_updates(pbvh, false, (void ***)&faces, &num_faces);
1226  if (num_faces) {
1227  BKE_subdiv_ccg_average_stitch_faces(subdiv_ccg, faces, num_faces);
1228  MEM_freeN(faces);
1229  }
1230 }
1231 
1234 {
1235  Mesh *me = ob->data;
1237  CCGDerivedMesh *ccgdm = NULL;
1238  CCGElem **gridData, **subGridData;
1239  CCGKey key;
1240  const bool render = (flags & MULTIRES_USE_RENDER_PARAMS) != 0;
1241  const bool ignore_simplify = (flags & MULTIRES_IGNORE_SIMPLIFY) != 0;
1242  int lvl = multires_get_level(scene, ob, mmd, render, ignore_simplify);
1243  int i, gridSize, numGrids;
1244 
1245  if (lvl == 0) {
1246  return dm;
1247  }
1248 
1249  const int subsurf_flags = ignore_simplify ? SUBSURF_IGNORE_SIMPLIFY : 0;
1250 
1252  ob,
1253  dm,
1254  lvl,
1255  false,
1258  flags & MULTIRES_ALLOC_PAINT_MASK,
1259  render,
1260  subsurf_flags);
1261 
1262  if (!(flags & MULTIRES_USE_LOCAL_MMD)) {
1263  ccgdm = (CCGDerivedMesh *)result;
1264 
1265  ccgdm->multires.ob = ob;
1266  ccgdm->multires.mmd = mmd;
1267  ccgdm->multires.local_mmd = 0;
1268  ccgdm->multires.lvl = lvl;
1269  ccgdm->multires.totlvl = mmd->totlvl;
1270  ccgdm->multires.modified_flags = 0;
1271  }
1272 
1273  numGrids = result->getNumGrids(result);
1274  gridSize = result->getGridSize(result);
1275  gridData = result->getGridData(result);
1276  result->getGridKey(result, &key);
1277 
1278  subGridData = MEM_malloc_arrayN(numGrids, sizeof(CCGElem *), "subGridData*");
1279 
1280  for (i = 0; i < numGrids; i++) {
1281  subGridData[i] = MEM_malloc_arrayN(key.elem_size, gridSize * gridSize, "subGridData");
1282  memcpy(subGridData[i], gridData[i], key.elem_size * gridSize * gridSize);
1283  }
1284 
1285  multires_set_tot_mdisps(me, mmd->totlvl);
1287 
1288  /*run displacement*/
1289  multiresModifier_disp_run(result, ob->data, dm, APPLY_DISPLACEMENTS, subGridData, mmd->totlvl);
1290 
1291  /* copy hidden elements for this level */
1292  if (ccgdm) {
1293  multires_output_hidden_to_ccgdm(ccgdm, me, lvl);
1294  }
1295 
1296  for (i = 0; i < numGrids; i++) {
1297  MEM_freeN(subGridData[i]);
1298  }
1299  MEM_freeN(subGridData);
1300 
1301  return result;
1302 }
1303 
1304 /* Adapted from sculptmode.c */
1305 void old_mdisps_bilinear(float out[3], float (*disps)[3], const int st, float u, float v)
1306 {
1307  int x, y, x2, y2;
1308  const int st_max = st - 1;
1309  float urat, vrat, uopp;
1310  float d[4][3], d2[2][3];
1311 
1312  if (!disps || isnan(u) || isnan(v)) {
1313  return;
1314  }
1315 
1316  if (u < 0) {
1317  u = 0;
1318  }
1319  else if (u >= st) {
1320  u = st_max;
1321  }
1322  if (v < 0) {
1323  v = 0;
1324  }
1325  else if (v >= st) {
1326  v = st_max;
1327  }
1328 
1329  x = floor(u);
1330  y = floor(v);
1331  x2 = x + 1;
1332  y2 = y + 1;
1333 
1334  if (x2 >= st) {
1335  x2 = st_max;
1336  }
1337  if (y2 >= st) {
1338  y2 = st_max;
1339  }
1340 
1341  urat = u - x;
1342  vrat = v - y;
1343  uopp = 1 - urat;
1344 
1345  mul_v3_v3fl(d[0], disps[y * st + x], uopp);
1346  mul_v3_v3fl(d[1], disps[y * st + x2], urat);
1347  mul_v3_v3fl(d[2], disps[y2 * st + x], uopp);
1348  mul_v3_v3fl(d[3], disps[y2 * st + x2], urat);
1349 
1350  add_v3_v3v3(d2[0], d[0], d[1]);
1351  add_v3_v3v3(d2[1], d[2], d[3]);
1352  mul_v3_fl(d2[0], 1 - vrat);
1353  mul_v3_fl(d2[1], vrat);
1354 
1355  add_v3_v3v3(out, d2[0], d2[1]);
1356 }
1357 
1358 /* If 'ob_src' and 'ob_dst' both have multires modifiers, synchronize them
1359  * such that 'ob_dst' has the same total number of levels as 'ob_src'. */
1361  MultiresModifierData *mmd_src,
1362  MultiresModifierData *mmd_dst)
1363 {
1364  if (mmd_src->totlvl == mmd_dst->totlvl) {
1365  return;
1366  }
1367 
1368  if (mmd_src->totlvl > mmd_dst->totlvl) {
1370  ob_dst, mmd_dst, mmd_src->totlvl, MULTIRES_SUBDIVIDE_CATMULL_CLARK);
1371  }
1372  else {
1373  multires_del_higher(mmd_dst, ob_dst, mmd_src->totlvl);
1374  }
1375 }
1376 
1377 static void multires_sync_levels(Scene *scene, Object *ob_src, Object *ob_dst)
1378 {
1379  MultiresModifierData *mmd_src = get_multires_modifier(scene, ob_src, true);
1380  MultiresModifierData *mmd_dst = get_multires_modifier(scene, ob_dst, true);
1381 
1382  if (!mmd_src) {
1383  /* object could have MDISP even when there is no multires modifier
1384  * this could lead to troubles due to i've got no idea how mdisp could be
1385  * up-sampled correct without modifier data.
1386  * just remove mdisps if no multires present (nazgul) */
1387 
1389  }
1390 
1391  if (mmd_src && mmd_dst) {
1392  multiresModifier_sync_levels_ex(ob_dst, mmd_src, mmd_dst);
1393  }
1394 }
1395 
1396 static void multires_apply_uniform_scale(Object *object, const float scale)
1397 {
1398  Mesh *mesh = (Mesh *)object->data;
1400  for (int i = 0; i < mesh->totloop; i++) {
1401  MDisps *grid = &mdisps[i];
1402  for (int j = 0; j < grid->totdisp; j++) {
1403  mul_v3_fl(grid->disps[j], scale);
1404  }
1405  }
1406 }
1407 
1409  Scene *scene,
1410  Object *object,
1411  const float smat[3][3])
1412 {
1413  const MultiresModifierData *mmd = get_multires_modifier(scene, object, true);
1414  if (mmd == NULL || mmd->totlvl == 0) {
1415  return;
1416  }
1417  /* Make sure layer present. */
1418  Mesh *mesh = (Mesh *)object->data;
1421  return;
1422  }
1423  if (is_uniform_scaled_m3(smat)) {
1424  const float scale = mat3_to_scale(smat);
1425  multires_apply_uniform_scale(object, scale);
1426  }
1427  else {
1428  /* TODO(sergey): This branch of code actually requires more work to
1429  * preserve all the details.
1430  */
1431  const float scale = mat3_to_scale(smat);
1432  multires_apply_uniform_scale(object, scale);
1433  }
1434 }
1435 
1437 {
1438  int lvl = 13;
1439 
1440  while (lvl > 0) {
1441  int side = (1 << (lvl - 1)) + 1;
1442  if ((s->totdisp % (side * side)) == 0) {
1443  return s->totdisp / (side * side);
1444  }
1445  lvl--;
1446  }
1447 
1448  return 0;
1449 }
1450 
1452 {
1453  float smat[3][3];
1454 
1455  /* object's scale matrix */
1456  BKE_object_scale_to_mat3(ob, smat);
1457 
1458  multires_apply_smat(depsgraph, scene, ob, smat);
1459 }
1460 
1462  Scene *scene,
1463  Object *ob,
1464  Object *to_ob)
1465 {
1466  float smat[3][3], tmat[3][3], mat[3][3];
1467  multires_sync_levels(scene, to_ob, ob);
1468 
1469  /* construct scale matrix for displacement */
1470  BKE_object_scale_to_mat3(to_ob, tmat);
1471  invert_m3(tmat);
1472  BKE_object_scale_to_mat3(ob, smat);
1473  mul_m3_m3m3(mat, smat, tmat);
1474 
1475  multires_apply_smat(depsgraph, scene, ob, mat);
1476 }
1477 
1478 /* update multires data after topology changing */
1480 {
1481  MDisps *mdisp = NULL, *cur = NULL;
1482  int i, grid = 0;
1483 
1485  mdisp = CustomData_get_layer(&me->ldata, CD_MDISPS);
1486 
1487  if (!mdisp) {
1488  return;
1489  }
1490 
1491  cur = mdisp;
1492  for (i = 0; i < me->totloop; i++, cur++) {
1493  if (cur->totdisp) {
1494  grid = mdisp->totdisp;
1495 
1496  break;
1497  }
1498  }
1499 
1500  for (i = 0; i < me->totloop; i++, mdisp++) {
1501  /* allocate memory for mdisp, the whole disp layer would be erased otherwise */
1502  if (!mdisp->totdisp || !mdisp->disps) {
1503  if (grid) {
1504  mdisp->totdisp = grid;
1505  mdisp->disps = MEM_calloc_arrayN(sizeof(float[3]), mdisp->totdisp, "mdisp topology");
1506  }
1507 
1508  continue;
1509  }
1510  }
1511 }
1512 
1513 /* Makes sure data from an external file is fully read.
1514  *
1515  * Since the multires data files only contain displacement vectors without knowledge about
1516  * subdivision level some extra work is needed. Namely make is to all displacement grids have
1517  * proper level and number of displacement vectors set. */
1518 void multires_ensure_external_read(struct Mesh *mesh, int top_level)
1519 {
1521  return;
1522  }
1523 
1525  if (mdisps == NULL) {
1527  }
1528 
1529  const int totloop = mesh->totloop;
1530 
1531  for (int i = 0; i < totloop; ++i) {
1532  if (mdisps[i].level != top_level) {
1533  MEM_SAFE_FREE(mdisps[i].disps);
1534  }
1535 
1536  /* NOTE: CustomData_external_read will take care of allocation of displacement vectors if
1537  * they are missing. */
1538 
1539  const int totdisp = multires_grid_tot[top_level];
1540  mdisps[i].totdisp = totdisp;
1541  mdisps[i].level = top_level;
1542  }
1543 
1545 }
1547 {
1549 }
1550 
1551 /***************** Multires interpolation stuff *****************/
1552 
1553 /* Find per-corner coordinate with given per-face UV coord */
1555  struct MPoly *mpoly,
1556  struct MLoop *UNUSED(mloop),
1557  const struct MLoopTri *UNUSED(lt),
1558  const int face_side,
1559  const float u,
1560  const float v,
1561  float *x,
1562  float *y)
1563 {
1564  const float offset = face_side * 0.5f - 0.5f;
1565  int S = 0;
1566 
1567  if (mpoly->totloop == 4) {
1568  if (u <= offset && v <= offset) {
1569  S = 0;
1570  }
1571  else if (u > offset && v <= offset) {
1572  S = 1;
1573  }
1574  else if (u > offset && v > offset) {
1575  S = 2;
1576  }
1577  else if (u <= offset && v >= offset) {
1578  S = 3;
1579  }
1580 
1581  if (S == 0) {
1582  *y = offset - u;
1583  *x = offset - v;
1584  }
1585  else if (S == 1) {
1586  *x = u - offset;
1587  *y = offset - v;
1588  }
1589  else if (S == 2) {
1590  *y = u - offset;
1591  *x = v - offset;
1592  }
1593  else if (S == 3) {
1594  *x = offset - u;
1595  *y = v - offset;
1596  }
1597  }
1598  else if (mpoly->totloop == 3) {
1599  int grid_size = offset;
1600  float w = (face_side - 1) - u - v;
1601  float W1, W2;
1602 
1603  if (u >= v && u >= w) {
1604  S = 0;
1605  W1 = w;
1606  W2 = v;
1607  }
1608  else if (v >= u && v >= w) {
1609  S = 1;
1610  W1 = u;
1611  W2 = w;
1612  }
1613  else {
1614  S = 2;
1615  W1 = v;
1616  W2 = u;
1617  }
1618 
1619  W1 /= (face_side - 1);
1620  W2 /= (face_side - 1);
1621 
1622  *x = (1 - (2 * W1) / (1 - W2)) * grid_size;
1623  *y = (1 - (2 * W2) / (1 - W1)) * grid_size;
1624  }
1625  else {
1626  /* the complicated ngon case: find the actual coordinate from
1627  * the barycentric coordinates and finally find the closest vertex
1628  * should work reliably for convex cases only but better than nothing */
1629 
1630 #if 0
1631  int minS, i;
1632  float mindist = FLT_MAX;
1633 
1634  for (i = 0; i < mpoly->totloop; i++) {
1635  float len = len_v3v3(NULL, mvert[mloop[mpoly->loopstart + i].v].co);
1636  if (len < mindist) {
1637  mindist = len;
1638  minS = i;
1639  }
1640  }
1641  S = minS;
1642 #endif
1643  /* temp not implemented yet and also not working properly in current master.
1644  * (was worked around by subdividing once) */
1645  S = 0;
1646  *x = 0;
1647  *y = 0;
1648  }
1649 
1650  return S;
1651 }
typedef float(TangentPoint)[2]
void DM_set_only_copy(DerivedMesh *dm, const struct CustomData_MeshMasks *mask)
BLI_INLINE float * CCG_grid_elem_co(const CCGKey *key, CCGElem *elem, int x, int y)
Definition: BKE_ccg.h:130
BLI_INLINE float * CCG_grid_elem_no(const CCGKey *key, CCGElem *elem, int x, int y)
Definition: BKE_ccg.h:135
struct CCGElem CCGElem
Definition: BKE_ccg.h:46
BLI_INLINE float * CCG_elem_offset_co(const CCGKey *key, CCGElem *elem, int offset)
Definition: BKE_ccg.h:145
BLI_INLINE float * CCG_grid_elem_mask(const CCGKey *key, CCGElem *elem, int x, int y)
Definition: BKE_ccg.h:140
struct DerivedMesh * CDDM_from_mesh(struct Mesh *mesh)
bool CustomData_free_layer_active(struct CustomData *data, int type, int totelem)
Definition: customdata.c:2707
@ CD_DEFAULT
bool CustomData_has_layer(const struct CustomData *data, int type)
void CustomData_external_reload(struct CustomData *data, struct ID *id, CustomDataMask mask, int totelem)
const CustomData_MeshMasks CD_MASK_BAREMESH
Definition: customdata.c:1919
void * CustomData_get_layer(const struct CustomData *data, int type)
bool CustomData_external_test(struct CustomData *data, int type)
Definition: customdata.c:4748
void * CustomData_add_layer(struct CustomData *data, int type, eCDAllocType alloctype, void *layer, int totelem)
Definition: customdata.c:2620
void CustomData_external_read(struct CustomData *data, struct ID *id, CustomDataMask mask, int totelem)
Definition: customdata.c:4523
void CustomData_external_remove(struct CustomData *data, struct ID *id, int type, int totelem)
Definition: customdata.c:4724
struct Mesh * BKE_mesh_from_object(struct Object *ob)
Definition: mesh.c:1271
struct Mesh * BKE_mesh_copy_for_eval(struct Mesh *source, bool reference)
Definition: mesh.c:995
float(* BKE_mesh_vert_coords_alloc(const struct Mesh *mesh, int *r_vert_len))[3]
struct Mesh * mesh_get_eval_deform(struct Depsgraph *depsgraph, struct Scene *scene, struct Object *ob, const struct CustomData_MeshMasks *dataMask)
const ModifierTypeInfo * BKE_modifier_get_info(ModifierType type)
bool BKE_modifier_is_enabled(const struct Scene *scene, struct ModifierData *md, int required_mode)
void BKE_modifier_deform_verts(ModifierData *md, const struct ModifierEvalContext *ctx, struct Mesh *me, float(*vertexCos)[3], int numVerts)
struct ModifierData * BKE_modifiers_get_virtual_modifierlist(const struct Object *ob, struct VirtualModifierData *data)
@ eModifierTypeType_OnlyDeform
Definition: BKE_modifier.h:58
@ MOD_APPLY_USECACHE
Definition: BKE_modifier.h:131
@ MOD_APPLY_RENDER
Definition: BKE_modifier.h:128
@ MOD_APPLY_IGNORE_SIMPLIFY
Definition: BKE_modifier.h:136
void multiresModifier_subdivide_to_level(struct Object *object, struct MultiresModifierData *mmd, const int top_level, const eMultiresSubdivideModeType mode)
bool multiresModifier_reshapeFromCCG(const int tot_level, struct Mesh *coarse_mesh, struct SubdivCCG *subdiv_ccg)
MultiresFlags
Definition: BKE_multires.h:67
@ MULTIRES_USE_RENDER_PARAMS
Definition: BKE_multires.h:69
@ MULTIRES_ALLOC_PAINT_MASK
Definition: BKE_multires.h:70
@ MULTIRES_USE_LOCAL_MMD
Definition: BKE_multires.h:68
@ MULTIRES_IGNORE_SIMPLIFY
Definition: BKE_multires.h:71
@ MULTIRES_SUBDIVIDE_CATMULL_CLARK
Definition: BKE_multires.h:172
General operations, lookup, etc. for blender objects.
void BKE_object_scale_to_mat3(struct Object *ob, float r_mat[3][3])
Definition: object.c:3038
float paint_grid_paint_mask(const struct GridPaintMask *gpm, uint level, uint x, uint y)
A BVH for high poly meshes.
void BKE_pbvh_get_grid_updates(PBVH *pbvh, bool clear, void ***r_gridfaces, int *r_totface)
Definition: pbvh.c:1614
void BKE_pbvh_free(PBVH *pbvh)
Definition: pbvh.c:679
PBVHType BKE_pbvh_type(const PBVH *pbvh)
Definition: pbvh.c:1661
@ PBVH_GRIDS
Definition: BKE_pbvh.h:211
int get_render_subsurf_level(const struct RenderData *r, int lvl, bool for_render)
void BKE_subdiv_ccg_average_stitch_faces(SubdivCCG *subdiv_ccg, struct CCGFace **effected_faces, int num_effected_faces)
Definition: subdiv_ccg.c:1221
int BKE_ccg_gridsize(int level)
Definition: CCGSubSurf.c:37
MultiresModifiedFlags
Definition: BKE_subsurf.h:89
@ MULTIRES_HIDDEN_MODIFIED
Definition: BKE_subsurf.h:94
@ MULTIRES_COORDS_MODIFIED
Definition: BKE_subsurf.h:92
struct DerivedMesh * subsurf_make_derived_from_derived(struct DerivedMesh *dm, struct SubsurfModifierData *smd, const struct Scene *scene, float(*vertCos)[3], SubsurfFlags flags)
Definition: subsurf_ccg.c:2343
SubsurfFlags
Definition: BKE_subsurf.h:51
@ SUBSURF_IN_EDIT_MODE
Definition: BKE_subsurf.h:55
@ SUBSURF_ALLOC_PAINT_MASK
Definition: BKE_subsurf.h:56
@ SUBSURF_USE_RENDER_PARAMS
Definition: BKE_subsurf.h:52
@ SUBSURF_IGNORE_SIMPLIFY
Definition: BKE_subsurf.h:58
int BKE_ccg_factor(int low_level, int high_level)
Definition: CCGSubSurf.c:42
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define BLI_BITMAP_TEST(_bitmap, _index)
Definition: BLI_bitmap.h:63
#define BLI_BITMAP_NEW(_tot, _alloc_string)
Definition: BLI_bitmap.h:50
#define BLI_BITMAP_SET(_bitmap, _index, _set)
Definition: BLI_bitmap.h:93
unsigned int BLI_bitmap
Definition: BLI_bitmap.h:32
MINLINE int square_i(int a)
bool is_uniform_scaled_m3(const float mat[3][3])
Definition: math_matrix.c:1850
float mat3_to_scale(const float M[3][3])
Definition: math_matrix.c:2187
bool invert_m3(float R[3][3])
Definition: math_matrix.c:1152
void mul_v3_m3v3(float r[3], const float M[3][3], const float a[3])
Definition: math_matrix.c:901
void mul_m3_m3m3(float R[3][3], const float A[3][3], const float B[3][3])
Definition: math_matrix.c:391
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE float normalize_v3(float r[3])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f)
MINLINE void add_v3_v3(float r[3], const float a[3])
MINLINE void sub_v4_v4v4(float r[4], const float a[4], const float b[4])
void BLI_task_parallel_range(const int start, const int stop, void *userdata, TaskParallelRangeFunc func, const TaskParallelSettings *settings)
Definition: task_range.cc:110
BLI_INLINE void BLI_parallel_range_settings_defaults(TaskParallelSettings *settings)
Definition: BLI_task.h:231
#define CLAMPIS(a, b, c)
#define UNUSED(x)
#define MAX2(a, b)
#define MIN2(a, b)
CCGError ccgSubSurf_updateFromFaces(CCGSubSurf *ss, int lvl, CCGFace **effectedF, int numEffectedF)
Definition: CCGSubSurf.c:943
CCGError ccgSubSurf_stitchFaces(CCGSubSurf *ss, int lvl, CCGFace **effectedF, int numEffectedF)
Definition: CCGSubSurf.c:1040
CCGError ccgSubSurf_updateNormals(CCGSubSurf *ss, CCGFace **effectedF, int numEffectedF)
#define CCG_TASK_LIMIT
Definition: CCGSubSurf.h:70
CCGError ccgSubSurf_updateLevels(CCGSubSurf *ss, int lvl, CCGFace **effectedF, int numEffectedF)
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:51
@ DAG_EVAL_RENDER
Definition: DEG_depsgraph.h:62
eEvaluationMode DEG_get_mode(const Depsgraph *graph)
struct Object * DEG_get_evaluated_object(const struct Depsgraph *depsgraph, struct Object *object)
struct Scene * DEG_get_evaluated_scene(const struct Depsgraph *graph)
@ CD_GRID_PAINT_MASK
#define CD_MASK_MDISPS
@ ME_SIMPLE_SUBSURF
@ eMultiresModifierFlag_ControlEdges
@ eModifierMode_Render
@ eModifierMode_Realtime
@ eModifierType_Multires
@ eSubsurfModifierFlag_ControlEdges
@ SUBSURF_UV_SMOOTH_NONE
@ SUBSURF_UV_SMOOTH_PRESERVE_BOUNDARIES
@ OB_MODE_EDIT
@ OB_MODE_SCULPT
Object is a sort of wrapper for general info.
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble x2
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
void BM_data_layer_free(BMesh *bm, CustomData *data, int type)
Definition: bmesh_interp.c:930
ATTR_WARN_UNUSED_RESULT const BMVert * v
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition: btQuadWord.h:119
Scene scene
const Depsgraph * depsgraph
void *(* MEM_malloc_arrayN)(size_t len, size_t size, const char *str)
Definition: mallocn.c:48
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_dupallocN)(const void *vmemh)
Definition: mallocn.c:42
void *(* MEM_calloc_arrayN)(size_t len, size_t size, const char *str)
Definition: mallocn.c:46
static char faces[256]
static void multires_copy_grid(float(*gridA)[3], float(*gridB)[3], int sizeA, int sizeB)
Definition: multires.c:585
void multires_modifier_update_mdisps(struct DerivedMesh *dm, Scene *scene)
Definition: multires.c:1046
void multires_force_external_reload(Object *object)
Definition: multires.c:482
static void multires_output_hidden_to_ccgdm(CCGDerivedMesh *ccgdm, Mesh *me, int level)
Definition: multires.c:200
static const int multires_grid_tot[]
Definition: multires.c:66
static void multires_grid_paint_mask_downsample(GridPaintMask *gpm, int level)
Definition: multires.c:639
DerivedMesh * multires_make_derived_from_derived(DerivedMesh *dm, MultiresModifierData *mmd, Scene *scene, Object *ob, MultiresFlags flags)
Definition: multires.c:1232
static void multires_sync_levels(Scene *scene, Object *ob_src, Object *ob_dst)
Definition: multires.c:1377
void multiresModifier_sync_levels_ex(Object *ob_dst, MultiresModifierData *mmd_src, MultiresModifierData *mmd_dst)
Definition: multires.c:1360
MultiresModifierData * find_multires_modifier_before(Scene *scene, ModifierData *lastmd)
Definition: multires.c:318
static void multiresModifier_disp_run(DerivedMesh *dm, Mesh *me, DerivedMesh *dm2, DispOp op, CCGElem **oldGridData, int totlvl)
Definition: multires.c:963
static void grid_tangent_matrix(float mat[3][3], const CCGKey *key, int x, int y, CCGElem *grid)
Definition: multires.c:842
static void multires_del_higher(MultiresModifierData *mmd, Object *ob, int lvl)
Definition: multires.c:659
void multiresModifier_del_levels(MultiresModifierData *mmd, Scene *scene, Object *ob, int direction)
Definition: multires.c:722
static void multires_ccg_mark_as_modified(SubdivCCG *subdiv_ccg, MultiresModifiedFlags flags)
Definition: multires.c:396
static DerivedMesh * multires_dm_create_local(Scene *scene, Object *ob, DerivedMesh *dm, int lvl, int totlvl, bool alloc_paint_mask, int flags)
Definition: multires.c:745
static void grid_tangent(const CCGKey *key, int x, int y, int axis, CCGElem *grid, float t[3])
Definition: multires.c:809
static void multires_apply_smat(struct Depsgraph *UNUSED(depsgraph), Scene *scene, Object *object, const float smat[3][3])
Definition: multires.c:1408
static void multires_mdisps_subdivide_hidden(MDisps *md, int new_level)
Definition: multires.c:224
static DerivedMesh * subsurf_dm_create_local(Scene *scene, Object *ob, DerivedMesh *dm, int lvl, bool is_simple, bool is_optimal, bool is_plain_uv, bool alloc_paint_mask, bool for_render, SubsurfFlags flags)
Definition: multires.c:768
void multires_flush_sculpt_updates(Object *object)
Definition: multires.c:427
MultiresModifierData * get_multires_modifier(Scene *scene, Object *ob, bool use_first)
Definition: multires.c:336
void old_mdisps_bilinear(float out[3], float(*disps)[3], const int st, float u, float v)
Definition: multires.c:1305
static const int multires_side_tot[]
Definition: multires.c:68
DispOp
Definition: multires.c:72
@ APPLY_DISPLACEMENTS
Definition: multires.c:73
@ CALC_DISPLACEMENTS
Definition: multires.c:74
@ ADD_DISPLACEMENTS
Definition: multires.c:75
static int get_levels_from_disps(Object *ob)
Definition: multires.c:491
struct MultiresThreadedData MultiresThreadedData
void multires_stitch_grids(Object *ob)
Definition: multires.c:1205
void multiresModifier_ensure_external_read(struct Mesh *mesh, const MultiresModifierData *mmd)
Definition: multires.c:1546
Mesh * BKE_multires_create_mesh(struct Depsgraph *depsgraph, Object *object, MultiresModifierData *mmd)
Definition: multires.c:242
static void multires_apply_uniform_scale(Object *object, const float scale)
Definition: multires.c:1396
static BLI_bitmap * multires_mdisps_upsample_hidden(BLI_bitmap *lo_hidden, int lo_level, int hi_level, const BLI_bitmap *prev_hidden)
Definition: multires.c:109
int mdisp_rot_face_to_crn(struct MVert *UNUSED(mvert), struct MPoly *mpoly, struct MLoop *UNUSED(mloop), const struct MLoopTri *UNUSED(lt), const int face_side, const float u, const float v, float *x, float *y)
Definition: multires.c:1554
void multires_mark_as_modified(Depsgraph *depsgraph, Object *object, MultiresModifiedFlags flags)
Definition: multires.c:406
static BLI_bitmap * multires_mdisps_downsample_hidden(const BLI_bitmap *old_hidden, int old_level, int new_level)
Definition: multires.c:176
void multiresModifier_set_levels_from_disps(MultiresModifierData *mmd, Object *ob)
Definition: multires.c:529
void multires_ensure_external_read(struct Mesh *mesh, int top_level)
Definition: multires.c:1518
float(* BKE_multires_create_deformed_base_mesh_vert_coords(struct Depsgraph *depsgraph, struct Object *object, struct MultiresModifierData *mmd, int *r_num_deformed_verts))[3]
Definition: multires.c:265
int multires_mdisp_corners(MDisps *s)
Definition: multires.c:1436
void multires_topology_changed(Mesh *me)
Definition: multires.c:1479
void multiresModifier_scale_disp(struct Depsgraph *depsgraph, Scene *scene, Object *ob)
Definition: multires.c:1451
static void multires_set_tot_mdisps(Mesh *me, int lvl)
Definition: multires.c:549
void multires_force_sculpt_rebuild(Object *object)
Definition: multires.c:456
void multires_modifier_update_hidden(DerivedMesh *dm)
Definition: multires.c:1173
void multires_set_tot_level(Object *ob, MultiresModifierData *mmd, int lvl)
Definition: multires.c:384
static void multires_copy_dm_grid(CCGElem *gridA, CCGElem *gridB, CCGKey *keyA, CCGKey *keyB)
Definition: multires.c:609
static void multires_reallocate_mdisps(int totloop, MDisps *mdisps, int lvl)
Definition: multires.c:562
int multires_get_level(const Scene *scene, const Object *ob, const MultiresModifierData *mmd, bool render, bool ignore_simplify)
Definition: multires.c:364
void multires_customdata_delete(Mesh *me)
Definition: multires.c:83
void multiresModifier_prepare_join(struct Depsgraph *depsgraph, Scene *scene, Object *ob, Object *to_ob)
Definition: multires.c:1461
static void multires_disp_run_cb(void *__restrict userdata, const int pidx, const TaskParallelTLS *__restrict UNUSED(tls))
Definition: multires.c:866
bool isnan(double i)
Definition: numeric.h:451
struct BMesh * bm
Definition: BKE_editmesh.h:52
CustomData ldata
Definition: bmesh_class.h:337
struct CCGSubSurf * ss
Definition: BKE_subsurf.h:102
struct MultiresModifierData * mmd
Definition: BKE_subsurf.h:141
MultiresModifiedFlags modified_flags
Definition: BKE_subsurf.h:148
unsigned int ** gridHidden
Definition: BKE_subsurf.h:136
DerivedMesh dm
Definition: BKE_subsurf.h:100
struct Object * ob
Definition: BKE_subsurf.h:147
struct CCGDerivedMesh::@73 multires
Definition: BKE_ccg.h:48
int has_mask
Definition: BKE_ccg.h:71
int grid_size
Definition: BKE_ccg.h:56
int grid_area
Definition: BKE_ccg.h:58
int level
Definition: BKE_ccg.h:49
int elem_size
Definition: BKE_ccg.h:53
int(* getGridSize)(DerivedMesh *dm)
struct CCGElem **(* getGridData)(DerivedMesh *dm)
int(* getNumGrids)(DerivedMesh *dm)
CustomData polyData
void(* getGridKey)(DerivedMesh *dm, struct CCGKey *key)
CustomData loopData
void(* release)(DerivedMesh *dm)
int *(* getGridOffset)(DerivedMesh *dm)
unsigned int level
void * first
Definition: DNA_listBase.h:47
float(* disps)[3]
unsigned int * hidden
struct SubdivCCG * subdiv_ccg
struct BMEditMesh * edit_mesh
struct CustomData pdata ldata
Mesh_Runtime runtime
int totpoly
int totloop
struct MPoly * mpoly
struct ModifierData * next
struct ModifierData * prev
struct Depsgraph * depsgraph
Definition: BKE_modifier.h:153
ModifierApplyFlag flag
Definition: BKE_modifier.h:155
ModifierTypeType type
Definition: BKE_modifier.h:173
struct Mesh *(* modifyMesh)(struct ModifierData *md, const struct ModifierEvalContext *ctx, struct Mesh *mesh)
Definition: BKE_modifier.h:244
GridPaintMask * grid_paint_mask
Definition: multires.c:860
float(* smat)[3]
Definition: multires.c:863
CCGElem ** subGridData
Definition: multires.c:855
CCGElem ** gridData
Definition: multires.c:855
ListBase modifiers
struct SculptSession * sculpt
void * data
struct RenderData r
struct SubdivCCG * subdiv_ccg
Definition: BKE_paint.h:501
int * pmap_mem
Definition: BKE_paint.h:475
struct MeshElemMap * pmap
Definition: BKE_paint.h:474
struct MultiresModifierData * modifier
Definition: BKE_paint.h:453
struct SculptSession::@53 multires
struct PBVH * pbvh
Definition: BKE_paint.h:504
struct SubdivCCG::@68 dirty
ccl_device_inline float2 floor(const float2 &a)
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)
uint len