diff --git a/src/ComputeSharp/Graphics/Resources/ReadOnlyTexture1D{T,TPixel}.cs b/src/ComputeSharp/Graphics/Resources/ReadOnlyTexture1D{T,TPixel}.cs new file mode 100644 index 00000000..53315a0a --- /dev/null +++ b/src/ComputeSharp/Graphics/Resources/ReadOnlyTexture1D{T,TPixel}.cs @@ -0,0 +1,43 @@ +using System.Diagnostics; +using ComputeSharp.Exceptions; +using ComputeSharp.Graphics.Resources.Enums; +using ComputeSharp.Resources; +using ComputeSharp.Resources.Debug; +using static TerraFX.Interop.DirectX.D3D12_FORMAT_SUPPORT1; + +namespace ComputeSharp; + +/// +/// A representing a typed readonly 1D texture stored on GPU memory. +/// +/// The type of items stored on the texture. +/// The type of pixels used on the GPU side. +[DebuggerTypeProxy(typeof(Texture1DDebugView<>))] +[DebuggerDisplay("{ToString(),raw}")] +public sealed class ReadOnlyTexture1D : Texture1D, IReadOnlyNormalizedTexture1D + where T : unmanaged, IPixel + where TPixel : unmanaged +{ + /// + /// Creates a new instance with the specified parameters. + /// + /// The associated with the current instance. + /// The width of the texture. + /// The allocation mode to use for the new resource. + internal ReadOnlyTexture1D(GraphicsDevice device, int width, AllocationMode allocationMode) + : base(device, width, ResourceType.ReadOnly, allocationMode, D3D12_FORMAT_SUPPORT1_TEXTURE1D) + { + } + + /// + public ref readonly TPixel this[int x] => throw new InvalidExecutionContextException($"{typeof(ReadOnlyTexture1D)}[{typeof(int)}]"); + + /// + public ref readonly TPixel Sample(float u) => throw new InvalidExecutionContextException($"{typeof(ReadOnlyTexture1D)}.{nameof(Sample)}({typeof(float)})"); + + /// + public override string ToString() + { + return $"ComputeSharp.ReadOnlyTexture1D<{typeof(T)}, {typeof(TPixel)}>[{Width}]"; + } +} diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture1D{T,TPixel}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture1D{T,TPixel}.ReadOnly.cs new file mode 100644 index 00000000..7ed9ac15 --- /dev/null +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture1D{T,TPixel}.ReadOnly.cs @@ -0,0 +1,146 @@ +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 ReadWriteTexture1D +{ + /// + /// The wrapping instance, if available. + /// + private ReadOnly? readOnlyWrapper; + + /// + internal IReadOnlyNormalizedTexture1D AsReadOnly() + { + using var _0 = GraphicsDevice.GetReferenceTrackingLease(); + using var _1 = GetReferenceTrackingLease(); + + GraphicsDevice.ThrowIfDeviceLost(); + + ThrowIfIsNotInReadOnlyState(); + + ReadOnly? readOnlyWrapper = this.readOnlyWrapper; + + if (readOnlyWrapper is not null) + { + return readOnlyWrapper; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + static ReadOnly InitializeWrapper(ReadWriteTexture1D texture) + { + return texture.readOnlyWrapper = new(texture); + } + + return InitializeWrapper(this); + } + + /// + private 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, IReadOnlyNormalizedTexture1D, GraphicsResourceHelper.IGraphicsResource + { + /// + /// The owning instance being wrapped. + /// + private readonly ReadWriteTexture1D 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(ReadWriteTexture1D owner) + { + owner.DangerousAddRef(); + + this.owner = owner; + + owner.GraphicsDevice.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles); + + owner.GraphicsDevice.D3D12Device->CreateShaderResourceView(owner.D3D12Resource, DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE1D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); + } + + /// + public ref readonly TPixel this[int x] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture1D.ReadOnly)}[{typeof(int)}]"); + + /// + public ref readonly TPixel Sample(float u) => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture1D.ReadOnly)}.{nameof(Sample)}({typeof(float)})"); + + /// + public int Width => this.owner.Width; + + /// + public GraphicsDevice GraphicsDevice => this.owner.GraphicsDevice; + + /// + D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) + { + using var _0 = GetReferenceTrackingLease(); + using var _1 = this.owner.GetReferenceTrackingLease(); + + 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, out Lease lease) + { + lease = GetReferenceTrackingLease(); + + using var _1 = this.owner.GetReferenceTrackingLease(); + + this.owner.ThrowIfDeviceMismatch(device); + + return this.owner.D3D12Resource; + } + + /// + (D3D12_RESOURCE_STATES, D3D12_RESOURCE_STATES) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource, out Lease lease) + { + throw new NotSupportedException("This operation cannot be performaned on a readonly wrapper."); + } + + /// + private protected override void OnDispose() + { + this.owner.DangerousRelease(); + + if (this.owner.GraphicsDevice is GraphicsDevice device) + { + device.ReturnShaderResourceViewDescriptorHandles(in this.d3D12ResourceDescriptorHandles); + } + } + } +} diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture1D{T,TPixel}.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture1D{T,TPixel}.cs new file mode 100644 index 00000000..b40d5b82 --- /dev/null +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture1D{T,TPixel}.cs @@ -0,0 +1,40 @@ +using System.Diagnostics; +using ComputeSharp.Exceptions; +using ComputeSharp.Graphics.Resources.Enums; +using ComputeSharp.Resources; +using ComputeSharp.Resources.Debug; +using static TerraFX.Interop.DirectX.D3D12_FORMAT_SUPPORT1; + +namespace ComputeSharp; + +/// +/// A representing a typed read write 1D texture stored on GPU memory. +/// +/// The type of items stored on the texture. +/// The type of pixels used on the GPU side. +[DebuggerTypeProxy(typeof(Texture1DDebugView<>))] +[DebuggerDisplay("{ToString(),raw}")] +public sealed partial class ReadWriteTexture1D : Texture1D, IReadWriteNormalizedTexture1D + where T : unmanaged, IPixel + where TPixel : unmanaged +{ + /// + /// Creates a new instance with the specified parameters. + /// + /// The associated with the current instance. + /// The width of the texture. + /// The allocation mode to use for the new resource. + internal ReadWriteTexture1D(GraphicsDevice device, int width, AllocationMode allocationMode) + : base(device, width, ResourceType.ReadWrite, allocationMode, D3D12_FORMAT_SUPPORT1_TEXTURE1D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW) + { + } + + /// + public ref TPixel this[int x] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture2D)}[{typeof(int)}]"); + + /// + public override string ToString() + { + return $"ComputeSharp.ReadWriteTexture1D<{typeof(T)}, {typeof(TPixel)}>[{Width}]"; + } +} diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture1D{T}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture1D{T}.ReadOnly.cs new file mode 100644 index 00000000..4f03c9b9 --- /dev/null +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture1D{T}.ReadOnly.cs @@ -0,0 +1,146 @@ +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 ReadWriteTexture1D +{ + /// + /// The wrapping instance, if available. + /// + private ReadOnly? readOnlyWrapper; + + /// + public IReadOnlyTexture1D AsReadOnly() + { + using var _0 = GraphicsDevice.GetReferenceTrackingLease(); + using var _1 = GetReferenceTrackingLease(); + + GraphicsDevice.ThrowIfDeviceLost(); + + ThrowIfIsNotInReadOnlyState(); + + ReadOnly? readOnlyWrapper = this.readOnlyWrapper; + + if (readOnlyWrapper is not null) + { + return readOnlyWrapper; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + static ReadOnly InitializeWrapper(ReadWriteTexture1D texture) + { + return texture.readOnlyWrapper = new(texture); + } + + return InitializeWrapper(this); + } + + /// + private 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, IReadOnlyTexture1D, GraphicsResourceHelper.IGraphicsResource + { + /// + /// The owning instance being wrapped. + /// + private readonly ReadWriteTexture1D 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(ReadWriteTexture1D owner) + { + owner.DangerousAddRef(); + + this.owner = owner; + + owner.GraphicsDevice.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles); + + owner.GraphicsDevice.D3D12Device->CreateShaderResourceView(owner.D3D12Resource, DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE1D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); + } + + /// + public ref readonly T this[int x] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture1D.ReadOnly)}[{typeof(int)}]"); + + /// + public ref readonly T Sample(float u) => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture1D.ReadOnly)}.{nameof(Sample)}({typeof(float)})"); + + /// + public int Width => this.owner.Width; + + /// + public GraphicsDevice GraphicsDevice => this.owner.GraphicsDevice; + + /// + D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) + { + using var _0 = GetReferenceTrackingLease(); + using var _1 = this.owner.GetReferenceTrackingLease(); + + 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, out Lease lease) + { + lease = GetReferenceTrackingLease(); + + using var _1 = this.owner.GetReferenceTrackingLease(); + + this.owner.ThrowIfDeviceMismatch(device); + + return this.owner.D3D12Resource; + } + + /// + (D3D12_RESOURCE_STATES, D3D12_RESOURCE_STATES) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource, out Lease lease) + { + throw new NotSupportedException("This operation cannot be performaned on a readonly wrapper."); + } + + /// + private protected override void OnDispose() + { + this.owner.DangerousRelease(); + + if (this.owner.GraphicsDevice is GraphicsDevice device) + { + device.ReturnShaderResourceViewDescriptorHandles(in this.d3D12ResourceDescriptorHandles); + } + } + } +}