Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
134 changes: 85 additions & 49 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<long> live_pipeline_state_count{0}; // live_pipeline_states()
Expand Down Expand Up @@ -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<rt::d3d12_xs::PsoStreamRecord> 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<const std::uint8_t> bytes{static_cast<const std::uint8_t*>(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<const std::uint8_t> bytes{static_cast<const std::uint8_t*>(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<const rt::d3d12_xs::PsoStreamRecord::ShaderData> containers,
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 9 additions & 5 deletions runtime/shims/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<PsoStreamRecord> 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<Fn>(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<Fn>(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';
Expand All @@ -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<IUnknown*>(*out), *desc);
if (!prepared && SUCCEEDED(hr) && out && *out && desc) keep_pso_stream(static_cast<IUnknown*>(*out), *desc);
return hr;
}

Expand Down
28 changes: 27 additions & 1 deletion runtime/test/d3d12_mesh_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "../shims/d3d12_xs_pso_stream.h"
#include "../src/shader_library.h"

#include <string>
#include <string_view>
#include <array>
#include <cstring>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -217,6 +219,19 @@ int main(int argc, char** argv) {
stream.add(TargetsSubobject{.inner = one_target()});
Ref<ID3D12PipelineState> 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<D3D12_PIPELINE_STATE_SUBOBJECT_TYPE(24), D3D12_SHADER_BYTECODE>{.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<Create>(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};
Expand Down Expand Up @@ -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<Create>(slot(device.p, 86))(device.p, &old_desc, IID_PPV_ARGS(&refused.p)) ==
E_INVALIDARG &&
!refused.p);
}

// Streams the PC runtime refuses.
Expand Down Expand Up @@ -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<Create>(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<D3D12_PIPELINE_STATE_SUBOBJECT_TYPE(24), D3D12_SHADER_BYTECODE>{.inner = ms});
Expand Down
Loading
Loading