From 322168c778ed026a31d76df20dadb6eef308746b Mon Sep 17 00:00:00 2001 From: Douglas Dwyer Date: Sun, 25 Oct 2026 08:33:50 -0400 Subject: [PATCH] fix(wgpu): Make TextureBlitter respect sample_type when downsampling (#10657) Co-authored-by: Inner Daemons <85136135+inner-daemons@users.noreply.github.com> --- CHANGELOG.md | 1 + tests/tests/wgpu-gpu/texture_blit.rs | 95 +++++++++++++++++++++++++++- wgpu/src/util/blit.wgsl | 6 +- wgpu/src/util/texture_blitter.rs | 1 + 4 files changed, 101 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cc54ae240..feacdd2e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/tests/tests/wgpu-gpu/texture_blit.rs b/tests/tests/wgpu-gpu/texture_blit.rs index 11d9792ea..e6555e650 100644 --- a/tests/tests/wgpu-gpu/texture_blit.rs +++ b/tests/tests/wgpu-gpu/texture_blit.rs @@ -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) { 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}" + ); + }); diff --git a/wgpu/src/util/blit.wgsl b/wgpu/src/util/blit.wgsl index c73845c2a..ed877360e 100644 --- a/wgpu/src/util/blit.wgsl +++ b/wgpu/src/util/blit.wgsl @@ -26,5 +26,9 @@ var texture_sampler: sampler; @fragment fn fs_main(vs: VertexOutput) -> @location(0) vec4 { - 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); } \ No newline at end of file diff --git a/wgpu/src/util/texture_blitter.rs b/wgpu/src/util/texture_blitter.rs index c4bd3348c..fcf083be7 100644 --- a/wgpu/src/util/texture_blitter.rs +++ b/wgpu/src/util/texture_blitter.rs @@ -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() });