Compare commits

...

10 Commits

Author SHA1 Message Date
Samuele Lorefice
c65346aeec Added release build configs 2025-04-07 07:56:23 +02:00
Samuele Lorefice
a74ae1167e Updated gitignore 2025-04-07 07:51:39 +02:00
mm00
d7d2091d64 Merge remote-tracking branch 'origin/ILGpu' into ILGpu 2025-04-04 19:23:20 +02:00
mm00
ea28738280 added border to initial image used in final image calculation 2025-04-04 19:23:02 +02:00
Samuele Lorefice
2faab96a7d Merge remote-tracking branch 'origin/ILGpu' into ILGpu 2025-04-04 19:22:26 +02:00
Samuele Lorefice
8c5ca2ce21 Small kernel fixes 2025-04-04 19:22:05 +02:00
mm00
d0fbd31c1d fixed final image masking 2025-04-04 19:20:43 +02:00
mm00
5cf8612699 Fixed directional blur 2025-04-03 20:49:44 +02:00
mm00
dfa2cf3f31 added luma threshould 2025-04-03 19:28:26 +02:00
mm00
64b7eb9dcc added SDF normalization 2025-04-03 19:28:20 +02:00
6 changed files with 118 additions and 29 deletions

1
.gitignore vendored
View File

@@ -3,3 +3,4 @@ obj/
/packages/
riderModule.iml
/_ReSharper.Caches/
publish/

View File

@@ -0,0 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Release Build Linux-x64" type="DotNetFolderPublish" factoryName="Publish to folder">
<riderPublish configuration="Release" delete_existing_files="true" include_native_libs_for_self_extract="true" platform="Any CPU" produce_single_file="true" runtime="linux-x64" target_folder="$PROJECT_DIR$/SDFMapCreator/publish/linux-x64" target_framework="net8.0" uuid_high="-7972981449231152312" uuid_low="-5206031561210231144" />
<method v="2" />
</configuration>
</component>

View File

@@ -0,0 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Release Build Win-x64" type="DotNetFolderPublish" factoryName="Publish to folder">
<riderPublish configuration="Release" delete_existing_files="true" include_native_libs_for_self_extract="true" platform="Any CPU" produce_single_file="true" runtime="win-x64" target_folder="$PROJECT_DIR$/SDFMapCreator/publish/win-x64" target_framework="net8.0" uuid_high="-7972981449231152312" uuid_low="-5206031561210231144" />
<method v="2" />
</configuration>
</component>

View File

@@ -12,4 +12,8 @@
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/INDENT_NESTED_FOR_STMT/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_BEFORE_ARROW_WITH_EXPRESSIONS/@EntryValue">True</s:Boolean>
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_LIMIT/@EntryValue">151</s:Int64>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_OBJECT_AND_COLLECTION_INITIALIZER_STYLE/@EntryValue">WRAP_IF_LONG</s:String></wpf:ResourceDictionary>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_OBJECT_AND_COLLECTION_INITIALIZER_STYLE/@EntryValue">WRAP_IF_LONG</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpUseContinuousIndentInsideBracesMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

View File

@@ -29,12 +29,14 @@ public static class Program {
Console.WriteLine("Debug mode enabled.");
}
var imagesPath = "images";
for (var i = 0; i < 8; i++) {
var pixels = ImageUtil.LoadImage<Vector3>($"./{imagesPath}{Path.DirectorySeparatorChar}{i + 1:00}.png");
Images.Add(new(pixels, pixels.GetLength(0), pixels.GetLength(1)));
}
/*
/*
var pixels = ImageUtil.LoadImage<Vector3>($"./sphereempty.png");
Images.Add(new(pixels, pixels.GetLength(0), pixels.GetLength(1)));
pixels = ImageUtil.LoadImage<Vector3>($"./spherehalf.png");
@@ -54,6 +56,22 @@ public static class Program {
width = (uint)Images[0].Width;
height = (uint)Images[0].Height;
// sum all images together
if(debug) Images[0].Pixels.SaveImage("Debug/Sum0.png");
for (int i = 1; i < Images.Count; i++) {
for (int x = 0; x < Images[i].Width; x++) {
for (int y = 0; y < Images[i].Height; y++) {
Images[i].Pixels[x, y].X = MathF.Min(Images[i - 1].Pixels[x, y].X + Images[i].Pixels[x, y].X, MAX);
Images[i].Pixels[x, y].Y = MathF.Min(Images[i - 1].Pixels[x, y].Y + Images[i].Pixels[x, y].X, MAX);
Images[i].Pixels[x, y].Z = MathF.Min(Images[i - 1].Pixels[x, y].Z + Images[i].Pixels[x, y].X, MAX);
}
}
if(debug)Images[i].Pixels.SaveImage($"Debug/Sum{i}.png");
}
Console.WriteLine("Creating masks...");
for (var i = 0; i < Images.Count; i++) { //for each image pair, create a mask
var selfMask = SelfMask(Images[i].Pixels);
@@ -63,7 +81,6 @@ public static class Program {
ConsoleUpdateLine($"Creating mask {i}...");
var mask = GetABMask(Images[i].Pixels, Images[i + 1].Pixels);
TransitionMasks.Add(new(mask, Images[i], Images[i + 1]));
if (debug) mask.SaveImage($"Debug/mask{i}.png");
}
//EdgeDetect all masks
@@ -96,9 +113,10 @@ public static class Program {
var currStep = 0f;
var stepIncrement = MAX / Gradients.Count;
for (var x = 0; x < width; x++)
for (var y = 0; y < height; y++)
finalImage[x, y] = new(Images[0].Pixels[x, y].X);
finalImage[x, y] = new(ImageMasks[0].Mask[x, y].X != 0 || ImageMasks[0].Mask[x, y].Y > 0 ? MAX : MIN);
for (var i = 0; i < Gradients.Count; i++) {
var mask = ImageMasks[i + 1];
@@ -106,7 +124,7 @@ public static class Program {
Console.WriteLine($"Applying gradient {i}..., {currStep} -> {currStep + stepIncrement}");
for (var x = 0; x < mask.Mask.GetLength(0); x++) {
for (var y = 0; y < mask.Mask.GetLength(1); y++) {
if (gradient[x, y].X == 0) continue;
if (mask.Mask[x,y].X == 0) continue;
var pixel = new Vector3(Remap(gradient[x, y].X, MIN, MAX, 1.0f - currStep,
1.0f - (currStep + stepIncrement)));
if (pixel.X > finalImage[x, y].X) finalImage[x, y] = pixel;
@@ -115,17 +133,21 @@ public static class Program {
currStep += stepIncrement;
}
finalImage.SaveImage("Debug/Final.png");
// apply directional blur
var iterations = 10;
var radius = 3f;
var iterations = 1;
var radius = 100f;
var step = .5f;
var sigma = 1f;
var totalMask = SelfMask(Images[^1].Pixels);
totalMask.SaveImage("Debug/TotalMask.png");
for (var i = 0; i < iterations; i++) {
Console.WriteLine($"Applying directional blur {i + 1}/{iterations}...");
finalImage = DirectionalBlur(finalImage, ImageMasks[0].Mask, radius, step, sigma);
finalImage = DirectionalBlur(finalImage, totalMask, radius, step, sigma);
}
finalImage.SaveImage("final.png");
finalImage.SaveImage("finalBlur.png");
Console.WriteLine("Done!");
}
@@ -182,6 +204,24 @@ public static class Program {
sw.Start();
kernels.Sdf(mask.Edges.ToArray(), (int)width, (int)height, out var temp);
Console.WriteLine($"\nSDF Generation Time: {sw.Elapsed.TotalSeconds:N4}s ({width * height / sw.Elapsed.TotalSeconds:N0} pixels/s)");
sw.Restart();
var absMax = 0f;
foreach (var pixel in temp) {
if (pixel.X > absMax) absMax = pixel.X;
}
Parallel.For(0, width * height, parallelOptions, (i) => {
//convert 1D index to 2D index
var x = (int)(i % width);
var y = (int)(i / width);
temp[x, y] = new(Remap(temp[x, y].X, 0, absMax, MIN, MAX));
});
sw.Stop();
Console.WriteLine(
$"SDF Normalization Time: {sw.Elapsed.TotalSeconds:N4}s ({width * height / sw.Elapsed.TotalSeconds:N0} pixels/s)");
return new(temp);
}

View File

@@ -5,12 +5,14 @@ using ILGPU.Runtime;
namespace SDFMapCreator;
public partial class SdfKernels {
private const float LUMA_THRESHOLD = 0.0f;
static void SelfMaskKernel(Index2D index, ArrayView2D<Vector3, Stride2D.DenseX> input, ArrayView2D<Vector3, Stride2D.DenseX> mask) {
var x = index.X;
var y = index.Y;
var value = input[x, y];
var lumaA = (value.X + value.Y + value.Z) / 3f;
var r = lumaA > 0.99f ? 1f : 0f;
var lumaA = value.X;
var r = lumaA > LUMA_THRESHOLD ? 1f : 0f;
mask[x, y] = new(r, 0f, 0f);
}
@@ -23,9 +25,9 @@ public partial class SdfKernels {
var y = index.Y;
var valueA = A[x, y];
var valueB = B[x, y];
var lumaA = (valueA.X + valueA.Y + valueA.Z) / 3f;
var lumaB = (valueB.X + valueB.Y + valueB.Z) / 3f;
var r = lumaA > 0.99f && lumaB > 0.99f ? 1f : 0f;
var lumaA = valueA.X;
var lumaB = valueB.X;
var r = lumaB - lumaA > LUMA_THRESHOLD ? 1f : 0f;
mask[x, y] = new(r, 0f, 0f);
}
@@ -37,7 +39,10 @@ public partial class SdfKernels {
var x = index.X;
var y = index.Y;
//if we are on the edge of the image, return false
if (x == 0 || y == 0 || x == width - 1 || y == height - 1) return;
if (x == 0 || y == 0 || x == width - 1 || y == height - 1) {
mask[index].Y = 1f; //set the edge flag
return;
}
//check the 3x3 kernel
for (var xi = x - 1; xi <= x + 1; xi++) {
@@ -76,12 +81,33 @@ public partial class SdfKernels {
ArrayView2D<Vector3, Stride2D.DenseX> sdfb,
ArrayView2D<Vector3, Stride2D.DenseX> gradient
) { //early exit if not on mask
if (mask[index].X == 0f || mask[index].Y > 0f) return;
if (mask[index].X == 0f) return;
var a = sdfa[index].X;
var b = sdfb[index].X;
gradient[index] = new(a / (a + b));
}
static Vector3 SampleBilinear(ArrayView2D<Vector3, Stride2D.DenseX> image, float x, float y) {
int width = image.IntExtent.X;
int height = image.IntExtent.Y;
var x0 = (int)x;
var y0 = (int)y;
var x1 = x0 + 1;
var y1 = y0 + 1;
if (x0 < 0 || x1 >= width || y0 < 0 || y1 >= height) return Vector3.Zero;
var a = new Vector2(x - x0, y - y0);
var b = new Vector2(1f - a.X, 1f - a.Y);
return Vector3.Lerp(
Vector3.Lerp(image[x0, y0], image[x1, y0], a.X),
Vector3.Lerp(image[x0, y1], image[x1, y1], a.X),
a.Y
);
}
static void DirectionalBlurKernel(Index2D index,
ArrayView2D<Vector3, Stride2D.DenseX> image,
ArrayView2D<Vector3, Stride2D.DenseX> mask,
@@ -100,13 +126,19 @@ public partial class SdfKernels {
var gradient = Vector2.Zero;
for (var dx = -1; dx <= 1; dx++) {
for (var dy = -1; dy <= 1; dy++) {
if (x + dx < 0 || x + dx >= width || y + dy < 0 || y + dy >= height) continue;
gradient += new Vector2(dx, dy) * image[x + dx, y + dy].X;
}
// calculate the gradient
for (int i = -1; i <= 1; i++) {
if (x + i < 0 || x + i >= width || y + i < 0 || y + i >= height) continue;
gradient.X += i * image[x + i, y].X;
gradient.Y += i * image[x, y + i].X;
}
/*
output[x, y] = new Vector3(float.Abs((gradient.X * 0.5f) + 0.5f),
float.Abs((gradient.Y * 0.5f) + 0.5f), 0.5f);
return;
*/
if (gradient == Vector2.Zero) {
output[x, y] = value;
return;
@@ -117,19 +149,19 @@ public partial class SdfKernels {
// now we follow the direction line and sample the image for length;
for (var l = -radius; l <= radius; l += step) {
var xOffset = (int)(gradient.X * l);
var yOffset = (int)(gradient.Y * l);
var xOffset = (gradient.X * l);
var yOffset = (gradient.Y * l);
var xSample = x + xOffset;
var ySample = y + yOffset;
if (xSample < 0 || xSample >= width || ySample < 0 || ySample >= height) continue;
var sampleValue = image[xSample, ySample];
var weight = MathF.Exp(-l * l / (2f * sigma * sigma));
var sampleValue = SampleBilinear(image, xSample, ySample);
var weight = MathF.Exp(-(l * l) / (2f * sigma * sigma));
output[x, y] += sampleValue * weight;
sum += weight;
}
output[x, y] /= sum;
output[x, y] = output[x, y] / sum;
}
}