@@ -892,6 +892,7 @@ def generateSPV( # noqa: C901
892892 output_dir : str ,
893893 cache_dir : Optional [str ] = None ,
894894 force_rebuild : bool = False ,
895+ nthreads : int = - 1 ,
895896 ) -> Dict [str , str ]:
896897 # The key of this dictionary is the full path to a generated source file. The
897898 # value is a tuple that contains 3 entries:
@@ -1118,11 +1119,21 @@ def compile_spirv(shader_paths_pair) -> Tuple[str, str]:
11181119 gen_file_meta [gen_out_path ] = (file_changed , include_list )
11191120
11201121 # Parallelize SPIR-V compilation to optimize build time
1121- with ThreadPool (os .cpu_count ()) as pool :
1122- for spv_out_path , glsl_out_path in pool .map (
1123- compile_spirv , self .output_file_map .items ()
1124- ):
1122+ # Determine number of threads: -1 means use all CPU cores, 1 means sequential
1123+ num_processes = os .cpu_count () if nthreads == - 1 else nthreads
1124+
1125+ if num_processes == 1 :
1126+ # Sequential compilation (single-threaded)
1127+ for shader_pair in self .output_file_map .items ():
1128+ spv_out_path , glsl_out_path = compile_spirv (shader_pair )
11251129 spv_to_glsl_map [spv_out_path ] = glsl_out_path
1130+ else :
1131+ # Parallel compilation
1132+ with ThreadPool (num_processes ) as pool :
1133+ for spv_out_path , glsl_out_path in pool .map (
1134+ compile_spirv , self .output_file_map .items ()
1135+ ):
1136+ spv_to_glsl_map [spv_out_path ] = glsl_out_path
11261137
11271138 return spv_to_glsl_map
11281139
@@ -1443,6 +1454,12 @@ def main(argv: List[str]) -> int:
14431454 parser .add_argument (
14441455 "--env" , metavar = "KEY=VALUE" , nargs = "*" , help = "Set a number of key-value pairs"
14451456 )
1457+ parser .add_argument (
1458+ "--nthreads" ,
1459+ type = int ,
1460+ default = - 1 ,
1461+ help = "Number of threads for shader compilation. -1 (default) uses all available CPU cores, 1 uses sequential compilation." ,
1462+ )
14461463 options = parser .parse_args ()
14471464
14481465 env = DEFAULT_ENV
@@ -1477,7 +1494,10 @@ def main(argv: List[str]) -> int:
14771494 replace_u16vecn = options .replace_u16vecn ,
14781495 )
14791496 output_spv_files = shader_generator .generateSPV (
1480- options .output_path , options .tmp_dir_path , options .force_rebuild
1497+ options .output_path ,
1498+ options .tmp_dir_path ,
1499+ options .force_rebuild ,
1500+ options .nthreads ,
14811501 )
14821502
14831503 genCppFiles (
0 commit comments