diff --git a/SpectrumColorShift.Shader/Physical.hlsli b/SpectrumColorShift.Shader/Physical.hlsli index 8c785b0..3adc5d1 100644 --- a/SpectrumColorShift.Shader/Physical.hlsli +++ b/SpectrumColorShift.Shader/Physical.hlsli @@ -4,8 +4,8 @@ #include "Srgb.hlsli" -Texture2D LinearTexture : register(t1); -SamplerState LinearSampler : register(s1); +Texture2D LightTexture : register(t1); +SamplerState LightSampler : register(s1); static const int MaxSteps = 64; static const int TableSize = 128; @@ -69,7 +69,7 @@ float4 SampleCovered(float2 uv, float2 pixelStep, float2 scenePosition, float2 t float2 minimum = inputBounds.xy + 0.5; float2 maximum = inputBounds.zw - 0.5; float2 clamped = clamp(target, minimum, maximum); - float4 color = LinearTexture.SampleLevel(LinearSampler, uv + (clamped - scenePosition) * pixelStep, 0); + float4 color = LightTexture.SampleLevel(LightSampler, uv + (clamped - scenePosition) * pixelStep, 0); float2 coverage = saturate(min(target - inputBounds.xy, inputBounds.zw - target) + 0.5); return color * (coverage.x * coverage.y); } diff --git a/SpectrumColorShift.Shader/SpectrumColorShiftPhysical.hlsl b/SpectrumColorShift.Shader/SpectrumColorShiftPhysical.hlsl index 36b63b0..7c16961 100644 --- a/SpectrumColorShift.Shader/SpectrumColorShiftPhysical.hlsl +++ b/SpectrumColorShift.Shader/SpectrumColorShiftPhysical.hlsl @@ -9,7 +9,7 @@ void AccumulateRadial(int index, float2 uv, float2 offsetStep, float rate, inout float4 gain = samples[index * 2]; float4 basis = samples[index * 2 + 1]; float scale = clamp(basis.w * rate, -MaxRadialShift, MaxRadialShift); - Accumulate(gain, basis.rgb, LinearTexture.SampleLevel(LinearSampler, uv - offsetStep * scale, 0), color, alpha); + Accumulate(gain, basis.rgb, LightTexture.SampleLevel(LightSampler, uv - offsetStep * scale, 0), color, alpha); } void AccumulateRotational(int index, float2 uv, float2 offset, float2 pixelStep, float rate, inout float3 color, inout float alpha) @@ -20,14 +20,14 @@ void AccumulateRotational(int index, float2 uv, float2 offset, float2 pixelStep, float cosine; sincos(basis.w * rate, sine, cosine); float2 delta = float2((cosine - 1.0) * offset.x + sine * offset.y, (cosine - 1.0) * offset.y - sine * offset.x); - Accumulate(gain, basis.rgb, LinearTexture.SampleLevel(LinearSampler, uv + delta * pixelStep, 0), color, alpha); + Accumulate(gain, basis.rgb, LightTexture.SampleLevel(LightSampler, uv + delta * pixelStep, 0), color, alpha); } void AccumulateLinear(int index, float2 uv, float2 stride, inout float3 color, inout float alpha) { float4 gain = samples[index * 2]; float4 basis = samples[index * 2 + 1]; - Accumulate(gain, basis.rgb, LinearTexture.SampleLevel(LinearSampler, uv - basis.w * stride, 0), color, alpha); + Accumulate(gain, basis.rgb, LightTexture.SampleLevel(LightSampler, uv - basis.w * stride, 0), color, alpha); } float4 GatherInside(float2 uv, float2 pixelStep, float2 scenePosition) diff --git a/SpectrumColorShift.Shader/SpectrumColorShiftPhysicalFilter.hlsl b/SpectrumColorShift.Shader/SpectrumColorShiftPhysicalFilter.hlsl index 6648f7d..21a4497 100644 --- a/SpectrumColorShift.Shader/SpectrumColorShiftPhysicalFilter.hlsl +++ b/SpectrumColorShift.Shader/SpectrumColorShiftPhysicalFilter.hlsl @@ -6,7 +6,7 @@ void AccumulateTap(int tap, float2 position, inout float3 color, inout float alpha) { - float4 shifted = LinearTexture.SampleLevel(LinearSampler, position, 0); + float4 shifted = LightTexture.SampleLevel(LightSampler, position, 0); float4 red = taps[tap * 3]; float4 green = taps[tap * 3 + 1]; float4 blue = taps[tap * 3 + 2]; diff --git a/SpectrumColorShift.Tests/FilterRenderingTests.cs b/SpectrumColorShift.Tests/FilterRenderingTests.cs index 081e075..c358944 100644 --- a/SpectrumColorShift.Tests/FilterRenderingTests.cs +++ b/SpectrumColorShift.Tests/FilterRenderingTests.cs @@ -35,7 +35,7 @@ public sealed class FilterRenderingTests using var context = devices.CreateContext(); using var source = new SourceImage(context, 64, 40, Gradient); using var effect = new SpectrumColorShiftCustomEffect(context); - effect.SetInput(0, source.Bitmap, true); + effect.SetSource(source.Bitmap, true); using var output = effect.Output; var settings = new ShiftSettings(ShiftMode.Linear, distance, angle, default, 1f, law, 550f, steps, true); settings.Apply(effect); @@ -60,7 +60,7 @@ public sealed class FilterRenderingTests using var context = devices.CreateContext(); using var source = new SourceImage(context, 64, 40, Gradient); using var effect = new SpectrumColorShiftCustomEffect(context); - effect.SetInput(0, source.Bitmap, true); + effect.SetSource(source.Bitmap, true); using var output = effect.Output; foreach (var (angle, distance) in new[] { (0f, 8f), (30f, 8f), (0f, 8f), (0f, 40f), (90f, 8f), (0f, 0f), (0f, 8f) }) diff --git a/SpectrumColorShift.Tests/SpectralRenderingTests.cs b/SpectrumColorShift.Tests/SpectralRenderingTests.cs index 0f6bbe9..43b93fb 100644 --- a/SpectrumColorShift.Tests/SpectralRenderingTests.cs +++ b/SpectrumColorShift.Tests/SpectralRenderingTests.cs @@ -48,7 +48,7 @@ public sealed class SpectralRenderingTests static Rendering Render(IGraphicsDevicesAndContext devices, SourceImage source, ShiftSettings settings) { using var effect = new SpectrumColorShiftCustomEffect(devices); - effect.SetInput(0, source.Bitmap, true); + effect.SetSource(source.Bitmap, true); settings.Apply(effect); using var output = effect.Output; @@ -602,7 +602,7 @@ public sealed class SpectralRenderingTests var inPlace = Render(context, source, settings); using var effect = new SpectrumColorShiftCustomEffect(context); - effect.SetInput(0, movedOutput, true); + effect.SetSource(movedOutput, true); settings.Apply(effect); using var output = effect.Output; var travelled = Rendering.Capture(context, output); @@ -698,7 +698,7 @@ public sealed class SpectralRenderingTests using var bitmap = context.DeviceContext.CreateBitmap(new SizeI(96, 96), nint.Zero, stride, new BitmapProperties1(SourceImage.PixelFormat, SourceImage.Dpi, SourceImage.Dpi, BitmapOptions.None)); bitmap.CopyFromMemory(pixels, stride); using var effect = new SpectrumColorShiftCustomEffect(context); - effect.SetInput(0, bitmap, true); + effect.SetSource(bitmap, true); Settings(Mode: mode, Distance: 12f).Apply(effect); using var output = effect.Output; @@ -735,7 +735,7 @@ public sealed class SpectralRenderingTests var expected = Render(context, source, legacy); using var effect = new SpectrumColorShiftCustomEffect(context); - effect.SetInput(0, source.Bitmap, true); + effect.SetSource(source.Bitmap, true); legacy.Apply(effect); effect.Physical = 1; using var output = effect.Output; diff --git a/SpectrumColorShift.Tests/SpectralShaderContractTests.cs b/SpectrumColorShift.Tests/SpectralShaderContractTests.cs index 962e648..ba5a353 100644 --- a/SpectrumColorShift.Tests/SpectralShaderContractTests.cs +++ b/SpectrumColorShift.Tests/SpectralShaderContractTests.cs @@ -33,14 +33,12 @@ public sealed partial class SpectralShaderContractTests static string Here([CallerFilePath] string path = "") => path; - static int LinearInput { get; } = (int)typeof(SpectrumColorShiftCustomEffect).GetField("LinearInput", BindingFlags.NonPublic | BindingFlags.Static)!.GetRawConstantValue()!; - static Type ConstantBuffer => typeof(EffectImpl).GetNestedType("PhysicalConstantBuffer", BindingFlags.NonPublic)!; [GeneratedRegex(@"static const (?:int|float) (?\w+) = (?[0-9.]+);")] private static partial Regex ConstantPattern(); - [GeneratedRegex(@"Texture2D\s+LinearTexture\s*:\s*register\(t(?\d+)\)")] + [GeneratedRegex(@"Texture2D\s+LightTexture\s*:\s*register\(t(?\d+)\)")] private static partial Regex TexturePattern(); [GeneratedRegex(@"float4\s+\w+\s*:\s*TEXCOORD(?\d+)")] @@ -65,7 +63,7 @@ public sealed partial class SpectralShaderContractTests [Fact] public void TheShaderReadsTheTextureOfTheLinearizedInput() { - Assert.Equal(LinearInput, int.Parse(TexturePattern().Match(Source).Groups["register"].Value, CultureInfo.InvariantCulture)); + Assert.Equal(SpectrumColorShiftCustomEffect.LightInput, int.Parse(TexturePattern().Match(Source).Groups["register"].Value, CultureInfo.InvariantCulture)); } [Fact] @@ -73,7 +71,7 @@ public sealed partial class SpectralShaderContractTests { var declared = CoordinatePattern().Matches(Source).Select(match => int.Parse(match.Groups["index"].Value, CultureInfo.InvariantCulture)); - Assert.Equal(Enumerable.Range(0, LinearInput + 1), declared.Order()); + Assert.Equal(Enumerable.Range(0, SpectrumColorShiftCustomEffect.LightInput + 1), declared.Order()); } [Theory] diff --git a/SpectrumColorShift.Tests/SpectrumColorShiftCustomEffectTests.cs b/SpectrumColorShift.Tests/SpectrumColorShiftCustomEffectTests.cs index 187dbb1..96fcff4 100644 --- a/SpectrumColorShift.Tests/SpectrumColorShiftCustomEffectTests.cs +++ b/SpectrumColorShift.Tests/SpectrumColorShiftCustomEffectTests.cs @@ -10,14 +10,14 @@ public sealed class SpectrumColorShiftCustomEffectTests { const float MaxInputPixel = 4096f; - static float Read(SpectrumColorShiftCustomEffect effect, PropertyIndex index) => effect.GetFloatValue((int)index); + static float Read(SpectrumColorShiftCustomEffect effect, PropertyIndex index) => effect.ReadFloat(index); - static int ReadInt(SpectrumColorShiftCustomEffect effect, PropertyIndex index) => effect.GetIntValue((int)index); + static int ReadInt(SpectrumColorShiftCustomEffect effect, PropertyIndex index) => effect.ReadInt(index); static (float Left, float Top, float Right, float Bottom) Bounds(IGraphicsDevicesAndContext devices, SourceImage source, Action configure) { using var effect = new SpectrumColorShiftCustomEffect(devices); - effect.SetInput(0, source.Bitmap, true); + effect.SetSource(source.Bitmap, true); configure(effect); using var output = effect.Output; var bounds = devices.DeviceContext.GetImageLocalBounds(output); @@ -318,7 +318,7 @@ public sealed class SpectrumColorShiftCustomEffectTests using var context = devices.CreateContext(); using var source = SourceImage.Solid(context, 40, 30, Bgra.Opaque(10, 20, 30)); using var effect = new SpectrumColorShiftCustomEffect(context); - effect.SetInput(0, source.Bitmap, true); + effect.SetSource(source.Bitmap, true); effect.Mode = (int)ShiftMode.Rotational; effect.Distance = MaxInputPixel; using var output = effect.Output; @@ -388,7 +388,7 @@ public sealed class SpectrumColorShiftCustomEffectTests crop.SetInput(0, source.Bitmap, true); using var cropped = crop.Output; using var effect = new SpectrumColorShiftCustomEffect(context); - effect.SetInput(0, cropped, true); + effect.SetSource(cropped, true); effect.Mode = (int)mode; effect.Distance = 3f; using var output = effect.Output; diff --git a/SpectrumColorShift.Tests/SpectrumColorShiftEffectProcessorTests.cs b/SpectrumColorShift.Tests/SpectrumColorShiftEffectProcessorTests.cs index f479e28..1e2a370 100644 --- a/SpectrumColorShift.Tests/SpectrumColorShiftEffectProcessorTests.cs +++ b/SpectrumColorShift.Tests/SpectrumColorShiftEffectProcessorTests.cs @@ -228,7 +228,7 @@ public sealed class SpectrumColorShiftEffectProcessorTests using var processor = effect.CreateVideoEffect(context); processor.SetInput(source.Bitmap); using var direct = new SpectrumColorShiftCustomEffect(context); - direct.SetInput(0, source.Bitmap, true); + direct.SetSource(source.Bitmap, true); direct.Distance = 14.5f; direct.Mode = (int)ShiftMode.Rotational; direct.Angle = 33f; diff --git a/SpectrumColorShift.Tests/SpectrumColorShiftRenderingTests.cs b/SpectrumColorShift.Tests/SpectrumColorShiftRenderingTests.cs index 14e6c9d..941b1a6 100644 --- a/SpectrumColorShift.Tests/SpectrumColorShiftRenderingTests.cs +++ b/SpectrumColorShift.Tests/SpectrumColorShiftRenderingTests.cs @@ -37,7 +37,7 @@ public sealed class SpectrumColorShiftRenderingTests static Rendering Render(IGraphicsDevicesAndContext devices, SourceImage source, ShiftSettings settings) { using var effect = new SpectrumColorShiftCustomEffect(devices); - effect.SetInput(0, source.Bitmap, true); + effect.SetSource(source.Bitmap, true); settings.Apply(effect); using var output = effect.Output; @@ -476,7 +476,7 @@ public sealed class SpectrumColorShiftRenderingTests var inPlace = Render(context, source, settings); using var effect = new SpectrumColorShiftCustomEffect(context); - effect.SetInput(0, movedOutput, true); + effect.SetSource(movedOutput, true); settings.Apply(effect); using var output = effect.Output; var travelled = Rendering.Capture(context, output); @@ -572,7 +572,7 @@ public sealed class SpectrumColorShiftRenderingTests using var bitmap = context.DeviceContext.CreateBitmap(new SizeI(96, 96), nint.Zero, stride, new BitmapProperties1(SourceImage.PixelFormat, SourceImage.Dpi, SourceImage.Dpi, BitmapOptions.None)); bitmap.CopyFromMemory(pixels, stride); using var effect = new SpectrumColorShiftCustomEffect(context); - effect.SetInput(0, bitmap, true); + effect.SetSource(bitmap, true); new ShiftSettings(Mode: mode, Distance: 12f).Apply(effect); using var output = effect.Output; diff --git a/SpectrumColorShift.Tests/StepCountRenderingTests.cs b/SpectrumColorShift.Tests/StepCountRenderingTests.cs index 70fdcae..b3f9e3a 100644 --- a/SpectrumColorShift.Tests/StepCountRenderingTests.cs +++ b/SpectrumColorShift.Tests/StepCountRenderingTests.cs @@ -24,7 +24,7 @@ public sealed class StepCountRenderingTests using var context = devices.CreateContext(); using var source = new SourceImage(context, 48, 32, Gradient); using var effect = new SpectrumColorShiftCustomEffect(context); - effect.SetInput(0, source.Bitmap, true); + effect.SetSource(source.Bitmap, true); using var output = effect.Output; foreach (var steps in Enumerable.Range(Spectrum.MinSteps, Spectrum.MaxSteps - Spectrum.MinSteps + 1)) diff --git a/SpectrumColorShift/SpectrumColorShiftCustomEffect.cs b/SpectrumColorShift/SpectrumColorShiftCustomEffect.cs index b9d0602..b554805 100644 --- a/SpectrumColorShift/SpectrumColorShiftCustomEffect.cs +++ b/SpectrumColorShift/SpectrumColorShiftCustomEffect.cs @@ -9,39 +9,40 @@ using YukkuriMovieMaker.Player.Video; namespace SpectrumColorShift; -internal sealed class SpectrumColorShiftCustomEffect : D2D1CustomShaderEffectBase +internal sealed class SpectrumColorShiftCustomEffect : IDisposable { - private const int SourceInput = 0; - private const int LinearInput = 1; - private const int EffectInputs = LinearInput + 1; + internal const int SourceInput = 0; + internal const int LightInput = 1; + private const int EffectInputs = LightInput + 1; + private readonly ShiftEffect _shift; private readonly LinearizeCustomEffect _linearized; - public SpectrumColorShiftCustomEffect(IGraphicsDevicesAndContext devices) : base(Create(devices)) + public SpectrumColorShiftCustomEffect(IGraphicsDevicesAndContext devices) { + _shift = new ShiftEffect(devices); _linearized = new LinearizeCustomEffect(devices); if (!IsEnabled) return; - using var linearized = _linearized.Output; - base.SetInput(LinearInput, linearized, true); + using var light = _linearized.Output; + _shift.SetInput(LightInput, light, true); } - public new bool IsEnabled => base.IsEnabled && _linearized.IsEnabled; + public bool IsEnabled => _shift.IsEnabled && _linearized.IsEnabled; - public new void SetInput(int index, ID2D1Image? input, RawBool invalidate) + public ID2D1Image Output => _shift.Output; + + public void SetSource(ID2D1Image? source, RawBool invalidate) { - base.SetInput(index, input, invalidate); - if (index == SourceInput) - _linearized.SetInput(SourceInput, input, invalidate); + _shift.SetInput(SourceInput, source, invalidate); + _linearized.SetInput(0, source, invalidate); } - protected override void DisposeCore(nint nativePointer, bool disposing) + public void Dispose() { - if (disposing) - _linearized.Dispose(); - - base.DisposeCore(nativePointer, disposing); + _linearized.Dispose(); + _shift.Dispose(); } internal enum PropertyIndex @@ -58,16 +59,22 @@ internal sealed class SpectrumColorShiftCustomEffect : D2D1CustomShaderEffectBas Physical, } - public float Distance { set => SetValue((int)PropertyIndex.Distance, value); } - public int Mode { set => SetValue((int)PropertyIndex.Mode, value); } - public float Angle { set => SetValue((int)PropertyIndex.Angle, value); } - public float CenterX { set => SetValue((int)PropertyIndex.CenterX, value); } - public float CenterY { set => SetValue((int)PropertyIndex.CenterY, value); } - public float Strength { set => SetValue((int)PropertyIndex.Strength, value); } - public int Dispersion { set => SetValue((int)PropertyIndex.Dispersion, value); } - public float ReferenceWavelength { set => SetValue((int)PropertyIndex.ReferenceWavelength, value); } - public int Steps { set => SetValue((int)PropertyIndex.Steps, value); } - public int Physical { set => SetValue((int)PropertyIndex.Physical, value); } + public float Distance { set => _shift.SetValue((int)PropertyIndex.Distance, value); } + public int Mode { set => _shift.SetValue((int)PropertyIndex.Mode, value); } + public float Angle { set => _shift.SetValue((int)PropertyIndex.Angle, value); } + public float CenterX { set => _shift.SetValue((int)PropertyIndex.CenterX, value); } + public float CenterY { set => _shift.SetValue((int)PropertyIndex.CenterY, value); } + public float Strength { set => _shift.SetValue((int)PropertyIndex.Strength, value); } + public int Dispersion { set => _shift.SetValue((int)PropertyIndex.Dispersion, value); } + public float ReferenceWavelength { set => _shift.SetValue((int)PropertyIndex.ReferenceWavelength, value); } + public int Steps { set => _shift.SetValue((int)PropertyIndex.Steps, value); } + public int Physical { set => _shift.SetValue((int)PropertyIndex.Physical, value); } + + internal float ReadFloat(PropertyIndex index) => _shift.GetFloatValue((int)index); + + internal int ReadInt(PropertyIndex index) => _shift.GetIntValue((int)index); + + private sealed class ShiftEffect(IGraphicsDevicesAndContext devices) : D2D1CustomShaderEffectBase(Create(devices)); [CustomEffect(EffectInputs)] internal sealed class EffectImpl : D2D1CustomShaderEffectImplBase @@ -323,7 +330,7 @@ internal sealed class SpectrumColorShiftCustomEffect : D2D1CustomShaderEffectBas var requested = inputRect.Right <= inputRect.Left || inputRect.Bottom <= inputRect.Top ? inputRect : ShiftGeometry.Input(outputRect, _mode, _header.Distance, _header.Direction, _header.Center, _header.Radius); - var activeInput = _physical ? LinearInput : SourceInput; + var activeInput = _physical ? LightInput : SourceInput; for (var index = 0; index < inputRects.Length; index++) inputRects[index] = index == activeInput ? requested : default; diff --git a/SpectrumColorShift/SpectrumColorShiftEffectProcessor.cs b/SpectrumColorShift/SpectrumColorShiftEffectProcessor.cs index 09e098e..b47facf 100644 --- a/SpectrumColorShift/SpectrumColorShiftEffectProcessor.cs +++ b/SpectrumColorShift/SpectrumColorShiftEffectProcessor.cs @@ -94,7 +94,7 @@ internal sealed class SpectrumColorShiftEffectProcessor( protected override void setInput(ID2D1Image? input) { - _effect?.SetInput(0, input, true); + _effect?.SetSource(input, true); } protected override void ClearEffectChain() @@ -112,7 +112,7 @@ internal sealed class SpectrumColorShiftEffectProcessor( private void ClearEffectChainCore() { - _effect?.SetInput(0, null, true); + _effect?.SetSource(null, true); _isFirst = true; }