diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index 4b28afdc..b77a16f6 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -156,6 +156,11 @@ 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. /// @@ -582,6 +587,13 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra D3D12_RESOURCE_STATES d3D12ResourceStatesBefore = this.d3D12ResourceState; D3D12_RESOURCE_STATES d3D12ResourceStatesAfter = ResourceStateHelper.GetD3D12ResourceStates(resourceState); + // If the original state is COMMON, at the point where the transition is inserted into a ComputeContext the resource + // would have already been implicitly promoted to be NON_PIXEL_SHADER_RESOURCE. Because of this, adjust this transition. + if (d3D12ResourceStatesBefore == D3D12_RESOURCE_STATE_COMMON) + { + d3D12ResourceStatesBefore = D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE; + } + this.d3D12ResourceState = d3D12ResourceStatesAfter; d3D12Resource = D3D12Resource; diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs new file mode 100644 index 00000000..ab100627 --- /dev/null +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs @@ -0,0 +1,160 @@ +using System; +using System.Runtime.CompilerServices; +using ComputeSharp.__Internals; +using ComputeSharp.Exceptions; +using ComputeSharp.Graphics.Commands.Interop; +using ComputeSharp.Graphics.Extensions; +using ComputeSharp.Graphics.Helpers; +using ComputeSharp.Interop; +using TerraFX.Interop.DirectX; +using static TerraFX.Interop.DirectX.D3D12_SRV_DIMENSION; + +#pragma warning disable CS0618 + +namespace ComputeSharp; + +/// +partial class ReadWriteTexture2D +{ + /// + /// The wrapping instance, if available. + /// + private ReadOnly? readOnlyWrapper; + + /// + /// Retrieves a wrapping instance for the current resource. + /// + /// An instance wrapping the current resource. + /// The returned instance can be used in a shader to enable texture sampling. + public IReadOnlyTexture2D AsReadOnly() + { + 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: + + ReadOnly? readOnlyWrapper = this.readOnlyWrapper; + + if (readOnlyWrapper is not null) + { + 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. + [MethodImpl(MethodImplOptions.NoInlining)] + static void InitializeWrapper(ReadWriteTexture2D texture) + { + texture.readOnlyWrapper = new ReadOnly(texture); + } + + InitializeWrapper(this); + + goto GetWrapperAndReturnIfNotNull; + } + + /// + /// A wrapper for a resource that has been temporarily transitioned to readonly. + /// + private sealed unsafe class ReadOnly : NativeObject, IReadOnlyTexture2D, GraphicsResourceHelper.IGraphicsResource + { + private ReadWriteTexture2D owner; + + /// + /// The instance for the current resource. + /// + private readonly ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles; + + /// + /// Creates a new instance with the specified parameters. + /// + /// The owning instance to wrap. + public ReadOnly(ReadWriteTexture2D owner) + { + this.owner = owner; + + owner.GraphicsDevice.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles); + + owner.GraphicsDevice.D3D12Device->CreateShaderResourceView(owner.D3D12Resource, DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); + } + + /// + public TPixel this[int x, int y] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture2D.ReadOnly)}[{typeof(int)}, {typeof(int)}]"); + + /// + public TPixel this[Int2 xy] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture2D.ReadOnly)}[{typeof(Int2)}]"); + + /// + public TPixel this[float u, float v] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture2D.ReadOnly)}[{typeof(float)}, {typeof(float)}]"); + + /// + public TPixel this[Float2 uv] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture2D.ReadOnly)}[{typeof(Float2)}]"); + + /// + public int Width => this.owner.Width; + + /// + public int Height => this.owner.Height; + + /// + public GraphicsDevice GraphicsDevice => this.owner.GraphicsDevice; + + /// + D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) + { + ThrowIfDisposed(); + + this.owner.ThrowIfDisposed(); + this.owner.ThrowIfDeviceMismatch(device); + + return this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle; + } + + /// + (D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) + { + throw new NotSupportedException("This operation cannot be performaned on a readonly wrapper."); + } + + /// + ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device) + { + ThrowIfDisposed(); + + this.owner.ThrowIfDisposed(); + this.owner.ThrowIfDeviceMismatch(device); + + return this.owner.D3D12Resource; + } + + /// + (D3D12_RESOURCE_STATES, D3D12_RESOURCE_STATES) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource) + { + throw new NotSupportedException("This operation cannot be performaned on a readonly wrapper."); + } + + /// + protected override void OnDispose() + { + if (this.owner.GraphicsDevice is GraphicsDevice device) + { + device.ReturnShaderResourceViewDescriptorHandles(in this.d3D12ResourceDescriptorHandles); + } + } + } +} diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.cs index ac9b2214..4647cace 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.cs @@ -14,7 +14,7 @@ namespace ComputeSharp; /// The type of pixels used on the GPU side. [DebuggerTypeProxy(typeof(Texture2DDebugView<>))] [DebuggerDisplay("{ToString(),raw}")] -public sealed class ReadWriteTexture2D : Texture2D, IReadWriteTexture2D +public sealed partial class ReadWriteTexture2D : Texture2D, IReadWriteTexture2D where T : unmanaged, IPixel where TPixel : unmanaged {