diff --git a/.run/Run SDF Tool [8 pic].run.xml b/.run/Run SDF Tool [8 pic CUDA].run.xml
similarity index 70%
rename from .run/Run SDF Tool [8 pic].run.xml
rename to .run/Run SDF Tool [8 pic CUDA].run.xml
index 3837197..64f278d 100644
--- a/.run/Run SDF Tool [8 pic].run.xml
+++ b/.run/Run SDF Tool [8 pic CUDA].run.xml
@@ -1,7 +1,7 @@
-
+
-
+
@@ -15,8 +15,6 @@
-
-
\ No newline at end of file
diff --git a/.run/Run SDF Tool [8 pic OpenCL].run.xml b/.run/Run SDF Tool [8 pic OpenCL].run.xml
new file mode 100644
index 0000000..f09a330
--- /dev/null
+++ b/.run/Run SDF Tool [8 pic OpenCL].run.xml
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.run/Run SDF Tool [Debug, 8 pic].run.xml b/.run/Run SDF Tool [Debug, 8 pic].run.xml
index a5bda4c..58bcbc5 100644
--- a/.run/Run SDF Tool [Debug, 8 pic].run.xml
+++ b/.run/Run SDF Tool [Debug, 8 pic].run.xml
@@ -15,8 +15,6 @@
-
-
\ No newline at end of file
diff --git a/Readme.md b/Readme.md
index 8a82b96..eca3545 100644
--- a/Readme.md
+++ b/Readme.md
@@ -8,7 +8,8 @@
- Automatic CUDA/OpenCL GPU Acceleration for most of the process
- *nix style command line interface
-## How to use:
+## How to use
+
From command line: `SDFMapTool -D -i "" -o ...`
Available options:
@@ -31,8 +32,10 @@ Available options:
--sigma Blur sigma value (weighting).
+ --device Device to use for computation. (CPU, OpenCL, CUDA)
+
--help Display this help screen.
--version Display version information.
-```
\ No newline at end of file
+```
diff --git a/SDFMapCreator/Program.cs b/SDFMapCreator/Program.cs
index 4a112ae..52e9051 100644
--- a/SDFMapCreator/Program.cs
+++ b/SDFMapCreator/Program.cs
@@ -5,6 +5,12 @@ using CommandLine;
namespace SDFMapCreator;
+public enum DeviceType {
+ CPU,
+ OpenCL,
+ CUDA
+}
+
public class Options {
[Option('d', "debug", Required = false, HelpText = "Enable debug mode.")]
public bool Debug { get; set; } = false;
@@ -22,6 +28,8 @@ public class Options {
public float BlurStep { get; set; } = 0.5f;
[Option("sigma", Required = false, HelpText = "Blur sigma value (weighting).")]
public float BlurSigma { get; set; } = 1f;
+ [Option("device", Required = false, HelpText = "Device to use for computation. (CPU, OpenCL, CUDA)")]
+ public DeviceType? Device { get; set; } = null;
}
public static class Program {
@@ -40,7 +48,7 @@ public static class Program {
static List TransitionMasks = new();
static List SDFs = new();
static List Gradients = new();
- static readonly SdfKernels kernels = new();
+ static SdfKernels kernels;
static uint width;
static uint height;
@@ -51,9 +59,11 @@ public static class Program {
parser.ParseArguments(args)
.WithParsed(o => options = o)
- .WithNotParsed(o => {
+ .WithNotParsed(_ => {
Environment.Exit(1);
});
+ if(options.Device != null) kernels = new (options.Device.Value);
+ else kernels = new();
string debugPath = $"{Environment.CurrentDirectory}{PSep}Debug";
if (options.Debug) {
diff --git a/SDFMapCreator/SdfKernels.cs b/SDFMapCreator/SdfKernels.cs
index ce1a6cf..57e9f20 100644
--- a/SDFMapCreator/SdfKernels.cs
+++ b/SDFMapCreator/SdfKernels.cs
@@ -1,3 +1,4 @@
+using System.Diagnostics;
using System.Numerics;
using ILGPU;
using ILGPU.Runtime;
@@ -9,12 +10,12 @@ namespace SDFMapCreator;
public partial class SdfKernels {
Context gpuContext;
-
+ Accelerator accelerator;
public SdfKernels() {
// Initialize the GPU context
gpuContext = Context.Create(builder => builder
- .OpenCL()
.Cuda()
+ .OpenCL()
.CPU()
.Math(MathMode.Fast32BitOnly)
.EnableAlgorithms()
@@ -22,35 +23,52 @@ public partial class SdfKernels {
Console.WriteLine("Reading available accelerators (CUDA only)...");
foreach (var device in gpuContext.GetCudaDevices()) {
- using var accelerator = device.CreateAccelerator(gpuContext);
+ accelerator = device.CreateAccelerator(gpuContext);
Console.WriteLine($"Found accelerator: {accelerator.Name} ({accelerator.AcceleratorType})");
}
Console.WriteLine("Reading available accelerators (OpenCL only)...");
foreach (var device in gpuContext.GetCLDevices()) {
- using var accelerator = device.CreateAccelerator(gpuContext);
+ accelerator = device.CreateAccelerator(gpuContext);
Console.WriteLine($"Found accelerator: {accelerator.Name} ({accelerator.AcceleratorType})");
}
Console.WriteLine("Reading available accelerators (CPU only)...");
foreach (var device in gpuContext.GetCPUDevices()) {
- using var accelerator = device.CreateAccelerator(gpuContext);
+ accelerator = device.CreateAccelerator(gpuContext);
Console.WriteLine($"Found accelerator: {accelerator.Name} ({accelerator.AcceleratorType})");
}
+
+ accelerator = gpuContext.GetPreferredDevice(false).CreateAccelerator(gpuContext);
+ }
+
+ public SdfKernels(DeviceType device) {
+ Console.WriteLine($"Looking up for {device.ToString()} accelerators...");
+ var builder = Context.Create();
+ switch (device) {
+ case DeviceType.CPU: builder.CPU(); break;
+ case DeviceType.CUDA: builder.Cuda(); break;
+ case DeviceType.OpenCL: builder.OpenCL(); break;
+ }
+
+ gpuContext = builder
+ .Math(MathMode.Fast32BitOnly)
+ .EnableAlgorithms()
+ .ToContext();
+
+ accelerator = gpuContext.GetPreferredDevice(false).CreateAccelerator(gpuContext);
}
public void SelfMask(Vector3[,] input, out Vector3[,] mask) {
- var dev = gpuContext.GetPreferredDevice(false);
var width = input.GetLength(0);
var height = input.GetLength(1);
mask = new Vector3[width, height];
- using var accelerator = dev.CreateAccelerator(gpuContext);
using var buffer = accelerator.Allocate2DDenseX(new(width, height));
buffer.CopyFromCPU(input);
using var maskBuffer = accelerator.Allocate2DDenseX(new(width, height));
-
+
var selfMaskKernel = accelerator.LoadAutoGroupedStreamKernel,
ArrayView2D>(SelfMaskKernel);
@@ -62,11 +80,9 @@ public partial class SdfKernels {
}
public void ABMask(Vector3[,] A, Vector3[,] B, out Vector3[,] mask) {
- var dev = gpuContext.GetPreferredDevice(false);
var width = A.GetLength(0);
var height = A.GetLength(1);
mask = new Vector3[width, height];
- using var accelerator = dev.CreateAccelerator(gpuContext);
using var bufferA = accelerator.Allocate2DDenseX(new(width, height));
bufferA.CopyFromCPU(A);
@@ -89,11 +105,9 @@ public partial class SdfKernels {
}
public void EdgeDetect(Vector3[,] mask, out Vector3[,] edge) {
- var dev = gpuContext.GetPreferredDevice(false);
var width = mask.GetLength(0);
var height = mask.GetLength(1);
edge = new Vector3[width, height];
- using var accelerator = dev.CreateAccelerator(gpuContext);
using var buffer = accelerator.Allocate2DDenseX(new(width, height));
buffer.CopyFromCPU(mask);
@@ -109,9 +123,6 @@ public partial class SdfKernels {
}
public void Sdf(Vector2[] edges, int width, int height, out Vector3[,] sdf) {
- var dev = gpuContext.GetPreferredDevice(false);
- using var accelerator = dev.CreateAccelerator(gpuContext);
-
using var buffer = accelerator.Allocate2DDenseX(new(width, height));
using var edgeBuffer = accelerator.Allocate1D(edges.Length);
@@ -130,11 +141,9 @@ public partial class SdfKernels {
}
public void Gradient(Vector3[,] mask, Vector3[,] sdfa, Vector3[,] sdfb, out Vector3[,] gradient) {
- var dev = gpuContext.GetPreferredDevice(false);
var width = mask.GetLength(0);
var height = mask.GetLength(1);
gradient = new Vector3[width, height];
- using var accelerator = dev.CreateAccelerator(gpuContext);
using var bufferMask = accelerator.Allocate2DDenseX(new(width, height));
bufferMask.CopyFromCPU(mask);
@@ -162,11 +171,9 @@ public partial class SdfKernels {
public void DirectionalBlur(Vector3[,] image, Vector3[,] mask, out Vector3[,] output, float radius = 3f,
float step = .5f, float sigma = 1f) {
- var dev = gpuContext.GetPreferredDevice(false);
var width = image.GetLength(0);
var height = image.GetLength(1);
output = new Vector3[width, height];
- using var accelerator = dev.CreateAccelerator(gpuContext);
using var imageBuffer = accelerator.Allocate2DDenseX(new(width, height));
imageBuffer.CopyFromCPU(image);
@@ -195,4 +202,9 @@ public partial class SdfKernels {
a.PrintInformation(infoString);
return infoString.ToString();
}
+
+ ~SdfKernels() {
+ accelerator?.Dispose();
+ gpuContext?.Dispose();
+ }
}
\ No newline at end of file
diff --git a/SDFMapCreator/copyImages.ps1 b/SDFMapCreator/copyImages.ps1
index 36b3d58..377f508 100644
--- a/SDFMapCreator/copyImages.ps1
+++ b/SDFMapCreator/copyImages.ps1
@@ -1,11 +1,15 @@
+$imagesPath = "./images/*"
+
if (Test-Path "./bin/Debug/net8.0/")
{
- echo "Copying images to Debug folder"
- copy ./images/* "./bin/Debug/net8.0/"
+ Write-Output "Copying images to Debug folder"
+ Copy-Item -Path $imagesPath -Destination "./bin/Debug/net8.0/images/"
}
if(Test-Path "./bin/Release/net8.0/")
{
- echo "Copying images to Release folder"
- copy ./images/* "./bin/Release/net8.0/"
-}
\ No newline at end of file
+ Write-Output "Copying images to Release folder"
+ Copy-Item -Path $imagesPath -Destination "./bin/Release/net8.0/images/"
+}
+
+return 0
\ No newline at end of file