Skip to content

Commit 56841ac

Browse files
committed
Add progress update rate flag
1 parent 60a60fc commit 56841ac

4 files changed

Lines changed: 51 additions & 6 deletions

File tree

PixelRay/Output/Progress/ProgressReporter.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,24 @@ namespace PixelRay.Output.Progress;
33
/// <summary>
44
/// Handles rendering progress updates via a callback function
55
/// </summary>
6-
public class ProgressReporter(int frame, int total, Action<RenderProgress> callback)
6+
/// <param name="updateRate">How many pixels to read before sending progress update. Default is 500, internal lower
7+
/// bound is 100: values below this and probably even a bit above it will cause a lot of flickering in progress
8+
/// prints. If updateRate >= total, progress will only update once from 0% to 100%
9+
/// Any value < 100 will default to 500 except 0 which sets updateRate = total
10+
/// </param>
11+
public class ProgressReporter(int frame, int total, Action<RenderProgress> callback, int updateRate = 500)
712
{
813
private int _done;
914
private readonly int _total = total;
15+
private readonly int _updateRate = updateRate == 0 ? total : updateRate >= 100 ? updateRate : 500;
1016
private readonly Action<RenderProgress> _callback = callback;
1117

1218
public void Increment(int step = 1)
1319
{
1420
int done = Interlocked.Add(ref _done, step);
1521

1622
// throttle updates (important for console performance)
17-
if (done % 500 == 0 || done == _total)
23+
if (done % _updateRate == 0 || done == _total)
1824
{
1925
_callback(new RenderProgress(
2026
frame,

PixelRay/Program.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public static void Main(string[] args)
1919
values.Add("produceGif", "");
2020
values.Add("preview", "");
2121
values.Add("debug", "");
22+
values.Add("progressUpdateRate", "");
2223

2324
// parse CLI args and update config dictionary values
2425
try
@@ -160,6 +161,22 @@ public static void Main(string[] args)
160161
}
161162
break;
162163

164+
case "-u" or "--updaterate":
165+
try
166+
{
167+
if (args[i + 1] is not null)
168+
{
169+
values["progressUpdateRate"] = args[i + 1];
170+
i++;
171+
}
172+
}
173+
catch (IndexOutOfRangeException)
174+
{
175+
Console.WriteLine("Usage: -u <updateRateInPixels>");
176+
return;
177+
}
178+
break;
179+
163180
default:
164181
Console.WriteLine($"Unsupported arg: {val}");
165182
return;
@@ -203,6 +220,14 @@ public static void Main(string[] args)
203220
};
204221

205222
FrameBuffer buffer;
223+
int progressUpdateRate = 500;
224+
225+
// update bar progress rate: how many pixels to read between update prints
226+
if (values["progressUpdateRate"] != "")
227+
{
228+
if (int.TryParse(values["progressUpdateRate"], out int updateRate))
229+
progressUpdateRate = updateRate;
230+
}
206231

207232
// Lua script animations
208233
if (values["script"] != "")
@@ -250,7 +275,8 @@ public static void Main(string[] args)
250275
{
251276
lua.Update(frame);
252277

253-
buffer = renderer.Render(scene, scene.Camera, settings.Threading, upScaleFactor, debug, frame);
278+
buffer = renderer.Render(scene, scene.Camera, settings.Threading, upScaleFactor, debug, frame,
279+
progressUpdateRate);
254280

255281
ImageWriter.WritePNG($"{frameOutputDir}{Path.DirectorySeparatorChar}frame-{frame}.png", buffer);
256282
}
@@ -262,7 +288,8 @@ public static void Main(string[] args)
262288
return;
263289
}
264290

265-
buffer = renderer.Render(scene, scene.Camera, settings.Threading, upScaleFactor, debug);
291+
buffer = renderer.Render(scene, scene.Camera, settings.Threading, upScaleFactor, debug,
292+
updateRate: progressUpdateRate);
266293

267294
string outputPath = values["output"];
268295
string imageFormat = values["outputFormat"];

PixelRay/Rendering/Renderer.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,21 @@ public class Renderer(
3838
/// <summary>
3939
/// Render a scene through camera and return pixel buffer of rendered image.
4040
/// </summary>
41+
/// <param name="threading">Toggle multithreading</param>
4142
/// <param name="upScale">Image upscaling factor using nearest-neighbor scaling</param>
4243
/// <param name="mode">What debug mode is used. Pass value as any DebugMode enum, which are: <br/>
4344
/// None, Normals, DepthHeat, ObjectId
4445
/// </param>
46+
/// <param name="frame">Current frame. Should only pass value >= 0 when creating a script animation</param>
47+
/// <param name="updateRate">Amount of pixels to render before sending progress updates. Default is 500</param>
4548
public FrameBuffer Render(
4649
Scene scene,
4750
Camera camera,
4851
bool threading = true,
4952
int upScale = 1,
5053
DebugMode mode = DebugMode.None,
51-
int frame = -1
54+
int frame = -1,
55+
int updateRate = 500
5256
)
5357
{
5458
int scaledW = upScale * _width;
@@ -61,7 +65,8 @@ public FrameBuffer Render(
6165
var reporter = new ProgressReporter(
6266
frame,
6367
_width * _height,
64-
consoleBar.Update
68+
consoleBar.Update,
69+
updateRate
6570
);
6671

6772
for (int y = 0; y < _height; y++)

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ Both the frame directory and output gif are created inside `<scripFilePath>` pat
107107
- both the output frames and produced gif are created inside the script location e.g.
108108
`<luaScriptPath>/frames/` and `<luaScriptPath>/outputGif.gif`
109109

110+
- `-u <updateRate>` or `--updaterate <updateRate>`
111+
112+
Changes progress bar update interval: how many pixels to render (from base resolution) before sending update print.
113+
- default value is 500
114+
- updateRate < 100 defaults to 500 (except 0): terminal can start to flickers with too fast updates. Even values above 100 will likely cause issues so adjust this accordingly.
115+
- 0 is a special value: it sets updateRate=base_width*base_height, meaning there will only be a single update from 0% to 100%
116+
110117
- `--debug <mode>`
111118

112119
Renders image based on selected debug mode. These are:

0 commit comments

Comments
 (0)