Implement texture clear APIs

このコミットが含まれているのは:
Sergio Pedri committed 2021-12-29 15:26:00 +01:00
1 親 a9f50c1e46
コミット 1d138d8aae
9ファイル変更
+119 -49

検索条件に一致するファイルはありません

+14 -18
ファイルの表示
@@ -222,31 +222,19 @@ internal static unsafe class ID3D12GraphicsCommandListExtensions
/// <summary>
/// Clears a target UAV resource.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="d3D12GraphicsCommandList">The <see cref="ID3D12GraphicsCommandList"/> instance in use.</param>
/// <param name="d3D12Resource">The <see cref="ID3D12Resource"/> to clear.</param>
/// <param name="d3D12GpuDescriptorHandle">The <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> value for the target resource.</param>
/// <param name="d3D12CpuDescriptorHandle">The <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> value for the target resource.</param>
public static unsafe void ClearUnorderedAccessView<T>(
/// <param name="isNormalized">Indicates whether the target resource uses a normalized format.</param>
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);
}
}
+43
ファイルの表示
@@ -45,4 +45,47 @@ internal static class DXGIFormatHelper
else if (typeof(T) == typeof(Rg32)) return DXGI_FORMAT_R16G16_UNORM;
else return ThrowHelper.ThrowArgumentException<DXGI_FORMAT>("Invalid texture type");
}
/// <summary>
/// Gets whether or not the input type corresponds to a normalized format.
/// </summary>
/// <typeparam name="T">The input type argument to check.</typeparam>
/// <returns>TWhether or not the input type corresponds to a normalized format..</returns>
/// <exception cref="System.ArgumentException">Thrown when the input type <typeparamref name="T"/> is not supported.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNormalizedType<T>()
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<bool>("Invalid texture type");
}
}
+3 -2
ファイルの表示
@@ -27,11 +27,12 @@ public static class GraphicsResourceHelper
D3D12_GPU_DESCRIPTOR_HANDLE ValidateAndGetGpuDescriptorHandle(GraphicsDevice device);
/// <summary>
/// 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.
/// </summary>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance in use.</param>
/// <param name="isNormalized">Indicates whether the current resource uses a normalized format.</param>
/// <returns>The GPU and CPU descriptor handles for the resource.</returns>
(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);
/// <summary>
/// Validates the given resource for usage with a specified device, and retrieves the underlying <see cref="ID3D12Resource"/> object.
+1 -1
ファイルの表示
@@ -156,7 +156,7 @@ public unsafe abstract class Buffer<T> : NativeObject
/// <summary>
/// Gets the non shader visible <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> instance for the current resource.
/// </summary>
internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonGpuVisible => this.d3D12CpuDescriptorHandleNonShaderVisible;
internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonShaderVisible => this.d3D12CpuDescriptorHandleNonShaderVisible;
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Buffer{T}"/> instance and writes them into a target memory area.
+3 -3
ファイルの表示
@@ -305,13 +305,13 @@ public abstract class StructuredBuffer<T> : Buffer<T>
}
}
/// <inheritdoc cref="__Internals.GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice)"/>
internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice device)
/// <inheritdoc cref="__Internals.GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice, out bool)"/>
internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device)
{
ThrowIfDisposed();
ThrowIfDeviceMismatch(device);
return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandleNonGpuVisible);
return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandleNonShaderVisible);
}
/// <inheritdoc cref="__Internals.GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice)"/>
+8 -6
ファイルの表示
@@ -174,7 +174,7 @@ public unsafe abstract class Texture2D<T> : NativeObject, GraphicsResourceHelper
/// <summary>
/// Gets the non shader visible <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> instance for the current resource.
/// </summary>
internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonGpuVisible => this.d3D12CpuDescriptorHandleNonShaderVisible;
internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonShaderVisible => this.d3D12CpuDescriptorHandleNonShaderVisible;
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target memory area.
@@ -573,13 +573,15 @@ public unsafe abstract class Texture2D<T> : NativeObject, GraphicsResourceHelper
}
}
/// <inheritdoc cref="GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice)"/>
internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice device)
/// <inheritdoc cref="GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice, out bool)"/>
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<T>();
return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandleNonShaderVisible);
}
/// <inheritdoc cref="GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice)"/>
@@ -601,9 +603,9 @@ public unsafe abstract class Texture2D<T> : NativeObject, GraphicsResourceHelper
}
/// <inheritdoc/>
(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);
}
/// <inheritdoc/>
+13 -6
ファイルの表示
@@ -181,6 +181,11 @@ public unsafe abstract class Texture3D<T> : NativeObject, GraphicsResourceHelper
/// </summary>
internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle => this.d3D12CpuDescriptorHandle;
/// <summary>
/// Gets the non shader visible <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> instance for the current resource.
/// </summary>
internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonShaderVisible => this.d3D12CpuDescriptorHandleNonShaderVisible;
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture3D{T}"/> instance and writes them into a target memory area.
/// </summary>
@@ -621,13 +626,15 @@ public unsafe abstract class Texture3D<T> : NativeObject, GraphicsResourceHelper
}
}
/// <inheritdoc cref="GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice)"/>
internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice device)
/// <inheritdoc cref="GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice, out bool)"/>
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<T>();
return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandleNonShaderVisible);
}
/// <inheritdoc cref="GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice)"/>
@@ -648,10 +655,10 @@ public unsafe abstract class Texture3D<T> : NativeObject, GraphicsResourceHelper
return D3D12GpuDescriptorHandle;
}
/// <inheritdoc cref="GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice)"/>
(D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice device)
/// <inheritdoc/>
(D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized)
{
return ValidateAndGetGpuAndCpuDescriptorHandles(device);
return ValidateAndGetGpuAndCpuDescriptorHandlesForClear(device, out isNormalized);
}
/// <inheritdoc/>
+14 -5
ファイルの表示
@@ -40,7 +40,15 @@ public ref struct ComputeContext
/// <summary>
/// Gets the <see cref="ComputeSharp.GraphicsDevice"/> associated with the current instance.
/// </summary>
public GraphicsDevice GraphicsDevice => this.device;
public GraphicsDevice GraphicsDevice
{
get
{
ThrowInvalidOperationExceptionIfDeviceIsNull();
return this.device;
}
}
/// <summary>
/// Clears a specific resource.
@@ -48,17 +56,18 @@ public ref struct ComputeContext
/// <param name="d3D12Resource">The <see cref="ID3D12Resource"/> to clear.</param>
/// <param name="d3D12GpuDescriptorHandle">The <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> value for the target resource.</param>
/// <param name="d3D12CpuDescriptorHandle">The <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> value for the target resource.</param>
internal readonly unsafe void Clear<T>(
/// <param name="isNormalized">Indicates whether the target resource uses a normalized format.</param>
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<T>(d3D12Resource, d3D12GpuDescriptorHandle, d3D12CpuDescriptorHandle);
commandList.D3D12GraphicsCommandList->ClearUnorderedAccessView(d3D12Resource, d3D12GpuDescriptorHandle, d3D12CpuDescriptorHandle, isNormalized);
}
/// <summary>
+20 -8
ファイルの表示
@@ -107,9 +107,9 @@ public static partial class ComputeContextExtensions
public static unsafe void Clear<T>(this in ComputeContext context, ReadWriteBuffer<T> buffer)
where T : unmanaged
{
var handles = buffer.ValidateAndGetGpuAndCpuDescriptorHandles(context.GraphicsDevice);
var handles = buffer.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice);
context.Clear<uint>(buffer.D3D12Resource, handles.Gpu, handles.Cpu);
context.Clear(buffer.D3D12Resource, handles.Gpu, handles.Cpu, isNormalized: false);
}
/// <summary>
@@ -121,7 +121,9 @@ public static partial class ComputeContextExtensions
public static unsafe void Clear<T>(this in ComputeContext context, ReadWriteTexture2D<T> 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);
}
/// <summary>
@@ -133,7 +135,9 @@ public static partial class ComputeContextExtensions
public static unsafe void Clear<T>(this in ComputeContext context, ReadWriteTexture3D<T> 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);
}
/// <summary>
@@ -147,7 +151,9 @@ public static partial class ComputeContextExtensions
where T : unmanaged, IUnorm<TPixel>
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);
}
/// <summary>
@@ -161,7 +167,9 @@ public static partial class ComputeContextExtensions
where T : unmanaged, IUnorm<TPixel>
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);
}
/// <summary>
@@ -173,7 +181,9 @@ public static partial class ComputeContextExtensions
public static unsafe void Clear<TPixel>(this in ComputeContext context, IReadWriteTexture2D<TPixel> 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);
}
/// <summary>
@@ -185,7 +195,9 @@ public static partial class ComputeContextExtensions
public static unsafe void Clear<TPixel>(this in ComputeContext context, IReadWriteTexture3D<TPixel> 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);
}
/// <summary>