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-12-14 11:49:04 +00:00
using ANX.Framework.Graphics ;
2012-09-07 09:48:45 +00:00
using ANX.Framework.NonXNA ;
using SharpDX.D3DCompiler ;
using SharpDX.Direct3D ;
using SharpDX.Direct3D11 ;
using SharpDX.DXGI ;
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 Device = SharpDX . Direct3D11 . Device ;
2011-11-16 14:27:53 +00:00
namespace ANX.RenderSystem.Windows.DX11
{
2012-09-15 13:43:31 +00:00
public partial class GraphicsDeviceDX : INativeGraphicsDevice
2011-12-14 11:49:04 +00:00
{
2012-09-07 09:48:45 +00:00
#region Private
2012-09-15 13:43:31 +00:00
private DeviceContext nativeDevice ;
2011-11-16 14:27:53 +00:00
private RenderTargetView renderView ;
2012-08-10 08:38:01 +00:00
private RenderTargetView [ ] renderTargetView = new RenderTargetView [ 1 ] ;
2011-12-14 11:49:04 +00:00
private DepthStencilView depthStencilView ;
private SharpDX . Direct3D11 . Texture2D depthStencilBuffer ;
2011-11-16 14:27:53 +00:00
private SharpDX . Direct3D11 . Texture2D backBuffer ;
2012-09-15 13:43:31 +00:00
internal EffectDX currentEffect ;
2011-11-16 14:27:53 +00:00
private SharpDX . Direct3D11 . Viewport currentViewport ;
2012-09-07 09:48:45 +00:00
#endregion
2011-12-14 11:49:04 +00:00
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 ,
2012-09-07 09:48:45 +00:00
ModeDescription = new ModeDescription ( presentationParameters . BackBufferWidth ,
presentationParameters . BackBufferHeight , new Rational ( 60 , 1 ) ,
2012-09-15 13:43:31 +00:00
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 ,
Usage = Usage . RenderTargetOutput
} ;
// Create Device and SwapChain
Device dxDevice ;
// http://msdn.microsoft.com/en-us/library/windows/desktop/bb205068(v=vs.85).aspx
var flags = IsDebugMode ? DeviceCreationFlags . Debug : DeviceCreationFlags . None ;
Device . CreateWithSwapChain ( DriverType . Hardware , flags , desc , out dxDevice , out swapChain ) ;
nativeDevice = dxDevice . ImmediateContext ;
}
#endregion
2011-11-16 14:27:53 +00:00
2012-09-08 10:37:41 +00:00
#region CreateRenderView
2012-09-15 13:43:31 +00:00
protected void CreateRenderView ( )
2012-09-08 10:37:41 +00:00
{
backBuffer = SharpDX . Direct3D11 . Texture2D . FromSwapChain < SharpDX . Direct3D11 . Texture2D > ( swapChain , 0 ) ;
renderView = new RenderTargetView ( nativeDevice . Device , backBuffer ) ;
2012-09-28 13:05:38 +00:00
nativeDevice . OutputMerger . SetTargets ( this . depthStencilView , this . renderView ) ;
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 CreateDepthStencilBuffer
2012-09-15 13:43:31 +00:00
protected void CreateDepthStencilBuffer ( Format depthFormat )
2011-11-16 14:27:53 +00:00
{
2011-12-14 11:49:04 +00:00
if ( this . depthStencilBuffer ! = null & &
this . depthStencilBuffer . Description . Format = = depthFormat & &
this . depthStencilBuffer . Description . Width = = this . backBuffer . Description . Width & &
this . depthStencilBuffer . Description . Height = = this . backBuffer . Description . Height )
{
// a DepthStencilBuffer with the right format and the right size already exists -> nothing to do
return ;
}
2011-11-16 14:27:53 +00:00
2011-12-14 11:49:04 +00:00
if ( this . depthStencilView ! = null )
2011-11-16 14:27:53 +00:00
{
2011-12-14 11:49:04 +00:00
this . depthStencilView . Dispose ( ) ;
this . depthStencilView = null ;
2011-11-16 14:27:53 +00:00
}
2011-12-14 11:49:04 +00:00
if ( this . depthStencilBuffer ! = null )
{
this . depthStencilBuffer . Dispose ( ) ;
this . depthStencilBuffer = null ;
}
2011-11-16 14:27:53 +00:00
2011-12-14 11:49:04 +00:00
if ( depthFormat = = Format . Unknown )
{
// no DepthStencilBuffer to create... Old one was disposed already...
return ;
}
2011-11-16 14:27:53 +00:00
2011-12-14 11:49:04 +00:00
DepthStencilViewDescription depthStencilViewDesc = new DepthStencilViewDescription ( )
{
Format = depthFormat ,
} ;
2011-11-16 14:27:53 +00:00
2011-12-14 11:49:04 +00:00
Texture2DDescription depthStencilTextureDesc = new Texture2DDescription ( )
{
Width = this . backBuffer . Description . Width ,
Height = this . backBuffer . Description . Height ,
MipLevels = 1 ,
ArraySize = 1 ,
Format = depthFormat ,
SampleDescription = new SampleDescription ( 1 , 0 ) ,
Usage = ResourceUsage . Default ,
BindFlags = BindFlags . DepthStencil ,
CpuAccessFlags = CpuAccessFlags . None ,
OptionFlags = ResourceOptionFlags . None
} ;
2012-09-08 10:37:41 +00:00
this . depthStencilBuffer = new SharpDX . Direct3D11 . Texture2D ( nativeDevice . Device , depthStencilTextureDesc ) ;
2011-11-16 14:27:53 +00:00
2012-09-08 10:37:41 +00:00
this . depthStencilView = new DepthStencilView ( nativeDevice . Device , this . depthStencilBuffer ) ;
2012-09-28 13:05:38 +00:00
nativeDevice . OutputMerger . SetTargets ( this . depthStencilView , this . renderView ) ;
2011-12-14 11:49:04 +00:00
Clear ( ClearOptions . DepthBuffer | ClearOptions . Stencil , ANX . Framework . Vector4 . Zero , 1.0f , 0 ) ; //TODO: this workaround is working but maybe not the best solution to issue #472
2011-11-16 14:27:53 +00:00
}
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 Clear
public void Clear ( ref Color color )
2011-11-16 14:27:53 +00:00
{
2012-09-08 10:37:41 +00:00
UpdateClearColorIfNeeded ( ref color ) ;
2011-12-14 11:49:04 +00:00
2012-08-10 08:38:01 +00:00
if ( this . renderTargetView [ 0 ] = = null )
2012-09-08 10:37:41 +00:00
nativeDevice . ClearRenderTargetView ( this . renderView , this . clearColor ) ;
2012-08-10 08:38:01 +00:00
else
{
for ( int i = 0 ; i < this . renderTargetView . Length ; i + + )
{
if ( this . renderTargetView [ i ] = = null )
{
break ;
}
2012-09-08 10:37:41 +00:00
nativeDevice . ClearRenderTargetView ( this . renderTargetView [ i ] , this . clearColor ) ;
2012-08-10 08:38:01 +00:00
}
}
2011-12-14 11:49:04 +00:00
}
public void Clear ( ClearOptions options , ANX . Framework . Vector4 color , float depth , int stencil )
{
if ( ( options & ClearOptions . Target ) = = ClearOptions . Target )
2011-11-16 14:27:53 +00:00
{
2011-12-14 11:49:04 +00:00
// Clear a RenderTarget (or BackBuffer)
this . clearColor . Red = color . X ;
this . clearColor . Green = color . Y ;
this . clearColor . Blue = color . Z ;
this . clearColor . Alpha = color . W ;
this . lastClearColor = 0 ;
2012-08-10 08:38:01 +00:00
if ( this . renderTargetView [ 0 ] = = null )
2012-09-08 10:37:41 +00:00
nativeDevice . ClearRenderTargetView ( this . renderView , this . clearColor ) ;
2012-08-10 08:38:01 +00:00
else
{
for ( int i = 0 ; i < this . renderTargetView . Length ; i + + )
{
if ( this . renderTargetView [ i ] = = null )
break ;
2012-09-08 10:37:41 +00:00
nativeDevice . ClearRenderTargetView ( this . renderTargetView [ i ] , this . clearColor ) ;
2012-08-10 08:38:01 +00:00
}
}
2011-11-16 14:27:53 +00:00
}
2011-12-14 11:49:04 +00:00
if ( this . depthStencilView ! = null )
2011-11-16 14:27:53 +00:00
{
2011-12-14 11:49:04 +00:00
if ( ( options | ClearOptions . Stencil | ClearOptions . DepthBuffer ) = = options )
{
// Clear the stencil buffer
2012-09-08 10:37:41 +00:00
nativeDevice . ClearDepthStencilView ( this . depthStencilView , DepthStencilClearFlags . Depth |
DepthStencilClearFlags . Stencil , depth , ( byte ) stencil ) ;
2011-12-14 11:49:04 +00:00
}
else if ( ( options | ClearOptions . Stencil ) = = options )
{
2012-09-08 10:37:41 +00:00
nativeDevice . ClearDepthStencilView ( this . depthStencilView , DepthStencilClearFlags . Stencil , depth ,
( byte ) stencil ) ;
2011-12-14 11:49:04 +00:00
}
else
{
2012-09-08 10:37:41 +00:00
nativeDevice . ClearDepthStencilView ( this . depthStencilView , DepthStencilClearFlags . Depth , depth , ( byte ) stencil ) ;
2011-12-14 11:49:04 +00:00
}
2011-11-16 14:27:53 +00:00
}
}
2011-12-14 11:49:04 +00:00
#endregion
#region Present
public void Present ( )
{
2012-09-07 09:48:45 +00:00
swapChain . Present ( VSync ? 1 : 0 , PresentFlags . None ) ;
2011-12-14 11:49:04 +00:00
}
2012-09-07 09:48:45 +00:00
#endregion
2011-12-14 11:49:04 +00:00
2012-09-08 10:37:41 +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-08 09:07:23 +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-20 11:53:50 +00:00
SharpDX . Direct3D11 . EffectTechnique technique = SetupEffectForDraw ( ) ;
int vertexCount = DxFormatConverter . CalculateVertexCount ( primitiveType , primitiveCount ) ;
2011-11-16 14:27:53 +00:00
2012-09-20 11:53:50 +00:00
nativeDevice . InputAssembler . PrimitiveTopology = DxFormatConverter . Translate ( primitiveType ) ;
nativeDevice . Rasterizer . SetViewports ( currentViewport ) ;
2012-09-28 13:05:38 +00:00
//nativeDevice.OutputMerger.SetTargets(this.depthStencilView, this.renderView);
2011-11-16 14:27:53 +00:00
2012-09-21 12:07:20 +00:00
if ( indexBuffer ! = null )
{
SetIndexBuffer ( indexBuffer ) ;
}
2012-09-20 11:53:50 +00:00
for ( int i = 0 ; i < technique . Description . PassCount ; + + i )
{
technique . GetPassByIndex ( i ) . Apply ( nativeDevice ) ;
nativeDevice . DrawIndexed ( vertexCount , startIndex , baseVertex ) ;
}
2011-11-16 14:27:53 +00:00
2012-09-20 11:53:50 +00:00
nativeDevice . InputAssembler . InputLayout . Dispose ( ) ;
nativeDevice . InputAssembler . InputLayout = null ;
2012-09-08 09:07:23 +00:00
}
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 DrawPrimitives
2012-09-08 09:07:23 +00:00
public void DrawPrimitives ( PrimitiveType primitiveType , int vertexOffset , int primitiveCount )
{
SharpDX . Direct3D11 . EffectPass pass ; SharpDX . Direct3D11 . EffectTechnique technique ; ShaderBytecode passSignature ;
SetupEffectForDraw ( out pass , out technique , out passSignature ) ;
2011-11-16 14:27:53 +00:00
2012-09-08 09:07:23 +00:00
var layout = SetupInputLayout ( passSignature ) ;
// Prepare All the stages
2012-09-15 13:43:31 +00:00
nativeDevice . InputAssembler . PrimitiveTopology = DxFormatConverter . Translate ( primitiveType ) ;
2012-09-08 10:37:41 +00:00
nativeDevice . Rasterizer . SetViewports ( currentViewport ) ;
2011-11-16 14:27:53 +00:00
2012-09-28 13:05:38 +00:00
//nativeDevice.OutputMerger.SetTargets(this.depthStencilView, this.renderView);
2011-12-14 11:49:04 +00:00
2012-09-08 09:07:23 +00:00
for ( int i = 0 ; i < technique . Description . PassCount ; + + i )
{
2012-09-08 10:37:41 +00:00
pass . Apply ( nativeDevice ) ;
nativeDevice . Draw ( primitiveCount , vertexOffset ) ;
2012-09-08 09:07:23 +00:00
}
layout . Dispose ( ) ;
layout = null ;
}
2012-09-08 10:37:41 +00:00
#endregion
2011-11-16 14:27:53 +00:00
2011-12-14 11:49:04 +00:00
#region DrawInstancedPrimitives
2012-09-22 11:17:36 +00:00
public void DrawInstancedPrimitives ( PrimitiveType primitiveType , int baseVertex , int minVertexIndex , int numVertices , int startIndex , int primitiveCount , int instanceCount , IndexBuffer indexBuffer )
2011-12-14 11:49:04 +00:00
{
2012-09-22 11:17:36 +00:00
SharpDX . Direct3D11 . EffectTechnique technique = SetupEffectForDraw ( ) ;
int vertexCount = DxFormatConverter . CalculateVertexCount ( primitiveType , primitiveCount ) ;
nativeDevice . InputAssembler . PrimitiveTopology = DxFormatConverter . Translate ( primitiveType ) ;
nativeDevice . Rasterizer . SetViewports ( currentViewport ) ;
2012-09-28 13:05:38 +00:00
//nativeDevice.OutputMerger.SetTargets(this.depthStencilView, this.renderView);
2012-09-22 11:17:36 +00:00
if ( indexBuffer ! = null )
{
SetIndexBuffer ( indexBuffer ) ;
}
for ( int i = 0 ; i < technique . Description . PassCount ; + + i )
{
technique . GetPassByIndex ( i ) . Apply ( nativeDevice ) ;
nativeDevice . DrawIndexedInstanced ( vertexCount , instanceCount , startIndex , baseVertex , 0 ) ;
}
nativeDevice . InputAssembler . InputLayout . Dispose ( ) ;
nativeDevice . InputAssembler . InputLayout = null ;
2011-11-16 14:27:53 +00:00
}
2011-12-14 11:49:04 +00:00
#endregion // DrawInstancedPrimitives
#region DrawUserIndexedPrimitives < T >
2012-09-08 09:07:23 +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-16 14:27:53 +00:00
{
2012-01-17 05:14:18 +00:00
int vertexCount = vertexData . Length ;
int indexCount = indexData . Length ;
2012-09-27 10:08:00 +00:00
VertexBuffer vertexBuffer = new VertexBuffer ( vertexDeclaration . GraphicsDevice , vertexDeclaration , vertexCount , BufferUsage . WriteOnly ) ;
vertexBuffer . SetData ( vertexData ) ;
this . SetVertexBuffers ( new [ ] { new Framework . Graphics . VertexBufferBinding ( vertexBuffer , vertexOffset ) } ) ;
2012-01-17 05:14:18 +00:00
2012-09-27 10:08:00 +00:00
IndexBuffer indexBuffer = new IndexBuffer ( vertexDeclaration . GraphicsDevice , indexFormat , indexCount , BufferUsage . WriteOnly ) ;
2012-01-19 05:57:54 +00:00
if ( indexData . GetType ( ) = = typeof ( Int16 [ ] ) )
2012-09-20 10:23:03 +00:00
{
2012-09-27 10:08:00 +00:00
indexBuffer . SetData < short > ( ( short [ ] ) indexData ) ;
2012-09-20 10:23:03 +00:00
}
2012-01-19 05:57:54 +00:00
else
2012-09-20 10:23:03 +00:00
{
2012-09-27 10:08:00 +00:00
indexBuffer . SetData < int > ( ( int [ ] ) indexData ) ;
2012-09-20 10:23:03 +00:00
}
2012-09-27 10:08:00 +00:00
DrawIndexedPrimitives ( primitiveType , 0 , vertexOffset , numVertices , indexOffset , primitiveCount , indexBuffer ) ;
2011-11-16 14:27:53 +00:00
}
2011-12-14 11:49:04 +00:00
#endregion // DrawUserIndexedPrimitives<T>
2011-11-16 14:27:53 +00:00
2011-12-14 11:49:04 +00:00
#region DrawUserPrimitives < T >
public void DrawUserPrimitives < T > ( PrimitiveType primitiveType , T [ ] vertexData , int vertexOffset , int primitiveCount , VertexDeclaration vertexDeclaration ) where T : struct , IVertexType
2011-11-16 14:27:53 +00:00
{
2012-01-09 18:09:10 +00:00
int vertexCount = vertexData . Length ;
2012-09-15 13:43:31 +00:00
DxVertexBuffer vb11 = new DxVertexBuffer ( nativeDevice . Device , vertexDeclaration , vertexCount , BufferUsage . None ) ;
2012-01-09 18:09:10 +00:00
vb11 . SetData < T > ( null , vertexData ) ;
SharpDX . Direct3D11 . VertexBufferBinding nativeVertexBufferBindings = new SharpDX . Direct3D11 . VertexBufferBinding ( vb11 . NativeBuffer , vertexDeclaration . VertexStride , 0 ) ;
2012-09-08 10:37:41 +00:00
nativeDevice . InputAssembler . SetVertexBuffers ( 0 , nativeVertexBufferBindings ) ;
2012-01-09 18:09:10 +00:00
2012-08-10 08:38:01 +00:00
SharpDX . Direct3D11 . EffectPass pass ; SharpDX . Direct3D11 . EffectTechnique technique ; ShaderBytecode passSignature ;
SetupEffectForDraw ( out pass , out technique , out passSignature ) ;
2012-09-08 10:37:41 +00:00
var layout = CreateInputLayout ( nativeDevice . Device , passSignature , vertexDeclaration ) ;
2012-08-10 08:38:01 +00:00
2012-09-08 10:37:41 +00:00
nativeDevice . InputAssembler . InputLayout = layout ;
2012-08-10 08:38:01 +00:00
// Prepare All the stages
2012-09-15 13:43:31 +00:00
nativeDevice . InputAssembler . PrimitiveTopology = DxFormatConverter . Translate ( primitiveType ) ;
2012-09-08 10:37:41 +00:00
nativeDevice . Rasterizer . SetViewports ( currentViewport ) ;
2012-08-10 08:38:01 +00:00
//device.OutputMerger.SetTargets(this.depthStencilView, this.renderView);
for ( int i = 0 ; i < technique . Description . PassCount ; + + i )
{
2012-09-08 10:37:41 +00:00
pass . Apply ( nativeDevice ) ;
nativeDevice . Draw ( primitiveCount , vertexOffset ) ;
2012-09-08 09:07:23 +00:00
}
layout . Dispose ( ) ;
layout = null ;
2011-12-14 11:49:04 +00:00
}
2011-11-16 14:27:53 +00:00
2011-12-14 11:49:04 +00:00
#endregion // DrawUserPrimitives<T>
2011-11-16 14:27:53 +00:00
2012-09-08 10:37:41 +00:00
#region SetupEffectForDraw
private void SetupEffectForDraw ( out SharpDX . Direct3D11 . EffectPass pass , out SharpDX . Direct3D11 . EffectTechnique technique ,
out ShaderBytecode passSignature )
2011-12-14 11:49:04 +00:00
{
// get the current effect
//TODO: check for null and throw exception
2012-09-15 13:43:31 +00:00
EffectDX effect = this . currentEffect ;
2011-11-16 14:27:53 +00:00
2011-12-14 11:49:04 +00:00
// get the input semantic of the current effect / technique that is used
2012-09-05 20:17:34 +00:00
//TODO: check for null's and throw exceptions
// TODO: get the correct pass index!
technique = effect . GetCurrentTechnique ( ) . NativeTechnique ;
2011-12-14 11:49:04 +00:00
pass = technique . GetPassByIndex ( 0 ) ;
passSignature = pass . Description . Signature ;
}
2012-09-20 11:53:50 +00:00
private SharpDX . Direct3D11 . EffectTechnique SetupEffectForDraw ( )
{
//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 ) ;
SetupInputLayout ( pass . Description . Signature ) ;
return technique ;
}
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 SetupInputLayout
private InputLayout SetupInputLayout ( ShaderBytecode passSignature )
2011-12-14 11:49:04 +00:00
{
// get the VertexDeclaration from current VertexBuffer to create input layout for the input assembler
2012-09-22 11:17:36 +00:00
var layout = CreateInputLayout ( nativeDevice . Device , passSignature , currentVertexBuffer ) ;
2011-11-16 14:27:53 +00:00
2012-09-08 10:37:41 +00:00
nativeDevice . InputAssembler . InputLayout = layout ;
2012-09-08 09:07:23 +00:00
return layout ;
2011-11-16 14:27:53 +00:00
}
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 SetIndexBuffer
public void SetIndexBuffer ( IndexBuffer indexBuffer )
2011-11-16 14:27:53 +00:00
{
2011-12-14 11:49:04 +00:00
if ( indexBuffer = = null )
throw new ArgumentNullException ( "indexBuffer" ) ;
2011-11-16 14:27:53 +00:00
2011-12-14 11:49:04 +00:00
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
2011-12-14 11:49:04 +00:00
if ( nativeIndexBuffer ! = null )
{
2012-09-08 10:37:41 +00:00
nativeDevice . InputAssembler . SetIndexBuffer ( nativeIndexBuffer . NativeBuffer ,
2012-09-15 13:43:31 +00:00
DxFormatConverter . Translate ( indexBuffer . IndexElementSize ) , 0 ) ;
2011-12-14 11:49:04 +00:00
}
else
throw new Exception ( "couldn't fetch native DirectX10 IndexBuffer" ) ;
2012-08-29 10:48:21 +00:00
}
2012-09-08 10:37:41 +00:00
#endregion
2012-08-29 10:48:21 +00:00
2012-09-08 10:37:41 +00:00
#region SetVertexBuffers
public void SetVertexBuffers ( ANX . Framework . Graphics . VertexBufferBinding [ ] vertexBuffers )
2011-11-16 14:27:53 +00:00
{
2011-12-14 11:49:04 +00:00
if ( vertexBuffers = = null )
throw new ArgumentNullException ( "vertexBuffers" ) ;
2011-11-16 14:27:53 +00:00
2012-09-22 11:17:36 +00:00
this . currentVertexBufferCount = vertexBuffers . Length ;
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 ] . VertexBuffer ;
}
2011-11-16 14:27:53 +00:00
2012-09-08 10:37:41 +00:00
var nativeVertexBufferBindings = new SharpDX . Direct3D11 . VertexBufferBinding [ vertexBuffers . Length ] ;
2011-12-14 11:49:04 +00:00
for ( int i = 0 ; i < vertexBuffers . Length ; i + + )
2011-11-16 14:27:53 +00:00
{
2011-12-14 11:49:04 +00:00
ANX . Framework . Graphics . VertexBufferBinding anxVertexBufferBinding = vertexBuffers [ i ] ;
2012-09-15 13:43:31 +00:00
var nativeVertexBuffer = anxVertexBufferBinding . VertexBuffer . NativeVertexBuffer as DxVertexBuffer ;
2011-12-14 11:49:04 +00:00
if ( nativeVertexBuffer ! = null )
{
2012-09-20 07:17:14 +00:00
int vertexStride = anxVertexBufferBinding . VertexBuffer . VertexDeclaration . VertexStride ;
nativeVertexBufferBindings [ i ] = new SharpDX . Direct3D11 . VertexBufferBinding ( nativeVertexBuffer . NativeBuffer , vertexStride , anxVertexBufferBinding . VertexOffset * vertexStride ) ;
2011-12-14 11:49:04 +00:00
}
else
{
throw new Exception ( "couldn't fetch native DirectX10 VertexBuffer" ) ;
}
2011-11-16 14:27:53 +00:00
}
2012-09-08 10:37:41 +00:00
nativeDevice . InputAssembler . SetVertexBuffers ( 0 , nativeVertexBufferBindings ) ;
}
#endregion
2011-12-14 11:49:04 +00:00
2012-09-08 10:37:41 +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
{
currentViewport = new SharpDX . Direct3D11 . Viewport ( x , y , width , height , minDepth , maxDepth ) ;
}
#endregion
2011-11-16 14:27:53 +00:00
2012-09-08 10:37:41 +00:00
#region CreateInputLayout
2012-09-22 11:17:36 +00:00
private InputLayout CreateInputLayout ( 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 InputLayout ( device , passSignature , inputElements . ToArray ( ) ) ;
}
private InputLayout CreateInputLayout ( 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 + + ;
}
2011-11-16 14:27:53 +00:00
// Layout from VertexShader input signature
2012-09-22 11:17:36 +00:00
return new InputLayout ( device , passSignature , inputElements . ToArray ( ) ) ;
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 CreateInputElementFromVertexElement
2012-09-22 11:17:36 +00:00
private InputElement CreateInputElementFromVertexElement ( VertexElement vertexElement , int slot )
{
return CreateInputElementFromVertexElement ( vertexElement , 0 , slot ) ;
}
private InputElement CreateInputElementFromVertexElement ( VertexElement vertexElement , int instanceFrequency , int slot )
2012-09-07 09:48:45 +00:00
{
2012-09-15 13:43:31 +00:00
string elementName = DxFormatConverter . Translate ( ref vertexElement ) ;
Format elementFormat = DxFormatConverter . ConvertVertexElementFormat ( vertexElement . VertexElementFormat ) ;
2012-09-22 11:17:36 +00:00
return new InputElement ( elementName , vertexElement . UsageIndex , elementFormat , vertexElement . Offset , slot , instanceFrequency = = 0 ? InputClassification . PerVertexData : InputClassification . PerInstanceData , instanceFrequency ) ;
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 SetRenderTargets
public void SetRenderTargets ( params RenderTargetBinding [ ] renderTargets )
2011-11-16 14:27:53 +00:00
{
2011-12-14 11:49:04 +00:00
if ( renderTargets = = null )
2011-11-16 14:27:53 +00:00
{
2011-12-14 11:49:04 +00:00
// reset the RenderTarget to backbuffer
2012-08-10 08:38:01 +00:00
for ( int i = 0 ; i < renderTargetView . Length ; i + + )
2011-12-14 11:49:04 +00:00
{
2012-08-10 08:38:01 +00:00
if ( renderTargetView [ i ] ! = null )
{
renderTargetView [ i ] . Dispose ( ) ;
renderTargetView [ i ] = null ;
}
2011-12-14 11:49:04 +00:00
}
2012-08-10 10:17:24 +00:00
//deviceContext.OutputMerger.SetRenderTargets(1, new RenderTargetView[] { this.renderView }, this.depthStencilView);
2012-09-08 10:37:41 +00:00
nativeDevice . OutputMerger . SetTargets ( this . depthStencilView , this . renderView ) ;
2011-11-16 14:27:53 +00:00
}
else
{
2012-08-10 08:38:01 +00:00
int renderTargetCount = renderTargets . Length ;
if ( this . renderTargetView . Length ! = renderTargetCount )
2011-12-14 11:49:04 +00:00
{
2012-08-10 08:38:01 +00:00
for ( int i = 0 ; i < renderTargetView . Length ; i + + )
2011-12-14 11:49:04 +00:00
{
2012-08-10 08:38:01 +00:00
if ( renderTargetView [ i ] ! = null )
2011-12-14 11:49:04 +00:00
{
2012-08-10 08:38:01 +00:00
renderTargetView [ i ] . Dispose ( ) ;
renderTargetView [ i ] = null ;
2011-12-14 11:49:04 +00:00
}
}
2012-08-10 08:38:01 +00:00
this . renderTargetView = new RenderTargetView [ renderTargetCount ] ;
2011-12-14 11:49:04 +00:00
}
2012-08-10 08:38:01 +00:00
for ( int i = 0 ; i < renderTargetCount ; i + + )
2011-12-14 11:49:04 +00:00
{
2012-08-10 08:38:01 +00:00
RenderTarget2D renderTarget = renderTargets [ i ] . RenderTarget as RenderTarget2D ;
if ( renderTarget ! = null )
{
RenderTarget2D_DX11 nativeRenderTarget = renderTarget . NativeRenderTarget as RenderTarget2D_DX11 ;
if ( renderTargetView [ i ] ! = null )
{
renderTargetView [ i ] . Dispose ( ) ;
}
2012-09-08 10:37:41 +00:00
renderTargetView [ i ] = new RenderTargetView ( nativeDevice . Device ,
2012-09-15 13:43:31 +00:00
( ( DxTexture2D ) nativeRenderTarget ) . NativeShaderResourceView . Resource ) ;
2012-08-10 08:38:01 +00:00
}
2011-12-14 11:49:04 +00:00
}
2012-08-10 08:38:01 +00:00
2012-08-10 10:17:24 +00:00
//deviceContext.OutputMerger.SetRenderTargets(renderTargetCount, renderTargetView, this.depthStencilView);
2012-09-08 10:37:41 +00:00
nativeDevice . OutputMerger . SetTargets ( this . depthStencilView , this . renderTargetView ) ;
2011-11-16 14:27:53 +00:00
}
}
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 DisposeRenderView
2012-09-15 13:43:31 +00:00
protected void DisposeRenderView ( )
2012-09-08 10:37:41 +00:00
{
renderView . Dispose ( ) ;
renderView = null ;
2011-12-14 11:49:04 +00:00
2012-09-08 10:37:41 +00:00
backBuffer . Dispose ( ) ;
backBuffer = null ;
}
#endregion
2011-11-22 14:51:30 +00:00
2012-09-08 10:37:41 +00:00
#region Dispose
public void Dispose ( )
2011-11-22 14:51:30 +00:00
{
2012-08-10 08:38:01 +00:00
for ( int i = 0 ; i < renderTargetView . Length ; i + + )
2011-12-14 11:49:04 +00:00
{
2012-08-10 08:38:01 +00:00
if ( renderTargetView [ i ] ! = null )
{
renderTargetView [ i ] . Dispose ( ) ;
renderTargetView [ i ] = null ;
}
2011-12-14 11:49:04 +00:00
}
if ( swapChain ! = null )
{
2012-09-08 10:37:41 +00:00
DisposeRenderView ( ) ;
2011-12-14 11:49:04 +00:00
swapChain . Dispose ( ) ;
swapChain = null ;
}
if ( this . depthStencilView ! = null )
{
this . depthStencilBuffer . Dispose ( ) ;
this . depthStencilBuffer = null ;
this . depthStencilView . Dispose ( ) ;
this . depthStencilView = null ;
}
//TODO: dispose everything else
2011-11-16 14:27:53 +00:00
}
2012-09-08 10:37:41 +00:00
#endregion
2012-09-15 13:43:31 +00:00
internal DeviceContext NativeDevice
{
get
{
return this . nativeDevice ;
}
}
2011-11-22 14:51:30 +00:00
}
2011-11-16 14:27:53 +00:00
}