创建临时文件来供调试

This commit is contained in:
daiqingshuang
2025-06-12 11:36:25 +08:00
parent faae9a6195
commit ccfb6bfe12
2 changed files with 10 additions and 11 deletions

View File

@@ -42,19 +42,21 @@ class SDL3GPUSlangCompiler:
# 生成带绑定信息的着色器代码
modified_source = self.binding_manager.inject_bindings(shader_info, target)
temp_source = os.path.join(global_vars.source_path, global_vars.source_file_name + '.slang.temp')
# 写入临时文件
with tempfile.NamedTemporaryFile(mode='w', suffix='.slang', delete=False, encoding='utf8') as tmp:
tmp.write(modified_source)
tmp_path = tmp.name
with open(temp_source, 'w', encoding='utf8') as f:
f.write(modified_source)
with tempfile.NamedTemporaryFile(mode='w', suffix='.cso', delete=False, encoding='utf8') as tmp:
signed_shader = tmp.name
try:
# 编译着色器
if target == TargetFormat.DXIL:
cmd = make_cmd(tmp_path, TargetFormat.HLSL_DX12, shader_info.stage, shader_info.entry_point, output_path)
cmd = make_cmd(temp_source, TargetFormat.HLSL_DX12, shader_info.stage, shader_info.entry_point, output_path)
else:
cmd = make_cmd(tmp_path, target, shader_info.stage, shader_info.entry_point, output_path)
cmd = make_cmd(temp_source, target, shader_info.stage, shader_info.entry_point, output_path)
print(f"Compiling shader with command: {' '.join(cmd)}")
shader_file = output_path
@@ -74,13 +76,13 @@ class SDL3GPUSlangCompiler:
finally:
# 清理临时文件
os.unlink(tmp_path)
os.unlink(signed_shader)
os.unlink(output_path)
def generate_binding_functions(self, binding_infos: List[Dict], output_path: str):
"""生成C/C++绑定函数"""
self.code_generator.generate_binding_functions(binding_infos, output_path)
def _compile_dxil(self, shader_info: ShaderInfo, input_file: str, output_file: str):
"""编译DXIL着色器"""
# 根据stage选择不同的-T参数
@@ -107,4 +109,4 @@ class SDL3GPUSlangCompiler:
if result.stderr is None:
print(f"Signed shader compiled successfully")
else:
raise RuntimeError(f"Failed to compile DXIL shader: {result.stderr.decode('utf-8')}")
raise RuntimeError(f"Failed to sign DXIL shader: {result.stderr.decode('utf-8')}")

View File

@@ -16,7 +16,7 @@ def main():
parser = argparse.ArgumentParser(description='SDL3 GPU Slang Compiler')
parser.add_argument('input', help='Input Slang shader file')
parser.add_argument('-t', '--target', choices=['spirv', 'dxil', 'dxbc', 'msl'],
parser.add_argument('-t', '--target', choices=['spirv', 'dxil', 'msl'],
required=True, help='Target shader format')
parser.add_argument('-o', '--output-dir', required=True, help='Output path for binding code')
parser.add_argument('-i', '--include-dir', help='Include path for slang shader')
@@ -31,9 +31,6 @@ def main():
global_vars.source_path = os.path.dirname(input_path)
global_vars.output_dir = os.path.abspath(args.output_dir)
global_vars.target = TargetFormat(args.target)
# if global_vars.target == TargetFormat.DXIL:
# global_vars.target = TargetFormat.DXBC
# print("Warning: DXIL target is not supported, using DXBC instead.")
# 仅保留路径部分
include_dirs = [