Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bloom wasm support #8631

Merged
merged 5 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ path = "examples/2d/bloom_2d.rs"
name = "2D Bloom"
description = "Illustrates bloom post-processing in 2d"
category = "2D Rendering"
wasm = false
wasm = true

[[example]]
name = "move_sprite"
Expand Down Expand Up @@ -535,7 +535,7 @@ path = "examples/3d/bloom_3d.rs"
name = "3D Bloom"
description = "Illustrates bloom configuration using HDR and emissive materials"
category = "3D Rendering"
wasm = false
wasm = true

[[example]]
name = "load_gltf"
Expand Down
42 changes: 38 additions & 4 deletions crates/bevy_core_pipeline/src/bloom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,18 +302,33 @@ impl ViewNode for BloomNode {
#[derive(Component)]
struct BloomTexture {
// First mip is half the screen resolution, successive mips are half the previous
#[cfg(any(not(feature = "webgl"), not(target_arch = "wasm32")))]
texture: CachedTexture,
// WebGL does not support binding specific mip levels for sampling, fallback to separate textures instead
#[cfg(all(feature = "webgl", target_arch = "wasm32"))]
texture: Vec<CachedTexture>,
mip_count: u32,
}

impl BloomTexture {
#[cfg(any(not(feature = "webgl"), not(target_arch = "wasm32")))]
fn view(&self, base_mip_level: u32) -> TextureView {
self.texture.texture.create_view(&TextureViewDescriptor {
base_mip_level,
mip_level_count: Some(1u32),
..Default::default()
})
}
#[cfg(all(feature = "webgl", target_arch = "wasm32"))]
fn view(&self, base_mip_level: u32) -> TextureView {
self.texture[base_mip_level as usize]
.texture
.create_view(&TextureViewDescriptor {
base_mip_level: 0,
mip_level_count: Some(1u32),
..Default::default()
})
}
}

fn prepare_bloom_textures(
Expand Down Expand Up @@ -347,10 +362,29 @@ fn prepare_bloom_textures(
view_formats: &[],
};

commands.entity(entity).insert(BloomTexture {
texture: texture_cache.get(&render_device, texture_descriptor),
mip_count,
});
#[cfg(any(not(feature = "webgl"), not(target_arch = "wasm32")))]
let texture = texture_cache.get(&render_device, texture_descriptor);
#[cfg(all(feature = "webgl", target_arch = "wasm32"))]
let texture: Vec<CachedTexture> = (0..mip_count)
.map(|mip| {
texture_cache.get(
&render_device,
TextureDescriptor {
size: Extent3d {
width: (texture_descriptor.size.width >> mip).max(1),
height: (texture_descriptor.size.height >> mip).max(1),
depth_or_array_layers: 1,
},
mip_level_count: 1,
..texture_descriptor.clone()
},
)
})
.collect();

commands
.entity(entity)
.insert(BloomTexture { texture, mip_count });
}
}
}
Expand Down