| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# Copyright 2015-2020 Arm Limited |
| 4 |
# |
| 5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
# you may not use this file except in compliance with the License. |
| 7 |
# You may obtain a copy of the License at |
| 8 |
# |
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
# |
| 11 |
# Unless required by applicable law or agreed to in writing, software |
| 12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
# See the License for the specific language governing permissions and |
| 15 |
# limitations under the License. |
| 16 |
|
| 17 |
import sys |
| 18 |
import os |
| 19 |
import os.path |
| 20 |
import subprocess |
| 21 |
import tempfile |
| 22 |
import re |
| 23 |
import itertools |
| 24 |
import hashlib |
| 25 |
import shutil |
| 26 |
import argparse |
| 27 |
import codecs |
| 28 |
import json |
| 29 |
import multiprocessing |
| 30 |
import errno |
| 31 |
from functools import partial |
| 32 |
|
| 33 |
class Paths(): |
| 34 |
def __init__(self, spirv_cross, glslang, spirv_as, spirv_val, spirv_opt): |
| 35 |
self.spirv_cross = spirv_cross |
| 36 |
self.glslang = glslang |
| 37 |
self.spirv_as = spirv_as |
| 38 |
self.spirv_val = spirv_val |
| 39 |
self.spirv_opt = spirv_opt |
| 40 |
|
| 41 |
def remove_file(path): |
| 42 |
#print('Removing file:', path) |
| 43 |
os.remove(path) |
| 44 |
|
| 45 |
def create_temporary(suff = ''): |
| 46 |
f, path = tempfile.mkstemp(suffix = suff) |
| 47 |
os.close(f) |
| 48 |
#print('Creating temporary:', path) |
| 49 |
return path |
| 50 |
|
| 51 |
def parse_stats(stats): |
| 52 |
m = re.search('([0-9]+) work registers', stats) |
| 53 |
registers = int(m.group(1)) if m else 0 |
| 54 |
|
| 55 |
m = re.search('([0-9]+) uniform registers', stats) |
| 56 |
uniform_regs = int(m.group(1)) if m else 0 |
| 57 |
|
| 58 |
m_list = re.findall('(-?[0-9]+)\s+(-?[0-9]+)\s+(-?[0-9]+)', stats) |
| 59 |
alu_short = float(m_list[1][0]) if m_list else 0 |
| 60 |
ls_short = float(m_list[1][1]) if m_list else 0 |
| 61 |
tex_short = float(m_list[1][2]) if m_list else 0 |
| 62 |
alu_long = float(m_list[2][0]) if m_list else 0 |
| 63 |
ls_long = float(m_list[2][1]) if m_list else 0 |
| 64 |
tex_long = float(m_list[2][2]) if m_list else 0 |
| 65 |
|
| 66 |
return (registers, uniform_regs, alu_short, ls_short, tex_short, alu_long, ls_long, tex_long) |
| 67 |
|
| 68 |
def get_shader_type(shader): |
| 69 |
_, ext = os.path.splitext(shader) |
| 70 |
if ext == '.vert': |
| 71 |
return '--vertex' |
| 72 |
elif ext == '.frag': |
| 73 |
return '--fragment' |
| 74 |
elif ext == '.comp': |
| 75 |
return '--compute' |
| 76 |
elif ext == '.tesc': |
| 77 |
return '--tessellation_control' |
| 78 |
elif ext == '.tese': |
| 79 |
return '--tessellation_evaluation' |
| 80 |
elif ext == '.geom': |
| 81 |
return '--geometry' |
| 82 |
else: |
| 83 |
return '' |
| 84 |
|
| 85 |
def get_shader_stats(shader): |
| 86 |
path = create_temporary() |
| 87 |
|
| 88 |
p = subprocess.Popen(['malisc', get_shader_type(shader), '--core', 'Mali-T760', '-V', shader], stdout = subprocess.PIPE, stderr = subprocess.PIPE) |
| 89 |
stdout, stderr = p.communicate() |
| 90 |
remove_file(path) |
| 91 |
|
| 92 |
if p.returncode != 0: |
| 93 |
print(stderr.decode('utf-8')) |
| 94 |
raise OSError('malisc failed') |
| 95 |
p.wait() |
| 96 |
|
| 97 |
returned = stdout.decode('utf-8') |
| 98 |
return parse_stats(returned) |
| 99 |
|
| 100 |
def print_msl_compiler_version(): |
| 101 |
try: |
| 102 |
subprocess.check_call(['xcrun', '--sdk', 'iphoneos', 'metal', '--version']) |
| 103 |
print('...are the Metal compiler characteristics.\n') # display after so xcrun FNF is silent |
| 104 |
except OSError as e: |
| 105 |
if (e.errno != errno.ENOENT): # Ignore xcrun not found error |
| 106 |
raise |
| 107 |
except subprocess.CalledProcessError: |
| 108 |
pass |
| 109 |
|
| 110 |
def msl_compiler_supports_version(version): |
| 111 |
try: |
| 112 |
subprocess.check_call(['xcrun', '--sdk', 'macosx', 'metal', '-x', 'metal', '-std=macos-metal' + version, '-'], |
| 113 |
stdin = subprocess.DEVNULL, stdout = subprocess.DEVNULL, stderr = subprocess.DEVNULL) |
| 114 |
print('Current SDK supports MSL {0}. Enabling validation for MSL {0} shaders.'.format(version)) |
| 115 |
return True |
| 116 |
except OSError as e: |
| 117 |
print('Failed to check if MSL {} is not supported. It probably is not.'.format(version)) |
| 118 |
return False |
| 119 |
except subprocess.CalledProcessError: |
| 120 |
print('Current SDK does NOT support MSL {0}. Disabling validation for MSL {0} shaders.'.format(version)) |
| 121 |
return False |
| 122 |
|
| 123 |
def path_to_msl_standard(shader): |
| 124 |
if '.ios.' in shader: |
| 125 |
if '.msl2.' in shader: |
| 126 |
return '-std=ios-metal2.0' |
| 127 |
elif '.msl21.' in shader: |
| 128 |
return '-std=ios-metal2.1' |
| 129 |
elif '.msl22.' in shader: |
| 130 |
return '-std=ios-metal2.2' |
| 131 |
elif '.msl23.' in shader: |
| 132 |
return '-std=ios-metal2.3' |
| 133 |
elif '.msl11.' in shader: |
| 134 |
return '-std=ios-metal1.1' |
| 135 |
elif '.msl10.' in shader: |
| 136 |
return '-std=ios-metal1.0' |
| 137 |
else: |
| 138 |
return '-std=ios-metal1.2' |
| 139 |
else: |
| 140 |
if '.msl2.' in shader: |
| 141 |
return '-std=macos-metal2.0' |
| 142 |
elif '.msl21.' in shader: |
| 143 |
return '-std=macos-metal2.1' |
| 144 |
elif '.msl22.' in shader: |
| 145 |
return '-std=macos-metal2.2' |
| 146 |
elif '.msl23.' in shader: |
| 147 |
return '-std=macos-metal2.3' |
| 148 |
elif '.msl11.' in shader: |
| 149 |
return '-std=macos-metal1.1' |
| 150 |
else: |
| 151 |
return '-std=macos-metal1.2' |
| 152 |
|
| 153 |
def path_to_msl_standard_cli(shader): |
| 154 |
if '.msl2.' in shader: |
| 155 |
return '20000' |
| 156 |
elif '.msl21.' in shader: |
| 157 |
return '20100' |
| 158 |
elif '.msl22.' in shader: |
| 159 |
return '20200' |
| 160 |
elif '.msl23.' in shader: |
| 161 |
return '20300' |
| 162 |
elif '.msl11.' in shader: |
| 163 |
return '10100' |
| 164 |
else: |
| 165 |
return '10200' |
| 166 |
|
| 167 |
def validate_shader_msl(shader, opt): |
| 168 |
msl_path = reference_path(shader[0], shader[1], opt) |
| 169 |
try: |
| 170 |
if '.ios.' in msl_path: |
| 171 |
msl_os = 'iphoneos' |
| 172 |
else: |
| 173 |
msl_os = 'macosx' |
| 174 |
subprocess.check_call(['xcrun', '--sdk', msl_os, 'metal', '-x', 'metal', path_to_msl_standard(msl_path), '-Werror', '-Wno-unused-variable', msl_path]) |
| 175 |
print('Compiled Metal shader: ' + msl_path) # display after so xcrun FNF is silent |
| 176 |
except OSError as oe: |
| 177 |
if (oe.errno != errno.ENOENT): # Ignore xcrun not found error |
| 178 |
raise |
| 179 |
except subprocess.CalledProcessError: |
| 180 |
print('Error compiling Metal shader: ' + msl_path) |
| 181 |
raise RuntimeError('Failed to compile Metal shader') |
| 182 |
|
| 183 |
def cross_compile_msl(shader, spirv, opt, iterations, paths): |
| 184 |
spirv_path = create_temporary() |
| 185 |
msl_path = create_temporary(os.path.basename(shader)) |
| 186 |
|
| 187 |
spirv_env = 'vulkan1.1spv1.4' if ('.spv14.' in shader) else 'vulkan1.1' |
| 188 |
|
| 189 |
spirv_cmd = [paths.spirv_as, '--target-env', spirv_env, '-o', spirv_path, shader] |
| 190 |
if '.preserve.' in shader: |
| 191 |
spirv_cmd.append('--preserve-numeric-ids') |
| 192 |
|
| 193 |
if spirv: |
| 194 |
subprocess.check_call(spirv_cmd) |
| 195 |
else: |
| 196 |
subprocess.check_call([paths.glslang, '--amb' ,'--target-env', 'vulkan1.1', '-V', '-o', spirv_path, shader]) |
| 197 |
|
| 198 |
if opt and (not shader_is_invalid_spirv(shader)): |
| 199 |
if '.graphics-robust-access.' in shader: |
| 200 |
subprocess.check_call([paths.spirv_opt, '--skip-validation', '-O', '--graphics-robust-access', '-o', spirv_path, spirv_path]) |
| 201 |
else: |
| 202 |
subprocess.check_call([paths.spirv_opt, '--skip-validation', '-O', '-o', spirv_path, spirv_path]) |
| 203 |
|
| 204 |
spirv_cross_path = paths.spirv_cross |
| 205 |
|
| 206 |
msl_args = [spirv_cross_path, '--entry', 'main', '--output', msl_path, spirv_path, '--msl', '--iterations', str(iterations)] |
| 207 |
msl_args.append('--msl-version') |
| 208 |
msl_args.append(path_to_msl_standard_cli(shader)) |
| 209 |
if '.swizzle.' in shader: |
| 210 |
msl_args.append('--msl-swizzle-texture-samples') |
| 211 |
if '.ios.' in shader: |
| 212 |
msl_args.append('--msl-ios') |
| 213 |
if '.pad-fragment.' in shader: |
| 214 |
msl_args.append('--msl-pad-fragment-output') |
| 215 |
if '.capture.' in shader: |
| 216 |
msl_args.append('--msl-capture-output') |
| 217 |
if '.domain.' in shader: |
| 218 |
msl_args.append('--msl-domain-lower-left') |
| 219 |
if '.argument.' in shader: |
| 220 |
msl_args.append('--msl-argument-buffers') |
| 221 |
if '.texture-buffer-native.' in shader: |
| 222 |
msl_args.append('--msl-texture-buffer-native') |
| 223 |
if '.framebuffer-fetch.' in shader: |
| 224 |
msl_args.append('--msl-framebuffer-fetch') |
| 225 |
if '.invariant-float-math.' in shader: |
| 226 |
msl_args.append('--msl-invariant-float-math') |
| 227 |
if '.emulate-cube-array.' in shader: |
| 228 |
msl_args.append('--msl-emulate-cube-array') |
| 229 |
if '.discrete.' in shader: |
| 230 |
# Arbitrary for testing purposes. |
| 231 |
msl_args.append('--msl-discrete-descriptor-set') |
| 232 |
msl_args.append('2') |
| 233 |
msl_args.append('--msl-discrete-descriptor-set') |
| 234 |
msl_args.append('3') |
| 235 |
if '.force-active.' in shader: |
| 236 |
msl_args.append('--msl-force-active-argument-buffer-resources') |
| 237 |
if '.line.' in shader: |
| 238 |
msl_args.append('--emit-line-directives') |
| 239 |
if '.multiview.' in shader: |
| 240 |
msl_args.append('--msl-multiview') |
| 241 |
if '.no-layered.' in shader: |
| 242 |
msl_args.append('--msl-multiview-no-layered-rendering') |
| 243 |
if '.viewfromdev.' in shader: |
| 244 |
msl_args.append('--msl-view-index-from-device-index') |
| 245 |
if '.dispatchbase.' in shader: |
| 246 |
msl_args.append('--msl-dispatch-base') |
| 247 |
if '.dynamic-buffer.' in shader: |
| 248 |
# Arbitrary for testing purposes. |
| 249 |
msl_args.append('--msl-dynamic-buffer') |
| 250 |
msl_args.append('0') |
| 251 |
msl_args.append('0') |
| 252 |
msl_args.append('--msl-dynamic-buffer') |
| 253 |
msl_args.append('1') |
| 254 |
msl_args.append('2') |
| 255 |
if '.inline-block.' in shader: |
| 256 |
# Arbitrary for testing purposes. |
| 257 |
msl_args.append('--msl-inline-uniform-block') |
| 258 |
msl_args.append('0') |
| 259 |
msl_args.append('0') |
| 260 |
if '.device-argument-buffer.' in shader: |
| 261 |
msl_args.append('--msl-device-argument-buffer') |
| 262 |
msl_args.append('0') |
| 263 |
msl_args.append('--msl-device-argument-buffer') |
| 264 |
msl_args.append('1') |
| 265 |
if '.force-native-array.' in shader: |
| 266 |
msl_args.append('--msl-force-native-arrays') |
| 267 |
if '.zero-initialize.' in shader: |
| 268 |
msl_args.append('--force-zero-initialized-variables') |
| 269 |
if '.frag-output.' in shader: |
| 270 |
# Arbitrary for testing purposes. |
| 271 |
msl_args.append('--msl-disable-frag-depth-builtin') |
| 272 |
msl_args.append('--msl-disable-frag-stencil-ref-builtin') |
| 273 |
msl_args.append('--msl-enable-frag-output-mask') |
| 274 |
msl_args.append('0x000000ca') |
| 275 |
if '.no-user-varying.' in shader: |
| 276 |
msl_args.append('--msl-no-clip-distance-user-varying') |
| 277 |
if '.shader-inputs.' in shader: |
| 278 |
# Arbitrary for testing purposes. |
| 279 |
msl_args.append('--msl-shader-input') |
| 280 |
msl_args.append('0') |
| 281 |
msl_args.append('u8') |
| 282 |
msl_args.append('2') |
| 283 |
msl_args.append('--msl-shader-input') |
| 284 |
msl_args.append('1') |
| 285 |
msl_args.append('u16') |
| 286 |
msl_args.append('3') |
| 287 |
msl_args.append('--msl-shader-input') |
| 288 |
msl_args.append('6') |
| 289 |
msl_args.append('other') |
| 290 |
msl_args.append('4') |
| 291 |
if '.multi-patch.' in shader: |
| 292 |
msl_args.append('--msl-multi-patch-workgroup') |
| 293 |
# Arbitrary for testing purposes. |
| 294 |
msl_args.append('--msl-shader-input') |
| 295 |
msl_args.append('0') |
| 296 |
msl_args.append('any32') |
| 297 |
msl_args.append('3') |
| 298 |
msl_args.append('--msl-shader-input') |
| 299 |
msl_args.append('1') |
| 300 |
msl_args.append('any16') |
| 301 |
msl_args.append('2') |
| 302 |
if '.for-tess.' in shader: |
| 303 |
msl_args.append('--msl-vertex-for-tessellation') |
| 304 |
if '.fixed-sample-mask.' in shader: |
| 305 |
msl_args.append('--msl-additional-fixed-sample-mask') |
| 306 |
msl_args.append('0x00000022') |
| 307 |
if '.arrayed-subpass.' in shader: |
| 308 |
msl_args.append('--msl-arrayed-subpass-input') |
| 309 |
if '.1d-as-2d.' in shader: |
| 310 |
msl_args.append('--msl-texture-1d-as-2d') |
| 311 |
|
| 312 |
subprocess.check_call(msl_args) |
| 313 |
|
| 314 |
if not shader_is_invalid_spirv(msl_path): |
| 315 |
subprocess.check_call([paths.spirv_val, '--scalar-block-layout', '--target-env', spirv_env, spirv_path]) |
| 316 |
|
| 317 |
return (spirv_path, msl_path) |
| 318 |
|
| 319 |
def shader_model_hlsl(shader): |
| 320 |
if '.vert' in shader: |
| 321 |
if '.sm30.' in shader: |
| 322 |
return '-Tvs_3_0' |
| 323 |
else: |
| 324 |
return '-Tvs_5_1' |
| 325 |
elif '.frag' in shader: |
| 326 |
if '.sm30.' in shader: |
| 327 |
return '-Tps_3_0' |
| 328 |
else: |
| 329 |
return '-Tps_5_1' |
| 330 |
elif '.comp' in shader: |
| 331 |
return '-Tcs_5_1' |
| 332 |
else: |
| 333 |
return None |
| 334 |
|
| 335 |
def shader_to_win_path(shader): |
| 336 |
# It's (very) convenient to be able to run HLSL testing in wine on Unix-likes, so support that. |
| 337 |
try: |
| 338 |
with subprocess.Popen(['winepath', '-w', shader], stdout = subprocess.PIPE, stderr = subprocess.PIPE) as f: |
| 339 |
stdout_data, stderr_data = f.communicate() |
| 340 |
return stdout_data.decode('utf-8') |
| 341 |
except OSError as oe: |
| 342 |
if (oe.errno != errno.ENOENT): # Ignore not found errors |
| 343 |
return shader |
| 344 |
except subprocess.CalledProcessError: |
| 345 |
raise |
| 346 |
|
| 347 |
return shader |
| 348 |
|
| 349 |
ignore_fxc = False |
| 350 |
def validate_shader_hlsl(shader, force_no_external_validation, paths): |
| 351 |
test_glslang = True |
| 352 |
if '.nonuniformresource.' in shader: |
| 353 |
test_glslang = False |
| 354 |
if '.fxconly.' in shader: |
| 355 |
test_glslang = False |
| 356 |
|
| 357 |
hlsl_args = [paths.glslang, '--amb', '-e', 'main', '-D', '--target-env', 'vulkan1.1', '-V', shader] |
| 358 |
if '.sm30.' in shader: |
| 359 |
hlsl_args.append('--hlsl-dx9-compatible') |
| 360 |
|
| 361 |
if test_glslang: |
| 362 |
subprocess.check_call(hlsl_args) |
| 363 |
|
| 364 |
is_no_fxc = '.nofxc.' in shader |
| 365 |
global ignore_fxc |
| 366 |
if (not ignore_fxc) and (not force_no_external_validation) and (not is_no_fxc): |
| 367 |
try: |
| 368 |
win_path = shader_to_win_path(shader) |
| 369 |
args = ['fxc', '-nologo', shader_model_hlsl(shader), win_path] |
| 370 |
if '.nonuniformresource.' in shader: |
| 371 |
args.append('/enable_unbounded_descriptor_tables') |
| 372 |
subprocess.check_call(args) |
| 373 |
except OSError as oe: |
| 374 |
if (oe.errno != errno.ENOENT): # Ignore not found errors |
| 375 |
print('Failed to run FXC.') |
| 376 |
ignore_fxc = True |
| 377 |
raise |
| 378 |
else: |
| 379 |
print('Could not find FXC.') |
| 380 |
ignore_fxc = True |
| 381 |
except subprocess.CalledProcessError: |
| 382 |
print('Failed compiling HLSL shader:', shader, 'with FXC.') |
| 383 |
raise RuntimeError('Failed compiling HLSL shader') |
| 384 |
|
| 385 |
def shader_to_sm(shader): |
| 386 |
if '.sm62.' in shader: |
| 387 |
return '62' |
| 388 |
elif '.sm60.' in shader: |
| 389 |
return '60' |
| 390 |
elif '.sm51.' in shader: |
| 391 |
return '51' |
| 392 |
elif '.sm30.' in shader: |
| 393 |
return '30' |
| 394 |
else: |
| 395 |
return '50' |
| 396 |
|
| 397 |
def cross_compile_hlsl(shader, spirv, opt, force_no_external_validation, iterations, paths): |
| 398 |
spirv_path = create_temporary() |
| 399 |
hlsl_path = create_temporary(os.path.basename(shader)) |
| 400 |
|
| 401 |
spirv_cmd = [paths.spirv_as, '--target-env', 'vulkan1.1', '-o', spirv_path, shader] |
| 402 |
if '.preserve.' in shader: |
| 403 |
spirv_cmd.append('--preserve-numeric-ids') |
| 404 |
|
| 405 |
if spirv: |
| 406 |
subprocess.check_call(spirv_cmd) |
| 407 |
else: |
| 408 |
subprocess.check_call([paths.glslang, '--amb', '--target-env', 'vulkan1.1', '-V', '-o', spirv_path, shader]) |
| 409 |
|
| 410 |
if opt and (not shader_is_invalid_spirv(hlsl_path)): |
| 411 |
subprocess.check_call([paths.spirv_opt, '--skip-validation', '-O', '-o', spirv_path, spirv_path]) |
| 412 |
|
| 413 |
spirv_cross_path = paths.spirv_cross |
| 414 |
|
| 415 |
sm = shader_to_sm(shader) |
| 416 |
|
| 417 |
hlsl_args = [spirv_cross_path, '--entry', 'main', '--output', hlsl_path, spirv_path, '--hlsl-enable-compat', '--hlsl', '--shader-model', sm, '--iterations', str(iterations)] |
| 418 |
if '.line.' in shader: |
| 419 |
hlsl_args.append('--emit-line-directives') |
| 420 |
if '.force-uav.' in shader: |
| 421 |
hlsl_args.append('--hlsl-force-storage-buffer-as-uav') |
| 422 |
if '.zero-initialize.' in shader: |
| 423 |
hlsl_args.append('--force-zero-initialized-variables') |
| 424 |
if '.nonwritable-uav-texture.' in shader: |
| 425 |
hlsl_args.append('--hlsl-nonwritable-uav-texture-as-srv') |
| 426 |
if '.native-16bit.' in shader: |
| 427 |
hlsl_args.append('--hlsl-enable-16bit-types') |
| 428 |
if '.flatten-matrix-vertex-input.' in shader: |
| 429 |
hlsl_args.append('--hlsl-flatten-matrix-vertex-input-semantics') |
| 430 |
|
| 431 |
subprocess.check_call(hlsl_args) |
| 432 |
|
| 433 |
if not shader_is_invalid_spirv(hlsl_path): |
| 434 |
subprocess.check_call([paths.spirv_val, '--scalar-block-layout', '--target-env', 'vulkan1.1', spirv_path]) |
| 435 |
|
| 436 |
validate_shader_hlsl(hlsl_path, force_no_external_validation, paths) |
| 437 |
|
| 438 |
return (spirv_path, hlsl_path) |
| 439 |
|
| 440 |
def cross_compile_reflect(shader, spirv, opt, iterations, paths): |
| 441 |
spirv_path = create_temporary() |
| 442 |
reflect_path = create_temporary(os.path.basename(shader)) |
| 443 |
|
| 444 |
spirv_cmd = [paths.spirv_as, '--target-env', 'vulkan1.1', '-o', spirv_path, shader] |
| 445 |
if '.preserve.' in shader: |
| 446 |
spirv_cmd.append('--preserve-numeric-ids') |
| 447 |
|
| 448 |
if spirv: |
| 449 |
subprocess.check_call(spirv_cmd) |
| 450 |
else: |
| 451 |
subprocess.check_call([paths.glslang, '--amb', '--target-env', 'vulkan1.1', '-V', '-o', spirv_path, shader]) |
| 452 |
|
| 453 |
if opt and (not shader_is_invalid_spirv(reflect_path)): |
| 454 |
subprocess.check_call([paths.spirv_opt, '--skip-validation', '-O', '-o', spirv_path, spirv_path]) |
| 455 |
|
| 456 |
spirv_cross_path = paths.spirv_cross |
| 457 |
|
| 458 |
sm = shader_to_sm(shader) |
| 459 |
subprocess.check_call([spirv_cross_path, '--entry', 'main', '--output', reflect_path, spirv_path, '--reflect', '--iterations', str(iterations)]) |
| 460 |
return (spirv_path, reflect_path) |
| 461 |
|
| 462 |
def validate_shader(shader, vulkan, paths): |
| 463 |
if vulkan: |
| 464 |
subprocess.check_call([paths.glslang, '--amb', '--target-env', 'vulkan1.1', '-V', shader]) |
| 465 |
else: |
| 466 |
subprocess.check_call([paths.glslang, shader]) |
| 467 |
|
| 468 |
def cross_compile(shader, vulkan, spirv, invalid_spirv, eliminate, is_legacy, flatten_ubo, sso, flatten_dim, opt, push_ubo, iterations, paths): |
| 469 |
spirv_path = create_temporary() |
| 470 |
glsl_path = create_temporary(os.path.basename(shader)) |
| 471 |
|
| 472 |
spirv_env = 'vulkan1.1spv1.4' if ('.spv14.' in shader) else 'vulkan1.1' |
| 473 |
|
| 474 |
if vulkan or spirv: |
| 475 |
vulkan_glsl_path = create_temporary('vk' + os.path.basename(shader)) |
| 476 |
|
| 477 |
spirv_cmd = [paths.spirv_as, '--target-env', spirv_env, '-o', spirv_path, shader] |
| 478 |
if '.preserve.' in shader: |
| 479 |
spirv_cmd.append('--preserve-numeric-ids') |
| 480 |
|
| 481 |
if spirv: |
| 482 |
subprocess.check_call(spirv_cmd) |
| 483 |
else: |
| 484 |
subprocess.check_call([paths.glslang, '--amb', '--target-env', 'vulkan1.1', '-V', '-o', spirv_path, shader]) |
| 485 |
|
| 486 |
if opt and (not invalid_spirv): |
| 487 |
subprocess.check_call([paths.spirv_opt, '--skip-validation', '-O', '-o', spirv_path, spirv_path]) |
| 488 |
|
| 489 |
if not invalid_spirv: |
| 490 |
subprocess.check_call([paths.spirv_val, '--scalar-block-layout', '--target-env', spirv_env, spirv_path]) |
| 491 |
|
| 492 |
extra_args = ['--iterations', str(iterations)] |
| 493 |
if eliminate: |
| 494 |
extra_args += ['--remove-unused-variables'] |
| 495 |
if is_legacy: |
| 496 |
extra_args += ['--version', '100', '--es'] |
| 497 |
if flatten_ubo: |
| 498 |
extra_args += ['--flatten-ubo'] |
| 499 |
if sso: |
| 500 |
extra_args += ['--separate-shader-objects'] |
| 501 |
if flatten_dim: |
| 502 |
extra_args += ['--flatten-multidimensional-arrays'] |
| 503 |
if push_ubo: |
| 504 |
extra_args += ['--glsl-emit-push-constant-as-ubo'] |
| 505 |
if '.line.' in shader: |
| 506 |
extra_args += ['--emit-line-directives'] |
| 507 |
if '.no-samplerless.' in shader: |
| 508 |
extra_args += ['--vulkan-glsl-disable-ext-samplerless-texture-functions'] |
| 509 |
if '.no-qualifier-deduction.' in shader: |
| 510 |
extra_args += ['--disable-storage-image-qualifier-deduction'] |
| 511 |
if '.framebuffer-fetch.' in shader: |
| 512 |
extra_args += ['--glsl-remap-ext-framebuffer-fetch', '0', '0'] |
| 513 |
extra_args += ['--glsl-remap-ext-framebuffer-fetch', '1', '1'] |
| 514 |
extra_args += ['--glsl-remap-ext-framebuffer-fetch', '2', '2'] |
| 515 |
extra_args += ['--glsl-remap-ext-framebuffer-fetch', '3', '3'] |
| 516 |
if '.zero-initialize.' in shader: |
| 517 |
extra_args += ['--force-zero-initialized-variables'] |
| 518 |
if '.force-flattened-io.' in shader: |
| 519 |
extra_args += ['--glsl-force-flattened-io-blocks'] |
| 520 |
|
| 521 |
spirv_cross_path = paths.spirv_cross |
| 522 |
|
| 523 |
# A shader might not be possible to make valid GLSL from, skip validation for this case. |
| 524 |
if (not ('nocompat' in glsl_path)) or (not vulkan): |
| 525 |
subprocess.check_call([spirv_cross_path, '--entry', 'main', '--output', glsl_path, spirv_path] + extra_args) |
| 526 |
if not 'nocompat' in glsl_path: |
| 527 |
validate_shader(glsl_path, False, paths) |
| 528 |
else: |
| 529 |
remove_file(glsl_path) |
| 530 |
glsl_path = None |
| 531 |
|
| 532 |
if (vulkan or spirv) and (not is_legacy): |
| 533 |
subprocess.check_call([spirv_cross_path, '--entry', 'main', '--vulkan-semantics', '--output', vulkan_glsl_path, spirv_path] + extra_args) |
| 534 |
validate_shader(vulkan_glsl_path, True, paths) |
| 535 |
# SPIR-V shaders might just want to validate Vulkan GLSL output, we don't always care about the output. |
| 536 |
if not vulkan: |
| 537 |
remove_file(vulkan_glsl_path) |
| 538 |
|
| 539 |
return (spirv_path, glsl_path, vulkan_glsl_path if vulkan else None) |
| 540 |
|
| 541 |
def make_unix_newline(buf): |
| 542 |
decoded = codecs.decode(buf, 'utf-8') |
| 543 |
decoded = decoded.replace('\r', '') |
| 544 |
return codecs.encode(decoded, 'utf-8') |
| 545 |
|
| 546 |
def md5_for_file(path): |
| 547 |
md5 = hashlib.md5() |
| 548 |
with open(path, 'rb') as f: |
| 549 |
for chunk in iter(lambda: make_unix_newline(f.read(8192)), b''): |
| 550 |
md5.update(chunk) |
| 551 |
return md5.digest() |
| 552 |
|
| 553 |
def make_reference_dir(path): |
| 554 |
base = os.path.dirname(path) |
| 555 |
if not os.path.exists(base): |
| 556 |
os.makedirs(base) |
| 557 |
|
| 558 |
def reference_path(directory, relpath, opt): |
| 559 |
split_paths = os.path.split(directory) |
| 560 |
reference_dir = os.path.join(split_paths[0], 'reference/' + ('opt/' if opt else '')) |
| 561 |
reference_dir = os.path.join(reference_dir, split_paths[1]) |
| 562 |
return os.path.join(reference_dir, relpath) |
| 563 |
|
| 564 |
def regression_check_reflect(shader, json_file, args): |
| 565 |
reference = reference_path(shader[0], shader[1], args.opt) + '.json' |
| 566 |
joined_path = os.path.join(shader[0], shader[1]) |
| 567 |
print('Reference shader reflection path:', reference) |
| 568 |
if os.path.exists(reference): |
| 569 |
actual = md5_for_file(json_file) |
| 570 |
expected = md5_for_file(reference) |
| 571 |
if actual != expected: |
| 572 |
if args.update: |
| 573 |
print('Generated reflection json has changed for {}!'.format(reference)) |
| 574 |
# If we expect changes, update the reference file. |
| 575 |
if os.path.exists(reference): |
| 576 |
remove_file(reference) |
| 577 |
make_reference_dir(reference) |
| 578 |
shutil.move(json_file, reference) |
| 579 |
else: |
| 580 |
print('Generated reflection json in {} does not match reference {}!'.format(json_file, reference)) |
| 581 |
with open(json_file, 'r') as f: |
| 582 |
print('') |
| 583 |
print('Generated:') |
| 584 |
print('======================') |
| 585 |
print(f.read()) |
| 586 |
print('======================') |
| 587 |
print('') |
| 588 |
|
| 589 |
# Otherwise, fail the test. Keep the shader file around so we can inspect. |
| 590 |
if not args.keep: |
| 591 |
remove_file(json_file) |
| 592 |
|
| 593 |
raise RuntimeError('Does not match reference') |
| 594 |
else: |
| 595 |
remove_file(json_file) |
| 596 |
else: |
| 597 |
print('Found new shader {}. Placing generated source code in {}'.format(joined_path, reference)) |
| 598 |
make_reference_dir(reference) |
| 599 |
shutil.move(json_file, reference) |
| 600 |
|
| 601 |
def regression_check(shader, glsl, args): |
| 602 |
reference = reference_path(shader[0], shader[1], args.opt) |
| 603 |
joined_path = os.path.join(shader[0], shader[1]) |
| 604 |
print('Reference shader path:', reference) |
| 605 |
|
| 606 |
if os.path.exists(reference): |
| 607 |
if md5_for_file(glsl) != md5_for_file(reference): |
| 608 |
if args.update: |
| 609 |
print('Generated source code has changed for {}!'.format(reference)) |
| 610 |
# If we expect changes, update the reference file. |
| 611 |
if os.path.exists(reference): |
| 612 |
remove_file(reference) |
| 613 |
make_reference_dir(reference) |
| 614 |
shutil.move(glsl, reference) |
| 615 |
else: |
| 616 |
print('Generated source code in {} does not match reference {}!'.format(glsl, reference)) |
| 617 |
with open(glsl, 'r') as f: |
| 618 |
print('') |
| 619 |
print('Generated:') |
| 620 |
print('======================') |
| 621 |
print(f.read()) |
| 622 |
print('======================') |
| 623 |
print('') |
| 624 |
|
| 625 |
# Otherwise, fail the test. Keep the shader file around so we can inspect. |
| 626 |
if not args.keep: |
| 627 |
remove_file(glsl) |
| 628 |
raise RuntimeError('Does not match reference') |
| 629 |
else: |
| 630 |
remove_file(glsl) |
| 631 |
else: |
| 632 |
print('Found new shader {}. Placing generated source code in {}'.format(joined_path, reference)) |
| 633 |
make_reference_dir(reference) |
| 634 |
shutil.move(glsl, reference) |
| 635 |
|
| 636 |
def shader_is_vulkan(shader): |
| 637 |
return '.vk.' in shader |
| 638 |
|
| 639 |
def shader_is_desktop(shader): |
| 640 |
return '.desktop.' in shader |
| 641 |
|
| 642 |
def shader_is_eliminate_dead_variables(shader): |
| 643 |
return '.noeliminate.' not in shader |
| 644 |
|
| 645 |
def shader_is_spirv(shader): |
| 646 |
return '.asm.' in shader |
| 647 |
|
| 648 |
def shader_is_invalid_spirv(shader): |
| 649 |
return '.invalid.' in shader |
| 650 |
|
| 651 |
def shader_is_legacy(shader): |
| 652 |
return '.legacy.' in shader |
| 653 |
|
| 654 |
def shader_is_flatten_ubo(shader): |
| 655 |
return '.flatten.' in shader |
| 656 |
|
| 657 |
def shader_is_sso(shader): |
| 658 |
return '.sso.' in shader |
| 659 |
|
| 660 |
def shader_is_flatten_dimensions(shader): |
| 661 |
return '.flatten_dim.' in shader |
| 662 |
|
| 663 |
def shader_is_noopt(shader): |
| 664 |
return '.noopt.' in shader |
| 665 |
|
| 666 |
def shader_is_push_ubo(shader): |
| 667 |
return '.push-ubo.' in shader |
| 668 |
|
| 669 |
def test_shader(stats, shader, args, paths): |
| 670 |
joined_path = os.path.join(shader[0], shader[1]) |
| 671 |
vulkan = shader_is_vulkan(shader[1]) |
| 672 |
desktop = shader_is_desktop(shader[1]) |
| 673 |
eliminate = shader_is_eliminate_dead_variables(shader[1]) |
| 674 |
is_spirv = shader_is_spirv(shader[1]) |
| 675 |
invalid_spirv = shader_is_invalid_spirv(shader[1]) |
| 676 |
is_legacy = shader_is_legacy(shader[1]) |
| 677 |
flatten_ubo = shader_is_flatten_ubo(shader[1]) |
| 678 |
sso = shader_is_sso(shader[1]) |
| 679 |
flatten_dim = shader_is_flatten_dimensions(shader[1]) |
| 680 |
noopt = shader_is_noopt(shader[1]) |
| 681 |
push_ubo = shader_is_push_ubo(shader[1]) |
| 682 |
|
| 683 |
print('Testing shader:', joined_path) |
| 684 |
spirv, glsl, vulkan_glsl = cross_compile(joined_path, vulkan, is_spirv, invalid_spirv, eliminate, is_legacy, flatten_ubo, sso, flatten_dim, args.opt and (not noopt), push_ubo, args.iterations, paths) |
| 685 |
|
| 686 |
# Only test GLSL stats if we have a shader following GL semantics. |
| 687 |
if stats and (not vulkan) and (not is_spirv) and (not desktop): |
| 688 |
cross_stats = get_shader_stats(glsl) |
| 689 |
|
| 690 |
if glsl: |
| 691 |
regression_check(shader, glsl, args) |
| 692 |
if vulkan_glsl: |
| 693 |
regression_check((shader[0], shader[1] + '.vk'), vulkan_glsl, args) |
| 694 |
|
| 695 |
remove_file(spirv) |
| 696 |
|
| 697 |
if stats and (not vulkan) and (not is_spirv) and (not desktop): |
| 698 |
pristine_stats = get_shader_stats(joined_path) |
| 699 |
|
| 700 |
a = [] |
| 701 |
a.append(shader[1]) |
| 702 |
for i in pristine_stats: |
| 703 |
a.append(str(i)) |
| 704 |
for i in cross_stats: |
| 705 |
a.append(str(i)) |
| 706 |
print(','.join(a), file = stats) |
| 707 |
|
| 708 |
def test_shader_msl(stats, shader, args, paths): |
| 709 |
joined_path = os.path.join(shader[0], shader[1]) |
| 710 |
print('\nTesting MSL shader:', joined_path) |
| 711 |
is_spirv = shader_is_spirv(shader[1]) |
| 712 |
noopt = shader_is_noopt(shader[1]) |
| 713 |
spirv, msl = cross_compile_msl(joined_path, is_spirv, args.opt and (not noopt), args.iterations, paths) |
| 714 |
regression_check(shader, msl, args) |
| 715 |
|
| 716 |
# Uncomment the following line to print the temp SPIR-V file path. |
| 717 |
# This temp SPIR-V file is not deleted until after the Metal validation step below. |
| 718 |
# If Metal validation fails, the temp SPIR-V file can be copied out and |
| 719 |
# used as input to an invocation of spirv-cross to debug from Xcode directly. |
| 720 |
# To do so, build spriv-cross using `make DEBUG=1`, then run the spriv-cross |
| 721 |
# executable from Xcode using args: `--msl --entry main --output msl_path spirv_path`. |
| 722 |
# print('SPRIV shader: ' + spirv) |
| 723 |
|
| 724 |
shader_is_msl22 = 'msl22' in joined_path |
| 725 |
shader_is_msl23 = 'msl23' in joined_path |
| 726 |
skip_validation = (shader_is_msl22 and (not args.msl22)) or (shader_is_msl23 and (not args.msl23)) |
| 727 |
if '.invalid.' in joined_path: |
| 728 |
skip_validation = True |
| 729 |
|
| 730 |
if (not args.force_no_external_validation) and (not skip_validation): |
| 731 |
validate_shader_msl(shader, args.opt) |
| 732 |
|
| 733 |
remove_file(spirv) |
| 734 |
|
| 735 |
def test_shader_hlsl(stats, shader, args, paths): |
| 736 |
joined_path = os.path.join(shader[0], shader[1]) |
| 737 |
print('Testing HLSL shader:', joined_path) |
| 738 |
is_spirv = shader_is_spirv(shader[1]) |
| 739 |
noopt = shader_is_noopt(shader[1]) |
| 740 |
spirv, hlsl = cross_compile_hlsl(joined_path, is_spirv, args.opt and (not noopt), args.force_no_external_validation, args.iterations, paths) |
| 741 |
regression_check(shader, hlsl, args) |
| 742 |
remove_file(spirv) |
| 743 |
|
| 744 |
def test_shader_reflect(stats, shader, args, paths): |
| 745 |
joined_path = os.path.join(shader[0], shader[1]) |
| 746 |
print('Testing shader reflection:', joined_path) |
| 747 |
is_spirv = shader_is_spirv(shader[1]) |
| 748 |
noopt = shader_is_noopt(shader[1]) |
| 749 |
spirv, reflect = cross_compile_reflect(joined_path, is_spirv, args.opt and (not noopt), args.iterations, paths) |
| 750 |
regression_check_reflect(shader, reflect, args) |
| 751 |
remove_file(spirv) |
| 752 |
|
| 753 |
def test_shader_file(relpath, stats, args, backend): |
| 754 |
paths = Paths(args.spirv_cross, args.glslang, args.spirv_as, args.spirv_val, args.spirv_opt) |
| 755 |
try: |
| 756 |
if backend == 'msl': |
| 757 |
test_shader_msl(stats, (args.folder, relpath), args, paths) |
| 758 |
elif backend == 'hlsl': |
| 759 |
test_shader_hlsl(stats, (args.folder, relpath), args, paths) |
| 760 |
elif backend == 'reflect': |
| 761 |
test_shader_reflect(stats, (args.folder, relpath), args, paths) |
| 762 |
else: |
| 763 |
test_shader(stats, (args.folder, relpath), args, paths) |
| 764 |
return None |
| 765 |
except Exception as e: |
| 766 |
return e |
| 767 |
|
| 768 |
def test_shaders_helper(stats, backend, args): |
| 769 |
all_files = [] |
| 770 |
for root, dirs, files in os.walk(os.path.join(args.folder)): |
| 771 |
files = [ f for f in files if not f.startswith(".") ] #ignore system files (esp OSX) |
| 772 |
for i in files: |
| 773 |
path = os.path.join(root, i) |
| 774 |
relpath = os.path.relpath(path, args.folder) |
| 775 |
all_files.append(relpath) |
| 776 |
|
| 777 |
# The child processes in parallel execution mode don't have the proper state for the global args variable, so |
| 778 |
# at this point we need to switch to explicit arguments |
| 779 |
if args.parallel: |
| 780 |
with multiprocessing.Pool(multiprocessing.cpu_count()) as pool: |
| 781 |
results = [] |
| 782 |
for f in all_files: |
| 783 |
results.append(pool.apply_async(test_shader_file, |
| 784 |
args = (f, stats, args, backend))) |
| 785 |
|
| 786 |
pool.close() |
| 787 |
pool.join() |
| 788 |
results_completed = [res.get() for res in results] |
| 789 |
|
| 790 |
for error in results_completed: |
| 791 |
if error is not None: |
| 792 |
print('Error:', error) |
| 793 |
sys.exit(1) |
| 794 |
|
| 795 |
else: |
| 796 |
for i in all_files: |
| 797 |
e = test_shader_file(i, stats, args, backend) |
| 798 |
if e is not None: |
| 799 |
print('Error:', e) |
| 800 |
sys.exit(1) |
| 801 |
|
| 802 |
def test_shaders(backend, args): |
| 803 |
if args.malisc: |
| 804 |
with open('stats.csv', 'w') as stats: |
| 805 |
print('Shader,OrigRegs,OrigUniRegs,OrigALUShort,OrigLSShort,OrigTEXShort,OrigALULong,OrigLSLong,OrigTEXLong,CrossRegs,CrossUniRegs,CrossALUShort,CrossLSShort,CrossTEXShort,CrossALULong,CrossLSLong,CrossTEXLong', file = stats) |
| 806 |
test_shaders_helper(stats, backend, args) |
| 807 |
else: |
| 808 |
test_shaders_helper(None, backend, args) |
| 809 |
|
| 810 |
def main(): |
| 811 |
parser = argparse.ArgumentParser(description = 'Script for regression testing.') |
| 812 |
parser.add_argument('folder', |
| 813 |
help = 'Folder containing shader files to test.') |
| 814 |
parser.add_argument('--update', |
| 815 |
action = 'store_true', |
| 816 |
help = 'Updates reference files if there is a mismatch. Use when legitimate changes in output is found.') |
| 817 |
parser.add_argument('--keep', |
| 818 |
action = 'store_true', |
| 819 |
help = 'Leave failed GLSL shaders on disk if they fail regression. Useful for debugging.') |
| 820 |
parser.add_argument('--malisc', |
| 821 |
action = 'store_true', |
| 822 |
help = 'Use malisc offline compiler to determine static cycle counts before and after spirv-cross.') |
| 823 |
parser.add_argument('--msl', |
| 824 |
action = 'store_true', |
| 825 |
help = 'Test Metal backend.') |
| 826 |
parser.add_argument('--metal', |
| 827 |
action = 'store_true', |
| 828 |
help = 'Deprecated Metal option. Use --msl instead.') |
| 829 |
parser.add_argument('--hlsl', |
| 830 |
action = 'store_true', |
| 831 |
help = 'Test HLSL backend.') |
| 832 |
parser.add_argument('--force-no-external-validation', |
| 833 |
action = 'store_true', |
| 834 |
help = 'Disable all external validation.') |
| 835 |
parser.add_argument('--opt', |
| 836 |
action = 'store_true', |
| 837 |
help = 'Run SPIRV-Tools optimization passes as well.') |
| 838 |
parser.add_argument('--reflect', |
| 839 |
action = 'store_true', |
| 840 |
help = 'Test reflection backend.') |
| 841 |
parser.add_argument('--parallel', |
| 842 |
action = 'store_true', |
| 843 |
help = 'Execute tests in parallel. Useful for doing regression quickly, but bad for debugging and stat output.') |
| 844 |
parser.add_argument('--spirv-cross', |
| 845 |
default = './spirv-cross', |
| 846 |
help = 'Explicit path to spirv-cross') |
| 847 |
parser.add_argument('--glslang', |
| 848 |
default = 'glslangValidator', |
| 849 |
help = 'Explicit path to glslangValidator') |
| 850 |
parser.add_argument('--spirv-as', |
| 851 |
default = 'spirv-as', |
| 852 |
help = 'Explicit path to spirv-as') |
| 853 |
parser.add_argument('--spirv-val', |
| 854 |
default = 'spirv-val', |
| 855 |
help = 'Explicit path to spirv-val') |
| 856 |
parser.add_argument('--spirv-opt', |
| 857 |
default = 'spirv-opt', |
| 858 |
help = 'Explicit path to spirv-opt') |
| 859 |
parser.add_argument('--iterations', |
| 860 |
default = 1, |
| 861 |
type = int, |
| 862 |
help = 'Number of iterations to run SPIRV-Cross (benchmarking)') |
| 863 |
|
| 864 |
args = parser.parse_args() |
| 865 |
if not args.folder: |
| 866 |
sys.stderr.write('Need shader folder.\n') |
| 867 |
sys.exit(1) |
| 868 |
|
| 869 |
if (args.parallel and (args.malisc or args.force_no_external_validation or args.update)): |
| 870 |
sys.stderr.write('Parallel execution is disabled when using the flags --update, --malisc or --force-no-external-validation\n') |
| 871 |
args.parallel = False |
| 872 |
|
| 873 |
args.msl22 = False |
| 874 |
args.msl23 = False |
| 875 |
if args.msl: |
| 876 |
print_msl_compiler_version() |
| 877 |
args.msl22 = msl_compiler_supports_version('2.2') |
| 878 |
args.msl23 = msl_compiler_supports_version('2.3') |
| 879 |
|
| 880 |
backend = 'glsl' |
| 881 |
if (args.msl or args.metal): |
| 882 |
backend = 'msl' |
| 883 |
elif args.hlsl: |
| 884 |
backend = 'hlsl' |
| 885 |
elif args.reflect: |
| 886 |
backend = 'reflect' |
| 887 |
|
| 888 |
test_shaders(backend, args) |
| 889 |
if args.malisc: |
| 890 |
print('Stats in stats.csv!') |
| 891 |
print('Tests completed!') |
| 892 |
|
| 893 |
if __name__ == '__main__': |
| 894 |
main() |