upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tools/rdna2spirv.py
diff options
context:
space:
mode:
Diffstat (limited to 'tools/rdna2spirv.py')
-rw-r--r--tools/rdna2spirv.py35
1 files changed, 24 insertions, 11 deletions
diff --git a/tools/rdna2spirv.py b/tools/rdna2spirv.py
index fcb4e0f9f..8dbbbbcfb 100644
--- a/tools/rdna2spirv.py
+++ b/tools/rdna2spirv.py
@@ -842,16 +842,20 @@ class Gen:
842 return words 842 return words
843 843
844 def decode_format(self, words, code): 844 def decode_format(self, words, code):
845 """Decode one element of format `code` from raw dwords -> 4 uint ids holding f32 bits (or uint for *_UINT/uint).""" 845 """Decode one element of format `code` from raw dwords -> 4 uint ids holding f32 bits (or uint for *_UINT/uint).
846
847 Missing channels repeat the format's own (component c = c mod n): measured on RX 6700 for SHADER-86.
848 UNORM/SNORM use correctly rounded OpFDiv by (2^n-1), not a reciprocal multiply."""
846 try: 849 try:
847 size, kind, widths = self.FORMATS[code] 850 size, kind, widths = self.FORMATS[code]
848 except KeyError: 851 except KeyError:
849 raise Unsupported(f"format {code} (not in FORMATS)") 852 raise Unsupported(f"format {code} (not in FORMATS)")
850 if kind in ("f32", "u32"): 853 if kind in ("f32", "u32"):
851 vals = list(words[:len(widths)]) 854 n = len(widths)
852 if kind == "u32": 855 vals = list(words[:n])
853 vals = vals # integers are passed through as their bits 856 while len(vals) < 4:
854 return vals + [self.cu(0)] * (3 - len(vals) + 1 if len(vals) < 4 else 0) 857 vals.append(vals[len(vals) % n])
858 return vals
855 out, bit = [], 0 859 out, bit = [], 0
856 for w in widths: 860 for w in widths:
857 src = words[bit // 32] 861 src = words[bit // 32]
@@ -865,21 +869,21 @@ class Gen:
865 elif kind == "unorm": 869 elif kind == "unorm":
866 c, x = self.tmp(), self.tmp() 870 c, x = self.tmp(), self.tmp()
867 self.emit(f"{c} = OpConvertUToF %float {f}") 871 self.emit(f"{c} = OpConvertUToF %float {f}")
868 self.emit(f"{x} = OpFMul %float {c} {self.fconst(1.0 / ((1 << w) - 1))}") 872 self.emit(f"{x} = OpFDiv %float {c} {self.fconst(float((1 << w) - 1))}")
869 out.append(self.tou(x)) 873 out.append(self.tou(x))
870 elif kind == "snorm": 874 elif kind == "snorm":
871 si, c, x, cl = self.tmp(), self.tmp(), self.tmp(), self.tmp() 875 si, c, x, cl = self.tmp(), self.tmp(), self.tmp(), self.tmp()
872 self.emit(f"{si} = OpBitFieldSExtract %int {self.as_int(src)} {self.cu(bit % 32)} {self.cu(w)}") 876 self.emit(f"{si} = OpBitFieldSExtract %int {self.as_int(src)} {self.cu(bit % 32)} {self.cu(w)}")
873 self.emit(f"{c} = OpConvertSToF %float {si}") 877 self.emit(f"{c} = OpConvertSToF %float {si}")
874 self.emit(f"{x} = OpFMul %float {c} {self.fconst(1.0 / ((1 << (w - 1)) - 1))}") 878 self.emit(f"{x} = OpFDiv %float {c} {self.fconst(float((1 << (w - 1)) - 1))}")
875 self.emit(f"{cl} = OpExtInst %float %glsl FMax {x} {self.fconst(-1.0)}") 879 self.emit(f"{cl} = OpExtInst %float %glsl FMax {x} {self.fconst(-1.0)}")
876 out.append(self.tou(cl)) 880 out.append(self.tou(cl))
877 else: # uint: raw integer 881 else: # uint: raw integer
878 out.append(f) 882 out.append(f)
879 bit += w 883 bit += w
880 one = self.cu(0x3F800000) if kind in ("f16", "unorm", "snorm") else self.cu(1) 884 n = len(out)
881 while len(out) < 4: 885 while len(out) < 4:
882 out.append(self.cu(0) if len(out) < 3 else one) 886 out.append(out[len(out) % n])
883 return out 887 return out
884 888
885 BUF_ATOMICS = {"add": ("OpAtomicIAdd", 0), "sub": ("OpAtomicISub", 0), "umin": ("OpAtomicUMin", 0xFFFFFFFF), 889 BUF_ATOMICS = {"add": ("OpAtomicIAdd", 0), "sub": ("OpAtomicISub", 0), "umin": ("OpAtomicUMin", 0xFFFFFFFF),
@@ -1055,13 +1059,22 @@ class Gen:
1055 self.emit(f"{s} = OpSelect %uint {e} {dec[c]} {comps[c]}") 1059 self.emit(f"{s} = OpSelect %uint {e} {dec[c]} {comps[c]}")
1056 nxt.append(s) 1060 nxt.append(s)
1057 comps = nxt 1061 comps = nxt
1058 # DST_SEL swizzle: 0 -> 0, 1 -> 1.0, 4..7 -> X..W. 1062 # DST_SEL swizzle: 0 -> 0, 1 -> 1.0 (float/norm) or integer 1 (UINT/SINT; SHADER-86), 4..7 -> X..W.
1063 is_int_fmt = "%false"
1064 for code_v, (_, kind, _) in self.FORMATS.items():
1065 if kind in ("u32", "uint"):
1066 e, o = self.tmp(), self.tmp()
1067 self.emit(f"{e} = OpIEqual %bool {fmt} {self.cu(code_v)}")
1068 self.emit(f"{o} = OpLogicalOr %bool {is_int_fmt} {e}")
1069 is_int_fmt = o
1070 dst1 = self.tmp()
1071 self.emit(f"{dst1} = OpSelect %uint {is_int_fmt} {self.cu(1)} {self.cu(0x3F800000)}")
1059 outs = [] 1072 outs = []
1060 for c in range(ncomp): 1073 for c in range(ncomp):
1061 sel = self.tmp() 1074 sel = self.tmp()
1062 self.emit(f"{sel} = OpBitFieldUExtract %uint {d3} {self.cu(3 * c)} {self.cu(3)}") 1075 self.emit(f"{sel} = OpBitFieldUExtract %uint {d3} {self.cu(3 * c)} {self.cu(3)}")
1063 r = self.cu(0) 1076 r = self.cu(0)
1064 for code_v, src in ((1, self.cu(0x3F800000)), (4, comps[0]), (5, comps[1]), (6, comps[2]), (7, comps[3])): 1077 for code_v, src in ((1, dst1), (4, comps[0]), (5, comps[1]), (6, comps[2]), (7, comps[3])):
1065 e, s = self.tmp(), self.tmp() 1078 e, s = self.tmp(), self.tmp()
1066 self.emit(f"{e} = OpIEqual %bool {sel} {self.cu(code_v)}") 1079 self.emit(f"{e} = OpIEqual %bool {sel} {self.cu(code_v)}")
1067 self.emit(f"{s} = OpSelect %uint {e} {src} {r}") 1080 self.emit(f"{s} = OpSelect %uint {e} {src} {r}")