From 307acd1f2296a1ba1cfca89c00bb2eac9b6d4655 Mon Sep 17 00:00:00 2001 From: routersys Date: Tue, 13 Oct 2026 11:12:46 +0900 Subject: [PATCH] =?UTF-8?q?=E6=96=B9=E5=BC=8F=E3=81=94=E3=81=A8=E3=81=AB?= =?UTF-8?q?=E5=88=86=E3=81=91=E3=81=9F=E3=82=B7=E3=82=A7=E3=83=BC=E3=83=80?= =?UTF-8?q?=E3=83=BC=E3=81=A764=E3=82=B9=E3=83=86=E3=83=83=E3=83=97?= =?UTF-8?q?=E3=81=AE=E6=8F=8F=E7=94=BB=E3=81=A8=E5=88=9D=E5=9B=9E=E6=8F=8F?= =?UTF-8?q?=E7=94=BB=E3=82=92=E9=80=9F=E3=81=8F=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ・直線、放射、回転の方式ごとにコンパイルしたシェーダーを選んで使うよう変更-M --- .../SpectrumColorShift.Shader.vcxproj | 28 +++++++++-- .../SpectrumColorShift.hlsl | 17 ++++--- .../SpectrumColorShiftPhysical.hlsl | 17 ++++--- SpectrumColorShift.Tests/StepShadersTests.cs | 37 +++++++++++---- .../SpectrumColorShiftCustomEffect.cs | 2 +- SpectrumColorShift/StepShaders.cs | 46 ++++++++++++------- 6 files changed, 105 insertions(+), 42 deletions(-) diff --git a/SpectrumColorShift.Shader/SpectrumColorShift.Shader.vcxproj b/SpectrumColorShift.Shader/SpectrumColorShift.Shader.vcxproj index a1f85a1..0e0d021 100644 --- a/SpectrumColorShift.Shader/SpectrumColorShift.Shader.vcxproj +++ b/SpectrumColorShift.Shader/SpectrumColorShift.Shader.vcxproj @@ -163,13 +163,33 @@ Pixel - StepCount=%(StepCount.Identity) - $(OutDir)SpectrumColorShift%(StepCount.Identity).cso + StepCount=%(StepCount.Identity);ShaderMode=1 + $(OutDir)SpectrumColorShiftLinear%(StepCount.Identity).cso + + + Pixel + StepCount=%(StepCount.Identity);ShaderMode=2 + $(OutDir)SpectrumColorShiftRadial%(StepCount.Identity).cso + + + Pixel + StepCount=%(StepCount.Identity);ShaderMode=4 + $(OutDir)SpectrumColorShiftRotational%(StepCount.Identity).cso Pixel - StepCount=%(StepCount.Identity) - $(OutDir)SpectrumColorShiftPhysical%(StepCount.Identity).cso + StepCount=%(StepCount.Identity);ShaderMode=1 + $(OutDir)SpectrumColorShiftPhysicalLinear%(StepCount.Identity).cso + + + Pixel + StepCount=%(StepCount.Identity);ShaderMode=2 + $(OutDir)SpectrumColorShiftPhysicalRadial%(StepCount.Identity).cso + + + Pixel + StepCount=%(StepCount.Identity);ShaderMode=4 + $(OutDir)SpectrumColorShiftPhysicalRotational%(StepCount.Identity).cso diff --git a/SpectrumColorShift.Shader/SpectrumColorShift.hlsl b/SpectrumColorShift.Shader/SpectrumColorShift.hlsl index aced186..f7b1c6a 100644 --- a/SpectrumColorShift.Shader/SpectrumColorShift.hlsl +++ b/SpectrumColorShift.Shader/SpectrumColorShift.hlsl @@ -2,12 +2,17 @@ #error StepCount must be defined by the build. #endif +#ifndef ShaderMode +#error ShaderMode must be defined by the build. +#endif + Texture2D SourceTexture : register(t0); SamplerState SourceSampler : register(s0); static const int MaxSteps = 64; static const int ModeRadial = 2; static const int ModeRotational = 4; +static const int CompiledMode = ShaderMode; static const float MaxRadialShift = 0.5; static const float InteriorGuard = 0.51; @@ -29,13 +34,13 @@ float2 SamplePosition(float2 scenePosition, float2 offset, float coefficient) { float shift = coefficient * shiftDistance; - if (mode == ModeRadial) + if (CompiledMode == ModeRadial) { float scale = 1.0 - clamp(shift / radius, -MaxRadialShift, MaxRadialShift); return center + offset * scale; } - if (mode == ModeRotational) + if (CompiledMode == ModeRotational) { float sine; float cosine; @@ -48,10 +53,10 @@ float2 SamplePosition(float2 scenePosition, float2 offset, float coefficient) float2 Spread(float2 offset) { - if (mode == ModeRadial) + if (CompiledMode == ModeRadial) return abs(offset) * spreadScale; - if (mode == ModeRotational) + if (CompiledMode == ModeRotational) return length(offset) * spreadScale; return abs(shiftDistance) * abs(direction); @@ -124,7 +129,7 @@ float4 GatherInside(float2 uv, float2 pixelStep, float2 scenePosition) float3 color = 0.0; float3 coverage = 0.0; - if (mode == ModeRadial) + if (CompiledMode == ModeRadial) { float2 offsetStep = offset * pixelStep; float rate = shiftDistance / radius; @@ -133,7 +138,7 @@ float4 GatherInside(float2 uv, float2 pixelStep, float2 scenePosition) for (int index = 0; index < StepCount; index++) AccumulateRadial(index, uv, offsetStep, rate, color, coverage); } - else if (mode == ModeRotational) + else if (CompiledMode == ModeRotational) { float rate = shiftDistance / radius; diff --git a/SpectrumColorShift.Shader/SpectrumColorShiftPhysical.hlsl b/SpectrumColorShift.Shader/SpectrumColorShiftPhysical.hlsl index fce0fb1..8713e4d 100644 --- a/SpectrumColorShift.Shader/SpectrumColorShiftPhysical.hlsl +++ b/SpectrumColorShift.Shader/SpectrumColorShiftPhysical.hlsl @@ -2,6 +2,10 @@ #error StepCount must be defined by the build. #endif +#ifndef ShaderMode +#error ShaderMode must be defined by the build. +#endif + #include "Srgb.hlsli" Texture2D LinearTexture : register(t1); @@ -11,6 +15,7 @@ static const int MaxSteps = 64; static const int TableSize = 128; static const int ModeRadial = 2; static const int ModeRotational = 4; +static const int CompiledMode = ShaderMode; static const float MaxRadialShift = 0.5; static const float InteriorGuard = 0.51; @@ -32,13 +37,13 @@ float2 SamplePosition(float2 scenePosition, float2 offset, float coefficient) { float shift = coefficient * shiftDistance; - if (mode == ModeRadial) + if (CompiledMode == ModeRadial) { float scale = 1.0 - clamp(shift / radius, -MaxRadialShift, MaxRadialShift); return center + offset * scale; } - if (mode == ModeRotational) + if (CompiledMode == ModeRotational) { float sine; float cosine; @@ -51,10 +56,10 @@ float2 SamplePosition(float2 scenePosition, float2 offset, float coefficient) float2 Spread(float2 offset) { - if (mode == ModeRadial) + if (CompiledMode == ModeRadial) return abs(offset) * spreadScale; - if (mode == ModeRotational) + if (CompiledMode == ModeRotational) return length(offset) * spreadScale; return abs(shiftDistance) * abs(direction); @@ -135,7 +140,7 @@ float4 GatherInside(float2 uv, float2 pixelStep, float2 scenePosition) float3 color = 0.0; float alpha = 0.0; - if (mode == ModeRadial) + if (CompiledMode == ModeRadial) { float2 offsetStep = offset * pixelStep; float rate = shiftDistance / radius; @@ -144,7 +149,7 @@ float4 GatherInside(float2 uv, float2 pixelStep, float2 scenePosition) for (int index = 0; index < StepCount; index++) AccumulateRadial(index, uv, offsetStep, rate, color, alpha); } - else if (mode == ModeRotational) + else if (CompiledMode == ModeRotational) { float rate = shiftDistance / radius; diff --git a/SpectrumColorShift.Tests/StepShadersTests.cs b/SpectrumColorShift.Tests/StepShadersTests.cs index 32c1730..0a215db 100644 --- a/SpectrumColorShift.Tests/StepShadersTests.cs +++ b/SpectrumColorShift.Tests/StepShadersTests.cs @@ -2,6 +2,7 @@ using System.Globalization; using System.IO; using System.Runtime.CompilerServices; using System.Text.RegularExpressions; +using YukkuriMovieMaker.Commons; namespace SpectrumColorShift.Tests; @@ -14,6 +15,9 @@ public sealed partial class StepShadersTests [GeneratedRegex(@"[^""]+)""")] private static partial Regex CountsPattern(); + [GeneratedRegex(@"ShaderMode=(?\d+)\s*\$\(OutDir\)(?SpectrumColorShift(?:Physical)?)(?[A-Za-z]+)%\(StepCount\.Identity\)\.cso")] + private static partial Regex VariantPattern(); + [Fact] public void TheCountsCoverTheWholeRangeInWholeQuanta() { @@ -31,11 +35,20 @@ public sealed partial class StepShadersTests } [Fact] - public void EveryCountHasItsOwnShaderInEachFamily() + public void TheShaderProjectCompilesEveryModeOfBothFamiliesWithItsEnumValue() { - var ids = StepShaders.Counts.SelectMany(count => new[] { StepShaders.Select(false, count), StepShaders.Select(true, count) }).ToList(); + var compiled = VariantPattern().Matches(Project).Select(match => (match.Groups["prefix"].Value, match.Groups["mode"].Value, int.Parse(match.Groups["value"].Value, CultureInfo.InvariantCulture))); + var expected = from prefix in new[] { "SpectrumColorShift", "SpectrumColorShiftPhysical" } from mode in StepShaders.Modes select (prefix, mode.ToString(), (int)mode); - Assert.Equal(2 * StepShaders.Counts.Count(), ids.Distinct().Count()); + Assert.Equal(expected.Order(), compiled.Order()); + } + + [Fact] + public void EveryModeAndCountHasItsOwnShaderInEachFamily() + { + var ids = (from physical in new[] { false, true } from mode in StepShaders.Modes from count in StepShaders.Counts select StepShaders.Select(physical, mode, count)).ToList(); + + Assert.Equal(2 * StepShaders.Modes.Count() * StepShaders.Counts.Count(), ids.Distinct().Count()); } [Theory] @@ -43,19 +56,25 @@ public sealed partial class StepShadersTests [InlineData(true)] public void StepsShareTheShaderOfTheirPaddedCount(bool physical) { - foreach (var steps in Enumerable.Range(Spectrum.MinSteps, Spectrum.MaxSteps - Spectrum.MinSteps + 1)) - Assert.Equal(StepShaders.Select(physical, Spectrum.Padded(steps)), StepShaders.Select(physical, steps)); + foreach (var mode in StepShaders.Modes) + { + foreach (var steps in Enumerable.Range(Spectrum.MinSteps, Spectrum.MaxSteps - Spectrum.MinSteps + 1)) + Assert.Equal(StepShaders.Select(physical, mode, Spectrum.Padded(steps)), StepShaders.Select(physical, mode, steps)); + } } [Theory] [InlineData(false)] [InlineData(true)] - public void EveryCountIsShippedAsAResource(bool physical) + public void EveryVariantIsShippedAsAResource(bool physical) { - foreach (var count in StepShaders.Counts) + foreach (var mode in StepShaders.Modes) { - var bytes = YukkuriMovieMaker.Commons.PackResourceReader.ReadAllBytes(ShaderResourceUri.Get(StepShaders.ResourceName(physical, count))); - Assert.True(bytes.Length > 0, $"{StepShaders.ResourceName(physical, count)}"); + foreach (var count in StepShaders.Counts) + { + var name = StepShaders.ResourceName(physical, mode, count); + Assert.True(PackResourceReader.ReadAllBytes(ShaderResourceUri.Get(name)).Length > 0, name); + } } } } diff --git a/SpectrumColorShift/SpectrumColorShiftCustomEffect.cs b/SpectrumColorShift/SpectrumColorShiftCustomEffect.cs index d04a6f4..72c759d 100644 --- a/SpectrumColorShift/SpectrumColorShiftCustomEffect.cs +++ b/SpectrumColorShift/SpectrumColorShiftCustomEffect.cs @@ -228,7 +228,7 @@ internal sealed class SpectrumColorShiftCustomEffect : D2D1CustomShaderEffectBas { _cb.SpreadScale = ShiftGeometry.Spread(_mode, _cb.Distance, _cb.Radius); - var shader = StepShaders.Select(_physical, _steps); + var shader = StepShaders.Select(_physical, _mode, _steps); if (shader != _shader) { drawInformation.SetPixelShader(shader, PixelOptions.None); diff --git a/SpectrumColorShift/StepShaders.cs b/SpectrumColorShift/StepShaders.cs index bddc2f3..5392a1e 100644 --- a/SpectrumColorShift/StepShaders.cs +++ b/SpectrumColorShift/StepShaders.cs @@ -11,13 +11,15 @@ internal static class StepShaders private static readonly Variants Legacy = new(LegacyName); private static readonly Variants Physical = new(PhysicalName); - public static IEnumerable Counts => Enumerable.Range(0, Variants.Length).Select(CountAt); + public static IEnumerable Counts => Enumerable.Range(0, Variants.CountLength).Select(Variants.CountAt); - public static byte[] DefaultBytes => Legacy.Bytes(Spectrum.DefaultSteps); + public static IEnumerable Modes => Variants.Modes; - public static string ResourceName(bool physical, int count) => (physical ? Physical : Legacy).ResourceName(count); + public static byte[] DefaultBytes => Legacy.Bytes(ShiftMode.Linear, Spectrum.DefaultSteps); - public static Guid Select(bool physical, int steps) => (physical ? Physical : Legacy).Id(steps); + public static string ResourceName(bool physical, ShiftMode mode, int count) => (physical ? Physical : Legacy).ResourceName(mode, count); + + public static Guid Select(bool physical, ShiftMode mode, int steps) => (physical ? Physical : Legacy).Id(mode, steps); public static void Load(ID2D1EffectContext effectContext) { @@ -25,32 +27,44 @@ internal static class StepShaders Physical.Load(effectContext); } - private static int CountAt(int index) => Spectrum.MinSteps + index * Spectrum.StepQuantum; - - private sealed class Variants(string name) + private sealed class Variants { - public static int Length { get; } = (Spectrum.MaxSteps - Spectrum.MinSteps) / Spectrum.StepQuantum + 1; + public static ShiftMode[] Modes { get; } = Enum.GetValues(); - private readonly Guid[] _ids = [.. Enumerable.Range(0, Length).Select(_ => Guid.NewGuid())]; - private readonly Lazy[] _bytes = [.. Enumerable.Range(0, Length).Select(index => new Lazy(() => Read(name, CountAt(index))))]; + public static int CountLength { get; } = (Spectrum.MaxSteps - Spectrum.MinSteps) / Spectrum.StepQuantum + 1; - public string ResourceName(int count) => $"{name}{count}"; + private readonly string _name; + private readonly Guid[] _ids; + private readonly Lazy[] _bytes; - public Guid Id(int steps) => _ids[IndexOf(steps)]; + public Variants(string name) + { + _name = name; + _ids = [.. Enumerable.Range(0, Modes.Length * CountLength).Select(_ => Guid.NewGuid())]; + _bytes = [.. Enumerable.Range(0, Modes.Length * CountLength).Select(index => new Lazy(() => Read(index)))]; + } - public byte[] Bytes(int steps) => _bytes[IndexOf(steps)].Value; + public static int CountAt(int index) => Spectrum.MinSteps + index * Spectrum.StepQuantum; + + public string ResourceName(ShiftMode mode, int count) => $"{_name}{mode}{count}"; + + public Guid Id(ShiftMode mode, int steps) => _ids[IndexOf(mode, steps)]; + + public byte[] Bytes(ShiftMode mode, int steps) => _bytes[IndexOf(mode, steps)].Value; public void Load(ID2D1EffectContext effectContext) { - for (var index = 0; index < Length; index++) + for (var index = 0; index < _ids.Length; index++) { var bytes = _bytes[index].Value; effectContext.LoadPixelShader(_ids[index], bytes, bytes.Length); } } - private static int IndexOf(int steps) => (Spectrum.Padded(steps) - Spectrum.MinSteps) / Spectrum.StepQuantum; + private static int IndexOf(ShiftMode mode, int steps) + => Array.IndexOf(Modes, mode) * CountLength + (Spectrum.Padded(steps) - Spectrum.MinSteps) / Spectrum.StepQuantum; - private static byte[] Read(string name, int count) => PackResourceReader.ReadAllBytes(ShaderResourceUri.Get($"{name}{count}")); + private byte[] Read(int index) + => PackResourceReader.ReadAllBytes(ShaderResourceUri.Get(ResourceName(Modes[index / CountLength], CountAt(index % CountLength)))); } }