diff --git a/SpectralConvolution/SpectralConvolution.Tests/GpuTileConvolverTests.cs b/SpectralConvolution/SpectralConvolution.Tests/GpuTileConvolverTests.cs index 1bd60f0..b91b578 100644 --- a/SpectralConvolution/SpectralConvolution.Tests/GpuTileConvolverTests.cs +++ b/SpectralConvolution/SpectralConvolution.Tests/GpuTileConvolverTests.cs @@ -146,6 +146,29 @@ public sealed class GpuTileConvolverTests } } + [Theory] + [InlineData(1f)] + [InlineData(2.75f)] + [InlineData(0.3f)] + public void RenderingTheStoreMatchesTheConvolutionBitForBit(float gain) + { + var scene = Scene(64, 5, 150, 97, 11, 47); + var device = HardwareOrDefault(); + var fused = Run(device, scene, gain); + using var convolver = new GpuTileConvolver(device); + var stored = Run(device, convolver, scene, 1f); + var width = scene.Plan.RegionWidth; + var height = scene.Plan.RegionHeight; + using var output = device.AllocateReadWriteTexture2D(width, height); + + using (var context = device.CreateComputeContext()) + GpuTileConvolver.RecordStored(in context, convolver.StoreFor(stored.Job), output, width, height, gain); + + var pixels = new Bgra32[width * height]; + output.CopyTo(pixels); + Assert.Equal(fused.Output, MemoryMarshal.Cast(pixels).ToArray()); + } + [Fact] public void TheStatisticsDescribeTheSource() { diff --git a/SpectralConvolution/SpectralConvolution/GpuTileConvolver.cs b/SpectralConvolution/SpectralConvolution/GpuTileConvolver.cs index 2818342..1267e2b 100644 --- a/SpectralConvolution/SpectralConvolution/GpuTileConvolver.cs +++ b/SpectralConvolution/SpectralConvolution/GpuTileConvolver.cs @@ -226,6 +226,15 @@ internal sealed class GpuTileConvolver : IDisposable context.Barrier(report); } + public static void RecordStored( + in ComputeContext context, + ReadWriteBuffer store, + ReadWriteTexture2D output, + int width, + int height, + float gain) + => context.For(width, height, new StoredRenderShader(store, output, width, height, gain)); + public void Dispose() { tiles?.Dispose(); diff --git a/SpectralConvolution/SpectralConvolution/GpuTileShaders.cs b/SpectralConvolution/SpectralConvolution/GpuTileShaders.cs index f755bd9..01f2028 100644 --- a/SpectralConvolution/SpectralConvolution/GpuTileShaders.cs +++ b/SpectralConvolution/SpectralConvolution/GpuTileShaders.cs @@ -16,6 +16,30 @@ internal readonly partial struct ReportResetShader( } } +[ThreadGroupSize(DefaultThreadGroupSizes.XY)] +[GeneratedComputeShaderDescriptor] +[CompileOptions(CompileOptions.Default | CompileOptions.IeeeStrictness)] +internal readonly partial struct StoredRenderShader( + ReadWriteBuffer store, + ReadWriteTexture2D output, + int width, + int height, + float gain) : IComputeShader +{ + private readonly ReadWriteBuffer store = store; + private readonly ReadWriteTexture2D output = output; + private readonly int width = width; + private readonly int height = height; + private readonly float gain = gain; + + public void Execute() + { + if (ThreadIds.X >= width || ThreadIds.Y >= height) + return; + output[ThreadIds.XY] = store[ThreadIds.Y * width + ThreadIds.X] * gain; + } +} + [ThreadGroupSize(DefaultThreadGroupSizes.XY)] [GeneratedComputeShaderDescriptor] [CompileOptions(CompileOptions.Default | CompileOptions.IeeeStrictness)]