Esempio n. 1
0
        public void ValidateDevice()
        {
            DxgiAdapterDesc2 previousDesc;

            using (var dxgiDevice = new DxgiDevice2(this.d3dDevice.Handle))
                using (var deviceAdapter = dxgiDevice.GetAdapter())
                    using (var deviceFactory = deviceAdapter.GetParent())
                        using (var previousDefaultAdapter = deviceFactory.EnumAdapters().First())
                        {
                            previousDesc = previousDefaultAdapter.Description;
                        }

            DxgiAdapterDesc2 currentDesc;

            using (var currentFactory = DxgiFactory2.Create())
                using (var currentDefaultAdapter = currentFactory.EnumAdapters().First())
                {
                    currentDesc = currentDefaultAdapter.Description;
                }

            if (previousDesc.AdapterLuid != currentDesc.AdapterLuid || this.d3dDevice.GetDeviceRemovedReason() != null)
            {
                this.HandleDeviceLost();
            }
        }
        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));
        }