@@ -22,6 +22,24 @@ use spirv_builder::{
22
22
23
23
use tracing:: { error, info} ;
24
24
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
+
25
43
/// Clap application struct.
26
44
#[ derive( Debug , Clone , Parser ) ]
27
45
#[ command( author, version, about, long_about = None ) ]
@@ -30,6 +48,9 @@ struct ShaderBuilder {
30
48
path_to_crate : PathBuf ,
31
49
/// If set, combined SPIR-V and entrypoint metadata will be written to this file on succesful compile.
32
50
output_path : Option < PathBuf > ,
51
+ /// The format to write output in.
52
+ #[ arg( long, default_value = "Messagepack" ) ]
53
+ output_format : OutputFormat ,
33
54
/// rust-gpu compile target.
34
55
#[ arg( short, long, default_value = "spirv-unknown-vulkan1.2" ) ]
35
56
target : String ,
@@ -194,7 +215,11 @@ async fn async_watch<P: AsRef<Path>>(
194
215
Ok ( ( ) )
195
216
}
196
217
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
+ ) {
198
223
info ! ( "Entry Points:" ) ;
199
224
for entry in & result. entry_points {
200
225
println ! ( "{entry:}" ) ;
@@ -246,10 +271,20 @@ async fn handle_compile_result(result: CompileResult, output_path: Option<PathBu
246
271
modules,
247
272
} ;
248
273
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
+ }
253
288
println ! ( ) ;
254
289
info ! ( "Wrote output to {output_path:?}" ) ;
255
290
}
@@ -266,7 +301,11 @@ fn main() {
266
301
info ! ( "Building shader..." ) ;
267
302
println ! ( ) ;
268
303
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
+ ) ) ;
270
309
} else {
271
310
error ! ( "Build failed!" ) ;
272
311
}
@@ -317,7 +356,8 @@ fn main() {
317
356
Ok ( Msg :: Build ( result) ) => {
318
357
if let Ok ( result) = result {
319
358
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) )
321
361
. detach ( ) ;
322
362
} else {
323
363
error ! ( "Build failed!" ) ;
0 commit comments