Skip to content

Commit

Permalink
remove util/u64.h and use luisa::ulong instead (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike-Leo-Smith committed Jun 2, 2024
1 parent e0dbb60 commit 163bb6f
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 407 deletions.
107 changes: 54 additions & 53 deletions src/integrators/mega_vpt.cpp

Large diffs are not rendered by default.

83 changes: 42 additions & 41 deletions src/integrators/mega_vpt_naive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class MegakernelVolumePathTracingNaiveInstance final : public ProgressiveIntegra
[[nodiscard]] UInt _event(const SampledWavelengths &swl, luisa::shared_ptr<Interaction> it, Expr<float> time,
Expr<float3> wo, Expr<float3> wi) const noexcept {
Float3 wo_local, wi_local;
$if(it->shape().has_surface()) {
$if (it->shape().has_surface()) {
PolymorphicCall<Surface::Closure> call;
pipeline().surfaces().dispatch(it->shape().surface_tag(), [&](auto surface) noexcept {
surface->closure(call, *it, swl, wo, 1.f, time);
Expand Down Expand Up @@ -105,11 +105,11 @@ class MegakernelVolumePathTracingNaiveInstance final : public ProgressiveIntegra
transmittance.pdf = 0.f;

// trace shadow ray
$while(any(transmittance.f > 0.f)) {
$while (any(transmittance.f > 0.f)) {
auto it = pipeline().geometry()->intersect(ray);

// end tracing
$if(!it->valid()) { $break; };
$if (!it->valid()) { $break; };

auto t2surface = length(it->p() - ray->origin());
auto has_medium = it->shape().has_medium();
Expand All @@ -120,7 +120,7 @@ class MegakernelVolumePathTracingNaiveInstance final : public ProgressiveIntegra
auto surface_event = _event(swl, it, time, wo, wi);

// transmittance through medium
$if(!medium_tracker.vacuum()) {
$if (!medium_tracker.vacuum()) {
pipeline().media().dispatch(medium_tracker.current().medium_tag, [&](auto medium) {
medium_priority = medium->priority();
auto closure = medium->closure(ray, swl, time);
Expand All @@ -131,12 +131,12 @@ class MegakernelVolumePathTracingNaiveInstance final : public ProgressiveIntegra
};

// update medium tracker
$if(has_medium) {
$if (has_medium) {
pipeline().media().dispatch(medium_tag, [&](auto medium) {
medium_priority = medium->priority();
});
auto medium_info = make_medium_info(medium_priority, medium_tag);
$if(surface_event == Surface::event_exit) {
$if (surface_event == Surface::event_exit) {
medium_tracker.exit(medium_priority, medium_info);
}
$else {
Expand All @@ -145,7 +145,7 @@ class MegakernelVolumePathTracingNaiveInstance final : public ProgressiveIntegra
};

// hit solid/transmissive surface
$if(it->shape().has_surface()) {
$if (it->shape().has_surface()) {
auto surface_tag = it->shape().surface_tag();
PolymorphicCall<Surface::Closure> call;
pipeline().surfaces().dispatch(surface_tag, [&](auto surface) noexcept {
Expand Down Expand Up @@ -182,7 +182,8 @@ class MegakernelVolumePathTracingNaiveInstance final : public ProgressiveIntegra
MediumTracker medium_tracker;

// Initialize RNG for sampling the majorant transmittance
PCG32 rng(U64(as<UInt2>(sampler()->generate_2d())));
auto u = as<uint2>(sampler()->generate_2d());
PCG32 rng{(cast<ulong>(u.x) << 32ull) | cast<ulong>(u.y)};

// initialize medium tracker
auto env_medium_tag = pipeline().environment_medium_tag();
Expand All @@ -194,13 +195,13 @@ class MegakernelVolumePathTracingNaiveInstance final : public ProgressiveIntegra
#ifdef VPT_NAIVE_ENABLE_MEDIUM_STACK_INIT
// TODO: bug in initialization of medium tracker where the angle between shared edge is small
auto depth_track = def<uint>(0u);
$while(true) {
$while (true) {
auto it = pipeline().geometry()->intersect(ray);
$if(!it->valid()) { $break; };
$if (!it->valid()) { $break; };

device_log("depth={}", depth_track);

$if(it->shape().has_medium()) {
$if (it->shape().has_medium()) {
auto surface_tag = it->shape().surface_tag();
auto medium_tag = it->shape().medium_tag();

Expand All @@ -215,13 +216,13 @@ class MegakernelVolumePathTracingNaiveInstance final : public ProgressiveIntegra
pipeline().surfaces().dispatch(surface_tag, [&](auto surface) {
device_log("surface event={}", surface_event);
// update medium tracker
$switch(surface_event) {
$case(Surface::event_enter) {
$switch (surface_event) {
$case (Surface::event_enter) {
medium_tracker.enter(medium_priority, medium_info);
device_log("enter: priority={}, medium_tag={}", medium_priority, medium_tag);
};
$case(Surface::event_exit) {
$if(medium_tracker.exist(medium_priority, medium_info)) {
$case (Surface::event_exit) {
$if (medium_tracker.exist(medium_priority, medium_info)) {
medium_tracker.exit(medium_priority, medium_info);
device_log("exit exist: priority={}, medium_tag={}", medium_priority, medium_tag);
}
Expand Down Expand Up @@ -251,10 +252,10 @@ class MegakernelVolumePathTracingNaiveInstance final : public ProgressiveIntegra
auto pdf_bsdf = def(1e16f);
auto eta_scale = def(1.f);
auto max_depth = node<MegakernelVolumePathTracingNaive>()->max_depth();
$for(depth, max_depth) {
$for (depth, max_depth) {
auto eta = def(1.f);
auto u_rr = def(0.f);
$if(depth + 1u >= rr_depth) { u_rr = sampler()->generate_1d(); };
$if (depth + 1u >= rr_depth) { u_rr = sampler()->generate_1d(); };

// trace
auto it = pipeline().geometry()->intersect(ray);
Expand All @@ -263,7 +264,7 @@ class MegakernelVolumePathTracingNaiveInstance final : public ProgressiveIntegra

device_log("depth={}", depth);
device_log("before: medium tracker size={}, priority={}, tag={}",
medium_tracker.size(), medium_tracker.current().priority, medium_tracker.current().medium_tag);
medium_tracker.size(), medium_tracker.current().priority, medium_tracker.current().medium_tag);
device_log(
"ray=({}, {}, {}) + t * ({}, {}, {})",
ray->origin().x, ray->origin().y, ray->origin().z,
Expand All @@ -272,7 +273,7 @@ class MegakernelVolumePathTracingNaiveInstance final : public ProgressiveIntegra

auto medium_sample = Medium::Sample::zero(swl.dimension());
// sample the participating medium
$if(!medium_tracker.vacuum()) {
$if (!medium_tracker.vacuum()) {
#ifdef VPT_NAIVE_ENABLE_DIRECT_LIGHTING
// direct light
// generate uniform samples
Expand All @@ -286,7 +287,7 @@ class MegakernelVolumePathTracingNaiveInstance final : public ProgressiveIntegra

// trace shadow ray
auto transmittance_evaluation = _transmittance(frame_index, pixel_id, time, swl, rng, medium_tracker, light_sample.shadow_ray);
$if(transmittance_evaluation.pdf > 0.f) {
$if (transmittance_evaluation.pdf > 0.f) {
auto w = 1.f / (pdf_bsdf + transmittance_evaluation.pdf + light_sample.eval.pdf);
Li += w * beta * transmittance_evaluation.f * light_sample.eval.L;
};
Expand All @@ -310,9 +311,9 @@ class MegakernelVolumePathTracingNaiveInstance final : public ProgressiveIntegra
};

// sample the surface
$if((medium_sample.medium_event == Medium::event_invalid) | (medium_sample.medium_event == Medium::event_hit_surface)) {
$if ((medium_sample.medium_event == Medium::event_invalid) | (medium_sample.medium_event == Medium::event_hit_surface)) {
// miss, environment light
$if(!it->valid()) {
$if (!it->valid()) {
if (pipeline().environment()) {
auto eval = light_sampler()->evaluate_miss(ray->direction(), swl, time);
Li += beta * eval.L * balance_heuristic(pdf_bsdf, eval.pdf);
Expand All @@ -322,7 +323,7 @@ class MegakernelVolumePathTracingNaiveInstance final : public ProgressiveIntegra

// hit light
if (!pipeline().lights().empty()) {
$if(it->shape().has_light()) {
$if (it->shape().has_light()) {
auto eval = light_sampler()->evaluate_hit(*it, ray->origin(), swl, time);
Li += beta * eval.L * balance_heuristic(pdf_bsdf, eval.pdf);
device_log(
Expand All @@ -341,7 +342,7 @@ class MegakernelVolumePathTracingNaiveInstance final : public ProgressiveIntegra
}

// hit ordinary surface
$if(!it->shape().has_surface()) { $break; };
$if (!it->shape().has_surface()) { $break; };

// generate uniform samples
auto u_light_selection = sampler()->generate_1d();
Expand All @@ -363,7 +364,7 @@ class MegakernelVolumePathTracingNaiveInstance final : public ProgressiveIntegra
auto medium_tag = it->shape().medium_tag();
auto medium_priority = def(Medium::VACUUM_PRIORITY);
auto eta_next = def(1.f);
$if(has_medium) {
$if (has_medium) {
pipeline().media().dispatch(medium_tag, [&](auto medium) {
auto closure = medium->closure(ray, swl, time);
medium_priority = medium->priority();
Expand All @@ -384,21 +385,21 @@ class MegakernelVolumePathTracingNaiveInstance final : public ProgressiveIntegra
});
call.execute([&](auto closure) noexcept {
UInt surface_event;
$if(!medium_tracker.true_hit(medium_info.medium_tag)) {
$if (!medium_tracker.true_hit(medium_info.medium_tag)) {
surface_event = surface_event_skip;
ray = it->spawn_ray(ray->direction());
pdf_bsdf = 1e16f;
}
$else {
if (auto dispersive = closure->is_dispersive()) {
$if(*dispersive) { swl.terminate_secondary(); };
$if (*dispersive) { swl.terminate_secondary(); };
}

// direct lighting
#ifdef VPT_NAIVE_ENABLE_DIRECT_LIGHTING
$if(light_sample.eval.pdf > 0.0f) {
$if (light_sample.eval.pdf > 0.0f) {
#else
$if(light_sample.eval.pdf > 0.0f & !occluded) {
$if (light_sample.eval.pdf > 0.0f & !occluded) {
#endif
auto wi = light_sample.shadow_ray->direction();
auto eval = closure->evaluate(wo, wi);
Expand Down Expand Up @@ -433,24 +434,24 @@ class MegakernelVolumePathTracingNaiveInstance final : public ProgressiveIntegra
beta *= w * surface_sample.eval.f;

// apply eta scale & update medium tracker
$if(has_medium) {
$switch(surface_event) {
$case(Surface::event_enter) {
$if (has_medium) {
$switch (surface_event) {
$case (Surface::event_enter) {
eta_scale = sqr(eta_next / eta);
};
$case(Surface::event_exit) {
$case (Surface::event_exit) {
eta_scale = sqr(eta / eta_next);
};
};
};
};

$if(has_medium) {
$switch(surface_event) {
$case(Surface::event_enter) {
$if (has_medium) {
$switch (surface_event) {
$case (Surface::event_enter) {
medium_tracker.enter(medium_priority, medium_info);
};
$case(Surface::event_exit) {
$case (Surface::event_exit) {
medium_tracker.exit(medium_priority, medium_info);
};
};
Expand All @@ -467,18 +468,18 @@ class MegakernelVolumePathTracingNaiveInstance final : public ProgressiveIntegra
medium_sample.medium_event, beta[0u], beta[1u], beta[2u], pdf_bsdf, Li[0u], Li[1u], Li[2u]);

beta = zero_if_any_nan(beta);
$if(all(beta <= 0.f)) { $break; };
$if (all(beta <= 0.f)) { $break; };
// rr
auto rr_threshold = node<MegakernelVolumePathTracingNaive>()->rr_threshold();
auto q = max(beta.max() * eta_scale, .05f);
$if(depth + 1u >= rr_depth) {
$if(q < rr_threshold & u_rr >= q) { $break; };
$if (depth + 1u >= rr_depth) {
$if (q < rr_threshold & u_rr >= q) { $break; };
beta *= ite(q < rr_threshold, 1.0f / q, 1.f);
};

device_log("beta=({}, {}, {})", beta[0u], beta[1u], beta[2u]);
device_log("after: medium tracker size={}, priority={}, tag={}",
medium_tracker.size(), medium_tracker.current().priority, medium_tracker.current().medium_tag);
medium_tracker.size(), medium_tracker.current().priority, medium_tracker.current().medium_tag);
};
return spectrum->srgb(swl, Li);
}
Expand Down
Loading

0 comments on commit 163bb6f

Please sign in to comment.