Added validation to Buffer<T> constructor

このコミットが含まれているのは:
Sergio Pedri committed 2020-11-23 13:42:41 +01:00
1 親 d6d5c4bcc9
コミット 998fb919c8
5ファイル変更
+30 -25

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

+12 -7
ファイルの表示
@@ -6,6 +6,7 @@ using ComputeSharp.Core.Interop;
using ComputeSharp.Exceptions;
using ComputeSharp.Graphics.Buffers.Enums;
using ComputeSharp.Graphics.Extensions;
using Microsoft.Toolkit.Diagnostics;
using TerraFX.Interop;
using static TerraFX.Interop.D3D12_SRV_DIMENSION;
using static TerraFX.Interop.D3D12_UAV_DIMENSION;
@@ -31,25 +32,29 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle;
/// <summary>
/// The size in bytes of the current buffer.
/// The size in bytes of the current buffer (this value is never negative).
/// </summary>
protected readonly int SizeInBytes;
protected readonly nint SizeInBytes;
/// <summary>
/// Creates a new <see cref="Buffer{T}"/> instance with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
/// <param name="length">The number of items to store in the current buffer.</param>
/// <param name="sizeInBytes">The size in bytes for the current buffer.</param>
/// <param name="elementSizeInBytes">The size in bytes of each buffer item (including padding, if any).</param>
/// <param name="bufferType">The buffer type for the current buffer.</param>
internal Buffer(GraphicsDevice device, int length, int sizeInBytes, BufferType bufferType)
internal Buffer(GraphicsDevice device, int length, uint elementSizeInBytes, BufferType bufferType)
{
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(bufferType, sizeInBytes);
device.ThrowIfDisposed();
SizeInBytes = sizeInBytes;
Guard.IsGreaterThanOrEqualTo(length, 0, nameof(length));
SizeInBytes = checked((nint)(length * elementSizeInBytes));
GraphicsDevice = device;
Length = length;
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(bufferType, (ulong)SizeInBytes);
GraphicsDevice.AllocateShaderResourceViewDescriptorHandles(out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle);
switch (bufferType)
@@ -192,7 +197,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// <param name="d3D12CpuDescriptorHandle">The <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> instance for the current resource.</param>
private void CreateConstantBufferView(D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle)
{
uint constantBufferSize = (uint)((SizeInBytes + 255) & ~255);
uint constantBufferSize = checked((uint)((SizeInBytes + 255) & ~255));
D3D12_CONSTANT_BUFFER_VIEW_DESC d3D12ConstantBufferViewDescription;
d3D12ConstantBufferViewDescription.BufferLocation = D3D12Resource->GetGPUVirtualAddress();
+12 -12
ファイルの表示
@@ -24,22 +24,22 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// <param name="length">The number of items to store in the current buffer</param>
/// <param name="bufferType">The buffer type for the current buffer</param>
internal StructuredBuffer(GraphicsDevice device, int length, BufferType bufferType)
: base(device, length, length * Unsafe.SizeOf<T>(), bufferType)
: base(device, length, (uint)Unsafe.SizeOf<T>(), bufferType)
{
}
/// <inheritdoc/>
public override unsafe void GetData(Span<T> span, int offset, int count)
{
int
byteOffset = offset * ElementSizeInBytes,
byteSize = count * ElementSizeInBytes;
nint
byteOffset = (nint)offset * ElementSizeInBytes,
byteSize = (nint)count * ElementSizeInBytes;
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(BufferType.ReadBack, byteSize);
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(BufferType.ReadBack, (ulong)byteSize);
using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY))
{
copyCommandList.CopyBufferRegion(D3D12Resource, byteOffset, d3D12Resource.Get(), 0, byteSize);
copyCommandList.CopyBufferRegion(D3D12Resource, (ulong)byteOffset, d3D12Resource.Get(), 0, (ulong)byteSize);
copyCommandList.ExecuteAndWaitForCompletion();
}
@@ -51,11 +51,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// <inheritdoc/>
public override unsafe void SetData(ReadOnlySpan<T> span, int offset, int count)
{
int
byteOffset = offset * ElementSizeInBytes,
byteSize = count * ElementSizeInBytes;
nint
byteOffset = (nint)offset * ElementSizeInBytes,
byteSize = (nint)count * ElementSizeInBytes;
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(BufferType.Upload, byteSize);
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(BufferType.Upload, (ulong)byteSize);
using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
{
@@ -64,7 +64,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
copyCommandList.CopyBufferRegion(d3D12Resource.Get(), 0, D3D12Resource, byteOffset, byteSize);
copyCommandList.CopyBufferRegion(d3D12Resource.Get(), 0, D3D12Resource, (ulong)byteOffset, (ulong)byteSize);
copyCommandList.ExecuteAndWaitForCompletion();
}
@@ -76,7 +76,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
// Directly copy the input buffer
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
copyCommandList.CopyBufferRegion(buffer.D3D12Resource, 0, D3D12Resource, 0, SizeInBytes);
copyCommandList.CopyBufferRegion(buffer.D3D12Resource, 0, D3D12Resource, 0, (ulong)SizeInBytes);
copyCommandList.ExecuteAndWaitForCompletion();
}
else SetDataWithCpuBuffer(buffer);
+2 -2
ファイルの表示
@@ -31,7 +31,7 @@ namespace ComputeSharp
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance</param>
/// <param name="length">The number of items to store in the current buffer</param>
internal ConstantBuffer(GraphicsDevice device, int length)
: base(device, length, length * GetPaddedSize(), BufferType.Constant)
: base(device, length, (uint)GetPaddedSize(), BufferType.Constant)
{
}
@@ -106,7 +106,7 @@ namespace ComputeSharp
// Directly copy the input buffer, if possible
using CommandList copyCommandList = new CommandList(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
copyCommandList.CopyBufferRegion(buffer.D3D12Resource, 0, D3D12Resource, 0, SizeInBytes);
copyCommandList.CopyBufferRegion(buffer.D3D12Resource, 0, D3D12Resource, 0, (ulong)SizeInBytes);
copyCommandList.ExecuteAndWaitForCompletion();
}
else SetDataWithCpuBuffer(buffer);
+2 -2
ファイルの表示
@@ -86,9 +86,9 @@ namespace ComputeSharp.Graphics.Commands
/// <param name="d3d12ResourceDestination">The destination <see cref="ID3D12Resource"/> to write to.</param>
/// <param name="destinationOffset">The starting offset to write the destination resource from.</param>
/// <param name="numBytes">The total number of bytes to copy from one resource to another.</param>
public readonly void CopyBufferRegion(ID3D12Resource* d3D12ResourceSource, int sourceOffset, ID3D12Resource* d3d12ResourceDestination, int destinationOffset, int numBytes)
public readonly void CopyBufferRegion(ID3D12Resource* d3D12ResourceSource, ulong sourceOffset, ID3D12Resource* d3d12ResourceDestination, ulong destinationOffset, ulong numBytes)
{
this.d3D12GraphicsCommandList.Get()->CopyBufferRegion(d3d12ResourceDestination, (uint)destinationOffset, d3D12ResourceSource, (uint)sourceOffset, (uint)numBytes);
this.d3D12GraphicsCommandList.Get()->CopyBufferRegion(d3d12ResourceDestination, destinationOffset, d3D12ResourceSource, sourceOffset, numBytes);
}
/// <summary>
+2 -2
ファイルの表示
@@ -106,7 +106,7 @@ namespace ComputeSharp.Graphics.Extensions
public static ComPtr<ID3D12Resource> CreateCommittedResource(
this ref ID3D12Device d3D12Device,
BufferType bufferType,
int sizeInBytes)
ulong sizeInBytes)
{
(D3D12_HEAP_TYPE d3D12HeapType,
D3D12_RESOURCE_FLAGS d3D12ResourceFlags,
@@ -128,7 +128,7 @@ namespace ComputeSharp.Graphics.Extensions
d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
d3D12HeapProperties.CreationNodeMask = 1;
d3D12HeapProperties.VisibleNodeMask = 1;
D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Buffer((uint)sizeInBytes, d3D12ResourceFlags);
D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Buffer(sizeInBytes, d3D12ResourceFlags);
Guid d3D12ResourceGuid = FX.IID_ID3D12Resource;
d3D12Device.CreateCommittedResource(