Merge pull request #101 from Sergio0694/feature/d3d12memoryallocator2
Added D3D12MA integration
このコミットが含まれているのは:
19ファイル変更
+534
-70
検索条件に一致するファイルはありません
@@ -7,10 +7,11 @@
|
||||
<Description>ComputeSharp</Description>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Toolkit.Diagnostics" Version="7.0.0-build.776" />
|
||||
<PackageReference Include="TerraFX.Interop.Windows" Version="10.0.19041-beta2" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Toolkit.Diagnostics" Version="7.0.0-build.776" />
|
||||
<PackageReference Include="TerraFX.Interop.Windows" Version="10.0.19041-beta2" />
|
||||
<PackageReference Include="TerraFX.Interop.D3D12MemoryAllocator" Version="2.0.0-beta1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="Libraries\dxcompiler.dll"
|
||||
|
||||
@@ -23,10 +23,10 @@ namespace ComputeSharp.Graphics.Commands
|
||||
/// </summary>
|
||||
/// <param name="d3D12RootSignature">The <see cref="ID3D12RootSignature"/> value for the current shader.</param>
|
||||
/// <param name="d3D12PipelineState">The compiled pipeline state to reuse for the current shader.</param>
|
||||
public PipelineData(ComPtr<ID3D12RootSignature> d3D12RootSignature, ComPtr<ID3D12PipelineState> d3D12PipelineState)
|
||||
public PipelineData(ID3D12RootSignature* d3D12RootSignature, ID3D12PipelineState* d3D12PipelineState)
|
||||
{
|
||||
this.d3D12RootSignature = d3D12RootSignature;
|
||||
this.d3D12PipelineState = d3D12PipelineState;
|
||||
this.d3D12RootSignature = new ComPtr<ID3D12RootSignature>(d3D12RootSignature);
|
||||
this.d3D12PipelineState = new ComPtr<ID3D12PipelineState>(d3D12PipelineState);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
using ComputeSharp.Core.Extensions;
|
||||
using ComputeSharp.Interop;
|
||||
using Microsoft.Toolkit.Diagnostics;
|
||||
using TerraFX.Interop;
|
||||
using static TerraFX.Interop.D3D12_HEAP_TYPE;
|
||||
using static TerraFX.Interop.D3D12_RESOURCE_FLAGS;
|
||||
using static TerraFX.Interop.D3D12_RESOURCE_STATES;
|
||||
using static TerraFX.Interop.D3D12MA_ALLOCATION_FLAGS;
|
||||
using ResourceType = ComputeSharp.Graphics.Resources.Enums.ResourceType;
|
||||
|
||||
namespace ComputeSharp.Graphics.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> with extensions for the <see cref="D3D12MA_Allocator"/> type.
|
||||
/// </summary>
|
||||
internal static unsafe class AllocatorExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a resource for a given buffer type.
|
||||
/// </summary>
|
||||
/// <param name="allocator">The <see cref="D3D12MA_Allocator"/> 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>
|
||||
/// <returns>An <see cref="UniquePtr{T}"/> reference for the current <see cref="D3D12MA_Allocation"/> object.</returns>
|
||||
public static UniquePtr<D3D12MA_Allocation> CreateResource(
|
||||
this ref D3D12MA_Allocator allocator,
|
||||
ResourceType resourceType,
|
||||
AllocationMode allocationMode,
|
||||
ulong sizeInBytes)
|
||||
{
|
||||
D3D12MA_ALLOCATION_FLAGS allocationFlags = allocationMode == AllocationMode.Default ? D3D12MA_ALLOCATION_FLAG_NONE : D3D12MA_ALLOCATION_FLAG_COMMITTED;
|
||||
(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 UniquePtr<D3D12MA_Allocation> allocation = default;
|
||||
|
||||
D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Buffer(sizeInBytes, d3D12ResourceFlags);
|
||||
|
||||
D3D12MA_ALLOCATION_DESC allocationDesc = default;
|
||||
allocationDesc.HeapType = d3D12HeapType;
|
||||
allocationDesc.Flags = allocationFlags;
|
||||
|
||||
allocator.CreateResource(
|
||||
&allocationDesc,
|
||||
&d3D12ResourceDescription,
|
||||
d3D12ResourceStates,
|
||||
null,
|
||||
allocation.GetAddressOf(),
|
||||
null,
|
||||
null).Assert();
|
||||
|
||||
return allocation.Move();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a resource for a given 2D texture type.
|
||||
/// </summary>
|
||||
/// <param name="allocator">The <see cref="D3D12MA_Allocator"/> 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="d3D12ResourceStates">The default <see cref="D3D12_RESOURCE_STATES"/> value for the resource.</param>
|
||||
/// <returns>An <see cref="UniquePtr{T}"/> reference for the current <see cref="D3D12MA_Allocation"/> object.</returns>
|
||||
public static UniquePtr<D3D12MA_Allocation> CreateResource(
|
||||
this ref D3D12MA_Allocator allocator,
|
||||
ResourceType resourceType,
|
||||
AllocationMode allocationMode,
|
||||
DXGI_FORMAT dxgiFormat,
|
||||
uint width,
|
||||
uint height,
|
||||
out D3D12_RESOURCE_STATES d3D12ResourceStates)
|
||||
{
|
||||
D3D12MA_ALLOCATION_FLAGS allocationFlags = allocationMode == AllocationMode.Default ? D3D12MA_ALLOCATION_FLAG_NONE : D3D12MA_ALLOCATION_FLAG_COMMITTED;
|
||||
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 UniquePtr<D3D12MA_Allocation> allocation = default;
|
||||
|
||||
D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex2D(dxgiFormat, width, height, mipLevels: 1, flags: d3D12ResourceFlags);
|
||||
|
||||
D3D12MA_ALLOCATION_DESC allocationDesc = default;
|
||||
allocationDesc.HeapType = D3D12_HEAP_TYPE_DEFAULT;
|
||||
allocationDesc.Flags = allocationFlags;
|
||||
|
||||
allocator.CreateResource(
|
||||
&allocationDesc,
|
||||
&d3D12ResourceDescription,
|
||||
d3D12ResourceStates,
|
||||
null,
|
||||
allocation.GetAddressOf(),
|
||||
null,
|
||||
null);
|
||||
|
||||
return allocation.Move();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a resource for a given 3D texture type.
|
||||
/// </summary>
|
||||
/// <param name="allocator">The <see cref="D3D12MA_Allocator"/> 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="d3D12ResourceStates">The default <see cref="D3D12_RESOURCE_STATES"/> value for the resource.</param>
|
||||
/// <returns>An <see cref="UniquePtr{T}"/> reference for the current <see cref="D3D12MA_Allocation"/> object.</returns>
|
||||
public static UniquePtr<D3D12MA_Allocation> CreateResource(
|
||||
this ref D3D12MA_Allocator allocator,
|
||||
ResourceType resourceType,
|
||||
AllocationMode allocationMode,
|
||||
DXGI_FORMAT dxgiFormat,
|
||||
uint width,
|
||||
uint height,
|
||||
ushort depth,
|
||||
out D3D12_RESOURCE_STATES d3D12ResourceStates)
|
||||
{
|
||||
D3D12MA_ALLOCATION_FLAGS allocationFlags = allocationMode == AllocationMode.Default ? D3D12MA_ALLOCATION_FLAG_NONE : D3D12MA_ALLOCATION_FLAG_COMMITTED;
|
||||
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 UniquePtr<D3D12MA_Allocation> allocation = default;
|
||||
|
||||
D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex3D(dxgiFormat, width, height, depth, mipLevels: 1, flags: d3D12ResourceFlags);
|
||||
|
||||
D3D12MA_ALLOCATION_DESC allocationDesc = default;
|
||||
allocationDesc.HeapType = D3D12_HEAP_TYPE_DEFAULT;
|
||||
allocationDesc.Flags = allocationFlags;
|
||||
|
||||
allocator.CreateResource(
|
||||
&allocationDesc,
|
||||
&d3D12ResourceDescription,
|
||||
d3D12ResourceStates,
|
||||
null,
|
||||
allocation.GetAddressOf(),
|
||||
null,
|
||||
null);
|
||||
|
||||
return allocation.Move();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,12 +43,11 @@ namespace ComputeSharp.Core.Extensions
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the address of the current <see cref="ComPtr{T}"/> instance as a raw <see langword="void"/> double pointer.
|
||||
/// Moves the current <see cref="ComPtr{T}"/> instance and resets it without releasing the reference.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to wrap in the current <see cref="ComPtr{T}"/> instance.</typeparam>
|
||||
/// <param name="ptr">The input <see cref="ComPtr{T}"/> instance to get the address for.</param>
|
||||
/// <returns>The raw pointer to the input <see cref="ComPtr{T}"/> instance.</returns>
|
||||
/// <remarks>This method is only valid when the current <see cref="ComPtr{T}"/> instance is on the stack or pinned.</remarks>
|
||||
/// <param name="ptr">The input <see cref="ComPtr{T}"/> instance to move.</param>
|
||||
/// <returns>The moved <see cref="ComPtr{T}"/> instance.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe ComPtr<T> Move<T>(this in ComPtr<T> ptr)
|
||||
where T : unmanaged
|
||||
|
||||
@@ -72,18 +72,24 @@ namespace ComputeSharp
|
||||
/// </summary>
|
||||
private ulong nextD3D12CopyFenceValue = 1;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12MA_Allocator"/> in use associated to the current device.
|
||||
/// </summary>
|
||||
private UniquePtr<D3D12MA_Allocator> allocator;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="GraphicsDevice"/> instance for the input <see cref="ID3D12Device"/>.
|
||||
/// </summary>
|
||||
/// <param name="d3D12Device">The <see cref="ID3D12Device"/> to use for the new <see cref="GraphicsDevice"/> instance.</param>
|
||||
/// <param name="dxgiAdapter">The <see cref="IDXGIAdapter"/> that <paramref name="d3D12Device"/> was created from.</param>
|
||||
/// <param name="dxgiDescription1">The available info for the new <see cref="GraphicsDevice"/> instance.</param>
|
||||
internal GraphicsDevice(ComPtr<ID3D12Device> d3D12Device, DXGI_ADAPTER_DESC1* dxgiDescription1)
|
||||
internal GraphicsDevice(ID3D12Device* d3D12Device, IDXGIAdapter* dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1)
|
||||
{
|
||||
this.d3D12Device = d3D12Device;
|
||||
this.d3D12ComputeCommandQueue = d3D12Device.Get()->CreateCommandQueue(D3D12_COMMAND_LIST_TYPE_COMPUTE);
|
||||
this.d3D12CopyCommandQueue = d3D12Device.Get()->CreateCommandQueue(D3D12_COMMAND_LIST_TYPE_COPY);
|
||||
this.d3D12ComputeFence = d3D12Device.Get()->CreateFence();
|
||||
this.d3D12CopyFence = d3D12Device.Get()->CreateFence();
|
||||
this.d3D12Device = new ComPtr<ID3D12Device>(d3D12Device);
|
||||
this.d3D12ComputeCommandQueue = d3D12Device->CreateCommandQueue(D3D12_COMMAND_LIST_TYPE_COMPUTE);
|
||||
this.d3D12CopyCommandQueue = d3D12Device->CreateCommandQueue(D3D12_COMMAND_LIST_TYPE_COPY);
|
||||
this.d3D12ComputeFence = d3D12Device->CreateFence();
|
||||
this.d3D12CopyFence = d3D12Device->CreateFence();
|
||||
|
||||
this.computeCommandAllocatorPool = new ID3D12CommandAllocatorPool(D3D12_COMMAND_LIST_TYPE_COMPUTE);
|
||||
this.copyCommandAllocatorPool = new ID3D12CommandAllocatorPool(D3D12_COMMAND_LIST_TYPE_COPY);
|
||||
@@ -95,14 +101,26 @@ namespace ComputeSharp
|
||||
SharedMemorySize = dxgiDescription1->SharedSystemMemory;
|
||||
IsHardwareAccelerated = (dxgiDescription1->Flags & (uint)DXGI_ADAPTER_FLAG_SOFTWARE) == 0;
|
||||
|
||||
var d3D12Options1Data = d3D12Device.Get()->CheckFeatureSupport<D3D12_FEATURE_DATA_D3D12_OPTIONS1>(D3D12_FEATURE_D3D12_OPTIONS1);
|
||||
var d3D12Options1Data = d3D12Device->CheckFeatureSupport<D3D12_FEATURE_DATA_D3D12_OPTIONS1>(D3D12_FEATURE_D3D12_OPTIONS1);
|
||||
|
||||
ComputeUnits = d3D12Options1Data.TotalLaneCount;
|
||||
WavefrontSize = d3D12Options1Data.WaveLaneCountMin;
|
||||
|
||||
var d3D12Architecture1Data = d3D12Device.Get()->CheckFeatureSupport<D3D12_FEATURE_DATA_ARCHITECTURE1>(D3D12_FEATURE_ARCHITECTURE1);
|
||||
var d3D12Architecture1Data = d3D12Device->CheckFeatureSupport<D3D12_FEATURE_DATA_ARCHITECTURE1>(D3D12_FEATURE_ARCHITECTURE1);
|
||||
|
||||
IsCacheCoherentUMA = d3D12Architecture1Data.CacheCoherentUMA != 0;
|
||||
|
||||
D3D12MA_ALLOCATOR_DESC allocatorDesc = default;
|
||||
allocatorDesc.pDevice = d3D12Device;
|
||||
allocatorDesc.pAdapter = dxgiAdapter;
|
||||
|
||||
if (!IsCacheCoherentUMA)
|
||||
{
|
||||
fixed (D3D12MA_Allocator** allocator = this.allocator)
|
||||
{
|
||||
D3D12MemAlloc.D3D12MA_CreateAllocator(&allocatorDesc, allocator).Assert();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -146,6 +164,11 @@ namespace ComputeSharp
|
||||
/// </summary>
|
||||
internal ID3D12Device* D3D12Device => this.d3D12Device;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the underlying <see cref="D3D12MA_Allocator"/> wrapped by the current instance.
|
||||
/// </summary>
|
||||
internal D3D12MA_Allocator* Allocator => this.allocator;
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether or not the current device has a cache coherent UMA architecture.
|
||||
/// </summary>
|
||||
@@ -316,6 +339,7 @@ namespace ComputeSharp
|
||||
this.computeCommandAllocatorPool.Dispose();
|
||||
this.copyCommandAllocatorPool.Dispose();
|
||||
this.shaderResourceViewDescriptorAllocator.Dispose();
|
||||
this.allocator.Dispose();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -141,7 +141,7 @@ namespace ComputeSharp.Graphics.Helpers
|
||||
FX.__uuidof<ID3D12Device>(),
|
||||
d3D12Device.GetVoidAddressOf()).Assert();
|
||||
|
||||
this.graphicsDevice = GetOrCreateDevice(d3D12Device.Move(), &dxgiDescription1);
|
||||
this.graphicsDevice = GetOrCreateDevice(d3D12Device.Get(), (IDXGIAdapter*)dxgiAdapter1.Get(), &dxgiDescription1);
|
||||
this.isCompleted = true;
|
||||
|
||||
return true;
|
||||
@@ -182,7 +182,7 @@ namespace ComputeSharp.Graphics.Helpers
|
||||
FX.__uuidof<ID3D12Device>(),
|
||||
d3D12Device.GetVoidAddressOf()).Assert();
|
||||
|
||||
this.graphicsDevice = GetOrCreateDevice(d3D12Device.Move(), &dxgiDescription1);
|
||||
this.graphicsDevice = GetOrCreateDevice(d3D12Device.Get(), (IDXGIAdapter*)dxgiAdapter1.Get(), &dxgiDescription1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -25,9 +25,10 @@ namespace ComputeSharp.Graphics.Helpers
|
||||
/// Retrieves a <see cref="GraphicsDevice"/> instance for an <see cref="ID3D12Device"/> object.
|
||||
/// </summary>
|
||||
/// <param name="d3D12Device">The <see cref="ID3D12Device"/> to use to get a <see cref="GraphicsDevice"/> instance.</param>
|
||||
/// <param name="dxgiAdapter">The <see cref="IDXGIAdapter"/> that <paramref name="d3D12Device"/> was created from.</param>
|
||||
/// <param name="dxgiDescription1">The available info for the <see cref="GraphicsDevice"/> instance.</param>
|
||||
/// <returns>A <see cref="GraphicsDevice"/> instance for the input device.</returns>
|
||||
private static unsafe GraphicsDevice GetOrCreateDevice(ComPtr<ID3D12Device> d3D12Device, DXGI_ADAPTER_DESC1* dxgiDescription1)
|
||||
private static unsafe GraphicsDevice GetOrCreateDevice(ID3D12Device* d3D12Device, IDXGIAdapter* dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1)
|
||||
{
|
||||
lock (DevicesCache)
|
||||
{
|
||||
@@ -35,12 +36,12 @@ namespace ComputeSharp.Graphics.Helpers
|
||||
|
||||
if (!DevicesCache.TryGetValue(luid, out GraphicsDevice? device))
|
||||
{
|
||||
device = new GraphicsDevice(d3D12Device, dxgiDescription1);
|
||||
device = new GraphicsDevice(d3D12Device, dxgiAdapter, dxgiDescription1);
|
||||
|
||||
DevicesCache.Add(luid, device);
|
||||
|
||||
#if DEBUG
|
||||
D3D12InfoQueueMap.Add(luid, d3D12Device.Get()->CreateInfoQueue());
|
||||
D3D12InfoQueueMap.Add(luid, d3D12Device->CreateInfoQueue());
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ namespace ComputeSharp.Graphics.Helpers
|
||||
{
|
||||
DXGI_ADAPTER_DESC1 dxgiDescription1;
|
||||
|
||||
return TryGetDefaultDevice(null, &dxgiDescription1) || TryGetWarpDevice(null, &dxgiDescription1);
|
||||
return TryGetDefaultDevice(null, null, &dxgiDescription1) || TryGetWarpDevice(null, null, &dxgiDescription1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -46,7 +46,7 @@ namespace ComputeSharp.Graphics.Helpers
|
||||
{
|
||||
DXGI_ADAPTER_DESC1 dxgiDescription1;
|
||||
|
||||
_ = TryGetDefaultDevice(null, &dxgiDescription1) || TryGetWarpDevice(null, &dxgiDescription1);
|
||||
_ = TryGetDefaultDevice(null, null, &dxgiDescription1) || TryGetWarpDevice(null, null, &dxgiDescription1);
|
||||
|
||||
return Luid.FromLUID(dxgiDescription1.AdapterLuid);
|
||||
}
|
||||
@@ -59,13 +59,14 @@ namespace ComputeSharp.Graphics.Helpers
|
||||
public static unsafe GraphicsDevice GetDefaultDevice()
|
||||
{
|
||||
using ComPtr<ID3D12Device> d3D12Device = default;
|
||||
using ComPtr<IDXGIAdapter> dxgiAdapter = default;
|
||||
|
||||
DXGI_ADAPTER_DESC1 dxgiDescription1;
|
||||
|
||||
if (TryGetDefaultDevice(d3D12Device.GetAddressOf(), &dxgiDescription1) ||
|
||||
TryGetWarpDevice(d3D12Device.GetAddressOf(), &dxgiDescription1))
|
||||
if (TryGetDefaultDevice(d3D12Device.GetAddressOf(), dxgiAdapter.GetAddressOf(), &dxgiDescription1) ||
|
||||
TryGetWarpDevice(d3D12Device.GetAddressOf(), dxgiAdapter.GetAddressOf(), &dxgiDescription1))
|
||||
{
|
||||
return GetOrCreateDevice(d3D12Device.Move(), &dxgiDescription1);
|
||||
return GetOrCreateDevice(d3D12Device.Get(), dxgiAdapter.Get(), &dxgiDescription1);
|
||||
}
|
||||
|
||||
return ThrowHelper.ThrowNotSupportedException<GraphicsDevice>("There isn't a supported GPU device on the current machine");
|
||||
@@ -75,9 +76,10 @@ namespace ComputeSharp.Graphics.Helpers
|
||||
/// Tries to check or create a default <see cref="ID3D12Device"/> object.
|
||||
/// </summary>
|
||||
/// <param name="d3D12Device">A pointer to the <see cref="ID3D12Device"/> object to create, or <see langword="null"/>.</param>
|
||||
/// <param name="dxgiAdapter">A pointer to the <see cref="IDXGIAdapter"/> object used to create <paramref name="d3D12Device"/>, or <see langword="null"/>.</param>
|
||||
/// <param name="dxgiDescription1">A pointer to the <see cref="DXGI_ADAPTER_DESC1"/> value for the device found.</param>
|
||||
/// <returns>Whether a default device was found with the requested feature level.</returns>
|
||||
private static unsafe bool TryGetDefaultDevice(ID3D12Device** d3D12Device, DXGI_ADAPTER_DESC1* dxgiDescription1)
|
||||
private static unsafe bool TryGetDefaultDevice(ID3D12Device** d3D12Device, IDXGIAdapter** dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1)
|
||||
{
|
||||
using ComPtr<IDXGIFactory4> dxgiFactory4 = default;
|
||||
|
||||
@@ -116,6 +118,8 @@ namespace ComputeSharp.Graphics.Helpers
|
||||
|
||||
if (FX.SUCCEEDED(createDeviceResult))
|
||||
{
|
||||
dxgiAdapter1.CopyTo(dxgiAdapter);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -125,9 +129,10 @@ namespace ComputeSharp.Graphics.Helpers
|
||||
/// Tries to check or create a warp <see cref="ID3D12Device"/> object.
|
||||
/// </summary>
|
||||
/// <param name="d3D12Device">A pointer to the <see cref="ID3D12Device"/> object to create, or <see langword="null"/>.</param>
|
||||
/// <param name="dxgiAdapter">A pointer to the <see cref="IDXGIAdapter"/> object used to create <paramref name="d3D12Device"/>, or <see langword="null"/>.</param>
|
||||
/// <param name="dxgiDescription1">A pointer to the <see cref="DXGI_ADAPTER_DESC1"/> value for the device found.</param>
|
||||
/// <returns>Whether a warp device was created successfully.</returns>
|
||||
private static unsafe bool TryGetWarpDevice(ID3D12Device** d3D12Device, DXGI_ADAPTER_DESC1* dxgiDescription1)
|
||||
private static unsafe bool TryGetWarpDevice(ID3D12Device** d3D12Device, IDXGIAdapter** dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1)
|
||||
{
|
||||
using ComPtr<IDXGIFactory4> dxgiFactory4 = default;
|
||||
|
||||
@@ -147,6 +152,8 @@ namespace ComputeSharp.Graphics.Helpers
|
||||
FX.__uuidof<ID3D12Device>(),
|
||||
(void**)d3D12Device);
|
||||
|
||||
dxgiAdapter1.CopyTo(dxgiAdapter);
|
||||
|
||||
return FX.SUCCEEDED(createDeviceResult);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Runtime.CompilerServices;
|
||||
using TerraFX.Interop;
|
||||
|
||||
namespace ComputeSharp.Interop
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see cref="ComPtr{T}"/>-equivalent type to safely work with pointers to public <see cref="D3D12MemAlloc"/> APIs.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Must be either <see cref="D3D12MA_Allocator"/>, <see cref="D3D12MA_Allocation"/>, <see cref="D3D12MA_Pool"/> or <see cref="D3D12MA_VirtualBlock"/>.</typeparam>
|
||||
/// <remarks>
|
||||
/// This type is not marked as <see langword="ref"/> so that it can also be used in fields, so make sure to
|
||||
/// avoid copying it and disposing it multiple times, as that would lead to a access violation exceptions.
|
||||
/// </remarks>
|
||||
internal unsafe struct UniquePtr<T> : IDisposable
|
||||
where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// The raw pointer to an <typeparamref name="T"/> object, if existing.
|
||||
/// </summary>
|
||||
private T* pointer;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="UniquePtr{T}"/> instance from a raw pointer.
|
||||
/// </summary>
|
||||
/// <param name="other">The raw pointer to wrap.</param>
|
||||
public UniquePtr(T* other)
|
||||
{
|
||||
pointer = other;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unwraps a <see cref="UniquePtr{T}"/> instance and returns the internal raw pointer.
|
||||
/// </summary>
|
||||
/// <param name="other">The <see cref="UniquePtr{T}"/> instance to unwrap.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static implicit operator T*(UniquePtr<T> other)
|
||||
{
|
||||
return other.Get();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Dispose()
|
||||
{
|
||||
T* pointer = this.pointer;
|
||||
|
||||
if (pointer != null)
|
||||
{
|
||||
this.pointer = null;
|
||||
|
||||
if (typeof(T) == typeof(D3D12MA_Allocator)) ((D3D12MA_Allocator*)pointer)->Release();
|
||||
else if (typeof(T) == typeof(D3D12MA_Allocation)) ((D3D12MA_Allocation*)pointer)->Release();
|
||||
else if (typeof(T) == typeof(D3D12MA_Pool)) ((D3D12MA_Pool*)pointer)->Release();
|
||||
else if (typeof(T) == typeof(D3D12MA_VirtualBlock)) ((D3D12MA_VirtualBlock*)pointer)->Release();
|
||||
else throw new ArgumentException("Invalid pointer type");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the currently wrapped raw pointer to an <typeparamref name="T"/> object.
|
||||
/// </summary>
|
||||
/// <returns>The raw pointer wrapped by the current <typeparamref name="T"/> instance.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly T* Get()
|
||||
{
|
||||
return this.pointer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the address of the current <see cref="UniquePtr{T}"/> instance as a raw <typeparamref name="T"/> double pointer.
|
||||
/// </summary>
|
||||
/// <returns>The raw pointer to the current <see cref="UniquePtr{T}"/> instance.</returns>
|
||||
/// <remarks>This method is only valid when the current <see cref="UniquePtr{T}"/> instance is on the stack or pinned.</remarks>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly T** GetAddressOf()
|
||||
{
|
||||
return (T**)Unsafe.AsPointer(ref Unsafe.AsRef(in this));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the address of the current <see cref="UniquePtr{T}"/> instance as a managed <typeparamref name="T"/> reference to pointer.
|
||||
/// </summary>
|
||||
/// <returns>The reference to the current <see cref="UniquePtr{T}"/> instance.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
public readonly ref T* GetPinnableReference()
|
||||
{
|
||||
fixed (T** ptr = &this.pointer)
|
||||
{
|
||||
return ref *ptr;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Moves the current <see cref="UniquePtr{T}"/> instance and resets it without releasing the reference.
|
||||
/// </summary>
|
||||
/// <returns>The moved <see cref="UniquePtr{T}"/> instance.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public UniquePtr<T> Move()
|
||||
{
|
||||
UniquePtr<T> copy = this;
|
||||
|
||||
this = default;
|
||||
|
||||
return copy;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -38,6 +38,12 @@ namespace ComputeSharp.Resources
|
||||
/// </summary>
|
||||
protected readonly nint SizeInBytes;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12MA_Allocation"/> instance used to retrieve <see cref="d3D12Resource"/>, if any.
|
||||
/// </summary>
|
||||
/// <remarks>This will be <see langword="null"/> if the owning device has <see cref="GraphicsDevice.IsCacheCoherentUMA"/> set.</remarks>
|
||||
private UniquePtr<D3D12MA_Allocation> allocation;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Buffer{T}"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
@@ -64,7 +70,15 @@ namespace ComputeSharp.Resources
|
||||
GraphicsDevice = device;
|
||||
Length = length;
|
||||
|
||||
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, allocationMode, (ulong)SizeInBytes, device.IsCacheCoherentUMA);
|
||||
if (device.IsCacheCoherentUMA)
|
||||
{
|
||||
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, allocationMode, (ulong)SizeInBytes, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.allocation = device.Allocator->CreateResource(resourceType, allocationMode, (ulong)SizeInBytes);
|
||||
this.d3D12Resource = new ComPtr<ID3D12Resource>(this.allocation.Get()->GetResource());
|
||||
}
|
||||
|
||||
device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle);
|
||||
|
||||
@@ -156,6 +170,7 @@ namespace ComputeSharp.Resources
|
||||
protected override bool OnDispose()
|
||||
{
|
||||
this.d3D12Resource.Dispose();
|
||||
this.allocation.Dispose();
|
||||
|
||||
if (GraphicsDevice?.IsDisposed == false)
|
||||
{
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using ComputeSharp.Graphics.Extensions;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
using ComputeSharp.Graphics.Resources.Interop;
|
||||
using ComputeSharp.Interop;
|
||||
using Microsoft.Toolkit.Diagnostics;
|
||||
using TerraFX.Interop;
|
||||
using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE;
|
||||
@@ -58,15 +59,15 @@ namespace ComputeSharp.Resources
|
||||
byteOffset = (nint)offset * sizeof(T),
|
||||
byteLength = length * sizeof(T);
|
||||
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, AllocationMode.Default, (ulong)byteLength, false);
|
||||
using UniquePtr<D3D12MA_Allocation> allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.ReadBack, AllocationMode.Default, (ulong)byteLength);
|
||||
|
||||
using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY))
|
||||
{
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(d3D12Resource.Get(), 0, D3D12Resource, (ulong)byteOffset, (ulong)byteLength);
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(allocation.Get()->GetResource(), 0, D3D12Resource, (ulong)byteOffset, (ulong)byteLength);
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
|
||||
using ID3D12ResourceMap resource = d3D12Resource.Get()->Map();
|
||||
using ID3D12ResourceMap resource = allocation.Get()->GetResource()->Map();
|
||||
|
||||
fixed (void* destinationPointer = &destination)
|
||||
{
|
||||
@@ -156,9 +157,9 @@ namespace ComputeSharp.Resources
|
||||
byteOffset = (nint)offset * sizeof(T),
|
||||
byteLength = length * sizeof(T);
|
||||
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, AllocationMode.Default, (ulong)byteLength, false);
|
||||
using UniquePtr<D3D12MA_Allocation> allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.Upload, AllocationMode.Default, (ulong)byteLength);
|
||||
|
||||
using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
|
||||
using (ID3D12ResourceMap resource = allocation.Get()->GetResource()->Map())
|
||||
fixed (void* sourcePointer = &source)
|
||||
{
|
||||
MemoryHelper.Copy(
|
||||
@@ -171,7 +172,7 @@ namespace ComputeSharp.Resources
|
||||
|
||||
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
|
||||
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, (ulong)byteOffset, d3D12Resource.Get(), 0, (ulong)byteLength);
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, (ulong)byteOffset, allocation.Get()->GetResource(), 0, (ulong)byteLength);
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,6 +56,12 @@ namespace ComputeSharp.Resources
|
||||
/// </summary>
|
||||
private readonly D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12MA_Allocation"/> instance used to retrieve <see cref="d3D12Resource"/>, if any.
|
||||
/// </summary>
|
||||
/// <remarks>This will be <see langword="null"/> if the owning device has <see cref="GraphicsDevice.IsCacheCoherentUMA"/> set.</remarks>
|
||||
private UniquePtr<D3D12MA_Allocation> allocation;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Texture2D{T}"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
@@ -79,20 +85,35 @@ namespace ComputeSharp.Resources
|
||||
|
||||
GraphicsDevice = device;
|
||||
|
||||
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(
|
||||
resourceType,
|
||||
allocationMode,
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
device.IsCacheCoherentUMA,
|
||||
out this.d3D12ResourceState);
|
||||
if (device.IsCacheCoherentUMA)
|
||||
{
|
||||
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(
|
||||
resourceType,
|
||||
allocationMode,
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
true,
|
||||
out this.d3D12ResourceState);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.allocation = device.Allocator->CreateResource(
|
||||
resourceType,
|
||||
allocationMode,
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
out this.d3D12ResourceState);
|
||||
|
||||
this.d3D12Resource = new ComPtr<ID3D12Resource>(this.allocation.Get()->GetResource());
|
||||
}
|
||||
|
||||
this.d3D12CommandListType = this.d3D12ResourceState == D3D12_RESOURCE_STATE_COMMON
|
||||
? D3D12_COMMAND_LIST_TYPE_COPY
|
||||
: D3D12_COMMAND_LIST_TYPE_COMPUTE;
|
||||
|
||||
GraphicsDevice.D3D12Device->GetCopyableFootprint(
|
||||
device.D3D12Device->GetCopyableFootprint(
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
@@ -166,7 +187,18 @@ namespace ComputeSharp.Resources
|
||||
out ulong rowSizeInBytes,
|
||||
out ulong totalSizeInBytes);
|
||||
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, AllocationMode.Default, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA);
|
||||
using UniquePtr<D3D12MA_Allocation> allocation = default;
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = default;
|
||||
|
||||
if (GraphicsDevice.IsCacheCoherentUMA)
|
||||
{
|
||||
*&d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, AllocationMode.Default, totalSizeInBytes, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
*&allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.ReadBack, AllocationMode.Default, totalSizeInBytes);
|
||||
*&d3D12Resource = new ComPtr<ID3D12Resource>(allocation.Get()->GetResource());
|
||||
}
|
||||
|
||||
using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType))
|
||||
{
|
||||
@@ -305,7 +337,18 @@ namespace ComputeSharp.Resources
|
||||
out ulong rowSizeInBytes,
|
||||
out ulong totalSizeInBytes);
|
||||
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, AllocationMode.Default, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA);
|
||||
using UniquePtr<D3D12MA_Allocation> allocation = default;
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = default;
|
||||
|
||||
if (GraphicsDevice.IsCacheCoherentUMA)
|
||||
{
|
||||
*&d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, AllocationMode.Default, totalSizeInBytes, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
*&allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.Upload, AllocationMode.Default, totalSizeInBytes);
|
||||
*&d3D12Resource = new ComPtr<ID3D12Resource>(allocation.Get()->GetResource());
|
||||
}
|
||||
|
||||
using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
|
||||
fixed (void* sourcePointer = &source)
|
||||
@@ -415,6 +458,7 @@ namespace ComputeSharp.Resources
|
||||
protected override bool OnDispose()
|
||||
{
|
||||
this.d3D12Resource.Dispose();
|
||||
this.allocation.Dispose();
|
||||
|
||||
if (GraphicsDevice?.IsDisposed == false)
|
||||
{
|
||||
|
||||
@@ -56,6 +56,12 @@ namespace ComputeSharp.Resources
|
||||
/// </summary>
|
||||
private readonly D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12MA_Allocation"/> instance used to retrieve <see cref="d3D12Resource"/>, if any.
|
||||
/// </summary>
|
||||
/// <remarks>This will be <see langword="null"/> if the owning device has <see cref="GraphicsDevice.IsCacheCoherentUMA"/> set.</remarks>
|
||||
private UniquePtr<D3D12MA_Allocation> allocation;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Texture3D{T}"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
@@ -81,21 +87,37 @@ namespace ComputeSharp.Resources
|
||||
|
||||
GraphicsDevice = device;
|
||||
|
||||
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(
|
||||
resourceType,
|
||||
allocationMode,
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
(ushort)depth,
|
||||
device.IsCacheCoherentUMA,
|
||||
out this.d3D12ResourceState);
|
||||
if (device.IsCacheCoherentUMA)
|
||||
{
|
||||
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(
|
||||
resourceType,
|
||||
allocationMode,
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
(ushort)depth,
|
||||
true,
|
||||
out this.d3D12ResourceState);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.allocation = device.Allocator->CreateResource(
|
||||
resourceType,
|
||||
allocationMode,
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
(ushort)depth,
|
||||
out this.d3D12ResourceState);
|
||||
|
||||
this.d3D12Resource = new ComPtr<ID3D12Resource>(this.allocation.Get()->GetResource());
|
||||
}
|
||||
|
||||
this.d3D12CommandListType = this.d3D12ResourceState == D3D12_RESOURCE_STATE_COMMON
|
||||
? D3D12_COMMAND_LIST_TYPE_COPY
|
||||
: D3D12_COMMAND_LIST_TYPE_COMPUTE;
|
||||
|
||||
GraphicsDevice.D3D12Device->GetCopyableFootprint(
|
||||
device.D3D12Device->GetCopyableFootprint(
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
@@ -181,7 +203,18 @@ namespace ComputeSharp.Resources
|
||||
out ulong rowSizeInBytes,
|
||||
out ulong totalSizeInBytes);
|
||||
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, AllocationMode.Default, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA);
|
||||
using UniquePtr<D3D12MA_Allocation> allocation = default;
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = default;
|
||||
|
||||
if (GraphicsDevice.IsCacheCoherentUMA)
|
||||
{
|
||||
*&d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, AllocationMode.Default, totalSizeInBytes, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
*&allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.ReadBack, AllocationMode.Default, totalSizeInBytes);
|
||||
*&d3D12Resource = new ComPtr<ID3D12Resource>(allocation.Get()->GetResource());
|
||||
}
|
||||
|
||||
using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType))
|
||||
{
|
||||
@@ -337,7 +370,18 @@ namespace ComputeSharp.Resources
|
||||
out ulong rowSizeInBytes,
|
||||
out ulong totalSizeInBytes);
|
||||
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, AllocationMode.Default, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA);
|
||||
using UniquePtr<D3D12MA_Allocation> allocation = default;
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = default;
|
||||
|
||||
if (GraphicsDevice.IsCacheCoherentUMA)
|
||||
{
|
||||
*&d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, AllocationMode.Default, totalSizeInBytes, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
*&allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.Upload, AllocationMode.Default, totalSizeInBytes);
|
||||
*&d3D12Resource = new ComPtr<ID3D12Resource>(allocation.Get()->GetResource());
|
||||
}
|
||||
|
||||
using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
|
||||
fixed (void* sourcePointer = &source)
|
||||
@@ -458,6 +502,7 @@ namespace ComputeSharp.Resources
|
||||
protected override bool OnDispose()
|
||||
{
|
||||
this.d3D12Resource.Dispose();
|
||||
this.allocation.Dispose();
|
||||
|
||||
if (GraphicsDevice?.IsDisposed == false)
|
||||
{
|
||||
|
||||
@@ -27,6 +27,12 @@ namespace ComputeSharp.Resources
|
||||
/// </summary>
|
||||
private readonly T* mappedData;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12MA_Allocation"/> instance used to retrieve <see cref="d3D12Resource"/>, if any.
|
||||
/// </summary>
|
||||
/// <remarks>This will be <see langword="null"/> if the owning device has <see cref="GraphicsDevice.IsCacheCoherentUMA"/> set.</remarks>
|
||||
private UniquePtr<D3D12MA_Allocation> allocation;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="TransferBuffer{T}"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
@@ -46,7 +52,16 @@ namespace ComputeSharp.Resources
|
||||
|
||||
ulong sizeInBytes = (uint)length * (uint)sizeof(T);
|
||||
|
||||
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, allocationMode, sizeInBytes, device.IsCacheCoherentUMA);
|
||||
if (device.IsCacheCoherentUMA)
|
||||
{
|
||||
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, allocationMode, sizeInBytes, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.allocation = device.Allocator->CreateResource(resourceType, allocationMode, sizeInBytes);
|
||||
this.d3D12Resource = new ComPtr<ID3D12Resource>(this.allocation.Get()->GetResource());
|
||||
}
|
||||
|
||||
this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer;
|
||||
|
||||
this.d3D12Resource.Get()->SetName(this);
|
||||
@@ -100,6 +115,7 @@ namespace ComputeSharp.Resources
|
||||
protected override bool OnDispose()
|
||||
{
|
||||
this.d3D12Resource.Dispose();
|
||||
this.allocation.Dispose();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -33,6 +33,12 @@ namespace ComputeSharp.Resources
|
||||
/// </summary>
|
||||
private readonly D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12MA_Allocation"/> instance used to retrieve <see cref="d3D12Resource"/>, if any.
|
||||
/// </summary>
|
||||
/// <remarks>This will be <see langword="null"/> if the owning device has <see cref="GraphicsDevice.IsCacheCoherentUMA"/> set.</remarks>
|
||||
private UniquePtr<D3D12MA_Allocation> allocation;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="TransferTexture2D{T}"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
@@ -55,7 +61,7 @@ namespace ComputeSharp.Resources
|
||||
|
||||
GraphicsDevice = device;
|
||||
|
||||
GraphicsDevice.D3D12Device->GetCopyableFootprint(
|
||||
device.D3D12Device->GetCopyableFootprint(
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
@@ -63,7 +69,16 @@ namespace ComputeSharp.Resources
|
||||
out _,
|
||||
out ulong totalSizeInBytes);
|
||||
|
||||
this.d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(resourceType, allocationMode, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA);
|
||||
if (device.IsCacheCoherentUMA)
|
||||
{
|
||||
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, allocationMode, totalSizeInBytes, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.allocation = device.Allocator->CreateResource(resourceType, allocationMode, totalSizeInBytes);
|
||||
this.d3D12Resource = new ComPtr<ID3D12Resource>(this.allocation.Get()->GetResource());
|
||||
}
|
||||
|
||||
this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer;
|
||||
|
||||
this.d3D12Resource.Get()->SetName(this);
|
||||
@@ -110,6 +125,7 @@ namespace ComputeSharp.Resources
|
||||
protected override bool OnDispose()
|
||||
{
|
||||
this.d3D12Resource.Dispose();
|
||||
this.allocation.Dispose();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -33,6 +33,12 @@ namespace ComputeSharp.Resources
|
||||
/// </summary>
|
||||
private readonly D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12MA_Allocation"/> instance used to retrieve <see cref="d3D12Resource"/>, if any.
|
||||
/// </summary>
|
||||
/// <remarks>This will be <see langword="null"/> if the owning device has <see cref="GraphicsDevice.IsCacheCoherentUMA"/> set.</remarks>
|
||||
private UniquePtr<D3D12MA_Allocation> allocation;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="TransferTexture3D{T}"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
@@ -57,7 +63,7 @@ namespace ComputeSharp.Resources
|
||||
|
||||
GraphicsDevice = device;
|
||||
|
||||
GraphicsDevice.D3D12Device->GetCopyableFootprint(
|
||||
device.D3D12Device->GetCopyableFootprint(
|
||||
DXGIFormatHelper.GetForType<T>(),
|
||||
(uint)width,
|
||||
(uint)height,
|
||||
@@ -66,7 +72,16 @@ namespace ComputeSharp.Resources
|
||||
out _,
|
||||
out ulong totalSizeInBytes);
|
||||
|
||||
this.d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(resourceType, allocationMode, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA);
|
||||
if (device.IsCacheCoherentUMA)
|
||||
{
|
||||
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, allocationMode, totalSizeInBytes, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.allocation = device.Allocator->CreateResource(resourceType, allocationMode, totalSizeInBytes);
|
||||
this.d3D12Resource = new ComPtr<ID3D12Resource>(this.allocation.Get()->GetResource());
|
||||
}
|
||||
|
||||
this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer;
|
||||
|
||||
this.d3D12Resource.Get()->SetName(this);
|
||||
@@ -117,6 +132,7 @@ namespace ComputeSharp.Resources
|
||||
protected override bool OnDispose()
|
||||
{
|
||||
this.d3D12Resource.Dispose();
|
||||
this.allocation.Dispose();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Core.Extensions;
|
||||
using ComputeSharp.Graphics.Commands;
|
||||
using ComputeSharp.Graphics.Extensions;
|
||||
using ComputeSharp.Shaders.Extensions;
|
||||
@@ -212,7 +211,7 @@ namespace ComputeSharp.Shaders
|
||||
using ComPtr<ID3D12RootSignature> d3D12RootSignature = device.D3D12Device->CreateRootSignature(shaderData.Loader.D3D12Root32BitConstantsCount, shaderData.Loader.D3D12DescriptorRanges1);
|
||||
using ComPtr<ID3D12PipelineState> d3D12PipelineState = device.D3D12Device->CreateComputePipelineState(d3D12RootSignature.Get(), shaderData.Bytecode.D3D12ShaderBytecode);
|
||||
|
||||
pipelineData = new PipelineData(d3D12RootSignature.Move(), d3D12PipelineState.Move());
|
||||
pipelineData = new PipelineData(d3D12RootSignature.Get(), d3D12PipelineState.Get());
|
||||
|
||||
shaderData.CachedPipelines.Add(device, pipelineData);
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ using ComputeSharp.__Internals;
|
||||
using ComputeSharp.Shaders.Translation.Models;
|
||||
using TerraFX.Interop;
|
||||
|
||||
#pragma warning disable CS0618
|
||||
#pragma warning disable CS0419, CS0618
|
||||
|
||||
namespace ComputeSharp.Shaders.Translation
|
||||
{
|
||||
|
||||
@@ -10,7 +10,7 @@ using Microsoft.Toolkit.Diagnostics;
|
||||
using TerraFX.Interop;
|
||||
using static TerraFX.Interop.D3D12_DESCRIPTOR_RANGE_TYPE;
|
||||
|
||||
#pragma warning disable CS0618, CS8618
|
||||
#pragma warning disable CS0419, CS0618, CS8618
|
||||
|
||||
namespace ComputeSharp.Shaders.Translation
|
||||
{
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする