2012-09-08 10:37:41 +00:00
//#define DIRECTX_DEBUG_LAYER
2012-09-15 13:43:31 +00:00
#region Using Statements
2012-09-08 10:37:41 +00:00
using System ;
using ANX.Framework ;
using ANX.Framework.Graphics ;
using SharpDX.DXGI ;
2012-09-15 13:43:31 +00:00
#endregion
2012-09-08 10:37:41 +00:00
// This file is part of the ANX.Framework created by the
// "ANX.Framework developer group" and released under the Ms-PL license.
// For details see: http://anxframework.codeplex.com/license
2012-09-15 13:43:31 +00:00
#if DX10
2015-10-14 23:59:27 +02:00
using SharpDX.Direct3D10 ;
2012-09-15 13:43:31 +00:00
namespace ANX.RenderSystem.Windows.DX10
#endif
#if DX11
2015-10-14 23:59:27 +02:00
using SharpDX.Direct3D11 ;
2012-09-15 13:43:31 +00:00
namespace ANX.RenderSystem.Windows.DX11
#endif
2012-09-08 10:37:41 +00:00
{
2012-09-15 13:43:31 +00:00
public partial class GraphicsDeviceDX
2012-09-08 10:37:41 +00:00
{
#region Private
protected SharpDX . DXGI . SwapChain swapChain ;
2015-10-14 23:59:27 +02:00
protected Framework . Graphics . VertexBufferBinding [ ] currentVertexBuffer = new Framework . Graphics . VertexBufferBinding [ 0 ] ;
2012-09-22 11:17:36 +00:00
protected int currentVertexBufferCount ;
2012-09-08 10:37:41 +00:00
protected IndexBuffer currentIndexBuffer ;
2015-10-14 23:59:27 +02:00
private InputLayoutManager inputLayoutManager = new InputLayoutManager ( ) ;
private InputLayout currentInputLayout = null ;
2012-09-08 10:37:41 +00:00
#endregion
#region Public
public bool VSync { get ; set ; }
#endregion
#region Constructor
2012-09-15 13:43:31 +00:00
public GraphicsDeviceDX ( PresentationParameters presentationParameters )
2012-09-08 10:37:41 +00:00
{
VSync = true ;
CreateDevice ( presentationParameters ) ;
MakeWindowAssociationAndResize ( presentationParameters ) ;
2015-09-30 21:31:15 +02:00
CreateRenderView ( presentationParameters ) ;
2012-09-08 10:37:41 +00:00
}
#endregion
#region MakeWindowAssociationAndResize
protected void MakeWindowAssociationAndResize ( PresentationParameters presentationParameters )
{
// Ignore all windows events
var factory = swapChain . GetParent < SharpDX . DXGI . Factory > ( ) ;
factory . MakeWindowAssociation ( presentationParameters . DeviceWindowHandle ,
SharpDX . DXGI . WindowAssociationFlags . IgnoreAll ) ;
WindowHelper . ResizeRenderWindow ( presentationParameters ) ;
SetViewport ( 0 , 0 , presentationParameters . BackBufferWidth , presentationParameters . BackBufferHeight , 0f , 1f ) ;
}
#endregion
#region SetViewport
public void SetViewport ( Viewport viewport )
{
SetViewport ( viewport . X , viewport . Y , viewport . Width , viewport . Height , viewport . MinDepth , viewport . MaxDepth ) ;
}
#endregion
#region ResizeBuffers
public void ResizeBuffers ( PresentationParameters presentationParameters )
{
if ( swapChain ! = null )
{
2015-09-30 21:31:15 +02:00
DisposeBackBuffer ( ) ;
2012-09-08 10:37:41 +00:00
2012-09-15 13:43:31 +00:00
Format colorFormat = DxFormatConverter . Translate ( presentationParameters . BackBufferFormat ) ;
2012-09-08 10:37:41 +00:00
swapChain . ResizeBuffers ( swapChain . Description . BufferCount , presentationParameters . BackBufferWidth ,
presentationParameters . BackBufferHeight , colorFormat , swapChain . Description . Flags ) ;
2015-09-30 21:31:15 +02:00
CreateRenderView ( presentationParameters ) ;
2012-09-08 10:37:41 +00:00
SetViewport ( 0 , 0 , presentationParameters . BackBufferWidth , presentationParameters . BackBufferHeight , 0f , 1f ) ;
}
WindowHelper . ResizeRenderWindow ( presentationParameters ) ;
}
#endregion
2015-10-14 23:59:27 +02:00
public void DrawIndexedPrimitives ( PrimitiveType primitiveType , int baseVertex , int minVertexIndex , int numVertices , int startIndex , int primitiveCount )
{
if ( primitiveCount < = 0 ) throw new ArgumentOutOfRangeException ( "primitiveCount is less than or equal to zero. When drawing, at least one primitive must be drawn." ) ;
if ( this . currentVertexBufferCount = = 0 ) throw new InvalidOperationException ( "you have to set a valid vertex buffer before drawing." ) ;
int vertexCount = DxFormatConverter . CalculateVertexCount ( primitiveType , primitiveCount ) ;
SetupDraw ( primitiveType ) ;
nativeDevice . DrawIndexed ( vertexCount , startIndex , baseVertex ) ;
}
public void DrawPrimitives ( PrimitiveType primitiveType , int vertexOffset , int primitiveCount )
{
int vertexCount = DxFormatConverter . CalculateVertexCount ( primitiveType , primitiveCount ) ;
SetupDraw ( primitiveType ) ;
nativeDevice . Draw ( vertexCount , vertexOffset ) ;
}
public void DrawInstancedPrimitives ( PrimitiveType primitiveType , int baseVertex , int minVertexIndex , int numVertices ,
int startIndex , int primitiveCount , int instanceCount )
{
int vertexCount = DxFormatConverter . CalculateVertexCount ( primitiveType , primitiveCount ) ;
SetupDraw ( primitiveType ) ;
nativeDevice . DrawIndexedInstanced ( vertexCount , instanceCount , startIndex , baseVertex , 0 ) ;
}
public void DrawUserIndexedPrimitives < T > ( PrimitiveType primitiveType , T [ ] vertexData , int vertexOffset , int numVertices ,
Array indexData , int indexOffset , int primitiveCount , VertexDeclaration vertexDeclaration ,
IndexElementSize indexFormat ) where T : struct , IVertexType
{
int vertexCount = vertexData . Length ;
int indexCount = indexData . Length ;
using ( var vertexBuffer = new DynamicVertexBuffer ( vertexDeclaration . GraphicsDevice , vertexDeclaration , vertexCount , BufferUsage . WriteOnly ) )
using ( var indexBuffer = new DynamicIndexBuffer ( vertexDeclaration . GraphicsDevice , indexFormat , indexCount , BufferUsage . WriteOnly ) )
{
vertexBuffer . SetData ( vertexData ) ;
this . SetVertexBuffers ( new [ ] { new Framework . Graphics . VertexBufferBinding ( vertexBuffer , vertexOffset ) } ) ;
if ( indexData . GetType ( ) = = typeof ( Int16 [ ] ) )
{
indexBuffer . SetData < short > ( ( short [ ] ) indexData ) ;
}
else
{
indexBuffer . SetData < int > ( ( int [ ] ) indexData ) ;
}
this . IndexBuffer = indexBuffer ;
DrawIndexedPrimitives ( primitiveType , 0 , vertexOffset , numVertices , indexOffset , primitiveCount ) ;
}
}
public void DrawUserPrimitives < T > ( PrimitiveType primitiveType , T [ ] vertexData , int vertexOffset , int primitiveCount , VertexDeclaration vertexDeclaration ) where T : struct , IVertexType
{
int vertexCount = vertexData . Length ;
//TODO: use a shared vertexBuffer, instead of creating one on every call.
using ( var vertexBuffer = new DynamicVertexBuffer ( vertexDeclaration . GraphicsDevice , vertexDeclaration , vertexCount , BufferUsage . WriteOnly ) )
{
vertexBuffer . SetData ( vertexData ) ;
this . SetVertexBuffers ( new [ ] { new Framework . Graphics . VertexBufferBinding ( vertexBuffer , vertexOffset ) } ) ;
SetupDraw ( primitiveType ) ;
nativeDevice . Draw ( vertexCount , vertexOffset ) ;
}
}
#region GetBackBufferData
2012-09-08 10:37:41 +00:00
public void GetBackBufferData < T > ( Rectangle ? rect , T [ ] data , int startIndex , int elementCount ) where T : struct
{
2015-09-30 21:31:15 +02:00
this . backBuffer . GetData ( 0 , rect , data , startIndex , elementCount ) ;
2012-09-08 10:37:41 +00:00
}
public void GetBackBufferData < T > ( T [ ] data ) where T : struct
{
2015-09-30 21:31:15 +02:00
this . backBuffer . GetData ( data ) ;
2012-09-08 10:37:41 +00:00
}
public void GetBackBufferData < T > ( T [ ] data , int startIndex , int elementCount ) where T : struct
{
2015-09-30 21:31:15 +02:00
this . backBuffer . GetData ( data , startIndex , elementCount ) ;
2012-09-08 10:37:41 +00:00
}
#endregion
2015-10-14 23:59:27 +02:00
public IndexBuffer IndexBuffer
{
get
{
return this . currentIndexBuffer ;
}
set
{
if ( value = = null )
throw new ArgumentNullException ( "indexBuffer" ) ;
if ( this . currentIndexBuffer ! = value )
{
this . currentIndexBuffer = value ;
DxIndexBuffer nativeIndexBuffer = value . NativeIndexBuffer as DxIndexBuffer ;
if ( nativeIndexBuffer ! = null )
{
nativeDevice . InputAssembler . SetIndexBuffer ( nativeIndexBuffer . NativeBuffer , DxFormatConverter . Translate ( value . IndexElementSize ) , 0 ) ;
}
else
{
throw new Exception ( "couldn't fetch native DirectX10 IndexBuffer" ) ;
}
}
}
}
2012-09-08 10:37:41 +00:00
#if XNAEXT
#region SetConstantBuffer ( TODO )
public void SetConstantBuffer ( int slot , ConstantBuffer constantBuffer )
{
if ( constantBuffer = = null )
throw new ArgumentNullException ( "constantBuffer" ) ;
throw new NotImplementedException ( ) ;
}
#endregion
#endif
2015-10-14 23:59:27 +02:00
public void Dispose ( )
{
this . Dispose ( true ) ;
GC . SuppressFinalize ( this ) ;
}
protected virtual void Dispose ( bool diposeManaged )
{
if ( swapChain ! = null )
{
DisposeBackBuffer ( ) ;
swapChain . Dispose ( ) ;
swapChain = null ;
}
if ( inputLayoutManager ! = null )
{
inputLayoutManager . Dispose ( ) ;
inputLayoutManager = null ;
}
//TODO: dispose everything else
}
2012-09-08 10:37:41 +00:00
}
}