convert-hlsl_lines-to-pattern_lines.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Copyright (c) Contributors to the Open 3D Engine Project.
  5. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  6. SPDX-License-Identifier: Apache-2.0 OR MIT
  7. """
  8. import io
  9. import re
  10. def GetPatternLineFromHlslLine(line):
  11. """
  12. Returns a string, with the pattern version of the input string.
  13. Example:
  14. The following HLSL line produced by AZSLc:
  15. float2 samplePos = ::PassSrg_GetSamplePosition ( IN . m_sampleIndex ) ;
  16. Will become like this when converted to pattern line as required by testhelper.verifyEmissionPattern():
  17. "float2 samplePos = :: PassSrg_GetSamplePosition ( IN . m_sampleIndex ) ;"
  18. The '"' characters are also included in the returned string, which is required
  19. by testhelper.verifyEmissionPattern().
  20. """
  21. line = line.rstrip("\r\n")
  22. if line == "":
  23. return None
  24. allidents = re.split("([a-zA-Z_]+[a-zA-Z_0-9]*)|(;)|(\()|(\))|(<)|(>)|( )", line)
  25. allidents = filter(lambda s: s is not None and s!="" and s!=" ", allidents) # remove empties from ['', 'code', '', 'piece']...
  26. line = " ".join(allidents)
  27. return f"\"{line}\""
  28. def GeneratePatternLinesFromHlslFile(filePath):
  29. """
  30. Returns all lines in the file named @filePath
  31. as pattern lines.
  32. In general, @filePath is output HLSL code produced by AZSLc.
  33. """
  34. resultLines = []
  35. with io.open(filePath, "r", encoding="latin-1") as f:
  36. for line in f:
  37. predicateLine = GetPatternLineFromHlslLine(line)
  38. if not (predicateLine is None): resultLines.append(predicateLine)
  39. return resultLines
  40. # The main purpose of this helper script is to take
  41. # an HLSL output produced by AZSLc and convert each line
  42. # into a pattern line. The idea is that the pattern
  43. # line version is what is used for emission validation.
  44. # The pattern line version is what is required when testhelper.verifyEmissionPattern() is called.
  45. #
  46. # Example:
  47. # The following HLSL line produced by AZSLc:
  48. # float2 samplePos = ::PassSrg_GetSamplePosition ( IN . m_sampleIndex ) ;
  49. # Will become like this when converted to pattern line as required by testhelper.verifyEmissionPattern():
  50. # "float2 samplePos = :: PassSrg_GetSamplePosition ( IN . m_sampleIndex ) ;"
  51. #
  52. # This script is NOT intended to be used during testing,
  53. # it is used to pre-generate/bake data files data that can be used during testing.
  54. if __name__ == "__main__":
  55. import sys
  56. HELP = f"{sys.argv[0]} <hlslFile>"
  57. if len(sys.argv) != 2:
  58. print(HELP)
  59. sys.exit(-1)
  60. filePath = sys.argv[1]
  61. lines = GeneratePatternLinesFromHlslFile(filePath)
  62. for line in lines:
  63. print(line)