diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs index e3774a34..363c2a9a 100644 --- a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs @@ -28,7 +28,7 @@ namespace ComputeSharp /// /// The alignment boundary for elements in a constant buffer. /// - private const int ElementSizeAlignment = FX.D3D12_COMMONSHADER_CONSTANT_BUFFER_PARTIAL_UPDATE_EXTENTS_BYTE_ALIGNMENT; + private const int ElementAlignment = FX.D3D12_COMMONSHADER_CONSTANT_BUFFER_PARTIAL_UPDATE_EXTENTS_BYTE_ALIGNMENT; /// /// Creates a new instance with the specified parameters. @@ -54,7 +54,7 @@ namespace ComputeSharp [MethodImpl(MethodImplOptions.AggressiveInlining)] private static unsafe int GetPaddedSize() { - return AlignmentHelper.Pad(sizeof(T), ElementSizeAlignment); + return AlignmentHelper.Pad(sizeof(T), ElementAlignment); } /// diff --git a/src/ComputeSharp.Shaders/ShaderRunner{T}.cs b/src/ComputeSharp.Shaders/ShaderRunner{T}.cs index 5bec10c2..3ceb999b 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner{T}.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner{T}.cs @@ -38,7 +38,7 @@ namespace ComputeSharp.Shaders { ConditionalWeakTable> map = variablesBuffers ??= new(); - return map.GetValue(device, static gpu => new(gpu, 4096)); + return map.GetValue(device, static gpu => new(gpu, 256)); } } diff --git a/src/ComputeSharp.Shaders/Translation/Models/DispatchData.cs b/src/ComputeSharp.Shaders/Translation/Models/DispatchData.cs index 7b1d16c5..4f1eb61f 100644 --- a/src/ComputeSharp.Shaders/Translation/Models/DispatchData.cs +++ b/src/ComputeSharp.Shaders/Translation/Models/DispatchData.cs @@ -63,15 +63,14 @@ namespace ComputeSharp.Shaders.Translation.Models /// /// Gets a with the padded data representing all the captured variables. /// - public ReadOnlySpan Variables + public unsafe ReadOnlySpan Variables { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { ref byte r0 = ref MemoryMarshal.GetArrayDataReference(this.variablesArray); ref Int4 r1 = ref Unsafe.As(ref r0); - bool mod = (this.variablesByteSize & 15) > 0; - int length = variablesByteSize / 4 + Unsafe.As(ref mod); + int length = (int)((uint)this.variablesByteSize / (uint)sizeof(Int4)); return MemoryMarshal.CreateReadOnlySpan(ref r1, length); } diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs index 9012fb0f..40ff7baa 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs @@ -157,6 +157,14 @@ namespace ComputeSharp.Shaders.Translation il.Emit(OpCodes.Ret); }); + + // 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. + this.totalVariablesByteSize = AlignmentHelper.Pad( + this.totalVariablesByteSize, + FX.D3D12_COMMONSHADER_CONSTANT_BUFFER_PARTIAL_UPDATE_EXTENTS_BYTE_ALIGNMENT); } } }