Skip to content

Interactive 2D wave optics simulator. Turn any image into a refractive scene, add sources, watch waves refract, diffract and interfere. Probes, intensity maps, video export.

C#

Frames kept in memory: Auto and 10 % steps instead of three shares

Alexander Graef0dae709eba7d
diff --git a/CLAUDE.md b/CLAUDE.md
index dde37d7..127a718 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -773,10 +773,17 @@ FrameStore (temporary file in %LOCALAPPDATA%\WaveDude\cache, DeleteOnClose, pre-
frames in the run's `FrameShape` (halves, or palette index bytes; the canvas shrunk by the divisor, see
"Reduced preview" in the WPF shell list), planes in `FrameLayer` order, only those
present (`RunSpec.Layers`: field always; intensity when the intensity preview or export is on or a
- probe needs it). The budget is `FrameMemory.Budget(percent)`: the user's share of the physical RAM
- (Simulation > Frames kept in memory: a quarter, half, three quarters; setting `FrameMemoryPercent`,
- default 50) capped by what `GlobalMemoryStatusEx` says is free now minus a 2 GB margin, so a run never
- pushes the machine into its page file; the window logs frames in memory and on disk with the run start,
+ probe needs it). The budget is `FrameMemory.Budget(percent)`: what `GlobalMemoryStatusEx` says is free now
+ minus a 2 GB margin (the application's own buffers and headroom for other programs, a sum that does not
+ scale with the machine's RAM), so a run never pushes the machine into its page file; Simulation > Frames
+ kept in memory (2026-10-15, the user's design after the three-step share read as crude) offers "Auto" (the
+ same word in every language) and 10 % to 100 % of the physical RAM as an upper bound on top of that, the
+ setting `FrameMemoryPercent` with 0 = Auto, the default (a `user.config` from before keeps its 50 until
+ the user picks Auto); the submenu is generated from `MainWindow.FrameMemoryOptions` with the
+ `FrameMemoryMenuItemStyle` (header through `FrameMemoryOptionConverter`, which re-reads "Auto" on a
+ language switch; ids `MenuFrameMemory0`, `MenuFrameMemory10`, …). Not built, discussed 2026-10-15: lowering
+ the preview quality automatically until a run fits the budget instead of spilling to disk, which would
+ spare a laptop's small SSD. The window logs frames in memory and on disk with the run start,
and `FrameCache.CheckSpace` is only asked for the spilled part. An allocation that fails after all (the
commit limit) sends that frame and the rest to the file, noted once. The worker writes in order and
publishes `Count`; readers use `CopyLayer` (a memory copy, or `RandomAccess.Read`; safe against a
diff --git a/WaveDude.WPF/Converters/FrameMemoryOptionConverter.cs b/WaveDude.WPF/Converters/FrameMemoryOptionConverter.cs
new file mode 100644
index 0000000..53112ec
--- /dev/null
+++ b/WaveDude.WPF/Converters/FrameMemoryOptionConverter.cs
@@ -0,0 +1,34 @@
+using System.Globalization;
+using System.Windows;
+using System.Windows.Data;
+using WaveDude.Runs;
+
+namespace WaveDude.WPF.Converters;
+
+/// <summary>
+/// The header of a Simulation > Frames kept in memory entry: "Auto" (the string table's
+/// <c>MenuSimulationFrameMemoryAuto</c>) for <see cref="FrameMemory.Automatic"/>, else the percentage of the
+/// RAM, "10 %" to "100 %". A multi-value converter whose first input is the option and whose second, the
+/// current language, only serves to re-read the resource on a language switch (2026-10-15).
+/// </summary>
+public sealed class FrameMemoryOptionConverter : IMultiValueConverter
+{
+ /// <inheritdoc/>
+ public object? Convert(object[] values, Type targetType, object? parameter, CultureInfo culture)
+ {
+ if (values.Length == 0 || values[0] is not int percent)
+ {
+ return null;
+ }
+ if (percent <= FrameMemory.Automatic)
+ {
+ return Application.Current?.TryFindResource("MenuSimulationFrameMemoryAuto") as string ?? "_Auto";
+ }
+ // the underscore of a mnemonic would be swallowed; a digit mnemonic is of no use anyway
+ return string.Format(culture, "{0} %", percent);
+ }
+
+ /// <inheritdoc/>
+ public object[] ConvertBack(object value, Type[] targetTypes, object? parameter, CultureInfo culture) =>
+ throw new NotSupportedException();
+}
diff --git a/WaveDude.WPF/MainWindow.Simulation.cs b/WaveDude.WPF/MainWindow.Simulation.cs
index 1589b03..1015799 100644
--- a/WaveDude.WPF/MainWindow.Simulation.cs
+++ b/WaveDude.WPF/MainWindow.Simulation.cs
@@ -44,6 +44,12 @@ public partial class MainWindow
}
}
+ /// <summary>
+ /// The choices of Simulation > Frames kept in memory: <see cref="FrameMemory.Automatic"/> (the application
+ /// decides from what is free) and 10 % to 100 % of the RAM as an upper bound (2026-10-15, the user's design).
+ /// </summary>
+ public static int[] FrameMemoryOptions { get; } = [FrameMemory.Automatic, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
+
/// <summary>
/// The preview quality chosen in the View menu (persisted user settings, 2026-10-15): how much of a run is
/// captured and kept, for weaker machines. Applied to the preview's runs and scratch frames; an export's run
diff --git a/WaveDude.WPF/MainWindow.xaml b/WaveDude.WPF/MainWindow.xaml
index d1acb17..34fc8f8 100644
--- a/WaveDude.WPF/MainWindow.xaml
+++ b/WaveDude.WPF/MainWindow.xaml
@@ -1,6 +1,7 @@
<Window x:Class="WaveDude.WPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+ xmlns:local="clr-namespace:WaveDude.WPF"
xmlns:docking="clr-namespace:WaveDude.WPF.Docking"
xmlns:theming="clr-namespace:WaveDude.WPF.Theming"
xmlns:localization="clr-namespace:WaveDude.WPF.Localization"
@@ -23,6 +24,7 @@
<Window.Resources>
<converters:EqualityConverter x:Key="Equality" />
+ <converters:FrameMemoryOptionConverter x:Key="FrameMemoryOption" />
<converters:IntEqualsConverter x:Key="IntEquals" />
<converters:TitleConverter x:Key="TitleConverter" />
<converters:RunStatusConverter x:Key="RunStatus" />
@@ -72,6 +74,29 @@
</Setter>
</Style>
<!-- one entry per theme: check mark follows the current theme, click selects -->
+ <!-- Simulation > Frames kept in memory: "Auto" and 10 % to 100 % of the RAM as an upper bound (2026-10-15); the
+ header re-reads its word on a language switch through the second binding -->
+ <Style x:Key="FrameMemoryMenuItemStyle" TargetType="{x:Type MenuItem}" BasedOn="{StaticResource {x:Type MenuItem}}">
+ <Setter Property="Header">
+ <Setter.Value>
+ <MultiBinding Converter="{StaticResource FrameMemoryOption}" Mode="OneWay">
+ <Binding />
+ <Binding Path="Current" Source="{x:Static localization:LanguageController.Instance}" />
+ </MultiBinding>
+ </Setter.Value>
+ </Setter>
+ <Setter Property="Command" Value="{x:Static commands:SimulationCommands.SetFrameMemory}" />
+ <Setter Property="CommandParameter" Value="{Binding}" />
+ <Setter Property="IsChecked">
+ <Setter.Value>
+ <MultiBinding Converter="{StaticResource Equality}" Mode="OneWay">
+ <Binding Path="FrameMemoryPercent" Source="{x:Static p:Settings.Default}" />
+ <Binding />
+ </MultiBinding>
+ </Setter.Value>
+ </Setter>
+ <Setter Property="AutomationProperties.AutomationId" Value="{Binding StringFormat=MenuFrameMemory{0}}" />
+ </Style>
<Style x:Key="ThemeMenuItemStyle" TargetType="{x:Type MenuItem}" BasedOn="{StaticResource {x:Type MenuItem}}">
<Setter Property="Header" Value="{Binding Converter={StaticResource EnumName}}" />
<Setter Property="Command" Value="{Binding SelectCommand, Source={x:Static theming:ThemeController.Instance}}" />
@@ -243,18 +268,11 @@
<MenuItem Header="{DynamicResource MenuSimulationAutoRerun}" IsCheckable="True"
IsChecked="{Binding Source={x:Static p:Settings.Default}, Path=AutoRerun, Mode=TwoWay}"
AutomationProperties.AutomationId="MenuAutoRerun" />
- <!-- the share of the RAM a run's frames may take before the rest goes to disk (2026-10-14); a run never takes more than what is free -->
- <MenuItem Header="{DynamicResource MenuSimulationFrameMemory}" AutomationProperties.AutomationId="MenuFrameMemory">
- <MenuItem Header="{DynamicResource MenuSimulationFrameMemory25}" Command="{x:Static commands:SimulationCommands.SetFrameMemory}" CommandParameter="25"
- IsChecked="{Binding Source={x:Static p:Settings.Default}, Path=FrameMemoryPercent, Converter={StaticResource IntEquals}, ConverterParameter=25, Mode=OneWay}"
- AutomationProperties.AutomationId="MenuFrameMemory25" />
- <MenuItem Header="{DynamicResource MenuSimulationFrameMemory50}" Command="{x:Static commands:SimulationCommands.SetFrameMemory}" CommandParameter="50"
- IsChecked="{Binding Source={x:Static p:Settings.Default}, Path=FrameMemoryPercent, Converter={StaticResource IntEquals}, ConverterParameter=50, Mode=OneWay}"
- AutomationProperties.AutomationId="MenuFrameMemory50" />
- <MenuItem Header="{DynamicResource MenuSimulationFrameMemory75}" Command="{x:Static commands:SimulationCommands.SetFrameMemory}" CommandParameter="75"
- IsChecked="{Binding Source={x:Static p:Settings.Default}, Path=FrameMemoryPercent, Converter={StaticResource IntEquals}, ConverterParameter=75, Mode=OneWay}"
- AutomationProperties.AutomationId="MenuFrameMemory75" />
- </MenuItem>
+ <!-- the memory a run's frames may take before the rest goes to disk: the application decides from what is free
+ ("Auto", 2026-10-15), a percentage of the RAM is only an upper bound on that -->
+ <MenuItem Header="{DynamicResource MenuSimulationFrameMemory}" AutomationProperties.AutomationId="MenuFrameMemory"
+ ItemsSource="{x:Static local:MainWindow.FrameMemoryOptions}"
+ ItemContainerStyle="{StaticResource FrameMemoryMenuItemStyle}" />
</MenuItem>
<MenuItem Header="{DynamicResource MenuHelp}">
<MenuItem Header="{DynamicResource MenuHelpAbout}" />
diff --git a/WaveDude.WPF/MainWindow.xaml.cs b/WaveDude.WPF/MainWindow.xaml.cs
index 436fdc9..0bc37d6 100644
--- a/WaveDude.WPF/MainWindow.xaml.cs
+++ b/WaveDude.WPF/MainWindow.xaml.cs
@@ -83,7 +83,9 @@ public partial class MainWindow : Window
}
else if (args.PropertyName == nameof(Settings.FrameMemoryPercent))
{
- Logger.Info("Frames kept in memory: {Percent} % of the RAM, {Budget} right now (Simulation menu)", Settings.Default.FrameMemoryPercent, FrameMemory.Gigabytes(FrameMemory.Budget(Settings.Default.FrameMemoryPercent)));
+ Logger.Info("Frames kept in memory: {Bound}, {Budget} right now (Simulation menu)",
+ Settings.Default.FrameMemoryPercent <= FrameMemory.Automatic ? "automatic (what is free minus " + FrameMemory.Gigabytes(FrameMemory.Margin) + ")" : "at most " + Settings.Default.FrameMemoryPercent + " % of the RAM",
+ FrameMemory.Gigabytes(FrameMemory.Budget(Settings.Default.FrameMemoryPercent)));
}
else if (args.PropertyName is nameof(Settings.PreviewResolution) or nameof(Settings.PreviewFrames) or nameof(Settings.PreviewSampleBytes))
{
@@ -345,14 +347,20 @@ public partial class MainWindow : Window
/// <summary>View > Preview samples: the bytes per sample (2 halves, 1 palette indices).</summary>
private void OnSetPreviewSamples(object sender, ExecutedRoutedEventArgs e) => SetIntSetting(e, bytes => Settings.Default.PreviewSampleBytes = bytes);
- /// <summary>A menu choice carried as a number in the command parameter: stores it and saves the settings.</summary>
+ /// <summary>A menu choice carried as a number in the command parameter (an int, or digits as text): stores it and saves the settings.</summary>
private static void SetIntSetting(ExecutedRoutedEventArgs e, Action<int> apply)
{
- if (e.Parameter is string text && int.TryParse(text, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out int value))
+ int value;
+ if (e.Parameter is int number)
{
- apply(value);
- SaveSettings();
+ value = number;
}
+ else if (e.Parameter is not string text || !int.TryParse(text, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out value))
+ {
+ return;
+ }
+ apply(value);
+ SaveSettings();
}
private void OnFileNew(object sender, ExecutedRoutedEventArgs e)
diff --git a/WaveDude.WPF/Properties/Settings.Designer.cs b/WaveDude.WPF/Properties/Settings.Designer.cs
index b7e084e..3316783 100644
--- a/WaveDude.WPF/Properties/Settings.Designer.cs
+++ b/WaveDude.WPF/Properties/Settings.Designer.cs
@@ -179,7 +179,7 @@ namespace WaveDude.WPF.Properties {
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
- [global::System.Configuration.DefaultSettingValueAttribute("50")]
+ [global::System.Configuration.DefaultSettingValueAttribute("0")]
public int FrameMemoryPercent {
get {
return ((int)(this["FrameMemoryPercent"]));
diff --git a/WaveDude.WPF/Properties/Settings.settings b/WaveDude.WPF/Properties/Settings.settings
index 766a931..c782144 100644
--- a/WaveDude.WPF/Properties/Settings.settings
+++ b/WaveDude.WPF/Properties/Settings.settings
@@ -42,7 +42,7 @@
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="FrameMemoryPercent" Type="System.Int32" Scope="User">
- <Value Profile="(Default)">50</Value>
+ <Value Profile="(Default)">0</Value>
</Setting>
<Setting Name="PreviewResolution" Type="System.Int32" Scope="User">
<Value Profile="(Default)">1</Value>
diff --git a/WaveDude.WPF/Strings/Strings.DE.xaml b/WaveDude.WPF/Strings/Strings.DE.xaml
index 3291fe4..13e5267 100644
--- a/WaveDude.WPF/Strings/Strings.DE.xaml
+++ b/WaveDude.WPF/Strings/Strings.DE.xaml
@@ -45,9 +45,7 @@
<s:String x:Key="MenuSimulationUseGpu">_GPU verwenden (OpenCL)</s:String>
<s:String x:Key="MenuSimulationAutoRerun">Nach Änderungen _automatisch neu starten</s:String>
<s:String x:Key="MenuSimulationFrameMemory">Bilder im Arbeits_speicher</s:String>
- <s:String x:Key="MenuSimulationFrameMemory25">Ein _Viertel des RAM</s:String>
- <s:String x:Key="MenuSimulationFrameMemory50">Die _Hälfte des RAM</s:String>
- <s:String x:Key="MenuSimulationFrameMemory75">_Drei Viertel des RAM</s:String>
+ <s:String x:Key="MenuSimulationFrameMemoryAuto">_Auto</s:String>
<s:String x:Key="MenuHelp">_Hilfe</s:String>
<s:String x:Key="MenuHelpAbout">_Über Wave Dude</s:String>
diff --git a/WaveDude.WPF/Strings/Strings.EN.xaml b/WaveDude.WPF/Strings/Strings.EN.xaml
index 0dabfbd..9b899fe 100644
--- a/WaveDude.WPF/Strings/Strings.EN.xaml
+++ b/WaveDude.WPF/Strings/Strings.EN.xaml
@@ -45,9 +45,7 @@
<s:String x:Key="MenuSimulationUseGpu">Use the _GPU (OpenCL)</s:String>
<s:String x:Key="MenuSimulationAutoRerun">Re-run _automatically after edits</s:String>
<s:String x:Key="MenuSimulationFrameMemory">Frames kept in _memory</s:String>
- <s:String x:Key="MenuSimulationFrameMemory25">A _quarter of the RAM</s:String>
- <s:String x:Key="MenuSimulationFrameMemory50">_Half of the RAM</s:String>
- <s:String x:Key="MenuSimulationFrameMemory75">_Three quarters of the RAM</s:String>
+ <s:String x:Key="MenuSimulationFrameMemoryAuto">_Auto</s:String>
<s:String x:Key="MenuHelp">_Help</s:String>
<s:String x:Key="MenuHelpAbout">_About Wave Dude</s:String>
diff --git a/WaveDude.WPF/Strings/Strings.FR.xaml b/WaveDude.WPF/Strings/Strings.FR.xaml
index 7a73df3..8497e4b 100644
--- a/WaveDude.WPF/Strings/Strings.FR.xaml
+++ b/WaveDude.WPF/Strings/Strings.FR.xaml
@@ -45,9 +45,7 @@
<s:String x:Key="MenuSimulationUseGpu">Utiliser le _GPU (OpenCL)</s:String>
<s:String x:Key="MenuSimulationAutoRerun">Relancer a_utomatiquement après une modification</s:String>
<s:String x:Key="MenuSimulationFrameMemory">Images gardées en _mémoire</s:String>
- <s:String x:Key="MenuSimulationFrameMemory25">Un _quart de la RAM</s:String>
- <s:String x:Key="MenuSimulationFrameMemory50">La _moitié de la RAM</s:String>
- <s:String x:Key="MenuSimulationFrameMemory75">_Trois quarts de la RAM</s:String>
+ <s:String x:Key="MenuSimulationFrameMemoryAuto">_Auto</s:String>
<s:String x:Key="MenuHelp">_Aide</s:String>
<s:String x:Key="MenuHelpAbout">À _propos de Wave Dude</s:String>
diff --git a/WaveDude.WPF/Strings/Strings.NL.xaml b/WaveDude.WPF/Strings/Strings.NL.xaml
index 146a77c..0c19b3d 100644
--- a/WaveDude.WPF/Strings/Strings.NL.xaml
+++ b/WaveDude.WPF/Strings/Strings.NL.xaml
@@ -45,9 +45,7 @@
<s:String x:Key="MenuSimulationUseGpu">_GPU gebruiken (OpenCL)</s:String>
<s:String x:Key="MenuSimulationAutoRerun">Na bewerkingen _automatisch opnieuw starten</s:String>
<s:String x:Key="MenuSimulationFrameMemory">Beelden in het ge_heugen</s:String>
- <s:String x:Key="MenuSimulationFrameMemory25">Een _kwart van het RAM</s:String>
- <s:String x:Key="MenuSimulationFrameMemory50">De _helft van het RAM</s:String>
- <s:String x:Key="MenuSimulationFrameMemory75">_Driekwart van het RAM</s:String>
+ <s:String x:Key="MenuSimulationFrameMemoryAuto">_Auto</s:String>
<s:String x:Key="MenuHelp">_Help</s:String>
<s:String x:Key="MenuHelpAbout">_Over Wave Dude</s:String>
diff --git a/WaveDude/Runs/FrameMemory.cs b/WaveDude/Runs/FrameMemory.cs
index 59fda21..40e508c 100644
--- a/WaveDude/Runs/FrameMemory.cs
+++ b/WaveDude/Runs/FrameMemory.cs
@@ -12,8 +12,11 @@ namespace WaveDude.Runs;
/// </summary>
public static class FrameMemory
{
- /// <summary>Share of the physical memory a run may take when the user has not chosen one.</summary>
- public const int DefaultPercent = 50;
+ /// <summary>The share value meaning "no cap: whatever is free now minus the margin" (2026-10-15, the user's choice: the application decides, a percentage is only an upper bound).</summary>
+ public const int Automatic = 0;
+
+ /// <summary>Share of the physical memory a run may take when the user has not chosen one: <see cref="Automatic"/>.</summary>
+ public const int DefaultPercent = Automatic;
/// <summary>Memory left untouched below what is available: the application, the engines' own buffers and the system need room too.</summary>
public const long Margin = 2L << 30;
@@ -39,15 +42,19 @@ public static class FrameMemory
}
/// <summary>
- /// The frame budget for a run started now: <paramref name="percentOfPhysical"/> of the physical memory, capped
- /// at what is available minus <see cref="Margin"/>; never negative.
+ /// The frame budget for a run started now: what is available minus <see cref="Margin"/>, and no more than
+ /// <paramref name="percentOfPhysical"/> of the physical memory when that is not <see cref="Automatic"/>; never negative.
/// </summary>
- /// <param name="percentOfPhysical">The user's share, 0 to 100 (clamped).</param>
+ /// <param name="percentOfPhysical">The user's upper bound, 1 to 100 (clamped), or <see cref="Automatic"/> for none.</param>
public static long Budget(int percentOfPhysical)
{
- double share = Math.Clamp(percentOfPhysical, 0, 100) / 100.0;
- long byShare = (long)(PhysicalBytes() * share);
long byAvailable = AvailableBytes() - Margin;
+ if (percentOfPhysical <= Automatic)
+ {
+ return Math.Max(0, byAvailable);
+ }
+ double share = Math.Clamp(percentOfPhysical, 1, 100) / 100.0;
+ long byShare = (long)(PhysicalBytes() * share);
return Math.Max(0, Math.Min(byShare, byAvailable));
}