ステップ数ごとに展開したシェーダーで描画を速くする

・ステップ数に合わせて展開したシェーダーを選んで使うよう変更-M
このコミットが含まれているのは:
routersys committed 2026-10-13 09:39:27 +09:00
1 親 1505b0e1c8
コミット 15fda80d2d
12ファイル変更
+248 -83

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

+2 -2
ファイルの表示
@@ -8,7 +8,7 @@ internal static class Spectrum
public const int MaxSteps = 64;
public const int DefaultSteps = 32;
public const int EntriesPerStep = 2;
public const int Unroll = 4;
public const int StepQuantum = 4;
public const int SpectralTableLength = MaxSteps * EntriesPerStep;
public const float ShortestWavelength = 400f;
public const float LongestWavelength = 700f;
@@ -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) + Unroll - 1) / Unroll * Unroll;
public static int Padded(int steps) => (ClampSteps(steps) + StepQuantum - 1) / StepQuantum * StepQuantum;
public static float ClampReference(float wavelength)
=> float.IsNaN(wavelength) ? DefaultReferenceWavelength : Math.Clamp(wavelength, ShortestWavelength, LongestWavelength);
+1 -3
ファイルの表示
@@ -61,9 +61,7 @@
</ItemGroup>
<ItemGroup>
<Resource Include="$(ShaderDirPath)SpectrumColorShift.cso" Link="Resources\Shader\SpectrumColorShift.cso" />
<Resource Include="$(ShaderDirPath)SpectrumColorShiftPhysical.cso" Link="Resources\Shader\SpectrumColorShiftPhysical.cso" />
<Resource Include="$(ShaderDirPath)SpectrumColorShiftLinearize.cso" Link="Resources\Shader\SpectrumColorShiftLinearize.cso" />
<Resource Include="$(ShaderDirPath)SpectrumColorShift*.cso" Link="Resources\Shader\%(Filename)%(Extension)" />
</ItemGroup>
<ItemGroup>
+7 -12
ファイルの表示
@@ -71,10 +71,6 @@ internal sealed class SpectrumColorShiftCustomEffect : D2D1CustomShaderEffectBas
[CustomEffect(2)]
internal sealed class EffectImpl : D2D1CustomShaderEffectImplBase<EffectImpl>
{
private const string PhysicalShaderName = "SpectrumColorShiftPhysical";
private static readonly Guid PhysicalShaderId = Guid.NewGuid();
private ConstantBuffer _cb;
private PhysicalConstantBuffer _physicalCb;
private ShiftMode _mode = ShiftMode.Linear;
@@ -85,7 +81,7 @@ internal sealed class SpectrumColorShiftCustomEffect : D2D1CustomShaderEffectBas
private bool _tableStale = true;
private bool _spectralStale = true;
private bool _physical;
private bool _physicalShaderApplied;
private Guid _shader = GUID_PixelShader;
[CustomEffectProperty(PropertyType.Float, (int)PropertyIndex.Distance)]
public float Distance
@@ -208,7 +204,7 @@ internal sealed class SpectrumColorShiftCustomEffect : D2D1CustomShaderEffectBas
}
}
public EffectImpl() : base(ShaderResourceUri.Get("SpectrumColorShift"))
public EffectImpl() : base(StepShaders.DefaultBytes)
{
_cb.Direction = Vector2.UnitX;
_cb.Radius = 1f;
@@ -220,9 +216,7 @@ internal sealed class SpectrumColorShiftCustomEffect : D2D1CustomShaderEffectBas
public override void Initialize(ID2D1EffectContext effectContext, ID2D1TransformGraph transformGraph)
{
base.Initialize(effectContext, transformGraph);
var bytes = PackResourceReader.ReadAllBytes(ShaderResourceUri.Get(PhysicalShaderName));
effectContext.LoadPixelShader(PhysicalShaderId, bytes, bytes.Length);
StepShaders.Load(effectContext);
}
protected override void UpdateConstants()
@@ -234,10 +228,11 @@ internal sealed class SpectrumColorShiftCustomEffect : D2D1CustomShaderEffectBas
{
_cb.SpreadScale = ShiftGeometry.Spread(_mode, _cb.Distance, _cb.Radius);
if (_physical != _physicalShaderApplied)
var shader = StepShaders.Select(_physical, _steps);
if (shader != _shader)
{
drawInformation.SetPixelShader(_physical ? PhysicalShaderId : GUID_PixelShader, PixelOptions.None);
_physicalShaderApplied = _physical;
drawInformation.SetPixelShader(shader, PixelOptions.None);
_shader = shader;
}
if (_physical)
+56
ファイルの表示
@@ -0,0 +1,56 @@
using Vortice.Direct2D1;
using YukkuriMovieMaker.Commons;
namespace SpectrumColorShift;
internal static class StepShaders
{
private const string LegacyName = "SpectrumColorShift";
private const string PhysicalName = "SpectrumColorShiftPhysical";
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 byte[] DefaultBytes => Legacy.Bytes(Spectrum.DefaultSteps);
public static string ResourceName(bool physical, int count) => (physical ? Physical : Legacy).ResourceName(count);
public static Guid Select(bool physical, int steps) => (physical ? Physical : Legacy).Id(steps);
public static void Load(ID2D1EffectContext effectContext)
{
Legacy.Load(effectContext);
Physical.Load(effectContext);
}
private static int CountAt(int index) => Spectrum.MinSteps + index * Spectrum.StepQuantum;
private sealed class Variants(string name)
{
public static int Length { get; } = (Spectrum.MaxSteps - Spectrum.MinSteps) / Spectrum.StepQuantum + 1;
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 string ResourceName(int count) => $"{name}{count}";
public Guid Id(int steps) => _ids[IndexOf(steps)];
public byte[] Bytes(int steps) => _bytes[IndexOf(steps)].Value;
public void Load(ID2D1EffectContext effectContext)
{
for (var index = 0; index < 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 byte[] Read(string name, int count) => PackResourceReader.ReadAllBytes(ShaderResourceUri.Get($"{name}{count}"));
}
}