diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs index bd17555d..162c7fb6 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs @@ -53,9 +53,6 @@ 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, control goes directly to the - // helper that will initialize and return it, which translates to a single jump. [MethodImpl(MethodImplOptions.NoInlining)] static ReadOnly InitializeWrapper(ReadWriteTexture2D texture) { diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T}.ReadOnly.cs new file mode 100644 index 00000000..b8207acd --- /dev/null +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T}.ReadOnly.cs @@ -0,0 +1,168 @@ +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. + /// + /// This is an advanced API that can only be used after the current instance has been transitioned to be in a readonly state. To do so, + /// use , + /// and specify . After that, can be used to get a readonly wrapper for + /// the current texture to use in a shader. This instance should not be cached or reused, but just passed directly to a shader + /// being dispatched through that same , as it will not work if the texture changes state later on. + /// Before the end of that list of operations, the texture also needs to be transitioned back to writeable state, using the same + /// API as before but specifying . Failing to do so results in undefined behavior. + /// + /// + /// Thrown if the current instance or its associated device are disposed. + /// Thrown if the current instance is not in a readonly state. + public IReadOnlyTexture2D AsReadOnly() + { + GraphicsDevice.ThrowIfDisposed(); + + ThrowIfDisposed(); + ThrowIfIsNotInReadOnlyState(); + + 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, control goes directly to the + // helper that will initialize and return it, which translates to a single jump. + [MethodImpl(MethodImplOptions.NoInlining)] + static ReadOnly InitializeWrapper(ReadWriteTexture2D texture) + { + return texture.readOnlyWrapper = new(texture); + } + + return InitializeWrapper(this); + } + + /// + 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, IReadOnlyTexture2D, GraphicsResourceHelper.IGraphicsResource + { + /// + /// The owning instance being wrapped. + /// + private readonly 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 ref readonly T this[int x, int y] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture2D.ReadOnly)}[{typeof(int)}, {typeof(int)}]"); + + /// + public ref readonly T this[Int2 xy] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture2D.ReadOnly)}[{typeof(Int2)}]"); + + /// + public ref readonly T this[float u, float v] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture2D.ReadOnly)}[{typeof(float)}, {typeof(float)}]"); + + /// + public ref readonly T 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}.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T}.cs index f341e786..d4e055ff 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T}.cs @@ -13,7 +13,7 @@ namespace ComputeSharp; /// The type of items stored on the texture. [DebuggerTypeProxy(typeof(Texture2DDebugView<>))] [DebuggerDisplay("{ToString(),raw}")] -public sealed class ReadWriteTexture2D : Texture2D +public sealed partial class ReadWriteTexture2D : Texture2D where T : unmanaged { /// diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T}.ReadOnly.cs new file mode 100644 index 00000000..0aa28c65 --- /dev/null +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T}.ReadOnly.cs @@ -0,0 +1,168 @@ +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. + /// + /// This is an advanced API that can only be used after the current instance has been transitioned to be in a readonly state. To do so, + /// use , + /// and specify . After that, can be used to get a readonly wrapper for + /// the current texture to use in a shader. This instance should not be cached or reused, but just passed directly to a shader + /// being dispatched through that same , as it will not work if the texture changes state later on. + /// Before the end of that list of operations, the texture also needs to be transitioned back to writeable state, using the same + /// API as before but specifying . Failing to do so results in undefined behavior. + /// + /// + /// Thrown if the current instance or its associated device are disposed. + /// Thrown if the current instance is not in a readonly state. + public IReadOnlyTexture3D AsReadOnly() + { + GraphicsDevice.ThrowIfDisposed(); + + ThrowIfDisposed(); + ThrowIfIsNotInReadOnlyState(); + + ReadOnly? readOnlyWrapper = this.readOnlyWrapper; + + if (readOnlyWrapper is not null) + { + return readOnlyWrapper; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + static ReadOnly InitializeWrapper(ReadWriteTexture3D texture) + { + return texture.readOnlyWrapper = new(texture); + } + + return InitializeWrapper(this); + } + + /// + 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_TEXTURE3D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); + } + + /// + public ref readonly T this[int x, int y, int z] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture3D.ReadOnly)}[{typeof(int)}, {typeof(int)}, {typeof(int)}]"); + + /// + public ref readonly T this[Int3 xyz] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture3D.ReadOnly)}[{typeof(Int3)}]"); + + /// + public ref readonly T this[float u, float v, float w] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture3D.ReadOnly)}[{typeof(float)}, {typeof(float)}, {typeof(float)}]"); + + /// + public ref readonly T 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}.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T}.cs index 1e5e6f89..28f3e10f 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T}.cs @@ -13,7 +13,7 @@ namespace ComputeSharp; /// The type of items stored on the texture. [DebuggerTypeProxy(typeof(Texture3DDebugView<>))] [DebuggerDisplay("{ToString(),raw}")] -public sealed class ReadWriteTexture3D : Texture3D +public sealed partial class ReadWriteTexture3D : Texture3D where T : unmanaged { ///