|
| 1 | +import sys |
| 2 | +import os |
| 3 | +import re |
| 4 | +import struct |
| 5 | +import subprocess |
| 6 | + |
| 7 | +remove_temp_file = False |
| 8 | + |
| 9 | +def create_temp_file(shader_file): |
| 10 | + with open(shader_file, 'r') as f: |
| 11 | + shader_file_data = f.read() |
| 12 | + |
| 13 | + match = re.search(r'\{([^}]*)\}', shader_file_data, re.DOTALL) |
| 14 | + if not match: |
| 15 | + return '' |
| 16 | + |
| 17 | + array_text = match.group(1) |
| 18 | + array_text = re.sub(r'//.*', '', array_text) |
| 19 | + array_text = re.sub(r'/\*.*?\*/', '', array_text, flags=re.DOTALL) |
| 20 | + int_strings = [s.strip() for s in array_text.split(',') if s.strip()] |
| 21 | + int_values = [int(s, 0) for s in int_strings] |
| 22 | + |
| 23 | + spv_filename = shader_file + '.spv' |
| 24 | + with open(spv_filename, "wb") as out: |
| 25 | + out.write(struct.pack('<%dI' % len(int_values), *int_values)) |
| 26 | + |
| 27 | + return spv_filename |
| 28 | + |
| 29 | +def validate_shader(path_to_shaders, shader_file): |
| 30 | + if not os.path.exists(os.path.join(path_to_shaders, shader_file)): |
| 31 | + print(os.path.join(path_to_shaders, shader_file) + ' not found') |
| 32 | + return |
| 33 | + if shader_file.endswith('.h'): |
| 34 | + spv_file = create_temp_file(os.path.join(path_to_shaders, shader_file)) |
| 35 | + else: |
| 36 | + spv_file = os.path.join(path_to_shaders, shader_file) |
| 37 | + if len(spv_file) > 0 and os.path.exists(spv_file): |
| 38 | + res = subprocess.Popen([spirv_val_path, '--scalar-block-layout', '--target-env', 'vulkan1.4', spv_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) |
| 39 | + stdout,stderr = res.communicate() |
| 40 | + if len(stdout) > 0 or len(stderr) > 0: |
| 41 | + print(spv_file) |
| 42 | + if len(stdout) > 0: |
| 43 | + print('SPIRV-val: ' + stdout) |
| 44 | + if len(stderr) > 0: |
| 45 | + print('SPIRV-val: ' + stderr) |
| 46 | + exit(1) |
| 47 | + if remove_temp_file: |
| 48 | + os.remove(spv_file) |
| 49 | + |
| 50 | +if len(sys.argv) != 2: |
| 51 | + print(sys.argv[0] + ' <path to spv files>') |
| 52 | + exit(1) |
| 53 | + |
| 54 | +# path to spirv-val provided by a build of spirv-tools in packman |
| 55 | +spirv_val_path = os.path.join('..', 'external', 'spirv_tools', 'spirv-val.exe') |
| 56 | +if not os.path.exists(spirv_val_path): |
| 57 | + print('spirv-val not found at ' + spirv_val_path) |
| 58 | + exit(1) |
| 59 | + |
| 60 | +if sys.argv[1].endswith('.h') or sys.argv[1].endswith('.spv'): |
| 61 | + validate_shader('', sys.argv[1]) |
0 commit comments