#!/user/bin/env python3
# invokes shadercross
# and builds shaders for samples

import os
import subprocess

origDir = os.path.abspath(os.path.dirname(__file__))
shadercrossDir = os.path.join(origDir, 'shadercross')
shadercross = os.path.join(shadercrossDir, 'bin/shadercross')

contentDir = os.path.join(origDir, 'content/')

inputFiles = [os.path.abspath(os.path.join(contentDir, x)) for x in os.listdir(contentDir) if x.endswith('.hlsl')]

def cookAll():
    # general cooking flow I think for shaders is 
    # they will go under 
    cookedRoot = os.path.join(contentDir, '_cooked')

    # we cook into 3 formats 
    # spirv
    # msl
    # dxil

    outputFormats = [
        ('dxil', []),
        ('spv', []),
        ('msl', []),
    ]

    for fmt in outputFormats:
        outdir = os.path.join(cookedRoot, fmt[0])
        for f in inputFiles:
            basefile = f[:-5]
            outfile = os.path.join(outdir, os.path.basename(basefile) + '.' + fmt[0] )
            os.makedirs(os.path.dirname(outfile), exist_ok=True)
            cmd = [shadercross, f] + \
                fmt[1] + [ '-o', outfile ]

            print(cmd)
            subprocess.run(cmd)

if __name__ == '__main__':
    cookAll()
