diff --git a/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs b/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs index 195862ad..bc80a50a 100644 --- a/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs @@ -40,6 +40,15 @@ public static class GraphicsResourceHelper /// The target instance in use. /// The the underlying object. ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device); + + /// + /// Validates the given resource for usage with a specified device, and retrieves the underlying object, along with the transition states. + /// + /// The target instance in use. + /// The target state to transition the resource to. + /// The the underlying object. + /// The resource states for , before and after the transition. + (D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource); } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index 2e5f35b1..4b28afdc 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -5,6 +5,7 @@ using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Commands.Interop; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; +using ComputeSharp.Graphics.Resources.Helpers; using ComputeSharp.Graphics.Resources.Interop; using ComputeSharp.Interop; using Microsoft.Toolkit.Diagnostics; @@ -44,11 +45,6 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra /// private readonly ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles; - /// - /// The default value for the current resource. - /// - private readonly D3D12_RESOURCE_STATES d3D12ResourceState; - /// /// The value to use for copy operations. /// @@ -59,6 +55,11 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra /// private readonly D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint; + /// + /// The current value for the current resource. + /// + private D3D12_RESOURCE_STATES d3D12ResourceState; + /// /// Creates a new instance with the specified parameters. /// @@ -572,6 +573,22 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra return D3D12Resource; } + /// + internal (D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource) + { + ThrowIfDisposed(); + ThrowIfDeviceMismatch(device); + + D3D12_RESOURCE_STATES d3D12ResourceStatesBefore = this.d3D12ResourceState; + D3D12_RESOURCE_STATES d3D12ResourceStatesAfter = ResourceStateHelper.GetD3D12ResourceStates(resourceState); + + this.d3D12ResourceState = d3D12ResourceStatesAfter; + + d3D12Resource = D3D12Resource; + + return (d3D12ResourceStatesBefore, d3D12ResourceStatesAfter); + } + /// D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) { @@ -592,4 +609,10 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra { return ValidateAndGetID3D12Resource(device); } + + /// + (D3D12_RESOURCE_STATES, D3D12_RESOURCE_STATES) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource) + { + return ValidateAndGetID3D12ResourceAndTransitionStates(device, resourceState, out d3D12Resource); + } } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index ddfda4af..32c2d09d 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -5,6 +5,7 @@ using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Commands.Interop; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; +using ComputeSharp.Graphics.Resources.Helpers; using ComputeSharp.Graphics.Resources.Interop; using ComputeSharp.Interop; using Microsoft.Toolkit.Diagnostics; @@ -44,11 +45,6 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra /// private readonly ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles; - /// - /// The default value for the current resource. - /// - private readonly D3D12_RESOURCE_STATES d3D12ResourceState; - /// /// The value to use for copy operations. /// @@ -59,6 +55,11 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra /// private readonly D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint; + /// + /// The current value for the current resource. + /// + private D3D12_RESOURCE_STATES d3D12ResourceState; + /// /// Creates a new instance with the specified parameters. /// @@ -625,6 +626,22 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra return D3D12Resource; } + /// + internal (D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource) + { + ThrowIfDisposed(); + ThrowIfDeviceMismatch(device); + + D3D12_RESOURCE_STATES d3D12ResourceStatesBefore = this.d3D12ResourceState; + D3D12_RESOURCE_STATES d3D12ResourceStatesAfter = ResourceStateHelper.GetD3D12ResourceStates(resourceState); + + this.d3D12ResourceState = d3D12ResourceStatesAfter; + + d3D12Resource = D3D12Resource; + + return (d3D12ResourceStatesBefore, d3D12ResourceStatesAfter); + } + /// D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) { @@ -645,4 +662,10 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra { return ValidateAndGetID3D12Resource(device); } + + /// + (D3D12_RESOURCE_STATES, D3D12_RESOURCE_STATES) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource) + { + return ValidateAndGetID3D12ResourceAndTransitionStates(device, resourceState, out d3D12Resource); + } } diff --git a/src/ComputeSharp/Graphics/Resources/Enums/ResourceState.cs b/src/ComputeSharp/Graphics/Resources/Enums/ResourceState.cs new file mode 100644 index 00000000..684ed25b --- /dev/null +++ b/src/ComputeSharp/Graphics/Resources/Enums/ResourceState.cs @@ -0,0 +1,17 @@ +namespace ComputeSharp; + +/// +/// An that indicates the state of a resource after a given transition. +/// +public enum ResourceState +{ + /// + /// A readonly resource, that can only be read from by the GPU and supports texture sampling. + /// + ReadOnly, + + /// + /// A read write resource, with both read and write access for the GPU, but without support for texture sampling. + /// + ReadWrite +} diff --git a/src/ComputeSharp/Graphics/Resources/Helpers/ResourceStateHelper.cs b/src/ComputeSharp/Graphics/Resources/Helpers/ResourceStateHelper.cs new file mode 100644 index 00000000..bed53dbb --- /dev/null +++ b/src/ComputeSharp/Graphics/Resources/Helpers/ResourceStateHelper.cs @@ -0,0 +1,33 @@ +using System.Runtime.CompilerServices; +using Microsoft.Toolkit.Diagnostics; +using TerraFX.Interop.DirectX; +using static TerraFX.Interop.DirectX.D3D12_RESOURCE_STATES; + +namespace ComputeSharp.Graphics.Resources.Helpers; + +/// +/// A helper type with utility methods for . +/// +internal static class ResourceStateHelper +{ + /// + /// Gets the corresponding value for the input resource state. + /// + /// The value corresponding to the input resource state. + /// Thrown when the input value is not valid. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static D3D12_RESOURCE_STATES GetD3D12ResourceStates(ResourceState resourceState) + { + if (resourceState == ResourceState.ReadOnly) + { + return D3D12_RESOURCE_STATE_COMMON; + } + + if (resourceState == ResourceState.ReadWrite) + { + return D3D12_RESOURCE_STATE_UNORDERED_ACCESS; + } + + return ThrowHelper.ThrowArgumentException("The target resource state is not valid."); + } +}