import { assert } from '../utils/assert' ; import type { ShaderModule } from '../../types' ; import { ShaderModuleInstance } from '../shader-module/shader-module-instance' ; export function resolveModules ( modules ) { return getShaderDependencies ( instantiateModules ( modules ) ) ; } function instantiateModules ( modules ) { return modules . map ( ( module ) => { if ( module instanceof ShaderModuleInstance ) { return module ; } assert ( typeof module !== 'string' , `Shader module use by name is deprecated. Import shader module '${ module }' and use it directly.` ) ; assert ( module . name , 'shader module has no name' ) ; const moduleObject = new ShaderModuleInstance ( module ) ; moduleObject . dependencies = instantiateModules ( module . dependencies || [ ] ) ; return moduleObject ; } ) ; } param modules return function getShaderDependencies ( modules ) { const moduleMap = { } ; const moduleDepth = { } ; getDependencyGraph ( { modules , level : 0 , moduleMap , moduleDepth } ) ; return Object . keys ( moduleDepth ) . sort ( ( a , b ) => moduleDepth [ b ] - moduleDepth [ a ] ) . map ( ( name ) => moduleMap [ name ] ) ; } param options . modules param options . level param options . moduleMap param options . moduleDepth return function getDependencyGraph ( options ) { const { modules , level , moduleMap , moduleDepth } = options ; if ( level >= 5 ) { throw new Error ( 'Possible loop in shader dependency graph' ) ; } for ( const module of modules ) { moduleMap [ module . name ] = module ; if ( moduleDepth [ module . name ] === undefined || moduleDepth [ module . name ] < level ) { moduleDepth [ module . name ] = level ; } } for ( const module of modules ) { if ( module . dependencies ) { getDependencyGraph ( { modules : module . dependencies , level : level + 1 , moduleMap , moduleDepth } ) ; } } } export const TEST_EXPORTS = { getShaderDependencies , getDependencyGraph } ;