From 5743da3d56f9073cc2c8d366541d51b1f53d2eb0 Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Wed, 11 May 2022 23:50:09 -0400 Subject: [PATCH] Initial attempt at build script compilation --- citro3d/build.rs | 39 ++++++++++++++++++++++++++++ citro3d/examples/assets/vshader.pica | 2 -- citro3d/examples/triangle.rs | 21 +++++---------- 3 files changed, 46 insertions(+), 16 deletions(-) create mode 100644 citro3d/build.rs diff --git a/citro3d/build.rs b/citro3d/build.rs new file mode 100644 index 0000000..b6935f0 --- /dev/null +++ b/citro3d/build.rs @@ -0,0 +1,39 @@ +use std::ffi::OsStr; +use std::path::PathBuf; +use std::process::Command; + +fn main() { + println!("cargo:rerun-if-changed=build.rs"); + println!("cargo:rerun-if-changed=examples/assets"); + + let mut asset_dir = PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").unwrap()); + asset_dir.push("examples"); + asset_dir.push("assets"); + + println!("Checking dir {:?}", asset_dir.display()); + + for entry in asset_dir.read_dir().unwrap().flatten() { + println!("Checking {:?}", entry.path().display()); + if let Some("pica") = entry.path().extension().and_then(OsStr::to_str) { + println!("cargo:rerun-if-changed={}", entry.path().display()); + + let mut out_path = PathBuf::from(std::env::var_os("OUT_DIR").unwrap()); + out_path.push("examples"); + out_path.push("assets"); + out_path.push(entry.path().with_extension("shbin").file_name().unwrap()); + + std::fs::create_dir_all(out_path.parent().unwrap()).unwrap(); + + println!("Compiling {:?}", out_path.display()); + + let mut cmd = Command::new("picasso"); + cmd.arg(entry.path()).arg("--out").arg(out_path); + + let status = cmd.spawn().unwrap().wait().unwrap(); + assert!( + status.success(), + "Command {cmd:#?} failed with code {status:?}" + ); + } + } +} diff --git a/citro3d/examples/assets/vshader.pica b/citro3d/examples/assets/vshader.pica index 79077d1..27ce6bf 100644 --- a/citro3d/examples/assets/vshader.pica +++ b/citro3d/examples/assets/vshader.pica @@ -5,8 +5,6 @@ ; Constants .constf myconst(0.0, 1.0, -1.0, 0.1) -.constf myconst2(0.3, 0.0, 0.0, 0.0) -.alias zeros myconst.xxxx ; Vector full of zeros .alias ones myconst.yyyy ; Vector full of ones ; Outputs diff --git a/citro3d/examples/triangle.rs b/citro3d/examples/triangle.rs index 20d4c25..266d165 100644 --- a/citro3d/examples/triangle.rs +++ b/citro3d/examples/triangle.rs @@ -91,25 +91,18 @@ impl Vertex { } } +static SHBIN_BYTES: &[u8] = + include_bytes!(concat!(env!("OUT_DIR"), "/examples/assets/vshader.shbin")); + fn scene_init() -> (shaderProgram_s, i8, C3D_Mtx, *mut libc::c_void, *mut DVLB_s) { // Load the vertex shader, create a shader program and bind it unsafe { - // To compile the shader: - // ```sh - // picasso assets/vshader.v.pica -o assets/vshader.shbin - // ``` - // TODO: can we do this in a build script? - - // boo, this way we have to specify the length. Alternative seems to be allocating a - // slice first, then copying into it with a transmute... - const SHBIN_BYTES: &[u8; 280] = include_bytes!("assets/vshader.shbin"); - // Assume the data is aligned properly... - let mut shbin_data: [u32; SHBIN_BYTES.len() / 4] = std::mem::transmute_copy(SHBIN_BYTES); + let mut shader_bytes = SHBIN_BYTES.to_owned(); + // Assume the data is aligned properly... let vshader_dvlb = citro3d_sys::DVLB_ParseFile( - shbin_data.as_mut_ptr(), - shbin_data - .len() + shader_bytes.as_mut_ptr() as _, + (shader_bytes.len() / 4) .try_into() .expect("shader len fits in a u32"), );