Skip to content

Commit

Permalink
Command line argument for single-threaded rendering (for debugging)
Browse files Browse the repository at this point in the history
  • Loading branch information
tizian committed Jan 31, 2020
1 parent 98a6134 commit 2d9cca9
Showing 1 changed file with 62 additions and 29 deletions.
91 changes: 62 additions & 29 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@
#include <nori/gui.h>
#include <tbb/parallel_for.h>
#include <tbb/blocked_range.h>
#include <tbb/task_scheduler_init.h>
#include <filesystem/resolver.h>
#include <thread>

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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
});

Expand Down Expand Up @@ -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] << " <scene.xml>" << 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<NoriObject> 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<NoriObject> root(loadFromXML(argv[1]));
/* When the XML root object is a scene, start rendering it .. */
if (root->getClassType() == NoriObject::EScene)
render(static_cast<Scene *>(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;
}

0 comments on commit 2d9cca9

Please sign in to comment.