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);