Build renderer and material component foundations
Add dedicated skinned rendering, pose restoration, shared Material and Material Instance slots, registry-driven components, Surface/Solari integration, transactional schema upgrades, navigation authoring, documentation, and evaluation evidence.
This commit is contained in:
1 parent
0553a85220
commit
0798aa5d57
169 files changed
+27098
-3469
No files matched your search
@@ -0,0 +1,36 @@
|
||||
use super::{prepare::PathtracerAccumulationTexture, Pathtracer};
|
||||
use bevy_camera::Camera;
|
||||
use bevy_ecs::{
|
||||
change_detection::DetectChanges,
|
||||
system::{Commands, Query},
|
||||
world::Ref,
|
||||
};
|
||||
use bevy_render::{sync_world::RenderEntity, Extract};
|
||||
use bevy_transform::components::GlobalTransform;
|
||||
|
||||
pub fn extract_pathtracer(
|
||||
cameras_3d: Extract<
|
||||
Query<(
|
||||
RenderEntity,
|
||||
&Camera,
|
||||
Ref<GlobalTransform>,
|
||||
Option<&Pathtracer>,
|
||||
)>,
|
||||
>,
|
||||
mut commands: Commands,
|
||||
) {
|
||||
for (entity, camera, global_transform, pathtracer) in &cameras_3d {
|
||||
let mut entity_commands = commands
|
||||
.get_entity(entity)
|
||||
.expect("Camera entity wasn't synced.");
|
||||
if let Some(pathtracer) = pathtracer
|
||||
&& camera.is_active
|
||||
{
|
||||
let mut pathtracer = pathtracer.clone();
|
||||
pathtracer.reset |= global_transform.is_changed();
|
||||
entity_commands.insert(pathtracer);
|
||||
} else {
|
||||
entity_commands.remove::<(Pathtracer, PathtracerAccumulationTexture)>();
|
||||
}
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
mod extract;
|
||||
mod node;
|
||||
mod prepare;
|
||||
|
||||
use crate::SolariPlugins;
|
||||
use bevy_app::{App, Plugin};
|
||||
use bevy_asset::embedded_asset;
|
||||
use bevy_camera::Hdr;
|
||||
use bevy_core_pipeline::{
|
||||
schedule::{Core3d, Core3dSystems},
|
||||
tonemapping::tonemapping,
|
||||
};
|
||||
use bevy_ecs::{component::Component, reflect::ReflectComponent, schedule::IntoScheduleConfigs};
|
||||
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
|
||||
use bevy_render::{
|
||||
renderer::RenderDevice, ExtractSchedule, Render, RenderApp, RenderStartup, RenderSystems,
|
||||
};
|
||||
use extract::extract_pathtracer;
|
||||
use node::{init_pathtracer_pipelines, pathtracer};
|
||||
use prepare::prepare_pathtracer_accumulation_texture;
|
||||
use tracing::warn;
|
||||
|
||||
/// Non-realtime pathtracing.
|
||||
///
|
||||
/// This plugin is meant to generate reference screenshots to compare against,
|
||||
/// and is not intended to be used by games.
|
||||
pub struct PathtracingPlugin;
|
||||
|
||||
impl Plugin for PathtracingPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
embedded_asset!(app, "pathtracer.wgsl");
|
||||
}
|
||||
|
||||
fn finish(&self, app: &mut App) {
|
||||
let render_app = app.sub_app_mut(RenderApp);
|
||||
|
||||
let render_device = render_app.world().resource::<RenderDevice>();
|
||||
let features = render_device.features();
|
||||
if !features.contains(SolariPlugins::required_wgpu_features()) {
|
||||
warn!(
|
||||
"PathtracingPlugin not loaded. GPU lacks support for required features: {:?}.",
|
||||
SolariPlugins::required_wgpu_features().difference(features)
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
render_app
|
||||
.add_systems(RenderStartup, init_pathtracer_pipelines)
|
||||
.add_systems(ExtractSchedule, extract_pathtracer)
|
||||
.add_systems(
|
||||
Render,
|
||||
prepare_pathtracer_accumulation_texture.in_set(RenderSystems::PrepareResources),
|
||||
)
|
||||
.add_systems(
|
||||
Core3d,
|
||||
pathtracer
|
||||
.after(Core3dSystems::MainPass)
|
||||
.before(tonemapping),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component, Reflect, Default, Clone)]
|
||||
#[reflect(Component, Default, Clone)]
|
||||
#[require(Hdr)]
|
||||
pub struct Pathtracer {
|
||||
pub reset: bool,
|
||||
}
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
use super::{prepare::PathtracerAccumulationTexture, Pathtracer};
|
||||
use crate::scene::RaytracingSceneBindings;
|
||||
use bevy_asset::{load_embedded_asset, AssetServer};
|
||||
use bevy_ecs::{prelude::*, resource::Resource, system::Commands};
|
||||
use bevy_render::{
|
||||
camera::ExtractedCamera,
|
||||
render_resource::{
|
||||
binding_types::{texture_storage_2d, uniform_buffer},
|
||||
BindGroupEntries, BindGroupLayoutDescriptor, BindGroupLayoutEntries,
|
||||
CachedComputePipelineId, ComputePassDescriptor, ComputePipelineDescriptor,
|
||||
ImageSubresourceRange, PipelineCache, ShaderStages, StorageTextureAccess, TextureFormat,
|
||||
},
|
||||
renderer::{RenderContext, RenderDevice, ViewQuery},
|
||||
view::{ViewTarget, ViewUniform, ViewUniformOffset, ViewUniforms},
|
||||
};
|
||||
use bevy_utils::default;
|
||||
|
||||
/// Resource holding the pathtracer pipeline configuration.
|
||||
#[derive(Resource)]
|
||||
pub struct PathtracerPipelines {
|
||||
bind_group_layout: BindGroupLayoutDescriptor,
|
||||
pipeline: CachedComputePipelineId,
|
||||
}
|
||||
|
||||
/// Initializes the pathtracer pipelines at render startup.
|
||||
pub fn init_pathtracer_pipelines(
|
||||
mut commands: Commands,
|
||||
pipeline_cache: Res<PipelineCache>,
|
||||
scene_bindings: Res<RaytracingSceneBindings>,
|
||||
asset_server: Res<AssetServer>,
|
||||
) {
|
||||
let bind_group_layout = BindGroupLayoutDescriptor::new(
|
||||
"pathtracer_bind_group_layout",
|
||||
&BindGroupLayoutEntries::sequential(
|
||||
ShaderStages::COMPUTE,
|
||||
(
|
||||
texture_storage_2d(TextureFormat::Rgba32Float, StorageTextureAccess::ReadWrite),
|
||||
texture_storage_2d(TextureFormat::Rgba16Float, StorageTextureAccess::WriteOnly),
|
||||
uniform_buffer::<ViewUniform>(true),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
let pipeline = pipeline_cache.queue_compute_pipeline(ComputePipelineDescriptor {
|
||||
label: Some("pathtracer_pipeline".into()),
|
||||
layout: vec![
|
||||
scene_bindings.bind_group_layout.clone(),
|
||||
bind_group_layout.clone(),
|
||||
],
|
||||
shader: load_embedded_asset!(asset_server.as_ref(), "pathtracer.wgsl"),
|
||||
..default()
|
||||
});
|
||||
|
||||
commands.insert_resource(PathtracerPipelines {
|
||||
bind_group_layout,
|
||||
pipeline,
|
||||
});
|
||||
}
|
||||
|
||||
pub fn pathtracer(
|
||||
view: ViewQuery<(
|
||||
&Pathtracer,
|
||||
&PathtracerAccumulationTexture,
|
||||
&ExtractedCamera,
|
||||
&ViewTarget,
|
||||
&ViewUniformOffset,
|
||||
)>,
|
||||
pathtracer_pipelines: Option<Res<PathtracerPipelines>>,
|
||||
pipeline_cache: Res<PipelineCache>,
|
||||
scene_bindings: Res<RaytracingSceneBindings>,
|
||||
view_uniforms: Res<ViewUniforms>,
|
||||
render_device: Res<RenderDevice>,
|
||||
mut ctx: RenderContext,
|
||||
) {
|
||||
let (pathtracer_settings, accumulation_texture, camera, view_target, view_uniform_offset) =
|
||||
view.into_inner();
|
||||
|
||||
let Some(pathtracer_pipelines) = pathtracer_pipelines else {
|
||||
return;
|
||||
};
|
||||
|
||||
let (Some(pipeline), Some(scene_bind_group), Some(viewport), Some(view_uniforms_binding)) = (
|
||||
pipeline_cache.get_compute_pipeline(pathtracer_pipelines.pipeline),
|
||||
&scene_bindings.bind_group,
|
||||
camera.physical_viewport_size,
|
||||
view_uniforms.uniforms.binding(),
|
||||
) else {
|
||||
return;
|
||||
};
|
||||
|
||||
let bind_group = render_device.create_bind_group(
|
||||
"pathtracer_bind_group",
|
||||
&pipeline_cache.get_bind_group_layout(&pathtracer_pipelines.bind_group_layout),
|
||||
&BindGroupEntries::sequential((
|
||||
&accumulation_texture.0.default_view,
|
||||
view_target.get_unsampled_color_attachment().view,
|
||||
view_uniforms_binding,
|
||||
)),
|
||||
);
|
||||
|
||||
let command_encoder = ctx.command_encoder();
|
||||
|
||||
if pathtracer_settings.reset {
|
||||
command_encoder.clear_texture(
|
||||
&accumulation_texture.0.texture,
|
||||
&ImageSubresourceRange::default(),
|
||||
);
|
||||
}
|
||||
|
||||
let mut pass = command_encoder.begin_compute_pass(&ComputePassDescriptor {
|
||||
label: Some("pathtracer"),
|
||||
timestamp_writes: None,
|
||||
});
|
||||
pass.set_pipeline(pipeline);
|
||||
pass.set_bind_group(0, scene_bind_group, &[]);
|
||||
pass.set_bind_group(1, &bind_group, &[view_uniform_offset.offset]);
|
||||
pass.dispatch_workgroups(viewport.x.div_ceil(8), viewport.y.div_ceil(8), 1);
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
enable wgpu_ray_query;
|
||||
|
||||
#import bevy_core_pipeline::tonemapping::tonemapping_luminance as luminance
|
||||
#import bevy_pbr::pbr_functions::calculate_F0
|
||||
#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::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}
|
||||
|
||||
@group(1) @binding(0) var accumulation_texture: texture_storage_2d<rgba32float, read_write>;
|
||||
@group(1) @binding(1) var view_output: texture_storage_2d<rgba16float, write>;
|
||||
@group(1) @binding(2) var<uniform> view: View;
|
||||
|
||||
@compute @workgroup_size(8, 8, 1)
|
||||
fn pathtrace(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
||||
if any(global_id.xy >= vec2u(view.viewport.zw)) {
|
||||
return;
|
||||
}
|
||||
|
||||
let old_color = textureLoad(accumulation_texture, global_id.xy);
|
||||
|
||||
// Setup RNG
|
||||
let pixel_index = global_id.x + global_id.y * u32(view.viewport.z);
|
||||
let frame_index = u32(old_color.a) * 5782582u;
|
||||
var rng = pixel_index + frame_index;
|
||||
|
||||
// Shoot the first ray from the camera
|
||||
let pixel_center = vec2<f32>(global_id.xy) + 0.5;
|
||||
let jitter = rand_vec2f(&rng) - 0.5;
|
||||
let pixel_uv = (pixel_center + jitter) / view.viewport.zw;
|
||||
let pixel_ndc = (pixel_uv * 2.0) - 1.0;
|
||||
let primary_ray_target = view.world_from_clip * vec4(pixel_ndc.x, -pixel_ndc.y, 1.0, 1.0);
|
||||
var ray_origin = view.world_position;
|
||||
var ray_direction = normalize((primary_ray_target.xyz / primary_ray_target.w) - ray_origin);
|
||||
var ray_t_min = 0.0;
|
||||
|
||||
// Path trace
|
||||
var radiance = vec3(0.0);
|
||||
var throughput = vec3(1.0);
|
||||
var p_bounce = 0.0;
|
||||
loop {
|
||||
let ray = trace_ray(ray_origin, ray_direction, ray_t_min, RAY_T_MAX, RAY_FLAG_NONE);
|
||||
if ray.kind != RAY_QUERY_INTERSECTION_NONE {
|
||||
let ray_hit = resolve_ray_hit_full(ray);
|
||||
let wo = -ray_direction;
|
||||
|
||||
// Emissive contribution
|
||||
var mis_weight = 1.0;
|
||||
if p_bounce != 0.0 { // Not first bounce
|
||||
let p_light = random_emissive_light_pdf(ray_hit);
|
||||
mis_weight = power_heuristic(p_bounce, p_light);
|
||||
}
|
||||
radiance += mis_weight * throughput * ray_hit.material.emissive;
|
||||
|
||||
// Sample direct lighting, but only if the surface is not mirror-like
|
||||
// TODO: randomly choose to use NEE or not with probability proportional to roughness and metallicness
|
||||
let is_perfectly_specular = ray_hit.material.roughness <= MIRROR_ROUGHNESS_THRESHOLD && ray_hit.material.metallic > 0.9999;
|
||||
if !is_perfectly_specular {
|
||||
let direct_lighting = sample_random_light(ray_hit.world_position, ray_hit.world_normal, &rng);
|
||||
|
||||
mis_weight = 1.0;
|
||||
if direct_lighting.brdf_rays_can_hit {
|
||||
let pdf_of_bounce = brdf_pdf(wo, direct_lighting.wi, ray_hit);
|
||||
mis_weight = power_heuristic(1.0 / direct_lighting.inverse_pdf, pdf_of_bounce);
|
||||
}
|
||||
|
||||
let direct_lighting_brdf = evaluate_brdf(wo, direct_lighting.wi, ray_hit.world_normal, ray_hit.material);
|
||||
radiance += mis_weight * throughput * direct_lighting.radiance * direct_lighting.inverse_pdf * direct_lighting_brdf;
|
||||
}
|
||||
|
||||
// Sample new ray direction from the material BRDF for next bounce and apply BRDF
|
||||
let next_bounce = evaluate_and_sample_brdf(wo, ray_hit.world_normal, ray_hit.material, &rng);
|
||||
if next_bounce.pdf == 0.0 { break; }
|
||||
ray_direction = next_bounce.wi;
|
||||
ray_origin = ray_hit.world_position + (ray_hit.geometric_world_normal * RAY_T_MIN);
|
||||
ray_t_min = RAY_T_MIN;
|
||||
p_bounce = next_bounce.pdf;
|
||||
throughput *= next_bounce.throughput;
|
||||
|
||||
// Russian roulette for early termination
|
||||
let p = luminance(throughput);
|
||||
if rand_f(&rng) > p { break; }
|
||||
throughput /= p;
|
||||
} else { break; }
|
||||
}
|
||||
|
||||
// Camera exposure
|
||||
radiance *= view.exposure;
|
||||
|
||||
// Accumulation over time via running average
|
||||
let new_color = mix(old_color.rgb, radiance, 1.0 / (old_color.a + 1.0));
|
||||
textureStore(accumulation_texture, global_id.xy, vec4(new_color, old_color.a + 1.0));
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
use super::Pathtracer;
|
||||
use bevy_ecs::{
|
||||
component::Component,
|
||||
entity::Entity,
|
||||
query::With,
|
||||
system::{Commands, Query, Res, ResMut},
|
||||
};
|
||||
use bevy_image::ToExtents;
|
||||
use bevy_render::{
|
||||
camera::ExtractedCamera,
|
||||
render_resource::{TextureDescriptor, TextureDimension, TextureFormat, TextureUsages},
|
||||
renderer::RenderDevice,
|
||||
texture::{CachedTexture, TextureCache},
|
||||
};
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct PathtracerAccumulationTexture(pub CachedTexture);
|
||||
|
||||
pub fn prepare_pathtracer_accumulation_texture(
|
||||
query: Query<(Entity, &ExtractedCamera), With<Pathtracer>>,
|
||||
mut texture_cache: ResMut<TextureCache>,
|
||||
render_device: Res<RenderDevice>,
|
||||
mut commands: Commands,
|
||||
) {
|
||||
for (entity, camera) in &query {
|
||||
let Some(viewport) = camera.physical_viewport_size else {
|
||||
continue;
|
||||
};
|
||||
|
||||
let descriptor = TextureDescriptor {
|
||||
label: Some("pathtracer_accumulation_texture"),
|
||||
size: viewport.to_extents(),
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: TextureDimension::D2,
|
||||
format: TextureFormat::Rgba32Float,
|
||||
usage: TextureUsages::STORAGE_BINDING,
|
||||
view_formats: &[],
|
||||
};
|
||||
|
||||
commands
|
||||
.entity(entity)
|
||||
.insert(PathtracerAccumulationTexture(
|
||||
texture_cache.get(&render_device, descriptor),
|
||||
));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user