diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs
index d4f85208..06e2c7af 100644
--- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs
+++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs
@@ -11,6 +11,7 @@ using ComputeSharp.Graphics.Helpers;
using Microsoft.Toolkit.Diagnostics;
using TerraFX.Interop;
using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE;
+using static TerraFX.Interop.D3D12_RESOURCE_STATES;
namespace ComputeSharp.Graphics.Buffers.Abstract
{
@@ -32,6 +33,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// This field is dynamically accessed when loading shader dispatch data.
internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle;
+ ///
+ /// The default value for the current resource.
+ ///
+ private readonly D3D12_RESOURCE_STATES D3D12ResourceStates;
+
///
/// Creates a new instance with the specified parameters.
///
@@ -50,7 +56,12 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
Width = width;
Height = height;
- this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, DXGIFormatHelper.GetForType(), (uint)width, (uint)height);
+ this.d3D12Resource = device.D3D12Device->CreateCommittedResource(
+ resourceType,
+ DXGIFormatHelper.GetForType(),
+ (uint)width,
+ (uint)height,
+ out D3D12ResourceStates);
device.AllocateShaderResourceViewDescriptorHandles(out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle);
@@ -230,9 +241,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, (ulong)byteSize);
- using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY))
+ using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COMPUTE))
{
+ copyCommandList.ResourceBarrier(D3D12Resource, D3D12ResourceStates, D3D12_RESOURCE_STATE_COPY_SOURCE);
copyCommandList.CopyTextureRegion(d3D12Resource.Get(), (uint)Unsafe.SizeOf(), D3D12Resource, DXGIFormatHelper.GetForType(), (uint)x, (uint)y, (uint)width, (uint)height);
+ copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12ResourceStates);
copyCommandList.ExecuteAndWaitForCompletion();
}
@@ -367,9 +380,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
MemoryHelper.Copy(source, resource.Pointer, 0);
}
- using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
+ using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COMPUTE);
+ copyCommandList.ResourceBarrier(D3D12Resource, D3D12ResourceStates, D3D12_RESOURCE_STATE_COPY_DEST);
copyCommandList.CopyTextureRegion(D3D12Resource, DXGIFormatHelper.GetForType(), (uint)x, (uint)y, d3D12Resource.Get(), (uint)width, (uint)height, (uint)Unsafe.SizeOf());
+ copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, D3D12ResourceStates);
copyCommandList.ExecuteAndWaitForCompletion();
}
diff --git a/src/ComputeSharp.Graphics/Commands/CommandList.cs b/src/ComputeSharp.Graphics/Commands/CommandList.cs
index f41999e1..18cecdf0 100644
--- a/src/ComputeSharp.Graphics/Commands/CommandList.cs
+++ b/src/ComputeSharp.Graphics/Commands/CommandList.cs
@@ -140,6 +140,22 @@ namespace ComputeSharp.Graphics.Commands
this.d3D12GraphicsCommandList.Get()->CopyTextureRegion(d3d12ResourceDestination, elementSizeInBytes, d3D12ResourceSource, dxgiFormat, x, y, width, height);
}
+ ///
+ /// Creates a resource barrier to transition a resource to a specific state.
+ ///
+ /// The to change state for.
+ /// The starting value for the transition.
+ /// The destnation value for the transition.
+ public readonly void ResourceBarrier(
+ ID3D12Resource* d3D12Resource,
+ D3D12_RESOURCE_STATES d3D12ResourceStatesBefore,
+ D3D12_RESOURCE_STATES d3D12ResourceStatesAfter)
+ {
+ D3D12_RESOURCE_BARRIER d3D12ResourceBarrier = D3D12_RESOURCE_BARRIER.InitTransition(d3D12Resource, d3D12ResourceStatesBefore, d3D12ResourceStatesAfter);
+
+ this.d3D12GraphicsCommandList.Get()->ResourceBarrier(1, &d3D12ResourceBarrier);
+ }
+
///
/// Binds an input value to a specified root parameter.
///
diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs
index feab584f..3c95e4fc 100644
--- a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs
+++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs
@@ -149,29 +149,29 @@ namespace ComputeSharp.Graphics.Extensions
/// The value to use.
/// The width of the texture resource.
/// The height of the texture resource.
+ /// The default value for the resource.
/// An reference for the current texture.
public static ComPtr CreateCommittedResource(
this ref ID3D12Device d3D12Device,
ResourceType resourceType,
DXGI_FORMAT dxgiFormat,
uint width,
- uint height)
+ uint height,
+ out D3D12_RESOURCE_STATES d3D12ResourceStates)
{
- (D3D12_HEAP_TYPE d3D12HeapType,
- D3D12_RESOURCE_FLAGS d3D12ResourceFlags,
- D3D12_RESOURCE_STATES d3D12ResourceStates) = resourceType switch
+ D3D12_RESOURCE_FLAGS d3D12ResourceFlags;
+
+ (d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch
{
- ResourceType.ReadOnly => (D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON),
- ResourceType.ReadWrite => (D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COMMON),
- ResourceType.ReadBack => (D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COPY_DEST),
- ResourceType.Upload => (D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_GENERIC_READ),
- _ => ThrowHelper.ThrowArgumentException<(D3D12_HEAP_TYPE, D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>()
+ ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE),
+ ResourceType.ReadWrite => (D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_UNORDERED_ACCESS),
+ _ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>()
};
using ComPtr d3D12Resource = default;
D3D12_HEAP_PROPERTIES d3D12HeapProperties;
- d3D12HeapProperties.Type = d3D12HeapType;
+ d3D12HeapProperties.Type = D3D12_HEAP_TYPE_DEFAULT;
d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
d3D12HeapProperties.CreationNodeMask = 1;