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](628a95cd1c/wgpu-hal/src/gles/device.rs (L1038))

---

---------

Co-authored-by: Wilhelm Vallrand <>
This commit is contained in:
Wilhelm Vallrand 2023-05-19 23:11:41 +03:00 committed by GitHub
parent a75634ddc7
commit f76b3c4230
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 6 deletions

View file

@ -274,7 +274,7 @@ path = "examples/2d/bloom_2d.rs"
name = "2D Bloom" name = "2D Bloom"
description = "Illustrates bloom post-processing in 2d" description = "Illustrates bloom post-processing in 2d"
category = "2D Rendering" category = "2D Rendering"
wasm = false wasm = true
[[example]] [[example]]
name = "move_sprite" name = "move_sprite"
@ -535,7 +535,7 @@ path = "examples/3d/bloom_3d.rs"
name = "3D Bloom" name = "3D Bloom"
description = "Illustrates bloom configuration using HDR and emissive materials" description = "Illustrates bloom configuration using HDR and emissive materials"
category = "3D Rendering" category = "3D Rendering"
wasm = false wasm = true
[[example]] [[example]]
name = "load_gltf" name = "load_gltf"

View file

@ -302,11 +302,16 @@ impl ViewNode for BloomNode {
#[derive(Component)] #[derive(Component)]
struct BloomTexture { struct BloomTexture {
// First mip is half the screen resolution, successive mips are half the previous // First mip is half the screen resolution, successive mips are half the previous
#[cfg(any(not(feature = "webgl"), not(target_arch = "wasm32")))]
texture: CachedTexture, 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, mip_count: u32,
} }
impl BloomTexture { impl BloomTexture {
#[cfg(any(not(feature = "webgl"), not(target_arch = "wasm32")))]
fn view(&self, base_mip_level: u32) -> TextureView { fn view(&self, base_mip_level: u32) -> TextureView {
self.texture.texture.create_view(&TextureViewDescriptor { self.texture.texture.create_view(&TextureViewDescriptor {
base_mip_level, base_mip_level,
@ -314,6 +319,16 @@ impl BloomTexture {
..Default::default() ..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( fn prepare_bloom_textures(
@ -347,10 +362,29 @@ fn prepare_bloom_textures(
view_formats: &[], view_formats: &[],
}; };
commands.entity(entity).insert(BloomTexture { #[cfg(any(not(feature = "webgl"), not(target_arch = "wasm32")))]
texture: texture_cache.get(&render_device, texture_descriptor), let texture = texture_cache.get(&render_device, texture_descriptor);
mip_count, #[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 });
} }
} }
} }