From 2d9cca9598cf80b5394f256c37db28e6ab9bc3b4 Mon Sep 17 00:00:00 2001 From: Tizian Zeltner Date: Fri, 31 Jan 2020 12:31:00 +0100 Subject: [PATCH] Command line argument for single-threaded rendering (for debugging) --- src/main.cpp | 91 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 62 insertions(+), 29 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 1a497b94..04640b56 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,11 +27,14 @@ #include #include #include +#include #include #include using namespace nori; +static int threadCount = -1; + static void renderBlock(const Scene *scene, Sampler *sampler, ImageBlock &block) { const Camera *camera = scene->getCamera(); const Integrator *integrator = scene->getIntegrator(); @@ -81,6 +84,8 @@ static void render(Scene *scene, const std::string &filename) { /* Do the following in parallel and asynchronously */ std::thread render_thread([&] { + tbb::task_scheduler_init init(threadCount); + cout << "Rendering .. "; cout.flush(); Timer timer; @@ -112,12 +117,12 @@ static void render(Scene *scene, const std::string &filename) { } }; - /// Uncomment the following line for single threaded rendering - // map(range); - /// Default: parallel rendering tbb::parallel_for(range, map); + /// (equivalent to the following single-threaded call) + // map(range); + cout << "done. (took " << timer.elapsedString() << ")" << endl; }); @@ -148,42 +153,70 @@ static void render(Scene *scene, const std::string &filename) { } int main(int argc, char **argv) { - if (argc != 2) { + if (argc < 2) { cerr << "Syntax: " << argv[0] << " " << endl; return -1; } - filesystem::path path(argv[1]); + std::string sceneName = ""; - try { - if (path.extension() == "xml") { - /* Add the parent directory of the scene file to the - file resolver. That way, the XML file can reference - resources (OBJ files, textures) using relative paths */ - getFileResolver()->prepend(path.parent_path()); + for (int i = 1; i < argc; ++i) { + std::string token(argv[i]); + if (token == "-t" || token == "--threads") { + if (i+1 >= argc) { + cerr << "\"--threads\" argument expects a positive integer following it." << endl; + return -1; + } + threadCount = atoi(argv[i+1]); + i++; + if (threadCount <= 0) { + cerr << "\"--threads\" argument expects a positive integer following it." << endl; + return -1; + } - std::unique_ptr root(loadFromXML(argv[1])); + continue; + } + filesystem::path path(argv[i]); + + try { + if (path.extension() == "xml") { + sceneName = argv[i]; + + /* Add the parent directory of the scene file to the + file resolver. That way, the XML file can reference + resources (OBJ files, textures) using relative paths */ + getFileResolver()->prepend(path.parent_path()); + } else if (path.extension() == "exr") { + /* Alternatively, provide a basic OpenEXR image viewer */ + Bitmap bitmap(argv[1]); + ImageBlock block(Vector2i((int) bitmap.cols(), (int) bitmap.rows()), nullptr); + block.fromBitmap(bitmap); + nanogui::init(); + NoriScreen *screen = new NoriScreen(block); + nanogui::mainloop(); + delete screen; + nanogui::shutdown(); + } else { + cerr << "Fatal error: unknown file \"" << argv[1] + << "\", expected an extension of type .xml or .exr" << endl; + } + } catch (const std::exception &e) { + cerr << "Fatal error: " << e.what() << endl; + return -1; + } + } + + if (threadCount < 0) { + threadCount = tbb::task_scheduler_init::automatic; + } + + if (sceneName != "") { + std::unique_ptr root(loadFromXML(argv[1])); /* When the XML root object is a scene, start rendering it .. */ if (root->getClassType() == NoriObject::EScene) render(static_cast(root.get()), argv[1]); - } else if (path.extension() == "exr") { - /* Alternatively, provide a basic OpenEXR image viewer */ - Bitmap bitmap(argv[1]); - ImageBlock block(Vector2i((int) bitmap.cols(), (int) bitmap.rows()), nullptr); - block.fromBitmap(bitmap); - nanogui::init(); - NoriScreen *screen = new NoriScreen(block); - nanogui::mainloop(); - delete screen; - nanogui::shutdown(); - } else { - cerr << "Fatal error: unknown file \"" << argv[1] - << "\", expected an extension of type .xml or .exr" << endl; - } - } catch (const std::exception &e) { - cerr << "Fatal error: " << e.what() << endl; - return -1; } + return 0; }