シェーダーの系統と分光表の1ステップを型にして、切り上げの重複をなくす
このコミットが含まれているのは:
11ファイル変更
+275
-248
検索条件に一致するファイルはありません
@@ -0,0 +1,6 @@
|
||||
namespace SpectrumColorShift;
|
||||
|
||||
internal static class Quantum
|
||||
{
|
||||
public static int RoundUp(int value, int quantum) => (value + quantum - 1) / quantum * quantum;
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using Vortice.Direct2D1;
|
||||
using YukkuriMovieMaker.Commons;
|
||||
|
||||
namespace SpectrumColorShift;
|
||||
|
||||
internal sealed class ShaderFamily
|
||||
{
|
||||
private readonly string _name;
|
||||
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 ShaderFamily(string name, IReadOnlyList<ShiftMode> modes, int minimum, int maximum, int quantum)
|
||||
{
|
||||
_name = name;
|
||||
Modes = modes;
|
||||
_minimum = minimum;
|
||||
_maximum = maximum;
|
||||
_quantum = quantum;
|
||||
_length = (maximum - minimum) / quantum + 1;
|
||||
_ids = [.. Enumerable.Range(0, modes.Count * _length).Select(_ => Guid.NewGuid())];
|
||||
_bytes = [.. Enumerable.Range(0, modes.Count * _length).Select(index => new Lazy<byte[]>(() => Read(index)))];
|
||||
}
|
||||
|
||||
public IReadOnlyList<ShiftMode> Modes { get; }
|
||||
|
||||
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 count) => _ids[IndexOf(mode, count)];
|
||||
|
||||
public byte[] Bytes(ShiftMode mode, int count) => _bytes[IndexOf(mode, count)].Value;
|
||||
|
||||
public void LoadAll(ID2D1EffectContext effectContext)
|
||||
{
|
||||
for (var index = 0; index < _ids.Length; index++)
|
||||
{
|
||||
var bytes = _bytes[index].Value;
|
||||
effectContext.LoadPixelShader(_ids[index], bytes, bytes.Length);
|
||||
}
|
||||
}
|
||||
|
||||
private int CountAt(int index) => _minimum + index * _quantum;
|
||||
|
||||
private int IndexOf(ShiftMode mode, int count)
|
||||
{
|
||||
var offset = Quantum.RoundUp(Math.Clamp(count, _minimum, _maximum) - _minimum, _quantum) / _quantum;
|
||||
|
||||
return IndexOfMode(mode) * _length + offset;
|
||||
}
|
||||
|
||||
private int IndexOfMode(ShiftMode mode)
|
||||
{
|
||||
for (var index = 0; index < Modes.Count; index++)
|
||||
{
|
||||
if (Modes[index] == mode)
|
||||
return index;
|
||||
}
|
||||
|
||||
throw new ArgumentOutOfRangeException(nameof(mode), mode, null);
|
||||
}
|
||||
|
||||
private byte[] Read(int index)
|
||||
=> PackResourceReader.ReadAllBytes(ShaderResourceUri.Get(ResourceName(Modes[index / _length], CountAt(index % _length))));
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using Vortice.Direct2D1;
|
||||
|
||||
namespace SpectrumColorShift;
|
||||
|
||||
internal static class ShaderVariants
|
||||
{
|
||||
public static ShaderFamily Legacy { get; } = new("SpectrumColorShift", Enum.GetValues<ShiftMode>(), Spectrum.MinSteps, Spectrum.MaxSteps, Spectrum.StepQuantum);
|
||||
|
||||
public static ShaderFamily Physical { get; } = new("SpectrumColorShiftPhysical", Enum.GetValues<ShiftMode>(), Spectrum.MinSteps, Spectrum.MaxSteps, Spectrum.StepQuantum);
|
||||
|
||||
public static ShaderFamily Filter { get; } = new("SpectrumColorShiftPhysicalFilter", [ShiftMode.Linear], SpectralFilter.TapQuantum, SpectralFilter.MaxTaps, SpectralFilter.TapQuantum);
|
||||
|
||||
public static byte[] DefaultBytes => Legacy.Bytes(ShiftMode.Linear, Spectrum.DefaultSteps);
|
||||
|
||||
public static ShaderFamily Stepped(bool physical) => physical ? Physical : Legacy;
|
||||
|
||||
public static void LoadAll(ID2D1EffectContext effectContext)
|
||||
{
|
||||
Legacy.LoadAll(effectContext);
|
||||
Physical.LoadAll(effectContext);
|
||||
Filter.LoadAll(effectContext);
|
||||
}
|
||||
}
|
||||
@@ -8,65 +8,60 @@ internal static class SpectralFilter
|
||||
public const int TapQuantum = 4;
|
||||
public const int MaxTaps = 44;
|
||||
public const int TableLength = MaxTaps * EntriesPerTap;
|
||||
public const float MaxTapShare = 0.65f;
|
||||
|
||||
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);
|
||||
|
||||
private readonly record struct Placement(int Floor, double Fraction);
|
||||
|
||||
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];
|
||||
Span<Placement> placements = stackalloc Placement[Spectrum.MaxSteps];
|
||||
var layout = Plan(spectral, steps, distance * along, vertical, placements);
|
||||
if (layout.Taps == 0)
|
||||
return default;
|
||||
|
||||
Span<double> sums = stackalloc double[layout.Taps * ValuesPerTap];
|
||||
sums.Clear();
|
||||
for (var step = 0; step < steps; step++)
|
||||
{
|
||||
var spectralStep = SpectralStep.Read(spectral, step);
|
||||
var placement = placements[step];
|
||||
Add(sums, placement.Floor - layout.Origin, 1d - placement.Fraction, spectralStep);
|
||||
Add(sums, placement.Floor + 1 - layout.Origin, placement.Fraction, spectralStep);
|
||||
}
|
||||
|
||||
Write(table, sums, layout.Taps);
|
||||
|
||||
return layout;
|
||||
}
|
||||
|
||||
private static Layout Plan(ReadOnlySpan<Vector4> spectral, int steps, float signedDistance, bool vertical, Span<Placement> placements)
|
||||
{
|
||||
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 offset = -(double)SpectralStep.Read(spectral, step).Displacement * signedDistance;
|
||||
var floor = (int)Math.Floor(offset);
|
||||
placements[step] = new Placement(floor, offset - floor);
|
||||
low = Math.Min(low, floor);
|
||||
high = Math.Max(high, floor + 1);
|
||||
}
|
||||
|
||||
var taps = Padded(high - low + 1);
|
||||
if (taps > MaxTaps || taps * ShareDenominator > Spectrum.Padded(steps) * ShareNumerator)
|
||||
return default;
|
||||
var taps = Quantum.RoundUp(high - low + 1, TapQuantum);
|
||||
|
||||
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);
|
||||
return taps <= MaxTaps && taps <= Spectrum.Padded(steps) * MaxTapShare ? new Layout(taps, low, vertical) : default;
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -79,17 +74,28 @@ internal static class SpectralFilter
|
||||
return MathF.Abs(direction.X) <= AxisTolerance;
|
||||
}
|
||||
|
||||
private static void Add(Span<double> sums, int tap, double weight, Vector4 gain, Vector4 basis)
|
||||
private static void Add(Span<double> sums, int tap, double weight, SpectralStep step)
|
||||
{
|
||||
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[row * Channels + column] += weight * step.Gain[row] * step.Basis[column];
|
||||
}
|
||||
|
||||
values[Channels * Channels] += weight * gain.W;
|
||||
values[Channels * Channels] += weight * step.AlphaGain;
|
||||
}
|
||||
|
||||
private static float Component(Vector4 vector, int index) => index switch { 0 => vector.X, 1 => vector.Y, _ => vector.Z };
|
||||
private static void Write(Span<Vector4> table, ReadOnlySpan<double> sums, int taps)
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using System.Numerics;
|
||||
|
||||
namespace SpectrumColorShift;
|
||||
|
||||
internal readonly record struct SpectralStep(Vector3 Gain, float AlphaGain, Vector3 Basis, float Displacement)
|
||||
{
|
||||
public static SpectralStep Read(ReadOnlySpan<Vector4> table, int step)
|
||||
{
|
||||
var gain = table[step * Spectrum.EntriesPerStep];
|
||||
var basis = table[step * Spectrum.EntriesPerStep + 1];
|
||||
|
||||
return new SpectralStep(new Vector3(gain.X, gain.Y, gain.Z), gain.W, new Vector3(basis.X, basis.Y, basis.Z), basis.W);
|
||||
}
|
||||
|
||||
public void Write(Span<Vector4> table, int step)
|
||||
{
|
||||
table[step * Spectrum.EntriesPerStep] = new Vector4(Gain, AlphaGain);
|
||||
table[step * Spectrum.EntriesPerStep + 1] = new Vector4(Basis, Displacement);
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ internal static class Spectrum
|
||||
|
||||
public static int ClampSteps(int steps) => Math.Clamp(steps, MinSteps, MaxSteps);
|
||||
|
||||
public static int Padded(int steps) => (ClampSteps(steps) + StepQuantum - 1) / StepQuantum * StepQuantum;
|
||||
public static int Padded(int steps) => Quantum.RoundUp(ClampSteps(steps), StepQuantum);
|
||||
|
||||
public static float ClampReference(float wavelength)
|
||||
=> float.IsNaN(wavelength) ? DefaultReferenceWavelength : Math.Clamp(wavelength, ShortestWavelength, LongestWavelength);
|
||||
@@ -125,8 +125,7 @@ internal static class Spectrum
|
||||
(float)(inverse[0] * x + inverse[1] * y + inverse[2] * z),
|
||||
(float)(inverse[3] * x + inverse[4] * y + inverse[5] * z),
|
||||
(float)(inverse[6] * x + inverse[7] * y + inverse[8] * z));
|
||||
table[index * EntriesPerStep] = new Vector4(gain, (float)(y / luminance));
|
||||
table[index * EntriesPerStep + 1] = new Vector4(basis, (float)(Displacement(law, wavelength, reference) / norm));
|
||||
new SpectralStep(gain, (float)(y / luminance), basis, (float)(Displacement(law, wavelength, reference) / norm)).Write(table, index);
|
||||
}
|
||||
|
||||
table[(steps * EntriesPerStep)..].Clear();
|
||||
|
||||
@@ -221,7 +221,7 @@ internal sealed class SpectrumColorShiftCustomEffect : D2D1CustomShaderEffectBas
|
||||
}
|
||||
}
|
||||
|
||||
public EffectImpl() : base(StepShaders.DefaultBytes)
|
||||
public EffectImpl() : base(ShaderVariants.DefaultBytes)
|
||||
{
|
||||
_cb.Direction = Vector2.UnitX;
|
||||
_cb.Radius = 1f;
|
||||
@@ -233,7 +233,7 @@ internal sealed class SpectrumColorShiftCustomEffect : D2D1CustomShaderEffectBas
|
||||
public override void Initialize(ID2D1EffectContext effectContext, ID2D1TransformGraph transformGraph)
|
||||
{
|
||||
base.Initialize(effectContext, transformGraph);
|
||||
StepShaders.Load(effectContext);
|
||||
ShaderVariants.LoadAll(effectContext);
|
||||
}
|
||||
|
||||
protected override void UpdateConstants()
|
||||
@@ -248,7 +248,7 @@ internal sealed class SpectrumColorShiftCustomEffect : D2D1CustomShaderEffectBas
|
||||
if (_physical)
|
||||
RefreshPhysical();
|
||||
|
||||
var shader = _physical && _filter.Taps > 0 ? StepShaders.SelectFilter(_filter.Taps) : StepShaders.Select(_physical, _mode, _steps);
|
||||
var shader = _physical && _filter.Taps > 0 ? ShaderVariants.Filter.Id(ShiftMode.Linear, _filter.Taps) : ShaderVariants.Stepped(_physical).Id(_mode, _steps);
|
||||
if (shader != _shader)
|
||||
{
|
||||
drawInformation.SetPixelShader(shader, PixelOptions.None);
|
||||
|
||||
@@ -1,89 +0,0 @@
|
||||
using Vortice.Direct2D1;
|
||||
using YukkuriMovieMaker.Commons;
|
||||
|
||||
namespace SpectrumColorShift;
|
||||
|
||||
internal static class StepShaders
|
||||
{
|
||||
private const string LegacyName = "SpectrumColorShift";
|
||||
private const string PhysicalName = "SpectrumColorShiftPhysical";
|
||||
private const string FilterName = "SpectrumColorShiftPhysicalFilter";
|
||||
|
||||
private static readonly ShiftMode[] AllModes = Enum.GetValues<ShiftMode>();
|
||||
|
||||
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<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
|
||||
{
|
||||
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, ShiftMode[] modes, int minimum, int maximum, int quantum)
|
||||
{
|
||||
_name = name;
|
||||
_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 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 count) => _ids[IndexOf(mode, count)];
|
||||
|
||||
public byte[] Bytes(ShiftMode mode, int count) => _bytes[IndexOf(mode, count)].Value;
|
||||
|
||||
public void Load(ID2D1EffectContext effectContext)
|
||||
{
|
||||
for (var index = 0; index < _ids.Length; index++)
|
||||
{
|
||||
var bytes = _bytes[index].Value;
|
||||
effectContext.LoadPixelShader(_ids[index], bytes, bytes.Length);
|
||||
}
|
||||
}
|
||||
|
||||
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 / _length], CountAt(index % _length))));
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする