From c81eecfd64bceccaa96e10879640fa81dbaf3e85 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 22 Jan 2022 15:55:47 +0100 Subject: [PATCH] Add new Transition and AsReadOnly extensions for writeable textures --- .../ReadWriteTexture2DExtensions.cs | 133 ++++++++++++++++++ .../ReadWriteTexture3DExtensions.cs | 133 ++++++++++++++++++ .../ReadWriteTexture2D{T,TPixel}.ReadOnly.cs | 21 +-- .../ReadWriteTexture2D{T}.ReadOnly.cs | 19 +-- .../ReadWriteTexture3D{T,TPixel}.ReadOnly.cs | 19 +-- .../ReadWriteTexture3D{T}.ReadOnly.cs | 19 +-- .../Extensions/ComputeContextExtensions.cs | 104 ++++++++++++++ 7 files changed, 375 insertions(+), 73 deletions(-) create mode 100644 src/ComputeSharp/Graphics/Extensions/ReadWriteTexture2DExtensions.cs create mode 100644 src/ComputeSharp/Graphics/Extensions/ReadWriteTexture3DExtensions.cs diff --git a/src/ComputeSharp/Graphics/Extensions/ReadWriteTexture2DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/ReadWriteTexture2DExtensions.cs new file mode 100644 index 00000000..7b9f48dc --- /dev/null +++ b/src/ComputeSharp/Graphics/Extensions/ReadWriteTexture2DExtensions.cs @@ -0,0 +1,133 @@ +using System; + +namespace ComputeSharp; + +/// +/// A that contains extension methods for the and types. +/// +public static class ReadWriteTexture2DExtensions +{ + /// + /// Retrieves a wrapping instance for the input resource. + /// + /// The input instance to create a wrapper for. + /// An instance wrapping the current resource. + /// + /// The returned instance can be used in a shader to enable texture sampling. + /// + /// This is an advanced API that can only be used after the current instance has been transitioned to be in a readonly state. To do so, + /// use , + /// and specify . After that, this method can be used to get a readonly wrapper for + /// the current texture to use in a shader. This instance should not be cached or reused, but just passed directly to a shader + /// being dispatched through that same , as it will not work if the texture changes state later on. + /// Before the end of that list of operations, the texture also needs to be transitioned back to writeable state, using the same + /// API as before but specifying . Failing to do so results in undefined behavior. + /// + /// + /// Thrown if the current instance or its associated device are disposed. + /// Thrown if the current instance is not in a readonly state. + public static IReadOnlyTexture2D AsReadOnly(this ReadWriteTexture2D texture) + { + return texture.AsReadOnly(); + } + + /// + /// Retrieves a wrapping instance for the input resource. + /// + /// The input instance to create a wrapper for. + /// An instance wrapping the current resource. + /// + /// The returned instance can be used in a shader to enable texture sampling. + /// + /// This is an advanced API that can only be used after the current instance has been transitioned to be in a readonly state. To do so, + /// use , + /// and specify . After that, this method can be used to get a readonly wrapper for + /// the current texture to use in a shader. This instance should not be cached or reused, but just passed directly to a shader + /// being dispatched through that same , as it will not work if the texture changes state later on. + /// Before the end of that list of operations, the texture also needs to be transitioned back to writeable state, using the same + /// API as before but specifying . Failing to do so results in undefined behavior. + /// + /// + /// Thrown if the current instance or its associated device are disposed. + /// Thrown if the current instance is not in a readonly state. + public static IReadOnlyTexture2D AsReadOnly(this ReadWriteTexture2D texture) + { + return texture.AsReadOnly(); + } + + /// + /// Retrieves a wrapping instance for the input resource. + /// + /// The input instance to create a wrapper for. + /// An instance wrapping the current resource. + /// + /// The returned instance can be used in a shader to enable texture sampling. + /// + /// This is an advanced API that can only be used after the current instance has been transitioned to be in a readonly state. To do so, + /// use , + /// and specify . After that, this method can be used to get a readonly wrapper for + /// the current texture to use in a shader. This instance should not be cached or reused, but just passed directly to a shader + /// being dispatched through that same , as it will not work if the texture changes state later on. + /// Before the end of that list of operations, the texture also needs to be transitioned back to writeable state, using the same + /// API as before but specifying . Failing to do so results in undefined behavior. + /// + /// + /// Thrown if the current instance or its associated device are disposed. + /// Thrown if the current instance is not in a readonly state. + public static IReadOnlyTexture2D AsReadOnly(this ReadWriteTexture2D texture) + { + return texture.AsReadOnly(); + } + + /// + /// Retrieves a wrapping instance for the input resource. + /// + /// The input instance to create a wrapper for. + /// An instance wrapping the current resource. + /// + /// The returned instance can be used in a shader to enable texture sampling. + /// + /// This is an advanced API that can only be used after the current instance has been transitioned to be in a readonly state. To do so, + /// use , + /// and specify . After that, this method can be used to get a readonly wrapper for + /// the current texture to use in a shader. This instance should not be cached or reused, but just passed directly to a shader + /// being dispatched through that same , as it will not work if the texture changes state later on. + /// Before the end of that list of operations, the texture also needs to be transitioned back to writeable state, using the same + /// API as before but specifying . Failing to do so results in undefined behavior. + /// + /// + /// Thrown if the current instance or its associated device are disposed. + /// Thrown if the current instance is not in a readonly state. + public static IReadOnlyTexture2D AsReadOnly(this ReadWriteTexture2D texture) + { + return texture.AsReadOnly(); + } + + /// + /// Retrieves a wrapping instance for the input resource. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The input instance to create a wrapper for. + /// An instance wrapping the current resource. + /// + /// The returned instance can be used in a shader to enable texture sampling. + /// + /// This is an advanced API that can only be used after the current instance has been transitioned to be in a readonly state. To do so, + /// use , + /// and specify . After that, this method can be used to get a readonly wrapper for + /// the current texture to use in a shader. This instance should not be cached or reused, but just passed directly to a shader + /// being dispatched through that same , as it will not work if the texture changes state later on. + /// Before the end of that list of operations, the texture also needs to be transitioned back to writeable state, using the same + /// API as before but specifying . Failing to do so results in undefined behavior. + /// + /// + /// Thrown if the current instance or its associated device are disposed. + /// Thrown if the current instance is not in a readonly state. + public static IReadOnlyNormalizedTexture2D AsReadOnly(this ReadWriteTexture2D texture) + where T : unmanaged, IPixel + where TPixel : unmanaged + { + return texture.AsReadOnly(); + } +} diff --git a/src/ComputeSharp/Graphics/Extensions/ReadWriteTexture3DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/ReadWriteTexture3DExtensions.cs new file mode 100644 index 00000000..fd2f9657 --- /dev/null +++ b/src/ComputeSharp/Graphics/Extensions/ReadWriteTexture3DExtensions.cs @@ -0,0 +1,133 @@ +using System; + +namespace ComputeSharp; + +/// +/// A that contains extension methods for the and types. +/// +public static class ReadWriteTexture3DExtensions +{ + /// + /// Retrieves a wrapping instance for the input resource. + /// + /// The input instance to create a wrapper for. + /// An instance wrapping the current resource. + /// + /// The returned instance can be used in a shader to enable texture sampling. + /// + /// This is an advanced API that can only be used after the current instance has been transitioned to be in a readonly state. To do so, + /// use , + /// and specify . After that, this method can be used to get a readonly wrapper for + /// the current texture to use in a shader. This instance should not be cached or reused, but just passed directly to a shader + /// being dispatched through that same , as it will not work if the texture changes state later on. + /// Before the end of that list of operations, the texture also needs to be transitioned back to writeable state, using the same + /// API as before but specifying . Failing to do so results in undefined behavior. + /// + /// + /// Thrown if the current instance or its associated device are disposed. + /// Thrown if the current instance is not in a readonly state. + public static IReadOnlyTexture3D AsReadOnly(this ReadWriteTexture3D texture) + { + return texture.AsReadOnly(); + } + + /// + /// Retrieves a wrapping instance for the input resource. + /// + /// The input instance to create a wrapper for. + /// An instance wrapping the current resource. + /// + /// The returned instance can be used in a shader to enable texture sampling. + /// + /// This is an advanced API that can only be used after the current instance has been transitioned to be in a readonly state. To do so, + /// use , + /// and specify . After that, this method can be used to get a readonly wrapper for + /// the current texture to use in a shader. This instance should not be cached or reused, but just passed directly to a shader + /// being dispatched through that same , as it will not work if the texture changes state later on. + /// Before the end of that list of operations, the texture also needs to be transitioned back to writeable state, using the same + /// API as before but specifying . Failing to do so results in undefined behavior. + /// + /// + /// Thrown if the current instance or its associated device are disposed. + /// Thrown if the current instance is not in a readonly state. + public static IReadOnlyTexture3D AsReadOnly(this ReadWriteTexture3D texture) + { + return texture.AsReadOnly(); + } + + /// + /// Retrieves a wrapping instance for the input resource. + /// + /// The input instance to create a wrapper for. + /// An instance wrapping the current resource. + /// + /// The returned instance can be used in a shader to enable texture sampling. + /// + /// This is an advanced API that can only be used after the current instance has been transitioned to be in a readonly state. To do so, + /// use , + /// and specify . After that, this method can be used to get a readonly wrapper for + /// the current texture to use in a shader. This instance should not be cached or reused, but just passed directly to a shader + /// being dispatched through that same , as it will not work if the texture changes state later on. + /// Before the end of that list of operations, the texture also needs to be transitioned back to writeable state, using the same + /// API as before but specifying . Failing to do so results in undefined behavior. + /// + /// + /// Thrown if the current instance or its associated device are disposed. + /// Thrown if the current instance is not in a readonly state. + public static IReadOnlyTexture3D AsReadOnly(this ReadWriteTexture3D texture) + { + return texture.AsReadOnly(); + } + + /// + /// Retrieves a wrapping instance for the input resource. + /// + /// The input instance to create a wrapper for. + /// An instance wrapping the current resource. + /// + /// The returned instance can be used in a shader to enable texture sampling. + /// + /// This is an advanced API that can only be used after the current instance has been transitioned to be in a readonly state. To do so, + /// use , + /// and specify . After that, this method can be used to get a readonly wrapper for + /// the current texture to use in a shader. This instance should not be cached or reused, but just passed directly to a shader + /// being dispatched through that same , as it will not work if the texture changes state later on. + /// Before the end of that list of operations, the texture also needs to be transitioned back to writeable state, using the same + /// API as before but specifying . Failing to do so results in undefined behavior. + /// + /// + /// Thrown if the current instance or its associated device are disposed. + /// Thrown if the current instance is not in a readonly state. + public static IReadOnlyTexture3D AsReadOnly(this ReadWriteTexture3D texture) + { + return texture.AsReadOnly(); + } + + /// + /// Retrieves a wrapping instance for the input resource. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The input instance to create a wrapper for. + /// An instance wrapping the current resource. + /// + /// The returned instance can be used in a shader to enable texture sampling. + /// + /// This is an advanced API that can only be used after the current instance has been transitioned to be in a readonly state. To do so, + /// use , + /// and specify . After that, this method can be used to get a readonly wrapper for + /// the current texture to use in a shader. This instance should not be cached or reused, but just passed directly to a shader + /// being dispatched through that same , as it will not work if the texture changes state later on. + /// Before the end of that list of operations, the texture also needs to be transitioned back to writeable state, using the same + /// API as before but specifying . Failing to do so results in undefined behavior. + /// + /// + /// Thrown if the current instance or its associated device are disposed. + /// Thrown if the current instance is not in a readonly state. + public static IReadOnlyNormalizedTexture3D AsReadOnly(this ReadWriteTexture3D texture) + where T : unmanaged, IPixel + where TPixel : unmanaged + { + return texture.AsReadOnly(); + } +} diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs index 162c7fb6..55cb5933 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs @@ -21,25 +21,8 @@ partial class ReadWriteTexture2D /// private ReadOnly? readOnlyWrapper; - /// - /// Retrieves a wrapping instance for the current resource. - /// - /// An instance wrapping the current resource. - /// - /// The returned instance can be used in a shader to enable texture sampling. - /// - /// This is an advanced API that can only be used after the current instance has been transitioned to be in a readonly state. To do so, - /// use , - /// and specify . After that, can be used to get a readonly wrapper for - /// the current texture to use in a shader. This instance should not be cached or reused, but just passed directly to a shader - /// being dispatched through that same , as it will not work if the texture changes state later on. - /// Before the end of that list of operations, the texture also needs to be transitioned back to writeable state, using the same - /// API as before but specifying . Failing to do so results in undefined behavior. - /// - /// - /// Thrown if the current instance or its associated device are disposed. - /// Thrown if the current instance is not in a readonly state. - public IReadOnlyNormalizedTexture2D AsReadOnly() + /// + internal IReadOnlyNormalizedTexture2D AsReadOnly() { GraphicsDevice.ThrowIfDisposed(); diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T}.ReadOnly.cs index b8207acd..fdb58dd9 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T}.ReadOnly.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T}.ReadOnly.cs @@ -21,24 +21,7 @@ partial class ReadWriteTexture2D /// private ReadOnly? readOnlyWrapper; - /// - /// Retrieves a wrapping instance for the current resource. - /// - /// An instance wrapping the current resource. - /// - /// The returned instance can be used in a shader to enable texture sampling. - /// - /// This is an advanced API that can only be used after the current instance has been transitioned to be in a readonly state. To do so, - /// use , - /// and specify . After that, can be used to get a readonly wrapper for - /// the current texture to use in a shader. This instance should not be cached or reused, but just passed directly to a shader - /// being dispatched through that same , as it will not work if the texture changes state later on. - /// Before the end of that list of operations, the texture also needs to be transitioned back to writeable state, using the same - /// API as before but specifying . Failing to do so results in undefined behavior. - /// - /// - /// Thrown if the current instance or its associated device are disposed. - /// Thrown if the current instance is not in a readonly state. + /// public IReadOnlyTexture2D AsReadOnly() { GraphicsDevice.ThrowIfDisposed(); diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.ReadOnly.cs index b406276a..270c7135 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.ReadOnly.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.ReadOnly.cs @@ -21,24 +21,7 @@ partial class ReadWriteTexture3D /// private ReadOnly? readOnlyWrapper; - /// - /// Retrieves a wrapping instance for the current resource. - /// - /// An instance wrapping the current resource. - /// - /// The returned instance can be used in a shader to enable texture sampling. - /// - /// This is an advanced API that can only be used after the current instance has been transitioned to be in a readonly state. To do so, - /// use , - /// and specify . After that, can be used to get a readonly wrapper for - /// the current texture to use in a shader. This instance should not be cached or reused, but just passed directly to a shader - /// being dispatched through that same , as it will not work if the texture changes state later on. - /// Before the end of that list of operations, the texture also needs to be transitioned back to writeable state, using the same - /// API as before but specifying . Failing to do so results in undefined behavior. - /// - /// - /// Thrown if the current instance or its associated device are disposed. - /// Thrown if the current instance is not in a readonly state. + /// public IReadOnlyNormalizedTexture3D AsReadOnly() { GraphicsDevice.ThrowIfDisposed(); diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T}.ReadOnly.cs index 0aa28c65..5d38517d 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T}.ReadOnly.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T}.ReadOnly.cs @@ -21,24 +21,7 @@ partial class ReadWriteTexture3D /// private ReadOnly? readOnlyWrapper; - /// - /// Retrieves a wrapping instance for the current resource. - /// - /// An instance wrapping the current resource. - /// - /// The returned instance can be used in a shader to enable texture sampling. - /// - /// This is an advanced API that can only be used after the current instance has been transitioned to be in a readonly state. To do so, - /// use , - /// and specify . After that, can be used to get a readonly wrapper for - /// the current texture to use in a shader. This instance should not be cached or reused, but just passed directly to a shader - /// being dispatched through that same , as it will not work if the texture changes state later on. - /// Before the end of that list of operations, the texture also needs to be transitioned back to writeable state, using the same - /// API as before but specifying . Failing to do so results in undefined behavior. - /// - /// - /// Thrown if the current instance or its associated device are disposed. - /// Thrown if the current instance is not in a readonly state. + /// public IReadOnlyTexture3D AsReadOnly() { GraphicsDevice.ThrowIfDisposed(); diff --git a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs index 72f07318..ffe0449b 100644 --- a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs +++ b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs @@ -363,6 +363,58 @@ public static class ComputeContextExtensions context.Run(texture, ref Unsafe.AsRef(in shader)); } + /// + /// Transitions the state of a specific resource. + /// + /// The to use to transition the resource. + /// The input instance to transition. + /// The state to transition the input resource to. + public static unsafe void Transition(this in ComputeContext context, ReadWriteTexture2D texture, ResourceState resourceState) + { + var states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource); + + context.Transition(d3D12Resource, states.Before, states.After); + } + + /// + /// Transitions the state of a specific resource. + /// + /// The to use to transition the resource. + /// The input instance to transition. + /// The state to transition the input resource to. + public static unsafe void Transition(this in ComputeContext context, ReadWriteTexture2D texture, ResourceState resourceState) + { + var states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource); + + context.Transition(d3D12Resource, states.Before, states.After); + } + + /// + /// Transitions the state of a specific resource. + /// + /// The to use to transition the resource. + /// The input instance to transition. + /// The state to transition the input resource to. + public static unsafe void Transition(this in ComputeContext context, ReadWriteTexture2D texture, ResourceState resourceState) + { + var states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource); + + context.Transition(d3D12Resource, states.Before, states.After); + } + + /// + /// Transitions the state of a specific resource. + /// + /// The to use to transition the resource. + /// The input instance to transition. + /// The state to transition the input resource to. + public static unsafe void Transition(this in ComputeContext context, ReadWriteTexture2D texture, ResourceState resourceState) + { + var states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource); + + context.Transition(d3D12Resource, states.Before, states.After); + } + /// /// Transitions the state of a specific resource. /// @@ -380,6 +432,58 @@ public static class ComputeContextExtensions context.Transition(d3D12Resource, states.Before, states.After); } + /// + /// Transitions the state of a specific resource. + /// + /// The to use to transition the resource. + /// The input instance to transition. + /// The state to transition the input resource to. + public static unsafe void Transition(this in ComputeContext context, ReadWriteTexture3D texture, ResourceState resourceState) + { + var states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource); + + context.Transition(d3D12Resource, states.Before, states.After); + } + + /// + /// Transitions the state of a specific resource. + /// + /// The to use to transition the resource. + /// The input instance to transition. + /// The state to transition the input resource to. + public static unsafe void Transition(this in ComputeContext context, ReadWriteTexture3D texture, ResourceState resourceState) + { + var states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource); + + context.Transition(d3D12Resource, states.Before, states.After); + } + + /// + /// Transitions the state of a specific resource. + /// + /// The to use to transition the resource. + /// The input instance to transition. + /// The state to transition the input resource to. + public static unsafe void Transition(this in ComputeContext context, ReadWriteTexture3D texture, ResourceState resourceState) + { + var states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource); + + context.Transition(d3D12Resource, states.Before, states.After); + } + + /// + /// Transitions the state of a specific resource. + /// + /// The to use to transition the resource. + /// The input instance to transition. + /// The state to transition the input resource to. + public static unsafe void Transition(this in ComputeContext context, ReadWriteTexture3D texture, ResourceState resourceState) + { + var states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource); + + context.Transition(d3D12Resource, states.Before, states.After); + } + /// /// Transitions the state of a specific resource. ///