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
33 changes: 23 additions & 10 deletions src/shader_compute_request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,30 @@ x2w::GraphicsRequest read_rtv_variant(const fs::path &marker) {
request.rtv_formats = std::move(formats);
return request;
}
void read_pixel_inputs(const fs::path &marker, uint64_t expected, Bytes &entry, Bytes &description) {
if (fs::exists(fs::path(marker).replace_extension(".psin")))
parse_pixel_inputs(marker, expected, entry, description);
if (fs::exists(fs::path(marker).replace_extension(".ps57in"))) {
if (!entry.empty())
throw std::runtime_error("conflicting stream and slot-57 pixel entry records");
entry = part(marker, ".ps57in", 12);
if (entry.size() != 12 || entry.compare(0, 4, "PSI1"))
throw std::runtime_error("invalid slot-57 pixel entry record");
description = entry;
}
}
} // namespace

std::string x2w::read_pixel_input_description(const fs::path &base_marker) {
const auto stem = base_marker.stem().string();
if (base_marker.extension() != ".a" || stem.size() != 20 || stem.compare(0, 4, "req_"))
throw std::runtime_error("expected req_<16 hex digits>.a");
const uint64_t expected = parse_request_stem(stem, "invalid graphics request key");
Bytes entry, description;
read_pixel_inputs(base_marker, expected, entry, description);
return description;
}

x2w::GraphicsRequest x2w::read_graphics_request(const fs::path &marker) {
if (marker.extension() == ".rtv") return read_rtv_variant(marker);
auto stem = marker.stem().string();
Expand All @@ -193,16 +215,7 @@ x2w::GraphicsRequest x2w::read_graphics_request(const fs::path &marker) {
part(marker, ".root", 16 << 20), {}, {}, {}, expected};
if (fs::exists(fs::path(marker).replace_extension(".ia")))
request.input_layout = part(marker, ".ia", 1 << 20);
if (fs::exists(fs::path(marker).replace_extension(".psin")))
parse_pixel_inputs(marker, expected, request.pixel_entry, request.pixel_input_description);
if (fs::exists(fs::path(marker).replace_extension(".ps57in"))) {
if (!request.pixel_entry.empty())
throw std::runtime_error("conflicting stream and slot-57 pixel entry records");
request.pixel_entry = part(marker, ".ps57in", 12);
if (request.pixel_entry.size() != 12 || request.pixel_entry.compare(0, 4, "PSI1"))
throw std::runtime_error("invalid slot-57 pixel entry record");
request.pixel_input_description = request.pixel_entry;
}
read_pixel_inputs(marker, expected, request.pixel_entry, request.pixel_input_description);
const auto request_key = [&](std::string_view revision) {
uint64_t key = 1469598103934665603ull;
hash_bytes(key, revision.data(), revision.size());
Expand Down
2 changes: 2 additions & 0 deletions src/shader_compute_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ struct GraphicsRequest {
};
// marker: req_<key>.a, or req_<key>.rtv for an RTV variant of a base request in the same directory.
GraphicsRequest read_graphics_request(const std::filesystem::path &marker);
// Canonical pixel-input sidecar (.psin or .ps57in) content for req_<key>.a; empty when absent, throws if invalid/conflicting.
std::string read_pixel_input_description(const std::filesystem::path &base_marker);
std::string read_shader_container_bytes(const std::filesystem::path &file);
ComputeContainer read_compute_container(const std::filesystem::path &file);
// Verify the content key over all six immutable capture parts before exposing any bytes.
Expand Down
103 changes: 103 additions & 0 deletions tests/native_compile_dxil_watch_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
sys.path.insert(0, str(HERE.parent / "tools" / "startup"))

import gfx2hlsl as G # noqa: E402
from native_direct_container_check import patched_pixel, publish as publish_raw, vertex as container_vertex, words as raw_words # noqa: E402
from native_rtv_variant_check import FLOAT32, UINT16, UNORM8, put_variant # noqa: E402
from native_startup_request_cache_check import ( # noqa: E402
COMPUTE_GROUP,
Expand Down Expand Up @@ -136,6 +137,19 @@ def main(argv: list[str]) -> int:
assert not (d_lost / f"{lost_key:016x}.txt").exists()
assert (d_lost / f"{lost_key:016x}.fail").is_file()

# A 0-byte <key>.psdep next to a published manifest + valid .psin triggers retranslation and restores .psdep.
zero_psdep = d_lost / "zero_psdep"
zero_psdep.mkdir()
z_key = put_graphics_request(zero_psdep, VS_WORDS, ps_words(4), g_root, TARGET_MASKS)
assert run_once(cli, sdk, frontend, zero_psdep, jobs=1).returncode == 0
z_psdep = zero_psdep / f"{z_key:016x}.psdep"
orig_dep = z_psdep.read_bytes()
assert orig_dep
z_psdep.write_bytes(b"")
r_zero = run_once(cli, sdk, frontend, zero_psdep, jobs=1)
assert r_zero.returncode == 0 and f"{z_key:016x}: compiled graphics" in r_zero.stdout, (r_zero.stdout, r_zero.stderr)
assert z_psdep.read_bytes() == orig_dep

# A copied slot-57 sidecar can be older than the published stream manifest.
conflict_dir = work / "copied_pixel_sidecar"
conflict_dir.mkdir()
Expand Down Expand Up @@ -194,6 +208,95 @@ def main(argv: list[str]) -> int:
assert not (d_rtv / f"{key:016x}.txt").exists()
assert (d_rtv / f"{key:016x}.fail").is_file()

# Issue #3133: sidecar content changes or contradictory .ps57in with preserved/older mtimes must invalidate.
# Contradictory .ps57in appearing with older mtime vs newer mtime must withdraw stale .txt identically.
outcomes_ps57 = []
for label, offset in (("older", -60.0), ("newer", 10.0)):
d_cmp = work / f"ps57_contradict_{label}"
d_cmp.mkdir()
s_key = put_graphics_request(d_cmp, VS_WORDS, ps_words(4), g_root, TARGET_MASKS)
r_pub = run_once(cli, sdk, frontend, d_cmp, jobs=1)
assert r_pub.returncode == 0 and f"{s_key:016x}: compiled graphics" in r_pub.stdout, r_pub.stdout
txt_path = d_cmp / f"{s_key:016x}.txt"
fail_path = d_cmp / f"{s_key:016x}.fail"
assert txt_path.is_file() and (d_cmp / f"{s_key:016x}.psdep").is_file()
# Unchanged-input cache-hit control before adding contradictory .ps57in.
txt_ns = txt_path.stat().st_mtime_ns
r_hit = run_once(cli, sdk, frontend, d_cmp, jobs=1)
assert r_hit.returncode == 0 and f"{s_key:016x}:" not in r_hit.stdout and txt_path.stat().st_mtime_ns == txt_ns
touch(
d_cmp / f"req_{s_key:016x}.ps57in",
b"PSI1" + struct.pack("<II", *DECOY_MASKS),
mtime=txt_path.stat().st_mtime + offset,
)
r_bad = run_once(cli, sdk, frontend, d_cmp, jobs=1)
assert r_bad.returncode == 0 and not txt_path.exists() and fail_path.is_file(), (label, r_bad.stdout, r_bad.stderr)
assert f"{s_key:016x}: refused: conflicting stream and slot-57 pixel entry records" in r_bad.stdout, r_bad.stdout
outcomes_ps57.append((r_bad.stdout, fail_path.read_text(encoding="utf-8")))
assert outcomes_ps57[0] == outcomes_ps57[1], outcomes_ps57

# Changed .psin content preserving an older mtime must withdraw stale .txt and report conflict/mismatch.
d_psin_mtime = work / "psin_preserved_mtime"
d_psin_mtime.mkdir()
pm_key = put_graphics_request(d_psin_mtime, VS_WORDS, ps_words(4), g_root, TARGET_MASKS)
assert run_once(cli, sdk, frontend, d_psin_mtime, jobs=1).returncode == 0
pm_txt = d_psin_mtime / f"{pm_key:016x}.txt"
older_t = pm_txt.stat().st_mtime - 60.0
touch(
d_psin_mtime / f"req_{pm_key:016x}.psin",
describe_psin(pm_key, DECOY_MASKS).encode("ascii"),
mtime=older_t,
)
r_pm = run_once(cli, sdk, frontend, d_psin_mtime, jobs=1)
assert r_pm.returncode == 0 and not pm_txt.exists() and (d_psin_mtime / f"{pm_key:016x}.fail").is_file()
assert f"{pm_key:016x}: refused: stale pixel input description: graphics request key mismatch" in r_pm.stdout, r_pm.stdout

# Slot-57 .ps57in-only unchanged cache-hit control and changed-content preserved-mtime regression.
d_s57 = work / "ps57_preserved_mtime"
idx57 = work / "index57"
frontend57 = work / "frontend57"
idx57.mkdir()
vp57 = [0x7E0002F2, 0xF80008CF, 0, 0xF800020F, 0, 0xF800021F, 0, 0xBF810000]
(idx57 / "varying.cdxil").write_bytes(container_vertex(program=vp57))
interp57 = lambda attr, ch, op, dst, src: 0xC8000000 | dst << 18 | op << 16 | attr << 10 | ch << 8 | src
prefix57 = [0xD5800000, 255, 17, 0xD5800000, 255, 23]
tmpl57 = prefix57 + [interp57(0, 0, 0, 4, 0), interp57(0, 0, 1, 4, 1)]
attrs57 = [(6, row, 15, row, 0, 0, row, 2) for row in range(2)]
(idx57 / "smooth.cdxil").write_bytes(patched_pixel(len(tmpl57), tmpl57, input_address=2, attributes=attrs57))
subprocess.run([cli, "--index-graphics-interfaces", idx57, frontend57], check=True, capture_output=True)
root57 = b"DXBC" + bytes(16) + raw_words(1, 68, 1, 36) + b"RTS0" + raw_words(24, 2, 0, 24, 0, 24, 0)
code57 = raw_words(*tmpl57, interp57(0, 1, 2, 5, 2), 0xF800180F, 0x05040504, 0xBF810000)
k57 = publish_raw(d_s57, code57, raw_words(*vp57), root57, slot57_inputs=(2, 1))
ps57_bytes = b"PSI1" + struct.pack("<II", 2, 1)
assert run_once(cli, sdk, frontend57, d_s57, jobs=1).returncode == 0
txt57 = d_s57 / f"{k57}.txt"
assert txt57.is_file() and (d_s57 / f"{k57}.psdep").read_bytes() == ps57_bytes
txt57_ns = txt57.stat().st_mtime_ns
r57_hit = run_once(cli, sdk, frontend57, d_s57, jobs=1)
assert r57_hit.returncode == 0 and f"{k57}:" not in r57_hit.stdout and txt57.stat().st_mtime_ns == txt57_ns
touch(
d_s57 / f"req_{k57}.ps57in",
b"PSI1" + struct.pack("<II", 2, 2),
mtime=txt57.stat().st_mtime - 60.0,
)
r57_bad = run_once(cli, sdk, frontend57, d_s57, jobs=1)
assert r57_bad.returncode == 0 and not txt57.exists() and (d_s57 / f"{k57}.fail").is_file()
assert f"{k57}: refused: stale pixel input description: graphics request key mismatch" in r57_bad.stdout, r57_bad.stdout

# RTV variant must also withdraw when base gains contradictory .ps57in with older mtime.
d_rtv57 = work / "rtv_ps57_older"
d_rtv57.mkdir()
b57 = put_graphics_request(d_rtv57, VS_WORDS, ps_words(4), g_root, TARGET_MASKS)
v57, _ = put_variant(d_rtv57, b57, (UINT16, UNORM8))
assert run_once(cli, sdk, frontend, d_rtv57, jobs=2).returncode == 0
min_txt_mtime = min((d_rtv57 / f"{k:016x}.txt").stat().st_mtime for k in (b57, v57))
touch(d_rtv57 / f"req_{b57:016x}.ps57in", b"PSI1" + struct.pack("<II", *DECOY_MASKS), mtime=min_txt_mtime - 60.0)
r_rtv57 = run_once(cli, sdk, frontend, d_rtv57, jobs=2)
assert r_rtv57.returncode == 0
for key in (b57, v57):
assert not (d_rtv57 / f"{key:016x}.txt").exists() and (d_rtv57 / f"{key:016x}.fail").is_file()
assert f"{key:016x}: refused:" in r_rtv57.stdout and "conflicting stream and slot-57 pixel entry records" in r_rtv57.stdout, r_rtv57.stdout

# Part 2: Live background --watch process retries pre-existing .fail once, skips unchanged .fail,
# translates newly written graphics/compute requests, and clears .fail when fixed.
wdir = work / "live_watch"
Expand Down
48 changes: 30 additions & 18 deletions tools/startup/compile_dxil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,18 @@ struct WatchRequest {
bool compute = false;
};

static bool read_small_file(const fs::path& path, size_t max_bytes, std::string& out) {
std::ifstream file(path, std::ios::binary | std::ios::ate);
if (!file)
return false;
const auto size = file.tellg();
if (size < 0 || static_cast<size_t>(size) > max_bytes)
return false;
out.assign(static_cast<size_t>(size), '\0');
file.seekg(0);
return !size || static_cast<bool>(file.read(out.data(), size));
}

static bool is_watch_request_pending(const fs::path& request_dir, const WatchRequest& req, bool skip_failed) {
std::error_code ec;
const auto marker_mtime = fs::last_write_time(req.marker, ec);
Expand All @@ -704,37 +716,35 @@ static bool is_watch_request_pending(const fs::path& request_dir, const WatchReq
const fs::path psdep_path = fs::path(stem) += L".psdep";

std::optional<fs::file_time_type> psin_mtime;
bool dependency_changed = false;
bool sidecar_changed = false;
if (!req.compute) {
if (const auto psin_path = graphics_psin_path(req.marker)) {
const auto packed_path = fs::path(*psin_path).replace_extension(L".ps57in");
const bool stream = fs::is_regular_file(*psin_path, ec);
const bool packed = fs::is_regular_file(packed_path, ec);
std::ifstream dependency(psdep_path, std::ios::binary);
char previous[13]{};
dependency.read(previous, sizeof(previous));
const auto previous_size = dependency.gcount();
const bool was_packed = previous_size >= 4 && std::memcmp(previous, "PSI1", 4) == 0;
if (packed) {
// A copied sidecar may carry a timestamp older than the published manifest.
std::ifstream input(packed_path, std::ios::binary);
char current[13]{};
input.read(current, sizeof(current));
dependency_changed = stream || !was_packed || previous_size != 12 || input.gcount() != 12 ||
std::memcmp(current, previous, 12) != 0;
} else {
dependency_changed = was_packed || (stream != (previous_size != 0));
}
if (stream || packed) {
const auto time = fs::last_write_time(stream ? *psin_path : packed_path, ec);
if (!ec)
psin_mtime = time;
}
const bool has_psdep = fs::is_regular_file(psdep_path, ec);
std::string published_dep;
if (has_psdep && (!read_small_file(psdep_path, 256, published_dep) || published_dep.empty())) {
sidecar_changed = true;
} else {
try {
const std::string current_dep =
x2w::read_pixel_input_description(fs::path(*psin_path).replace_extension(L".a"));
sidecar_changed = current_dep != published_dep;
} catch (const std::exception&) {
sidecar_changed = true;
}
}
}
}

const bool has_manifest = fs::is_regular_file(manifest_path, ec);
if (has_manifest && !dependency_changed) {
if (has_manifest && !sidecar_changed) {
const auto manifest_mtime = fs::last_write_time(manifest_path, ec);
if (!ec && !(psin_mtime.has_value() && *psin_mtime > manifest_mtime)) {
if (req.compute) {
Expand All @@ -748,7 +758,7 @@ static bool is_watch_request_pending(const fs::path& request_dir, const WatchReq
}
}

const bool recheck = dependency_changed && has_manifest;
const bool recheck = sidecar_changed && has_manifest;
if (skip_failed && !recheck && fs::is_regular_file(fail_path, ec)) {
const auto fail_mtime = fs::last_write_time(fail_path, ec);
const auto latest_input =
Expand Down Expand Up @@ -1160,6 +1170,8 @@ int wmain(int argc, wchar_t** argv) {
if (manifest) {
if (!input.pixel_input_description.empty())
publish_atomic(psdep_path, input.pixel_input_description.data(), input.pixel_input_description.size());
else
fs::remove(psdep_path, ignored);
publish_atomic(manifest_path, manifest->data(), manifest->size());
}
fs::remove(fs::path(stems[i]) += L".fail", ignored);
Expand Down