diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs index bf9be010..c502dbf9 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs @@ -64,7 +64,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract GraphicsDevice = device; Length = length; - this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, (ulong)SizeInBytes); + this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, (ulong)SizeInBytes, device.IsCacheCoherentUMA); device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs index 37686b47..3e73a1c7 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs @@ -37,28 +37,45 @@ namespace ComputeSharp.Graphics.Buffers.Abstract Guard.IsInRange(offset, 0, Length, nameof(offset)); Guard.IsLessThanOrEqualTo(offset + size, Length, nameof(size)); - nint - byteOffset = (nint)offset * sizeof(T), - byteSize = size * sizeof(T); - - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, (ulong)byteSize); - - using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY)) + if (GraphicsDevice.IsCacheCoherentUMA) { - copyCommandList.CopyBufferRegion(d3D12Resource.Get(), 0, D3D12Resource, (ulong)byteOffset,(ulong)byteSize); - copyCommandList.ExecuteAndWaitForCompletion(); + using ID3D12ResourceMap resource = D3D12Resource->Map(); + + fixed (void* destinationPointer = &destination) + { + MemoryHelper.Copy( + resource.Pointer, + (uint)offset, + (uint)size, + (uint)sizeof(T), + destinationPointer); + } } - - using ID3D12ResourceMap resource = d3D12Resource.Get()->Map(); - - fixed (void* destinationPointer = &destination) + else { - MemoryHelper.Copy( - resource.Pointer, - 0u, - (uint)size, - (uint)sizeof(T), - destinationPointer); + nint + byteOffset = (nint)offset * sizeof(T), + byteSize = size * sizeof(T); + + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, (ulong)byteSize, false); + + using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY)) + { + copyCommandList.CopyBufferRegion(d3D12Resource.Get(), 0, D3D12Resource, (ulong)byteOffset, (ulong)byteSize); + copyCommandList.ExecuteAndWaitForCompletion(); + } + + using ID3D12ResourceMap resource = d3D12Resource.Get()->Map(); + + fixed (void* destinationPointer = &destination) + { + MemoryHelper.Copy( + resource.Pointer, + 0u, + (uint)size, + (uint)sizeof(T), + destinationPointer); + } } } @@ -72,27 +89,44 @@ namespace ComputeSharp.Graphics.Buffers.Abstract Guard.IsInRange(offset, 0, Length, nameof(offset)); Guard.IsLessThanOrEqualTo(offset + size, Length, nameof(size)); - nint - byteOffset = (nint)offset * sizeof(T), - byteSize = size * sizeof(T); - - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, (ulong)byteSize); - - using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) - fixed (void* sourcePointer = &source) + if (GraphicsDevice.IsCacheCoherentUMA) { - MemoryHelper.Copy( - sourcePointer, - 0u, - (uint)size, - (uint)sizeof(T), - resource.Pointer); + using ID3D12ResourceMap resource = D3D12Resource->Map(); + + fixed (void* sourcePointer = &source) + { + MemoryHelper.Copy( + sourcePointer, + (uint)offset, + (uint)size, + (uint)sizeof(T), + resource.Pointer); + } } + else + { + nint + byteOffset = (nint)offset * sizeof(T), + byteSize = size * sizeof(T); - using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, (ulong)byteSize, false); - copyCommandList.CopyBufferRegion(D3D12Resource, (ulong)byteOffset, d3D12Resource.Get(), 0,(ulong)byteSize); - copyCommandList.ExecuteAndWaitForCompletion(); + using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) + fixed (void* sourcePointer = &source) + { + MemoryHelper.Copy( + sourcePointer, + 0u, + (uint)size, + (uint)sizeof(T), + resource.Pointer); + } + + using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); + + copyCommandList.CopyBufferRegion(D3D12Resource, (ulong)byteOffset, d3D12Resource.Get(), 0, (ulong)byteSize); + copyCommandList.ExecuteAndWaitForCompletion(); + } } /// diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs index 963c3b5f..d8849e0f 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs @@ -78,6 +78,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract DXGIFormatHelper.GetForType(), (uint)width, (uint)height, + device.IsCacheCoherentUMA, out this.d3D12ResourceState); this.d3D12CommandListType = this.d3D12ResourceState == D3D12_RESOURCE_STATE_COMMON @@ -148,7 +149,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, totalSizeInBytes); + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType)) { @@ -220,7 +221,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, totalSizeInBytes); + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) fixed (void* sourcePointer = &source) diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs index 6539319a..abe74bc4 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs @@ -82,6 +82,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract (uint)width, (uint)height, (ushort)depth, + device.IsCacheCoherentUMA, out this.d3D12ResourceState); this.d3D12CommandListType = this.d3D12ResourceState == D3D12_RESOURCE_STATE_COMMON @@ -162,7 +163,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, totalSizeInBytes); + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType)) { @@ -241,7 +242,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, totalSizeInBytes); + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) fixed (void* sourcePointer = &source) diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs index 8a6f278c..239e2369 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs @@ -102,11 +102,13 @@ namespace ComputeSharp.Graphics.Extensions /// The instance in use. /// The resource type currently in use. /// The size in bytes of the current buffer. + /// Indicates whether or not the current device has a cache coherent UMA architecture. /// An reference for the current buffer. public static ComPtr CreateCommittedResource( this ref ID3D12Device d3D12Device, ResourceType resourceType, - ulong sizeInBytes) + ulong sizeInBytes, + bool isCacheCoherentUMA) { (D3D12_HEAP_TYPE d3D12HeapType, D3D12_RESOURCE_FLAGS d3D12ResourceFlags, @@ -123,11 +125,22 @@ namespace ComputeSharp.Graphics.Extensions using ComPtr d3D12Resource = default; D3D12_HEAP_PROPERTIES d3D12HeapProperties; - d3D12HeapProperties.Type = d3D12HeapType; - d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; - d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; d3D12HeapProperties.CreationNodeMask = 1; d3D12HeapProperties.VisibleNodeMask = 1; + + if (isCacheCoherentUMA) + { + d3D12HeapProperties.Type = D3D12_HEAP_TYPE_CUSTOM; + d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_WRITE_BACK; + d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_L0; + } + else + { + d3D12HeapProperties.Type = d3D12HeapType; + d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; + d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; + } + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Buffer(sizeInBytes, d3D12ResourceFlags); d3D12Device.CreateCommittedResource( @@ -150,6 +163,7 @@ namespace ComputeSharp.Graphics.Extensions /// The value to use. /// The width of the texture resource. /// The height of the texture resource. + /// Indicates whether or not the current device has a cache coherent UMA architecture. /// The default value for the resource. /// An reference for the current texture. public static ComPtr CreateCommittedResource( @@ -158,6 +172,7 @@ namespace ComputeSharp.Graphics.Extensions DXGI_FORMAT dxgiFormat, uint width, uint height, + bool isCacheCoherentUMA, out D3D12_RESOURCE_STATES d3D12ResourceStates) { D3D12_RESOURCE_FLAGS d3D12ResourceFlags; @@ -172,11 +187,22 @@ namespace ComputeSharp.Graphics.Extensions using ComPtr d3D12Resource = default; D3D12_HEAP_PROPERTIES d3D12HeapProperties; - d3D12HeapProperties.Type = D3D12_HEAP_TYPE_DEFAULT; - d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; - d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; d3D12HeapProperties.CreationNodeMask = 1; d3D12HeapProperties.VisibleNodeMask = 1; + + if (isCacheCoherentUMA) + { + d3D12HeapProperties.Type = D3D12_HEAP_TYPE_CUSTOM; + d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_WRITE_BACK; + d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_L0; + } + else + { + d3D12HeapProperties.Type = D3D12_HEAP_TYPE_DEFAULT; + d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; + d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; + } + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex2D(dxgiFormat, width, height, flags: d3D12ResourceFlags); d3D12Device.CreateCommittedResource( @@ -200,6 +226,7 @@ namespace ComputeSharp.Graphics.Extensions /// The width of the texture resource. /// The height of the texture resource. /// The depth of the texture resource. + /// Indicates whether or not the current device has a cache coherent UMA architecture. /// The default value for the resource. /// An reference for the current texture. public static ComPtr CreateCommittedResource( @@ -209,6 +236,7 @@ namespace ComputeSharp.Graphics.Extensions uint width, uint height, ushort depth, + bool isCacheCoherentUMA, out D3D12_RESOURCE_STATES d3D12ResourceStates) { D3D12_RESOURCE_FLAGS d3D12ResourceFlags; @@ -223,11 +251,22 @@ namespace ComputeSharp.Graphics.Extensions using ComPtr d3D12Resource = default; D3D12_HEAP_PROPERTIES d3D12HeapProperties; - d3D12HeapProperties.Type = D3D12_HEAP_TYPE_DEFAULT; - d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; - d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; d3D12HeapProperties.CreationNodeMask = 1; d3D12HeapProperties.VisibleNodeMask = 1; + + if (isCacheCoherentUMA) + { + d3D12HeapProperties.Type = D3D12_HEAP_TYPE_CUSTOM; + d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_WRITE_BACK; + d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_L0; + } + else + { + d3D12HeapProperties.Type = D3D12_HEAP_TYPE_DEFAULT; + d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; + d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; + } + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex3D(dxgiFormat, width, height, depth, flags: d3D12ResourceFlags); d3D12Device.CreateCommittedResource(