diff --git a/src/ComputeSharp.Graphics/Helpers/AlignmentHelper.cs b/src/ComputeSharp.Core/Helpers/AlignmentHelper.cs similarity index 97% rename from src/ComputeSharp.Graphics/Helpers/AlignmentHelper.cs rename to src/ComputeSharp.Core/Helpers/AlignmentHelper.cs index 6191c0c3..20baf93f 100644 --- a/src/ComputeSharp.Graphics/Helpers/AlignmentHelper.cs +++ b/src/ComputeSharp.Core/Helpers/AlignmentHelper.cs @@ -1,7 +1,7 @@ using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; -namespace ComputeSharp.Graphics.Helpers +namespace ComputeSharp.Core.Helpers { /// /// A with helper methods to work with alignment and padding. diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs index 363c2a9a..e95116b7 100644 --- a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs @@ -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; diff --git a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj index d2b96445..566c816a 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj +++ b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj @@ -21,6 +21,7 @@ + diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs index 453a5b40..4f513246 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs @@ -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()) { @@ -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: