From 88bfc29323c757807fa310eb58c4494486761a3d Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 2 Nov 2026 21:47:42 -0500 Subject: [PATCH] Add CLI option to set number of render threads --- src/fart/Cli.cpp | 16 ++++++++++++++++ src/fart/RenderParams.cpp | 11 +++++++++++ src/fart/RenderParams.h | 1 + 3 files changed, 28 insertions(+) diff --git a/src/fart/Cli.cpp b/src/fart/Cli.cpp index c29d198..a1aaadd 100644 --- a/src/fart/Cli.cpp +++ b/src/fart/Cli.cpp @@ -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)) diff --git a/src/fart/RenderParams.cpp b/src/fart/RenderParams.cpp index f7d3ed4..39eccf6 100644 --- a/src/fart/RenderParams.cpp +++ b/src/fart/RenderParams.cpp @@ -1,4 +1,5 @@ #include +#include 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; + } } diff --git a/src/fart/RenderParams.h b/src/fart/RenderParams.h index 0ec22d3..7763f69 100644 --- a/src/fart/RenderParams.h +++ b/src/fart/RenderParams.h @@ -6,6 +6,7 @@ struct RenderParams { opt width; opt height; + opt threads; /* Set each field that is not already set to the value from other. */ void merge(const RenderParams & other);