Added ThreadIds.Normalized type

このコミットが含まれているのは:
Sergio Pedri committed 2021-03-08 14:25:22 +01:00
1 親 b7ab492ff1
コミット 5794b43fe0
5ファイル変更
+285 -1

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

+1
ファイルの表示
@@ -22,6 +22,7 @@
<Compile Include="..\ComputeSharp\Core\Dispatch\GroupIds.cs" Link="Core\Dispatch\GroupIds.cs" />
<Compile Include="..\ComputeSharp\Core\Dispatch\GroupSize.cs" Link="Core\Dispatch\GroupSize.cs" />
<Compile Include="..\ComputeSharp\Core\Dispatch\ThreadIds.cs" Link="Core\Dispatch\ThreadIds.cs" />
<Compile Include="..\ComputeSharp\Core\Dispatch\ThreadIds.Normalized.cs" Link="Core\Dispatch\ThreadIds.Normalized.cs" />
<Compile Include="..\ComputeSharp\Core\Exceptions\InvalidExecutionContextException.cs" Link="Core\Exceptions\InvalidExecutionContextException.cs" />
<Compile Include="..\ComputeSharp\Core\Exceptions\MissingMethodSourceException.cs" Link="Core\Exceptions\MissingMethodSourceException.cs" />
<Compile Include="..\ComputeSharp\Core\Extensions\MethodInfoExtensions.cs" Link="Core\Extensions\MethodInfoExtensions.cs" />
+31
ファイルの表示
@@ -227,6 +227,37 @@ namespace ComputeSharp.SourceGenerators.Mappings
}
}
// Programmatically load mappings for the normalized thread ids
foreach (var property in typeof(ThreadIds.Normalized).GetProperties(BindingFlags.Static | BindingFlags.Public))
{
string key = $"{typeof(ThreadIds).FullName}{Type.Delimiter}{typeof(ThreadIds.Normalized).Name}{Type.Delimiter}{property.Name}";
switch (property.Name)
{
case string name when name.Length == 1:
knownMembers.Add(key, $"{typeof(ThreadIds).Name}.{char.ToLower(name[0])} / (float)__{char.ToLower(name[0])}");
break;
case string name when name.Length == 2:
{
string
numerator = $"float2({typeof(ThreadIds).Name}.{char.ToLower(name[0])}, {typeof(ThreadIds).Name}.{char.ToLower(name[1])})",
denominator = $"float2(__{char.ToLower(name[0])}, __{char.ToLower(name[1])})";
knownMembers.Add(key, $"{numerator} / {denominator}");
break;
}
case string name when name.Length == 3:
{
string
numerator = $"float3({typeof(ThreadIds).Name}.{char.ToLower(name[0])}, {typeof(ThreadIds).Name}.{char.ToLower(name[1])}, {typeof(ThreadIds).Name}.{char.ToLower(name[2])})",
denominator = $"float3(__{char.ToLower(name[0])}, __{char.ToLower(name[1])}, __{char.ToLower(name[2])})";
knownMembers.Add(key, $"{numerator} / {denominator}");
break;
}
}
}
// Programmatically load mappings for the group size
foreach (var property in typeof(GroupSize).GetProperties(BindingFlags.Static | BindingFlags.Public))
{
+211
ファイルの表示
@@ -0,0 +1,211 @@
using ComputeSharp.Exceptions;
namespace ComputeSharp
{
/// <inheritdoc cref="ThreadIds"/>
public static partial class ThreadIds
{
/// <summary>
/// A <see langword="class"/> that indicates the normalized ids of a given GPU thread running a compute shader.
/// These ids represent equivalent info to those from <see cref="ThreadIds"/>, but normalized in the [0, 1] range.
/// The range used for the normalization is the one given by the target dispatch size (see <see cref="DispatchSize"/>).
/// </summary>
public static class Normalized
{
/// <summary>
/// Gets the normalized X id of the current thread.
/// </summary>
public static float X => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(X)}");
/// <summary>
/// Gets the normalized Y id of the current thread.
/// </summary>
public static float Y => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(Y)}");
/// <summary>
/// Gets the normalized Z id of the current thread.
/// </summary>
public static float Z => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(Z)}");
/// <summary>
/// Gets a <see cref="Float2"/> value with the components <see cref="X"/>, <see cref="X"/>.
/// </summary>
public static Float2 XX => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(XX)}");
/// <summary>
/// Gets a <see cref="Float2"/> value with the components <see cref="X"/>, <see cref="Y"/>.
/// </summary>
public static Float2 XY => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(XY)}");
/// <summary>
/// Gets a <see cref="Float2"/> value with the components <see cref="X"/>, <see cref="Z"/>.
/// </summary>
public static Float2 XZ => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(XZ)}");
/// <summary>
/// Gets a <see cref="Float2"/> value with the components <see cref="Y"/>, <see cref="X"/>.
/// </summary>
public static Float2 YX => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(YX)}");
/// <summary>
/// Gets a <see cref="Float2"/> value with the components <see cref="Y"/>, <see cref="Y"/>.
/// </summary>
public static Float2 YY => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(YY)}");
/// <summary>
/// Gets a <see cref="Float2"/> value with the components <see cref="Y"/>, <see cref="Z"/>.
/// </summary>
public static Float2 YZ => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(YZ)}");
/// <summary>
/// Gets a <see cref="Float2"/> value with the components <see cref="Z"/>, <see cref="X"/>.
/// </summary>
public static Float2 ZX => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(ZX)}");
/// <summary>
/// Gets a <see cref="Float2"/> value with the components <see cref="Z"/>, <see cref="Y"/>.
/// </summary>
public static Float2 ZY => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(ZY)}");
/// <summary>
/// Gets a <see cref="Float2"/> value with the components <see cref="Z"/>, <see cref="Z"/>.
/// </summary>
public static Float2 ZZ => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(ZZ)}");
/// <summary>
/// Gets a <see cref="Float3"/> value with the components <see cref="X"/>, <see cref="X"/>, <see cref="X"/>.
/// </summary>
public static Float3 XXX => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(XXX)}");
/// <summary>
/// Gets a <see cref="Float3"/> value with the components <see cref="X"/>, <see cref="X"/>, <see cref="Y"/>.
/// </summary>
public static Float3 XXY => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(XXY)}");
/// <summary>
/// Gets a <see cref="Float3"/> value with the components <see cref="X"/>, <see cref="X"/>, <see cref="Z"/>.
/// </summary>
public static Float3 XXZ => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(XXZ)}");
/// <summary>
/// Gets a <see cref="Float3"/> value with the components <see cref="X"/>, <see cref="Y"/>, <see cref="X"/>.
/// </summary>
public static Float3 XYX => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(XYX)}");
/// <summary>
/// Gets a <see cref="Float3"/> value with the components <see cref="X"/>, <see cref="Y"/>, <see cref="Y"/>.
/// </summary>
public static Float3 XYY => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(XYY)}");
/// <summary>
/// Gets a <see cref="Float3"/> value with the components <see cref="X"/>, <see cref="Y"/>, <see cref="Z"/>.
/// </summary>
public static Float3 XYZ => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(XYZ)}");
/// <summary>
/// Gets a <see cref="Float3"/> value with the components <see cref="X"/>, <see cref="Z"/>, <see cref="X"/>.
/// </summary>
public static Float3 XZX => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(XZX)}");
/// <summary>
/// Gets a <see cref="Float3"/> value with the components <see cref="X"/>, <see cref="Z"/>, <see cref="Y"/>.
/// </summary>
public static Float3 XZY => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(XZY)}");
/// <summary>
/// Gets a <see cref="Float3"/> value with the components <see cref="X"/>, <see cref="Z"/>, <see cref="Z"/>.
/// </summary>
public static Float3 XZZ => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(XZZ)}");
/// <summary>
/// Gets a <see cref="Float3"/> value with the components <see cref="Y"/>, <see cref="X"/>, <see cref="X"/>.
/// </summary>
public static Float3 YXX => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(YXX)}");
/// <summary>
/// Gets a <see cref="Float3"/> value with the components <see cref="Y"/>, <see cref="X"/>, <see cref="Y"/>.
/// </summary>
public static Float3 YXY => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(YXY)}");
/// <summary>
/// Gets a <see cref="Float3"/> value with the components <see cref="Y"/>, <see cref="X"/>, <see cref="Z"/>.
/// </summary>
public static Float3 YXZ => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(YXZ)}");
/// <summary>
/// Gets a <see cref="Float3"/> value with the components <see cref="Y"/>, <see cref="Y"/>, <see cref="X"/>.
/// </summary>
public static Float3 YYX => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(YYX)}");
/// <summary>
/// Gets a <see cref="Float3"/> value with the components <see cref="Y"/>, <see cref="Y"/>, <see cref="Y"/>.
/// </summary>
public static Float3 YYY => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(YYY)}");
/// <summary>
/// Gets a <see cref="Float3"/> value with the components <see cref="Y"/>, <see cref="Y"/>, <see cref="Z"/>.
/// </summary>
public static Float3 YYZ => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(YYZ)}");
/// <summary>
/// Gets a <see cref="Float3"/> value with the components <see cref="Y"/>, <see cref="Z"/>, <see cref="X"/>.
/// </summary>
public static Float3 YZX => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(YZX)}");
/// <summary>
/// Gets a <see cref="Float3"/> value with the components <see cref="Y"/>, <see cref="Z"/>, <see cref="Y"/>.
/// </summary>
public static Float3 YZY => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(YZY)}");
/// <summary>
/// Gets a <see cref="Float3"/> value with the components <see cref="Y"/>, <see cref="Z"/>, <see cref="Z"/>.
/// </summary>
public static Float3 YZZ => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(YZZ)}");
/// <summary>
/// Gets a <see cref="Float3"/> value with the components <see cref="Z"/>, <see cref="X"/>, <see cref="X"/>.
/// </summary>
public static Float3 ZXX => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(ZXX)}");
/// <summary>
/// Gets a <see cref="Float3"/> value with the components <see cref="Z"/>, <see cref="X"/>, <see cref="Y"/>.
/// </summary>
public static Float3 ZXY => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(ZXY)}");
/// <summary>
/// Gets a <see cref="Float3"/> value with the components <see cref="Z"/>, <see cref="X"/>, <see cref="Z"/>.
/// </summary>
public static Float3 ZXZ => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(ZXZ)}");
/// <summary>
/// Gets a <see cref="Float3"/> value with the components <see cref="Z"/>, <see cref="Y"/>, <see cref="X"/>.
/// </summary>
public static Float3 ZYX => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(ZYX)}");
/// <summary>
/// Gets a <see cref="Float3"/> value with the components <see cref="Z"/>, <see cref="Y"/>, <see cref="Y"/>.
/// </summary>
public static Float3 ZYY => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(ZYY)}");
/// <summary>
/// Gets a <see cref="Float3"/> value with the components <see cref="Z"/>, <see cref="Y"/>, <see cref="Z"/>.
/// </summary>
public static Float3 ZYZ => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(ZYZ)}");
/// <summary>
/// Gets a <see cref="Float3"/> value with the components <see cref="Z"/>, <see cref="Z"/>, <see cref="X"/>.
/// </summary>
public static Float3 ZZX => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(ZZX)}");
/// <summary>
/// Gets a <see cref="Float3"/> value with the components <see cref="Z"/>, <see cref="Z"/>, <see cref="Y"/>.
/// </summary>
public static Float3 ZZY => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(ZZY)}");
/// <summary>
/// Gets a <see cref="Float3"/> value with the components <see cref="Z"/>, <see cref="Z"/>, <see cref="Z"/>.
/// </summary>
public static Float3 ZZZ => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(ZZZ)}");
}
}
}
+1 -1
ファイルの表示
@@ -6,7 +6,7 @@ namespace ComputeSharp
/// A <see langword="class"/> that indicates the ids of a given GPU thread running a compute shader.
/// That is, it enables a shader to access info on the current iteration index along each axis.
/// </summary>
public static class ThreadIds
public static partial class ThreadIds
{
/// <summary>
/// Gets the X id of the current thread.
+41
ファイルの表示
@@ -65,6 +65,47 @@ namespace ComputeSharp.Tests
}
}
[CombinatorialTestMethod]
[AllDevices]
public unsafe void Verify_ThreadIdsNormalized(Device device)
{
using ReadWriteTexture3D<Float4> buffer = device.Get().AllocateReadWriteTexture3D<Float4>(10, 20, 30);
device.Get().For(buffer.Width, buffer.Height, buffer.Depth, new ThreadIdsNormalizedShader(buffer));
Float4[,,] data = buffer.ToArray();
float* value = stackalloc float[4];
for (int z = 0; z < 30; z++)
{
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 20; y++)
{
*(Float4*)value = data[z, y, x];
Assert.AreEqual(x / (float)buffer.Width, value[0], 0.000001f);
Assert.AreEqual(y / (float)buffer.Height, value[1], 0.000001f);
Assert.AreEqual(z / (float)buffer.Depth, value[2], 0.000001f);
Assert.AreEqual(x / (float)buffer.Width, value[3], 0.000001f);
}
}
}
}
[AutoConstructor]
internal readonly partial struct ThreadIdsNormalizedShader : IComputeShader
{
public readonly ReadWriteTexture3D<Float4> buffer;
public void Execute()
{
buffer[ThreadIds.XYZ].XYZ = ThreadIds.Normalized.XYZ;
buffer[ThreadIds.XYZ].XY = ThreadIds.Normalized.XY;
buffer[ThreadIds.XYZ].W = ThreadIds.Normalized.X;
}
}
[CombinatorialTestMethod]
[AllDevices]
public unsafe void Verify_GroupIds(Device device)