着色器编译脚本
This commit is contained in:
@@ -10,15 +10,27 @@ def find_shader_files(input_dir, extensions):
|
||||
if any(file.endswith(ext) for ext in extensions):
|
||||
yield os.path.join(root, file)
|
||||
|
||||
def has_glsl_main(input_file):
|
||||
try:
|
||||
with open(input_file, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
return "void main" in content
|
||||
except Exception as e:
|
||||
print(f"Error parsing {input_file}: {str(e)}")
|
||||
return False
|
||||
|
||||
def compile_glsl(input_file, output_dir, glslang_validator):
|
||||
try:
|
||||
if not has_glsl_main(input_file):
|
||||
print(f"Skipping {input_file}: No main function found")
|
||||
return True
|
||||
|
||||
base = os.path.splitext(os.path.basename(input_file))[0]
|
||||
ext = os.path.splitext(input_file)[1][1:]
|
||||
relative_path = os.path.relpath(os.path.dirname(input_file), args.input_dir)
|
||||
output_subdir = os.path.join(output_dir, relative_path)
|
||||
output_file = os.path.join(output_subdir, f"{base}.{ext}.spv")
|
||||
abs_path = os.path.abspath(output_dir)
|
||||
output_file = os.path.join(abs_path, f"{base}.{ext}.spv")
|
||||
|
||||
os.makedirs(output_subdir, exist_ok=True)
|
||||
os.makedirs(abs_path, exist_ok=True)
|
||||
|
||||
cmd = [glslang_validator, "-V", input_file, "-o", output_file]
|
||||
result = subprocess.run(cmd, check=True, capture_output=True, text=True)
|
||||
@@ -34,7 +46,7 @@ def compile_glsl(input_file, output_dir, glslang_validator):
|
||||
|
||||
def find_slang_entries(input_file):
|
||||
try:
|
||||
with open(input_file, 'r') as f:
|
||||
with open(input_file, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
pattern = r'\[shader\(\s*"(?:\w+)"\s*\)\]\s*\n\s*\w+\s+(\w+)\s*\('
|
||||
return list(set(re.findall(pattern, content)))
|
||||
@@ -46,17 +58,18 @@ def compile_slang(input_file, output_dir, slangc):
|
||||
try:
|
||||
entries = find_slang_entries(input_file)
|
||||
if not entries:
|
||||
print(f"No entries found in {input_file}")
|
||||
return False
|
||||
print(f"Skipping {input_file}: No shader entries found")
|
||||
return True
|
||||
|
||||
base = os.path.splitext(os.path.basename(input_file))[0]
|
||||
relative_path = os.path.relpath(os.path.dirname(input_file), args.input_dir)
|
||||
output_subdir = os.path.join(output_dir, relative_path)
|
||||
os.makedirs(output_subdir, exist_ok=True)
|
||||
|
||||
abs_path = os.path.abspath(output_dir)
|
||||
|
||||
os.makedirs(abs_path, exist_ok=True)
|
||||
|
||||
success = True
|
||||
for entry in entries:
|
||||
output_file = os.path.join(output_subdir, f"{base}_{entry}.spv")
|
||||
output_file = os.path.join(abs_path, f"{base}_{entry}.spv")
|
||||
cmd = [slangc, input_file, "-entry", entry, "-o", output_file, "-target", "spirv"]
|
||||
try:
|
||||
subprocess.run(cmd, check=True, capture_output=True, text=True)
|
||||
@@ -72,26 +85,32 @@ def compile_slang(input_file, output_dir, slangc):
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Compile shaders to SPIR-V")
|
||||
parser.add_argument("--input-dir", required=True, help="Input directory containing shaders")
|
||||
parser.add_argument("--output-dir", required=True, help="Output directory for SPIR-V files")
|
||||
parser.add_argument("--output-dir", help="Output directory for SPIR-V files")
|
||||
parser.add_argument("--glslang", default="glslangValidator", help="Path to glslangValidator")
|
||||
parser.add_argument("--slangc", default="slangc", help="Path to slangc")
|
||||
args = parser.parse_args()
|
||||
|
||||
output_dir = args.output_dir or "shaders"
|
||||
|
||||
# 读取当前同级目录下的shader_paths.txt
|
||||
with open("shader_paths.txt", 'r') as f:
|
||||
shader_paths = f.readlines()
|
||||
|
||||
glsl_ext = [".vert", ".frag", ".comp", ".geom", ".tesc", ".tese", ".glsl"]
|
||||
slang_ext = [".slang"]
|
||||
|
||||
all_success = True
|
||||
|
||||
# Compile GLSL
|
||||
for file in find_shader_files(args.input_dir, glsl_ext):
|
||||
if not compile_glsl(file, args.output_dir, args.glang):
|
||||
all_success = False
|
||||
for shader_path in shader_paths:
|
||||
# Compile GLSL
|
||||
for file in find_shader_files(shader_path, glsl_ext):
|
||||
if not compile_glsl(file, output_dir, args.glslang):
|
||||
all_success = False
|
||||
|
||||
# Compile Slang
|
||||
for file in find_shader_files(args.input_dir, slang_ext):
|
||||
if not compile_slang(file, args.output_dir, args.slangc):
|
||||
all_success = False
|
||||
# Compile Slang
|
||||
for file in find_shader_files(shader_path, slang_ext):
|
||||
if not compile_slang(file, output_dir, args.slangc):
|
||||
all_success = False
|
||||
|
||||
if not all_success:
|
||||
sys.exit(1)
|
||||
|
||||
Reference in New Issue
Block a user