Skip to content

Make cargo-gpu a library, usable from build scripts #71

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 10 commits into
base: updating-target-json-support
Choose a base branch
from

Conversation

Firestar99
Copy link
Member

@Firestar99 Firestar99 commented May 12, 2025

Requires #75 (and base retarget)

Build script API example:

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()?;
    let path_to_spv = spv_result.module.unwrap_single();

    // emit path to wgsl into env var, used in `quad.rs` like this:
    // > include_str!(env!("MY_SHADER_PATH"))
    println!(
        "cargo::rustc-env=MY_SHADER_PATH={}",
        path_to_spv.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(())
}

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

closes #67
closes #73

@Firestar99 Firestar99 force-pushed the library branch 2 times, most recently from a4d7b41 to 51eb0ac Compare May 26, 2025 16:46
@Firestar99 Firestar99 changed the base branch from main to updating-target-json-support May 28, 2025 11:57
@Firestar99 Firestar99 force-pushed the updating-target-json-support branch from 09edda2 to af18f7f Compare May 28, 2025 14:06
@Firestar99 Firestar99 force-pushed the updating-target-json-support branch from af18f7f to 8fc529a Compare May 28, 2025 17:49
@Firestar99 Firestar99 force-pushed the updating-target-json-support branch from 8fc529a to fb10e19 Compare May 28, 2025 17:51
@Firestar99 Firestar99 force-pushed the updating-target-json-support branch from fb10e19 to fd5da3c Compare May 30, 2025 08:44
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.

cargo-gpu as a library discussion: ask for toolchain installation
1 participant