Skip to content

Commit 45ba5b9

Browse files
authored
refactor(render): cleanup add_import_to_composer (#19269)
# Objective - Reduce nesting ## Solution - Refactor ## Testing - `bevy run --example=3d_scene web --open`
1 parent 2db1037 commit 45ba5b9

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

crates/bevy_render/src/render_resource/pipeline_cache.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -203,28 +203,25 @@ impl ShaderCache {
203203
shaders: &HashMap<AssetId<Shader>, Shader>,
204204
import: &ShaderImport,
205205
) -> Result<(), PipelineCacheError> {
206-
if !composer.contains_module(&import.module_name()) {
207-
if let Some(shader_handle) = import_path_shaders.get(import) {
208-
if let Some(shader) = shaders.get(shader_handle) {
209-
for import in &shader.imports {
210-
Self::add_import_to_composer(
211-
composer,
212-
import_path_shaders,
213-
shaders,
214-
import,
215-
)?;
216-
}
206+
// Early out if we've already imported this module
207+
if composer.contains_module(&import.module_name()) {
208+
return Ok(());
209+
}
217210

218-
composer.add_composable_module(shader.into())?;
219-
} else {
220-
Err(PipelineCacheError::ShaderImportNotYetAvailable)?;
221-
}
222-
} else {
223-
Err(PipelineCacheError::ShaderImportNotYetAvailable)?;
224-
}
225-
// if we fail to add a module the composer will tell us what is missing
211+
// Check if the import is available (this handles the recursive import case)
212+
let shader = import_path_shaders
213+
.get(import)
214+
.and_then(|handle| shaders.get(handle))
215+
.ok_or(PipelineCacheError::ShaderImportNotYetAvailable)?;
216+
217+
// Recurse down to ensure all import dependencies are met
218+
for import in &shader.imports {
219+
Self::add_import_to_composer(composer, import_path_shaders, shaders, import)?;
226220
}
227221

222+
composer.add_composable_module(shader.into())?;
223+
// if we fail to add a module the composer will tell us what is missing
224+
228225
Ok(())
229226
}
230227

0 commit comments

Comments
 (0)