Add ReadWriteTexture2D<T, TPixel>.ReadOnly type

このコミットが含まれているのは:
Sergio Pedri committed 2022-01-17 00:29:37 +01:00
1 親 5d66b7fe05
コミット 095c715515
3ファイル変更
+173 -1

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

+12
ファイルの表示
@@ -156,6 +156,11 @@ public unsafe abstract class Texture2D<T> : NativeObject, IGraphicsResource, Gra
/// </summary>
internal D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle => this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle;
/// <summary>
/// Gets whether or not the current resource is in readonly state.
/// </summary>
protected bool IsInReadOnlyState => this.d3D12ResourceState != D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target memory area.
/// </summary>
@@ -582,6 +587,13 @@ public unsafe abstract class Texture2D<T> : NativeObject, IGraphicsResource, Gra
D3D12_RESOURCE_STATES d3D12ResourceStatesBefore = this.d3D12ResourceState;
D3D12_RESOURCE_STATES d3D12ResourceStatesAfter = ResourceStateHelper.GetD3D12ResourceStates(resourceState);
// If the original state is COMMON, at the point where the transition is inserted into a ComputeContext the resource
// would have already been implicitly promoted to be NON_PIXEL_SHADER_RESOURCE. Because of this, adjust this transition.
if (d3D12ResourceStatesBefore == D3D12_RESOURCE_STATE_COMMON)
{
d3D12ResourceStatesBefore = D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
}
this.d3D12ResourceState = d3D12ResourceStatesAfter;
d3D12Resource = D3D12Resource;
+160
ファイルの表示
@@ -0,0 +1,160 @@
using System;
using System.Runtime.CompilerServices;
using ComputeSharp.__Internals;
using ComputeSharp.Exceptions;
using ComputeSharp.Graphics.Commands.Interop;
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.Interop;
using TerraFX.Interop.DirectX;
using static TerraFX.Interop.DirectX.D3D12_SRV_DIMENSION;
#pragma warning disable CS0618
namespace ComputeSharp;
/// <inheritdoc/>
partial class ReadWriteTexture2D<T, TPixel>
{
/// <summary>
/// The wrapping <see cref="ReadOnly"/> instance, if available.
/// </summary>
private ReadOnly? readOnlyWrapper;
/// <summary>
/// Retrieves a wrapping <see cref="IReadOnlyTexture2D{TPixel}"/> instance for the current resource.
/// </summary>
/// <returns>An <see cref="IReadOnlyTexture2D{TPixel}"/> instance wrapping the current resource.</returns>
/// <remarks>The returned instance can be used in a shader to enable texture sampling.</remarks>
public IReadOnlyTexture2D<TPixel> AsReadOnly()
{
GraphicsDevice.ThrowIfDisposed();
ThrowIfDisposed();
if (!IsInReadOnlyState)
{
static void Throw()
{
throw new InvalidOperationException(
"The texture is not currently in readonly mode. This API can only be used when creating a compute graph with " +
"the ComputeContext type, and after having used ComputeContext.Transition() to change the state of the texture.");
}
Throw();
}
GetWrapperAndReturnIfNotNull:
ReadOnly? readOnlyWrapper = this.readOnlyWrapper;
if (readOnlyWrapper is not null)
{
return readOnlyWrapper;
}
// The wrapper initialization is moved to a non inlined helper to keep the codegen of this
// method as compact as possible. If the current wrapper is not initialized, a new one is
// created, then code jumps back to the check. This allows the method to have a single exit
// point, which further improves the final codegen.
[MethodImpl(MethodImplOptions.NoInlining)]
static void InitializeWrapper(ReadWriteTexture2D<T, TPixel> texture)
{
texture.readOnlyWrapper = new ReadOnly(texture);
}
InitializeWrapper(this);
goto GetWrapperAndReturnIfNotNull;
}
/// <summary>
/// A wrapper for a <see cref="ReadWriteTexture2D{T, TPixel}"/> resource that has been temporarily transitioned to readonly.
/// </summary>
private sealed unsafe class ReadOnly : NativeObject, IReadOnlyTexture2D<TPixel>, GraphicsResourceHelper.IGraphicsResource
{
private ReadWriteTexture2D<T, TPixel> owner;
/// <summary>
/// The <see cref="ID3D12ResourceDescriptorHandles"/> instance for the current resource.
/// </summary>
private readonly ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles;
/// <summary>
/// Creates a new <see cref="ReadOnly"/> instance with the specified parameters.
/// </summary>
/// <param name="owner">The owning <see cref="ReadWriteTexture2D{T, TPixel}"/> instance to wrap.</param>
public ReadOnly(ReadWriteTexture2D<T, TPixel> owner)
{
this.owner = owner;
owner.GraphicsDevice.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles);
owner.GraphicsDevice.D3D12Device->CreateShaderResourceView(owner.D3D12Resource, DXGIFormatHelper.GetForType<T>(), D3D12_SRV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
}
/// <inheritdoc/>
public TPixel this[int x, int y] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture2D<T, TPixel>.ReadOnly)}[{typeof(int)}, {typeof(int)}]");
/// <inheritdoc/>
public TPixel this[Int2 xy] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture2D<T, TPixel>.ReadOnly)}[{typeof(Int2)}]");
/// <inheritdoc/>
public TPixel this[float u, float v] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture2D<T, TPixel>.ReadOnly)}[{typeof(float)}, {typeof(float)}]");
/// <inheritdoc/>
public TPixel this[Float2 uv] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture2D<T, TPixel>.ReadOnly)}[{typeof(Float2)}]");
/// <inheritdoc/>
public int Width => this.owner.Width;
/// <inheritdoc/>
public int Height => this.owner.Height;
/// <inheritdoc/>
public GraphicsDevice GraphicsDevice => this.owner.GraphicsDevice;
/// <inheritdoc/>
D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device)
{
ThrowIfDisposed();
this.owner.ThrowIfDisposed();
this.owner.ThrowIfDeviceMismatch(device);
return this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle;
}
/// <inheritdoc/>
(D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized)
{
throw new NotSupportedException("This operation cannot be performaned on a readonly wrapper.");
}
/// <inheritdoc/>
ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device)
{
ThrowIfDisposed();
this.owner.ThrowIfDisposed();
this.owner.ThrowIfDeviceMismatch(device);
return this.owner.D3D12Resource;
}
/// <inheritdoc/>
(D3D12_RESOURCE_STATES, D3D12_RESOURCE_STATES) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource)
{
throw new NotSupportedException("This operation cannot be performaned on a readonly wrapper.");
}
/// <inheritdoc/>
protected override void OnDispose()
{
if (this.owner.GraphicsDevice is GraphicsDevice device)
{
device.ReturnShaderResourceViewDescriptorHandles(in this.d3D12ResourceDescriptorHandles);
}
}
}
}
+1 -1
ファイルの表示
@@ -14,7 +14,7 @@ namespace ComputeSharp;
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
[DebuggerTypeProxy(typeof(Texture2DDebugView<>))]
[DebuggerDisplay("{ToString(),raw}")]
public sealed class ReadWriteTexture2D<T, TPixel> : Texture2D<T>, IReadWriteTexture2D<TPixel>
public sealed partial class ReadWriteTexture2D<T, TPixel> : Texture2D<T>, IReadWriteTexture2D<TPixel>
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{