Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metal shader to bit code #27

Open
Youlean opened this issue Feb 3, 2023 · 2 comments
Open

Metal shader to bit code #27

Youlean opened this issue Feb 3, 2023 · 2 comments

Comments

@Youlean
Copy link

Youlean commented Feb 3, 2023

Hey, how do you compile metal shader to bit code?

@Youlean
Copy link
Author

Youlean commented Feb 4, 2023

I wrote the python script for that. You may add this to the source code as it can be useful:

import subprocess

def compile_metal_shader(input_file, metallib_file):
    compile_command = [
        "xcrun",
        "-sdk",
        "macosx",
        "Metal",
        "-c",
        input_file,
        "-o",
        "temp.air",
        "-mmacosx-version-min=10.11"
    ]
    subprocess.run(compile_command, check=True)
    
    metallib_command = [
        "xcrun",
        "-sdk",
        "macosx",
        "metallib",
        "temp.air",
        "-o",
        metallib_file
    ]
    subprocess.run(metallib_command, check=True)

def metallib_to_cpp(metallib_file, output_file):
    with open(metallib_file, 'rb') as f:
        metallib = f.read()

    with open(output_file, 'w') as f:
        f.write("const unsigned char mnvg_bitcode_macos[] = {\n")
        for i in range(0, len(metallib), 16):
            f.write("    ")
            f.write(", ".join(f"0x{b:02x}" for b in metallib[i:i + 16]))
            f.write(",\n")
        f.write("};\n")
        f.write(f"const int mnvg_bitcode_macos_len = {len(metallib)};\n")

if __name__ == "__main__":
    import sys
    if len(sys.argv) != 4:
        print("Usage: python compile_and_convert.py input_file metallib_file output_file")
        sys.exit(1)
    input_file = sys.argv[1]
    metallib_file = sys.argv[2]
    output_file = sys.argv[3]

    compile_metal_shader(input_file, metallib_file)
    metallib_to_cpp(metallib_file, output_file)

@zeromake
Copy link

zeromake commented Feb 9, 2023

project is has similar bash script metallib

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants