1+ use alloc:: borrow:: ToOwned ;
12use alloc:: {
23 borrow:: Cow ,
34 string:: { String , ToString as _} ,
@@ -264,27 +265,8 @@ impl super::Device {
264265 naga_stage : naga:: ShaderStage ,
265266 fragment_stage : Option < & crate :: ProgrammableStage < super :: ShaderModule > > ,
266267 ) -> Result < super :: CompiledShader , crate :: PipelineError > {
267- use naga:: back:: hlsl;
268-
269- let frag_ep = fragment_stage
270- . map ( |fs_stage| {
271- hlsl:: FragmentEntryPoint :: new ( & fs_stage. module . naga . module , fs_stage. entry_point )
272- . ok_or ( crate :: PipelineError :: EntryPoint (
273- naga:: ShaderStage :: Fragment ,
274- ) )
275- } )
276- . transpose ( ) ?;
277-
278268 let stage_bit = auxil:: map_naga_stage ( naga_stage) ;
279269
280- let ( module, info) = naga:: back:: pipeline_constants:: process_overrides (
281- & stage. module . naga . module ,
282- & stage. module . naga . info ,
283- Some ( ( naga_stage, stage. entry_point ) ) ,
284- stage. constants ,
285- )
286- . map_err ( |e| crate :: PipelineError :: PipelineConstants ( stage_bit, format ! ( "HLSL: {e:?}" ) ) ) ?;
287-
288270 let needs_temp_options = stage. zero_initialize_workgroup_memory
289271 != layout. naga_options . zero_initialize_workgroup_memory
290272 || stage. module . runtime_checks . bounds_checks != layout. naga_options . restrict_indexing
@@ -301,43 +283,90 @@ impl super::Device {
301283 & layout. naga_options
302284 } ;
303285
304- let pipeline_options = hlsl :: PipelineOptions {
305- entry_point : Some ( ( naga_stage , stage . entry_point . to_string ( ) ) ) ,
306- } ;
286+ let key = match & stage . module . source {
287+ super :: ShaderModuleSource :: Naga ( naga_shader ) => {
288+ use naga :: back :: hlsl ;
307289
308- //TODO: reuse the writer
309- let ( source, entry_point) = {
310- let mut source = String :: new ( ) ;
311- let mut writer = hlsl:: Writer :: new ( & mut source, naga_options, & pipeline_options) ;
290+ let frag_ep = match fragment_stage {
291+ Some ( crate :: ProgrammableStage {
292+ module :
293+ super :: ShaderModule {
294+ source : super :: ShaderModuleSource :: Naga ( naga_shader) ,
295+ ..
296+ } ,
297+ entry_point,
298+ ..
299+ } ) => Some (
300+ hlsl:: FragmentEntryPoint :: new ( & naga_shader. module , entry_point) . ok_or (
301+ crate :: PipelineError :: EntryPoint ( naga:: ShaderStage :: Fragment ) ,
302+ ) ,
303+ ) ,
304+ _ => None ,
305+ }
306+ . transpose ( ) ?;
307+ let ( module, info) = naga:: back:: pipeline_constants:: process_overrides (
308+ & naga_shader. module ,
309+ & naga_shader. info ,
310+ Some ( ( naga_stage, stage. entry_point ) ) ,
311+ stage. constants ,
312+ )
313+ . map_err ( |e| {
314+ crate :: PipelineError :: PipelineConstants ( stage_bit, format ! ( "HLSL: {e:?}" ) )
315+ } ) ?;
312316
313- profiling:: scope!( "naga::back::hlsl::write" ) ;
314- let mut reflection_info = writer
315- . write ( & module, & info, frag_ep. as_ref ( ) )
316- . map_err ( |e| crate :: PipelineError :: Linkage ( stage_bit, format ! ( "HLSL: {e:?}" ) ) ) ?;
317+ let pipeline_options = hlsl:: PipelineOptions {
318+ entry_point : Some ( ( naga_stage, stage. entry_point . to_string ( ) ) ) ,
319+ } ;
317320
318- assert_eq ! ( reflection_info. entry_point_names. len( ) , 1 ) ;
321+ //TODO: reuse the writer
322+ let ( source, entry_point) = {
323+ let mut source = String :: new ( ) ;
324+ let mut writer =
325+ hlsl:: Writer :: new ( & mut source, naga_options, & pipeline_options) ;
319326
320- let entry_point = reflection_info
321- . entry_point_names
322- . pop ( )
323- . unwrap ( )
324- . map_err ( |e| crate :: PipelineError :: Linkage ( stage_bit, format ! ( "{e}" ) ) ) ?;
327+ profiling:: scope!( "naga::back::hlsl::write" ) ;
328+ let mut reflection_info = writer
329+ . write ( & module, & info, frag_ep. as_ref ( ) )
330+ . map_err ( |e| {
331+ crate :: PipelineError :: Linkage ( stage_bit, format ! ( "HLSL: {e:?}" ) )
332+ } ) ?;
325333
326- ( source, entry_point)
327- } ;
334+ assert_eq ! ( reflection_info. entry_point_names. len( ) , 1 ) ;
328335
329- log:: info!(
330- "Naga generated shader for {:?} at {:?}:\n {}" ,
331- entry_point,
332- naga_stage,
333- source
334- ) ;
336+ let entry_point = reflection_info
337+ . entry_point_names
338+ . pop ( )
339+ . unwrap ( )
340+ . map_err ( |e| crate :: PipelineError :: Linkage ( stage_bit, format ! ( "{e}" ) ) ) ?;
335341
336- let key = ShaderCacheKey {
337- source,
338- entry_point,
339- stage : naga_stage,
340- shader_model : naga_options. shader_model ,
342+ ( source, entry_point)
343+ } ;
344+ log:: info!(
345+ "Naga generated shader for {:?} at {:?}:\n {}" ,
346+ entry_point,
347+ naga_stage,
348+ source
349+ ) ;
350+
351+ ShaderCacheKey {
352+ source,
353+ entry_point,
354+ stage : naga_stage,
355+ shader_model : naga_options. shader_model ,
356+ }
357+ }
358+ super :: ShaderModuleSource :: HlslPassthrough ( passthrough) => ShaderCacheKey {
359+ source : passthrough. shader . clone ( ) ,
360+ entry_point : passthrough. entry_point . clone ( ) ,
361+ stage : naga_stage,
362+ shader_model : naga_options. shader_model ,
363+ } ,
364+
365+ super :: ShaderModuleSource :: DxilPassthrough ( passthrough) => {
366+ return Ok ( super :: CompiledShader :: Precompiled (
367+ passthrough. shader . clone ( ) ,
368+ ) )
369+ }
341370 } ;
342371
343372 {
@@ -351,11 +380,7 @@ impl super::Device {
351380
352381 let source_name = stage. module . raw_name . as_deref ( ) ;
353382
354- let full_stage = format ! (
355- "{}_{}" ,
356- naga_stage. to_hlsl_str( ) ,
357- naga_options. shader_model. to_str( )
358- ) ;
383+ let full_stage = format ! ( "{}_{}" , naga_stage. to_hlsl_str( ) , key. shader_model. to_str( ) ) ;
359384
360385 let compiled_shader = self . compiler_container . compile (
361386 self ,
@@ -1671,7 +1696,7 @@ impl crate::Device for super::Device {
16711696 . and_then ( |label| alloc:: ffi:: CString :: new ( label) . ok ( ) ) ;
16721697 match shader {
16731698 crate :: ShaderInput :: Naga ( naga) => Ok ( super :: ShaderModule {
1674- naga,
1699+ source : super :: ShaderModuleSource :: Naga ( naga) ,
16751700 raw_name,
16761701 runtime_checks : desc. runtime_checks ,
16771702 } ) ,
@@ -1681,6 +1706,32 @@ impl crate::Device for super::Device {
16811706 crate :: ShaderInput :: Msl { .. } => {
16821707 panic ! ( "MSL_SHADER_PASSTHROUGH is not enabled for this backend" )
16831708 }
1709+ crate :: ShaderInput :: Dxil {
1710+ shader,
1711+ entry_point,
1712+ num_workgroups,
1713+ } => Ok ( super :: ShaderModule {
1714+ source : super :: ShaderModuleSource :: DxilPassthrough ( super :: DxilPassthroughShader {
1715+ shader : shader. to_vec ( ) ,
1716+ entry_point,
1717+ num_workgroups,
1718+ } ) ,
1719+ raw_name,
1720+ runtime_checks : desc. runtime_checks ,
1721+ } ) ,
1722+ crate :: ShaderInput :: Hlsl {
1723+ shader,
1724+ entry_point,
1725+ num_workgroups,
1726+ } => Ok ( super :: ShaderModule {
1727+ source : super :: ShaderModuleSource :: HlslPassthrough ( super :: HlslPassthroughShader {
1728+ shader : shader. to_owned ( ) ,
1729+ entry_point,
1730+ num_workgroups,
1731+ } ) ,
1732+ raw_name,
1733+ runtime_checks : desc. runtime_checks ,
1734+ } ) ,
16841735 }
16851736 }
16861737 unsafe fn destroy_shader_module ( & self , _module : super :: ShaderModule ) {
0 commit comments