2012-09-15 13:43:31 +00:00
#region Using Statements
2011-11-16 14:27:53 +00:00
using System ;
2012-09-22 11:17:36 +00:00
using System.Collections.Generic ;
2012-08-10 08:38:01 +00:00
using ANX.Framework ;
2011-11-16 14:27:53 +00:00
using ANX.Framework.Graphics ;
2012-09-06 09:58:13 +00:00
using ANX.Framework.NonXNA ;
using SharpDX.D3DCompiler ;
using SharpDX.DXGI ;
2012-09-22 11:17:36 +00:00
using SharpDX.Direct3D10 ;
2012-09-15 13:43:31 +00:00
#endregion
2011-11-16 14:27:53 +00:00
2012-08-09 09:45:04 +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
2011-11-16 14:27:53 +00:00
2012-09-15 13:43:31 +00:00
using Dx10 = SharpDX . Direct3D10 ;
2012-08-09 09:45:04 +00:00
namespace ANX.RenderSystem.Windows.DX10
2011-11-16 14:27:53 +00:00
{
2012-09-15 13:43:31 +00:00
public partial class GraphicsDeviceDX : INativeGraphicsDevice
2011-11-16 14:27:53 +00:00
{
2015-09-30 21:31:15 +02:00
#if DEBUG
static int graphicsDeviceCount = 0 ;
static int swapChainCount = 0 ;
#endif
//Restricted to 8 from DirectX side.
const int MAX_RENDER_TARGETS = 8 ;
2012-09-06 09:58:13 +00:00
#region Private
2012-09-15 13:43:31 +00:00
private Dx10 . Device nativeDevice ;
2015-09-30 21:31:15 +02:00
private Dx10 . RenderTargetView [ ] renderTargetView = new RenderTargetView [ MAX_RENDER_TARGETS ] ;
private Dx10 . DepthStencilView [ ] depthStencilView = new DepthStencilView [ MAX_RENDER_TARGETS ] ;
private RenderTarget2D_DX10 backBuffer ;
2012-09-15 13:43:31 +00:00
internal EffectDX currentEffect ;
2012-09-06 09:58:13 +00:00
#endregion
2012-09-08 10:37:41 +00:00
#region CreateDevice
2012-09-15 13:43:31 +00:00
protected void CreateDevice ( PresentationParameters presentationParameters )
2012-09-08 10:37:41 +00:00
{
var desc = new SwapChainDescription ( )
{
BufferCount = 1 ,
2015-09-30 21:31:15 +02:00
ModeDescription = new ModeDescription (
presentationParameters . BackBufferWidth ,
presentationParameters . BackBufferHeight ,
new Rational ( 60 , 1 ) ,
DxFormatConverter . Translate ( presentationParameters . BackBufferFormat )
) ,
2012-09-08 10:37:41 +00:00
IsWindowed = true ,
OutputHandle = presentationParameters . DeviceWindowHandle ,
SampleDescription = new SampleDescription ( 1 , 0 ) ,
SwapEffect = SwapEffect . Discard ,
2015-09-30 21:31:15 +02:00
Usage = Usage . RenderTargetOutput | Usage . ShaderInput ,
2012-09-08 10:37:41 +00:00
} ;
// http://msdn.microsoft.com/en-us/library/windows/desktop/bb205068(v=vs.85).aspx
2015-09-30 21:31:15 +02:00
#if DEBUG
var flags = Dx10 . DeviceCreationFlags . Debug ;
#else
var flags = Dx10 . DeviceCreationFlags . None ;
#endif
2012-09-08 10:37:41 +00:00
Dx10 . Device . CreateWithSwapChain ( Dx10 . DriverType . Hardware , flags , desc , out nativeDevice , out swapChain ) ;
2015-09-30 21:31:15 +02:00
#if DEBUG
nativeDevice . DebugName = "GraphicsDevice_" + graphicsDeviceCount + + ;
swapChain . DebugName = "SwapChain_" + swapChainCount + + ;
#endif
2012-09-08 10:37:41 +00:00
}
#endregion
2011-11-16 14:27:53 +00:00
2012-09-08 10:37:41 +00:00
#region CreateRenderView
2015-09-30 21:31:15 +02:00
protected void CreateRenderView ( PresentationParameters presentationParameters )
2012-09-08 10:37:41 +00:00
{
2015-09-30 21:31:15 +02:00
backBuffer = new RenderTarget2D_DX10 ( this , Dx10 . Texture2D . FromSwapChain < Dx10 . Texture2D > ( swapChain , 0 ) , presentationParameters . DepthStencilFormat ) ;
this . SetRenderTargets ( ) ;
2012-09-08 10:37:41 +00:00
}
2012-09-06 09:58:13 +00:00
#endregion
2011-11-29 13:05:37 +00:00
2015-09-30 21:31:15 +02:00
#region Clear
public void Clear ( ClearOptions options , Vector4 color , float depth , int stencil )
2011-12-09 08:46:25 +00:00
{
2015-09-30 21:31:15 +02:00
if ( ( options & ClearOptions . Target ) = = ClearOptions . Target )
2012-09-29 09:42:27 +00:00
{
2015-09-30 21:31:15 +02:00
// Clear a RenderTarget (or BackBuffer)
var clearColor = new SharpDX . Color4 ( color . X , color . Y , color . Z , color . W ) ;
2011-11-16 14:27:53 +00:00
2015-09-30 21:31:15 +02:00
for ( int i = 0 ; i < MAX_RENDER_TARGETS ; i + + )
2012-08-10 08:38:01 +00:00
{
if ( this . renderTargetView [ i ] = = null )
break ;
2015-09-30 21:31:15 +02:00
nativeDevice . ClearRenderTargetView ( this . renderTargetView [ i ] , clearColor ) ;
2012-08-10 08:38:01 +00:00
}
}
2011-11-16 14:27:53 +00:00
2015-09-30 21:31:15 +02:00
Dx10 . DepthStencilClearFlags clearFlags ;
if ( ( options | ClearOptions . Stencil | ClearOptions . DepthBuffer ) = = options )
2011-11-16 14:27:53 +00:00
{
2015-09-30 21:31:15 +02:00
clearFlags = Dx10 . DepthStencilClearFlags . Depth | Dx10 . DepthStencilClearFlags . Stencil ;
}
else if ( ( options | ClearOptions . Stencil ) = = options )
{
clearFlags = Dx10 . DepthStencilClearFlags . Stencil ;
}
else
{
clearFlags = Dx10 . DepthStencilClearFlags . Depth ;
2011-11-16 14:27:53 +00:00
}
2015-09-30 21:31:15 +02:00
for ( int i = 0 ; i < MAX_RENDER_TARGETS ; i + + )
2011-11-16 14:27:53 +00:00
{
2015-09-30 21:31:15 +02:00
if ( this . depthStencilView [ i ] = = null )
break ;
nativeDevice . ClearDepthStencilView ( this . depthStencilView [ i ] , clearFlags , depth , ( byte ) stencil ) ;
2011-11-16 14:27:53 +00:00
}
}
#endregion
2011-11-30 16:03:51 +00:00
#region Present
public void Present ( )
2011-11-16 14:27:53 +00:00
{
2012-09-06 09:58:13 +00:00
swapChain . Present ( VSync ? 1 : 0 , PresentFlags . None ) ;
2011-11-16 14:27:53 +00:00
}
2012-09-06 09:58:13 +00:00
#endregion
2011-11-16 14:27:53 +00:00
2012-09-06 09:58:13 +00:00
#region DrawIndexedPrimitives
2012-09-21 12:07:20 +00:00
public void DrawIndexedPrimitives ( PrimitiveType primitiveType , int baseVertex , int minVertexIndex , int numVertices , int startIndex , int primitiveCount , IndexBuffer indexBuffer )
2012-09-06 09:58:13 +00:00
{
2012-09-27 10:08:00 +00:00
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 . currentVertexBuffer = = null | | this . currentVertexBufferCount < = 0 ) throw new InvalidOperationException ( "you have to set a valid vertex buffer before drawing." ) ;
2012-09-06 09:58:13 +00:00
Dx10 . EffectTechnique technique = SetupEffectForDraw ( ) ;
2012-09-15 13:43:31 +00:00
int vertexCount = DxFormatConverter . CalculateVertexCount ( primitiveType , primitiveCount ) ;
2011-11-30 16:03:51 +00:00
2015-09-30 21:31:15 +02:00
nativeDevice . InputAssembler . PrimitiveTopology = DxFormatConverter . Translate ( primitiveType ) ;
2011-11-30 16:03:51 +00:00
2012-09-21 12:07:20 +00:00
if ( indexBuffer ! = null )
{
SetIndexBuffer ( indexBuffer ) ;
}
2011-11-30 16:03:51 +00:00
for ( int i = 0 ; i < technique . Description . PassCount ; + + i )
2012-09-06 09:58:13 +00:00
{
technique . GetPassByIndex ( i ) . Apply ( ) ;
2012-09-08 10:37:41 +00:00
nativeDevice . DrawIndexed ( vertexCount , startIndex , baseVertex ) ;
2012-09-08 09:07:23 +00:00
}
2012-09-20 11:53:50 +00:00
nativeDevice . InputAssembler . InputLayout . Dispose ( ) ;
nativeDevice . InputAssembler . InputLayout = null ;
2012-09-06 09:58:13 +00:00
}
#endregion
2011-11-30 16:03:51 +00:00
2012-09-06 09:58:13 +00:00
#region DrawPrimitives
public void DrawPrimitives ( PrimitiveType primitiveType , int vertexOffset , int primitiveCount )
{
Dx10 . EffectTechnique technique = SetupEffectForDraw ( ) ;
2012-09-20 11:53:50 +00:00
int vertexCount = DxFormatConverter . CalculateVertexCount ( primitiveType , primitiveCount ) ;
2011-11-30 16:03:51 +00:00
2012-09-15 13:43:31 +00:00
nativeDevice . InputAssembler . PrimitiveTopology = DxFormatConverter . Translate ( primitiveType ) ;
2011-11-30 16:03:51 +00:00
for ( int i = 0 ; i < technique . Description . PassCount ; + + i )
2012-09-06 09:58:13 +00:00
{
technique . GetPassByIndex ( i ) . Apply ( ) ;
2012-09-20 11:53:50 +00:00
nativeDevice . Draw ( vertexCount , vertexOffset ) ;
2011-11-30 16:03:51 +00:00
}
2012-09-08 09:07:23 +00:00
2012-09-08 10:37:41 +00:00
nativeDevice . InputAssembler . InputLayout . Dispose ( ) ;
nativeDevice . InputAssembler . InputLayout = null ;
2011-11-30 16:03:51 +00:00
}
2012-09-06 09:58:13 +00:00
#endregion
2011-11-30 16:03:51 +00:00
2012-09-06 09:58:13 +00:00
public void DrawInstancedPrimitives ( PrimitiveType primitiveType , int baseVertex , int minVertexIndex , int numVertices ,
2012-09-22 11:17:36 +00:00
int startIndex , int primitiveCount , int instanceCount , IndexBuffer indexBuffer )
2011-11-30 16:03:51 +00:00
{
2012-09-22 11:17:36 +00:00
Dx10 . EffectTechnique technique = SetupEffectForDraw ( ) ;
int vertexCount = DxFormatConverter . CalculateVertexCount ( primitiveType , primitiveCount ) ;
nativeDevice . InputAssembler . PrimitiveTopology = DxFormatConverter . Translate ( primitiveType ) ;
if ( indexBuffer ! = null )
{
SetIndexBuffer ( indexBuffer ) ;
}
for ( int i = 0 ; i < technique . Description . PassCount ; + + i )
{
technique . GetPassByIndex ( i ) . Apply ( ) ;
nativeDevice . DrawIndexedInstanced ( vertexCount , instanceCount , startIndex , baseVertex , 0 ) ;
}
nativeDevice . InputAssembler . InputLayout . Dispose ( ) ;
nativeDevice . InputAssembler . InputLayout = null ;
2011-11-30 16:03:51 +00:00
}
2012-09-06 09:58:13 +00:00
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
2011-11-30 16:03:51 +00:00
{
2012-01-17 05:14:18 +00:00
int vertexCount = vertexData . Length ;
int indexCount = indexData . Length ;
2015-10-04 21:30:00 +02:00
using ( var vertexBuffer = new DynamicVertexBuffer ( vertexDeclaration . GraphicsDevice , vertexDeclaration , vertexCount , BufferUsage . WriteOnly ) )
using ( var indexBuffer = new DynamicIndexBuffer ( vertexDeclaration . GraphicsDevice , indexFormat , indexCount , BufferUsage . WriteOnly ) )
2012-09-20 10:23:03 +00:00
{
2015-10-04 21:30:00 +02:00
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 ) ;
}
2012-09-20 10:23:03 +00:00
2015-10-04 21:30:00 +02:00
DrawIndexedPrimitives ( primitiveType , 0 , vertexOffset , numVertices , indexOffset , primitiveCount , indexBuffer ) ;
}
2011-11-30 16:03:51 +00:00
}
2012-09-20 11:53:50 +00:00
public void DrawUserPrimitives < T > ( PrimitiveType primitiveType , T [ ] vertexData , int vertexOffset , int primitiveCount , VertexDeclaration vertexDeclaration ) where T : struct , IVertexType
2011-11-30 16:03:51 +00:00
{
2012-01-09 18:09:10 +00:00
int vertexCount = vertexData . Length ;
2015-10-04 21:30:00 +02:00
//TODO: use a shared vertexBuffer, instead of creating one on every call.
using ( DxVertexBuffer vb10 = new DxVertexBuffer ( nativeDevice , vertexDeclaration , vertexCount , BufferUsage . WriteOnly , dynamic : true ) )
{
vb10 . SetData < T > ( vertexData ) ;
2012-01-09 18:09:10 +00:00
2015-10-04 21:30:00 +02:00
Dx10 . VertexBufferBinding nativeVertexBufferBindings = new Dx10 . VertexBufferBinding ( vb10 . NativeBuffer , vertexDeclaration . VertexStride , 0 ) ;
2012-01-09 18:09:10 +00:00
2015-10-04 21:30:00 +02:00
nativeDevice . InputAssembler . SetVertexBuffers ( 0 , nativeVertexBufferBindings ) ;
2012-01-09 18:09:10 +00:00
2015-10-04 21:30:00 +02:00
//TODO: check for currentEffect null and throw exception
// TODO: check for null's and throw exceptions
// TODO: get the correct pass index!
var technique = currentEffect . GetCurrentTechnique ( ) . NativeTechnique ;
var pass = technique . GetPassByIndex ( 0 ) ;
var layout = CreateInputLayout ( nativeDevice , pass . Description . Signature , vertexDeclaration ) ;
2012-08-10 08:38:01 +00:00
2015-10-04 21:30:00 +02:00
nativeDevice . InputAssembler . InputLayout = layout ;
// Prepare All the stages
nativeDevice . InputAssembler . PrimitiveTopology = DxFormatConverter . Translate ( primitiveType ) ;
2012-08-10 08:38:01 +00:00
2015-10-04 21:30:00 +02:00
for ( int i = 0 ; i < technique . Description . PassCount ; + + i )
{
technique . GetPassByIndex ( i ) . Apply ( ) ;
nativeDevice . Draw ( vertexCount , vertexOffset ) ;
}
2012-09-08 09:07:23 +00:00
2015-10-04 21:30:00 +02:00
nativeDevice . InputAssembler . InputLayout . Dispose ( ) ;
nativeDevice . InputAssembler . InputLayout = null ;
}
2011-11-30 16:03:51 +00:00
}
2012-09-06 09:58:13 +00:00
private Dx10 . EffectTechnique SetupEffectForDraw ( )
2011-11-16 14:27:53 +00:00
{
2012-09-06 09:58:13 +00:00
//TODO: check for currentEffect null and throw exception
2012-09-05 19:50:10 +00:00
// TODO: check for null's and throw exceptions
// TODO: get the correct pass index!
2012-09-06 09:58:13 +00:00
var technique = currentEffect . GetCurrentTechnique ( ) . NativeTechnique ;
var pass = technique . GetPassByIndex ( 0 ) ;
SetupInputLayout ( pass . Description . Signature ) ;
return technique ;
2011-11-16 14:27:53 +00:00
}
2012-09-06 09:58:13 +00:00
#region SetupInputLayout
2012-09-22 11:17:36 +00:00
private InputLayout SetupInputLayout ( ShaderBytecode passSignature )
2011-11-16 14:27:53 +00:00
{
2012-09-22 11:17:36 +00:00
// get the VertexDeclaration from current VertexBuffer to create input layout for the input assembler
var layout = CreateInputLayout ( nativeDevice , passSignature , currentVertexBuffer ) ;
2011-11-16 14:27:53 +00:00
2012-09-22 11:17:36 +00:00
nativeDevice . InputAssembler . InputLayout = layout ;
return layout ;
2011-11-16 14:27:53 +00:00
}
2012-09-06 09:58:13 +00:00
#endregion
#region SetIndexBuffer
2011-11-16 14:27:53 +00:00
public void SetIndexBuffer ( IndexBuffer indexBuffer )
{
if ( indexBuffer = = null )
throw new ArgumentNullException ( "indexBuffer" ) ;
this . currentIndexBuffer = indexBuffer ;
2012-09-15 13:43:31 +00:00
DxIndexBuffer nativeIndexBuffer = indexBuffer . NativeIndexBuffer as DxIndexBuffer ;
2011-11-16 14:27:53 +00:00
if ( nativeIndexBuffer ! = null )
{
2012-09-21 12:07:20 +00:00
nativeDevice . InputAssembler . SetIndexBuffer ( nativeIndexBuffer . NativeBuffer , DxFormatConverter . Translate ( indexBuffer . IndexElementSize ) , 0 ) ;
2011-11-16 14:27:53 +00:00
}
else
{
throw new Exception ( "couldn't fetch native DirectX10 IndexBuffer" ) ;
}
}
2012-09-06 09:58:13 +00:00
#endregion
2011-11-16 14:27:53 +00:00
2012-09-06 09:58:13 +00:00
#region SetVertexBuffers
2012-09-22 11:17:36 +00:00
public void SetVertexBuffers ( ANX . Framework . Graphics . VertexBufferBinding [ ] vertexBuffers )
2011-11-16 14:27:53 +00:00
{
if ( vertexBuffers = = null )
throw new ArgumentNullException ( "vertexBuffers" ) ;
2012-09-22 11:17:36 +00:00
this . currentVertexBufferCount = vertexBuffers . Length ;
2011-11-16 14:27:53 +00:00
2012-09-22 11:17:36 +00:00
if ( this . currentVertexBuffer = = null | | this . currentVertexBuffer . Length < currentVertexBufferCount )
{
this . currentVertexBuffer = new ANX . Framework . Graphics . VertexBufferBinding [ currentVertexBufferCount ] ;
}
for ( int i = 0 ; i < this . currentVertexBufferCount ; i + + )
{
this . currentVertexBuffer [ i ] = vertexBuffers [ i ] ;
}
Dx10 . VertexBufferBinding [ ] nativeVertexBufferBindings = new Dx10 . VertexBufferBinding [ vertexBuffers . Length ] ;
2011-11-16 14:27:53 +00:00
for ( int i = 0 ; i < vertexBuffers . Length ; i + + )
{
ANX . Framework . Graphics . VertexBufferBinding anxVertexBufferBinding = vertexBuffers [ i ] ;
2012-09-15 13:43:31 +00:00
var nativeVertexBuffer = anxVertexBufferBinding . VertexBuffer . NativeVertexBuffer as DxVertexBuffer ;
2011-11-16 14:27:53 +00:00
if ( nativeVertexBuffer ! = null )
{
2012-09-20 07:17:14 +00:00
int vertexStride = anxVertexBufferBinding . VertexBuffer . VertexDeclaration . VertexStride ;
nativeVertexBufferBindings [ i ] = new Dx10 . VertexBufferBinding ( nativeVertexBuffer . NativeBuffer , vertexStride , anxVertexBufferBinding . VertexOffset * vertexStride ) ;
2011-11-16 14:27:53 +00:00
}
else
{
throw new Exception ( "couldn't fetch native DirectX10 VertexBuffer" ) ;
}
}
2012-09-08 10:37:41 +00:00
nativeDevice . InputAssembler . SetVertexBuffers ( 0 , nativeVertexBufferBindings ) ;
2012-09-06 09:58:13 +00:00
}
#endregion
2011-11-16 14:27:53 +00:00
2012-09-06 09:58:13 +00:00
#region SetViewport
2012-09-15 13:43:31 +00:00
protected void SetViewport ( int x , int y , int width , int height , float minDepth , float maxDepth )
2012-09-08 10:37:41 +00:00
{
2013-07-23 10:56:42 +00:00
nativeDevice . Rasterizer . SetViewports ( new SharpDX . Viewport ( x , y , width , height , minDepth , maxDepth ) ) ;
2012-09-08 10:37:41 +00:00
}
2011-11-16 14:27:53 +00:00
2013-07-23 10:56:42 +00:00
protected void SetViewport ( params SharpDX . Viewport [ ] viewports )
2012-09-29 09:42:27 +00:00
{
nativeDevice . Rasterizer . SetViewports ( viewports ) ;
}
#endregion
#region CreateInputLayout
/// <summary>
2012-09-05 19:50:10 +00:00
/// This method creates a InputLayout which is needed by DirectX 10 for rendering primitives.
2012-09-29 09:42:27 +00:00
/// The VertexDeclaration of ANX/XNA needs to be mapped to the DirectX 10 types.
2011-11-16 14:27:53 +00:00
/// </summary>
2012-09-29 09:42:27 +00:00
private Dx10 . InputLayout CreateInputLayout ( Dx10 . Device device , ShaderBytecode passSignature , params VertexDeclaration [ ] vertexDeclaration )
2011-11-16 14:27:53 +00:00
{
2012-09-27 10:08:00 +00:00
if ( device = = null ) throw new ArgumentNullException ( "device" ) ;
if ( passSignature = = null ) throw new ArgumentNullException ( "passSignature" ) ;
if ( vertexDeclaration = = null ) throw new ArgumentNullException ( "vertexDeclaration" ) ;
2012-09-22 11:17:36 +00:00
//TODO: try to get rid of the list
List < InputElement > inputElements = new List < InputElement > ( ) ;
foreach ( VertexDeclaration decl in vertexDeclaration )
{
foreach ( VertexElement vertexElement in decl . GetVertexElements ( ) )
{
inputElements . Add ( CreateInputElementFromVertexElement ( vertexElement , 0 ) ) ;
}
}
2011-11-16 14:27:53 +00:00
2012-09-22 11:17:36 +00:00
return new Dx10 . InputLayout ( device , passSignature , inputElements . ToArray ( ) ) ;
2012-09-06 09:58:13 +00:00
}
2012-09-22 11:17:36 +00:00
private Dx10 . InputLayout CreateInputLayout ( Dx10 . Device device , ShaderBytecode passSignature , params ANX . Framework . Graphics . VertexBufferBinding [ ] vertexBufferBindings )
{
2012-09-27 10:08:00 +00:00
if ( device = = null ) throw new ArgumentNullException ( "device" ) ;
if ( passSignature = = null ) throw new ArgumentNullException ( "passSignature" ) ;
if ( vertexBufferBindings = = null ) throw new ArgumentNullException ( "vertexBufferBindings" ) ;
2012-09-22 11:17:36 +00:00
//TODO: try to get rid of the list
List < InputElement > inputElements = new List < InputElement > ( ) ;
int slot = 0 ;
foreach ( ANX . Framework . Graphics . VertexBufferBinding binding in vertexBufferBindings )
{
foreach ( VertexElement vertexElement in binding . VertexBuffer . VertexDeclaration . GetVertexElements ( ) )
{
inputElements . Add ( CreateInputElementFromVertexElement ( vertexElement , binding . InstanceFrequency , slot ) ) ;
}
slot + + ;
}
// Layout from VertexShader input signature
return new InputLayout ( device , passSignature , inputElements . ToArray ( ) ) ;
}
2012-09-06 09:58:13 +00:00
#endregion
2011-11-16 14:27:53 +00:00
2012-09-06 09:58:13 +00:00
#region CreateInputElementFromVertexElement
2012-09-22 11:17:36 +00:00
private InputElement CreateInputElementFromVertexElement ( VertexElement vertexElement , int slot )
2011-11-16 14:27:53 +00:00
{
2012-09-22 11:17:36 +00:00
return CreateInputElementFromVertexElement ( vertexElement , 0 , slot ) ;
}
private InputElement CreateInputElementFromVertexElement ( VertexElement vertexElement , int instanceFrequency , int slot )
{
string elementName = DxFormatConverter . Translate ( ref vertexElement ) ;
Format elementFormat = DxFormatConverter . ConvertVertexElementFormat ( vertexElement . VertexElementFormat ) ;
return new InputElement ( elementName , vertexElement . UsageIndex , elementFormat , vertexElement . Offset , slot , instanceFrequency = = 0 ? InputClassification . PerVertexData : InputClassification . PerInstanceData , instanceFrequency ) ;
}
#endregion
2011-11-16 14:27:53 +00:00
2012-09-05 19:50:10 +00:00
#region SetRenderTargets
public void SetRenderTargets ( params RenderTargetBinding [ ] renderTargets )
2011-11-16 14:27:53 +00:00
{
2015-09-30 21:31:15 +02:00
if ( renderTargets = = null | | renderTargets . Length = = 0 )
2011-11-28 16:06:52 +00:00
{
2015-09-30 21:31:15 +02:00
this . renderTargetView [ 0 ] = this . backBuffer . RenderTargetView ;
this . depthStencilView [ 0 ] = this . backBuffer . DepthStencilView ;
for ( int i = 1 ; i < MAX_RENDER_TARGETS ; i + + )
2011-11-28 16:06:52 +00:00
{
2015-09-30 21:31:15 +02:00
this . renderTargetView [ i ] = null ;
this . depthStencilView [ i ] = null ;
2011-11-28 16:06:52 +00:00
}
2015-09-30 21:31:15 +02:00
//To correctly unset renderTargets, the amount of given rendertargetViews must be max(#previousRenderTargets, #newRenderTargets),
//otherwise the old ones at the slots stay bound. For us it means, we try to unbind every possible previous slot.
nativeDevice . OutputMerger . SetRenderTargets ( MAX_RENDER_TARGETS , this . renderTargetView , this . backBuffer . DepthStencilView ) ;
nativeDevice . Rasterizer . SetViewports ( new SharpDX . Viewport ( 0 , 0 , this . backBuffer . Width , this . backBuffer . Height ) ) ;
2011-11-28 16:06:52 +00:00
}
else
{
2012-08-10 08:38:01 +00:00
int renderTargetCount = renderTargets . Length ;
2015-09-30 21:31:15 +02:00
if ( renderTargetCount > MAX_RENDER_TARGETS )
throw new NotSupportedException ( string . Format ( "{0} render targets are not supported. The maximum is {1}." , renderTargetCount , MAX_RENDER_TARGETS ) ) ;
2013-07-23 10:56:42 +00:00
SharpDX . Viewport [ ] rtViewports = new SharpDX . Viewport [ renderTargetCount ] ;
2012-09-29 09:42:27 +00:00
2015-09-30 21:31:15 +02:00
var firstRenderTarget = renderTargets [ 0 ] . RenderTarget as RenderTarget2D ;
for ( int i = 1 ; i < renderTargetCount ; i + + )
2011-11-28 16:06:52 +00:00
{
2015-09-30 21:31:15 +02:00
var renderTarget = renderTargets [ i ] . RenderTarget as RenderTarget2D ;
if ( renderTarget . Width ! = firstRenderTarget . Width | | renderTarget . Height ! = firstRenderTarget . Height | | renderTarget . MultiSampleCount ! = firstRenderTarget . MultiSampleCount )
throw new ArgumentException ( "The render targets don't match" ) ;
2011-11-28 16:06:52 +00:00
}
2012-08-10 08:38:01 +00:00
for ( int i = 0 ; i < renderTargetCount ; i + + )
2011-11-28 16:06:52 +00:00
{
2012-08-10 08:38:01 +00:00
RenderTarget2D renderTarget = renderTargets [ i ] . RenderTarget as RenderTarget2D ;
2015-09-30 21:31:15 +02:00
RenderTarget2D_DX10 nativeRenderTarget = renderTarget . NativeRenderTarget as RenderTarget2D_DX10 ;
2012-09-29 09:42:27 +00:00
2015-09-30 21:31:15 +02:00
renderTargetView [ i ] = nativeRenderTarget . RenderTargetView ;
depthStencilView [ i ] = nativeRenderTarget . DepthStencilView ;
rtViewports [ i ] = new SharpDX . Viewport ( 0 , 0 , renderTarget . Width , renderTarget . Height ) ;
2011-11-28 16:06:52 +00:00
}
2012-08-10 08:38:01 +00:00
2015-09-30 21:31:15 +02:00
for ( int i = renderTargetCount ; i < MAX_RENDER_TARGETS ; i + + )
2012-09-29 09:42:27 +00:00
{
2015-09-30 21:31:15 +02:00
this . renderTargetView [ i ] = null ;
this . depthStencilView [ i ] = null ;
2012-09-29 09:42:27 +00:00
}
2015-09-30 21:31:15 +02:00
nativeDevice . OutputMerger . SetRenderTargets ( MAX_RENDER_TARGETS , renderTargetView , this . depthStencilView [ 0 ] ) ;
nativeDevice . Rasterizer . SetViewports ( rtViewports ) ;
2011-11-28 16:06:52 +00:00
}
2011-11-16 14:27:53 +00:00
}
2012-09-05 19:50:10 +00:00
#endregion
2011-11-16 14:27:53 +00:00
2015-09-30 21:31:15 +02:00
protected void DisposeBackBuffer ( )
2012-09-08 10:37:41 +00:00
{
2015-09-30 21:31:15 +02:00
if ( backBuffer ! = null )
{
2015-10-04 21:30:00 +02:00
for ( int i = 0 ; i < MAX_RENDER_TARGETS ; i + + )
{
this . renderTargetView [ i ] = null ;
this . depthStencilView [ i ] = null ;
}
nativeDevice . OutputMerger . SetRenderTargets ( MAX_RENDER_TARGETS , this . renderTargetView , null ) ;
2015-09-30 21:31:15 +02:00
backBuffer . Dispose ( ) ;
backBuffer = null ;
}
2012-09-08 10:37:41 +00:00
}
2011-11-30 13:58:55 +00:00
2012-09-05 19:50:10 +00:00
#region Dispose
public void Dispose ( )
2011-11-22 14:51:30 +00:00
{
2011-11-28 16:06:52 +00:00
if ( swapChain ! = null )
{
2015-09-30 21:31:15 +02:00
DisposeBackBuffer ( ) ;
2011-11-28 16:06:52 +00:00
swapChain . Dispose ( ) ;
swapChain = null ;
}
//TODO: dispose everything else
2012-09-05 19:50:10 +00:00
}
#endregion
2012-09-15 13:43:31 +00:00
internal Dx10 . Device NativeDevice
{
get
{
return this . nativeDevice ;
}
}
2012-10-13 14:55:06 +00:00
public Rectangle ScissorRectangle
{
get { throw new NotImplementedException ( ) ; }
set { throw new NotImplementedException ( ) ; }
}
}
2011-11-16 14:27:53 +00:00
}