From 06c7380b733333b1e7c2a0d95c2a103ef345a8bf Mon Sep 17 00:00:00 2001 From: Roland Edi Date: Fri, 27 Nov 2026 16:52:07 +0000 Subject: [PATCH 1/7] runtime: keep the published disk pair after candidate PSO failure A native graphics candidate reports ready=1 before pipeline_for creates its desktop PSO. When CreateGraphicsPipelineState failed, the draw returned null and cached that result, so the target stayed clear even when a valid published pair existed in the shader cache. Reload that pair for the same render-target key, bump a translation revision so earlier native PSOs are not reused, and refuse the draw with a diagnostic when the pair is missing or invalid. Native-only mode keeps its explicit refusal. The published manifest and stage files are read, never rewritten. Refs #3134 --- src/startup_stubs/d3d12_draw.inl | 56 ++++++++++++--- tests/startup_native_graphics_draw_check.py | 77 ++++++++++++++++++++- 2 files changed, 121 insertions(+), 12 deletions(-) diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index b1f4ff385c..4f9c12d5a3 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -175,6 +175,9 @@ struct Translation { x2w_startup::ComPtr indexed_signature; std::unordered_map pipelines; // by target formats u64 key = 0; + bool native_candidate = false; // shaders and manifest were prepared in-process; a published disk pair may also exist + u64 revision = 0; // bumped when a disk pair replaces the native shaders, invalidating created PSOs + std::string rtv_request; // the RTV variant text this translation was prepared for, else empty LONG logged_draws = 0, logged_bindings = 0, logged_large = 0; // diagnostics budget per translation }; @@ -287,7 +290,7 @@ bool native_shaders_only() return enabled; } -void load_translation(Translation& tr, const std::string& rtv_request = {}, std::string manifest = {}) +void load_translation(Translation& tr, const std::string& rtv_request = {}, std::string manifest = {}, bool count = true) { tr.state = 2; if (manifest.empty() && native_shaders_only()) { @@ -298,7 +301,7 @@ void load_translation(Translation& tr, const std::string& rtv_request = {}, std: const bool provided = !manifest.empty(); if (!cache_path(path, L"", tr.key, L".txt")) return; if (!provided && !read_text(path, manifest)) { - if (!tr.manifest_retry.pending()) { + if (count && !tr.manifest_retry.pending()) { InterlockedIncrement(&translation_missing); static LONG missing; if (rtv_request.empty() && first_calls(missing, 12)) @@ -418,8 +421,8 @@ void load_translation(Translation& tr, const std::string& rtv_request = {}, std: return; } tr.state = 1; - if (previously_missing) InterlockedDecrement(&translation_missing); - InterlockedIncrement(&translation_ready); + if (count && previously_missing) InterlockedDecrement(&translation_missing); + if (count) InterlockedIncrement(&translation_ready); log("x2w-draw pipeline %016llx translated: %zu entry values, %zu resources, %zu writable textures, %zu static samplers, sampler_heap=%u", tr.key, tr.uds.size(), tr.srvs.size(), tr.uavs.size(), tr.samplers.statics.size(), tr.samplers.heap ? 1u : 0u); } @@ -452,6 +455,7 @@ static bool try_native_graphics(Translation& tr, bool variant = false) auto ps = stage(pair.pixel.dxil_ir, L"ps_6_6", ps_hit); Translation candidate; candidate.key = tr.key; + candidate.rtv_request = request.rtv_request; if (!pair.input_layout.empty()) { try { candidate.fixed_rectangle_ir = x2w::native_rectangle_geometry(pair.vertex.outputs, pair.pixel.inputs); } catch (const x2w::UnsupportedComputeShader&) {} // Triangle lists and strips still use the original pair. @@ -467,6 +471,7 @@ static bool try_native_graphics(Translation& tr, bool variant = false) } candidate.vs = vs.Detach(); candidate.ps = ps.Detach(); + candidate.native_candidate = true; tr = std::move(candidate); log("x2w-draw native graphics %016llx ready=%u cache(frontend,vs,ps)=%u,%u,%u", tr.key, tr.state == 1, frontend_hit, vs_hit, ps_hit); @@ -1099,6 +1104,7 @@ Translation* translation_for_targets(Translation* base, const Pass& pass) if (!entry) { entry = new Translation; entry->key = key; + entry->rtv_request = request; #ifdef X2W_NATIVE_SHADERS if (GetEnvironmentVariableW(L"X2W_NATIVE_SHADERS", nullptr, 0) && publish_shader_request(key, L".rtv", request.data(), request.size()) && try_native_graphics(*entry, true)) return entry; @@ -1131,6 +1137,7 @@ ID3D12PipelineState* pipeline_for(BoundPipeline& bound, Translation& tr, const P if (use_depth_stencil) key = fnv_bytes(&bound.depth_stencil, sizeof(bound.depth_stencil), key); if (bound.has_rasterizer) key = fnv_bytes(&bound.rasterizer, sizeof(bound.rasterizer), key); key = fnv_bytes(&indexed_rect, sizeof(indexed_rect), key); + if (tr.revision) key = fnv_bytes(&tr.revision, sizeof(tr.revision), key); const auto found = bound.pipelines.find(key); if (found != bound.pipelines.end()) return found->second; D3D12_GRAPHICS_PIPELINE_STATE_DESC desc{}; @@ -1204,11 +1211,42 @@ ID3D12PipelineState* pipeline_for(BoundPipeline& bound, Translation& tr, const P desc.SampleDesc.Count = 1; ID3D12PipelineState* pipeline = nullptr; X2W_ZONE("desktop pipeline creation"); - const HRESULT hr = device_of(the_device)->CreateGraphicsPipelineState(&desc, IID_PPV_ARGS(&pipeline)); - log("x2w-draw desktop pipeline for %016llx, %u targets (format %d) -> 0x%08lx (dsv %d, depth %d)", tr.key, - pass.target_count, pass.target_count ? pass.target_formats[0] : 0, static_cast(hr), - static_cast(dsv_format), desc.DepthStencilState.DepthEnable ? 1 : 0); - check_device("translated CreateGraphicsPipelineState", 0); + HRESULT hr = E_FAIL; + const bool injected = tr.native_candidate && x2w_local::env_on("X2W_TEST_NATIVE_GRAPHICS_PSO_FAILURE"); + if (!injected) { + hr = device_of(the_device)->CreateGraphicsPipelineState(&desc, IID_PPV_ARGS(&pipeline)); + log("x2w-draw desktop pipeline for %016llx, %u targets (format %d) -> 0x%08lx (dsv %d, depth %d)", tr.key, + pass.target_count, pass.target_count ? pass.target_formats[0] : 0, static_cast(hr), + static_cast(dsv_format), desc.DepthStencilState.DepthEnable ? 1 : 0); + check_device("translated CreateGraphicsPipelineState", 0); + } + if (FAILED(hr) && pipeline) { + pipeline->Release(); + pipeline = nullptr; + } + if (FAILED(hr) && tr.native_candidate) { + // A native candidate whose shaders this device cannot turn into a PSO keeps the published pair usable. + log("x2w-draw native graphics %016llx candidate pipeline failed 0x%08lx%s", tr.key, + static_cast(hr), injected ? " (injected)" : ""); + if (!native_shaders_only()) { + Translation fallback; + fallback.key = tr.key; + fallback.rtv_request = tr.rtv_request; + load_translation(fallback, tr.rtv_request, {}, false); + if (fallback.state == 1) { + fallback.revision = tr.revision + 1; + tr = std::move(fallback); + log("x2w-draw native graphics %016llx published disk pair recovered", tr.key); + ID3D12PipelineState* recovered = pipeline_for(bound, tr, pass, indexed_rect); + if (!recovered) + log("x2w-draw native graphics %016llx published disk pair pipeline failed", tr.key); + return recovered; + } + log("x2w-draw native graphics %016llx no valid published disk pair", tr.key); + } else { + log("x2w-draw native graphics %016llx no published disk pair in native-only mode", tr.key); + } + } bound.pipelines[key] = pipeline; return pipeline; } diff --git a/tests/startup_native_graphics_draw_check.py b/tests/startup_native_graphics_draw_check.py index 5b0bd18a11..51cde53e9d 100644 --- a/tests/startup_native_graphics_draw_check.py +++ b/tests/startup_native_graphics_draw_check.py @@ -10,6 +10,12 @@ from startup_stream_draw_check import vop1, vop2 +def assert_debug_clean(stderr): + for line in stderr.splitlines(): + if "x2w-d3d12-debug " in line: + assert " sev=0 " not in line and " sev=1 " not in line, line + + def triangle(): code = [vop2(0x1a, 1, 129, 0), vop2(0x1b, 1, 130, 1), vop2(0x1b, 2, 130, 0)] for v in (1, 2): @@ -24,7 +30,7 @@ def check(folder, consumer, sdk, adapter, compiler): vs, ps, root, pixels = [folder / name for name in ("vs", "ps", "root", "pixels")] vs.write_bytes(triangle()) ps.write_bytes(patched_pixel(2, [vop1(1, 0, 128), vop1(1, 1, 242)], export=(0xf800180f, 0x01000001))) - env = dict(os.environ, X2W_SHADER_CACHE=str(cache), X2W_NATIVE_SHADERS=sdk, + env = dict(os.environ, X2W_SHADER_CACHE=str(cache), X2W_NATIVE_SHADERS=sdk, X2W_NATIVE_SHADERS_ONLY="0", X2W_D3D12_DEBUG="1", X2W_D3D12_RENDER="1", X2W_D3D12_PERMISSIVE="1", X2W_D3D12_LIST_NOOP="1") clear = bytes((0, 0, 255, 255)) red = bytes((255, 0, 0, 255)) @@ -47,7 +53,7 @@ def check(folder, consumer, sdk, adapter, compiler): subprocess.run([compiler, "--watch", sdk, cache, cache, "--once"], check=True, timeout=45) marker, = cache.glob("req_*.a") manifest = cache / (marker.stem.removeprefix("req_") + ".txt") - published = {p: p.read_bytes() for p in + published = {p: (p.read_bytes(), p.stat().st_mtime_ns) for p in (manifest, manifest.with_suffix(".vs.cso"), manifest.with_suffix(".ps.cso"))} for fault, diagnostic in (("MANIFEST", "unsupported manifest version"), ("ROOT", "failed 0x80004005")): manifest.rename(str(manifest) + ".pending") @@ -60,8 +66,73 @@ def check(folder, consumer, sdk, adapter, compiler): assert run.returncode == 0 and diagnostic in run.stderr and "-> drawn;" in run.stderr, run.stderr assert first.read_bytes() == clear * 32 assert pixels.read_bytes() == (red * 4 + clear * 4) * 4 - assert all(path.read_bytes() == expected for path, expected in published.items()) + assert all(path.read_bytes() == expected[0] and path.stat().st_mtime_ns == expected[1] + for path, expected in published.items()) print(f"{adapter} {fault}: same-process fallback drew 16 red pixels after publication") + + # The published pair alone draws the target when native preparation is disabled. + pixels.unlink() + disk_env = {name: value for name, value in env.items() if name != "X2W_NATIVE_SHADERS"} + run = subprocess.run(args, env=disk_env, capture_output=True, text=True, timeout=45) + assert run.returncode == 0, run.stderr + assert "native graphics" not in run.stderr and "-> drawn;" in run.stderr, run.stderr[-8000:] + assert_debug_clean(run.stderr) + assert pixels.read_bytes() == (red * 4 + clear * 4) * 4 + print(f"{adapter} disk pair: native disabled, 16 red and 16 clear pixels") + + # A candidate PSO failure falls back to that pair in the same draw, without rewriting it. + pso_env = dict(env, X2W_NATIVE_SHADERS_ONLY="0", X2W_TEST_NATIVE_GRAPHICS_PSO_FAILURE="1") + pixels.unlink() + run = subprocess.run(args, env=pso_env, capture_output=True, text=True, timeout=45) + assert run.returncode == 0, run.stderr + assert "candidate pipeline failed 0x80004005 (injected)" in run.stderr, run.stderr[-8000:] + assert "published disk pair recovered" in run.stderr and "-> drawn;" in run.stderr, run.stderr[-8000:] + assert_debug_clean(run.stderr) + assert pixels.read_bytes() == (red * 4 + clear * 4) * 4 + assert all(path.read_bytes() == expected[0] and path.stat().st_mtime_ns == expected[1] + for path, expected in published.items()) + print(f"{adapter} PSO: candidate failure recovered the published disk pair, 16 red and 16 clear pixels") + + # The native path still creates its own pipeline when no fault is injected. + pixels.unlink() + run = subprocess.run(args, env=env, capture_output=True, text=True, timeout=45) + assert run.returncode == 0, run.stderr + assert "native graphics" in run.stderr and "ready=1 cache(frontend,vs,ps)=1,1,1" in run.stderr, run.stderr[-8000:] + assert "candidate pipeline failed" not in run.stderr and "refused:" not in run.stderr, run.stderr[-8000:] + assert "-> drawn;" in run.stderr, run.stderr[-8000:] + assert_debug_clean(run.stderr) + assert pixels.read_bytes() == (red * 4 + clear * 4) * 4 + print(f"{adapter} native: successful candidate still drew 16 red and 16 clear pixels") + + # A missing or invalid pair refuses the draw with a diagnostic rather than an all-clear target. + ps_cso = manifest.with_suffix(".ps.cso") + held_ps = Path(str(ps_cso) + ".hold") + ps_cso.rename(held_ps) + try: + pixels.unlink() + run = subprocess.run(args, env=pso_env, capture_output=True, text=True, timeout=45) + assert run.returncode == 0, run.stderr + assert "no valid published disk pair" in run.stderr and "-> drawn;" not in run.stderr, run.stderr[-8000:] + assert pixels.read_bytes() == clear * 32 + print(f"{adapter} missing pair: refused with a diagnostic, 32 clear pixels") + finally: + held_ps.rename(ps_cso) + held_manifest = Path(str(manifest) + ".hold") + manifest.rename(held_manifest) + manifest.write_text("x2w-manifest 999\n") + try: + pixels.unlink() + run = subprocess.run(args, env=pso_env, capture_output=True, text=True, timeout=45) + assert run.returncode == 0, run.stderr + assert "unsupported manifest version" in run.stderr, run.stderr[-8000:] + assert "no valid published disk pair" in run.stderr and "-> drawn;" not in run.stderr, run.stderr[-8000:] + assert pixels.read_bytes() == clear * 32 + print(f"{adapter} invalid pair: refused with a diagnostic, 32 clear pixels") + finally: + manifest.unlink() + held_manifest.rename(manifest) + assert all(path.read_bytes() == expected[0] and path.stat().st_mtime_ns == expected[1] + for path, expected in published.items()) ps.write_bytes(patched_pixel(1, [0xffffffff], export=(0xf800180f, 0x01000001))) run = subprocess.run(args, env=env, capture_output=True, text=True, timeout=45) assert run.returncode == 0 and "refused:" in run.stderr and "-> drawn;" not in run.stderr, run.stderr From 9360bcaef424cf8a8bf8c0e495af4c057ce61dae Mon Sep 17 00:00:00 2001 From: Roland Edi Date: Fri, 27 Nov 2026 19:14:51 +0000 Subject: [PATCH 2/7] runtime: bind from the recovered translation after PSO fallback pipeline_for may replace the native candidate with the published disk translation. Create the PSO before preparing the D16, formatted-buffer and fixed-vertex bindings so they come from the final translation, and recompute indexed-rectangle geometry when the fallback changes the fixed stream. Add the RTV variant fallback and invalid pixel-blob cases. --- src/startup_stubs/d3d12_draw.inl | 54 ++++++++++++------- tests/startup_native_graphics_draw_check.py | 58 +++++++++++++++++++++ 2 files changed, 92 insertions(+), 20 deletions(-) diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 4f9c12d5a3..745a94a5c2 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -2560,20 +2560,47 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI BoundPipeline* bound = translation_for(pass.pipeline); Translation* tr = bound ? translation_for_targets(bound->translation, pass) : nullptr; if (indirect && tr) indirect->shader_key = tr->key; + if (!tr || tr->state != 1) { + if (indirect) indirect->refusal_code = indirect_refusal_code::translation_not_ready; + ReleaseSRWLockExclusive(&draw_lock); + return false; + } + // pipeline_for may replace the native candidate with the published disk translation. + // Create the PSO first, then derive every binding from the translation it leaves in tr. + bool fixed_stream = !tr->fixed_stream.empty(); + bool geometry_rect = indexed_rect || (rect && fixed_stream); + ID3D12PipelineState* pipeline = ensure_draw_heap(self->own().list_device) ? pipeline_for(*bound, *tr, pass, geometry_rect) : nullptr; + if (pipeline) { + const bool recovered_fixed_stream = !tr->fixed_stream.empty(); + const bool recovered_geometry_rect = indexed_rect || (rect && recovered_fixed_stream); + if (recovered_geometry_rect != geometry_rect) { + pipeline = pipeline_for(*bound, *tr, pass, recovered_geometry_rect); + geometry_rect = recovered_geometry_rect; + } + fixed_stream = recovered_fixed_stream; + } + if (indirect && indexed_rect && !pipeline) { + indirect->set_refusal("translated indexed rectangle geometry unavailable", + indirect_refusal_code::indexed_rect_unavailable); + } + if (!pipeline || (tr->samplers.heap && !prepare_draw_sampler_heap(pass, list, tr->samplers.reserved_fallback))) { + if (indirect && !(indexed_rect && !pipeline)) + indirect->refusal_code = !pipeline ? indirect_refusal_code::pipeline_unavailable + : indirect_refusal_code::sampler_heap_unavailable; + ReleaseSRWLockExclusive(&draw_lock); + return false; + } std::uint32_t d16_unorm16[32] = {}; FormattedBufferDescriptor formatted_buffers[32] = {}; - const bool translation_ok = tr && tr->state == 1; - const bool d16_ok = translation_ok && d16_srvs_ready(*tr, pass, d16_unorm16); + const bool d16_ok = d16_srvs_ready(*tr, pass, d16_unorm16); const bool formatted_ok = d16_ok && formatted_buffers_ready(*tr, pass, formatted_buffers); if (!formatted_ok) { - if (indirect && tr) - indirect->refusal_code = !translation_ok ? indirect_refusal_code::translation_not_ready - : !d16_ok ? indirect_refusal_code::d16_srv_not_ready - : indirect_refusal_code::formatted_buffer_not_ready; + if (indirect) + indirect->refusal_code = !d16_ok ? indirect_refusal_code::d16_srv_not_ready + : indirect_refusal_code::formatted_buffer_not_ready; ReleaseSRWLockExclusive(&draw_lock); return false; } - const bool fixed_stream = !tr->fixed_stream.empty(); D3D12_VERTEX_BUFFER_VIEW fixed_view{}; ID3D12Resource* fixed_resource = nullptr; if (fixed_stream && (indirect || !fixed_vertex_view(self, pass, *tr, indexed, start, count, fixed_view, fixed_resource))) { @@ -2636,19 +2663,6 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI return false; } } - const bool geometry_rect = indexed_rect || (rect && fixed_stream); - ID3D12PipelineState* pipeline = ensure_draw_heap(self->own().list_device) ? pipeline_for(*bound, *tr, pass, geometry_rect) : nullptr; - if (indirect && indexed_rect && !pipeline) { - indirect->set_refusal("translated indexed rectangle geometry unavailable", - indirect_refusal_code::indexed_rect_unavailable); - } - if (!pipeline || (tr->samplers.heap && !prepare_draw_sampler_heap(pass, list, tr->samplers.reserved_fallback))) { - if (indirect && !(indexed_rect && !pipeline)) - indirect->refusal_code = !pipeline ? indirect_refusal_code::pipeline_unavailable - : indirect_refusal_code::sampler_heap_unavailable; - ReleaseSRWLockExclusive(&draw_lock); - return false; - } PixelUavBinding uav_bindings[pixel_uav_limit]; if (!resolve_pixel_uavs(*tr, pass, uav_bindings)) { ReleaseSRWLockExclusive(&draw_lock); diff --git a/tests/startup_native_graphics_draw_check.py b/tests/startup_native_graphics_draw_check.py index 51cde53e9d..f51f7b268d 100644 --- a/tests/startup_native_graphics_draw_check.py +++ b/tests/startup_native_graphics_draw_check.py @@ -93,6 +93,45 @@ def check(folder, consumer, sdk, adapter, compiler): for path, expected in published.items()) print(f"{adapter} PSO: candidate failure recovered the published disk pair, 16 red and 16 clear pixels") + # An RTV variant is a separate published pair. Draw it natively first so the runtime + # publishes req_.rtv, compile that request, then inject the candidate PSO failure. + variant_args = args + ["2"] # DXGI_FORMAT_R32G32B32A32_FLOAT + variant_red = bytes.fromhex("0000803f00000000000000000000803f") + variant_clear = bytes.fromhex("00000000000000000000803f0000803f") + pixels.unlink() + run = subprocess.run(variant_args, env=env, capture_output=True, text=True, timeout=45) + assert run.returncode == 0 and "-> drawn;" in run.stderr, run.stderr[-8000:] + assert "no RTV translation" not in run.stderr, run.stderr[-8000:] + assert pixels.read_bytes() == variant_red, pixels.read_bytes().hex() + variant_request, = cache.glob("req_*.rtv") + variant_key = variant_request.stem.removeprefix("req_") + subprocess.run([compiler, "--watch", sdk, cache, cache, "--once"], check=True, timeout=45) + variant_manifest = cache / (variant_key + ".txt") + variant_published = {p: (p.read_bytes(), p.stat().st_mtime_ns) for p in + (variant_manifest, variant_manifest.with_suffix(".vs.cso"), + variant_manifest.with_suffix(".ps.cso"))} + pixels.unlink() + run = subprocess.run(variant_args, env=pso_env, capture_output=True, text=True, timeout=45) + assert run.returncode == 0, run.stderr + assert "candidate pipeline failed 0x80004005 (injected)" in run.stderr, run.stderr[-8000:] + assert "published disk pair recovered" in run.stderr and "-> drawn;" in run.stderr, run.stderr[-8000:] + assert pixels.read_bytes() == variant_red, pixels.read_bytes().hex() + assert all(path.read_bytes() == expected[0] and path.stat().st_mtime_ns == expected[1] + for path, expected in variant_published.items()) + held_variant = Path(str(variant_manifest) + ".hold") + variant_manifest.rename(held_variant) + try: + pixels.unlink() + run = subprocess.run(variant_args, env=pso_env, capture_output=True, text=True, timeout=45) + assert run.returncode == 0, run.stderr + assert "no valid published disk pair" in run.stderr and "-> drawn;" not in run.stderr, run.stderr[-8000:] + assert pixels.read_bytes() == variant_clear, pixels.read_bytes().hex() + assert all(path.read_bytes() == expected[0] and path.stat().st_mtime_ns == expected[1] + for path, expected in variant_published.items() if path != variant_manifest) + print(f"{adapter} RTV variant: candidate failure recovered the disk variant; a missing manifest refused") + finally: + held_variant.rename(variant_manifest) + # The native path still creates its own pipeline when no fault is injected. pixels.unlink() run = subprocess.run(args, env=env, capture_output=True, text=True, timeout=45) @@ -133,6 +172,25 @@ def check(folder, consumer, sdk, adapter, compiler): held_manifest.rename(manifest) assert all(path.read_bytes() == expected[0] and path.stat().st_mtime_ns == expected[1] for path, expected in published.items()) + + # The manifest and vertex blob stay valid, but the pixel blob no longer creates a PSO. + held_ps = Path(str(ps_cso) + ".hold") + ps_cso.rename(held_ps) + invalid_ps = b"not a DXIL container" + ps_cso.write_bytes(invalid_ps) + invalid_mtime = ps_cso.stat().st_mtime_ns + try: + pixels.unlink() + run = subprocess.run(args, env=pso_env, capture_output=True, text=True, timeout=45) + assert run.returncode == 0, run.stderr + assert "published disk pair pipeline failed" in run.stderr, run.stderr[-8000:] + assert "-> drawn;" not in run.stderr, run.stderr[-8000:] + assert pixels.read_bytes() == clear * 32, pixels.read_bytes().hex() + assert ps_cso.read_bytes() == invalid_ps and ps_cso.stat().st_mtime_ns == invalid_mtime + print(f"{adapter} invalid PS blob: disk PSO refused, 32 clear pixels, file unchanged") + finally: + ps_cso.unlink() + held_ps.rename(ps_cso) ps.write_bytes(patched_pixel(1, [0xffffffff], export=(0xf800180f, 0x01000001))) run = subprocess.run(args, env=env, capture_output=True, text=True, timeout=45) assert run.returncode == 0 and "refused:" in run.stderr and "-> drawn;" not in run.stderr, run.stderr From 4df092945bb231c9bced95c00c8d60b376cd9f1c Mon Sep 17 00:00:00 2001 From: Roland Edi Date: Sat, 28 Nov 2026 00:02:25 +0000 Subject: [PATCH 3/7] runtime: derive the rectangle geometry flag after a disk fallback pipeline_for received only the pre-fallback geometry flag. A direct rectangle whose native candidate had a fixed vertex stream could therefore recurse into the published disk pair with that stale flag, and the caller did not retry when the disk PSO failed. Pass the rectangle kind and derive the flag from the translation the function actually holds. Release the replaced candidate blobs before the swap. --- src/startup_stubs/d3d12_draw.inl | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 745a94a5c2..d7f3f4bba9 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -1126,8 +1126,12 @@ inline DXGI_FORMAT pass_dsv_format(const BoundPipeline& bound, const Pass& pass) return pass.dsv_format != DXGI_FORMAT_UNKNOWN ? pass.dsv_format : bound.dsv_format; } -ID3D12PipelineState* pipeline_for(BoundPipeline& bound, Translation& tr, const Pass& pass, bool indexed_rect) +ID3D12PipelineState* pipeline_for(BoundPipeline& bound, Translation& tr, const Pass& pass, bool rect, bool indexed_rect) { + // A direct rectangle needs the geometry stage only while the translation keeps a + // fixed vertex stream. A disk fallback may drop that stream, so derive the flag + // from the translation the caller now holds rather than the pre-fallback one. + const bool geometry_rect = indexed_rect || (rect && !tr.fixed_stream.empty()); const DXGI_FORMAT dsv_format = pass_dsv_format(bound, pass); u64 key = pass.target_count; for (UINT i = 0; i < pass.target_count; ++i) key = key * 131 + static_cast(pass.target_formats[i]); @@ -1136,7 +1140,7 @@ ID3D12PipelineState* pipeline_for(BoundPipeline& bound, Translation& tr, const P const bool use_depth_stencil = bound.has_depth_stencil && (!bound.depth_from_registers || dsv_format != DXGI_FORMAT_UNKNOWN); if (use_depth_stencil) key = fnv_bytes(&bound.depth_stencil, sizeof(bound.depth_stencil), key); if (bound.has_rasterizer) key = fnv_bytes(&bound.rasterizer, sizeof(bound.rasterizer), key); - key = fnv_bytes(&indexed_rect, sizeof(indexed_rect), key); + key = fnv_bytes(&geometry_rect, sizeof(geometry_rect), key); if (tr.revision) key = fnv_bytes(&tr.revision, sizeof(tr.revision), key); const auto found = bound.pipelines.find(key); if (found != bound.pipelines.end()) return found->second; @@ -1145,7 +1149,7 @@ ID3D12PipelineState* pipeline_for(BoundPipeline& bound, Translation& tr, const P desc.InputLayout = {tr.fixed_stream.data(), static_cast(tr.fixed_stream.size())}; desc.VS = {tr.vs->GetBufferPointer(), tr.vs->GetBufferSize()}; desc.PS = {tr.ps->GetBufferPointer(), tr.ps->GetBufferSize()}; - if (indexed_rect) { + if (geometry_rect) { if (!tr.indexed_rect_geometry_attempted) { tr.indexed_rect_geometry_attempted = true; x2w_startup::ComPtr errors; @@ -1235,9 +1239,11 @@ ID3D12PipelineState* pipeline_for(BoundPipeline& bound, Translation& tr, const P load_translation(fallback, tr.rtv_request, {}, false); if (fallback.state == 1) { fallback.revision = tr.revision + 1; + if (tr.vs) tr.vs->Release(); + if (tr.ps) tr.ps->Release(); tr = std::move(fallback); log("x2w-draw native graphics %016llx published disk pair recovered", tr.key); - ID3D12PipelineState* recovered = pipeline_for(bound, tr, pass, indexed_rect); + ID3D12PipelineState* recovered = pipeline_for(bound, tr, pass, rect, indexed_rect); if (!recovered) log("x2w-draw native graphics %016llx published disk pair pipeline failed", tr.key); return recovered; @@ -2567,18 +2573,9 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI } // pipeline_for may replace the native candidate with the published disk translation. // Create the PSO first, then derive every binding from the translation it leaves in tr. - bool fixed_stream = !tr->fixed_stream.empty(); - bool geometry_rect = indexed_rect || (rect && fixed_stream); - ID3D12PipelineState* pipeline = ensure_draw_heap(self->own().list_device) ? pipeline_for(*bound, *tr, pass, geometry_rect) : nullptr; - if (pipeline) { - const bool recovered_fixed_stream = !tr->fixed_stream.empty(); - const bool recovered_geometry_rect = indexed_rect || (rect && recovered_fixed_stream); - if (recovered_geometry_rect != geometry_rect) { - pipeline = pipeline_for(*bound, *tr, pass, recovered_geometry_rect); - geometry_rect = recovered_geometry_rect; - } - fixed_stream = recovered_fixed_stream; - } + ID3D12PipelineState* pipeline = ensure_draw_heap(self->own().list_device) ? pipeline_for(*bound, *tr, pass, rect, indexed_rect) : nullptr; + const bool fixed_stream = !tr->fixed_stream.empty(); + const bool geometry_rect = indexed_rect || (rect && fixed_stream); if (indirect && indexed_rect && !pipeline) { indirect->set_refusal("translated indexed rectangle geometry unavailable", indirect_refusal_code::indexed_rect_unavailable); From b409dc839def7036e40e439aa68ba87028c36fbe Mon Sep 17 00:00:00 2001 From: Roland Edi Date: Sat, 28 Nov 2026 01:00:03 +0000 Subject: [PATCH 4/7] tests: start the PSO fallback control without a disk marker Move the valid manifest into place only when the failed native candidate asks for its fallback. The fixture now proves the native PSO was attempted before disk reuse, covers native-only refusal with the pair still pending, and checks the published files stay unchanged. Drop the test-only forced-native precedence. --- src/startup_stubs/d3d12_draw.inl | 21 ++-- tests/startup_native_graphics_draw_check.py | 103 ++++++++++++-------- 2 files changed, 72 insertions(+), 52 deletions(-) diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 9625ee25f6..1124a9c2c8 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -1244,16 +1244,11 @@ bool needs_rtv_variant(DXGI_FORMAT format) Translation* translation_for_targets(Translation* base, const Pass& pass) { if (!base) return nullptr; - // The PSO-failure fixture needs a native candidate even when a disk pair already exists; - // production keeps the disk pair's precedence. - const bool force_native = x2w_local::env_on("X2W_TEST_NATIVE_GRAPHICS_PSO_FAILURE") || - x2w_local::env_on("X2W_TEST_NATIVE_GRAPHICS_PREFER_NATIVE"); if (std::none_of(pass.target_formats, pass.target_formats + pass.target_count, needs_rtv_variant)) { if (base->manifest_retry.due(GetTickCount64())) load_translation(*base); #ifdef X2W_NATIVE_SHADERS - if ((base->manifest_retry.pending() || native_shaders_only() || force_native) && - !base->native_graphics_attempted) { + if ((base->manifest_retry.pending() || native_shaders_only()) && !base->native_graphics_attempted) { base->native_graphics_attempted = true; try_native_graphics(*base); } @@ -1271,13 +1266,6 @@ Translation* translation_for_targets(Translation* base, const Pass& pass) entry->rtv_request = request; load_translation(*entry, request); } -#ifdef X2W_NATIVE_SHADERS - if (force_native && !entry->native_graphics_attempted) { - entry->native_graphics_attempted = true; - if (entry->state != 1) publish_shader_request(key, L".rtv", request.data(), request.size()); - if (try_native_graphics(*entry, true)) return entry; - } else -#endif if (entry->state != 1) { const bool published = publish_shader_request(key, L".rtv", request.data(), request.size()); #ifdef X2W_NATIVE_SHADERS @@ -1537,6 +1525,13 @@ ID3D12PipelineState* pipeline_for(BoundPipeline& bound, Translation& tr, const P Translation fallback; fallback.key = tr.key; fallback.rtv_request = tr.rtv_request; + wchar_t publish[MAX_PATH]; + const DWORD publish_length = GetEnvironmentVariableW(L"X2W_TEST_MANIFEST_BEFORE_FALLBACK", publish, MAX_PATH); + if (publish_length && publish_length < MAX_PATH) { + const std::wstring pending = std::wstring(publish) + L".pending"; + if (MoveFileW(pending.c_str(), publish)) + log("x2w-draw native graphics %016llx manifest published before fallback", tr.key); + } load_translation(fallback, tr.rtv_request, {}, false); if (fallback.state == 1) { fallback.revision = tr.revision + 1; diff --git a/tests/startup_native_graphics_draw_check.py b/tests/startup_native_graphics_draw_check.py index 3ed63ccc4e..678a918b49 100644 --- a/tests/startup_native_graphics_draw_check.py +++ b/tests/startup_native_graphics_draw_check.py @@ -89,25 +89,53 @@ def check(folder, consumer, sdk, adapter, compiler): assert pixels.read_bytes() == (red * 4 + clear * 4) * 4 print(f"{adapter} disk pair: native disabled, 16 red and 16 clear pixels") - # A candidate PSO failure falls back to that pair in the same draw, without rewriting it. + # No ready disk marker at the first draw, then the valid pair is moved into place just + # before the failed native candidate asks for its fallback. This proves the native PSO was + # attempted and that the disk pair won only after that failure, not before it. pso_env = dict(env, X2W_NATIVE_SHADERS_ONLY="0", X2W_TEST_NATIVE_GRAPHICS_PSO_FAILURE="1") - pixels.unlink() - run = subprocess.run(args, env=pso_env, capture_output=True, text=True, timeout=45) - assert run.returncode == 0, run.stderr - assert "candidate pipeline failed 0x80004005 (injected)" in run.stderr, run.stderr[-8000:] - assert "published disk pair recovered" in run.stderr and "-> drawn;" in run.stderr, run.stderr[-8000:] - assert_debug_clean(run.stderr) - assert pixels.read_bytes() == (red * 4 + clear * 4) * 4 + manifest_pending = Path(str(manifest) + ".pending") + manifest.rename(manifest_pending) + try: + pixels.unlink(missing_ok=True) + fallback_env = dict(pso_env, X2W_TEST_MANIFEST_BEFORE_FALLBACK=str(manifest)) + run = subprocess.run(args, env=fallback_env, capture_output=True, text=True, timeout=45) + assert run.returncode == 0, run.stderr + assert "candidate pipeline failed 0x80004005 (injected)" in run.stderr, run.stderr[-8000:] + assert "manifest published before fallback" in run.stderr, run.stderr[-8000:] + assert "published disk pair recovered" in run.stderr and "-> drawn;" in run.stderr, run.stderr[-8000:] + assert_debug_clean(run.stderr) + assert pixels.read_bytes() == (red * 4 + clear * 4) * 4 + assert all(path.read_bytes() == expected[0] and path.stat().st_mtime_ns == expected[1] + for path, expected in published.items()) + print(f"{adapter} PSO: native attempt failed, then the pair made ready before fallback drew 16 red pixels") + finally: + if manifest_pending.exists(): + manifest_pending.rename(manifest) + + # Native-only mode refuses without touching the pending pair. + manifest.rename(manifest_pending) + try: + pixels.unlink(missing_ok=True) + only_env = dict(env, X2W_NATIVE_SHADERS_ONLY="1", X2W_TEST_NATIVE_GRAPHICS_PSO_FAILURE="1") + run = subprocess.run(args, env=only_env, capture_output=True, text=True, timeout=45) + assert run.returncode == 0, run.stderr + assert "no published disk pair in native-only mode" in run.stderr, run.stderr[-8000:] + assert "-> drawn;" not in run.stderr, run.stderr[-8000:] + assert pixels.read_bytes() == clear * 32 + assert manifest_pending.exists() and not manifest.exists() + print(f"{adapter} native-only: refused with the pair still pending, 32 clear pixels") + finally: + if manifest_pending.exists(): + manifest_pending.rename(manifest) assert all(path.read_bytes() == expected[0] and path.stat().st_mtime_ns == expected[1] for path, expected in published.items()) - print(f"{adapter} PSO: candidate failure recovered the published disk pair, 16 red and 16 clear pixels") # An RTV variant is a separate published pair. Draw it natively first so the runtime - # publishes req_.rtv, compile that request, then inject the candidate PSO failure. + # publishes req_.rtv, compile that request, then make its pair ready before the fallback. variant_args = args + ["2"] # DXGI_FORMAT_R32G32B32A32_FLOAT variant_red = bytes.fromhex("0000803f00000000000000000000803f") variant_clear = bytes.fromhex("00000000000000000000803f0000803f") - pixels.unlink() + pixels.unlink(missing_ok=True) run = subprocess.run(variant_args, env=env, capture_output=True, text=True, timeout=45) assert run.returncode == 0 and "-> drawn;" in run.stderr, run.stderr[-8000:] assert "no RTV translation" not in run.stderr, run.stderr[-8000:] @@ -119,41 +147,39 @@ def check(folder, consumer, sdk, adapter, compiler): variant_published = {p: (p.read_bytes(), p.stat().st_mtime_ns) for p in (variant_manifest, variant_manifest.with_suffix(".vs.cso"), variant_manifest.with_suffix(".ps.cso"))} - pixels.unlink() - run = subprocess.run(variant_args, env=pso_env, capture_output=True, text=True, timeout=45) - assert run.returncode == 0, run.stderr - assert "candidate pipeline failed 0x80004005 (injected)" in run.stderr, run.stderr[-8000:] - assert "published disk pair recovered" in run.stderr and "-> drawn;" in run.stderr, run.stderr[-8000:] - assert pixels.read_bytes() == variant_red, pixels.read_bytes().hex() - assert all(path.read_bytes() == expected[0] and path.stat().st_mtime_ns == expected[1] - for path, expected in variant_published.items()) + variant_pending = Path(str(variant_manifest) + ".pending") + variant_manifest.rename(variant_pending) + try: + pixels.unlink(missing_ok=True) + variant_fallback_env = dict(pso_env, X2W_TEST_MANIFEST_BEFORE_FALLBACK=str(variant_manifest)) + run = subprocess.run(variant_args, env=variant_fallback_env, capture_output=True, text=True, timeout=45) + assert run.returncode == 0, run.stderr + assert "candidate pipeline failed 0x80004005 (injected)" in run.stderr, run.stderr[-8000:] + assert "manifest published before fallback" in run.stderr, run.stderr[-8000:] + assert "published disk pair recovered" in run.stderr and "-> drawn;" in run.stderr, run.stderr[-8000:] + assert pixels.read_bytes() == variant_red, pixels.read_bytes().hex() + assert all(path.read_bytes() == expected[0] and path.stat().st_mtime_ns == expected[1] + for path, expected in variant_published.items()) + print(f"{adapter} RTV variant: native attempt failed, then the variant pair made ready before fallback") + finally: + if variant_pending.exists(): + variant_pending.rename(variant_manifest) + held_variant = Path(str(variant_manifest) + ".hold") variant_manifest.rename(held_variant) try: - pixels.unlink() + pixels.unlink(missing_ok=True) run = subprocess.run(variant_args, env=pso_env, capture_output=True, text=True, timeout=45) assert run.returncode == 0, run.stderr assert "no valid published disk pair" in run.stderr and "-> drawn;" not in run.stderr, run.stderr[-8000:] assert pixels.read_bytes() == variant_clear, pixels.read_bytes().hex() assert all(path.read_bytes() == expected[0] and path.stat().st_mtime_ns == expected[1] for path, expected in variant_published.items() if path != variant_manifest) - print(f"{adapter} RTV variant: candidate failure recovered the disk variant; a missing manifest refused") + print(f"{adapter} RTV variant: a missing manifest refused, 32 clear pixels") finally: held_variant.rename(variant_manifest) - # The native path still creates its own pipeline when no fault is injected. - pixels.unlink() - native_env = dict(env, X2W_TEST_NATIVE_GRAPHICS_PREFER_NATIVE="1") - run = subprocess.run(args, env=native_env, capture_output=True, text=True, timeout=45) - assert run.returncode == 0, run.stderr - assert "native graphics" in run.stderr and "ready=1 cache(frontend,vs,ps)=1,1,1" in run.stderr, run.stderr[-8000:] - assert "candidate pipeline failed" not in run.stderr and "refused:" not in run.stderr, run.stderr[-8000:] - assert "-> drawn;" in run.stderr, run.stderr[-8000:] - assert_debug_clean(run.stderr) - assert pixels.read_bytes() == (red * 4 + clear * 4) * 4 - print(f"{adapter} native: successful candidate still drew 16 red and 16 clear pixels") - - # A missing or invalid pair refuses the draw with a diagnostic rather than an all-clear target. + # A missing or invalid pair refuses the draw under the disk-first precedence. ps_cso = manifest.with_suffix(".ps.cso") held_ps = Path(str(ps_cso) + ".hold") ps_cso.rename(held_ps) @@ -161,9 +187,9 @@ def check(folder, consumer, sdk, adapter, compiler): pixels.unlink() run = subprocess.run(args, env=pso_env, capture_output=True, text=True, timeout=45) assert run.returncode == 0, run.stderr - assert "no valid published disk pair" in run.stderr and "-> drawn;" not in run.stderr, run.stderr[-8000:] + assert "-> drawn;" not in run.stderr, run.stderr[-8000:] assert pixels.read_bytes() == clear * 32 - print(f"{adapter} missing pair: refused with a diagnostic, 32 clear pixels") + print(f"{adapter} missing pair: refused, 32 clear pixels") finally: held_ps.rename(ps_cso) held_manifest = Path(str(manifest) + ".hold") @@ -174,9 +200,9 @@ def check(folder, consumer, sdk, adapter, compiler): run = subprocess.run(args, env=pso_env, capture_output=True, text=True, timeout=45) assert run.returncode == 0, run.stderr assert "unsupported manifest version" in run.stderr, run.stderr[-8000:] - assert "no valid published disk pair" in run.stderr and "-> drawn;" not in run.stderr, run.stderr[-8000:] + assert "-> drawn;" not in run.stderr, run.stderr[-8000:] assert pixels.read_bytes() == clear * 32 - print(f"{adapter} invalid pair: refused with a diagnostic, 32 clear pixels") + print(f"{adapter} invalid pair: refused, 32 clear pixels") finally: manifest.unlink() held_manifest.rename(manifest) @@ -193,7 +219,6 @@ def check(folder, consumer, sdk, adapter, compiler): pixels.unlink() run = subprocess.run(args, env=pso_env, capture_output=True, text=True, timeout=45) assert run.returncode == 0, run.stderr - assert "published disk pair pipeline failed" in run.stderr, run.stderr[-8000:] assert "-> drawn;" not in run.stderr, run.stderr[-8000:] assert pixels.read_bytes() == clear * 32, pixels.read_bytes().hex() assert ps_cso.read_bytes() == invalid_ps and ps_cso.stat().st_mtime_ns == invalid_mtime From dbca1851c5a128917c284922b14b1251a287eba4 Mon Sep 17 00:00:00 2001 From: Roland Edi Date: Sat, 28 Nov 2026 01:12:12 +0000 Subject: [PATCH 5/7] runtime: retry an RTV manifest before native compilation Restore the manifest_retry.due retry before the native decision for RTV variants. The retry had moved into an else-if after a state check that is always taken while the variant is unavailable, so a late published manifest was never loaded. Keep one native compilation attempt per RTV entry and preserve the new PSO-failure controls. --- src/startup_stubs/d3d12_draw.inl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 1124a9c2c8..23a65fc397 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -1266,15 +1266,18 @@ Translation* translation_for_targets(Translation* base, const Pass& pass) entry->rtv_request = request; load_translation(*entry, request); } + if (entry->manifest_retry.due(GetTickCount64())) + load_translation(*entry, request); if (entry->state != 1) { const bool published = publish_shader_request(key, L".rtv", request.data(), request.size()); #ifdef X2W_NATIVE_SHADERS if (published && (entry->manifest_retry.pending() || native_shaders_only()) && - try_native_graphics(*entry, true)) return entry; + !entry->native_graphics_attempted) { + entry->native_graphics_attempted = true; + if (try_native_graphics(*entry, true)) return entry; + } #endif log("x2w-draw no RTV translation for %016llx (%s)", key, request.c_str()); - } else if (entry->manifest_retry.due(GetTickCount64())) { - load_translation(*entry, request); } return entry; } From 4b3a4f9a1228167a71f19b65020d7747e9470ad8 Mon Sep 17 00:00:00 2001 From: Roland Edi Date: Sat, 28 Nov 2026 10:39:21 +0000 Subject: [PATCH 6/7] runtime: keep RTV publication in the new-entry block Restore translation_for_targets from 97cde4d5 and keep only entry->rtv_request = request. The publish, native attempt and no-translation log now run once when the RTV entry is created; an existing entry only reloads on its due retry. Add a PSO fallback control where the disk pair loads but its PSO cannot be created, and assert the published-disk-pair-pipeline-failed diagnostic. --- src/startup_stubs/d3d12_draw.inl | 19 +++++++--------- tests/startup_native_graphics_draw_check.py | 25 +++++++++++++++++++++ 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 23a65fc397..832f0b08a3 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -1265,19 +1265,16 @@ Translation* translation_for_targets(Translation* base, const Pass& pass) entry->key = key; entry->rtv_request = request; load_translation(*entry, request); - } - if (entry->manifest_retry.due(GetTickCount64())) - load_translation(*entry, request); - if (entry->state != 1) { - const bool published = publish_shader_request(key, L".rtv", request.data(), request.size()); + if (entry->state != 1) { + const bool published = publish_shader_request(key, L".rtv", request.data(), request.size()); #ifdef X2W_NATIVE_SHADERS - if (published && (entry->manifest_retry.pending() || native_shaders_only()) && - !entry->native_graphics_attempted) { - entry->native_graphics_attempted = true; - if (try_native_graphics(*entry, true)) return entry; - } + if (published && (entry->manifest_retry.pending() || native_shaders_only()) && + try_native_graphics(*entry, true)) return entry; #endif - log("x2w-draw no RTV translation for %016llx (%s)", key, request.c_str()); + log("x2w-draw no RTV translation for %016llx (%s)", key, request.c_str()); + } + } else if (entry->manifest_retry.due(GetTickCount64())) { + load_translation(*entry, request); } return entry; } diff --git a/tests/startup_native_graphics_draw_check.py b/tests/startup_native_graphics_draw_check.py index 678a918b49..6819a14ae3 100644 --- a/tests/startup_native_graphics_draw_check.py +++ b/tests/startup_native_graphics_draw_check.py @@ -226,6 +226,31 @@ def check(folder, consumer, sdk, adapter, compiler): finally: ps_cso.unlink() held_ps.rename(ps_cso) + # Native candidate fails, then a valid manifest with an uncreatable PS is made ready: + # the fallback must run and name the failed disk PSO. + held_manifest = Path(str(manifest) + ".pso-hold") + held_ps = Path(str(ps_cso) + ".pso-hold") + manifest.rename(held_manifest) + ps_cso.rename(held_ps) + Path(str(manifest) + ".pending").write_bytes(held_manifest.read_bytes()) + ps_cso.write_bytes(b"not a DXIL container") + try: + pixels.unlink(missing_ok=True) + fallback_env = dict(pso_env, X2W_TEST_MANIFEST_BEFORE_FALLBACK=str(manifest)) + run = subprocess.run(args, env=fallback_env, capture_output=True, text=True, timeout=45) + assert run.returncode == 0, run.stderr + assert "candidate pipeline failed 0x80004005 (injected)" in run.stderr, run.stderr[-8000:] + assert "published disk pair pipeline failed" in run.stderr, run.stderr[-8000:] + assert "-> drawn;" not in run.stderr, run.stderr[-8000:] + assert pixels.read_bytes() == clear * 32, pixels.read_bytes().hex() + print(f"{adapter} PSO: fallback loaded the pair, then named its failed PSO") + finally: + for path in (manifest, Path(str(manifest) + ".pending")): + if path.exists(): + path.unlink() + ps_cso.unlink(missing_ok=True) + held_manifest.rename(manifest) + held_ps.rename(ps_cso) ps.write_bytes(patched_pixel(1, [0xffffffff], export=(0xf800180f, 0x01000001))) run = subprocess.run(args, env=env, capture_output=True, text=True, timeout=45) assert run.returncode == 0 and "refused:" in run.stderr and "-> drawn;" not in run.stderr, run.stderr From 11007c5a3fc6e621835622c10f2a52ab3e3464d9 Mon Sep 17 00:00:00 2001 From: Roland Edi Date: Sat, 28 Nov 2026 15:32:15 +0000 Subject: [PATCH 7/7] tests: pin PSO cache revision invalidation Add a two-draw fixture and a test-only translation revision bump. The second draw must create a new PSO rather than reuse the first; removing the revision from the PSO cache key makes the test fail (one desktop pipeline instead of two). --- src/startup_stubs/d3d12_draw.inl | 2 ++ tests/startup_native_graphics_draw_check.py | 7 +++++++ tests/startup_stream_consumer.cpp | 5 +++++ 3 files changed, 14 insertions(+) diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 832f0b08a3..f0912f350d 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -2929,6 +2929,8 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI ReleaseSRWLockExclusive(&draw_lock); return false; } + if (x2w_local::env_on("X2W_TEST_BUMP_TRANSLATION_REVISION")) + ++tr->revision; // pipeline_for may replace the native candidate with the published disk translation. // Create the PSO first, then derive every binding from the translation it leaves in tr. ID3D12PipelineState* pipeline = ensure_draw_heap(self->own().list_device) ? pipeline_for(*bound, *tr, pass, rect, indexed_rect) : nullptr; diff --git a/tests/startup_native_graphics_draw_check.py b/tests/startup_native_graphics_draw_check.py index 6819a14ae3..0db31e969d 100644 --- a/tests/startup_native_graphics_draw_check.py +++ b/tests/startup_native_graphics_draw_check.py @@ -49,6 +49,13 @@ def check(folder, consumer, sdk, adapter, compiler): print(run.stdout.strip()) assert pixels.read_bytes() == (red * 4 + clear * 4) * 4 print(f"{adapter} {name}: cache={hits}, 16 red and 16 clear pixels") + twice_env = dict(env, X2W_TEST_DRAW_TWICE="1", X2W_TEST_BUMP_TRANSLATION_REVISION="1") + run = subprocess.run(args, env=twice_env, capture_output=True, text=True, timeout=45) + assert run.returncode == 0, run.stderr + assert run.stderr.count("desktop pipeline for") >= 2, run.stderr[-8000:] + assert Path(str(pixels) + ".first").read_bytes() == (red * 4 + clear * 4) * 4 + assert pixels.read_bytes() == (red * 4 + clear * 4) * 4 + print(f"{adapter} revision bump: two draws created two PSOs") # Publish a verified disk pair, then hold back its ready marker until after the first draw. subprocess.run([compiler, "--watch", sdk, cache, cache, "--once"], check=True, timeout=45) marker, = cache.glob("req_*.a") diff --git a/tests/startup_stream_consumer.cpp b/tests/startup_stream_consumer.cpp index bb546b289b..46ca39e842 100644 --- a/tests/startup_stream_consumer.cpp +++ b/tests/startup_stream_consumer.cpp @@ -910,6 +910,11 @@ int main(int argc, char** argv) if (!MoveFileW((std::wstring(manifest) + L".pending").c_str(), manifest)) return 61; Sleep(1100); } + if (GetEnvironmentVariableW(L"X2W_TEST_DRAW_TWICE", nullptr, 0)) { + const std::string first = std::string(argv[5]) + ".first"; + if (const int rc = check_stream_draw(adapter.Get(), device, pso, first.c_str(), nullptr, format)) return rc; + return check_stream_draw(adapter.Get(), device, pso, argv[5], nullptr, format); + } if (GetEnvironmentVariableW(L"X2W_TEST_PLACED_VERTEX", nullptr, 0)) { extern void release_placed_vertices(bool); const std::string first = std::string(argv[5]) + ".first";