EmbarkStudios / rust-gpu

🐉 Making Rust a first-class language and ecosystem for GPU shaders 🚧

Home Page:https://shader.rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

For loop not supported by Naga

Kethku opened this issue · comments

Expected Behaviour

Disabling spirt should not fix runtime errors.

Example & Steps To Reproduce

Compiling the following:

#[spirv(fragment)]
pub fn fragment(
    #[spirv(storage_buffer, descriptor_set = 0, binding = 0)] quads: &[InstancedQuad],
    #[spirv(descriptor_set = 1, binding = 0)] surface: &Image2d,
    #[spirv(descriptor_set = 1, binding = 1)] sampler: &Sampler,
    #[spirv(push_constant)] constants: &ShaderConstants,
    #[spirv(flat)] instance_index: i32,
    #[spirv(frag_coord)] surface_position: Vec4,
    out_color: &mut Vec4,
) {
    let quad = quads[instance_index as usize];

    if quad.blur != 0 {
        for y in -quad.blur..(quad.blur + 1) {
            for x in -quad.blur..(quad.blur + 1) {
                let offset = vec2(x as f32, y as f32);
                let sample_pos = (surface_position.xy() + offset) / constants.surface_size;
                let sample = surface.sample_by_lod(*sampler, sample_pos, 0.);
                *out_color += sample / (quad.blur * quad.blur) as f32;
            }
        }

        *out_color = *out_color * quad.color;
    } else {
        *out_color = quad.color;
    }
}

Causes a panic at runtime in the naga crate:

thread 'main' panicked at 'assertion failed: prev.is_none()', C:\Users\KaySimmons\.cargo\git\checkouts\naga-dbb2b19faed4
9210\f59668c\src\front\spv\mod.rs:3151:25

Adding the --no-spirt compile arg fixes this issue.

This issue upstream seems relevant: gfx-rs/wgpu#4388

System Info

  • Rust: rustc 1.68.0-nightly (5ce39f42b 2023-01-20)
  • OS: Windows 11
  • GPU: Intel(R) UHD Graphics
  • SPIR-V: SPIRV-Tools v2023.2 v2022.4-135-g44d72a9b

Rewriting the for loop as a loop with break, or while loop doesn't fix the issue. Similarly doing a single iteration without a loop works fine, so the loop itself seems to be the issue when using spirt

See these links for more details:

Assuming you're using Naga 0.11, this may work:

[patch.crates-io]
naga = { git = "https://github.com/EmbarkStudios/naga", branch = "spv-in-break-if-v0.11.0" }

This is 100% a Naga bug (they're not implementing a legal form of SPIR-V control-flow) and will be fixed independently of Rust-GPU.

Sweet! Thank you for the details.