0

I am encountering a strange error in rustgpu. I had a shader that was compiling through a custom wrapper around rustgpu, but I recently switched to cargo gpu.

In that shader I have an array input that I use to initialize a voxel oct tree on the gpu, this is what it looks like:

#[spirv(fragment)]
pub fn main_fs(
    #[spirv(uniform, descriptor_set = 1, binding = 1)] window_data: &WindowData,
    #[spirv(uniform, descriptor_set = 1, binding = 2)] voctree_meta: &VoctreeMetadata,
    #[spirv(storage_buffer, descriptor_set = 1, binding = 4)] counter: &mut [u32; 1],
    #[spirv(storage_buffer, descriptor_set = 1, binding = 5)] nodes: &mut RuntimeArray<
        Node<SimpleNodeData>,
    >,
    #[spirv(frag_coord)] screen_pos: Vec4,
    out_color: &mut Vec4,
)
{
    let window_dimensions = window_data.dimensions.xy();
    let view_matrix = window_data.view_matrix;
    let screen_position = screen_pos.xy();

    let display_width = window_dimensions.x;
    let display_height = window_dimensions.y;
    let aspect_ratio = display_width / display_height;

    let x = screen_position.x * 2.0 / display_width - 1.0;
    let y = screen_position.y * 2.0 / display_height - 1.0;

    let screen_ray = generate_ray(&Vec2::new(x, y), aspect_ratio, 45.);
    let ray = (view_matrix.inverse() * screen_ray.extend(0.)).xyz();
    let cam_pos = (view_matrix.inverse() * Vec4::new(0., 0., 0., 1.)).xyz();

    let node_count = unsafe { counter.index_unchecked_mut(0_usize) };
    let voctree =
        GpuVoctree::<SimpleNodeData>::new(voctree_meta.clone(), node_count, nodes);

    let (cam_pos, ray) = create_ray_from_camera_data(
        &screen_pos.xy(),
        &window_data.dimensions.xy(),
        &window_data.view_matrix,
    );

    let (was_hit, hit_node, iter_count) = trace_svo(cam_pos, ray, &voctree);

    // let (was_hit, color) = trace_voxel_grid(cam_pos, ray, &volume_data, &volume);
    // *out_color = color.xyz().extend(1.0);

    if was_hit
    {
        *out_color = voctree.node_at(hit_node as usize).data.color;
    }
    else
    {
        *out_color = Vec3::splat(0.).extend(1.);
    }
}

I compile this shader via cargo gpu and then I am parsing its metadata through spirq. Which I have at the latest version.

According to spirq, there is no variable named counter in the emitted spirv code.

i.e. I iterate over the spriv code like this:

 let entry_list: Vec<_> = spirv_data
        .iter()
        .map(|spirv_data| {
            ReflectConfig::new()
                .spv(spirv_data.clone())
                .ref_all_rscs(true)
                .combine_img_samplers(true)
                .gen_unique_names(true)
                .reflect()
                .unwrap()
        })
        .collect();


    for entry_point in entry_list.iter().flatten()
    {
        for var in &entry_point.vars
        {
            match var
            {
                Variable::Input { name, location, ty } =>
                {
                   println!("{:?}", name);
                }
                Variable::Descriptor {
                    desc_bind,
                    desc_ty,
                    ty,
                    nbind,
                    name,
                    ..
                } =>
                {
                    println!("{:?}", name);
                   
                }
                Variable::Output { ty, name, .. } =>
                {
                    println!("{:?}", name);
                }
                _ => panic!(),
            }
        }
    }

Which outputs:

Some("window_data")
Some("voctree_meta")
Some("nodes")
Some("out_color")

I don't understand why cargo gpu would elide the variable when the old script I was using did not.

1 Answer 1

1

The issue is that the variable is elided because the compiler considers it unused, adding:

let node_count = unsafe { counter.index_unchecked_mut(0_usize) };
*out_color = Vec3::splat(*node_count as f32).extend(1.);

to the fragment shader makes the variable be present.

So likely a different version of rustgpu with different optimization logic is used, explaining the difference in behaviour,

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.