From 8c2aedf054fed318d4813109d6ca6f67ab399a7f Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Sat, 4 May 2024 01:14:05 +0900 Subject: [PATCH] Work around D3D11 texture pointer getting lost before disposal --- src/Veldrid/D3D11/D3D11Texture.cs | 6 ++++++ src/Veldrid/D3D11/D3D11TextureView.cs | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/src/Veldrid/D3D11/D3D11Texture.cs b/src/Veldrid/D3D11/D3D11Texture.cs index 93f8d2531..1ea33c19a 100644 --- a/src/Veldrid/D3D11/D3D11Texture.cs +++ b/src/Veldrid/D3D11/D3D11Texture.cs @@ -145,6 +145,9 @@ public D3D11Texture(ID3D11Device device, ref TextureDescription description) DeviceTexture = device.CreateTexture3D(desc3D); } + + // See: https://github.com/ppy/veldrid/issues/53 + GC.SuppressFinalize(DeviceTexture); } public D3D11Texture(ID3D11Texture2D existingTexture, TextureType type, PixelFormat format) @@ -167,6 +170,9 @@ public D3D11Texture(ID3D11Texture2D existingTexture, TextureType type, PixelForm format, (Usage & TextureUsage.DepthStencil) == TextureUsage.DepthStencil); TypelessDxgiFormat = D3D11Formats.GetTypelessFormat(DxgiFormat); + + // See: https://github.com/ppy/veldrid/issues/53 + GC.SuppressFinalize(DeviceTexture); } private protected override TextureView CreateFullTextureView(GraphicsDevice gd) diff --git a/src/Veldrid/D3D11/D3D11TextureView.cs b/src/Veldrid/D3D11/D3D11TextureView.cs index c774f0e73..cfb498572 100644 --- a/src/Veldrid/D3D11/D3D11TextureView.cs +++ b/src/Veldrid/D3D11/D3D11TextureView.cs @@ -37,8 +37,12 @@ public D3D11TextureView(D3D11GraphicsDevice gd, ref TextureViewDescription descr description.BaseArrayLayer, description.ArrayLayers, Format); + ShaderResourceView = device.CreateShaderResourceView(d3dTex.DeviceTexture, srvDesc); + // See: https://github.com/ppy/veldrid/issues/53 + GC.SuppressFinalize(ShaderResourceView); + if ((d3dTex.Usage & TextureUsage.Storage) == TextureUsage.Storage) { var uavDesc = new UnorderedAccessViewDescription @@ -93,6 +97,9 @@ public D3D11TextureView(D3D11GraphicsDevice gd, ref TextureViewDescription descr } UnorderedAccessView = device.CreateUnorderedAccessView(d3dTex.DeviceTexture, uavDesc); + + // See: https://github.com/ppy/veldrid/issues/53 + GC.SuppressFinalize(UnorderedAccessView); } }