From 65fe7f53c603626afc0cdf69d6bc180f34658eac Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 24 Jul 2022 15:56:50 +0200 Subject: [PATCH] Switch all GetLease() calls to using declarations --- .../Helpers/SwapChainManager.Rendering.cs | 57 +- .../GraphicsDeviceExtensions.Dispatching.cs | 9 +- .../Graphics/GraphicsDevice.DeviceLost.cs | 43 +- src/ComputeSharp/Graphics/GraphicsDevice.cs | 55 +- .../__Internals/GraphicsResourceHelper.cs | 55 +- .../Graphics/Resources/Abstract/Buffer{T}.cs | 99 ++- .../Resources/Abstract/StructuredBuffer{T}.cs | 367 ++++++----- .../Resources/Abstract/Texture2D{T}.cs | 592 +++++++++-------- .../Resources/Abstract/Texture3D{T}.cs | 610 +++++++++--------- .../Resources/Abstract/TransferBuffer{T}.cs | 48 +- .../Abstract/TransferTexture2D{T}.cs | 50 +- .../Abstract/TransferTexture3D{T}.cs | 52 +- .../Graphics/Resources/ConstantBuffer{T}.cs | 107 ++- .../ReadWriteTexture2D{T,TPixel}.ReadOnly.cs | 61 +- .../ReadWriteTexture2D{T}.ReadOnly.cs | 67 +- .../ReadWriteTexture3D{T,TPixel}.ReadOnly.cs | 61 +- .../ReadWriteTexture3D{T}.ReadOnly.cs | 61 +- 17 files changed, 1165 insertions(+), 1229 deletions(-) diff --git a/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs b/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs index 99c5edf2..261236b1 100644 --- a/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs +++ b/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs @@ -29,7 +29,8 @@ partial class SwapChainManager { Guard.IsNotNull(shaderRunner); - using (GetReferenceTracker().GetLease()) + using var _0 = GetReferenceTracker().GetLease(); + using (await this.setupSemaphore.LockAsync()) { this.renderCancellationTokenSource?.Cancel(); @@ -54,7 +55,8 @@ partial class SwapChainManager /// public async void StopRenderLoop() { - using (GetReferenceTracker().GetLease()) + using var _0 = GetReferenceTracker().GetLease(); + using (await this.setupSemaphore.LockAsync()) { this.renderCancellationTokenSource?.Cancel(); @@ -67,7 +69,8 @@ partial class SwapChainManager /// Whether or not to use dynamic resolution. public async void QueueDynamicResolutionModeChange(bool isDynamicResolutionEnabled) { - using (GetReferenceTracker().GetLease()) + using var _0 = GetReferenceTracker().GetLease(); + using (await this.setupSemaphore.LockAsync()) { // If there is a render thread currently running, stop it and restart it @@ -100,7 +103,8 @@ partial class SwapChainManager /// Whether or not to use vertical sync. public async void QueueVerticalSyncModeChange(bool isVerticalSyncEnabled) { - using (GetReferenceTracker().GetLease()) + using var _0 = GetReferenceTracker().GetLease(); + using (await this.setupSemaphore.LockAsync()) { // The v-sync option can be toggled on the fly when not using dynamic resolution @@ -134,13 +138,12 @@ partial class SwapChainManager /// The height of the render resolution. public void QueueResize(double width, double height) { - using (GetReferenceTracker().GetLease()) - { - this.width = (float)width; - this.height = (float)height; + using var _0 = GetReferenceTracker().GetLease(); - this.isResizePending = true; - } + this.width = (float)width; + this.height = (float)height; + + this.isResizePending = true; } /// @@ -150,13 +153,12 @@ partial class SwapChainManager /// The composition scale on the Y axis public void QueueCompositionScaleChange(double compositionScaleX, double compositionScaleY) { - using (GetReferenceTracker().GetLease()) - { - this.compositionScaleX = (float)compositionScaleX; - this.compositionScaleY = (float)compositionScaleY; + using var _0 = GetReferenceTracker().GetLease(); - this.isResizePending = true; - } + this.compositionScaleX = (float)compositionScaleX; + this.compositionScaleY = (float)compositionScaleY; + + this.isResizePending = true; } /// @@ -165,12 +167,11 @@ partial class SwapChainManager /// The resolution scale factor to use. public void QueueResolutionScaleChange(double resolutionScale) { - using (GetReferenceTracker().GetLease()) - { - this.resolutionScale = (float)resolutionScale; + using var _0 = GetReferenceTracker().GetLease(); - this.isResizePending = true; - } + this.resolutionScale = (float)resolutionScale; + + this.isResizePending = true; } /// @@ -187,17 +188,15 @@ partial class SwapChainManager this.dynamicResolutionScale = this.resolutionScale; // These two leases are needed to ensure the manager isn't disposed while rendering is running - using (GetReferenceTracker().GetLease()) - { - RenderLoopWithDynamicResolution(); - } + using var _0 = GetReferenceTracker().GetLease(); + + RenderLoopWithDynamicResolution(); } else { - using (GetReferenceTracker().GetLease()) - { - RenderLoop(); - } + using var _0 = GetReferenceTracker().GetLease(); + + RenderLoop(); } this.renderStopwatch?.Stop(); diff --git a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs index afdae583..658d4da4 100644 --- a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs +++ b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs @@ -150,11 +150,10 @@ public static partial class GraphicsDeviceExtensions { Guard.IsNotNull(device); - using (device.GetReferenceTracker().GetLease()) - { - device.ThrowIfDeviceLost(); + using var _0 = device.GetReferenceTracker().GetLease(); - return new(device); - } + device.ThrowIfDeviceLost(); + + return new(device); } } diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.DeviceLost.cs b/src/ComputeSharp/Graphics/GraphicsDevice.DeviceLost.cs index b44272ba..beeb283e 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.DeviceLost.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.DeviceLost.cs @@ -48,32 +48,31 @@ unsafe partial class GraphicsDevice /// private void QueueRaiseDeviceLostEventIfNeeded() { - using (GetReferenceTracker().TryGetLease(out bool leaseTaken)) + using var _0 = GetReferenceTracker().TryGetLease(out bool leaseTaken); + + if (!leaseTaken) { - if (!leaseTaken) - { - return; - } + 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(ref this.deviceRemovedReason)) != S.S_OK) - { - 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(ref this.deviceRemovedReason)) != S.S_OK) + { + return; + } - HRESULT result = this.d3D12Device.Get()->GetDeviceRemovedReason(); + 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(ref this.deviceRemovedReason), - (int)result, - S.S_OK) == S.S_OK) - { - QueueRaiseDeviceLostEvent(); - } + // Only raise the event once, and store the device removed reason to track it + if (result != S.S_OK && + Interlocked.CompareExchange( + ref Unsafe.As(ref this.deviceRemovedReason), + (int)result, + S.S_OK) == S.S_OK) + { + QueueRaiseDeviceLostEvent(); } } diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index 1e5bf383..a516d4a0 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -238,14 +238,13 @@ public sealed unsafe partial class GraphicsDevice : NativeObject /// Whether the current device supports double precision floating point operations in shaders. public bool IsDoublePrecisionSupportAvailable() { - using (GetReferenceTracker().GetLease()) - { - ThrowIfDeviceLost(); + using var _0 = GetReferenceTracker().GetLease(); - var d3D12OptionsData = this.d3D12Device.Get()->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS); + ThrowIfDeviceLost(); - return d3D12OptionsData.DoublePrecisionFloatShaderOps; - } + var d3D12OptionsData = this.d3D12Device.Get()->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS); + + return d3D12OptionsData.DoublePrecisionFloatShaderOps; } /// @@ -257,12 +256,11 @@ public sealed unsafe partial class GraphicsDevice : NativeObject public bool IsReadOnlyTexture2DSupportedForType() where T : unmanaged { - using (GetReferenceTracker().GetLease()) - { - ThrowIfDeviceLost(); + using var _0 = GetReferenceTracker().GetLease(); - return this.d3D12Device.Get()->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE2D); - } + ThrowIfDeviceLost(); + + return this.d3D12Device.Get()->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE2D); } /// @@ -274,14 +272,13 @@ public sealed unsafe partial class GraphicsDevice : NativeObject public bool IsReadWriteTexture2DSupportedForType() where T : unmanaged { - using (GetReferenceTracker().GetLease()) - { - ThrowIfDeviceLost(); + using var _0 = GetReferenceTracker().GetLease(); - return this.d3D12Device.Get()->IsDxgiFormatSupported( - DXGIFormatHelper.GetForType(), - D3D12_FORMAT_SUPPORT1_TEXTURE2D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW); - } + ThrowIfDeviceLost(); + + return this.d3D12Device.Get()->IsDxgiFormatSupported( + DXGIFormatHelper.GetForType(), + D3D12_FORMAT_SUPPORT1_TEXTURE2D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW); } /// @@ -293,12 +290,11 @@ public sealed unsafe partial class GraphicsDevice : NativeObject public bool IsReadOnlyTexture3DSupportedForType() where T : unmanaged { - using (GetReferenceTracker().GetLease()) - { - ThrowIfDeviceLost(); + using var _0 = GetReferenceTracker().GetLease(); - return this.d3D12Device.Get()->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE3D); - } + ThrowIfDeviceLost(); + + return this.d3D12Device.Get()->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE3D); } /// @@ -310,14 +306,13 @@ public sealed unsafe partial class GraphicsDevice : NativeObject public bool IsReadWriteTexture3DSupportedForType() where T : unmanaged { - using (GetReferenceTracker().GetLease()) - { - ThrowIfDeviceLost(); + using var _0 = GetReferenceTracker().GetLease(); - return this.d3D12Device.Get()->IsDxgiFormatSupported( - DXGIFormatHelper.GetForType(), - D3D12_FORMAT_SUPPORT1_TEXTURE3D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW); - } + ThrowIfDeviceLost(); + + return this.d3D12Device.Get()->IsDxgiFormatSupported( + DXGIFormatHelper.GetForType(), + D3D12_FORMAT_SUPPORT1_TEXTURE3D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW); } /// diff --git a/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs b/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs index 29317385..a326435a 100644 --- a/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs @@ -64,14 +64,13 @@ public static class GraphicsResourceHelper { Guard.IsNotNull(buffer); - using (buffer.GetReferenceTracker().GetLease()) - { - buffer.ThrowIfDeviceMismatch(device); + using var _0 = buffer.GetReferenceTracker().GetLease(); - D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = buffer.D3D12GpuDescriptorHandle; + buffer.ThrowIfDeviceMismatch(device); - return *(ulong*)&d3D12GpuDescriptorHandle; - } + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = buffer.D3D12GpuDescriptorHandle; + + return *(ulong*)&d3D12GpuDescriptorHandle; } /// @@ -87,14 +86,13 @@ public static class GraphicsResourceHelper { Guard.IsNotNull(texture); - using (texture.GetReferenceTracker().GetLease()) - { - texture.ThrowIfDeviceMismatch(device); + using var _0 = texture.GetReferenceTracker().GetLease(); - D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle; + texture.ThrowIfDeviceMismatch(device); - return *(ulong*)&d3D12GpuDescriptorHandle; - } + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle; + + return *(ulong*)&d3D12GpuDescriptorHandle; } /// @@ -110,14 +108,13 @@ public static class GraphicsResourceHelper { Guard.IsNotNull(texture); - using (texture.GetReferenceTracker().GetLease()) - { - texture.ThrowIfDeviceMismatch(device); + using var _0 = texture.GetReferenceTracker().GetLease(); - D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle; + texture.ThrowIfDeviceMismatch(device); - return *(ulong*)&d3D12GpuDescriptorHandle; - } + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle; + + return *(ulong*)&d3D12GpuDescriptorHandle; } /// @@ -202,14 +199,13 @@ public static class GraphicsResourceHelper { Guard.IsNotNull(texture); - using (texture.GetReferenceTracker().GetLease()) - { - texture.ThrowIfDeviceMismatch(device); + using var _0 = texture.GetReferenceTracker().GetLease(); - D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle; + texture.ThrowIfDeviceMismatch(device); - return *(ulong*)&d3D12GpuDescriptorHandle; - } + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle; + + return *(ulong*)&d3D12GpuDescriptorHandle; } /// @@ -225,14 +221,13 @@ public static class GraphicsResourceHelper { Guard.IsNotNull(texture); - using (texture.GetReferenceTracker().GetLease()) - { - texture.ThrowIfDeviceMismatch(device); + using var _0 = texture.GetReferenceTracker().GetLease(); - D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle; + texture.ThrowIfDeviceMismatch(device); - return *(ulong*)&d3D12GpuDescriptorHandle; - } + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle; + + return *(ulong*)&d3D12GpuDescriptorHandle; } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index 0a1f4745..69b53980 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -75,55 +75,54 @@ public unsafe abstract class Buffer : NativeObject, IGraphicsResource Guard.IsBetweenOrEqualTo(length, 1, (uint.MaxValue / elementSizeInBytes) & ~255); } - using (device.GetReferenceTracker().GetLease()) + using var _0 = device.GetReferenceTracker().GetLease(); + + device.ThrowIfDeviceLost(); + + if (TypeInfo.IsDoubleOrContainsDoubles && + device.D3D12Device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS).DoublePrecisionFloatShaderOps == 0) { - device.ThrowIfDeviceLost(); + UnsupportedDoubleOperationException.Throw(); + } - if (TypeInfo.IsDoubleOrContainsDoubles && - device.D3D12Device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS).DoublePrecisionFloatShaderOps == 0) - { - UnsupportedDoubleOperationException.Throw(); - } + nint usableSizeInBytes = checked((nint)(length * elementSizeInBytes)); + nint effectiveSizeInBytes = resourceType == ResourceType.Constant ? AlignmentHelper.Pad(usableSizeInBytes, D3D12.D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT) : usableSizeInBytes; - 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; + 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(this.allocation.Get()->GetResource()); + this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, (ulong)effectiveSizeInBytes); + this.d3D12Resource = new ComPtr(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; - } - - this.d3D12Resource.Get()->SetName(this); + 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); } /// @@ -234,24 +233,22 @@ public unsafe abstract class Buffer : NativeObject, IGraphicsResource /// internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device) { - using (GetReferenceTracker().GetLease()) - { - ThrowIfDeviceMismatch(device); + using var _0 = GetReferenceTracker().GetLease(); - return ( - this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12GpuDescriptorHandle, - this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12CpuDescriptorHandleNonShaderVisible); - } + ThrowIfDeviceMismatch(device); + + return ( + this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12GpuDescriptorHandle, + this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12CpuDescriptorHandleNonShaderVisible); } /// internal unsafe ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device) { - using (GetReferenceTracker().GetLease()) - { - ThrowIfDeviceMismatch(device); + using var _0 = GetReferenceTracker().GetLease(); - return D3D12Resource; - } + ThrowIfDeviceMismatch(device); + + return D3D12Resource; } } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index 1a49abdb..a5b88ddc 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -36,68 +36,67 @@ public abstract class StructuredBuffer : Buffer Guard.IsInRange(sourceOffset, 0, Length); Guard.IsLessThanOrEqualTo(sourceOffset + count, Length, nameof(sourceOffset)); - using (GraphicsDevice.GetReferenceTracker().GetLease()) - using (GetReferenceTracker().GetLease()) + using var _0 = GraphicsDevice.GetReferenceTracker().GetLease(); + using var _1 = GetReferenceTracker().GetLease(); + + GraphicsDevice.ThrowIfDeviceLost(); + + if (GraphicsDevice.IsCacheCoherentUMA) { - GraphicsDevice.ThrowIfDeviceLost(); + using ID3D12ResourceMap resource = D3D12Resource->Map(); - if (GraphicsDevice.IsCacheCoherentUMA) + fixed (void* destinationPointer = &destination) { - using ID3D12ResourceMap resource = D3D12Resource->Map(); - - fixed (void* destinationPointer = &destination) - { - MemoryHelper.Copy( - source: resource.Pointer, - destination: destinationPointer, - sourceElementOffset: (uint)sourceOffset, - destinationElementOffset: 0, - sourceElementPitchInBytes: (uint)sizeof(T), - destinationElementPitchInBytes: (uint)sizeof(T), - count: (uint)count); - } + MemoryHelper.Copy( + source: resource.Pointer, + destination: destinationPointer, + sourceElementOffset: (uint)sourceOffset, + destinationElementOffset: 0, + sourceElementPitchInBytes: (uint)sizeof(T), + destinationElementPitchInBytes: (uint)sizeof(T), + count: (uint)count); } - else + } + else + { + nint byteOffset = (nint)sourceOffset * sizeof(T); + nint byteLength = count * sizeof(T); + +#if NET6_0_OR_GREATER + using ComPtr allocation = GraphicsDevice.Allocator->CreateResource(null, ResourceType.ReadBack, AllocationMode.Default, (ulong)byteLength); +#else + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource( + ResourceType.ReadBack, + (ulong)byteLength, + GraphicsDevice.IsCacheCoherentUMA); +#endif + + using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY)) { - nint byteOffset = (nint)sourceOffset * sizeof(T); - nint byteLength = count * sizeof(T); +#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 ComPtr allocation = GraphicsDevice.Allocator->CreateResource(null, ResourceType.ReadBack, AllocationMode.Default, (ulong)byteLength); + using ID3D12ResourceMap resource = allocation.Get()->GetResource()->Map(); #else - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource( - ResourceType.ReadBack, - (ulong)byteLength, - GraphicsDevice.IsCacheCoherentUMA); + using ID3D12ResourceMap resource = d3D12Resource.Get()->Map(); #endif - 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( - source: resource.Pointer, - destination: destinationPointer, - sourceElementOffset: 0, - destinationElementOffset: 0, - sourceElementPitchInBytes: (uint)sizeof(T), - destinationElementPitchInBytes: (uint)sizeof(T), - count: (uint)count); - } + fixed (void* destinationPointer = &destination) + { + MemoryHelper.Copy( + source: resource.Pointer, + destination: destinationPointer, + sourceElementOffset: 0, + destinationElementOffset: 0, + sourceElementPitchInBytes: (uint)sizeof(T), + destinationElementPitchInBytes: (uint)sizeof(T), + count: (uint)count); } } } @@ -112,34 +111,33 @@ public abstract class StructuredBuffer : Buffer Guard.IsInRange(destinationOffset, 0, destination.Length); Guard.IsLessThanOrEqualTo(destinationOffset + count, destination.Length, nameof(destinationOffset)); - using (GraphicsDevice.GetReferenceTracker().GetLease()) - using (GetReferenceTracker().GetLease()) - using (destination.GetReferenceTracker().GetLease()) + using var _0 = GraphicsDevice.GetReferenceTracker().GetLease(); + using var _1 = GetReferenceTracker().GetLease(); + using var _2 = destination.GetReferenceTracker().GetLease(); + + GraphicsDevice.ThrowIfDeviceLost(); + + destination.ThrowIfDeviceMismatch(GraphicsDevice); + + if (!destination.IsPaddingPresent) { - GraphicsDevice.ThrowIfDeviceLost(); + nint sourceByteOffset = (nint)sourceOffset * sizeof(T); + nint destinationByteOffset = (nint)destinationOffset * sizeof(T); + nint byteLength = count * sizeof(T); - destination.ThrowIfDeviceMismatch(GraphicsDevice); + // Directly copy the input buffer + using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); - if (!destination.IsPaddingPresent) - { - nint sourceByteOffset = (nint)sourceOffset * sizeof(T); - nint destinationByteOffset = (nint)destinationOffset * sizeof(T); - nint byteLength = count * sizeof(T); + copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion( + destination.D3D12Resource, + (ulong)destinationByteOffset, + D3D12Resource, + (ulong)sourceByteOffset, + (ulong)byteLength); - // 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); + copyCommandList.ExecuteAndWaitForCompletion(); } + else CopyToWithCpuBuffer(destination, sourceOffset, destinationOffset, count); } /// @@ -158,38 +156,37 @@ public abstract class StructuredBuffer : Buffer Guard.IsInRange(destinationOffset, 0, destination.Length); Guard.IsLessThanOrEqualTo(destinationOffset + count, destination.Length, nameof(destinationOffset)); - using (GraphicsDevice.GetReferenceTracker().GetLease()) - using (GetReferenceTracker().GetLease()) - using (destination.GetReferenceTracker().GetLease()) + using var _0 = GraphicsDevice.GetReferenceTracker().GetLease(); + using var _1 = GetReferenceTracker().GetLease(); + using var _2 = destination.GetReferenceTracker().GetLease(); + + GraphicsDevice.ThrowIfDeviceLost(); + + destination.ThrowIfDeviceMismatch(GraphicsDevice); + + if (GraphicsDevice.IsCacheCoherentUMA) { - GraphicsDevice.ThrowIfDeviceLost(); + using ID3D12ResourceMap resource = D3D12Resource->Map(); - destination.ThrowIfDeviceMismatch(GraphicsDevice); + MemoryHelper.Copy( + 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); - if (GraphicsDevice.IsCacheCoherentUMA) - { - using ID3D12ResourceMap resource = D3D12Resource->Map(); + using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); - MemoryHelper.Copy( - 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(); - } + copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(destination.D3D12Resource, byteDestinationOffset, D3D12Resource, byteOffset, byteLength); + copyCommandList.ExecuteAndWaitForCompletion(); } } @@ -200,67 +197,66 @@ public abstract class StructuredBuffer : Buffer Guard.IsInRange(offset, 0, Length); Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset)); - using (GraphicsDevice.GetReferenceTracker().GetLease()) - using (GetReferenceTracker().GetLease()) + using var _0 = GraphicsDevice.GetReferenceTracker().GetLease(); + using var _1 = GetReferenceTracker().GetLease(); + + GraphicsDevice.ThrowIfDeviceLost(); + + if (GraphicsDevice.IsCacheCoherentUMA) { - GraphicsDevice.ThrowIfDeviceLost(); + using ID3D12ResourceMap resource = D3D12Resource->Map(); - if (GraphicsDevice.IsCacheCoherentUMA) + fixed (void* sourcePointer = &source) { - using ID3D12ResourceMap resource = D3D12Resource->Map(); - - fixed (void* sourcePointer = &source) - { - MemoryHelper.Copy( - source: sourcePointer, - destination: resource.Pointer, - sourceElementOffset: 0, - destinationElementOffset: (uint)offset, - sourceElementPitchInBytes: (uint)sizeof(T), - destinationElementPitchInBytes: (uint)sizeof(T), - count: (uint)length); - } + MemoryHelper.Copy( + source: sourcePointer, + destination: resource.Pointer, + sourceElementOffset: 0, + destinationElementOffset: (uint)offset, + sourceElementPitchInBytes: (uint)sizeof(T), + destinationElementPitchInBytes: (uint)sizeof(T), + count: (uint)length); } - else + } + else + { + nint byteOffset = (nint)offset * sizeof(T); + nint byteLength = length * sizeof(T); + +#if NET6_0_OR_GREATER + using ComPtr allocation = GraphicsDevice.Allocator->CreateResource(null, ResourceType.Upload, AllocationMode.Default, (ulong)byteLength); +#else + using ComPtr 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) { - nint byteOffset = (nint)offset * sizeof(T); - nint byteLength = length * sizeof(T); - -#if NET6_0_OR_GREATER - using ComPtr allocation = GraphicsDevice.Allocator->CreateResource(null, ResourceType.Upload, AllocationMode.Default, (ulong)byteLength); -#else - using ComPtr 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) - { - MemoryHelper.Copy( - 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(); + MemoryHelper.Copy( + 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(); } } @@ -280,38 +276,37 @@ public abstract class StructuredBuffer : Buffer Guard.IsInRange(destinationOffset, 0, Length); Guard.IsLessThanOrEqualTo(destinationOffset + count, Length, nameof(destinationOffset)); - using (GraphicsDevice.GetReferenceTracker().GetLease()) - using (GetReferenceTracker().GetLease()) - using (source.GetReferenceTracker().GetLease()) + using var _0 = GraphicsDevice.GetReferenceTracker().GetLease(); + using var _1 = GetReferenceTracker().GetLease(); + using var _2 = source.GetReferenceTracker().GetLease(); + + GraphicsDevice.ThrowIfDeviceLost(); + + source.ThrowIfDeviceMismatch(GraphicsDevice); + + if (GraphicsDevice.IsCacheCoherentUMA) { - GraphicsDevice.ThrowIfDeviceLost(); + using ID3D12ResourceMap resource = D3D12Resource->Map(); - source.ThrowIfDeviceMismatch(GraphicsDevice); + MemoryHelper.Copy( + 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); - if (GraphicsDevice.IsCacheCoherentUMA) - { - using ID3D12ResourceMap resource = D3D12Resource->Map(); + using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); - MemoryHelper.Copy( - 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(); - } + copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, byteOffset, source.D3D12Resource, byteSourceOffset, byteLength); + copyCommandList.ExecuteAndWaitForCompletion(); } } } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index 4dfd4596..78876122 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -75,66 +75,65 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION); Guard.IsBetweenOrEqualTo(height, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION); - using (device.GetReferenceTracker().GetLease()) + using var _0 = device.GetReferenceTracker().GetLease(); + + device.ThrowIfDeviceLost(); + + if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), d3D12FormatSupport)) { - device.ThrowIfDeviceLost(); + UnsupportedTextureTypeException.ThrowForTexture2D(); + } - if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), d3D12FormatSupport)) - { - UnsupportedTextureTypeException.ThrowForTexture2D(); - } - - GraphicsDevice = device; + GraphicsDevice = device; #if NET6_0_OR_GREATER - this.allocation = device.Allocator->CreateResource( - device.Pool, - resourceType, - allocationMode, - DXGIFormatHelper.GetForType(), - (uint)width, - (uint)height, - out this.d3D12ResourceState); + this.allocation = device.Allocator->CreateResource( + device.Pool, + resourceType, + allocationMode, + DXGIFormatHelper.GetForType(), + (uint)width, + (uint)height, + out this.d3D12ResourceState); - this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); + this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); #else - this.d3D12Resource = device.D3D12Device->CreateCommittedResource( - resourceType, - DXGIFormatHelper.GetForType(), - (uint)width, - (uint)height, - device.IsCacheCoherentUMA, - out this.d3D12ResourceState); + this.d3D12Resource = device.D3D12Device->CreateCommittedResource( + resourceType, + DXGIFormatHelper.GetForType(), + (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(), - (uint)width, - (uint)height, - out this.d3D12PlacedSubresourceFootprint, - out _, - out _); + device.D3D12Device->GetCopyableFootprint( + DXGIFormatHelper.GetForType(), + (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(), D3D12_SRV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); - break; - case ResourceType.ReadWrite: - device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); - device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible); - break; - } - - this.d3D12Resource.Get()->SetName(this); + switch (resourceType) + { + case ResourceType.ReadOnly: + device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); + break; + case ResourceType.ReadWrite: + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible); + break; } + + this.d3D12Resource.Get()->SetName(this); } /// @@ -179,74 +178,73 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY)); Guard.IsGreaterThanOrEqualTo(size, (nint)width * height); - using (GraphicsDevice.GetReferenceTracker().GetLease()) - using (GetReferenceTracker().GetLease()) - { - GraphicsDevice.ThrowIfDeviceLost(); + using var _0 = GraphicsDevice.GetReferenceTracker().GetLease(); + using var _1 = GetReferenceTracker().GetLease(); - GraphicsDevice.D3D12Device->GetCopyableFootprint( - DXGIFormatHelper.GetForType(), - (uint)width, - (uint)height, - out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintDestination, - out ulong rowSizeInBytes, - out ulong totalSizeInBytes); + GraphicsDevice.ThrowIfDeviceLost(); + + GraphicsDevice.D3D12Device->GetCopyableFootprint( + DXGIFormatHelper.GetForType(), + (uint)width, + (uint)height, + out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintDestination, + out ulong rowSizeInBytes, + out ulong totalSizeInBytes); #if NET6_0_OR_GREATER - using ComPtr allocation = GraphicsDevice.Allocator->CreateResource( - GraphicsDevice.Pool, - ResourceType.ReadBack, - AllocationMode.Default, - totalSizeInBytes); + using ComPtr allocation = GraphicsDevice.Allocator->CreateResource( + GraphicsDevice.Pool, + ResourceType.ReadBack, + AllocationMode.Default, + totalSizeInBytes); - using ComPtr d3D12Resource = new(allocation.Get()->GetResource()); + using ComPtr d3D12Resource = new(allocation.Get()->GetResource()); #else - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource( - ResourceType.ReadBack, - totalSizeInBytes, - GraphicsDevice.IsCacheCoherentUMA); + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource( + ResourceType.ReadBack, + totalSizeInBytes, + GraphicsDevice.IsCacheCoherentUMA); #endif - using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType)) + using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType)) + { + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) { - 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->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE); } - using ID3D12ResourceMap resource = d3D12Resource.Get()->Map(); + 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); - fixed (void* destinationPointer = &destination) + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) { - MemoryHelper.Copy( - resource.Pointer, - (uint)height, - rowSizeInBytes, - d3D12PlacedSubresourceFootprintDestination.Footprint.RowPitch, - destinationPointer); + copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState); } + + copyCommandList.ExecuteAndWaitForCompletion(); + } + + using ID3D12ResourceMap resource = d3D12Resource.Get()->Map(); + + fixed (void* destinationPointer = &destination) + { + MemoryHelper.Copy( + resource.Pointer, + (uint)height, + rowSizeInBytes, + d3D12PlacedSubresourceFootprintDestination.Footprint.RowPitch, + destinationPointer); } } @@ -275,49 +273,48 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY)); - using (GraphicsDevice.GetReferenceTracker().GetLease()) - using (GetReferenceTracker().GetLease()) - using (destination.GetReferenceTracker().GetLease()) + using var _0 = GraphicsDevice.GetReferenceTracker().GetLease(); + using var _1 = GetReferenceTracker().GetLease(); + using var _2 = destination.GetReferenceTracker().GetLease(); + + GraphicsDevice.ThrowIfDeviceLost(); + + destination.ThrowIfDeviceMismatch(GraphicsDevice); + + 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) { - GraphicsDevice.ThrowIfDeviceLost(); - - destination.ThrowIfDeviceMismatch(GraphicsDevice); - - 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->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(); } /// @@ -345,45 +342,44 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY)); - using (GraphicsDevice.GetReferenceTracker().GetLease()) - using (GetReferenceTracker().GetLease()) - using (destination.GetReferenceTracker().GetLease()) + using var _0 = GraphicsDevice.GetReferenceTracker().GetLease(); + using var _1 = GetReferenceTracker().GetLease(); + using var _2 = destination.GetReferenceTracker().GetLease(); + + GraphicsDevice.ThrowIfDeviceLost(); + + destination.ThrowIfDeviceMismatch(GraphicsDevice); + + using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType); + + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) { - GraphicsDevice.ThrowIfDeviceLost(); - - destination.ThrowIfDeviceMismatch(GraphicsDevice); - - 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(); + 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(); } /// @@ -405,73 +401,72 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY)); Guard.IsGreaterThanOrEqualTo(size, (nint)width * height); - using (GraphicsDevice.GetReferenceTracker().GetLease()) - using (GetReferenceTracker().GetLease()) - { - GraphicsDevice.ThrowIfDeviceLost(); + using var _0 = GraphicsDevice.GetReferenceTracker().GetLease(); + using var _1 = GetReferenceTracker().GetLease(); - GraphicsDevice.D3D12Device->GetCopyableFootprint( - DXGIFormatHelper.GetForType(), - (uint)width, - (uint)height, - out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintSource, - out ulong rowSizeInBytes, - out ulong totalSizeInBytes); + GraphicsDevice.ThrowIfDeviceLost(); + + GraphicsDevice.D3D12Device->GetCopyableFootprint( + DXGIFormatHelper.GetForType(), + (uint)width, + (uint)height, + out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintSource, + out ulong rowSizeInBytes, + out ulong totalSizeInBytes); #if NET6_0_OR_GREATER - using ComPtr allocation = GraphicsDevice.Allocator->CreateResource( - GraphicsDevice.Pool, - ResourceType.Upload, - AllocationMode.Default, - totalSizeInBytes); + using ComPtr allocation = GraphicsDevice.Allocator->CreateResource( + GraphicsDevice.Pool, + ResourceType.Upload, + AllocationMode.Default, + totalSizeInBytes); - using ComPtr d3D12Resource = new(allocation.Get()->GetResource()); + using ComPtr d3D12Resource = new(allocation.Get()->GetResource()); #else - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource( - ResourceType.Upload, - totalSizeInBytes, - GraphicsDevice.IsCacheCoherentUMA); + using ComPtr 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, - (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, + using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) + fixed (void* sourcePointer = &source) + { + MemoryHelper.Copy( + sourcePointer, + resource.Pointer, (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(); + 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, + depth: 1); + + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState); + } + + copyCommandList.ExecuteAndWaitForCompletion(); } /// @@ -499,45 +494,44 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX)); Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY)); - using (GraphicsDevice.GetReferenceTracker().GetLease()) - using (GetReferenceTracker().GetLease()) - using (source.GetReferenceTracker().GetLease()) + using var _0 = GraphicsDevice.GetReferenceTracker().GetLease(); + using var _1 = GetReferenceTracker().GetLease(); + using var _2 = source.GetReferenceTracker().GetLease(); + + GraphicsDevice.ThrowIfDeviceLost(); + + source.ThrowIfDeviceMismatch(GraphicsDevice); + + using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType); + + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) { - GraphicsDevice.ThrowIfDeviceLost(); - - source.ThrowIfDeviceMismatch(GraphicsDevice); - - 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(); + 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(); } /// @@ -589,54 +583,50 @@ public unsafe abstract class Texture2D : NativeObject, IGraphicsResource, Gra /// internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) { - using (GetReferenceTracker().GetLease()) - { - ThrowIfDeviceMismatch(device); + using var _0 = GetReferenceTracker().GetLease(); - isNormalized = DXGIFormatHelper.IsNormalizedType(); + ThrowIfDeviceMismatch(device); - return (this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible); - } + isNormalized = DXGIFormatHelper.IsNormalizedType(); + + return (this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible); } /// internal ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device) { - using (GetReferenceTracker().GetLease()) - { - ThrowIfDeviceMismatch(device); + using var _0 = GetReferenceTracker().GetLease(); - return D3D12Resource; - } + ThrowIfDeviceMismatch(device); + + return D3D12Resource; } /// internal (D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource) { - using (GetReferenceTracker().GetLease()) - { - ThrowIfDeviceMismatch(device); + using var _0 = GetReferenceTracker().GetLease(); - D3D12_RESOURCE_STATES d3D12ResourceStatesBefore = this.d3D12ResourceState; - D3D12_RESOURCE_STATES d3D12ResourceStatesAfter = ResourceStateHelper.GetD3D12ResourceStates(resourceState); + ThrowIfDeviceMismatch(device); - this.d3D12ResourceState = d3D12ResourceStatesAfter; + D3D12_RESOURCE_STATES d3D12ResourceStatesBefore = this.d3D12ResourceState; + D3D12_RESOURCE_STATES d3D12ResourceStatesAfter = ResourceStateHelper.GetD3D12ResourceStates(resourceState); - d3D12Resource = D3D12Resource; + this.d3D12ResourceState = d3D12ResourceStatesAfter; - return (d3D12ResourceStatesBefore, d3D12ResourceStatesAfter); - } + d3D12Resource = D3D12Resource; + + return (d3D12ResourceStatesBefore, d3D12ResourceStatesAfter); } /// D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) { - using (GetReferenceTracker().GetLease()) - { - ThrowIfDeviceMismatch(device); + using var _0 = GetReferenceTracker().GetLease(); - return D3D12GpuDescriptorHandle; - } + ThrowIfDeviceMismatch(device); + + return D3D12GpuDescriptorHandle; } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 7afe5fe3..090b3212 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -77,69 +77,68 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra 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); - using (device.GetReferenceTracker().GetLease()) + using var _0 = device.GetReferenceTracker().GetLease(); + + device.ThrowIfDeviceLost(); + + if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), d3D12FormatSupport)) { - device.ThrowIfDeviceLost(); + UnsupportedTextureTypeException.ThrowForTexture3D(); + } - if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), d3D12FormatSupport)) - { - UnsupportedTextureTypeException.ThrowForTexture3D(); - } - - GraphicsDevice = device; + GraphicsDevice = device; #if NET6_0_OR_GREATER - this.allocation = device.Allocator->CreateResource( - device.Pool, - resourceType, - allocationMode, - DXGIFormatHelper.GetForType(), - (uint)width, - (uint)height, - (ushort)depth, - out this.d3D12ResourceState); + this.allocation = device.Allocator->CreateResource( + device.Pool, + resourceType, + allocationMode, + DXGIFormatHelper.GetForType(), + (uint)width, + (uint)height, + (ushort)depth, + out this.d3D12ResourceState); - this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); + this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); #else - this.d3D12Resource = device.D3D12Device->CreateCommittedResource( - resourceType, - DXGIFormatHelper.GetForType(), - (uint)width, - (uint)height, - (ushort)depth, - device.IsCacheCoherentUMA, - out this.d3D12ResourceState); + this.d3D12Resource = device.D3D12Device->CreateCommittedResource( + resourceType, + DXGIFormatHelper.GetForType(), + (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(), - (uint)width, - (uint)height, - (ushort)depth, - out this.d3D12PlacedSubresourceFootprint, - out _, - out _); + device.D3D12Device->GetCopyableFootprint( + DXGIFormatHelper.GetForType(), + (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(), D3D12_SRV_DIMENSION_TEXTURE3D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); - break; - case ResourceType.ReadWrite: - device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE3D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); - device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE3D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible); - break; - } - - this.d3D12Resource.Get()->SetName(this); + switch (resourceType) + { + case ResourceType.ReadOnly: + device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE3D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); + break; + case ResourceType.ReadWrite: + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE3D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE3D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible); + break; } + + this.d3D12Resource.Get()->SetName(this); } /// @@ -194,77 +193,76 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ)); Guard.IsGreaterThanOrEqualTo(size, (nint)width * height * depth); - using (GraphicsDevice.GetReferenceTracker().GetLease()) - using (GetReferenceTracker().GetLease()) - { - GraphicsDevice.ThrowIfDeviceLost(); + using var _0 = GraphicsDevice.GetReferenceTracker().GetLease(); + using var _1 = GetReferenceTracker().GetLease(); - GraphicsDevice.D3D12Device->GetCopyableFootprint( - DXGIFormatHelper.GetForType(), - (uint)width, - (uint)height, - (ushort)depth, - out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintDestination, - out ulong rowSizeInBytes, - out ulong totalSizeInBytes); + GraphicsDevice.ThrowIfDeviceLost(); + + GraphicsDevice.D3D12Device->GetCopyableFootprint( + DXGIFormatHelper.GetForType(), + (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 allocation = GraphicsDevice.Allocator->CreateResource( - GraphicsDevice.Pool, - ResourceType.ReadBack, - AllocationMode.Default, - totalSizeInBytes); + using ComPtr allocation = GraphicsDevice.Allocator->CreateResource( + GraphicsDevice.Pool, + ResourceType.ReadBack, + AllocationMode.Default, + totalSizeInBytes); - using ComPtr d3D12Resource = new(allocation.Get()->GetResource()); + using ComPtr d3D12Resource = new(allocation.Get()->GetResource()); #else - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource( - ResourceType.ReadBack, - totalSizeInBytes, - GraphicsDevice.IsCacheCoherentUMA); + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource( + ResourceType.ReadBack, + totalSizeInBytes, + GraphicsDevice.IsCacheCoherentUMA); #endif - using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType)) + using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType)) + { + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) { - 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.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE); } - using ID3D12ResourceMap resource = d3D12Resource.Get()->Map(); + 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); - fixed (void* destinationPointer = &destination) + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) { - MemoryHelper.Copy( - resource.Pointer, - (uint)height, - (uint)depth, - rowSizeInBytes, - d3D12PlacedSubresourceFootprintDestination.Footprint.RowPitch, - d3D12PlacedSubresourceFootprintDestination.Footprint.RowPitch * (uint)height, - destinationPointer); + copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState); } + + copyCommandList.ExecuteAndWaitForCompletion(); + } + + 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); } } @@ -302,49 +300,48 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY)); Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ)); - using (GraphicsDevice.GetReferenceTracker().GetLease()) - using (GetReferenceTracker().GetLease()) - using (destination.GetReferenceTracker().GetLease()) + using var _0 = GraphicsDevice.GetReferenceTracker().GetLease(); + using var _1 = GetReferenceTracker().GetLease(); + using var _2 = destination.GetReferenceTracker().GetLease(); + + GraphicsDevice.ThrowIfDeviceLost(); + + destination.ThrowIfDeviceMismatch(GraphicsDevice); + + 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) { - GraphicsDevice.ThrowIfDeviceLost(); - - destination.ThrowIfDeviceMismatch(GraphicsDevice); - - 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->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(); } /// @@ -381,45 +378,44 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY)); Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ)); - using (GraphicsDevice.GetReferenceTracker().GetLease()) - using (GetReferenceTracker().GetLease()) - using (destination.GetReferenceTracker().GetLease()) + using var _0 = GraphicsDevice.GetReferenceTracker().GetLease(); + using var _1 = GetReferenceTracker().GetLease(); + using var _2 = destination.GetReferenceTracker().GetLease(); + + GraphicsDevice.ThrowIfDeviceLost(); + + destination.ThrowIfDeviceMismatch(GraphicsDevice); + + using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType); + + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) { - GraphicsDevice.ThrowIfDeviceLost(); - - destination.ThrowIfDeviceMismatch(GraphicsDevice); - - 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(); + 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(); } /// @@ -446,76 +442,75 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra Guard.IsLessThanOrEqualTo(destinationOffsetZ + depth, Depth, nameof(destinationOffsetZ)); Guard.IsGreaterThanOrEqualTo(size, (nint)width * height * depth); - using (GraphicsDevice.GetReferenceTracker().GetLease()) - using (GetReferenceTracker().GetLease()) - { - GraphicsDevice.ThrowIfDeviceLost(); + using var _0 = GraphicsDevice.GetReferenceTracker().GetLease(); + using var _1 = GetReferenceTracker().GetLease(); - GraphicsDevice.D3D12Device->GetCopyableFootprint( - DXGIFormatHelper.GetForType(), - (uint)width, - (uint)height, - (ushort)depth, - out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintSource, - out ulong rowSizeInBytes, - out ulong totalSizeInBytes); + GraphicsDevice.ThrowIfDeviceLost(); + + GraphicsDevice.D3D12Device->GetCopyableFootprint( + DXGIFormatHelper.GetForType(), + (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 allocation = GraphicsDevice.Allocator->CreateResource( - GraphicsDevice.Pool, - ResourceType.Upload, - AllocationMode.Default, - totalSizeInBytes); + using ComPtr allocation = GraphicsDevice.Allocator->CreateResource( + GraphicsDevice.Pool, + ResourceType.Upload, + AllocationMode.Default, + totalSizeInBytes); - using ComPtr d3D12Resource = new(allocation.Get()->GetResource()); + using ComPtr d3D12Resource = new(allocation.Get()->GetResource()); #else - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource( - ResourceType.Upload, - totalSizeInBytes, - GraphicsDevice.IsCacheCoherentUMA); + using ComPtr 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, - (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, + using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) + fixed (void* sourcePointer = &source) + { + MemoryHelper.Copy( + sourcePointer, + resource.Pointer, (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(); + (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, + (ushort)depth); + + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState); + } + + copyCommandList.ExecuteAndWaitForCompletion(); } /// @@ -552,45 +547,44 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY)); Guard.IsLessThanOrEqualTo(destinationOffsetZ + depth, Depth, nameof(destinationOffsetZ)); - using (GraphicsDevice.GetReferenceTracker().GetLease()) - using (GetReferenceTracker().GetLease()) - using (source.GetReferenceTracker().GetLease()) + using var _0 = GraphicsDevice.GetReferenceTracker().GetLease(); + using var _1 = GetReferenceTracker().GetLease(); + using var _2 = source.GetReferenceTracker().GetLease(); + + GraphicsDevice.ThrowIfDeviceLost(); + + source.ThrowIfDeviceMismatch(GraphicsDevice); + + using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType); + + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) { - GraphicsDevice.ThrowIfDeviceLost(); - - source.ThrowIfDeviceMismatch(GraphicsDevice); - - 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(); + 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(); } /// @@ -642,54 +636,50 @@ public unsafe abstract class Texture3D : NativeObject, IGraphicsResource, Gra /// internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) { - using (GetReferenceTracker().GetLease()) - { - ThrowIfDeviceMismatch(device); + using var _0 = GetReferenceTracker().GetLease(); - isNormalized = DXGIFormatHelper.IsNormalizedType(); + ThrowIfDeviceMismatch(device); - return (this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible); - } + isNormalized = DXGIFormatHelper.IsNormalizedType(); + + return (this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible); } /// internal ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device) { - using (GetReferenceTracker().GetLease()) - { - ThrowIfDeviceMismatch(device); + using var _0 = GetReferenceTracker().GetLease(); - return D3D12Resource; - } + ThrowIfDeviceMismatch(device); + + return D3D12Resource; } /// internal (D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource) { - using (GetReferenceTracker().GetLease()) - { - ThrowIfDeviceMismatch(device); + using var _0 = GetReferenceTracker().GetLease(); - D3D12_RESOURCE_STATES d3D12ResourceStatesBefore = this.d3D12ResourceState; - D3D12_RESOURCE_STATES d3D12ResourceStatesAfter = ResourceStateHelper.GetD3D12ResourceStates(resourceState); + ThrowIfDeviceMismatch(device); - this.d3D12ResourceState = d3D12ResourceStatesAfter; + D3D12_RESOURCE_STATES d3D12ResourceStatesBefore = this.d3D12ResourceState; + D3D12_RESOURCE_STATES d3D12ResourceStatesAfter = ResourceStateHelper.GetD3D12ResourceStates(resourceState); - d3D12Resource = D3D12Resource; + this.d3D12ResourceState = d3D12ResourceStatesAfter; - return (d3D12ResourceStatesBefore, d3D12ResourceStatesAfter); - } + d3D12Resource = D3D12Resource; + + return (d3D12ResourceStatesBefore, d3D12ResourceStatesAfter); } /// D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) { - using (GetReferenceTracker().GetLease()) - { - ThrowIfDeviceMismatch(device); + using var _0 = GetReferenceTracker().GetLease(); - return D3D12GpuDescriptorHandle; - } + ThrowIfDeviceMismatch(device); + + return D3D12GpuDescriptorHandle; } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs index 53f03fcb..93c68d5e 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs @@ -47,28 +47,27 @@ public abstract unsafe class TransferBuffer : NativeObject, IGraphicsResource // 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(); + using var _0 = device.GetReferenceTracker().GetLease(); - GraphicsDevice = device; - Length = length; + device.ThrowIfDeviceLost(); - 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(this.allocation.Get()->GetResource()); + this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, sizeInBytes); + this.d3D12Resource = new ComPtr(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); } /// @@ -95,10 +94,9 @@ public abstract unsafe class TransferBuffer : NativeObject, IGraphicsResource [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - using (GetReferenceTracker().GetLease()) - { - return new MemoryManager(this).Memory; - } + using var _0 = GetReferenceTracker().GetLease(); + + return new MemoryManager(this).Memory; } } @@ -108,10 +106,9 @@ public abstract unsafe class TransferBuffer : NativeObject, IGraphicsResource [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - using (GetReferenceTracker().GetLease()) - { - return new(this.mappedData, Length); - } + using var _0 = GetReferenceTracker().GetLease(); + + return new(this.mappedData, Length); } } @@ -175,12 +172,11 @@ public abstract unsafe class TransferBuffer : NativeObject, IGraphicsResource /// public override MemoryHandle Pin(int elementIndex = 0) { - using (this.buffer.GetReferenceTracker().GetLease()) - { - Guard.IsEqualTo(elementIndex, 0); + Guard.IsEqualTo(elementIndex, 0); - return new(this.buffer.mappedData); - } + using var _0 = this.buffer.GetReferenceTracker().GetLease(); + + return new(this.buffer.mappedData); } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs index 4cf69cb3..232e59ed 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs @@ -53,38 +53,37 @@ public unsafe abstract class TransferTexture2D : NativeObject, IGraphicsResou Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION); Guard.IsBetweenOrEqualTo(height, 1, D3D12.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION); - using (device.GetReferenceTracker().GetLease()) + using var _0 = device.GetReferenceTracker().GetLease(); + + device.ThrowIfDeviceLost(); + + if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE2D)) { - device.ThrowIfDeviceLost(); + UnsupportedTextureTypeException.ThrowForTexture2D(); + } - if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE2D)) - { - UnsupportedTextureTypeException.ThrowForTexture2D(); - } + GraphicsDevice = device; - GraphicsDevice = device; - - device.D3D12Device->GetCopyableFootprint( - DXGIFormatHelper.GetForType(), - (uint)width, - (uint)height, - out this.d3D12PlacedSubresourceFootprint, - out _, - out ulong totalSizeInBytes); + device.D3D12Device->GetCopyableFootprint( + DXGIFormatHelper.GetForType(), + (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(this.allocation.Get()->GetResource()); + this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, totalSizeInBytes); + this.d3D12Resource = new ComPtr(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); } /// @@ -116,10 +115,9 @@ public unsafe abstract class TransferTexture2D : NativeObject, IGraphicsResou [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - using (this.GetReferenceTracker().GetLease()) - { - return new(this.mappedData, Width, Height, (int)this.d3D12PlacedSubresourceFootprint.Footprint.RowPitch); - } + using var _0 = this.GetReferenceTracker().GetLease(); + + return new(this.mappedData, Width, Height, (int)this.d3D12PlacedSubresourceFootprint.Footprint.RowPitch); } } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs index 149c53f9..04bf0bb6 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs @@ -55,39 +55,38 @@ public unsafe abstract class TransferTexture3D : NativeObject, IGraphicsResou 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); - using (device.GetReferenceTracker().GetLease()) + using var _0 = device.GetReferenceTracker().GetLease(); + + device.ThrowIfDeviceLost(); + + if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE3D)) { - device.ThrowIfDeviceLost(); + UnsupportedTextureTypeException.ThrowForTexture3D(); + } - if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE3D)) - { - UnsupportedTextureTypeException.ThrowForTexture3D(); - } + GraphicsDevice = device; - GraphicsDevice = device; - - device.D3D12Device->GetCopyableFootprint( - DXGIFormatHelper.GetForType(), - (uint)width, - (uint)height, - (ushort)depth, - out this.d3D12PlacedSubresourceFootprint, - out _, - out ulong totalSizeInBytes); + device.D3D12Device->GetCopyableFootprint( + DXGIFormatHelper.GetForType(), + (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(this.allocation.Get()->GetResource()); + this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, totalSizeInBytes); + this.d3D12Resource = new ComPtr(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); } /// @@ -124,10 +123,9 @@ public unsafe abstract class TransferTexture3D : NativeObject, IGraphicsResou [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - using (this.GetReferenceTracker().GetLease()) - { - return new(this.mappedData, Width, Height, Depth, (int)this.d3D12PlacedSubresourceFootprint.Footprint.RowPitch); - } + using var _0 = this.GetReferenceTracker().GetLease(); + + return new(this.mappedData, Width, Height, Depth, (int)this.d3D12PlacedSubresourceFootprint.Footprint.RowPitch); } } diff --git a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs index fcd3b810..490f9b6f 100644 --- a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs @@ -61,24 +61,23 @@ public sealed class ConstantBuffer : Buffer Guard.IsInRange(offset, 0, Length); Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset)); - using (GraphicsDevice.GetReferenceTracker().GetLease()) - using (GetReferenceTracker().GetLease()) + using var _0 = GraphicsDevice.GetReferenceTracker().GetLease(); + using var _1 = GetReferenceTracker().GetLease(); + + GraphicsDevice.ThrowIfDeviceLost(); + + using ID3D12ResourceMap resource = D3D12Resource->Map(); + + fixed (void* destinationPointer = &destination) { - GraphicsDevice.ThrowIfDeviceLost(); - - using ID3D12ResourceMap resource = D3D12Resource->Map(); - - fixed (void* destinationPointer = &destination) - { - MemoryHelper.Copy( - source: resource.Pointer, - destination: destinationPointer, - sourceElementOffset: (uint)offset, - destinationElementOffset: 0, - sourceElementPitchInBytes: (uint)GetPaddedSize(), - destinationElementPitchInBytes: (uint)sizeof(T), - count: (uint)length); - } + MemoryHelper.Copy( + source: resource.Pointer, + destination: destinationPointer, + sourceElementOffset: (uint)offset, + destinationElementOffset: 0, + sourceElementPitchInBytes: (uint)GetPaddedSize(), + destinationElementPitchInBytes: (uint)sizeof(T), + count: (uint)length); } } @@ -92,30 +91,29 @@ public sealed class ConstantBuffer : Buffer Guard.IsInRange(destinationOffset, 0, destination.Length); Guard.IsLessThanOrEqualTo(destinationOffset + length, destination.Length, nameof(destinationOffset)); - using (GraphicsDevice.GetReferenceTracker().GetLease()) - using (GetReferenceTracker().GetLease()) - using (destination.GetReferenceTracker().GetLease()) + using var _0 = GraphicsDevice.GetReferenceTracker().GetLease(); + using var _1 = GetReferenceTracker().GetLease(); + using var _2 = destination.GetReferenceTracker().GetLease(); + + GraphicsDevice.ThrowIfDeviceLost(); + + destination.ThrowIfDeviceMismatch(GraphicsDevice); + + if (destination is ConstantBuffer buffer) { - GraphicsDevice.ThrowIfDeviceLost(); + using ID3D12ResourceMap sourceMap = D3D12Resource->Map(); + using ID3D12ResourceMap destinationMap = buffer.D3D12Resource->Map(); - destination.ThrowIfDeviceMismatch(GraphicsDevice); - - if (destination is ConstantBuffer buffer) - { - using ID3D12ResourceMap sourceMap = D3D12Resource->Map(); - using ID3D12ResourceMap destinationMap = buffer.D3D12Resource->Map(); - - MemoryHelper.Copy( - 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); + MemoryHelper.Copy( + 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); } /// @@ -125,24 +123,23 @@ public sealed class ConstantBuffer : Buffer Guard.IsInRange(offset, 0, Length); Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset)); - using (GraphicsDevice.GetReferenceTracker().GetLease()) - using (GetReferenceTracker().GetLease()) + using var _0 = GraphicsDevice.GetReferenceTracker().GetLease(); + using var _1 = GetReferenceTracker().GetLease(); + + GraphicsDevice.ThrowIfDeviceLost(); + + using ID3D12ResourceMap resource = D3D12Resource->Map(); + + fixed (void* sourcePointer = &source) { - GraphicsDevice.ThrowIfDeviceLost(); - - using ID3D12ResourceMap resource = D3D12Resource->Map(); - - fixed (void* sourcePointer = &source) - { - MemoryHelper.Copy( - source: sourcePointer, - destination: resource.Pointer, - sourceElementOffset: 0, - destinationElementOffset: (uint)offset, - sourceElementPitchInBytes: (uint)sizeof(T), - destinationElementPitchInBytes: (uint)GetPaddedSize(), - count: (uint)length); - } + MemoryHelper.Copy( + source: sourcePointer, + destination: resource.Pointer, + sourceElementOffset: 0, + destinationElementOffset: (uint)offset, + sourceElementPitchInBytes: (uint)sizeof(T), + destinationElementPitchInBytes: (uint)GetPaddedSize(), + count: (uint)length); } } diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs index 014ba208..925c63e8 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.ReadOnly.cs @@ -24,28 +24,27 @@ partial class ReadWriteTexture2D /// internal IReadOnlyNormalizedTexture2D AsReadOnly() { - using (GraphicsDevice.GetReferenceTracker().GetLease()) - using (GetReferenceTracker().GetLease()) + using var _0 = GraphicsDevice.GetReferenceTracker().GetLease(); + using var _1 = GetReferenceTracker().GetLease(); + + GraphicsDevice.ThrowIfDeviceLost(); + + ThrowIfIsNotInReadOnlyState(); + + ReadOnly? readOnlyWrapper = this.readOnlyWrapper; + + if (readOnlyWrapper is not null) { - GraphicsDevice.ThrowIfDeviceLost(); - - ThrowIfIsNotInReadOnlyState(); - - ReadOnly? readOnlyWrapper = this.readOnlyWrapper; - - if (readOnlyWrapper is not null) - { - return readOnlyWrapper; - } - - [MethodImpl(MethodImplOptions.NoInlining)] - static ReadOnly InitializeWrapper(ReadWriteTexture2D texture) - { - return texture.readOnlyWrapper = new(texture); - } - - return InitializeWrapper(this); + return readOnlyWrapper; } + + [MethodImpl(MethodImplOptions.NoInlining)] + static ReadOnly InitializeWrapper(ReadWriteTexture2D texture) + { + return texture.readOnlyWrapper = new(texture); + } + + return InitializeWrapper(this); } /// @@ -108,13 +107,12 @@ partial class ReadWriteTexture2D /// D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) { - using (GetReferenceTracker().GetLease()) - using (this.owner.GetReferenceTracker().GetLease()) - { - this.owner.ThrowIfDeviceMismatch(device); + using var _0 = GetReferenceTracker().GetLease(); + using var _1 = this.owner.GetReferenceTracker().GetLease(); - return this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle; - } + this.owner.ThrowIfDeviceMismatch(device); + + return this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle; } /// @@ -126,13 +124,12 @@ partial class ReadWriteTexture2D /// ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device) { - using (GetReferenceTracker().GetLease()) - using (this.owner.GetReferenceTracker().GetLease()) - { - this.owner.ThrowIfDeviceMismatch(device); + using var _0 = GetReferenceTracker().GetLease(); + using var _1 = this.owner.GetReferenceTracker().GetLease(); - return this.owner.D3D12Resource; - } + this.owner.ThrowIfDeviceMismatch(device); + + return this.owner.D3D12Resource; } /// diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T}.ReadOnly.cs index 091fae0b..b1ea02fc 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T}.ReadOnly.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T}.ReadOnly.cs @@ -24,31 +24,30 @@ partial class ReadWriteTexture2D /// public IReadOnlyTexture2D AsReadOnly() { - using (GraphicsDevice.GetReferenceTracker().GetLease()) - using (GetReferenceTracker().GetLease()) + using var _0 = GraphicsDevice.GetReferenceTracker().GetLease(); + using var _1 = GetReferenceTracker().GetLease(); + + GraphicsDevice.ThrowIfDeviceLost(); + + ThrowIfIsNotInReadOnlyState(); + + ReadOnly? readOnlyWrapper = this.readOnlyWrapper; + + if (readOnlyWrapper is not null) { - GraphicsDevice.ThrowIfDeviceLost(); - - ThrowIfIsNotInReadOnlyState(); - - 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 texture) - { - return texture.readOnlyWrapper = new(texture); - } - - return InitializeWrapper(this); + 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 texture) + { + return texture.readOnlyWrapper = new(texture); + } + + return InitializeWrapper(this); } /// @@ -111,13 +110,12 @@ partial class ReadWriteTexture2D /// D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) { - using (GetReferenceTracker().GetLease()) - using (this.owner.GetReferenceTracker().GetLease()) - { - this.owner.ThrowIfDeviceMismatch(device); + using var _0 = GetReferenceTracker().GetLease(); + using var _1 = this.owner.GetReferenceTracker().GetLease(); - return this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle; - } + this.owner.ThrowIfDeviceMismatch(device); + + return this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle; } /// @@ -129,13 +127,12 @@ partial class ReadWriteTexture2D /// ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device) { - using (GetReferenceTracker().GetLease()) - using (this.owner.GetReferenceTracker().GetLease()) - { - this.owner.ThrowIfDeviceMismatch(device); + using var _0 = GetReferenceTracker().GetLease(); + using var _1 = this.owner.GetReferenceTracker().GetLease(); - return this.owner.D3D12Resource; - } + this.owner.ThrowIfDeviceMismatch(device); + + return this.owner.D3D12Resource; } /// diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.ReadOnly.cs index 7aec0e6f..96e35601 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.ReadOnly.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.ReadOnly.cs @@ -24,28 +24,27 @@ partial class ReadWriteTexture3D /// public IReadOnlyNormalizedTexture3D AsReadOnly() { - using (GraphicsDevice.GetReferenceTracker().GetLease()) - using (GetReferenceTracker().GetLease()) + using var _0 = GraphicsDevice.GetReferenceTracker().GetLease(); + using var _1 = GetReferenceTracker().GetLease(); + + GraphicsDevice.ThrowIfDeviceLost(); + + ThrowIfIsNotInReadOnlyState(); + + ReadOnly? readOnlyWrapper = this.readOnlyWrapper; + + if (readOnlyWrapper is not null) { - GraphicsDevice.ThrowIfDeviceLost(); - - ThrowIfIsNotInReadOnlyState(); - - ReadOnly? readOnlyWrapper = this.readOnlyWrapper; - - if (readOnlyWrapper is not null) - { - return readOnlyWrapper; - } - - [MethodImpl(MethodImplOptions.NoInlining)] - static ReadOnly InitializeWrapper(ReadWriteTexture3D texture) - { - return texture.readOnlyWrapper = new(texture); - } - - return InitializeWrapper(this); + return readOnlyWrapper; } + + [MethodImpl(MethodImplOptions.NoInlining)] + static ReadOnly InitializeWrapper(ReadWriteTexture3D texture) + { + return texture.readOnlyWrapper = new(texture); + } + + return InitializeWrapper(this); } /// @@ -111,13 +110,12 @@ partial class ReadWriteTexture3D /// D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) { - using (GetReferenceTracker().GetLease()) - using (this.owner.GetReferenceTracker().GetLease()) - { - this.owner.ThrowIfDeviceMismatch(device); + using var _0 = GetReferenceTracker().GetLease(); + using var _1 = this.owner.GetReferenceTracker().GetLease(); - return this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle; - } + this.owner.ThrowIfDeviceMismatch(device); + + return this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle; } /// @@ -129,13 +127,12 @@ partial class ReadWriteTexture3D /// ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device) { - using (GetReferenceTracker().GetLease()) - using (this.owner.GetReferenceTracker().GetLease()) - { - this.owner.ThrowIfDeviceMismatch(device); + using var _0 = GetReferenceTracker().GetLease(); + using var _1 = this.owner.GetReferenceTracker().GetLease(); - return this.owner.D3D12Resource; - } + this.owner.ThrowIfDeviceMismatch(device); + + return this.owner.D3D12Resource; } /// diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T}.ReadOnly.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T}.ReadOnly.cs index 401d91ef..db0e67d1 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T}.ReadOnly.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T}.ReadOnly.cs @@ -24,28 +24,27 @@ partial class ReadWriteTexture3D /// public IReadOnlyTexture3D AsReadOnly() { - using (GraphicsDevice.GetReferenceTracker().GetLease()) - using (GetReferenceTracker().GetLease()) + using var _0 = GraphicsDevice.GetReferenceTracker().GetLease(); + using var _1 = GetReferenceTracker().GetLease(); + + GraphicsDevice.ThrowIfDeviceLost(); + + ThrowIfIsNotInReadOnlyState(); + + ReadOnly? readOnlyWrapper = this.readOnlyWrapper; + + if (readOnlyWrapper is not null) { - GraphicsDevice.ThrowIfDeviceLost(); - - ThrowIfIsNotInReadOnlyState(); - - ReadOnly? readOnlyWrapper = this.readOnlyWrapper; - - if (readOnlyWrapper is not null) - { - return readOnlyWrapper; - } - - [MethodImpl(MethodImplOptions.NoInlining)] - static ReadOnly InitializeWrapper(ReadWriteTexture3D texture) - { - return texture.readOnlyWrapper = new(texture); - } - - return InitializeWrapper(this); + return readOnlyWrapper; } + + [MethodImpl(MethodImplOptions.NoInlining)] + static ReadOnly InitializeWrapper(ReadWriteTexture3D texture) + { + return texture.readOnlyWrapper = new(texture); + } + + return InitializeWrapper(this); } /// @@ -111,13 +110,12 @@ partial class ReadWriteTexture3D /// D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) { - using (GetReferenceTracker().GetLease()) - using (this.owner.GetReferenceTracker().GetLease()) - { - this.owner.ThrowIfDeviceMismatch(device); + using var _0 = GetReferenceTracker().GetLease(); + using var _1 = this.owner.GetReferenceTracker().GetLease(); - return this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle; - } + this.owner.ThrowIfDeviceMismatch(device); + + return this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle; } /// @@ -129,13 +127,12 @@ partial class ReadWriteTexture3D /// ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device) { - using (GetReferenceTracker().GetLease()) - using (this.owner.GetReferenceTracker().GetLease()) - { - this.owner.ThrowIfDeviceMismatch(device); + using var _0 = GetReferenceTracker().GetLease(); + using var _1 = this.owner.GetReferenceTracker().GetLease(); - return this.owner.D3D12Resource; - } + this.owner.ThrowIfDeviceMismatch(device); + + return this.owner.D3D12Resource; } ///