Add support for NaN/infinity constants for float/double
このコミットが含まれているのは:
1 親
b27c89acc0
コミット
14fa96998f
8ファイル変更
+151
-1
検索条件に一致するファイルはありません
@@ -228,6 +228,11 @@ internal sealed class ShaderSourceRewriter : HlslSourceRewriter
|
||||
if (fieldOperation.Field.IsConst &&
|
||||
fieldOperation.Type!.TypeKind != TypeKind.Enum)
|
||||
{
|
||||
if (HlslKnownFields.TryGetMappedName(fieldOperation.Member.ToDisplayString(), out string? constantLiteral))
|
||||
{
|
||||
return ParseExpression(constantLiteral!);
|
||||
}
|
||||
|
||||
ConstantDefinitions[fieldOperation.Field] = ((IFormattable)fieldOperation.Field.ConstantValue!).ToString(null, CultureInfo.InvariantCulture);
|
||||
|
||||
var ownerTypeName = ((INamedTypeSymbol)fieldOperation.Field.ContainingSymbol).ToDisplayString().ToHlslIdentifierName();
|
||||
|
||||
@@ -58,6 +58,11 @@ internal sealed class StaticFieldRewriter : HlslSourceRewriter
|
||||
fieldOperation.Field.IsConst &&
|
||||
fieldOperation.Type!.TypeKind != TypeKind.Enum)
|
||||
{
|
||||
if (HlslKnownFields.TryGetMappedName(fieldOperation.Member.ToDisplayString(), out string? constantLiteral))
|
||||
{
|
||||
return ParseExpression(constantLiteral!);
|
||||
}
|
||||
|
||||
ConstantDefinitions[fieldOperation.Field] = ((IFormattable)fieldOperation.Field.ConstantValue!).ToString(null, CultureInfo.InvariantCulture);
|
||||
|
||||
var ownerTypeName = ((INamedTypeSymbol)fieldOperation.Field.ContainingSymbol).ToDisplayString().ToHlslIdentifierName();
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Extensions\StringExtensions.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Extensions\SyntaxNodeExtensions.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Mappings\HlslKnownFields.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Mappings\HlslKnownKeywords.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Mappings\HlslKnownMethods.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Mappings\HlslKnownProperties.cs" />
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ComputeSharp.SourceGeneration.Mappings;
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that contains and maps known HLSL field names to common .NET fields.
|
||||
/// </summary>
|
||||
internal static partial class HlslKnownFields
|
||||
{
|
||||
/// <summary>
|
||||
/// The mapping of supported known fields to HLSL names.
|
||||
/// </summary>
|
||||
private static readonly IReadOnlyDictionary<string, string> KnownFields = BuildKnownFieldsMap();
|
||||
|
||||
/// <summary>
|
||||
/// Builds the mapping of supported known fields to HLSL names.
|
||||
/// </summary>
|
||||
private static IReadOnlyDictionary<string, string> BuildKnownFieldsMap()
|
||||
{
|
||||
Dictionary<string, string> knownMembers = new()
|
||||
{
|
||||
[$"float.{nameof(float.NaN)}"] = "asfloat(0xFFC00000)",
|
||||
[$"float.{nameof(float.PositiveInfinity)}"] = "asfloat(0x7F800000)",
|
||||
[$"float.{nameof(float.NegativeInfinity)}"] = "asfloat(0xFF800000)",
|
||||
|
||||
[$"double.{nameof(double.NaN)}"] = "asdouble(0x00000000, 0xFFF80000)",
|
||||
[$"double.{nameof(double.PositiveInfinity)}"] = "asdouble(0x00000000, 0x7FF00000)",
|
||||
[$"double.{nameof(double.NegativeInfinity)}"] = "asdouble(0x00000000, 0xFFF00000)"
|
||||
};
|
||||
|
||||
return knownMembers;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the mapped HLSL-compatible field name for the input field name.
|
||||
/// </summary>
|
||||
/// <param name="name">The input fully qualified field name.</param>
|
||||
/// <param name="mapped">The mapped name, if one is found.</param>
|
||||
/// <returns>The HLSL-compatible field name that can be used in an HLSL shader.</returns>
|
||||
public static bool TryGetMappedName(string name, out string? mapped)
|
||||
{
|
||||
return KnownFields.TryGetValue(name, out mapped);
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ using System.Text.RegularExpressions;
|
||||
namespace ComputeSharp.SourceGeneration.Mappings;
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that contains and maps known HLSL properties names to common .NET members.
|
||||
/// A <see langword="class"/> that contains and maps known HLSL properties names to common .NET properties.
|
||||
/// </summary>
|
||||
internal static partial class HlslKnownProperties
|
||||
{
|
||||
|
||||
@@ -260,6 +260,11 @@ internal sealed class ShaderSourceRewriter : HlslSourceRewriter
|
||||
if (fieldOperation.Field.IsConst &&
|
||||
fieldOperation.Type!.TypeKind != TypeKind.Enum)
|
||||
{
|
||||
if (HlslKnownFields.TryGetMappedName(fieldOperation.Member.ToDisplayString(), out string? constantLiteral))
|
||||
{
|
||||
return ParseExpression(constantLiteral!);
|
||||
}
|
||||
|
||||
ConstantDefinitions[fieldOperation.Field] = ((IFormattable)fieldOperation.Field.ConstantValue!).ToString(null, CultureInfo.InvariantCulture);
|
||||
|
||||
var ownerTypeName = ((INamedTypeSymbol)fieldOperation.Field.ContainingSymbol).ToDisplayString().ToHlslIdentifierName();
|
||||
|
||||
@@ -59,6 +59,11 @@ internal sealed class StaticFieldRewriter : HlslSourceRewriter
|
||||
fieldOperation.Field.IsConst &&
|
||||
fieldOperation.Type!.TypeKind != TypeKind.Enum)
|
||||
{
|
||||
if (HlslKnownFields.TryGetMappedName(fieldOperation.Member.ToDisplayString(), out string? constantLiteral))
|
||||
{
|
||||
return ParseExpression(constantLiteral!);
|
||||
}
|
||||
|
||||
ConstantDefinitions[fieldOperation.Field] = ((IFormattable)fieldOperation.Field.ConstantValue!).ToString(null, CultureInfo.InvariantCulture);
|
||||
|
||||
var ownerTypeName = ((INamedTypeSymbol)fieldOperation.Field.ContainingSymbol).ToDisplayString().ToHlslIdentifierName();
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
using ComputeSharp.Tests.Attributes;
|
||||
using ComputeSharp.Tests.Extensions;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace ComputeSharp.Tests;
|
||||
|
||||
[TestClass]
|
||||
[TestCategory("ShaderRewriter")]
|
||||
public partial class ShaderRewriterTests
|
||||
{
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
public void NanAndInfinite(Device device)
|
||||
{
|
||||
if (!device.Get().IsDoublePrecisionSupportAvailable())
|
||||
{
|
||||
Assert.Inconclusive();
|
||||
}
|
||||
|
||||
using ReadWriteBuffer<float> buffer1 = device.Get().AllocateReadWriteBuffer<float>(16);
|
||||
using ReadWriteBuffer<double> buffer2 = device.Get().AllocateReadWriteBuffer<double>(16);
|
||||
|
||||
device.Get().For(1, new NanAndInfiniteShader(buffer1, buffer2));
|
||||
|
||||
float[] results1 = buffer1.ToArray();
|
||||
double[] results2 = buffer2.ToArray();
|
||||
|
||||
Assert.IsTrue(float.IsNaN(results1[0]));
|
||||
Assert.IsTrue(float.IsPositiveInfinity(results1[1]));
|
||||
Assert.IsTrue(float.IsNegativeInfinity(results1[2]));
|
||||
Assert.AreEqual(results1[3], 1);
|
||||
Assert.AreEqual(results1[4], 1);
|
||||
Assert.AreEqual(results1[5], 1);
|
||||
Assert.IsTrue(float.IsNaN(results1[6]));
|
||||
Assert.IsTrue(float.IsPositiveInfinity(results1[7]));
|
||||
Assert.IsTrue(float.IsNegativeInfinity(results1[8]));
|
||||
Assert.AreEqual(results1[9], 1);
|
||||
Assert.AreEqual(results1[10], 1);
|
||||
Assert.AreEqual(results1[11], 1);
|
||||
|
||||
Assert.IsTrue(double.IsNaN(results2[0]));
|
||||
Assert.IsTrue(double.IsPositiveInfinity(results2[1]));
|
||||
Assert.IsTrue(double.IsNegativeInfinity(results2[2]));
|
||||
Assert.IsTrue(double.IsNaN(results2[3]));
|
||||
Assert.IsTrue(double.IsPositiveInfinity(results2[4]));
|
||||
Assert.IsTrue(double.IsNegativeInfinity(results2[5]));
|
||||
}
|
||||
|
||||
[AutoConstructor]
|
||||
internal readonly partial struct NanAndInfiniteShader : IComputeShader
|
||||
{
|
||||
public readonly ReadWriteBuffer<float> buffer1;
|
||||
public readonly ReadWriteBuffer<double> buffer2;
|
||||
|
||||
static readonly float float1 = float.NaN;
|
||||
static readonly float float2 = float.PositiveInfinity;
|
||||
static readonly float float3 = float.NegativeInfinity;
|
||||
static readonly double double1 = double.NaN;
|
||||
static readonly double double2 = double.PositiveInfinity;
|
||||
static readonly double double3 = double.NegativeInfinity;
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
buffer1[0] = float.NaN;
|
||||
buffer1[1] = float.PositiveInfinity;
|
||||
buffer1[2] = float.NegativeInfinity;
|
||||
buffer1[3] = Hlsl.IsNaN(buffer1[0]) ? 1 : 0;
|
||||
buffer1[4] = Hlsl.IsInfinite(buffer1[1]) ? 1 : 0;
|
||||
buffer1[5] = Hlsl.IsInfinite(buffer1[2]) ? 1 : 0;
|
||||
buffer1[6] = float1;
|
||||
buffer1[7] = float2;
|
||||
buffer1[8] = float3;
|
||||
buffer1[9] = Hlsl.IsNaN(buffer1[6]) ? 1 : 0;
|
||||
buffer1[10] = Hlsl.IsInfinite(buffer1[7]) ? 1 : 0;
|
||||
buffer1[11] = Hlsl.IsInfinite(buffer1[8]) ? 1 : 0;
|
||||
|
||||
buffer2[0] = double.NaN;
|
||||
buffer2[1] = double.PositiveInfinity;
|
||||
buffer2[2] = double.NegativeInfinity;
|
||||
buffer2[3] = double1;
|
||||
buffer2[4] = double2;
|
||||
buffer2[5] = double3;
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする