From 0f5fe6776b806e630a5a1ff06b081a91738ee103 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 23 Aug 2019 19:39:49 +0200 Subject: [PATCH] Code tweaks to the bokeh sample --- samples/ComputeSharp.BokehBlur/Program.cs | 50 ++++++++++++----------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/samples/ComputeSharp.BokehBlur/Program.cs b/samples/ComputeSharp.BokehBlur/Program.cs index f6274586..52208139 100644 --- a/samples/ComputeSharp.BokehBlur/Program.cs +++ b/samples/ComputeSharp.BokehBlur/Program.cs @@ -79,40 +79,42 @@ namespace ComputeSharp.BokehBlur for (int j = 0; j < diameter; j++) kernel[i * diameter + j] /= ones; - // Apply the effect Console.WriteLine(">> Applying effect"); - using ReadOnlyBuffer image_gpu = Gpu.Default.AllocateReadOnlyBuffer(vectorArray); - using ReadOnlyBuffer kernel_gpu = Gpu.Default.AllocateReadOnlyBuffer(kernel); - using ReadWriteBuffer result_gpu = Gpu.Default.AllocateReadWriteBuffer(vectorArray.Length); - Gpu.Default.For(height, width, id => + using (ReadOnlyBuffer image_gpu = Gpu.Default.AllocateReadOnlyBuffer(vectorArray)) + using (ReadOnlyBuffer kernel_gpu = Gpu.Default.AllocateReadOnlyBuffer(kernel)) + using (ReadWriteBuffer result_gpu = Gpu.Default.AllocateReadWriteBuffer(vectorArray.Length)) { - Vector4 total = Vector4.Zero; - - for (int y = -Radius; y <= Radius; y++) + // Apply the effect + Gpu.Default.For(height, width, id => { - for (int x = -Radius; x <= Radius; x++) + Vector4 total = Vector4.Zero; + + for (int y = -Radius; y <= Radius; y++) { - int iy = (int)id.X + y; - int jx = (int)id.Y + x; + for (int x = -Radius; x <= Radius; x++) + { + int iy = (int)id.X + y; + int jx = (int)id.Y + x; - if (iy < 0) iy = -iy; - else if (iy > height) iy = 2 * height - iy; - if (jx < 0) jx = -jx; - else if (jx > width) jx = 2 * width - jx; + if (iy < 0) iy = -iy; + else if (iy > height) iy = 2 * height - iy; + if (jx < 0) jx = -jx; + else if (jx > width) jx = 2 * width - jx; - int ki = Radius - y; - int kj = Radius - x; + int ki = Radius - y; + int kj = Radius - x; - total += image_gpu[(uint)(iy * width + jx)] * kernel_gpu[(uint)(ki * diameter + kj)]; + total += image_gpu[(uint)(iy * width + jx)] * kernel_gpu[(uint)(ki * diameter + kj)]; + } } - } - result_gpu[(uint)(id.X * width + id.Y)] = total; - }); + result_gpu[(uint)(id.X * width + id.Y)] = total; + }); - // Copy data back - Console.WriteLine(">> Copying data back"); - result_gpu.GetData(vectorArray); + // Copy data back + Console.WriteLine(">> Copying data back"); + result_gpu.GetData(vectorArray); + } // Copy the modified image back Console.WriteLine(">> Storing pixel data");