Move D3D12MA usage behind USE_D3D12MA flag
このコミットが含まれているのは:
1 親
1951406270
コミット
f57f44985c
12ファイル変更
+356
-67
検索条件に一致するファイルはありません
@@ -9,7 +9,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Toolkit.Diagnostics" Version="7.1.1" />
|
||||
<PackageReference Include="TerraFX.Interop.D3D12MemoryAllocator" Version="2.0.0-beta3" />
|
||||
<PackageReference Include="TerraFX.Interop.Windows" Version="10.0.20348-rc2" />
|
||||
</ItemGroup>
|
||||
|
||||
<Choose>
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using ComputeSharp.Core.Extensions;
|
||||
#if USE_D3D12MA
|
||||
|
||||
using ComputeSharp.Core.Extensions;
|
||||
using Microsoft.Toolkit.Diagnostics;
|
||||
using TerraFX.Interop.DirectX;
|
||||
using TerraFX.Interop.Windows;
|
||||
@@ -196,3 +198,5 @@ internal static unsafe class AllocatorExtensions
|
||||
return allocation.Move();
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,16 +1,34 @@
|
||||
using System;
|
||||
using System.Diagnostics.Contracts;
|
||||
#if USE_D3D12MA
|
||||
using System.Runtime.CompilerServices;
|
||||
#endif
|
||||
using ComputeSharp.Core.Extensions;
|
||||
#if !USE_D3D12MA
|
||||
using ComputeSharp.Graphics.Resources.Enums;
|
||||
using Microsoft.Toolkit.Diagnostics;
|
||||
#endif
|
||||
using TerraFX.Interop.DirectX;
|
||||
using TerraFX.Interop.Windows;
|
||||
using static TerraFX.Interop.DirectX.D3D12_COMMAND_QUEUE_FLAGS;
|
||||
using static TerraFX.Interop.DirectX.D3D12_COMMAND_QUEUE_PRIORITY;
|
||||
#if !USE_D3D12MA
|
||||
using static TerraFX.Interop.DirectX.D3D12_CPU_PAGE_PROPERTY;
|
||||
#endif
|
||||
using static TerraFX.Interop.DirectX.D3D12_DESCRIPTOR_HEAP_FLAGS;
|
||||
using static TerraFX.Interop.DirectX.D3D12_DESCRIPTOR_HEAP_TYPE;
|
||||
using static TerraFX.Interop.DirectX.D3D12_FEATURE;
|
||||
using static TerraFX.Interop.DirectX.D3D12_FENCE_FLAGS;
|
||||
#if !USE_D3D12MA
|
||||
using static TerraFX.Interop.DirectX.D3D12_HEAP_FLAGS;
|
||||
using static TerraFX.Interop.DirectX.D3D12_MEMORY_POOL;
|
||||
#endif
|
||||
using static TerraFX.Interop.DirectX.D3D12_MESSAGE_ID;
|
||||
#if !USE_D3D12MA
|
||||
using static TerraFX.Interop.DirectX.D3D12_HEAP_TYPE;
|
||||
using static TerraFX.Interop.DirectX.D3D12_RESOURCE_FLAGS;
|
||||
using static TerraFX.Interop.DirectX.D3D12_RESOURCE_STATES;
|
||||
#endif
|
||||
using static TerraFX.Interop.DirectX.D3D12_SRV_DIMENSION;
|
||||
using static TerraFX.Interop.DirectX.D3D12_UAV_DIMENSION;
|
||||
|
||||
@@ -21,6 +39,7 @@ namespace ComputeSharp.Graphics.Extensions;
|
||||
/// </summary>
|
||||
internal static unsafe class ID3D12DeviceExtensions
|
||||
{
|
||||
#if USE_D3D12MA
|
||||
/// <summary>
|
||||
/// Creates a <see cref="D3D12MA_Allocator"/> instance for a given device.
|
||||
/// </summary>
|
||||
@@ -39,6 +58,198 @@ internal static unsafe class ID3D12DeviceExtensions
|
||||
|
||||
return allocator.Move();
|
||||
}
|
||||
#else
|
||||
/// <summary>
|
||||
/// Creates a committed resource for a given buffer type.
|
||||
/// </summary>
|
||||
/// <param name="d3D12Device">The <see cref="ID3D12Device"/> instance in use.</param>
|
||||
/// <param name="resourceType">The resource type currently in use.</param>
|
||||
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
|
||||
/// <param name="sizeInBytes">The size in bytes of the current buffer.</param>
|
||||
/// <param name="isCacheCoherentUMA">Indicates whether or not the current device has a cache coherent UMA architecture.</param>
|
||||
/// <returns>An <see cref="ID3D12Resource"/> reference for the current buffer.</returns>
|
||||
public static ComPtr<ID3D12Resource> CreateCommittedResource(
|
||||
this ref ID3D12Device d3D12Device,
|
||||
ResourceType resourceType,
|
||||
AllocationMode allocationMode,
|
||||
ulong sizeInBytes,
|
||||
bool isCacheCoherentUMA)
|
||||
{
|
||||
(D3D12_HEAP_TYPE d3D12HeapType,
|
||||
D3D12_RESOURCE_FLAGS d3D12ResourceFlags,
|
||||
D3D12_RESOURCE_STATES d3D12ResourceStates) = resourceType switch
|
||||
{
|
||||
ResourceType.Constant => (D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_GENERIC_READ),
|
||||
ResourceType.ReadOnly => (D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON),
|
||||
ResourceType.ReadWrite => (D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COMMON),
|
||||
ResourceType.ReadBack => (D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COPY_DEST),
|
||||
ResourceType.Upload => (D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_GENERIC_READ),
|
||||
_ => ThrowHelper.ThrowArgumentException<(D3D12_HEAP_TYPE, D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>()
|
||||
};
|
||||
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = default;
|
||||
|
||||
D3D12_HEAP_PROPERTIES d3D12HeapProperties;
|
||||
d3D12HeapProperties.CreationNodeMask = 1;
|
||||
d3D12HeapProperties.VisibleNodeMask = 1;
|
||||
|
||||
if (isCacheCoherentUMA)
|
||||
{
|
||||
d3D12HeapProperties.Type = D3D12_HEAP_TYPE_CUSTOM;
|
||||
d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_WRITE_BACK;
|
||||
d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_L0;
|
||||
}
|
||||
else
|
||||
{
|
||||
d3D12HeapProperties.Type = d3D12HeapType;
|
||||
d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
|
||||
d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
|
||||
}
|
||||
|
||||
D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Buffer(sizeInBytes, d3D12ResourceFlags);
|
||||
|
||||
d3D12Device.CreateCommittedResource(
|
||||
&d3D12HeapProperties,
|
||||
D3D12_HEAP_FLAG_NONE,
|
||||
&d3D12ResourceDescription,
|
||||
d3D12ResourceStates,
|
||||
null,
|
||||
Windows.__uuidof<ID3D12Resource>(),
|
||||
d3D12Resource.GetVoidAddressOf()).Assert();
|
||||
|
||||
return d3D12Resource.Move();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a committed resource for a given 2D texture type.
|
||||
/// </summary>
|
||||
/// <param name="d3D12Device">The <see cref="ID3D12Device"/> instance in use.</param>
|
||||
/// <param name="resourceType">The resource type currently in use.</param>
|
||||
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
|
||||
/// <param name="dxgiFormat">The <see cref="DXGI_FORMAT"/> value to use.</param>
|
||||
/// <param name="width">The width of the texture resource.</param>
|
||||
/// <param name="height">The height of the texture resource.</param>
|
||||
/// <param name="isCacheCoherentUMA">Indicates whether or not the current device has a cache coherent UMA architecture.</param>
|
||||
/// <param name="d3D12ResourceStates">The default <see cref="D3D12_RESOURCE_STATES"/> value for the resource.</param>
|
||||
/// <returns>An <see cref="ID3D12Resource"/> reference for the current texture.</returns>
|
||||
public static ComPtr<ID3D12Resource> CreateCommittedResource(
|
||||
this ref ID3D12Device d3D12Device,
|
||||
ResourceType resourceType,
|
||||
AllocationMode allocationMode,
|
||||
DXGI_FORMAT dxgiFormat,
|
||||
uint width,
|
||||
uint height,
|
||||
bool isCacheCoherentUMA,
|
||||
out D3D12_RESOURCE_STATES d3D12ResourceStates)
|
||||
{
|
||||
D3D12_RESOURCE_FLAGS d3D12ResourceFlags;
|
||||
|
||||
(d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch
|
||||
{
|
||||
ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON),
|
||||
ResourceType.ReadWrite => (D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_UNORDERED_ACCESS),
|
||||
_ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>()
|
||||
};
|
||||
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = default;
|
||||
|
||||
D3D12_HEAP_PROPERTIES d3D12HeapProperties;
|
||||
d3D12HeapProperties.CreationNodeMask = 1;
|
||||
d3D12HeapProperties.VisibleNodeMask = 1;
|
||||
|
||||
if (isCacheCoherentUMA)
|
||||
{
|
||||
d3D12HeapProperties.Type = D3D12_HEAP_TYPE_CUSTOM;
|
||||
d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_WRITE_BACK;
|
||||
d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_L0;
|
||||
}
|
||||
else
|
||||
{
|
||||
d3D12HeapProperties.Type = D3D12_HEAP_TYPE_DEFAULT;
|
||||
d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
|
||||
d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
|
||||
}
|
||||
|
||||
D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex2D(dxgiFormat, width, height, mipLevels: 1, flags: d3D12ResourceFlags);
|
||||
|
||||
d3D12Device.CreateCommittedResource(
|
||||
&d3D12HeapProperties,
|
||||
D3D12_HEAP_FLAG_NONE,
|
||||
&d3D12ResourceDescription,
|
||||
d3D12ResourceStates,
|
||||
null,
|
||||
Windows.__uuidof<ID3D12Resource>(),
|
||||
d3D12Resource.GetVoidAddressOf()).Assert();
|
||||
|
||||
return d3D12Resource.Move();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a committed resource for a given 3D texture type.
|
||||
/// </summary>
|
||||
/// <param name="d3D12Device">The <see cref="ID3D12Device"/> instance in use.</param>
|
||||
/// <param name="resourceType">The resource type currently in use.</param>
|
||||
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
|
||||
/// <param name="dxgiFormat">The <see cref="DXGI_FORMAT"/> value to use.</param>
|
||||
/// <param name="width">The width of the texture resource.</param>
|
||||
/// <param name="height">The height of the texture resource.</param>
|
||||
/// <param name="depth">The depth of the texture resource.</param>
|
||||
/// <param name="isCacheCoherentUMA">Indicates whether or not the current device has a cache coherent UMA architecture.</param>
|
||||
/// <param name="d3D12ResourceStates">The default <see cref="D3D12_RESOURCE_STATES"/> value for the resource.</param>
|
||||
/// <returns>An <see cref="ID3D12Resource"/> reference for the current texture.</returns>
|
||||
public static ComPtr<ID3D12Resource> CreateCommittedResource(
|
||||
this ref ID3D12Device d3D12Device,
|
||||
ResourceType resourceType,
|
||||
AllocationMode allocationMode,
|
||||
DXGI_FORMAT dxgiFormat,
|
||||
uint width,
|
||||
uint height,
|
||||
ushort depth,
|
||||
bool isCacheCoherentUMA,
|
||||
out D3D12_RESOURCE_STATES d3D12ResourceStates)
|
||||
{
|
||||
D3D12_RESOURCE_FLAGS d3D12ResourceFlags;
|
||||
|
||||
(d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch
|
||||
{
|
||||
ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON),
|
||||
ResourceType.ReadWrite => (D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_UNORDERED_ACCESS),
|
||||
_ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>()
|
||||
};
|
||||
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = default;
|
||||
|
||||
D3D12_HEAP_PROPERTIES d3D12HeapProperties;
|
||||
d3D12HeapProperties.CreationNodeMask = 1;
|
||||
d3D12HeapProperties.VisibleNodeMask = 1;
|
||||
|
||||
if (isCacheCoherentUMA)
|
||||
{
|
||||
d3D12HeapProperties.Type = D3D12_HEAP_TYPE_CUSTOM;
|
||||
d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_WRITE_BACK;
|
||||
d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_L0;
|
||||
}
|
||||
else
|
||||
{
|
||||
d3D12HeapProperties.Type = D3D12_HEAP_TYPE_DEFAULT;
|
||||
d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
|
||||
d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
|
||||
}
|
||||
|
||||
D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex3D(dxgiFormat, width, height, depth, mipLevels: 1, flags: d3D12ResourceFlags);
|
||||
|
||||
d3D12Device.CreateCommittedResource(
|
||||
&d3D12HeapProperties,
|
||||
D3D12_HEAP_FLAG_NONE,
|
||||
&d3D12ResourceDescription,
|
||||
d3D12ResourceStates,
|
||||
null,
|
||||
Windows.__uuidof<ID3D12Resource>(),
|
||||
d3D12Resource.GetVoidAddressOf()).Assert();
|
||||
|
||||
return d3D12Resource.Move();
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ID3D12InfoQueue"/> for a given device.
|
||||
|
||||
@@ -74,6 +74,7 @@ public sealed unsafe class GraphicsDevice : NativeObject
|
||||
/// </summary>
|
||||
private ulong nextD3D12CopyFenceValue;
|
||||
|
||||
#if USE_D3D12MA
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12MA_Allocator"/> in use associated to the current device.
|
||||
/// </summary>
|
||||
@@ -83,6 +84,7 @@ public sealed unsafe class GraphicsDevice : NativeObject
|
||||
/// The <see cref="D3D12MA_Pool"/> instance in use, if <see cref="IsCacheCoherentUMA"/> is <see langword="true"/>.
|
||||
/// </summary>
|
||||
private ComPtr<D3D12MA_Pool> pool;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="GraphicsDevice"/> instance for the input <see cref="ID3D12Device"/>.
|
||||
@@ -118,12 +120,14 @@ public sealed unsafe class GraphicsDevice : NativeObject
|
||||
|
||||
IsCacheCoherentUMA = d3D12Architecture1Data.CacheCoherentUMA != 0;
|
||||
|
||||
#if USE_D3D12MA
|
||||
this.allocator = d3D12Device->CreateAllocator(dxgiAdapter);
|
||||
|
||||
if (IsCacheCoherentUMA)
|
||||
{
|
||||
this.pool = this.allocator.Get()->CreatePoolForCacheCoherentUMA();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -167,6 +171,7 @@ public sealed unsafe class GraphicsDevice : NativeObject
|
||||
/// </summary>
|
||||
internal ID3D12Device* D3D12Device => this.d3D12Device;
|
||||
|
||||
#if USE_D3D12MA
|
||||
/// <summary>
|
||||
/// Gets the underlying <see cref="D3D12MA_Allocator"/> wrapped by the current instance.
|
||||
/// </summary>
|
||||
@@ -176,6 +181,7 @@ public sealed unsafe class GraphicsDevice : NativeObject
|
||||
/// Gets the underlying <see cref="D3D12MA_Pool"/> wrapped by the current instance, if any.
|
||||
/// </summary>
|
||||
internal D3D12MA_Pool* Pool => this.pool;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether or not the current device has a cache coherent UMA architecture.
|
||||
@@ -251,8 +257,10 @@ public sealed unsafe class GraphicsDevice : NativeObject
|
||||
/// </summary>
|
||||
internal void RegisterAllocatedResource()
|
||||
{
|
||||
#if USE_D3D12MA
|
||||
this.pool.AddRef();
|
||||
this.allocator.AddRef();
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -260,8 +268,10 @@ public sealed unsafe class GraphicsDevice : NativeObject
|
||||
/// </summary>
|
||||
internal void UnregisterAllocatedResource()
|
||||
{
|
||||
#if USE_D3D12MA
|
||||
this.pool.Release();
|
||||
this.allocator.Release();
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ID3D12DescriptorHandleAllocator.Rent"/>
|
||||
@@ -394,8 +404,10 @@ public sealed unsafe class GraphicsDevice : NativeObject
|
||||
this.computeCommandListPool.Dispose();
|
||||
this.copyCommandListPool.Dispose();
|
||||
this.shaderResourceViewDescriptorAllocator.Dispose();
|
||||
#if USE_D3D12MA
|
||||
this.pool.Dispose();
|
||||
this.allocator.Dispose();
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Runtime.CompilerServices;
|
||||
using TerraFX.Interop.DirectX;
|
||||
using TerraFX.Interop.Windows;
|
||||
using static TerraFX.Interop.DirectX.D3D12_FEATURE;
|
||||
|
||||
namespace ComputeSharp.Graphics.Helpers;
|
||||
|
||||
/// <summary>
|
||||
/// A helper <see langword="class"/> exposing some runtime or OS dependent D3D12 features.
|
||||
/// </summary>
|
||||
internal static class D3D12FeatureHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// The currently supported <see cref="D3D12_HEAP_FLAGS.D3D12_HEAP_FLAG_CREATE_NOT_ZEROED"/> flag (or fallback).
|
||||
/// </summary>
|
||||
private static readonly D3D12_HEAP_FLAGS D3D12_HEAP_FLAG_CREATE_NOT_ZEROED = GetD3D12_HEAP_FLAG_CREATE_NOT_ZEROED();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="D3D12_HEAP_FLAGS"/> value for a given <see cref="AllocationMode"/> setting.
|
||||
/// </summary>
|
||||
/// <param name="allocationMode">The allocation mode to use for creating new resources.</param>
|
||||
/// <returns>The <see cref="D3D12_HEAP_FLAGS"/> matching <paramref name="allocationMode"/> that is currently supported</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static D3D12_HEAP_FLAGS GetD3D12HeapFlags(AllocationMode allocationMode)
|
||||
{
|
||||
if (allocationMode == AllocationMode.Default)
|
||||
{
|
||||
return D3D12_HEAP_FLAG_CREATE_NOT_ZEROED;
|
||||
}
|
||||
|
||||
return D3D12_HEAP_FLAGS.D3D12_HEAP_FLAG_NONE;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the <see cref="D3D12_HEAP_FLAG_CREATE_NOT_ZEROED"/> field.
|
||||
/// </summary>
|
||||
/// <returns>The currently supported <see cref="D3D12_HEAP_FLAGS.D3D12_HEAP_FLAG_CREATE_NOT_ZEROED"/> flag.</returns>
|
||||
private static unsafe D3D12_HEAP_FLAGS GetD3D12_HEAP_FLAG_CREATE_NOT_ZEROED()
|
||||
{
|
||||
lock (DeviceHelper.DevicesCache)
|
||||
{
|
||||
foreach (KeyValuePair<Luid, GraphicsDevice> pair in DeviceHelper.DevicesCache)
|
||||
{
|
||||
D3D12_FEATURE_DATA_D3D12_OPTIONS7 d3D12Options7;
|
||||
|
||||
int result = pair.Value.D3D12Device->CheckFeatureSupport(
|
||||
D3D12_FEATURE_D3D12_OPTIONS7,
|
||||
&d3D12Options7,
|
||||
(uint)sizeof(D3D12_FEATURE_DATA_D3D12_OPTIONS7));
|
||||
|
||||
if (result == S.S_OK)
|
||||
{
|
||||
return D3D12_HEAP_FLAGS.D3D12_HEAP_FLAG_CREATE_NOT_ZEROED;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return D3D12_HEAP_FLAGS.D3D12_HEAP_FLAG_NONE;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,10 +21,12 @@ namespace ComputeSharp.Resources;
|
||||
public unsafe abstract class Buffer<T> : NativeObject
|
||||
where T : unmanaged
|
||||
{
|
||||
#if USE_D3D12MA
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12MA_Allocation"/> instance used to retrieve <see cref="d3D12Resource"/>.
|
||||
/// </summary>
|
||||
private ComPtr<D3D12MA_Allocation> allocation;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ID3D12Resource"/> instance currently mapped.
|
||||
@@ -81,8 +83,12 @@ public unsafe abstract class Buffer<T> : NativeObject
|
||||
GraphicsDevice = device;
|
||||
Length = length;
|
||||
|
||||
#if USE_D3D12MA
|
||||
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, allocationMode, (ulong)effectiveSizeInBytes, device.IsCacheCoherentUMA);
|
||||
#endif
|
||||
|
||||
device.RegisterAllocatedResource();
|
||||
device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle);
|
||||
@@ -181,7 +187,9 @@ public unsafe abstract class Buffer<T> : NativeObject
|
||||
protected override void OnDispose()
|
||||
{
|
||||
this.d3D12Resource.Dispose();
|
||||
#if USE_D3D12MA
|
||||
this.allocation.Dispose();
|
||||
#endif
|
||||
|
||||
if (GraphicsDevice is GraphicsDevice device)
|
||||
{
|
||||
|
||||
@@ -61,15 +61,31 @@ public abstract class StructuredBuffer<T> : Buffer<T>
|
||||
nint byteOffset = (nint)sourceOffset * sizeof(T);
|
||||
nint byteLength = count * sizeof(T);
|
||||
|
||||
#if USE_D3D12MA
|
||||
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,
|
||||
AllocationMode.Default,
|
||||
(ulong)byteLength,
|
||||
GraphicsDevice.IsCacheCoherentUMA);
|
||||
#endif
|
||||
|
||||
using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY))
|
||||
{
|
||||
#if USE_D3D12MA
|
||||
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 USE_D3D12MA
|
||||
using ID3D12ResourceMap resource = allocation.Get()->GetResource()->Map();
|
||||
#else
|
||||
using ID3D12ResourceMap resource = d3D12Resource.Get()->Map();
|
||||
#endif
|
||||
|
||||
fixed (void* destinationPointer = &destination)
|
||||
{
|
||||
@@ -204,9 +220,21 @@ public abstract class StructuredBuffer<T> : Buffer<T>
|
||||
nint byteOffset = (nint)offset * sizeof(T);
|
||||
nint byteLength = length * sizeof(T);
|
||||
|
||||
#if USE_D3D12MA
|
||||
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,
|
||||
AllocationMode.Default,
|
||||
(ulong)byteLength,
|
||||
GraphicsDevice.IsCacheCoherentUMA);
|
||||
#endif
|
||||
|
||||
#if USE_D3D12MA
|
||||
using (ID3D12ResourceMap resource = allocation.Get()->GetResource()->Map())
|
||||
#else
|
||||
using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
|
||||
#endif
|
||||
fixed (void* sourcePointer = &source)
|
||||
{
|
||||
MemoryHelper.Copy<T>(
|
||||
@@ -221,7 +249,11 @@ public abstract class StructuredBuffer<T> : Buffer<T>
|
||||
|
||||
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
|
||||
|
||||
#if USE_D3D12MA
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,10 +26,12 @@ namespace ComputeSharp.Resources;
|
||||
public unsafe abstract class Texture2D<T> : NativeObject, GraphicsResourceHelper.IGraphicsResource
|
||||
where T : unmanaged
|
||||
{
|
||||
#if USE_D3D12MA
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12MA_Allocation"/> instance used to retrieve <see cref="d3D12Resource"/>.
|
||||
/// </summary>
|
||||
private ComPtr<D3D12MA_Allocation> allocation;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ID3D12Resource"/> instance currently mapped.
|
||||
@@ -84,6 +86,7 @@ public unsafe abstract class Texture2D<T> : NativeObject, GraphicsResourceHelper
|
||||
|
||||
GraphicsDevice = device;
|
||||
|
||||
#if USE_D3D12MA
|
||||
this.allocation = device.Allocator->CreateResource(
|
||||
device.Pool,
|
||||
resourceType,
|
||||
@@ -94,6 +97,16 @@ public unsafe abstract class Texture2D<T> : NativeObject, GraphicsResourceHelper
|
||||
out this.d3D12ResourceState);
|
||||
|
||||
this.d3D12Resource = new ComPtr<ID3D12Resource>(this.allocation.Get()->GetResource());
|
||||
#else
|
||||
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(
|
||||
resourceType,
|
||||
allocationMode,
|
||||
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
|
||||
@@ -174,6 +187,7 @@ public unsafe abstract class Texture2D<T> : NativeObject, GraphicsResourceHelper
|
||||
out ulong rowSizeInBytes,
|
||||
out ulong totalSizeInBytes);
|
||||
|
||||
#if USE_D3D12MA
|
||||
using ComPtr<D3D12MA_Allocation> allocation = GraphicsDevice.Allocator->CreateResource(
|
||||
GraphicsDevice.Pool,
|
||||
ResourceType.ReadBack,
|
||||
@@ -181,6 +195,13 @@ public unsafe abstract class Texture2D<T> : NativeObject, GraphicsResourceHelper
|
||||
totalSizeInBytes);
|
||||
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = new(allocation.Get()->GetResource());
|
||||
#else
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
|
||||
ResourceType.ReadBack,
|
||||
AllocationMode.Default,
|
||||
totalSizeInBytes,
|
||||
GraphicsDevice.IsCacheCoherentUMA);
|
||||
#endif
|
||||
|
||||
using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType))
|
||||
{
|
||||
@@ -387,6 +408,7 @@ public unsafe abstract class Texture2D<T> : NativeObject, GraphicsResourceHelper
|
||||
out ulong rowSizeInBytes,
|
||||
out ulong totalSizeInBytes);
|
||||
|
||||
#if USE_D3D12MA
|
||||
using ComPtr<D3D12MA_Allocation> allocation = GraphicsDevice.Allocator->CreateResource(
|
||||
GraphicsDevice.Pool,
|
||||
ResourceType.Upload,
|
||||
@@ -394,6 +416,13 @@ public unsafe abstract class Texture2D<T> : NativeObject, GraphicsResourceHelper
|
||||
totalSizeInBytes);
|
||||
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = new(allocation.Get()->GetResource());
|
||||
#else
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
|
||||
ResourceType.Upload,
|
||||
AllocationMode.Default,
|
||||
totalSizeInBytes,
|
||||
GraphicsDevice.IsCacheCoherentUMA);
|
||||
#endif
|
||||
|
||||
using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
|
||||
fixed (void* sourcePointer = &source)
|
||||
@@ -503,7 +532,9 @@ public unsafe abstract class Texture2D<T> : NativeObject, GraphicsResourceHelper
|
||||
protected override void OnDispose()
|
||||
{
|
||||
this.d3D12Resource.Dispose();
|
||||
#if USE_D3D12MA
|
||||
this.allocation.Dispose();
|
||||
#endif
|
||||
|
||||
if (GraphicsDevice is GraphicsDevice device)
|
||||
{
|
||||
|
||||
@@ -26,10 +26,12 @@ namespace ComputeSharp.Resources;
|
||||
public unsafe abstract class Texture3D<T> : NativeObject, GraphicsResourceHelper.IGraphicsResource
|
||||
where T : unmanaged
|
||||
{
|
||||
#if USE_D3D12MA
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12MA_Allocation"/> instance used to retrieve <see cref="d3D12Resource"/>.
|
||||
/// </summary>
|
||||
private ComPtr<D3D12MA_Allocation> allocation;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ID3D12Resource"/> instance currently mapped.
|
||||
@@ -86,6 +88,7 @@ public unsafe abstract class Texture3D<T> : NativeObject, GraphicsResourceHelper
|
||||
|
||||
GraphicsDevice = device;
|
||||
|
||||
#if USE_D3D12MA
|
||||
this.allocation = device.Allocator->CreateResource(
|
||||
device.Pool,
|
||||
resourceType,
|
||||
@@ -97,6 +100,17 @@ public unsafe abstract class Texture3D<T> : NativeObject, GraphicsResourceHelper
|
||||
out this.d3D12ResourceState);
|
||||
|
||||
this.d3D12Resource = new ComPtr<ID3D12Resource>(this.allocation.Get()->GetResource());
|
||||
#else
|
||||
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(
|
||||
resourceType,
|
||||
allocationMode,
|
||||
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
|
||||
@@ -189,6 +203,7 @@ public unsafe abstract class Texture3D<T> : NativeObject, GraphicsResourceHelper
|
||||
out ulong rowSizeInBytes,
|
||||
out ulong totalSizeInBytes);
|
||||
|
||||
#if USE_D3D12MA
|
||||
using ComPtr<D3D12MA_Allocation> allocation = GraphicsDevice.Allocator->CreateResource(
|
||||
GraphicsDevice.Pool,
|
||||
ResourceType.ReadBack,
|
||||
@@ -196,6 +211,13 @@ public unsafe abstract class Texture3D<T> : NativeObject, GraphicsResourceHelper
|
||||
totalSizeInBytes);
|
||||
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = new(allocation.Get()->GetResource());
|
||||
#else
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
|
||||
ResourceType.ReadBack,
|
||||
AllocationMode.Default,
|
||||
totalSizeInBytes,
|
||||
GraphicsDevice.IsCacheCoherentUMA);
|
||||
#endif
|
||||
|
||||
using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType))
|
||||
{
|
||||
@@ -428,6 +450,7 @@ public unsafe abstract class Texture3D<T> : NativeObject, GraphicsResourceHelper
|
||||
out ulong rowSizeInBytes,
|
||||
out ulong totalSizeInBytes);
|
||||
|
||||
#if USE_D3D12MA
|
||||
using ComPtr<D3D12MA_Allocation> allocation = GraphicsDevice.Allocator->CreateResource(
|
||||
GraphicsDevice.Pool,
|
||||
ResourceType.Upload,
|
||||
@@ -435,6 +458,13 @@ public unsafe abstract class Texture3D<T> : NativeObject, GraphicsResourceHelper
|
||||
totalSizeInBytes);
|
||||
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = new(allocation.Get()->GetResource());
|
||||
#else
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
|
||||
ResourceType.Upload,
|
||||
AllocationMode.Default,
|
||||
totalSizeInBytes,
|
||||
GraphicsDevice.IsCacheCoherentUMA);
|
||||
#endif
|
||||
|
||||
using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
|
||||
fixed (void* sourcePointer = &source)
|
||||
@@ -555,7 +585,9 @@ public unsafe abstract class Texture3D<T> : NativeObject, GraphicsResourceHelper
|
||||
protected override void OnDispose()
|
||||
{
|
||||
this.d3D12Resource.Dispose();
|
||||
#if USE_D3D12MA
|
||||
this.allocation.Dispose();
|
||||
#endif
|
||||
|
||||
if (GraphicsDevice is GraphicsDevice device)
|
||||
{
|
||||
|
||||
@@ -18,10 +18,12 @@ namespace ComputeSharp.Resources;
|
||||
public abstract unsafe class TransferBuffer<T> : NativeObject, IMemoryOwner<T>
|
||||
where T : unmanaged
|
||||
{
|
||||
#if USE_D3D12MA
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12MA_Allocation"/> instance used to retrieve <see cref="d3D12Resource"/>.
|
||||
/// </summary>
|
||||
private ComPtr<D3D12MA_Allocation> allocation;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ID3D12Resource"/> instance currently mapped.
|
||||
@@ -52,8 +54,12 @@ public abstract unsafe class TransferBuffer<T> : NativeObject, IMemoryOwner<T>
|
||||
|
||||
ulong sizeInBytes = (uint)length * (uint)sizeof(T);
|
||||
|
||||
#if USE_D3D12MA
|
||||
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, allocationMode, sizeInBytes, device.IsCacheCoherentUMA);
|
||||
#endif
|
||||
|
||||
device.RegisterAllocatedResource();
|
||||
|
||||
@@ -110,7 +116,9 @@ public abstract unsafe class TransferBuffer<T> : NativeObject, IMemoryOwner<T>
|
||||
protected override void OnDispose()
|
||||
{
|
||||
this.d3D12Resource.Dispose();
|
||||
#if USE_D3D12MA
|
||||
this.allocation.Dispose();
|
||||
#endif
|
||||
|
||||
if (GraphicsDevice is GraphicsDevice device)
|
||||
{
|
||||
|
||||
@@ -18,10 +18,12 @@ namespace ComputeSharp.Resources;
|
||||
public unsafe abstract class TransferTexture2D<T> : NativeObject
|
||||
where T : unmanaged
|
||||
{
|
||||
#if USE_D3D12MA
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12MA_Allocation"/> instance used to retrieve <see cref="d3D12Resource"/>.
|
||||
/// </summary>
|
||||
private ComPtr<D3D12MA_Allocation> allocation;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ID3D12Resource"/> instance currently mapped.
|
||||
@@ -68,8 +70,12 @@ public unsafe abstract class TransferTexture2D<T> : NativeObject
|
||||
out _,
|
||||
out ulong totalSizeInBytes);
|
||||
|
||||
#if USE_D3D12MA
|
||||
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, allocationMode, totalSizeInBytes, device.IsCacheCoherentUMA);
|
||||
#endif
|
||||
|
||||
device.RegisterAllocatedResource();
|
||||
|
||||
@@ -119,7 +125,9 @@ public unsafe abstract class TransferTexture2D<T> : NativeObject
|
||||
protected override void OnDispose()
|
||||
{
|
||||
this.d3D12Resource.Dispose();
|
||||
#if USE_D3D12MA
|
||||
this.allocation.Dispose();
|
||||
#endif
|
||||
|
||||
if (GraphicsDevice is GraphicsDevice device)
|
||||
{
|
||||
|
||||
@@ -18,10 +18,12 @@ namespace ComputeSharp.Resources;
|
||||
public unsafe abstract class TransferTexture3D<T> : NativeObject
|
||||
where T : unmanaged
|
||||
{
|
||||
#if USE_D3D12MA
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12MA_Allocation"/> instance used to retrieve <see cref="d3D12Resource"/>.
|
||||
/// </summary>
|
||||
private ComPtr<D3D12MA_Allocation> allocation;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ID3D12Resource"/> instance currently mapped.
|
||||
@@ -71,8 +73,12 @@ public unsafe abstract class TransferTexture3D<T> : NativeObject
|
||||
out _,
|
||||
out ulong totalSizeInBytes);
|
||||
|
||||
#if USE_D3D12MA
|
||||
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, allocationMode, totalSizeInBytes, device.IsCacheCoherentUMA);
|
||||
#endif
|
||||
|
||||
device.RegisterAllocatedResource();
|
||||
|
||||
@@ -127,7 +133,9 @@ public unsafe abstract class TransferTexture3D<T> : NativeObject
|
||||
protected override void OnDispose()
|
||||
{
|
||||
this.d3D12Resource.Dispose();
|
||||
#if USE_D3D12MA
|
||||
this.allocation.Dispose();
|
||||
#endif
|
||||
|
||||
if (GraphicsDevice is GraphicsDevice device)
|
||||
{
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする