Skip to content

Commit

Permalink
Reformat ReSTIR_DI integrator code
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonKang130 committed Apr 19, 2024
1 parent 4d38498 commit 68df500
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/integrators/restir_di.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class ReSTIRDirectLightingInstance final : public ProgressiveIntegrator::Instanc
w2 = r.weight.total_weight * r.weight.m;
weight.m = weight.m + r.weight.m;
weight.total_weight = w1 + w2;
$if (u_sel * weight.total_weight < w2) {
$if(u_sel * weight.total_weight < w2) {
sample = r.sample;
weight.target_pdf = r.weight.target_pdf;
};
Expand All @@ -78,15 +78,16 @@ class ReSTIRDirectLightingInstance final : public ProgressiveIntegrator::Instanc
auto w1 = weight.total_weight * m1, w2 = r.weight.total_weight * m2;
weight.m = weight.m + r.weight.m;
weight.total_weight = w1 + w2;
$if (u_sel * weight.total_weight < w2) {
$if(u_sel * weight.total_weight < w2) {
sample = r.sample;
weight.target_pdf = r.weight.target_pdf;
};
}
};

private:
[[nodiscard]] std::pair<SampledSpectrum, Float> _evaluate_without_occlusion(const ReservoirSample &sample, const Interaction &it, Expr<float3> wo,
const SampledWavelengths &swl, Expr<float> time) const noexcept {
const SampledWavelengths &swl, Expr<float> time) const noexcept {
auto L = SampledSpectrum{swl.dimension(), 0.f};
auto pdf = def(0.f);
auto prob = light_sampler()->evaluate_selection(sample.tag, it.p(), swl, time);
Expand All @@ -108,7 +109,7 @@ class ReSTIRDirectLightingInstance final : public ProgressiveIntegrator::Instanc
return std::make_pair(L, pdf);
}
[[nodiscard]] std::pair<SampledSpectrum, Float> _evaluate_with_occlusion(const ReservoirSample &sample, const Interaction &it, Expr<float3> wo,
const SampledWavelengths &swl, Expr<float> time) const noexcept {
const SampledWavelengths &swl, Expr<float> time) const noexcept {
auto L = SampledSpectrum{swl.dimension(), 0.f};
auto pdf = def(0.f);
auto prob = light_sampler()->evaluate_selection(sample.tag, it.p(), swl, time);
Expand All @@ -130,13 +131,15 @@ class ReSTIRDirectLightingInstance final : public ProgressiveIntegrator::Instanc
});
return std::make_pair(L, pdf);
}

public:
class VisibilityBuffer {
private:
uint2 _resolution;
Buffer<float> _weight;
Buffer<Ray> _ray;
Buffer<Hit> _hit;

public:
VisibilityBuffer(const Pipeline &pipeline, uint2 resolution) noexcept
: _resolution{resolution} {
Expand Down Expand Up @@ -169,6 +172,7 @@ class ReSTIRDirectLightingInstance final : public ProgressiveIntegrator::Instanc
uint2 _resolution;
Buffer<uint3> _sample;
Buffer<float3> _weight;

public:
ReservoirBuffer(const Pipeline &pipeline, uint2 resolution) noexcept
: _resolution{resolution} {
Expand Down Expand Up @@ -223,10 +227,9 @@ class ReSTIRDirectLightingInstance final : public ProgressiveIntegrator::Instanc
auto sel = light_sampler()->select(*it, u_sel, swl, time);
auto u_light_selection = sampler()->generate_2d();
auto target_pdf = def(0.f), total_weight = def(0.f);
Reservoir candidate {
Reservoir candidate{
ReservoirSample{sel.tag, u_light_selection},
ReservoirWeight{1.f, total_weight, target_pdf}
};
ReservoirWeight{1.f, total_weight, target_pdf}};
auto [L, pdf] = _evaluate_without_occlusion(candidate.sample, *it, wo, swl, time);
candidate.weight.target_pdf = L.sum();
candidate.weight.total_weight = ite(pdf == 0.f, 0.f, candidate.weight.target_pdf / pdf);
Expand Down Expand Up @@ -320,13 +323,14 @@ class ReSTIRDirectLightingInstance final : public ProgressiveIntegrator::Instanc
$outline {
$if(pass_index % 2u == 0u) {
reservoir = _spatial_reservoir_buffer->read(pixel_id);
} $else {
}
$else {
reservoir = _temporal_reservoir_buffer->read(pixel_id);
};
auto camera_to_world = camera->camera_to_world();
auto world_to_camera = inverse(camera_to_world);
auto current_pixel_depth = (world_to_camera * make_float4(it->p(), 1.f)).z;
$for (_, num_neighbor_sample) {
$for(_, num_neighbor_sample) {
auto u_radius = sampler()->generate_1d(), u_theta = sampler()->generate_1d();
auto radius = neighbor_radius * u_radius * u_radius;
auto theta = 2.f * pi * u_theta;
Expand All @@ -342,7 +346,8 @@ class ReSTIRDirectLightingInstance final : public ProgressiveIntegrator::Instanc
auto neighbor_reservoir = Reservoir::zero();
$if(pass_index % 2u == 0u) {
neighbor_reservoir = _spatial_reservoir_buffer->read(neighbor_id);
} $else {
}
$else {
neighbor_reservoir = _temporal_reservoir_buffer->read(neighbor_id);
};
$if(dsl::isnan(neighbor_reservoir.weight.total_weight) | dsl::isnan(neighbor_reservoir.weight.target_pdf)) { $continue; };
Expand All @@ -361,10 +366,12 @@ class ReSTIRDirectLightingInstance final : public ProgressiveIntegrator::Instanc
};
$if(pass_index % 2u == 0u) {
_temporal_reservoir_buffer->write(reservoir, pixel_id);
} $else {
}
$else {
_spatial_reservoir_buffer->write(reservoir, pixel_id);
};
}

protected:
void _render_one_camera(CommandBuffer &command_buffer,
Camera::Instance *camera) noexcept override {
Expand Down Expand Up @@ -419,7 +426,8 @@ class ReSTIRDirectLightingInstance final : public ProgressiveIntegrator::Instanc
auto pixel_id = dispatch_id().xy();
$if(pass_index % 2u == 0u) {
_spatial_reuse(pass_index, camera, frame_index, pixel_id, time);
} $else {
}
$else {
_spatial_reuse(pass_index, camera, frame_index, pixel_id, time);
};
};
Expand Down

0 comments on commit 68df500

Please sign in to comment.