From 9f5a90358c3f71553365db14b65e2f285cb057bd Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Sun, 12 Jan 2025 11:49:26 +0000 Subject: [PATCH] Dynamically grow lightmap layers This can be a huge memory saving on large maps. For example TPOAIR M2 goes from ~3gb of RAM to ~500mb --- .../Database/Chunks/WorldRep.cs | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/KeepersCompound.LGS/Database/Chunks/WorldRep.cs b/KeepersCompound.LGS/Database/Chunks/WorldRep.cs index e65bff8..32be9f5 100644 --- a/KeepersCompound.LGS/Database/Chunks/WorldRep.cs +++ b/KeepersCompound.LGS/Database/Chunks/WorldRep.cs @@ -243,7 +243,12 @@ public class WorldRep : IChunk public void AddLight(int layer, int x, int y, float r, float g, float b) { ArgumentOutOfRangeException.ThrowIfLessThan(layer, 0, nameof(layer)); - ArgumentOutOfRangeException.ThrowIfGreaterThan(layer, Layers, nameof(layer)); + ArgumentOutOfRangeException.ThrowIfGreaterThan(layer, 32, nameof(layer)); + + while (layer >= Layers) + { + AddLayer(); + } var idx = (x + y * Width) * Bpp; var pLayer = Pixels[layer]; @@ -293,16 +298,12 @@ public class WorldRep : IChunk public void Reset(Vector3 ambientLight, bool hdr) { - Layers = 33; + Layers = 0; Pixels.Clear(); - var bytesPerLayer = Width * Height * Bpp; - for (var i = 0; i < Layers; i++) + AddLayer(); + + for (var i = 0; i < _litLayers.Length; i++) { - Pixels.Add(new byte[bytesPerLayer]); - for (var j = 0; j < bytesPerLayer; j++) - { - Pixels[i][j] = 0; - } _litLayers[i] = false; } @@ -325,6 +326,18 @@ public class WorldRep : IChunk } } } + + private void AddLayer() + { + var bytesPerLayer = Width * Height * Bpp; + var bytes = new byte[bytesPerLayer]; + for (var i = 0; i < bytesPerLayer; i++) + { + bytes[i] = 0; + } + Pixels.Add(bytes); + Layers++; + } } public byte VertexCount { get; set; }