diff --git a/compiler.py b/compiler.py index 1b7c7e0..565b366 100644 --- a/compiler.py +++ b/compiler.py @@ -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')}") \ No newline at end of file + raise RuntimeError(f"Failed to sign DXIL shader: {result.stderr.decode('utf-8')}") diff --git a/main.py b/main.py index 98c2bf6..7be13d0 100644 --- a/main.py +++ b/main.py @@ -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 = [