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
117 changes: 103 additions & 14 deletions runtime/shims/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@
#include <new>
#include <vector>
#include <memory>
#include <mutex>
#include <new>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -1195,8 +1193,72 @@ struct ChildProxy {
// Queues only: the gate and write fences of list memory waits and writes (d3d12_xs_list_values.h), made at the
// first submission that needs them, under queue_order.
std::shared_ptr<list_values::QueueValues> values;
ChildKind kind = ChildKind::allocator;
ChildProxy* live_next = nullptr; // protected by g_live_child_lock; no identity-tracking allocation
};

SRWLOCK g_live_child_lock = SRWLOCK_INIT;
constexpr std::size_t kLiveChildBuckets = 256;
ChildProxy* g_live_children[kLiveChildBuckets]{};

std::size_t live_child_bucket(const void* object) {
const auto address = reinterpret_cast<std::uintptr_t>(object);
return ((address >> 4) ^ (address >> 12)) & (kLiveChildBuckets - 1);
}

void register_live_child(ChildProxy* child) {
AcquireSRWLockExclusive(&g_live_child_lock);
auto& head = g_live_children[live_child_bucket(child)];
child->live_next = head;
head = child;
ReleaseSRWLockExclusive(&g_live_child_lock);
}

void unregister_live_child(ChildProxy* child) {
AcquireSRWLockExclusive(&g_live_child_lock);
for (auto** at = &g_live_children[live_child_bucket(child)]; *at; at = &(*at)->live_next) {
if (*at != child) continue;
*at = child->live_next;
child->live_next = nullptr;
break;
}
ReleaseSRWLockExclusive(&g_live_child_lock);
}

// Identity is the live allocation and its kind, not a candidate's first word. Hold the shared lock while a
// reference is acquired so destruction cannot free the allocation; a zero count must never be resurrected.
template <ChildKind K>
ChildProxy* pin_live_child(const void* candidate) {
ChildProxy* pinned = nullptr;
AcquireSRWLockShared(&g_live_child_lock);
for (auto* child = g_live_children[live_child_bucket(candidate)]; child; child = child->live_next) {
if (child != candidate || child->kind != K) continue;
auto refs = child->refs.load(std::memory_order_relaxed);
while (refs && refs != (std::numeric_limits<unsigned long>::max)()) {
if (child->refs.compare_exchange_weak(refs, refs + 1, std::memory_order_relaxed)) {
pinned = child;
break;
}
}
break;
}
ReleaseSRWLockShared(&g_live_child_lock);
return pinned;
}

template <ChildKind K>
bool is_live_child(const void* candidate) {
bool found = false;
AcquireSRWLockShared(&g_live_child_lock);
for (const auto* child = g_live_children[live_child_bucket(candidate)]; child; child = child->live_next) {
if (child != candidate || child->kind != K) continue;
found = child->refs.load(std::memory_order_relaxed) != 0;
break;
}
ReleaseSRWLockShared(&g_live_child_lock);
return found;
}

template <ChildKind K>
struct ChildStats {
static constexpr unsigned kSlots = K == ChildKind::queue ? kQueueSlots
Expand Down Expand Up @@ -1523,6 +1585,7 @@ std::uint64_t fence_completed_value_address(ChildProxy* self, std::uint64_t valu
// reference keeps the fence alive, the event stays open (one handle per such fence) and a later completion sets
// that event, not a closed or reused handle.
void destroy_child(ChildProxy* self) {
unregister_live_child(self); // no foreign Release callback runs under the registry lock
IUnknown* real = self->real;
DeviceProxy* device = self->device;
FenceMirror* mirror = self->mirror.load(std::memory_order_acquire);
Expand Down Expand Up @@ -1551,6 +1614,12 @@ void destroy_child(ChildProxy* self) {
if (device) proxy_release(device);
}

unsigned long release_child_reference(ChildProxy* self) {
const unsigned long refs = self->refs.fetch_sub(1, std::memory_order_acq_rel) - 1;
if (refs == 0) destroy_child(self);
return refs;
}

template <ChildKind K>
unsigned long child_add_ref(ChildProxy* self) {
note_child_slot<K>(1);
Expand All @@ -1560,9 +1629,7 @@ unsigned long child_add_ref(ChildProxy* self) {
template <ChildKind K>
unsigned long child_release(ChildProxy* self) {
note_child_slot<K>(2);
const unsigned long refs = self->refs.fetch_sub(1, std::memory_order_acq_rel) - 1;
if (refs == 0) destroy_child(self);
return refs;
return release_child_reference(self);
}

// The interfaces the proxy answers: the base chain and the object's own interface.
Expand Down Expand Up @@ -1653,17 +1720,22 @@ IUnknown* list_real_or_null(const void* object);

template <ChildKind K>
bool is_child_proxy(const void* object) {
return object && *reinterpret_cast<void* const*>(object) == reinterpret_cast<void*>(get_child_vtable<K>());
return is_live_child<K>(object);
}

// The real object behind a heap, queue, fence or list proxy, or null when `object` is none of them.
IUnknown* proxy_real_object(const void* object) {
if (!object) return nullptr;
void* const vtable = *reinterpret_cast<void* const*>(object);
if (vtable == reinterpret_cast<void*>(get_heap_vtable())) return static_cast<const HeapProxy*>(object)->real;
if (vtable == reinterpret_cast<void*>(get_child_vtable<ChildKind::queue>()) ||
vtable == reinterpret_cast<void*>(get_child_vtable<ChildKind::fence>()) ||
vtable == reinterpret_cast<void*>(get_child_vtable<ChildKind::allocator>()))
// Native objects take no registry lock or walk. A child-vtable match only selects the one identity lookup;
// matching bytes alone still do not establish that the candidate is a live child proxy.
if ((vtable == reinterpret_cast<void*>(get_child_vtable<ChildKind::queue>()) &&
is_child_proxy<ChildKind::queue>(object)) ||
(vtable == reinterpret_cast<void*>(get_child_vtable<ChildKind::fence>()) &&
is_child_proxy<ChildKind::fence>(object)) ||
(vtable == reinterpret_cast<void*>(get_child_vtable<ChildKind::allocator>()) &&
is_child_proxy<ChildKind::allocator>(object)))
return static_cast<const ChildProxy*>(object)->real;
if (IUnknown* real = list_real_or_null(object)) return real;
return nullptr;
Expand Down Expand Up @@ -2467,6 +2539,20 @@ std::uint64_t WINAPI child_slot(ChildProxy* self, std::uint64_t a1, std::uint64_
return fence_completed_value_address(self, a1, a2, a3);
} else {
note_child_slot<K>(Slot);
// A reentrant Release can drop the caller's last reference while this call is still forwarding.
// The internal references also contribute to AddRef and Release return counts until this call returns.
using Pin = std::unique_ptr<ChildProxy, decltype(&release_child_reference)>;
Pin queue_pin(nullptr, &release_child_reference), fence_pin(nullptr, &release_child_reference);
if constexpr (K == ChildKind::queue) {
queue_pin.reset(pin_live_child<ChildKind::queue>(self));
if (!queue_pin) return hr_to_u64(E_INVALIDARG);
if constexpr (Slot == kQueueSignal || Slot == kQueueWait || Slot == 16 || Slot == 17 ||
Slot == 23 || Slot == 25)
fence_pin.reset(pin_live_child<ChildKind::fence>(reinterpret_cast<const void*>(a1)));
}
const auto fence_argument = [&]() {
return fence_pin ? fence_pin->real : proxy_real_object(reinterpret_cast<const void*>(a1));
};
std::unique_lock<std::mutex> queue_order;
// Both queue-order fixes: only work that orders on the queue takes queue_order. The markers do not, and
// neither does slot 18 - an ExecuteCommandListsX holds it for its whole replay, which can include building a
Expand All @@ -2479,7 +2565,7 @@ std::uint64_t WINAPI child_slot(ChildProxy* self, std::uint64_t a1, std::uint64_
if constexpr (K == ChildKind::queue) {
bool orders = queue_slot_orders_work(Slot);
if (orders && (Slot == 16 || Slot == 17))
orders = proxy_real_object(reinterpret_cast<const void*>(a1)) != nullptr;
orders = fence_argument() != nullptr;
if (orders && (Slot == 19 || Slot == 20)) {
const unsigned char own = self->queue_layout.load(std::memory_order_relaxed);
const bool newer = own == ChildProxy::kQueueLayoutNewer ||
Expand Down Expand Up @@ -2547,7 +2633,7 @@ std::uint64_t WINAPI child_slot(ChildProxy* self, std::uint64_t a1, std::uint64_
return own == ChildProxy::kQueueLayoutUnknown && shared == ChildProxy::kQueueLayoutNewer;
}();
if (Slot == 16 || Slot == 17) {
if (IUnknown* fence = proxy_real_object(reinterpret_cast<const void*>(a1))) {
if (IUnknown* fence = fence_argument()) {
self->queue_layout.store(ChildProxy::kQueueLayoutNewer, std::memory_order_relaxed);
self->device->console_layout.store(ChildProxy::kQueueLayoutNewer,
std::memory_order_relaxed);
Expand All @@ -2563,7 +2649,7 @@ std::uint64_t WINAPI child_slot(ChildProxy* self, std::uint64_t a1, std::uint64_
self->device->console_layout.store(ChildProxy::kQueueLayoutOlder,
std::memory_order_relaxed);
}
} else if ((Slot == 14 || Slot == 15) && !proxy_real_object(reinterpret_cast<const void*>(a1)) &&
} else if ((Slot == 14 || Slot == 15) && !fence_argument() &&
newer) {
return hr_to_u64(S_OK); // the newer layout's two inserted console methods, unmeasured
}
Expand All @@ -2587,7 +2673,7 @@ std::uint64_t WINAPI child_slot(ChildProxy* self, std::uint64_t a1, std::uint64_
// alternative is the fence itself carrying the layout evidence, per the comment above; a
// proven-older queue still leaves slot 25 to the console-only no-op, since its fence-taking
// methods sit at 14/15/23 there.
if (IUnknown* fence = proxy_real_object(reinterpret_cast<const void*>(a1))) {
if (IUnknown* fence = fence_argument()) {
if (Slot == 25u && !newer) {
self->queue_layout.store(ChildProxy::kQueueLayoutNewer,
std::memory_order_relaxed);
Expand Down Expand Up @@ -2687,7 +2773,8 @@ std::uint64_t WINAPI child_slot(ChildProxy* self, std::uint64_t a1, std::uint64_
self->real, reinterpret_cast<UINT64*>(a1)));
}
}
if constexpr (K == ChildKind::queue && (Slot == kQueueSignal || Slot == kQueueWait)) a1 = unwrap_object(a1);
if constexpr (K == ChildKind::queue && (Slot == kQueueSignal || Slot == kQueueWait))
a1 = fence_pin ? reinterpret_cast<std::uint64_t>(fence_pin->real) : unwrap_object(a1);
UnwrappedArray unwrapped_lists;
if constexpr (K == ChildKind::queue && Slot == 10) {
if (!unwrapped_lists.fill(a2, a1)) return hr_to_u64(E_OUTOFMEMORY);
Expand Down Expand Up @@ -2783,7 +2870,9 @@ HRESULT wrap_child(IUnknown* created, DeviceProxy* device, const IID* riid, void
proxy->slot_count = kAllocatorPublicSlots;
}
proxy->refs.store(1, std::memory_order_relaxed);
proxy->kind = K;
proxy_add_ref(device); // the object keeps its Xbox-ABI device alive for GetDevice
register_live_child(proxy); // before QueryInterface publishes the output pointer

const HRESULT hr = child_query_interface<K>(proxy, riid, out);
if (FAILED(hr)) {
Expand Down
30 changes: 27 additions & 3 deletions runtime/test/xs_proxy_mock_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ struct Mock {
Mock* completes_with; // fence: once its completed value reaches completes_at, a GetCompletedValue first
std::uint64_t completes_at, completes_value; // signals that fence to completes_value (GPU order across queues)
bool signals_complete; // queue: a Signal (slot 14) completes the fence at once, as an idle GPU would
const void* observed_queue; // optional proxy pair to sample during a forwarded fence call
const void* observed_fence;
unsigned long observed_queue_refs, observed_fence_refs;
unsigned calls[kMockSlots];
std::uint64_t args[kMockSlots][6];
std::uint64_t array[kMockSlots][4]; // device: the first entries of the array argument, as received
Expand Down Expand Up @@ -581,6 +584,10 @@ std::uint64_t mock_call(Mock* m, unsigned slot, const std::uint64_t* words) {
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
}
if (m->kind == Kind::queue && (slot == 14 || slot == 15) && m->observed_queue) {
m->observed_queue_refs = xs::command_object_proxy_ref_count(m->observed_queue);
m->observed_fence_refs = xs::command_object_proxy_ref_count(m->observed_fence);
}
return S_OK;
}

Expand Down Expand Up @@ -3288,9 +3295,16 @@ int check_tail(bool want_guarded) {
EXPECT(queue && fence);
EXPECT(xs::command_object_tables_guarded() == want_guarded);
Mock* const queue_mock = reinterpret_cast<Mock*>(xs::queue_proxy_real_queue(queue));
// Signal (14) still reaches the real queue, with the real fence.
EXPECT(hr_of(raw(queue, 14, word(fence), 5)) == S_OK && queue_mock->calls[14] == 1 &&
queue_mock->args[14][0] == word(xs::fence_proxy_real_fence(fence)) && queue_mock->args[14][1] == 5);
// Both wrappers keep a reference until the native call returns.
queue_mock->observed_queue = queue;
queue_mock->observed_fence = fence;
for (unsigned slot : {14u, 15u}) {
queue_mock->observed_queue_refs = queue_mock->observed_fence_refs = 0;
EXPECT(hr_of(raw(queue, slot, word(fence), 5)) == S_OK && queue_mock->calls[slot] == 1 &&
queue_mock->args[slot][0] == word(xs::fence_proxy_real_fence(fence)) && queue_mock->args[slot][1] == 5);
EXPECT(queue_mock->observed_queue_refs == 2 && queue_mock->observed_fence_refs == 2);
EXPECT(xs::command_object_proxy_ref_count(queue) == 1 && xs::command_object_proxy_ref_count(fence) == 1);
}
// Console-only queue methods (the tail past the PC chain, SignalX, PresentX) report S_OK instead of
// refusing: frame pacing flows through them. Nothing is forwarded — the real queue never sees them.
for (unsigned slot = 19; slot < xs::queue_proxy_table_size(); ++slot) {
Expand All @@ -3299,6 +3313,16 @@ int check_tail(bool want_guarded) {
}
EXPECT(raw(fence, 12) != 0); // the completed value's address (check_fence_completed_value)
for (unsigned slot = 13; slot < xs::fence_proxy_table_size(); ++slot) EXPECT(hr_of(raw(fence, slot)) == E_NOTIMPL);
for (unsigned slot : {16u, 17u, 25u}) {
const unsigned native_slot = slot == 17u ? 15u : 14u;
const unsigned expected_calls = queue_mock->calls[native_slot] + 1;
queue_mock->observed_queue_refs = queue_mock->observed_fence_refs = 0;
EXPECT(hr_of(raw(queue, slot, word(fence), 6)) == S_OK && queue_mock->calls[native_slot] == expected_calls &&
queue_mock->args[native_slot][0] == word(xs::fence_proxy_real_fence(fence)) &&
queue_mock->args[native_slot][1] == 6);
EXPECT(queue_mock->observed_queue_refs == 2 && queue_mock->observed_fence_refs == 2);
EXPECT(xs::command_object_proxy_ref_count(queue) == 1 && xs::command_object_proxy_ref_count(fence) == 1);
}
EXPECT(xs::queue_table_overruns() == 0 && xs::fence_table_overruns() == 0);
EXPECT(release(queue) == 0 && release(fence) == 0 && release(device) == 0 && mock->refs == 0);
return 0;
Expand Down
Loading