Merge pull request #8200 from RoosterDragon/sheet-fixes

Sheet fixes
This commit is contained in:
Pavel Penev
2015-05-22 18:30:13 +03:00
5 changed files with 37 additions and 60 deletions

View File

@@ -18,7 +18,6 @@ namespace OpenRA.Graphics
{ {
public sealed class Sheet : IDisposable public sealed class Sheet : IDisposable
{ {
readonly object textureLock = new object();
bool dirty; bool dirty;
bool releaseBufferOnCommit; bool releaseBufferOnCommit;
ITexture texture; ITexture texture;
@@ -66,33 +65,21 @@ namespace OpenRA.Graphics
public ITexture GetTexture() public ITexture GetTexture()
{ {
// This is only called from the main thread but 'dirty' if (texture == null)
// is set from other threads too via CommitData().
GenerateTexture();
return texture;
}
void GenerateTexture()
{
lock (textureLock)
{ {
if (texture == null) texture = Game.Renderer.Device.CreateTexture();
{ dirty = true;
texture = Game.Renderer.Device.CreateTexture();
dirty = true;
}
if (data != null)
{
if (dirty)
{
texture.SetData(data, Size.Width, Size.Height);
dirty = false;
if (releaseBufferOnCommit)
data = null;
}
}
} }
if (data != null && dirty)
{
texture.SetData(data, Size.Width, Size.Height);
dirty = false;
if (releaseBufferOnCommit)
data = null;
}
return texture;
} }
public Bitmap AsBitmap() public Bitmap AsBitmap()
@@ -141,42 +128,32 @@ namespace OpenRA.Graphics
public void CreateBuffer() public void CreateBuffer()
{ {
lock (textureLock) if (data != null)
{ return;
if (data != null) if (texture == null)
return; data = new byte[4 * Size.Width * Size.Height];
if (texture == null) else
data = new byte[4 * Size.Width * Size.Height]; data = texture.GetData();
else releaseBufferOnCommit = false;
data = texture.GetData();
releaseBufferOnCommit = false;
}
} }
public void CommitData() public void CommitBufferedData()
{ {
CommitData(false); if (!Buffered)
throw new InvalidOperationException(
"This sheet is unbuffered. You cannot call CommitBufferedData 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;
} }
public void ReleaseBuffer() public void ReleaseBuffer()
{ {
CommitData(true); if (!Buffered)
} return;
dirty = true;
void CommitData(bool releaseBuffer) releaseBufferOnCommit = true;
{
lock (textureLock)
{
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;
}
} }
public void Dispose() public void Dispose()

View File

@@ -70,7 +70,7 @@ namespace OpenRA.Graphics
var rect = Allocate(size, spriteOffset); var rect = Allocate(size, spriteOffset);
Util.FastCopyIntoChannel(rect, src); Util.FastCopyIntoChannel(rect, src);
current.CommitData(); current.CommitBufferedData();
return rect; return rect;
} }
@@ -78,7 +78,7 @@ namespace OpenRA.Graphics
{ {
var rect = Allocate(src.Size); var rect = Allocate(src.Size);
Util.FastCopyIntoSprite(rect, src); Util.FastCopyIntoSprite(rect, src);
current.CommitData(); current.CommitBufferedData();
return rect; return rect;
} }

View File

@@ -136,7 +136,7 @@ namespace OpenRA.Graphics
} }
} }
s.Sheet.CommitData(); s.Sheet.CommitBufferedData();
return g; return g;
} }

View File

@@ -86,7 +86,7 @@ namespace OpenRA.Graphics
var s = sheetBuilder.Allocate(new Size(su, sv)); var s = sheetBuilder.Allocate(new Size(su, sv));
Util.FastCopyIntoChannel(s, 0, colors); Util.FastCopyIntoChannel(s, 0, colors);
Util.FastCopyIntoChannel(s, 1, normals); Util.FastCopyIntoChannel(s, 1, normals);
s.Sheet.CommitData(); s.Sheet.CommitBufferedData();
var channelP = ChannelSelect[(int)s.Channel]; var channelP = ChannelSelect[(int)s.Channel];
var channelC = ChannelSelect[(int)s.Channel + 1]; var channelC = ChannelSelect[(int)s.Channel + 1];

View File

@@ -216,7 +216,7 @@ namespace OpenRA.Mods.Common.Widgets
dirtyShroudCells.Clear(); dirtyShroudCells.Clear();
} }
radarSheet.CommitData(); radarSheet.CommitBufferedData();
var o = new float2(mapRect.Location.X, mapRect.Location.Y + world.Map.Bounds.Height * previewScale * (1 - radarMinimapHeight) / 2); var o = new float2(mapRect.Location.X, mapRect.Location.Y + world.Map.Bounds.Height * previewScale * (1 - radarMinimapHeight) / 2);
var s = new float2(mapRect.Size.Width, mapRect.Size.Height * radarMinimapHeight); var s = new float2(mapRect.Size.Width, mapRect.Size.Height * radarMinimapHeight);