hcttest-system-values.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. # Copyright (C) Microsoft Corporation. All rights reserved.
  2. # This file is distributed under the University of Illinois Open Source License. See LICENSE.TXT for details.
  3. """hcttest-system-values.py - Test all system values with each signature point through fxc.
  4. Builds csv tables for the results for each shader model from 4.0 through 5.1.
  5. """
  6. import re
  7. SignaturePoints = [
  8. # sig point, stage, tessfactors already present
  9. ('VSIn', 'vs', False),
  10. ('VSOut', 'vs', False),
  11. ('PCIn', 'hs', False),
  12. ('HSIn', 'hs', False),
  13. ('HSCPIn', 'hs', False),
  14. ('HSCPOut', 'hs', False),
  15. ('PCOut', 'hs', True ),
  16. ('DSIn', 'ds', True ),
  17. ('DSCPIn', 'ds', False),
  18. ('DSOut', 'ds', False),
  19. ('GSVIn', 'gs', False),
  20. ('GSIn', 'gs', False),
  21. ('GSOut', 'gs', False),
  22. ('PSIn', 'ps', False),
  23. ('PSOut', 'ps', False),
  24. ('CSIn', 'cs', False),
  25. ]
  26. SysValues = """
  27. VertexID
  28. InstanceID
  29. Position
  30. RenderTargetArrayIndex
  31. ViewportArrayIndex
  32. ClipDistance
  33. CullDistance
  34. OutputControlPointID
  35. DomainLocation
  36. PrimitiveID
  37. GSInstanceID
  38. SampleIndex
  39. IsFrontFace
  40. Coverage
  41. InnerCoverage
  42. Target
  43. Depth
  44. DepthLessEqual
  45. DepthGreaterEqual
  46. StencilRef
  47. DispatchThreadID
  48. GroupID
  49. GroupIndex
  50. GroupThreadID
  51. TessFactor
  52. InsideTessFactor
  53. """.split()
  54. def run(cmd, output_filename=None):
  55. import os
  56. print(cmd)
  57. if output_filename:
  58. if os.path.isfile(output_filename):
  59. os.unlink(output_filename)
  60. with file(output_filename, 'wt') as f:
  61. f.write('%s\n\n' % cmd)
  62. ret = os.system(cmd + ' >> "%s" 2>&1' % output_filename)
  63. output = ''
  64. if os.path.isfile(output_filename):
  65. with open(output_filename, 'rt') as f:
  66. output = f.read()
  67. return ret, output
  68. else:
  69. ret = os.system(cmd)
  70. return ret, None
  71. sig_examples = """
  72. // Patch Constant signature:
  73. //
  74. // Name Index Mask Register SysValue Format Used
  75. // -------------------- ----- ------ -------- -------- ------- ------
  76. // Arb 0 x 0 NONE float x
  77. // SV_TessFactor 0 x 1 QUADEDGE float x
  78. // SV_TessFactor 1 x 2 QUADEDGE float x
  79. // SV_TessFactor 2 x 3 QUADEDGE float x
  80. // SV_TessFactor 3 x 4 QUADEDGE float x
  81. // SV_InsideTessFactor 0 x 5 QUADINT float x
  82. // SV_InsideTessFactor 1 x 6 QUADINT float x
  83. //
  84. //
  85. // Input signature:
  86. //
  87. // Name Index Mask Register SysValue Format Used
  88. // -------------------- ----- ------ -------- -------- ------- ------
  89. // Arb 0 x 0 NONE float
  90. //
  91. //
  92. // Output signature:
  93. //
  94. // Name Index Mask Register SysValue Format Used
  95. // -------------------- ----- ------ -------- -------- ------- ------
  96. // Arb 0 x 0 NONE float x
  97. """
  98. rxSigBegin = re.compile(r'^// (Input|Output|Patch Constant) signature:\s*$')
  99. rxSigElementsBegin = re.compile(r'^// -------------------- ----- ------ -------- -------- ------- ------\s*$')
  100. # // SV_Target 1 xyzw 1 TARGET float xyzw
  101. rxSigElement = re.compile(r'^// ([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s*([^\s]+)?\s*$')
  102. # Name, Index, Mask, Register, SysValue, Format, Used = m.groups()
  103. # For now, ParseSigs just looks for a matching semantic name and reports whether it uses
  104. # a separate register (unpacked) and whether it's treated as arbitrary.
  105. def ParseSigs(text, semantic):
  106. "return separate_reg, as_arb for matching semantic"
  107. it = iter(text.splitlines())
  108. sigtype = None
  109. try:
  110. while True:
  111. line = it.next()
  112. m = rxSigBegin.match(line)
  113. if m:
  114. sigtype = m.group(1)
  115. continue
  116. m = rxSigElementsBegin.match(line)
  117. if m:
  118. while True:
  119. line = it.next()
  120. m = rxSigElement.match(line)
  121. if m:
  122. Name, Index, Mask, Register, SysValue, Format, Used = m.groups()
  123. if Name.lower() == semantic.lower():
  124. try:
  125. regnum = int(Register)
  126. reg = False
  127. except:
  128. reg = True
  129. arb = SysValue == 'NONE'
  130. return reg, arb
  131. except StopIteration:
  132. pass
  133. return None
  134. # Internal error or validation error:
  135. rxInternalError = re.compile(r"^(internal error:.*|error X8000:.*)$")
  136. # error X4502: invalid ps input semantic 'Foo'
  137. # error X4502: SV_Coverage input not supported on ps_4_0
  138. # error X4502: SV_SampleIndex isn't supported on ps_4_0
  139. # error X3514: SV_GSInstanceID is an invalid input semantic for geometry shader primitives, it must be its own parameter.
  140. # error X3514: 'GSMain': input parameter 'tfactor' must have a geometry specifier
  141. # also errors for unsupported shader models
  142. # error X3660: cs_4_0 does not support interlocked operations
  143. rxSemanticErrors = [
  144. re.compile(r".*?\.hlsl.*?: error X4502: invalid .*? semantic '(\w+)'"),
  145. re.compile(r".*?\.hlsl.*?: error X4502: (\w+) .*? supported on \w+"),
  146. re.compile(r".*?\.hlsl.*?: error X3514: (\w+) is an invalid input semantic for geometry shader primitives, it must be its own parameter\."),
  147. re.compile(r".*?\.hlsl.*?: error X3514: 'GSMain': input parameter '\w+' must have a geometry specifier"),
  148. ]
  149. def map_gen(fn, *sequences):
  150. "generator style map"
  151. iters = map(iter, sequences)
  152. while True:
  153. yield fn(*map(next, iters))
  154. def firstTrue(iterable):
  155. "returns first non-False element, or None."
  156. for it in iterable:
  157. if it:
  158. return it
  159. def ParseSVError(text, semantic):
  160. "return true if error is about illegal use of matching semantic"
  161. for line in text.splitlines():
  162. m = firstTrue(map_gen(lambda rx: rx.match(line), rxSemanticErrors))
  163. if m:
  164. if len(m.groups()) < 1 or m.group(1).lower() == semantic.lower():
  165. return True
  166. else:
  167. m = rxInternalError.match(line)
  168. if m:
  169. print('#### Internal error detected!')
  170. print(m.group(1))
  171. return 'InternalError'
  172. return False
  173. # TODO: Fill in the correct error pattern
  174. # error X4576: Non system-generated input signature parameter (Arb) cannot appear after a system generated value.
  175. rxMustBeLastError = re.compile(r".*?\.hlsl.*?: error X4576: Non system-generated input signature parameter \(\w+\) cannot appear after a system generated value.")
  176. def ParseSGVError(text, semantic):
  177. "return true if error is about matching semantic having to be declared last"
  178. for line in text.splitlines():
  179. m = rxMustBeLastError.match(line)
  180. if m:
  181. return True
  182. else:
  183. m = rxInternalError.match(line)
  184. if m:
  185. print('#### Internal error detected!')
  186. print(m.group(1))
  187. return 'InternalError'
  188. return False
  189. hlsl_filename = os.abspath(os.path.join()
  190. os.environ['HLSL_SRC_DIR'],
  191. r'tools\clang\test\HLSL',
  192. 'system-values.hlsl'))
  193. def main():
  194. do('5_1')
  195. do('5_0')
  196. do('4_1')
  197. do('4_0')
  198. def do(sm):
  199. import os, sys
  200. # set up table:
  201. table = [[None] * len(SignaturePoints) for sv in SysValues]
  202. null_filename = 'output\\test_sv_null.txt'
  203. for col, (sigpoint, stage, tfpresent) in enumerate(SignaturePoints):
  204. entry = stage.upper() + 'Main'
  205. target = stage + '_%s' % sm
  206. # test arb support:
  207. ret, output = run('fxc %s /E %s /T %s /D%s_Defs=Def_Arb(float,Arb1,Arb1)' %
  208. (hlsl_filename, entry, target, sigpoint),
  209. null_filename)
  210. arb_supported = ret == 0
  211. # iterate all system values
  212. sysvalues = tfpresent and SysValues[:-2] or SysValues
  213. for row, sv in enumerate(sysvalues):
  214. output_filename = 'output\\test_sv_output_%s_%s_%s.txt' % (sm, sv, sigpoint)
  215. separate_reg, as_arb, def_last, result = False, False, False, 'NotInSig'
  216. ret, output = run('fxc %s /E %s /T %s /D%s_Defs=Def_%s' %
  217. (hlsl_filename, entry, target, sigpoint, sv),
  218. output_filename)
  219. if ret:
  220. # Failed, look for expected error message:
  221. found = ParseSVError(output, 'SV_'+sv)
  222. if found == 'InternalError':
  223. table[row][col] = 'InternalError'
  224. print('#### Internal error from ParseSVError - see "%s"' % output_filename)
  225. elif not found:
  226. table[row][col] = 'ParseSVError'
  227. print('#### Error from ParseSVError - see "%s"' % output_filename)
  228. else:
  229. table[row][col] = 'NA'
  230. if os.path.isfile(output_filename):
  231. os.unlink(output_filename)
  232. continue
  233. parse_result = ParseSigs(output, 'SV_'+sv)
  234. if parse_result:
  235. separate_reg, as_arb = parse_result
  236. if as_arb:
  237. if separate_reg:
  238. table[row][col] = 'Error: both as_arb and separate_reg set!'
  239. print('#### Error from ParseSigs, both as_arb and separate_reg set - see "%s"' % output_filename)
  240. continue
  241. result = 'Arb'
  242. else:
  243. if separate_reg:
  244. result = 'NotPacked'
  245. else:
  246. result = 'SV'
  247. if os.path.isfile(output_filename):
  248. os.unlink(output_filename)
  249. else:
  250. print('## Not in signature? See "%s"' % output_filename)
  251. if arb_supported and not as_arb and not separate_reg:
  252. output_filename = 'output\\test_sv_output_last_%s_%s_%s.txt' % (sm, sv, sigpoint)
  253. # must system value be declared last? test by adding arb last if arb support
  254. ret, output = run('fxc %s /E %s /T %s /D%s_Defs="Def_%s Def_Arb(float,Arb1,Arb1)"' %
  255. (hlsl_filename, entry, target, sigpoint, sv),
  256. output_filename)
  257. if ret:
  258. found = ParseSGVError(output, 'SV_'+sv)
  259. if found == 'InternalError':
  260. result += ' | InternalError found with ParseSGVError'
  261. print('#### Internal error from ParseSGVError - see "%s"' % output_filename)
  262. elif not found:
  263. result += ' | ParseSGVError'
  264. print('#### Error from ParseSGVError - see "%s"' % output_filename)
  265. elif result == 'SV':
  266. result = 'SGV'
  267. if os.path.isfile(output_filename):
  268. os.unlink(output_filename)
  269. else:
  270. result += ' | Error: last required detected, but not SV?'
  271. print('#### Error: last required detected, but not SV? - see "%s"' % output_filename)
  272. else:
  273. if os.path.isfile(output_filename):
  274. os.unlink(output_filename)
  275. table[row][col] = result
  276. for row in range(row+1, len(SysValues)):
  277. table[row][col] = 'TessFactor'
  278. def WriteTable(writefn, table):
  279. writefn('Semantic,' + ','.join([sigpoint for sigpoint, stage, tfpresent in SignaturePoints]) + '\n')
  280. for n, row in enumerate(table):
  281. writefn((SysValues[n]) + ','.join(row) + '\n')
  282. WriteTable(sys.stdout.write, table)
  283. with open('fxc_sig_packing_table_%s.csv' % sm, 'wt') as f:
  284. WriteTable(f.write, table)
  285. if __name__ == '__main__':
  286. main()