Fixed dispatch data loading for matrix types

このコミットが含まれているのは:
Sergio Pedri committed 2021-03-05 15:27:18 +01:00
1 親 4d900664a4
コミット 1adde2afb1
2ファイル変更
+67 -11

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

+35 -11
ファイルの表示
@@ -181,21 +181,45 @@ namespace ComputeSharp.SourceGenerators
{
if (fieldSymbol.IsConst) continue;
// For scalar, vector and linear matrix types, serialize the value normally.
// Only the initial alignment needs to be considered, while data is packed.
var (fieldSize, fieldPack) = HlslKnownSizes.GetTypeInfo(typeName);
// Calculate the right offset with 16-bytes padding (HLSL constant buffer).
// Since we're in a constant buffer, we need to both pad the starting offset
// to be aligned to the packing size of the type, and also to align the initial
// offset to ensure that values do not cross 16 bytes boundaries either.
rawDataOffset = AlignmentHelper.AlignToBoundary(
AlignmentHelper.Pad(rawDataOffset, fieldPack),
fieldSize,
16);
// Check if the current type is a matrix type with more than one row. In this
// case, each row is aligned as if it was a separate array, so the start of
// each row needs to be at a multiple of 16 bytes (a float4 register).
if (HlslKnownTypes.IsNonLinearMatrixType(typeName, out string? elementName, out int rows, out int columns))
{
rawDataOffset = AlignmentHelper.Pad(rawDataOffset, 16);
statements.Add(ExpressionStatement(
ParseExpression($"Unsafe.WriteUnaligned(ref Unsafe.Add(ref r1, {rawDataOffset}), shader.{fieldSymbol.Name})")));
string
rowTypeName = $"ComputeSharp.{elementName}{columns}",
rowLocalName = $"__{fieldSymbol.Name}__row0";
rawDataOffset += fieldSize;
statements.Add(ParseStatement($"ref {rowTypeName} {rowLocalName} = ref Unsafe.As<{typeName}, {rowTypeName}>(ref Unsafe.AsRef(in shader.{fieldSymbol.Name}));"));
for (int i = 0; i < rows; i++)
{
statements.Add(ParseStatement($"Unsafe.WriteUnaligned(ref Unsafe.Add(ref r1, {rawDataOffset}), Unsafe.Add(ref {rowLocalName}, {i}));"));
rawDataOffset += Math.Max(fieldPack * columns, 16);
}
}
else
{
// Calculate the right offset with 16-bytes padding (HLSL constant buffer).
// Since we're in a constant buffer, we need to both pad the starting offset
// to be aligned to the packing size of the type, and also to align the initial
// offset to ensure that values do not cross 16 bytes boundaries either.
rawDataOffset = AlignmentHelper.AlignToBoundary(
AlignmentHelper.Pad(rawDataOffset, fieldPack),
fieldSize,
16);
statements.Add(ParseStatement($"Unsafe.WriteUnaligned(ref Unsafe.Add(ref r1, {rawDataOffset}), shader.{fieldSymbol.Name});"));
rawDataOffset += fieldSize;
}
}
}
+32
ファイルの表示
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Numerics;
using System.Text.RegularExpressions;
using ComputeSharp.SourceGenerators.Extensions;
using Microsoft.CodeAnalysis;
@@ -176,6 +177,37 @@ namespace ComputeSharp.SourceGenerators.Mappings
return KnownMatrixTypes.Any(type => type.FullName == typeName);
}
/// <summary>
/// Checks whether or not a given type name is a non linear matrix type.
/// That is, a matrix type with more than a single row (which affects the constant buffer alignment).
/// </summary>
/// <param name="typeName">The input type name to check.</param>
/// <param name="elementName">The element name of the matrix type.</param>
/// <param name="rows">The number of rows in the matrix type.</param>
/// <param name="columns">The number of columns in the matrix type.</param>
/// <returns>Whether or not <paramref name="typeName"/> represents a non linear matrix type.</returns>
public static bool IsNonLinearMatrixType(string typeName, out string? elementName, out int rows, out int columns)
{
if (KnownMatrixTypes.Any(type => type.FullName == typeName))
{
var match = Regex.Match(typeName, @"^ComputeSharp\.(Bool|Int|UInt|Float|Double)([2-4])x([1-4])$");
if (match.Success)
{
elementName = match.Groups[1].Value;
rows = int.Parse(match.Groups[2].Value);
columns = int.Parse(match.Groups[3].Value);
return true;
}
}
elementName = null;
rows = columns = 0;
return false;
}
/// <summary>
/// Gets the mapped HLSL-compatible type name for the input type symbol.
/// </summary>