Add ReadWriteTexture3D<T, TPixel>.ReadOnly type

このコミットが含まれているのは:
Sergio Pedri committed 2022-01-17 00:29:37 +01:00
1 親 4fce0c1d2d
コミット b55f6bd346
4ファイル変更
+180 -2

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

+5
ファイルの表示
@@ -166,6 +166,11 @@ public unsafe abstract class Texture3D<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="Texture3D{T}"/> instance and writes them into a target memory area.
/// </summary>
+4 -1
ファイルの表示
@@ -81,7 +81,10 @@ partial class ReadWriteTexture2D<T, TPixel>
/// </summary>
private sealed unsafe class ReadOnly : NativeObject, IReadOnlyTexture2D<TPixel>, GraphicsResourceHelper.IGraphicsResource
{
private ReadWriteTexture2D<T, TPixel> owner;
/// <summary>
/// The owning <see cref="ReadWriteTexture2D{T, TPixel}"/> instance being wrapped.
/// </summary>
private readonly ReadWriteTexture2D<T, TPixel> owner;
/// <summary>
/// The <see cref="ID3D12ResourceDescriptorHandles"/> instance for the current resource.
+170
ファイルの表示
@@ -0,0 +1,170 @@
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 ReadWriteTexture3D<T, TPixel>
{
/// <summary>
/// The wrapping <see cref="ReadOnly"/> instance, if available.
/// </summary>
private ReadOnly? readOnlyWrapper;
/// <summary>
/// Retrieves a wrapping <see cref="IReadOnlyTexture3D{TPixel}"/> instance for the current resource.
/// </summary>
/// <returns>An <see cref="IReadOnlyTexture3D{TPixel}"/> instance wrapping the current resource.</returns>
/// <remarks>The returned instance can be used in a shader to enable texture sampling.</remarks>
public IReadOnlyTexture3D<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;
}
[MethodImpl(MethodImplOptions.NoInlining)]
static void InitializeWrapper(ReadWriteTexture3D<T, TPixel> texture)
{
texture.readOnlyWrapper = new ReadOnly(texture);
}
InitializeWrapper(this);
goto GetWrapperAndReturnIfNotNull;
}
/// <inheritdoc/>
protected override void OnDispose()
{
base.OnDispose();
this.readOnlyWrapper?.Dispose();
}
/// <summary>
/// A wrapper for a <see cref="ReadWriteTexture3D{T, TPixel}"/> resource that has been temporarily transitioned to readonly.
/// </summary>
private sealed unsafe class ReadOnly : NativeObject, IReadOnlyTexture3D<TPixel>, GraphicsResourceHelper.IGraphicsResource
{
/// <summary>
/// The owning <see cref="ReadWriteTexture3D{T, TPixel}"/> instance being wrapped.
/// </summary>
private readonly ReadWriteTexture3D<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="ReadWriteTexture3D{T, TPixel}"/> instance to wrap.</param>
public ReadOnly(ReadWriteTexture3D<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, int z] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture3D<T, TPixel>.ReadOnly)}[{typeof(int)}, {typeof(int)}, {typeof(int)}]");
/// <inheritdoc/>
public TPixel this[Int3 xyz] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture3D<T, TPixel>.ReadOnly)}[{typeof(Int3)}]");
/// <inheritdoc/>
public TPixel this[float u, float v, float w] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture3D<T, TPixel>.ReadOnly)}[{typeof(float)}, {typeof(float)}, {typeof(float)}]");
/// <inheritdoc/>
public TPixel this[Float3 uvw] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture3D<T, TPixel>.ReadOnly)}[{typeof(Float3)}]");
/// <inheritdoc/>
public int Width => this.owner.Width;
/// <inheritdoc/>
public int Height => this.owner.Height;
/// <inheritdoc/>
public int Depth => this.owner.Depth;
/// <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(Texture3DDebugView<>))]
[DebuggerDisplay("{ToString(),raw}")]
public sealed class ReadWriteTexture3D<T, TPixel> : Texture3D<T>, IReadWriteTexture3D<TPixel>
public sealed partial class ReadWriteTexture3D<T, TPixel> : Texture3D<T>, IReadWriteTexture3D<TPixel>
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{