Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ to check RGBA8 texel and alpha values, rebinding, and a mismatched 2D view on ha

`tools/startup/gfx2hlsl.py` lowers vertex-stage `buffer_store_dword`, `dwordx2`, `dwordx3` and `dwordx4` in the `off` form to `RWByteAddressBuffer.Store*`. The descriptor must come from a root-table slot, and the manifest becomes version 2 with a `uav <slot> table <param> <byte offset>` line. This is offline only: the shipped runtime refuses version 2 manifests, so no store runs in a game. Refused forms: `glc`, `slc`, `offen`, `idxen`, pixel stage, a descriptor also used for a load, a descriptor whose base was changed, and an immediate or inline offset that is not a multiple of four. A register offset is rounded down to a multiple of four.

The descriptor modifications that compute buffer stores accept are accepted here too, with the same checks: the stride clear of DW1 and the structured-to-raw conversion (stride DW1[29:16] extracted, optionally clamped to at least 1, DW2 NUM_RECORDS scaled by it, DW3 OOB_SELECT set to 3, stride cleared), all four words from one descriptor load. The store and manifest are those of the unmodified descriptor. In the vertex stage, as in compute, a descriptor load through a pointer other than the untouched pair is tagged with the constant high word of a 32-bit table pointer, or otherwise with its own load, so the conversion cannot mix words of two loads. Any other modification stays refused. As for compute, matching out-of-range behavior would need a UAV view sized to the converted byte count; the runtime binds no vertex-stage UAV yet.

```powershell
ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_buffer_store$' --output-on-failure
```
Expand Down
64 changes: 64 additions & 0 deletions tests/startup_gfx2hlsl_buffer_store_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ def v_mov(vdst, inline):
return struct.pack('<I', 0x7e000000 | (vdst << 17) | (1 << 9) | (128 + inline))


SOP2 = {'s_add_u32': 0x00, 's_max_u32': 0x09, 's_and_b32': 0x0e, 's_or_b32': 0x10, 's_mul_i32': 0x26, 's_bfe_u32': 0x27}


def sop2(op, sdst, ssrc0, ssrc1, literal=None):
"""SOP2 word (operand numbers: SGPR n is n, inline integer n is 128 + n, 255 takes the literal)."""
word = struct.pack('<I', (0b10 << 30) | (op << 23) | (sdst << 16) | (ssrc1 << 8) | ssrc0)
return word if literal is None else word + struct.pack('<I', literal)


# One descriptor table at s[8:9] with an SRV/UAV range of two entries (the same layout the buffer address check uses).
RTS0 = b''.join((struct.pack('<6I', 2, 1, 24, 0, 0, 0), struct.pack('<3I', 0, 0, 36),
struct.pack('<2I', 1, 44), struct.pack('<6I', 0, 2, 0, 0, 0, 0)))
Expand Down Expand Up @@ -113,6 +122,61 @@ def main():
check('store through a modified descriptor is refused', result.startswith('unsupported') and 'modified' in fail_reason(name),
'%s %s' % (result, fail_reason(name)))

# 3c. The descriptor modifications a compute store accepts (#2320, #2546): the stride clear and the
# structured-to-raw conversion (stride DW1[29:16] extracted, optionally clamped to 1, NUM_RECORDS in DW2
# scaled by it, OOB_SELECT DW3[29:28] set to 3, stride cleared). The store is the plain one, so the HLSL and
# manifest are those of the unmodified descriptor; everything else stays refused.
plain_store = encode_mubuf(STORE_OPS['dwordx3'], imm=8, soffset=13)
set_s13 = struct.pack('<I', 0xbe800000 | (13 << 16) | (3 << 8) | 128) # SOP1 s_mov_b32 s13, 0
plain, name = request(folder, LOAD_DESCRIPTOR + set_s13 + DEFINE_V + plain_store + EXPORT_POSITION)
check('plain store with a scalar offset translates', plain == 'ok', '%s %s' % (plain, fail_reason(name)))
plain_hlsl = name.with_suffix('.vs.hlsl').read_text() if plain == 'ok' else None
plain_manifest = name.with_suffix('.txt').read_text() if plain == 'ok' else None
extract = sop2(SOP2['s_bfe_u32'], 12, 33, 255, 0xe0010)
clamp = sop2(SOP2['s_max_u32'], 12, 12, 129)
clear = sop2(SOP2['s_and_b32'], 33, 33, 255, 0xc000ffff)
oob_clear = sop2(SOP2['s_and_b32'], 35, 35, 255, 0xcfffffff)
scale = sop2(SOP2['s_mul_i32'], 34, 34, 12)
oob_raw = sop2(SOP2['s_or_b32'], 35, 35, 255, 0x30000000)
check('conversion decodes as captured', [a for (_, _, _, a) in G.decode(extract + clamp + clear + oob_clear + scale + oob_raw)] == [
's_bfe_u32 s12, s33, 0xe0010', 's_max_u32 s12, s12, 1', 's_and_b32 s33, s33, 0xc000ffff',
's_and_b32 s35, s35, 0xcfffffff', 's_mul_i32 s34, s34, s12', 's_or_b32 s35, s35, 0x30000000'])
for label, mod in (('clamped raw conversion', extract + clamp + clear + oob_clear + scale + oob_raw),
('raw conversion', extract + clear + scale + oob_raw),
('stride clear', clear)):
result, name = request(folder, LOAD_DESCRIPTOR + set_s13 + mod + DEFINE_V + plain_store + EXPORT_POSITION)
check('%s translates as the plain store' % label, result == 'ok' and plain_hlsl is not None
and name.with_suffix('.vs.hlsl').read_text() == plain_hlsl
and name.with_suffix('.txt').read_text() == plain_manifest, '%s %s' % (result, fail_reason(name)))
# Captured programs set the high word of the 32-bit table pointer to 0 before loading; a second load through
# the same untouched pointer and high word reads the same words, so its stride is the same value.
table_hi = struct.pack('<I', 0xbe800000 | (9 << 16) | (3 << 8) | 128) # SOP1 s_mov_b32 s9, 0
reload = struct.pack('<3I', 0xf4080904, 0xfa000010, 0xbf8cc07f) # s_load_dwordx4 s[36:39], s[8:9], 0x10
result, name = request(folder, table_hi + LOAD_DESCRIPTOR + set_s13 + reload + sop2(SOP2['s_bfe_u32'], 12, 37, 255, 0xe0010)
+ clear + scale + oob_raw + DEFINE_V + plain_store + EXPORT_POSITION)
reference, ref_name = request(folder, table_hi + LOAD_DESCRIPTOR + set_s13 + DEFINE_V + plain_store + EXPORT_POSITION)
check('stride of a second load through the same pointer translates as the plain store', result == reference == 'ok'
and name.with_suffix('.vs.hlsl').read_text() == ref_name.with_suffix('.vs.hlsl').read_text(),
'%s %s' % (result, fail_reason(name)))
# After an add on the table pointer, a second load reads other words under the same table offset.
add_pointer = sop2(SOP2['s_add_u32'], 8, 8, 255, 32)
refused_mods = {
'clamp other than 1': extract + sop2(SOP2['s_max_u32'], 12, 12, 130) + clear + oob_clear + scale + oob_raw,
'NUM_RECORDS not scaled': extract + clamp + clear + oob_clear + oob_raw,
'NUM_RECORDS scaled by a constant': clear + sop2(SOP2['s_mul_i32'], 34, 34, 132) + oob_raw,
'OOB_SELECT 2': extract + clamp + clear + oob_clear + scale + sop2(SOP2['s_or_b32'], 35, 35, 255, 0x20000000),
'stride not cleared': extract + clamp + oob_clear + scale + oob_raw,
'another bit field': sop2(SOP2['s_bfe_u32'], 12, 33, 255, 0xe0011) + clear + scale + oob_raw,
'stride of a load after a pointer add': table_hi + add_pointer + reload + sop2(SOP2['s_bfe_u32'], 12, 37, 255, 0xe0010)
+ clear + scale + oob_raw,
'NUM_RECORDS of a load after a pointer add': table_hi + add_pointer + reload + extract + clear
+ sop2(SOP2['s_mul_i32'], 34, 38, 12) + oob_raw,
}
for label, mod in refused_mods.items():
result, name = request(folder, LOAD_DESCRIPTOR + set_s13 + mod + DEFINE_V + plain_store + EXPORT_POSITION)
check('%s is refused' % label, result.startswith('unsupported')
and fail_reason(name) == 'buffer store through a modified descriptor', '%s %s' % (result, fail_reason(name)))

# 4. Control: a program with only a load keeps manifest version 1 and no writable view.
load = encode_mubuf(MUBUF_LOAD_DWORD, vdata=6, srsrc=32)
result, name = request(folder, LOAD_DESCRIPTOR + load + EXPORT_POSITION)
Expand Down
14 changes: 8 additions & 6 deletions tools/startup/gfx2hlsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -3166,7 +3166,7 @@ def src_sym(t):
d = reg_range(dname)[1]
own = self.sym.get(d)
raw = (raw_descriptor_alu(base, syms, [self.const_u32(t) for t in srcs])
if getattr(self, 'kind', None) == 'cs' and len(syms) == 2 else None)
if getattr(self, 'kind', None) in ('cs', 'vs') and len(syms) == 2 else None)
if base in ('s_mov_b32', 's_mov_b64'):
self.sym[d] = syms[0]
if pair:
Expand Down Expand Up @@ -3544,12 +3544,13 @@ def op_s_load_dwordx4(self, ops, mods, count=4):
self.set_s(dst[1] + i, '%s.Load(%d)' % (buf, off + 4 * i))
return
# The pointer symbol does not record an addend or a rewritten high word, so two loads through such a
# pointer may read different words under the same (source, offset). In a compute stage those loads
# are told apart by the offset of the s_load; loads through the untouched pair keep one identity.
# pointer may read different words under the same (source, offset). In a compute or vertex stage (the
# stages that accept a raw descriptor conversion before a store) those loads are told apart by the
# offset of the s_load; loads through the untouched pair keep one identity.
# A 32-bit table pointer carries a high word the program sets to a constant: loads through the same
# untouched low word and the same constant read the same words (tag -1 - constant).
hi = self.sym.get(base[1] + 1)
if getattr(self, 'kind', None) != 'cs' or (p == ('ptr', p[1]) and hi == ('ptrhi', p[1])):
if getattr(self, 'kind', None) not in ('cs', 'vs') or (p == ('ptr', p[1]) and hi == ('ptrhi', p[1])):
load = ()
elif p == ('ptr', p[1]) and hi and hi[0] == 'const' and len(hi) == 2:
load = (-1 - hi[1],)
Expand Down Expand Up @@ -3851,7 +3852,8 @@ def op_buffer_store_vs(self, ops, mods, count):
if key[0][0] != 'table' or key[1] is None:
raise Unsupported('buffer store through %s' % (key,))
if key in self.pipe.modified:
raise Unsupported('buffer store through a modified descriptor')
# Only the modifications a compute store accepts: the stride clear or the structured-to-raw conversion.
self.store_descriptor(ops[2], 4, 'buffer store', stride0_ok=True, raw_conversion_ok=True)
if re.fullmatch(r'0x[0-9a-f]+|\d+', ops[3]):
offset += int(ops[3], 0)
soffset = ''
Expand Down Expand Up @@ -6072,7 +6074,7 @@ def __init__(self, vs_code, ps_code, root_blob, sampler_heap=None, rtv_formats=(
self.uds = [] # entry-constant sources
self.notes = set() # shortcuts taken (written to the manifest as comments)
self.lod_unvalidated = set() # image_sample forms read through a heap sampler (LOD fields unchecked, #2221)
self.modified = set()
self.modified, self.other_modified = set(), set() # other: unproved descriptor writes
self.rect = self.ud(('rect',))
if refusals is None:
self.vs = Stage(self, self.vertex_kind, vs_code)
Expand Down
Loading