Skip to content

Commit 5c6b9db

Browse files
committed
fixed stuff but big memory usage
1 parent d84604c commit 5c6b9db

File tree

5 files changed

+24
-71
lines changed

5 files changed

+24
-71
lines changed

crates/bevy_pbr/src/atmosphere/functions.wgsl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,15 @@ fn sample_aerial_view_lut(uv: vec2<f32>, t: f32) -> vec3<f32> {
182182
// ATMOSPHERE SAMPLING
183183

184184
fn sample_density_lut(r: f32, component: f32) -> vec3<f32> {
185-
let normed_altitude = clamp((r - atmosphere.bottom_radius) / (atmosphere.top_radius - atmosphere.bottom_radius), 0.0, 1.0);
185+
let normed_altitude = saturate((r - atmosphere.bottom_radius) / (atmosphere.top_radius - atmosphere.bottom_radius));
186186
let uv = vec2(1.0 - normed_altitude, component);
187-
return atmosphere.density_max * textureSampleLevel(medium_density_lut, medium_sampler, uv, 0.0).xyz;
187+
return textureSampleLevel(medium_density_lut, medium_sampler, uv, 0.0).xyz;
188188
}
189189

190190
fn sample_scattering_lut(r: f32, neg_LdotV: f32) -> vec3<f32> {
191-
let normed_altitude = clamp((r - atmosphere.bottom_radius) / (atmosphere.top_radius - atmosphere.bottom_radius), 0.0, 1.0);
191+
let normed_altitude = saturate((r - atmosphere.bottom_radius) / (atmosphere.top_radius - atmosphere.bottom_radius));
192192
let uv = vec2(1.0 - normed_altitude, neg_LdotV * 0.5 + 0.5);
193-
return atmosphere.scattering_max * textureSampleLevel(medium_scattering_lut, medium_sampler, uv, 0.0).xyz;
193+
return textureSampleLevel(medium_scattering_lut, medium_sampler, uv, 0.0).xyz;
194194
}
195195

196196
/// evaluates L_scat, equation 3 in the paper, which gives the total single-order scattering towards the view at a single point

crates/bevy_pbr/src/atmosphere/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ use bevy_reflect::{std_traits::ReflectDefault, Reflect};
5555
use bevy_render::{
5656
extract_component::UniformComponentPlugin,
5757
render_resource::{DownlevelFlags, ShaderType, SpecializedRenderPipelines},
58-
sync_world::MainEntity,
5958
view::Hdr,
6059
RenderStartup,
6160
};
@@ -124,7 +123,7 @@ impl Plugin for AtmospherePlugin {
124123
.resource_mut::<Assets<ScatteringMedium>>()
125124
.add(ScatteringMedium::earth_atmosphere());
126125
world.insert_resource(EarthAtmosphere(Atmosphere {
127-
bottom_radius: 6_460_000.0,
126+
bottom_radius: 6_360_000.0,
128127
top_radius: 6_460_000.0,
129128
ground_albedo: Vec3::splat(0.3),
130129
medium: earth_atmosphere,

crates/bevy_pbr/src/atmosphere/resources.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -526,9 +526,7 @@ struct ScatteringMediumMissingError(AssetId<ScatteringMedium>);
526526
pub struct GpuAtmosphere {
527527
pub ground_albedo: Vec3,
528528
pub bottom_radius: f32,
529-
pub density_max: Vec3,
530529
pub top_radius: f32,
531-
pub scattering_max: Vec3,
532530
}
533531

534532
pub fn prepare_atmosphere_uniforms(
@@ -543,9 +541,7 @@ pub fn prepare_atmosphere_uniforms(
543541
commands.entity(entity).insert(GpuAtmosphere {
544542
ground_albedo: atmosphere.ground_albedo,
545543
bottom_radius: atmosphere.bottom_radius,
546-
density_max: gpu_medium.density_max,
547544
top_radius: atmosphere.top_radius,
548-
scattering_max: gpu_medium.scattering_max,
549545
});
550546
}
551547
Ok(())

crates/bevy_pbr/src/atmosphere/types.wgsl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ struct Atmosphere {
44
ground_albedo: vec3<f32>,
55
// Radius of the planet
66
bottom_radius: f32, // units: m
7-
density_max: vec3<f32>,
87
// Radius at which we consider the atmosphere to 'end' for out calculations (from center of planet)
98
top_radius: f32, // units: m
10-
scattering_max: vec3<f32>,
119
}
1210

1311
struct AtmosphereSettings {

crates/bevy_pbr/src/medium.rs

Lines changed: 19 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use bevy_ecs::{
1010
};
1111
use bevy_math::{
1212
curve::{EaseFunction, EasingCurve, Interval},
13-
ops, Curve, FloatPow, U8Vec3, Vec3, VectorSpace,
13+
ops, Curve, FloatPow, U8Vec3, Vec3, Vec4, VectorSpace,
1414
};
1515
use bevy_reflect::TypePath;
1616
use bevy_render::{
@@ -143,18 +143,14 @@ impl ScatteringMedium {
143143
ScatteringTerm {
144144
absorption: Vec3::ZERO,
145145
scattering: Vec3::new(5.802e-6, 13.558e-6, 33.100e-6),
146-
// falloff: Falloff::Exponential { scale: 12.5 }, //TODO: check if matches reference
147-
falloff: Falloff::Linear,
146+
falloff: Falloff::Exponential { scale: 12.5 }, //TODO: check if matches reference
148147
phase: PhaseFunction::Rayleigh,
149-
// phase: PhaseFunction::Isotropic,
150148
},
151149
ScatteringTerm {
152150
absorption: Vec3::splat(3.996e-6),
153151
scattering: Vec3::splat(0.444e-6),
154-
// falloff: Falloff::Exponential { scale: 83.5 }, //TODO: check if matches reference
155-
falloff: Falloff::Linear,
152+
falloff: Falloff::Exponential { scale: 83.5 }, //TODO: check if matches reference
156153
phase: PhaseFunction::Mie { bias: 0.8 },
157-
// phase: PhaseFunction::Isotropic,
158154
},
159155
ScatteringTerm {
160156
absorption: Vec3::new(0.650e-6, 1.881e-6, 0.085e-6),
@@ -264,10 +260,8 @@ pub struct GpuScatteringMedium {
264260
pub terms: SmallVec<[ScatteringTerm; 1]>,
265261
pub falloff_resolution: u32,
266262
pub phase_resolution: u32,
267-
pub density_max: Vec3,
268263
pub density_lut: Texture,
269264
pub density_lut_view: TextureView,
270-
pub scattering_max: Vec3,
271265
pub scattering_lut: Texture,
272266
pub scattering_lut_view: TextureView,
273267
}
@@ -283,55 +277,33 @@ impl RenderAsset for GpuScatteringMedium {
283277
(render_device, render_queue): &mut SystemParamItem<Self::Param>,
284278
_previous_asset: Option<&Self>,
285279
) -> Result<Self, PrepareAssetError<Self::SourceAsset>> {
286-
let mut density: Vec<Vec3> = Vec::with_capacity(source_asset.falloff_resolution as usize);
287-
let mut density_lut_data: Vec<u8> =
288-
Vec::with_capacity(2 * source_asset.falloff_resolution as usize * size_of::<u32>());
289-
290-
let mut density_max = Vec3::ZERO;
280+
let mut density: Vec<Vec4> =
281+
Vec::with_capacity(2 * source_asset.falloff_resolution as usize);
291282

292283
density.extend((0..source_asset.falloff_resolution).map(|i| {
293284
let falloff = (i as f32 + 0.5) / source_asset.falloff_resolution as f32;
294285

295-
let absorption = source_asset
286+
source_asset
296287
.terms
297288
.iter()
298-
.map(|term| term.absorption * term.falloff.sample(falloff))
299-
.sum();
300-
301-
density_max = density_max.max(absorption);
302-
absorption
289+
.map(|term| term.absorption.extend(0.0) * term.falloff.sample(falloff))
290+
.sum::<Vec4>()
303291
}));
304292

305293
density.extend((0..source_asset.falloff_resolution).map(|i| {
306294
let falloff = (i as f32 + 0.5) / source_asset.falloff_resolution as f32;
307295

308-
let scattering = source_asset
296+
source_asset
309297
.terms
310298
.iter()
311-
.map(|term| term.scattering * term.falloff.sample(falloff))
312-
.sum();
313-
314-
density_max = density_max.max(scattering);
315-
scattering
316-
}));
317-
318-
density_lut_data.extend(density.iter().flat_map(|absorption| {
319-
(*absorption * 255.0 / density_max)
320-
.as_u8vec3()
321-
.extend(0)
322-
.to_array()
299+
.map(|term| term.scattering.extend(0.0) * term.falloff.sample(falloff))
300+
.sum::<Vec4>()
323301
}));
324302

325-
let mut scattering: Vec<Vec3> = Vec::with_capacity(
303+
let mut scattering: Vec<Vec4> = Vec::with_capacity(
326304
source_asset.falloff_resolution as usize * source_asset.phase_resolution as usize,
327305
);
328-
let mut scattering_lut_data: Vec<u8> = Vec::with_capacity(
329-
source_asset.falloff_resolution as usize
330-
* source_asset.phase_resolution as usize
331-
* size_of::<u32>(),
332-
);
333306

334-
let mut scattering_max = Vec3::ZERO;
335307
scattering.extend(
336308
(0..source_asset.falloff_resolution * source_asset.phase_resolution).map(|raw_i| {
337309
let i = raw_i % source_asset.phase_resolution;
@@ -340,28 +312,18 @@ impl RenderAsset for GpuScatteringMedium {
340312
let phase = (j as f32 + 0.5) / source_asset.phase_resolution as f32;
341313
let neg_l_dot_v = phase * 2.0 - 1.0;
342314

343-
let scattering = source_asset
315+
source_asset
344316
.terms
345317
.iter()
346318
.map(|term| {
347-
term.scattering
319+
term.scattering.extend(0.0)
348320
* term.falloff.sample(falloff)
349321
* term.phase.sample(neg_l_dot_v)
350322
})
351-
.sum();
352-
353-
scattering_max = scattering_max.max(scattering);
354-
scattering
323+
.sum::<Vec4>()
355324
}),
356325
);
357326

358-
scattering_lut_data.extend(scattering.iter().flat_map(|scattering| {
359-
(*scattering * 255.0 / scattering_max)
360-
.as_u8vec3()
361-
.extend(0)
362-
.to_array()
363-
}));
364-
365327
let density_lut = render_device.create_texture_with_data(
366328
render_queue,
367329
&TextureDescriptor {
@@ -379,12 +341,12 @@ impl RenderAsset for GpuScatteringMedium {
379341
mip_level_count: 1,
380342
sample_count: 1,
381343
dimension: TextureDimension::D2,
382-
format: TextureFormat::Rgba8Unorm,
344+
format: TextureFormat::Rgba32Float,
383345
usage: TextureUsages::TEXTURE_BINDING,
384346
view_formats: &[],
385347
},
386348
TextureDataOrder::LayerMajor,
387-
density_lut_data.as_slice(),
349+
bytemuck::cast_slice(density.as_slice()),
388350
);
389351

390352
let density_lut_view = density_lut.create_view(&TextureViewDescriptor {
@@ -414,12 +376,12 @@ impl RenderAsset for GpuScatteringMedium {
414376
mip_level_count: 1,
415377
sample_count: 1,
416378
dimension: TextureDimension::D2,
417-
format: TextureFormat::Rgba8Unorm,
379+
format: TextureFormat::Rgba32Float,
418380
usage: TextureUsages::TEXTURE_BINDING,
419381
view_formats: &[],
420382
},
421383
TextureDataOrder::LayerMajor,
422-
scattering_lut_data.as_slice(),
384+
bytemuck::cast_slice(scattering.as_slice()),
423385
);
424386

425387
let scattering_lut_view = scattering_lut.create_view(&TextureViewDescriptor {
@@ -436,10 +398,8 @@ impl RenderAsset for GpuScatteringMedium {
436398
terms: source_asset.terms,
437399
falloff_resolution: source_asset.falloff_resolution,
438400
phase_resolution: source_asset.phase_resolution,
439-
density_max,
440401
density_lut,
441402
density_lut_view,
442-
scattering_max,
443403
scattering_lut,
444404
scattering_lut_view,
445405
})

0 commit comments

Comments
 (0)