248 行
6.7 KiB
C#
248 行
6.7 KiB
C#
using ComputeSharp;
|
|
using ComputeSharp.Interop;
|
|
using ComputeSharp.Tests.Misc;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
namespace ComputeSharp.Tests
|
|
{
|
|
[TestClass]
|
|
[TestCategory("ShaderCompiler")]
|
|
public partial class ShaderCompilerTests
|
|
{
|
|
[TestMethod]
|
|
public void ReservedKeywords()
|
|
{
|
|
_ = ReflectionServices.GetShaderInfo<ReservedKeywordsShader>();
|
|
}
|
|
|
|
[AutoConstructor]
|
|
public readonly partial struct ReservedKeywordsShader : IComputeShader
|
|
{
|
|
public readonly ReadWriteBuffer<float> row_major;
|
|
public readonly float dword;
|
|
|
|
public void Execute()
|
|
{
|
|
float exp = Hlsl.Exp(dword * row_major[ThreadIds.X]);
|
|
float log = Hlsl.Log(1 + exp);
|
|
|
|
row_major[ThreadIds.X] = log / dword;
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void SpecialTypeAsReturnType()
|
|
{
|
|
_ = ReflectionServices.GetShaderInfo<SpecialTypeAsReturnTypeShader>();
|
|
}
|
|
|
|
[AutoConstructor]
|
|
public readonly partial struct SpecialTypeAsReturnTypeShader : IComputeShader
|
|
{
|
|
public readonly ReadWriteBuffer<float2> buffer;
|
|
|
|
float2 Foo(float x) => x;
|
|
|
|
public void Execute()
|
|
{
|
|
static float3 Bar(float x) => x;
|
|
|
|
buffer[ThreadIds.X] = Foo(ThreadIds.X) + Bar(ThreadIds.X).XY;
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void LocalFunctionInExternalMethods()
|
|
{
|
|
_ = ReflectionServices.GetShaderInfo<LocalFunctionInExternalMethodsShader>();
|
|
}
|
|
|
|
[AutoConstructor]
|
|
public readonly partial struct LocalFunctionInExternalMethodsShader : IComputeShader
|
|
{
|
|
public readonly ReadWriteBuffer<float2> buffer;
|
|
|
|
float2 Foo(float x)
|
|
{
|
|
static float2 Baz(float y) => y;
|
|
|
|
return Baz(x);
|
|
}
|
|
|
|
public void Execute()
|
|
{
|
|
buffer[ThreadIds.X] = Foo(ThreadIds.X);
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void CapturedNestedStructType()
|
|
{
|
|
_ = ReflectionServices.GetShaderInfo<CapturedNestedStructTypeShader>();
|
|
}
|
|
|
|
[AutoConstructor]
|
|
public partial struct CustomStructType
|
|
{
|
|
public float2 a;
|
|
public int b;
|
|
}
|
|
|
|
[AutoConstructor]
|
|
public readonly partial struct CapturedNestedStructTypeShader : IComputeShader
|
|
{
|
|
public readonly ReadWriteBuffer<float> buffer;
|
|
public readonly CustomStructType foo;
|
|
|
|
/// <inheritdoc/>
|
|
public void Execute()
|
|
{
|
|
buffer[ThreadIds.X] *= foo.a.X + foo.b;
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ExternalStructType_Ok()
|
|
{
|
|
_ = ReflectionServices.GetShaderInfo<ExternalStructTypeShader>();
|
|
}
|
|
|
|
[AutoConstructor]
|
|
public readonly partial struct ExternalStructTypeShader : IComputeShader
|
|
{
|
|
public readonly ReadWriteBuffer<float> buffer;
|
|
|
|
/// <inheritdoc/>
|
|
public void Execute()
|
|
{
|
|
float value = buffer[ThreadIds.X];
|
|
ExternalStructType type = ExternalStructType.New((int)value, Hlsl.Abs(value));
|
|
|
|
buffer[ThreadIds.X] = ExternalStructType.Sum(type);
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void OutOfOrderMethods()
|
|
{
|
|
_ = ReflectionServices.GetShaderInfo<OutOfOrderMethodsShader>();
|
|
}
|
|
|
|
[AutoConstructor]
|
|
public readonly partial struct OutOfOrderMethodsShader : IComputeShader
|
|
{
|
|
public readonly ReadWriteBuffer<float> buffer;
|
|
|
|
static int A() => B();
|
|
|
|
static int B() => 1 + C();
|
|
|
|
static int C() => 1;
|
|
|
|
int D() => A() + E() + F();
|
|
|
|
int E() => 1;
|
|
|
|
static int F() => 1;
|
|
|
|
/// <inheritdoc/>
|
|
public void Execute()
|
|
{
|
|
float value = buffer[ThreadIds.X];
|
|
ExternalStructType type = ExternalStructType.New((int)value, Hlsl.Abs(value));
|
|
|
|
buffer[ThreadIds.X] = ExternalStructType.Sum(type);
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void PixelShader()
|
|
{
|
|
ShaderInfo info = ReflectionServices.GetShaderInfo<StatelessPixelShader, float4>();
|
|
|
|
Assert.AreEqual(info.TextureStoreInstructionCount, 1u);
|
|
Assert.AreEqual(info.BoundResourceCount, 2u);
|
|
}
|
|
|
|
public readonly partial struct StatelessPixelShader : IPixelShader<float4>
|
|
{
|
|
/// <inheritdoc/>
|
|
public float4 Execute()
|
|
{
|
|
return new(1, 1, 1, 1);
|
|
}
|
|
}
|
|
|
|
[AutoConstructor]
|
|
public readonly partial struct LoopWithVarCounterShader : IComputeShader
|
|
{
|
|
public readonly ReadWriteBuffer<float> buffer;
|
|
|
|
/// <inheritdoc/>
|
|
public void Execute()
|
|
{
|
|
for (var i = 0; i < 10; i++)
|
|
{
|
|
buffer[ThreadIds.X * 10 + i] = i;
|
|
}
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void LoopWithVarCounter()
|
|
{
|
|
_ = ReflectionServices.GetShaderInfo<LoopWithVarCounterShader>();
|
|
}
|
|
|
|
[TestMethod]
|
|
public void DoublePrecisionSupport()
|
|
{
|
|
ShaderInfo info = ReflectionServices.GetShaderInfo<DoublePrecisionSupportShader>();
|
|
|
|
Assert.IsTrue(info.RequiresDoublePrecisionSupport);
|
|
}
|
|
|
|
[AutoConstructor]
|
|
public readonly partial struct DoublePrecisionSupportShader : IComputeShader
|
|
{
|
|
public readonly ReadWriteBuffer<double> buffer;
|
|
public readonly double factor;
|
|
|
|
/// <inheritdoc/>
|
|
public void Execute()
|
|
{
|
|
buffer[ThreadIds.X] *= factor + 3.14;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace ExternalNamespace
|
|
{
|
|
[TestClass]
|
|
[TestCategory("ShaderCompiler")]
|
|
public partial class ShaderCompilerTestsInExternalNamespace
|
|
{
|
|
[AutoConstructor]
|
|
public readonly partial struct UserDefinedTypeShader : IComputeShader
|
|
{
|
|
public readonly ReadWriteBuffer<float> buffer;
|
|
|
|
/// <inheritdoc/>
|
|
public void Execute()
|
|
{
|
|
for (var i = 0; i < 10; i++)
|
|
{
|
|
buffer[ThreadIds.X * 10 + i] = i;
|
|
}
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void UserDefinedType()
|
|
{
|
|
_ = ReflectionServices.GetShaderInfo<UserDefinedTypeShader>();
|
|
}
|
|
}
|
|
}
|