Exemple #1
0
        public HolographicGraphicsPresenter(GraphicsDevice device, PresentationParameters presentationParameters, HolographicSpace holographicSpace)
            : base(device, presentationParameters)
        {
            if (GraphicsDevice.RenderTargetViewAllocator.DescriptorHeap.Description.DescriptorCount != BufferCount)
            {
                GraphicsDevice.RenderTargetViewAllocator.Dispose();
                GraphicsDevice.RenderTargetViewAllocator = new DescriptorAllocator(GraphicsDevice, DescriptorHeapType.RenderTargetView, descriptorCount: BufferCount);
            }

            using (IDXGIDevice dxgiDevice = GraphicsDevice.NativeDirect3D11Device.QueryInterface <IDXGIDevice>())
            {
                IDirect3DDevice direct3DInteropDevice = Direct3DInterop.CreateDirect3DDevice(dxgiDevice);

                HolographicSpace = holographicSpace;
                HolographicSpace.SetDirect3D11Device(direct3DInteropDevice);
            }

            HolographicDisplay = HolographicDisplay.GetDefault();
            SpatialStationaryFrameOfReference = HolographicDisplay.SpatialLocator.CreateStationaryFrameOfReferenceAtCurrentLocation();

            HolographicFrame      = HolographicSpace.CreateNextFrame();
            HolographicSurface    = HolographicFrame.GetRenderingParameters(HolographicFrame.CurrentPrediction.CameraPoses[0]).Direct3D11BackBuffer;
            HolographicBackBuffer = GetHolographicBackBuffer();

            renderTarget           = CreateRenderTarget();
            direct3D11RenderTarget = CreateDirect3D11RenderTarget();

            DepthStencilBuffer.Dispose();
            DepthStencilBuffer = CreateDepthStencilBuffer();
        }
Exemple #2
0
 public void Begin(int backBufferWidth, int backBufferHeight, SurfaceFormat bufferFormat, MultiSampleType multiSampleType)
 {
     if (renderTarget == null ||
         renderTargetWidth != backBufferWidth ||
         renderTargetHeight != backBufferHeight)
     {
         if (renderTarget != null)
         {
             renderTarget.Dispose();
             depthBuffer.Dispose();
         }
         renderTarget =
             new RenderTarget2D(graphicsDevice,
                                backBufferWidth, backBufferHeight,
                                1, bufferFormat,
                                multiSampleType, 0);
         depthBuffer =
             new DepthStencilBuffer(graphicsDevice,
                                    backBufferWidth, backBufferHeight,
                                    graphicsDevice.DepthStencilBuffer.Format,
                                    multiSampleType, 0);
         renderTargetWidth  = backBufferWidth;
         renderTargetHeight = backBufferHeight;
     }
     graphicsDevice.SetRenderTarget(0, renderTarget);
     savedDepthBuffer = graphicsDevice.DepthStencilBuffer;
     graphicsDevice.DepthStencilBuffer = depthBuffer;
 }
Exemple #3
0
 public void ShutDown()
 {
     SwapChain?.SetFullscreenState(false, null);
     AlphaEnableBlendingState?.Dispose();
     AlphaEnableBlendingState = null;
     AlphaDisableBlendingState?.Dispose();
     AlphaDisableBlendingState = null;
     DepthDisabledStencilState?.Dispose();
     DepthDisabledStencilState = null;
     RasterState?.Dispose();
     RasterState = null;
     RasterStateNoCulling?.Dispose();
     RasterStateNoCulling = null;
     RasterStateWirefram?.Dispose();
     RasterStateWirefram = null;
     DepthStencilView?.Dispose();
     DepthStencilView = null;
     DepthStencilState?.Dispose();
     DepthStencilState = null;
     DepthStencilBuffer?.Dispose();
     DepthStencilBuffer = null;
     RenderTargetView?.Dispose();
     RenderTargetView = null;
     DeviceContext?.Dispose();
     DeviceContext = null;
     Device?.Dispose();
     Device = null;
     SwapChain?.Dispose();
     SwapChain = null;
 }
Exemple #4
0
 public void Shutdown()
 {
     DepthStencilView?.Dispose();
     DepthStencilView = null;
     DepthStencilBuffer?.Dispose();
     DepthStencilBuffer = null;
     for (int i = 0; i < BUFFER_COUNT; i++)
     {
         ShaderResourceViewArray[i]?.Dispose();
         ShaderResourceViewArray[i] = null;
     }
     ShaderResourceViewArray = null;
     for (int i = 0; i < BUFFER_COUNT; i++)
     {
         RenderTargetViewArray[i]?.Dispose();
         RenderTargetViewArray[i] = null;
     }
     RenderTargetViewArray = null;
     for (int i = 0; i < BUFFER_COUNT; i++)
     {
         RenderTargetTexture2DArray[i]?.Dispose();
         RenderTargetTexture2DArray[i] = null;
     }
     RenderTargetTexture2DArray = null;
 }
        public void ShutDown()
        {
            // Before shutting down set to windowed mode or when you release the swap chain it will throw an exception.
            SwapChain?.SetFullscreenState(false, null);

            // Dispose of all objects.
            DepthDisabledStencilState?.Dispose();
            DepthDisabledStencilState = null;
            RasterState?.Dispose();
            RasterState = null;
            DepthStencilView?.Dispose();
            DepthStencilView = null;
            DepthStencilState?.Dispose();
            DepthStencilState = null;
            DepthStencilBuffer?.Dispose();
            DepthStencilBuffer = null;
            RenderTargetView?.Dispose();
            RenderTargetView = null;
            DeviceContext?.Dispose();
            DeviceContext = null;
            Device?.Dispose();
            Device = null;
            SwapChain?.Dispose();
            SwapChain = null;
        }
Exemple #6
0
        protected virtual void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (disposing)
            {
                if (DepthStencilBuffer != null)
                {
                    DepthStencilBuffer.Dispose();
                }
                if (RenderTarget != null)
                {
                    RenderTarget.Dispose();
                }
                if (HMD != null)
                {
                    HMD.Dispose();
                }
                OVR.Shutdown();
            }
            _disposed = true;
        }
 public void Dispose()
 {
     if (_device != null)
     {
         _device.SetVertexInputLayout(null);
     }
     if (_meshes != null)
     {
         foreach (WarpMesh mesh in _meshes)
         {
             mesh.Dispose();
         }
     }
     if (_effect != null)
     {
         _effect.Dispose();
     }
     if (_renderTexture != null)
     {
         _renderTexture.Dispose();
     }
     if (_depthStencilTexture != null)
     {
         _depthStencilTexture.Dispose();
     }
     if (_contentManager != null)
     {
         _contentManager.Dispose();
     }
     if (_device != null)
     {
         _device.Dispose();
     }
 }
        /// <summary>
        /// Event handler called when the graphics device is resetting
        /// </summary>
        /// <param name="sender">unused</param>
        /// <param name="e">unused</param>
        private void DisposeDepthBuffer(object sender, EventArgs e)
        {
            if (mDepthBuffer != null)
            {
                mDepthBuffer.Dispose();
            }

            mDepthBuffer = null;
        }
Exemple #9
0
        public void Shutdown()
        {
            // Before shutting down set to windowed mode or when you release the swap chain it will throw an exception.
            if (SwapChain != null)
            {
                SwapChain.SetFullscreenState(false, null);
            }

            if (DepthDisabledStencilState != null)
            {
                DepthDisabledStencilState.Dispose();
                DepthDisabledStencilState = null;
            }

            if (RasterState != null)
            {
                RasterState.Dispose();
                RasterState = null;
            }

            if (DepthStencilView != null)
            {
                DepthStencilView.Dispose();
                DepthStencilView = null;
            }

            if (DepthStencilState != null)
            {
                DepthStencilState.Dispose();
                DepthStencilState = null;
            }

            if (DepthStencilBuffer != null)
            {
                DepthStencilBuffer.Dispose();
                DepthStencilBuffer = null;
            }

            if (RenderTargetView != null)
            {
                RenderTargetView.Dispose();
                RenderTargetView = null;
            }

            if (Device != null)
            {
                Device.Dispose();
                Device = null;
            }

            if (SwapChain != null)
            {
                SwapChain.Dispose();
                SwapChain = null;
            }
        }
Exemple #10
0
        protected virtual void DisposeBuffers()
        {
            if (!_buffersCreated)
            {
                return;
            }

            _frontBufferPointer = IntPtr.Zero;

            _compositionTargetSetBackBufferRequired = true;

            var device = _graphicsDeviceManager.GraphicsDevice;

            if (device != null && !device.IsDisposed)
            {
                device.SetRenderTarget(0, null);
            }

            if (_frontBuffer != null)
            {
                _frontBuffer.Dispose();
            }

            if (_frontBuffer != null)
            {
                _backBuffer.Dispose();
            }

            _frontBuffer = null;
            _backBuffer  = null;

            if (_deviceDepthStencilBuffer != null)
            {
                if (device != null && !device.IsDisposed)
                {
                    device.DepthStencilBuffer = _deviceDepthStencilBuffer;
                }
                else if (!_deviceDepthStencilBuffer.IsDisposed)
                {
                    _deviceDepthStencilBuffer.Dispose();
                }
            }

            if (_depthStencilBuffer != null)
            {
                _depthStencilBuffer.Dispose();
            }

            _depthStencilBuffer       = null;
            _deviceDepthStencilBuffer = null;

            _buffersCreated = false;
        }
Exemple #11
0
 public void Shutdown()
 {
     DepthStencilView?.Dispose();
     DepthStencilView = null;
     DepthStencilBuffer?.Dispose();
     DepthStencilBuffer = null;
     ShaderResourceView?.Dispose();
     ShaderResourceView = null;
     RenderTargetView?.Dispose();
     RenderTargetView = null;
     RenderTargetTexture?.Dispose();
     RenderTargetTexture = null;
 }
Exemple #12
0
        private void DisposeBuffers()
        {
            _frontBufferPointer = IntPtr.Zero;
            _backBufferPointer  = IntPtr.Zero;

            _compositionTargetSetBackBufferRequired = true;

            _device.SetRenderTarget(0, null);
            _device.DepthStencilBuffer = _deviceDepthStencilBuffer;

            _frontBuffer.Dispose();
            _backBuffer.Dispose();
            _depthStencilBuffer.Dispose();
        }
Exemple #13
0
        public override void Process(ITimeService time)
        {
            IsEnabled = false;
            var device             = Services.GetService <IGraphicsDeviceService>().DirectXDevice;
            var cScreenshot        = Entities.First().GetComponent <ScreenshotComponent>();
            var backBuffer         = device.BackBuffer;
            var textureDescription = new Texture2DDescription()
            {
                CpuAccessFlags    = CpuAccessFlags.None,
                Format            = backBuffer.Format.Value,
                Height            = backBuffer.Height,
                Usage             = ResourceUsage.Default,
                Width             = backBuffer.Width,
                ArraySize         = 1,
                SampleDescription = new SampleDescription(1, 0),
                BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource,
                MipLevels         = 1,
                OptionFlags       = ResourceOptionFlags.None
            };

            renderTarget = RenderTarget2D.New(device, textureDescription);

            var depthStencilDesc = new Texture2DDescription()
            {
                ArraySize         = 1,
                MipLevels         = 1,
                BindFlags         = BindFlags.DepthStencil,
                Format            = device.DepthStencilBuffer.Format.Value,
                Width             = textureDescription.Width,
                Height            = textureDescription.Height,
                SampleDescription = textureDescription.SampleDescription,
                CpuAccessFlags    = CpuAccessFlags.None,
                Usage             = ResourceUsage.Default,
                OptionFlags       = ResourceOptionFlags.None
            };

            depthStencil = DepthStencilBuffer.New(device, depthStencilDesc);


            ((DeviceContext)device).ResolveSubresource(backBuffer, 0, renderTarget, 0, textureDescription.Format);
            renderTarget.Initialize();
            depthStencil.Initialize();

            device.SetRenderTargets(depthStencil, renderTarget);
            renderTarget.Save("test.png", ImageFileType.Png);

            renderTarget.Dispose();
            depthStencil.Dispose();
            IsEnabled = false;
        }
Exemple #14
0
        private void CreateDevice()
        {
            _device = XnaHelper.CreateDevice(
                _targetSize,
                true,
                true,
                true);

            if (_deviceDepthStencilBuffer != null &&
                _deviceDepthStencilBuffer != _device.DepthStencilBuffer &&
                !_deviceDepthStencilBuffer.IsDisposed)
            {
                _deviceDepthStencilBuffer.Dispose();
            }

            _deviceDepthStencilBuffer = _device.DepthStencilBuffer;
        }
Exemple #15
0
            public void DestroyBuffers()
            {
                oldState = null;

                if (CreatedBuffers)
                {
                    if (RenderTarget != null)
                    {
                        RenderTarget.Dispose();
                        RenderTarget = null;
                    }
                    if (DepthBuffer != null)
                    {
                        DepthBuffer.Dispose();
                        DepthBuffer = null;
                    }
                }
            }
Exemple #16
0
        public void Dispose()
        {
            if (IsDisposed)
            {
                return;
            }

            if (RenderContext != null)
            {
                RenderContext.Dispose();
            }
            if (BackBuffer != null)
            {
                BackBuffer.Dispose();
            }
            if (DepthStencilBuffer != null)
            {
                DepthStencilBuffer.Dispose();
            }
            //if (Factory != null)
            //Factory.Dispose();
            if (swapChain != null)
            {
                swapChain.Dispose();
            }

            GraphicsObject[] created = CreatedObjects.ToArray();
            for (int i = 0; i < created.Length; i++)
            {
                if (!created[i].IsDisposed)
                {
                    Log.Warning("{0} was not disposed! Created at:\r\n{1}", created[i].GetType().Name, created[i].CreationStack);
                    created[i].Dispose();
                }
            }
            CreatedObjects.Clear();

            if (Device != null)
            {
                Device.Dispose();
            }

            IsDisposed = true;
        }
Exemple #17
0
        /// <inheritdoc />
        public override void Destroy()
        {
            base.Destroy();

            lock (Renderer.Locker)
            {
                CanRender = false;

                _bloomEffect.Destroy();
                _ambientOcclusion.Destroy();

                RenderHandle = IntPtr.Zero;

                lock (Locker)
                {
                    LayerCollectors.Clear();
                }

                Constants.Dispose();

                RenderTarget2D?.Dispose();

                BackBuffer?.Dispose();
                Buffer0?.Dispose();
                Buffer1?.Dispose();
                Buffer2?.Dispose();
                Buffer3?.Dispose();

                BufferDownscaleHalf0?.Dispose();
                BufferDownscaleHalf1?.Dispose();
                BufferDownscaleQuarter0?.Dispose();
                BufferDownscaleQuarter1?.Dispose();

                DepthStencilBuffer?.Dispose();

                SwapChain?.Dispose();

                BackBuffer = null;
                Buffer0    = null;
                Buffer1    = null;
                Buffer2    = null;
                Buffer3    = null;
            }
        }
Exemple #18
0
        public void Shutdown()
        {
            // Before shutting down set to windowed mode or when you release the swap chain it will throw an exception.
            _swapChain?.SetFullscreenState(false, null);

            _alphaEnableBlendingState?.Dispose();
            _alphaEnableBlendingState = null;

            _alphaDisableBlendingState?.Dispose();
            _alphaDisableBlendingState = null;

            BackBuffer?.Dispose();
            BackBuffer = null;

            _depthDisabledStencilState?.Dispose();
            _depthDisabledStencilState = null;

            _rasterState?.Dispose();
            _rasterState = null;

            DepthStencilView?.Dispose();
            DepthStencilView = null;

            _depthStencilState?.Dispose();
            _depthStencilState = null;

            DepthStencilBuffer?.Dispose();
            DepthStencilBuffer = null;

            _renderTargetView?.Dispose();
            _renderTargetView = null;

            DeviceContext?.Dispose();
            DeviceContext = null;

            Device?.Dispose();
            Device = null;

            BackBuffer?.Dispose();
            BackBuffer = null;

            _swapChain?.Dispose();
            _swapChain = null;
        }
        protected override void DeviceReset()
        {
            RenderTarget2D     oldTarget  = renderTarget;
            DepthStencilBuffer oldStencil = depthStencilBuffer;

            try
            {
                renderTarget       = new RenderTarget2D(GraphicsDevice, (int)(GraphicsDevice.DisplayMode.Width / scale), (int)(GraphicsDevice.DisplayMode.Height / scale), 1, SurfaceFormat.Color);
                depthStencilBuffer = CreateDepthStencil(renderTarget);
            }
            catch
            {
                renderTarget       = oldTarget;
                depthStencilBuffer = oldStencil;
                throw;
            }
            oldTarget.Dispose();
            oldStencil.Dispose();
        }
Exemple #20
0
 public void ShutDown()
 {
     SwapChain?.SetFullscreenState(false, null);
     RasterState?.Dispose();
     RasterState = null;
     DepthStencilView?.Dispose();
     DepthStencilView = null;
     DepthStencilState?.Dispose();
     DepthStencilState = null;
     DepthStencilBuffer?.Dispose();
     DepthStencilBuffer = null;
     RenderTargetView?.Dispose();
     RenderTargetView = null;
     DeviceContext?.Dispose();
     DeviceContext = null;
     Device?.Dispose();
     Device = null;
     SwapChain?.Dispose();
     SwapChain = null;
 }
Exemple #21
0
        public void ShutDown()
        {
            // Before shutting down set to windowed mode or when you release the swap chain it will throw an exception.
            SwapChain?.SetFullscreenState(false, null);
            RasterState?.Dispose();
            RasterState = null;
            depthStencilState?.Dispose();
            depthStencilState = null;
            DepthStencilBuffer?.Dispose();
            DepthStencilBuffer = null;
            _depthStencilView?.Dispose();
            _depthStencilView = null;
            _renderTargetView?.Dispose();
            _renderTargetView = null;
            DeviceContext?.Dispose();
            Device?.Dispose();
            SwapChain?.Dispose();



            AlphaEnableBlendingState?.Dispose();
            AlphaEnableBlendingState = null;
            AlphaDisableBlendingState?.Dispose();
            AlphaDisableBlendingState = null;
            DepthDisabledStencilState?.Dispose();
            DepthDisabledStencilState = null;
            //DepthStencilView?.Dispose();
            //DepthStencilView = null;
            DepthStencilState?.Dispose();
            DepthStencilState = null;
            DepthStencilBuffer?.Dispose();
            DepthStencilBuffer = null;


            if (main_thread_update != null)
            {
                //main_thread_update.Suspend();
                main_thread_update = null;
            }
            ShutDownGraphics();
        }
        protected virtual void Dispose(bool disposing)
        {
            // Implements the basic dispose pattern.
            // Ref: https://msdn.microsoft.com/en-us/library/b1yfkh5e(v=vs.110).aspx
            if (disposing)
            {
                FlushCommandQueue();

                RtvHeap?.Dispose();
                DsvHeap?.Dispose();
                SwapChain?.Dispose();
                foreach (Resource buffer in _swapChainBuffers)
                {
                    buffer?.Dispose();
                }
                DepthStencilBuffer?.Dispose();
                CommandList?.Dispose();
                DirectCmdListAlloc?.Dispose();
                CommandQueue?.Dispose();
                Fence?.Dispose();
                Device?.Dispose();
            }
        }
Exemple #23
0
        /// <summary>
        /// Creates render targets and DS buffers needed for rendering
        /// </summary>
        private void CreateRenderTargets()
        {
            Vector2 gBufferMultiplier = GBufferSizes[(int)gBufferSize];
            int     gBufferWidth      = (int)(gBufferMultiplier.X * ScreenWidth);
            int     gBufferHeight     = (int)(gBufferMultiplier.Y * ScreenHeight);

            gBufferDimensions = new Vector2(gBufferWidth, gBufferHeight);

            if (depthIDBuffer != null)
            {
                depthIDBuffer.Dispose();
            }
            depthIDBuffer = new RenderTarget2D(GraphicsDevice, gBufferWidth, gBufferHeight, 1,
                                               SurfaceFormat.HalfVector2, RenderTargetUsage.PreserveContents);

            // We'll prefer Rgba1010102 for normals + specular exponent, since we only need 3 components
            // and it's nice to have the extra precision for normals.  If you needed another component,
            // you could use Color and only use 8-bits for the normals.  Or if you wanted to check to see if
            // the GPU supports MRT with independed bit-depths, you could go with HalfVector4 and get
            // even more precision.
            SurfaceFormat   normalSpecularFormat = SurfaceFormat.Color;
            GraphicsAdapter adapter       = GraphicsAdapter.DefaultAdapter;
            SurfaceFormat   adapterFormat = adapter.CurrentDisplayMode.Format;

            if (adapter.CheckDeviceFormat(DeviceType.Hardware, adapterFormat, TextureUsage.None, QueryUsages.None,
                                          ResourceType.RenderTarget, SurfaceFormat.Rgba1010102))
            {
                normalSpecularFormat = SurfaceFormat.Rgba1010102;
            }

            if (normalSpecularBuffer != null)
            {
                normalSpecularBuffer.Dispose();
            }
            normalSpecularBuffer = new RenderTarget2D(GraphicsDevice, gBufferWidth, gBufferHeight, 1,
                                                      normalSpecularFormat, RenderTargetUsage.PreserveContents);

            if (lightBuffer != null)
            {
                lightBuffer.Dispose();
            }
            lightBuffer = new RenderTarget2D(GraphicsDevice, gBufferWidth, gBufferHeight, 1,
                                             SurfaceFormat.HalfVector4, RenderTargetUsage.PreserveContents);

            if (colorBuffer != null)
            {
                colorBuffer.Dispose();
            }

            if (enableMSAA)
            {
                colorBuffer = new RenderTarget2D(GraphicsDevice, ScreenWidth, ScreenHeight, 1,
                                                 SurfaceFormat.Color, MultiSampleType.FourSamples, 0,
                                                 RenderTargetUsage.PreserveContents);
            }
            else
            {
                colorBuffer = new RenderTarget2D(GraphicsDevice, ScreenWidth, ScreenHeight, 1,
                                                 SurfaceFormat.Color, RenderTargetUsage.PreserveContents);
            }

            nonMSAADS = GraphicsDevice.DepthStencilBuffer;

            if (msaaDS != null && msaaDS != nonMSAADS)
            {
                msaaDS.Dispose();
            }
            if (enableMSAA)
            {
                msaaDS = new DepthStencilBuffer(GraphicsDevice, ScreenWidth, ScreenHeight,
                                                DepthFormat.Depth24Stencil8,
                                                MultiSampleType.FourSamples, 0);
            }
            else
            {
                msaaDS = nonMSAADS;
            }

            if (shadowMap != null)
            {
                shadowMap.Dispose();
            }
            shadowMap = new RenderTarget2D(GraphicsDevice, (int)shadowMapSize, (int)shadowMapSize,
                                           1, SurfaceFormat.Single, RenderTargetUsage.PreserveContents);

            if (shadowMapDS != null)
            {
                shadowMapDS.Dispose();
            }
            shadowMapDS = new DepthStencilBuffer(GraphicsDevice, (int)shadowMapSize, (int)shadowMapSize, DepthFormat.Depth24Stencil8);
        }
Exemple #24
0
        /// <summary>
        /// Resizes the GBuffer.
        /// </summary>
        internal void Resize(Device device)
        {
            UpdateProjectionMatrix();
            RebuildFrustum(_frustumCulling);

            CanRender = false;

            var newWidth  = Math.Max(1, (int)_viewportSize.x);
            var newHeight = Math.Max(1, (int)_viewportSize.y);

            RenderTarget2D?.Dispose();
            RenderTarget2D = null;

            Renderer.Context2D.Target = null;

            _surface?.Dispose();
            BackBuffer?.Dispose();
            Buffer0?.Dispose();
            Buffer1?.Dispose();
            Buffer2?.Dispose();
            Buffer3?.Dispose();
            DepthStencilBuffer?.Dispose();

            Viewport = new ViewportF(0, 0, ViewportSize.x, ViewportSize.y);

            try
            {
                SwapChain.ResizeBuffers(RenderConstants.FrameCount, newWidth, newHeight,
                                        RenderConstants.BackBufferFormat, SwapChainFlags.None);
            }
            catch (SharpDXException e)
            {
                Logger.Error(e);
                throw;
            }

            var backBufferTexture = Resource.FromSwapChain <Texture2D>(SwapChain, 0);

            BackBuffer = new Texture(backBufferTexture);

            Buffer0 = CreateCameraBuffer("Buffer0", 1f, RenderConstants.BackBufferFormat);
            Buffer1 = CreateCameraBuffer("Buffer1", 1f, Format.R8G8B8A8_UNorm);
            Buffer2 = CreateCameraBuffer("Buffer2", 1f, Format.R16G16B16A16_Float);
            Buffer3 = CreateCameraBuffer("Buffer3", 1f, Format.R8G8B8A8_UNorm);
            BufferDownscaleHalf0    = CreateCameraBuffer("BufferDownsampleHalf0", 1 / 2f, Format.R8G8B8A8_UNorm);
            BufferDownscaleHalf1    = CreateCameraBuffer("BufferDownsampleHalf1", 1 / 2f, Format.R8G8B8A8_UNorm);
            BufferDownscaleQuarter0 = CreateCameraBuffer("BufferDownsampleQuarter0", 1 / 4f, Format.R8G8B8A8_UNorm);
            BufferDownscaleQuarter1 = CreateCameraBuffer("BufferDownsampleQuarter1", 1 / 4f, Format.R8G8B8A8_UNorm);

            var depthStencilBuffer = new Texture2D(Renderer.Device, new Texture2DDescription
            {
                Format            = Format.R24G8_Typeless,
                ArraySize         = 1,
                MipLevels         = 1,
                Width             = newWidth,
                Height            = newHeight,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Default,
                BindFlags         = BindFlags.DepthStencil | BindFlags.ShaderResource,
                CpuAccessFlags    = CpuAccessFlags.None,
                OptionFlags       = ResourceOptionFlags.None
            });

            DepthStencilBuffer = new Texture(depthStencilBuffer, new DepthStencilViewDescription
            {
                Format    = Format.D24_UNorm_S8_UInt,
                Dimension = DepthStencilViewDimension.Texture2D
            }, new ShaderResourceViewDescription
            {
                Format    = Format.R24_UNorm_X8_Typeless,
                Dimension = ShaderResourceViewDimension.Texture2D,
                Texture2D = new ShaderResourceViewDescription.Texture2DResource
                {
                    MostDetailedMip = 0,
                    MipLevels       = -1
                }
            });

            _surface           = BackBuffer.QueryInterface <Surface>();
            _surface.DebugName = "BackBufferSurface";

            try
            {
                RenderTarget2D = new Bitmap1(Renderer.Context2D, _surface,
                                             new BitmapProperties1(new PixelFormat(_surface.Description.Format, AlphaMode.Premultiplied), 96,
                                                                   96, BitmapOptions.Target | BitmapOptions.CannotDraw));
            }
            catch (Exception)
            {
                _surface.Dispose();
                RenderTarget2D?.Dispose();
                Logger.Info("An exception occured while creating the GUI surface render target.");
                throw;
            }

            lock (Locker)
            {
                foreach (var collector in LayerCollectors)
                {
                    collector.Target = RenderTarget2D;
                }
            }

            lock (PostEffectsLocker)
            {
                foreach (var effect in PostEffects)
                {
                    effect.UpdateSize(this);
                }
            }

            var cb = Constants;

            cb.Data.ScreenParams.X = newWidth;
            cb.Data.ScreenParams.Y = newHeight;
            cb.Data.ScreenParams.Z = AspectRatio;
            cb.Data.ScreenParams.W = FieldOfView * 0.5f / newHeight;

            NeedsResize = false;
            CanRender   = true;
            //Logger.Trace("Camera resized.");
        }
 protected override void ResizeDepthStencilBuffer(int width, int height)
 {
     DepthStencilBuffer.Dispose();
     DepthStencilBuffer = CreateDepthStencilBuffer();
 }
        protected virtual void OnResize()
        {
            Debug.Assert(Device != null);
            Debug.Assert(SwapChain != null);
            Debug.Assert(DirectCmdListAlloc != null);

            // Flush before changing any resources.
            FlushCommandQueue();

            CommandList.Reset(DirectCmdListAlloc, null);

            // Release the previous resources we will be recreating.
            foreach (Resource buffer in _swapChainBuffers)
            {
                buffer?.Dispose();
            }
            DepthStencilBuffer?.Dispose();

            // Resize the swap chain.
            SwapChain.ResizeBuffers(
                SwapChainBufferCount,
                ClientWidth, ClientHeight,
                BackBufferFormat,
                SwapChainFlags.AllowModeSwitch);

            CpuDescriptorHandle rtvHeapHandle = RtvHeap.CPUDescriptorHandleForHeapStart;

            for (int i = 0; i < SwapChainBufferCount; i++)
            {
                Resource backBuffer = SwapChain.GetBackBuffer <Resource>(i);
                _swapChainBuffers[i] = backBuffer;
                Device.CreateRenderTargetView(backBuffer, null, rtvHeapHandle);
                rtvHeapHandle += RtvDescriptorSize;
            }

            // Create the depth/stencil buffer and view.
            var depthStencilDesc = new ResourceDescription
            {
                Dimension         = ResourceDimension.Texture2D,
                Alignment         = 0,
                Width             = ClientWidth,
                Height            = ClientHeight,
                DepthOrArraySize  = 1,
                MipLevels         = 1,
                Format            = Format.R24G8_Typeless,
                SampleDescription = new SampleDescription
                {
                    Count   = MsaaCount,
                    Quality = MsaaQuality
                },
                Layout = TextureLayout.Unknown,
                Flags  = ResourceFlags.AllowDepthStencil
            };
            var optClear = new ClearValue
            {
                Format       = DepthStencilFormat,
                DepthStencil = new DepthStencilValue
                {
                    Depth   = 1.0f,
                    Stencil = 0
                }
            };

            DepthStencilBuffer = Device.CreateCommittedResource(
                new HeapProperties(HeapType.Default),
                HeapFlags.None,
                depthStencilDesc,
                ResourceStates.Common,
                optClear);

            var depthStencilViewDesc = new DepthStencilViewDescription
            {
                Dimension = DepthStencilViewDimension.Texture2D,
                Format    = DepthStencilFormat
            };
            // Create descriptor to mip level 0 of entire resource using a depth stencil format.
            CpuDescriptorHandle dsvHeapHandle = DsvHeap.CPUDescriptorHandleForHeapStart;

            Device.CreateDepthStencilView(DepthStencilBuffer, depthStencilViewDesc, dsvHeapHandle);

            // Transition the resource from its initial state to be used as a depth buffer.
            CommandList.ResourceBarrierTransition(DepthStencilBuffer, ResourceStates.Common, ResourceStates.DepthWrite);

            // Execute the resize commands.
            CommandList.Close();
            CommandQueue.ExecuteCommandList(CommandList);

            // Wait until resize is complete.
            FlushCommandQueue();

            Viewport         = new ViewportF(0, 0, ClientWidth, ClientHeight, 0.0f, 1.0f);
            ScissorRectangle = new RectangleF(0, 0, ClientWidth, ClientHeight);
        }
Exemple #27
0
 protected void DisposeDepthStencilBuffer()
 {
     DepthStencilBuffer.Dispose();
     DepthStencilBuffer = null;
 }