Simplify API further.

This commit is contained in:
UnknownShadow200 2015-08-04 07:03:20 +10:00
parent efc7b0bfe6
commit 749a1b0436
9 changed files with 60 additions and 77 deletions

View File

@ -230,7 +230,14 @@ namespace ClassicalSharp.GraphicsAPI {
}; };
public override int CreateVb<T>( T[] vertices, VertexFormat format, int count ) { public override int CreateVb<T>( T[] vertices, VertexFormat format, int count ) {
VertexBuffer buffer = CreateVb( vertices, count, format ); GCHandle handle = GCHandle.Alloc( vertices, GCHandleType.Pinned );
VertexBuffer buffer = CreateVertexBuffer( handle.AddrOfPinnedObject(), count, format );
handle.Free();
return GetOrExpand( ref vBuffers, buffer, vBufferSize );
}
public override int CreateVb( IntPtr vertices, VertexFormat format, int count ) {
VertexBuffer buffer = CreateVertexBuffer( vertices, count, format );
return GetOrExpand( ref vBuffers, buffer, vBufferSize ); return GetOrExpand( ref vBuffers, buffer, vBufferSize );
} }
@ -247,26 +254,22 @@ namespace ClassicalSharp.GraphicsAPI {
return GetOrExpand( ref iBuffers, buffer, iBufferSize ); return GetOrExpand( ref iBuffers, buffer, iBufferSize );
} }
unsafe VertexBuffer CreateVb<T>( T[] vertices, int count, VertexFormat format ) { unsafe VertexBuffer CreateVertexBuffer( IntPtr src, int count, VertexFormat format ) {
int sizeInBytes = count * strideSizes[(int)format]; int size = count * strideSizes[(int)format];
D3D.VertexFormat d3dFormat = formatMapping[(int)format]; VertexBuffer buffer = new VertexBuffer( device, size, Usage.None, formatMapping[(int)format], Pool.Managed );
VertexBuffer buffer = new VertexBuffer( device, sizeInBytes, Usage.None, d3dFormat, Pool.Managed );
IntPtr vbData = buffer.Lock( 0, sizeInBytes, LockFlags.None ); IntPtr vbData = buffer.Lock( 0, size, LockFlags.None );
GCHandle handle = GCHandle.Alloc( vertices, GCHandleType.Pinned ); memcpy( src, vbData, size );
IntPtr source = handle.AddrOfPinnedObject();
memcpy( source, vbData, sizeInBytes );
buffer.Unlock(); buffer.Unlock();
handle.Free();
return buffer; return buffer;
} }
unsafe IndexBuffer CreateIndexBuffer( IntPtr src, int count ) { unsafe IndexBuffer CreateIndexBuffer( IntPtr src, int count ) {
int sizeInBytes = count * sizeof( ushort ); int size = count * sizeof( ushort );
IndexBuffer buffer = new IndexBuffer( device, sizeInBytes, Usage.None, Pool.Managed, true ); IndexBuffer buffer = new IndexBuffer( device, size, Usage.None, Pool.Managed, true );
IntPtr vbData = buffer.Lock( 0, sizeInBytes, LockFlags.None ); IntPtr vbData = buffer.Lock( 0, size, LockFlags.None );
memcpy( src, vbData, sizeInBytes ); memcpy( src, vbData, size );
buffer.Unlock(); buffer.Unlock();
return buffer; return buffer;
} }
@ -279,28 +282,17 @@ namespace ClassicalSharp.GraphicsAPI {
Delete( iBuffers, ib ); Delete( 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] );
device.SetVertexFormat( formatMapping[(int)format] );
device.DrawPrimitives( modeMappings[(int)mode], startVertex, NumPrimitives( verticesCount, mode ) );
}
int batchStride; int batchStride;
public override void BeginVbBatch( VertexFormat format ) { public override void BeginVbBatch( VertexFormat format ) {
device.SetVertexFormat( formatMapping[(int)format] ); device.SetVertexFormat( formatMapping[(int)format] );
batchStride = strideSizes[(int)format]; batchStride = strideSizes[(int)format];
} }
public override void DrawVbBatch( DrawMode mode, int id, int startVertex, int verticesCount ) { public override void DrawVb( DrawMode mode, int id, int startVertex, int verticesCount ) {
device.SetStreamSource( 0, vBuffers[id], 0, batchStride ); device.SetStreamSource( 0, vBuffers[id], 0, batchStride );
device.DrawPrimitives( modeMappings[(int)mode], startVertex, NumPrimitives( verticesCount, mode ) ); device.DrawPrimitives( modeMappings[(int)mode], startVertex, NumPrimitives( verticesCount, mode ) );
} }
public override void BeginIndexedVbBatch() {
device.SetVertexFormat( formatMapping[(int)VertexFormat.Pos3fTex2fCol4b] );
batchStride = VertexPos3fTex2fCol4b.Size;
}
public override void BindVb( int vb ) { public override void BindVb( int vb ) {
device.SetStreamSource( 0, vBuffers[vb], 0, batchStride ); device.SetStreamSource( 0, vBuffers[vb], 0, batchStride );
} }

View File

@ -104,6 +104,8 @@ namespace ClassicalSharp.GraphicsAPI {
public abstract int CreateVb<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 CreateVb( IntPtr vertices, VertexFormat format, int count );
public abstract int CreateIb( ushort[] indices, int indicesCount ); public abstract int CreateIb( ushort[] indices, int indicesCount );
public abstract int CreateIb( IntPtr indices, int indicesCount ); public abstract int CreateIb( IntPtr indices, int indicesCount );
@ -120,13 +122,9 @@ namespace ClassicalSharp.GraphicsAPI {
public abstract void DrawDynamicVb<T>( DrawMode mode, int vb, T[] vertices, VertexFormat format, int count ) where T : struct; 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 ); public abstract void BeginVbBatch( VertexFormat format );
public abstract void DrawVbBatch( DrawMode mode, int id, int startVertex, int verticesCount ); public abstract void DrawVb( DrawMode mode, int id, int startVertex, int verticesCount );
public abstract void BeginIndexedVbBatch();
public abstract void DrawIndexedVb( DrawMode mode, int indicesCount, int startVertex, int startIndex ); public abstract void DrawIndexedVb( DrawMode mode, int indicesCount, int startVertex, int startIndex );

View File

@ -182,28 +182,35 @@ namespace ClassicalSharp.GraphicsAPI {
Action setupBatchFunc; Action setupBatchFunc;
Action setupBatchFuncTex2f, setupBatchFuncCol4b, setupBatchFuncTex2fCol4b; Action setupBatchFuncTex2f, setupBatchFuncCol4b, setupBatchFuncTex2fCol4b;
public unsafe override int CreateDynamicVb( VertexFormat format, int maxVertices ) { public override int CreateDynamicVb( VertexFormat format, int maxVertices ) {
int id = GenAndBind( BufferTarget.ArrayBuffer ); int id = GenAndBind( BufferTarget.ArrayBuffer );
int sizeInBytes = maxVertices * strideSizes[(int)format]; int sizeInBytes = maxVertices * strideSizes[(int)format];
GL.BufferDataARB( BufferTarget.ArrayBuffer, new IntPtr( sizeInBytes ), IntPtr.Zero, BufferUsageHint.DynamicDraw ); GL.BufferDataARB( BufferTarget.ArrayBuffer, new IntPtr( sizeInBytes ), IntPtr.Zero, BufferUsageHint.DynamicDraw );
return id; return id;
} }
public unsafe override int CreateVb<T>( T[] vertices, VertexFormat format, int count ) { public override int CreateVb<T>( T[] vertices, VertexFormat format, int count ) {
int id = GenAndBind( BufferTarget.ArrayBuffer ); int id = GenAndBind( BufferTarget.ArrayBuffer );
int sizeInBytes = count * strideSizes[(int)format]; int sizeInBytes = count * strideSizes[(int)format];
GL.BufferDataARB( BufferTarget.ArrayBuffer, new IntPtr( sizeInBytes ), vertices, BufferUsageHint.StaticDraw ); GL.BufferDataARB( BufferTarget.ArrayBuffer, new IntPtr( sizeInBytes ), vertices, BufferUsageHint.StaticDraw );
return id; return id;
} }
public unsafe override int CreateIb( ushort[] indices, int indicesCount ) { public override int CreateVb( IntPtr 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 override int CreateIb( ushort[] indices, int indicesCount ) {
int id = GenAndBind( BufferTarget.ElementArrayBuffer ); int id = GenAndBind( BufferTarget.ElementArrayBuffer );
int sizeInBytes = indicesCount * sizeof( ushort ); int sizeInBytes = indicesCount * sizeof( ushort );
GL.BufferDataARB( BufferTarget.ElementArrayBuffer, new IntPtr( sizeInBytes ), indices, BufferUsageHint.StaticDraw ); GL.BufferDataARB( BufferTarget.ElementArrayBuffer, new IntPtr( sizeInBytes ), indices, BufferUsageHint.StaticDraw );
return id; return id;
} }
public unsafe override int CreateIb( IntPtr indices, int indicesCount ) { public override int CreateIb( IntPtr indices, int indicesCount ) {
int id = GenAndBind( BufferTarget.ElementArrayBuffer ); int id = GenAndBind( BufferTarget.ElementArrayBuffer );
int sizeInBytes = indicesCount * sizeof( ushort ); int sizeInBytes = indicesCount * sizeof( ushort );
GL.BufferDataARB( BufferTarget.ElementArrayBuffer, new IntPtr( sizeInBytes ), indices, BufferUsageHint.StaticDraw ); GL.BufferDataARB( BufferTarget.ElementArrayBuffer, new IntPtr( sizeInBytes ), indices, BufferUsageHint.StaticDraw );
@ -242,13 +249,6 @@ namespace ClassicalSharp.GraphicsAPI {
GL.DeleteBuffersARB( 1, &id ); GL.DeleteBuffersARB( 1, &id );
} }
public override void DrawVb( DrawMode mode, VertexFormat format, int id, int startVertex, int verticesCount ) {
BeginVbBatch( format );
GL.BindBufferARB( BufferTarget.ArrayBuffer, id );
setupBatchFunc();
GL.DrawArrays( modeMappings[(int)mode], startVertex, verticesCount );
}
VertexFormat batchFormat = (VertexFormat)999; VertexFormat batchFormat = (VertexFormat)999;
public override void BeginVbBatch( VertexFormat format ) { public override void BeginVbBatch( VertexFormat format ) {
if( format == batchFormat ) return; if( format == batchFormat ) return;
@ -276,11 +276,7 @@ namespace ClassicalSharp.GraphicsAPI {
} }
} }
public override void BeginIndexedVbBatch() { public override void DrawVb( DrawMode mode, int id, int startVertex, int verticesCount ) {
BeginVbBatch( VertexFormat.Pos3fTex2fCol4b );
}
public override void DrawVbBatch( DrawMode mode, int id, int startVertex, int verticesCount ) {
GL.BindBufferARB( BufferTarget.ArrayBuffer, id ); GL.BindBufferARB( BufferTarget.ArrayBuffer, id );
setupBatchFunc(); setupBatchFunc();
GL.DrawArrays( modeMappings[(int)mode], startVertex, verticesCount ); GL.DrawArrays( modeMappings[(int)mode], startVertex, verticesCount );

View File

@ -27,6 +27,7 @@ namespace ClassicalSharp.Model {
graphics.PushMatrix(); graphics.PushMatrix();
Matrix4 mat = Matrix4.RotateY( -p.YawRadians ) * Matrix4.Translate( p.Position ); Matrix4 mat = Matrix4.RotateY( -p.YawRadians ) * Matrix4.Translate( p.Position );
graphics.MultiplyMatrix( ref mat ); graphics.MultiplyMatrix( ref mat );
graphics.BeginVbBatch( VertexFormat.Pos3fTex2fCol4b );
DrawPlayerModel( p ); DrawPlayerModel( p );
graphics.PopMatrix(); graphics.PopMatrix();
} }

View File

@ -16,7 +16,7 @@ namespace ClassicalSharp {
} }
public void Render( int vb ) { public void Render( int vb ) {
Graphics.DrawVb( DrawMode.Triangles, VertexFormat.Pos3fTex2fCol4b, vb, Offset, Count ); Graphics.DrawVb( DrawMode.Triangles, vb, Offset, Count );
} }
} }

View File

@ -40,26 +40,21 @@ namespace ClassicalSharp {
ResetSidesAndEdges( null, null ); ResetSidesAndEdges( null, null );
} }
public void RenderMapSides( double deltaTime ) { public void Render( double deltaTime ) {
if( sidesVboId == -1 ) return; if( sidesVboId == -1 || edgesVboId == -1 ) return;
Graphics.Texturing = true; Graphics.Texturing = true;
Graphics.Bind2DTexture( sideTexId ); Graphics.Bind2DTexture( sideTexId );
Graphics.DrawVb( DrawMode.Triangles, VertexFormat.Pos3fTex2fCol4b, sidesVboId, 0, sidesVertices ); Graphics.BeginVbBatch( VertexFormat.Pos3fTex2fCol4b );
Graphics.Texturing = false; Graphics.DrawVb( DrawMode.Triangles, sidesVboId, 0, sidesVertices );
}
public void RenderMapEdges( double deltaTime ) {
if( edgesVboId == -1 ) return;
// Do not draw water when we cannot see it. // Do not draw water when we cannot see it.
// Fixes 'depth bleeding through' issues with 16 bit depth buffers on large maps. // Fixes 'depth bleeding through' issues with 16 bit depth buffers on large maps.
if( Window.LocalPlayer.EyePosition.Y < 0 ) return; if( Window.LocalPlayer.EyePosition.Y >= 0 ) {
Graphics.Texturing = true;
Graphics.AlphaBlending = true; Graphics.AlphaBlending = true;
Graphics.Bind2DTexture( edgeTexId ); Graphics.Bind2DTexture( edgeTexId );
Graphics.DrawVb( DrawMode.Triangles, VertexFormat.Pos3fTex2fCol4b, edgesVboId, 0, edgesVertices ); Graphics.DrawVb( DrawMode.Triangles, edgesVboId, 0, edgesVertices );
Graphics.AlphaBlending = false; Graphics.AlphaBlending = false;
}
Graphics.Texturing = false; Graphics.Texturing = false;
} }

View File

@ -194,8 +194,7 @@ namespace ClassicalSharp {
api.BindIb( chunkIb ); api.BindIb( chunkIb );
RenderNormal(); RenderNormal();
game.MapEnvRenderer.RenderMapSides( deltaTime ); game.MapEnvRenderer.Render( deltaTime );
game.MapEnvRenderer.RenderMapEdges( deltaTime );
api.BindIb( chunkIb ); api.BindIb( chunkIb );
RenderTranslucent(); RenderTranslucent();
} }
@ -263,7 +262,7 @@ namespace ClassicalSharp {
// 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.BeginVbBatch( VertexFormat.Pos3fTex2fCol4b );
api.Texturing = true; api.Texturing = true;
api.AlphaTest = true; api.AlphaTest = true;
@ -298,7 +297,7 @@ namespace ClassicalSharp {
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.BeginVbBatch( VertexFormat.Pos3fTex2fCol4b );
api.Texturing = false; api.Texturing = false;
api.AlphaBlending = false; api.AlphaBlending = false;
api.ColourWrite = false; api.ColourWrite = false;

View File

@ -29,7 +29,8 @@ namespace ClassicalSharp.Renderers {
Vector3 pos = Window.LocalPlayer.EyePosition; Vector3 pos = Window.LocalPlayer.EyePosition;
if( pos.Y < Map.Height + skyOffset ) { if( pos.Y < Map.Height + skyOffset ) {
Graphics.DrawVb( DrawMode.Triangles, VertexFormat.Pos3fCol4b, skyVbo, 0, skyVertices ); Graphics.BeginVbBatch( VertexFormat.Pos3fCol4b );
Graphics.DrawVb( DrawMode.Triangles, skyVbo, 0, skyVertices );
} }
RenderClouds( deltaTime ); RenderClouds( deltaTime );
ResetFog(); ResetFog();
@ -97,7 +98,8 @@ namespace ClassicalSharp.Renderers {
Graphics.AlphaTest = true; Graphics.AlphaTest = true;
Graphics.Texturing = true; Graphics.Texturing = true;
Graphics.Bind2DTexture( cloudTexture ); Graphics.Bind2DTexture( cloudTexture );
Graphics.DrawVb( DrawMode.Triangles, VertexFormat.Pos3fTex2fCol4b, cloudsVbo, 0, cloudsVertices ); Graphics.BeginVbBatch( VertexFormat.Pos3fTex2fCol4b );
Graphics.DrawVb( DrawMode.Triangles, cloudsVbo, 0, cloudsVertices );
Graphics.AlphaTest = false; Graphics.AlphaTest = false;
Graphics.Texturing = false; Graphics.Texturing = false;

View File

@ -19,9 +19,9 @@ namespace ClassicalSharp.Selections {
public void Render( double delta ) { public void Render( double delta ) {
Graphics.DepthWrite = false; Graphics.DepthWrite = false;
Graphics.DrawVbBatch( DrawMode.Triangles, Vb, 0, VerticesCount ); Graphics.DrawVb( DrawMode.Triangles, Vb, 0, VerticesCount );
Graphics.DepthWrite = true; Graphics.DepthWrite = true;
Graphics.DrawVbBatch( DrawMode.Lines, LineVb, 0, LineVerticesCount ); Graphics.DrawVb( DrawMode.Lines, LineVb, 0, LineVerticesCount );
} }
static VertexPos3fCol4b[] vertices = new VertexPos3fCol4b[VerticesCount]; static VertexPos3fCol4b[] vertices = new VertexPos3fCol4b[VerticesCount];