// Allocate all memory resources that change on a window SizeChanged event. protected virtual void CreateWindowSizeDependentResources() { _windowBounds = _window.Bounds; if (_swapChain != null) { _swapChain.ResizeBuffers(2, 0, 0, DXGI.Format.B8G8R8A8_UNorm, 0); } else { var swapChainDesc = new DXGI.SwapChainDescription1() { Width = 0, Height = 0, Format = DXGI.Format.B8G8R8A8_UNorm, Stereo = false, Usage = DXGI.Usage.RenderTargetOutput, BufferCount = 2, Scaling = DXGI.Scaling.None, SwapEffect = DXGI.SwapEffect.FlipSequential, SampleDescription = new DXGI.SampleDescription { Count = 1, Quality = 0 }, Flags = 0 }; _swapChain = _window.CreateSwapChain(_device, ref swapChainDesc); // gotta figure out some reasonable way of doing this // dxgiDevice.MaximumFrameLatency = 1; D3D11.Texture2D backBuffer = _swapChain.GetBackBuffer<D3D11.Texture2D>(0); _renderTargetView = new D3D11.RenderTargetView(_device, backBuffer); // Cache the rendertarget dimensions in our helper class for convenient use. _renderTargetSize.Width = backBuffer.Description.Width; _renderTargetSize.Height = backBuffer.Description.Height; // Create a descriptor for the depth/stencil buffer. var depthStencilDesc = new D3D11.Texture2DDescription { Format = DXGI.Format.D24_UNorm_S8_UInt, Width = backBuffer.Description.Width, Height = backBuffer.Description.Height, ArraySize = 1, MipLevels = 1, BindFlags = D3D11.BindFlags.DepthStencil, SampleDescription = new DXGI.SampleDescription { Count = 1 } }; // Allocate a 2-D surface as the depth/stencil buffer. var depthStencil = new D3D11.Texture2D(_device, depthStencilDesc); // Create a DepthStencil view on this surface to use on bind. _depthStencilView = new D3D11.DepthStencilView(_device, depthStencil, new D3D11.DepthStencilViewDescription { Dimension = D3D11.DepthStencilViewDimension.Texture2D }); // Create a viewport descriptor of the full window size. var viewPort = new D3D11.Viewport { TopLeftX = 0.0f, TopLeftY = 0.0f, Width = backBuffer.Description.Width, Height = backBuffer.Description.Height }; // Set the current viewport using the descriptor. _deviceContext.Rasterizer.SetViewports(viewPort); } }
public virtual void CreateWindowSizeDependentResources() { var resource = _renderTargetview.Resource; using (var texture2D = new Texture2D(resource.NativePointer)) { var currentWidth = (int) _renderTargetSize.Width; var currentHeight = (int) _renderTargetSize.Height; if (currentWidth != texture2D.Description.Width && currentHeight != texture2D.Description.Height) { _renderTargetSize.Width = texture2D.Description.Width; _renderTargetSize.Height = texture2D.Description.Height; ComObject.Dispose(ref _depthStencilView); using (var depthTexture = new Texture2D( _device, new Texture2DDescription() { Width = (int) _renderTargetSize.Width, Height = (int) _renderTargetSize.Height, ArraySize = 1, BindFlags = BindFlags.DepthStencil, CpuAccessFlags = CpuAccessFlags.None, Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt, MipLevels = 1, OptionFlags = ResourceOptionFlags.None, SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), Usage = ResourceUsage.Default })) _depthStencilView = new DepthStencilView(_device, depthTexture); } } _windowBounds.Width = _renderTargetSize.Width; _windowBounds.Height = _renderTargetSize.Height; // Create a viewport descriptor of the full window size. var viewport = new SharpDX.Direct3D11.Viewport(0, 0, (float)_renderTargetSize.Width, (float)_renderTargetSize.Height ); _deviceContext.Rasterizer.SetViewports(viewport); }