Skip to content

Naga transpiling to wgsl #76

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

Naga transpiling to wgsl #76

wants to merge 4 commits into from

Conversation

Firestar99
Copy link
Member

@Firestar99 Firestar99 commented May 28, 2025

Requires #71 and Rust-GPU/rust-gpu#264 (and base retarget)

pub fn main() -> Result<(), Box<dyn std::error::Error>> {
    let shader_crate = PathBuf::from("./shaders");

    // install the toolchain and build the `rustc_codegen_spirv` codegen backend with it
    let backend = cargo_gpu::Install::from_shader_crate(shader_crate.clone()).run()?;

    // build the shader crate
    let builder = SpirvBuilder {
        print_metadata: MetadataPrintout::DependencyOnly,
        spirv_metadata: SpirvMetadata::Full,
        ..backend.to_spirv_builder(shader_crate, "spirv-unknown-vulkan1.2")
    };
    let spv_result = builder.build()?;

    // transpile the spv binaries to wgsl
    let wgsl_result = spv_result
        .naga_transpile(Capabilities::empty())?
        .to_wgsl(WriterFlags::empty())?;
    let path_to_wgsl = wgsl_result.module.unwrap_single();

    // emit path to wgsl into env var, used in `quad.rs` like this:
    // > include_str!(env!("COLORBUBBLE_WGSL_SHADER_PATH"))
    println!(
        "cargo::rustc-env=COLORBUBBLE_WGSL_SHADER_PATH={}",
        path_to_wgsl.display()
    );

    // you could also generate some rust source code into the `std::env::var("OUT_DIR")` dir
    // and use `include!(concat!(env!("OUT_DIR"), "/shader_symbols.rs"));` to include it
    Ok(())
}

https://github.com/Firestar99/colorbubble/blob/eedd20ecc8155f9be15bc95b0fcd68b5002b94f3/build.rs

I've separated the transpiling into two steps: naga_transpile and to_wgsl. This would allow us to add more naga transpiling targets in the future, without having to reparse the spv again or cause major code breakage.

@tombh
Copy link
Collaborator

tombh commented May 28, 2025

Great idea!

Just wondering if you knew about being able to base PRs off branches other than main? It's just a little change in the Github PR:
image
It's not at all necessary, just FYI. And it doesn't mean you have to actually merge into the new base branch (unless you want to of course), because you can always change the base branch back to main before the actual merge. That way you get a nice diff in the Github UI whilst it's being developed.

@Firestar99
Copy link
Member Author

@tombh feel free to comment on the build script example in the description, or try out the linked project on branch cargo-gpu :D

@LegNeato
Copy link

I'm not sure we want to do this. Is there any way this can be pushed "down" into rust-gpu? It feels like surfacing unnecessary impl details. To users, the transpiling is basically part of the compilation step...except targeting something like wgsl-unknown-unknown

@Firestar99 Firestar99 force-pushed the library branch 2 times, most recently from ec26508 to c2d886d Compare June 2, 2025 15:29
Base automatically changed from library to main June 2, 2025 15:42
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

Successfully merging this pull request may close these issues.

3 participants