diff --git a/crates/bevy_render/src/render_resource/pipeline_cache.rs b/crates/bevy_render/src/render_resource/pipeline_cache.rs index f2ab16a23e..7c54b0f406 100644 --- a/crates/bevy_render/src/render_resource/pipeline_cache.rs +++ b/crates/bevy_render/src/render_resource/pipeline_cache.rs @@ -203,28 +203,25 @@ impl ShaderCache { shaders: &HashMap, Shader>, import: &ShaderImport, ) -> Result<(), PipelineCacheError> { - if !composer.contains_module(&import.module_name()) { - if let Some(shader_handle) = import_path_shaders.get(import) { - if let Some(shader) = shaders.get(shader_handle) { - for import in &shader.imports { - Self::add_import_to_composer( - composer, - import_path_shaders, - shaders, - import, - )?; - } - - composer.add_composable_module(shader.into())?; - } else { - Err(PipelineCacheError::ShaderImportNotYetAvailable)?; - } - } else { - Err(PipelineCacheError::ShaderImportNotYetAvailable)?; - } - // if we fail to add a module the composer will tell us what is missing + // Early out if we've already imported this module + if composer.contains_module(&import.module_name()) { + return Ok(()); } + // Check if the import is available (this handles the recursive import case) + let shader = import_path_shaders + .get(import) + .and_then(|handle| shaders.get(handle)) + .ok_or(PipelineCacheError::ShaderImportNotYetAvailable)?; + + // Recurse down to ensure all import dependencies are met + for import in &shader.imports { + Self::add_import_to_composer(composer, import_path_shaders, shaders, import)?; + } + + composer.add_composable_module(shader.into())?; + // if we fail to add a module the composer will tell us what is missing + Ok(()) }