diff --git a/WaveOptics/Rendering/WaveOpticsGpuShaders.cs b/WaveOptics/Rendering/WaveOpticsGpuShaders.cs index 86d916a..ce09625 100644 --- a/WaveOptics/Rendering/WaveOpticsGpuShaders.cs +++ b/WaveOptics/Rendering/WaveOpticsGpuShaders.cs @@ -62,29 +62,46 @@ internal readonly partial struct SourceHashShader( public void Execute() { - var x = ThreadIds.X; + var first = ThreadIds.X * WaveOpticsSettings.SourceHashSpan; var y = ThreadIds.Y; - if (x >= sourceWidth || y >= sourceHeight) + if (first >= sourceWidth || y >= sourceHeight) return; - var color = source[ThreadIds.XY]; - var quantized = ((uint)(color.X * 255f + 0.5f) << 24) - | ((uint)(color.Y * 255f + 0.5f) << 16) - | ((uint)(color.Z * 255f + 0.5f) << 8) - | (uint)(color.W * 255f + 0.5f); - if (quantized == 0u) - return; + var count = 0; + var sum = 0; + var mix = 0; + var minimumX = 2147483647; + var maximumX = -2147483648; + var last = Hlsl.Min(first + WaveOpticsSettings.SourceHashSpan, sourceWidth); + for (var x = first; x < last; x++) + { + var color = source[new Int2(x, y)]; + var quantized = ((uint)(color.X * 255f + 0.5f) << 24) + | ((uint)(color.Y * 255f + 0.5f) << 16) + | ((uint)(color.Z * 255f + 0.5f) << 8) + | (uint)(color.W * 255f + 0.5f); + if (quantized == 0u) + continue; - var mixed = ((uint)(y * sourceWidth + x) * 0x9E3779B9u) ^ (quantized * 0x85EBCA6Bu); - mixed ^= mixed >> 16; - mixed *= 0x85EBCA6Bu; - mixed ^= mixed >> 13; - Hlsl.InterlockedAdd(ref scratch[WaveOpticsSettings.ScratchHashSum], (int)mixed); - Hlsl.InterlockedXor(ref scratch[WaveOpticsSettings.ScratchHashMix], (int)(mixed * 0xC2B2AE35u)); - Hlsl.InterlockedAdd(ref scratch[WaveOpticsSettings.ScratchLitCount], 1); - Hlsl.InterlockedMin(ref scratch[WaveOpticsSettings.ScratchBoundsMinX], sourceX + x); + var mixed = ((uint)(y * sourceWidth + x) * 0x9E3779B9u) ^ (quantized * 0x85EBCA6Bu); + mixed ^= mixed >> 16; + mixed *= 0x85EBCA6Bu; + mixed ^= mixed >> 13; + sum += (int)mixed; + mix ^= (int)(mixed * 0xC2B2AE35u); + count++; + minimumX = Hlsl.Min(minimumX, sourceX + x); + maximumX = Hlsl.Max(maximumX, sourceX + x); + } + + if (count == 0) + return; + Hlsl.InterlockedAdd(ref scratch[WaveOpticsSettings.ScratchHashSum], sum); + Hlsl.InterlockedXor(ref scratch[WaveOpticsSettings.ScratchHashMix], mix); + Hlsl.InterlockedAdd(ref scratch[WaveOpticsSettings.ScratchLitCount], count); + Hlsl.InterlockedMin(ref scratch[WaveOpticsSettings.ScratchBoundsMinX], minimumX); Hlsl.InterlockedMin(ref scratch[WaveOpticsSettings.ScratchBoundsMinY], sourceY + y); - Hlsl.InterlockedMax(ref scratch[WaveOpticsSettings.ScratchBoundsMaxX], sourceX + x); + Hlsl.InterlockedMax(ref scratch[WaveOpticsSettings.ScratchBoundsMaxX], maximumX); Hlsl.InterlockedMax(ref scratch[WaveOpticsSettings.ScratchBoundsMaxY], sourceY + y); } } diff --git a/WaveOptics/Rendering/WaveOpticsPipelineHost.cs b/WaveOptics/Rendering/WaveOpticsPipelineHost.cs index d00e95b..8b3a57b 100644 --- a/WaveOptics/Rendering/WaveOpticsPipelineHost.cs +++ b/WaveOptics/Rendering/WaveOpticsPipelineHost.cs @@ -132,7 +132,7 @@ internal sealed partial class WaveOpticsPipelineHost { context.For(1, new SourceHashResetShader(scratch)); context.Barrier(scratch); - context.For(sourceRect.Width, sourceRect.Height, new SourceHashShader( + context.For(GetBlockCount(sourceRect.Width, WaveOpticsSettings.SourceHashSpan), sourceRect.Height, new SourceHashShader( source, scratch, sourceRect.X, sourceRect.Y, sourceRect.Width, sourceRect.Height)); context.Barrier(scratch); } @@ -151,13 +151,13 @@ internal sealed partial class WaveOpticsPipelineHost var bottom = Math.Min(rect.Y + rect.Height + kernel.Radius, sourceRect.Y + sourceRect.Height); for (var term = 0; term < kernel.Rank; term++) { - context.For(GetBlockCount(rect.Width), bottom - top, new HorizontalPassShader( + context.For(GetBlockCount(rect.Width, WaveOpticsSettings.ConvolutionBlock), bottom - top, new HorizontalPassShader( source, weights, canvas.Horizontal, WaveOpticsSettings.GetWeightOffset(term, 0), kernel.Radius, rect.X, top, rect.Width, bottom - top, canvasWidth, sourceRect.X, sourceRect.Y, sourceRect.Width)); context.Barrier(canvas.Horizontal); - context.For(rect.Width, GetBlockCount(rect.Height), new VerticalPassShader( + context.For(rect.Width, GetBlockCount(rect.Height, WaveOpticsSettings.ConvolutionBlock), new VerticalPassShader( canvas.Horizontal, weights, canvas.Convolved, WaveOpticsSettings.GetWeightOffset(term, 1), kernel.Radius, rect.X, rect.Y, rect.Width, rect.Height, canvasWidth, @@ -167,8 +167,8 @@ internal sealed partial class WaveOpticsPipelineHost } } - private static int GetBlockCount(int length) - => (length + WaveOpticsSettings.ConvolutionBlock - 1) / WaveOpticsSettings.ConvolutionBlock; + private static int GetBlockCount(int length, int block) + => (length + block - 1) / block; private static void RecordRenderStage( in ComputeContext context, diff --git a/WaveOptics/WaveOpticsSettings.cs b/WaveOptics/WaveOpticsSettings.cs index fb7cfb6..d906929 100644 --- a/WaveOptics/WaveOpticsSettings.cs +++ b/WaveOptics/WaveOpticsSettings.cs @@ -11,6 +11,7 @@ internal static class WaveOpticsSettings public const double SeparableResidualRatio = 1e-4; public const int WeightsLength = MaximumRank * 2 * MaximumKernelSize; public const int ConvolutionBlock = 8; + public const int SourceHashSpan = 16; public const int CanvasMargin = (MaximumKernelRadius + 3) & ~3; public const int MaximumCanvasSize = 8192; public const int ScratchLength = 7;