Fixed resource state transition for textures

このコミットが含まれているのは:
Sergio Pedri committed 2020-12-04 00:01:27 +01:00
1 親 e631066a10
コミット 15191efb05
3ファイル変更
+44 -13

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

+18 -3
ファイルの表示
@@ -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
/// <remarks>This field is dynamically accessed when loading shader dispatch data.</remarks>
internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle;
/// <summary>
/// The default <see cref="D3D12_RESOURCE_STATES"/> value for the current resource.
/// </summary>
private readonly D3D12_RESOURCE_STATES D3D12ResourceStates;
/// <summary>
/// Creates a new <see cref="Texture2D{T}"/> instance with the specified parameters.
/// </summary>
@@ -50,7 +56,12 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
Width = width;
Height = height;
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, DXGIFormatHelper.GetForType<T>(), (uint)width, (uint)height);
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(
resourceType,
DXGIFormatHelper.GetForType<T>(),
(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<ID3D12Resource> 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<T>(), D3D12Resource, DXGIFormatHelper.GetForType<T>(), (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<T>(), (uint)x, (uint)y, d3D12Resource.Get(), (uint)width, (uint)height, (uint)Unsafe.SizeOf<T>());
copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, D3D12ResourceStates);
copyCommandList.ExecuteAndWaitForCompletion();
}
+16
ファイルの表示
@@ -140,6 +140,22 @@ namespace ComputeSharp.Graphics.Commands
this.d3D12GraphicsCommandList.Get()->CopyTextureRegion(d3d12ResourceDestination, elementSizeInBytes, d3D12ResourceSource, dxgiFormat, x, y, width, height);
}
/// <summary>
/// Creates a resource barrier to transition a resource to a specific state.
/// </summary>
/// <param name="d3D12Resource">The <see cref="ID3D12Resource"/> to change state for.</param>
/// <param name="d3D12ResourceStatesBefore">The starting <see cref="D3D12_RESOURCE_STATES"/> value for the transition.</param>
/// <param name="d3D12ResourceStatesAfter">The destnation <see cref="D3D12_RESOURCE_STATES"/> value for the transition.</param>
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);
}
/// <summary>
/// Binds an input <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> value to a specified root parameter.
/// </summary>
+10 -10
ファイルの表示
@@ -149,29 +149,29 @@ namespace ComputeSharp.Graphics.Extensions
/// <param name="dxgiFormat">The <see cref="DXGI_FORMAT"/> value to use.</param>
/// <param name="width">The width of the texture resource.</param>
/// <param name="height">The height of the texture resource.</param>
/// <param name="d3D12ResourceStates">The default <see cref="D3D12_RESOURCE_STATES"/> value for the resource.</param>
/// <returns>An <see cref="ID3D12Resource"/> reference for the current texture.</returns>
public static ComPtr<ID3D12Resource> 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<ID3D12Resource> 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;