Fix [GroupShared] HLSL generation, add unit tests

このコミットが含まれているのは:
Sergio Pedri committed 2021-12-08 11:17:07 +01:00
1 親 e564864b6c
コミット 689e0e7202
2ファイル変更
+89 -1

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

+1 -1
ファイルの表示
@@ -680,7 +680,7 @@ public sealed partial class IShaderGenerator
// Shared buffers
foreach (var (bufferName, bufferType, bufferCount) in sharedBuffers)
{
object count = (object?)bufferCount ?? "threadsX * threadsY * threadsZ";
object count = (object?)bufferCount ?? "__GroupSize__get_X * __GroupSize__get_Y * __GroupSize__get_Z";
AppendLF();
AppendLineAndLF($"groupshared {bufferType} {bufferName} [{count}];");
+88
ファイルの表示
@@ -4,6 +4,8 @@ using ComputeSharp.Tests.Attributes;
using ComputeSharp.Tests.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
#pragma warning disable CS0649, CS8618
namespace ComputeSharp.Tests;
[TestClass]
@@ -298,4 +300,90 @@ public partial class DispatchTests
1);
}
}
[CombinatorialTestMethod]
[AllDevices]
public void Verify_GroupShared_WithFixedSize(Device device)
{
using ReadWriteBuffer<int> buffer = device.Get().AllocateReadWriteBuffer<int>(128);
device.Get().For(256, new FixedGroupSharedPixelShader(buffer));
int[] result = buffer.ToArray();
for (int i = 0; i < 128; i++)
{
Assert.AreEqual(result[i], i);
}
}
[AutoConstructor]
internal readonly partial struct FixedGroupSharedPixelShader : IComputeShader
{
private readonly ReadWriteBuffer<int> buffer;
[GroupShared(128)]
private static readonly int[] cache;
public void Execute()
{
int index = ThreadIds.X / 2;
bool isWritingToGroupShared = ThreadIds.X % 2 == 0;
if (isWritingToGroupShared)
{
cache[index] = index;
}
Hlsl.GroupMemoryBarrier();
if (!isWritingToGroupShared)
{
buffer[index] = cache[index];
}
}
}
[CombinatorialTestMethod]
[AllDevices]
public void Verify_GroupShared_WithDynamicSize(Device device)
{
using ReadWriteBuffer<int> buffer = device.Get().AllocateReadWriteBuffer<int>(32);
device.Get().For(64, 1, 1, 32, 1, 1, new DynamicGroupSharedPixelShader(buffer));
int[] result = buffer.ToArray();
for (int i = 0; i < 32; i++)
{
Assert.AreEqual(result[i], i);
}
}
[AutoConstructor]
internal readonly partial struct DynamicGroupSharedPixelShader : IComputeShader
{
private readonly ReadWriteBuffer<int> buffer;
[GroupShared]
private static readonly int[] cache;
public void Execute()
{
int index = ThreadIds.X / 2;
bool isWritingToGroupShared = ThreadIds.X % 2 == 0;
if (isWritingToGroupShared)
{
cache[index] = index;
}
Hlsl.GroupMemoryBarrier();
if (!isWritingToGroupShared)
{
buffer[index] = cache[index];
}
}
}
}