Use stackalloc to avoid allocating index buffer on LOH, remove Is<X> methods in IGraphicsApi and make IGraphicsApi more consistent.

This commit is contained in:
UnknownShadow200 2015-08-04 06:32:12 +10:00
parent 281e815125
commit efc7b0bfe6
18 changed files with 57 additions and 110 deletions

View file

@ -180,10 +180,6 @@ namespace ClassicalSharp.GraphicsAPI {
Delete( textures, texId );
texId = -1;
}
public override bool IsValidTexture( int texId ) {
return texId < textures.Length && textures[texId] != null;
}
int lastClearCol;
public override void Clear() {
@ -233,13 +229,21 @@ namespace ClassicalSharp.GraphicsAPI {
D3D.VertexFormat.Position | D3D.VertexFormat.Texture2 | D3D.VertexFormat.Diffuse,
};
public override int InitVb<T>( T[] vertices, VertexFormat format, int count ) {
public override int CreateVb<T>( T[] vertices, VertexFormat format, int count ) {
VertexBuffer buffer = CreateVb( vertices, count, format );
return GetOrExpand( ref vBuffers, buffer, vBufferSize );
}
public override int InitIb( ushort[] indices, int count ) {
IndexBuffer buffer = CreateIb( indices, count );
public unsafe override int CreateIb( ushort[] indices, int count ) {
IndexBuffer buffer;
fixed( ushort* src = indices )
buffer = CreateIndexBuffer( (IntPtr)src, count );
return GetOrExpand( ref iBuffers, buffer, iBufferSize );
}
public override int CreateIb( IntPtr indices, int count ) {
IndexBuffer buffer = CreateIndexBuffer( indices, count );
return GetOrExpand( ref iBuffers, buffer, iBufferSize );
}
@ -257,14 +261,12 @@ namespace ClassicalSharp.GraphicsAPI {
return buffer;
}
unsafe IndexBuffer CreateIb( ushort[] indices, int count ) {
int sizeInBytes = count * 2;
unsafe IndexBuffer CreateIndexBuffer( IntPtr src, int count ) {
int sizeInBytes = count * sizeof( ushort );
IndexBuffer buffer = new IndexBuffer( device, sizeInBytes, Usage.None, Pool.Managed, true );
IntPtr vbData = buffer.Lock( 0, sizeInBytes, LockFlags.None );
fixed( ushort* src = indices ) {
memcpy( (IntPtr)src, vbData, sizeInBytes );
}
memcpy( src, vbData, sizeInBytes );
buffer.Unlock();
return buffer;
}
@ -276,14 +278,6 @@ namespace ClassicalSharp.GraphicsAPI {
public override void DeleteIb( int ib ) {
Delete( iBuffers, ib );
}
public override bool IsValidVb( int vb ) {
return IsValid( vBuffers, vb );
}
public override bool IsValidIb( int ib ) {
return IsValid( iBuffers, ib );
}
public override void DrawVb( DrawMode mode, VertexFormat format, int id, int startVertex, int verticesCount ) {
device.SetStreamSource( 0, vBuffers[id], 0, strideSizes[(int)format] );
@ -320,10 +314,6 @@ namespace ClassicalSharp.GraphicsAPI {
indicesCount / 6 * 4, startIndex, NumPrimitives( indicesCount, mode ) );
}
public override void EndIndexedVbBatch() {
device.SetIndices( null );
}
#endregion
@ -550,10 +540,6 @@ namespace ClassicalSharp.GraphicsAPI {
return vertices / 2;
}
static bool IsValid<T>( T[] array, int id ) {
return id > 0 && id < array.Length && array[id] != null;
}
protected unsafe override void LoadOrthoMatrix( float width, float height ) {
Matrix4 matrix = Matrix4.CreateOrthographicOffCenter( 0, width, height, 0, 0, 1 );
matrix.M33 = -1;

View file

@ -51,8 +51,6 @@ namespace ClassicalSharp.GraphicsAPI {
DeleteTexture( ref texture.ID );
}
public abstract bool IsValidTexture( int texId );
/// <summary> Whether fog is currently enabled. </summary>
public abstract bool Fog { set; }
@ -100,21 +98,19 @@ namespace ClassicalSharp.GraphicsAPI {
public abstract int CreateDynamicVb( VertexFormat format, int maxVertices );
public abstract void DrawDynamicVb<T>( DrawMode mode, int vb, T[] vertices, VertexFormat format, int count ) where T : struct;
public virtual int InitVb<T>( T[] vertices, VertexFormat format ) where T : struct {
return InitVb( vertices, format, vertices.Length );
public virtual int CreateVb<T>( T[] vertices, VertexFormat format ) where T : struct {
return CreateVb( vertices, format, vertices.Length );
}
public abstract int InitVb<T>( T[] vertices, VertexFormat format, int count ) where T : struct;
public abstract int CreateVb<T>( T[] vertices, VertexFormat format, int count ) where T : struct;
public abstract int InitIb( ushort[] indices, int indicesCount );
public abstract int CreateIb( ushort[] indices, int indicesCount );
public abstract int InitIb( IntPtr indices, int indicesCount );
public abstract int CreateIb( IntPtr indices, int indicesCount );
public abstract bool IsValidVb( int vb );
public abstract void BindVb( int vb );
public abstract bool IsValidIb( int ib );
public abstract void BindIb( int ib );
public abstract void DeleteDynamicVb( int id );
@ -122,6 +118,8 @@ namespace ClassicalSharp.GraphicsAPI {
public abstract void DeleteIb( int ib );
public abstract void DrawDynamicVb<T>( DrawMode mode, int vb, T[] vertices, VertexFormat format, int count ) where T : struct;
public abstract void DrawVb( DrawMode mode, VertexFormat format, int id, int startVertex, int verticesCount );
public abstract void BeginVbBatch( VertexFormat format );
@ -130,14 +128,8 @@ namespace ClassicalSharp.GraphicsAPI {
public abstract void BeginIndexedVbBatch();
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 EndIndexedVbBatch();
protected static int[] strideSizes = { 20, 16, 24 };
public abstract void SetMatrixMode( MatrixType mode );

View file

@ -139,10 +139,6 @@ namespace ClassicalSharp.GraphicsAPI {
texId = -1;
}
public override bool IsValidTexture( int texId ) {
return GL.IsTexture( texId );
}
public override bool Texturing {
set { ToggleCap( EnableCap.Texture2D, value ); }
}
@ -193,21 +189,21 @@ namespace ClassicalSharp.GraphicsAPI {
return id;
}
public unsafe override int InitVb<T>( T[] vertices, VertexFormat format, int count ) {
public unsafe override int CreateVb<T>( T[] vertices, VertexFormat format, int count ) {
int id = GenAndBind( BufferTarget.ArrayBuffer );
int sizeInBytes = count * strideSizes[(int)format];
GL.BufferDataARB( BufferTarget.ArrayBuffer, new IntPtr( sizeInBytes ), vertices, BufferUsageHint.StaticDraw );
return id;
}
public unsafe override int InitIb( ushort[] indices, int indicesCount ) {
public unsafe override int CreateIb( ushort[] indices, int indicesCount ) {
int id = GenAndBind( BufferTarget.ElementArrayBuffer );
int sizeInBytes = indicesCount * sizeof( ushort );
GL.BufferDataARB( BufferTarget.ElementArrayBuffer, new IntPtr( sizeInBytes ), indices, BufferUsageHint.StaticDraw );
return id;
}
public unsafe override int InitIb( IntPtr indices, int indicesCount ) {
public unsafe override int CreateIb( IntPtr indices, int indicesCount ) {
int id = GenAndBind( BufferTarget.ElementArrayBuffer );
int sizeInBytes = indicesCount * sizeof( ushort );
GL.BufferDataARB( BufferTarget.ElementArrayBuffer, new IntPtr( sizeInBytes ), indices, BufferUsageHint.StaticDraw );
@ -246,14 +242,6 @@ namespace ClassicalSharp.GraphicsAPI {
GL.DeleteBuffersARB( 1, &id );
}
public override bool IsValidVb( int vb ) {
return GL.IsBufferARB( vb );
}
public override bool IsValidIb( int ib ) {
return GL.IsBufferARB( ib );
}
public override void DrawVb( DrawMode mode, VertexFormat format, int id, int startVertex, int verticesCount ) {
BeginVbBatch( format );
GL.BindBufferARB( BufferTarget.ArrayBuffer, id );
@ -315,10 +303,6 @@ namespace ClassicalSharp.GraphicsAPI {
GL.DrawElements( modeMappings[(int)mode], indicesCount, indexType, new IntPtr( startIndex * 2 ) );
}
public override void EndIndexedVbBatch() {
GL.BindBufferARB( BufferTarget.ElementArrayBuffer, 0 );
}
IntPtr zero = new IntPtr( 0 ), twelve = new IntPtr( 12 ), sixteen = new IntPtr( 16 );
void SetupVbPos3fTex2f() {
GL.VertexPointer( 3, PointerType.Float, VertexPos3fTex2f.Size, zero );

View file

@ -129,7 +129,7 @@ namespace ClassicalSharp {
if( part.iCount == 0 ) return;
ChunkPartInfo info;
info.VbId = Graphics.InitVb( part.vertices, VertexFormat.Pos3fTex2fCol4b, part.vCount );
info.VbId = Graphics.CreateVb( part.vertices, VertexFormat.Pos3fTex2fCol4b, part.vCount );
info.IndicesCount = part.iCount;
info.leftCount = (ushort)part.Count.left; info.rightCount = (ushort)part.Count.right;
info.frontCount = (ushort)part.Count.front; info.backCount = (ushort)part.Count.back;

View file

@ -18,7 +18,7 @@ namespace ClassicalSharp.Model {
LeftWing = MakeWing( -0.25f, -0.1875f );
RightWing = MakeWing( 0.1875f, 0.25f );
vb = graphics.InitVb( vertices, VertexFormat.Pos3fTex2fCol4b );
vb = graphics.CreateVb( vertices, VertexFormat.Pos3fTex2fCol4b );
vertices = null;
DefaultTexId = graphics.LoadTexture( "chicken.png" );
}

View file

@ -16,7 +16,7 @@ namespace ClassicalSharp.Model {
LeftLegBack = MakeLeg( -0.25f, 0, 0.125f, 0.375f );
RightLegBack = MakeLeg( 0, 0.25f, 0.125f, 0.375f );
vb = graphics.InitVb( vertices, VertexFormat.Pos3fTex2fCol4b );
vb = graphics.CreateVb( vertices, VertexFormat.Pos3fTex2fCol4b );
vertices = null;
DefaultTexId = graphics.LoadTexture( "creeper.png" );
}

View file

@ -16,7 +16,7 @@ namespace ClassicalSharp.Model {
LeftLegBack = MakeLeg( -0.3125f, -0.0625f, 0.3125f, 0.5625f );
RightLegBack = MakeLeg( 0.0625f, 0.3125f, 0.3125f, 0.5625f );
vb = graphics.InitVb( vertices, VertexFormat.Pos3fTex2fCol4b );
vb = graphics.CreateVb( vertices, VertexFormat.Pos3fTex2fCol4b );
vertices = null;
DefaultTexId = graphics.LoadTexture( "pig.png" );
}

View file

@ -38,7 +38,7 @@ namespace ClassicalSharp.Model {
Set64x64Slim.RightArm = MakeRightArm( 40, 16, 0.25f, 0.4375f, 3, true );
Set64x64Slim.Hat = Set64x64.Hat;
vb = graphics.InitVb( vertices, VertexFormat.Pos3fTex2fCol4b );
vb = graphics.CreateVb( vertices, VertexFormat.Pos3fTex2fCol4b );
vertices = null;
using( Bitmap bmp = new Bitmap( "char.png" ) ) {

View file

@ -27,7 +27,7 @@ namespace ClassicalSharp.Model {
FurRightLegBack = MakeFurLeg( 0.03125f, 0.34375f, 0.28125f, 0.59375f );
}
vb = graphics.InitVb( vertices, VertexFormat.Pos3fTex2fCol4b );
vb = graphics.CreateVb( vertices, VertexFormat.Pos3fTex2fCol4b );
vertices = null;
DefaultTexId = graphics.LoadTexture( "sheep.png" );
furTextureId = graphics.LoadTexture( "sheep_fur.png" );

View file

@ -16,7 +16,7 @@ namespace ClassicalSharp.Model {
LeftArm = MakeLeftArm( 0.375f, 0.25f );
RightArm = MakeRightArm( 0.25f, 0.375f );
vb = graphics.InitVb( vertices, VertexFormat.Pos3fTex2fCol4b );
vb = graphics.CreateVb( vertices, VertexFormat.Pos3fTex2fCol4b );
vertices = null;
DefaultTexId = graphics.LoadTexture( "skeleton.png" );
}

View file

@ -15,7 +15,7 @@ namespace ClassicalSharp.Model {
LeftLeg = MakeLeg( -1.1875f, -0.1875f );
RightLeg = MakeLeg( 0.1875f, 1.1875f );
vb = graphics.InitVb( vertices, VertexFormat.Pos3fTex2fCol4b );
vb = graphics.CreateVb( vertices, VertexFormat.Pos3fTex2fCol4b );
vertices = null;
DefaultTexId = graphics.LoadTexture( "spider.png" );
}

View file

@ -16,7 +16,7 @@ namespace ClassicalSharp.Model {
LeftArm = MakeLeftArm( 0.5f, 0.25f );
RightArm = MakeRightArm( 0.25f, 0.5f );
vb = graphics.InitVb( vertices, VertexFormat.Pos3fTex2fCol4b );
vb = graphics.CreateVb( vertices, VertexFormat.Pos3fTex2fCol4b );
vertices = null;
DefaultTexId = graphics.LoadTexture( "zombie.png" );
}

View file

@ -161,7 +161,7 @@ namespace ClassicalSharp {
DrawZPlane( Map.Length, 0, Map.Width, 0, groundLevel, sidesCol, vertices );
DrawXPlane( 0, 0, Map.Length, 0, groundLevel, sidesCol, vertices );
DrawXPlane( Map.Width, 0, Map.Length, 0, groundLevel, sidesCol, vertices );
sidesVboId = Graphics.InitVb( vertices, VertexFormat.Pos3fTex2fCol4b );
sidesVboId = Graphics.CreateVb( vertices, VertexFormat.Pos3fTex2fCol4b );
}
void RebuildEdgesModern( int waterLevel ) {
@ -171,7 +171,7 @@ namespace ClassicalSharp {
foreach( Rectangle rec in OutsideMap( Window.ViewDistance ) ) {
DrawYPlane( rec.X, rec.Y, rec.X + rec.Width, rec.Y + rec.Height, waterLevel, edgesCol, vertices );
}
edgesVboId = Graphics.InitVb( vertices, VertexFormat.Pos3fTex2fCol4b );
edgesVboId = Graphics.CreateVb( vertices, VertexFormat.Pos3fTex2fCol4b );
}
#endregion
@ -196,7 +196,7 @@ namespace ClassicalSharp {
DrawZPlaneParts( Map.Length, 0, Map.Width, 0, groundLevel, sidesCol, vertices );
DrawXPlaneParts( 0, 0, Map.Length, 0, groundLevel, sidesCol, vertices );
DrawXPlaneParts( Map.Width, 0, Map.Length, 0, groundLevel, sidesCol, vertices );
sidesVboId = Graphics.InitVb( vertices, VertexFormat.Pos3fTex2fCol4b );
sidesVboId = Graphics.CreateVb( vertices, VertexFormat.Pos3fTex2fCol4b );
}
void RebuildEdgesLegacy( int waterLevel ) {
@ -209,7 +209,7 @@ namespace ClassicalSharp {
foreach( Rectangle rec in OutsideMap( Window.ViewDistance ) ) {
DrawYPlaneParts( rec.X, rec.Y, rec.X + rec.Width, rec.Y + rec.Height, waterLevel, edgesCol, vertices );
}
edgesVboId = Graphics.InitVb( vertices, VertexFormat.Pos3fTex2fCol4b );
edgesVboId = Graphics.CreateVb( vertices, VertexFormat.Pos3fTex2fCol4b );
}
const int axisSize = 128;

View file

@ -273,24 +273,25 @@ namespace ClassicalSharp {
}
api.AlphaTest = false;
api.Texturing = false;
api.EndIndexedVbBatch();
}
int chunkIb = -1;
void MakeIndices() {
unsafe 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 );
ushort* indices = stackalloc ushort[maxIndices];
IntPtr ptr = (IntPtr)indices;
for( int i = 0; i < maxIndices; i += 6 ) {
*indices++ = (ushort)( element + 0 );
*indices++ = (ushort)( element + 1 );
*indices++ = (ushort)( element + 2 );
indices[i++] = (ushort)( element + 2 );
indices[i++] = (ushort)( element + 3 );
indices[i++] = (ushort)( element + 0 );
*indices++ = (ushort)( element + 2 );
*indices++ = (ushort)( element + 3 );
*indices++ = (ushort)( element + 0 );
element += 4;
}
chunkIb = api.InitIb( indices, indices.Length );
chunkIb = api.CreateIb( ptr, maxIndices );
}
// Render translucent(liquid) blocks. These 'blend' into other blocks.
@ -319,7 +320,6 @@ namespace ClassicalSharp {
api.AlphaTest = false;
api.AlphaBlending = false;
api.Texturing = false;
api.EndIndexedVbBatch();
}
}
}

View file

@ -175,14 +175,14 @@ namespace ClassicalSharp.Renderers {
cloudsVertices = 6;
VertexPos3fTex2fCol4b[] vertices = new VertexPos3fTex2fCol4b[cloudsVertices];
DrawCloudsYPlane( x1, z1, x2, z2, y, Map.CloudsCol, vertices );
cloudsVbo = Graphics.InitVb( vertices, VertexFormat.Pos3fTex2fCol4b );
cloudsVbo = Graphics.CreateVb( vertices, VertexFormat.Pos3fTex2fCol4b );
}
void ResetSkyModern( int y, int x1, int z1, int x2, int z2 ) {
skyVertices = 6;
VertexPos3fCol4b[] vertices = new VertexPos3fCol4b[skyVertices];
DrawSkyYPlane( x1, z1, x2, z2, y, Map.SkyCol, vertices );
skyVbo = Graphics.InitVb( vertices, VertexFormat.Pos3fCol4b );
skyVbo = Graphics.CreateVb( vertices, VertexFormat.Pos3fCol4b );
}
#endregion
@ -193,14 +193,14 @@ namespace ClassicalSharp.Renderers {
cloudsVertices = Utils.CountVertices( x2 - x1, z2 - z1, 128 );
VertexPos3fTex2fCol4b[] vertices = new VertexPos3fTex2fCol4b[cloudsVertices];
DrawCloudsYPlaneParts( x1, z1, x2, z2, y, Map.CloudsCol, vertices );
cloudsVbo = Graphics.InitVb( vertices, VertexFormat.Pos3fTex2fCol4b );
cloudsVbo = Graphics.CreateVb( vertices, VertexFormat.Pos3fTex2fCol4b );
}
void ResetSkyLegacy( int y, int x1, int z1, int x2, int z2 ) {
skyVertices = Utils.CountVertices( x2 - x1, z2 - z1, 128 );
VertexPos3fCol4b[] vertices = new VertexPos3fCol4b[skyVertices];
DrawSkyYPlaneParts( x1, z1, x2, z2, y, Map.SkyCol, vertices );
skyVbo = Graphics.InitVb( vertices, VertexFormat.Pos3fCol4b );
skyVbo = Graphics.CreateVb( vertices, VertexFormat.Pos3fCol4b );
}
void DrawSkyYPlaneParts( int x1, int z1, int x2, int z2, int y, FastColour col, VertexPos3fCol4b[] vertices ) {

View file

@ -51,7 +51,7 @@ namespace ClassicalSharp.Selections {
Line( ref index, p2.X, p1.Y, p1.Z, p2.X, p2.Y, p1.Z, lineCol );
Line( ref index, p2.X, p1.Y, p2.Z, p2.X, p2.Y, p2.Z, lineCol );
Line( ref index, p1.X, p1.Y, p2.Z, p1.X, p2.Y, p2.Z, lineCol );
LineVb = Graphics.InitVb( vertices, VertexFormat.Pos3fCol4b, LineVerticesCount );
LineVb = Graphics.CreateVb( vertices, VertexFormat.Pos3fCol4b, LineVerticesCount );
index = 0;
RenderYPlane( ref index, p1.X, p1.Z, p2.X, p2.Z, p1.Y, col ); // bottom
@ -60,7 +60,7 @@ namespace ClassicalSharp.Selections {
RenderXPlane( ref index, p1.X, p2.X, p1.Y, p2.Y, p2.Z, col );
RenderZPlane( ref index, p1.Z, p2.Z, p1.Y, p2.Y, p1.X, col );
RenderZPlane( ref index, p1.Z, p2.Z, p1.Y, p2.Y, p2.X, col );
Vb = Graphics.InitVb( vertices, VertexFormat.Pos3fCol4b, VerticesCount );
Vb = Graphics.CreateVb( vertices, VertexFormat.Pos3fCol4b, VerticesCount );
}
void Line( ref int index, float x1, float y1, float z1, float x2, float y2, float z2, FastColour col ) {

View file

@ -164,18 +164,6 @@ namespace OpenTK.Graphics.OpenGL {
Interop.Calli( (int)target, (int)mode, HintAddress );
} static IntPtr HintAddress;
public static bool IsBuffer( int buffer ) {
return Interop.Calli_UInt8( buffer, IsBufferAddress ) != 0;
} static IntPtr IsBufferAddress;
public static bool IsBufferARB( int buffer ) {
return Interop.Calli_UInt8( buffer, IsBufferARBAddress ) != 0;
} static IntPtr IsBufferARBAddress;
public static bool IsTexture( int texture ) {
return Interop.Calli_UInt8( texture, IsTextureAddress ) != 0;
} static IntPtr IsTextureAddress;
public static void LoadIdentity() {
Interop.Calli( LoadIdentityAddress );
} static IntPtr LoadIdentityAddress;

View file

@ -64,9 +64,6 @@ namespace OpenTK.Graphics.OpenGL
GetIntegervAddress = GetAddress( "glGetIntegerv" );
GetStringAddress = GetAddress( "glGetString" );
HintAddress = GetAddress( "glHint" );
IsBufferAddress = GetAddress( "glIsBuffer" );
IsBufferARBAddress = GetAddress( "glIsBufferARB" );
IsTextureAddress = GetAddress( "glIsTexture" );
LoadIdentityAddress = GetAddress( "glLoadIdentity" );
LoadMatrixfAddress = GetAddress( "glLoadMatrixf" );
MatrixModeAddress = GetAddress( "glMatrixMode" );