GPUで色ごとのカーネルを畳み込む
このコミットが含まれているのは:
5ファイル変更
+422
-25
検索条件に一致するファイルはありません
@@ -0,0 +1,159 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeWeave;
|
||||
|
||||
namespace SpectralConvolution.Tests;
|
||||
|
||||
[Collection("Direct3D12")]
|
||||
public sealed class ChromaticGpuConvolverTests
|
||||
{
|
||||
sealed record GpuRun(byte[] Output, float[] Store, uint[] Report, GpuTileJob Job, Int2[] Samples);
|
||||
|
||||
static GraphicsDevice HardwareOrDefault() => GraphicsDevice.GetDefault();
|
||||
|
||||
static Int2[] Samples(in TilePlan plan)
|
||||
{
|
||||
var width = plan.RegionWidth;
|
||||
var height = plan.RegionHeight;
|
||||
var valid = plan.ValidSize;
|
||||
List<Int2> samples =
|
||||
[
|
||||
new(0, 0), new(width - 1, 0), new(0, height - 1), new(width - 1, height - 1), new(width / 2, height / 2),
|
||||
new(Math.Min(valid - 1, width - 1), Math.Min(valid - 1, height - 1)), new(Math.Min(valid, width - 1), Math.Min(valid, height - 1)),
|
||||
];
|
||||
var random = new Random(width * 31 + height);
|
||||
while (samples.Count < GpuTileConvolver.MaximumSamples)
|
||||
samples.Add(new Int2(random.Next(width), random.Next(height)));
|
||||
return [.. samples];
|
||||
}
|
||||
|
||||
static GpuRun Run(GraphicsDevice device, GpuTileConvolver convolver, ChromaticConvolutionScene scene, float gain = 1f)
|
||||
{
|
||||
var plan = scene.Plan;
|
||||
var samples = Samples(plan);
|
||||
convolver.Upload(scene.Spectrum);
|
||||
var job = convolver.Prepare(plan, scene.SourceX, scene.SourceY, scene.SourceWidth, scene.SourceHeight, gain, samples, true, scene.Light, true);
|
||||
using var source = device.AllocateReadWriteTexture2D<Bgra32, Float4>(scene.SourceWidth, scene.SourceHeight);
|
||||
source.CopyFrom(MemoryMarshal.Cast<byte, Bgra32>(scene.Source));
|
||||
using var output = device.AllocateReadWriteTexture2D<Bgra32, Float4>(plan.RegionWidth, plan.RegionHeight);
|
||||
using (var context = device.CreateComputeContext())
|
||||
{
|
||||
GpuTileConvolver.Record(
|
||||
in context, source, output, convolver.Tiles, convolver.Twiddles, convolver.Spectrum,
|
||||
convolver.Report, convolver.Samples, convolver.StoreFor(job), job);
|
||||
}
|
||||
|
||||
var report = convolver.ReadReport(job).ToArray();
|
||||
var store = new Float4[plan.RegionWidth * plan.RegionHeight];
|
||||
convolver.ReadStore(job, store);
|
||||
var pixels = new Bgra32[plan.RegionWidth * plan.RegionHeight];
|
||||
output.CopyTo(pixels);
|
||||
return new GpuRun(MemoryMarshal.Cast<Bgra32, byte>(pixels).ToArray(), MemoryMarshal.Cast<Float4, float>(store).ToArray(), report, job, samples);
|
||||
}
|
||||
|
||||
static GpuRun Run(GraphicsDevice device, ChromaticConvolutionScene scene, float gain = 1f)
|
||||
{
|
||||
using var convolver = new GpuTileConvolver(device);
|
||||
return Run(device, convolver, scene, gain);
|
||||
}
|
||||
|
||||
static ChromaticConvolutionScene Scene(int size, int radius, int width, int height, int margin, int seed, LightOptions light = default)
|
||||
=> ChromaticConvolutionScene.Create(ConvolutionScene.RandomSource(width, height, seed, 0.45), width, height, margin, radius, size, light);
|
||||
|
||||
[Theory]
|
||||
[InlineData(64, 5, 150, 97, 11)]
|
||||
[InlineData(128, 23, 140, 90, 24)]
|
||||
[InlineData(256, 40, 110, 70, 44)]
|
||||
[InlineData(512, 3, 1100, 560, 8)]
|
||||
[InlineData(512, 63, 600, 300, 64)]
|
||||
public void TheResultStaysWithinTheBoundOfTheExactCorrelationOfEachChannel(int size, int radius, int width, int height, int margin)
|
||||
{
|
||||
var scene = Scene(size, radius, width, height, margin, size + radius);
|
||||
|
||||
var run = Run(HardwareOrDefault(), scene);
|
||||
|
||||
Assert.True(scene.Plan.TileCount > 1 || size == 512);
|
||||
Assert.InRange(scene.WorstRatio(run.Store, ConvolutionBound.GpuOperationError), 0d, 1d);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(true, 0f, 0f)]
|
||||
[InlineData(true, 0.8f, 20f)]
|
||||
public void TheLinearLightResultStaysWithinTheBound(bool linear, float threshold, float boost)
|
||||
{
|
||||
var scene = Scene(128, 9, 130, 90, 12, 21, new LightOptions(linear, false, threshold, boost));
|
||||
|
||||
var run = Run(HardwareOrDefault(), scene);
|
||||
|
||||
Assert.InRange(scene.WorstRatio(run.Store, ConvolutionBound.GpuOperationError), 0d, 1d);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TheResultMatchesTheCpuResultWithinTheBoundsOfBoth()
|
||||
{
|
||||
var scene = Scene(128, 15, 200, 120, 20, 7);
|
||||
var cpu = new float[scene.RegionLength];
|
||||
var cpuOutput = new byte[scene.RegionLength];
|
||||
using (var convolver = new CpuTileConvolver(2))
|
||||
convolver.Convolve(scene.Source, scene.SourceX, scene.SourceY, scene.SourceWidth, scene.SourceHeight, scene.Spectrum, scene.Plan, cpuOutput, 1f, cpu);
|
||||
|
||||
var run = Run(HardwareOrDefault(), scene);
|
||||
|
||||
var tolerance = ConvolutionBound.ChromaticAbsolute(128, ConvolutionBound.GpuOperationError, 1e4) + ConvolutionBound.ChromaticAbsolute(128, ConvolutionBound.CpuOperationError, 1e4);
|
||||
for (var index = 0; index < cpu.Length; index++)
|
||||
Assert.True(Math.Abs(cpu[index] - run.Store[index]) <= tolerance, $"{index}: {cpu[index]} {run.Store[index]}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EveryMeasurementOfTheCheckPasses()
|
||||
{
|
||||
var scene = Scene(512, 20, 700, 400, 24, 3);
|
||||
|
||||
var run = Run(HardwareOrDefault(), scene);
|
||||
|
||||
var measurements = new ConvolutionMeasurement[GpuTileCheck.MeasurementCount(run.Samples.Length)];
|
||||
GpuTileCheck.Evaluate(run.Report, run.Job, run.Samples, scene.Spectrum.Red.Kernel, scene.Spectrum.Green.Kernel, scene.Spectrum.Blue.Kernel, measurements);
|
||||
Assert.All(measurements, measurement => Assert.True(measurement.Passes, $"{measurement}"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ACheckWithTheWrongKernelOfOneChannelFails()
|
||||
{
|
||||
var scene = Scene(128, 12, 160, 100, 16, 11);
|
||||
var run = Run(HardwareOrDefault(), scene);
|
||||
var measurements = new ConvolutionMeasurement[GpuTileCheck.MeasurementCount(run.Samples.Length)];
|
||||
|
||||
GpuTileCheck.Evaluate(run.Report, run.Job, run.Samples, scene.Spectrum.Red.Kernel, scene.Spectrum.Green.Kernel, scene.Spectrum.Green.Kernel, measurements);
|
||||
|
||||
Assert.Contains(measurements, measurement => measurement.Name == "spot.blue" && !measurement.Passes);
|
||||
Assert.All(measurements.Where(measurement => measurement.Name is "spot.red" or "spot.green" or "spot.alpha"), measurement => Assert.True(measurement.Passes, $"{measurement}"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TwoRunsAgreeBitForBit()
|
||||
{
|
||||
var scene = Scene(128, 9, 300, 170, 16, 5);
|
||||
|
||||
var first = Run(HardwareOrDefault(), scene, 1.5f);
|
||||
var second = Run(HardwareOrDefault(), scene, 1.5f);
|
||||
|
||||
Assert.Equal(first.Output, second.Output);
|
||||
Assert.Equal(first.Store, second.Store);
|
||||
Assert.Equal(first.Report, second.Report);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AMonochromeRunAfterAChromaticOneUsesTheMonochromeSpectrum()
|
||||
{
|
||||
var device = HardwareOrDefault();
|
||||
var chromatic = Scene(128, 9, 150, 100, 16, 5);
|
||||
var single = ConvolutionScene.Create(ConvolutionScene.RandomSource(150, 100, 5, 0.45), 150, 100, 16, 9, 128);
|
||||
using var convolver = new GpuTileConvolver(device);
|
||||
|
||||
_ = Run(device, convolver, chromatic);
|
||||
convolver.Upload(single.Spectrum);
|
||||
var job = convolver.Prepare(single.Plan, single.SourceX, single.SourceY, single.SourceWidth, single.SourceHeight, 1f, [], false);
|
||||
|
||||
Assert.False(job.Chromatic);
|
||||
Assert.Throws<InvalidOperationException>(() => convolver.Prepare(single.Plan, single.SourceX, single.SourceY, single.SourceWidth, single.SourceHeight, 1f, [], false, default, true));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
using ComputeWeave;
|
||||
|
||||
namespace SpectralConvolution;
|
||||
|
||||
[ThreadGroupSize(256, 1, 1)]
|
||||
[GeneratedComputeShaderDescriptor]
|
||||
[CompileOptions(CompileOptions.Default | CompileOptions.IeeeStrictness)]
|
||||
internal readonly partial struct ColumnChromaticShader(
|
||||
ReadWriteBuffer<Float4> tiles,
|
||||
ReadOnlyBuffer<Float2> twiddles,
|
||||
ReadOnlyBuffer<Float2> spectrum,
|
||||
int log2Size) : IComputeShader
|
||||
{
|
||||
private readonly ReadWriteBuffer<Float4> tiles = tiles;
|
||||
private readonly ReadOnlyBuffer<Float2> twiddles = twiddles;
|
||||
private readonly ReadOnlyBuffer<Float2> spectrum = spectrum;
|
||||
private readonly int log2Size = log2Size;
|
||||
|
||||
[GroupShared(1024)]
|
||||
private static readonly Float4[] row = null!;
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
var local = GroupIds.X;
|
||||
var group = ThreadIds.X >> 8;
|
||||
var size = (int)(1u << log2Size);
|
||||
var mask = size - 1;
|
||||
var shift = 32 - log2Size;
|
||||
var halfColumns = size / 2 + 1;
|
||||
var unitsPerGroup = (int)(512u >> log2Size);
|
||||
var groupsPerTile = (halfColumns + unitsPerGroup - 1) / unitsPerGroup;
|
||||
var tile = group / groupsPerTile;
|
||||
var firstColumn = group % groupsPerTile * unitsPerGroup;
|
||||
var tileBase = tile << (log2Size * 2);
|
||||
var planeLength = halfColumns * size;
|
||||
|
||||
for (var step = 0; step < 2; step++)
|
||||
{
|
||||
var pair = local + step * 256;
|
||||
var unit = pair % unitsPerGroup;
|
||||
var y = pair / unitsPerGroup;
|
||||
var column = firstColumn + unit;
|
||||
var redGreen = new Float4(0f, 0f, 0f, 0f);
|
||||
var blueAlpha = new Float4(0f, 0f, 0f, 0f);
|
||||
if (column < halfColumns)
|
||||
{
|
||||
var near = tiles[tileBase + (y << log2Size) + column];
|
||||
var far = tiles[tileBase + (y << log2Size) + ((size - column) & mask)];
|
||||
redGreen = new Float4(near.X + far.X, near.Y - far.Y, near.Y + far.Y, far.X - near.X) * 0.5f;
|
||||
blueAlpha = new Float4(near.Z + far.Z, near.W - far.W, near.W + far.W, far.Z - near.Z) * 0.5f;
|
||||
}
|
||||
|
||||
var reversed = (int)(Hlsl.ReverseBits((uint)y) >> shift);
|
||||
row[((unit * 2) << log2Size) + reversed] = redGreen;
|
||||
row[((unit * 2 + 1) << log2Size) + reversed] = blueAlpha;
|
||||
}
|
||||
|
||||
Hlsl.GroupMemoryBarrierWithGroupSync();
|
||||
|
||||
Butterflies(local, 1f);
|
||||
|
||||
var product0 = Multiply(local, planeLength, halfColumns, firstColumn, mask, 0);
|
||||
var product1 = Multiply(local, planeLength, halfColumns, firstColumn, mask, 1);
|
||||
var product2 = Multiply(local, planeLength, halfColumns, firstColumn, mask, 2);
|
||||
var product3 = Multiply(local, planeLength, halfColumns, firstColumn, mask, 3);
|
||||
Hlsl.GroupMemoryBarrierWithGroupSync();
|
||||
row[ReversedIndex(local, shift, mask, 0)] = product0;
|
||||
row[ReversedIndex(local, shift, mask, 1)] = product1;
|
||||
row[ReversedIndex(local, shift, mask, 2)] = product2;
|
||||
row[ReversedIndex(local, shift, mask, 3)] = product3;
|
||||
Hlsl.GroupMemoryBarrierWithGroupSync();
|
||||
|
||||
Butterflies(local, -1f);
|
||||
|
||||
for (var step = 0; step < 2; step++)
|
||||
{
|
||||
var pair = local + step * 256;
|
||||
var unit = pair % unitsPerGroup;
|
||||
var y = pair / unitsPerGroup;
|
||||
var column = firstColumn + unit;
|
||||
if (column >= halfColumns)
|
||||
continue;
|
||||
|
||||
var redGreen = row[((unit * 2) << log2Size) + y];
|
||||
var blueAlpha = row[((unit * 2 + 1) << log2Size) + y];
|
||||
var address = tileBase + (y << log2Size);
|
||||
tiles[address + column] = new Float4(
|
||||
redGreen.X - redGreen.W,
|
||||
redGreen.Y + redGreen.Z,
|
||||
blueAlpha.X - blueAlpha.W,
|
||||
blueAlpha.Y + blueAlpha.Z);
|
||||
var mirror = (size - column) & mask;
|
||||
if (mirror != column)
|
||||
{
|
||||
tiles[address + mirror] = new Float4(
|
||||
redGreen.X + redGreen.W,
|
||||
redGreen.Z - redGreen.Y,
|
||||
blueAlpha.X + blueAlpha.W,
|
||||
blueAlpha.Z - blueAlpha.Y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private int ReversedIndex(int local, int shift, int mask, int part)
|
||||
{
|
||||
var element = local + part * 256;
|
||||
return (element & ~mask) + (int)(Hlsl.ReverseBits((uint)(element & mask)) >> shift);
|
||||
}
|
||||
|
||||
private Float4 Multiply(int local, int planeLength, int halfColumns, int firstColumn, int mask, int part)
|
||||
{
|
||||
var element = local + part * 256;
|
||||
var line = element >> log2Size;
|
||||
var frequency = element & mask;
|
||||
var column = firstColumn + (line >> 1);
|
||||
if (column >= halfColumns)
|
||||
return new Float4(0f, 0f, 0f, 0f);
|
||||
|
||||
var value = row[element];
|
||||
var index = (column << log2Size) + frequency;
|
||||
var first = spectrum[(line & 1) * 2 * planeLength + index];
|
||||
var second = spectrum[planeLength + index];
|
||||
return new Float4(
|
||||
value.X * first.X - value.Y * first.Y,
|
||||
value.X * first.Y + value.Y * first.X,
|
||||
value.Z * second.X - value.W * second.Y,
|
||||
value.Z * second.Y + value.W * second.X);
|
||||
}
|
||||
|
||||
private void Butterflies(int local, float direction)
|
||||
{
|
||||
var halfSize = (int)(1u << (log2Size - 1));
|
||||
for (var stage = 0; stage < log2Size; stage++)
|
||||
{
|
||||
var half = (int)(1u << stage);
|
||||
for (var step = 0; step < 2; step++)
|
||||
{
|
||||
var index = local + step * 256;
|
||||
var rowBase = (index >> (log2Size - 1)) << log2Size;
|
||||
var pair = index & (halfSize - 1);
|
||||
var position = pair & (half - 1);
|
||||
var even = rowBase + ((pair >> stage) << (stage + 1)) + position;
|
||||
var odd = even + half;
|
||||
var twiddle = twiddles[position << (log2Size - 1 - stage)];
|
||||
var real = twiddle.X;
|
||||
var imaginary = twiddle.Y * direction;
|
||||
var evenValue = row[even];
|
||||
var oddValue = row[odd];
|
||||
var product = new Float4(
|
||||
oddValue.X * real - oddValue.Y * imaginary,
|
||||
oddValue.X * imaginary + oddValue.Y * real,
|
||||
oddValue.Z * real - oddValue.W * imaginary,
|
||||
oddValue.Z * imaginary + oddValue.W * real);
|
||||
row[even] = evenValue + product;
|
||||
row[odd] = evenValue - product;
|
||||
}
|
||||
|
||||
Hlsl.GroupMemoryBarrierWithGroupSync();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,27 @@ internal static class GpuTileCheck
|
||||
ReadOnlySpan<Int2> samples,
|
||||
ReadOnlySpan<double> kernel,
|
||||
Span<ConvolutionMeasurement> measurements)
|
||||
=> Evaluate(report, in job, samples, kernel, kernel, kernel, false, measurements);
|
||||
|
||||
public static void Evaluate(
|
||||
ReadOnlySpan<uint> report,
|
||||
in GpuTileJob job,
|
||||
ReadOnlySpan<Int2> samples,
|
||||
ReadOnlySpan<double> redKernel,
|
||||
ReadOnlySpan<double> greenKernel,
|
||||
ReadOnlySpan<double> blueKernel,
|
||||
Span<ConvolutionMeasurement> measurements)
|
||||
=> Evaluate(report, in job, samples, redKernel, greenKernel, blueKernel, true, measurements);
|
||||
|
||||
static void Evaluate(
|
||||
ReadOnlySpan<uint> report,
|
||||
in GpuTileJob job,
|
||||
ReadOnlySpan<Int2> samples,
|
||||
ReadOnlySpan<double> redKernel,
|
||||
ReadOnlySpan<double> greenKernel,
|
||||
ReadOnlySpan<double> blueKernel,
|
||||
bool chromatic,
|
||||
Span<ConvolutionMeasurement> measurements)
|
||||
{
|
||||
var plan = job.Plan;
|
||||
var layout = job.Layout;
|
||||
@@ -33,15 +54,17 @@ internal static class GpuTileCheck
|
||||
throw new ArgumentException(null, nameof(report));
|
||||
if (samples.Length != job.SampleCount)
|
||||
throw new ArgumentException(null, nameof(samples));
|
||||
if (kernel.Length != job.KernelArea)
|
||||
throw new ArgumentException(null, nameof(kernel));
|
||||
if (redKernel.Length != job.KernelArea || greenKernel.Length != job.KernelArea || blueKernel.Length != job.KernelArea)
|
||||
throw new ArgumentException(null, nameof(greenKernel));
|
||||
if (measurements.Length < MeasurementCount(job.SampleCount))
|
||||
throw new ArgumentException(null, nameof(measurements));
|
||||
|
||||
var light = job.Light;
|
||||
var floating = !light.IsDefault;
|
||||
var relative = ConvolutionBound.Relative(plan.Size, ConvolutionBound.GpuOperationError);
|
||||
var flushed = ConvolutionBound.FlushedOperations(plan.Size) * ConvolutionBound.SmallestNormal;
|
||||
var relative = chromatic
|
||||
? ConvolutionBound.ChromaticRelative(plan.Size, ConvolutionBound.GpuOperationError)
|
||||
: ConvolutionBound.Relative(plan.Size, ConvolutionBound.GpuOperationError);
|
||||
var flushed = (chromatic ? ConvolutionBound.ChromaticFlushedOperations(plan.Size) : ConvolutionBound.FlushedOperations(plan.Size)) * ConvolutionBound.SmallestNormal;
|
||||
measurements[0] = new ConvolutionMeasurement(NonFiniteName, report[GpuTileLayout.CountOffset], 0d, 0d);
|
||||
|
||||
Span<ulong> totals = stackalloc ulong[4];
|
||||
@@ -89,13 +112,17 @@ internal static class GpuTileCheck
|
||||
}
|
||||
|
||||
Span<double> expected = stackalloc double[4];
|
||||
var referenceError = DirectCorrelation.ErrorBound(kernel.Length);
|
||||
var referenceError = DirectCorrelation.ErrorBound(greenKernel.Length);
|
||||
for (var sample = 0; sample < job.SampleCount; sample++)
|
||||
{
|
||||
var position = samples[sample];
|
||||
var tile = plan.TileAt(plan.RegionX + position.X, plan.RegionY + position.Y);
|
||||
var (redGreen, blueAlpha) = Norms(report, plan, tile, floating);
|
||||
DirectCorrelation.Evaluate(report.Slice(layout.GatheredOffset + sample * job.KernelArea, job.KernelArea), kernel, light, expected);
|
||||
var neighborhood = report.Slice(layout.GatheredOffset + sample * job.KernelArea, job.KernelArea);
|
||||
if (chromatic)
|
||||
DirectCorrelation.Evaluate(neighborhood, redKernel, greenKernel, blueKernel, light, expected);
|
||||
else
|
||||
DirectCorrelation.Evaluate(neighborhood, greenKernel, light, expected);
|
||||
for (var channel = 0; channel < 4; channel++)
|
||||
{
|
||||
var norm = channel < 2 ? redGreen : blueAlpha;
|
||||
|
||||
@@ -28,7 +28,8 @@ internal readonly record struct GpuTileJob(
|
||||
float Gain,
|
||||
int SampleCount,
|
||||
bool Store,
|
||||
LightOptions Light = default)
|
||||
LightOptions Light = default,
|
||||
bool Chromatic = false)
|
||||
{
|
||||
public int KernelArea => (Plan.Radius * 2 + 1) * (Plan.Radius * 2 + 1);
|
||||
|
||||
@@ -40,11 +41,13 @@ internal sealed class GpuTileConvolver : IDisposable
|
||||
public const int MaximumSamples = 16;
|
||||
public const int SampleTilesPerWord = 32;
|
||||
public const int WordsPerSampleEntry = 2;
|
||||
public const int ColumnChromaticGroupThreads = 256;
|
||||
|
||||
readonly GraphicsDevice device;
|
||||
readonly ReadWriteBuffer<Float4> spare;
|
||||
ReadOnlyBuffer<Int2> samples;
|
||||
Int2[] sampleStaging = [];
|
||||
Float2[] chromaticStaging = [];
|
||||
ReadWriteBuffer<Float4>? tiles;
|
||||
ReadOnlyBuffer<Float2>? twiddles;
|
||||
ReadOnlyBuffer<Float2>? spectrum;
|
||||
@@ -52,7 +55,9 @@ internal sealed class GpuTileConvolver : IDisposable
|
||||
ReadBackBuffer<uint>? readBack;
|
||||
ReadWriteBuffer<Float4>? store;
|
||||
int spectrumSize;
|
||||
int spectrumLength;
|
||||
int spectrumRadius = -1;
|
||||
bool spectrumChromatic;
|
||||
|
||||
public GpuTileConvolver(GraphicsDevice device)
|
||||
{
|
||||
@@ -80,24 +85,46 @@ internal sealed class GpuTileConvolver : IDisposable
|
||||
if (kernelSpectrum.Size == 0)
|
||||
throw new ArgumentException(null, nameof(kernelSpectrum));
|
||||
|
||||
spectrumRadius = -1;
|
||||
if (spectrumSize != kernelSpectrum.Size)
|
||||
{
|
||||
twiddles?.Dispose();
|
||||
spectrum?.Dispose();
|
||||
twiddles = null;
|
||||
spectrum = null;
|
||||
spectrumSize = 0;
|
||||
twiddles = device.AllocateReadOnlyBuffer<Float2>(kernelSpectrum.Size / 2);
|
||||
spectrum = device.AllocateReadOnlyBuffer<Float2>(kernelSpectrum.Size * kernelSpectrum.Size);
|
||||
spectrumSize = kernelSpectrum.Size;
|
||||
}
|
||||
|
||||
EnsureSpectrumBuffers(kernelSpectrum.Size, kernelSpectrum.Size * kernelSpectrum.Size, false);
|
||||
twiddles!.CopyFrom(kernelSpectrum.Twiddles);
|
||||
spectrum!.CopyFrom(kernelSpectrum.Spectrum);
|
||||
spectrumRadius = kernelSpectrum.Radius;
|
||||
}
|
||||
|
||||
public void Upload(ChromaticKernelSpectrum kernelSpectrum)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(kernelSpectrum);
|
||||
if (kernelSpectrum.Size == 0)
|
||||
throw new ArgumentException(null, nameof(kernelSpectrum));
|
||||
|
||||
var length = ChromaticKernelSpectrum.ChannelCount * kernelSpectrum.HalfColumns * kernelSpectrum.Size;
|
||||
EnsureSpectrumBuffers(kernelSpectrum.Size, length, true);
|
||||
if (chromaticStaging.Length < length)
|
||||
chromaticStaging = new Float2[length];
|
||||
kernelSpectrum.CopyHalfSpectra(chromaticStaging);
|
||||
twiddles!.CopyFrom(kernelSpectrum.Twiddles);
|
||||
spectrum!.CopyFrom(chromaticStaging.AsSpan(0, length));
|
||||
spectrumRadius = kernelSpectrum.Radius;
|
||||
}
|
||||
|
||||
void EnsureSpectrumBuffers(int size, int length, bool chromatic)
|
||||
{
|
||||
spectrumRadius = -1;
|
||||
if (spectrumSize == size && spectrumLength == length && spectrumChromatic == chromatic)
|
||||
return;
|
||||
|
||||
twiddles?.Dispose();
|
||||
spectrum?.Dispose();
|
||||
twiddles = null;
|
||||
spectrum = null;
|
||||
spectrumSize = 0;
|
||||
twiddles = device.AllocateReadOnlyBuffer<Float2>(size / 2);
|
||||
spectrum = device.AllocateReadOnlyBuffer<Float2>(length);
|
||||
spectrumSize = size;
|
||||
spectrumLength = length;
|
||||
spectrumChromatic = chromatic;
|
||||
}
|
||||
|
||||
public GpuTileJob Prepare(
|
||||
in TilePlan plan,
|
||||
int sourceX,
|
||||
@@ -107,9 +134,10 @@ internal sealed class GpuTileConvolver : IDisposable
|
||||
float gain,
|
||||
ReadOnlySpan<Int2> sampleValues,
|
||||
bool storeValues,
|
||||
LightOptions light = default)
|
||||
LightOptions light = default,
|
||||
bool chromatic = false)
|
||||
{
|
||||
if (plan.Size != spectrumSize || plan.Radius != spectrumRadius)
|
||||
if (plan.Size != spectrumSize || plan.Radius != spectrumRadius || chromatic != spectrumChromatic)
|
||||
throw new InvalidOperationException();
|
||||
light.Validate();
|
||||
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(sourceWidth);
|
||||
@@ -124,7 +152,7 @@ internal sealed class GpuTileConvolver : IDisposable
|
||||
throw new ArgumentOutOfRangeException(nameof(sampleValues));
|
||||
}
|
||||
|
||||
var job = new GpuTileJob(plan, sourceX, sourceY, sourceWidth, sourceHeight, gain, sampleValues.Length, storeValues, light);
|
||||
var job = new GpuTileJob(plan, sourceX, sourceY, sourceWidth, sourceHeight, gain, sampleValues.Length, storeValues, light, chromatic);
|
||||
var tileLength = (long)plan.BatchTiles * plan.TileElements;
|
||||
if (tiles is null || tiles.Length < tileLength)
|
||||
{
|
||||
@@ -242,6 +270,7 @@ internal sealed class GpuTileConvolver : IDisposable
|
||||
{
|
||||
var count = Math.Min(plan.BatchTiles, plan.TileCount - start);
|
||||
var threads = count * plan.TileElements / 2;
|
||||
var columnThreads = job.Chromatic ? count * plan.ChromaticGroupsPerTile * ColumnChromaticGroupThreads : threads;
|
||||
var groupStart = start * plan.GroupsPerTile;
|
||||
if (job.Light.IsDefault)
|
||||
{
|
||||
@@ -250,7 +279,7 @@ internal sealed class GpuTileConvolver : IDisposable
|
||||
plan.RegionX, plan.RegionY, plan.RegionWidth, plan.RegionHeight,
|
||||
job.SourceX, job.SourceY, job.SourceWidth, job.SourceHeight, GpuTileLayout.StatisticsOffset, groupStart));
|
||||
context.Barrier(tiles);
|
||||
context.For(threads, new ColumnShader(tiles, twiddles, spectrum, plan.Log2Size));
|
||||
RecordColumns(in context, tiles, twiddles, spectrum, plan.Log2Size, columnThreads, job.Chromatic);
|
||||
context.Barrier(tiles);
|
||||
context.For(threads, new InverseRowShader(
|
||||
tiles, twiddles, output, report, samples, store, plan.Log2Size, plan.Radius, plan.ValidSize, plan.TilesX, start,
|
||||
@@ -267,7 +296,7 @@ internal sealed class GpuTileConvolver : IDisposable
|
||||
job.SourceX, job.SourceY, job.SourceWidth, job.SourceHeight, GpuTileLayout.StatisticsOffset, groupStart,
|
||||
light.Linear ? 1 : 0, light.Highlights ? 1 : 0, light.Threshold, light.Boost));
|
||||
context.Barrier(tiles);
|
||||
context.For(threads, new ColumnShader(tiles, twiddles, spectrum, plan.Log2Size));
|
||||
RecordColumns(in context, tiles, twiddles, spectrum, plan.Log2Size, columnThreads, job.Chromatic);
|
||||
context.Barrier(tiles);
|
||||
context.For(threads, new InverseRowLightShader(
|
||||
tiles, twiddles, output, report, samples, store, plan.Log2Size, plan.Radius, plan.ValidSize, plan.TilesX, start,
|
||||
@@ -280,6 +309,21 @@ internal sealed class GpuTileConvolver : IDisposable
|
||||
context.Barrier(report);
|
||||
}
|
||||
|
||||
static void RecordColumns(
|
||||
in ComputeContext context,
|
||||
ReadWriteBuffer<Float4> tiles,
|
||||
ReadOnlyBuffer<Float2> twiddles,
|
||||
ReadOnlyBuffer<Float2> spectrum,
|
||||
int log2Size,
|
||||
int threads,
|
||||
bool chromatic)
|
||||
{
|
||||
if (chromatic)
|
||||
context.For(threads, new ColumnChromaticShader(tiles, twiddles, spectrum, log2Size));
|
||||
else
|
||||
context.For(threads, new ColumnShader(tiles, twiddles, spectrum, log2Size));
|
||||
}
|
||||
|
||||
public static void RecordStored(
|
||||
in ComputeContext context,
|
||||
ReadWriteBuffer<Float4> store,
|
||||
@@ -323,6 +367,8 @@ internal sealed class GpuTileConvolver : IDisposable
|
||||
readBack = null;
|
||||
store = null;
|
||||
spectrumSize = 0;
|
||||
spectrumLength = 0;
|
||||
spectrumRadius = -1;
|
||||
spectrumChromatic = false;
|
||||
}
|
||||
}
|
||||
@@ -55,6 +55,10 @@ internal readonly record struct TilePlan
|
||||
|
||||
public int GroupCount => TileCount * GroupsPerTile;
|
||||
|
||||
public int ColumnUnitsPerGroup => GroupElements / Size;
|
||||
|
||||
public int ChromaticGroupsPerTile => (ChromaticKernelSpectrum.HalfColumnsOf(Size) + ColumnUnitsPerGroup - 1) / ColumnUnitsPerGroup;
|
||||
|
||||
public int BatchTiles => (int)Math.Min(TileCount, Math.Max(1L, BatchBudgetBytes / ((long)ElementBytes * TileElements)));
|
||||
|
||||
public static int SelectSize(int radius)
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする