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
Original file line number Diff line number Diff line change
Expand Up @@ -5193,6 +5193,7 @@ Graphics::Context VulkanDevice::buildContext() const {
context.individuallyFreeDescriptorSets = state->individuallyFreeDescriptorSets;
context.detileWordGroupsFaulty = state->detileWordGroupsFaulty;
context.hostImportsInHeapUsage = Graphics::HostImportsInHeapUsage(state->driverId);
context.queryBatchesNeedFence = Graphics::QueryBatchesNeedFence(state->driverId);
context.tessellationPipelineCache = state->tessellationPipelineCache;
context.orderedInterlockEpilogs = state->orderedInterlockEpilogs;
context.hostPointerImport = state->hostPointerImport;
Expand Down
1 change: 1 addition & 0 deletions core/libs/prx/libSceAgcDriver/Graphics/include/Context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ struct Context {
bool individuallyFreeDescriptorSets = false;
bool detileWordGroupsFaulty = false;
bool hostImportsInHeapUsage = false;
bool queryBatchesNeedFence = false;
bool tessellationPipelineCache = true;
bool orderedInterlockEpilogs = false;
bool imageTableUpdateAfterBind = false;
Expand Down
5 changes: 5 additions & 0 deletions core/libs/prx/libSceAgcDriver/Graphics/include/Recorder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@

namespace AgcDriver::Graphics {

inline bool QueryBatchesNeedFence(VkDriverId driver) {
return driver == VK_DRIVER_ID_MOLTENVK;
}

class Buffer;
class DeviceBuffer;

Expand Down Expand Up @@ -1043,6 +1047,7 @@ class Recorder {
}
VkResult fenceStatus(VkFence fence) const { return function(getFenceStatus, "vkGetFenceStatus")(context.device, fence); }
bool gpuDone(const Batch& batch) const;
VkFence takeFence();
std::uint64_t readTimeline() const;
void recordBarrier(VkCommandBuffer commands, VkPipelineStageFlags sourceStage, VkPipelineStageFlags destinationStage, VkAccessFlags sourceAccess, VkAccessFlags destinationAccess) const;
// Opens a batch when none is open, without closing a store run (Commands() does both).
Expand Down
38 changes: 26 additions & 12 deletions core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2265,6 +2265,18 @@ void Recorder::recordKeyStores(bool forWriter) {
if (forWriter) keyStoreRunsForWriter.fetch_add(1, std::memory_order_relaxed);
}

VkFence Recorder::takeFence() {
if (!spare.empty()) {
const auto fence = spare.back();
spare.pop_back();
return fence;
}
VkFenceCreateInfo info{VK_STRUCTURE_TYPE_FENCE_CREATE_INFO};
VkFence fence = VK_NULL_HANDLE;
Check(context.Function<PFN_vkCreateFence>("vkCreateFence")(context.device, &info, nullptr, &fence), "vkCreateFence recorder");
return fence;
}

void Recorder::ensureOpen() {
GuestMemory::AssertGpuLockHeld("Recorder::Commands");
if (open == nullptr) {
Expand All @@ -2277,15 +2289,7 @@ void Recorder::ensureOpen() {
allocation.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocation.commandBufferCount = 1;
Check(context.Function<PFN_vkAllocateCommandBuffers>("vkAllocateCommandBuffers")(context.device, &allocation, &batch->commands), "vkAllocateCommandBuffers recorder");
if (!fenceless) {
if (!spare.empty()) {
batch->fence = spare.back();
spare.pop_back();
} else {
VkFenceCreateInfo info{VK_STRUCTURE_TYPE_FENCE_CREATE_INFO};
Check(context.Function<PFN_vkCreateFence>("vkCreateFence")(context.device, &info, nullptr, &batch->fence), "vkCreateFence recorder");
}
}
if (!fenceless) batch->fence = takeFence();
VkCommandBufferBeginInfo begin{VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO};
begin.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
Check(function(beginCommandBuffer, "vkBeginCommandBuffer")(batch->commands, &begin), "vkBeginCommandBuffer recorder");
Expand Down Expand Up @@ -4490,7 +4494,8 @@ bool Recorder::SubmittedWorkRunning() const {

bool Recorder::gpuDone(const Batch& batch) const {
if (!fenceless) return fenceStatus(batch.fence) == VK_SUCCESS;
return batch.serial != 0 && (batch.serial <= reachedSerial || batch.serial <= readTimeline());
const bool reached = batch.serial != 0 && (batch.serial <= reachedSerial || batch.serial <= readTimeline());
return reached && (batch.fence == VK_NULL_HANDLE || fenceStatus(batch.fence) == VK_SUCCESS);
}

bool Recorder::PendingWriteSettled(std::uint64_t address, std::size_t bytes) const {
Expand Down Expand Up @@ -5076,6 +5081,8 @@ void Recorder::Submit() {
submission.pSignalSemaphores = &timeline;
}
if (hold != VK_NULL_HANDLE || timeline != VK_NULL_HANDLE) submission.pNext = &timelineInfo;
const bool holdsQueries = batch->queries != VK_NULL_HANDLE || batch->samples != VK_NULL_HANDLE || !batch->endedSamples.empty() || batch->segments != VK_NULL_HANDLE;
if (context.queryBatchesNeedFence && batch->fence == VK_NULL_HANDLE && holdsQueries) batch->fence = takeFence();
DestroyDeferredAtIdle(context);
const auto submitStart = std::chrono::steady_clock::now();
batch->submitStarted.store(true, std::memory_order_release);
Expand Down Expand Up @@ -5563,7 +5570,9 @@ bool Recorder::Reap() {
std::exception_ptr failure;
bool draining = validationFailure != nullptr;
while (!inFlight.empty()) {
if (fenceless ? inFlight.front()->serial > reachedSerial : fenceStatus(inFlight.front()->fence) != VK_SUCCESS) break;
const auto& front = *inFlight.front();
if (fenceless && front.serial > reachedSerial) break;
if (front.fence != VK_NULL_HANDLE && fenceStatus(front.fence) != VK_SUCCESS) break;
auto batch = std::move(inFlight.front());
inFlight.pop_front();
try {
Expand Down Expand Up @@ -5659,6 +5668,11 @@ void Recorder::finish(std::unique_ptr<Batch> batch, bool wait, int source, bool
};
const auto waitFences = function(waitForFences, "vkWaitForFences");
auto result = fenceless ? waitTimeline() : waitFences(context.device, 1, &batch->fence, VK_TRUE, 5'000'000'000ull);
const char* failedWait = fenceless ? "vkWaitSemaphoresKHR recorder" : "vkWaitForFences recorder";
if (result == VK_SUCCESS && fenceless && batch->fence != VK_NULL_HANDLE) {
result = waitFences(context.device, 1, &batch->fence, VK_TRUE, 5'000'000'000ull);
failedWait = "vkWaitForFences recorder";
}
for (int waited = 5; result == VK_TIMEOUT; waited += 5) {
// A batch still running after 5 s is reported (every 5 s) so a GPU-side hang is visible.
std::fprintf(stderr, "[gpu] recorded batch still running on the GPU after %d s\n", waited);
Expand All @@ -5670,7 +5684,7 @@ void Recorder::finish(std::unique_ptr<Batch> batch, bool wait, int source, bool
}
if (result != VK_SUCCESS) {
release(*batch);
Check(result, fenceless ? "vkWaitSemaphoresKHR recorder" : "vkWaitForFences recorder");
Check(result, failedWait);
}
}
if (!publishActions || validationFailure != nullptr) {
Expand Down
Loading
Loading