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
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5248,6 +5248,10 @@ if(EXISTS "${INTAKE_PYTHON}")
# Startup shader translator: descriptor offsets inside wide scalar loads (no game data).
add_test(NAME startup_translator COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_translator_check.py")
# Startup shader translator: slot 57 pixel entries (.ps57in) translate, and a cached plain-key
# translation is reused instead of the suffixed key (#3385).
add_test(NAME startup_gfx2hlsl_ps57in COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_ps57in_check.py")
# Startup shader translator: the SMEM offset 'off' is the literal 0; m0 and other operands keep their handling (#1674).
add_test(NAME startup_gfx2hlsl_smem_offset COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_smem_offset_check.py")
Expand Down
18 changes: 16 additions & 2 deletions src/startup_stubs/d3d12_draw.inl
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,21 @@ Translation* cached_translation(const void* a, std::size_t a_bytes, const void*
u64 key = fnv_bytes(root_blob.data(), root_blob.size(), fnv_bytes(b, b_bytes, fnv_bytes(a, a_bytes, seed)));
// A stream's input layout (stream_input_layout) feeds the vertex fetch, so it is part of the key.
if (!layout.empty()) key = fnv_bytes(layout.data(), layout.size(), key);
if (!slot57_entry.empty()) key = fnv_bytes(slot57_entry.data(), slot57_entry.size(), key);
std::string pixel_entry = slot57_entry;
if (!pixel_entry.empty()) {
// #3385: with X2W_NATIVE_SHADERS set, the fixed pixel entry used to join every slot 57 key, so a
// translation already cached under the plain key never hit and the Python fallback refused the
// suffixed request (it never read .ps57in). Reuse the cached plain-key translation when one is
// already published: the pipeline already ran with it, and the native path can still try the plain
// request. Only a cold pipeline keeps the entry in its key. In native-shaders-only mode .txt
// manifests are ignored, so a leftover Python manifest must not strip the entry from the request
// the native path receives: the reuse is skipped there and the entry stays in the key.
wchar_t base_path[MAX_PATH];
const bool base_cached = !native_shaders_only() && cache_path(base_path, L"", key, L".txt") &&
GetFileAttributesW(base_path) != INVALID_FILE_ATTRIBUTES;
if (base_cached) pixel_entry.clear();
else key = fnv_bytes(pixel_entry.data(), pixel_entry.size(), key);
}
auto& entry = translation_by_key()[key];
if (!entry) {
entry = new Translation;
Expand All @@ -695,7 +709,7 @@ Translation* cached_translation(const void* a, std::size_t a_bytes, const void*
if (publish_shader_request(key, L".b", b, b_bytes) &&
publish_shader_request(key, L".root", root_blob.data(), root_blob.size()) &&
(layout.empty() || publish_shader_request(key, L".ia", layout.data(), layout.size())) &&
(slot57_entry.empty() || publish_shader_request(key, L".ps57in", slot57_entry.data(), slot57_entry.size())))
(pixel_entry.empty() || publish_shader_request(key, L".ps57in", pixel_entry.data(), pixel_entry.size())))
publish_shader_request(key, L".a", a, a_bytes);
load_translation(*entry);
}
Expand Down
1 change: 1 addition & 0 deletions tests/checks/python-linux/startup_gfx2hlsl_ps57in.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/startup_gfx2hlsl_ps57in_check.py
341 changes: 341 additions & 0 deletions tests/startup_gfx2hlsl_ps57in_check.py

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions tests/startup_shader_cache_key_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def check_slot57_entry(vector, blobs, canonical):
assert gfx2hlsl.pipeline_key(*vector, b"ia\x00\xfe", slot57_entry=entry) == 0x06315afc32bae986
entry_key = gfx2hlsl.pipeline_key(*blobs, slot57_entry=entry)
assert entry_key == gfx2hlsl.fnv(entry, canonical) != canonical
accepted = check_request(blobs, entry_key, records={".ps57in": entry})
check_request(blobs, entry_key, records={".ps57in": entry})
print("slot 57 entry: request keyed with its .ps57in accepted and published")
check_request(blobs, canonical, BASE_MISMATCH, records={".ps57in": entry})
check_request(blobs, entry_key, BASE_MISMATCH)
Expand All @@ -77,11 +77,12 @@ def check_slot57_entry(vector, blobs, canonical):
check_request(blobs, both, "conflicting stream and slot-57 pixel entry records",
records={".psin": description, ".ps57in": entry})
print("slot 57 entry: a request with both .psin and .ps57in refused by name")
# The translation does not read the record's values: another record under its own key gives the same shaders.
changed = check_request(blobs, gfx2hlsl.pipeline_key(*blobs, slot57_entry=other), records={".ps57in": other})
for stage in (".vs.hlsl", ".ps.hlsl"):
assert accepted[f"{entry_key:016x}{stage}"] == changed[f"{gfx2hlsl.fnv(other, canonical):016x}{stage}"], stage
print("slot 57 entry: two records with the same stages translate to the same HLSL")
# request_ps_inputs reads the record's values: an address the native fixed entry does not accept
# (2 or 0x8002 with 1 to 7 interpolants) is refused by layout even under its own key, so a changed
# record no longer translates to the same shaders. The key still hashes the record as stored.
check_request(blobs, gfx2hlsl.pipeline_key(*blobs, slot57_entry=other),
"invalid slot-57 pixel entry layout", records={".ps57in": other})
print("slot 57 entry: a record outside the fixed entry's layout is refused by name")
for correct in (True, False):
base = entry_key if correct else canonical
request = f"rtv1 {base} 28"
Expand Down
39 changes: 31 additions & 8 deletions tools/startup/gfx2hlsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -13327,19 +13327,39 @@ def request_ps_inputs(out_dir, key):
"""The pixel input masks (SPI_PS_INPUT_ENA, SPI_PS_INPUT_ADDR) of one graphics request, or None.

They come from the stream description request_source() names: req_<key>.psin, the separately captured
description of exactly that pipeline (see read_ps_inputs), or for an RTV variant its source pipeline's. The
masks are part of the request's identity: the key must equal pipeline_key() of the stage blobs, root
signature, input layout and these masks, so a description copied from another pipeline, left stale after a
stage change or rewritten with another mask pair is refused, never applied. No file means no captured
description: the translation keeps its model without hardware inputs."""
description of exactly that pipeline (see read_ps_inputs), or for an RTV variant its source pipeline's; or
from the slot 57 pixel entry req_<key>.ps57in (see read_slot57_entry), whose address is both masks, as the
native fixed entry builds it. The masks are part of the request's identity: the key must equal
pipeline_key() of the stage blobs, root signature, input layout and these masks or that entry, so a
description copied from another pipeline, left stale after a stage change or rewritten with another mask
pair or entry is refused, never applied. No file means no captured description: the translation keeps its
model without hardware inputs. A request has one pixel entry or the other, never both. The entry's layout
is validated here: like the native fixed entry (src/direct_compute_cache.cpp prepare_direct_graphics),
only address 2 or 0x8002 with 1 to 7 interpolants is accepted; anything else is refused by name. The
native path additionally checks the count against the reflected pixel interface's attribute count and the
address against its reflected input address, which the Python reader has no interface for, so that part
stays native-only.
"""
_, base, _, description = request_source(out_dir, key)
masks = read_ps_inputs(description, base)
if masks is None:
entry = read_slot57_entry(os.path.join(out_dir, 'req_%016x.ps57in' % base))
if masks is not None and entry:
raise Unsupported('conflicting stream and slot-57 pixel entry records')
if masks is None and not entry:
return None
name = os.path.join(out_dir, 'req_%016x' % base)
a, b, root = (open(name + ext, 'rb').read() for ext in ('.a', '.b', '.root'))
layout = open(name + '.ia', 'rb').read() if os.path.isfile(name + '.ia') else b''
if pipeline_key(a, b, root, layout, masks) != base:
if entry:
address, interpolants = struct.unpack('<II', entry[4:])
if address not in (2, 0x8002) or not 1 <= interpolants <= 7:
raise Unsupported('invalid slot-57 pixel entry layout')
masks = (address, address)
# The key hashes the entry alone: masks are derived from it, never hashed alongside it
# (pipeline_key hashes ps_inputs only for a .psin request, and the two never coexist).
if pipeline_key(a, b, root, layout, slot57_entry=entry) != base:
raise Unsupported('slot-57 pixel entry names another pipeline')
elif pipeline_key(a, b, root, layout, masks) != base:
raise Unsupported('pixel input description names another pipeline')
return masks

Expand Down Expand Up @@ -13520,7 +13540,10 @@ def translate_request(out_dir, key):
raise Unsupported('pixel input description missing for a request translated with one')
kinds, root, formats, request, fetch, _, attr_map = load_request(out_dir, key, validate_base_key=True)
ps_inputs = request_ps_inputs(out_dir, key)
if ps_inputs is not None:
# A .psdep records a result as depending on req_<base>.psin; pending_keys withdraws it when that
# file is gone. A slot 57 entry has no .psin to lose (its bytes are part of the key, so a changed
# entry is a different request), so no record is written for it.
if ps_inputs is not None and not os.path.isfile(os.path.join(out_dir, 'req_%016x.ps57in' % base)):
record_dependency(out_dir, key, base, ps_inputs) # before the manifest is published
except Unsupported:
# A result published earlier was translated from sources or masks this request no longer confirms.
Expand Down
14 changes: 11 additions & 3 deletions tools/startup/shader_cache_rekey.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
that already exists in <new request dir> is left as it is. Each file is written to a temporary name and renamed;
the ready marker (.a, .c67, .m57) goes last, as the runtime writes it. A stream description (.psin) is written
again under the new key with the same masks, since its line names the key and its masks are part of the key.
A slot 57 pixel entry (.ps57in) holds no key and is copied unchanged, since its bytes are part of the key.
Slot 86 mesh requests are only counted.

All requests are read before anything is written. When a request part is missing or unreadable, or an RTV
Expand All @@ -31,9 +32,10 @@

REQUEST = re.compile(r'req_([0-9a-f]{16})\.(a|rtv|c67|m57|m86)')
# Request files of each kind; the ready marker is last.
PARTS = {'g': ('.b', '.root', '.ia', '.psin', '.a'), 'c': ('.desc', '.sa', '.sb', '.data', '.root', '.c67'),
PARTS = {'g': ('.b', '.root', '.ia', '.psin', '.ps57in', '.a'), 'c': ('.desc', '.sa', '.sb', '.data', '.root', '.c67'),
'm57': ('.ps57', '.root', '.m57')}
OPTIONAL = {'.ia', '.psin'} # a graphics request may have no input layout (.ia) and no stream description (.psin)
OPTIONAL = {'.ia', '.psin', '.ps57in'} # a graphics request may have no input layout (.ia) and no stream
# description (.psin) or slot 57 pixel entry (.ps57in); the entry holds no key, so it is copied unchanged


class Incomplete(Exception):
Expand Down Expand Up @@ -63,11 +65,17 @@ def current_key(job):
except OSError as error:
return kind, key, None, 'unreadable %s: %s' % (os.path.basename(error.filename or ''), error.strerror)
if kind == 'g':
# The pixel input masks of a stream description are part of the key; the description names its key.
# The pixel input masks of a stream description, or the bytes of a slot 57 pixel entry, are part of
# the key; the description names its key. A request has one pixel entry or the other, never both.
try:
masks = g.read_ps_inputs(request_name(src, key, '.psin'), key)
entry = g.read_slot57_entry(request_name(src, key, '.ps57in'))
except g.Unsupported as reason:
return kind, key, None, str(reason)
if masks is not None and entry:
return kind, key, None, 'conflicting stream and slot-57 pixel entry records'
if entry:
return kind, key, g.pipeline_key(p['.a'], p['.b'], p['.root'], p.get('.ia', b''), slot57_entry=entry), None
return kind, key, g.pipeline_key(p['.a'], p['.b'], p['.root'], p.get('.ia', b''), masks), None
if kind == 'm57':
return kind, key, g.mesh57_key(p['.m57'], p['.ps57'], p['.root']), None
Expand Down
Loading