use super::*;
#[must_use]
pub fn wang_hash(seed: u32) -> u32 {
let mut x: u32 = (seed ^ 61) ^ (seed >> 16);
x = x.wrapping_mul(9);
x ^= x >> 4;
x = x.wrapping_mul(0x27d4_eb2d);
x ^ (x >> 15)
}
#[must_use]
pub fn rgb_luminance(color: Vec3) -> f32 {
0.2126 * color.get_x() + 0.7152 * color.get_y() + 0.0722 * color.get_z()
}
#[must_use]
pub fn sanitize_radiance(color: Vec3, max_luminance: f32) -> Vec3 {
let r: f32 = if color.get_x().is_finite() {
color.get_x().max(0.0)
} else {
0.0
};
let g: f32 = if color.get_y().is_finite() {
color.get_y().max(0.0)
} else {
0.0
};
let b: f32 = if color.get_z().is_finite() {
color.get_z().max(0.0)
} else {
0.0
};
let clean: Vec3 = Vec3::new(r, g, b);
if max_luminance > 0.0 {
let luma: f32 = rgb_luminance(clean);
if luma > max_luminance && luma.is_finite() {
return clean.scale(max_luminance / luma);
}
}
clean
}
#[must_use]
pub fn fresnel_schlick(cos_theta: f32, f0: Vec3) -> Vec3 {
let m: f32 = (1.0 - cos_theta.clamp(0.0, 1.0)).clamp(0.0, 1.0);
let m2: f32 = m * m;
let m5: f32 = m2 * m2 * m;
Vec3::new(
(f0.get_x() + (1.0 - f0.get_x()) * m5).clamp(0.0, 1.0),
(f0.get_y() + (1.0 - f0.get_y()) * m5).clamp(0.0, 1.0),
(f0.get_z() + (1.0 - f0.get_z()) * m5).clamp(0.0, 1.0),
)
}
#[must_use]
pub fn fresnel_dielectric(cos_theta_i: f32, eta_i: f32, eta_t: f32) -> f32 {
let cos_i: f32 = cos_theta_i.clamp(0.0, 1.0);
let eta: f32 = eta_i / eta_t.max(1e-4);
let sin_t_sq: f32 = eta * eta * (1.0 - cos_i * cos_i).max(0.0);
if sin_t_sq >= 1.0 {
return 1.0;
}
let cos_t: f32 = (1.0 - sin_t_sq).max(0.0).sqrt();
let r_par_num: f32 = eta_t * cos_i - eta_i * cos_t;
let r_par_den: f32 = (eta_t * cos_i + eta_i * cos_t).max(RT_MIN_PDF);
let r_perp_num: f32 = eta_i * cos_i - eta_t * cos_t;
let r_perp_den: f32 = (eta_i * cos_i + eta_t * cos_t).max(RT_MIN_PDF);
let r_par: f32 = r_par_num / r_par_den;
let r_perp: f32 = r_perp_num / r_perp_den;
(0.5 * (r_par * r_par + r_perp * r_perp)).clamp(0.0, 1.0)
}
#[must_use]
pub fn refract_snell(incident: Vec3, normal: Vec3, eta: f32) -> Option<Vec3> {
let cos_i: f32 = (-incident.dot(normal)).clamp(0.0, 1.0);
let sin_t_sq: f32 = eta * eta * (1.0 - cos_i * cos_i).max(0.0);
if sin_t_sq >= 1.0 {
return None;
}
let cos_t: f32 = (1.0 - sin_t_sq).max(0.0).sqrt();
let refracted: Vec3 = incident.scale(eta).add(normal.scale(eta * cos_i - cos_t));
if refracted.length_sq() > RT_MIN_PDF {
Some(refracted.normalize())
} else {
None
}
}
#[must_use]
pub fn ggx_d(n_dot_h: f32, alpha: f32) -> f32 {
let a: f32 = alpha.max(RT_MIN_ALPHA);
let a2: f32 = a * a;
let nh: f32 = n_dot_h.clamp(0.0, 1.0);
let denom: f32 = nh * nh * (a2 - 1.0) + 1.0;
a2 / (PI * denom * denom).max(RT_MIN_PDF)
}
#[must_use]
pub fn ggx_lambda(cos_theta: f32, alpha: f32) -> f32 {
let c: f32 = cos_theta.abs().clamp(1e-4, 1.0);
let a: f32 = alpha.max(RT_MIN_ALPHA);
let tan2: f32 = (1.0 - c * c).max(0.0) / (c * c);
0.5 * ((1.0 + a * a * tan2).sqrt() - 1.0)
}
#[must_use]
pub fn ggx_g1(n_dot_v: f32, alpha: f32) -> f32 {
if n_dot_v <= 0.0 {
return 0.0;
}
1.0 / (1.0 + ggx_lambda(n_dot_v, alpha))
}
#[must_use]
pub fn ggx_g2(n_dot_v: f32, n_dot_l: f32, alpha: f32) -> f32 {
if n_dot_v <= 0.0 || n_dot_l <= 0.0 {
return 0.0;
}
1.0 / (1.0 + ggx_lambda(n_dot_v, alpha) + ggx_lambda(n_dot_l, alpha))
}
#[must_use]
pub fn ggx_vndf_pdf(n_dot_v: f32, n_dot_h: f32, alpha: f32) -> f32 {
if n_dot_v <= RT_MIN_PDF || n_dot_h <= RT_MIN_PDF {
return 0.0;
}
let g1: f32 = ggx_g1(n_dot_v, alpha);
let d: f32 = ggx_d(n_dot_h, alpha);
(g1 * d) / (4.0 * n_dot_v).max(RT_MIN_PDF)
}
#[must_use]
pub fn sample_ggx_vndf(view_world: Vec3, normal: Vec3, alpha: f32, xi: [f32; 2]) -> Vec3 {
let a: f32 = alpha.max(RT_MIN_ALPHA);
let (tangent, bitangent): (Vec3, Vec3) = build_orthonormal_basis(normal);
let ve_x: f32 = view_world.dot(tangent);
let ve_y: f32 = view_world.dot(bitangent);
let ve_z: f32 = view_world.dot(normal).max(1e-4);
let vh: Vec3 = Vec3::new(a * ve_x, a * ve_y, ve_z).normalize();
let lensq: f32 = vh.get_x() * vh.get_x() + vh.get_y() * vh.get_y();
let t1: Vec3 = if lensq > 1e-8 {
let inv_len: f32 = 1.0 / lensq.sqrt();
Vec3::new(-vh.get_y() * inv_len, vh.get_x() * inv_len, 0.0)
} else {
Vec3::new(1.0, 0.0, 0.0)
};
let t2: Vec3 = vh.cross(t1);
let r: f32 = xi[0].clamp(0.0, 0.999_999).sqrt();
let phi: f32 = TAU * xi[1];
let p1: f32 = r * phi.cos();
let raw_p2: f32 = r * phi.sin();
let s: f32 = 0.5 * (1.0 + vh.get_z());
let p2: f32 = (1.0 - s) * (1.0 - p1 * p1).max(0.0).sqrt() + s * raw_p2;
let nh_z: f32 = (1.0 - p1 * p1 - p2 * p2).max(0.0).sqrt();
let nh: Vec3 = t1.scale(p1).add(t2.scale(p2)).add(vh.scale(nh_z));
let ne_local: Vec3 = Vec3::new(a * nh.get_x(), a * nh.get_y(), nh.get_z().max(0.0)).normalize();
tangent
.scale(ne_local.get_x())
.add(bitangent.scale(ne_local.get_y()))
.add(normal.scale(ne_local.get_z()))
.normalize()
}
#[must_use]
pub fn sample_cosine_hemisphere(normal: Vec3, xi: [f32; 2]) -> (Vec3, f32) {
let disk: [f32; 2] = concentric_sample_disk(xi);
let z: f32 = (1.0 - disk[0] * disk[0] - disk[1] * disk[1])
.max(0.0)
.sqrt();
let (tangent, bitangent): (Vec3, Vec3) = build_orthonormal_basis(normal);
let dir: Vec3 = tangent
.scale(disk[0])
.add(bitangent.scale(disk[1]))
.add(normal.scale(z))
.normalize();
let pdf: f32 = (z / PI).max(RT_MIN_PDF);
(dir, pdf)
}
#[must_use]
pub fn mis_weight_balance(pdf_a: f32, pdf_b: f32) -> f32 {
let a: f32 = pdf_a.max(0.0);
let b: f32 = pdf_b.max(0.0);
let sum: f32 = a + b;
if sum <= RT_MIN_PDF {
0.0
} else {
(a / sum).clamp(0.0, 1.0)
}
}
#[must_use]
pub fn mis_weight_power(pdf_a: f32, pdf_b: f32) -> f32 {
let a: f32 = pdf_a.max(0.0);
let b: f32 = pdf_b.max(0.0);
let a2: f32 = a * a;
let b2: f32 = b * b;
let sum: f32 = a2 + b2;
if sum <= RT_MIN_PDF * RT_MIN_PDF {
0.0
} else {
(a2 / sum).clamp(0.0, 1.0)
}
}
#[must_use]
pub(crate) fn offset_ray_origin(point: Vec3, geometric_normal: Vec3, ray_dir: Vec3) -> Vec3 {
let sign: f32 = if ray_dir.dot(geometric_normal) >= 0.0 {
1.0
} else {
-1.0
};
point.add(geometric_normal.scale(sign * RT_SURFACE_BIAS))
}
#[must_use]
pub(crate) fn eval_oren_nayar_brdf(
albedo: Vec3,
sigma: f32,
normal: Vec3,
wo: Vec3,
wi: Vec3,
) -> Vec3 {
let n_dot_v: f32 = normal.dot(wo).max(0.0);
let n_dot_l: f32 = normal.dot(wi).max(0.0);
if n_dot_v <= RT_MIN_PDF || n_dot_l <= RT_MIN_PDF {
return Vec3::zero();
}
let s: f32 = sigma.clamp(0.0, 1.0);
if s <= 1e-4 {
return albedo.scale(1.0 / PI);
}
let a_coeff: f32 = 1.0 / (PI + (FRAC_PI_2 - (2.0 / 3.0)) * s);
let b_coeff: f32 = s * a_coeff;
let s_dot: f32 = wo.dot(wi) - n_dot_l * n_dot_v;
let t_den: f32 = if s_dot > 0.0 {
n_dot_l.max(n_dot_v).max(1e-4)
} else {
1.0
};
let factor: f32 = (a_coeff + b_coeff * (s_dot / t_den)).max(0.0);
albedo.scale(factor)
}
#[must_use]
pub(crate) fn eval_material_bsdf_and_pdf(
material: &RtMaterial,
normal: Vec3,
wo: Vec3,
wi: Vec3,
) -> (Vec3, f32) {
let n_dot_v: f32 = normal.dot(wo);
let n_dot_l: f32 = normal.dot(wi);
if n_dot_v <= RT_MIN_PDF || n_dot_l <= RT_MIN_PDF {
return (Vec3::zero(), 0.0);
}
let half_sum: Vec3 = wo.add(wi);
if half_sum.length_sq() <= RT_MIN_PDF {
return (Vec3::zero(), 0.0);
}
let h: Vec3 = half_sum.normalize();
let n_dot_h: f32 = normal.dot(h).max(0.0);
let v_dot_h: f32 = wo.dot(h).max(0.0);
match material.get_kind() {
RtMaterialKind::Emissive => (Vec3::zero(), 0.0),
RtMaterialKind::Diffuse => {
let brdf: Vec3 = eval_oren_nayar_brdf(
material.get_albedo(),
material.get_oren_nayar_sigma(),
normal,
wo,
wi,
);
(brdf, n_dot_l / PI)
}
RtMaterialKind::Metal => {
if material.is_delta_specular() {
return (Vec3::zero(), 0.0);
}
let r: f32 = material.get_roughness().clamp(0.01, 1.0);
let alpha: f32 = (r * r).max(RT_MIN_ALPHA);
let f: Vec3 = fresnel_schlick(v_dot_h, material.get_albedo());
let d: f32 = ggx_d(n_dot_h, alpha);
let g2: f32 = ggx_g2(n_dot_v, n_dot_l, alpha);
let spec: Vec3 = f.scale((d * g2) / (4.0 * n_dot_v * n_dot_l).max(RT_MIN_PDF));
let pdf: f32 = ggx_vndf_pdf(n_dot_v, n_dot_h, alpha);
(spec, pdf)
}
RtMaterialKind::Dielectric => (Vec3::zero(), 0.0),
RtMaterialKind::Principled => {
let albedo: Vec3 = material.get_albedo();
let metallic: f32 = material.get_metallic().clamp(0.0, 1.0);
let r: f32 = material.get_roughness().clamp(0.01, 1.0);
let alpha: f32 = (r * r).max(RT_MIN_ALPHA);
let ior: f32 = material.get_ior().clamp(1.001, 3.0);
let f0_scalar: f32 = ((ior - 1.0) / (ior + 1.0)).powi(2);
let f0: Vec3 = Vec3::splat(f0_scalar).lerp(albedo, metallic);
let fresnel: Vec3 = fresnel_schlick(v_dot_h, f0);
let d: f32 = ggx_d(n_dot_h, alpha);
let g2: f32 = ggx_g2(n_dot_v, n_dot_l, alpha);
let base_spec: Vec3 =
fresnel.scale((d * g2) / (4.0 * n_dot_v * n_dot_l).max(RT_MIN_PDF));
let trans: f32 = material.get_transmission().clamp(0.0, 1.0);
let kd_weight: f32 = (1.0 - metallic) * (1.0 - trans);
let kd: Vec3 = Vec3::new(
((1.0 - fresnel.get_x()) * kd_weight).max(0.0),
((1.0 - fresnel.get_y()) * kd_weight).max(0.0),
((1.0 - fresnel.get_z()) * kd_weight).max(0.0),
);
let diff_base: Vec3 =
eval_oren_nayar_brdf(albedo, material.get_oren_nayar_sigma(), normal, wo, wi);
let mut total_brdf: Vec3 = Vec3::new(
kd.get_x() * diff_base.get_x() + base_spec.get_x(),
kd.get_y() * diff_base.get_y() + base_spec.get_y(),
kd.get_z() * diff_base.get_z() + base_spec.get_z(),
);
let coat: f32 = material.get_clearcoat().clamp(0.0, 1.0);
let coat_r: f32 = material.get_clearcoat_roughness().clamp(0.01, 1.0);
let coat_alpha: f32 = (coat_r * coat_r).max(RT_MIN_ALPHA);
let mut coat_pdf: f32 = 0.0;
if coat > 1e-4 {
let coat_f: f32 = fresnel_schlick(v_dot_h, Vec3::splat(0.04)).get_x();
let coat_d: f32 = ggx_d(n_dot_h, coat_alpha);
let coat_g2: f32 = ggx_g2(n_dot_v, n_dot_l, coat_alpha);
let coat_spec: f32 =
(coat * coat_f * coat_d * coat_g2) / (4.0 * n_dot_v * n_dot_l).max(RT_MIN_PDF);
let atten: f32 = 1.0 - coat * coat_f;
total_brdf = total_brdf.scale(atten).add(Vec3::splat(coat_spec));
coat_pdf = ggx_vndf_pdf(n_dot_v, n_dot_h, coat_alpha);
}
let (p_coat, p_spec, p_diff): (f32, f32, f32) =
principled_lobe_probabilities(material, n_dot_v);
let spec_pdf: f32 = ggx_vndf_pdf(n_dot_v, n_dot_h, alpha);
let diff_pdf: f32 = n_dot_l / PI;
let combined_pdf: f32 = p_coat * coat_pdf + p_spec * spec_pdf + p_diff * diff_pdf;
(total_brdf, combined_pdf)
}
}
}
#[must_use]
pub(crate) fn principled_lobe_probabilities(
material: &RtMaterial,
n_dot_v: f32,
) -> (f32, f32, f32) {
let metallic: f32 = material.get_metallic().clamp(0.0, 1.0);
let trans: f32 = material.get_transmission().clamp(0.0, 1.0);
let coat: f32 = material.get_clearcoat().clamp(0.0, 1.0);
let w_coat: f32 = coat * 0.25;
let f0_s: f32 = ((material.get_ior() - 1.0) / (material.get_ior() + 1.0)).powi(2);
let f_approx: f32 = fresnel_schlick(n_dot_v, Vec3::splat(f0_s)).get_x();
let w_spec: f32 = (metallic + (1.0 - metallic) * f_approx).clamp(0.05, 1.0);
let w_diff: f32 = ((1.0 - metallic) * (1.0 - trans) * (1.0 - f_approx)).max(0.0);
let w_trans: f32 = ((1.0 - metallic) * trans * (1.0 - f_approx)).max(0.0);
let sum: f32 = (w_coat + w_spec + w_diff + w_trans).max(RT_MIN_PDF);
(w_coat / sum, w_spec / sum, w_diff / sum)
}
#[must_use]
pub(crate) fn sample_material_bsdf(
material: &RtMaterial,
normal: Vec3,
front_face: bool,
wo: Vec3,
rng: &mut RtRng,
) -> Option<RtBsdfSample> {
let n_dot_v: f32 = normal.dot(wo);
if n_dot_v <= RT_MIN_PDF {
return None;
}
match material.get_kind() {
RtMaterialKind::Emissive => None,
RtMaterialKind::Diffuse => {
let xi: [f32; 2] = rng.next_f32_pair();
let (wi, pdf): (Vec3, f32) = sample_cosine_hemisphere(normal, xi);
let n_dot_l: f32 = normal.dot(wi).max(0.0);
if n_dot_l <= RT_MIN_PDF {
return None;
}
let brdf: Vec3 = eval_oren_nayar_brdf(
material.get_albedo(),
material.get_oren_nayar_sigma(),
normal,
wo,
wi,
);
let weight: Vec3 = brdf.scale(n_dot_l / pdf.max(RT_MIN_PDF));
Some(RtBsdfSample::new(wi, weight, pdf, false, false))
}
RtMaterialKind::Metal => {
if material.is_delta_specular() {
let wi: Vec3 = wo.scale(-1.0).reflect(normal).normalize();
if normal.dot(wi) <= 0.0 {
return None;
}
let f: Vec3 = fresnel_schlick(n_dot_v, material.get_albedo());
return Some(RtBsdfSample::new(wi, f, 0.0, true, false));
}
let r: f32 = material.get_roughness().clamp(0.01, 1.0);
let alpha: f32 = (r * r).max(RT_MIN_ALPHA);
let xi: [f32; 2] = rng.next_f32_pair();
let h: Vec3 = sample_ggx_vndf(wo, normal, alpha, xi);
let wi: Vec3 = wo.scale(-1.0).reflect(h).normalize();
let n_dot_l: f32 = normal.dot(wi);
if n_dot_l <= RT_MIN_PDF {
return None;
}
let v_dot_h: f32 = wo.dot(h).max(0.0);
let n_dot_h: f32 = normal.dot(h).max(0.0);
let f: Vec3 = fresnel_schlick(v_dot_h, material.get_albedo());
let g1: f32 = ggx_g1(n_dot_v, alpha).max(RT_MIN_PDF);
let g2: f32 = ggx_g2(n_dot_v, n_dot_l, alpha);
let weight: Vec3 = f.scale((g2 / g1).clamp(0.0, 1.0));
let pdf: f32 = ggx_vndf_pdf(n_dot_v, n_dot_h, alpha);
Some(RtBsdfSample::new(wi, weight, pdf, false, false))
}
RtMaterialKind::Dielectric => {
let ior: f32 = material.get_ior().clamp(1.001, 3.0);
let (eta_i, eta_t): (f32, f32) = if front_face { (1.0, ior) } else { (ior, 1.0) };
let micro_n: Vec3 = if material.get_roughness() > 0.005 {
let r: f32 = material.get_roughness().clamp(0.005, 1.0);
let alpha: f32 = (r * r).max(RT_MIN_ALPHA);
let xi: [f32; 2] = rng.next_f32_pair();
sample_ggx_vndf(wo, normal, alpha, xi)
} else {
normal
};
let cos_i: f32 = wo.dot(micro_n).clamp(0.0, 1.0);
let refl_prob: f32 = fresnel_dielectric(cos_i, eta_i, eta_t);
let incident: Vec3 = wo.scale(-1.0);
let u_select: f32 = rng.next_f32();
if u_select < refl_prob {
let wi: Vec3 = incident.reflect(micro_n).normalize();
Some(RtBsdfSample::new(
wi,
material.get_albedo(),
0.0,
true,
false,
))
} else if let Some(refracted) = refract_snell(incident, micro_n, eta_i / eta_t) {
Some(RtBsdfSample::new(
refracted,
material.get_albedo(),
0.0,
true,
true,
))
} else {
let wi: Vec3 = incident.reflect(micro_n).normalize();
Some(RtBsdfSample::new(
wi,
material.get_albedo(),
0.0,
true,
false,
))
}
}
RtMaterialKind::Principled => {
let (p_coat, p_spec, p_diff): (f32, f32, f32) =
principled_lobe_probabilities(material, n_dot_v);
let u_lobe: f32 = rng.next_f32();
let xi: [f32; 2] = rng.next_f32_pair();
if u_lobe < p_coat {
let coat_r: f32 = material.get_clearcoat_roughness().clamp(0.01, 1.0);
let coat_alpha: f32 = (coat_r * coat_r).max(RT_MIN_ALPHA);
let h: Vec3 = sample_ggx_vndf(wo, normal, coat_alpha, xi);
let wi: Vec3 = wo.scale(-1.0).reflect(h).normalize();
let n_dot_l: f32 = normal.dot(wi);
if n_dot_l <= RT_MIN_PDF {
return None;
}
let (brdf, pdf): (Vec3, f32) = eval_material_bsdf_and_pdf(material, normal, wo, wi);
if pdf <= RT_MIN_PDF {
return None;
}
Some(RtBsdfSample::new(
wi,
brdf.scale(n_dot_l / pdf),
pdf,
false,
false,
))
} else if u_lobe < p_coat + p_spec {
let r: f32 = material.get_roughness().clamp(0.01, 1.0);
let alpha: f32 = (r * r).max(RT_MIN_ALPHA);
let h: Vec3 = sample_ggx_vndf(wo, normal, alpha, xi);
let wi: Vec3 = wo.scale(-1.0).reflect(h).normalize();
let n_dot_l: f32 = normal.dot(wi);
if n_dot_l <= RT_MIN_PDF {
return None;
}
let (brdf, pdf): (Vec3, f32) = eval_material_bsdf_and_pdf(material, normal, wo, wi);
if pdf <= RT_MIN_PDF {
return None;
}
Some(RtBsdfSample::new(
wi,
brdf.scale(n_dot_l / pdf),
pdf,
false,
false,
))
} else if u_lobe < p_coat + p_spec + p_diff {
let (wi, _): (Vec3, f32) = sample_cosine_hemisphere(normal, xi);
let n_dot_l: f32 = normal.dot(wi);
if n_dot_l <= RT_MIN_PDF {
return None;
}
let (brdf, pdf): (Vec3, f32) = eval_material_bsdf_and_pdf(material, normal, wo, wi);
if pdf <= RT_MIN_PDF {
return None;
}
Some(RtBsdfSample::new(
wi,
brdf.scale(n_dot_l / pdf),
pdf,
false,
false,
))
} else {
let ior: f32 = material.get_ior().clamp(1.001, 3.0);
let (eta_i, eta_t): (f32, f32) = if front_face { (1.0, ior) } else { (ior, 1.0) };
let incident: Vec3 = wo.scale(-1.0);
if let Some(refracted) = refract_snell(incident, normal, eta_i / eta_t) {
Some(RtBsdfSample::new(
refracted,
material.get_albedo(),
0.0,
true,
true,
))
} else {
let wi: Vec3 = incident.reflect(normal).normalize();
Some(RtBsdfSample::new(
wi,
material.get_albedo(),
0.0,
true,
false,
))
}
}
}
}
}