Skip to content

Commit

Permalink
Improve performance of R8Loader.
Browse files Browse the repository at this point in the history
The repeated small stream reads of ReadUInt16 generate a lot of overhead. Instead, consume the data in a single ReadBytes call and then unpack within the same buffer.
  • Loading branch information
RoosterDragon authored and PunkPun committed Mar 9, 2024
1 parent 00a23e6 commit 519db10
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions OpenRA.Mods.D2k/SpriteLoaders/R8Loader.cs
Expand Up @@ -13,6 +13,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using OpenRA.Graphics;
using OpenRA.Primitives;

Expand Down Expand Up @@ -139,17 +140,12 @@ public Frame(Stream s, uint[] lastPalette)
Data = new byte[width * height * 4];
Type = SpriteFrameType.Bgra32;

unsafe
var data = MemoryMarshal.Cast<byte, uint>(Data);
s.ReadBytes(Data.AsSpan()[..(Data.Length / 2)]);
for (var i = width * height - 1; i >= 0; i--)
{
fixed (byte* bd = &Data[0])
{
var data = (uint*)bd;
for (var i = 0; i < width * height; i++)
{
var packed = s.ReadUInt16();
data[i] = (uint)((0xFF << 24) | ((packed & 0x7C00) << 9) | ((packed & 0x3E0) << 6) | ((packed & 0x1f) << 3));
}
}
var packed = Data[i * 2 + 1] << 8 | Data[i * 2];
data[i] = (uint)((0xFF << 24) | ((packed & 0x7C00) << 9) | ((packed & 0x3E0) << 6) | ((packed & 0x1f) << 3));
}
}
else
Expand All @@ -166,9 +162,11 @@ public Frame(Stream s, uint[] lastPalette)
s.ReadUInt32();

Palette = new uint[256];
for (var i = 0; i < 256; i++)
var palette = MemoryMarshal.Cast<uint, byte>(Palette);
s.ReadBytes(palette[..(palette.Length / 2)]);
for (var i = 255; i >= 0; i--)
{
var packed = s.ReadUInt16();
var packed = palette[i * 2 + 1] << 8 | palette[i * 2];
Palette[i] = (uint)((0xFF << 24) | ((packed & 0x7C00) << 9) | ((packed & 0x3E0) << 6) | ((packed & 0x1f) << 3));
}

Expand Down

0 comments on commit 519db10

Please sign in to comment.