Add CLI option to set number of render threads

This commit is contained in:
josh committed 2026-11-02 21:47:42 -05:00
1 parent e964908e46
commit 88bfc29323
3 files changed
+28

No files matched your search

+16
View File
@@ -11,6 +11,8 @@
FLAG(help, "help", false) \
FLAG(output, "o", true) \
FLAG(output_l, "output", true) \
FLAG(threads, "t", true) \
FLAG(threads_l, "threads", true) \
FLAG(version, "version", false) \
FLAG(width, "w", true) \
FLAG(width_l, "width", true) \
@@ -184,6 +186,7 @@ int Cli::handle_option(size_t flag_idx, const char * arg_value)
printf(" -h, --height H Set render height to H\n");
printf(" --help Show this help and exit\n");
printf(" -o, --output F Write rendered image to file F\n");
printf(" -t, --threads N Use N render threads (default: number of CPUs)\n");
printf(" -w, --width W Set render width to W\n");
printf(" --version Show fart version and exit\n");
return 1;
@@ -193,6 +196,19 @@ int Cli::handle_option(size_t flag_idx, const char * arg_value)
this->output_file = arg_value;
return 0;
case flag_threads:
case flag_threads_l:
if (!parse_size(flag_defs[flag_idx].flag, arg_value, &this->params.threads))
{
return 2;
}
if (*this->params.threads == 0u)
{
fprintf(stderr, "Error: option %s must be at least 1\n", flag_defs[flag_idx].flag);
return 2;
}
return 0;
case flag_width:
case flag_width_l:
if (!parse_size(flag_defs[flag_idx].flag, arg_value, &this->params.width))
+11
View File
@@ -1,4 +1,5 @@
#include <fart/RenderParams.h>
#include <thread>
void RenderParams::merge(const RenderParams & other)
{
@@ -10,6 +11,10 @@ void RenderParams::merge(const RenderParams & other)
{
height = other.height;
}
if (!threads)
{
threads = other.threads;
}
}
void RenderParams::apply_defaults()
@@ -22,4 +27,10 @@ void RenderParams::apply_defaults()
{
height = 600u;
}
if (!threads)
{
/* hardware_concurrency() may return 0 if it cannot be determined. */
size_t n = std::thread::hardware_concurrency();
threads = n > 0u ? n : 1u;
}
}
+1
View File
@@ -6,6 +6,7 @@ struct RenderParams
{
opt<size_t> width;
opt<size_t> height;
opt<size_t> threads;
/* Set each field that is not already set to the value from other. */
void merge(const RenderParams & other);