2016-03-02 17:09:16 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import tempfile
|
|
|
|
import re
|
|
|
|
import itertools
|
|
|
|
import hashlib
|
|
|
|
import shutil
|
2016-03-22 13:44:12 +00:00
|
|
|
import argparse
|
2016-03-02 17:09:16 +00:00
|
|
|
|
|
|
|
def parse_stats(stats):
|
|
|
|
m = re.search('([0-9]+) work registers', stats)
|
|
|
|
registers = int(m.group(1)) if m else 0
|
|
|
|
|
|
|
|
m = re.search('([0-9]+) uniform registers', stats)
|
|
|
|
uniform_regs = int(m.group(1)) if m else 0
|
|
|
|
|
|
|
|
m_list = re.findall('(-?[0-9]+)\s+(-?[0-9]+)\s+(-?[0-9]+)', stats)
|
|
|
|
alu_short = float(m_list[1][0]) if m_list else 0
|
|
|
|
ls_short = float(m_list[1][1]) if m_list else 0
|
|
|
|
tex_short = float(m_list[1][2]) if m_list else 0
|
|
|
|
alu_long = float(m_list[2][0]) if m_list else 0
|
|
|
|
ls_long = float(m_list[2][1]) if m_list else 0
|
|
|
|
tex_long = float(m_list[2][2]) if m_list else 0
|
|
|
|
|
|
|
|
return (registers, uniform_regs, alu_short, ls_short, tex_short, alu_long, ls_long, tex_long)
|
|
|
|
|
|
|
|
def get_shader_type(shader):
|
|
|
|
_, ext = os.path.splitext(shader)
|
|
|
|
if ext == '.vert':
|
|
|
|
return '--vertex'
|
|
|
|
elif ext == '.frag':
|
|
|
|
return '--fragment'
|
|
|
|
elif ext == '.comp':
|
|
|
|
return '--compute'
|
|
|
|
elif ext == '.tesc':
|
|
|
|
return '--tessellation_control'
|
|
|
|
elif ext == '.tese':
|
|
|
|
return '--tessellation_evaluation'
|
|
|
|
elif ext == '.geom':
|
|
|
|
return '--geometry'
|
|
|
|
else:
|
|
|
|
return ''
|
|
|
|
|
|
|
|
def get_shader_stats(shader):
|
|
|
|
f, path = tempfile.mkstemp()
|
|
|
|
|
|
|
|
os.close(f)
|
|
|
|
p = subprocess.Popen(['malisc', get_shader_type(shader), '--core', 'Mali-T760', '-V', shader], stdout = subprocess.PIPE, stderr = subprocess.PIPE)
|
|
|
|
stdout, stderr = p.communicate()
|
|
|
|
os.remove(path)
|
|
|
|
|
|
|
|
if p.returncode != 0:
|
|
|
|
print(stderr.decode('utf-8'))
|
|
|
|
raise OSError('malisc failed')
|
|
|
|
p.wait()
|
|
|
|
|
|
|
|
returned = stdout.decode('utf-8')
|
|
|
|
return parse_stats(returned)
|
|
|
|
|
2016-04-19 09:13:47 +00:00
|
|
|
def validate_shader(shader, vulkan):
|
|
|
|
if vulkan:
|
|
|
|
subprocess.check_call(['glslangValidator', '-V', shader])
|
|
|
|
else:
|
|
|
|
subprocess.check_call(['glslangValidator', shader])
|
2016-03-22 13:44:12 +00:00
|
|
|
|
2016-05-10 21:39:41 +00:00
|
|
|
def cross_compile(shader, vulkan, spirv):
|
2016-03-02 17:09:16 +00:00
|
|
|
spirv_f, spirv_path = tempfile.mkstemp()
|
|
|
|
glsl_f, glsl_path = tempfile.mkstemp(suffix = os.path.basename(shader))
|
|
|
|
os.close(spirv_f)
|
|
|
|
os.close(glsl_f)
|
|
|
|
|
2016-05-10 21:39:41 +00:00
|
|
|
if vulkan or spirv:
|
2016-05-05 08:16:22 +00:00
|
|
|
vulkan_glsl_f, vulkan_glsl_path = tempfile.mkstemp(suffix = os.path.basename(shader))
|
|
|
|
os.close(vulkan_glsl_f)
|
|
|
|
|
2016-05-10 21:39:41 +00:00
|
|
|
if spirv:
|
|
|
|
subprocess.check_call(['spirv-as', '-o', spirv_path, shader])
|
|
|
|
else:
|
|
|
|
subprocess.check_call(['glslangValidator', '-V' if vulkan else '-G', '-o', spirv_path, shader])
|
|
|
|
|
2016-05-23 10:25:09 +00:00
|
|
|
# Workaround Issue #217 in SPIRV-Tools until the issue is resolved.
|
|
|
|
#if spirv:
|
|
|
|
# subprocess.check_call(['spirv-val', spirv_path])
|
2016-05-05 08:16:22 +00:00
|
|
|
|
2016-05-11 17:39:38 +00:00
|
|
|
spirv_cross_path = './spirv-cross'
|
|
|
|
subprocess.check_call([spirv_cross_path, '--output', glsl_path, spirv_path])
|
2016-05-05 08:16:22 +00:00
|
|
|
|
2016-05-11 17:55:57 +00:00
|
|
|
# A shader might not be possible to make valid GLSL from, skip validation for this case.
|
2016-05-10 21:39:41 +00:00
|
|
|
if (not ('nocompat' in glsl_path)) and (not spirv):
|
2016-05-05 08:16:22 +00:00
|
|
|
validate_shader(glsl_path, False)
|
|
|
|
|
2016-05-10 21:39:41 +00:00
|
|
|
if vulkan or spirv:
|
2016-05-11 17:39:38 +00:00
|
|
|
subprocess.check_call([spirv_cross_path, '--vulkan-semantics', '--output', vulkan_glsl_path, spirv_path])
|
2016-05-05 08:16:22 +00:00
|
|
|
validate_shader(vulkan_glsl_path, vulkan)
|
|
|
|
|
|
|
|
return (spirv_path, glsl_path, vulkan_glsl_path if vulkan else None)
|
2016-03-02 17:09:16 +00:00
|
|
|
|
|
|
|
def md5_for_file(path):
|
|
|
|
md5 = hashlib.md5()
|
|
|
|
with open(path, 'rb') as f:
|
|
|
|
for chunk in iter(lambda: f.read(8192), b''):
|
|
|
|
md5.update(chunk)
|
|
|
|
return md5.digest()
|
|
|
|
|
|
|
|
def make_reference_dir(path):
|
|
|
|
base = os.path.dirname(path)
|
|
|
|
if not os.path.exists(base):
|
|
|
|
os.makedirs(base)
|
|
|
|
|
2016-05-11 17:39:38 +00:00
|
|
|
def reference_path(directory, relpath):
|
|
|
|
split_paths = os.path.split(directory)
|
|
|
|
reference_dir = os.path.join(split_paths[0], 'reference/')
|
|
|
|
reference_dir = os.path.join(reference_dir, split_paths[1])
|
|
|
|
return os.path.join(reference_dir, relpath)
|
|
|
|
|
2016-03-22 13:56:50 +00:00
|
|
|
def regression_check(shader, glsl, update, keep):
|
2016-05-11 17:39:38 +00:00
|
|
|
reference = reference_path(shader[0], shader[1])
|
|
|
|
joined_path = os.path.join(shader[0], shader[1])
|
|
|
|
print('Reference shader path:', reference)
|
|
|
|
|
2016-03-02 17:09:16 +00:00
|
|
|
if os.path.exists(reference):
|
|
|
|
if md5_for_file(glsl) != md5_for_file(reference):
|
2016-03-22 13:44:12 +00:00
|
|
|
if update:
|
2016-03-22 13:56:50 +00:00
|
|
|
print('Generated GLSL has changed for {}!'.format(reference))
|
2016-03-22 13:44:12 +00:00
|
|
|
# If we expect changes, update the reference file.
|
|
|
|
if os.path.exists(reference):
|
|
|
|
os.remove(reference)
|
|
|
|
make_reference_dir(reference)
|
|
|
|
shutil.move(glsl, reference)
|
|
|
|
else:
|
2016-03-22 13:56:50 +00:00
|
|
|
print('Generated GLSL in {} does not match reference {}!'.format(glsl, reference))
|
|
|
|
# Otherwise, fail the test. Keep the shader file around so we can inspect.
|
|
|
|
if not keep:
|
|
|
|
os.remove(glsl)
|
2016-03-22 13:44:12 +00:00
|
|
|
sys.exit(1)
|
2016-03-02 17:09:16 +00:00
|
|
|
else:
|
|
|
|
os.remove(glsl)
|
|
|
|
else:
|
2016-05-11 17:39:38 +00:00
|
|
|
print('Found new shader {}. Placing GLSL in {}'.format(joined_path, reference))
|
2016-03-02 17:09:16 +00:00
|
|
|
make_reference_dir(reference)
|
|
|
|
shutil.move(glsl, reference)
|
|
|
|
|
2016-05-11 17:55:57 +00:00
|
|
|
def shader_is_vulkan(shader):
|
|
|
|
return '.vk.' in shader
|
|
|
|
|
2016-07-11 10:47:46 +00:00
|
|
|
def shader_is_desktop(shader):
|
|
|
|
return '.desktop.' in shader
|
|
|
|
|
2016-05-10 21:39:41 +00:00
|
|
|
def shader_is_spirv(shader):
|
|
|
|
return '.asm.' in shader
|
|
|
|
|
2016-05-11 17:55:57 +00:00
|
|
|
def test_shader(stats, shader, update, keep):
|
2016-05-11 17:39:38 +00:00
|
|
|
joined_path = os.path.join(shader[0], shader[1])
|
2016-05-11 17:55:57 +00:00
|
|
|
vulkan = shader_is_vulkan(shader[1])
|
2016-07-11 10:47:46 +00:00
|
|
|
desktop = shader_is_desktop(shader[1])
|
2016-05-10 21:39:41 +00:00
|
|
|
spirv = shader_is_spirv(shader[1])
|
2016-05-11 17:39:38 +00:00
|
|
|
|
|
|
|
print('Testing shader:', joined_path)
|
2016-05-10 21:39:41 +00:00
|
|
|
spirv, glsl, vulkan_glsl = cross_compile(joined_path, vulkan, spirv)
|
2016-03-02 17:09:16 +00:00
|
|
|
|
2016-05-11 17:55:57 +00:00
|
|
|
# Only test GLSL stats if we have a shader following GL semantics.
|
2016-07-11 10:47:46 +00:00
|
|
|
if stats and (not vulkan) and (not spirv) and (not desktop):
|
2016-03-22 13:44:12 +00:00
|
|
|
cross_stats = get_shader_stats(glsl)
|
|
|
|
|
2016-03-22 13:56:50 +00:00
|
|
|
regression_check(shader, glsl, update, keep)
|
2016-05-05 08:16:22 +00:00
|
|
|
if vulkan_glsl:
|
2016-05-11 17:39:38 +00:00
|
|
|
regression_check((shader[0], shader[1] + '.vk'), vulkan_glsl, update, keep)
|
2016-03-02 17:09:16 +00:00
|
|
|
os.remove(spirv)
|
|
|
|
|
2016-05-11 17:55:57 +00:00
|
|
|
if stats and (not vulkan):
|
2016-05-11 17:39:38 +00:00
|
|
|
pristine_stats = get_shader_stats(joined_path)
|
2016-03-22 13:44:12 +00:00
|
|
|
|
|
|
|
a = []
|
2016-05-11 17:39:38 +00:00
|
|
|
a.append(shader[1])
|
2016-03-22 13:44:12 +00:00
|
|
|
for i in pristine_stats:
|
|
|
|
a.append(str(i))
|
|
|
|
for i in cross_stats:
|
|
|
|
a.append(str(i))
|
|
|
|
print(','.join(a), file = stats)
|
|
|
|
|
2016-05-11 17:55:57 +00:00
|
|
|
def test_shaders_helper(stats, shader_dir, update, malisc, keep):
|
2016-05-11 17:39:38 +00:00
|
|
|
for root, dirs, files in os.walk(os.path.join(shader_dir)):
|
|
|
|
for i in files:
|
|
|
|
path = os.path.join(root, i)
|
|
|
|
relpath = os.path.relpath(path, shader_dir)
|
2016-05-11 17:55:57 +00:00
|
|
|
test_shader(stats, (shader_dir, relpath), update, keep)
|
2016-05-11 17:39:38 +00:00
|
|
|
|
2016-05-11 17:55:57 +00:00
|
|
|
def test_shaders(shader_dir, update, malisc, keep):
|
2016-03-22 13:44:12 +00:00
|
|
|
if malisc:
|
|
|
|
with open('stats.csv', 'w') as stats:
|
|
|
|
print('Shader,OrigRegs,OrigUniRegs,OrigALUShort,OrigLSShort,OrigTEXShort,OrigALULong,OrigLSLong,OrigTEXLong,CrossRegs,CrossUniRegs,CrossALUShort,CrossLSShort,CrossTEXShort,CrossALULong,CrossLSLong,CrossTEXLong', file = stats)
|
2016-05-11 17:55:57 +00:00
|
|
|
test_shaders_helper(stats, shader_dir, update, malisc, keep)
|
2016-03-22 13:44:12 +00:00
|
|
|
else:
|
2016-05-11 17:55:57 +00:00
|
|
|
test_shaders_helper(None, shader_dir, update, malisc, keep)
|
2016-03-22 13:44:12 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser(description = 'Script for regression testing.')
|
|
|
|
parser.add_argument('folder',
|
|
|
|
help = 'Folder containing shader files to test.')
|
|
|
|
parser.add_argument('--update',
|
|
|
|
action = 'store_true',
|
|
|
|
help = 'Updates reference files if there is a mismatch. Use when legitimate changes in output is found.')
|
2016-03-22 13:56:50 +00:00
|
|
|
parser.add_argument('--keep',
|
|
|
|
action = 'store_true',
|
|
|
|
help = 'Leave failed GLSL shaders on disk if they fail regression. Useful for debugging.')
|
2016-03-22 13:44:12 +00:00
|
|
|
parser.add_argument('--malisc',
|
|
|
|
action = 'store_true',
|
2016-04-04 07:36:04 +00:00
|
|
|
help = 'Use malisc offline compiler to determine static cycle counts before and after spirv-cross.')
|
2016-03-22 13:44:12 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if not args.folder:
|
|
|
|
sys.stderr.write('Need shader folder.\n')
|
|
|
|
sys.exit(1)
|
|
|
|
|
2016-05-11 17:55:57 +00:00
|
|
|
test_shaders(args.folder, args.update, args.malisc, args.keep)
|
2016-03-22 13:44:12 +00:00
|
|
|
if args.malisc:
|
|
|
|
print('Stats in stats.csv!')
|
2016-03-22 13:56:50 +00:00
|
|
|
print('Tests completed!')
|
2016-03-02 17:09:16 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2016-03-22 13:44:12 +00:00
|
|
|
main()
|