From 5c1748559d4e8be65e3e2df8a0d23ecc85af9a77 Mon Sep 17 00:00:00 2001 From: Alex Egger <237841395+AlexEgger2Build@users.noreply.github.com> Date: Sat, 5 Sep 2026 04:45:28 -0500 Subject: [PATCH] fix(hal/gles): copy every depth slice in copy_texture_to_texture (#10004) Co-authored-by: Andreas Reich --- CHANGELOG.md | 1 + tests/tests/wgpu-gpu/transfer.rs | 115 ++++++++++++++++++++++++++++++- wgpu-hal/src/gles/queue.rs | 106 ++++++++++++++-------------- 3 files changed, 170 insertions(+), 52 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 11c851733..1702c9c30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -210,6 +210,7 @@ By @sagudev in [#10109](https://github.com/gfx-rs/wgpu/pull/10109). - `@interpolate(linear)` is now rejected by `Device::create_shader_module` on adapters that lack `DownlevelFlags::LINEAR_INTERPOLATION` (GLES/WebGL2), with a shader label and a source span. Previously such a shader validated fine and then failed at pipeline creation with `The selected version doesn't support Features(NOPERSPECTIVE_QUALIFIER)`. By @emilk in [#9972](https://github.com/gfx-rs/wgpu/pull/9972). - Fixed signed integer `%` (and `%=`) returning the wrong result for negative operands in the GLSL (OpenGL/GLES) backend, e.g. `-1 % 768` yielding `255` instead of `-1`. GLSL's `%` is undefined when either operand is negative, so signed remainder is now lowered as `a - b * (a / b)`, matching the SPIR-V, HLSL, and Metal backends. By @mstampfli in [#9687](https://github.com/gfx-rs/wgpu/pull/9687). - Fix negative argument for `atomicSub` yielding incorrect GLSL. By @ErichDonGubler in [#9924](https://github.com/gfx-rs/wgpu/pull/9924). +- Fixed `copy_texture_to_texture()` copying only the first depth slice when the copy extent had a depth greater than 1, so copies between 3D textures silently lost every slice after the first. Copies with a nonzero source z also read the wrong slice. By @AlexEgger2Build in [#10004](https://github.com/gfx-rs/wgpu/pull/10004). #### WebGPU diff --git a/tests/tests/wgpu-gpu/transfer.rs b/tests/tests/wgpu-gpu/transfer.rs index bd23328f2..b3cba0ad7 100644 --- a/tests/tests/wgpu-gpu/transfer.rs +++ b/tests/tests/wgpu-gpu/transfer.rs @@ -1,7 +1,120 @@ -use wgpu_test::{apply, fail, gpu_test, GpuTestConfiguration, GpuTestInitializer, TestParameters}; +use wgpu::util::DeviceExt; +use wgpu_test::{ + apply, fail, gpu_test, image::ReadbackBuffers, GpuTestConfiguration, GpuTestInitializer, + TestParameters, +}; pub fn all_tests(vec: &mut Vec) { vec.push(COPY_OVERFLOW_Z); + vec.push(COPY_TEXTURE_TO_TEXTURE_3D); +} + +#[apply(gpu_test!)] +static COPY_TEXTURE_TO_TEXTURE_3D: GpuTestConfiguration = + GpuTestConfiguration::new().run_async(copy_texture_to_texture_3d); + +async fn copy_texture_to_texture_3d(ctx: wgpu_test::TestingContext) { + struct Case { + name: &'static str, + src_origin: wgpu::Origin3d, + dst_origin: wgpu::Origin3d, + size: wgpu::Extent3d, + } + + const TEXTURE_SIZE: wgpu::Extent3d = wgpu::Extent3d { + width: 7, + height: 5, + depth_or_array_layers: 6, + }; + + let cases = [ + Case { + name: "full texture", + src_origin: wgpu::Origin3d::ZERO, + dst_origin: wgpu::Origin3d::ZERO, + size: TEXTURE_SIZE, + }, + Case { + name: "depth subrange", + src_origin: wgpu::Origin3d { x: 0, y: 0, z: 1 }, + dst_origin: wgpu::Origin3d { x: 0, y: 0, z: 2 }, + size: wgpu::Extent3d { + width: 7, + height: 5, + depth_or_array_layers: 4, + }, + }, + Case { + name: "offset subvolume", + src_origin: wgpu::Origin3d { x: 1, y: 1, z: 2 }, + dst_origin: wgpu::Origin3d { x: 3, y: 0, z: 1 }, + size: wgpu::Extent3d { + width: 3, + height: 3, + depth_or_array_layers: 2, + }, + }, + ]; + + let descriptor = wgpu::TextureDescriptor { + label: None, + dimension: wgpu::TextureDimension::D3, + size: TEXTURE_SIZE, + format: wgpu::TextureFormat::R8Uint, + usage: wgpu::TextureUsages::COPY_DST | wgpu::TextureUsages::COPY_SRC, + mip_level_count: 1, + sample_count: 1, + view_formats: &[], + }; + let texel_count = + (TEXTURE_SIZE.width * TEXTURE_SIZE.height * TEXTURE_SIZE.depth_or_array_layers) as usize; + let src_data: Vec = (1..=texel_count).map(|value| value as u8).collect(); + let src = ctx.device.create_texture_with_data( + &ctx.queue, + &descriptor, + wgpu::util::TextureDataOrder::LayerMajor, + &src_data, + ); + let index = |origin: wgpu::Origin3d, z, y| { + (((origin.z + z) * TEXTURE_SIZE.height + origin.y + y) * TEXTURE_SIZE.width + origin.x) + as usize + }; + + for case in cases { + let mut expected = vec![0; texel_count]; + for z in 0..case.size.depth_or_array_layers { + for y in 0..case.size.height { + let src_start = index(case.src_origin, z, y); + let dst_start = index(case.dst_origin, z, y); + let width = case.size.width as usize; + expected[dst_start..dst_start + width] + .copy_from_slice(&src_data[src_start..src_start + width]); + } + } + + let dst = ctx.device.create_texture(&descriptor); + let mut encoder = ctx + .device + .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None }); + encoder.copy_texture_to_texture( + wgpu::TexelCopyTextureInfo { + origin: case.src_origin, + ..src.as_image_copy() + }, + wgpu::TexelCopyTextureInfo { + origin: case.dst_origin, + ..dst.as_image_copy() + }, + case.size, + ); + + let readback = ReadbackBuffers::new(&ctx.device, &dst); + readback.copy_from(&ctx.device, &mut encoder, &dst); + ctx.queue.submit(Some(encoder.finish())); + + let actual = readback.retrieve(&ctx, wgpu::TextureAspect::All).await; + assert_eq!(actual.all(), expected, "{}", case.name); + } } #[apply(gpu_test!)] diff --git a/wgpu-hal/src/gles/queue.rs b/wgpu-hal/src/gles/queue.rs index 17287b2c7..6d6a95730 100644 --- a/wgpu-hal/src/gles/queue.rs +++ b/wgpu-hal/src/gles/queue.rs @@ -723,59 +723,63 @@ impl super::Queue { dst_target, ref copy, } => { - //TODO: handle 3D copies unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, Some(self.copy_fbo)) }; - if is_layered_target(src_target) { - //TODO: handle GLES without framebuffer_texture_3d - unsafe { - gl.framebuffer_texture_layer( - glow::READ_FRAMEBUFFER, - glow::COLOR_ATTACHMENT0, - Some(src), - copy.src_base.mip_level as i32, - copy.src_base.array_layer as i32, - ) - }; - } else { - unsafe { - gl.framebuffer_texture_2d( - glow::READ_FRAMEBUFFER, - glow::COLOR_ATTACHMENT0, - src_target, - Some(src), - copy.src_base.mip_level as i32, - ) - }; - } - unsafe { gl.bind_texture(dst_target, Some(dst)) }; - if is_layered_target(dst_target) { - unsafe { - gl.copy_tex_sub_image_3d( - dst_target, - copy.dst_base.mip_level as i32, - copy.dst_base.origin.x as i32, - copy.dst_base.origin.y as i32, - get_z_offset(dst_target, ©.dst_base) as i32, - copy.src_base.origin.x as i32, - copy.src_base.origin.y as i32, - copy.size.width as i32, - copy.size.height as i32, - ) - }; - } else { - unsafe { - gl.copy_tex_sub_image_2d( - get_2d_target(dst_target, copy.dst_base.array_layer), - copy.dst_base.mip_level as i32, - copy.dst_base.origin.x as i32, - copy.dst_base.origin.y as i32, - copy.src_base.origin.x as i32, - copy.src_base.origin.y as i32, - copy.size.width as i32, - copy.size.height as i32, - ) - }; + + // The read framebuffer holds a single 2D slice at a time, so a copy of + // depth > 1 is issued one slice per iteration. + for z in 0..copy.size.depth { + if is_layered_target(src_target) { + //TODO: handle GLES without framebuffer_texture_3d + unsafe { + gl.framebuffer_texture_layer( + glow::READ_FRAMEBUFFER, + glow::COLOR_ATTACHMENT0, + Some(src), + copy.src_base.mip_level as i32, + (get_z_offset(src_target, ©.src_base) + z) as i32, + ) + }; + } else { + unsafe { + gl.framebuffer_texture_2d( + glow::READ_FRAMEBUFFER, + glow::COLOR_ATTACHMENT0, + get_2d_target(src_target, copy.src_base.array_layer + z), + Some(src), + copy.src_base.mip_level as i32, + ) + }; + } + + if is_layered_target(dst_target) { + unsafe { + gl.copy_tex_sub_image_3d( + dst_target, + copy.dst_base.mip_level as i32, + copy.dst_base.origin.x as i32, + copy.dst_base.origin.y as i32, + (get_z_offset(dst_target, ©.dst_base) + z) as i32, + copy.src_base.origin.x as i32, + copy.src_base.origin.y as i32, + copy.size.width as i32, + copy.size.height as i32, + ) + }; + } else { + unsafe { + gl.copy_tex_sub_image_2d( + get_2d_target(dst_target, copy.dst_base.array_layer + z), + copy.dst_base.mip_level as i32, + copy.dst_base.origin.x as i32, + copy.dst_base.origin.y as i32, + copy.src_base.origin.x as i32, + copy.src_base.origin.y as i32, + copy.size.width as i32, + copy.size.height as i32, + ) + }; + } } } C::CopyBufferToTexture {