Bug fixes and optimization to dispatch data buffer

✅ Reduced the cached buffer size from 65KB to 4KB
✅ Fixed out of bounds read when loading dispatch data
このコミットが含まれているのは:
Sergio Pedri committed 2020-12-08 16:02:43 +01:00
1 親 624c1a35d0
コミット 6a5826fdcc
4ファイル変更
+13 -6

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

+2 -2
ファイルの表示
@@ -28,7 +28,7 @@ namespace ComputeSharp
/// <summary>
/// The alignment boundary for elements in a constant buffer.
/// </summary>
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;
/// <summary>
/// Creates a new <see cref="ConstantBuffer{T}"/> 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);
}
/// <inheritdoc/>
+1 -1
ファイルの表示
@@ -38,7 +38,7 @@ namespace ComputeSharp.Shaders
{
ConditionalWeakTable<GraphicsDevice, ConstantBuffer<Int4>> map = variablesBuffers ??= new();
return map.GetValue(device, static gpu => new(gpu, 4096));
return map.GetValue(device, static gpu => new(gpu, 256));
}
}
+2 -3
ファイルの表示
@@ -63,15 +63,14 @@ namespace ComputeSharp.Shaders.Translation.Models
/// <summary>
/// Gets a <see cref="ReadOnlySpan{T}"/> with the padded data representing all the captured variables.
/// </summary>
public ReadOnlySpan<Int4> Variables
public unsafe ReadOnlySpan<Int4> Variables
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
ref byte r0 = ref MemoryMarshal.GetArrayDataReference(this.variablesArray);
ref Int4 r1 = ref Unsafe.As<byte, Int4>(ref r0);
bool mod = (this.variablesByteSize & 15) > 0;
int length = variablesByteSize / 4 + Unsafe.As<bool, byte>(ref mod);
int length = (int)((uint)this.variablesByteSize / (uint)sizeof(Int4));
return MemoryMarshal.CreateReadOnlySpan(ref r1, length);
}
+8
ファイルの表示
@@ -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);
}
}
}