diff --git a/vendor/bevy_pbr/src/deferred/functions.wesl b/vendor/bevy_pbr/src/deferred/functions.wesl index 3e310592..dc6ee058 100644 --- a/vendor/bevy_pbr/src/deferred/functions.wesl +++ b/vendor/bevy_pbr/src/deferred/functions.wesl @@ -47,7 +47,10 @@ fn deferred_gbuffer_from_pbr_input(in: PbrInput) -> vec4 { in.material.metallic, // could be fewer bits diffuse_occlusion, // is this worth including? clearcoat_props)); - let flags = deferred_types::deferred_flags_from_mesh_material_flags(in.flags, in.material.flags); + // raea: the diffuse transmission in the flags' top five bits (0-31), for Solari, which shades the light through a + // leaf or a curtain from the G-buffer (bevy_solari gbuffer_utils.wesl); Bevy's readers mask the three below + let flags = deferred_types::deferred_flags_from_mesh_material_flags(in.flags, in.material.flags) + | (u32(round(saturate(in.material.diffuse_transmission) * 31.0)) << 3u); let octahedral_normal = octahedral_encode(normalize(in.N)); var base_color_srgb = vec3(0.0); var emissive = in.material.emissive.rgb; diff --git a/vendor/bevy_solari/src/pathtracer/pathtracer.wesl b/vendor/bevy_solari/src/pathtracer/pathtracer.wesl index f9ac6286..63c49e53 100644 --- a/vendor/bevy_solari/src/pathtracer/pathtracer.wesl +++ b/vendor/bevy_solari/src/pathtracer/pathtracer.wesl @@ -5,7 +5,7 @@ import package::scene::brdf::{evaluate_brdf, evaluate_and_sample_brdf, brdf_pdf, import package::scene::sampling::{sky_portal_mis_weight, generate_random_light_sample, calculate_resolved_light_contribution, trace_visibility, LightContribution, random_emissive_light_pdf, power_heuristic, sample_environment_map_light_and_sun}; import package::pathtracer::media::{media_collision, media_transmittance}; import raea_sys_fog::froxel_volume::{henyey_greenstein, sample_phase}; -import package::scene::bindings::{trace_ray, resolve_ray_hit_full, RAY_T_MIN, RAY_T_MAX, MIRROR_ROUGHNESS_THRESHOLD, materials, material_ids, material_alpha, coat_albedo, facing_normal, ALPHA_PREMULTIPLIED}; +import package::scene::bindings::{trace_ray, resolve_ray_hit_full, RAY_T_MIN, RAY_T_MAX, MIRROR_ROUGHNESS_THRESHOLD, materials, material_ids, material_alpha, coat_albedo, face_hit, offset_ray_origin, ALPHA_PREMULTIPLIED}; enable wgpu_ray_query; @@ -99,7 +99,10 @@ fn pathtrace(@builtin(global_invocation_id) global_id: vec3) { } } let wo = -ray_direction; - ray_hit.world_normal = facing_normal(ray_hit.world_normal, ray_hit.geometric_world_normal, wo); + // raea: a hit seen from its back shaded as its front (bindings.wesl face_hit); its light only from its front, + // as light sampling finds it + let hit_front = dot(ray_hit.geometric_world_normal, wo) >= 0.0; + ray_hit = face_hit(ray_hit, wo); let NdotV = max(dot(ray_hit.world_normal, wo), 0.0001); let F_ab = F_AB(ray_hit.material.perceptual_roughness, NdotV); @@ -109,7 +112,7 @@ fn pathtrace(@builtin(global_invocation_id) global_id: vec3) { let p_light = random_emissive_light_pdf(ray_hit, ray.t, NdotV); mis_weight = power_heuristic(p_bounce, p_light); } - if dot(ray_hit.world_normal, wo) > 0.0 { + if hit_front { radiance += mis_weight * throughput * ray_hit.material.emissive; } @@ -134,7 +137,7 @@ fn pathtrace(@builtin(global_invocation_id) global_id: vec3) { // raea: off a mirror's specular lobe, which light sampling can't reach, the ray sees the sun itself sees_sun = ray_hit.material.roughness <= MIRROR_ROUGHNESS_THRESHOLD && !next_bounce.diffuse_selected; ray_direction = next_bounce.wi; - ray_origin = ray_hit.world_position + (ray_hit.geometric_world_normal * RAY_T_MIN); + ray_origin = offset_ray_origin(ray_hit.world_position, ray_hit.geometric_world_normal, ray_hit.world_normal, ray_direction); ray_t_min = RAY_T_MIN; p_bounce = next_bounce.pdf; throughput *= next_bounce.throughput; diff --git a/vendor/bevy_solari/src/realtime/gbuffer_utils.wesl b/vendor/bevy_solari/src/realtime/gbuffer_utils.wesl index 17ca2df7..ee08af0e 100644 --- a/vendor/bevy_solari/src/realtime/gbuffer_utils.wesl +++ b/vendor/bevy_solari/src/realtime/gbuffer_utils.wesl @@ -33,7 +33,9 @@ fn gpixel_resolve(gpixel: vec4, depth: f32, pixel_id: vec2, view_size: let metallic = props.g; // raea: Bevy's G-buffer holds it pre-exposed (bevy_pbr deferred/functions.wesl), so a bright lamp doesn't clip let emissive = rgb9e5_to_vec3_(gpixel.g) / view.exposure; - let material = ResolvedMaterial(base_color, emissive, reflectance, perceptual_roughness, roughness, metallic); + // raea: the diffuse transmission, in the flags' top five bits (bevy_pbr deferred/functions.wesl) + let diffuse_transmission = f32((gpixel.a >> 27u) & 31u) / 31.0; + let material = ResolvedMaterial(base_color, emissive, reflectance, perceptual_roughness, roughness, metallic, diffuse_transmission); return ResolvedGPixel(world_position, world_normal, material); } diff --git a/vendor/bevy_solari/src/realtime/initial_path.wesl b/vendor/bevy_solari/src/realtime/initial_path.wesl index 548cd386..62d3d74a 100644 --- a/vendor/bevy_solari/src/realtime/initial_path.wesl +++ b/vendor/bevy_solari/src/realtime/initial_path.wesl @@ -6,7 +6,7 @@ import package::scene::brdf::{brdf_pdf, evaluate_and_sample_brdf, evaluate_brdf, import package::realtime::presample_light_tiles::unpack_resolved_light_sample; import package::realtime::bindings::{empty_reservoir, light_tile_resolved_samples, light_tile_samples, Reservoir, constants, view}; import package::scene::sampling::{calculate_resolved_light_contribution, isinf, LightSample, NULL_LIGHT_ID, power_heuristic, trace_visibility, sample_environment_map_light_and_sun, sky_portal_mis_weight}; -import package::scene::bindings::{facing_normal, light_sources, MIRROR_ROUGHNESS_THRESHOLD, RAY_T_MAX, RAY_T_MIN, resolve_ray_hit_full, ResolvedMaterial, ResolvedRayHitFull, trace_ray}; +import package::scene::bindings::{face_hit, light_sources, MIRROR_ROUGHNESS_THRESHOLD, RAY_T_MAX, RAY_T_MIN, resolve_ray_hit_full, ResolvedMaterial, ResolvedRayHitFull, trace_ray}; import package::realtime::world_cache_query::{get_cell_size, query_world_cache, query_world_cache_found, WORLD_CACHE_CELL_LIFETIME}; @if(DLSS_RR_GUIDE_BUFFERS) @@ -34,6 +34,8 @@ struct InitialSamplingResult { /// surface), x2 = first BRDF-sampled hit (the reconnection vertex). struct PathState { ray_origin: vec3, + // raea: what ray_origin is off the vertex along, RAY_T_MIN out (path_origin) + origin_normal: vec3, normal: vec3, wo: vec3, material: ResolvedMaterial, @@ -57,6 +59,7 @@ struct PathState { fn new_path_state(world_position: vec3, world_normal: vec3, wo: vec3, material: ResolvedMaterial) -> PathState { var path: PathState; path.ray_origin = world_position + (world_normal * RAY_T_MIN); + path.origin_normal = world_normal; path.normal = world_normal; path.wo = wo; path.material = material; @@ -112,7 +115,7 @@ fn generate_initial_path(world_position: vec3, world_normal: vec3, mat var next_bounce = evaluate_and_sample_brdf(path.wo, path.normal, path.material, F_ab, rng); if next_bounce.pdf == 0.0 { break; } if bounce == 0u && q < 1.0 { next_bounce.throughput /= q; } - let ray = trace_ray(path.ray_origin, next_bounce.wi, RAY_T_MIN, RAY_T_MAX, RAY_FLAG_NONE); + let ray = trace_ray(path_origin(path, next_bounce.wi), next_bounce.wi, RAY_T_MIN, RAY_T_MAX, RAY_FLAG_NONE); if ray.kind == RAY_QUERY_INTERSECTION_NONE { @if(DLSS_RR_GUIDE_BUFFERS) @@ -134,8 +137,11 @@ fn generate_initial_path(world_position: vec3, world_normal: vec3, mat } var ray_hit = resolve_ray_hit_full(ray); - // raea: a hit's normal turned from the ray (a crest seen in a mirror) bent back to face it, as the primary's is - ray_hit.world_normal = facing_normal(ray_hit.world_normal, ray_hit.geometric_world_normal, -next_bounce.wi); + // raea: a hit seen from its back shaded as its front (bindings.wesl face_hit), and a normal turned from the ray (a + // crest seen in a mirror) bent back to face it, as the primary's is. Its light only from its front, as light + // sampling finds it + let hit_front = dot(ray_hit.geometric_world_normal, next_bounce.wi) <= 0.0; + ray_hit = face_hit(ray_hit, -next_bounce.wi); let p_brdf = next_bounce.pdf; @if(DLSS_RR_GUIDE_BUFFERS) @@ -182,7 +188,7 @@ fn generate_initial_path(world_position: vec3, world_normal: vec3, mat } // Resample emissive hits - if any(ray_hit.material.emissive > vec3(0.0)) && dot(ray_hit.world_normal, -next_bounce.wi) > 0.0 { + if any(ray_hit.material.emissive > vec3(0.0)) && hit_front { generate_emissive_candidate(&path, ray_hit, next_bounce.wi, p_brdf, ray.t, p_nee, di_samples, bounce, rng); } @@ -193,6 +199,7 @@ fn generate_initial_path(world_position: vec3, world_normal: vec3, mat // Advance to the next vertex path.ray_origin = ray_hit.world_position + (ray_hit.geometric_world_normal * RAY_T_MIN); + path.origin_normal = ray_hit.geometric_world_normal; path.normal = ray_hit.world_normal; path.wo = -next_bounce.wi; path.material = ray_hit.material; @@ -223,6 +230,12 @@ fn generate_initial_path(world_position: vec3, world_normal: vec3, mat return result; } +// raea: where a ray from the path's vertex toward wi starts: ray_origin, off the side the normal faces, or as far off the +// other for a ray through to the far side (diffuse transmission) +fn path_origin(path: PathState, wi: vec3) -> vec3 { + return select(path.ray_origin - path.origin_normal * (2.0 * RAY_T_MIN), path.ray_origin, dot(path.normal, wi) >= 0.0); +} + // raea: one draw in [0, 1) for a whole workgroup this frame (first_bounce_probability's coin) fn first_bounce_coin(workgroup_id: vec2) -> f32 { var wg_rng = (workgroup_id.x * 5782582u) ^ (workgroup_id.y * 1103515245u) ^ constants.frame_rng; @@ -241,7 +254,7 @@ fn generate_nee_candidate( ) { if rand_f(rng) >= p_nee { return; } - let di = sample_light_ris((*path).ray_origin, (*path).normal, (*path).wo, (*path).material, F_ab, di_samples, workgroup_id, bounce, rng); + let di = sample_light_ris((*path).ray_origin, (*path).origin_normal, (*path).normal, (*path).wo, (*path).material, F_ab, di_samples, workgroup_id, bounce, rng); let di_target_function = luminance(di.brdf_radiance); if di_target_function <= 0.0 { return; } @@ -290,7 +303,7 @@ struct DiSample { brdf_rays_can_hit: bool, } -fn sample_light_ris(ray_origin: vec3, normal: vec3, wo: vec3, material: ResolvedMaterial, F_ab: vec2, di_samples: u32, workgroup_id: vec2, bounce: u32, rng: ptr) -> DiSample { +fn sample_light_ris(ray_origin: vec3, origin_normal: vec3, normal: vec3, wo: vec3, material: ResolvedMaterial, F_ab: vec2, di_samples: u32, workgroup_id: vec2, bounce: u32, rng: ptr) -> DiSample { var workgroup_rng = (workgroup_id.x * 5782582u) + workgroup_id.y + bounce; let light_tile_start = rand_range_u(128u, &workgroup_rng) * 1024u; @@ -329,7 +342,9 @@ fn sample_light_ris(ray_origin: vec3, normal: vec3, wo: vec3, mat var unbiased_contribution_weight = 0.0; if selected_target_function > 0.0 { unbiased_contribution_weight = weight_sum / selected_target_function; - unbiased_contribution_weight *= trace_visibility(ray_origin, selected_world_position); + // raea: from the far side for light let through to this one (diffuse transmission) + let origin = select(ray_origin - origin_normal * (2.0 * RAY_T_MIN), ray_origin, dot(normal, selected_wi) >= 0.0); + unbiased_contribution_weight *= trace_visibility(origin, selected_world_position); } return DiSample(unbiased_contribution_weight, light_tile_samples[selected_tile_sample], selected_wi, selected_brdf_radiance, selected_inverse_solid_angle_pdf, selected_brdf_rays_can_hit); @@ -465,7 +480,7 @@ fn terminate_into_cache( let hit_wo = -wi; let hit_F_ab = F_AB(ray_hit.material.perceptual_roughness, max(dot(ray_hit.world_normal, hit_wo), 0.0001)); let hit_origin = ray_hit.world_position + (ray_hit.geometric_world_normal * RAY_T_MIN); - let di = sample_light_ris(hit_origin, ray_hit.world_normal, hit_wo, ray_hit.material, hit_F_ab, max(constants.world_cache_direct_light_sample_count, 1u), workgroup_id, bounce + 1u, rng); + let di = sample_light_ris(hit_origin, ray_hit.geometric_world_normal, ray_hit.world_normal, hit_wo, ray_hit.material, hit_F_ab, max(constants.world_cache_direct_light_sample_count, 1u), workgroup_id, bounce + 1u, rng); let cache_outgoing = di.brdf_radiance * di.unbiased_contribution_weight + (ray_hit.material.base_color / PI) * cached_radiance; let cache_L_at_reconnection = (*path).throughput_past_first_hit * cache_outgoing; diff --git a/vendor/bevy_solari/src/realtime/restir.wesl b/vendor/bevy_solari/src/realtime/restir.wesl index 8211b294..2c36dd10 100644 --- a/vendor/bevy_solari/src/realtime/restir.wesl +++ b/vendor/bevy_solari/src/realtime/restir.wesl @@ -7,7 +7,7 @@ import package::scene::brdf::{brdf_pdf, evaluate_brdf, F_AB}; import package::realtime::gbuffer_utils::{gpixel_resolve, permute_pixel, pixel_dissimilar}; import package::realtime::initial_path::{generate_initial_path, InitialSamplingResult}; import package::realtime::bindings::{depth_buffer, empty_reservoir, gbuffer, load_motion_vector, previous_depth_buffer, previous_gbuffer, previous_view, reservoirs_a, reservoirs_b, Reservoir, constants, view, view_output}; -import package::scene::sampling::{balance_heuristic, calculate_resolved_light_contribution, isinf, isnan, LightSample, NULL_LIGHT_ID, power_heuristic, resolve_light_sample, ResolvedLightSample, trace_visibility, trace_visibility_previous_frame}; +import package::scene::sampling::{balance_heuristic, calculate_resolved_light_contribution, isinf, isnan, LightSample, NULL_LIGHT_ID, power_heuristic, resolve_light_sample, ResolvedLightSample, trace_visibility, trace_visibility_previous_frame, ray_origin_toward_sample}; import package::scene::bindings::{light_sources, LIGHT_NOT_PRESENT_THIS_FRAME, previous_frame_light_id_translations, RAY_T_MAX, RAY_T_MIN, ResolvedMaterial}; import package::realtime::world_cache_query::{query_world_cache, WORLD_CACHE_CELL_LIFETIME}; @@ -152,7 +152,7 @@ struct NeighborInfo { } fn empty_material() -> ResolvedMaterial { - return ResolvedMaterial(vec3(0.0), vec3(0.0), 0.0, 0.0, 0.0, 0.0); + return ResolvedMaterial(vec3(0.0), vec3(0.0), 0.0, 0.0, 0.0, 0.0, 0.0); } fn jacobian( @@ -247,14 +247,16 @@ fn merge_reservoirs( // Visibility for the cross-domain targets if other_sample_at_canonical.target_function > 0.0 && other_sample_at_canonical_jacobian > 0.0 { - let visibility = trace_visibility(canonical_world_position + canonical_world_normal * RAY_T_MIN, other_sample_at_canonical.sample_world_position); + let origin = ray_origin_toward_sample(canonical_world_position, canonical_world_normal, other_sample_at_canonical.sample_world_position); + let visibility = trace_visibility(origin, other_sample_at_canonical.sample_world_position); other_sample_at_canonical.target_function *= visibility; } if canonical_sample_at_other.target_function > 0.0 && canonical_sample_at_other_jacobian > 0.0 { + let origin = ray_origin_toward_sample(other_world_position, other_world_normal, canonical_sample_at_other.sample_world_position); @if(SPATIAL_MERGE) - let visibility = trace_visibility(other_world_position + other_world_normal * RAY_T_MIN, canonical_sample_at_other.sample_world_position); + let visibility = trace_visibility(origin, canonical_sample_at_other.sample_world_position); @else - let visibility = trace_visibility_previous_frame(other_world_position + other_world_normal * RAY_T_MIN, canonical_sample_at_other.sample_world_position); + let visibility = trace_visibility_previous_frame(origin, canonical_sample_at_other.sample_world_position); canonical_sample_at_other.target_function *= visibility; } diff --git a/vendor/bevy_solari/src/realtime/world_cache_update.wesl b/vendor/bevy_solari/src/realtime/world_cache_update.wesl index 7a2bfab5..ec433fc7 100644 --- a/vendor/bevy_solari/src/realtime/world_cache_update.wesl +++ b/vendor/bevy_solari/src/realtime/world_cache_update.wesl @@ -2,8 +2,8 @@ import bevy_core_pipeline::tonemapping::tonemapping_luminance as luminance; import bevy_pbr::render::utils::{rand_f, rand_range_u, sample_cosine_hemisphere}; import bevy_render::maths::PI; import package::realtime::presample_light_tiles::unpack_resolved_light_sample; -import package::scene::sampling::{calculate_resolved_light_contribution, trace_visibility, sample_environment_map_light, sky_portal_light_pdf}; -import package::scene::bindings::{trace_ray, resolve_ray_hit_full, RAY_T_MIN, RAY_T_MAX}; +import package::scene::sampling::{calculate_resolved_light_contribution, trace_visibility, sample_environment_map_light, sky_portal_light_pdf, ray_origin_toward_sample}; +import package::scene::bindings::{trace_ray, resolve_ray_hit_full, face_hit, RAY_T_MIN, RAY_T_MAX}; import package::realtime::world_cache_query::query_world_cache_found; import package::realtime::bindings::{ light_tile_resolved_samples, @@ -44,7 +44,8 @@ fn sample_gi(@builtin(workgroup_id) workgroup_id: vec3, @builtin(global_inv let ray_direction = sample_cosine_hemisphere(geometry_data.world_normal, &rng); let ray = trace_ray(geometry_data.world_position + (geometry_data.world_normal * RAY_T_MIN), ray_direction, RAY_T_MIN, constants.world_cache_max_gi_ray_distance, RAY_FLAG_NONE); if ray.kind != RAY_QUERY_INTERSECTION_NONE { - let ray_hit = resolve_ray_hit_full(ray); + // raea: a hit seen from its back as its front (bindings.wesl face_hit): its own cell, its light from its side + let ray_hit = face_hit(resolve_ray_hit_full(ray), -ray_direction); let cell_life = atomicLoad(&world_cache.life[cell_index]); let cached = query_world_cache_found(ray_hit.world_position, ray_hit.geometric_world_normal, view.world_position, ray.t, cell_life, &rng); // raea: a hit whose cell holds no light yet is no sample for a cell that has light: this update is skipped @@ -55,7 +56,7 @@ fn sample_gi(@builtin(workgroup_id) workgroup_id: vec3, @builtin(global_inv return; } // raea: the lights' own light on the surface the ray found, sampled there, with what has bounced onto it - let direct = sample_random_light_ris(ray_hit.world_position, ray_hit.geometric_world_normal, workgroup_id.xy, &rng); + let direct = sample_random_light_ris(ray_hit.world_position, ray_hit.geometric_world_normal, ray_hit.material.diffuse_transmission, workgroup_id.xy, &rng); world_cache.active_cells_new_radiance[active_cell_id.x] += ray_hit.material.base_color * (direct + cached.rgb); } else { // The ray is cut short at world_cache_max_gi_ray_distance to save on traversal costs, so a miss may be a surface @@ -108,7 +109,7 @@ fn blend_new_samples(@builtin(global_invocation_id) active_cell_id: vec3) { world_cache.luminance_deltas[cell_index] = blended_luminance_delta; } -fn sample_random_light_ris(world_position: vec3, world_normal: vec3, workgroup_id: vec2, rng: ptr) -> vec3 { +fn sample_random_light_ris(world_position: vec3, world_normal: vec3, diffuse_transmission: f32, workgroup_id: vec2, rng: ptr) -> vec3 { var workgroup_rng = (workgroup_id.x * 5782582u) + workgroup_id.y; let light_tile_start = rand_range_u(128u, &workgroup_rng) * 1024u; @@ -122,7 +123,10 @@ fn sample_random_light_ris(world_position: vec3, world_normal: vec3, w let resolved_light_sample = unpack_resolved_light_sample(light_tile_resolved_samples[tile_sample], view.exposure); let light_contribution = calculate_resolved_light_contribution(resolved_light_sample, world_position, world_normal); - let contribution = light_contribution.radiance * saturate(dot(light_contribution.wi, world_normal)); + // raea: light from behind a thin surface for the share it lets through (diffuse transmission), the rest from in front + let cos_l = dot(light_contribution.wi, world_normal); + let cos_share = select(diffuse_transmission * -cos_l, (1.0 - diffuse_transmission) * cos_l, cos_l >= 0.0); + let contribution = light_contribution.radiance * min(max(cos_share, 0.0), 1.0); let target_function = luminance(contribution); let resampling_weight = mis_weight * (target_function * light_contribution.inverse_pdf); @@ -140,7 +144,7 @@ fn sample_random_light_ris(world_position: vec3, world_normal: vec3, w let inverse_target_function = select(0.0, 1.0 / selected_sample_target_function, selected_sample_target_function > 0.0); unbiased_contribution_weight = weight_sum * inverse_target_function; - unbiased_contribution_weight *= trace_visibility(world_position + (world_normal * RAY_T_MIN), selected_sample_world_position); + unbiased_contribution_weight *= trace_visibility(ray_origin_toward_sample(world_position, world_normal, selected_sample_world_position), selected_sample_world_position); } return selected_sample_radiance * unbiased_contribution_weight; diff --git a/vendor/bevy_solari/src/scene/binder/assets.rs b/vendor/bevy_solari/src/scene/binder/assets.rs index 73223cc7..5929b9f5 100644 --- a/vendor/bevy_solari/src/scene/binder/assets.rs +++ b/vendor/bevy_solari/src/scene/binder/assets.rs @@ -7,7 +7,7 @@ use bevy_asset::AssetId; use bevy_color::{Alpha, ColorToComponents, LinearRgba}; use bevy_image::Image; use bevy_material::AlphaMode; -use bevy_math::{Vec3, Vec4}; +use bevy_math::{Vec2, Vec3, Vec4}; use bevy_pbr::StandardMaterial; use bevy_platform::collections::{HashMap, HashSet}; use bevy_render::{ @@ -48,7 +48,10 @@ pub struct GpuMaterial { // raea: StandardMaterial's uv_transform (columns x and y, then the translation), applied to the mesh's UVs before // its textures are read, as the raster passes do uv_transform_xy: Vec4, - uv_transform_t: Vec4, + uv_transform_t: Vec2, + // raea: StandardMaterial's diffuse_transmission, for a ray's light through a leaf or a curtain (brdf.wesl) + diffuse_transmission: f32, + padding: f32, } // raea: how a traced ray treats a material's alpha (bindings.wesl's alpha_accepts) @@ -241,7 +244,9 @@ impl AssetState { alpha_mode, alpha_cutoff, uv_transform_xy: Vec4::new(uv.matrix2.x_axis.x, uv.matrix2.x_axis.y, uv.matrix2.y_axis.x, uv.matrix2.y_axis.y), - uv_transform_t: Vec4::new(uv.translation.x, uv.translation.y, 0.0, 0.0), + uv_transform_t: uv.translation, + diffuse_transmission: material.diffuse_transmission.clamp(0.0, 1.0), + padding: 0.0, }, ); // raea: a material that turns see-through (or back) moves its instances between the TLAS masks diff --git a/vendor/bevy_solari/src/scene/bindings.wesl b/vendor/bevy_solari/src/scene/bindings.wesl index 93c26feb..0f14d922 100644 --- a/vendor/bevy_solari/src/scene/bindings.wesl +++ b/vendor/bevy_solari/src/scene/bindings.wesl @@ -58,9 +58,12 @@ struct Material { alpha_mode: u32, alpha_cutoff: f32, reflectance: f32, - // raea: StandardMaterial's uv_transform: columns x (xy) and y (zw), then the translation (xy) + // raea: StandardMaterial's uv_transform: columns x (xy) and y (zw), then the translation uv_transform_xy: vec4, - uv_transform_t: vec4, + uv_transform_t: vec2, + // raea: StandardMaterial's diffuse_transmission: the share of the diffuse light let through to the far side + diffuse_transmission: f32, + padding: f32, } const TEXTURE_MAP_NONE = 0xFFFFFFFFu; @@ -96,6 +99,28 @@ fn facing_normal(world_normal: vec3, geometric_world_normal: vec3, wo: return normalize(min_cos * wo + sqrt(1.0 - min_cos * min_cos) * tangent); } +// raea: a hit seen from its back (a leaf's underside, a curtain from the window's side) shaded as its front: both its +// normals turned toward the ray, as raster turns a double-sided material's. A ray found a back face black before (its +// BRDF has nothing below the normal), in the path tracer and in Solari alike. Light passes a thin surface by its +// material's diffuse_transmission (brdf.wesl), never by which side was hit. Emission stays one-sided, as light sampling +// has it: a caller checks the side before the turn +fn face_hit(hit: ResolvedRayHitFull, wo: vec3) -> ResolvedRayHitFull { + var faced = hit; + if dot(faced.geometric_world_normal, wo) < 0.0 { + faced.geometric_world_normal = -faced.geometric_world_normal; + faced.world_normal = -faced.world_normal; + } + faced.world_normal = facing_normal(faced.world_normal, faced.geometric_world_normal, wo); + return faced; +} + +// raea: where a ray leaving a surface at p starts: off the side it leaves by, along offset_normal (the geometric normal +// at a hit). A ray below the shading normal has passed through (diffuse transmission) and starts off the far side, or +// it would stop on the surface it left +fn offset_ray_origin(p: vec3, offset_normal: vec3, shading_normal: vec3, direction: vec3) -> vec3 { + return p + offset_normal * select(-RAY_T_MIN, RAY_T_MIN, dot(shading_normal, direction) >= 0.0); +} + // raea: the share of light a premultiplied surface's clear coat reflects at an angle (Schlick, F0 from its reflectance) fn coat_albedo(reflectance: f32, cos_theta: f32) -> f32 { let f0 = 0.16 * reflectance * reflectance; @@ -271,6 +296,8 @@ struct ResolvedMaterial { perceptual_roughness: f32, roughness: f32, metallic: f32, + // raea: Material's + diffuse_transmission: f32, } struct ResolvedRayHitFull { @@ -309,6 +336,7 @@ fn resolve_material(material: Material, uv: vec2) -> ResolvedMaterial { } m.roughness = m.perceptual_roughness * m.perceptual_roughness; + m.diffuse_transmission = material.diffuse_transmission; return m; } diff --git a/vendor/bevy_solari/src/scene/brdf.wesl b/vendor/bevy_solari/src/scene/brdf.wesl index e4fe4fac..dd89a1e2 100644 --- a/vendor/bevy_solari/src/scene/brdf.wesl +++ b/vendor/bevy_solari/src/scene/brdf.wesl @@ -65,9 +65,13 @@ fn evaluate_and_sample_brdf( var wi: vec3; var wi_tangent: vec3; - let diffuse_selected = rand_f(rng) < diffuse_weight; + let lobe_draw = rand_f(rng); + let diffuse_selected = lobe_draw < diffuse_weight; if diffuse_selected { - wi = sample_cosine_hemisphere(world_normal, rng); + // raea: through to the far side for the material's diffuse_transmission share of the diffuse draws, decided by the + // same draw rescaled, so a material that lets none through draws exactly the numbers it did before + let through = lobe_draw < diffuse_weight * material.diffuse_transmission; + wi = sample_cosine_hemisphere(select(world_normal, -world_normal, through), rng); wi_tangent = vec3(dot(wi, T), dot(wi, B), dot(wi, N)); } else { wi_tangent = sample_ggx_vndf(wo_tangent, material.roughness, rng); @@ -87,8 +91,8 @@ fn evaluate_and_sample_brdf( } } - let diffuse_pdf = wi_tangent.z / PI; - let specular_pdf = ggx_vndf_pdf(wo_tangent, wi_tangent, material.roughness); + let diffuse_pdf = diffuse_lobe_pdf(wi_tangent.z, material.diffuse_transmission); + let specular_pdf = select(0.0, ggx_vndf_pdf(wo_tangent, wi_tangent, material.roughness), wi_tangent.z > 0.0); let pdf = (diffuse_weight * diffuse_pdf) + (specular_weight * specular_pdf); let throughput = evaluate_brdf(wo, wi, world_normal, material, F_ab) / pdf; return EvaluateAndSampleBrdfResult(wi, throughput, pdf, diffuse_selected); @@ -104,14 +108,28 @@ fn evaluate_brdf( return evaluate_diffuse_brdf(wo, wi, world_normal, material, F_ab) + evaluate_specular_brdf(wo, wi, world_normal, material, F_ab); } +// raea: with diffuse transmission (a leaf, a curtain, paper), the diffuse light is shared between the two sides: the +// material's diffuse_transmission of it leaves the far side, Lambertian there as the rest is on the near one (Bevy's +// raster splits it so: pbr_functions calculate_diffuse_color) fn evaluate_diffuse_brdf(wo: vec3, wi: vec3, world_normal: vec3, material: ResolvedMaterial, F_ab: vec2) -> vec3 { let NdotL = dot(world_normal, wi); let NdotV = dot(world_normal, wo); - if NdotL < 0.0001 || NdotV < 0.0001 { return vec3(0.0); } + if NdotV < 0.0001 { return vec3(0.0); } + var share = 1.0 - material.diffuse_transmission; + if NdotL < 0.0001 { + if NdotL > -0.0001 || material.diffuse_transmission <= 0.0 { return vec3(0.0); } + share = material.diffuse_transmission; + } let F0_metal = material.base_color; let F0_dielectric = calculate_F0_dielectric(vec3(material.reflectance)); let rho = lobe_reflectances(F0_metal, F0_dielectric, material, F_ab); - return rho.diffuse / PI * NdotL; + return rho.diffuse * share / PI * abs(NdotL); +} + +// raea: the diffuse lobe's pdf for a direction whose cosine to the normal is cos_l: the near side's share of it +// cosine-weighted above, the far side's below +fn diffuse_lobe_pdf(cos_l: f32, diffuse_transmission: f32) -> f32 { + return select((1.0 - diffuse_transmission) * cos_l, diffuse_transmission * -cos_l, cos_l < 0.0) / PI; } fn evaluate_specular_brdf(wo: vec3, wi: vec3, world_normal: vec3, material: ResolvedMaterial, F_ab: vec2) -> vec3 { @@ -161,8 +179,8 @@ fn brdf_pdf(wo: vec3, wi: vec3, world_normal: vec3, material: Res 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); + let diffuse_pdf = diffuse_lobe_pdf(wi_tangent.z, material.diffuse_transmission); + let specular_pdf = select(0.0, ggx_vndf_pdf(wo_tangent, wi_tangent, material.roughness), wi_tangent.z > 0.0); return (diffuse_weight * diffuse_pdf) + (specular_weight * specular_pdf); } diff --git a/vendor/bevy_solari/src/scene/sampling.wesl b/vendor/bevy_solari/src/scene/sampling.wesl index 7113ebb5..51c5e2a6 100644 --- a/vendor/bevy_solari/src/scene/sampling.wesl +++ b/vendor/bevy_solari/src/scene/sampling.wesl @@ -1,7 +1,7 @@ import bevy_pbr::render::pbr_lighting::D_GGX; import bevy_pbr::render::utils::{rand_vec2f, rand_u, rand_range_u}; import bevy_render::maths::{PI_2, orthonormalize}; -import package::scene::bindings::{trace_ray, trace_ray_previous_frame, RAY_T_MIN, RAY_T_MAX, light_sources, directional_lights, environment_map, environment_map_sampler, environment_map_light, LightSource, LIGHT_SOURCE_KIND_DIRECTIONAL, LIGHT_SOURCE_KIND_SKY_PORTAL, sky_portal, resolve_triangle_data_full, ResolvedRayHitFull, MIRROR_ROUGHNESS_THRESHOLD}; +import package::scene::bindings::{trace_ray, trace_ray_previous_frame, RAY_T_MIN, RAY_T_MAX, light_sources, directional_lights, environment_map, environment_map_sampler, environment_map_light, LightSource, LIGHT_SOURCE_KIND_DIRECTIONAL, LIGHT_SOURCE_KIND_SKY_PORTAL, sky_portal, resolve_triangle_data_full, ResolvedRayHitFull, MIRROR_ROUGHNESS_THRESHOLD, offset_ray_origin}; enable wgpu_ray_query; @@ -290,6 +290,12 @@ fn sky_portal_mis_weight(origin: vec3, wi: vec3, bounce_pdf: f32, nee_ return power_heuristic(bounce_pdf, p_light * nee_scale); } +// raea: where a visibility ray from a surface at p toward a light sample's point starts (w 0: the point is a direction), +// off the side the light comes in by: the far side for light let through (diffuse transmission) +fn ray_origin_toward_sample(p: vec3, normal: vec3, point: vec4) -> vec3 { + return offset_ray_origin(p, normal, normal, select(point.xyz - p, point.xyz, point.w == 0.0)); +} + fn trace_visibility(ray_origin: vec3, point: vec4) -> f32 { var ray_direction = point.xyz; var ray_t_max = RAY_T_MAX;