Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two storage buffer bindings point to the same buffer #5367

Closed
robostep9829 opened this issue Mar 9, 2024 · 1 comment
Closed

Two storage buffer bindings point to the same buffer #5367

robostep9829 opened this issue Mar 9, 2024 · 1 comment

Comments

@robostep9829
Copy link

robostep9829 commented Mar 9, 2024

Description
I'm trying to access vertex and index buffers from fragment shader. I tried to get vertex world position without getting data from vertex shader, only using acceleration structure. For that i needed to get primitive id by using RayQuery, then get vertex id based on primitive id and then get position of that vertex. So i added two buffer bindings to my render pipeline - one for indices and another for vertices. They are pointers to index and vertex buffers respectively.
Honorable mention: #1040. I think this is relevant, because i'm trying to replicate some concepts from vk_mini_path_tracer (See ./checkpoints/11_specular/. The shader makes use of two storage buffers which are pointing to vertex and index buffers).

Repro steps
This is probably runnable:
https://gitlab.com/robostep9829/hal-path-tracing-rs/-/tree/ssbo-error
See ./shaders/shader.frag, ./src/main.rs and ./src/resources.rs

Expected vs observed behavior
I noticed that no matter what value of vertex buffer i'm trying to access - it's always zero, however index values are proper and indbuf[rayQueryGetIntersectionPrimitiveIndexEXT(rq, true)] returns correct vertex number. I investigated further and found out that layout(set = 0, binding = 2) buffer ind_buf { uint indbuf[]; }; and layout(set = 0, binding = 3) buffer vtx_buf { Vertex vtxbuf[]; }; point to the same address (see nsight screenshot). In my program it is stated like this:

&wgpu_hal::BindGroupLayoutDescriptor {
                    label: Some("Bind Group layout"),
                    flags: wgpu_hal::BindGroupLayoutFlags::empty(),
                    entries: &[
                        wgt::BindGroupLayoutEntry {
                            binding: 0,
                            visibility: wgt::ShaderStages::FRAGMENT,
                            ty: wgt::BindingType::Texture {
                                sample_type: wgt::TextureSampleType::Float { filterable: true },
                                view_dimension: wgt::TextureViewDimension::D2,
                                multisampled: false,
                            },
                            count: None,
                        },
                        wgt::BindGroupLayoutEntry {
                            binding: 1,
                            visibility: wgt::ShaderStages::FRAGMENT,
                            ty: wgt::BindingType::Sampler(wgt::SamplerBindingType::Filtering),
                            count: None,
                        },
                        wgt::BindGroupLayoutEntry{
                            binding: 2,
                            visibility: wgt::ShaderStages::FRAGMENT,
                            ty: wgt::BindingType::Buffer {
                                ty: wgt::BufferBindingType::Storage { read_only: true },
                                has_dynamic_offset: false,
                                min_binding_size: None,
                            },
                            count: None,
                        },
                        wgt::BindGroupLayoutEntry{
                            binding: 3,
                            visibility: wgt::ShaderStages::FRAGMENT,
                            ty: wgt::BindingType::Buffer {
                                ty: wgt::BufferBindingType::Storage { read_only: true },
                                has_dynamic_offset: false,
                                min_binding_size: None,
                            },
                            count: None,
                        }
                    ],
                }

&wgpu_hal::BindGroupDescriptor {
                    layout,
                    buffers: &[
                        wgpu_hal::BufferBinding {
                            buffer: &index_buffer,
                            offset: 0,
                            size: None,
                        },
                        wgpu_hal::BufferBinding {
                            buffer: &vertex_buffer,
                            offset: 0,
                            size: None,
                        },
                    ],
                    samplers: &[&diffuse_texture.sampler],
                    textures: &[wgpu_hal::TextureBinding {
                        view: &diffuse_texture.view,
                        usage: wgpu_hal::TextureUses::RESOURCE,
                    }],
                    entries: &[
                        wgpu_hal::BindGroupEntry {
                            binding: 0,
                            resource_index: 0,
                            count: 1,
                        },
                        wgpu_hal::BindGroupEntry {
                            binding: 1,
                            resource_index: 0,
                            count: 1,
                        },
                        wgpu_hal::BindGroupEntry {
                            binding: 2,
                            resource_index: 0,
                            count: 1,
                        },
                        wgpu_hal::BindGroupEntry {
                            binding: 3,
                            resource_index: 0,
                            count: 1,
                        }
                    ],
                    label: Some(&format!("{} Bind group", m.name)),
                    acceleration_structures: &[],
                }

Extra materials
image
image
Here we can see that ind_buf and vtx_buf are the same buffer

Graphics-wgpu__2024_03_09__19_25_05.zip

Platform
OS: Windows 11 22H2, GPU: Radeon RX 6600, IDE: RustRover 233.14015.155, Vulkan SDK: 1.3.275.0, Rust: 1.71-msvc.

@robostep9829
Copy link
Author

Sorry for congestion. The solution was to change resource_index in the fourth BindGroupEntry. Is it worth documenting? Like if you want to bind multiple instances of the same binding type you should mark them with respective resource index.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant