diff --git a/docs/experiments/native-compile-dxil-refusals.md b/docs/experiments/native-compile-dxil-refusals.md index b0346e2fd..494f62a51 100644 --- a/docs/experiments/native-compile-dxil-refusals.md +++ b/docs/experiments/native-compile-dxil-refusals.md @@ -33,6 +33,6 @@ ctest --preset release --no-tests=error -V -R "^native_compile_dxil_refusals$" - `--refusals` reports the first refusal reached per request by `read_graphics_request` / `prepare_direct_graphics(..., /*write_cache=*/false)` and `read_compute_request` / `translate_compute`; it does not walk past the first refusal or invoke DXC. -- Graphics requests use `req_.a`. `--requests` also compiles `.rtv` variant requests - ([note](native-rtv-variant-requests.md)), with the same `.fail` lifecycle; `--refusals` does not summarize - them. Slot-57 mesh requests remain outside the native path. +- Graphics requests use `req_.a` and `req_.rtv` ([RTV variant note](native-rtv-variant-requests.md)); + when both markers share a key, the variant takes precedence. Slot-57 mesh requests remain outside the + native path. diff --git a/docs/experiments/native-compile-dxil-watch.md b/docs/experiments/native-compile-dxil-watch.md index e37fdfb86..1c5b5f816 100644 --- a/docs/experiments/native-compile-dxil-watch.md +++ b/docs/experiments/native-compile-dxil-watch.md @@ -23,7 +23,7 @@ ctest --preset release --no-tests=error -V -R "^native_compile_dxil_watch$" ## Limits -- Watches startup graphics (`req_.a`) and slot-67 compute (`req_.c67`) requests; `.rtv` variants and - slot-57 mesh requests remain outside the native path. +- Watches startup graphics (`req_.a`), RTV variant (`req_.rtv`), and slot-67 compute + (`req_.c67`) requests; slot-57 mesh requests remain outside the native path. - `run.ps1 -Translate` still invokes `gfx2hlsl.py --watch` until the remaining graphics/runtime migration items in #2935 land. diff --git a/tests/native_compile_dxil_refusals_check.py b/tests/native_compile_dxil_refusals_check.py index 1f2040972..97f0e6c9e 100644 --- a/tests/native_compile_dxil_refusals_check.py +++ b/tests/native_compile_dxil_refusals_check.py @@ -16,6 +16,7 @@ import gfx2hlsl as G # noqa: E402 import triage_run # noqa: E402 +from native_rtv_variant_check import FLOAT32, UINT16, UNORM8, pixel_stage, put_variant # noqa: E402 from native_startup_request_cache_check import ( # noqa: E402 COMPUTE_GROUP, COMPUTE_RSRC2, @@ -28,6 +29,7 @@ ps_words, put_compute_request, put_graphics_request, + stream_vertex_container, ) from startup_compute_translate_check import state_a # noqa: E402 @@ -236,6 +238,43 @@ def main(argv: list[str]) -> int: "compute": {"examined": 0, "no_refusal": 0, "refused": 0, "reasons": []}, }, empty_json + # RTV variant requests (.rtv): valid + refused variants, read-only guarantee, and same-key dedup. + rtv_dir = work / "rtv_refusals" + rtv_dir.mkdir() + rtv_base = put_graphics_request(rtv_dir, stream_vertex_container(), pixel_stage(), g_root) + rtv_u16, _ = put_variant(rtv_dir, rtv_base, (UINT16, UNORM8)) + rtv_f32, _ = put_variant(rtv_dir, rtv_base, (FLOAT32,)) + rtv_forged, _ = put_variant(rtv_dir, rtv_base, (UINT16,), name_key=0x1111222233334444) + rtv_missing, _ = put_variant(rtv_dir, 0x9999888877776666, (UINT16,)) + rtv_bad_fmt, _ = put_variant(rtv_dir, rtv_base, (7,)) + + rtv_before = snapshot_tree(rtv_dir) + r_rtv1 = subprocess.run([cli, "--refusals", rtv_dir, "--json"], capture_output=True, text=True, check=True) + r_rtv2 = subprocess.run( + [cli, "--refusals", rtv_dir, "--jobs", "2", "--json"], capture_output=True, text=True, check=True + ) + assert snapshot_tree(rtv_dir) == rtv_before, "--refusals modified RTV request directory" + assert r_rtv1.stdout == r_rtv2.stdout, (r_rtv1.stdout, r_rtv2.stdout) + rtv_json = json.loads(r_rtv1.stdout) + assert rtv_json["graphics"]["examined"] == 6, rtv_json + assert rtv_json["graphics"]["no_refusal"] == 3, rtv_json + assert rtv_json["graphics"]["refused"] == 3, rtv_json + for variant_key in (rtv_base, rtv_u16, rtv_f32, rtv_forged, rtv_missing, rtv_bad_fmt): + assert f"{variant_key:016x}" not in r_rtv1.stdout.lower(), (variant_key, r_rtv1.stdout) + + dedupe_dir = work / "rtv_dedupe" + dedupe_dir.mkdir() + dedupe_key = put_graphics_request(dedupe_dir, stream_vertex_container(), pixel_stage(), g_root) + put_variant(dedupe_dir, dedupe_key, (UINT16,), name_key=dedupe_key) + r_dedupe = subprocess.run([cli, "--refusals", dedupe_dir, "--json"], capture_output=True, text=True, check=True) + dedupe_json = json.loads(r_dedupe.stdout) + assert dedupe_json["graphics"] == { + "examined": 1, + "no_refusal": 0, + "refused": 1, + "reasons": [{"reason": "invalid RTV variant request", "count": 1}], + }, dedupe_json + # 3. Negative controls for invalid --refusals CLI invocations. bad_file = work / "not_a_dir.txt" bad_file.write_text("x", encoding="utf-8") diff --git a/tests/native_compile_dxil_watch_check.py b/tests/native_compile_dxil_watch_check.py index 91eeecc9a..4f5b61a71 100644 --- a/tests/native_compile_dxil_watch_check.py +++ b/tests/native_compile_dxil_watch_check.py @@ -16,6 +16,7 @@ sys.path.insert(0, str(HERE.parent / "tools" / "startup")) import gfx2hlsl as G # 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, COMPUTE_RSRC2, @@ -151,6 +152,34 @@ def main(argv: list[str]) -> int: assert rs.stdout == rp.stdout, (rs.stdout, rp.stdout) assert G.pending_keys(str(d_parallel), skip_failed=True) == [] + # RTV variants in --once: compile base + variants, publish rtv1 manifest + .psdep, and recheck on base .psin update. + d_rtv = work / "once_rtv" + d_rtv.mkdir() + rtv_base = put_graphics_request(d_rtv, VS_WORDS, ps_words(4), g_root, TARGET_MASKS) + rtv_u16, rtv_u16_line = put_variant(d_rtv, rtv_base, (UINT16, UNORM8)) + rtv_f32, rtv_f32_line = put_variant(d_rtv, rtv_base, (FLOAT32,)) + rtv_bad, _ = put_variant(d_rtv, rtv_base, (7,)) + r_rtv = run_once(cli, sdk, frontend, d_rtv, jobs=2) + assert r_rtv.returncode == 0, (r_rtv.returncode, r_rtv.stdout, r_rtv.stderr) + for vkey, vline in ((rtv_u16, rtv_u16_line), (rtv_f32, rtv_f32_line)): + assert f"{vkey:016x}: compiled graphics" in r_rtv.stdout, r_rtv.stdout + vtxt = (d_rtv / f"{vkey:016x}.txt").read_text(encoding="ascii") + assert vtxt.splitlines()[-1] == vline, vtxt + assert (d_rtv / f"{vkey:016x}.psdep").is_file() + assert not (d_rtv / f"{vkey:016x}.fail").exists() + assert f"{rtv_bad:016x}: refused:" in r_rtv.stdout and (d_rtv / f"{rtv_bad:016x}.fail").is_file() + assert G.pending_keys(str(d_rtv), skip_failed=True) == [] + + # Updating the base request's .psin (with mismatched .b) rechecks both the base and its .rtv variants. + (d_rtv / f"req_{rtv_base:016x}.b").write_bytes(ps_words(2)) + touch(d_rtv / f"req_{rtv_base:016x}.psin", mtime=time.time() + 2.0) + r_rtv_recheck = run_once(cli, sdk, frontend, d_rtv, jobs=2) + assert r_rtv_recheck.returncode == 0, (r_rtv_recheck.returncode, r_rtv_recheck.stdout, r_rtv_recheck.stderr) + for key in (rtv_base, rtv_u16, rtv_f32): + assert f"{key:016x}: refused:" in r_rtv_recheck.stdout, r_rtv_recheck.stdout + assert not (d_rtv / f"{key:016x}.txt").exists() + assert (d_rtv / f"{key:016x}.fail").is_file() + # 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" @@ -199,6 +228,18 @@ def main(argv: list[str]) -> int: assert (wdir / f"{mut_key:016x}.vs.cso").read_bytes().startswith(b"DXBC") assert (wdir / f"{mut_key:016x}.ps.cso").read_bytes().startswith(b"DXBC") + # Write a valid .rtv variant and an invalid .rtv variant while the watcher is running. + live_rtv, live_rtv_line = put_variant(wdir, mut_key, (UINT16, UNORM8)) + live_rtv_bad, _ = put_variant(wdir, mut_key, (7,)) + live_rtv_txt = wdir / f"{live_rtv:016x}.txt" + live_rtv_fail = wdir / f"{live_rtv_bad:016x}.fail" + assert wait_for(lambda: live_rtv_txt.exists() and live_rtv_fail.exists()), ( + "watcher did not process newly written .rtv variants" + ) + assert live_rtv_txt.read_text(encoding="ascii").splitlines()[-1] == live_rtv_line + assert (wdir / f"{live_rtv:016x}.vs.cso").read_bytes().startswith(b"DXBC") + assert (wdir / f"{live_rtv:016x}.ps.cso").read_bytes().startswith(b"DXBC") + # Write a new compute request while the watcher is running. live_cs = put_compute_request(wdir, c_code, c_root, c_sa) live_cs_txt = wdir / f"{live_cs:016x}.txt" diff --git a/tools/startup/compile_dxil.cpp b/tools/startup/compile_dxil.cpp index 8f164714e..7fb8f8da6 100644 --- a/tools/startup/compile_dxil.cpp +++ b/tools/startup/compile_dxil.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -366,6 +367,45 @@ static std::string json_escape(std::string_view text) { return out; } +static void dedupe_graphics_markers(std::vector& paths) { + // Requires paths to be sorted so a stem's .a immediately precedes its .rtv. + // A key with both markers is one request, the variant, as in gfx2hlsl.py request_source. + for (size_t i = 0; i + 1 < paths.size();) { + if (paths[i].extension() == L".a" && paths[i + 1].extension() == L".rtv" && + paths[i].stem() == paths[i + 1].stem()) + paths.erase(paths.begin() + i); + else + ++i; + } +} + +static std::optional graphics_psin_path(const fs::path& marker) { + if (marker.extension() == L".a") + return fs::path(marker).replace_extension(L".psin"); + if (marker.extension() != L".rtv") + return std::nullopt; + std::ifstream file(marker, std::ios::binary); + if (!file) + return std::nullopt; + char buf[257]{}; + file.read(buf, 256); + const std::string_view text(buf, static_cast(file.gcount())); + if (text.substr(0, 5) != "rtv1 ") + return std::nullopt; + const std::string_view rest = text.substr(5); + const std::string_view token = rest.substr(0, rest.find(' ')); + if (token.empty() || token.size() > 20 || token.find_first_not_of("0123456789") != token.npos) + return std::nullopt; + uint64_t base = 0; + const auto end = token.data() + token.size(); + const auto parsed = std::from_chars(token.data(), end, base); + if (parsed.ec != std::errc{} || parsed.ptr != end) + return std::nullopt; + wchar_t name[32]{}; + std::swprintf(name, std::size(name), L"req_%016llx.psin", static_cast(base)); + return marker.parent_path() / name; +} + static int run_refusals(int argc, wchar_t** argv) { fs::path request_dir; bool has_dir = false; @@ -406,12 +446,13 @@ static int run_refusals(int argc, wchar_t** argv) { const auto path = file.path(); if (path.stem().wstring().rfind(L"req_", 0) != 0) continue; - if (path.extension() == L".a") + if (path.extension() == L".a" || path.extension() == L".rtv") graphics_inputs.push_back(fs::weakly_canonical(path)); else if (path.extension() == L".c67") compute_inputs.push_back(fs::weakly_canonical(path)); } std::sort(graphics_inputs.begin(), graphics_inputs.end(), PathLess{}); + dedupe_graphics_markers(graphics_inputs); std::sort(compute_inputs.begin(), compute_inputs.end(), PathLess{}); struct Task { @@ -439,8 +480,13 @@ static int run_refusals(int argc, wchar_t** argv) { } else { auto input = x2w::read_graphics_request(graphics_inputs[task.index]); bool hit = false; - (void)x2w::prepare_direct_graphics(input.a, input.b, request_dir, hit, - input.root, input.input_layout, input.pixel_entry, false); + auto pair = x2w::prepare_direct_graphics(input.a, input.b, request_dir, hit, + input.root, input.input_layout, input.pixel_entry, + false, input.rtv_formats); + if (!input.rtv_request.empty() && + !x2w::format_graphics_runtime_manifest(pair, input.rtv_request)) + throw std::runtime_error( + "RTV variant has no runtime manifest: its vertex inputs, target types or bindings are not described"); } } catch (const fs::filesystem_error&) { results[t] = "unreadable shader request"; @@ -583,13 +629,14 @@ static bool is_watch_request_pending(const fs::path& request_dir, const WatchReq std::optional psin_mtime; bool lost = false; if (!req.compute) { - const fs::path psin_path = fs::path(req.marker).replace_extension(L".psin"); - if (fs::is_regular_file(psin_path, ec)) { - const auto t = fs::last_write_time(psin_path, ec); - if (!ec) - psin_mtime = t; + if (const auto psin_path = graphics_psin_path(req.marker)) { + if (fs::is_regular_file(*psin_path, ec)) { + const auto t = fs::last_write_time(*psin_path, ec); + if (!ec) + psin_mtime = t; + } + lost = !psin_mtime.has_value() && fs::is_regular_file(psdep_path, ec); } - lost = !psin_mtime.has_value() && fs::is_regular_file(psdep_path, ec); } const bool has_manifest = fs::is_regular_file(manifest_path, ec); @@ -627,7 +674,7 @@ static std::vector collect_watch_requests(const fs::path& request_ const auto path = file.path(); const auto ext = path.extension().wstring(); const bool is_compute = ext == L".c67"; - if (!is_compute && ext != L".a") + if (!is_compute && ext != L".a" && ext != L".rtv") continue; const auto stem = path.stem().wstring(); if (stem.size() != 20 || stem.rfind(L"req_", 0) != 0) @@ -640,6 +687,13 @@ static std::vector collect_watch_requests(const fs::path& request_ std::sort(found.begin(), found.end(), [](const WatchRequest& a, const WatchRequest& b) { return PathLess{}(a.marker, b.marker); }); + for (size_t i = 0; i + 1 < found.size();) { + if (!found[i].compute && !found[i + 1].compute && found[i].marker.extension() == L".a" && + found[i + 1].marker.extension() == L".rtv" && found[i].key == found[i + 1].key) + found.erase(found.begin() + i); + else + ++i; + } std::vector pending; for (auto& req : found) { if (is_watch_request_pending(request_dir, req, skip_failed)) @@ -671,10 +725,13 @@ static const char* compile_watch_item(x2w::ShaderDxilCompiler& compiler, const f auto input = x2w::read_graphics_request(req.marker); bool hit = false; auto pair = x2w::prepare_direct_graphics(input.a, input.b, cache_dir, hit, input.root, input.input_layout, - input.pixel_entry); - auto manifest = x2w::format_graphics_runtime_manifest(pair); + input.pixel_entry, true, input.rtv_formats); + auto manifest = x2w::format_graphics_runtime_manifest(pair, input.rtv_request); if (!manifest) - throw std::runtime_error("unsupported startup runtime manifest bindings"); + throw std::runtime_error( + input.rtv_request.empty() + ? "unsupported startup runtime manifest bindings" + : "RTV variant has no runtime manifest: its vertex inputs, target types or bindings are not described"); auto vs_dxil = compile_to_dxil(compiler, pair.vertex.dxil_ir, L"vs_6_6", cache_dir, true, sink); auto ps_dxil = compile_to_dxil(compiler, pair.pixel.dxil_ir, L"ps_6_6", cache_dir, true, sink); publish_atomic(fs::path(stem) += L".vs.cso", vs_dxil.data(), vs_dxil.size()); @@ -863,12 +920,7 @@ int wmain(int argc, wchar_t** argv) { } if (inputs.empty()) throw std::runtime_error("no ready shader requests"); std::sort(inputs.begin(),inputs.end()); - // A key with both markers is one request, the variant, as in gfx2hlsl.py request_source. The two would - // otherwise name the same outputs. - for (size_t i = 0; i + 1 < inputs.size();) - if (inputs[i].extension()==L".a" && inputs[i+1].extension()==L".rtv" && inputs[i].stem()==inputs[i+1].stem()) - inputs.erase(inputs.begin()+i); - else ++i; + dedupe_graphics_markers(inputs); fs::create_directories(argv[first+2]); for (const auto& path : inputs) { auto key_stem = path.stem().wstring().substr(4);