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 ;
2012-09-29 09:42:27 +00:00
using Dx11 = SharpDX . Direct3D11 ;
2012-09-15 13:43:31 +00:00
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
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 ;
private Dx11 . DeviceContext nativeDevice ;
private Dx11 . RenderTargetView [ ] renderTargetView = new RenderTargetView [ MAX_RENDER_TARGETS ] ;
private Dx11 . DepthStencilView [ ] depthStencilView = new DepthStencilView [ MAX_RENDER_TARGETS ] ;
private RenderTarget2D_DX11 backBuffer ;
2012-09-15 13:43:31 +00:00
internal EffectDX currentEffect ;
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 ;
2015-09-30 21:31:15 +02:00
#if DEBUG
var flags = DeviceCreationFlags . Debug ;
#else
var flags = DeviceCreationFlags . None ;
#endif
// http://msdn.microsoft.com/en-us/library/windows/desktop/bb205068(v=vs.85).aspx
Device . CreateWithSwapChain ( DriverType . Hardware , flags , desc , out dxDevice , out swapChain ) ;
#if DEBUG
nativeDevice . DebugName = "GraphicsDevice_" + graphicsDeviceCount + + ;
swapChain . DebugName = "SwapChain_" + swapChainCount + + ;
#endif
2012-09-08 10:37:41 +00:00
nativeDevice = dxDevice . ImmediateContext ;
}
#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 )
2011-11-16 14:27:53 +00:00
{
2015-09-30 21:31:15 +02:00
backBuffer = new RenderTarget2D_DX11 ( this , Dx11 . Texture2D . FromSwapChain < Dx11 . Texture2D > ( swapChain , 0 ) , presentationParameters . DepthStencilFormat ) ;
this . SetRenderTargets ( ) ;
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
2015-09-30 21:31:15 +02:00
public void Clear ( ClearOptions options , Vector4 color , float depth , int stencil )
2011-11-16 14:27:53 +00:00
{
2015-09-30 21:31:15 +02:00
if ( ( options & ClearOptions . Target ) = = ClearOptions . Target )
2012-08-10 08:38:01 +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 ) ;
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
}
}
2015-09-30 21:31:15 +02:00
Dx11 . 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 = Dx11 . DepthStencilClearFlags . Depth | Dx11 . DepthStencilClearFlags . Stencil ;
}
else if ( ( options | ClearOptions . Stencil ) = = options )
{
clearFlags = Dx11 . DepthStencilClearFlags . Stencil ;
}
else
{
clearFlags = Dx11 . DepthStencilClearFlags . Depth ;
2011-11-16 14:27:53 +00:00
}
2011-12-14 11:49:04 +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
}
}
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-29 09:42:27 +00:00
Dx11 . EffectTechnique technique = SetupEffectForDraw ( ) ;
2012-09-20 11:53:50 +00:00
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 ) ;
2012-09-29 09:42:27 +00:00
//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 )
{
2012-09-29 09:42:27 +00:00
Dx11 . EffectPass pass ; Dx11 . EffectTechnique technique ; ShaderBytecode passSignature ;
2012-09-08 09:07:23 +00:00
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-29 09:42:27 +00:00
//nativeDevice.Rasterizer.SetViewports(currentViewport);
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-29 09:42:27 +00:00
Dx11 . EffectTechnique technique = SetupEffectForDraw ( ) ;
2012-09-22 11:17:36 +00:00
int vertexCount = DxFormatConverter . CalculateVertexCount ( primitiveType , primitiveCount ) ;
nativeDevice . InputAssembler . PrimitiveTopology = DxFormatConverter . Translate ( primitiveType ) ;
2012-09-29 09:42:27 +00:00
//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 ;
2015-09-30 21:31:15 +02:00
DxVertexBuffer vb11 = new DxVertexBuffer ( this , vertexDeclaration , vertexCount , BufferUsage . None ) ;
vb11 . SetData < T > ( vertexData ) ;
2012-01-09 18:09:10 +00:00
2012-09-29 09:42:27 +00:00
Dx11 . VertexBufferBinding nativeVertexBufferBindings = new Dx11 . VertexBufferBinding ( vb11 . NativeBuffer , vertexDeclaration . VertexStride , 0 ) ;
2012-01-09 18:09:10 +00:00
2012-09-08 10:37:41 +00:00
nativeDevice . InputAssembler . SetVertexBuffers ( 0 , nativeVertexBufferBindings ) ;
2012-01-09 18:09:10 +00:00
2012-09-29 09:42:27 +00:00
Dx11 . EffectPass pass ; Dx11 . EffectTechnique technique ; ShaderBytecode passSignature ;
2012-08-10 08:38:01 +00:00
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-29 09:42:27 +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
2012-09-29 09:42:27 +00:00
private void SetupEffectForDraw ( out Dx11 . EffectPass pass , out Dx11 . EffectTechnique technique ,
2012-09-08 10:37:41 +00:00
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
2012-09-29 09:42:27 +00:00
private Dx11 . EffectTechnique SetupEffectForDraw ( )
2012-09-20 11:53:50 +00: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 ) ;
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-29 09:42:27 +00:00
var nativeVertexBufferBindings = new Dx11 . 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 ;
2012-09-29 09:42:27 +00:00
nativeVertexBufferBindings [ i ] = new Dx11 . 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
{
two commits were missing, both by KorsarNek:
"Removed the SupportedPlatformsImpl classes and replaced them with a new SupportedPlatforms attribute on the assembly level.
Removed a few class constructors which could cause problems when loading a game.
Made ResetElapsedTime in the game class reset to 0 instead of TimeSpan.MinValue.
Removed the restriction in the InputDeviceFactory for which InputDevices are supported.
Added a Logger for Metro which works with the current Logger implementation.
Changed that when a platform is recognized that is higher than Windows 8, it gets treated like Windows 8, not like Windows 7.
Due to the SupportedPlatforms change, the assembly loader is now faster in finding out which assemblies contains addIns. For not Metro system, it's also added that a warning gets written if an AddIn references a different ANX version than that of the running assembly.
OpenGL and DirectX have been updated to the newest versions.
XAudio system uses now the same SharpDX version as all the other systems.
ParameterBuffer for WindowsMetro gets now correctly created by considering the size constraints for constant buffers.
Fixed an erroneous finalizer in the xaudio system.
Made the metro projects convert to Windows 8.1, as Windows 8.0 is not supported by the newer SharpDX versions. It's now also necessary to use at least Visual Studio 2013 to build the Metro versions.
Made the samples work again on Windows."
"Fixed the creation of the swap chain for windows metro and removed the dependency of the Metro Rendersystem onto the Metro Platformsytem.
All occurrences of WindowHandles have been replaced with a custom WindowHandle type which should work out of the box in most cases, but does still represent a breaking change to XNA.
The ProjectConverter for Metro was adjusted so that with just changing the way the application is initialized, most projects that worked with ANX before should now work under win rt. The sample SimpleNoContent does now work out of the box for win rt, after a project conversion.
The application name for win rt apps is now a guid, the display name stayed the same though. That's to be more compliant with the way win rt apps are normally created.
The default namespace and namespace of the classes for the Sample "SimpleNoContent" is renamed from "SimpleModernUI" to "SimpleNoContent".
With the new way win rt apps are initialized for ANX, it's necessary to first create the WindowsGameHost for WinRT with a handler how to create the game instance and give that to the CoreApplication object to run it.
Also took care of a few annoying bugs when working with win rt and ANX where no InputDevices could be created on the first frame (Issue #1164 ) and that it wasn't possible to use the localfolder of the application on the first update and all the other stuff for which an instance of the Application class was necessary."
2015-03-29 13:48:33 +02:00
nativeDevice . Rasterizer . SetViewport ( new SharpDX . Viewport ( x , y , width , height , minDepth , maxDepth ) ) ;
2012-09-08 10:37:41 +00:00
}
2012-09-29 09:42:27 +00:00
2013-07-23 10:58:07 +00:00
protected void SetViewport ( params SharpDX . ViewportF [ ] viewports )
2012-09-29 09:42:27 +00:00
{
nativeDevice . Rasterizer . SetViewports ( viewports ) ;
}
#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
2015-09-30 21:31:15 +02:00
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-16 14:27:53 +00:00
{
2015-09-30 21:31:15 +02:00
this . renderTargetView [ 0 ] = this . backBuffer . RenderTargetView ;
this . depthStencilView [ 0 ] = this . backBuffer . DepthStencilView ;
2012-09-29 09:42:27 +00:00
2015-09-30 21:31:15 +02:00
for ( int i = 1 ; i < MAX_RENDER_TARGETS ; i + + )
2011-12-14 11:49:04 +00:00
{
2015-09-30 21:31:15 +02:00
this . renderTargetView [ i ] = null ;
this . depthStencilView [ i ] = null ;
2011-12-14 11:49:04 +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 ( this . backBuffer . DepthStencilView , this . renderTargetView ) ;
nativeDevice . Rasterizer . SetViewport ( new SharpDX . ViewportF ( 0 , 0 , this . backBuffer . Width , this . backBuffer . Height ) ) ;
2011-11-16 14:27:53 +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 ) ) ;
var rtViewports = new SharpDX . ViewportF [ 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-12-14 11:49:04 +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-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 ;
2015-09-30 21:31:15 +02:00
var nativeRenderTarget = renderTarget . NativeRenderTarget as RenderTarget2D_DX11 ;
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 . ViewportF ( 0 , 0 , renderTarget . Width , renderTarget . Height ) ;
2011-12-14 11:49:04 +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 ( this . depthStencilView [ 0 ] , renderTargetView ) ;
nativeDevice . Rasterizer . SetViewports ( rtViewports ) ;
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
2015-09-30 21:31:15 +02:00
protected void DisposeBackBuffer ( )
2011-11-22 14:51:30 +00:00
{
2015-09-30 21:31:15 +02:00
if ( backBuffer ! = null )
2011-12-14 11:49:04 +00:00
{
2015-09-30 21:31:15 +02:00
backBuffer . Dispose ( ) ;
backBuffer = null ;
2011-12-14 11:49:04 +00:00
}
2015-09-30 21:31:15 +02:00
}
2011-12-14 11:49:04 +00:00
2015-09-30 21:31:15 +02:00
#region Dispose
public void Dispose ( )
{
2011-12-14 11:49:04 +00:00
if ( swapChain ! = null )
{
2015-09-30 21:31:15 +02:00
DisposeBackBuffer ( ) ;
2011-12-14 11:49:04 +00:00
swapChain . Dispose ( ) ;
swapChain = 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 ;
}
}
2012-10-13 14:55:06 +00:00
public Rectangle ScissorRectangle
{
get { throw new NotImplementedException ( ) ; }
set { throw new NotImplementedException ( ) ; }
}
2011-11-22 14:51:30 +00:00
}
2011-11-16 14:27:53 +00:00
}