Geometry Nodes: free old bake when starting new bake

Previously, the old bake was only overwritten file by file. This caused issues like
the one fixed in 7d77691a83. Also, it could result in invalid
bake data if some bake is canceled and the data on disk is a mix of an old and
new bake.
This commit is contained in:
Jacques Lucke
2024-06-03 18:56:02 +02:00
parent 38e398b63e
commit 4fe672d7f5

View File

@@ -390,6 +390,57 @@ static void reset_old_bake_cache(NodeBakeRequest &request)
}
}
static void try_delete_bake(
bContext *C, Object &object, NodesModifierData &nmd, const int bake_id, ReportList *reports)
{
Main *bmain = CTX_data_main(C);
if (!nmd.runtime->cache) {
return;
}
bake::ModifierCache &modifier_cache = *nmd.runtime->cache;
std::lock_guard lock{modifier_cache.mutex};
if (auto *node_cache = modifier_cache.simulation_cache_by_id.lookup_ptr(bake_id)) {
(*node_cache)->reset();
}
else if (auto *node_cache = modifier_cache.bake_cache_by_id.lookup_ptr(bake_id)) {
(*node_cache)->reset();
}
NodesModifierBake *bake = nmd.find_bake(bake_id);
if (!bake) {
return;
}
clear_data_block_references(*bake);
const std::optional<bake::BakePath> bake_path = bake::get_node_bake_path(
*bmain, object, nmd, bake_id);
if (!bake_path) {
return;
}
const char *meta_dir = bake_path->meta_dir.c_str();
if (BLI_exists(meta_dir)) {
if (BLI_delete(meta_dir, true, true)) {
BKE_reportf(reports, RPT_ERROR, "Failed to remove metadata directory %s", meta_dir);
}
}
const char *blobs_dir = bake_path->blobs_dir.c_str();
if (BLI_exists(blobs_dir)) {
if (BLI_delete(blobs_dir, true, true)) {
BKE_reportf(reports, RPT_ERROR, "Failed to remove blobs directory %s", blobs_dir);
}
}
if (bake_path->bake_dir.has_value()) {
const char *zone_bake_dir = bake_path->bake_dir->c_str();
/* Try to delete zone bake directory if it is empty. */
BLI_delete(zone_bake_dir, true, false);
}
if (const std::optional<std::string> modifier_bake_dir = bake::get_modifier_bake_path(
*bmain, object, nmd))
{
/* Try to delete modifier bake directory if it is empty. */
BLI_delete(modifier_bake_dir->c_str(), true, false);
}
}
enum class BakeRequestsMode {
/**
* Bake all requests before returning from the function.
@@ -411,6 +462,7 @@ static int start_bake_job(bContext *C,
if (NodesModifierBake *bake = request.nmd->find_bake(request.bake_id)) {
clear_data_block_references(*bake);
}
try_delete_bake(C, *request.object, *request.nmd, request.bake_id, op->reports);
}
BakeGeometryNodesJob *job = MEM_new<BakeGeometryNodesJob>(__func__);
@@ -698,57 +750,6 @@ static int bake_simulation_modal(bContext *C, wmOperator * /*op*/, const wmEvent
return OPERATOR_PASS_THROUGH;
}
static void try_delete_bake(
bContext *C, Object &object, NodesModifierData &nmd, const int bake_id, ReportList *reports)
{
Main *bmain = CTX_data_main(C);
if (!nmd.runtime->cache) {
return;
}
bake::ModifierCache &modifier_cache = *nmd.runtime->cache;
std::lock_guard lock{modifier_cache.mutex};
if (auto *node_cache = modifier_cache.simulation_cache_by_id.lookup_ptr(bake_id)) {
(*node_cache)->reset();
}
else if (auto *node_cache = modifier_cache.bake_cache_by_id.lookup_ptr(bake_id)) {
(*node_cache)->reset();
}
NodesModifierBake *bake = nmd.find_bake(bake_id);
if (!bake) {
return;
}
clear_data_block_references(*bake);
const std::optional<bake::BakePath> bake_path = bake::get_node_bake_path(
*bmain, object, nmd, bake_id);
if (!bake_path) {
return;
}
const char *meta_dir = bake_path->meta_dir.c_str();
if (BLI_exists(meta_dir)) {
if (BLI_delete(meta_dir, true, true)) {
BKE_reportf(reports, RPT_ERROR, "Failed to remove metadata directory %s", meta_dir);
}
}
const char *blobs_dir = bake_path->blobs_dir.c_str();
if (BLI_exists(blobs_dir)) {
if (BLI_delete(blobs_dir, true, true)) {
BKE_reportf(reports, RPT_ERROR, "Failed to remove blobs directory %s", blobs_dir);
}
}
if (bake_path->bake_dir.has_value()) {
const char *zone_bake_dir = bake_path->bake_dir->c_str();
/* Try to delete zone bake directory if it is empty. */
BLI_delete(zone_bake_dir, true, false);
}
if (const std::optional<std::string> modifier_bake_dir = bake::get_modifier_bake_path(
*bmain, object, nmd))
{
/* Try to delete modifier bake directory if it is empty. */
BLI_delete(modifier_bake_dir->c_str(), true, false);
}
}
static int delete_baked_simulation_exec(bContext *C, wmOperator *op)
{
Vector<Object *> objects;