• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import argparse
2import pathlib
3import os
4import subprocess
5from datetime import datetime
6import sys
7
8
9def main():
10    parser = argparse.ArgumentParser(
11        description='Compile glsl source to spv bytes and generate a C file that export the spv bytes as an array.')
12    parser.add_argument('source_file', type=pathlib.Path)
13    parser.add_argument('target_file', type=str)
14    parser.add_argument('export_symbol', type=str)
15    args = parser.parse_args()
16    vulkan_sdk_path = os.getenv('VULKAN_SDK')
17    if vulkan_sdk_path == None:
18        print("Environment variable VULKAN_SDK not set. Please install the LunarG Vulkan SDK and set VULKAN_SDK accordingly")
19        return
20    vulkan_sdk_path = pathlib.Path(vulkan_sdk_path)
21    glslc_path = vulkan_sdk_path / 'Bin' / 'glslc'
22    if os.name == 'nt':
23        glslc_path = glslc_path.with_suffix('.exe')
24    if not glslc_path.exists():
25        print("Can't find " + str(glslc_path))
26        return
27    if not args.source_file.exists():
28        print("Can't find source file: " + str(args.source_file))
29        return
30
31    with subprocess.Popen([str(glslc_path), str(args.source_file), '-o', '-'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc, open(args.target_file, mode='wt') as target_file:
32        if proc.wait() != 0:
33            print(proc.stderr.read().decode('utf-8'))
34            return
35        spv_bytes = proc.stdout.read()
36        spv_bytes = [int.from_bytes(spv_bytes[i:i + 4], byteorder="little")
37                     for i in range(0, len(spv_bytes), 4)]
38        chunk_size = 4
39        spv_bytes_chunks = [spv_bytes[i:i + chunk_size]
40                            for i in range(0, len(spv_bytes), chunk_size)]
41        spv_bytes_in_c_array = ',\n'.join([', '.join(
42            [f'{spv_byte:#010x}' for spv_byte in spv_bytes_chunk]) for spv_bytes_chunk in spv_bytes_chunks])
43        spv_bytes_in_c_array = f'const uint32_t {args.export_symbol}[] = ' + \
44            '{\n' + spv_bytes_in_c_array + '\n};'
45        comments = f"""// Copyright (C) {datetime.today().year} The Android Open Source Project
46// Copyright (C) {datetime.today().year} Google Inc.
47//
48// Licensed under the Apache License, Version 2.0 (the "License");
49// you may not use this file except in compliance with the License.
50// You may obtain a copy of the License at
51//
52// http://www.apache.org/licenses/LICENSE-2.0
53//
54// Unless required by applicable law or agreed to in writing, software
55// distributed under the License is distributed on an "AS IS" BASIS,
56// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
57// See the License for the specific language governing permissions and
58// limitations under the License.
59
60// Autogenerated module {args.target_file}
61// generated by {" ".join(sys.argv)}
62// Please do not modify directly.\n\n"""
63        prelude = "#include <stdint.h>\n\n"
64        target_file.write(comments)
65        target_file.write(prelude)
66        target_file.write(spv_bytes_in_c_array)
67
68
69if __name__ == '__main__':
70    main()
71