Use single index buffer for all chunks. Very minor increase in performance, but does slightly reduce memory usage.

This commit is contained in:
UnknownShadow200 2015-08-03 19:17:53 +10:00
parent 618ded14e0
commit dd2eb1105a
6 changed files with 39 additions and 36 deletions

View File

@ -128,7 +128,9 @@ namespace ClassicalSharp.GraphicsAPI {
public abstract void BeginIndexedVbBatch(); public abstract void BeginIndexedVbBatch();
public abstract void BindIndexedVb( int vb, int ib ); public abstract void BindVb( int vb );
public abstract void BindIb( int ib );
public abstract void DrawIndexedVb( DrawMode mode, int indicesCount, int startVertex, int startIndex ); public abstract void DrawIndexedVb( DrawMode mode, int indicesCount, int startVertex, int startIndex );
@ -140,10 +142,7 @@ namespace ClassicalSharp.GraphicsAPI {
public abstract void LoadMatrix( ref Matrix4 matrix ); public abstract void LoadMatrix( ref Matrix4 matrix );
public virtual void LoadIdentityMatrix() { public abstract void LoadIdentityMatrix();
Matrix4 identity = Matrix4.Identity;
LoadMatrix( ref identity );
}
public abstract void MultiplyMatrix( ref Matrix4 matrix ); public abstract void MultiplyMatrix( ref Matrix4 matrix );
@ -154,8 +153,7 @@ namespace ClassicalSharp.GraphicsAPI {
public abstract void TakeScreenshot( string output, Size size ); public abstract void TakeScreenshot( string output, Size size );
public virtual void PrintApiSpecificInfo() { public abstract void PrintApiSpecificInfo();
}
public abstract void BeginFrame( Game game ); public abstract void BeginFrame( Game game );

View File

@ -296,8 +296,11 @@ namespace ClassicalSharp.GraphicsAPI {
GL.DrawArrays( modeMappings[(int)mode], startVertex, verticesCount ); GL.DrawArrays( modeMappings[(int)mode], startVertex, verticesCount );
} }
public override void BindIndexedVb( int vb, int ib ) { public override void BindVb( int vb ) {
GL.BindBufferARB( BufferTarget.ArrayBuffer, vb ); GL.BindBufferARB( BufferTarget.ArrayBuffer, vb );
}
public override void BindIb( int ib ) {
GL.BindBufferARB( BufferTarget.ElementArrayBuffer, ib ); GL.BindBufferARB( BufferTarget.ElementArrayBuffer, ib );
} }

View File

@ -334,8 +334,7 @@ namespace ClassicalSharp {
public struct ChunkPartInfo { public struct ChunkPartInfo {
public int VbId, IbId; public int VbId, IndicesCount;
public int IndicesCount;
public int leftIndex, rightIndex, frontIndex, public int leftIndex, rightIndex, frontIndex,
backIndex, bottomIndex, topIndex; backIndex, bottomIndex, topIndex;
public ushort leftCount, rightCount, frontCount, public ushort leftCount, rightCount, frontCount,

View File

@ -34,7 +34,6 @@ namespace ClassicalSharp {
class DrawInfo { class DrawInfo {
public VertexPos3fTex2fCol4b[] vertices; public VertexPos3fTex2fCol4b[] vertices;
public ushort[] indices;
public int vCount, iCount; public int vCount, iCount;
public DrawInfoFaceData vIndex; public DrawInfoFaceData vIndex;
public DrawInfoFaceData Count; public DrawInfoFaceData Count;
@ -45,8 +44,6 @@ namespace ClassicalSharp {
if( vertices == null || vCount > vertices.Length ) { if( vertices == null || vCount > vertices.Length ) {
vertices = new VertexPos3fTex2fCol4b[vCount]; vertices = new VertexPos3fTex2fCol4b[vCount];
indices = new ushort[iCount];
MakeIndices();
} }
vIndex.left = spriteCount / 6 * 4; vIndex.left = spriteCount / 6 * 4;
vIndex.right = vIndex.left + Count.left / 6 * 4; vIndex.right = vIndex.left + Count.left / 6 * 4;
@ -56,20 +53,6 @@ namespace ClassicalSharp {
vIndex.top = vIndex.bottom + Count.bottom / 6 * 4; vIndex.top = vIndex.bottom + Count.bottom / 6 * 4;
} }
void MakeIndices() {
int element = 0;
for( int i = 0; i < iCount; ) {
indices[i++] = (ushort)( element + 0 );
indices[i++] = (ushort)( element + 1 );
indices[i++] = (ushort)( element + 2 );
indices[i++] = (ushort)( element + 2 );
indices[i++] = (ushort)( element + 3 );
indices[i++] = (ushort)( element + 0 );
element += 4;
}
}
public void ResetState() { public void ResetState() {
vCount = iCount = 0; vCount = iCount = 0;
spriteIndex = spriteCount = 0; spriteIndex = spriteCount = 0;
@ -147,7 +130,6 @@ namespace ClassicalSharp {
ChunkPartInfo info; ChunkPartInfo info;
info.VbId = Graphics.InitVb( part.vertices, VertexFormat.Pos3fTex2fCol4b, part.vCount ); info.VbId = Graphics.InitVb( part.vertices, VertexFormat.Pos3fTex2fCol4b, part.vCount );
info.IbId = Graphics.InitIb( part.indices, part.iCount );
info.IndicesCount = part.iCount; info.IndicesCount = part.iCount;
info.leftCount = (ushort)part.Count.left; info.rightCount = (ushort)part.Count.right; info.leftCount = (ushort)part.Count.left; info.rightCount = (ushort)part.Count.right;
info.frontCount = (ushort)part.Count.front; info.backCount = (ushort)part.Count.back; info.frontCount = (ushort)part.Count.front; info.backCount = (ushort)part.Count.back;

View File

@ -50,7 +50,7 @@ namespace ClassicalSharp {
} }
void DrawPart( ChunkInfo info, ref ChunkPartInfo part ) { void DrawPart( ChunkInfo info, ref ChunkPartInfo part ) {
api.BindIndexedVb( part.VbId, part.IbId ); api.BindVb( part.VbId );
bool drawLeft = info.DrawLeft && part.leftCount > 0; bool drawLeft = info.DrawLeft && part.leftCount > 0;
bool drawRight = info.DrawRight && part.rightCount > 0; bool drawRight = info.DrawRight && part.rightCount > 0;
bool drawBottom = info.DrawBottom && part.bottomCount > 0; bool drawBottom = info.DrawBottom && part.bottomCount > 0;
@ -83,7 +83,7 @@ namespace ClassicalSharp {
if( part.IndicesCount > maxIndices ) { if( part.IndicesCount > maxIndices ) {
int part1Count = maxIndices - part.bottomIndex; int part1Count = maxIndices - part.bottomIndex;
api.DrawIndexedVb( mode, part1Count, 0, part.bottomIndex ); api.DrawIndexedVb( mode, part1Count, 0, part.bottomIndex );
api.DrawIndexedVb( mode, part.bottomCount + part.topCount - part1Count, maxVertex, part.bottomIndex ); api.DrawIndexedVb( mode, part.bottomCount + part.topCount - part1Count, maxVertex, 0 );
} else { } else {
api.DrawIndexedVb( mode, part.bottomCount + part.topCount, 0, part.bottomIndex ); api.DrawIndexedVb( mode, part.bottomCount + part.topCount, 0, part.bottomIndex );
} }
@ -93,7 +93,7 @@ namespace ClassicalSharp {
if( part.IndicesCount > maxIndices && if( part.IndicesCount > maxIndices &&
( part1Count = maxIndices - part.bottomIndex ) < part.bottomCount ) { ( part1Count = maxIndices - part.bottomIndex ) < part.bottomCount ) {
api.DrawIndexedVb( mode, part1Count, 0, part.bottomIndex ); api.DrawIndexedVb( mode, part1Count, 0, part.bottomIndex );
api.DrawIndexedVb( mode, part.bottomCount - part1Count, maxVertex, part.bottomIndex ); api.DrawIndexedVb( mode, part.bottomCount - part1Count, maxVertex, 0 );
} else { } else {
api.DrawIndexedVb( mode, part.bottomCount, 0, part.bottomIndex ); api.DrawIndexedVb( mode, part.bottomCount, 0, part.bottomIndex );
} }
@ -102,7 +102,7 @@ namespace ClassicalSharp {
if( part.IndicesCount > maxIndices && if( part.IndicesCount > maxIndices &&
( part1Count = maxIndices - part.topIndex ) < part.topCount ) { ( part1Count = maxIndices - part.topIndex ) < part.topCount ) {
api.DrawIndexedVb( mode, part1Count, 0, part.topIndex ); api.DrawIndexedVb( mode, part1Count, 0, part.topIndex );
api.DrawIndexedVb( mode, part.topCount - part1Count, maxVertex, part.topIndex ); api.DrawIndexedVb( mode, part.topCount - part1Count, maxVertex, 0 );
} else { } else {
api.DrawIndexedVb( mode, part.topCount, 0, part.topIndex ); api.DrawIndexedVb( mode, part.topCount, 0, part.topIndex );
} }

View File

@ -56,6 +56,7 @@ namespace ClassicalSharp {
game.OnNewMapLoaded -= OnNewMapLoaded; game.OnNewMapLoaded -= OnNewMapLoaded;
game.EnvVariableChanged -= EnvVariableChanged; game.EnvVariableChanged -= EnvVariableChanged;
builder.Dispose(); builder.Dispose();
api.DeleteIb( chunkIb );
} }
public void Refresh() { public void Refresh() {
@ -123,7 +124,6 @@ namespace ClassicalSharp {
for( int i = 0; i < parts.Length; i++ ) { for( int i = 0; i < parts.Length; i++ ) {
api.DeleteVb( parts[i].VbId ); api.DeleteVb( parts[i].VbId );
api.DeleteIb( parts[i].IbId );
} }
parts = null; parts = null;
} }
@ -188,11 +188,15 @@ namespace ClassicalSharp {
public void Render( double deltaTime ) { public void Render( double deltaTime ) {
if( chunks == null ) return; if( chunks == null ) return;
UpdateSortOrder(); UpdateSortOrder();
UpdateChunks(); UpdateChunks();
if( chunkIb == -1 )
MakeIndices();
api.BindIb( chunkIb );
RenderNormal(); RenderNormal();
game.MapEnvRenderer.RenderMapSides( deltaTime ); game.MapEnvRenderer.RenderMapSides( deltaTime );
game.MapEnvRenderer.RenderMapEdges( deltaTime ); game.MapEnvRenderer.RenderMapEdges( deltaTime );
api.BindIb( chunkIb );
RenderTranslucent(); RenderTranslucent();
} }
@ -257,7 +261,7 @@ namespace ClassicalSharp {
// Render solid and fully transparent to fill depth buffer. // Render solid and fully transparent to fill depth buffer.
// These blocks are treated as having an alpha value of either none or full. // These blocks are treated as having an alpha value of either none or full.
void RenderNormal() { void RenderNormal() {
int[] texIds = game.TerrainAtlas1D.TexIds; int[] texIds = game.TerrainAtlas1D.TexIds;
api.BeginIndexedVbBatch(); api.BeginIndexedVbBatch();
api.Texturing = true; api.Texturing = true;
@ -272,11 +276,28 @@ namespace ClassicalSharp {
api.EndIndexedVbBatch(); api.EndIndexedVbBatch();
} }
int chunkIb = -1;
void MakeIndices() {
int element = 0;
ushort[] indices = new ushort[maxIndices];
for( int i = 0; i < indices.Length; ) {
indices[i++] = (ushort)( element + 0 );
indices[i++] = (ushort)( element + 1 );
indices[i++] = (ushort)( element + 2 );
indices[i++] = (ushort)( element + 2 );
indices[i++] = (ushort)( element + 3 );
indices[i++] = (ushort)( element + 0 );
element += 4;
}
chunkIb = api.InitIb( indices, indices.Length );
}
// Render translucent(liquid) blocks. These 'blend' into other blocks. // Render translucent(liquid) blocks. These 'blend' into other blocks.
void RenderTranslucent() { void RenderTranslucent() {
// First fill depth buffer // First fill depth buffer
int[] texIds = game.TerrainAtlas1D.TexIds; int[] texIds = game.TerrainAtlas1D.TexIds;
api.BeginIndexedVbBatch(); api.BeginIndexedVbBatch();
api.Texturing = false; api.Texturing = false;
api.AlphaBlending = false; api.AlphaBlending = false;
api.ColourWrite = false; api.ColourWrite = false;