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: 3 additions & 4 deletions runtime/shims/xbox_present.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,9 @@ HRESULT XboxPresentation::present_x(UINT plane_count, const XboxPresentPlanePara

if (plane_count == 0) {
const HRESULT result = presenter_->present_blank(sync_interval);
if (SUCCEEDED(result)) {
pacer_.note_untokened_present();
++presents_;
}
if (FAILED(result)) return present_diag(__LINE__, pacer_.suspended() ? from_pacer(hr::invalid_state) : result);
pacer_.note_untokened_present();
++presents_;
return present_diag(__LINE__, result);
}

Expand Down
40 changes: 17 additions & 23 deletions runtime/src/vulkan_present.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,15 +389,27 @@ class VulkanPresenter {
// holds back the queue's own work; with nothing held this is the plain path. A blank present reads nothing and
// is only ordered behind earlier presents. Waiting instead was rejected: the Signal that releases the work may
// be one the application's own thread makes after Present returns. Like a full frame-latency queue, a Present that
// finds max_deferred_presents queued ahead of it does wait, for held work to release (logged once; set_abort ends
// the wait), so the queue behind never-released work is bounded. A queued Present reports its tear as false
// (last_present_tore()): the frame pacer's default sync.
// finds max_deferred_presents queued ahead of it waits for held work to release (logged once). If set_abort
// interrupts that wait, the present returns VK_NOT_READY before joining the queue. A queued Present reports its
// tear as false (last_present_tore()): the frame pacer's default sync.
static constexpr std::size_t max_deferred_presents = 8;

VkResult submit(const ResourceBinding* binding, ResourceId source, bool vsync, const PresentRect* source_rect,
bool srgb, bool tear = false, const PresentPlanes* planes = nullptr) {
wait_for_room();
std::lock_guard lock(present_mutex_);
std::unique_lock lock(present_mutex_);
if (deferred_.size() >= max_deferred_presents) {
// Release the lock while waiting so the thread draining the deferred queue can take it.
lock.unlock();
std::fprintf(stderr, "[rt] d3d12: %zu Presents are queued behind held work; this one waits for it\n",
max_deferred_presents);
for (;;) {
if (should_abort()) return VK_NOT_READY;
lock.lock();
if (deferred_.size() < max_deferred_presents) break;
lock.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
// `binding` null and `planes` null: clear instead of blit; `planes`: compose them (`binding`, `source`,
// `source_rect` unused). Only a present that reads a texture waits for held work.
if (deferred_.empty() && !((binding || planes) && context_.holds().any_held()))
Expand Down Expand Up @@ -425,24 +437,6 @@ class VulkanPresenter {
return abort && abort();
}

// Called without present_mutex_: the thread that releases the queued presents needs it.
void wait_for_room() {
bool logged = false;
for (;;) {
{
std::lock_guard lock(present_mutex_);
if (deferred_.size() < max_deferred_presents) return;
}
if (!logged) {
logged = true;
std::fprintf(stderr, "[rt] d3d12: %zu Presents are queued behind held work; this one waits for it\n",
max_deferred_presents);
}
if (should_abort()) return;
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}

struct PendingPresent {
bool has_source;
ResourceId source;
Expand Down
31 changes: 31 additions & 0 deletions runtime/test/vk_presenter_sync_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,37 @@ int main() {
context.remove_resource(2);
}

// A full deferred queue must not accept another present when suspension interrupts the wait.
{
VulkanFence gate(context, 0);
EXPECT(gate.semaphore() != VK_NULL_HANDLE);
CommandList draw;
EXPECT(draw.transition({1, 0, d3d12_state::common, d3d12_state::render_target}));
EXPECT(draw.clear_render_target({1, 0, 0, 1}, {0.9f, 0.1f, 0.2f, 1.0f}));
EXPECT(draw.transition({1, 0, d3d12_state::render_target, d3d12_state::common}));
EXPECT(draw.close());
const CommandList* ptr = &draw;
queue.wait(gate.semaphore(), 1);
EXPECT(queue.execute({&ptr, 1}) == ReplayResult::success);
EXPECT(context.holds().any_held());

constexpr std::uint64_t capacity = 8;
const std::uint64_t shown = presenter.presented();
for (std::uint64_t i = 0; i < capacity; ++i)
EXPECT(presenter.present(1, true) == VK_SUCCESS);
EXPECT(presenter.presented() == shown);

presenter.set_abort([] { return true; });
const VkResult interrupted = presenter.present_blank(true);
presenter.set_abort(nullptr);
EXPECT(gate.signal(1));
EXPECT(queue.idle());
EXPECT(interrupted == VK_NOT_READY);
EXPECT(presenter.presented() == shown + capacity);
EXPECT(presenter.present(1, true) == VK_SUCCESS);
EXPECT(presenter.presented() == shown + capacity + 1);
}

// 2c. A queue destroyed with held work lets the Presents queued behind it run (the work is dropped).
{
VulkanFence never(context, 0);
Expand Down