Fix bug with missing baked SDF data on save when asset data chunk memory was freed by GC

This commit is contained in:
Wojtek Figat
2024-08-10 20:45:59 +02:00
parent 7224494ce6
commit 97ab8940f0
3 changed files with 14 additions and 4 deletions

View File

@@ -682,7 +682,11 @@ bool Model::GenerateSDF(float resolutionScale, int32 lodIndex, bool cacheData, f
#if USE_EDITOR
// Set asset data
if (cacheData)
GetOrCreateChunk(15)->Data.Copy(sdfStream.GetHandle(), sdfStream.GetPosition());
{
auto chunk = GetOrCreateChunk(15);
chunk->Data.Copy(sdfStream.GetHandle(), sdfStream.GetPosition());
chunk->Flags |= FlaxChunkFlags::KeepInMemory; // Prevent GC-ing chunk data so it will be properly saved
}
#endif
return false;

View File

@@ -11,7 +11,7 @@
enum class FlaxChunkFlags
{
/// <summary>
/// The none.
/// Nothing.
/// </summary>
None = 0,
@@ -19,6 +19,11 @@ enum class FlaxChunkFlags
/// Compress chunk data using LZ4 algorithm.
/// </summary>
CompressedLZ4 = 1,
/// <summary>
/// Prevents chunk file data from being unloaded if unused for a certain amount of time. Runtime-only flag, not saved with the asset.
/// </summary>
KeepInMemory = 2,
};
DECLARE_ENUM_OPERATORS(FlaxChunkFlags);

View File

@@ -943,7 +943,8 @@ bool FlaxStorage::Create(WriteStream* stream, const AssetInitData* data, int32 d
{
FlaxChunk* chunk = chunks[i];
stream->WriteBytes(&chunk->LocationInFile, sizeof(chunk->LocationInFile));
stream->WriteInt32((int32)chunk->Flags);
FlaxChunkFlags flags = chunk->Flags & ~(FlaxChunkFlags::KeepInMemory); // Skip saving runtime-only flags
stream->WriteInt32((int32)flags);
}
#if ASSETS_LOADING_EXTRA_VERIFICATION
@@ -1410,7 +1411,7 @@ void FlaxStorage::Tick(double time)
{
auto chunk = _chunks.Get()[i];
const bool wasUsed = (time - chunk->LastAccessTime) < unusedDataChunksLifetime;
if (!wasUsed && chunk->IsLoaded())
if (!wasUsed && chunk->IsLoaded() && EnumHasNoneFlags(chunk->Flags, FlaxChunkFlags::KeepInMemory))
{
chunk->Unload();
}