diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs index 971a0114..dc48dba0 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs @@ -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; /// - /// The size in bytes of the current buffer. + /// The size in bytes of the current buffer (this value is never negative). /// - protected readonly int SizeInBytes; + protected readonly nint SizeInBytes; /// /// Creates a new instance with the specified parameters. /// /// The associated with the current instance. /// The number of items to store in the current buffer. - /// The size in bytes for the current buffer. + /// The size in bytes of each buffer item (including padding, if any). /// The buffer type for the current buffer. - 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 /// The instance for the current resource. 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(); diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs index 631da7e9..a09e4da2 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs @@ -24,22 +24,22 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// The number of items to store in the current buffer /// The buffer type for the current buffer internal StructuredBuffer(GraphicsDevice device, int length, BufferType bufferType) - : base(device, length, length * Unsafe.SizeOf(), bufferType) + : base(device, length, (uint)Unsafe.SizeOf(), bufferType) { } /// public override unsafe void GetData(Span span, int offset, int count) { - int - byteOffset = offset * ElementSizeInBytes, - byteSize = count * ElementSizeInBytes; + nint + byteOffset = (nint)offset * ElementSizeInBytes, + byteSize = (nint)count * ElementSizeInBytes; - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(BufferType.ReadBack, byteSize); + using ComPtr 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 /// public override unsafe void SetData(ReadOnlySpan span, int offset, int count) { - int - byteOffset = offset * ElementSizeInBytes, - byteSize = count * ElementSizeInBytes; + nint + byteOffset = (nint)offset * ElementSizeInBytes, + byteSize = (nint)count * ElementSizeInBytes; - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(BufferType.Upload, byteSize); + using ComPtr 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); diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs index c4aa162e..5a5de630 100644 --- a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs @@ -31,7 +31,7 @@ namespace ComputeSharp /// The associated with the current instance /// The number of items to store in the current buffer 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); diff --git a/src/ComputeSharp.Graphics/Commands/CommandList.cs b/src/ComputeSharp.Graphics/Commands/CommandList.cs index 4c621a4e..cebe74c8 100644 --- a/src/ComputeSharp.Graphics/Commands/CommandList.cs +++ b/src/ComputeSharp.Graphics/Commands/CommandList.cs @@ -86,9 +86,9 @@ namespace ComputeSharp.Graphics.Commands /// The destination to write to. /// The starting offset to write the destination resource from. /// The total number of bytes to copy from one resource to another. - 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); } /// diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs index 101d8f3b..a106aeca 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs @@ -106,7 +106,7 @@ namespace ComputeSharp.Graphics.Extensions public static ComPtr 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(