Skip to content

Commit 1d3a05e

Browse files
committed
more docs
2 parents bb8a01c + 853b64f commit 1d3a05e

File tree

16 files changed

+935
-401
lines changed

16 files changed

+935
-401
lines changed

crates/bevy_pbr/src/atmosphere/aerial_view_lut.wgsl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
types::{Atmosphere, AtmosphereSettings},
55
bindings::{atmosphere, settings, view, lights, aerial_view_lut_out},
66
functions::{
7-
sample_transmittance_lut, sample_atmosphere, rayleigh, henyey_greenstein,
7+
sample_transmittance_lut, sample_density_lut, rayleigh, henyey_greenstein,
88
sample_multiscattering_lut, AtmosphereSample, sample_local_inscattering,
99
uv_to_ndc, max_atmosphere_distance, uv_to_ray_direction,
10-
MIDPOINT_RATIO, get_view_position
10+
MIDPOINT_RATIO, get_view_position, MIN_EXTINCTION
1111
},
1212
}
1313
}
1414

1515

16-
@group(0) @binding(13) var aerial_view_lut_out: texture_storage_3d<rgba16float, write>;
16+
@group(0) @binding(16) var aerial_view_lut_out: texture_storage_3d<rgba16float, write>;
1717

1818
@compute
1919
@workgroup_size(16, 16, 1)
@@ -41,15 +41,18 @@ fn main(@builtin(global_invocation_id) idx: vec3<u32>) {
4141
let local_r = length(sample_pos);
4242
let local_up = normalize(sample_pos);
4343

44-
let local_atmosphere = sample_atmosphere(local_r);
45-
let sample_optical_depth = local_atmosphere.extinction * dt;
44+
let absorption = sample_density_lut(local_r, 0.0);
45+
let scattering = sample_density_lut(local_r, 1.0);
46+
let extinction = absorption + scattering;
47+
48+
let sample_optical_depth = extinction * dt;
4649
let sample_transmittance = exp(-sample_optical_depth);
4750

4851
// evaluate one segment of the integral
49-
var inscattering = sample_local_inscattering(local_atmosphere, ray_dir, sample_pos);
52+
var inscattering = sample_local_inscattering(scattering, ray_dir, sample_pos);
5053

5154
// Analytical integration of the single scattering term in the radiance transfer equation
52-
let s_int = (inscattering - inscattering * sample_transmittance) / local_atmosphere.extinction;
55+
let s_int = (inscattering - inscattering * sample_transmittance) / max(extinction, MIN_EXTINCTION);
5356
total_inscattering += throughput * s_int;
5457

5558
throughput *= sample_transmittance;

crates/bevy_pbr/src/atmosphere/bindings.wgsl

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@
1212
@group(0) @binding(2) var<uniform> atmosphere_transforms: AtmosphereTransforms;
1313
@group(0) @binding(3) var<uniform> view: View;
1414
@group(0) @binding(4) var<uniform> lights: Lights;
15-
@group(0) @binding(5) var transmittance_lut: texture_2d<f32>;
16-
@group(0) @binding(6) var transmittance_lut_sampler: sampler;
17-
@group(0) @binding(7) var multiscattering_lut: texture_2d<f32>;
18-
@group(0) @binding(8) var multiscattering_lut_sampler: sampler;
19-
@group(0) @binding(9) var sky_view_lut: texture_2d<f32>;
20-
@group(0) @binding(10) var sky_view_lut_sampler: sampler;
21-
@group(0) @binding(11) var aerial_view_lut: texture_3d<f32>;
22-
@group(0) @binding(12) var aerial_view_lut_sampler: sampler;
15+
16+
@group(0) @binding(5) var medium_density_lut: texture_2d<f32>;
17+
@group(0) @binding(6) var medium_scattering_lut: texture_2d<f32>;
18+
@group(0) @binding(7) var medium_sampler: sampler;
19+
20+
@group(0) @binding(8) var transmittance_lut: texture_2d<f32>;
21+
@group(0) @binding(9) var transmittance_lut_sampler: sampler;
22+
@group(0) @binding(10) var multiscattering_lut: texture_2d<f32>;
23+
@group(0) @binding(11) var multiscattering_lut_sampler: sampler;
24+
@group(0) @binding(12) var sky_view_lut: texture_2d<f32>;
25+
@group(0) @binding(13) var sky_view_lut_sampler: sampler;
26+
@group(0) @binding(14) var aerial_view_lut: texture_3d<f32>;
27+
@group(0) @binding(15) var aerial_view_lut_sampler: sampler;

crates/bevy_pbr/src/atmosphere/environment.rs

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::{
22
resources::{
33
AtmosphereSamplers, AtmosphereTextures, AtmosphereTransform, AtmosphereTransforms,
4-
AtmosphereTransformsOffset,
4+
AtmosphereTransformsOffset, GpuAtmosphere,
55
},
6-
GpuAtmosphereSettings, GpuLights, LightMeta, ViewLightsUniformOffset,
6+
ExtractedAtmosphere, GpuAtmosphereSettings, GpuLights, LightMeta, ViewLightsUniformOffset,
77
};
88
use bevy_asset::{load_embedded_asset, AssetServer, Assets, Handle, RenderAssetUsages};
99
use bevy_ecs::{
@@ -29,8 +29,6 @@ use bevy_render::{
2929
use bevy_utils::default;
3030
use tracing::warn;
3131

32-
use super::Atmosphere;
33-
3432
// Render world representation of an environment map light for the atmosphere
3533
#[derive(Component, ExtractComponent, Clone)]
3634
pub struct AtmosphereEnvironmentMap {
@@ -63,28 +61,37 @@ pub struct AtmosphereProbePipeline {
6361
}
6462

6563
pub fn init_atmosphere_probe_layout(mut commands: Commands, render_device: Res<RenderDevice>) {
64+
const FILTERED_TEX: TextureSampleType = TextureSampleType::Float { filterable: true };
65+
const FILTERED_SMP: SamplerBindingType = SamplerBindingType::Filtering;
6666
let environment = render_device.create_bind_group_layout(
6767
"environment_bind_group_layout",
68-
&BindGroupLayoutEntries::sequential(
68+
&BindGroupLayoutEntries::with_indices(
6969
ShaderStages::COMPUTE,
7070
(
71-
uniform_buffer::<Atmosphere>(true),
72-
uniform_buffer::<GpuAtmosphereSettings>(true),
73-
uniform_buffer::<AtmosphereTransform>(true),
74-
uniform_buffer::<ViewUniform>(true),
75-
uniform_buffer::<GpuLights>(true),
76-
texture_2d(TextureSampleType::Float { filterable: true }), //transmittance lut and sampler
77-
sampler(SamplerBindingType::Filtering),
78-
texture_2d(TextureSampleType::Float { filterable: true }), //multiscattering lut and sampler
79-
sampler(SamplerBindingType::Filtering),
80-
texture_2d(TextureSampleType::Float { filterable: true }), //sky view lut and sampler
81-
sampler(SamplerBindingType::Filtering),
82-
texture_3d(TextureSampleType::Float { filterable: true }), //aerial view lut ans sampler
83-
sampler(SamplerBindingType::Filtering),
84-
texture_storage_2d_array(
85-
// output 2D array texture
86-
TextureFormat::Rgba16Float,
87-
StorageTextureAccess::WriteOnly,
71+
(0, uniform_buffer::<GpuAtmosphere>(true)),
72+
(1, uniform_buffer::<GpuAtmosphereSettings>(true)),
73+
(2, uniform_buffer::<AtmosphereTransform>(true)),
74+
(3, uniform_buffer::<ViewUniform>(true)),
75+
(4, uniform_buffer::<GpuLights>(true)),
76+
//transmittance lut and sampler
77+
(8, texture_2d(FILTERED_TEX)),
78+
(9, sampler(FILTERED_SMP)),
79+
//multiscattering lut and sampler
80+
(10, texture_2d(FILTERED_TEX)),
81+
(11, sampler(FILTERED_SMP)),
82+
//sky view lut and sampler
83+
(12, texture_2d(FILTERED_TEX)),
84+
(13, sampler(FILTERED_SMP)),
85+
//aerial view lut ans sampler
86+
(14, texture_3d(FILTERED_TEX)),
87+
(15, sampler(FILTERED_SMP)),
88+
// output 2D array texture
89+
(
90+
16,
91+
texture_storage_2d_array(
92+
TextureFormat::Rgba16Float,
93+
StorageTextureAccess::WriteOnly,
94+
),
8895
),
8996
),
9097
),
@@ -101,29 +108,29 @@ pub(super) fn prepare_atmosphere_probe_bind_groups(
101108
view_uniforms: Res<ViewUniforms>,
102109
lights_uniforms: Res<LightMeta>,
103110
atmosphere_transforms: Res<AtmosphereTransforms>,
104-
atmosphere_uniforms: Res<ComponentUniforms<Atmosphere>>,
111+
atmosphere_uniforms: Res<ComponentUniforms<GpuAtmosphere>>,
105112
settings_uniforms: Res<ComponentUniforms<GpuAtmosphereSettings>>,
106113
mut commands: Commands,
107114
) {
108115
for (entity, textures) in &probes {
109116
let environment = render_device.create_bind_group(
110117
"environment_bind_group",
111118
&layouts.environment,
112-
&BindGroupEntries::sequential((
113-
atmosphere_uniforms.binding().unwrap(),
114-
settings_uniforms.binding().unwrap(),
115-
atmosphere_transforms.uniforms().binding().unwrap(),
116-
view_uniforms.uniforms.binding().unwrap(),
117-
lights_uniforms.view_gpu_lights.binding().unwrap(),
118-
&textures.transmittance_lut.default_view,
119-
&samplers.transmittance_lut,
120-
&textures.multiscattering_lut.default_view,
121-
&samplers.multiscattering_lut,
122-
&textures.sky_view_lut.default_view,
123-
&samplers.sky_view_lut,
124-
&textures.aerial_view_lut.default_view,
125-
&samplers.aerial_view_lut,
126-
&textures.environment,
119+
&BindGroupEntries::with_indices((
120+
(0, atmosphere_uniforms.binding().unwrap()),
121+
(1, settings_uniforms.binding().unwrap()),
122+
(2, atmosphere_transforms.uniforms().binding().unwrap()),
123+
(3, view_uniforms.uniforms.binding().unwrap()),
124+
(4, lights_uniforms.view_gpu_lights.binding().unwrap()),
125+
(8, &textures.transmittance_lut.default_view),
126+
(9, &samplers.transmittance_lut),
127+
(10, &textures.multiscattering_lut.default_view),
128+
(11, &samplers.multiscattering_lut),
129+
(12, &textures.sky_view_lut.default_view),
130+
(13, &samplers.sky_view_lut),
131+
(14, &textures.aerial_view_lut.default_view),
132+
(15, &samplers.aerial_view_lut),
133+
(16, &textures.environment),
127134
)),
128135
);
129136

@@ -134,7 +141,7 @@ pub(super) fn prepare_atmosphere_probe_bind_groups(
134141
}
135142

136143
pub(super) fn prepare_probe_textures(
137-
view_textures: Query<&AtmosphereTextures, With<Atmosphere>>,
144+
view_textures: Query<&AtmosphereTextures, With<ExtractedAtmosphere>>,
138145
probes: Query<
139146
(Entity, &AtmosphereEnvironmentMap),
140147
(
@@ -245,7 +252,7 @@ pub fn prepare_atmosphere_probe_components(
245252

246253
pub(super) struct EnvironmentNode {
247254
main_view_query: QueryState<(
248-
Read<DynamicUniformIndex<Atmosphere>>,
255+
Read<DynamicUniformIndex<GpuAtmosphere>>,
249256
Read<DynamicUniformIndex<GpuAtmosphereSettings>>,
250257
Read<AtmosphereTransformsOffset>,
251258
Read<ViewUniformOffset>,

crates/bevy_pbr/src/atmosphere/environment.wgsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
utils::sample_cube_dir
66
}
77

8-
@group(0) @binding(13) var output: texture_storage_2d_array<rgba16float, write>;
8+
@group(0) @binding(16) var output: texture_storage_2d_array<rgba16float, write>;
99

1010
@compute @workgroup_size(8, 8, 1)
1111
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
@@ -36,4 +36,4 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
3636
let color = vec4<f32>(inscattering, 1.0);
3737

3838
textureStore(output, vec2<i32>(global_id.xy), i32(slice_index), color);
39-
}
39+
}

0 commit comments

Comments
 (0)