Fixed padding/alignment in dispatch data loader

このコミットが含まれているのは:
Sergio Pedri committed 2020-12-29 23:54:05 +01:00
1 親 0c553d5b09
コミット 5b2f90e368
4ファイル変更
+21 -3

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

@@ -1,7 +1,7 @@
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
namespace ComputeSharp.Graphics.Helpers
namespace ComputeSharp.Core.Helpers
{
/// <summary>
/// A <see langword="class"/> with helper methods to work with alignment and padding.
+1
ファイルの表示
@@ -1,6 +1,7 @@
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using ComputeSharp.Core.Helpers;
using ComputeSharp.Exceptions;
using ComputeSharp.Graphics;
using ComputeSharp.Graphics.Buffers.Abstract;
+1
ファイルの表示
@@ -21,6 +21,7 @@
<Compile Include="..\ComputeSharp.Core\Exceptions\InvalidExecutionContextException.cs" Link="Core\Exceptions\InvalidExecutionContextException.cs" />
<Compile Include="..\ComputeSharp.Core\Exceptions\MissingMethodSourceException.cs" Link="Core\Exceptions\MissingMethodSourceException.cs" />
<Compile Include="..\ComputeSharp.Core\Extensions\MethodInfoExtensions.cs" Link="Core\Extensions\MethodInfoExtensions.cs" />
<Compile Include="..\ComputeSharp.Core\Helpers\AlignmentHelper.cs" Link="Core\Helpers\AlignmentHelper.cs" />
<Compile Include="..\ComputeSharp.Core\Interfaces\IComputeShader.cs" Link="Core\Interfaces\IComputeShader.cs" />
<Compile Include="..\ComputeSharp.Core\Intrinsics\Attributes\PreserveMemberNameAttribute.cs" Link="Core\Intrinsics\Attributes\PreserveMemberNameAttribute.cs" />
<Compile Include="..\ComputeSharp.Core\Intrinsics\Hlsl.cs" Link="Core\Intrinsics\Hlsl.cs" />
+18 -2
ファイルの表示
@@ -3,6 +3,7 @@ using System.Collections.Immutable;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using ComputeSharp.Core.Helpers;
using ComputeSharp.SourceGenerators.Extensions;
using ComputeSharp.SourceGenerators.Mappings;
using Microsoft.CodeAnalysis;
@@ -42,7 +43,7 @@ namespace ComputeSharp.SourceGenerators
BlockSyntax block = Block();
int
resourceOffset = 0,
rawDataOffset = 0;
rawDataOffset = sizeof(int) * 3;
foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType<IFieldSymbol>())
{
@@ -61,6 +62,15 @@ namespace ComputeSharp.SourceGenerators
{
var info = 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, info.Pack),
info.Size,
16);
block = block.AddStatements(ExpressionStatement(
ParseExpression($"Unsafe.WriteUnaligned(ref Unsafe.Add(ref r1, {rawDataOffset}), shader.{fieldSymbol.Name})")));
@@ -68,7 +78,13 @@ namespace ComputeSharp.SourceGenerators
}
}
block = block.AddStatements(ReturnStatement(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(1024))));
// After all the captured fields have been processed, ansure the reported byte size for
// the local variables is padded to the standard alignment for constant buffers. This is
// necessary to enable loading all the dispatch data after reinterpreting it to a sequence
// of values of size 16 bytes (in particular, Int4 is used), without reading out of bounds.
rawDataOffset = AlignmentHelper.Pad(rawDataOffset, 16);
block = block.AddStatements(ReturnStatement(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(rawDataOffset))));
// Create a static method to create the combined hashcode for a given shader type.
// This code takes a block syntax and produces a compilation unit as follows: