Skip to content
Open
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 @@ -4305,6 +4305,9 @@ if(TARGET Vulkan::Headers)
add_test(NAME d3d12_mesh_heavy_cache COMMAND d3d12_mesh_test)
set_tests_properties(d3d12_mesh_heavy_cache PROPERTIES SKIP_RETURN_CODE 77 ENVIRONMENT
"LEONIDA_HEAVY_PIPELINE_CACHE_MS=1;LEONIDA_PIPELINE_CACHE_DIR=${CMAKE_CURRENT_BINARY_DIR}/heavy_cache_test")
add_test(NAME d3d12_mesh_on_demand COMMAND d3d12_mesh_test)
set_tests_properties(d3d12_mesh_on_demand PROPERTIES ENVIRONMENT "LEONIDA_PIPELINE_COMPILE=on-demand"
SKIP_RETURN_CODE 77)
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
114 changes: 91 additions & 23 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15335,33 +15335,101 @@ 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;
if (!out) return E_POINTER;
*out = nullptr;
d3d12_trace::stream_refusal = nullptr;
d3d12_xs::used_console_stream = false;
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);}
return true;
bool used_console = false;
HRESULT parsed = parse_pipeline_stream(source, stream);
// As in ID3D12Device2::CreatePipelineState: a raw slot-86 stream the PC reading refuses, or leaves with an AS
// and no MS, is retried with the console's subobject numbering when it carries a DXIL mesh shader (kind 13).
if (!record && (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(source, 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;
used_console = true;
parsed = S_OK;
} else {
d3d12_trace::stream_refusal = pc_reason;
}
}
if (FAILED(parsed)) return parsed;
if (!stream.has_cs) {
const auto& g = stream.graphics;
if (stream.as.BytecodeLength)
return stream_unsupported("amplification shaders are not supported; stream subobject",
D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_AS);
if (stream.ms.BytecodeLength && !stream.ms.pShaderBytecode) return E_INVALIDARG;
const bool mesh = stream.ms.pShaderBytecode && stream.ms.BytecodeLength;
if (mesh && (g.VS.BytecodeLength || g.HS.BytecodeLength || g.DS.BytecodeLength || g.GS.BytecodeLength ||
g.StreamOutput.NumEntries))
return E_INVALIDARG;
if (!mesh && (!g.VS.pShaderBytecode || !g.VS.BytecodeLength)) return E_INVALIDARG;
if (mesh && (!state_.info.mesh_shader || !state_.context || !state_.context->vk.vkCmdDrawMeshTasksEXT))
return stream_unsupported(
"mesh-shader PSOs need VK_EXT_mesh_shader, which the device has not enabled; stage", 1);
if (g.HS.BytecodeLength || g.DS.BytecodeLength || g.StreamOutput.NumEntries)
return stream_unsupported("graphics pipeline stage not supported yet (hull/domain/stream output):", 0);
if (g.NumRenderTargets > D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT) return E_INVALIDARG;
}
const auto* library = state_.library();
if (!library) return E_FAIL;
const auto verify = [&](const char* name, const D3D12_SHADER_BYTECODE& code) -> HRESULT {
if (!code.BytecodeLength) return S_OK;
if (!code.pShaderBytecode) return E_INVALIDARG;
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 S_OK;
}
auto program = leonida::compute_program(bytes);
if (program.bytes.empty()) return E_INVALIDARG;
bool ambiguous = false;
const auto candidates = library->program_candidates(name, program, &ambiguous);
if (candidates.empty() || ambiguous) return E_INVALIDARG;
if (std::strcmp(name, "ms") == 0) {
const leonida::ShaderEntry* entry =
pick_entry(candidates, [](const leonida::ShaderEntry&) { return true; });
if (!entry) return E_INVALIDARG;
if (const std::string refusal = leonida::mesh_limit_refusal(*entry, state_.mesh_limits());
!refusal.empty()) {
std::fprintf(stderr, "[rt] d3d12: %s\n", refusal.c_str());
return stream_unsupported("mesh shader exceeds a device mesh limit");
}
if (!entry->graphics_root_window) {
std::fprintf(stderr,
"[rt] d3d12: mesh shader %s was translated before the graphics root window; translate "
"the shaders again (make_game.py --stages shaders)\n",
entry->name.c_str());
return E_INVALIDARG;
}
}
{
std::lock_guard lock(verified_program_mutex_);
verified_programs_.insert(identity);
}
return S_OK;
};
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) return E_INVALIDARG;
if (const HRESULT hr = verify("cs", stream.compute.CS); FAILED(hr)) return hr;
} 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;
const auto& g = stream.graphics;
for (const auto& [name, code] : {std::pair{"vs", g.VS}, std::pair{"gs", g.GS},
std::pair{"ms", stream.ms}, std::pair{"ps", g.PS}}) {
if (const HRESULT hr = verify(name, code); FAILED(hr)) return hr;
}
}
if(!record) record=rt::d3d12_xs::PsoStreamRecord::capture(source);
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());
Expand Down
19 changes: 13 additions & 6 deletions runtime/shims/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6110,16 +6110,23 @@ 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);
static const bool audit = [] {
char value[2]{};
return GetEnvironmentVariableA("LEONIDA_PSO_STREAM_AUDIT", value, sizeof(value)) == 1 && value[0] == '1';
}();
used_console_stream = false;
d3d12_trace::stream_refusal = nullptr;
if (self->prepare_stream && desc && out && IsEqualIID(*riid, __uuidof(ID3D12PipelineState))) {
const HRESULT hr = self->prepare_stream(self->real, *desc, *riid, out, std::move(derived));
if (audit && (hr == E_NOTIMPL || hr == E_FAIL)) {
try {
audit_pso_stream(*desc, hr, used_console_stream);
} catch (const std::bad_alloc&) {}
}
return hr;
}
using Fn = HRESULT(WINAPI*)(ID3D12Device*, const D3D12_PIPELINE_STATE_STREAM_DESC*, const IID*, void**);
const HRESULT hr = reinterpret_cast<Fn>(self->real_vtable[47])(self->real, desc, riid, out);
if (audit && desc && (hr == E_NOTIMPL || hr == E_FAIL)) {
try {
audit_pso_stream(*desc, hr, used_console_stream);
Expand Down
30 changes: 26 additions & 4 deletions runtime/test/d3d12_mesh_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// No game data. Set LEONIDA_VK_VALIDATION=1 to run under VK_LAYER_KHRONOS_validation.
#include "vk_test_device.h"
#include "d3d12_test_shaders.h"
#include "shader_fixture_path.h"
#include "../shims/d3d12_com.h"
#include "../shims/d3d12_xs_device.h"
#include "../shims/d3d12_xs_pso_stream.h"
Expand Down Expand Up @@ -127,10 +128,14 @@ int main(int argc, char** argv) {
const auto vs_container = shader_container(vs_program);
const auto library = std::filesystem::temp_directory_path() / "leonida_mesh_test";
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);
std::filesystem::copy_file(LEONIDA_TEST_PS_SPV, library / "ps_draw.spv", std::filesystem::copy_options::overwrite_existing);
std::filesystem::copy_file(LEONIDA_TEST_VS_FULLSCREEN_SPV, library / "vs_fullscreen.spv",
std::filesystem::copy_file(vktest::shader_fixture_path(LEONIDA_TEST_MS_SPV), library / "ms_draw.spv",
std::filesystem::copy_options::overwrite_existing);
std::filesystem::copy_file(vktest::shader_fixture_path(LEONIDA_TEST_MS_SPV), library / "ms_old.spv",
std::filesystem::copy_options::overwrite_existing);
std::filesystem::copy_file(vktest::shader_fixture_path(LEONIDA_TEST_PS_SPV), library / "ps_draw.spv",
std::filesystem::copy_options::overwrite_existing);
std::filesystem::copy_file(vktest::shader_fixture_path(LEONIDA_TEST_VS_FULLSCREEN_SPV),
library / "vs_fullscreen.spv",
std::filesystem::copy_options::overwrite_existing);
{
std::ofstream index(library / "index.json", std::ios::binary);
Expand Down Expand Up @@ -220,6 +225,18 @@ 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);
using CreateSlot86 = HRESULT(WINAPI*)(ID3D12Device*, const D3D12_PIPELINE_STATE_STREAM_DESC*, REFIID, void**);
const auto slot86_fn = reinterpret_cast<CreateSlot86>(slot(device.p, 86));
const auto pc_mesh_desc = stream.desc();
EXPECT(slot86_fn(device.p, &pc_mesh_desc, IID_PPV_ARGS(&pso.p)) == E_NOTIMPL && !pso.p);
if (!pc_only) {
Stream console_no_ext;
console_no_ext.add(RootSubobject{.inner = root.p});
console_no_ext.add(Subobject<D3D12_PIPELINE_STATE_SUBOBJECT_TYPE(24), D3D12_SHADER_BYTECODE>{.inner = ms});
console_no_ext.add(PsSubobject{.inner = ps}).add(TargetsSubobject{.inner = one_target()});
const auto console_no_ext_desc = console_no_ext.desc();
EXPECT(slot86_fn(device.p, &console_no_ext_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 @@ -290,6 +307,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 CreateSlot86 = HRESULT(WINAPI*)(ID3D12Device*, const D3D12_PIPELINE_STATE_STREAM_DESC*, REFIID, void**);
const auto old_desc = old_stream.desc();
EXPECT(reinterpret_cast<CreateSlot86>(slot(device.p, 86))(device.p, &old_desc, IID_PPV_ARGS(&refused.p)) ==
E_INVALIDARG &&
!refused.p);
}

// Streams the PC runtime refuses.
Expand Down