From 052e50a8d29c16583db523d72066e4ca967bee4d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 16 Jan 2021 17:23:54 +0100 Subject: [PATCH] Added AllocationMode option to create resources --- .../HlslBokehBlurProcessor{TPixel}.cs | 2 +- .../Extensions/ID3D12DeviceExtensions.cs | 15 ++- .../Resources/Abstract/Buffer{T}.cs | 5 +- .../Resources/Abstract/StructuredBuffer{T}.cs | 9 +- .../Resources/Abstract/Texture2D{T}.cs | 8 +- .../Resources/Abstract/Texture3D{T}.cs | 8 +- .../Resources/ConstantBuffer{T}.cs | 5 +- .../Resources/Enums/AllocationMode.cs | 20 ++++ .../Resources/ReadOnlyBuffer{T}.cs | 5 +- .../Resources/ReadOnlyTexture2D{T,TPixel}.cs | 5 +- .../Resources/ReadOnlyTexture2D{T}.cs | 5 +- .../Resources/ReadOnlyTexture3D{T,TPixel}.cs | 5 +- .../Resources/ReadOnlyTexture3D{T}.cs | 5 +- .../Resources/ReadWriteBuffer{T}.cs | 5 +- .../Resources/ReadWriteTexture2D{T,TPixel}.cs | 5 +- .../Resources/ReadWriteTexture2D{T}.cs | 5 +- .../Resources/ReadWriteTexture3D{T,TPixel}.cs | 5 +- .../Resources/ReadWriteTexture3D{T}.cs | 5 +- src/ComputeSharp.Shaders/ShaderRunner{T}.cs | 2 +- .../Extensions/GraphicsDeviceExtensions.cs | 99 ++++++++++--------- 20 files changed, 140 insertions(+), 83 deletions(-) create mode 100644 src/ComputeSharp.Graphics/Resources/Enums/AllocationMode.cs diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs index 9ae94951..864389ed 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs @@ -257,7 +257,7 @@ namespace ComputeSharp.BokehBlur.Processors Span span = MemoryMarshal.Cast(pixelSpan); using ReadWriteTexture2D texture = Gpu.Default.AllocateReadWriteTexture2D(span, source.Width, source.Height); - using ReadWriteTexture2D temporary = Gpu.Default.AllocateReadWriteTexture2D(source.Width, source.Height); + using ReadWriteTexture2D temporary = Gpu.Default.AllocateReadWriteTexture2D(source.Width, source.Height, AllocationMode.Clear); using ReadWriteTexture2D reals = Gpu.Default.AllocateReadWriteTexture2D(source.Width, source.Height); using ReadWriteTexture2D imaginaries = Gpu.Default.AllocateReadWriteTexture2D(source.Width, source.Height); using ReadOnlyBuffer kernel = Gpu.Default.AllocateReadOnlyBuffer(KernelSize); diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs index 0b621f4a..96570d5b 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs @@ -101,15 +101,18 @@ namespace ComputeSharp.Graphics.Extensions /// /// The instance in use. /// The resource type currently in use. + /// The allocation mode to use for the new resource. /// The size in bytes of the current buffer. /// Indicates whether or not the current device has a cache coherent UMA architecture. /// An reference for the current buffer. public static ComPtr CreateCommittedResource( this ref ID3D12Device d3D12Device, ResourceType resourceType, + AllocationMode allocationMode, ulong sizeInBytes, bool isCacheCoherentUMA) { + D3D12_HEAP_FLAGS d3D12HeapFlags = allocationMode == AllocationMode.Default ? D3D12_HEAP_FLAG_CREATE_NOT_ZEROED : D3D12_HEAP_FLAG_NONE; (D3D12_HEAP_TYPE d3D12HeapType, D3D12_RESOURCE_FLAGS d3D12ResourceFlags, D3D12_RESOURCE_STATES d3D12ResourceStates) = resourceType switch @@ -145,7 +148,7 @@ namespace ComputeSharp.Graphics.Extensions d3D12Device.CreateCommittedResource( &d3D12HeapProperties, - D3D12_HEAP_FLAG_NONE, + d3D12HeapFlags, &d3D12ResourceDescription, d3D12ResourceStates, null, @@ -160,6 +163,7 @@ namespace ComputeSharp.Graphics.Extensions /// /// The instance in use. /// The resource type currently in use. + /// The allocation mode to use for the new resource. /// The value to use. /// The width of the texture resource. /// The height of the texture resource. @@ -169,12 +173,14 @@ namespace ComputeSharp.Graphics.Extensions public static ComPtr CreateCommittedResource( this ref ID3D12Device d3D12Device, ResourceType resourceType, + AllocationMode allocationMode, DXGI_FORMAT dxgiFormat, uint width, uint height, bool isCacheCoherentUMA, out D3D12_RESOURCE_STATES d3D12ResourceStates) { + D3D12_HEAP_FLAGS d3D12HeapFlags = allocationMode == AllocationMode.Default ? D3D12_HEAP_FLAG_CREATE_NOT_ZEROED : D3D12_HEAP_FLAG_NONE; D3D12_RESOURCE_FLAGS d3D12ResourceFlags; (d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch @@ -207,7 +213,7 @@ namespace ComputeSharp.Graphics.Extensions d3D12Device.CreateCommittedResource( &d3D12HeapProperties, - D3D12_HEAP_FLAG_NONE, + d3D12HeapFlags, &d3D12ResourceDescription, d3D12ResourceStates, null, @@ -222,6 +228,7 @@ namespace ComputeSharp.Graphics.Extensions /// /// The instance in use. /// The resource type currently in use. + /// The allocation mode to use for the new resource. /// The value to use. /// The width of the texture resource. /// The height of the texture resource. @@ -232,6 +239,7 @@ namespace ComputeSharp.Graphics.Extensions public static ComPtr CreateCommittedResource( this ref ID3D12Device d3D12Device, ResourceType resourceType, + AllocationMode allocationMode, DXGI_FORMAT dxgiFormat, uint width, uint height, @@ -239,6 +247,7 @@ namespace ComputeSharp.Graphics.Extensions bool isCacheCoherentUMA, out D3D12_RESOURCE_STATES d3D12ResourceStates) { + D3D12_HEAP_FLAGS d3D12HeapFlags = allocationMode == AllocationMode.Default ? D3D12_HEAP_FLAG_CREATE_NOT_ZEROED : D3D12_HEAP_FLAG_NONE; D3D12_RESOURCE_FLAGS d3D12ResourceFlags; (d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch @@ -271,7 +280,7 @@ namespace ComputeSharp.Graphics.Extensions d3D12Device.CreateCommittedResource( &d3D12HeapProperties, - D3D12_HEAP_FLAG_NONE, + d3D12HeapFlags, &d3D12ResourceDescription, d3D12ResourceStates, null, diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/Buffer{T}.cs index 85085968..7cacdccd 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/Buffer{T}.cs @@ -50,7 +50,8 @@ namespace ComputeSharp.Resources /// The number of items to store in the current buffer. /// The size in bytes of each buffer item (including padding, if any). /// The resource type for the current buffer. - private protected Buffer(GraphicsDevice device, int length, uint elementSizeInBytes, ResourceType resourceType) + /// The allocation mode to use for the new resource. + private protected Buffer(GraphicsDevice device, int length, uint elementSizeInBytes, ResourceType resourceType, AllocationMode allocationMode) { device.ThrowIfDisposed(); @@ -68,7 +69,7 @@ namespace ComputeSharp.Resources GraphicsDevice = device; Length = length; - this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, (ulong)SizeInBytes, device.IsCacheCoherentUMA); + this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, allocationMode, (ulong)SizeInBytes, device.IsCacheCoherentUMA); device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/StructuredBuffer{T}.cs index ab650ac7..e47ea518 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -22,8 +22,9 @@ namespace ComputeSharp.Resources /// The associated with the current instance. /// The number of items to store in the current buffer. /// The buffer type for the current buffer. - private protected unsafe StructuredBuffer(GraphicsDevice device, int length, ResourceType resourceType) - : base(device, length, (uint)sizeof(T), resourceType) + /// The allocation mode to use for the new resource. + private protected unsafe StructuredBuffer(GraphicsDevice device, int length, ResourceType resourceType, AllocationMode allocationMode) + : base(device, length, (uint)sizeof(T), resourceType, allocationMode) { } @@ -57,7 +58,7 @@ namespace ComputeSharp.Resources byteOffset = (nint)offset * sizeof(T), byteSize = size * sizeof(T); - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, (ulong)byteSize, false); + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, AllocationMode.Default, (ulong)byteSize, false); using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY)) { @@ -109,7 +110,7 @@ namespace ComputeSharp.Resources byteOffset = (nint)offset * sizeof(T), byteSize = size * sizeof(T); - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, (ulong)byteSize, false); + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, AllocationMode.Default, (ulong)byteSize, false); using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) fixed (void* sourcePointer = &source) diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs index 8780a646..3b36a343 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs @@ -59,8 +59,9 @@ namespace ComputeSharp.Resources /// The height of the texture. /// The width of the texture. /// The resource type for the current texture. + /// The allocation mode to use for the new resource. /// The format support for the current texture type. - private protected Texture2D(GraphicsDevice device, int width, int height, ResourceType resourceType, D3D12_FORMAT_SUPPORT1 d3D12FormatSupport) + private protected Texture2D(GraphicsDevice device, int width, int height, ResourceType resourceType, AllocationMode allocationMode, D3D12_FORMAT_SUPPORT1 d3D12FormatSupport) { device.ThrowIfDisposed(); @@ -78,6 +79,7 @@ namespace ComputeSharp.Resources this.d3D12Resource = device.D3D12Device->CreateCommittedResource( resourceType, + allocationMode, DXGIFormatHelper.GetForType(), (uint)width, (uint)height, @@ -152,7 +154,7 @@ namespace ComputeSharp.Resources out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, AllocationMode.Default, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType)) { @@ -224,7 +226,7 @@ namespace ComputeSharp.Resources out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, AllocationMode.Default, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) fixed (void* sourcePointer = &source) diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs index fa5a08c7..35ae7c08 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs @@ -60,8 +60,9 @@ namespace ComputeSharp.Resources /// The width of the texture. /// The depth of the texture. /// The resource type for the current texture. + /// The allocation mode to use for the new resource. /// The format support for the current texture type. - private protected Texture3D(GraphicsDevice device, int width, int height, int depth, ResourceType resourceType, D3D12_FORMAT_SUPPORT1 d3D12FormatSupport) + private protected Texture3D(GraphicsDevice device, int width, int height, int depth, ResourceType resourceType, AllocationMode allocationMode, D3D12_FORMAT_SUPPORT1 d3D12FormatSupport) { device.ThrowIfDisposed(); @@ -81,6 +82,7 @@ namespace ComputeSharp.Resources this.d3D12Resource = device.D3D12Device->CreateCommittedResource( resourceType, + allocationMode, DXGIFormatHelper.GetForType(), (uint)width, (uint)height, @@ -166,7 +168,7 @@ namespace ComputeSharp.Resources out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, AllocationMode.Default, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType)) { @@ -245,7 +247,7 @@ namespace ComputeSharp.Resources out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, AllocationMode.Default, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) fixed (void* sourcePointer = &source) diff --git a/src/ComputeSharp.Graphics/Resources/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Resources/ConstantBuffer{T}.cs index 24ce2bfb..a4200c3e 100644 --- a/src/ComputeSharp.Graphics/Resources/ConstantBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/ConstantBuffer{T}.cs @@ -35,8 +35,9 @@ namespace ComputeSharp /// /// The associated with the current instance. /// The number of items to store in the current buffer. - internal ConstantBuffer(GraphicsDevice device, int length) - : base(device, length, (uint)GetPaddedSize(), ResourceType.Constant) + /// The allocation mode to use for the new resource. + internal ConstantBuffer(GraphicsDevice device, int length, AllocationMode allocationMode) + : base(device, length, (uint)GetPaddedSize(), ResourceType.Constant, allocationMode) { } diff --git a/src/ComputeSharp.Graphics/Resources/Enums/AllocationMode.cs b/src/ComputeSharp.Graphics/Resources/Enums/AllocationMode.cs new file mode 100644 index 00000000..4f2e508b --- /dev/null +++ b/src/ComputeSharp.Graphics/Resources/Enums/AllocationMode.cs @@ -0,0 +1,20 @@ +namespace ComputeSharp +{ + /// + /// An that indicates a mode to use when allocating resources. + /// + public enum AllocationMode + { + /// + /// The default allocation mode for graphics resources. Allocated buffers are not explicitly cleared when created. + /// They can still be cleared for security reasons in some situations, but this step will be skipped whenever possible. + /// As such, always make sure not to read directly to buffers created this way before writing data to them first. + /// + Default, + + /// + /// Clear allocated buffers when creating them. + /// + Clear + } +} diff --git a/src/ComputeSharp.Graphics/Resources/ReadOnlyBuffer{T}.cs b/src/ComputeSharp.Graphics/Resources/ReadOnlyBuffer{T}.cs index 785dcd56..2d902880 100644 --- a/src/ComputeSharp.Graphics/Resources/ReadOnlyBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/ReadOnlyBuffer{T}.cs @@ -20,8 +20,9 @@ namespace ComputeSharp /// /// The associated with the current instance. /// The number of items to store in the current buffer. - internal ReadOnlyBuffer(GraphicsDevice device, int length) - : base(device, length, ResourceType.ReadOnly) + /// The allocation mode to use for the new resource. + internal ReadOnlyBuffer(GraphicsDevice device, int length, AllocationMode allocationMode) + : base(device, length, ResourceType.ReadOnly, allocationMode) { } diff --git a/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture2D{T,TPixel}.cs b/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture2D{T,TPixel}.cs index e4709634..7ebb1b17 100644 --- a/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture2D{T,TPixel}.cs +++ b/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture2D{T,TPixel}.cs @@ -27,8 +27,9 @@ namespace ComputeSharp /// The associated with the current instance. /// The width of the texture. /// The height of the texture. - internal ReadOnlyTexture2D(GraphicsDevice device, int width, int height) - : base(device, width, height, ResourceType.ReadOnly, D3D12_FORMAT_SUPPORT1_TEXTURE2D) + /// The allocation mode to use for the new resource. + internal ReadOnlyTexture2D(GraphicsDevice device, int width, int height, AllocationMode allocationMode) + : base(device, width, height, ResourceType.ReadOnly, allocationMode, D3D12_FORMAT_SUPPORT1_TEXTURE2D) { } diff --git a/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture2D{T}.cs b/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture2D{T}.cs index 564d6453..4e86b8ca 100644 --- a/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture2D{T}.cs @@ -22,8 +22,9 @@ namespace ComputeSharp /// The associated with the current instance. /// The width of the texture. /// The height of the texture. - internal ReadOnlyTexture2D(GraphicsDevice device, int width, int height) - : base(device, width, height, ResourceType.ReadOnly, D3D12_FORMAT_SUPPORT1_TEXTURE2D) + /// The allocation mode to use for the new resource. + internal ReadOnlyTexture2D(GraphicsDevice device, int width, int height, AllocationMode allocationMode) + : base(device, width, height, ResourceType.ReadOnly, allocationMode, D3D12_FORMAT_SUPPORT1_TEXTURE2D) { } diff --git a/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture3D{T,TPixel}.cs b/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture3D{T,TPixel}.cs index 6b632835..76bd6395 100644 --- a/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture3D{T,TPixel}.cs +++ b/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture3D{T,TPixel}.cs @@ -28,8 +28,9 @@ namespace ComputeSharp /// The width of the texture. /// The height of the texture. /// The depth of the texture. - internal ReadOnlyTexture3D(GraphicsDevice device, int width, int height, int depth) - : base(device, width, height, depth, ResourceType.ReadOnly, D3D12_FORMAT_SUPPORT1_TEXTURE3D) + /// The allocation mode to use for the new resource. + internal ReadOnlyTexture3D(GraphicsDevice device, int width, int height, int depth, AllocationMode allocationMode) + : base(device, width, height, depth, ResourceType.ReadOnly, allocationMode, D3D12_FORMAT_SUPPORT1_TEXTURE3D) { } diff --git a/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture3D{T}.cs b/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture3D{T}.cs index 089a0f5d..29c12680 100644 --- a/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture3D{T}.cs @@ -23,8 +23,9 @@ namespace ComputeSharp /// The width of the texture. /// The height of the texture. /// The depth of the texture. - internal ReadOnlyTexture3D(GraphicsDevice device, int width, int height, int depth) - : base(device, width, height, depth, ResourceType.ReadOnly, D3D12_FORMAT_SUPPORT1_TEXTURE3D) + /// The allocation mode to use for the new resource. + internal ReadOnlyTexture3D(GraphicsDevice device, int width, int height, int depth, AllocationMode allocationMode) + : base(device, width, height, depth, ResourceType.ReadOnly, allocationMode, D3D12_FORMAT_SUPPORT1_TEXTURE3D) { } diff --git a/src/ComputeSharp.Graphics/Resources/ReadWriteBuffer{T}.cs b/src/ComputeSharp.Graphics/Resources/ReadWriteBuffer{T}.cs index 1ea2cfd4..4fd28bb1 100644 --- a/src/ComputeSharp.Graphics/Resources/ReadWriteBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/ReadWriteBuffer{T}.cs @@ -20,8 +20,9 @@ namespace ComputeSharp /// /// The associated with the current instance. /// The number of items to store in the current buffer. - internal ReadWriteBuffer(GraphicsDevice device, int length) - : base(device, length, ResourceType.ReadWrite) + /// The allocation mode to use for the new resource. + internal ReadWriteBuffer(GraphicsDevice device, int length, AllocationMode allocationMode) + : base(device, length, ResourceType.ReadWrite, allocationMode) { } diff --git a/src/ComputeSharp.Graphics/Resources/ReadWriteTexture2D{T,TPixel}.cs b/src/ComputeSharp.Graphics/Resources/ReadWriteTexture2D{T,TPixel}.cs index d120a371..61ff5f9e 100644 --- a/src/ComputeSharp.Graphics/Resources/ReadWriteTexture2D{T,TPixel}.cs +++ b/src/ComputeSharp.Graphics/Resources/ReadWriteTexture2D{T,TPixel}.cs @@ -27,8 +27,9 @@ namespace ComputeSharp /// The associated with the current instance. /// The width of the texture. /// The height of the texture. - internal ReadWriteTexture2D(GraphicsDevice device, int width, int height) - : base(device, width, height, ResourceType.ReadWrite, D3D12_FORMAT_SUPPORT1_TEXTURE2D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW) + /// The allocation mode to use for the new resource. + internal ReadWriteTexture2D(GraphicsDevice device, int width, int height, AllocationMode allocationMode) + : base(device, width, height, ResourceType.ReadWrite, allocationMode, D3D12_FORMAT_SUPPORT1_TEXTURE2D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW) { } diff --git a/src/ComputeSharp.Graphics/Resources/ReadWriteTexture2D{T}.cs b/src/ComputeSharp.Graphics/Resources/ReadWriteTexture2D{T}.cs index b8e83351..a3037378 100644 --- a/src/ComputeSharp.Graphics/Resources/ReadWriteTexture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/ReadWriteTexture2D{T}.cs @@ -22,8 +22,9 @@ namespace ComputeSharp /// The associated with the current instance. /// The width of the texture. /// The height of the texture. - internal ReadWriteTexture2D(GraphicsDevice device, int width, int height) - : base(device, width, height, ResourceType.ReadWrite, D3D12_FORMAT_SUPPORT1_TEXTURE2D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW) + /// The allocation mode to use for the new resource. + internal ReadWriteTexture2D(GraphicsDevice device, int width, int height, AllocationMode allocationMode) + : base(device, width, height, ResourceType.ReadWrite, allocationMode, D3D12_FORMAT_SUPPORT1_TEXTURE2D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW) { } diff --git a/src/ComputeSharp.Graphics/Resources/ReadWriteTexture3D{T,TPixel}.cs b/src/ComputeSharp.Graphics/Resources/ReadWriteTexture3D{T,TPixel}.cs index ce4026bd..157d98d7 100644 --- a/src/ComputeSharp.Graphics/Resources/ReadWriteTexture3D{T,TPixel}.cs +++ b/src/ComputeSharp.Graphics/Resources/ReadWriteTexture3D{T,TPixel}.cs @@ -28,8 +28,9 @@ namespace ComputeSharp /// The width of the texture. /// The height of the texture. /// The depth of the texture. - internal ReadWriteTexture3D(GraphicsDevice device, int width, int height, int depth) - : base(device, width, height, depth, ResourceType.ReadWrite, D3D12_FORMAT_SUPPORT1_TEXTURE3D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW) + /// The allocation mode to use for the new resource. + internal ReadWriteTexture3D(GraphicsDevice device, int width, int height, int depth, AllocationMode allocationMode) + : base(device, width, height, depth, ResourceType.ReadWrite, allocationMode, D3D12_FORMAT_SUPPORT1_TEXTURE3D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW) { } diff --git a/src/ComputeSharp.Graphics/Resources/ReadWriteTexture3D{T}.cs b/src/ComputeSharp.Graphics/Resources/ReadWriteTexture3D{T}.cs index 1a4b88e1..e13bcf0d 100644 --- a/src/ComputeSharp.Graphics/Resources/ReadWriteTexture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/ReadWriteTexture3D{T}.cs @@ -23,8 +23,9 @@ namespace ComputeSharp /// The width of the texture. /// The height of the texture. /// The depth of the texture. - internal ReadWriteTexture3D(GraphicsDevice device, int width, int height, int depth) - : base(device, width, height, depth, ResourceType.ReadWrite, D3D12_FORMAT_SUPPORT1_TEXTURE3D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW) + /// The allocation mode to use for the new resource. + internal ReadWriteTexture3D(GraphicsDevice device, int width, int height, int depth, AllocationMode allocationMode) + : base(device, width, height, depth, ResourceType.ReadWrite, allocationMode, D3D12_FORMAT_SUPPORT1_TEXTURE3D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW) { } diff --git a/src/ComputeSharp.Shaders/ShaderRunner{T}.cs b/src/ComputeSharp.Shaders/ShaderRunner{T}.cs index 04683a7f..57accca0 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner{T}.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner{T}.cs @@ -37,7 +37,7 @@ namespace ComputeSharp.Shaders { ConditionalWeakTable> map = variablesBuffers ??= new(); - return map.GetValue(device, static gpu => new(gpu, 256)); + return map.GetValue(device, static gpu => new(gpu, 256, AllocationMode.Default)); } } diff --git a/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs b/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs index 078e2ca4..fdb156ba 100644 --- a/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs +++ b/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs @@ -19,12 +19,13 @@ namespace ComputeSharp /// The type of items to store in the buffer. /// The instance to use to allocate the buffer. /// The length of the buffer to allocate. + /// The allocation mode to use for the new resource. /// A zeroed instance of size . [Pure] - public static ConstantBuffer AllocateConstantBuffer(this GraphicsDevice device, int length) + public static ConstantBuffer AllocateConstantBuffer(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { - return new(device, length); + return new(device, length, allocationMode); } /// @@ -52,7 +53,7 @@ namespace ComputeSharp public static ConstantBuffer AllocateConstantBuffer(this GraphicsDevice device, ReadOnlySpan span) where T : unmanaged { - ConstantBuffer buffer = new(device, span.Length); + ConstantBuffer buffer = new(device, span.Length, AllocationMode.Default); buffer.SetData(span); @@ -70,7 +71,7 @@ namespace ComputeSharp public static ConstantBuffer AllocateConstantBuffer(this GraphicsDevice device, Buffer buffer) where T : unmanaged { - ConstantBuffer constantBuffer = new(device, buffer.Length); + ConstantBuffer constantBuffer = new(device, buffer.Length, AllocationMode.Default); constantBuffer.SetData(buffer); @@ -83,12 +84,13 @@ namespace ComputeSharp /// The type of items to store in the buffer. /// The instance to use to allocate the buffer. /// The length of the buffer to allocate. + /// The allocation mode to use for the new resource. /// A zeroed instance of size . [Pure] - public static ReadOnlyBuffer AllocateReadOnlyBuffer(this GraphicsDevice device, int length) + public static ReadOnlyBuffer AllocateReadOnlyBuffer(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { - return new(device, length); + return new(device, length, allocationMode); } /// @@ -116,7 +118,7 @@ namespace ComputeSharp public static ReadOnlyBuffer AllocateReadOnlyBuffer(this GraphicsDevice device, ReadOnlySpan span) where T : unmanaged { - ReadOnlyBuffer buffer = new(device, span.Length); + ReadOnlyBuffer buffer = new(device, span.Length, AllocationMode.Default); buffer.SetData(span); @@ -134,7 +136,7 @@ namespace ComputeSharp public static ReadOnlyBuffer AllocateReadOnlyBuffer(this GraphicsDevice device, Buffer buffer) where T : unmanaged { - ReadOnlyBuffer readWriteBuffer = new(device, buffer.Length); + ReadOnlyBuffer readWriteBuffer = new(device, buffer.Length, AllocationMode.Default); readWriteBuffer.SetData(buffer); @@ -148,12 +150,13 @@ namespace ComputeSharp /// The instance to use to allocate the texture. /// The width of the texture. /// The height of the texture. + /// The allocation mode to use for the new resource. /// A zeroed instance of size [, ]. [Pure] - public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, int width, int height) + public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, int width, int height, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { - return new(device, width, height); + return new(device, width, height, allocationMode); } /// @@ -200,7 +203,7 @@ namespace ComputeSharp public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, T[,] array) where T : unmanaged { - ReadOnlyTexture2D texture = new(device, array.GetLength(1), array.GetLength(0)); + ReadOnlyTexture2D texture = new(device, array.GetLength(1), array.GetLength(0), AllocationMode.Default); texture.SetData(array); @@ -220,7 +223,7 @@ namespace ComputeSharp public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, ReadOnlySpan span, int width, int height) where T : unmanaged { - ReadOnlyTexture2D texture = new(device, width, height); + ReadOnlyTexture2D texture = new(device, width, height, AllocationMode.Default); texture.SetData(span); @@ -235,13 +238,14 @@ namespace ComputeSharp /// The instance to use to allocate the texture. /// The width of the texture. /// The height of the texture. + /// The allocation mode to use for the new resource. /// A zeroed instance of size [, ]. [Pure] - public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, int width, int height) + public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, int width, int height, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged, IUnorm where TPixel : unmanaged { - return new(device, width, height); + return new(device, width, height, allocationMode); } /// @@ -294,7 +298,7 @@ namespace ComputeSharp where T : unmanaged, IUnorm where TPixel : unmanaged { - ReadOnlyTexture2D texture = new(device, array.GetLength(1), array.GetLength(0)); + ReadOnlyTexture2D texture = new(device, array.GetLength(1), array.GetLength(0), AllocationMode.Default); texture.SetData(array); @@ -316,7 +320,7 @@ namespace ComputeSharp where T : unmanaged, IUnorm where TPixel : unmanaged { - ReadOnlyTexture2D texture = new(device, width, height); + ReadOnlyTexture2D texture = new(device, width, height, AllocationMode.Default); texture.SetData(span); @@ -331,12 +335,13 @@ namespace ComputeSharp /// The width of the texture. /// The height of the texture. /// The depth of the texture. + /// The allocation mode to use for the new resource. /// A zeroed instance of size [, , ]. [Pure] - public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, int width, int height, int depth) + public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, int width, int height, int depth, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { - return new(device, width, height, depth); + return new(device, width, height, depth, allocationMode); } /// @@ -389,7 +394,7 @@ namespace ComputeSharp public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, T[,,] array) where T : unmanaged { - ReadOnlyTexture3D texture = new(device, array.GetLength(2), array.GetLength(1), array.GetLength(0)); + ReadOnlyTexture3D texture = new(device, array.GetLength(2), array.GetLength(1), array.GetLength(0), AllocationMode.Default); texture.SetData(array); @@ -410,7 +415,7 @@ namespace ComputeSharp public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, ReadOnlySpan span, int width, int height, int depth) where T : unmanaged { - ReadOnlyTexture3D texture = new(device, width, height, depth); + ReadOnlyTexture3D texture = new(device, width, height, depth, AllocationMode.Default); texture.SetData(span); @@ -426,13 +431,14 @@ namespace ComputeSharp /// The width of the texture. /// The height of the texture. /// The depth of the texture. + /// The allocation mode to use for the new resource. /// A zeroed instance of size [, , ]. [Pure] - public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, int width, int height, int depth) + public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, int width, int height, int depth, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged, IUnorm where TPixel : unmanaged { - return new(device, width, height, depth); + return new(device, width, height, depth, allocationMode); } /// @@ -491,7 +497,7 @@ namespace ComputeSharp where T : unmanaged, IUnorm where TPixel : unmanaged { - ReadOnlyTexture3D texture = new(device, array.GetLength(2), array.GetLength(1), array.GetLength(0)); + ReadOnlyTexture3D texture = new(device, array.GetLength(2), array.GetLength(1), array.GetLength(0), AllocationMode.Default); texture.SetData(array); @@ -514,7 +520,7 @@ namespace ComputeSharp where T : unmanaged, IUnorm where TPixel : unmanaged { - ReadOnlyTexture3D texture = new(device, width, height, depth); + ReadOnlyTexture3D texture = new(device, width, height, depth, AllocationMode.Default); texture.SetData(span); @@ -527,12 +533,13 @@ namespace ComputeSharp /// The type of items to store in the buffer. /// The instance to use to allocate the buffer. /// The length of the buffer to allocate. + /// The allocation mode to use for the new resource. /// A zeroed instance of size . [Pure] - public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, int length) + public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { - return new(device, length); + return new(device, length, allocationMode); } /// @@ -560,7 +567,7 @@ namespace ComputeSharp public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, ReadOnlySpan span) where T : unmanaged { - ReadWriteBuffer buffer = new(device, span.Length); + ReadWriteBuffer buffer = new(device, span.Length, AllocationMode.Default); buffer.SetData(span); @@ -578,7 +585,7 @@ namespace ComputeSharp public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, Buffer buffer) where T : unmanaged { - ReadWriteBuffer readWriteBuffer = new(device, buffer.Length); + ReadWriteBuffer readWriteBuffer = new(device, buffer.Length, AllocationMode.Default); readWriteBuffer.SetData(buffer); @@ -592,12 +599,13 @@ namespace ComputeSharp /// The instance to use to allocate the texture. /// The width of the texture. /// The height of the texture. + /// The allocation mode to use for the new resource. /// A zeroed instance of size [, ]. [Pure] - public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, int width, int height) + public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, int width, int height, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { - return new(device, width, height); + return new(device, width, height, allocationMode); } /// @@ -644,7 +652,7 @@ namespace ComputeSharp public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, T[,] array) where T : unmanaged { - ReadWriteTexture2D texture = new(device, array.GetLength(1), array.GetLength(0)); + ReadWriteTexture2D texture = new(device, array.GetLength(1), array.GetLength(0), AllocationMode.Default); texture.SetData(array); @@ -664,7 +672,7 @@ namespace ComputeSharp public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, ReadOnlySpan span, int width, int height) where T : unmanaged { - ReadWriteTexture2D texture = new(device, width, height); + ReadWriteTexture2D texture = new(device, width, height, AllocationMode.Default); texture.SetData(span); @@ -679,13 +687,14 @@ namespace ComputeSharp /// The instance to use to allocate the texture. /// The width of the texture. /// The height of the texture. + /// The allocation mode to use for the new resource. /// A zeroed instance of size [, ]. [Pure] - public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, int width, int height) + public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, int width, int height, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged, IUnorm where TPixel : unmanaged { - return new(device, width, height); + return new(device, width, height, allocationMode); } /// @@ -738,7 +747,7 @@ namespace ComputeSharp where T : unmanaged, IUnorm where TPixel : unmanaged { - ReadWriteTexture2D texture = new(device, array.GetLength(1), array.GetLength(0)); + ReadWriteTexture2D texture = new(device, array.GetLength(1), array.GetLength(0), AllocationMode.Default); texture.SetData(array); @@ -760,7 +769,7 @@ namespace ComputeSharp where T : unmanaged, IUnorm where TPixel : unmanaged { - ReadWriteTexture2D texture = new(device, width, height); + ReadWriteTexture2D texture = new(device, width, height, AllocationMode.Default); texture.SetData(span); @@ -775,12 +784,13 @@ namespace ComputeSharp /// The width of the texture. /// The height of the texture. /// The depth of the texture. + /// The allocation mode to use for the new resource. /// A zeroed instance of size [, ]. [Pure] - public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, int width, int height, int depth) + public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, int width, int height, int depth, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged { - return new(device, width, height, depth); + return new(device, width, height, depth, allocationMode); } /// @@ -833,7 +843,7 @@ namespace ComputeSharp public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, T[,,] array) where T : unmanaged { - ReadWriteTexture3D texture = new(device, array.GetLength(2), array.GetLength(1), array.GetLength(0)); + ReadWriteTexture3D texture = new(device, array.GetLength(2), array.GetLength(1), array.GetLength(0), AllocationMode.Default); texture.SetData(array); @@ -854,7 +864,7 @@ namespace ComputeSharp public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, ReadOnlySpan span, int width, int height, int depth) where T : unmanaged { - ReadWriteTexture3D texture = new(device, width, height, depth); + ReadWriteTexture3D texture = new(device, width, height, depth, AllocationMode.Default); texture.SetData(span); @@ -870,13 +880,14 @@ namespace ComputeSharp /// The width of the texture. /// The height of the texture. /// The depth of the texture. + /// The allocation mode to use for the new resource. /// A zeroed instance of size [, ]. [Pure] - public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, int width, int height, int depth) + public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, int width, int height, int depth, AllocationMode allocationMode = AllocationMode.Default) where T : unmanaged, IUnorm where TPixel : unmanaged { - return new(device, width, height, depth); + return new(device, width, height, depth, allocationMode); } /// @@ -935,7 +946,7 @@ namespace ComputeSharp where T : unmanaged, IUnorm where TPixel : unmanaged { - ReadWriteTexture3D texture = new(device, array.GetLength(2), array.GetLength(1), array.GetLength(0)); + ReadWriteTexture3D texture = new(device, array.GetLength(2), array.GetLength(1), array.GetLength(0), AllocationMode.Default); texture.SetData(array); @@ -958,7 +969,7 @@ namespace ComputeSharp where T : unmanaged, IUnorm where TPixel : unmanaged { - ReadWriteTexture3D texture = new(device, width, height, depth); + ReadWriteTexture3D texture = new(device, width, height, depth, AllocationMode.Default); texture.SetData(span);