diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs index ad2664be..16bb50b1 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs @@ -222,31 +222,19 @@ internal static unsafe class ID3D12GraphicsCommandListExtensions /// /// Clears a target UAV resource. /// - /// /// The instance in use. /// The to clear. /// The value for the target resource. /// The value for the target resource. - public static unsafe void ClearUnorderedAccessView( + /// Indicates whether the target resource uses a normalized format. + public static unsafe void ClearUnorderedAccessView( this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, ID3D12Resource* d3D12Resource, D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle, - D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle) - where T : unmanaged + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, + bool isNormalized) { - if (typeof(T) == typeof(uint)) - { - UInt4 values = default; - - d3D12GraphicsCommandList.ClearUnorderedAccessViewUint( - ViewGPUHandleInCurrentHeap: d3D12GpuDescriptorHandle, - ViewCPUHandle: d3D12CpuDescriptorHandle, - pResource: d3D12Resource, - Values: (uint*)&values, - NumRects: 0, - pRects: null); - } - else if (typeof(T) == typeof(float)) + if (isNormalized) { Float4 values = default; @@ -260,7 +248,15 @@ internal static unsafe class ID3D12GraphicsCommandListExtensions } else { - ThrowHelper.ThrowArgumentException("Invalid clear type"); + UInt4 values = default; + + d3D12GraphicsCommandList.ClearUnorderedAccessViewUint( + ViewGPUHandleInCurrentHeap: d3D12GpuDescriptorHandle, + ViewCPUHandle: d3D12CpuDescriptorHandle, + pResource: d3D12Resource, + Values: (uint*)&values, + NumRects: 0, + pRects: null); } } diff --git a/src/ComputeSharp/Graphics/Helpers/DXGIFormatHelper.cs b/src/ComputeSharp/Graphics/Helpers/DXGIFormatHelper.cs index dec9ee3e..6fd25e28 100644 --- a/src/ComputeSharp/Graphics/Helpers/DXGIFormatHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/DXGIFormatHelper.cs @@ -45,4 +45,47 @@ internal static class DXGIFormatHelper else if (typeof(T) == typeof(Rg32)) return DXGI_FORMAT_R16G16_UNORM; else return ThrowHelper.ThrowArgumentException("Invalid texture type"); } + + /// + /// Gets whether or not the input type corresponds to a normalized format. + /// + /// The input type argument to check. + /// TWhether or not the input type corresponds to a normalized format.. + /// Thrown when the input type is not supported. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsNormalizedType() + where T : unmanaged + { + if (typeof(T) == typeof(int) || + typeof(T) == typeof(Int2) || + typeof(T) == typeof(Int3) || + typeof(T) == typeof(Int4) || + typeof(T) == typeof(uint) || + typeof(T) == typeof(UInt2) || + typeof(T) == typeof(UInt3) || + typeof(T) == typeof(UInt4) || + typeof(T) == typeof(float) || + typeof(T) == typeof(Float2) || + typeof(T) == typeof(Float3) || + typeof(T) == typeof(Float4) || + typeof(T) == typeof(Vector2) || + typeof(T) == typeof(Vector3) || + typeof(T) == typeof(Vector4)) + { + return false; + } + + if (typeof(T) == typeof(Bgra32) || + typeof(T) == typeof(Rgba32) || + typeof(T) == typeof(Rgba64) || + typeof(T) == typeof(R8) || + typeof(T) == typeof(R16) || + typeof(T) == typeof(Rg16) || + typeof(T) == typeof(Rg32)) + { + return true; + } + + return ThrowHelper.ThrowArgumentException("Invalid texture type"); + } } diff --git a/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs b/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs index b88ac3b4..195862ad 100644 --- a/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs @@ -27,11 +27,12 @@ public static class GraphicsResourceHelper D3D12_GPU_DESCRIPTOR_HANDLE ValidateAndGetGpuDescriptorHandle(GraphicsDevice device); /// - /// Validates the given resource for usage with a specified device, and retrieves its GPU and CPU descriptor handles. + /// Validates the given resource for usage with a specified device, and retrieves its GPU and CPU descriptor handles for a clear operation. /// /// The target instance in use. + /// Indicates whether the current resource uses a normalized format. /// The GPU and CPU descriptor handles for the resource. - (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice device); + (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized); /// /// Validates the given resource for usage with a specified device, and retrieves the underlying object. diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index 51000b13..40b7db1a 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -156,7 +156,7 @@ public unsafe abstract class Buffer : NativeObject /// /// Gets the non shader visible instance for the current resource. /// - internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonGpuVisible => this.d3D12CpuDescriptorHandleNonShaderVisible; + internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonShaderVisible => this.d3D12CpuDescriptorHandleNonShaderVisible; /// /// 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/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index 33e29398..c0c96b40 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -305,13 +305,13 @@ public abstract class StructuredBuffer : Buffer } } - /// - internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice device) + /// + internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device) { ThrowIfDisposed(); ThrowIfDeviceMismatch(device); - return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandleNonGpuVisible); + return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandleNonShaderVisible); } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index 802db69b..c8e0f27f 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -174,7 +174,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper /// /// Gets the non shader visible instance for the current resource. /// - internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonGpuVisible => this.d3D12CpuDescriptorHandleNonShaderVisible; + internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonShaderVisible => this.d3D12CpuDescriptorHandleNonShaderVisible; /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. @@ -573,13 +573,15 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper } } - /// - internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice device) + /// + internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) { ThrowIfDisposed(); ThrowIfDeviceMismatch(device); - return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandleNonGpuVisible); + isNormalized = DXGIFormatHelper.IsNormalizedType(); + + return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandleNonShaderVisible); } /// @@ -601,9 +603,9 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper } /// - (D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice device) + (D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) { - return ValidateAndGetGpuAndCpuDescriptorHandles(device); + return ValidateAndGetGpuAndCpuDescriptorHandlesForClear(device, out isNormalized); } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 87327438..ab71fe6e 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -181,6 +181,11 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper /// internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle => this.d3D12CpuDescriptorHandle; + /// + /// Gets the non shader visible instance for the current resource. + /// + internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonShaderVisible => this.d3D12CpuDescriptorHandleNonShaderVisible; + /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. /// @@ -621,13 +626,15 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper } } - /// - internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice device) + /// + internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) { ThrowIfDisposed(); ThrowIfDeviceMismatch(device); - return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandle); + isNormalized = DXGIFormatHelper.IsNormalizedType(); + + return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandleNonShaderVisible); } /// @@ -648,10 +655,10 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper return D3D12GpuDescriptorHandle; } - /// - (D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice device) + /// + (D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) { - return ValidateAndGetGpuAndCpuDescriptorHandles(device); + return ValidateAndGetGpuAndCpuDescriptorHandlesForClear(device, out isNormalized); } /// diff --git a/src/ComputeSharp/Shaders/ComputeContext.cs b/src/ComputeSharp/Shaders/ComputeContext.cs index cca1e2cb..3ffb9935 100644 --- a/src/ComputeSharp/Shaders/ComputeContext.cs +++ b/src/ComputeSharp/Shaders/ComputeContext.cs @@ -40,7 +40,15 @@ public ref struct ComputeContext /// /// Gets the associated with the current instance. /// - public GraphicsDevice GraphicsDevice => this.device; + public GraphicsDevice GraphicsDevice + { + get + { + ThrowInvalidOperationExceptionIfDeviceIsNull(); + + return this.device; + } + } /// /// Clears a specific resource. @@ -48,17 +56,18 @@ public ref struct ComputeContext /// The to clear. /// The value for the target resource. /// The value for the target resource. - internal readonly unsafe void Clear( + /// Indicates whether the target resource uses a normalized format. + internal readonly unsafe void Clear( ID3D12Resource* d3D12Resource, D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle, - D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle) - where T : unmanaged + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, + bool isNormalized) { ThrowInvalidOperationExceptionIfDeviceIsNull(); ref CommandList commandList = ref GetCommandList(in this, null); - commandList.D3D12GraphicsCommandList->ClearUnorderedAccessView(d3D12Resource, d3D12GpuDescriptorHandle, d3D12CpuDescriptorHandle); + commandList.D3D12GraphicsCommandList->ClearUnorderedAccessView(d3D12Resource, d3D12GpuDescriptorHandle, d3D12CpuDescriptorHandle, isNormalized); } /// diff --git a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs index 6e880332..ac2019ed 100644 --- a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs +++ b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs @@ -107,9 +107,9 @@ public static partial class ComputeContextExtensions public static unsafe void Clear(this in ComputeContext context, ReadWriteBuffer buffer) where T : unmanaged { - var handles = buffer.ValidateAndGetGpuAndCpuDescriptorHandles(context.GraphicsDevice); + var handles = buffer.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice); - context.Clear(buffer.D3D12Resource, handles.Gpu, handles.Cpu); + context.Clear(buffer.D3D12Resource, handles.Gpu, handles.Cpu, isNormalized: false); } /// @@ -121,7 +121,9 @@ public static partial class ComputeContextExtensions public static unsafe void Clear(this in ComputeContext context, ReadWriteTexture2D texture) where T : unmanaged { - throw new System.NotImplementedException(); + var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); + + context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, isNormalized); } /// @@ -133,7 +135,9 @@ public static partial class ComputeContextExtensions public static unsafe void Clear(this in ComputeContext context, ReadWriteTexture3D texture) where T : unmanaged { - throw new System.NotImplementedException(); + var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); + + context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, isNormalized); } /// @@ -147,7 +151,9 @@ public static partial class ComputeContextExtensions where T : unmanaged, IUnorm where TPixel : unmanaged { - throw new System.NotImplementedException(); + var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); + + context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, isNormalized); } /// @@ -161,7 +167,9 @@ public static partial class ComputeContextExtensions where T : unmanaged, IUnorm where TPixel : unmanaged { - throw new System.NotImplementedException(); + var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); + + context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, isNormalized); } /// @@ -173,7 +181,9 @@ public static partial class ComputeContextExtensions public static unsafe void Clear(this in ComputeContext context, IReadWriteTexture2D texture) where TPixel : unmanaged { - throw new System.NotImplementedException(); + var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); + + context.Clear(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice), handles.Gpu, handles.Cpu, isNormalized); } /// @@ -185,7 +195,9 @@ public static partial class ComputeContextExtensions public static unsafe void Clear(this in ComputeContext context, IReadWriteTexture3D texture) where TPixel : unmanaged { - throw new System.NotImplementedException(); + var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); + + context.Clear(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice), handles.Gpu, handles.Cpu, isNormalized); } ///