Changed blur iterations to reuse buffers. Huge memory savings + speedup

This commit is contained in:
Samuele Lorefice
2025-04-08 21:39:06 +02:00
parent 3e90df9e6e
commit 93002270d5
4 changed files with 21 additions and 16 deletions

View File

@@ -10,6 +10,8 @@
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ASafeFileHandle_002EWindows_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F261ea83c988816e3d8fe76b15b7ac6c10af64b8f9e739854f83c137c8ba9_003FSafeFileHandle_002EWindows_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AThrowHelper_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F2c8e7ca976f350cba9836d5565dac56b11e0b56656fa786460eb1395857a6fa_003FThrowHelper_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AVector3_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003Fhome_003Fmm00_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F6edafe13d8727aa238b865f5dc91dbc984b5abfbc60bece3744f6311c2c_003FVector3_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:Int64 x:Key="/Default/Dpa/Thresholds/=AllocationLoh/@EntryIndexedValue">52428800</s:Int64>
<s:Int64 x:Key="/Default/Dpa/Thresholds/=AllocationTopSoh/@EntryIndexedValue">26214400</s:Int64>
<s:String x:Key="/Default/Environment/AssemblyExplorer/XmlDocument/@EntryValue">&lt;AssemblyExplorer&gt;
&lt;Assembly Path="/mnt/nvme2/Railgun/SDFMapCreator/SDFMapCreator/bin/Debug/net8.0/Magick.NET-Q16-HDRI-OpenMP-x64.dll" /&gt;
&lt;/AssemblyExplorer&gt;</s:String></wpf:ResourceDictionary>

View File

@@ -177,10 +177,8 @@ public static class Program {
var sigma = options.BlurSigma;
var totalMask = SelfMask(Images[^1].Pixels);
if(options.Debug) totalMask.SaveImage($"{debugPath}{PSep}TotalMask.png");
for (var i = 0; i < iterations; i++) {
Console.WriteLine($"Applying directional blur {i + 1}/{iterations}...");
finalImage = DirectionalBlur(finalImage, totalMask, radius, step, sigma);
}
finalImage = DirectionalBlur(finalImage, totalMask, iterations, radius, step, sigma);
finalImage.SaveImage(options.OutputFile);
Console.WriteLine("Done!");
@@ -237,12 +235,12 @@ public static class Program {
return new(temp);
}
static Vector3[,] DirectionalBlur(Vector3[,] image, Vector3[,] mask, float radius = 3f, float step = .5f, float sigma = 1f) {
static Vector3[,] DirectionalBlur(Vector3[,] image, Vector3[,] mask, int iterations, float radius = 3f, float step = .5f, float sigma = 1f) {
var sw = new Stopwatch();
sw.Start();
kernels.DirectionalBlur(image, mask, out var temp, radius, step, sigma);
kernels.DirectionalBlur(image, mask, out var temp, iterations, radius, step, sigma);
Console.WriteLine(
$"Directional Blur Time: {sw.Elapsed.TotalSeconds:N4}s ({width * height / sw.Elapsed.TotalSeconds:N0} pixels/s)");
$"Directional Blur Time: {sw.Elapsed.TotalSeconds:N4}s ({width * height * iterations / sw.Elapsed.TotalSeconds:N0} pixels/s)");
return temp;
}

View File

@@ -138,6 +138,9 @@ public partial class SdfKernels {
return;
}
//clean up the buffer in case
output[index] = new(0f, 0f, 0f);
gradient = Vector2.Normalize(gradient);
float sum = 0;

View File

@@ -171,7 +171,7 @@ public partial class SdfKernels {
gradient = bufferGradient.GetAsArray2D();
}
public void DirectionalBlur(Vector3[,] image, Vector3[,] mask, out Vector3[,] output, float radius = 3f,
public void DirectionalBlur(Vector3[,] image, Vector3[,] mask, out Vector3[,] output, int iterations, float radius = 3f,
float step = .5f, float sigma = 1f) {
var width = image.GetLength(0);
var height = image.GetLength(1);
@@ -191,10 +191,12 @@ public partial class SdfKernels {
ArrayView2D<Vector3, Stride2D.DenseX>,
float, float, float, int, int>(DirectionalBlurKernel);
blurKernel(new(width, height), imageBuffer.View, maskBuffer.View, outputBuffer.View,
radius, step, sigma, width, height);
var stream = accelerator.DefaultStream;
for (int i = 0; i < iterations; i++) {
if (i > 0) outputBuffer.CopyTo(stream, imageBuffer);
blurKernel(new(width, height), imageBuffer.View, maskBuffer.View, outputBuffer.View, radius, step, sigma, width, height);
accelerator.Synchronize();
}
output = outputBuffer.GetAsArray2D();
}