Fix 2D in Direct3D api, optimise MatrixStack.

This commit is contained in:
UnknownShadow200 2015-06-06 10:41:28 +10:00
parent fa22afb556
commit ee5d8c4529
2 changed files with 25 additions and 18 deletions

View File

@ -53,9 +53,9 @@ namespace ClassicalSharp.GraphicsAPI {
caps = device.DeviceCaps; caps = device.DeviceCaps;
state = device.RenderState; state = device.RenderState;
viewStack = new MatrixStack( 32, m => device.SetTransform( TransformType.View, m ) ); viewStack = new MatrixStack( 32, device, TransformType.View );
projStack = new MatrixStack( 4, m => device.SetTransform( TransformType.Projection, m ) ); projStack = new MatrixStack( 4, device, TransformType.Projection );
texStack = new MatrixStack( 4, m => device.SetTransform( TransformType.Texture1, m ) ); // TODO: Texture0? texStack = new MatrixStack( 4, device, TransformType.Texture1 ); // TODO: Texture0?
state.ColorVertex = false; state.ColorVertex = false;
state.Lighting = false; state.Lighting = false;
@ -139,9 +139,6 @@ namespace ClassicalSharp.GraphicsAPI {
} }
public override void Bind2DTexture( int texId ) { public override void Bind2DTexture( int texId ) {
if( texId == 0 ) {
device.SetTexture( 0, null );
}
device.SetTexture( 0, textures[texId] ); device.SetTexture( 0, textures[texId] );
} }
@ -197,7 +194,7 @@ namespace ClassicalSharp.GraphicsAPI {
device.DrawUserPrimitives( modeMappings[(int)mode], NumPrimitives( count, mode ), vertices ); 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 }; FillMode[] fillModes = { FillMode.Point, FillMode.WireFrame, FillMode.Solid };
@ -224,7 +221,7 @@ namespace ClassicalSharp.GraphicsAPI {
} }
unsafe VertexBuffer CreateVb<T>( T[] vertices, int count, VertexFormat format ) { unsafe VertexBuffer CreateVb<T>( T[] vertices, int count, VertexFormat format ) {
int sizeInBytes = GetSizeInBytes( count, format ); int sizeInBytes = count * strideSizes[(int)format];
VertexFormats d3dFormat = formatMapping[(int)format]; VertexFormats d3dFormat = formatMapping[(int)format];
VertexBuffer buffer = new VertexBuffer( device, sizeInBytes, Usage.None, d3dFormat, Pool.Managed ); VertexBuffer buffer = new VertexBuffer( device, sizeInBytes, Usage.None, d3dFormat, Pool.Managed );
@ -325,8 +322,8 @@ namespace ClassicalSharp.GraphicsAPI {
curStack.SetTop( ref dxMatrix ); curStack.SetTop( ref dxMatrix );
} }
Matrix identity = Matrix.Identity;
public override void LoadIdentityMatrix() { public override void LoadIdentityMatrix() {
Matrix identity = Matrix.Identity;
curStack.SetTop( ref identity ); curStack.SetTop( ref identity );
} }
@ -373,12 +370,14 @@ namespace ClassicalSharp.GraphicsAPI {
{ {
Matrix[] stack; Matrix[] stack;
int stackIndex; int stackIndex;
Action<Matrix> dxSetMatrix; Device device;
TransformType matrixType;
public MatrixStack( int capacity, Action<Matrix> dxSetter ) { public MatrixStack( int capacity, Device device, TransformType matrixType ) {
stack = new Matrix[capacity]; stack = new Matrix[capacity];
stack[0] = Matrix.Identity; stack[0] = Matrix.Identity;
dxSetMatrix = dxSetter; this.device = device;
this.matrixType = matrixType;
} }
public void Push() { public void Push() {
@ -388,12 +387,12 @@ namespace ClassicalSharp.GraphicsAPI {
public void SetTop( ref Matrix matrix ) { public void SetTop( ref Matrix matrix ) {
stack[stackIndex] = matrix; stack[stackIndex] = matrix;
dxSetMatrix( matrix ); device.SetTransform( matrixType, stack[stackIndex] );
} }
public void MultiplyTop( ref Matrix matrix ) { public void MultiplyTop( ref Matrix matrix ) {
stack[stackIndex] *= matrix; stack[stackIndex] *= matrix;
dxSetMatrix( stack[stackIndex] ); device.SetTransform( matrixType, stack[stackIndex] );
} }
public Matrix GetTop() { public Matrix GetTop() {
@ -402,7 +401,7 @@ namespace ClassicalSharp.GraphicsAPI {
public void Pop() { public void Pop() {
stackIndex--; stackIndex--;
dxSetMatrix( stack[stackIndex] ); device.SetTransform( matrixType, stack[stackIndex] );
} }
} }
@ -476,6 +475,11 @@ namespace ClassicalSharp.GraphicsAPI {
static bool IsValid<T>( T[] array, int id ) { static bool IsValid<T>( T[] array, int id ) {
return id > 0 && id < array.Length && array[id] != null; 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() { public override void PrintApiSpecificInfo() {
Console.WriteLine( "D3D tex memory available: " + (uint)device.AvailableTextureMemory ); Console.WriteLine( "D3D tex memory available: " + (uint)device.AvailableTextureMemory );

View File

@ -229,16 +229,19 @@ namespace ClassicalSharp.GraphicsAPI {
SetMatrixMode( MatrixType.Projection ); SetMatrixMode( MatrixType.Projection );
PushMatrix(); PushMatrix();
LoadIdentityMatrix(); LoadIdentityMatrix();
//GL.Ortho( 0, width, height, 0, 0, 1 );
DepthTest = false; DepthTest = false;
Matrix4 matrix = Matrix4.CreateOrthographicOffCenter( 0, width, height, 0, 0, 1 ); LoadOrthoMatrix( width, height );
LoadMatrix( ref matrix );
SetMatrixMode( MatrixType.Modelview ); SetMatrixMode( MatrixType.Modelview );
PushMatrix(); PushMatrix();
LoadIdentityMatrix(); LoadIdentityMatrix();
AlphaBlending = true; 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() { public void Mode3D() {
// Get rid of orthographic 2D matrix. // Get rid of orthographic 2D matrix.
SetMatrixMode( MatrixType.Projection ); SetMatrixMode( MatrixType.Projection );