@@ -22,6 +22,24 @@ use spirv_builder::{
2222
2323use tracing:: { error, info} ;
2424
25+ #[ derive( Debug , Copy , Clone ) ]
26+ pub enum OutputFormat {
27+ Json ,
28+ Messagepack ,
29+ }
30+
31+ impl FromStr for OutputFormat {
32+ type Err = & ' static str ;
33+
34+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
35+ match s {
36+ "Json" => Ok ( Self :: Json ) ,
37+ "Messagepack" => Ok ( Self :: Messagepack ) ,
38+ _ => Err ( "Unrecognized output mode" ) ,
39+ }
40+ }
41+ }
42+
2543/// Clap application struct.
2644#[ derive( Debug , Clone , Parser ) ]
2745#[ command( author, version, about, long_about = None ) ]
@@ -30,6 +48,9 @@ struct ShaderBuilder {
3048 path_to_crate : PathBuf ,
3149 /// If set, combined SPIR-V and entrypoint metadata will be written to this file on succesful compile.
3250 output_path : Option < PathBuf > ,
51+ /// The format to write output in.
52+ #[ arg( long, default_value = "Messagepack" ) ]
53+ output_format : OutputFormat ,
3354 /// rust-gpu compile target.
3455 #[ arg( short, long, default_value = "spirv-unknown-vulkan1.2" ) ]
3556 target : String ,
@@ -194,7 +215,11 @@ async fn async_watch<P: AsRef<Path>>(
194215 Ok ( ( ) )
195216}
196217
197- async fn handle_compile_result ( result : CompileResult , output_path : Option < PathBuf > ) {
218+ async fn handle_compile_result (
219+ result : CompileResult ,
220+ output_path : Option < PathBuf > ,
221+ output_format : OutputFormat ,
222+ ) {
198223 info ! ( "Entry Points:" ) ;
199224 for entry in & result. entry_points {
200225 println ! ( "{entry:}" ) ;
@@ -246,10 +271,20 @@ async fn handle_compile_result(result: CompileResult, output_path: Option<PathBu
246271 modules,
247272 } ;
248273
249- let out = serde_json:: to_string_pretty ( & out) . expect ( "Failed to serialize output" ) ;
250- async_fs:: write ( & output_path, out)
251- . await
252- . expect ( "Failed to write output" ) ;
274+ match output_format {
275+ OutputFormat :: Json => {
276+ let out = serde_json:: to_string_pretty ( & out) . expect ( "Failed to serialize output" ) ;
277+ async_fs:: write ( & output_path, out)
278+ . await
279+ . expect ( "Failed to write output" ) ;
280+ }
281+ OutputFormat :: Messagepack => {
282+ let out = rmp_serde:: to_vec_named ( & out) . expect ( "Failed to serialize output" ) ;
283+ async_fs:: write ( & output_path, out)
284+ . await
285+ . expect ( "Failed to write output" ) ;
286+ }
287+ }
253288 println ! ( ) ;
254289 info ! ( "Wrote output to {output_path:?}" ) ;
255290}
@@ -266,7 +301,11 @@ fn main() {
266301 info ! ( "Building shader..." ) ;
267302 println ! ( ) ;
268303 if let Ok ( result) = args. build_shader ( ) {
269- future:: block_on ( handle_compile_result ( result, args. output_path . clone ( ) ) ) ;
304+ future:: block_on ( handle_compile_result (
305+ result,
306+ args. output_path . clone ( ) ,
307+ args. output_format ,
308+ ) ) ;
270309 } else {
271310 error ! ( "Build failed!" ) ;
272311 }
@@ -317,7 +356,8 @@ fn main() {
317356 Ok ( Msg :: Build ( result) ) => {
318357 if let Ok ( result) = result {
319358 let output_path = args. output_path . clone ( ) ;
320- ex. spawn ( handle_compile_result ( result, output_path) )
359+ let output_format = args. output_format ;
360+ ex. spawn ( handle_compile_result ( result, output_path, output_format) )
321361 . detach ( ) ;
322362 } else {
323363 error ! ( "Build failed!" ) ;
0 commit comments