<gh_stars>0
#! /usr/bin/python

import argparse
import string

COPYWRITE = '''
/******************************************************************************
 * Copyright 2017 <NAME> <<EMAIL>>           *
 *                                                                            *
 * Permission is hereby granted, free of charge, to any person obtaining a    *
 * copy of this software and associated documentation files (the "Software"), *
 * to deal in the Software without restriction, including without limitation  *
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
 * and/or sell copies of the Software, and to permit persons to whom the      *
 * Software is furnished to do so, subject to the following conditions:       *
 *                                                                            *
 * The above copyright notice and this permission notice shall be included in *
 * all copies or substantial portions of the Software.                        *
 *                                                                            *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    *
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        *
 * DEALINGS IN THE SOFTWARE.                                                  *
 ******************************************************************************/
'''

HDR = '''
{copywrite}
/******************************************************************************
 * This file is generated from spirv source code, do not edit this file       *
 * Your changes will be lost upon regeneration of this file.                  *
 ******************************************************************************/

#include <stdint.h>
#include <stddef.h>

/*
 * The shader source code length
 */
const size_t g_shader_{shader}_size = {shader_size};

/*
 * The compiled shaders source code
 */
const uint8_t g_shader_{shader}_code[] = {shader_code};
'''

def gen_hdr(args):
    code = "{"
    count = 0

    with open(args.src, "rb") as src:
        for i in src.read():
            if ((count % 10) == 0):
                code += "\n\t"

            code += "%#04x, " % i
            count += 1

    code += "\n}"

    with open(args.dst, 'w+') as dst:
        dst.write(HDR.format(copywrite=COPYWRITE,
                         shader=args.name,
                         shader_size=count,
                         shader_code=code))

def parse_arguments():
    parser = argparse.ArgumentParser(description='Convert a spir-v file to a c header file')
    parser.add_argument('src',
                        help='The input spir-v file');
    parser.add_argument('dst',
                        help='The output header file');
    parser.add_argument('name',
                        help='The shader name, for use in the c header');
    return parser.parse_args()

def main():
    args = parse_arguments()
    gen_hdr(args)
    print("Converted spir-v {src} to header {dst}".format(src=args.src, dst=args.dst))

if __name__ == "__main__":
    main()
