Implement dynamic hardware palette sizing.

The HardwarePalette will now grow its palette buffer and texture in power-of-2 increments. This avoids it having to allocate memory for a full 256x256 texture up front. In practice the default mods use 22 or 23 palettes so a 32x256 texture is used. This means both the buffer and texture save neatly on memory. Additionally, HardwarePalette.ApplyModifiers sees a nice speedup as it has to transfer a much smaller amount of memory from the buffer to the texture.

To facilitate this change, the MaxPalettes constant is no more. Instead the PaletteReference deals with the calculation of the index and this is passed into the appropriate methods.
This commit is contained in:
RoosterDragon
2015-01-07 21:08:48 +00:00
parent 40c9d0a47d
commit 53f06ba093
7 changed files with 42 additions and 32 deletions

View File

@@ -16,14 +16,13 @@ namespace OpenRA.Graphics
{
public sealed class HardwarePalette : IDisposable
{
public const int MaxPalettes = 256;
public ITexture Texture { get; private set; }
public int Height { get; private set; }
readonly Dictionary<string, ImmutablePalette> palettes = new Dictionary<string, ImmutablePalette>();
readonly Dictionary<string, MutablePalette> modifiablePalettes = new Dictionary<string, MutablePalette>();
readonly IReadOnlyDictionary<string, MutablePalette> readOnlyModifiablePalettes;
readonly Dictionary<string, int> indices = new Dictionary<string, int>();
readonly uint[,] buffer = new uint[MaxPalettes, Palette.Size];
byte[] buffer = new byte[0];
public HardwarePalette()
{
@@ -52,14 +51,19 @@ namespace OpenRA.Graphics
public void AddPalette(string name, ImmutablePalette p, bool allowModifiers)
{
if (palettes.Count >= MaxPalettes)
throw new InvalidOperationException("Limit of {0} palettes reached. Cannot add {1}.".F(MaxPalettes, name));
if (palettes.ContainsKey(name))
throw new InvalidOperationException("Palette {0} has already been defined".F(name));
int index = palettes.Count;
indices.Add(name, index);
palettes.Add(name, p);
if (palettes.Count > Height)
{
Height = Exts.NextPowerOf2(palettes.Count);
Array.Resize(ref buffer, Height * Palette.Size * 4);
}
if (allowModifiers)
modifiablePalettes.Add(name, new MutablePalette(p));
else
@@ -74,13 +78,13 @@ namespace OpenRA.Graphics
CopyPaletteToBuffer(indices[name], palettes[name] = new ImmutablePalette(p));
else
throw new InvalidOperationException("Palette `{0}` does not exist".F(name));
Texture.SetData(buffer);
CopyBufferToTexture();
}
public void Initialize()
{
CopyModifiablePalettesToBuffer();
Texture.SetData(buffer);
CopyBufferToTexture();
}
void CopyPaletteToBuffer(int index, IPalette p)
@@ -94,6 +98,11 @@ namespace OpenRA.Graphics
CopyPaletteToBuffer(indices[kvp.Key], kvp.Value);
}
void CopyBufferToTexture()
{
Texture.SetData(buffer, Palette.Size, Height);
}
public void ApplyModifiers(IEnumerable<IPaletteModifier> paletteMods)
{
foreach (var mod in paletteMods)
@@ -101,7 +110,7 @@ namespace OpenRA.Graphics
// Update our texture with the changes.
CopyModifiablePalettesToBuffer();
Texture.SetData(buffer);
CopyBufferToTexture();
// Reset modified palettes back to their original colors, ready for next time.
foreach (var kvp in modifiablePalettes)