Slang shader compilation script can now handle multiple stages in one file

This commit is contained in:
Sascha Willems 2025-03-29 09:52:21 +01:00
parent 22ae3726e6
commit 7327a9f4ab

View file

@ -29,6 +29,17 @@ def findCompiler():
sys.exit("Could not find slangc executable on PATH, and was not specified with --slangc") sys.exit("Could not find slangc executable on PATH, and was not specified with --slangc")
def getShaderStages(filename):
stages = []
with open(filename) as f:
filecontent = f.read()
if '[shader("vertex")]' in filecontent:
stages.append("vertex")
if '[shader("fragment")]' in filecontent:
stages.append("fragment")
f.close()
return stages
compiler_path = findCompiler() compiler_path = findCompiler()
print("Found slang compiler at %s", compiler_path) print("Found slang compiler at %s", compiler_path)
@ -39,8 +50,25 @@ for root, dirs, files in os.walk(dir_path):
for file in files: for file in files:
if file.endswith(".slang"): if file.endswith(".slang"):
input_file = os.path.join(root, file) input_file = os.path.join(root, file)
output_file = input_file + ".spv" # Slang can store multiple shader stages in a single file, we need to split into separate SPIR-V files for the sample framework
output_file = output_file.replace(".slang", "") stages = getShaderStages(input_file)
res = subprocess.call("%s %s -profile spirv_1_4 -matrix-layout-column-major -target spirv -o %s -entry %s" % (compiler_path, input_file, output_file, "main"), shell=True) print("Compiling %s" % input_file)
if res != 0: for stage in stages:
sys.exit(res) if (len(stages) > 1):
entry_point = stage + "main"
else:
entry_point = "main"
output_ext = ""
match stage:
case "vertex":
output_ext = ".vert"
case "fragment":
output_ext = ".frag"
output_file = input_file + output_ext + ".spv"
output_file = output_file.replace(".slang", "")
res = subprocess.call("%s %s -profile spirv_1_4 -matrix-layout-column-major -target spirv -o %s -entry %s -stage %s" % (compiler_path, input_file, output_file, entry_point, stage), shell=True)
print(output_file)
# res = subprocess.call("%s %s -profile spirv_1_4 -matrix-layout-column-major -target spirv -o %s -entry %s" % (compiler_path, input_file, output_file, "main"), shell=True)
# if res != 0:
# sys.exit(res)