fix(core): tell value-type Auto settings without advice from an advised default
Auto<T>.Resolve returned default instead of the fallback for value types because AutoValue cannot be null there. Auto<T> now records HasAdvice; the render pipeline's ProjectValues workaround and the advisor's ReasonKey heuristic are removed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
9 files changed
+41
-61
No files matched your search
@@ -94,10 +94,6 @@ internal static class ParameterBindings
|
||||
|
||||
public static string Size(int width, int height) => string.Create(CultureInfo.InvariantCulture, $"{width}x{height}");
|
||||
|
||||
public static bool HasAdvice<T>(Auto<T> auto) => typeof(T).IsValueType
|
||||
? auto.ReasonKey is not null && !auto.ReasonKey.StartsWith(AdvisorKeys.MissingPrefix, StringComparison.Ordinal)
|
||||
: auto.AutoValue is not null;
|
||||
|
||||
private static ParameterBinding AutoField<T>(string parameter, AdvisorSection section, bool module, Func<ProjectSettings, Auto<T>> get, Func<ProjectSettings, Auto<T>, ProjectSettings> set) => new()
|
||||
{
|
||||
Parameter = parameter,
|
||||
@@ -108,7 +104,7 @@ internal static class ParameterBindings
|
||||
ResetToAuto = s => set(s, get(s).Reset()),
|
||||
};
|
||||
|
||||
private static object? EffectiveOf<T>(Auto<T> auto) => !auto.IsAuto && auto.Manual is not null ? auto.Manual : HasAdvice(auto) ? auto.AutoValue : null;
|
||||
private static object? EffectiveOf<T>(Auto<T> auto) => !auto.IsAuto && auto.Manual is not null ? auto.Manual : auto.HasAdvice ? auto.AutoValue : null;
|
||||
|
||||
/// <summary>
|
||||
/// Stores a value with its reason. Missing data changes no value: the missing-data reason is stored only when the
|
||||
@@ -118,7 +114,7 @@ internal static class ParameterBindings
|
||||
{
|
||||
var args = Strings(recommendation.ReasonArgs);
|
||||
if (recommendation.Value is null)
|
||||
return HasAdvice(auto) ? auto : auto with { ReasonKey = recommendation.ReasonKey, ReasonArgs = args };
|
||||
return auto.HasAdvice ? auto : auto with { ReasonKey = recommendation.ReasonKey, ReasonArgs = args };
|
||||
if (recommendation.Value is not T value)
|
||||
throw new InvalidOperationException($"{recommendation.Parameter} expects {typeof(T).Name}, got {recommendation.Value.GetType().Name}.");
|
||||
return auto.WithAdvice(value, recommendation.ReasonKey, args);
|
||||
|
||||
@@ -9,9 +9,13 @@ public sealed record Auto<T>
|
||||
{
|
||||
public bool IsAuto { get; init; } = true;
|
||||
|
||||
/// <summary>The advisor's value (null until analysis provides one).</summary>
|
||||
/// <summary>The advisor's value; meaningful only when <see cref="HasAdvice"/>.</summary>
|
||||
public T? AutoValue { get; init; }
|
||||
|
||||
/// <summary>True once the advisor stored a value. Needed because a value type's <see cref="AutoValue"/> is never
|
||||
/// null: without it "no advice" and an advised 0 or false look the same.</summary>
|
||||
public bool HasAdvice { get; init; }
|
||||
|
||||
/// <summary>The user's value when <see cref="IsAuto"/> is false.</summary>
|
||||
public T? Manual { get; init; }
|
||||
|
||||
@@ -21,11 +25,11 @@ public sealed record Auto<T>
|
||||
public IReadOnlyList<string>? ReasonArgs { get; init; }
|
||||
|
||||
/// <summary>The value in force: manual when set by the user, otherwise the advisor's, otherwise <paramref name="fallback"/>.</summary>
|
||||
public T Resolve(T fallback) => !IsAuto && Manual is not null ? Manual : AutoValue ?? fallback;
|
||||
public T Resolve(T fallback) => !IsAuto && Manual is not null ? Manual : HasAdvice && AutoValue is not null ? AutoValue : fallback;
|
||||
|
||||
public Auto<T> WithManual(T value) => this with { IsAuto = false, Manual = value };
|
||||
|
||||
public Auto<T> Reset() => this with { IsAuto = true, Manual = default };
|
||||
|
||||
public Auto<T> WithAdvice(T value, string? reasonKey, params string[] args) => this with { AutoValue = value, ReasonKey = reasonKey, ReasonArgs = args };
|
||||
public Auto<T> WithAdvice(T value, string? reasonKey, params string[] args) => this with { AutoValue = value, HasAdvice = true, ReasonKey = reasonKey, ReasonArgs = args };
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
using Titano.Core.Project;
|
||||
|
||||
namespace Titano.Core.Render;
|
||||
|
||||
/// <summary>
|
||||
/// Reads value-type <see cref="Auto{T}"/> settings. For value types <see cref="Auto{T}.AutoValue"/> is never null (an
|
||||
/// unconstrained <c>T?</c> is <c>T</c>), so <see cref="Auto{T}.Resolve"/> returns <c>default</c> instead of the fallback
|
||||
/// when there is no advice; here a default automatic value means "no advice" and yields the fallback.
|
||||
/// </summary>
|
||||
internal static class ProjectValues
|
||||
{
|
||||
public static T Get<T>(Auto<T> setting, T fallback) where T : struct
|
||||
{
|
||||
if (!setting.IsAuto) return setting.Manual;
|
||||
return EqualityComparer<T>.Default.Equals(setting.AutoValue, default) ? fallback : setting.AutoValue;
|
||||
}
|
||||
}
|
||||
@@ -138,8 +138,8 @@ internal sealed class RenderRun : IDisposable
|
||||
else _warnings.Add(new RenderWarning("Export.Render.KneeIgnored", knee.ToString(CultureInfo.InvariantCulture)));
|
||||
}
|
||||
var motion = input.Project.Motion;
|
||||
_targetShutter = double.Clamp(ProjectValues.Get(motion.TargetShutterAngle, 180.0), 0, 360);
|
||||
_flowQuality = ProjectValues.Get(motion.FlowQuality, FlowQualitySetting.Balanced) switch
|
||||
_targetShutter = double.Clamp(motion.TargetShutterAngle.Resolve(180.0), 0, 360);
|
||||
_flowQuality = motion.FlowQuality.Resolve(FlowQualitySetting.Balanced) switch
|
||||
{
|
||||
FlowQualitySetting.Draft => FlowQuality.Draft,
|
||||
FlowQualitySetting.Best => FlowQuality.Best,
|
||||
|
||||
@@ -204,7 +204,7 @@ public sealed class RenderPlan
|
||||
options ??= RenderPlanOptions.Default;
|
||||
var n = captureTimes.Count;
|
||||
var timing = project.Timing;
|
||||
var mode = ProjectValues.Get(timing.Mode, TimingMode.Constant);
|
||||
var mode = timing.Mode.Resolve(TimingMode.Constant);
|
||||
var warnings = new List<string>();
|
||||
var playbackText = timing.FrameRate.Resolve(string.Empty);
|
||||
var playback = FrameRateText.TryParse(playbackText, out var rate) ? rate.Value : frameRate.Value;
|
||||
|
||||
@@ -118,12 +118,12 @@ public static class RenderPreparation
|
||||
Array.Fill(excluded, true);
|
||||
foreach (var i in included) excluded[i] = false;
|
||||
|
||||
var deflicker = ProjectValues.Get(exposure.Deflicker, false);
|
||||
var holyGrail = ProjectValues.Get(exposure.HolyGrail, false);
|
||||
var regionMode = deflicker ? ProjectValues.Get(exposure.Regions, RegionMode.Off) : RegionMode.Off;
|
||||
var whiteBalance = ProjectValues.Get(exposure.WhiteBalanceSmoothing, false);
|
||||
var stabilize = ProjectValues.Get(motion.Stabilize, false);
|
||||
var smoothing = ProjectValues.Get(motion.StabilizeSmoothing, new StabilizerOptions().SmoothingSigma);
|
||||
var deflicker = exposure.Deflicker.Resolve(false);
|
||||
var holyGrail = exposure.HolyGrail.Resolve(false);
|
||||
var regionMode = deflicker ? exposure.Regions.Resolve(RegionMode.Off) : RegionMode.Off;
|
||||
var whiteBalance = exposure.WhiteBalanceSmoothing.Resolve(false);
|
||||
var stabilize = motion.Stabilize.Resolve(false);
|
||||
var smoothing = motion.StabilizeSmoothing.Resolve(new StabilizerOptions().SmoothingSigma);
|
||||
|
||||
var luminance = deflicker || holyGrail ? Valid(cached?.LogLuminance, excluded) : null;
|
||||
var measureLuminance = (deflicker || holyGrail) && luminance is null;
|
||||
@@ -183,15 +183,15 @@ public static class RenderPreparation
|
||||
{
|
||||
var options = new DeflickerOptions
|
||||
{
|
||||
Window = int.Max(3, ProjectValues.Get(exposure.DeflickerWindow, 0) is var w and > 0 ? w : RecommendedWindow(luminance)),
|
||||
Strength = double.Clamp(ProjectValues.Get(exposure.DeflickerStrength, 1.0), 0, 1),
|
||||
Window = int.Max(3, exposure.DeflickerWindow.Resolve(0) is var w and > 0 ? w : RecommendedWindow(luminance)),
|
||||
Strength = double.Clamp(exposure.DeflickerStrength.Resolve(1.0), 0, 1),
|
||||
};
|
||||
var evs = new double?[n];
|
||||
// Excluded frames (test shots, bumps) must not create exposure steps.
|
||||
for (var i = 0; i < n; i++) evs[i] = excluded[i] ? null : input.Frames[i].ExposureValue;
|
||||
if (holyGrail)
|
||||
{
|
||||
var transition = ProjectValues.Get(exposure.TransitionLength, 0);
|
||||
var transition = exposure.TransitionLength.Resolve(0);
|
||||
hg = HolyGrailEngine.Compute(luminance, evs, new HolyGrailOptions { TransitionFrames = transition > 0 ? transition : null, Deflicker = options });
|
||||
}
|
||||
var offsets = hg?.StepOffsets ?? DeflickerEngine.OffsetsFromExposure(evs);
|
||||
|
||||
@@ -47,11 +47,11 @@ public sealed record RenderSetup
|
||||
warnings.AddRange(plan.Warnings.Select(k => new RenderWarning(k)));
|
||||
warnings.AddRange(selection.Notices.Select(n => new RenderWarning(n.Key, n.Args)));
|
||||
var stacking = project.Stack.Mode != StackSetting.None;
|
||||
var blur = ProjectValues.Get(project.Motion.MotionBlur, false);
|
||||
var blur = project.Motion.MotionBlur.Resolve(false);
|
||||
if (blur && stacking) warnings.Add(new RenderWarning("Export.Render.BlurOffWhileStacking"));
|
||||
var exposure = project.Exposure;
|
||||
var measuring = ProjectValues.Get(exposure.Deflicker, false) || ProjectValues.Get(exposure.HolyGrail, false)
|
||||
|| ProjectValues.Get(exposure.WhiteBalanceSmoothing, false) || ProjectValues.Get(project.Motion.Stabilize, false);
|
||||
var measuring = exposure.Deflicker.Resolve(false) || exposure.HolyGrail.Resolve(false)
|
||||
|| exposure.WhiteBalanceSmoothing.Resolve(false) || project.Motion.Stabilize.Resolve(false);
|
||||
return new RenderSetup
|
||||
{
|
||||
Target = target,
|
||||
|
||||
@@ -30,6 +30,23 @@ public sealed class ProjectSettingsTests
|
||||
Assert.Contains("\"deflickerWindow\"", settings.ToJson());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ValueTypesFallBackOnlyWithoutAdvice()
|
||||
{
|
||||
Assert.Equal(21, new Auto<int>().Resolve(21));
|
||||
Assert.Equal(FlowQualitySetting.Balanced, new Auto<FlowQualitySetting>().Resolve(FlowQualitySetting.Balanced));
|
||||
Assert.Equal(0, new Auto<int>().WithAdvice(0, null).Resolve(21));
|
||||
Assert.False(new Auto<bool>().WithAdvice(false, null).Resolve(true));
|
||||
Assert.Equal(0.5, new Auto<double>().WithAdvice(0.8, null).WithManual(0.5).Resolve(1.0));
|
||||
Assert.False(new Auto<bool>().WithManual(false).Resolve(true));
|
||||
var back = ProjectSettings.FromJson(new ProjectSettings
|
||||
{
|
||||
Exposure = new ExposureSettings { Deflicker = new Auto<bool>().WithAdvice(false, null) },
|
||||
}.ToJson());
|
||||
Assert.False(back.Exposure.Deflicker.Resolve(true));
|
||||
Assert.True(ProjectSettings.FromJson(new ProjectSettings().ToJson()).Exposure.Deflicker.Resolve(true));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ResetReturnsToAdvice()
|
||||
{
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
using Titano.Core.Project;
|
||||
using Titano.Core.Render;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.Core.Tests.Render;
|
||||
|
||||
public sealed class ProjectValuesTests
|
||||
{
|
||||
[Test]
|
||||
public void ValueTypeSettingsFallBackWithoutAdvice()
|
||||
{
|
||||
Assert.Equal(21, ProjectValues.Get(new Auto<int>(), 21));
|
||||
Assert.Equal(180.0, ProjectValues.Get(new Auto<double>(), 180.0));
|
||||
Assert.Equal(FlowQualitySetting.Balanced, ProjectValues.Get(new Auto<FlowQualitySetting>(), FlowQualitySetting.Balanced));
|
||||
Assert.Equal(41, ProjectValues.Get(new Auto<int>().WithAdvice(41, null), 21));
|
||||
Assert.Equal(0.5, ProjectValues.Get(new Auto<double>().WithAdvice(0.8, null).WithManual(0.5), 1.0));
|
||||
Assert.Equal(FlowQualitySetting.Draft, ProjectValues.Get(new Auto<FlowQualitySetting>().WithManual(FlowQualitySetting.Draft), FlowQualitySetting.Balanced));
|
||||
Assert.False(ProjectValues.Get(new Auto<bool>().WithManual(false), true));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user