public DxgiSwapChain2 CreateSwapChainForWindowHandle(
            object device,
            IntPtr hwnd,
            DxgiSwapChainDesc1 desc,
            DxgiSwapChainFullscreenDesc? fullscreenDesc,
            DxgiOutput2 restrictToOutput)
        {
            if (fullscreenDesc == null)
            {
                return new DxgiSwapChain2(this.factory.CreateSwapChainForWindowHandle(
                    device,
                    hwnd,
                    ref desc,
                    IntPtr.Zero,
                    restrictToOutput == null ? null : restrictToOutput.GetHandle<IDxgiOutput>()));
            }
            else
            {
                IntPtr fullscreenDescPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(DxgiSwapChainFullscreenDesc)));

                try
                {
                    Marshal.StructureToPtr(fullscreenDesc.Value, fullscreenDescPtr, false);
                    return new DxgiSwapChain2(this.factory.CreateSwapChainForWindowHandle(
                        device,
                        hwnd,
                        ref desc,
                        fullscreenDescPtr,
                        restrictToOutput == null ? null : restrictToOutput.GetHandle<IDxgiOutput>()));
                }
                finally
                {
                    Marshal.FreeHGlobal(fullscreenDescPtr);
                }
            }
        }
        protected override D3D11Texture2D OnCreateBackBuffer()
        {
            if (this.swapChain)
            {
                try
                {
                    this.swapChain.ResizeBuffers(3, 0, 0, DxgiFormat.Unknown, DxgiSwapChainOptions.None);
                }
                catch (Exception ex)
                {
                    if (ex.HResult == DxgiError.DeviceRemoved || ex.HResult == DxgiError.DeviceReset)
                    {
                        this.HandleDeviceLost();
                        return(null);
                    }

                    throw;
                }
            }
            else
            {
                DxgiSwapChainDesc1 swapChainDesc = new DxgiSwapChainDesc1
                {
                    Width             = 0,
                    Height            = 0,
                    Format            = DxgiFormat.B8G8R8A8UNorm,
                    Stereo            = false,
                    SampleDescription = new DxgiSampleDesc(1, 0),
                    BufferUsage       = DxgiUsages.RenderTargetOutput,
                    BufferCount       = 3,
                    Scaling           = DxgiScaling.None,
                    SwapEffect        = DxgiSwapEffect.FlipSequential,
                    AlphaMode         = DxgiAlphaMode.Ignore,
                    Options           = DxgiSwapChainOptions.None
                };

                using (var dxgiDevice = new DxgiDevice2(this.D3DDevice.Handle))
                    using (var dxgiAdapter = dxgiDevice.GetAdapter())
                        using (var dxgiFactory = dxgiAdapter.GetParent())
                        {
                            try
                            {
                                this.swapChain = dxgiFactory.CreateSwapChainForWindowHandle(
                                    this.D3DDevice.Handle,
                                    this.window.Handle,
                                    swapChainDesc,
                                    null,
                                    null);
                            }
                            catch (Exception ex)
                            {
                                if (ex.HResult == DxgiError.InvalidCall)
                                {
                                    swapChainDesc.SwapEffect = DxgiSwapEffect.Sequential;

                                    this.swapChain = dxgiFactory.CreateSwapChainForWindowHandle(
                                        this.D3DDevice.Handle,
                                        this.window.Handle,
                                        swapChainDesc,
                                        null,
                                        null);
                                }
                                else
                                {
                                    throw;
                                }
                            }

                            dxgiDevice.MaximumFrameLatency = 1;
                        }
            }

            return(this.swapChain.GetTexture2D(0));
        }