diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml
index e5b91118..5a9af369 100644
--- a/.github/workflows/dotnet.yml
+++ b/.github/workflows/dotnet.yml
@@ -3,6 +3,83 @@ name: .NET
on: [push, pull_request]
jobs:
+
+ # Build the ComputeSharp project directly. This is the project that
+ # would be built when running any of the local samples or unit tests.
+ build-project:
+ runs-on: windows-latest
+ steps:
+ - name: Git checkout
+ uses: actions/checkout@v2
+ - name: Setup .NET SDK
+ uses: actions/setup-dotnet@v1
+ with:
+ dotnet-version: 5.0.x
+ - name: Create local NuGet feed
+ run: mkdir src\ComputeSharp.Package\bin\Release
+ shell: cmd
+ - name: Build
+ run: dotnet build src\ComputeSharp\ComputeSharp.csproj -c Release
+ shell: cmd
+
+ # Run all the unit tests referencing the ComputeSharp project directly.
+ run-tests:
+ if: success()
+ needs: [build-project]
+ runs-on: windows-latest
+ steps:
+ - name: Git checkout
+ uses: actions/checkout@v2
+ - name: Setup .NET SDK
+ uses: actions/setup-dotnet@v1
+ with:
+ dotnet-version: 5.0.x
+ - name: Create local NuGet feed
+ run: mkdir src\ComputeSharp.Package\bin\Release
+ shell: cmd
+ - name: Run ComputeSharp.Tests
+ run: dotnet test tests\ComputeSharp.Tests\ComputeSharp.Tests.csproj -v n -l "console;verbosity=detailed"
+ shell: cmd
+ - name: Run ComputeSharp.Tests.Internals
+ run: dotnet test tests\ComputeSharp.Tests.Internals\ComputeSharp.Tests.Internals.csproj -v n -l "console;verbosity=detailed"
+ shell: cmd
+ - name: Run ComputeSharp.Tests.Internals.SourceGenerators
+ run: dotnet test tests\ComputeSharp.Tests.Internals.SourceGenerators\ComputeSharp.Tests.Internals.SourceGenerators.csproj -v n -l "console;verbosity=detailed"
+ shell: cmd
+
+ # Run all the local samples to ensure they build and run with no errors.
+ run-samples:
+ if: success()
+ needs: [run-tests]
+ runs-on: windows-latest
+ steps:
+ - name: Git checkout
+ uses: actions/checkout@v2
+ - name: Setup .NET SDK
+ uses: actions/setup-dotnet@v1
+ with:
+ dotnet-version: 5.0.x
+ - name: Create local NuGet feed
+ run: mkdir src\ComputeSharp.Package\bin\Release
+ shell: cmd
+ - name: Build ComputeSharp.Sample.Local
+ run: dotnet build samples\ComputeSharp.Sample.Local\ComputeSharp.Sample.Local.csproj -c Release
+ shell: cmd
+ - name: Run ComputeSharp.Sample.Local
+ run: dotnet samples\ComputeSharp.Sample.Local\bin\Release\net5.0\ComputeSharp.Sample.Local.dll
+ shell: cmd
+ - name: Build ComputeSharp.ImageProcessing.csproj
+ run: dotnet build samples\ComputeSharp.ImageProcessing\ComputeSharp.ImageProcessing.csproj -c Release
+ shell: cmd
+ - name: Run ComputeSharp.ImageProcessing.csproj
+ run: dotnet samples\ComputeSharp.ImageProcessing\bin\Release\net5.0\ComputeSharp.ImageProcessing.dll
+ shell: cmd
+ - name: Build ComputeSharp.Benchmark.csproj
+ run: dotnet build samples\ComputeSharp.Benchmark\ComputeSharp.Benchmark.csproj -c Release
+ shell: cmd
+
+ # Buils the .msbuildproj project to generate the NuGet package.
+ # This workflow also uploads the resulting package as an artifact.
build-package:
runs-on: windows-latest
steps:
@@ -22,4 +99,54 @@ jobs:
uses: actions/upload-artifact@v2
with:
name: ComputeSharp.nupkg
- path: src\ComputeSharp.Package\bin\Release\*.nupkg
\ No newline at end of file
+ path: src\ComputeSharp.Package\bin\Release\*.nupkg
+
+ # Download the NuGet package generated in the previous job and use it
+ # to build and run the sample project referencing it. This is used as
+ # a test to ensure the NuGet package works in a consuming project.
+ verify-package:
+ if: success()
+ needs: [build-package]
+ runs-on: windows-latest
+ steps:
+ - name: Git checkout
+ uses: actions/checkout@v2
+ - name: Setup .NET SDK
+ uses: actions/setup-dotnet@v1
+ with:
+ dotnet-version: 5.0.x
+ - name: Create local NuGet feed
+ run: mkdir src\ComputeSharp.Package\bin\Release
+ shell: cmd
+ - name: Download package artifact
+ uses: actions/download-artifact@v2
+ with:
+ name: ComputeSharp.nupkg
+ path: src\ComputeSharp.Package\bin\Release\ComputeSharp.nupkg
+ - name: Build ComputeSharp.Sample.NuGet
+ run: dotnet build samples\ComputeSharp.Sample.NuGet\ComputeSharp.Sample.NuGet.csproj -c Release
+ shell: cmd
+ - name: Run ComputeSharp.Sample.NuGet
+ run: dotnet samples\ComputeSharp.Sample.NuGet\bin\Release\net5.0\ComputeSharp.Sample.NuGet.dll
+ shell: cmd
+
+ # Run the extra tests to validate a number of build and publishing configurations.
+ # This is used to ensure the native dependencies can always be loaded correctly
+ # regardless of whether the app is shipping with R2R, self-contained, etc.
+ verify-libs:
+ if: success()
+ needs: [verify-package]
+ runs-on: windows-latest
+ steps:
+ - name: Git checkout
+ uses: actions/checkout@v2
+ - name: Setup .NET SDK
+ uses: actions/setup-dotnet@v1
+ with:
+ dotnet-version: 5.0.x
+ - name: Create local NuGet feed
+ run: mkdir src\ComputeSharp.Package\bin\Release
+ shell: cmd
+ - name: Run ComputeSharp.Tests.NativeLibrariesResolver
+ run: dotnet test tests\ComputeSharp.Tests.NativeLibrariesResolver\ComputeSharp.Tests.NativeLibrariesResolver.csproj -v n -l "console;verbosity=detailed"
+ shell: cmd
\ No newline at end of file
diff --git a/nuget.config b/nuget.config
index 5d5d8d13..c5173dab 100644
--- a/nuget.config
+++ b/nuget.config
@@ -3,5 +3,6 @@
+
diff --git a/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj b/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj
index 6e2d6959..44e91014 100644
--- a/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj
+++ b/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj
@@ -18,7 +18,7 @@
-
+
diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs
index dd4ecb24..25e9dfc9 100644
--- a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs
+++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs
@@ -26,7 +26,7 @@ namespace ComputeSharp.BokehBlur.Processors
/// Initializes a new instance of the class.
///
public HlslBokehBlurProcessor()
- : this(DefaultRadius, DefaultComponents)
+ : this(Gpu.Default, DefaultRadius, DefaultComponents)
{
}
@@ -36,11 +36,28 @@ namespace ComputeSharp.BokehBlur.Processors
/// The size of the area to sample.
/// The number of components to use to approximate the original 2D bokeh blur convolution kernel.
public HlslBokehBlurProcessor(int radius, int components)
+ : this(Gpu.Default, radius, components)
{
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The associated with the current instance.
+ /// The size of the area to sample.
+ /// The number of components to use to approximate the original 2D bokeh blur convolution kernel.
+ public HlslBokehBlurProcessor(GraphicsDevice device, int radius, int components)
+ {
+ GraphicsDevice = device;
Radius = radius;
Components = components;
}
+ ///
+ /// Gets the associated with the current instance.
+ ///
+ public GraphicsDevice GraphicsDevice { get; }
+
///
/// Gets the radius.
///
diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs
index f53f3759..eddd54af 100644
--- a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs
+++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs
@@ -20,6 +20,11 @@ namespace ComputeSharp.BokehBlur.Processors
/// This processor is based on the code from Mike Pound, see github.com/mikepound/convolve.
public sealed partial class Implementation : ImageProcessor
{
+ ///
+ /// The instance in use.
+ ///
+ private readonly GraphicsDevice GraphicsDevice;
+
///
/// The kernel radius.
///
@@ -65,6 +70,7 @@ namespace ComputeSharp.BokehBlur.Processors
public Implementation(HlslBokehBlurProcessor definition, Configuration configuration, Image source, Rectangle sourceRectangle)
: base(configuration, source, sourceRectangle)
{
+ GraphicsDevice = definition.GraphicsDevice;
Radius = definition.Radius;
KernelSize = Radius * 2 + 1;
ComponentsCount = definition.Components;
@@ -256,14 +262,14 @@ 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, 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);
+ using ReadWriteTexture2D texture = GraphicsDevice.AllocateReadWriteTexture2D(span, source.Width, source.Height);
+ using ReadWriteTexture2D temporary = GraphicsDevice.AllocateReadWriteTexture2D(source.Width, source.Height, AllocationMode.Clear);
+ using ReadWriteTexture2D reals = GraphicsDevice.AllocateReadWriteTexture2D(source.Width, source.Height);
+ using ReadWriteTexture2D imaginaries = GraphicsDevice.AllocateReadWriteTexture2D(source.Width, source.Height);
+ using ReadOnlyBuffer kernel = GraphicsDevice.AllocateReadOnlyBuffer(KernelSize);
// Preliminary gamma highlight pass
- Gpu.Default.For(source.Height, new(texture));
+ GraphicsDevice.For(source.Height, new(texture));
// Perform two 1D convolutions for each component in the current instance
for (int j = 0; j < Kernels.Length; j++)
@@ -272,19 +278,19 @@ namespace ComputeSharp.BokehBlur.Processors
kernel.CopyFrom(Kernels[j]);
- Gpu.Default.For(
+ GraphicsDevice.For(
source.Width,
source.Height,
new(texture, reals, imaginaries, kernel));
- Gpu.Default.For(
+ GraphicsDevice.For(
source.Width,
source.Height,
new(parameters.Z, parameters.W, reals, imaginaries, temporary, kernel));
}
// Apply the inverse gamma exposure pass
- Gpu.Default.For(source.Height, new(temporary, texture));
+ GraphicsDevice.For(source.Height, new(temporary, texture));
// Write the final pixel data
texture.CopyTo(span);
@@ -409,7 +415,7 @@ namespace ComputeSharp.BokehBlur.Processors
{
Float4 v = source[i, ThreadIds.X];
- v.XYZ = Hlsl.Clamp(v.XYZ, 0, float.MaxValue);
+ v = Hlsl.Clamp(v, 0, float.MaxValue);
v.XYZ = Hlsl.Pow(v.XYZ, 1 / 3f);
target[i, ThreadIds.X] = v;
diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs
index 962db9cf..159e1965 100644
--- a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs
+++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs
@@ -20,25 +20,48 @@ namespace ComputeSharp.BokehBlur.Processors
///
/// Initializes a new instance of the class.
///
- public HlslGaussianBlurProcessor() : this(DefaultSigma, 9) { }
+ public HlslGaussianBlurProcessor()
+ : this(Gpu.Default, DefaultSigma, 9)
+ {
+ }
///
/// Initializes a new instance of the class.
///
/// The radius value representing the size of the area to sample.
- public HlslGaussianBlurProcessor(int radius) : this(radius / 3F, radius) { }
+ public HlslGaussianBlurProcessor(int radius)
+ : this(Gpu.Default, radius / 3F, radius)
+ {
+ }
///
/// Initializes a new instance of the class.
///
+ /// The associated with the current instance.
+ /// The radius value representing the size of the area to sample.
+ public HlslGaussianBlurProcessor(GraphicsDevice device, int radius)
+ : this(device, radius / 3F, radius)
+ {
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The associated with the current instance.
/// The sigma value representing the weight of the blur.
/// The radius value representing the size of the area to sample (this should be at least twice the sigma value)
- public HlslGaussianBlurProcessor(float sigma, int radius)
+ public HlslGaussianBlurProcessor(GraphicsDevice device, float sigma, int radius)
{
+ GraphicsDevice = device;
Sigma = sigma;
Radius = radius;
}
+ ///
+ /// The instance in use.
+ ///
+ private readonly GraphicsDevice GraphicsDevice;
+
///
/// Gets the sigma value representing the weight of the blur.
///
diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs
index dcb932bd..e3b5a7c4 100644
--- a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs
+++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs
@@ -17,6 +17,11 @@ namespace ComputeSharp.BokehBlur.Processors
///
internal sealed partial class Implementation : ImageProcessor
{
+ ///
+ /// The instance in use.
+ ///
+ private readonly GraphicsDevice GraphicsDevice;
+
///
/// The 1D kernel to apply.
///
@@ -33,6 +38,8 @@ namespace ComputeSharp.BokehBlur.Processors
: base(configuration, source, sourceRectangle)
{
int kernelSize = definition.Radius * 2 + 1;
+
+ GraphicsDevice = definition.GraphicsDevice;
Kernel = CreateGaussianBlurKernel(kernelSize, definition.Sigma);
}
@@ -94,12 +101,12 @@ namespace ComputeSharp.BokehBlur.Processors
Span span = MemoryMarshal.Cast(pixelSpan);
- using ReadWriteTexture2D sourceTexture = Gpu.Default.AllocateReadWriteTexture2D(span, source.Width, source.Height);
- using ReadWriteTexture2D firstPassTexture = Gpu.Default.AllocateReadWriteTexture2D(source.Width, source.Height);
- using ReadOnlyBuffer kernelBuffer = Gpu.Default.AllocateReadOnlyBuffer(Kernel);
+ using ReadWriteTexture2D sourceTexture = GraphicsDevice.AllocateReadWriteTexture2D(span, source.Width, source.Height);
+ using ReadWriteTexture2D firstPassTexture = GraphicsDevice.AllocateReadWriteTexture2D(source.Width, source.Height);
+ using ReadOnlyBuffer kernelBuffer = GraphicsDevice.AllocateReadOnlyBuffer(Kernel);
- Gpu.Default.For(source.Width, source.Height, new(sourceTexture, firstPassTexture, kernelBuffer));
- Gpu.Default.For(source.Width, source.Height, new(firstPassTexture, sourceTexture, kernelBuffer));
+ GraphicsDevice.For(source.Width, source.Height, new(sourceTexture, firstPassTexture, kernelBuffer));
+ GraphicsDevice.For(source.Width, source.Height, new(firstPassTexture, sourceTexture, kernelBuffer));
sourceTexture.CopyTo(span);
}
diff --git a/samples/ComputeSharp.Sample.Shared/Program.cs b/samples/ComputeSharp.Sample.Shared/Program.cs
index 30807dac..ee01f2a1 100644
--- a/samples/ComputeSharp.Sample.Shared/Program.cs
+++ b/samples/ComputeSharp.Sample.Shared/Program.cs
@@ -18,24 +18,19 @@ namespace ComputeSharp.Sample
array[i] = i + 1;
}
- // Print the initial matrix
- Console.WriteLine("===================== BEFORE =====================");
- PrintMatrix(array, width, height);
- Console.WriteLine("==================================================");
- Console.WriteLine();
-
using ReadWriteBuffer gpuBuffer = Gpu.Default.AllocateReadWriteBuffer(array);
// Run the shader
Gpu.Default.For(100, new MainKernel(gpuBuffer));
+ // Print the initial matrix
+ PrintMatrix(array, width, height, "BEFORE");
+
// Get the data back
gpuBuffer.CopyTo(array);
// Print the updated matrix
- Console.WriteLine("===================== AFTER ======================");
- PrintMatrix(array, width, height);
- Console.WriteLine("==================================================");
+ PrintMatrix(array, width, height, "AFTER");
}
///
@@ -59,8 +54,14 @@ namespace ComputeSharp.Sample
/// The input array representing the matrix to print.
/// The width of the array to print.
/// The height of the array to print.
- private static void PrintMatrix(float[] array, int width, int height)
+ /// The name of the matrix to print.
+ private static void PrintMatrix(float[] array, int width, int height, string name)
{
+ int pad = 48 - name.Length;
+ string title = $"{new string('=', pad / 2)} {name} {new string('=', (pad + 1) / 2)}";
+
+ Console.WriteLine(title);
+
int length = width * height;
int numberWidth = array.Max().ToString(CultureInfo.InvariantCulture).Length;
ref float r = ref array[0];
@@ -73,6 +74,8 @@ namespace ComputeSharp.Sample
if (i < length - 1) Console.Write(", ");
if (i > 0 && (i + 1) % width == 0) Console.WriteLine();
}
+
+ Console.WriteLine(new string('=', 50));
}
}
}
diff --git a/src/ComputeSharp.Graphics/Extensions/HResultExtensions.cs b/src/ComputeSharp.Graphics/Extensions/HResultExtensions.cs
index f085dccc..d3a5b855 100644
--- a/src/ComputeSharp.Graphics/Extensions/HResultExtensions.cs
+++ b/src/ComputeSharp.Graphics/Extensions/HResultExtensions.cs
@@ -1,6 +1,7 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
+using ComputeSharp.Graphics.Helpers;
using Microsoft.Toolkit.Diagnostics;
using HRESULT = System.Int32;
@@ -20,10 +21,24 @@ namespace ComputeSharp.Core.Extensions
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Assert(this HRESULT result)
{
+#if DEBUG
+ bool hasErrorsOrWarnings = DeviceHelper.FlushAllID3D12InfoQueueMessagesAndCheckForErrorsOrWarnings();
+
if (result < 0)
{
ThrowHelper.ThrowWin32Exception(result);
}
+
+ if (hasErrorsOrWarnings)
+ {
+ ThrowHelper.ThrowWin32Exception("Warning or error detected by ID3D12InfoQueue");
+ }
+#else
+ if (result < 0)
+ {
+ ThrowHelper.ThrowWin32Exception(result);
+ }
+#endif
}
}
}
diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs
index b101e51a..f7f1bd7e 100644
--- a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs
+++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics.Contracts;
using ComputeSharp.Core.Extensions;
+using ComputeSharp.Graphics.Helpers;
using Microsoft.Toolkit.Diagnostics;
using TerraFX.Interop;
using static TerraFX.Interop.D3D12_COMMAND_QUEUE_FLAGS;
@@ -11,7 +12,7 @@ using static TerraFX.Interop.D3D12_DESCRIPTOR_HEAP_TYPE;
using static TerraFX.Interop.D3D12_FEATURE;
using static TerraFX.Interop.D3D12_FENCE_FLAGS;
using static TerraFX.Interop.D3D12_MEMORY_POOL;
-using static TerraFX.Interop.D3D12_HEAP_FLAGS;
+using static TerraFX.Interop.D3D12_MESSAGE_ID;
using static TerraFX.Interop.D3D12_HEAP_TYPE;
using static TerraFX.Interop.D3D12_RESOURCE_FLAGS;
using static TerraFX.Interop.D3D12_RESOURCE_STATES;
@@ -27,6 +28,34 @@ namespace ComputeSharp.Graphics.Extensions
///
internal static unsafe class ID3D12DeviceExtensions
{
+ ///
+ /// Creates a new for a given device.
+ ///
+ /// The target to use to create the info queue.
+ /// A pointer to the newly created instance.
+ /// Thrown when the creation of the info queue fails.
+ public static ComPtr CreateInfoQueue(this ref ID3D12Device d3D12Device)
+ {
+ ComPtr d3D12InfoQueue = default;
+
+ d3D12Device.QueryInterface(FX.__uuidof(), d3D12InfoQueue.GetVoidAddressOf()).Assert();
+
+ D3D12_MESSAGE_ID* d3D12MessageIds = stackalloc D3D12_MESSAGE_ID[3]
+ {
+ D3D12_MESSAGE_ID_CREATEDEVICE_DEBUG_LAYER_STARTUP_OPTIONS,
+ D3D12_MESSAGE_ID_MAP_INVALID_NULLRANGE,
+ D3D12_MESSAGE_ID_UNMAP_INVALID_NULLRANGE
+ };
+
+ D3D12_INFO_QUEUE_FILTER d3D12InfoQueueFilter = default;
+ d3D12InfoQueueFilter.DenyList.NumIDs = 3;
+ d3D12InfoQueueFilter.DenyList.pIDList = d3D12MessageIds;
+
+ d3D12InfoQueue.Get()->PushRetrievalFilter(&d3D12InfoQueueFilter).Assert();
+
+ return d3D12InfoQueue.Move();
+ }
+
///
/// Creates a new of the specified type, for a given device.
///
@@ -112,7 +141,6 @@ namespace ComputeSharp.Graphics.Extensions
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
@@ -148,7 +176,7 @@ namespace ComputeSharp.Graphics.Extensions
d3D12Device.CreateCommittedResource(
&d3D12HeapProperties,
- d3D12HeapFlags,
+ D3D12FeatureHelper.GetD3D12HeapFlags(allocationMode),
&d3D12ResourceDescription,
d3D12ResourceStates,
null,
@@ -180,7 +208,6 @@ 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
@@ -213,7 +240,7 @@ namespace ComputeSharp.Graphics.Extensions
d3D12Device.CreateCommittedResource(
&d3D12HeapProperties,
- d3D12HeapFlags,
+ D3D12FeatureHelper.GetD3D12HeapFlags(allocationMode),
&d3D12ResourceDescription,
d3D12ResourceStates,
null,
@@ -247,7 +274,6 @@ 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
@@ -280,7 +306,7 @@ namespace ComputeSharp.Graphics.Extensions
d3D12Device.CreateCommittedResource(
&d3D12HeapProperties,
- d3D12HeapFlags,
+ D3D12FeatureHelper.GetD3D12HeapFlags(allocationMode),
&d3D12ResourceDescription,
d3D12ResourceStates,
null,
diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12ResourceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12ResourceExtensions.cs
index c6afd4bd..3995f71e 100644
--- a/src/ComputeSharp.Graphics/Extensions/ID3D12ResourceExtensions.cs
+++ b/src/ComputeSharp.Graphics/Extensions/ID3D12ResourceExtensions.cs
@@ -1,4 +1,5 @@
using System;
+using System.Diagnostics;
using System.Runtime.CompilerServices;
using ComputeSharp.Core.Extensions;
using ComputeSharp.Graphics.Resources.Interop;
@@ -38,5 +39,22 @@ namespace ComputeSharp.Graphics.Extensions
{
d3D12Resource.Unmap(0, null);
}
+
+ ///
+ /// Assigns a name to a given .
+ ///
+ /// The target to map.
+ /// The wrapper object to get the name from.
+ /// Thrown when the mapping operation fails.
+ [Conditional("DEBUG")]
+ public static void SetName(this ref ID3D12Resource d3D12Resource, object wrapper)
+ {
+ string name = wrapper.ToString()!;
+
+ fixed (char* p = name)
+ {
+ d3D12Resource.SetName((ushort*)p).Assert();
+ }
+ }
}
}
diff --git a/src/ComputeSharp.Graphics/Helpers/D3D12FeatureHelper.cs b/src/ComputeSharp.Graphics/Helpers/D3D12FeatureHelper.cs
new file mode 100644
index 00000000..8c3e003b
--- /dev/null
+++ b/src/ComputeSharp.Graphics/Helpers/D3D12FeatureHelper.cs
@@ -0,0 +1,66 @@
+using System.Collections.Generic;
+using System.Diagnostics.Contracts;
+using System.Runtime.CompilerServices;
+using TerraFX.Interop;
+using static TerraFX.Interop.D3D12_FEATURE;
+using FX = TerraFX.Interop.Windows;
+
+namespace ComputeSharp.Graphics.Helpers
+{
+ ///
+ /// A helper exposing some runtime or OS dependent D3D12 features.
+ ///
+ internal static class D3D12FeatureHelper
+ {
+ ///
+ /// The currently supported flag (or fallback).
+ ///
+ private static readonly D3D12_HEAP_FLAGS D3D12_HEAP_FLAG_CREATE_NOT_ZEROED = GetD3D12_HEAP_FLAG_CREATE_NOT_ZEROED();
+
+ ///
+ /// Gets the value for a given setting.
+ ///
+ /// The allocation mode to use for creating new resources.
+ /// The matching that is currently supported
+ [Pure]
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static D3D12_HEAP_FLAGS GetD3D12HeapFlags(AllocationMode allocationMode)
+ {
+ if (allocationMode == AllocationMode.Default)
+ {
+ return D3D12_HEAP_FLAG_CREATE_NOT_ZEROED;
+ }
+
+ return D3D12_HEAP_FLAGS.D3D12_HEAP_FLAG_NONE;
+ }
+
+ ///
+ /// Initializes the field.
+ ///
+ /// The currently supported flag.
+ private static unsafe D3D12_HEAP_FLAGS GetD3D12_HEAP_FLAG_CREATE_NOT_ZEROED()
+ {
+ lock (DeviceHelper.DevicesCache)
+ {
+ foreach (KeyValuePair pair in DeviceHelper.DevicesCache)
+ {
+ D3D12_FEATURE_DATA_D3D12_OPTIONS7 d3D12Options7;
+
+ int result = pair.Value.D3D12Device->CheckFeatureSupport(
+ D3D12_FEATURE_D3D12_OPTIONS7,
+ &d3D12Options7,
+ (uint)sizeof(D3D12_FEATURE_DATA_D3D12_OPTIONS7));
+
+ if (result == FX.S_OK)
+ {
+ return D3D12_HEAP_FLAGS.D3D12_HEAP_FLAG_CREATE_NOT_ZEROED;
+ }
+
+ break;
+ }
+
+ return D3D12_HEAP_FLAGS.D3D12_HEAP_FLAG_NONE;
+ }
+ }
+ }
+}
diff --git a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Factory.cs b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Factory.cs
index d841433d..e2665b77 100644
--- a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Factory.cs
+++ b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Factory.cs
@@ -1,4 +1,7 @@
using System.Collections.Generic;
+#if DEBUG
+using ComputeSharp.Graphics.Extensions;
+#endif
using TerraFX.Interop;
namespace ComputeSharp.Graphics.Helpers
@@ -9,7 +12,14 @@ namespace ComputeSharp.Graphics.Helpers
///
/// The local cache of instances that are currently usable.
///
- private static readonly Dictionary DevicesCache = new();
+ internal static readonly Dictionary DevicesCache = new();
+
+#if DEBUG
+ ///
+ /// The local map of instances for the existing devices.
+ ///
+ private static readonly Dictionary> D3D12InfoQueueMap = new();
+#endif
///
/// Retrieves a instance for an object.
@@ -28,6 +38,10 @@ namespace ComputeSharp.Graphics.Helpers
device = new GraphicsDevice(d3D12Device, dxgiDescription1);
DevicesCache.Add(luid, device);
+
+#if DEBUG
+ D3D12InfoQueueMap.Add(luid, d3D12Device.Get()->CreateInfoQueue());
+#endif
}
return device;
@@ -43,6 +57,12 @@ namespace ComputeSharp.Graphics.Helpers
lock (DevicesCache)
{
DevicesCache.Remove(device.Luid);
+
+#if DEBUG
+ D3D12InfoQueueMap.Remove(device.Luid, out ComPtr queue);
+
+ queue.Dispose();
+#endif
}
}
}
diff --git a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs
new file mode 100644
index 00000000..ad37f005
--- /dev/null
+++ b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs
@@ -0,0 +1,82 @@
+#if DEBUG
+
+using System;
+using System.Diagnostics;
+using System.Runtime.InteropServices;
+using System.Text;
+using TerraFX.Interop;
+using static TerraFX.Interop.D3D12_MESSAGE_SEVERITY;
+
+namespace ComputeSharp.Graphics.Helpers
+{
+ ///
+ internal static partial class DeviceHelper
+ {
+ ///
+ /// Flushes all the pending debug messages for all existing instances to the console/debugger.
+ /// It also checks whether or not there are any error messages being logged that didn't result in an actual crash yet.
+ ///
+ /// Whether or not there are any logged errors or warnings.
+ public static unsafe bool FlushAllID3D12InfoQueueMessagesAndCheckForErrorsOrWarnings()
+ {
+ bool hasErrorsOrWarnings = false;
+
+ lock (DevicesCache)
+ {
+ int j = 0;
+ StringBuilder builder = new(1024);
+
+ foreach (var pair in D3D12InfoQueueMap)
+ {
+ var device = DevicesCache[pair.Key];
+ var queue = pair.Value;
+
+ ulong messages = queue.Get()->GetNumStoredMessagesAllowedByRetrievalFilter();
+
+ for (ulong i = 0; i < messages; i++)
+ {
+ nuint length;
+ queue.Get()->GetMessage(i, null, &length);
+
+ D3D12_MESSAGE* message = (D3D12_MESSAGE*)Marshal.AllocHGlobal((nint)length);
+
+ queue.Get()->GetMessage(i, message, &length);
+
+ builder.Clear();
+ builder.AppendLine($"[D3D12 message #{i} for \"{device}\" (HW: {device.IsHardwareAccelerated}, UMA: {device.IsCacheCoherentUMA})]");
+ builder.AppendLine($"[Category]: {Enum.GetName(message->Category)}");
+ builder.AppendLine($"[Severity]: {Enum.GetName(message->Severity)}");
+ builder.AppendLine($"[ID]: {Enum.GetName(message->ID)}");
+ builder.Append($"[Description]: \"{new string(message->pDescription)}\"");
+
+ Marshal.FreeHGlobal((IntPtr)message);
+
+ if (message->Severity is D3D12_MESSAGE_SEVERITY_ERROR or D3D12_MESSAGE_SEVERITY_CORRUPTION or D3D12_MESSAGE_SEVERITY_WARNING)
+ {
+ hasErrorsOrWarnings = true;
+ }
+
+ string text = builder.ToString();
+
+ if (Debugger.IsAttached)
+ {
+ Debug.WriteLine(text);
+ }
+ else
+ {
+ Console.WriteLine(text);
+ }
+ }
+
+ queue.Get()->ClearStoredMessages();
+
+ j++;
+ }
+ }
+
+ return hasErrorsOrWarnings;
+ }
+ }
+}
+
+#endif
diff --git a/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs b/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs
index 4a30f2b8..c3f56001 100644
--- a/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs
+++ b/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs
@@ -8,29 +8,6 @@ namespace ComputeSharp.Graphics.Helpers
///
internal static class MemoryHelper
{
- ///
- /// Copies the content of a source memory area to a target one.
- ///
- /// The source memory area to read from.
- /// The pointer for the destination memory area.
- /// The destination offset to start writing data to.
- /// The number of items to copy.
- /// The size in bytes of each item to copy.
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static unsafe void Copy(
- void* source,
- void* destination,
- uint offset,
- uint length,
- uint elementSizeInBytes)
- {
- Buffer.MemoryCopy(
- source,
- (byte*)destination + offset * elementSizeInBytes,
- ulong.MaxValue,
- elementSizeInBytes * length);
- }
-
///
/// Copies the content of a source memory area to a target one, accounting for padding.
/// The destination memory area has padding for each element, while the source does not.
@@ -40,19 +17,31 @@ namespace ComputeSharp.Graphics.Helpers
/// The destination offset to start writing data to.
/// The number of items to copy.
/// The padded size of each element.
+ /// Indicates whether or not the source is padded too.
public static unsafe void Copy(
void* source,
void* destination,
uint offset,
uint length,
- uint elementPitchInBytes)
+ uint elementPitchInBytes,
+ bool isPaddingInSourceToo)
where T : unmanaged
{
destination = (byte*)destination + offset * elementPitchInBytes;
- for (int i = 0; i < length; i++)
+ if (isPaddingInSourceToo)
{
- *(T*)&((byte*)destination)[i * elementPitchInBytes] = ((T*)source)[i];
+ for (int i = 0; i < length; i++)
+ {
+ *(T*)&((byte*)destination)[i * elementPitchInBytes] = *(T*)&((byte*)source)[i * elementPitchInBytes];
+ }
+ }
+ else
+ {
+ for (int i = 0; i < length; i++)
+ {
+ *(T*)&((byte*)destination)[i * elementPitchInBytes] = ((T*)source)[i];
+ }
}
}
diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/Buffer{T}.cs
index 46fbdc5a..fbb8ba9b 100644
--- a/src/ComputeSharp.Graphics/Resources/Abstract/Buffer{T}.cs
+++ b/src/ComputeSharp.Graphics/Resources/Abstract/Buffer{T}.cs
@@ -80,6 +80,8 @@ namespace ComputeSharp.Resources
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, D3D12CpuDescriptorHandle);
break;
}
+
+ this.d3D12Resource.Get()->SetName(this);
}
///
diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/StructuredBuffer{T}.cs
index 14395371..5c25645e 100644
--- a/src/ComputeSharp.Graphics/Resources/Abstract/StructuredBuffer{T}.cs
+++ b/src/ComputeSharp.Graphics/Resources/Abstract/StructuredBuffer{T}.cs
@@ -101,15 +101,29 @@ namespace ComputeSharp.Resources
Guard.IsInRange(destinationOffset, 0, destination.Length, nameof(destinationOffset));
Guard.IsLessThanOrEqualTo(destinationOffset + length, destination.Length, nameof(length));
- ulong
- byteDestinationOffset = (uint)destinationOffset * (uint)sizeof(T),
- byteOffset = (uint)offset * (uint)sizeof(T),
- byteLength = (uint)length * (uint)sizeof(T);
+ if (GraphicsDevice.IsCacheCoherentUMA)
+ {
+ using ID3D12ResourceMap resource = D3D12Resource->Map();
- using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
+ MemoryHelper.Copy(
+ resource.Pointer,
+ (uint)offset,
+ (uint)length,
+ (uint)sizeof(T),
+ destination.MappedData + destinationOffset);
+ }
+ else
+ {
+ ulong
+ byteDestinationOffset = (uint)destinationOffset * (uint)sizeof(T),
+ byteOffset = (uint)offset * (uint)sizeof(T),
+ byteLength = (uint)length * (uint)sizeof(T);
- copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(destination.D3D12Resource, byteDestinationOffset, D3D12Resource, byteOffset, byteLength);
- copyCommandList.ExecuteAndWaitForCompletion();
+ using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
+
+ copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(destination.D3D12Resource, byteDestinationOffset, D3D12Resource, byteOffset, byteLength);
+ copyCommandList.ExecuteAndWaitForCompletion();
+ }
}
///
@@ -183,15 +197,29 @@ namespace ComputeSharp.Resources
Guard.IsInRange(sourceOffset, 0, source.Length, nameof(sourceOffset));
Guard.IsLessThanOrEqualTo(sourceOffset + length, source.Length, nameof(length));
- ulong
- byteSourceOffset = (uint)sourceOffset * (uint)sizeof(T),
- byteOffset = (uint)offset * (uint)sizeof(T),
- byteLength = (uint)length * (uint)sizeof(T);
+ if (GraphicsDevice.IsCacheCoherentUMA)
+ {
+ using ID3D12ResourceMap resource = D3D12Resource->Map();
- using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
+ MemoryHelper.Copy(
+ source.MappedData,
+ (uint)sourceOffset,
+ (uint)length,
+ (uint)sizeof(T),
+ (T*)resource.Pointer + offset);
+ }
+ else
+ {
+ ulong
+ byteSourceOffset = (uint)sourceOffset * (uint)sizeof(T),
+ byteOffset = (uint)offset * (uint)sizeof(T),
+ byteLength = (uint)length * (uint)sizeof(T);
- copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, byteOffset, source.D3D12Resource, byteSourceOffset, byteLength);
- copyCommandList.ExecuteAndWaitForCompletion();
+ using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
+
+ copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, byteOffset, source.D3D12Resource, byteSourceOffset, byteLength);
+ copyCommandList.ExecuteAndWaitForCompletion();
+ }
}
///
@@ -199,12 +227,14 @@ namespace ComputeSharp.Resources
{
GraphicsDevice.ThrowIfDisposed();
- ThrowIfDeviceMismatch(source.GraphicsDevice);
ThrowIfDisposed();
+
+ source.ThrowIfDeviceMismatch(GraphicsDevice);
source.ThrowIfDisposed();
- if (!source.IsPaddingPresent &&
- source.GraphicsDevice == GraphicsDevice)
+ Guard.IsLessThanOrEqualTo(source.Length, Length, nameof(Length));
+
+ if (!source.IsPaddingPresent)
{
// Directly copy the input buffer
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs
index bed6e970..9510905c 100644
--- a/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs
+++ b/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs
@@ -111,6 +111,8 @@ namespace ComputeSharp.Resources
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE2D, D3D12CpuDescriptorHandle);
break;
}
+
+ this.d3D12Resource.Get()->SetName(this);
}
///
diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs
index 9e4e6d54..3b1c9253 100644
--- a/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs
+++ b/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs
@@ -115,6 +115,8 @@ namespace ComputeSharp.Resources
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE3D, D3D12CpuDescriptorHandle);
break;
}
+
+ this.d3D12Resource.Get()->SetName(this);
}
///
diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/TransferBuffer{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/TransferBuffer{T}.cs
index 431c5806..9c52d2a3 100644
--- a/src/ComputeSharp.Graphics/Resources/Abstract/TransferBuffer{T}.cs
+++ b/src/ComputeSharp.Graphics/Resources/Abstract/TransferBuffer{T}.cs
@@ -48,6 +48,8 @@ namespace ComputeSharp.Resources
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, allocationMode, sizeInBytes, device.IsCacheCoherentUMA);
this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer;
+
+ this.d3D12Resource.Get()->SetName(this);
}
///
@@ -65,6 +67,11 @@ namespace ComputeSharp.Resources
///
internal ID3D12Resource* D3D12Resource => this.d3D12Resource;
+ ///
+ /// Gets the pointer to the start of the mapped buffer data.
+ ///
+ internal T* MappedData => this.mappedData;
+
///
public Memory Memory
{
diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs
index 57390ada..003a04c2 100644
--- a/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs
+++ b/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs
@@ -65,6 +65,8 @@ namespace ComputeSharp.Resources
this.d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(resourceType, allocationMode, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA);
this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer;
+
+ this.d3D12Resource.Get()->SetName(this);
}
///
diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture3D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture3D{T}.cs
index 16acda8a..3e92d0e5 100644
--- a/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture3D{T}.cs
+++ b/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture3D{T}.cs
@@ -68,6 +68,8 @@ namespace ComputeSharp.Resources
this.d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(resourceType, allocationMode, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA);
this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer;
+
+ this.d3D12Resource.Get()->SetName(this);
}
///
diff --git a/src/ComputeSharp.Graphics/Resources/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Resources/ConstantBuffer{T}.cs
index 5274b1f3..96d673af 100644
--- a/src/ComputeSharp.Graphics/Resources/ConstantBuffer{T}.cs
+++ b/src/ComputeSharp.Graphics/Resources/ConstantBuffer{T}.cs
@@ -3,7 +3,6 @@ using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using ComputeSharp.Core.Helpers;
using ComputeSharp.Exceptions;
-using ComputeSharp.Graphics.Commands;
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.Graphics.Resources.Enums;
@@ -11,7 +10,6 @@ using ComputeSharp.Graphics.Resources.Interop;
using ComputeSharp.Resources;
using ComputeSharp.Resources.Debug;
using Microsoft.Toolkit.Diagnostics;
-using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE;
using FX = TerraFX.Interop.Windows;
namespace ComputeSharp
@@ -114,7 +112,8 @@ namespace ComputeSharp
resource.Pointer,
(uint)offset,
(uint)size,
- (uint)GetPaddedSize());
+ (uint)GetPaddedSize(),
+ isPaddingInSourceToo: false);
}
else
{
@@ -133,18 +132,37 @@ namespace ComputeSharp
{
GraphicsDevice.ThrowIfDisposed();
- ThrowIfDeviceMismatch(source.GraphicsDevice);
ThrowIfDisposed();
+
+ source.ThrowIfDeviceMismatch(GraphicsDevice);
source.ThrowIfDisposed();
- if (source is ConstantBuffer &&
- source.GraphicsDevice == GraphicsDevice)
- {
- // Directly copy the input buffer, if possible
- using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
+ Guard.IsLessThanOrEqualTo(source.Length, Length, nameof(Length));
- copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, 0, source.D3D12Resource, 0,(ulong)SizeInBytes);
- copyCommandList.ExecuteAndWaitForCompletion();
+ if (source is ConstantBuffer buffer)
+ {
+ using ID3D12ResourceMap sourceMap = buffer.D3D12Resource->Map();
+ using ID3D12ResourceMap destinationMap = D3D12Resource->Map();
+
+ if (IsPaddingPresent)
+ {
+ MemoryHelper.Copy(
+ sourceMap.Pointer,
+ destinationMap.Pointer,
+ 0,
+ (uint)source.Length,
+ (uint)GetPaddedSize(),
+ isPaddingInSourceToo: true);
+ }
+ else
+ {
+ MemoryHelper.Copy(
+ sourceMap.Pointer,
+ 0,
+ (uint)source.Length,
+ (uint)sizeof(T),
+ destinationMap.Pointer);
+ }
}
else CopyFromWithCpuBuffer(source);
}
diff --git a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj
index 9c25bf58..b044bc0e 100644
--- a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj
+++ b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj
@@ -48,7 +48,7 @@
-
+
diff --git a/tests/ComputeSharp.Tests/Attributes/AllDevicesAttribute.cs b/tests/ComputeSharp.Tests/Attributes/AllDevicesAttribute.cs
new file mode 100644
index 00000000..54550319
--- /dev/null
+++ b/tests/ComputeSharp.Tests/Attributes/AllDevicesAttribute.cs
@@ -0,0 +1,12 @@
+using System;
+
+namespace ComputeSharp.Tests.Attributes
+{
+ ///
+ /// An attribute to use with targeting all supported devices.
+ ///
+ [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
+ public sealed class AllDevicesAttribute : Attribute
+ {
+ }
+}
diff --git a/tests/ComputeSharp.Tests/Attributes/CombinatorialTestMethodAttribute.cs b/tests/ComputeSharp.Tests/Attributes/CombinatorialTestMethodAttribute.cs
new file mode 100644
index 00000000..ff68a4aa
--- /dev/null
+++ b/tests/ComputeSharp.Tests/Attributes/CombinatorialTestMethodAttribute.cs
@@ -0,0 +1,118 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using ComputeSharp.Tests.Extensions;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace ComputeSharp.Tests.Attributes
+{
+ ///
+ /// A custom implementation that also acts as a combinatorial
+ /// instance. It has dedicated paths for target devices and resource types, and arbitrary inputs for the test methods.
+ ///
+ ///
+ /// Use this attribute in combination with , ,
+ /// or , not other existing MSTest attributes.
+ ///
+ [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
+ public sealed class CombinatorialTestMethodAttribute : TestMethodAttribute, ITestDataSource
+ {
+ ///
+ IEnumerable