軸に平行な直線のずれを、タップを減らしたフィルターで描いて速くする
・「物理ベース」で軸に平行な短い直線のずれを、ステップの合計を畳み込んだタップのフィルターで描くよう変更-M
このコミットが含まれているのは:
14ファイル変更
+624
-31
検索条件に一致するファイルはありません
@@ -0,0 +1,95 @@
|
||||
using System.Numerics;
|
||||
|
||||
namespace SpectrumColorShift;
|
||||
|
||||
internal static class SpectralFilter
|
||||
{
|
||||
public const int EntriesPerTap = 3;
|
||||
public const int TapQuantum = 4;
|
||||
public const int MaxTaps = 44;
|
||||
public const int TableLength = MaxTaps * EntriesPerTap;
|
||||
|
||||
private const float AxisTolerance = 1e-5f;
|
||||
private const int ShareNumerator = 13;
|
||||
private const int ShareDenominator = 20;
|
||||
private const int Channels = 3;
|
||||
private const int ValuesPerTap = Channels * Channels + 1;
|
||||
|
||||
public static IEnumerable<int> Counts => Enumerable.Range(1, MaxTaps / TapQuantum).Select(index => index * TapQuantum);
|
||||
|
||||
public readonly record struct Layout(int Taps, int Origin, bool Vertical);
|
||||
|
||||
public static Layout Fill(Span<Vector4> table, ReadOnlySpan<Vector4> spectral, int steps, float distance, Vector2 direction)
|
||||
{
|
||||
if (distance == 0f || !TryAxis(direction, out var vertical, out var along))
|
||||
return default;
|
||||
|
||||
steps = Spectrum.ClampSteps(steps);
|
||||
Span<int> floors = stackalloc int[Spectrum.MaxSteps];
|
||||
Span<double> fractions = stackalloc double[Spectrum.MaxSteps];
|
||||
var low = int.MaxValue;
|
||||
var high = int.MinValue;
|
||||
for (var step = 0; step < steps; step++)
|
||||
{
|
||||
var offset = -(double)spectral[step * Spectrum.EntriesPerStep + 1].W * distance * along;
|
||||
floors[step] = (int)Math.Floor(offset);
|
||||
fractions[step] = offset - floors[step];
|
||||
low = Math.Min(low, floors[step]);
|
||||
high = Math.Max(high, floors[step] + 1);
|
||||
}
|
||||
|
||||
var taps = Padded(high - low + 1);
|
||||
if (taps > MaxTaps || taps * ShareDenominator > Spectrum.Padded(steps) * ShareNumerator)
|
||||
return default;
|
||||
|
||||
Span<double> sums = stackalloc double[MaxTaps * ValuesPerTap];
|
||||
sums[..(taps * ValuesPerTap)].Clear();
|
||||
for (var step = 0; step < steps; step++)
|
||||
{
|
||||
var gain = spectral[step * Spectrum.EntriesPerStep];
|
||||
var basis = spectral[step * Spectrum.EntriesPerStep + 1];
|
||||
Add(sums, floors[step] - low, 1d - fractions[step], gain, basis);
|
||||
Add(sums, floors[step] + 1 - low, fractions[step], gain, basis);
|
||||
}
|
||||
|
||||
for (var tap = 0; tap < taps; tap++)
|
||||
{
|
||||
var values = sums.Slice(tap * ValuesPerTap, ValuesPerTap);
|
||||
table[tap * EntriesPerTap] = new Vector4((float)values[0], (float)values[1], (float)values[2], (float)values[Channels * Channels]);
|
||||
table[tap * EntriesPerTap + 1] = new Vector4((float)values[3], (float)values[4], (float)values[5], 0f);
|
||||
table[tap * EntriesPerTap + 2] = new Vector4((float)values[6], (float)values[7], (float)values[8], 0f);
|
||||
}
|
||||
|
||||
table[(taps * EntriesPerTap)..].Clear();
|
||||
|
||||
return new Layout(taps, low, vertical);
|
||||
}
|
||||
|
||||
private static int Padded(int taps) => (taps + TapQuantum - 1) / TapQuantum * TapQuantum;
|
||||
|
||||
private static bool TryAxis(Vector2 direction, out bool vertical, out float along)
|
||||
{
|
||||
vertical = false;
|
||||
along = direction.X;
|
||||
if (MathF.Abs(direction.Y) <= AxisTolerance)
|
||||
return true;
|
||||
|
||||
vertical = true;
|
||||
along = direction.Y;
|
||||
return MathF.Abs(direction.X) <= AxisTolerance;
|
||||
}
|
||||
|
||||
private static void Add(Span<double> sums, int tap, double weight, Vector4 gain, Vector4 basis)
|
||||
{
|
||||
var values = sums.Slice(tap * ValuesPerTap, ValuesPerTap);
|
||||
for (var row = 0; row < Channels; row++)
|
||||
{
|
||||
for (var column = 0; column < Channels; column++)
|
||||
values[row * Channels + column] += weight * Component(gain, row) * Component(basis, column);
|
||||
}
|
||||
|
||||
values[Channels * Channels] += weight * gain.W;
|
||||
}
|
||||
|
||||
private static float Component(Vector4 vector, int index) => index switch { 0 => vector.X, 1 => vector.Y, _ => vector.Z };
|
||||
}
|
||||
@@ -80,6 +80,8 @@ internal sealed class SpectrumColorShiftCustomEffect : D2D1CustomShaderEffectBas
|
||||
private int _steps = Spectrum.DefaultSteps;
|
||||
private bool _tableStale = true;
|
||||
private bool _spectralStale = true;
|
||||
private bool _filterStale = true;
|
||||
private SpectralFilter.Layout _filter;
|
||||
private bool _physical;
|
||||
private Guid _shader = GUID_PixelShader;
|
||||
|
||||
@@ -90,6 +92,7 @@ internal sealed class SpectrumColorShiftCustomEffect : D2D1CustomShaderEffectBas
|
||||
set
|
||||
{
|
||||
_cb.Distance = Sanitize(value, -MaxInputPixel, MaxInputPixel);
|
||||
_filterStale = true;
|
||||
UpdateConstants();
|
||||
}
|
||||
}
|
||||
@@ -102,6 +105,7 @@ internal sealed class SpectrumColorShiftCustomEffect : D2D1CustomShaderEffectBas
|
||||
{
|
||||
_mode = value is (int)ShiftMode.Radial or (int)ShiftMode.Rotational ? (ShiftMode)value : ShiftMode.Linear;
|
||||
_cb.Mode = (int)_mode;
|
||||
_filterStale = true;
|
||||
UpdateConstants();
|
||||
}
|
||||
}
|
||||
@@ -114,6 +118,7 @@ internal sealed class SpectrumColorShiftCustomEffect : D2D1CustomShaderEffectBas
|
||||
{
|
||||
var radians = float.IsFinite(value) ? value * MathF.PI / 180f : 0f;
|
||||
_cb.Direction = new Vector2(MathF.Cos(radians), MathF.Sin(radians));
|
||||
_filterStale = true;
|
||||
UpdateConstants();
|
||||
}
|
||||
}
|
||||
@@ -204,6 +209,18 @@ internal sealed class SpectrumColorShiftCustomEffect : D2D1CustomShaderEffectBas
|
||||
}
|
||||
}
|
||||
|
||||
internal int FilterTaps
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!_physical)
|
||||
return 0;
|
||||
|
||||
RefreshPhysical();
|
||||
return _filter.Taps;
|
||||
}
|
||||
}
|
||||
|
||||
public EffectImpl() : base(StepShaders.DefaultBytes)
|
||||
{
|
||||
_cb.Direction = Vector2.UnitX;
|
||||
@@ -228,7 +245,10 @@ internal sealed class SpectrumColorShiftCustomEffect : D2D1CustomShaderEffectBas
|
||||
{
|
||||
_cb.SpreadScale = ShiftGeometry.Spread(_mode, _cb.Distance, _cb.Radius);
|
||||
|
||||
var shader = StepShaders.Select(_physical, _mode, _steps);
|
||||
if (_physical)
|
||||
RefreshPhysical();
|
||||
|
||||
var shader = _physical && _filter.Taps > 0 ? StepShaders.SelectFilter(_filter.Taps) : StepShaders.Select(_physical, _mode, _steps);
|
||||
if (shader != _shader)
|
||||
{
|
||||
drawInformation.SetPixelShader(shader, PixelOptions.None);
|
||||
@@ -258,14 +278,24 @@ internal sealed class SpectrumColorShiftCustomEffect : D2D1CustomShaderEffectBas
|
||||
drawInformation!.SetPixelShaderConstantBuffer(_cb);
|
||||
}
|
||||
|
||||
private void SendPhysicalConstants()
|
||||
private void RefreshPhysical()
|
||||
{
|
||||
if (_spectralStale)
|
||||
{
|
||||
Spectrum.FillSpectral(_physicalCb.Samples, _steps, _referenceWavelength, _dispersion);
|
||||
_spectralStale = false;
|
||||
_filterStale = true;
|
||||
}
|
||||
|
||||
if (_filterStale)
|
||||
{
|
||||
_filter = _mode == ShiftMode.Linear ? SpectralFilter.Fill(_physicalCb.Taps, _physicalCb.Samples, _steps, _cb.Distance, _cb.Direction) : default;
|
||||
_filterStale = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void SendPhysicalConstants()
|
||||
{
|
||||
_physicalCb.InputBounds = _cb.InputBounds;
|
||||
_physicalCb.Direction = _cb.Direction;
|
||||
_physicalCb.Center = _cb.Center;
|
||||
@@ -275,6 +305,8 @@ internal sealed class SpectrumColorShiftCustomEffect : D2D1CustomShaderEffectBas
|
||||
_physicalCb.Mode = _cb.Mode;
|
||||
_physicalCb.Steps = Spectrum.Padded(_steps);
|
||||
_physicalCb.SpreadScale = _cb.SpreadScale;
|
||||
_physicalCb.FilterOrigin = _filter.Origin;
|
||||
_physicalCb.FilterVertical = _filter.Vertical ? 1 : 0;
|
||||
drawInformation!.SetPixelShaderConstantBuffer(_physicalCb);
|
||||
}
|
||||
|
||||
@@ -338,6 +370,12 @@ internal sealed class SpectrumColorShiftCustomEffect : D2D1CustomShaderEffectBas
|
||||
private Vector4 _element;
|
||||
}
|
||||
|
||||
[InlineArray(SpectralFilter.TableLength)]
|
||||
private struct FilterTapTable
|
||||
{
|
||||
private Vector4 _element;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private struct PhysicalConstantBuffer
|
||||
{
|
||||
@@ -350,9 +388,10 @@ internal sealed class SpectrumColorShiftCustomEffect : D2D1CustomShaderEffectBas
|
||||
public int Mode;
|
||||
public int Steps;
|
||||
public float SpreadScale;
|
||||
public float Pad1;
|
||||
public float Pad2;
|
||||
public float FilterOrigin;
|
||||
public int FilterVertical;
|
||||
public SpectralSampleTable Samples;
|
||||
public FilterTapTable Taps;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
|
||||
@@ -7,50 +7,67 @@ internal static class StepShaders
|
||||
{
|
||||
private const string LegacyName = "SpectrumColorShift";
|
||||
private const string PhysicalName = "SpectrumColorShiftPhysical";
|
||||
private const string FilterName = "SpectrumColorShiftPhysicalFilter";
|
||||
|
||||
private static readonly Variants Legacy = new(LegacyName);
|
||||
private static readonly Variants Physical = new(PhysicalName);
|
||||
private static readonly ShiftMode[] AllModes = Enum.GetValues<ShiftMode>();
|
||||
|
||||
public static IEnumerable<int> Counts => Enumerable.Range(0, Variants.CountLength).Select(Variants.CountAt);
|
||||
private static readonly Variants Legacy = new(LegacyName, AllModes, Spectrum.MinSteps, Spectrum.MaxSteps, Spectrum.StepQuantum);
|
||||
private static readonly Variants Physical = new(PhysicalName, AllModes, Spectrum.MinSteps, Spectrum.MaxSteps, Spectrum.StepQuantum);
|
||||
private static readonly Variants Filter = new(FilterName, [ShiftMode.Linear], SpectralFilter.TapQuantum, SpectralFilter.MaxTaps, SpectralFilter.TapQuantum);
|
||||
|
||||
public static IEnumerable<ShiftMode> Modes => Variants.Modes;
|
||||
public static IEnumerable<int> Counts => Legacy.Counts;
|
||||
|
||||
public static IEnumerable<int> FilterCounts => Filter.Counts;
|
||||
|
||||
public static IEnumerable<ShiftMode> Modes => AllModes;
|
||||
|
||||
public static byte[] DefaultBytes => Legacy.Bytes(ShiftMode.Linear, Spectrum.DefaultSteps);
|
||||
|
||||
public static string ResourceName(bool physical, ShiftMode mode, int count) => (physical ? Physical : Legacy).ResourceName(mode, count);
|
||||
|
||||
public static string FilterResourceName(int taps) => Filter.ResourceName(ShiftMode.Linear, taps);
|
||||
|
||||
public static Guid Select(bool physical, ShiftMode mode, int steps) => (physical ? Physical : Legacy).Id(mode, steps);
|
||||
|
||||
public static Guid SelectFilter(int taps) => Filter.Id(ShiftMode.Linear, taps);
|
||||
|
||||
public static void Load(ID2D1EffectContext effectContext)
|
||||
{
|
||||
Legacy.Load(effectContext);
|
||||
Physical.Load(effectContext);
|
||||
Filter.Load(effectContext);
|
||||
}
|
||||
|
||||
private sealed class Variants
|
||||
{
|
||||
public static ShiftMode[] Modes { get; } = Enum.GetValues<ShiftMode>();
|
||||
|
||||
public static int CountLength { get; } = (Spectrum.MaxSteps - Spectrum.MinSteps) / Spectrum.StepQuantum + 1;
|
||||
|
||||
private readonly string _name;
|
||||
private readonly ShiftMode[] _modes;
|
||||
private readonly int _minimum;
|
||||
private readonly int _maximum;
|
||||
private readonly int _quantum;
|
||||
private readonly int _length;
|
||||
private readonly Guid[] _ids;
|
||||
private readonly Lazy<byte[]>[] _bytes;
|
||||
|
||||
public Variants(string name)
|
||||
public Variants(string name, ShiftMode[] modes, int minimum, int maximum, int quantum)
|
||||
{
|
||||
_name = name;
|
||||
_ids = [.. Enumerable.Range(0, Modes.Length * CountLength).Select(_ => Guid.NewGuid())];
|
||||
_bytes = [.. Enumerable.Range(0, Modes.Length * CountLength).Select(index => new Lazy<byte[]>(() => Read(index)))];
|
||||
_modes = modes;
|
||||
_minimum = minimum;
|
||||
_maximum = maximum;
|
||||
_quantum = quantum;
|
||||
_length = (maximum - minimum) / quantum + 1;
|
||||
_ids = [.. Enumerable.Range(0, modes.Length * _length).Select(_ => Guid.NewGuid())];
|
||||
_bytes = [.. Enumerable.Range(0, modes.Length * _length).Select(index => new Lazy<byte[]>(() => Read(index)))];
|
||||
}
|
||||
|
||||
public static int CountAt(int index) => Spectrum.MinSteps + index * Spectrum.StepQuantum;
|
||||
public IEnumerable<int> Counts => Enumerable.Range(0, _length).Select(CountAt);
|
||||
|
||||
public string ResourceName(ShiftMode mode, int count) => $"{_name}{mode}{count}";
|
||||
|
||||
public Guid Id(ShiftMode mode, int steps) => _ids[IndexOf(mode, steps)];
|
||||
public Guid Id(ShiftMode mode, int count) => _ids[IndexOf(mode, count)];
|
||||
|
||||
public byte[] Bytes(ShiftMode mode, int steps) => _bytes[IndexOf(mode, steps)].Value;
|
||||
public byte[] Bytes(ShiftMode mode, int count) => _bytes[IndexOf(mode, count)].Value;
|
||||
|
||||
public void Load(ID2D1EffectContext effectContext)
|
||||
{
|
||||
@@ -61,10 +78,12 @@ internal static class StepShaders
|
||||
}
|
||||
}
|
||||
|
||||
private static int IndexOf(ShiftMode mode, int steps)
|
||||
=> Array.IndexOf(Modes, mode) * CountLength + (Spectrum.Padded(steps) - Spectrum.MinSteps) / Spectrum.StepQuantum;
|
||||
private int CountAt(int index) => _minimum + index * _quantum;
|
||||
|
||||
private int IndexOf(ShiftMode mode, int count)
|
||||
=> Array.IndexOf(_modes, mode) * _length + (Math.Clamp(count, _minimum, _maximum) - _minimum + _quantum - 1) / _quantum;
|
||||
|
||||
private byte[] Read(int index)
|
||||
=> PackResourceReader.ReadAllBytes(ShaderResourceUri.Get(ResourceName(Modes[index / CountLength], CountAt(index % CountLength))));
|
||||
=> PackResourceReader.ReadAllBytes(ShaderResourceUri.Get(ResourceName(_modes[index / _length], CountAt(index % _length))));
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする