diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index b77a16f6..87cdd28a 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -1,4 +1,5 @@ -using System.Runtime.CompilerServices; +using System; +using System.Runtime.CompilerServices; using ComputeSharp.__Internals; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Commands; @@ -156,11 +157,6 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra /// internal D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle => this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle; - /// - /// Gets whether or not the current resource is in readonly state. - /// - protected bool IsInReadOnlyState => this.d3D12ResourceState != D3D12_RESOURCE_STATE_UNORDERED_ACCESS; - /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. /// @@ -546,6 +542,25 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra } } + /// + /// Throws an if the current resource is not in a readonly state. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + protected void ThrowIfIsNotInReadOnlyState() + { + if (this.d3D12ResourceState == D3D12_RESOURCE_STATE_UNORDERED_ACCESS) + { + static void Throw() + { + throw new InvalidOperationException( + "The texture is not currently in readonly mode. This API can only be used when creating a compute graph with " + + "the ComputeContext type, and after having used ComputeContext.Transition() to change the state of the texture."); + } + + Throw(); + } + } + /// /// Throws a if the target device doesn't match the current one. /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index cc6ecada..ac0c4265 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -1,4 +1,5 @@ -using System.Runtime.CompilerServices; +using System; +using System.Runtime.CompilerServices; using ComputeSharp.__Internals; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Commands; @@ -166,11 +167,6 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra /// internal D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle => this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle; - /// - /// Gets whether or not the current resource is in readonly state. - /// - protected bool IsInReadOnlyState => this.d3D12ResourceState != D3D12_RESOURCE_STATE_UNORDERED_ACCESS; - /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. /// @@ -599,6 +595,25 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra } } + /// + /// Throws an if the current resource is not in a readonly state. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + protected void ThrowIfIsNotInReadOnlyState() + { + if (this.d3D12ResourceState == D3D12_RESOURCE_STATE_UNORDERED_ACCESS) + { + static void Throw() + { + throw new InvalidOperationException( + "The texture is not currently in readonly mode. This API can only be used when creating a compute graph with " + + "the ComputeContext type, and after having used ComputeContext.Transition() to change the state of the texture."); + } + + Throw(); + } + } + /// /// Throws a if the target device doesn't match the current one. /// diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs index 76ffc78b..da053be2 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs @@ -31,20 +31,7 @@ partial class ReadWriteTexture2D GraphicsDevice.ThrowIfDisposed(); ThrowIfDisposed(); - - if (!IsInReadOnlyState) - { - static void Throw() - { - throw new InvalidOperationException( - "The texture is not currently in readonly mode. This API can only be used when creating a compute graph with " + - "the ComputeContext type, and after having used ComputeContext.Transition() to change the state of the texture."); - } - - Throw(); - } - - GetWrapperAndReturnIfNotNull: + ThrowIfIsNotInReadOnlyState(); ReadOnly? readOnlyWrapper = this.readOnlyWrapper; @@ -53,19 +40,16 @@ partial class ReadWriteTexture2D return readOnlyWrapper; } - // The wrapper initialization is moved to a non inlined helper to keep the codegen of this - // method as compact as possible. If the current wrapper is not initialized, a new one is - // created, then code jumps back to the check. This allows the method to have a single exit - // point, which further improves the final codegen. + // The wrapper initialization is moved to a non inlined helper to keep the codegen of this method + // as compact as possible. If the current wrapper is not initialized, control goes directly to the + // helper that will initialize and return it, which translates to a single jump. [MethodImpl(MethodImplOptions.NoInlining)] - static void InitializeWrapper(ReadWriteTexture2D texture) + static ReadOnly InitializeWrapper(ReadWriteTexture2D texture) { - texture.readOnlyWrapper = new ReadOnly(texture); + return texture.readOnlyWrapper = new(texture); } - InitializeWrapper(this); - - goto GetWrapperAndReturnIfNotNull; + return InitializeWrapper(this); } /// diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.ReadOnly.cs index dcffe937..287efcb1 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.ReadOnly.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.ReadOnly.cs @@ -31,20 +31,7 @@ partial class ReadWriteTexture3D GraphicsDevice.ThrowIfDisposed(); ThrowIfDisposed(); - - if (!IsInReadOnlyState) - { - static void Throw() - { - throw new InvalidOperationException( - "The texture is not currently in readonly mode. This API can only be used when creating a compute graph with " + - "the ComputeContext type, and after having used ComputeContext.Transition() to change the state of the texture."); - } - - Throw(); - } - - GetWrapperAndReturnIfNotNull: + ThrowIfIsNotInReadOnlyState(); ReadOnly? readOnlyWrapper = this.readOnlyWrapper; @@ -54,14 +41,12 @@ partial class ReadWriteTexture3D } [MethodImpl(MethodImplOptions.NoInlining)] - static void InitializeWrapper(ReadWriteTexture3D texture) + static ReadOnly InitializeWrapper(ReadWriteTexture3D texture) { - texture.readOnlyWrapper = new ReadOnly(texture); + return texture.readOnlyWrapper = new(texture); } - InitializeWrapper(this); - - goto GetWrapperAndReturnIfNotNull; + return InitializeWrapper(this); } ///