Fix constant buffer allocation with invalid alignment
このコミットが含まれているのは:
1 親
e3c79f4170
コミット
adde64fdeb
3ファイル変更
+44
-15
検索条件に一致するファイルはありません
@@ -21,6 +21,19 @@ internal static class AlignmentHelper
|
||||
return (size + alignment - 1) & ~(alignment - 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pads the input size so that its pitch is aligned to a multiple of a specified length.
|
||||
/// </summary>
|
||||
/// <param name="size">The input size to pad, if needed.</param>
|
||||
/// <param name="alignment">The target alignment.</param>
|
||||
/// <returns>The padded value relative to the given inputs.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static nint Pad(nint size, nint alignment)
|
||||
{
|
||||
return (size + alignment - 1) & ~(alignment - 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Aligns a given offset to a specified boundary, if a given size increment would have crossed it.
|
||||
/// </summary>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Buffers;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.Core.Helpers;
|
||||
using ComputeSharp.Exceptions;
|
||||
using ComputeSharp.Graphics.Extensions;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
@@ -72,11 +73,14 @@ public unsafe abstract class Buffer<T> : NativeObject
|
||||
UnsupportedDoubleOperationsException.Throw<T>();
|
||||
}
|
||||
|
||||
SizeInBytes = checked((nint)(length * elementSizeInBytes));
|
||||
nint usableSizeInBytes = checked((nint)(length * elementSizeInBytes));
|
||||
nint effectiveSizeInBytes = resourceType == ResourceType.Constant ? AlignmentHelper.Pad(usableSizeInBytes, Windows.D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT) : usableSizeInBytes;
|
||||
|
||||
SizeInBytes = usableSizeInBytes;
|
||||
GraphicsDevice = device;
|
||||
Length = length;
|
||||
|
||||
this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, (ulong)SizeInBytes);
|
||||
this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, (ulong)effectiveSizeInBytes);
|
||||
this.d3D12Resource = new ComPtr<ID3D12Resource>(this.allocation.Get()->GetResource());
|
||||
|
||||
device.RegisterAllocatedResource();
|
||||
@@ -85,7 +89,7 @@ public unsafe abstract class Buffer<T> : NativeObject
|
||||
switch (resourceType)
|
||||
{
|
||||
case ResourceType.Constant:
|
||||
device.D3D12Device->CreateConstantBufferView(this.d3D12Resource.Get(), SizeInBytes, D3D12CpuDescriptorHandle);
|
||||
device.D3D12Device->CreateConstantBufferView(this.d3D12Resource.Get(), effectiveSizeInBytes, D3D12CpuDescriptorHandle);
|
||||
break;
|
||||
case ResourceType.ReadOnly:
|
||||
device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, D3D12CpuDescriptorHandle);
|
||||
|
||||
@@ -18,12 +18,16 @@ public partial class BufferTests
|
||||
[Resource(typeof(ReadWriteBuffer<>))]
|
||||
[Data(AllocationMode.Default)]
|
||||
[Data(AllocationMode.Clear)]
|
||||
public void Allocate_Uninitialized_Ok(Device device, Type bufferType, AllocationMode allocationMode)
|
||||
[AdditionalData(128)]
|
||||
[AdditionalData(768)]
|
||||
[AdditionalData(1024)]
|
||||
[AdditionalData(443)]
|
||||
public void Allocate_Uninitialized_Ok(Device device, Type bufferType, AllocationMode allocationMode, int size)
|
||||
{
|
||||
using Buffer<float> buffer = device.Get().AllocateBuffer<float>(bufferType, 128, allocationMode);
|
||||
using Buffer<float> buffer = device.Get().AllocateBuffer<float>(bufferType, size, allocationMode);
|
||||
|
||||
Assert.IsNotNull(buffer);
|
||||
Assert.AreEqual(buffer.Length, 128);
|
||||
Assert.AreEqual(buffer.Length, size);
|
||||
Assert.AreSame(buffer.GraphicsDevice, device.Get());
|
||||
|
||||
if (allocationMode == AllocationMode.Clear)
|
||||
@@ -53,14 +57,18 @@ public partial class BufferTests
|
||||
[Resource(typeof(ConstantBuffer<>))]
|
||||
[Resource(typeof(ReadOnlyBuffer<>))]
|
||||
[Resource(typeof(ReadWriteBuffer<>))]
|
||||
public void Allocate_FromArray(Device device, Type bufferType)
|
||||
[Data(128)]
|
||||
[Data(768)]
|
||||
[Data(1024)]
|
||||
[Data(443)]
|
||||
public void Allocate_FromArray(Device device, Type bufferType, int size)
|
||||
{
|
||||
float[] data = Enumerable.Range(0, 128).Select(static i => (float)i).ToArray();
|
||||
float[] data = Enumerable.Range(0, size).Select(static i => (float)i).ToArray();
|
||||
|
||||
using Buffer<float> buffer = device.Get().AllocateBuffer(bufferType, data);
|
||||
|
||||
Assert.IsNotNull(buffer);
|
||||
Assert.AreEqual(buffer.Length, 128);
|
||||
Assert.AreEqual(buffer.Length, size);
|
||||
Assert.AreSame(buffer.GraphicsDevice, device.Get());
|
||||
|
||||
float[] result = buffer.ToArray();
|
||||
@@ -75,18 +83,22 @@ public partial class BufferTests
|
||||
[Resource(typeof(ConstantBuffer<>))]
|
||||
[Resource(typeof(ReadOnlyBuffer<>))]
|
||||
[Resource(typeof(ReadWriteBuffer<>))]
|
||||
[Data(typeof(ConstantBuffer<>))]
|
||||
[Data(typeof(ReadOnlyBuffer<>))]
|
||||
[Data(typeof(ReadWriteBuffer<>))]
|
||||
public void Allocate_FromBuffer(Device device, Type sourceType, Type destinationType)
|
||||
[AdditionalResource(typeof(ConstantBuffer<>))]
|
||||
[AdditionalResource(typeof(ReadOnlyBuffer<>))]
|
||||
[AdditionalResource(typeof(ReadWriteBuffer<>))]
|
||||
[Data(128)]
|
||||
[Data(768)]
|
||||
[Data(1024)]
|
||||
[Data(443)]
|
||||
public void Allocate_FromBuffer(Device device, Type sourceType, Type destinationType, int size)
|
||||
{
|
||||
float[] data = Enumerable.Range(0, 128).Select(static i => (float)i).ToArray();
|
||||
float[] data = Enumerable.Range(0, size).Select(static i => (float)i).ToArray();
|
||||
|
||||
using Buffer<float> source = device.Get().AllocateBuffer(sourceType, data);
|
||||
using Buffer<float> destination = device.Get().AllocateBuffer(destinationType, source);
|
||||
|
||||
Assert.IsNotNull(destination);
|
||||
Assert.AreEqual(destination.Length, 128);
|
||||
Assert.AreEqual(destination.Length, size);
|
||||
Assert.AreSame(destination.GraphicsDevice, device.Get());
|
||||
|
||||
float[] result = destination.ToArray();
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする