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
15 changes: 15 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,10 @@ target_sources(startup_stream_consumer PRIVATE tests/startup_root_reset.cpp)
add_executable(startup_stream_depth tests/startup_stream_depth.cpp $<TARGET_OBJECTS:startup_stream_setup_obj>)
target_compile_features(startup_stream_depth PRIVATE cxx_std_17)
target_link_libraries(startup_stream_depth PRIVATE d3d12_xs_shim d3d12 dxgi)
# X2W_DEPTH_STATE draw fixture (#1999, item A): slot 57 pipelines with depth-stencil register words.
add_executable(startup_slot57_depth tests/startup_slot57_depth.cpp $<TARGET_OBJECTS:startup_stream_setup_obj>)
target_compile_features(startup_slot57_depth PRIVATE cxx_std_17)
target_link_libraries(startup_slot57_depth PRIVATE d3d12_xs_shim d3d12 dxgi)
# Depth target binding for translated draws (list slot 50, #1999 item D): depth values and debug-layer
# messages after the fence, and an unresolved depth handle.
add_executable(startup_draw_depth_target tests/startup_draw_depth_target.cpp $<TARGET_OBJECTS:startup_stream_setup_obj>)
Expand Down Expand Up @@ -1425,6 +1429,13 @@ target_compile_features(startup_pipeline_state_check PRIVATE cxx_std_17)
add_test(NAME startup_pipeline_state COMMAND startup_pipeline_state_check)
add_test(NAME startup_pipeline_state_offset_control COMMAND startup_pipeline_state_check --mutate offset)
set_tests_properties(startup_pipeline_state_offset_control PROPERTIES WILL_FAIL TRUE)
# X2W_DEPTH_STATE (src/startup_stubs/depth_state_apply.h): off takes nothing from the block, on takes a decodable one.
add_executable(startup_depth_state_check tests/startup_depth_state_check.cpp)
target_include_directories(startup_depth_state_check PRIVATE src)
target_compile_features(startup_depth_state_check PRIVATE cxx_std_17)
add_test(NAME startup_depth_state COMMAND startup_depth_state_check)
add_test(NAME startup_depth_state_switch_control COMMAND startup_depth_state_check --mutate switch)
set_tests_properties(startup_depth_state_switch_control PROPERTIES WILL_FAIL TRUE)
# Root table handles resolved against a logical view heap, and the records a translated draw reads
# from the window (src/startup_stubs/logical_view_table.h).
add_executable(startup_logical_view_table_check tests/startup_logical_view_table_check.cpp)
Expand Down Expand Up @@ -2668,6 +2679,10 @@ if(EXISTS "${INTAKE_PYTHON}")
"${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_stream_depth_check.py"
"$<TARGET_FILE:startup_stream_depth>" "${backend}")
set_tests_properties(startup_stream_depth_${backend} PROPERTIES TIMEOUT 90 SKIP_RETURN_CODE 77)
add_test(NAME startup_slot57_depth_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}"
"${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_slot57_depth_check.py"
"$<TARGET_FILE:startup_slot57_depth>" "${backend}")
set_tests_properties(startup_slot57_depth_${backend} PROPERTIES TIMEOUT 90 SKIP_RETURN_CODE 77)
# The drop control draws the bound case with an unresolved depth handle and must fail only its picture.
add_test(NAME startup_draw_depth_target_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}"
"${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_draw_depth_target_check.py"
Expand Down
35 changes: 34 additions & 1 deletion docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,7 @@ ctest --preset dev --no-tests=error -R '^startup_trap_header(_prefix_control|_pr
| `X2W_SHADER_CACHE=<dir>` | translation cache: the runtime writes source requests `req_<key>.{a,b,root}` and, when needed, RTV variants `req_<key>.rtv`. Run `tools/startup/gfx2hlsl.py --pending <dir>` while the process is running, or start the run with `run.ps1 -Translate` (see [Translating shaders during a run](#translating-shaders-during-a-run)). Active draws poll for an absent manifest at most once per second per pipeline and use the published translation on a subsequent draw. The translator compiles `.vs.cso` and `.ps.cso` with `fxc.exe`: `X2W_FXC`, else the Windows SDK 10.0.26100.0 copy, else the newest `Windows Kits\10\bin\<version>\x64`, else `PATH`. When none exists it writes `<key>.fail` naming `X2W_FXC` without publishing a new manifest. `X2W_FXC=` (empty) publishes the manifest alone. Rerun `--pending` with compilation enabled to fill in missing bytecode for an existing manifest. |
| `X2W_D3D12_QUERY_SLOTS=1` | diagnostic, in render mode: binds command-list slots 28 to 30 to the hypothesised occlusion query methods (see [Occlusion queries](#occlusion-queries)). Unset, the three slots stay no-ops |
| `X2W_DRAW_NULL_IDENTITY=1` | null descriptors read as identity values instead of zero |
| `X2W_DEPTH_STATE=1` | translated draws of slot 57 pipelines take their depth-stencil state and stencil reference from state block A instead of the desktop default (depth and stencil off); a refused block keeps the default. Off by default, then nothing changes: see [Shortcuts that are known to be wrong](#shortcuts-that-are-known-to-be-wrong) |
| `X2W_PRESENT_SIGNATURE_EVERY=<n>` | in render mode, samples a coarse signature of the presented picture every `n` presents from the start, without a script request (`0` off). Unset, sampling starts, every 4 presents, only once a script with a `wait` asks for it: see [Waiting for the presented picture](#waiting-for-the-presented-picture) |
| `X2W_D3D12_DUMP_SURFACE=<bmp>` | writes the last presented surface |
| `X2W_DRAW_DUMP_FRAME=<n>`, `X2W_DRAW_DUMP_DIR` | Full-resolution RGB snapshots of single-sample RGBA8/BGRA8 UNORM or sRGB targets after translated draws. A later present writes completed queue-fenced copies. At most 48 copies are pending or written; a copy whose fence reports device removal (`UINT64_MAX`) is released unwritten and frees its slot (`src/startup_stubs/draw_dump_tracking.h`, `ctest --preset dev --no-tests=error -R '^startup_dump_fence_(removed_control_)?warp$' -V`; the control must fail). |
Expand Down Expand Up @@ -1597,7 +1598,8 @@ These are measured on one executable; treat them as hypotheses for others.
reason of each refusal. Its `_dest_alpha_control` run, which expects a destination alpha factor
in its colour form, is the negative control
(`ctest --preset dev --no-tests=error -R "^startup_cb_blend_decode" -V`).
- Depth-stencil registers are decoded but not applied yet (#1999, item A). State block A of a
- Depth-stencil registers are decoded, and applied only with `X2W_DEPTH_STATE=1` (#1999, item A;
see the next bullet). State block A of a
slot 57 pipeline holds `DB_DEPTH_CONTROL` at +0xB8, `DB_STENCIL_CONTROL` at +0xBC and
`DB_STENCILREFMASK` / `_BF` at +0xC0 / +0xC4, measured locally. Other pipeline layouts are not
covered. `src/startup_stubs/pipeline_state_decode.h`
Expand All @@ -1609,6 +1611,37 @@ These are measured on one executable; treat them as hypotheses for others.
each compare function and each stencil op with a D3D12 form, the refusals and a synthetic block.
Its `_offset_control` run (words one word later) is the negative control
(`ctest --preset dev --no-tests=error -R "^startup_pipeline_state" -V`).
- `X2W_DEPTH_STATE=1` (off by default) takes that decoded state for translated draws of slot 57
pipelines (`src/startup_stubs/depth_state_apply.h`, applied in `translation_for` in
`d3d12_draw.inl`). A slot 57 pipeline has no desktop `DEPTH_STENCIL` subobject, so there is no
desktop state to outrank; a state found first is kept regardless. When the decoder refuses the
words, or the block is too short, the pipeline keeps the desktop default (depth and stencil off)
and one line says why (`depth-stencil registers not used (...)`). The decoded state needs a depth
view: a draw with none bound keeps the desktop default. The stencil reference is list state in
D3D12, so each draw of such a pipeline with an enabled stencil test calls `OMSetStencilRef` with
the decoded value for that draw and restores the guest's list reference immediately after it.
A successful list reset restores the default reference of zero. The mixed-pipeline test sets a
distinct guest reference before the slot 57 draw, then checks a desktop-state stencil draw on
the same list without another reference call. The pipeline key includes the
decoded description. With the switch off nothing reads the words: no description, key or
reference changes, and no log line appears. That is checked device-free
(`startup_depth_state`, with `startup_depth_state_switch_control` as the negative control) and by
the draw fixture's switch-off run, and the existing `startup_stream_depth_*` and
`startup_slot57_blend_draw_*` tests, which run without the switch, still pass.
`tests/startup_slot57_depth.cpp` and `tests/startup_slot57_depth_check.py` draw two overlapping
triangles of unequal depth (z 0.25 and 0.75, same synthetic shaders and vertex program) into an
8x4 target with a D24S8 view, with the same renderer on WARP and on a hardware adapter. With
reversed-Z words (GREATER, depth cleared to 0) the nearer triangle wins (red); with the switch off
the last-drawn triangle wins (green); with LESS, the wrong direction for that clear, nothing
passes (blue); a forward-Z LESS case reverses the draw order and shows green with the switch on,
red with it off. Stencil is checked separately: EQUAL against a stored 0x5A passes with reference
0x5A and fails with 0x5B (the list's own reference is 0, so only the decoded one explains it), and
REPLACE 7 followed by EQUAL 7 or EQUAL 8 leaves green or red. Words with a colour-write tie or a
stencil `ONES` op fall back to the desktop result. Measured on WARP (10.0.26100.9818) and an
NVIDIA GeForce RTX 3080 (driver 32.0.16.2124), Windows 11 10.0.26200
(`ctest --preset dev --no-tests=error -R "^startup_(depth_state|slot57_depth)" -V`). Not covered:
raster bias, depth bounds, back-face-only state, the title's own `OMSetStencilRef` order, clears
(#1806), and any pipeline layout other than the measured one.
- Audio produces no output from the console audio path. A separate desktop render sink exists
(see "Audio output"); nothing connects the console path to it yet.

Expand Down
47 changes: 44 additions & 3 deletions src/startup_stubs/d3d12_draw.inl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ struct BoundPipeline {
D3D12_BLEND_DESC blend{};
bool has_depth_stencil = false;
D3D12_DEPTH_STENCIL_DESC depth_stencil{};
bool depth_from_registers = false; // X2W_DEPTH_STATE: depth_stencil was decoded from state block A (slot 57)
bool has_stencil_ref = false; // ... and its stencil test is on: stencil_ref goes on the list with each draw
std::uint8_t stencil_ref = 0;
DXGI_FORMAT dsv_format = DXGI_FORMAT_UNKNOWN;
std::unordered_map<u64, ID3D12PipelineState*> pipelines; // by target formats
};
Expand Down Expand Up @@ -389,6 +392,29 @@ BoundPipeline* translation_for(Wrapper* pipeline)
}
bound->dsv_format = info->stream_dsv_format;
}
// X2W_DEPTH_STATE=1 (#1999, item A): the depth-stencil registers of a slot 57 pipeline's state block A. A slot 57
// pipeline has no desktop DEPTH_STENCIL subobject, so has_depth_stencil can only be set here; the test keeps any
// desktop state that was found first. A block the decoder refuses leaves the desktop default (depth and stencil off).
if (info && info->path == 57 && !bound->has_depth_stencil && x2w_local::env_on("X2W_DEPTH_STATE")) {
const auto chosen = x2w_pipeline_state::select_depth_stencil(true, info->state_a.data(), info->state_a.size());
static LONG logged_applied, logged_refused;
if (chosen.applied) {
bound->depth_stencil = chosen.desc;
bound->has_depth_stencil = bound->depth_from_registers = true;
bound->has_stencil_ref = chosen.has_stencil_ref;
bound->stencil_ref = chosen.stencil_ref;
}
if (first_calls(chosen.applied ? logged_applied : logged_refused, 24)) {
if (chosen.applied)
log("x2w-draw pipeline %p: depth-stencil from state block A: depth %d func %d write %d, stencil %d reference 0x%x%s%s",
static_cast<void*>(pipeline), chosen.desc.DepthEnable, chosen.desc.DepthFunc, chosen.desc.DepthWriteMask,
chosen.desc.StencilEnable, chosen.stencil_ref, chosen.depth_bounds ? "; depth bounds not applied" : "",
chosen.masks_differ ? "; back-face masks or reference differ, front used" : "");
else
log("x2w-draw pipeline %p: depth-stencil registers not used (%s); keeping the desktop state", static_cast<void*>(pipeline),
chosen.reason);
}
}
if (info && info->path == 57 && info->desc.size() >= 0x60) {
u64 words[12];
std::memcpy(words, info->desc.data(), sizeof(words));
Expand Down Expand Up @@ -461,13 +487,21 @@ Translation* translation_for_targets(Translation* base, const Pass& pass)
return entry;
}

// The depth format a translated draw uses: the pass's own view, else the one the pipeline was created with.
inline DXGI_FORMAT pass_dsv_format(const BoundPipeline& bound, const Pass& pass)
{
return pass.dsv_format != DXGI_FORMAT_UNKNOWN ? pass.dsv_format : bound.dsv_format;
}

ID3D12PipelineState* pipeline_for(BoundPipeline& bound, Translation& tr, const Pass& pass)
{
const DXGI_FORMAT dsv_format = pass.dsv_format != DXGI_FORMAT_UNKNOWN ? pass.dsv_format : bound.dsv_format;
const DXGI_FORMAT dsv_format = pass_dsv_format(bound, pass);
u64 key = pass.target_count;
for (UINT i = 0; i < pass.target_count; ++i) key = key * 131 + static_cast<u64>(pass.target_formats[i]);
key = fnv_bytes(&dsv_format, sizeof(dsv_format), key);
if (bound.has_depth_stencil) key = fnv_bytes(&bound.depth_stencil, sizeof(bound.depth_stencil), key);
// A state decoded from registers needs a depth view to act on; with none bound the pass keeps the desktop default.
const bool use_depth_stencil = bound.has_depth_stencil && (!bound.depth_from_registers || dsv_format != DXGI_FORMAT_UNKNOWN);
if (use_depth_stencil) key = fnv_bytes(&bound.depth_stencil, sizeof(bound.depth_stencil), key);
const auto found = bound.pipelines.find(key);
if (found != bound.pipelines.end()) return found->second;
D3D12_GRAPHICS_PIPELINE_STATE_DESC desc{};
Expand All @@ -486,7 +520,7 @@ ID3D12PipelineState* pipeline_for(BoundPipeline& bound, Translation& tr, const P
bound.pipelines[key] = nullptr;
return nullptr;
}
if (bound.has_depth_stencil) desc.DepthStencilState = bound.depth_stencil;
if (use_depth_stencil) desc.DepthStencilState = bound.depth_stencil;
desc.SampleMask = ~0u;
desc.RasterizerState.FillMode = D3D12_FILL_MODE_SOLID;
desc.RasterizerState.CullMode = D3D12_CULL_MODE_NONE;
Expand Down Expand Up @@ -1161,6 +1195,12 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI
}
}
list->SetPipelineState(pipeline);
// X2W_DEPTH_STATE: the reference of the decoded stencil test is list state in D3D12. It replaces the one a
// preceding OMSetStencilRef set, for draws of this pipeline only when a depth view is bound.
const bool scoped_stencil_ref = bound->depth_from_registers && bound->has_stencil_ref &&
pass_dsv_format(*bound, pass) != DXGI_FORMAT_UNKNOWN &&
bound->stencil_ref != pass.stencil_reference;
if (scoped_stencil_ref) list->OMSetStencilRef(bound->stencil_ref);
list->SetGraphicsRootSignature(tr->root);
ID3D12DescriptorHeap* samplers = tr->samplers.heap ? pass.sampler_heap.Get() : nullptr;
if (!pass.heap_set || pass.bound_sampler_heap != samplers) {
Expand Down Expand Up @@ -1188,6 +1228,7 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI
list->DrawInstanced(rect ? count / 3 * 6 : count, instances ? instances : 1, 0, start_instance);
drawn = true;
}
if (scoped_stencil_ref) list->OMSetStencilRef(pass.stencil_reference);
if (drawn) draw_dump_after(self, list, tr->key, count);
ReleaseSRWLockExclusive(&draw_lock);
if (first_calls(tr->logged_draws, 10) || (count > 100 && first_calls(tr->logged_large, 12))) {
Expand Down
16 changes: 11 additions & 5 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "startup_stubs/draw_dump_tracking.h"
#include "startup_stubs/translated_sampler_bindings.h"
#include "startup_stubs/export_write_mask.h"
#include "startup_stubs/depth_state_apply.h"
#include "startup_stubs/heap_indexed_root.h"
#include "startup_stubs/bc7_4k_s.h"
#include "startup_stubs/bc64_chain.h"
Expand Down Expand Up @@ -1070,7 +1071,7 @@ HRESULT __stdcall device_residency(Wrapper* self, UINT count, void* const* objec
// Everything else on a list is still dropped.
void release_targets(Wrapper* self);
void end_open_queries(Wrapper* self, ID3D12GraphicsCommandList* list);
void reset_viewport_state(Wrapper* self);
void reset_list_dynamic_state(Wrapper* self);
void discard_draw_sampler_heaps(ID3D12CommandList* list);
void use_as_target(Wrapper* self, ID3D12Resource* resource);
bool is_container(const void* p);
Expand Down Expand Up @@ -1348,7 +1349,7 @@ HRESULT __stdcall list_reset(Wrapper* self, void* allocator)
}
ReleaseSRWLockExclusive(&allocator_lock);
if (SUCCEEDED(hr)) {
reset_viewport_state(self);
reset_list_dynamic_state(self);
discard_draw_sampler_heaps(previous);
if (draw_dump_frame()) draw_dumps().discard(previous);
}
Expand Down Expand Up @@ -1884,6 +1885,7 @@ struct Pass {
D3D12_INDEX_BUFFER_VIEW ib = {};
DXGI_FORMAT target_formats[8] = {};
DXGI_FORMAT dsv_format = DXGI_FORMAT_UNKNOWN;
UINT stencil_reference = 0; // last guest OMSetStencilRef on this recording
UINT target_count = 0;
ID3D12Resource* target0 = nullptr; // desktop resource of render target 0 (for per-draw dumps)
bool heap_set = false;
Expand Down Expand Up @@ -1949,10 +1951,11 @@ void release_targets(Wrapper* self)
pass = Pass{};
}

// A reset list starts without a viewport or scissor, whatever the previous recording set.
void reset_viewport_state(Wrapper* self)
// A reset list has no viewport or scissor and uses the default stencil reference.
void reset_list_dynamic_state(Wrapper* self)
{
if (!self->own().pass) return;
pass_of(self).stencil_reference = 0;
pass_of(self).viewport_set = false;
pass_of(self).scissor_set = false;
pass_of(self).default_viewport_w = pass_of(self).default_scissor_w = 0;
Expand Down Expand Up @@ -2450,7 +2453,10 @@ void __stdcall list_blend_factor(Wrapper* self, const float* factor)
// List slot 24, OMSetStencilRef(reference): list state that the stencil test of the next draws compares against.
void __stdcall list_stencil_ref(Wrapper* self, UINT reference)
{
if (auto* list = recording(self)) list->OMSetStencilRef(reference);
if (auto* list = recording(self)) {
pass_of(self).stencil_reference = reference;
list->OMSetStencilRef(reference);
}
}
// List slot 44 (desktop 40 + 4), SetGraphicsRootShaderResourceView(parameter, title GPU address).
void __stdcall list_root_srv(Wrapper* self, UINT parameter, u64 address)
Expand Down
Loading
Loading