Skip to content
Closed
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
21 changes: 21 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,27 @@ foreach(mutation IN ITEMS overflow typeless samples)
set_tests_properties(startup_list_copy_validation_${mutation}_control PROPERTIES WILL_FAIL TRUE)
endforeach()

# ClearDepthStencilView (list slot 51) on a depth view, read back through both planes.
add_executable(startup_depth_clear tests/startup_depth_clear.cpp)
target_compile_features(startup_depth_clear PRIVATE cxx_std_17)
target_link_libraries(startup_depth_clear PRIVATE d3d12_xs_shim d3d12 dxgi)
foreach(backend IN ITEMS warp hardware)
add_test(NAME startup_depth_clear_${backend} COMMAND startup_depth_clear ${backend})
set_tests_properties(startup_depth_clear_${backend} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77
ENVIRONMENT "X2W_D3D12_PERMISSIVE=1;X2W_D3D12_LIST_NOOP=1;X2W_D3D12_RENDER=1;X2W_D3D12_DEBUG=1")
endforeach()

# Which ClearDepthStencilView calls are forwarded (src/startup_stubs/list_clear_depth.h), without a device. The mutated
# runs are the negative controls.
add_executable(list_clear_depth_check tests/list_clear_depth_check.cpp)
target_include_directories(list_clear_depth_check PRIVATE src)
target_compile_features(list_clear_depth_check PRIVATE cxx_std_17)
add_test(NAME list_clear_depth COMMAND list_clear_depth_check)
foreach(mutation IN ITEMS keep-stencil allow-read-only)
add_test(NAME list_clear_depth_${mutation}_control COMMAND list_clear_depth_check --mutate ${mutation})
set_tests_properties(list_clear_depth_${mutation}_control PROPERTIES WILL_FAIL TRUE)
endforeach()

add_executable(startup_nv12_draw tests/startup_nv12_draw.cpp tests/desktop_gpu.cpp)
target_compile_features(startup_nv12_draw PRIVATE cxx_std_17)
target_include_directories(startup_nv12_draw PRIVATE src)
Expand Down
12 changes: 12 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -13261,6 +13261,18 @@ The [MASM fixture](../tests/cpu/switch_fsgsbase_rdpid.asm) exports 9 translated

Not covered by this experiment: host segment-base writes (`wrfsbase`/`wrgsbase`, which require an explicit isolation/restoration profile), cross-thread `FS_BASE`/`GS_BASE` migration, and title CPU code execution.

## Depth stencil clears (command-list slot 51)

`ClearDepthStencilView` had no handler; the title's call reached the `x2w-noop` stand-in and the depth buffer was never
cleared on the desktop list. Slot 51 is derived from the desktop order plus the console shift (47 + 4), not seen in a
title log. The stand-in forwards a clear whose depth is in [0, 1], whose planes the view can write and whose rectangles
can be read, and counts the refusals. See [Depth stencil clears](startup-runtime.md#depth-stencil-clears).

```powershell
cmake --build --preset dev --target startup_depth_clear list_clear_depth_check
ctest --preset dev --no-tests=error -R "^(startup_depth_clear_(warp|hardware)|list_clear_depth.*)$" -V
```

## Render target binds with a single descriptor range

`OMSetRenderTargets` (command-list slot 50) with `RTsSingleHandleToDescriptorRange` true used to be dropped without a
Expand Down
33 changes: 33 additions & 0 deletions docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,39 @@ These slots have not been seen in a title log yet. `CopyTextureRegion` (16) and
`ctest -R '^startup_list_copy_validation'` runs the pure checks on any host; `startup_list_copy_{warp,hardware}` copies,
resolves and reads back through the `x2w_d3d12_list_copy_stats` export.

## Depth stencil clears

Command-list slot 51 is `ClearDepthStencilView(view, flags, depth, stencil, rect count, rects)`. The slot is DERIVED, not
seen in a title log: it is desktop slot 47 plus four, between slots 50 (`OMSetRenderTargets`) and 52
(`ClearRenderTargetView`), which are both observed at desktop plus four. Before this change slot 51 fell through to the
`x2w-noop` stand-in, so a depth buffer that the title cleared was never cleared on the desktop list and a depth test
ran against whatever the new texture held.

The depth value is the fourth argument, so a real call passes it in `xmm3`. The generic noop entry takes integer
registers only and would lose it, so with `X2W_D3D12_LIST_NOOP=1` and render mode the list table points slot 51 at the
handler itself. The handler keeps the per-slot call counts the generic entry would have kept, but a frame trace
and a capture do not see it.

The handler reads the view through its shadow record as slot 50 does, moves the resource to `DEPTH_WRITE` and calls the
desktop `ClearDepthStencilView`. The decision is in `src/startup_stubs/list_clear_depth.h`: unknown flag bits and
the stencil bit of a format without a stencil plane are dropped; no flag left means nothing to clear (not counted); a
cleared depth that is not a number in [0, 1], a cleared plane that the view marks read-only, or rectangles that cannot be
read are refused, because the desktop list would report an error for them. A view that holds nothing adds to the
unresolved count of `x2w_d3d12_render_target_stats`; the other refusals add to the second counter of
`x2w_d3d12_depth_clear_stats(forwarded, refused)`. Each refusal logs one line (the first six); the first four calls log
their arguments. A forwarded clear also adds to `clears` in the `x2w-render totals` line. A closed list ignores the call.

One limit: a resource that an earlier bind in the same recording holds as `DEPTH_READ` (a read-only view) is moved to
`DEPTH_WRITE` by a clear through another view of it, and a later draw with the read-only view still bound would then
find the wrong state. No title call shows that pattern yet.

`list_clear_depth_check` runs the decision without a device; its two mutated runs (a stencil clear kept for a format
without stencil, a read-only plane cleared) must fail. `startup_depth_clear_{warp,hardware}` clears a 4x4
`D32_FLOAT_S8X24_UINT` texture through the stand-in and reads both planes back: both planes, depth only (the stencil
argument ignored), stencil only (the depth argument ignored), one rectangle, then the refusals and the closed list. It
does not draw. Before the first clear the planes are read once, and the check fails if they already hold what the first
clear writes.

## Composite provenance fixture

Issue #1628 asks whether a uniform colour left in a composite's destination came from the composite or was
Expand Down
108 changes: 93 additions & 15 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "console_plane_size.h"
#include "descriptor_heap_type.h"
#include "small_texture_4k.h"
#include "list_clear_depth.h"
#include "null_binding_cause.h"
#include "null_texture_view.h"
#include "packed_target_format.h"
Expand Down Expand Up @@ -180,7 +181,6 @@ std::vector<DescriptorRange> descriptor_ranges;
// API documents 1,048,576, but a driver may accept fewer (1,000,000 on the GTX 1660 SUPER here), so
// the accepted maximum is measured once and cached rather than assumed.
constexpr std::uint64_t view_heap_probe_floor = 65536;

void log(const char* format, ...);
bool first_calls(LONG& counter, LONG limit);
void request_translation(Wrapper* pipeline);
Expand Down Expand Up @@ -306,6 +306,21 @@ struct Timed {
InterlockedIncrement64(&time_calls[category]);
}
};
// Per-slot tick, call and census counters of a command-list slot, recorded when it goes out of scope (slot < 0: none).
struct ListSlotTimed {
int slot;
LARGE_INTEGER start;
explicit ListSlotTimed(int n) : slot(n) { QueryPerformanceCounter(&start); }
~ListSlotTimed()
{
if (slot < 0) return;
LARGE_INTEGER end;
QueryPerformanceCounter(&end);
InterlockedAdd64(&list_slot_ticks[slot], end.QuadPart - start.QuadPart);
InterlockedIncrement64(&list_slot_calls[slot]);
InterlockedIncrement64(&list_slot_census[slot]);
}
};

void log(const char* format, ...)
{
Expand Down Expand Up @@ -520,20 +535,7 @@ template <int K, int N>
__declspec(noinline) u64 noop_slot(u64 rcx, u64 rdx, u64 r8, u64 r9, u64 s0, u64 s1, u64 s2, u64 s3)
{
Timed timed(K == list_kind ? time_list : K == queue_kind ? time_queue : time_device_noop);
struct SlotTimed {
LARGE_INTEGER start;
SlotTimed() { QueryPerformanceCounter(&start); }
~SlotTimed()
{
LARGE_INTEGER end;
QueryPerformanceCounter(&end);
if (K == list_kind) {
InterlockedAdd64(&list_slot_ticks[N & 255], end.QuadPart - start.QuadPart);
InterlockedIncrement64(&list_slot_calls[N & 255]);
InterlockedIncrement64(&list_slot_census[N & 255]);
}
}
} slot_timed;
ListSlotTimed slot_timed(K == list_kind ? N & 255 : -1);
if (K == list_kind) InterlockedIncrement(&draw_calls_seen);
if (K == list_kind && capture_active) {
const u64 stack[4] = {s0, s1, s2, s3};
Expand Down Expand Up @@ -1175,6 +1177,8 @@ x2w_startup::DrawReadbacks& draw_dumps()
LONG render_resets, render_closes, render_clears, render_executes, render_reset_failures;
// OMSetRenderTargets calls that bound targets, and render target / depth handles that resolved to no view.
LONG render_target_binds, render_target_unresolved;
// ClearDepthStencilView calls forwarded to the desktop list, and calls refused by rule (not counting an unresolved view).
LONG render_depth_clears, render_depth_clears_refused;

// A console recording survives its allocator's Reset; a desktop one is discarded with it, and
// resetting an allocator whose lists the GPU still runs is invalid. Each title allocator therefore
Expand Down Expand Up @@ -1358,6 +1362,14 @@ extern "C" __declspec(dllexport) void x2w_d3d12_render_target_stats(unsigned* bi
if (unresolved) *unresolved = static_cast<unsigned>(InterlockedCompareExchange(&render_target_unresolved, 0, 0));
}

// Fixtures: ClearDepthStencilView calls forwarded, and calls refused for a read-only plane, a depth outside [0, 1] or
// unreadable rectangles. A view that holds nothing is counted by x2w_d3d12_render_target_stats instead.
extern "C" __declspec(dllexport) void x2w_d3d12_depth_clear_stats(unsigned* forwarded, unsigned* refused)
{
if (forwarded) *forwarded = static_cast<unsigned>(InterlockedCompareExchange(&render_depth_clears, 0, 0));
if (refused) *refused = static_cast<unsigned>(InterlockedCompareExchange(&render_depth_clears_refused, 0, 0));
}

// Fixtures: the number of desktop allocators a title allocator stands for (0 if it has none yet).
extern "C" __declspec(dllexport) unsigned x2w_d3d12_allocator_pool_size(void* allocator)
{
Expand Down Expand Up @@ -2558,6 +2570,70 @@ void __stdcall list_clear_target(Wrapper* self, u64 handle, const float* colour,
red_producers().after(self, red_sample);
InterlockedIncrement(&render_clears);
}
static_assert(x2w_clear_depth::clear_depth == D3D12_CLEAR_FLAG_DEPTH && x2w_clear_depth::clear_stencil == D3D12_CLEAR_FLAG_STENCIL,
"the clear flag bits of list_clear_depth.h are the SDK values");
// List slot 51 (desktop 47 + 4), ClearDepthStencilView(view, flags, depth, stencil, rect count, rects). DERIVED, not
// seen in a title log: slots 50 and 52 around it are observed at desktop + 4. The depth value is the fourth argument,
// so it arrives in xmm3 and the generic noop_slot entry would lose it; the table points at this function directly
// (see where the list table is filled) and it keeps the per-slot counts noop_slot would have kept.
// The view is read from its shadow record, as OMSetRenderTargets does. Desktop resources rest in COMMON, so the
// resource is moved to DEPTH_WRITE first. A view that holds nothing is counted with the unresolved handles of
// OMSetRenderTargets; a clear the desktop list would reject (read-only plane, depth outside [0, 1], unreadable
// rectangles) is refused and counted in x2w_d3d12_depth_clear_stats. See startup_stubs/list_clear_depth.h.
void __stdcall list_clear_depth(Wrapper* self, u64 handle, UINT flags, float depth, UINT stencil, UINT rect_count,
const D3D12_RECT* rects)
{
// The table calls this entry directly, so it keeps the timing noop_slot would have recorded for slot 51.
Timed timed(time_list);
ListSlotTimed slot_timed(51);
InterlockedIncrement(&draw_calls_seen);
auto* list = recording(self);
if (!list) return;
const u64 desktop = desktop_cpu_handle(handle);
const ResolvedView view = desktop ? resolve_view(desktop, 21) : ResolvedView{};
ID3D12Resource* target = view.resource;
static LONG logged;
if (first_calls(logged, 4))
log("x2w-render ClearDepthStencilView view=0x%llx target=%p flags=0x%x depth=%.3f stencil=0x%x rects=%u", handle,
static_cast<void*>(target), flags, depth, stencil & 0xffu, rect_count);
if (!target) {
InterlockedIncrement(&render_target_unresolved);
static LONG refused;
if (first_calls(refused, 6))
log("x2w-d3d12 ClearDepthStencilView skipped: view 0x%llx is %s", handle,
view.wrong_kind ? "not a depth stencil view"
: view.skipped ? "a skipped depth stencil view" : "not a known depth stencil view");
return;
}
x2w_clear_depth::Request request;
request.flags = flags;
request.depth = depth;
request.format_has_stencil = format_has_stencil(view_format_of(desktop, target));
request.depth_read_only = (view.dsv_flags & D3D12_DSV_FLAG_READ_ONLY_DEPTH) != 0;
request.stencil_read_only = (view.dsv_flags & D3D12_DSV_FLAG_READ_ONLY_STENCIL) != 0;
request.rect_count = rect_count;
request.rects_readable = !rect_count || (rects && x2w_local::readable(rects, std::size_t{rect_count} * sizeof(D3D12_RECT)));
const x2w_clear_depth::Plan plan = x2w_clear_depth::plan(request);
if (plan.verdict == x2w_clear_depth::Verdict::nothing_to_clear) return;
if (plan.verdict != x2w_clear_depth::Verdict::forward) {
InterlockedIncrement(&render_depth_clears_refused);
static LONG refused_by_rule;
if (first_calls(refused_by_rule, 6))
log("x2w-d3d12 ClearDepthStencilView refused: %s (flags 0x%x, depth %.3f)", x2w_clear_depth::reason(plan.verdict), flags, depth);
return;
}
// The clear needs DEPTH_WRITE; a texture still bound through a read-only view goes back to its read state after it.
D3D12_RESOURCE_STATES bound_read = D3D12_RESOURCE_STATE_COMMON;
for (const auto& known : pass_of(self).states)
if (known.first == target && (known.second & D3D12_RESOURCE_STATE_DEPTH_READ)) bound_read = known.second;
require_state(self, target, D3D12_RESOURCE_STATE_DEPTH_WRITE);
Comment thread
tedin7 marked this conversation as resolved.
Resolved
list->ClearDepthStencilView(D3D12_CPU_DESCRIPTOR_HANDLE{static_cast<SIZE_T>(desktop)},
static_cast<D3D12_CLEAR_FLAGS>(plan.flags), depth, static_cast<UINT8>(stencil), plan.rect_count,
plan.rect_count ? rects : nullptr);
if (bound_read != D3D12_RESOURCE_STATE_COMMON) require_state(self, target, bound_read);
InterlockedIncrement(&render_depth_clears);
InterlockedIncrement(&render_clears);
}
// List slot 50 (desktop 46 + 4), OMSetRenderTargets(count, handles, single range, depth).
// With single_range the title passes the first handle only and the rest follow it at the title's own
// descriptor stride (also for one target). A descriptor of the wrong kind, a handle that holds no view, a
Expand Down Expand Up @@ -4255,6 +4331,8 @@ void build_tables()
list_typed[30] = reinterpret_cast<ListTyped>(&list_resolve_query);
}
}
// Slot 51 takes a float (xmm3), which the generic entry cannot pass on: the table calls the handler itself.
if (render_mode() && list_noop()) tables[list_kind][51] = reinterpret_cast<void*>(&list_clear_depth);
tables[device_kind][16] = reinterpret_cast<void*>(&device_create_root_signature);

// Queue: not the desktop order (slot 18 is GetTimestampFrequency, desktop 16). Slots are
Expand Down
77 changes: 77 additions & 0 deletions src/startup_stubs/list_clear_depth.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// What a title's ClearDepthStencilView (command-list slot 51, desktop 47 + 4) may forward to the desktop list.
// Plain C++ with no Windows types, so the decision is checked without a device (tests/list_clear_depth_check.cpp).
//
// The desktop call is only valid for a plane the view can write, a depth value in [0, 1] when depth is cleared, and
// a rectangle array that can be read. Anything else would make the debug layer report an error or remove the
// device, so it is refused here and counted by the caller.
#pragma once

#include <cmath>

namespace x2w_clear_depth {

// D3D12_CLEAR_FLAG_DEPTH and D3D12_CLEAR_FLAG_STENCIL; the caller checks the values against the SDK.
constexpr unsigned clear_depth = 1;
constexpr unsigned clear_stencil = 2;

enum class Verdict {
forward, // call the desktop list with Plan::flags and Plan::rect_count
nothing_to_clear, // no plane left to clear after normalising: not an error, not forwarded
bad_depth, // depth is cleared and the value is not a number in [0, 1]
read_only_plane, // a plane to clear is read-only in this view
unreadable_rects, // rects were announced but cannot be read
};

struct Request {
unsigned flags = 0;
float depth = 0.0f;
bool format_has_stencil = false;
bool depth_read_only = false;
bool stencil_read_only = false;
unsigned rect_count = 0;
bool rects_readable = true; // meaningful only when rect_count is not zero
};

struct Plan {
Verdict verdict = Verdict::nothing_to_clear;
unsigned flags = 0;
unsigned rect_count = 0;
};

inline Plan plan(const Request& request)
{
Plan out;
unsigned flags = request.flags & (clear_depth | clear_stencil);
if (!request.format_has_stencil) flags &= ~clear_stencil;
if (!flags) return out;
if ((flags & clear_depth) && !(request.depth >= 0.0f && request.depth <= 1.0f)) { // false for NaN
out.verdict = Verdict::bad_depth;
return out;
}
if (((flags & clear_depth) && request.depth_read_only) || ((flags & clear_stencil) && request.stencil_read_only)) {
out.verdict = Verdict::read_only_plane;
return out;
}
if (request.rect_count && !request.rects_readable) {
out.verdict = Verdict::unreadable_rects;
return out;
}
out.verdict = Verdict::forward;
out.flags = flags;
out.rect_count = request.rect_count;
return out;
}

inline const char* reason(Verdict verdict)
{
switch (verdict) {
case Verdict::forward: return "forwarded";
case Verdict::nothing_to_clear: return "no plane to clear";
case Verdict::bad_depth: return "depth is not a number in [0, 1]";
case Verdict::read_only_plane: return "a plane to clear is read-only in the view";
case Verdict::unreadable_rects: return "the rectangles cannot be read";
}
return "unknown";
}

} // namespace x2w_clear_depth
Loading
Loading