Skip to content

Commit

Permalink
Separate the CLI argument parsing from the top-level generate funct…
Browse files Browse the repository at this point in the history
…ion (#10)
  • Loading branch information
ppenenko authored Dec 8, 2024
1 parent 9b8e779 commit 175518d
Showing 1 changed file with 61 additions and 46 deletions.
107 changes: 61 additions & 46 deletions src/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@ class _AssetResult(NamedTuple):
shaders : List[_Shader]

def _process_asset(
gltf_file_path : str,
out_dir : Path,
skip_codegen : bool = False
gltf_file_path : str,
out_dir : Path,
skip_codegen : bool = False
) -> _AssetResult:
log = io.StringIO()
log, sys.stdout = sys.stdout, log
Expand Down Expand Up @@ -226,49 +226,26 @@ def _process_asset(
log, sys.stdout = sys.stdout, log
return _AssetResult(log.getvalue(), per_asset_shaders)

if __name__ == "__main__":
parser = argparse.ArgumentParser(
description = "Generate shaders from glTF materials."
)
parser.add_argument("--gltf-dir", help = "Path to the source glTF assets")
parser.add_argument("--out-dir", help = "Path to the output directory")
parser.add_argument(
"--compile",
action = 'store_true',
help = "Compile the generated shaders with DXC (has to be in PATH)"
)
parser.add_argument(
"--to-glsl",
action = 'store_true',
help = "Cross-compile to GLSL with SPIRV-Cross"
)
parser.add_argument(
"--skip-codegen",
action = 'store_true',
help = "Assume that sources have been generated and proceed to "
"compilation."
)
parser.add_argument(
"--serial",
action = 'store_true',
help = "Disable parallelization to facilitate debugging."
)
args = parser.parse_args()

gltf_dir_path = Path(args.gltf_dir)
def generate(
gltf_dir_path : Path,
out_dir_path : Path,
compile : bool,
to_glsl : bool,
skip_codegen : bool,
serial : bool
):
if not gltf_dir_path.is_dir():
raise NotADirectoryError(gltf_dir_path)

os.makedirs(args.out_dir, exist_ok = True)
out_dir = Path(args.out_dir)

os.makedirs(out_dir_path, exist_ok = True)

shaders = []
if args.serial:
if serial:
for gltf_path in gltf_dir_path.glob('**/*.gltf'):
asset_result = _process_asset(
gltf_file_path = gltf_path,
out_dir = out_dir,
skip_codegen = args.skip_codegen
out_dir = out_dir_path,
skip_codegen = skip_codegen
)
print(asset_result.log)
shaders += asset_result.shaders
Expand All @@ -277,32 +254,70 @@ def _process_asset(
for asset_result in pool.imap_unordered(
functools.partial(
_process_asset,
out_dir = out_dir,
skip_codegen = args.skip_codegen
out_dir = out_dir_path,
skip_codegen = skip_codegen
),
gltf_dir_path.glob('**/*.gltf')
):
print(asset_result.log)
shaders += asset_result.shaders

if args.compile:
if compile:
print()
dxc.identify()
if args.to_glsl:
if to_glsl:
spirv_cross.identify()
glslc.identify()

if args.serial:
if serial:
for shader in shaders:
log = shader.compile(to_glsl = args.to_glsl)
log = shader.compile(to_glsl = to_glsl)
print(log, end = '')
else:
with mp.Pool() as pool:
for log in pool.imap_unordered(
functools.partial(
_compile_shader,
to_glsl = args.to_glsl
to_glsl = to_glsl
),
shaders
):
print(log, end = '')

if __name__ == "__main__":
parser = argparse.ArgumentParser(
description = "Generate shaders from glTF materials."
)
parser.add_argument("--gltf-dir", help = "Path to the source glTF assets")
parser.add_argument("--out-dir", help = "Path to the output directory")
parser.add_argument(
"--compile",
action = 'store_true',
help = "Compile the generated shaders with DXC (has to be in PATH)"
)
parser.add_argument(
"--to-glsl",
action = 'store_true',
help = "Cross-compile to GLSL with SPIRV-Cross"
)
parser.add_argument(
"--skip-codegen",
action = 'store_true',
help = "Assume that sources have been generated and proceed to "
"compilation."
)
parser.add_argument(
"--serial",
action = 'store_true',
help = "Disable parallelization to facilitate debugging."
)
args = parser.parse_args()

generate(
gltf_dir_path = Path(args.gltf_dir),
out_dir_path = Path(args.out_dir),
compile = args.compile,
to_glsl = args.to_glsl,
skip_codegen = args.skip_codegen,
serial = args.serial
)

0 comments on commit 175518d

Please sign in to comment.