From ee5d8c452938f7104e32ab3742b62528061c46f9 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 6 Jun 2015 10:41:28 +1000 Subject: [PATCH] Fix 2D in Direct3D api, optimise MatrixStack. --- GraphicsAPI/DirectXApi.cs | 34 +++++++++++++++++++--------------- GraphicsAPI/IGraphicsApi.cs | 9 ++++++--- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/GraphicsAPI/DirectXApi.cs b/GraphicsAPI/DirectXApi.cs index cc84e24ec..24043d35c 100644 --- a/GraphicsAPI/DirectXApi.cs +++ b/GraphicsAPI/DirectXApi.cs @@ -53,9 +53,9 @@ namespace ClassicalSharp.GraphicsAPI { caps = device.DeviceCaps; state = device.RenderState; - viewStack = new MatrixStack( 32, m => device.SetTransform( TransformType.View, m ) ); - projStack = new MatrixStack( 4, m => device.SetTransform( TransformType.Projection, m ) ); - texStack = new MatrixStack( 4, m => device.SetTransform( TransformType.Texture1, m ) ); // TODO: Texture0? + viewStack = new MatrixStack( 32, device, TransformType.View ); + projStack = new MatrixStack( 4, device, TransformType.Projection ); + texStack = new MatrixStack( 4, device, TransformType.Texture1 ); // TODO: Texture0? state.ColorVertex = false; state.Lighting = false; @@ -139,9 +139,6 @@ namespace ClassicalSharp.GraphicsAPI { } public override void Bind2DTexture( int texId ) { - if( texId == 0 ) { - device.SetTexture( 0, null ); - } device.SetTexture( 0, textures[texId] ); } @@ -197,7 +194,7 @@ namespace ClassicalSharp.GraphicsAPI { device.DrawUserPrimitives( modeMappings[(int)mode], NumPrimitives( count, mode ), vertices ); } - public override void DeleteDynamicVb(int id) { + public override void DeleteDynamicVb( int id ) { } FillMode[] fillModes = { FillMode.Point, FillMode.WireFrame, FillMode.Solid }; @@ -224,7 +221,7 @@ namespace ClassicalSharp.GraphicsAPI { } unsafe VertexBuffer CreateVb( T[] vertices, int count, VertexFormat format ) { - int sizeInBytes = GetSizeInBytes( count, format ); + int sizeInBytes = count * strideSizes[(int)format]; VertexFormats d3dFormat = formatMapping[(int)format]; VertexBuffer buffer = new VertexBuffer( device, sizeInBytes, Usage.None, d3dFormat, Pool.Managed ); @@ -325,8 +322,8 @@ namespace ClassicalSharp.GraphicsAPI { curStack.SetTop( ref dxMatrix ); } + Matrix identity = Matrix.Identity; public override void LoadIdentityMatrix() { - Matrix identity = Matrix.Identity; curStack.SetTop( ref identity ); } @@ -373,12 +370,14 @@ namespace ClassicalSharp.GraphicsAPI { { Matrix[] stack; int stackIndex; - Action dxSetMatrix; + Device device; + TransformType matrixType; - public MatrixStack( int capacity, Action dxSetter ) { + public MatrixStack( int capacity, Device device, TransformType matrixType ) { stack = new Matrix[capacity]; stack[0] = Matrix.Identity; - dxSetMatrix = dxSetter; + this.device = device; + this.matrixType = matrixType; } public void Push() { @@ -388,12 +387,12 @@ namespace ClassicalSharp.GraphicsAPI { public void SetTop( ref Matrix matrix ) { stack[stackIndex] = matrix; - dxSetMatrix( matrix ); + device.SetTransform( matrixType, stack[stackIndex] ); } public void MultiplyTop( ref Matrix matrix ) { stack[stackIndex] *= matrix; - dxSetMatrix( stack[stackIndex] ); + device.SetTransform( matrixType, stack[stackIndex] ); } public Matrix GetTop() { @@ -402,7 +401,7 @@ namespace ClassicalSharp.GraphicsAPI { public void Pop() { stackIndex--; - dxSetMatrix( stack[stackIndex] ); + device.SetTransform( matrixType, stack[stackIndex] ); } } @@ -476,6 +475,11 @@ namespace ClassicalSharp.GraphicsAPI { static bool IsValid( T[] array, int id ) { return id > 0 && id < array.Length && array[id] != null; } + + protected override void LoadOrthoMatrix( float width, float height ) { + Matrix dxMatrix = Matrix.OrthoOffCenterRH( 0, width, height, 0, 0, 1 ); + curStack.SetTop( ref dxMatrix ); + } public override void PrintApiSpecificInfo() { Console.WriteLine( "D3D tex memory available: " + (uint)device.AvailableTextureMemory ); diff --git a/GraphicsAPI/IGraphicsApi.cs b/GraphicsAPI/IGraphicsApi.cs index 09bc70270..6cc880189 100644 --- a/GraphicsAPI/IGraphicsApi.cs +++ b/GraphicsAPI/IGraphicsApi.cs @@ -229,16 +229,19 @@ namespace ClassicalSharp.GraphicsAPI { SetMatrixMode( MatrixType.Projection ); PushMatrix(); LoadIdentityMatrix(); - //GL.Ortho( 0, width, height, 0, 0, 1 ); DepthTest = false; - Matrix4 matrix = Matrix4.CreateOrthographicOffCenter( 0, width, height, 0, 0, 1 ); - LoadMatrix( ref matrix ); + LoadOrthoMatrix( width, height ); SetMatrixMode( MatrixType.Modelview ); PushMatrix(); LoadIdentityMatrix(); AlphaBlending = true; } + protected virtual void LoadOrthoMatrix( float width, float height ) { + Matrix4 matrix = Matrix4.CreateOrthographicOffCenter( 0, width, height, 0, 0, 1 ); + LoadMatrix( ref matrix ); + } + public void Mode3D() { // Get rid of orthographic 2D matrix. SetMatrixMode( MatrixType.Projection );