diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
index 086410da..a546df0a 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
@@ -430,17 +430,17 @@ internal static class DiagnosticDescriptors
///
/// Gets a for a shader with a root signature that is too large.
///
- /// Format: "The D2D1 shader of type {0} has exceeded the maximum allowed size for captured values and resources".
+ /// Format: "The D2D1 shader of type {0} has exceeded the maximum allowed size for captured values".
///
///
public static readonly DiagnosticDescriptor ShaderDispatchDataSizeExceeded = new DiagnosticDescriptor(
id: "CMPSD2D0032",
title: "Shader dispatch data size exceeded",
- messageFormat: "The D2D1 shader of type {0} has exceeded the maximum allowed size for captured values and resources",
+ messageFormat: "The D2D1 shader of type {0} has exceeded the maximum allowed size for captured values",
category: "ComputeSharp.D2D1.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
- description: "The D2D1 shader of type {0} has exceeded the maximum allowed size for captured values and resources.",
+ description: "The D2D1 shader of type {0} has exceeded the maximum allowed size for captured values.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
///
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs
index 0904600b..0a88badb 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs
@@ -34,16 +34,16 @@ partial class ID2D1ShaderGenerator
.AddModifiers(Token(SyntaxKind.ReadOnlyKeyword))
.AddTypeParameterListParameters(TypeParameter(Identifier("TLoader")))
.AddParameterListParameters(Parameter(Identifier("loader")).AddModifiers(Token(SyntaxKind.RefKeyword)).WithType(IdentifierName("TLoader")))
- .WithBody(Block(GetDispatchDataLoadingStatements(dispatchInfo.FieldInfos, dispatchInfo.Root32BitConstantCount)));
+ .WithBody(Block(GetDispatchDataLoadingStatements(dispatchInfo.FieldInfos, dispatchInfo.ConstantBufferSizeInBytes)));
}
///
/// Gets a sequence of statements to load the dispatch data for a given shader.
///
/// The array of values for all captured fields.
- /// The total number of needed 32 bit constants in the shader root signature.
+ /// The size of the shader constant buffer.
/// The sequence of instances to load shader dispatch data.
- private static ImmutableArray GetDispatchDataLoadingStatements(ImmutableArray fieldInfos, int root32BitConstantsCount)
+ private static ImmutableArray GetDispatchDataLoadingStatements(ImmutableArray fieldInfos, int constantBufferSizeInBytes)
{
// If there are no fields, just load an empty buffer
if (fieldInfos.IsEmpty)
@@ -74,12 +74,12 @@ partial class ID2D1ShaderGenerator
// Read a primitive value and serialize it into the target buffer. This will generate:
//
- // global::System.Runtime.CompilerServices.Unsafe.As>(
+ // global::System.Runtime.CompilerServices.Unsafe.As>(
// ref global::System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref r0, (nint))) =
statements.Add(ExpressionStatement(
AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
- ParseExpression($"global::System.Runtime.CompilerServices.Unsafe.As(ref global::System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref r0, (nint){primitive.Offset}))"),
+ ParseExpression($"global::System.Runtime.CompilerServices.Unsafe.As(ref global::System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref r0, (nint){primitive.Offset}))"),
ParseExpression($"{string.Join(".", primitive.FieldPath)}"))));
break;
@@ -97,39 +97,39 @@ partial class ID2D1ShaderGenerator
// This will result in the following (assuming Float2x3 m):
//
// ref global::ComputeSharp.Float3 __m__row0 = ref global::System.Runtime.CompilerServices.Unsafe.As(ref global::System.Runtime.CompilerServices.Unsafe.AsRef(in m));
- // global::System.Runtime.CompilerServices.Unsafe.As(ref global::System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref r0, (nint)rawDataOffset)) = global::System.Runtime.CompilerServices.Unsafe.Add(ref __m__row0, 0);
- // global::System.Runtime.CompilerServices.Unsafe.As(ref global::System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref r0, (nint)(rawDataOffset + 16))) = global::System.Runtime.CompilerServices.Unsafe.Add(ref __m__row0, 1);
+ // global::System.Runtime.CompilerServices.Unsafe.As(ref global::System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref r0, (nint)rawDataOffset)) = global::System.Runtime.CompilerServices.Unsafe.Add(ref __m__row0, 0);
+ // global::System.Runtime.CompilerServices.Unsafe.As(ref global::System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref r0, (nint)(rawDataOffset + 16))) = global::System.Runtime.CompilerServices.Unsafe.Add(ref __m__row0, 1);
for (int j = 0; j < matrix.Rows; j++)
{
statements.Add(ExpressionStatement(
AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
- ParseExpression($"global::System.Runtime.CompilerServices.Unsafe.As(ref global::System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref r0, (nint){matrix.Offsets[j]}))"),
+ ParseExpression($"global::System.Runtime.CompilerServices.Unsafe.As(ref global::System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref r0, (nint){matrix.Offsets[j]}))"),
ParseExpression($"global::System.Runtime.CompilerServices.Unsafe.Add(ref {rowLocalName}, {j})"))));
}
break;
}
}
- // global::System.Span data = stackalloc uint[];
+ // global::System.Span data = stackalloc byte[];
statements.Insert(0,
LocalDeclarationStatement(
VariableDeclaration(
GenericName(Identifier("global::System.Span"))
- .AddTypeArgumentListArguments(PredefinedType(Token(SyntaxKind.UIntKeyword))))
+ .AddTypeArgumentListArguments(PredefinedType(Token(SyntaxKind.ByteKeyword))))
.AddVariables(
VariableDeclarator(Identifier("data"))
.WithInitializer(EqualsValueClause(
StackAllocArrayCreationExpression(
- ArrayType(PredefinedType(Token(SyntaxKind.UIntKeyword)))
+ ArrayType(PredefinedType(Token(SyntaxKind.ByteKeyword)))
.AddRankSpecifiers(
ArrayRankSpecifier(SingletonSeparatedList(
- LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(root32BitConstantsCount)))))))))));
+ LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(constantBufferSizeInBytes)))))))))));
- // ref uint r0 = ref data[0];
+ // ref byte r0 = ref data[0];
statements.Insert(1,
LocalDeclarationStatement(
- VariableDeclaration(RefType(PredefinedType(Token(SyntaxKind.UIntKeyword))))
+ VariableDeclaration(RefType(PredefinedType(Token(SyntaxKind.ByteKeyword))))
.AddVariables(
VariableDeclarator(Identifier("r0"))
.WithInitializer(EqualsValueClause(
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.cs
index f68e9c16..714ed97b 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.cs
@@ -25,12 +25,12 @@ partial class ID2D1ShaderGenerator
///
/// The collection of produced instances.
/// The current shader type being explored.
- /// The total number of needed 32 bit constants in the shader root signature.
+ /// The size of the shader constant buffer.
/// The sequence of instances for all captured resources and values.
public static ImmutableArray GetInfo(
ImmutableArray.Builder diagnostics,
ITypeSymbol structDeclarationSymbol,
- out int root32BitConstantCount)
+ out int constantBufferSizeInBytes)
{
// Helper method that uses boxes instead of ref-s (illegal in enumerators)
static IEnumerable GetCapturedFieldInfos(
@@ -87,23 +87,14 @@ partial class ID2D1ShaderGenerator
ImmutableArray.Empty,
rawDataOffsetAsBox).ToImmutableArray();
- // After all the captured fields have been processed, ansure the reported byte size for
- // the local variables is padded to a multiple of a 32 bit value. This is necessary to
- // enable loading all the dispatch data after reinterpreting it to a sequence of values
- // of size 32 bits (via SetComputeRoot32BitConstants), without reading out of bounds.
- root32BitConstantCount = AlignmentHelper.Pad(rawDataOffsetAsBox.Value, sizeof(int)) / sizeof(int);
+ constantBufferSizeInBytes = rawDataOffsetAsBox.Value;
- // A shader root signature has a maximum size of 64 DWORDs, so 256 bytes.
- // Loaded values in the root signature have the following costs:
- // - Root constants cost 1 DWORD each, since they are 32-bit values.
- // - Descriptor tables cost 1 DWORD each.
- // - Root descriptors (64-bit GPU virtual addresses) cost 2 DWORDs each.
- // So here we check whether the current signature respects that constraint,
- // and emit a build error otherwise. For more info on this, see the docs here:
- // https://docs.microsoft.com/windows/win32/direct3d12/root-signature-limits.
- int rootSignatureDwordSize = root32BitConstantCount;
+ // The maximum size for a constant buffer is 64KB
+ const int D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT = 4096;
+ const int MaximumConstantBufferSize = D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * 4 * sizeof(float);
- if (rootSignatureDwordSize > 64)
+ // Emit a diagnostic if the shader constant buffer is too large
+ if (constantBufferSizeInBytes > MaximumConstantBufferSize)
{
diagnostics.Add(ShaderDispatchDataSizeExceeded, structDeclarationSymbol, structDeclarationSymbol);
}
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs
index db0eb3ab..41f73ce5 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs
@@ -64,11 +64,11 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
ImmutableArray fieldInfos = LoadDispatchData.GetInfo(
diagnostics,
item.Left.Symbol,
- out int root32BitConstantCount);
+ out int constantBufferSizeInBytes);
token.ThrowIfCancellationRequested();
- DispatchDataInfo dispatchDataInfo = new(fieldInfos, root32BitConstantCount);
+ DispatchDataInfo dispatchDataInfo = new(fieldInfos, constantBufferSizeInBytes);
// Get the input info for GetInputInfo()
GetInputType.GetInfo(
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/DispatchDataInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/DispatchDataInfo.cs
index a34e3bad..fc36a429 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/Models/DispatchDataInfo.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/DispatchDataInfo.cs
@@ -10,8 +10,8 @@ namespace ComputeSharp.D2D1.SourceGenerators.Models;
/// A model representing gathered info on a shader dispatch data.
///
/// The description on shader instance fields.
-/// The size of the shader root signature, in 32 bit constants.
-internal sealed record DispatchDataInfo(ImmutableArray FieldInfos, int Root32BitConstantCount)
+/// The size of the shader constant buffer.
+internal sealed record DispatchDataInfo(ImmutableArray FieldInfos, int ConstantBufferSizeInBytes)
{
///
/// An implementation for .
@@ -22,7 +22,7 @@ internal sealed record DispatchDataInfo(ImmutableArray FieldInfos, in
protected override void AddToHashCode(ref HashCode hashCode, DispatchDataInfo obj)
{
hashCode.AddRange(obj.FieldInfos, FieldInfo.Comparer.Default);
- hashCode.Add(obj.Root32BitConstantCount);
+ hashCode.Add(obj.ConstantBufferSizeInBytes);
}
///
@@ -30,7 +30,7 @@ internal sealed record DispatchDataInfo(ImmutableArray FieldInfos, in
{
return
x.FieldInfos.SequenceEqual(y.FieldInfos, FieldInfo.Comparer.Default) &&
- x.Root32BitConstantCount == y.Root32BitConstantCount;
+ x.ConstantBufferSizeInBytes == y.ConstantBufferSizeInBytes;
}
}
}
diff --git a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1DispatchDataLoader.cs b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1DispatchDataLoader.cs
index 447e3cc9..96e35357 100644
--- a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1DispatchDataLoader.cs
+++ b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1DispatchDataLoader.cs
@@ -14,5 +14,5 @@ public interface ID2D1DispatchDataLoader
/// Loads the captured values used by the D2D1 shader to be dispatched.
///
/// The sequence of serialized captured values.
- void LoadConstantBuffer(ReadOnlySpan data);
+ void LoadConstantBuffer(ReadOnlySpan data);
}
diff --git a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1BufferSizeDispatchDataLoader.cs b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1BufferSizeDispatchDataLoader.cs
index f0ec9dec..98a95f83 100644
--- a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1BufferSizeDispatchDataLoader.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1BufferSizeDispatchDataLoader.cs
@@ -28,9 +28,9 @@ internal unsafe struct D2D1BufferSizeDispatchDataLoader : ID2D1DispatchDataLoade
}
///
- void ID2D1DispatchDataLoader.LoadConstantBuffer(ReadOnlySpan data)
+ void ID2D1DispatchDataLoader.LoadConstantBuffer(ReadOnlySpan data)
{
- this.size = data.Length * sizeof(uint);
+ this.size = data.Length;
}
///
diff --git a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteArrayDispatchDataLoader.cs b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteArrayDispatchDataLoader.cs
index 5486fec6..b68e7407 100644
--- a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteArrayDispatchDataLoader.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteArrayDispatchDataLoader.cs
@@ -29,8 +29,8 @@ internal struct D2D1ByteArrayDispatchDataLoader : ID2D1DispatchDataLoader
}
///
- void ID2D1DispatchDataLoader.LoadConstantBuffer(ReadOnlySpan data)
+ void ID2D1DispatchDataLoader.LoadConstantBuffer(ReadOnlySpan data)
{
- this.data = MemoryMarshal.AsBytes(data).ToArray();
+ this.data = data.ToArray();
}
}
diff --git a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteBufferDispatchDataLoader.cs b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteBufferDispatchDataLoader.cs
index e10f2d0d..e45e3862 100644
--- a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteBufferDispatchDataLoader.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteBufferDispatchDataLoader.cs
@@ -1,6 +1,5 @@
using System;
using System.Runtime.CompilerServices;
-using System.Runtime.InteropServices;
using ComputeSharp.D2D1.__Internals;
using TerraFX.Interop.DirectX;
@@ -66,13 +65,11 @@ internal unsafe struct D2D1ByteBufferDispatchDataLoader : ID2D1DispatchDataLoade
}
///
- void ID2D1DispatchDataLoader.LoadConstantBuffer(ReadOnlySpan data)
+ void ID2D1DispatchDataLoader.LoadConstantBuffer(ReadOnlySpan data)
{
- ReadOnlySpan constantBuffer = MemoryMarshal.Cast(data);
-
- if (constantBuffer.TryCopyTo(new Span(this.buffer, this.length)))
+ if (data.TryCopyTo(new Span(this.buffer, this.length)))
{
- this.writtenBytes = constantBuffer.Length;
+ this.writtenBytes = data.Length;
}
}
}
diff --git a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1DrawInfoDispatchDataLoader.cs b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1DrawInfoDispatchDataLoader.cs
index 65142fcd..d2ac7e01 100644
--- a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1DrawInfoDispatchDataLoader.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1DrawInfoDispatchDataLoader.cs
@@ -29,10 +29,10 @@ internal readonly unsafe struct D2D1DrawInfoDispatchDataLoader : ID2D1DispatchDa
}
///
- void ID2D1DispatchDataLoader.LoadConstantBuffer(ReadOnlySpan data)
+ void ID2D1DispatchDataLoader.LoadConstantBuffer(ReadOnlySpan data)
{
this.d2D1DrawInfo->SetPixelShaderConstantBuffer(
buffer: (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(data)),
- bufferCount: (uint)data.Length * sizeof(uint));
+ bufferCount: (uint)data.Length);
}
}
diff --git a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1EffectDispatchDataLoader.cs b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1EffectDispatchDataLoader.cs
index ce3b0385..b7381119 100644
--- a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1EffectDispatchDataLoader.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1EffectDispatchDataLoader.cs
@@ -30,7 +30,7 @@ internal readonly unsafe struct D2D1EffectDispatchDataLoader : ID2D1DispatchData
}
///
- void ID2D1DispatchDataLoader.LoadConstantBuffer(ReadOnlySpan data)
+ void ID2D1DispatchDataLoader.LoadConstantBuffer(ReadOnlySpan data)
{
if (data.IsEmpty)
{
@@ -41,6 +41,6 @@ internal readonly unsafe struct D2D1EffectDispatchDataLoader : ID2D1DispatchData
index: 0,
type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_BLOB,
data: (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(data)),
- dataSize: (uint)data.Length * sizeof(uint)).Assert();
+ dataSize: (uint)data.Length).Assert();
}
}
diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs
index 40af5a53..8451edff 100644
--- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs
+++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs
@@ -105,7 +105,7 @@ partial class IShaderGenerator
resourceCount = resourceOffsetAsBox.Value;
- // After all the captured fields have been processed, ansure the reported byte size for
+ // After all the captured fields have been processed, ensure the reported byte size for
// the local variables is padded to a multiple of a 32 bit value. This is necessary to
// enable loading all the dispatch data after reinterpreting it to a sequence of values
// of size 32 bits (via SetComputeRoot32BitConstants), without reading out of bounds.