Add ComputeContext.Fill APIs

このコミットが含まれているのは:
Sergio Pedri committed 2022-01-08 19:56:44 +01:00
1 親 a0eb6427eb
コミット c5d4822e9f
4ファイル変更
+241 -20

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

+24
ファイルの表示
@@ -259,6 +259,30 @@ internal static unsafe class ID3D12GraphicsCommandListExtensions
}
}
/// <summary>
/// Fills a target UAV resource.
/// </summary>
/// <param name="d3D12GraphicsCommandList">The <see cref="ID3D12GraphicsCommandList"/> instance in use.</param>
/// <param name="d3D12Resource">The <see cref="ID3D12Resource"/> to fill.</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>
/// <param name="value">The value to use to fill the view.</param>
public static unsafe void FillUnorderedAccessView(
this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList,
ID3D12Resource* d3D12Resource,
D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle,
D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle,
Float4 value)
{
d3D12GraphicsCommandList.ClearUnorderedAccessViewFloat(
ViewGPUHandleInCurrentHeap: d3D12GpuDescriptorHandle,
ViewCPUHandle: d3D12CpuDescriptorHandle,
pResource: d3D12Resource,
Values: (float*)&value,
NumRects: 0,
pRects: null);
}
/// <summary>
/// Binds an input constant buffer to the first root parameter.
/// </summary>
+105 -1
ファイルの表示
@@ -50,7 +50,7 @@ internal static class DXGIFormatHelper
/// 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>
/// <returns>Whether 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>()
@@ -88,4 +88,108 @@ internal static class DXGIFormatHelper
return ThrowHelper.ThrowArgumentException<bool>("Invalid texture type");
}
/// <summary>
/// Extends a given pixel type to its <see cref="Float4"/> equivalent.
/// </summary>
/// <typeparam name="T">The input pixel value to convert.</typeparam>
/// <returns>The <see cref="Float4"/> equivalent value for <paramref name="value"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Float4 ExtendToNormalizedValue<T>(ref T value)
where T : unmanaged
{
if (typeof(T) == typeof(Float4))
{
return Unsafe.As<T, Float4>(ref value);
}
Float4 result = default;
Unsafe.As<Float4, T>(ref result) = value;
return result;
}
/// <summary>
/// Converts a given value to a normalized value.
/// </summary>
/// <typeparam name="T">The input value to convert.</typeparam>
/// <returns>The normalized value for <paramref name="value"/>.</returns>
/// <exception cref="System.ArgumentException">Thrown when the input type <typeparamref name="T"/> is not supported.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Float4 ConvertToNormalizedValue<T>(ref T value)
where T : unmanaged
{
if (typeof(T) == typeof(Bgra32))
{
ref Bgra32 bgra = ref Unsafe.As<T, Bgra32>(ref value);
Vector4 linear = new(bgra.R, bgra.G, bgra.B, bgra.A);
Vector4 normalized = linear / byte.MaxValue;
return normalized;
}
if (typeof(T) == typeof(Rgba32))
{
ref Rgba32 rgba = ref Unsafe.As<T, Rgba32>(ref value);
Vector4 linear = new(rgba.R, rgba.G, rgba.B, rgba.A);
Vector4 normalized = linear / byte.MaxValue;
return normalized;
}
if (typeof(T) == typeof(Rgba64))
{
ref Rgba64 rgba = ref Unsafe.As<T, Rgba64>(ref value);
Vector4 linear = new(rgba.R, rgba.G, rgba.B, rgba.A);
Vector4 normalized = linear / ushort.MaxValue;
return normalized;
}
if (typeof(T) == typeof(R8))
{
ref R8 r = ref Unsafe.As<T, R8>(ref value);
Vector4 linear = new(r.R, 0, 0, 0);
Vector4 normalized = linear / byte.MaxValue;
return normalized;
}
if (typeof(T) == typeof(R16))
{
ref R16 r = ref Unsafe.As<T, R16>(ref value);
Vector4 linear = new(r.R, 0, 0, 0);
Vector4 normalized = linear / ushort.MaxValue;
return normalized;
}
if (typeof(T) == typeof(Rg16))
{
ref Rg16 rg = ref Unsafe.As<T, Rg16>(ref value);
Vector4 linear = new(rg.R, rg.G, 0, 0);
Vector4 normalized = linear / byte.MaxValue;
return normalized;
}
if (typeof(T) == typeof(Rg32))
{
ref Rg32 rg = ref Unsafe.As<T, Rg32>(ref value);
Vector4 linear = new(rg.R, rg.G, 0, 0);
Vector4 normalized = linear / ushort.MaxValue;
return normalized;
}
return ThrowHelper.ThrowArgumentException<Float4>("Invalid pixel type");
}
}
+25 -5
ファイルの表示
@@ -52,6 +52,19 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
}
}
/// <summary>
/// Inserts a resource barrier for a specific resource.
/// </summary>
/// <param name="d3D12Resource">The <see cref="ID3D12Resource"/> to insert the barrier for.</param>
internal readonly unsafe void Barrier(ID3D12Resource* d3D12Resource)
{
ThrowInvalidOperationExceptionIfDeviceIsNull();
ref CommandList commandList = ref GetCommandList(in this);
commandList.D3D12GraphicsCommandList->UnorderedAccessViewBarrier(d3D12Resource);
}
/// <summary>
/// Clears a specific resource.
/// </summary>
@@ -73,16 +86,23 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
}
/// <summary>
/// Inserts a resource barrier for a specific resource.
/// Fills a specific resource.
/// </summary>
/// <param name="d3D12Resource">The <see cref="ID3D12Resource"/> to insert the barrier for.</param>
internal readonly unsafe void Barrier(ID3D12Resource* d3D12Resource)
/// <param name="d3D12Resource">The <see cref="ID3D12Resource"/> to fill.</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>
/// <param name="value">The value to use to fill the resource.</param>
internal readonly unsafe void Fill(
ID3D12Resource* d3D12Resource,
D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle,
D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle,
Float4 value)
{
ThrowInvalidOperationExceptionIfDeviceIsNull();
ref CommandList commandList = ref GetCommandList(in this);
ref CommandList commandList = ref GetCommandList(in this, null);
commandList.D3D12GraphicsCommandList->UnorderedAccessViewBarrier(d3D12Resource);
commandList.D3D12GraphicsCommandList->FillUnorderedAccessView(d3D12Resource, d3D12GpuDescriptorHandle, d3D12CpuDescriptorHandle, value);
}
/// <summary>
+87 -14
ファイルの表示
@@ -1,5 +1,6 @@
using System.Runtime.CompilerServices;
using ComputeSharp.__Internals;
using ComputeSharp.Graphics.Helpers;
#pragma warning disable CS0618
@@ -113,7 +114,7 @@ public static partial class ComputeContextExtensions
}
/// <summary>
/// Clears a specific resource, with a specified value.
/// Clears a specific resource.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="context">The <see cref="ComputeContext"/> to use to clear the resource.</param>
@@ -127,7 +128,7 @@ public static partial class ComputeContextExtensions
}
/// <summary>
/// Clears a specific resource, with a specified value.
/// Clears a specific resource.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="context">The <see cref="ComputeContext"/> to use to clear the resource.</param>
@@ -141,7 +142,7 @@ public static partial class ComputeContextExtensions
}
/// <summary>
/// Clears a specific resource, with a specified value.
/// Clears a specific resource.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
@@ -151,13 +152,13 @@ public static partial class ComputeContextExtensions
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized);
var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _);
context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, isNormalized);
context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, true);
}
/// <summary>
/// Clears a specific resource, with a specified value.
/// Clears a specific resource.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
@@ -167,13 +168,13 @@ public static partial class ComputeContextExtensions
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized);
var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _);
context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, isNormalized);
context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, true);
}
/// <summary>
/// Clears a specific resource, with a specified value.
/// Clears a specific resource.
/// </summary>
/// <typeparam name="TPixel">The type of pixels stored on the texture.</typeparam>
/// <param name="context">The <see cref="ComputeContext"/> to use to clear the resource.</param>
@@ -181,13 +182,13 @@ public static partial class ComputeContextExtensions
public static unsafe void Clear<TPixel>(this in ComputeContext context, IReadWriteTexture2D<TPixel> texture)
where TPixel : unmanaged
{
var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized);
var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _);
context.Clear(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice), handles.Gpu, handles.Cpu, isNormalized);
context.Clear(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice), handles.Gpu, handles.Cpu, true);
}
/// <summary>
/// Clears a specific resource, with a specified value.
/// Clears a specific resource.
/// </summary>
/// <typeparam name="TPixel">The type of pixels stored on the texture.</typeparam>
/// <param name="context">The <see cref="ComputeContext"/> to use to clear the resource.</param>
@@ -195,9 +196,81 @@ public static partial class ComputeContextExtensions
public static unsafe void Clear<TPixel>(this in ComputeContext context, IReadWriteTexture3D<TPixel> texture)
where TPixel : unmanaged
{
var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized);
var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _);
context.Clear(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice), handles.Gpu, handles.Cpu, isNormalized);
context.Clear(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice), handles.Gpu, handles.Cpu, true);
}
/// <summary>
/// Fills a specific texture with a given value.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="context">The <see cref="ComputeContext"/> to use to fill the resource.</param>
/// <param name="texture">The input <see cref="ReadWriteTexture2D{T,TPixel}"/> instance to fill.</param>
/// <param name="value">The value to use to fill <paramref name="texture"/>.</param>
public static unsafe void Fill<T, TPixel>(this in ComputeContext context, ReadWriteTexture2D<T, TPixel> texture, T value)
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _);
context.Fill(texture.D3D12Resource, handles.Gpu, handles.Cpu, DXGIFormatHelper.ConvertToNormalizedValue(ref value));
}
/// <summary>
/// Fills a specific texture with a given value.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="context">The <see cref="ComputeContext"/> to use to fill the resource.</param>
/// <param name="texture">The input <see cref="ReadWriteTexture3D{T,TPixel}"/> instance to fill.</param>
/// <param name="value">The value to use to fill <paramref name="texture"/>.</param>
public static unsafe void Fill<T, TPixel>(this in ComputeContext context, ReadWriteTexture3D<T, TPixel> texture, T value)
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _);
context.Fill(texture.D3D12Resource, handles.Gpu, handles.Cpu, DXGIFormatHelper.ConvertToNormalizedValue(ref value));
}
/// <summary>
/// Fills a specific texture with a given value.
/// </summary>
/// <typeparam name="TPixel">The type of pixels stored on the texture.</typeparam>
/// <param name="context">The <see cref="ComputeContext"/> to use to fill the resource.</param>
/// <param name="texture">The input <see cref="IReadWriteTexture2D{TPixel}"/> instance to fill.</param>
/// <param name="value">The value to use to fill <paramref name="texture"/>.</param>
public static unsafe void Fill<TPixel>(this in ComputeContext context, IReadWriteTexture2D<TPixel> texture, TPixel value)
where TPixel : unmanaged
{
var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _);
context.Fill(
((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice),
handles.Gpu,
handles.Cpu,
DXGIFormatHelper.ExtendToNormalizedValue(ref value));
}
/// <summary>
/// Fills a specific texture with a given value.
/// </summary>
/// <typeparam name="TPixel">The type of pixels stored on the texture.</typeparam>
/// <param name="context">The <see cref="ComputeContext"/> to use to fill the resource.</param>
/// <param name="texture">The input <see cref="IReadWriteTexture3D{TPixel}"/> instance to fill.</param>
/// <param name="value">The value to use to fill <paramref name="texture"/>.</param>
public static unsafe void Fill<TPixel>(this in ComputeContext context, IReadWriteTexture3D<TPixel> texture, TPixel value)
where TPixel : unmanaged
{
var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _);
context.Fill(
((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice),
handles.Gpu,
handles.Cpu,
DXGIFormatHelper.ExtendToNormalizedValue(ref value));
}
/// <summary>