Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 2 additions & 23 deletions crates/bevy_solari/src/pathtracer/pathtracer.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ enable wgpu_ray_query;
#import bevy_pbr::utils::{rand_f, rand_vec2f}
#import bevy_render::maths::{PI, orthonormalize}
#import bevy_render::view::View
#import bevy_solari::brdf::{evaluate_brdf, evaluate_and_sample_brdf, fresnel}
#import bevy_solari::brdf::{evaluate_brdf, evaluate_and_sample_brdf, brdf_pdf, lobe_reflectances}
Comment thread
dylansechet marked this conversation as resolved.
Outdated
#import bevy_solari::sampling::{sample_random_light, random_emissive_light_pdf, ggx_vndf_pdf, power_heuristic}
#import bevy_solari::scene_bindings::{trace_ray, resolve_ray_hit_full, ResolvedRayHitFull, RAY_T_MIN, RAY_T_MAX, MIRROR_ROUGHNESS_THRESHOLD}

Expand Down Expand Up @@ -62,7 +62,7 @@ fn pathtrace(@builtin(global_invocation_id) global_id: vec3<u32>) {

mis_weight = 1.0;
if direct_lighting.brdf_rays_can_hit {
let pdf_of_bounce = brdf_pdf(wo, direct_lighting.wi, ray_hit);
let pdf_of_bounce = brdf_pdf(wo, direct_lighting.wi, ray_hit.world_normal, ray_hit.material);
mis_weight = power_heuristic(1.0 / direct_lighting.inverse_pdf, pdf_of_bounce);
}

Expand Down Expand Up @@ -95,24 +95,3 @@ fn pathtrace(@builtin(global_invocation_id) global_id: vec3<u32>) {
textureStore(view_output, global_id.xy, vec4(new_color, 1.0));
}

fn brdf_pdf(wo: vec3<f32>, wi: vec3<f32>, ray_hit: ResolvedRayHitFull) -> f32 {
let NdotV = max(dot(ray_hit.world_normal, wo), 0.0001);
let F0 = calculate_F0(ray_hit.material.base_color, ray_hit.material.metallic, vec3(ray_hit.material.reflectance));
let df = 1.0 - luminance(fresnel(F0, NdotV));

let diffuse_weight = mix(df, 0.0, ray_hit.material.metallic);
let specular_weight = 1.0 - diffuse_weight;

let TBN = orthonormalize(ray_hit.world_normal);
let T = TBN[0];
let B = TBN[1];
let N = TBN[2];

let wo_tangent = vec3(dot(wo, T), dot(wo, B), dot(wo, N));
let wi_tangent = vec3(dot(wi, T), dot(wi, B), dot(wi, N));

let diffuse_pdf = wi_tangent.z / PI;
let specular_pdf = ggx_vndf_pdf(wo_tangent, wi_tangent, ray_hit.material.roughness);
let pdf = (diffuse_weight * diffuse_pdf) + (specular_weight * specular_pdf);
return pdf;
}
75 changes: 55 additions & 20 deletions crates/bevy_solari/src/scene/brdf.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ enable wgpu_ray_query;

#import bevy_core_pipeline::tonemapping::tonemapping_luminance as luminance
#import bevy_pbr::lighting::{D_GGX, V_SmithGGXCorrelated, specular_multiscatter}
#import bevy_pbr::pbr_functions::{calculate_diffuse_color, calculate_F0}
#import bevy_pbr::pbr_functions::calculate_F0
#import bevy_pbr::utils::{rand_f, sample_cosine_hemisphere}
#import bevy_render::maths::{PI, orthonormalize}
#import bevy_solari::sampling::{sample_ggx_vndf, ggx_vndf_pdf, ggx_vndf_sample_invalid}
Expand All @@ -16,6 +16,22 @@ struct EvaluateAndSampleBrdfResult {
pdf: f32,
}

struct LobeReflectances {
specular: vec3<f32>,
diffuse: vec3<f32>,
}
Comment thread
dylansechet marked this conversation as resolved.

// Hemispherical reflectance of each lobe
fn lobe_reflectances(F0: vec3<f32>, material: ResolvedMaterial, NdotV: f32) -> LobeReflectances {
let F_ab = F_AB(material.perceptual_roughness, NdotV);
let multiscattering_factor = 1.0 / (F_ab.x + F_ab.y) - 1.0;
let rho_spec = (F0 * F_ab.x + F_ab.y) * (1.0 + F0 * multiscattering_factor);
Comment thread
dylansechet marked this conversation as resolved.
Outdated
return LobeReflectances(
rho_spec,
(1.0 - material.metallic) * (1.0 - rho_spec) * material.base_color,
);
}

fn evaluate_and_sample_brdf(
wo: vec3<f32>,
world_normal: vec3<f32>,
Expand All @@ -25,10 +41,9 @@ fn evaluate_and_sample_brdf(
let NdotV = dot(world_normal, wo);
if NdotV < 0.0001 { return EvaluateAndSampleBrdfResult(vec3(0.0), vec3(0.0), 0.0); }
let F0 = calculate_F0(material.base_color, material.metallic, vec3(material.reflectance));
let df = 1.0 - luminance(fresnel(F0, NdotV));

let diffuse_weight = mix(df, 0.0, material.metallic);
let specular_weight = 1.0 - diffuse_weight;
let rho = lobe_reflectances(F0, material, NdotV);
let specular_weight = luminance(rho.specular) / luminance(rho.specular + rho.diffuse);
let diffuse_weight = 1.0 - specular_weight;

let TBN = orthonormalize(world_normal);
let T = TBN[0];
Expand All @@ -49,19 +64,21 @@ fn evaluate_and_sample_brdf(
return EvaluateAndSampleBrdfResult(vec3(0.0), vec3(0.0), 0.0);
}
wi = wi_tangent.x * T + wi_tangent.y * B + wi_tangent.z * N;

// Mirror specular is a delta function
if material.roughness <= MIRROR_ROUGHNESS_THRESHOLD {
return EvaluateAndSampleBrdfResult(
wi,
rho.specular / specular_weight,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think rho.specular / specular_weight is right here. You need to evaluate the BRDF.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be the same thing, as the total reflected energy for a mirror is the same as the brdf evaluated for the reflected ray.
Doing a brdf evaluation does feel more readable though, and the lut at 0 roughness probably isn't that great.

bitcast<f32>(0x7F800000u) // INF
);
Comment thread
dylansechet marked this conversation as resolved.
Outdated
}
}

let diffuse_pdf = wi_tangent.z / PI;
let specular_pdf = ggx_vndf_pdf(wo_tangent, wi_tangent, material.roughness);
let pdf = (diffuse_weight * diffuse_pdf) + (specular_weight * specular_pdf);

var throughput = evaluate_brdf(wo, wi, world_normal, material);
if diffuse_selected || material.roughness > MIRROR_ROUGHNESS_THRESHOLD {
throughput /= pdf;
} else {
throughput /= specular_weight;
}

let pdf = specular_weight * specular_pdf + diffuse_weight * diffuse_pdf;
let throughput = evaluate_brdf(wo, wi, world_normal, material) / pdf;
return EvaluateAndSampleBrdfResult(wi, throughput, pdf);
}

Expand All @@ -75,15 +92,12 @@ fn evaluate_brdf(
}

fn evaluate_diffuse_brdf(wo: vec3<f32>, wi: vec3<f32>, world_normal: vec3<f32>, material: ResolvedMaterial) -> vec3<f32> {
let diffuse_color = calculate_diffuse_color(material.base_color, material.metallic, 0.0, 0.0) / PI;

let NdotL = dot(world_normal, wi);
let NdotV = dot(world_normal, wo);
if NdotL < 0.0001 || NdotV < 0.0001 { return vec3(0.0); }
let F0 = calculate_F0(material.base_color, material.metallic, vec3(material.reflectance));
let layering = (1.0 - fresnel(F0, NdotL)) * (1.0 - fresnel(F0, NdotV));

return diffuse_color * layering * NdotL;
let rho = lobe_reflectances(F0, material, NdotV);
return rho.diffuse / PI * NdotL;
}

fn evaluate_specular_brdf(wo: vec3<f32>, wi: vec3<f32>, world_normal: vec3<f32>, material: ResolvedMaterial) -> vec3<f32> {
Comment thread
dylansechet marked this conversation as resolved.
Outdated
Expand All @@ -93,7 +107,7 @@ fn evaluate_specular_brdf(wo: vec3<f32>, wi: vec3<f32>, world_normal: vec3<f32>,
let LdotH = dot(wi, H);
let NdotV = dot(world_normal, wo);
if NdotL < 0.0001 || NdotH < 0.0001 || LdotH < 0.0001 || NdotV < 0.0001 { return vec3(0.0); }

Comment thread
dylansechet marked this conversation as resolved.
Comment thread
dylansechet marked this conversation as resolved.
Outdated
let F0 = calculate_F0(material.base_color, material.metallic, vec3(material.reflectance));
let F = fresnel(F0, LdotH);

Expand All @@ -111,6 +125,27 @@ fn evaluate_specular_brdf(wo: vec3<f32>, wi: vec3<f32>, world_normal: vec3<f32>,
return specular_multiscatter(D, Vs, F, F0, F_ab, 1.0) * NdotL;
}

fn brdf_pdf(wo: vec3<f32>, wi: vec3<f32>, world_normal: vec3<f32>, material: ResolvedMaterial) -> f32 {
let NdotV = max(dot(world_normal, wo), 0.0001);
let F0 = calculate_F0(material.base_color, material.metallic, vec3(material.reflectance));
let rho = lobe_reflectances(F0, material, NdotV);
let specular_weight = luminance(rho.specular) / luminance(rho.specular + rho.diffuse);
let diffuse_weight = 1.0 - specular_weight;

let TBN = orthonormalize(world_normal);
let T = TBN[0];
let B = TBN[1];
let N = TBN[2];

let wo_tangent = vec3(dot(wo, T), dot(wo, B), dot(wo, N));
let wi_tangent = vec3(dot(wi, T), dot(wi, B), dot(wi, N));

let diffuse_pdf = wi_tangent.z / PI;
let specular_pdf = ggx_vndf_pdf(wo_tangent, wi_tangent, material.roughness);
return specular_weight * specular_pdf + diffuse_weight * diffuse_pdf;
Comment thread
dylansechet marked this conversation as resolved.
Outdated
}


Comment thread
dylansechet marked this conversation as resolved.
Outdated
fn fresnel(f0: vec3<f32>, LdotH: f32) -> vec3<f32> {
return f0 + (1.0 - f0) * pow(1.0 - LdotH, 5.0);
}
Expand Down
Loading