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