Files
mandy/runtime/test/vk_host_texture_order_test.cpp
Jan Trencansky 5a984952b4 d3d12: title texture uploads wait for earlier work on the GPU, never on the CPU, and are never skipped (RT-TITLE-UPLOAD-GPU-ORDER)
A title texture upload waited on the CPU, with the submit lock held, for all work submitted on every queue, at
most 250 ms, and skipped the write when that work was not done. World frames take longer, so changed textures
(the planes of a video frame among them) arrived late or out of step, and the submitting thread stalled up to
250 ms per upload (the 200-277 ms maxima of the textures phase in world runs).

The staged transfer now waits for that work on the GPU: write_staged_title_image hands it every queue timeline
value submitted so far. Every such value is already submitted, and console waits whose signal is not submitted
live in QueueHolds, so the GPU wait cannot deadlock. The upload slots are reused once their transfer is done
(looked up, not waited for), and new ones are made while all are in flight. Every title fill goes through this
upload, so the host-copy path for title textures (write_host_texture, its bounded wait and
LEONIDA_ORDER_TEXTURE_UPLOADS) goes. As before, the transfer is submitted on the primary queue before the
submission whose before_execute runs the refresh, and the other queues wait for it on the GPU, so that
submission's draws sample the new texels.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 17:22:16 -05:00

68 lines
3.1 KiB
C++

// A title texture upload (VulkanContext::write_staged_title_image) never waits for the GPU on the CPU: it hands the
// transfer every queue timeline value submitted so far, to wait for on the GPU, and keeps other submissions out while
// it reads the layouts and submits. Until 2026-11 it waited on the CPU for that work for at most 250 ms and skipped the
// write when the work was not done.
#include "../src/vulkan_backend.h"
#include <cstdio>
#include <thread>
#include <utility>
#include <vector>
namespace {
unsigned waits = 0;
VkResult VKAPI_CALL wait_semaphores(VkDevice, const VkSemaphoreWaitInfo*, std::uint64_t) {
++waits;
return VK_TIMEOUT;
}
VkResult VKAPI_CALL device_idle(VkDevice) { return VK_ERROR_UNKNOWN; } // must not be called
PFN_vkVoidFunction VKAPI_CALL no_proc(VkDevice, const char*) { return nullptr; }
}
int main() {
leonida::VulkanContext ctx(no_proc, VK_NULL_HANDLE, false);
ctx.vk.vkWaitSemaphores = wait_semaphores;
ctx.vk.vkDeviceWaitIdle = device_idle;
leonida::ResourceBinding binding{};
binding.image = reinterpret_cast<VkImage>(1);
binding.mip_levels = 1;
binding.array_layers = 1;
ctx.add_resource(1, binding, VK_IMAGE_LAYOUT_GENERAL);
const VkImageSubresourceRange range{VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
unsigned copies = 0;
std::vector<std::pair<VkSemaphore, std::uint64_t>> seen;
const auto copy = [&](const leonida::ReplayFunctions&, const leonida::LayoutPieces& pieces, const auto& handed) {
++copies;
seen.assign(handed.begin(), handed.end());
bool raced = false;
std::thread contender([&] {
if (ctx.submit_mutex().try_lock()) {
raced = true;
ctx.submit_mutex().unlock();
}
});
contender.join();
if (raced || pieces.empty() || pieces[0].layout != VK_IMAGE_LAYOUT_GENERAL) return VK_ERROR_UNKNOWN;
return VK_SUCCESS;
};
// Nothing submitted: nothing to wait for.
if (ctx.write_staged_title_image(1, range, copy) != VK_SUCCESS || copies != 1 || !seen.empty()) return 1;
const auto graphics = reinterpret_cast<VkSemaphore>(std::uintptr_t{0x10});
const auto compute = reinterpret_cast<VkSemaphore>(std::uintptr_t{0x20});
{ // work in flight on two queues, which a CPU wait would time out on (wait_semaphores says VK_TIMEOUT)
std::lock_guard lock(ctx.submit_mutex());
ctx.record_submit_locked(graphics, 7);
ctx.record_submit_locked(compute, 3);
}
if (ctx.write_staged_title_image(1, range, copy) != VK_SUCCESS || copies != 2) return 2; // not skipped
if (waits) return 3; // no CPU wait
bool g = false, c = false;
for (const auto& [semaphore, value] : seen) {
g |= semaphore == graphics && value == 7;
c |= semaphore == compute && value == 3;
}
if (!g || !c) return 4; // the transfer waits for both queues' work on the GPU
if (ctx.write_staged_title_image(2, range, copy) != VK_ERROR_UNKNOWN || copies != 2) return 5; // unknown image
std::puts("title texture upload: no CPU wait, no skip, GPU waits for every queue's submitted work, submit exclusion");
}