Skip to content

Commit

Permalink
Add texture attributes on shape
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastien Speierer authored and njroussel committed Apr 25, 2023
1 parent c1a9b8f commit f6ec944
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 46 deletions.
5 changes: 4 additions & 1 deletion include/mitsuba/render/shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <mitsuba/core/bbox.h>
#include <mitsuba/core/field.h>
#include <drjit/packet.h>
#include <unordered_map>

#if defined(MI_ENABLE_CUDA)
# include <mitsuba/render/optix/common.h>
Expand All @@ -24,7 +25,7 @@ NAMESPACE_BEGIN(mitsuba)
template <typename Float, typename Spectrum>
class MI_EXPORT_LIB Shape : public Object {
public:
MI_IMPORT_TYPES(BSDF, Medium, Emitter, Sensor, MeshAttribute);
MI_IMPORT_TYPES(BSDF, Medium, Emitter, Sensor, MeshAttribute, Texture);

// Use 32 bit indices to keep track of indices to conserve memory
using ScalarIndex = uint32_t;
Expand Down Expand Up @@ -567,6 +568,8 @@ class MI_EXPORT_LIB Shape : public Object {
ref<Medium> m_exterior_medium;
std::string m_id;

std::unordered_map<std::string, ref<Texture>> m_texture_attributes;

field<Transform4f, ScalarTransform4f> m_to_world;
field<Transform4f, ScalarTransform4f> m_to_object;

Expand Down
24 changes: 6 additions & 18 deletions src/render/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -901,12 +901,8 @@ Mesh<Float, Spectrum>::eval_attribute(const std::string& name,
const SurfaceInteraction3f &si,
Mask active) const {
const auto& it = m_mesh_attributes.find(name);
if (it == m_mesh_attributes.end()) {
if constexpr (dr::is_jit_v<Float>)
return 0.f;
else
Throw("Invalid attribute requested %s.", name.c_str());
}
if (it == m_mesh_attributes.end())
return Base::eval_attribute(name, si, active);

const auto& attr = it->second;
if (attr.size == 1)
Expand All @@ -930,12 +926,8 @@ Mesh<Float, Spectrum>::eval_attribute_1(const std::string& name,
const SurfaceInteraction3f &si,
Mask active) const {
const auto& it = m_mesh_attributes.find(name);
if (it == m_mesh_attributes.end()) {
if constexpr (dr::is_jit_v<Float>)
return 0.f;
else
Throw("Invalid attribute requested %s.", name.c_str());
}
if (it == m_mesh_attributes.end())
return Base::eval_attribute_1(name, si, active);

const auto& attr = it->second;
if (attr.size == 1) {
Expand All @@ -953,12 +945,8 @@ Mesh<Float, Spectrum>::eval_attribute_3(const std::string& name,
const SurfaceInteraction3f &si,
Mask active) const {
const auto& it = m_mesh_attributes.find(name);
if (it == m_mesh_attributes.end()) {
if constexpr (dr::is_jit_v<Float>)
return 0.f;
else
Throw("Invalid attribute requested %s.", name.c_str());
}
if (it == m_mesh_attributes.end())
return Base::eval_attribute_3(name, si, active);

const auto& attr = it->second;
if (attr.size == 3) {
Expand Down
70 changes: 43 additions & 27 deletions src/render/shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ MI_VARIANT Shape<Float, Spectrum>::Shape(const Properties &props) : m_id(props.i
Sensor *sensor = dynamic_cast<Sensor *>(obj.get());
BSDF *bsdf = dynamic_cast<BSDF *>(obj.get());
Medium *medium = dynamic_cast<Medium *>(obj.get());
Texture *texture = dynamic_cast<Texture *>(obj.get());

if (emitter) {
if (m_emitter)
Expand All @@ -52,6 +53,8 @@ MI_VARIANT Shape<Float, Spectrum>::Shape(const Properties &props) : m_id(props.i
Throw("Only a single exterior medium can be specified per shape.");
m_exterior_medium = medium;
}
} else if (texture) {
m_texture_attributes.insert({ name, texture });
} else {
continue;
}
Expand Down Expand Up @@ -452,38 +455,51 @@ Shape<Float, Spectrum>::ray_intersect(const Ray3f &ray, uint32_t ray_flags, Mask
}

MI_VARIANT typename Shape<Float, Spectrum>::UnpolarizedSpectrum
Shape<Float, Spectrum>::eval_attribute(const std::string & /*name*/,
const SurfaceInteraction3f & /*si*/,
Mask /*active*/) const {
/* When virtual function calls are recorded in symbolic mode,
we can't throw an exception here. */
if constexpr (dr::is_jit_v<Float>)
return 0.f;
else
NotImplementedError("eval_attribute");
Shape<Float, Spectrum>::eval_attribute(const std::string & name,
const SurfaceInteraction3f & si,
Mask active) const {
const auto& it = m_texture_attributes.find(name);
if (it == m_texture_attributes.end()) {
if constexpr (dr::is_jit_v<Float>)
return 0.f;
else
Throw("Invalid attribute requested %s.", name.c_str());
}

const auto& texture = it->second;
return texture->eval(si, active);
}

MI_VARIANT Float
Shape<Float, Spectrum>::eval_attribute_1(const std::string& /*name*/,
const SurfaceInteraction3f &/*si*/,
Mask /*active*/) const {
/* When virtual function calls are recorded in symbolic mode,
we can't throw an exception here. */
if constexpr (dr::is_jit_v<Float>)
return 0.f;
else
NotImplementedError("eval_attribute_1");
Shape<Float, Spectrum>::eval_attribute_1(const std::string& name,
const SurfaceInteraction3f &si,
Mask active) const {
const auto& it = m_texture_attributes.find(name);
if (it == m_texture_attributes.end()) {
if constexpr (dr::is_jit_v<Float>)
return 0.f;
else
Throw("Invalid attribute requested %s.", name.c_str());
}

const auto& texture = it->second;
return texture->eval_1(si, active);
}

MI_VARIANT typename Shape<Float, Spectrum>::Color3f
Shape<Float, Spectrum>::eval_attribute_3(const std::string& /*name*/,
const SurfaceInteraction3f &/*si*/,
Mask /*active*/) const {
/* When virtual function calls are recorded in symbolic mode,
we can't throw an exception here. */
if constexpr (dr::is_jit_v<Float>)
return 0.f;
else
NotImplementedError("eval_attribute_3");
Shape<Float, Spectrum>::eval_attribute_3(const std::string& name,
const SurfaceInteraction3f &si,
Mask active) const {
const auto& it = m_texture_attributes.find(name);
if (it == m_texture_attributes.end()) {
if constexpr (dr::is_jit_v<Float>)
return 0.f;
else
Throw("Invalid attribute requested %s.", name.c_str());
}

const auto& texture = it->second;
return texture->eval_3(si, active);
}

MI_VARIANT Float Shape<Float, Spectrum>::surface_area() const {
Expand Down

0 comments on commit f6ec944

Please sign in to comment.