Skip to content
Open
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
7 changes: 6 additions & 1 deletion runtime/shims/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3394,8 +3394,11 @@ void apply_deferred_waits(ListProxy* self) {

// write64 with flag 1 (the GPU's timestamp) or 2 (the system's): the GPU takes its timestamp here, at the write's
// place in the list (the title's GPU timing reads these back: a begin/end pair must be its work's, not one landing
// time). Without the device's timestamp heap the write lands the CPU's clock, as before.
// time). Without the device's timestamp heap the write lands the CPU's clock, as before. So does a write on a COPY
// list: the device's heap is a TIMESTAMP one, and a copy list needs a COPY_QUEUE_TIMESTAMP heap (the queue would
// refuse the submission).
void take_timestamp(ListProxy* self, list_values::Write& write) {
if (self->real->GetType() == D3D12_COMMAND_LIST_TYPE_COPY) return;
DeviceProxy* device = self->device;
std::shared_ptr<list_values::Timestamps> stamps;
{
Expand Down Expand Up @@ -3873,6 +3876,8 @@ void WINAPI list_om_set_depth_bounds(ListProxy* self, FLOAT min, FLOAT max) {
using Fn = void(WINAPI*)(IUnknown*, FLOAT, FLOAT);
auto** const real_vtable = *reinterpret_cast<void***>(self->real);
reinterpret_cast<Fn>(real_vtable[kPcOMSetDepthBounds])(self->real, min, max);
// Kept with the rest of the list's state: a list cut replays it, and a bundle's stays set in the list running it.
if (self->track_state) self->state.depth_bounds(min, max);
}

template <std::size_t... I>
Expand Down
14 changes: 14 additions & 0 deletions runtime/shims/d3d12_xs_list_values.h
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,12 @@ class ListState {
stencil_ = ref;
set_ |= kStencil;
}
// OMSetDepthBounds (ID3D12GraphicsCommandList1, PC slot 62), recorded only once the real list took it.
void depth_bounds(FLOAT min, FLOAT max) {
depth_bounds_[0] = min;
depth_bounds_[1] = max;
set_ |= kDepthBounds;
}
void blend_factor(const FLOAT* factor) {
set_ |= kBlend;
blend_null_ = !factor;
Expand Down Expand Up @@ -1272,6 +1278,7 @@ class ListState {
if (bundle.set_ & kTopology) topology(bundle.topology_);
if (bundle.set_ & kStencil) stencil_ref(bundle.stencil_);
if (bundle.set_ & kBlend) blend_factor(bundle.blend_null_ ? nullptr : bundle.blend_);
if (bundle.set_ & kDepthBounds) depth_bounds(bundle.depth_bounds_[0], bundle.depth_bounds_[1]);
if (bundle.set_ & kIndexBuffer) index_buffer(bundle.ib_null_ ? nullptr : &bundle.ib_);
for (UINT i = 0; i < kMaxVertexBuffers; ++i)
if (bundle.vb_mask_ >> i & 1) vertex_buffers(i, 1, &bundle.vbs_[i]);
Expand Down Expand Up @@ -1353,6 +1360,11 @@ class ListState {
if (scissor_count_) list->RSSetScissorRects(scissor_count_, scissors_);
if (set_ & kBlend) list->OMSetBlendFactor(blend_null_ ? nullptr : blend_);
if (set_ & kStencil) list->OMSetStencilRef(static_cast<UINT>(stencil_));
if (set_ & kDepthBounds) {
// ID3D12GraphicsCommandList1::OMSetDepthBounds, slot 62 of the real list (which took it before the cut).
using Fn = void(WINAPI*)(ID3D12GraphicsCommandList*, FLOAT, FLOAT);
reinterpret_cast<Fn>((*reinterpret_cast<void***>(list))[62])(list, depth_bounds_[0], depth_bounds_[1]);
}
if (set_ & kIndexBuffer) list->IASetIndexBuffer(ib_null_ ? nullptr : &ib_);
for (UINT i = 0; i < kMaxVertexBuffers; ++i)
if (vb_mask_ >> i & 1) list->IASetVertexBuffers(i, 1, &vbs_[i]);
Expand All @@ -1374,6 +1386,7 @@ class ListState {
static constexpr UINT kMaxSoTargets = 4;
enum : std::uint32_t {
kPipeline = 1, kTopology = 2, kStencil = 4, kBlend = 8, kIndexBuffer = 16, kTargets = 32, kPredication = 64,
kDepthBounds = 128,
};
struct Side {
bool signature_set = false;
Expand All @@ -1398,6 +1411,7 @@ class ListState {
std::uint32_t set_ = 0;
std::uint64_t pso_ = 0, topology_ = 0, stencil_ = 0;
FLOAT blend_[4] = {};
FLOAT depth_bounds_[2] = {};
bool blend_null_ = false, ib_null_ = false, rt_has_dsv_ = false;
BOOL rt_single_ = FALSE;
UINT viewport_count_ = 0, scissor_count_ = 0, heap_count_ = 0, rt_count_ = 0;
Expand Down
95 changes: 95 additions & 0 deletions runtime/test/xs_proxy_mock_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ struct Mock {
D3D12_CLEAR_FLAGS clear_dsv_flags;
FLOAT clear_dsv_depth;
UINT8 clear_dsv_stencil;
unsigned list_type; // list: what GetType (slot 8) answers, the type it was created with
FLOAT depth_bounds[2]; // list slot 62 (OMSetDepthBounds): typed float-ABI capture
UINT clear_dsv_num_rects;
const D3D12_RECT* clear_dsv_rects;
D3D_FEATURE_LEVEL max_level; // device: the highest level FEATURE_LEVELS reports (0: the query is refused)
Expand Down Expand Up @@ -577,6 +579,7 @@ std::uint64_t mock_call(Mock* m, unsigned slot, const std::uint64_t* words) {
m->om_dsv = dsv ? dsv->ptr : 0;
}
if (m->kind == Kind::list || m->kind == Kind::command_list) {
if (slot == 8) return m->list_type; // GetType
if (slot == 9) return static_cast<std::uint32_t>(m->list_error ? E_INVALIDARG : S_OK); // Close
if (slot == 48 && mock_is_null_rtv(static_cast<SIZE_T>(a1))) m->list_error = true; // ClearRenderTargetView
if (slot == 80) mock_barrier(m, a1, a2); // Barrier
Expand Down Expand Up @@ -630,6 +633,13 @@ void WINAPI mock_clear_depth_stencil_view(Mock* self, D3D12_CPU_DESCRIPTOR_HANDL
self->clear_dsv_rects = rects;
}

// PC ID3D12GraphicsCommandList1::OMSetDepthBounds: its FLOAT arguments arrive in XMM1 and XMM2.
void WINAPI mock_om_set_depth_bounds(Mock* self, FLOAT min, FLOAT max) {
++self->calls[62];
self->depth_bounds[0] = min;
self->depth_bounds[1] = max;
}

// PC ID3D12Device::CreateCommandList takes six words after `this`, one more than the generic mock thunk declares, so
// it gets its own entry in the table: the out pointer is a6 (first stack argument on x64) and has to be written.
HRESULT WINAPI mock_create_command_list(Mock* self, std::uint64_t node_mask, std::uint64_t type,
Expand All @@ -648,6 +658,7 @@ HRESULT WINAPI mock_create_command_list(Mock* self, std::uint64_t node_mask, std
if (self->create_null || FAILED(self->create_result)) return self->create_result;
Mock* child = new_mock(self->full_chain_lists ? Kind::list : Kind::command_list);
child->list_level = self->list_level;
child->list_type = static_cast<unsigned>(type);
self->last_child = child;
return mock_query(child, riid, out);
}
Expand Down Expand Up @@ -765,6 +776,7 @@ void** mock_list_vtable() {
static const bool filled = [] {
fill_mock_vtable(table, std::make_index_sequence<kMockSlots>{});
table[47] = reinterpret_cast<void*>(&mock_clear_depth_stencil_view);
table[62] = reinterpret_cast<void*>(&mock_om_set_depth_bounds);
return true;
}();
(void)filled;
Expand Down Expand Up @@ -4003,6 +4015,88 @@ std::uint64_t submit(void* queue, void* list) {
return raw(queue, 35, 1, word(lists));
}

// ---- List values: no GPU timestamp on a COPY list; depth bounds survive a cut (RT-XS-LIST-VALUES-COPY-...) ----
// 1. A write64 timestamp (flag 1) on a COPY list records no TIMESTAMP query there (a COPY list needs a
// COPY_QUEUE_TIMESTAMP heap, and the queue would refuse the submission): it lands the CPU's clock. A DIRECT list
// still takes the GPU's.
// 2. OMSetDepthBoundsX (console slot 70) is replayed on the spare list a wait cuts to, and a bundle's stays set in the
// list that executes it, across a cut there too.
int check_list_values_copy_and_depth_bounds() {
Mock* mock = nullptr;
void* device = make_device(mock, 9, true, true, true);
EXPECT(device);
mock->zero_fences = true;
xs::reset_list_value_stats();
xs::set_list_value_wait_timeout_ms(5000);
const IID allocator_iid = __uuidof(ID3D12CommandAllocator);
const IID gcl_iid = __uuidof(ID3D12GraphicsCommandList);
void* allocator = nullptr;
EXPECT(hr_of(raw(device, 9, 0, word(&allocator_iid), word(&allocator))) == S_OK);
alignas(64) static std::uint64_t cells[3];
std::memset(cells, 0, sizeof(cells));

// 1.
void* copy = nullptr;
EXPECT(hr_of(raw(device, 12, 0, D3D12_COMMAND_LIST_TYPE_COPY, word(allocator), 0, word(&gcl_iid),
word(&copy))) == S_OK);
Mock* const copy_real = mock->last_child;
EXPECT(copy_real->list_type == D3D12_COMMAND_LIST_TYPE_COPY);
raw(copy, 72, word(&cells[0]), 0, 0, 1); // write64 flag 1: a GPU timestamp
EXPECT(copy_real->calls[53] == 0 && copy_real->calls[54] == 0); // no EndQuery, no ResolveQueryData
EXPECT(hr_of(raw(copy, 9)) == S_OK);
Mock* direct_real = nullptr;
void* direct = make_console_list(device, allocator, mock, &direct_real);
EXPECT(direct);
raw(direct, 72, word(&cells[1]), 0, 0, 1);
EXPECT(direct_real->calls[53] == 1 && direct_real->calls[54] == 1 &&
std::uint32_t(direct_real->args[53][1]) == D3D12_QUERY_TYPE_TIMESTAMP);
EXPECT(hr_of(raw(direct, 9)) == S_OK);

// 2. Depth bounds set on the list, then work and an unmet wait: the cut's spare list gets them again.
using DepthBoundsFn = void(WINAPI*)(void*, FLOAT, FLOAT);
const auto depth_bounds = [](void* list, FLOAT min, FLOAT max) {
reinterpret_cast<DepthBoundsFn>((*reinterpret_cast<void* const* const*>(list))[70])(list, min, max);
};
EXPECT(hr_of(raw(direct, 139, word(allocator), 0)) == S_OK);
depth_bounds(direct, 0.25f, 0.75f);
EXPECT(direct_real->calls[62] == 1 && direct_real->depth_bounds[0] == 0.25f && direct_real->depth_bounds[1] == 0.75f);
raw(direct, 14, 1, 1, 1);
const unsigned creates = mock->calls[12];
raw(direct, 74, word(&cells[2]), 7, 1, 1); // unmet, after work: a cut
EXPECT(xs::list_proxy_part_count(direct) == 2 && mock->calls[12] == creates + 1);
Mock* const spare = mock->last_child;
EXPECT(spare->calls[62] == 1 && spare->depth_bounds[0] == 0.25f && spare->depth_bounds[1] == 0.75f);
EXPECT(hr_of(raw(direct, 9)) == S_OK);

// A Reset forgets them: the next cut does not set them.
EXPECT(hr_of(raw(direct, 139, word(allocator), 0)) == S_OK);
raw(direct, 14, 1, 1, 1);
const unsigned spare_bounds = spare->calls[62];
raw(direct, 74, word(&cells[2]), 7, 1, 1);
EXPECT(xs::list_proxy_part_count(direct) == 2 && spare->calls[62] == spare_bounds);
EXPECT(hr_of(raw(direct, 9)) == S_OK);

// A bundle's depth bounds stay set in the list that executes it, and its next cut replays them.
void* bundle = nullptr;
EXPECT(hr_of(raw(device, 12, 0, D3D12_COMMAND_LIST_TYPE_BUNDLE, word(allocator), 0, word(&gcl_iid),
word(&bundle))) == S_OK);
depth_bounds(bundle, 0.125f, 0.5f);
EXPECT(hr_of(raw(bundle, 9)) == S_OK);
EXPECT(hr_of(raw(direct, 139, word(allocator), 0)) == S_OK);
raw(direct, 31, word(bundle)); // ExecuteBundle
raw(direct, 14, 1, 1, 1);
raw(direct, 74, word(&cells[2]), 7, 1, 1);
EXPECT(xs::list_proxy_part_count(direct) == 2 && spare->calls[62] == spare_bounds + 1 &&
spare->depth_bounds[0] == 0.125f && spare->depth_bounds[1] == 0.5f);
EXPECT(hr_of(raw(direct, 9)) == S_OK);

EXPECT(release(bundle) == 0 && release(copy) == 0 && release(direct) == 0 && release(allocator) == 0);
EXPECT(release(device) == 0);
std::printf("%s: a timestamp write on a COPY list records no TIMESTAMP query; depth bounds, the list's own or a "
"bundle's, are replayed on the list a wait cuts to\n", g_label);
return 0;
}

int check_list_values() {
// The slot decode: newer layout (and an undetected one) 71-74, older 69-72.
static_assert(xs::console_value_op(71, 0) == xs::ConsoleValueOp::write32);
Expand Down Expand Up @@ -5403,6 +5497,7 @@ int main(int argc, char** argv) {
return 1;
if (check_placed_resource_x() || check_depth_provider_refusal()) return 1;
if (check_list_values()) return 1; // last: it starts the list-values threads
if (check_list_values_copy_and_depth_bounds()) return 1;
if (check_list_values_more()) return 1; // and this one stops them
std::printf("xs_proxy_mock_test: PASS (%d allocations refused on purpose)\n", g_failed_news);
return 0;
Expand Down
Loading