gfx-rs / wgpu

A cross-platform, safe, pure-Rust graphics API.
https://wgpu.rs
Apache License 2.0
12.43k stars 908 forks source link

3d texture view is wrong in dx12 #5157

Open 2A5F opened 8 months ago

2A5F commented 8 months ago

Code

let texture1 = device
    .create_texture(&wgpu::TextureDescriptor {
        label: Some("Test Compute Texture 1"),
        size: wgpu::Extent3d {
            width: 16,
            height: 16,
            depth_or_array_layers: 16,
        },
        mip_level_count: 1,
        sample_count: 1,
        dimension: wgpu::TextureDimension::D3,
        format: wgpu::TextureFormat::Rgba32Uint,
        usage: wgpu::TextureUsages::COPY_SRC
            | wgpu::TextureUsages::COPY_DST
            | wgpu::TextureUsages::TEXTURE_BINDING
            | wgpu::TextureUsages::STORAGE_BINDING,
        view_formats: &[],
    });

let text_view = texture1.create_view(
    &wgpu::TextureViewDescriptor {
        label: Some("Test Compute Texture View 1"),
        ..Default::default()
    },
);
@group(0) @binding(0) var chunk: texture_storage_3d<rgba32uint, write>;

struct CsIn {
    @builtin(global_invocation_id) gid: vec3<u32>,
    @builtin(local_invocation_id) lid: vec3<u32>,
    @builtin(workgroup_id) wid: vec3<u32>,
}

@compute @workgroup_size(8,8,8)
fn test_fill(ci: CsIn) {
    let gid = ci.gid;
    let lid = ci.lid;
    textureStore(chunk, gid, vec4<u32>(gid, 1));
}

The texture is treated as 2d, causing only layer 0 to be written

image image image image

Platform

OS: win11 22h2 22621.3007
wgpu: 0.19

Other

teoxoy commented 8 months ago

It looks like we are passing the wrong parameters to D3D12_TEX3D_UAV.

https://github.com/gfx-rs/wgpu/blob/6e020a079e9a512a7465252c238c5a85b1177fc5/wgpu-hal/src/dx12/view.rs#L211-L212

We should be passing FirstWSlice: 0 and WSize: -1 as there isn't currently a way to specify a specific depth slice in our API.

ref: https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ns-d3d12-d3d12_tex3d_uav

ErichDonGubler commented 8 months ago

@teoxoy: Does this have a relation to https://github.com/gfx-rs/wgpu/issues/4764?

teoxoy commented 8 months ago

They are related but this one is strictly for UAVs (storage textures) and https://github.com/gfx-rs/wgpu/issues/4764 is concerned with RTVs (render attachments).

ori-sky commented 4 months ago

I'm encountering this issue and was wondering if there's anything I can do to mitigate it in the meantime while a fix is still pending? Would switching to a texture 2D array be sufficient?

teoxoy commented 4 months ago

Using a texture 2D array should work.