public static D3D11Texture2D GetTexture2D(this DxgiSwapChain2 swapChain, uint buffer)
        {
            if (swapChain == null)
            {
                throw new ArgumentNullException("swapChain");
            }

            return(new D3D11Texture2D((ID3D11Texture2D)swapChain.GetBuffer(buffer, typeof(ID3D11Texture2D).GUID)));
        }
        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));
        }