Ejemplo n.º 1
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
        }
Ejemplo n.º 2
0
 public void SetRenderTarget(Texture2D target)
 {
     SynchronizeToCurrentThread(() => {
         surfaceD3D?.SetRenderTargetDX11(target);
     });
 }