From f76b3c4230e890e3cdeb01c26809cf4bde8d26f4 Mon Sep 17 00:00:00 2001 From: Wilhelm Vallrand <44950746+vallrand@users.noreply.github.com> Date: Fri, 19 May 2023 23:11:41 +0300 Subject: [PATCH] Fix bloom wasm support (#8631) # Objective - Fixes #7352 ## Solution GLES doesn't support binding specific mip levels for sampling. Fallback to using separate textures instead. - [wgpu-hal/src/gles/device.rs](https://github.com/gfx-rs/wgpu/blob/628a95cd1c27abce4d4ff2ffe4558cc4ef1e7fd3/wgpu-hal/src/gles/device.rs#L1038) --- --------- Co-authored-by: Wilhelm Vallrand <> --- Cargo.toml | 4 +-- crates/bevy_core_pipeline/src/bloom/mod.rs | 42 +++++++++++++++++++--- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0e1b5d9f33..7698dc2ec8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" @@ -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" diff --git a/crates/bevy_core_pipeline/src/bloom/mod.rs b/crates/bevy_core_pipeline/src/bloom/mod.rs index 7157bc449e..3c41ca716e 100644 --- a/crates/bevy_core_pipeline/src/bloom/mod.rs +++ b/crates/bevy_core_pipeline/src/bloom/mod.rs @@ -302,11 +302,16 @@ 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, 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, @@ -314,6 +319,16 @@ impl BloomTexture { ..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( @@ -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 = (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 }); } } }