diff options
author | Campbell Barton <ideasman42@gmail.com> | 2017-10-09 12:49:27 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2017-10-09 12:49:27 +0300 |
commit | a5b4b0f21c1ae8c96e4fea9abdcfac2fab1cf300 (patch) | |
tree | 0658d8bdfb8ec03652aa04f82ee8a4d243ec6370 /source/blender/blenkernel/intern | |
parent | d68f698cf0321477c0734474150eb4bc43c4e85f (diff) | |
parent | abcda06934aba054de8540b66b13c2bbc5f8f515 (diff) |
Merge branch '28' into custom-manipulatorscustom-manipulators
Diffstat (limited to 'source/blender/blenkernel/intern')
99 files changed, 4643 insertions, 2871 deletions
diff --git a/source/blender/blenkernel/intern/DerivedMesh.c b/source/blender/blenkernel/intern/DerivedMesh.c index 55d5f6f89a1..18db368ee2b 100644 --- a/source/blender/blenkernel/intern/DerivedMesh.c +++ b/source/blender/blenkernel/intern/DerivedMesh.c @@ -96,6 +96,10 @@ static DerivedMesh *navmesh_dm_createNavMeshForVisualization(DerivedMesh *dm); # define ASSERT_IS_VALID_DM(dm) #endif + +static ThreadRWMutex loops_cache_lock = PTHREAD_RWLOCK_INITIALIZER; + + static void add_shapekey_layers(DerivedMesh *dm, Mesh *me, Object *ob); static void shapekey_layers_to_keyblocks(DerivedMesh *dm, Mesh *me, int actshape_uid); @@ -238,6 +242,30 @@ static int dm_getNumLoopTri(DerivedMesh *dm) return numlooptris; } +static const MLoopTri *dm_getLoopTriArray(DerivedMesh *dm) +{ + MLoopTri *looptri; + + BLI_rw_mutex_lock(&loops_cache_lock, THREAD_LOCK_READ); + looptri = dm->looptris.array; + BLI_rw_mutex_unlock(&loops_cache_lock); + + if (looptri != NULL) { + BLI_assert(dm->getNumLoopTri(dm) == dm->looptris.num); + } + else { + BLI_rw_mutex_lock(&loops_cache_lock, THREAD_LOCK_WRITE); + /* We need to ensure array is still NULL inside mutex-protected code, some other thread might have already + * recomputed those looptris. */ + if (dm->looptris.array == NULL) { + dm->recalcLoopTri(dm); + } + looptri = dm->looptris.array; + BLI_rw_mutex_unlock(&loops_cache_lock); + } + return looptri; +} + static CustomData *dm_getVertCData(DerivedMesh *dm) { return &dm->vertData; @@ -281,6 +309,8 @@ void DM_init_funcs(DerivedMesh *dm) dm->dupLoopArray = dm_dupLoopArray; dm->dupPolyArray = dm_dupPolyArray; + dm->getLoopTriArray = dm_getLoopTriArray; + /* subtypes handle getting actual data */ dm->getNumLoopTri = dm_getNumLoopTri; @@ -475,6 +505,8 @@ void DM_ensure_tessface(DerivedMesh *dm) /** * Ensure the array is large enough + * + * /note This function must always be thread-protected by caller. It should only be used by internal code. */ void DM_ensure_looptri_data(DerivedMesh *dm) { @@ -482,18 +514,22 @@ void DM_ensure_looptri_data(DerivedMesh *dm) const unsigned int totloop = dm->numLoopData; const int looptris_num = poly_to_tri_count(totpoly, totloop); + BLI_assert(dm->looptris.array_wip == NULL); + + SWAP(MLoopTri *, dm->looptris.array, dm->looptris.array_wip); + if ((looptris_num > dm->looptris.num_alloc) || (looptris_num < dm->looptris.num_alloc * 2) || (totpoly == 0)) { - MEM_SAFE_FREE(dm->looptris.array); + MEM_SAFE_FREE(dm->looptris.array_wip); dm->looptris.num_alloc = 0; dm->looptris.num = 0; } if (totpoly) { - if (dm->looptris.array == NULL) { - dm->looptris.array = MEM_mallocN(sizeof(*dm->looptris.array) * looptris_num, __func__); + if (dm->looptris.array_wip == NULL) { + dm->looptris.array_wip = MEM_mallocN(sizeof(*dm->looptris.array_wip) * looptris_num, __func__); dm->looptris.num_alloc = looptris_num; } @@ -501,19 +537,6 @@ void DM_ensure_looptri_data(DerivedMesh *dm) } } -/** - * The purpose of this function is that we can call: - * `dm->getLoopTriArray(dm)` and get the array returned. - */ -void DM_ensure_looptri(DerivedMesh *dm) -{ - const int numPolys = dm->getNumPolys(dm); - - if ((dm->looptris.num == 0) && (numPolys != 0)) { - dm->recalcLoopTri(dm); - } -} - void DM_verttri_from_looptri(MVertTri *verttri, const MLoop *mloop, const MLoopTri *looptri, int looptri_num) { int i; @@ -1126,7 +1149,7 @@ DerivedMesh *mesh_create_derived(Mesh *me, float (*vertCos)[3]) } DerivedMesh *mesh_create_derived_for_modifier( - Scene *scene, Object *ob, + const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, ModifierData *md, int build_shapekey_layers) { Mesh *me = ob->data; @@ -1152,7 +1175,7 @@ DerivedMesh *mesh_create_derived_for_modifier( int numVerts; float (*deformedVerts)[3] = BKE_mesh_vertexCos_get(me, &numVerts); - modwrap_deformVerts(md, ob, NULL, deformedVerts, numVerts, 0); + modwrap_deformVerts(md, eval_ctx, ob, NULL, deformedVerts, numVerts, 0); dm = mesh_create_derived(me, deformedVerts); if (build_shapekey_layers) @@ -1166,7 +1189,7 @@ DerivedMesh *mesh_create_derived_for_modifier( if (build_shapekey_layers) add_shapekey_layers(tdm, me, ob); - dm = modwrap_applyModifier(md, ob, tdm, 0); + dm = modwrap_applyModifier(md, eval_ctx, ob, tdm, 0); ASSERT_IS_VALID_DM(dm); if (tdm != dm) tdm->release(tdm); @@ -1732,7 +1755,7 @@ static void dm_ensure_display_normals(DerivedMesh *dm) * - apply deform modifiers and input vertexco */ static void mesh_calc_modifiers( - Scene *scene, Object *ob, float (*inputVertexCos)[3], + const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, float (*inputVertexCos)[3], const bool useRenderParams, int useDeform, const bool need_mapping, CustomDataMask dataMask, const int index, const bool useCache, const bool build_shapekey_layers, @@ -1843,7 +1866,7 @@ static void mesh_calc_modifiers( if (!deformedVerts) deformedVerts = BKE_mesh_vertexCos_get(me, &numVerts); - modwrap_deformVerts(md, ob, NULL, deformedVerts, numVerts, deform_app_flags); + modwrap_deformVerts(md, eval_ctx, ob, NULL, deformedVerts, numVerts, deform_app_flags); } else { break; @@ -1984,7 +2007,7 @@ static void mesh_calc_modifiers( } } - modwrap_deformVerts(md, ob, dm, deformedVerts, numVerts, deform_app_flags); + modwrap_deformVerts(md, eval_ctx, ob, dm, deformedVerts, numVerts, deform_app_flags); } else { DerivedMesh *ndm; @@ -2059,7 +2082,7 @@ static void mesh_calc_modifiers( } } - ndm = modwrap_applyModifier(md, ob, dm, app_flags); + ndm = modwrap_applyModifier(md, eval_ctx, ob, dm, app_flags); ASSERT_IS_VALID_DM(ndm); if (ndm) { @@ -2086,7 +2109,7 @@ static void mesh_calc_modifiers( (mti->requiredDataMask ? mti->requiredDataMask(ob, md) : 0)); - ndm = modwrap_applyModifier(md, ob, orcodm, (app_flags & ~MOD_APPLY_USECACHE) | MOD_APPLY_ORCO); + ndm = modwrap_applyModifier(md, eval_ctx, ob, orcodm, (app_flags & ~MOD_APPLY_USECACHE) | MOD_APPLY_ORCO); ASSERT_IS_VALID_DM(ndm); if (ndm) { @@ -2104,7 +2127,7 @@ static void mesh_calc_modifiers( nextmask &= ~CD_MASK_CLOTH_ORCO; DM_set_only_copy(clothorcodm, nextmask | CD_MASK_ORIGINDEX); - ndm = modwrap_applyModifier(md, ob, clothorcodm, (app_flags & ~MOD_APPLY_USECACHE) | MOD_APPLY_ORCO); + ndm = modwrap_applyModifier(md, eval_ctx, ob, clothorcodm, (app_flags & ~MOD_APPLY_USECACHE) | MOD_APPLY_ORCO); ASSERT_IS_VALID_DM(ndm); if (ndm) { @@ -2203,7 +2226,6 @@ static void mesh_calc_modifiers( if (dataMask & CD_MASK_MFACE) { DM_ensure_tessface(finaldm); } - DM_ensure_looptri(finaldm); /* without this, drawing ngon tri's faces will show ugly tessellated face * normals and will also have to calculate normals on the fly, try avoid @@ -2288,8 +2310,8 @@ bool editbmesh_modifier_is_enabled(Scene *scene, ModifierData *md, DerivedMesh * } static void editbmesh_calc_modifiers( - Scene *scene, Object *ob, BMEditMesh *em, - CustomDataMask dataMask, + const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, + BMEditMesh *em, CustomDataMask dataMask, /* return args */ DerivedMesh **r_cage, DerivedMesh **r_final) { @@ -2376,9 +2398,9 @@ static void editbmesh_calc_modifiers( } if (mti->deformVertsEM) - modwrap_deformVertsEM(md, ob, em, dm, deformedVerts, numVerts); + modwrap_deformVertsEM(md, eval_ctx, ob, em, dm, deformedVerts, numVerts); else - modwrap_deformVerts(md, ob, dm, deformedVerts, numVerts, 0); + modwrap_deformVerts(md, eval_ctx, ob, dm, deformedVerts, numVerts, 0); } else { DerivedMesh *ndm; @@ -2423,10 +2445,10 @@ static void editbmesh_calc_modifiers( DM_set_only_copy(orcodm, mask | CD_MASK_ORIGINDEX); if (mti->applyModifierEM) { - ndm = modwrap_applyModifierEM(md, ob, em, orcodm, MOD_APPLY_ORCO); + ndm = modwrap_applyModifierEM(md, eval_ctx, ob, em, orcodm, MOD_APPLY_ORCO); } else { - ndm = modwrap_applyModifier(md, ob, orcodm, MOD_APPLY_ORCO); + ndm = modwrap_applyModifier(md, eval_ctx, ob, orcodm, MOD_APPLY_ORCO); } ASSERT_IS_VALID_DM(ndm); @@ -2451,9 +2473,9 @@ static void editbmesh_calc_modifiers( } if (mti->applyModifierEM) - ndm = modwrap_applyModifierEM(md, ob, em, dm, MOD_APPLY_USECACHE | MOD_APPLY_ALLOW_GPU); + ndm = modwrap_applyModifierEM(md, eval_ctx, ob, em, dm, MOD_APPLY_USECACHE | MOD_APPLY_ALLOW_GPU); else - ndm = modwrap_applyModifier(md, ob, dm, MOD_APPLY_USECACHE | MOD_APPLY_ALLOW_GPU); + ndm = modwrap_applyModifier(md, eval_ctx, ob, dm, MOD_APPLY_USECACHE | MOD_APPLY_ALLOW_GPU); ASSERT_IS_VALID_DM(ndm); if (ndm) { @@ -2605,7 +2627,8 @@ static bool calc_modifiers_skip_orco(Scene *scene, else if ((ob->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT | OB_MODE_TEXTURE_PAINT)) != 0) { return false; } - else if ((DEG_get_eval_flags_for_id(scene->depsgraph, &ob->id) & DAG_EVAL_NEED_CPU) != 0) { + /* TODO(sergey): How do we get depsgraph here? */ + else if ((DEG_get_eval_flags_for_id(scene->depsgraph_legacy, &ob->id) & DAG_EVAL_NEED_CPU) != 0) { return false; } SubsurfModifierData *smd = (SubsurfModifierData *)last_md; @@ -2617,7 +2640,7 @@ static bool calc_modifiers_skip_orco(Scene *scene, #endif static void mesh_build_data( - Scene *scene, Object *ob, CustomDataMask dataMask, + const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, CustomDataMask dataMask, const bool build_shapekey_layers, const bool need_mapping) { BLI_assert(ob->type == OB_MESH); @@ -2632,7 +2655,7 @@ static void mesh_build_data( #endif mesh_calc_modifiers( - scene, ob, NULL, false, 1, need_mapping, dataMask, -1, true, build_shapekey_layers, + eval_ctx, scene, ob, NULL, false, 1, need_mapping, dataMask, -1, true, build_shapekey_layers, true, &ob->derivedDeform, &ob->derivedFinal); @@ -2643,17 +2666,19 @@ static void mesh_build_data( ob->lastDataMask = dataMask; ob->lastNeedMapping = need_mapping; - if ((ob->mode & OB_MODE_SCULPT) && ob->sculpt) { + if ((ob->mode & OB_MODE_ALL_SCULPT) && ob->sculpt) { /* create PBVH immediately (would be created on the fly too, * but this avoids waiting on first stroke) */ - BKE_sculpt_update_mesh_elements(scene, scene->toolsettings->sculpt, ob, false, false); + BKE_sculpt_update_mesh_elements(eval_ctx, scene, scene->toolsettings->sculpt, ob, false, false); } BLI_assert(!(ob->derivedFinal->dirty & DM_DIRTY_NORMALS)); } -static void editbmesh_build_data(Scene *scene, Object *obedit, BMEditMesh *em, CustomDataMask dataMask) +static void editbmesh_build_data( + const struct EvaluationContext *eval_ctx, Scene *scene, + Object *obedit, BMEditMesh *em, CustomDataMask dataMask) { BKE_object_free_derived_caches(obedit); BKE_object_sculpt_modifiers_changed(obedit); @@ -2667,7 +2692,7 @@ static void editbmesh_build_data(Scene *scene, Object *obedit, BMEditMesh *em, C #endif editbmesh_calc_modifiers( - scene, obedit, em, dataMask, + eval_ctx, scene, obedit, em, dataMask, &em->derivedCage, &em->derivedFinal); DM_set_object_boundbox(obedit, em->derivedFinal); @@ -2682,7 +2707,7 @@ static void editbmesh_build_data(Scene *scene, Object *obedit, BMEditMesh *em, C static CustomDataMask object_get_datamask(const Scene *scene, Object *ob, bool *r_need_mapping) { /* TODO(sergey): Avoid this linear list lookup. */ - SceneLayer *sl = BKE_scene_layer_context_active(scene); + SceneLayer *sl = BKE_scene_layer_context_active_PLACEHOLDER(scene); Object *actob = sl->basact ? sl->basact->object : NULL; CustomDataMask mask = ob->customdata_mask; @@ -2720,23 +2745,24 @@ static CustomDataMask object_get_datamask(const Scene *scene, Object *ob, bool * } void makeDerivedMesh( - Scene *scene, Object *ob, BMEditMesh *em, + const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, BMEditMesh *em, CustomDataMask dataMask, const bool build_shapekey_layers) { bool need_mapping; dataMask |= object_get_datamask(scene, ob, &need_mapping); if (em) { - editbmesh_build_data(scene, ob, em, dataMask); + editbmesh_build_data(eval_ctx, scene, ob, em, dataMask); } else { - mesh_build_data(scene, ob, dataMask, build_shapekey_layers, need_mapping); + mesh_build_data(eval_ctx, scene, ob, dataMask, build_shapekey_layers, need_mapping); } } /***/ -DerivedMesh *mesh_get_derived_final(Scene *scene, Object *ob, CustomDataMask dataMask) +DerivedMesh *mesh_get_derived_final( + const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, CustomDataMask dataMask) { /* if there's no derived mesh or the last data mask used doesn't include * the data we need, rebuild the derived mesh @@ -2748,14 +2774,14 @@ DerivedMesh *mesh_get_derived_final(Scene *scene, Object *ob, CustomDataMask dat ((dataMask & ob->lastDataMask) != dataMask) || (need_mapping != ob->lastNeedMapping)) { - mesh_build_data(scene, ob, dataMask, false, need_mapping); + mesh_build_data(eval_ctx, scene, ob, dataMask, false, need_mapping); } if (ob->derivedFinal) { BLI_assert(!(ob->derivedFinal->dirty & DM_DIRTY_NORMALS)); } return ob->derivedFinal; } -DerivedMesh *mesh_get_derived_deform(Scene *scene, Object *ob, CustomDataMask dataMask) +DerivedMesh *mesh_get_derived_deform(const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, CustomDataMask dataMask) { /* if there's no derived mesh or the last data mask used doesn't include * the data we need, rebuild the derived mesh @@ -2768,37 +2794,37 @@ DerivedMesh *mesh_get_derived_deform(Scene *scene, Object *ob, CustomDataMask da ((dataMask & ob->lastDataMask) != dataMask) || (need_mapping != ob->lastNeedMapping)) { - mesh_build_data(scene, ob, dataMask, false, need_mapping); + mesh_build_data(eval_ctx, scene, ob, dataMask, false, need_mapping); } return ob->derivedDeform; } -DerivedMesh *mesh_create_derived_render(Scene *scene, Object *ob, CustomDataMask dataMask) +DerivedMesh *mesh_create_derived_render(const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, CustomDataMask dataMask) { DerivedMesh *final; mesh_calc_modifiers( - scene, ob, NULL, true, 1, false, dataMask, -1, false, false, false, + eval_ctx, scene, ob, NULL, true, 1, false, dataMask, -1, false, false, false, NULL, &final); return final; } -DerivedMesh *mesh_create_derived_index_render(Scene *scene, Object *ob, CustomDataMask dataMask, int index) +DerivedMesh *mesh_create_derived_index_render(const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, CustomDataMask dataMask, int index) { DerivedMesh *final; mesh_calc_modifiers( - scene, ob, NULL, true, 1, false, dataMask, index, false, false, false, + eval_ctx, scene, ob, NULL, true, 1, false, dataMask, index, false, false, false, NULL, &final); return final; } DerivedMesh *mesh_create_derived_view( - Scene *scene, Object *ob, - CustomDataMask dataMask) + const struct EvaluationContext *eval_ctx, Scene *scene, + Object *ob, CustomDataMask dataMask) { DerivedMesh *final; @@ -2809,7 +2835,7 @@ DerivedMesh *mesh_create_derived_view( ob->transflag |= OB_NO_PSYS_UPDATE; mesh_calc_modifiers( - scene, ob, NULL, false, 1, false, dataMask, -1, false, false, false, + eval_ctx, scene, ob, NULL, false, 1, false, dataMask, -1, false, false, false, NULL, &final); ob->transflag &= ~OB_NO_PSYS_UPDATE; @@ -2818,53 +2844,53 @@ DerivedMesh *mesh_create_derived_view( } DerivedMesh *mesh_create_derived_no_deform( - Scene *scene, Object *ob, float (*vertCos)[3], - CustomDataMask dataMask) + const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, + float (*vertCos)[3], CustomDataMask dataMask) { DerivedMesh *final; mesh_calc_modifiers( - scene, ob, vertCos, false, 0, false, dataMask, -1, false, false, false, + eval_ctx, scene, ob, vertCos, false, 0, false, dataMask, -1, false, false, false, NULL, &final); return final; } DerivedMesh *mesh_create_derived_no_virtual( - Scene *scene, Object *ob, float (*vertCos)[3], - CustomDataMask dataMask) + const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, + float (*vertCos)[3], CustomDataMask dataMask) { DerivedMesh *final; mesh_calc_modifiers( - scene, ob, vertCos, false, -1, false, dataMask, -1, false, false, false, + eval_ctx, scene, ob, vertCos, false, -1, false, dataMask, -1, false, false, false, NULL, &final); return final; } DerivedMesh *mesh_create_derived_physics( - Scene *scene, Object *ob, float (*vertCos)[3], - CustomDataMask dataMask) + const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, + float (*vertCos)[3], CustomDataMask dataMask) { DerivedMesh *final; mesh_calc_modifiers( - scene, ob, vertCos, false, -1, true, dataMask, -1, false, false, false, + eval_ctx, scene, ob, vertCos, false, -1, true, dataMask, -1, false, false, false, NULL, &final); return final; } DerivedMesh *mesh_create_derived_no_deform_render( - Scene *scene, Object *ob, - float (*vertCos)[3], + const struct EvaluationContext *eval_ctx, Scene *scene, + Object *ob, float (*vertCos)[3], CustomDataMask dataMask) { DerivedMesh *final; mesh_calc_modifiers( - scene, ob, vertCos, true, 0, false, dataMask, -1, false, false, false, + eval_ctx, scene, ob, vertCos, true, 0, false, dataMask, -1, false, false, false, NULL, &final); return final; @@ -2873,7 +2899,7 @@ DerivedMesh *mesh_create_derived_no_deform_render( /***/ DerivedMesh *editbmesh_get_derived_cage_and_final( - Scene *scene, Object *obedit, BMEditMesh *em, + const struct EvaluationContext *eval_ctx, Scene *scene, Object *obedit, BMEditMesh *em, CustomDataMask dataMask, /* return args */ DerivedMesh **r_final) @@ -2886,7 +2912,7 @@ DerivedMesh *editbmesh_get_derived_cage_and_final( if (!em->derivedCage || (em->lastDataMask & dataMask) != dataMask) { - editbmesh_build_data(scene, obedit, em, dataMask); + editbmesh_build_data(eval_ctx, scene, obedit, em, dataMask); } *r_final = em->derivedFinal; @@ -2894,7 +2920,9 @@ DerivedMesh *editbmesh_get_derived_cage_and_final( return em->derivedCage; } -DerivedMesh *editbmesh_get_derived_cage(Scene *scene, Object *obedit, BMEditMesh *em, CustomDataMask dataMask) +DerivedMesh *editbmesh_get_derived_cage( + const struct EvaluationContext *eval_ctx, Scene *scene, Object *obedit, BMEditMesh *em, + CustomDataMask dataMask) { /* if there's no derived mesh or the last data mask used doesn't include * the data we need, rebuild the derived mesh @@ -2904,7 +2932,7 @@ DerivedMesh *editbmesh_get_derived_cage(Scene *scene, Object *obedit, BMEditMesh if (!em->derivedCage || (em->lastDataMask & dataMask) != dataMask) { - editbmesh_build_data(scene, obedit, em, dataMask); + editbmesh_build_data(eval_ctx, scene, obedit, em, dataMask); } return em->derivedCage; @@ -3054,14 +3082,23 @@ void DM_calc_tangents_names_from_gpu( *r_tangent_names_count = count; } +void DM_add_named_tangent_layer_for_uv( + CustomData *uv_data, CustomData *tan_data, int numLoopData, + const char *layer_name) +{ + if (CustomData_get_named_layer_index(tan_data, CD_TANGENT, layer_name) == -1 && + CustomData_get_named_layer_index(uv_data, CD_MLOOPUV, layer_name) != -1) + { + CustomData_add_layer_named( + tan_data, CD_TANGENT, CD_CALLOC, NULL, + numLoopData, layer_name); + } +} + void DM_calc_loop_tangents( DerivedMesh *dm, bool calc_active_tangent, const char (*tangent_names)[MAX_NAME], int tangent_names_len) { - if (CustomData_number_of_layers(&dm->loopData, CD_MLOOPUV) == 0) { - return; - } - BKE_mesh_calc_loop_tangent_ex( dm->getVertArray(dm), dm->getPolyArray(dm), dm->getNumPolys(dm), @@ -3941,35 +3978,3 @@ MFace *DM_get_tessface_array(DerivedMesh *dm, bool *r_allocated) return mface; } - -const MLoopTri *DM_get_looptri_array( - DerivedMesh *dm, - const MVert *mvert, - const MPoly *mpoly, int mpoly_len, - const MLoop *mloop, int mloop_len, - bool *r_allocated) -{ - const MLoopTri *looptri = dm->getLoopTriArray(dm); - *r_allocated = false; - - if (looptri == NULL) { - if (mpoly_len > 0) { - const int looptris_num = poly_to_tri_count(mpoly_len, mloop_len); - MLoopTri *looptri_data; - - looptri_data = MEM_mallocN(sizeof(MLoopTri) * looptris_num, __func__); - - BKE_mesh_recalc_looptri( - mloop, mpoly, - mvert, - mloop_len, mpoly_len, - looptri_data); - - looptri = looptri_data; - - *r_allocated = true; - } - } - - return looptri; -} diff --git a/source/blender/blenkernel/intern/action.c b/source/blender/blenkernel/intern/action.c index 8a94d4b8666..6f7b3286e40 100644 --- a/source/blender/blenkernel/intern/action.c +++ b/source/blender/blenkernel/intern/action.c @@ -89,7 +89,7 @@ bAction *add_empty_action(Main *bmain, const char name[]) { bAction *act; - act = BKE_libblock_alloc(bmain, ID_AC, name); + act = BKE_libblock_alloc(bmain, ID_AC, name, 0); return act; } @@ -121,46 +121,56 @@ void BKE_action_free(bAction *act) /* .................................. */ -bAction *BKE_action_copy(Main *bmain, const bAction *src) +/** + * Only copy internal data of Action ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_action_copy_data(Main *UNUSED(bmain), bAction *act_dst, const bAction *act_src, const int UNUSED(flag)) { - bAction *dst = NULL; - bActionGroup *dgrp, *sgrp; - FCurve *dfcu, *sfcu; - - if (src == NULL) - return NULL; - dst = BKE_libblock_copy(bmain, &src->id); - + bActionGroup *grp_dst, *grp_src; + FCurve *fcu_dst, *fcu_src; + /* duplicate the lists of groups and markers */ - BLI_duplicatelist(&dst->groups, &src->groups); - BLI_duplicatelist(&dst->markers, &src->markers); - + BLI_duplicatelist(&act_dst->groups, &act_src->groups); + BLI_duplicatelist(&act_dst->markers, &act_src->markers); + /* copy F-Curves, fixing up the links as we go */ - BLI_listbase_clear(&dst->curves); - - for (sfcu = src->curves.first; sfcu; sfcu = sfcu->next) { + BLI_listbase_clear(&act_dst->curves); + + for (fcu_src = act_src->curves.first; fcu_src; fcu_src = fcu_src->next) { /* duplicate F-Curve */ - dfcu = copy_fcurve(sfcu); - BLI_addtail(&dst->curves, dfcu); - + fcu_dst = copy_fcurve(fcu_src); /* XXX TODO pass subdata flag? But surprisingly does not seem to be doing any ID refcounting... */ + BLI_addtail(&act_dst->curves, fcu_dst); + /* fix group links (kindof bad list-in-list search, but this is the most reliable way) */ - for (dgrp = dst->groups.first, sgrp = src->groups.first; dgrp && sgrp; dgrp = dgrp->next, sgrp = sgrp->next) { - if (sfcu->grp == sgrp) { - dfcu->grp = dgrp; - - if (dgrp->channels.first == sfcu) - dgrp->channels.first = dfcu; - if (dgrp->channels.last == sfcu) - dgrp->channels.last = dfcu; - + for (grp_dst = act_dst->groups.first, grp_src = act_src->groups.first; + grp_dst && grp_src; + grp_dst = grp_dst->next, grp_src = grp_src->next) + { + if (fcu_src->grp == grp_src) { + fcu_dst->grp = grp_dst; + + if (grp_dst->channels.first == fcu_src) { + grp_dst->channels.first = fcu_dst; + } + if (grp_dst->channels.last == fcu_src) { + grp_dst->channels.last = fcu_dst; + } break; } } } - - BKE_id_copy_ensure_local(bmain, &src->id, &dst->id); +} - return dst; +bAction *BKE_action_copy(Main *bmain, const bAction *act_src) +{ + bAction *act_copy; + BKE_id_copy_ex(bmain, &act_src->id, (ID **)&act_copy, 0, false); + return act_copy; } /* *************** Action Groups *************** */ @@ -524,7 +534,7 @@ const char *BKE_pose_ikparam_get_name(bPose *pose) * * \param dst Should be freed already, makes entire duplicate. */ -void BKE_pose_copy_data(bPose **dst, const bPose *src, const bool copy_constraints) +void BKE_pose_copy_data_ex(bPose **dst, const bPose *src, const int flag, const bool copy_constraints) { bPose *outPose; bPoseChannel *pchan; @@ -554,9 +564,8 @@ void BKE_pose_copy_data(bPose **dst, const bPose *src, const bool copy_constrain outPose->avs = src->avs; for (pchan = outPose->chanbase.first; pchan; pchan = pchan->next) { - - if (pchan->custom) { - id_us_plus(&pchan->custom->id); + if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) { + id_us_plus((ID *)pchan->custom); } /* warning, O(n2) here, if done without the hash, but these are rarely used features. */ @@ -571,13 +580,13 @@ void BKE_pose_copy_data(bPose **dst, const bPose *src, const bool copy_constrain } if (copy_constraints) { - BKE_constraints_copy(&listb, &pchan->constraints, true); // BKE_constraints_copy NULLs listb + BKE_constraints_copy_ex(&listb, &pchan->constraints, flag, true); // BKE_constraints_copy NULLs listb pchan->constraints = listb; pchan->mpath = NULL; /* motion paths should not get copied yet... */ } if (pchan->prop) { - pchan->prop = IDP_CopyProperty(pchan->prop); + pchan->prop = IDP_CopyProperty_ex(pchan->prop, flag); } pchan->draw_data = NULL; /* Drawing cache, no need to copy. */ @@ -591,6 +600,11 @@ void BKE_pose_copy_data(bPose **dst, const bPose *src, const bool copy_constrain *dst = outPose; } +void BKE_pose_copy_data(bPose **dst, const bPose *src, const bool copy_constraints) +{ + BKE_pose_copy_data_ex(dst, src, 0, copy_constraints); +} + void BKE_pose_itasc_init(bItasc *itasc) { if (itasc) { @@ -1121,9 +1135,13 @@ void calc_action_range(const bAction *act, float *start, float *end, short incl_ if (fcu->totvert) { float nmin, nmax; - /* get extents for this curve */ - /* TODO: allow enabling/disabling this? */ - calc_fcurve_range(fcu, &nmin, &nmax, false, true); + /* get extents for this curve + * - no "selected only", since this is often used in the backend + * - no "minimum length" (we will apply this later), otherwise + * single-keyframe curves will increase the overall length by + * a phantom frame (T50354) + */ + calc_fcurve_range(fcu, &nmin, &nmax, false, false); /* compare to the running tally */ min = min_ff(min, nmin); @@ -1176,7 +1194,9 @@ void calc_action_range(const bAction *act, float *start, float *end, short incl_ } if (foundvert || foundmod) { + /* ensure that action is at least 1 frame long (for NLA strips to have a valid length) */ if (min == max) max += 1.0f; + *start = min; *end = max; } diff --git a/source/blender/blenkernel/intern/anim.c b/source/blender/blenkernel/intern/anim.c index 59484724aee..34ab8a064d4 100644 --- a/source/blender/blenkernel/intern/anim.c +++ b/source/blender/blenkernel/intern/anim.c @@ -43,6 +43,7 @@ #include "DNA_key_types.h" #include "DNA_scene_types.h" +#include "BKE_context.h" #include "BKE_curve.h" #include "BKE_global.h" #include "BKE_key.h" @@ -282,10 +283,11 @@ void animviz_get_object_motionpaths(Object *ob, ListBase *targets) */ /* tweak the object ordering to trick depsgraph into making MotionPath calculations run faster */ -static void motionpaths_calc_optimise_depsgraph(Scene *scene, ListBase *targets) +static void motionpaths_calc_optimise_depsgraph(bContext *C, Scene *scene, ListBase *targets) { BaseLegacy *base, *baseNext; MPathTarget *mpt; + Main *bmain = CTX_data_main(C); /* make sure our temp-tag isn't already in use */ for (base = scene->base.first; base; base = base->next) @@ -309,7 +311,7 @@ static void motionpaths_calc_optimise_depsgraph(Scene *scene, ListBase *targets) } /* "brew me a list that's sorted a bit faster now depsy" */ - DEG_scene_relations_rebuild(G.main, scene); + DEG_scene_relations_rebuild(bmain, scene); } /* update scene for current frame */ @@ -373,7 +375,7 @@ static void motionpaths_calc_bake_targets(Scene *scene, ListBase *targets) * - recalc: whether we need to */ /* TODO: include reports pointer? */ -void animviz_calc_motionpaths(Scene *scene, ListBase *targets) +void animviz_calc_motionpaths(bContext *C, Scene *scene, ListBase *targets) { MPathTarget *mpt; int sfra, efra; @@ -399,7 +401,7 @@ void animviz_calc_motionpaths(Scene *scene, ListBase *targets) /* optimize the depsgraph for faster updates */ /* TODO: whether this is used should depend on some setting for the level of optimizations used */ - motionpaths_calc_optimise_depsgraph(scene, targets); + motionpaths_calc_optimise_depsgraph(C, scene, targets); /* calculate path over requested range */ for (CFRA = sfra; CFRA <= efra; CFRA++) { diff --git a/source/blender/blenkernel/intern/anim_sys.c b/source/blender/blenkernel/intern/anim_sys.c index 6475e5c8acd..5b7947df9dd 100644 --- a/source/blender/blenkernel/intern/anim_sys.c +++ b/source/blender/blenkernel/intern/anim_sys.c @@ -261,7 +261,7 @@ void BKE_animdata_free(ID *id, const bool do_id_user) /* Copying -------------------------------------------- */ /* Make a copy of the given AnimData - to be used when copying datablocks */ -AnimData *BKE_animdata_copy(AnimData *adt, const bool do_action) +AnimData *BKE_animdata_copy(Main *bmain, AnimData *adt, const bool do_action) { AnimData *dadt; @@ -272,8 +272,9 @@ AnimData *BKE_animdata_copy(AnimData *adt, const bool do_action) /* make a copy of action - at worst, user has to delete copies... */ if (do_action) { - dadt->action = BKE_action_copy(G.main, adt->action); - dadt->tmpact = BKE_action_copy(G.main, adt->tmpact); + BLI_assert(bmain != NULL); + BKE_id_copy_ex(bmain, (ID *)dadt->action, (ID **)&dadt->action, 0, false); + BKE_id_copy_ex(bmain, (ID *)dadt->tmpact, (ID **)&dadt->tmpact, 0, false); } else { id_us_plus((ID *)dadt->action); @@ -293,7 +294,7 @@ AnimData *BKE_animdata_copy(AnimData *adt, const bool do_action) return dadt; } -bool BKE_animdata_copy_id(ID *id_to, ID *id_from, const bool do_action) +bool BKE_animdata_copy_id(Main *bmain, ID *id_to, ID *id_from, const bool do_action) { AnimData *adt; @@ -305,7 +306,7 @@ bool BKE_animdata_copy_id(ID *id_to, ID *id_from, const bool do_action) adt = BKE_animdata_from_id(id_from); if (adt) { IdAdtTemplate *iat = (IdAdtTemplate *)id_to; - iat->adt = BKE_animdata_copy(adt, do_action); + iat->adt = BKE_animdata_copy(bmain, adt, do_action); } return true; @@ -625,6 +626,8 @@ char *BKE_animdata_driver_path_hack(bContext *C, PointerRNA *ptr, PropertyRNA *p } break; } + default: + break; } /* fix RNA pointer, as we've now changed the ID root by changing the paths */ @@ -1349,7 +1352,7 @@ void BKE_keyingset_free_path(KeyingSet *ks, KS_Path *ksp) } /* Copy all KeyingSets in the given list */ -void BKE_keyingsets_copy(ListBase *newlist, ListBase *list) +void BKE_keyingsets_copy(ListBase *newlist, const ListBase *list) { KeyingSet *ksn; KS_Path *kspn; @@ -1521,7 +1524,8 @@ static bool animsys_write_rna_setting(PathResolvedRNA *anim_rna, const float val } case PROP_INT: { - const int value_coerce = (int)value; + int value_coerce = (int)value; + RNA_property_int_clamp(ptr, prop, &value_coerce); if (array_index != -1) { if (RNA_property_int_get_index(ptr, prop, array_index) != value_coerce) { RNA_property_int_set_index(ptr, prop, array_index, value_coerce); @@ -1538,15 +1542,17 @@ static bool animsys_write_rna_setting(PathResolvedRNA *anim_rna, const float val } case PROP_FLOAT: { + float value_coerce = value; + RNA_property_float_clamp(ptr, prop, &value_coerce); if (array_index != -1) { - if (RNA_property_float_get_index(ptr, prop, array_index) != value) { - RNA_property_float_set_index(ptr, prop, array_index, value); + if (RNA_property_float_get_index(ptr, prop, array_index) != value_coerce) { + RNA_property_float_set_index(ptr, prop, array_index, value_coerce); written = true; } } else { - if (RNA_property_float_get(ptr, prop) != value) { - RNA_property_float_set(ptr, prop, value); + if (RNA_property_float_get(ptr, prop) != value_coerce) { + RNA_property_float_set(ptr, prop, value_coerce); written = true; } } @@ -2846,7 +2852,7 @@ void BKE_animsys_evaluate_all_animation(Main *main, Scene *scene, float ctime) #define DEBUG_PRINT if (G.debug & G_DEBUG_DEPSGRAPH) printf -void BKE_animsys_eval_animdata(EvaluationContext *eval_ctx, ID *id) +void BKE_animsys_eval_animdata(const EvaluationContext *eval_ctx, ID *id) { AnimData *adt = BKE_animdata_from_id(id); Scene *scene = NULL; /* XXX: this is only needed for flushing RNA updates, @@ -2856,7 +2862,7 @@ void BKE_animsys_eval_animdata(EvaluationContext *eval_ctx, ID *id) BKE_animsys_evaluate_animdata(scene, id, adt, eval_ctx->ctime, ADT_RECALC_ANIM); } -void BKE_animsys_eval_driver(EvaluationContext *eval_ctx, +void BKE_animsys_eval_driver(const EvaluationContext *eval_ctx, ID *id, FCurve *fcu) { diff --git a/source/blender/blenkernel/intern/appdir.c b/source/blender/blenkernel/intern/appdir.c index 43fd47981b1..6dd852c7875 100644 --- a/source/blender/blenkernel/intern/appdir.c +++ b/source/blender/blenkernel/intern/appdir.c @@ -106,7 +106,8 @@ const char *BKE_appdir_folder_default(void) static char *blender_version_decimal(const int ver) { static char version_str[5]; - sprintf(version_str, "%d.%02d", ver / 100, ver % 100); + BLI_assert(ver < 1000); + BLI_snprintf(version_str, sizeof(version_str), "%d.%02d", ver / 100, ver % 100); return version_str; } @@ -212,8 +213,10 @@ static bool get_path_local( /* try EXECUTABLE_DIR/2.5x/folder_name - new default directory for local blender installed files */ #ifdef __APPLE__ /* due new codesign situation in OSX > 10.9.5 we must move the blender_version dir with contents to Resources */ - static char osx_resourses[FILE_MAX]; - sprintf(osx_resourses, "%s../Resources", bprogdir); + char osx_resourses[FILE_MAX]; + BLI_snprintf(osx_resourses, sizeof(osx_resourses), "%s../Resources", bprogdir); + /* Remove the '/../' added above. */ + BLI_cleanup_path(NULL, osx_resourses); return test_path(targetpath, targetpath_len, osx_resourses, blender_version_decimal(ver), relfolder); #else return test_path(targetpath, targetpath_len, bprogdir, blender_version_decimal(ver), relfolder); @@ -326,10 +329,12 @@ static bool get_path_system( return true; } } - /* try EXECUTABLE_DIR/release/folder_name */ - if (test_path(targetpath, targetpath_len, bprogdir, "release", relfolder)) + if (test_path(targetpath, targetpath_len, bprogdir, "release", relfolder)) { return true; + } + /* never use if not existing. */ + targetpath[0] = '\0'; /* end developer overrides */ @@ -591,6 +596,9 @@ static void where_am_i(char *fullname, const size_t maxlen, const char *name) else { BLI_path_program_search(fullname, maxlen, name); } + /* Remove "/./" and "/../" so string comparisons can be used on the path. */ + BLI_cleanup_path(NULL, fullname); + #if defined(DEBUG) if (!STREQ(name, fullname)) { printf("guessing '%s' == '%s'\n", name, fullname); @@ -683,13 +691,16 @@ bool BKE_appdir_program_python_search( return is_found; } +/** Keep in sync with `bpy.utils.app_template_paths()` */ static const char *app_template_directory_search[2] = { "startup" SEP_STR "bl_app_templates_user", "startup" SEP_STR "bl_app_templates_system", }; static const int app_template_directory_id[2] = { + /* Only 'USER' */ BLENDER_USER_SCRIPTS, + /* Covers 'LOCAL' & 'SYSTEM'. */ BLENDER_SYSTEM_SCRIPTS, }; diff --git a/source/blender/blenkernel/intern/armature.c b/source/blender/blenkernel/intern/armature.c index 38b98f1eee6..7dfc4df114c 100644 --- a/source/blender/blenkernel/intern/armature.c +++ b/source/blender/blenkernel/intern/armature.c @@ -82,7 +82,7 @@ bArmature *BKE_armature_add(Main *bmain, const char *name) { bArmature *arm; - arm = BKE_libblock_alloc(bmain, ID_AR, name); + arm = BKE_libblock_alloc(bmain, ID_AR, name, 0); arm->deformflag = ARM_DEF_VGROUP | ARM_DEF_ENVELOPE; arm->flag = ARM_COL_CUSTOM; /* custom bone-group colors */ arm->layer = 1; @@ -149,54 +149,70 @@ void BKE_armature_make_local(Main *bmain, bArmature *arm, const bool lib_local) BKE_id_make_local_generic(bmain, &arm->id, true, lib_local); } -static void copy_bonechildren(Bone *newBone, const Bone *oldBone, const Bone *actBone, Bone **newActBone) +static void copy_bonechildren( + Bone *bone_dst, const Bone *bone_src, const Bone *bone_src_act, Bone **r_bone_dst_act, const int flag) { - Bone *curBone, *newChildBone; + Bone *bone_src_child, *bone_dst_child; - if (oldBone == actBone) - *newActBone = newBone; + if (bone_src == bone_src_act) { + *r_bone_dst_act = bone_dst; + } - if (oldBone->prop) - newBone->prop = IDP_CopyProperty(oldBone->prop); + if (bone_src->prop) { + bone_dst->prop = IDP_CopyProperty_ex(bone_src->prop, flag); + } /* Copy this bone's list */ - BLI_duplicatelist(&newBone->childbase, &oldBone->childbase); + BLI_duplicatelist(&bone_dst->childbase, &bone_src->childbase); /* For each child in the list, update it's children */ - newChildBone = newBone->childbase.first; - for (curBone = oldBone->childbase.first; curBone; curBone = curBone->next) { - newChildBone->parent = newBone; - copy_bonechildren(newChildBone, curBone, actBone, newActBone); - newChildBone = newChildBone->next; + for (bone_src_child = bone_src->childbase.first, bone_dst_child = bone_dst->childbase.first; + bone_src_child; + bone_src_child = bone_src_child->next, bone_dst_child = bone_dst_child->next) + { + bone_dst_child->parent = bone_dst; + copy_bonechildren(bone_dst_child, bone_src_child, bone_src_act, r_bone_dst_act, flag); } } -bArmature *BKE_armature_copy(Main *bmain, const bArmature *arm) +/** + * Only copy internal data of Armature ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_armature_copy_data(Main *UNUSED(bmain), bArmature *arm_dst, const bArmature *arm_src, const int flag) { - bArmature *newArm; - Bone *oldBone, *newBone; - Bone *newActBone = NULL; + Bone *bone_src, *bone_dst; + Bone *bone_dst_act = NULL; + + /* We never handle usercount here for own data. */ + const int flag_subdata = flag | LIB_ID_CREATE_NO_USER_REFCOUNT; - newArm = BKE_libblock_copy(bmain, &arm->id); - BLI_duplicatelist(&newArm->bonebase, &arm->bonebase); + BLI_duplicatelist(&arm_dst->bonebase, &arm_src->bonebase); /* Duplicate the childrens' lists */ - newBone = newArm->bonebase.first; - for (oldBone = arm->bonebase.first; oldBone; oldBone = oldBone->next) { - newBone->parent = NULL; - copy_bonechildren(newBone, oldBone, arm->act_bone, &newActBone); - newBone = newBone->next; + bone_dst = arm_dst->bonebase.first; + for (bone_src = arm_src->bonebase.first; bone_src; bone_src = bone_src->next) { + bone_dst->parent = NULL; + copy_bonechildren(bone_dst, bone_src, arm_src->act_bone, &bone_dst_act, flag_subdata); + bone_dst = bone_dst->next; } - newArm->act_bone = newActBone; - - newArm->edbo = NULL; - newArm->act_edbone = NULL; - newArm->sketch = NULL; + arm_dst->act_bone = bone_dst_act; - BKE_id_copy_ensure_local(bmain, &arm->id, &newArm->id); + arm_dst->edbo = NULL; + arm_dst->act_edbone = NULL; + arm_dst->sketch = NULL; +} - return newArm; +bArmature *BKE_armature_copy(Main *bmain, const bArmature *arm) +{ + bArmature *arm_copy; + BKE_id_copy_ex(bmain, &arm->id, (ID **)&arm_copy, 0, false); + return arm_copy; } static Bone *get_named_bone_bonechildren(ListBase *lb, const char *name) @@ -1445,13 +1461,13 @@ void BKE_armature_loc_pose_to_bone(bPoseChannel *pchan, const float inloc[3], fl copy_v3_v3(outloc, nLocMat[3]); } -void BKE_armature_mat_pose_to_bone_ex(Object *ob, bPoseChannel *pchan, float inmat[4][4], float outmat[4][4]) +void BKE_armature_mat_pose_to_bone_ex(const struct EvaluationContext *eval_ctx, Object *ob, bPoseChannel *pchan, float inmat[4][4], float outmat[4][4]) { bPoseChannel work_pchan = *pchan; /* recalculate pose matrix with only parent transformations, * bone loc/sca/rot is ignored, scene and frame are not used. */ - BKE_pose_where_is_bone(NULL, ob, &work_pchan, 0.0f, false); + BKE_pose_where_is_bone(eval_ctx, NULL, ob, &work_pchan, 0.0f, false); /* find the matrix, need to remove the bone transforms first so this is * calculated as a matrix to set rather then a difference ontop of whats @@ -2178,7 +2194,9 @@ void BKE_pose_where_is_bone_tail(bPoseChannel *pchan) /* pchan is validated, as having bone and parent pointer * 'do_extra': when zero skips loc/size/rot, constraints and strip modifiers. */ -void BKE_pose_where_is_bone(Scene *scene, Object *ob, bPoseChannel *pchan, float ctime, bool do_extra) +void BKE_pose_where_is_bone( + const struct EvaluationContext *eval_ctx, Scene *scene, + Object *ob, bPoseChannel *pchan, float ctime, bool do_extra) { /* This gives a chan_mat with actions (ipos) results. */ if (do_extra) @@ -2217,7 +2235,7 @@ void BKE_pose_where_is_bone(Scene *scene, Object *ob, bPoseChannel *pchan, float cob = BKE_constraints_make_evalob(scene, ob, pchan, CONSTRAINT_OBTYPE_BONE); /* Solve PoseChannel's Constraints */ - BKE_constraints_solve(&pchan->constraints, cob, ctime); /* ctime doesnt alter objects */ + BKE_constraints_solve(eval_ctx, &pchan->constraints, cob, ctime); /* ctime doesnt alter objects */ /* cleanup after Constraint Solving * - applies matrix back to pchan, and frees temporary struct used @@ -2239,7 +2257,7 @@ void BKE_pose_where_is_bone(Scene *scene, Object *ob, bPoseChannel *pchan, float /* This only reads anim data from channels, and writes to channels */ /* This is the only function adding poses */ -void BKE_pose_where_is(Scene *scene, Object *ob) +void BKE_pose_where_is(const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob) { bArmature *arm; Bone *bone; @@ -2278,7 +2296,7 @@ void BKE_pose_where_is(Scene *scene, Object *ob) } /* 2a. construct the IK tree (standard IK) */ - BIK_initialize_tree(scene, ob, ctime); + BIK_initialize_tree(eval_ctx, scene, ob, ctime); /* 2b. construct the Spline IK trees * - this is not integrated as an IK plugin, since it should be able @@ -2290,15 +2308,15 @@ void BKE_pose_where_is(Scene *scene, Object *ob) for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) { /* 4a. if we find an IK root, we handle it separated */ if (pchan->flag & POSE_IKTREE) { - BIK_execute_tree(scene, ob, pchan, ctime); + BIK_execute_tree(eval_ctx, scene, ob, pchan, ctime); } /* 4b. if we find a Spline IK root, we handle it separated too */ else if (pchan->flag & POSE_IKSPLINE) { - BKE_splineik_execute_tree(scene, ob, pchan, ctime); + BKE_splineik_execute_tree(eval_ctx, scene, ob, pchan, ctime); } /* 5. otherwise just call the normal solver */ else if (!(pchan->flag & POSE_DONE)) { - BKE_pose_where_is_bone(scene, ob, pchan, ctime, 1); + BKE_pose_where_is_bone(eval_ctx, scene, ob, pchan, ctime, 1); } } /* 6. release the IK tree */ diff --git a/source/blender/blenkernel/intern/armature_update.c b/source/blender/blenkernel/intern/armature_update.c index 73e9f5d0774..1addbcbadc0 100644 --- a/source/blender/blenkernel/intern/armature_update.c +++ b/source/blender/blenkernel/intern/armature_update.c @@ -113,9 +113,11 @@ static void splineik_init_tree_from_pchan(Scene *scene, Object *UNUSED(ob), bPos * currently for paths to work it needs to go through the bevlist/displist system (ton) */ + /* TODO: Make sure this doesn't crash. */ +#if 0 /* only happens on reload file, but violates depsgraph still... fix! */ if (ELEM(NULL, ikData->tar->curve_cache, ikData->tar->curve_cache->path, ikData->tar->curve_cache->path->data)) { - BKE_displist_make_curveTypes(scene, ikData->tar, 0); + BKE_displist_make_curveTypes(eval_ctx, scene, ikData->tar, 0); /* path building may fail in EditMode after removing verts [#33268]*/ if (ELEM(NULL, ikData->tar->curve_cache->path, ikData->tar->curve_cache->path->data)) { @@ -123,6 +125,9 @@ static void splineik_init_tree_from_pchan(Scene *scene, Object *UNUSED(ob), bPos return; } } +#else + (void) scene; +#endif } /* find the root bone and the chain of bones from the root to the tip @@ -261,15 +266,16 @@ static void splineik_init_tree(Scene *scene, Object *ob, float UNUSED(ctime)) /* ----------- */ /* Evaluate spline IK for a given bone */ -static void splineik_evaluate_bone(tSplineIK_Tree *tree, Scene *scene, Object *ob, bPoseChannel *pchan, - int index, float ctime) +static void splineik_evaluate_bone( + const struct EvaluationContext *eval_ctx, tSplineIK_Tree *tree, Scene *scene, Object *ob, bPoseChannel *pchan, + int index, float ctime) { bSplineIKConstraint *ikData = tree->ikData; float poseHead[3], poseTail[3], poseMat[4][4]; float splineVec[3], scaleFac, radius = 1.0f; /* firstly, calculate the bone matrix the standard way, since this is needed for roll control */ - BKE_pose_where_is_bone(scene, ob, pchan, ctime, 1); + BKE_pose_where_is_bone(eval_ctx, scene, ob, pchan, ctime, 1); copy_v3_v3(poseHead, pchan->pose_head); copy_v3_v3(poseTail, pchan->pose_tail); @@ -511,7 +517,7 @@ static void splineik_evaluate_bone(tSplineIK_Tree *tree, Scene *scene, Object *o } /* Evaluate the chain starting from the nominated bone */ -static void splineik_execute_tree(Scene *scene, Object *ob, bPoseChannel *pchan_root, float ctime) +static void splineik_execute_tree(const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, bPoseChannel *pchan_root, float ctime) { tSplineIK_Tree *tree; @@ -525,7 +531,7 @@ static void splineik_execute_tree(Scene *scene, Object *ob, bPoseChannel *pchan_ */ for (i = tree->chainlen - 1; i >= 0; i--) { bPoseChannel *pchan = tree->chain[i]; - splineik_evaluate_bone(tree, scene, ob, pchan, i, ctime); + splineik_evaluate_bone(eval_ctx, tree, scene, ob, pchan, i, ctime); } /* free the tree info specific to SplineIK trees now */ @@ -544,19 +550,20 @@ void BKE_pose_splineik_init_tree(Scene *scene, Object *ob, float ctime) splineik_init_tree(scene, ob, ctime); } -void BKE_splineik_execute_tree(Scene *scene, Object *ob, bPoseChannel *pchan_root, float ctime) +void BKE_splineik_execute_tree( + const struct EvaluationContext *eval_ctx, Scene *scene, + Object *ob, bPoseChannel *pchan_root, float ctime) { - splineik_execute_tree(scene, ob, pchan_root, ctime); + splineik_execute_tree(eval_ctx, scene, ob, pchan_root, ctime); } /* *************** Depsgraph evaluation callbacks ************ */ -void BKE_pose_eval_init(struct EvaluationContext *UNUSED(eval_ctx), - Scene *scene, +void BKE_pose_eval_init(const struct EvaluationContext *UNUSED(eval_ctx), + Scene *UNUSED(scene), Object *ob, bPose *pose) { - float ctime = BKE_scene_frame_get(scene); /* not accurate... */ bPoseChannel *pchan; DEBUG_PRINT("%s on %s\n", __func__, ob->id.name); @@ -574,9 +581,19 @@ void BKE_pose_eval_init(struct EvaluationContext *UNUSED(eval_ctx), for (pchan = pose->chanbase.first; pchan != NULL; pchan = pchan->next) { pchan->flag &= ~(POSE_DONE | POSE_CHAIN | POSE_IKTREE | POSE_IKSPLINE); } +} + +void BKE_pose_eval_init_ik(const struct EvaluationContext *eval_ctx, + Scene *scene, + Object *ob, + bPose *UNUSED(pose)) +{ + float ctime = BKE_scene_frame_get(scene); /* not accurate... */ + + DEBUG_PRINT("%s on %s\n", __func__, ob->id.name); /* 2a. construct the IK tree (standard IK) */ - BIK_initialize_tree(scene, ob, ctime); + BIK_initialize_tree(eval_ctx, scene, ob, ctime); /* 2b. construct the Spline IK trees * - this is not integrated as an IK plugin, since it should be able @@ -585,7 +602,7 @@ void BKE_pose_eval_init(struct EvaluationContext *UNUSED(eval_ctx), BKE_pose_splineik_init_tree(scene, ob, ctime); } -void BKE_pose_eval_bone(struct EvaluationContext *UNUSED(eval_ctx), +void BKE_pose_eval_bone(const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, bPoseChannel *pchan) @@ -613,14 +630,14 @@ void BKE_pose_eval_bone(struct EvaluationContext *UNUSED(eval_ctx), if ((pchan->flag & POSE_DONE) == 0) { /* TODO(sergey): Use time source node for time. */ float ctime = BKE_scene_frame_get(scene); /* not accurate... */ - BKE_pose_where_is_bone(scene, ob, pchan, ctime, 1); + BKE_pose_where_is_bone(eval_ctx, scene, ob, pchan, ctime, 1); } } } } } -void BKE_pose_constraints_evaluate(struct EvaluationContext *UNUSED(eval_ctx), +void BKE_pose_constraints_evaluate(const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, bPoseChannel *pchan) @@ -636,12 +653,12 @@ void BKE_pose_constraints_evaluate(struct EvaluationContext *UNUSED(eval_ctx), else { if ((pchan->flag & POSE_DONE) == 0) { float ctime = BKE_scene_frame_get(scene); /* not accurate... */ - BKE_pose_where_is_bone(scene, ob, pchan, ctime, 1); + BKE_pose_where_is_bone(eval_ctx, scene, ob, pchan, ctime, 1); } } } -void BKE_pose_bone_done(struct EvaluationContext *UNUSED(eval_ctx), +void BKE_pose_bone_done(const struct EvaluationContext *UNUSED(eval_ctx), bPoseChannel *pchan) { float imat[4][4]; @@ -652,27 +669,27 @@ void BKE_pose_bone_done(struct EvaluationContext *UNUSED(eval_ctx), } } -void BKE_pose_iktree_evaluate(struct EvaluationContext *UNUSED(eval_ctx), +void BKE_pose_iktree_evaluate(const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, bPoseChannel *rootchan) { float ctime = BKE_scene_frame_get(scene); /* not accurate... */ DEBUG_PRINT("%s on %s pchan %s\n", __func__, ob->id.name, rootchan->name); - BIK_execute_tree(scene, ob, rootchan, ctime); + BIK_execute_tree(eval_ctx, scene, ob, rootchan, ctime); } -void BKE_pose_splineik_evaluate(struct EvaluationContext *UNUSED(eval_ctx), +void BKE_pose_splineik_evaluate(const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, bPoseChannel *rootchan) { float ctime = BKE_scene_frame_get(scene); /* not accurate... */ DEBUG_PRINT("%s on %s pchan %s\n", __func__, ob->id.name, rootchan->name); - BKE_splineik_execute_tree(scene, ob, rootchan, ctime); + BKE_splineik_execute_tree(eval_ctx, scene, ob, rootchan, ctime); } -void BKE_pose_eval_flush(struct EvaluationContext *UNUSED(eval_ctx), +void BKE_pose_eval_flush(const struct EvaluationContext *UNUSED(eval_ctx), Scene *scene, Object *ob, bPose *UNUSED(pose)) @@ -687,7 +704,7 @@ void BKE_pose_eval_flush(struct EvaluationContext *UNUSED(eval_ctx), ob->recalc &= ~OB_RECALC_ALL; } -void BKE_pose_eval_proxy_copy(struct EvaluationContext *UNUSED(eval_ctx), Object *ob) +void BKE_pose_eval_proxy_copy(const struct EvaluationContext *UNUSED(eval_ctx), Object *ob) { BLI_assert(ID_IS_LINKED_DATABLOCK(ob) && ob->proxy_from != NULL); DEBUG_PRINT("%s on %s\n", __func__, ob->id.name); diff --git a/source/blender/blenkernel/intern/blender.c b/source/blender/blenkernel/intern/blender.c index ebb10c02683..1f75c85291d 100644 --- a/source/blender/blenkernel/intern/blender.c +++ b/source/blender/blenkernel/intern/blender.c @@ -230,19 +230,6 @@ void BKE_blender_userdef_free_data(UserDef *userdef) } /** - * Handle changes in settings that need refreshing. - */ -void BKE_blender_userdef_refresh(void) -{ - /* prevent accidents */ - if (U.pixelsize == 0) U.pixelsize = 1; - - BLF_default_dpi(U.pixelsize * U.dpi); - U.widget_unit = (U.pixelsize * U.dpi * 20 + 36) / 72; - -} - -/** * Write U from userdef. * This function defines which settings a template will override for the user preferences. */ diff --git a/source/blender/blenkernel/intern/blender_copybuffer.c b/source/blender/blenkernel/intern/blender_copybuffer.c index 206b0f2a8cc..2d37f3ab0cb 100644 --- a/source/blender/blenkernel/intern/blender_copybuffer.c +++ b/source/blender/blenkernel/intern/blender_copybuffer.c @@ -49,6 +49,7 @@ #include "BKE_main.h" #include "BKE_scene.h" +#include "DEG_depsgraph.h" #include "DEG_depsgraph_build.h" #include "BLO_readfile.h" @@ -160,6 +161,10 @@ bool BKE_copybuffer_paste(bContext *C, const char *libname, const short flag, Re /* recreate dependency graph to include new objects */ DEG_relations_tag_update(bmain); + /* Tag update the scene to flush base collection settings, since the new object is added to a + * new (active) collection, not its original collection, thus need recalculation. */ + DEG_id_tag_update(&scene->id, 0); + BLO_blendhandle_close(bh); /* remove library... */ diff --git a/source/blender/blenkernel/intern/blendfile.c b/source/blender/blenkernel/intern/blendfile.c index 53f2462ee98..e1350aa8a46 100644 --- a/source/blender/blenkernel/intern/blendfile.c +++ b/source/blender/blenkernel/intern/blendfile.c @@ -117,6 +117,7 @@ static void setup_app_data( const char *filepath, ReportList *reports) { Scene *curscene = NULL; + const bool is_startup = (bfd->filename[0] == '\0'); const bool recover = (G.fileflags & G_FILE_RECOVER) != 0; enum { LOAD_UI = 1, @@ -132,7 +133,7 @@ static void setup_app_data( else if (BLI_listbase_is_empty(&bfd->main->screen)) { mode = LOAD_UNDO; } - else if (G.fileflags & G_FILE_NO_UI) { + else if ((G.fileflags & G_FILE_NO_UI) && (is_startup == false)) { mode = LOAD_UI_OFF; } else { @@ -193,7 +194,7 @@ static void setup_app_data( } if (cur_render_layer == NULL) { /* fallback to scene layer */ - cur_render_layer = BKE_scene_layer_render_active(curscene); + cur_render_layer = BKE_scene_layer_from_scene_get(curscene); } if (track_undo_scene) { @@ -218,6 +219,13 @@ static void setup_app_data( BKE_screen_view3d_scene_sync(curscreen, curscene); } } + + /* We need to tag this here because events may be handled immediately after. + * only the current screen is important because we wont have to handle + * events from multiple screens at once.*/ + { + BKE_screen_manipulator_tag_refresh(curscreen); + } } /* free G.main Main database */ @@ -257,7 +265,9 @@ static void setup_app_data( CTX_data_scene_set(C, curscene); } else { - G.fileflags = bfd->fileflags; + /* Keep state from preferences. */ + const int fileflags_skip = G_FILE_FLAGS_RUNTIME; + G.fileflags = (G.fileflags & fileflags_skip) | (bfd->fileflags & ~fileflags_skip); CTX_wm_manager_set(C, G.main->wm.first); CTX_wm_screen_set(C, bfd->curscreen); CTX_data_scene_set(C, bfd->curscene); @@ -333,20 +343,19 @@ static void setup_app_data( } } - if (mode == LOAD_UI_OFF && BLI_listbase_is_empty(&G.main->wm)) { - /* XXX prevent crash in pdInitEffectors called through DEG_scene_relations_rebuild (see T51794). - * Can be removed once BKE_scene_layer_context_active_ex gets workspace passed. */ - BLI_addhead(&G.main->wm, CTX_wm_manager(C)); - BKE_scene_set_background(G.main, curscene); - BLI_listbase_clear(&G.main->wm); - } - else { - BKE_scene_set_background(G.main, curscene); + /* Setting scene might require having a dependency graph, with copy on write + * we need to make sure we ensure scene has correct color management before + * constructing dependency graph. + */ + if (mode != LOAD_UNDO) { + IMB_colormanagement_check_file_config(G.main); } + BKE_scene_set_background(G.main, curscene); + if (mode != LOAD_UNDO) { + /* TODO(sergey): Can this be also move above? */ RE_FreeAllPersistentData(); - IMB_colormanagement_check_file_config(G.main); } MEM_freeN(bfd); diff --git a/source/blender/blenkernel/intern/boids.c b/source/blender/blenkernel/intern/boids.c index 7ca4e07076d..8c78787c259 100644 --- a/source/blender/blenkernel/intern/boids.c +++ b/source/blender/blenkernel/intern/boids.c @@ -132,6 +132,7 @@ static int rule_goal_avoid(BoidRule *rule, BoidBrainData *bbd, BoidValues *val, if (eff == NULL && gabr->ob) { memset(&temp_eff, 0, sizeof(EffectorCache)); temp_eff.ob = gabr->ob; + temp_eff.eval_ctx = bbd->sim->eval_ctx; temp_eff.scene = bbd->sim->scene; eff = &temp_eff; get_effector_data(eff, &efd, &epoint, 0); diff --git a/source/blender/blenkernel/intern/brush.c b/source/blender/blenkernel/intern/brush.c index aae323a5056..aeaead578a1 100644 --- a/source/blender/blenkernel/intern/brush.c +++ b/source/blender/blenkernel/intern/brush.c @@ -152,7 +152,7 @@ Brush *BKE_brush_add(Main *bmain, const char *name, short ob_mode) { Brush *brush; - brush = BKE_libblock_alloc(bmain, ID_BR, name); + brush = BKE_libblock_alloc(bmain, ID_BR, name, 0); BKE_brush_init(brush); @@ -172,34 +172,38 @@ struct Brush *BKE_brush_first_search(struct Main *bmain, short ob_mode) return NULL; } -Brush *BKE_brush_copy(Main *bmain, const Brush *brush) +/** + * Only copy internal data of Brush ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_brush_copy_data(Main *UNUSED(bmain), Brush *brush_dst, const Brush *brush_src, const int flag) { - Brush *brushn; - - brushn = BKE_libblock_copy(bmain, &brush->id); - - if (brush->mtex.tex) - id_us_plus((ID *)brush->mtex.tex); - - if (brush->mask_mtex.tex) - id_us_plus((ID *)brush->mask_mtex.tex); - - if (brush->paint_curve) - id_us_plus((ID *)brush->paint_curve); - - if (brush->icon_imbuf) - brushn->icon_imbuf = IMB_dupImBuf(brush->icon_imbuf); + if (brush_src->icon_imbuf) { + brush_dst->icon_imbuf = IMB_dupImBuf(brush_src->icon_imbuf); + } - brushn->preview = NULL; + if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0) { + BKE_previewimg_id_copy(&brush_dst->id, &brush_src->id); + } + else { + brush_dst->preview = NULL; + } - brushn->curve = curvemapping_copy(brush->curve); + brush_dst->curve = curvemapping_copy(brush_src->curve); /* enable fake user by default */ - id_fake_user_set(&brushn->id); - - BKE_id_copy_ensure_local(bmain, &brush->id, &brushn->id); + id_fake_user_set(&brush_dst->id); +} - return brushn; +Brush *BKE_brush_copy(Main *bmain, const Brush *brush) +{ + Brush *brush_copy; + BKE_id_copy_ex(bmain, &brush->id, (ID **)&brush_copy, 0, false); + return brush_copy; } /** Free (or release) any data used by this brush (does not free the brush itself). */ @@ -517,13 +521,13 @@ int BKE_brush_clone_image_delete(Brush *brush) * region space mouse coordinates, or 3d world coordinates for 3D mapping. * * rgba outputs straight alpha. */ -float BKE_brush_sample_tex_3D(const Scene *scene, Brush *br, +float BKE_brush_sample_tex_3D(const Scene *scene, const Brush *br, const float point[3], float rgba[4], const int thread, struct ImagePool *pool) { UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings; - MTex *mtex = &br->mtex; + const MTex *mtex = &br->mtex; float intensity = 1.0; bool hasrgb = false; @@ -817,7 +821,7 @@ int BKE_brush_size_get(const Scene *scene, const Brush *brush) return size; } -int BKE_brush_use_locked_size(const Scene *scene, const Brush *brush) +bool BKE_brush_use_locked_size(const Scene *scene, const Brush *brush) { const short us_flag = scene->toolsettings->unified_paint_settings.flag; @@ -826,7 +830,7 @@ int BKE_brush_use_locked_size(const Scene *scene, const Brush *brush) (brush->flag & BRUSH_LOCK_SIZE); } -int BKE_brush_use_size_pressure(const Scene *scene, const Brush *brush) +bool BKE_brush_use_size_pressure(const Scene *scene, const Brush *brush) { const short us_flag = scene->toolsettings->unified_paint_settings.flag; @@ -835,7 +839,7 @@ int BKE_brush_use_size_pressure(const Scene *scene, const Brush *brush) (brush->flag & BRUSH_SIZE_PRESSURE); } -int BKE_brush_use_alpha_pressure(const Scene *scene, const Brush *brush) +bool BKE_brush_use_alpha_pressure(const Scene *scene, const Brush *brush) { const short us_flag = scene->toolsettings->unified_paint_settings.flag; @@ -844,6 +848,16 @@ int BKE_brush_use_alpha_pressure(const Scene *scene, const Brush *brush) (brush->flag & BRUSH_ALPHA_PRESSURE); } +bool BKE_brush_sculpt_has_secondary_color(const Brush *brush) +{ + return ELEM( + brush->sculpt_tool, SCULPT_TOOL_BLOB, SCULPT_TOOL_DRAW, + SCULPT_TOOL_INFLATE, SCULPT_TOOL_CLAY, SCULPT_TOOL_CLAY_STRIPS, + SCULPT_TOOL_PINCH, SCULPT_TOOL_CREASE, SCULPT_TOOL_LAYER, + SCULPT_TOOL_FLATTEN, SCULPT_TOOL_FILL, SCULPT_TOOL_SCRAPE, + SCULPT_TOOL_MASK); +} + void BKE_brush_unprojected_radius_set(Scene *scene, Brush *brush, float unprojected_radius) { UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings; @@ -962,7 +976,7 @@ void BKE_brush_randomize_texture_coords(UnifiedPaintSettings *ups, bool mask) } /* Uses the brush curve control to find a strength value */ -float BKE_brush_curve_strength(Brush *br, float p, const float len) +float BKE_brush_curve_strength(const Brush *br, float p, const float len) { float strength; diff --git a/source/blender/blenkernel/intern/bvhutils.c b/source/blender/blenkernel/intern/bvhutils.c index c1fad4f80c8..775499304d4 100644 --- a/source/blender/blenkernel/intern/bvhutils.c +++ b/source/blender/blenkernel/intern/bvhutils.c @@ -1137,7 +1137,6 @@ BVHTree *bvhtree_from_mesh_looptri( const MLoopTri *looptri = NULL; bool vert_allocated = false; bool loop_allocated = false; - bool looptri_allocated = false; BLI_rw_mutex_lock(&cache_rwlock, THREAD_LOCK_READ); tree = bvhcache_find(dm->bvhCache, BVHTREE_FROM_LOOPTRI); @@ -1150,12 +1149,7 @@ BVHTree *bvhtree_from_mesh_looptri( mpoly = DM_get_poly_array(dm, &poly_allocated); mloop = DM_get_loop_array(dm, &loop_allocated); - looptri = DM_get_looptri_array( - dm, - mvert, - mpoly, dm->getNumPolys(dm), - mloop, dm->getNumLoops(dm), - &looptri_allocated); + looptri = dm->getLoopTriArray(dm); if (poly_allocated) { MEM_freeN(mpoly); @@ -1193,7 +1187,7 @@ BVHTree *bvhtree_from_mesh_looptri( data, tree, true, epsilon, mvert, vert_allocated, mloop, loop_allocated, - looptri, looptri_allocated); + looptri, false); } else { if (vert_allocated) { @@ -1202,9 +1196,6 @@ BVHTree *bvhtree_from_mesh_looptri( if (loop_allocated) { MEM_freeN(mloop); } - if (looptri_allocated) { - MEM_freeN((void *)looptri); - } memset(data, 0, sizeof(*data)); } diff --git a/source/blender/blenkernel/intern/cachefile.c b/source/blender/blenkernel/intern/cachefile.c index 43dcde302b3..9d02a27187c 100644 --- a/source/blender/blenkernel/intern/cachefile.c +++ b/source/blender/blenkernel/intern/cachefile.c @@ -66,7 +66,7 @@ void BKE_cachefiles_exit(void) void *BKE_cachefile_add(Main *bmain, const char *name) { - CacheFile *cache_file = BKE_libblock_alloc(bmain, ID_CF, name); + CacheFile *cache_file = BKE_libblock_alloc(bmain, ID_CF, name, 0); BKE_cachefile_init(cache_file); @@ -100,16 +100,26 @@ void BKE_cachefile_free(CacheFile *cache_file) BLI_freelistN(&cache_file->object_paths); } -CacheFile *BKE_cachefile_copy(Main *bmain, const CacheFile *cache_file) +/** + * Only copy internal data of CacheFile ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_cachefile_copy_data( + Main *UNUSED(bmain), CacheFile *cache_file_dst, const CacheFile *UNUSED(cache_file_src), const int UNUSED(flag)) { - CacheFile *new_cache_file = BKE_libblock_copy(bmain, &cache_file->id); - new_cache_file->handle = NULL; - - BLI_listbase_clear(&new_cache_file->object_paths); - - BKE_id_copy_ensure_local(bmain, &cache_file->id, &new_cache_file->id); + cache_file_dst->handle = NULL; + BLI_listbase_clear(&cache_file_dst->object_paths); +} - return new_cache_file; +CacheFile *BKE_cachefile_copy(Main *bmain, const CacheFile *cache_file) +{ + CacheFile *cache_file_copy; + BKE_id_copy_ex(bmain, &cache_file->id, (ID **)&cache_file_copy, 0, false); + return cache_file_copy; } void BKE_cachefile_make_local(Main *bmain, CacheFile *cache_file, const bool lib_local) @@ -166,10 +176,12 @@ void BKE_cachefile_update_frame(Main *bmain, Scene *scene, const float ctime, co const float time = BKE_cachefile_time_offset(cache_file, ctime, fps); if (BKE_cachefile_filepath_get(bmain, cache_file, time, filename)) { + BKE_cachefile_clean(bmain, cache_file); #ifdef WITH_ALEMBIC ABC_free_handle(cache_file->handle); cache_file->handle = ABC_create_handle(filename, NULL); #endif + break; } } } diff --git a/source/blender/blenkernel/intern/camera.c b/source/blender/blenkernel/intern/camera.c index 2e36de4e8d4..97bb679f8b0 100644 --- a/source/blender/blenkernel/intern/camera.c +++ b/source/blender/blenkernel/intern/camera.c @@ -86,22 +86,31 @@ void *BKE_camera_add(Main *bmain, const char *name) { Camera *cam; - cam = BKE_libblock_alloc(bmain, ID_CA, name); + cam = BKE_libblock_alloc(bmain, ID_CA, name, 0); BKE_camera_init(cam); return cam; } -Camera *BKE_camera_copy(Main *bmain, const Camera *cam) +/** + * Only copy internal data of Camera ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_camera_copy_data(Main *UNUSED(bmain), Camera *UNUSED(cam_dst), const Camera *UNUSED(cam_src), const int UNUSED(flag)) { - Camera *camn; - - camn = BKE_libblock_copy(bmain, &cam->id); - - BKE_id_copy_ensure_local(bmain, &cam->id, &camn->id); + /* Nothing to do! */ +} - return camn; +Camera *BKE_camera_copy(Main *bmain, const Camera *cam) +{ + Camera *cam_copy; + BKE_id_copy_ex(bmain, &cam->id, (ID **)&cam_copy, 0, false); + return cam_copy; } void BKE_camera_make_local(Main *bmain, Camera *cam, const bool lib_local) diff --git a/source/blender/blenkernel/intern/cdderivedmesh.c b/source/blender/blenkernel/intern/cdderivedmesh.c index 155634c9d9c..d1282c1a0fe 100644 --- a/source/blender/blenkernel/intern/cdderivedmesh.c +++ b/source/blender/blenkernel/intern/cdderivedmesh.c @@ -34,10 +34,12 @@ * \ingroup bke */ +#include "atomic_ops.h" + #include "BLI_math.h" #include "BLI_edgehash.h" #include "BLI_utildefines.h" -#include "BLI_stackdefines.h" +#include "BLI_utildefines_stack.h" #include "BKE_pbvh.h" #include "BKE_cdderivedmesh.h" @@ -549,6 +551,11 @@ static void cdDM_drawMappedFaces( const int *index_mp_to_orig = dm->getPolyDataArray(dm, CD_ORIGINDEX); + if (cddm->pbvh) { + if (G.debug_value == 14) + BKE_pbvh_draw_BB(cddm->pbvh); + } + /* fist, setup common buffers */ GPU_vertex_setup(dm); GPU_triangle_setup(dm); @@ -1398,8 +1405,8 @@ static void cdDM_buffer_copy_mcol( for (i = 0; i < totpoly; i++, mpoly++) { for (j = 0; j < mpoly->totloop; j++) { - copy_v3_v3_uchar(&varray[start], &mloopcol[mpoly->loopstart + j].r); - start += 3; + copy_v4_v4_uchar(&varray[start], &mloopcol[mpoly->loopstart + j].r); + start += 4; } } } @@ -1799,25 +1806,17 @@ void CDDM_recalc_looptri(DerivedMesh *dm) const unsigned int totloop = dm->numLoopData; DM_ensure_looptri_data(dm); + BLI_assert(totpoly == 0 || cddm->dm.looptris.array_wip != NULL); BKE_mesh_recalc_looptri( cddm->mloop, cddm->mpoly, cddm->mvert, totloop, totpoly, - cddm->dm.looptris.array); -} + cddm->dm.looptris.array_wip); -static const MLoopTri *cdDM_getLoopTriArray(DerivedMesh *dm) -{ - if (dm->looptris.array) { - BLI_assert(poly_to_tri_count(dm->numPolyData, dm->numLoopData) == dm->looptris.num); - } - else { - dm->recalcLoopTri(dm); - - /* ccdm is an exception here, that recalcLoopTri will fill in the array too */ - } - return dm->looptris.array; + BLI_assert(cddm->dm.looptris.array == NULL); + atomic_cas_ptr((void **)&cddm->dm.looptris.array, cddm->dm.looptris.array, cddm->dm.looptris.array_wip); + cddm->dm.looptris.array_wip = NULL; } static void cdDM_free_internal(CDDerivedMesh *cddm) @@ -1870,8 +1869,6 @@ static CDDerivedMesh *cdDM_create(const char *desc) dm->getEdgeDataArray = DM_get_edge_data_layer; dm->getTessFaceDataArray = DM_get_tessface_data_layer; - dm->getLoopTriArray = cdDM_getLoopTriArray; - dm->calcNormals = CDDM_calc_normals; dm->calcLoopNormals = CDDM_calc_loop_normals; dm->calcLoopNormalsSpaceArray = CDDM_calc_loop_normals_spacearr; @@ -3056,7 +3053,7 @@ DerivedMesh *CDDM_merge_verts(DerivedMesh *dm, const int *vtargetmap, const int MPoly *target_poly = cddm->mpoly + *(cddm->pmap[v_target].indices + i_poly); if (cddm_poly_compare(cddm->mloop, mp, target_poly, vtargetmap, +1) || - cddm_poly_compare(cddm->mloop, mp, target_poly, vtargetmap, -1)) + cddm_poly_compare(cddm->mloop, mp, target_poly, vtargetmap, -1)) { found = true; break; diff --git a/source/blender/blenkernel/intern/cloth.c b/source/blender/blenkernel/intern/cloth.c index 6d0ad3255a2..09b793629f7 100644 --- a/source/blender/blenkernel/intern/cloth.c +++ b/source/blender/blenkernel/intern/cloth.c @@ -345,7 +345,7 @@ static int do_init_cloth(Object *ob, ClothModifierData *clmd, DerivedMesh *resul return 1; } -static int do_step_cloth(Object *ob, ClothModifierData *clmd, DerivedMesh *result, int framenr) +static int do_step_cloth(const struct EvaluationContext *eval_ctx, Object *ob, ClothModifierData *clmd, DerivedMesh *result, int framenr) { ClothVertex *verts = NULL; Cloth *cloth; @@ -370,7 +370,7 @@ static int do_step_cloth(Object *ob, ClothModifierData *clmd, DerivedMesh *resul mul_m4_v3(ob->obmat, verts->xconst); } - effectors = pdInitEffectors(clmd->scene, ob, NULL, clmd->sim_parms->effector_weights, true); + effectors = pdInitEffectors(eval_ctx, clmd->scene, ob, NULL, clmd->sim_parms->effector_weights, true); if (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_DYNAMIC_BASEMESH ) cloth_update_verts ( ob, clmd, result ); @@ -400,7 +400,7 @@ static int do_step_cloth(Object *ob, ClothModifierData *clmd, DerivedMesh *resul /************************************************ * clothModifier_do - main simulation function ************************************************/ -void clothModifier_do(ClothModifierData *clmd, Scene *scene, Object *ob, DerivedMesh *dm, float (*vertexCos)[3]) +void clothModifier_do(ClothModifierData *clmd, const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, DerivedMesh *dm, float (*vertexCos)[3]) { PointCache *cache; PTCacheID pid; @@ -489,7 +489,7 @@ void clothModifier_do(ClothModifierData *clmd, Scene *scene, Object *ob, Derived /* do simulation */ BKE_ptcache_validate(cache, framenr); - if (!do_step_cloth(ob, clmd, dm, framenr)) { + if (!do_step_cloth(eval_ctx, ob, clmd, dm, framenr)) { BKE_ptcache_invalidate(cache); } else @@ -806,7 +806,6 @@ static int cloth_from_object(Object *ob, ClothModifierData *clmd, DerivedMesh *d if ( !dm ) return 0; - DM_ensure_looptri(dm); cloth_from_mesh ( clmd, dm ); // create springs diff --git a/source/blender/blenkernel/intern/collection.c b/source/blender/blenkernel/intern/collection.c index 3bf3a5ce5a4..2b69c176c39 100644 --- a/source/blender/blenkernel/intern/collection.c +++ b/source/blender/blenkernel/intern/collection.c @@ -261,6 +261,9 @@ void BKE_collection_object_add_from(Scene *scene, Object *ob_src, Object *ob_dst for (SceneLayer *sl = scene->render_layers.first; sl; sl = sl->next) { Base *base_src = BKE_scene_layer_base_find(sl, ob_src); if (base_src != NULL) { + if (base_src->collection_properties == NULL) { + continue; + } Base *base_dst = BKE_scene_layer_base_find(sl, ob_dst); IDP_MergeGroup(base_dst->collection_properties, base_src->collection_properties, true); } @@ -565,7 +568,7 @@ void BKE_scene_collections_iterator_end(struct BLI_Iterator *iter) typedef struct SceneObjectsIteratorData { GSet *visited; - LinkData *link; + LinkData *link_next; BLI_Iterator scene_collection_iter; } SceneObjectsIteratorData; @@ -597,8 +600,9 @@ static LinkData *object_base_unique(GSet *gs, LinkData *link) { for (; link != NULL; link = link->next) { Object *ob = link->data; - if (!BLI_gset_haskey(gs, ob)) { - BLI_gset_add(gs, ob); + void **ob_key_p; + if (!BLI_gset_ensure_p_ex(gs, ob, &ob_key_p)) { + *ob_key_p = ob; return link; } } @@ -608,10 +612,10 @@ static LinkData *object_base_unique(GSet *gs, LinkData *link) void BKE_scene_objects_iterator_next(BLI_Iterator *iter) { SceneObjectsIteratorData *data = iter->data; - LinkData *link = data->link ? object_base_unique(data->visited, data->link->next) : NULL; + LinkData *link = data->link_next ? object_base_unique(data->visited, data->link_next) : NULL; if (link) { - data->link = link; + data->link_next = link->next; iter->current = link->data; } else { @@ -623,8 +627,8 @@ void BKE_scene_objects_iterator_next(BLI_Iterator *iter) /* get the first unique object of this collection */ LinkData *new_link = object_base_unique(data->visited, sc->objects.first); if (new_link) { - data->link = new_link; - iter->current = data->link->data; + data->link_next = new_link->next; + iter->current = new_link->data; return; } BKE_scene_collections_iterator_next(&data->scene_collection_iter); diff --git a/source/blender/blenkernel/intern/colortools.c b/source/blender/blenkernel/intern/colortools.c index ee0f904c3a6..310255a15c1 100644 --- a/source/blender/blenkernel/intern/colortools.c +++ b/source/blender/blenkernel/intern/colortools.c @@ -1380,7 +1380,7 @@ void scopes_update(Scopes *scopes, ImBuf *ibuf, const ColorManagedViewSettings * /* Keep number of threads in sync with the merge parts below. */ ScopesUpdateData data = { - .scopes = scopes, . ibuf = ibuf, + .scopes = scopes, .ibuf = ibuf, .cm_processor = cm_processor, .display_buffer = display_buffer, .ycc_mode = ycc_mode, .bin_lum = bin_lum, .bin_r = bin_r, .bin_g = bin_g, .bin_b = bin_b, .bin_a = bin_a, }; diff --git a/source/blender/blenkernel/intern/constraint.c b/source/blender/blenkernel/intern/constraint.c index 07a6b304dff..1c6bc7f98a2 100644 --- a/source/blender/blenkernel/intern/constraint.c +++ b/source/blender/blenkernel/intern/constraint.c @@ -689,7 +689,7 @@ static bConstraintTypeInfo CTI_CONSTRNAME = { /* This function should be used for the get_target_matrix member of all * constraints that are not picky about what happens to their target matrix. */ -static void default_get_tarmat(bConstraint *con, bConstraintOb *UNUSED(cob), bConstraintTarget *ct, float UNUSED(ctime)) +static void default_get_tarmat(const struct EvaluationContext *UNUSED(eval_ctx), bConstraint *con, bConstraintOb *UNUSED(cob), bConstraintTarget *ct, float UNUSED(ctime)) { if (VALID_CONS_TARGET(ct)) constraint_target_to_mat4(ct->tar, ct->subtarget, ct->matrix, CONSTRAINT_SPACE_WORLD, ct->space, con->flag, con->headtail); @@ -1019,7 +1019,7 @@ static void vectomat(const float vec[3], const float target_up[3], short axis, s } /* project the up vector onto the plane specified by n */ - project_v3_v3v3(proj, u, n); /* first u onto n... */ + project_v3_v3v3_normalized(proj, u, n); /* first u onto n... */ sub_v3_v3v3(proj, u, proj); /* then onto the plane */ /* proj specifies the transformation of the up axis */ @@ -1155,7 +1155,7 @@ static void kinematic_flush_tars(bConstraint *con, ListBase *list, bool no_copy) } } -static void kinematic_get_tarmat(bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float UNUSED(ctime)) +static void kinematic_get_tarmat(const struct EvaluationContext *UNUSED(eval_ctx), bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float UNUSED(ctime)) { bKinematicConstraint *data = con->data; @@ -1242,7 +1242,7 @@ static void followpath_flush_tars(bConstraint *con, ListBase *list, bool no_copy } } -static void followpath_get_tarmat(bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float UNUSED(ctime)) +static void followpath_get_tarmat(const struct EvaluationContext *eval_ctx, bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float UNUSED(ctime)) { bFollowPathConstraint *data = con->data; @@ -1259,7 +1259,7 @@ static void followpath_get_tarmat(bConstraint *con, bConstraintOb *cob, bConstra #ifdef CYCLIC_DEPENDENCY_WORKAROUND if (ct->tar->curve_cache == NULL) { - BKE_displist_make_curveTypes(cob->scene, ct->tar, false); + BKE_displist_make_curveTypes(eval_ctx, cob->scene, ct->tar, false); } #endif @@ -1930,7 +1930,7 @@ static void samevolume_evaluate(bConstraint *con, bConstraintOb *cob, ListBase * /* calculate normalizing scale factor for non-essential values */ if (obsize[data->flag] != 0) - fac = sqrtf(volume / obsize[data->flag]) / obsize[data->flag]; + fac = sqrtf(volume / obsize[data->flag]); /* apply scaling factor to the channels not being kept */ switch (data->flag) { @@ -2024,7 +2024,7 @@ static void pycon_id_looper(bConstraint *con, ConstraintIDFunc func, void *userd } /* Whether this approach is maintained remains to be seen (aligorith) */ -static void pycon_get_tarmat(bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float UNUSED(ctime)) +static void pycon_get_tarmat(const struct EvaluationContext *eval_ctx, bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float UNUSED(ctime)) { #ifdef WITH_PYTHON bPythonConstraint *data = con->data; @@ -2035,7 +2035,7 @@ static void pycon_get_tarmat(bConstraint *con, bConstraintOb *cob, bConstraintTa /* special exception for curves - depsgraph issues */ if (ct->tar->type == OB_CURVE) { if (ct->tar->curve_cache == NULL) { - BKE_displist_make_curveTypes(cob->scene, ct->tar, false); + BKE_displist_make_curveTypes(eval_ctx, cob->scene, ct->tar, false); } } #endif @@ -2142,7 +2142,7 @@ static void actcon_flush_tars(bConstraint *con, ListBase *list, bool no_copy) } } -static void actcon_get_tarmat(bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float UNUSED(ctime)) +static void actcon_get_tarmat(const struct EvaluationContext *UNUSED(eval_ctx), bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float UNUSED(ctime)) { bActionConstraint *data = con->data; @@ -3131,12 +3131,12 @@ static void clampto_flush_tars(bConstraint *con, ListBase *list, bool no_copy) } } -static void clampto_get_tarmat(bConstraint *UNUSED(con), bConstraintOb *cob, bConstraintTarget *ct, float UNUSED(ctime)) +static void clampto_get_tarmat(const struct EvaluationContext *eval_ctx, bConstraint *UNUSED(con), bConstraintOb *cob, bConstraintTarget *ct, float UNUSED(ctime)) { #ifdef CYCLIC_DEPENDENCY_WORKAROUND if (VALID_CONS_TARGET(ct)) { if (ct->tar->curve_cache == NULL) { - BKE_displist_make_curveTypes(cob->scene, ct->tar, false); + BKE_displist_make_curveTypes(eval_ctx, cob->scene, ct->tar, false); } } #endif @@ -3474,7 +3474,7 @@ static void shrinkwrap_flush_tars(bConstraint *con, ListBase *list, bool no_copy } -static void shrinkwrap_get_tarmat(bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float UNUSED(ctime)) +static void shrinkwrap_get_tarmat(const struct EvaluationContext *UNUSED(eval_ctx), bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float UNUSED(ctime)) { bShrinkwrapConstraint *scon = (bShrinkwrapConstraint *) con->data; @@ -3806,12 +3806,12 @@ static void splineik_flush_tars(bConstraint *con, ListBase *list, bool no_copy) } } -static void splineik_get_tarmat(bConstraint *UNUSED(con), bConstraintOb *cob, bConstraintTarget *ct, float UNUSED(ctime)) +static void splineik_get_tarmat(const struct EvaluationContext *eval_ctx, bConstraint *UNUSED(con), bConstraintOb *cob, bConstraintTarget *ct, float UNUSED(ctime)) { #ifdef CYCLIC_DEPENDENCY_WORKAROUND if (VALID_CONS_TARGET(ct)) { if (ct->tar->curve_cache == NULL) { - BKE_displist_make_curveTypes(cob->scene, ct->tar, false); + BKE_displist_make_curveTypes(eval_ctx, cob->scene, ct->tar, false); } } #endif @@ -4737,29 +4737,30 @@ static void con_fix_copied_refs_cb(bConstraint *UNUSED(con), ID **idpoin, bool i } /* duplicate all of the constraints in a constraint stack */ -void BKE_constraints_copy(ListBase *dst, const ListBase *src, bool do_extern) +void BKE_constraints_copy_ex(ListBase *dst, const ListBase *src, const int flag, bool do_extern) { bConstraint *con, *srccon; - + BLI_listbase_clear(dst); BLI_duplicatelist(dst, src); - + for (con = dst->first, srccon = src->first; con && srccon; srccon = srccon->next, con = con->next) { const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(con); - + /* make a new copy of the constraint's data */ con->data = MEM_dupallocN(con->data); - + /* only do specific constraints if required */ if (cti) { /* perform custom copying operations if needed */ if (cti->copy_data) cti->copy_data(con, srccon); - - /* fix usercounts for all referenced data in referenced data */ - if (cti->id_looper) + + /* Fix usercounts for all referenced data that need it. */ + if (cti->id_looper && (flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) { cti->id_looper(con, con_fix_copied_refs_cb, NULL); - + } + /* for proxies we don't want to make extern */ if (do_extern) { /* go over used ID-links for this constraint to ensure that they are valid for proxies */ @@ -4770,6 +4771,11 @@ void BKE_constraints_copy(ListBase *dst, const ListBase *src, bool do_extern) } } +void BKE_constraints_copy(ListBase *dst, const ListBase *src, bool do_extern) +{ + BKE_constraints_copy_ex(dst, src, 0, do_extern); +} + /* ......... */ bConstraint *BKE_constraints_find_name(ListBase *list, const char *name) @@ -4858,7 +4864,7 @@ bool BKE_constraints_proxylocked_owner(Object *ob, bPoseChannel *pchan) * None of the actual calculations of the matrices should be done here! Also, this function is * not to be used by any new constraints, particularly any that have multiple targets. */ -void BKE_constraint_target_matrix_get(Scene *scene, bConstraint *con, int index, short ownertype, void *ownerdata, float mat[4][4], float ctime) +void BKE_constraint_target_matrix_get(const struct EvaluationContext *eval_ctx, Scene *scene, bConstraint *con, int index, short ownertype, void *ownerdata, float mat[4][4], float ctime) { const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(con); ListBase targets = {NULL, NULL}; @@ -4909,7 +4915,7 @@ void BKE_constraint_target_matrix_get(Scene *scene, bConstraint *con, int index, if (ct) { if (cti->get_target_matrix) - cti->get_target_matrix(con, cob, ct, ctime); + cti->get_target_matrix(eval_ctx, con, cob, ct, ctime); copy_m4_m4(mat, ct->matrix); } @@ -4925,7 +4931,7 @@ void BKE_constraint_target_matrix_get(Scene *scene, bConstraint *con, int index, } /* Get the list of targets required for solving a constraint */ -void BKE_constraint_targets_for_solving_get(bConstraint *con, bConstraintOb *cob, ListBase *targets, float ctime) +void BKE_constraint_targets_for_solving_get(const struct EvaluationContext *eval_ctx, bConstraint *con, bConstraintOb *cob, ListBase *targets, float ctime) { const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(con); @@ -4943,7 +4949,7 @@ void BKE_constraint_targets_for_solving_get(bConstraint *con, bConstraintOb *cob */ if (cti->get_target_matrix) { for (ct = targets->first; ct; ct = ct->next) - cti->get_target_matrix(con, cob, ct, ctime); + cti->get_target_matrix(eval_ctx, con, cob, ct, ctime); } else { for (ct = targets->first; ct; ct = ct->next) @@ -4960,7 +4966,7 @@ void BKE_constraint_targets_for_solving_get(bConstraint *con, bConstraintOb *cob * BKE_constraints_make_evalob and BKE_constraints_clear_evalob should be called before and * after running this function, to sort out cob */ -void BKE_constraints_solve(ListBase *conlist, bConstraintOb *cob, float ctime) +void BKE_constraints_solve(const struct EvaluationContext *eval_ctx, ListBase *conlist, bConstraintOb *cob, float ctime) { bConstraint *con; float oldmat[4][4]; @@ -4995,7 +5001,7 @@ void BKE_constraints_solve(ListBase *conlist, bConstraintOb *cob, float ctime) BKE_constraint_mat_convertspace(cob->ob, cob->pchan, cob->matrix, CONSTRAINT_SPACE_WORLD, con->ownspace, false); /* prepare targets for constraint solving */ - BKE_constraint_targets_for_solving_get(con, cob, &targets, ctime); + BKE_constraint_targets_for_solving_get(eval_ctx, con, cob, &targets, ctime); /* Solve the constraint and put result in cob->matrix */ cti->evaluate_constraint(con, cob, &targets); diff --git a/source/blender/blenkernel/intern/context.c b/source/blender/blenkernel/intern/context.c index ac214a70b75..4f1dd18b39c 100644 --- a/source/blender/blenkernel/intern/context.c +++ b/source/blender/blenkernel/intern/context.c @@ -52,6 +52,7 @@ #include "BKE_context.h" #include "BKE_layer.h" #include "BKE_main.h" +#include "BKE_scene.h" #include "BKE_screen.h" #include "BKE_sound.h" #include "BKE_workspace.h" @@ -76,6 +77,7 @@ struct bContext { struct ScrArea *area; struct ARegion *region; struct ARegion *menu; + struct wmManipulatorGroup *manipulator_group; struct bContextStore *store; const char *operator_poll_msg; /* reason for poll failing */ } wm; @@ -670,6 +672,11 @@ struct ARegion *CTX_wm_menu(const bContext *C) return C->wm.menu; } +struct wmManipulatorGroup *CTX_wm_manipulator_group(const bContext *C) +{ + return C->wm.manipulator_group; +} + struct ReportList *CTX_wm_reports(const bContext *C) { if (C->wm.manager) @@ -869,6 +876,11 @@ void CTX_wm_menu_set(bContext *C, ARegion *menu) C->wm.menu = menu; } +void CTX_wm_manipulator_group_set(bContext *C, struct wmManipulatorGroup *mgroup) +{ + C->wm.manipulator_group = mgroup; +} + void CTX_wm_operator_poll_msg_set(bContext *C, const char *msg) { C->wm.operator_poll_msg = msg; @@ -915,7 +927,7 @@ SceneLayer *CTX_data_scene_layer(const bContext *C) return sl; } else { - return BKE_scene_layer_context_active(CTX_data_scene(C)); + return BKE_scene_layer_from_workspace_get(CTX_wm_workspace(C)); } } @@ -1225,5 +1237,17 @@ int CTX_data_editable_gpencil_strokes(const bContext *C, ListBase *list) Depsgraph *CTX_data_depsgraph(const bContext *C) { Scene *scene = CTX_data_scene(C); - return scene->depsgraph; + SceneLayer *scene_layer = CTX_data_scene_layer(C); + return BKE_scene_get_depsgraph(scene, scene_layer); +} + +void CTX_data_eval_ctx(const bContext *C, EvaluationContext *eval_ctx) +{ + BLI_assert(C != NULL); + + Scene *scene = CTX_data_scene(C); + SceneLayer *scene_layer = CTX_data_scene_layer(C); + DEG_evaluation_context_init_from_scene(eval_ctx, + scene, scene_layer, + DAG_EVAL_VIEWPORT); } diff --git a/source/blender/blenkernel/intern/crazyspace.c b/source/blender/blenkernel/intern/crazyspace.c index 56df8e51eba..d2ffbbcf27b 100644 --- a/source/blender/blenkernel/intern/crazyspace.c +++ b/source/blender/blenkernel/intern/crazyspace.c @@ -99,7 +99,8 @@ static int modifiers_disable_subsurf_temporary(Object *ob) } /* disable subsurf temporal, get mapped cos, and enable it */ -float (*BKE_crazyspace_get_mapped_editverts(Scene *scene, Object *obedit))[3] +float (*BKE_crazyspace_get_mapped_editverts( + const struct EvaluationContext *eval_ctx, Scene *scene, Object *obedit))[3] { Mesh *me = obedit->data; DerivedMesh *dm; @@ -109,13 +110,13 @@ float (*BKE_crazyspace_get_mapped_editverts(Scene *scene, Object *obedit))[3] /* disable subsurf temporal, get mapped cos, and enable it */ if (modifiers_disable_subsurf_temporary(obedit)) { /* need to make new derivemesh */ - makeDerivedMesh(scene, obedit, me->edit_btmesh, CD_MASK_BAREMESH, false); + makeDerivedMesh(eval_ctx, scene, obedit, me->edit_btmesh, CD_MASK_BAREMESH, false); } /* now get the cage */ vertexcos = MEM_mallocN(sizeof(*vertexcos) * nverts, "vertexcos map"); - dm = editbmesh_get_derived_cage(scene, obedit, me->edit_btmesh, CD_MASK_BAREMESH); + dm = editbmesh_get_derived_cage(eval_ctx, scene, obedit, me->edit_btmesh, CD_MASK_BAREMESH); mesh_get_mapped_verts_coords(dm, vertexcos, nverts); @@ -250,8 +251,9 @@ void BKE_crazyspace_set_quats_mesh(Mesh *me, float (*origcos)[3], float (*mapped /** returns an array of deform matrices for crazyspace correction, and the * number of modifiers left */ -int BKE_crazyspace_get_first_deform_matrices_editbmesh(Scene *scene, Object *ob, BMEditMesh *em, - float (**deformmats)[3][3], float (**deformcos)[3]) +int BKE_crazyspace_get_first_deform_matrices_editbmesh( + const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, BMEditMesh *em, + float (**deformmats)[3][3], float (**deformcos)[3]) { ModifierData *md; DerivedMesh *dm; @@ -290,7 +292,7 @@ int BKE_crazyspace_get_first_deform_matrices_editbmesh(Scene *scene, Object *ob, unit_m3(defmats[a]); } - mti->deformMatricesEM(md, ob, em, dm, deformedVerts, defmats, + mti->deformMatricesEM(md, eval_ctx, ob, em, dm, deformedVerts, defmats, numVerts); } else @@ -310,7 +312,9 @@ int BKE_crazyspace_get_first_deform_matrices_editbmesh(Scene *scene, Object *ob, return numleft; } -int BKE_sculpt_get_first_deform_matrices(Scene *scene, Object *ob, float (**deformmats)[3][3], float (**deformcos)[3]) +int BKE_sculpt_get_first_deform_matrices( + const struct EvaluationContext *eval_ctx, Scene *scene, + Object *ob, float (**deformmats)[3][3], float (**deformcos)[3]) { ModifierData *md; DerivedMesh *dm; @@ -346,7 +350,7 @@ int BKE_sculpt_get_first_deform_matrices(Scene *scene, Object *ob, float (**defo unit_m3(defmats[a]); } - if (mti->deformMatrices) mti->deformMatrices(md, ob, dm, deformedVerts, defmats, numVerts); + if (mti->deformMatrices) mti->deformMatrices(md, eval_ctx, ob, dm, deformedVerts, defmats, numVerts); else break; } } @@ -369,9 +373,9 @@ int BKE_sculpt_get_first_deform_matrices(Scene *scene, Object *ob, float (**defo return numleft; } -void BKE_crazyspace_build_sculpt(Scene *scene, Object *ob, float (**deformmats)[3][3], float (**deformcos)[3]) +void BKE_crazyspace_build_sculpt(const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, float (**deformmats)[3][3], float (**deformcos)[3]) { - int totleft = BKE_sculpt_get_first_deform_matrices(scene, ob, deformmats, deformcos); + int totleft = BKE_sculpt_get_first_deform_matrices(eval_ctx, scene, ob, deformmats, deformcos); if (totleft) { /* there are deformation modifier which doesn't support deformation matrices @@ -396,7 +400,7 @@ void BKE_crazyspace_build_sculpt(Scene *scene, Object *ob, float (**deformmats)[ if (mti->deformMatrices && !deformed) continue; - mti->deformVerts(md, ob, NULL, deformedVerts, me->totvert, 0); + mti->deformVerts(md, eval_ctx, ob, NULL, deformedVerts, me->totvert, 0); deformed = 1; } } diff --git a/source/blender/blenkernel/intern/curve.c b/source/blender/blenkernel/intern/curve.c index 8a6d9f57093..5fbaae7212b 100644 --- a/source/blender/blenkernel/intern/curve.c +++ b/source/blender/blenkernel/intern/curve.c @@ -182,7 +182,7 @@ Curve *BKE_curve_add(Main *bmain, const char *name, int type) { Curve *cu; - cu = BKE_libblock_alloc(bmain, ID_CU, name); + cu = BKE_libblock_alloc(bmain, ID_CU, name, 0); cu->type = type; BKE_curve_init(cu); @@ -190,43 +190,40 @@ Curve *BKE_curve_add(Main *bmain, const char *name, int type) return cu; } -Curve *BKE_curve_copy(Main *bmain, const Curve *cu) +/** + * Only copy internal data of Curve ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_curve_copy_data(Main *bmain, Curve *cu_dst, const Curve *cu_src, const int flag) { - Curve *cun; - int a; + BLI_listbase_clear(&cu_dst->nurb); + BKE_nurbList_duplicate(&(cu_dst->nurb), &(cu_src->nurb)); - cun = BKE_libblock_copy(bmain, &cu->id); + cu_dst->mat = MEM_dupallocN(cu_src->mat); - BLI_listbase_clear(&cun->nurb); - BKE_nurbList_duplicate(&(cun->nurb), &(cu->nurb)); + cu_dst->str = MEM_dupallocN(cu_src->str); + cu_dst->strinfo = MEM_dupallocN(cu_src->strinfo); + cu_dst->tb = MEM_dupallocN(cu_src->tb); + cu_dst->bb = MEM_dupallocN(cu_src->bb); + cu_dst->batch_cache = NULL; - cun->mat = MEM_dupallocN(cu->mat); - for (a = 0; a < cun->totcol; a++) { - id_us_plus((ID *)cun->mat[a]); + if (cu_src->key) { + BKE_id_copy_ex(bmain, &cu_src->key->id, (ID **)&cu_dst->key, flag, false); } - cun->str = MEM_dupallocN(cu->str); - cun->strinfo = MEM_dupallocN(cu->strinfo); - cun->tb = MEM_dupallocN(cu->tb); - cun->bb = MEM_dupallocN(cu->bb); - cun->batch_cache = NULL; - - if (cu->key) { - cun->key = BKE_key_copy(bmain, cu->key); - cun->key->from = (ID *)cun; - } - - cun->editnurb = NULL; - cun->editfont = NULL; - - id_us_plus((ID *)cun->vfont); - id_us_plus((ID *)cun->vfontb); - id_us_plus((ID *)cun->vfonti); - id_us_plus((ID *)cun->vfontbi); - - BKE_id_copy_ensure_local(bmain, &cu->id, &cun->id); + cu_dst->editnurb = NULL; + cu_dst->editfont = NULL; +} - return cun; +Curve *BKE_curve_copy(Main *bmain, const Curve *cu) +{ + Curve *cu_copy; + BKE_id_copy_ex(bmain, &cu->id, (ID **)&cu_copy, 0, false); + return cu_copy; } void BKE_curve_make_local(Main *bmain, Curve *cu, const bool lib_local) @@ -748,6 +745,7 @@ BezTriple *BKE_nurb_bezt_get_prev(Nurb *nu, BezTriple *bezt) BezTriple *bezt_prev; BLI_assert(ARRAY_HAS_ITEM(bezt, nu->bezt, nu->pntsu)); + BLI_assert(nu->pntsv == 1); if (bezt == nu->bezt) { if (nu->flagu & CU_NURB_CYCLIC) { @@ -769,6 +767,7 @@ BPoint *BKE_nurb_bpoint_get_prev(Nurb *nu, BPoint *bp) BPoint *bp_prev; BLI_assert(ARRAY_HAS_ITEM(bp, nu->bp, nu->pntsu)); + BLI_assert(nu->pntsv == 1); if (bp == nu->bp) { if (nu->flagu & CU_NURB_CYCLIC) { @@ -785,7 +784,7 @@ BPoint *BKE_nurb_bpoint_get_prev(Nurb *nu, BPoint *bp) return bp_prev; } -void BKE_nurb_bezt_calc_normal(struct Nurb *UNUSED(nu), struct BezTriple *bezt, float r_normal[3]) +void BKE_nurb_bezt_calc_normal(struct Nurb *UNUSED(nu), BezTriple *bezt, float r_normal[3]) { /* calculate the axis matrix from the spline */ float dir_prev[3], dir_next[3]; @@ -800,7 +799,7 @@ void BKE_nurb_bezt_calc_normal(struct Nurb *UNUSED(nu), struct BezTriple *bezt, normalize_v3(r_normal); } -void BKE_nurb_bezt_calc_plane(struct Nurb *nu, struct BezTriple *bezt, float r_plane[3]) +void BKE_nurb_bezt_calc_plane(struct Nurb *nu, BezTriple *bezt, float r_plane[3]) { float dir_prev[3], dir_next[3]; @@ -837,7 +836,7 @@ void BKE_nurb_bezt_calc_plane(struct Nurb *nu, struct BezTriple *bezt, float r_p normalize_v3(r_plane); } -void BKE_nurb_bpoint_calc_normal(struct Nurb *nu, struct BPoint *bp, float r_normal[3]) +void BKE_nurb_bpoint_calc_normal(struct Nurb *nu, BPoint *bp, float r_normal[3]) { BPoint *bp_prev = BKE_nurb_bpoint_get_prev(nu, bp); BPoint *bp_next = BKE_nurb_bpoint_get_next(nu, bp); @@ -860,6 +859,34 @@ void BKE_nurb_bpoint_calc_normal(struct Nurb *nu, struct BPoint *bp, float r_nor normalize_v3(r_normal); } +void BKE_nurb_bpoint_calc_plane(struct Nurb *nu, BPoint *bp, float r_plane[3]) +{ + BPoint *bp_prev = BKE_nurb_bpoint_get_prev(nu, bp); + BPoint *bp_next = BKE_nurb_bpoint_get_next(nu, bp); + + float dir_prev[3] = {0.0f}, dir_next[3] = {0.0f}; + + if (bp_prev) { + sub_v3_v3v3(dir_prev, bp_prev->vec, bp->vec); + normalize_v3(dir_prev); + } + if (bp_next) { + sub_v3_v3v3(dir_next, bp->vec, bp_next->vec); + normalize_v3(dir_next); + } + cross_v3_v3v3(r_plane, dir_prev, dir_next); + + /* matches with bones more closely */ + { + float dir_mid[3], tvec[3]; + add_v3_v3v3(dir_mid, dir_prev, dir_next); + cross_v3_v3v3(tvec, r_plane, dir_mid); + copy_v3_v3(r_plane, tvec); + } + + normalize_v3(r_plane); +} + /* ~~~~~~~~~~~~~~~~~~~~Non Uniform Rational B Spline calculations ~~~~~~~~~~~ */ @@ -1595,7 +1622,7 @@ float *BKE_curve_surf_make_orco(Object *ob) /* NOTE: This routine is tied to the order of vertex * built by displist and as passed to the renderer. */ -float *BKE_curve_make_orco(Scene *scene, Object *ob, int *r_numVerts) +float *BKE_curve_make_orco(const EvaluationContext *eval_ctx, Scene *scene, Object *ob, int *r_numVerts) { Curve *cu = ob->data; DispList *dl; @@ -1603,7 +1630,7 @@ float *BKE_curve_make_orco(Scene *scene, Object *ob, int *r_numVerts) float *fp, *coord_array; ListBase disp = {NULL, NULL}; - BKE_displist_make_curveTypes_forOrco(scene, ob, &disp); + BKE_displist_make_curveTypes_forOrco(eval_ctx, scene, ob, &disp); numVerts = 0; for (dl = disp.first; dl; dl = dl->next) { @@ -1694,8 +1721,9 @@ float *BKE_curve_make_orco(Scene *scene, Object *ob, int *r_numVerts) /* ***************** BEVEL ****************** */ -void BKE_curve_bevel_make(Scene *scene, Object *ob, ListBase *disp, - const bool for_render, const bool use_render_resolution) +void BKE_curve_bevel_make( + const EvaluationContext *eval_ctx, Scene *scene, Object *ob, ListBase *disp, + const bool for_render, const bool use_render_resolution) { DispList *dl, *dlnew; Curve *bevcu, *cu; @@ -1719,7 +1747,7 @@ void BKE_curve_bevel_make(Scene *scene, Object *ob, ListBase *disp, facy = cu->bevobj->size[1]; if (for_render) { - BKE_displist_make_curveTypes_forRender(scene, cu->bevobj, &bevdisp, NULL, false, use_render_resolution); + BKE_displist_make_curveTypes_forRender(eval_ctx, scene, cu->bevobj, &bevdisp, NULL, false, use_render_resolution); dl = bevdisp.first; } else if (cu->bevobj->curve_cache) { @@ -4016,7 +4044,7 @@ bool BKE_nurb_check_valid_u(struct Nurb *nu) return true; /* not a nurb, lets assume its valid */ if (nu->pntsu < nu->orderu) return false; - if (((nu->flag & CU_NURB_CYCLIC) == 0) && (nu->flagu & CU_NURB_BEZIER)) { /* Bezier U Endpoints */ + if (((nu->flagu & CU_NURB_CYCLIC) == 0) && (nu->flagu & CU_NURB_BEZIER)) { /* Bezier U Endpoints */ if (nu->orderu == 4) { if (nu->pntsu < 5) return false; /* bezier with 4 orderu needs 5 points */ @@ -4037,7 +4065,7 @@ bool BKE_nurb_check_valid_v(struct Nurb *nu) if (nu->pntsv < nu->orderv) return false; - if (((nu->flag & CU_NURB_CYCLIC) == 0) && (nu->flagv & CU_NURB_BEZIER)) { /* Bezier V Endpoints */ + if (((nu->flagv & CU_NURB_CYCLIC) == 0) && (nu->flagv & CU_NURB_BEZIER)) { /* Bezier V Endpoints */ if (nu->orderv == 4) { if (nu->pntsv < 5) return false; /* bezier with 4 orderu needs 5 points */ @@ -4419,7 +4447,9 @@ bool BKE_curve_center_bounds(Curve *cu, float cent[3]) } -void BKE_curve_transform_ex(Curve *cu, float mat[4][4], const bool do_keys, const float unit_scale) +void BKE_curve_transform_ex( + Curve *cu, float mat[4][4], + const bool do_keys, const bool do_props, const float unit_scale) { Nurb *nu; BPoint *bp; @@ -4433,7 +4463,9 @@ void BKE_curve_transform_ex(Curve *cu, float mat[4][4], const bool do_keys, cons mul_m4_v3(mat, bezt->vec[0]); mul_m4_v3(mat, bezt->vec[1]); mul_m4_v3(mat, bezt->vec[2]); - bezt->radius *= unit_scale; + if (do_props) { + bezt->radius *= unit_scale; + } } BKE_nurb_handles_calc(nu); } @@ -4441,7 +4473,9 @@ void BKE_curve_transform_ex(Curve *cu, float mat[4][4], const bool do_keys, cons i = nu->pntsu * nu->pntsv; for (bp = nu->bp; i--; bp++) { mul_m4_v3(mat, bp->vec); - bp->radius *= unit_scale; + if (do_props) { + bp->radius *= unit_scale; + } } } } @@ -4457,10 +4491,12 @@ void BKE_curve_transform_ex(Curve *cu, float mat[4][4], const bool do_keys, cons } } -void BKE_curve_transform(Curve *cu, float mat[4][4], const bool do_keys) +void BKE_curve_transform( + Curve *cu, float mat[4][4], + const bool do_keys, const bool do_props) { float unit_scale = mat4_to_scale(mat); - BKE_curve_transform_ex(cu, mat, do_keys, unit_scale); + BKE_curve_transform_ex(cu, mat, do_keys, do_props, unit_scale); } void BKE_curve_translate(Curve *cu, float offset[3], const bool do_keys) @@ -4639,7 +4675,7 @@ void BKE_curve_rect_from_textbox(const struct Curve *cu, const struct TextBox *t /* **** Depsgraph evaluation **** */ -void BKE_curve_eval_geometry(EvaluationContext *UNUSED(eval_ctx), +void BKE_curve_eval_geometry(const EvaluationContext *UNUSED(eval_ctx), Curve *curve) { if (G.debug & G_DEBUG_DEPSGRAPH) { @@ -4650,17 +4686,6 @@ void BKE_curve_eval_geometry(EvaluationContext *UNUSED(eval_ctx), } } -void BKE_curve_eval_path(EvaluationContext *UNUSED(eval_ctx), - Curve *curve) -{ - /* TODO(sergey): This will probably need to be a part of - * the modifier stack still. - */ - if (G.debug & G_DEBUG_DEPSGRAPH) { - printf("%s on %s\n", __func__, curve->id.name); - } -} - /* Draw Engine */ void (*BKE_curve_batch_cache_dirty_cb)(Curve *cu, int mode) = NULL; void (*BKE_curve_batch_cache_free_cb)(Curve *cu) = NULL; @@ -4676,4 +4701,4 @@ void BKE_curve_batch_cache_free(Curve *cu) if (cu->batch_cache) { BKE_curve_batch_cache_free_cb(cu); } -}
\ No newline at end of file +} diff --git a/source/blender/blenkernel/intern/customdata.c b/source/blender/blenkernel/intern/customdata.c index 3ccf619fd11..3bf5784e674 100644 --- a/source/blender/blenkernel/intern/customdata.c +++ b/source/blender/blenkernel/intern/customdata.c @@ -810,10 +810,10 @@ static void layerInterp_mloopcol( * although weights should also not cause this situation */ /* also delay writing to the destination incase dest is in sources */ - mc->r = CLAMPIS(iroundf(col.r), 0, 255); - mc->g = CLAMPIS(iroundf(col.g), 0, 255); - mc->b = CLAMPIS(iroundf(col.b), 0, 255); - mc->a = CLAMPIS(iroundf(col.a), 0, 255); + mc->r = round_fl_to_uchar_clamp(col.r); + mc->g = round_fl_to_uchar_clamp(col.g); + mc->b = round_fl_to_uchar_clamp(col.b); + mc->a = round_fl_to_uchar_clamp(col.a); } static int layerMaxNum_mloopcol(void) @@ -1036,10 +1036,10 @@ static void layerInterp_mcol( /* Subdivide smooth or fractal can cause problems without clamping * although weights should also not cause this situation */ - mc[j].a = CLAMPIS(iroundf(col[j].a), 0, 255); - mc[j].r = CLAMPIS(iroundf(col[j].r), 0, 255); - mc[j].g = CLAMPIS(iroundf(col[j].g), 0, 255); - mc[j].b = CLAMPIS(iroundf(col[j].b), 0, 255); + mc[j].a = round_fl_to_uchar_clamp(col[j].a); + mc[j].r = round_fl_to_uchar_clamp(col[j].r); + mc[j].g = round_fl_to_uchar_clamp(col[j].g); + mc[j].b = round_fl_to_uchar_clamp(col[j].b); } } @@ -1344,7 +1344,7 @@ const CustomDataMask CD_MASK_BMESH = CD_MASK_PROP_STR | CD_MASK_SHAPEKEY | CD_MASK_SHAPE_KEYINDEX | CD_MASK_MDISPS | CD_MASK_CREASE | CD_MASK_BWEIGHT | CD_MASK_RECAST | CD_MASK_PAINT_MASK | CD_MASK_GRID_PAINT_MASK | CD_MASK_MVERT_SKIN | CD_MASK_FREESTYLE_EDGE | CD_MASK_FREESTYLE_FACE | - CD_MASK_CUSTOMLOOPNORMAL | CD_MASK_FACEMAP; + CD_MASK_CUSTOMLOOPNORMAL | CD_MASK_FACEMAP; /** * cover values copied by #BKE_mesh_loops_to_tessdata */ diff --git a/source/blender/blenkernel/intern/data_transfer.c b/source/blender/blenkernel/intern/data_transfer.c index a83ec8f0486..00b8063110d 100644 --- a/source/blender/blenkernel/intern/data_transfer.c +++ b/source/blender/blenkernel/intern/data_transfer.c @@ -1010,7 +1010,8 @@ static bool data_transfer_layersmapping_generate( * to get (as much as possible) exact copy of source data layout. */ void BKE_object_data_transfer_layout( - Scene *scene, Object *ob_src, Object *ob_dst, const int data_types, const bool use_delete, + const struct EvaluationContext *eval_ctx, Scene *scene, + Object *ob_src, Object *ob_dst, const int data_types, const bool use_delete, const int fromlayers_select[DT_MULTILAYER_INDEX_MAX], const int tolayers_select[DT_MULTILAYER_INDEX_MAX]) { DerivedMesh *dm_src; @@ -1027,7 +1028,7 @@ void BKE_object_data_transfer_layout( /* Get source DM.*/ dm_src_mask |= BKE_object_data_transfer_dttypes_to_cdmask(data_types); - dm_src = mesh_get_derived_final(scene, ob_src, dm_src_mask); + dm_src = mesh_get_derived_final(eval_ctx, scene, ob_src, dm_src_mask); if (!dm_src) { return; } @@ -1085,9 +1086,9 @@ void BKE_object_data_transfer_layout( } bool BKE_object_data_transfer_dm( - Scene *scene, Object *ob_src, Object *ob_dst, DerivedMesh *dm_dst, const int data_types, bool use_create, - const int map_vert_mode, const int map_edge_mode, const int map_loop_mode, const int map_poly_mode, - SpaceTransform *space_transform, const bool auto_transform, + const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob_src, Object *ob_dst, DerivedMesh *dm_dst, + const int data_types, bool use_create, const int map_vert_mode, const int map_edge_mode, + const int map_loop_mode, const int map_poly_mode, SpaceTransform *space_transform, const bool auto_transform, const float max_distance, const float ray_radius, const float islands_handling_precision, const int fromlayers_select[DT_MULTILAYER_INDEX_MAX], const int tolayers_select[DT_MULTILAYER_INDEX_MAX], const int mix_mode, const float mix_factor, const char *vgroup_name, const bool invert_vgroup, @@ -1149,7 +1150,7 @@ bool BKE_object_data_transfer_dm( * Also, we need to make a local copy of dm_src, otherwise we may end with concurrent creation * of data in it (multi-threaded evaluation of the modifier stack, see T46672). */ - dm_src = dm_dst ? ob_src->derivedFinal : mesh_get_derived_final(scene, ob_src, dm_src_mask); + dm_src = dm_dst ? ob_src->derivedFinal : mesh_get_derived_final(eval_ctx, scene, ob_src, dm_src_mask); if (!dm_src) { return changed; } @@ -1457,16 +1458,16 @@ bool BKE_object_data_transfer_dm( } bool BKE_object_data_transfer_mesh( - Scene *scene, Object *ob_src, Object *ob_dst, const int data_types, const bool use_create, - const int map_vert_mode, const int map_edge_mode, const int map_loop_mode, const int map_poly_mode, - SpaceTransform *space_transform, const bool auto_transform, + const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob_src, Object *ob_dst, const int data_types, + const bool use_create, const int map_vert_mode, const int map_edge_mode, const int map_loop_mode, + const int map_poly_mode, SpaceTransform *space_transform, const bool auto_transform, const float max_distance, const float ray_radius, const float islands_handling_precision, const int fromlayers_select[DT_MULTILAYER_INDEX_MAX], const int tolayers_select[DT_MULTILAYER_INDEX_MAX], const int mix_mode, const float mix_factor, const char *vgroup_name, const bool invert_vgroup, ReportList *reports) { return BKE_object_data_transfer_dm( - scene, ob_src, ob_dst, NULL, data_types, use_create, + eval_ctx, scene, ob_src, ob_dst, NULL, data_types, use_create, map_vert_mode, map_edge_mode, map_loop_mode, map_poly_mode, space_transform, auto_transform, max_distance, ray_radius, islands_handling_precision, diff --git a/source/blender/blenkernel/intern/deform.c b/source/blender/blenkernel/intern/deform.c index 9f0024c01fd..1fc83b69bfe 100644 --- a/source/blender/blenkernel/intern/deform.c +++ b/source/blender/blenkernel/intern/deform.c @@ -74,7 +74,7 @@ bDeformGroup *BKE_defgroup_new(Object *ob, const char *name) BLI_addtail(&ob->defbase, defgroup); defgroup_unique_name(defgroup, ob); - BKE_mesh_batch_cache_dirty(ob->data, BKE_MESH_BATCH_DIRTY_PAINT); + BKE_mesh_batch_cache_dirty(ob->data, BKE_MESH_BATCH_DIRTY_ALL); return defgroup; } @@ -624,8 +624,17 @@ float defvert_find_weight(const struct MDeformVert *dvert, const int defgroup) */ float defvert_array_find_weight_safe(const struct MDeformVert *dvert, const int index, const int defgroup) { - if (defgroup == -1 || dvert == NULL) + /* Invalid defgroup index means the vgroup selected is invalid, does not exist, in that case it is OK to return 1.0 + * (i.e. maximum weight, as if no vgroup was selected). + * But in case of valid defgroup and NULL dvert data pointer, it means that vgroup **is** valid, + * and just totally empty, so we shall return '0.0' value then! + */ + if (defgroup == -1) { return 1.0f; + } + else if (dvert == NULL) { + return 0.0f; + } return defvert_find_weight(dvert + index, defgroup); } diff --git a/source/blender/blenkernel/intern/displist.c b/source/blender/blenkernel/intern/displist.c index 44f284d043e..8316d68b35c 100644 --- a/source/blender/blenkernel/intern/displist.c +++ b/source/blender/blenkernel/intern/displist.c @@ -678,7 +678,7 @@ static void curve_to_filledpoly(Curve *cu, ListBase *UNUSED(nurb), ListBase *dis * - first point left, last point right * - based on subdivided points in original curve, not on points in taper curve (still) */ -static float displist_calc_taper(Scene *scene, Object *taperobj, float fac) +static float displist_calc_taper(const EvaluationContext *eval_ctx, Scene *scene, Object *taperobj, float fac) { DispList *dl; @@ -687,7 +687,7 @@ static float displist_calc_taper(Scene *scene, Object *taperobj, float fac) dl = taperobj->curve_cache ? taperobj->curve_cache->disp.first : NULL; if (dl == NULL) { - BKE_displist_make_curveTypes(scene, taperobj, 0); + BKE_displist_make_curveTypes(eval_ctx, scene, taperobj, 0); dl = taperobj->curve_cache->disp.first; } if (dl) { @@ -718,14 +718,14 @@ static float displist_calc_taper(Scene *scene, Object *taperobj, float fac) return 1.0; } -float BKE_displist_calc_taper(Scene *scene, Object *taperobj, int cur, int tot) +float BKE_displist_calc_taper(const EvaluationContext *eval_ctx, Scene *scene, Object *taperobj, int cur, int tot) { float fac = ((float)cur) / (float)(tot - 1); - return displist_calc_taper(scene, taperobj, fac); + return displist_calc_taper(eval_ctx, scene, taperobj, fac); } -void BKE_displist_make_mball(EvaluationContext *eval_ctx, Scene *scene, Object *ob) +void BKE_displist_make_mball(const EvaluationContext *eval_ctx, Scene *scene, Object *ob) { if (!ob || ob->type != OB_MBALL) return; @@ -748,7 +748,7 @@ void BKE_displist_make_mball(EvaluationContext *eval_ctx, Scene *scene, Object * } } -void BKE_displist_make_mball_forRender(EvaluationContext *eval_ctx, Scene *scene, Object *ob, ListBase *dispbase) +void BKE_displist_make_mball_forRender(const EvaluationContext *eval_ctx, Scene *scene, Object *ob, ListBase *dispbase) { BKE_mball_polygonize(eval_ctx, scene, ob, dispbase); BKE_mball_texspace_calc(ob); @@ -798,8 +798,9 @@ static ModifierData *curve_get_tessellate_point(Scene *scene, Object *ob, return pretessellatePoint; } -static void curve_calc_modifiers_pre(Scene *scene, Object *ob, ListBase *nurb, - const bool for_render, const bool use_render_resolution) +static void curve_calc_modifiers_pre( + const EvaluationContext *eval_ctx, Scene *scene, Object *ob, ListBase *nurb, + const bool for_render, const bool use_render_resolution) { VirtualModifierData virtualModifierData; ModifierData *md = modifiers_getVirtualModifierList(ob, &virtualModifierData); @@ -856,7 +857,7 @@ static void curve_calc_modifiers_pre(Scene *scene, Object *ob, ListBase *nurb, deformedVerts = BKE_curve_nurbs_vertexCos_get(nurb, &numVerts); } - mti->deformVerts(md, ob, NULL, deformedVerts, numVerts, app_flag); + mti->deformVerts(md, eval_ctx, ob, NULL, deformedVerts, numVerts, app_flag); if (md == pretessellatePoint) break; @@ -908,9 +909,10 @@ static void displist_apply_allverts(ListBase *dispbase, float (*allverts)[3]) } } -static void curve_calc_modifiers_post(Scene *scene, Object *ob, ListBase *nurb, - ListBase *dispbase, DerivedMesh **r_dm_final, - const bool for_render, const bool use_render_resolution) +static void curve_calc_modifiers_post( + const EvaluationContext *eval_ctx, Scene *scene, Object *ob, ListBase *nurb, + ListBase *dispbase, DerivedMesh **r_dm_final, + const bool for_render, const bool use_render_resolution) { VirtualModifierData virtualModifierData; ModifierData *md = modifiers_getVirtualModifierList(ob, &virtualModifierData); @@ -964,14 +966,14 @@ static void curve_calc_modifiers_post(Scene *scene, Object *ob, ListBase *nurb, dm->getVertCos(dm, vertCos); } - mti->deformVerts(md, ob, dm, vertCos, totvert, appf); + mti->deformVerts(md, eval_ctx, ob, dm, vertCos, totvert, appf); } else { if (!vertCos) { vertCos = displist_get_allverts(dispbase, &totvert); } - mti->deformVerts(md, ob, NULL, vertCos, totvert, appf); + mti->deformVerts(md, eval_ctx, ob, NULL, vertCos, totvert, appf); } } else { @@ -1013,7 +1015,7 @@ static void curve_calc_modifiers_post(Scene *scene, Object *ob, ListBase *nurb, if (useCache) appf |= MOD_APPLY_USECACHE; - ndm = modwrap_applyModifier(md, ob, dm, appf); + ndm = modwrap_applyModifier(md, eval_ctx, ob, dm, appf); if (ndm) { /* Modifier returned a new derived mesh */ @@ -1090,13 +1092,13 @@ static void displist_surf_indices(DispList *dl) } } -static DerivedMesh *create_orco_dm(Scene *scene, Object *ob) +static DerivedMesh *create_orco_dm(const EvaluationContext *eval_ctx, Scene *scene, Object *ob) { DerivedMesh *dm; ListBase disp = {NULL, NULL}; /* OrcoDM should be created from underformed disp lists */ - BKE_displist_make_curveTypes_forOrco(scene, ob, &disp); + BKE_displist_make_curveTypes_forOrco(eval_ctx, scene, ob, &disp); dm = CDDM_from_curve_displist(ob, &disp); BKE_displist_free(&disp); @@ -1134,8 +1136,9 @@ static void add_orco_dm(Object *ob, DerivedMesh *dm, DerivedMesh *orcodm) DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, orco); } -static void curve_calc_orcodm(Scene *scene, Object *ob, DerivedMesh *dm_final, - const bool for_render, const bool use_render_resolution) +static void curve_calc_orcodm( + const EvaluationContext *eval_ctx, Scene *scene, Object *ob, DerivedMesh *dm_final, + const bool for_render, const bool use_render_resolution) { /* this function represents logic of mesh's orcodm calculation * for displist-based objects @@ -1172,7 +1175,7 @@ static void curve_calc_orcodm(Scene *scene, Object *ob, DerivedMesh *dm_final, * This means we can create ORCO DM in advance and assume it's * never NULL. */ - orcodm = create_orco_dm(scene, ob); + orcodm = create_orco_dm(eval_ctx, scene, ob); for (; md; md = md->next) { const ModifierTypeInfo *mti = modifierType_getInfo(md->type); @@ -1184,7 +1187,7 @@ static void curve_calc_orcodm(Scene *scene, Object *ob, DerivedMesh *dm_final, if (mti->type != eModifierTypeType_Constructive) continue; - ndm = modwrap_applyModifier(md, ob, orcodm, app_flag); + ndm = modwrap_applyModifier(md, eval_ctx, ob, orcodm, app_flag); if (ndm) { /* if the modifier returned a new dm, release the old one */ @@ -1201,9 +1204,10 @@ static void curve_calc_orcodm(Scene *scene, Object *ob, DerivedMesh *dm_final, orcodm->release(orcodm); } -void BKE_displist_make_surf(Scene *scene, Object *ob, ListBase *dispbase, - DerivedMesh **r_dm_final, - const bool for_render, const bool for_orco, const bool use_render_resolution) +void BKE_displist_make_surf( + const EvaluationContext *eval_ctx, Scene *scene, Object *ob, ListBase *dispbase, + DerivedMesh **r_dm_final, + const bool for_render, const bool for_orco, const bool use_render_resolution) { ListBase nubase = {NULL, NULL}; Nurb *nu; @@ -1220,7 +1224,7 @@ void BKE_displist_make_surf(Scene *scene, Object *ob, ListBase *dispbase, } if (!for_orco) - curve_calc_modifiers_pre(scene, ob, &nubase, for_render, use_render_resolution); + curve_calc_modifiers_pre(eval_ctx, scene, ob, &nubase, for_render, use_render_resolution); for (nu = nubase.first; nu; nu = nu->next) { if ((for_render || nu->hide == 0) && BKE_nurb_check_valid_uv(nu)) { @@ -1287,7 +1291,7 @@ void BKE_displist_make_surf(Scene *scene, Object *ob, ListBase *dispbase, if (!for_orco) { BKE_nurbList_duplicate(&ob->curve_cache->deformed_nurbs, &nubase); - curve_calc_modifiers_post(scene, ob, &nubase, dispbase, r_dm_final, + curve_calc_modifiers_post(eval_ctx, scene, ob, &nubase, dispbase, r_dm_final, for_render, use_render_resolution); } @@ -1513,9 +1517,10 @@ static void calc_bevfac_mapping(Curve *cu, BevList *bl, Nurb *nu, } } -static void do_makeDispListCurveTypes(Scene *scene, Object *ob, ListBase *dispbase, - DerivedMesh **r_dm_final, - const bool for_render, const bool for_orco, const bool use_render_resolution) +static void do_makeDispListCurveTypes( + const EvaluationContext *eval_ctx, Scene *scene, Object *ob, ListBase *dispbase, + DerivedMesh **r_dm_final, + const bool for_render, const bool for_orco, const bool use_render_resolution) { Curve *cu = ob->data; @@ -1523,7 +1528,7 @@ static void do_makeDispListCurveTypes(Scene *scene, Object *ob, ListBase *dispba if (!ELEM(ob->type, OB_SURF, OB_CURVE, OB_FONT)) return; if (ob->type == OB_SURF) { - BKE_displist_make_surf(scene, ob, dispbase, r_dm_final, for_render, for_orco, use_render_resolution); + BKE_displist_make_surf(eval_ctx, scene, ob, dispbase, r_dm_final, for_render, for_orco, use_render_resolution); } else if (ELEM(ob->type, OB_CURVE, OB_FONT)) { ListBase dlbev; @@ -1548,12 +1553,12 @@ static void do_makeDispListCurveTypes(Scene *scene, Object *ob, ListBase *dispba } if (!for_orco) - curve_calc_modifiers_pre(scene, ob, &nubase, for_render, use_render_resolution); + curve_calc_modifiers_pre(eval_ctx, scene, ob, &nubase, for_render, use_render_resolution); BKE_curve_bevelList_make(ob, &nubase, for_render != false); /* If curve has no bevel will return nothing */ - BKE_curve_bevel_make(scene, ob, &dlbev, for_render, use_render_resolution); + BKE_curve_bevel_make(eval_ctx, scene, ob, &dlbev, for_render, use_render_resolution); /* no bevel or extrude, and no width correction? */ if (!dlbev.first && cu->width == 1.0f) { @@ -1688,7 +1693,7 @@ static void do_makeDispListCurveTypes(Scene *scene, Object *ob, ListBase *dispba taper_fac -= (1.0f - lastblend) / len; } - fac = displist_calc_taper(scene, cu->taperobj, taper_fac); + fac = displist_calc_taper(eval_ctx, scene, cu->taperobj, taper_fac); } if (bevp->split_tag) { @@ -1740,8 +1745,9 @@ static void do_makeDispListCurveTypes(Scene *scene, Object *ob, ListBase *dispba } if (!for_orco) { + /* TODO(sergey): How do we get depsgraph here? */ if ((cu->flag & CU_PATH) || - DEG_get_eval_flags_for_id(scene->depsgraph, &ob->id) & DAG_EVAL_NEED_CURVE_PATH) + DEG_get_eval_flags_for_id(scene->depsgraph_legacy, &ob->id) & DAG_EVAL_NEED_CURVE_PATH) { calc_curvepath(ob, &nubase); } @@ -1749,7 +1755,7 @@ static void do_makeDispListCurveTypes(Scene *scene, Object *ob, ListBase *dispba if (!for_orco) { BKE_nurbList_duplicate(&ob->curve_cache->deformed_nurbs, &nubase); - curve_calc_modifiers_post(scene, ob, &nubase, dispbase, r_dm_final, for_render, use_render_resolution); + curve_calc_modifiers_post(eval_ctx, scene, ob, &nubase, dispbase, r_dm_final, for_render, use_render_resolution); } if (cu->flag & CU_DEFORM_FILL && !ob->derivedFinal) { @@ -1760,7 +1766,7 @@ static void do_makeDispListCurveTypes(Scene *scene, Object *ob, ListBase *dispba } } -void BKE_displist_make_curveTypes(Scene *scene, Object *ob, const bool for_orco) +void BKE_displist_make_curveTypes(const EvaluationContext *eval_ctx, Scene *scene, Object *ob, const bool for_orco) { ListBase *dispbase; @@ -1778,35 +1784,38 @@ void BKE_displist_make_curveTypes(Scene *scene, Object *ob, const bool for_orco) dispbase = &(ob->curve_cache->disp); - do_makeDispListCurveTypes(scene, ob, dispbase, &ob->derivedFinal, 0, for_orco, 0); + do_makeDispListCurveTypes(eval_ctx, scene, ob, dispbase, &ob->derivedFinal, 0, for_orco, 0); boundbox_displist_object(ob); } -void BKE_displist_make_curveTypes_forRender(Scene *scene, Object *ob, ListBase *dispbase, - DerivedMesh **r_dm_final, const bool for_orco, - const bool use_render_resolution) +void BKE_displist_make_curveTypes_forRender( + const EvaluationContext *eval_ctx, Scene *scene, Object *ob, ListBase *dispbase, + DerivedMesh **r_dm_final, const bool for_orco, + const bool use_render_resolution) { if (ob->curve_cache == NULL) { ob->curve_cache = MEM_callocN(sizeof(CurveCache), "CurveCache for Curve"); } - do_makeDispListCurveTypes(scene, ob, dispbase, r_dm_final, true, for_orco, use_render_resolution); + do_makeDispListCurveTypes(eval_ctx, scene, ob, dispbase, r_dm_final, true, for_orco, use_render_resolution); } -void BKE_displist_make_curveTypes_forOrco(struct Scene *scene, struct Object *ob, struct ListBase *dispbase) +void BKE_displist_make_curveTypes_forOrco( + const EvaluationContext *eval_ctx, Scene *scene, Object *ob, ListBase *dispbase) { if (ob->curve_cache == NULL) { ob->curve_cache = MEM_callocN(sizeof(CurveCache), "CurveCache for Curve"); } - do_makeDispListCurveTypes(scene, ob, dispbase, NULL, 1, 1, 1); + do_makeDispListCurveTypes(eval_ctx, scene, ob, dispbase, NULL, 1, 1, 1); } /* add Orco layer to the displist object which has got derived mesh and return orco */ -float *BKE_displist_make_orco(Scene *scene, Object *ob, DerivedMesh *dm_final, - const bool for_render, - const bool use_render_resolution) +float *BKE_displist_make_orco( + const EvaluationContext *eval_ctx, Scene *scene, Object *ob, DerivedMesh *dm_final, + const bool for_render, + const bool use_render_resolution) { float *orco; @@ -1814,7 +1823,7 @@ float *BKE_displist_make_orco(Scene *scene, Object *ob, DerivedMesh *dm_final, dm_final = ob->derivedFinal; if (!dm_final->getVertDataArray(dm_final, CD_ORCO)) { - curve_calc_orcodm(scene, ob, dm_final, for_render, use_render_resolution); + curve_calc_orcodm(eval_ctx, scene, ob, dm_final, for_render, use_render_resolution); } orco = dm_final->getVertDataArray(dm_final, CD_ORCO); diff --git a/source/blender/blenkernel/intern/dynamicpaint.c b/source/blender/blenkernel/intern/dynamicpaint.c index fb5ef403218..724b30d0cb1 100644 --- a/source/blender/blenkernel/intern/dynamicpaint.c +++ b/source/blender/blenkernel/intern/dynamicpaint.c @@ -74,6 +74,8 @@ #include "BKE_scene.h" #include "BKE_texture.h" +#include "DEG_depsgraph.h" + /* for image output */ #include "IMB_imbuf_types.h" #include "IMB_imbuf.h" @@ -498,7 +500,7 @@ static int surface_getBrushFlags(DynamicPaintSurface *surface, const SceneLayer if (surface->brush_group) go = surface->brush_group->gobject.first; else - base = FIRSTBASE_NEW; + base = FIRSTBASE_NEW(sl); while (base || go) { brushObj = NULL; @@ -1973,7 +1975,9 @@ static void canvas_copyDerivedMesh(DynamicPaintCanvasSettings *canvas, DerivedMe /* * Updates derived mesh copy and processes dynamic paint step / caches. */ -static void dynamicPaint_frameUpdate(DynamicPaintModifierData *pmd, Scene *scene, SceneLayer *sl, Object *ob, DerivedMesh *dm) +static void dynamicPaint_frameUpdate( + DynamicPaintModifierData *pmd, const struct EvaluationContext *eval_ctx, Scene *scene, + Object *ob, DerivedMesh *dm) { if (pmd->canvas) { DynamicPaintCanvasSettings *canvas = pmd->canvas; @@ -2036,7 +2040,7 @@ static void dynamicPaint_frameUpdate(DynamicPaintModifierData *pmd, Scene *scene else if (can_simulate) { /* calculate surface frame */ canvas->flags |= MOD_DPAINT_BAKING; - dynamicPaint_calculateFrame(surface, scene, sl, ob, current_frame); + dynamicPaint_calculateFrame(surface, eval_ctx, scene, ob, current_frame); canvas->flags &= ~MOD_DPAINT_BAKING; /* restore canvas derivedmesh if required */ @@ -2055,16 +2059,15 @@ static void dynamicPaint_frameUpdate(DynamicPaintModifierData *pmd, Scene *scene } /* Modifier call. Processes dynamic paint modifier step. */ -DerivedMesh *dynamicPaint_Modifier_do(DynamicPaintModifierData *pmd, Scene *scene, SceneLayer *sl, Object *ob, DerivedMesh *dm) +DerivedMesh *dynamicPaint_Modifier_do( + DynamicPaintModifierData *pmd, const struct EvaluationContext *eval_ctx, Scene *scene, + Object *ob, DerivedMesh *dm) { if (pmd->canvas) { DerivedMesh *ret; - /* For now generate looptris in every case */ - DM_ensure_looptri(dm); - /* Update canvas data for a new frame */ - dynamicPaint_frameUpdate(pmd, scene, sl, ob, dm); + dynamicPaint_frameUpdate(pmd, eval_ctx, scene, ob, dm); /* Return output mesh */ ret = dynamicPaint_Modifier_apply(pmd, ob, dm); @@ -2072,11 +2075,8 @@ DerivedMesh *dynamicPaint_Modifier_do(DynamicPaintModifierData *pmd, Scene *scen return ret; } else { - /* For now generate looptris in every case */ - DM_ensure_looptri(dm); - /* Update canvas data for a new frame */ - dynamicPaint_frameUpdate(pmd, scene, sl, ob, dm); + dynamicPaint_frameUpdate(pmd, eval_ctx, scene, ob, dm); /* Return output mesh */ return dynamicPaint_Modifier_apply(pmd, ob, dm); @@ -3582,7 +3582,8 @@ static void dynamic_paint_brush_velocity_compute_cb(void *userdata, const int i) } static void dynamicPaint_brushMeshCalculateVelocity( - Scene *scene, Object *ob, DynamicPaintBrushSettings *brush, Vec3f **brushVel, float timescale) + const struct EvaluationContext *eval_ctx, Scene *scene, + Object *ob, DynamicPaintBrushSettings *brush, Vec3f **brushVel, float timescale) { float prev_obmat[4][4]; DerivedMesh *dm_p, *dm_c; @@ -3604,7 +3605,7 @@ static void dynamicPaint_brushMeshCalculateVelocity( scene->r.subframe = prev_sfra; BKE_object_modifier_update_subframe( - scene, ob, true, SUBFRAME_RECURSION, BKE_scene_frame_get(scene), eModifierType_DynamicPaint); + eval_ctx, scene, ob, true, SUBFRAME_RECURSION, BKE_scene_frame_get(scene), eModifierType_DynamicPaint); dm_p = CDDM_copy(brush->dm); numOfVerts_p = dm_p->getNumVerts(dm_p); mvert_p = dm_p->getVertArray(dm_p); @@ -3615,7 +3616,7 @@ static void dynamicPaint_brushMeshCalculateVelocity( scene->r.subframe = cur_sfra; BKE_object_modifier_update_subframe( - scene, ob, true, SUBFRAME_RECURSION, BKE_scene_frame_get(scene), eModifierType_DynamicPaint); + eval_ctx, scene, ob, true, SUBFRAME_RECURSION, BKE_scene_frame_get(scene), eModifierType_DynamicPaint); dm_c = brush->dm; numOfVerts_c = dm_c->getNumVerts(dm_c); mvert_c = dm_p->getVertArray(dm_c); @@ -3640,7 +3641,7 @@ static void dynamicPaint_brushMeshCalculateVelocity( } /* calculate velocity for object center point */ -static void dynamicPaint_brushObjectCalculateVelocity(Scene *scene, Object *ob, Vec3f *brushVel, float timescale) +static void dynamicPaint_brushObjectCalculateVelocity(const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, Vec3f *brushVel, float timescale) { float prev_obmat[4][4]; float cur_loc[3] = {0.0f}, prev_loc[3] = {0.0f}; @@ -3659,14 +3660,14 @@ static void dynamicPaint_brushObjectCalculateVelocity(Scene *scene, Object *ob, scene->r.cfra = prev_fra; scene->r.subframe = prev_sfra; BKE_object_modifier_update_subframe( - scene, ob, false, SUBFRAME_RECURSION, BKE_scene_frame_get(scene), eModifierType_DynamicPaint); + eval_ctx, scene, ob, false, SUBFRAME_RECURSION, BKE_scene_frame_get(scene), eModifierType_DynamicPaint); copy_m4_m4(prev_obmat, ob->obmat); /* current frame dm */ scene->r.cfra = cur_fra; scene->r.subframe = cur_sfra; BKE_object_modifier_update_subframe( - scene, ob, false, SUBFRAME_RECURSION, BKE_scene_frame_get(scene), eModifierType_DynamicPaint); + eval_ctx, scene, ob, false, SUBFRAME_RECURSION, BKE_scene_frame_get(scene), eModifierType_DynamicPaint); /* calculate speed */ mul_m4_v3(prev_obmat, prev_loc); @@ -4036,7 +4037,7 @@ static void dynamic_paint_paint_mesh_cell_point_cb_ex( } } -static int dynamicPaint_paintMesh(DynamicPaintSurface *surface, +static int dynamicPaint_paintMesh(const struct EvaluationContext *eval_ctx, DynamicPaintSurface *surface, DynamicPaintBrushSettings *brush, Object *brushOb, BrushMaterials *bMats, @@ -4052,7 +4053,7 @@ static int dynamicPaint_paintMesh(DynamicPaintSurface *surface, const MLoop *mloop = NULL; if (brush->flags & MOD_DPAINT_USES_VELOCITY) - dynamicPaint_brushMeshCalculateVelocity(scene, brushOb, brush, &brushVelocity, timescale); + dynamicPaint_brushMeshCalculateVelocity(eval_ctx, scene, brushOb, brush, &brushVelocity, timescale); if (!brush->dm) return 0; @@ -4530,7 +4531,7 @@ static void dynamic_paint_paint_single_point_cb_ex( } static int dynamicPaint_paintSinglePoint( - DynamicPaintSurface *surface, float *pointCoord, DynamicPaintBrushSettings *brush, + const struct EvaluationContext *eval_ctx, DynamicPaintSurface *surface, float *pointCoord, DynamicPaintBrushSettings *brush, Object *brushOb, BrushMaterials *bMats, Scene *scene, float timescale) { PaintSurfaceData *sData = surface->data; @@ -4538,7 +4539,7 @@ static int dynamicPaint_paintSinglePoint( Vec3f brushVel; if (brush->flags & MOD_DPAINT_USES_VELOCITY) - dynamicPaint_brushObjectCalculateVelocity(scene, brushOb, &brushVel, timescale); + dynamicPaint_brushObjectCalculateVelocity(eval_ctx, scene, brushOb, &brushVel, timescale); const MVert *mvert = brush->dm->getVertArray(brush->dm); @@ -4845,7 +4846,7 @@ static void dynamic_paint_prepare_effect_cb(void *userdata, const int index) } static int dynamicPaint_prepareEffectStep( - DynamicPaintSurface *surface, Scene *scene, Object *ob, float **force, float timescale) + const struct EvaluationContext *eval_ctx, DynamicPaintSurface *surface, Scene *scene, Object *ob, float **force, float timescale) { double average_force = 0.0f; float shrink_speed = 0.0f, spread_speed = 0.0f; @@ -4856,7 +4857,7 @@ static int dynamicPaint_prepareEffectStep( /* Init force data if required */ if (surface->effect & MOD_DPAINT_EFFECT_DO_DRIP) { - ListBase *effectors = pdInitEffectors(scene, ob, NULL, surface->effector_weights, true); + ListBase *effectors = pdInitEffectors(eval_ctx, scene, ob, NULL, surface->effector_weights, true); /* allocate memory for force data (dir vector + strength) */ *force = MEM_mallocN(sData->total_points * 4 * sizeof(float), "PaintEffectForces"); @@ -5758,7 +5759,7 @@ static int dynamicPaint_generateBakeData(DynamicPaintSurface *surface, const Sce /* * Do Dynamic Paint step. Paints scene brush objects of current state/frame to the surface. */ -static int dynamicPaint_doStep(Scene *scene, SceneLayer *sl, Object *ob, DynamicPaintSurface *surface, float timescale, float subframe) +static int dynamicPaint_doStep(const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, DynamicPaintSurface *surface, float timescale, float subframe) { PaintSurfaceData *sData = surface->data; PaintBakeData *bData = sData->bData; @@ -5782,6 +5783,7 @@ static int dynamicPaint_doStep(Scene *scene, SceneLayer *sl, Object *ob, Dynamic GroupObject *go = NULL; Object *brushObj = NULL; ModifierData *md = NULL; + SceneLayer *sl = eval_ctx->scene_layer; /* backup current scene frame */ int scene_frame = scene->r.cfra; @@ -5791,7 +5793,7 @@ static int dynamicPaint_doStep(Scene *scene, SceneLayer *sl, Object *ob, Dynamic if (surface->brush_group) go = surface->brush_group->gobject.first; else - base = FIRSTBASE_NEW; + base = FIRSTBASE_NEW(sl); while (base || go) { brushObj = NULL; @@ -5836,7 +5838,7 @@ static int dynamicPaint_doStep(Scene *scene, SceneLayer *sl, Object *ob, Dynamic /* update object data on this subframe */ if (subframe) { scene_setSubframe(scene, subframe); - BKE_object_modifier_update_subframe(scene, brushObj, true, SUBFRAME_RECURSION, + BKE_object_modifier_update_subframe(eval_ctx, scene, brushObj, true, SUBFRAME_RECURSION, BKE_scene_frame_get(scene), eModifierType_DynamicPaint); } /* Prepare materials if required */ @@ -5855,11 +5857,11 @@ static int dynamicPaint_doStep(Scene *scene, SceneLayer *sl, Object *ob, Dynamic } /* Object center distance: */ if (brush->collision == MOD_DPAINT_COL_POINT && brushObj != ob) { - dynamicPaint_paintSinglePoint(surface, brushObj->loc, brush, brushObj, &bMats, scene, timescale); + dynamicPaint_paintSinglePoint(eval_ctx, surface, brushObj->loc, brush, brushObj, &bMats, scene, timescale); } /* Mesh volume/proximity: */ else if (brushObj != ob) { - dynamicPaint_paintMesh(surface, brush, brushObj, &bMats, scene, timescale); + dynamicPaint_paintMesh(eval_ctx, surface, brush, brushObj, &bMats, scene, timescale); } /* free temp material data */ @@ -5869,7 +5871,7 @@ static int dynamicPaint_doStep(Scene *scene, SceneLayer *sl, Object *ob, Dynamic if (subframe) { scene->r.cfra = scene_frame; scene->r.subframe = scene_subframe; - BKE_object_modifier_update_subframe(scene, brushObj, true, SUBFRAME_RECURSION, + BKE_object_modifier_update_subframe(eval_ctx, scene, brushObj, true, SUBFRAME_RECURSION, BKE_scene_frame_get(scene), eModifierType_DynamicPaint); } @@ -5904,7 +5906,7 @@ static int dynamicPaint_doStep(Scene *scene, SceneLayer *sl, Object *ob, Dynamic return setError(canvas, N_("Not enough free memory")); /* Prepare effects and get number of required steps */ - steps = dynamicPaint_prepareEffectStep(surface, scene, ob, &force, timescale); + steps = dynamicPaint_prepareEffectStep(eval_ctx, surface, scene, ob, &force, timescale); for (s = 0; s < steps; s++) { dynamicPaint_doEffectStep(surface, force, prevPoint, timescale, (float)steps); } @@ -5928,7 +5930,9 @@ static int dynamicPaint_doStep(Scene *scene, SceneLayer *sl, Object *ob, Dynamic /* * Calculate a single frame and included subframes for surface */ -int dynamicPaint_calculateFrame(DynamicPaintSurface *surface, Scene *scene, SceneLayer *sl, Object *cObject, int frame) +int dynamicPaint_calculateFrame( + DynamicPaintSurface *surface, const struct EvaluationContext *eval_ctx, + Scene *scene, Object *cObject, int frame) { float timescale = 1.0f; @@ -5937,7 +5941,7 @@ int dynamicPaint_calculateFrame(DynamicPaintSurface *surface, Scene *scene, Scen dynamicPaint_applySurfaceDisplace(surface, surface->canvas->dm); /* update bake data */ - dynamicPaint_generateBakeData(surface, sl, cObject); + dynamicPaint_generateBakeData(surface, eval_ctx->scene_layer, cObject); /* don't do substeps for first frame */ if (surface->substeps && (frame != surface->start_frame)) { @@ -5946,10 +5950,10 @@ int dynamicPaint_calculateFrame(DynamicPaintSurface *surface, Scene *scene, Scen for (st = 1; st <= surface->substeps; st++) { float subframe = ((float) st) / (surface->substeps + 1); - if (!dynamicPaint_doStep(scene, sl, cObject, surface, timescale, subframe)) + if (!dynamicPaint_doStep(eval_ctx, scene, cObject, surface, timescale, subframe)) return 0; } } - return dynamicPaint_doStep(scene, sl, cObject, surface, timescale, 0.0f); + return dynamicPaint_doStep(eval_ctx, scene, cObject, surface, timescale, 0.0f); } diff --git a/source/blender/blenkernel/intern/editderivedmesh.c b/source/blender/blenkernel/intern/editderivedmesh.c index c8ae3598097..0491cbd21f0 100644 --- a/source/blender/blenkernel/intern/editderivedmesh.c +++ b/source/blender/blenkernel/intern/editderivedmesh.c @@ -41,6 +41,8 @@ * is likely to be a little slow. */ +#include "atomic_ops.h" + #include "BLI_math.h" #include "BLI_jitter.h" #include "BLI_bitmap.h" @@ -279,8 +281,9 @@ static void emDM_recalcLoopTri(DerivedMesh *dm) int i; DM_ensure_looptri_data(dm); - mlooptri = dm->looptris.array; + mlooptri = dm->looptris.array_wip; + BLI_assert(tottri == 0 || mlooptri != NULL); BLI_assert(poly_to_tri_count(dm->numPolyData, dm->numLoopData) == dm->looptris.num); BLI_assert(tottri == dm->looptris.num); @@ -297,18 +300,10 @@ static void emDM_recalcLoopTri(DerivedMesh *dm) BM_elem_index_get(ltri[2])); lt->poly = BM_elem_index_get(ltri[0]->f); } -} - -static const MLoopTri *emDM_getLoopTriArray(DerivedMesh *dm) -{ - if (dm->looptris.array) { - BLI_assert(poly_to_tri_count(dm->numPolyData, dm->numLoopData) == dm->looptris.num); - } - else { - dm->recalcLoopTri(dm); - } - return dm->looptris.array; + BLI_assert(dm->looptris.array == NULL); + atomic_cas_ptr((void **)&dm->looptris.array, dm->looptris.array, dm->looptris.array_wip); + dm->looptris.array_wip = NULL; } static void emDM_foreachMappedVert( @@ -1639,8 +1634,6 @@ DerivedMesh *getEditDerivedBMesh( bmdm->dm.getNumLoops = emDM_getNumLoops; bmdm->dm.getNumPolys = emDM_getNumPolys; - bmdm->dm.getLoopTriArray = emDM_getLoopTriArray; - bmdm->dm.getVert = emDM_getVert; bmdm->dm.getVertCo = emDM_getVertCo; bmdm->dm.getVertNo = emDM_getVertNo; @@ -2018,7 +2011,7 @@ static void statvis_calc_distort( vertexCos[BM_elem_index_get(l_iter->next->v)]); } else { - BM_loop_calc_face_normal(l_iter, no_corner); + BM_loop_calc_face_normal_safe(l_iter, no_corner); } /* simple way to detect (what is most likely) concave */ if (dot_v3v3(f_no, no_corner) < 0.0f) { @@ -2183,14 +2176,14 @@ static void cage_mapped_verts_callback( } } -float (*BKE_editmesh_vertexCos_get(BMEditMesh *em, Scene *scene, int *r_numVerts))[3] +float (*BKE_editmesh_vertexCos_get(const struct EvaluationContext *eval_ctx, BMEditMesh *em, Scene *scene, int *r_numVerts))[3] { DerivedMesh *cage, *final; BLI_bitmap *visit_bitmap; struct CageUserData data; float (*cos_cage)[3]; - cage = editbmesh_get_derived_cage_and_final(scene, em->ob, em, CD_MASK_BAREMESH, &final); + cage = editbmesh_get_derived_cage_and_final(eval_ctx, scene, em->ob, em, CD_MASK_BAREMESH, &final); cos_cage = MEM_callocN(sizeof(*cos_cage) * em->bm->totvert, "bmbvh cos_cage"); /* when initializing cage verts, we only want the first cage coordinate for each vertex, diff --git a/source/blender/blenkernel/intern/editmesh_tangent.c b/source/blender/blenkernel/intern/editmesh_tangent.c index b04fc753f7a..f725a1793b4 100644 --- a/source/blender/blenkernel/intern/editmesh_tangent.c +++ b/source/blender/blenkernel/intern/editmesh_tangent.c @@ -283,33 +283,36 @@ void BKE_editmesh_loop_tangent_calc( /* result */ CustomData *loopdata_out, const uint loopdata_out_len, - char *tangent_mask_curr_p) + short *tangent_mask_curr_p) { BMesh *bm = em->bm; - BLI_assert(CustomData_number_of_layers(&bm->ldata, CD_MLOOPUV) != 0); - int act_uv_n = -1; int ren_uv_n = -1; bool calc_act = false; bool calc_ren = false; char act_uv_name[MAX_NAME]; char ren_uv_name[MAX_NAME]; - char tangent_mask = 0; - char tangent_mask_curr = *tangent_mask_curr_p; + short tangent_mask = 0; + short tangent_mask_curr = *tangent_mask_curr_p; BKE_mesh_calc_loop_tangent_step_0( &bm->ldata, calc_active_tangent, tangent_names, tangent_names_len, &calc_act, &calc_ren, &act_uv_n, &ren_uv_n, act_uv_name, ren_uv_name, &tangent_mask); if ((tangent_mask_curr | tangent_mask) != tangent_mask_curr) { - for (int i = 0; i < tangent_names_len; i++) - if (tangent_names[i][0]) - BKE_mesh_add_loop_tangent_named_layer_for_uv(&bm->ldata, loopdata_out, loopdata_out_len, tangent_names[i]); + for (int i = 0; i < tangent_names_len; i++) { + if (tangent_names[i][0]) { + BKE_mesh_add_loop_tangent_named_layer_for_uv( + &bm->ldata, loopdata_out, (int)loopdata_out_len, tangent_names[i]); + } + } + if ((tangent_mask & DM_TANGENT_MASK_ORCO) && CustomData_get_named_layer_index(loopdata_out, CD_TANGENT, "") == -1) + CustomData_add_layer_named(loopdata_out, CD_TANGENT, CD_CALLOC, NULL, (int)loopdata_out_len, ""); if (calc_act && act_uv_name[0]) - BKE_mesh_add_loop_tangent_named_layer_for_uv(&bm->ldata, loopdata_out, loopdata_out_len, act_uv_name); + BKE_mesh_add_loop_tangent_named_layer_for_uv(&bm->ldata, loopdata_out, (int)loopdata_out_len, act_uv_name); if (calc_ren && ren_uv_name[0]) - BKE_mesh_add_loop_tangent_named_layer_for_uv(&bm->ldata, loopdata_out, loopdata_out_len, ren_uv_name); + BKE_mesh_add_loop_tangent_named_layer_for_uv(&bm->ldata, loopdata_out, (int)loopdata_out_len, ren_uv_name); int totface = em->tottri; #ifdef USE_LOOPTRI_DETECT_QUADS int num_face_as_quad_map; @@ -336,7 +339,7 @@ void BKE_editmesh_loop_tangent_calc( } #endif /* Calculation */ - { + if (em->tottri != 0) { TaskScheduler *scheduler = BLI_task_scheduler_get(); TaskPool *task_pool; task_pool = BLI_task_pool_create(scheduler, NULL); @@ -372,6 +375,15 @@ void BKE_editmesh_loop_tangent_calc( continue; /* needed for orco lookups */ htype_index |= BM_VERT; + tangent_mask_curr |= DM_TANGENT_MASK_ORCO; + } + else { + /* Fill the resulting tangent_mask */ + int uv_ind = CustomData_get_named_layer_index(&bm->ldata, CD_MLOOPUV, loopdata_out->layers[index].name); + int uv_start = CustomData_get_layer_index(&bm->ldata, CD_MLOOPUV); + BLI_assert(uv_ind != -1 && uv_start != -1); + BLI_assert(uv_ind - uv_start < MAX_MTFACE); + tangent_mask_curr |= 1 << (uv_ind - uv_start); } if (mesh2tangent->precomputedFaceNormals) { /* needed for face normal lookups */ @@ -382,13 +394,6 @@ void BKE_editmesh_loop_tangent_calc( mesh2tangent->looptris = (const BMLoop *(*)[3])em->looptris; mesh2tangent->tangent = loopdata_out->layers[index].data; - /* Fill the resulting tangent_mask */ - int uv_ind = CustomData_get_named_layer_index( - &bm->ldata, CD_MLOOPUV, loopdata_out->layers[index].name); - int uv_start = CustomData_get_layer_index(&bm->ldata, CD_MLOOPUV); - BLI_assert(uv_ind != -1 && uv_start != -1); - BLI_assert(uv_ind - uv_start < MAX_MTFACE); - tangent_mask_curr |= 1 << (uv_ind - uv_start); BLI_task_pool_push(task_pool, emDM_calc_loop_tangents_thread, mesh2tangent, false, TASK_PRIORITY_LOW); } @@ -396,6 +401,9 @@ void BKE_editmesh_loop_tangent_calc( BLI_task_pool_work_and_wait(task_pool); BLI_task_pool_free(task_pool); } + else { + tangent_mask_curr = tangent_mask; + } #ifdef USE_LOOPTRI_DETECT_QUADS if (face_as_quad_map) { MEM_freeN(face_as_quad_map); @@ -406,15 +414,18 @@ void BKE_editmesh_loop_tangent_calc( *tangent_mask_curr_p = tangent_mask_curr; - /* Update active layer index */ - int uv_index = CustomData_get_layer_index_n(&bm->ldata, CD_MLOOPUV, act_uv_n); - int tan_index = CustomData_get_named_layer_index(loopdata_out, CD_TANGENT, bm->ldata.layers[uv_index].name); - CustomData_set_layer_active_index(loopdata_out, CD_TANGENT, tan_index); + int act_uv_index = CustomData_get_layer_index_n(&bm->ldata, CD_MLOOPUV, act_uv_n); + if (act_uv_index >= 0) { + int tan_index = CustomData_get_named_layer_index(loopdata_out, CD_TANGENT, bm->ldata.layers[act_uv_index].name); + CustomData_set_layer_active_index(loopdata_out, CD_TANGENT, tan_index); + } /* else tangent has been built from orco */ /* Update render layer index */ - uv_index = CustomData_get_layer_index_n(&bm->ldata, CD_MLOOPUV, ren_uv_n); - tan_index = CustomData_get_named_layer_index(loopdata_out, CD_TANGENT, bm->ldata.layers[uv_index].name); - CustomData_set_layer_render_index(loopdata_out, CD_TANGENT, tan_index); + int ren_uv_index = CustomData_get_layer_index_n(&bm->ldata, CD_MLOOPUV, ren_uv_n); + if (ren_uv_index >= 0) { + int tan_index = CustomData_get_named_layer_index(loopdata_out, CD_TANGENT, bm->ldata.layers[ren_uv_index].name); + CustomData_set_layer_render_index(loopdata_out, CD_TANGENT, tan_index); + } /* else tangent has been built from orco */ } /** \} */
\ No newline at end of file diff --git a/source/blender/blenkernel/intern/effect.c b/source/blender/blenkernel/intern/effect.c index 2213094cd0b..36f7f6d604e 100644 --- a/source/blender/blenkernel/intern/effect.c +++ b/source/blender/blenkernel/intern/effect.c @@ -72,6 +72,7 @@ #include "BKE_scene.h" #include "BKE_smoke.h" +#include "DEG_depsgraph.h" #include "RE_render_ext.h" #include "RE_shader_ext.h" @@ -146,9 +147,10 @@ void free_partdeflect(PartDeflect *pd) MEM_freeN(pd); } -static EffectorCache *new_effector_cache(Scene *scene, Object *ob, ParticleSystem *psys, PartDeflect *pd) +static EffectorCache *new_effector_cache(const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, ParticleSystem *psys, PartDeflect *pd) { EffectorCache *eff = MEM_callocN(sizeof(EffectorCache), "EffectorCache"); + eff->eval_ctx = eval_ctx; eff->scene = scene; eff->ob = ob; eff->psys = psys; @@ -156,7 +158,7 @@ static EffectorCache *new_effector_cache(Scene *scene, Object *ob, ParticleSyste eff->frame = -1; return eff; } -static void add_object_to_effectors(ListBase **effectors, Scene *scene, EffectorWeights *weights, Object *ob, Object *ob_src, bool for_simulation) +static void add_object_to_effectors(ListBase **effectors, const struct EvaluationContext *eval_ctx, Scene *scene, EffectorWeights *weights, Object *ob, Object *ob_src, bool for_simulation) { EffectorCache *eff = NULL; @@ -174,14 +176,14 @@ static void add_object_to_effectors(ListBase **effectors, Scene *scene, Effector if (*effectors == NULL) *effectors = MEM_callocN(sizeof(ListBase), "effectors list"); - eff = new_effector_cache(scene, ob, NULL, ob->pd); + eff = new_effector_cache(eval_ctx, scene, ob, NULL, ob->pd); /* make sure imat is up to date */ invert_m4_m4(ob->imat, ob->obmat); BLI_addtail(*effectors, eff); } -static void add_particles_to_effectors(ListBase **effectors, Scene *scene, EffectorWeights *weights, Object *ob, ParticleSystem *psys, ParticleSystem *psys_src, bool for_simulation) +static void add_particles_to_effectors(ListBase **effectors, const struct EvaluationContext *eval_ctx, Scene *scene, EffectorWeights *weights, Object *ob, ParticleSystem *psys, ParticleSystem *psys_src, bool for_simulation) { ParticleSettings *part= psys->part; @@ -195,25 +197,34 @@ static void add_particles_to_effectors(ListBase **effectors, Scene *scene, Effec if (*effectors == NULL) *effectors = MEM_callocN(sizeof(ListBase), "effectors list"); - BLI_addtail(*effectors, new_effector_cache(scene, ob, psys, part->pd)); + BLI_addtail(*effectors, new_effector_cache(eval_ctx, scene, ob, psys, part->pd)); } if (part->pd2 && part->pd2->forcefield && (!for_simulation || weights->weight[part->pd2->forcefield] != 0.0f)) { if (*effectors == NULL) *effectors = MEM_callocN(sizeof(ListBase), "effectors list"); - BLI_addtail(*effectors, new_effector_cache(scene, ob, psys, part->pd2)); + BLI_addtail(*effectors, new_effector_cache(eval_ctx, scene, ob, psys, part->pd2)); } } /* returns ListBase handle with objects taking part in the effecting */ -ListBase *pdInitEffectors(Scene *scene, Object *ob_src, ParticleSystem *psys_src, - EffectorWeights *weights, bool for_simulation) +ListBase *pdInitEffectors( + const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob_src, ParticleSystem *psys_src, + EffectorWeights *weights, bool for_simulation) { - SceneLayer *sl = BKE_scene_layer_context_active(scene); /* Can't get sl from the calling modifiers yet */ + SceneLayer *sl; Base *base; unsigned int layer= ob_src->lay; ListBase *effectors = NULL; + + /* eval_ctx is NULL during deg build */ + if (eval_ctx) { + sl = eval_ctx->scene_layer; + } + else { + sl = BKE_scene_layer_context_active_PLACEHOLDER(scene); + } if (weights->group) { GroupObject *go; @@ -221,33 +232,33 @@ ListBase *pdInitEffectors(Scene *scene, Object *ob_src, ParticleSystem *psys_src for (go= weights->group->gobject.first; go; go= go->next) { if ( (go->ob->lay & layer) ) { if ( go->ob->pd && go->ob->pd->forcefield ) - add_object_to_effectors(&effectors, scene, weights, go->ob, ob_src, for_simulation); + add_object_to_effectors(&effectors, eval_ctx, scene, weights, go->ob, ob_src, for_simulation); if ( go->ob->particlesystem.first ) { ParticleSystem *psys= go->ob->particlesystem.first; for ( ; psys; psys=psys->next ) - add_particles_to_effectors(&effectors, scene, weights, go->ob, psys, psys_src, for_simulation); + add_particles_to_effectors(&effectors, eval_ctx, scene, weights, go->ob, psys, psys_src, for_simulation); } } } } else { - for (base = FIRSTBASE_NEW; base; base = base->next) { + for (base = FIRSTBASE_NEW(sl); base; base = base->next) { if ( base->object->pd && base->object->pd->forcefield ) - add_object_to_effectors(&effectors, scene, weights, base->object, ob_src, for_simulation); + add_object_to_effectors(&effectors, eval_ctx, scene, weights, base->object, ob_src, for_simulation); if ( base->object->particlesystem.first ) { ParticleSystem *psys= base->object->particlesystem.first; for ( ; psys; psys=psys->next ) - add_particles_to_effectors(&effectors, scene, weights, base->object, psys, psys_src, for_simulation); + add_particles_to_effectors(&effectors, eval_ctx, scene, weights, base->object, psys, psys_src, for_simulation); } } } if (for_simulation) - pdPrecalculateEffectors(effectors); + pdPrecalculateEffectors(eval_ctx, effectors); return effectors; } @@ -268,7 +279,7 @@ void pdEndEffectors(ListBase **effectors) } } -static void precalculate_effector(EffectorCache *eff) +static void precalculate_effector(const struct EvaluationContext *eval_ctx, EffectorCache *eff) { unsigned int cfra = (unsigned int)(eff->scene->r.cfra >= 0 ? eff->scene->r.cfra : -eff->scene->r.cfra); if (!eff->pd->rng) @@ -280,7 +291,7 @@ static void precalculate_effector(EffectorCache *eff) Curve *cu= eff->ob->data; if (cu->flag & CU_PATH) { if (eff->ob->curve_cache == NULL || eff->ob->curve_cache->path==NULL || eff->ob->curve_cache->path->data==NULL) - BKE_displist_make_curveTypes(eff->scene, eff->ob, 0); + BKE_displist_make_curveTypes(eval_ctx, eff->scene, eff->ob, 0); if (eff->ob->curve_cache->path && eff->ob->curve_cache->path->data) { where_on_path(eff->ob, 0.0, eff->guide_loc, eff->guide_dir, NULL, &eff->guide_radius, NULL); @@ -301,19 +312,19 @@ static void precalculate_effector(EffectorCache *eff) if (eff->ob) { float old_vel[3]; - BKE_object_where_is_calc_time(eff->scene, eff->ob, cfra - 1.0f); + BKE_object_where_is_calc_time(eval_ctx, eff->scene, eff->ob, cfra - 1.0f); copy_v3_v3(old_vel, eff->ob->obmat[3]); - BKE_object_where_is_calc_time(eff->scene, eff->ob, cfra); + BKE_object_where_is_calc_time(eval_ctx, eff->scene, eff->ob, cfra); sub_v3_v3v3(eff->velocity, eff->ob->obmat[3], old_vel); } } -void pdPrecalculateEffectors(ListBase *effectors) +void pdPrecalculateEffectors(const struct EvaluationContext *eval_ctx, ListBase *effectors) { if (effectors) { EffectorCache *eff = effectors->first; for (; eff; eff=eff->next) - precalculate_effector(eff); + precalculate_effector(eval_ctx, eff); } } @@ -612,6 +623,7 @@ int get_effector_data(EffectorCache *eff, EffectorData *efd, EffectedPoint *poin } else { ParticleSimulationData sim= {NULL}; + sim.eval_ctx = eff->eval_ctx; sim.scene= eff->scene; sim.ob= eff->ob; sim.psys= eff->psys; diff --git a/source/blender/blenkernel/intern/fluidsim.c b/source/blender/blenkernel/intern/fluidsim.c index 8247336d915..12b9abc6d03 100644 --- a/source/blender/blenkernel/intern/fluidsim.c +++ b/source/blender/blenkernel/intern/fluidsim.c @@ -65,7 +65,7 @@ // file handling //------------------------------------------------------------------------------- -void initElbeemMesh(struct Scene *scene, struct Object *ob, +void initElbeemMesh(const struct EvaluationContext *eval_ctx, struct Scene *scene, struct Object *ob, int *numVertices, float **vertices, int *numTriangles, int **triangles, int useGlobalCoords, int modifierIndex) @@ -78,9 +78,7 @@ void initElbeemMesh(struct Scene *scene, struct Object *ob, float *verts; int *tris; - dm = mesh_create_derived_index_render(scene, ob, CD_MASK_BAREMESH, modifierIndex); - - DM_ensure_looptri(dm); + dm = mesh_create_derived_index_render(eval_ctx, scene, ob, CD_MASK_BAREMESH, modifierIndex); mvert = dm->getVertArray(dm); mloop = dm->getLoopArray(dm); diff --git a/source/blender/blenkernel/intern/font.c b/source/blender/blenkernel/intern/font.c index 401fed74c52..d6b28cfaf70 100644 --- a/source/blender/blenkernel/intern/font.c +++ b/source/blender/blenkernel/intern/font.c @@ -106,6 +106,23 @@ void BKE_vfont_free(struct VFont *vf) } } +void BKE_vfont_copy_data(Main *UNUSED(bmain), VFont *vfont_dst, const VFont *UNUSED(vfont_src), const int flag) +{ + /* We never handle usercount here for own data. */ + const int flag_subdata = flag | LIB_ID_CREATE_NO_USER_REFCOUNT; + + /* Just to be sure, should not have any value actually after reading time. */ + vfont_dst->temp_pf = NULL; + + if (vfont_dst->packedfile) { + vfont_dst->packedfile = dupPackedFile(vfont_dst->packedfile); + } + + if (vfont_dst->data) { + vfont_dst->data = BLI_vfontdata_copy(vfont_dst->data, flag_subdata); + } +} + static void *builtin_font_data = NULL; static int builtin_font_size = 0; @@ -249,7 +266,7 @@ VFont *BKE_vfont_load(Main *bmain, const char *filepath) vfd = BLI_vfontdata_from_freetypefont(pf); if (vfd) { - vfont = BKE_libblock_alloc(bmain, ID_VF, filename); + vfont = BKE_libblock_alloc(bmain, ID_VF, filename, 0); vfont->data = vfd; /* if there's a font name, use it for the ID name */ diff --git a/source/blender/blenkernel/intern/freestyle.c b/source/blender/blenkernel/intern/freestyle.c index 0a0b023df82..e45a938a4fc 100644 --- a/source/blender/blenkernel/intern/freestyle.c +++ b/source/blender/blenkernel/intern/freestyle.c @@ -44,7 +44,7 @@ // function declarations static FreestyleLineSet *alloc_lineset(void); -static void copy_lineset(FreestyleLineSet *new_lineset, FreestyleLineSet *lineset); +static void copy_lineset(FreestyleLineSet *new_lineset, FreestyleLineSet *lineset, const int flag); static FreestyleModuleConfig *alloc_module(void); static void copy_module(FreestyleModuleConfig *new_module, FreestyleModuleConfig *module); @@ -79,7 +79,7 @@ void BKE_freestyle_config_free(FreestyleConfig *config) BLI_freelistN(&config->modules); } -void BKE_freestyle_config_copy(FreestyleConfig *new_config, FreestyleConfig *config) +void BKE_freestyle_config_copy(FreestyleConfig *new_config, FreestyleConfig *config, const int flag) { FreestyleLineSet *lineset, *new_lineset; FreestyleModuleConfig *module, *new_module; @@ -93,7 +93,7 @@ void BKE_freestyle_config_copy(FreestyleConfig *new_config, FreestyleConfig *con BLI_listbase_clear(&new_config->linesets); for (lineset = (FreestyleLineSet *)config->linesets.first; lineset; lineset = lineset->next) { new_lineset = alloc_lineset(); - copy_lineset(new_lineset, lineset); + copy_lineset(new_lineset, lineset, flag); BLI_addtail(&new_config->linesets, (void *)new_lineset); } @@ -105,11 +105,9 @@ void BKE_freestyle_config_copy(FreestyleConfig *new_config, FreestyleConfig *con } } -static void copy_lineset(FreestyleLineSet *new_lineset, FreestyleLineSet *lineset) +static void copy_lineset(FreestyleLineSet *new_lineset, FreestyleLineSet *lineset, const int flag) { new_lineset->linestyle = lineset->linestyle; - if (new_lineset->linestyle) - id_us_plus(&new_lineset->linestyle->id); new_lineset->flags = lineset->flags; new_lineset->selection = lineset->selection; new_lineset->qi = lineset->qi; @@ -118,10 +116,12 @@ static void copy_lineset(FreestyleLineSet *new_lineset, FreestyleLineSet *linese new_lineset->edge_types = lineset->edge_types; new_lineset->exclude_edge_types = lineset->exclude_edge_types; new_lineset->group = lineset->group; - if (new_lineset->group) { - id_us_plus(&new_lineset->group->id); - } strcpy(new_lineset->name, lineset->name); + + if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) { + id_us_plus((ID *)new_lineset->linestyle); + id_us_plus((ID *)new_lineset->group); + } } static FreestyleModuleConfig *alloc_module(void) diff --git a/source/blender/blenkernel/intern/gpencil.c b/source/blender/blenkernel/intern/gpencil.c index 758438bb051..ee0d0b41898 100644 --- a/source/blender/blenkernel/intern/gpencil.c +++ b/source/blender/blenkernel/intern/gpencil.c @@ -627,7 +627,7 @@ bGPdata *BKE_gpencil_data_addnew(const char name[]) bGPdata *gpd; /* allocate memory for a new block */ - gpd = BKE_libblock_alloc(G.main, ID_GD, name); + gpd = BKE_libblock_alloc(G.main, ID_GD, name, 0); /* initial settings */ gpd->flag = (GP_DATA_DISPINFO | GP_DATA_EXPAND); @@ -753,47 +753,62 @@ bGPDlayer *BKE_gpencil_layer_duplicate(const bGPDlayer *gpl_src) return gpl_dst; } -/* make a copy of a given gpencil datablock */ -bGPdata *BKE_gpencil_data_duplicate(Main *bmain, const bGPdata *gpd_src, bool internal_copy) +/** + * Only copy internal data of GreasePencil ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_gpencil_copy_data(Main *UNUSED(bmain), bGPdata *gpd_dst, const bGPdata *gpd_src, const int UNUSED(flag)) { - const bGPDlayer *gpl_src; - bGPDlayer *gpl_dst; - bGPdata *gpd_dst; + /* copy layers */ + BLI_listbase_clear(&gpd_dst->layers); + for (const bGPDlayer *gpl_src = gpd_src->layers.first; gpl_src; gpl_src = gpl_src->next) { + /* make a copy of source layer and its data */ + bGPDlayer *gpl_dst = BKE_gpencil_layer_duplicate(gpl_src); /* TODO here too could add unused flags... */ + BLI_addtail(&gpd_dst->layers, gpl_dst); + } - /* error checking */ - if (gpd_src == NULL) { - return NULL; + /* copy palettes */ + BLI_listbase_clear(&gpd_dst->palettes); + for (const bGPDpalette *palette_src = gpd_src->palettes.first; palette_src; palette_src = palette_src->next) { + bGPDpalette *palette_dst = BKE_gpencil_palette_duplicate(palette_src); /* TODO here too could add unused flags... */ + BLI_addtail(&gpd_dst->palettes, palette_dst); } - - /* make a copy of the base-data */ +} + +/* make a copy of a given gpencil datablock */ +bGPdata *BKE_gpencil_data_duplicate(Main *bmain, const bGPdata *gpd_src, bool internal_copy) +{ + /* Yuck and super-uber-hyper yuck!!! + * Should be replaceable with a no-main copy (LIB_ID_COPY_NO_MAIN etc.), but not sure about it, + * so for now keep old code for that one. */ if (internal_copy) { + const bGPDlayer *gpl_src; + bGPDlayer *gpl_dst; + bGPdata *gpd_dst; + /* make a straight copy for undo buffers used during stroke drawing */ gpd_dst = MEM_dupallocN(gpd_src); + + /* copy layers */ + BLI_listbase_clear(&gpd_dst->layers); + for (gpl_src = gpd_src->layers.first; gpl_src; gpl_src = gpl_src->next) { + /* make a copy of source layer and its data */ + gpl_dst = BKE_gpencil_layer_duplicate(gpl_src); + BLI_addtail(&gpd_dst->layers, gpl_dst); + } + + /* return new */ + return gpd_dst; } else { - /* make a copy when others use this */ - gpd_dst = BKE_libblock_copy(bmain, &gpd_src->id); - } - - /* copy layers */ - BLI_listbase_clear(&gpd_dst->layers); - for (gpl_src = gpd_src->layers.first; gpl_src; gpl_src = gpl_src->next) { - /* make a copy of source layer and its data */ - gpl_dst = BKE_gpencil_layer_duplicate(gpl_src); - BLI_addtail(&gpd_dst->layers, gpl_dst); - } - if (!internal_copy) { - /* copy palettes */ - bGPDpalette *palette_src, *palette_dst; - BLI_listbase_clear(&gpd_dst->palettes); - for (palette_src = gpd_src->palettes.first; palette_src; palette_src = palette_src->next) { - palette_dst = BKE_gpencil_palette_duplicate(palette_src); - BLI_addtail(&gpd_dst->palettes, palette_dst); - } + bGPdata *gpd_copy; + BKE_id_copy_ex(bmain, &gpd_src->id, (ID **)&gpd_copy, 0, false); + return gpd_copy; } - - /* return new */ - return gpd_dst; } void BKE_gpencil_make_local(Main *bmain, bGPdata *gpd, const bool lib_local) diff --git a/source/blender/blenkernel/intern/group.c b/source/blender/blenkernel/intern/group.c index 94a341de366..5ccbe9f7054 100644 --- a/source/blender/blenkernel/intern/group.c +++ b/source/blender/blenkernel/intern/group.c @@ -77,7 +77,7 @@ Group *BKE_group_add(Main *bmain, const char *name) { Group *group; - group = BKE_libblock_alloc(bmain, ID_GR, name); + group = BKE_libblock_alloc(bmain, ID_GR, name, 0); id_us_min(&group->id); id_us_ensure_real(&group->id); group->layer = (1 << 20) - 1; @@ -87,19 +87,32 @@ Group *BKE_group_add(Main *bmain, const char *name) return group; } -Group *BKE_group_copy(Main *bmain, const Group *group) +/** + * Only copy internal data of Group ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_group_copy_data(Main *UNUSED(bmain), Group *group_dst, const Group *group_src, const int flag) { - Group *groupn; - - groupn = BKE_libblock_copy(bmain, &group->id); - BLI_duplicatelist(&groupn->gobject, &group->gobject); + BLI_duplicatelist(&group_dst->gobject, &group_src->gobject); /* Do not copy group's preview (same behavior as for objects). */ - groupn->preview = NULL; - - BKE_id_copy_ensure_local(bmain, &group->id, &groupn->id); + if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0 && false) { /* XXX TODO temp hack */ + BKE_previewimg_id_copy(&group_dst->id, &group_src->id); + } + else { + group_dst->preview = NULL; + } +} - return groupn; +Group *BKE_group_copy(Main *bmain, const Group *group) +{ + Group *group_copy; + BKE_id_copy_ex(bmain, &group->id, (ID **)&group_copy, 0, false); + return group_copy; } void BKE_group_make_local(Main *bmain, Group *group, const bool lib_local) @@ -314,7 +327,7 @@ static void group_replaces_nla(Object *parent, Object *target, char mode) * you can draw everything, leaves tags in objects to signal it needs further updating */ /* note: does not work for derivedmesh and render... it recreates all again in convertblender.c */ -void BKE_group_handle_recalc_and_update(struct EvaluationContext *eval_ctx, Scene *scene, Object *UNUSED(parent), Group *group) +void BKE_group_handle_recalc_and_update(const struct EvaluationContext *eval_ctx, Scene *scene, Object *UNUSED(parent), Group *group) { GroupObject *go; diff --git a/source/blender/blenkernel/intern/icons.c b/source/blender/blenkernel/intern/icons.c index db50a04348b..a407fd0bae8 100644 --- a/source/blender/blenkernel/intern/icons.c +++ b/source/blender/blenkernel/intern/icons.c @@ -255,6 +255,8 @@ PreviewImage **BKE_previewimg_id_get_p(const ID *id) ID_PRV_CASE(ID_SCE, Scene); ID_PRV_CASE(ID_SCR, bScreen); #undef ID_PRV_CASE + default: + break; } return NULL; diff --git a/source/blender/blenkernel/intern/idprop.c b/source/blender/blenkernel/intern/idprop.c index a3bd15252cb..b00a62a1a87 100644 --- a/source/blender/blenkernel/intern/idprop.c +++ b/source/blender/blenkernel/intern/idprop.c @@ -90,7 +90,7 @@ IDProperty *IDP_NewIDPArray(const char *name) return prop; } -IDProperty *IDP_CopyIDPArray(const IDProperty *array) +IDProperty *IDP_CopyIDPArray(const IDProperty *array, const int flag) { /* don't use MEM_dupallocN because this may be part of an array */ IDProperty *narray, *tmp; @@ -109,7 +109,7 @@ IDProperty *IDP_CopyIDPArray(const IDProperty *array) * then free it. this makes for more maintainable * code than simply reimplementing the copy functions * in this loop.*/ - tmp = IDP_CopyProperty(GETPROP(narray, i)); + tmp = IDP_CopyProperty_ex(GETPROP(narray, i), flag); memcpy(GETPROP(narray, i), tmp, sizeof(IDProperty)); MEM_freeN(tmp); } @@ -285,9 +285,9 @@ void IDP_FreeArray(IDProperty *prop) } -static IDProperty *idp_generic_copy(const IDProperty *prop) +static IDProperty *idp_generic_copy(const IDProperty *prop, const int UNUSED(flag)) { - IDProperty *newp = MEM_callocN(sizeof(IDProperty), "IDProperty array dup"); + IDProperty *newp = MEM_callocN(sizeof(IDProperty), __func__); BLI_strncpy(newp->name, prop->name, MAX_IDPROP_NAME); newp->type = prop->type; @@ -298,9 +298,9 @@ static IDProperty *idp_generic_copy(const IDProperty *prop) return newp; } -static IDProperty *IDP_CopyArray(const IDProperty *prop) +static IDProperty *IDP_CopyArray(const IDProperty *prop, const int flag) { - IDProperty *newp = idp_generic_copy(prop); + IDProperty *newp = idp_generic_copy(prop, flag); if (prop->data.pointer) { newp->data.pointer = MEM_dupallocN(prop->data.pointer); @@ -310,7 +310,7 @@ static IDProperty *IDP_CopyArray(const IDProperty *prop) int a; for (a = 0; a < prop->len; a++) - array[a] = IDP_CopyProperty(array[a]); + array[a] = IDP_CopyProperty_ex(array[a], flag); } } newp->len = prop->len; @@ -363,12 +363,12 @@ IDProperty *IDP_NewString(const char *st, const char *name, int maxlen) return prop; } -static IDProperty *IDP_CopyString(const IDProperty *prop) +static IDProperty *IDP_CopyString(const IDProperty *prop, const int flag) { IDProperty *newp; BLI_assert(prop->type == IDP_STRING); - newp = idp_generic_copy(prop); + newp = idp_generic_copy(prop, flag); if (prop->data.pointer) newp->data.pointer = MEM_dupallocN(prop->data.pointer); @@ -442,15 +442,17 @@ void IDP_FreeString(IDProperty *prop) /** \name IDProperty ID API * \{ */ -static IDProperty *IDP_CopyID(const IDProperty *prop) +static IDProperty *IDP_CopyID(const IDProperty *prop, const int flag) { IDProperty *newp; BLI_assert(prop->type == IDP_ID); - newp = idp_generic_copy(prop); + newp = idp_generic_copy(prop, flag); newp->data.pointer = prop->data.pointer; - id_us_plus(IDP_Id(newp)); + if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) { + id_us_plus(IDP_Id(newp)); + } return newp; } @@ -467,17 +469,17 @@ static IDProperty *IDP_CopyID(const IDProperty *prop) /** * Checks if a property with the same name as prop exists, and if so replaces it. */ -static IDProperty *IDP_CopyGroup(const IDProperty *prop) +static IDProperty *IDP_CopyGroup(const IDProperty *prop, const int flag) { IDProperty *newp, *link; BLI_assert(prop->type == IDP_GROUP); - newp = idp_generic_copy(prop); + newp = idp_generic_copy(prop, flag); newp->len = prop->len; newp->subtype = prop->subtype; for (link = prop->data.group.first; link; link = link->next) { - BLI_addtail(&newp->data.group, IDP_CopyProperty(link)); + BLI_addtail(&newp->data.group, IDP_CopyProperty_ex(link, flag)); } return newp; @@ -603,7 +605,7 @@ void IDP_ReplaceInGroup(IDProperty *group, IDProperty *prop) * If a property is missing in \a dest, add it. * Do it recursively. */ -void IDP_MergeGroup(IDProperty *dest, const IDProperty *src, const bool do_overwrite) +void IDP_MergeGroup_ex(IDProperty *dest, const IDProperty *src, const bool do_overwrite, const int flag) { IDProperty *prop; @@ -616,12 +618,12 @@ void IDP_MergeGroup(IDProperty *dest, const IDProperty *src, const bool do_overw IDProperty *prop_exist = IDP_GetPropertyFromGroup(dest, prop->name); if (prop_exist != NULL) { - IDP_MergeGroup(prop_exist, prop, do_overwrite); + IDP_MergeGroup_ex(prop_exist, prop, do_overwrite, flag); continue; } } - IDProperty *copy = IDP_CopyProperty(prop); + IDProperty *copy = IDP_CopyProperty_ex(prop, flag); IDP_ReplaceInGroup(dest, copy); } } @@ -630,12 +632,12 @@ void IDP_MergeGroup(IDProperty *dest, const IDProperty *src, const bool do_overw IDProperty *prop_exist = IDP_GetPropertyFromGroup(dest, prop->name); if (prop_exist != NULL) { if (prop->type == IDP_GROUP) { - IDP_MergeGroup(prop_exist, prop, do_overwrite); + IDP_MergeGroup_ex(prop_exist, prop, do_overwrite, flag); continue; } } else { - IDProperty *copy = IDP_CopyProperty(prop); + IDProperty *copy = IDP_CopyProperty_ex(prop, flag); dest->len++; BLI_addtail(&dest->data.group, copy); } @@ -644,6 +646,15 @@ void IDP_MergeGroup(IDProperty *dest, const IDProperty *src, const bool do_overw } /** + * If a property is missing in \a dest, add it. + * Do it recursively. + */ +void IDP_MergeGroup(IDProperty *dest, const IDProperty *src, const bool do_overwrite) +{ + IDP_MergeGroup_ex(dest, src, do_overwrite, 0); +} + +/** * This function has a sanity check to make sure ID properties with the same name don't * get added to the group. * @@ -748,18 +759,23 @@ static void IDP_FreeGroup(IDProperty *prop, const bool do_id_user) /** \name IDProperty Main API * \{ */ -IDProperty *IDP_CopyProperty(const IDProperty *prop) +IDProperty *IDP_CopyProperty_ex(const IDProperty *prop, const int flag) { switch (prop->type) { - case IDP_GROUP: return IDP_CopyGroup(prop); - case IDP_STRING: return IDP_CopyString(prop); - case IDP_ID: return IDP_CopyID(prop); - case IDP_ARRAY: return IDP_CopyArray(prop); - case IDP_IDPARRAY: return IDP_CopyIDPArray(prop); - default: return idp_generic_copy(prop); + case IDP_GROUP: return IDP_CopyGroup(prop, flag); + case IDP_STRING: return IDP_CopyString(prop, flag); + case IDP_ID: return IDP_CopyID(prop, flag); + case IDP_ARRAY: return IDP_CopyArray(prop, flag); + case IDP_IDPARRAY: return IDP_CopyIDPArray(prop, flag); + default: return idp_generic_copy(prop, flag); } } +IDProperty *IDP_CopyProperty(const IDProperty *prop) +{ + return IDP_CopyProperty_ex(prop, 0); +} + /* Updates ID pointers after an object has been copied */ /* TODO Nuke this once its only user has been correctly converted to use generic ID management from BKE_library! */ void IDP_RelinkProperty(struct IDProperty *prop) diff --git a/source/blender/blenkernel/intern/image.c b/source/blender/blenkernel/intern/image.c index 2915e9d715d..33a690b32c6 100644 --- a/source/blender/blenkernel/intern/image.c +++ b/source/blender/blenkernel/intern/image.c @@ -304,8 +304,11 @@ static void image_free_anims(Image *ima) * Simply free the image data from memory, * on display the image can load again (except for render buffers). */ -void BKE_image_free_buffers(Image *ima) +void BKE_image_free_buffers_ex(Image *ima, bool do_lock) { + if (do_lock) { + BLI_spin_lock(&image_spin); + } image_free_cached_frames(ima); image_free_anims(ima); @@ -324,6 +327,15 @@ void BKE_image_free_buffers(Image *ima) } ima->ok = IMA_OK; + + if (do_lock) { + BLI_spin_unlock(&image_spin); + } +} + +void BKE_image_free_buffers(Image *ima) +{ + BKE_image_free_buffers_ex(ima, false); } /** Free (or release) any data used by this image (does not free the image itself). */ @@ -383,7 +395,7 @@ static Image *image_alloc(Main *bmain, const char *name, short source, short typ { Image *ima; - ima = BKE_libblock_alloc(bmain, ID_IM, name); + ima = BKE_libblock_alloc(bmain, ID_IM, name, 0); if (ima) { image_init(ima, source, type); } @@ -434,39 +446,53 @@ static void copy_image_packedfiles(ListBase *lb_dst, const ListBase *lb_src) } } -/* empty image block, of similar type and filename */ -Image *BKE_image_copy(Main *bmain, const Image *ima) +/** + * Only copy internal data of Image ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_image_copy_data(Main *UNUSED(bmain), Image *ima_dst, const Image *ima_src, const int flag) { - Image *nima = image_alloc(bmain, ima->id.name + 2, ima->source, ima->type); + BKE_color_managed_colorspace_settings_copy(&ima_dst->colorspace_settings, &ima_src->colorspace_settings); - BLI_strncpy(nima->name, ima->name, sizeof(ima->name)); + copy_image_packedfiles(&ima_dst->packedfiles, &ima_src->packedfiles); - nima->flag = ima->flag; - nima->tpageflag = ima->tpageflag; + ima_dst->stereo3d_format = MEM_dupallocN(ima_src->stereo3d_format); + BLI_duplicatelist(&ima_dst->views, &ima_src->views); - nima->gen_x = ima->gen_x; - nima->gen_y = ima->gen_y; - nima->gen_type = ima->gen_type; - copy_v4_v4(nima->gen_color, ima->gen_color); - - nima->animspeed = ima->animspeed; - - nima->aspx = ima->aspx; - nima->aspy = ima->aspy; - - BKE_color_managed_colorspace_settings_copy(&nima->colorspace_settings, &ima->colorspace_settings); - - copy_image_packedfiles(&nima->packedfiles, &ima->packedfiles); + /* Cleanup stuff that cannot be copied. */ + ima_dst->cache = NULL; + ima_dst->rr = NULL; + for (int i = 0; i < IMA_MAX_RENDER_SLOT; i++) { + ima_dst->renders[i] = NULL; + } - /* nima->stere3d_format is already allocated by image_alloc... */ - *nima->stereo3d_format = *ima->stereo3d_format; - BLI_duplicatelist(&nima->views, &ima->views); + BLI_listbase_clear(&ima_dst->anims); - BKE_previewimg_id_copy(&nima->id, &ima->id); + ima_dst->totbind = 0; + for (int i = 0; i < TEXTARGET_COUNT; i++) { + ima_dst->bindcode[i] = 0; + ima_dst->gputexture[i] = NULL; + } + ima_dst->repbind = NULL; - BKE_id_copy_ensure_local(bmain, &ima->id, &nima->id); + if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0) { + BKE_previewimg_id_copy(&ima_dst->id, &ima_src->id); + } + else { + ima_dst->preview = NULL; + } +} - return nima; +/* empty image block, of similar type and filename */ +Image *BKE_image_copy(Main *bmain, const Image *ima) +{ + Image *ima_copy; + BKE_id_copy_ex(bmain, &ima->id, (ID **)&ima_copy, 0, false); + return ima_copy; } void BKE_image_make_local(Main *bmain, Image *ima, const bool lib_local) @@ -1178,7 +1204,6 @@ bool BKE_imtype_is_movie(const char imtype) switch (imtype) { case R_IMF_IMTYPE_AVIRAW: case R_IMF_IMTYPE_AVIJPEG: - case R_IMF_IMTYPE_QUICKTIME: case R_IMF_IMTYPE_FFMPEG: case R_IMF_IMTYPE_H264: case R_IMF_IMTYPE_THEORA: @@ -1250,7 +1275,6 @@ char BKE_imtype_valid_channels(const char imtype, bool write_file) case R_IMF_IMTYPE_MULTILAYER: case R_IMF_IMTYPE_DDS: case R_IMF_IMTYPE_JP2: - case R_IMF_IMTYPE_QUICKTIME: case R_IMF_IMTYPE_DPX: chan_flag |= IMA_CHAN_FLAG_ALPHA; break; @@ -1313,7 +1337,6 @@ char BKE_imtype_from_arg(const char *imtype_arg) else if (STREQ(imtype_arg, "AVIRAW")) return R_IMF_IMTYPE_AVIRAW; else if (STREQ(imtype_arg, "AVIJPEG")) return R_IMF_IMTYPE_AVIJPEG; else if (STREQ(imtype_arg, "PNG")) return R_IMF_IMTYPE_PNG; - else if (STREQ(imtype_arg, "QUICKTIME")) return R_IMF_IMTYPE_QUICKTIME; else if (STREQ(imtype_arg, "BMP")) return R_IMF_IMTYPE_BMP; #ifdef WITH_HDR else if (STREQ(imtype_arg, "HDR")) return R_IMF_IMTYPE_RADHDR; @@ -1424,7 +1447,7 @@ static bool do_add_image_extension(char *string, const char imtype, const ImageF } } #endif - else { // R_IMF_IMTYPE_AVIRAW, R_IMF_IMTYPE_AVIJPEG, R_IMF_IMTYPE_JPEG90, R_IMF_IMTYPE_QUICKTIME etc + else { // R_IMF_IMTYPE_AVIRAW, R_IMF_IMTYPE_AVIJPEG, R_IMF_IMTYPE_JPEG90 etc if (!(BLI_testextensie_n(string, extension_test = ".jpg", ".jpeg", NULL))) extension = extension_test; } @@ -1432,9 +1455,7 @@ static bool do_add_image_extension(char *string, const char imtype, const ImageF if (extension) { /* prefer this in many cases to avoid .png.tga, but in certain cases it breaks */ /* remove any other known image extension */ - if (BLI_testextensie_array(string, imb_ext_image) || - (G.have_quicktime && BLI_testextensie_array(string, imb_ext_image_qt))) - { + if (BLI_testextensie_array(string, imb_ext_image)) { return BLI_replace_extension(string, FILE_MAX, extension); } else { @@ -1710,7 +1731,7 @@ static void stampdata(Scene *scene, Object *camera, StampData *stamp_data, int d } { - Render *re = RE_GetRender(scene->id.name); + Render *re = RE_GetSceneRender(scene); RenderStats *stats = re ? RE_GetStats(re) : NULL; if (stats && (scene->r.stamp & R_STAMP_RENDERTIME)) { @@ -2905,7 +2926,7 @@ RenderResult *BKE_image_acquire_renderresult(Scene *scene, Image *ima) } else if (ima->type == IMA_TYPE_R_RESULT) { if (ima->render_slot == ima->last_render_slot) - rr = RE_AcquireResultRead(RE_GetRender(scene->id.name)); + rr = RE_AcquireResultRead(RE_GetSceneRender(scene)); else rr = ima->renders[ima->render_slot]; @@ -2923,7 +2944,7 @@ void BKE_image_release_renderresult(Scene *scene, Image *ima) } else if (ima->type == IMA_TYPE_R_RESULT) { if (ima->render_slot == ima->last_render_slot) - RE_ReleaseResult(RE_GetRender(scene->id.name)); + RE_ReleaseResult(RE_GetSceneRender(scene)); } } @@ -2943,7 +2964,7 @@ void BKE_image_backup_render(Scene *scene, Image *ima, bool free_current_slot) { /* called right before rendering, ima->renders contains render * result pointers for everything but the current render */ - Render *re = RE_GetRender(scene->id.name); + Render *re = RE_GetSceneRender(scene); int slot = ima->render_slot, last = ima->last_render_slot; if (slot != last) { @@ -3668,7 +3689,7 @@ static ImBuf *image_get_render_result(Image *ima, ImageUser *iuser, void **r_loc if (!r_lock) return NULL; - re = RE_GetRender(iuser->scene->id.name); + re = RE_GetSceneRender(iuser->scene); channels = 4; layer = iuser->layer; diff --git a/source/blender/blenkernel/intern/image_gen.c b/source/blender/blenkernel/intern/image_gen.c index 2c8399adece..1f1f4c9d341 100644 --- a/source/blender/blenkernel/intern/image_gen.c +++ b/source/blender/blenkernel/intern/image_gen.c @@ -132,7 +132,7 @@ static void image_buf_fill_checker_slice(unsigned char *rect, float hsv[3] = {0.0f, 0.9f, 0.9f}; float rgb[3]; - float dark_linear_color, bright_linear_color; + float dark_linear_color = 0.0f, bright_linear_color = 0.0f; if (rect_float != NULL) { dark_linear_color = srgb_to_linearrgb(0.25f); bright_linear_color = srgb_to_linearrgb(0.58f); diff --git a/source/blender/blenkernel/intern/key.c b/source/blender/blenkernel/intern/key.c index 98b251294ae..fd42556067e 100644 --- a/source/blender/blenkernel/intern/key.c +++ b/source/blender/blenkernel/intern/key.c @@ -107,7 +107,7 @@ Key *BKE_key_add(ID *id) /* common function */ Key *key; char *el; - key = BKE_libblock_alloc(G.main, ID_KE, "Key"); + key = BKE_libblock_alloc(G.main, ID_KE, "Key", 0); key->type = KEY_NORMAL; key->from = id; @@ -146,36 +146,48 @@ Key *BKE_key_add(ID *id) /* common function */ key->elemsize = 16; break; + + default: + break; } return key; } -Key *BKE_key_copy(Main *bmain, const Key *key) +/** + * Only copy internal data of ShapeKey ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_key_copy_data(Main *UNUSED(bmain), Key *key_dst, const Key *key_src, const int UNUSED(flag)) { - Key *keyn; - KeyBlock *kbn, *kb; - - keyn = BKE_libblock_copy(bmain, &key->id); - - BLI_duplicatelist(&keyn->block, &key->block); - - kb = key->block.first; - kbn = keyn->block.first; - while (kbn) { - - if (kbn->data) kbn->data = MEM_dupallocN(kbn->data); - if (kb == key->refkey) keyn->refkey = kbn; - - kbn = kbn->next; - kb = kb->next; - } + BLI_duplicatelist(&key_dst->block, &key_src->block); - BKE_id_copy_ensure_local(bmain, &key->id, &keyn->id); + KeyBlock *kb_dst, *kb_src; + for (kb_src = key_src->block.first, kb_dst = key_dst->block.first; + kb_dst; + kb_src = kb_src->next, kb_dst = kb_dst->next) + { + if (kb_dst->data) { + kb_dst->data = MEM_dupallocN(kb_dst->data); + } + if (kb_src == key_src->refkey) { + key_dst->refkey = kb_dst; + } + } +} - return keyn; +Key *BKE_key_copy(Main *bmain, const Key *key) +{ + Key *key_copy; + BKE_id_copy_ex(bmain, &key->id, (ID **)&key_copy, 0, false); + return key_copy; } +/* XXX TODO get rid of this! */ Key *BKE_key_copy_nolib(Key *key) { Key *keyn; @@ -1397,6 +1409,8 @@ Key **BKE_key_from_id_p(ID *id) Lattice *lt = (Lattice *)id; return <->key; } + default: + break; } return NULL; diff --git a/source/blender/blenkernel/intern/lamp.c b/source/blender/blenkernel/intern/lamp.c index 4a3a1c50195..3d0d5f87f15 100644 --- a/source/blender/blenkernel/intern/lamp.c +++ b/source/blender/blenkernel/intern/lamp.c @@ -69,7 +69,7 @@ void BKE_lamp_init(Lamp *la) la->bufsize = 512; la->clipsta = 0.5f; la->clipend = 40.0f; - la->bleedexp = 120.0f; + la->bleedexp = 2.5f; la->samp = 3; la->bias = 1.0f; la->soft = 3.0f; @@ -102,6 +102,14 @@ void BKE_lamp_init(Lamp *la) la->sky_colorspace = BLI_XYZ_CIE; la->sky_exposure = 1.0f; la->shadow_frustum_size = 10.0f; + la->cascade_max_dist = 1000.0f; + la->cascade_count = 4; + la->cascade_exponent = 0.8f; + la->cascade_fade = 0.1f; + la->contact_dist = 1.0f; + la->contact_bias = 0.03f; + la->contact_spread = 0.2f; + la->contact_thickness = 0.5f; curvemapping_initialize(la->curfalloff); } @@ -110,42 +118,60 @@ Lamp *BKE_lamp_add(Main *bmain, const char *name) { Lamp *la; - la = BKE_libblock_alloc(bmain, ID_LA, name); + la = BKE_libblock_alloc(bmain, ID_LA, name, 0); BKE_lamp_init(la); return la; } -Lamp *BKE_lamp_copy(Main *bmain, const Lamp *la) +/** + * Only copy internal data of Lamp ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_lamp_copy_data(Main *bmain, Lamp *la_dst, const Lamp *la_src, const int flag) { - Lamp *lan; - int a; - - lan = BKE_libblock_copy(bmain, &la->id); - - for (a = 0; a < MAX_MTEX; a++) { - if (lan->mtex[a]) { - lan->mtex[a] = MEM_mallocN(sizeof(MTex), "copylamptex"); - memcpy(lan->mtex[a], la->mtex[a], sizeof(MTex)); - id_us_plus((ID *)lan->mtex[a]->tex); + for (int a = 0; a < MAX_MTEX; a++) { + if (la_dst->mtex[a]) { + la_dst->mtex[a] = MEM_mallocN(sizeof(*la_dst->mtex[a]), __func__); + *la_dst->mtex[a] = *la_src->mtex[a]; } } - - lan->curfalloff = curvemapping_copy(la->curfalloff); - if (la->nodetree) - lan->nodetree = ntreeCopyTree(bmain, la->nodetree); + la_dst->curfalloff = curvemapping_copy(la_src->curfalloff); - BKE_previewimg_id_copy(&lan->id, &la->id); + if (la_src->nodetree) { + BKE_id_copy_ex(bmain, (ID *)la_src->nodetree, (ID **)&la_dst->nodetree, flag, false); + } - BKE_id_copy_ensure_local(bmain, &la->id, &lan->id); + if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0) { + BKE_previewimg_id_copy(&la_dst->id, &la_src->id); + } + else { + la_dst->preview = NULL; + } +} - return lan; +Lamp *BKE_lamp_copy(Main *bmain, const Lamp *la) +{ + Lamp *la_copy; + BKE_id_copy_ex(bmain, &la->id, (ID **)&la_copy, 0, false); + return la_copy; } Lamp *localize_lamp(Lamp *la) { + /* TODO replace with something like + * Lamp *la_copy; + * BKE_id_copy_ex(bmain, &la->id, (ID **)&la_copy, LIB_ID_COPY_NO_MAIN | LIB_ID_COPY_NO_PREVIEW | LIB_ID_COPY_NO_USER_REFCOUNT, false); + * return la_copy; + * + * ... Once f*** nodes are fully converted to that too :( */ + Lamp *lan; int a; diff --git a/source/blender/blenkernel/intern/lattice.c b/source/blender/blenkernel/intern/lattice.c index c6773d98adf..0f26f53113f 100644 --- a/source/blender/blenkernel/intern/lattice.c +++ b/source/blender/blenkernel/intern/lattice.c @@ -269,39 +269,46 @@ Lattice *BKE_lattice_add(Main *bmain, const char *name) { Lattice *lt; - lt = BKE_libblock_alloc(bmain, ID_LT, name); + lt = BKE_libblock_alloc(bmain, ID_LT, name, 0); BKE_lattice_init(lt); return lt; } -Lattice *BKE_lattice_copy(Main *bmain, const Lattice *lt) +/** + * Only copy internal data of Lattice ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_lattice_copy_data(Main *bmain, Lattice *lt_dst, const Lattice *lt_src, const int flag) { - Lattice *ltn; + lt_dst->def = MEM_dupallocN(lt_src->def); - ltn = BKE_libblock_copy(bmain, <->id); - ltn->def = MEM_dupallocN(lt->def); - - if (lt->key) { - ltn->key = BKE_key_copy(bmain, ltn->key); - ltn->key->from = (ID *)ltn; - } - - if (lt->dvert) { - int tot = lt->pntsu * lt->pntsv * lt->pntsw; - ltn->dvert = MEM_mallocN(sizeof(MDeformVert) * tot, "Lattice MDeformVert"); - BKE_defvert_array_copy(ltn->dvert, lt->dvert, tot); + if (lt_src->key) { + BKE_id_copy_ex(bmain, <_src->key->id, (ID **)<_dst->key, flag, false); } - ltn->editlatt = NULL; + if (lt_src->dvert) { + int tot = lt_src->pntsu * lt_src->pntsv * lt_src->pntsw; + lt_dst->dvert = MEM_mallocN(sizeof(MDeformVert) * tot, "Lattice MDeformVert"); + BKE_defvert_array_copy(lt_dst->dvert, lt_src->dvert, tot); + } - BKE_id_copy_ensure_local(bmain, <->id, <n->id); + lt_dst->editlatt = NULL; +} - return ltn; +Lattice *BKE_lattice_copy(Main *bmain, const Lattice *lt) +{ + Lattice *lt_copy; + BKE_id_copy_ex(bmain, <->id, (ID **)<_copy, 0, false); + return lt_copy; } -/** Free (or release) any data used by this lattice (does not free the lattice itself). */ + /** Free (or release) any data used by this lattice (does not free the lattice itself). */ void BKE_lattice_free(Lattice *lt) { BKE_animdata_free(<->id, false); @@ -596,7 +603,7 @@ static bool where_on_path_deform(Object *ob, float ctime, float vec[4], float di /* co: local coord, result local too */ /* returns quaternion for rotation, using cd->no_rot_axis */ /* axis is using another define!!! */ -static bool calc_curve_deform(Scene *scene, Object *par, float co[3], +static bool calc_curve_deform(const struct EvaluationContext *eval_ctx, Scene *scene, Object *par, float co[3], const short axis, CurveDeform *cd, float r_quat[4]) { Curve *cu = par->data; @@ -607,7 +614,7 @@ static bool calc_curve_deform(Scene *scene, Object *par, float co[3], /* to be sure, mostly after file load, also cyclic dependencies */ #ifdef CYCLIC_DEPENDENCY_WORKAROUND if (par->curve_cache == NULL) { - BKE_displist_make_curveTypes(scene, par, false); + BKE_displist_make_curveTypes(eval_ctx, scene, par, false); } #endif @@ -700,7 +707,8 @@ static bool calc_curve_deform(Scene *scene, Object *par, float co[3], } void curve_deform_verts( - Scene *scene, Object *cuOb, Object *target, DerivedMesh *dm, float (*vertexCos)[3], + const struct EvaluationContext *eval_ctx, Scene *scene, + Object *cuOb, Object *target, DerivedMesh *dm, float (*vertexCos)[3], int numVerts, const char *vgroup, short defaxis) { Curve *cu; @@ -759,7 +767,7 @@ void curve_deform_verts( if (weight > 0.0f) { mul_m4_v3(cd.curvespace, vertexCos[a]); copy_v3_v3(vec, vertexCos[a]); - calc_curve_deform(scene, cuOb, vec, defaxis, &cd, NULL); + calc_curve_deform(eval_ctx, scene, cuOb, vec, defaxis, &cd, NULL); interp_v3_v3v3(vertexCos[a], vertexCos[a], vec, weight); mul_m4_v3(cd.objectspace, vertexCos[a]); } @@ -782,7 +790,7 @@ void curve_deform_verts( if (weight > 0.0f) { /* already in 'cd.curvespace', prev for loop */ copy_v3_v3(vec, vertexCos[a]); - calc_curve_deform(scene, cuOb, vec, defaxis, &cd, NULL); + calc_curve_deform(eval_ctx, scene, cuOb, vec, defaxis, &cd, NULL); interp_v3_v3v3(vertexCos[a], vertexCos[a], vec, weight); mul_m4_v3(cd.objectspace, vertexCos[a]); } @@ -793,7 +801,7 @@ void curve_deform_verts( if (cu->flag & CU_DEFORM_BOUNDS_OFF) { for (a = 0; a < numVerts; a++) { mul_m4_v3(cd.curvespace, vertexCos[a]); - calc_curve_deform(scene, cuOb, vertexCos[a], defaxis, &cd, NULL); + calc_curve_deform(eval_ctx, scene, cuOb, vertexCos[a], defaxis, &cd, NULL); mul_m4_v3(cd.objectspace, vertexCos[a]); } } @@ -808,7 +816,7 @@ void curve_deform_verts( for (a = 0; a < numVerts; a++) { /* already in 'cd.curvespace', prev for loop */ - calc_curve_deform(scene, cuOb, vertexCos[a], defaxis, &cd, NULL); + calc_curve_deform(eval_ctx, scene, cuOb, vertexCos[a], defaxis, &cd, NULL); mul_m4_v3(cd.objectspace, vertexCos[a]); } } @@ -818,7 +826,7 @@ void curve_deform_verts( /* input vec and orco = local coord in armature space */ /* orco is original not-animated or deformed reference point */ /* result written in vec and mat */ -void curve_deform_vector(Scene *scene, Object *cuOb, Object *target, +void curve_deform_vector(const struct EvaluationContext *eval_ctx, Scene *scene, Object *cuOb, Object *target, float orco[3], float vec[3], float mat[3][3], int no_rot_axis) { CurveDeform cd; @@ -837,7 +845,7 @@ void curve_deform_vector(Scene *scene, Object *cuOb, Object *target, mul_m4_v3(cd.curvespace, vec); - if (calc_curve_deform(scene, cuOb, vec, target->trackflag, &cd, quat)) { + if (calc_curve_deform(eval_ctx, scene, cuOb, vec, target->trackflag, &cd, quat)) { float qmat[3][3]; quat_to_mat3(qmat, quat); @@ -1027,7 +1035,7 @@ void BKE_lattice_vertexcos_apply(struct Object *ob, float (*vertexCos)[3]) } } -void BKE_lattice_modifiers_calc(Scene *scene, Object *ob) +void BKE_lattice_modifiers_calc(const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob) { Lattice *lt = ob->data; VirtualModifierData virtualModifierData; @@ -1054,7 +1062,7 @@ void BKE_lattice_modifiers_calc(Scene *scene, Object *ob) if (mti->type != eModifierTypeType_OnlyDeform) continue; if (!vertexCos) vertexCos = BKE_lattice_vertexcos_get(ob, &numVerts); - mti->deformVerts(md, ob, NULL, vertexCos, numVerts, 0); + mti->deformVerts(md, eval_ctx, ob, NULL, vertexCos, numVerts, 0); } /* always displist to make this work like derivedmesh */ @@ -1228,7 +1236,7 @@ void BKE_lattice_translate(Lattice *lt, float offset[3], bool do_keys) /* **** Depsgraph evaluation **** */ -void BKE_lattice_eval_geometry(struct EvaluationContext *UNUSED(eval_ctx), +void BKE_lattice_eval_geometry(const struct EvaluationContext *UNUSED(eval_ctx), Lattice *UNUSED(latt)) { } @@ -1248,4 +1256,4 @@ void BKE_lattice_batch_cache_free(Lattice *lt) if (lt->batch_cache) { BKE_lattice_batch_cache_free_cb(lt); } -}
\ No newline at end of file +} diff --git a/source/blender/blenkernel/intern/layer.c b/source/blender/blenkernel/intern/layer.c index 78954e56f8c..f20b96b8bc7 100644 --- a/source/blender/blenkernel/intern/layer.c +++ b/source/blender/blenkernel/intern/layer.c @@ -57,6 +57,7 @@ /* prototype */ struct EngineSettingsCB_Type; static void layer_collection_free(SceneLayer *sl, LayerCollection *lc); +static void layer_collection_objects_populate(SceneLayer *sl, LayerCollection *lc, ListBase *objects); static LayerCollection *layer_collection_add(SceneLayer *sl, LayerCollection *parent, SceneCollection *sc); static LayerCollection *find_layer_collection_by_scene_collection(LayerCollection *lc, const SceneCollection *sc); static IDProperty *collection_engine_settings_create(struct EngineSettingsCB_Type *ces_type, const bool populate); @@ -69,9 +70,9 @@ static void object_bases_iterator_next(BLI_Iterator *iter, const int flag); /** * Returns the SceneLayer to be used for rendering - * Most of the time BKE_scene_layer_context_active should be used instead + * Most of the time BKE_scene_layer_from_workspace_get should be used instead */ -SceneLayer *BKE_scene_layer_render_active(const Scene *scene) +SceneLayer *BKE_scene_layer_from_scene_get(const Scene *scene) { SceneLayer *sl = BLI_findlink(&scene->render_layers, scene->active_layer); BLI_assert(sl); @@ -81,23 +82,18 @@ SceneLayer *BKE_scene_layer_render_active(const Scene *scene) /** * Returns the SceneLayer to be used for drawing, outliner, and other context related areas. */ -SceneLayer *BKE_scene_layer_context_active_ex(const Main *bmain, const Scene *UNUSED(scene)) +SceneLayer *BKE_scene_layer_from_workspace_get(const struct WorkSpace *workspace) { - /* XXX We should really pass the workspace as argument, but would require - * some bigger changes since it's often not available where we call this. - * Just working around this by getting active window from WM for now */ - for (wmWindowManager *wm = bmain->wm.first; wm; wm = wm->id.next) { - /* Called on startup, so 'winactive' may not be set, in that case fall back to first window. */ - wmWindow *win = wm->winactive ? wm->winactive : wm->windows.first; - const WorkSpace *workspace = BKE_workspace_active_get(win->workspace_hook); - return BKE_workspace_render_layer_get(workspace); - } - - return NULL; + return BKE_workspace_render_layer_get(workspace); } -SceneLayer *BKE_scene_layer_context_active(const Scene *scene) + +/** + * This is a placeholder to know which areas of the code need to be addressed for the Workspace changes. + * Never use this, you should either use BKE_scene_layer_workspace_active or get SceneLayer explicitly. + */ +SceneLayer *BKE_scene_layer_context_active_PLACEHOLDER(const Scene *scene) { - return BKE_scene_layer_context_active_ex(G.main, scene); + return BKE_scene_layer_from_scene_get(scene); } /** @@ -293,11 +289,7 @@ static Base *object_base_add(SceneLayer *sl, Object *ob) /* LayerCollection */ -/** - * When freeing the entire SceneLayer at once we don't bother with unref - * otherwise SceneLayer is passed to keep the syncing of the LayerCollection tree - */ -static void layer_collection_free(SceneLayer *sl, LayerCollection *lc) +static void layer_collection_objects_unpopulate(SceneLayer *sl, LayerCollection *lc) { if (sl) { for (LinkData *link = lc->object_bases.first; link; link = link->next) { @@ -306,6 +298,15 @@ static void layer_collection_free(SceneLayer *sl, LayerCollection *lc) } BLI_freelistN(&lc->object_bases); +} + +/** + * When freeing the entire SceneLayer at once we don't bother with unref + * otherwise SceneLayer is passed to keep the syncing of the LayerCollection tree + */ +static void layer_collection_free(SceneLayer *sl, LayerCollection *lc) +{ + layer_collection_objects_unpopulate(sl, lc); BLI_freelistN(&lc->overrides); if (lc->properties) { @@ -357,6 +358,15 @@ static LayerCollection *collection_from_index(ListBase *lb, const int number, in } /** + * Get the collection for a given index + */ +LayerCollection *BKE_layer_collection_from_index(SceneLayer *sl, const int index) +{ + int i = 0; + return collection_from_index(&sl->layer_collections, index, &i); +} + +/** * Get the active collection */ LayerCollection *BKE_layer_collection_get_active(SceneLayer *sl) @@ -802,6 +812,60 @@ void BKE_collection_unlink(SceneLayer *sl, LayerCollection *lc) sl->active_collection = 0; } +/** + * Recursively enable nested collections + */ +static void layer_collection_enable(SceneLayer *sl, LayerCollection *lc) +{ + layer_collection_objects_populate(sl, lc, &lc->scene_collection->objects); + + for (LayerCollection *nlc = lc->layer_collections.first; nlc; nlc = nlc->next) { + layer_collection_enable(sl, nlc); + } +} + +/** + * Enable collection + * Add its objects bases to SceneLayer + * Depsgraph needs to be rebuilt afterwards + */ +void BKE_collection_enable(SceneLayer *sl, LayerCollection *lc) +{ + if ((lc->flag & COLLECTION_DISABLED) == 0) { + return; + } + + lc->flag &= ~COLLECTION_DISABLED; + layer_collection_enable(sl, lc); +} + +/** + * Recursively disable nested collections + */ +static void layer_collection_disable(SceneLayer *sl, LayerCollection *lc) +{ + layer_collection_objects_unpopulate(sl, lc); + + for (LayerCollection *nlc = lc->layer_collections.first; nlc; nlc = nlc->next) { + layer_collection_disable(sl, nlc); + } +} + +/** + * Disable collection + * Remove all its object bases from SceneLayer + * Depsgraph needs to be rebuilt afterwards + */ +void BKE_collection_disable(SceneLayer *sl, LayerCollection *lc) +{ + if ((lc->flag & COLLECTION_DISABLED) != 0) { + return; + } + + lc->flag |= COLLECTION_DISABLED; + layer_collection_disable(sl, lc); +} + static void layer_collection_object_add(SceneLayer *sl, LayerCollection *lc, Object *ob) { Base *base = object_base_add(sl, ob); @@ -1356,7 +1420,7 @@ void BKE_collection_engine_property_add_float_array( val.array.len = array_length; val.array.type = IDP_FLOAT; - IDProperty *idprop= IDP_New(IDP_ARRAY, &val, name); + IDProperty *idprop = IDP_New(IDP_ARRAY, &val, name); memcpy(IDP_Array(idprop), values, sizeof(float) * idprop->len); IDP_AddToGroup(props, idprop); } @@ -1739,10 +1803,10 @@ static void idproperty_reset(IDProperty **props, IDProperty *props_ref) } } -void BKE_layer_eval_layer_collection_pre(struct EvaluationContext *UNUSED(eval_ctx), +void BKE_layer_eval_layer_collection_pre(const struct EvaluationContext *UNUSED(eval_ctx), Scene *scene, SceneLayer *scene_layer) { - DEBUG_PRINT("%s on %s\n", __func__, scene_layer->name); + DEBUG_PRINT("%s on %s (%p)\n", __func__, scene_layer->name, scene_layer); for (Base *base = scene_layer->object_bases.first; base != NULL; base = base->next) { base->flag &= ~(BASE_VISIBLED | BASE_SELECTABLED); idproperty_reset(&base->collection_properties, scene->collection_properties); @@ -1756,14 +1820,16 @@ void BKE_layer_eval_layer_collection_pre(struct EvaluationContext *UNUSED(eval_c scene_layer->flag |= SCENE_LAYER_ENGINE_DIRTY; } -void BKE_layer_eval_layer_collection(struct EvaluationContext *UNUSED(eval_ctx), +void BKE_layer_eval_layer_collection(const struct EvaluationContext *UNUSED(eval_ctx), LayerCollection *layer_collection, LayerCollection *parent_layer_collection) { - DEBUG_PRINT("%s on %s, parent %s\n", + DEBUG_PRINT("%s on %s (%p), parent %s (%p)\n", __func__, layer_collection->scene_collection->name, - (parent_layer_collection != NULL) ? parent_layer_collection->scene_collection->name : "NONE"); + layer_collection->scene_collection, + (parent_layer_collection != NULL) ? parent_layer_collection->scene_collection->name : "NONE", + (parent_layer_collection != NULL) ? parent_layer_collection->scene_collection : NULL); /* visibility */ layer_collection->flag_evaluated = layer_collection->flag; @@ -1794,6 +1860,9 @@ void BKE_layer_eval_layer_collection(struct EvaluationContext *UNUSED(eval_ctx), IDP_MergeGroup(base->collection_properties, layer_collection->properties_evaluated, true); base->flag |= BASE_VISIBLED; } + else { + base->flag &= ~BASE_VISIBLED; + } if (is_selectable) { base->flag |= BASE_SELECTABLED; @@ -1801,10 +1870,10 @@ void BKE_layer_eval_layer_collection(struct EvaluationContext *UNUSED(eval_ctx), } } -void BKE_layer_eval_layer_collection_post(struct EvaluationContext *UNUSED(eval_ctx), +void BKE_layer_eval_layer_collection_post(const struct EvaluationContext *UNUSED(eval_ctx), SceneLayer *scene_layer) { - DEBUG_PRINT("%s on %s\n", __func__, scene_layer->name); + DEBUG_PRINT("%s on %s (%p)\n", __func__, scene_layer->name, scene_layer); /* if base is not selectabled, clear select */ for (Base *base = scene_layer->object_bases.first; base; base = base->next) { if ((base->flag & BASE_SELECTABLED) == 0) { diff --git a/source/blender/blenkernel/intern/library.c b/source/blender/blenkernel/intern/library.c index ed64de9b1aa..71fbb3d726a 100644 --- a/source/blender/blenkernel/intern/library.c +++ b/source/blender/blenkernel/intern/library.c @@ -385,7 +385,6 @@ bool id_make_local(Main *bmain, ID *id, const bool test, const bool lib_local) switch ((ID_Type)GS(id->name)) { case ID_SCE: - /* Partially implemented (has no copy...). */ if (!test) BKE_scene_make_local(bmain, (Scene *)id, lib_local); return true; case ID_OB: @@ -428,14 +427,12 @@ bool id_make_local(Main *bmain, ID *id, const bool test, const bool lib_local) if (!test) BKE_world_make_local(bmain, (World *)id, lib_local); return true; case ID_VF: - /* Partially implemented (has no copy...). */ if (!test) BKE_vfont_make_local(bmain, (VFont *)id, lib_local); return true; case ID_TXT: if (!test) BKE_text_make_local(bmain, (Text *)id, lib_local); return true; case ID_SO: - /* Partially implemented (has no copy...). */ if (!test) BKE_sound_make_local(bmain, (bSound *)id, lib_local); return true; case ID_GR: @@ -493,118 +490,193 @@ bool id_make_local(Main *bmain, ID *id, const bool test, const bool lib_local) return false; } +struct IDCopyLibManagementData { + const ID *id_src; + ID *id_dst; + int flag; +}; + +/* Increases usercount as required, and remap self ID pointers. */ +static int id_copy_libmanagement_cb(void *user_data, ID *UNUSED(id_self), ID **id_pointer, int cb_flag) +{ + struct IDCopyLibManagementData *data = user_data; + ID *id = *id_pointer; + + /* Remap self-references to new copied ID. */ + if (id == data->id_src) { + /* We cannot use id_self here, it is not *always* id_dst (thanks to $£!+@#&/? nodetrees). */ + id = *id_pointer = data->id_dst; + } + + /* Increase used IDs refcount if needed and required. */ + if ((data->flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0 && (cb_flag & IDWALK_CB_USER)) { + id_us_plus(id); + } + + return IDWALK_RET_NOP; +} + /** - * Invokes the appropriate copy method for the block and returns the result in - * newid, unless test. Returns true if the block can be copied. + * Generic entry point for copying a datablock (new API). + * + * \note Copy is only affecting given data-block (no ID used by copied one will be affected, besides usercount). + * There is only one exception, if LIB_ID_COPY_ACTIONS is defined, actions used by animdata will be duplicated. + * + * \note Usercount of new copy is always set to 1. + * + * \param bmain Main database, may be NULL only if LIB_ID_COPY_NO_MAIN is specified. + * \param id Source datablock. + * \param r_newid Pointer to new (copied) ID pointer. + * \param flag Set of copy options, see DNA_ID.h enum for details (leave to zero for default, full copy). + * \param test If set, do not do any copy, just test whether copy is supported. + * \return False when copying that ID type is not supported, true otherwise. */ -bool id_copy(Main *bmain, const ID *id, ID **newid, bool test) +/* XXX TODO remove test thing, *all* IDs should be copyable that way! */ +bool BKE_id_copy_ex(Main *bmain, const ID *id, ID **r_newid, const int flag, const bool test) { - if (!test) { - *newid = NULL; +#define LIB_ID_TYPES_NOCOPY ID_LI, ID_SCR, ID_WM, /* Not supported */ \ + ID_IP /* Deprecated */ + + BLI_assert(test || (r_newid != NULL)); + if (r_newid != NULL) { + *r_newid = NULL; + } + if (id == NULL) { + return false; } - /* conventions: - * - make shallow copy, only this ID block - * - id.us of the new ID is set to 1 */ + if (ELEM(GS(id->name), LIB_ID_TYPES_NOCOPY)) { + return false; + } + else if (test) { + return true; + } + + BKE_libblock_copy_ex(bmain, id, r_newid, flag); + switch ((ID_Type)GS(id->name)) { + case ID_SCE: + BKE_scene_copy_data(bmain, (Scene *)*r_newid, (Scene *)id, flag); + break; case ID_OB: - if (!test) *newid = (ID *)BKE_object_copy(bmain, (Object *)id); - return true; + BKE_object_copy_data(bmain, (Object *)*r_newid, (Object *)id, flag); + break; case ID_ME: - if (!test) *newid = (ID *)BKE_mesh_copy(bmain, (Mesh *)id); - return true; + BKE_mesh_copy_data(bmain, (Mesh *)*r_newid, (Mesh *)id, flag); + break; case ID_CU: - if (!test) *newid = (ID *)BKE_curve_copy(bmain, (Curve *)id); - return true; + BKE_curve_copy_data(bmain, (Curve *)*r_newid, (Curve *)id, flag); + break; case ID_MB: - if (!test) *newid = (ID *)BKE_mball_copy(bmain, (MetaBall *)id); - return true; + BKE_mball_copy_data(bmain, (MetaBall *)*r_newid, (MetaBall *)id, flag); + break; case ID_MA: - if (!test) *newid = (ID *)BKE_material_copy(bmain, (Material *)id); - return true; + BKE_material_copy_data(bmain, (Material *)*r_newid, (Material *)id, flag); + break; case ID_TE: - if (!test) *newid = (ID *)BKE_texture_copy(bmain, (Tex *)id); - return true; + BKE_texture_copy_data(bmain, (Tex *)*r_newid, (Tex *)id, flag); + break; case ID_IM: - if (!test) *newid = (ID *)BKE_image_copy(bmain, (Image *)id); - return true; + BKE_image_copy_data(bmain, (Image *)*r_newid, (Image *)id, flag); + break; case ID_LT: - if (!test) *newid = (ID *)BKE_lattice_copy(bmain, (Lattice *)id); - return true; + BKE_lattice_copy_data(bmain, (Lattice *)*r_newid, (Lattice *)id, flag); + break; case ID_LA: - if (!test) *newid = (ID *)BKE_lamp_copy(bmain, (Lamp *)id); - return true; + BKE_lamp_copy_data(bmain, (Lamp *)*r_newid, (Lamp *)id, flag); + break; case ID_SPK: - if (!test) *newid = (ID *)BKE_speaker_copy(bmain, (Speaker *)id); - return true; + BKE_speaker_copy_data(bmain, (Speaker *)*r_newid, (Speaker *)id, flag); + break; case ID_LP: - if (!test) *newid = (ID *)BKE_lightprobe_copy(bmain, (LightProbe *)id); - return true; + BKE_lightprobe_copy_data(bmain, (LightProbe *)*r_newid, (LightProbe *)id, flag); + break; case ID_CA: - if (!test) *newid = (ID *)BKE_camera_copy(bmain, (Camera *)id); - return true; + BKE_camera_copy_data(bmain, (Camera *)*r_newid, (Camera *)id, flag); + break; case ID_KE: - if (!test) *newid = (ID *)BKE_key_copy(bmain, (Key *)id); - return true; + BKE_key_copy_data(bmain, (Key *)*r_newid, (Key *)id, flag); + break; case ID_WO: - if (!test) *newid = (ID *)BKE_world_copy(bmain, (World *)id); - return true; + BKE_world_copy_data(bmain, (World *)*r_newid, (World *)id, flag); + break; case ID_TXT: - if (!test) *newid = (ID *)BKE_text_copy(bmain, (Text *)id); - return true; + BKE_text_copy_data(bmain, (Text *)*r_newid, (Text *)id, flag); + break; case ID_GR: - if (!test) *newid = (ID *)BKE_group_copy(bmain, (Group *)id); - return true; + BKE_group_copy_data(bmain, (Group *)*r_newid, (Group *)id, flag); + break; case ID_AR: - if (!test) *newid = (ID *)BKE_armature_copy(bmain, (bArmature *)id); - return true; + BKE_armature_copy_data(bmain, (bArmature *)*r_newid, (bArmature *)id, flag); + break; case ID_AC: - if (!test) *newid = (ID *)BKE_action_copy(bmain, (bAction *)id); - return true; + BKE_action_copy_data(bmain, (bAction *)*r_newid, (bAction *)id, flag); + break; case ID_NT: - if (!test) *newid = (ID *)ntreeCopyTree(bmain, (bNodeTree *)id); - return true; + BKE_node_tree_copy_data(bmain, (bNodeTree *)*r_newid, (bNodeTree *)id, flag); + break; case ID_BR: - if (!test) *newid = (ID *)BKE_brush_copy(bmain, (Brush *)id); - return true; + BKE_brush_copy_data(bmain, (Brush *)*r_newid, (Brush *)id, flag); + break; case ID_PA: - if (!test) *newid = (ID *)BKE_particlesettings_copy(bmain, (ParticleSettings *)id); - return true; + BKE_particlesettings_copy_data(bmain, (ParticleSettings *)*r_newid, (ParticleSettings *)id, flag); + break; case ID_GD: - if (!test) *newid = (ID *)BKE_gpencil_data_duplicate(bmain, (bGPdata *)id, false); - return true; + BKE_gpencil_copy_data(bmain, (bGPdata *)*r_newid, (bGPdata *)id, flag); + break; case ID_MC: - if (!test) *newid = (ID *)BKE_movieclip_copy(bmain, (MovieClip *)id); - return true; + BKE_movieclip_copy_data(bmain, (MovieClip *)*r_newid, (MovieClip *)id, flag); + break; case ID_MSK: - if (!test) *newid = (ID *)BKE_mask_copy(bmain, (Mask *)id); - return true; + BKE_mask_copy_data(bmain, (Mask *)*r_newid, (Mask *)id, flag); + break; case ID_LS: - if (!test) *newid = (ID *)BKE_linestyle_copy(bmain, (FreestyleLineStyle *)id); - return true; + BKE_linestyle_copy_data(bmain, (FreestyleLineStyle *)*r_newid, (FreestyleLineStyle *)id, flag); + break; case ID_PAL: - if (!test) *newid = (ID *)BKE_palette_copy(bmain, (Palette *)id); - return true; + BKE_palette_copy_data(bmain, (Palette *)*r_newid, (Palette *)id, flag); + break; case ID_PC: - if (!test) *newid = (ID *)BKE_paint_curve_copy(bmain, (PaintCurve *)id); - return true; + BKE_paint_curve_copy_data(bmain, (PaintCurve *)*r_newid, (PaintCurve *)id, flag); + break; case ID_CF: - if (!test) *newid = (ID *)BKE_cachefile_copy(bmain, (CacheFile *)id); - return true; - case ID_WS: - case ID_SCE: + BKE_cachefile_copy_data(bmain, (CacheFile *)*r_newid, (CacheFile *)id, flag); + break; + case ID_SO: + BKE_sound_copy_data(bmain, (bSound *)*r_newid, (bSound *)id, flag); + break; + case ID_VF: + BKE_vfont_copy_data(bmain, (VFont *)*r_newid, (VFont *)id, flag); + break; case ID_LI: case ID_SCR: case ID_WM: - return false; /* can't be copied from here */ - case ID_VF: - case ID_SO: - return false; /* not implemented */ + case ID_WS: case ID_IP: - return false; /* deprecated */ + BLI_assert(0); /* Should have been rejected at start of function! */ + break; } - - return false; + + /* Update ID refcount, remap pointers to self in new ID. */ + struct IDCopyLibManagementData data = {.id_src = id, .id_dst = *r_newid, .flag = flag}; + BKE_library_foreach_ID_link(bmain, *r_newid, id_copy_libmanagement_cb, &data, IDWALK_NOP); + + /* Do not make new copy local in case we are copying outside of main... + * XXX TODO: is this behavior OK, or should we need own flag to control that? */ + if ((flag & LIB_ID_CREATE_NO_MAIN) == 0) { + BKE_id_copy_ensure_local(bmain, id, *r_newid); + } + + return true; +} + +/** + * Invokes the appropriate copy method for the block and returns the result in + * newid, unless test. Returns true if the block can be copied. + */ +bool id_copy(Main *bmain, const ID *id, ID **newid, bool test) +{ + return BKE_id_copy_ex(bmain, id, newid, 0, test); } /** Does *not* set ID->newid pointer. */ @@ -636,6 +708,101 @@ bool id_single_user(bContext *C, ID *id, PointerRNA *ptr, PropertyRNA *prop) return false; } +static int libblock_management_us_plus(void *UNUSED(user_data), ID *UNUSED(id_self), ID **id_pointer, int cb_flag) +{ + if (cb_flag & IDWALK_CB_USER) { + id_us_plus(*id_pointer); + } + if (cb_flag & IDWALK_CB_USER_ONE) { + id_us_ensure_real(*id_pointer); + } + + return IDWALK_RET_NOP; +} + +static int libblock_management_us_min(void *UNUSED(user_data), ID *UNUSED(id_self), ID **id_pointer, int cb_flag) +{ + if (cb_flag & IDWALK_CB_USER) { + id_us_min(*id_pointer); + } + /* We can do nothing in IDWALK_CB_USER_ONE case! */ + + return IDWALK_RET_NOP; +} + +/** Add a 'NO_MAIN' datablock to given main (also sets usercounts of its IDs if needed). */ +void BKE_libblock_management_main_add(Main *bmain, void *idv) +{ + ID *id = idv; + + BLI_assert(bmain != NULL); + if ((id->tag & LIB_TAG_NO_MAIN) == 0) { + return; + } + + if ((id->tag & LIB_TAG_NOT_ALLOCATED) != 0) { + /* We cannot add non-allocated ID to Main! */ + return; + } + + /* We cannot allow non-userrefcounting IDs in Main database! */ + if ((id->tag & LIB_TAG_NO_USER_REFCOUNT) != 0) { + BKE_library_foreach_ID_link(bmain, id, libblock_management_us_plus, NULL, IDWALK_NOP); + } + + ListBase *lb = which_libbase(bmain, GS(id->name)); + BKE_main_lock(bmain); + BLI_addtail(lb, id); + new_id(lb, id, NULL); + /* alphabetic insertion: is in new_id */ + id->tag &= ~(LIB_TAG_NO_MAIN | LIB_TAG_NO_USER_REFCOUNT); + BKE_main_unlock(bmain); +} + +/** Remove a datablock from given main (set it to 'NO_MAIN' status). */ +void BKE_libblock_management_main_remove(Main *bmain, void *idv) +{ + ID *id = idv; + + BLI_assert(bmain != NULL); + if ((id->tag & LIB_TAG_NO_MAIN) != 0) { + return; + } + + /* For now, allow userrefcounting IDs to get out of Main - can be handy in some cases... */ + + ListBase *lb = which_libbase(bmain, GS(id->name)); + BKE_main_lock(bmain); + BLI_remlink(lb, id); + id->tag |= LIB_TAG_NO_MAIN; + BKE_main_unlock(bmain); +} + +void BKE_libblock_management_usercounts_set(Main *bmain, void *idv) +{ + ID *id = idv; + + if ((id->tag & LIB_TAG_NO_USER_REFCOUNT) == 0) { + return; + } + + BKE_library_foreach_ID_link(bmain, id, libblock_management_us_plus, NULL, IDWALK_NOP); + id->tag &= ~LIB_TAG_NO_USER_REFCOUNT; +} + +void BKE_libblock_management_usercounts_clear(Main *bmain, void *idv) +{ + ID *id = idv; + + /* We do not allow IDs in Main database to not be userrefcounting. */ + if ((id->tag & LIB_TAG_NO_USER_REFCOUNT) != 0 || (id->tag & LIB_TAG_NO_MAIN) != 0) { + return; + } + + BKE_library_foreach_ID_link(bmain, id, libblock_management_us_min, NULL, IDWALK_NOP); + id->tag |= LIB_TAG_NO_USER_REFCOUNT; +} + ListBase *which_libbase(Main *mainlib, short type) { switch ((ID_Type)type) { @@ -953,23 +1120,44 @@ void *BKE_libblock_alloc_notest(short type) * The user count is set to 1, all other content (apart from name and links) being * initialized to zero. */ -void *BKE_libblock_alloc(Main *bmain, short type, const char *name) +void *BKE_libblock_alloc(Main *bmain, short type, const char *name, const int flag) { - ID *id = NULL; - ListBase *lb = which_libbase(bmain, type); - - id = BKE_libblock_alloc_notest(type); + BLI_assert((flag & LIB_ID_CREATE_NO_ALLOCATE) == 0); + + ID *id = BKE_libblock_alloc_notest(type); + if (id) { - BKE_main_lock(bmain); - BLI_addtail(lb, id); - id->us = 1; + if ((flag & LIB_ID_CREATE_NO_MAIN) != 0) { + id->tag |= LIB_TAG_NO_MAIN; + } + if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) != 0) { + id->tag |= LIB_TAG_NO_USER_REFCOUNT; + } + id->icon_id = 0; - *( (short *)id->name) = type; - new_id(lb, id, name); - /* alphabetic insertion: is in new_id */ - BKE_main_unlock(bmain); + *((short *)id->name) = type; + if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) { + id->us = 1; + } + if ((flag & LIB_ID_CREATE_NO_MAIN) == 0) { + ListBase *lb = which_libbase(bmain, type); + + BKE_main_lock(bmain); + BLI_addtail(lb, id); + new_id(lb, id, name); + /* alphabetic insertion: is in new_id */ + BKE_main_unlock(bmain); + + /* TODO to be removed from here! */ + if ((flag & LIB_ID_CREATE_NO_DEG_TAG) == 0) { + DEG_id_type_tag(bmain, type); + } + } + else { + BLI_strncpy(id->name + 2, name, sizeof(id->name) - 2); + } } - DEG_id_type_tag(bmain, type); + return id; } @@ -1095,70 +1283,80 @@ void BKE_libblock_init_empty(ID *id) /* by spec, animdata is first item after ID */ /* and, trust that BKE_animdata_from_id() will only find AnimData for valid ID-types */ -static void id_copy_animdata(ID *id, const bool do_action) +static void id_copy_animdata(Main *bmain, ID *id, const bool do_action) { AnimData *adt = BKE_animdata_from_id(id); if (adt) { IdAdtTemplate *iat = (IdAdtTemplate *)id; - iat->adt = BKE_animdata_copy(iat->adt, do_action); /* could be set to false, need to investigate */ + iat->adt = BKE_animdata_copy(bmain, iat->adt, do_action); /* could be set to false, need to investigate */ } } -/* material nodes use this since they are not treated as libdata */ -void BKE_libblock_copy_data(ID *id, const ID *id_from, const bool do_action) +void BKE_libblock_copy_ex(Main *bmain, const ID *id, ID **r_newid, const int flag) { - if (id_from->properties) - id->properties = IDP_CopyProperty(id_from->properties); + ID *new_id = *r_newid; + + /* Grrrrrrrrr... Not adding 'root' nodetrees to bmain.... grrrrrrrrrrrrrrrrrrrr! */ + /* This is taken from original ntree copy code, might be weak actually? */ + const bool use_nodetree_alloc_exception = ((GS(id->name) == ID_NT) && (bmain != NULL) && + (BLI_findindex(&bmain->nodetree, id) < 0)); + + BLI_assert((flag & LIB_ID_CREATE_NO_MAIN) != 0 || bmain != NULL); + BLI_assert((flag & LIB_ID_CREATE_NO_MAIN) != 0 || (flag & LIB_ID_CREATE_NO_ALLOCATE) == 0); + BLI_assert((flag & LIB_ID_CREATE_NO_MAIN) == 0 || (flag & LIB_ID_CREATE_NO_USER_REFCOUNT) != 0); + + if ((flag & LIB_ID_CREATE_NO_ALLOCATE) != 0) { + /* r_newid already contains pointer to allocated memory. */ + /* TODO do we want to memset(0) whole mem before filling it? */ + BLI_strncpy(new_id->name, id->name, sizeof(new_id->name)); + new_id->us = 0; + new_id->tag |= LIB_TAG_NOT_ALLOCATED | LIB_TAG_NO_MAIN | LIB_TAG_NO_USER_REFCOUNT; + /* TODO Do we want/need to copy more from ID struct itself? */ + } + else { + new_id = BKE_libblock_alloc(bmain, GS(id->name), id->name + 2, flag | (use_nodetree_alloc_exception ? LIB_ID_CREATE_NO_MAIN : 0)); + } + BLI_assert(new_id != NULL); + + const size_t id_len = BKE_libblock_get_alloc_info(GS(new_id->name), NULL); + const size_t id_offset = sizeof(ID); + if ((int)id_len - (int)id_offset > 0) { /* signed to allow neg result */ /* XXX ????? */ + const char *cp = (const char *)id; + char *cpn = (char *)new_id; + + memcpy(cpn + id_offset, cp + id_offset, id_len - id_offset); + } + + if (id->properties) { + new_id->properties = IDP_CopyProperty_ex(id->properties, flag); + } /* the duplicate should get a copy of the animdata */ - id_copy_animdata(id, do_action); + id_copy_animdata(bmain, new_id, (flag & LIB_ID_COPY_ACTIONS) != 0 && (flag & LIB_ID_CREATE_NO_MAIN) == 0); + + if ((flag & LIB_ID_CREATE_NO_DEG_TAG) == 0 && (flag & LIB_ID_CREATE_NO_MAIN) == 0) { + DEG_id_type_tag(bmain, GS(new_id->name)); + } + + *r_newid = new_id; } /* used everywhere in blenkernel */ void *BKE_libblock_copy(Main *bmain, const ID *id) { ID *idn; - size_t idn_len; - - idn = BKE_libblock_alloc(bmain, GS(id->name), id->name + 2); - assert(idn != NULL); + BKE_libblock_copy_ex(bmain, id, &idn, 0); - idn_len = MEM_allocN_len(idn); - if ((int)idn_len - (int)sizeof(ID) > 0) { /* signed to allow neg result */ - const char *cp = (const char *)id; - char *cpn = (char *)idn; - - memcpy(cpn + sizeof(ID), cp + sizeof(ID), idn_len - sizeof(ID)); - } - - BKE_libblock_copy_data(idn, id, false); - return idn; } void *BKE_libblock_copy_nolib(const ID *id, const bool do_action) { ID *idn; - size_t idn_len; - - idn = BKE_libblock_alloc_notest(GS(id->name)); - assert(idn != NULL); - - BLI_strncpy(idn->name, id->name, sizeof(idn->name)); - - idn_len = MEM_allocN_len(idn); - if ((int)idn_len - (int)sizeof(ID) > 0) { /* signed to allow neg result */ - const char *cp = (const char *)id; - char *cpn = (char *)idn; - - memcpy(cpn + sizeof(ID), cp + sizeof(ID), idn_len - sizeof(ID)); - } - - idn->us = 1; - BKE_libblock_copy_data(idn, id, do_action); + BKE_libblock_copy_ex(NULL, id, &idn, LIB_ID_CREATE_NO_MAIN | LIB_ID_CREATE_NO_USER_REFCOUNT | (do_action ? LIB_ID_COPY_ACTIONS : 0)); return idn; } @@ -1198,41 +1396,41 @@ void BKE_main_free(Main *mainvar) /* errors freeing ID's can be hard to track down, * enable this so valgrind will give the line number in its error log */ switch (a) { - case 0: BKE_libblock_free_ex(mainvar, id, false); break; - case 1: BKE_libblock_free_ex(mainvar, id, false); break; - case 2: BKE_libblock_free_ex(mainvar, id, false); break; - case 3: BKE_libblock_free_ex(mainvar, id, false); break; - case 4: BKE_libblock_free_ex(mainvar, id, false); break; - case 5: BKE_libblock_free_ex(mainvar, id, false); break; - case 6: BKE_libblock_free_ex(mainvar, id, false); break; - case 7: BKE_libblock_free_ex(mainvar, id, false); break; - case 8: BKE_libblock_free_ex(mainvar, id, false); break; - case 9: BKE_libblock_free_ex(mainvar, id, false); break; - case 10: BKE_libblock_free_ex(mainvar, id, false); break; - case 11: BKE_libblock_free_ex(mainvar, id, false); break; - case 12: BKE_libblock_free_ex(mainvar, id, false); break; - case 13: BKE_libblock_free_ex(mainvar, id, false); break; - case 14: BKE_libblock_free_ex(mainvar, id, false); break; - case 15: BKE_libblock_free_ex(mainvar, id, false); break; - case 16: BKE_libblock_free_ex(mainvar, id, false); break; - case 17: BKE_libblock_free_ex(mainvar, id, false); break; - case 18: BKE_libblock_free_ex(mainvar, id, false); break; - case 19: BKE_libblock_free_ex(mainvar, id, false); break; - case 20: BKE_libblock_free_ex(mainvar, id, false); break; - case 21: BKE_libblock_free_ex(mainvar, id, false); break; - case 22: BKE_libblock_free_ex(mainvar, id, false); break; - case 23: BKE_libblock_free_ex(mainvar, id, false); break; - case 24: BKE_libblock_free_ex(mainvar, id, false); break; - case 25: BKE_libblock_free_ex(mainvar, id, false); break; - case 26: BKE_libblock_free_ex(mainvar, id, false); break; - case 27: BKE_libblock_free_ex(mainvar, id, false); break; - case 28: BKE_libblock_free_ex(mainvar, id, false); break; - case 29: BKE_libblock_free_ex(mainvar, id, false); break; - case 30: BKE_libblock_free_ex(mainvar, id, false); break; - case 31: BKE_libblock_free_ex(mainvar, id, false); break; - case 32: BKE_libblock_free_ex(mainvar, id, false); break; - case 33: BKE_libblock_free_ex(mainvar, id, false); break; - case 34: BKE_libblock_free_ex(mainvar, id, false); break; + case 0: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 1: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 2: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 3: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 4: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 5: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 6: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 7: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 8: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 9: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 10: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 11: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 12: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 13: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 14: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 15: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 16: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 17: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 18: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 19: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 20: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 21: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 22: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 23: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 24: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 25: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 26: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 27: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 28: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 29: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 30: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 31: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 32: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 33: BKE_libblock_free_ex(mainvar, id, false, false); break; + case 34: BKE_libblock_free_ex(mainvar, id, false, false); break; default: BLI_assert(0); break; diff --git a/source/blender/blenkernel/intern/library_query.c b/source/blender/blenkernel/intern/library_query.c index 8a42a3334bd..e397dcf7667 100644 --- a/source/blender/blenkernel/intern/library_query.c +++ b/source/blender/blenkernel/intern/library_query.c @@ -536,7 +536,8 @@ void BKE_library_foreach_ID_link(Main *bmain, ID *id, LibraryIDLinkCallback call /* Object is special, proxies make things hard... */ const int data_cb_flag = data.cb_flag; - const int proxy_cb_flag = (object->proxy || object->proxy_group) ? IDWALK_CB_INDIRECT_USAGE : 0; + const int proxy_cb_flag = ((data.flag & IDWALK_NO_INDIRECT_PROXY_DATA_USAGE) == 0 && (object->proxy || object->proxy_group)) ? + IDWALK_CB_INDIRECT_USAGE : 0; /* object data special case */ data.cb_flag |= proxy_cb_flag; diff --git a/source/blender/blenkernel/intern/library_remap.c b/source/blender/blenkernel/intern/library_remap.c index f855de0dac5..8581530907f 100644 --- a/source/blender/blenkernel/intern/library_remap.c +++ b/source/blender/blenkernel/intern/library_remap.c @@ -193,8 +193,8 @@ static int foreach_libblock_remap_callback(void *user_data, ID *id_self, ID **id const bool skip_never_null = (id_remap_data->flag & ID_REMAP_SKIP_NEVER_NULL_USAGE) != 0; #ifdef DEBUG_PRINT - printf("In %s: Remapping %s (%p) to %s (%p) (skip_indirect: %d)\n", - id->name, old_id->name, old_id, new_id ? new_id->name : "<NONE>", new_id, skip_indirect); + printf("In %s: Remapping %s (%p) to %s (%p) (is_indirect: %d, skip_indirect: %d)\n", + id->name, old_id->name, old_id, new_id ? new_id->name : "<NONE>", new_id, is_indirect, skip_indirect); #endif if ((id_remap_data->flag & ID_REMAP_FLAG_NEVER_NULL_USAGE) && (cb_flag & IDWALK_CB_NEVER_NULL)) { @@ -208,6 +208,14 @@ static int foreach_libblock_remap_callback(void *user_data, ID *id_self, ID **id { if (is_indirect) { id_remap_data->skipped_indirect++; + if (is_obj) { + Object *ob = (Object *)id; + if (ob->data == *id_p && ob->proxy != NULL) { + /* And another 'Proudly brought to you by Proxy Hell' hack! + * This will allow us to avoid clearing 'LIB_EXTERN' flag of obdata of proxies... */ + id_remap_data->skipped_direct++; + } + } } else if (is_never_null || is_obj_editmode) { id_remap_data->skipped_direct++; @@ -248,7 +256,7 @@ static int foreach_libblock_remap_callback(void *user_data, ID *id_self, ID **id return IDWALK_RET_NOP; } -/* Some reamapping unfortunately require extra and/or specific handling, tackle those here. */ +/* Some remapping unfortunately require extra and/or specific handling, tackle those here. */ static void libblock_remap_data_preprocess_scene_base_unlink( IDRemap *r_id_remap_data, Scene *sce, BaseLegacy *base, const bool skip_indirect, const bool is_indirect) { @@ -352,7 +360,7 @@ static void libblock_remap_data_preprocess(IDRemap *r_id_remap_data) } } -static void libblock_remap_data_postprocess_object_fromgroup_update(Main *bmain, Object *old_ob, Object *new_ob) +static void libblock_remap_data_postprocess_object_update(Main *bmain, Object *old_ob, Object *new_ob) { if (old_ob->flag & OB_FROMGROUP) { /* Note that for Scene's BaseObject->flag, either we: @@ -371,6 +379,13 @@ static void libblock_remap_data_postprocess_object_fromgroup_update(Main *bmain, new_ob->flag |= OB_FROMGROUP; } } + if (old_ob->type == OB_MBALL) { + for (Object *ob = bmain->object.first; ob; ob = ob->id.next) { + if (ob->type == OB_MBALL && BKE_mball_is_basis_for(ob, old_ob)) { + DEG_id_tag_update(&ob->id, OB_RECALC_DATA); + } + } + } } static void libblock_remap_data_postprocess_group_scene_unlink(Main *UNUSED(bmain), Scene *sce, ID *old_id) @@ -447,6 +462,7 @@ ATTR_NONNULL(1) static void libblock_remap_data( IDRemap id_remap_data; ListBase *lb_array[MAX_LIBARRAY]; int i; + const int foreach_id_flags = (remap_flags & ID_REMAP_NO_INDIRECT_PROXY_DATA_USAGE) != 0 ? IDWALK_NO_INDIRECT_PROXY_DATA_USAGE : IDWALK_NOP; if (r_id_remap_data == NULL) { r_id_remap_data = &id_remap_data; @@ -467,7 +483,7 @@ ATTR_NONNULL(1) static void libblock_remap_data( #endif r_id_remap_data->id = id; libblock_remap_data_preprocess(r_id_remap_data); - BKE_library_foreach_ID_link(NULL, id, foreach_libblock_remap_callback, (void *)r_id_remap_data, IDWALK_NOP); + BKE_library_foreach_ID_link(NULL, id, foreach_libblock_remap_callback, (void *)r_id_remap_data, foreach_id_flags); } else { i = set_listbasepointers(bmain, lb_array); @@ -484,7 +500,7 @@ ATTR_NONNULL(1) static void libblock_remap_data( r_id_remap_data->id = id_curr; libblock_remap_data_preprocess(r_id_remap_data); BKE_library_foreach_ID_link( - NULL, id_curr, foreach_libblock_remap_callback, (void *)r_id_remap_data, IDWALK_NOP); + NULL, id_curr, foreach_libblock_remap_callback, (void *)r_id_remap_data, foreach_id_flags); } } } @@ -574,7 +590,7 @@ void BKE_libblock_remap_locked( */ switch (GS(old_id->name)) { case ID_OB: - libblock_remap_data_postprocess_object_fromgroup_update(bmain, (Object *)old_id, (Object *)new_id); + libblock_remap_data_postprocess_object_update(bmain, (Object *)old_id, (Object *)new_id); break; case ID_GR: if (!new_id) { /* Only affects us in case group was unlinked. */ @@ -604,7 +620,7 @@ void BKE_libblock_remap_locked( libblock_remap_data_postprocess_nodetree_update(bmain, new_id); BKE_main_lock(bmain); - /* Full rebuild of DAG! */ + /* Full rebuild of DEG! */ DEG_relations_tag_update(bmain); } @@ -684,8 +700,7 @@ void BKE_libblock_relink_ex( switch (GS(old_id->name)) { case ID_OB: { - libblock_remap_data_postprocess_object_fromgroup_update( - bmain, (Object *)old_id, (Object *)new_id); + libblock_remap_data_postprocess_object_update(bmain, (Object *)old_id, (Object *)new_id); break; } case ID_GR: @@ -700,7 +715,7 @@ void BKE_libblock_relink_ex( else { /* No choice but to check whole objects/groups. */ for (Object *ob = bmain->object.first; ob; ob = ob->id.next) { - libblock_remap_data_postprocess_object_fromgroup_update(bmain, ob, NULL); + libblock_remap_data_postprocess_object_update(bmain, ob, NULL); } for (Group *grp = bmain->group.first; grp; grp = grp->id.next) { libblock_remap_data_postprocess_group_scene_unlink(bmain, sce, NULL); @@ -757,9 +772,11 @@ void BKE_libblock_free_data(ID *id, const bool do_id_user) IDP_FreeProperty_ex(id->properties, do_id_user); MEM_freeN(id->properties); } + + /* XXX TODO remove animdata handling from each type's freeing func, and do it here, like for copy! */ } -void BKE_libblock_free_datablock(ID *id) +void BKE_libblock_free_datablock(ID *id, const int UNUSED(flag)) { const short type = GS(id->name); switch (type) { @@ -875,6 +892,90 @@ void BKE_libblock_free_datablock(ID *id) } } + +void BKE_id_free_ex(Main *bmain, void *idv, int flag, const bool use_flag_from_idtag) +{ + ID *id = idv; + + if (use_flag_from_idtag) { + if ((id->tag & LIB_TAG_NO_MAIN) != 0) { + flag |= LIB_ID_FREE_NO_MAIN; + } + else { + flag &= ~LIB_ID_FREE_NO_MAIN; + } + + if ((id->tag & LIB_TAG_NO_USER_REFCOUNT) != 0) { + flag |= LIB_ID_FREE_NO_USER_REFCOUNT; + } + else { + flag &= ~LIB_ID_FREE_NO_USER_REFCOUNT; + } + + if ((id->tag & LIB_TAG_NOT_ALLOCATED) != 0) { + flag |= LIB_ID_FREE_NOT_ALLOCATED; + } + else { + flag &= ~LIB_ID_FREE_NOT_ALLOCATED; + } + } + + BLI_assert((flag & LIB_ID_FREE_NO_MAIN) != 0 || bmain != NULL); + BLI_assert((flag & LIB_ID_FREE_NO_MAIN) != 0 || (flag & LIB_ID_FREE_NOT_ALLOCATED) == 0); + BLI_assert((flag & LIB_ID_FREE_NO_MAIN) != 0 || (flag & LIB_ID_FREE_NO_USER_REFCOUNT) == 0); + + const short type = GS(id->name); + + if (bmain && (flag & LIB_ID_FREE_NO_DEG_TAG) == 0) { + DEG_id_type_tag(bmain, type); + } + +#ifdef WITH_PYTHON + BPY_id_release(id); +#endif + + if ((flag & LIB_ID_FREE_NO_USER_REFCOUNT) == 0) { + BKE_libblock_relink_ex(bmain, id, NULL, NULL, true); + } + + BKE_libblock_free_datablock(id, flag); + + /* avoid notifying on removed data */ + if (bmain) { + BKE_main_lock(bmain); + } + + if ((flag & LIB_ID_FREE_NO_UI_USER) == 0) { + if (free_notifier_reference_cb) { + free_notifier_reference_cb(id); + } + + if (remap_editor_id_reference_cb) { + remap_editor_id_reference_cb(id, NULL); + } + } + + if ((flag & LIB_ID_FREE_NO_MAIN) == 0) { + ListBase *lb = which_libbase(bmain, type); + BLI_remlink(lb, id); + } + + BKE_libblock_free_data(id, (flag & LIB_ID_FREE_NO_USER_REFCOUNT) == 0); + + if (bmain) { + BKE_main_unlock(bmain); + } + + if ((flag & LIB_ID_FREE_NOT_ALLOCATED) == 0) { + MEM_freeN(id); + } +} + +void BKE_id_free(Main *bmain, void *idv) +{ + BKE_id_free_ex(bmain, idv, 0, true); +} + /** * used in headerbuttons.c image.c mesh.c screen.c sound.c and library.c * @@ -892,14 +993,19 @@ void BKE_libblock_free_ex(Main *bmain, void *idv, const bool do_id_user, const b DEG_id_type_tag(bmain, type); #ifdef WITH_PYTHON +#ifdef WITH_PYTHON_SAFETY BPY_id_release(id); #endif + if (id->py_instance) { + BPY_DECREF_RNA_INVALIDATE(id->py_instance); + } +#endif if (do_id_user) { BKE_libblock_relink_ex(bmain, id, NULL, NULL, true); } - BKE_libblock_free_datablock(id); + BKE_libblock_free_datablock(id, 0); /* avoid notifying on removed data */ BKE_main_lock(bmain); diff --git a/source/blender/blenkernel/intern/lightprobe.c b/source/blender/blenkernel/intern/lightprobe.c index a1fa266512b..03bd2344f7b 100644 --- a/source/blender/blenkernel/intern/lightprobe.c +++ b/source/blender/blenkernel/intern/lightprobe.c @@ -25,7 +25,7 @@ * ***** END GPL LICENSE BLOCK ***** */ -/** \file blender/blenkernel/intern/probe.c +/** \file blender/blenkernel/intern/lightprobe.c * \ingroup bke */ @@ -59,22 +59,32 @@ void *BKE_lightprobe_add(Main *bmain, const char *name) { LightProbe *probe; - probe = BKE_libblock_alloc(bmain, ID_LP, name); + probe = BKE_libblock_alloc(bmain, ID_LP, name, 0); BKE_lightprobe_init(probe); return probe; } -LightProbe *BKE_lightprobe_copy(Main *bmain, LightProbe *probe) +/** + * Only copy internal data of LightProbe ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_lightprobe_copy_data( + Main *UNUSED(bmain), LightProbe *UNUSED(probe_dst), const LightProbe *UNUSED(probe_src), const int UNUSED(flag)) { - LightProbe *probe_new; - - probe_new = BKE_libblock_copy(bmain, &probe->id); - - BKE_id_copy_ensure_local(bmain, &probe->id, &probe_new->id); + /* Nothing to do here. */ +} - return probe_new; +LightProbe *BKE_lightprobe_copy(Main *bmain, const LightProbe *probe) +{ + LightProbe *probe_copy; + BKE_id_copy_ex(bmain, &probe->id, (ID **)&probe_copy, 0, false); + return probe_copy; } void BKE_lightprobe_make_local(Main *bmain, LightProbe *probe, const bool lib_local) diff --git a/source/blender/blenkernel/intern/linestyle.c b/source/blender/blenkernel/intern/linestyle.c index 771e81ddc4f..1b1a12e702a 100644 --- a/source/blender/blenkernel/intern/linestyle.c +++ b/source/blender/blenkernel/intern/linestyle.c @@ -119,7 +119,7 @@ FreestyleLineStyle *BKE_linestyle_new(struct Main *bmain, const char *name) { FreestyleLineStyle *linestyle; - linestyle = (FreestyleLineStyle *)BKE_libblock_alloc(bmain, ID_LS, name); + linestyle = (FreestyleLineStyle *)BKE_libblock_alloc(bmain, ID_LS, name, 0); BKE_linestyle_init(linestyle); @@ -155,73 +155,54 @@ void BKE_linestyle_free(FreestyleLineStyle *linestyle) BKE_linestyle_geometry_modifier_remove(linestyle, m); } -FreestyleLineStyle *BKE_linestyle_copy(struct Main *bmain, const FreestyleLineStyle *linestyle) +/** + * Only copy internal data of Linestyle ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_linestyle_copy_data( + struct Main *bmain, FreestyleLineStyle *linestyle_dst, const FreestyleLineStyle *linestyle_src, const int flag) { - FreestyleLineStyle *new_linestyle; - LineStyleModifier *m; - int a; + /* We never handle usercount here for own data. */ + const int flag_subdata = flag | LIB_ID_CREATE_NO_USER_REFCOUNT; - new_linestyle = BKE_linestyle_new(bmain, linestyle->id.name + 2); - BKE_linestyle_free(new_linestyle); - - for (a = 0; a < MAX_MTEX; a++) { - if (linestyle->mtex[a]) { - new_linestyle->mtex[a] = MEM_mallocN(sizeof(MTex), "BKE_linestyle_copy"); - memcpy(new_linestyle->mtex[a], linestyle->mtex[a], sizeof(MTex)); - id_us_plus((ID *)new_linestyle->mtex[a]->tex); + for (int a = 0; a < MAX_MTEX; a++) { + if (linestyle_src->mtex[a]) { + linestyle_dst->mtex[a] = MEM_mallocN(sizeof(*linestyle_dst->mtex[a]), __func__); + *linestyle_dst->mtex[a] = *linestyle_src->mtex[a]; } } - if (linestyle->nodetree) { - new_linestyle->nodetree = ntreeCopyTree(bmain, linestyle->nodetree); + if (linestyle_src->nodetree) { + BKE_id_copy_ex(bmain, (ID *)linestyle_src->nodetree, (ID **)&linestyle_dst->nodetree, flag, false); } - new_linestyle->r = linestyle->r; - new_linestyle->g = linestyle->g; - new_linestyle->b = linestyle->b; - new_linestyle->alpha = linestyle->alpha; - new_linestyle->thickness = linestyle->thickness; - new_linestyle->thickness_position = linestyle->thickness_position; - new_linestyle->thickness_ratio = linestyle->thickness_ratio; - new_linestyle->flag = linestyle->flag; - new_linestyle->caps = linestyle->caps; - new_linestyle->chaining = linestyle->chaining; - new_linestyle->rounds = linestyle->rounds; - new_linestyle->split_length = linestyle->split_length; - new_linestyle->min_angle = linestyle->min_angle; - new_linestyle->max_angle = linestyle->max_angle; - new_linestyle->min_length = linestyle->min_length; - new_linestyle->max_length = linestyle->max_length; - new_linestyle->chain_count = linestyle->chain_count; - new_linestyle->split_dash1 = linestyle->split_dash1; - new_linestyle->split_gap1 = linestyle->split_gap1; - new_linestyle->split_dash2 = linestyle->split_dash2; - new_linestyle->split_gap2 = linestyle->split_gap2; - new_linestyle->split_dash3 = linestyle->split_dash3; - new_linestyle->split_gap3 = linestyle->split_gap3; - new_linestyle->dash1 = linestyle->dash1; - new_linestyle->gap1 = linestyle->gap1; - new_linestyle->dash2 = linestyle->dash2; - new_linestyle->gap2 = linestyle->gap2; - new_linestyle->dash3 = linestyle->dash3; - new_linestyle->gap3 = linestyle->gap3; - new_linestyle->panel = linestyle->panel; - new_linestyle->sort_key = linestyle->sort_key; - new_linestyle->integration_type = linestyle->integration_type; - new_linestyle->texstep = linestyle->texstep; - new_linestyle->pr_texture = linestyle->pr_texture; - new_linestyle->use_nodes = linestyle->use_nodes; - for (m = (LineStyleModifier *)linestyle->color_modifiers.first; m; m = m->next) - BKE_linestyle_color_modifier_copy(new_linestyle, m); - for (m = (LineStyleModifier *)linestyle->alpha_modifiers.first; m; m = m->next) - BKE_linestyle_alpha_modifier_copy(new_linestyle, m); - for (m = (LineStyleModifier *)linestyle->thickness_modifiers.first; m; m = m->next) - BKE_linestyle_thickness_modifier_copy(new_linestyle, m); - for (m = (LineStyleModifier *)linestyle->geometry_modifiers.first; m; m = m->next) - BKE_linestyle_geometry_modifier_copy(new_linestyle, m); - - BKE_id_copy_ensure_local(bmain, &linestyle->id, &new_linestyle->id); - - return new_linestyle; + LineStyleModifier *m; + BLI_listbase_clear(&linestyle_dst->color_modifiers); + for (m = (LineStyleModifier *)linestyle_src->color_modifiers.first; m; m = m->next) { + BKE_linestyle_color_modifier_copy(linestyle_dst, m, flag_subdata); + } + BLI_listbase_clear(&linestyle_dst->alpha_modifiers); + for (m = (LineStyleModifier *)linestyle_src->alpha_modifiers.first; m; m = m->next) { + BKE_linestyle_alpha_modifier_copy(linestyle_dst, m, flag_subdata); + } + BLI_listbase_clear(&linestyle_dst->thickness_modifiers); + for (m = (LineStyleModifier *)linestyle_src->thickness_modifiers.first; m; m = m->next) { + BKE_linestyle_thickness_modifier_copy(linestyle_dst, m, flag_subdata); + } + BLI_listbase_clear(&linestyle_dst->geometry_modifiers); + for (m = (LineStyleModifier *)linestyle_src->geometry_modifiers.first; m; m = m->next) { + BKE_linestyle_geometry_modifier_copy(linestyle_dst, m, flag_subdata); + } +} + +FreestyleLineStyle *BKE_linestyle_copy(struct Main *bmain, const FreestyleLineStyle *linestyle) +{ + FreestyleLineStyle *linestyle_copy; + BKE_id_copy_ex(bmain, &linestyle->id, (ID **)&linestyle_copy, 0, false); + return linestyle_copy; } void BKE_linestyle_make_local(struct Main *bmain, FreestyleLineStyle *linestyle, const bool lib_local) @@ -355,7 +336,8 @@ LineStyleModifier *BKE_linestyle_color_modifier_add(FreestyleLineStyle *linestyl return m; } -LineStyleModifier *BKE_linestyle_color_modifier_copy(FreestyleLineStyle *linestyle, const LineStyleModifier *m) +LineStyleModifier *BKE_linestyle_color_modifier_copy( + FreestyleLineStyle *linestyle, const LineStyleModifier *m, const int flag) { LineStyleModifier *new_m; @@ -388,9 +370,10 @@ LineStyleModifier *BKE_linestyle_color_modifier_copy(FreestyleLineStyle *linesty { LineStyleColorModifier_DistanceFromObject *p = (LineStyleColorModifier_DistanceFromObject *)m; LineStyleColorModifier_DistanceFromObject *q = (LineStyleColorModifier_DistanceFromObject *)new_m; - if (p->target) - id_us_plus(&p->target->id); q->target = p->target; + if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) { + id_us_plus((ID *)q->target); + } q->color_ramp = MEM_dupallocN(p->color_ramp); q->range_min = p->range_min; q->range_max = p->range_max; @@ -594,7 +577,8 @@ LineStyleModifier *BKE_linestyle_alpha_modifier_add(FreestyleLineStyle *linestyl return m; } -LineStyleModifier *BKE_linestyle_alpha_modifier_copy(FreestyleLineStyle *linestyle, const LineStyleModifier *m) +LineStyleModifier *BKE_linestyle_alpha_modifier_copy( + FreestyleLineStyle *linestyle, const LineStyleModifier *m, const int UNUSED(flag)) { LineStyleModifier *new_m; @@ -863,7 +847,8 @@ LineStyleModifier *BKE_linestyle_thickness_modifier_add(FreestyleLineStyle *line return m; } -LineStyleModifier *BKE_linestyle_thickness_modifier_copy(FreestyleLineStyle *linestyle, const LineStyleModifier *m) +LineStyleModifier *BKE_linestyle_thickness_modifier_copy( + FreestyleLineStyle *linestyle, const LineStyleModifier *m, const int flag) { LineStyleModifier *new_m; @@ -901,9 +886,10 @@ LineStyleModifier *BKE_linestyle_thickness_modifier_copy(FreestyleLineStyle *lin { LineStyleThicknessModifier_DistanceFromObject *p = (LineStyleThicknessModifier_DistanceFromObject *)m; LineStyleThicknessModifier_DistanceFromObject *q = (LineStyleThicknessModifier_DistanceFromObject *)new_m; - if (p->target) - id_us_plus(&p->target->id); q->target = p->target; + if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) { + id_us_plus((ID *)q->target); + } q->curve = curvemapping_copy(p->curve); q->flags = p->flags; q->range_min = p->range_min; @@ -1195,7 +1181,8 @@ LineStyleModifier *BKE_linestyle_geometry_modifier_add(FreestyleLineStyle *lines return m; } -LineStyleModifier *BKE_linestyle_geometry_modifier_copy(FreestyleLineStyle *linestyle, const LineStyleModifier *m) +LineStyleModifier *BKE_linestyle_geometry_modifier_copy( + FreestyleLineStyle *linestyle, const LineStyleModifier *m, const int UNUSED(flag)) { LineStyleModifier *new_m; diff --git a/source/blender/blenkernel/intern/mask.c b/source/blender/blenkernel/intern/mask.c index ae27e9bcd34..1e9447c4f09 100644 --- a/source/blender/blenkernel/intern/mask.c +++ b/source/blender/blenkernel/intern/mask.c @@ -61,6 +61,8 @@ #include "BKE_movieclip.h" #include "BKE_image.h" +#include "DEG_depsgraph_build.h" + static struct { ListBase splines; struct GHash *id_hash; @@ -794,7 +796,7 @@ static Mask *mask_alloc(Main *bmain, const char *name) { Mask *mask; - mask = BKE_libblock_alloc(bmain, ID_MSK, name); + mask = BKE_libblock_alloc(bmain, ID_MSK, name, 0); id_fake_user_set(&mask->id); @@ -817,10 +819,13 @@ Mask *BKE_mask_new(Main *bmain, const char *name) mask->sfra = 1; mask->efra = 100; + DEG_relations_tag_update(bmain); + return mask; } /* TODO(sergey): Use generic BKE_libblock_copy_nolib() instead. */ +/* TODO(bastien): Use new super cool & generic BKE_id_copy_ex() instead! */ Mask *BKE_mask_copy_nolib(Mask *mask) { Mask *mask_new; @@ -840,22 +845,29 @@ Mask *BKE_mask_copy_nolib(Mask *mask) return mask_new; } -Mask *BKE_mask_copy(Main *bmain, const Mask *mask) +/** + * Only copy internal data of Mask ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_mask_copy_data(Main *UNUSED(bmain), Mask *mask_dst, const Mask *mask_src, const int UNUSED(flag)) { - Mask *mask_new; + BLI_listbase_clear(&mask_dst->masklayers); - mask_new = BKE_libblock_copy(bmain, &mask->id); - - BLI_listbase_clear(&mask_new->masklayers); - - BKE_mask_layer_copy_list(&mask_new->masklayers, &mask->masklayers); + BKE_mask_layer_copy_list(&mask_dst->masklayers, &mask_src->masklayers); /* TODO add unused flag to those as well. */ /* enable fake user by default */ - id_fake_user_set(&mask_new->id); - - BKE_id_copy_ensure_local(bmain, &mask->id, &mask_new->id); + id_fake_user_set(&mask_dst->id); +} - return mask_new; +Mask *BKE_mask_copy(Main *bmain, const Mask *mask) +{ + Mask *mask_copy; + BKE_id_copy_ex(bmain, &mask->id, (ID **)&mask_copy, 0, false); + return mask_copy; } void BKE_mask_make_local(Main *bmain, Mask *mask, const bool lib_local) @@ -1171,17 +1183,6 @@ void BKE_mask_point_parent_matrix_get(MaskSplinePoint *point, float ctime, float } } -static void mask_evaluate_apply_point_parent(MaskSplinePoint *point, float ctime) -{ - float parent_matrix[3][3]; - - BKE_mask_point_parent_matrix_get(point, ctime, parent_matrix); - - mul_m3_v2(parent_matrix, point->bezt.vec[0]); - mul_m3_v2(parent_matrix, point->bezt.vec[1]); - mul_m3_v2(parent_matrix, point->bezt.vec[2]); -} - static void mask_calc_point_handle(MaskSplinePoint *point, MaskSplinePoint *point_prev, MaskSplinePoint *point_next) { BezTriple *bezt = &point->bezt; @@ -1397,80 +1398,12 @@ void BKE_mask_spline_ensure_deform(MaskSpline *spline) void BKE_mask_layer_evaluate(MaskLayer *masklay, const float ctime, const bool do_newframe) { - /* animation if available */ + /* Animation if available. */ if (do_newframe) { - MaskLayerShape *masklay_shape_a; - MaskLayerShape *masklay_shape_b; - int found; - - if ((found = BKE_mask_layer_shape_find_frame_range(masklay, ctime, - &masklay_shape_a, &masklay_shape_b))) - { - if (found == 1) { -#if 0 - printf("%s: exact %d %d (%d)\n", __func__, (int)ctime, BLI_listbase_count(&masklay->splines_shapes), - masklay_shape_a->frame); -#endif - - BKE_mask_layer_shape_to_mask(masklay, masklay_shape_a); - } - else if (found == 2) { - float w = masklay_shape_b->frame - masklay_shape_a->frame; -#if 0 - printf("%s: tween %d %d (%d %d)\n", __func__, (int)ctime, BLI_listbase_count(&masklay->splines_shapes), - masklay_shape_a->frame, masklay_shape_b->frame); -#endif - BKE_mask_layer_shape_to_mask_interp(masklay, masklay_shape_a, masklay_shape_b, - (ctime - masklay_shape_a->frame) / w); - } - else { - /* always fail, should never happen */ - BLI_assert(found == 2); - } - } - } - /* animation done... */ - - BKE_mask_layer_calc_handles(masklay); - - /* update deform */ - { - MaskSpline *spline; - - for (spline = masklay->splines.first; spline; spline = spline->next) { - int i; - bool need_handle_recalc = false; - - BKE_mask_spline_ensure_deform(spline); - - for (i = 0; i < spline->tot_point; i++) { - MaskSplinePoint *point = &spline->points[i]; - MaskSplinePoint *point_deform = &spline->points_deform[i]; - - BKE_mask_point_free(point_deform); - - *point_deform = *point; - point_deform->uw = point->uw ? MEM_dupallocN(point->uw) : NULL; - - mask_evaluate_apply_point_parent(point_deform, ctime); - - if (ELEM(point->bezt.h1, HD_AUTO, HD_VECT)) { - need_handle_recalc = true; - } - } - - /* if the spline has auto or vector handles, these need to be recalculated after deformation */ - if (need_handle_recalc) { - for (i = 0; i < spline->tot_point; i++) { - MaskSplinePoint *point_deform = &spline->points_deform[i]; - if (ELEM(point_deform->bezt.h1, HD_AUTO, HD_VECT)) { - BKE_mask_calc_handle_point(spline, point_deform); - } - } - } - /* end extra calc handles loop */ - } + BKE_mask_layer_evaluate_animation(masklay, ctime); } + /* Update deform. */ + BKE_mask_layer_evaluate_deform(masklay, ctime); } void BKE_mask_evaluate(Mask *mask, const float ctime, const bool do_newframe) diff --git a/source/blender/blenkernel/intern/mask_evaluate.c b/source/blender/blenkernel/intern/mask_evaluate.c index 1b275f455f4..f60d87f2464 100644 --- a/source/blender/blenkernel/intern/mask_evaluate.c +++ b/source/blender/blenkernel/intern/mask_evaluate.c @@ -42,8 +42,11 @@ #include "DNA_mask_types.h" #include "BKE_curve.h" +#include "BKE_global.h" #include "BKE_mask.h" +#include "DEG_depsgraph.h" + unsigned int BKE_mask_spline_resolution(MaskSpline *spline, int width, int height) { @@ -810,3 +813,111 @@ float *BKE_mask_point_segment_diff(MaskSpline *spline, MaskSplinePoint *point, return diff_points; } + +static void mask_evaluate_apply_point_parent(MaskSplinePoint *point, float ctime) +{ + float parent_matrix[3][3]; + BKE_mask_point_parent_matrix_get(point, ctime, parent_matrix); + mul_m3_v2(parent_matrix, point->bezt.vec[0]); + mul_m3_v2(parent_matrix, point->bezt.vec[1]); + mul_m3_v2(parent_matrix, point->bezt.vec[2]); +} + +void BKE_mask_layer_evaluate_animation(MaskLayer *masklay, const float ctime) +{ + /* animation if available */ + MaskLayerShape *masklay_shape_a; + MaskLayerShape *masklay_shape_b; + int found; + if ((found = BKE_mask_layer_shape_find_frame_range( + masklay, ctime, &masklay_shape_a, &masklay_shape_b))) + { + if (found == 1) { +#if 0 + printf("%s: exact %d %d (%d)\n", + __func__, + (int)ctime, + BLI_listbase_count(&masklay->splines_shapes), + masklay_shape_a->frame); +#endif + BKE_mask_layer_shape_to_mask(masklay, masklay_shape_a); + } + else if (found == 2) { + float w = masklay_shape_b->frame - masklay_shape_a->frame; +#if 0 + printf("%s: tween %d %d (%d %d)\n", + __func__, + (int)ctime, + BLI_listbase_count(&masklay->splines_shapes), + masklay_shape_a->frame, masklay_shape_b->frame); +#endif + BKE_mask_layer_shape_to_mask_interp( + masklay, + masklay_shape_a, masklay_shape_b, + (ctime - masklay_shape_a->frame) / w); + } + else { + /* always fail, should never happen */ + BLI_assert(found == 2); + } + } +} + +void BKE_mask_layer_evaluate_deform(MaskLayer *masklay, const float ctime) +{ + BKE_mask_layer_calc_handles(masklay); + for (MaskSpline *spline = masklay->splines.first; + spline != NULL; + spline = spline->next) + { + bool need_handle_recalc = false; + BKE_mask_spline_ensure_deform(spline); + for (int i = 0; i < spline->tot_point; i++) { + MaskSplinePoint *point = &spline->points[i]; + MaskSplinePoint *point_deform = &spline->points_deform[i]; + BKE_mask_point_free(point_deform); + *point_deform = *point; + point_deform->uw = point->uw ? MEM_dupallocN(point->uw) : NULL; + mask_evaluate_apply_point_parent(point_deform, ctime); + if (ELEM(point->bezt.h1, HD_AUTO, HD_VECT)) { + need_handle_recalc = true; + } + } + /* if the spline has auto or vector handles, these need to be + * recalculated after deformation. + */ + if (need_handle_recalc) { + for (int i = 0; i < spline->tot_point; i++) { + MaskSplinePoint *point_deform = &spline->points_deform[i]; + if (ELEM(point_deform->bezt.h1, HD_AUTO, HD_VECT)) { + BKE_mask_calc_handle_point(spline, point_deform); + } + } + } + /* end extra calc handles loop */ + } +} + +#define DEBUG_PRINT if (G.debug & G_DEBUG_DEPSGRAPH) printf + +void BKE_mask_eval_animation(struct EvaluationContext *eval_ctx, Mask *mask) +{ + DEBUG_PRINT("%s on %s (%p)\n", __func__, mask->id.name, mask); + for (MaskLayer *mask_layer = mask->masklayers.first; + mask_layer != NULL; + mask_layer = mask_layer->next) + { + BKE_mask_layer_evaluate_animation(mask_layer, eval_ctx->ctime); + } +} + +void BKE_mask_eval_update(struct EvaluationContext *eval_ctx, Mask *mask) +{ + DEBUG_PRINT("%s on %s (%p)\n", __func__, mask->id.name, mask); + for (MaskLayer *mask_layer = mask->masklayers.first; + mask_layer != NULL; + mask_layer = mask_layer->next) + { + BKE_mask_layer_evaluate_deform(mask_layer, eval_ctx->ctime); + } +} diff --git a/source/blender/blenkernel/intern/material.c b/source/blender/blenkernel/intern/material.c index 824151f9c98..3a742520471 100644 --- a/source/blender/blenkernel/intern/material.c +++ b/source/blender/blenkernel/intern/material.c @@ -207,59 +207,78 @@ void BKE_material_init(Material *ma) ma->mode2 = MA_CASTSHADOW; ma->shade_flag = MA_APPROX_OCCLUSION; ma->preview = NULL; + + ma->alpha_threshold = 0.5f; } Material *BKE_material_add(Main *bmain, const char *name) { Material *ma; - ma = BKE_libblock_alloc(bmain, ID_MA, name); + ma = BKE_libblock_alloc(bmain, ID_MA, name, 0); BKE_material_init(ma); return ma; } -/* XXX keep synced with next function */ -Material *BKE_material_copy(Main *bmain, const Material *ma) +/** + * Only copy internal data of Material ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_material_copy_data(Main *bmain, Material *ma_dst, const Material *ma_src, const int flag) { - Material *man; - int a; - - man = BKE_libblock_copy(bmain, &ma->id); - - id_lib_extern((ID *)man->group); - id_lib_extern((ID *)man->edit_image); - - for (a = 0; a < MAX_MTEX; a++) { - if (ma->mtex[a]) { - man->mtex[a] = MEM_mallocN(sizeof(MTex), "copymaterial"); - memcpy(man->mtex[a], ma->mtex[a], sizeof(MTex)); - id_us_plus((ID *)man->mtex[a]->tex); + for (int a = 0; a < MAX_MTEX; a++) { + if (ma_src->mtex[a]) { + ma_dst->mtex[a] = MEM_mallocN(sizeof(*ma_dst->mtex[a]), __func__); + *ma_dst->mtex[a] = *ma_src->mtex[a]; } } - - if (ma->ramp_col) man->ramp_col = MEM_dupallocN(ma->ramp_col); - if (ma->ramp_spec) man->ramp_spec = MEM_dupallocN(ma->ramp_spec); - - if (ma->nodetree) { - man->nodetree = ntreeCopyTree(bmain, ma->nodetree); + + if (ma_src->ramp_col) { + ma_dst->ramp_col = MEM_dupallocN(ma_src->ramp_col); + } + if (ma_src->ramp_spec) { + ma_dst->ramp_spec = MEM_dupallocN(ma_src->ramp_spec); } - BKE_previewimg_id_copy(&man->id, &ma->id); + if (ma_src->nodetree) { + BKE_id_copy_ex(bmain, (ID *)ma_src->nodetree, (ID **)&ma_dst->nodetree, flag, false); + } - BLI_listbase_clear(&man->gpumaterial); + if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0) { + BKE_previewimg_id_copy(&ma_dst->id, &ma_src->id); + } + else { + ma_dst->preview = NULL; + } - /* TODO Duplicate Engine Settings and set runtime to NULL */ + BLI_listbase_clear(&ma_dst->gpumaterial); - BKE_id_copy_ensure_local(bmain, &ma->id, &man->id); + /* TODO Duplicate Engine Settings and set runtime to NULL */ +} - return man; +Material *BKE_material_copy(Main *bmain, const Material *ma) +{ + Material *ma_copy; + BKE_id_copy_ex(bmain, &ma->id, (ID **)&ma_copy, 0, false); + return ma_copy; } /* XXX (see above) material copy without adding to main dbase */ Material *localize_material(Material *ma) { + /* TODO replace with something like + * Material *ma_copy; + * BKE_id_copy_ex(bmain, &ma->id, (ID **)&ma_copy, LIB_ID_COPY_NO_MAIN | LIB_ID_COPY_NO_PREVIEW | LIB_ID_COPY_NO_USER_REFCOUNT, false); + * return ma_copy; + * + * ... Once f*** nodes are fully converted to that too :( */ + Material *man; int a; @@ -349,6 +368,8 @@ Material ***give_matarar_id(ID *id) return &(((Curve *)id)->mat); case ID_MB: return &(((MetaBall *)id)->mat); + default: + break; } return NULL; } @@ -365,6 +386,8 @@ short *give_totcolp_id(ID *id) return &(((Curve *)id)->totcol); case ID_MB: return &(((MetaBall *)id)->totcol); + default: + break; } return NULL; } @@ -384,6 +407,8 @@ static void material_data_index_remove_id(ID *id, short index) case ID_MB: /* meta-elems don't have materials atm */ break; + default: + break; } } @@ -402,6 +427,8 @@ static void material_data_index_clear_id(ID *id) case ID_MB: /* meta-elems don't have materials atm */ break; + default: + break; } } @@ -1795,3 +1822,13 @@ bool BKE_object_material_edit_image_set(Object *ob, short mat_nr, Image *image) } return false; } + +void BKE_material_eval(const struct EvaluationContext *UNUSED(eval_ctx), Material *material) +{ + if (G.debug & G_DEBUG_DEPSGRAPH) { + printf("%s on %s (%p)\n", __func__, material->id.name, material); + } + if ((BLI_listbase_is_empty(&material->gpumaterial) == false)) { + GPU_material_uniform_buffer_tag_dirty(&material->gpumaterial); + } +} diff --git a/source/blender/blenkernel/intern/mball.c b/source/blender/blenkernel/intern/mball.c index 27e2b17b4e2..f1e549b1400 100644 --- a/source/blender/blenkernel/intern/mball.c +++ b/source/blender/blenkernel/intern/mball.c @@ -97,33 +97,36 @@ MetaBall *BKE_mball_add(Main *bmain, const char *name) { MetaBall *mb; - mb = BKE_libblock_alloc(bmain, ID_MB, name); + mb = BKE_libblock_alloc(bmain, ID_MB, name, 0); BKE_mball_init(mb); return mb; } -MetaBall *BKE_mball_copy(Main *bmain, const MetaBall *mb) +/** + * Only copy internal data of MetaBall ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_mball_copy_data(Main *UNUSED(bmain), MetaBall *mb_dst, const MetaBall *mb_src, const int UNUSED(flag)) { - MetaBall *mbn; - int a; - - mbn = BKE_libblock_copy(bmain, &mb->id); + BLI_duplicatelist(&mb_dst->elems, &mb_src->elems); - BLI_duplicatelist(&mbn->elems, &mb->elems); - - mbn->mat = MEM_dupallocN(mb->mat); - for (a = 0; a < mbn->totcol; a++) { - id_us_plus((ID *)mbn->mat[a]); - } + mb_dst->mat = MEM_dupallocN(mb_src->mat); - mbn->editelems = NULL; - mbn->lastelem = NULL; - - BKE_id_copy_ensure_local(bmain, &mb->id, &mbn->id); + mb_dst->editelems = NULL; + mb_dst->lastelem = NULL; +} - return mbn; +MetaBall *BKE_mball_copy(Main *bmain, const MetaBall *mb) +{ + MetaBall *mb_copy; + BKE_id_copy_ex(bmain, &mb->id, (ID **)&mb_copy, 0, false); + return mb_copy; } void BKE_mball_make_local(Main *bmain, MetaBall *mb, const bool lib_local) @@ -469,7 +472,7 @@ bool BKE_mball_center_bounds(MetaBall *mb, float r_cent[3]) return false; } -void BKE_mball_transform(MetaBall *mb, float mat[4][4]) +void BKE_mball_transform(MetaBall *mb, float mat[4][4], const bool do_props) { MetaElem *me; float quat[4]; @@ -481,14 +484,17 @@ void BKE_mball_transform(MetaBall *mb, float mat[4][4]) for (me = mb->elems.first; me; me = me->next) { mul_m4_v3(mat, &me->x); mul_qt_qtqt(me->quat, quat, me->quat); - me->rad *= scale; - /* hrmf, probably elems shouldn't be - * treating scale differently - campbell */ - if (!MB_TYPE_SIZE_SQUARED(me->type)) { - mul_v3_fl(&me->expx, scale); - } - else { - mul_v3_fl(&me->expx, scale_sqrt); + + if (do_props) { + me->rad *= scale; + /* hrmf, probably elems shouldn't be + * treating scale differently - campbell */ + if (!MB_TYPE_SIZE_SQUARED(me->type)) { + mul_v3_fl(&me->expx, scale); + } + else { + mul_v3_fl(&me->expx, scale_sqrt); + } } } } @@ -532,7 +538,7 @@ void BKE_mball_select_swap(struct MetaBall *mb) /* **** Depsgraph evaluation **** */ -void BKE_mball_eval_geometry(struct EvaluationContext *UNUSED(eval_ctx), +void BKE_mball_eval_geometry(const struct EvaluationContext *UNUSED(eval_ctx), MetaBall *UNUSED(mball)) { } diff --git a/source/blender/blenkernel/intern/mball_tessellate.c b/source/blender/blenkernel/intern/mball_tessellate.c index d897213d362..d8e3df239df 100644 --- a/source/blender/blenkernel/intern/mball_tessellate.c +++ b/source/blender/blenkernel/intern/mball_tessellate.c @@ -424,13 +424,13 @@ static void make_face(PROCESS *process, int i1, int i2, int i3, int i4) #ifdef USE_ACCUM_NORMAL if (i4 == 0) { normal_tri_v3(n, process->co[i1], process->co[i2], process->co[i3]); - accumulate_vertex_normals( + accumulate_vertex_normals_v3( process->no[i1], process->no[i2], process->no[i3], NULL, n, process->co[i1], process->co[i2], process->co[i3], NULL); } else { normal_quad_v3(n, process->co[i1], process->co[i2], process->co[i3], process->co[i4]); - accumulate_vertex_normals( + accumulate_vertex_normals_v3( process->no[i1], process->no[i2], process->no[i3], process->no[i4], n, process->co[i1], process->co[i2], process->co[i3], process->co[i4]); } @@ -1079,7 +1079,7 @@ static void polygonize(PROCESS *process) * Iterates over ALL objects in the scene and all of its sets, including * making all duplis(not only metas). Copies metas to mainb array. * Computes bounding boxes for building BVH. */ -static void init_meta(EvaluationContext *eval_ctx, PROCESS *process, Scene *scene, Object *ob) +static void init_meta(const EvaluationContext *eval_ctx, PROCESS *process, Scene *scene, Object *ob) { Scene *sce_iter = scene; BaseLegacy *base; @@ -1256,7 +1256,7 @@ static void init_meta(EvaluationContext *eval_ctx, PROCESS *process, Scene *scen } } -void BKE_mball_polygonize(EvaluationContext *eval_ctx, Scene *scene, Object *ob, ListBase *dispbase) +void BKE_mball_polygonize(const EvaluationContext *eval_ctx, Scene *scene, Object *ob, ListBase *dispbase) { MetaBall *mb; DispList *dl; diff --git a/source/blender/blenkernel/intern/mesh.c b/source/blender/blenkernel/intern/mesh.c index 9c0881e79ff..8f41f0611d4 100644 --- a/source/blender/blenkernel/intern/mesh.c +++ b/source/blender/blenkernel/intern/mesh.c @@ -387,6 +387,48 @@ void BKE_mesh_ensure_skin_customdata(Mesh *me) } } +bool BKE_mesh_ensure_facemap_customdata(struct Mesh *me) +{ + BMesh *bm = me->edit_btmesh ? me->edit_btmesh->bm : NULL; + bool changed = false; + if (bm) { + if (!CustomData_has_layer(&bm->pdata, CD_FACEMAP)) { + BM_data_layer_add(bm, &bm->pdata, CD_FACEMAP); + changed = true; + } + } + else { + if (!CustomData_has_layer(&me->pdata, CD_FACEMAP)) { + CustomData_add_layer(&me->pdata, + CD_FACEMAP, + CD_DEFAULT, + NULL, + me->totpoly); + changed = true; + } + } + return changed; +} + +bool BKE_mesh_clear_facemap_customdata(struct Mesh *me) +{ + BMesh *bm = me->edit_btmesh ? me->edit_btmesh->bm : NULL; + bool changed = false; + if (bm) { + if (CustomData_has_layer(&bm->pdata, CD_FACEMAP)) { + BM_data_layer_free(bm, &bm->pdata, CD_FACEMAP); + changed = true; + } + } + else { + if (CustomData_has_layer(&me->pdata, CD_FACEMAP)) { + CustomData_free_layers(&me->pdata, CD_FACEMAP, me->totpoly); + changed = true; + } + } + return changed; +} + /* this ensures grouped customdata (e.g. mtexpoly and mloopuv and mtface, or * mloopcol and mcol) have the same relative active/render/clone/mask indices. * @@ -491,54 +533,57 @@ Mesh *BKE_mesh_add(Main *bmain, const char *name) { Mesh *me; - me = BKE_libblock_alloc(bmain, ID_ME, name); + me = BKE_libblock_alloc(bmain, ID_ME, name, 0); BKE_mesh_init(me); return me; } -Mesh *BKE_mesh_copy(Main *bmain, const Mesh *me) +/** + * Only copy internal data of Mesh ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_mesh_copy_data(Main *bmain, Mesh *me_dst, const Mesh *me_src, const int flag) { - Mesh *men; - int a; - const int do_tessface = ((me->totface != 0) && (me->totpoly == 0)); /* only do tessface if we have no polys */ - - men = BKE_libblock_copy(bmain, &me->id); - - men->mat = MEM_dupallocN(me->mat); - for (a = 0; a < men->totcol; a++) { - id_us_plus((ID *)men->mat[a]); - } - id_us_plus((ID *)men->texcomesh); + const bool do_tessface = ((me_src->totface != 0) && (me_src->totpoly == 0)); /* only do tessface if we have no polys */ + + me_dst->mat = MEM_dupallocN(me_src->mat); - CustomData_copy(&me->vdata, &men->vdata, CD_MASK_MESH, CD_DUPLICATE, men->totvert); - CustomData_copy(&me->edata, &men->edata, CD_MASK_MESH, CD_DUPLICATE, men->totedge); - CustomData_copy(&me->ldata, &men->ldata, CD_MASK_MESH, CD_DUPLICATE, men->totloop); - CustomData_copy(&me->pdata, &men->pdata, CD_MASK_MESH, CD_DUPLICATE, men->totpoly); + CustomData_copy(&me_src->vdata, &me_dst->vdata, CD_MASK_MESH, CD_DUPLICATE, me_dst->totvert); + CustomData_copy(&me_src->edata, &me_dst->edata, CD_MASK_MESH, CD_DUPLICATE, me_dst->totedge); + CustomData_copy(&me_src->ldata, &me_dst->ldata, CD_MASK_MESH, CD_DUPLICATE, me_dst->totloop); + CustomData_copy(&me_src->pdata, &me_dst->pdata, CD_MASK_MESH, CD_DUPLICATE, me_dst->totpoly); if (do_tessface) { - CustomData_copy(&me->fdata, &men->fdata, CD_MASK_MESH, CD_DUPLICATE, men->totface); + CustomData_copy(&me_src->fdata, &me_dst->fdata, CD_MASK_MESH, CD_DUPLICATE, me_dst->totface); } else { - mesh_tessface_clear_intern(men, false); + mesh_tessface_clear_intern(me_dst, false); } - BKE_mesh_update_customdata_pointers(men, do_tessface); + BKE_mesh_update_customdata_pointers(me_dst, do_tessface); - men->edit_btmesh = NULL; - men->batch_cache = NULL; + me_dst->edit_btmesh = NULL; + me_dst->batch_cache = NULL; - men->mselect = MEM_dupallocN(men->mselect); - men->bb = MEM_dupallocN(men->bb); + me_dst->mselect = MEM_dupallocN(me_dst->mselect); + me_dst->bb = MEM_dupallocN(me_dst->bb); - if (me->key) { - men->key = BKE_key_copy(bmain, me->key); - men->key->from = (ID *)men; + /* TODO Do we want to add flag to prevent this? */ + if (me_src->key) { + BKE_id_copy_ex(bmain, &me_src->key->id, (ID **)&me_dst->key, flag, false); } +} - BKE_id_copy_ensure_local(bmain, &me->id, &men->id); - - return men; +Mesh *BKE_mesh_copy(Main *bmain, const Mesh *me) +{ + Mesh *me_copy; + BKE_id_copy_ex(bmain, &me->id, (ID **)&me_copy, 0, false); + return me_copy; } BMesh *BKE_mesh_to_bmesh( @@ -581,43 +626,44 @@ bool BKE_mesh_uv_cdlayer_rename_index(Mesh *me, const int poly_index, const int ldata = &me->ldata; fdata = &me->fdata; } - cdlp = (poly_index != -1) ? &pdata->layers[poly_index] : NULL; - cdlu = &ldata->layers[loop_index]; - cdlf = fdata && do_tessface ? &fdata->layers[face_index] : NULL; - if (cdlp == NULL && cdlf == NULL) { - return false; - } + cdlu = &ldata->layers[loop_index]; + cdlp = (poly_index != -1) ? &pdata->layers[poly_index] : NULL; + cdlf = (face_index != -1) && fdata && do_tessface ? &fdata->layers[face_index] : NULL; if (cdlu->name != new_name) { /* Mesh validate passes a name from the CD layer as the new name, * Avoid memcpy from self to self in this case. */ BLI_strncpy(cdlu->name, new_name, sizeof(cdlu->name)); - CustomData_set_layer_unique_name(pdata, cdlu - pdata->layers); + CustomData_set_layer_unique_name(ldata, loop_index); + } + + if (cdlp == NULL && cdlf == NULL) { + return false; } /* Loop until we do have exactly the same name for all layers! */ for (i = 1; - (cdlp && !STREQ(cdlp->name, cdlu->name)) || - (cdlf && !STREQ(cdlp->name, cdlf->name)); + (cdlp && !STREQ(cdlu->name, cdlp->name)) || + (cdlf && !STREQ(cdlu->name, cdlf->name)); i++) { switch (i % step) { case 0: if (cdlp) { BLI_strncpy(cdlp->name, cdlu->name, sizeof(cdlp->name)); - CustomData_set_layer_unique_name(pdata, cdlp - pdata->layers); + CustomData_set_layer_unique_name(pdata, poly_index); } break; case 1: BLI_strncpy(cdlu->name, cdlp->name, sizeof(cdlu->name)); - CustomData_set_layer_unique_name(ldata, cdlu - ldata->layers); + CustomData_set_layer_unique_name(ldata, loop_index); break; case 2: if (cdlf) { BLI_strncpy(cdlf->name, cdlu->name, sizeof(cdlf->name)); - CustomData_set_layer_unique_name(fdata, cdlf - fdata->layers); + CustomData_set_layer_unique_name(fdata, face_index); } break; } @@ -1585,10 +1631,10 @@ void BKE_mesh_to_curve_nurblist(DerivedMesh *dm, ListBase *nurblist, const int e } } -void BKE_mesh_to_curve(Scene *scene, Object *ob) +void BKE_mesh_to_curve(const EvaluationContext *eval_ctx, Scene *scene, Object *ob) { /* make new mesh data from the original copy */ - DerivedMesh *dm = mesh_get_derived_final(scene, ob, CD_MASK_MESH); + DerivedMesh *dm = mesh_get_derived_final(eval_ctx, scene, ob, CD_MASK_MESH); ListBase nurblist = {NULL, NULL}; bool needsFree = false; @@ -2053,6 +2099,12 @@ void BKE_mesh_mselect_active_set(Mesh *me, int index, int type) (me->mselect[me->totselect - 1].type == type)); } +/** + * Compute 'split' (aka loop, or per face corner's) normals. + * + * \param r_lnors_spacearr Allows to get computed loop normal space array. That data, among other things, + * contains 'smooth fan' info, useful e.g. to split geometry along sharp edges... + */ void BKE_mesh_calc_normals_split_ex(Mesh *mesh, MLoopNorSpaceArray *r_lnors_spacearr) { float (*r_loopnors)[3]; @@ -2060,6 +2112,11 @@ void BKE_mesh_calc_normals_split_ex(Mesh *mesh, MLoopNorSpaceArray *r_lnors_spac short (*clnors)[2] = NULL; bool free_polynors = false; + /* Note that we enforce computing clnors when the clnor space array is requested by caller here. + * However, we obviously only use the autosmooth angle threshold only in case autosmooth is enabled. */ + const bool use_split_normals = (r_lnors_spacearr != NULL) || ((mesh->flag & ME_AUTOSMOOTH) != 0); + const float split_angle = (mesh->flag & ME_AUTOSMOOTH) != 0 ? mesh->smoothresh : (float)M_PI; + if (CustomData_has_layer(&mesh->ldata, CD_NORMAL)) { r_loopnors = CustomData_get_layer(&mesh->ldata, CD_NORMAL); memset(r_loopnors, 0, sizeof(float[3]) * mesh->totloop); @@ -2088,7 +2145,7 @@ void BKE_mesh_calc_normals_split_ex(Mesh *mesh, MLoopNorSpaceArray *r_lnors_spac BKE_mesh_normals_loop_split( mesh->mvert, mesh->totvert, mesh->medge, mesh->totedge, mesh->mloop, r_loopnors, mesh->totloop, mesh->mpoly, (const float (*)[3])polynors, mesh->totpoly, - (mesh->flag & ME_AUTOSMOOTH) != 0, mesh->smoothresh, r_lnors_spacearr, clnors, NULL); + use_split_normals, split_angle, r_lnors_spacearr, clnors, NULL); if (free_polynors) { MEM_freeN(polynors); @@ -2120,118 +2177,70 @@ typedef struct SplitFaceNewEdge { /* Detect needed new vertices, and update accordingly loops' vertex indices. * WARNING! Leaves mesh in invalid state. */ static int split_faces_prepare_new_verts( - const Mesh *mesh, MLoopNorSpaceArray *lnors_spacearr, SplitFaceNewVert **new_verts, MemArena *memarena, - bool *r_need_vnors_recalc) + const Mesh *mesh, MLoopNorSpaceArray *lnors_spacearr, SplitFaceNewVert **new_verts, MemArena *memarena) { - /* Note: if lnors_spacearr is NULL, ther is no autosmooth handling, and we only split out flat polys. */ + /* This is now mandatory, trying to do the job in simple way without that data is doomed to fail, even when only + * dealing with smooth/flat faces one can find cases that no simple algorithm can handle properly. */ + BLI_assert(lnors_spacearr != NULL); + const int num_loops = mesh->totloop; int num_verts = mesh->totvert; MVert *mvert = mesh->mvert; MLoop *mloop = mesh->mloop; BLI_bitmap *verts_used = BLI_BITMAP_NEW(num_verts, __func__); + BLI_bitmap *done_loops = BLI_BITMAP_NEW(num_loops, __func__); - if (lnors_spacearr) { - BLI_bitmap *done_loops = BLI_BITMAP_NEW(num_loops, __func__); - - MLoop *ml = mloop; - MLoopNorSpace **lnor_space = lnors_spacearr->lspacearr; - for (int loop_idx = 0; loop_idx < num_loops; loop_idx++, ml++, lnor_space++) { - if (!BLI_BITMAP_TEST(done_loops, loop_idx)) { - const int vert_idx = ml->v; - const bool vert_used = BLI_BITMAP_TEST_BOOL(verts_used, vert_idx); - /* If vert is already used by another smooth fan, we need a new vert for this one. */ - const int new_vert_idx = vert_used ? num_verts++ : vert_idx; - - BLI_assert(*lnor_space); - - if ((*lnor_space)->loops) { - for (LinkNode *lnode = (*lnor_space)->loops; lnode; lnode = lnode->next) { - const int ml_fan_idx = GET_INT_FROM_POINTER(lnode->link); - BLI_BITMAP_ENABLE(done_loops, ml_fan_idx); - if (vert_used) { - mloop[ml_fan_idx].v = new_vert_idx; - } - } - } - else { - /* Single loop in this fan... */ - BLI_BITMAP_ENABLE(done_loops, loop_idx); + MLoop *ml = mloop; + MLoopNorSpace **lnor_space = lnors_spacearr->lspacearr; + + for (int loop_idx = 0; loop_idx < num_loops; loop_idx++, ml++, lnor_space++) { + if (!BLI_BITMAP_TEST(done_loops, loop_idx)) { + const int vert_idx = ml->v; + const bool vert_used = BLI_BITMAP_TEST_BOOL(verts_used, vert_idx); + /* If vert is already used by another smooth fan, we need a new vert for this one. */ + const int new_vert_idx = vert_used ? num_verts++ : vert_idx; + + BLI_assert(*lnor_space); + + if ((*lnor_space)->loops) { + for (LinkNode *lnode = (*lnor_space)->loops; lnode; lnode = lnode->next) { + const int ml_fan_idx = GET_INT_FROM_POINTER(lnode->link); + BLI_BITMAP_ENABLE(done_loops, ml_fan_idx); if (vert_used) { - ml->v = new_vert_idx; + mloop[ml_fan_idx].v = new_vert_idx; } } - - if (!vert_used) { - BLI_BITMAP_ENABLE(verts_used, vert_idx); - /* We need to update that vertex's normal here, we won't go over it again. */ - /* This is important! *DO NOT* set vnor to final computed lnor, vnor should always be defined to - * 'automatic normal' value computed from its polys, not some custom normal. - * Fortunately, that's the loop normal space's 'lnor' reference vector. ;) */ - normal_float_to_short_v3(mvert[vert_idx].no, (*lnor_space)->vec_lnor); - } - else { - /* Add new vert to list. */ - SplitFaceNewVert *new_vert = BLI_memarena_alloc(memarena, sizeof(*new_vert)); - new_vert->orig_index = vert_idx; - new_vert->new_index = new_vert_idx; - new_vert->vnor = (*lnor_space)->vec_lnor; /* See note above. */ - new_vert->next = *new_verts; - *new_verts = new_vert; - } } - } - - MEM_freeN(done_loops); - } - else { - /* No loop normal spaces available, we only split out flat polys. */ - const int num_polys = mesh->totpoly; - const MPoly *mpoly = mesh->mpoly; - - /* We do that in two loops, to keep original edges/verts to smooth polys preferencially. */ - const MPoly *mp = mpoly; - for (int i = 0; i < num_polys; i++, mp++) { - if (mp->flag & ME_SMOOTH) { - const MLoop *ml = &mloop[mp->loopstart]; - for (int j = 0; j < mp->totloop; j++, ml++) { - /* Just mark the vertex as used/reserved, that way neighbor flat polys, if any, - * will have to create their own. */ - BLI_BITMAP_ENABLE(verts_used, ml->v); + else { + /* Single loop in this fan... */ + BLI_BITMAP_ENABLE(done_loops, loop_idx); + if (vert_used) { + ml->v = new_vert_idx; } } - } - mp = mpoly; - for (int i = 0; i < num_polys; i++, mp++) { - if (!(mp->flag & ME_SMOOTH)) { - MLoop *ml = &mloop[mp->loopstart]; - for (int j = 0; j < mp->totloop; j++, ml++) { - const int vert_idx = ml->v; - - if (BLI_BITMAP_TEST(verts_used, vert_idx)) { - /* Add new vert to list. */ - const int new_vert_idx = num_verts++; - ml->v = new_vert_idx; - - SplitFaceNewVert *new_vert = BLI_memarena_alloc(memarena, sizeof(*new_vert)); - new_vert->orig_index = vert_idx; - new_vert->new_index = new_vert_idx; - new_vert->vnor = NULL; /* See note below about normals. */ - new_vert->next = *new_verts; - *new_verts = new_vert; - } - else { - BLI_BITMAP_ENABLE(verts_used, vert_idx); - } - } - /* Note: there is no way to get new normals for smooth vertices here (and we don't have direct access - * to poly normals either for flat ones), so we'll have to recompute all vnors at the end... */ - *r_need_vnors_recalc = true; + if (!vert_used) { + BLI_BITMAP_ENABLE(verts_used, vert_idx); + /* We need to update that vertex's normal here, we won't go over it again. */ + /* This is important! *DO NOT* set vnor to final computed lnor, vnor should always be defined to + * 'automatic normal' value computed from its polys, not some custom normal. + * Fortunately, that's the loop normal space's 'lnor' reference vector. ;) */ + normal_float_to_short_v3(mvert[vert_idx].no, (*lnor_space)->vec_lnor); + } + else { + /* Add new vert to list. */ + SplitFaceNewVert *new_vert = BLI_memarena_alloc(memarena, sizeof(*new_vert)); + new_vert->orig_index = vert_idx; + new_vert->new_index = new_vert_idx; + new_vert->vnor = (*lnor_space)->vec_lnor; /* See note above. */ + new_vert->next = *new_verts; + *new_verts = new_vert; } } } + MEM_freeN(done_loops); MEM_freeN(verts_used); return num_verts - mesh->totvert; @@ -2350,27 +2359,17 @@ void BKE_mesh_split_faces(Mesh *mesh, bool free_loop_normals) } BKE_mesh_tessface_clear(mesh); - MLoopNorSpaceArray *lnors_spacearr = NULL; - MemArena *memarena; - bool need_vnors_recalc = false; - - if (mesh->flag & ME_AUTOSMOOTH) { - lnors_spacearr = MEM_callocN(sizeof(*lnors_spacearr), __func__); - /* Compute loop normals and loop normal spaces (a.k.a. smooth fans of faces around vertices). */ - BKE_mesh_calc_normals_split_ex(mesh, lnors_spacearr); - /* Stealing memarena from loop normals space array. */ - memarena = lnors_spacearr->mem; - } - else { - /* We still have to split out flat faces... */ - memarena = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__); - } + MLoopNorSpaceArray lnors_spacearr = {NULL}; + /* Compute loop normals and loop normal spaces (a.k.a. smooth fans of faces around vertices). */ + BKE_mesh_calc_normals_split_ex(mesh, &lnors_spacearr); + /* Stealing memarena from loop normals space array. */ + MemArena *memarena = lnors_spacearr.mem; SplitFaceNewVert *new_verts = NULL; SplitFaceNewEdge *new_edges = NULL; /* Detect loop normal spaces (a.k.a. smooth fans) that will need a new vert. */ - const int num_new_verts = split_faces_prepare_new_verts(mesh, lnors_spacearr, &new_verts, memarena, &need_vnors_recalc); + const int num_new_verts = split_faces_prepare_new_verts(mesh, &lnors_spacearr, &new_verts, memarena); if (num_new_verts > 0) { /* Reminder: beyond this point, there is no way out, mesh is in invalid state (due to early-reassignment of @@ -2382,9 +2381,9 @@ void BKE_mesh_split_faces(Mesh *mesh, bool free_loop_normals) /* Reallocate all vert and edge related data. */ mesh->totvert += num_new_verts; - mesh->totedge += num_new_edges; CustomData_realloc(&mesh->vdata, mesh->totvert); if (do_edges) { + mesh->totedge += num_new_edges; CustomData_realloc(&mesh->edata, mesh->totedge); } /* Update pointers to a newly allocated memory. */ @@ -2404,18 +2403,9 @@ void BKE_mesh_split_faces(Mesh *mesh, bool free_loop_normals) CustomData_free_layers(&mesh->ldata, CD_NORMAL, mesh->totloop); } - if (lnors_spacearr) { - /* Also frees new_verts/edges temp data, since we used its memarena to allocate them. */ - BKE_lnor_spacearr_free(lnors_spacearr); - MEM_freeN(lnors_spacearr); - } - else { - BLI_memarena_free(memarena); - } + /* Also frees new_verts/edges temp data, since we used its memarena to allocate them. */ + BKE_lnor_spacearr_free(&lnors_spacearr); - if (need_vnors_recalc) { - BKE_mesh_calc_normals(mesh); - } #ifdef VALIDATE_MESH BKE_mesh_validate(mesh, true, true); #endif @@ -2423,7 +2413,7 @@ void BKE_mesh_split_faces(Mesh *mesh, bool free_loop_normals) /* settings: 1 - preview, 2 - render */ Mesh *BKE_mesh_new_from_object( - Main *bmain, Scene *sce, Object *ob, + const EvaluationContext *eval_ctx, Main *bmain, Scene *sce, Object *ob, int apply_modifiers, int settings, int calc_tessface, int calc_undeformed) { Mesh *tmpmesh; @@ -2444,7 +2434,9 @@ Mesh *BKE_mesh_new_from_object( int uv_from_orco; /* copies object and modifiers (but not the data) */ - Object *tmpobj = BKE_object_copy_ex(bmain, ob, true); + Object *tmpobj; + /* TODO: make it temp copy outside bmain! */ + BKE_id_copy_ex(bmain, &ob->id, (ID **)&tmpobj, LIB_ID_COPY_CACHES, false); tmpcu = (Curve *)tmpobj->data; id_us_min(&tmpcu->id); @@ -2475,7 +2467,7 @@ Mesh *BKE_mesh_new_from_object( copycu->editnurb = tmpcu->editnurb; /* get updated display list, and convert to a mesh */ - BKE_displist_make_curveTypes_forRender(sce, tmpobj, &dispbase, &derivedFinal, false, render); + BKE_displist_make_curveTypes_forRender(eval_ctx, sce, tmpobj, &dispbase, &derivedFinal, false, render); copycu->editfont = NULL; copycu->editnurb = NULL; @@ -2526,13 +2518,7 @@ Mesh *BKE_mesh_new_from_object( if (render) { ListBase disp = {NULL, NULL}; - /* TODO(sergey): This is gonna to work for until EvaluationContext - * only contains for_render flag. As soon as CoW is - * implemented, this is to be rethinked. - */ - EvaluationContext eval_ctx; - DEG_evaluation_context_init(&eval_ctx, DAG_EVAL_RENDER); - BKE_displist_make_mball_forRender(&eval_ctx, sce, ob, &disp); + BKE_displist_make_mball_forRender(eval_ctx, sce, ob, &disp); BKE_mesh_from_metaball(&disp, tmpmesh); BKE_displist_free(&disp); } @@ -2571,9 +2557,9 @@ Mesh *BKE_mesh_new_from_object( /* Write the display mesh into the dummy mesh */ if (render) - dm = mesh_create_derived_render(sce, ob, mask); + dm = mesh_create_derived_render(eval_ctx, sce, ob, mask); else - dm = mesh_create_derived_view(sce, ob, mask); + dm = mesh_create_derived_view(eval_ctx, sce, ob, mask); tmpmesh = BKE_mesh_add(bmain, ((ID *)ob->data)->name + 2); DM_to_mesh(dm, tmpmesh, ob, mask, true); @@ -2664,7 +2650,7 @@ Mesh *BKE_mesh_new_from_object( /* **** Depsgraph evaluation **** */ -void BKE_mesh_eval_geometry(EvaluationContext *UNUSED(eval_ctx), +void BKE_mesh_eval_geometry(const EvaluationContext *UNUSED(eval_ctx), Mesh *mesh) { if (G.debug & G_DEBUG_DEPSGRAPH) { @@ -2690,4 +2676,4 @@ void BKE_mesh_batch_cache_free(Mesh *me) if (me->batch_cache) { BKE_mesh_batch_cache_free_cb(me); } -}
\ No newline at end of file +} diff --git a/source/blender/blenkernel/intern/mesh_evaluate.c b/source/blender/blenkernel/intern/mesh_evaluate.c index ca8b58ffae3..4a1952c798b 100644 --- a/source/blender/blenkernel/intern/mesh_evaluate.c +++ b/source/blender/blenkernel/intern/mesh_evaluate.c @@ -225,7 +225,7 @@ static void mesh_calc_normals_poly_accum_task_cb(void *userdata, const int pidx) } /* accumulate angle weighted face normal */ - /* inline version of #accumulate_vertex_normals_poly */ + /* inline version of #accumulate_vertex_normals_poly_v3 */ { const float *prev_edge = edgevecbuf[nverts - 1]; @@ -334,8 +334,9 @@ void BKE_mesh_calc_normals_tessface( else normal_tri_v3(f_no, mverts[mf->v1].co, mverts[mf->v2].co, mverts[mf->v3].co); - accumulate_vertex_normals(tnorms[mf->v1], tnorms[mf->v2], tnorms[mf->v3], n4, - f_no, mverts[mf->v1].co, mverts[mf->v2].co, mverts[mf->v3].co, c4); + accumulate_vertex_normals_v3( + tnorms[mf->v1], tnorms[mf->v2], tnorms[mf->v3], n4, + f_no, mverts[mf->v1].co, mverts[mf->v2].co, mverts[mf->v3].co, c4); } /* following Mesh convention; we use vertex coordinate itself for normal in this case */ @@ -379,7 +380,7 @@ void BKE_mesh_calc_normals_looptri( f_no, mverts[vtri[0]].co, mverts[vtri[1]].co, mverts[vtri[2]].co); - accumulate_vertex_normals_tri( + accumulate_vertex_normals_tri_v3( tnorms[vtri[0]], tnorms[vtri[1]], tnorms[vtri[2]], f_no, mverts[vtri[0]].co, mverts[vtri[1]].co, mverts[vtri[2]].co); } @@ -845,7 +846,7 @@ static void split_loop_nor_fan_do(LoopSplitTaskDataCommon *common_data, LoopSpli // printf("\thandling edge %d / loop %d\n", mlfan_curr->e, mlfan_curr_index); { - /* Code similar to accumulate_vertex_normals_poly. */ + /* Code similar to accumulate_vertex_normals_poly_v3. */ /* Calculate angle between the two poly edges incident on this vertex. */ const float fac = saacos(dot_v3v3(vec_curr, vec_prev)); /* Accumulate */ @@ -1856,11 +1857,14 @@ float BKE_mesh_calc_poly_area( * - http://forums.cgsociety.org/archive/index.php?t-756235.html * - http://www.globalspec.com/reference/52702/203279/4-8-the-centroid-of-a-tetrahedron * - * \note volume is 6x actual volume, and centroid is 4x actual volume-weighted centroid - * (so division can be done once at the end) - * \note results will have bias if polygon is non-planar. + * \note + * - Volume is 6x actual volume, and centroid is 4x actual volume-weighted centroid + * (so division can be done once at the end). + * - Results will have bias if polygon is non-planar. + * - The resulting volume will only be correct if the mesh is manifold and has consistent face winding + * (non-contiguous face normals or holes in the mesh surface). */ -static float mesh_calc_poly_volume_and_weighted_centroid( +static float mesh_calc_poly_volume_centroid( const MPoly *mpoly, const MLoop *loopstart, const MVert *mvarray, float r_cent[3]) { @@ -1897,6 +1901,43 @@ static float mesh_calc_poly_volume_and_weighted_centroid( return total_volume; } +/** + * \note + * - Results won't be correct if polygon is non-planar. + * - This has the advantage over #mesh_calc_poly_volume_centroid + * that it doesn't depend on solid geometry, instead it weights the surface by volume. + */ +static float mesh_calc_poly_area_centroid( + const MPoly *mpoly, const MLoop *loopstart, const MVert *mvarray, + float r_cent[3]) +{ + int i; + float tri_area; + float total_area = 0.0f; + float v1[3], v2[3], v3[3], normal[3], tri_cent[3]; + + BKE_mesh_calc_poly_normal(mpoly, loopstart, mvarray, normal); + copy_v3_v3(v1, mvarray[loopstart[0].v].co); + copy_v3_v3(v2, mvarray[loopstart[1].v].co); + zero_v3(r_cent); + + for (i = 2; i < mpoly->totloop; i++) { + copy_v3_v3(v3, mvarray[loopstart[i].v].co); + + tri_area = area_tri_signed_v3(v1, v2, v3, normal); + total_area += tri_area; + + mid_v3_v3v3v3(tri_cent, v1, v2, v3); + madd_v3_v3fl(r_cent, tri_cent, tri_area); + + copy_v3_v3(v2, v3); + } + + mul_v3_fl(r_cent, 1.0f / total_area); + + return total_area; +} + #if 0 /* slow version of the function below */ void BKE_mesh_calc_poly_angles(MPoly *mpoly, MLoop *loopstart, MVert *mvarray, float angles[]) @@ -2011,7 +2052,40 @@ bool BKE_mesh_center_bounds(const Mesh *me, float r_cent[3]) return false; } -bool BKE_mesh_center_centroid(const Mesh *me, float r_cent[3]) +bool BKE_mesh_center_of_surface(const Mesh *me, float r_cent[3]) +{ + int i = me->totpoly; + MPoly *mpoly; + float poly_area; + float total_area = 0.0f; + float poly_cent[3]; + + zero_v3(r_cent); + + /* calculate a weighted average of polygon centroids */ + for (mpoly = me->mpoly; i--; mpoly++) { + poly_area = mesh_calc_poly_area_centroid(mpoly, me->mloop + mpoly->loopstart, me->mvert, poly_cent); + + madd_v3_v3fl(r_cent, poly_cent, poly_area); + total_area += poly_area; + } + /* otherwise we get NAN for 0 polys */ + if (me->totpoly) { + mul_v3_fl(r_cent, 1.0f / total_area); + } + + /* zero area faces cause this, fallback to median */ + if (UNLIKELY(!is_finite_v3(r_cent))) { + return BKE_mesh_center_median(me, r_cent); + } + + return (me->totpoly != 0); +} + +/** + * \note Mesh must be manifold with consistent face-winding, see #mesh_calc_poly_volume_centroid for details. + */ +bool BKE_mesh_center_of_volume(const Mesh *me, float r_cent[3]) { int i = me->totpoly; MPoly *mpoly; @@ -2023,7 +2097,7 @@ bool BKE_mesh_center_centroid(const Mesh *me, float r_cent[3]) /* calculate a weighted average of polyhedron centroids */ for (mpoly = me->mpoly; i--; mpoly++) { - poly_volume = mesh_calc_poly_volume_and_weighted_centroid(mpoly, me->mloop + mpoly->loopstart, me->mvert, poly_cent); + poly_volume = mesh_calc_poly_volume_centroid(mpoly, me->mloop + mpoly->loopstart, me->mvert, poly_cent); /* poly_cent is already volume-weighted, so no need to multiply by the volume */ add_v3_v3(r_cent, poly_cent); @@ -2043,6 +2117,7 @@ bool BKE_mesh_center_centroid(const Mesh *me, float r_cent[3]) return (me->totpoly != 0); } + /** \} */ diff --git a/source/blender/blenkernel/intern/mesh_remap.c b/source/blender/blenkernel/intern/mesh_remap.c index c5fa9b15896..d2fe8f27f4a 100644 --- a/source/blender/blenkernel/intern/mesh_remap.c +++ b/source/blender/blenkernel/intern/mesh_remap.c @@ -1184,7 +1184,6 @@ void BKE_mesh_remap_calc_loops_from_dm( bool polys_allocated_src; MPoly *polys_src = DM_get_poly_array(dm_src, &polys_allocated_src); const int num_polys_src = dm_src->getNumPolys(dm_src); - bool looptri_allocated_src = false; const MLoopTri *looptri_src = NULL; int num_looptri_src = 0; @@ -1374,17 +1373,11 @@ void BKE_mesh_remap_calc_loops_from_dm( if (dirty_tess_flag) { dm_src->dirty &= ~dirty_tess_flag; } - DM_ensure_looptri(dm_src); if (dirty_tess_flag) { dm_src->dirty |= dirty_tess_flag; } - looptri_src = DM_get_looptri_array( - dm_src, - verts_src, - polys_src, num_polys_src, - loops_src, num_loops_src, - &looptri_allocated_src); + looptri_src = dm_src->getLoopTriArray(dm_src); num_looptri_src = dm_src->getNumLoopTri(dm_src); looptri_active = BLI_BITMAP_NEW((size_t)num_looptri_src, __func__); @@ -1403,7 +1396,7 @@ void BKE_mesh_remap_calc_loops_from_dm( &treedata[tindex], verts_src, verts_allocated_src, loops_src, loops_allocated_src, - looptri_src, num_looptri_src, looptri_allocated_src, + looptri_src, num_looptri_src, false, looptri_active, num_looptri_active, bvh_epsilon, 2, 6); if (verts_allocated_src) { verts_allocated_src = false; /* Only 'give' our verts once, to first tree! */ @@ -1411,9 +1404,6 @@ void BKE_mesh_remap_calc_loops_from_dm( if (loops_allocated_src) { loops_allocated_src = false; /* Only 'give' our loops once, to first tree! */ } - if (looptri_allocated_src) { - looptri_allocated_src = false; /* Only 'give' our looptri once, to first tree! */ - } } MEM_freeN(looptri_active); @@ -1928,9 +1918,6 @@ void BKE_mesh_remap_calc_loops_from_dm( if (polys_allocated_src) { MEM_freeN(polys_src); } - if (looptri_allocated_src) { - MEM_freeN((void *)looptri_src); - } if (vert_to_loop_map_src) { MEM_freeN(vert_to_loop_map_src); } diff --git a/source/blender/blenkernel/intern/mesh_tangent.c b/source/blender/blenkernel/intern/mesh_tangent.c index b9d1025d7b1..77288bd672e 100644 --- a/source/blender/blenkernel/intern/mesh_tangent.c +++ b/source/blender/blenkernel/intern/mesh_tangent.c @@ -230,8 +230,6 @@ typedef struct { } SGLSLMeshToTangent; /* interface */ -#include "mikktspace.h" - static int dm_ts_GetNumFaces(const SMikkTSpaceContext *pContext) { SGLSLMeshToTangent *pMesh = pContext->m_pUserData; @@ -474,7 +472,7 @@ void BKE_mesh_calc_loop_tangent_step_0( const CustomData *loopData, bool calc_active_tangent, const char (*tangent_names)[MAX_NAME], int tangent_names_count, bool *rcalc_act, bool *rcalc_ren, int *ract_uv_n, int *rren_uv_n, - char *ract_uv_name, char *rren_uv_name, char *rtangent_mask) { + char *ract_uv_name, char *rren_uv_name, short *rtangent_mask) { /* Active uv in viewport */ int layer_index = CustomData_get_layer_index(loopData, CD_MLOOPUV); *ract_uv_n = CustomData_get_active_layer(loopData, CD_MLOOPUV); @@ -526,8 +524,11 @@ void BKE_mesh_calc_loop_tangent_step_0( add = true; } if (add) - *rtangent_mask |= (char)(1 << n); + *rtangent_mask |= (short)(1 << n); } + + if (uv_layer_num == 0) + *rtangent_mask |= DM_TANGENT_MASK_ORCO; } /** @@ -549,18 +550,16 @@ void BKE_mesh_calc_loop_tangent_ex( /* result */ CustomData *loopdata_out, const uint loopdata_out_len, - char *tangent_mask_curr_p) + short *tangent_mask_curr_p) { - BLI_assert(CustomData_number_of_layers(loopdata, CD_MLOOPUV) != 0); - int act_uv_n = -1; int ren_uv_n = -1; bool calc_act = false; bool calc_ren = false; char act_uv_name[MAX_NAME]; char ren_uv_name[MAX_NAME]; - char tangent_mask = 0; - char tangent_mask_curr = *tangent_mask_curr_p; + short tangent_mask = 0; + short tangent_mask_curr = *tangent_mask_curr_p; BKE_mesh_calc_loop_tangent_step_0( loopdata, calc_active_tangent, tangent_names, tangent_names_len, @@ -571,6 +570,8 @@ void BKE_mesh_calc_loop_tangent_ex( for (int i = 0; i < tangent_names_len; i++) if (tangent_names[i][0]) BKE_mesh_add_loop_tangent_named_layer_for_uv(loopdata, loopdata_out, (int)loopdata_out_len, tangent_names[i]); + if ((tangent_mask & DM_TANGENT_MASK_ORCO) && CustomData_get_named_layer_index(loopdata, CD_TANGENT, "") == -1) + CustomData_add_layer_named(loopdata_out, CD_TANGENT, CD_CALLOC, NULL, (int)loopdata_out_len, ""); if (calc_act && act_uv_name[0]) BKE_mesh_add_loop_tangent_named_layer_for_uv(loopdata, loopdata_out, (int)loopdata_out_len, act_uv_name); if (calc_ren && ren_uv_name[0]) @@ -602,7 +603,7 @@ void BKE_mesh_calc_loop_tangent_ex( #endif /* Calculation */ - { + if (looptri_len != 0) { TaskScheduler *scheduler = BLI_task_scheduler_get(); TaskPool *task_pool; task_pool = BLI_task_pool_create(scheduler, NULL); @@ -631,21 +632,25 @@ void BKE_mesh_calc_loop_tangent_ex( mesh2tangent->precomputedFaceNormals = poly_normals; mesh2tangent->orco = NULL; - mesh2tangent->mloopuv = CustomData_get_layer_named(loopdata, CD_MLOOPUV, loopdata->layers[index].name); + mesh2tangent->mloopuv = CustomData_get_layer_named(loopdata, CD_MLOOPUV, loopdata_out->layers[index].name); + + /* Fill the resulting tangent_mask */ if (!mesh2tangent->mloopuv) { - mesh2tangent->orco = vert_orco; - if (!mesh2tangent->orco) - continue; + mesh2tangent->orco = vert_orco; + if (!mesh2tangent->orco) + continue; + + tangent_mask_curr |= DM_TANGENT_MASK_ORCO; + } + else { + int uv_ind = CustomData_get_named_layer_index(loopdata, CD_MLOOPUV, loopdata_out->layers[index].name); + int uv_start = CustomData_get_layer_index(loopdata, CD_MLOOPUV); + BLI_assert(uv_ind != -1 && uv_start != -1); + BLI_assert(uv_ind - uv_start < MAX_MTFACE); + tangent_mask_curr |= (short)(1 << (uv_ind - uv_start)); } - mesh2tangent->tangent = loopdata_out->layers[index].data; - /* Fill the resulting tangent_mask */ - int uv_ind = CustomData_get_named_layer_index( - loopdata, CD_MLOOPUV, loopdata_out->layers[index].name); - int uv_start = CustomData_get_layer_index(loopdata, CD_MLOOPUV); - BLI_assert(uv_ind != -1 && uv_start != -1); - BLI_assert(uv_ind - uv_start < MAX_MTFACE); - tangent_mask_curr |= (char)(1 << (uv_ind - uv_start)); + mesh2tangent->tangent = loopdata_out->layers[index].data; BLI_task_pool_push(task_pool, DM_calc_loop_tangents_thread, mesh2tangent, false, TASK_PRIORITY_LOW); } @@ -653,6 +658,9 @@ void BKE_mesh_calc_loop_tangent_ex( BLI_task_pool_work_and_wait(task_pool); BLI_task_pool_free(task_pool); } + else { + tangent_mask_curr = tangent_mask; + } #ifdef USE_LOOPTRI_DETECT_QUADS if (face_as_quad_map) { MEM_freeN(face_as_quad_map); @@ -663,21 +671,19 @@ void BKE_mesh_calc_loop_tangent_ex( *tangent_mask_curr_p = tangent_mask_curr; - int uv_index, tan_index; - /* Update active layer index */ - uv_index = CustomData_get_layer_index_n(loopdata, CD_MLOOPUV, act_uv_n); - if (uv_index != -1) { - tan_index = CustomData_get_named_layer_index(loopdata_out, CD_TANGENT, loopdata->layers[uv_index].name); - CustomData_set_layer_active_index(loopdata_out, CD_TANGENT, tan_index); - } + int act_uv_index = CustomData_get_layer_index_n(loopdata, CD_MLOOPUV, act_uv_n); + if (act_uv_index != -1) { + int tan_index = CustomData_get_named_layer_index(loopdata, CD_TANGENT, loopdata->layers[act_uv_index].name); + CustomData_set_layer_active_index(loopdata, CD_TANGENT, tan_index); + }/* else tangent has been built from orco */ /* Update render layer index */ - uv_index = CustomData_get_layer_index_n(loopdata, CD_MLOOPUV, ren_uv_n); - if (uv_index != -1) { - tan_index = CustomData_get_named_layer_index(loopdata_out, CD_TANGENT, loopdata->layers[uv_index].name); - CustomData_set_layer_render_index(loopdata_out, CD_TANGENT, tan_index); - } + int ren_uv_index = CustomData_get_layer_index_n(loopdata, CD_MLOOPUV, ren_uv_n); + if (ren_uv_index != -1) { + int tan_index = CustomData_get_named_layer_index(loopdata, CD_TANGENT, loopdata->layers[ren_uv_index].name); + CustomData_set_layer_render_index(loopdata, CD_TANGENT, tan_index); + }/* else tangent has been built from orco */ } } diff --git a/source/blender/blenkernel/intern/modifier.c b/source/blender/blenkernel/intern/modifier.c index 2276d56b9c6..1a146dc67d1 100644 --- a/source/blender/blenkernel/intern/modifier.c +++ b/source/blender/blenkernel/intern/modifier.c @@ -59,6 +59,8 @@ #include "BKE_appdir.h" #include "BKE_key.h" +#include "BKE_library.h" +#include "BKE_library_query.h" #include "BKE_multires.h" #include "BKE_DerivedMesh.h" @@ -269,14 +271,37 @@ void modifier_copyData_generic(const ModifierData *md_src, ModifierData *md_dst) memcpy(md_dst_data, md_src_data, (size_t)mti->structSize - data_size); } -void modifier_copyData(ModifierData *md, ModifierData *target) +static void modifier_copy_data_id_us_cb(void *UNUSED(userData), Object *UNUSED(ob), ID **idpoin, int cb_flag) +{ + ID *id = *idpoin; + if (id != NULL && (cb_flag & IDWALK_CB_USER) != 0) { + id_us_plus(id); + } +} + +void modifier_copyData_ex(ModifierData *md, ModifierData *target, const int flag) { const ModifierTypeInfo *mti = modifierType_getInfo(md->type); target->mode = md->mode; - if (mti->copyData) + if (mti->copyData) { mti->copyData(md, target); + } + + if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) { + if (mti->foreachIDLink) { + mti->foreachIDLink(target, NULL, modifier_copy_data_id_us_cb, NULL); + } + else if (mti->foreachObjectLink) { + mti->foreachObjectLink(target, NULL, (ObjectWalkFunc)modifier_copy_data_id_us_cb, NULL); + } + } +} + +void modifier_copyData(ModifierData *md, ModifierData *target) +{ + modifier_copyData_ex(md, target, 0); } @@ -732,8 +757,8 @@ void modifier_path_init(char *path, int path_maxlen, const char *name) /* wrapper around ModifierTypeInfo.applyModifier that ensures valid normals */ struct DerivedMesh *modwrap_applyModifier( - ModifierData *md, Object *ob, - struct DerivedMesh *dm, + ModifierData *md, const struct EvaluationContext *eval_ctx, + Object *ob, struct DerivedMesh *dm, ModifierApplyFlag flag) { const ModifierTypeInfo *mti = modifierType_getInfo(md->type); @@ -742,12 +767,12 @@ struct DerivedMesh *modwrap_applyModifier( if (mti->dependsOnNormals && mti->dependsOnNormals(md)) { DM_ensure_normals(dm); } - return mti->applyModifier(md, ob, dm, flag); + return mti->applyModifier(md, eval_ctx, ob, dm, flag); } struct DerivedMesh *modwrap_applyModifierEM( - ModifierData *md, Object *ob, - struct BMEditMesh *em, + ModifierData *md, const struct EvaluationContext *eval_ctx, + Object *ob, struct BMEditMesh *em, DerivedMesh *dm, ModifierApplyFlag flag) { @@ -757,12 +782,12 @@ struct DerivedMesh *modwrap_applyModifierEM( if (mti->dependsOnNormals && mti->dependsOnNormals(md)) { DM_ensure_normals(dm); } - return mti->applyModifierEM(md, ob, em, dm, flag); + return mti->applyModifierEM(md, eval_ctx, ob, em, dm, flag); } void modwrap_deformVerts( - ModifierData *md, Object *ob, - DerivedMesh *dm, + ModifierData *md, const struct EvaluationContext *eval_ctx, + Object *ob, DerivedMesh *dm, float (*vertexCos)[3], int numVerts, ModifierApplyFlag flag) { @@ -772,11 +797,11 @@ void modwrap_deformVerts( if (dm && mti->dependsOnNormals && mti->dependsOnNormals(md)) { DM_ensure_normals(dm); } - mti->deformVerts(md, ob, dm, vertexCos, numVerts, flag); + mti->deformVerts(md, eval_ctx, ob, dm, vertexCos, numVerts, flag); } void modwrap_deformVertsEM( - ModifierData *md, Object *ob, + ModifierData *md, const struct EvaluationContext *eval_ctx, Object *ob, struct BMEditMesh *em, DerivedMesh *dm, float (*vertexCos)[3], int numVerts) { @@ -786,6 +811,6 @@ void modwrap_deformVertsEM( if (dm && mti->dependsOnNormals && mti->dependsOnNormals(md)) { DM_ensure_normals(dm); } - mti->deformVertsEM(md, ob, em, dm, vertexCos, numVerts); + mti->deformVertsEM(md, eval_ctx, ob, em, dm, vertexCos, numVerts); } /* end modifier callback wrappers */ diff --git a/source/blender/blenkernel/intern/movieclip.c b/source/blender/blenkernel/intern/movieclip.c index dfa8742a295..16d597e25fa 100644 --- a/source/blender/blenkernel/intern/movieclip.c +++ b/source/blender/blenkernel/intern/movieclip.c @@ -588,7 +588,7 @@ static MovieClip *movieclip_alloc(Main *bmain, const char *name) { MovieClip *clip; - clip = BKE_libblock_alloc(bmain, ID_MC, name); + clip = BKE_libblock_alloc(bmain, ID_MC, name, 0); clip->aspx = clip->aspy = 1.0f; @@ -1488,25 +1488,33 @@ void BKE_movieclip_free(MovieClip *clip) BKE_animdata_free((ID *) clip, false); } -MovieClip *BKE_movieclip_copy(Main *bmain, const MovieClip *clip) +/** + * Only copy internal data of MovieClip ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_movieclip_copy_data(Main *UNUSED(bmain), MovieClip *clip_dst, const MovieClip *clip_src, const int flag) { - MovieClip *clip_new; - - clip_new = BKE_libblock_copy(bmain, &clip->id); + /* We never handle usercount here for own data. */ + const int flag_subdata = flag | LIB_ID_CREATE_NO_USER_REFCOUNT; - clip_new->anim = NULL; - clip_new->cache = NULL; + clip_dst->anim = NULL; + clip_dst->cache = NULL; - BKE_tracking_copy(&clip_new->tracking, &clip->tracking); - clip_new->tracking_context = NULL; + BKE_tracking_copy(&clip_dst->tracking, &clip_src->tracking, flag_subdata); + clip_dst->tracking_context = NULL; - id_us_plus((ID *)clip_new->gpd); - - BKE_color_managed_colorspace_settings_copy(&clip_new->colorspace_settings, &clip->colorspace_settings); - - BKE_id_copy_ensure_local(bmain, &clip->id, &clip_new->id); + BKE_color_managed_colorspace_settings_copy(&clip_dst->colorspace_settings, &clip_src->colorspace_settings); +} - return clip_new; +MovieClip *BKE_movieclip_copy(Main *bmain, const MovieClip *clip) +{ + MovieClip *clip_copy; + BKE_id_copy_ex(bmain, &clip->id, (ID **)&clip_copy, 0, false); + return clip_copy; } void BKE_movieclip_make_local(Main *bmain, MovieClip *clip, const bool lib_local) diff --git a/source/blender/blenkernel/intern/multires.c b/source/blender/blenkernel/intern/multires.c index 9679b585e6f..7ef4b588dcd 100644 --- a/source/blender/blenkernel/intern/multires.c +++ b/source/blender/blenkernel/intern/multires.c @@ -276,14 +276,14 @@ static MDisps *multires_mdisps_initialize_hidden(Mesh *me, int level) return mdisps; } -DerivedMesh *get_multires_dm(Scene *scene, MultiresModifierData *mmd, Object *ob) +DerivedMesh *get_multires_dm(const struct EvaluationContext *eval_ctx, Scene *scene, MultiresModifierData *mmd, Object *ob) { ModifierData *md = (ModifierData *)mmd; const ModifierTypeInfo *mti = modifierType_getInfo(md->type); - DerivedMesh *tdm = mesh_get_derived_deform(scene, ob, CD_MASK_BAREMESH); + DerivedMesh *tdm = mesh_get_derived_deform(eval_ctx, scene, ob, CD_MASK_BAREMESH); DerivedMesh *dm; - dm = mti->applyModifier(md, ob, tdm, MOD_APPLY_USECACHE | MOD_APPLY_IGNORE_SIMPLIFY); + dm = mti->applyModifier(md, eval_ctx, ob, tdm, MOD_APPLY_USECACHE | MOD_APPLY_IGNORE_SIMPLIFY); if (dm == tdm) { dm = CDDM_copy(tdm); } @@ -397,10 +397,10 @@ void multires_force_render_update(Object *ob) multires_force_update(ob); } -int multiresModifier_reshapeFromDM(Scene *scene, MultiresModifierData *mmd, +int multiresModifier_reshapeFromDM(const struct EvaluationContext *eval_ctx, Scene *scene, MultiresModifierData *mmd, Object *ob, DerivedMesh *srcdm) { - DerivedMesh *mrdm = get_multires_dm(scene, mmd, ob); + DerivedMesh *mrdm = get_multires_dm(eval_ctx, scene, mmd, ob); if (mrdm && srcdm && mrdm->getNumVerts(mrdm) == srcdm->getNumVerts(srcdm)) { multires_mvert_to_ss(mrdm, srcdm->getVertArray(srcdm)); @@ -419,13 +419,13 @@ int multiresModifier_reshapeFromDM(Scene *scene, MultiresModifierData *mmd, } /* Returns 1 on success, 0 if the src's totvert doesn't match */ -int multiresModifier_reshape(Scene *scene, MultiresModifierData *mmd, Object *dst, Object *src) +int multiresModifier_reshape(const struct EvaluationContext *eval_ctx, Scene *scene, MultiresModifierData *mmd, Object *dst, Object *src) { - DerivedMesh *srcdm = mesh_get_derived_final(scene, src, CD_MASK_BAREMESH); - return multiresModifier_reshapeFromDM(scene, mmd, dst, srcdm); + DerivedMesh *srcdm = mesh_get_derived_final(eval_ctx, scene, src, CD_MASK_BAREMESH); + return multiresModifier_reshapeFromDM(eval_ctx, scene, mmd, dst, srcdm); } -int multiresModifier_reshapeFromDeformMod(Scene *scene, MultiresModifierData *mmd, +int multiresModifier_reshapeFromDeformMod(const struct EvaluationContext *eval_ctx, Scene *scene, MultiresModifierData *mmd, Object *ob, ModifierData *md) { const ModifierTypeInfo *mti = modifierType_getInfo(md->type); @@ -437,12 +437,12 @@ int multiresModifier_reshapeFromDeformMod(Scene *scene, MultiresModifierData *mm return 0; /* Create DerivedMesh for deformation modifier */ - dm = get_multires_dm(scene, mmd, ob); + dm = get_multires_dm(eval_ctx, scene, mmd, ob); numVerts = dm->getNumVerts(dm); deformedVerts = MEM_mallocN(sizeof(float[3]) * numVerts, "multiresReshape_deformVerts"); dm->getVertCos(dm, deformedVerts); - mti->deformVerts(md, ob, dm, deformedVerts, numVerts, 0); + mti->deformVerts(md, eval_ctx, ob, dm, deformedVerts, numVerts, 0); ndm = CDDM_copy(dm); CDDM_apply_vert_coords(ndm, deformedVerts); @@ -451,7 +451,7 @@ int multiresModifier_reshapeFromDeformMod(Scene *scene, MultiresModifierData *mm dm->release(dm); /* Reshaping */ - result = multiresModifier_reshapeFromDM(scene, mmd, ob, ndm); + result = multiresModifier_reshapeFromDM(eval_ctx, scene, mmd, ob, ndm); /* Cleanup */ ndm->release(ndm); @@ -2174,7 +2174,7 @@ static void multires_sync_levels(Scene *scene, Object *ob_src, Object *ob_dst) } } -static void multires_apply_smat(Scene *scene, Object *ob, float smat[3][3]) +static void multires_apply_smat(const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, float smat[3][3]) { DerivedMesh *dm = NULL, *cddm = NULL, *subdm = NULL; CCGElem **gridData, **subGridData; @@ -2199,10 +2199,10 @@ static void multires_apply_smat(Scene *scene, Object *ob, float smat[3][3]) high_mmd.lvl = high_mmd.totlvl; /* unscaled multires with applied displacement */ - subdm = get_multires_dm(scene, &high_mmd, ob); + subdm = get_multires_dm(eval_ctx, scene, &high_mmd, ob); /* prepare scaled CDDM to create ccgDN */ - cddm = mesh_get_derived_deform(scene, ob, CD_MASK_BAREMESH); + cddm = mesh_get_derived_deform(eval_ctx, scene, ob, CD_MASK_BAREMESH); totvert = cddm->getNumVerts(cddm); vertCos = MEM_mallocN(sizeof(*vertCos) * totvert, "multiresScale vertCos"); @@ -2276,17 +2276,17 @@ int multires_mdisp_corners(MDisps *s) return 0; } -void multiresModifier_scale_disp(Scene *scene, Object *ob) +void multiresModifier_scale_disp(const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob) { float smat[3][3]; /* object's scale matrix */ BKE_object_scale_to_mat3(ob, smat); - multires_apply_smat(scene, ob, smat); + multires_apply_smat(eval_ctx, scene, ob, smat); } -void multiresModifier_prepare_join(Scene *scene, Object *ob, Object *to_ob) +void multiresModifier_prepare_join(const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, Object *to_ob) { float smat[3][3], tmat[3][3], mat[3][3]; multires_sync_levels(scene, to_ob, ob); @@ -2297,7 +2297,7 @@ void multiresModifier_prepare_join(Scene *scene, Object *ob, Object *to_ob) BKE_object_scale_to_mat3(ob, smat); mul_m3_m3m3(mat, smat, tmat); - multires_apply_smat(scene, ob, mat); + multires_apply_smat(eval_ctx, scene, ob, mat); } /* update multires data after topology changing */ diff --git a/source/blender/blenkernel/intern/nla.c b/source/blender/blenkernel/intern/nla.c index 148fc3827e0..478b854c4df 100644 --- a/source/blender/blenkernel/intern/nla.c +++ b/source/blender/blenkernel/intern/nla.c @@ -59,7 +59,7 @@ #include "BKE_library.h" #ifdef WITH_AUDASPACE -# include AUD_SPECIAL_H +# include <AUD_Special.h> #endif #include "RNA_access.h" diff --git a/source/blender/blenkernel/intern/node.c b/source/blender/blenkernel/intern/node.c index 8ccd652edd4..3ff4885f7fc 100644 --- a/source/blender/blenkernel/intern/node.c +++ b/source/blender/blenkernel/intern/node.c @@ -902,80 +902,100 @@ bNode *nodeAddStaticNode(const struct bContext *C, bNodeTree *ntree, int type) return nodeAddNode(C, ntree, idname); } -static void node_socket_copy(bNodeSocket *dst, bNodeSocket *src) +static void node_socket_copy(bNodeSocket *sock_dst, bNodeSocket *sock_src, const int flag) { - src->new_sock = dst; - - if (src->prop) - dst->prop = IDP_CopyProperty(src->prop); - - if (src->default_value) - dst->default_value = MEM_dupallocN(src->default_value); - - dst->stack_index = 0; + sock_src->new_sock = sock_dst; + + if (sock_src->prop) { + sock_dst->prop = IDP_CopyProperty_ex(sock_src->prop, flag); + } + + if (sock_src->default_value) { + sock_dst->default_value = MEM_dupallocN(sock_src->default_value); + } + + sock_dst->stack_index = 0; /* XXX some compositor node (e.g. image, render layers) still store * some persistent buffer data here, need to clear this to avoid dangling pointers. */ - dst->cache = NULL; + sock_dst->cache = NULL; } /* keep socket listorder identical, for copying links */ /* ntree is the target tree */ -bNode *nodeCopyNode(bNodeTree *ntree, bNode *node) +bNode *BKE_node_copy_ex(bNodeTree *ntree, bNode *node_src, const int flag) { - bNode *nnode = MEM_callocN(sizeof(bNode), "dupli node"); - bNodeSocket *sock, *oldsock; - bNodeLink *link, *oldlink; + bNode *node_dst = MEM_callocN(sizeof(bNode), "dupli node"); + bNodeSocket *sock_dst, *sock_src; + bNodeLink *link_dst, *link_src; - *nnode = *node; + *node_dst = *node_src; /* can be called for nodes outside a node tree (e.g. clipboard) */ if (ntree) { - nodeUniqueName(ntree, nnode); + nodeUniqueName(ntree, node_dst); - BLI_addtail(&ntree->nodes, nnode); + BLI_addtail(&ntree->nodes, node_dst); } - BLI_duplicatelist(&nnode->inputs, &node->inputs); - oldsock = node->inputs.first; - for (sock = nnode->inputs.first; sock; sock = sock->next, oldsock = oldsock->next) - node_socket_copy(sock, oldsock); - - BLI_duplicatelist(&nnode->outputs, &node->outputs); - oldsock = node->outputs.first; - for (sock = nnode->outputs.first; sock; sock = sock->next, oldsock = oldsock->next) - node_socket_copy(sock, oldsock); - - if (node->prop) - nnode->prop = IDP_CopyProperty(node->prop); - - BLI_duplicatelist(&nnode->internal_links, &node->internal_links); - oldlink = node->internal_links.first; - for (link = nnode->internal_links.first; link; link = link->next, oldlink = oldlink->next) { - link->fromnode = nnode; - link->tonode = nnode; - link->fromsock = link->fromsock->new_sock; - link->tosock = link->tosock->new_sock; + BLI_duplicatelist(&node_dst->inputs, &node_src->inputs); + for (sock_dst = node_dst->inputs.first, sock_src = node_src->inputs.first; + sock_dst != NULL; + sock_dst = sock_dst->next, sock_src = sock_src->next) + { + node_socket_copy(sock_dst, sock_src, flag); } - - /* don't increase node->id users, freenode doesn't decrement either */ - - if (node->typeinfo->copyfunc) - node->typeinfo->copyfunc(ntree, nnode, node); - - node->new_node = nnode; - nnode->new_node = NULL; - - if (nnode->typeinfo->copyfunc_api) { + + BLI_duplicatelist(&node_dst->outputs, &node_src->outputs); + for (sock_dst = node_dst->outputs.first, sock_src = node_src->outputs.first; + sock_dst != NULL; + sock_dst = sock_dst->next, sock_src = sock_src->next) + { + node_socket_copy(sock_dst, sock_src, flag); + } + + if (node_src->prop) { + node_dst->prop = IDP_CopyProperty_ex(node_src->prop, flag); + } + + BLI_duplicatelist(&node_dst->internal_links, &node_src->internal_links); + for (link_dst = node_dst->internal_links.first, link_src = node_src->internal_links.first; + link_dst != NULL; + link_dst = link_dst->next, link_src = link_src->next) + { + link_dst->fromnode = node_dst; + link_dst->tonode = node_dst; + link_dst->fromsock = link_dst->fromsock->new_sock; + link_dst->tosock = link_dst->tosock->new_sock; + } + + if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) { + id_us_plus(node_dst->id); + } + + if (node_src->typeinfo->copyfunc) { + node_src->typeinfo->copyfunc(ntree, node_dst, node_src); + } + + node_src->new_node = node_dst; + node_dst->new_node = NULL; + + if (node_dst->typeinfo->copyfunc_api) { PointerRNA ptr; - RNA_pointer_create((ID *)ntree, &RNA_Node, nnode, &ptr); - - nnode->typeinfo->copyfunc_api(&ptr, node); + RNA_pointer_create((ID *)ntree, &RNA_Node, node_dst, &ptr); + + node_dst->typeinfo->copyfunc_api(&ptr, node_src); } - - if (ntree) + + if (ntree) { ntree->update |= NTREE_UPDATE_NODES; - - return nnode; + } + + return node_dst; +} + +bNode *nodeCopyNode(bNodeTree *ntree, bNode *node) +{ + return BKE_node_copy_ex(ntree, node, LIB_ID_CREATE_NO_USER_REFCOUNT); } /* also used via rna api, so we check for proper input output direction */ @@ -1172,7 +1192,7 @@ bNodeTree *ntreeAddTree(Main *bmain, const char *name, const char *idname) * node groups and other tree types are created as library data. */ if (bmain) { - ntree = BKE_libblock_alloc(bmain, ID_NT, name); + ntree = BKE_libblock_alloc(bmain, ID_NT, name, 0); } else { ntree = MEM_callocN(sizeof(bNodeTree), "new node tree"); @@ -1191,119 +1211,96 @@ bNodeTree *ntreeAddTree(Main *bmain, const char *name, const char *idname) return ntree; } -/* Warning: this function gets called during some rather unexpected times - * - this gets called when executing compositing updates (for threaded previews) - * - when the nodetree datablock needs to be copied (i.e. when users get copied) - * - for scene duplication use ntreeSwapID() after so we don't have stale pointers. +/** + * Only copy internal data of NodeTree ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! * - * do_make_extern: keep enabled for general use, only reason _not_ to enable is when - * copying for internal use (threads for eg), where you wont want it to modify the - * scene data. + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). */ -static bNodeTree *ntreeCopyTree_internal( - const bNodeTree *ntree, Main *bmain, - bool skip_database, bool do_id_user, bool do_make_extern, bool copy_previews) +void BKE_node_tree_copy_data(Main *UNUSED(bmain), bNodeTree *ntree_dst, const bNodeTree *ntree_src, const int flag) { - bNodeTree *newtree; - bNode *node /*, *nnode */ /* UNUSED */, *last; - bNodeSocket *sock, *oldsock; - bNodeLink *link; - - if (ntree == NULL) return NULL; - - /* is ntree part of library? */ - if (bmain && !skip_database && BLI_findindex(&bmain->nodetree, ntree) >= 0) { - newtree = BKE_libblock_copy(bmain, &ntree->id); - } - else { - newtree = BKE_libblock_copy_nolib(&ntree->id, true); - } + bNodeSocket *sock_dst, *sock_src; + bNodeLink *link_dst; - id_us_plus((ID *)newtree->gpd); + /* We never handle usercount here for own data. */ + const int flag_subdata = flag | LIB_ID_CREATE_NO_USER_REFCOUNT; /* in case a running nodetree is copied */ - newtree->execdata = NULL; + ntree_dst->execdata = NULL; - newtree->duplilock = NULL; - - BLI_listbase_clear(&newtree->nodes); - BLI_listbase_clear(&newtree->links); - - last = ntree->nodes.last; - for (node = ntree->nodes.first; node; node = node->next) { - - /* ntreeUserDecrefID inline */ - if (do_id_user) { - id_us_plus(node->id); - } + ntree_dst->duplilock = NULL; - if (do_make_extern) { - id_lib_extern(node->id); - } + BLI_listbase_clear(&ntree_dst->nodes); + BLI_listbase_clear(&ntree_dst->links); - node->new_node = NULL; - /* nnode = */ nodeCopyNode(newtree, node); /* sets node->new */ - - /* make sure we don't copy new nodes again! */ - if (node == last) - break; + for (bNode *node_src = ntree_src->nodes.first; node_src; node_src = node_src->next) { + BKE_node_copy_ex(ntree_dst, node_src, flag_subdata); } - + /* copy links */ - BLI_duplicatelist(&newtree->links, &ntree->links); - for (link = newtree->links.first; link; link = link->next) { - link->fromnode = (link->fromnode ? link->fromnode->new_node : NULL); - link->fromsock = (link->fromsock ? link->fromsock->new_sock : NULL); - link->tonode = (link->tonode ? link->tonode->new_node : NULL); - link->tosock = (link->tosock ? link->tosock->new_sock : NULL); + BLI_duplicatelist(&ntree_dst->links, &ntree_src->links); + for (link_dst = ntree_dst->links.first; link_dst; link_dst = link_dst->next) { + link_dst->fromnode = (link_dst->fromnode ? link_dst->fromnode->new_node : NULL); + link_dst->fromsock = (link_dst->fromsock ? link_dst->fromsock->new_sock : NULL); + link_dst->tonode = (link_dst->tonode ? link_dst->tonode->new_node : NULL); + link_dst->tosock = (link_dst->tosock ? link_dst->tosock->new_sock : NULL); /* update the link socket's pointer */ - if (link->tosock) - link->tosock->link = link; + if (link_dst->tosock) { + link_dst->tosock->link = link_dst; + } } - + /* copy interface sockets */ - BLI_duplicatelist(&newtree->inputs, &ntree->inputs); - oldsock = ntree->inputs.first; - for (sock = newtree->inputs.first; sock; sock = sock->next, oldsock = oldsock->next) - node_socket_copy(sock, oldsock); - - BLI_duplicatelist(&newtree->outputs, &ntree->outputs); - oldsock = ntree->outputs.first; - for (sock = newtree->outputs.first; sock; sock = sock->next, oldsock = oldsock->next) - node_socket_copy(sock, oldsock); - + BLI_duplicatelist(&ntree_dst->inputs, &ntree_src->inputs); + for (sock_dst = ntree_dst->inputs.first, sock_src = ntree_src->inputs.first; + sock_dst != NULL; + sock_dst = sock_dst->next, sock_src = sock_src->next) + { + node_socket_copy(sock_dst, sock_src, flag_subdata); + } + + BLI_duplicatelist(&ntree_dst->outputs, &ntree_src->outputs); + for (sock_dst = ntree_dst->outputs.first, sock_src = ntree_src->outputs.first; + sock_dst != NULL; + sock_dst = sock_dst->next, sock_src = sock_src->next) + { + node_socket_copy(sock_dst, sock_src, flag_subdata); + } + /* copy preview hash */ - if (ntree->previews && copy_previews) { + if (ntree_src->previews && (flag & LIB_ID_COPY_NO_PREVIEW) == 0) { bNodeInstanceHashIterator iter; - - newtree->previews = BKE_node_instance_hash_new("node previews"); - - NODE_INSTANCE_HASH_ITER(iter, ntree->previews) { + + ntree_dst->previews = BKE_node_instance_hash_new("node previews"); + + NODE_INSTANCE_HASH_ITER(iter, ntree_src->previews) { bNodeInstanceKey key = BKE_node_instance_hash_iterator_get_key(&iter); bNodePreview *preview = BKE_node_instance_hash_iterator_get_value(&iter); - BKE_node_instance_hash_insert(newtree->previews, key, BKE_node_preview_copy(preview)); + BKE_node_instance_hash_insert(ntree_dst->previews, key, BKE_node_preview_copy(preview)); } } - else - newtree->previews = NULL; - + else { + ntree_dst->previews = NULL; + } + /* update node->parent pointers */ - for (node = newtree->nodes.first; node; node = node->next) { - if (node->parent) - node->parent = node->parent->new_node; + for (bNode *node_dst = ntree_dst->nodes.first, *node_src = ntree_src->nodes.first; node_dst; node_dst = node_dst->next, node_src = node_src->next) { + if (node_dst->parent) { + node_dst->parent = node_dst->parent->new_node; + } } - - /* node tree will generate its own interface type */ - newtree->interface_type = NULL; - - BKE_id_copy_ensure_local(bmain, &ntree->id, &newtree->id); - return newtree; + /* node tree will generate its own interface type */ + ntree_dst->interface_type = NULL; } bNodeTree *ntreeCopyTree_ex(const bNodeTree *ntree, Main *bmain, const bool do_id_user) { - return ntreeCopyTree_internal(ntree, bmain, false, do_id_user, true, true); + bNodeTree *ntree_copy; + BKE_id_copy_ex(bmain, (ID *)ntree, (ID **)&ntree_copy, do_id_user ? 0 : LIB_ID_CREATE_NO_USER_REFCOUNT, false); + return ntree_copy; } bNodeTree *ntreeCopyTree(Main *bmain, const bNodeTree *ntree) { @@ -1700,11 +1697,12 @@ static void node_free_node_ex(bNodeTree *ntree, bNode *node, bool remove_animdat ntreeTexEndExecTree(ntree->execdata); ntree->execdata = NULL; } - - if (node->typeinfo->freefunc) - node->typeinfo->freefunc(node); } - + + if (node->typeinfo->freefunc) { + node->typeinfo->freefunc(node); + } + for (sock = node->inputs.first; sock; sock = nextsock) { nextsock = sock->next; node_socket_free(ntree, sock, node); @@ -1993,10 +1991,11 @@ bNodeTree *ntreeLocalize(bNodeTree *ntree) adt->tmpact = NULL; } - /* Make full copy. + /* Make full copy outside of Main database. * Note: previews are not copied here. */ - ltree = ntreeCopyTree_internal(ntree, G.main, true, false, false, false); + BKE_id_copy_ex(G.main, (ID *)ntree, (ID **)<ree, + LIB_ID_CREATE_NO_MAIN | LIB_ID_CREATE_NO_USER_REFCOUNT | LIB_ID_COPY_NO_PREVIEW, false); ltree->flag |= NTREE_IS_LOCALIZED; for (node = ltree->nodes.first; node; node = node->next) { @@ -2024,7 +2023,7 @@ bNodeTree *ntreeLocalize(bNodeTree *ntree) for (node = ntree->nodes.first; node; node = node->next) { /* store new_node pointer to original */ - node->new_node->new_node = node; + node->new_node->original = node; } if (ntree->typeinfo->localize) @@ -2236,7 +2235,7 @@ static void ntree_interface_type_create(bNodeTree *ntree) /* register a subtype of PropertyGroup */ srna = RNA_def_struct_ptr(&BLENDER_RNA, identifier, &RNA_PropertyGroup); RNA_def_struct_ui_text(srna, name, description); - RNA_def_struct_duplicate_pointers(srna); + RNA_def_struct_duplicate_pointers(&BLENDER_RNA, srna); /* associate the RNA type with the node tree */ ntree->interface_type = srna; @@ -2275,10 +2274,10 @@ StructRNA *ntreeInterfaceTypeGet(bNodeTree *ntree, int create) ntree_interface_identifier(ntree, base, identifier, sizeof(identifier), name, description); /* rename the RNA type */ - RNA_def_struct_free_pointers(srna); - RNA_def_struct_identifier(srna, identifier); + RNA_def_struct_free_pointers(&BLENDER_RNA, srna); + RNA_def_struct_identifier(&BLENDER_RNA, srna, identifier); RNA_def_struct_ui_text(srna, name, description); - RNA_def_struct_duplicate_pointers(srna); + RNA_def_struct_duplicate_pointers(&BLENDER_RNA, srna); } } else if (create) { @@ -3174,12 +3173,20 @@ void nodeSynchronizeID(bNode *node, bool copy_to_id) void nodeLabel(bNodeTree *ntree, bNode *node, char *label, int maxlen) { - if (node->label[0] != '\0') + if (node->label[0] != '\0') { BLI_strncpy(label, node->label, maxlen); - else if (node->typeinfo->labelfunc) + } + else if (node->typeinfo->labelfunc) { node->typeinfo->labelfunc(ntree, node, label, maxlen); - else - BLI_strncpy(label, IFACE_(node->typeinfo->ui_name), maxlen); + } + else { + /* Kind of hacky and weak... Ideally would be better to use RNA here. :| */ + const char *tmp = CTX_IFACE_(BLT_I18NCONTEXT_ID_NODETREE, node->typeinfo->ui_name); + if (tmp == node->typeinfo->ui_name) { + tmp = IFACE_(node->typeinfo->ui_name); + } + BLI_strncpy(label, tmp, maxlen); + } } static void node_type_base_defaults(bNodeType *ntype) @@ -3587,12 +3594,10 @@ static void registerShaderNodes(void) register_node_type_sh_add_shader(); register_node_type_sh_uvmap(); register_node_type_sh_uvalongstroke(); - register_node_type_sh_eevee_metallic(); register_node_type_sh_eevee_specular(); register_node_type_sh_output_lamp(); register_node_type_sh_output_material(); - register_node_type_sh_output_eevee_material(); register_node_type_sh_output_world(); register_node_type_sh_output_linestyle(); @@ -3799,3 +3804,46 @@ void BKE_nodetree_remove_layer_n(bNodeTree *ntree, Scene *scene, const int layer } } } + +static void node_copy_default_values_list(ListBase *sockets_dst, + const ListBase *sockets_src) +{ + bNodeSocket *sock_dst = sockets_dst->first; + const bNodeSocket *sock_src = sockets_src->first; + while (sock_dst != NULL) { + node_socket_copy_default_value(sock_dst, sock_src); + sock_dst = sock_dst->next; + sock_src = sock_src->next; + } +} + +static void node_copy_default_values(bNode *node_dst, const bNode *node_src) +{ + node_copy_default_values_list(&node_dst->inputs, &node_src->inputs); + node_copy_default_values_list(&node_dst->outputs, &node_src->outputs); +} + +void BKE_nodetree_copy_default_values(bNodeTree *ntree_dst, + const bNodeTree *ntree_src) +{ + if (ntree_dst == ntree_src) { + return; + } + bNode *node_dst = ntree_dst->nodes.first; + const bNode *node_src = ntree_src->nodes.first; + while (node_dst != NULL) { + node_copy_default_values(node_dst, node_src); + node_dst = node_dst->next; + node_src = node_src->next; + } +} + +void BKE_nodetree_shading_params_eval(const struct EvaluationContext *UNUSED(eval_ctx), + bNodeTree *ntree_dst, + const bNodeTree *ntree_src) +{ + if (G.debug & G_DEBUG_DEPSGRAPH) { + printf("%s on %s (%p)\n", __func__, ntree_src->id.name, ntree_dst); + } + BKE_nodetree_copy_default_values(ntree_dst, ntree_src); +} diff --git a/source/blender/blenkernel/intern/object.c b/source/blender/blenkernel/intern/object.c index 68e5ddf8583..a47e88a8e48 100644 --- a/source/blender/blenkernel/intern/object.c +++ b/source/blender/blenkernel/intern/object.c @@ -319,7 +319,7 @@ void BKE_object_link_modifiers(struct Object *ob_dst, const struct Object *ob_sr modifier_unique_name(&ob_dst->modifiers, nmd); } - BKE_object_copy_particlesystems(ob_dst, ob_src); + BKE_object_copy_particlesystems(ob_dst, ob_src, 0); /* TODO: smoke?, cloth? */ } @@ -337,14 +337,14 @@ void BKE_object_free_derived_caches(Object *ob) Mesh *me = ob->data; if (me && me->bb) { - atomic_fetch_and_or_uint32((uint*)&me->bb->flag, BOUNDBOX_DIRTY); + atomic_fetch_and_or_uint32((uint *)&me->bb->flag, BOUNDBOX_DIRTY); } } else if (ELEM(ob->type, OB_SURF, OB_CURVE, OB_FONT)) { Curve *cu = ob->data; if (cu && cu->bb) { - atomic_fetch_and_or_uint32((uint*)&cu->bb->flag, BOUNDBOX_DIRTY); + atomic_fetch_and_or_uint32((uint *)&cu->bb->flag, BOUNDBOX_DIRTY); } } @@ -365,6 +365,8 @@ void BKE_object_free_derived_caches(Object *ob) } if (ob->mesh_evaluated != NULL) { + /* Restore initial pointer. */ + ob->data = ob->mesh_evaluated->id.newid; /* Evaluated mesh points to edit mesh, but does not own it. */ ob->mesh_evaluated->edit_btmesh = NULL; BKE_mesh_free(ob->mesh_evaluated); @@ -560,6 +562,15 @@ bool BKE_object_is_in_wpaint_select_vert(Object *ob) return false; } +/** + * Return if the object is visible, as evaluated by depsgraph + * Keep in sync with rna_object.c (object.is_visible). + */ +bool BKE_object_is_visible(Object *ob) +{ + return (ob->base_flag & BASE_VISIBLED) != 0; +} + bool BKE_object_exists_check(Object *obtest) { Object *ob; @@ -703,7 +714,7 @@ Object *BKE_object_add_only_object(Main *bmain, int type, const char *name) if (!name) name = get_obdata_defname(type); - ob = BKE_libblock_alloc(bmain, ID_OB, name); + ob = BKE_libblock_alloc(bmain, ID_OB, name, 0); /* default object vars */ ob->type = type; @@ -832,7 +843,7 @@ static LodLevel *lod_level_select(Object *ob, const float camera_position[3]) bool BKE_object_lod_is_usable(Object *ob, SceneLayer *sl) { - bool active = (sl) ? ob == OBACT_NEW : false; + bool active = (sl) ? ob == OBACT_NEW(sl) : false; return (ob->mode == OB_MODE_OBJECT || !active); } @@ -873,7 +884,7 @@ struct Object *BKE_object_lod_matob_get(Object *ob, SceneLayer *sl) #endif /* WITH_GAMEENGINE */ -SoftBody *copy_softbody(const SoftBody *sb, bool copy_caches) +SoftBody *copy_softbody(const SoftBody *sb, const int flag) { SoftBody *sbn; @@ -881,7 +892,7 @@ SoftBody *copy_softbody(const SoftBody *sb, bool copy_caches) sbn = MEM_dupallocN(sb); - if (copy_caches == false) { + if ((flag & LIB_ID_COPY_CACHES) == 0) { sbn->totspring = sbn->totpoint = 0; sbn->bpoint = NULL; sbn->bspring = NULL; @@ -910,7 +921,7 @@ SoftBody *copy_softbody(const SoftBody *sb, bool copy_caches) sbn->scratch = NULL; - sbn->pointcache = BKE_ptcache_copy_list(&sbn->ptcaches, &sb->ptcaches, copy_caches); + sbn->pointcache = BKE_ptcache_copy_list(&sbn->ptcaches, &sb->ptcaches, flag); if (sb->effector_weights) sbn->effector_weights = MEM_dupallocN(sb->effector_weights); @@ -918,7 +929,7 @@ SoftBody *copy_softbody(const SoftBody *sb, bool copy_caches) return sbn; } -BulletSoftBody *copy_bulletsoftbody(const BulletSoftBody *bsb) +BulletSoftBody *copy_bulletsoftbody(const BulletSoftBody *bsb, const int UNUSED(flag)) { BulletSoftBody *bsbn; @@ -929,7 +940,7 @@ BulletSoftBody *copy_bulletsoftbody(const BulletSoftBody *bsb) return bsbn; } -ParticleSystem *BKE_object_copy_particlesystem(ParticleSystem *psys) +ParticleSystem *BKE_object_copy_particlesystem(ParticleSystem *psys, const int flag) { ParticleSystem *psysn; ParticleData *pa; @@ -966,7 +977,7 @@ ParticleSystem *BKE_object_copy_particlesystem(ParticleSystem *psys) if (psys->clmd) { psysn->clmd = (ClothModifierData *)modifier_new(eModifierType_Cloth); - modifier_copyData((ModifierData *)psys->clmd, (ModifierData *)psysn->clmd); + modifier_copyData_ex((ModifierData *)psys->clmd, (ModifierData *)psysn->clmd, flag); psys->hair_in_dm = psys->hair_out_dm = NULL; } @@ -985,7 +996,8 @@ ParticleSystem *BKE_object_copy_particlesystem(ParticleSystem *psys) BLI_listbase_clear(&psysn->childcachebufs); psysn->renderdata = NULL; - psysn->pointcache = BKE_ptcache_copy_list(&psysn->ptcaches, &psys->ptcaches, false); + /* XXX Never copy caches here? */ + psysn->pointcache = BKE_ptcache_copy_list(&psysn->ptcaches, &psys->ptcaches, flag & ~LIB_ID_COPY_CACHES); /* XXX - from reading existing code this seems correct but intended usage of * pointcache should /w cloth should be added in 'ParticleSystem' - campbell */ @@ -993,12 +1005,14 @@ ParticleSystem *BKE_object_copy_particlesystem(ParticleSystem *psys) psysn->clmd->point_cache = psysn->pointcache; } - id_us_plus((ID *)psysn->part); + if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) { + id_us_plus((ID *)psysn->part); + } return psysn; } -void BKE_object_copy_particlesystems(Object *ob_dst, const Object *ob_src) +void BKE_object_copy_particlesystems(Object *ob_dst, const Object *ob_src, const int flag) { ParticleSystem *psys, *npsys; ModifierData *md; @@ -1010,7 +1024,7 @@ void BKE_object_copy_particlesystems(Object *ob_dst, const Object *ob_src) BLI_listbase_clear(&ob_dst->particlesystem); for (psys = ob_src->particlesystem.first; psys; psys = psys->next) { - npsys = BKE_object_copy_particlesystem(psys); + npsys = BKE_object_copy_particlesystem(psys, flag); BLI_addtail(&ob_dst->particlesystem, npsys); @@ -1047,23 +1061,25 @@ void BKE_object_copy_softbody(Object *ob_dst, const Object *ob_src) { if (ob_src->soft) { ob_dst->softflag = ob_src->softflag; - ob_dst->soft = copy_softbody(ob_src->soft, false); + ob_dst->soft = copy_softbody(ob_src->soft, 0); } } -static void copy_object_pose(Object *obn, const Object *ob) +static void copy_object_pose(Object *obn, const Object *ob, const int flag) { bPoseChannel *chan; /* note: need to clear obn->pose pointer first, so that BKE_pose_copy_data works (otherwise there's a crash) */ obn->pose = NULL; - BKE_pose_copy_data(&obn->pose, ob->pose, 1); /* 1 = copy constraints */ + BKE_pose_copy_data_ex(&obn->pose, ob->pose, flag, true); /* true = copy constraints */ for (chan = obn->pose->chanbase.first; chan; chan = chan->next) { bConstraint *con; chan->flag &= ~(POSE_LOC | POSE_ROT | POSE_SIZE); + /* XXX Remapping object pointing onto itself should be handled by generic BKE_library_remap stuff, but... + * the flush_constraint_targets callback am not sure about, so will delay that for now. */ for (con = chan->constraints.first; con; con = con->next) { const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(con); ListBase targets = {NULL, NULL}; @@ -1084,13 +1100,10 @@ static void copy_object_pose(Object *obn, const Object *ob) } } -static void copy_object_lod(Object *obn, const Object *ob) +static void copy_object_lod(Object *obn, const Object *ob, const int UNUSED(flag)) { BLI_duplicatelist(&obn->lodlevels, &ob->lodlevels); - if (obn->lodlevels.first) - ((LodLevel *)obn->lodlevels.first)->source = obn; - obn->currentlod = (LodLevel *)obn->lodlevels.first; } @@ -1135,100 +1148,101 @@ void BKE_object_transform_copy(Object *ob_tar, const Object *ob_src) copy_v3_v3(ob_tar->size, ob_src->size); } -Object *BKE_object_copy_ex(Main *bmain, const Object *ob, bool copy_caches) +/** + * Only copy internal data of Object ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_object_copy_data(Main *UNUSED(bmain), Object *ob_dst, const Object *ob_src, const int flag) { - Object *obn; ModifierData *md; - int a; - obn = BKE_libblock_copy(bmain, &ob->id); - - if (ob->totcol) { - obn->mat = MEM_dupallocN(ob->mat); - obn->matbits = MEM_dupallocN(ob->matbits); - obn->totcol = ob->totcol; + /* We never handle usercount here for own data. */ + const int flag_subdata = flag | LIB_ID_CREATE_NO_USER_REFCOUNT; + + if (ob_src->totcol) { + ob_dst->mat = MEM_dupallocN(ob_src->mat); + ob_dst->matbits = MEM_dupallocN(ob_src->matbits); + ob_dst->totcol = ob_src->totcol; } - if (ob->iuser) obn->iuser = MEM_dupallocN(ob->iuser); + if (ob_src->iuser) ob_dst->iuser = MEM_dupallocN(ob_src->iuser); - if (ob->bb) obn->bb = MEM_dupallocN(ob->bb); - obn->flag &= ~OB_FROMGROUP; + if (ob_src->bb) ob_dst->bb = MEM_dupallocN(ob_src->bb); + ob_dst->flag &= ~OB_FROMGROUP; - BLI_listbase_clear(&obn->modifiers); + BLI_listbase_clear(&ob_dst->modifiers); - for (md = ob->modifiers.first; md; md = md->next) { + for (md = ob_src->modifiers.first; md; md = md->next) { ModifierData *nmd = modifier_new(md->type); BLI_strncpy(nmd->name, md->name, sizeof(nmd->name)); - modifier_copyData(md, nmd); - BLI_addtail(&obn->modifiers, nmd); + modifier_copyData_ex(md, nmd, flag_subdata); + BLI_addtail(&ob_dst->modifiers, nmd); } - BLI_listbase_clear(&obn->prop); - BKE_bproperty_copy_list(&obn->prop, &ob->prop); + BLI_listbase_clear(&ob_dst->prop); + BKE_bproperty_copy_list(&ob_dst->prop, &ob_src->prop); - BKE_sca_logic_copy(obn, ob); + BKE_sca_logic_copy(ob_dst, ob_src, flag_subdata); - if (ob->pose) { - copy_object_pose(obn, ob); + if (ob_src->pose) { + copy_object_pose(ob_dst, ob_src, flag_subdata); /* backwards compat... non-armatures can get poses in older files? */ - if (ob->type == OB_ARMATURE) - BKE_pose_rebuild(obn, obn->data); + if (ob_src->type == OB_ARMATURE) + BKE_pose_rebuild(ob_dst, ob_dst->data); } - defgroup_copy_list(&obn->defbase, &ob->defbase); - BKE_object_facemap_copy_list(&obn->fmaps, &ob->fmaps); - BKE_constraints_copy(&obn->constraints, &ob->constraints, true); + defgroup_copy_list(&ob_dst->defbase, &ob_src->defbase); + BKE_object_facemap_copy_list(&ob_dst->fmaps, &ob_src->fmaps); + BKE_constraints_copy_ex(&ob_dst->constraints, &ob_src->constraints, flag_subdata, true); - obn->mode = OB_MODE_OBJECT; - obn->sculpt = NULL; + ob_dst->mode = OB_MODE_OBJECT; + ob_dst->sculpt = NULL; - /* increase user numbers */ - id_us_plus((ID *)obn->data); - id_us_plus((ID *)obn->poselib); - id_us_plus((ID *)obn->gpd); - id_us_plus((ID *)obn->dup_group); + if (ob_src->pd) { + ob_dst->pd = MEM_dupallocN(ob_src->pd); + if (ob_dst->pd->rng) { + ob_dst->pd->rng = MEM_dupallocN(ob_src->pd->rng); + } + } + ob_dst->soft = copy_softbody(ob_src->soft, flag_subdata); + ob_dst->bsoft = copy_bulletsoftbody(ob_src->bsoft, flag_subdata); + ob_dst->rigidbody_object = BKE_rigidbody_copy_object(ob_src, flag_subdata); + ob_dst->rigidbody_constraint = BKE_rigidbody_copy_constraint(ob_src, flag_subdata); - for (a = 0; a < obn->totcol; a++) id_us_plus((ID *)obn->mat[a]); + BKE_object_copy_particlesystems(ob_dst, ob_src, flag_subdata); - if (ob->pd) { - obn->pd = MEM_dupallocN(ob->pd); - if (obn->pd->tex) - id_us_plus(&(obn->pd->tex->id)); - if (obn->pd->rng) - obn->pd->rng = MEM_dupallocN(ob->pd->rng); - } - obn->soft = copy_softbody(ob->soft, copy_caches); - obn->bsoft = copy_bulletsoftbody(ob->bsoft); - obn->rigidbody_object = BKE_rigidbody_copy_object(ob); - obn->rigidbody_constraint = BKE_rigidbody_copy_constraint(ob); - - BKE_object_copy_particlesystems(obn, ob); - - obn->derivedDeform = NULL; - obn->derivedFinal = NULL; + ob_dst->derivedDeform = NULL; + ob_dst->derivedFinal = NULL; - BLI_listbase_clear(&obn->gpulamp); - BLI_listbase_clear(&obn->pc_ids); - BLI_listbase_clear(&obn->drawdata); + BLI_listbase_clear(&ob_dst->gpulamp); + BLI_listbase_clear(&ob_dst->drawdata); + BLI_listbase_clear(&ob_dst->pc_ids); - obn->mpath = NULL; + ob_dst->mpath = NULL; - copy_object_lod(obn, ob); + copy_object_lod(ob_dst, ob_src, flag_subdata); - /* Copy runtime surve data. */ - obn->curve_cache = NULL; - - BKE_id_copy_ensure_local(bmain, &ob->id, &obn->id); + /* Do not copy runtime curve data. */ + ob_dst->curve_cache = NULL; /* Do not copy object's preview (mostly due to the fact renderers create temp copy of objects). */ - obn->preview = NULL; - - return obn; + if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0 && false) { /* XXX TODO temp hack */ + BKE_previewimg_id_copy(&ob_dst->id, &ob_src->id); + } + else { + ob_dst->preview = NULL; + } } /* copy objects, will re-initialize cached simulation data */ Object *BKE_object_copy(Main *bmain, const Object *ob) { - return BKE_object_copy_ex(bmain, ob, false); + Object *ob_copy; + BKE_id_copy_ex(bmain, &ob->id, (ID **)&ob_copy, 0, false); + return ob_copy; } void BKE_object_make_local_ex(Main *bmain, Object *ob, const bool lib_local, const bool clear_proxy) @@ -1304,10 +1318,10 @@ static void armature_set_id_extern(Object *ob) unsigned int lay = arm->layer_protected; for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) { - if (!(pchan->bone->layer & lay)) { + if (!(pchan->bone->layer & lay)) id_lib_extern((ID *)pchan->custom); - } } + } void BKE_object_copy_proxy_drivers(Object *ob, Object *target) @@ -1426,7 +1440,7 @@ void BKE_object_make_proxy(Object *ob, Object *target, Object *gob) /* type conversions */ if (target->type == OB_ARMATURE) { - copy_object_pose(ob, target); /* data copy, object pointers in constraints */ + copy_object_pose(ob, target, 0); /* data copy, object pointers in constraints */ BKE_pose_rest(ob->pose); /* clear all transforms in channels */ BKE_pose_rebuild(ob, ob->data); /* set all internal links */ @@ -1709,13 +1723,16 @@ static bool ob_parcurve(Scene *scene, Object *ob, Object *par, float mat[4][4]) Curve *cu = par->data; float vec[4], dir[3], quat[4], radius, ctime; + /* TODO: Make sure this doesn't crash. */ +#if 0 /* only happens on reload file, but violates depsgraph still... fix! */ if (par->curve_cache == NULL) { if (scene == NULL) { return false; } - BKE_displist_make_curveTypes(scene, par, 0); + BKE_displist_make_curveTypes(eval_ctx, scene, par, 0); } +#endif if (par->curve_cache->path == NULL) { return false; @@ -2096,8 +2113,9 @@ static bool where_is_object_parslow(Object *ob, float obmat[4][4], float slowmat } /* note, scene is the active scene while actual_scene is the scene the object resides in */ -void BKE_object_where_is_calc_time_ex(Scene *scene, Object *ob, float ctime, - RigidBodyWorld *rbw, float r_originmat[3][3]) +void BKE_object_where_is_calc_time_ex( + const EvaluationContext *eval_ctx, Scene *scene, Object *ob, float ctime, + RigidBodyWorld *rbw, float r_originmat[3][3]) { if (ob == NULL) return; @@ -2132,7 +2150,7 @@ void BKE_object_where_is_calc_time_ex(Scene *scene, Object *ob, float ctime, if (ob->constraints.first && !(ob->transflag & OB_NO_CONSTRAINTS)) { bConstraintOb *cob; cob = BKE_constraints_make_evalob(scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT); - BKE_constraints_solve(&ob->constraints, cob, ctime); + BKE_constraints_solve(eval_ctx, &ob->constraints, cob, ctime); BKE_constraints_clear_evalob(cob); } @@ -2141,9 +2159,9 @@ void BKE_object_where_is_calc_time_ex(Scene *scene, Object *ob, float ctime, else ob->transflag &= ~OB_NEG_SCALE; } -void BKE_object_where_is_calc_time(Scene *scene, Object *ob, float ctime) +void BKE_object_where_is_calc_time(const EvaluationContext *eval_ctx, Scene *scene, Object *ob, float ctime) { - BKE_object_where_is_calc_time_ex(scene, ob, ctime, NULL, NULL); + BKE_object_where_is_calc_time_ex(eval_ctx, scene, ob, ctime, NULL, NULL); } /* get object transformation matrix without recalculating dependencies and @@ -2168,17 +2186,17 @@ void BKE_object_where_is_calc_mat4(Scene *scene, Object *ob, float obmat[4][4]) } } -void BKE_object_where_is_calc_ex(Scene *scene, RigidBodyWorld *rbw, Object *ob, float r_originmat[3][3]) +void BKE_object_where_is_calc_ex(const EvaluationContext *eval_ctx, Scene *scene, RigidBodyWorld *rbw, Object *ob, float r_originmat[3][3]) { - BKE_object_where_is_calc_time_ex(scene, ob, BKE_scene_frame_get(scene), rbw, r_originmat); + BKE_object_where_is_calc_time_ex(eval_ctx, scene, ob, BKE_scene_frame_get(scene), rbw, r_originmat); } -void BKE_object_where_is_calc(Scene *scene, Object *ob) +void BKE_object_where_is_calc(const EvaluationContext *eval_ctx, Scene *scene, Object *ob) { - BKE_object_where_is_calc_time_ex(scene, ob, BKE_scene_frame_get(scene), NULL, NULL); + BKE_object_where_is_calc_time_ex(eval_ctx, scene, ob, BKE_scene_frame_get(scene), NULL, NULL); } /* for calculation of the inverse parent transform, only used for editor */ -void BKE_object_workob_calc_parent(Scene *scene, Object *ob, Object *workob) +void BKE_object_workob_calc_parent(const EvaluationContext *eval_ctx, Scene *scene, Object *ob, Object *workob) { BKE_object_workob_clear(workob); @@ -2200,7 +2218,7 @@ void BKE_object_workob_calc_parent(Scene *scene, Object *ob, Object *workob) BLI_strncpy(workob->parsubstr, ob->parsubstr, sizeof(workob->parsubstr)); - BKE_object_where_is_calc(scene, workob); + BKE_object_where_is_calc(eval_ctx, scene, workob); } /* see BKE_pchan_apply_mat4() for the equivalent 'pchan' function */ @@ -2531,7 +2549,7 @@ void BKE_scene_foreach_display_point( Base *base; Object *ob; - for (base = FIRSTBASE_NEW; base; base = base->next) { + for (base = FIRSTBASE_NEW(sl); base; base = base->next) { if (((base->flag & BASE_VISIBLED) != 0) && ((base->flag & BASE_SELECTED) != 0)) { ob = base->object; @@ -2630,7 +2648,7 @@ bool BKE_object_parent_loop_check(const Object *par, const Object *ob) /* the main object update call, for object matrix, constraints, keys and displist (modifiers) */ /* requires flags to be set! */ /* Ideally we shouldn't have to pass the rigid body world, but need bigger restructuring to avoid id */ -void BKE_object_handle_update_ex(EvaluationContext *eval_ctx, +void BKE_object_handle_update_ex(const EvaluationContext *eval_ctx, Scene *scene, Object *ob, RigidBodyWorld *rbw, const bool do_proxy_update) @@ -2679,7 +2697,7 @@ void BKE_object_handle_update_ex(EvaluationContext *eval_ctx, copy_m4_m4(ob->obmat, ob->proxy_from->obmat); } else - BKE_object_where_is_calc_ex(scene, rbw, ob, NULL); + BKE_object_where_is_calc_ex(eval_ctx, scene, rbw, ob, NULL); } if (ob->recalc & OB_RECALC_DATA) { @@ -2709,7 +2727,7 @@ void BKE_object_handle_update_ex(EvaluationContext *eval_ctx, * e.g. "scene" <-- set 1 <-- set 2 ("ob" lives here) <-- set 3 <-- ... <-- set n * rigid bodies depend on their world so use BKE_object_handle_update_ex() to also pass along the corrent rigid body world */ -void BKE_object_handle_update(EvaluationContext *eval_ctx, Scene *scene, Object *ob) +void BKE_object_handle_update(const EvaluationContext *eval_ctx, Scene *scene, Object *ob) { BKE_object_handle_update_ex(eval_ctx, scene, ob, NULL, true); } @@ -2718,7 +2736,7 @@ void BKE_object_sculpt_modifiers_changed(Object *ob) { SculptSession *ss = ob->sculpt; - if (ss) { + if (ss && ss->building_vp_handle == false) { if (!ss->cache) { /* we free pbvh on changes, except during sculpt since it can't deal with * changing PVBH node organization, we hope topology does not change in @@ -2729,6 +2747,9 @@ void BKE_object_sculpt_modifiers_changed(Object *ob) } BKE_sculptsession_free_deformMats(ob->sculpt); + + /* In vertex/weight paint, force maps to be rebuilt. */ + BKE_sculptsession_free_vwpaint_data(ob->sculpt); } else { PBVHNode **nodes; @@ -3625,9 +3646,9 @@ static void object_cacheIgnoreClear(Object *ob, int state) /* Note: this function should eventually be replaced by depsgraph functionality. * Avoid calling this in new code unless there is a very good reason for it! */ -bool BKE_object_modifier_update_subframe(Scene *scene, Object *ob, bool update_mesh, - int parent_recursion, float frame, - int type) +bool BKE_object_modifier_update_subframe( + const EvaluationContext *eval_ctx, Scene *scene, Object *ob, bool update_mesh, + int parent_recursion, float frame, int type) { ModifierData *md = modifiers_findByType(ob, (ModifierType)type); bConstraint *con; @@ -3650,8 +3671,8 @@ bool BKE_object_modifier_update_subframe(Scene *scene, Object *ob, bool update_m if (parent_recursion) { int recursion = parent_recursion - 1; bool no_update = false; - if (ob->parent) no_update |= BKE_object_modifier_update_subframe(scene, ob->parent, 0, recursion, frame, type); - if (ob->track) no_update |= BKE_object_modifier_update_subframe(scene, ob->track, 0, recursion, frame, type); + if (ob->parent) no_update |= BKE_object_modifier_update_subframe(eval_ctx, scene, ob->parent, 0, recursion, frame, type); + if (ob->track) no_update |= BKE_object_modifier_update_subframe(eval_ctx, scene, ob->track, 0, recursion, frame, type); /* skip subframe if object is parented * to vertex of a dynamic paint canvas */ @@ -3668,7 +3689,7 @@ bool BKE_object_modifier_update_subframe(Scene *scene, Object *ob, bool update_m cti->get_constraint_targets(con, &targets); for (ct = targets.first; ct; ct = ct->next) { if (ct->tar) - BKE_object_modifier_update_subframe(scene, ct->tar, 0, recursion, frame, type); + BKE_object_modifier_update_subframe(eval_ctx, scene, ct->tar, 0, recursion, frame, type); } /* free temp targets */ if (cti->flush_constraint_targets) @@ -3688,7 +3709,7 @@ bool BKE_object_modifier_update_subframe(Scene *scene, Object *ob, bool update_m object_cacheIgnoreClear(ob, 0); } else - BKE_object_where_is_calc_time(scene, ob, frame); + BKE_object_where_is_calc_time(eval_ctx, scene, ob, frame); /* for curve following objects, parented curve has to be updated too */ if (ob->type == OB_CURVE) { @@ -3699,7 +3720,7 @@ bool BKE_object_modifier_update_subframe(Scene *scene, Object *ob, bool update_m if (ob->type == OB_ARMATURE) { bArmature *arm = ob->data; BKE_animsys_evaluate_animdata(scene, &arm->id, arm->adt, frame, ADT_RECALC_ANIM); - BKE_pose_where_is(scene, ob); + BKE_pose_where_is(eval_ctx, scene, ob); } return false; diff --git a/source/blender/blenkernel/intern/object_deform.c b/source/blender/blenkernel/intern/object_deform.c index 455c4b6352d..754434eaef6 100644 --- a/source/blender/blenkernel/intern/object_deform.c +++ b/source/blender/blenkernel/intern/object_deform.c @@ -407,7 +407,7 @@ void BKE_object_defgroup_remove(Object *ob, bDeformGroup *defgroup) else object_defgroup_remove_object_mode(ob, defgroup); - BKE_mesh_batch_cache_dirty(ob->data, BKE_MESH_BATCH_DIRTY_PAINT); + BKE_mesh_batch_cache_dirty(ob->data, BKE_MESH_BATCH_DIRTY_ALL); } /** @@ -484,6 +484,8 @@ bool BKE_object_defgroup_array_get(ID *id, MDeformVert **dvert_arr, int *dvert_t *dvert_tot = lt->pntsu * lt->pntsv * lt->pntsw; return true; } + default: + break; } } diff --git a/source/blender/blenkernel/intern/object_dupli.c b/source/blender/blenkernel/intern/object_dupli.c index 774ddca061d..c25207d81bf 100644 --- a/source/blender/blenkernel/intern/object_dupli.c +++ b/source/blender/blenkernel/intern/object_dupli.c @@ -69,7 +69,7 @@ /* Dupli-Geometry */ typedef struct DupliContext { - EvaluationContext *eval_ctx; + const EvaluationContext *eval_ctx; bool do_update; bool animated; Group *group; /* XXX child objects are selected from this group if set, could be nicer */ @@ -95,7 +95,7 @@ typedef struct DupliGenerator { static const DupliGenerator *get_dupli_generator(const DupliContext *ctx); /* create initial context for root object */ -static void init_context(DupliContext *r_ctx, EvaluationContext *eval_ctx, Scene *scene, Object *ob, float space_mat[4][4], bool update) +static void init_context(DupliContext *r_ctx, const EvaluationContext *eval_ctx, Scene *scene, Object *ob, float space_mat[4][4], bool update) { r_ctx->eval_ctx = eval_ctx; r_ctx->scene = scene; @@ -341,7 +341,7 @@ static void make_duplis_group(const DupliContext *ctx) } } -const DupliGenerator gen_dupli_group = { +static const DupliGenerator gen_dupli_group = { OB_DUPLIGROUP, /* type */ make_duplis_group /* make_duplis */ }; @@ -397,7 +397,7 @@ static void make_duplis_frames(const DupliContext *ctx) * However, this has always been the way that this worked (i.e. pre 2.5), so I guess that it'll be fine! */ BKE_animsys_evaluate_animdata(scene, &ob->id, ob->adt, (float)scene->r.cfra, ADT_RECALC_ANIM); /* ob-eval will do drivers, so we don't need to do them */ - BKE_object_where_is_calc_time(scene, ob, (float)scene->r.cfra); + BKE_object_where_is_calc_time(ctx->eval_ctx, scene, ob, (float)scene->r.cfra); make_dupli(ctx, ob, ob->obmat, scene->r.cfra, false, false); } @@ -411,7 +411,7 @@ static void make_duplis_frames(const DupliContext *ctx) scene->r.cfra = cfrao; BKE_animsys_evaluate_animdata(scene, &ob->id, ob->adt, (float)scene->r.cfra, ADT_RECALC_ANIM); /* ob-eval will do drivers, so we don't need to do them */ - BKE_object_where_is_calc_time(scene, ob, (float)scene->r.cfra); + BKE_object_where_is_calc_time(ctx->eval_ctx, scene, ob, (float)scene->r.cfra); /* but, to make sure unkeyed object transforms are still sane, * let's copy object's original data back over @@ -419,7 +419,7 @@ static void make_duplis_frames(const DupliContext *ctx) *ob = copyob; } -const DupliGenerator gen_dupli_frames = { +static const DupliGenerator gen_dupli_frames = { OB_DUPLIFRAMES, /* type */ make_duplis_frames /* make_duplis */ }; @@ -545,10 +545,15 @@ static void make_duplis_verts(const DupliContext *ctx) BMEditMesh *em = BKE_editmesh_from_object(parent); CustomDataMask dm_mask = (use_texcoords ? CD_MASK_BAREMESH | CD_MASK_ORCO : CD_MASK_BAREMESH); - if (em) - vdd.dm = editbmesh_get_derived_cage(scene, parent, em, dm_mask); - else - vdd.dm = mesh_get_derived_final(scene, parent, dm_mask); + if (ctx->eval_ctx->mode == DAG_EVAL_RENDER) { + vdd.dm = mesh_create_derived_render(ctx->eval_ctx, scene, parent, dm_mask); + } + else if (em) { + vdd.dm = editbmesh_get_derived_cage(ctx->eval_ctx, scene, parent, em, dm_mask); + } + else { + vdd.dm = mesh_get_derived_final(ctx->eval_ctx, scene, parent, dm_mask); + } vdd.edit_btmesh = me->edit_btmesh; if (use_texcoords) @@ -564,7 +569,7 @@ static void make_duplis_verts(const DupliContext *ctx) vdd.dm->release(vdd.dm); } -const DupliGenerator gen_dupli_verts = { +static const DupliGenerator gen_dupli_verts = { OB_DUPLIVERTS, /* type */ make_duplis_verts /* make_duplis */ }; @@ -677,7 +682,7 @@ static void make_duplis_font(const DupliContext *ctx) MEM_freeN(chartransdata); } -const DupliGenerator gen_dupli_verts_font = { +static const DupliGenerator gen_dupli_verts_font = { OB_DUPLIVERTS, /* type */ make_duplis_font /* make_duplis */ }; @@ -808,10 +813,15 @@ static void make_duplis_faces(const DupliContext *ctx) BMEditMesh *em = BKE_editmesh_from_object(parent); CustomDataMask dm_mask = (use_texcoords ? CD_MASK_BAREMESH | CD_MASK_ORCO | CD_MASK_MLOOPUV : CD_MASK_BAREMESH); - if (em) - fdd.dm = editbmesh_get_derived_cage(scene, parent, em, dm_mask); - else - fdd.dm = mesh_get_derived_final(scene, parent, dm_mask); + if (ctx->eval_ctx->mode == DAG_EVAL_RENDER) { + fdd.dm = mesh_create_derived_render(ctx->eval_ctx, scene, parent, dm_mask); + } + else if (em) { + fdd.dm = editbmesh_get_derived_cage(ctx->eval_ctx, scene, parent, em, dm_mask); + } + else { + fdd.dm = mesh_get_derived_final(ctx->eval_ctx, scene, parent, dm_mask); + } if (use_texcoords) { CustomData *ml_data = fdd.dm->getLoopDataLayout(fdd.dm); @@ -835,7 +845,7 @@ static void make_duplis_faces(const DupliContext *ctx) fdd.dm->release(fdd.dm); } -const DupliGenerator gen_dupli_faces = { +static const DupliGenerator gen_dupli_faces = { OB_DUPLIFACES, /* type */ make_duplis_faces /* make_duplis */ }; @@ -888,6 +898,7 @@ static void make_duplis_particle_system(const DupliContext *ctx, ParticleSystem if ((psys->renderdata || part->draw_as == PART_DRAW_REND) && ELEM(part->ren_as, PART_DRAW_OB, PART_DRAW_GR)) { ParticleSimulationData sim = {NULL}; + sim.eval_ctx = ctx->eval_ctx; sim.scene = scene; sim.ob = par; sim.psys = psys; @@ -1069,7 +1080,7 @@ static void make_duplis_particle_system(const DupliContext *ctx, ParticleSystem } else { /* to give ipos in object correct offset */ - BKE_object_where_is_calc_time(scene, ob, ctime - pa_time); + BKE_object_where_is_calc_time(ctx->eval_ctx, scene, ob, ctime - pa_time); copy_v3_v3(vec, obmat[3]); obmat[3][0] = obmat[3][1] = obmat[3][2] = 0.0f; @@ -1156,7 +1167,7 @@ static void make_duplis_particles(const DupliContext *ctx) } } -const DupliGenerator gen_dupli_particles = { +static const DupliGenerator gen_dupli_particles = { OB_DUPLIPARTS, /* type */ make_duplis_particles /* make_duplis */ }; @@ -1205,7 +1216,7 @@ static const DupliGenerator *get_dupli_generator(const DupliContext *ctx) /* ---- ListBase dupli container implementation ---- */ /* Returns a list of DupliObject */ -ListBase *object_duplilist_ex(EvaluationContext *eval_ctx, Scene *scene, Object *ob, bool update) +ListBase *object_duplilist_ex(const EvaluationContext *eval_ctx, Scene *scene, Object *ob, bool update) { ListBase *duplilist = MEM_callocN(sizeof(ListBase), "duplilist"); DupliContext ctx; @@ -1220,7 +1231,7 @@ ListBase *object_duplilist_ex(EvaluationContext *eval_ctx, Scene *scene, Object /* note: previously updating was always done, this is why it defaults to be on * but there are likely places it can be called without updating */ -ListBase *object_duplilist(EvaluationContext *eval_ctx, Scene *sce, Object *ob) +ListBase *object_duplilist(const EvaluationContext *eval_ctx, Scene *sce, Object *ob) { return object_duplilist_ex(eval_ctx, sce, ob, true); } @@ -1261,7 +1272,7 @@ int count_duplilist(Object *ob) return 1; } -DupliApplyData *duplilist_apply(Object *ob, Scene *scene, ListBase *duplilist) +DupliApplyData *duplilist_apply(const EvaluationContext *eval_ctx, Object *ob, Scene *scene, ListBase *duplilist) { DupliApplyData *apply_data = NULL; int num_objects = BLI_listbase_count(duplilist); @@ -1277,7 +1288,7 @@ DupliApplyData *duplilist_apply(Object *ob, Scene *scene, ListBase *duplilist) for (dob = duplilist->first, i = 0; dob; dob = dob->next, ++i) { /* make sure derivedmesh is calculated once, before drawing */ if (scene && !(dob->ob->transflag & OB_DUPLICALCDERIVED) && dob->ob->type == OB_MESH) { - mesh_get_derived_final(scene, dob->ob, scene->customdata_mask); + mesh_get_derived_final(eval_ctx, scene, dob->ob, scene->customdata_mask); dob->ob->transflag |= OB_DUPLICALCDERIVED; } } diff --git a/source/blender/blenkernel/intern/object_update.c b/source/blender/blenkernel/intern/object_update.c index 70e1f434388..c5d267ace9d 100644 --- a/source/blender/blenkernel/intern/object_update.c +++ b/source/blender/blenkernel/intern/object_update.c @@ -69,7 +69,7 @@ static ThreadMutex material_lock = BLI_MUTEX_INITIALIZER; -void BKE_object_eval_local_transform(EvaluationContext *UNUSED(eval_ctx), +void BKE_object_eval_local_transform(const EvaluationContext *UNUSED(eval_ctx), Scene *UNUSED(scene), Object *ob) { @@ -81,7 +81,7 @@ void BKE_object_eval_local_transform(EvaluationContext *UNUSED(eval_ctx), /* Evaluate parent */ /* NOTE: based on solve_parenting(), but with the cruft stripped out */ -void BKE_object_eval_parent(EvaluationContext *UNUSED(eval_ctx), +void BKE_object_eval_parent(const EvaluationContext *UNUSED(eval_ctx), Scene *scene, Object *ob) { @@ -113,7 +113,7 @@ void BKE_object_eval_parent(EvaluationContext *UNUSED(eval_ctx), } } -void BKE_object_eval_constraints(EvaluationContext *UNUSED(eval_ctx), +void BKE_object_eval_constraints(const EvaluationContext *eval_ctx, Scene *scene, Object *ob) { @@ -132,11 +132,11 @@ void BKE_object_eval_constraints(EvaluationContext *UNUSED(eval_ctx), * */ cob = BKE_constraints_make_evalob(scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT); - BKE_constraints_solve(&ob->constraints, cob, ctime); + BKE_constraints_solve(eval_ctx, &ob->constraints, cob, ctime); BKE_constraints_clear_evalob(cob); } -void BKE_object_eval_done(EvaluationContext *UNUSED(eval_ctx), Object *ob) +void BKE_object_eval_done(const EvaluationContext *UNUSED(eval_ctx), Object *ob) { DEBUG_PRINT("%s on %s\n", __func__, ob->id.name); @@ -145,9 +145,10 @@ void BKE_object_eval_done(EvaluationContext *UNUSED(eval_ctx), Object *ob) else ob->transflag &= ~OB_NEG_SCALE; } -void BKE_object_handle_data_update(EvaluationContext *eval_ctx, - Scene *scene, - Object *ob) +void BKE_object_handle_data_update( + const EvaluationContext *eval_ctx, + Scene *scene, + Object *ob) { ID *data_id = (ID *)ob->data; AnimData *adt = BKE_animdata_from_id(data_id); @@ -184,10 +185,10 @@ void BKE_object_handle_data_update(EvaluationContext *eval_ctx, } #endif if (em) { - makeDerivedMesh(scene, ob, em, data_mask, false); /* was CD_MASK_BAREMESH */ + makeDerivedMesh(eval_ctx, scene, ob, em, data_mask, false); /* was CD_MASK_BAREMESH */ } else { - makeDerivedMesh(scene, ob, NULL, data_mask, false); + makeDerivedMesh(eval_ctx, scene, ob, NULL, data_mask, false); } break; } @@ -199,7 +200,7 @@ void BKE_object_handle_data_update(EvaluationContext *eval_ctx, } } else { - BKE_pose_where_is(scene, ob); + BKE_pose_where_is(eval_ctx, scene, ob); } break; @@ -210,11 +211,11 @@ void BKE_object_handle_data_update(EvaluationContext *eval_ctx, case OB_CURVE: case OB_SURF: case OB_FONT: - BKE_displist_make_curveTypes(scene, ob, 0); + BKE_displist_make_curveTypes(eval_ctx, scene, ob, 0); break; case OB_LATTICE: - BKE_lattice_modifiers_calc(scene, ob); + BKE_lattice_modifiers_calc(eval_ctx, scene, ob); break; case OB_EMPTY: @@ -267,7 +268,7 @@ void BKE_object_handle_data_update(EvaluationContext *eval_ctx, ob->transflag |= OB_DUPLIPARTS; } - particle_system_update(scene, ob, psys, (eval_ctx->mode == DAG_EVAL_RENDER)); + particle_system_update(eval_ctx, scene, ob, psys, (eval_ctx->mode == DAG_EVAL_RENDER)); psys = psys->next; } else if (psys->flag & PSYS_DELETE) { @@ -285,7 +286,7 @@ void BKE_object_handle_data_update(EvaluationContext *eval_ctx, * the derivedmesh must be created before init_render_mesh, * since object_duplilist does dupliparticles before that */ CustomDataMask data_mask = CD_MASK_BAREMESH | CD_MASK_MFACE | CD_MASK_MTFACE | CD_MASK_MCOL; - dm = mesh_create_derived_render(scene, ob, data_mask); + dm = mesh_create_derived_render(eval_ctx, scene, ob, data_mask); dm->release(dm); for (psys = ob->particlesystem.first; psys; psys = psys->next) @@ -296,7 +297,7 @@ void BKE_object_handle_data_update(EvaluationContext *eval_ctx, /* quick cache removed */ } -void BKE_object_eval_uber_transform(EvaluationContext *UNUSED(eval_ctx), +void BKE_object_eval_uber_transform(const EvaluationContext *UNUSED(eval_ctx), Scene *UNUSED(scene), Object *ob) { @@ -326,7 +327,7 @@ void BKE_object_eval_uber_transform(EvaluationContext *UNUSED(eval_ctx), } } -void BKE_object_eval_uber_data(EvaluationContext *eval_ctx, +void BKE_object_eval_uber_data(const EvaluationContext *eval_ctx, Scene *scene, Object *ob) { @@ -361,7 +362,7 @@ void BKE_object_eval_uber_data(EvaluationContext *eval_ctx, /* Copy materials so render engines can access them. */ new_mesh->mat = MEM_dupallocN(mesh->mat); new_mesh->totcol = mesh->totcol; - DM_to_mesh(dm, new_mesh, ob, ob->lastDataMask, true); + DM_to_mesh(dm, new_mesh, ob, CD_MASK_MESH, true); new_mesh->edit_btmesh = mesh->edit_btmesh; /* Store result mesh as derived_mesh of object. This way we have * explicit way to query final object evaluated data and know for sure @@ -377,6 +378,11 @@ void BKE_object_eval_uber_data(EvaluationContext *eval_ctx, /* NOTE: Watch out, some tools might need it! * So keep around for now.. */ + /* Store original ID as a pointer in evaluated ID. + * This way we can restore original object data when we are freeing + * evaluated mesh. + */ + new_mesh->id.newid = &mesh->id; } #if 0 if (ob->derivedFinal != NULL) { @@ -396,8 +402,16 @@ void BKE_object_eval_uber_data(EvaluationContext *eval_ctx, ob->recalc &= ~(OB_RECALC_DATA | OB_RECALC_TIME); } -void BKE_object_eval_cloth(EvaluationContext *UNUSED(eval_ctx), Scene *scene, Object *object) +void BKE_object_eval_cloth(const EvaluationContext *UNUSED(eval_ctx), Scene *scene, Object *object) { DEBUG_PRINT("%s on %s\n", __func__, object->id.name); BKE_ptcache_object_reset(scene, object, PTCACHE_RESET_DEPSGRAPH); } + +void BKE_object_eval_update_shading(const EvaluationContext *UNUSED(eval_ctx), Object *object) +{ + DEBUG_PRINT("%s on %s\n", __func__, object->id.name); + if (object->type == OB_MESH) { + BKE_mesh_batch_cache_dirty(object->data, BKE_MESH_BATCH_DIRTY_SHADING); + } +} diff --git a/source/blender/blenkernel/intern/packedFile.c b/source/blender/blenkernel/intern/packedFile.c index 64f90ef5c52..89f25136caf 100644 --- a/source/blender/blenkernel/intern/packedFile.c +++ b/source/blender/blenkernel/intern/packedFile.c @@ -522,6 +522,8 @@ static void unpack_generate_paths( case ID_IM: BLI_snprintf(r_relpath, relpathlen, "//textures/%s", tempname); break; + default: + break; } { @@ -712,6 +714,8 @@ bool BKE_pack_check(ID *id) Library *li = (Library *)id; return li->packedfile != NULL; } + default: + break; } return false; } @@ -750,5 +754,7 @@ void BKE_unpack_id(Main *bmain, ID *id, ReportList *reports, int how) BKE_reportf(reports, RPT_ERROR, "Cannot unpack individual Library file, '%s'", li->name); break; } + default: + break; } } diff --git a/source/blender/blenkernel/intern/paint.c b/source/blender/blenkernel/intern/paint.c index 00efa4aa732..7acddbbb90e 100644 --- a/source/blender/blenkernel/intern/paint.c +++ b/source/blender/blenkernel/intern/paint.c @@ -49,6 +49,7 @@ #include "BKE_brush.h" #include "BKE_colortools.h" +#include "BKE_deform.h" #include "BKE_main.h" #include "BKE_context.h" #include "BKE_crazyspace.h" @@ -312,24 +313,31 @@ PaintCurve *BKE_paint_curve_add(Main *bmain, const char *name) { PaintCurve *pc; - pc = BKE_libblock_alloc(bmain, ID_PC, name); + pc = BKE_libblock_alloc(bmain, ID_PC, name, 0); return pc; } -PaintCurve *BKE_paint_curve_copy(Main *bmain, const PaintCurve *pc) +/** + * Only copy internal data of PaintCurve ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_paint_curve_copy_data(Main *UNUSED(bmain), PaintCurve *pc_dst, const PaintCurve *pc_src, const int UNUSED(flag)) { - PaintCurve *pc_new; - - pc_new = BKE_libblock_copy(bmain, &pc->id); - - if (pc->tot_points != 0) { - pc_new->points = MEM_dupallocN(pc->points); + if (pc_src->tot_points != 0) { + pc_dst->points = MEM_dupallocN(pc_src->points); } +} - BKE_id_copy_ensure_local(bmain, &pc->id, &pc_new->id); - - return pc_new; +PaintCurve *BKE_paint_curve_copy(Main *bmain, const PaintCurve *pc) +{ + PaintCurve *pc_copy; + BKE_id_copy_ex(bmain, &pc->id, (ID **)&pc_copy, 0, false); + return pc_copy; } void BKE_paint_curve_make_local(Main *bmain, PaintCurve *pc, const bool lib_local) @@ -391,7 +399,7 @@ Palette *BKE_palette_add(Main *bmain, const char *name) { Palette *palette; - palette = BKE_libblock_alloc(bmain, ID_PAL, name); + palette = BKE_libblock_alloc(bmain, ID_PAL, name, 0); /* enable fake user by default */ id_fake_user_set(&palette->id); @@ -399,17 +407,24 @@ Palette *BKE_palette_add(Main *bmain, const char *name) return palette; } -Palette *BKE_palette_copy(Main *bmain, const Palette *palette) +/** + * Only copy internal data of Palette ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_palette_copy_data(Main *UNUSED(bmain), Palette *palette_dst, const Palette *palette_src, const int UNUSED(flag)) { - Palette *palette_new; - - palette_new = BKE_libblock_copy(bmain, &palette->id); - - BLI_duplicatelist(&palette_new->colors, &palette->colors); - - BKE_id_copy_ensure_local(bmain, &palette->id, &palette_new->id); + BLI_duplicatelist(&palette_dst->colors, &palette_src->colors); +} - return palette_new; +Palette *BKE_palette_copy(Main *bmain, const Palette *palette) +{ + Palette *palette_copy; + BKE_id_copy_ex(bmain, &palette->id, (ID **)&palette_copy, 0, false); + return palette_copy; } void BKE_palette_make_local(Main *bmain, Palette *palette, const bool lib_local) @@ -455,7 +470,7 @@ bool BKE_paint_select_vert_test(Object *ob) (ob->type == OB_MESH) && (ob->data != NULL) && (((Mesh *)ob->data)->editflag & ME_EDIT_PAINT_VERT_SEL) && - (ob->mode & OB_MODE_WEIGHT_PAINT) + (ob->mode & OB_MODE_WEIGHT_PAINT || ob->mode & OB_MODE_VERTEX_PAINT) ); } @@ -540,12 +555,15 @@ void BKE_paint_free(Paint *paint) * still do a id_us_plus(), rather then if we were copying between 2 existing * scenes where a matching value should decrease the existing user count as * with paint_brush_set() */ -void BKE_paint_copy(Paint *src, Paint *tar) +void BKE_paint_copy(Paint *src, Paint *tar, const int flag) { tar->brush = src->brush; - id_us_plus((ID *)tar->brush); - id_us_plus((ID *)tar->palette); tar->cavity_curve = curvemapping_copy(src->cavity_curve); + + if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) { + id_us_plus((ID *)tar->brush); + id_us_plus((ID *)tar->palette); + } } void BKE_paint_stroke_get_average(Scene *scene, Object *ob, float stroke[3]) @@ -659,6 +677,33 @@ void BKE_sculptsession_free_deformMats(SculptSession *ss) MEM_SAFE_FREE(ss->deform_imats); } +void BKE_sculptsession_free_vwpaint_data(struct SculptSession *ss) +{ + struct SculptVertexPaintGeomMap *gmap = NULL; + if (ss->mode_type == OB_MODE_VERTEX_PAINT) { + gmap = &ss->mode.vpaint.gmap; + + MEM_SAFE_FREE(ss->mode.vpaint.previous_color); + } + else if (ss->mode_type == OB_MODE_WEIGHT_PAINT) { + gmap = &ss->mode.wpaint.gmap; + + MEM_SAFE_FREE(ss->mode.wpaint.alpha_weight); + if (ss->mode.wpaint.dvert_prev) { + BKE_defvert_array_free_elems(ss->mode.wpaint.dvert_prev, ss->totvert); + MEM_freeN(ss->mode.wpaint.dvert_prev); + ss->mode.wpaint.dvert_prev = NULL; + } + } + else { + return; + } + MEM_SAFE_FREE(gmap->vert_to_loop); + MEM_SAFE_FREE(gmap->vert_map_mem); + MEM_SAFE_FREE(gmap->vert_to_poly); + MEM_SAFE_FREE(gmap->poly_map_mem); +} + /* Write out the sculpt dynamic-topology BMesh to the Mesh */ static void sculptsession_bm_to_me_update_data_only(Object *ob, bool reorder) { @@ -694,7 +739,7 @@ void BKE_sculptsession_bm_to_me_for_render(Object *object) if (object->sculpt->bm) { /* Ensure no points to old arrays are stored in DM * - * Apparently, we could not use DAG_id_tag_update + * Apparently, we could not use DEG_id_tag_update * here because this will lead to the while object * surface to disappear, so we'll release DM in place. */ @@ -750,6 +795,8 @@ void BKE_sculptsession_free(Object *ob) if (ss->deform_imats) MEM_freeN(ss->deform_imats); + BKE_sculptsession_free_vwpaint_data(ob->sculpt); + MEM_freeN(ss); ob->sculpt = NULL; @@ -823,8 +870,9 @@ static bool sculpt_modifiers_active(Scene *scene, Sculpt *sd, Object *ob) /** * \param need_mask So the DerivedMesh thats returned has mask data */ -void BKE_sculpt_update_mesh_elements(Scene *scene, Sculpt *sd, Object *ob, - bool need_pmap, bool need_mask) +void BKE_sculpt_update_mesh_elements( + const EvaluationContext *eval_ctx, Scene *scene, Sculpt *sd, Object *ob, + bool need_pmap, bool need_mask) { DerivedMesh *dm; SculptSession *ss = ob->sculpt; @@ -834,6 +882,8 @@ void BKE_sculpt_update_mesh_elements(Scene *scene, Sculpt *sd, Object *ob, ss->modifiers_active = sculpt_modifiers_active(scene, sd, ob); ss->show_diffuse_color = (sd->flags & SCULPT_SHOW_DIFFUSE) != 0; + ss->building_vp_handle = false; + if (need_mask) { if (mmd == NULL) { if (!CustomData_has_layer(&me->vdata, CD_PAINT_MASK)) { @@ -860,9 +910,10 @@ void BKE_sculpt_update_mesh_elements(Scene *scene, Sculpt *sd, Object *ob, ss->kb = (mmd == NULL) ? BKE_keyblock_from_object(ob) : NULL; - dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH); + dm = mesh_get_derived_final(eval_ctx, scene, ob, CD_MASK_BAREMESH); - if (mmd) { + /* VWPaint require mesh info for loop lookup, so require sculpt mode here */ + if (mmd && ob->mode & OB_MODE_SCULPT) { ss->multires = mmd; ss->totvert = dm->getNumVerts(dm); ss->totpoly = dm->getNumPolys(dm); @@ -893,7 +944,7 @@ void BKE_sculpt_update_mesh_elements(Scene *scene, Sculpt *sd, Object *ob, ss->orig_cos = (ss->kb) ? BKE_keyblock_convert_to_vertcos(ob, ss->kb) : BKE_mesh_vertexCos_get(me, NULL); - BKE_crazyspace_build_sculpt(scene, ob, &ss->deform_imats, &ss->deform_cos); + BKE_crazyspace_build_sculpt(eval_ctx, scene, ob, &ss->deform_imats, &ss->deform_cos); BKE_pbvh_apply_vertCos(ss->pbvh, ss->deform_cos); for (a = 0; a < me->totvert; ++a) { @@ -929,6 +980,9 @@ void BKE_sculpt_update_mesh_elements(Scene *scene, Sculpt *sd, Object *ob, } } } + + /* 2.8x - avoid full mesh update! */ + BKE_mesh_batch_cache_dirty(me, BKE_MESH_BATCH_DIRTY_SCULPT_COORDS); } int BKE_sculpt_mask_layers_ensure(Object *ob, MultiresModifierData *mmd) diff --git a/source/blender/blenkernel/intern/particle.c b/source/blender/blenkernel/intern/particle.c index 0a7ccc3ce35..7ba7c13fe40 100644 --- a/source/blender/blenkernel/intern/particle.c +++ b/source/blender/blenkernel/intern/particle.c @@ -255,11 +255,16 @@ struct LatticeDeformData *psys_create_lattice_deform_data(ParticleSimulationData if (psys_in_edit_mode(sim->scene, sim->psys) == 0) { Object *lattice = NULL; ModifierData *md = (ModifierData *)psys_get_modifier(sim->ob, sim->psys); + int mode = G.is_rendering ? eModifierMode_Render : eModifierMode_Realtime; for (; md; md = md->next) { if (md->type == eModifierType_Lattice) { - LatticeModifierData *lmd = (LatticeModifierData *)md; - lattice = lmd->object; + if (md->mode & mode) { + LatticeModifierData *lmd = (LatticeModifierData *)md; + lattice = lmd->object; + sim->psys->lattice_strength = lmd->strength; + } + break; } } @@ -1840,7 +1845,7 @@ void precalc_guides(ParticleSimulationData *sim, ListBase *effectors) } } -int do_guides(ParticleSettings *part, ListBase *effectors, ParticleKey *state, int index, float time) +int do_guides(const EvaluationContext *eval_ctx, ParticleSettings *part, ListBase *effectors, ParticleKey *state, int index, float time) { CurveMapping *clumpcurve = (part->child_flag & PART_CHILD_USE_CLUMP_CURVE) ? part->clumpcurve : NULL; CurveMapping *roughcurve = (part->child_flag & PART_CHILD_USE_ROUGH_CURVE) ? part->roughcurve : NULL; @@ -1903,7 +1908,7 @@ int do_guides(ParticleSettings *part, ListBase *effectors, ParticleKey *state, i /* curve taper */ if (cu->taperobj) - mul_v3_fl(vec_to_point, BKE_displist_calc_taper(eff->scene, cu->taperobj, (int)(data->strength * guidetime * 100.0f), 100)); + mul_v3_fl(vec_to_point, BKE_displist_calc_taper(eval_ctx, eff->scene, cu->taperobj, (int)(data->strength * guidetime * 100.0f), 100)); else { /* curve size*/ if (cu->flag & CU_PATH_RADIUS) { @@ -2704,13 +2709,13 @@ void psys_cache_paths(ParticleSimulationData *sim, float cfra, const bool use_re if (sim->psys->effectors && (psys->part->flag & PART_CHILD_EFFECT) == 0) { for (k = 0, ca = cache[p]; k <= segments; k++, ca++) /* ca is safe to cast, since only co and vel are used */ - do_guides(sim->psys->part, sim->psys->effectors, (ParticleKey *)ca, p, (float)k / (float)segments); + do_guides(sim->eval_ctx, sim->psys->part, sim->psys->effectors, (ParticleKey *)ca, p, (float)k / (float)segments); } /* lattices have to be calculated separately to avoid mixups between effector calculations */ if (psys->lattice_deform_data) { for (k = 0, ca = cache[p]; k <= segments; k++, ca++) - calc_latt_deform(psys->lattice_deform_data, ca->co, 1.0f); + calc_latt_deform(psys->lattice_deform_data, ca->co, psys->lattice_strength); } } @@ -2752,7 +2757,7 @@ void psys_cache_paths(ParticleSimulationData *sim, float cfra, const bool use_re if (vg_length) MEM_freeN(vg_length); } -void psys_cache_edit_paths(Scene *scene, Object *ob, PTCacheEdit *edit, float cfra, const bool use_render_params) +void psys_cache_edit_paths(const EvaluationContext *eval_ctx, Scene *scene, Object *ob, PTCacheEdit *edit, float cfra, const bool use_render_params) { ParticleCacheKey *ca, **cache = edit->pathcache; ParticleEditSettings *pset = &scene->toolsettings->particle; @@ -2943,6 +2948,7 @@ void psys_cache_edit_paths(Scene *scene, Object *ob, PTCacheEdit *edit, float cf if (psys) { ParticleSimulationData sim = {0}; + sim.eval_ctx = eval_ctx; sim.scene = scene; sim.ob = ob; sim.psys = psys; @@ -3300,7 +3306,7 @@ ParticleSettings *psys_new_settings(const char *name, Main *main) if (main == NULL) main = G.main; - part = BKE_libblock_alloc(main, ID_PA, name); + part = BKE_libblock_alloc(main, ID_PA, name, 0); default_particle_settings(part); @@ -3331,38 +3337,45 @@ void BKE_particlesettings_rough_curve_init(ParticleSettings *part) part->roughcurve = cumap; } -ParticleSettings *BKE_particlesettings_copy(Main *bmain, const ParticleSettings *part) +/** + * Only copy internal data of ParticleSettings ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_particlesettings_copy_data( + Main *UNUSED(bmain), ParticleSettings *part_dst, const ParticleSettings *part_src, const int UNUSED(flag)) { - ParticleSettings *partn; - int a; + part_dst->pd = MEM_dupallocN(part_src->pd); + part_dst->pd2 = MEM_dupallocN(part_src->pd2); + part_dst->effector_weights = MEM_dupallocN(part_src->effector_weights); + part_dst->fluid = MEM_dupallocN(part_src->fluid); - partn = BKE_libblock_copy(bmain, &part->id); - - partn->pd = MEM_dupallocN(part->pd); - partn->pd2 = MEM_dupallocN(part->pd2); - partn->effector_weights = MEM_dupallocN(part->effector_weights); - partn->fluid = MEM_dupallocN(part->fluid); + if (part_src->clumpcurve) { + part_dst->clumpcurve = curvemapping_copy(part_src->clumpcurve); + } + if (part_src->roughcurve) { + part_dst->roughcurve = curvemapping_copy(part_src->roughcurve); + } - if (part->clumpcurve) - partn->clumpcurve = curvemapping_copy(part->clumpcurve); - if (part->roughcurve) - partn->roughcurve = curvemapping_copy(part->roughcurve); - - partn->boids = boid_copy_settings(part->boids); + part_dst->boids = boid_copy_settings(part_src->boids); - for (a = 0; a < MAX_MTEX; a++) { - if (part->mtex[a]) { - partn->mtex[a] = MEM_mallocN(sizeof(MTex), "psys_copy_tex"); - memcpy(partn->mtex[a], part->mtex[a], sizeof(MTex)); - id_us_plus((ID *)partn->mtex[a]->tex); + for (int a = 0; a < MAX_MTEX; a++) { + if (part_src->mtex[a]) { + part_dst->mtex[a] = MEM_dupallocN(part_src->mtex[a]); } } - BLI_duplicatelist(&partn->dupliweights, &part->dupliweights); - - BKE_id_copy_ensure_local(bmain, &part->id, &partn->id); + BLI_duplicatelist(&part_dst->dupliweights, &part_src->dupliweights); +} - return partn; +ParticleSettings *BKE_particlesettings_copy(Main *bmain, const ParticleSettings *part) +{ + ParticleSettings *part_copy; + BKE_id_copy_ex(bmain, &part->id, (ID **)&part_copy, 0, false); + return part_copy; } void BKE_particlesettings_make_local(Main *bmain, ParticleSettings *part, const bool lib_local) @@ -3374,7 +3387,8 @@ void BKE_particlesettings_make_local(Main *bmain, ParticleSettings *part, const /* Textures */ /************************************************/ -static int get_particle_uv(DerivedMesh *dm, ParticleData *pa, int face_index, const float fuv[4], char *name, float *texco) +static int get_particle_uv(DerivedMesh *dm, ParticleData *pa, int index, const float fuv[4], + char *name, float *texco, bool from_vert) { MFace *mf; MTFace *tf; @@ -3390,11 +3404,15 @@ static int get_particle_uv(DerivedMesh *dm, ParticleData *pa, int face_index, co if (pa) { i = ELEM(pa->num_dmcache, DMCACHE_NOTFOUND, DMCACHE_ISCHILD) ? pa->num : pa->num_dmcache; - if (i >= dm->getNumTessFaces(dm)) + if ((!from_vert && i >= dm->getNumTessFaces(dm)) || + (from_vert && i >= dm->getNumVerts(dm))) + { i = -1; + } + } + else { + i = index; } - else - i = face_index; if (i == -1) { texco[0] = 0.0f; @@ -3402,7 +3420,22 @@ static int get_particle_uv(DerivedMesh *dm, ParticleData *pa, int face_index, co texco[2] = 0.0f; } else { - mf = dm->getTessFaceData(dm, i, CD_MFACE); + if (from_vert) { + mf = dm->getTessFaceDataArray(dm, CD_MFACE); + + /* This finds the first face to contain the emitting vertex, + * this is not ideal, but is mostly fine as UV seams generally + * map to equal-colored parts of a texture */ + for (int j = 0; j < dm->getNumTessFaces(dm); j++, mf++) { + if (ELEM(i, mf->v1, mf->v2, mf->v3, mf->v4)) { + i = j; + break; + } + } + } + else { + mf = dm->getTessFaceData(dm, i, CD_MFACE); + } psys_interpolate_uvs(&tf[i], mf->v4, fuv, texco); @@ -3469,8 +3502,11 @@ static void get_cpa_texture(DerivedMesh *dm, ParticleSystem *psys, ParticleSetti mul_m4_v3(mtex->object->imat, texvec); break; case TEXCO_UV: - if (fw && get_particle_uv(dm, NULL, face_index, fw, mtex->uvname, texvec)) + if (fw && get_particle_uv(dm, NULL, face_index, fw, mtex->uvname, + texvec, (part->from == PART_FROM_VERT))) + { break; + } /* no break, failed to get uv's, so let's try orco's */ ATTR_FALLTHROUGH; case TEXCO_ORCO: @@ -3542,8 +3578,11 @@ void psys_get_texture(ParticleSimulationData *sim, ParticleData *pa, ParticleTex mul_m4_v3(mtex->object->imat, texvec); break; case TEXCO_UV: - if (get_particle_uv(sim->psmd->dm_final, pa, 0, pa->fuv, mtex->uvname, texvec)) + if (get_particle_uv(sim->psmd->dm_final, pa, 0, pa->fuv, mtex->uvname, + texvec, (part->from == PART_FROM_VERT))) + { break; + } /* no break, failed to get uv's, so let's try orco's */ ATTR_FALLTHROUGH; case TEXCO_ORCO: @@ -3748,12 +3787,12 @@ void psys_get_particle_on_path(ParticleSimulationData *sim, int p, ParticleKey * mul_mat3_m4_v3(hairmat, state->vel); if (sim->psys->effectors && (part->flag & PART_CHILD_GUIDE) == 0) { - do_guides(sim->psys->part, sim->psys->effectors, state, p, state->time); + do_guides(sim->eval_ctx, sim->psys->part, sim->psys->effectors, state, p, state->time); /* TODO: proper velocity handling */ } if (psys->lattice_deform_data && edit == 0) - calc_latt_deform(psys->lattice_deform_data, state->co, 1.0f); + calc_latt_deform(psys->lattice_deform_data, state->co, psys->lattice_strength); } } } @@ -3992,7 +4031,7 @@ int psys_get_particle_state(ParticleSimulationData *sim, int p, ParticleKey *sta do_child_modifiers(NULL, sim, NULL, key1->co, key1->vel, key1->rot, par_orco, cpa, cpa->fuv, mat, state, t); if (psys->lattice_deform_data) - calc_latt_deform(psys->lattice_deform_data, state->co, 1.0f); + calc_latt_deform(psys->lattice_deform_data, state->co, psys->lattice_strength); } else { if (pa->state.time == cfra || ELEM(part->phystype, PART_PHYS_NO, PART_PHYS_KEYED)) @@ -4051,7 +4090,7 @@ int psys_get_particle_state(ParticleSimulationData *sim, int p, ParticleKey *sta } if (sim->psys->lattice_deform_data) - calc_latt_deform(sim->psys->lattice_deform_data, state->co, 1.0f); + calc_latt_deform(sim->psys->lattice_deform_data, state->co, psys->lattice_strength); } return 1; @@ -4275,9 +4314,10 @@ void psys_make_billboard(ParticleBillboardData *bb, float xvec[3], float yvec[3] madd_v3_v3fl(center, yvec, bb->offset[1]); } -void psys_apply_hair_lattice(Scene *scene, Object *ob, ParticleSystem *psys) +void psys_apply_hair_lattice(const EvaluationContext *eval_ctx, Scene *scene, Object *ob, ParticleSystem *psys) { ParticleSimulationData sim = {0}; + sim.eval_ctx = eval_ctx; sim.scene = scene; sim.ob = ob; sim.psys = psys; @@ -4298,7 +4338,7 @@ void psys_apply_hair_lattice(Scene *scene, Object *ob, ParticleSystem *psys) hkey = pa->hair; for (h = 0; h < pa->totkey; h++, hkey++) { mul_m4_v3(hairmat, hkey->co); - calc_latt_deform(psys->lattice_deform_data, hkey->co, 1.0f); + calc_latt_deform(psys->lattice_deform_data, hkey->co, psys->lattice_strength); mul_m4_v3(imat, hkey->co); } } diff --git a/source/blender/blenkernel/intern/particle_child.c b/source/blender/blenkernel/intern/particle_child.c index bfcda89a635..d2ad05c20b7 100644 --- a/source/blender/blenkernel/intern/particle_child.c +++ b/source/blender/blenkernel/intern/particle_child.c @@ -702,7 +702,7 @@ void do_child_modifiers(ParticleThreadContext *ctx, ParticleSimulationData *sim, if (part->flag & PART_CHILD_EFFECT) /* state is safe to cast, since only co and vel are used */ - guided = do_guides(sim->psys->part, sim->psys->effectors, (ParticleKey *)state, cpa->parent, t); + guided = do_guides(sim->eval_ctx, sim->psys->part, sim->psys->effectors, (ParticleKey *)state, cpa->parent, t); if (guided == 0) { float orco_offset[3]; diff --git a/source/blender/blenkernel/intern/particle_distribute.c b/source/blender/blenkernel/intern/particle_distribute.c index 6603521c42f..ff8a638089f 100644 --- a/source/blender/blenkernel/intern/particle_distribute.c +++ b/source/blender/blenkernel/intern/particle_distribute.c @@ -39,6 +39,7 @@ #include "BLI_jitter.h" #include "BLI_kdtree.h" #include "BLI_math.h" +#include "BLI_math_geom.h" #include "BLI_rand.h" #include "BLI_sort.h" #include "BLI_task.h" @@ -213,14 +214,22 @@ static void distribute_grid(DerivedMesh *dm, ParticleSystem *psys) copy_v3_v3(co2, co1); co2[a] += delta[a] + 0.001f*d; co1[a] -= 0.001f*d; - + + struct IsectRayPrecalc isect_precalc; + float ray_direction[3]; + sub_v3_v3v3(ray_direction, co2, co1); + isect_ray_tri_watertight_v3_precalc(&isect_precalc, ray_direction); + /* lets intersect the faces */ for (i=0; i<totface; i++,mface++) { copy_v3_v3(v1, mvert[mface->v1].co); copy_v3_v3(v2, mvert[mface->v2].co); copy_v3_v3(v3, mvert[mface->v3].co); - bool intersects_tri = isect_axial_line_segment_tri_v3(a, co1, co2, v2, v3, v1, &lambda); + bool intersects_tri = isect_ray_tri_watertight_v3(co1, + &isect_precalc, + v1, v2, v3, + &lambda, NULL); if (intersects_tri) { if (from==PART_FROM_FACE) (pa+(int)(lambda*size[a])*a0mul)->flag &= ~PARS_UNEXIST; @@ -231,7 +240,10 @@ static void distribute_grid(DerivedMesh *dm, ParticleSystem *psys) if (mface->v4 && (!intersects_tri || from==PART_FROM_VOLUME)) { copy_v3_v3(v4, mvert[mface->v4].co); - if (isect_axial_line_segment_tri_v3(a, co1, co2, v4, v1, v3, &lambda)) { + if (isect_ray_tri_watertight_v3(co1, + &isect_precalc, + v1, v3, v4, + &lambda, NULL)) { if (from==PART_FROM_FACE) (pa+(int)(lambda*size[a])*a0mul)->flag &= ~PARS_UNEXIST; else @@ -300,19 +312,16 @@ static void distribute_grid(DerivedMesh *dm, ParticleSystem *psys) static void hammersley_create(float *out, int n, int seed, float amount) { RNG *rng; - double p, t, offs[2]; - int k, kk; + + double offs[2], t; rng = BLI_rng_new(31415926 + n + seed); offs[0] = BLI_rng_get_double(rng) + (double)amount; offs[1] = BLI_rng_get_double(rng) + (double)amount; BLI_rng_free(rng); - for (k = 0; k < n; k++) { - t = 0; - for (p = 0.5, kk = k; kk; p *= 0.5, kk >>= 1) - if (kk & 1) /* kk mod 2 = 1 */ - t += p; + for (int k = 0; k < n; k++) { + BLI_hammersley_1D(k, &t); out[2*k + 0] = fmod((double)k/(double)n + offs[0], 1.0); out[2*k + 1] = fmod(t + offs[1], 1.0); @@ -427,12 +436,37 @@ static int distribute_binary_search(float *sum, int n, float value) static void distribute_from_verts_exec(ParticleTask *thread, ParticleData *pa, int p) { ParticleThreadContext *ctx= thread->ctx; - int rng_skip_tot= PSYS_RND_DIST_SKIP; /* count how many rng_* calls wont need skipping */ + MFace *mface; + + mface = ctx->dm->getTessFaceDataArray(ctx->dm, CD_MFACE); + + int rng_skip_tot = PSYS_RND_DIST_SKIP; /* count how many rng_* calls wont need skipping */ /* TODO_PARTICLE - use original index */ - pa->num= ctx->index[p]; - pa->fuv[0] = 1.0f; - pa->fuv[1] = pa->fuv[2] = pa->fuv[3] = 0.0; + pa->num = ctx->index[p]; + + zero_v4(pa->fuv); + + if (pa->num != DMCACHE_NOTFOUND && pa->num < ctx->dm->getNumVerts(ctx->dm)) { + + /* This finds the first face to contain the emitting vertex, + * this is not ideal, but is mostly fine as UV seams generally + * map to equal-colored parts of a texture */ + for (int i = 0; i < ctx->dm->getNumTessFaces(ctx->dm); i++, mface++) { + if (ELEM(pa->num, mface->v1, mface->v2, mface->v3, mface->v4)) { + unsigned int *vert = &mface->v1; + + for (int j = 0; j < 4; j++, vert++) { + if (*vert == pa->num) { + pa->fuv[j] = 1.0f; + break; + } + } + + break; + } + } + } #if ONLY_WORKING_WITH_PA_VERTS if (ctx->tree) { @@ -873,10 +907,7 @@ static int psys_thread_context_init_distribute(ParticleThreadContext *ctx, Parti else dm= CDDM_from_mesh((Mesh*)ob->data); - /* BMESH ONLY, for verts we don't care about tessfaces */ - if (from != PART_FROM_VERT) { - DM_ensure_tessface(dm); - } + DM_ensure_tessface(dm); /* we need orco for consistent distributions */ if (!CustomData_has_layer(&dm->vertData, CD_ORCO)) @@ -1070,13 +1101,10 @@ static int psys_thread_context_init_distribute(ParticleThreadContext *ctx, Parti /* This is to address tricky issues with vertex-emitting when user tries (and expects) exact 1-1 vert/part * distribution (see T47983 and its two example files). It allows us to consider pos as * 'midpoint between v and v+1' (or 'p and p+1', depending whether we have more vertices than particles or not), - * and avoid stumbling over float imprecisions in element_sum. */ - if (from == PART_FROM_VERT) { - pos = (totpart < totmapped) ? 0.5 / (double)totmapped : step * 0.5; /* We choose the smaller step. */ - } - else { - pos = 0.0; - } + * and avoid stumbling over float imprecisions in element_sum. + * Note: moved face and volume distribution to this as well (instead of starting at zero), + * for the same reasons, see T52682. */ + pos = (totpart < totmapped) ? 0.5 / (double)totmapped : step * 0.5; /* We choose the smaller step. */ for (i = 0, p = 0; p < totpart; p++, pos += step) { for ( ; (i < totmapped - 1) && (pos > (double)element_sum[i]); i++); @@ -1115,7 +1143,7 @@ static int psys_thread_context_init_distribute(ParticleThreadContext *ctx, Parti if (jitlevel == 0) { jitlevel= totpart/totelem; - if (part->flag & PART_EDISTR) jitlevel*= 2; /* looks better in general, not very scietific */ + if (part->flag & PART_EDISTR) jitlevel*= 2; /* looks better in general, not very scientific */ if (jitlevel<3) jitlevel= 3; } diff --git a/source/blender/blenkernel/intern/particle_system.c b/source/blender/blenkernel/intern/particle_system.c index a6ed50fd0e9..ddf4f04c808 100644 --- a/source/blender/blenkernel/intern/particle_system.c +++ b/source/blender/blenkernel/intern/particle_system.c @@ -980,14 +980,14 @@ void psys_get_birth_coords(ParticleSimulationData *sim, ParticleData *pa, Partic } /* recursively evaluate emitter parent anim at cfra */ -static void evaluate_emitter_anim(Scene *scene, Object *ob, float cfra) +static void evaluate_emitter_anim(const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, float cfra) { if (ob->parent) - evaluate_emitter_anim(scene, ob->parent, cfra); + evaluate_emitter_anim(eval_ctx, scene, ob->parent, cfra); /* we have to force RECALC_ANIM here since where_is_objec_time only does drivers */ BKE_animsys_evaluate_animdata(scene, &ob->id, ob->adt, cfra, ADT_RECALC_ANIM); - BKE_object_where_is_calc_time(scene, ob, cfra); + BKE_object_where_is_calc_time(eval_ctx, scene, ob, cfra); } /* sets particle to the emitter surface with initial velocity & rotation */ @@ -1001,7 +1001,7 @@ void reset_particle(ParticleSimulationData *sim, ParticleData *pa, float dtime, /* get precise emitter matrix if particle is born */ if (part->type != PART_HAIR && dtime > 0.f && pa->time < cfra && pa->time >= sim->psys->cfra) { - evaluate_emitter_anim(sim->scene, sim->ob, pa->time); + evaluate_emitter_anim(sim->eval_ctx, sim->scene, sim->ob, pa->time); psys->flag |= PSYS_OB_ANIM_RESTORE; } @@ -1133,7 +1133,8 @@ static void set_keyed_keys(ParticleSimulationData *sim) int totpart = psys->totpart, k, totkeys = psys->totkeyed; int keyed_flag = 0; - ksim.scene= sim->scene; + ksim.eval_ctx = sim->eval_ctx; + ksim.scene = sim->scene; /* no proper targets so let's clear and bail out */ if (psys->totkeyed==0) { @@ -1294,7 +1295,7 @@ void psys_update_particle_tree(ParticleSystem *psys, float cfra) static void psys_update_effectors(ParticleSimulationData *sim) { pdEndEffectors(&sim->psys->effectors); - sim->psys->effectors = pdInitEffectors(sim->scene, sim->ob, sim->psys, + sim->psys->effectors = pdInitEffectors(sim->eval_ctx, sim->scene, sim->ob, sim->psys, sim->psys->part->effector_weights, true); precalc_guides(sim, sim->psys->effectors); } @@ -2115,7 +2116,7 @@ static void basic_integrate(ParticleSimulationData *sim, int p, float dfra, floa tkey.time=pa->state.time; if (part->type != PART_HAIR) { - if (do_guides(sim->psys->part, sim->psys->effectors, &tkey, p, time)) { + if (do_guides(sim->eval_ctx, sim->psys->part, sim->psys->effectors, &tkey, p, time)) { copy_v3_v3(pa->state.co,tkey.co); /* guides don't produce valid velocity */ sub_v3_v3v3(pa->state.vel, tkey.co, pa->prev_state.co); @@ -2319,21 +2320,21 @@ static void collision_point_on_surface(float p[3], ParticleCollisionElement *pce } case 3: { - float p0[3], e1[3], e2[3], nor[3]; + float p0[3], e1[3], e2[3], nor[3]; - sub_v3_v3v3(e1, pce->x1, pce->x0); - sub_v3_v3v3(e2, pce->x2, pce->x0); - sub_v3_v3v3(p0, p, pce->x0); + sub_v3_v3v3(e1, pce->x1, pce->x0); + sub_v3_v3v3(e2, pce->x2, pce->x0); + sub_v3_v3v3(p0, p, pce->x0); - cross_v3_v3v3(nor, e1, e2); - normalize_v3(nor); + cross_v3_v3v3(nor, e1, e2); + normalize_v3(nor); - if (pce->inv_nor == 1) - negate_v3(nor); + if (pce->inv_nor == 1) + negate_v3(nor); - madd_v3_v3v3fl(co, pce->x0, nor, col->radius); - madd_v3_v3fl(co, e1, pce->uv[0]); - madd_v3_v3fl(co, e2, pce->uv[1]); + madd_v3_v3v3fl(co, pce->x0, nor, col->radius); + madd_v3_v3fl(co, e1, pce->uv[0]); + madd_v3_v3fl(co, e2, pce->uv[1]); break; } } @@ -3043,10 +3044,12 @@ static void hair_create_input_dm(ParticleSimulationData *sim, int totpoint, int /* calculate maximum segment length */ max_length = 0.0f; LOOP_PARTICLES { - for (k=1, key=pa->hair+1; k<pa->totkey; k++,key++) { - float length = len_v3v3(key->co, (key-1)->co); - if (max_length < length) - max_length = length; + if (!(pa->flag & PARS_UNEXIST)) { + for (k=1, key=pa->hair+1; k<pa->totkey; k++,key++) { + float length = len_v3v3(key->co, (key-1)->co); + if (max_length < length) + max_length = length; + } } } @@ -3058,76 +3061,78 @@ static void hair_create_input_dm(ParticleSimulationData *sim, int totpoint, int /* make vgroup for pin roots etc.. */ hair_index = 1; LOOP_PARTICLES { - float root_mat[4][4]; - float bending_stiffness; - bool use_hair; - - pa->hair_index = hair_index; - use_hair = psys_hair_use_simulation(pa, max_length); - - psys_mat_hair_to_object(sim->ob, sim->psmd->dm_final, psys->part->from, pa, hairmat); - mul_m4_m4m4(root_mat, sim->ob->obmat, hairmat); - normalize_m4(root_mat); - - bending_stiffness = CLAMPIS(1.0f - part->bending_random * psys_frand(psys, p + 666), 0.0f, 1.0f); - - for (k=0, key=pa->hair; k<pa->totkey; k++,key++) { - ClothHairData *hair; - float *co, *co_next; - - co = key->co; - co_next = (key+1)->co; - - /* create fake root before actual root to resist bending */ - if (k==0) { - hair = &psys->clmd->hairdata[pa->hair_index - 1]; + if (!(pa->flag & PARS_UNEXIST)) { + float root_mat[4][4]; + float bending_stiffness; + bool use_hair; + + pa->hair_index = hair_index; + use_hair = psys_hair_use_simulation(pa, max_length); + + psys_mat_hair_to_object(sim->ob, sim->psmd->dm_final, psys->part->from, pa, hairmat); + mul_m4_m4m4(root_mat, sim->ob->obmat, hairmat); + normalize_m4(root_mat); + + bending_stiffness = CLAMPIS(1.0f - part->bending_random * psys_frand(psys, p + 666), 0.0f, 1.0f); + + for (k=0, key=pa->hair; k<pa->totkey; k++,key++) { + ClothHairData *hair; + float *co, *co_next; + + co = key->co; + co_next = (key+1)->co; + + /* create fake root before actual root to resist bending */ + if (k==0) { + hair = &psys->clmd->hairdata[pa->hair_index - 1]; + copy_v3_v3(hair->loc, root_mat[3]); + copy_m3_m4(hair->rot, root_mat); + + hair->radius = hair_radius; + hair->bending_stiffness = bending_stiffness; + + add_v3_v3v3(mvert->co, co, co); + sub_v3_v3(mvert->co, co_next); + mul_m4_v3(hairmat, mvert->co); + + medge->v1 = pa->hair_index - 1; + medge->v2 = pa->hair_index; + + dvert = hair_set_pinning(dvert, 1.0f); + + mvert++; + medge++; + } + + /* store root transform in cloth data */ + hair = &psys->clmd->hairdata[pa->hair_index + k]; copy_v3_v3(hair->loc, root_mat[3]); copy_m3_m4(hair->rot, root_mat); - + hair->radius = hair_radius; hair->bending_stiffness = bending_stiffness; - - add_v3_v3v3(mvert->co, co, co); - sub_v3_v3(mvert->co, co_next); + + copy_v3_v3(mvert->co, co); mul_m4_v3(hairmat, mvert->co); - - medge->v1 = pa->hair_index - 1; - medge->v2 = pa->hair_index; - - dvert = hair_set_pinning(dvert, 1.0f); - + + if (k) { + medge->v1 = pa->hair_index + k - 1; + medge->v2 = pa->hair_index + k; + } + + /* roots and disabled hairs should be 1.0, the rest can be anything from 0.0 to 1.0 */ + if (use_hair) + dvert = hair_set_pinning(dvert, key->weight); + else + dvert = hair_set_pinning(dvert, 1.0f); + mvert++; - medge++; - } - - /* store root transform in cloth data */ - hair = &psys->clmd->hairdata[pa->hair_index + k]; - copy_v3_v3(hair->loc, root_mat[3]); - copy_m3_m4(hair->rot, root_mat); - - hair->radius = hair_radius; - hair->bending_stiffness = bending_stiffness; - - copy_v3_v3(mvert->co, co); - mul_m4_v3(hairmat, mvert->co); - - if (k) { - medge->v1 = pa->hair_index + k - 1; - medge->v2 = pa->hair_index + k; + if (k) + medge++; } - - /* roots and disabled hairs should be 1.0, the rest can be anything from 0.0 to 1.0 */ - if (use_hair) - dvert = hair_set_pinning(dvert, key->weight); - else - dvert = hair_set_pinning(dvert, 1.0f); - - mvert++; - if (k) - medge++; + + hair_index += pa->totkey + 1; } - - hair_index += pa->totkey + 1; } } @@ -3153,9 +3158,11 @@ static void do_hair_dynamics(ParticleSimulationData *sim) totpoint = 0; totedge = 0; LOOP_PARTICLES { - /* "out" dm contains all hairs */ - totedge += pa->totkey; - totpoint += pa->totkey + 1; /* +1 for virtual root point */ + if (!(pa->flag & PARS_UNEXIST)) { + /* "out" dm contains all hairs */ + totedge += pa->totkey; + totpoint += pa->totkey + 1; /* +1 for virtual root point */ + } } realloc_roots = false; /* whether hair root info array has to be reallocated */ @@ -3191,7 +3198,7 @@ static void do_hair_dynamics(ParticleSimulationData *sim) psys->hair_out_dm = CDDM_copy(psys->hair_in_dm); psys->hair_out_dm->getVertCos(psys->hair_out_dm, deformedVerts); - clothModifier_do(psys->clmd, sim->scene, sim->ob, psys->hair_in_dm, deformedVerts); + clothModifier_do(psys->clmd, sim->eval_ctx, sim->scene, sim->ob, psys->hair_in_dm, deformedVerts); CDDM_apply_vert_coords(psys->hair_out_dm, deformedVerts); @@ -4151,7 +4158,7 @@ static int hair_needs_recalc(ParticleSystem *psys) /* main particle update call, checks that things are ok on the large scale and * then advances in to actual particle calculations depending on particle type */ -void particle_system_update(Scene *scene, Object *ob, ParticleSystem *psys, const bool use_render_params) +void particle_system_update(const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, ParticleSystem *psys, const bool use_render_params) { ParticleSimulationData sim= {0}; ParticleSettings *part = psys->part; @@ -4165,10 +4172,11 @@ void particle_system_update(Scene *scene, Object *ob, ParticleSystem *psys, cons cfra= BKE_scene_frame_get(scene); - sim.scene= scene; - sim.ob= ob; - sim.psys= psys; - sim.psmd= psys_get_modifier(ob, psys); + sim.eval_ctx = eval_ctx; + sim.scene = scene; + sim.ob = ob; + sim.psys = psys; + sim.psmd = psys_get_modifier(ob, psys); /* system was already updated from modifier stack */ if (sim.psmd->flag & eParticleSystemFlag_psys_updated) { @@ -4311,7 +4319,7 @@ void particle_system_update(Scene *scene, Object *ob, ParticleSystem *psys, cons /* make sure emitter is left at correct time (particle emission can change this) */ if (psys->flag & PSYS_OB_ANIM_RESTORE) { - evaluate_emitter_anim(scene, ob, cfra); + evaluate_emitter_anim(eval_ctx, scene, ob, cfra); psys->flag &= ~PSYS_OB_ANIM_RESTORE; } @@ -4353,13 +4361,30 @@ void BKE_particlesystem_id_loop(ParticleSystem *psys, ParticleSystemIDFunc func, /* **** Depsgraph evaluation **** */ -void BKE_particle_system_eval(struct EvaluationContext *UNUSED(eval_ctx), - Scene *scene, - Object *ob, - ParticleSystem *psys) +void BKE_particle_system_settings_eval(const struct EvaluationContext *UNUSED(eval_ctx), + ParticleSystem *psys) +{ + if (G.debug & G_DEBUG_DEPSGRAPH) { + printf("%s on %s (%p)\n", __func__, psys->name, psys); + } + psys->recalc |= psys->part->recalc; +} + +void BKE_particle_system_settings_recalc_clear(struct EvaluationContext *UNUSED(eval_ctx), + ParticleSettings *particle_settings) +{ + if (G.debug & G_DEBUG_DEPSGRAPH) { + printf("%s on %s (%p)\n", __func__, particle_settings->id.name, particle_settings); + } + particle_settings->recalc = 0; +} + +void BKE_particle_system_eval_init(const struct EvaluationContext *UNUSED(eval_ctx), + Scene *scene, + Object *ob) { if (G.debug & G_DEBUG_DEPSGRAPH) { - printf("%s on %s:%s\n", __func__, ob->id.name, psys->name); + printf("%s on %s (%p)\n", __func__, ob->id.name, ob); } BKE_ptcache_object_reset(scene, ob, PTCACHE_RESET_DEPSGRAPH); } diff --git a/source/blender/blenkernel/intern/pbvh.c b/source/blender/blenkernel/intern/pbvh.c index ef2eaf8405b..6fc89eb778a 100644 --- a/source/blender/blenkernel/intern/pbvh.c +++ b/source/blender/blenkernel/intern/pbvh.c @@ -34,6 +34,7 @@ #include "BKE_pbvh.h" #include "BKE_ccg.h" +#include "BKE_subsurf.h" #include "BKE_DerivedMesh.h" #include "BKE_global.h" #include "BKE_mesh.h" /* for BKE_mesh_calc_normals */ @@ -607,6 +608,11 @@ void BKE_pbvh_build_grids(PBVH *bvh, CCGElem **grids, MEM_freeN(prim_bbc); } +void BKE_pbvh_set_ccgdm(PBVH *bvh, CCGDerivedMesh *ccgdm) +{ + bvh->ccgdm = ccgdm; +} + PBVH *BKE_pbvh_new(void) { PBVH *bvh = MEM_callocN(sizeof(PBVH), "pbvh"); @@ -1157,7 +1163,7 @@ static void pbvh_update_draw_buffers(PBVH *bvh, PBVHNode **nodes, int totnode) } } -static void pbvh_draw_BB(PBVH *bvh) +void BKE_pbvh_draw_BB(PBVH *bvh) { unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT); immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR); @@ -1331,6 +1337,12 @@ void BKE_pbvh_get_grid_key(const PBVH *bvh, CCGKey *key) *key = bvh->gridkey; } +CCGDerivedMesh *BKE_pbvh_get_ccgdm(const PBVH *bvh) +{ + return bvh->ccgdm; +} + + BMesh *BKE_pbvh_get_bmesh(PBVH *bvh) { BLI_assert(bvh->type == PBVH_BMESH); @@ -1529,14 +1541,16 @@ void BKE_pbvh_raycast( bool ray_face_intersection_quad( const float ray_start[3], const float ray_normal[3], const float t0[3], const float t1[3], const float t2[3], const float t3[3], - float *dist) + float *depth) { - float dist_test; + float depth_test; - if ((isect_ray_tri_epsilon_v3(ray_start, ray_normal, t0, t1, t2, &dist_test, NULL, 0.1f) && (dist_test < *dist)) || - (isect_ray_tri_epsilon_v3(ray_start, ray_normal, t0, t2, t3, &dist_test, NULL, 0.1f) && (dist_test < *dist))) + if ((isect_ray_tri_epsilon_v3( + ray_start, ray_normal, t0, t1, t2, &depth_test, NULL, 0.1f) && (depth_test < *depth)) || + (isect_ray_tri_epsilon_v3( + ray_start, ray_normal, t0, t2, t3, &depth_test, NULL, 0.1f) && (depth_test < *depth))) { - *dist = dist_test; + *depth = depth_test; return true; } else { @@ -1547,12 +1561,82 @@ bool ray_face_intersection_quad( bool ray_face_intersection_tri( const float ray_start[3], const float ray_normal[3], const float t0[3], const float t1[3], const float t2[3], - float *dist) + float *depth) +{ + float depth_test; + + if ((isect_ray_tri_epsilon_v3( + ray_start, ray_normal, t0, t1, t2, &depth_test, NULL, 0.1f) && (depth_test < *depth))) + { + *depth = depth_test; + return true; + } + else { + return false; + } +} + +/* Take advantage of the fact we know this wont be an intersection. + * Just handle ray-tri edges. */ +static float dist_squared_ray_to_tri_v3_fast( + const float ray_origin[3], const float ray_direction[3], + const float v0[3], const float v1[3], const float v2[3], + float r_point[3], float *r_depth) +{ + const float *tri[3] = {v0, v1, v2}; + float dist_sq_best = FLT_MAX; + for (int i = 0, j = 2; i < 3; j = i++) { + float point_test[3], depth_test = FLT_MAX; + const float dist_sq_test = dist_squared_ray_to_seg_v3( + ray_origin, ray_direction, tri[i], tri[j], point_test, &depth_test); + if (dist_sq_test < dist_sq_best || i == 0) { + copy_v3_v3(r_point, point_test); + *r_depth = depth_test; + dist_sq_best = dist_sq_test; + } + } + return dist_sq_best; +} + +bool ray_face_nearest_quad( + const float ray_start[3], const float ray_normal[3], + const float t0[3], const float t1[3], const float t2[3], const float t3[3], + float *depth, float *dist_sq) +{ + float dist_sq_test; + float co[3], depth_test; + + if (((dist_sq_test = dist_squared_ray_to_tri_v3_fast( + ray_start, ray_normal, t0, t1, t2, co, &depth_test)) < *dist_sq)) + { + *dist_sq = dist_sq_test; + *depth = depth_test; + if (((dist_sq_test = dist_squared_ray_to_tri_v3_fast( + ray_start, ray_normal, t0, t2, t3, co, &depth_test)) < *dist_sq)) + { + *dist_sq = dist_sq_test; + *depth = depth_test; + } + return true; + } + else { + return false; + } +} + +bool ray_face_nearest_tri( + const float ray_start[3], const float ray_normal[3], + const float t0[3], const float t1[3], const float t2[3], + float *depth, float *dist_sq) { - float dist_test; + float dist_sq_test; + float co[3], depth_test; - if ((isect_ray_tri_epsilon_v3(ray_start, ray_normal, t0, t1, t2, &dist_test, NULL, 0.1f) && (dist_test < *dist))) { - *dist = dist_test; + if (((dist_sq_test = dist_squared_ray_to_tri_v3_fast( + ray_start, ray_normal, t0, t1, t2, co, &depth_test)) < *dist_sq)) + { + *dist_sq = dist_sq_test; + *depth = depth_test; return true; } else { @@ -1564,7 +1648,7 @@ static bool pbvh_faces_node_raycast( PBVH *bvh, const PBVHNode *node, float (*origco)[3], const float ray_start[3], const float ray_normal[3], - float *dist) + float *depth) { const MVert *vert = bvh->verts; const MLoop *mloop = bvh->mloop; @@ -1586,7 +1670,7 @@ static bool pbvh_faces_node_raycast( origco[face_verts[0]], origco[face_verts[1]], origco[face_verts[2]], - dist); + depth); } else { /* intersect with current coordinates */ @@ -1595,7 +1679,7 @@ static bool pbvh_faces_node_raycast( vert[mloop[lt->tri[0]].v].co, vert[mloop[lt->tri[1]].v].co, vert[mloop[lt->tri[2]].v].co, - dist); + depth); } } @@ -1606,7 +1690,7 @@ static bool pbvh_grids_node_raycast( PBVH *bvh, PBVHNode *node, float (*origco)[3], const float ray_start[3], const float ray_normal[3], - float *dist) + float *depth) { const int totgrid = node->totprim; const int gridsize = bvh->gridkey.grid_size; @@ -1636,7 +1720,7 @@ static bool pbvh_grids_node_raycast( origco[y * gridsize + x + 1], origco[(y + 1) * gridsize + x + 1], origco[(y + 1) * gridsize + x], - dist); + depth); } else { hit |= ray_face_intersection_quad( @@ -1645,7 +1729,7 @@ static bool pbvh_grids_node_raycast( CCG_grid_elem_co(&bvh->gridkey, grid, x + 1, y), CCG_grid_elem_co(&bvh->gridkey, grid, x + 1, y + 1), CCG_grid_elem_co(&bvh->gridkey, grid, x, y + 1), - dist); + depth); } } } @@ -1660,7 +1744,7 @@ static bool pbvh_grids_node_raycast( bool BKE_pbvh_node_raycast( PBVH *bvh, PBVHNode *node, float (*origco)[3], bool use_origco, const float ray_start[3], const float ray_normal[3], - float *dist) + float *depth) { bool hit = false; @@ -1671,16 +1755,16 @@ bool BKE_pbvh_node_raycast( case PBVH_FACES: hit |= pbvh_faces_node_raycast( bvh, node, origco, - ray_start, ray_normal, dist); + ray_start, ray_normal, depth); break; case PBVH_GRIDS: hit |= pbvh_grids_node_raycast( bvh, node, origco, - ray_start, ray_normal, dist); + ray_start, ray_normal, depth); break; case PBVH_BMESH: hit = pbvh_bmesh_node_raycast( - node, ray_start, ray_normal, dist, use_origco); + node, ray_start, ray_normal, depth, use_origco); break; } @@ -1731,6 +1815,176 @@ void BKE_pbvh_raycast_project_ray_root( } } +/* -------------------------------------------------------------------- */ + +typedef struct { + struct DistRayAABB_Precalc dist_ray_to_aabb_precalc; + bool original; +} FindNearestRayData; + +static bool nearest_to_ray_aabb_dist_sq(PBVHNode *node, void *data_v) +{ + FindNearestRayData *rcd = data_v; + const float *bb_min, *bb_max; + + if (rcd->original) { + /* BKE_pbvh_node_get_original_BB */ + bb_min = node->orig_vb.bmin; + bb_max = node->orig_vb.bmax; + } + else { + /* BKE_pbvh_node_get_BB */ + bb_min = node->vb.bmin; + bb_max = node->vb.bmax; + } + + float co_dummy[3], depth; + node->tmin = dist_squared_ray_to_aabb_v3(&rcd->dist_ray_to_aabb_precalc, bb_min, bb_max, co_dummy, &depth); + /* Ideally we would skip distances outside the range. */ + return depth > 0.0f; +} + +void BKE_pbvh_find_nearest_to_ray( + PBVH *bvh, BKE_pbvh_SearchNearestCallback cb, void *data, + const float ray_start[3], const float ray_normal[3], + bool original) +{ + FindNearestRayData ncd; + + dist_squared_ray_to_aabb_v3_precalc(&ncd.dist_ray_to_aabb_precalc, ray_start, ray_normal); + ncd.original = original; + + BKE_pbvh_search_callback_occluded(bvh, nearest_to_ray_aabb_dist_sq, &ncd, cb, data); +} + + +static bool pbvh_faces_node_nearest_to_ray( + PBVH *bvh, const PBVHNode *node, + float (*origco)[3], + const float ray_start[3], const float ray_normal[3], + float *depth, float *dist_sq) +{ + const MVert *vert = bvh->verts; + const MLoop *mloop = bvh->mloop; + const int *faces = node->prim_indices; + int i, totface = node->totprim; + bool hit = false; + + for (i = 0; i < totface; ++i) { + const MLoopTri *lt = &bvh->looptri[faces[i]]; + const int *face_verts = node->face_vert_indices[i]; + + if (paint_is_face_hidden(lt, vert, mloop)) + continue; + + if (origco) { + /* intersect with backuped original coordinates */ + hit |= ray_face_nearest_tri( + ray_start, ray_normal, + origco[face_verts[0]], + origco[face_verts[1]], + origco[face_verts[2]], + depth, dist_sq); + } + else { + /* intersect with current coordinates */ + hit |= ray_face_nearest_tri( + ray_start, ray_normal, + vert[mloop[lt->tri[0]].v].co, + vert[mloop[lt->tri[1]].v].co, + vert[mloop[lt->tri[2]].v].co, + depth, dist_sq); + } + } + + return hit; +} + +static bool pbvh_grids_node_nearest_to_ray( + PBVH *bvh, PBVHNode *node, + float (*origco)[3], + const float ray_start[3], const float ray_normal[3], + float *depth, float *dist_sq) +{ + const int totgrid = node->totprim; + const int gridsize = bvh->gridkey.grid_size; + bool hit = false; + + for (int i = 0; i < totgrid; ++i) { + CCGElem *grid = bvh->grids[node->prim_indices[i]]; + BLI_bitmap *gh; + + if (!grid) + continue; + + gh = bvh->grid_hidden[node->prim_indices[i]]; + + for (int y = 0; y < gridsize - 1; ++y) { + for (int x = 0; x < gridsize - 1; ++x) { + /* check if grid face is hidden */ + if (gh) { + if (paint_is_grid_face_hidden(gh, gridsize, x, y)) + continue; + } + + if (origco) { + hit |= ray_face_nearest_quad( + ray_start, ray_normal, + origco[y * gridsize + x], + origco[y * gridsize + x + 1], + origco[(y + 1) * gridsize + x + 1], + origco[(y + 1) * gridsize + x], + depth, dist_sq); + } + else { + hit |= ray_face_nearest_quad( + ray_start, ray_normal, + CCG_grid_elem_co(&bvh->gridkey, grid, x, y), + CCG_grid_elem_co(&bvh->gridkey, grid, x + 1, y), + CCG_grid_elem_co(&bvh->gridkey, grid, x + 1, y + 1), + CCG_grid_elem_co(&bvh->gridkey, grid, x, y + 1), + depth, dist_sq); + } + } + } + + if (origco) + origco += gridsize * gridsize; + } + + return hit; +} + +bool BKE_pbvh_node_find_nearest_to_ray( + PBVH *bvh, PBVHNode *node, float (*origco)[3], bool use_origco, + const float ray_start[3], const float ray_normal[3], + float *depth, float *dist_sq) +{ + bool hit = false; + + if (node->flag & PBVH_FullyHidden) + return false; + + switch (bvh->type) { + case PBVH_FACES: + hit |= pbvh_faces_node_nearest_to_ray( + bvh, node, origco, + ray_start, ray_normal, depth, dist_sq); + break; + case PBVH_GRIDS: + hit |= pbvh_grids_node_nearest_to_ray( + bvh, node, origco, + ray_start, ray_normal, depth, dist_sq); + break; + case PBVH_BMESH: + hit = pbvh_bmesh_node_nearest_to_ray( + node, ray_start, ray_normal, depth, dist_sq, use_origco); + break; + } + + return hit; +} + typedef struct { DMSetMaterial setMaterial; bool wireframe; @@ -1844,7 +2098,7 @@ void BKE_pbvh_draw(PBVH *bvh, float (*planes)[4], float (*fnors)[3], } if (G.debug_value == 14) - pbvh_draw_BB(bvh); + BKE_pbvh_draw_BB(bvh); } struct PBVHNodeDrawCallbackData { diff --git a/source/blender/blenkernel/intern/pbvh_bmesh.c b/source/blender/blenkernel/intern/pbvh_bmesh.c index c5e49883dc6..187891e7210 100644 --- a/source/blender/blenkernel/intern/pbvh_bmesh.c +++ b/source/blender/blenkernel/intern/pbvh_bmesh.c @@ -718,20 +718,24 @@ static void pbvh_bmesh_node_drop_orig(PBVHNode *node) /****************************** EdgeQueue *****************************/ -typedef struct { +struct EdgeQueue; + +typedef struct EdgeQueue { Heap *heap; const float *center; + float center_proj[3]; /* for when we use projected coords. */ float radius_squared; float limit_len_squared; #ifdef USE_EDGEQUEUE_EVEN_SUBDIV float limit_len; #endif -#ifdef USE_EDGEQUEUE_FRONTFACE + bool (*edge_queue_tri_in_range)(const struct EdgeQueue *q, BMFace *f); + const float *view_normal; +#ifdef USE_EDGEQUEUE_FRONTFACE unsigned int use_view_normal : 1; #endif - } EdgeQueue; typedef struct { @@ -785,7 +789,6 @@ static bool edge_queue_tri_in_sphere(const EdgeQueue *q, BMFace *f) float c[3]; /* Get closest point in triangle to sphere center */ - // BM_iter_as_array(NULL, BM_VERTS_OF_FACE, f, (void **)v_tri, 3); BM_face_as_array_vert_tri(f, v_tri); closest_on_tri_to_point_v3(c, q->center, v_tri[0]->co, v_tri[1]->co, v_tri[2]->co); @@ -794,6 +797,25 @@ static bool edge_queue_tri_in_sphere(const EdgeQueue *q, BMFace *f) return len_squared_v3v3(q->center, c) <= q->radius_squared; } +static bool edge_queue_tri_in_circle(const EdgeQueue *q, BMFace *f) +{ + BMVert *v_tri[3]; + float c[3]; + float tri_proj[3][3]; + + /* Get closest point in triangle to sphere center */ + BM_face_as_array_vert_tri(f, v_tri); + + project_plane_normalized_v3_v3v3(tri_proj[0], v_tri[0]->co, q->view_normal); + project_plane_normalized_v3_v3v3(tri_proj[1], v_tri[1]->co, q->view_normal); + project_plane_normalized_v3_v3v3(tri_proj[2], v_tri[2]->co, q->view_normal); + + closest_on_tri_to_point_v3(c, q->center_proj, tri_proj[0], tri_proj[1], tri_proj[2]); + + /* Check if triangle intersects the sphere */ + return len_squared_v3v3(q->center_proj, c) <= q->radius_squared; +} + /* Return true if the vertex mask is less than 1.0, false otherwise */ static bool check_mask(EdgeQueueContext *eq_ctx, BMVert *v) { @@ -929,7 +951,7 @@ static void long_edge_queue_face_add( } #endif - if (edge_queue_tri_in_sphere(eq_ctx->q, f)) { + if (eq_ctx->q->edge_queue_tri_in_range(eq_ctx->q, f)) { /* Check each edge of the face */ BMLoop *l_first = BM_FACE_FIRST_LOOP(f); BMLoop *l_iter = l_first; @@ -960,7 +982,7 @@ static void short_edge_queue_face_add( } #endif - if (edge_queue_tri_in_sphere(eq_ctx->q, f)) { + if (eq_ctx->q->edge_queue_tri_in_range(eq_ctx->q, f)) { BMLoop *l_iter; BMLoop *l_first; @@ -984,7 +1006,7 @@ static void short_edge_queue_face_add( static void long_edge_queue_create( EdgeQueueContext *eq_ctx, PBVH *bvh, const float center[3], const float view_normal[3], - float radius) + float radius, const bool use_frontface, const bool use_projected) { eq_ctx->q->heap = BLI_heap_new(); eq_ctx->q->center = center; @@ -994,13 +1016,22 @@ static void long_edge_queue_create( eq_ctx->q->limit_len = bvh->bm_max_edge_len; #endif -#ifdef USE_EDGEQUEUE_FRONTFACE eq_ctx->q->view_normal = view_normal; - eq_ctx->q->use_view_normal = (view_normal != NULL); + +#ifdef USE_EDGEQUEUE_FRONTFACE + eq_ctx->q->use_view_normal = use_frontface; #else - UNUSED_VARS(view_normal); + UNUSED_VARS(use_frontface); #endif + if (use_projected) { + eq_ctx->q->edge_queue_tri_in_range = edge_queue_tri_in_circle; + project_plane_normalized_v3_v3v3(eq_ctx->q->center_proj, center, view_normal); + } + else { + eq_ctx->q->edge_queue_tri_in_range = edge_queue_tri_in_sphere; + } + #ifdef USE_EDGEQUEUE_TAG_VERIFY pbvh_bmesh_edge_tag_verify(bvh); #endif @@ -1037,7 +1068,7 @@ static void long_edge_queue_create( static void short_edge_queue_create( EdgeQueueContext *eq_ctx, PBVH *bvh, const float center[3], const float view_normal[3], - float radius) + float radius, const bool use_frontface, const bool use_projected) { eq_ctx->q->heap = BLI_heap_new(); eq_ctx->q->center = center; @@ -1047,13 +1078,22 @@ static void short_edge_queue_create( eq_ctx->q->limit_len = bvh->bm_min_edge_len; #endif -#ifdef USE_EDGEQUEUE_FRONTFACE eq_ctx->q->view_normal = view_normal; - eq_ctx->q->use_view_normal = (view_normal != NULL); + +#ifdef USE_EDGEQUEUE_FRONTFACE + eq_ctx->q->use_view_normal = use_frontface; #else - UNUSED_VARS(view_normal); + UNUSED_VARS(use_frontface); #endif + if (use_projected) { + eq_ctx->q->edge_queue_tri_in_range = edge_queue_tri_in_circle; + project_plane_normalized_v3_v3v3(eq_ctx->q->center_proj, center, view_normal); + } + else { + eq_ctx->q->edge_queue_tri_in_range = edge_queue_tri_in_sphere; + } + for (int n = 0; n < bvh->totnode; n++) { PBVHNode *node = &bvh->nodes[n]; @@ -1466,7 +1506,7 @@ static bool pbvh_bmesh_collapse_short_edges( bool pbvh_bmesh_node_raycast( PBVHNode *node, const float ray_start[3], - const float ray_normal[3], float *dist, + const float ray_normal[3], float *depth, bool use_original) { bool hit = false; @@ -1479,7 +1519,7 @@ bool pbvh_bmesh_node_raycast( node->bm_orco[t[0]], node->bm_orco[t[1]], node->bm_orco[t[2]], - dist); + depth); } } else { @@ -1498,7 +1538,7 @@ bool pbvh_bmesh_node_raycast( v_tri[0]->co, v_tri[1]->co, v_tri[2]->co, - dist); + depth); } } } @@ -1509,7 +1549,7 @@ bool pbvh_bmesh_node_raycast( bool BKE_pbvh_bmesh_node_raycast_detail( PBVHNode *node, const float ray_start[3], const float ray_normal[3], - float *dist, float *r_detail) + float *depth, float *r_detail) { if (node->flag & PBVH_FullyHidden) return 0; @@ -1531,7 +1571,7 @@ bool BKE_pbvh_bmesh_node_raycast_detail( v_tri[0]->co, v_tri[1]->co, v_tri[2]->co, - dist); + depth); if (hit_local) { f_hit = f; @@ -1554,6 +1594,47 @@ bool BKE_pbvh_bmesh_node_raycast_detail( return hit; } +bool pbvh_bmesh_node_nearest_to_ray( + PBVHNode *node, const float ray_start[3], + const float ray_normal[3], float *depth, float *dist_sq, + bool use_original) +{ + bool hit = false; + + if (use_original && node->bm_tot_ortri) { + for (int i = 0; i < node->bm_tot_ortri; i++) { + const int *t = node->bm_ortri[i]; + hit |= ray_face_nearest_tri( + ray_start, ray_normal, + node->bm_orco[t[0]], + node->bm_orco[t[1]], + node->bm_orco[t[2]], + depth, dist_sq); + } + } + else { + GSetIterator gs_iter; + + GSET_ITER (gs_iter, node->bm_faces) { + BMFace *f = BLI_gsetIterator_getKey(&gs_iter); + + BLI_assert(f->len == 3); + if (!BM_elem_flag_test(f, BM_ELEM_HIDDEN)) { + BMVert *v_tri[3]; + + BM_face_as_array_vert_tri(f, v_tri); + hit |= ray_face_nearest_tri( + ray_start, ray_normal, + v_tri[0]->co, + v_tri[1]->co, + v_tri[2]->co, + depth, dist_sq); + } + } + } + + return hit; +} void pbvh_bmesh_normals_update(PBVHNode **nodes, int totnode) { @@ -1854,7 +1935,7 @@ void BKE_pbvh_build_bmesh( bool BKE_pbvh_bmesh_update_topology( PBVH *bvh, PBVHTopologyUpdateMode mode, const float center[3], const float view_normal[3], - float radius) + float radius, const bool use_frontface, const bool use_projected) { /* 2 is enough for edge faces - manifold edge */ BLI_buffer_declare_static(BMLoop *, edge_loops, BLI_BUFFER_NOP, 2); @@ -1877,7 +1958,7 @@ bool BKE_pbvh_bmesh_update_topology( cd_vert_mask_offset, cd_vert_node_offset, cd_face_node_offset, }; - short_edge_queue_create(&eq_ctx, bvh, center, view_normal, radius); + short_edge_queue_create(&eq_ctx, bvh, center, view_normal, radius, use_frontface, use_projected); modified |= pbvh_bmesh_collapse_short_edges( &eq_ctx, bvh, &deleted_faces); BLI_heap_free(q.heap, NULL); @@ -1892,7 +1973,7 @@ bool BKE_pbvh_bmesh_update_topology( cd_vert_mask_offset, cd_vert_node_offset, cd_face_node_offset, }; - long_edge_queue_create(&eq_ctx, bvh, center, view_normal, radius); + long_edge_queue_create(&eq_ctx, bvh, center, view_normal, radius, use_frontface, use_projected); modified |= pbvh_bmesh_subdivide_long_edges( &eq_ctx, bvh, &edge_loops); BLI_heap_free(q.heap, NULL); diff --git a/source/blender/blenkernel/intern/pbvh_intern.h b/source/blender/blenkernel/intern/pbvh_intern.h index 19d3b31bd31..e05a3068682 100644 --- a/source/blender/blenkernel/intern/pbvh_intern.h +++ b/source/blender/blenkernel/intern/pbvh_intern.h @@ -149,6 +149,8 @@ struct PBVH { * objects in sculpt mode with different sizes at the same time, so now storing that common gpu buffer * in an opaque pointer per pbvh. See T47637. */ struct GridCommonGPUBuffer *grid_common_gpu_buffer; + /* The ccgdm is required for CD_ORIGINDEX lookup in vertex paint + multires */ + struct CCGDerivedMesh *ccgdm; /* Only used during BVH build and update, * don't need to remain valid after */ @@ -183,11 +185,21 @@ void pbvh_grow_nodes(PBVH *bvh, int totnode); bool ray_face_intersection_quad( const float ray_start[3], const float ray_normal[3], const float *t0, const float *t1, const float *t2, const float *t3, - float *r_dist); + float *depth); bool ray_face_intersection_tri( const float ray_start[3], const float ray_normal[3], const float *t0, const float *t1, const float *t2, - float *r_dist); + float *depth); + +bool ray_face_nearest_quad( + const float ray_start[3], const float ray_normal[3], + const float *t0, const float *t1, const float *t2, const float *t3, + float *r_depth, float *r_dist_sq); +bool ray_face_nearest_tri( + const float ray_start[3], const float ray_normal[3], + const float *t0, const float *t1, const float *t2, + float *r_depth, float *r_dist_sq); + void pbvh_update_BB_redraw(PBVH *bvh, PBVHNode **nodes, int totnode, int flag); /* pbvh_bmesh.c */ @@ -195,6 +207,10 @@ bool pbvh_bmesh_node_raycast( PBVHNode *node, const float ray_start[3], const float ray_normal[3], float *dist, bool use_original); +bool pbvh_bmesh_node_nearest_to_ray( + PBVHNode *node, const float ray_start[3], + const float ray_normal[3], float *depth, float *dist_sq, + bool use_original); void pbvh_bmesh_normals_update(PBVHNode **nodes, int totnode); diff --git a/source/blender/blenkernel/intern/pointcache.c b/source/blender/blenkernel/intern/pointcache.c index 0b152534dcb..5ec3156f4df 100644 --- a/source/blender/blenkernel/intern/pointcache.c +++ b/source/blender/blenkernel/intern/pointcache.c @@ -3449,7 +3449,7 @@ void BKE_ptcache_free_list(ListBase *ptcaches) } } -static PointCache *ptcache_copy(PointCache *cache, bool copy_data) +static PointCache *ptcache_copy(PointCache *cache, const bool copy_data) { PointCache *ncache; @@ -3492,14 +3492,15 @@ static PointCache *ptcache_copy(PointCache *cache, bool copy_data) } /* returns first point cache */ -PointCache *BKE_ptcache_copy_list(ListBase *ptcaches_new, const ListBase *ptcaches_old, bool copy_data) +PointCache *BKE_ptcache_copy_list(ListBase *ptcaches_new, const ListBase *ptcaches_old, const int flag) { PointCache *cache = ptcaches_old->first; BLI_listbase_clear(ptcaches_new); - for (; cache; cache=cache->next) - BLI_addtail(ptcaches_new, ptcache_copy(cache, copy_data)); + for (; cache; cache=cache->next) { + BLI_addtail(ptcaches_new, ptcache_copy(cache, (flag & LIB_ID_COPY_CACHES) != 0)); + } return ptcaches_new->first; } @@ -3618,7 +3619,13 @@ void BKE_ptcache_bake(PTCacheBaker *baker) psys_get_pointcache_start_end(scene, pid->calldata, &cache->startframe, &cache->endframe); } - if (((cache->flag & PTCACHE_BAKED) == 0) && (render || bake)) { + // XXX workaround for regression inroduced in ee3fadd, needs looking into + if (pid->type == PTCACHE_TYPE_RIGIDBODY) { + if ((cache->flag & PTCACHE_REDO_NEEDED || (cache->flag & PTCACHE_SIMULATION_VALID)==0) && (render || bake)) { + BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0); + } + } + else if (((cache->flag & PTCACHE_BAKED) == 0) && (render || bake)) { BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0); } diff --git a/source/blender/blenkernel/intern/rigidbody.c b/source/blender/blenkernel/intern/rigidbody.c index 6056515af9a..f75b14579c7 100644 --- a/source/blender/blenkernel/intern/rigidbody.c +++ b/source/blender/blenkernel/intern/rigidbody.c @@ -183,7 +183,7 @@ void BKE_rigidbody_free_constraint(Object *ob) * be added to relevant groups later... */ -RigidBodyOb *BKE_rigidbody_copy_object(const Object *ob) +RigidBodyOb *BKE_rigidbody_copy_object(const Object *ob, const int UNUSED(flag)) { RigidBodyOb *rboN = NULL; @@ -203,7 +203,7 @@ RigidBodyOb *BKE_rigidbody_copy_object(const Object *ob) return rboN; } -RigidBodyCon *BKE_rigidbody_copy_constraint(const Object *ob) +RigidBodyCon *BKE_rigidbody_copy_constraint(const Object *ob, const int UNUSED(flag)) { RigidBodyCon *rbcN = NULL; @@ -290,8 +290,6 @@ static rbCollisionShape *rigidbody_get_shape_trimesh_from_mesh(Object *ob) if (dm == NULL) return NULL; - DM_ensure_looptri(dm); - mvert = dm->getVertArray(dm); totvert = dm->getNumVerts(dm); looptri = dm->getLoopTriArray(dm); @@ -523,8 +521,6 @@ void BKE_rigidbody_calc_volume(Object *ob, float *r_vol) if (dm == NULL) return; - DM_ensure_looptri(dm); - mvert = dm->getVertArray(dm); totvert = dm->getNumVerts(dm); lt = dm->getLoopTriArray(dm); @@ -608,8 +604,6 @@ void BKE_rigidbody_calc_center_of_mass(Object *ob, float r_center[3]) if (dm == NULL) return; - DM_ensure_looptri(dm); - mvert = dm->getVertArray(dm); totvert = dm->getNumVerts(dm); looptri = dm->getLoopTriArray(dm); @@ -944,24 +938,26 @@ RigidBodyWorld *BKE_rigidbody_create_world(Scene *scene) return rbw; } -RigidBodyWorld *BKE_rigidbody_world_copy(RigidBodyWorld *rbw) +RigidBodyWorld *BKE_rigidbody_world_copy(RigidBodyWorld *rbw, const int flag) { - RigidBodyWorld *rbwn = MEM_dupallocN(rbw); + RigidBodyWorld *rbw_copy = MEM_dupallocN(rbw); - if (rbw->effector_weights) - rbwn->effector_weights = MEM_dupallocN(rbw->effector_weights); - if (rbwn->group) - id_us_plus(&rbwn->group->id); - if (rbwn->constraints) - id_us_plus(&rbwn->constraints->id); + if (rbw->effector_weights) { + rbw_copy->effector_weights = MEM_dupallocN(rbw->effector_weights); + } + if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) { + id_us_plus((ID *)rbw_copy->group); + id_us_plus((ID *)rbw_copy->constraints); + } - rbwn->pointcache = BKE_ptcache_copy_list(&rbwn->ptcaches, &rbw->ptcaches, false); + /* XXX Never copy caches here? */ + rbw_copy->pointcache = BKE_ptcache_copy_list(&rbw_copy->ptcaches, &rbw->ptcaches, flag & ~LIB_ID_COPY_CACHES); - rbwn->objects = NULL; - rbwn->physics_world = NULL; - rbwn->numbodies = 0; + rbw_copy->objects = NULL; + rbw_copy->physics_world = NULL; + rbw_copy->numbodies = 0; - return rbwn; + return rbw_copy; } void BKE_rigidbody_world_groups_relink(RigidBodyWorld *rbw) @@ -1226,7 +1222,7 @@ static void rigidbody_update_sim_world(Scene *scene, RigidBodyWorld *rbw) rigidbody_update_ob_array(rbw); } -static void rigidbody_update_sim_ob(Scene *scene, RigidBodyWorld *rbw, Object *ob, RigidBodyOb *rbo) +static void rigidbody_update_sim_ob(const struct EvaluationContext *eval_ctx, Scene *scene, RigidBodyWorld *rbw, Object *ob, RigidBodyOb *rbo) { float loc[3]; float rot[4]; @@ -1274,7 +1270,7 @@ static void rigidbody_update_sim_ob(Scene *scene, RigidBodyWorld *rbw, Object *o ListBase *effectors; /* get effectors present in the group specified by effector_weights */ - effectors = pdInitEffectors(scene, ob, NULL, effector_weights, true); + effectors = pdInitEffectors(eval_ctx, scene, ob, NULL, effector_weights, true); if (effectors) { float eff_force[3] = {0.0f, 0.0f, 0.0f}; float eff_loc[3], eff_vel[3]; @@ -1315,7 +1311,7 @@ static void rigidbody_update_sim_ob(Scene *scene, RigidBodyWorld *rbw, Object *o * * \param rebuild Rebuild entire simulation */ -static void rigidbody_update_simulation(Scene *scene, RigidBodyWorld *rbw, bool rebuild) +static void rigidbody_update_simulation(const struct EvaluationContext *eval_ctx, Scene *scene, RigidBodyWorld *rbw, bool rebuild) { GroupObject *go; @@ -1352,7 +1348,7 @@ static void rigidbody_update_simulation(Scene *scene, RigidBodyWorld *rbw, bool /* validate that we've got valid object set up here... */ RigidBodyOb *rbo = ob->rigidbody_object; /* update transformation matrix of the object so we don't get a frame of lag for simple animations */ - BKE_object_where_is_calc(scene, ob); + BKE_object_where_is_calc(eval_ctx, scene, ob); if (rbo == NULL) { /* Since this object is included in the sim group but doesn't have @@ -1386,7 +1382,7 @@ static void rigidbody_update_simulation(Scene *scene, RigidBodyWorld *rbw, bool } /* update simulation object... */ - rigidbody_update_sim_ob(scene, rbw, ob, rbo); + rigidbody_update_sim_ob(eval_ctx, scene, rbw, ob, rbo); } } @@ -1400,7 +1396,7 @@ static void rigidbody_update_simulation(Scene *scene, RigidBodyWorld *rbw, bool /* validate that we've got valid object set up here... */ RigidBodyCon *rbc = ob->rigidbody_constraint; /* update transformation matrix of the object so we don't get a frame of lag for simple animations */ - BKE_object_where_is_calc(scene, ob); + BKE_object_where_is_calc(eval_ctx, scene, ob); if (rbc == NULL) { /* Since this object is included in the group but doesn't have @@ -1485,24 +1481,60 @@ void BKE_rigidbody_sync_transforms(RigidBodyWorld *rbw, Object *ob, float ctime) void BKE_rigidbody_aftertrans_update(Object *ob, float loc[3], float rot[3], float quat[4], float rotAxis[3], float rotAngle) { RigidBodyOb *rbo = ob->rigidbody_object; + bool correct_delta = !(rbo->flag & RBO_FLAG_KINEMATIC || rbo->type == RBO_TYPE_PASSIVE); /* return rigid body and object to their initial states */ copy_v3_v3(rbo->pos, ob->loc); copy_v3_v3(ob->loc, loc); + if (correct_delta) { + add_v3_v3(rbo->pos, ob->dloc); + } + if (ob->rotmode > 0) { - eulO_to_quat(rbo->orn, ob->rot, ob->rotmode); + float qt[4]; + eulO_to_quat(qt, ob->rot, ob->rotmode); + + if (correct_delta) { + float dquat[4]; + eulO_to_quat(dquat, ob->drot, ob->rotmode); + + mul_qt_qtqt(rbo->orn, dquat, qt); + } + else { + copy_qt_qt(rbo->orn, qt); + } + copy_v3_v3(ob->rot, rot); } else if (ob->rotmode == ROT_MODE_AXISANGLE) { - axis_angle_to_quat(rbo->orn, ob->rotAxis, ob->rotAngle); + float qt[4]; + axis_angle_to_quat(qt, ob->rotAxis, ob->rotAngle); + + if (correct_delta) { + float dquat[4]; + axis_angle_to_quat(dquat, ob->drotAxis, ob->drotAngle); + + mul_qt_qtqt(rbo->orn, dquat, qt); + } + else { + copy_qt_qt(rbo->orn, qt); + } + copy_v3_v3(ob->rotAxis, rotAxis); ob->rotAngle = rotAngle; } else { - copy_qt_qt(rbo->orn, ob->quat); + if (correct_delta) { + mul_qt_qtqt(rbo->orn, ob->dquat, ob->quat); + } + else { + copy_qt_qt(rbo->orn, ob->quat); + } + copy_qt_qt(ob->quat, quat); } + if (rbo->physics_object) { /* allow passive objects to return to original transform */ if (rbo->type == RBO_TYPE_PASSIVE) @@ -1514,15 +1546,16 @@ void BKE_rigidbody_aftertrans_update(Object *ob, float loc[3], float rot[3], flo void BKE_rigidbody_cache_reset(RigidBodyWorld *rbw) { - if (rbw) + if (rbw) { rbw->pointcache->flag |= PTCACHE_OUTDATED; + } } /* ------------------ */ /* Rebuild rigid body world */ /* NOTE: this needs to be called before frame update to work correctly */ -void BKE_rigidbody_rebuild_world(Scene *scene, float ctime) +void BKE_rigidbody_rebuild_world(const struct EvaluationContext *eval_ctx, Scene *scene, float ctime) { RigidBodyWorld *rbw = scene->rigidbody_world; PointCache *cache; @@ -1541,7 +1574,7 @@ void BKE_rigidbody_rebuild_world(Scene *scene, float ctime) if (ctime == startframe + 1 && rbw->ltime == startframe) { if (cache->flag & PTCACHE_OUTDATED) { BKE_ptcache_id_reset(scene, &pid, PTCACHE_RESET_OUTDATED); - rigidbody_update_simulation(scene, rbw, true); + rigidbody_update_simulation(eval_ctx, scene, rbw, true); BKE_ptcache_validate(cache, (int)ctime); cache->last_exact = 0; cache->flag &= ~PTCACHE_REDO_NEEDED; @@ -1550,7 +1583,7 @@ void BKE_rigidbody_rebuild_world(Scene *scene, float ctime) } /* Run RigidBody simulation for the specified physics world */ -void BKE_rigidbody_do_simulation(Scene *scene, float ctime) +void BKE_rigidbody_do_simulation(const struct EvaluationContext *eval_ctx, Scene *scene, float ctime) { float timestep; RigidBodyWorld *rbw = scene->rigidbody_world; @@ -1581,7 +1614,7 @@ void BKE_rigidbody_do_simulation(Scene *scene, float ctime) // RB_TODO deal with interpolated, old and baked results bool can_simulate = (ctime == rbw->ltime + 1) && !(cache->flag & PTCACHE_BAKED); - if (BKE_ptcache_read(&pid, ctime, can_simulate)) { + if (BKE_ptcache_read(&pid, ctime, can_simulate) == PTCACHE_READ_EXACT) { BKE_ptcache_validate(cache, (int)ctime); rbw->ltime = ctime; return; @@ -1595,7 +1628,7 @@ void BKE_rigidbody_do_simulation(Scene *scene, float ctime) } /* update and validate simulation */ - rigidbody_update_simulation(scene, rbw, false); + rigidbody_update_simulation(eval_ctx, scene, rbw, false); /* calculate how much time elapsed since last step in seconds */ timestep = 1.0f / (float)FPS * (ctime - rbw->ltime) * rbw->time_scale; @@ -1621,13 +1654,13 @@ void BKE_rigidbody_do_simulation(Scene *scene, float ctime) # pragma GCC diagnostic ignored "-Wunused-parameter" #endif -struct RigidBodyOb *BKE_rigidbody_copy_object(const Object *ob) { return NULL; } -struct RigidBodyCon *BKE_rigidbody_copy_constraint(const Object *ob) { return NULL; } +struct RigidBodyOb *BKE_rigidbody_copy_object(const Object *ob, const int flag) { return NULL; } +struct RigidBodyCon *BKE_rigidbody_copy_constraint(const Object *ob, const int flag) { return NULL; } void BKE_rigidbody_validate_sim_world(Scene *scene, RigidBodyWorld *rbw, bool rebuild) {} void BKE_rigidbody_calc_volume(Object *ob, float *r_vol) { if (r_vol) *r_vol = 0.0f; } void BKE_rigidbody_calc_center_of_mass(Object *ob, float r_center[3]) { zero_v3(r_center); } struct RigidBodyWorld *BKE_rigidbody_create_world(Scene *scene) { return NULL; } -struct RigidBodyWorld *BKE_rigidbody_world_copy(RigidBodyWorld *rbw) { return NULL; } +struct RigidBodyWorld *BKE_rigidbody_world_copy(RigidBodyWorld *rbw, const int flag) { return NULL; } void BKE_rigidbody_world_groups_relink(struct RigidBodyWorld *rbw) {} void BKE_rigidbody_world_id_loop(struct RigidBodyWorld *rbw, RigidbodyWorldIDFunc func, void *userdata) {} struct RigidBodyOb *BKE_rigidbody_create_object(Scene *scene, Object *ob, short type) { return NULL; } @@ -1639,8 +1672,8 @@ void BKE_rigidbody_sync_transforms(RigidBodyWorld *rbw, Object *ob, float ctime) void BKE_rigidbody_aftertrans_update(Object *ob, float loc[3], float rot[3], float quat[4], float rotAxis[3], float rotAngle) {} bool BKE_rigidbody_check_sim_running(RigidBodyWorld *rbw, float ctime) { return false; } void BKE_rigidbody_cache_reset(RigidBodyWorld *rbw) {} -void BKE_rigidbody_rebuild_world(Scene *scene, float ctime) {} -void BKE_rigidbody_do_simulation(Scene *scene, float ctime) {} +void BKE_rigidbody_rebuild_world(const struct EvaluationContext *eval_ctx, Scene *scene, float ctime) {} +void BKE_rigidbody_do_simulation(const struct EvaluationContext *eval_ctx, Scene *scene, float ctime) {} #ifdef __GNUC__ # pragma GCC diagnostic pop @@ -1651,7 +1684,7 @@ void BKE_rigidbody_do_simulation(Scene *scene, float ctime) {} /* -------------------- */ /* Depsgraph evaluation */ -void BKE_rigidbody_rebuild_sim(struct EvaluationContext *UNUSED(eval_ctx), +void BKE_rigidbody_rebuild_sim(const struct EvaluationContext *eval_ctx, Scene *scene) { float ctime = BKE_scene_frame_get(scene); @@ -1662,11 +1695,11 @@ void BKE_rigidbody_rebuild_sim(struct EvaluationContext *UNUSED(eval_ctx), /* rebuild sim data (i.e. after resetting to start of timeline) */ if (BKE_scene_check_rigidbody_active(scene)) { - BKE_rigidbody_rebuild_world(scene, ctime); + BKE_rigidbody_rebuild_world(eval_ctx, scene, ctime); } } -void BKE_rigidbody_eval_simulation(struct EvaluationContext *UNUSED(eval_ctx), +void BKE_rigidbody_eval_simulation(const struct EvaluationContext *eval_ctx, Scene *scene) { float ctime = BKE_scene_frame_get(scene); @@ -1677,11 +1710,11 @@ void BKE_rigidbody_eval_simulation(struct EvaluationContext *UNUSED(eval_ctx), /* evaluate rigidbody sim */ if (BKE_scene_check_rigidbody_active(scene)) { - BKE_rigidbody_do_simulation(scene, ctime); + BKE_rigidbody_do_simulation(eval_ctx, scene, ctime); } } -void BKE_rigidbody_object_sync_transforms(struct EvaluationContext *UNUSED(eval_ctx), +void BKE_rigidbody_object_sync_transforms(const struct EvaluationContext *UNUSED(eval_ctx), Scene *scene, Object *ob) { diff --git a/source/blender/blenkernel/intern/sca.c b/source/blender/blenkernel/intern/sca.c index 19d646daf9f..fb81ed4d47f 100644 --- a/source/blender/blenkernel/intern/sca.c +++ b/source/blender/blenkernel/intern/sca.c @@ -72,7 +72,7 @@ void free_sensors(ListBase *lb) } } -bSensor *copy_sensor(bSensor *sens) +bSensor *copy_sensor(bSensor *sens, const int UNUSED(flag)) { bSensor *sensn; @@ -87,14 +87,14 @@ bSensor *copy_sensor(bSensor *sens) return sensn; } -void copy_sensors(ListBase *lbn, const ListBase *lbo) +void copy_sensors(ListBase *lbn, const ListBase *lbo, const int flag) { bSensor *sens, *sensn; lbn->first= lbn->last= NULL; sens= lbo->first; while (sens) { - sensn= copy_sensor(sens); + sensn= copy_sensor(sens, flag); BLI_addtail(lbn, sensn); sens= sens->next; } @@ -234,7 +234,7 @@ void free_controllers(ListBase *lb) } } -bController *copy_controller(bController *cont) +bController *copy_controller(bController *cont, const int UNUSED(flag)) { bController *contn; @@ -251,14 +251,14 @@ bController *copy_controller(bController *cont) return contn; } -void copy_controllers(ListBase *lbn, const ListBase *lbo) +void copy_controllers(ListBase *lbn, const ListBase *lbo, const int flag) { bController *cont, *contn; lbn->first= lbn->last= NULL; cont= lbo->first; while (cont) { - contn= copy_controller(cont); + contn= copy_controller(cont, flag); BLI_addtail(lbn, contn); cont= cont->next; } @@ -359,7 +359,7 @@ void free_actuators(ListBase *lb) } } -bActuator *copy_actuator(bActuator *act) +bActuator *copy_actuator(bActuator *act, const int flag) { bActuator *actn; @@ -374,29 +374,31 @@ bActuator *copy_actuator(bActuator *act) case ACT_SHAPEACTION: { bActionActuator *aa = (bActionActuator *)act->data; - if (aa->act) + if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) { id_us_plus((ID *)aa->act); + } break; } case ACT_SOUND: { bSoundActuator *sa = (bSoundActuator *)act->data; - if (sa->sound) + if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) { id_us_plus((ID *)sa->sound); + } break; } } return actn; } -void copy_actuators(ListBase *lbn, const ListBase *lbo) +void copy_actuators(ListBase *lbn, const ListBase *lbo, const int flag) { bActuator *act, *actn; lbn->first= lbn->last= NULL; act= lbo->first; while (act) { - actn= copy_actuator(act); + actn= copy_actuator(act, flag); BLI_addtail(lbn, actn); act= act->next; } @@ -783,11 +785,11 @@ void BKE_sca_logic_links_remap(Main *bmain, Object *ob_old, Object *ob_new) * Handle the copying of logic data into a new object, including internal logic links update. * External links (links between logic bricks of different objects) must be handled separately. */ -void BKE_sca_logic_copy(Object *ob_new, const Object *ob) +void BKE_sca_logic_copy(Object *ob_new, const Object *ob, const int flag) { - copy_sensors(&ob_new->sensors, &ob->sensors); - copy_controllers(&ob_new->controllers, &ob->controllers); - copy_actuators(&ob_new->actuators, &ob->actuators); + copy_sensors(&ob_new->sensors, &ob->sensors, flag); + copy_controllers(&ob_new->controllers, &ob->controllers, flag); + copy_actuators(&ob_new->actuators, &ob->actuators, flag); for (bSensor *sens = ob_new->sensors.first; sens; sens = sens->next) { if (sens->flag & SENS_NEW) { diff --git a/source/blender/blenkernel/intern/scene.c b/source/blender/blenkernel/intern/scene.c index f54a02f033a..d36f128cef1 100644 --- a/source/blender/blenkernel/intern/scene.c +++ b/source/blender/blenkernel/intern/scene.c @@ -131,17 +131,6 @@ void free_avicodecdata(AviCodecData *acd) } } -void free_qtcodecdata(QuicktimeCodecData *qcd) -{ - if (qcd) { - if (qcd->cdParms) { - MEM_freeN(qcd->cdParms); - qcd->cdParms = NULL; - qcd->cdSize = 0; - } - } -} - static void remove_sequencer_fcurves(Scene *sce) { AnimData *adt = BKE_animdata_from_id(&sce->id); @@ -161,314 +150,415 @@ static void remove_sequencer_fcurves(Scene *sce) } /* copy SceneCollection tree but keep pointing to the same objects */ -static void scene_collection_copy(SceneCollection *scn, SceneCollection *sc) +static void scene_collection_copy(SceneCollection *sc_dst, SceneCollection *sc_src, const int flag) { - BLI_duplicatelist(&scn->objects, &sc->objects); - for (LinkData *link = scn->objects.first; link; link = link->next) { - id_us_plus(link->data); + BLI_duplicatelist(&sc_dst->objects, &sc_src->objects); + if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) { + for (LinkData *link = sc_dst->objects.first; link; link = link->next) { + id_us_plus(link->data); + } } - BLI_duplicatelist(&scn->filter_objects, &sc->filter_objects); - for (LinkData *link = scn->filter_objects.first; link; link = link->next) { - id_us_plus(link->data); + BLI_duplicatelist(&sc_dst->filter_objects, &sc_src->filter_objects); + if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) { + for (LinkData *link = sc_dst->filter_objects.first; link; link = link->next) { + id_us_plus(link->data); + } } - BLI_duplicatelist(&scn->scene_collections, &sc->scene_collections); - SceneCollection *nscn = scn->scene_collections.first; /* nested SceneCollection new */ - for (SceneCollection *nsc = sc->scene_collections.first; nsc; nsc = nsc->next) { - scene_collection_copy(nscn, nsc); - nscn = nscn->next; + BLI_duplicatelist(&sc_dst->scene_collections, &sc_src->scene_collections); + for (SceneCollection *nsc_src = sc_src->scene_collections.first, *nsc_dst = sc_dst->scene_collections.first; + nsc_src; + nsc_src = nsc_src->next, nsc_dst = nsc_dst->next) + { + scene_collection_copy(nsc_dst, nsc_src, flag); } } /* Find the equivalent SceneCollection in the new tree */ -static SceneCollection *scene_collection_from_new_tree(SceneCollection *sc_reference, SceneCollection *scn, SceneCollection *sc) +static SceneCollection *scene_collection_from_new_tree(SceneCollection *sc_reference, SceneCollection *sc_dst, SceneCollection *sc_src) { - if (sc == sc_reference) { - return scn; + if (sc_src == sc_reference) { + return sc_dst; } - SceneCollection *nscn = scn->scene_collections.first; /* nested master collection new */ - for (SceneCollection *nsc = sc->scene_collections.first; nsc; nsc = nsc->next) { - - SceneCollection *found = scene_collection_from_new_tree(sc_reference, nscn, nsc); - if (found) { + for (SceneCollection *nsc_src = sc_src->scene_collections.first, *nsc_dst = sc_dst->scene_collections.first; + nsc_src; + nsc_src = nsc_src->next, nsc_dst = nsc_dst->next) + { + SceneCollection *found = scene_collection_from_new_tree(sc_reference, nsc_dst, nsc_src); + if (found != NULL) { return found; } - nscn = nscn->next; } return NULL; } -/* recreate the LayerCollection tree */ -static void layer_collections_recreate(SceneLayer *sl, ListBase *lb, SceneCollection *mcn, SceneCollection *mc) +static void layer_collections_sync_flags(ListBase *layer_collections_dst, const ListBase *layer_collections_src) { - for (LayerCollection *lc = lb->first; lc; lc = lc->next) { + LayerCollection *layer_collection_dst = (LayerCollection *)layer_collections_dst->first; + const LayerCollection *layer_collection_src = (const LayerCollection *)layer_collections_src->first; + while (layer_collection_dst != NULL) { + layer_collection_dst->flag = layer_collection_src->flag; + layer_collections_sync_flags(&layer_collection_dst->layer_collections, + &layer_collection_src->layer_collections); + /* TODO(sergey/dfelinto): Overrides. */ + layer_collection_dst = layer_collection_dst->next; + layer_collection_src = layer_collection_src->next; + } +} - SceneCollection *sc = scene_collection_from_new_tree(lc->scene_collection, mcn, mc); - BLI_assert(sc); - /* instead of syncronizing both trees we simply re-create it */ - BKE_collection_link(sl, sc); +/* recreate the LayerCollection tree */ +static void layer_collections_recreate( + SceneLayer *sl_dst, ListBase *lb_src, SceneCollection *mc_dst, SceneCollection *mc_src) +{ + for (LayerCollection *lc_src = lb_src->first; lc_src; lc_src = lc_src->next) { + SceneCollection *sc_dst = scene_collection_from_new_tree(lc_src->scene_collection, mc_dst, mc_src); + BLI_assert(sc_dst); + + /* instead of synchronizing both trees we simply re-create it */ + BKE_collection_link(sl_dst, sc_dst); } } -Scene *BKE_scene_copy(Main *bmain, Scene *sce, int type) +/** + * Only copy internal data of Scene ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_scene_copy_data(Main *bmain, Scene *sce_dst, const Scene *sce_src, const int flag) { - Scene *scen; - SceneRenderLayer *srl, *new_srl; - FreestyleLineSet *lineset; - ToolSettings *ts; - BaseLegacy *legacy_base, *olegacy_base; - - if (type == SCE_COPY_EMPTY) { - ListBase rl, rv; - scen = BKE_scene_add(bmain, sce->id.name + 2); - - rl = scen->r.layers; - rv = scen->r.views; - curvemapping_free_data(&scen->r.mblur_shutter_curve); - scen->r = sce->r; - scen->r.layers = rl; - scen->r.actlay = 0; - scen->r.views = rv; - scen->unit = sce->unit; - scen->physics_settings = sce->physics_settings; - scen->gm = sce->gm; - scen->audio = sce->audio; + /* We never handle usercount here for own data. */ + const int flag_subdata = flag | LIB_ID_CREATE_NO_USER_REFCOUNT; - if (sce->id.properties) - scen->id.properties = IDP_CopyProperty(sce->id.properties); + sce_dst->ed = NULL; + sce_dst->theDag = NULL; + sce_dst->depsgraph_legacy = NULL; + sce_dst->obedit = NULL; + sce_dst->fps_info = NULL; - MEM_freeN(scen->toolsettings); - BKE_sound_destroy_scene(scen); + BLI_duplicatelist(&(sce_dst->base), &(sce_src->base)); + for (BaseLegacy *base_dst = sce_dst->base.first, *base_src = sce_src->base.first; + base_dst; + base_dst = base_dst->next, base_src = base_src->next) + { + if (base_src == sce_src->basact) { + sce_dst->basact = base_dst; + } } - else { - scen = BKE_libblock_copy(bmain, &sce->id); - BLI_duplicatelist(&(scen->base), &(sce->base)); - - id_us_plus((ID *)scen->world); - id_us_plus((ID *)scen->set); - /* id_us_plus((ID *)scen->gm.dome.warptext); */ /* XXX Not refcounted? see readfile.c */ - scen->ed = NULL; - scen->theDag = NULL; - scen->depsgraph = NULL; - scen->obedit = NULL; - scen->fps_info = NULL; + /* layers and collections */ + sce_dst->collection = MEM_dupallocN(sce_src->collection); + SceneCollection *mc_src = BKE_collection_master(sce_src); + SceneCollection *mc_dst = BKE_collection_master(sce_dst); - if (sce->rigidbody_world) - scen->rigidbody_world = BKE_rigidbody_world_copy(sce->rigidbody_world); + /* recursively creates a new SceneCollection tree */ + scene_collection_copy(mc_dst, mc_src, flag_subdata); - BLI_duplicatelist(&(scen->markers), &(sce->markers)); - BLI_duplicatelist(&(scen->r.layers), &(sce->r.layers)); - BLI_duplicatelist(&(scen->r.views), &(sce->r.views)); - BKE_keyingsets_copy(&(scen->keyingsets), &(sce->keyingsets)); - - if (sce->nodetree) { - /* ID's are managed on both copy and switch */ - scen->nodetree = ntreeCopyTree(bmain, sce->nodetree); - BKE_libblock_relink_ex(bmain, scen->nodetree, &sce->id, &scen->id, false); - } + IDPropertyTemplate val = {0}; + BLI_duplicatelist(&sce_dst->render_layers, &sce_src->render_layers); + for (SceneLayer *sl_src = sce_src->render_layers.first, *sl_dst = sce_dst->render_layers.first; + sl_src; + sl_src = sl_src->next, sl_dst = sl_dst->next) + { + sl_dst->stats = NULL; + sl_dst->properties_evaluated = NULL; + sl_dst->properties = IDP_New(IDP_GROUP, &val, ROOT_PROP); + IDP_MergeGroup_ex(sl_dst->properties, sl_src->properties, true, flag_subdata); + + /* we start fresh with no overrides and no visibility flags set + * instead of syncing both trees we simply unlink and relink the scene collection */ + BLI_listbase_clear(&sl_dst->layer_collections); + BLI_listbase_clear(&sl_dst->object_bases); + BLI_listbase_clear(&sl_dst->drawdata); + + layer_collections_recreate(sl_dst, &sl_src->layer_collections, mc_dst, mc_src); + + /* Now we handle the syncing for visibility, selectability, ... */ + layer_collections_sync_flags(&sl_dst->layer_collections, &sl_src->layer_collections); + + Object *active_ob = OBACT_NEW(sl_src); + for (Base *base_src = sl_src->object_bases.first, *base_dst = sl_dst->object_bases.first; + base_src; + base_src = base_src->next, base_dst = base_dst->next) + { + base_dst->flag = base_src->flag; + base_dst->flag_legacy = base_src->flag_legacy; - olegacy_base = sce->base.first; - legacy_base = scen->base.first; - while (legacy_base) { - id_us_plus(&legacy_base->object->id); - if (olegacy_base == sce->basact) scen->basact = legacy_base; - - olegacy_base = olegacy_base->next; - legacy_base = legacy_base->next; + if (base_dst->object == active_ob) { + sl_dst->basact = base_dst; + } } + } - /* copy action and remove animation used by sequencer */ - BKE_animdata_copy_id_action(&scen->id, false); + sce_dst->collection_properties = IDP_New(IDP_GROUP, &val, ROOT_PROP); + if (sce_src->collection_properties) { + IDP_MergeGroup_ex(sce_dst->collection_properties, sce_src->collection_properties, true, flag_subdata); + } + sce_dst->layer_properties = IDP_New(IDP_GROUP, &val, ROOT_PROP); + if (sce_src->layer_properties) { + IDP_MergeGroup_ex(sce_dst->layer_properties, sce_src->layer_properties, true, flag_subdata); + } - if (type != SCE_COPY_FULL) - remove_sequencer_fcurves(scen); + BLI_duplicatelist(&(sce_dst->markers), &(sce_src->markers)); + BLI_duplicatelist(&(sce_dst->r.layers), &(sce_src->r.layers)); + BLI_duplicatelist(&(sce_dst->r.views), &(sce_src->r.views)); + BKE_keyingsets_copy(&(sce_dst->keyingsets), &(sce_src->keyingsets)); - /* copy Freestyle settings */ - new_srl = scen->r.layers.first; - for (srl = sce->r.layers.first; srl; srl = srl->next) { - if (new_srl->prop != NULL) { - new_srl->prop = IDP_CopyProperty(new_srl->prop); - } - BKE_freestyle_config_copy(&new_srl->freestyleConfig, &srl->freestyleConfig); - if (type == SCE_COPY_FULL) { - for (lineset = new_srl->freestyleConfig.linesets.first; lineset; lineset = lineset->next) { - if (lineset->linestyle) { - id_us_plus((ID *)lineset->linestyle); - lineset->linestyle = BKE_linestyle_copy(bmain, lineset->linestyle); - } - } - } - new_srl = new_srl->next; - } + if (sce_src->nodetree) { + BKE_id_copy_ex(bmain, (ID *)sce_src->nodetree, (ID **)&sce_dst->nodetree, flag, false); + BKE_libblock_relink_ex(bmain, sce_dst->nodetree, (void *)(&sce_src->id), &sce_dst->id, false); + } - /* layers and collections */ - scen->collection = MEM_dupallocN(sce->collection); - SceneCollection *mcn = BKE_collection_master(scen); - SceneCollection *mc = BKE_collection_master(sce); - - /* recursively creates a new SceneCollection tree */ - scene_collection_copy(mcn, mc); - - IDPropertyTemplate val = {0}; - BLI_duplicatelist(&scen->render_layers, &sce->render_layers); - SceneLayer *new_sl = scen->render_layers.first; - for (SceneLayer *sl = sce->render_layers.first; sl; sl = sl->next) { - new_sl->stats = NULL; - new_sl->properties_evaluated = NULL; - new_sl->properties = IDP_New(IDP_GROUP, &val, ROOT_PROP); - IDP_MergeGroup(new_sl->properties, sl->properties, true); - - /* we start fresh with no overrides and no visibility flags set - * instead of syncing both trees we simply unlink and relink the scene collection */ - BLI_listbase_clear(&new_sl->layer_collections); - BLI_listbase_clear(&new_sl->object_bases); - BLI_listbase_clear(&new_sl->drawdata); - layer_collections_recreate(new_sl, &sl->layer_collections, mcn, mc); - - Object *active_ob = OBACT_NEW; - Base *new_base = new_sl->object_bases.first; - for (Base *base = sl->object_bases.first; base; base = base->next) { - new_base->flag = base->flag; - new_base->flag_legacy = base->flag_legacy; - - if (new_base->object == active_ob) { - new_sl->basact = new_base; - } + if (sce_src->rigidbody_world) { + sce_dst->rigidbody_world = BKE_rigidbody_world_copy(sce_src->rigidbody_world, flag_subdata); + } - new_base = new_base->next; - } - new_sl = new_sl->next; + /* copy Freestyle settings */ + for (SceneRenderLayer *srl_dst = sce_dst->r.layers.first, *srl_src = sce_src->r.layers.first; + srl_src; + srl_dst = srl_dst->next, srl_src = srl_src->next) + { + if (srl_dst->prop != NULL) { + srl_dst->prop = IDP_CopyProperty_ex(srl_dst->prop, flag_subdata); } - - scen->collection_properties = IDP_New(IDP_GROUP, &val, ROOT_PROP); - scen->layer_properties = IDP_New(IDP_GROUP, &val, ROOT_PROP); + BKE_freestyle_config_copy(&srl_dst->freestyleConfig, &srl_src->freestyleConfig, flag_subdata); } /* copy color management settings */ - BKE_color_managed_display_settings_copy(&scen->display_settings, &sce->display_settings); - BKE_color_managed_view_settings_copy(&scen->view_settings, &sce->view_settings); - BKE_color_managed_colorspace_settings_copy(&scen->sequencer_colorspace_settings, &sce->sequencer_colorspace_settings); + BKE_color_managed_display_settings_copy(&sce_dst->display_settings, &sce_src->display_settings); + BKE_color_managed_view_settings_copy(&sce_dst->view_settings, &sce_src->view_settings); + BKE_color_managed_colorspace_settings_copy(&sce_dst->sequencer_colorspace_settings, &sce_src->sequencer_colorspace_settings); - BKE_color_managed_display_settings_copy(&scen->r.im_format.display_settings, &sce->r.im_format.display_settings); - BKE_color_managed_view_settings_copy(&scen->r.im_format.view_settings, &sce->r.im_format.view_settings); + BKE_color_managed_display_settings_copy(&sce_dst->r.im_format.display_settings, &sce_src->r.im_format.display_settings); + BKE_color_managed_view_settings_copy(&sce_dst->r.im_format.view_settings, &sce_src->r.im_format.view_settings); - BKE_color_managed_display_settings_copy(&scen->r.bake.im_format.display_settings, &sce->r.bake.im_format.display_settings); - BKE_color_managed_view_settings_copy(&scen->r.bake.im_format.view_settings, &sce->r.bake.im_format.view_settings); + BKE_color_managed_display_settings_copy(&sce_dst->r.bake.im_format.display_settings, &sce_src->r.bake.im_format.display_settings); + BKE_color_managed_view_settings_copy(&sce_dst->r.bake.im_format.view_settings, &sce_src->r.bake.im_format.view_settings); - curvemapping_copy_data(&scen->r.mblur_shutter_curve, &sce->r.mblur_shutter_curve); + curvemapping_copy_data(&sce_dst->r.mblur_shutter_curve, &sce_src->r.mblur_shutter_curve); /* tool settings */ - scen->toolsettings = MEM_dupallocN(sce->toolsettings); - - ts = scen->toolsettings; - if (ts) { + if (sce_dst->toolsettings != NULL) { + ToolSettings *ts = sce_dst->toolsettings = MEM_dupallocN(sce_dst->toolsettings); if (ts->vpaint) { ts->vpaint = MEM_dupallocN(ts->vpaint); - ts->vpaint->paintcursor = NULL; - ts->vpaint->vpaint_prev = NULL; - ts->vpaint->wpaint_prev = NULL; - BKE_paint_copy(&ts->vpaint->paint, &ts->vpaint->paint); + BKE_paint_copy(&ts->vpaint->paint, &ts->vpaint->paint, flag_subdata); } if (ts->wpaint) { ts->wpaint = MEM_dupallocN(ts->wpaint); - ts->wpaint->paintcursor = NULL; - ts->wpaint->vpaint_prev = NULL; - ts->wpaint->wpaint_prev = NULL; - BKE_paint_copy(&ts->wpaint->paint, &ts->wpaint->paint); + BKE_paint_copy(&ts->wpaint->paint, &ts->wpaint->paint, flag_subdata); } if (ts->sculpt) { ts->sculpt = MEM_dupallocN(ts->sculpt); - BKE_paint_copy(&ts->sculpt->paint, &ts->sculpt->paint); + BKE_paint_copy(&ts->sculpt->paint, &ts->sculpt->paint, flag_subdata); + } + if (ts->uvsculpt) { + ts->uvsculpt = MEM_dupallocN(ts->uvsculpt); + BKE_paint_copy(&ts->uvsculpt->paint, &ts->uvsculpt->paint, flag_subdata); } - BKE_paint_copy(&ts->imapaint.paint, &ts->imapaint.paint); + BKE_paint_copy(&ts->imapaint.paint, &ts->imapaint.paint, flag_subdata); ts->imapaint.paintcursor = NULL; - id_us_plus((ID *)ts->imapaint.stencil); ts->particle.paintcursor = NULL; - + ts->particle.scene = NULL; + ts->particle.object = NULL; + /* duplicate Grease Pencil Drawing Brushes */ BLI_listbase_clear(&ts->gp_brushes); - for (bGPDbrush *brush = sce->toolsettings->gp_brushes.first; brush; brush = brush->next) { + for (bGPDbrush *brush = sce_src->toolsettings->gp_brushes.first; brush; brush = brush->next) { bGPDbrush *newbrush = BKE_gpencil_brush_duplicate(brush); BLI_addtail(&ts->gp_brushes, newbrush); } - + /* duplicate Grease Pencil interpolation curve */ ts->gp_interpolate.custom_ipo = curvemapping_copy(ts->gp_interpolate.custom_ipo); } - + /* make a private copy of the avicodecdata */ - if (sce->r.avicodecdata) { - scen->r.avicodecdata = MEM_dupallocN(sce->r.avicodecdata); - scen->r.avicodecdata->lpFormat = MEM_dupallocN(scen->r.avicodecdata->lpFormat); - scen->r.avicodecdata->lpParms = MEM_dupallocN(scen->r.avicodecdata->lpParms); - } - - /* make a private copy of the qtcodecdata */ - if (sce->r.qtcodecdata) { - scen->r.qtcodecdata = MEM_dupallocN(sce->r.qtcodecdata); - scen->r.qtcodecdata->cdParms = MEM_dupallocN(scen->r.qtcodecdata->cdParms); + if (sce_src->r.avicodecdata) { + sce_dst->r.avicodecdata = MEM_dupallocN(sce_src->r.avicodecdata); + sce_dst->r.avicodecdata->lpFormat = MEM_dupallocN(sce_dst->r.avicodecdata->lpFormat); + sce_dst->r.avicodecdata->lpParms = MEM_dupallocN(sce_dst->r.avicodecdata->lpParms); } - - if (sce->r.ffcodecdata.properties) { /* intentionally check scen not sce. */ - scen->r.ffcodecdata.properties = IDP_CopyProperty(sce->r.ffcodecdata.properties); + + if (sce_src->r.ffcodecdata.properties) { /* intentionally check sce_dst not sce_src. */ /* XXX ??? comment outdated... */ + sce_dst->r.ffcodecdata.properties = IDP_CopyProperty_ex(sce_src->r.ffcodecdata.properties, flag_subdata); } - /* NOTE: part of SCE_COPY_LINK_DATA and SCE_COPY_FULL operations - * are done outside of blenkernel with ED_objects_single_users! */ + /* before scene copy */ + BKE_sound_create_scene(sce_dst); + + /* Copy sequencer, this is local data! */ + if (sce_src->ed) { + sce_dst->ed = MEM_callocN(sizeof(*sce_dst->ed), __func__); + sce_dst->ed->seqbasep = &sce_dst->ed->seqbase; + BKE_sequence_base_dupli_recursive( + sce_src, sce_dst, &sce_dst->ed->seqbase, &sce_src->ed->seqbase, SEQ_DUPE_ALL, flag_subdata); + } - /* camera */ - if (type == SCE_COPY_LINK_DATA || type == SCE_COPY_FULL) { - ID_NEW_REMAP(scen->camera); + if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0) { + BKE_previewimg_id_copy(&sce_dst->id, &sce_src->id); } - - /* before scene copy */ - BKE_sound_create_scene(scen); - - /* world */ - if (type == SCE_COPY_FULL) { - if (scen->world) { - id_us_plus((ID *)scen->world); - scen->world = BKE_world_copy(bmain, scen->world); - BKE_animdata_copy_id_action((ID *)scen->world, false); + else { + sce_dst->preview = NULL; + } +} + +Scene *BKE_scene_copy(Main *bmain, Scene *sce, int type) +{ + Scene *sce_copy; + + /* TODO this should/could most likely be replaced by call to more generic code at some point... + * But for now, let's keep it well isolated here. */ + if (type == SCE_COPY_EMPTY) { + ToolSettings *ts; + ListBase rl, rv; + + sce_copy = BKE_scene_add(bmain, sce->id.name + 2); + + rl = sce_copy->r.layers; + rv = sce_copy->r.views; + curvemapping_free_data(&sce_copy->r.mblur_shutter_curve); + sce_copy->r = sce->r; + sce_copy->r.layers = rl; + sce_copy->r.actlay = 0; + sce_copy->r.views = rv; + sce_copy->unit = sce->unit; + sce_copy->physics_settings = sce->physics_settings; + sce_copy->gm = sce->gm; + sce_copy->audio = sce->audio; + + if (sce->id.properties) + sce_copy->id.properties = IDP_CopyProperty(sce->id.properties); + + MEM_freeN(sce_copy->toolsettings); + BKE_sound_destroy_scene(sce_copy); + + /* copy color management settings */ + BKE_color_managed_display_settings_copy(&sce_copy->display_settings, &sce->display_settings); + BKE_color_managed_view_settings_copy(&sce_copy->view_settings, &sce->view_settings); + BKE_color_managed_colorspace_settings_copy(&sce_copy->sequencer_colorspace_settings, &sce->sequencer_colorspace_settings); + + BKE_color_managed_display_settings_copy(&sce_copy->r.im_format.display_settings, &sce->r.im_format.display_settings); + BKE_color_managed_view_settings_copy(&sce_copy->r.im_format.view_settings, &sce->r.im_format.view_settings); + + BKE_color_managed_display_settings_copy(&sce_copy->r.bake.im_format.display_settings, &sce->r.bake.im_format.display_settings); + BKE_color_managed_view_settings_copy(&sce_copy->r.bake.im_format.view_settings, &sce->r.bake.im_format.view_settings); + + curvemapping_copy_data(&sce_copy->r.mblur_shutter_curve, &sce->r.mblur_shutter_curve); + + /* tool settings */ + sce_copy->toolsettings = MEM_dupallocN(sce->toolsettings); + + ts = sce_copy->toolsettings; + if (ts) { + if (ts->vpaint) { + ts->vpaint = MEM_dupallocN(ts->vpaint); + BKE_paint_copy(&ts->vpaint->paint, &ts->vpaint->paint, 0); + } + if (ts->wpaint) { + ts->wpaint = MEM_dupallocN(ts->wpaint); + BKE_paint_copy(&ts->wpaint->paint, &ts->wpaint->paint, 0); + } + if (ts->sculpt) { + ts->sculpt = MEM_dupallocN(ts->sculpt); + BKE_paint_copy(&ts->sculpt->paint, &ts->sculpt->paint, 0); + } + if (ts->uvsculpt) { + ts->uvsculpt = MEM_dupallocN(ts->uvsculpt); + BKE_paint_copy(&ts->uvsculpt->paint, &ts->uvsculpt->paint, 0); + } + + BKE_paint_copy(&ts->imapaint.paint, &ts->imapaint.paint, 0); + ts->imapaint.paintcursor = NULL; + id_us_plus((ID *)ts->imapaint.stencil); + id_us_plus((ID *)ts->imapaint.clone); + id_us_plus((ID *)ts->imapaint.canvas); + ts->particle.paintcursor = NULL; + ts->particle.scene = NULL; + ts->particle.object = NULL; + + /* duplicate Grease Pencil Drawing Brushes */ + BLI_listbase_clear(&ts->gp_brushes); + for (bGPDbrush *brush = sce->toolsettings->gp_brushes.first; brush; brush = brush->next) { + bGPDbrush *newbrush = BKE_gpencil_brush_duplicate(brush); + BLI_addtail(&ts->gp_brushes, newbrush); + } + + /* duplicate Grease Pencil interpolation curve */ + ts->gp_interpolate.custom_ipo = curvemapping_copy(ts->gp_interpolate.custom_ipo); + } + + /* make a private copy of the avicodecdata */ + if (sce->r.avicodecdata) { + sce_copy->r.avicodecdata = MEM_dupallocN(sce->r.avicodecdata); + sce_copy->r.avicodecdata->lpFormat = MEM_dupallocN(sce_copy->r.avicodecdata->lpFormat); + sce_copy->r.avicodecdata->lpParms = MEM_dupallocN(sce_copy->r.avicodecdata->lpParms); } - if (sce->ed) { - scen->ed = MEM_callocN(sizeof(Editing), "addseq"); - scen->ed->seqbasep = &scen->ed->seqbase; - BKE_sequence_base_dupli_recursive(sce, scen, &scen->ed->seqbase, &sce->ed->seqbase, SEQ_DUPE_ALL); + if (sce->r.ffcodecdata.properties) { /* intentionally check scen not sce. */ + sce_copy->r.ffcodecdata.properties = IDP_CopyProperty(sce->r.ffcodecdata.properties); } + + /* before scene copy */ + BKE_sound_create_scene(sce_copy); + + /* grease pencil */ + sce_copy->gpd = NULL; + + sce_copy->preview = NULL; + + return sce_copy; } - - /* grease pencil */ - if (scen->gpd) { + else { + BKE_id_copy_ex(bmain, (ID *)sce, (ID **)&sce_copy, LIB_ID_COPY_ACTIONS, false); + + /* Extra actions, most notably SCE_FULL_COPY also duplicates several 'children' datablocks... */ + if (type == SCE_COPY_FULL) { - scen->gpd = BKE_gpencil_data_duplicate(bmain, scen->gpd, false); - } - else if (type == SCE_COPY_EMPTY) { - scen->gpd = NULL; + /* Copy Freestyle LineStyle datablocks. */ + for (SceneRenderLayer *srl_dst = sce_copy->r.layers.first; srl_dst; srl_dst = srl_dst->next) { + for (FreestyleLineSet *lineset = srl_dst->freestyleConfig.linesets.first; lineset; lineset = lineset->next) { + if (lineset->linestyle) { + /* XXX Not copying anim/actions here? */ + BKE_id_copy_ex(bmain, (ID *)lineset->linestyle, (ID **)&lineset->linestyle, 0, false); + } + } + } + + /* Full copy of world (included animations) */ + if (sce_copy->world) { + BKE_id_copy_ex(bmain, (ID *)sce_copy->world, (ID **)&sce_copy->world, LIB_ID_COPY_ACTIONS, false); + } + + /* Full copy of GreasePencil. */ + /* XXX Not copying anim/actions here? */ + if (sce_copy->gpd) { + BKE_id_copy_ex(bmain, (ID *)sce_copy->gpd, (ID **)&sce_copy->gpd, 0, false); + } } else { - id_us_plus((ID *)scen->gpd); + /* Remove sequencer if not full copy */ + /* XXX Why in Hell? :/ */ + remove_sequencer_fcurves(sce_copy); + BKE_sequencer_editing_free(sce_copy); } - } - BKE_previewimg_id_copy(&scen->id, &sce->id); + /* NOTE: part of SCE_COPY_LINK_DATA and SCE_COPY_FULL operations + * are done outside of blenkernel with ED_objects_single_users! */ - if (type != SCE_COPY_NEW) { - if (sce->collection_properties) { - IDP_MergeGroup(scen->collection_properties, sce->collection_properties, true); + /* camera */ + if (ELEM(type, SCE_COPY_LINK_DATA, SCE_COPY_FULL)) { + ID_NEW_REMAP(sce_copy->camera); } - if (sce->layer_properties) { - IDP_MergeGroup(scen->layer_properties, sce->layer_properties, true); - } - } - return scen; + return sce_copy; + } } void BKE_scene_groups_relink(Scene *sce) @@ -517,11 +607,6 @@ void BKE_scene_free_ex(Scene *sce, const bool do_id_user) MEM_freeN(sce->r.avicodecdata); sce->r.avicodecdata = NULL; } - if (sce->r.qtcodecdata) { - free_qtcodecdata(sce->r.qtcodecdata); - MEM_freeN(sce->r.qtcodecdata); - sce->r.qtcodecdata = NULL; - } if (sce->r.ffcodecdata.properties) { IDP_FreeProperty(sce->r.ffcodecdata.properties); MEM_freeN(sce->r.ffcodecdata.properties); @@ -539,7 +624,7 @@ void BKE_scene_free_ex(Scene *sce, const bool do_id_user) BLI_freelistN(&sce->markers); BLI_freelistN(&sce->r.layers); BLI_freelistN(&sce->r.views); - + if (sce->toolsettings) { if (sce->toolsettings->vpaint) { BKE_paint_free(&sce->toolsettings->vpaint->paint); @@ -573,8 +658,8 @@ void BKE_scene_free_ex(Scene *sce, const bool do_id_user) } DEG_scene_graph_free(sce); - if (sce->depsgraph) - DEG_graph_free(sce->depsgraph); + if (sce->depsgraph_legacy) + DEG_graph_free(sce->depsgraph_legacy); MEM_SAFE_FREE(sce->fps_info); @@ -614,7 +699,7 @@ void BKE_scene_free_ex(Scene *sce, const bool do_id_user) void BKE_scene_free(Scene *sce) { - return BKE_scene_free_ex(sce, true); + BKE_scene_free_ex(sce, true); } void BKE_scene_init(Scene *sce) @@ -710,7 +795,7 @@ void BKE_scene_init(Scene *sce) sce->r.seq_prev_type = OB_SOLID; sce->r.seq_rend_type = OB_SOLID; - sce->r.seq_flag = R_SEQ_GL_PREV; + sce->r.seq_flag = 0; sce->r.threads = 1; @@ -818,7 +903,7 @@ void BKE_scene_init(Scene *sce) sce->r.ffcodecdata.audio_bitrate = 192; sce->r.ffcodecdata.audio_channels = 2; - BLI_strncpy(sce->r.engine, RE_engine_id_BLENDER_RENDER, sizeof(sce->r.engine)); + BLI_strncpy(sce->r.engine, RE_engine_id_BLENDER_EEVEE, sizeof(sce->r.engine)); sce->audio.distance_model = 2.0f; sce->audio.doppler_factor = 1.0f; @@ -984,7 +1069,7 @@ Scene *BKE_scene_add(Main *bmain, const char *name) { Scene *sce; - sce = BKE_libblock_alloc(bmain, ID_SCE, name); + sce = BKE_libblock_alloc(bmain, ID_SCE, name, 0); id_us_min(&sce->id); id_us_ensure_real(&sce->id); @@ -1074,8 +1159,9 @@ Scene *BKE_scene_set_name(Main *bmain, const char *name) } /* Used by metaballs, return *all* objects (including duplis) existing in the scene (including scene's sets) */ -int BKE_scene_base_iter_next(EvaluationContext *eval_ctx, SceneBaseIter *iter, - Scene **scene, int val, BaseLegacy **base, Object **ob) +int BKE_scene_base_iter_next( + const EvaluationContext *eval_ctx, SceneBaseIter *iter, + Scene **scene, int val, BaseLegacy **base, Object **ob) { bool run_again = true; @@ -1517,7 +1603,7 @@ void BKE_scene_update_tagged(EvaluationContext *eval_ctx, Main *bmain, Scene *sc prepare_mesh_for_viewport_render(bmain, scene); /* flush recalc flags to dependencies */ - DEG_ids_flush_tagged(bmain); + DEG_scene_flush_update(bmain, scene); /* removed calls to quick_cache, see pointcache.c */ @@ -1532,9 +1618,7 @@ void BKE_scene_update_tagged(EvaluationContext *eval_ctx, Main *bmain, Scene *sc * * in the future this should handle updates for all datablocks, not * only objects and scenes. - brecht */ - DEG_evaluate_on_refresh(eval_ctx, scene->depsgraph, scene); - /* TODO(sergey): This is to beocme a node in new depsgraph. */ - BKE_mask_update_scene(bmain, scene); + DEG_evaluate_on_refresh(eval_ctx, scene->depsgraph_legacy, scene); /* update sound system animation (TODO, move to depsgraph) */ BKE_sound_update_scene(bmain, scene); @@ -1571,15 +1655,13 @@ void BKE_scene_update_for_newframe(EvaluationContext *eval_ctx, Main *bmain, Sce BKE_image_update_frame(bmain, sce->r.cfra); BKE_sound_set_cfra(sce->r.cfra); - + /* clear animation overrides */ /* XXX TODO... */ for (sce_iter = sce; sce_iter; sce_iter = sce_iter->set) DEG_scene_relations_update(bmain, sce_iter); - BKE_mask_evaluate_all_masks(bmain, ctime, true); - /* Update animated cache files for modifiers. */ BKE_cachefile_update_frame(bmain, sce, ctime, (((double)sce->r.frs_sec) / (double)sce->r.frs_sec_base)); @@ -1594,7 +1676,7 @@ void BKE_scene_update_for_newframe(EvaluationContext *eval_ctx, Main *bmain, Sce BKE_main_id_tag_idcode(bmain, ID_LA, LIB_TAG_DOIT, false); /* BKE_object_handle_update() on all objects, groups and sets */ - DEG_evaluate_on_framechange(eval_ctx, bmain, sce->depsgraph, ctime); + DEG_evaluate_on_framechange(eval_ctx, bmain, sce->depsgraph_legacy, ctime); /* update sound system animation (TODO, move to depsgraph) */ BKE_sound_update_scene(bmain, sce); @@ -1764,7 +1846,7 @@ Base *_setlooper_base_step(Scene **sce_iter, Base *base) /* first time looping, return the scenes first base */ /* for the first loop we should get the layer from context */ - SceneLayer *sl = BKE_scene_layer_context_active((*sce_iter)); + SceneLayer *sl = BKE_scene_layer_context_active_PLACEHOLDER((*sce_iter)); /* TODO For first scene (non-background set), we should pass the render layer as argument. * In some cases we want it to be the workspace one, in other the scene one. */ TODO_LAYER; @@ -1779,7 +1861,7 @@ Base *_setlooper_base_step(Scene **sce_iter, Base *base) next_set: /* reached the end, get the next base in the set */ while ((*sce_iter = (*sce_iter)->set)) { - SceneLayer *sl = BKE_scene_layer_render_active((*sce_iter)); + SceneLayer *sl = BKE_scene_layer_from_scene_get((*sce_iter)); base = (Base *)sl->object_bases.first; if (base) { @@ -1935,6 +2017,14 @@ int BKE_scene_num_threads(const Scene *scene) return BKE_render_num_threads(&scene->r); } +int BKE_render_preview_pixel_size(const RenderData *r) +{ + if (r->preview_pixel_size == 0) { + return (U.pixelsize > 1.5f) ? 2 : 1; + } + return r->preview_pixel_size; +} + /* Apply the needed correction factor to value, based on unit_type (only length-related are affected currently) * and unit->scale_length. */ @@ -2240,3 +2330,9 @@ int BKE_scene_multiview_num_videos_get(const RenderData *rd) return BKE_scene_multiview_num_views_get(rd); } } + +Depsgraph *BKE_scene_get_depsgraph(Scene *scene, SceneLayer *scene_layer) +{ + (void) scene_layer; + return scene->depsgraph_legacy; +} diff --git a/source/blender/blenkernel/intern/screen.c b/source/blender/blenkernel/intern/screen.c index f3a93a0a42c..6bd88099792 100644 --- a/source/blender/blenkernel/intern/screen.c +++ b/source/blender/blenkernel/intern/screen.c @@ -185,6 +185,7 @@ ARegion *BKE_area_region_copy(SpaceType *st, ARegion *ar) newar->swinid = 0; newar->manipulator_map = NULL; newar->regiontimer = NULL; + newar->headerstr = NULL; /* use optional regiondata callback */ if (ar->regiondata) { @@ -292,6 +293,32 @@ void BKE_spacedata_id_unref(struct ScrArea *sa, struct SpaceLink *sl, struct ID } } +/** + * Avoid bad-level calls to #WM_manipulatormap_tag_refresh. + */ +static void (*region_refresh_tag_manipulatormap_callback)(struct wmManipulatorMap *) = NULL; + +void BKE_region_callback_refresh_tag_manipulatormap_set(void (*callback)(struct wmManipulatorMap *)) +{ + region_refresh_tag_manipulatormap_callback = callback; +} + +void BKE_screen_manipulator_tag_refresh(struct bScreen *sc) +{ + if (region_refresh_tag_manipulatormap_callback == NULL) { + return; + } + + ScrArea *sa; + ARegion *ar; + for (sa = sc->areabase.first; sa; sa = sa->next) { + for (ar = sa->regionbase.first; ar; ar = ar->next) { + if (ar->manipulator_map != NULL) { + region_refresh_tag_manipulatormap_callback(ar->manipulator_map); + } + } + } +} /** * Avoid bad-level calls to #WM_manipulatormap_delete. @@ -353,7 +380,10 @@ void BKE_area_region_free(SpaceType *st, ARegion *ar) } } - region_free_manipulatormap_callback(ar->manipulator_map); + if (ar->manipulator_map != NULL) { + region_free_manipulatormap_callback(ar->manipulator_map); + } + BLI_freelistN(&ar->ui_lists); BLI_freelistN(&ar->ui_previews); BLI_freelistN(&ar->panels_category); diff --git a/source/blender/blenkernel/intern/seqeffects.c b/source/blender/blenkernel/intern/seqeffects.c index e435d87024e..a2c45057bf7 100644 --- a/source/blender/blenkernel/intern/seqeffects.c +++ b/source/blender/blenkernel/intern/seqeffects.c @@ -1861,7 +1861,7 @@ static void RVBlurBitmap2_float(float *map, int width, int height, float blur, i float *filter = NULL; int x, y, i, fx, fy; int index, ix, halfWidth; - float fval, k, curColor[3], curColor2[3], weight = 0; + float fval, k, curColor[4], curColor2[4], weight = 0; /* If we're not really blurring, bail out */ if (blur <= 0) @@ -1906,47 +1906,38 @@ static void RVBlurBitmap2_float(float *map, int width, int height, float blur, i for (y = 0; y < height; y++) { /* Do the left & right strips */ for (x = 0; x < halfWidth; x++) { - index = (x + y * width) * 4; fx = 0; - curColor[0] = curColor[1] = curColor[2] = 0.0f; - curColor2[0] = curColor2[1] = curColor2[2] = 0.0f; + zero_v4(curColor); + zero_v4(curColor2); for (i = x - halfWidth; i < x + halfWidth; i++) { if ((i >= 0) && (i < width)) { - curColor[0] += map[(i + y * width) * 4 + GlowR] * filter[fx]; - curColor[1] += map[(i + y * width) * 4 + GlowG] * filter[fx]; - curColor[2] += map[(i + y * width) * 4 + GlowB] * filter[fx]; + index = (i + y * width) * 4; + madd_v4_v4fl(curColor, map + index, filter[fx]); - curColor2[0] += map[(width - 1 - i + y * width) * 4 + GlowR] * filter[fx]; - curColor2[1] += map[(width - 1 - i + y * width) * 4 + GlowG] * filter[fx]; - curColor2[2] += map[(width - 1 - i + y * width) * 4 + GlowB] * filter[fx]; + index = (width - 1 - i + y * width) * 4; + madd_v4_v4fl(curColor2, map + index, filter[fx]); } fx++; } - temp[index + GlowR] = curColor[0]; - temp[index + GlowG] = curColor[1]; - temp[index + GlowB] = curColor[2]; - - temp[((width - 1 - x + y * width) * 4) + GlowR] = curColor2[0]; - temp[((width - 1 - x + y * width) * 4) + GlowG] = curColor2[1]; - temp[((width - 1 - x + y * width) * 4) + GlowB] = curColor2[2]; + index = (x + y * width) * 4; + copy_v4_v4(temp + index, curColor); + index = (width - 1 - x + y * width) * 4; + copy_v4_v4(temp + index, curColor2); } /* Do the main body */ for (x = halfWidth; x < width - halfWidth; x++) { - index = (x + y * width) * 4; fx = 0; - zero_v3(curColor); + zero_v4(curColor); for (i = x - halfWidth; i < x + halfWidth; i++) { - curColor[0] += map[(i + y * width) * 4 + GlowR] * filter[fx]; - curColor[1] += map[(i + y * width) * 4 + GlowG] * filter[fx]; - curColor[2] += map[(i + y * width) * 4 + GlowB] * filter[fx]; + index = (i + y * width) * 4; + madd_v4_v4fl(curColor, map + index, filter[fx]); fx++; } - temp[index + GlowR] = curColor[0]; - temp[index + GlowG] = curColor[1]; - temp[index + GlowB] = curColor[2]; + index = (x + y * width) * 4; + copy_v4_v4(temp + index, curColor); } } @@ -1957,46 +1948,39 @@ static void RVBlurBitmap2_float(float *map, int width, int height, float blur, i for (x = 0; x < width; x++) { /* Do the top & bottom strips */ for (y = 0; y < halfWidth; y++) { - index = (x + y * width) * 4; fy = 0; - zero_v3(curColor); - zero_v3(curColor2); + zero_v4(curColor); + zero_v4(curColor2); for (i = y - halfWidth; i < y + halfWidth; i++) { if ((i >= 0) && (i < height)) { /* Bottom */ - curColor[0] += map[(x + i * width) * 4 + GlowR] * filter[fy]; - curColor[1] += map[(x + i * width) * 4 + GlowG] * filter[fy]; - curColor[2] += map[(x + i * width) * 4 + GlowB] * filter[fy]; + index = (x + i * width) * 4; + madd_v4_v4fl(curColor, map + index, filter[fy]); /* Top */ - curColor2[0] += map[(x + (height - 1 - i) * width) * 4 + GlowR] * filter[fy]; - curColor2[1] += map[(x + (height - 1 - i) * width) * 4 + GlowG] * filter[fy]; - curColor2[2] += map[(x + (height - 1 - i) * width) * 4 + GlowB] * filter[fy]; + index = (x + (height - 1 - i) * width) * 4; + madd_v4_v4fl(curColor2, map + index, filter[fy]); } fy++; } - temp[index + GlowR] = curColor[0]; - temp[index + GlowG] = curColor[1]; - temp[index + GlowB] = curColor[2]; - temp[((x + (height - 1 - y) * width) * 4) + GlowR] = curColor2[0]; - temp[((x + (height - 1 - y) * width) * 4) + GlowG] = curColor2[1]; - temp[((x + (height - 1 - y) * width) * 4) + GlowB] = curColor2[2]; + index = (x + y * width) * 4; + copy_v4_v4(temp + index, curColor); + + index = (x + (height - 1 - y) * width) * 4; + copy_v4_v4(temp + index, curColor2); } /* Do the main body */ for (y = halfWidth; y < height - halfWidth; y++) { - index = (x + y * width) * 4; fy = 0; - zero_v3(curColor); + zero_v4(curColor); for (i = y - halfWidth; i < y + halfWidth; i++) { - curColor[0] += map[(x + i * width) * 4 + GlowR] * filter[fy]; - curColor[1] += map[(x + i * width) * 4 + GlowG] * filter[fy]; - curColor[2] += map[(x + i * width) * 4 + GlowB] * filter[fy]; + index = (x + i * width) * 4; + madd_v4_v4fl(curColor, map + index, filter[fy]); fy++; } - temp[index + GlowR] = curColor[0]; - temp[index + GlowG] = curColor[1]; - temp[index + GlowB] = curColor[2]; + index = (x + y * width) * 4; + copy_v4_v4(temp + index, curColor); } } @@ -2015,10 +1999,10 @@ static void RVAddBitmaps_float(float *a, float *b, float *c, int width, int heig for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { index = (x + y * width) * 4; - c[index + GlowR] = MIN2(1.0f, a[index + GlowR] + b[index + GlowR]); - c[index + GlowG] = MIN2(1.0f, a[index + GlowG] + b[index + GlowG]); - c[index + GlowB] = MIN2(1.0f, a[index + GlowB] + b[index + GlowB]); - c[index + GlowA] = MIN2(1.0f, a[index + GlowA] + b[index + GlowA]); + c[index + GlowR] = min_ff(1.0f, a[index + GlowR] + b[index + GlowR]); + c[index + GlowG] = min_ff(1.0f, a[index + GlowG] + b[index + GlowG]); + c[index + GlowB] = min_ff(1.0f, a[index + GlowB] + b[index + GlowB]); + c[index + GlowA] = min_ff(1.0f, a[index + GlowA] + b[index + GlowA]); } } } @@ -2035,10 +2019,10 @@ static void RVIsolateHighlights_float(float *in, float *out, int width, int heig /* Isolate the intensity */ intensity = (in[index + GlowR] + in[index + GlowG] + in[index + GlowB] - threshold); if (intensity > 0) { - out[index + GlowR] = MIN2(clamp, (in[index + GlowR] * boost * intensity)); - out[index + GlowG] = MIN2(clamp, (in[index + GlowG] * boost * intensity)); - out[index + GlowB] = MIN2(clamp, (in[index + GlowB] * boost * intensity)); - out[index + GlowA] = MIN2(clamp, (in[index + GlowA] * boost * intensity)); + out[index + GlowR] = min_ff(clamp, (in[index + GlowR] * boost * intensity)); + out[index + GlowG] = min_ff(clamp, (in[index + GlowG] * boost * intensity)); + out[index + GlowB] = min_ff(clamp, (in[index + GlowB] * boost * intensity)); + out[index + GlowA] = min_ff(clamp, (in[index + GlowA] * boost * intensity)); } else { out[index + GlowR] = 0; diff --git a/source/blender/blenkernel/intern/sequencer.c b/source/blender/blenkernel/intern/sequencer.c index e316b7f26e9..c6b6f97b0bc 100644 --- a/source/blender/blenkernel/intern/sequencer.c +++ b/source/blender/blenkernel/intern/sequencer.c @@ -91,7 +91,7 @@ #include "BKE_sound.h" #ifdef WITH_AUDASPACE -# include AUD_SPECIAL_H +# include <AUD_Special.h> #endif /* mutable state for sequencer */ @@ -379,6 +379,8 @@ static void seqclipboard_ptr_restore(Main *bmain, ID **id_pt) } break; } + default: + break; } } @@ -482,9 +484,12 @@ void BKE_sequencer_editing_free(Scene *scene) static void sequencer_imbuf_assign_spaces(Scene *scene, ImBuf *ibuf) { +#if 0 + /* Bute buffer is supposed to be in sequencer working space already. */ if (ibuf->rect != NULL) { IMB_colormanagement_assign_rect_colorspace(ibuf, scene->sequencer_colorspace_settings.name); } +#endif if (ibuf->rect_float != NULL) { IMB_colormanagement_assign_float_colorspace(ibuf, scene->sequencer_colorspace_settings.name); } @@ -2084,7 +2089,7 @@ void BKE_sequencer_proxy_set(struct Sequence *seq, bool value) } } else { - seq->flag ^= SEQ_USE_PROXY; + seq->flag &= ~SEQ_USE_PROXY; } } @@ -3235,7 +3240,7 @@ static ImBuf *seq_render_scene_strip(const SeqRenderData *context, Sequence *seq const bool is_background = G.background; const bool do_seq_gl = is_rendering ? 0 /* (context->scene->r.seq_flag & R_SEQ_GL_REND) */ : - (context->scene->r.seq_flag & R_SEQ_GL_PREV) != 0; + (context->scene->r.seq_prev_type) != OB_RENDER; // bool have_seq = false; /* UNUSED */ bool have_comp = false; bool use_gpencil = true; @@ -3306,7 +3311,7 @@ static ImBuf *seq_render_scene_strip(const SeqRenderData *context, Sequence *seq BKE_scene_update_for_newframe(context->eval_ctx, context->bmain, scene); ibuf = sequencer_view3d_cb( /* set for OpenGL render (NULL when scrubbing) */ - scene, BKE_scene_layer_render_active(scene), camera, width, height, IB_rect, + context->eval_ctx, scene, BKE_scene_layer_from_scene_get(scene), camera, width, height, IB_rect, context->scene->r.seq_prev_type, (context->scene->r.seq_flag & R_SEQ_SOLID_TEX) != 0, use_gpencil, use_background, scene->r.alphamode, @@ -3317,7 +3322,7 @@ static ImBuf *seq_render_scene_strip(const SeqRenderData *context, Sequence *seq } } else { - Render *re = RE_GetRender(scene->id.name); + Render *re = RE_GetSceneRender(scene); const int totviews = BKE_scene_multiview_num_views_get(&scene->r); int i; ImBuf **ibufs_arr; @@ -3334,7 +3339,7 @@ static ImBuf *seq_render_scene_strip(const SeqRenderData *context, Sequence *seq */ if (!is_thread_main || is_rendering == false || is_background || context->eval_ctx->mode == DAG_EVAL_RENDER) { if (re == NULL) - re = RE_NewRender(scene->id.name); + re = RE_NewSceneRender(scene); BKE_scene_update_for_newframe(context->eval_ctx, context->bmain, scene); RE_BlenderFrame(re, context->bmain, scene, NULL, camera, scene->lay, frame, false); @@ -5373,9 +5378,8 @@ Sequence *BKE_sequencer_add_movie_strip(bContext *C, ListBase *seqbasep, SeqLoad return seq; } -static Sequence *seq_dupli(Scene *scene, Scene *scene_to, Sequence *seq, int dupe_flag) +static Sequence *seq_dupli(const Scene *scene_src, Scene *scene_dst, Sequence *seq, int dupe_flag, const int flag) { - Scene *sce_audio = scene_to ? scene_to : scene; Sequence *seqn = MEM_dupallocN(seq); seq->tmp = seqn; @@ -5399,7 +5403,7 @@ static Sequence *seq_dupli(Scene *scene, Scene *scene_to, Sequence *seq, int dup } if (seq->prop) { - seqn->prop = IDP_CopyProperty(seq->prop); + seqn->prop = IDP_CopyProperty_ex(seq->prop, flag); } if (seqn->modifiers.first) { @@ -5418,7 +5422,7 @@ static Sequence *seq_dupli(Scene *scene, Scene *scene_to, Sequence *seq, int dup else if (seq->type == SEQ_TYPE_SCENE) { seqn->strip->stripdata = NULL; if (seq->scene_sound) - seqn->scene_sound = BKE_sound_scene_add_scene_sound_defaults(sce_audio, seqn); + seqn->scene_sound = BKE_sound_scene_add_scene_sound_defaults(scene_dst, seqn); } else if (seq->type == SEQ_TYPE_MOVIECLIP) { /* avoid assert */ @@ -5435,9 +5439,11 @@ static Sequence *seq_dupli(Scene *scene, Scene *scene_to, Sequence *seq, int dup seqn->strip->stripdata = MEM_dupallocN(seq->strip->stripdata); if (seq->scene_sound) - seqn->scene_sound = BKE_sound_add_scene_sound_defaults(sce_audio, seqn); + seqn->scene_sound = BKE_sound_add_scene_sound_defaults(scene_dst, seqn); - id_us_plus((ID *)seqn->sound); + if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) { + id_us_plus((ID *)seqn->sound); + } } else if (seq->type == SEQ_TYPE_IMAGE) { seqn->strip->stripdata = @@ -5457,11 +5463,15 @@ static Sequence *seq_dupli(Scene *scene, Scene *scene_to, Sequence *seq, int dup BLI_assert(0); } - if (dupe_flag & SEQ_DUPE_UNIQUE_NAME) - BKE_sequence_base_unique_name_recursive(&scene->ed->seqbase, seqn); + if (scene_src == scene_dst) { + if (dupe_flag & SEQ_DUPE_UNIQUE_NAME) { + BKE_sequence_base_unique_name_recursive(&scene_dst->ed->seqbase, seqn); + } - if (dupe_flag & SEQ_DUPE_ANIM) - BKE_sequencer_dupe_animdata(scene, seq->name + 2, seqn->name + 2); + if (dupe_flag & SEQ_DUPE_ANIM) { + BKE_sequencer_dupe_animdata(scene_dst, seq->name + 2, seqn->name + 2); + } + } return seqn; } @@ -5488,16 +5498,16 @@ static void seq_new_fix_links_recursive(Sequence *seq) } } -Sequence *BKE_sequence_dupli_recursive(Scene *scene, Scene *scene_to, Sequence *seq, int dupe_flag) +Sequence *BKE_sequence_dupli_recursive(const Scene *scene_src, Scene *scene_dst, Sequence *seq, int dupe_flag) { Sequence *seqn; seq->tmp = NULL; - seqn = seq_dupli(scene, scene_to, seq, dupe_flag); + seqn = seq_dupli(scene_src, scene_dst, seq, dupe_flag, 0); if (seq->type == SEQ_TYPE_META) { Sequence *s; for (s = seq->seqbase.first; s; s = s->next) { - Sequence *n = BKE_sequence_dupli_recursive(scene, scene_to, s, dupe_flag); + Sequence *n = BKE_sequence_dupli_recursive(scene_src, scene_dst, s, dupe_flag); if (n) { BLI_addtail(&seqn->seqbase, n); } @@ -5510,19 +5520,19 @@ Sequence *BKE_sequence_dupli_recursive(Scene *scene, Scene *scene_to, Sequence * } void BKE_sequence_base_dupli_recursive( - Scene *scene, Scene *scene_to, ListBase *nseqbase, ListBase *seqbase, - int dupe_flag) + const Scene *scene_src, Scene *scene_dst, ListBase *nseqbase, const ListBase *seqbase, + int dupe_flag, const int flag) { Sequence *seq; Sequence *seqn = NULL; - Sequence *last_seq = BKE_sequencer_active_get(scene); + Sequence *last_seq = BKE_sequencer_active_get((Scene *)scene_src); /* always include meta's strips */ int dupe_flag_recursive = dupe_flag | SEQ_DUPE_ALL; for (seq = seqbase->first; seq; seq = seq->next) { seq->tmp = NULL; if ((seq->flag & SELECT) || (dupe_flag & SEQ_DUPE_ALL)) { - seqn = seq_dupli(scene, scene_to, seq, dupe_flag); + seqn = seq_dupli(scene_src, scene_dst, seq, dupe_flag, flag); if (seqn) { /*should never fail */ if (dupe_flag & SEQ_DUPE_CONTEXT) { seq->flag &= ~SEQ_ALLSEL; @@ -5532,13 +5542,13 @@ void BKE_sequence_base_dupli_recursive( BLI_addtail(nseqbase, seqn); if (seq->type == SEQ_TYPE_META) { BKE_sequence_base_dupli_recursive( - scene, scene_to, &seqn->seqbase, &seq->seqbase, - dupe_flag_recursive); + scene_src, scene_dst, &seqn->seqbase, &seq->seqbase, + dupe_flag_recursive, flag); } if (dupe_flag & SEQ_DUPE_CONTEXT) { if (seq == last_seq) { - BKE_sequencer_active_set(scene, seqn); + BKE_sequencer_active_set(scene_dst, seqn); } } } diff --git a/source/blender/blenkernel/intern/shrinkwrap.c b/source/blender/blenkernel/intern/shrinkwrap.c index 7094d5a3547..f9d1793d7cb 100644 --- a/source/blender/blenkernel/intern/shrinkwrap.c +++ b/source/blender/blenkernel/intern/shrinkwrap.c @@ -152,6 +152,9 @@ static void shrinkwrap_calc_nearest_vertex(ShrinkwrapCalcData *calc) BVHTreeFromMesh treeData = NULL_BVHTreeFromMesh; BVHTreeNearest nearest = NULL_BVHTreeNearest; + if (calc->target != NULL && calc->target->getNumVerts(calc->target) == 0) { + return; + } TIMEIT_BENCH(bvhtree_from_mesh_verts(&treeData, calc->target, 0.0, 2, 6), bvhtree_verts); if (treeData.tree == NULL) { @@ -376,6 +379,9 @@ static void shrinkwrap_calc_normal_projection(ShrinkwrapCalcData *calc, bool for if ((calc->smd->shrinkOpts & (MOD_SHRINKWRAP_PROJECT_ALLOW_POS_DIR | MOD_SHRINKWRAP_PROJECT_ALLOW_NEG_DIR)) == 0) return; + if (calc->target != NULL && calc->target->getNumPolys(calc->target) == 0) { + return; + } /* Prepare data to retrieve the direction in which we should project each vertex */ if (calc->smd->projAxis == MOD_SHRINKWRAP_PROJECT_OVER_NORMAL) { @@ -432,7 +438,7 @@ static void shrinkwrap_calc_normal_projection(ShrinkwrapCalcData *calc, bool for if (targ_tree) { BVHTree *aux_tree = NULL; void *aux_callback = NULL; - if (auxMesh != NULL) { + if (auxMesh != NULL && auxMesh->getNumPolys(auxMesh) != 0) { /* use editmesh to avoid array allocation */ if (calc->smd->auxTarget && auxMesh->type == DM_TYPE_EDITBMESH) { emaux = BKE_editmesh_from_object(calc->smd->auxTarget); @@ -560,6 +566,10 @@ static void shrinkwrap_calc_nearest_surface_point(ShrinkwrapCalcData *calc) BVHTreeFromMesh treeData = NULL_BVHTreeFromMesh; BVHTreeNearest nearest = NULL_BVHTreeNearest; + if (calc->target->getNumPolys(calc->target) == 0) { + return; + } + /* Create a bvh-tree of the given target */ bvhtree_from_mesh_looptri(&treeData, calc->target, 0.0, 2, 6); if (treeData.tree == NULL) { diff --git a/source/blender/blenkernel/intern/smoke.c b/source/blender/blenkernel/intern/smoke.c index 667f8d1e8c7..7ea985a311b 100644 --- a/source/blender/blenkernel/intern/smoke.c +++ b/source/blender/blenkernel/intern/smoke.c @@ -83,6 +83,8 @@ #include "BKE_smoke.h" #include "BKE_texture.h" +#include "DEG_depsgraph.h" + #include "RE_shader_ext.h" #include "GPU_glew.h" @@ -126,7 +128,7 @@ void smoke_initWaveletBlenderRNA(struct WTURBULENCE *UNUSED(wt), float *UNUSED(s void smoke_initBlenderRNA(struct FLUID_3D *UNUSED(fluid), float *UNUSED(alpha), float *UNUSED(beta), float *UNUSED(dt_factor), float *UNUSED(vorticity), int *UNUSED(border_colli), float *UNUSED(burning_rate), float *UNUSED(flame_smoke), float *UNUSED(flame_smoke_color), float *UNUSED(flame_vorticity), float *UNUSED(flame_ignition_temp), float *UNUSED(flame_max_temp)) {} -struct DerivedMesh *smokeModifier_do(SmokeModifierData *UNUSED(smd), Scene *UNUSED(scene), SceneLayer *UNUSED(sl), Object *UNUSED(ob), DerivedMesh *UNUSED(dm)) { return NULL; } +struct DerivedMesh *smokeModifier_do(SmokeModifierData *UNUSED(smd), const struct EvaluationContext *UNUSED(eval_ctx), Scene *UNUSED(scene), Object *UNUSED(ob), DerivedMesh *UNUSED(dm)) { return NULL; } float smoke_get_velocity_at(struct Object *UNUSED(ob), float UNUSED(position[3]), float UNUSED(velocity[3])) { return 0.0f; } #endif /* WITH_SMOKE */ @@ -677,7 +679,7 @@ void smokeModifier_copy(struct SmokeModifierData *smd, struct SmokeModifierData tsmd->flow->texture_size = smd->flow->texture_size; tsmd->flow->texture_offset = smd->flow->texture_offset; - BLI_strncpy(tsmd->flow->uvlayer_name, tsmd->flow->uvlayer_name, sizeof(tsmd->flow->uvlayer_name)); + BLI_strncpy(tsmd->flow->uvlayer_name, smd->flow->uvlayer_name, sizeof(tsmd->flow->uvlayer_name)); tsmd->flow->vgroup_density = smd->flow->vgroup_density; tsmd->flow->type = smd->flow->type; @@ -702,7 +704,7 @@ static int get_lamp(SceneLayer *sl, float *light) int found_lamp = 0; // try to find a lamp, preferably local - for (base_tmp = FIRSTBASE_NEW; base_tmp; base_tmp = base_tmp->next) { + for (base_tmp = FIRSTBASE_NEW(sl); base_tmp; base_tmp = base_tmp->next) { if (base_tmp->object->type == OB_LAMP) { Lamp *la = base_tmp->object->data; @@ -2071,7 +2073,7 @@ BLI_INLINE void apply_inflow_fields(SmokeFlowSettings *sfs, float emission_value } } -static void update_flowsfluids(Scene *scene, Object *ob, SmokeDomainSettings *sds, float dt) +static void update_flowsfluids(const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, SmokeDomainSettings *sds, float dt) { Object **flowobjs = NULL; EmissionMap *emaps = NULL; @@ -2178,7 +2180,7 @@ static void update_flowsfluids(Scene *scene, Object *ob, SmokeDomainSettings *sd else { /* MOD_SMOKE_FLOW_SOURCE_MESH */ /* update flow object frame */ BLI_mutex_lock(&object_update_lock); - BKE_object_modifier_update_subframe(scene, collob, true, 5, BKE_scene_frame_get(scene), eModifierType_Smoke); + BKE_object_modifier_update_subframe(eval_ctx, scene, collob, true, 5, BKE_scene_frame_get(scene), eModifierType_Smoke); BLI_mutex_unlock(&object_update_lock); /* apply flow */ @@ -2487,12 +2489,12 @@ static void update_effectors_task_cb(void *userdata, const int x) } } -static void update_effectors(Scene *scene, Object *ob, SmokeDomainSettings *sds, float UNUSED(dt)) +static void update_effectors(const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, SmokeDomainSettings *sds, float UNUSED(dt)) { ListBase *effectors; /* make sure smoke flow influence is 0.0f */ sds->effector_weights->weight[PFIELD_SMOKEFLOW] = 0.0f; - effectors = pdInitEffectors(scene, ob, NULL, sds->effector_weights, true); + effectors = pdInitEffectors(eval_ctx, scene, ob, NULL, sds->effector_weights, true); if (effectors) { // precalculate wind forces @@ -2516,7 +2518,7 @@ static void update_effectors(Scene *scene, Object *ob, SmokeDomainSettings *sds, pdEndEffectors(&effectors); } -static void step(Scene *scene, Object *ob, SmokeModifierData *smd, DerivedMesh *domain_dm, float fps) +static void step(const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, SmokeModifierData *smd, DerivedMesh *domain_dm, float fps) { SmokeDomainSettings *sds = smd->domain; /* stability values copied from wturbulence.cpp */ @@ -2586,11 +2588,11 @@ static void step(Scene *scene, Object *ob, SmokeModifierData *smd, DerivedMesh * for (substep = 0; substep < totalSubsteps; substep++) { // calc animated obstacle velocities - update_flowsfluids(scene, ob, sds, dtSubdiv); + update_flowsfluids(eval_ctx, scene, ob, sds, dtSubdiv); update_obstacles(scene, ob, sds, dtSubdiv, substep, totalSubsteps); if (sds->total_cells > 1) { - update_effectors(scene, ob, sds, dtSubdiv); // DG TODO? problem --> uses forces instead of velocity, need to check how they need to be changed with variable dt + update_effectors(eval_ctx, scene, ob, sds, dtSubdiv); // DG TODO? problem --> uses forces instead of velocity, need to check how they need to be changed with variable dt smoke_step(sds->fluid, gravity, dtSubdiv); } } @@ -2683,7 +2685,8 @@ static DerivedMesh *createDomainGeometry(SmokeDomainSettings *sds, Object *ob) return result; } -static void smokeModifier_process(SmokeModifierData *smd, Scene *scene, SceneLayer *sl, Object *ob, DerivedMesh *dm) +static void smokeModifier_process( + SmokeModifierData *smd, const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, DerivedMesh *dm) { if ((smd->type & MOD_SMOKE_TYPE_FLOW)) { @@ -2692,7 +2695,6 @@ static void smokeModifier_process(SmokeModifierData *smd, Scene *scene, SceneLay if (smd->flow->dm) smd->flow->dm->release(smd->flow->dm); smd->flow->dm = CDDM_copy(dm); - DM_ensure_looptri(smd->flow->dm); if (scene->r.cfra > smd->time) { @@ -2715,7 +2717,6 @@ static void smokeModifier_process(SmokeModifierData *smd, Scene *scene, SceneLay smd->coll->dm->release(smd->coll->dm); smd->coll->dm = CDDM_copy(dm); - DM_ensure_looptri(smd->coll->dm); } smd->time = scene->r.cfra; @@ -2806,11 +2807,11 @@ static void smokeModifier_process(SmokeModifierData *smd, Scene *scene, SceneLay } - step(scene, ob, smd, dm, scene->r.frs_sec / scene->r.frs_sec_base); + step(eval_ctx, scene, ob, smd, dm, scene->r.frs_sec / scene->r.frs_sec_base); } // create shadows before writing cache so they get stored - smoke_calc_transparency(sds, sl); + smoke_calc_transparency(sds, eval_ctx->scene_layer); if (sds->wt && sds->total_cells > 1) { smoke_turbulence_step(sds->wt, sds->fluid); @@ -2827,13 +2828,14 @@ static void smokeModifier_process(SmokeModifierData *smd, Scene *scene, SceneLay } } -struct DerivedMesh *smokeModifier_do(SmokeModifierData *smd, Scene *scene, SceneLayer *sl, Object *ob, DerivedMesh *dm) +struct DerivedMesh *smokeModifier_do( + SmokeModifierData *smd, const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, DerivedMesh *dm) { /* lock so preview render does not read smoke data while it gets modified */ if ((smd->type & MOD_SMOKE_TYPE_DOMAIN) && smd->domain) BLI_rw_mutex_lock(smd->domain->fluid_mutex, THREAD_LOCK_WRITE); - smokeModifier_process(smd, scene, sl, ob, dm); + smokeModifier_process(smd, eval_ctx, scene, ob, dm); if ((smd->type & MOD_SMOKE_TYPE_DOMAIN) && smd->domain) BLI_rw_mutex_unlock(smd->domain->fluid_mutex); diff --git a/source/blender/blenkernel/intern/softbody.c b/source/blender/blenkernel/intern/softbody.c index 0b8c11f6dc8..1ba8fe83971 100644 --- a/source/blender/blenkernel/intern/softbody.c +++ b/source/blender/blenkernel/intern/softbody.c @@ -81,6 +81,8 @@ variables on the UI for now #include "BKE_mesh.h" #include "BKE_scene.h" +#include "DEG_depsgraph.h" + #include "PIL_time.h" /* callbacks for errors and interrupts and some goo */ @@ -530,7 +532,7 @@ static void ccd_build_deflector_hash(SceneLayer *sl, Group *group, Object *verte } } else { - for (Base *base = FIRSTBASE_NEW; base; base = base->next) { + for (Base *base = FIRSTBASE_NEW(sl); base; base = base->next) { /*Only proceed for mesh object in same layer */ if (base->object->type == OB_MESH) { ob = base->object; @@ -576,7 +578,7 @@ static void ccd_update_deflector_hash(SceneLayer *sl, Group *group, Object *vert } } else { - for (Base *base = FIRSTBASE_NEW; base; base = base->next) { + for (Base *base = FIRSTBASE_NEW(sl); base; base = base->next) { /*Only proceed for mesh object in same layer */ if (base->object->type == OB_MESH) { ob = base->object; @@ -986,7 +988,7 @@ static bool are_there_deflectors(SceneLayer *sl, Group *group) } } else { - for (Base *base = FIRSTBASE_NEW; base; base = base->next) { + for (Base *base = FIRSTBASE_NEW(sl); base; base = base->next) { if (base->object->pd) { if (base->object->pd->deflect) return 1; @@ -1544,12 +1546,12 @@ static void _scan_for_ext_spring_forces(Scene *scene, Object *ob, float timenow, } -static void scan_for_ext_spring_forces(Scene *scene, Object *ob, float timenow) +static void scan_for_ext_spring_forces(const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, float timenow) { SoftBody *sb = ob->soft; ListBase *do_effector = NULL; - do_effector = pdInitEffectors(scene, ob, NULL, sb->effector_weights, true); + do_effector = pdInitEffectors(eval_ctx, scene, ob, NULL, sb->effector_weights, true); _scan_for_ext_spring_forces(scene, ob, timenow, 0, sb->totspring, do_effector); pdEndEffectors(&do_effector); } @@ -1561,7 +1563,7 @@ static void *exec_scan_for_ext_spring_forces(void *data) return NULL; } -static void sb_sfesf_threads_run(Scene *scene, struct Object *ob, float timenow, int totsprings, int *UNUSED(ptr_to_break_func(void))) +static void sb_sfesf_threads_run(const struct EvaluationContext *eval_ctx, Scene *scene, struct Object *ob, float timenow, int totsprings, int *UNUSED(ptr_to_break_func(void))) { ListBase *do_effector = NULL; ListBase threads; @@ -1569,7 +1571,7 @@ static void sb_sfesf_threads_run(Scene *scene, struct Object *ob, float timenow, int i, totthread, left, dec; int lowsprings =100; /* wild guess .. may increase with better thread management 'above' or even be UI option sb->spawn_cf_threads_nopts */ - do_effector= pdInitEffectors(scene, ob, NULL, ob->soft->effector_weights, true); + do_effector= pdInitEffectors(eval_ctx, scene, ob, NULL, ob->soft->effector_weights, true); /* figure the number of threads while preventing pretty pointless threading overhead */ totthread= BKE_scene_num_threads(scene); @@ -2233,7 +2235,7 @@ static void sb_cf_threads_run(Scene *scene, Object *ob, float forcetime, float t MEM_freeN(sb_threads); } -static void softbody_calc_forcesEx(Scene *scene, SceneLayer *sl, Object *ob, float forcetime, float timenow) +static void softbody_calc_forcesEx(const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, float forcetime, float timenow) { /* rule we never alter free variables :bp->vec bp->pos in here ! * this will ruin adaptive stepsize AKA heun! (BM) @@ -2249,7 +2251,7 @@ static void softbody_calc_forcesEx(Scene *scene, SceneLayer *sl, Object *ob, flo /* gravity = sb->grav * sb_grav_force_scale(ob); */ /* UNUSED */ /* check conditions for various options */ - do_deflector= query_external_colliders(sl, sb->collision_group); + do_deflector= query_external_colliders(eval_ctx->scene_layer, sb->collision_group); /* do_selfcollision=((ob->softflag & OB_SB_EDGES) && (sb->bspring)&& (ob->softflag & OB_SB_SELF)); */ /* UNUSED */ do_springcollision=do_deflector && (ob->softflag & OB_SB_EDGES) &&(ob->softflag & OB_SB_EDGECOLL); do_aero=((sb->aeroedge)&& (ob->softflag & OB_SB_EDGES)); @@ -2258,10 +2260,10 @@ static void softbody_calc_forcesEx(Scene *scene, SceneLayer *sl, Object *ob, flo /* bproot= sb->bpoint; */ /* need this for proper spring addressing */ /* UNUSED */ if (do_springcollision || do_aero) - sb_sfesf_threads_run(scene, ob, timenow, sb->totspring, NULL); + sb_sfesf_threads_run(eval_ctx, scene, ob, timenow, sb->totspring, NULL); /* after spring scan because it uses Effoctors too */ - do_effector= pdInitEffectors(scene, ob, NULL, sb->effector_weights, true); + do_effector= pdInitEffectors(eval_ctx, scene, ob, NULL, sb->effector_weights, true); if (do_deflector) { float defforce[3]; @@ -2278,11 +2280,11 @@ static void softbody_calc_forcesEx(Scene *scene, SceneLayer *sl, Object *ob, flo } -static void softbody_calc_forces(Scene *scene, SceneLayer *sl, Object *ob, float forcetime, float timenow) +static void softbody_calc_forces(const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, float forcetime, float timenow) { /* redirection to the new threaded Version */ if (!(G.debug_value & 0x10)) { // 16 - softbody_calc_forcesEx(scene, sl, ob, forcetime, timenow); + softbody_calc_forcesEx(eval_ctx, scene, ob, forcetime, timenow); return; } else { @@ -2313,7 +2315,7 @@ static void softbody_calc_forces(Scene *scene, SceneLayer *sl, Object *ob, float } /* check conditions for various options */ - do_deflector= query_external_colliders(sl, sb->collision_group); + do_deflector= query_external_colliders(eval_ctx->scene_layer, sb->collision_group); do_selfcollision=((ob->softflag & OB_SB_EDGES) && (sb->bspring)&& (ob->softflag & OB_SB_SELF)); do_springcollision=do_deflector && (ob->softflag & OB_SB_EDGES) &&(ob->softflag & OB_SB_EDGECOLL); do_aero=((sb->aeroedge)&& (ob->softflag & OB_SB_EDGES)); @@ -2321,9 +2323,9 @@ static void softbody_calc_forces(Scene *scene, SceneLayer *sl, Object *ob, float iks = 1.0f/(1.0f-sb->inspring)-1.0f ;/* inner spring constants function */ /* bproot= sb->bpoint; */ /* need this for proper spring addressing */ /* UNUSED */ - if (do_springcollision || do_aero) scan_for_ext_spring_forces(scene, ob, timenow); + if (do_springcollision || do_aero) scan_for_ext_spring_forces(eval_ctx, scene, ob, timenow); /* after spring scan because it uses Effoctors too */ - do_effector= pdInitEffectors(scene, ob, NULL, ob->soft->effector_weights, true); + do_effector= pdInitEffectors(eval_ctx, scene, ob, NULL, ob->soft->effector_weights, true); if (do_deflector) { float defforce[3]; @@ -3412,7 +3414,7 @@ static void softbody_update_positions(Object *ob, SoftBody *sb, float (*vertexCo * lloc, lrot, lscale are allowed to be NULL, just in case you don't need it. * should be pretty useful for pythoneers :) * not! velocity .. 2nd order stuff - * vcloud_estimate_transform see + * vcloud_estimate_transform_v3 see */ void SB_estimate_transform(Object *ob, float lloc[3], float lrot[3][3], float lscale[3][3]) @@ -3436,7 +3438,7 @@ void SB_estimate_transform(Object *ob, float lloc[3], float lrot[3][3], float ls copy_v3_v3(opos[a], bp->pos); } - vcloud_estimate_transform(sb->totpoint, opos, NULL, rpos, NULL, com, rcom, lrot, lscale); + vcloud_estimate_transform_v3(sb->totpoint, opos, NULL, rpos, NULL, com, rcom, lrot, lscale); //sub_v3_v3(com, rcom); if (lloc) copy_v3_v3(lloc, com); copy_v3_v3(sb->lcom, com); @@ -3510,7 +3512,7 @@ static void softbody_reset(Object *ob, SoftBody *sb, float (*vertexCos)[3], int } } -static void softbody_step(Scene *scene, SceneLayer *sl, Object *ob, SoftBody *sb, float dtime) +static void softbody_step(const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, SoftBody *sb, float dtime) { /* the simulator */ float forcetime; @@ -3524,11 +3526,11 @@ static void softbody_step(Scene *scene, SceneLayer *sl, Object *ob, SoftBody *sb */ if (dtime < 0 || dtime > 10.5f) return; - ccd_update_deflector_hash(sl, sb->collision_group, ob, sb->scratch->colliderhash); + ccd_update_deflector_hash(eval_ctx->scene_layer, sb->collision_group, ob, sb->scratch->colliderhash); if (sb->scratch->needstobuildcollider) { - if (query_external_colliders(sl, sb->collision_group)) { - ccd_build_deflector_hash(sl, sb->collision_group, ob, sb->scratch->colliderhash); + if (query_external_colliders(eval_ctx->scene_layer, sb->collision_group)) { + ccd_build_deflector_hash(eval_ctx->scene_layer, sb->collision_group, ob, sb->scratch->colliderhash); } sb->scratch->needstobuildcollider=0; } @@ -3558,12 +3560,12 @@ static void softbody_step(Scene *scene, SceneLayer *sl, Object *ob, SoftBody *sb sb->scratch->flag &= ~SBF_DOFUZZY; /* do predictive euler step */ - softbody_calc_forces(scene, sl, ob, forcetime, timedone/dtime); + softbody_calc_forces(eval_ctx, scene, ob, forcetime, timedone/dtime); softbody_apply_forces(ob, forcetime, 1, NULL, mid_flags); /* crop new slope values to do averaged slope step */ - softbody_calc_forces(scene, sl, ob, forcetime, timedone/dtime); + softbody_calc_forces(eval_ctx, scene, ob, forcetime, timedone/dtime); softbody_apply_forces(ob, forcetime, 2, &err, mid_flags); softbody_apply_goalsnap(ob); @@ -3644,7 +3646,7 @@ static void softbody_step(Scene *scene, SceneLayer *sl, Object *ob, SoftBody *sb } /* simulates one step. framenr is in frames */ -void sbObjectStep(Scene *scene, SceneLayer *sl, Object *ob, float cfra, float (*vertexCos)[3], int numVerts) +void sbObjectStep(const struct EvaluationContext *eval_ctx, Scene *scene, Object *ob, float cfra, float (*vertexCos)[3], int numVerts) { SoftBody *sb= ob->soft; PointCache *cache; @@ -3759,7 +3761,7 @@ void sbObjectStep(Scene *scene, SceneLayer *sl, Object *ob, float cfra, float (* dtime = framedelta*timescale; /* do simulation */ - softbody_step(scene, sl, ob, sb, dtime); + softbody_step(eval_ctx, scene, ob, sb, dtime); softbody_to_object(ob, vertexCos, numVerts, 0); diff --git a/source/blender/blenkernel/intern/sound.c b/source/blender/blenkernel/intern/sound.c index 8469351c54a..a2568707b0a 100644 --- a/source/blender/blenkernel/intern/sound.c +++ b/source/blender/blenkernel/intern/sound.c @@ -48,13 +48,11 @@ #include "DNA_speaker_types.h" #ifdef WITH_AUDASPACE -# include AUD_SOUND_H -# include AUD_SEQUENCE_H -# include AUD_HANDLE_H -# include AUD_SPECIAL_H -# ifdef WITH_SYSTEM_AUDASPACE -# include "../../../intern/audaspace/intern/AUD_Set.h" -# endif +# include <AUD_Sound.h> +# include <AUD_Sequence.h> +# include <AUD_Handle.h> +# include <AUD_Special.h> +# include "../../../intern/audaspace/intern/AUD_Set.h" #endif #include "BKE_global.h" @@ -83,7 +81,7 @@ bSound *BKE_sound_new_file(struct Main *bmain, const char *filepath) BLI_path_abs(str, path); - sound = BKE_libblock_alloc(bmain, ID_SO, BLI_path_basename(filepath)); + sound = BKE_libblock_alloc(bmain, ID_SO, BLI_path_basename(filepath), 0); BLI_strncpy(sound->name, filepath, FILE_MAX); /* sound->type = SOUND_TYPE_FILE; */ /* XXX unused currently */ @@ -155,6 +153,34 @@ void BKE_sound_free(bSound *sound) } } +/** + * Only copy internal data of Sound ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_sound_copy_data(Main *bmain, bSound *sound_dst, const bSound *UNUSED(sound_src), const int UNUSED(flag)) +{ + sound_dst->handle = NULL; + sound_dst->cache = NULL; + sound_dst->waveform = NULL; + sound_dst->playback_handle = NULL; + sound_dst->spinlock = NULL; /* Think this is OK? Otherwise, easy to create new spinlock here... */ + + /* Just to be sure, should not have any value actually after reading time. */ + sound_dst->ipo = NULL; + sound_dst->newpackedfile = NULL; + + if (sound_dst->packedfile) { + sound_dst->packedfile = dupPackedFile(sound_dst->packedfile); + } + + /* Initialize whole runtime (audaspace) stuff. */ + BKE_sound_load(bmain, sound_dst); +} + void BKE_sound_make_local(Main *bmain, bSound *sound, const bool lib_local) { BKE_id_make_local_generic(bmain, &sound->id, true, lib_local); @@ -200,7 +226,7 @@ void BKE_sound_init_once(void) atexit(BKE_sound_exit_once); } -static AUD_Device *sound_device; +static AUD_Device *sound_device = NULL; void *BKE_sound_get_device(void) { @@ -209,6 +235,9 @@ void *BKE_sound_get_device(void) void BKE_sound_init(struct Main *bmain) { + /* Make sure no instance of the sound system is running, otherwise we get leaks. */ + BKE_sound_exit(); + AUD_DeviceSpecs specs; int device, buffersize; const char *device_name; @@ -274,7 +303,6 @@ void BKE_sound_exit_once(void) sound_device = NULL; AUD_exitOnce(); -#ifdef WITH_SYSTEM_AUDASPACE if (audio_device_names != NULL) { int i; for (i = 0; audio_device_names[i]; i++) { @@ -283,7 +311,6 @@ void BKE_sound_exit_once(void) free(audio_device_names); audio_device_names = NULL; } -#endif } /* XXX unused currently */ @@ -879,28 +906,12 @@ float BKE_sound_get_length(bSound *sound) char **BKE_sound_get_device_names(void) { if (audio_device_names == NULL) { -#ifdef WITH_SYSTEM_AUDASPACE audio_device_names = AUD_getDeviceNames(); -#else - static const char *names[] = { - "Null", "SDL", "OpenAL", "JACK", NULL - }; - audio_device_names = (char **)names; -#endif } return audio_device_names; } -bool BKE_sound_is_jack_supported(void) -{ -#ifdef WITH_SYSTEM_AUDASPACE - return 1; -#else - return (bool)AUD_isJackSupported(); -#endif -} - #else /* WITH_AUDASPACE */ #include "BLI_utildefines.h" @@ -947,5 +958,6 @@ void BKE_sound_set_scene_sound_pan(void *UNUSED(handle), float UNUSED(pan), char void BKE_sound_set_scene_volume(struct Scene *UNUSED(scene), float UNUSED(volume)) {} void BKE_sound_set_scene_sound_pitch(void *UNUSED(handle), float UNUSED(pitch), char UNUSED(animated)) {} float BKE_sound_get_length(struct bSound *UNUSED(sound)) { return 0; } -bool BKE_sound_is_jack_supported(void) { return false; } +char **BKE_sound_get_device_names(void) { static char *names[1] = {NULL}; return names; } + #endif /* WITH_AUDASPACE */ diff --git a/source/blender/blenkernel/intern/speaker.c b/source/blender/blenkernel/intern/speaker.c index d00e4b1a0d2..9d604a9382a 100644 --- a/source/blender/blenkernel/intern/speaker.c +++ b/source/blender/blenkernel/intern/speaker.c @@ -61,25 +61,31 @@ void *BKE_speaker_add(Main *bmain, const char *name) { Speaker *spk; - spk = BKE_libblock_alloc(bmain, ID_SPK, name); + spk = BKE_libblock_alloc(bmain, ID_SPK, name, 0); BKE_speaker_init(spk); return spk; } -Speaker *BKE_speaker_copy(Main *bmain, const Speaker *spk) +/** + * Only copy internal data of Speaker ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_speaker_copy_data(Main *UNUSED(bmain), Speaker *UNUSED(spk_dst), const Speaker *UNUSED(spk_src), const int UNUSED(flag)) { - Speaker *spkn; - - spkn = BKE_libblock_copy(bmain, &spk->id); - - if (spkn->sound) - id_us_plus(&spkn->sound->id); - - BKE_id_copy_ensure_local(bmain, &spk->id, &spkn->id); + /* Nothing to do! */ +} - return spkn; +Speaker *BKE_speaker_copy(Main *bmain, const Speaker *spk) +{ + Speaker *spk_copy; + BKE_id_copy_ex(bmain, &spk->id, (ID **)&spk_copy, 0, false); + return spk_copy; } void BKE_speaker_make_local(Main *bmain, Speaker *spk, const bool lib_local) diff --git a/source/blender/blenkernel/intern/subsurf_ccg.c b/source/blender/blenkernel/intern/subsurf_ccg.c index 1f72744bdee..d461e03fcda 100644 --- a/source/blender/blenkernel/intern/subsurf_ccg.c +++ b/source/blender/blenkernel/intern/subsurf_ccg.c @@ -42,6 +42,8 @@ #include <math.h> #include <float.h> +#include "atomic_ops.h" + #include "MEM_guardedalloc.h" #include "DNA_mesh_types.h" @@ -2197,12 +2199,12 @@ static void ccgDM_buffer_copy_color( for (S = 0; S < numVerts; S++) { for (y = 0; y < gridFaces; y++) { for (x = 0; x < gridFaces; x++) { - copy_v3_v3_uchar(&varray[start + 0], &mloopcol[iface * 16 + 0]); - copy_v3_v3_uchar(&varray[start + 3], &mloopcol[iface * 16 + 12]); - copy_v3_v3_uchar(&varray[start + 6], &mloopcol[iface * 16 + 8]); - copy_v3_v3_uchar(&varray[start + 9], &mloopcol[iface * 16 + 4]); + copy_v4_v4_uchar(&varray[start + 0], &mloopcol[iface * 16 + 0]); + copy_v4_v4_uchar(&varray[start + 4], &mloopcol[iface * 16 + 12]); + copy_v4_v4_uchar(&varray[start + 8], &mloopcol[iface * 16 + 8]); + copy_v4_v4_uchar(&varray[start + 12], &mloopcol[iface * 16 + 4]); - start += 12; + start += 16; iface++; } } @@ -3427,6 +3429,11 @@ static void ccgDM_drawMappedFaces(DerivedMesh *dm, int gridFaces = gridSize - 1, totface; int prev_mat_nr = -1; + if (ccgdm->pbvh) { + if (G.debug_value == 14) + BKE_pbvh_draw_BB(ccgdm->pbvh); + } + #ifdef WITH_OPENSUBDIV if (ccgdm->useGpuBackend) { int new_matnr; @@ -3537,16 +3544,16 @@ static void ccgDM_drawMappedFaces(DerivedMesh *dm, float *c = CCG_grid_elem_co(&key, faceGridData, x + 1, y + 1); float *d = CCG_grid_elem_co(&key, faceGridData, x, y + 1); - if (cp) glColor3ubv(&cp[4]); + if (cp) glColor4ubv(&cp[4]); glNormal3fv(ln[1]); glVertex3fv(d); - if (cp) glColor3ubv(&cp[8]); + if (cp) glColor4ubv(&cp[8]); glNormal3fv(ln[2]); glVertex3fv(c); - if (cp) glColor3ubv(&cp[12]); + if (cp) glColor4ubv(&cp[12]); glNormal3fv(ln[3]); glVertex3fv(b); - if (cp) glColor3ubv(&cp[0]); + if (cp) glColor4ubv(&cp[0]); glNormal3fv(ln[0]); glVertex3fv(a); @@ -3564,10 +3571,10 @@ static void ccgDM_drawMappedFaces(DerivedMesh *dm, a = CCG_grid_elem(&key, faceGridData, x, y + 0); b = CCG_grid_elem(&key, faceGridData, x, y + 1); - if (cp) glColor3ubv(&cp[0]); + if (cp) glColor4ubv(&cp[0]); glNormal3fv(CCG_elem_no(&key, a)); glVertex3fv(CCG_elem_co(&key, a)); - if (cp) glColor3ubv(&cp[4]); + if (cp) glColor4ubv(&cp[4]); glNormal3fv(CCG_elem_no(&key, b)); glVertex3fv(CCG_elem_co(&key, b)); @@ -3579,10 +3586,10 @@ static void ccgDM_drawMappedFaces(DerivedMesh *dm, a = CCG_grid_elem(&key, faceGridData, x, y + 0); b = CCG_grid_elem(&key, faceGridData, x, y + 1); - if (cp) glColor3ubv(&cp[12]); + if (cp) glColor4ubv(&cp[12]); glNormal3fv(CCG_elem_no(&key, a)); glVertex3fv(CCG_elem_co(&key, a)); - if (cp) glColor3ubv(&cp[8]); + if (cp) glColor4ubv(&cp[8]); glNormal3fv(CCG_elem_no(&key, b)); glVertex3fv(CCG_elem_co(&key, b)); @@ -3602,13 +3609,13 @@ static void ccgDM_drawMappedFaces(DerivedMesh *dm, ccgDM_glNormalFast(a, b, c, d); - if (cp) glColor3ubv(&cp[4]); + if (cp) glColor4ubv(&cp[4]); glVertex3fv(d); - if (cp) glColor3ubv(&cp[8]); + if (cp) glColor4ubv(&cp[8]); glVertex3fv(c); - if (cp) glColor3ubv(&cp[12]); + if (cp) glColor4ubv(&cp[12]); glVertex3fv(b); - if (cp) glColor3ubv(&cp[0]); + if (cp) glColor4ubv(&cp[0]); glVertex3fv(a); if (cp) cp += 16; @@ -4160,7 +4167,8 @@ static struct PBVH *ccgDM_getPBVH(Object *ob, DerivedMesh *dm) if (!ob->sculpt) return NULL; - grid_pbvh = ccgDM_use_grid_pbvh(ccgdm); + /* In vwpaint, we always use a grid_pbvh for multires/subsurf */ + grid_pbvh = (!(ob->mode & OB_MODE_SCULPT) || ccgDM_use_grid_pbvh(ccgdm)); if (ob->sculpt->pbvh) { if (grid_pbvh) { @@ -4176,12 +4184,18 @@ static struct PBVH *ccgDM_getPBVH(Object *ob, DerivedMesh *dm) ccgdm->pbvh = ob->sculpt->pbvh; } - if (ccgdm->pbvh) + if (ccgdm->pbvh) { + /* For vertex paint, keep track of ccgdm */ + if (!(ob->mode & OB_MODE_SCULPT)) { + BKE_pbvh_set_ccgdm(ccgdm->pbvh, ccgdm); + } return ccgdm->pbvh; + } /* no pbvh exists yet, we need to create one. only in case of multires * we build a pbvh over the modified mesh, in other cases the base mesh * is being sculpted, so we build a pbvh from that. */ + /* Note: vwpaint always builds a pbvh over the modified mesh. */ if (grid_pbvh) { ccgdm_create_grids(dm); @@ -4212,6 +4226,10 @@ static struct PBVH *ccgDM_getPBVH(Object *ob, DerivedMesh *dm) if (ccgdm->pbvh) pbvh_show_diffuse_color_set(ccgdm->pbvh, ob->sculpt->show_diffuse_color); + /* For vertex paint, keep track of ccgdm */ + if (!(ob->mode & OB_MODE_SCULPT) && ccgdm->pbvh) { + BKE_pbvh_set_ccgdm(ccgdm->pbvh, ccgdm); + } return ccgdm->pbvh; } @@ -4220,16 +4238,17 @@ static void ccgDM_recalcTessellation(DerivedMesh *UNUSED(dm)) /* Nothing to do: CCG handles creating its own tessfaces */ } +/* WARNING! *MUST* be called in an 'loops_cache_rwlock' protected thread context! */ static void ccgDM_recalcLoopTri(DerivedMesh *dm) { - BLI_rw_mutex_lock(&loops_cache_rwlock, THREAD_LOCK_WRITE); - MLoopTri *mlooptri; + MLoopTri *mlooptri = dm->looptris.array; const int tottri = dm->numPolyData * 2; int i, poly_index; DM_ensure_looptri_data(dm); - mlooptri = dm->looptris.array; + mlooptri = dm->looptris.array_wip; + BLI_assert(tottri == 0 || mlooptri != NULL); BLI_assert(poly_to_tri_count(dm->numPolyData, dm->numLoopData) == dm->looptris.num); BLI_assert(tottri == dm->looptris.num); @@ -4248,19 +4267,10 @@ static void ccgDM_recalcLoopTri(DerivedMesh *dm) lt->tri[2] = (poly_index * 4) + 2; lt->poly = poly_index; } - BLI_rw_mutex_unlock(&loops_cache_rwlock); -} - -static const MLoopTri *ccgDM_getLoopTriArray(DerivedMesh *dm) -{ - if (dm->looptris.array) { - BLI_assert(poly_to_tri_count(dm->numPolyData, dm->numLoopData) == dm->looptris.num); - } - else { - dm->recalcLoopTri(dm); - } - return dm->looptris.array; + BLI_assert(dm->looptris.array == NULL); + atomic_cas_ptr((void **)&dm->looptris.array, dm->looptris.array, dm->looptris.array_wip); + dm->looptris.array_wip = NULL; } static void ccgDM_calcNormals(DerivedMesh *dm) @@ -4279,8 +4289,6 @@ static void set_default_ccgdm_callbacks(CCGDerivedMesh *ccgdm) ccgdm->dm.getNumPolys = ccgDM_getNumPolys; ccgdm->dm.getNumTessFaces = ccgDM_getNumTessFaces; - ccgdm->dm.getLoopTriArray = ccgDM_getLoopTriArray; - ccgdm->dm.getVert = ccgDM_getFinalVert; ccgdm->dm.getEdge = ccgDM_getFinalEdge; ccgdm->dm.getTessFace = ccgDM_getFinalFace; diff --git a/source/blender/blenkernel/intern/text.c b/source/blender/blenkernel/intern/text.c index 9444e605e6d..4ae9818f891 100644 --- a/source/blender/blenkernel/intern/text.c +++ b/source/blender/blenkernel/intern/text.c @@ -223,7 +223,7 @@ Text *BKE_text_add(Main *bmain, const char *name) { Text *ta; - ta = BKE_libblock_alloc(bmain, ID_TXT, name); + ta = BKE_libblock_alloc(bmain, ID_TXT, name, 0); BKE_text_init(ta); @@ -409,7 +409,7 @@ Text *BKE_text_load_ex(Main *bmain, const char *file, const char *relpath, const return false; } - ta = BKE_libblock_alloc(bmain, ID_TXT, BLI_path_basename(filepath_abs)); + ta = BKE_libblock_alloc(bmain, ID_TXT, BLI_path_basename(filepath_abs), 0); ta->id.us = 0; BLI_listbase_clear(&ta->lines); @@ -448,53 +448,49 @@ Text *BKE_text_load(Main *bmain, const char *file, const char *relpath) return BKE_text_load_ex(bmain, file, relpath, false); } -Text *BKE_text_copy(Main *bmain, const Text *ta) +/** + * Only copy internal data of Text ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_text_copy_data(Main *UNUSED(bmain), Text *ta_dst, const Text *ta_src, const int UNUSED(flag)) { - Text *tan; - TextLine *line, *tmp; - - tan = BKE_libblock_copy(bmain, &ta->id); - /* file name can be NULL */ - if (ta->name) { - tan->name = BLI_strdup(ta->name); - } - else { - tan->name = NULL; + if (ta_src->name) { + ta_dst->name = BLI_strdup(ta_src->name); } - tan->flags = ta->flags | TXT_ISDIRTY; - - BLI_listbase_clear(&tan->lines); - tan->curl = tan->sell = NULL; - tan->compiled = NULL; - - tan->nlines = ta->nlines; + ta_dst->flags |= TXT_ISDIRTY; + + BLI_listbase_clear(&ta_dst->lines); + ta_dst->curl = ta_dst->sell = NULL; + ta_dst->compiled = NULL; - line = ta->lines.first; /* Walk down, reconstructing */ - while (line) { - tmp = (TextLine *) MEM_mallocN(sizeof(TextLine), "textline"); - tmp->line = MEM_mallocN(line->len + 1, "textline_string"); - tmp->format = NULL; - - strcpy(tmp->line, line->line); + for (TextLine *line_src = ta_src->lines.first; line_src; line_src = line_src->next) { + TextLine *line_dst = MEM_mallocN(sizeof(*line_dst), __func__); - tmp->len = line->len; - - BLI_addtail(&tan->lines, tmp); - - line = line->next; - } + line_dst->line = BLI_strdup(line_src->line); + line_dst->format = NULL; + line_dst->len = line_src->len; - tan->curl = tan->sell = tan->lines.first; - tan->curc = tan->selc = 0; + BLI_addtail(&ta_dst->lines, line_dst); + } - init_undo_text(tan); + ta_dst->curl = ta_dst->sell = ta_dst->lines.first; + ta_dst->curc = ta_dst->selc = 0; - BKE_id_copy_ensure_local(bmain, &ta->id, &tan->id); + init_undo_text(ta_dst); +} - return tan; +Text *BKE_text_copy(Main *bmain, const Text *ta) +{ + Text *ta_copy; + BKE_id_copy_ex(bmain, &ta->id, (ID **)&ta_copy, 0, false); + return ta_copy; } void BKE_text_make_local(Main *bmain, Text *text, const bool lib_local) diff --git a/source/blender/blenkernel/intern/texture.c b/source/blender/blenkernel/intern/texture.c index 1e0659d3d67..8f0334a4752 100644 --- a/source/blender/blenkernel/intern/texture.c +++ b/source/blender/blenkernel/intern/texture.c @@ -707,7 +707,7 @@ Tex *BKE_texture_add(Main *bmain, const char *name) { Tex *tex; - tex = BKE_libblock_alloc(bmain, ID_TE, name); + tex = BKE_libblock_alloc(bmain, ID_TE, name, 0); BKE_texture_default(tex); @@ -846,41 +846,71 @@ MTex *BKE_texture_mtex_add_id(ID *id, int slot) /* ------------------------------------------------------------------------- */ -Tex *BKE_texture_copy(Main *bmain, const Tex *tex) +/** + * Only copy internal data of Texture ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_texture_copy_data(Main *bmain, Tex *tex_dst, const Tex *tex_src, const int flag) { - Tex *texn; - - texn = BKE_libblock_copy(bmain, &tex->id); - if (BKE_texture_is_image_user(tex)) { - id_us_plus((ID *)texn->ima); + /* We never handle usercount here for own data. */ + const int flag_subdata = flag | LIB_ID_CREATE_NO_USER_REFCOUNT; + + if (!BKE_texture_is_image_user(tex_src)) { + tex_dst->ima = NULL; } - else { - texn->ima = NULL; + + if (tex_dst->coba) { + tex_dst->coba = MEM_dupallocN(tex_dst->coba); + } + if (tex_dst->env) { + tex_dst->env = BKE_texture_envmap_copy(tex_dst->env, flag_subdata); + } + if (tex_dst->pd) { + tex_dst->pd = BKE_texture_pointdensity_copy(tex_dst->pd, flag_subdata); + } + if (tex_dst->vd) { + tex_dst->vd = MEM_dupallocN(tex_dst->vd); + } + if (tex_dst->ot) { + tex_dst->ot = BKE_texture_ocean_copy(tex_dst->ot, flag_subdata); } - - if (texn->coba) texn->coba = MEM_dupallocN(texn->coba); - if (texn->env) texn->env = BKE_texture_envmap_copy(texn->env); - if (texn->pd) texn->pd = BKE_texture_pointdensity_copy(texn->pd); - if (texn->vd) texn->vd = MEM_dupallocN(texn->vd); - if (texn->ot) texn->ot = BKE_texture_ocean_copy(texn->ot); - if (tex->nodetree) { - if (tex->nodetree->execdata) { - ntreeTexEndExecTree(tex->nodetree->execdata); + if (tex_src->nodetree) { + if (tex_src->nodetree->execdata) { + ntreeTexEndExecTree(tex_src->nodetree->execdata); } - texn->nodetree = ntreeCopyTree(bmain, tex->nodetree); + BKE_id_copy_ex(bmain, (ID *)tex_src->nodetree, (ID **)&tex_dst->nodetree, flag, false); } - BKE_previewimg_id_copy(&texn->id, &tex->id); - - BKE_id_copy_ensure_local(bmain, &tex->id, &texn->id); + if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0) { + BKE_previewimg_id_copy(&tex_dst->id, &tex_src->id); + } + else { + tex_dst->preview = NULL; + } +} - return texn; +Tex *BKE_texture_copy(Main *bmain, const Tex *tex) +{ + Tex *tex_copy; + BKE_id_copy_ex(bmain, &tex->id, (ID **)&tex_copy, 0, false); + return tex_copy; } /* texture copy without adding to main dbase */ Tex *BKE_texture_localize(Tex *tex) { + /* TODO replace with something like + * Tex *tex_copy; + * BKE_id_copy_ex(bmain, &tex->id, (ID **)&tex_copy, LIB_ID_COPY_NO_MAIN | LIB_ID_COPY_NO_PREVIEW | LIB_ID_COPY_NO_USER_REFCOUNT, false); + * return tex_copy; + * + * ... Once f*** nodes are fully converted to that too :( */ + Tex *texn; texn = BKE_libblock_copy_nolib(&tex->id, false); @@ -889,17 +919,17 @@ Tex *BKE_texture_localize(Tex *tex) if (texn->coba) texn->coba = MEM_dupallocN(texn->coba); if (texn->env) { - texn->env = BKE_texture_envmap_copy(texn->env); + texn->env = BKE_texture_envmap_copy(texn->env, LIB_ID_CREATE_NO_USER_REFCOUNT); id_us_min(&texn->env->ima->id); } - if (texn->pd) texn->pd = BKE_texture_pointdensity_copy(texn->pd); + if (texn->pd) texn->pd = BKE_texture_pointdensity_copy(texn->pd, LIB_ID_CREATE_NO_USER_REFCOUNT); if (texn->vd) { texn->vd = MEM_dupallocN(texn->vd); if (texn->vd->dataset) texn->vd->dataset = MEM_dupallocN(texn->vd->dataset); } if (texn->ot) { - texn->ot = BKE_texture_ocean_copy(tex->ot); + texn->ot = BKE_texture_ocean_copy(tex->ot, LIB_ID_CREATE_NO_USER_REFCOUNT); } texn->preview = NULL; @@ -1099,6 +1129,8 @@ void set_active_mtex(ID *id, short act) case ID_PA: ((ParticleSettings *)id)->texact = act; break; + default: + break; } } @@ -1263,16 +1295,20 @@ EnvMap *BKE_texture_envmap_add(void) /* ------------------------------------------------------------------------- */ -EnvMap *BKE_texture_envmap_copy(const EnvMap *env) +EnvMap *BKE_texture_envmap_copy(const EnvMap *env, const int flag) { EnvMap *envn; int a; envn = MEM_dupallocN(env); envn->ok = 0; - for (a = 0; a < 6; a++) envn->cube[a] = NULL; - if (envn->ima) id_us_plus((ID *)envn->ima); - + for (a = 0; a < 6; a++) { + envn->cube[a] = NULL; + } + if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) { + id_us_plus((ID *)envn->ima); + } + return envn; } @@ -1336,14 +1372,16 @@ PointDensity *BKE_texture_pointdensity_add(void) return pd; } -PointDensity *BKE_texture_pointdensity_copy(const PointDensity *pd) +PointDensity *BKE_texture_pointdensity_copy(const PointDensity *pd, const int UNUSED(flag)) { PointDensity *pdn; pdn = MEM_dupallocN(pd); pdn->point_tree = NULL; pdn->point_data = NULL; - if (pdn->coba) pdn->coba = MEM_dupallocN(pdn->coba); + if (pdn->coba) { + pdn->coba = MEM_dupallocN(pdn->coba); + } pdn->falloff_curve = curvemapping_copy(pdn->falloff_curve); /* can be NULL */ return pdn; } @@ -1430,7 +1468,7 @@ OceanTex *BKE_texture_ocean_add(void) return ot; } -OceanTex *BKE_texture_ocean_copy(const OceanTex *ot) +OceanTex *BKE_texture_ocean_copy(const OceanTex *ot, const int UNUSED(flag)) { OceanTex *otn = MEM_dupallocN(ot); diff --git a/source/blender/blenkernel/intern/tracking.c b/source/blender/blenkernel/intern/tracking.c index 9120d384a16..b4ef381534f 100644 --- a/source/blender/blenkernel/intern/tracking.c +++ b/source/blender/blenkernel/intern/tracking.c @@ -190,7 +190,7 @@ void BKE_tracking_free(MovieTracking *tracking) } /* Copy the whole list of tracks. */ -static void tracking_tracks_copy(ListBase *tracks_dst, const ListBase *tracks_src, GHash *tracks_mapping) +static void tracking_tracks_copy(ListBase *tracks_dst, const ListBase *tracks_src, GHash *tracks_mapping, const int flag) { MovieTrackingTrack *track_dst, *track_src; @@ -202,7 +202,9 @@ static void tracking_tracks_copy(ListBase *tracks_dst, const ListBase *tracks_sr if (track_src->markers) { track_dst->markers = MEM_dupallocN(track_src->markers); } - id_us_plus(&track_dst->gpd->id); + if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) { + id_us_plus(&track_dst->gpd->id); + } BLI_addtail(tracks_dst, track_dst); BLI_ghash_insert(tracks_mapping, track_src, track_dst); } @@ -210,7 +212,8 @@ static void tracking_tracks_copy(ListBase *tracks_dst, const ListBase *tracks_sr /* copy the whole list of plane tracks (need whole MovieTracking structures due to embedded pointers to tracks). * WARNING: implies tracking_[dst/src] and their tracks have already been copied. */ -static void tracking_plane_tracks_copy(ListBase *plane_tracks_dst, const ListBase *plane_tracks_src, GHash *tracks_mapping) +static void tracking_plane_tracks_copy( + ListBase *plane_tracks_dst, const ListBase *plane_tracks_src, GHash *tracks_mapping, const int flag) { MovieTrackingPlaneTrack *plane_track_dst, *plane_track_src; @@ -225,14 +228,17 @@ static void tracking_plane_tracks_copy(ListBase *plane_tracks_dst, const ListBas for (int i = 0; i < plane_track_dst->point_tracksnr; i++) { plane_track_dst->point_tracks[i] = BLI_ghash_lookup(tracks_mapping, plane_track_src->point_tracks[i]); } - id_us_plus(&plane_track_dst->image->id); + if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) { + id_us_plus(&plane_track_dst->image->id); + } BLI_addtail(plane_tracks_dst, plane_track_dst); } } /* Copy reconstruction structure. */ static void tracking_reconstruction_copy( - MovieTrackingReconstruction *reconstruction_dst, const MovieTrackingReconstruction *reconstruction_src) + MovieTrackingReconstruction *reconstruction_dst, const MovieTrackingReconstruction *reconstruction_src, + const int UNUSED(flag)) { *reconstruction_dst = *reconstruction_src; if (reconstruction_src->cameras) { @@ -242,23 +248,25 @@ static void tracking_reconstruction_copy( /* Copy stabilization structure. */ static void tracking_stabilization_copy( - MovieTrackingStabilization *stabilization_dst, const MovieTrackingStabilization *stabilization_src) + MovieTrackingStabilization *stabilization_dst, const MovieTrackingStabilization *stabilization_src, + const int UNUSED(flag)) { *stabilization_dst = *stabilization_src; } /* Copy tracking object. */ static void tracking_object_copy( - MovieTrackingObject *object_dst, const MovieTrackingObject *object_src, GHash *tracks_mapping) + MovieTrackingObject *object_dst, const MovieTrackingObject *object_src, GHash *tracks_mapping, const int flag) { *object_dst = *object_src; - tracking_tracks_copy(&object_dst->tracks, &object_src->tracks, tracks_mapping); - tracking_plane_tracks_copy(&object_dst->plane_tracks, &object_src->plane_tracks, tracks_mapping); - tracking_reconstruction_copy(&object_dst->reconstruction, &object_src->reconstruction); + tracking_tracks_copy(&object_dst->tracks, &object_src->tracks, tracks_mapping, flag); + tracking_plane_tracks_copy(&object_dst->plane_tracks, &object_src->plane_tracks, tracks_mapping, flag); + tracking_reconstruction_copy(&object_dst->reconstruction, &object_src->reconstruction, flag); } /* Copy list of tracking objects. */ -static void tracking_objects_copy(ListBase *objects_dst, const ListBase *objects_src, GHash *tracks_mapping) +static void tracking_objects_copy( + ListBase *objects_dst, const ListBase *objects_src, GHash *tracks_mapping, const int flag) { MovieTrackingObject *object_dst, *object_src; @@ -266,22 +274,22 @@ static void tracking_objects_copy(ListBase *objects_dst, const ListBase *objects for (object_src = objects_src->first; object_src != NULL; object_src = object_src->next) { object_dst = MEM_mallocN(sizeof(*object_dst), __func__); - tracking_object_copy(object_dst, object_src, tracks_mapping); + tracking_object_copy(object_dst, object_src, tracks_mapping, flag); BLI_addtail(objects_dst, object_dst); } } /* Copy tracking structure content. */ -void BKE_tracking_copy(MovieTracking *tracking_dst, const MovieTracking *tracking_src) +void BKE_tracking_copy(MovieTracking *tracking_dst, const MovieTracking *tracking_src, const int flag) { GHash *tracks_mapping = BLI_ghash_ptr_new(__func__); *tracking_dst = *tracking_src; - tracking_tracks_copy(&tracking_dst->tracks, &tracking_src->tracks, tracks_mapping); - tracking_plane_tracks_copy(&tracking_dst->plane_tracks, &tracking_src->plane_tracks, tracks_mapping); - tracking_reconstruction_copy(&tracking_dst->reconstruction, &tracking_src->reconstruction); - tracking_stabilization_copy(&tracking_dst->stabilization, &tracking_src->stabilization); + tracking_tracks_copy(&tracking_dst->tracks, &tracking_src->tracks, tracks_mapping, flag); + tracking_plane_tracks_copy(&tracking_dst->plane_tracks, &tracking_src->plane_tracks, tracks_mapping, flag); + tracking_reconstruction_copy(&tracking_dst->reconstruction, &tracking_src->reconstruction, flag); + tracking_stabilization_copy(&tracking_dst->stabilization, &tracking_src->stabilization, flag); if (tracking_src->act_track) { tracking_dst->act_track = BLI_ghash_lookup(tracks_mapping, tracking_src->act_track); } @@ -299,7 +307,7 @@ void BKE_tracking_copy(MovieTracking *tracking_dst, const MovieTracking *trackin } /* Warning! Will override tracks_mapping. */ - tracking_objects_copy(&tracking_dst->objects, &tracking_src->objects, tracks_mapping); + tracking_objects_copy(&tracking_dst->objects, &tracking_src->objects, tracks_mapping, flag); /* Those remaining are runtime data, they will be reconstructed as needed, do not bother copying them. */ tracking_dst->dopesheet.ok = false; diff --git a/source/blender/blenkernel/intern/tracking_auto.c b/source/blender/blenkernel/intern/tracking_auto.c index 9475925cdda..30981ed8f23 100644 --- a/source/blender/blenkernel/intern/tracking_auto.c +++ b/source/blender/blenkernel/intern/tracking_auto.c @@ -312,7 +312,7 @@ AutoTrackContext *BKE_autotrack_context_new(MovieClip *clip, int num_total_tracks = BLI_listbase_count(tracksbase); context->tracks = - MEM_callocN(sizeof(MovieTrackingTrack*) * num_total_tracks, + MEM_callocN(sizeof(MovieTrackingTrack *) * num_total_tracks, "auto track pointers"); context->image_accessor = @@ -381,7 +381,7 @@ AutoTrackContext *BKE_autotrack_context_new(MovieClip *clip, bool BKE_autotrack_context_step(AutoTrackContext *context) { - int frame_delta = context->backwards ? -1 : 1; + const int frame_delta = context->backwards ? -1 : 1; bool ok = false; int track; @@ -395,67 +395,64 @@ bool BKE_autotrack_context_step(AutoTrackContext *context) libmv_reference_marker, libmv_tracked_marker; libmv_TrackRegionResult libmv_result; - int frame = BKE_movieclip_remap_scene_to_clip_frame( - context->clips[options->clip_index], - context->user.framenr); - bool has_marker; - + const int frame = BKE_movieclip_remap_scene_to_clip_frame( + context->clips[options->clip_index], + context->user.framenr); BLI_spin_lock(&context->spin_lock); - has_marker = libmv_autoTrackGetMarker(context->autotrack, - options->clip_index, - frame, - options->track_index, - &libmv_current_marker); + const bool has_marker = libmv_autoTrackGetMarker(context->autotrack, + options->clip_index, + frame, + options->track_index, + &libmv_current_marker); BLI_spin_unlock(&context->spin_lock); - - if (has_marker) { - if (!tracking_check_marker_margin(&libmv_current_marker, - options->track->margin, - context->frame_width, - context->frame_height)) - { - continue; - } - - libmv_tracked_marker = libmv_current_marker; - libmv_tracked_marker.frame = frame + frame_delta; - - if (options->use_keyframe_match) { - libmv_tracked_marker.reference_frame = - libmv_current_marker.reference_frame; - libmv_autoTrackGetMarker(context->autotrack, - options->clip_index, - libmv_tracked_marker.reference_frame, - options->track_index, - &libmv_reference_marker); - } - else { - libmv_tracked_marker.reference_frame = frame; - libmv_reference_marker = libmv_current_marker; - } - - if (libmv_autoTrackMarker(context->autotrack, - &options->track_region_options, - &libmv_tracked_marker, - &libmv_result)) - { - BLI_spin_lock(&context->spin_lock); - libmv_autoTrackAddMarker(context->autotrack, - &libmv_tracked_marker); - BLI_spin_unlock(&context->spin_lock); - } - else { - options->is_failed = true; - options->failed_frame = frame + frame_delta; - } - ok = true; + /* Check whether we've got marker to sync with. */ + if (!has_marker) { + continue; + } + /* Check whether marker is going outside of allowed frame margin. */ + if (!tracking_check_marker_margin(&libmv_current_marker, + options->track->margin, + context->frame_width, + context->frame_height)) + { + continue; + } + libmv_tracked_marker = libmv_current_marker; + libmv_tracked_marker.frame = frame + frame_delta; + /* Update reference frame. */ + if (options->use_keyframe_match) { + libmv_tracked_marker.reference_frame = + libmv_current_marker.reference_frame; + libmv_autoTrackGetMarker(context->autotrack, + options->clip_index, + libmv_tracked_marker.reference_frame, + options->track_index, + &libmv_reference_marker); } + else { + libmv_tracked_marker.reference_frame = frame; + libmv_reference_marker = libmv_current_marker; + } + /* Perform actual tracking. */ + if (libmv_autoTrackMarker(context->autotrack, + &options->track_region_options, + &libmv_tracked_marker, + &libmv_result)) + { + BLI_spin_lock(&context->spin_lock); + libmv_autoTrackAddMarker(context->autotrack, &libmv_tracked_marker); + BLI_spin_unlock(&context->spin_lock); + } + else { + options->is_failed = true; + options->failed_frame = frame + frame_delta; + } + ok = true; } - + /* Advance the frame. */ BLI_spin_lock(&context->spin_lock); context->user.framenr += frame_delta; BLI_spin_unlock(&context->spin_lock); - return ok; } diff --git a/source/blender/blenkernel/intern/tracking_util.c b/source/blender/blenkernel/intern/tracking_util.c index a95399562d5..d8e98291117 100644 --- a/source/blender/blenkernel/intern/tracking_util.c +++ b/source/blender/blenkernel/intern/tracking_util.c @@ -58,6 +58,15 @@ #include "libmv-capi.h" +/* Uncomment this to have caching-specific debug prints. */ +// #define DEBUG_CACHE + +#ifdef DEBUG_CACHE +# define CACHE_PRINTF(...) printf(__VA_ARGS__) +#else +# define CACHE_PRINTF(...) +#endif + /*********************** Tracks map *************************/ TracksMap *tracks_map_new(const char *object_name, bool is_camera, int num_tracks, int customdata_size) @@ -523,6 +532,8 @@ typedef struct AccessCacheKey { int frame; int downscale; libmv_InputMode input_mode; + bool has_region; + float region_min[2], region_max[2]; int64_t transform_key; } AccessCacheKey; @@ -537,23 +548,44 @@ static bool accesscache_hashcmp(const void *a_v, const void *b_v) { const AccessCacheKey *a = (const AccessCacheKey *) a_v; const AccessCacheKey *b = (const AccessCacheKey *) b_v; - -#define COMPARE_FIELD(field) - { \ - if (a->clip_index != b->clip_index) { \ - return false; \ - } \ - } (void) 0 - - COMPARE_FIELD(clip_index); - COMPARE_FIELD(frame); - COMPARE_FIELD(downscale); - COMPARE_FIELD(input_mode); - COMPARE_FIELD(transform_key); - -#undef COMPARE_FIELD - - return true; + if (a->clip_index != b->clip_index || + a->frame != b->frame || + a->downscale != b->downscale || + a->input_mode != b->input_mode || + a->has_region != b->has_region || + a->transform_key != b->transform_key) + { + return true; + } + /* If there is region applied, compare it. */ + if (a->has_region) { + if (!equals_v2v2(a->region_min, b->region_min) || + !equals_v2v2(a->region_max, b->region_max)) + { + return true; + } + } + return false; +} + +static void accesscache_construct_key(AccessCacheKey *key, + int clip_index, + int frame, + libmv_InputMode input_mode, + int downscale, + const libmv_Region *region, + int64_t transform_key) +{ + key->clip_index = clip_index; + key->frame = frame; + key->input_mode = input_mode; + key->downscale = downscale; + key->has_region = (region != NULL); + if (key->has_region) { + copy_v2_v2(key->region_min, region->min); + copy_v2_v2(key->region_max, region->max); + } + key->transform_key = transform_key; } static void accesscache_put(TrackingImageAccessor *accessor, @@ -561,15 +593,13 @@ static void accesscache_put(TrackingImageAccessor *accessor, int frame, libmv_InputMode input_mode, int downscale, + const libmv_Region *region, int64_t transform_key, ImBuf *ibuf) { AccessCacheKey key; - key.clip_index = clip_index; - key.frame = frame; - key.input_mode = input_mode; - key.downscale = downscale; - key.transform_key = transform_key; + accesscache_construct_key(&key, clip_index, frame, input_mode, downscale, + region, transform_key); IMB_moviecache_put(accessor->cache, &key, ibuf); } @@ -578,14 +608,12 @@ static ImBuf *accesscache_get(TrackingImageAccessor *accessor, int frame, libmv_InputMode input_mode, int downscale, + const libmv_Region *region, int64_t transform_key) { AccessCacheKey key; - key.clip_index = clip_index; - key.frame = frame; - key.input_mode = input_mode; - key.downscale = downscale; - key.transform_key = transform_key; + accesscache_construct_key(&key, clip_index, frame, input_mode, downscale, + region, transform_key); return IMB_moviecache_get(accessor->cache, &key); } @@ -674,29 +702,37 @@ static ImBuf *accessor_get_ibuf(TrackingImageAccessor *accessor, { ImBuf *ibuf, *orig_ibuf, *final_ibuf; int64_t transform_key = 0; - if (transform != NULL) { transform_key = libmv_frameAccessorgetTransformKey(transform); } - /* First try to get fully processed image from the cache. */ + BLI_spin_lock(&accessor->cache_lock); ibuf = accesscache_get(accessor, clip_index, frame, input_mode, downscale, + region, transform_key); + BLI_spin_unlock(&accessor->cache_lock); if (ibuf != NULL) { + CACHE_PRINTF("Used cached buffer for frame %d\n", frame); + /* This is a little heuristic here: if we re-used image once, this is + * a high probability of the image to be related to a keyframe matched + * reference image. Those images we don't want to be thrown away because + * if we toss them out we'll be re-calculating them at the next + * iteration. + */ + ibuf->userflags |= IB_PERSISTENT; return ibuf; } - + CACHE_PRINTF("Calculate new buffer for frame %d\n", frame); /* And now we do postprocessing of the original frame. */ orig_ibuf = accessor_get_preprocessed_ibuf(accessor, clip_index, frame); - if (orig_ibuf == NULL) { return NULL; } - + /* Cut a region if requested. */ if (region != NULL) { int width = region->max[0] - region->min[0], height = region->max[1] - region->min[1]; @@ -756,7 +792,7 @@ static ImBuf *accessor_get_ibuf(TrackingImageAccessor *accessor, BLI_unlock_thread(LOCK_MOVIECLIP); final_ibuf = orig_ibuf; } - + /* Downscale if needed. */ if (downscale > 0) { if (final_ibuf == orig_ibuf) { final_ibuf = IMB_dupImBuf(orig_ibuf); @@ -765,7 +801,7 @@ static ImBuf *accessor_get_ibuf(TrackingImageAccessor *accessor, orig_ibuf->x / (1 << downscale), orig_ibuf->y / (1 << downscale)); } - + /* Apply possible transformation. */ if (transform != NULL) { libmv_FloatImage input_image, output_image; ibuf_to_float_image(final_ibuf, &input_image); @@ -778,12 +814,13 @@ static ImBuf *accessor_get_ibuf(TrackingImageAccessor *accessor, final_ibuf = float_image_to_ibuf(&output_image); libmv_floatImageDestroy(&output_image); } - + /* Transform number of channels. */ if (input_mode == LIBMV_IMAGE_MODE_RGBA) { BLI_assert(orig_ibuf->channels == 3 || orig_ibuf->channels == 4); /* pass */ } else /* if (input_mode == LIBMV_IMAGE_MODE_MONO) */ { + BLI_assert(input_mode == LIBMV_IMAGE_MODE_MONO); if (final_ibuf->channels != 1) { ImBuf *grayscale_ibuf = make_grayscale_ibuf_copy(final_ibuf); if (final_ibuf != orig_ibuf) { @@ -793,37 +830,25 @@ static ImBuf *accessor_get_ibuf(TrackingImageAccessor *accessor, final_ibuf = grayscale_ibuf; } } - - /* it's possible processing still didn't happen at this point, + /* It's possible processing still didn't happen at this point, * but we really need a copy of the buffer to be transformed * and to be put to the cache. */ if (final_ibuf == orig_ibuf) { final_ibuf = IMB_dupImBuf(orig_ibuf); } - IMB_freeImBuf(orig_ibuf); - - /* We put postprocessed frame to the cache always for now, - * not the smartest thing in the world, but who cares at this point. - */ - - /* TODO(sergey): Disable cache for now, because we don't store region - * in the cache key and can't check whether cached version is usable for - * us or not. - * - * Need to think better about what to cache and when. - */ - if (false) { - accesscache_put(accessor, - clip_index, - frame, - input_mode, - downscale, - transform_key, - final_ibuf); - } - + BLI_spin_lock(&accessor->cache_lock); + /* Put final buffer to cache. */ + accesscache_put(accessor, + clip_index, + frame, + input_mode, + downscale, + region, + transform_key, + final_ibuf); + BLI_spin_unlock(&accessor->cache_lock); return final_ibuf; } @@ -876,7 +901,7 @@ static void accessor_release_image_callback(libmv_CacheKey cache_key) } static libmv_CacheKey accessor_get_mask_for_track_callback( - libmv_FrameAccessorUserData* user_data, + libmv_FrameAccessorUserData *user_data, int clip_index, int frame, int track_index, @@ -958,6 +983,8 @@ TrackingImageAccessor *tracking_image_accessor_new(MovieClip *clips[MAX_ACCESSOR accessor_get_mask_for_track_callback, accessor_release_mask_callback); + BLI_spin_init(&accessor->cache_lock); + return accessor; } @@ -965,5 +992,6 @@ void tracking_image_accessor_destroy(TrackingImageAccessor *accessor) { IMB_moviecache_free(accessor->cache); libmv_FrameAccessorDestroy(accessor->libmv_accessor); + BLI_spin_end(&accessor->cache_lock); MEM_freeN(accessor); } diff --git a/source/blender/blenkernel/intern/unit.c b/source/blender/blenkernel/intern/unit.c index c0a373395dc..8606da0743b 100644 --- a/source/blender/blenkernel/intern/unit.c +++ b/source/blender/blenkernel/intern/unit.c @@ -372,6 +372,12 @@ static size_t unit_as_string(char *str, int len_max, double value, int prec, con value_conv = value / unit->scalar; + /* Adjust precision to expected number of significant digits. + * Note that here, we shall not have to worry about very big/small numbers, units are expected to replace + * 'scientific notation' in those cases. */ + prec -= integer_digits_d(value_conv); + CLAMP(prec, 0, 6); + /* Convert to a string */ len = BLI_snprintf_rlen(str, len_max, "%.*f", prec, value_conv); @@ -442,12 +448,15 @@ size_t bUnit_AsString(char *str, int len_max, double value, int prec, int system size_t i; i = unit_as_string(str, len_max, value_a, prec, usys, unit_a, '\0'); + prec -= integer_digits_d(value_a / unit_b->scalar) - integer_digits_d(value_b / unit_b->scalar); + prec = max_ii(prec, 0); + /* is there enough space for at least 1 char of the next unit? */ if (i + 2 < len_max) { str[i++] = ' '; /* use low precision since this is a smaller unit */ - i += unit_as_string(str + i, len_max - i, value_b, prec ? 1 : 0, usys, unit_b, '\0'); + i += unit_as_string(str + i, len_max - i, value_b, prec, usys, unit_b, '\0'); } return i; } diff --git a/source/blender/blenkernel/intern/workspace.c b/source/blender/blenkernel/intern/workspace.c index 82fface7d06..05dab9208e5 100644 --- a/source/blender/blenkernel/intern/workspace.c +++ b/source/blender/blenkernel/intern/workspace.c @@ -117,11 +117,11 @@ static void *workspace_relation_get_data_matching_parent( * Hence, this should only be used as assert check before assigining a screen to a workspace. */ #ifndef NDEBUG -static bool workspaces_is_screen_used( +static bool workspaces_is_screen_used #else -static bool UNUSED_FUNCTION(workspaces_is_screen_used)( +static bool UNUSED_FUNCTION(workspaces_is_screen_used) #endif - const Main *bmain, bScreen *screen) + (const Main *bmain, bScreen *screen) { for (WorkSpace *workspace = bmain->workspaces.first; workspace; workspace = workspace->id.next) { if (workspace_layout_find_exec(workspace, screen)) { @@ -137,7 +137,7 @@ static bool UNUSED_FUNCTION(workspaces_is_screen_used)( WorkSpace *BKE_workspace_add(Main *bmain, const char *name) { - WorkSpace *new_workspace = BKE_libblock_alloc(bmain, ID_WS, name); + WorkSpace *new_workspace = BKE_libblock_alloc(bmain, ID_WS, name, 0); return new_workspace; } diff --git a/source/blender/blenkernel/intern/world.c b/source/blender/blenkernel/intern/world.c index 363c36e644d..cbe00e3bbc6 100644 --- a/source/blender/blenkernel/intern/world.c +++ b/source/blender/blenkernel/intern/world.c @@ -112,43 +112,59 @@ World *add_world(Main *bmain, const char *name) { World *wrld; - wrld = BKE_libblock_alloc(bmain, ID_WO, name); + wrld = BKE_libblock_alloc(bmain, ID_WO, name, 0); BKE_world_init(wrld); return wrld; } -World *BKE_world_copy(Main *bmain, const World *wrld) +/** + * Only copy internal data of World ID from source to already allocated/initialized destination. + * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs. + * + * WARNING! This function will not handle ID user count! + * + * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). + */ +void BKE_world_copy_data(Main *bmain, World *wrld_dst, const World *wrld_src, const int flag) { - World *wrldn; - int a; - - wrldn = BKE_libblock_copy(bmain, &wrld->id); - - for (a = 0; a < MAX_MTEX; a++) { - if (wrld->mtex[a]) { - wrldn->mtex[a] = MEM_mallocN(sizeof(MTex), "BKE_world_copy"); - memcpy(wrldn->mtex[a], wrld->mtex[a], sizeof(MTex)); - id_us_plus((ID *)wrldn->mtex[a]->tex); + for (int a = 0; a < MAX_MTEX; a++) { + if (wrld_src->mtex[a]) { + wrld_dst->mtex[a] = MEM_dupallocN(wrld_src->mtex[a]); } } - if (wrld->nodetree) { - wrldn->nodetree = ntreeCopyTree(bmain, wrld->nodetree); + if (wrld_src->nodetree) { + BKE_id_copy_ex(bmain, (ID *)wrld_src->nodetree, (ID **)&wrld_dst->nodetree, flag, false); } - - BKE_previewimg_id_copy(&wrldn->id, &wrld->id); - BLI_listbase_clear(&wrldn->gpumaterial); + BLI_listbase_clear(&wrld_dst->gpumaterial); - BKE_id_copy_ensure_local(bmain, &wrld->id, &wrldn->id); + if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0) { + BKE_previewimg_id_copy(&wrld_dst->id, &wrld_src->id); + } + else { + wrld_dst->preview = NULL; + } +} - return wrldn; +World *BKE_world_copy(Main *bmain, const World *wrld) +{ + World *wrld_copy; + BKE_id_copy_ex(bmain, &wrld->id, (ID **)&wrld_copy, 0, false); + return wrld_copy; } World *localize_world(World *wrld) { + /* TODO replace with something like + * World *wrld_copy; + * BKE_id_copy_ex(bmain, &wrld->id, (ID **)&wrld_copy, LIB_ID_COPY_NO_MAIN | LIB_ID_COPY_NO_PREVIEW | LIB_ID_COPY_NO_USER_REFCOUNT, false); + * return wrld_copy; + * + * ... Once f*** nodes are fully converted to that too :( */ + World *wrldn; int a; @@ -175,3 +191,14 @@ void BKE_world_make_local(Main *bmain, World *wrld, const bool lib_local) { BKE_id_make_local_generic(bmain, &wrld->id, true, lib_local); } + +void BKE_world_eval(const struct EvaluationContext *UNUSED(eval_ctx), World *world) +{ + if (G.debug & G_DEBUG_DEPSGRAPH) { + printf("%s on %s (%p)\n", __func__, world->id.name, world); + } + if (!BLI_listbase_is_empty(&world->gpumaterial)) { + world->update_flag = 1; + GPU_material_uniform_buffer_tag_dirty(&world->gpumaterial); + } +} diff --git a/source/blender/blenkernel/intern/writeavi.c b/source/blender/blenkernel/intern/writeavi.c index 89b2caa5ac7..2fb4ed03603 100644 --- a/source/blender/blenkernel/intern/writeavi.c +++ b/source/blender/blenkernel/intern/writeavi.c @@ -80,10 +80,6 @@ static void *context_create_avi(void); static void context_free_avi(void *context_v); #endif /* WITH_AVI */ -#ifdef WITH_QUICKTIME -# include "quicktime_export.h" -#endif - #ifdef WITH_FFMPEG # include "BKE_writeffmpeg.h" #endif @@ -115,16 +111,6 @@ bMovieHandle *BKE_movie_handle_get(const char imtype) #endif /* do the platform specific handles */ -#ifdef WITH_QUICKTIME - if (imtype == R_IMF_IMTYPE_QUICKTIME) { - mh.start_movie = start_qt; - mh.append_movie = append_qt; - mh.end_movie = end_qt; - mh.get_movie_path = filepath_qt; - mh.context_create = context_create_qt; - mh.context_free = context_free_qt; - } -#endif #ifdef WITH_FFMPEG if (ELEM(imtype, R_IMF_IMTYPE_FFMPEG, R_IMF_IMTYPE_H264, R_IMF_IMTYPE_XVID, R_IMF_IMTYPE_THEORA)) { mh.start_movie = BKE_ffmpeg_start; diff --git a/source/blender/blenkernel/intern/writeffmpeg.c b/source/blender/blenkernel/intern/writeffmpeg.c index 156b74f5c3d..b47846cbdec 100644 --- a/source/blender/blenkernel/intern/writeffmpeg.c +++ b/source/blender/blenkernel/intern/writeffmpeg.c @@ -45,8 +45,8 @@ #include "BLI_blenlib.h" #ifdef WITH_AUDASPACE -# include AUD_DEVICE_H -# include AUD_SPECIAL_H +# include <AUD_Device.h> +# include <AUD_Special.h> #endif #include "BLI_utildefines.h" @@ -681,6 +681,7 @@ static AVStream *alloc_video_stream(FFMpegContext *context, RenderData *rd, int /* xasp & yasp got float lately... */ st->sample_aspect_ratio = c->sample_aspect_ratio = av_d2q(((double) rd->xasp / (double) rd->yasp), 255); + st->avg_frame_rate = av_inv_q(c->time_base); set_ffmpeg_properties(rd, c, "video", &opts); |