From b55f6bd3460679634a1778f7d04f2acf99953703 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 16 Jan 2022 01:18:44 +0100 Subject: [PATCH] Add ReadWriteTexture3D.ReadOnly type --- .../Resources/Abstract/Texture3D{T}.cs | 5 + .../ReadWriteTexture2D{T,TPixel}.ReadOnly.cs | 5 +- .../ReadWriteTexture3D{T,TPixel}.ReadOnly.cs | 170 ++++++++++++++++++ .../Resources/ReadWriteTexture3D{T,TPixel}.cs | 2 +- 4 files changed, 180 insertions(+), 2 deletions(-) create mode 100644 src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.ReadOnly.cs diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 32c2d09d..cc6ecada 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -166,6 +166,11 @@ 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. /// diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs index 5b06bcc9..76ffc78b 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs @@ -81,7 +81,10 @@ partial class ReadWriteTexture2D /// private sealed unsafe class ReadOnly : NativeObject, IReadOnlyTexture2D, GraphicsResourceHelper.IGraphicsResource { - private ReadWriteTexture2D owner; + /// + /// The owning instance being wrapped. + /// + private readonly ReadWriteTexture2D owner; /// /// The instance for the current resource. diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.ReadOnly.cs new file mode 100644 index 00000000..dcffe937 --- /dev/null +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.ReadOnly.cs @@ -0,0 +1,170 @@ +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 ReadWriteTexture3D +{ + /// + /// 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 IReadOnlyTexture3D 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; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + static void InitializeWrapper(ReadWriteTexture3D texture) + { + texture.readOnlyWrapper = new ReadOnly(texture); + } + + InitializeWrapper(this); + + goto GetWrapperAndReturnIfNotNull; + } + + /// + protected override void OnDispose() + { + base.OnDispose(); + + this.readOnlyWrapper?.Dispose(); + } + + /// + /// A wrapper for a resource that has been temporarily transitioned to readonly. + /// + private sealed unsafe class ReadOnly : NativeObject, IReadOnlyTexture3D, GraphicsResourceHelper.IGraphicsResource + { + /// + /// The owning instance being wrapped. + /// + private readonly ReadWriteTexture3D 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(ReadWriteTexture3D 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, int z] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture3D.ReadOnly)}[{typeof(int)}, {typeof(int)}, {typeof(int)}]"); + + /// + public TPixel this[Int3 xyz] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture3D.ReadOnly)}[{typeof(Int3)}]"); + + /// + public TPixel this[float u, float v, float w] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture3D.ReadOnly)}[{typeof(float)}, {typeof(float)}, {typeof(float)}]"); + + /// + public TPixel this[Float3 uvw] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture3D.ReadOnly)}[{typeof(Float3)}]"); + + /// + public int Width => this.owner.Width; + + /// + public int Height => this.owner.Height; + + /// + public int Depth => this.owner.Depth; + + /// + 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/ReadWriteTexture3D{T,TPixel}.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.cs index 3007240b..492c6fd6 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.cs @@ -14,7 +14,7 @@ namespace ComputeSharp; /// The type of pixels used on the GPU side. [DebuggerTypeProxy(typeof(Texture3DDebugView<>))] [DebuggerDisplay("{ToString(),raw}")] -public sealed class ReadWriteTexture3D : Texture3D, IReadWriteTexture3D +public sealed partial class ReadWriteTexture3D : Texture3D, IReadWriteTexture3D where T : unmanaged, IPixel where TPixel : unmanaged {