diff --git a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs index 393fe304..47d37914 100644 --- a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs +++ b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs @@ -151,6 +151,7 @@ public static partial class GraphicsDeviceExtensions Guard.IsNotNull(device); device.ThrowIfDisposed(); + device.ThrowIfDeviceLost(); return new(device); } diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.DeviceLost.cs b/src/ComputeSharp/Graphics/GraphicsDevice.DeviceLost.cs index e5f077f7..2e67481e 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.DeviceLost.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.DeviceLost.cs @@ -21,6 +21,28 @@ unsafe partial class GraphicsDevice private static readonly WaitForSingleObjectCallbackDelegate WaitForSingleObjectCallbackForRegisterDeviceLostCallbackWrapper = WaitForSingleObjectCallbackForWaitForFenceAsync; #endif + /// + /// Throws an if the current device has been lost. + /// + /// Thrown if the current device has been lost. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal void ThrowIfDeviceLost() + { + // This method is called as a check before performing any operations. In order to minimize overhead, + // the GetDeviceRemovedReason() method is not called here, and the local field is just used instead. + // That field is set by the device lost callback anyway, and the extra precision of calling the API + // explicitly is not needed since there are no thread safety guarantees in this scenario anyway. + // That is, the check isn't meant to cover 100% of cases, as a device might be lost right after this + // check is performed anyway. This check is only meant to catch the common scenarios to help users. + // For the same reason, this method doesn't have to worry about raising the event either. + if (Volatile.Read(ref Unsafe.As(ref this.deviceRemovedReason)) != S.S_OK) + { + static void Throw(object self) => throw new InvalidOperationException($"The device \"{self}\" has been lost and can no longer be used."); + + Throw(this); + } + } + /// /// Raises the event if needed. /// diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index 2abc731f..6e7ad77e 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -234,6 +234,7 @@ public sealed unsafe partial class GraphicsDevice : NativeObject public bool IsDoublePrecisionSupportAvailable() { ThrowIfDisposed(); + ThrowIfDeviceLost(); var d3D12OptionsData = this.d3D12Device.Get()->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS); @@ -250,6 +251,7 @@ public sealed unsafe partial class GraphicsDevice : NativeObject where T : unmanaged { ThrowIfDisposed(); + ThrowIfDeviceLost(); return this.d3D12Device.Get()->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE2D); } @@ -264,6 +266,7 @@ public sealed unsafe partial class GraphicsDevice : NativeObject where T : unmanaged { ThrowIfDisposed(); + ThrowIfDeviceLost(); return this.d3D12Device.Get()->IsDxgiFormatSupported( DXGIFormatHelper.GetForType(), @@ -280,6 +283,7 @@ public sealed unsafe partial class GraphicsDevice : NativeObject where T : unmanaged { ThrowIfDisposed(); + ThrowIfDeviceLost(); return this.d3D12Device.Get()->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE3D); } @@ -294,6 +298,7 @@ public sealed unsafe partial class GraphicsDevice : NativeObject where T : unmanaged { ThrowIfDisposed(); + ThrowIfDeviceLost(); return this.d3D12Device.Get()->IsDxgiFormatSupported( DXGIFormatHelper.GetForType(), diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index 4d5b5561..b830a105 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -65,6 +65,7 @@ public unsafe abstract class Buffer : NativeObject, IGraphicsResource private protected Buffer(GraphicsDevice device, int length, uint elementSizeInBytes, ResourceType resourceType, AllocationMode allocationMode) { device.ThrowIfDisposed(); + device.ThrowIfDeviceLost(); if (resourceType == ResourceType.Constant) { diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index 98c99bdf..c2693cea 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -33,6 +33,7 @@ public abstract class StructuredBuffer : Buffer internal override unsafe void CopyTo(ref T destination, int sourceOffset, int count) { GraphicsDevice.ThrowIfDisposed(); + GraphicsDevice.ThrowIfDeviceLost(); ThrowIfDisposed(); @@ -104,6 +105,7 @@ public abstract class StructuredBuffer : Buffer internal override unsafe void CopyTo(Buffer destination, int sourceOffset, int destinationOffset, int count) { GraphicsDevice.ThrowIfDisposed(); + GraphicsDevice.ThrowIfDeviceLost(); ThrowIfDisposed(); @@ -148,6 +150,7 @@ public abstract class StructuredBuffer : Buffer internal unsafe void CopyTo(ReadBackBuffer destination, int sourceOffset, int destinationOffset, int count) { GraphicsDevice.ThrowIfDisposed(); + GraphicsDevice.ThrowIfDeviceLost(); ThrowIfDisposed(); @@ -191,6 +194,7 @@ public abstract class StructuredBuffer : Buffer internal override unsafe void CopyFrom(ref T source, int offset, int length) { GraphicsDevice.ThrowIfDisposed(); + GraphicsDevice.ThrowIfDeviceLost(); ThrowIfDisposed(); @@ -266,6 +270,7 @@ public abstract class StructuredBuffer : Buffer internal unsafe void CopyFrom(UploadBuffer source, int sourceOffset, int destinationOffset, int count) { GraphicsDevice.ThrowIfDisposed(); + GraphicsDevice.ThrowIfDeviceLost(); ThrowIfDisposed(); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index dacc203e..e6190b93 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -73,6 +73,7 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra private protected Texture2D(GraphicsDevice device, int width, int height, ResourceType resourceType, AllocationMode allocationMode, D3D12_FORMAT_SUPPORT1 d3D12FormatSupport) { device.ThrowIfDisposed(); + device.ThrowIfDeviceLost(); Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION); Guard.IsBetweenOrEqualTo(height, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION); @@ -169,6 +170,7 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra internal void CopyTo(ref T destination, int size, int sourceOffsetX, int sourceOffsetY, int width, int height) { GraphicsDevice.ThrowIfDisposed(); + GraphicsDevice.ThrowIfDeviceLost(); ThrowIfDisposed(); @@ -258,6 +260,7 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra internal void CopyTo(Texture2D destination, int sourceOffsetX, int sourceOffsetY, int destinationOffsetX, int destinationOffsetY, int width, int height) { GraphicsDevice.ThrowIfDisposed(); + GraphicsDevice.ThrowIfDeviceLost(); ThrowIfDisposed(); @@ -326,6 +329,7 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra internal void CopyTo(ReadBackTexture2D destination, int sourceOffsetX, int sourceOffsetY, int destinationOffsetX, int destinationOffsetY, int width, int height) { GraphicsDevice.ThrowIfDisposed(); + GraphicsDevice.ThrowIfDeviceLost(); ThrowIfDisposed(); @@ -389,6 +393,7 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra internal void CopyFrom(ref T source, int size, int destinationOffsetX, int destinationOffsetY, int width, int height) { GraphicsDevice.ThrowIfDisposed(); + GraphicsDevice.ThrowIfDeviceLost(); ThrowIfDisposed(); @@ -476,6 +481,7 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra internal void CopyFrom(UploadTexture2D source, int sourceOffsetX, int sourceOffsetY, int destinationOffsetX, int destinationOffsetY, int width, int height) { GraphicsDevice.ThrowIfDisposed(); + GraphicsDevice.ThrowIfDeviceLost(); ThrowIfDisposed(); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 70e7361d..b12dc576 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -74,6 +74,7 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra private protected Texture3D(GraphicsDevice device, int width, int height, int depth, ResourceType resourceType, AllocationMode allocationMode, D3D12_FORMAT_SUPPORT1 d3D12FormatSupport) { device.ThrowIfDisposed(); + device.ThrowIfDeviceLost(); Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION); Guard.IsBetweenOrEqualTo(height, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION); @@ -181,6 +182,7 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra internal void CopyTo(ref T destination, int size, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int width, int height, int depth) { GraphicsDevice.ThrowIfDisposed(); + GraphicsDevice.ThrowIfDeviceLost(); ThrowIfDisposed(); @@ -279,6 +281,7 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra internal void CopyTo(Texture3D destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) { GraphicsDevice.ThrowIfDisposed(); + GraphicsDevice.ThrowIfDeviceLost(); ThrowIfDisposed(); @@ -356,6 +359,7 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra internal void CopyTo(ReadBackTexture3D destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) { GraphicsDevice.ThrowIfDisposed(); + GraphicsDevice.ThrowIfDeviceLost(); ThrowIfDisposed(); @@ -427,6 +431,7 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra internal void CopyFrom(ref T source, int size, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) { GraphicsDevice.ThrowIfDisposed(); + GraphicsDevice.ThrowIfDeviceLost(); ThrowIfDisposed(); @@ -523,6 +528,7 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra internal void CopyFrom(UploadTexture3D source, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) { GraphicsDevice.ThrowIfDisposed(); + GraphicsDevice.ThrowIfDeviceLost(); ThrowIfDisposed(); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs index edd1d028..8e42995a 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs @@ -45,6 +45,7 @@ public abstract unsafe class TransferBuffer : NativeObject, IGraphicsResource private protected TransferBuffer(GraphicsDevice device, int length, ResourceType resourceType, AllocationMode allocationMode) { device.ThrowIfDisposed(); + device.ThrowIfDeviceLost(); // The maximum length is set such that the aligned buffer size can't exceed uint.MaxValue Guard.IsBetweenOrEqualTo(length, 1, (uint.MaxValue / (uint)sizeof(T)) & ~255); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs index ec77a7af..8e546be2 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs @@ -51,6 +51,7 @@ public unsafe abstract class TransferTexture2D : NativeObject, IGraphicsResou private protected TransferTexture2D(GraphicsDevice device, int width, int height, ResourceType resourceType, AllocationMode allocationMode) { device.ThrowIfDisposed(); + device.ThrowIfDeviceLost(); Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION); Guard.IsBetweenOrEqualTo(height, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs index e4f48331..72201ccb 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs @@ -52,6 +52,7 @@ public unsafe abstract class TransferTexture3D : NativeObject, IGraphicsResou private protected TransferTexture3D(GraphicsDevice device, int width, int height, int depth, ResourceType resourceType, AllocationMode allocationMode) { device.ThrowIfDisposed(); + device.ThrowIfDeviceLost(); Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION); Guard.IsBetweenOrEqualTo(height, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION); diff --git a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs index e08040ea..c51fca8b 100644 --- a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs @@ -58,6 +58,7 @@ public sealed class ConstantBuffer : Buffer internal override unsafe void CopyTo(ref T destination, int offset, int length) { GraphicsDevice.ThrowIfDisposed(); + GraphicsDevice.ThrowIfDeviceLost(); ThrowIfDisposed(); @@ -84,6 +85,7 @@ public sealed class ConstantBuffer : Buffer internal override unsafe void CopyTo(Buffer destination, int sourceOffset, int destinationOffset, int length) { GraphicsDevice.ThrowIfDisposed(); + GraphicsDevice.ThrowIfDeviceLost(); ThrowIfDisposed(); @@ -118,6 +120,7 @@ public sealed class ConstantBuffer : Buffer internal override unsafe void CopyFrom(ref T source, int offset, int length) { GraphicsDevice.ThrowIfDisposed(); + GraphicsDevice.ThrowIfDeviceLost(); ThrowIfDisposed(); diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs index 55cb5933..dee9a948 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs @@ -25,6 +25,7 @@ partial class ReadWriteTexture2D internal IReadOnlyNormalizedTexture2D AsReadOnly() { GraphicsDevice.ThrowIfDisposed(); + GraphicsDevice.ThrowIfDeviceLost(); ThrowIfDisposed(); ThrowIfIsNotInReadOnlyState(); diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T}.ReadOnly.cs index fdb58dd9..12c4c5ec 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T}.ReadOnly.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T}.ReadOnly.cs @@ -25,6 +25,7 @@ partial class ReadWriteTexture2D public IReadOnlyTexture2D AsReadOnly() { GraphicsDevice.ThrowIfDisposed(); + GraphicsDevice.ThrowIfDeviceLost(); ThrowIfDisposed(); ThrowIfIsNotInReadOnlyState(); diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.ReadOnly.cs index 270c7135..f9e7ccfb 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.ReadOnly.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.ReadOnly.cs @@ -25,6 +25,7 @@ partial class ReadWriteTexture3D public IReadOnlyNormalizedTexture3D AsReadOnly() { GraphicsDevice.ThrowIfDisposed(); + GraphicsDevice.ThrowIfDeviceLost(); ThrowIfDisposed(); ThrowIfIsNotInReadOnlyState(); diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T}.ReadOnly.cs index 5d38517d..c825d5f7 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T}.ReadOnly.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T}.ReadOnly.cs @@ -25,6 +25,7 @@ partial class ReadWriteTexture3D public IReadOnlyTexture3D AsReadOnly() { GraphicsDevice.ThrowIfDisposed(); + GraphicsDevice.ThrowIfDeviceLost(); ThrowIfDisposed(); ThrowIfIsNotInReadOnlyState();