方式ごとに分けたシェーダーで64ステップの描画と初回描画を速くする

・直線、放射、回転の方式ごとにコンパイルしたシェーダーを選んで使うよう変更-M
このコミットが含まれているのは:
routersys committed 2026-10-13 11:12:46 +09:00
1 親 d5a114d14c
コミット 307acd1f22
6ファイル変更
+105 -42

検索条件に一致するファイルはありません

+24 -4
ファイルの表示
@@ -163,13 +163,33 @@
<ItemGroup>
<FxCompile Include="SpectrumColorShift.hlsl">
<ShaderType>Pixel</ShaderType>
<PreprocessorDefinitions>StepCount=%(StepCount.Identity)</PreprocessorDefinitions>
<ObjectFileOutput>$(OutDir)SpectrumColorShift%(StepCount.Identity).cso</ObjectFileOutput>
<PreprocessorDefinitions>StepCount=%(StepCount.Identity);ShaderMode=1</PreprocessorDefinitions>
<ObjectFileOutput>$(OutDir)SpectrumColorShiftLinear%(StepCount.Identity).cso</ObjectFileOutput>
</FxCompile>
<FxCompile Include="SpectrumColorShift.hlsl">
<ShaderType>Pixel</ShaderType>
<PreprocessorDefinitions>StepCount=%(StepCount.Identity);ShaderMode=2</PreprocessorDefinitions>
<ObjectFileOutput>$(OutDir)SpectrumColorShiftRadial%(StepCount.Identity).cso</ObjectFileOutput>
</FxCompile>
<FxCompile Include="SpectrumColorShift.hlsl">
<ShaderType>Pixel</ShaderType>
<PreprocessorDefinitions>StepCount=%(StepCount.Identity);ShaderMode=4</PreprocessorDefinitions>
<ObjectFileOutput>$(OutDir)SpectrumColorShiftRotational%(StepCount.Identity).cso</ObjectFileOutput>
</FxCompile>
<FxCompile Include="SpectrumColorShiftPhysical.hlsl">
<ShaderType>Pixel</ShaderType>
<PreprocessorDefinitions>StepCount=%(StepCount.Identity)</PreprocessorDefinitions>
<ObjectFileOutput>$(OutDir)SpectrumColorShiftPhysical%(StepCount.Identity).cso</ObjectFileOutput>
<PreprocessorDefinitions>StepCount=%(StepCount.Identity);ShaderMode=1</PreprocessorDefinitions>
<ObjectFileOutput>$(OutDir)SpectrumColorShiftPhysicalLinear%(StepCount.Identity).cso</ObjectFileOutput>
</FxCompile>
<FxCompile Include="SpectrumColorShiftPhysical.hlsl">
<ShaderType>Pixel</ShaderType>
<PreprocessorDefinitions>StepCount=%(StepCount.Identity);ShaderMode=2</PreprocessorDefinitions>
<ObjectFileOutput>$(OutDir)SpectrumColorShiftPhysicalRadial%(StepCount.Identity).cso</ObjectFileOutput>
</FxCompile>
<FxCompile Include="SpectrumColorShiftPhysical.hlsl">
<ShaderType>Pixel</ShaderType>
<PreprocessorDefinitions>StepCount=%(StepCount.Identity);ShaderMode=4</PreprocessorDefinitions>
<ObjectFileOutput>$(OutDir)SpectrumColorShiftPhysicalRotational%(StepCount.Identity).cso</ObjectFileOutput>
</FxCompile>
</ItemGroup>
</Target>
+11 -6
ファイルの表示
@@ -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;
+11 -6
ファイルの表示
@@ -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;
+28 -9
ファイルの表示
@@ -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(@"<StepCount Include=""(?<counts>[^""]+)""")]
private static partial Regex CountsPattern();
[GeneratedRegex(@"ShaderMode=(?<value>\d+)</PreprocessorDefinitions>\s*<ObjectFileOutput>\$\(OutDir\)(?<prefix>SpectrumColorShift(?:Physical)?)(?<mode>[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);
}
}
}
}
+1 -1
ファイルの表示
@@ -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);
+30 -16
ファイルの表示
@@ -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<int> Counts => Enumerable.Range(0, Variants.Length).Select(CountAt);
public static IEnumerable<int> Counts => Enumerable.Range(0, Variants.CountLength).Select(Variants.CountAt);
public static byte[] DefaultBytes => Legacy.Bytes(Spectrum.DefaultSteps);
public static IEnumerable<ShiftMode> 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<ShiftMode>();
private readonly Guid[] _ids = [.. Enumerable.Range(0, Length).Select(_ => Guid.NewGuid())];
private readonly Lazy<byte[]>[] _bytes = [.. Enumerable.Range(0, Length).Select(index => new Lazy<byte[]>(() => 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<byte[]>[] _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<byte[]>(() => 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))));
}
}