Пример #1
0
        public virtual void OnResize()
        {
            Debug.Assert(ImmediateContext != null);
            Debug.Assert(Device != null);
            Debug.Assert(SwapChain != null);
            Util.ReleaseCom(ref RenderTargetView);
            Util.ReleaseCom(ref DepthStencilView);
            Util.ReleaseCom(ref DepthStencilBuffer);
            SwapChain.ResizeBuffers(1, ClientWidth, ClientHeight, DXGI.Format.R8G8B8A8_UNorm, DXGI.SwapChainFlags.None);
            using (var resource = D3D11.Resource.FromSwapChain <D3D11.Texture2D>(SwapChain, 0))
            {
                RenderTargetView = new D3D11.RenderTargetView(Device, resource);
            }
            var depthStencilDesc = new D3D11.Texture2DDescription()
            {
                Width             = ClientWidth,
                Height            = ClientHeight,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = DXGI.Format.D24_UNorm_S8_UInt,
                SampleDescription = (Enable4XMsaa) ? new DXGI.SampleDescription(4, Msaa4XQuality - 1) : new DXGI.SampleDescription(1, 0),
                Usage             = D3D11.ResourceUsage.Default,
                BindFlags         = D3D11.BindFlags.DepthStencil,
                CpuAccessFlags    = D3D11.CpuAccessFlags.None,
                OptionFlags       = D3D11.ResourceOptionFlags.None
            };

            DepthStencilBuffer = new D3D11.Texture2D(Device, depthStencilDesc);
            DepthStencilView   = new D3D11.DepthStencilView(Device, DepthStencilBuffer);
            ImmediateContext.OutputMerger.SetTargets(DepthStencilView, RenderTargetView);
            Viewport = new Viewport(0, 0, ClientWidth, ClientHeight, 0.0f, 1.0f);
            ImmediateContext.Rasterizer.SetViewport(Viewport);
        }
Пример #2
0
 public void SetRenderTargets(D3D11.DepthStencilView depthStencilView, D3D11.RenderTargetView renderTargetView)
 {
     DevCon.OutputMerger.SetRenderTargets(depthStencilView, renderTargetView);
 }
Пример #3
0
        public void Resize()
        {
            m_RenderTargetView?.Dispose();
            m_DepthStencilView?.Dispose();
            m_DepthBuffer?.Dispose();
            m_Backbuffer?.Dispose();
            D2DRenderTarget?.Dispose();

            SwapChain.ResizeBuffers(1, m_ApplicationInfo.Width, m_ApplicationInfo.Height, DXGI.Format.R8G8B8A8_UNorm, DXGI.SwapChainFlags.None);

            m_Backbuffer       = SwapChain.GetBackBuffer <D3D11.Texture2D>(0);
            m_RenderTargetView = new D3D11.RenderTargetView(Dev, m_Backbuffer);

            var d2dRenderTargetProps = new D2D1.RenderTargetProperties
            {
                PixelFormat = new D2D1.PixelFormat(DXGI.Format.Unknown, D2D1.AlphaMode.Premultiplied),
                Type        = D2D1.RenderTargetType.Default,
                DpiX        = D2DFactory.DesktopDpi.Width,
                DpiY        = D2DFactory.DesktopDpi.Height
            };

            DXGI.Surface2 backBuffer = SwapChain.GetBackBuffer <DXGI.Surface2>(0);
            D2DRenderTarget = new D2D1.RenderTarget(D2DFactory, backBuffer, d2dRenderTargetProps);
            backBuffer.Dispose();

            var depthBufferDesc = new D3D11.Texture2DDescription
            {
                Format            = DXGI.Format.D32_Float_S8X24_UInt,
                ArraySize         = 1,
                MipLevels         = 1,
                Width             = m_Backbuffer.Description.Width,
                Height            = m_Backbuffer.Description.Height,
                SampleDescription = SwapChain.Description.SampleDescription,
                BindFlags         = D3D11.BindFlags.DepthStencil,
            };

            m_Backbuffer.Dispose();

            var depthStencilViewDesc = new D3D11.DepthStencilViewDescription
            {
                Dimension = SwapChain.Description.SampleDescription.Count > 1 || SwapChain.Description.SampleDescription.Quality > 0
                            ? D3D11.DepthStencilViewDimension.Texture2DMultisampled
                            : D3D11.DepthStencilViewDimension.Texture2D
            };

            var depthStencilStateDesc = new D3D11.DepthStencilStateDescription
            {
                IsDepthEnabled   = true,
                DepthComparison  = D3D11.Comparison.Less,
                DepthWriteMask   = D3D11.DepthWriteMask.All,
                IsStencilEnabled = false,
                StencilReadMask  = 0xff,
                StencilWriteMask = 0xff,
                FrontFace        = new D3D11.DepthStencilOperationDescription
                {
                    Comparison         = D3D11.Comparison.Always,
                    PassOperation      = D3D11.StencilOperation.Keep,
                    FailOperation      = D3D11.StencilOperation.Keep,
                    DepthFailOperation = D3D11.StencilOperation.Increment
                },
                BackFace = new D3D11.DepthStencilOperationDescription
                {
                    Comparison         = D3D11.Comparison.Always,
                    PassOperation      = D3D11.StencilOperation.Keep,
                    FailOperation      = D3D11.StencilOperation.Keep,
                    DepthFailOperation = D3D11.StencilOperation.Decrement
                }
            };

            m_DepthBuffer       = new D3D11.Texture2D(Dev, depthBufferDesc);
            m_DepthStencilView  = new D3D11.DepthStencilView(Dev, m_DepthBuffer, depthStencilViewDesc);
            m_DepthStencilState = new D3D11.DepthStencilState(Dev, depthStencilStateDesc);

            SetRenderTargets(m_DepthStencilView, m_RenderTargetView);
            DevCon.OutputMerger.DepthStencilState = m_DepthStencilState;

            m_ScreenViewport.X        = 0;
            m_ScreenViewport.Y        = 0;
            m_ScreenViewport.Width    = m_ApplicationInfo.Width;
            m_ScreenViewport.Height   = m_ApplicationInfo.Height;
            m_ScreenViewport.MinDepth = 0.0f;
            m_ScreenViewport.MaxDepth = 1.0f;
            DevCon.Rasterizer.SetViewport(m_ScreenViewport);
        }