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:
Rbanh committed 2026-07-12 00:24:06 -04:00
1 parent 0553a85220
commit 0798aa5d57
169 files changed
+27098 -3469

No files matched your search

+1
View File
@@ -0,0 +1 @@
{"v":1}
+6
View File
@@ -0,0 +1,6 @@
{
"git": {
"sha1": "c6f634ca9f406d68ba5109d921247b654cb42c10"
},
"path_in_vcs": "crates/bevy_solari"
}
+24
View File
@@ -0,0 +1,24 @@
# Blacksite Solari Patch Boundary
Base: `bevy_solari` 0.19.0.
Blacksite-owned changes are deliberately limited to the scene module and dependency manifest:
- extract either `MeshMaterial3d<StandardMaterial>` or `MeshMaterial3d<SurfaceMaterial>`;
- pack the Surface ABI v1 uniform lanes, UV transforms, and eight texture bindings into Solari's
scene material buffer;
- rebuild the embedded scene shader library from the active, validated `SurfaceEvaluatorRegistry`;
- namespace evaluator/helper functions by stable shader ID and use the same evaluator for ray-hit
shading and cutout candidate acceptance;
- build shared mesh BLAS geometry as non-opaque so per-instance cutout materials can reject hits;
- retain standard Bevy materials and reject blended material modes from the ray scene.
The `game_hot` bridge owns eligibility and diagnostics. Until an instance-owned post-morph/post-skin
vertex buffer can feed a dynamic BLAS, it removes `RaytracingMesh3d` from skinned or morphed entities
and reports them through `deformed_meshes_excluded`. Rigid animated descendants remain supported
because their geometry is static and their current/previous transforms are already extracted.
When rebasing, compare upstream changes only in `src/scene/` first. Preserve the evaluator-registry
extraction, GPU/WGSL material layouts, non-opaque BLAS flag, and candidate traversal as one atomic
unit; a layout mismatch can pass Rust compilation but corrupt GPU reads. Validate the generated WGSL
with Naga and run `cargo test -p game_hot --lib` after every rebase.
+3602
View File
File diff suppressed because it is too large. Load diff
+159
View File
@@ -0,0 +1,159 @@
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
# "normalize" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
# to registry (e.g., crates.io) dependencies.
#
# If you are reading this file be aware that the original Cargo.toml
# will likely look very different (and much more reasonable).
# See Cargo.toml.orig for the original contents.
[package]
edition = "2024"
name = "bevy_solari"
version = "0.19.0"
build = false
autolib = false
autobins = false
autoexamples = false
autotests = false
autobenches = false
description = "Provides raytraced lighting for Bevy Engine"
homepage = "https://bevy.org"
readme = "README.md"
keywords = ["bevy"]
license = "MIT OR Apache-2.0"
repository = "https://github.com/bevyengine/bevy"
[package.metadata.docs.rs]
rustdoc-args = [
"-Zunstable-options",
"--generate-link-to-definition",
"--generate-macro-expansion",
]
all-features = true
[features]
dlss = ["dep:bevy_anti_alias", "bevy_anti_alias/dlss"]
force_disable_dlss = ["dep:bevy_anti_alias", "bevy_anti_alias/force_disable_dlss"]
[lib]
name = "bevy_solari"
path = "src/lib.rs"
[dependencies.bevy_anti_alias]
version = "0.19.0"
optional = true
[dependencies.bevy_app]
version = "0.19.0"
[dependencies.bevy_asset]
version = "0.19.0"
[dependencies.bevy_camera]
version = "0.19.0"
[dependencies.bevy_color]
version = "0.19.0"
[dependencies.bevy_core_pipeline]
version = "0.19.0"
[dependencies.bevy_derive]
version = "0.19.0"
[dependencies.bevy_diagnostic]
version = "0.19.0"
[dependencies.bevy_ecs]
version = "0.19.0"
[dependencies.bevy_image]
version = "0.19.0"
[dependencies.bevy_math]
version = "0.19.0"
[dependencies.bevy_material]
version = "0.19.0"
[dependencies.bevy_mesh]
version = "0.19.0"
[dependencies.bevy_pbr]
version = "0.19.0"
features = ["dfg_lut"]
[dependencies.bevy_platform]
version = "0.19.0"
features = ["std"]
default-features = false
[dependencies.bevy_reflect]
version = "0.19.0"
[dependencies.bevy_render]
version = "0.19.0"
[dependencies.bevy_shader]
version = "0.19.0"
[dependencies.bevy_transform]
version = "0.19.0"
[dependencies.bevy_utils]
version = "0.19.0"
[dependencies.blacksite_surface]
path = "../../crates/blacksite_surface"
[dependencies.bytemuck]
version = "1"
[dependencies.derive_more]
version = "2"
features = ["from"]
default-features = false
[dependencies.tracing]
version = "0.1"
features = ["std"]
default-features = false
[lints.clippy]
alloc_instead_of_core = "warn"
allow_attributes = "warn"
allow_attributes_without_reason = "warn"
doc_markdown = "warn"
manual_let_else = "warn"
match_same_arms = "warn"
needless_lifetimes = "allow"
nonstandard_macro_braces = "warn"
print_stderr = "warn"
print_stdout = "warn"
ptr_as_ptr = "warn"
ptr_cast_constness = "warn"
redundant_closure_for_method_calls = "warn"
redundant_else = "warn"
ref_as_ptr = "warn"
semicolon_if_nothing_returned = "warn"
std_instead_of_alloc = "warn"
std_instead_of_core = "warn"
too_long_first_doc_paragraph = "allow"
too_many_arguments = "allow"
type_complexity = "allow"
undocumented_unsafe_blocks = "warn"
unwrap_or_default = "warn"
[lints.rust]
missing_docs = "warn"
unsafe_code = "deny"
unsafe_op_in_unsafe_fn = "warn"
unused_qualifications = "warn"
[lints.rust.unexpected_cfgs]
level = "warn"
priority = 0
check-cfg = ["cfg(docsrs_dep)"]
+55
View File
@@ -0,0 +1,55 @@
[package]
name = "bevy_solari"
version = "0.19.0"
edition = "2024"
description = "Provides raytraced lighting for Bevy Engine"
homepage = "https://bevy.org"
repository = "https://github.com/bevyengine/bevy"
license = "MIT OR Apache-2.0"
keywords = ["bevy"]
[dependencies]
# bevy
bevy_anti_alias = { path = "../bevy_anti_alias", version = "0.19.0" }
bevy_app = { path = "../bevy_app", version = "0.19.0" }
bevy_asset = { path = "../bevy_asset", version = "0.19.0" }
bevy_camera = { path = "../bevy_camera", version = "0.19.0" }
bevy_color = { path = "../bevy_color", version = "0.19.0" }
bevy_core_pipeline = { path = "../bevy_core_pipeline", version = "0.19.0" }
bevy_derive = { path = "../bevy_derive", version = "0.19.0" }
bevy_diagnostic = { path = "../bevy_diagnostic", version = "0.19.0" }
bevy_ecs = { path = "../bevy_ecs", version = "0.19.0" }
bevy_shader = { path = "../bevy_shader", version = "0.19.0" }
bevy_math = { path = "../bevy_math", version = "0.19.0" }
bevy_mesh = { path = "../bevy_mesh", version = "0.19.0" }
bevy_pbr = { path = "../bevy_pbr", version = "0.19.0", features = [
"dfg_lut",
] }
bevy_platform = { path = "../bevy_platform", version = "0.19.0", default-features = false, features = [
"std",
] }
bevy_reflect = { path = "../bevy_reflect", version = "0.19.0" }
bevy_render = { path = "../bevy_render", version = "0.19.0" }
bevy_image = { path = "../bevy_image", version = "0.19.0" }
bevy_utils = { path = "../bevy_utils", version = "0.19.0" }
bevy_transform = { path = "../bevy_transform", version = "0.19.0" }
# other
bytemuck = { version = "1" }
derive_more = { version = "2", default-features = false, features = ["from"] }
tracing = { version = "0.1", default-features = false, features = ["std"] }
[features]
dlss = ["bevy_anti_alias/dlss"]
force_disable_dlss = ["bevy_anti_alias/force_disable_dlss"]
[lints]
workspace = true
[package.metadata.docs.rs]
rustdoc-args = [
"-Zunstable-options",
"--generate-link-to-definition",
"--generate-macro-expansion",
]
all-features = true
+176
View File
@@ -0,0 +1,176 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
+19
View File
@@ -0,0 +1,19 @@
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+20
View File
@@ -0,0 +1,20 @@
# Bevy Solari
[![License](https://img.shields.io/badge/license-MIT%2FApache-blue.svg)](https://github.com/bevyengine/bevy#license)
[![Crates.io](https://img.shields.io/crates/v/bevy_solari.svg)](https://crates.io/crates/bevy_solari)
[![Downloads](https://img.shields.io/crates/d/bevy_solari.svg)](https://crates.io/crates/bevy_solari)
[![Docs](https://docs.rs/bevy_solari/badge.svg)](https://docs.rs/bevy_solari/latest/bevy_solari/)
[![Discord](https://img.shields.io/discord/691052431525675048.svg?label=&logo=discord&logoColor=ffffff&color=7389D8&labelColor=6A7EC2)](https://discord.gg/bevy)
![Logo](../../assets/branding/bevy_solari.svg)
## Blacksite fork
This vendored Bevy 0.19 crate is patched to consume both Bevy `StandardMaterial` and Blacksite's
versioned Surface ABI. Validated Surface evaluator generations are namespaced into the shared
ray-query shader library, including evaluator-driven opaque/cutout candidate acceptance.
Skinned and morphed meshes are intentionally not submitted by Blacksite's `game_hot` bridge. Bevy
0.19 exposes the source mesh slab to Solari rather than the raster renderer's post-morph/post-skin
vertices; tracing that slab would render a stale bind-pose proxy. See `BLACKSITE_PATCH.md` for the
maintained boundary and upgrade notes.
+59
View File
@@ -0,0 +1,59 @@
#![expect(missing_docs, reason = "Not all docs are written yet, see #3492.")]
//! Provides raytraced lighting.
//!
//! See [`SolariPlugins`] for more info.
//!
//! ![`bevy_solari` logo](https://raw.githubusercontent.com/bevyengine/bevy/refs/heads/main/assets/branding/bevy_solari.svg)
extern crate alloc;
pub mod pathtracer;
pub mod realtime;
pub mod scene;
/// The solari prelude.
///
/// This includes the most common types in this crate, re-exported for your convenience.
pub mod prelude {
pub use super::SolariPlugins;
pub use crate::realtime::SolariLighting;
pub use crate::scene::RaytracingMesh3d;
}
use crate::realtime::SolariLightingPlugin;
use crate::scene::RaytracingScenePlugin;
use bevy_app::{PluginGroup, PluginGroupBuilder};
use bevy_render::settings::WgpuFeatures;
/// An experimental set of plugins for raytraced lighting.
///
/// This plugin group provides:
/// * [`SolariLightingPlugin`] - Raytraced direct and indirect lighting.
/// * [`RaytracingScenePlugin`] - BLAS building, resource and lighting binding.
///
/// There's also:
/// * [`pathtracer::PathtracingPlugin`] - A non-realtime pathtracer for validation purposes (not added by default).
///
/// To get started, add this plugin to your app, and then add `RaytracingMesh3d` plus either a Bevy
/// standard material or a validated Blacksite Surface material to your entities.
pub struct SolariPlugins;
impl PluginGroup for SolariPlugins {
fn build(self) -> PluginGroupBuilder {
PluginGroupBuilder::start::<Self>()
.add(RaytracingScenePlugin)
.add(SolariLightingPlugin)
}
}
impl SolariPlugins {
/// [`WgpuFeatures`] required for these plugins to function.
pub fn required_wgpu_features() -> WgpuFeatures {
WgpuFeatures::EXPERIMENTAL_RAY_QUERY
| WgpuFeatures::BUFFER_BINDING_ARRAY
| WgpuFeatures::TEXTURE_BINDING_ARRAY
| WgpuFeatures::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING
| WgpuFeatures::PARTIALLY_BOUND_BINDING_ARRAY
}
}
+36
View File
@@ -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
View File
@@ -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
View File
@@ -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);
}
+118
View File
@@ -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;
}
+47
View File
@@ -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),
));
}
}
+27
View File
@@ -0,0 +1,27 @@
use super::{prepare::SolariLightingResources, SolariLighting};
use bevy_camera::Camera;
use bevy_ecs::system::{Commands, ResMut};
use bevy_pbr::deferred::SkipDeferredLighting;
use bevy_render::{sync_world::RenderEntity, MainWorld};
pub fn extract_solari_lighting(mut main_world: ResMut<MainWorld>, mut commands: Commands) {
let mut cameras_3d = main_world.query::<(RenderEntity, &Camera, Option<&mut SolariLighting>)>();
for (entity, camera, solari_lighting) in cameras_3d.iter_mut(&mut main_world) {
let mut entity_commands = commands
.get_entity(entity)
.expect("Camera entity wasn't synced.");
if let Some(mut solari_lighting) = solari_lighting
&& camera.is_active
{
entity_commands.insert((solari_lighting.clone(), SkipDeferredLighting));
solari_lighting.reset = false;
} else {
entity_commands.remove::<(
SolariLighting,
SolariLightingResources,
SkipDeferredLighting,
)>();
}
}
}
+55
View File
@@ -0,0 +1,55 @@
#define_import_path bevy_solari::gbuffer_utils
#import bevy_pbr::pbr_deferred_types::unpack_24bit_normal
#import bevy_pbr::rgb9e5::rgb9e5_to_vec3_
#import bevy_pbr::utils::octahedral_decode
#import bevy_render::view::{View, depth_ndc_to_view_z}
#import bevy_solari::scene_bindings::ResolvedMaterial
struct ResolvedGPixel {
world_position: vec3<f32>,
world_normal: vec3<f32>,
material: ResolvedMaterial,
}
fn gpixel_resolve(gpixel: vec4<u32>, depth: f32, pixel_id: vec2<u32>, view_size: vec2<f32>, world_from_clip: mat4x4<f32>) -> ResolvedGPixel {
let world_position = reconstruct_world_position(pixel_id, depth, view_size, world_from_clip);
let world_normal = octahedral_decode(unpack_24bit_normal(gpixel.a));
let base_rough = unpack4x8unorm(gpixel.r);
let base_color = pow(base_rough.rgb, vec3(2.2));
let perceptual_roughness = base_rough.a;
let roughness = perceptual_roughness * perceptual_roughness;
let props = unpack4x8unorm(gpixel.b);
let reflectance = props.r;
let metallic = props.g;
let emissive = rgb9e5_to_vec3_(gpixel.g);
let material = ResolvedMaterial(base_color, emissive, reflectance, perceptual_roughness, roughness, metallic);
return ResolvedGPixel(world_position, world_normal, material);
}
fn reconstruct_world_position(pixel_id: vec2<u32>, depth: f32, view_size: vec2<f32>, world_from_clip: mat4x4<f32>) -> vec3<f32> {
let uv = (vec2<f32>(pixel_id) + 0.5) / view_size;
let xy_ndc = (uv - vec2(0.5)) * vec2(2.0, -2.0);
let world_pos = world_from_clip * vec4(xy_ndc, depth, 1.0);
return world_pos.xyz / world_pos.w;
}
// Reject if tangent plane differs more than 0.3% or angle between normals more than 90 degrees
fn pixel_dissimilar(depth: f32, world_position: vec3<f32>, other_world_position: vec3<f32>, normal: vec3<f32>, other_normal: vec3<f32>, view: View) -> bool {
// https://developer.download.nvidia.com/video/gputechconf/gtc/2020/presentations/s22699-fast-denoising-with-self-stabilizing-recurrent-blurs.pdf#page=45
let tangent_plane_distance = abs(dot(normal, other_world_position - world_position));
let view_z = -depth_ndc_to_view_z(depth, view.clip_from_view, view.view_from_clip);
return tangent_plane_distance / view_z > 0.003 || dot(normal, other_normal) < 0.0;
}
fn permute_pixel(pixel_id: vec2<u32>, frame_index: u32, view_size: vec2<f32>) -> vec2<u32> {
let r = frame_index;
let offset = vec2(r & 3u, (r >> 2u) & 3u);
var shifted_pixel_id = pixel_id + offset;
shifted_pixel_id ^= vec2(3u);
shifted_pixel_id -= offset;
return min(shifted_pixel_id, vec2<u32>(view_size - 1.0));
}
+112
View File
@@ -0,0 +1,112 @@
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::{
core_3d::main_opaque_pass_3d,
prepass::{
DeferredPrepass, DeferredPrepassDoubleBuffer, DepthPrepass, DepthPrepassDoubleBuffer,
MotionVectorPrepass,
},
schedule::{Core3d, Core3dSystems},
};
use bevy_ecs::{component::Component, reflect::ReflectComponent, schedule::IntoScheduleConfigs};
use bevy_pbr::DefaultOpaqueRendererMethod;
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
use bevy_render::{
renderer::RenderDevice, ExtractSchedule, Render, RenderApp, RenderStartup, RenderSystems,
};
use bevy_shader::load_shader_library;
use extract::extract_solari_lighting;
use node::{init_solari_lighting_pipelines, solari_lighting};
use prepare::prepare_solari_lighting_resources;
use tracing::warn;
/// Raytraced direct and indirect lighting.
///
/// When using this plugin, it's highly recommended to set `shadow_maps_enabled: false` on all lights, as Solari replaces
/// traditional shadow mapping.
pub struct SolariLightingPlugin;
impl Plugin for SolariLightingPlugin {
fn build(&self, app: &mut App) {
load_shader_library!(app, "gbuffer_utils.wgsl");
load_shader_library!(app, "realtime_bindings.wgsl");
load_shader_library!(app, "presample_light_tiles.wgsl");
embedded_asset!(app, "restir_di.wgsl");
embedded_asset!(app, "restir_gi.wgsl");
load_shader_library!(app, "specular_gi.wgsl");
load_shader_library!(app, "world_cache_query.wgsl");
embedded_asset!(app, "world_cache_compact.wgsl");
embedded_asset!(app, "world_cache_update.wgsl");
load_shader_library!(app, "resolve_dlss_rr_textures.wgsl");
app.insert_resource(DefaultOpaqueRendererMethod::deferred());
}
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!(
"SolariLightingPlugin not loaded. GPU lacks support for required features: {:?}.",
SolariPlugins::required_wgpu_features().difference(features)
);
return;
}
render_app
.add_systems(RenderStartup, init_solari_lighting_pipelines)
.add_systems(ExtractSchedule, extract_solari_lighting)
.add_systems(
Render,
prepare_solari_lighting_resources.in_set(RenderSystems::PrepareResources),
)
.add_systems(
Core3d,
solari_lighting
.before(main_opaque_pass_3d)
.in_set(Core3dSystems::MainPass),
);
}
}
/// A component for a 3d camera entity to enable the Solari raytraced lighting system.
///
/// Must be used with `CameraMainTextureUsages::default().with(TextureUsages::STORAGE_BINDING)`, and
/// `Msaa::Off`.
#[derive(Component, Reflect, Clone)]
#[reflect(Component, Default, Clone)]
#[require(
Hdr,
DeferredPrepass,
DepthPrepass,
MotionVectorPrepass,
DeferredPrepassDoubleBuffer,
DepthPrepassDoubleBuffer
)]
pub struct SolariLighting {
/// Set to true to delete the saved temporal history (past frames).
///
/// Useful for preventing ghosting when the history is no longer
/// representative of the current frame, such as in sudden camera cuts.
///
/// After setting this to true, it will automatically be toggled
/// back to false at the end of the frame.
pub reset: bool,
}
impl Default for SolariLighting {
fn default() -> Self {
Self {
reset: true, // No temporal history on the first frame
}
}
}
+597
View File
@@ -0,0 +1,597 @@
use super::{
prepare::{SolariLightingResources, LIGHT_TILE_BLOCKS, WORLD_CACHE_SIZE},
SolariLighting,
};
use crate::scene::RaytracingSceneBindings;
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
use bevy_anti_alias::dlss::ViewDlssRayReconstructionTextures;
use bevy_asset::{load_embedded_asset, AssetServer, Handle};
use bevy_core_pipeline::prepass::{
PreviousViewData, PreviousViewUniformOffset, PreviousViewUniforms, ViewPrepassTextures,
};
use bevy_diagnostic::FrameCount;
use bevy_ecs::{prelude::*, resource::Resource, system::Commands};
use bevy_render::{
diagnostic::RecordDiagnostics as _,
render_resource::{
binding_types::{
storage_buffer_sized, texture_2d, texture_depth_2d, texture_storage_2d, uniform_buffer,
},
BindGroupEntries, BindGroupLayoutDescriptor, BindGroupLayoutEntries,
CachedComputePipelineId, ComputePassDescriptor, ComputePipelineDescriptor, LoadOp,
PipelineCache, RenderPassDescriptor, ShaderStages, StorageTextureAccess, TextureFormat,
TextureSampleType,
},
renderer::{RenderContext, RenderDevice, ViewQuery},
view::{ViewTarget, ViewUniform, ViewUniformOffset, ViewUniforms},
};
use bevy_shader::{Shader, ShaderDefVal};
use bevy_utils::default;
/// Resource holding the Solari lighting pipeline configuration.
#[derive(Resource)]
pub struct SolariLightingPipelines {
bind_group_layout: BindGroupLayoutDescriptor,
bind_group_layout_world_cache_active_cells_dispatch: BindGroupLayoutDescriptor,
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
bind_group_layout_resolve_dlss_rr_textures: BindGroupLayoutDescriptor,
decay_world_cache_pipeline: CachedComputePipelineId,
compact_world_cache_single_block_pipeline: CachedComputePipelineId,
compact_world_cache_blocks_pipeline: CachedComputePipelineId,
compact_world_cache_write_active_cells_pipeline: CachedComputePipelineId,
sample_di_for_world_cache_pipeline: CachedComputePipelineId,
sample_gi_for_world_cache_pipeline: CachedComputePipelineId,
blend_new_world_cache_samples_pipeline: CachedComputePipelineId,
presample_light_tiles_pipeline: CachedComputePipelineId,
di_initial_and_temporal_pipeline: CachedComputePipelineId,
di_spatial_and_shade_pipeline: CachedComputePipelineId,
gi_initial_and_temporal_pipeline: CachedComputePipelineId,
gi_spatial_and_shade_pipeline: CachedComputePipelineId,
specular_gi_pipeline: CachedComputePipelineId,
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
specular_gi_with_psr_pipeline: CachedComputePipelineId,
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
resolve_dlss_rr_textures_pipeline: CachedComputePipelineId,
}
#[cfg(any(not(feature = "dlss"), feature = "force_disable_dlss"))]
type SolariLightingViewQuery = (
&'static SolariLighting,
&'static SolariLightingResources,
&'static ViewTarget,
&'static ViewPrepassTextures,
&'static ViewUniformOffset,
&'static PreviousViewUniformOffset,
);
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
type SolariLightingViewQuery = (
&'static SolariLighting,
&'static SolariLightingResources,
&'static ViewTarget,
&'static ViewPrepassTextures,
&'static ViewUniformOffset,
&'static PreviousViewUniformOffset,
Option<&'static ViewDlssRayReconstructionTextures>,
);
pub fn solari_lighting(
view: ViewQuery<SolariLightingViewQuery>,
solari_pipelines: Option<Res<SolariLightingPipelines>>,
pipeline_cache: Res<PipelineCache>,
scene_bindings: Res<RaytracingSceneBindings>,
view_uniforms: Res<ViewUniforms>,
previous_view_uniforms: Res<PreviousViewUniforms>,
frame_count: Res<FrameCount>,
render_device: Res<RenderDevice>,
mut ctx: RenderContext,
) {
#[cfg(any(not(feature = "dlss"), feature = "force_disable_dlss"))]
let (
solari_lighting,
solari_lighting_resources,
view_target,
view_prepass_textures,
view_uniform_offset,
previous_view_uniform_offset,
) = view.into_inner();
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
let (
solari_lighting,
solari_lighting_resources,
view_target,
view_prepass_textures,
view_uniform_offset,
previous_view_uniform_offset,
view_dlss_rr_textures,
) = view.into_inner();
let Some(pipelines) = solari_pipelines else {
return;
};
#[cfg(not(all(feature = "dlss", not(feature = "force_disable_dlss"))))]
let specular_gi_pipeline = pipelines.specular_gi_pipeline;
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
let specular_gi_pipeline = if view_dlss_rr_textures.is_some() {
pipelines.specular_gi_with_psr_pipeline
} else {
pipelines.specular_gi_pipeline
};
let (
Some(decay_world_cache_pipeline),
Some(compact_world_cache_single_block_pipeline),
Some(compact_world_cache_blocks_pipeline),
Some(compact_world_cache_write_active_cells_pipeline),
Some(sample_di_for_world_cache_pipeline),
Some(sample_gi_for_world_cache_pipeline),
Some(blend_new_world_cache_samples_pipeline),
Some(presample_light_tiles_pipeline),
Some(di_initial_and_temporal_pipeline),
Some(di_spatial_and_shade_pipeline),
Some(gi_initial_and_temporal_pipeline),
Some(gi_spatial_and_shade_pipeline),
Some(specular_gi_pipeline),
Some(scene_bind_group),
Some(gbuffer),
Some(depth_buffer),
Some(motion_vectors),
Some(previous_gbuffer),
Some(previous_depth_buffer),
Some(view_uniforms_binding),
Some(previous_view_uniforms_binding),
) = (
pipeline_cache.get_compute_pipeline(pipelines.decay_world_cache_pipeline),
pipeline_cache.get_compute_pipeline(pipelines.compact_world_cache_single_block_pipeline),
pipeline_cache.get_compute_pipeline(pipelines.compact_world_cache_blocks_pipeline),
pipeline_cache
.get_compute_pipeline(pipelines.compact_world_cache_write_active_cells_pipeline),
pipeline_cache.get_compute_pipeline(pipelines.sample_di_for_world_cache_pipeline),
pipeline_cache.get_compute_pipeline(pipelines.sample_gi_for_world_cache_pipeline),
pipeline_cache.get_compute_pipeline(pipelines.blend_new_world_cache_samples_pipeline),
pipeline_cache.get_compute_pipeline(pipelines.presample_light_tiles_pipeline),
pipeline_cache.get_compute_pipeline(pipelines.di_initial_and_temporal_pipeline),
pipeline_cache.get_compute_pipeline(pipelines.di_spatial_and_shade_pipeline),
pipeline_cache.get_compute_pipeline(pipelines.gi_initial_and_temporal_pipeline),
pipeline_cache.get_compute_pipeline(pipelines.gi_spatial_and_shade_pipeline),
pipeline_cache.get_compute_pipeline(specular_gi_pipeline),
&scene_bindings.bind_group,
view_prepass_textures.deferred_view(),
view_prepass_textures.depth_view(),
view_prepass_textures.motion_vectors_view(),
view_prepass_textures.previous_deferred_view(),
view_prepass_textures.previous_depth_view(),
view_uniforms.uniforms.binding(),
previous_view_uniforms.uniforms.binding(),
)
else {
return;
};
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
let Some(resolve_dlss_rr_textures_pipeline) =
pipeline_cache.get_compute_pipeline(pipelines.resolve_dlss_rr_textures_pipeline)
else {
return;
};
let view_target_attachment = view_target.get_unsampled_color_attachment();
let s = solari_lighting_resources;
let bind_group = render_device.create_bind_group(
"solari_lighting_bind_group",
&pipeline_cache.get_bind_group_layout(&pipelines.bind_group_layout),
&BindGroupEntries::sequential((
view_target_attachment.view,
s.light_tile_samples.as_entire_binding(),
s.light_tile_resolved_samples.as_entire_binding(),
&s.di_reservoirs_a,
&s.di_reservoirs_b,
s.gi_reservoirs_a.as_entire_binding(),
s.gi_reservoirs_b.as_entire_binding(),
gbuffer,
depth_buffer,
motion_vectors,
previous_gbuffer,
previous_depth_buffer,
view_uniforms_binding,
previous_view_uniforms_binding,
s.world_cache_checksums.as_entire_binding(),
s.world_cache_life.as_entire_binding(),
s.world_cache_radiance.as_entire_binding(),
s.world_cache_geometry_data.as_entire_binding(),
s.world_cache_luminance_deltas.as_entire_binding(),
s.world_cache_active_cells_new_radiance.as_entire_binding(),
s.world_cache_a.as_entire_binding(),
s.world_cache_b.as_entire_binding(),
s.world_cache_active_cell_indices.as_entire_binding(),
s.world_cache_active_cells_count.as_entire_binding(),
)),
);
let bind_group_world_cache_active_cells_dispatch = render_device.create_bind_group(
"solari_lighting_bind_group_world_cache_active_cells_dispatch",
&pipeline_cache
.get_bind_group_layout(&pipelines.bind_group_layout_world_cache_active_cells_dispatch),
&BindGroupEntries::single(s.world_cache_active_cells_dispatch.as_entire_binding()),
);
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
let bind_group_resolve_dlss_rr_textures = view_dlss_rr_textures.map(|d| {
render_device.create_bind_group(
"solari_lighting_bind_group_resolve_dlss_rr_textures",
&pipeline_cache
.get_bind_group_layout(&pipelines.bind_group_layout_resolve_dlss_rr_textures),
&BindGroupEntries::sequential((
&d.diffuse_albedo.default_view,
&d.specular_albedo.default_view,
&d.normal_roughness.default_view,
&d.specular_motion_vectors.default_view,
)),
)
});
// Choice of number here is arbitrary
let frame_index = frame_count.0.wrapping_mul(5782582);
let diagnostics = ctx.diagnostic_recorder();
let diagnostics = diagnostics.as_deref();
let command_encoder = ctx.command_encoder();
// Clear the view target if we're the first node to write to it
if matches!(view_target_attachment.ops.load, LoadOp::Clear(_)) {
command_encoder.begin_render_pass(&RenderPassDescriptor {
label: Some("solari_lighting_clear"),
color_attachments: &[Some(view_target_attachment)],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
multiview_mask: None,
});
}
let mut pass = command_encoder.begin_compute_pass(&ComputePassDescriptor {
label: Some("solari_lighting"),
timestamp_writes: None,
});
let dx = solari_lighting_resources.view_size.x.div_ceil(8);
let dy = solari_lighting_resources.view_size.y.div_ceil(8);
pass.set_bind_group(0, scene_bind_group, &[]);
pass.set_bind_group(
1,
&bind_group,
&[
view_uniform_offset.offset,
previous_view_uniform_offset.offset,
],
);
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
if let Some(bind_group_resolve_dlss_rr_textures) = &bind_group_resolve_dlss_rr_textures {
pass.set_bind_group(2, bind_group_resolve_dlss_rr_textures, &[]);
pass.set_pipeline(resolve_dlss_rr_textures_pipeline);
pass.dispatch_workgroups(dx, dy, 1);
}
let d = diagnostics.time_span(&mut pass, "solari_lighting/presample_light_tiles");
pass.set_pipeline(presample_light_tiles_pipeline);
pass.set_immediates(
0,
bytemuck::cast_slice(&[frame_index, solari_lighting.reset as u32]),
);
pass.dispatch_workgroups(LIGHT_TILE_BLOCKS as u32, 1, 1);
d.end(&mut pass);
let d = diagnostics.time_span(&mut pass, "solari_lighting/world_cache");
pass.set_bind_group(2, &bind_group_world_cache_active_cells_dispatch, &[]);
pass.set_pipeline(decay_world_cache_pipeline);
pass.dispatch_workgroups((WORLD_CACHE_SIZE / 1024) as u32, 1, 1);
pass.set_pipeline(compact_world_cache_single_block_pipeline);
pass.dispatch_workgroups((WORLD_CACHE_SIZE / 1024) as u32, 1, 1);
pass.set_pipeline(compact_world_cache_blocks_pipeline);
pass.dispatch_workgroups(1, 1, 1);
pass.set_pipeline(compact_world_cache_write_active_cells_pipeline);
pass.dispatch_workgroups((WORLD_CACHE_SIZE / 1024) as u32, 1, 1);
pass.set_bind_group(2, None, &[]);
pass.set_pipeline(sample_di_for_world_cache_pipeline);
pass.set_immediates(
0,
bytemuck::cast_slice(&[frame_index, solari_lighting.reset as u32]),
);
pass.dispatch_workgroups_indirect(
&solari_lighting_resources.world_cache_active_cells_dispatch,
0,
);
pass.set_pipeline(sample_gi_for_world_cache_pipeline);
pass.set_immediates(
0,
bytemuck::cast_slice(&[frame_index, solari_lighting.reset as u32]),
);
pass.dispatch_workgroups_indirect(
&solari_lighting_resources.world_cache_active_cells_dispatch,
0,
);
pass.set_pipeline(blend_new_world_cache_samples_pipeline);
pass.dispatch_workgroups_indirect(
&solari_lighting_resources.world_cache_active_cells_dispatch,
0,
);
d.end(&mut pass);
let d = diagnostics.time_span(&mut pass, "solari_lighting/direct_lighting");
pass.set_pipeline(di_initial_and_temporal_pipeline);
pass.set_immediates(
0,
bytemuck::cast_slice(&[frame_index, solari_lighting.reset as u32]),
);
pass.dispatch_workgroups(dx, dy, 1);
pass.set_pipeline(di_spatial_and_shade_pipeline);
pass.set_immediates(
0,
bytemuck::cast_slice(&[frame_index, solari_lighting.reset as u32]),
);
pass.dispatch_workgroups(dx, dy, 1);
d.end(&mut pass);
let d = diagnostics.time_span(&mut pass, "solari_lighting/diffuse_indirect_lighting");
pass.set_pipeline(gi_initial_and_temporal_pipeline);
pass.set_immediates(
0,
bytemuck::cast_slice(&[frame_index, solari_lighting.reset as u32]),
);
pass.dispatch_workgroups(dx, dy, 1);
pass.set_pipeline(gi_spatial_and_shade_pipeline);
pass.set_immediates(
0,
bytemuck::cast_slice(&[frame_index, solari_lighting.reset as u32]),
);
pass.dispatch_workgroups(dx, dy, 1);
d.end(&mut pass);
let d = diagnostics.time_span(&mut pass, "solari_lighting/specular_indirect_lighting");
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
if let Some(bind_group_resolve_dlss_rr_textures) = &bind_group_resolve_dlss_rr_textures {
pass.set_bind_group(2, bind_group_resolve_dlss_rr_textures, &[]);
}
pass.set_pipeline(specular_gi_pipeline);
pass.set_immediates(
0,
bytemuck::cast_slice(&[frame_index, solari_lighting.reset as u32]),
);
pass.dispatch_workgroups(dx, dy, 1);
d.end(&mut pass);
drop(pass);
diagnostics.record_u32(
ctx.command_encoder(),
&s.world_cache_active_cells_count.slice(..),
"solari_lighting/world_cache_active_cells_count",
);
}
/// Initializes the Solari lighting pipelines at render startup.
pub fn init_solari_lighting_pipelines(
mut commands: Commands,
pipeline_cache: Res<PipelineCache>,
scene_bindings: Res<RaytracingSceneBindings>,
asset_server: Res<AssetServer>,
) {
let bind_group_layout = BindGroupLayoutDescriptor::new(
"solari_lighting_bind_group_layout",
&BindGroupLayoutEntries::sequential(
ShaderStages::COMPUTE,
(
texture_storage_2d(TextureFormat::Rgba16Float, StorageTextureAccess::ReadWrite),
storage_buffer_sized(false, None),
storage_buffer_sized(false, None),
texture_storage_2d(TextureFormat::Rgba32Uint, StorageTextureAccess::ReadWrite),
texture_storage_2d(TextureFormat::Rgba32Uint, StorageTextureAccess::ReadWrite),
storage_buffer_sized(false, None),
storage_buffer_sized(false, None),
texture_2d(TextureSampleType::Uint),
texture_depth_2d(),
texture_2d(TextureSampleType::Float { filterable: true }),
texture_2d(TextureSampleType::Uint),
texture_depth_2d(),
uniform_buffer::<ViewUniform>(true),
uniform_buffer::<PreviousViewData>(true),
storage_buffer_sized(false, None),
storage_buffer_sized(false, None),
storage_buffer_sized(false, None),
storage_buffer_sized(false, None),
storage_buffer_sized(false, None),
storage_buffer_sized(false, None),
storage_buffer_sized(false, None),
storage_buffer_sized(false, None),
storage_buffer_sized(false, None),
storage_buffer_sized(false, None),
),
),
);
let bind_group_layout_world_cache_active_cells_dispatch = BindGroupLayoutDescriptor::new(
"solari_lighting_bind_group_layout_world_cache_active_cells_dispatch",
&BindGroupLayoutEntries::single(ShaderStages::COMPUTE, storage_buffer_sized(false, None)),
);
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
let bind_group_layout_resolve_dlss_rr_textures = BindGroupLayoutDescriptor::new(
"solari_lighting_bind_group_layout_resolve_dlss_rr_textures",
&BindGroupLayoutEntries::sequential(
ShaderStages::COMPUTE,
(
texture_storage_2d(TextureFormat::Rgba8Unorm, StorageTextureAccess::WriteOnly),
texture_storage_2d(TextureFormat::Rgba8Unorm, StorageTextureAccess::WriteOnly),
texture_storage_2d(TextureFormat::Rgba16Float, StorageTextureAccess::WriteOnly),
texture_storage_2d(TextureFormat::Rg16Float, StorageTextureAccess::WriteOnly),
),
),
);
let create_pipeline = |label: &'static str,
entry_point: &'static str,
shader: Handle<Shader>,
extra_bind_group_layout: Option<&BindGroupLayoutDescriptor>,
extra_shader_defs: Vec<ShaderDefVal>| {
let mut layout = vec![
scene_bindings.bind_group_layout.clone(),
bind_group_layout.clone(),
];
if let Some(extra_bind_group_layout) = extra_bind_group_layout {
layout.push(extra_bind_group_layout.clone());
}
let mut shader_defs = vec![ShaderDefVal::UInt(
"WORLD_CACHE_SIZE".into(),
WORLD_CACHE_SIZE as u32,
)];
shader_defs.extend_from_slice(&extra_shader_defs);
pipeline_cache.queue_compute_pipeline(ComputePipelineDescriptor {
label: Some(label.into()),
layout,
immediate_size: 8,
shader,
shader_defs,
entry_point: Some(entry_point.into()),
..default()
})
};
commands.insert_resource(SolariLightingPipelines {
bind_group_layout: bind_group_layout.clone(),
bind_group_layout_world_cache_active_cells_dispatch:
bind_group_layout_world_cache_active_cells_dispatch.clone(),
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
bind_group_layout_resolve_dlss_rr_textures: bind_group_layout_resolve_dlss_rr_textures
.clone(),
decay_world_cache_pipeline: create_pipeline(
"solari_lighting_decay_world_cache_pipeline",
"decay_world_cache",
load_embedded_asset!(asset_server.as_ref(), "world_cache_compact.wgsl"),
Some(&bind_group_layout_world_cache_active_cells_dispatch),
vec!["WORLD_CACHE_NON_ATOMIC_LIFE_BUFFER".into()],
),
compact_world_cache_single_block_pipeline: create_pipeline(
"solari_lighting_compact_world_cache_single_block_pipeline",
"compact_world_cache_single_block",
load_embedded_asset!(asset_server.as_ref(), "world_cache_compact.wgsl"),
Some(&bind_group_layout_world_cache_active_cells_dispatch),
vec!["WORLD_CACHE_NON_ATOMIC_LIFE_BUFFER".into()],
),
compact_world_cache_blocks_pipeline: create_pipeline(
"solari_lighting_compact_world_cache_blocks_pipeline",
"compact_world_cache_blocks",
load_embedded_asset!(asset_server.as_ref(), "world_cache_compact.wgsl"),
Some(&bind_group_layout_world_cache_active_cells_dispatch),
vec![],
),
compact_world_cache_write_active_cells_pipeline: create_pipeline(
"solari_lighting_compact_world_cache_write_active_cells_pipeline",
"compact_world_cache_write_active_cells",
load_embedded_asset!(asset_server.as_ref(), "world_cache_compact.wgsl"),
Some(&bind_group_layout_world_cache_active_cells_dispatch),
vec!["WORLD_CACHE_NON_ATOMIC_LIFE_BUFFER".into()],
),
sample_di_for_world_cache_pipeline: create_pipeline(
"solari_lighting_sample_di_for_world_cache_pipeline",
"sample_di",
load_embedded_asset!(asset_server.as_ref(), "world_cache_update.wgsl"),
None,
vec![],
),
sample_gi_for_world_cache_pipeline: create_pipeline(
"solari_lighting_sample_gi_for_world_cache_pipeline",
"sample_gi",
load_embedded_asset!(asset_server.as_ref(), "world_cache_update.wgsl"),
None,
vec!["WORLD_CACHE_QUERY_ATOMIC_MAX_LIFETIME".into()],
),
blend_new_world_cache_samples_pipeline: create_pipeline(
"solari_lighting_blend_new_world_cache_samples_pipeline",
"blend_new_samples",
load_embedded_asset!(asset_server.as_ref(), "world_cache_update.wgsl"),
None,
vec![],
),
presample_light_tiles_pipeline: create_pipeline(
"solari_lighting_presample_light_tiles_pipeline",
"presample_light_tiles",
load_embedded_asset!(asset_server.as_ref(), "presample_light_tiles.wgsl"),
None,
vec![],
),
di_initial_and_temporal_pipeline: create_pipeline(
"solari_lighting_di_initial_and_temporal_pipeline",
"initial_and_temporal",
load_embedded_asset!(asset_server.as_ref(), "restir_di.wgsl"),
None,
vec![],
),
di_spatial_and_shade_pipeline: create_pipeline(
"solari_lighting_di_spatial_and_shade_pipeline",
"spatial_and_shade",
load_embedded_asset!(asset_server.as_ref(), "restir_di.wgsl"),
None,
vec![],
),
gi_initial_and_temporal_pipeline: create_pipeline(
"solari_lighting_gi_initial_and_temporal_pipeline",
"initial_and_temporal",
load_embedded_asset!(asset_server.as_ref(), "restir_gi.wgsl"),
None,
vec!["WORLD_CACHE_FIRST_BOUNCE_LIGHT_LEAK_PREVENTION".into()],
),
gi_spatial_and_shade_pipeline: create_pipeline(
"solari_lighting_gi_spatial_and_shade_pipeline",
"spatial_and_shade",
load_embedded_asset!(asset_server.as_ref(), "restir_gi.wgsl"),
None,
vec![],
),
specular_gi_pipeline: create_pipeline(
"solari_lighting_specular_gi_pipeline",
"specular_gi",
load_embedded_asset!(asset_server.as_ref(), "specular_gi.wgsl"),
None,
vec![],
),
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
specular_gi_with_psr_pipeline: create_pipeline(
"solari_lighting_specular_gi_with_psr_pipeline",
"specular_gi",
load_embedded_asset!(asset_server.as_ref(), "specular_gi.wgsl"),
Some(&bind_group_layout_resolve_dlss_rr_textures),
vec!["DLSS_RR_GUIDE_BUFFERS".into()],
),
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
resolve_dlss_rr_textures_pipeline: create_pipeline(
"solari_lighting_resolve_dlss_rr_textures_pipeline",
"resolve_dlss_rr_textures",
load_embedded_asset!(asset_server.as_ref(), "resolve_dlss_rr_textures.wgsl"),
Some(&bind_group_layout_resolve_dlss_rr_textures),
vec!["DLSS_RR_GUIDE_BUFFERS".into()],
),
});
}
+324
View File
@@ -0,0 +1,324 @@
use super::SolariLighting;
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
use bevy_anti_alias::dlss::{
Dlss, DlssRayReconstructionFeature, ViewDlssRayReconstructionTextures,
};
use bevy_camera::MainPassResolutionOverride;
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
use bevy_ecs::query::Has;
use bevy_ecs::{
component::Component,
entity::Entity,
query::With,
system::{Commands, Query, Res},
};
use bevy_image::ToExtents;
use bevy_math::UVec2;
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
use bevy_render::texture::CachedTexture;
use bevy_render::{
camera::ExtractedCamera,
render_resource::{
Buffer, BufferDescriptor, BufferUsages, TextureDescriptor, TextureDimension, TextureFormat,
TextureUsages, TextureView, TextureViewDescriptor,
},
renderer::RenderDevice,
};
/// Size of the `LightSample` shader struct in bytes.
const LIGHT_SAMPLE_STRUCT_SIZE: u64 = 8;
/// Size of the `ResolvedLightSamplePacked` shader struct in bytes.
const RESOLVED_LIGHT_SAMPLE_STRUCT_SIZE: u64 = 24;
/// Size of the GI `Reservoir` shader struct in bytes.
const GI_RESERVOIR_STRUCT_SIZE: u64 = 48;
pub const LIGHT_TILE_BLOCKS: u64 = 128;
pub const LIGHT_TILE_SAMPLES_PER_BLOCK: u64 = 1024;
/// Amount of entries in the world cache (must be a power of 2, and >= 2^10)
pub const WORLD_CACHE_SIZE: u64 = 2u64.pow(20);
/// Internal rendering resources used for Solari lighting.
#[derive(Component)]
pub struct SolariLightingResources {
pub light_tile_samples: Buffer,
pub light_tile_resolved_samples: Buffer,
pub di_reservoirs_a: TextureView,
pub di_reservoirs_b: TextureView,
pub gi_reservoirs_a: Buffer,
pub gi_reservoirs_b: Buffer,
pub world_cache_checksums: Buffer,
pub world_cache_life: Buffer,
pub world_cache_radiance: Buffer,
pub world_cache_geometry_data: Buffer,
pub world_cache_luminance_deltas: Buffer,
pub world_cache_active_cells_new_radiance: Buffer,
pub world_cache_a: Buffer,
pub world_cache_b: Buffer,
pub world_cache_active_cell_indices: Buffer,
pub world_cache_active_cells_count: Buffer,
pub world_cache_active_cells_dispatch: Buffer,
pub view_size: UVec2,
}
pub fn prepare_solari_lighting_resources(
#[cfg(any(not(feature = "dlss"), feature = "force_disable_dlss"))] query: Query<
(
Entity,
&ExtractedCamera,
Option<&SolariLightingResources>,
Option<&MainPassResolutionOverride>,
),
With<SolariLighting>,
>,
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))] query: Query<
(
Entity,
&ExtractedCamera,
Option<&SolariLightingResources>,
Option<&MainPassResolutionOverride>,
Has<Dlss<DlssRayReconstructionFeature>>,
),
With<SolariLighting>,
>,
render_device: Res<RenderDevice>,
mut commands: Commands,
) {
for query_item in &query {
#[cfg(any(not(feature = "dlss"), feature = "force_disable_dlss"))]
let (entity, camera, solari_lighting_resources, resolution_override) = query_item;
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
let (entity, camera, solari_lighting_resources, resolution_override, has_dlss_rr) =
query_item;
let Some(mut view_size) = camera.physical_viewport_size else {
continue;
};
if let Some(MainPassResolutionOverride(resolution_override)) = resolution_override {
view_size = *resolution_override;
}
if solari_lighting_resources.map(|r| r.view_size) == Some(view_size) {
continue;
}
let light_tile_samples = render_device.create_buffer(&BufferDescriptor {
label: Some("solari_lighting_light_tile_samples"),
size: LIGHT_TILE_BLOCKS * LIGHT_TILE_SAMPLES_PER_BLOCK * LIGHT_SAMPLE_STRUCT_SIZE,
usage: BufferUsages::STORAGE,
mapped_at_creation: false,
});
let light_tile_resolved_samples = render_device.create_buffer(&BufferDescriptor {
label: Some("solari_lighting_light_tile_resolved_samples"),
size: LIGHT_TILE_BLOCKS
* LIGHT_TILE_SAMPLES_PER_BLOCK
* RESOLVED_LIGHT_SAMPLE_STRUCT_SIZE,
usage: BufferUsages::STORAGE,
mapped_at_creation: false,
});
let di_reservoirs = |name| {
render_device
.create_texture(&TextureDescriptor {
label: Some(name),
size: view_size.to_extents(),
mip_level_count: 1,
sample_count: 1,
dimension: TextureDimension::D2,
format: TextureFormat::Rgba32Uint,
usage: TextureUsages::STORAGE_BINDING,
view_formats: &[],
})
.create_view(&TextureViewDescriptor::default())
};
let di_reservoirs_a = di_reservoirs("solari_lighting_di_reservoirs_a");
let di_reservoirs_b = di_reservoirs("solari_lighting_di_reservoirs_b");
let gi_reservoirs = |name| {
render_device.create_buffer(&BufferDescriptor {
label: Some(name),
size: (view_size.x * view_size.y) as u64 * GI_RESERVOIR_STRUCT_SIZE,
usage: BufferUsages::STORAGE,
mapped_at_creation: false,
})
};
let gi_reservoirs_a = gi_reservoirs("solari_lighting_gi_reservoirs_a");
let gi_reservoirs_b = gi_reservoirs("solari_lighting_gi_reservoirs_b");
let world_cache_checksums = render_device.create_buffer(&BufferDescriptor {
label: Some("solari_lighting_world_cache_checksums"),
size: WORLD_CACHE_SIZE * size_of::<u32>() as u64,
usage: BufferUsages::STORAGE,
mapped_at_creation: false,
});
let world_cache_life = render_device.create_buffer(&BufferDescriptor {
label: Some("solari_lighting_world_cache_life"),
size: WORLD_CACHE_SIZE * size_of::<u32>() as u64,
usage: BufferUsages::STORAGE,
mapped_at_creation: false,
});
let world_cache_radiance = render_device.create_buffer(&BufferDescriptor {
label: Some("solari_lighting_world_cache_radiance"),
size: WORLD_CACHE_SIZE * size_of::<[f32; 4]>() as u64,
usage: BufferUsages::STORAGE,
mapped_at_creation: false,
});
let world_cache_geometry_data = render_device.create_buffer(&BufferDescriptor {
label: Some("solari_lighting_world_cache_geometry_data"),
size: WORLD_CACHE_SIZE * size_of::<[f32; 8]>() as u64,
usage: BufferUsages::STORAGE,
mapped_at_creation: false,
});
let world_cache_luminance_deltas = render_device.create_buffer(&BufferDescriptor {
label: Some("solari_lighting_world_cache_luminance_deltas"),
size: WORLD_CACHE_SIZE * size_of::<f32>() as u64,
usage: BufferUsages::STORAGE,
mapped_at_creation: false,
});
let world_cache_active_cells_new_radiance =
render_device.create_buffer(&BufferDescriptor {
label: Some("solari_lighting_world_cache_active_cells_new_radiance"),
size: WORLD_CACHE_SIZE * size_of::<[f32; 4]>() as u64,
usage: BufferUsages::STORAGE,
mapped_at_creation: false,
});
let world_cache_a = render_device.create_buffer(&BufferDescriptor {
label: Some("solari_lighting_world_cache_a"),
size: WORLD_CACHE_SIZE * size_of::<u32>() as u64,
usage: BufferUsages::STORAGE,
mapped_at_creation: false,
});
let world_cache_b = render_device.create_buffer(&BufferDescriptor {
label: Some("solari_lighting_world_cache_b"),
size: 1024 * size_of::<u32>() as u64,
usage: BufferUsages::STORAGE,
mapped_at_creation: false,
});
let world_cache_active_cell_indices = render_device.create_buffer(&BufferDescriptor {
label: Some("solari_lighting_world_cache_active_cell_indices"),
size: WORLD_CACHE_SIZE * size_of::<u32>() as u64,
usage: BufferUsages::STORAGE,
mapped_at_creation: false,
});
let world_cache_active_cells_count = render_device.create_buffer(&BufferDescriptor {
label: Some("solari_lighting_world_cache_active_cells_count"),
size: size_of::<u32>() as u64,
usage: BufferUsages::STORAGE | BufferUsages::COPY_SRC,
mapped_at_creation: false,
});
let world_cache_active_cells_dispatch = render_device.create_buffer(&BufferDescriptor {
label: Some("solari_lighting_world_cache_active_cells_dispatch"),
size: size_of::<[u32; 3]>() as u64,
usage: BufferUsages::INDIRECT | BufferUsages::STORAGE,
mapped_at_creation: false,
});
commands.entity(entity).insert(SolariLightingResources {
light_tile_samples,
light_tile_resolved_samples,
di_reservoirs_a,
di_reservoirs_b,
gi_reservoirs_a,
gi_reservoirs_b,
world_cache_checksums,
world_cache_life,
world_cache_radiance,
world_cache_geometry_data,
world_cache_luminance_deltas,
world_cache_active_cells_new_radiance,
world_cache_a,
world_cache_b,
world_cache_active_cell_indices,
world_cache_active_cells_count,
world_cache_active_cells_dispatch,
view_size,
});
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
if has_dlss_rr {
let diffuse_albedo = render_device.create_texture(&TextureDescriptor {
label: Some("solari_lighting_diffuse_albedo"),
size: view_size.to_extents(),
mip_level_count: 1,
sample_count: 1,
dimension: TextureDimension::D2,
format: TextureFormat::Rgba8Unorm,
usage: TextureUsages::TEXTURE_BINDING | TextureUsages::STORAGE_BINDING,
view_formats: &[],
});
let diffuse_albedo_view = diffuse_albedo.create_view(&TextureViewDescriptor::default());
let specular_albedo = render_device.create_texture(&TextureDescriptor {
label: Some("solari_lighting_specular_albedo"),
size: view_size.to_extents(),
mip_level_count: 1,
sample_count: 1,
dimension: TextureDimension::D2,
format: TextureFormat::Rgba8Unorm,
usage: TextureUsages::TEXTURE_BINDING | TextureUsages::STORAGE_BINDING,
view_formats: &[],
});
let specular_albedo_view =
specular_albedo.create_view(&TextureViewDescriptor::default());
let normal_roughness = render_device.create_texture(&TextureDescriptor {
label: Some("solari_lighting_normal_roughness"),
size: view_size.to_extents(),
mip_level_count: 1,
sample_count: 1,
dimension: TextureDimension::D2,
format: TextureFormat::Rgba16Float,
usage: TextureUsages::TEXTURE_BINDING | TextureUsages::STORAGE_BINDING,
view_formats: &[],
});
let normal_roughness_view =
normal_roughness.create_view(&TextureViewDescriptor::default());
let specular_motion_vectors = render_device.create_texture(&TextureDescriptor {
label: Some("solari_lighting_specular_motion_vectors"),
size: view_size.to_extents(),
mip_level_count: 1,
sample_count: 1,
dimension: TextureDimension::D2,
format: TextureFormat::Rg16Float,
usage: TextureUsages::TEXTURE_BINDING | TextureUsages::STORAGE_BINDING,
view_formats: &[],
});
let specular_motion_vectors_view =
specular_motion_vectors.create_view(&TextureViewDescriptor::default());
commands
.entity(entity)
.insert(ViewDlssRayReconstructionTextures {
diffuse_albedo: CachedTexture {
texture: diffuse_albedo,
default_view: diffuse_albedo_view,
},
specular_albedo: CachedTexture {
texture: specular_albedo,
default_view: specular_albedo_view,
},
normal_roughness: CachedTexture {
texture: normal_roughness,
default_view: normal_roughness_view,
},
specular_motion_vectors: CachedTexture {
texture: specular_motion_vectors,
default_view: specular_motion_vectors_view,
},
});
}
}
}
@@ -0,0 +1,43 @@
// https://cwyman.org/papers/hpg21_rearchitectingReSTIR.pdf
enable wgpu_ray_query;
#define_import_path bevy_solari::presample_light_tiles
#import bevy_pbr::rgb9e5::{vec3_to_rgb9e5_, rgb9e5_to_vec3_}
#import bevy_pbr::utils::{octahedral_encode, octahedral_decode}
#import bevy_render::view::View
#import bevy_solari::sampling::{generate_random_light_sample, LightSample, ResolvedLightSample}
#import bevy_solari::realtime_bindings::{light_tile_samples, light_tile_resolved_samples, view, constants, ResolvedLightSamplePacked}
@compute @workgroup_size(1024, 1, 1)
fn presample_light_tiles(@builtin(workgroup_id) workgroup_id: vec3<u32>, @builtin(local_invocation_index) sample_index: u32) {
let tile_id = workgroup_id.x;
var rng = (tile_id * 5782582u) + sample_index + constants.frame_index;
let sample = generate_random_light_sample(&rng);
let i = (tile_id * 1024u) + sample_index;
light_tile_samples[i] = sample.light_sample;
light_tile_resolved_samples[i] = pack_resolved_light_sample(sample.resolved_light_sample);
}
fn pack_resolved_light_sample(sample: ResolvedLightSample) -> ResolvedLightSamplePacked {
return ResolvedLightSamplePacked(
sample.world_position.x,
sample.world_position.y,
sample.world_position.z,
pack2x16unorm(octahedral_encode(sample.world_normal)),
vec3_to_rgb9e5_(log2(sample.radiance * view.exposure + 1.0)),
sample.inverse_pdf * select(1.0, -1.0, sample.world_position.w == 0.0),
);
}
fn unpack_resolved_light_sample(packed: ResolvedLightSamplePacked, exposure: f32) -> ResolvedLightSample {
return ResolvedLightSample(
vec4(packed.world_position_x, packed.world_position_y, packed.world_position_z, select(1.0, 0.0, packed.inverse_pdf < 0.0)),
octahedral_decode(unpack2x16unorm(packed.world_normal)),
(exp2(rgb9e5_to_vec3_(packed.radiance)) - 1.0) / exposure,
abs(packed.inverse_pdf),
);
}
@@ -0,0 +1,74 @@
enable wgpu_ray_query;
#define_import_path bevy_solari::realtime_bindings
#import bevy_render::view::View
#import bevy_pbr::prepass_bindings::PreviousViewUniforms
#import bevy_solari::sampling::LightSample
@group(1) @binding(0) var view_output: texture_storage_2d<rgba16float, read_write>;
@group(1) @binding(1) var<storage, read_write> light_tile_samples: array<LightSample>;
@group(1) @binding(2) var<storage, read_write> light_tile_resolved_samples: array<ResolvedLightSamplePacked>;
@group(1) @binding(3) var di_reservoirs_a: texture_storage_2d<rgba32uint, read_write>;
@group(1) @binding(4) var di_reservoirs_b: texture_storage_2d<rgba32uint, read_write>;
@group(1) @binding(5) var<storage, read_write> gi_reservoirs_a: array<Reservoir>;
@group(1) @binding(6) var<storage, read_write> gi_reservoirs_b: array<Reservoir>;
@group(1) @binding(7) var gbuffer: texture_2d<u32>;
@group(1) @binding(8) var depth_buffer: texture_depth_2d;
@group(1) @binding(9) var motion_vectors: texture_2d<f32>;
@group(1) @binding(10) var previous_gbuffer: texture_2d<u32>;
@group(1) @binding(11) var previous_depth_buffer: texture_depth_2d;
@group(1) @binding(12) var<uniform> view: View;
@group(1) @binding(13) var<uniform> previous_view: PreviousViewUniforms;
@group(1) @binding(14) var<storage, read_write> world_cache_checksums: array<atomic<u32>, #{WORLD_CACHE_SIZE}>;
#ifdef WORLD_CACHE_NON_ATOMIC_LIFE_BUFFER
@group(1) @binding(15) var<storage, read_write> world_cache_life: array<u32, #{WORLD_CACHE_SIZE}>;
#else
@group(1) @binding(15) var<storage, read_write> world_cache_life: array<atomic<u32>, #{WORLD_CACHE_SIZE}>;
#endif
@group(1) @binding(16) var<storage, read_write> world_cache_radiance: array<vec4<f32>, #{WORLD_CACHE_SIZE}>;
@group(1) @binding(17) var<storage, read_write> world_cache_geometry_data: array<WorldCacheGeometryData, #{WORLD_CACHE_SIZE}>;
@group(1) @binding(18) var<storage, read_write> world_cache_luminance_deltas: array<f32, #{WORLD_CACHE_SIZE}>;
@group(1) @binding(19) var<storage, read_write> world_cache_active_cells_new_radiance: array<vec3<f32>, #{WORLD_CACHE_SIZE}>;
@group(1) @binding(20) var<storage, read_write> world_cache_a: array<u32, #{WORLD_CACHE_SIZE}>;
@group(1) @binding(21) var<storage, read_write> world_cache_b: array<u32, 1024u>;
@group(1) @binding(22) var<storage, read_write> world_cache_active_cell_indices: array<u32, #{WORLD_CACHE_SIZE}>;
@group(1) @binding(23) var<storage, read_write> world_cache_active_cells_count: u32;
#ifdef DLSS_RR_GUIDE_BUFFERS
@group(2) @binding(0) var diffuse_albedo: texture_storage_2d<rgba8unorm, write>;
@group(2) @binding(1) var specular_albedo: texture_storage_2d<rgba8unorm, write>;
@group(2) @binding(2) var normal_roughness: texture_storage_2d<rgba16float, write>;
@group(2) @binding(3) var specular_motion_vectors: texture_storage_2d<rg16float, write>;
#endif
struct PushConstants { frame_index: u32, reset: u32 }
var<immediate> constants: PushConstants;
// Don't adjust the size of this struct without also adjusting `prepare::RESOLVED_LIGHT_SAMPLE_STRUCT_SIZE`.
struct ResolvedLightSamplePacked {
world_position_x: f32,
world_position_y: f32,
world_position_z: f32,
world_normal: u32,
radiance: u32,
inverse_pdf: f32,
}
// Don't adjust the size of this struct without also adjusting `prepare::GI_RESERVOIR_STRUCT_SIZE`.
struct Reservoir {
sample_point_world_position: vec3<f32>,
weight_sum: f32,
radiance: vec3<f32>,
confidence_weight: f32,
sample_point_world_normal: vec3<f32>,
unbiased_contribution_weight: f32,
}
struct WorldCacheGeometryData {
world_position: vec3<f32>,
padding_a: u32,
world_normal: vec3<f32>,
padding_b: u32
}
@@ -0,0 +1,59 @@
enable wgpu_ray_query;
#define_import_path bevy_solari::resolve_dlss_rr_textures
#import bevy_pbr::pbr_functions::{calculate_diffuse_color, calculate_F0}
#import bevy_render::view::View
#import bevy_solari::gbuffer_utils::gpixel_resolve
#import bevy_solari::realtime_bindings::{gbuffer, depth_buffer, motion_vectors, view, diffuse_albedo, specular_albedo, normal_roughness, specular_motion_vectors}
@compute @workgroup_size(8, 8, 1)
fn resolve_dlss_rr_textures(@builtin(global_invocation_id) global_id: vec3<u32>) {
let pixel_id = global_id.xy;
if any(pixel_id >= vec2u(view.main_pass_viewport.zw)) { return; }
textureStore(specular_motion_vectors, pixel_id, textureLoad(motion_vectors, pixel_id, 0));
let depth = textureLoad(depth_buffer, global_id.xy, 0);
if depth == 0.0 {
textureStore(diffuse_albedo, pixel_id, vec4(0.0));
textureStore(specular_albedo, pixel_id, vec4(0.5));
textureStore(normal_roughness, pixel_id, vec4(0.0, 0.0, 1.0, 0.0));
return;
}
let surface = gpixel_resolve(textureLoad(gbuffer, pixel_id, 0), depth, pixel_id, view.main_pass_viewport.zw, view.world_from_clip);
let F0 = calculate_F0(surface.material.base_color, surface.material.metallic, vec3(surface.material.reflectance));
let wo = normalize(view.world_position - surface.world_position);
textureStore(diffuse_albedo, pixel_id, vec4(calculate_diffuse_color(surface.material.base_color, surface.material.metallic, 0.0, 0.0), 0.0));
textureStore(specular_albedo, pixel_id, vec4(env_brdf_approx2(F0, surface.material.roughness, surface.world_normal, wo), 0.0));
textureStore(normal_roughness, pixel_id, vec4(surface.world_normal, surface.material.perceptual_roughness));
}
fn env_brdf_approx2(specular_color: vec3<f32>, alpha: f32, N: vec3<f32>, V: vec3<f32>) -> vec3<f32> {
let NoV = abs(dot(N, V));
var X: vec4<f32>;
X.x = 1.0;
X.y = NoV;
X.z = NoV * NoV;
X.w = NoV * X.z;
var Y: vec4<f32>;
Y.x = 1.0;
Y.y = alpha;
Y.z = alpha * alpha;
Y.w = alpha * Y.z;
let M1 = mat2x2<f32>(0.99044, 1.29678, -1.28514, -0.755907);
let M2 = mat3x3<f32>(1.0, 20.3225, 121.563, 2.92338, -27.0302, 626.13, 59.4188, 222.592, 316.627);
let M3 = mat2x2<f32>(0.0365463, 9.0632, 3.32707, -9.04756);
let M4 = mat3x3<f32>(1.0, 9.04401, 5.56589, 3.59685, -16.3174, 19.7886, -1.36772, 9.22949, -20.2123);
var bias = dot(M1 * X.xy, Y.xy) / dot(M2 * X.xyw, Y.xyw);
let scale = dot(M3 * X.xy, Y.xy) / dot(M4 * X.xzw, Y.xyw);
bias *= saturate(specular_color.g * 50.0);
return fma(specular_color, vec3(max(0.0, scale)), vec3(max(0.0, bias)));
}
+339
View File
@@ -0,0 +1,339 @@
// https://intro-to-restir.cwyman.org/presentations/2023ReSTIR_Course_Notes.pdf
// https://d1qx31qr3h6wln.cloudfront.net/publications/ReSTIR%20GI.pdf
enable wgpu_ray_query;
#import bevy_core_pipeline::tonemapping::tonemapping_luminance as luminance
#import bevy_pbr::prepass_bindings::PreviousViewUniforms
#import bevy_pbr::utils::{rand_f, rand_range_u, sample_disk}
#import bevy_render::maths::PI
#import bevy_render::view::View
#import bevy_solari::brdf::{evaluate_diffuse_brdf, evaluate_specular_brdf}
#import bevy_solari::gbuffer_utils::{gpixel_resolve, pixel_dissimilar, permute_pixel}
#import bevy_solari::presample_light_tiles::unpack_resolved_light_sample
#import bevy_solari::sampling::{LightSample, ResolvedLightSample, NULL_LIGHT_ID, calculate_resolved_light_contribution, resolve_and_calculate_light_contribution, resolve_light_sample, trace_light_visibility, balance_heuristic}
#import bevy_solari::scene_bindings::{light_sources, previous_frame_light_id_translations, LIGHT_NOT_PRESENT_THIS_FRAME, RAY_T_MIN}
#import bevy_solari::specular_gi::SPECULAR_GI_FOR_DI_ROUGHNESS_THRESHOLD
#import bevy_solari::realtime_bindings::{view_output, light_tile_samples, light_tile_resolved_samples, di_reservoirs_a, di_reservoirs_b, gbuffer, depth_buffer, motion_vectors, previous_gbuffer, previous_depth_buffer, view, previous_view, constants, ResolvedLightSamplePacked}
const INITIAL_SAMPLES = 8u;
const SPATIAL_REUSE_RADIUS_PIXELS = 30.0;
const CONFIDENCE_WEIGHT_CAP = 20.0;
@compute @workgroup_size(8, 8, 1)
fn initial_and_temporal(@builtin(workgroup_id) workgroup_id: vec3<u32>, @builtin(global_invocation_id) global_id: vec3<u32>) {
if any(global_id.xy >= vec2u(view.main_pass_viewport.zw)) { return; }
let pixel_index = global_id.x + global_id.y * u32(view.main_pass_viewport.z);
var rng = pixel_index + constants.frame_index;
let depth = textureLoad(depth_buffer, global_id.xy, 0);
if depth == 0.0 {
store_reservoir_b(global_id.xy, empty_reservoir());
return;
}
let surface = gpixel_resolve(textureLoad(gbuffer, global_id.xy, 0), depth, global_id.xy, view.main_pass_viewport.zw, view.world_from_clip);
let diffuse_brdf = surface.material.base_color / PI;
let initial_reservoir = generate_initial_reservoir(surface.world_position, surface.world_normal, diffuse_brdf, workgroup_id.xy, &rng);
let temporal = load_temporal_reservoir(global_id.xy, depth, surface.world_position, surface.world_normal);
let merge_result = merge_reservoirs(initial_reservoir, surface.world_position, surface.world_normal, diffuse_brdf,
temporal.reservoir, temporal.world_position, temporal.world_normal, temporal.diffuse_brdf, &rng);
store_reservoir_b(global_id.xy, merge_result.merged_reservoir);
}
@compute @workgroup_size(8, 8, 1)
fn spatial_and_shade(@builtin(global_invocation_id) global_id: vec3<u32>) {
if any(global_id.xy >= vec2u(view.main_pass_viewport.zw)) { return; }
let pixel_index = global_id.x + global_id.y * u32(view.main_pass_viewport.z);
var rng = pixel_index + constants.frame_index;
let depth = textureLoad(depth_buffer, global_id.xy, 0);
if depth == 0.0 {
store_reservoir_a(global_id.xy, empty_reservoir());
return;
}
let surface = gpixel_resolve(textureLoad(gbuffer, global_id.xy, 0), depth, global_id.xy, view.main_pass_viewport.zw, view.world_from_clip);
let diffuse_brdf = surface.material.base_color / PI;
let input_reservoir = load_reservoir_b(global_id.xy);
let spatial = load_spatial_reservoir(global_id.xy, depth, surface.world_position, surface.world_normal, &rng);
let merge_result = merge_reservoirs(input_reservoir, surface.world_position, surface.world_normal, diffuse_brdf,
spatial.reservoir, spatial.world_position, spatial.world_normal, spatial.diffuse_brdf, &rng);
var combined_reservoir = merge_result.merged_reservoir;
// More accuracy, less stability
#ifndef BIASED_RESAMPLING
store_reservoir_a(global_id.xy, combined_reservoir);
#endif
if reservoir_valid(combined_reservoir) {
combined_reservoir.unbiased_contribution_weight *= trace_light_visibility(surface.world_position + (surface.world_normal * RAY_T_MIN), merge_result.selected_light_world_position);
}
// More stability, less accuracy (shadows extend further out than they should)
#ifdef BIASED_RESAMPLING
store_reservoir_a(global_id.xy, combined_reservoir);
#endif
let wo = normalize(view.world_position - surface.world_position);
var brdf = evaluate_diffuse_brdf(wo, merge_result.wi, surface.world_normal, surface.material);
// Only consider the specular lobe if the surface is not smooth, else leave it for the specular GI pass to handle
if surface.material.roughness > SPECULAR_GI_FOR_DI_ROUGHNESS_THRESHOLD {
brdf += evaluate_specular_brdf(wo, merge_result.wi, surface.world_normal, surface.material);
}
var pixel_color = merge_result.selected_sample_radiance * combined_reservoir.unbiased_contribution_weight;
pixel_color *= brdf;
pixel_color += surface.material.emissive;
pixel_color *= view.exposure;
textureStore(view_output, global_id.xy, vec4(pixel_color, 1.0));
}
fn generate_initial_reservoir(world_position: vec3<f32>, world_normal: vec3<f32>, diffuse_brdf: vec3<f32>, workgroup_id: vec2<u32>, rng: ptr<function, u32>) -> Reservoir {
var workgroup_rng = (workgroup_id.x * 5782582u) + workgroup_id.y;
let light_tile_start = rand_range_u(128u, &workgroup_rng) * 1024u;
var reservoir = empty_reservoir();
var weight_sum = 0.0;
let mis_weight = 1.0 / f32(INITIAL_SAMPLES);
var reservoir_target_function = 0.0;
var light_sample_world_position = vec4(0.0);
var selected_tile_sample = 0u;
for (var i = 0u; i < INITIAL_SAMPLES; i++) {
let tile_sample = light_tile_start + rand_range_u(1024u, rng);
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 target_function = luminance(light_contribution.radiance * diffuse_brdf * saturate(dot(light_contribution.wi, world_normal)));
let resampling_weight = mis_weight * (target_function * light_contribution.inverse_pdf);
weight_sum += resampling_weight;
if rand_f(rng) < resampling_weight / weight_sum {
reservoir_target_function = target_function;
light_sample_world_position = resolved_light_sample.world_position;
selected_tile_sample = tile_sample;
}
}
if reservoir_target_function != 0.0 {
reservoir.sample = light_tile_samples[selected_tile_sample];
}
if reservoir_valid(reservoir) {
let inverse_target_function = select(0.0, 1.0 / reservoir_target_function, reservoir_target_function > 0.0);
reservoir.unbiased_contribution_weight = weight_sum * inverse_target_function;
reservoir.unbiased_contribution_weight *= trace_light_visibility(world_position + (world_normal * RAY_T_MIN), light_sample_world_position);
}
reservoir.confidence_weight = 1.0;
return reservoir;
}
fn load_temporal_reservoir(pixel_id: vec2<u32>, depth: f32, world_position: vec3<f32>, world_normal: vec3<f32>) -> NeighborInfo {
let motion_vector = textureLoad(motion_vectors, pixel_id, 0).xy;
let temporal_pixel_id_float = round(vec2<f32>(pixel_id) - (motion_vector * view.main_pass_viewport.zw));
var point_temporal_pixel_id = vec2<u32>(temporal_pixel_id_float);
if bool(constants.reset) {
return NeighborInfo(empty_reservoir(), vec3(0.0), vec3(0.0), vec3(0.0));
}
if any(temporal_pixel_id_float < vec2(0.0)) || any(temporal_pixel_id_float >= view.main_pass_viewport.zw) {
point_temporal_pixel_id = pixel_id;
}
let permuted_temporal_pixel_id = permute_pixel(point_temporal_pixel_id, constants.frame_index, view.main_pass_viewport.zw);
var temporal = load_temporal_reservoir_inner(permuted_temporal_pixel_id, depth, world_position, world_normal);
// Check if the light selected in the previous frame no longer exists in the current frame (e.g. entity despawned)
let previous_light_id = temporal.reservoir.sample.light_id >> 16u;
let triangle_id = temporal.reservoir.sample.light_id & 0xFFFFu;
let light_id = previous_frame_light_id_translations[previous_light_id];
if light_id == LIGHT_NOT_PRESENT_THIS_FRAME {
return NeighborInfo(empty_reservoir(), vec3(0.0), vec3(0.0), vec3(0.0));
}
temporal.reservoir.sample.light_id = (light_id << 16u) | triangle_id;
temporal.reservoir.confidence_weight = min(temporal.reservoir.confidence_weight, CONFIDENCE_WEIGHT_CAP);
return temporal;
}
fn load_temporal_reservoir_inner(temporal_pixel_id: vec2<u32>, depth: f32, world_position: vec3<f32>, world_normal: vec3<f32>) -> NeighborInfo {
// Check if the pixel features have changed heavily between the current and previous frame
let temporal_depth = textureLoad(previous_depth_buffer, temporal_pixel_id, 0);
let temporal_surface = gpixel_resolve(textureLoad(previous_gbuffer, temporal_pixel_id, 0), temporal_depth, temporal_pixel_id, view.main_pass_viewport.zw, previous_view.world_from_clip);
let temporal_diffuse_brdf = temporal_surface.material.base_color / PI;
if pixel_dissimilar(depth, world_position, temporal_surface.world_position, world_normal, temporal_surface.world_normal, view) {
return NeighborInfo(empty_reservoir(), vec3(0.0), vec3(0.0), vec3(0.0));
}
let temporal_reservoir = load_reservoir_a(temporal_pixel_id);
return NeighborInfo(temporal_reservoir, temporal_surface.world_position, temporal_surface.world_normal, temporal_diffuse_brdf);
}
fn load_spatial_reservoir(pixel_id: vec2<u32>, depth: f32, world_position: vec3<f32>, world_normal: vec3<f32>, rng: ptr<function, u32>) -> NeighborInfo {
for (var i = 0u; i < 5u; i++) {
let spatial_pixel_id = get_neighbor_pixel_id(pixel_id, SPATIAL_REUSE_RADIUS_PIXELS, rng);
let spatial_depth = textureLoad(depth_buffer, spatial_pixel_id, 0);
let spatial_surface = gpixel_resolve(textureLoad(gbuffer, spatial_pixel_id, 0), spatial_depth, spatial_pixel_id, view.main_pass_viewport.zw, view.world_from_clip);
let spatial_diffuse_brdf = spatial_surface.material.base_color / PI;
if pixel_dissimilar(depth, world_position, spatial_surface.world_position, world_normal, spatial_surface.world_normal, view) {
continue;
}
let spatial_reservoir = load_reservoir_b(spatial_pixel_id);
return NeighborInfo(spatial_reservoir, spatial_surface.world_position, spatial_surface.world_normal, spatial_diffuse_brdf);
}
return NeighborInfo(empty_reservoir(), world_position, world_normal, vec3(0.0));
}
fn get_neighbor_pixel_id(center_pixel_id: vec2<u32>, search_radius: f32, rng: ptr<function, u32>) -> vec2<u32> {
var spatial_id = vec2<f32>(center_pixel_id) + sample_disk(search_radius, rng);
spatial_id = clamp(spatial_id, vec2(0.0), view.main_pass_viewport.zw - 1.0);
return vec2<u32>(spatial_id);
}
struct NeighborInfo {
reservoir: Reservoir,
world_position: vec3<f32>,
world_normal: vec3<f32>,
diffuse_brdf: vec3<f32>,
}
struct Reservoir {
sample: LightSample,
confidence_weight: f32,
unbiased_contribution_weight: f32,
}
fn empty_reservoir() -> Reservoir {
return Reservoir(
LightSample(NULL_LIGHT_ID, 0u),
0.0,
0.0,
);
}
fn reservoir_valid(reservoir: Reservoir) -> bool {
return reservoir.sample.light_id != NULL_LIGHT_ID;
}
fn pack_reservoir(reservoir: Reservoir) -> vec4<u32> {
let weights = bitcast<vec2<u32>>(vec2<f32>(reservoir.confidence_weight, reservoir.unbiased_contribution_weight));
return vec4<u32>(reservoir.sample.light_id, reservoir.sample.seed, weights);
}
fn store_reservoir_a(pixel: vec2<u32>, reservoir: Reservoir) {
textureStore(di_reservoirs_a, pixel, pack_reservoir(reservoir));
}
fn store_reservoir_b(pixel: vec2<u32>, reservoir: Reservoir) {
textureStore(di_reservoirs_b, pixel, pack_reservoir(reservoir));
}
fn unpack_reservoir(packed: vec4<u32>) -> Reservoir {
let weights = bitcast<vec2<f32>>(packed.zw);
return Reservoir(LightSample(packed.x, packed.y), weights.x, weights.y);
}
fn load_reservoir_a(pixel: vec2<u32>) -> Reservoir {
return unpack_reservoir(textureLoad(di_reservoirs_a, pixel));
}
fn load_reservoir_b(pixel: vec2<u32>) -> Reservoir {
return unpack_reservoir(textureLoad(di_reservoirs_b, pixel));
}
struct ReservoirMergeResult {
merged_reservoir: Reservoir,
selected_sample_radiance: vec3<f32>,
wi: vec3<f32>,
selected_light_world_position: vec4<f32>,
}
fn merge_reservoirs(
canonical_reservoir: Reservoir,
canonical_world_position: vec3<f32>,
canonical_world_normal: vec3<f32>,
canonical_diffuse_brdf: vec3<f32>,
other_reservoir: Reservoir,
other_world_position: vec3<f32>,
other_world_normal: vec3<f32>,
other_diffuse_brdf: vec3<f32>,
rng: ptr<function, u32>,
) -> ReservoirMergeResult {
// Resolve each light sample once, then evaluate at both positions
var canonical_resolved: ResolvedLightSample;
if reservoir_valid(canonical_reservoir) {
canonical_resolved = resolve_light_sample(canonical_reservoir.sample, light_sources[canonical_reservoir.sample.light_id >> 16u]);
}
var other_resolved: ResolvedLightSample;
if reservoir_valid(other_reservoir) {
other_resolved = resolve_light_sample(other_reservoir.sample, light_sources[other_reservoir.sample.light_id >> 16u]);
}
// Contributions for resampling (evaluate resolved samples at canonical position)
let canonical_contribution_canonical_sample = resolved_reservoir_contribution(canonical_reservoir, canonical_resolved, canonical_world_position, canonical_world_normal, canonical_diffuse_brdf);
let canonical_contribution_other_sample = resolved_reservoir_contribution(other_reservoir, other_resolved, canonical_world_position, canonical_world_normal, canonical_diffuse_brdf);
// Extra contributions for MIS (evaluate resolved samples at other position)
let other_contribution_canonical_sample = resolved_reservoir_contribution(canonical_reservoir, canonical_resolved, other_world_position, other_world_normal, other_diffuse_brdf);
let other_contribution_other_sample = resolved_reservoir_contribution(other_reservoir, other_resolved, other_world_position, other_world_normal, other_diffuse_brdf);
// Resampling weight for canonical sample
let canonical_sample_mis_weight = balance_heuristic(
canonical_reservoir.confidence_weight * canonical_contribution_canonical_sample.target_function,
other_reservoir.confidence_weight * other_contribution_canonical_sample.target_function,
);
let canonical_sample_resampling_weight = canonical_sample_mis_weight * canonical_contribution_canonical_sample.target_function * canonical_reservoir.unbiased_contribution_weight;
// Resampling weight for other sample
let other_sample_mis_weight = balance_heuristic(
other_reservoir.confidence_weight * other_contribution_other_sample.target_function,
canonical_reservoir.confidence_weight * canonical_contribution_other_sample.target_function,
);
let other_sample_resampling_weight = other_sample_mis_weight * canonical_contribution_other_sample.target_function * other_reservoir.unbiased_contribution_weight;
// Perform resampling
var combined_reservoir = empty_reservoir();
combined_reservoir.confidence_weight = canonical_reservoir.confidence_weight + other_reservoir.confidence_weight;
let weight_sum = canonical_sample_resampling_weight + other_sample_resampling_weight;
if rand_f(rng) < other_sample_resampling_weight / weight_sum {
combined_reservoir.sample = other_reservoir.sample;
let inverse_target_function = select(0.0, 1.0 / canonical_contribution_other_sample.target_function, canonical_contribution_other_sample.target_function > 0.0);
combined_reservoir.unbiased_contribution_weight = weight_sum * inverse_target_function;
return ReservoirMergeResult(combined_reservoir, canonical_contribution_other_sample.radiance, canonical_contribution_other_sample.wi, other_resolved.world_position);
} else {
combined_reservoir.sample = canonical_reservoir.sample;
let inverse_target_function = select(0.0, 1.0 / canonical_contribution_canonical_sample.target_function, canonical_contribution_canonical_sample.target_function > 0.0);
combined_reservoir.unbiased_contribution_weight = weight_sum * inverse_target_function;
return ReservoirMergeResult(combined_reservoir, canonical_contribution_canonical_sample.radiance, canonical_contribution_canonical_sample.wi, canonical_resolved.world_position);
}
}
struct ReservoirContribution {
radiance: vec3<f32>,
target_function: f32,
wi: vec3<f32>,
}
fn resolved_reservoir_contribution(reservoir: Reservoir, resolved: ResolvedLightSample, world_position: vec3<f32>, world_normal: vec3<f32>, diffuse_brdf: vec3<f32>) -> ReservoirContribution {
if !reservoir_valid(reservoir) { return ReservoirContribution(vec3(0.0), 0.0, vec3(0.0)); }
let light_contribution = calculate_resolved_light_contribution(resolved, world_position, world_normal);
let target_function = luminance(light_contribution.radiance * diffuse_brdf * saturate(dot(light_contribution.wi, world_normal)));
return ReservoirContribution(light_contribution.radiance, target_function, light_contribution.wi);
}
+318
View File
@@ -0,0 +1,318 @@
// https://intro-to-restir.cwyman.org/presentations/2023ReSTIR_Course_Notes.pdf
enable wgpu_ray_query;
#import bevy_core_pipeline::tonemapping::tonemapping_luminance as luminance
#import bevy_pbr::prepass_bindings::PreviousViewUniforms
#import bevy_pbr::utils::{rand_f, sample_uniform_hemisphere, uniform_hemisphere_inverse_pdf, sample_disk}
#import bevy_render::maths::PI
#import bevy_render::view::View
#import bevy_solari::brdf::evaluate_diffuse_brdf
#import bevy_solari::gbuffer_utils::{gpixel_resolve, pixel_dissimilar, permute_pixel}
#import bevy_solari::sampling::{sample_random_light, trace_point_visibility, balance_heuristic, isnan}
#import bevy_solari::scene_bindings::{trace_ray, resolve_ray_hit_full, RAY_T_MIN, RAY_T_MAX}
#import bevy_solari::world_cache::{query_world_cache, WORLD_CACHE_CELL_LIFETIME}
#import bevy_solari::realtime_bindings::{view_output, gi_reservoirs_a, gi_reservoirs_b, gbuffer, depth_buffer, motion_vectors, previous_gbuffer, previous_depth_buffer, view, previous_view, constants, Reservoir}
#import bevy_solari::specular_gi::DIFFUSE_GI_REUSE_ROUGHNESS_THRESHOLD
const SPATIAL_REUSE_RADIUS_PIXELS = 30.0;
const CONFIDENCE_WEIGHT_CAP = 8.0;
@compute @workgroup_size(8, 8, 1)
fn initial_and_temporal(@builtin(global_invocation_id) global_id: vec3<u32>) {
if any(global_id.xy >= vec2u(view.main_pass_viewport.zw)) { return; }
let pixel_index = global_id.x + global_id.y * u32(view.main_pass_viewport.z);
var rng = pixel_index + constants.frame_index;
let depth = textureLoad(depth_buffer, global_id.xy, 0);
if depth == 0.0 {
gi_reservoirs_b[pixel_index] = empty_reservoir();
return;
}
let surface = gpixel_resolve(textureLoad(gbuffer, global_id.xy, 0), depth, global_id.xy, view.main_pass_viewport.zw, view.world_from_clip);
if surface.material.metallic > 0.9999 && surface.material.roughness <= DIFFUSE_GI_REUSE_ROUGHNESS_THRESHOLD {
gi_reservoirs_b[pixel_index] = empty_reservoir();
return;
}
let initial_reservoir = generate_initial_reservoir(surface.world_position, surface.world_normal, &rng);
let temporal = load_temporal_reservoir(global_id.xy, depth, surface.world_position, surface.world_normal);
let merge_result = merge_reservoirs(initial_reservoir, surface.world_position, surface.world_normal, surface.material.base_color / PI,
temporal.reservoir, temporal.world_position, temporal.world_normal, temporal.diffuse_brdf, &rng);
gi_reservoirs_b[pixel_index] = merge_result.merged_reservoir;
}
@compute @workgroup_size(8, 8, 1)
fn spatial_and_shade(@builtin(global_invocation_id) global_id: vec3<u32>) {
if any(global_id.xy >= vec2u(view.main_pass_viewport.zw)) { return; }
let pixel_index = global_id.x + global_id.y * u32(view.main_pass_viewport.z);
var rng = pixel_index + constants.frame_index;
let depth = textureLoad(depth_buffer, global_id.xy, 0);
if depth == 0.0 {
gi_reservoirs_a[pixel_index] = empty_reservoir();
return;
}
let surface = gpixel_resolve(textureLoad(gbuffer, global_id.xy, 0), depth, global_id.xy, view.main_pass_viewport.zw, view.world_from_clip);
if surface.material.metallic > 0.9999 && surface.material.roughness <= DIFFUSE_GI_REUSE_ROUGHNESS_THRESHOLD {
gi_reservoirs_a[pixel_index] = empty_reservoir();
return;
}
let input_reservoir = gi_reservoirs_b[pixel_index];
let spatial = load_spatial_reservoir(global_id.xy, depth, surface.world_position, surface.world_normal, &rng);
let merge_result = merge_reservoirs(input_reservoir, surface.world_position, surface.world_normal, surface.material.base_color / PI,
spatial.reservoir, spatial.world_position, spatial.world_normal, spatial.diffuse_brdf, &rng);
var combined_reservoir = merge_result.merged_reservoir;
// More accuracy, less stability
#ifndef BIASED_RESAMPLING
gi_reservoirs_a[pixel_index] = combined_reservoir;
#endif
combined_reservoir.unbiased_contribution_weight *= trace_point_visibility(surface.world_position + (surface.world_normal * RAY_T_MIN), combined_reservoir.sample_point_world_position);
// More stability, less accuracy (shadows extend further out than they should)
#ifdef BIASED_RESAMPLING
gi_reservoirs_a[pixel_index] = combined_reservoir;
#endif
let wo = normalize(view.world_position - surface.world_position);
let brdf = evaluate_diffuse_brdf(wo, merge_result.wi, surface.world_normal, surface.material);
var pixel_color = textureLoad(view_output, global_id.xy);
pixel_color += vec4(merge_result.selected_sample_radiance * combined_reservoir.unbiased_contribution_weight * view.exposure * brdf, 0.0);
textureStore(view_output, global_id.xy, pixel_color);
}
fn generate_initial_reservoir(world_position: vec3<f32>, world_normal: vec3<f32>, rng: ptr<function, u32>) -> Reservoir {
var reservoir = empty_reservoir();
let ray_direction = sample_uniform_hemisphere(world_normal, rng);
let ray = trace_ray(world_position + (world_normal * RAY_T_MIN), ray_direction, RAY_T_MIN, RAY_T_MAX, RAY_FLAG_NONE);
if ray.kind == RAY_QUERY_INTERSECTION_NONE {
return reservoir;
}
let sample_point = resolve_ray_hit_full(ray);
if any(sample_point.material.emissive != vec3(0.0)) {
return reservoir;
}
reservoir.sample_point_world_position = sample_point.world_position;
reservoir.sample_point_world_normal = sample_point.world_normal;
reservoir.confidence_weight = 1.0;
#ifdef NO_WORLD_CACHE
let direct_lighting = sample_random_light(sample_point.world_position, sample_point.world_normal, rng);
reservoir.radiance = direct_lighting.radiance * saturate(dot(direct_lighting.wi, sample_point.world_normal));
reservoir.unbiased_contribution_weight = direct_lighting.inverse_pdf * uniform_hemisphere_inverse_pdf();
#else
reservoir.radiance = query_world_cache(sample_point.world_position, sample_point.geometric_world_normal, view.world_position, ray.t, WORLD_CACHE_CELL_LIFETIME, rng);
reservoir.unbiased_contribution_weight = uniform_hemisphere_inverse_pdf();
#endif
let sample_point_diffuse_brdf = sample_point.material.base_color / PI;
reservoir.radiance *= sample_point_diffuse_brdf;
return reservoir;
}
fn load_temporal_reservoir(pixel_id: vec2<u32>, depth: f32, world_position: vec3<f32>, world_normal: vec3<f32>) -> NeighborInfo {
let motion_vector = textureLoad(motion_vectors, pixel_id, 0).xy;
let temporal_pixel_id_float = round(vec2<f32>(pixel_id) - (motion_vector * view.main_pass_viewport.zw));
var point_temporal_pixel_id = vec2<u32>(temporal_pixel_id_float);
if bool(constants.reset) {
return NeighborInfo(empty_reservoir(), vec3(0.0), vec3(0.0), vec3(0.0));
}
if any(temporal_pixel_id_float < vec2(0.0)) || any(temporal_pixel_id_float >= view.main_pass_viewport.zw) {
point_temporal_pixel_id = pixel_id;
}
let permuted_temporal_pixel_id = permute_pixel(point_temporal_pixel_id, constants.frame_index, view.main_pass_viewport.zw);
var temporal = load_temporal_reservoir_inner(permuted_temporal_pixel_id, depth, world_position, world_normal);
temporal.reservoir.confidence_weight = min(temporal.reservoir.confidence_weight, CONFIDENCE_WEIGHT_CAP);
return temporal;
}
fn load_temporal_reservoir_inner(temporal_pixel_id: vec2<u32>, depth: f32, world_position: vec3<f32>, world_normal: vec3<f32>) -> NeighborInfo {
// Check if the pixel features have changed heavily between the current and previous frame
let temporal_depth = textureLoad(previous_depth_buffer, temporal_pixel_id, 0);
let temporal_surface = gpixel_resolve(textureLoad(previous_gbuffer, temporal_pixel_id, 0), temporal_depth, temporal_pixel_id, view.main_pass_viewport.zw, previous_view.world_from_clip);
let temporal_diffuse_brdf = temporal_surface.material.base_color / PI;
if pixel_dissimilar(depth, world_position, temporal_surface.world_position, world_normal, temporal_surface.world_normal, view) {
return NeighborInfo(empty_reservoir(), vec3(0.0), vec3(0.0), vec3(0.0));
}
let temporal_pixel_index = temporal_pixel_id.x + temporal_pixel_id.y * u32(view.main_pass_viewport.z);
let temporal_reservoir = gi_reservoirs_a[temporal_pixel_index];
return NeighborInfo(temporal_reservoir, temporal_surface.world_position, temporal_surface.world_normal, temporal_diffuse_brdf);
}
fn load_spatial_reservoir(pixel_id: vec2<u32>, depth: f32, world_position: vec3<f32>, world_normal: vec3<f32>, rng: ptr<function, u32>) -> NeighborInfo {
for (var i = 0u; i < 5u; i++) {
let spatial_pixel_id = get_neighbor_pixel_id(pixel_id, SPATIAL_REUSE_RADIUS_PIXELS, rng);
let spatial_depth = textureLoad(depth_buffer, spatial_pixel_id, 0);
let spatial_surface = gpixel_resolve(textureLoad(gbuffer, spatial_pixel_id, 0), spatial_depth, spatial_pixel_id, view.main_pass_viewport.zw, view.world_from_clip);
let spatial_diffuse_brdf = spatial_surface.material.base_color / PI;
if pixel_dissimilar(depth, world_position, spatial_surface.world_position, world_normal, spatial_surface.world_normal, view) {
continue;
}
let spatial_pixel_index = spatial_pixel_id.x + spatial_pixel_id.y * u32(view.main_pass_viewport.z);
let spatial_reservoir = gi_reservoirs_b[spatial_pixel_index];
return NeighborInfo(spatial_reservoir, spatial_surface.world_position, spatial_surface.world_normal, spatial_diffuse_brdf);
}
return NeighborInfo(empty_reservoir(), world_position, world_normal, vec3(0.0));
}
fn get_neighbor_pixel_id(center_pixel_id: vec2<u32>, search_radius: f32, rng: ptr<function, u32>) -> vec2<u32> {
var spatial_id = vec2<f32>(center_pixel_id) + sample_disk(search_radius, rng);
spatial_id = clamp(spatial_id, vec2(0.0), view.main_pass_viewport.zw - 1.0);
return vec2<u32>(spatial_id);
}
struct NeighborInfo {
reservoir: Reservoir,
world_position: vec3<f32>,
world_normal: vec3<f32>,
diffuse_brdf: vec3<f32>,
}
fn jacobian(
new_world_position: vec3<f32>,
original_world_position: vec3<f32>,
sample_point_world_position: vec3<f32>,
sample_point_world_normal: vec3<f32>,
) -> f32 {
let r = new_world_position - sample_point_world_position;
let q = original_world_position - sample_point_world_position;
let rl = length(r);
let ql = length(q);
let phi_r = saturate(dot(r / rl, sample_point_world_normal));
let phi_q = saturate(dot(q / ql, sample_point_world_normal));
let jacobian = (phi_r * ql * ql) / (phi_q * rl * rl);
return select(jacobian, 0.0, isinf(jacobian) || isnan(jacobian));
}
fn isinf(x: f32) -> bool {
return (bitcast<u32>(x) & 0x7fffffffu) == 0x7f800000u;
}
fn empty_reservoir() -> Reservoir {
return Reservoir(
vec3(0.0),
0.0,
vec3(0.0),
0.0,
vec3(0.0),
0.0,
);
}
struct ReservoirMergeResult {
merged_reservoir: Reservoir,
selected_sample_radiance: vec3<f32>,
wi: vec3<f32>,
}
fn merge_reservoirs(
canonical_reservoir: Reservoir,
canonical_world_position: vec3<f32>,
canonical_world_normal: vec3<f32>,
canonical_diffuse_brdf: vec3<f32>,
other_reservoir: Reservoir,
other_world_position: vec3<f32>,
other_world_normal: vec3<f32>,
other_diffuse_brdf: vec3<f32>,
rng: ptr<function, u32>,
) -> ReservoirMergeResult {
// Radiances for resampling
let canonical_sample_wi = normalize(canonical_reservoir.sample_point_world_position - canonical_world_position);
let other_sample_wi = normalize(other_reservoir.sample_point_world_position - canonical_world_position);
// Target functions for resampling and MIS
let canonical_target_function_canonical_sample = luminance(
canonical_reservoir.radiance * saturate(dot(canonical_sample_wi, canonical_world_normal)) * canonical_diffuse_brdf
);
let canonical_target_function_other_sample = luminance(
other_reservoir.radiance * saturate(dot(other_sample_wi, canonical_world_normal)) * canonical_diffuse_brdf
);
// Extra target functions for MIS
let other_target_function_canonical_sample = luminance(
canonical_reservoir.radiance * saturate(dot(normalize(canonical_reservoir.sample_point_world_position - other_world_position), other_world_normal)) * other_diffuse_brdf
);
let other_target_function_other_sample = luminance(
other_reservoir.radiance * saturate(dot(normalize(other_reservoir.sample_point_world_position - other_world_position), other_world_normal)) * other_diffuse_brdf
);
// Jacobians for resampling and MIS
let canonical_target_function_other_sample_jacobian = jacobian(
canonical_world_position,
other_world_position,
other_reservoir.sample_point_world_position,
other_reservoir.sample_point_world_normal
);
let other_target_function_canonical_sample_jacobian = jacobian(
other_world_position,
canonical_world_position,
canonical_reservoir.sample_point_world_position,
canonical_reservoir.sample_point_world_normal
);
// Don't merge samples with huge jacobians, as it explodes the variance
if canonical_target_function_other_sample_jacobian > 1.2 || other_target_function_canonical_sample_jacobian > 1.2 {
return ReservoirMergeResult(canonical_reservoir, canonical_reservoir.radiance, canonical_sample_wi);
}
// Resampling weight for canonical sample
let canonical_sample_mis_weight = balance_heuristic(
canonical_reservoir.confidence_weight * canonical_target_function_canonical_sample,
other_reservoir.confidence_weight * other_target_function_canonical_sample * other_target_function_canonical_sample_jacobian,
);
let canonical_sample_resampling_weight = canonical_sample_mis_weight * canonical_target_function_canonical_sample * canonical_reservoir.unbiased_contribution_weight;
// Resampling weight for other sample
let other_sample_mis_weight = balance_heuristic(
other_reservoir.confidence_weight * other_target_function_other_sample,
canonical_reservoir.confidence_weight * canonical_target_function_other_sample * canonical_target_function_other_sample_jacobian,
);
let other_sample_resampling_weight = other_sample_mis_weight * canonical_target_function_other_sample * other_reservoir.unbiased_contribution_weight * canonical_target_function_other_sample_jacobian;
// Perform resampling
var combined_reservoir = empty_reservoir();
combined_reservoir.confidence_weight = canonical_reservoir.confidence_weight + other_reservoir.confidence_weight;
combined_reservoir.weight_sum = canonical_sample_resampling_weight + other_sample_resampling_weight;
if rand_f(rng) < other_sample_resampling_weight / combined_reservoir.weight_sum {
combined_reservoir.sample_point_world_position = other_reservoir.sample_point_world_position;
combined_reservoir.sample_point_world_normal = other_reservoir.sample_point_world_normal;
combined_reservoir.radiance = other_reservoir.radiance;
let inverse_target_function = select(0.0, 1.0 / canonical_target_function_other_sample, canonical_target_function_other_sample > 0.0);
combined_reservoir.unbiased_contribution_weight = combined_reservoir.weight_sum * inverse_target_function;
return ReservoirMergeResult(combined_reservoir, other_reservoir.radiance, other_sample_wi);
} else {
combined_reservoir.sample_point_world_position = canonical_reservoir.sample_point_world_position;
combined_reservoir.sample_point_world_normal = canonical_reservoir.sample_point_world_normal;
combined_reservoir.radiance = canonical_reservoir.radiance;
let inverse_target_function = select(0.0, 1.0 / canonical_target_function_canonical_sample, canonical_target_function_canonical_sample > 0.0);
combined_reservoir.unbiased_contribution_weight = combined_reservoir.weight_sum * inverse_target_function;
return ReservoirMergeResult(combined_reservoir, canonical_reservoir.radiance, canonical_sample_wi);
}
}
+242
View File
@@ -0,0 +1,242 @@
enable wgpu_ray_query;
#define_import_path bevy_solari::specular_gi
#import bevy_core_pipeline::tonemapping::tonemapping_luminance as luminance
#import bevy_pbr::pbr_functions::{calculate_diffuse_color, calculate_F0}
#import bevy_pbr::utils::rand_f
#import bevy_render::maths::{orthonormalize, PI}
#import bevy_render::view::View
#import bevy_solari::brdf::{evaluate_brdf, evaluate_specular_brdf}
#import bevy_solari::gbuffer_utils::{gpixel_resolve, ResolvedGPixel}
#import bevy_solari::sampling::{sample_random_light, random_emissive_light_pdf, sample_ggx_vndf, ggx_vndf_pdf, ggx_vndf_sample_invalid, power_heuristic}
#import bevy_solari::scene_bindings::{trace_ray, resolve_ray_hit_full, ResolvedRayHitFull, RAY_T_MIN, RAY_T_MAX, MIRROR_ROUGHNESS_THRESHOLD}
#import bevy_solari::world_cache::{query_world_cache, get_cell_size, WORLD_CACHE_CELL_LIFETIME}
#import bevy_solari::realtime_bindings::{view_output, gi_reservoirs_a, gbuffer, depth_buffer, view, constants}
#ifdef DLSS_RR_GUIDE_BUFFERS
#import bevy_solari::realtime_bindings::{diffuse_albedo, specular_albedo, normal_roughness, specular_motion_vectors, previous_view}
#import bevy_solari::resolve_dlss_rr_textures::env_brdf_approx2
#endif
const DIFFUSE_GI_REUSE_ROUGHNESS_THRESHOLD: f32 = 0.4;
const SPECULAR_GI_FOR_DI_ROUGHNESS_THRESHOLD: f32 = 0.0225;
@compute @workgroup_size(8, 8, 1)
fn specular_gi(@builtin(global_invocation_id) global_id: vec3<u32>) {
if any(global_id.xy >= vec2u(view.main_pass_viewport.zw)) { return; }
let pixel_index = global_id.x + global_id.y * u32(view.main_pass_viewport.z);
var rng = pixel_index + constants.frame_index;
let depth = textureLoad(depth_buffer, global_id.xy, 0);
if depth == 0.0 {
return;
}
let surface = gpixel_resolve(textureLoad(gbuffer, global_id.xy, 0), depth, global_id.xy, view.main_pass_viewport.zw, view.world_from_clip);
let wo_unnormalized = view.world_position - surface.world_position;
let wo_length = length(wo_unnormalized);
let wo = wo_unnormalized / wo_length;
var radiance: vec3<f32>;
var wi: vec3<f32>;
if surface.material.roughness > DIFFUSE_GI_REUSE_ROUGHNESS_THRESHOLD {
// Surface is very rough, reuse the ReSTIR GI reservoir
let gi_reservoir = gi_reservoirs_a[pixel_index];
wi = normalize(gi_reservoir.sample_point_world_position - surface.world_position);
radiance = gi_reservoir.radiance * gi_reservoir.unbiased_contribution_weight;
} else {
// Surface is glossy or mirror-like, trace a new path
let TBN = orthonormalize(surface.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 = sample_ggx_vndf(wo_tangent, surface.material.roughness, &rng);
if ggx_vndf_sample_invalid(wi_tangent) {
wi = vec3(0.0);
radiance = vec3(0.0);
} else {
wi = wi_tangent.x * T + wi_tangent.y * B + wi_tangent.z * N;
let pdf = ggx_vndf_pdf(wo_tangent, wi_tangent, surface.material.roughness);
radiance = trace_glossy_path(global_id.xy, surface, wo_length, wi, pdf, &rng);
if surface.material.roughness > MIRROR_ROUGHNESS_THRESHOLD {
radiance /= pdf;
}
}
}
let brdf = evaluate_specular_brdf(wo, wi, surface.world_normal, surface.material);
radiance *= brdf * view.exposure;
var pixel_color = textureLoad(view_output, global_id.xy);
pixel_color += vec4(radiance, 0.0);
textureStore(view_output, global_id.xy, pixel_color);
#ifdef VISUALIZE_WORLD_CACHE
textureStore(view_output, global_id.xy, vec4(query_world_cache(surface.world_position, surface.world_normal, view.world_position, RAY_T_MAX, WORLD_CACHE_CELL_LIFETIME, &rng) * view.exposure, 1.0));
#endif
}
fn trace_glossy_path(pixel_id: vec2<u32>, primary_surface: ResolvedGPixel, initial_ray_t: f32, initial_wi: vec3<f32>, initial_p_bounce: f32, rng: ptr<function, u32>) -> vec3<f32> {
var radiance = vec3(0.0);
var throughput = vec3(1.0);
var ray_origin = primary_surface.world_position + (primary_surface.world_normal * RAY_T_MIN);
var wi = initial_wi;
var p_bounce = initial_p_bounce;
var path_roughness = primary_surface.material.roughness;
#ifdef DLSS_RR_GUIDE_BUFFERS
var mirror_rotations = reflection_matrix(primary_surface.world_normal);
var psr_finished = false;
#endif
// Trace up to three bounces
for (var i = 0u; i < 3u; i += 1u) {
// Trace ray
let ray = trace_ray(ray_origin, wi, RAY_T_MIN, RAY_T_MAX, RAY_FLAG_NONE);
if ray.kind == RAY_QUERY_INTERSECTION_NONE { break; }
let ray_hit = resolve_ray_hit_full(ray);
let TBN = orthonormalize(ray_hit.world_normal);
let T = TBN[0];
let B = TBN[1];
let N = TBN[2];
let wo = -wi;
let wo_tangent = vec3(dot(wo, T), dot(wo, B), dot(wo, N));
// Add emissive contribution
let mis_weight = emissive_mis_weight(i, primary_surface.material.roughness, p_bounce, ray_hit);
radiance += throughput * mis_weight * ray_hit.material.emissive;
// Should not perform NEE for mirror-like surfaces
let surface_perfect_mirror = ray_hit.material.roughness <= MIRROR_ROUGHNESS_THRESHOLD && ray_hit.material.metallic > 0.9999;
// Primary surface replacement for perfect mirrors
// https://developer.nvidia.com/blog/rendering-perfect-reflections-and-refractions-in-path-traced-games/#primary_surface_replacement
#ifdef DLSS_RR_GUIDE_BUFFERS
if !psr_finished && primary_surface.material.roughness <= MIRROR_ROUGHNESS_THRESHOLD && primary_surface.material.metallic > 0.9999 {
if surface_perfect_mirror {
mirror_rotations = mirror_rotations * reflection_matrix(ray_hit.world_normal);
} else {
psr_finished = true;
replace_primary_surface(pixel_id, ray_hit, mirror_rotations, primary_surface.world_position);
}
}
#endif
// Terminate path in the world cache if the ray is long enough and the path spread is large enough
var rng_copy = *rng;
let world_cache_cell_size = get_cell_size(ray_hit.world_position, view.world_position, &rng_copy);
let ray_longer_than_cell = ray.t > sqrt(3.0) * world_cache_cell_size;
if i == 2u || (ray_longer_than_cell && path_roughness > DIFFUSE_GI_REUSE_ROUGHNESS_THRESHOLD) {
let diffuse_brdf = ray_hit.material.base_color / PI;
radiance += throughput * diffuse_brdf * query_world_cache(ray_hit.world_position, ray_hit.geometric_world_normal, view.world_position, ray.t, WORLD_CACHE_CELL_LIFETIME, rng);
break;
} else if !surface_perfect_mirror {
// Sample direct lighting (NEE)
let direct_lighting = sample_random_light(ray_hit.world_position, ray_hit.world_normal, rng);
let direct_lighting_brdf = evaluate_brdf(wo, direct_lighting.wi, ray_hit.world_normal, ray_hit.material);
let mis_weight = nee_mis_weight(direct_lighting.inverse_pdf, direct_lighting.brdf_rays_can_hit, wo_tangent, direct_lighting.wi, ray_hit, TBN);
radiance += throughput * mis_weight * direct_lighting.radiance * direct_lighting.inverse_pdf * direct_lighting_brdf;
}
// Sample new ray direction from the GGX BRDF for next bounce
let wi_tangent = sample_ggx_vndf(wo_tangent, ray_hit.material.roughness, rng);
if ggx_vndf_sample_invalid(wi_tangent) { break; }
wi = wi_tangent.x * T + wi_tangent.y * B + wi_tangent.z * N;
ray_origin = ray_hit.world_position + (ray_hit.geometric_world_normal * RAY_T_MIN);
// Update throughput for next bounce
p_bounce = ggx_vndf_pdf(wo_tangent, wi_tangent, ray_hit.material.roughness);
throughput *= evaluate_brdf(wo, wi, N, ray_hit.material);
if ray_hit.material.roughness > MIRROR_ROUGHNESS_THRESHOLD {
throughput /= p_bounce;
}
path_roughness += ray_hit.material.roughness;
// Russian roulette for early termination
let p = luminance(throughput);
if rand_f(rng) > p { break; }
throughput /= p;
}
return radiance;
}
fn emissive_mis_weight(i: u32, initial_roughness: f32, p_bounce: f32, ray_hit: ResolvedRayHitFull) -> f32 {
if i != 0u {
let p_light = random_emissive_light_pdf(ray_hit);
return power_heuristic(p_bounce, p_light);
} else {
// The first bounce gets MIS weight 0.0 or 1.0 depending on if ReSTIR DI shaded using the specular lobe or not
if initial_roughness <= SPECULAR_GI_FOR_DI_ROUGHNESS_THRESHOLD {
return 1.0;
} else {
return 0.0;
}
}
}
fn nee_mis_weight(inverse_p_light: f32, brdf_rays_can_hit: bool, wo_tangent: vec3<f32>, wi: vec3<f32>, ray_hit: ResolvedRayHitFull, TBN: mat3x3<f32>) -> f32 {
if !brdf_rays_can_hit {
return 1.0;
}
let T = TBN[0];
let B = TBN[1];
let N = TBN[2];
let wi_tangent = vec3(dot(wi, T), dot(wi, B), dot(wi, N));
let p_light = 1.0 / inverse_p_light;
let p_bounce = ggx_vndf_pdf(wo_tangent, wi_tangent, ray_hit.material.roughness);
return power_heuristic(p_light, p_bounce);
}
#ifdef DLSS_RR_GUIDE_BUFFERS
// https://en.wikipedia.org/wiki/Householder_transformation
fn reflection_matrix(plane_normal: vec3f) -> mat3x3<f32> {
// N times Nᵀ.
let n_nt = mat3x3<f32>(
plane_normal * plane_normal.x,
plane_normal * plane_normal.y,
plane_normal * plane_normal.z,
);
let identity_matrix = mat3x3<f32>(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0);
return identity_matrix - n_nt * 2.0;
}
fn replace_primary_surface(pixel_id: vec2<u32>, ray_hit: ResolvedRayHitFull, mirror_rotations: mat3x3<f32>, primary_surface_world_position: vec3<f32>) {
// Simplification: Apply all rotations in the chain around the first mirror, rather than applying each rotation around its respective mirror
let virtual_position = (mirror_rotations * (ray_hit.world_position - primary_surface_world_position)) + primary_surface_world_position;
let virtual_previous_frame_position = (mirror_rotations * (ray_hit.previous_frame_world_position - primary_surface_world_position)) + primary_surface_world_position;
let specular_motion_vector = calculate_motion_vector(virtual_position, virtual_previous_frame_position);
let F0 = calculate_F0(ray_hit.material.base_color, ray_hit.material.metallic, vec3(ray_hit.material.reflectance));
let wo = normalize(view.world_position - virtual_position);
let virtual_normal = normalize(mirror_rotations * ray_hit.world_normal);
textureStore(specular_motion_vectors, pixel_id, vec4(specular_motion_vector, vec2(0.0)));
textureStore(diffuse_albedo, pixel_id, vec4(calculate_diffuse_color(ray_hit.material.base_color, ray_hit.material.metallic, 0.0, 0.0), 0.0));
textureStore(specular_albedo, pixel_id, vec4(env_brdf_approx2(F0, ray_hit.material.roughness, virtual_normal, wo), 0.0));
textureStore(normal_roughness, pixel_id, vec4(virtual_normal, ray_hit.material.perceptual_roughness));
}
fn calculate_motion_vector(world_position: vec3<f32>, previous_world_position: vec3<f32>) -> vec2<f32> {
let clip_position_t = view.unjittered_clip_from_world * vec4(world_position, 1.0);
let clip_position = clip_position_t.xy / clip_position_t.w;
let previous_clip_position_t = previous_view.clip_from_world * vec4(previous_world_position, 1.0);
let previous_clip_position = previous_clip_position_t.xy / previous_clip_position_t.w;
// These motion vectors are used as offsets to UV positions and are stored
// in the range -1,1 to allow offsetting from the one corner to the
// diagonally-opposite corner in UV coordinates, in either direction.
// A difference between diagonally-opposite corners of clip space is in the
// range -2,2, so this needs to be scaled by 0.5. And the V direction goes
// down where clip space y goes up, so y needs to be flipped.
return (clip_position - previous_clip_position) * vec2(0.5, -0.5);
}
#endif
@@ -0,0 +1,86 @@
enable wgpu_ray_query;
#import bevy_solari::world_cache::WORLD_CACHE_EMPTY_CELL
#import bevy_solari::realtime_bindings::{
world_cache_life,
world_cache_checksums,
world_cache_radiance,
world_cache_luminance_deltas,
world_cache_a,
world_cache_b,
world_cache_active_cell_indices,
world_cache_active_cells_count,
}
@group(2) @binding(0) var<storage, read_write> world_cache_active_cells_dispatch: vec3<u32>;
var<workgroup> w1: array<u32, 1024u>;
var<workgroup> w2: array<u32, 1024u>;
@compute @workgroup_size(1024, 1, 1)
fn decay_world_cache(@builtin(global_invocation_id) global_id: vec3<u32>) {
var life = world_cache_life[global_id.x];
if life > 0u {
life -= 1u;
world_cache_life[global_id.x] = life;
if life == 0u {
world_cache_checksums[global_id.x] = WORLD_CACHE_EMPTY_CELL;
world_cache_radiance[global_id.x] = vec4(0.0);
world_cache_luminance_deltas[global_id.x] = 0.0;
}
}
}
@compute @workgroup_size(1024, 1, 1)
fn compact_world_cache_single_block(
@builtin(global_invocation_id) cell_id: vec3<u32>,
@builtin(local_invocation_index) t: u32,
) {
if t == 0u { w1[0u] = 0u; } else { w1[t] = u32(world_cache_life[cell_id.x - 1u] != 0u); }; workgroupBarrier();
if t < 1u { w2[t] = w1[t]; } else { w2[t] = w1[t] + w1[t - 1u]; } workgroupBarrier();
if t < 2u { w1[t] = w2[t]; } else { w1[t] = w2[t] + w2[t - 2u]; } workgroupBarrier();
if t < 4u { w2[t] = w1[t]; } else { w2[t] = w1[t] + w1[t - 4u]; } workgroupBarrier();
if t < 8u { w1[t] = w2[t]; } else { w1[t] = w2[t] + w2[t - 8u]; } workgroupBarrier();
if t < 16u { w2[t] = w1[t]; } else { w2[t] = w1[t] + w1[t - 16u]; } workgroupBarrier();
if t < 32u { w1[t] = w2[t]; } else { w1[t] = w2[t] + w2[t - 32u]; } workgroupBarrier();
if t < 64u { w2[t] = w1[t]; } else { w2[t] = w1[t] + w1[t - 64u]; } workgroupBarrier();
if t < 128u { w1[t] = w2[t]; } else { w1[t] = w2[t] + w2[t - 128u]; } workgroupBarrier();
if t < 256u { w2[t] = w1[t]; } else { w2[t] = w1[t] + w1[t - 256u]; } workgroupBarrier();
if t < 512u { world_cache_a[cell_id.x] = w2[t]; } else { world_cache_a[cell_id.x] = w2[t] + w2[t - 512u]; }
}
@compute @workgroup_size(1024, 1, 1)
fn compact_world_cache_blocks(@builtin(local_invocation_index) t: u32) {
if t == 0u { w1[0u] = 0u; } else { w1[t] = world_cache_a[t * 1024u - 1u]; }; workgroupBarrier();
if t < 1u { w2[t] = w1[t]; } else { w2[t] = w1[t] + w1[t - 1u]; } workgroupBarrier();
if t < 2u { w1[t] = w2[t]; } else { w1[t] = w2[t] + w2[t - 2u]; } workgroupBarrier();
if t < 4u { w2[t] = w1[t]; } else { w2[t] = w1[t] + w1[t - 4u]; } workgroupBarrier();
if t < 8u { w1[t] = w2[t]; } else { w1[t] = w2[t] + w2[t - 8u]; } workgroupBarrier();
if t < 16u { w2[t] = w1[t]; } else { w2[t] = w1[t] + w1[t - 16u]; } workgroupBarrier();
if t < 32u { w1[t] = w2[t]; } else { w1[t] = w2[t] + w2[t - 32u]; } workgroupBarrier();
if t < 64u { w2[t] = w1[t]; } else { w2[t] = w1[t] + w1[t - 64u]; } workgroupBarrier();
if t < 128u { w1[t] = w2[t]; } else { w1[t] = w2[t] + w2[t - 128u]; } workgroupBarrier();
if t < 256u { w2[t] = w1[t]; } else { w2[t] = w1[t] + w1[t - 256u]; } workgroupBarrier();
if t < 512u { world_cache_b[t] = w2[t]; } else { world_cache_b[t] = w2[t] + w2[t - 512u]; }
}
@compute @workgroup_size(1024, 1, 1)
fn compact_world_cache_write_active_cells(
@builtin(global_invocation_id) cell_id: vec3<u32>,
@builtin(workgroup_id) workgroup_id: vec3<u32>,
@builtin(local_invocation_index) thread_index: u32,
) {
let compacted_index = world_cache_a[cell_id.x] + world_cache_b[workgroup_id.x];
let cell_active = world_cache_life[cell_id.x] != 0u;
if cell_active {
world_cache_active_cell_indices[compacted_index] = cell_id.x;
}
if thread_index == 1023u && workgroup_id.x == 1023u {
let active_cell_count = compacted_index + u32(cell_active);
world_cache_active_cells_count = active_cell_count;
world_cache_active_cells_dispatch = vec3((active_cell_count + 63u) / 64u, 1u, 1u);
}
}
@@ -0,0 +1,148 @@
enable wgpu_ray_query;
#define_import_path bevy_solari::world_cache
#import bevy_pbr::utils::{rand_f, rand_vec2f}
#import bevy_render::maths::orthonormalize
#import bevy_solari::realtime_bindings::{
world_cache_life,
world_cache_checksums,
world_cache_radiance,
world_cache_geometry_data,
world_cache_luminance_deltas,
world_cache_a,
world_cache_b,
world_cache_active_cell_indices,
world_cache_active_cells_count,
WorldCacheGeometryData,
}
/// How responsive the world cache is to changes in lighting (higher is less responsive but more stable, lower is more responsive but less stable)
const WORLD_CACHE_MAX_TEMPORAL_SAMPLES: f32 = 32.0;
/// How many direct light samples each cell takes when updating each frame
const WORLD_CACHE_DIRECT_LIGHT_SAMPLE_COUNT: u32 = 32u;
/// Maximum amount of distance to trace GI rays between two cache cells
const WORLD_CACHE_MAX_GI_RAY_DISTANCE: f32 = 50.0;
/// Soft upper limit on the amount of cache cells to update each frame
const WORLD_CACHE_CELL_UPDATES_SOFT_CAP: u32 = 40000u;
/// Maximum amount of frames a cell can live for without being queried
const WORLD_CACHE_CELL_LIFETIME: u32 = 10u;
/// Maximum amount of attempts to find a cache entry after a hash collision
const WORLD_CACHE_MAX_SEARCH_STEPS: u32 = 3u;
/// Size of a cache cell at the lowest LOD in meters
const WORLD_CACHE_POSITION_BASE_CELL_SIZE: f32 = 0.15;
/// How fast the world cache transitions between LODs as a function of distance to the camera
const WORLD_CACHE_POSITION_LOD_SCALE: f32 = 15.0;
/// Marker value for an empty cell
const WORLD_CACHE_EMPTY_CELL: u32 = 0u;
#ifndef WORLD_CACHE_NON_ATOMIC_LIFE_BUFFER
fn query_world_cache(world_position_in: vec3<f32>, world_normal: vec3<f32>, view_position: vec3<f32>, ray_t: f32, cell_lifetime: u32, rng: ptr<function, u32>) -> vec3<f32> {
var world_position = world_position_in;
var cell_size = get_cell_size(world_position, view_position, rng);
#ifdef WORLD_CACHE_FIRST_BOUNCE_LIGHT_LEAK_PREVENTION
if ray_t < cell_size {
// Prevent light leaks
cell_size = WORLD_CACHE_POSITION_BASE_CELL_SIZE;
}
#endif
#ifndef NO_JITTER_WORLD_CACHE
// Jitter query point, which essentially blurs the cache a bit so it's not so grid-like
// https://tomclabault.github.io/blog/2025/regir, jitter_world_position_tangent_plane
let TBN = orthonormalize(world_normal);
let offset = (rand_vec2f(rng) * 2.0 - 1.0) * cell_size * 0.5;
world_position += offset.x * TBN[0] + offset.y * TBN[1];
cell_size = get_cell_size(world_position, view_position, rng);
#endif
let world_position_quantized = bitcast<vec3<u32>>(quantize_position(world_position, cell_size));
let world_normal_quantized = bitcast<vec3<u32>>(quantize_normal(world_normal));
var key = compute_key(world_position_quantized, world_normal_quantized);
let checksum = compute_checksum(world_position_quantized, world_normal_quantized);
for (var i = 0u; i < WORLD_CACHE_MAX_SEARCH_STEPS; i++) {
let existing_checksum = atomicCompareExchangeWeak(&world_cache_checksums[key], WORLD_CACHE_EMPTY_CELL, checksum).old_value;
// Cell already exists or is empty - reset lifetime
if existing_checksum == checksum || existing_checksum == WORLD_CACHE_EMPTY_CELL {
#ifndef WORLD_CACHE_QUERY_ATOMIC_MAX_LIFETIME
atomicStore(&world_cache_life[key], cell_lifetime);
#else
atomicMax(&world_cache_life[key], cell_lifetime);
#endif
}
if existing_checksum == checksum {
// Cache entry already exists - get radiance
return world_cache_radiance[key].rgb;
} else if existing_checksum == WORLD_CACHE_EMPTY_CELL {
// Cell is empty - initialize it
world_cache_geometry_data[key].world_position = world_position;
world_cache_geometry_data[key].world_normal = world_normal;
return vec3(0.0);
} else {
// Collision - linear probe to next entry
key += 1u;
}
}
return vec3(0.0);
}
#endif
fn get_cell_size(world_position: vec3<f32>, view_position: vec3<f32>, rng: ptr<function, u32>) -> f32 {
let camera_distance = distance(view_position, world_position) / WORLD_CACHE_POSITION_LOD_SCALE;
let lod_f = log2(1.0 + camera_distance);
let lod_fract = fract(lod_f);
let lod = floor(lod_f) + select(0.0, 1.0, rand_f(rng) < lod_fract * lod_fract * lod_fract);
return WORLD_CACHE_POSITION_BASE_CELL_SIZE * exp2(lod);
}
fn quantize_position(world_position: vec3<f32>, quantization_factor: f32) -> vec3<f32> {
return floor(world_position / quantization_factor + 0.0001);
}
fn quantize_normal(world_normal: vec3<f32>) -> vec3<f32> {
return floor(world_normal * 2.0 + 0.0001);
}
// TODO: Clustering
fn compute_key(world_position: vec3<u32>, world_normal: vec3<u32>) -> u32 {
var key = pcg_hash(world_position.x);
key = pcg_hash(key + world_position.y);
key = pcg_hash(key + world_position.z);
key = pcg_hash(key + world_normal.x);
key = pcg_hash(key + world_normal.y);
key = pcg_hash(key + world_normal.z);
return wrap_key(key);
}
fn compute_checksum(world_position: vec3<u32>, world_normal: vec3<u32>) -> u32 {
var key = iqint_hash(world_position.x);
key = iqint_hash(key + world_position.y);
key = iqint_hash(key + world_position.z);
key = iqint_hash(key + world_normal.x);
key = iqint_hash(key + world_normal.y);
key = iqint_hash(key + world_normal.z);
return max(key, 1u); // 0u is reserved for WORLD_CACHE_EMPTY_CELL
}
fn pcg_hash(input: u32) -> u32 {
let state = input * 747796405u + 2891336453u;
let word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;
return (word >> 22u) ^ word;
}
fn iqint_hash(input: u32) -> u32 {
let n = (input << 13u) ^ input;
return n * (n * n * 15731u + 789221u) + 1376312589u;
}
fn wrap_key(key: u32) -> u32 {
return key & (#{WORLD_CACHE_SIZE} - 1u);
}
@@ -0,0 +1,129 @@
enable wgpu_ray_query;
#import bevy_core_pipeline::tonemapping::tonemapping_luminance as luminance
#import bevy_pbr::utils::{rand_f, rand_range_u, sample_cosine_hemisphere}
#import bevy_render::view::View
#import bevy_solari::presample_light_tiles::{ResolvedLightSamplePacked, unpack_resolved_light_sample}
#import bevy_solari::sampling::{calculate_resolved_light_contribution, trace_light_visibility}
#import bevy_solari::scene_bindings::{trace_ray, resolve_ray_hit_full, RAY_T_MIN}
#import bevy_solari::world_cache::{
WORLD_CACHE_MAX_TEMPORAL_SAMPLES,
WORLD_CACHE_DIRECT_LIGHT_SAMPLE_COUNT,
WORLD_CACHE_MAX_GI_RAY_DISTANCE,
WORLD_CACHE_CELL_UPDATES_SOFT_CAP,
query_world_cache,
}
#import bevy_solari::realtime_bindings::{
light_tile_resolved_samples,
view,
constants,
world_cache_active_cells_count,
world_cache_active_cell_indices,
world_cache_life,
world_cache_geometry_data,
world_cache_radiance,
world_cache_luminance_deltas,
world_cache_active_cells_new_radiance,
}
@compute @workgroup_size(64, 1, 1)
fn sample_di(@builtin(workgroup_id) workgroup_id: vec3<u32>, @builtin(global_invocation_id) active_cell_id: vec3<u32>) {
if active_cell_id.x >= world_cache_active_cells_count { return; }
let cell_index = world_cache_active_cell_indices[active_cell_id.x];
let geometry_data = world_cache_geometry_data[cell_index];
var rng = cell_index + constants.frame_index;
if rand_f(&rng) >= f32(WORLD_CACHE_CELL_UPDATES_SOFT_CAP) / f32(world_cache_active_cells_count) { return; }
let new_radiance = sample_random_light_ris(geometry_data.world_position, geometry_data.world_normal, workgroup_id.xy, &rng);
world_cache_active_cells_new_radiance[active_cell_id.x] = new_radiance;
}
@compute @workgroup_size(64, 1, 1)
fn sample_gi(@builtin(workgroup_id) workgroup_id: vec3<u32>, @builtin(global_invocation_id) active_cell_id: vec3<u32>) {
if active_cell_id.x >= world_cache_active_cells_count { return; }
let cell_index = world_cache_active_cell_indices[active_cell_id.x];
let geometry_data = world_cache_geometry_data[cell_index];
var rng = cell_index + constants.frame_index;
if rand_f(&rng) >= f32(WORLD_CACHE_CELL_UPDATES_SOFT_CAP) / f32(world_cache_active_cells_count) { return; }
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, WORLD_CACHE_MAX_GI_RAY_DISTANCE, RAY_FLAG_NONE);
if ray.kind != RAY_QUERY_INTERSECTION_NONE {
let ray_hit = resolve_ray_hit_full(ray);
let cell_life = atomicLoad(&world_cache_life[cell_index]);
let radiance = query_world_cache(ray_hit.world_position, ray_hit.geometric_world_normal, view.world_position, ray.t, cell_life, &rng);
world_cache_active_cells_new_radiance[active_cell_id.x] += ray_hit.material.base_color * radiance;
}
}
@compute @workgroup_size(64, 1, 1)
fn blend_new_samples(@builtin(global_invocation_id) active_cell_id: vec3<u32>) {
if active_cell_id.x >= world_cache_active_cells_count { return; }
let cell_index = world_cache_active_cell_indices[active_cell_id.x];
var rng = cell_index + constants.frame_index;
if rand_f(&rng) >= f32(WORLD_CACHE_CELL_UPDATES_SOFT_CAP) / f32(world_cache_active_cells_count) { return; }
let old_radiance = world_cache_radiance[cell_index];
let new_radiance = world_cache_active_cells_new_radiance[active_cell_id.x];
let luminance_delta = world_cache_luminance_deltas[cell_index];
// https://bsky.app/profile/gboisse.bsky.social/post/3m5blga3ftk2a
let sample_count = min(old_radiance.a + 1.0, WORLD_CACHE_MAX_TEMPORAL_SAMPLES);
let alpha = abs(luminance_delta) / max(luminance(old_radiance.rgb), 0.001);
let max_sample_count = mix(WORLD_CACHE_MAX_TEMPORAL_SAMPLES, 1.0, pow(saturate(alpha), 1.0 / 8.0));
var blend_amount = 1.0 / min(sample_count, max_sample_count);
if bool(constants.reset) {
blend_amount = 1.0;
}
let blended_radiance = mix(old_radiance.rgb, new_radiance, blend_amount);
let blended_luminance_delta = select(mix(luminance_delta, luminance(blended_radiance) - luminance(old_radiance.rgb), 1.0 / 8.0), 0.0, bool(constants.reset));
world_cache_radiance[cell_index] = vec4(blended_radiance, sample_count);
world_cache_luminance_deltas[cell_index] = blended_luminance_delta;
}
fn sample_random_light_ris(world_position: vec3<f32>, world_normal: vec3<f32>, workgroup_id: vec2<u32>, rng: ptr<function, u32>) -> vec3<f32> {
var workgroup_rng = (workgroup_id.x * 5782582u) + workgroup_id.y;
let light_tile_start = rand_range_u(128u, &workgroup_rng) * 1024u;
var weight_sum = 0.0;
var selected_sample_radiance = vec3(0.0);
var selected_sample_target_function = 0.0;
var selected_sample_world_position = vec4(0.0);
let mis_weight = 1.0 / f32(WORLD_CACHE_DIRECT_LIGHT_SAMPLE_COUNT);
for (var i = 0u; i < WORLD_CACHE_DIRECT_LIGHT_SAMPLE_COUNT; i++) {
let tile_sample = light_tile_start + rand_range_u(1024u, rng);
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));
let target_function = luminance(contribution);
let resampling_weight = mis_weight * (target_function * light_contribution.inverse_pdf);
weight_sum += resampling_weight;
if rand_f(rng) < resampling_weight / weight_sum {
selected_sample_radiance = contribution;
selected_sample_target_function = target_function;
selected_sample_world_position = resolved_light_sample.world_position;
}
}
var unbiased_contribution_weight = 0.0;
if all(selected_sample_radiance != vec3(0.0)) {
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_light_visibility(world_position + (world_normal * RAY_T_MIN), selected_sample_world_position);
}
return selected_sample_radiance * unbiased_contribution_weight;
}
+589
View File
@@ -0,0 +1,589 @@
use super::{
RaytracingMesh3d,
blas::BlasManager,
extract::{RaytracingMaterial3d, StandardMaterialAssets, SurfaceMaterialAssets},
};
use bevy_asset::{AssetId, Handle};
use bevy_color::{ColorToComponents, LinearRgba};
use bevy_ecs::{
entity::{Entity, EntityHashMap},
resource::Resource,
system::{Query, Res, ResMut},
};
use bevy_material::AlphaMode;
use bevy_math::{Mat4, UVec4, Vec3, Vec4, ops::cos};
use bevy_pbr::{DfgLut, ExtractedDirectionalLight, PreviousGlobalTransform, StandardMaterial};
use bevy_platform::{collections::HashMap, hash::FixedHasher};
use bevy_render::{
mesh::allocator::MeshAllocator,
render_asset::RenderAssets,
render_resource::{binding_types::*, *},
renderer::{RenderDevice, RenderQueue},
texture::{FallbackImage, GpuImage},
};
use bevy_transform::components::GlobalTransform;
use blacksite_surface::{SurfaceEvaluatorRegistry, SurfaceUniform};
use core::{f32::consts::TAU, hash::Hash, num::NonZeroU32, ops::Deref};
const MAX_MESH_SLAB_COUNT: NonZeroU32 = NonZeroU32::new(500).unwrap();
const MAX_TEXTURE_COUNT: NonZeroU32 = NonZeroU32::new(5_000).unwrap();
const TEXTURE_MAP_NONE: u32 = u32::MAX;
const LIGHT_NOT_PRESENT_THIS_FRAME: u32 = u32::MAX;
const MATERIAL_FLAG_ALPHA_CUTOUT: u32 = 1 << 0;
const MATERIAL_FLAG_SURFACE: u32 = 1 << 1;
#[derive(Resource)]
pub struct RaytracingSceneBindings {
pub bind_group: Option<BindGroup>,
pub bind_group_layout: BindGroupLayoutDescriptor,
previous_frame_light_entities: Vec<Entity>,
}
pub fn prepare_raytracing_scene_bindings(
instances_query: Query<(
Entity,
&RaytracingMesh3d,
&RaytracingMaterial3d,
&GlobalTransform,
Option<&PreviousGlobalTransform>,
)>,
directional_lights_query: Query<(Entity, &ExtractedDirectionalLight)>,
mesh_allocator: Res<MeshAllocator>,
blas_manager: Res<BlasManager>,
material_assets: Res<StandardMaterialAssets>,
surface_material_assets: Res<SurfaceMaterialAssets>,
surface_evaluators: Res<SurfaceEvaluatorRegistry>,
texture_assets: Res<RenderAssets<GpuImage>>,
fallback_texture: Res<FallbackImage>,
dfg_lut: Res<DfgLut>,
render_device: Res<RenderDevice>,
pipeline_cache: Res<PipelineCache>,
render_queue: Res<RenderQueue>,
mut raytracing_scene_bindings: ResMut<RaytracingSceneBindings>,
) {
raytracing_scene_bindings.bind_group = None;
let mut this_frame_entity_to_light_id = EntityHashMap::<u32>::default();
let previous_frame_light_entities: Vec<_> = raytracing_scene_bindings
.previous_frame_light_entities
.drain(..)
.collect();
if instances_query.iter().len() == 0 {
return;
}
let mut vertex_buffers = CachedBindingArray::new();
let mut index_buffers = CachedBindingArray::new();
let mut textures = CachedBindingArray::new();
let mut samplers = Vec::new();
let mut materials = StorageBufferList::<GpuMaterial>::default();
let mut tlas = render_device
.wgpu_device()
.create_tlas(&CreateTlasDescriptor {
label: Some("tlas"),
flags: AccelerationStructureFlags::PREFER_FAST_TRACE,
update_mode: AccelerationStructureUpdateMode::Build,
max_instances: instances_query.iter().len() as u32,
});
let mut transforms = StorageBufferList::<Mat4>::default();
let mut previous_frame_transforms = StorageBufferList::<Mat4>::default();
let mut geometry_ids = StorageBufferList::<GpuInstanceGeometryIds>::default();
let mut material_ids = StorageBufferList::<u32>::default();
let mut light_sources = StorageBufferList::<GpuLightSource>::default();
let mut directional_lights = StorageBufferList::<GpuDirectionalLight>::default();
let mut previous_frame_light_id_translations = StorageBufferList::<u32>::default();
let mut material_id_map: HashMap<RaytracingMaterialId, u32, FixedHasher> = HashMap::default();
let mut material_id = 0;
let mut process_texture = |texture_handle: &Option<Handle<_>>| -> Option<u32> {
match texture_handle {
Some(texture_handle) => match texture_assets.get(texture_handle.id()) {
Some(texture) => {
let (texture_id, is_new) =
textures.push_if_absent(texture.texture_view.deref(), texture_handle.id());
if is_new {
samplers.push(texture.sampler.deref());
}
Some(texture_id)
}
None => None,
},
None => Some(TEXTURE_MAP_NONE),
}
};
for (asset_id, material) in material_assets.iter() {
let Some((material_flags, alpha_cutoff)) = material_alpha_state(&material.alpha_mode)
else {
continue;
};
let Some(base_color_texture_id) = process_texture(&material.base_color_texture) else {
continue;
};
let Some(normal_map_texture_id) = process_texture(&material.normal_map_texture) else {
continue;
};
let Some(emissive_texture_id) = process_texture(&material.emissive_texture) else {
continue;
};
let Some(metallic_roughness_texture_id) =
process_texture(&material.metallic_roughness_texture)
else {
continue;
};
materials.get_mut().push(GpuMaterial::new(
UVec4::new(
normal_map_texture_id,
base_color_texture_id,
emissive_texture_id,
metallic_roughness_texture_id,
),
[TEXTURE_MAP_NONE; 8],
material_flags,
alpha_cutoff,
material,
SurfaceUniform::default(),
));
material_id_map.insert(RaytracingMaterialId::Standard(*asset_id), material_id);
material_id += 1;
}
for (asset_id, material) in surface_material_assets.iter() {
let Some(evaluator) = surface_evaluators
.evaluators
.get(&material.extension.uniform.shader_id)
else {
continue;
};
if evaluator.shader_id != material.extension.uniform.shader_id {
continue;
}
let Some((alpha_flags, alpha_cutoff)) = material_alpha_state(&material.base.alpha_mode)
else {
continue;
};
let Some(base_color_texture_id) = process_texture(&material.base.base_color_texture) else {
continue;
};
let Some(normal_map_texture_id) = process_texture(&material.base.normal_map_texture) else {
continue;
};
let Some(emissive_texture_id) = process_texture(&material.base.emissive_texture) else {
continue;
};
let Some(metallic_roughness_texture_id) =
process_texture(&material.base.metallic_roughness_texture)
else {
continue;
};
let surface_texture_handles = [
&material.extension.texture0,
&material.extension.texture1,
&material.extension.texture2,
&material.extension.texture3,
&material.extension.texture4,
&material.extension.texture5,
&material.extension.texture6,
&material.extension.texture7,
];
let mut surface_texture_ids = [TEXTURE_MAP_NONE; 8];
let mut textures_ready = true;
for (texture_id, handle) in surface_texture_ids.iter_mut().zip(surface_texture_handles) {
let Some(id) = process_texture(handle) else {
textures_ready = false;
break;
};
*texture_id = id;
}
if !textures_ready {
continue;
}
materials.get_mut().push(GpuMaterial::new(
UVec4::new(
normal_map_texture_id,
base_color_texture_id,
emissive_texture_id,
metallic_roughness_texture_id,
),
surface_texture_ids,
alpha_flags | MATERIAL_FLAG_SURFACE,
alpha_cutoff,
&material.base,
material.extension.uniform,
));
material_id_map.insert(RaytracingMaterialId::Surface(*asset_id), material_id);
material_id += 1;
}
if material_id == 0 {
return;
}
if textures.is_empty() {
textures.vec.push(fallback_texture.d2.texture_view.deref());
samplers.push(fallback_texture.d2.sampler.deref());
}
let mut instance_id = 0;
for (entity, mesh, material, transform, previous_frame_transform) in &instances_query {
let Some(blas) = blas_manager.get(&mesh.id()) else {
continue;
};
let Some(vertex_slice) = mesh_allocator.mesh_vertex_slice(&mesh.id()) else {
continue;
};
let Some(index_slice) = mesh_allocator.mesh_index_slice(&mesh.id()) else {
continue;
};
let Some(material_id) = material_id_map
.get(&RaytracingMaterialId::from(material))
.copied()
else {
continue;
};
let Some(material) = materials.get().get(material_id as usize) else {
continue;
};
let transform = transform.to_matrix();
*tlas.get_mut_single(instance_id).unwrap() = Some(TlasInstance::new(
blas,
tlas_transform(&transform),
Default::default(),
0xFF,
));
transforms.get_mut().push(transform);
previous_frame_transforms.get_mut().push(
previous_frame_transform
.map(|t| Mat4::from(t.0))
.unwrap_or(transform),
);
let (vertex_buffer_id, _) = vertex_buffers.push_if_absent(
vertex_slice.buffer.as_entire_buffer_binding(),
vertex_slice.buffer.id(),
);
let (index_buffer_id, _) = index_buffers.push_if_absent(
index_slice.buffer.as_entire_buffer_binding(),
index_slice.buffer.id(),
);
geometry_ids.get_mut().push(GpuInstanceGeometryIds {
vertex_buffer_id,
vertex_buffer_offset: vertex_slice.range.start,
index_buffer_id,
index_buffer_offset: index_slice.range.start,
triangle_count: (index_slice.range.len() / 3) as u32,
});
material_ids.get_mut().push(material_id);
if material.is_potentially_emissive() {
light_sources
.get_mut()
.push(GpuLightSource::new_emissive_mesh_light(
instance_id as u32,
(index_slice.range.len() / 3) as u32,
));
this_frame_entity_to_light_id.insert(entity, light_sources.get().len() as u32 - 1);
raytracing_scene_bindings
.previous_frame_light_entities
.push(entity);
}
instance_id += 1;
}
if instance_id == 0 {
return;
}
for (entity, directional_light) in &directional_lights_query {
let directional_lights = directional_lights.get_mut();
let directional_light_id = directional_lights.len() as u32;
directional_lights.push(GpuDirectionalLight::new(directional_light));
light_sources
.get_mut()
.push(GpuLightSource::new_directional_light(directional_light_id));
this_frame_entity_to_light_id.insert(entity, light_sources.get().len() as u32 - 1);
raytracing_scene_bindings
.previous_frame_light_entities
.push(entity);
}
for previous_frame_light_entity in previous_frame_light_entities {
let current_frame_index = this_frame_entity_to_light_id
.get(&previous_frame_light_entity)
.copied()
.unwrap_or(LIGHT_NOT_PRESENT_THIS_FRAME);
previous_frame_light_id_translations
.get_mut()
.push(current_frame_index);
}
if light_sources.get().len() > u16::MAX as usize {
panic!("Too many light sources in the scene, maximum is 65535.");
}
materials.write_buffer(&render_device, &render_queue);
transforms.write_buffer(&render_device, &render_queue);
previous_frame_transforms.write_buffer(&render_device, &render_queue);
geometry_ids.write_buffer(&render_device, &render_queue);
material_ids.write_buffer(&render_device, &render_queue);
light_sources.write_buffer(&render_device, &render_queue);
directional_lights.write_buffer(&render_device, &render_queue);
previous_frame_light_id_translations.write_buffer(&render_device, &render_queue);
let mut command_encoder = render_device.create_command_encoder(&CommandEncoderDescriptor {
label: Some("build_tlas_command_encoder"),
});
command_encoder.build_acceleration_structures(&[], [&tlas]);
render_queue.submit([command_encoder.finish()]);
let (dfg_view, dfg_sampler) = texture_assets
.get(&dfg_lut.texture)
.map(|img| (&img.texture_view, &img.sampler))
.unwrap_or((
&fallback_texture.d2.texture_view,
&fallback_texture.d2.sampler,
));
raytracing_scene_bindings.bind_group = Some(render_device.create_bind_group(
"raytracing_scene_bind_group",
&pipeline_cache.get_bind_group_layout(&raytracing_scene_bindings.bind_group_layout),
&BindGroupEntries::sequential((
vertex_buffers.as_slice(),
index_buffers.as_slice(),
textures.as_slice(),
samplers.as_slice(),
materials.binding().unwrap(),
tlas.as_binding(),
transforms.binding().unwrap(),
previous_frame_transforms.binding().unwrap(),
geometry_ids.binding().unwrap(),
material_ids.binding().unwrap(),
light_sources.binding().unwrap(),
directional_lights.binding().unwrap(),
previous_frame_light_id_translations.binding().unwrap(),
dfg_view,
dfg_sampler,
)),
));
}
impl RaytracingSceneBindings {
pub fn new() -> Self {
Self {
bind_group: None,
bind_group_layout: BindGroupLayoutDescriptor::new(
"raytracing_scene_bind_group_layout",
&BindGroupLayoutEntries::sequential(
ShaderStages::COMPUTE,
(
storage_buffer_read_only_sized(false, None).count(MAX_MESH_SLAB_COUNT),
storage_buffer_read_only_sized(false, None).count(MAX_MESH_SLAB_COUNT),
texture_2d(TextureSampleType::Float { filterable: true })
.count(MAX_TEXTURE_COUNT),
sampler(SamplerBindingType::Filtering).count(MAX_TEXTURE_COUNT),
storage_buffer_read_only_sized(false, None),
acceleration_structure(),
storage_buffer_read_only_sized(false, None),
storage_buffer_read_only_sized(false, None),
storage_buffer_read_only_sized(false, None),
storage_buffer_read_only_sized(false, None),
storage_buffer_read_only_sized(false, None),
storage_buffer_read_only_sized(false, None),
storage_buffer_read_only_sized(false, None),
texture_2d(TextureSampleType::Float { filterable: true }),
sampler(SamplerBindingType::Filtering),
),
),
),
previous_frame_light_entities: Vec::new(),
}
}
}
impl Default for RaytracingSceneBindings {
fn default() -> Self {
Self::new()
}
}
struct CachedBindingArray<T, I: Eq + Hash> {
map: HashMap<I, u32>,
vec: Vec<T>,
}
impl<T, I: Eq + Hash> CachedBindingArray<T, I> {
fn new() -> Self {
Self {
map: HashMap::default(),
vec: Vec::default(),
}
}
fn push_if_absent(&mut self, item: T, item_id: I) -> (u32, bool) {
let mut is_new = false;
let i = *self.map.entry(item_id).or_insert_with(|| {
is_new = true;
let i = self.vec.len() as u32;
self.vec.push(item);
i
});
(i, is_new)
}
fn is_empty(&self) -> bool {
self.vec.is_empty()
}
fn as_slice(&self) -> &[T] {
self.vec.as_slice()
}
}
type StorageBufferList<T> = StorageBuffer<Vec<T>>;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
enum RaytracingMaterialId {
Standard(AssetId<StandardMaterial>),
Surface(AssetId<blacksite_surface::SurfaceMaterial>),
}
impl From<&RaytracingMaterial3d> for RaytracingMaterialId {
fn from(material: &RaytracingMaterial3d) -> Self {
match material {
RaytracingMaterial3d::Standard(handle) => Self::Standard(handle.id()),
RaytracingMaterial3d::Surface(handle) => Self::Surface(handle.id()),
}
}
}
fn material_alpha_state(alpha_mode: &AlphaMode) -> Option<(u32, f32)> {
match alpha_mode {
AlphaMode::Opaque => Some((0, 0.0)),
AlphaMode::Mask(cutoff) => Some((MATERIAL_FLAG_ALPHA_CUTOUT, *cutoff)),
AlphaMode::AlphaToCoverage => Some((MATERIAL_FLAG_ALPHA_CUTOUT, 0.5)),
AlphaMode::Blend | AlphaMode::Premultiplied | AlphaMode::Add | AlphaMode::Multiply => None,
}
}
#[derive(ShaderType)]
struct GpuInstanceGeometryIds {
vertex_buffer_id: u32,
vertex_buffer_offset: u32,
index_buffer_id: u32,
index_buffer_offset: u32,
triangle_count: u32,
}
#[derive(ShaderType)]
struct GpuMaterial {
texture_ids: UVec4,
surface_texture_ids_0: UVec4,
surface_texture_ids_1: UVec4,
material_info: UVec4,
base_color: Vec4,
emissive_roughness: Vec4,
pbr: Vec4,
surface_uniform: SurfaceUniform,
}
impl GpuMaterial {
fn new(
texture_ids: UVec4,
surface_texture_ids: [u32; 8],
flags: u32,
alpha_cutoff: f32,
material: &StandardMaterial,
surface_uniform: SurfaceUniform,
) -> Self {
Self {
texture_ids,
surface_texture_ids_0: UVec4::from_slice(&surface_texture_ids[..4]),
surface_texture_ids_1: UVec4::from_slice(&surface_texture_ids[4..]),
material_info: UVec4::new(flags, 0, 0, 0),
base_color: LinearRgba::from(material.base_color).to_vec4(),
emissive_roughness: material
.emissive
.to_vec3()
.extend(material.perceptual_roughness),
pbr: Vec4::new(material.metallic, material.reflectance, alpha_cutoff, 0.0),
surface_uniform,
}
}
fn emissive(&self) -> Vec3 {
self.emissive_roughness.truncate()
}
fn is_potentially_emissive(&self) -> bool {
(self.material_info.x & MATERIAL_FLAG_SURFACE) != 0 || self.emissive() != Vec3::ZERO
}
}
#[derive(ShaderType)]
struct GpuLightSource {
kind: u32,
id: u32,
}
impl GpuLightSource {
fn new_emissive_mesh_light(instance_id: u32, triangle_count: u32) -> GpuLightSource {
if triangle_count > u16::MAX as u32 {
panic!("Too many triangles ({triangle_count}) in an emissive mesh, maximum is 65535.");
}
Self {
kind: triangle_count << 1,
id: instance_id,
}
}
fn new_directional_light(directional_light_id: u32) -> GpuLightSource {
Self {
kind: 1,
id: directional_light_id,
}
}
}
#[derive(ShaderType, Default)]
struct GpuDirectionalLight {
direction_to_light: Vec3,
cos_theta_max: f32,
luminance: Vec3,
inverse_pdf: f32,
}
impl GpuDirectionalLight {
fn new(directional_light: &ExtractedDirectionalLight) -> Self {
let cos_theta_max = cos(directional_light.sun_disk_angular_size / 2.0);
let solid_angle = TAU * (1.0 - cos_theta_max);
let luminance =
(directional_light.color.to_vec3() * directional_light.illuminance) / solid_angle;
Self {
direction_to_light: directional_light.transform.back().into(),
cos_theta_max,
luminance,
inverse_pdf: solid_angle,
}
}
}
fn tlas_transform(transform: &Mat4) -> [f32; 12] {
transform.transpose().to_cols_array()[..12]
.try_into()
.unwrap()
}
+186
View File
@@ -0,0 +1,186 @@
use alloc::collections::VecDeque;
use bevy_asset::AssetId;
use bevy_ecs::{
resource::Resource,
system::{Res, ResMut},
};
use bevy_mesh::{Indices, Mesh};
use bevy_platform::collections::HashMap;
use bevy_render::{
mesh::{
RenderMesh,
allocator::{MeshAllocator, MeshBufferSlice},
},
render_asset::ExtractedAssets,
render_resource::*,
renderer::{RenderDevice, RenderQueue},
};
/// After compacting this many vertices worth of meshes per frame, no further BLAS will be compacted.
/// Lower this number to distribute the work across more frames.
const MAX_COMPACTION_VERTICES_PER_FRAME: u32 = 400_000;
#[derive(Resource, Default)]
pub struct BlasManager {
blas: HashMap<AssetId<Mesh>, Blas>,
compaction_queue: VecDeque<(AssetId<Mesh>, u32, bool)>,
}
impl BlasManager {
pub fn get(&self, mesh: &AssetId<Mesh>) -> Option<&Blas> {
self.blas.get(mesh)
}
}
pub fn prepare_raytracing_blas(
mut blas_manager: ResMut<BlasManager>,
extracted_meshes: Res<ExtractedAssets<RenderMesh>>,
mesh_allocator: Res<MeshAllocator>,
render_device: Res<RenderDevice>,
render_queue: Res<RenderQueue>,
) {
// Delete BLAS for deleted or modified meshes
for asset_id in extracted_meshes
.removed
.iter()
.chain(extracted_meshes.modified.iter())
{
blas_manager.blas.remove(asset_id);
}
if extracted_meshes.extracted.is_empty() {
return;
}
// Create new BLAS for added or changed meshes
let blas_resources = extracted_meshes
.extracted
.iter()
.filter(|(_, mesh)| is_mesh_raytracing_compatible(mesh))
.map(|(asset_id, _)| {
let vertex_slice = mesh_allocator.mesh_vertex_slice(asset_id).unwrap();
let index_slice = mesh_allocator.mesh_index_slice(asset_id).unwrap();
let (blas, blas_size) =
allocate_blas(&vertex_slice, &index_slice, asset_id, &render_device);
blas_manager.blas.insert(*asset_id, blas);
blas_manager
.compaction_queue
.push_back((*asset_id, blas_size.vertex_count, false));
(*asset_id, vertex_slice, index_slice, blas_size)
})
.collect::<Vec<_>>();
// Build geometry into each BLAS
let build_entries = blas_resources
.iter()
.map(|(asset_id, vertex_slice, index_slice, blas_size)| {
let geometry = BlasTriangleGeometry {
size: blas_size,
vertex_buffer: vertex_slice.buffer,
first_vertex: vertex_slice.range.start,
vertex_stride: 48,
index_buffer: Some(index_slice.buffer),
first_index: Some(index_slice.range.start),
transform_buffer: None,
transform_buffer_offset: None,
};
BlasBuildEntry {
blas: &blas_manager.blas[asset_id],
geometry: BlasGeometries::TriangleGeometries(vec![geometry]),
}
})
.collect::<Vec<_>>();
let mut command_encoder = render_device.create_command_encoder(&CommandEncoderDescriptor {
label: Some("build_blas_command_encoder"),
});
command_encoder.build_acceleration_structures(&build_entries, &[]);
render_queue.submit([command_encoder.finish()]);
}
pub fn compact_raytracing_blas(
mut blas_manager: ResMut<BlasManager>,
render_queue: Res<RenderQueue>,
) {
let queue_size = blas_manager.compaction_queue.len();
let mut meshes_processed = 0;
let mut vertices_compacted = 0;
while !blas_manager.compaction_queue.is_empty()
&& vertices_compacted < MAX_COMPACTION_VERTICES_PER_FRAME
&& meshes_processed < queue_size
{
meshes_processed += 1;
let (mesh, vertex_count, compaction_started) =
blas_manager.compaction_queue.pop_front().unwrap();
let Some(blas) = blas_manager.get(&mesh) else {
continue;
};
if !compaction_started {
blas.prepare_compaction_async(|_| {});
}
if blas.ready_for_compaction() {
let compacted_blas = render_queue.compact_blas(blas);
blas_manager.blas.insert(mesh, compacted_blas);
vertices_compacted += vertex_count;
continue;
}
// BLAS not ready for compaction, put back in queue
blas_manager
.compaction_queue
.push_back((mesh, vertex_count, true));
}
}
fn allocate_blas(
vertex_slice: &MeshBufferSlice,
index_slice: &MeshBufferSlice,
asset_id: &AssetId<Mesh>,
render_device: &RenderDevice,
) -> (Blas, BlasTriangleGeometrySizeDescriptor) {
let blas_size = BlasTriangleGeometrySizeDescriptor {
vertex_format: Mesh::ATTRIBUTE_POSITION.format,
vertex_count: vertex_slice.range.len() as u32,
index_format: Some(IndexFormat::Uint32),
index_count: Some(index_slice.range.len() as u32),
// Surface ABI cutouts must inspect each triangle candidate before committing it. Mesh
// assets are shared by differently-materialed instances, so conservatively build every
// geometry as non-opaque and let the shared evaluator accept/reject candidates.
flags: AccelerationStructureGeometryFlags::empty(),
};
let blas = render_device.wgpu_device().create_blas(
&CreateBlasDescriptor {
label: Some(&asset_id.to_string()),
flags: AccelerationStructureFlags::PREFER_FAST_TRACE
| AccelerationStructureFlags::ALLOW_COMPACTION,
update_mode: AccelerationStructureUpdateMode::Build,
},
BlasGeometrySizeDescriptors::Triangles {
descriptors: vec![blas_size.clone()],
},
);
(blas, blas_size)
}
fn is_mesh_raytracing_compatible(mesh: &Mesh) -> bool {
let triangle_list = mesh.primitive_topology() == PrimitiveTopology::TriangleList;
let vertex_attributes = mesh.attributes().map(|(attribute, _)| attribute.id).eq([
Mesh::ATTRIBUTE_POSITION.id,
Mesh::ATTRIBUTE_NORMAL.id,
Mesh::ATTRIBUTE_UV_0.id,
Mesh::ATTRIBUTE_TANGENT.id,
]);
let indexed_32 = matches!(mesh.indices(), Some(Indices::U32(..)));
mesh.enable_raytracing && triangle_list && vertex_attributes && indexed_32
}
+121
View File
@@ -0,0 +1,121 @@
enable wgpu_ray_query;
#define_import_path bevy_solari::brdf
#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::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}
#import bevy_solari::scene_bindings::{ResolvedMaterial, MIRROR_ROUGHNESS_THRESHOLD, brdf_dfg_lut, brdf_dfg_lut_sampler}
struct EvaluateAndSampleBrdfResult {
wi: vec3<f32>,
throughput: vec3<f32>,
pdf: f32,
}
fn evaluate_and_sample_brdf(
wo: vec3<f32>,
world_normal: vec3<f32>,
material: ResolvedMaterial,
rng: ptr<function, u32>,
) -> EvaluateAndSampleBrdfResult {
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 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));
var wi: vec3<f32>;
var wi_tangent: vec3<f32>;
let diffuse_selected = rand_f(rng) < diffuse_weight;
if diffuse_selected {
wi = sample_cosine_hemisphere(world_normal, rng);
wi_tangent = vec3(dot(wi, T), dot(wi, B), dot(wi, N));
} else {
wi_tangent = sample_ggx_vndf(wo_tangent, material.roughness, rng);
if ggx_vndf_sample_invalid(wi_tangent) {
return EvaluateAndSampleBrdfResult(vec3(0.0), vec3(0.0), 0.0);
}
wi = wi_tangent.x * T + wi_tangent.y * B + wi_tangent.z * N;
}
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;
}
return EvaluateAndSampleBrdfResult(wi, throughput, pdf);
}
fn evaluate_brdf(
wo: vec3<f32>,
wi: vec3<f32>,
world_normal: vec3<f32>,
material: ResolvedMaterial,
) -> vec3<f32> {
return evaluate_diffuse_brdf(wo, wi, world_normal, material) + evaluate_specular_brdf(wo, wi, world_normal, material);
}
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;
}
fn evaluate_specular_brdf(wo: vec3<f32>, wi: vec3<f32>, world_normal: vec3<f32>, material: ResolvedMaterial) -> vec3<f32> {
let H = normalize(wi + wo);
let NdotL = dot(world_normal, wi);
let NdotH = dot(world_normal, H);
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); }
let F0 = calculate_F0(material.base_color, material.metallic, vec3(material.reflectance));
let F = fresnel(F0, LdotH);
if material.roughness <= MIRROR_ROUGHNESS_THRESHOLD {
if abs(NdotH - 1.0) < 0.0001 {
return F;
} else {
return vec3(0.0);
}
}
let D = D_GGX(material.roughness, NdotH);
let Vs = V_SmithGGXCorrelated(material.roughness, NdotV, NdotL);
let F_ab = F_AB(material.perceptual_roughness, NdotV);
return specular_multiscatter(D, Vs, F, F0, F_ab, 1.0) * NdotL;
}
fn fresnel(f0: vec3<f32>, LdotH: f32) -> vec3<f32> {
return f0 + (1.0 - f0) * pow(1.0 - LdotH, 5.0);
}
// Scale/bias approximation
fn F_AB(perceptual_roughness: f32, NdotV: f32) -> vec2<f32> {
return textureSampleLevel(brdf_dfg_lut, brdf_dfg_lut_sampler, vec2<f32>(NdotV, perceptual_roughness), 0.0).rg;
}
+88
View File
@@ -0,0 +1,88 @@
use super::RaytracingMesh3d;
use bevy_asset::{AssetId, Assets};
use bevy_derive::Deref;
use bevy_ecs::{
component::Component,
resource::Resource,
system::{Commands, Query},
};
use bevy_pbr::{MeshMaterial3d, PreviousGlobalTransform, StandardMaterial};
use bevy_platform::collections::HashMap;
use bevy_render::{Extract, extract_resource::ExtractResource, sync_world::RenderEntity};
use bevy_transform::components::GlobalTransform;
use blacksite_surface::SurfaceMaterial;
pub fn extract_raytracing_scene(
instances: Extract<
Query<(
RenderEntity,
&RaytracingMesh3d,
Option<&MeshMaterial3d<StandardMaterial>>,
Option<&MeshMaterial3d<SurfaceMaterial>>,
&GlobalTransform,
Option<&PreviousGlobalTransform>,
)>,
>,
mut commands: Commands,
) {
for (render_entity, mesh, standard, surface, transform, previous_frame_transform) in &instances
{
let material = if let Some(surface) = surface {
RaytracingMaterial3d::Surface(surface.0.clone())
} else if let Some(standard) = standard {
RaytracingMaterial3d::Standard(standard.0.clone())
} else {
continue;
};
let mut commands = commands.entity(render_entity);
match previous_frame_transform.cloned() {
Some(previous_frame_transform) => commands.insert((
mesh.clone(),
material.clone(),
*transform,
previous_frame_transform,
)),
None => commands.insert((mesh.clone(), material.clone(), *transform)),
};
}
}
/// Render-world material identity selected during extraction.
#[derive(Component, Clone, Debug)]
pub enum RaytracingMaterial3d {
Standard(bevy_asset::Handle<StandardMaterial>),
Surface(bevy_asset::Handle<SurfaceMaterial>),
}
#[derive(Resource, Deref, Default)]
pub struct StandardMaterialAssets(HashMap<AssetId<StandardMaterial>, StandardMaterial>);
impl ExtractResource for StandardMaterialAssets {
type Source = Assets<StandardMaterial>;
fn extract_resource(source: &Self::Source) -> Self {
Self(
source
.iter()
.map(|(asset_id, material)| (asset_id, material.clone()))
.collect(),
)
}
}
#[derive(Resource, Deref, Default)]
pub struct SurfaceMaterialAssets(HashMap<AssetId<SurfaceMaterial>, SurfaceMaterial>);
impl ExtractResource for SurfaceMaterialAssets {
type Source = Assets<SurfaceMaterial>;
fn extract_resource(source: &Self::Source) -> Self {
Self(
source
.iter()
.map(|(asset_id, material)| (asset_id, material.clone()))
.collect(),
)
}
}
+249
View File
@@ -0,0 +1,249 @@
mod binder;
mod blas;
mod extract;
mod types;
use bevy_asset::{Assets, Handle};
use bevy_ecs::{
resource::Resource,
system::{Res, ResMut},
};
use bevy_shader::{Shader, load_shader_library};
pub use binder::RaytracingSceneBindings;
use blacksite_surface::SurfaceEvaluatorRegistry;
pub use types::RaytracingMesh3d;
use crate::SolariPlugins;
use bevy_app::{App, Plugin, PostUpdate};
use bevy_ecs::schedule::IntoScheduleConfigs;
use bevy_render::{
ExtractSchedule, GpuResourceAppExt, Render, RenderApp, RenderSystems,
extract_resource::ExtractResourcePlugin,
mesh::{
RenderMesh,
allocator::{MeshAllocatorSettings, allocate_and_free_meshes},
},
render_asset::prepare_assets,
render_resource::BufferUsages,
renderer::RenderDevice,
};
use binder::prepare_raytracing_scene_bindings;
use blas::{BlasManager, compact_raytracing_blas, prepare_raytracing_blas};
use extract::{StandardMaterialAssets, SurfaceMaterialAssets, extract_raytracing_scene};
use tracing::warn;
const EVALUATOR_LIBRARY_MARKER: &str = "// BLACKSITE_SURFACE_EVALUATOR_LIBRARY";
const EVALUATOR_DISPATCH_MARKER: &str = "// BLACKSITE_SURFACE_EVALUATOR_DISPATCH";
#[derive(Resource)]
struct SceneShaderLibraryHandle(Handle<Shader>);
#[derive(Resource, Default)]
struct SceneShaderEvaluatorRevisions(Vec<(u32, u64)>);
/// Creates acceleration structures and binding arrays of resources for raytracing.
pub struct RaytracingScenePlugin;
impl Plugin for RaytracingScenePlugin {
fn build(&self, app: &mut App) {
load_shader_library!(app, "brdf.wgsl");
bevy_asset::embedded_asset!(app, "raytracing_scene_bindings.wgsl");
let scene_shader: Handle<Shader> =
bevy_asset::load_embedded_asset!(app, "raytracing_scene_bindings.wgsl");
app.insert_resource(SceneShaderLibraryHandle(scene_shader));
app.init_resource::<SceneShaderEvaluatorRevisions>()
.add_systems(PostUpdate, sync_surface_evaluator_library);
load_shader_library!(app, "sampling.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!(
"RaytracingScenePlugin not loaded. GPU lacks support for required features: {:?}.",
SolariPlugins::required_wgpu_features().difference(features)
);
return;
}
app.add_plugins((
ExtractResourcePlugin::<StandardMaterialAssets>::default(),
ExtractResourcePlugin::<SurfaceMaterialAssets>::default(),
ExtractResourcePlugin::<SurfaceEvaluatorRegistry>::default(),
));
let render_app = app.sub_app_mut(RenderApp);
render_app
.world_mut()
.resource_mut::<MeshAllocatorSettings>()
.extra_buffer_usages |= BufferUsages::BLAS_INPUT | BufferUsages::STORAGE;
render_app
.init_gpu_resource::<BlasManager>()
.init_gpu_resource::<StandardMaterialAssets>()
.init_gpu_resource::<SurfaceMaterialAssets>()
.init_gpu_resource::<SurfaceEvaluatorRegistry>()
.insert_resource(RaytracingSceneBindings::new())
.add_systems(ExtractSchedule, extract_raytracing_scene)
.add_systems(
Render,
(
prepare_raytracing_blas
.in_set(RenderSystems::PrepareAssets)
.before(prepare_assets::<RenderMesh>)
.after(allocate_and_free_meshes),
compact_raytracing_blas
.in_set(RenderSystems::PrepareAssets)
.after(prepare_raytracing_blas),
prepare_raytracing_scene_bindings.in_set(RenderSystems::PrepareBindGroups),
),
);
}
}
fn sync_surface_evaluator_library(
registry: Res<SurfaceEvaluatorRegistry>,
handle: Res<SceneShaderLibraryHandle>,
mut revisions: ResMut<SceneShaderEvaluatorRevisions>,
mut shaders: ResMut<Assets<Shader>>,
) {
let mut next_revisions = registry
.evaluators
.values()
.map(|record| (record.shader_id, record.revision))
.collect::<Vec<_>>();
next_revisions.sort_unstable();
if revisions.0 == next_revisions {
return;
}
let source =
generate_scene_shader_source(include_str!("raytracing_scene_bindings.wgsl"), &registry);
let shader = Shader::from_wgsl(source, "generated://bevy_solari/scene_bindings.wgsl");
if let Err(error) = shaders.insert(handle.0.id(), shader) {
warn!("Solari kept the last-good Surface evaluator library: {error}");
return;
}
revisions.0 = next_revisions;
}
fn generate_scene_shader_source(template: &str, registry: &SurfaceEvaluatorRegistry) -> String {
let mut records = registry.evaluators.values().collect::<Vec<_>>();
records.sort_unstable_by_key(|record| record.shader_id);
let mut library = String::new();
let mut dispatch = String::new();
for record in records {
let names = wgsl_declaration_names(&record.evaluator_source);
if !names.iter().any(|name| name == "evaluate") {
continue;
}
let prefix = format!("blacksite_{}_", record.shader_id);
let renamed = rename_wgsl_identifiers(&record.evaluator_source, &names, &prefix);
library.push_str(&renamed);
library.push('\n');
dispatch.push_str(&format!(
" if shader_id == {}u {{ return {}evaluate(input, params, samples); }}\n",
record.shader_id, prefix
));
}
template
.replace(EVALUATOR_LIBRARY_MARKER, &library)
.replace(EVALUATOR_DISPATCH_MARKER, &dispatch)
}
fn wgsl_declaration_names(source: &str) -> Vec<String> {
let bytes = source.as_bytes();
let mut names = Vec::new();
let mut index = 0;
while index < bytes.len() {
if !is_ident_start(bytes[index]) {
index += 1;
continue;
}
let token_start = index;
index += 1;
while index < bytes.len() && is_ident_continue(bytes[index]) {
index += 1;
}
if !matches!(
&source[token_start..index],
"fn" | "struct" | "alias" | "const" | "override"
) {
continue;
}
while index < bytes.len() && bytes[index].is_ascii_whitespace() {
index += 1;
}
if index >= bytes.len() || !is_ident_start(bytes[index]) {
continue;
}
let name_start = index;
index += 1;
while index < bytes.len() && is_ident_continue(bytes[index]) {
index += 1;
}
let name = source[name_start..index].to_string();
if !names.contains(&name) {
names.push(name);
}
}
names
}
fn rename_wgsl_identifiers(source: &str, names: &[String], prefix: &str) -> String {
let bytes = source.as_bytes();
let mut result = String::with_capacity(source.len() + names.len() * prefix.len());
let mut index = 0;
while index < bytes.len() {
if !is_ident_start(bytes[index]) {
let character = source[index..]
.chars()
.next()
.expect("index is inside the source string");
result.push(character);
index += character.len_utf8();
continue;
}
let start = index;
index += 1;
while index < bytes.len() && is_ident_continue(bytes[index]) {
index += 1;
}
let token = &source[start..index];
if names.iter().any(|name| name == token) {
result.push_str(prefix);
}
result.push_str(token);
}
result
}
const fn is_ident_start(byte: u8) -> bool {
byte == b'_' || byte.is_ascii_alphabetic()
}
const fn is_ident_continue(byte: u8) -> bool {
is_ident_start(byte) || byte.is_ascii_digit()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn evaluator_generation_namespaces_helpers() {
let evaluator = "const SCALE: f32 = 1.0; fn helper(v: f32) -> f32 { return v * SCALE; } fn evaluate(input: SurfaceInput, params: SurfaceParams, samples: SurfaceSamples) -> Surface { var s = surface_default(); s.metallic = helper(1.0); return s; }";
let names = wgsl_declaration_names(evaluator);
let generated = rename_wgsl_identifiers(evaluator, &names, "blacksite_42_");
assert!(generated.contains("const blacksite_42_SCALE"));
assert!(generated.contains("v * blacksite_42_SCALE"));
assert!(generated.contains("fn blacksite_42_helper"));
assert!(generated.contains("blacksite_42_helper(1.0)"));
assert!(generated.contains("fn blacksite_42_evaluate"));
}
}
@@ -0,0 +1,396 @@
enable wgpu_ray_query;
#define_import_path bevy_solari::scene_bindings
#import bevy_pbr::lighting::perceptualRoughnessToRoughness
#import bevy_pbr::pbr_functions::calculate_tbn_mikktspace
struct InstanceGeometryIds {
vertex_buffer_id: u32,
vertex_buffer_offset: u32,
index_buffer_id: u32,
index_buffer_offset: u32,
triangle_count: u32,
}
struct VertexBuffer { vertices: array<PackedVertex> }
struct IndexBuffer { indices: array<u32> }
struct PackedVertex {
a: vec4<f32>,
b: vec4<f32>,
tangent: vec4<f32>,
}
struct Vertex {
position: vec3<f32>,
normal: vec3<f32>,
uv: vec2<f32>,
tangent: vec4<f32>,
}
fn unpack_vertex(packed: PackedVertex) -> Vertex {
var vertex: Vertex;
vertex.position = packed.a.xyz;
vertex.normal = vec3(packed.a.w, packed.b.xy);
vertex.uv = packed.b.zw;
vertex.tangent = packed.tangent;
return vertex;
}
struct SurfaceUniform {
shader_id: u32,
flags: u32,
alpha_cutoff: f32,
abi_version: u32,
params: array<vec4<f32>, 16>,
uv_transforms: array<vec4<f32>, 8>,
}
struct Material {
texture_ids: vec4<u32>,
surface_texture_ids_0: vec4<u32>,
surface_texture_ids_1: vec4<u32>,
material_info: vec4<u32>,
base_color: vec4<f32>,
emissive_roughness: vec4<f32>,
pbr: vec4<f32>,
surface_uniform: SurfaceUniform,
}
const TEXTURE_MAP_NONE = 0xFFFFFFFFu;
const MATERIAL_FLAG_ALPHA_CUTOUT = 1u << 0u;
const MATERIAL_FLAG_SURFACE = 1u << 1u;
const BLACKSITE_SURFACE_UNLIT = 1u;
const MIRROR_ROUGHNESS_THRESHOLD = 0.001f;
struct LightSource {
kind: u32, // 1 bit for kind, 31 bits for extra data
id: u32,
}
const LIGHT_SOURCE_KIND_EMISSIVE_MESH = 0u;
const LIGHT_SOURCE_KIND_DIRECTIONAL = 1u;
struct DirectionalLight {
direction_to_light: vec3<f32>,
cos_theta_max: f32,
luminance: vec3<f32>,
inverse_pdf: f32,
}
const LIGHT_NOT_PRESENT_THIS_FRAME = 0xFFFFFFFFu;
@group(0) @binding(0) var<storage> vertex_buffers: binding_array<VertexBuffer>;
@group(0) @binding(1) var<storage> index_buffers: binding_array<IndexBuffer>;
@group(0) @binding(2) var textures: binding_array<texture_2d<f32>>;
@group(0) @binding(3) var samplers: binding_array<sampler>;
@group(0) @binding(4) var<storage> materials: array<Material>;
@group(0) @binding(5) var tlas: acceleration_structure;
@group(0) @binding(6) var<storage> transforms: array<mat4x4<f32>>; // TODO: Use mat3x4<f32>?
@group(0) @binding(7) var<storage> previous_frame_transforms: array<mat4x4<f32>>; // TODO: Use mat3x4<f32>?
@group(0) @binding(8) var<storage> geometry_ids: array<InstanceGeometryIds>;
@group(0) @binding(9) var<storage> material_ids: array<u32>; // TODO: Store material_id in instance_custom_index instead?
@group(0) @binding(10) var<storage> light_sources: array<LightSource>;
@group(0) @binding(11) var<storage> directional_lights: array<DirectionalLight>;
@group(0) @binding(12) var<storage> previous_frame_light_id_translations: array<u32>;
@group(0) @binding(13) var brdf_dfg_lut: texture_2d<f32>;
@group(0) @binding(14) var brdf_dfg_lut_sampler: sampler;
const RAY_T_MIN = 0.001f;
const RAY_T_MAX = 100000.0f;
const RAY_NO_CULL = 0xFFu;
fn trace_ray(ray_origin: vec3<f32>, ray_direction: vec3<f32>, ray_t_min: f32, ray_t_max: f32, ray_flag: u32) -> RayIntersection {
let ray = RayDesc(ray_flag, RAY_NO_CULL, ray_t_min, ray_t_max, ray_origin, ray_direction);
var rq: ray_query;
rayQueryInitialize(&rq, tlas, ray);
while (rayQueryProceed(&rq)) {
let candidate = rayQueryGetCandidateIntersection(&rq);
if candidate.kind == RAY_QUERY_INTERSECTION_TRIANGLE && candidate_passes_alpha(candidate) {
rayQueryConfirmIntersection(&rq);
}
}
return rayQueryGetCommittedIntersection(&rq);
}
fn sample_texture(id: u32, uv: vec2<f32>) -> vec4<f32> {
return textureSampleLevel(textures[id], samplers[id], uv, 0.0); // TODO: Mipmap
}
struct SurfaceInput {
uv0: vec2<f32>,
world_position: vec3<f32>,
world_normal: vec3<f32>,
}
struct SurfaceParams {
lanes: array<vec4<f32>, 16>,
}
struct SurfaceSamples {
values: array<vec4<f32>, 8>,
}
struct Surface {
base_color: vec4<f32>,
normal_ts: vec3<f32>,
emissive: vec3<f32>,
metallic: f32,
perceptual_roughness: f32,
reflectance: f32,
occlusion: f32,
model: u32,
}
fn surface_default() -> Surface {
var surface: Surface;
surface.base_color = vec4<f32>(1.0);
surface.normal_ts = vec3<f32>(0.0, 0.0, 1.0);
surface.emissive = vec3<f32>(0.0);
surface.metallic = 0.0;
surface.perceptual_roughness = 0.5;
surface.reflectance = 0.5;
surface.occlusion = 1.0;
surface.model = 0u;
return surface;
}
fn surface_texture_id(material: Material, index: u32) -> u32 {
if index < 4u {
return material.surface_texture_ids_0[index];
}
return material.surface_texture_ids_1[index - 4u];
}
fn surface_samples(material: Material, uv: vec2<f32>) -> SurfaceSamples {
var samples: SurfaceSamples;
for (var index = 0u; index < 8u; index += 1u) {
let transform = material.surface_uniform.uv_transforms[index];
let transformed_uv = uv * transform.xy + transform.zw;
let texture_id = surface_texture_id(material, index);
samples.values[index] = vec4<f32>(1.0);
if texture_id != TEXTURE_MAP_NONE {
samples.values[index] = sample_texture(texture_id, transformed_uv);
}
}
return samples;
}
// BLACKSITE_SURFACE_EVALUATOR_LIBRARY
fn evaluate_surface(
shader_id: u32,
input: SurfaceInput,
params: SurfaceParams,
samples: SurfaceSamples,
) -> Surface {
// BLACKSITE_SURFACE_EVALUATOR_DISPATCH
return surface_default();
}
struct ResolvedMaterial {
base_color: vec3<f32>,
emissive: vec3<f32>,
reflectance: f32,
perceptual_roughness: f32,
roughness: f32,
metallic: f32,
}
struct ResolvedSurface {
material: ResolvedMaterial,
normal_ts: vec3<f32>,
alpha: f32,
}
struct ResolvedRayHitFull {
world_position: vec3<f32>,
previous_frame_world_position: vec3<f32>,
world_normal: vec3<f32>,
geometric_world_normal: vec3<f32>,
world_tangent: vec4<f32>,
uv: vec2<f32>,
triangle_area: f32,
triangle_count: u32,
material: ResolvedMaterial,
}
fn resolve_standard_surface(material: Material, uv: vec2<f32>) -> ResolvedSurface {
var m: ResolvedMaterial;
var base_color = material.base_color;
if material.texture_ids.y != TEXTURE_MAP_NONE {
base_color *= sample_texture(material.texture_ids.y, uv);
}
m.base_color = base_color.rgb;
m.emissive = material.emissive_roughness.rgb;
if material.texture_ids.z != TEXTURE_MAP_NONE {
m.emissive *= sample_texture(material.texture_ids.z, uv).rgb;
}
m.reflectance = material.pbr.y;
m.perceptual_roughness = material.emissive_roughness.w;
m.metallic = material.pbr.x;
if material.texture_ids.w != TEXTURE_MAP_NONE {
let metallic_roughness = sample_texture(material.texture_ids.w, uv).rgb;
m.perceptual_roughness *= metallic_roughness.g;
m.metallic *= metallic_roughness.b;
}
m.roughness = m.perceptual_roughness * m.perceptual_roughness;
return ResolvedSurface(m, vec3<f32>(0.0, 0.0, 1.0), base_color.a);
}
fn resolve_surface(
material: Material,
uv: vec2<f32>,
world_position: vec3<f32>,
world_normal: vec3<f32>,
) -> ResolvedSurface {
if (material.material_info.x & MATERIAL_FLAG_SURFACE) == 0u {
return resolve_standard_surface(material, uv);
}
let input = SurfaceInput(uv, world_position, world_normal);
let params = SurfaceParams(material.surface_uniform.params);
let surface = evaluate_surface(
material.surface_uniform.shader_id,
input,
params,
surface_samples(material, uv),
);
var m: ResolvedMaterial;
m.base_color = surface.base_color.rgb * clamp(surface.occlusion, 0.0, 1.0);
m.emissive = surface.emissive;
m.reflectance = clamp(surface.reflectance, 0.0, 1.0);
m.perceptual_roughness = clamp(surface.perceptual_roughness, 0.001, 1.0);
m.roughness = m.perceptual_roughness * m.perceptual_roughness;
m.metallic = clamp(surface.metallic, 0.0, 1.0);
if surface.model == BLACKSITE_SURFACE_UNLIT {
m.emissive += surface.base_color.rgb;
m.base_color = vec3<f32>(0.0);
m.metallic = 0.0;
m.perceptual_roughness = 1.0;
m.roughness = 1.0;
}
return ResolvedSurface(m, surface.normal_ts, surface.base_color.a);
}
fn resolve_ray_hit_full(ray_hit: RayIntersection) -> ResolvedRayHitFull {
let barycentrics = vec3(1.0 - ray_hit.barycentrics.x - ray_hit.barycentrics.y, ray_hit.barycentrics);
return resolve_triangle_data_full(ray_hit.instance_index, ray_hit.primitive_index, barycentrics);
}
fn load_vertices(instance_geometry_ids: InstanceGeometryIds, triangle_id: u32) -> array<Vertex, 3> {
let index_buffer = &index_buffers[instance_geometry_ids.index_buffer_id].indices;
let vertex_buffer = &vertex_buffers[instance_geometry_ids.vertex_buffer_id].vertices;
let indices_i = (triangle_id * 3u) + vec3(0u, 1u, 2u) + instance_geometry_ids.index_buffer_offset;
let indices = vec3((*index_buffer)[indices_i.x], (*index_buffer)[indices_i.y], (*index_buffer)[indices_i.z]) + instance_geometry_ids.vertex_buffer_offset;
return array<Vertex, 3>(
unpack_vertex((*vertex_buffer)[indices.x]),
unpack_vertex((*vertex_buffer)[indices.y]),
unpack_vertex((*vertex_buffer)[indices.z])
);
}
fn transform_positions(transform: mat4x4<f32>, vertices: array<Vertex, 3>) -> array<vec3<f32>, 3> {
return array<vec3<f32>, 3>(
(transform * vec4(vertices[0].position, 1.0)).xyz,
(transform * vec4(vertices[1].position, 1.0)).xyz,
(transform * vec4(vertices[2].position, 1.0)).xyz
);
}
fn candidate_passes_alpha(candidate: RayIntersection) -> bool {
let material = materials[material_ids[candidate.instance_index]];
if (material.material_info.x & MATERIAL_FLAG_ALPHA_CUTOUT) == 0u {
return true;
}
let barycentrics = vec3(
1.0 - candidate.barycentrics.x - candidate.barycentrics.y,
candidate.barycentrics,
);
let instance_geometry_ids = geometry_ids[candidate.instance_index];
let vertices = load_vertices(instance_geometry_ids, candidate.primitive_index);
let transform = transforms[candidate.instance_index];
let world_vertices = transform_positions(transform, vertices);
let world_position = mat3x3(world_vertices[0], world_vertices[1], world_vertices[2]) * barycentrics;
let uv = mat3x2(vertices[0].uv, vertices[1].uv, vertices[2].uv) * barycentrics;
let local_normal = mat3x3(vertices[0].normal, vertices[1].normal, vertices[2].normal) * barycentrics;
let world_normal = normalize(mat3x3(transform[0].xyz, transform[1].xyz, transform[2].xyz) * local_normal);
let resolved = resolve_surface(material, uv, world_position, world_normal);
let cutoff = select(
material.pbr.z,
material.surface_uniform.alpha_cutoff,
(material.material_info.x & MATERIAL_FLAG_SURFACE) != 0u,
);
return resolved.alpha >= cutoff;
}
fn resolve_triangle_data_full(instance_id: u32, triangle_id: u32, barycentrics: vec3<f32>) -> ResolvedRayHitFull {
let material_id = material_ids[instance_id];
let material = materials[material_id];
let transform = transforms[instance_id];
let previous_frame_transform = previous_frame_transforms[instance_id];
let instance_geometry_ids = geometry_ids[instance_id];
let vertices = load_vertices(instance_geometry_ids, triangle_id);
let world_vertices = transform_positions(transform, vertices);
let world_position = mat3x3(world_vertices[0], world_vertices[1], world_vertices[2]) * barycentrics;
let previous_frame_world_vertices = transform_positions(previous_frame_transform, vertices);
let previous_frame_world_position = mat3x3(previous_frame_world_vertices[0], previous_frame_world_vertices[1], previous_frame_world_vertices[2]) * barycentrics;
let uv = mat3x2(vertices[0].uv, vertices[1].uv, vertices[2].uv) * barycentrics;
let local_tangent = mat3x3(vertices[0].tangent.xyz, vertices[1].tangent.xyz, vertices[2].tangent.xyz) * barycentrics;
let world_tangent = vec4(
normalize(mat3x3(transform[0].xyz, transform[1].xyz, transform[2].xyz) * local_tangent),
vertices[0].tangent.w,
);
let local_normal = mat3x3(vertices[0].normal, vertices[1].normal, vertices[2].normal) * barycentrics; // TODO: Use barycentric lerp, ray_hit.object_to_world, cross product geo normal
var world_normal = normalize(mat3x3(transform[0].xyz, transform[1].xyz, transform[2].xyz) * local_normal);
let geometric_world_normal = world_normal;
let resolved_surface = resolve_surface(material, uv, world_position, world_normal);
if (material.material_info.x & MATERIAL_FLAG_SURFACE) != 0u {
let TBN = calculate_tbn_mikktspace(world_normal, world_tangent);
let Nt = normalize(resolved_surface.normal_ts);
world_normal = normalize(Nt.x * TBN[0] + Nt.y * TBN[1] + Nt.z * TBN[2]);
} else if material.texture_ids.x != TEXTURE_MAP_NONE {
let TBN = calculate_tbn_mikktspace(world_normal, world_tangent);
let T = TBN[0];
let B = TBN[1];
let N = TBN[2];
let Nt = sample_texture(material.texture_ids.x, uv).xyz;
world_normal = normalize(Nt.x * T + Nt.y * B + Nt.z * N);
}
let triangle_edge0 = world_vertices[0] - world_vertices[1];
let triangle_edge1 = world_vertices[0] - world_vertices[2];
let triangle_area = length(cross(triangle_edge0, triangle_edge1)) / 2.0;
return ResolvedRayHitFull(
world_position,
previous_frame_world_position,
world_normal,
geometric_world_normal,
world_tangent,
uv,
triangle_area,
instance_geometry_ids.triangle_count,
resolved_surface.material,
);
}
+251
View File
@@ -0,0 +1,251 @@
enable wgpu_ray_query;
#define_import_path bevy_solari::sampling
#import bevy_pbr::lighting::D_GGX
#import bevy_pbr::utils::{rand_f, rand_vec2f, rand_u, rand_range_u}
#import bevy_render::maths::{PI_2, orthonormalize}
#import bevy_solari::scene_bindings::{trace_ray, RAY_T_MIN, RAY_T_MAX, light_sources, directional_lights, LightSource, LIGHT_SOURCE_KIND_DIRECTIONAL, resolve_triangle_data_full, ResolvedRayHitFull, MIRROR_ROUGHNESS_THRESHOLD}
fn power_heuristic(f: f32, g: f32) -> f32 {
return balance_heuristic(f * f, g * g);
}
fn balance_heuristic(f: f32, g: f32) -> f32 {
// Need to guard against NaNs since ReSTIR reservoirs can have UCW=0
if f == 0.0 {
return 0.0;
}
return max(0.0, 1.0 / (1.0 + (g / f)));
}
// https://gpuopen.com/download/Bounded_VNDF_Sampling_for_Smith-GGX_Reflections.pdf (Listing 1)
// Result is invalid when output.z <= 0.0, and must be discarded
fn sample_ggx_vndf(wi_tangent: vec3<f32>, roughness: f32, rng: ptr<function, u32>) -> vec3<f32> {
// Mirror BRDF case
if roughness <= MIRROR_ROUGHNESS_THRESHOLD {
return vec3(-wi_tangent.xy, wi_tangent.z);
}
let i = wi_tangent;
let rand = rand_vec2f(rng);
let i_std = normalize(vec3(i.xy * roughness, i.z));
let phi = PI_2 * rand.x;
let a = roughness;
let s = 1.0 + length(vec2(i.xy));
let a2 = a * a;
let s2 = s * s;
let k = (1.0 - a2) * s2 / (s2 + a2 * i.z * i.z);
let b = select(i_std.z, k * i_std.z, i.z > 0.0);
let z = fma(1.0 - rand.y, 1.0 + b, -b);
let sin_theta = sqrt(saturate(1.0 - z * z));
let o_std = vec3(sin_theta * cos(phi), sin_theta * sin(phi), z);
let m_std = i_std + o_std;
let m = normalize(vec3(m_std.xy * roughness, m_std.z));
return 2.0 * dot(i, m) * m - i;
}
fn ggx_vndf_sample_invalid(ray_tangent: vec3<f32>) -> bool {
return !(ray_tangent.z > 0.0);
}
// https://gpuopen.com/download/Bounded_VNDF_Sampling_for_Smith-GGX_Reflections.pdf (Listing 2)
fn ggx_vndf_pdf(wi_tangent: vec3<f32>, wo_tangent: vec3<f32>, roughness: f32) -> f32 {
// Mirror BRDF case
if roughness <= MIRROR_ROUGHNESS_THRESHOLD {
let mirror_wo = vec3(-wi_tangent.xy, wi_tangent.z);
if all(abs(mirror_wo - wo_tangent) < vec3(0.0001)) {
return bitcast<f32>(0x7F800000u); // INF
} else {
return 0.0;
}
}
let i = wi_tangent;
let o = wo_tangent;
let m = normalize(i + o);
let ndf = D_GGX(roughness, saturate(m.z));
let ai = roughness * i.xy;
let len2 = dot(ai, ai);
let t = sqrt(len2 + i.z * i.z);
var pdf: f32;
if i.z >= 0.0 {
let a = roughness;
let s = 1.0 + length(i.xy);
let a2 = a * a;
let s2 = s * s;
let k = (1.0 - a2) * s2 / (s2 + a2 * i.z * i.z);
pdf = ndf / (2.0 * (k * i.z + t));
} else {
pdf = ndf * (t - i.z) / (2.0 * len2);
}
return select(pdf, 0.0, isnan(pdf));
}
fn isnan(x: f32) -> bool {
return (bitcast<u32>(x) & 0x7fffffffu) > 0x7f800000u;
}
const NULL_LIGHT_ID = 0xFFFFFFFFu;
struct LightSample {
light_id: u32,
seed: u32,
}
struct ResolvedLightSample {
world_position: vec4<f32>,
world_normal: vec3<f32>,
radiance: vec3<f32>,
inverse_pdf: f32,
}
struct LightContribution {
radiance: vec3<f32>,
inverse_pdf: f32,
wi: vec3<f32>,
brdf_rays_can_hit: bool,
}
struct LightContributionNoPdf {
radiance: vec3<f32>,
wi: vec3<f32>,
}
struct GenerateRandomLightSampleResult {
light_sample: LightSample,
resolved_light_sample: ResolvedLightSample,
}
fn sample_random_light(ray_origin: vec3<f32>, origin_world_normal: vec3<f32>, rng: ptr<function, u32>) -> LightContribution {
let sample = generate_random_light_sample(rng);
var light_contribution = calculate_resolved_light_contribution(sample.resolved_light_sample, ray_origin, origin_world_normal);
light_contribution.radiance *= trace_light_visibility(ray_origin, sample.resolved_light_sample.world_position);
return light_contribution;
}
fn random_emissive_light_pdf(hit: ResolvedRayHitFull) -> f32 {
let light_count = arrayLength(&light_sources);
return 1.0 / (f32(light_count) * f32(hit.triangle_count) * hit.triangle_area);
}
fn generate_random_light_sample(rng: ptr<function, u32>) -> GenerateRandomLightSampleResult {
let light_count = arrayLength(&light_sources);
let light_id = rand_range_u(light_count, rng);
let light_source = light_sources[light_id];
var triangle_id = 0u;
if light_source.kind != LIGHT_SOURCE_KIND_DIRECTIONAL {
let triangle_count = light_source.kind >> 1u;
triangle_id = rand_range_u(triangle_count, rng);
}
let seed = rand_u(rng);
let light_sample = LightSample((light_id << 16u) | triangle_id, seed);
var resolved_light_sample = resolve_light_sample(light_sample, light_source);
resolved_light_sample.inverse_pdf *= f32(light_count);
return GenerateRandomLightSampleResult(light_sample, resolved_light_sample);
}
fn resolve_light_sample(light_sample: LightSample, light_source: LightSource) -> ResolvedLightSample {
if light_source.kind == LIGHT_SOURCE_KIND_DIRECTIONAL {
let directional_light = directional_lights[light_source.id];
#ifndef NO_DIRECTIONAL_LIGHT_SOFT_SHADOWS
// Sample a random direction within a cone whose base is the sun approximated as a disk
// https://www.realtimerendering.com/raytracinggems/unofficial_RayTracingGems_v1.9.pdf#0004286901.INDD%3ASec30%3A305
var rng = light_sample.seed;
let random = rand_vec2f(&rng);
let cos_theta = (1.0 - random.x) + random.x * directional_light.cos_theta_max;
let sin_theta = sqrt(1.0 - cos_theta * cos_theta);
let phi = random.y * PI_2;
let x = cos(phi) * sin_theta;
let y = sin(phi) * sin_theta;
var direction_to_light = vec3(x, y, cos_theta);
// Rotate the ray so that the cone it was sampled from is aligned with the light direction
direction_to_light = orthonormalize(directional_light.direction_to_light) * direction_to_light;
#else
let direction_to_light = directional_light.direction_to_light;
#endif
return ResolvedLightSample(
vec4(direction_to_light, 0.0),
-direction_to_light,
directional_light.luminance,
directional_light.inverse_pdf,
);
} else {
let triangle_count = light_source.kind >> 1u;
let triangle_id = light_sample.light_id & 0xFFFFu;
let barycentrics = triangle_barycentrics(light_sample.seed);
let triangle_data = resolve_triangle_data_full(light_source.id, triangle_id, barycentrics);
return ResolvedLightSample(
vec4(triangle_data.world_position, 1.0),
triangle_data.world_normal,
triangle_data.material.emissive.rgb,
f32(triangle_count) * triangle_data.triangle_area,
);
}
}
fn calculate_resolved_light_contribution(resolved_light_sample: ResolvedLightSample, ray_origin: vec3<f32>, origin_world_normal: vec3<f32>) -> LightContribution {
let ray = resolved_light_sample.world_position.xyz - (resolved_light_sample.world_position.w * ray_origin);
let light_distance = length(ray);
let wi = ray / light_distance;
let cos_theta_light = saturate(dot(-wi, resolved_light_sample.world_normal));
let light_distance_squared = light_distance * light_distance;
let radiance = resolved_light_sample.radiance * (cos_theta_light / light_distance_squared);
return LightContribution(radiance, resolved_light_sample.inverse_pdf, wi, resolved_light_sample.world_position.w == 1.0);
}
fn resolve_and_calculate_light_contribution(light_sample: LightSample, ray_origin: vec3<f32>, origin_world_normal: vec3<f32>) -> LightContributionNoPdf {
let resolved_light_sample = resolve_light_sample(light_sample, light_sources[light_sample.light_id >> 16u]);
let light_contribution = calculate_resolved_light_contribution(resolved_light_sample, ray_origin, origin_world_normal);
return LightContributionNoPdf(light_contribution.radiance, light_contribution.wi);
}
fn trace_light_visibility(ray_origin: vec3<f32>, light_sample_world_position: vec4<f32>) -> f32 {
var ray_direction = light_sample_world_position.xyz;
var ray_t_max = RAY_T_MAX;
if light_sample_world_position.w == 1.0 {
let ray = ray_direction - ray_origin;
let dist = length(ray);
ray_direction = ray / dist;
ray_t_max = dist - RAY_T_MIN;
}
if ray_t_max < RAY_T_MIN { return 0.0; }
let ray_hit = trace_ray(ray_origin, ray_direction, RAY_T_MIN, ray_t_max, RAY_FLAG_TERMINATE_ON_FIRST_HIT);
return f32(ray_hit.kind == RAY_QUERY_INTERSECTION_NONE);
}
fn trace_point_visibility(ray_origin: vec3<f32>, point: vec3<f32>) -> f32 {
let ray = point - ray_origin;
let dist = length(ray);
let ray_direction = ray / dist;
let ray_t_max = dist - RAY_T_MIN;
if ray_t_max < RAY_T_MIN { return 0.0; }
let ray_hit = trace_ray(ray_origin, ray_direction, RAY_T_MIN, ray_t_max, RAY_FLAG_TERMINATE_ON_FIRST_HIT);
return f32(ray_hit.kind == RAY_QUERY_INTERSECTION_NONE);
}
// https://www.realtimerendering.com/raytracinggems/unofficial_RayTracingGems_v1.9.pdf#0004286901.INDD%3ASec22%3A297
fn triangle_barycentrics(seed: u32) -> vec3<f32> {
var rng = seed;
var barycentrics = rand_vec2f(&rng);
if barycentrics.x + barycentrics.y > 1.0 { barycentrics = 1.0 - barycentrics; }
return vec3(1.0 - barycentrics.x - barycentrics.y, barycentrics);
}
+24
View File
@@ -0,0 +1,24 @@
use bevy_asset::Handle;
use bevy_derive::{Deref, DerefMut};
use bevy_ecs::{component::Component, prelude::ReflectComponent, template::FromTemplate};
use bevy_mesh::Mesh;
use bevy_reflect::{Reflect, prelude::ReflectDefault};
use bevy_render::sync_world::SyncToRenderWorld;
use bevy_transform::components::Transform;
use derive_more::derive::From;
/// A mesh component used for raytracing.
///
/// The mesh used in this component must have [`Mesh::enable_raytracing`] set to true,
/// use the following set of vertex attributes: `{POSITION, NORMAL, UV_0, TANGENT}`, use [`bevy_mesh::PrimitiveTopology::TriangleList`],
/// and use [`bevy_mesh::Indices::U32`].
///
/// The entity must also have either a standard Bevy material or a Blacksite Surface material.
/// Unsupported or missing material generations are omitted from the ray-tracing scene instead of
/// being rendered with an approximate fallback.
#[derive(
Component, FromTemplate, Clone, Debug, Default, Deref, DerefMut, Reflect, PartialEq, Eq, From,
)]
#[reflect(Component, Default, Clone, PartialEq)]
#[require(Transform, SyncToRenderWorld)]
pub struct RaytracingMesh3d(pub Handle<Mesh>);