@@ -84,19 +84,51 @@ use std::{
84
84
ffi:: { CStr , CString } ,
85
85
fs:: File ,
86
86
os:: raw:: c_char,
87
+ path:: PathBuf ,
87
88
sync:: mpsc:: { TryRecvError , TrySendError , sync_channel} ,
88
89
thread,
89
90
} ;
90
91
91
- use clap:: Parser ;
92
+ use clap:: { Parser , ValueEnum } ;
92
93
93
94
use spirv_builder:: { MetadataPrintout , SpirvBuilder } ;
94
95
95
96
use shared:: ShaderConstants ;
96
97
98
+ // This runner currently doesn't run the `compute` shader example.
99
+ #[ derive( Debug , PartialEq , Eq , Copy , Clone , ValueEnum ) ]
100
+ pub enum RustGPUShader {
101
+ Simplest ,
102
+ Sky ,
103
+ Mouse ,
104
+ }
105
+
106
+ impl RustGPUShader {
107
+ // The form with dashes, e.g. `sky-shader`.
108
+ fn crate_name ( & self ) -> & ' static str {
109
+ match self {
110
+ RustGPUShader :: Simplest => "simplest-shader" ,
111
+ RustGPUShader :: Sky => "sky-shader" ,
112
+ RustGPUShader :: Mouse => "mouse-shader" ,
113
+ }
114
+ }
115
+
116
+ // The form with underscores, e.g. `sky_shader`.
117
+ fn crate_ident ( & self ) -> & ' static str {
118
+ match self {
119
+ RustGPUShader :: Simplest => "simplest_shader" ,
120
+ RustGPUShader :: Sky => "sky_shader" ,
121
+ RustGPUShader :: Mouse => "mouse_shader" ,
122
+ }
123
+ }
124
+ }
125
+
97
126
#[ derive( Debug , Parser ) ]
98
127
#[ command( ) ]
99
128
pub struct Options {
129
+ #[ arg( short, long, default_value = "sky" ) ]
130
+ shader : RustGPUShader ,
131
+
100
132
/// Use Vulkan debug layer (requires Vulkan SDK installed)
101
133
#[ arg( short, long) ]
102
134
debug_layer : bool ,
@@ -115,7 +147,7 @@ pub fn main() {
115
147
}
116
148
117
149
let options = Options :: parse ( ) ;
118
- let shaders = compile_shaders ( ) ;
150
+ let shaders = compile_shaders ( & options . shader ) ;
119
151
120
152
// runtime setup
121
153
let event_loop = EventLoop :: new ( ) . unwrap ( ) ;
@@ -138,17 +170,19 @@ pub fn main() {
138
170
for SpvFile { name, data } in shaders {
139
171
ctx. insert_shader_module ( name, & data) ;
140
172
}
173
+
174
+ let crate_ident = options. shader . crate_ident ( ) ;
141
175
ctx. build_pipelines (
142
176
vk:: PipelineCache :: null ( ) ,
143
177
vec ! [ (
144
- // HACK(eddyb) used to be `module: "sky_shader"` but we need `multimodule `
145
- // for `debugPrintf` instrumentation to work (see `compile_shaders`).
178
+ // HACK(eddyb) we need `multimodule` for `debugPrintf `
179
+ // instrumentation to work (see `compile_shaders`).
146
180
VertexShaderEntryPoint {
147
- module: "sky_shader ::main_vs". into ( ) ,
181
+ module: format! ( "{crate_ident} ::main_vs") ,
148
182
entry_point: "main_vs" . into( ) ,
149
183
} ,
150
184
FragmentShaderEntryPoint {
151
- module: "sky_shader ::main_fs". into ( ) ,
185
+ module: format! ( "{crate_ident} ::main_fs") ,
152
186
entry_point: "main_fs" . into( ) ,
153
187
} ,
154
188
) ] ,
@@ -203,7 +237,7 @@ pub fn main() {
203
237
let compiler_sender = compiler_sender. clone ( ) ;
204
238
thread:: spawn ( move || {
205
239
if let Err ( TrySendError :: Disconnected ( _) ) =
206
- compiler_sender. try_send ( compile_shaders ( ) )
240
+ compiler_sender. try_send ( compile_shaders ( & options . shader ) )
207
241
{
208
242
panic ! ( "compiler sender disconnected unexpectedly" ) ;
209
243
} ;
@@ -236,29 +270,33 @@ pub fn main() {
236
270
. unwrap ( ) ;
237
271
}
238
272
239
- pub fn compile_shaders ( ) -> Vec < SpvFile > {
240
- SpirvBuilder :: new (
241
- concat ! ( env!( "CARGO_MANIFEST_DIR" ) , "/../../shaders/sky-shader" ) ,
242
- "spirv-unknown-vulkan1.1" ,
243
- )
244
- . print_metadata ( MetadataPrintout :: None )
245
- . shader_panic_strategy ( spirv_builder:: ShaderPanicStrategy :: DebugPrintfThenExit {
246
- print_inputs : true ,
247
- print_backtrace : true ,
248
- } )
249
- // HACK(eddyb) needed because of `debugPrintf` instrumentation limitations
250
- // (see https://github.com/KhronosGroup/SPIRV-Tools/issues/4892).
251
- . multimodule ( true )
252
- . build ( )
253
- . unwrap ( )
254
- . module
255
- . unwrap_multi ( )
256
- . iter ( )
257
- . map ( |( name, path) | SpvFile {
258
- name : format ! ( "sky_shader::{name}" ) ,
259
- data : read_spv ( & mut File :: open ( path) . unwrap ( ) ) . unwrap ( ) ,
260
- } )
261
- . collect ( )
273
+ pub fn compile_shaders ( shader : & RustGPUShader ) -> Vec < SpvFile > {
274
+ let manifest_dir = env ! ( "CARGO_MANIFEST_DIR" ) ;
275
+ let crate_path = [ manifest_dir, ".." , ".." , "shaders" , shader. crate_name ( ) ]
276
+ . iter ( )
277
+ . copied ( )
278
+ . collect :: < PathBuf > ( ) ;
279
+ let crate_ident = shader. crate_ident ( ) ;
280
+
281
+ SpirvBuilder :: new ( crate_path, "spirv-unknown-vulkan1.1" )
282
+ . print_metadata ( MetadataPrintout :: None )
283
+ . shader_panic_strategy ( spirv_builder:: ShaderPanicStrategy :: DebugPrintfThenExit {
284
+ print_inputs : true ,
285
+ print_backtrace : true ,
286
+ } )
287
+ // HACK(eddyb) needed because of `debugPrintf` instrumentation limitations
288
+ // (see https://github.com/KhronosGroup/SPIRV-Tools/issues/4892).
289
+ . multimodule ( true )
290
+ . build ( )
291
+ . unwrap ( )
292
+ . module
293
+ . unwrap_multi ( )
294
+ . iter ( )
295
+ . map ( |( name, path) | SpvFile {
296
+ name : format ! ( "{crate_ident}::{name}" ) ,
297
+ data : read_spv ( & mut File :: open ( path) . unwrap ( ) ) . unwrap ( ) ,
298
+ } )
299
+ . collect ( )
262
300
}
263
301
264
302
#[ derive( Debug ) ]
@@ -685,6 +723,7 @@ pub struct RenderCtx {
685
723
pub recompiling_shaders : bool ,
686
724
pub start : std:: time:: Instant ,
687
725
726
+ // Only used for sky-shader.
688
727
// NOTE(eddyb) this acts like an integration test for specialization constants.
689
728
pub sky_fs_spec_id_0x5007_sun_intensity_extra_spec_const_factor : u32 ,
690
729
}
0 commit comments