Skip to content

Commit

Permalink
More Invoke() for cross-thread UI access, fix log box issue, allow -1…
Browse files Browse the repository at this point in the history
… as GPU ID for CPU
  • Loading branch information
n00mkrad committed Dec 27, 2024
1 parent 482ed1b commit 048a849
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 50 deletions.
8 changes: 4 additions & 4 deletions CodeLegacy/Forms/SettingsForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion CodeLegacy/Forms/SettingsForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,13 @@ private void InitGpus()
tooltipTorch += $"{i} = {NvApi.NvGpus[i].FullName} ({NvApi.NvGpus[i].GetVramGb().ToString("0.")} GB)\n";
}

ncnnGpus.Items.Add(-1);
tooltipNcnn += $"-1 = CPU\n";

foreach (var vkGpu in VulkanUtils.VkDevices)
{
ncnnGpus.Items.Add(vkGpu.Id);
tooltipNcnn += $"{vkGpu.Id} = {vkGpu.Name}\n";
tooltipNcnn += $"{vkGpu.Id.ToString().PadLeft(2)} = {vkGpu.Name}\n";
}

toolTip1.SetToolTip(tooltipTorchGpu, tooltipTorch.Trim());
Expand Down
3 changes: 0 additions & 3 deletions CodeLegacy/Forms/SettingsForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,6 @@
<metadata name="toolTip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="toolTip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<data name="info1.ToolTip" xml:space="preserve">
<value>Set the filename patterns using placeholders:
[NAME] for the input filename
Expand Down
22 changes: 16 additions & 6 deletions CodeLegacy/IO/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;
Expand Down Expand Up @@ -58,7 +59,10 @@ public static void ShowNext()

public static void Show(LogEntry entry, bool logToFile = true)
{
if (string.IsNullOrWhiteSpace(entry.logMessage))
//if (entry.logMessage.Contains("frame order info"))
// Debugger.Break();

if (entry.logMessage.IsEmpty())
return;

string msg = entry.logMessage;
Expand All @@ -77,12 +81,18 @@ public static void Show(LogEntry entry, bool logToFile = true)
{
if (!entry.hidden && entry.replaceLastLine)
{
textbox.Suspend();
string[] lines = textbox.Text.SplitIntoLines();
textbox.Text = string.Join(Environment.NewLine, lines.Take(lines.Count() - 1).ToArray());
textbox.Invoke(new Action(() => {
textbox.Suspend();
string[] lines = textbox.Text.SplitIntoLines();
textbox.Text = string.Join(Environment.NewLine, lines.Take(lines.Count() - 1).ToArray());
}));
}
}
catch { }
catch (Exception ex)
{
Console.WriteLine($"{ex.Message}\n{ex.StackTrace}");
if (Debugger.IsAttached) Debugger.Break();
}

msg = msg.Replace("\n", Environment.NewLine);

Expand All @@ -91,7 +101,7 @@ public static void Show(LogEntry entry, bool logToFile = true)

if (entry.replaceLastLine)
{
textbox.Resume();
textbox.Invoke(() => textbox.Resume());
msg = "[^] " + msg;
}

Expand Down
4 changes: 2 additions & 2 deletions CodeLegacy/Main/FrameOrder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ public static async Task CreateFramesFileImgSeq(string tempFolder, bool loop, fl
Array.Resize(ref frameFilesWithoutLast, frameFilesWithoutLast.Length - 1);
string framesFile = Path.Combine(tempFolder, Paths.GetFrameOrderFilename(interpFactor));
string fileContent = "";
string dupesFile = Path.Combine(tempFolder, "dupes.json");
LoadDupesFile(dupesFile);
// string dupesFile = Path.Combine(tempFolder, "dupes.json");
// LoadDupesFile(dupesFile);

string scnFramesPath = Path.Combine(tempFolder, Paths.scenesDir);

Expand Down
41 changes: 21 additions & 20 deletions CodeLegacy/Main/Interpolate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public static async Task Start()
if (!BatchProcessing.busy && Program.busy) return;
canceled = false;
Program.initialRun = false;
Program.mainForm.SetWorking(true);
Program.mainForm.Invoke(() => Program.mainForm.SetWorking(true));
if (!Utils.InputIsValid(currentSettings)) return; // General input checks
if (!Utils.CheckPathValid(currentSettings.inPath)) return; // Check if input path/file is valid
if (!Utils.CheckAiAvailable(currentSettings.ai, currentSettings.model)) return; // Check if selected AI pkg is installed
if (!AutoEncodeResume.resumeNextRun && !Utils.CheckDeleteOldTempFolder()) return; // Try to delete temp folder if an old one exists
if (!(await Utils.CheckEncoderValid())) return; // Check encoder compat
Utils.ShowWarnings(currentSettings.interpFactor, currentSettings.ai);
currentSettings.stepByStep = false;
Program.mainForm.SetStatus("Starting...");
Program.mainForm.Invoke(() => Program.mainForm.SetStatus("Starting..."));
sw.Restart();

if (currentMediaFile.IsVfr)
Expand All @@ -63,7 +63,7 @@ public static async Task Start()
if (skip || canceled) return;
await RunAi(currentSettings.interpFolder, currentSettings.ai);
if (canceled) return;
Program.mainForm.SetProgress(100);
Program.mainForm.Invoke(() => Program.mainForm.SetProgress(100));

if (!currentlyUsingAutoEnc)
{
Expand All @@ -88,36 +88,36 @@ public static async Task Start()
public static async Task Done()
{
await Cleanup();
Program.mainForm.SetWorking(false);
Program.mainForm.Invoke(() => Program.mainForm.SetWorking(false));
Logger.Log("Total processing time: " + FormatUtils.Time(sw.Elapsed));
sw.Stop();

if (!BatchProcessing.busy)
OsUtils.ShowNotificationIfInBackground("Flowframes", $"Finished interpolation after {FormatUtils.Time(sw.Elapsed)}.");

Program.mainForm.InterpolationDone();
Program.mainForm.Invoke(() => Program.mainForm.InterpolationDone());
}

public static async Task Realtime ()
{
canceled = false;

Program.mainForm.SetWorking(true);
Program.mainForm.Invoke(() => Program.mainForm.SetWorking(true));

if(currentSettings.ai.NameInternal != Implementations.rifeNcnnVs.NameInternal)
Cancel($"Real-time interpolation is only available when using {Implementations.rifeNcnnVs.FriendlyName}.");

if (canceled) return;

Program.mainForm.SetStatus("Downloading models...");
Program.mainForm.Invoke(() => Program.mainForm.SetStatus("Downloading models..."));
await ModelDownloader.DownloadModelFiles(currentSettings.ai, currentSettings.model.Dir);

if (canceled) return;

Program.mainForm.SetStatus("Running real-time interpolation...");
Program.mainForm.Invoke(() => Program.mainForm.SetStatus("Running real-time interpolation..."));
await AiProcess.RunRifeNcnnVs(currentSettings.framesFolder, "", currentSettings.interpFactor, currentSettings.model.Dir, true);
Program.mainForm.SetStatus("Ready");
Program.mainForm.SetWorking(false);
Program.mainForm.Invoke(() => Program.mainForm.SetStatus("Ready"));
Program.mainForm.Invoke(() => Program.mainForm.SetWorking(false));
}

public static async Task GetFrames()
Expand All @@ -127,7 +127,7 @@ public static async Task GetFrames()

if (Config.GetBool(Config.Key.scnDetect) && !currentSettings.ai.Piped)
{
Program.mainForm.SetStatus("Extracting scenes from video...");
Program.mainForm.Invoke(() => Program.mainForm.SetStatus("Extracting scenes from video..."));
await FfmpegExtract.ExtractSceneChanges(currentSettings.inPath, Path.Combine(currentSettings.tempFolder, Paths.scenesDir), new Fraction(), currentSettings.inputIsFrames, currentSettings.framesExt);
}

Expand All @@ -140,7 +140,7 @@ public static async Task GetFrames()
public static async Task ExtractFrames(string inPath, string outPath, bool alpha)
{
if (canceled) return;
Program.mainForm.SetStatus("Extracting frames from video...");
Program.mainForm.Invoke(() => Program.mainForm.SetStatus("Extracting frames from video..."));
currentSettings.RefreshExtensions(InterpSettings.FrameType.Import);
bool mpdecimate = Config.GetInt(Config.Key.dedupMode) == 2;
Size res = await Utils.GetOutputResolution(FfmpegCommands.ModuloMode.ForEncoding, inPath, print: true);
Expand Down Expand Up @@ -170,7 +170,7 @@ public static async Task PostProcessFrames(bool stepByStep)
{
if (canceled) return;

Program.mainForm.SetStatus("Processing frames...");
Program.mainForm.Invoke(() => Program.mainForm.SetStatus("Processing frames..."));

int extractedFrames = IoUtils.GetAmountOfFiles(currentSettings.framesFolder, false, "*" + currentSettings.framesExt);

Expand Down Expand Up @@ -226,7 +226,7 @@ public static async Task RunAi(string outpath, AiInfo ai, bool stepByStep = fals

if (canceled) return;

Program.mainForm.SetStatus("Downloading models...");
Program.mainForm.Invoke(() => Program.mainForm.SetStatus("Downloading models..."));
await ModelDownloader.DownloadModelFiles(ai, currentSettings.model.Dir);

if (canceled) return;
Expand Down Expand Up @@ -263,18 +263,19 @@ public static async Task RunAi(string outpath, AiInfo ai, bool stepByStep = fals
tasks.Add(AutoEncode.MainLoop(outpath));
}

Program.mainForm.SetStatus("Running AI...");
Program.mainForm.Invoke(() => Program.mainForm.SetStatus("Running AI..."));
await Task.WhenAll(tasks);
}

public static void Cancel(string reason = "", bool noMsgBox = false)
{
if (currentSettings == null)
if (currentSettings == null || canceled)
return;

canceled = true;
Program.mainForm.SetStatus("Canceled.");
Program.mainForm.SetProgress(0);

Program.mainForm.Invoke(() => Program.mainForm.SetStatus("Canceled."));
Program.mainForm.Invoke(() => Program.mainForm.SetProgress(0));
AiProcess.Kill();
AvProcess.Kill();

Expand All @@ -296,8 +297,8 @@ public static void Cancel(string reason = "", bool noMsgBox = false)
}

AutoEncode.busy = false;
Program.mainForm.SetWorking(false);
Program.mainForm.SetTab(Program.mainForm.interpOptsTab.Name);
Program.mainForm.Invoke(() => Program.mainForm.SetWorking(false));
Program.mainForm.Invoke(() => Program.mainForm.SetTab(Program.mainForm.interpOptsTab.Name));
Logger.LogIfLastLineDoesNotContainMsg("Canceled interpolation.");

if(Cli.AutoRun)
Expand Down
11 changes: 6 additions & 5 deletions CodeLegacy/Os/AiProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class AiProcess
public static Stopwatch processTimeMulti = new Stopwatch();

public static int lastStartupTimeMs = 1000;
static string lastInPath;
private static string lastInPath;
private static string NcnnGpuIds => Config.Get(Config.Key.ncnnGpus).Trim();

public static void Kill()
{
Expand Down Expand Up @@ -330,7 +331,7 @@ static async Task RunRifeNcnnProcess(string inPath, float factor, string outPath
string ttaStr = Config.GetBool(Config.Key.rifeNcnnUseTta, false) ? "-x" : "";

rifeNcnn.StartInfo.Arguments = $"{OsUtils.GetCmdArg()} cd /D {Path.Combine(Paths.GetPkgPath(), Implementations.rifeNcnn.PkgDir).Wrap()} & rife-ncnn-vulkan.exe " +
$" -v -i {inPath.Wrap()} -o {outPath.Wrap()} {frames} -m {mdl.Lower()} {ttaStr} {uhdStr} -g {Config.Get(Config.Key.ncnnGpus)} -f {NcnnUtils.GetNcnnPattern()} -j {NcnnUtils.GetNcnnThreads(Implementations.rifeNcnn)}";
$" -v -i {inPath.Wrap()} -o {outPath.Wrap()} {frames} -m {mdl.Lower()} {ttaStr} {uhdStr} -g {NcnnGpuIds} -f {NcnnUtils.GetNcnnPattern()} -j {NcnnUtils.GetNcnnThreads(Implementations.rifeNcnn)}";

Logger.Log("cmd.exe " + rifeNcnn.StartInfo.Arguments, true);

Expand Down Expand Up @@ -379,7 +380,7 @@ static async Task RunRifeNcnnVsProcess(string inPath, float factor, string outPa
string avDir = Path.Combine(Paths.GetPkgPath(), Paths.audioVideoDir);
string pipedTargetArgs = $"{Path.Combine(avDir, "ffmpeg").Wrap()} -loglevel warning -stats -y {await Export.GetPipedFfmpegCmd(rt)}";
string pkgDir = Path.Combine(Paths.GetPkgPath(), Implementations.rifeNcnnVs.PkgDir);
int gpuId = Config.Get(Config.Key.ncnnGpus).Split(',')[0].GetInt();
int gpuId = NcnnGpuIds.Split(',')[0].GetInt();

var vsSettings = new VapourSynthUtils.VsSettings()
{
Expand Down Expand Up @@ -462,7 +463,7 @@ public static async Task RunDainNcnnProcess(string framesPath, string outPath, f
int targetFrames = ((IoUtils.GetAmountOfFiles(lastInPath, false, "*.*") * factor).RoundToInt());

string args = $" -v -i {framesPath.Wrap()} -o {outPath.Wrap()} -n {targetFrames} -m {mdl.Lower()}" +
$" -t {NcnnUtils.GetNcnnTilesize(tilesize)} -g {Config.Get(Config.Key.ncnnGpus)} -f {NcnnUtils.GetNcnnPattern()} -j 2:1:2";
$" -t {NcnnUtils.GetNcnnTilesize(tilesize)} -g {NcnnGpuIds} -f {NcnnUtils.GetNcnnPattern()} -j 2:1:2";

dain.StartInfo.Arguments = $"{OsUtils.GetCmdArg()} cd /D {dainDir.Wrap()} & dain-ncnn-vulkan.exe {args}";
Logger.Log("Running DAIN...", false);
Expand Down Expand Up @@ -584,7 +585,7 @@ static async Task RunIfrnetNcnnProcess(string inPath, float factor, string outPa
string ttaStr = ""; // Config.GetBool(Config.Key.rifeNcnnUseTta, false) ? "-x" : "";

ifrnetNcnn.StartInfo.Arguments = $"{OsUtils.GetCmdArg()} cd /D {Path.Combine(Paths.GetPkgPath(), Implementations.ifrnetNcnn.PkgDir).Wrap()} & ifrnet-ncnn-vulkan.exe " +
$" -v -i {inPath.Wrap()} -o {outPath.Wrap()} -m {mdl} {ttaStr} {uhdStr} -g {Config.Get(Config.Key.ncnnGpus)} -f {NcnnUtils.GetNcnnPattern()} -j {NcnnUtils.GetNcnnThreads(Implementations.ifrnetNcnn)}";
$" -v -i {inPath.Wrap()} -o {outPath.Wrap()} -m {mdl} {ttaStr} {uhdStr} -g {NcnnGpuIds} -f {NcnnUtils.GetNcnnPattern()} -j {NcnnUtils.GetNcnnThreads(Implementations.ifrnetNcnn)}";

Logger.Log("cmd.exe " + ifrnetNcnn.StartInfo.Arguments, true);

Expand Down
2 changes: 1 addition & 1 deletion CodeLegacy/Os/VapourSynthUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public static string CreateScript(VsSettings s, bool alwaysPreferFactorOverFps =

Fraction factor = new Fraction(s.Factor);
string interpStr = alwaysPreferFactorOverFps || Interpolate.currentMediaFile.IsVfr ? $"factor_num={factor.Numerator}, factor_den={factor.Denominator}" : $"fps_num={outFps.Numerator}, fps_den={outFps.Denominator}";
l.Add($"clip = core.rife.RIFE(clip, {interpStr}, model_path={mdlPath}, gpu_id={s.GpuId}, gpu_thread={s.GpuThreads}, tta={s.Tta}, uhd={s.Uhd}, sc={sc})"); // Interpolate
l.Add($"clip = core.rife.RIFE(clip, {interpStr}, model_path={mdlPath}, gpu_id={s.GpuId.ToString().Replace("-1", "None")}, gpu_thread={s.GpuThreads}, tta={s.Tta}, uhd={s.Uhd}, sc={sc})"); // Interpolate

if (s.Dedupe && !s.Realtime)
{
Expand Down
19 changes: 11 additions & 8 deletions CodeLegacy/Os/VulkanUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,22 @@ public static void Init()
// Clean up Vulkan resources
vkInstance.Destroy();
Logger.Log($"[VK] Vulkan device check completed after {sw.ElapsedMs} ms", true);

if (VkDevices.Count == 0)
return;

// Set the device that has the most compute queues as default GPU
var maxQueuesDevice = VkDevices.OrderByDescending(d => d.ComputeQueueCount).First();
Config.Set(Config.Key.ncnnGpus, $"{maxQueuesDevice.Id}");
}
catch(Exception ex)
{
Logger.Log($"Vulkan Error: {ex.Message}", true);
Logger.Log($"Vulkan initialization failed. NCNN implementations might not work, or run on the CPU.");
}

if (VkDevices.Count == 0)
{
Logger.Log($"No Vulkan-capable GPUs found. NCNN implementations will run on the CPU instead and may be unstable.");
Config.Set(Config.Key.ncnnGpus, "-1"); // -1 = CPU
return;
}

// Set the device that has the most compute queues as default GPU
var maxQueuesDevice = VkDevices.OrderByDescending(d => d.ComputeQueueCount).First();
Config.Set(Config.Key.ncnnGpus, $"{maxQueuesDevice.Id}");
}

public static int GetMaxNcnnThreads(int deviceId)
Expand Down

0 comments on commit 048a849

Please sign in to comment.