diff --git a/engine/Age.Cli/Program.cs b/engine/Age.Cli/Program.cs index 8297b44..babe1cb 100644 --- a/engine/Age.Cli/Program.cs +++ b/engine/Age.Cli/Program.cs @@ -99,10 +99,10 @@ if (args[0] == "gfx") var gfxObjs = vm.Gfx.Objects.OrderBy(o => o.Slot).ToList(); Console.WriteLine($" gfx objects: {gfxObjs.Count} -> " + string.Join(", ", gfxObjs.Select(o => $"0x{o.Handle:x}=slot{o.Slot}"))); - var layers = vm.Gfx.SnapshotLayers(); - Console.WriteLine($" layers ({layers.Count}, composite order):"); - foreach (var l in layers) - Console.WriteLine($" h=0x{l.Handle:x} slot={l.Slot} src=({l.SrcX},{l.SrcY} {l.W}x{l.H}) dst=({l.DstX},{l.DstY})"); + var vis = vm.Gfx.SnapshotVisibleObjects(); + Console.WriteLine($" visible objects ({vis.Count}, ascending-handle = z-order):"); + foreach (var v in vis) + Console.WriteLine($" h=0x{v.Handle:x} surf=0x{v.SurfaceResId:x} ({res.Resolve(sceneKey, v.SurfaceResId)?.Name ?? "?"}) src=({v.SrcX},{v.SrcY} {v.W}x{v.H}) dst=({v.DstX},{v.DstY})"); return 0; } diff --git a/engine/Age.Engine.Tests/GfxCommandBufferTests.cs b/engine/Age.Engine.Tests/GfxCommandBufferTests.cs index 8a433d4..aa4b191 100644 --- a/engine/Age.Engine.Tests/GfxCommandBufferTests.cs +++ b/engine/Age.Engine.Tests/GfxCommandBufferTests.cs @@ -96,20 +96,25 @@ public class GfxCommandBufferTests private static (int, Operand[]) DrawTex(int handle, int slot, int w, int h, int dx, int dy) => (0x1fb, new[] { G(handle), G(slot), I(0), I(0), G(w), G(h), G(dx), G(dy) }); + private static (int, Operand[]) SetTex(int resId, int slot) => (0x1f9, new[] { G(resId), G(slot), I(0) }); + [Fact] - public void DrawTextureRecordsARetainedLayer() + public void SetThenDrawTextureMakesAVisibleObjectFromTheSurface() { var t = T(); var scene = ScriptAssembler.Assemble(t, "GFX", new List<(int, Operand[])> { - MovGI(1, 0xA), MovGI(2, 4), MovGI(3, 800), MovGI(4, 600), MovGI(5, 0), MovGI(6, 0), - DrawTex(1, 2, 3, 4, 5, 6), Exit(), + MovGI(1, 0xA), MovGI(2, 4), MovGI(7, 0x25), MovGI(3, 800), MovGI(4, 600), MovGI(5, 0), MovGI(6, 0), + SetTex(7, 2), // load resId 0x25 into surface slot 4 + DrawTex(1, 2, 3, 4, 5, 6), // object 0xA draws surface 4 at (0,0) + Exit(), }, System.Array.Empty()); var vm = new VirtualMachine(scene, t, new RecordingHost()); vm.Run(); - var layers = vm.Gfx.SnapshotLayers(); - Assert.Single(layers); - Assert.Equal(0xA, layers[0].Handle); - Assert.Equal((800, 600, 0, 0), (layers[0].W, layers[0].H, layers[0].DstX, layers[0].DstY)); + var vis = vm.Gfx.SnapshotVisibleObjects(); + Assert.Single(vis); + Assert.Equal(0xA, vis[0].Handle); + Assert.Equal(0x25, vis[0].SurfaceResId); // resolved from the object's live source slot + Assert.Equal((800, 600, 0, 0), (vis[0].W, vis[0].H, vis[0].DstX, vis[0].DstY)); } } diff --git a/engine/Age.Engine.Tests/GfxStateTests.cs b/engine/Age.Engine.Tests/GfxStateTests.cs index fc7be97..30627c0 100644 --- a/engine/Age.Engine.Tests/GfxStateTests.cs +++ b/engine/Age.Engine.Tests/GfxStateTests.cs @@ -61,28 +61,38 @@ public class GfxStateTests } [Fact] - public void LayersAppendInOrderAndUpdateInPlace() + public void BindDrawMakesAVisibleRenderObjectFromItsSurface() { var g = new GfxState(); - g.AddOrUpdateLayer(new DrawLayer(0xA, 4, 0, 0, 800, 600, 0, 0)); - g.AddOrUpdateLayer(new DrawLayer(0xB, 5, 0, 0, 200, 200, 100, 100)); - g.AddOrUpdateLayer(new DrawLayer(0xA, 4, 0, 0, 800, 600, 0, 50)); // re-draw A -> update in place - var s = g.SnapshotLayers(); - Assert.Equal(2, s.Count); - Assert.Equal(0xA, s[0].Handle); // order preserved (A still first) - Assert.Equal(50, s[0].DstY); // updated - Assert.Equal(0xB, s[1].Handle); + g.SetSurface(4, 0x25, 0); // load resId 0x25 into surface slot 4 + g.BindDraw(0xcb2a, 4, 0, 0, 800, 600, 0, 0); // object 0xcb2a draws surface 4 at (0,0) + var vis = g.SnapshotVisibleObjects(); + Assert.Single(vis); + Assert.Equal(0xcb2a, vis[0].Handle); + Assert.Equal(0x25, vis[0].SurfaceResId); // resolved from the object's live source slot + Assert.Equal((800, 600, 0, 0), (vis[0].W, vis[0].H, vis[0].DstX, vis[0].DstY)); } [Fact] - public void EraseRangeAlsoDropsLayers() + public void VisibleObjectsComeInAscendingHandleOrder() // ascending handle == the engine's z-order { var g = new GfxState(); - g.AddOrUpdateLayer(new DrawLayer(0x10, 4, 0, 0, 10, 10, 0, 0)); - g.AddOrUpdateLayer(new DrawLayer(0x20, 5, 0, 0, 10, 10, 0, 0)); + g.SetSurface(4, 0x1, 0); g.SetSurface(5, 0x2, 0); + g.BindDraw(0xcf08, 5, 0, 0, 10, 10, 0, 0); // higher handle (should be on top / last) + g.BindDraw(0xcb20, 4, 0, 0, 10, 10, 0, 0); // lower handle (behind / first) + var vis = g.SnapshotVisibleObjects(); + Assert.Equal(2, vis.Count); + Assert.Equal(0xcb20, vis[0].Handle); + Assert.Equal(0xcf08, vis[1].Handle); + } + + [Fact] + public void EraseRangeRemovesTheObjectFromCompositing() + { + var g = new GfxState(); + g.SetSurface(4, 0x1, 0); + g.BindDraw(0x10, 4, 0, 0, 10, 10, 0, 0); g.EraseRange(0x10, 1); - var s = g.SnapshotLayers(); - Assert.Single(s); - Assert.Equal(0x20, s[0].Handle); + Assert.Empty(g.SnapshotVisibleObjects()); // erased => gone from the registry => not composited } } diff --git a/engine/Age.Engine/Model/GfxState.cs b/engine/Age.Engine/Model/GfxState.cs index 95bd83e..4346602 100644 --- a/engine/Age.Engine/Model/GfxState.cs +++ b/engine/Age.Engine/Model/GfxState.cs @@ -1,9 +1,13 @@ +using System.Linq; + namespace Age.Engine.Model; -/// A retained composite layer captured when the VM executes draw-texture — the host clears and -/// re-composites the layer set each frame (replacing the immediate-mode permanent canvas). Keyed by the -/// object so a re-draw updates in place and an erase (op 0x1f7) removes it. -public readonly record struct DrawLayer(long Handle, int Slot, int SrcX, int SrcY, int W, int H, int DstX, int DstY); +/// A renderable view of one visible gfx object — the host composites these in ascending-handle order +/// (= the engine's z-order) each frame. Built by ; the surface +/// resId/colorkey are resolved from the object's live source slot at snapshot time (see docs/engine-re.md, +/// "The full gfx render model"). +public readonly record struct RenderObject(long Handle, long SurfaceResId, long ColorKey, + int SrcX, int SrcY, int W, int H, int DstX, int DstY); /// Host-agnostic model of the AGE native gfx command-buffer (reversed in /// docs/engine-re.md, gfx op-contract table). One registry maps an object handle to a GfxObject — the @@ -19,6 +23,10 @@ public sealed class GfxState public (long X, long Y, long Z) V18, V24, V16c; public long Field64, Field68, Field6c; public long Color; + // draw-texture bind (gfx_object_bind_draw): the surface to draw + its source rect + the visible flag. + public int SourceSlot = -1; + public (int X, int Y, int W, int H) SrcRect; + public bool Visible; } private readonly Dictionary _objects = new(); @@ -64,30 +72,51 @@ public sealed class GfxState /// NOT a create — objects are created lazily by the geometry SET ops (gfx_object_get_or_create). public void EraseRange(long handle, long count) { - if (count > 1) for (long i = handle; i < handle + count; i++) { Release(i); RemoveLayers(i); } - else { Release(handle); RemoveLayers(handle); } - } - - // ---- retained composite layers (mutated on the VM thread; snapshotted on the host render thread) ---- - private readonly List _layers = new(); - private readonly object _lock = new(); - - /// Record a draw: update the layer with this handle in place (preserving composite order), else - /// append. Order = execution order = paint order. - public void AddOrUpdateLayer(DrawLayer l) - { + // Registry/slot cleanup (native gfx_registry_erase): removes the object from the registry, so it stops + // compositing next frame. Faithful to the engine (the render loop iterates the registry). lock (_lock) { - for (int i = 0; i < _layers.Count; i++) - if (_layers[i].Handle == l.Handle) { _layers[i] = l; return; } - _layers.Add(l); + if (count > 1) for (long i = handle; i < handle + count; i++) Release(i); + else Release(handle); } } - public void RemoveLayers(long handle) { lock (_lock) { _layers.RemoveAll(l => l.Handle == handle); } } + private readonly object _lock = new(); - /// An immutable snapshot of the current layers, in composite order (for the host render loop). - public IReadOnlyList SnapshotLayers() { lock (_lock) { return _layers.ToArray(); } } + // ---- surfaces (image buffers per slot): ctx+0x52bd4[slot], from create/set-texture ---- + private readonly Dictionary _surfaces = new(); + public void SetSurface(int slot, long resId, long colorKey) { lock (_lock) { _surfaces[slot] = (resId, colorKey); } } + public void ClearSurface(int slot) { lock (_lock) { _surfaces[slot] = (0, 0); } } // create-texture (blank) + + /// draw-texture bind (gfx_object_bind_draw): object draws surface + /// 's rect at (dstX,dstY) and becomes visible. + public void BindDraw(long handle, int slot, int sx, int sy, int w, int h, int dstX, int dstY) + { + lock (_lock) + { + var o = GetOrCreate(handle); + o.SourceSlot = slot; o.SrcRect = (sx, sy, w, h); o.V24 = (dstX, dstY, 0); o.Visible = true; + } + } + + /// Visible objects in ascending-handle order (= the engine's z-order), each with its source + /// surface (resId/colorkey) resolved from its live source slot — for the host per-frame compositor. + public IReadOnlyList SnapshotVisibleObjects() + { + lock (_lock) + { + var list = new List(); + foreach (var kv in _objects.OrderBy(k => k.Key)) + { + var o = kv.Value; + if (!o.Visible) continue; + var (resId, ck) = _surfaces.TryGetValue(o.SourceSlot, out var s) ? s : (0L, 0L); + list.Add(new RenderObject(kv.Key, resId, ck, o.SrcRect.X, o.SrcRect.Y, o.SrcRect.W, o.SrcRect.H, + (int)o.V24.X, (int)o.V24.Y)); + } + return list; + } + } /// Pack (alpha, rgb) → 0xAARRGGBB, matching op 0x202/0x203's handler bit-manipulation for the /// common (non-negative-sentinel) case. The alpha<0 / color<0 native-fetch path is deferred. diff --git a/engine/Age.Engine/Vm/VirtualMachine.cs b/engine/Age.Engine/Vm/VirtualMachine.cs index 488a5d6..0fb0644 100644 --- a/engine/Age.Engine/Vm/VirtualMachine.cs +++ b/engine/Age.Engine/Vm/VirtualMachine.cs @@ -211,15 +211,17 @@ public sealed class VirtualMachine case "end-text-line": case "set-font": case "comment": case "display-furigana": case "dev_ukn": return pc + 1; - case "create-texture": + case "create-texture": // 0x1f8 (slot)(w)(h) — allocate a blank surface at the slot + Gfx.ClearSurface((int)Read(a[0])); _host.CreateTexture((int)Read(a[0]), (int)Read(a[1]), (int)Read(a[2])); return pc + 1; - case "set-texture": - _host.SetTexture(Read(a[0]), (int)Read(a[1])); return pc + 1; - case "draw-texture": // (handle, slot, srcX, srcY, w, h, dstX, dstY) - Gfx.AddOrUpdateLayer(new DrawLayer(Read(a[0]), (int)Read(a[1]), (int)Read(a[2]), (int)Read(a[3]), - (int)Read(a[4]), (int)Read(a[5]), (int)Read(a[6]), (int)Read(a[7]))); + case "set-texture": // 0x1f9 (resId)(slot)(colorkey) — load a file into the slot's surface + Gfx.SetSurface((int)Read(a[1]), Read(a[0]), a.Count > 2 ? Read(a[2]) : 0); + _host.SetTexture(Read(a[0]), (int)Read(a[1])); return pc + 1; // host still tracks dims for get-texture-size + case "draw-texture": // 0x1fb (handle)(slot)(srcX)(srcY)(w)(h)(dstX)(dstY) — bind object -> surface + rect + pos + Gfx.BindDraw(Read(a[0]), (int)Read(a[1]), (int)Read(a[2]), (int)Read(a[3]), + (int)Read(a[4]), (int)Read(a[5]), (int)Read(a[6]), (int)Read(a[7])); _host.DrawTexture((int)Read(a[1]), (int)Read(a[2]), (int)Read(a[3]), (int)Read(a[4]), - (int)Read(a[5]), (int)Read(a[6]), (int)Read(a[7])); return pc + 1; + (int)Read(a[5]), (int)Read(a[6]), (int)Read(a[7])); return pc + 1; // IHost seam (oracle log; Godot no-ops) case "get-texture-size": // 0x208 (slot) (out_w) (out_h) { var (gw, gh) = _host.GetTextureSize((int)Read(a[0]));