24 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_DIRECT3D 
   26 #if SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP 
   27 using System.Windows.Forms;
 
   32 namespace SiliconStudio.
Paradox.Graphics
 
   37     public class SwapChainGraphicsPresenter : GraphicsPresenter
 
   41         private SwapChain swapChain;
 
   43         private int bufferCount;
 
   45         public SwapChainGraphicsPresenter(GraphicsDevice device, PresentationParameters presentationParameters)
 
   46             : base(device, presentationParameters)
 
   51             swapChain = CreateSwapChain();
 
   53             var backBufferTexture = 
new Texture2D(device, swapChain.GetBackBuffer<SharpDX.Direct3D11.Texture2D>(0));
 
   54             backBuffer = backBufferTexture.ToRenderTarget();
 
   68         public override object NativePresenter
 
   76         public override bool IsFullScreen
 
   80 #if SILICONSTUDIO_PLATFORM_WINDOWS_RUNTIME 
   83                 return swapChain.IsFullScreen;
 
   89 #if !SILICONSTUDIO_PLATFORM_WINDOWS_RUNTIME 
   90                 if (swapChain == null)
 
   93                 var outputIndex = Description.PreferredFullScreenOutputIndex;
 
   96                 var output = GraphicsDevice.Adapter != null && outputIndex < GraphicsDevice.Adapter.Outputs.Length ? GraphicsDevice.Adapter.Outputs[outputIndex] : null;
 
   98                 Output currentOutput = null;
 
  102                     Bool isCurrentlyFullscreen;
 
  103                     swapChain.GetFullscreenState(out isCurrentlyFullscreen, out currentOutput);
 
  106                     if (isCurrentlyFullscreen == value && output != null && currentOutput != null && currentOutput.NativePointer == output.NativeOutput.NativePointer)
 
  111                     if (currentOutput != null)
 
  112                         currentOutput.Dispose();
 
  115                 bool switchToFullScreen = value;
 
  117                 var description = 
new ModeDescription(backBuffer.Width, backBuffer.Height, Description.RefreshRate.ToSharpDX(), (SharpDX.DXGI.Format)Description.BackBufferFormat);
 
  118                 if (switchToFullScreen)
 
  122                     backBuffer.OnDestroyed();
 
  126                     Description.IsFullScreen = 
true;
 
  131                     backBuffer.OnRecreate();
 
  135                     Description.IsFullScreen = 
false;
 
  136                     swapChain.IsFullScreen = 
false;
 
  139                     Resize(backBuffer.Width, backBuffer.Height, backBuffer.Texture.ViewFormat);
 
  143                 if (!switchToFullScreen)
 
  146                     description.RefreshRate = 
new SharpDX.DXGI.Rational(0, 0);
 
  147                     swapChain.ResizeTarget(ref description);
 
  153         public override void Present()
 
  159             catch (SharpDXException sharpDxException)
 
  161                 throw new GraphicsException(
"Unexpected error on Present", sharpDxException, GraphicsDevice.GraphicsDeviceStatus);
 
  165         protected override void OnNameChanged()
 
  167             base.OnNameChanged();
 
  168             if (Name != null && GraphicsDevice != null && GraphicsDevice.IsDebugMode && swapChain != null)
 
  170                 swapChain.DebugName = Name;
 
  174         public override void OnDestroyed()
 
  177             backBuffer.Texture.OnDestroyed();
 
  178             backBuffer.Texture.LifetimeState = GraphicsResourceLifetimeState.Destroyed;
 
  186         public override void OnRecreated()
 
  191             swapChain = CreateSwapChain();
 
  194             var backBufferTexture = swapChain.GetBackBuffer<SharpDX.Direct3D11.Texture2D>(0);
 
  198             ((
Texture2D)backBuffer.Texture).Recreate(backBufferTexture);
 
  199             backBuffer.Texture.LifetimeState = GraphicsResourceLifetimeState.Active;
 
  202         protected override void ResizeBackBuffer(
int width, 
int height, 
PixelFormat format)
 
  205             backBuffer.Texture.OnDestroyed();
 
  209             backBuffer.OnDestroyed();
 
  211 #if SILICONSTUDIO_PLATFORM_WINDOWS_RUNTIME 
  212             var swapChainPanel = Description.DeviceWindowHandle.NativeHandle as Windows.UI.Xaml.Controls.SwapChainPanel;
 
  213             if (swapChainPanel != null)
 
  215                 var swapChain2 = swapChain.QueryInterface<SwapChain2>();
 
  216                 if (swapChain2 != null)
 
  218                     swapChain2.MatrixTransform = Matrix3x2.Scaling(1f / swapChainPanel.CompositionScaleX, 1f / swapChainPanel.CompositionScaleY);
 
  219                     swapChain2.Dispose();
 
  224             swapChain.ResizeBuffers(bufferCount, width, height, (SharpDX.DXGI.Format)format, SwapChainFlags.None);
 
  227             var backBufferTexture = swapChain.GetBackBuffer<SharpDX.Direct3D11.Texture2D>(0);
 
  230             ((
Texture2D)backBuffer.Texture).Recreate(backBufferTexture);
 
  233             backBuffer.OnRecreate();
 
  237             backBuffer.Texture.Width = width;
 
  238             backBuffer.Texture.Height = height;
 
  239             backBuffer.Width = width;
 
  240             backBuffer.Height = height;
 
  243         protected override void ResizeDepthStencilBuffer(
int width, 
int height, 
PixelFormat format)
 
  245             var newTextureDescrition = DepthStencilBuffer.Texture.NativeDescription;
 
  246             newTextureDescrition.Width = width;
 
  247             newTextureDescrition.Height = height;
 
  250             DepthStencilBuffer.Texture.OnDestroyed();
 
  254             DepthStencilBuffer.OnDestroyed();
 
  257             DepthStencilBuffer.Texture.Recreate(newTextureDescrition);
 
  260             DepthStencilBuffer.OnRecreate();
 
  264             DepthStencilBuffer.Texture.Width = width;
 
  265             DepthStencilBuffer.Texture.Height = height;
 
  266             DepthStencilBuffer.DescriptionInternal.Width = width;
 
  267             DepthStencilBuffer.DescriptionInternal.Height = height;
 
  271         private SwapChain CreateSwapChain()
 
  274             if (Description.DeviceWindowHandle == null)
 
  276                 throw new ArgumentException(
"DeviceWindowHandle cannot be null");
 
  279 #if SILICONSTUDIO_PLATFORM_WINDOWS_RUNTIME 
  280             return CreateSwapChainForWindowsRuntime();
 
  282             return CreateSwapChainForDesktop();
 
  286 #if SILICONSTUDIO_PLATFORM_WINDOWS_RUNTIME 
  287         private SwapChain CreateSwapChainForWindowsRuntime()
 
  290             var description = 
new SwapChainDescription1
 
  293                 Width = Description.BackBufferWidth,
 
  294                 Height = Description.BackBufferHeight,
 
  295                 Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, 
 
  297                 SampleDescription = 
new SharpDX.DXGI.SampleDescription((int)Description.MultiSampleCount, 0),
 
  298                 Usage = Usage.BackBuffer | Usage.RenderTargetOutput,
 
  300                 BufferCount = bufferCount,
 
  301                 Scaling = SharpDX.DXGI.Scaling.Stretch,
 
  302                 SwapEffect = SharpDX.DXGI.SwapEffect.FlipSequential,
 
  305             SwapChain swapChain = null;
 
  306             switch (Description.DeviceWindowHandle.Context)
 
  308                 case Games.AppContextType.WindowsRuntime:
 
  310                     var nativePanel = ComObject.As<ISwapChainPanelNative>(Description.DeviceWindowHandle.NativeHandle);
 
  312                     swapChain = 
new SwapChain1(GraphicsAdapterFactory.NativeFactory, GraphicsDevice.NativeDevice, ref description);
 
  315                     nativePanel.SwapChain = swapChain;
 
  319                     throw new NotSupportedException(
string.Format(
"Window context [{0}] not supported while creating SwapChain", Description.DeviceWindowHandle.Context));
 
  325         private SwapChain CreateSwapChainForDesktop()
 
  327             var control = Description.DeviceWindowHandle.NativeHandle as 
Control;
 
  330                 throw new NotSupportedException(
string.Format(
"Form of type [{0}] is not supported. Only System.Windows.Control are supported", Description.DeviceWindowHandle != null ? Description.DeviceWindowHandle.GetType().Name : 
"null"));
 
  334             var description = 
new SwapChainDescription
 
  336                     ModeDescription = 
new ModeDescription(Description.BackBufferWidth, Description.BackBufferHeight, Description.RefreshRate.ToSharpDX(), (SharpDX.DXGI.Format)Description.BackBufferFormat), 
 
  337                     BufferCount = bufferCount, 
 
  338                     OutputHandle = control.Handle, 
 
  339                     SampleDescription = 
new SampleDescription((
int)Description.MultiSampleCount, 0), 
 
  340                     SwapEffect = SwapEffect.Discard,
 
  341                     Usage = SharpDX.DXGI.Usage.BackBuffer | SharpDX.DXGI.Usage.RenderTargetOutput,
 
  343                     Flags = Description.IsFullScreen ? SwapChainFlags.AllowModeSwitch : SwapChainFlags.None, 
 
  346             var newSwapChain = 
new SwapChain(GraphicsAdapterFactory.NativeFactory, GraphicsDevice.NativeDevice, description);
 
  347             if (Description.IsFullScreen)
 
  350                 newSwapChain.ResizeTarget(ref description.ModeDescription);
 
  353                 newSwapChain.IsFullScreen = 
true;
 
  356                 newSwapChain.ResizeBuffers(bufferCount, Description.BackBufferWidth, Description.BackBufferHeight, (SharpDX.DXGI.Format)Description.BackBufferFormat, SwapChainFlags.AllowModeSwitch);
 
HRESULT Resize(_In_ const Image &srcImage, _In_ size_t width, _In_ size_t height, _In_ DWORD filter, _Out_ ScratchImage &image)
Flags
Enumeration of the new Assimp's flags. 
PresentInterval
Defines flags that describe the relationship between the adapter refresh rate and the rate at which P...
Creates a render target buffer. 
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
A 2-channels stereo sounds. 
PixelFormat
Defines various types of pixel formats. 
The texture dimension is 2D.