MOAR PERF

git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1956 993157c7-ee19-0410-b2c4-bb4e9862e678
This commit is contained in:
chrisf
2008-03-25 02:07:15 +00:00
parent f218d212ff
commit b77a116f60
7 changed files with 98 additions and 123 deletions

View File

@@ -7,35 +7,22 @@ namespace OpenRa.FileFormats
{ {
public static class Format40 public static class Format40
{ {
static byte ReadByte( MemoryStream input ) public static int DecodeInto( byte[] src, byte[] dest )
{
int inp = input.ReadByte();
if( inp == -1 )
throw new InvalidDataException();
return (byte)inp;
}
static int ReadWord( MemoryStream input )
{
int inp = ReadByte( input );
return inp + ( ReadByte( input ) << 8 );
}
public static int DecodeInto( MemoryStream input, byte[] dest )
{ {
var ctx = new FastByteReader(src);
int destIndex = 0; int destIndex = 0;
while( true ) while( true )
{ {
byte i = ReadByte( input ); byte i = ctx.ReadByte();
if( ( i & 0x80 ) == 0 ) if( ( i & 0x80 ) == 0 )
{ {
int count = i & 0x7F; int count = i & 0x7F;
if( count == 0 ) if( count == 0 )
{ {
// case 6 // case 6
count = ReadByte( input ); count = ctx.ReadByte();
byte value = ReadByte( input ); byte value = ctx.ReadByte();
for( int end = destIndex + count ; destIndex < end ; destIndex++ ) for( int end = destIndex + count ; destIndex < end ; destIndex++ )
dest[ destIndex ] ^= value; dest[ destIndex ] ^= value;
} }
@@ -43,7 +30,7 @@ namespace OpenRa.FileFormats
{ {
// case 5 // case 5
for( int end = destIndex + count ; destIndex < end ; destIndex++ ) for( int end = destIndex + count ; destIndex < end ; destIndex++ )
dest[ destIndex ] ^= ReadByte( input ); dest[destIndex] ^= ctx.ReadByte();
} }
} }
else else
@@ -51,7 +38,7 @@ namespace OpenRa.FileFormats
int count = i & 0x7F; int count = i & 0x7F;
if( count == 0 ) if( count == 0 )
{ {
count = ReadWord( input ); count = ctx.ReadWord();
if( count == 0 ) if( count == 0 )
return destIndex; return destIndex;
@@ -64,12 +51,12 @@ namespace OpenRa.FileFormats
{ {
// case 3 // case 3
for( int end = destIndex + ( count & 0x3FFF ) ; destIndex < end ; destIndex++ ) for( int end = destIndex + ( count & 0x3FFF ) ; destIndex < end ; destIndex++ )
dest[ destIndex ] ^= ReadByte( input ); dest[destIndex] ^= ctx.ReadByte();
} }
else else
{ {
// case 4 // case 4
byte value = ReadByte( input ); byte value = ctx.ReadByte();
for( int end = destIndex + ( count & 0x3FFF ) ; destIndex < end ; destIndex++ ) for( int end = destIndex + ( count & 0x3FFF ) ; destIndex < end ; destIndex++ )
dest[ destIndex ] ^= value; dest[ destIndex ] ^= value;
} }

View File

@@ -5,23 +5,33 @@ using System.IO;
namespace OpenRa.FileFormats namespace OpenRa.FileFormats
{ {
class FastByteReader
{
readonly byte[] src;
int offset = 0;
public FastByteReader(byte[] src)
{
this.src = src;
}
public bool Done() { return offset >= src.Length; }
public byte ReadByte() { return src[offset++]; }
public int ReadWord()
{
int x = ReadByte();
return x | (ReadByte() << 8);
}
public void CopyTo(byte[] dest, int offset, int count)
{
Array.Copy(src, this.offset, dest, offset, count);
this.offset += count;
}
}
public static class Format80 public static class Format80
{ {
static byte ReadByte( MemoryStream input )
{
int inp = input.ReadByte();
if( inp == -1 )
throw new InvalidDataException();
return (byte)inp;
}
static int ReadWord( MemoryStream input )
{
int inp = ReadByte( input );
return inp + ( ReadByte( input ) << 8 );
}
static void ReplicatePrevious( byte[] dest, int destIndex, int srcIndex, int count ) static void ReplicatePrevious( byte[] dest, int destIndex, int srcIndex, int count )
{ {
if( srcIndex >= destIndex ) if( srcIndex >= destIndex )
@@ -39,16 +49,18 @@ namespace OpenRa.FileFormats
} }
} }
public static int DecodeInto( MemoryStream input, byte[] dest ) public static int DecodeInto( byte[] src, byte[] dest )
{ {
var ctx = new FastByteReader(src);
int destIndex = 0; int destIndex = 0;
while( true ) while( true )
{ {
byte i = ReadByte( input ); byte i = ctx.ReadByte();
if( ( i & 0x80 ) == 0 ) if( ( i & 0x80 ) == 0 )
{ {
// case 2 // case 2
byte secondByte = ReadByte( input ); byte secondByte = ctx.ReadByte();
int count = ( ( i & 0x70 ) >> 4 ) + 3; int count = ( ( i & 0x70 ) >> 4 ) + 3;
int rpos = ( ( i & 0xf ) << 8 ) + secondByte; int rpos = ( ( i & 0xf ) << 8 ) + secondByte;
@@ -62,7 +74,7 @@ namespace OpenRa.FileFormats
if( count == 0 ) if( count == 0 )
return destIndex; return destIndex;
input.Read( dest, destIndex, count ); ctx.CopyTo( dest, destIndex, count );
destIndex += count; destIndex += count;
} }
else else
@@ -71,8 +83,8 @@ namespace OpenRa.FileFormats
if( count3 == 0x3E ) if( count3 == 0x3E )
{ {
// case 4 // case 4
int count = ReadWord( input ); int count = ctx.ReadWord();
byte color = ReadByte( input ); byte color = ctx.ReadByte();
for( int end = destIndex + count ; destIndex < end ; destIndex++ ) for( int end = destIndex + count ; destIndex < end ; destIndex++ )
dest[ destIndex ] = color; dest[ destIndex ] = color;
@@ -80,8 +92,8 @@ namespace OpenRa.FileFormats
else if( count3 == 0x3F ) else if( count3 == 0x3F )
{ {
// case 5 // case 5
int count = ReadWord( input ); int count = ctx.ReadWord();
int srcIndex = ReadWord( input ); int srcIndex = ctx.ReadWord();
if( srcIndex >= destIndex ) if( srcIndex >= destIndex )
throw new NotImplementedException( string.Format( "srcIndex >= destIndex {0} {1}", srcIndex, destIndex ) ); throw new NotImplementedException( string.Format( "srcIndex >= destIndex {0} {1}", srcIndex, destIndex ) );
@@ -92,7 +104,7 @@ namespace OpenRa.FileFormats
{ {
// case 3 // case 3
int count = count3 + 3; int count = count3 + 3;
int srcIndex = ReadWord( input ); int srcIndex = ctx.ReadWord();
if( srcIndex >= destIndex ) if( srcIndex >= destIndex )
throw new NotImplementedException( string.Format( "srcIndex >= destIndex {0} {1}", srcIndex, destIndex ) ); throw new NotImplementedException( string.Format( "srcIndex >= destIndex {0} {1}", srcIndex, destIndex ) );

View File

@@ -72,7 +72,7 @@ namespace OpenRa.FileFormats
byte[] dest = new byte[8192]; byte[] dest = new byte[8192];
byte[] src = reader.ReadBytes((int)length); byte[] src = reader.ReadBytes((int)length);
int actualLength = Format80.DecodeInto(new MemoryStream(src), dest); int actualLength = Format80.DecodeInto(src, dest);
chunks.Add(dest); chunks.Add(dest);
} }
@@ -113,7 +113,7 @@ namespace OpenRa.FileFormats
for( int i = 0 ; i < 128 ; i++ ) for( int i = 0 ; i < 128 ; i++ )
for( int j = 0 ; j < 128 ; j++ ) for( int j = 0 ; j < 128 ; j++ )
{ {
MapTiles[ j, i ].image = ReadByte( ms ); MapTiles[j, i].image = (byte)ms.ReadByte();// ReadByte(ms);
if( MapTiles[ j, i ].tile == 0xff || MapTiles[ j, i ].tile == 0xffff ) if( MapTiles[ j, i ].tile == 0xff || MapTiles[ j, i ].tile == 0xffff )
MapTiles[ j, i ].image = (byte)( i % 4 + ( j % 4 ) * 4 ); MapTiles[ j, i ].image = (byte)( i % 4 + ( j % 4 ) * 4 );
} }

View File

@@ -109,16 +109,13 @@ namespace OpenRa.FileFormats
} }
h.Image = CopyImageData( h.RefImage.Image ); h.Image = CopyImageData( h.RefImage.Image );
Format40.DecodeInto(ReadCompressedData(stream, h), h.Image);
MemoryStream ms = ReadCompressedData( stream, h );
Format40.DecodeInto( ms, h.Image );
break; break;
} }
case Format.Format80: case Format.Format80:
{ {
MemoryStream ms = ReadCompressedData( stream, h );
byte[] imageBytes = new byte[ Width * Height ]; byte[] imageBytes = new byte[ Width * Height ];
Format80.DecodeInto( ms, imageBytes ); Format80.DecodeInto( ReadCompressedData( stream, h ), imageBytes );
h.Image = imageBytes; h.Image = imageBytes;
break; break;
} }
@@ -127,7 +124,7 @@ namespace OpenRa.FileFormats
} }
} }
private static MemoryStream ReadCompressedData( Stream stream, ImageHeader h ) static byte[] ReadCompressedData( Stream stream, ImageHeader h )
{ {
stream.Position = h.Offset; stream.Position = h.Offset;
// Actually, far too big. There's no length field with the correct length though :( // Actually, far too big. There's no length field with the correct length though :(
@@ -136,8 +133,8 @@ namespace OpenRa.FileFormats
byte[] compressedBytes = new byte[ compressedLength ]; byte[] compressedBytes = new byte[ compressedLength ];
stream.Read( compressedBytes, 0, compressedLength ); stream.Read( compressedBytes, 0, compressedLength );
MemoryStream ms = new MemoryStream( compressedBytes ); //MemoryStream ms = new MemoryStream( compressedBytes );
return ms; return compressedBytes;
} }
private byte[] CopyImageData( byte[] baseImage ) private byte[] CopyImageData( byte[] baseImage )

View File

@@ -15,18 +15,19 @@ namespace OpenRa.Game
const int spritesPerBatch = 1024; const int spritesPerBatch = 1024;
List<Vertex> vertices = new List<Vertex>(); Vertex[] vertices = new Vertex[4 * spritesPerBatch];
List<ushort> indices = new List<ushort>(); ushort[] indices = new ushort[6 * spritesPerBatch];
Sheet currentSheet = null; Sheet currentSheet = null;
int sprites = 0; int sprites = 0;
ShaderQuality quality; ShaderQuality quality;
int nv = 0, ni = 0;
public SpriteRenderer(Renderer renderer, bool allowAlpha) public SpriteRenderer(Renderer renderer, bool allowAlpha)
{ {
this.renderer = renderer; this.renderer = renderer;
vertexBuffer = new FvfVertexBuffer<Vertex>(renderer.Device, 4 * spritesPerBatch, Vertex.Format); vertexBuffer = new FvfVertexBuffer<Vertex>(renderer.Device, vertices.Length, Vertex.Format);
indexBuffer = new IndexBuffer(renderer.Device, 6 * spritesPerBatch); indexBuffer = new IndexBuffer(renderer.Device, indices.Length);
quality = allowAlpha ? ShaderQuality.High : ShaderQuality.Low; quality = allowAlpha ? ShaderQuality.High : ShaderQuality.Low;
} }
@@ -37,16 +38,15 @@ namespace OpenRa.Game
{ {
renderer.DrawWithShader(quality, delegate renderer.DrawWithShader(quality, delegate
{ {
vertexBuffer.SetData(vertices.ToArray()); vertexBuffer.SetData(vertices);
indexBuffer.SetData(indices.ToArray()); indexBuffer.SetData(indices);
renderer.DrawBatch(vertexBuffer, indexBuffer, renderer.DrawBatch(vertexBuffer, indexBuffer,
new Range<int>(0, vertices.Count), new Range<int>(0, nv),
new Range<int>(0, indices.Count), new Range<int>(0, ni),
currentSheet.Texture); currentSheet.Texture);
}); });
vertices = new List<Vertex>(); nv = 0; ni = 0;
indices = new List<ushort>();
currentSheet = null; currentSheet = null;
sprites = 0; sprites = 0;
} }
@@ -58,8 +58,8 @@ namespace OpenRa.Game
Flush(); Flush();
currentSheet = s.sheet; currentSheet = s.sheet;
Util.CreateQuad(vertices, indices, location, s, palette); Util.FastCreateQuad(vertices, indices, location, s, palette, nv, ni);
nv += 4; ni += 6;
if (++sprites >= spritesPerBatch) if (++sprites >= spritesPerBatch)
Flush(); Flush();
} }

View File

@@ -7,6 +7,7 @@ using System.Drawing;
using System.IO; using System.IO;
using System.Windows.Forms; using System.Windows.Forms;
using Ijw.DirectX; using Ijw.DirectX;
using IjwFramework.Collections;
namespace OpenRa.Game namespace OpenRa.Game
{ {
@@ -30,33 +31,32 @@ namespace OpenRa.Game
tileSet = new TileSet( map.TileSuffix ); tileSet = new TileSet( map.TileSuffix );
Dictionary<TileReference, Sprite> tileMapping = Size tileSize = new Size( 24, 24 );
new Dictionary<TileReference, Sprite>();
Size tileSize = new Size( 24, 24 ); var tileMapping = new Cache<TileReference, Sprite>(
x => SheetBuilder.Add(tileSet.GetBytes(x), tileSize));
List<Vertex> vertices = new List<Vertex>(); Vertex[] vertices = new Vertex[4 * map.Height * map.Width];
List<ushort> indices = new List<ushort>(); ushort[] indices = new ushort[6 * map.Height * map.Width];
int nv = 0;
int ni = 0;
for( int j = 0 ; j < map.Height ; j++ ) for( int j = 0 ; j < map.Height ; j++ )
for( int i = 0 ; i < map.Width ; i++ ) for (int i = 0; i < map.Width; i++)
{ {
TileReference tileRef = map.MapTiles[ i + map.XOffset, j + map.YOffset ]; Sprite tile = tileMapping[map.MapTiles[i + map.XOffset, j + map.YOffset]];
Sprite tile; Util.FastCreateQuad(vertices, indices, 24 * new float2(i, j), tile, 0, nv, ni);
nv += 4;
ni += 6;
}
if( !tileMapping.TryGetValue( tileRef, out tile ) ) terrainSheet = tileMapping[map.MapTiles[map.XOffset, map.YOffset]].sheet;
tileMapping.Add( tileRef, tile = SheetBuilder.Add( tileSet.GetBytes( tileRef ), tileSize ) );
terrainSheet = tile.sheet; vertexBuffer = new FvfVertexBuffer<Vertex>( renderer.Device, vertices.Length, Vertex.Format );
vertexBuffer.SetData( vertices );
Util.CreateQuad( vertices, indices, 24 * new float2( i, j ), tile, 0 ); indexBuffer = new IndexBuffer( renderer.Device, indices.Length );
} indexBuffer.SetData( indices );
vertexBuffer = new FvfVertexBuffer<Vertex>( renderer.Device, vertices.Count, Vertex.Format );
vertexBuffer.SetData( vertices.ToArray() );
indexBuffer = new IndexBuffer( renderer.Device, indices.Count );
indexBuffer.SetData( indices.ToArray() );
} }
void Draw() void Draw()

View File

@@ -10,13 +10,6 @@ namespace OpenRa.Game
{ {
static class Util static class Util
{ {
static float[] channelSelect = { 0.75f, 0.25f, -0.25f, -0.75f };
static float2 EncodeVertexAttributes(TextureChannel channel, int paletteLine)
{
return new float2(paletteLine / 16.0f, channelSelect[(int)channel]);
}
static float2 KLerp(float2 o, float2 d, int k) static float2 KLerp(float2 o, float2 d, int k)
{ {
switch (k) switch (k)
@@ -29,14 +22,6 @@ namespace OpenRa.Game
} }
} }
static Vertex MakeVertex(float2 o, int k, Sprite r, float2 attrib)
{
return new Vertex(
KLerp( o, r.size, k ),
r.FastMapTextureCoords(k),
attrib);
}
public static string[] ReadAllLines(Stream s) public static string[] ReadAllLines(Stream s)
{ {
List<string> result = new List<string>(); List<string> result = new List<string>();
@@ -56,28 +41,22 @@ namespace OpenRa.Game
return result; return result;
} }
public static void CreateQuad(List<Vertex> vertices, List<ushort> indices, float2 o, Sprite r, int palette) static float[] channelSelect = { 0.75f, 0.25f, -0.25f, -0.75f };
{
ushort offset = (ushort)vertices.Count;
float2 attrib = EncodeVertexAttributes(r.channel, palette);
Vertex[] v = new Vertex[] public static void FastCreateQuad(Vertex[] vertices, ushort[] indices, float2 o, Sprite r, int palette, int nv, int ni)
{ {
Util.MakeVertex(o, 0, r, attrib), float2 attrib = new float2(palette / 16.0f, channelSelect[(int)r.channel]);
Util.MakeVertex(o, 1, r, attrib),
Util.MakeVertex(o, 2, r, attrib),
Util.MakeVertex(o, 3, r, attrib),
};
vertices.AddRange(v); vertices[nv] = new Vertex(KLerp(o, r.size, 0), r.FastMapTextureCoords(0), attrib);
vertices[nv + 1] = new Vertex(KLerp(o, r.size, 1), r.FastMapTextureCoords(1), attrib);
vertices[nv + 2] = new Vertex(KLerp(o, r.size, 2), r.FastMapTextureCoords(2), attrib);
vertices[nv + 3] = new Vertex(KLerp(o, r.size, 3), r.FastMapTextureCoords(3), attrib);
ushort[] i = new ushort[] indices[ni] = (ushort)(nv);
{ indices[ni + 1] = indices[ni + 3] = (ushort)(nv + 1);
offset, (ushort)(offset + 1), (ushort)(offset + 2), (ushort)(offset + 1), (ushort)(offset + 3), (ushort)(offset + 2) indices[ni + 2] = indices[ni + 5] = (ushort)(nv + 2);
}; indices[ni + 4] = (ushort)(nv + 3);
}
indices.AddRange(i);
}
public static void FastCopyIntoChannel(Sprite dest, byte[] src) public static void FastCopyIntoChannel(Sprite dest, byte[] src)
{ {