Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Components/Managers/ChiselDecalManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public static class ChiselDecalManager
// Cached until registration or material metadata changes.
static readonly Dictionary<ChiselDecalComponent, DecalState> s_DecalStates = new();
static bool s_Dirty = true;
// ChiselDecalStore.Version when the trees' decal counts were last compared with what was given them
static int s_SeenDecalsVersion = -1;

sealed class DecalState
{
Expand Down Expand Up @@ -188,6 +190,9 @@ public static void Update()
return;
}

// a tree's decal count changes only when the decal store's version does
var countsMayDiffer = ChiselDecalStore.Version != s_SeenDecalsVersion;
s_SeenDecalsVersion = ChiselDecalStore.Version;
foreach (var model in s_ModelList)
{
if (!s_Models.TryGetValue(model, out var state))
Expand All @@ -196,7 +201,7 @@ public static void Update()
s_Models.Add(model, state);
}
// A rebuilt tree may reuse a handle, so also compare its decal count.
if (state.tree != model.Node || state.tree.GetDecalCount() != state.previous.Count)
if (state.tree != model.Node || (countsMayDiffer && state.tree.GetDecalCount() != state.previous.Count))
{
state.tree = model.Node;
state.previous.Clear();
Expand Down Expand Up @@ -381,6 +386,8 @@ public static void Update()
state.previousOutlines.Clear();
state.previousOutlines.AddRange(state.outlines);
}
// what this update gave the trees moved the version, and is what they have now
s_SeenDecalsVersion = ChiselDecalStore.Version;
s_Dirty = false;
}
finally
Expand Down
18 changes: 18 additions & 0 deletions Components/Tests/ChiselDecalComponentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,24 @@ public void Rebuild_KeepsTheDecals()
Assert.That(UpAreas(model).decal, Is.EqualTo(1.0).Within(kTolerance));
}

// The decal manager reads each tree's decal count only after the decal store says some tree's decals may have
// changed: planted here, decals taken from the tree without the store saying so are not noticed until it does
[Test]
public void ATreesDecals_AreLookedAtAgain_OnlyWhenTheStoreChanged()
{
var model = Floors(out _, out _, out _);
Update();
Assert.That(model.Node.GetDecalCount(), Is.EqualTo(1));

ChiselTreeLookup.Value[model.Node].decalInstances.Clear();
Update();
Assert.That(model.Node.GetDecalCount(), Is.EqualTo(0), "not looked at: nothing said the decals changed");

ChiselDecalStore.DecalsChanged();
Update();
Assert.That(model.Node.GetDecalCount(), Is.EqualTo(1), "looked at again, and given its decal");
}

[Test]
public void Targets_LimitTheDecalToTheirSurfaces()
{
Expand Down
2 changes: 2 additions & 0 deletions Core/2.Processing/Cache/ChiselLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ internal static void ReleaseTree(CSGTree treeNode)
_singleton.chiselTreeData.Remove(treeNode);
data.lastJobHandle.Complete();
data.Dispose();
ChiselDecalStore.DecalsChanged();
}

/// <summary>Releases the caches of every tree, for when all trees go at once.</summary>
Expand All @@ -411,6 +412,7 @@ void DisposeAllTrees()
data.Dispose();
}
chiselTreeData.Clear();
ChiselDecalStore.DecalsChanged();
}

void Dispose()
Expand Down
7 changes: 7 additions & 0 deletions Core/2.Processing/Decals/ChiselDecalStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ struct BySecond : IComparer<int4>

static bool IsEmpty(in MinMaxAABB bounds) => math.any(bounds.Min > bounds.Max);

// Goes up whenever the decals of any tree may have changed: given (SetDecals), or gone with their tree
// (ChiselTreeLookup.ReleaseTree and ReleaseAllTrees). A tree's decal count only changes when it does.
internal static int Version { get; private set; }

internal static void DecalsChanged() => Version++;

public static void SetDecals(CSGTree tree, NativeArray<ChiselDecalInstance> decals, NativeArray<ChiselDecalTarget> targets,
NativeArray<float2> outlines)
{
Expand Down Expand Up @@ -195,6 +201,7 @@ public static void SetDecals(CSGTree tree, NativeArray<ChiselDecalInstance> deca

data.decalInstances.Clear();
data.decalInstances.AddRange(sorted.AsArray());
DecalsChanged();
data.decalSequences.Clear();
data.decalSequences.AddRange(sequences.AsArray());
data.decalBounds.Clear();
Expand Down
30 changes: 30 additions & 0 deletions Core/Tests/ChiselTreeLookupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,36 @@ public void DestroyingATree_FreesItsHierarchy()
}
}

// A tree's decals change only when they are given (SetDecals) or go with the tree: the decal store's version
// tells (ChiselDecalManager reads the trees' decal counts only when it moved)
[Test]
public void TheDecalStoresVersion_MovesWhenATreesDecalsCanHaveChanged()
{
var tree = CSGTree.Create(default(UnityEngine.EntityId));
try
{
var version = ChiselDecalStore.Version;
tree.ClearDecals();
Assert.That(ChiselDecalStore.Version, Is.Not.EqualTo(version), "decals given");

version = ChiselDecalStore.Version;
_ = tree.GetDecalCount();
Assert.That(ChiselDecalStore.Version, Is.EqualTo(version), "only read");

tree.Destroy();
Assert.That(ChiselDecalStore.Version, Is.Not.EqualTo(version), "the tree went, with its decals");

version = ChiselDecalStore.Version;
ChiselTreeLookup.ReleaseAllTrees();
Assert.That(ChiselDecalStore.Version, Is.Not.EqualTo(version), "every tree's decals went");
}
finally
{
if (tree.Valid)
tree.Destroy();
}
}

[Test]
public void DestroyingATree_LeavesOtherTreesAlone()
{
Expand Down
Loading