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
16 changes: 15 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ set_tests_properties(startup_dump_fence_removed_control_warp PROPERTIES ENVIRONM
WILL_FAIL TRUE)

add_executable(startup_stream_consumer tests/startup_stream_consumer.cpp tests/startup_stream_setup.cpp tests/startup_stream_draw.cpp
tests/startup_address_srv.cpp tests/startup_root_cbv.cpp)
tests/startup_address_srv.cpp tests/startup_root_cbv.cpp tests/startup_root_heap.cpp)
target_compile_features(startup_stream_consumer PRIVATE cxx_std_17)
target_link_libraries(startup_stream_consumer PRIVATE d3d12_xs_shim startup_stub_xg_xs d3d12 dxgi)
target_sources(startup_stream_consumer PRIVATE tests/startup_frame_copy.cpp)
Expand Down Expand Up @@ -779,6 +779,16 @@ add_test(NAME startup_translated_sampler_bindings COMMAND startup_translated_sam
add_test(NAME startup_translated_sampler_bindings_shared_axis_control
COMMAND startup_translated_sampler_bindings_check --mutate shared-axis)
set_tests_properties(startup_translated_sampler_bindings_shared_axis_control PROPERTIES WILL_FAIL TRUE)
# Real-draw decision for title root signatures that index descriptor heaps directly
# (src/startup_stubs/heap_indexed_root.h), without a device. The mutated runs are the negative controls.
add_executable(startup_heap_indexed_root_check tests/startup_heap_indexed_root_check.cpp)
target_include_directories(startup_heap_indexed_root_check PRIVATE src)
target_compile_features(startup_heap_indexed_root_check PRIVATE cxx_std_17)
add_test(NAME startup_heap_indexed_root COMMAND startup_heap_indexed_root_check)
foreach(mutation IN ITEMS sampler-alone bind-unrestorable)
add_test(NAME startup_heap_indexed_root_${mutation}_control COMMAND startup_heap_indexed_root_check --mutate ${mutation})
set_tests_properties(startup_heap_indexed_root_${mutation}_control PROPERTIES WILL_FAIL TRUE)
endforeach()
target_sources(offscreen_check PRIVATE tests/startup_image_3d.cpp)
target_sources(offscreen_check PRIVATE tests/startup_flat_param.cpp)
# The storage root handed to the executable ends in a separator and is never cut off
Expand Down Expand Up @@ -1781,6 +1791,10 @@ if(EXISTS "${INTAKE_PYTHON}")
"${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_root_cbv_check.py"
"$<TARGET_FILE:startup_stream_consumer>" ${backend})
set_tests_properties(startup_root_cbv_${backend} PROPERTIES TIMEOUT 90 SKIP_RETURN_CODE 77)
add_test(NAME startup_root_heap_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}"
"${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_root_heap_check.py"
"$<TARGET_FILE:startup_stream_consumer>" ${backend})
set_tests_properties(startup_root_heap_${backend} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77)
add_test(NAME startup_address_srv_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}"
"${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_address_srv_check.py"
"$<TARGET_FILE:startup_stream_consumer>" "${backend}")
Expand Down
9 changes: 9 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -10705,3 +10705,12 @@ Command-list slot 42 records the address of a root parameter declared as a CBV,
cmake --build --preset dev --target startup_stream_consumer
ctest --preset dev --no-tests=error -R "^startup_root_cbv_(warp|hardware)$" -V
```

## Startup heap-indexed root signatures

Command-list slot 34 no longer sets a title root signature flagged `CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED` or `SAMPLER_HEAP_DIRECTLY_INDEXED` on the desktop list, because the title's heaps are not forwarded there (debug layer message ID 1419). Real draws under such a root are refused with a specific reason. The decision matrix (`tests/startup_heap_indexed_root_check.cpp`) runs without a device; its two mutated runs must fail. The draw fixture (`tests/startup_root_heap_check.py`) is synthetic only and reuses the root CBV draw with the debug layer on; a plain desktop list with the same root signature must first report ID 1419 for each flag, or the run is a capability skip. Details are in [startup-runtime.md](startup-runtime.md#heap-indexed-root-signatures).

```powershell
cmake --build --preset dev --target startup_stream_consumer startup_heap_indexed_root_check
ctest --preset dev --no-tests=error -R "^startup_(heap_indexed_root.*|root_heap_(warp|hardware))$" -V
```
24 changes: 24 additions & 0 deletions docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,30 @@ the console shift, not yet seen in a title log; `startup_root_cbv_{warp,hardware
unbound, a null-overwrite and a wrong-type call. Rebinding the same root signature keeps
the address. Switching to a different signature invalidates the stored root addresses.

## Heap-indexed root signatures

Device slot 16 records whether a title root signature was created with
`D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED` (`0x400`), `SAMPLER_HEAP_DIRECTLY_INDEXED` (`0x800`)
or both. Command-list slot 32 only tracks the heaps a title sets and never forwards them, so setting such a root
signature on the desktop list made the debug layer report message ID 1419 for each flag. Slot 34 now keeps
tracking the title's parameters, root addresses and constants for every root signature, but sets only an unflagged
one on the desktop list, as before; a flagged one is deferred (`root signature ... with heap flags 0x... not set on
the desktop list`, first three). Root SRVs (slot 44) are not forwarded under a deferred root. Translated draws use
their own root signature (no flags) and are unaffected.

A real draw under a deferred root is decided by `src/startup_stubs/heap_indexed_root.h`: it may bind only when the
list holds a mirror of every heap the root indexes, covering the indices the draw reads, and every root constant,
descriptor table and root descriptor can be set again. A one-entry sampler heap covers only index 0 and never stands
in for the view heap. The stand-in has no mirrored heap and cannot restore that state, so it refuses every real draw
under a flagged root, counts it by reason and logs `real draw refused: root signature heap flags 0x..., <reason>`
(first three per reason, then every 1000th). The `x2w-render totals` line reports `heap-indexed roots deferred N
(real draws refused: view heap, sampler heap, both heaps, state)`. On current main no source-built path reaches a
real draw (`compile_translation` returns no shader), so the refusal itself is checked only by the device-free
`startup_heap_indexed_root` matrix. `startup_root_heap_{warp,hardware}` runs the root CBV draw with the debug layer
on, for each flag alone, both and neither: a plain desktop list with the same root signature and no heaps must
produce one ID 1419 per flag first (otherwise the run skips with 77), and through the stand-in no such message may
appear while the translated draw keeps its colour.

## Composite provenance fixture

Issue #1628 asks whether a uniform colour left in a composite's destination came from the composite or was
Expand Down
79 changes: 71 additions & 8 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "startup_stubs/draw_bmp.h"
#include "startup_stubs/draw_dump_tracking.h"
#include "startup_stubs/translated_sampler_bindings.h"
#include "startup_stubs/heap_indexed_root.h"
#include "startup_stubs/bc7_4k_s.h"
#include "startup_stubs/bc64_chain.h"
#include "startup_stubs/derived_blend.h"
Expand Down Expand Up @@ -1418,6 +1419,20 @@ std::unordered_map<const Wrapper*, std::vector<RootParameter>>& root_infos()
static std::unordered_map<const Wrapper*, std::vector<RootParameter>> map;
return map;
}
// The heap-indexing flags (x2w_startup::root_flags_heap_indexed) each title root signature was created with.
std::unordered_map<const Wrapper*, UINT>& root_heap_flag_map()
{
static std::unordered_map<const Wrapper*, UINT> map;
return map;
}
UINT root_heap_flags(const Wrapper* root)
{
AcquireSRWLockShared(&root_lock);
const auto found = root_heap_flag_map().find(root);
const UINT flags = found == root_heap_flag_map().end() ? 0 : found->second;
ReleaseSRWLockShared(&root_lock);
return flags;
}
const std::vector<RootParameter>* root_info(const Wrapper* root)
{
AcquireSRWLockShared(&root_lock);
Expand All @@ -1428,7 +1443,8 @@ const std::vector<RootParameter>* root_info(const Wrapper* root)
}

// Device slot 16, desktop CreateRootSignature(node, blob, size, riid, ppv); the parameter list is
// kept for the translated shaders (register and space of each root descriptor).
// kept for the translated shaders (register and space of each root descriptor), and the heap-indexing
// flags for command-list slot 34.
HRESULT __stdcall device_create_root_signature(Wrapper* self, UINT node, const void* blob, SIZE_T size, const GUID* iid, void** out)
{
if (!out || !iid) return E_POINTER;
Expand All @@ -1445,10 +1461,12 @@ HRESULT __stdcall device_create_root_signature(Wrapper* self, UINT node, const v
Wrapper* w = wrap(root, root_signature_kind);
note_root_blob(w, blob, size);
std::vector<RootParameter> parameters;
UINT heap_flags = 0;
ID3D12VersionedRootSignatureDeserializer* deserializer = nullptr;
if (SUCCEEDED(D3D12CreateVersionedRootSignatureDeserializer(blob, size, IID_PPV_ARGS(&deserializer)))) {
const D3D12_VERSIONED_ROOT_SIGNATURE_DESC* desc = nullptr;
if (SUCCEEDED(deserializer->GetRootSignatureDescAtVersion(D3D_ROOT_SIGNATURE_VERSION_1_1, &desc)) && desc) {
heap_flags = static_cast<UINT>(desc->Desc_1_1.Flags) & x2w_startup::root_flags_heap_indexed;
for (UINT i = 0; i < desc->Desc_1_1.NumParameters; ++i) {
const auto& parameter = desc->Desc_1_1.pParameters[i];
RootParameter info{parameter.ParameterType, 0, 0};
Expand All @@ -1468,11 +1486,12 @@ HRESULT __stdcall device_create_root_signature(Wrapper* self, UINT node, const v
for (std::size_t i = 0; i < parameters.size() && used < 220; ++i)
used += std::snprintf(text + used, sizeof(text) - used, " %d:r%u,s%u", parameters[i].type, parameters[i].shader_register,
parameters[i].space);
log("x2w-d3d12 Device::CreateRootSignature size=0x%llx -> %p parameters (type:register,space)%s", static_cast<u64>(size),
static_cast<void*>(w), text);
log("x2w-d3d12 Device::CreateRootSignature size=0x%llx -> %p heap flags 0x%x parameters (type:register,space)%s",
static_cast<u64>(size), static_cast<void*>(w), heap_flags, text);
}
AcquireSRWLockExclusive(&root_lock);
root_infos()[w] = std::move(parameters);
root_heap_flag_map()[w] = heap_flags;
ReleaseSRWLockExclusive(&root_lock);
*out = w;
return S_OK;
Expand Down Expand Up @@ -1631,6 +1650,10 @@ struct Pass {
bool topology_ok = false;
unsigned srv_bound = 0; // bit per root parameter with a desktop address bound
const std::vector<RootParameter>* root = nullptr;
// Heap-indexing flags of the title root signature last set by slot 34. While nonzero, that root signature is
// deferred: it is not set on the desktop list, root SRVs are not forwarded, and real draws go through decide_real_root.
UINT root_heap_flags = 0;
ID3D12RootSignature* deferred_root = nullptr;
std::vector<std::pair<ID3D12Resource*, D3D12_RESOURCE_STATES>> states; // resources this list moved out of COMMON
// What the title bound, kept for draws through translated pipelines:
Wrapper* pipeline = nullptr;
Expand Down Expand Up @@ -1660,6 +1683,8 @@ struct Pass {
UINT default_viewport_h = 0, default_scissor_h = 0;
};
LONG render_draws, render_draws_skipped, render_pipelines_real, render_pipelines_missing;
// Heap-indexed title root signatures not set on the desktop list, and real draws refused under one, by decision.
LONG heap_roots_deferred, heap_root_refusals[x2w_startup::real_root_decision_count];

ID3D12GraphicsCommandList* recording(Wrapper* self) { return self->own().list_open ? static_cast<ID3D12GraphicsCommandList*>(self->own().real) : nullptr; }
Pass& pass_of(Wrapper* self)
Expand Down Expand Up @@ -1727,7 +1752,9 @@ void __stdcall list_pipeline(Wrapper* self, void* pipeline)
InterlockedIncrement(pass.pipeline_real ? &render_pipelines_real : &render_pipelines_missing);
if (auto* list = recording(self); list && pass.pipeline_real) list->SetPipelineState(static_cast<ID3D12PipelineState*>(w->own().real));
}
// List slot 34 (desktop 30 + 4), SetGraphicsRootSignature.
// List slot 34 (desktop 30 + 4), SetGraphicsRootSignature. The title's parameters are tracked for translated
// draws either way. A root signature that indexes a descriptor heap directly is not set on the desktop list: the
// title's heaps are never forwarded there (slot 32), so the debug layer would reject it (message ID 1419).
void __stdcall list_graphics_root(Wrapper* self, void* root)
{
Wrapper* w = as_wrapper(root);
Expand All @@ -1737,8 +1764,19 @@ void __stdcall list_graphics_root(Wrapper* self, void* root)
std::fill_n(pass.root_address, 16, 0);
pass.root = params;
pass.srv_bound = 0;
if (auto* list = recording(self); list && w && w->own().kind == root_signature_kind && w->own().real)
const bool real = w && w->own().kind == root_signature_kind && w->own().real;
pass.root_heap_flags = real ? root_heap_flags(w) : 0;
pass.deferred_root = pass.root_heap_flags ? static_cast<ID3D12RootSignature*>(w->own().real) : nullptr;
auto* list = recording(self);
if (!list || !real) return;
if (!pass.root_heap_flags) {
list->SetGraphicsRootSignature(static_cast<ID3D12RootSignature*>(w->own().real));
return;
}
const LONG deferred = InterlockedIncrement(&heap_roots_deferred);
if (deferred <= 3)
log("x2w-render root signature %p with heap flags 0x%x not set on the desktop list (title heaps are not forwarded)",
static_cast<void*>(w), pass.root_heap_flags);
}
DXGI_FORMAT view_format_of(u64 desktop_handle, ID3D12Resource* target)
{
Expand Down Expand Up @@ -1881,7 +1919,8 @@ void __stdcall list_root_srv(Wrapper* self, UINT parameter, u64 address)
Pass& pass = pass_of(self);
if (parameter < 32) pass.srv_bound &= ~(1u << parameter);
if (parameter < 16) pass.root_address[parameter] = address;
if (!list || !pass.root || parameter >= pass.root->size() || parameter >= 32 ||
// Under a deferred heap-indexed root the desktop list holds another root signature; only the title address is kept.
if (!list || !pass.root || pass.root_heap_flags || parameter >= pass.root->size() || parameter >= 32 ||
(*pass.root)[parameter].type != D3D12_ROOT_PARAMETER_TYPE_SRV)
return;
const std::size_t bytes = readable_window(address, 0x10000);
Expand Down Expand Up @@ -1926,6 +1965,25 @@ void __stdcall list_draw(Wrapper* self, UINT vertices, UINT instances, UINT firs
: &untranslated_draws);
return;
}
if (list && pass.root_heap_flags) {
// The desktop list holds no copy of the title's heaps, and the root constants and descriptor tables the
// title set are kept only as title values, so the deferred root signature cannot be set with its state.
x2w_startup::RealRootDraw request;
request.root_flags = pass.root_heap_flags;
request.state_restorable = false;
const x2w_startup::RealRootDecision decision = x2w_startup::decide_real_root(request);
if (decision != x2w_startup::RealRootDecision::bind) {
const LONG n = InterlockedIncrement(&heap_root_refusals[static_cast<int>(decision)]);
InterlockedIncrement(&render_draws_skipped);
if (n <= 3 || n % 1000 == 0)
log("x2w-render real draw refused: root signature heap flags 0x%x, %s (%ld so far)", pass.root_heap_flags,
x2w_startup::real_root_reason(decision), n);
return;
}
// Not reached with the facts above: a change that mirrors the title heaps and restores the root state on the
// list, with an end-to-end fixture, makes the deferred bind here.
list->SetGraphicsRootSignature(pass.deferred_root);
}
// The translated vertex shaders read their fetch table through root parameter 0.
if (list && pass.pipeline_real && pass.targets_bound && pass.topology_ok && pass.topology != 0x11 && (pass.srv_bound & 1)) {
list->DrawInstanced(vertices, instances, first_vertex, first_instance);
Expand Down Expand Up @@ -3110,11 +3168,16 @@ HRESULT __stdcall queue_present(Wrapper* self, UINT planes, const void* plane_pa
if (render_mode() && frame % 300 == 0)
log("x2w-render totals: resets %ld (failed %ld), closes %ld, clears %ld, executes %ld, draws %ld (skipped %ld), "
"translated draws %ld (not drawn %ld), pipelines translated %ld / missing %ld, null bindings %ld (entry %ld, buffer %ld, token %ld, wrapper %ld, dimension %ld), "
"textures uploaded %ld (%ld KiB, unreadable %ld)", render_resets,
"textures uploaded %ld (%ld KiB, unreadable %ld), heap-indexed roots deferred %ld (real draws refused: view heap %ld, "
"sampler heap %ld, both heaps %ld, state %ld)", render_resets,
render_reset_failures, render_closes, render_clears, render_executes, render_draws, render_draws_skipped,
translated_draws, untranslated_draws, translation_ready, translation_missing, null_total,
null_causes[0], null_causes[1], null_causes[2], null_causes[3], null_causes[4], textures_uploaded,
texture_bytes_uploaded, textures_unreadable);
texture_bytes_uploaded, textures_unreadable, heap_roots_deferred,
heap_root_refusals[static_cast<int>(x2w_startup::RealRootDecision::view_heap_missing)],
heap_root_refusals[static_cast<int>(x2w_startup::RealRootDecision::sampler_heap_missing)],
heap_root_refusals[static_cast<int>(x2w_startup::RealRootDecision::both_heaps_missing)],
heap_root_refusals[static_cast<int>(x2w_startup::RealRootDecision::state_not_restorable)]);
if (render_mode() && frame % 300 == 0) {
// Which command-list slots the title called in the last 300 frames, per frame.
char line[2048];
Expand Down
Loading
Loading