/// <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; } }
/// <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."); }