Merge pull request #6756 from RoosterDragon/release-more-buffers

Release more sheet buffers
This commit is contained in:
obrakmann
2014-11-29 17:47:06 +01:00
13 changed files with 115 additions and 65 deletions

View File

@@ -62,8 +62,10 @@ namespace OpenRA.Graphics
public void Preload()
{
SpriteCache.SheetBuilder.Current.CreateBuffer();
foreach (var unitSeq in sequences.Value.Values)
foreach (var seq in unitSeq.Value.Values) { }
SpriteCache.SheetBuilder.Current.ReleaseBuffer();
}
}

View File

@@ -25,14 +25,16 @@ namespace OpenRA.Graphics
byte[] data;
public readonly Size Size;
public byte[] Data { get { return data ?? texture.GetData(); } }
public bool Buffered { get { return data != null; } }
public byte[] GetData()
{
CreateBuffer();
return data;
}
public bool Buffered { get { return data != null || texture == null; } }
public Sheet(Size size, bool buffered)
public Sheet(Size size)
{
Size = size;
if (buffered)
data = new byte[4 * Size.Width * Size.Height];
}
public Sheet(ITexture texture)
@@ -61,29 +63,27 @@ namespace OpenRA.Graphics
ReleaseBuffer();
}
public ITexture Texture
public ITexture GetTexture()
{
// This is only called from the main thread but 'dirty'
// is set from other threads too via CommitData().
get
{
GenerateTexture();
return texture;
}
GenerateTexture();
return texture;
}
void GenerateTexture()
{
if (texture == null)
lock (textureLock)
{
texture = Game.Renderer.Device.CreateTexture();
dirty = true;
}
if (Buffered)
{
lock (textureLock)
if (texture == null)
{
texture = Game.Renderer.Device.CreateTexture();
dirty = true;
}
if (data != null)
{
if (dirty)
{
texture.SetData(data, Size.Width, Size.Height);
@@ -97,7 +97,7 @@ namespace OpenRA.Graphics
public Bitmap AsBitmap()
{
var d = Data;
var d = GetData();
var dataStride = 4 * Size.Width;
var bitmap = new Bitmap(Size.Width, Size.Height);
@@ -112,7 +112,7 @@ namespace OpenRA.Graphics
public Bitmap AsBitmap(TextureChannel channel, IPalette pal)
{
var d = Data;
var d = GetData();
var dataStride = 4 * Size.Width;
var bitmap = new Bitmap(Size.Width, Size.Height);
var channelOffset = (int)channel;
@@ -139,22 +139,44 @@ namespace OpenRA.Graphics
return bitmap;
}
public void CreateBuffer()
{
lock (textureLock)
{
if (data != null)
return;
if (texture == null)
data = new byte[4 * Size.Width * Size.Height];
else
data = texture.GetData();
releaseBufferOnCommit = false;
}
}
public void CommitData()
{
if (!Buffered)
throw new InvalidOperationException(
"This sheet is unbuffered. You cannot call CommitData on an unbuffered sheet. " +
"If you need to completely replace the texture data you should set data into the texture directly. " +
"If you need to make only small changes to the texture data consider creating a buffered sheet instead.");
lock (textureLock)
dirty = true;
CommitData(false);
}
public void ReleaseBuffer()
{
CommitData(true);
}
void CommitData(bool releaseBuffer)
{
lock (textureLock)
releaseBufferOnCommit = true;
{
if (!Buffered)
throw new InvalidOperationException(
"This sheet is unbuffered. You cannot call CommitData on an unbuffered sheet. " +
"If you need to completely replace the texture data you should set data into the texture directly. " +
"If you need to make only small changes to the texture data consider creating a buffered sheet instead.");
dirty = true;
if (releaseBuffer)
releaseBufferOnCommit = true;
}
}
}
}

View File

@@ -38,7 +38,7 @@ namespace OpenRA.Graphics
public static Sheet AllocateSheet()
{
return new Sheet(new Size(Renderer.SheetSize, Renderer.SheetSize), true);
return new Sheet(new Size(Renderer.SheetSize, Renderer.SheetSize));
}
public SheetBuilder(SheetType t)

View File

@@ -118,7 +118,7 @@ namespace OpenRA.Graphics
unsafe
{
var p = (byte*)bitmap.Buffer;
var dest = s.sheet.Data;
var dest = s.sheet.GetData();
var destStride = s.sheet.Size.Width * 4;
for (var j = 0; j < s.size.Y; j++)

View File

@@ -32,7 +32,7 @@ namespace OpenRA.Graphics
{
if (nv > 0)
{
shader.SetTexture("DiffuseTexture", currentSheet.Texture);
shader.SetTexture("DiffuseTexture", currentSheet.GetTexture());
renderer.Device.SetBlendMode(currentBlend);
shader.Render(() =>
@@ -109,7 +109,7 @@ namespace OpenRA.Graphics
public void DrawVertexBuffer(IVertexBuffer<Vertex> buffer, int start, int length, PrimitiveType type, Sheet sheet)
{
shader.SetTexture("DiffuseTexture", sheet.Texture);
shader.SetTexture("DiffuseTexture", sheet.GetTexture());
renderer.Device.SetBlendMode(BlendMode.Alpha);
shader.Render(() => renderer.DrawBatch(buffer, start, length, type));
renderer.Device.SetBlendMode(BlendMode.None);

View File

@@ -33,7 +33,7 @@ namespace OpenRA.Graphics
throw new SheetOverflowException("Terrain sheet overflow. Try increasing the tileset SheetSize parameter.");
allocated = true;
return new Sheet(new Size(tileset.SheetSize, tileset.SheetSize), true);
return new Sheet(new Size(tileset.SheetSize, tileset.SheetSize));
};
sheetBuilder = new SheetBuilder(SheetType.Indexed, allocate);

View File

@@ -43,7 +43,7 @@ namespace OpenRA.Graphics
public static void FastCopyIntoChannel(Sprite dest, byte[] src) { FastCopyIntoChannel(dest, 0, src); }
public static void FastCopyIntoChannel(Sprite dest, int channelOffset, byte[] src)
{
var data = dest.sheet.Data;
var data = dest.sheet.GetData();
var srcStride = dest.bounds.Width;
var destStride = dest.sheet.Size.Width * 4;
var destOffset = destStride * dest.bounds.Top + dest.bounds.Left * 4 + channelMasks[(int)dest.channel + channelOffset];
@@ -64,7 +64,7 @@ namespace OpenRA.Graphics
public static void FastCopyIntoSprite(Sprite dest, Bitmap src)
{
var data = dest.sheet.Data;
var data = dest.sheet.GetData();
var dataStride = dest.sheet.Size.Width * 4;
var x = dest.bounds.Left * 4;
var width = dest.bounds.Width * 4;

View File

@@ -257,7 +257,7 @@ namespace OpenRA.Graphics
float[] ambientLight, float[] diffuseLight,
int colorPalette, int normalsPalette)
{
shader.SetTexture("DiffuseTexture", renderData.Sheet.Texture);
shader.SetTexture("DiffuseTexture", renderData.Sheet.GetTexture());
shader.SetVec("PaletteRows", (colorPalette + 0.5f) / HardwarePalette.MaxPalettes,
(normalsPalette + 0.5f) / HardwarePalette.MaxPalettes);
shader.SetMatrix("TransformMatrix", t);