Skip to content

Commit

Permalink
Pass Scenes around using unique_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
BachiLi committed Feb 15, 2024
1 parent 4c9da41 commit 5d633d6
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 30 deletions.
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ int main(int argc, char *argv[]) {
Timer timer;
tick(timer);
std::cout << "Parsing and constructing scene " << filename << "." << std::endl;
Scene scene = parse_scene(filename, embree_device);
std::unique_ptr<Scene> scene = parse_scene(filename, embree_device);
std::cout << "Done. Took " << tick(timer) << " seconds." << std::endl;
std::cout << "Rendering..." << std::endl;
Image3 img = render(scene);
if (outputfile.compare("") == 0) {outputfile = scene.output_filename;}
Image3 img = render(*scene);
if (outputfile.compare("") == 0) {outputfile = scene->output_filename;}
std::cout << "Done. Took " << tick(timer) << " seconds." << std::endl;
imwrite(outputfile, img);
std::cout << "Image written to " << outputfile << std::endl;
Expand Down
27 changes: 14 additions & 13 deletions src/parsers/parse_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1406,7 +1406,7 @@ Shape parse_shape(pugi::xml_node node,
return shape;
}

Scene parse_scene(pugi::xml_node node, const RTCDevice &embree_device) {
std::unique_ptr<Scene> parse_scene(pugi::xml_node node, const RTCDevice &embree_device) {
RenderOptions options;
Camera camera(Matrix4x4::identity(),
c_default_fov,
Expand Down Expand Up @@ -1586,19 +1586,20 @@ Scene parse_scene(pugi::xml_node node, const RTCDevice &embree_device) {
}
}
}
return Scene{embree_device,
camera,
materials,
shapes,
lights,
media,
envmap_light_id,
texture_pool,
options,
filename};
return std::make_unique<Scene>(
embree_device,
camera,
materials,
shapes,
lights,
media,
envmap_light_id,
texture_pool,
options,
filename);
}

Scene parse_scene(const fs::path &filename, const RTCDevice &embree_device) {
std::unique_ptr<Scene> parse_scene(const fs::path &filename, const RTCDevice &embree_device) {
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(filename.c_str());
if (!result) {
Expand All @@ -1609,7 +1610,7 @@ Scene parse_scene(const fs::path &filename, const RTCDevice &embree_device) {
// back up the current working directory and switch to the parent folder of the file
fs::path old_path = fs::current_path();
fs::current_path(filename.parent_path());
Scene scene = parse_scene(doc.child("scene"), embree_device);
std::unique_ptr<Scene> scene = parse_scene(doc.child("scene"), embree_device);
// switch back to the old current working directory
fs::current_path(old_path);
return scene;
Expand Down
2 changes: 1 addition & 1 deletion src/parsers/parse_scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
#include <memory>

/// Parse Mitsuba's XML scene format.
Scene parse_scene(const fs::path &filename, const RTCDevice &embree_device);
std::unique_ptr<Scene> parse_scene(const fs::path &filename, const RTCDevice &embree_device);
12 changes: 0 additions & 12 deletions src/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,6 @@ Scene::Scene(const RTCDevice &embree_device,
light_dist = make_table_dist_1d(power);
}

Scene::Scene(const Scene& t) :
embree_device(t.embree_device), embree_scene(t.embree_scene),
camera(t.camera), materials(t.materials),
shapes(t.shapes), lights(t.lights), media(t.media),
envmap_light_id(t.envmap_light_id),
texture_pool(t.texture_pool), bounds(t.bounds), options(t.options),
output_filename(t.output_filename), light_dist(t.light_dist) {
// embree_scene is managed by Embree using reference counting.
// it's crucial for us to also maintain the reference count when copying scenes.
rtcRetainScene(t.embree_scene);
}

Scene::~Scene() {
// This decreses the reference count of embree_scene in Embree,
// if it reaches zero, Embree will deallocate the scene.
Expand Down
2 changes: 1 addition & 1 deletion src/scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ struct Scene {
const RenderOptions &options,
const std::string &output_filename);
~Scene();
Scene(const Scene& t);
Scene(const Scene& t) = delete;
Scene& operator=(const Scene& t) = delete;

RTCDevice embree_device;
Expand Down

0 comments on commit 5d633d6

Please sign in to comment.