void IRenderLoopHost.OnRenderLoop_PrepareRendering(EngineDevice engineDevice)
 {
     if ((m_targetPanel != null) &&
         (m_renderLoop != null) &&
         (m_renderLoop.Camera != null) &&
         (m_swapChain != null))
     {
         // Update swap chain scaling (only relevant for SwapChainPanel targets)
         //  see https://www.packtpub.com/books/content/integrating-direct3d-xaml-and-windows-81
         if (m_compositionScaleChanged &&
             m_targetPanel.CompositionRescalingNeeded)
         {
             m_compositionScaleChanged = false;
             DXGI.SwapChain2 swapChain2 = m_swapChain.QueryInterfaceOrNull <DXGI.SwapChain2>();
             if (swapChain2 != null)
             {
                 try
                 {
                     SharpDX.Matrix3x2 inverseScale = new SharpDX.Matrix3x2();
                     inverseScale.M11           = 1.0f / (float)m_targetPanel.CompositionScaleX;
                     inverseScale.M22           = 1.0f / (float)m_targetPanel.CompositionScaleY;
                     swapChain2.MatrixTransform = inverseScale;
                 }
                 finally
                 {
                     swapChain2.Dispose();
                 }
             }
         }
     }
 }
        public SwapChainGraphicsPresenter(GraphicsDevice device, PresentationParameters presentationParameters)
            : base(device, presentationParameters)
        {
            PresentInterval = presentationParameters.PresentationInterval;

            // Initialize the swap chain
            swapChain = ToDispose(CreateSwapChain());

#if DIRECTX11_2
            swapChain2 = ToDispose(new SwapChain2(swapChain.NativePointer));
#endif

            backBuffer = ToDispose(RenderTarget2D.New(device, swapChain.GetBackBuffer<Direct3D11.Texture2D>(0)));
        }
Exemplo n.º 3
0
        private void InitializeD3D()
        {
            using (D3D11.Device defaultDevice = new D3D11.Device(D3D.DriverType.Hardware, D3D11.DeviceCreationFlags.Debug))
                this.device = defaultDevice.QueryInterface <D3D11.Device2>();
            this.deviceContext = this.device.ImmediateContext2;

            DXGI.SwapChainDescription1 swapChainDescription = new DXGI.SwapChainDescription1()
            {
                AlphaMode         = DXGI.AlphaMode.Ignore,
                BufferCount       = 2,
                Format            = DXGI.Format.R8G8B8A8_UNorm,
                Height            = (int)(this.swapChainPanel.RenderSize.Height),
                Width             = (int)(this.swapChainPanel.RenderSize.Width),
                SampleDescription = new DXGI.SampleDescription(1, 0),
                Scaling           = SharpDX.DXGI.Scaling.Stretch,
                Stereo            = false,
                SwapEffect        = DXGI.SwapEffect.FlipSequential,
                Usage             = DXGI.Usage.RenderTargetOutput
            };

            using (DXGI.Device3 dxgiDevice3 = this.device.QueryInterface <DXGI.Device3>())
                using (DXGI.Factory3 dxgiFactory3 = dxgiDevice3.Adapter.GetParent <DXGI.Factory3>())
                {
                    DXGI.SwapChain1 swapChain1 = new DXGI.SwapChain1(dxgiFactory3, this.device, ref swapChainDescription);
                    this.swapChain = swapChain1.QueryInterface <DXGI.SwapChain2>();
                }

            using (DXGI.ISwapChainPanelNative nativeObject = ComObject.As <DXGI.ISwapChainPanelNative>(this.swapChainPanel))
                nativeObject.SwapChain = this.swapChain;

            this.backBufferTexture = this.swapChain.GetBackBuffer <D3D11.Texture2D>(0);
            this.backBufferView    = new D3D11.RenderTargetView(this.device, this.backBufferTexture);

            deviceContext.Rasterizer.SetViewport(0, 0, (int)swapChainPanel.ActualWidth, (int)swapChainPanel.ActualHeight);

            CompositionTarget.Rendering    += CompositionTarget_Rendering;
            Application.Current.Suspending += Current_Suspending;

            isDXInitialized = true;
        }
Exemplo n.º 4
0
        private void SwapChainPanel_Loaded(object sender, RoutedEventArgs e)
        {
            var v = ApplicationView.GetForCurrentView();

            v.FullScreenSystemOverlayMode = FullScreenSystemOverlayMode.Minimal;
            v.TryEnterFullScreenMode();
#if DEBUG
            var debugLevel = D3D11.DeviceCreationFlags.Debug | D3D11.DeviceCreationFlags.BgraSupport;
#else
            var debugLevel = D3D11.DeviceCreationFlags.None | D3D11.DeviceCreationFlags.BgraSupport;
#endif
            using (D3D11.Device defaultDevice = new D3D11.Device(D3D.DriverType.Hardware, debugLevel))
            {
                this.device = defaultDevice.QueryInterface <D3D11.Device2>();
            }
            this.deviceContext = this.device.ImmediateContext2;
            float pixelScale = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi / 96.0f;

            DXGI.SwapChainDescription1 swapChainDescription = new DXGI.SwapChainDescription1()
            {
                AlphaMode         = DXGI.AlphaMode.Premultiplied,
                BufferCount       = 2,
                Format            = DXGI.Format.B8G8R8A8_UNorm,
                Height            = (int)(this.SwapChainPanel.RenderSize.Height * pixelScale),
                Width             = (int)(this.SwapChainPanel.RenderSize.Width * pixelScale),
                SampleDescription = new DXGI.SampleDescription(1, 0),
                Scaling           = DXGI.Scaling.Stretch,
                Stereo            = false,
                SwapEffect        = DXGI.SwapEffect.FlipSequential,
                Usage             = DXGI.Usage.BackBuffer | DXGI.Usage.RenderTargetOutput,
            };
            using (DXGI.Device3 dxgiDevice3 = this.device.QueryInterface <DXGI.Device3>())
            {
                using (DXGI.Factory3 dxgiFactory3 = dxgiDevice3.Adapter.GetParent <DXGI.Factory3>())
                {
                    using (DXGI.SwapChain1 swapChain1 = new DXGI.SwapChain1(dxgiFactory3, this.device, ref swapChainDescription))
                    {
                        this.swapChain = swapChain1.QueryInterface <DXGI.SwapChain2>();
                    }
                }
            }
            using (DXGI.ISwapChainPanelNative nativeObject = ComObject.As <DXGI.ISwapChainPanelNative>(this.SwapChainPanel))
            {
                nativeObject.SwapChain = this.swapChain;
            }
            this.backBufferTexture = D3D11.Texture2D.FromSwapChain <D3D11.Texture2D>(this.swapChain, 0);
            this.backBufferView    = new D3D11.RenderTargetView(this.device, this.backBufferTexture);
            isDXInitialized        = true;
            tw = new TextWirter(device, swapChain, Color.Black, DisplayInformation.GetForCurrentView().LogicalDpi);
//            #region D2D

//#if DEBUG
//            var debug = SharpDX.Direct2D1.DebugLevel.Error;
//#else
//            var debug = SharpDX.Direct2D1.DebugLevel.None;
//#endif

//            d2dFactory = new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.SingleThreaded, debug);
//            using (var dxgiDevice = device.QueryInterface<SharpDX.DXGI.Device>())
//            {
//                d2dDevice = new SharpDX.Direct2D1.Device(d2dFactory, dxgiDevice);
//            }
//            d2dContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None);

//            BitmapProperties1 properties = new BitmapProperties1(
//                new PixelFormat(
//                    SharpDX.DXGI.Format.B8G8R8A8_UNorm,
//                    SharpDX.Direct2D1.AlphaMode.Premultiplied),
//                DisplayInformation.GetForCurrentView().LogicalDpi,
//                DisplayInformation.GetForCurrentView().LogicalDpi,
//                BitmapOptions.Target | BitmapOptions.CannotDraw);
//            DXGI.Surface backBuffer = swapChain.GetBackBuffer<DXGI.Surface>(0);
//            d2dTarget = new Bitmap1(d2dContext, backBuffer, properties);
//            SharpDX.DirectWrite.Factory fontFactory = new SharpDX.DirectWrite.Factory();
//            textFormat = new TextFormat(fontFactory, "Segoe UI", 24.0f);
//            textLayout1 = new TextLayout(fontFactory, "This is an example of a moving TextLayout object with snapped pixel boundaries.", textFormat, 400.0f, 200.0f);
//            textLayout2 = new TextLayout(fontFactory, "This is an example of a moving TextLayout object with no snapped pixel boundaries.", textFormat, 400.0f, 200.0f);
//            layoutY = 0.0f;
//            backgroundBrush = new SharpDX.Direct2D1.SolidColorBrush(d2dContext, Color.White);
//            textBrush = new SharpDX.Direct2D1.SolidColorBrush(d2dContext, Color.Black);
//            #endregion

            CompositionTarget.Rendering    += CompositionTarget_Rendering;
            Application.Current.Suspending += Application_Suspending;
        }
Exemplo n.º 5
0
        private void SwapChainPanel_Loaded(object sender, RoutedEventArgs e)
        {
            // Create a new Direct3D hardware device and ask for Direct3D 11.2 support
            using (D3D11.Device defaultDevice = new D3D11.Device(D3D.DriverType.Hardware, D3D11.DeviceCreationFlags.Debug))
            {
                this.device = defaultDevice.QueryInterface <D3D11.Device2>();
            }

            // Save the context instance
            this.deviceContext = this.device.ImmediateContext2;

            Size2 sizeInPixels = RenderSizeToPixelSize(this.SwapChainPanel.RenderSize);

            // Properties of the swap chain
            DXGI.SwapChainDescription1 swapChainDescription = new DXGI.SwapChainDescription1()
            {
                // No transparency.
                AlphaMode = DXGI.AlphaMode.Ignore,
                // Double buffer.
                BufferCount = 2,
                // BGRA 32bit pixel format.
                Format = DXGI.Format.B8G8R8A8_UNorm,
                // Unlike in CoreWindow swap chains, the dimensions must be set.
                Height = sizeInPixels.Height,
                Width  = sizeInPixels.Width,
                // Default multisampling.
                SampleDescription = new DXGI.SampleDescription(1, 0),
                // In case the control is resized, stretch the swap chain accordingly.
                Scaling = DXGI.Scaling.Stretch,
                // No support for stereo display.
                Stereo = false,
                // Sequential displaying for double buffering.
                SwapEffect = DXGI.SwapEffect.FlipSequential,
                // This swapchain is going to be used as the back buffer.
                Usage = DXGI.Usage.BackBuffer | DXGI.Usage.RenderTargetOutput,
            };

            // Retrive the DXGI device associated to the Direct3D device.
            using (DXGI.Device3 dxgiDevice3 = this.device.QueryInterface <DXGI.Device3>())
            {
                // Get the DXGI factory automatically created when initializing the Direct3D device.
                using (DXGI.Factory3 dxgiFactory3 = dxgiDevice3.Adapter.GetParent <DXGI.Factory3>())
                {
                    // Create the swap chain and get the highest version available.
                    using (DXGI.SwapChain1 swapChain1 = new DXGI.SwapChain1(dxgiFactory3, this.device, ref swapChainDescription))
                    {
                        this.swapChain = swapChain1.QueryInterface <DXGI.SwapChain2>();
                    }
                }
            }

            // Obtain a reference to the native COM object of the SwapChainPanel.
            using (DXGI.ISwapChainPanelNative nativeObject = ComObject.As <DXGI.ISwapChainPanelNative>(this.SwapChainPanel))
            {
                // Set its swap chain.
                nativeObject.SwapChain = this.swapChain;
            }

            // Create a Texture2D from the existing swap chain to use as
            this.backBufferTexture = D3D11.Texture2D.FromSwapChain <D3D11.Texture2D>(this.swapChain, 0);
            this.backBufferView    = new D3D11.RenderTargetView(this.device, this.backBufferTexture);

            // This event is fired when the application requests a new frame from the DirectX interop controls.
            CompositionTarget.Rendering += CompositionTarget_Rendering;

            // Subscribe to the suspending event
            Application.Current.Suspending += Application_Suspending;

            // Mark our resources as initialized
            isDXInitialized = true;
        }
Exemplo n.º 6
0
        protected virtual void CreateSizeDependentResources(TargetBase renderBase)
        {
            var d3dDevice  = DeviceManager.DeviceDirect3D;
            var d3dContext = DeviceManager.ContextDirect3D;
            var d2dContext = DeviceManager.ContextDirect2D;

            d2dContext.Target = null;
            RemoveAndDispose(ref renderTargetView);
            RemoveAndDispose(ref depthStencilView);
            RemoveAndDispose(ref bitmapTarget);
            RemoveAndDispose(ref backBuffer);

            // If the swap chain already exists, resize it.
            if (swapChain != null)
            {
                swapChain.ResizeBuffers(2, Width, Height, SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.DXGI.SwapChainFlags.None);
            }
            // Otherwise, create a new one.
            else
            {
                // SwapChain description
                var desc = CreateSwapChainDescription();

                // Once the desired swap chain description is configured, it must be created on the same adapter as our D3D Device

                // First, retrieve the underlying DXGI Device from the D3D Device.
                // Creates the swap chain
                using (var dxgiDevice2 = d3dDevice.QueryInterface <SharpDX.DXGI.Device2>())
                    using (var dxgiAdapter = dxgiDevice2.Adapter)
                        using (var dxgiFactory2 = dxgiAdapter.GetParent <SharpDX.DXGI.Factory2>())
                        {
                            swapChain = ToDispose(CreateSwapChain(dxgiFactory2, d3dDevice, desc));

                            // Ensure that DXGI does not queue more than one frame at a time. This both reduces
                            // latency and ensures that the application will only render after each VSync, minimizing
                            // power consumption.
                            dxgiDevice2.MaximumFrameLatency = 1;
                        }
            }

            // Obtain the backbuffer for this window which will be the final 3D rendertarget.
            backBuffer = ToDispose(SharpDX.Direct3D11.Texture2D.FromSwapChain <SharpDX.Direct3D11.Texture2D>(swapChain, 0));
            {
                // Create a view interface on the rendertarget to use on bind.
                renderTargetView = ToDispose(new SharpDX.Direct3D11.RenderTargetView(d3dDevice, BackBuffer));

                // Cache the rendertarget dimensions in our helper class for convenient use.
                var backBufferDesc = BackBuffer.Description;
                RenderTargetBounds = new Windows.Foundation.Rect(0, 0, backBufferDesc.Width, backBufferDesc.Height);
            }

            // Create a descriptor for the depth/stencil buffer.
            // Allocate a 2-D surface as the depth/stencil buffer.
            // Create a DepthStencil view on this surface to use on bind.
            using (var depthBuffer = new SharpDX.Direct3D11.Texture2D(d3dDevice, new SharpDX.Direct3D11.Texture2DDescription()
            {
                Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt,
                ArraySize = 1,
                MipLevels = 1,
                Width = (int)RenderTargetSize.Width,
                Height = (int)RenderTargetSize.Height,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                BindFlags = SharpDX.Direct3D11.BindFlags.DepthStencil,
            }))
                depthStencilView = ToDispose(new SharpDX.Direct3D11.DepthStencilView(d3dDevice, depthBuffer, new SharpDX.Direct3D11.DepthStencilViewDescription()
                {
                    Dimension = SharpDX.Direct3D11.DepthStencilViewDimension.Texture2D
                }));

            // Create a viewport descriptor of the full window size.
            var viewport = new SharpDX.ViewportF((float)RenderTargetBounds.X, (float)RenderTargetBounds.Y, (float)RenderTargetBounds.Width, (float)RenderTargetBounds.Height, 0.0f, 1.0f);

            // Set the current viewport using the descriptor.
            d3dContext.Rasterizer.SetViewport(viewport);

            // Now we set up the Direct2D render target bitmap linked to the swapchain.
            // Whenever we render to this bitmap, it will be directly rendered to the
            // swapchain associated with the window.
            var bitmapProperties = new SharpDX.Direct2D1.BitmapProperties1(
                new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied),
                DeviceManager.Dpi,
                DeviceManager.Dpi,
                SharpDX.Direct2D1.BitmapOptions.Target | SharpDX.Direct2D1.BitmapOptions.CannotDraw);

            // Direct2D needs the dxgi version of the backbuffer surface pointer.
            // Get a D2D surface from the DXGI back buffer to use as the D2D render target.
            using (var dxgiBackBuffer = swapChain.GetBackBuffer <SharpDX.DXGI.Surface>(0))
                bitmapTarget = ToDispose(new SharpDX.Direct2D1.Bitmap1(d2dContext, dxgiBackBuffer, bitmapProperties));

            // So now we can set the Direct2D render target.
            d2dContext.Target = BitmapTarget2D;

            // Set D2D text anti-alias mode to Grayscale to ensure proper rendering of text on intermediate surfaces.
            d2dContext.TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Grayscale;
        }
        private void SwapChainPanel_Loaded(object sender, RoutedEventArgs e)
        {
            // Create a new Direct3D hardware device and ask for Direct3D 11.2 support
            using (D3D11.Device defaultDevice = new D3D11.Device(D3D.DriverType.Hardware, D3D11.DeviceCreationFlags.Debug))
            {
                this.device = defaultDevice.QueryInterface<D3D11.Device2>();
            }

            // Save the context instance
            this.deviceContext = this.device.ImmediateContext2;

            // We have to take into account pixel scaling; Windows Phone 8.1 uses virtual resolutions smaller than the physical screen size.
            float pixelScale = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi / 96.0f;

            // Properties of the swap chain
            DXGI.SwapChainDescription1 swapChainDescription = new DXGI.SwapChainDescription1()
            {
                // No transparency.
                AlphaMode = DXGI.AlphaMode.Ignore,
                // Double buffer.
                BufferCount = 2,
                // BGRA 32bit pixel format.
                Format = DXGI.Format.B8G8R8A8_UNorm,
                // Unlike in CoreWindow swap chains, the dimensions must be set.
                Height = (int)(this.SwapChainPanel.RenderSize.Height * pixelScale),
                Width = (int)(this.SwapChainPanel.RenderSize.Width * pixelScale),
                // Default multisampling.
                SampleDescription = new DXGI.SampleDescription(1, 0),
                // In case the control is resized, stretch the swap chain accordingly.
                Scaling = DXGI.Scaling.Stretch,
                // No support for stereo display.
                Stereo = false,
                // Sequential displaying for double buffering.
                SwapEffect = DXGI.SwapEffect.FlipSequential,
                // This swapchain is going to be used as the back buffer.
                Usage = DXGI.Usage.BackBuffer | DXGI.Usage.RenderTargetOutput,
            };

            // Retrive the DXGI device associated to the Direct3D device.
            using (DXGI.Device3 dxgiDevice3 = this.device.QueryInterface<DXGI.Device3>())
            {
                // Get the DXGI factory automatically created when initializing the Direct3D device.
                using (DXGI.Factory3 dxgiFactory3 = dxgiDevice3.Adapter.GetParent<DXGI.Factory3>())
                {
                    // Create the swap chain and get the highest version available.
                    using (DXGI.SwapChain1 swapChain1 = new DXGI.SwapChain1(dxgiFactory3, this.device, ref swapChainDescription))
                    {
                        this.swapChain = swapChain1.QueryInterface<DXGI.SwapChain2>();
                    }
                }
            }

            // Obtain a reference to the native COM object of the SwapChainPanel.
            using (DXGI.ISwapChainPanelNative nativeObject = ComObject.As<DXGI.ISwapChainPanelNative>(this.SwapChainPanel))
            {
                // Set its swap chain.
                nativeObject.SwapChain = this.swapChain;
            }

            // Create a Texture2D from the existing swap chain to use as 
            this.backBufferTexture = D3D11.Texture2D.FromSwapChain<D3D11.Texture2D>(this.swapChain, 0);
            this.backBufferView = new D3D11.RenderTargetView(this.device, this.backBufferTexture);

            // This event is fired when the application requests a new frame from the DirectX interop controls.
            CompositionTarget.Rendering += CompositionTarget_Rendering;
        }
Exemplo n.º 8
0
        protected virtual void CreateSizeDependentResources(TargetBase renderBase)
        {
            var d3dDevice = DeviceManager.DeviceDirect3D;
            var d3dContext = DeviceManager.ContextDirect3D;
            var d2dContext = DeviceManager.ContextDirect2D;

            d2dContext.Target = null;
            RemoveAndDispose(ref renderTargetView);
            RemoveAndDispose(ref depthStencilView);
            RemoveAndDispose(ref bitmapTarget);
            RemoveAndDispose(ref backBuffer);

            // If the swap chain already exists, resize it.
            if (swapChain != null)
            {
                swapChain.ResizeBuffers(2, Width, Height, SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.DXGI.SwapChainFlags.None);
            }
            // Otherwise, create a new one.
            else
            {
                // SwapChain description
                var desc = CreateSwapChainDescription();

                // Once the desired swap chain description is configured, it must be created on the same adapter as our D3D Device

                // First, retrieve the underlying DXGI Device from the D3D Device.
                // Creates the swap chain 
                using (var dxgiDevice2 = d3dDevice.QueryInterface<SharpDX.DXGI.Device2>())
                using (var dxgiAdapter = dxgiDevice2.Adapter)
                using (var dxgiFactory2 = dxgiAdapter.GetParent<SharpDX.DXGI.Factory2>())
                {
                    swapChain = ToDispose(CreateSwapChain(dxgiFactory2, d3dDevice, desc));

                    // Ensure that DXGI does not queue more than one frame at a time. This both reduces 
                    // latency and ensures that the application will only render after each VSync, minimizing 
                    // power consumption.
                    dxgiDevice2.MaximumFrameLatency = 1;
                }
            }

            // Obtain the backbuffer for this window which will be the final 3D rendertarget.
            backBuffer = ToDispose(SharpDX.Direct3D11.Texture2D.FromSwapChain<SharpDX.Direct3D11.Texture2D>(swapChain, 0));
            {
                // Create a view interface on the rendertarget to use on bind.
                renderTargetView = ToDispose(new SharpDX.Direct3D11.RenderTargetView(d3dDevice, BackBuffer));

                // Cache the rendertarget dimensions in our helper class for convenient use.
                var backBufferDesc = BackBuffer.Description;
                RenderTargetBounds = new Windows.Foundation.Rect(0, 0, backBufferDesc.Width, backBufferDesc.Height);
            }

            // Create a descriptor for the depth/stencil buffer.
            // Allocate a 2-D surface as the depth/stencil buffer.
            // Create a DepthStencil view on this surface to use on bind.
            using (var depthBuffer = new SharpDX.Direct3D11.Texture2D(d3dDevice, new SharpDX.Direct3D11.Texture2DDescription()
            {
                Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt,
                ArraySize = 1,
                MipLevels = 1,
                Width = (int)RenderTargetSize.Width,
                Height = (int)RenderTargetSize.Height,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                BindFlags = SharpDX.Direct3D11.BindFlags.DepthStencil,
            }))
                depthStencilView = ToDispose(new SharpDX.Direct3D11.DepthStencilView(d3dDevice, depthBuffer, new SharpDX.Direct3D11.DepthStencilViewDescription() { Dimension = SharpDX.Direct3D11.DepthStencilViewDimension.Texture2D }));

            // Create a viewport descriptor of the full window size.
            var viewport = new SharpDX.ViewportF((float)RenderTargetBounds.X, (float)RenderTargetBounds.Y, (float)RenderTargetBounds.Width, (float)RenderTargetBounds.Height, 0.0f, 1.0f);

            // Set the current viewport using the descriptor.
            d3dContext.Rasterizer.SetViewport(viewport);

            // Now we set up the Direct2D render target bitmap linked to the swapchain. 
            // Whenever we render to this bitmap, it will be directly rendered to the 
            // swapchain associated with the window.
            var bitmapProperties = new SharpDX.Direct2D1.BitmapProperties1(
                new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied),
                DeviceManager.Dpi,
                DeviceManager.Dpi,
                SharpDX.Direct2D1.BitmapOptions.Target | SharpDX.Direct2D1.BitmapOptions.CannotDraw);

            // Direct2D needs the dxgi version of the backbuffer surface pointer.
            // Get a D2D surface from the DXGI back buffer to use as the D2D render target.
            using (var dxgiBackBuffer = swapChain.GetBackBuffer<SharpDX.DXGI.Surface>(0))
                bitmapTarget = ToDispose(new SharpDX.Direct2D1.Bitmap1(d2dContext, dxgiBackBuffer, bitmapProperties));

            // So now we can set the Direct2D render target.
            d2dContext.Target = BitmapTarget2D;

            // Set D2D text anti-alias mode to Grayscale to ensure proper rendering of text on intermediate surfaces.
            d2dContext.TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Grayscale;            
        }