Fix retained background lifecycle

This commit is contained in:
gamer147
2026-07-12 00:00:45 -04:00
parent fb3a7206ce
commit 96e5306148
13 changed files with 195 additions and 42 deletions

View File

@@ -20,6 +20,7 @@ public class AnimChannelTests
{
(0x55, new[]{G(1), I(0xcf3a)}), (0x55, new[]{G(2), I(0)}),
(0x55, new[]{G(3), I(360)}), (0x55, new[]{G(4), I(20)}), (0x55, new[]{G(5), I(0)}),
(0x1fb, new[]{G(1), I(0), I(0), I(0), I(1), I(1), I(0), I(0)}),
(0x22f, new[]{G(1), G(2), G(3), G(4), G(5)}),
(0x55, new[]{G(6), I(300)}), (0x55, new[]{G(7), I(40)}),
(0x55, new[]{G(8), I(20)}), (0x51, new[]{G(8), I(0), G(8)}),
@@ -62,6 +63,7 @@ public class AnimChannelTests
(0x55, new[]{G(4), I(360)}), (0x55, new[]{G(5), I(20)}), (0x55, new[]{G(6), I(0)}),
(0x55, new[]{G(40), I(20)}), (0x51, new[]{G(40), I(0), G(40)}),
(0x55, new[]{G(41), I(60)}), (0x51, new[]{G(41), I(0), G(41)}),
(0x1fb, new[]{G(1), I(0), I(0), I(0), I(1), I(1), I(0), I(0)}),
(0x22f, new[]{G(1), G(2), G(4), G(5), G(6)}),
(0x228, new[]{G(10), G(1), G(11), G(12), G(13)}),

View File

@@ -13,6 +13,7 @@ public class GfxAnimationTests
public void ScaleAndTranslationChannels_AreIndependent()
{
var g = new GfxState();
g.BindDraw(0x1000, 1, 0, 0, 1, 1, 0, 0);
g.SetScaleChannel(0x1000, delayMs: 7, durationMs: 900, percent: (200, 50, 100));
g.SetTranslationChannel(0x1000, delayMs: 9, durationMs: 1200, target: (80, -25, 6));
var o = g.TryGet(0x1000)!;
@@ -44,6 +45,7 @@ public class GfxAnimationTests
public void RotationCycle_DoesNotOverwriteMatrixChannels()
{
var g = new GfxState();
g.BindDraw(0x1000, 1, 0, 0, 1, 1, 0, 0);
g.SetScaleChannel(0x1000, 0, 100, (150, 150, 100));
g.SetTranslationChannel(0x1000, 0, 100, (10, 20, 0));
g.SetRotationCycle(0x1000, periodMs: 30, axis: (0, 0, 5));
@@ -55,6 +57,30 @@ public class GfxAnimationTests
Assert.True(o.RotationEnabled);
}
[Fact]
public void PreBindMatrixChannels_CreateNeutralPlaceholderWithoutLatentMotion()
{
// SC0000 calls op 0x220 on background handle 0xcb20 before BG001A is bound. Native creates the
// object (so op 0x215 returns slot 0) but ignores all three matrix setters while visible bit 0 is clear.
var g = new GfxState();
g.SetScaleChannel(0xcb20, 0, 500, (200, 200, 100));
g.SetRotationChannel(0xcb20, 0, 500, (0, 0, 1), 90);
g.SetTranslationChannel(0xcb20, 0, 500, (0, 600, 0));
var placeholder = g.TryGet(0xcb20)!;
Assert.Equal(0, g.QuerySlot(0xcb20));
Assert.False(placeholder.Visible);
Assert.False(placeholder.ScaleEnabled);
Assert.False(placeholder.RotationChannelEnabled);
Assert.False(placeholder.TranslationEnabled);
Assert.Equal((0.0, 0.0, 0.0), placeholder.TranslationTarget);
g.BindDraw(0xcb20, 4, 0, 0, 800, 500, 0, -500);
g.SetTranslationChannel(0xcb20, 0, 500, (0, 600, 0));
Assert.True(placeholder.TranslationEnabled);
Assert.Equal((0.0, 600.0, 0.0), placeholder.TranslationTarget);
}
[Fact]
public void ActiveVisualPresentation_ExcludesStaticWaits_ButIncludesAmbientChannels()
{
@@ -159,6 +185,7 @@ public class GfxAnimationTests
var scene = ScriptAssembler.Assemble(t, "ANIM", new List<(int, Operand[])>
{
MovGI(1, 0x1000), MovGI(2, 7), MovGI(3, 9), MovGI(4, 800), MovGI(5, 500), MovGI(6, 0),
(0x1fb, new[] { G(1), I(0), I(0), I(0), I(1), I(1), I(0), I(0) }),
(0x220, new[] { G(1), G(2), G(3), G(4), G(5), G(6) }),
MovGI(4, 200), MovGI(5, 50), MovGI(6, 100),
(0x21e, new[] { G(1), G(2), G(3), G(4), G(5), G(6) }),
@@ -215,6 +242,23 @@ public class GfxAnimationTests
Assert.Equal(1, vm.Gfx.AnimClockGeneration);
}
[Fact]
public void CurrentTranslationSetter_ReplacesTheLiveMatrixImmediately()
{
var t = T();
var scene = ScriptAssembler.Assemble(t, "CURRENTTRANSLATION", new List<(int, Operand[])>
{
MovGI(1, 0xcb20), MovGI(2, 12), MovGI(3, 34), MovGI(4, 5),
(0x1ff, new[] { G(1), G(2), G(3), G(4) }),
Exit(),
}, System.Array.Empty<string>());
var vm = new VirtualMachine(scene, t, new RecordingHost());
vm.Run();
var o = vm.Gfx.TryGet(0xcb20)!;
Assert.Equal((12.0, 34.0, 5.0), o.TranslationCurrent);
Assert.Equal((12L, 34L, 5L), o.V16c);
}
[Fact]
public void ResetAnimClock_DispatchClearsOnlyGlobalServiceClock()
{

View File

@@ -39,10 +39,10 @@ public class GfxCommandBufferTests
private static (int, Operand[]) Register(int handle) => (0x1a2, new[] { G(handle) });
[Fact]
public void QueryReturnsMinusOneUntilDrawBound_ThenSourceSlot()
public void QueryReturnsZeroForCreatedUnboundObject_ThenBoundSourceSlot()
{
// Op 0x215 returns obj+4 from the retained gfx object. Geometry creates the object but leaves it unbound;
// op 0x1a2's descriptor registry is unrelated. Draw-texture binds the source slot returned by the query.
// Op 0x215 returns obj+4 from the retained gfx object. Native initialization leaves an unbound object's
// field at zero; op 0x1a2 is unrelated. Draw-texture replaces it with the bound source slot.
var t = T();
var scene = ScriptAssembler.Assemble(t, "GFX", new List<(int, Operand[])>
{
@@ -55,7 +55,7 @@ public class GfxCommandBufferTests
}, System.Array.Empty<string>());
var vm = new VirtualMachine(scene, t, new RecordingHost());
vm.Run();
Assert.Equal(-1, vm.Globals[10]);
Assert.Equal(0, vm.Globals[10]);
Assert.Equal(6, vm.Globals[11]);
}

View File

@@ -6,20 +6,41 @@ public class GfxStateTests
[Fact]
public void QueryReturnsBoundSourceSlot_NotOperandRegistryValue()
{
// Native op 0x215 queries the retained-object map and returns obj+4, the source slot set by draw-texture.
// Geometry alone creates an object but leaves obj+4 at -1. Op 0x1a2 is a separate descriptor registry.
// Native op 0x215 queries the retained-object map and returns obj+4. The default initializer zeroes
// that field, while draw-texture replaces it with the bound slot. Op 0x1a2 is a separate registry.
var g = new GfxState();
g.GetOrCreate(0xcb2a).V18 = (400, 600, 0);
Assert.Equal(-1, g.QuerySlot(0xcb2a));
Assert.Equal(0, g.QuerySlot(0xcb2a));
g.Register(0xcb2a);
Assert.Equal(-1, g.QuerySlot(0xcb2a));
Assert.Equal(0, g.QuerySlot(0xcb2a));
g.BindDraw(0xcb2a, 6, 0, 0, 200, 200, 10, 20);
Assert.Equal(6, g.QuerySlot(0xcb2a));
Assert.Equal(-1, g.QuerySlot(0x9999));
}
[Fact]
public void UnboundMatrixSetterCreatesQueryableObjectForCleanupBeforeReuse()
{
// Native matrix setters create a neutral placeholder even when they ignore the channel because it is
// not draw-bound. obj+4 still defaults to slot 0, so the later query >= 0 cleanup erases the object.
var g = new GfxState();
g.SetTranslationChannel(0xcb2a, 0, 150, (-100, 0, 0));
g.SetRotationChannel(0xcb2a, 0, 150, (0, 0, 1), -90);
Assert.Equal(0, g.QuerySlot(0xcb2a));
if (g.QuerySlot(0xcb2a) >= 0)
g.EraseRange(0xcb2a, 10);
g.SetSurface(5, 0x7a, 0);
g.BindDraw(0xcb2a, 5, 0, 0, 800, 600, 0, 0);
var reused = Assert.Single(g.SnapshotVisibleObjects(1000));
Assert.Equal((0.0, 0.0, 0.0),
(reused.Transform.TranslateX, reused.Transform.TranslateY, reused.Transform.RotationAngleDegrees));
Assert.Equal(255, reused.Alpha);
}
[Fact]
public void VectorsRoundTripPerObject()
{