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