2012-09-08 09:07:23 +00:00
#define DIRECTX_DEBUG_LAYER
2011-11-16 14:27:53 +00:00
using System ;
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 ANX.RenderSystem.Windows.DX11.Helpers ;
using SharpDX.D3DCompiler ;
using SharpDX.Direct3D ;
using SharpDX.Direct3D11 ;
using SharpDX.DXGI ;
using Device = SharpDX . Direct3D11 . Device ;
using ANX.BaseDirectX ;
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
namespace ANX.RenderSystem.Windows.DX11
{
public class GraphicsDeviceWindowsDX11 : INativeGraphicsDevice
2011-12-14 11:49:04 +00:00
{
#region Constants
private const float ColorMultiplier = 1f / 255f ;
#endregion
2012-09-07 09:48:45 +00:00
#region Private
2011-12-14 11:49:04 +00:00
private DeviceContext deviceContext ;
private SwapChain swapChain ;
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 ;
internal Effect_DX11 currentEffect ;
private VertexBuffer currentVertexBuffer ;
private IndexBuffer currentIndexBuffer ;
private SharpDX . Direct3D11 . Viewport currentViewport ;
2011-12-14 11:49:04 +00:00
private uint lastClearColor ;
private SharpDX . Color4 clearColor ;
2012-09-07 09:48:45 +00:00
#endregion
2011-12-14 11:49:04 +00:00
2012-09-07 09:48:45 +00:00
internal DeviceContext NativeDevice
{
get
{
return this . deviceContext ;
}
}
public bool VSync { get ; set ; }
2011-11-16 14:27:53 +00:00
public GraphicsDeviceWindowsDX11 ( PresentationParameters presentationParameters )
{
2012-09-07 09:48:45 +00:00
VSync = true ;
2011-12-14 11:49:04 +00:00
2011-11-16 14:27:53 +00:00
// SwapChain description
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 ) ,
BaseFormatConverter . Translate ( presentationParameters . BackBufferFormat ) ) ,
2011-11-16 14:27:53 +00:00
IsWindowed = true ,
OutputHandle = presentationParameters . DeviceWindowHandle ,
SampleDescription = new SampleDescription ( 1 , 0 ) ,
SwapEffect = SwapEffect . Discard ,
Usage = Usage . RenderTargetOutput
} ;
// Create Device and SwapChain
2011-12-14 11:49:04 +00:00
Device dxDevice ;
2011-12-14 12:50:44 +00:00
2011-11-16 14:27:53 +00:00
#if DIRECTX_DEBUG_LAYER
// http://msdn.microsoft.com/en-us/library/windows/desktop/bb205068(v=vs.85).aspx
2011-12-14 11:49:04 +00:00
Device . CreateWithSwapChain ( DriverType . Hardware , DeviceCreationFlags . Debug , desc , out dxDevice , out swapChain ) ;
2011-11-16 14:27:53 +00:00
#else
2011-12-14 11:49:04 +00:00
Device . CreateWithSwapChain ( DriverType . Hardware , DeviceCreationFlags . None , desc , out dxDevice , out swapChain ) ;
2011-11-16 14:27:53 +00:00
#endif
2011-12-14 13:02:49 +00:00
this . deviceContext = dxDevice . ImmediateContext ;
2011-11-16 14:27:53 +00:00
// Ignore all windows events
Factory factory = swapChain . GetParent < Factory > ( ) ;
factory . MakeWindowAssociation ( presentationParameters . DeviceWindowHandle , WindowAssociationFlags . IgnoreAll ) ;
2012-09-07 09:48:45 +00:00
WindowHelper . ResizeRenderWindow ( presentationParameters ) ;
2011-12-14 11:49:04 +00:00
2011-11-16 14:27:53 +00:00
// New RenderTargetView from the backbuffer
backBuffer = SharpDX . Direct3D11 . Texture2D . FromSwapChain < SharpDX . Direct3D11 . Texture2D > ( swapChain , 0 ) ;
2011-12-14 11:49:04 +00:00
renderView = new RenderTargetView ( deviceContext . Device , backBuffer ) ;
2011-11-16 14:27:53 +00:00
currentViewport = new SharpDX . Direct3D11 . Viewport ( 0 , 0 , presentationParameters . BackBufferWidth , presentationParameters . BackBufferHeight ) ;
2011-12-14 11:49:04 +00:00
//
// create the depth stencil buffer
//
2012-09-07 09:48:45 +00:00
Format depthFormat = BaseFormatConverter . Translate ( presentationParameters . DepthStencilFormat ) ;
2011-12-14 11:49:04 +00:00
if ( depthFormat ! = Format . Unknown )
{
CreateDepthStencilBuffer ( depthFormat ) ;
}
2011-11-16 14:27:53 +00:00
}
2011-12-14 11:49:04 +00:00
private 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
} ;
this . depthStencilBuffer = new SharpDX . Direct3D11 . Texture2D ( deviceContext . Device , depthStencilTextureDesc ) ;
2011-11-16 14:27:53 +00:00
2011-12-14 11:49:04 +00:00
this . depthStencilView = new DepthStencilView ( deviceContext . Device , this . depthStencilBuffer ) ;
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
}
2011-12-14 11:49:04 +00:00
#region Clear
public void Clear ( ref Color color )
2011-11-16 14:27:53 +00:00
{
2011-12-14 11:49:04 +00:00
uint newClearColor = color . PackedValue ;
if ( lastClearColor ! = newClearColor )
2011-11-16 14:27:53 +00:00
{
2011-12-14 11:49:04 +00:00
lastClearColor = newClearColor ;
clearColor . Red = color . R * ColorMultiplier ;
clearColor . Green = color . G * ColorMultiplier ;
clearColor . Blue = color . B * ColorMultiplier ;
clearColor . Alpha = color . A * ColorMultiplier ;
2011-11-16 14:27:53 +00:00
}
2011-12-14 11:49:04 +00:00
2012-08-10 08:38:01 +00:00
if ( this . renderTargetView [ 0 ] = = null )
{
this . deviceContext . ClearRenderTargetView ( this . renderView , this . clearColor ) ;
}
else
{
for ( int i = 0 ; i < this . renderTargetView . Length ; i + + )
{
if ( this . renderTargetView [ i ] = = null )
{
break ;
}
this . deviceContext . ClearRenderTargetView ( this . renderTargetView [ i ] , this . clearColor ) ;
}
}
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 )
{
this . deviceContext . ClearRenderTargetView ( this . renderView , this . clearColor ) ;
}
else
{
for ( int i = 0 ; i < this . renderTargetView . Length ; i + + )
{
if ( this . renderTargetView [ i ] = = null )
{
break ;
}
this . deviceContext . ClearRenderTargetView ( this . renderTargetView [ i ] , this . clearColor ) ;
}
}
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
deviceContext . ClearDepthStencilView ( this . depthStencilView , DepthStencilClearFlags . Depth | DepthStencilClearFlags . Stencil , depth , ( byte ) stencil ) ;
}
else if ( ( options | ClearOptions . Stencil ) = = options )
{
deviceContext . ClearDepthStencilView ( this . depthStencilView , DepthStencilClearFlags . Stencil , depth , ( byte ) stencil ) ;
}
else
{
deviceContext . ClearDepthStencilView ( this . depthStencilView , DepthStencilClearFlags . Depth , depth , ( byte ) stencil ) ;
}
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
#region DrawPrimitives & DrawIndexedPrimitives
2012-09-08 09:07:23 +00:00
public void DrawIndexedPrimitives ( PrimitiveType primitiveType , int baseVertex , int minVertexIndex ,
2012-08-19 14:38:58 +00:00
int numVertices , int startIndex , int primitiveCount )
2012-09-08 09:07:23 +00:00
{
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 ) ;
2011-11-16 14:27:53 +00:00
2012-09-08 09:07:23 +00:00
// Prepare All the stages
2012-09-07 09:48:45 +00:00
deviceContext . InputAssembler . PrimitiveTopology = BaseFormatConverter . Translate ( primitiveType ) ;
2012-09-08 09:07:23 +00:00
deviceContext . Rasterizer . SetViewports ( currentViewport ) ;
2011-11-16 14:27:53 +00:00
2012-09-08 09:07:23 +00:00
deviceContext . OutputMerger . SetTargets ( this . depthStencilView , this . renderView ) ;
2011-11-16 14:27:53 +00:00
2012-09-08 09:07:23 +00:00
for ( int i = 0 ; i < technique . Description . PassCount ; + + i )
{
pass . Apply ( deviceContext ) ;
2012-09-07 09:48:45 +00:00
deviceContext . DrawIndexed ( BaseFormatConverter . CalculateVertexCount ( primitiveType , primitiveCount ) , startIndex , baseVertex ) ;
2012-09-08 09:07:23 +00:00
}
2011-11-16 14:27:53 +00:00
2012-09-08 09:07:23 +00:00
layout . Dispose ( ) ;
layout = null ;
}
2011-11-16 14:27:53 +00:00
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-07 09:48:45 +00:00
deviceContext . InputAssembler . PrimitiveTopology = BaseFormatConverter . Translate ( primitiveType ) ;
2012-09-08 09:07:23 +00:00
deviceContext . Rasterizer . SetViewports ( currentViewport ) ;
2011-11-16 14:27:53 +00:00
2012-09-08 09:07:23 +00:00
deviceContext . 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 )
{
pass . Apply ( deviceContext ) ;
deviceContext . Draw ( primitiveCount , vertexOffset ) ;
}
layout . Dispose ( ) ;
layout = null ;
}
2011-11-16 14:27:53 +00:00
2011-12-14 11:49:04 +00:00
#endregion // DrawPrimitives & DrawIndexedPrimitives
2011-11-16 14:27:53 +00:00
2011-12-14 11:49:04 +00:00
#region DrawInstancedPrimitives
public void DrawInstancedPrimitives ( PrimitiveType primitiveType , int baseVertex , int minVertexIndex , int numVertices , int startIndex , int primitiveCount , int instanceCount )
{
2012-01-19 05:57:54 +00:00
deviceContext . DrawIndexedInstanced ( numVertices , instanceCount , startIndex , baseVertex , 0 ) ;
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-08 09:07:23 +00:00
var vb11 = new VertexBuffer_DX11 ( this . deviceContext . Device , vertexDeclaration , vertexCount , BufferUsage . None ) ;
2012-01-17 05:14:18 +00:00
vb11 . SetData < T > ( null , vertexData ) ;
2012-09-08 09:07:23 +00:00
var nativeVertexBufferBindings = new SharpDX . Direct3D11 . VertexBufferBinding ( vb11 . NativeBuffer ,
vertexDeclaration . VertexStride , 0 ) ;
2012-01-17 05:14:18 +00:00
deviceContext . InputAssembler . SetVertexBuffers ( 0 , nativeVertexBufferBindings ) ;
IndexBuffer_DX11 idx10 = new IndexBuffer_DX11 ( this . deviceContext . Device , indexFormat , indexCount , BufferUsage . None ) ;
2012-01-19 05:57:54 +00:00
if ( indexData . GetType ( ) = = typeof ( Int16 [ ] ) )
idx10 . SetData < short > ( null , ( short [ ] ) indexData ) ;
else
idx10 . SetData < int > ( null , ( int [ ] ) indexData ) ;
2012-01-17 05:14:18 +00:00
DrawIndexedPrimitives ( primitiveType , 0 , vertexOffset , numVertices , indexOffset , primitiveCount ) ;
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 ;
VertexBuffer_DX11 vb11 = new VertexBuffer_DX11 ( this . deviceContext . Device , vertexDeclaration , vertexCount , BufferUsage . None ) ;
vb11 . SetData < T > ( null , vertexData ) ;
SharpDX . Direct3D11 . VertexBufferBinding nativeVertexBufferBindings = new SharpDX . Direct3D11 . VertexBufferBinding ( vb11 . NativeBuffer , vertexDeclaration . VertexStride , 0 ) ;
deviceContext . InputAssembler . SetVertexBuffers ( 0 , nativeVertexBufferBindings ) ;
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 ) ;
var layout = CreateInputLayout ( deviceContext . Device , passSignature , vertexDeclaration ) ;
deviceContext . InputAssembler . InputLayout = layout ;
// Prepare All the stages
2012-09-07 09:48:45 +00:00
deviceContext . InputAssembler . PrimitiveTopology = BaseFormatConverter . Translate ( primitiveType ) ;
2012-08-10 08:38:01 +00:00
deviceContext . Rasterizer . SetViewports ( currentViewport ) ;
//device.OutputMerger.SetTargets(this.depthStencilView, this.renderView);
for ( int i = 0 ; i < technique . Description . PassCount ; + + i )
{
pass . Apply ( deviceContext ) ;
deviceContext . 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
2011-12-14 11:49:04 +00:00
private void SetupEffectForDraw ( out SharpDX . Direct3D11 . EffectPass pass , out SharpDX . Direct3D11 . EffectTechnique technique , out ShaderBytecode passSignature )
{
// get the current effect
//TODO: check for null and throw exception
Effect_DX11 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 ;
}
2011-11-16 14:27:53 +00:00
2012-09-08 09:07:23 +00:00
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
//TODO: check for null and throw exception
VertexDeclaration vertexDeclaration = currentVertexBuffer . VertexDeclaration ;
var layout = CreateInputLayout ( deviceContext . Device , passSignature , vertexDeclaration ) ;
2011-11-16 14:27:53 +00:00
2011-12-14 11:49:04 +00:00
deviceContext . InputAssembler . InputLayout = layout ;
2012-09-08 09:07:23 +00:00
return layout ;
2011-11-16 14:27:53 +00:00
}
2011-12-14 11:49:04 +00:00
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 ;
2011-11-16 14:27:53 +00:00
2011-12-14 11:49:04 +00:00
IndexBuffer_DX11 nativeIndexBuffer = indexBuffer . NativeIndexBuffer as IndexBuffer_DX11 ;
2011-11-16 14:27:53 +00:00
2011-12-14 11:49:04 +00:00
if ( nativeIndexBuffer ! = null )
{
2012-09-07 09:48:45 +00:00
deviceContext . InputAssembler . SetIndexBuffer ( nativeIndexBuffer . NativeBuffer ,
BaseFormatConverter . Translate ( indexBuffer . IndexElementSize ) , 0 ) ;
2011-12-14 11:49:04 +00:00
}
else
{
throw new Exception ( "couldn't fetch native DirectX10 IndexBuffer" ) ;
}
2011-11-16 14:27:53 +00:00
}
2012-08-29 10:48:21 +00:00
#if XNAEXT
public void SetConstantBuffer ( int slot , ANX . Framework . Graphics . ConstantBuffer constantBuffer )
{
if ( constantBuffer = = null )
{
throw new ArgumentNullException ( "constantBuffer" ) ;
}
throw new NotImplementedException ( ) ;
}
#endif
2011-12-14 11:49:04 +00:00
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
}
2011-12-14 11:49:04 +00:00
this . currentVertexBuffer = vertexBuffers [ 0 ] . VertexBuffer ; //TODO: hmmmmm, not nice :-)
2011-11-16 14:27:53 +00:00
2011-12-14 11:49:04 +00:00
SharpDX . Direct3D11 . VertexBufferBinding [ ] nativeVertexBufferBindings = new SharpDX . Direct3D11 . VertexBufferBinding [ vertexBuffers . Length ] ;
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 ] ;
VertexBuffer_DX11 nativeVertexBuffer = anxVertexBufferBinding . VertexBuffer . NativeVertexBuffer as VertexBuffer_DX11 ;
if ( nativeVertexBuffer ! = null )
{
nativeVertexBufferBindings [ i ] = new SharpDX . Direct3D11 . VertexBufferBinding ( nativeVertexBuffer . NativeBuffer , anxVertexBufferBinding . VertexBuffer . VertexDeclaration . VertexStride , anxVertexBufferBinding . VertexOffset ) ;
}
else
{
throw new Exception ( "couldn't fetch native DirectX10 VertexBuffer" ) ;
}
2011-11-16 14:27:53 +00:00
}
2011-12-14 11:49:04 +00:00
deviceContext . InputAssembler . SetVertexBuffers ( 0 , nativeVertexBufferBindings ) ;
}
public void SetViewport ( ANX . Framework . Graphics . Viewport viewport )
{
this . currentViewport = new SharpDX . Direct3D11 . Viewport ( viewport . X , viewport . Y , viewport . Width , viewport . Height , viewport . MinDepth , viewport . MaxDepth ) ;
2011-11-16 14:27:53 +00:00
}
/// <summary>
/// This method creates a InputLayout which is needed by DirectX 10 for rendering primitives. The VertexDeclaration of ANX/XNA needs to be mapped
/// to the DirectX 10 types. This is what this method is for.
/// </summary>
private InputLayout CreateInputLayout ( Device device , ShaderBytecode passSignature , VertexDeclaration vertexDeclaration )
{
VertexElement [ ] vertexElements = vertexDeclaration . GetVertexElements ( ) ;
int elementCount = vertexElements . Length ;
InputElement [ ] inputElements = new InputElement [ elementCount ] ;
for ( int i = 0 ; i < elementCount ; i + + )
{
inputElements [ i ] = CreateInputElementFromVertexElement ( vertexElements [ i ] ) ;
}
// Layout from VertexShader input signature
return new InputLayout ( device , passSignature , inputElements ) ;
}
private InputElement CreateInputElementFromVertexElement ( VertexElement vertexElement )
2012-09-07 09:48:45 +00:00
{
string elementName = BaseFormatConverter . Translate ( ref vertexElement ) ;
2011-11-16 14:27:53 +00:00
Format elementFormat ;
switch ( vertexElement . VertexElementFormat )
{
case VertexElementFormat . Vector2 :
elementFormat = Format . R32G32_Float ;
break ;
case VertexElementFormat . Vector3 :
elementFormat = Format . R32G32B32_Float ;
break ;
case VertexElementFormat . Vector4 :
elementFormat = Format . R32G32B32A32_Float ;
break ;
case VertexElementFormat . Color :
elementFormat = Format . R8G8B8A8_UNorm ;
break ;
default :
throw new Exception ( "can't map '" + vertexElement . VertexElementFormat . ToString ( ) + "' to DXGI.Format in DirectX10 RenderSystem CreateInputElementFromVertexElement" ) ;
}
return new InputElement ( elementName , vertexElement . UsageIndex , elementFormat , vertexElement . Offset , 0 ) ;
}
2011-12-14 11:49:04 +00:00
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);
2011-12-14 11:49:04 +00:00
deviceContext . 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 ( ) ;
}
renderTargetView [ i ] = new RenderTargetView ( deviceContext . Device , ( ( Texture2D_DX11 ) nativeRenderTarget ) . NativeShaderResourceView . Resource ) ;
}
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-08-10 08:38:01 +00:00
deviceContext . OutputMerger . SetTargets ( this . depthStencilView , this . renderTargetView ) ;
2011-11-16 14:27:53 +00:00
}
}
2011-12-14 11:49:04 +00:00
public void GetBackBufferData < T > ( ANX . Framework . Rectangle ? rect , T [ ] data , int startIndex , int elementCount ) where T : struct
2011-11-16 14:27:53 +00:00
{
throw new NotImplementedException ( ) ;
}
public void GetBackBufferData < T > ( T [ ] data ) where T : struct
{
throw new NotImplementedException ( ) ;
}
public void GetBackBufferData < T > ( T [ ] data , int startIndex , int elementCount ) where T : struct
{
throw new NotImplementedException ( ) ;
}
2011-12-14 11:49:04 +00:00
public void ResizeBuffers ( PresentationParameters presentationParameters )
2011-11-16 14:27:53 +00:00
{
2011-12-14 11:49:04 +00:00
if ( swapChain ! = null )
{
renderView . Dispose ( ) ;
backBuffer . Dispose ( ) ;
2011-11-16 14:27:53 +00:00
2011-12-14 11:49:04 +00:00
//TODO: handle format
2011-11-16 14:27:53 +00:00
2012-08-10 10:17:24 +00:00
swapChain . ResizeBuffers ( swapChain . Description . BufferCount , presentationParameters . BackBufferWidth , presentationParameters . BackBufferHeight , Format . R8G8B8A8_UNorm , swapChain . Description . Flags ) ;
2011-11-16 14:27:53 +00:00
2011-12-14 11:49:04 +00:00
backBuffer = SharpDX . Direct3D11 . Texture2D . FromSwapChain < SharpDX . Direct3D11 . Texture2D > ( swapChain , 0 ) ;
renderView = new RenderTargetView ( deviceContext . Device , backBuffer ) ;
2011-11-16 14:27:53 +00:00
2011-12-14 11:49:04 +00:00
currentViewport = new SharpDX . Direct3D11 . Viewport ( 0 , 0 , presentationParameters . BackBufferWidth , presentationParameters . BackBufferHeight ) ;
2011-11-16 14:27:53 +00:00
2011-12-14 11:49:04 +00:00
//
// create the depth stencil buffer
//
2012-09-07 09:48:45 +00:00
Format depthFormat = BaseFormatConverter . Translate ( presentationParameters . DepthStencilFormat ) ;
2011-12-14 11:49:04 +00:00
if ( depthFormat ! = Format . Unknown )
{
CreateDepthStencilBuffer ( depthFormat ) ;
}
}
2012-09-07 09:48:45 +00:00
WindowHelper . ResizeRenderWindow ( presentationParameters ) ;
2011-11-16 14:27:53 +00:00
}
2011-11-22 14:51:30 +00:00
public void Dispose ( )
{
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 )
{
renderView . Dispose ( ) ;
renderView = null ;
backBuffer . Dispose ( ) ;
backBuffer = null ;
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
}
2011-11-22 14:51:30 +00:00
}
2011-11-16 14:27:53 +00:00
}