Skip to content
Merged
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
10 changes: 9 additions & 1 deletion src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7313,9 +7313,14 @@ HRESULT __stdcall device_create_pipeline_placeholder(Wrapper*, const void* desc,
}
}
note_pipeline(static_cast<Wrapper*>(*out), N, nullptr, desc, 0x300);
if constexpr (N == 57) {
#ifdef X2W_NATIVE_SHADERS
if constexpr (N == 57) if (render_mode()) prepare_native_mesh57(static_cast<Wrapper*>(*out));
if (render_mode())
prepare_native_mesh57(static_cast<Wrapper*>(*out));
else
#endif
note_mesh57_outcome(Mesh57Outcome::disabled);
}
return S_OK;
}

Expand Down Expand Up @@ -7848,6 +7853,9 @@ HRESULT __stdcall queue_present(Wrapper* self, UINT planes, const void* plane_pa
compute_dispatches_translated, compute_dispatches_refused, compute_dispatches_untranslated, compute_dispatches_unmodelled,
compute_builder_gate_refusals, compute_inputs_gpu_copied, compute_inputs_snapshot, compute_inputs_snapshot_kib);
log_compute_census();
char mesh57_line[1536];
mesh57_outcome_line(mesh57_line);
log("%s", mesh57_line);
}
if (render_mode() && frame % 300 == 0) {
// Slot-61 calls since the start, by outcome, then by signature kind (zero kinds left out).
Expand Down
101 changes: 77 additions & 24 deletions src/startup_stubs/native_mesh57.inl
Original file line number Diff line number Diff line change
Expand Up @@ -218,51 +218,99 @@ extern "C" __declspec(dllexport) void x2w_d3d12_mesh57_profile_stats(LONG* looku
if (opens) *opens = mesh57_profile_opens;
}

enum class Mesh57Outcome {
disabled, ps_absent, unsupported_stage_or_descriptor, missing_creation_root_or_state,
profile_absent, profile_open_failed, profile_header, profile_pixel_entry, profile_version,
profile_constants, profile_root_mapping, profile_view_count, profile_view_entries,
profile_view_mapping, profile_heap_mapping, profile_input_length, profile_input_content,
profile_trailing_bytes, profile_attachment_state, profile_creation_root, profile_exception,
lowering_reached, lowering_refused, pipeline_refused, ready, count
};

volatile LONG64 mesh57_outcomes[static_cast<int>(Mesh57Outcome::count)];

void note_mesh57_outcome(Mesh57Outcome outcome)
{
InterlockedIncrement64(&mesh57_outcomes[static_cast<int>(outcome)]);
}

void mesh57_outcome_line(char (&line)[1536])
{
static constexpr const char* names[]{
"disabled", "ps_absent", "unsupported_stage_or_descriptor", "missing_creation_root_or_state",
"profile_absent", "profile_open_failed", "profile_rejected_header", "profile_rejected_pixel_entry",
"profile_rejected_version", "profile_rejected_constants", "profile_rejected_root_mapping",
"profile_rejected_view_count", "profile_rejected_view_entries", "profile_rejected_view_mapping",
"profile_rejected_heap_mapping", "profile_rejected_input_length", "profile_rejected_input_content",
"profile_rejected_trailing_bytes", "profile_rejected_attachment_state", "profile_rejected_creation_root",
"profile_rejected_exception", "lowering_reached", "lowering_refused", "pipeline_refused", "ready"
};
static_assert(std::size(names) == std::size(mesh57_outcomes));
constexpr char prefix[] = "x2w-render native slot57 preparation:";
std::memcpy(line, prefix, sizeof(prefix));
int used = sizeof(prefix) - 1;
for (int i = 0; i < static_cast<int>(Mesh57Outcome::count); ++i) {
const auto count = InterlockedCompareExchange64(&mesh57_outcomes[i], 0, 0);
const int remaining = static_cast<int>(sizeof(line)) - used;
const int n = std::snprintf(line + used, remaining, " %s %lld", names[i], static_cast<long long>(count));
if (n < 0 || n >= remaining)
return;
used += n;
}
}

#ifdef X2W_NATIVE_SHADERS
void prepare_native_mesh57(Wrapper* pipeline)
{
wchar_t sdk[MAX_PATH], path[MAX_PATH];
const DWORD sdk_length = GetEnvironmentVariableW(L"X2W_NATIVE_SHADERS", sdk, MAX_PATH);
if (!sdk_length || sdk_length >= MAX_PATH)
return;
return note_mesh57_outcome(Mesh57Outcome::disabled);
const auto* info = pipeline_info(pipeline);
if (!info || !info->fixed_ms || !info->fixed_ps || info->desc.size() != 0x300)
return;
if (!info || !info->fixed_ms || info->desc.size() != 0x300)
return note_mesh57_outcome(Mesh57Outcome::unsupported_stage_or_descriptor);
if (!info->fixed_ps)
return note_mesh57_outcome(Mesh57Outcome::ps_absent);
const auto& ms_bytes = *info->fixed_ms;
const auto& ps_bytes = *info->fixed_ps;
u64 fields[0x300 / sizeof(u64)];
std::memcpy(fields, info->desc.data(), sizeof(fields));
auto* root = as_wrapper(reinterpret_cast<const void*>(fields[0]));
if (!root || root->own().kind != root_signature_kind || !root->own().real || !root_info(root) ||
fields[2] != info->state_a.size() || fields[4] != info->state_b.size())
return;
return note_mesh57_outcome(Mesh57Outcome::missing_creation_root_or_state);
std::vector<unsigned char> root_blob;
{
x2w_startup::ExclusiveNoteLock guard(note_lock);
const auto it = root_blobs().find(root);
if (it == root_blobs().end())
return;
return note_mesh57_outcome(Mesh57Outcome::missing_creation_root_or_state);
root_blob = it->second;
}
constexpr char revision[] = "x2w-startup-mesh-57-3";
const u64 key = fnv_part(root_blob, fnv_part(ps_bytes, fnv_part(ms_bytes, fnv_bytes(revision, sizeof(revision) - 1))));
const auto normalized = x2w_startup::normalize_mesh57_descriptor(info->desc);
const auto variant = fnv_part(info->state_b, fnv_part(info->state_a, fnv_part(normalized, key)));
if (!cache_path(path, L"", variant, L".mesh57-state"))
return;
return note_mesh57_outcome(Mesh57Outcome::profile_absent);
InterlockedIncrement(&mesh57_profile_lookups);
// Profiles are placed before the process starts and most creations have none: skip the open when the
// cache listing has no such name. A profile written while the process runs is read by the next one.
if (cache_lookup(L"", variant, L".mesh57-state") == x2w_startup::CacheLookup::absent)
return;
return note_mesh57_outcome(Mesh57Outcome::profile_absent);
InterlockedIncrement(&mesh57_profile_opens);
std::ifstream file(std::filesystem::path(path), std::ios::binary);
if (!file)
return;
return note_mesh57_outcome(Mesh57Outcome::profile_open_failed);
auto refusal = Mesh57Outcome::profile_exception;
const auto profile_error = [&](Mesh57Outcome reason, const auto& message) {
refusal = reason;
return std::runtime_error(message);
};
try {
x2w_startup::Mesh57Profile profile{};
if (!file.read(reinterpret_cast<char*>(&profile), sizeof(profile)))
throw std::runtime_error("invalid profile header");
throw profile_error(Mesh57Outcome::profile_header, "invalid profile header");
const bool pixel_heap = !std::memcmp(profile.magic,"X2WM576",8);
const bool direct_heap = pixel_heap || !std::memcmp(profile.magic, "X2WM575", 8);
const bool buffers = direct_heap || !std::memcmp(profile.magic, "X2WM574", 8);
Expand All @@ -271,43 +319,42 @@ void prepare_native_mesh57(Wrapper* pipeline)
x2w_startup::Mesh57PixelEntry entry{};
if (varying_pixel) {
if (!file.read(reinterpret_cast<char*>(&entry), sizeof(entry)))
throw std::runtime_error("truncated fixed pixel entry");
throw profile_error(Mesh57Outcome::profile_pixel_entry, "truncated fixed pixel entry");
} else if (std::memcmp(profile.magic, "X2WM571", 8)) {
throw std::runtime_error("unknown fixed profile version");
throw profile_error(Mesh57Outcome::profile_version, "unknown fixed profile version");
}
x2w_startup::Mesh57ConstantEntry mapping{};
if (mapped) {
if (!file.read(reinterpret_cast<char*>(&mapping), sizeof(mapping)) || mapping.root_parameter >= 16 ||
!mapping.count || mapping.count > 8 || !root_heap_flags(root))
throw std::runtime_error("invalid MS constant mapping or guest root is not deferred");
throw profile_error(Mesh57Outcome::profile_constants, "invalid MS constant mapping or guest root is not deferred");
} else if (!root_info(root)->empty() || root_heap_flags(root)) {
throw std::runtime_error("fixed mesh profile lacks guest root mapping");
throw profile_error(Mesh57Outcome::profile_root_mapping, "fixed mesh profile lacks guest root mapping");
}
std::vector<x2w_startup::Mesh57ViewEntry> view_entries;
if (buffers) {
UINT count{};
if (!file.read(reinterpret_cast<char*>(&count), sizeof(count)) || !count || count > 15)
throw std::runtime_error("invalid MS view entry count");
throw profile_error(Mesh57Outcome::profile_view_count, "invalid MS view entry count");
view_entries.resize(count);
if (!file.read(reinterpret_cast<char*>(view_entries.data()), count * sizeof(view_entries[0])))
throw std::runtime_error("truncated MS view mappings");
throw profile_error(Mesh57Outcome::profile_view_entries, "truncated MS view mappings");
for (const auto& e : view_entries)
if (e.root_address > 1) throw std::runtime_error("invalid MS view mapping");
if (e.root_address > 1) throw profile_error(Mesh57Outcome::profile_view_mapping, "invalid MS view mapping");
}
UINT heap_sgpr{};
if (direct_heap && (!file.read(reinterpret_cast<char*>(&heap_sgpr), sizeof(heap_sgpr)) ||
!(root_heap_flags(root) & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED)))
throw std::runtime_error("missing MS resource heap mapping");
throw profile_error(Mesh57Outcome::profile_heap_mapping, "missing MS resource heap mapping");
const auto match = [&](UINT part, const char* name, const void* bytes, size_t size) {
if (size != profile.lengths[part])
throw std::runtime_error(std::string("profile input bytes differ: ") + name +
" length " + std::to_string(profile.lengths[part]) +
" != " + std::to_string(size));
throw profile_error(Mesh57Outcome::profile_input_length, std::string("profile input bytes differ: ") + name +
" length " + std::to_string(profile.lengths[part]) + " != " + std::to_string(size));
if (!size)
return;
std::string copy(size, 0);
if (!file.read(copy.data(), size) || std::memcmp(copy.data(), bytes, size))
throw std::runtime_error(std::string("profile input bytes differ: ") + name + " content");
throw profile_error(Mesh57Outcome::profile_input_content, std::string("profile input bytes differ: ") + name + " content");
};
match(0, "normalized descriptor", normalized.data(), normalized.size());
match(1, "state_a", info->state_a.data(), info->state_a.size());
Expand All @@ -316,13 +363,13 @@ void prepare_native_mesh57(Wrapper* pipeline)
match(4, "mesh shader", ms_bytes.data(), ms_bytes.size());
match(5, "pixel shader", ps_bytes.data(), ps_bytes.size());
if (file.peek() != EOF)
throw std::runtime_error("profile input bytes differ: trailing bytes after the pixel shader");
throw profile_error(Mesh57Outcome::profile_trailing_bytes, "profile input bytes differ: trailing bytes after the pixel shader");
if (!profile.targets.NumRenderTargets || profile.targets.NumRenderTargets > D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT ||
((profile.depth.DepthEnable || profile.depth.StencilEnable) && profile.depth_format == DXGI_FORMAT_UNKNOWN) ||
profile.samples.Count != 1 ||
profile.samples.Quality || profile.sample_mask != UINT_MAX ||
(profile.wave_size != 32 && profile.wave_size != 64))
throw std::runtime_error("unsupported fixed attachment or sample state");
throw profile_error(Mesh57Outcome::profile_attachment_state, "unsupported fixed attachment or sample state");
const std::string ms(ms_bytes.begin(), ms_bytes.end()), ps(ps_bytes.begin(), ps_bytes.end());
x2w::MeshStageLayout mesh_layout{profile.threads, profile.vertices, profile.primitives,
profile.topology, profile.group_stride, profile.wave_size == 32};
Expand All @@ -336,11 +383,14 @@ void prepare_native_mesh57(Wrapper* pipeline)
if (mapped) {
if (FAILED(D3D12CreateVersionedRootSignatureDeserializer(root_blob.data(), root_blob.size(), IID_PPV_ARGS(&deserializer))) ||
FAILED(deserializer->GetRootSignatureDescAtVersion(D3D_ROOT_SIGNATURE_VERSION_1_1, &guest_desc)))
throw std::runtime_error("invalid MS creation root signature");
throw profile_error(Mesh57Outcome::profile_creation_root, "invalid MS creation root signature");
mesh_layout.root_inputs.push_back({mapping.sgpr, mapping.root_parameter, mapping.count});
for (const auto& e : view_entries)
mesh_layout.root_inputs.push_back({e.sgpr, e.root_parameter, 0, e.root_address != 0});
}
// This milestone overlaps the terminal counts below.
note_mesh57_outcome(Mesh57Outcome::lowering_reached);
refusal = Mesh57Outcome::lowering_refused;
auto native = mapped ? x2w::translate_mesh_program(ms, mesh_layout, guest_desc->Desc_1_1)
: x2w::translate_mesh_program(ms, mesh_layout);
const bool mapped_constants = mapped && native.constants.size() == 1 &&
Expand Down Expand Up @@ -405,6 +455,7 @@ void prepare_native_mesh57(Wrapper* pipeline)
} else {
ps_ir = x2w::direct_gfx1013_constant_pixel(ps);
}
refusal = Mesh57Outcome::pipeline_refused;
auto compiler = x2w::ShaderDxilCompiler::runtime_worker(sdk);
const auto cache = std::filesystem::path(path).parent_path();
const auto ms_output = compiler.compile(native.dxil_ir, L"ms_6_6", cache, x2w::ShaderSourceFormat::dxil_ir);
Expand Down Expand Up @@ -461,8 +512,10 @@ void prepare_native_mesh57(Wrapper* pipeline)
info->mesh57_reads = std::move(reads);
info->mesh57_heap_word = heap_word;
}
note_mesh57_outcome(Mesh57Outcome::ready);
log("x2w-render native slot57 mesh %016llx ready", key);
} catch (const std::exception& e) {
note_mesh57_outcome(refusal);
log("x2w-render native slot57 mesh %016llx refused: %s", key, e.what());
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/cmake/native_mesh57_outcomes.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
if(TARGET compile_dxil AND TARGET startup_indirect_draw_indexed
AND EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${DXC_BIN_DIR}/dxil.dll")
foreach(backend IN ITEMS warp hardware)
add_test(NAME native_mesh57_outcomes_${backend} COMMAND startup_indirect_draw_indexed ${backend} mesh)
set_tests_properties(native_mesh57_outcomes_${backend} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77
ENVIRONMENT "X2W_TEST_NATIVE_MESH57=1;X2W_TEST_NATIVE_MESH57_OUTCOMES=1;X2W_NATIVE_SHADERS=${DXC_BIN_DIR}")
endforeach()
endif()
30 changes: 26 additions & 4 deletions tests/startup_indirect_draw_indexed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,7 @@ void refuse_each_reason(ID3D12Device* guest, ID3D12CommandSignature* draw_indexe
}

// 300 empty console presents (queue slot 32) reach the render-totals heartbeat; nothing is shown.
std::string indirect_line(ID3D12Device* guest, const std::wstring& log_path)
std::string render_totals_line(ID3D12Device* guest, const std::wstring& log_path, const char* prefix)
{
const D3D12_COMMAND_QUEUE_DESC desc{};
ComPtr<IUnknown> queue;
Expand All @@ -1509,7 +1509,7 @@ std::string indirect_line(ID3D12Device* guest, const std::wstring& log_path)
std::ifstream file(log_path, std::ios::binary);
std::string line, found;
while (std::getline(file, line))
if (line.rfind("x2w-render indirect:", 0) == 0) found = line;
if (line.rfind(prefix, 0) == 0) found = line;
while (!found.empty() && (found.back() == '\r' || found.back() == '\n')) found.pop_back();
return found;
}
Expand Down Expand Up @@ -2056,6 +2056,7 @@ bool fixed_mesh_create_contract(ID3D12Device* guest)
static int native_fixed_mesh_fixture(ID3D12Device* device, ID3D12Device* guest, ID3D12RootSignature* writer,
const std::wstring& log_path)
{
const bool outcomes = GetEnvironmentVariableA("X2W_TEST_NATIVE_MESH57_OUTCOMES", nullptr, 0) != 0;
const bool mrt = GetEnvironmentVariableA("X2W_TEST_NATIVE_MESH57_MRT", nullptr, 0) != 0;
const bool waterfall=GetEnvironmentVariableA("X2W_TEST_NATIVE_MESH57_WATERFALL",nullptr,0)!=0;
const bool pixel_heap=waterfall || GetEnvironmentVariableA("X2W_TEST_NATIVE_MESH57_PIXEL_HEAP",nullptr,0)!=0;
Expand Down Expand Up @@ -2270,11 +2271,16 @@ static int native_fixed_mesh_fixture(ID3D12Device* device, ID3D12Device* guest,
"create native fixed mesh");
return pso;
};
if (outcomes) {
fields[5] = fields[6] = 0;
create();
fields[5] = reinterpret_cast<UINT64>(ps.data());
fields[6] = ps_size;
}
const auto pso = create();
fields[15] = 1; // Same stages and root, unreviewed fixed pipeline state.
const auto mismatched = create();
{
// native_mesh57.inl: throw std::runtime_error(std::string("profile input bytes differ: ") + name + " content");
char expected[128];
std::snprintf(expected, sizeof(expected),
"x2w-render native slot57 mesh %016llx refused: profile input bytes differ: normalized descriptor content",
Expand All @@ -2292,6 +2298,22 @@ static int native_fixed_mesh_fixture(ID3D12Device* device, ID3D12Device* guest,
fields[15] = extra;
create();
}
if (outcomes) {
const char* prefix = "x2w-render native slot57 preparation:";
const auto line = render_totals_line(guest, log_path, prefix);
const auto expected = std::string(prefix) +
" disabled 0 ps_absent 1 unsupported_stage_or_descriptor 0 missing_creation_root_or_state 0"
" profile_absent 64 profile_open_failed 0 profile_rejected_header 0 profile_rejected_pixel_entry 0"
" profile_rejected_version 0 profile_rejected_constants 0 profile_rejected_root_mapping 0"
" profile_rejected_view_count 0 profile_rejected_view_entries 0 profile_rejected_view_mapping 0"
" profile_rejected_heap_mapping 0 profile_rejected_input_length 0 profile_rejected_input_content 1"
" profile_rejected_trailing_bytes 0 profile_rejected_attachment_state 0 profile_rejected_creation_root 0"
" profile_rejected_exception 0 lowering_reached 1 lowering_refused 0 pipeline_refused 0 ready 1";
if (line != expected) {
std::fprintf(stderr, "expected preparation totals:\n%s\nreceived:\n%s\n", expected.c_str(), line.c_str());
return 1;
}
}
fields[15] = 1;
LONG lookups = -1, opens = -1;
x2w_d3d12_mesh57_profile_stats(&lookups, &opens);
Expand Down Expand Up @@ -3211,7 +3233,7 @@ int main(int argc, char** argv) try
if (indexed) {
// Forwarded: the three unrefused runs. Other reasons: the short argument range, the invalid index range and two commands.
refuse_each_reason(guest.Get(), signature.Get());
const std::string line = indirect_line(guest.Get(), log_path);
const std::string line = render_totals_line(guest.Get(), log_path, "x2w-render indirect:");
DeleteFileW(log_path.c_str());
const std::string expected = std::string("x2w-render indirect: forwarded: drawindexed ") + (count_control ? "4" : "3") +
"; refused graphics-pipeline: drawindexed 1; compute-pipeline: dispatch 1; placeholder-signature: other 1; "
Expand Down
Loading