Added SDF kernel
This commit is contained in:
@@ -133,7 +133,9 @@ public class Program {
|
|||||||
kernels.EdgeDetect(maskData.Mask, out var temp);
|
kernels.EdgeDetect(maskData.Mask, out var temp);
|
||||||
maskData.Mask = temp;
|
maskData.Mask = temp;
|
||||||
Parallel.For(0, width * height, parallelOptions, (i) => {
|
Parallel.For(0, width * height, parallelOptions, (i) => {
|
||||||
if(maskData.Mask[i % width, i / width].Y != 0) lock (maskData.Edges) maskData.Edges.Add(new(i % width, i / width));
|
if (maskData.Mask[i % width, i / width].Y == 0) return;
|
||||||
|
// ReSharper disable once PossibleLossOfFraction
|
||||||
|
lock (maskData.Edges) maskData.Edges.Add(new(i % width, i / width));
|
||||||
});
|
});
|
||||||
|
|
||||||
sw.Stop();
|
sw.Stop();
|
||||||
@@ -179,12 +181,11 @@ public class Program {
|
|||||||
static SDFData SDF(MaskData mask) {
|
static SDFData SDF(MaskData mask) {
|
||||||
var width = (uint)mask.Mask.GetLength(0);
|
var width = (uint)mask.Mask.GetLength(0);
|
||||||
var height = (uint)mask.Mask.GetLength(1);
|
var height = (uint)mask.Mask.GetLength(1);
|
||||||
var temp = new Vector3[width, height];
|
|
||||||
float AbsMax = MIN;
|
float AbsMax = MIN;
|
||||||
int iterCount = 0;
|
int iterCount = 0;
|
||||||
var sw = new Stopwatch();
|
var sw = new Stopwatch();
|
||||||
sw.Start();
|
sw.Start();
|
||||||
Parallel.For(0, width * height, parallelOptions, (i) =>
|
/*Parallel.For(0, width * height, parallelOptions, (i) =>
|
||||||
{
|
{
|
||||||
//convert 1D index to 2D index
|
//convert 1D index to 2D index
|
||||||
var x = (int)(i % width);
|
var x = (int)(i % width);
|
||||||
@@ -210,8 +211,9 @@ public class Program {
|
|||||||
ConsoleUpdateLine(
|
ConsoleUpdateLine(
|
||||||
$"Progress: {iterCount / (float)(width * height):P}% | {iterCount / (sw.Elapsed.TotalSeconds):N0} pixels/s");
|
$"Progress: {iterCount / (float)(width * height):P}% | {iterCount / (sw.Elapsed.TotalSeconds):N0} pixels/s");
|
||||||
}
|
}
|
||||||
});
|
});*/
|
||||||
|
kernels.Sdf(mask.Edges.ToArray(), out var temp);
|
||||||
|
|
||||||
Console.WriteLine(
|
Console.WriteLine(
|
||||||
$"\nSDF Generation Time: {sw.Elapsed.TotalSeconds:N4}s ({iterCount / sw.Elapsed.TotalSeconds:N0} pixels/s)");
|
$"\nSDF Generation Time: {sw.Elapsed.TotalSeconds:N4}s ({iterCount / sw.Elapsed.TotalSeconds:N0} pixels/s)");
|
||||||
sw.Restart();
|
sw.Restart();
|
||||||
|
|||||||
@@ -49,4 +49,24 @@ public partial class SdfKernels {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void SdfKernel(Index2D index,
|
||||||
|
ArrayView2D<Vector3, Stride2D.DenseX> sdf,
|
||||||
|
ArrayView1D<Vector2, Stride1D.Dense> edges,
|
||||||
|
int width, int height
|
||||||
|
) {
|
||||||
|
Vector2 pos = new((float)index.X / width, (float)index.Y / height);
|
||||||
|
float minDist = 2f;
|
||||||
|
int count = edges.IntExtent.X;
|
||||||
|
|
||||||
|
for (int i = 0; i < count; i++) {
|
||||||
|
Vector2 edgeNrm = new (edges[i].X/width, edges[i].Y/height);
|
||||||
|
float dist = Vector2.Distance(pos, edgeNrm);
|
||||||
|
if (dist < minDist) minDist = dist;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (minDist > 1f) minDist = 1f;
|
||||||
|
|
||||||
|
sdf[index] = new(minDist);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -106,6 +106,30 @@ public partial class SdfKernels {
|
|||||||
edge = buffer.GetAsArray2D();
|
edge = buffer.GetAsArray2D();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Sdf(Vector2[] edges, out Vector3[,] sdf) {
|
||||||
|
var dev = gpuContext.GetPreferredDevice(preferCPU:false);
|
||||||
|
int width = edges.Length;
|
||||||
|
int height = edges.Length;
|
||||||
|
sdf = new Vector3[width, height];
|
||||||
|
using Accelerator accelerator = dev.CreateAccelerator(gpuContext);
|
||||||
|
|
||||||
|
using var buffer = accelerator.Allocate2DDenseX<Vector3>(new (width, height));
|
||||||
|
|
||||||
|
using var edgeBuffer = accelerator.Allocate1D<Vector2>(edges.Length);
|
||||||
|
edgeBuffer.CopyFromCPU(edges);
|
||||||
|
|
||||||
|
var sdfKernel = accelerator.LoadAutoGroupedStreamKernel<Index2D,
|
||||||
|
ArrayView2D<Vector3, Stride2D.DenseX>,
|
||||||
|
ArrayView1D<Vector2, Stride1D.Dense>,
|
||||||
|
int, int>(SdfKernel);
|
||||||
|
|
||||||
|
sdfKernel(new (width, height), buffer.View, edgeBuffer.View, width, height);
|
||||||
|
|
||||||
|
accelerator.Synchronize();
|
||||||
|
|
||||||
|
sdf = buffer.GetAsArray2D();
|
||||||
|
}
|
||||||
|
|
||||||
private static string GetInfoString(Accelerator a)
|
private static string GetInfoString(Accelerator a)
|
||||||
{
|
{
|
||||||
StringWriter infoString = new StringWriter();
|
StringWriter infoString = new StringWriter();
|
||||||
|
|||||||
Reference in New Issue
Block a user