Swoorup / wgsl-bindgen

Generate typesafe Rust bindings for wgsl shaders in wgpu
https://crates.io/crates/wgsl_bindgen
MIT License
34 stars 6 forks source link

Rust bindings assume all vertex shaders in a file have the same input, even if they don't #44

Open Gonkalbell opened 2 months ago

Gonkalbell commented 2 months ago

I have a file with multiple vertex shaders, like this:

struct VertexInput {
    @location(0) position: vec3f,
};

struct InstanceInput {
    @location(1) position: vec3f,
};

@vertex
fn dummy_vertex_shader(vert_in: VertexInput) -> @builtin(position) vec4f {
    return vec4f(vert_in.position, 1);
}

@vertex
fn dummy_instanced_vertex_shader(vert_in: VertexInput, instance_in: InstanceInput) -> @builtin(position) vec4f {
    return vec4f(vert_in.position + instance_in.position, 1);
}

In the generated rust bindings, there's a generated VERTEX_ATTRIBUTES and vertex_buffer_layout for VertexInput, but not for InstanceInput. Also, their _vertex_shader_entrys are the same, even though dummy_instanced_vertex_shader should have 2 inputs instead of one:

    pub fn dummy_vertex_shader_entry(
        vertex_input: wgpu::VertexStepMode,
    ) -> VertexEntry<1> {
        VertexEntry {
            entry_point: ENTRY_DUMMY_VERTEX_SHADER,
            buffers: [VertexInput::vertex_buffer_layout(vertex_input)],
            constants: Default::default(),
        }
    }
    pub fn dummy_instanced_vertex_shader_entry(
        vertex_input: wgpu::VertexStepMode,
    ) -> VertexEntry<1> {
        VertexEntry {
            entry_point: ENTRY_DUMMY_INSTANCED_VERTEX_SHADER,
            buffers: [VertexInput::vertex_buffer_layout(vertex_input)],
            constants: Default::default(),
        }
    }

If I move dummy_instanced_vertex_shader to be before dummy_vertex_shader in that file, I then get a VERTEX_ATTRIBUTES and vertex_buffer_layout for both VertexInput and InstanceInput. However, now dummy_vertex_shader_entry is incorrect:

    pub fn dummy_instanced_vertex_shader_entry(
        vertex_input: wgpu::VertexStepMode,
        instance_input: wgpu::VertexStepMode,
    ) -> VertexEntry<2> {
        VertexEntry {
            entry_point: ENTRY_DUMMY_INSTANCED_VERTEX_SHADER,
            buffers: [
                VertexInput::vertex_buffer_layout(vertex_input),
                InstanceInput::vertex_buffer_layout(instance_input),
            ],
            constants: Default::default(),
        }
    }
    pub fn dummy_vertex_shader_entry(
        vertex_input: wgpu::VertexStepMode,
        instance_input: wgpu::VertexStepMode,
    ) -> VertexEntry<2> {
        VertexEntry {
            entry_point: ENTRY_DUMMY_VERTEX_SHADER,
            buffers: [
                VertexInput::vertex_buffer_layout(vertex_input),
                InstanceInput::vertex_buffer_layout(instance_input),
            ],
            constants: Default::default(),
        }
    }

So it seems like wgsl_bindgen assumes that every vertex shader in a file shares the same input as the first vertex shader.

Swoorup commented 2 months ago

Should be fairly straightforward to fix. Will take a look. Been a bit busy with other stuff.