fix(wgpu): Make TextureBlitter respect sample_type when downsampling (#10657)

Co-authored-by: Inner Daemons <85136135+inner-daemons@users.noreply.github.com>
This commit is contained in:
Douglas DwyerandInner Daemons authored and GitHub committed 2026-10-25 12:33:50 +00:00
1 parent 2edafa5240
commit 322168c778
4 files changed
+101 -2

No files matched your search

+1
View File
@@ -224,6 +224,7 @@ By @sagudev in [#10307](https://github.com/gfx-rs/wgpu/pull/10307).
- Clean up resources properly when `Device::new` fails, to avoid a leak or panic. By @andyleiserson in [#10160](https://github.com/gfx-rs/wgpu/pull/10160).
- Fix pending buffer mappings incorrectly succeeding when the device is lost before they are processed. By @jinleili in [#10301](https://github.com/gfx-rs/wgpu/pull/10301).
- Fix initialization tracking for `external_texture` binding points, for both `ExternalTexture`s and `TextureView`s bound to them. By @ErichDonGubler in [#10276](https://github.com/gfx-rs/wgpu/pull/10276) and [#10366](https://github.com/gfx-rs/wgpu/pull/10366).
- Make `TextureBlitter` respect the `sample_type` when performing downsampling. Previously, `FilterMode::Nearest` was always used when copying a larger texture to a smaller one. By @DouglasDwyer in [#10657](https://github.com/gfx-rs/wgpu/pull/10657)
#### naga
+94 -1
View File
@@ -1,9 +1,14 @@
use wgpu_test::{apply, gpu_test, GpuTestConfiguration, GpuTestInitializer, TestParameters};
use wgpu_test::{
apply, gpu_test, image::ReadbackBuffers, GpuTestConfiguration, GpuTestInitializer,
TestParameters, TestingContext,
};
pub fn all_tests(vec: &mut Vec<GpuTestInitializer>) {
vec.extend([
TEXTURE_BLIT_WITH_LINEAR_FILTER_TEST,
TEXTURE_BLIT_WITH_NEAREST_FILTER_TEST,
TEXTURE_BLIT_DOWNSAMPLE_LINEAR_TEST,
TEXTURE_BLIT_DOWNSAMPLE_NEAREST_TEST,
]);
}
@@ -111,3 +116,91 @@ static TEXTURE_BLIT_WITH_NEAREST_FILTER_TEST: GpuTestConfiguration = GpuTestConf
&target.create_view(&wgpu::TextureViewDescriptor::default()),
);
});
/// Blits a 2x2 texture with a black left column and a white right column into a
/// 1x1 texture, and returns the red channel of the result.
async fn downsample_red_channel(ctx: &TestingContext, filter: wgpu::FilterMode) -> u8 {
let source = ctx.device.create_texture(&wgpu::TextureDescriptor {
label: None,
size: wgpu::Extent3d {
width: 2,
height: 2,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8Unorm,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
view_formats: &[],
});
let column = [[0, 0, 0, 255], [255, 255, 255, 255]];
let texels = [column, column].concat().concat();
ctx.queue.write_texture(
source.as_image_copy(),
&texels,
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(8),
rows_per_image: None,
},
source.size(),
);
let target = ctx.device.create_texture(&wgpu::TextureDescriptor {
label: None,
size: wgpu::Extent3d {
width: 1,
height: 1,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8Unorm,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC,
view_formats: &[],
});
let blitter =
wgpu::util::TextureBlitterBuilder::new(&ctx.device, wgpu::TextureFormat::Rgba8Unorm)
.sample_type(filter)
.build();
let readback = ReadbackBuffers::new(&ctx.device, &target);
let mut encoder = ctx
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor::default());
blitter.copy(
&ctx.device,
&mut encoder,
&source.create_view(&wgpu::TextureViewDescriptor::default()),
&target.create_view(&wgpu::TextureViewDescriptor::default()),
);
readback.copy_from(&ctx.device, &mut encoder, &target);
ctx.queue.submit(Some(encoder.finish()));
let data = readback.retrieve(ctx, wgpu::TextureAspect::All).await;
data.all()[0]
}
#[apply(gpu_test!)]
static TEXTURE_BLIT_DOWNSAMPLE_LINEAR_TEST: GpuTestConfiguration = GpuTestConfiguration::new()
.run_async(|ctx| async move {
let red = downsample_red_channel(&ctx, wgpu::FilterMode::Linear).await;
assert!(
(120..=135).contains(&red),
"linear downsampling should blend the texels, got {red}"
);
});
#[apply(gpu_test!)]
static TEXTURE_BLIT_DOWNSAMPLE_NEAREST_TEST: GpuTestConfiguration = GpuTestConfiguration::new()
.run_async(|ctx| async move {
let red = downsample_red_channel(&ctx, wgpu::FilterMode::Nearest).await;
assert!(
red == 0 || red == 255,
"nearest downsampling should pick a single texel, got {red}"
);
});
+5 -1
View File
@@ -26,5 +26,9 @@ var texture_sampler: sampler;
@fragment
fn fs_main(vs: VertexOutput) -> @location(0) vec4<f32> {
return textureSample(texture, texture_sampler, vs.tex_coords);
// Note: we want to copy the texture with the user's provided filter mode,
// similar to vkCmdBlitImage. We don't want anything to do with mipmapping
// or partial derivatives, so use `textureSampleLevel` to read from the
// base texture with the mag filter
return textureSampleLevel(texture, texture_sampler, vs.tex_coords, 0.0);
}
+1
View File
@@ -57,6 +57,7 @@ impl<'a> TextureBlitterBuilder<'a> {
address_mode_v: AddressMode::ClampToEdge,
address_mode_w: AddressMode::ClampToEdge,
mag_filter: self.sample_type,
// Note: blitting employs `textureSampleLevel` which unconditionally uses mag_filter, so we don't need to set min_filter
..Default::default()
});