Skip to content

Commit

Permalink
Remove SamplingContext from BSDFSample
Browse files Browse the repository at this point in the history
  • Loading branch information
dictoon committed Sep 28, 2015
1 parent bbda48e commit e76b494
Show file tree
Hide file tree
Showing 26 changed files with 86 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,9 @@ void DirectLightingIntegrator::take_single_bsdf_sample(
assert(m_light_sampler.get_emitting_triangle_count() > 0);

// Sample the BSDF.
BSDFSample sample(m_shading_point, sampling_context, outgoing);
BSDFSample sample(m_shading_point, outgoing);
m_bsdf.sample(
sampling_context,
m_bsdf_data,
false, // not adjoint
true, // multiply by |cos(incoming, normal)|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,9 @@ void compute_ibl_bsdf_sampling(
// includes the contribution of a specular component since these are explicitly rejected
// afterward. We need a mechanism to indicate that we want the contribution of some of
// the components only.
BSDFSample sample(shading_point, sampling_context, outgoing);
BSDFSample sample(shading_point, outgoing);
bsdf.sample(
sampling_context,
bsdf_data,
false, // not adjoint
true, // multiply by |cos(incoming, normal)|
Expand Down
6 changes: 2 additions & 4 deletions src/appleseed/renderer/kernel/lighting/pathtracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,9 @@ size_t PathTracer<PathVisitor, Adjoint>::trace(
if (vertex.m_bsdf)
{
// Sample the BSDF.
BSDFSample sample(
*vertex.m_shading_point,
sampling_context,
vertex.m_outgoing);
BSDFSample sample(*vertex.m_shading_point, vertex.m_outgoing);
vertex.m_bsdf->sample(
sampling_context,
vertex.m_bsdf_data,
Adjoint,
true, // multiply by |cos(incoming, normal)|
Expand Down
5 changes: 3 additions & 2 deletions src/appleseed/renderer/modeling/bsdf/ashikhminbrdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ namespace
}

FORCE_INLINE virtual void sample(
SamplingContext& sampling_context,
const void* data,
const bool adjoint,
const bool cosine_mult,
Expand All @@ -124,8 +125,8 @@ namespace
compute_sval(sval, values->m_nu, values->m_nv);

// Generate a uniform sample in [0,1)^3.
sample.get_sampling_context().split_in_place(3, 1);
const Vector3d s = sample.get_sampling_context().next_vector2<3>();
sampling_context.split_in_place(3, 1);
const Vector3d s = sampling_context.next_vector2<3>();

ScatteringMode::Mode mode;
Vector3d h, incoming;
Expand Down
1 change: 1 addition & 0 deletions src/appleseed/renderer/modeling/bsdf/bsdf.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ class APPLESEED_DLLSYMBOL BSDF
// pair of directions. Return the scattering mode. If the scattering mode
// is Absorption, the BSDF and PDF values are undefined.
virtual void sample(
SamplingContext& sampling_context,
const void* data, // input values
const bool adjoint, // if true, use the adjoint scattering kernel
const bool cosine_mult, // if true, multiply by |cos(incoming, normal)|
Expand Down
6 changes: 4 additions & 2 deletions src/appleseed/renderer/modeling/bsdf/bsdfblend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ namespace
}

FORCE_INLINE virtual void sample(
SamplingContext& sampling_context,
const void* data,
const bool adjoint,
const bool cosine_mult,
Expand All @@ -169,12 +170,13 @@ namespace
const InputValues* values = static_cast<const InputValues*>(data);

// Choose which of the two BSDFs to sample.
sample.get_sampling_context().split_in_place(1, 1);
const double s = sample.get_sampling_context().next_double2();
sampling_context.split_in_place(1, 1);
const double s = sampling_context.next_double2();
const size_t bsdf_index = s < values->m_weight ? 0 : 1;

// Sample the chosen BSDF.
m_bsdf[bsdf_index]->sample(
sampling_context,
get_bsdf_data(data, bsdf_index),
adjoint,
false, // do not multiply by |cos(incoming, normal)|
Expand Down
6 changes: 4 additions & 2 deletions src/appleseed/renderer/modeling/bsdf/bsdfmix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ namespace
}

FORCE_INLINE virtual void sample(
SamplingContext& sampling_context,
const void* data,
const bool adjoint,
const bool cosine_mult,
Expand All @@ -176,12 +177,13 @@ namespace
return;

// Choose which of the two BSDFs to sample.
sample.get_sampling_context().split_in_place(1, 1);
const double s = sample.get_sampling_context().next_double2();
sampling_context.split_in_place(1, 1);
const double s = sampling_context.next_double2();
const size_t bsdf_index = s * total_weight < w[0] ? 0 : 1;

// Sample the chosen BSDF.
m_bsdf[bsdf_index]->sample(
sampling_context,
get_bsdf_data(data, bsdf_index),
adjoint,
false, // do not multiply by |cos(incoming, normal)|
Expand Down
10 changes: 0 additions & 10 deletions src/appleseed/renderer/modeling/bsdf/bsdfsample.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,10 @@ class BSDFSample
// Constructor.
BSDFSample(
const ShadingPoint& shading_point,
SamplingContext& sampling_context,
const foundation::Dual3d& outgoing);

// Input fields.

SamplingContext& get_sampling_context();
const foundation::Vector3d& get_geometric_normal() const;
const foundation::Vector3d& get_shading_normal() const;
const foundation::Vector3d& get_outgoing_vector() const;
Expand Down Expand Up @@ -95,7 +93,6 @@ class BSDFSample
void apply_pdf_differentials_heuristic();

const ShadingPoint& m_shading_point; // shading point at which the sampling is done
SamplingContext& m_sampling_context; // sampling context used to sample BSDFs
foundation::Dual3d m_outgoing; // world space outgoing direction, unit-length
ScatteringMode::Mode m_mode; // scattering mode
foundation::Dual3d m_incoming; // world space incoming direction, unit-length
Expand All @@ -110,22 +107,15 @@ class BSDFSample

inline BSDFSample::BSDFSample(
const ShadingPoint& shading_point,
SamplingContext& sampling_context,
const foundation::Dual3d& outgoing)
: m_shading_point(shading_point)
, m_sampling_context(sampling_context)
, m_outgoing(outgoing)
, m_mode(ScatteringMode::Absorption)
, m_value(0.0f)
, m_probability(0.0)
{
}

inline SamplingContext& BSDFSample::get_sampling_context()
{
return m_sampling_context;
}

inline const foundation::Vector3d& BSDFSample::get_geometric_normal() const
{
return m_shading_point.get_geometric_normal();
Expand Down
3 changes: 3 additions & 0 deletions src/appleseed/renderer/modeling/bsdf/bsdfwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class BSDFWrapper
const ParamArray& params);

virtual void sample(
SamplingContext& sampling_context,
const void* data,
const bool adjoint,
const bool cosine_mult,
Expand Down Expand Up @@ -104,6 +105,7 @@ BSDFWrapper<BSDFImpl>::BSDFWrapper(

template <typename BSDFImpl>
void BSDFWrapper<BSDFImpl>::sample(
SamplingContext& sampling_context,
const void* data,
const bool adjoint,
const bool cosine_mult,
Expand All @@ -113,6 +115,7 @@ void BSDFWrapper<BSDFImpl>::sample(
assert(foundation::is_normalized(sample.get_outgoing_vector()));

BSDFImpl::sample(
sampling_context,
data,
adjoint,
false,
Expand Down
5 changes: 3 additions & 2 deletions src/appleseed/renderer/modeling/bsdf/diffusebtdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,15 @@ namespace
}

FORCE_INLINE virtual void sample(
SamplingContext& sampling_context,
const void* data,
const bool adjoint,
const bool cosine_mult,
BSDFSample& sample) const
{
// Compute the incoming direction in local space.
sample.get_sampling_context().split_in_place(2, 1);
const Vector2d s = sample.get_sampling_context().next_vector2<2>();
sampling_context.split_in_place(2, 1);
const Vector2d s = sampling_context.next_vector2<2>();
const Vector3d wi = sample_hemisphere_cosine(s);

// Transform the incoming direction to parent space.
Expand Down
31 changes: 23 additions & 8 deletions src/appleseed/renderer/modeling/bsdf/disneybrdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,13 @@ namespace
{
public:
void sample(
SamplingContext& sampling_context,
const DisneyBRDFInputValues* values,
BSDFSample& sample) const
{
// Compute the incoming direction in local space.
sample.get_sampling_context().split_in_place(2, 1);
const Vector2d s = sample.get_sampling_context().next_vector2<2>();
sampling_context.split_in_place(2, 1);
const Vector2d s = sampling_context.next_vector2<2>();
const Vector3d wi = sample_hemisphere_cosine(s);

// Transform the incoming direction to parent space.
Expand Down Expand Up @@ -241,12 +242,13 @@ namespace
{
public:
void sample(
SamplingContext& sampling_context,
const DisneyBRDFInputValues* values,
BSDFSample& sample) const
{
// Compute the incoming direction in local space.
sample.get_sampling_context().split_in_place(2, 1);
const Vector2d s = sample.get_sampling_context().next_vector2<2>();
sampling_context.split_in_place(2, 1);
const Vector2d s = sampling_context.next_vector2<2>();
const Vector3d wi = sample_hemisphere_uniform(s);

// Transform the incoming direction to parent space.
Expand Down Expand Up @@ -375,6 +377,7 @@ namespace
}

virtual void sample(
SamplingContext& sampling_context,
const void* data,
const bool adjoint,
const bool cosine_mult,
Expand All @@ -387,19 +390,30 @@ namespace
compute_component_cdf(values, cdf);

// Choose which of the components to sample.
sample.get_sampling_context().split_in_place(1, 1);
const double s = sample.get_sampling_context().next_double2();
sampling_context.split_in_place(1, 1);
const double s = sampling_context.next_double2();

if (s < cdf[DiffuseComponent])
DisneyDiffuseComponent().sample(values, sample);
{
DisneyDiffuseComponent().sample(
sampling_context,
values,
sample);
}
else if (s < cdf[SheenComponent])
DisneySheenComponent().sample(values, sample);
{
DisneySheenComponent().sample(
sampling_context,
values,
sample);
}
else if (s < cdf[SpecularComponent])
{
double alpha_x, alpha_y;
specular_roughness(values, alpha_x, alpha_y);
const GGXMDF<double> ggx_mdf;
MicrofacetBRDFHelper<double>::sample(
sampling_context,
ggx_mdf,
alpha_x,
alpha_y,
Expand All @@ -413,6 +427,7 @@ namespace
const double alpha = clearcoat_roughness(values);
const BerryMDF<double> berry_mdf;
MicrofacetBRDFHelper<double>::sample(
sampling_context,
berry_mdf,
alpha,
alpha,
Expand Down
2 changes: 2 additions & 0 deletions src/appleseed/renderer/modeling/bsdf/disneylayeredbrdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ void DisneyLayeredBRDF::evaluate_inputs(
}

void DisneyLayeredBRDF::sample(
SamplingContext& sampling_context,
const void* data,
const bool adjoint,
const bool cosine_mult,
Expand All @@ -147,6 +148,7 @@ void DisneyLayeredBRDF::sample(
return;

m_brdf->sample(
sampling_context,
data,
adjoint,
cosine_mult,
Expand Down
1 change: 1 addition & 0 deletions src/appleseed/renderer/modeling/bsdf/disneylayeredbrdf.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class DisneyLayeredBRDF
const size_t offset = 0) const APPLESEED_OVERRIDE;

virtual void sample(
SamplingContext& sampling_context,
const void* data,
const bool adjoint,
const bool cosine_mult,
Expand Down
5 changes: 3 additions & 2 deletions src/appleseed/renderer/modeling/bsdf/kelemenbrdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ namespace
}

FORCE_INLINE virtual void sample(
SamplingContext& sampling_context,
const void* data,
const bool adjoint,
const bool cosine_mult,
Expand Down Expand Up @@ -244,8 +245,8 @@ namespace
const double matte_prob = average_value(matte_albedo);

// Generate a uniform sample in [0,1)^3.
sample.get_sampling_context().split_in_place(3, 1);
const Vector3d s = sample.get_sampling_context().next_vector2<3>();
sampling_context.split_in_place(3, 1);
const Vector3d s = sampling_context.next_vector2<3>();

ScatteringMode::Mode mode;
Vector3d H, incoming;
Expand Down
5 changes: 3 additions & 2 deletions src/appleseed/renderer/modeling/bsdf/lambertianbrdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,15 @@ namespace
}

FORCE_INLINE virtual void sample(
SamplingContext& sampling_context,
const void* data,
const bool adjoint,
const bool cosine_mult,
BSDFSample& sample) const
{
// Compute the incoming direction in local space.
sample.get_sampling_context().split_in_place(2, 1);
const Vector2d s = sample.get_sampling_context().next_vector2<2>();
sampling_context.split_in_place(2, 1);
const Vector2d s = sampling_context.next_vector2<2>();
const Vector3d wi = sample_hemisphere_cosine(s);

// Transform the incoming direction to parent space.
Expand Down
5 changes: 5 additions & 0 deletions src/appleseed/renderer/modeling/bsdf/microfacetbrdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ namespace
}

FORCE_INLINE virtual void sample(
SamplingContext& sampling_context,
const void* data,
const bool adjoint,
const bool cosine_mult,
Expand All @@ -171,6 +172,7 @@ namespace
const double e = glossiness_to_blinn_exponent(glossiness);
const BlinnMDF<double> blinn_mdf;
MicrofacetBRDFHelper<double>::sample(
sampling_context,
blinn_mdf,
e,
e,
Expand All @@ -186,6 +188,7 @@ namespace
const double a = glossiness_to_roughness(glossiness);
const BeckmannMDF<double> beckmann_mdf;
MicrofacetBRDFHelper<double>::sample(
sampling_context,
beckmann_mdf,
a,
a,
Expand All @@ -201,6 +204,7 @@ namespace
const double a = glossiness_to_roughness(glossiness);
const WardMDF<double> ward_mdf;
MicrofacetBRDFHelper<double>::sample(
sampling_context,
ward_mdf,
a,
a,
Expand All @@ -216,6 +220,7 @@ namespace
const double a = glossiness_to_roughness(glossiness);
const GGXMDF<double> ggx_mdf;
MicrofacetBRDFHelper<double>::sample(
sampling_context,
ggx_mdf,
a,
a,
Expand Down
5 changes: 3 additions & 2 deletions src/appleseed/renderer/modeling/bsdf/microfacetbrdfhelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class MicrofacetBRDFHelper

template <typename MDF, typename FresnelFun>
static void sample(
SamplingContext& sampling_context,
const MDF& mdf,
const T alpha_x,
const T alpha_y,
Expand All @@ -67,8 +68,8 @@ class MicrofacetBRDFHelper
return;

// Compute the incoming direction by sampling the MDF.
sample.get_sampling_context().split_in_place(3, 1);
const VectorType s = sample.get_sampling_context().next_vector2<3>();
sampling_context.split_in_place(3, 1);
const VectorType s = sampling_context.next_vector2<3>();
const VectorType wo = sample.get_shading_basis().transform_to_local(sample.get_outgoing_vector());
const VectorType m = mdf.sample(wo, s, alpha_x, alpha_y);
const VectorType h = sample.get_shading_basis().transform_to_parent(m);
Expand Down
Loading

0 comments on commit e76b494

Please sign in to comment.