diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 1933d5f78..a4a214dd4 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -182,6 +182,9 @@ struct Translation { std::unordered_map pipelines; // by target formats std::vector pso_recipes; // opt-in X2W_PSO_RECIPE_DIR records for this key 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 }; @@ -447,7 +450,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()) { @@ -458,7 +461,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)) @@ -580,8 +583,8 @@ void load_translation(Translation& tr, const std::string& rtv_request = {}, std: } tr.state = 1; load_pso_recipes(tr); - 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 views, %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); } @@ -614,6 +617,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; candidate.manifest_retry = tr.manifest_retry; candidate.native_graphics_attempted = true; if (!pair.input_layout.empty()) { @@ -631,6 +635,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); @@ -1258,6 +1263,7 @@ Translation* translation_for_targets(Translation* base, const Pass& pass) if (!entry) { entry = new Translation; entry->key = key; + entry->rtv_request = request; load_translation(*entry, request); if (entry->state != 1) { const bool published = publish_shader_request(key, L".rtv", request.data(), request.size()); @@ -1454,30 +1460,36 @@ bool replay_pso_recipes(BoundPipeline& bound, Translation& tr, const Pass& pass, return precreated || mismatched || failed; } -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); // A state decoded from registers needs a depth view to act on; with none bound the pass keeps the desktop default. const bool use_depth_stencil = bound.has_depth_stencil && (!bound.depth_from_registers || dsv_format != DXGI_FORMAT_UNKNOWN); - const u64 key = x2w_startup::pso_state_key(pass.target_count, pass.target_formats, dsv_format, use_depth_stencil, - bound.depth_stencil, bound.has_rasterizer, bound.rasterizer, indexed_rect); - const auto found = bound.pipelines.find(key); + const u64 state_key = x2w_startup::pso_state_key(pass.target_count, pass.target_formats, dsv_format, use_depth_stencil, + bound.depth_stencil, bound.has_rasterizer, bound.rasterizer, geometry_rect); + u64 cache_key = state_key; + if (tr.revision) cache_key = x2w_startup::pso_recipe_hash(&tr.revision, sizeof(tr.revision), cache_key); + const auto found = bound.pipelines.find(cache_key); if (found != bound.pipelines.end()) return found->second; D3D12_BLEND_DESC blend{}; if (!effective_blend_state(bound, tr, pass.target_count, blend)) { log("x2w-draw export write masks incompatible with shared logic op for %016llx", tr.key); - bound.pipelines[key] = nullptr; + bound.pipelines[cache_key] = nullptr; return nullptr; } const u64 targets_key = x2w_startup::pso_targets_key(pass.target_count, pass.target_formats, dsv_format); - if (!tr.pso_recipes.empty() && bound.pso_recipe_targets_tried.insert(targets_key).second) { + if (!tr.revision && !tr.pso_recipes.empty() && bound.pso_recipe_targets_tried.insert(targets_key).second) { const bool attempted = replay_pso_recipes(bound, tr, pass, dsv_format, use_depth_stencil, blend); - const auto replayed = bound.pipelines.find(key); + const auto replayed = bound.pipelines.find(cache_key); if (replayed != bound.pipelines.end()) return replayed->second; if (attempted) InterlockedIncrement(&pso_recipe_fallbacks); } D3D12_GRAPHICS_PIPELINE_STATE_DESC desc{}; - if (!prepare_translated_pso_desc(tr, indexed_rect, desc)) return nullptr; + if (!prepare_translated_pso_desc(tr, geometry_rect, desc)) return nullptr; // Blend state as found in the pipeline's state block, else opaque. Depth-stencil state and // DSVFormat come from the stream PSO (or bound DSV), else disabled. Raster state comes from // the stream RASTERIZER / RASTERIZER1 subobject, else solid fill, no culling, depth clip on. @@ -1492,17 +1504,57 @@ 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); - bound.pipelines[key] = pipeline; - if (pipeline) { + 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; + 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; + 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, rect, 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[cache_key] = pipeline; + if (pipeline && !tr.revision) { wchar_t path[MAX_PATH]; if (pso_recipe_path(path, tr.key)) { const x2w_startup::PsoRecipe recipe = x2w_startup::make_pso_recipe( - tr.key, key, indexed_rect, use_depth_stencil, bound.has_rasterizer, desc); + tr.key, state_key, geometry_rect, use_depth_stencil, bound.has_rasterizer, desc); if (!append_pso_recipe(path, recipe)) { static LONG logged; if (first_calls(logged, 16)) log("x2w-draw pso recipe for %016llx was not written (directory, size limit or I/O)", tr.key); @@ -2890,20 +2942,40 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI return true; } 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; + } + 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; + 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); + } + if (!pipeline || (tr->samplers.heap && !prepare_draw_sampler_heap(self, 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))) { @@ -2966,19 +3038,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(self, 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; - } const bool raw_uav = std::any_of(tr->uavs.begin(), tr->uavs.end(), [](const PixelUav& uav) { return uav.raw; }); // ponytail: prove direct draws with one writable buffer first. SRVs and indexed // draws can alias it through guest addresses, so do not guess their independence. diff --git a/tests/startup_native_graphics_draw_check.py b/tests/startup_native_graphics_draw_check.py index b62230cca..5ca7dc965 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)) @@ -43,18 +49,28 @@ 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") key = marker.stem.removeprefix("req_") manifest = cache / f"{key}.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"))} published_draw = subprocess.run(args, env=env, capture_output=True, text=True, timeout=45) assert published_draw.returncode == 0 and "-> drawn;" in published_draw.stderr, published_draw.stderr assert "native graphics " not in published_draw.stderr, published_draw.stderr assert pixels.read_bytes() == (red * 4 + clear * 4) * 4 - assert all(path.read_bytes() == expected for path, expected in published.items()) + changed = [(path.name, path.read_bytes(), expected[0]) for path, expected in published.items() + if path.read_bytes() != expected[0]] + assert not changed, [(name, len(got), len(expected), got[:48].hex(), expected[:48].hex()) + for name, got, expected in changed] print(f"{adapter} existing disk pair takes precedence with native enabled") faults = ( ("missing DXC", {"X2W_NATIVE_SHADERS": str(folder / "missing-sdk")}, f"native graphics {key} refused:"), @@ -71,8 +87,182 @@ 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") + + # 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") + 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()) + + # An RTV variant is a separate published pair. Draw it natively first so the runtime + # 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(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:] + 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"))} + 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(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: a missing manifest refused, 32 clear pixels") + finally: + held_variant.rename(variant_manifest) + + # 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) + try: + pixels.unlink() + run = subprocess.run(args, env=pso_env, capture_output=True, text=True, timeout=45) + assert run.returncode == 0, run.stderr + assert "-> drawn;" not in run.stderr, run.stderr[-8000:] + assert pixels.read_bytes() == clear * 32 + print(f"{adapter} missing pair: refused, 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 "-> drawn;" not in run.stderr, run.stderr[-8000:] + assert pixels.read_bytes() == clear * 32 + print(f"{adapter} invalid pair: refused, 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()) + + # 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 "-> 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) + # 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 diff --git a/tests/startup_stream_consumer.cpp b/tests/startup_stream_consumer.cpp index 092b6e91e..024c4063f 100644 --- a/tests/startup_stream_consumer.cpp +++ b/tests/startup_stream_consumer.cpp @@ -1095,6 +1095,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";