From 4045610f0f87e321eda15d4a479c043e7e156057 Mon Sep 17 00:00:00 2001 From: IlyassRS Date: Wed, 25 Nov 2026 12:56:27 +0100 Subject: [PATCH] d3d12: handle console stream numbering under on-demand pipeline compile Under LEONIDA_PIPELINE_COMPILE=on-demand (set by --first-run-ui), device slot 86 routes to Device::prepare_console_stream instead of eager ID3D12Device2::CreatePipelineState. prepare_console_stream previously parsed only with PC subobject numbering and called PsoStreamRecord::capture(source) without the console flag, rejecting console-numbered mesh streams (type 24 = MS, type 26 = RASTERIZER1) and leaving captured tags unnormalized for slot 55 derivation and bind-time resolve_type. Extract parse_pipeline_stream_any shared by CreatePipelineState and prepare_console_stream, pass used_console to PsoStreamRecord::capture, reject unsupported stages and missing VK_EXT_mesh_shader / mesh root_window up front in prepare_console_stream, run LEONIDA_PSO_STREAM_AUDIT consistently across both paths in create_pso_from_stream, make d3d12_mesh_test's temp folder per-process, and register d3d12_mesh_on_demand in CTest. Item: RT-ONDEMAND-CONSOLE-STREAM-ENUM --- runtime/CMakeLists.txt | 3 + runtime/shims/d3d12_vulkan_device.cpp | 134 ++++++++++++++++---------- runtime/shims/d3d12_xs_device.cpp | 14 ++- runtime/test/d3d12_mesh_test.cpp | 28 +++++- runtime/test/pso_stream_test.cpp | 48 +++++++++ 5 files changed, 172 insertions(+), 55 deletions(-) diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index e9cb8d4345..2b87f0ee27 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -3976,6 +3976,9 @@ if(TARGET Vulkan::Headers) target_compile_options(d3d12_mesh_test PRIVATE /W4 /permissive- /utf-8) add_test(NAME d3d12_mesh COMMAND d3d12_mesh_test) set_tests_properties(d3d12_mesh PROPERTIES SKIP_RETURN_CODE 77) + add_test(NAME d3d12_mesh_on_demand COMMAND d3d12_mesh_test) + set_tests_properties(d3d12_mesh_on_demand PROPERTIES SKIP_RETURN_CODE 77 + ENVIRONMENT "LEONIDA_PIPELINE_COMPILE=on-demand") else() message(STATUS "glslangValidator not found: d3d12_compute_test, d3d12_draw_test, d3d12_set0_compute_test and d3d12_mesh_test are not built") endif() diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 49743d0c1d..dfafd935db 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -7998,6 +7998,34 @@ HRESULT parse_pipeline_stream(const D3D12_PIPELINE_STATE_STREAM_DESC& desc, Pipe return S_OK; } +// A stream the PC reading refuses, or leaves with an AS and no MS (no valid PC stream), is read again with the +// console's numbering (parse_pipeline_stream), and that reading is taken when it holds a mesh shader (its 24) and no +// AS. A stream the PC reading accepts as valid is never read differently. LEONIDA_XS_STREAM_ENUM=pc keeps the PC +// reading alone (such PSOs are then refused, as before). +HRESULT parse_pipeline_stream_any(const D3D12_PIPELINE_STATE_STREAM_DESC& desc, PipelineStream& stream, + bool* used_console = nullptr) { + if (used_console) *used_console = false; + d3d12_trace::stream_refusal = nullptr; + d3d12_xs::used_console_stream = false; + HRESULT parsed = parse_pipeline_stream(desc, stream); + if ((FAILED(parsed) || (stream.as.pShaderBytecode && !stream.ms.pShaderBytecode)) && + console_stream_enum_enabled()) { + const char* pc_reason = d3d12_trace::stream_refusal; + PipelineStream console; + if (SUCCEEDED(parse_pipeline_stream(desc, console, true)) && !console.as.pShaderBytecode && + is_mesh_container(console.ms)) { + log_once("console pipeline stream: type 24 contains a mesh shader; DXIL kind", 13); + d3d12_xs::used_console_stream = true; + d3d12_trace::stream_refusal = nullptr; + stream = console; + if (used_console) *used_console = true; + return S_OK; + } + d3d12_trace::stream_refusal = pc_reason; + } + return parsed; +} + // A pipeline state, compute (create_compute) or graphics (create_graphics): each shader is found by the SHA-256 of // its FSSM program in the translator's index, and the FSSM blob is copied into the arena as the shader's code base. std::atomic live_pipeline_state_count{0}; // live_pipeline_states() @@ -11567,40 +11595,68 @@ class Device final // hold one compiled backend PSO while either public object lives. The device only holds weak references. HRESULT prepare_console_stream(const D3D12_PIPELINE_STATE_STREAM_DESC& source, REFIID iid, void** out, std::unique_ptr record) { - if (!out)return E_POINTER;*out=nullptr; + const auto finish = [](HRESULT hr) { + if (hr == E_NOTIMPL) + d3d12_trace::record("PipelineStream::Refused", d3d12_trace::stream_refusal ? + d3d12_trace::stream_refusal : "other backend state"); + return hr; + }; + if (!out) return E_POINTER; + *out = nullptr; PipelineStream stream{}; - if(const auto hr=parse_pipeline_stream(source,stream);FAILED(hr))return hr; - const auto* library=state_.library();if(!library)return E_FAIL; - const auto verify=[&](const char* name,const D3D12_SHADER_BYTECODE& code) { - if(!code.BytecodeLength)return true; - if(!code.pShaderBytecode)return false; - const std::span bytes{static_cast(code.pShaderBytecode),code.BytecodeLength}; - const std::string identity=std::string(name)+leonida::sha256_hex(bytes); - {std::lock_guard lock(verified_program_mutex_);if(verified_programs_.count(identity))return true;} - auto program=leonida::compute_program(bytes); - if(program.bytes.empty())return false; - bool ambiguous=false; - const auto candidates=library->program_candidates(name,program,&ambiguous); - if(candidates.empty() || ambiguous)return false; - {std::lock_guard lock(verified_program_mutex_);verified_programs_.insert(identity);} + bool used_console = false; + if (const auto hr = parse_pipeline_stream_any(source, stream, &used_console); FAILED(hr)) return finish(hr); + const auto* library = state_.library(); + if (!library) return E_FAIL; + const auto verify = [&](const char* name, const D3D12_SHADER_BYTECODE& code) { + if (!code.BytecodeLength) return true; + if (!code.pShaderBytecode) return false; + const std::span bytes{static_cast(code.pShaderBytecode), code.BytecodeLength}; + const std::string identity = std::string(name) + leonida::sha256_hex(bytes); + {std::lock_guard lock(verified_program_mutex_); if (verified_programs_.count(identity)) return true;} + auto program = leonida::compute_program(bytes); + if (program.bytes.empty()) return false; + bool ambiguous = false; + const auto candidates = library->program_candidates(name, program, &ambiguous); + if (candidates.empty() || ambiguous) return false; + if (std::strcmp(name, "ms") == 0) { + bool has_root_window = false; + for (const auto* entry : candidates) { + if (entry->graphics_root_window) { has_root_window = true; break; } + } + if (!has_root_window) return false; + } + {std::lock_guard lock(verified_program_mutex_); verified_programs_.insert(identity);} return true; }; - if(stream.has_cs) { - if(!stream.compute.CS.BytecodeLength || !verify("cs",stream.compute.CS))return E_INVALIDARG; + if (stream.has_cs) { + if (!stream.compute.CS.BytecodeLength || !verify("cs", stream.compute.CS)) return E_INVALIDARG; } else { - const auto& g=stream.graphics; - if(stream.as.BytecodeLength || g.HS.BytecodeLength || g.DS.BytecodeLength)return E_NOTIMPL; - if((bool(g.VS.BytecodeLength)==bool(stream.ms.BytecodeLength)) || g.NumRenderTargets>8 || - !verify("vs",g.VS)||!verify("gs",g.GS)||!verify("ms",stream.ms)||!verify("ps",g.PS))return E_INVALIDARG; - } - if(!record) record=rt::d3d12_xs::PsoStreamRecord::capture(source); - if(!record)return E_INVALIDARG; - auto* pipeline=new PipelineState(state_); + const auto& g = stream.graphics; + if (stream.as.BytecodeLength) + return finish(stream_unsupported("amplification shaders are not supported; stream subobject", + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_AS)); + if (g.HS.BytecodeLength || g.DS.BytecodeLength || g.StreamOutput.NumEntries) + return finish(stream_unsupported("graphics pipeline stage not supported yet (hull/domain/stream output):", 0)); + const bool mesh = stream.ms.pShaderBytecode && stream.ms.BytecodeLength; + if (mesh && (!state_.info.mesh_shader || !state_.context->vk.vkCmdDrawMeshTasksEXT)) + return finish(stream_unsupported( + "mesh-shader PSOs need VK_EXT_mesh_shader, which the device has not enabled; stage", 1)); + if (mesh && g.GS.BytecodeLength) return E_INVALIDARG; + if ((bool(g.VS.BytecodeLength) == bool(stream.ms.BytecodeLength)) || g.NumRenderTargets > 8 || + !verify("vs", g.VS) || !verify("gs", g.GS) || !verify("ms", stream.ms) || !verify("ps", g.PS)) + return E_INVALIDARG; + } + if (!record) record = rt::d3d12_xs::PsoStreamRecord::capture(source, used_console); + if (!record) return E_INVALIDARG; + auto* pipeline = new PipelineState(state_); pipeline->attach_device(identity()); - pipeline->set_cache_key(stream.has_cs?pipeline_key(stream.compute):pipeline_key(stream.graphics)); - if(!rt::d3d12_xs::attach_pso_record(pipeline,std::move(record))){pipeline->Release();return E_OUTOFMEMORY;} + pipeline->set_cache_key(stream.has_cs ? pipeline_key(stream.compute) : pipeline_key(stream.graphics)); + if (!rt::d3d12_xs::attach_pso_record(pipeline, std::move(record))) { pipeline->Release(); return E_OUTOFMEMORY; } pipeline->set_pending_stream(stream.has_cs); - return hand_out(pipeline,iid,out); + const HRESULT result = hand_out(pipeline, iid, out); + if (SUCCEEDED(result) && stream.ms.pShaderBytecode) d3d12_trace::record("PipelineStream::Mesh", "accepted"); + return result; } HRESULT shared_native_graphics(const D3D12_GRAPHICS_PIPELINE_STATE_DESC& pc, D3D12_SHADER_BYTECODE mesh, std::span containers, @@ -13655,27 +13711,7 @@ class Device final *out = nullptr; if (!desc) return finish(E_INVALIDARG); PipelineStream stream; - HRESULT parsed = parse_pipeline_stream(*desc, stream); - // A stream the PC reading refuses, or leaves with an AS and no MS (no valid PC stream), is read again with the - // console's numbering (parse_pipeline_stream), and that reading is taken when it holds a mesh shader (its 24) - // and no AS. A stream the PC reading accepts as valid is never read differently. LEONIDA_XS_STREAM_ENUM=pc - // keeps the PC reading alone (such PSOs are then refused, as before). - if ((FAILED(parsed) || (stream.as.pShaderBytecode && !stream.ms.pShaderBytecode)) && - console_stream_enum_enabled()) { - const char* pc_reason = d3d12_trace::stream_refusal; - PipelineStream console; - if (SUCCEEDED(parse_pipeline_stream(*desc, console, true)) && !console.as.pShaderBytecode && - is_mesh_container(console.ms)) { - log_once("console pipeline stream: type 24 contains a mesh shader; DXIL kind", 13); - d3d12_xs::used_console_stream = true; - d3d12_trace::stream_refusal = nullptr; - stream = console; - parsed = S_OK; - } else { - d3d12_trace::stream_refusal = pc_reason; - } - } - if (FAILED(parsed)) return finish(parsed); + if (const HRESULT parsed = parse_pipeline_stream_any(*desc, stream); FAILED(parsed)) return finish(parsed); auto* pipeline = new PipelineState(state_); pipeline->attach_device(identity()); const HRESULT hr = stream.has_cs ? pipeline->create_compute(stream.compute) diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index 90dcc61136..1bbff353ae 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -5657,12 +5657,16 @@ void keep_pso_stream(IUnknown* pso, const D3D12_PIPELINE_STATE_STREAM_DESC& desc HRESULT create_pso_from_stream(DeviceProxy* self, const D3D12_PIPELINE_STATE_STREAM_DESC* desc, const IID* riid, void** out, std::unique_ptr derived = {}) { if (!riid) return E_POINTER; - if (self->prepare_stream && desc && out && IsEqualIID(*riid, __uuidof(ID3D12PipelineState))) - return self->prepare_stream(self->real, *desc, *riid, out, std::move(derived)); - using Fn = HRESULT(WINAPI*)(ID3D12Device*, const D3D12_PIPELINE_STATE_STREAM_DESC*, const IID*, void**); used_console_stream = false; d3d12_trace::stream_refusal = nullptr; - const HRESULT hr = reinterpret_cast(self->real_vtable[47])(self->real, desc, riid, out); + const bool prepared = self->prepare_stream && desc && out && IsEqualIID(*riid, __uuidof(ID3D12PipelineState)); + HRESULT hr; + if (prepared) { + hr = self->prepare_stream(self->real, *desc, *riid, out, std::move(derived)); + } else { + using Fn = HRESULT(WINAPI*)(ID3D12Device*, const D3D12_PIPELINE_STATE_STREAM_DESC*, const IID*, void**); + hr = reinterpret_cast(self->real_vtable[47])(self->real, desc, riid, out); + } static const bool audit = [] { char value[2]{}; return GetEnvironmentVariableA("LEONIDA_PSO_STREAM_AUDIT", value, sizeof(value)) == 1 && value[0] == '1'; @@ -5672,7 +5676,7 @@ HRESULT create_pso_from_stream(DeviceProxy* self, const D3D12_PIPELINE_STATE_STR audit_pso_stream(*desc, hr, used_console_stream); } catch (const std::bad_alloc&) {} } - if (SUCCEEDED(hr) && out && *out && desc) keep_pso_stream(static_cast(*out), *desc); + if (!prepared && SUCCEEDED(hr) && out && *out && desc) keep_pso_stream(static_cast(*out), *desc); return hr; } diff --git a/runtime/test/d3d12_mesh_test.cpp b/runtime/test/d3d12_mesh_test.cpp index 2d9bc5212e..c42ac37660 100644 --- a/runtime/test/d3d12_mesh_test.cpp +++ b/runtime/test/d3d12_mesh_test.cpp @@ -23,6 +23,7 @@ #include "../shims/d3d12_xs_pso_stream.h" #include "../src/shader_library.h" +#include #include #include #include @@ -124,7 +125,8 @@ int main(int argc, char** argv) { ms_container[36 + 8 + 2] = 13; // DXIL mesh shader kind ms_old_container[36 + 8 + 2] = 13; const auto vs_container = shader_container(vs_program); - const auto library = std::filesystem::temp_directory_path() / "leonida_mesh_test"; + const auto library = + std::filesystem::temp_directory_path() / ("leonida_mesh_test_" + std::to_string(GetCurrentProcessId())); std::filesystem::create_directories(library); std::filesystem::copy_file(LEONIDA_TEST_MS_SPV, library / "ms_draw.spv", std::filesystem::copy_options::overwrite_existing); std::filesystem::copy_file(LEONIDA_TEST_MS_SPV, library / "ms_old.spv", std::filesystem::copy_options::overwrite_existing); @@ -217,6 +219,19 @@ int main(int argc, char** argv) { stream.add(TargetsSubobject{.inner = one_target()}); Ref pso; EXPECT(create(device.p, stream, pso) == E_NOTIMPL && !pso.p); + // Slot 86 on a console-numbered mesh stream must also refuse with E_NOTIMPL without VK_EXT_mesh_shader + // (including under LEONIDA_PIPELINE_COMPILE=on-demand). + { + Stream console_stream; + console_stream.add(RootSubobject{.inner = root.p}); + console_stream.add(Subobject{.inner = ms}); + console_stream.add(PsSubobject{.inner = ps}).add(TargetsSubobject{.inner = one_target()}); + using Create = HRESULT(WINAPI*)(ID3D12Device*, const D3D12_PIPELINE_STATE_STREAM_DESC*, REFIID, void**); + const auto console_desc = console_stream.desc(); + EXPECT(reinterpret_cast(slot(device.p, 86))(device.p, &console_desc, IID_PPV_ARGS(&pso.p)) == + E_NOTIMPL && + !pso.p); + } // A DISPATCH_MESH command signature is refused the same way. const D3D12_INDIRECT_ARGUMENT_DESC mesh_arg{D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH_MESH}; const D3D12_COMMAND_SIGNATURE_DESC mesh_sig{12, 1, &mesh_arg, 0}; @@ -259,6 +274,11 @@ int main(int argc, char** argv) { old_stream.add(RootSubobject{.inner = root.p}).add(MsSubobject{.inner = ms_old}).add(PsSubobject{.inner = ps}); old_stream.add(TargetsSubobject{.inner = one_target()}); EXPECT(create(device.p, old_stream, refused) == E_INVALIDARG && !refused.p); + using Create = HRESULT(WINAPI*)(ID3D12Device*, const D3D12_PIPELINE_STATE_STREAM_DESC*, REFIID, void**); + const auto old_desc = old_stream.desc(); + EXPECT(reinterpret_cast(slot(device.p, 86))(device.p, &old_desc, IID_PPV_ARGS(&refused.p)) == + E_INVALIDARG && + !refused.p); } // Streams the PC runtime refuses. @@ -360,6 +380,12 @@ int main(int argc, char** argv) { .add(AsSubobject{.inner = {as_container.data(), as_container.size()}}) .add(PsSubobject{.inner = ps}).add(TargetsSubobject{.inner = one_target()}); EXPECT(FAILED(create(device.p, as_only, refused)) && !refused.p); + { + using Create = HRESULT(WINAPI*)(ID3D12Device*, const D3D12_PIPELINE_STATE_STREAM_DESC*, REFIID, void**); + const auto as_desc = as_only.desc(); + EXPECT(FAILED(reinterpret_cast(slot(device.p, 86))(device.p, &as_desc, IID_PPV_ARGS(&refused.p))) && + !refused.p); + } // An MS and an AS in PC numbering stay the PC's (and refused, the layer builds no AS). bad = mesh_stream; bad.add(Subobject{.inner = ms}); diff --git a/runtime/test/pso_stream_test.cpp b/runtime/test/pso_stream_test.cpp index 07ac13a326..5bff6c9610 100644 --- a/runtime/test/pso_stream_test.cpp +++ b/runtime/test/pso_stream_test.cpp @@ -44,6 +44,7 @@ std::size_t payload_of_type(const std::vector& s, unsigned wanted if (type == D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_BLEND) { size = sizeof(D3D12_BLEND_DESC); align = 4; } else if (type == D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_SAMPLE_MASK) { size = 4; align = 4; } else if (type == D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RASTERIZER) { size = sizeof(D3D12_RASTERIZER_DESC); align = 4; } + else if (type == D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RASTERIZER1) { size = sizeof(D3D12_RASTERIZER_DESC1); align = 4; } else if (type == D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL1) { size = sizeof(D3D12_DEPTH_STENCIL_DESC1); align = 4; } else if (type == D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_ROOT_SIGNATURE) { size = 8; align = 8; } else if (type == D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_INPUT_LAYOUT) { size = sizeof(D3D12_INPUT_LAYOUT_DESC); align = 8; } @@ -254,6 +255,52 @@ void test_refusals() { "a stream with an unmodelled subobject type is not captured"); } +void test_console_stream_normalization() { + // Console numbering has no gap at 23: MS is 24 (PC AS) and RASTERIZER1 is 26 (PC DEPTH_STENCIL2). + // Placing console tag 26 last ensures PC walking fails on size (DEPTH_STENCIL2 > RASTERIZER1), while console + // capture succeeds, rewrites tags to canonical PC numbering (25 = MS, 27 = RASTERIZER1), deep-copies the mesh + // bytecode, and allows subsequent PC-tag slot-55 derivation. + StreamBuilder s; + ID3D12RootSignature* root = nullptr; + s.entry(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_ROOT_SIGNATURE, &root, 8, 8); + D3D12_SHADER_BYTECODE ms{kVsCode, sizeof(kVsCode)}; + s.entry(24u, &ms, sizeof(ms), 8); + UINT mask = 0xffffffffu; + s.entry(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_SAMPLE_MASK, &mask, 4, 4); + D3D12_RASTERIZER_DESC1 raster1{}; + raster1.FillMode = D3D12_FILL_MODE_SOLID; + raster1.CullMode = D3D12_CULL_MODE_NONE; + raster1.DepthClipEnable = TRUE; + s.entry(26u, &raster1, sizeof(raster1), 4); + + check(rt::d3d12_xs::PsoStreamRecord::capture({s.bytes.size(), s.bytes.data()}, false) == nullptr, + "console tag 26 (RASTERIZER1) is rejected when walked as PC DEPTH_STENCIL2"); + auto rec = rt::d3d12_xs::PsoStreamRecord::capture({s.bytes.size(), s.bytes.data()}, true); + check(rec != nullptr, "console-numbered mesh + RASTERIZER1 stream captures with console=true"); + if (!rec) return; + + const std::size_t ms_off = payload_of_type(rec->bytes(), D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_MS); + const std::size_t r1_off = payload_of_type(rec->bytes(), D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RASTERIZER1); + check(ms_off != static_cast(-1), "console tag 24 is normalized to PC MS (25)"); + check(r1_off != static_cast(-1), "console tag 26 is normalized to PC RASTERIZER1 (27)"); + if (ms_off != static_cast(-1)) { + D3D12_SHADER_BYTECODE ms_copy{}; + std::memcpy(&ms_copy, rec->bytes().data() + ms_off, sizeof(ms_copy)); + check(ms_copy.pShaderBytecode != kVsCode && ms_copy.BytecodeLength == sizeof(kVsCode) && + std::memcmp(ms_copy.pShaderBytecode, kVsCode, sizeof(kVsCode)) == 0, + "normalized console MS bytecode is deep-copied"); + } + + UINT patched_mask = 0x5u; + auto r3 = record(3, &patched_mask, sizeof(patched_mask)); + std::vector derived; + check(rt::d3d12_xs::derive_stream(*rec, r3.data(), 1, derived) == rt::d3d12_xs::DeriveStatus::ok, + "slot 55 derives from a normalized console stream"); + const std::size_t mp = payload_of_type(derived, D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_SAMPLE_MASK); + check(mp != static_cast(-1) && std::memcmp(derived.data() + mp, &patched_mask, 4) == 0, + "derived console stream updates SAMPLE_MASK"); +} + } // namespace int main() { @@ -263,6 +310,7 @@ int main() { test_derive_single_and_all_tags(); test_stencil_mask_record(); test_refusals(); + test_console_stream_normalization(); std::printf("%s: %d failure(s)\n", g_failures ? "FAILED" : "OK", g_failures); return g_failures ? 1 : 0; }