mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-20 19:56:09 +08:00
Shaders: rust-gpu integration to compile shader nodes to WGSL (#3097)
* shaders: shader compilation setup * nix: use rustc_codegen_spirv.so from nix * shaders: codegen for per_pixel_adjust shader nodes * shaders: disable nodes needing bool * shaders: `#[repr(u32)]` some enums * shaders: add lint ignores from rust-gpu * shaders: fix node-macro tests * gcore-shaders: toml cleanup * shader-nodes feature: put rust-gpu to wgsl compile behind feature gate * shaders: fix use TokenStream2 * shaders: allow providing shader externally * Update iai runner in workflow --------- Co-authored-by: Timon Schelling <me@timon.zip> Co-authored-by: Dennis Kobert <dennis@kobert.dev>
This commit is contained in:
co-authored by
Timon Schelling
Dennis Kobert
parent
083dfa5f49
commit
a10103311e
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "graphene-raster-nodes-shaders"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
description = "graphene raster data format"
|
||||
authors = ["Graphite Authors <contact@graphite.rs>"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
|
||||
[dependencies]
|
||||
|
||||
[build-dependencies]
|
||||
cargo-gpu = { workspace = true }
|
||||
env_logger = { workspace = true }
|
||||
@@ -0,0 +1,55 @@
|
||||
use cargo_gpu::InstalledBackend;
|
||||
use cargo_gpu::spirv_builder::{MetadataPrintout, SpirvMetadata};
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
env_logger::builder().init();
|
||||
|
||||
// Skip building the shader if they are provided externally
|
||||
println!("cargo:rerun-if-env-changed=GRAPHENE_RASTER_NODES_SHADER_PATH");
|
||||
if !std::env::var("GRAPHENE_RASTER_NODES_SHADER_PATH").unwrap_or_default().is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Allows overriding the PATH to inject the rust-gpu rust toolchain when building the rest of the project with stable rustc.
|
||||
// Used in nix shell. Do not remove without checking with developers using nix.
|
||||
println!("cargo:rerun-if-env-changed=RUST_GPU_PATH_OVERRIDE");
|
||||
if let Ok(path_override) = std::env::var("RUST_GPU_PATH_OVERRIDE") {
|
||||
let current_path = std::env::var("PATH").unwrap_or_default();
|
||||
let new_path = format!("{path_override}:{current_path}");
|
||||
// SAFETY: Build script is single-threaded therefore this cannot lead to undefined behavior.
|
||||
unsafe {
|
||||
std::env::set_var("PATH", &new_path);
|
||||
}
|
||||
}
|
||||
|
||||
let shader_crate = PathBuf::from(concat!(env!("CARGO_MANIFEST_DIR"), "/.."));
|
||||
|
||||
println!("cargo:rerun-if-env-changed=RUSTC_CODEGEN_SPIRV_PATH");
|
||||
let rustc_codegen_spirv_path = std::env::var("RUSTC_CODEGEN_SPIRV_PATH").unwrap_or_default();
|
||||
let backend = if rustc_codegen_spirv_path.is_empty() {
|
||||
// install the toolchain and build the `rustc_codegen_spirv` codegen backend with it
|
||||
cargo_gpu::Install::from_shader_crate(shader_crate.clone()).run()?
|
||||
} else {
|
||||
// use the `RUSTC_CODEGEN_SPIRV` environment variable to find the codegen backend
|
||||
let mut backend = InstalledBackend::default();
|
||||
backend.rustc_codegen_spirv_location = PathBuf::from(rustc_codegen_spirv_path);
|
||||
backend.toolchain_channel = "nightly".to_string();
|
||||
backend.target_spec_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
backend
|
||||
};
|
||||
|
||||
// build the shader crate
|
||||
let mut builder = backend.to_spirv_builder(shader_crate, "spirv-unknown-naga-wgsl");
|
||||
builder.print_metadata = MetadataPrintout::DependencyOnly;
|
||||
builder.spirv_metadata = SpirvMetadata::Full;
|
||||
builder.shader_crate_features.default_features = false;
|
||||
let wgsl_result = builder.build()?;
|
||||
let path_to_spv = wgsl_result.module.unwrap_single();
|
||||
|
||||
// needs to be fixed upstream
|
||||
let path_to_wgsl = path_to_spv.with_extension("wgsl");
|
||||
|
||||
println!("cargo::rustc-env=GRAPHENE_RASTER_NODES_SHADER_PATH={}", path_to_wgsl.display());
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"allows-weak-linkage": false,
|
||||
"arch": "spirv",
|
||||
"crt-objects-fallback": "false",
|
||||
"crt-static-allows-dylibs": true,
|
||||
"crt-static-respected": true,
|
||||
"data-layout": "e-m:e-p:32:32:32-i64:64-n8:16:32:64",
|
||||
"dll-prefix": "",
|
||||
"dll-suffix": ".spv.json",
|
||||
"dynamic-linking": true,
|
||||
"emit-debug-gdb-scripts": false,
|
||||
"env": "naga-wgsl",
|
||||
"linker-flavor": "unix",
|
||||
"linker-is-gnu": false,
|
||||
"llvm-target": "spirv-unknown-naga-wgsl",
|
||||
"main-needs-argc-argv": false,
|
||||
"metadata": {
|
||||
"description": null,
|
||||
"host_tools": null,
|
||||
"std": null,
|
||||
"tier": null
|
||||
},
|
||||
"panic-strategy": "abort",
|
||||
"simd-types-indirect": false,
|
||||
"target-pointer-width": "32"
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
pub const WGSL_SHADER: &str = include_str!(env!("GRAPHENE_RASTER_NODES_SHADER_PATH"));
|
||||
Reference in New Issue
Block a user