/// <summary>
        ///
        /// </summary>
        private void CreateAndBindTargets()
        {
            //surfaceD3D.SetRenderTargetDX11(null);

            int width  = System.Math.Max((int)ActualWidth, 100);
            int height = System.Math.Max((int)ActualHeight, 100);

            device.ImmediateContext.OutputMerger.ResetTargets();
            Disposer.RemoveAndDispose(ref colorBufferView);
            Disposer.RemoveAndDispose(ref colorBuffer);
            Disposer.RemoveAndDispose(ref backBuffer);
            Disposer.RemoveAndDispose(ref depthStencilBufferView);
            Disposer.RemoveAndDispose(ref depthStencilBuffer);
            CreateSwapChain();
            backBuffer = Texture2D.FromSwapChain <Texture2D>(swapChain, 0);

#if DoubleBuffer
            int sampleCount   = 1;
            int sampleQuality = 0;
            if (MSAA != MSAALevel.Disable)
            {
                do
                {
                    var newSampleCount   = sampleCount * 2;
                    var newSampleQuality = device.CheckMultisampleQualityLevels(Format.B8G8R8A8_UNorm, newSampleCount) - 1;

                    if (newSampleQuality < 0)
                    {
                        break;
                    }

                    sampleCount   = newSampleCount;
                    sampleQuality = newSampleQuality;
                    if (sampleCount == (int)MSAA)
                    {
                        break;
                    }
                } while (sampleCount < 32);
            }
            var sampleDesc = new SampleDescription(sampleCount, sampleQuality);
            var colordesc  = new Texture2DDescription
            {
                BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource,
                Format            = Format.B8G8R8A8_UNorm,
                Width             = width,
                Height            = height,
                MipLevels         = 1,
                SampleDescription = sampleDesc,
                Usage             = ResourceUsage.Default,
                OptionFlags       = ResourceOptionFlags.None,
                CpuAccessFlags    = CpuAccessFlags.None,
                ArraySize         = 1
            };
            colorBuffer     = new Texture2D(device, colordesc);
            colorBufferView = new RenderTargetView(device, colorBuffer);
#else
            var sampleDesc = swapChain.Description1.SampleDescription;
            colorBufferView = new RenderTargetView(device, backBuffer);
#endif
            var depthdesc = new Texture2DDescription
            {
                BindFlags = BindFlags.DepthStencil,
                //Format = Format.D24_UNorm_S8_UInt,
                Format            = Format.D32_Float_S8X24_UInt,
                Width             = width,
                Height            = height,
                MipLevels         = 1,
                SampleDescription = sampleDesc,
                Usage             = ResourceUsage.Default,
                OptionFlags       = ResourceOptionFlags.None,
                CpuAccessFlags    = CpuAccessFlags.None,
                ArraySize         = 1,
            };
            depthStencilBuffer     = new Texture2D(device, depthdesc);
            depthStencilBufferView = new DepthStencilView(device, depthStencilBuffer);
            this.device.ImmediateContext.Rasterizer.SetScissorRectangle(0, 0, width, height);
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        private void CreateAndBindTargets()
        {
            surfaceD3D.SetRenderTargetDX11(null);

            int width  = System.Math.Max((int)ActualWidth, 100);
            int height = System.Math.Max((int)ActualHeight, 100);

            Disposer.RemoveAndDispose(ref colorBufferView);
            Disposer.RemoveAndDispose(ref depthStencilBufferView);
            Disposer.RemoveAndDispose(ref colorBuffer);
            Disposer.RemoveAndDispose(ref depthStencilBuffer);
#if MSAA
            Disposer.RemoveAndDispose(ref renderTargetNMS);

            int sampleCount   = 1;
            int sampleQuality = 0;
            if (IsMSAAEnabled)
            {
                do
                {
                    var newSampleCount   = sampleCount * 2;
                    var newSampleQuality = device.CheckMultisampleQualityLevels(Format.B8G8R8A8_UNorm, newSampleCount) - 1;

                    if (newSampleQuality < 0)
                    {
                        break;
                    }

                    sampleCount   = newSampleCount;
                    sampleQuality = newSampleQuality;
                } while (sampleCount < 32);
            }

            var sampleDesc  = new SampleDescription(sampleCount, sampleQuality);
            var optionFlags = ResourceOptionFlags.None;
#else
            var sampleDesc  = new SampleDescription(1, 0);
            var optionFlags = ResourceOptionFlags.Shared;
#endif

            var colordesc = new Texture2DDescription
            {
                BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource,
                Format            = Format.B8G8R8A8_UNorm,
                Width             = width,
                Height            = height,
                MipLevels         = 1,
                SampleDescription = sampleDesc,
                Usage             = ResourceUsage.Default,
                OptionFlags       = optionFlags,
                CpuAccessFlags    = CpuAccessFlags.None,
                ArraySize         = 1
            };

            var depthdesc = new Texture2DDescription
            {
                BindFlags = BindFlags.DepthStencil,
                //Format = Format.D24_UNorm_S8_UInt,
                Format            = Format.D32_Float_S8X24_UInt,
                Width             = width,
                Height            = height,
                MipLevels         = 1,
                SampleDescription = sampleDesc,
                Usage             = ResourceUsage.Default,
                OptionFlags       = ResourceOptionFlags.None,
                CpuAccessFlags    = CpuAccessFlags.None,
                ArraySize         = 1,
            };

            colorBuffer        = new Texture2D(device, colordesc);
            depthStencilBuffer = new Texture2D(device, depthdesc);

            colorBufferView        = new RenderTargetView(device, colorBuffer);
            depthStencilBufferView = new DepthStencilView(device, depthStencilBuffer);

#if MSAA
            var colordescNMS = new Texture2DDescription
            {
                BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource,
                Format            = Format.B8G8R8A8_UNorm,
                Width             = width,
                Height            = height,
                MipLevels         = 1,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Default,
                OptionFlags       = ResourceOptionFlags.Shared,
                CpuAccessFlags    = CpuAccessFlags.None,
                ArraySize         = 1
            };

            renderTargetNMS = new Texture2D(device, colordescNMS);
            device.ImmediateContext.ResolveSubresource(colorBuffer, 0, renderTargetNMS, 0, Format.B8G8R8A8_UNorm);
            surfaceD3D.SetRenderTargetDX11(renderTargetNMS);
#else
            this.surfaceD3D.SetRenderTargetDX11(this.colorBuffer);
#endif
        }