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
This commit is contained in:
Jarrod Doyle 2025-01-12 11:49:26 +00:00
parent 224d79e1df
commit 9f5a90358c
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
1 changed files with 22 additions and 9 deletions

View File

@ -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; }