色ごとの点像をパイプラインで畳み込む
このコミットが含まれているのは:
5ファイル変更
+469
-12
検索条件に一致するファイルはありません
@@ -0,0 +1,266 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeWeave;
|
||||
using SpectralConvolution;
|
||||
using WaveOptics.Effects;
|
||||
using WaveOptics.Rendering;
|
||||
|
||||
namespace WaveOptics.Tests;
|
||||
|
||||
[Collection("Direct3D12")]
|
||||
public sealed class WaveOpticsChromaticPipelineTests
|
||||
{
|
||||
const int Width = 160;
|
||||
const int Height = 144;
|
||||
const int White = unchecked((int)0xFFFFFFFF);
|
||||
const int Opaque = unchecked((int)0xFFC0A080);
|
||||
|
||||
static WaveOpticsPipeline.Parameters Parameters(
|
||||
WaveOpticsColorMode mode,
|
||||
int kernelRadius = 15,
|
||||
float defocus = 0.5f,
|
||||
float wavelength = 550f,
|
||||
float fNumber = 8f,
|
||||
float pixelPitch = 4f,
|
||||
WaveOpticsQuality quality = WaveOpticsQuality.Standard,
|
||||
LightOptions light = default,
|
||||
float coma = 0.3f)
|
||||
=> new(1f, new WaveOpticsPipeline.PsfParameters(
|
||||
quality, kernelRadius, wavelength, fNumber, pixelPitch, WaveOpticsApertureShape.Circular, 6, 0f, 0f, defocus, 0f, 0f, coma, 0f, 0f, mode), light);
|
||||
|
||||
static int[] Square(int left, int top, int size, int color)
|
||||
{
|
||||
var pixels = new int[Width * Height];
|
||||
for (var y = top; y < top + size; y++)
|
||||
{
|
||||
for (var x = left; x < left + size; x++)
|
||||
pixels[y * Width + x] = color;
|
||||
}
|
||||
|
||||
return pixels;
|
||||
}
|
||||
|
||||
static int[] Pattern()
|
||||
{
|
||||
var pixels = new int[Width * Height];
|
||||
for (var y = 50; y < 90; y++)
|
||||
{
|
||||
for (var x = 40; x < 110; x++)
|
||||
pixels[y * Width + x] = (x + y) % 7 == 0 ? 0 : Opaque;
|
||||
}
|
||||
|
||||
return pixels;
|
||||
}
|
||||
|
||||
static int[] RenderCpu(WaveOpticsCpuPipeline pipeline, int[] source, in WaveOpticsPipeline.Parameters parameters)
|
||||
{
|
||||
var destination = new int[source.Length];
|
||||
pipeline.Process(source, destination, Width, Height, in parameters);
|
||||
return destination;
|
||||
}
|
||||
|
||||
static int[] RenderGpu(WaveOpticsPipeline pipeline, int[] source, in WaveOpticsPipeline.Parameters parameters)
|
||||
{
|
||||
var destination = new int[source.Length];
|
||||
pipeline.Process(source, destination, Width, Height, in parameters);
|
||||
return destination;
|
||||
}
|
||||
|
||||
static byte Channel(int pixel, int shift) => (byte)(pixel >> shift);
|
||||
|
||||
public static TheoryData<WaveOpticsColorMode, int, WaveOpticsQuality> Modes => new()
|
||||
{
|
||||
{ WaveOpticsColorMode.Primaries, 15, WaveOpticsQuality.Standard },
|
||||
{ WaveOpticsColorMode.Primaries, 40, WaveOpticsQuality.High },
|
||||
{ WaveOpticsColorMode.Broadband, 15, WaveOpticsQuality.Draft },
|
||||
{ WaveOpticsColorMode.Broadband, 30, WaveOpticsQuality.Standard },
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(Modes))]
|
||||
public void TheCpuAndTheGpuDrawTheSameImageWithinTwoLevels(WaveOpticsColorMode mode, int kernelRadius, WaveOpticsQuality quality)
|
||||
{
|
||||
using var cpu = new WaveOpticsCpuPipeline(3);
|
||||
using var gpu = WaveOpticsPipeline.TryCreate();
|
||||
Assert.NotNull(gpu);
|
||||
var source = Pattern();
|
||||
var parameters = Parameters(mode, kernelRadius, quality: quality);
|
||||
|
||||
var expected = RenderGpu(gpu, source, in parameters);
|
||||
var actual = RenderCpu(cpu, source, in parameters);
|
||||
|
||||
var expectedBytes = MemoryMarshal.AsBytes(expected.AsSpan());
|
||||
var actualBytes = MemoryMarshal.AsBytes(actual.AsSpan());
|
||||
for (var index = 0; index < expectedBytes.Length; index++)
|
||||
Assert.InRange(actualBytes[index] - expectedBytes[index], -2, 2);
|
||||
Assert.Contains(actualBytes.ToArray(), value => value > 0);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(WaveOpticsColorMode.Primaries, 15)]
|
||||
[InlineData(WaveOpticsColorMode.Primaries, 40)]
|
||||
[InlineData(WaveOpticsColorMode.Broadband, 30)]
|
||||
public void AHealthyChromaticConvolutionPassesEveryCheck(WaveOpticsColorMode mode, int kernelRadius)
|
||||
{
|
||||
using var pipeline = WaveOpticsPipeline.TryCreate();
|
||||
Assert.NotNull(pipeline);
|
||||
var device = GraphicsDevice.GetDefault();
|
||||
using var source = device.AllocateReadWriteTexture2D<Bgra32, Float4>(Width, Height);
|
||||
source.CopyFrom(MemoryMarshal.Cast<int, Bgra32>(Pattern().AsSpan()));
|
||||
using var output = device.AllocateReadWriteTexture2D<Bgra32, Float4>(Width, Height);
|
||||
var parameters = Parameters(mode, kernelRadius);
|
||||
var measurements = new ConvolutionMeasurement[WaveOpticsPipeline.MeasurementCount];
|
||||
|
||||
pipeline.Simulate(source, Width, Height, 0, 0, Width, Height, in parameters);
|
||||
Assert.True(pipeline.TryGetVisibleBounds(Width, Height, in parameters, out var rect));
|
||||
var count = pipeline.RenderVisible(source, output, rect, in parameters, measurements);
|
||||
|
||||
Assert.Equal(WaveOpticsPipeline.MeasurementCount, count);
|
||||
Assert.All(measurements, measurement => Assert.True(measurement.Passes, $"{measurement}"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ACorruptedSpectrumOfTheChromaticModeIsFoundByTheCheck()
|
||||
{
|
||||
using var pipeline = WaveOpticsPipeline.TryCreate();
|
||||
Assert.NotNull(pipeline);
|
||||
var device = GraphicsDevice.GetDefault();
|
||||
using var source = device.AllocateReadWriteTexture2D<Bgra32, Float4>(Width, Height);
|
||||
source.CopyFrom(MemoryMarshal.Cast<int, Bgra32>(Pattern().AsSpan()));
|
||||
using var output = device.AllocateReadWriteTexture2D<Bgra32, Float4>(Width, Height);
|
||||
var parameters = Parameters(WaveOpticsColorMode.Primaries, 40);
|
||||
var measurements = new ConvolutionMeasurement[WaveOpticsPipeline.MeasurementCount];
|
||||
pipeline.SpectrumTamper = values =>
|
||||
{
|
||||
var copy = (Float2[])values.Clone();
|
||||
var blueStart = 2 * copy.Length / 3;
|
||||
for (var index = blueStart; index < copy.Length; index++)
|
||||
copy[index] = new Float2(copy[index].X * 0.5f, copy[index].Y * 0.5f);
|
||||
return copy;
|
||||
};
|
||||
|
||||
pipeline.Simulate(source, Width, Height, 0, 0, Width, Height, in parameters);
|
||||
Assert.True(pipeline.TryGetVisibleBounds(Width, Height, in parameters, out var rect));
|
||||
pipeline.RenderVisible(source, output, rect, in parameters, measurements);
|
||||
|
||||
Assert.Contains(measurements, measurement => !measurement.Passes && measurement.Name.EndsWith("blue", StringComparison.Ordinal));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public void ARedChannelSpreadsFurtherThanABlueChannelAroundAWhiteSquare(bool onTheGpu)
|
||||
{
|
||||
var source = Square(60, 50, 40, White);
|
||||
var parameters = Parameters(WaveOpticsColorMode.Primaries, 30, defocus: 0f, fNumber: 16f, pixelPitch: 2f, coma: 0f);
|
||||
int[] result;
|
||||
if (onTheGpu)
|
||||
{
|
||||
using var gpu = WaveOpticsPipeline.TryCreate();
|
||||
Assert.NotNull(gpu);
|
||||
result = RenderGpu(gpu, source, in parameters);
|
||||
}
|
||||
else
|
||||
{
|
||||
using var cpu = new WaveOpticsCpuPipeline(2);
|
||||
result = RenderCpu(cpu, source, in parameters);
|
||||
}
|
||||
|
||||
var red = 0d;
|
||||
var green = 0d;
|
||||
var blue = 0d;
|
||||
for (var y = 38; y < 112; y++)
|
||||
{
|
||||
for (var x = 48; x < 112; x++)
|
||||
{
|
||||
if (x >= 58 && x < 102 && y >= 48 && y < 92)
|
||||
continue;
|
||||
var pixel = result[y * Width + x];
|
||||
red += Channel(pixel, 16);
|
||||
green += Channel(pixel, 8);
|
||||
blue += Channel(pixel, 0);
|
||||
}
|
||||
}
|
||||
|
||||
Assert.True(red > green && green > blue, $"{red} {green} {blue}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AMonochromeFrameAfterAChromaticFrameIsTheMonochromeFrameOfAFreshPipeline()
|
||||
{
|
||||
var source = Pattern();
|
||||
var mono = Parameters(WaveOpticsColorMode.Monochrome);
|
||||
var chromatic = Parameters(WaveOpticsColorMode.Primaries);
|
||||
using var cpu = new WaveOpticsCpuPipeline(2);
|
||||
using var gpu = WaveOpticsPipeline.TryCreate();
|
||||
Assert.NotNull(gpu);
|
||||
using var freshCpu = new WaveOpticsCpuPipeline(2);
|
||||
using var freshGpu = WaveOpticsPipeline.TryCreate();
|
||||
Assert.NotNull(freshGpu);
|
||||
|
||||
_ = RenderCpu(cpu, source, in mono);
|
||||
_ = RenderCpu(cpu, source, in chromatic);
|
||||
_ = RenderGpu(gpu, source, in mono);
|
||||
_ = RenderGpu(gpu, source, in chromatic);
|
||||
|
||||
Assert.Equal(RenderCpu(freshCpu, source, in mono), RenderCpu(cpu, source, in mono));
|
||||
Assert.Equal(RenderGpu(freshGpu, source, in mono), RenderGpu(gpu, source, in mono));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(WaveOpticsColorMode.Primaries)]
|
||||
[InlineData(WaveOpticsColorMode.Broadband)]
|
||||
public void TheWavelengthDoesNotChangeAChromaticFrame(WaveOpticsColorMode mode)
|
||||
{
|
||||
var source = Pattern();
|
||||
using var cpu = new WaveOpticsCpuPipeline(2);
|
||||
var first = Parameters(mode, wavelength: 450f);
|
||||
var second = Parameters(mode, wavelength: 700f);
|
||||
|
||||
Assert.Equal(RenderCpu(cpu, source, in first), RenderCpu(cpu, source, in second));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AChromaticFrameIsLinearLightAware()
|
||||
{
|
||||
var source = Square(60, 50, 4, White);
|
||||
var light = new LightOptions(true, false, 0.5f, 20f);
|
||||
using var cpu = new WaveOpticsCpuPipeline(2);
|
||||
using var gpu = WaveOpticsPipeline.TryCreate();
|
||||
Assert.NotNull(gpu);
|
||||
var parameters = Parameters(WaveOpticsColorMode.Primaries, 30, defocus: 0f, fNumber: 16f, pixelPitch: 2f, light: light);
|
||||
|
||||
var expected = RenderGpu(gpu, source, in parameters);
|
||||
var actual = RenderCpu(cpu, source, in parameters);
|
||||
|
||||
var expectedBytes = MemoryMarshal.AsBytes(expected.AsSpan());
|
||||
var actualBytes = MemoryMarshal.AsBytes(actual.AsSpan());
|
||||
for (var index = 0; index < expectedBytes.Length; index++)
|
||||
Assert.InRange(actualBytes[index] - expectedBytes[index], -3, 3);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(WaveOpticsColorMode.Primaries)]
|
||||
[InlineData(WaveOpticsColorMode.Broadband)]
|
||||
public void AWarmChromaticFrameAllocatesNothingOnTheCallingThread(WaveOpticsColorMode mode)
|
||||
{
|
||||
using var pipeline = new WaveOpticsCpuPipeline(3);
|
||||
var sources = new[] { MemoryMarshal.AsBytes(Square(30, 28, 36, Opaque).AsSpan()).ToArray(), MemoryMarshal.AsBytes(Square(31, 28, 36, Opaque).AsSpan()).ToArray() };
|
||||
var parameters = Parameters(mode);
|
||||
for (var warmUp = 0; warmUp < 4; warmUp++)
|
||||
{
|
||||
pipeline.Simulate(sources[warmUp % 2], Width, Height, 0, 0, Width, Height, in parameters);
|
||||
pipeline.TryGetVisibleBounds(Width, Height, in parameters, out var warmRect);
|
||||
pipeline.RenderVisible(warmRect, in parameters);
|
||||
}
|
||||
|
||||
var before = GC.GetAllocatedBytesForCurrentThread();
|
||||
for (var frame = 0; frame < 8; frame++)
|
||||
{
|
||||
pipeline.Simulate(sources[frame % 2], Width, Height, 0, 0, Width, Height, in parameters);
|
||||
pipeline.TryGetVisibleBounds(Width, Height, in parameters, out var rect);
|
||||
pipeline.RenderVisible(rect, in parameters);
|
||||
}
|
||||
|
||||
Assert.Equal(0L, GC.GetAllocatedBytesForCurrentThread() - before);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
using WaveOptics.Effects;
|
||||
using WaveOptics.Rendering;
|
||||
|
||||
namespace WaveOptics.Tests;
|
||||
|
||||
public sealed class WaveOpticsKernelChromaticTests
|
||||
{
|
||||
static WaveOpticsPipeline.PsfParameters Psf(WaveOpticsColorMode mode, float wavelength = 550f, int radius = 15, float defocus = 0f, WaveOpticsApertureShape shape = WaveOpticsApertureShape.Circular, int blades = 6)
|
||||
=> new(WaveOpticsQuality.Standard, radius, wavelength, 8f, 4f, shape, blades, 0f, 0f, defocus, 0f, 0f, 0f, 0f, 0f, mode);
|
||||
|
||||
[Fact]
|
||||
public void AMonochromeKernelIsNotChromatic()
|
||||
{
|
||||
var kernel = new WaveOpticsKernel();
|
||||
|
||||
Assert.True(kernel.TryUpdate(Psf(WaveOpticsColorMode.Monochrome)));
|
||||
|
||||
Assert.False(kernel.IsChromatic);
|
||||
Assert.Equal(128, kernel.Size);
|
||||
Assert.Equal(15, kernel.Radius);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(WaveOpticsColorMode.Primaries)]
|
||||
[InlineData(WaveOpticsColorMode.Broadband)]
|
||||
public void AChromaticKernelHoldsASpectrumForEveryChannel(WaveOpticsColorMode mode)
|
||||
{
|
||||
var kernel = new WaveOpticsKernel();
|
||||
|
||||
Assert.True(kernel.TryUpdate(Psf(mode, radius: 30, defocus: 0.5f)));
|
||||
|
||||
Assert.True(kernel.IsChromatic);
|
||||
Assert.True(kernel.IsValid);
|
||||
Assert.Equal(512, kernel.Size);
|
||||
Assert.Equal(30, kernel.Radius);
|
||||
Assert.Equal(kernel.ChromaticSpectrum.Size, kernel.Size);
|
||||
Assert.NotEqual(kernel.ChromaticSpectrum.Red.Kernel.ToArray(), kernel.ChromaticSpectrum.Blue.Kernel.ToArray());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(WaveOpticsColorMode.Primaries)]
|
||||
[InlineData(WaveOpticsColorMode.Broadband)]
|
||||
public void TheWavelengthIsNotPartOfTheKeyInAChromaticMode(WaveOpticsColorMode mode)
|
||||
{
|
||||
var kernel = new WaveOpticsKernel();
|
||||
Assert.True(kernel.TryUpdate(Psf(mode, 450f)));
|
||||
var version = kernel.Version;
|
||||
|
||||
Assert.True(kernel.TryUpdate(Psf(mode, 700f)));
|
||||
|
||||
Assert.Equal(version, kernel.Version);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TheWavelengthIsPartOfTheKeyInTheMonochromeMode()
|
||||
{
|
||||
var kernel = new WaveOpticsKernel();
|
||||
Assert.True(kernel.TryUpdate(Psf(WaveOpticsColorMode.Monochrome, 450f)));
|
||||
var version = kernel.Version;
|
||||
|
||||
Assert.True(kernel.TryUpdate(Psf(WaveOpticsColorMode.Monochrome, 700f)));
|
||||
|
||||
Assert.NotEqual(version, kernel.Version);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ChangingTheModeRecomputesTheKernel()
|
||||
{
|
||||
var kernel = new WaveOpticsKernel();
|
||||
Assert.True(kernel.TryUpdate(Psf(WaveOpticsColorMode.Primaries)));
|
||||
var version = kernel.Version;
|
||||
|
||||
Assert.True(kernel.TryUpdate(Psf(WaveOpticsColorMode.Broadband)));
|
||||
var second = kernel.Version;
|
||||
Assert.True(kernel.TryUpdate(Psf(WaveOpticsColorMode.Monochrome)));
|
||||
|
||||
Assert.NotEqual(version, second);
|
||||
Assert.NotEqual(second, kernel.Version);
|
||||
Assert.False(kernel.IsChromatic);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ABladeCountOfACircularApertureIsNotPartOfTheKeyInAChromaticMode()
|
||||
{
|
||||
var kernel = new WaveOpticsKernel();
|
||||
Assert.True(kernel.TryUpdate(Psf(WaveOpticsColorMode.Primaries, blades: 5)));
|
||||
var version = kernel.Version;
|
||||
|
||||
Assert.True(kernel.TryUpdate(Psf(WaveOpticsColorMode.Primaries, blades: 9)));
|
||||
|
||||
Assert.Equal(version, kernel.Version);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TheKeyOfAChromaticKernelIgnoresTheWavelengthOnly()
|
||||
{
|
||||
var first = WaveOpticsKernel.KeyOf(Psf(WaveOpticsColorMode.Primaries, 450f, defocus: 1f));
|
||||
var second = WaveOpticsKernel.KeyOf(Psf(WaveOpticsColorMode.Primaries, 700f, defocus: 1f));
|
||||
var third = WaveOpticsKernel.KeyOf(Psf(WaveOpticsColorMode.Primaries, 700f, defocus: 2f));
|
||||
|
||||
Assert.Equal(first, second);
|
||||
Assert.NotEqual(second, third);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AKernelThatCannotBeSampledIsInvalidAndStaysSoUntilTheKeyChanges()
|
||||
{
|
||||
var kernel = new WaveOpticsKernel();
|
||||
var closed = Psf(WaveOpticsColorMode.Primaries, shape: WaveOpticsApertureShape.RegularPolygon, blades: 4) with { Obstruction = 0.95f, Quality = WaveOpticsQuality.Draft, KernelRadius = 1 };
|
||||
Assert.True(kernel.TryUpdate(Psf(WaveOpticsColorMode.Primaries)));
|
||||
|
||||
var updated = kernel.TryUpdate(closed);
|
||||
|
||||
Assert.Equal(updated, kernel.IsValid);
|
||||
Assert.True(kernel.TryUpdate(Psf(WaveOpticsColorMode.Primaries)));
|
||||
Assert.True(kernel.IsValid);
|
||||
}
|
||||
}
|
||||
@@ -46,8 +46,8 @@ internal sealed class WaveOpticsCpuPipeline : IDisposable
|
||||
if (_output.Length < length)
|
||||
_output = new byte[length];
|
||||
MemoryMarshal.AsBytes(source[..(width * height)]).CopyTo(_packed);
|
||||
var plan = TilePlan.Create(_kernel.Spectrum.Size, _kernel.Spectrum.Radius, 0, 0, width, height);
|
||||
_convolver.Convolve(_packed, 0, 0, width, height, _kernel.Spectrum, plan, _output, parameters.Gain, null, parameters.Light);
|
||||
var plan = TilePlan.Create(_kernel.Size, _kernel.Radius, 0, 0, width, height);
|
||||
Convolve(_packed, 0, 0, width, height, in plan, parameters.Gain, null, parameters.Light);
|
||||
_output.AsSpan(0, length).CopyTo(MemoryMarshal.AsBytes(destination[..(width * height)]));
|
||||
}
|
||||
|
||||
@@ -119,11 +119,19 @@ internal sealed class WaveOpticsCpuPipeline : IDisposable
|
||||
store = _store;
|
||||
}
|
||||
|
||||
var plan = TilePlan.Create(_kernel.Spectrum.Size, _kernel.Spectrum.Radius, rect.X, rect.Y, rect.Width, rect.Height);
|
||||
_convolver.Convolve(_source, _sourceRect.X, _sourceRect.Y, _sourceRect.Width, _sourceRect.Height, _kernel.Spectrum, plan, _output, parameters.Gain, store, parameters.Light);
|
||||
var plan = TilePlan.Create(_kernel.Size, _kernel.Radius, rect.X, rect.Y, rect.Width, rect.Height);
|
||||
Convolve(_source, _sourceRect.X, _sourceRect.Y, _sourceRect.Width, _sourceRect.Height, in plan, parameters.Gain, store, parameters.Light);
|
||||
return _output.AsSpan(0, length);
|
||||
}
|
||||
|
||||
private void Convolve(byte[] source, int sourceX, int sourceY, int sourceWidth, int sourceHeight, in TilePlan plan, float gain, float[]? store, LightOptions light)
|
||||
{
|
||||
if (_kernel.IsChromatic)
|
||||
_convolver.Convolve(source, sourceX, sourceY, sourceWidth, sourceHeight, _kernel.ChromaticSpectrum, in plan, _output, gain, store, light);
|
||||
else
|
||||
_convolver.Convolve(source, sourceX, sourceY, sourceWidth, sourceHeight, _kernel.Spectrum, in plan, _output, gain, store, light);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_convolver.Dispose();
|
||||
|
||||
@@ -8,14 +8,26 @@ namespace WaveOptics.Rendering;
|
||||
internal sealed class WaveOpticsKernel
|
||||
{
|
||||
private readonly double[] _values = new double[WaveOpticsSettings.MaximumKernelSize * WaveOpticsSettings.MaximumKernelSize];
|
||||
private readonly double[] _redValues = new double[WaveOpticsSettings.MaximumKernelSize * WaveOpticsSettings.MaximumKernelSize];
|
||||
private readonly double[] _blueValues = new double[WaveOpticsSettings.MaximumKernelSize * WaveOpticsSettings.MaximumKernelSize];
|
||||
private readonly FraunhoferKernelSampler _sampler = new();
|
||||
private readonly ChromaticKernelSampler _chromaticSampler = new();
|
||||
private readonly KernelSpectrum _spectrum = new();
|
||||
private readonly ChromaticKernelSpectrum _chromaticSpectrum = new();
|
||||
private WaveOpticsPipeline.PsfParameters? _key;
|
||||
private bool _isValid;
|
||||
private int _version;
|
||||
|
||||
public KernelSpectrum Spectrum => _spectrum;
|
||||
|
||||
public ChromaticKernelSpectrum ChromaticSpectrum => _chromaticSpectrum;
|
||||
|
||||
public bool IsChromatic => _key is { ColorMode: not WaveOpticsColorMode.Monochrome };
|
||||
|
||||
public int Size => IsChromatic ? _chromaticSpectrum.Size : _spectrum.Size;
|
||||
|
||||
public int Radius => IsChromatic ? _chromaticSpectrum.Radius : _spectrum.Radius;
|
||||
|
||||
public bool IsValid => _isValid;
|
||||
|
||||
public int Version => _version;
|
||||
@@ -37,11 +49,12 @@ internal sealed class WaveOpticsKernel
|
||||
comaHorizontalWaves: psf.ComaHorizontal,
|
||||
comaVerticalWaves: psf.ComaVertical,
|
||||
sphericalWaves: psf.Spherical);
|
||||
var chromatic = psf.ColorMode != WaveOpticsColorMode.Monochrome;
|
||||
var specification = new PsfSpecification(
|
||||
pupilGridSize,
|
||||
WaveOpticsSettings.GetPupilDiameterSamples(pupilGridSize),
|
||||
WaveOpticsSettings.GetKernelSize(psf.KernelRadius),
|
||||
psf.Wavelength,
|
||||
chromatic ? SpectralPlan.ReferenceWavelength : psf.Wavelength,
|
||||
psf.FNumber,
|
||||
psf.PixelPitch,
|
||||
psf.ApertureShape == WaveOpticsApertureShape.Circular ? ApertureShape.Circular : ApertureShape.RegularPolygon,
|
||||
@@ -51,6 +64,9 @@ internal sealed class WaveOpticsKernel
|
||||
aberration);
|
||||
var size = specification.KernelSize;
|
||||
var values = _values.AsSpan(0, size * size);
|
||||
if (chromatic)
|
||||
return TryUpdateChromatic(in specification, psf, values);
|
||||
|
||||
if (!_sampler.TrySample(in specification, values))
|
||||
return false;
|
||||
|
||||
@@ -65,6 +81,32 @@ internal sealed class WaveOpticsKernel
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool TryUpdateChromatic(in PsfSpecification specification, in WaveOpticsPipeline.PsfParameters psf, Span<double> green)
|
||||
{
|
||||
var size = green.Length;
|
||||
var red = _redValues.AsSpan(0, size);
|
||||
var blue = _blueValues.AsSpan(0, size);
|
||||
if (!_chromaticSampler.TrySample(in specification, psf.ColorMode, psf.Quality, red, green, blue))
|
||||
return false;
|
||||
if (!HasEnergy(red) || !HasEnergy(green) || !HasEnergy(blue))
|
||||
return false;
|
||||
|
||||
_chromaticSpectrum.Update(red, green, blue, psf.KernelRadius, TilePlan.SelectSize(psf.KernelRadius));
|
||||
_isValid = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool HasEnergy(ReadOnlySpan<double> values)
|
||||
{
|
||||
var sum = 0d;
|
||||
foreach (var value in values)
|
||||
sum += value;
|
||||
return double.IsFinite(sum) && sum > 0d;
|
||||
}
|
||||
|
||||
public static WaveOpticsPipeline.PsfParameters KeyOf(in WaveOpticsPipeline.PsfParameters psf)
|
||||
=> psf.ApertureShape == WaveOpticsApertureShape.Circular ? psf with { BladeCount = 0, BladeRotation = 0f } : psf;
|
||||
{
|
||||
var key = psf.ApertureShape == WaveOpticsApertureShape.Circular ? psf with { BladeCount = 0, BladeRotation = 0f } : psf;
|
||||
return key.ColorMode == WaveOpticsColorMode.Monochrome ? key : key with { Wavelength = 0f };
|
||||
}
|
||||
}
|
||||
@@ -259,7 +259,15 @@ internal sealed class WaveOpticsPipeline : IDisposable
|
||||
return 0;
|
||||
|
||||
var count = GpuTileCheck.MeasurementCount(job.SampleCount);
|
||||
GpuTileCheck.Evaluate(_convolver.ReadReport(in job), in job, samples, _kernel.Spectrum.Kernel, measurements[..count]);
|
||||
if (_kernel.IsChromatic)
|
||||
{
|
||||
var spectrum = _kernel.ChromaticSpectrum;
|
||||
GpuTileCheck.Evaluate(_convolver.ReadReport(in job), in job, samples, spectrum.Red.Kernel, spectrum.Green.Kernel, spectrum.Blue.Kernel, measurements[..count]);
|
||||
}
|
||||
else
|
||||
{
|
||||
GpuTileCheck.Evaluate(_convolver.ReadReport(in job), in job, samples, _kernel.Spectrum.Kernel, measurements[..count]);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
@@ -278,9 +286,12 @@ internal sealed class WaveOpticsPipeline : IDisposable
|
||||
{
|
||||
if (_uploadedKernelVersion != _kernel.Version)
|
||||
{
|
||||
_convolver.Upload(_kernel.Spectrum);
|
||||
if (_kernel.IsChromatic)
|
||||
_convolver.Upload(_kernel.ChromaticSpectrum);
|
||||
else
|
||||
_convolver.Upload(_kernel.Spectrum);
|
||||
if (_spectrumTamper is { } tamper)
|
||||
_convolver.Spectrum.CopyFrom(tamper(_kernel.Spectrum.Spectrum.ToArray()));
|
||||
_convolver.Spectrum.CopyFrom(tamper(UploadedSpectrum()));
|
||||
_uploadedKernelVersion = _kernel.Version;
|
||||
}
|
||||
|
||||
@@ -292,14 +303,25 @@ internal sealed class WaveOpticsPipeline : IDisposable
|
||||
samples[index] = new Int2(picks[index].X, picks[index].Y);
|
||||
}
|
||||
|
||||
var plan = TilePlan.Create(_kernel.Spectrum.Size, _kernel.Spectrum.Radius, rect.X, rect.Y, rect.Width, rect.Height);
|
||||
var job = _convolver.Prepare(plan, _sourceRect.X, _sourceRect.Y, _sourceRect.Width, _sourceRect.Height, gain, samples, store, light);
|
||||
var plan = TilePlan.Create(_kernel.Size, _kernel.Radius, rect.X, rect.Y, rect.Width, rect.Height);
|
||||
var job = _convolver.Prepare(plan, _sourceRect.X, _sourceRect.Y, _sourceRect.Width, _sourceRect.Height, gain, samples, store, light, _kernel.IsChromatic);
|
||||
if (store)
|
||||
_storedJob = job;
|
||||
|
||||
return job;
|
||||
}
|
||||
|
||||
private Float2[] UploadedSpectrum()
|
||||
{
|
||||
if (!_kernel.IsChromatic)
|
||||
return _kernel.Spectrum.Spectrum.ToArray();
|
||||
|
||||
var spectra = _kernel.ChromaticSpectrum;
|
||||
var values = new Float2[ChromaticKernelSpectrum.ChannelCount * spectra.HalfColumns * spectra.Size];
|
||||
spectra.CopyHalfSpectra(values);
|
||||
return values;
|
||||
}
|
||||
|
||||
private void ResetRendering()
|
||||
{
|
||||
if (_tracker.Reset())
|
||||
@@ -396,7 +418,8 @@ internal sealed class WaveOpticsPipeline : IDisposable
|
||||
float AstigmatismOblique,
|
||||
float ComaHorizontal,
|
||||
float ComaVertical,
|
||||
float Spherical);
|
||||
float Spherical,
|
||||
WaveOpticsColorMode ColorMode = WaveOpticsColorMode.Monochrome);
|
||||
|
||||
internal readonly record struct Parameters(float Gain, PsfParameters Psf, LightOptions Light = default);
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする