Skip to content

Commit

Permalink
fixed memory issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Shiina Miyuki committed Nov 1, 2020
1 parent 9edec09 commit c912f31
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 24 deletions.
1 change: 0 additions & 1 deletion src/akari/core/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ namespace akari {
stbi_image_free((void *)data);
} else {
const auto *data = stbi_load(path.string().c_str(), &x, &y, &channel, 0);
info("channel={}", channel);
image = std::make_shared<RGBAImage>(ivec2(x, y));
parallel_for(
image->resolution().y,
Expand Down
4 changes: 4 additions & 0 deletions src/akari/core/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ namespace akari::sdl {
ctx.expect("export");
expect_space(ctx);
auto var = parse_identifier(ctx);
ctx.cur_var = var;
if (ctx.cur_mod()->exports.find(var) != ctx.cur_mod()->exports.end()) {
ctx.report_error(fmt::format("{} is already defined", var), ctx.loc);
}
Expand All @@ -204,11 +205,13 @@ namespace akari::sdl {
auto val = parse(ctx);
ctx.cur_mod()->exports[var] = val;
expect_newline(ctx);
ctx.cur_var = "";
}
void Parser::parse_let(ParserContext &ctx) {
ctx.expect("let");
expect_space(ctx);
auto var = parse_identifier(ctx);
ctx.cur_var = var;
if (ctx.cur_mod()->locals.find(var) != ctx.cur_mod()->locals.end()) {
ctx.report_error(fmt::format("{} is already defined", var), ctx.loc);
}
Expand All @@ -218,6 +221,7 @@ namespace akari::sdl {
auto val = parse(ctx);
ctx.cur_mod()->locals[var] = val;
expect_newline(ctx);
ctx.cur_var = "";
}
Value Parser::parse(ParserContext &ctx) {
auto c = ctx.peek();
Expand Down
2 changes: 1 addition & 1 deletion src/akari/core/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ namespace akari::sdl {
struct AKR_EXPORT ParserContext {
std::vector<P<Module>> mod_stack;
P<Module> main;

std::string cur_var;
SourceLoc loc;
const std::string &source;
size_t pos = 0;
Expand Down
3 changes: 3 additions & 0 deletions src/akari/render/lights/area.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ namespace akari::render {
AKR_ASSERT_THROW(color);
}
}
void finalize()override{
color->finalize();
}
};
AKR_EXPORT_NODE(AreaLight, AreaLightNode);
} // namespace akari::render
1 change: 1 addition & 0 deletions src/akari/render/lights/envmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ namespace akari::render {
AKR_CHECK(!triangle.has_value());
return InfiniteAreaLight::create(*scene, transform, envmap->create_texture(allocator), allocator);
}
void finalize() override { envmap->finalize(); }
};
AKR_EXPORT_NODE(EnvMap, EnvMapNode);
} // namespace akari::render
12 changes: 9 additions & 3 deletions src/akari/render/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,18 @@ namespace akari::render {
if (field == "fraction" || field == "frac") {
fraction = resolve_texture(value);
} else if (field == "first") {
mat_A = dyn_cast<MaterialNode>(value.object());
mat_A = sg_dyn_cast<MaterialNode>(value.object());
AKR_ASSERT_THROW(mat_A);
} else if (field == "second") {
mat_B = dyn_cast<MaterialNode>(value.object());
mat_B = sg_dyn_cast<MaterialNode>(value.object());
AKR_ASSERT_THROW(mat_B);
}
}
void finalize() override {
fraction->finalize();
mat_A->finalize();
mat_B->finalize();
}
std::shared_ptr<const Material> create_material(Allocator<> allocator) override {
return make_pmr_shared<const MixMaterial>(allocator, fraction->create_texture(allocator),
mat_A->create_material(allocator),
Expand All @@ -82,13 +87,14 @@ namespace akari::render {
void object_field(sdl::Parser &parser, sdl::ParserContext &ctx, const std::string &field,
const sdl::Value &value) override {
if (field == "light") {
light_ = dyn_cast<LightNode>(value.object());
light_ = sg_dyn_cast<LightNode>(value.object());
AKR_ASSERT_THROW(light_);
}
}
std::shared_ptr<const Material> create_material(Allocator<> allocator) override {
return make_pmr_shared<EmissiveMaterial>(allocator, light_);
}
void finalize() override { light_->finalize(); }
};
AKR_EXPORT std::shared_ptr<EmissiveMaterialNode> create_emissive_material() {
return std::make_shared<EmissiveMaterialNodeImpl>();
Expand Down
4 changes: 2 additions & 2 deletions src/akari/render/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ namespace akari::render {
class AKR_EXPORT EmissiveMaterialNode : public MaterialNode {
public:
[[nodiscard]] std::shared_ptr<EmissiveMaterialNode> as_emissive() override {
return dyn_cast<EmissiveMaterialNode>(shared_from_this());
return sg_dyn_cast<EmissiveMaterialNode>(shared_from_this());
}
[[nodiscard]] virtual std::shared_ptr<LightNode> light() = 0;
};
Expand All @@ -147,7 +147,7 @@ namespace akari::render {
return create_image_texture(path);
} else {
AKR_ASSERT_THROW(value.is_object());
auto tex = dyn_cast<TextureNode>(value.object());
auto tex = sg_dyn_cast<TextureNode>(value.object());
AKR_ASSERT_THROW(tex);
return tex;
}
Expand Down
3 changes: 3 additions & 0 deletions src/akari/render/materials/diffuse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ namespace akari::render {
std::shared_ptr<const Material> create_material(Allocator<> allocator) override {
return make_pmr_shared<DiffuseMaterial>(allocator, color->create_texture(allocator));
}
void finalize() override {
color->finalize();
}
};
AKR_EXPORT_NODE(DiffuseMaterial, DiffuseMaterialNode)
} // namespace akari::render
8 changes: 5 additions & 3 deletions src/akari/render/materials/glossy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ namespace akari::render {
auto R = color->evaluate(sp);
return R;
}
Float tr(const ShadingPoint &sp) const override {
return color->tr(sp);
}
Float tr(const ShadingPoint &sp) const override { return color->tr(sp); }
};

class GlossyMaterialNode final : public MaterialNode {
Expand All @@ -64,6 +62,10 @@ namespace akari::render {
return make_pmr_shared<GlossyMaterial>(allocator, color->create_texture(allocator),
roughness->create_texture(allocator));
}
void finalize() override {
color->finalize();
roughness->finalize();
}
};
AKR_EXPORT_NODE(GlossyMaterial, GlossyMaterialNode)
} // namespace akari::render
12 changes: 6 additions & 6 deletions src/akari/render/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,26 +171,26 @@ namespace akari::render {
void SceneNode::object_field(sdl::Parser &parser, sdl::ParserContext &ctx, const std::string &field,
const sdl::Value &value) {
if (field == "camera") {
camera = dyn_cast<CameraNode>(value.object());
camera = sg_dyn_cast<CameraNode>(value.object());
AKR_ASSERT_THROW(camera);
} else if (field == "output") {
output = value.get<std::string>().value();
} else if (field == "integrator") {
integrator = dyn_cast<IntegratorNode>(value.object());
integrator = sg_dyn_cast<IntegratorNode>(value.object());
AKR_ASSERT_THROW(integrator);
} else if (field == "shapes") {
AKR_ASSERT_THROW(value.is_array());
for (auto shape : value) {
shapes.emplace_back(dyn_cast<MeshNode>(shape.object()));
shapes.emplace_back(sg_dyn_cast<MeshNode>(shape.object()));
}
} else if (field == "sampler") {
sampler = dyn_cast<SamplerNode>(value.object());
sampler = sg_dyn_cast<SamplerNode>(value.object());
AKR_ASSERT_THROW(sampler);
} else if (field == "accelerator") {
accel = dyn_cast<AcceleratorNode>(value.object());
accel = sg_dyn_cast<AcceleratorNode>(value.object());
AKR_ASSERT_THROW(accel);
} else if (field == "envmap") {
envmap = dyn_cast<LightNode>(value.object());
envmap = sg_dyn_cast<LightNode>(value.object());
AKR_ASSERT_THROW(envmap);
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/akari/render/scenegraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ namespace akari::render {
if (!pi) {
throw std::runtime_error("failed to load plugin");
}
return pi->make_shared();
auto object = pi->make_shared();
if(!ctx.cur_var.empty()){
return std::make_shared<NamedNode>(ctx.cur_var, object);
}
return object;
}
};
std::shared_ptr<SceneGraphParser> SceneGraphParser::create_parser() {
Expand Down
48 changes: 42 additions & 6 deletions src/akari/render/scenegraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ namespace akari::render {
virtual void enter(SceneGraphNode *) const {}
virtual void leave(SceneGraphNode *) const {}
};
class NamedNode;
class AKR_EXPORT SceneGraphNode : public sdl::Object {
protected:
virtual void do_traverse(TraversalCallback *cb) {}

public:
void object_field(sdl::Parser &parser, sdl::ParserContext &ctx, const std::string &field,
const sdl::Value &value) override {}
virtual bool is_named() const { return false; }
virtual void on_parameter_change(const std::string &field, const sdl::Value &value) {}
virtual void commit() {}
virtual const char *description() { return "unknown"; }
Expand All @@ -61,23 +63,57 @@ namespace akari::render {
}
virtual void finalize() {}

template <typename T>
inline std::shared_ptr<const T> cast() const;

template <typename T>
inline std::shared_ptr<T> cast();

typedef std::shared_ptr<SceneGraphNode> (*CreateFunc)(void);
};
class AKR_EXPORT NamedNode : public SceneGraphNode {
std::shared_ptr<SceneGraphNode> underlying;
std::string name_;
std::shared_ptr<SceneGraphNode> underlying_;

public:
NamedNode(std::string name, std::shared_ptr<SceneGraphNode> underlying_)
: name_(name), underlying_(underlying_) {}
const std::string &name() { return name_; }
void object_field(sdl::Parser &parser, sdl::ParserContext &ctx, const std::string &field,
const sdl::Value &value) override {
return underlying->object_field(parser, ctx, field, value);
return underlying_->object_field(parser, ctx, field, value);
}
void commit() override { underlying->commit(); }
const char *description() override { return underlying->description(); }
void traverse(TraversalCallback *cb) override { underlying->traverse(cb); }
void finalize() override { underlying->finalize(); }
void commit() override { underlying_->commit(); }
const char *description() override { return underlying_->description(); }
void traverse(TraversalCallback *cb) override { underlying_->traverse(cb); }
void finalize() override { underlying_->finalize(); }
bool is_named() const final override { return true; }
std::shared_ptr<SceneGraphNode> underlying() { return underlying_; }
};

template <typename T>
inline std::shared_ptr<const T> SceneGraphNode::cast() const {
if (is_named()) {
return dyn_cast<const NamedNode>(shared_from_this())->underlying();
} else {
return dyn_cast<const T>(shared_from_this());
}
}

template <typename T>
inline std::shared_ptr<T> SceneGraphNode::cast() {
if (is_named()) {
return dyn_cast<T>(dyn_cast<NamedNode>(shared_from_this())->underlying());
} else {
return dyn_cast<T>(shared_from_this());
}
}
template <typename U, typename T>
std::shared_ptr<U> sg_dyn_cast(const std::shared_ptr<T> &_p) {
auto p = dyn_cast<SceneGraphNode>(_p);
static_assert(std::is_base_of_v<SceneGraphNode, U>);
return p == nullptr ? nullptr : p->cast<U>();
}
class AKR_EXPORT SceneGraphParser : public sdl::Parser {
public:
virtual void register_node(const std::string &name, SceneGraphNode::CreateFunc) = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/akari/render/shapes/akari-mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ namespace akari::render {
} else if (field == "materials") {
AKR_ASSERT_THROW(value.is_array());
for (auto mat : value) {
auto m = dyn_cast<MaterialNode>(mat.object());
auto m = sg_dyn_cast<MaterialNode>(mat.object());
AKR_ASSERT(m);
materials.emplace_back(m);
}
Expand Down

0 comments on commit c912f31

Please sign in to comment.