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
4 changes: 2 additions & 2 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"xmem_shim_test_controls", "startup_stub_xcrdapi", "startup_stub_xcrdapi_break_window_control",
"xmem_run_break_readers_check", "xmem_run_break_readers_control", "xmem_cross_call_merge_check",
"xmem_placeholder_split_check", "xmem_map_split_first_check", "startup_dstorage_break_window_check",
"xmem_page_runs_device_check", "linear_buffer_backing_check", "existing_heap_shared_buffer_check", "mesh86_typed_vsharp_check", "mesh86_typed_vsharp_wide_format_mut", "mesh86_typed_vsharp_any_map_mut", "mesh86_typed_vsharp_any_swizzle_mut", "mesh86_typed_vsharp_any_tid_mut",
"xmem_page_runs_device_check", "linear_buffer_backing_check", "existing_heap_shared_buffer_check", "startup_request_publisher_check", "mesh86_typed_vsharp_check", "mesh86_typed_vsharp_wide_format_mut", "mesh86_typed_vsharp_any_map_mut", "mesh86_typed_vsharp_any_swizzle_mut", "mesh86_typed_vsharp_any_tid_mut",
"bcpack_formats_check", "startup_stub_consumer", "startup_inline_retire", "startup_resource_retire_title", "startup_table_lock",
"native_compute_ir_dump", "native_pixel_ir_dump", "native_store_replay", "native_raw_buffer_view_check"
]
Expand All @@ -50,7 +50,7 @@
"displayName": "Windows pull request smoke tests",
"filter": {
"include": {
"name": "^(bcpack_all_formats(_golden_control)?|startup_bcpack_storage|desktop_root_signature|era_heap_(allocation|failure)|xmem_shim_mapped_pages|cpu_aot_fuzz_smoke|cpu_archive_texture_gpu_warp|dual_uav_warp|spirv_.*_warp|static_exe_intake|descriptor_heap_paging(_swapped)?_warp|startup_launch|startup_mesh57_real_shape|startup_mesh57_runtime(_[a-z]+)?_warp|native_fixed_mesh57_published_pair(_refusal)?_warp|startup_tools_lint|startup_gfx2hlsl_cvt_off_f32_i4_controls|startup_gfx2hlsl_sad_hi_msad_controls|startup_indirect_(dispatch|count_split|draw(_indexed)?|mesh(_xmem_arguments(_skip_wait)?)?|counts_control)_warp|startup_indirect_(dispatch|constants|count)_inline_warp|startup_inline_retire_(on|control)_warp|startup_resource_retire_title_inline_warp_debug0|startup_table_lock_(control_)?warp|startup_indirect_mesh_lease_warp_dual(-trace(-disabled)?)?|startup_indirect_mesh_cache_heap(_wide)?_warp|runtime_mesh86_cache_cli_check|startup_shader_cache_key|startup_present_readback|startup_present_planes(_control)?|startup_present_composite_(warp|overlay_removed_control_warp)|startup_mesh_title_snapshot_warp|startup_list_copy_(warp|title_snapshot_warp|validation(_[a-z_]+_control)?)|xmem_(run_break_readers|cross_call_merge|placeholder_split|map_split_first)(_[a-z_]+)?|startup_dstorage_break_window(_[a-z_]+)?|xmem_page_runs_device(_join)?_warp|linear_buffer_backing_share_warp|existing_heap_shared_buffer(_offset_control)?_warp|startup_stage_package|startup_compute_packed_d16_x_uav_reference|native_.*_(ir|dxil|controls)|native_direct_mesh|startup_triage_run|mesh86_typed_vsharp_[a-z_]+)$"
"name": "^(bcpack_all_formats(_golden_control)?|startup_bcpack_storage|desktop_root_signature|era_heap_(allocation|failure)|xmem_shim_mapped_pages|cpu_aot_fuzz_smoke|cpu_archive_texture_gpu_warp|dual_uav_warp|spirv_.*_warp|static_exe_intake|descriptor_heap_paging(_swapped)?_warp|startup_launch|startup_mesh57_real_shape|startup_mesh57_runtime(_[a-z]+)?_warp|native_fixed_mesh57_published_pair(_refusal)?_warp|startup_tools_lint|startup_gfx2hlsl_cvt_off_f32_i4_controls|startup_gfx2hlsl_sad_hi_msad_controls|startup_indirect_(dispatch|count_split|draw(_indexed)?|mesh(_xmem_arguments(_skip_wait)?)?|counts_control)_warp|startup_indirect_(dispatch|constants|count)_inline_warp|startup_inline_retire_(on|control)_warp|startup_resource_retire_title_inline_warp_debug0|startup_table_lock_(control_)?warp|startup_indirect_mesh_lease_warp_dual(-trace(-disabled)?)?|startup_indirect_mesh_cache_heap(_wide)?_warp|runtime_mesh86_cache_cli_check|startup_shader_cache_key|startup_present_readback|startup_present_planes(_control)?|startup_present_composite_(warp|overlay_removed_control_warp)|startup_mesh_title_snapshot_warp|startup_list_copy_(warp|title_snapshot_warp|validation(_[a-z_]+_control)?)|xmem_(run_break_readers|cross_call_merge|placeholder_split|map_split_first)(_[a-z_]+)?|startup_dstorage_break_window(_[a-z_]+)?|xmem_page_runs_device(_join)?_warp|linear_buffer_backing_share_warp|existing_heap_shared_buffer(_offset_control)?_warp|startup_stage_package|startup_compute_packed_d16_x_uav_reference|native_.*_(ir|dxil|controls)|native_direct_mesh|startup_triage_run|mesh86_typed_vsharp_[a-z_]+|startup_request_publisher(_inline_control)?)$"
}
}
},
Expand Down
1 change: 1 addition & 0 deletions docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -1865,6 +1865,7 @@ ctest --preset dev --no-tests=error -R '^startup_hook_(window|overlap)$' -V
| `X2W_D3D12_RELEASE_RETIRED=1` | opt-in, effective with `X2W_D3D12_RENDER=1` only (the list handlers that record holds exist only there): frees the desktop object of a released committed 2D texture of one narrow class once no recorded list, submission or release-time fence refers to it ([details](#d3d12-resource-budget)). Unset, nothing changes |
| `X2W_D3D12_INLINE_DESCRIPTORS=1` | opt-in: leaves byte +0x18 of the guest device at 0, so the title writes constant buffer views and copies CBV/SRV/UAV descriptors itself instead of calling device slots 17 and 24 (sampler copies still arrive through slot 24); with `X2W_D3D12_RELEASE_RETIRED=1`, a retired resource is then freed once its release fences pass ([details](#d3d12-inline-descriptors)). Unset, nothing changes |
| `X2W_D3D12_NO_VISIBLE_HEAP_MIRROR=1` | negative control only: title view and sampler heaps created shader-visible get no CPU-only mirror. By default every view, sampler and copy the stand-in writes into a shader-visible range goes to the mirror first and is copied from there, and copies and `ClearUnorderedAccessView*` read a visible descriptor from the mirror, because desktop D3D12 reads those only from CPU-only heaps (debug layer message 654; a driver fault on a GTX 1060, #3558). The switch restores the direct read |
| `X2W_SYNC_MESH_REQUESTS=1` | negative control only: mesh pipeline creations write their requests themselves, under the path's lock, before they answer: slot 57 (`req_<key>.ps57`, `.root`, `.m57`, and `.s57` with `X2W_MESH57_CAPTURE_STATE=1`) and slot 86 (`.ps86`, `.root`, `.stream86`, `.m86`). By default a creation queues its request and returns; one background thread writes the parts in order (`src/startup_stubs/request_publisher.h`). A key is queued once per process, at most 64 MiB of parts wait at a time (a request past that is dropped; slot 57 keeps its stages and queues it again on a later call, slot 86 asks again in a later run), and what is still queued is written at module unload for at most 2 s. The periodic `x2w-render mesh request publisher:` line counts queued, repeated, written, failed and dropped requests. On a GTX 1060 a title creating thousands of mesh pipelines waited on these writes one creation at a time: about 7 slot 86 creations a second, and a 45 s gap in presents while title threads queued on the slot 57 request lock. `ctest --preset dev --no-tests=error -R '^startup_request_publisher'` (the inline control must fail) |
| `X2W_D3D12_RELEASE_RETIRED_VIEWS=1` | legacy diagnostic. Logs that clearing live slots is disabled and enables the existing retirement breakdown when base retirement and budgets are on ([details](#d3d12-resource-budget)) |
| `X2W_D3D12_AGILITY=<dir>` | opt-in: the stand-ins select the Agility SDK `D3D12Core.dll` in `<dir>` before the first D3D12 device of the process (Windows Developer Mode only; `<dir>` on the executable's drive). Unset, the in-box runtime stays and nothing is read ([details](#d3d12-agility-sdk-runtime)) |
| `X2W_D3D12_RETIRE_REASONS=1` | diagnostic only, effective with `X2W_D3D12_RELEASE_RETIRED=1` and `X2W_D3D12_BUDGET=1`: adds one `x2w-retire held:` line to the heartbeat that counts final-released resources whose desktop object is still alive, by reason ([details](#d3d12-resource-budget)). Unset, nothing is recorded |
Expand Down
101 changes: 97 additions & 4 deletions src/startup_stubs/d3d12_draw.inl
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,93 @@ bool publish_shader_request(u64 key, const wchar_t* ext, const void* data, std::
return exists;
}

// Mesh pipeline requests (slot 57 and slot 86) are written on one background thread (request_publisher.h), so a title
// that creates thousands of mesh pipelines does not wait on a probe, a temporary file and a rename per request part,
// one creation at a time. At most mesh_request_publish_limit bytes of copied parts wait; a request past that is
// dropped and counted, and the title asks for it again in a later run. X2W_SYNC_MESH_REQUESTS=1 (negative control)
// writes them on the creating thread, as before.
constexpr std::size_t mesh_request_publish_limit = std::size_t{64} << 20;

bool sync_mesh_requests()
{
static const bool on = x2w_local::env_on("X2W_SYNC_MESH_REQUESTS");
return on;
}

bool write_mesh_request_part(void*, u64 key, const wchar_t* extension, const void* data, std::size_t bytes)
{
return publish_shader_request(key, extension, data, bytes);
}

// The cache directory, read once when the publisher is made, so the unload writer reads no environment variable.
wchar_t mesh_request_dir[MAX_PATH];

// Made on first use and never destroyed: the drain below runs at module unload, after function-local statics are gone.
x2w_startup::RequestPublisher* volatile mesh_request_instance;

x2w_startup::RequestPublisher& mesh_request_publisher()
{
static auto* publisher = [] {
const DWORD n = GetEnvironmentVariableW(L"X2W_SHADER_CACHE", mesh_request_dir, MAX_PATH);
if (!n || n >= MAX_PATH) mesh_request_dir[0] = 0;
auto* made = new x2w_startup::RequestPublisher(write_mesh_request_part, nullptr, mesh_request_publish_limit);
mesh_request_instance = made;
return made;
}();
return *publisher;
}

// The unload writer: a temporary file of its own and a rename, without the cache index (whose lock a terminated
// thread may hold) or anything else that locks. A part already on disk is left as it is.
bool write_mesh_request_part_at_exit(void*, u64 key, const wchar_t* extension, const void* data, std::size_t bytes)
{
if (!mesh_request_dir[0]) return false;
wchar_t path[MAX_PATH], temporary[MAX_PATH];
if (swprintf(path, MAX_PATH, L"%s\\req_%016llx%s", mesh_request_dir, key, extension) < 0 ||
swprintf(temporary, MAX_PATH, L"%s.%lu.exit.tmp", path, GetCurrentProcessId()) < 0)
return false;
if (GetFileAttributesW(path) != INVALID_FILE_ATTRIBUTES) return true;
HANDLE file = CreateFileW(temporary, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
if (file == INVALID_HANDLE_VALUE) return false;
DWORD written = 0;
const bool complete = WriteFile(file, data, static_cast<DWORD>(bytes), &written, nullptr) && written == bytes;
CloseHandle(file);
if (complete && MoveFileExW(temporary, path, 0)) return true;
DeleteFileW(temporary);
return GetFileAttributesW(path) != INVALID_FILE_ATTRIBUTES;
}

// At module unload, write what is still queued, for at most two seconds, so a short run (a fixture) leaves its
// requests on disk. Nothing is made here when no request was queued.
struct MeshRequestDrain {
~MeshRequestDrain()
{
if (auto* publisher = mesh_request_instance) publisher->drain(2000, write_mesh_request_part_at_exit);
}
} mesh_request_drain;

// Publishes the parts of one request in order: queued, or written now under X2W_SYNC_MESH_REQUESTS=1. False when the
// request was dropped (queued) or a part could not be written (sync).
bool publish_mesh_request(u64 key, const x2w_startup::RequestPublisher::Part* parts, std::size_t count)
{
if (!sync_mesh_requests()) return mesh_request_publisher().enqueue(key, parts, count);
for (std::size_t i = 0; i < count; ++i)
if (!publish_shader_request(key, parts[i].extension, parts[i].data, parts[i].bytes)) return false;
return true;
}

void mesh_request_publisher_line(char (&line)[256])
{
line[0] = 0;
auto* publisher = mesh_request_instance;
if (!publisher) return;
const auto s = publisher->stats();
if (!s.queued && !s.dropped) return;
std::snprintf(line, sizeof(line),
"x2w-render mesh request publisher: queued %lld repeated %lld written %lld failed %lld dropped %lld pending %zu (%zu KiB, high water %zu KiB)",
s.queued, s.repeated, s.written, s.failed, s.dropped, s.pending, s.pending_bytes >> 10, s.high_water_bytes >> 10);
}

bool native_shaders_only()
{
static const bool enabled = [] {
Expand Down Expand Up @@ -1067,13 +1154,19 @@ void request_slot57_mesh(Wrapper* pipeline, const std::vector<RootParameter>* bo
std::memcpy(packed.data() + sizeof(key), lengths, sizeof(lengths));
for (const auto* bytes : {&desc, &info->state_a, &info->state_b})
packed.insert(packed.end(), bytes->begin(), bytes->end());
if (!publish_shader_request(variant, L".s57", packed.data(), packed.size()))
const x2w_startup::RequestPublisher::Part state{L".s57", packed.data(), packed.size()};
if (!publish_mesh_request(variant, &state, 1))
log("x2w-draw fixed mesh state %016llx could not be published", variant);
}
const unsigned char none = 0;
if (written.count(key) || (publish_shader_request(key, L".ps57", pixel.empty() ? &none : pixel.data(), pixel.size()) &&
publish_shader_request(key, L".root", root_blob.data(), root_blob.size()) &&
publish_shader_request(key, L".m57", mesh.data(), mesh.size()))) {
// The parts are copied into the publisher's queue (or written now with X2W_SYNC_MESH_REQUESTS=1); a dropped
// request keeps its stages, so a later call queues it again.
const x2w_startup::RequestPublisher::Part parts[]{
{L".ps57", pixel.empty() ? &none : pixel.data(), pixel.size()},
{L".root", root_blob.data(), root_blob.size()},
{L".m57", mesh.data(), mesh.size()},
};
if (written.count(key) || publish_mesh_request(key, parts, std::size(parts))) {
const bool first = written.insert(key).second;
static LONG logged;
if (first && first_calls(logged, 12))
Expand Down
4 changes: 4 additions & 0 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include <optional>
#include "startup_stubs/derived_blend.h"
#include "startup_stubs/placement_publish.h"
#include "startup_stubs/request_publisher.h"
#include "startup_stubs/resource_address_index.h"
#include "startup_stubs/placed_build.h"
#include "startup_stubs/placed_buffer_range.h"
Expand Down Expand Up @@ -9009,6 +9010,9 @@ HRESULT __stdcall queue_present(Wrapper* self, UINT planes, const void* plane_pa
char mesh57_line[1536];
mesh57_outcome_line(mesh57_line);
log("%s", mesh57_line);
char mesh_request_line[256];
mesh_request_publisher_line(mesh_request_line);
if (mesh_request_line[0]) log("%s", mesh_request_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
26 changes: 19 additions & 7 deletions src/startup_stubs/mesh86_cache.inl
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,27 @@ ID3D12PipelineState* try_cached_mesh86(const D3D12_PIPELINE_STATE_STREAM_DESC& d
auto key = fnv_bytes(revision, sizeof(revision) - 1);
for (const auto* part : {&request.ms, &request.ps, &root_blob, &request.stream}) key = fnv_part(*part, key);
static SRWLOCK lock = SRWLOCK_INIT;
x2w_startup::ExclusiveNoteLock guard(lock);
const unsigned char none = 0;
if (!publish_shader_request(key, L".ps86", request.ps.empty() ? &none : request.ps.data(), request.ps.size()) ||
!publish_shader_request(key, L".root", root_blob.data(), root_blob.size()) ||
!publish_shader_request(key, L".stream86", request.stream.data(), request.stream.size()) ||
!publish_shader_request(key, L".m86", request.ms.data(), request.ms.size())) {
std::snprintf(why, why_size, "slot86 mesh request %016llx publication failed", key);
return nullptr;
const x2w_startup::RequestPublisher::Part parts[]{
{L".ps86", request.ps.empty() ? &none : request.ps.data(), request.ps.size()},
{L".root", root_blob.data(), root_blob.size()},
{L".stream86", request.stream.data(), request.stream.size()},
{L".m86", request.ms.data(), request.ms.size()},
};
if (sync_mesh_requests()) {
// X2W_SYNC_MESH_REQUESTS=1: every creation writes its request under the lock, as before the publisher.
x2w_startup::ExclusiveNoteLock guard(lock);
for (const auto& part : parts)
if (!publish_shader_request(key, part.extension, part.data, part.bytes)) {
std::snprintf(why, why_size, "slot86 mesh request %016llx publication failed", key);
return nullptr;
}
} else {
// The request is written on the publisher's thread; until the translator answers it the creation below finds
// no marker and reports it pending, the same answer a creation gave while its own write was in progress.
publish_mesh_request(key, parts, std::size(parts));
}
x2w_startup::ExclusiveNoteLock guard(lock);
published_key = key;
const auto* params = root_info(request.root);
// Only the resource heap can be indexed, and only through a published heap-buffer window (bind_mesh86_heap). The
Expand Down
Loading
Loading