gfx-rs / wgpu

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

[naga-cli] add --defines options for the glsl parser #5859

Closed theomonnom closed 6 days ago

theomonnom commented 6 days ago

Description The motivation is to enable one glsl shader file that includes both vertex and fragment stages separated through preprocessing. This matches the behaviour of glslangValidator

Example of this particular glsl shader:

#version 450 core

#ifdef VERTEX_SHADER
struct VertexInput {
    vec3 position;
    vec2 tex_coord;
};

layout(location = 0) in vec3 in_position;
layout(location = 1) in vec2 in_tex_coord;
layout(location = 0) out vec2 frag_tex_coord;

void main() {
    VertexInput vertex;
    vertex.position = in_position;
    vertex.tex_coord = in_tex_coord;

    frag_tex_coord = vertex.tex_coord;
    gl_Position = vec4(vertex.position, 1.0);
}
#endif

#ifdef FRAGMENT_SHADER
layout(location = 0) in vec2 frag_tex_coord;
layout(location = 0) out vec4 out_color;

void main() {
    out_color = vec4(frag_tex_coord, 0.0, 1.0);
}
#endif

Testing

cargo run -- my_shader.glsl my_shader.frag.wgsl --shader-stage fragment --entry-point main -D FRAGMENT_SHADER
cargo run -- my_shader.glsl my_shader.vert.wgsl --shader-stage vertex --entry-point main -D VERTEX_SHADER

Checklist