Merge pull request #364 from Sergio0694/dev/hlsl-cast-intrinsics
Add HLSL intrinsics to cast to/from bool types
このコミットが含まれているのは:
16ファイル変更
+1802
-226
検索条件に一致するファイルはありません
@@ -12,6 +12,7 @@
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\InteropServices\MemoryMarshal.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime.CompilerServices\InterpolatedStringHandlerArgumentAttribute.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime.CompilerServices\InterpolatedStringHandlerAttribute.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime.CompilerServices\IsExternalInit.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime.CompilerServices\RuntimeHelpers.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime.CompilerServices\SkipLocalsInitAttribute.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
差分なしでファイル名を変更。
@@ -16,8 +16,8 @@
|
||||
and to match members based on full name, so the types being in the generator assembly are not an issue.-->
|
||||
<ItemGroup>
|
||||
<Compile Include="..\ComputeSharp.Core\Attributes\AutoConstructorAttribute.cs" Link="ComputeSharp.Core\Attributes\AutoConstructorAttribute.cs" />
|
||||
<Compile Include="..\ComputeSharp.Core\Attributes\Internals\AutoConstructorBehaviorAttribute.cs" Link="ComputeSharp.Core\Attributes\Internals\AutoConstructorBehaviorAttribute.cs" />
|
||||
<Compile Include="..\ComputeSharp.Core\Attributes\Internals\AutoConstructorBehavior.cs" Link="ComputeSharp.Core\Attributes\Internals\AutoConstructorBehavior.cs" />
|
||||
<Compile Include="..\ComputeSharp.Core\Attributes\Internals\AutoConstructorBehaviorAttribute.cs" Link="ComputeSharp.Core\Attributes\Internals\AutoConstructorBehaviorAttribute.cs" />
|
||||
<Compile Include="..\ComputeSharp.Core\Attributes\Internals\AutoConstructorBehavior.cs" Link="ComputeSharp.Core\Attributes\Internals\AutoConstructorBehavior.cs" />
|
||||
<Compile Include="..\ComputeSharp.Core\Exceptions\InvalidExecutionContextException.cs" Link="ComputeSharp.Core\Exceptions\InvalidExecutionContextException.cs" />
|
||||
<Compile Include="..\ComputeSharp.Core\Intrinsics\Attributes\HlslIntrinsicNameAttribute.cs" Link="ComputeSharp.Core\Intrinsics\Attributes\HlslIntrinsicNameAttribute.cs" />
|
||||
<Compile Include="..\ComputeSharp.Core\Intrinsics\Hlsl.cs" Link="ComputeSharp.Core\Intrinsics\Hlsl.cs" />
|
||||
|
||||
-12
@@ -1,12 +0,0 @@
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace System.Runtime.CompilerServices;
|
||||
|
||||
/// <summary>
|
||||
/// Reserved to be used by the compiler for tracking metadata.
|
||||
/// This class should not be used by developers in source code.
|
||||
/// </summary>
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
internal static class IsExternalInit
|
||||
{
|
||||
}
|
||||
@@ -22,208 +22,17 @@
|
||||
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- Import all .tt files with their generated .g.cs files -->
|
||||
<ItemGroup>
|
||||
<Compile Update="Intrinsics\Hlsl.Void.g.cs">
|
||||
<None Update="**\*.tt">
|
||||
<LastGenOutput>%(Filename).g.cs</LastGenOutput>
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
</None>
|
||||
<Compile Update="**\*.g.cs">
|
||||
<DependentUpon>$([System.IO.Path]::GetFileNameWithoutExtension('%(Filename)')).tt</DependentUpon>
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Hlsl.Void.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Intrinsics\Hlsl.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Hlsl.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Primitives\Bool\Bool2.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Bool2.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Primitives\Bool\Bool3.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Bool3.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Primitives\Bool\Bool4.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Bool4.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Primitives\Bool\BoolMxN.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>BoolMxN.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Primitives\Double\Double2.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Double2.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Primitives\Double\Double3.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Double3.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Primitives\Double\Double4.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Double4.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Primitives\Double\DoubleMxN.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>DoubleMxN.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Primitives\Float\Float2.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Float2.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Primitives\Float\Float3.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Float3.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Primitives\Float\Float4.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Float4.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Primitives\Float\FloatMxN.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>FloatMxN.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Primitives\Int\Int2.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Int2.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Primitives\Int\Int3.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Int3.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Primitives\Int\Int4.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Int4.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Primitives\Int\IntMxN.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>IntMxN.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Primitives\UInt\UInt2.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>UInt2.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Primitives\UInt\UInt3.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>UInt3.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Primitives\UInt\UInt4.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>UInt4.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="Primitives\UInt\UIntMxN.g.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>UIntMxN.tt</DependentUpon>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="Intrinsics\Hlsl.Void.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>Hlsl.Void.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Intrinsics\Hlsl.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>Hlsl.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Primitives\Bool\Bool2.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>Bool2.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Primitives\Bool\Bool3.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>Bool3.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Primitives\Bool\Bool4.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>Bool4.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Primitives\Bool\BoolMxN.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>BoolMxN.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Primitives\Double\Double2.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>Double2.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Primitives\Double\Double3.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>Double3.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Primitives\Double\Double4.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>Double4.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Primitives\Double\DoubleMxN.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>DoubleMxN.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Primitives\Float\Float2.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>Float2.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Primitives\Float\Float3.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>Float3.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Primitives\Float\Float4.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>Float4.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Primitives\Float\FloatMxN.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>FloatMxN.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Primitives\Int\Int2.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>Int2.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Primitives\Int\Int3.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>Int3.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Primitives\Int\Int4.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>Int4.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Primitives\Int\IntMxN.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>IntMxN.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Primitives\UInt\UInt2.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>UInt2.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Primitives\UInt\UInt3.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>UInt3.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Primitives\UInt\UInt4.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>UInt4.g.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Update="Primitives\UInt\UIntMxN.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>UIntMxN.g.cs</LastGenOutput>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
<!-- Shared project with .NET Standard 2.0 polyfills -->
|
||||
|
||||
@@ -23,4 +23,9 @@ internal sealed class HlslIntrinsicNameAttribute : Attribute
|
||||
/// Gets the name of the HLSL intrinsic.
|
||||
/// </summary>
|
||||
public string Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether or not matching parameters is also required to extract the name of this intrinsic.
|
||||
/// </summary>
|
||||
public bool RequiresParametersMatching { get; init; }
|
||||
}
|
||||
ファイル差分が大きすぎるため表示されません。
差分を読み込み
@@ -0,0 +1,80 @@
|
||||
<#@ template debug="false" hostspecific="false" language="C#" #>
|
||||
<#@ assembly name="System.Core" #>
|
||||
<#@ import namespace="System.Collections.Generic" #>
|
||||
<#@ output extension=".g.cs"#>
|
||||
using ComputeSharp.Core.Intrinsics.Attributes;
|
||||
|
||||
namespace ComputeSharp;
|
||||
|
||||
/// <inheritdoc cref="Hlsl"/>
|
||||
partial class Hlsl
|
||||
{
|
||||
<#
|
||||
List<(string Type, List<(string Source, string Target)> Info)> intrinsics = new List<(string, List<(string, string)>)>();
|
||||
|
||||
// Prepare the intrinsic names
|
||||
foreach (var type in new[] { "Float", "Double", "Int", "UInt" })
|
||||
{
|
||||
List<(string Source, string Target)> typeNames = new List<(string, string)>();
|
||||
|
||||
typeNames.Add((type.ToLower(), "bool"));
|
||||
|
||||
// Vector types
|
||||
for (int i = 2; i <= 4; i++)
|
||||
{
|
||||
typeNames.Add(($"{type}{i}", $"Bool{i}"));
|
||||
}
|
||||
|
||||
// Matrix types
|
||||
for (int i = 1; i <= 4; i++)
|
||||
{
|
||||
for (int j = 1; j <= 4; j++)
|
||||
{
|
||||
typeNames.Add(($"{type}{i}x{j}", $"Bool{i}x{j}"));
|
||||
}
|
||||
}
|
||||
|
||||
intrinsics.Add((type, typeNames));
|
||||
}
|
||||
|
||||
// Numeric to bool
|
||||
foreach (var intrinsic in intrinsics)
|
||||
{
|
||||
int index = 0;
|
||||
|
||||
foreach (var pair in intrinsic.Info)
|
||||
{
|
||||
if (index++ != 0) WriteLine("");
|
||||
#>
|
||||
/// <summary>
|
||||
/// Converts the input <see cref="<#=pair.Source#>"/> value into a <see cref="<#=pair.Target#>"/> value (non-zero elements map to <see langword="true"/>, zero elements map to <see langword="false"/>).
|
||||
/// </summary>
|
||||
/// <param name="x">The input <see cref="<#=pair.Source#>"/> value.</param>
|
||||
/// <returns>The converted <see cref="<#=pair.Target#>"/> value.</returns>
|
||||
/// <remarks>This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior.</remarks>
|
||||
[HlslIntrinsicName("<#=pair.Target.ToLower()#>", RequiresParametersMatching = true)]
|
||||
public static <#=pair.Target#> <#=intrinsic.Type#>ToBool(<#=pair.Source#> x) => default;
|
||||
<#
|
||||
}
|
||||
}
|
||||
|
||||
// Bool to numeric
|
||||
foreach (var intrinsic in intrinsics)
|
||||
{
|
||||
foreach (var pair in intrinsic.Info)
|
||||
{
|
||||
WriteLine("");
|
||||
#>
|
||||
/// <summary>
|
||||
/// Converts the input <see cref="<#=pair.Target#>"/> value into a <see cref="<#=pair.Source#>"/> value (<see langword="true"/> elements map to 1, <see langword="false"/> map to 0).
|
||||
/// </summary>
|
||||
/// <param name="x">The input <see cref="<#=pair.Target#>"/> value.</param>
|
||||
/// <returns>The converted <see cref="<#=pair.Source#>"/> value.</returns>
|
||||
/// <remarks>This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior.</remarks>
|
||||
[HlslIntrinsicName("<#=pair.Source.ToLower()#>", RequiresParametersMatching = true)]
|
||||
public static <#=pair.Source#> BoolTo<#=intrinsic.Type#>(<#=pair.Target#> x) => default;
|
||||
<#
|
||||
}
|
||||
}
|
||||
#>
|
||||
}
|
||||
@@ -15,7 +15,6 @@
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\RuntimeHelpers.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)..\ComputeSharp.NetStandard\System\Runtime\InteropServices\NativeMemory.cs" Link="System\Runtime\InteropServices\NativeMemory.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)..\ComputeSharp.NetStandard\System\Runtime\InteropServices\UnmanagedCallersOnlyAttribute.cs" Link="System\Runtime\InteropServices\UnmanagedCallersOnlyAttribute.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)..\ComputeSharp.NetStandard\System\Runtime\CompilerServices\IsExternalInit.cs" Link="System\Runtime\CompilerServices\IsExternalInit.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Text\EncodingExtensions.cs" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
@@ -18,6 +18,7 @@
|
||||
<Compile Include="..\ComputeSharp.Core\Attributes\AutoConstructorAttribute.cs" Link="ComputeSharp.Core\Attributes\AutoConstructorAttribute.cs" />
|
||||
<Compile Include="..\ComputeSharp.Core\Exceptions\InvalidExecutionContextException.cs" Link="ComputeSharp.Core\Exceptions\InvalidExecutionContextException.cs" />
|
||||
<Compile Include="..\ComputeSharp.Core\Intrinsics\Attributes\HlslIntrinsicNameAttribute.cs" Link="ComputeSharp.Core\Intrinsics\Attributes\HlslIntrinsicNameAttribute.cs" />
|
||||
<Compile Include="..\ComputeSharp.Core\Intrinsics\Hlsl.Casts.g.cs" Link="ComputeSharp.Core\Intrinsics\Hlsl.Casts.g.cs" />
|
||||
<Compile Include="..\ComputeSharp.Core\Intrinsics\Hlsl.cs" Link="ComputeSharp.Core\Intrinsics\Hlsl.cs" />
|
||||
<Compile Include="..\ComputeSharp.Core\Intrinsics\Hlsl.g.cs" Link="ComputeSharp.Core\Intrinsics\Hlsl.g.cs" />
|
||||
<Compile Include="..\ComputeSharp.Core\Intrinsics\Hlsl.Void.g.cs" Link="ComputeSharp.Core\Intrinsics\Hlsl.Void.g.cs" />
|
||||
|
||||
@@ -19,7 +19,6 @@
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\FormatExtensions.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\IO\StreamExtensions.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Path.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\IsExternalInit.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\MemoryMarshal.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\RuntimeHelpers.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\UnmanagedCallersOnlyAttribute.cs" />
|
||||
|
||||
@@ -17,6 +17,11 @@ internal static partial class HlslKnownMethods
|
||||
/// </summary>
|
||||
private static readonly IReadOnlyDictionary<string, string> KnownMethods = BuildKnownMethodsMap();
|
||||
|
||||
/// <summary>
|
||||
/// The mapping of supported known methods that require parameters mapping to HLSL names.
|
||||
/// </summary>
|
||||
private static readonly IReadOnlyDictionary<string, string> KnownMethodsParametersMapping = BuildKnownMethodsParametersMappingMap();
|
||||
|
||||
/// <summary>
|
||||
/// Builds the mapping of supported known methods to HLSL names.
|
||||
/// </summary>
|
||||
@@ -122,19 +127,32 @@ internal static partial class HlslKnownMethods
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.Normalize)}"] = "normalize"
|
||||
};
|
||||
|
||||
// Programmatically load mappings from the Hlsl class as well
|
||||
// Programmatically load mappings from the Hlsl class as well (the ones with no parameter matching)
|
||||
foreach (var method in
|
||||
from method in typeof(Hlsl).GetMethods(BindingFlags.Public | BindingFlags.Static)
|
||||
group method by method.Name
|
||||
into groups
|
||||
select groups.First())
|
||||
{
|
||||
// Check whether the current method should be translated with the original name
|
||||
// or with the lowercase version. This is needed because all C# methods are exposed
|
||||
// with the upper camel case format, while HLSL intrinsics use multiple different formats.
|
||||
string hlslName = method.GetCustomAttribute<HlslIntrinsicNameAttribute>()?.Name ?? method.Name;
|
||||
HlslIntrinsicNameAttribute? attribute = method.GetCustomAttribute<HlslIntrinsicNameAttribute>();
|
||||
|
||||
knownMethods.Add($"{typeof(Hlsl).FullName}{Type.Delimiter}{method.Name}", hlslName);
|
||||
// If the intrinsic requires parameters matching, skip it. It will be tracked separately.
|
||||
// To avoid processing all types again, add a special identifier for it to detect it.
|
||||
if (attribute is { RequiresParametersMatching: true })
|
||||
{
|
||||
knownMethods.Add($"{typeof(Hlsl).FullName}{Type.Delimiter}{method.Name}", nameof(HlslIntrinsicNameAttribute.RequiresParametersMatching));
|
||||
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Check whether the current method should be translated with the original name
|
||||
// or with the lowercase version. This is needed because all C# methods are exposed
|
||||
// with the upper camel case format, while HLSL intrinsics use multiple different formats.
|
||||
string hlslName = attribute?.Name ?? method.Name;
|
||||
|
||||
knownMethods.Add($"{typeof(Hlsl).FullName}{Type.Delimiter}{method.Name}", hlslName);
|
||||
}
|
||||
}
|
||||
|
||||
// Let other types inject additional methods
|
||||
@@ -143,6 +161,38 @@ internal static partial class HlslKnownMethods
|
||||
return knownMethods;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds the mapping of supported known methods that require parameters mapping to HLSL names.
|
||||
/// </summary>
|
||||
private static IReadOnlyDictionary<string, string> BuildKnownMethodsParametersMappingMap()
|
||||
{
|
||||
ILookup<string, MethodInfo> methodsLookup = typeof(Hlsl).GetMethods(BindingFlags.Public | BindingFlags.Static).ToLookup(static method => method.Name);
|
||||
|
||||
Dictionary<string, string> knownMethods = new();
|
||||
|
||||
// Go through the previously discovered methods to find the ones requiring parameters mapping
|
||||
foreach (var pair in KnownMethods)
|
||||
{
|
||||
// Only process methods marked as requiring parameters mapping
|
||||
if (pair.Value != nameof(HlslIntrinsicNameAttribute.RequiresParametersMatching))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Go through the intrinsics with the current name, and track the method name and parameters. We don't need
|
||||
// to also track the fully qualified type name, as that part has already been matched previously at this point.
|
||||
foreach (MethodInfo method in methodsLookup[pair.Key.Split('.').Last()])
|
||||
{
|
||||
// The key is in the format "<METHOD_NAME>(<PARAMETER_TYPES>)", and the intrinsic name is guaranteed to be available
|
||||
knownMethods.Add(
|
||||
$"{method.Name}({string.Join(", ", method.GetParameters().Select(static p => p.ParameterType.Name))})",
|
||||
method.GetCustomAttribute<HlslIntrinsicNameAttribute>()!.Name);
|
||||
}
|
||||
}
|
||||
|
||||
return knownMethods;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds more known methods to the mapping to use.
|
||||
/// </summary>
|
||||
@@ -176,9 +226,40 @@ internal static partial class HlslKnownMethods
|
||||
/// </summary>
|
||||
/// <param name="name">The input fully qualified method name.</param>
|
||||
/// <param name="mapped">The mapped name, if one is found.</param>
|
||||
/// <returns>The HLSL-compatible member name that can be used in an HLSL shader.</returns>
|
||||
public static bool TryGetMappedName(string name, out string? mapped)
|
||||
/// <param name="requiresParametersMapping">Whether the method is an intrinsic, but it requires matching parameters too.</param>
|
||||
/// <returns>Whether the input method was an intrinsic.</returns>
|
||||
public static bool TryGetMappedName(string name, out string? mapped, out bool requiresParametersMapping)
|
||||
{
|
||||
return KnownMethods.TryGetValue(name, out mapped);
|
||||
if (KnownMethods.TryGetValue(name, out string? value))
|
||||
{
|
||||
if (value == nameof(HlslIntrinsicNameAttribute.RequiresParametersMatching))
|
||||
{
|
||||
mapped = null;
|
||||
requiresParametersMapping = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
mapped = value;
|
||||
requiresParametersMapping = false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
mapped = null;
|
||||
requiresParametersMapping = false;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mapped HLSL-compatible method name for the input method name and parameters.
|
||||
/// </summary>
|
||||
/// <param name="name">The method name (previously validated with <see cref="TryGetMappedName"/>).</param>
|
||||
/// <param name="parameterTypeNames">The sequence of type names for the method.</param>
|
||||
/// <returns>The intrinsic name for the current method.</returns>
|
||||
public static string GetMappedNameWithParameters(string name, IEnumerable<string> parameterTypeNames)
|
||||
{
|
||||
return KnownMethodsParametersMapping[$"{name}({string.Join(", ", parameterTypeNames)})"];
|
||||
}
|
||||
}
|
||||
@@ -353,8 +353,13 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter
|
||||
// If the invocation consists of invoking a static method that has a direct
|
||||
// mapping to HLSL, rewrite the expression in the current invocation node.
|
||||
// For instance: Math.Abs(expr) => abs(expr).
|
||||
if (HlslKnownMethods.TryGetMappedName(metadataName, out string? mapping))
|
||||
if (HlslKnownMethods.TryGetMappedName(metadataName, out string? mapping, out bool requiresParametersMapping))
|
||||
{
|
||||
if (requiresParametersMapping)
|
||||
{
|
||||
mapping = HlslKnownMethods.GetMappedNameWithParameters(method.Name, method.Parameters.Select(static p => p.Type.Name));
|
||||
}
|
||||
|
||||
// Allow specialized types to track the method invocation, if needed
|
||||
TrackKnownMethodInvocation(metadataName);
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using ComputeSharp.SourceGeneration.Extensions;
|
||||
using ComputeSharp.SourceGeneration.Helpers;
|
||||
using ComputeSharp.SourceGeneration.Mappings;
|
||||
@@ -98,8 +99,13 @@ internal sealed partial class StaticFieldRewriter : HlslSourceRewriter
|
||||
method.IsStatic)
|
||||
{
|
||||
// Rewrite HLSL intrinsic methods
|
||||
if (HlslKnownMethods.TryGetMappedName(method.GetFullMetadataName(), out string? mapping))
|
||||
if (HlslKnownMethods.TryGetMappedName(method.GetFullMetadataName(), out string? mapping, out bool requiresParametersMapping))
|
||||
{
|
||||
if (requiresParametersMapping)
|
||||
{
|
||||
mapping = HlslKnownMethods.GetMappedNameWithParameters(method.Name, method.Parameters.Select(static p => p.Type.Name));
|
||||
}
|
||||
|
||||
return updatedNode.WithExpression(ParseExpression(mapping!));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
<Compile Include="..\ComputeSharp.Core\Attributes\AutoConstructorAttribute.cs" Link="ComputeSharp.Core\Attributes\AutoConstructorAttribute.cs" />
|
||||
<Compile Include="..\ComputeSharp.Core\Exceptions\InvalidExecutionContextException.cs" Link="ComputeSharp.Core\Exceptions\InvalidExecutionContextException.cs" />
|
||||
<Compile Include="..\ComputeSharp.Core\Intrinsics\Attributes\HlslIntrinsicNameAttribute.cs" Link="ComputeSharp.Core\Intrinsics\Attributes\HlslIntrinsicNameAttribute.cs" />
|
||||
<Compile Include="..\ComputeSharp.Core\Intrinsics\Hlsl.Casts.g.cs" Link="ComputeSharp.Core\Intrinsics\Hlsl.Casts.g.cs" />
|
||||
<Compile Include="..\ComputeSharp.Core\Intrinsics\Hlsl.cs" Link="ComputeSharp.Core\Intrinsics\Hlsl.cs" />
|
||||
<Compile Include="..\ComputeSharp.Core\Intrinsics\Hlsl.g.cs" Link="ComputeSharp.Core\Intrinsics\Hlsl.g.cs" />
|
||||
<Compile Include="..\ComputeSharp.Core\Intrinsics\Hlsl.Void.g.cs" Link="ComputeSharp.Core\Intrinsics\Hlsl.Void.g.cs" />
|
||||
|
||||
@@ -237,6 +237,163 @@ public partial class ShaderRewriterTests
|
||||
}
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
public void ToBooleanConversionHlslIntrinsics(Device device)
|
||||
{
|
||||
float[] data1 = new[] { 3.14f, 0, 1.44f, 0, 0, 0.4445f };
|
||||
int[] data2 = new[] { 3, 0, 1, 0, 0, 156 };
|
||||
|
||||
using ReadOnlyBuffer<float> buffer1 = device.Get().AllocateReadOnlyBuffer(data1);
|
||||
using ReadOnlyBuffer<int> buffer2 = device.Get().AllocateReadOnlyBuffer(data2);
|
||||
using ReadWriteBuffer<int> buffer3 = device.Get().AllocateReadWriteBuffer<int>(18, AllocationMode.Clear);
|
||||
|
||||
device.Get().For(1, new ToBooleanConversionIntrinsicsShader(buffer1, buffer2, buffer3));
|
||||
|
||||
int[] results = buffer3.ToArray();
|
||||
|
||||
CollectionAssert.AreEqual(
|
||||
expected: new[] { 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1 },
|
||||
actual: results);
|
||||
}
|
||||
|
||||
[AutoConstructor]
|
||||
internal readonly partial struct ToBooleanConversionIntrinsicsShader : IComputeShader
|
||||
{
|
||||
public readonly ReadOnlyBuffer<float> buffer1;
|
||||
public readonly ReadOnlyBuffer<int> buffer2;
|
||||
public readonly ReadWriteBuffer<int> result;
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
float f1 = buffer1[0];
|
||||
float f2 = buffer1[1];
|
||||
float4 f4 = new(buffer1[2], buffer1[3], buffer1[4], buffer1[5]);
|
||||
|
||||
int i1 = buffer2[0];
|
||||
int i2 = buffer2[1];
|
||||
int4 i4 = new(buffer2[2], buffer2[3], buffer2[4], buffer2[5]);
|
||||
|
||||
uint u1 = (uint)buffer2[0];
|
||||
uint u2 = (uint)buffer2[1];
|
||||
uint4 u4 = new((uint)buffer2[2], (uint)buffer2[3], (uint)buffer2[4], (uint)buffer2[5]);
|
||||
|
||||
result[0] = Hlsl.FloatToBool(f1) ? 1 : 0;
|
||||
result[1] = Hlsl.FloatToBool(f2) ? 1 : 0;
|
||||
|
||||
bool4 f4b = Hlsl.FloatToBool(f4);
|
||||
|
||||
result[2] = f4b[0] ? 1 : 0;
|
||||
result[3] = f4b[1] ? 1 : 0;
|
||||
result[4] = f4b[2] ? 1 : 0;
|
||||
result[5] = f4b[3] ? 1 : 0;
|
||||
|
||||
result[6] = Hlsl.IntToBool(i1) ? 1 : 0;
|
||||
result[7] = Hlsl.IntToBool(i2) ? 1 : 0;
|
||||
|
||||
bool4 i4b = Hlsl.IntToBool(i4);
|
||||
|
||||
result[8] = i4b[0] ? 1 : 0;
|
||||
result[9] = i4b[1] ? 1 : 0;
|
||||
result[10] = i4b[2] ? 1 : 0;
|
||||
result[11] = i4b[3] ? 1 : 0;
|
||||
|
||||
result[12] = Hlsl.UIntToBool(u1) ? 1 : 0;
|
||||
result[13] = Hlsl.UIntToBool(u2) ? 1 : 0;
|
||||
|
||||
bool4 u4b = Hlsl.UIntToBool(u4);
|
||||
|
||||
result[14] = u4b[0] ? 1 : 0;
|
||||
result[15] = u4b[1] ? 1 : 0;
|
||||
result[16] = u4b[2] ? 1 : 0;
|
||||
result[17] = u4b[3] ? 1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
public void FromBooleanConversionHlslIntrinsics(Device device)
|
||||
{
|
||||
int[] data = new[] { 1, 0, 1, 0, 0, 1 };
|
||||
|
||||
using ReadOnlyBuffer<int> buffer1 = device.Get().AllocateReadOnlyBuffer(data);
|
||||
using ReadWriteBuffer<float> buffer2 = device.Get().AllocateReadWriteBuffer<float>(6, AllocationMode.Clear);
|
||||
using ReadWriteBuffer<int> buffer3 = device.Get().AllocateReadWriteBuffer<int>(6, AllocationMode.Clear);
|
||||
using ReadWriteBuffer<uint> buffer4 = device.Get().AllocateReadWriteBuffer<uint>(6, AllocationMode.Clear);
|
||||
|
||||
device.Get().For(1, new FromBooleanConversionIntrinsicsShader(buffer1, buffer2, buffer3, buffer4));
|
||||
|
||||
float[] results1 = buffer2.ToArray();
|
||||
int[] results2 = buffer3.ToArray();
|
||||
uint[] results3 = buffer4.ToArray();
|
||||
|
||||
CollectionAssert.AreEqual(
|
||||
expected: new float[] { 1, 0, 1, 0, 0, 1 },
|
||||
actual: results1);
|
||||
|
||||
CollectionAssert.AreEqual(
|
||||
expected: new int[] { 1, 0, 1, 0, 0, 1 },
|
||||
actual: results2);
|
||||
|
||||
CollectionAssert.AreEqual(
|
||||
expected: new uint[] { 1, 0, 1, 0, 0, 1 },
|
||||
actual: results3);
|
||||
}
|
||||
|
||||
[AutoConstructor]
|
||||
internal readonly partial struct FromBooleanConversionIntrinsicsShader : IComputeShader
|
||||
{
|
||||
public readonly ReadOnlyBuffer<int> source;
|
||||
public readonly ReadWriteBuffer<float> destination1;
|
||||
public readonly ReadWriteBuffer<int> destination2;
|
||||
public readonly ReadWriteBuffer<uint> destination3;
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
bool b1 = source[0] != 0;
|
||||
bool b2 = source[1] != 0;
|
||||
bool4 b4 = new(source[2] != 0, source[3] != 0, source[4] != 0, source[5] != 0);
|
||||
|
||||
bool b11 = source[0] != 0;
|
||||
bool b22 = source[1] != 0;
|
||||
bool4 b44 = new(source[2] != 0, source[3] != 0, source[4] != 0, source[5] != 0);
|
||||
|
||||
bool b111 = source[0] != 0;
|
||||
bool b222 = source[1] != 0;
|
||||
bool4 b444 = new(source[2] != 0, source[3] != 0, source[4] != 0, source[5] != 0);
|
||||
|
||||
destination1[0] = Hlsl.BoolToFloat(b1);
|
||||
destination1[1] = Hlsl.BoolToFloat(b2);
|
||||
|
||||
float4 f4 = Hlsl.BoolToFloat(b4);
|
||||
|
||||
destination1[2] = f4.X;
|
||||
destination1[3] = f4.Y;
|
||||
destination1[4] = f4.Z;
|
||||
destination1[5] = f4.W;
|
||||
|
||||
destination2[0] = Hlsl.BoolToInt(b11);
|
||||
destination2[1] = Hlsl.BoolToInt(b22);
|
||||
|
||||
int4 i4 = Hlsl.BoolToInt(b44);
|
||||
|
||||
destination2[2] = i4.X;
|
||||
destination2[3] = i4.Y;
|
||||
destination2[4] = i4.Z;
|
||||
destination2[5] = i4.W;
|
||||
|
||||
destination3[0] = Hlsl.BoolToUInt(b111);
|
||||
destination3[1] = Hlsl.BoolToUInt(b222);
|
||||
|
||||
uint4 u4 = Hlsl.BoolToUInt(b444);
|
||||
|
||||
destination3[2] = u4.X;
|
||||
destination3[3] = u4.Y;
|
||||
destination3[4] = u4.Z;
|
||||
destination3[5] = u4.W;
|
||||
}
|
||||
}
|
||||
|
||||
// See: https://github.com/Sergio0694/ComputeSharp/issues/259
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする