Switch to ReferenceTracker for all device/resource types
このコミットが含まれているのは:
1 親
c5997d2c62
コミット
297aa7ab36
19ファイル変更
+1747
-1341
検索条件に一致するファイルはありません
@@ -22,15 +22,15 @@ public sealed class GraphicsDeviceMismatchException : InvalidOperationException
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="GraphicsDeviceMismatchException"/> instance from the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="resource">The input <see cref="NativeObject"/> that was used.</param>
|
||||
/// <param name="resource">The input <see cref="ReferenceTracker.ITrackedObject"/> that was used.</param>
|
||||
/// <param name="sourceDevice">The source <see cref="GraphicsDevice"/> instance tied to <paramref name="resource"/>.</param>
|
||||
/// <param name="destinationDevice">The target <see cref="GraphicsDevice"/> instance that was used.</param>
|
||||
/// <returns>A new <see cref="GraphicsDeviceMismatchException"/> instance with a formatted error message.</returns>
|
||||
/// <remarks>
|
||||
/// This method only takes a <see cref="NativeObject"/> instance and the associated <see cref="GraphicsDevice"/> instance as
|
||||
/// This method only takes a <see cref="ReferenceTracker.ITrackedObject"/> instance and the associated <see cref="GraphicsDevice"/> instance as
|
||||
/// <see cref="object.GetType"/> will still be available, but without the unnecessary generic type specializations for the method.
|
||||
/// </remarks>
|
||||
private static GraphicsDeviceMismatchException Create(NativeObject resource, GraphicsDevice sourceDevice, GraphicsDevice destinationDevice)
|
||||
private static GraphicsDeviceMismatchException Create(ReferenceTracker.ITrackedObject resource, GraphicsDevice sourceDevice, GraphicsDevice destinationDevice)
|
||||
{
|
||||
StringBuilder builder = new(512);
|
||||
|
||||
|
||||
@@ -150,9 +150,11 @@ public static partial class GraphicsDeviceExtensions
|
||||
{
|
||||
Guard.IsNotNull(device);
|
||||
|
||||
device.ThrowIfDisposed();
|
||||
device.ThrowIfDeviceLost();
|
||||
using (device.GetReferenceTracker().GetLease())
|
||||
{
|
||||
device.ThrowIfDeviceLost();
|
||||
|
||||
return new(device);
|
||||
return new(device);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -48,42 +48,32 @@ unsafe partial class GraphicsDevice
|
||||
/// </summary>
|
||||
private void QueueRaiseDeviceLostEventIfNeeded()
|
||||
{
|
||||
if (IsDisposed)
|
||||
using (this.referenceTracker.TryGetLease(out bool leaseTaken))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// If the current device removed reason is not S_OK, it means the event
|
||||
// has already been raised (but the device will keep reporting that reason).
|
||||
// In that case, do nothing to avoid raising it again while the device is alive.
|
||||
if (Volatile.Read(ref Unsafe.As<HRESULT, int>(ref this.deviceRemovedReason)) != S.S_OK)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
HRESULT result;
|
||||
|
||||
// Make sure to guard against concurrent accesses from the dispose logic
|
||||
lock (this.d3D12DeviceLock)
|
||||
{
|
||||
ID3D12Device* d3D12Device = this.d3D12Device.Get();
|
||||
|
||||
if (d3D12Device is null)
|
||||
if (!leaseTaken)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
result = d3D12Device->GetDeviceRemovedReason();
|
||||
}
|
||||
// If the current device removed reason is not S_OK, it means the event
|
||||
// has already been raised (but the device will keep reporting that reason).
|
||||
// In that case, do nothing to avoid raising it again while the device is alive.
|
||||
if (Volatile.Read(ref Unsafe.As<HRESULT, int>(ref this.deviceRemovedReason)) != S.S_OK)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Only raise the event once, and store the device removed reason to track it
|
||||
if (result != S.S_OK &&
|
||||
Interlocked.CompareExchange(
|
||||
ref Unsafe.As<HRESULT, int>(ref this.deviceRemovedReason),
|
||||
(int)result,
|
||||
S.S_OK) == S.S_OK)
|
||||
{
|
||||
QueueRaiseDeviceLostEvent();
|
||||
HRESULT result = this.d3D12Device.Get()->GetDeviceRemovedReason();
|
||||
|
||||
// Only raise the event once, and store the device removed reason to track it
|
||||
if (result != S.S_OK &&
|
||||
Interlocked.CompareExchange(
|
||||
ref Unsafe.As<HRESULT, int>(ref this.deviceRemovedReason),
|
||||
(int)result,
|
||||
S.S_OK) == S.S_OK)
|
||||
{
|
||||
QueueRaiseDeviceLostEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,8 +23,13 @@ namespace ComputeSharp;
|
||||
/// A <see langword="class"/> that represents an <see cref="ID3D12Device"/> instance that can be used to run compute shaders.
|
||||
/// </summary>
|
||||
[DebuggerDisplay("{ToString(),raw}")]
|
||||
public sealed unsafe partial class GraphicsDevice : NativeObject
|
||||
public sealed unsafe partial class GraphicsDevice : ReferenceTracker.ITrackedObject, IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// The owning <see cref="ReferenceTracker"/> object for the current instance.
|
||||
/// </summary>
|
||||
private ReferenceTracker referenceTracker;
|
||||
|
||||
/// <summary>
|
||||
/// The underlying <see cref="ID3D12Device"/> wrapped by the current instance.
|
||||
/// </summary>
|
||||
@@ -107,13 +112,6 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
|
||||
/// </summary>
|
||||
private HRESULT deviceRemovedReason;
|
||||
|
||||
/// <summary>
|
||||
/// A lcok object used to synchronize accesses to <see cref="d3D12Device"/> between <see cref="OnDispose"/> and <see cref="QueueRaiseDeviceLostEventIfNeeded"/>.
|
||||
/// This is needed because the device lost callback might race with a dispose call and end up trying to invoke a method on an invalid object.
|
||||
/// We can't just check whether it's <see langword="null"/>, as a null condition might still cause a racing thread to access a freed up object.
|
||||
/// </summary>
|
||||
private readonly object d3D12DeviceLock = new();
|
||||
|
||||
/// <summary>
|
||||
/// Raised whenever the device is lost.
|
||||
/// </summary>
|
||||
@@ -141,6 +139,8 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
|
||||
/// <param name="dxgiDescription1">The available info for the new <see cref="GraphicsDevice"/> instance.</param>
|
||||
internal GraphicsDevice(ID3D12Device* d3D12Device, IDXGIAdapter* dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1)
|
||||
{
|
||||
this.referenceTracker = new ReferenceTracker(this);
|
||||
|
||||
this.d3D12Device = new ComPtr<ID3D12Device>(d3D12Device);
|
||||
|
||||
this.d3D12ComputeCommandQueue = d3D12Device->CreateCommandQueue(D3D12_COMMAND_LIST_TYPE_COMPUTE);
|
||||
@@ -181,6 +181,14 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
|
||||
RegisterDeviceLostCallback(this, out this.deviceHandle, out this.deviceRemovedEvent, out this.deviceRemovedWaitHandle);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Releases unmanaged resources for the current <see cref="GraphicsDevice"/> instance.
|
||||
/// </summary>
|
||||
~GraphicsDevice()
|
||||
{
|
||||
this.referenceTracker.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the locally unique identifier for the current device.
|
||||
/// </summary>
|
||||
@@ -245,12 +253,14 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
|
||||
/// <returns>Whether the current device supports double precision floating point operations in shaders.</returns>
|
||||
public bool IsDoublePrecisionSupportAvailable()
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
ThrowIfDeviceLost();
|
||||
using (this.referenceTracker.GetLease())
|
||||
{
|
||||
ThrowIfDeviceLost();
|
||||
|
||||
var d3D12OptionsData = this.d3D12Device.Get()->CheckFeatureSupport<D3D12_FEATURE_DATA_D3D12_OPTIONS>(D3D12_FEATURE_D3D12_OPTIONS);
|
||||
var d3D12OptionsData = this.d3D12Device.Get()->CheckFeatureSupport<D3D12_FEATURE_DATA_D3D12_OPTIONS>(D3D12_FEATURE_D3D12_OPTIONS);
|
||||
|
||||
return d3D12OptionsData.DoublePrecisionFloatShaderOps;
|
||||
return d3D12OptionsData.DoublePrecisionFloatShaderOps;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -262,10 +272,12 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
|
||||
public bool IsReadOnlyTexture2DSupportedForType<T>()
|
||||
where T : unmanaged
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
ThrowIfDeviceLost();
|
||||
using (this.referenceTracker.GetLease())
|
||||
{
|
||||
ThrowIfDeviceLost();
|
||||
|
||||
return this.d3D12Device.Get()->IsDxgiFormatSupported(DXGIFormatHelper.GetForType<T>(), D3D12_FORMAT_SUPPORT1_TEXTURE2D);
|
||||
return this.d3D12Device.Get()->IsDxgiFormatSupported(DXGIFormatHelper.GetForType<T>(), D3D12_FORMAT_SUPPORT1_TEXTURE2D);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -277,12 +289,14 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
|
||||
public bool IsReadWriteTexture2DSupportedForType<T>()
|
||||
where T : unmanaged
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
ThrowIfDeviceLost();
|
||||
using (this.referenceTracker.GetLease())
|
||||
{
|
||||
ThrowIfDeviceLost();
|
||||
|
||||
return this.d3D12Device.Get()->IsDxgiFormatSupported(
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
D3D12_FORMAT_SUPPORT1_TEXTURE2D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW);
|
||||
return this.d3D12Device.Get()->IsDxgiFormatSupported(
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
D3D12_FORMAT_SUPPORT1_TEXTURE2D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -294,10 +308,12 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
|
||||
public bool IsReadOnlyTexture3DSupportedForType<T>()
|
||||
where T : unmanaged
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
ThrowIfDeviceLost();
|
||||
using (this.referenceTracker.GetLease())
|
||||
{
|
||||
ThrowIfDeviceLost();
|
||||
|
||||
return this.d3D12Device.Get()->IsDxgiFormatSupported(DXGIFormatHelper.GetForType<T>(), D3D12_FORMAT_SUPPORT1_TEXTURE3D);
|
||||
return this.d3D12Device.Get()->IsDxgiFormatSupported(DXGIFormatHelper.GetForType<T>(), D3D12_FORMAT_SUPPORT1_TEXTURE3D);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -309,12 +325,14 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
|
||||
public bool IsReadWriteTexture3DSupportedForType<T>()
|
||||
where T : unmanaged
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
ThrowIfDeviceLost();
|
||||
using (this.referenceTracker.GetLease())
|
||||
{
|
||||
ThrowIfDeviceLost();
|
||||
|
||||
return this.d3D12Device.Get()->IsDxgiFormatSupported(
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
D3D12_FORMAT_SUPPORT1_TEXTURE3D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW);
|
||||
return this.d3D12Device.Get()->IsDxgiFormatSupported(
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
D3D12_FORMAT_SUPPORT1_TEXTURE3D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -405,16 +423,32 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void OnDispose()
|
||||
public void Dispose()
|
||||
{
|
||||
this.referenceTracker.Dispose();
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ReferenceTracker.ITrackedObject.GetReferenceTracker"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal ref ReferenceTracker GetReferenceTracker()
|
||||
{
|
||||
return ref this.referenceTracker;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
ref ReferenceTracker ReferenceTracker.ITrackedObject.GetReferenceTracker()
|
||||
{
|
||||
return ref this.referenceTracker;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ReferenceTracker.ITrackedObject.DangerousRelease()
|
||||
{
|
||||
DeviceHelper.NotifyDisposedDevice(this);
|
||||
|
||||
// Guard the dispose call around the native device to avoid device lost races
|
||||
lock (this.d3D12DeviceLock)
|
||||
{
|
||||
this.d3D12Device.Dispose();
|
||||
}
|
||||
|
||||
this.d3D12Device.Dispose();
|
||||
this.d3D12ComputeCommandQueue.Dispose();
|
||||
this.d3D12CopyCommandQueue.Dispose();
|
||||
this.d3D12ComputeFence.Dispose();
|
||||
|
||||
@@ -64,12 +64,14 @@ public static class GraphicsResourceHelper
|
||||
{
|
||||
Guard.IsNotNull(buffer);
|
||||
|
||||
buffer.ThrowIfDisposed();
|
||||
buffer.ThrowIfDeviceMismatch(device);
|
||||
using (buffer.GetReferenceTracker().GetLease())
|
||||
{
|
||||
buffer.ThrowIfDeviceMismatch(device);
|
||||
|
||||
D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = buffer.D3D12GpuDescriptorHandle;
|
||||
D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = buffer.D3D12GpuDescriptorHandle;
|
||||
|
||||
return *(ulong*)&d3D12GpuDescriptorHandle;
|
||||
return *(ulong*)&d3D12GpuDescriptorHandle;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -85,12 +87,14 @@ public static class GraphicsResourceHelper
|
||||
{
|
||||
Guard.IsNotNull(texture);
|
||||
|
||||
texture.ThrowIfDisposed();
|
||||
texture.ThrowIfDeviceMismatch(device);
|
||||
using (texture.GetReferenceTracker().GetLease())
|
||||
{
|
||||
texture.ThrowIfDeviceMismatch(device);
|
||||
|
||||
D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle;
|
||||
D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle;
|
||||
|
||||
return *(ulong*)&d3D12GpuDescriptorHandle;
|
||||
return *(ulong*)&d3D12GpuDescriptorHandle;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -106,12 +110,14 @@ public static class GraphicsResourceHelper
|
||||
{
|
||||
Guard.IsNotNull(texture);
|
||||
|
||||
texture.ThrowIfDisposed();
|
||||
texture.ThrowIfDeviceMismatch(device);
|
||||
using (texture.GetReferenceTracker().GetLease())
|
||||
{
|
||||
texture.ThrowIfDeviceMismatch(device);
|
||||
|
||||
D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle;
|
||||
D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle;
|
||||
|
||||
return *(ulong*)&d3D12GpuDescriptorHandle;
|
||||
return *(ulong*)&d3D12GpuDescriptorHandle;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -196,12 +202,14 @@ public static class GraphicsResourceHelper
|
||||
{
|
||||
Guard.IsNotNull(texture);
|
||||
|
||||
texture.ThrowIfDisposed();
|
||||
texture.ThrowIfDeviceMismatch(device);
|
||||
using (texture.GetReferenceTracker().GetLease())
|
||||
{
|
||||
texture.ThrowIfDeviceMismatch(device);
|
||||
|
||||
D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle;
|
||||
D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle;
|
||||
|
||||
return *(ulong*)&d3D12GpuDescriptorHandle;
|
||||
return *(ulong*)&d3D12GpuDescriptorHandle;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -217,12 +225,14 @@ public static class GraphicsResourceHelper
|
||||
{
|
||||
Guard.IsNotNull(texture);
|
||||
|
||||
texture.ThrowIfDisposed();
|
||||
texture.ThrowIfDeviceMismatch(device);
|
||||
using (texture.GetReferenceTracker().GetLease())
|
||||
{
|
||||
texture.ThrowIfDeviceMismatch(device);
|
||||
|
||||
D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle;
|
||||
D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle;
|
||||
|
||||
return *(ulong*)&d3D12GpuDescriptorHandle;
|
||||
return *(ulong*)&d3D12GpuDescriptorHandle;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace ComputeSharp.Interop;
|
||||
/// <summary>
|
||||
/// Base class for a <see cref="IDisposable"/> class.
|
||||
/// </summary>
|
||||
public abstract class NativeObject : IDisposable
|
||||
internal abstract class NativeObject : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Indicates whether or not the current instance has been disposed.
|
||||
|
||||
@@ -154,7 +154,7 @@ internal struct ReferenceTracker : IDisposable
|
||||
/// <summary>
|
||||
/// A reference tracking lease to extend the lifetime of a given tracked object while in a given scope.
|
||||
/// </summary>
|
||||
public struct Lease
|
||||
public struct Lease : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// The tracked object being wrapped, if any.
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Buffers;
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Runtime.CompilerServices;
|
||||
using CommunityToolkit.Diagnostics;
|
||||
using ComputeSharp.Core.Helpers;
|
||||
@@ -24,9 +25,14 @@ namespace ComputeSharp.Resources;
|
||||
/// A <see langword="class"/> representing a typed buffer stored on GPU memory.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the buffer.</typeparam>
|
||||
public unsafe abstract class Buffer<T> : NativeObject, IGraphicsResource
|
||||
public unsafe abstract class Buffer<T> : ReferenceTracker.ITrackedObject, IDisposable, IGraphicsResource
|
||||
where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// The owning <see cref="ReferenceTracker"/> object for the current instance.
|
||||
/// </summary>
|
||||
private ReferenceTracker referenceTracker;
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12MA_Allocation"/> instance used to retrieve <see cref="d3D12Resource"/>.
|
||||
@@ -64,63 +70,75 @@ public unsafe abstract class Buffer<T> : NativeObject, IGraphicsResource
|
||||
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
|
||||
private protected Buffer(GraphicsDevice device, int length, uint elementSizeInBytes, ResourceType resourceType, AllocationMode allocationMode)
|
||||
{
|
||||
device.ThrowIfDisposed();
|
||||
device.ThrowIfDeviceLost();
|
||||
this.referenceTracker = new ReferenceTracker(this);
|
||||
|
||||
if (resourceType == ResourceType.Constant)
|
||||
using (device.GetReferenceTracker().GetLease())
|
||||
{
|
||||
Guard.IsBetweenOrEqualTo(length, 1, D3D12.D3D12_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
|
||||
}
|
||||
else
|
||||
{
|
||||
// The maximum length is set such that the aligned buffer size can't exceed uint.MaxValue
|
||||
Guard.IsBetweenOrEqualTo(length, 1, (uint.MaxValue / elementSizeInBytes) & ~255);
|
||||
}
|
||||
device.ThrowIfDeviceLost();
|
||||
|
||||
if (TypeInfo<T>.IsDoubleOrContainsDoubles &&
|
||||
device.D3D12Device->CheckFeatureSupport<D3D12_FEATURE_DATA_D3D12_OPTIONS>(D3D12_FEATURE_D3D12_OPTIONS).DoublePrecisionFloatShaderOps == 0)
|
||||
{
|
||||
UnsupportedDoubleOperationException.Throw<T>();
|
||||
}
|
||||
if (resourceType == ResourceType.Constant)
|
||||
{
|
||||
Guard.IsBetweenOrEqualTo(length, 1, D3D12.D3D12_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
|
||||
}
|
||||
else
|
||||
{
|
||||
// The maximum length is set such that the aligned buffer size can't exceed uint.MaxValue
|
||||
Guard.IsBetweenOrEqualTo(length, 1, (uint.MaxValue / elementSizeInBytes) & ~255);
|
||||
}
|
||||
|
||||
nint usableSizeInBytes = checked((nint)(length * elementSizeInBytes));
|
||||
nint effectiveSizeInBytes = resourceType == ResourceType.Constant ? AlignmentHelper.Pad(usableSizeInBytes, D3D12.D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT) : usableSizeInBytes;
|
||||
if (TypeInfo<T>.IsDoubleOrContainsDoubles &&
|
||||
device.D3D12Device->CheckFeatureSupport<D3D12_FEATURE_DATA_D3D12_OPTIONS>(D3D12_FEATURE_D3D12_OPTIONS).DoublePrecisionFloatShaderOps == 0)
|
||||
{
|
||||
UnsupportedDoubleOperationException.Throw<T>();
|
||||
}
|
||||
|
||||
SizeInBytes = usableSizeInBytes;
|
||||
GraphicsDevice = device;
|
||||
Length = length;
|
||||
nint usableSizeInBytes = checked((nint)(length * elementSizeInBytes));
|
||||
nint effectiveSizeInBytes = resourceType == ResourceType.Constant ? AlignmentHelper.Pad(usableSizeInBytes, D3D12.D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT) : usableSizeInBytes;
|
||||
|
||||
SizeInBytes = usableSizeInBytes;
|
||||
GraphicsDevice = device;
|
||||
Length = length;
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, (ulong)effectiveSizeInBytes);
|
||||
this.d3D12Resource = new ComPtr<ID3D12Resource>(this.allocation.Get()->GetResource());
|
||||
this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, (ulong)effectiveSizeInBytes);
|
||||
this.d3D12Resource = new ComPtr<ID3D12Resource>(this.allocation.Get()->GetResource());
|
||||
#else
|
||||
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, (ulong)effectiveSizeInBytes, device.IsCacheCoherentUMA);
|
||||
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, (ulong)effectiveSizeInBytes, device.IsCacheCoherentUMA);
|
||||
#endif
|
||||
|
||||
device.RegisterAllocatedResource();
|
||||
device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles);
|
||||
device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView);
|
||||
device.RegisterAllocatedResource();
|
||||
device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles);
|
||||
device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView);
|
||||
|
||||
switch (resourceType)
|
||||
{
|
||||
case ResourceType.Constant:
|
||||
device.D3D12Device->CreateConstantBufferView(this.d3D12Resource.Get(), effectiveSizeInBytes, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
|
||||
break;
|
||||
case ResourceType.ReadOnly:
|
||||
device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
|
||||
break;
|
||||
case ResourceType.ReadWrite:
|
||||
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
|
||||
device.D3D12Device->CreateUnorderedAccessViewForClear(
|
||||
this.d3D12Resource.Get(),
|
||||
DXGI_FORMAT_R32_UINT,
|
||||
(uint)(usableSizeInBytes / sizeof(uint)),
|
||||
this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12CpuDescriptorHandle,
|
||||
this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12CpuDescriptorHandleNonShaderVisible);
|
||||
break;
|
||||
switch (resourceType)
|
||||
{
|
||||
case ResourceType.Constant:
|
||||
device.D3D12Device->CreateConstantBufferView(this.d3D12Resource.Get(), effectiveSizeInBytes, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
|
||||
break;
|
||||
case ResourceType.ReadOnly:
|
||||
device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
|
||||
break;
|
||||
case ResourceType.ReadWrite:
|
||||
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
|
||||
device.D3D12Device->CreateUnorderedAccessViewForClear(
|
||||
this.d3D12Resource.Get(),
|
||||
DXGI_FORMAT_R32_UINT,
|
||||
(uint)(usableSizeInBytes / sizeof(uint)),
|
||||
this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12CpuDescriptorHandle,
|
||||
this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12CpuDescriptorHandleNonShaderVisible);
|
||||
break;
|
||||
}
|
||||
|
||||
this.d3D12Resource.Get()->SetName(this);
|
||||
}
|
||||
}
|
||||
|
||||
this.d3D12Resource.Get()->SetName(this);
|
||||
/// <summary>
|
||||
/// Releases unmanaged resources for the current <see cref="Buffer{T}"/> instance.
|
||||
/// </summary>
|
||||
~Buffer()
|
||||
{
|
||||
this.referenceTracker.Dispose();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -201,7 +219,28 @@ public unsafe abstract class Buffer<T> : NativeObject, IGraphicsResource
|
||||
internal abstract void CopyFrom(ref T source, int destinationOffset, int count);
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void OnDispose()
|
||||
public void Dispose()
|
||||
{
|
||||
this.referenceTracker.Dispose();
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ReferenceTracker.ITrackedObject.GetReferenceTracker"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal ref ReferenceTracker GetReferenceTracker()
|
||||
{
|
||||
return ref this.referenceTracker;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
ref ReferenceTracker ReferenceTracker.ITrackedObject.GetReferenceTracker()
|
||||
{
|
||||
return ref this.referenceTracker;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ReferenceTracker.ITrackedObject.DangerousRelease()
|
||||
{
|
||||
this.d3D12Resource.Dispose();
|
||||
#if NET6_0_OR_GREATER
|
||||
@@ -231,20 +270,24 @@ public unsafe abstract class Buffer<T> : NativeObject, IGraphicsResource
|
||||
/// <inheritdoc cref="__Internals.GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice, out bool)"/>
|
||||
internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
ThrowIfDeviceMismatch(device);
|
||||
using (this.referenceTracker.GetLease())
|
||||
{
|
||||
ThrowIfDeviceMismatch(device);
|
||||
|
||||
return (
|
||||
this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12GpuDescriptorHandle,
|
||||
this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12CpuDescriptorHandleNonShaderVisible);
|
||||
return (
|
||||
this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12GpuDescriptorHandle,
|
||||
this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12CpuDescriptorHandleNonShaderVisible);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="__Internals.GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice)"/>
|
||||
internal unsafe ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
ThrowIfDeviceMismatch(device);
|
||||
using (this.referenceTracker.GetLease())
|
||||
{
|
||||
ThrowIfDeviceMismatch(device);
|
||||
|
||||
return D3D12Resource;
|
||||
return D3D12Resource;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -32,71 +32,72 @@ public abstract class StructuredBuffer<T> : Buffer<T>
|
||||
/// <inheritdoc/>
|
||||
internal override unsafe void CopyTo(ref T destination, int sourceOffset, int count)
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
ThrowIfDisposed();
|
||||
|
||||
Guard.IsBetweenOrEqualTo(count, 0, Length);
|
||||
Guard.IsInRange(sourceOffset, 0, Length);
|
||||
Guard.IsLessThanOrEqualTo(sourceOffset + count, Length, nameof(sourceOffset));
|
||||
|
||||
if (GraphicsDevice.IsCacheCoherentUMA)
|
||||
using (GraphicsDevice.GetReferenceTracker().GetLease())
|
||||
using (GetReferenceTracker().GetLease())
|
||||
{
|
||||
using ID3D12ResourceMap resource = D3D12Resource->Map();
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
fixed (void* destinationPointer = &destination)
|
||||
Guard.IsBetweenOrEqualTo(count, 0, Length);
|
||||
Guard.IsInRange(sourceOffset, 0, Length);
|
||||
Guard.IsLessThanOrEqualTo(sourceOffset + count, Length, nameof(sourceOffset));
|
||||
|
||||
if (GraphicsDevice.IsCacheCoherentUMA)
|
||||
{
|
||||
MemoryHelper.Copy<T>(
|
||||
source: resource.Pointer,
|
||||
destination: destinationPointer,
|
||||
sourceElementOffset: (uint)sourceOffset,
|
||||
destinationElementOffset: 0,
|
||||
sourceElementPitchInBytes: (uint)sizeof(T),
|
||||
destinationElementPitchInBytes: (uint)sizeof(T),
|
||||
count: (uint)count);
|
||||
using ID3D12ResourceMap resource = D3D12Resource->Map();
|
||||
|
||||
fixed (void* destinationPointer = &destination)
|
||||
{
|
||||
MemoryHelper.Copy<T>(
|
||||
source: resource.Pointer,
|
||||
destination: destinationPointer,
|
||||
sourceElementOffset: (uint)sourceOffset,
|
||||
destinationElementOffset: 0,
|
||||
sourceElementPitchInBytes: (uint)sizeof(T),
|
||||
destinationElementPitchInBytes: (uint)sizeof(T),
|
||||
count: (uint)count);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
nint byteOffset = (nint)sourceOffset * sizeof(T);
|
||||
nint byteLength = count * sizeof(T);
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
using ComPtr<D3D12MA_Allocation> allocation = GraphicsDevice.Allocator->CreateResource(null, ResourceType.ReadBack, AllocationMode.Default, (ulong)byteLength);
|
||||
#else
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
|
||||
ResourceType.ReadBack,
|
||||
(ulong)byteLength,
|
||||
GraphicsDevice.IsCacheCoherentUMA);
|
||||
#endif
|
||||
|
||||
using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY))
|
||||
else
|
||||
{
|
||||
#if NET6_0_OR_GREATER
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(allocation.Get()->GetResource(), 0, D3D12Resource, (ulong)byteOffset, (ulong)byteLength);
|
||||
#else
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(d3D12Resource.Get(), 0, D3D12Resource, (ulong)byteOffset, (ulong)byteLength);
|
||||
#endif
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
nint byteOffset = (nint)sourceOffset * sizeof(T);
|
||||
nint byteLength = count * sizeof(T);
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
using ID3D12ResourceMap resource = allocation.Get()->GetResource()->Map();
|
||||
using ComPtr<D3D12MA_Allocation> allocation = GraphicsDevice.Allocator->CreateResource(null, ResourceType.ReadBack, AllocationMode.Default, (ulong)byteLength);
|
||||
#else
|
||||
using ID3D12ResourceMap resource = d3D12Resource.Get()->Map();
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
|
||||
ResourceType.ReadBack,
|
||||
(ulong)byteLength,
|
||||
GraphicsDevice.IsCacheCoherentUMA);
|
||||
#endif
|
||||
|
||||
fixed (void* destinationPointer = &destination)
|
||||
{
|
||||
MemoryHelper.Copy<T>(
|
||||
source: resource.Pointer,
|
||||
destination: destinationPointer,
|
||||
sourceElementOffset: 0,
|
||||
destinationElementOffset: 0,
|
||||
sourceElementPitchInBytes: (uint)sizeof(T),
|
||||
destinationElementPitchInBytes: (uint)sizeof(T),
|
||||
count: (uint)count);
|
||||
using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY))
|
||||
{
|
||||
#if NET6_0_OR_GREATER
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(allocation.Get()->GetResource(), 0, D3D12Resource, (ulong)byteOffset, (ulong)byteLength);
|
||||
#else
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(d3D12Resource.Get(), 0, D3D12Resource, (ulong)byteOffset, (ulong)byteLength);
|
||||
#endif
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
using ID3D12ResourceMap resource = allocation.Get()->GetResource()->Map();
|
||||
#else
|
||||
using ID3D12ResourceMap resource = d3D12Resource.Get()->Map();
|
||||
#endif
|
||||
|
||||
fixed (void* destinationPointer = &destination)
|
||||
{
|
||||
MemoryHelper.Copy<T>(
|
||||
source: resource.Pointer,
|
||||
destination: destinationPointer,
|
||||
sourceElementOffset: 0,
|
||||
destinationElementOffset: 0,
|
||||
sourceElementPitchInBytes: (uint)sizeof(T),
|
||||
destinationElementPitchInBytes: (uint)sizeof(T),
|
||||
count: (uint)count);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -104,40 +105,41 @@ public abstract class StructuredBuffer<T> : Buffer<T>
|
||||
/// <inheritdoc/>
|
||||
internal override unsafe void CopyTo(Buffer<T> destination, int sourceOffset, int destinationOffset, int count)
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
ThrowIfDisposed();
|
||||
|
||||
destination.ThrowIfDeviceMismatch(GraphicsDevice);
|
||||
destination.ThrowIfDisposed();
|
||||
|
||||
Guard.IsBetweenOrEqualTo(count, 0, Length);
|
||||
Guard.IsBetweenOrEqualTo(count, 0, destination.Length);
|
||||
Guard.IsInRange(sourceOffset, 0, Length);
|
||||
Guard.IsLessThanOrEqualTo(sourceOffset + count, Length, nameof(sourceOffset));
|
||||
Guard.IsInRange(destinationOffset, 0, destination.Length);
|
||||
Guard.IsLessThanOrEqualTo(destinationOffset + count, destination.Length, nameof(destinationOffset));
|
||||
|
||||
if (!destination.IsPaddingPresent)
|
||||
using (GraphicsDevice.GetReferenceTracker().GetLease())
|
||||
using (GetReferenceTracker().GetLease())
|
||||
using (destination.GetReferenceTracker().GetLease())
|
||||
{
|
||||
nint sourceByteOffset = (nint)sourceOffset * sizeof(T);
|
||||
nint destinationByteOffset = (nint)destinationOffset * sizeof(T);
|
||||
nint byteLength = count * sizeof(T);
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
// Directly copy the input buffer
|
||||
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
|
||||
destination.ThrowIfDeviceMismatch(GraphicsDevice);
|
||||
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(
|
||||
destination.D3D12Resource,
|
||||
(ulong)destinationByteOffset,
|
||||
D3D12Resource,
|
||||
(ulong)sourceByteOffset,
|
||||
(ulong)byteLength);
|
||||
Guard.IsBetweenOrEqualTo(count, 0, Length);
|
||||
Guard.IsBetweenOrEqualTo(count, 0, destination.Length);
|
||||
Guard.IsInRange(sourceOffset, 0, Length);
|
||||
Guard.IsLessThanOrEqualTo(sourceOffset + count, Length, nameof(sourceOffset));
|
||||
Guard.IsInRange(destinationOffset, 0, destination.Length);
|
||||
Guard.IsLessThanOrEqualTo(destinationOffset + count, destination.Length, nameof(destinationOffset));
|
||||
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
if (!destination.IsPaddingPresent)
|
||||
{
|
||||
nint sourceByteOffset = (nint)sourceOffset * sizeof(T);
|
||||
nint destinationByteOffset = (nint)destinationOffset * sizeof(T);
|
||||
nint byteLength = count * sizeof(T);
|
||||
|
||||
// Directly copy the input buffer
|
||||
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
|
||||
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(
|
||||
destination.D3D12Resource,
|
||||
(ulong)destinationByteOffset,
|
||||
D3D12Resource,
|
||||
(ulong)sourceByteOffset,
|
||||
(ulong)byteLength);
|
||||
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
else CopyToWithCpuBuffer(destination, sourceOffset, destinationOffset, count);
|
||||
}
|
||||
else CopyToWithCpuBuffer(destination, sourceOffset, destinationOffset, count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -149,114 +151,116 @@ public abstract class StructuredBuffer<T> : Buffer<T>
|
||||
/// <param name="count">The number of items to read.</param>
|
||||
internal unsafe void CopyTo(ReadBackBuffer<T> destination, int sourceOffset, int destinationOffset, int count)
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
ThrowIfDisposed();
|
||||
|
||||
destination.ThrowIfDeviceMismatch(GraphicsDevice);
|
||||
destination.ThrowIfDisposed();
|
||||
|
||||
Guard.IsBetweenOrEqualTo(count, 0, Length);
|
||||
Guard.IsBetweenOrEqualTo(count, 0, destination.Length);
|
||||
Guard.IsInRange(sourceOffset, 0, Length);
|
||||
Guard.IsLessThanOrEqualTo(sourceOffset + count, Length, nameof(sourceOffset));
|
||||
Guard.IsInRange(destinationOffset, 0, destination.Length);
|
||||
Guard.IsLessThanOrEqualTo(destinationOffset + count, destination.Length, nameof(destinationOffset));
|
||||
|
||||
if (GraphicsDevice.IsCacheCoherentUMA)
|
||||
using (GraphicsDevice.GetReferenceTracker().GetLease())
|
||||
using (GetReferenceTracker().GetLease())
|
||||
using (destination.GetReferenceTracker().GetLease())
|
||||
{
|
||||
using ID3D12ResourceMap resource = D3D12Resource->Map();
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
MemoryHelper.Copy<T>(
|
||||
source: resource.Pointer,
|
||||
destination: destination.MappedData,
|
||||
sourceElementOffset: (uint)sourceOffset,
|
||||
destinationElementOffset: (uint)destinationOffset,
|
||||
sourceElementPitchInBytes: (uint)sizeof(T),
|
||||
destinationElementPitchInBytes: (uint)sizeof(T),
|
||||
count: (uint)count);
|
||||
}
|
||||
else
|
||||
{
|
||||
ulong byteDestinationOffset = (uint)destinationOffset * (uint)sizeof(T);
|
||||
ulong byteOffset = (uint)sourceOffset * (uint)sizeof(T);
|
||||
ulong byteLength = (uint)count * (uint)sizeof(T);
|
||||
destination.ThrowIfDeviceMismatch(GraphicsDevice);
|
||||
|
||||
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
|
||||
Guard.IsBetweenOrEqualTo(count, 0, Length);
|
||||
Guard.IsBetweenOrEqualTo(count, 0, destination.Length);
|
||||
Guard.IsInRange(sourceOffset, 0, Length);
|
||||
Guard.IsLessThanOrEqualTo(sourceOffset + count, Length, nameof(sourceOffset));
|
||||
Guard.IsInRange(destinationOffset, 0, destination.Length);
|
||||
Guard.IsLessThanOrEqualTo(destinationOffset + count, destination.Length, nameof(destinationOffset));
|
||||
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(destination.D3D12Resource, byteDestinationOffset, D3D12Resource, byteOffset, byteLength);
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
if (GraphicsDevice.IsCacheCoherentUMA)
|
||||
{
|
||||
using ID3D12ResourceMap resource = D3D12Resource->Map();
|
||||
|
||||
MemoryHelper.Copy<T>(
|
||||
source: resource.Pointer,
|
||||
destination: destination.MappedData,
|
||||
sourceElementOffset: (uint)sourceOffset,
|
||||
destinationElementOffset: (uint)destinationOffset,
|
||||
sourceElementPitchInBytes: (uint)sizeof(T),
|
||||
destinationElementPitchInBytes: (uint)sizeof(T),
|
||||
count: (uint)count);
|
||||
}
|
||||
else
|
||||
{
|
||||
ulong byteDestinationOffset = (uint)destinationOffset * (uint)sizeof(T);
|
||||
ulong byteOffset = (uint)sourceOffset * (uint)sizeof(T);
|
||||
ulong byteLength = (uint)count * (uint)sizeof(T);
|
||||
|
||||
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
|
||||
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(destination.D3D12Resource, byteDestinationOffset, D3D12Resource, byteOffset, byteLength);
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
internal override unsafe void CopyFrom(ref T source, int offset, int length)
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
ThrowIfDisposed();
|
||||
|
||||
Guard.IsBetweenOrEqualTo(length, 0, Length);
|
||||
Guard.IsInRange(offset, 0, Length);
|
||||
Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset));
|
||||
|
||||
if (GraphicsDevice.IsCacheCoherentUMA)
|
||||
using (GraphicsDevice.GetReferenceTracker().GetLease())
|
||||
using (GetReferenceTracker().GetLease())
|
||||
{
|
||||
using ID3D12ResourceMap resource = D3D12Resource->Map();
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
fixed (void* sourcePointer = &source)
|
||||
Guard.IsBetweenOrEqualTo(length, 0, Length);
|
||||
Guard.IsInRange(offset, 0, Length);
|
||||
Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset));
|
||||
|
||||
if (GraphicsDevice.IsCacheCoherentUMA)
|
||||
{
|
||||
MemoryHelper.Copy<T>(
|
||||
source: sourcePointer,
|
||||
destination: resource.Pointer,
|
||||
sourceElementOffset: 0,
|
||||
destinationElementOffset: (uint)offset,
|
||||
sourceElementPitchInBytes: (uint)sizeof(T),
|
||||
destinationElementPitchInBytes: (uint)sizeof(T),
|
||||
count: (uint)length);
|
||||
using ID3D12ResourceMap resource = D3D12Resource->Map();
|
||||
|
||||
fixed (void* sourcePointer = &source)
|
||||
{
|
||||
MemoryHelper.Copy<T>(
|
||||
source: sourcePointer,
|
||||
destination: resource.Pointer,
|
||||
sourceElementOffset: 0,
|
||||
destinationElementOffset: (uint)offset,
|
||||
sourceElementPitchInBytes: (uint)sizeof(T),
|
||||
destinationElementPitchInBytes: (uint)sizeof(T),
|
||||
count: (uint)length);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
nint byteOffset = (nint)offset * sizeof(T);
|
||||
nint byteLength = length * sizeof(T);
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
using ComPtr<D3D12MA_Allocation> allocation = GraphicsDevice.Allocator->CreateResource(null, ResourceType.Upload, AllocationMode.Default, (ulong)byteLength);
|
||||
#else
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
|
||||
ResourceType.Upload,
|
||||
(ulong)byteLength,
|
||||
GraphicsDevice.IsCacheCoherentUMA);
|
||||
#endif
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
using (ID3D12ResourceMap resource = allocation.Get()->GetResource()->Map())
|
||||
#else
|
||||
using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
|
||||
#endif
|
||||
fixed (void* sourcePointer = &source)
|
||||
else
|
||||
{
|
||||
MemoryHelper.Copy<T>(
|
||||
source: sourcePointer,
|
||||
destination: resource.Pointer,
|
||||
sourceElementOffset: 0,
|
||||
destinationElementOffset: 0,
|
||||
sourceElementPitchInBytes: (uint)sizeof(T),
|
||||
destinationElementPitchInBytes: (uint)sizeof(T),
|
||||
count: (uint)length);
|
||||
}
|
||||
|
||||
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
|
||||
nint byteOffset = (nint)offset * sizeof(T);
|
||||
nint byteLength = length * sizeof(T);
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, (ulong)byteOffset, allocation.Get()->GetResource(), 0, (ulong)byteLength);
|
||||
using ComPtr<D3D12MA_Allocation> allocation = GraphicsDevice.Allocator->CreateResource(null, ResourceType.Upload, AllocationMode.Default, (ulong)byteLength);
|
||||
#else
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, (ulong)byteOffset, d3D12Resource.Get(), 0, (ulong)byteLength);
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
|
||||
ResourceType.Upload,
|
||||
(ulong)byteLength,
|
||||
GraphicsDevice.IsCacheCoherentUMA);
|
||||
#endif
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
using (ID3D12ResourceMap resource = allocation.Get()->GetResource()->Map())
|
||||
#else
|
||||
using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
|
||||
#endif
|
||||
fixed (void* sourcePointer = &source)
|
||||
{
|
||||
MemoryHelper.Copy<T>(
|
||||
source: sourcePointer,
|
||||
destination: resource.Pointer,
|
||||
sourceElementOffset: 0,
|
||||
destinationElementOffset: 0,
|
||||
sourceElementPitchInBytes: (uint)sizeof(T),
|
||||
destinationElementPitchInBytes: (uint)sizeof(T),
|
||||
count: (uint)length);
|
||||
}
|
||||
|
||||
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, (ulong)byteOffset, allocation.Get()->GetResource(), 0, (ulong)byteLength);
|
||||
#else
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, (ulong)byteOffset, d3D12Resource.Get(), 0, (ulong)byteLength);
|
||||
#endif
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -269,44 +273,45 @@ public abstract class StructuredBuffer<T> : Buffer<T>
|
||||
/// <param name="count">The number of items to read.</param>
|
||||
internal unsafe void CopyFrom(UploadBuffer<T> source, int sourceOffset, int destinationOffset, int count)
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
ThrowIfDisposed();
|
||||
|
||||
source.ThrowIfDeviceMismatch(GraphicsDevice);
|
||||
source.ThrowIfDisposed();
|
||||
|
||||
Guard.IsBetweenOrEqualTo(count, 0, Length);
|
||||
Guard.IsBetweenOrEqualTo(count, 0, source.Length);
|
||||
Guard.IsInRange(sourceOffset, 0, source.Length);
|
||||
Guard.IsLessThanOrEqualTo(sourceOffset + count, source.Length, nameof(sourceOffset));
|
||||
Guard.IsInRange(destinationOffset, 0, Length);
|
||||
Guard.IsLessThanOrEqualTo(destinationOffset + count, Length, nameof(destinationOffset));
|
||||
|
||||
if (GraphicsDevice.IsCacheCoherentUMA)
|
||||
using (GraphicsDevice.GetReferenceTracker().GetLease())
|
||||
using (GetReferenceTracker().GetLease())
|
||||
using (source.GetReferenceTracker().GetLease())
|
||||
{
|
||||
using ID3D12ResourceMap resource = D3D12Resource->Map();
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
MemoryHelper.Copy<T>(
|
||||
source: source.MappedData,
|
||||
destination: resource.Pointer,
|
||||
sourceElementOffset: (uint)sourceOffset,
|
||||
destinationElementOffset: (uint)destinationOffset,
|
||||
sourceElementPitchInBytes: (uint)sizeof(T),
|
||||
destinationElementPitchInBytes: (uint)sizeof(T),
|
||||
count: (uint)count);
|
||||
}
|
||||
else
|
||||
{
|
||||
ulong byteSourceOffset = (uint)sourceOffset * (uint)sizeof(T);
|
||||
ulong byteOffset = (uint)destinationOffset * (uint)sizeof(T);
|
||||
ulong byteLength = (uint)count * (uint)sizeof(T);
|
||||
source.ThrowIfDeviceMismatch(GraphicsDevice);
|
||||
|
||||
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
|
||||
Guard.IsBetweenOrEqualTo(count, 0, Length);
|
||||
Guard.IsBetweenOrEqualTo(count, 0, source.Length);
|
||||
Guard.IsInRange(sourceOffset, 0, source.Length);
|
||||
Guard.IsLessThanOrEqualTo(sourceOffset + count, source.Length, nameof(sourceOffset));
|
||||
Guard.IsInRange(destinationOffset, 0, Length);
|
||||
Guard.IsLessThanOrEqualTo(destinationOffset + count, Length, nameof(destinationOffset));
|
||||
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, byteOffset, source.D3D12Resource, byteSourceOffset, byteLength);
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
if (GraphicsDevice.IsCacheCoherentUMA)
|
||||
{
|
||||
using ID3D12ResourceMap resource = D3D12Resource->Map();
|
||||
|
||||
MemoryHelper.Copy<T>(
|
||||
source: source.MappedData,
|
||||
destination: resource.Pointer,
|
||||
sourceElementOffset: (uint)sourceOffset,
|
||||
destinationElementOffset: (uint)destinationOffset,
|
||||
sourceElementPitchInBytes: (uint)sizeof(T),
|
||||
destinationElementPitchInBytes: (uint)sizeof(T),
|
||||
count: (uint)count);
|
||||
}
|
||||
else
|
||||
{
|
||||
ulong byteSourceOffset = (uint)sourceOffset * (uint)sizeof(T);
|
||||
ulong byteOffset = (uint)destinationOffset * (uint)sizeof(T);
|
||||
ulong byteLength = (uint)count * (uint)sizeof(T);
|
||||
|
||||
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
|
||||
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, byteOffset, source.D3D12Resource, byteSourceOffset, byteLength);
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,9 +26,14 @@ namespace ComputeSharp.Resources;
|
||||
/// A <see langword="class"/> representing a typed 2D texture stored on GPU memory.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
|
||||
public unsafe abstract class Texture2D<T> : NativeObject, IGraphicsResource, GraphicsResourceHelper.IGraphicsResource
|
||||
public unsafe abstract class Texture2D<T> : ReferenceTracker.ITrackedObject, IDisposable, IGraphicsResource, GraphicsResourceHelper.IGraphicsResource
|
||||
where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// The owning <see cref="ReferenceTracker"/> object for the current instance.
|
||||
/// </summary>
|
||||
private ReferenceTracker referenceTracker;
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12MA_Allocation"/> instance used to retrieve <see cref="d3D12Resource"/>.
|
||||
@@ -72,67 +77,79 @@ public unsafe abstract class Texture2D<T> : NativeObject, IGraphicsResource, Gra
|
||||
/// <param name="d3D12FormatSupport">The format support for the current texture type.</param>
|
||||
private protected Texture2D(GraphicsDevice device, int width, int height, ResourceType resourceType, AllocationMode allocationMode, D3D12_FORMAT_SUPPORT1 d3D12FormatSupport)
|
||||
{
|
||||
device.ThrowIfDisposed();
|
||||
device.ThrowIfDeviceLost();
|
||||
this.referenceTracker = new ReferenceTracker(this);
|
||||
|
||||
Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION);
|
||||
|
||||
if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType<T>(), d3D12FormatSupport))
|
||||
using (device.GetReferenceTracker().GetLease())
|
||||
{
|
||||
UnsupportedTextureTypeException.ThrowForTexture2D<T>();
|
||||
}
|
||||
device.ThrowIfDeviceLost();
|
||||
|
||||
GraphicsDevice = device;
|
||||
Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION);
|
||||
|
||||
if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType<T>(), d3D12FormatSupport))
|
||||
{
|
||||
UnsupportedTextureTypeException.ThrowForTexture2D<T>();
|
||||
}
|
||||
|
||||
GraphicsDevice = device;
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
this.allocation = device.Allocator->CreateResource(
|
||||
device.Pool,
|
||||
resourceType,
|
||||
allocationMode,
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
out this.d3D12ResourceState);
|
||||
this.allocation = device.Allocator->CreateResource(
|
||||
device.Pool,
|
||||
resourceType,
|
||||
allocationMode,
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
out this.d3D12ResourceState);
|
||||
|
||||
this.d3D12Resource = new ComPtr<ID3D12Resource>(this.allocation.Get()->GetResource());
|
||||
this.d3D12Resource = new ComPtr<ID3D12Resource>(this.allocation.Get()->GetResource());
|
||||
#else
|
||||
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(
|
||||
resourceType,
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
device.IsCacheCoherentUMA,
|
||||
out this.d3D12ResourceState);
|
||||
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(
|
||||
resourceType,
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
device.IsCacheCoherentUMA,
|
||||
out this.d3D12ResourceState);
|
||||
#endif
|
||||
|
||||
this.d3D12CommandListType = this.d3D12ResourceState == D3D12_RESOURCE_STATE_COMMON
|
||||
? D3D12_COMMAND_LIST_TYPE_COPY
|
||||
: D3D12_COMMAND_LIST_TYPE_COMPUTE;
|
||||
this.d3D12CommandListType = this.d3D12ResourceState == D3D12_RESOURCE_STATE_COMMON
|
||||
? D3D12_COMMAND_LIST_TYPE_COPY
|
||||
: D3D12_COMMAND_LIST_TYPE_COMPUTE;
|
||||
|
||||
device.D3D12Device->GetCopyableFootprint(
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
out this.d3D12PlacedSubresourceFootprint,
|
||||
out _,
|
||||
out _);
|
||||
device.D3D12Device->GetCopyableFootprint(
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
out this.d3D12PlacedSubresourceFootprint,
|
||||
out _,
|
||||
out _);
|
||||
|
||||
device.RegisterAllocatedResource();
|
||||
device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles);
|
||||
device.RegisterAllocatedResource();
|
||||
device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles);
|
||||
|
||||
switch (resourceType)
|
||||
{
|
||||
case ResourceType.ReadOnly:
|
||||
device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType<T>(), D3D12_SRV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
|
||||
break;
|
||||
case ResourceType.ReadWrite:
|
||||
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType<T>(), D3D12_UAV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
|
||||
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType<T>(), D3D12_UAV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible);
|
||||
break;
|
||||
switch (resourceType)
|
||||
{
|
||||
case ResourceType.ReadOnly:
|
||||
device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType<T>(), D3D12_SRV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
|
||||
break;
|
||||
case ResourceType.ReadWrite:
|
||||
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType<T>(), D3D12_UAV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
|
||||
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType<T>(), D3D12_UAV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible);
|
||||
break;
|
||||
}
|
||||
|
||||
this.d3D12Resource.Get()->SetName(this);
|
||||
}
|
||||
}
|
||||
|
||||
this.d3D12Resource.Get()->SetName(this);
|
||||
/// <summary>
|
||||
/// Releases unmanaged resources for the current <see cref="Texture2D{T}"/> instance.
|
||||
/// </summary>
|
||||
~Texture2D()
|
||||
{
|
||||
this.referenceTracker.Dispose();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -169,20 +186,20 @@ public unsafe abstract class Texture2D<T> : NativeObject, IGraphicsResource, Gra
|
||||
/// <param name="height">The height of the memory area to copy.</param>
|
||||
internal void CopyTo(ref T destination, int size, int sourceOffsetX, int sourceOffsetY, int width, int height)
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
using (GraphicsDevice.GetReferenceTracker().GetLease())
|
||||
using (this.referenceTracker.GetLease())
|
||||
{
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
ThrowIfDisposed();
|
||||
Guard.IsInRange(sourceOffsetX, 0, Width);
|
||||
Guard.IsInRange(sourceOffsetY, 0, Height);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, Height);
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY));
|
||||
Guard.IsGreaterThanOrEqualTo(size, (nint)width * height);
|
||||
|
||||
Guard.IsInRange(sourceOffsetX, 0, Width);
|
||||
Guard.IsInRange(sourceOffsetY, 0, Height);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, Height);
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY));
|
||||
Guard.IsGreaterThanOrEqualTo(size, (nint)width * height);
|
||||
|
||||
GraphicsDevice.D3D12Device->GetCopyableFootprint(
|
||||
GraphicsDevice.D3D12Device->GetCopyableFootprint(
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
@@ -191,59 +208,60 @@ public unsafe abstract class Texture2D<T> : NativeObject, IGraphicsResource, Gra
|
||||
out ulong totalSizeInBytes);
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
using ComPtr<D3D12MA_Allocation> allocation = GraphicsDevice.Allocator->CreateResource(
|
||||
GraphicsDevice.Pool,
|
||||
ResourceType.ReadBack,
|
||||
AllocationMode.Default,
|
||||
totalSizeInBytes);
|
||||
using ComPtr<D3D12MA_Allocation> allocation = GraphicsDevice.Allocator->CreateResource(
|
||||
GraphicsDevice.Pool,
|
||||
ResourceType.ReadBack,
|
||||
AllocationMode.Default,
|
||||
totalSizeInBytes);
|
||||
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = new(allocation.Get()->GetResource());
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = new(allocation.Get()->GetResource());
|
||||
#else
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
|
||||
ResourceType.ReadBack,
|
||||
totalSizeInBytes,
|
||||
GraphicsDevice.IsCacheCoherentUMA);
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
|
||||
ResourceType.ReadBack,
|
||||
totalSizeInBytes,
|
||||
GraphicsDevice.IsCacheCoherentUMA);
|
||||
#endif
|
||||
|
||||
using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType))
|
||||
{
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType))
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
|
||||
}
|
||||
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
|
||||
d3D12ResourceDestination: d3D12Resource.Get(),
|
||||
&d3D12PlacedSubresourceFootprintDestination,
|
||||
destinationX: 0,
|
||||
destinationY: 0,
|
||||
destinationZ: 0,
|
||||
d3D12ResourceSource: D3D12Resource,
|
||||
sourceX: (uint)sourceOffsetX,
|
||||
sourceY: (uint)sourceOffsetY,
|
||||
sourceZ: 0,
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
depth: 1);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
|
||||
}
|
||||
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
|
||||
d3D12ResourceDestination: d3D12Resource.Get(),
|
||||
&d3D12PlacedSubresourceFootprintDestination,
|
||||
destinationX: 0,
|
||||
destinationY: 0,
|
||||
destinationZ: 0,
|
||||
d3D12ResourceSource: D3D12Resource,
|
||||
sourceX: (uint)sourceOffsetX,
|
||||
sourceY: (uint)sourceOffsetY,
|
||||
sourceZ: 0,
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
depth: 1);
|
||||
using ID3D12ResourceMap resource = d3D12Resource.Get()->Map();
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
fixed (void* destinationPointer = &destination)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
|
||||
MemoryHelper.Copy(
|
||||
resource.Pointer,
|
||||
(uint)height,
|
||||
rowSizeInBytes,
|
||||
d3D12PlacedSubresourceFootprintDestination.Footprint.RowPitch,
|
||||
destinationPointer);
|
||||
}
|
||||
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
|
||||
using ID3D12ResourceMap resource = d3D12Resource.Get()->Map();
|
||||
|
||||
fixed (void* destinationPointer = &destination)
|
||||
{
|
||||
MemoryHelper.Copy(
|
||||
resource.Pointer,
|
||||
(uint)height,
|
||||
rowSizeInBytes,
|
||||
d3D12PlacedSubresourceFootprintDestination.Footprint.RowPitch,
|
||||
destinationPointer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -259,61 +277,62 @@ public unsafe abstract class Texture2D<T> : NativeObject, IGraphicsResource, Gra
|
||||
/// <param name="height">The height of the memory area to copy.</param>
|
||||
internal void CopyTo(Texture2D<T> destination, int sourceOffsetX, int sourceOffsetY, int destinationOffsetX, int destinationOffsetY, int width, int height)
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
ThrowIfDisposed();
|
||||
|
||||
destination.ThrowIfDeviceMismatch(GraphicsDevice);
|
||||
destination.ThrowIfDisposed();
|
||||
|
||||
Guard.IsInRange(sourceOffsetX, 0, Width);
|
||||
Guard.IsInRange(sourceOffsetY, 0, Height);
|
||||
Guard.IsInRange(destinationOffsetX, 0, destination.Width);
|
||||
Guard.IsInRange(destinationOffsetY, 0, destination.Height);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, Height);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, destination.Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, destination.Height);
|
||||
Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX));
|
||||
Guard.IsBetweenOrEqualTo(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY));
|
||||
|
||||
D3D12_COMMAND_LIST_TYPE d3D12CommandListType =
|
||||
this.d3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE ||
|
||||
destination.d3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE
|
||||
? D3D12_COMMAND_LIST_TYPE_COMPUTE
|
||||
: D3D12_COMMAND_LIST_TYPE_COPY;
|
||||
|
||||
using CommandList copyCommandList = new(GraphicsDevice, d3D12CommandListType);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
using (GraphicsDevice.GetReferenceTracker().GetLease())
|
||||
using (this.referenceTracker.GetLease())
|
||||
using (destination.referenceTracker.GetLease())
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(destination.D3D12Resource, destination.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
destination.ThrowIfDeviceMismatch(GraphicsDevice);
|
||||
|
||||
Guard.IsInRange(sourceOffsetX, 0, Width);
|
||||
Guard.IsInRange(sourceOffsetY, 0, Height);
|
||||
Guard.IsInRange(destinationOffsetX, 0, destination.Width);
|
||||
Guard.IsInRange(destinationOffsetY, 0, destination.Height);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, Height);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, destination.Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, destination.Height);
|
||||
Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX));
|
||||
Guard.IsBetweenOrEqualTo(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY));
|
||||
|
||||
D3D12_COMMAND_LIST_TYPE d3D12CommandListType =
|
||||
this.d3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE ||
|
||||
destination.d3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE
|
||||
? D3D12_COMMAND_LIST_TYPE_COMPUTE
|
||||
: D3D12_COMMAND_LIST_TYPE_COPY;
|
||||
|
||||
using CommandList copyCommandList = new(GraphicsDevice, d3D12CommandListType);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(destination.D3D12Resource, destination.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
|
||||
}
|
||||
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
|
||||
d3D12ResourceDestination: destination.D3D12Resource,
|
||||
(uint)destinationOffsetX,
|
||||
(uint)destinationOffsetY,
|
||||
destinationZ: 0,
|
||||
d3D12ResourceSource: D3D12Resource,
|
||||
(uint)sourceOffsetX,
|
||||
(uint)sourceOffsetY,
|
||||
sourceZ: 0,
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
1);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(destination.D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, destination.d3D12ResourceState);
|
||||
}
|
||||
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
|
||||
d3D12ResourceDestination: destination.D3D12Resource,
|
||||
(uint)destinationOffsetX,
|
||||
(uint)destinationOffsetY,
|
||||
destinationZ: 0,
|
||||
d3D12ResourceSource: D3D12Resource,
|
||||
(uint)sourceOffsetX,
|
||||
(uint)sourceOffsetY,
|
||||
sourceZ: 0,
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
1);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(destination.D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, destination.d3D12ResourceState);
|
||||
}
|
||||
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -328,57 +347,58 @@ public unsafe abstract class Texture2D<T> : NativeObject, IGraphicsResource, Gra
|
||||
/// <param name="height">The height of the memory area to copy.</param>
|
||||
internal void CopyTo(ReadBackTexture2D<T> destination, int sourceOffsetX, int sourceOffsetY, int destinationOffsetX, int destinationOffsetY, int width, int height)
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
ThrowIfDisposed();
|
||||
|
||||
destination.ThrowIfDeviceMismatch(GraphicsDevice);
|
||||
destination.ThrowIfDisposed();
|
||||
|
||||
Guard.IsInRange(sourceOffsetX, 0, Width);
|
||||
Guard.IsInRange(sourceOffsetY, 0, Height);
|
||||
Guard.IsInRange(destinationOffsetX, 0, destination.Width);
|
||||
Guard.IsInRange(destinationOffsetY, 0, destination.Height);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, Height);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, destination.Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, destination.Height);
|
||||
Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX));
|
||||
Guard.IsBetweenOrEqualTo(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY));
|
||||
|
||||
using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
using (GraphicsDevice.GetReferenceTracker().GetLease())
|
||||
using (this.referenceTracker.GetLease())
|
||||
using (destination.GetReferenceTracker().GetLease())
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
|
||||
}
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintDestination = &destination.D3D12PlacedSubresourceFootprint)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
|
||||
d3D12ResourceDestination: destination.D3D12Resource,
|
||||
d3D12PlacedSubresourceFootprintDestination,
|
||||
(uint)destinationOffsetX,
|
||||
(uint)destinationOffsetY,
|
||||
destinationZ: 0,
|
||||
d3D12ResourceSource: D3D12Resource,
|
||||
(uint)sourceOffsetX,
|
||||
(uint)sourceOffsetY,
|
||||
sourceZ: 0,
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
1);
|
||||
}
|
||||
destination.ThrowIfDeviceMismatch(GraphicsDevice);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
|
||||
}
|
||||
Guard.IsInRange(sourceOffsetX, 0, Width);
|
||||
Guard.IsInRange(sourceOffsetY, 0, Height);
|
||||
Guard.IsInRange(destinationOffsetX, 0, destination.Width);
|
||||
Guard.IsInRange(destinationOffsetY, 0, destination.Height);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, Height);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, destination.Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, destination.Height);
|
||||
Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX));
|
||||
Guard.IsBetweenOrEqualTo(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY));
|
||||
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
|
||||
}
|
||||
|
||||
fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintDestination = &destination.D3D12PlacedSubresourceFootprint)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
|
||||
d3D12ResourceDestination: destination.D3D12Resource,
|
||||
d3D12PlacedSubresourceFootprintDestination,
|
||||
(uint)destinationOffsetX,
|
||||
(uint)destinationOffsetY,
|
||||
destinationZ: 0,
|
||||
d3D12ResourceSource: D3D12Resource,
|
||||
(uint)sourceOffsetX,
|
||||
(uint)sourceOffsetY,
|
||||
sourceZ: 0,
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
1);
|
||||
}
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
|
||||
}
|
||||
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -392,80 +412,81 @@ public unsafe abstract class Texture2D<T> : NativeObject, IGraphicsResource, Gra
|
||||
/// <param name="height">The height of the memory area to write to.</param>
|
||||
internal void CopyFrom(ref T source, int size, int destinationOffsetX, int destinationOffsetY, int width, int height)
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
using (GraphicsDevice.GetReferenceTracker().GetLease())
|
||||
using (this.referenceTracker.GetLease())
|
||||
{
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
ThrowIfDisposed();
|
||||
Guard.IsInRange(destinationOffsetX, 0, Width);
|
||||
Guard.IsInRange(destinationOffsetY, 0, Height);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, Height);
|
||||
Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX));
|
||||
Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY));
|
||||
Guard.IsGreaterThanOrEqualTo(size, (nint)width * height);
|
||||
|
||||
Guard.IsInRange(destinationOffsetX, 0, Width);
|
||||
Guard.IsInRange(destinationOffsetY, 0, Height);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, Height);
|
||||
Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX));
|
||||
Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY));
|
||||
Guard.IsGreaterThanOrEqualTo(size, (nint)width * height);
|
||||
|
||||
GraphicsDevice.D3D12Device->GetCopyableFootprint(
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintSource,
|
||||
out ulong rowSizeInBytes,
|
||||
out ulong totalSizeInBytes);
|
||||
GraphicsDevice.D3D12Device->GetCopyableFootprint(
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintSource,
|
||||
out ulong rowSizeInBytes,
|
||||
out ulong totalSizeInBytes);
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
using ComPtr<D3D12MA_Allocation> allocation = GraphicsDevice.Allocator->CreateResource(
|
||||
GraphicsDevice.Pool,
|
||||
ResourceType.Upload,
|
||||
AllocationMode.Default,
|
||||
totalSizeInBytes);
|
||||
using ComPtr<D3D12MA_Allocation> allocation = GraphicsDevice.Allocator->CreateResource(
|
||||
GraphicsDevice.Pool,
|
||||
ResourceType.Upload,
|
||||
AllocationMode.Default,
|
||||
totalSizeInBytes);
|
||||
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = new(allocation.Get()->GetResource());
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = new(allocation.Get()->GetResource());
|
||||
#else
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
|
||||
ResourceType.Upload,
|
||||
totalSizeInBytes,
|
||||
GraphicsDevice.IsCacheCoherentUMA);
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
|
||||
ResourceType.Upload,
|
||||
totalSizeInBytes,
|
||||
GraphicsDevice.IsCacheCoherentUMA);
|
||||
#endif
|
||||
|
||||
using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
|
||||
fixed (void* sourcePointer = &source)
|
||||
{
|
||||
MemoryHelper.Copy(
|
||||
sourcePointer,
|
||||
resource.Pointer,
|
||||
using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
|
||||
fixed (void* sourcePointer = &source)
|
||||
{
|
||||
MemoryHelper.Copy(
|
||||
sourcePointer,
|
||||
resource.Pointer,
|
||||
(uint)height,
|
||||
rowSizeInBytes,
|
||||
d3D12PlacedSubresourceFootprintSource.Footprint.RowPitch);
|
||||
}
|
||||
|
||||
using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
|
||||
}
|
||||
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
|
||||
d3D12ResourceDestination: D3D12Resource,
|
||||
destinationX: (uint)destinationOffsetX,
|
||||
destinationY: (uint)destinationOffsetY,
|
||||
destinationZ: 0,
|
||||
d3D12ResourceSource: d3D12Resource.Get(),
|
||||
&d3D12PlacedSubresourceFootprintSource,
|
||||
sourceX: 0,
|
||||
sourceY: 0,
|
||||
sourceZ: 0,
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
rowSizeInBytes,
|
||||
d3D12PlacedSubresourceFootprintSource.Footprint.RowPitch);
|
||||
depth: 1);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState);
|
||||
}
|
||||
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
|
||||
using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
|
||||
}
|
||||
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
|
||||
d3D12ResourceDestination: D3D12Resource,
|
||||
destinationX: (uint)destinationOffsetX,
|
||||
destinationY: (uint)destinationOffsetY,
|
||||
destinationZ: 0,
|
||||
d3D12ResourceSource: d3D12Resource.Get(),
|
||||
&d3D12PlacedSubresourceFootprintSource,
|
||||
sourceX: 0,
|
||||
sourceY: 0,
|
||||
sourceZ: 0,
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
depth: 1);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState);
|
||||
}
|
||||
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -480,61 +501,83 @@ public unsafe abstract class Texture2D<T> : NativeObject, IGraphicsResource, Gra
|
||||
/// <param name="height">The height of the memory area to write to.</param>
|
||||
internal void CopyFrom(UploadTexture2D<T> source, int sourceOffsetX, int sourceOffsetY, int destinationOffsetX, int destinationOffsetY, int width, int height)
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
ThrowIfDisposed();
|
||||
|
||||
source.ThrowIfDeviceMismatch(GraphicsDevice);
|
||||
source.ThrowIfDisposed();
|
||||
|
||||
Guard.IsInRange(sourceOffsetX, 0, source.Width);
|
||||
Guard.IsInRange(sourceOffsetY, 0, source.Height);
|
||||
Guard.IsInRange(destinationOffsetX, 0, Width);
|
||||
Guard.IsInRange(destinationOffsetY, 0, Height);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, Height);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, source.Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, source.Height);
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetX + width, source.Width, nameof(sourceOffsetX));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetY + height, source.Height, nameof(sourceOffsetY));
|
||||
Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX));
|
||||
Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY));
|
||||
|
||||
using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
using (GraphicsDevice.GetReferenceTracker().GetLease())
|
||||
using (this.referenceTracker.GetLease())
|
||||
using (source.GetReferenceTracker().GetLease())
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
|
||||
}
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintSource = &source.D3D12PlacedSubresourceFootprint)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
|
||||
d3D12ResourceDestination: D3D12Resource,
|
||||
(uint)destinationOffsetX,
|
||||
(uint)destinationOffsetY,
|
||||
destinationZ: 0,
|
||||
d3D12ResourceSource: source.D3D12Resource,
|
||||
d3D12PlacedSubresourceFootprintSource,
|
||||
(uint)sourceOffsetX,
|
||||
(uint)sourceOffsetY,
|
||||
sourceZ: 0,
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
depth: 1);
|
||||
}
|
||||
source.ThrowIfDeviceMismatch(GraphicsDevice);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState);
|
||||
}
|
||||
Guard.IsInRange(sourceOffsetX, 0, source.Width);
|
||||
Guard.IsInRange(sourceOffsetY, 0, source.Height);
|
||||
Guard.IsInRange(destinationOffsetX, 0, Width);
|
||||
Guard.IsInRange(destinationOffsetY, 0, Height);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, Height);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, source.Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, source.Height);
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetX + width, source.Width, nameof(sourceOffsetX));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetY + height, source.Height, nameof(sourceOffsetY));
|
||||
Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX));
|
||||
Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY));
|
||||
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
|
||||
}
|
||||
|
||||
fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintSource = &source.D3D12PlacedSubresourceFootprint)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
|
||||
d3D12ResourceDestination: D3D12Resource,
|
||||
(uint)destinationOffsetX,
|
||||
(uint)destinationOffsetY,
|
||||
destinationZ: 0,
|
||||
d3D12ResourceSource: source.D3D12Resource,
|
||||
d3D12PlacedSubresourceFootprintSource,
|
||||
(uint)sourceOffsetX,
|
||||
(uint)sourceOffsetY,
|
||||
sourceZ: 0,
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
depth: 1);
|
||||
}
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState);
|
||||
}
|
||||
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void OnDispose()
|
||||
public virtual void Dispose()
|
||||
{
|
||||
this.referenceTracker.Dispose();
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ReferenceTracker.ITrackedObject.GetReferenceTracker"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal ref ReferenceTracker GetReferenceTracker()
|
||||
{
|
||||
return ref this.referenceTracker;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
ref ReferenceTracker ReferenceTracker.ITrackedObject.GetReferenceTracker()
|
||||
{
|
||||
return ref this.referenceTracker;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ReferenceTracker.ITrackedObject.DangerousRelease()
|
||||
{
|
||||
this.d3D12Resource.Dispose();
|
||||
#if NET6_0_OR_GREATER
|
||||
@@ -582,46 +625,54 @@ public unsafe abstract class Texture2D<T> : NativeObject, IGraphicsResource, Gra
|
||||
/// <inheritdoc cref="GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice, out bool)"/>
|
||||
internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
ThrowIfDeviceMismatch(device);
|
||||
using (this.referenceTracker.GetLease())
|
||||
{
|
||||
ThrowIfDeviceMismatch(device);
|
||||
|
||||
isNormalized = DXGIFormatHelper.IsNormalizedType<T>();
|
||||
isNormalized = DXGIFormatHelper.IsNormalizedType<T>();
|
||||
|
||||
return (this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible);
|
||||
return (this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice)"/>
|
||||
internal ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
ThrowIfDeviceMismatch(device);
|
||||
using (this.referenceTracker.GetLease())
|
||||
{
|
||||
ThrowIfDeviceMismatch(device);
|
||||
|
||||
return D3D12Resource;
|
||||
return D3D12Resource;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice, ResourceState, out ID3D12Resource*)"/>
|
||||
internal (D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
ThrowIfDeviceMismatch(device);
|
||||
using (this.referenceTracker.GetLease())
|
||||
{
|
||||
ThrowIfDeviceMismatch(device);
|
||||
|
||||
D3D12_RESOURCE_STATES d3D12ResourceStatesBefore = this.d3D12ResourceState;
|
||||
D3D12_RESOURCE_STATES d3D12ResourceStatesAfter = ResourceStateHelper.GetD3D12ResourceStates(resourceState);
|
||||
D3D12_RESOURCE_STATES d3D12ResourceStatesBefore = this.d3D12ResourceState;
|
||||
D3D12_RESOURCE_STATES d3D12ResourceStatesAfter = ResourceStateHelper.GetD3D12ResourceStates(resourceState);
|
||||
|
||||
this.d3D12ResourceState = d3D12ResourceStatesAfter;
|
||||
this.d3D12ResourceState = d3D12ResourceStatesAfter;
|
||||
|
||||
d3D12Resource = D3D12Resource;
|
||||
d3D12Resource = D3D12Resource;
|
||||
|
||||
return (d3D12ResourceStatesBefore, d3D12ResourceStatesAfter);
|
||||
return (d3D12ResourceStatesBefore, d3D12ResourceStatesAfter);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
ThrowIfDeviceMismatch(device);
|
||||
using (this.referenceTracker.GetLease())
|
||||
{
|
||||
ThrowIfDeviceMismatch(device);
|
||||
|
||||
return D3D12GpuDescriptorHandle;
|
||||
return D3D12GpuDescriptorHandle;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
||||
@@ -26,9 +26,14 @@ namespace ComputeSharp.Resources;
|
||||
/// A <see langword="class"/> representing a typed 3D texture stored on GPU memory.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
|
||||
public unsafe abstract class Texture3D<T> : NativeObject, IGraphicsResource, GraphicsResourceHelper.IGraphicsResource
|
||||
public unsafe abstract class Texture3D<T> : ReferenceTracker.ITrackedObject, IDisposable, IGraphicsResource, GraphicsResourceHelper.IGraphicsResource
|
||||
where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// The owning <see cref="ReferenceTracker"/> object for the current instance.
|
||||
/// </summary>
|
||||
private ReferenceTracker referenceTracker;
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12MA_Allocation"/> instance used to retrieve <see cref="d3D12Resource"/>.
|
||||
@@ -73,71 +78,83 @@ public unsafe abstract class Texture3D<T> : NativeObject, IGraphicsResource, Gra
|
||||
/// <param name="d3D12FormatSupport">The format support for the current texture type.</param>
|
||||
private protected Texture3D(GraphicsDevice device, int width, int height, int depth, ResourceType resourceType, AllocationMode allocationMode, D3D12_FORMAT_SUPPORT1 d3D12FormatSupport)
|
||||
{
|
||||
device.ThrowIfDisposed();
|
||||
device.ThrowIfDeviceLost();
|
||||
this.referenceTracker = new ReferenceTracker(this);
|
||||
|
||||
Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION);
|
||||
Guard.IsBetweenOrEqualTo(depth, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION);
|
||||
|
||||
if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType<T>(), d3D12FormatSupport))
|
||||
using (device.GetReferenceTracker().GetLease())
|
||||
{
|
||||
UnsupportedTextureTypeException.ThrowForTexture3D<T>();
|
||||
}
|
||||
device.ThrowIfDeviceLost();
|
||||
|
||||
GraphicsDevice = device;
|
||||
Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION);
|
||||
Guard.IsBetweenOrEqualTo(depth, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION);
|
||||
|
||||
if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType<T>(), d3D12FormatSupport))
|
||||
{
|
||||
UnsupportedTextureTypeException.ThrowForTexture3D<T>();
|
||||
}
|
||||
|
||||
GraphicsDevice = device;
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
this.allocation = device.Allocator->CreateResource(
|
||||
device.Pool,
|
||||
resourceType,
|
||||
allocationMode,
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
(ushort)depth,
|
||||
out this.d3D12ResourceState);
|
||||
this.allocation = device.Allocator->CreateResource(
|
||||
device.Pool,
|
||||
resourceType,
|
||||
allocationMode,
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
(ushort)depth,
|
||||
out this.d3D12ResourceState);
|
||||
|
||||
this.d3D12Resource = new ComPtr<ID3D12Resource>(this.allocation.Get()->GetResource());
|
||||
this.d3D12Resource = new ComPtr<ID3D12Resource>(this.allocation.Get()->GetResource());
|
||||
#else
|
||||
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(
|
||||
resourceType,
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
(ushort)depth,
|
||||
device.IsCacheCoherentUMA,
|
||||
out this.d3D12ResourceState);
|
||||
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(
|
||||
resourceType,
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
(ushort)depth,
|
||||
device.IsCacheCoherentUMA,
|
||||
out this.d3D12ResourceState);
|
||||
#endif
|
||||
|
||||
this.d3D12CommandListType = this.d3D12ResourceState == D3D12_RESOURCE_STATE_COMMON
|
||||
? D3D12_COMMAND_LIST_TYPE_COPY
|
||||
: D3D12_COMMAND_LIST_TYPE_COMPUTE;
|
||||
this.d3D12CommandListType = this.d3D12ResourceState == D3D12_RESOURCE_STATE_COMMON
|
||||
? D3D12_COMMAND_LIST_TYPE_COPY
|
||||
: D3D12_COMMAND_LIST_TYPE_COMPUTE;
|
||||
|
||||
device.D3D12Device->GetCopyableFootprint(
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
(ushort)depth,
|
||||
out this.d3D12PlacedSubresourceFootprint,
|
||||
out _,
|
||||
out _);
|
||||
device.D3D12Device->GetCopyableFootprint(
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
(ushort)depth,
|
||||
out this.d3D12PlacedSubresourceFootprint,
|
||||
out _,
|
||||
out _);
|
||||
|
||||
device.RegisterAllocatedResource();
|
||||
device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles);
|
||||
device.RegisterAllocatedResource();
|
||||
device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles);
|
||||
|
||||
switch (resourceType)
|
||||
{
|
||||
case ResourceType.ReadOnly:
|
||||
device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType<T>(), D3D12_SRV_DIMENSION_TEXTURE3D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
|
||||
break;
|
||||
case ResourceType.ReadWrite:
|
||||
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType<T>(), D3D12_UAV_DIMENSION_TEXTURE3D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
|
||||
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType<T>(), D3D12_UAV_DIMENSION_TEXTURE3D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible);
|
||||
break;
|
||||
switch (resourceType)
|
||||
{
|
||||
case ResourceType.ReadOnly:
|
||||
device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType<T>(), D3D12_SRV_DIMENSION_TEXTURE3D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
|
||||
break;
|
||||
case ResourceType.ReadWrite:
|
||||
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType<T>(), D3D12_UAV_DIMENSION_TEXTURE3D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
|
||||
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType<T>(), D3D12_UAV_DIMENSION_TEXTURE3D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible);
|
||||
break;
|
||||
}
|
||||
|
||||
this.d3D12Resource.Get()->SetName(this);
|
||||
}
|
||||
}
|
||||
|
||||
this.d3D12Resource.Get()->SetName(this);
|
||||
/// <summary>
|
||||
/// Releases unmanaged resources for the current <see cref="Texture3D{T}"/> instance.
|
||||
/// </summary>
|
||||
~Texture3D()
|
||||
{
|
||||
this.referenceTracker.Dispose();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -181,87 +198,88 @@ public unsafe abstract class Texture3D<T> : NativeObject, IGraphicsResource, Gra
|
||||
/// <param name="depth">The depth of the memory area to copy.</param>
|
||||
internal void CopyTo(ref T destination, int size, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int width, int height, int depth)
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
ThrowIfDisposed();
|
||||
|
||||
Guard.IsInRange(sourceOffsetX, 0, Width);
|
||||
Guard.IsInRange(sourceOffsetY, 0, Height);
|
||||
Guard.IsInRange(sourceOffsetZ, 0, Depth);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, Height);
|
||||
Guard.IsBetweenOrEqualTo(depth, 1, Depth);
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ));
|
||||
Guard.IsGreaterThanOrEqualTo(size, (nint)width * height * depth);
|
||||
|
||||
GraphicsDevice.D3D12Device->GetCopyableFootprint(
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
(ushort)depth,
|
||||
out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintDestination,
|
||||
out ulong rowSizeInBytes,
|
||||
out ulong totalSizeInBytes);
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
using ComPtr<D3D12MA_Allocation> allocation = GraphicsDevice.Allocator->CreateResource(
|
||||
GraphicsDevice.Pool,
|
||||
ResourceType.ReadBack,
|
||||
AllocationMode.Default,
|
||||
totalSizeInBytes);
|
||||
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = new(allocation.Get()->GetResource());
|
||||
#else
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
|
||||
ResourceType.ReadBack,
|
||||
totalSizeInBytes,
|
||||
GraphicsDevice.IsCacheCoherentUMA);
|
||||
#endif
|
||||
|
||||
using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType))
|
||||
using (GraphicsDevice.GetReferenceTracker().GetLease())
|
||||
using (this.referenceTracker.GetLease())
|
||||
{
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
|
||||
}
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
|
||||
d3D12ResourceDestination: d3D12Resource.Get(),
|
||||
&d3D12PlacedSubresourceFootprintDestination,
|
||||
destinationX: 0,
|
||||
destinationY: 0,
|
||||
destinationZ: 0,
|
||||
d3D12ResourceSource: D3D12Resource,
|
||||
sourceX: (uint)sourceOffsetX,
|
||||
sourceY: (uint)sourceOffsetY,
|
||||
sourceZ: (ushort)sourceOffsetZ,
|
||||
Guard.IsInRange(sourceOffsetX, 0, Width);
|
||||
Guard.IsInRange(sourceOffsetY, 0, Height);
|
||||
Guard.IsInRange(sourceOffsetZ, 0, Depth);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, Height);
|
||||
Guard.IsBetweenOrEqualTo(depth, 1, Depth);
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ));
|
||||
Guard.IsGreaterThanOrEqualTo(size, (nint)width * height * depth);
|
||||
|
||||
GraphicsDevice.D3D12Device->GetCopyableFootprint(
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
(ushort)depth);
|
||||
(ushort)depth,
|
||||
out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintDestination,
|
||||
out ulong rowSizeInBytes,
|
||||
out ulong totalSizeInBytes);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
#if NET6_0_OR_GREATER
|
||||
using ComPtr<D3D12MA_Allocation> allocation = GraphicsDevice.Allocator->CreateResource(
|
||||
GraphicsDevice.Pool,
|
||||
ResourceType.ReadBack,
|
||||
AllocationMode.Default,
|
||||
totalSizeInBytes);
|
||||
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = new(allocation.Get()->GetResource());
|
||||
#else
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
|
||||
ResourceType.ReadBack,
|
||||
totalSizeInBytes,
|
||||
GraphicsDevice.IsCacheCoherentUMA);
|
||||
#endif
|
||||
|
||||
using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType))
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
|
||||
}
|
||||
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
|
||||
d3D12ResourceDestination: d3D12Resource.Get(),
|
||||
&d3D12PlacedSubresourceFootprintDestination,
|
||||
destinationX: 0,
|
||||
destinationY: 0,
|
||||
destinationZ: 0,
|
||||
d3D12ResourceSource: D3D12Resource,
|
||||
sourceX: (uint)sourceOffsetX,
|
||||
sourceY: (uint)sourceOffsetY,
|
||||
sourceZ: (ushort)sourceOffsetZ,
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
(ushort)depth);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
|
||||
}
|
||||
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
using ID3D12ResourceMap resource = d3D12Resource.Get()->Map();
|
||||
|
||||
using ID3D12ResourceMap resource = d3D12Resource.Get()->Map();
|
||||
|
||||
fixed (void* destinationPointer = &destination)
|
||||
{
|
||||
MemoryHelper.Copy(
|
||||
resource.Pointer,
|
||||
(uint)height,
|
||||
(uint)depth,
|
||||
rowSizeInBytes,
|
||||
d3D12PlacedSubresourceFootprintDestination.Footprint.RowPitch,
|
||||
d3D12PlacedSubresourceFootprintDestination.Footprint.RowPitch * (uint)height,
|
||||
destinationPointer);
|
||||
fixed (void* destinationPointer = &destination)
|
||||
{
|
||||
MemoryHelper.Copy(
|
||||
resource.Pointer,
|
||||
(uint)height,
|
||||
(uint)depth,
|
||||
rowSizeInBytes,
|
||||
d3D12PlacedSubresourceFootprintDestination.Footprint.RowPitch,
|
||||
d3D12PlacedSubresourceFootprintDestination.Footprint.RowPitch * (uint)height,
|
||||
destinationPointer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -280,67 +298,68 @@ public unsafe abstract class Texture3D<T> : NativeObject, IGraphicsResource, Gra
|
||||
/// <param name="depth">The depth of the memory area to copy.</param>
|
||||
internal void CopyTo(Texture3D<T> destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth)
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
ThrowIfDisposed();
|
||||
|
||||
destination.ThrowIfDeviceMismatch(GraphicsDevice);
|
||||
destination.ThrowIfDisposed();
|
||||
|
||||
Guard.IsInRange(sourceOffsetX, 0, Width);
|
||||
Guard.IsInRange(sourceOffsetY, 0, Height);
|
||||
Guard.IsInRange(sourceOffsetZ, 0, Depth);
|
||||
Guard.IsInRange(destinationOffsetX, 0, destination.Width);
|
||||
Guard.IsInRange(destinationOffsetY, 0, destination.Height);
|
||||
Guard.IsInRange(destinationOffsetZ, 0, destination.Depth);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, Height);
|
||||
Guard.IsBetweenOrEqualTo(depth, 1, Depth);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, destination.Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, destination.Height);
|
||||
Guard.IsBetweenOrEqualTo(depth, 1, destination.Depth);
|
||||
Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX));
|
||||
Guard.IsBetweenOrEqualTo(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY));
|
||||
Guard.IsBetweenOrEqualTo(destinationOffsetZ + depth, 1, destination.Depth, nameof(destinationOffsetZ));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ));
|
||||
|
||||
D3D12_COMMAND_LIST_TYPE d3D12CommandListType =
|
||||
this.d3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE ||
|
||||
destination.d3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE
|
||||
? D3D12_COMMAND_LIST_TYPE_COMPUTE
|
||||
: D3D12_COMMAND_LIST_TYPE_COPY;
|
||||
|
||||
using CommandList copyCommandList = new(GraphicsDevice, d3D12CommandListType);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
using (GraphicsDevice.GetReferenceTracker().GetLease())
|
||||
using (this.referenceTracker.GetLease())
|
||||
using (destination.referenceTracker.GetLease())
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(destination.D3D12Resource, destination.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
destination.ThrowIfDeviceMismatch(GraphicsDevice);
|
||||
|
||||
Guard.IsInRange(sourceOffsetX, 0, Width);
|
||||
Guard.IsInRange(sourceOffsetY, 0, Height);
|
||||
Guard.IsInRange(sourceOffsetZ, 0, Depth);
|
||||
Guard.IsInRange(destinationOffsetX, 0, destination.Width);
|
||||
Guard.IsInRange(destinationOffsetY, 0, destination.Height);
|
||||
Guard.IsInRange(destinationOffsetZ, 0, destination.Depth);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, Height);
|
||||
Guard.IsBetweenOrEqualTo(depth, 1, Depth);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, destination.Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, destination.Height);
|
||||
Guard.IsBetweenOrEqualTo(depth, 1, destination.Depth);
|
||||
Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX));
|
||||
Guard.IsBetweenOrEqualTo(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY));
|
||||
Guard.IsBetweenOrEqualTo(destinationOffsetZ + depth, 1, destination.Depth, nameof(destinationOffsetZ));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ));
|
||||
|
||||
D3D12_COMMAND_LIST_TYPE d3D12CommandListType =
|
||||
this.d3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE ||
|
||||
destination.d3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE
|
||||
? D3D12_COMMAND_LIST_TYPE_COMPUTE
|
||||
: D3D12_COMMAND_LIST_TYPE_COPY;
|
||||
|
||||
using CommandList copyCommandList = new(GraphicsDevice, d3D12CommandListType);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(destination.D3D12Resource, destination.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
|
||||
}
|
||||
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
|
||||
d3D12ResourceDestination: destination.D3D12Resource,
|
||||
(uint)destinationOffsetX,
|
||||
(uint)destinationOffsetY,
|
||||
(ushort)destinationOffsetZ,
|
||||
d3D12ResourceSource: D3D12Resource,
|
||||
(uint)sourceOffsetX,
|
||||
(uint)sourceOffsetY,
|
||||
(ushort)sourceOffsetZ,
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
(ushort)depth);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(destination.D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, destination.d3D12ResourceState);
|
||||
}
|
||||
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
|
||||
d3D12ResourceDestination: destination.D3D12Resource,
|
||||
(uint)destinationOffsetX,
|
||||
(uint)destinationOffsetY,
|
||||
(ushort)destinationOffsetZ,
|
||||
d3D12ResourceSource: D3D12Resource,
|
||||
(uint)sourceOffsetX,
|
||||
(uint)sourceOffsetY,
|
||||
(ushort)sourceOffsetZ,
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
(ushort)depth);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(destination.D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, destination.d3D12ResourceState);
|
||||
}
|
||||
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -358,63 +377,64 @@ public unsafe abstract class Texture3D<T> : NativeObject, IGraphicsResource, Gra
|
||||
/// <param name="depth">The depth of the memory area to copy.</param>
|
||||
internal void CopyTo(ReadBackTexture3D<T> destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth)
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
ThrowIfDisposed();
|
||||
|
||||
destination.ThrowIfDeviceMismatch(GraphicsDevice);
|
||||
destination.ThrowIfDisposed();
|
||||
|
||||
Guard.IsInRange(sourceOffsetX, 0, Width);
|
||||
Guard.IsInRange(sourceOffsetY, 0, Height);
|
||||
Guard.IsInRange(sourceOffsetZ, 0, Depth);
|
||||
Guard.IsInRange(destinationOffsetX, 0, destination.Width);
|
||||
Guard.IsInRange(destinationOffsetY, 0, destination.Height);
|
||||
Guard.IsInRange(destinationOffsetZ, 0, destination.Depth);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, Height);
|
||||
Guard.IsBetweenOrEqualTo(depth, 1, Depth);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, destination.Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, destination.Height);
|
||||
Guard.IsBetweenOrEqualTo(depth, 1, destination.Depth);
|
||||
Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX));
|
||||
Guard.IsBetweenOrEqualTo(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY));
|
||||
Guard.IsBetweenOrEqualTo(destinationOffsetZ + depth, 1, destination.Depth, nameof(destinationOffsetZ));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ));
|
||||
|
||||
using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
using (GraphicsDevice.GetReferenceTracker().GetLease())
|
||||
using (this.referenceTracker.GetLease())
|
||||
using (destination.GetReferenceTracker().GetLease())
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
|
||||
}
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintDestination = &destination.D3D12PlacedSubresourceFootprint)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
|
||||
d3D12ResourceDestination: destination.D3D12Resource,
|
||||
d3D12PlacedSubresourceFootprintDestination,
|
||||
(uint)destinationOffsetX,
|
||||
(uint)destinationOffsetY,
|
||||
(ushort)destinationOffsetZ,
|
||||
d3D12ResourceSource: D3D12Resource,
|
||||
(uint)sourceOffsetX,
|
||||
(uint)sourceOffsetY,
|
||||
(ushort)sourceOffsetZ,
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
(ushort)depth);
|
||||
}
|
||||
destination.ThrowIfDeviceMismatch(GraphicsDevice);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
|
||||
}
|
||||
Guard.IsInRange(sourceOffsetX, 0, Width);
|
||||
Guard.IsInRange(sourceOffsetY, 0, Height);
|
||||
Guard.IsInRange(sourceOffsetZ, 0, Depth);
|
||||
Guard.IsInRange(destinationOffsetX, 0, destination.Width);
|
||||
Guard.IsInRange(destinationOffsetY, 0, destination.Height);
|
||||
Guard.IsInRange(destinationOffsetZ, 0, destination.Depth);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, Height);
|
||||
Guard.IsBetweenOrEqualTo(depth, 1, Depth);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, destination.Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, destination.Height);
|
||||
Guard.IsBetweenOrEqualTo(depth, 1, destination.Depth);
|
||||
Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX));
|
||||
Guard.IsBetweenOrEqualTo(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY));
|
||||
Guard.IsBetweenOrEqualTo(destinationOffsetZ + depth, 1, destination.Depth, nameof(destinationOffsetZ));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ));
|
||||
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
|
||||
}
|
||||
|
||||
fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintDestination = &destination.D3D12PlacedSubresourceFootprint)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
|
||||
d3D12ResourceDestination: destination.D3D12Resource,
|
||||
d3D12PlacedSubresourceFootprintDestination,
|
||||
(uint)destinationOffsetX,
|
||||
(uint)destinationOffsetY,
|
||||
(ushort)destinationOffsetZ,
|
||||
d3D12ResourceSource: D3D12Resource,
|
||||
(uint)sourceOffsetX,
|
||||
(uint)sourceOffsetY,
|
||||
(ushort)sourceOffsetZ,
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
(ushort)depth);
|
||||
}
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
|
||||
}
|
||||
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -430,86 +450,87 @@ public unsafe abstract class Texture3D<T> : NativeObject, IGraphicsResource, Gra
|
||||
/// <param name="depth">The depth of the memory area to write to.</param>
|
||||
internal void CopyFrom(ref T source, int size, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth)
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
using (GraphicsDevice.GetReferenceTracker().GetLease())
|
||||
using (this.referenceTracker.GetLease())
|
||||
{
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
ThrowIfDisposed();
|
||||
Guard.IsInRange(destinationOffsetX, 0, Width);
|
||||
Guard.IsInRange(destinationOffsetY, 0, Height);
|
||||
Guard.IsInRange(destinationOffsetZ, 0, Depth);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, Height);
|
||||
Guard.IsBetweenOrEqualTo(depth, 1, Depth);
|
||||
Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX));
|
||||
Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY));
|
||||
Guard.IsLessThanOrEqualTo(destinationOffsetZ + depth, Depth, nameof(destinationOffsetZ));
|
||||
Guard.IsGreaterThanOrEqualTo(size, (nint)width * height * depth);
|
||||
|
||||
Guard.IsInRange(destinationOffsetX, 0, Width);
|
||||
Guard.IsInRange(destinationOffsetY, 0, Height);
|
||||
Guard.IsInRange(destinationOffsetZ, 0, Depth);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, Height);
|
||||
Guard.IsBetweenOrEqualTo(depth, 1, Depth);
|
||||
Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX));
|
||||
Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY));
|
||||
Guard.IsLessThanOrEqualTo(destinationOffsetZ + depth, Depth, nameof(destinationOffsetZ));
|
||||
Guard.IsGreaterThanOrEqualTo(size, (nint)width * height * depth);
|
||||
|
||||
GraphicsDevice.D3D12Device->GetCopyableFootprint(
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
(ushort)depth,
|
||||
out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintSource,
|
||||
out ulong rowSizeInBytes,
|
||||
out ulong totalSizeInBytes);
|
||||
GraphicsDevice.D3D12Device->GetCopyableFootprint(
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
(ushort)depth,
|
||||
out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintSource,
|
||||
out ulong rowSizeInBytes,
|
||||
out ulong totalSizeInBytes);
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
using ComPtr<D3D12MA_Allocation> allocation = GraphicsDevice.Allocator->CreateResource(
|
||||
GraphicsDevice.Pool,
|
||||
ResourceType.Upload,
|
||||
AllocationMode.Default,
|
||||
totalSizeInBytes);
|
||||
using ComPtr<D3D12MA_Allocation> allocation = GraphicsDevice.Allocator->CreateResource(
|
||||
GraphicsDevice.Pool,
|
||||
ResourceType.Upload,
|
||||
AllocationMode.Default,
|
||||
totalSizeInBytes);
|
||||
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = new(allocation.Get()->GetResource());
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = new(allocation.Get()->GetResource());
|
||||
#else
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
|
||||
ResourceType.Upload,
|
||||
totalSizeInBytes,
|
||||
GraphicsDevice.IsCacheCoherentUMA);
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
|
||||
ResourceType.Upload,
|
||||
totalSizeInBytes,
|
||||
GraphicsDevice.IsCacheCoherentUMA);
|
||||
#endif
|
||||
|
||||
using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
|
||||
fixed (void* sourcePointer = &source)
|
||||
{
|
||||
MemoryHelper.Copy(
|
||||
sourcePointer,
|
||||
resource.Pointer,
|
||||
using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
|
||||
fixed (void* sourcePointer = &source)
|
||||
{
|
||||
MemoryHelper.Copy(
|
||||
sourcePointer,
|
||||
resource.Pointer,
|
||||
(uint)height,
|
||||
(uint)depth,
|
||||
rowSizeInBytes,
|
||||
d3D12PlacedSubresourceFootprintSource.Footprint.RowPitch,
|
||||
d3D12PlacedSubresourceFootprintSource.Footprint.RowPitch * (uint)height);
|
||||
}
|
||||
|
||||
using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
|
||||
}
|
||||
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
|
||||
d3D12ResourceDestination: D3D12Resource,
|
||||
destinationX: (uint)destinationOffsetX,
|
||||
destinationY: (uint)destinationOffsetY,
|
||||
destinationZ: (ushort)destinationOffsetZ,
|
||||
d3D12ResourceSource: d3D12Resource.Get(),
|
||||
&d3D12PlacedSubresourceFootprintSource,
|
||||
sourceX: 0,
|
||||
sourceY: 0,
|
||||
sourceZ: 0,
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
(uint)depth,
|
||||
rowSizeInBytes,
|
||||
d3D12PlacedSubresourceFootprintSource.Footprint.RowPitch,
|
||||
d3D12PlacedSubresourceFootprintSource.Footprint.RowPitch * (uint)height);
|
||||
(ushort)depth);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState);
|
||||
}
|
||||
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
|
||||
using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
|
||||
}
|
||||
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
|
||||
d3D12ResourceDestination: D3D12Resource,
|
||||
destinationX: (uint)destinationOffsetX,
|
||||
destinationY: (uint)destinationOffsetY,
|
||||
destinationZ: (ushort)destinationOffsetZ,
|
||||
d3D12ResourceSource: d3D12Resource.Get(),
|
||||
&d3D12PlacedSubresourceFootprintSource,
|
||||
sourceX: 0,
|
||||
sourceY: 0,
|
||||
sourceZ: 0,
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
(ushort)depth);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState);
|
||||
}
|
||||
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -527,67 +548,89 @@ public unsafe abstract class Texture3D<T> : NativeObject, IGraphicsResource, Gra
|
||||
/// <param name="depth">The depth of the memory area to write to.</param>
|
||||
internal void CopyFrom(UploadTexture3D<T> source, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth)
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
ThrowIfDisposed();
|
||||
|
||||
source.ThrowIfDeviceMismatch(GraphicsDevice);
|
||||
source.ThrowIfDisposed();
|
||||
|
||||
Guard.IsInRange(sourceOffsetX, 0, source.Width);
|
||||
Guard.IsInRange(sourceOffsetY, 0, source.Height);
|
||||
Guard.IsInRange(sourceOffsetZ, 0, source.Depth);
|
||||
Guard.IsInRange(destinationOffsetX, 0, Width);
|
||||
Guard.IsInRange(destinationOffsetY, 0, Height);
|
||||
Guard.IsInRange(destinationOffsetZ, 0, Depth);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, Height);
|
||||
Guard.IsBetweenOrEqualTo(depth, 1, Depth);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, source.Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, source.Height);
|
||||
Guard.IsBetweenOrEqualTo(depth, 1, source.Depth);
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetX + width, source.Width, nameof(sourceOffsetX));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetY + height, source.Height, nameof(sourceOffsetY));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, source.Depth, nameof(sourceOffsetZ));
|
||||
Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX));
|
||||
Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY));
|
||||
Guard.IsLessThanOrEqualTo(destinationOffsetZ + depth, Depth, nameof(destinationOffsetZ));
|
||||
|
||||
using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
using (GraphicsDevice.GetReferenceTracker().GetLease())
|
||||
using (this.referenceTracker.GetLease())
|
||||
using (source.GetReferenceTracker().GetLease())
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
|
||||
}
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintSource = &source.D3D12PlacedSubresourceFootprint)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
|
||||
d3D12ResourceDestination: D3D12Resource,
|
||||
(uint)destinationOffsetX,
|
||||
(uint)destinationOffsetY,
|
||||
(ushort)destinationOffsetZ,
|
||||
d3D12ResourceSource: source.D3D12Resource,
|
||||
d3D12PlacedSubresourceFootprintSource,
|
||||
(uint)sourceOffsetX,
|
||||
(uint)sourceOffsetY,
|
||||
(ushort)sourceOffsetZ,
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
(ushort)depth);
|
||||
}
|
||||
source.ThrowIfDeviceMismatch(GraphicsDevice);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState);
|
||||
}
|
||||
Guard.IsInRange(sourceOffsetX, 0, source.Width);
|
||||
Guard.IsInRange(sourceOffsetY, 0, source.Height);
|
||||
Guard.IsInRange(sourceOffsetZ, 0, source.Depth);
|
||||
Guard.IsInRange(destinationOffsetX, 0, Width);
|
||||
Guard.IsInRange(destinationOffsetY, 0, Height);
|
||||
Guard.IsInRange(destinationOffsetZ, 0, Depth);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, Height);
|
||||
Guard.IsBetweenOrEqualTo(depth, 1, Depth);
|
||||
Guard.IsBetweenOrEqualTo(width, 1, source.Width);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, source.Height);
|
||||
Guard.IsBetweenOrEqualTo(depth, 1, source.Depth);
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetX + width, source.Width, nameof(sourceOffsetX));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetY + height, source.Height, nameof(sourceOffsetY));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, source.Depth, nameof(sourceOffsetZ));
|
||||
Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX));
|
||||
Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY));
|
||||
Guard.IsLessThanOrEqualTo(destinationOffsetZ + depth, Depth, nameof(destinationOffsetZ));
|
||||
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
|
||||
}
|
||||
|
||||
fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintSource = &source.D3D12PlacedSubresourceFootprint)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
|
||||
d3D12ResourceDestination: D3D12Resource,
|
||||
(uint)destinationOffsetX,
|
||||
(uint)destinationOffsetY,
|
||||
(ushort)destinationOffsetZ,
|
||||
d3D12ResourceSource: source.D3D12Resource,
|
||||
d3D12PlacedSubresourceFootprintSource,
|
||||
(uint)sourceOffsetX,
|
||||
(uint)sourceOffsetY,
|
||||
(ushort)sourceOffsetZ,
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
(ushort)depth);
|
||||
}
|
||||
|
||||
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState);
|
||||
}
|
||||
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void OnDispose()
|
||||
public virtual void Dispose()
|
||||
{
|
||||
this.referenceTracker.Dispose();
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ReferenceTracker.ITrackedObject.GetReferenceTracker"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal ref ReferenceTracker GetReferenceTracker()
|
||||
{
|
||||
return ref this.referenceTracker;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
ref ReferenceTracker ReferenceTracker.ITrackedObject.GetReferenceTracker()
|
||||
{
|
||||
return ref this.referenceTracker;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ReferenceTracker.ITrackedObject.DangerousRelease()
|
||||
{
|
||||
this.d3D12Resource.Dispose();
|
||||
#if NET6_0_OR_GREATER
|
||||
@@ -635,46 +678,54 @@ public unsafe abstract class Texture3D<T> : NativeObject, IGraphicsResource, Gra
|
||||
/// <inheritdoc cref="GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice, out bool)"/>
|
||||
internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
ThrowIfDeviceMismatch(device);
|
||||
using (this.referenceTracker.GetLease())
|
||||
{
|
||||
ThrowIfDeviceMismatch(device);
|
||||
|
||||
isNormalized = DXGIFormatHelper.IsNormalizedType<T>();
|
||||
isNormalized = DXGIFormatHelper.IsNormalizedType<T>();
|
||||
|
||||
return (this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible);
|
||||
return (this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice)"/>
|
||||
internal ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
ThrowIfDeviceMismatch(device);
|
||||
using (this.referenceTracker.GetLease())
|
||||
{
|
||||
ThrowIfDeviceMismatch(device);
|
||||
|
||||
return D3D12Resource;
|
||||
return D3D12Resource;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice, ResourceState, out ID3D12Resource*)"/>
|
||||
internal (D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
ThrowIfDeviceMismatch(device);
|
||||
using (this.referenceTracker.GetLease())
|
||||
{
|
||||
ThrowIfDeviceMismatch(device);
|
||||
|
||||
D3D12_RESOURCE_STATES d3D12ResourceStatesBefore = this.d3D12ResourceState;
|
||||
D3D12_RESOURCE_STATES d3D12ResourceStatesAfter = ResourceStateHelper.GetD3D12ResourceStates(resourceState);
|
||||
D3D12_RESOURCE_STATES d3D12ResourceStatesBefore = this.d3D12ResourceState;
|
||||
D3D12_RESOURCE_STATES d3D12ResourceStatesAfter = ResourceStateHelper.GetD3D12ResourceStates(resourceState);
|
||||
|
||||
this.d3D12ResourceState = d3D12ResourceStatesAfter;
|
||||
this.d3D12ResourceState = d3D12ResourceStatesAfter;
|
||||
|
||||
d3D12Resource = D3D12Resource;
|
||||
d3D12Resource = D3D12Resource;
|
||||
|
||||
return (d3D12ResourceStatesBefore, d3D12ResourceStatesAfter);
|
||||
return (d3D12ResourceStatesBefore, d3D12ResourceStatesAfter);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
ThrowIfDeviceMismatch(device);
|
||||
using (this.referenceTracker.GetLease())
|
||||
{
|
||||
ThrowIfDeviceMismatch(device);
|
||||
|
||||
return D3D12GpuDescriptorHandle;
|
||||
return D3D12GpuDescriptorHandle;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
||||
@@ -15,9 +15,14 @@ namespace ComputeSharp.Resources;
|
||||
/// A <see langword="class"/> representing a typed buffer stored on CPU memory, that can be used to transfer data to/from the GPU.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the buffer.</typeparam>
|
||||
public abstract unsafe class TransferBuffer<T> : NativeObject, IGraphicsResource, IMemoryOwner<T>
|
||||
public abstract unsafe class TransferBuffer<T> : ReferenceTracker.ITrackedObject, IGraphicsResource, IMemoryOwner<T>
|
||||
where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// The owning <see cref="ReferenceTracker"/> object for the current instance.
|
||||
/// </summary>
|
||||
private ReferenceTracker referenceTracker;
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12MA_Allocation"/> instance used to retrieve <see cref="d3D12Resource"/>.
|
||||
@@ -44,29 +49,41 @@ public abstract unsafe class TransferBuffer<T> : NativeObject, IGraphicsResource
|
||||
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
|
||||
private protected TransferBuffer(GraphicsDevice device, int length, ResourceType resourceType, AllocationMode allocationMode)
|
||||
{
|
||||
device.ThrowIfDisposed();
|
||||
device.ThrowIfDeviceLost();
|
||||
this.referenceTracker = new ReferenceTracker(this);
|
||||
|
||||
// The maximum length is set such that the aligned buffer size can't exceed uint.MaxValue
|
||||
Guard.IsBetweenOrEqualTo(length, 1, (uint.MaxValue / (uint)sizeof(T)) & ~255);
|
||||
using (device.GetReferenceTracker().GetLease())
|
||||
{
|
||||
device.ThrowIfDeviceLost();
|
||||
|
||||
GraphicsDevice = device;
|
||||
Length = length;
|
||||
// The maximum length is set such that the aligned buffer size can't exceed uint.MaxValue
|
||||
Guard.IsBetweenOrEqualTo(length, 1, (uint.MaxValue / (uint)sizeof(T)) & ~255);
|
||||
|
||||
ulong sizeInBytes = (uint)length * (uint)sizeof(T);
|
||||
GraphicsDevice = device;
|
||||
Length = length;
|
||||
|
||||
ulong sizeInBytes = (uint)length * (uint)sizeof(T);
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, sizeInBytes);
|
||||
this.d3D12Resource = new ComPtr<ID3D12Resource>(this.allocation.Get()->GetResource());
|
||||
this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, sizeInBytes);
|
||||
this.d3D12Resource = new ComPtr<ID3D12Resource>(this.allocation.Get()->GetResource());
|
||||
#else
|
||||
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, sizeInBytes, device.IsCacheCoherentUMA);
|
||||
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, sizeInBytes, device.IsCacheCoherentUMA);
|
||||
#endif
|
||||
|
||||
device.RegisterAllocatedResource();
|
||||
device.RegisterAllocatedResource();
|
||||
|
||||
this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer;
|
||||
this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer;
|
||||
|
||||
this.d3D12Resource.Get()->SetName(this);
|
||||
this.d3D12Resource.Get()->SetName(this);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Releases unmanaged resources for the current <see cref="TransferBuffer{T}"/> instance.
|
||||
/// </summary>
|
||||
~TransferBuffer()
|
||||
{
|
||||
this.referenceTracker.Dispose();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -93,9 +110,10 @@ public abstract unsafe class TransferBuffer<T> : NativeObject, IGraphicsResource
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
|
||||
return new MemoryManager(this).Memory;
|
||||
using (this.referenceTracker.GetLease())
|
||||
{
|
||||
return new MemoryManager(this).Memory;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,14 +123,36 @@ public abstract unsafe class TransferBuffer<T> : NativeObject, IGraphicsResource
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
|
||||
return new(this.mappedData, Length);
|
||||
using (this.referenceTracker.GetLease())
|
||||
{
|
||||
return new(this.mappedData, Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void OnDispose()
|
||||
public void Dispose()
|
||||
{
|
||||
this.referenceTracker.Dispose();
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ReferenceTracker.ITrackedObject.GetReferenceTracker"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal ref ReferenceTracker GetReferenceTracker()
|
||||
{
|
||||
return ref this.referenceTracker;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
ref ReferenceTracker ReferenceTracker.ITrackedObject.GetReferenceTracker()
|
||||
{
|
||||
return ref this.referenceTracker;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ReferenceTracker.ITrackedObject.DangerousRelease()
|
||||
{
|
||||
this.d3D12Resource.Dispose();
|
||||
#if NET6_0_OR_GREATER
|
||||
@@ -171,11 +211,12 @@ public abstract unsafe class TransferBuffer<T> : NativeObject, IGraphicsResource
|
||||
/// <inheritdoc/>
|
||||
public override MemoryHandle Pin(int elementIndex = 0)
|
||||
{
|
||||
Guard.IsEqualTo(elementIndex, 0);
|
||||
using (this.buffer.referenceTracker.GetLease())
|
||||
{
|
||||
Guard.IsEqualTo(elementIndex, 0);
|
||||
|
||||
this.buffer.ThrowIfDisposed();
|
||||
|
||||
return new(this.buffer.mappedData);
|
||||
return new(this.buffer.mappedData);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using CommunityToolkit.Diagnostics;
|
||||
using ComputeSharp.Exceptions;
|
||||
using ComputeSharp.Graphics.Extensions;
|
||||
@@ -15,9 +16,14 @@ namespace ComputeSharp.Resources;
|
||||
/// A <see langword="class"/> representing a typed 2D texture stored on on CPU memory, that can be used to transfer data to/from the GPU.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
|
||||
public unsafe abstract class TransferTexture2D<T> : NativeObject, IGraphicsResource
|
||||
public unsafe abstract class TransferTexture2D<T> : ReferenceTracker.ITrackedObject, IDisposable, IGraphicsResource
|
||||
where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// The owning <see cref="ReferenceTracker"/> object for the current instance.
|
||||
/// </summary>
|
||||
private ReferenceTracker referenceTracker;
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12MA_Allocation"/> instance used to retrieve <see cref="d3D12Resource"/>.
|
||||
@@ -50,39 +56,51 @@ public unsafe abstract class TransferTexture2D<T> : NativeObject, IGraphicsResou
|
||||
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
|
||||
private protected TransferTexture2D(GraphicsDevice device, int width, int height, ResourceType resourceType, AllocationMode allocationMode)
|
||||
{
|
||||
device.ThrowIfDisposed();
|
||||
device.ThrowIfDeviceLost();
|
||||
this.referenceTracker = new ReferenceTracker(this);
|
||||
|
||||
Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION);
|
||||
|
||||
if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType<T>(), D3D12_FORMAT_SUPPORT1_TEXTURE2D))
|
||||
using (device.GetReferenceTracker().GetLease())
|
||||
{
|
||||
UnsupportedTextureTypeException.ThrowForTexture2D<T>();
|
||||
}
|
||||
device.ThrowIfDeviceLost();
|
||||
|
||||
GraphicsDevice = device;
|
||||
Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION);
|
||||
|
||||
device.D3D12Device->GetCopyableFootprint(
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
out this.d3D12PlacedSubresourceFootprint,
|
||||
out _,
|
||||
out ulong totalSizeInBytes);
|
||||
if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType<T>(), D3D12_FORMAT_SUPPORT1_TEXTURE2D))
|
||||
{
|
||||
UnsupportedTextureTypeException.ThrowForTexture2D<T>();
|
||||
}
|
||||
|
||||
GraphicsDevice = device;
|
||||
|
||||
device.D3D12Device->GetCopyableFootprint(
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
out this.d3D12PlacedSubresourceFootprint,
|
||||
out _,
|
||||
out ulong totalSizeInBytes);
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, totalSizeInBytes);
|
||||
this.d3D12Resource = new ComPtr<ID3D12Resource>(this.allocation.Get()->GetResource());
|
||||
this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, totalSizeInBytes);
|
||||
this.d3D12Resource = new ComPtr<ID3D12Resource>(this.allocation.Get()->GetResource());
|
||||
#else
|
||||
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, totalSizeInBytes, device.IsCacheCoherentUMA);
|
||||
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, totalSizeInBytes, device.IsCacheCoherentUMA);
|
||||
#endif
|
||||
|
||||
device.RegisterAllocatedResource();
|
||||
device.RegisterAllocatedResource();
|
||||
|
||||
this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer;
|
||||
this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer;
|
||||
|
||||
this.d3D12Resource.Get()->SetName(this);
|
||||
this.d3D12Resource.Get()->SetName(this);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Releases unmanaged resources for the current <see cref="TransferTexture2D{T}"/> instance.
|
||||
/// </summary>
|
||||
~TransferTexture2D()
|
||||
{
|
||||
this.referenceTracker.Dispose();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -114,14 +132,36 @@ public unsafe abstract class TransferTexture2D<T> : NativeObject, IGraphicsResou
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
|
||||
return new(this.mappedData, Width, Height, (int)this.d3D12PlacedSubresourceFootprint.Footprint.RowPitch);
|
||||
using (this.referenceTracker.GetLease())
|
||||
{
|
||||
return new(this.mappedData, Width, Height, (int)this.d3D12PlacedSubresourceFootprint.Footprint.RowPitch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void OnDispose()
|
||||
public void Dispose()
|
||||
{
|
||||
this.referenceTracker.Dispose();
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ReferenceTracker.ITrackedObject.GetReferenceTracker"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal ref ReferenceTracker GetReferenceTracker()
|
||||
{
|
||||
return ref this.referenceTracker;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
ref ReferenceTracker ReferenceTracker.ITrackedObject.GetReferenceTracker()
|
||||
{
|
||||
return ref this.referenceTracker;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ReferenceTracker.ITrackedObject.DangerousRelease()
|
||||
{
|
||||
this.d3D12Resource.Dispose();
|
||||
#if NET6_0_OR_GREATER
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using CommunityToolkit.Diagnostics;
|
||||
using ComputeSharp.Exceptions;
|
||||
using ComputeSharp.Graphics.Extensions;
|
||||
@@ -15,9 +16,14 @@ namespace ComputeSharp.Resources;
|
||||
/// A <see langword="class"/> representing a typed 3D texture stored on on CPU memory, that can be used to transfer data to/from the GPU.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
|
||||
public unsafe abstract class TransferTexture3D<T> : NativeObject, IGraphicsResource
|
||||
public unsafe abstract class TransferTexture3D<T> : ReferenceTracker.ITrackedObject, IDisposable, IGraphicsResource
|
||||
where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// The owning <see cref="ReferenceTracker"/> object for the current instance.
|
||||
/// </summary>
|
||||
private ReferenceTracker referenceTracker;
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12MA_Allocation"/> instance used to retrieve <see cref="d3D12Resource"/>.
|
||||
@@ -51,41 +57,53 @@ public unsafe abstract class TransferTexture3D<T> : NativeObject, IGraphicsResou
|
||||
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
|
||||
private protected TransferTexture3D(GraphicsDevice device, int width, int height, int depth, ResourceType resourceType, AllocationMode allocationMode)
|
||||
{
|
||||
device.ThrowIfDisposed();
|
||||
device.ThrowIfDeviceLost();
|
||||
this.referenceTracker = new ReferenceTracker(this);
|
||||
|
||||
Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION);
|
||||
Guard.IsBetweenOrEqualTo(depth, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION);
|
||||
|
||||
if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType<T>(), D3D12_FORMAT_SUPPORT1_TEXTURE3D))
|
||||
using (device.GetReferenceTracker().GetLease())
|
||||
{
|
||||
UnsupportedTextureTypeException.ThrowForTexture3D<T>();
|
||||
}
|
||||
device.ThrowIfDeviceLost();
|
||||
|
||||
GraphicsDevice = device;
|
||||
Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION);
|
||||
Guard.IsBetweenOrEqualTo(height, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION);
|
||||
Guard.IsBetweenOrEqualTo(depth, 1, D3D12.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION);
|
||||
|
||||
device.D3D12Device->GetCopyableFootprint(
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
(ushort)depth,
|
||||
out this.d3D12PlacedSubresourceFootprint,
|
||||
out _,
|
||||
out ulong totalSizeInBytes);
|
||||
if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType<T>(), D3D12_FORMAT_SUPPORT1_TEXTURE3D))
|
||||
{
|
||||
UnsupportedTextureTypeException.ThrowForTexture3D<T>();
|
||||
}
|
||||
|
||||
GraphicsDevice = device;
|
||||
|
||||
device.D3D12Device->GetCopyableFootprint(
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
(ushort)depth,
|
||||
out this.d3D12PlacedSubresourceFootprint,
|
||||
out _,
|
||||
out ulong totalSizeInBytes);
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, totalSizeInBytes);
|
||||
this.d3D12Resource = new ComPtr<ID3D12Resource>(this.allocation.Get()->GetResource());
|
||||
this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, totalSizeInBytes);
|
||||
this.d3D12Resource = new ComPtr<ID3D12Resource>(this.allocation.Get()->GetResource());
|
||||
#else
|
||||
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, totalSizeInBytes, device.IsCacheCoherentUMA);
|
||||
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, totalSizeInBytes, device.IsCacheCoherentUMA);
|
||||
#endif
|
||||
|
||||
device.RegisterAllocatedResource();
|
||||
device.RegisterAllocatedResource();
|
||||
|
||||
this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer;
|
||||
this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer;
|
||||
|
||||
this.d3D12Resource.Get()->SetName(this);
|
||||
this.d3D12Resource.Get()->SetName(this);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Releases unmanaged resources for the current <see cref="TransferTexture3D{T}"/> instance.
|
||||
/// </summary>
|
||||
~TransferTexture3D()
|
||||
{
|
||||
this.referenceTracker.Dispose();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -122,14 +140,36 @@ public unsafe abstract class TransferTexture3D<T> : NativeObject, IGraphicsResou
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
|
||||
return new(this.mappedData, Width, Height, Depth, (int)this.d3D12PlacedSubresourceFootprint.Footprint.RowPitch);
|
||||
using (this.referenceTracker.GetLease())
|
||||
{
|
||||
return new(this.mappedData, Width, Height, Depth, (int)this.d3D12PlacedSubresourceFootprint.Footprint.RowPitch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void OnDispose()
|
||||
public void Dispose()
|
||||
{
|
||||
this.referenceTracker.Dispose();
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ReferenceTracker.ITrackedObject.GetReferenceTracker"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal ref ReferenceTracker GetReferenceTracker()
|
||||
{
|
||||
return ref this.referenceTracker;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
ref ReferenceTracker ReferenceTracker.ITrackedObject.GetReferenceTracker()
|
||||
{
|
||||
return ref this.referenceTracker;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ReferenceTracker.ITrackedObject.DangerousRelease()
|
||||
{
|
||||
this.d3D12Resource.Dispose();
|
||||
#if NET6_0_OR_GREATER
|
||||
|
||||
@@ -57,89 +57,92 @@ public sealed class ConstantBuffer<T> : Buffer<T>
|
||||
/// <inheritdoc/>
|
||||
internal override unsafe void CopyTo(ref T destination, int offset, int length)
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
ThrowIfDisposed();
|
||||
|
||||
Guard.IsBetweenOrEqualTo(length, 0, Length);
|
||||
Guard.IsInRange(offset, 0, Length);
|
||||
Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset));
|
||||
|
||||
using ID3D12ResourceMap resource = D3D12Resource->Map();
|
||||
|
||||
fixed (void* destinationPointer = &destination)
|
||||
using (GraphicsDevice.GetReferenceTracker().GetLease())
|
||||
using (GetReferenceTracker().GetLease())
|
||||
{
|
||||
MemoryHelper.Copy<T>(
|
||||
source: resource.Pointer,
|
||||
destination: destinationPointer,
|
||||
sourceElementOffset: (uint)offset,
|
||||
destinationElementOffset: 0,
|
||||
sourceElementPitchInBytes: (uint)GetPaddedSize(),
|
||||
destinationElementPitchInBytes: (uint)sizeof(T),
|
||||
count: (uint)length);
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
Guard.IsBetweenOrEqualTo(length, 0, Length);
|
||||
Guard.IsInRange(offset, 0, Length);
|
||||
Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset));
|
||||
|
||||
using ID3D12ResourceMap resource = D3D12Resource->Map();
|
||||
|
||||
fixed (void* destinationPointer = &destination)
|
||||
{
|
||||
MemoryHelper.Copy<T>(
|
||||
source: resource.Pointer,
|
||||
destination: destinationPointer,
|
||||
sourceElementOffset: (uint)offset,
|
||||
destinationElementOffset: 0,
|
||||
sourceElementPitchInBytes: (uint)GetPaddedSize(),
|
||||
destinationElementPitchInBytes: (uint)sizeof(T),
|
||||
count: (uint)length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
internal override unsafe void CopyTo(Buffer<T> destination, int sourceOffset, int destinationOffset, int length)
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
ThrowIfDisposed();
|
||||
|
||||
destination.ThrowIfDeviceMismatch(GraphicsDevice);
|
||||
destination.ThrowIfDisposed();
|
||||
|
||||
Guard.IsBetweenOrEqualTo(length, 0, Length);
|
||||
Guard.IsBetweenOrEqualTo(length, 0, destination.Length);
|
||||
Guard.IsInRange(sourceOffset, 0, Length);
|
||||
Guard.IsLessThanOrEqualTo(sourceOffset + length, Length, nameof(sourceOffset));
|
||||
Guard.IsInRange(destinationOffset, 0, destination.Length);
|
||||
Guard.IsLessThanOrEqualTo(destinationOffset + length, destination.Length, nameof(destinationOffset));
|
||||
|
||||
if (destination is ConstantBuffer<T> buffer)
|
||||
using (GraphicsDevice.GetReferenceTracker().GetLease())
|
||||
using (GetReferenceTracker().GetLease())
|
||||
using (destination.GetReferenceTracker().GetLease())
|
||||
{
|
||||
using ID3D12ResourceMap sourceMap = D3D12Resource->Map();
|
||||
using ID3D12ResourceMap destinationMap = buffer.D3D12Resource->Map();
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
MemoryHelper.Copy<T>(
|
||||
source: sourceMap.Pointer,
|
||||
destination: destinationMap.Pointer,
|
||||
sourceElementOffset: (uint)sourceOffset,
|
||||
destinationElementOffset: (uint)destinationOffset,
|
||||
sourceElementPitchInBytes: (uint)GetPaddedSize(),
|
||||
destinationElementPitchInBytes: (uint)GetPaddedSize(),
|
||||
count: (uint)length);
|
||||
destination.ThrowIfDeviceMismatch(GraphicsDevice);
|
||||
|
||||
Guard.IsBetweenOrEqualTo(length, 0, Length);
|
||||
Guard.IsBetweenOrEqualTo(length, 0, destination.Length);
|
||||
Guard.IsInRange(sourceOffset, 0, Length);
|
||||
Guard.IsLessThanOrEqualTo(sourceOffset + length, Length, nameof(sourceOffset));
|
||||
Guard.IsInRange(destinationOffset, 0, destination.Length);
|
||||
Guard.IsLessThanOrEqualTo(destinationOffset + length, destination.Length, nameof(destinationOffset));
|
||||
|
||||
if (destination is ConstantBuffer<T> buffer)
|
||||
{
|
||||
using ID3D12ResourceMap sourceMap = D3D12Resource->Map();
|
||||
using ID3D12ResourceMap destinationMap = buffer.D3D12Resource->Map();
|
||||
|
||||
MemoryHelper.Copy<T>(
|
||||
source: sourceMap.Pointer,
|
||||
destination: destinationMap.Pointer,
|
||||
sourceElementOffset: (uint)sourceOffset,
|
||||
destinationElementOffset: (uint)destinationOffset,
|
||||
sourceElementPitchInBytes: (uint)GetPaddedSize(),
|
||||
destinationElementPitchInBytes: (uint)GetPaddedSize(),
|
||||
count: (uint)length);
|
||||
}
|
||||
else CopyToWithCpuBuffer(destination, sourceOffset, destinationOffset, length);
|
||||
}
|
||||
else CopyToWithCpuBuffer(destination, sourceOffset, destinationOffset, length);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
internal override unsafe void CopyFrom(ref T source, int offset, int length)
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
ThrowIfDisposed();
|
||||
|
||||
Guard.IsBetweenOrEqualTo(length, 0, Length);
|
||||
Guard.IsInRange(offset, 0, Length);
|
||||
Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset));
|
||||
|
||||
using ID3D12ResourceMap resource = D3D12Resource->Map();
|
||||
|
||||
fixed (void* sourcePointer = &source)
|
||||
using (GraphicsDevice.GetReferenceTracker().GetLease())
|
||||
using (GetReferenceTracker().GetLease())
|
||||
{
|
||||
MemoryHelper.Copy<T>(
|
||||
source: sourcePointer,
|
||||
destination: resource.Pointer,
|
||||
sourceElementOffset: 0,
|
||||
destinationElementOffset: (uint)offset,
|
||||
sourceElementPitchInBytes: (uint)sizeof(T),
|
||||
destinationElementPitchInBytes: (uint)GetPaddedSize(),
|
||||
count: (uint)length);
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
Guard.IsBetweenOrEqualTo(length, 0, Length);
|
||||
Guard.IsInRange(offset, 0, Length);
|
||||
Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset));
|
||||
|
||||
using ID3D12ResourceMap resource = D3D12Resource->Map();
|
||||
|
||||
fixed (void* sourcePointer = &source)
|
||||
{
|
||||
MemoryHelper.Copy<T>(
|
||||
source: sourcePointer,
|
||||
destination: resource.Pointer,
|
||||
sourceElementOffset: 0,
|
||||
destinationElementOffset: (uint)offset,
|
||||
sourceElementPitchInBytes: (uint)sizeof(T),
|
||||
destinationElementPitchInBytes: (uint)GetPaddedSize(),
|
||||
count: (uint)length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,32 +24,34 @@ partial class ReadWriteTexture2D<T, TPixel>
|
||||
/// <inheritdoc cref="ReadWriteTexture2DExtensions.AsReadOnly{T, TPixel}(ReadWriteTexture2D{T, TPixel})"/>
|
||||
internal IReadOnlyNormalizedTexture2D<TPixel> AsReadOnly()
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
ThrowIfDisposed();
|
||||
ThrowIfIsNotInReadOnlyState();
|
||||
|
||||
ReadOnly? readOnlyWrapper = this.readOnlyWrapper;
|
||||
|
||||
if (readOnlyWrapper is not null)
|
||||
using (GraphicsDevice.GetReferenceTracker().GetLease())
|
||||
using (GetReferenceTracker().GetLease())
|
||||
{
|
||||
return readOnlyWrapper;
|
||||
}
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
static ReadOnly InitializeWrapper(ReadWriteTexture2D<T, TPixel> texture)
|
||||
{
|
||||
return texture.readOnlyWrapper = new(texture);
|
||||
}
|
||||
ThrowIfIsNotInReadOnlyState();
|
||||
|
||||
return InitializeWrapper(this);
|
||||
ReadOnly? readOnlyWrapper = this.readOnlyWrapper;
|
||||
|
||||
if (readOnlyWrapper is not null)
|
||||
{
|
||||
return readOnlyWrapper;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
static ReadOnly InitializeWrapper(ReadWriteTexture2D<T, TPixel> texture)
|
||||
{
|
||||
return texture.readOnlyWrapper = new(texture);
|
||||
}
|
||||
|
||||
return InitializeWrapper(this);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void OnDispose()
|
||||
public override void Dispose()
|
||||
{
|
||||
base.OnDispose();
|
||||
base.Dispose();
|
||||
|
||||
this.readOnlyWrapper?.Dispose();
|
||||
}
|
||||
@@ -57,8 +59,13 @@ partial class ReadWriteTexture2D<T, TPixel>
|
||||
/// <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, IReadOnlyNormalizedTexture2D<TPixel>, GraphicsResourceHelper.IGraphicsResource
|
||||
private sealed unsafe class ReadOnly : ReferenceTracker.ITrackedObject, IDisposable, IReadOnlyNormalizedTexture2D<TPixel>, GraphicsResourceHelper.IGraphicsResource
|
||||
{
|
||||
/// <summary>
|
||||
/// The owning <see cref="ReferenceTracker"/> object for the current instance.
|
||||
/// </summary>
|
||||
private ReferenceTracker referenceTracker;
|
||||
|
||||
/// <summary>
|
||||
/// The owning <see cref="ReadWriteTexture2D{T, TPixel}"/> instance being wrapped.
|
||||
/// </summary>
|
||||
@@ -75,6 +82,7 @@ partial class ReadWriteTexture2D<T, TPixel>
|
||||
/// <param name="owner">The owning <see cref="ReadWriteTexture2D{T, TPixel}"/> instance to wrap.</param>
|
||||
public ReadOnly(ReadWriteTexture2D<T, TPixel> owner)
|
||||
{
|
||||
this.referenceTracker = new ReferenceTracker(this);
|
||||
this.owner = owner;
|
||||
|
||||
owner.GraphicsDevice.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles);
|
||||
@@ -106,12 +114,13 @@ partial class ReadWriteTexture2D<T, TPixel>
|
||||
/// <inheritdoc/>
|
||||
D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
using (this.referenceTracker.GetLease())
|
||||
using (this.owner.GetReferenceTracker().GetLease())
|
||||
{
|
||||
this.owner.ThrowIfDeviceMismatch(device);
|
||||
|
||||
this.owner.ThrowIfDisposed();
|
||||
this.owner.ThrowIfDeviceMismatch(device);
|
||||
|
||||
return this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle;
|
||||
return this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -123,12 +132,13 @@ partial class ReadWriteTexture2D<T, TPixel>
|
||||
/// <inheritdoc/>
|
||||
ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
using (this.referenceTracker.GetLease())
|
||||
using (this.owner.GetReferenceTracker().GetLease())
|
||||
{
|
||||
this.owner.ThrowIfDeviceMismatch(device);
|
||||
|
||||
this.owner.ThrowIfDisposed();
|
||||
this.owner.ThrowIfDeviceMismatch(device);
|
||||
|
||||
return this.owner.D3D12Resource;
|
||||
return this.owner.D3D12Resource;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -138,7 +148,21 @@ partial class ReadWriteTexture2D<T, TPixel>
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void OnDispose()
|
||||
public void Dispose()
|
||||
{
|
||||
this.referenceTracker.Dispose();
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
ref ReferenceTracker ReferenceTracker.ITrackedObject.GetReferenceTracker()
|
||||
{
|
||||
return ref this.referenceTracker;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ReferenceTracker.ITrackedObject.DangerousRelease()
|
||||
{
|
||||
if (this.owner.GraphicsDevice is GraphicsDevice device)
|
||||
{
|
||||
|
||||
@@ -24,35 +24,37 @@ partial class ReadWriteTexture2D<T>
|
||||
/// <inheritdoc cref="ReadWriteTexture2DExtensions.AsReadOnly(ReadWriteTexture2D{float})"/>
|
||||
public IReadOnlyTexture2D<T> AsReadOnly()
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
ThrowIfDisposed();
|
||||
ThrowIfIsNotInReadOnlyState();
|
||||
|
||||
ReadOnly? readOnlyWrapper = this.readOnlyWrapper;
|
||||
|
||||
if (readOnlyWrapper is not null)
|
||||
using (GraphicsDevice.GetReferenceTracker().GetLease())
|
||||
using (GetReferenceTracker().GetLease())
|
||||
{
|
||||
return readOnlyWrapper;
|
||||
}
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
// 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, control goes directly to the
|
||||
// helper that will initialize and return it, which translates to a single jump.
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
static ReadOnly InitializeWrapper(ReadWriteTexture2D<T> texture)
|
||||
{
|
||||
return texture.readOnlyWrapper = new(texture);
|
||||
}
|
||||
ThrowIfIsNotInReadOnlyState();
|
||||
|
||||
return InitializeWrapper(this);
|
||||
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, control goes directly to the
|
||||
// helper that will initialize and return it, which translates to a single jump.
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
static ReadOnly InitializeWrapper(ReadWriteTexture2D<T> texture)
|
||||
{
|
||||
return texture.readOnlyWrapper = new(texture);
|
||||
}
|
||||
|
||||
return InitializeWrapper(this);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void OnDispose()
|
||||
public override void Dispose()
|
||||
{
|
||||
base.OnDispose();
|
||||
base.Dispose();
|
||||
|
||||
this.readOnlyWrapper?.Dispose();
|
||||
}
|
||||
@@ -60,8 +62,13 @@ partial class ReadWriteTexture2D<T>
|
||||
/// <summary>
|
||||
/// A wrapper for a <see cref="ReadWriteTexture2D{T}"/> resource that has been temporarily transitioned to readonly.
|
||||
/// </summary>
|
||||
private sealed unsafe class ReadOnly : NativeObject, IReadOnlyTexture2D<T>, GraphicsResourceHelper.IGraphicsResource
|
||||
private sealed unsafe class ReadOnly : ReferenceTracker.ITrackedObject, IDisposable, IReadOnlyTexture2D<T>, GraphicsResourceHelper.IGraphicsResource
|
||||
{
|
||||
/// <summary>
|
||||
/// The owning <see cref="ReferenceTracker"/> object for the current instance.
|
||||
/// </summary>
|
||||
private ReferenceTracker referenceTracker;
|
||||
|
||||
/// <summary>
|
||||
/// The owning <see cref="ReadWriteTexture2D{T}"/> instance being wrapped.
|
||||
/// </summary>
|
||||
@@ -78,6 +85,7 @@ partial class ReadWriteTexture2D<T>
|
||||
/// <param name="owner">The owning <see cref="ReadWriteTexture2D{T}"/> instance to wrap.</param>
|
||||
public ReadOnly(ReadWriteTexture2D<T> owner)
|
||||
{
|
||||
this.referenceTracker = new ReferenceTracker(this);
|
||||
this.owner = owner;
|
||||
|
||||
owner.GraphicsDevice.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles);
|
||||
@@ -109,12 +117,13 @@ partial class ReadWriteTexture2D<T>
|
||||
/// <inheritdoc/>
|
||||
D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
using (this.referenceTracker.GetLease())
|
||||
using (this.owner.GetReferenceTracker().GetLease())
|
||||
{
|
||||
this.owner.ThrowIfDeviceMismatch(device);
|
||||
|
||||
this.owner.ThrowIfDisposed();
|
||||
this.owner.ThrowIfDeviceMismatch(device);
|
||||
|
||||
return this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle;
|
||||
return this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -126,12 +135,13 @@ partial class ReadWriteTexture2D<T>
|
||||
/// <inheritdoc/>
|
||||
ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
using (this.referenceTracker.GetLease())
|
||||
using (this.owner.GetReferenceTracker().GetLease())
|
||||
{
|
||||
this.owner.ThrowIfDeviceMismatch(device);
|
||||
|
||||
this.owner.ThrowIfDisposed();
|
||||
this.owner.ThrowIfDeviceMismatch(device);
|
||||
|
||||
return this.owner.D3D12Resource;
|
||||
return this.owner.D3D12Resource;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -141,7 +151,21 @@ partial class ReadWriteTexture2D<T>
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void OnDispose()
|
||||
public void Dispose()
|
||||
{
|
||||
this.referenceTracker.Dispose();
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
ref ReferenceTracker ReferenceTracker.ITrackedObject.GetReferenceTracker()
|
||||
{
|
||||
return ref this.referenceTracker;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ReferenceTracker.ITrackedObject.DangerousRelease()
|
||||
{
|
||||
if (this.owner.GraphicsDevice is GraphicsDevice device)
|
||||
{
|
||||
|
||||
@@ -24,32 +24,34 @@ partial class ReadWriteTexture3D<T, TPixel>
|
||||
/// <inheritdoc cref="ReadWriteTexture3DExtensions.AsReadOnly{T, TPixel}(ReadWriteTexture3D{T, TPixel})"/>
|
||||
public IReadOnlyNormalizedTexture3D<TPixel> AsReadOnly()
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
ThrowIfDisposed();
|
||||
ThrowIfIsNotInReadOnlyState();
|
||||
|
||||
ReadOnly? readOnlyWrapper = this.readOnlyWrapper;
|
||||
|
||||
if (readOnlyWrapper is not null)
|
||||
using (GraphicsDevice.GetReferenceTracker().GetLease())
|
||||
using (GetReferenceTracker().GetLease())
|
||||
{
|
||||
return readOnlyWrapper;
|
||||
}
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
static ReadOnly InitializeWrapper(ReadWriteTexture3D<T, TPixel> texture)
|
||||
{
|
||||
return texture.readOnlyWrapper = new(texture);
|
||||
}
|
||||
ThrowIfIsNotInReadOnlyState();
|
||||
|
||||
return InitializeWrapper(this);
|
||||
ReadOnly? readOnlyWrapper = this.readOnlyWrapper;
|
||||
|
||||
if (readOnlyWrapper is not null)
|
||||
{
|
||||
return readOnlyWrapper;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
static ReadOnly InitializeWrapper(ReadWriteTexture3D<T, TPixel> texture)
|
||||
{
|
||||
return texture.readOnlyWrapper = new(texture);
|
||||
}
|
||||
|
||||
return InitializeWrapper(this);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void OnDispose()
|
||||
public override void Dispose()
|
||||
{
|
||||
base.OnDispose();
|
||||
base.Dispose();
|
||||
|
||||
this.readOnlyWrapper?.Dispose();
|
||||
}
|
||||
@@ -57,8 +59,13 @@ partial class ReadWriteTexture3D<T, TPixel>
|
||||
/// <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, IReadOnlyNormalizedTexture3D<TPixel>, GraphicsResourceHelper.IGraphicsResource
|
||||
private sealed unsafe class ReadOnly : ReferenceTracker.ITrackedObject, IDisposable, IReadOnlyNormalizedTexture3D<TPixel>, GraphicsResourceHelper.IGraphicsResource
|
||||
{
|
||||
/// <summary>
|
||||
/// The owning <see cref="ReferenceTracker"/> object for the current instance.
|
||||
/// </summary>
|
||||
private ReferenceTracker referenceTracker;
|
||||
|
||||
/// <summary>
|
||||
/// The owning <see cref="ReadWriteTexture3D{T, TPixel}"/> instance being wrapped.
|
||||
/// </summary>
|
||||
@@ -75,6 +82,7 @@ partial class ReadWriteTexture3D<T, TPixel>
|
||||
/// <param name="owner">The owning <see cref="ReadWriteTexture3D{T, TPixel}"/> instance to wrap.</param>
|
||||
public ReadOnly(ReadWriteTexture3D<T, TPixel> owner)
|
||||
{
|
||||
this.referenceTracker = new ReferenceTracker(this);
|
||||
this.owner = owner;
|
||||
|
||||
owner.GraphicsDevice.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles);
|
||||
@@ -109,12 +117,13 @@ partial class ReadWriteTexture3D<T, TPixel>
|
||||
/// <inheritdoc/>
|
||||
D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
using (this.referenceTracker.GetLease())
|
||||
using (this.owner.GetReferenceTracker().GetLease())
|
||||
{
|
||||
this.owner.ThrowIfDeviceMismatch(device);
|
||||
|
||||
this.owner.ThrowIfDisposed();
|
||||
this.owner.ThrowIfDeviceMismatch(device);
|
||||
|
||||
return this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle;
|
||||
return this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -126,12 +135,13 @@ partial class ReadWriteTexture3D<T, TPixel>
|
||||
/// <inheritdoc/>
|
||||
ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
using (this.referenceTracker.GetLease())
|
||||
using (this.owner.GetReferenceTracker().GetLease())
|
||||
{
|
||||
this.owner.ThrowIfDeviceMismatch(device);
|
||||
|
||||
this.owner.ThrowIfDisposed();
|
||||
this.owner.ThrowIfDeviceMismatch(device);
|
||||
|
||||
return this.owner.D3D12Resource;
|
||||
return this.owner.D3D12Resource;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -141,7 +151,21 @@ partial class ReadWriteTexture3D<T, TPixel>
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void OnDispose()
|
||||
public void Dispose()
|
||||
{
|
||||
this.referenceTracker.Dispose();
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
ref ReferenceTracker ReferenceTracker.ITrackedObject.GetReferenceTracker()
|
||||
{
|
||||
return ref this.referenceTracker;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ReferenceTracker.ITrackedObject.DangerousRelease()
|
||||
{
|
||||
if (this.owner.GraphicsDevice is GraphicsDevice device)
|
||||
{
|
||||
|
||||
@@ -24,32 +24,34 @@ partial class ReadWriteTexture3D<T>
|
||||
/// <inheritdoc cref="ReadWriteTexture3DExtensions.AsReadOnly(ReadWriteTexture3D{float})"/>
|
||||
public IReadOnlyTexture3D<T> AsReadOnly()
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
ThrowIfDisposed();
|
||||
ThrowIfIsNotInReadOnlyState();
|
||||
|
||||
ReadOnly? readOnlyWrapper = this.readOnlyWrapper;
|
||||
|
||||
if (readOnlyWrapper is not null)
|
||||
using (GraphicsDevice.GetReferenceTracker().GetLease())
|
||||
using (GetReferenceTracker().GetLease())
|
||||
{
|
||||
return readOnlyWrapper;
|
||||
}
|
||||
GraphicsDevice.ThrowIfDeviceLost();
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
static ReadOnly InitializeWrapper(ReadWriteTexture3D<T> texture)
|
||||
{
|
||||
return texture.readOnlyWrapper = new(texture);
|
||||
}
|
||||
ThrowIfIsNotInReadOnlyState();
|
||||
|
||||
return InitializeWrapper(this);
|
||||
ReadOnly? readOnlyWrapper = this.readOnlyWrapper;
|
||||
|
||||
if (readOnlyWrapper is not null)
|
||||
{
|
||||
return readOnlyWrapper;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
static ReadOnly InitializeWrapper(ReadWriteTexture3D<T> texture)
|
||||
{
|
||||
return texture.readOnlyWrapper = new(texture);
|
||||
}
|
||||
|
||||
return InitializeWrapper(this);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void OnDispose()
|
||||
public override void Dispose()
|
||||
{
|
||||
base.OnDispose();
|
||||
base.Dispose();
|
||||
|
||||
this.readOnlyWrapper?.Dispose();
|
||||
}
|
||||
@@ -57,8 +59,13 @@ partial class ReadWriteTexture3D<T>
|
||||
/// <summary>
|
||||
/// A wrapper for a <see cref="ReadWriteTexture3D{T}"/> resource that has been temporarily transitioned to readonly.
|
||||
/// </summary>
|
||||
private sealed unsafe class ReadOnly : NativeObject, IReadOnlyTexture3D<T>, GraphicsResourceHelper.IGraphicsResource
|
||||
private sealed unsafe class ReadOnly : ReferenceTracker.ITrackedObject, IDisposable, IReadOnlyTexture3D<T>, GraphicsResourceHelper.IGraphicsResource
|
||||
{
|
||||
/// <summary>
|
||||
/// The owning <see cref="ReferenceTracker"/> object for the current instance.
|
||||
/// </summary>
|
||||
private ReferenceTracker referenceTracker;
|
||||
|
||||
/// <summary>
|
||||
/// The owning <see cref="ReadWriteTexture3D{T}"/> instance being wrapped.
|
||||
/// </summary>
|
||||
@@ -75,6 +82,7 @@ partial class ReadWriteTexture3D<T>
|
||||
/// <param name="owner">The owning <see cref="ReadWriteTexture3D{T}"/> instance to wrap.</param>
|
||||
public ReadOnly(ReadWriteTexture3D<T> owner)
|
||||
{
|
||||
this.referenceTracker = new ReferenceTracker(this);
|
||||
this.owner = owner;
|
||||
|
||||
owner.GraphicsDevice.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles);
|
||||
@@ -109,12 +117,13 @@ partial class ReadWriteTexture3D<T>
|
||||
/// <inheritdoc/>
|
||||
D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
using (this.referenceTracker.GetLease())
|
||||
using (this.owner.GetReferenceTracker().GetLease())
|
||||
{
|
||||
this.owner.ThrowIfDeviceMismatch(device);
|
||||
|
||||
this.owner.ThrowIfDisposed();
|
||||
this.owner.ThrowIfDeviceMismatch(device);
|
||||
|
||||
return this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle;
|
||||
return this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -126,12 +135,13 @@ partial class ReadWriteTexture3D<T>
|
||||
/// <inheritdoc/>
|
||||
ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
using (this.referenceTracker.GetLease())
|
||||
using (this.owner.GetReferenceTracker().GetLease())
|
||||
{
|
||||
this.owner.ThrowIfDeviceMismatch(device);
|
||||
|
||||
this.owner.ThrowIfDisposed();
|
||||
this.owner.ThrowIfDeviceMismatch(device);
|
||||
|
||||
return this.owner.D3D12Resource;
|
||||
return this.owner.D3D12Resource;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -141,7 +151,21 @@ partial class ReadWriteTexture3D<T>
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void OnDispose()
|
||||
public void Dispose()
|
||||
{
|
||||
this.referenceTracker.Dispose();
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
ref ReferenceTracker ReferenceTracker.ITrackedObject.GetReferenceTracker()
|
||||
{
|
||||
return ref this.referenceTracker;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ReferenceTracker.ITrackedObject.DangerousRelease()
|
||||
{
|
||||
if (this.owner.GraphicsDevice is GraphicsDevice device)
|
||||
{
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする