Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import sys |
| 4 | import os |
| 5 | import subprocess |
| 6 | import tempfile |
| 7 | import re |
| 8 | import itertools |
| 9 | import hashlib |
| 10 | import shutil |
| 11 | |
| 12 | def parse_stats(stats): |
| 13 | m = re.search('([0-9]+) work registers', stats) |
| 14 | registers = int(m.group(1)) if m else 0 |
| 15 | |
| 16 | m = re.search('([0-9]+) uniform registers', stats) |
| 17 | uniform_regs = int(m.group(1)) if m else 0 |
| 18 | |
| 19 | m_list = re.findall('(-?[0-9]+)\s+(-?[0-9]+)\s+(-?[0-9]+)', stats) |
| 20 | alu_short = float(m_list[1][0]) if m_list else 0 |
| 21 | ls_short = float(m_list[1][1]) if m_list else 0 |
| 22 | tex_short = float(m_list[1][2]) if m_list else 0 |
| 23 | alu_long = float(m_list[2][0]) if m_list else 0 |
| 24 | ls_long = float(m_list[2][1]) if m_list else 0 |
| 25 | tex_long = float(m_list[2][2]) if m_list else 0 |
| 26 | |
| 27 | return (registers, uniform_regs, alu_short, ls_short, tex_short, alu_long, ls_long, tex_long) |
| 28 | |
| 29 | def get_shader_type(shader): |
| 30 | _, ext = os.path.splitext(shader) |
| 31 | if ext == '.vert': |
| 32 | return '--vertex' |
| 33 | elif ext == '.frag': |
| 34 | return '--fragment' |
| 35 | elif ext == '.comp': |
| 36 | return '--compute' |
| 37 | elif ext == '.tesc': |
| 38 | return '--tessellation_control' |
| 39 | elif ext == '.tese': |
| 40 | return '--tessellation_evaluation' |
| 41 | elif ext == '.geom': |
| 42 | return '--geometry' |
| 43 | else: |
| 44 | return '' |
| 45 | |
| 46 | def get_shader_stats(shader): |
| 47 | f, path = tempfile.mkstemp() |
| 48 | |
| 49 | os.close(f) |
| 50 | p = subprocess.Popen(['malisc', get_shader_type(shader), '--core', 'Mali-T760', '-V', shader], stdout = subprocess.PIPE, stderr = subprocess.PIPE) |
| 51 | stdout, stderr = p.communicate() |
| 52 | os.remove(path) |
| 53 | |
| 54 | if p.returncode != 0: |
| 55 | print(stderr.decode('utf-8')) |
| 56 | raise OSError('malisc failed') |
| 57 | p.wait() |
| 58 | |
| 59 | returned = stdout.decode('utf-8') |
| 60 | return parse_stats(returned) |
| 61 | |
| 62 | def cross_compile(shader): |
| 63 | spirv_f, spirv_path = tempfile.mkstemp() |
| 64 | glsl_f, glsl_path = tempfile.mkstemp(suffix = os.path.basename(shader)) |
| 65 | os.close(spirv_f) |
| 66 | os.close(glsl_f) |
| 67 | |
| 68 | subprocess.check_call(['glslangValidator', '-G', '-o', spirv_path, shader]) |
| 69 | subprocess.check_call(['./spir2cross', '--output', glsl_path, spirv_path]) |
| 70 | return (spirv_path, glsl_path) |
| 71 | |
| 72 | def md5_for_file(path): |
| 73 | md5 = hashlib.md5() |
| 74 | with open(path, 'rb') as f: |
| 75 | for chunk in iter(lambda: f.read(8192), b''): |
| 76 | md5.update(chunk) |
| 77 | return md5.digest() |
| 78 | |
| 79 | def make_reference_dir(path): |
| 80 | base = os.path.dirname(path) |
| 81 | if not os.path.exists(base): |
| 82 | os.makedirs(base) |
| 83 | |
| 84 | def regression_check(shader, glsl): |
| 85 | reference = os.path.join('./reference', shader) |
| 86 | if os.path.exists(reference): |
| 87 | if md5_for_file(glsl) != md5_for_file(reference): |
| 88 | print('Generated GLSL has changed for {}!'.format(reference)) |
| 89 | if os.path.exists(reference): |
| 90 | os.remove(reference) |
| 91 | make_reference_dir(reference) |
| 92 | shutil.move(glsl, reference) |
| 93 | else: |
| 94 | os.remove(glsl) |
| 95 | else: |
| 96 | print('Found new shader {}. Placing GLSL in {}'.format(shader, reference)) |
| 97 | make_reference_dir(reference) |
| 98 | shutil.move(glsl, reference) |
| 99 | |
| 100 | def test_shader(stats, shader): |
| 101 | print('Testing shader:', shader) |
| 102 | pristine_stats = get_shader_stats(shader) |
| 103 | spirv, glsl = cross_compile(shader) |
| 104 | cross_stats = get_shader_stats(glsl) |
| 105 | |
| 106 | regression_check(shader, glsl) |
| 107 | os.remove(spirv) |
| 108 | |
| 109 | a = [] |
| 110 | a.append(shader) |
| 111 | for i in pristine_stats: |
| 112 | a.append(str(i)) |
| 113 | for i in cross_stats: |
| 114 | a.append(str(i)) |
| 115 | print(','.join(a), file = stats) |
| 116 | |
| 117 | def test_shaders(shader_dir): |
| 118 | with open('stats.csv', 'w') as stats: |
| 119 | print('Shader,OrigRegs,OrigUniRegs,OrigALUShort,OrigLSShort,OrigTEXShort,OrigALULong,OrigLSLong,OrigTEXLong,CrossRegs,CrossUniRegs,CrossALUShort,CrossLSShort,CrossTEXShort,CrossALULong,CrossLSLong,CrossTEXLong', file = stats) |
| 120 | for f in os.walk(os.path.join(shader_dir)): |
| 121 | for i in f[2]: |
| 122 | shader = os.path.join(f[0], i) |
| 123 | test_shader(stats, shader) |
| 124 | |
| 125 | if __name__ == '__main__': |
| 126 | test_shaders(sys.argv[1]) |
| 127 | print('Stats in stats.csv!') |