fix eop/flipping

This commit is contained in:
nmzik
2026-07-17 01:50:43 +02:00
parent a96b5111e6
commit bf1ea80bd9
14 changed files with 557 additions and 224 deletions
+6 -2
View File
@@ -4,6 +4,7 @@
#include "common/common.h"
namespace Libs::Graphics {
class CommandBuffer;
struct VideoOutVulkanImage;
} // namespace Libs::Graphics
@@ -17,8 +18,11 @@ struct DisplayBufferImage {
};
DisplayBufferImage DisplayBufferFind(uint64_t addr, bool render_target = false);
int DisplayBufferSubmitFlipFromGpu(int handle, int index, int flip_mode, int64_t flip_arg);
void DisplayBufferCompleteFlipFromGpu(int handle, int index);
int DisplayBufferSubmitFlipFromGpu(Graphics::CommandBuffer* buffer, int handle, int index,
int flip_mode, int64_t flip_arg, uint64_t* request_id);
uint64_t DisplayBufferPrepareNextFlipOnGpu(Graphics::CommandBuffer* buffer);
void DisplayBufferCompleteFlipFromGpu(uint64_t request_id);
void DisplayBufferWaitForFlipQueueSlot();
} // namespace Libs::Presentation
+186 -80
View File
@@ -10,6 +10,7 @@
#include "common/threads.h"
#include "common/timer.h"
#include "graphics/guest_gpu/gpu_defs.h"
#include "graphics/guest_gpu/graphicsRun.h"
#include "graphics/guest_gpu/tile.h"
#include "graphics/host_gpu/renderer/render.h"
#include "graphics/host_gpu/renderer/renderContext.h"
@@ -21,6 +22,7 @@
#include "libs/libs.h"
#include <algorithm>
#include <limits>
#include <list>
#include <vector>
@@ -45,6 +47,7 @@ constexpr int VIDEO_OUT_FLIP_MODE_VSYNC_MULTI = 4;
constexpr int VIDEO_OUT_BUFFER_INDEX_BLACK = -2;
constexpr int VIDEO_OUT_BUFFER_INDEX_BLANK = -1;
constexpr int VIDEO_OUT_BUFFER_NUM_MAX = 16;
constexpr size_t VIDEO_OUT_FLIP_QUEUE_CAPACITY = 16;
constexpr int VIDEO_OUT_BUFFER_ATTRIBUTE_NUM_MAX = 4;
constexpr uint64_t VIDEO_OUT_OUTPUT_MODE_DEFAULT = 0x0000000000000001ULL;
constexpr uint64_t VIDEO_OUT_OUTPUT_MODE_119_88HZ = 0x000000000000000FULL;
@@ -160,28 +163,39 @@ public:
virtual ~FlipQueue() { KYTY_NOT_IMPLEMENTED; }
KYTY_CLASS_NO_COPY(FlipQueue);
bool Submit(VideoOutConfig* cfg, int index, int64_t flip_arg, bool gpu_eop);
bool Reserve(VideoOutConfig* cfg, int index, int64_t flip_arg, bool gpu_eop,
uint64_t* request_id);
void Prepare(uint64_t request_id, Graphics::CommandBuffer* buffer);
uint64_t PrepareNextCpu(Graphics::CommandBuffer* buffer);
void Complete(uint64_t request_id);
void WaitForSubmitSlot();
bool Flip(uint32_t micros);
bool HasPending(VideoOutConfig* cfg, int start_index, int count);
void GetFlipStatus(VideoOutConfig* cfg, VideoOutFlipStatus* out);
void AllowEopPresent(VideoOutConfig* cfg, int index);
void Wait(VideoOutConfig* cfg, int index);
private:
enum class RequestState { Reserved, Recording, Ready, Presenting };
struct Request {
uint64_t id;
VideoOutConfig* cfg;
int index;
int64_t flip_arg;
uint64_t submit_ptc;
bool gpu_eop;
bool prepared;
RequestState state;
Graphics::PreparedFrame* frame;
};
Common::Mutex m_mutex;
Common::CondVar m_submit_cond_var;
Common::CondVar m_submit_slot_cond_var;
Common::CondVar m_done_cond_var;
std::list<Request> m_requests;
std::list<Request> m_cpu_requests;
bool m_processing = false;
uint64_t m_next_request_id = 1;
};
class VideoOutContext {
@@ -558,52 +572,145 @@ Presentation::DisplayBufferImage VideoOutContext::FindImage(const void* buffer,
return ret;
}
bool FlipQueue::Submit(VideoOutConfig* cfg, int index, int64_t flip_arg, bool gpu_eop) {
bool FlipQueue::Reserve(VideoOutConfig* cfg, int index, int64_t flip_arg, bool gpu_eop,
uint64_t* request_id) {
EXIT_IF(cfg == nullptr || request_id == nullptr);
Common::LockGuard lock(m_mutex);
if (m_requests.size() >= 16) {
if (m_requests.size() + m_cpu_requests.size() >= VIDEO_OUT_FLIP_QUEUE_CAPACITY) {
return false;
}
auto& pending = gpu_eop ? m_requests : m_cpu_requests;
Request r {};
r.id = m_next_request_id++;
r.cfg = cfg;
r.index = index;
r.flip_arg = flip_arg;
r.submit_ptc = LibKernel::KernelGetProcessTimeCounter();
r.gpu_eop = gpu_eop;
r.prepared = !gpu_eop;
r.state = RequestState::Reserved;
m_requests.push_back(r);
pending.push_back(r);
*request_id = r.id;
cfg->flip_status.flipPendingNum = static_cast<int>(m_requests.size());
cfg->flip_status.flipPendingNum = static_cast<int>(m_requests.size() + m_cpu_requests.size());
cfg->flip_status.submitProcessTimeCounter = r.submit_ptc;
if (gpu_eop) {
cfg->flip_status.gcQueueNum++;
}
m_submit_cond_var.Signal();
return true;
}
void FlipQueue::AllowEopPresent(VideoOutConfig* cfg, int index) {
Common::LockGuard lock(m_mutex);
auto request = std::find_if(m_requests.begin(), m_requests.end(), [cfg, index](const auto& r) {
return r.cfg == cfg && r.index == index && r.gpu_eop && !r.prepared;
});
if (request == m_requests.end()) {
EXIT("completed GPU flip has no queued request\n");
void FlipQueue::Prepare(uint64_t request_id, Graphics::CommandBuffer* buffer) {
EXIT_IF(buffer == nullptr);
VideoOutConfig* cfg = nullptr;
int index = 0;
{
Common::LockGuard lock(m_mutex);
auto request = std::find_if(m_requests.begin(), m_requests.end(),
[request_id](const auto& r) { return r.id == request_id; });
if (request == m_requests.end()) {
auto pending = std::find_if(m_cpu_requests.begin(), m_cpu_requests.end(),
[request_id](const auto& r) { return r.id == request_id; });
if (pending == m_cpu_requests.end()) {
EXIT("cannot prepare video-out request id=%" PRIu64 "\n", request_id);
}
request = m_requests.insert(m_requests.end(), *pending);
m_cpu_requests.erase(pending);
}
if (request->state != RequestState::Reserved) {
EXIT("cannot prepare video-out request id=%" PRIu64 "\n", request_id);
}
request->state = RequestState::Recording;
cfg = request->cfg;
index = request->index;
}
request->prepared = true;
const bool special = IsSpecialBufferIndex(index);
Graphics::VideoOutVulkanImage* source = nullptr;
uint32_t width = 0;
uint32_t height = 0;
{
Common::LockGuard lock(cfg->mutex);
if (cfg->closing) {
EXIT("cannot prepare flip for a closing video-out, id=%" PRIu64 "\n", request_id);
}
if (special) {
width = cfg->width;
height = cfg->height;
} else {
if (cfg->unregistering[index]) {
EXIT("cannot prepare flip from an unavailable surface, id=%" PRIu64 " index=%d\n",
request_id, index);
}
source = cfg->buffers[index].buffer_vulkan;
if (source == nullptr) {
EXIT("cannot prepare flip without a native surface, id=%" PRIu64 " index=%d\n",
request_id, index);
}
}
}
auto* frame = special ? Graphics::WindowPrepareBlankFrame(buffer, width, height,
index == VIDEO_OUT_BUFFER_INDEX_BLACK)
: Graphics::WindowPrepareFrame(buffer, source);
Common::LockGuard lock(m_mutex);
auto request = std::find_if(m_requests.begin(), m_requests.end(),
[request_id](const auto& r) { return r.id == request_id; });
if (request == m_requests.end() || request->state != RequestState::Recording ||
request->frame != nullptr) {
EXIT("video-out request changed while recording, id=%" PRIu64 "\n", request_id);
}
request->frame = frame;
}
uint64_t FlipQueue::PrepareNextCpu(Graphics::CommandBuffer* buffer) {
uint64_t request_id = 0;
{
Common::LockGuard lock(m_mutex);
if (m_cpu_requests.empty()) {
EXIT("CPU flip preparation has no accepted request\n");
}
request_id = m_cpu_requests.front().id;
}
Prepare(request_id, buffer);
return request_id;
}
void FlipQueue::Complete(uint64_t request_id) {
Common::LockGuard lock(m_mutex);
auto request = std::find_if(m_requests.begin(), m_requests.end(),
[request_id](const auto& r) { return r.id == request_id; });
if (request == m_requests.end() || request->state != RequestState::Recording ||
request->frame == nullptr) {
EXIT("completed GPU flip has no prepared recording, id=%" PRIu64 "\n", request_id);
}
request->state = RequestState::Ready;
m_submit_cond_var.Signal();
}
void FlipQueue::WaitForSubmitSlot() {
Common::LockGuard lock(m_mutex);
while (m_requests.size() + m_cpu_requests.size() >= VIDEO_OUT_FLIP_QUEUE_CAPACITY) {
if (m_requests.empty()) {
EXIT("video-out queue is saturated by CPU flips queued behind the current EOP\n");
}
m_submit_slot_cond_var.Wait(&m_mutex);
}
}
void FlipQueue::Wait(VideoOutConfig* cfg, int index) {
Common::LockGuard lock(m_mutex);
while (std::find_if(m_requests.begin(), m_requests.end(), [cfg, index](const auto& r) {
return r.cfg == cfg && r.index == index;
}) != m_requests.end()) {
auto has_request = [this, cfg, index] {
auto matches = [cfg, index](const auto& r) { return r.cfg == cfg && r.index == index; };
return std::any_of(m_requests.begin(), m_requests.end(), matches) ||
std::any_of(m_cpu_requests.begin(), m_cpu_requests.end(), matches);
};
while (has_request()) {
m_done_cond_var.Wait(&m_mutex);
}
}
@@ -613,10 +720,12 @@ bool FlipQueue::HasPending(VideoOutConfig* cfg, int start_index, int count) {
EXIT("invalid video-out pending-flip query range\n");
}
Common::LockGuard lock(m_mutex);
return std::any_of(m_requests.begin(), m_requests.end(), [&](const auto& request) {
auto matches = [&](const auto& request) {
return request.cfg == cfg && request.index >= start_index &&
request.index < start_index + count;
});
};
return std::any_of(m_requests.begin(), m_requests.end(), matches) ||
std::any_of(m_cpu_requests.begin(), m_cpu_requests.end(), matches);
}
bool FlipQueue::Flip(uint32_t micros) {
@@ -634,7 +743,7 @@ bool FlipQueue::Flip(uint32_t micros) {
if (m_processing) {
EXIT("video-out flip queue processing is already active\n");
}
if (m_requests.front().gpu_eop && !m_requests.front().prepared) {
if (m_requests.front().state != RequestState::Ready) {
m_mutex.Unlock();
return false;
}
@@ -647,23 +756,19 @@ bool FlipQueue::Flip(uint32_t micros) {
return false;
}
Graphics::VideoOutVulkanImage* buffer = nullptr;
r.cfg->mutex.Lock();
if (!IsSpecialBufferIndex(r.index)) {
buffer = r.cfg->buffers[r.index].buffer_vulkan;
if (buffer == nullptr) {
EXIT("queued video-out flip has no native surface\n");
}
}
r.cfg->mutex.Unlock();
if (buffer != nullptr) {
Graphics::g_render_ctx->GetTextureCache()->RefreshVideoOut(buffer);
Graphics::WindowDrawBuffer(buffer);
m_mutex.Lock();
if (m_requests.empty() || m_requests.front().id != r.id ||
m_requests.front().state != RequestState::Ready || !m_processing) {
EXIT("video-out request changed before presentation, id=%" PRIu64 "\n", r.id);
}
m_requests.front().state = RequestState::Presenting;
m_mutex.Unlock();
Graphics::WindowPresentFrame(r.frame);
m_mutex.Lock();
if (m_requests.empty() || m_requests.front().cfg != r.cfg ||
m_requests.front().index != r.index || m_requests.front().submit_ptc != r.submit_ptc) {
if (m_requests.empty() || m_requests.front().id != r.id ||
m_requests.front().state != RequestState::Presenting) {
EXIT("video-out flip queue changed while processing its front request\n");
}
m_requests.pop_front();
@@ -674,13 +779,14 @@ bool FlipQueue::Flip(uint32_t micros) {
r.cfg->flip_status.submitProcessTimeCounter = r.submit_ptc;
r.cfg->flip_status.flipArg = r.flip_arg;
r.cfg->flip_status.currentBuffer = r.index;
r.cfg->flip_status.flipPendingNum = static_cast<int>(m_requests.size());
r.cfg->flip_status.flipPendingNum = static_cast<int>(m_requests.size() + m_cpu_requests.size());
if (r.gpu_eop && r.cfg->flip_status.gcQueueNum > 0) {
r.cfg->flip_status.gcQueueNum--;
}
m_processing = false;
m_done_cond_var.Signal();
m_done_cond_var.SignalAll();
m_submit_slot_cond_var.Signal();
m_mutex.Unlock();
r.cfg->mutex.Lock();
@@ -1448,17 +1554,20 @@ KYTY_SYSV_ABI int VideoOutSubmitFlip(int handle, int index, int flip_mode, int64
if (!IsValidBufferIndex(index)) {
return VIDEO_OUT_ERROR_INVALID_INDEX;
}
Common::LockGuard lock(ctx->mutex);
if (ctx->closing || (!IsSpecialBufferIndex(index) && ctx->unregistering[index])) {
return VIDEO_OUT_ERROR_INVALID_INDEX;
}
if (!IsSpecialBufferIndex(index) && ctx->buffers[index].buffer_vulkan == nullptr) {
return VIDEO_OUT_ERROR_INVALID_INDEX;
}
if (!g_video_out_context->GetFlipQueue().Submit(ctx, index, flip_arg, false)) {
return VIDEO_OUT_ERROR_FLIP_QUEUE_FULL;
uint64_t request_id = 0;
{
Common::LockGuard lock(ctx->mutex);
const bool special = IsSpecialBufferIndex(index);
if (ctx->closing || (!special && (ctx->unregistering[index] ||
ctx->buffers[index].buffer_vulkan == nullptr))) {
return VIDEO_OUT_ERROR_INVALID_INDEX;
}
if (!g_video_out_context->GetFlipQueue().Reserve(ctx, index, flip_arg, false,
&request_id)) {
return VIDEO_OUT_ERROR_FLIP_QUEUE_FULL;
}
}
Graphics::GraphicsRunSubmitFlipPreparation();
return OK;
}
@@ -1467,8 +1576,10 @@ KYTY_SYSV_ABI int VideoOutSubmitFlip(int handle, int index, int flip_mode, int64
namespace Libs::Presentation {
int DisplayBufferSubmitFlipFromGpu(int handle, int index, int flip_mode, int64_t flip_arg) {
EXIT_IF(VideoOut::g_video_out_context == nullptr || Graphics::g_render_ctx == nullptr);
int DisplayBufferSubmitFlipFromGpu(Graphics::CommandBuffer* buffer, int handle, int index,
int flip_mode, int64_t flip_arg, uint64_t* request_id) {
EXIT_IF(VideoOut::g_video_out_context == nullptr || Graphics::g_render_ctx == nullptr ||
buffer == nullptr || request_id == nullptr);
auto* ctx = VideoOut::g_video_out_context->Get(handle);
if (ctx == nullptr) {
@@ -1483,41 +1594,36 @@ int DisplayBufferSubmitFlipFromGpu(int handle, int index, int flip_mode, int64_t
if (!VideoOut::IsValidBufferIndex(index)) {
return VideoOut::VIDEO_OUT_ERROR_INVALID_INDEX;
}
Common::LockGuard lock(ctx->mutex);
if (ctx->closing || (!VideoOut::IsSpecialBufferIndex(index) && ctx->unregistering[index])) {
return VideoOut::VIDEO_OUT_ERROR_INVALID_INDEX;
}
if (!VideoOut::IsSpecialBufferIndex(index) && ctx->buffers[index].buffer_vulkan == nullptr) {
return VideoOut::VIDEO_OUT_ERROR_INVALID_INDEX;
}
if (!VideoOut::g_video_out_context->GetFlipQueue().Submit(ctx, index, flip_arg, true)) {
return VideoOut::VIDEO_OUT_ERROR_FLIP_QUEUE_FULL;
{
Common::LockGuard lock(ctx->mutex);
const bool special = VideoOut::IsSpecialBufferIndex(index);
if (ctx->closing || (!special && (ctx->unregistering[index] ||
ctx->buffers[index].buffer_vulkan == nullptr))) {
return VideoOut::VIDEO_OUT_ERROR_INVALID_INDEX;
}
if (!VideoOut::g_video_out_context->GetFlipQueue().Reserve(ctx, index, flip_arg, true,
request_id)) {
return VideoOut::VIDEO_OUT_ERROR_FLIP_QUEUE_FULL;
}
}
VideoOut::g_video_out_context->GetFlipQueue().Prepare(*request_id, buffer);
return OK;
}
void DisplayBufferCompleteFlipFromGpu(int handle, int index) {
uint64_t DisplayBufferPrepareNextFlipOnGpu(Graphics::CommandBuffer* buffer) {
EXIT_IF(VideoOut::g_video_out_context == nullptr || Graphics::g_render_ctx == nullptr);
return VideoOut::g_video_out_context->GetFlipQueue().PrepareNextCpu(buffer);
}
auto* ctx = VideoOut::g_video_out_context->Get(handle);
if (ctx == nullptr || !VideoOut::IsValidBufferIndex(index)) {
EXIT("GPU flip completed with invalid handle or index, handle=%d index=%d\n", handle,
index);
}
Common::LockGuard lock(ctx->mutex);
if (ctx->closing || (!VideoOut::IsSpecialBufferIndex(index) && ctx->unregistering[index])) {
EXIT("GPU flip completed for an unavailable surface, handle=%d index=%d\n", handle, index);
}
if (!VideoOut::IsSpecialBufferIndex(index)) {
auto* image = ctx->buffers[index].buffer_vulkan;
if (image == nullptr) {
EXIT("GPU flip completed without a native surface, handle=%d index=%d\n", handle,
index);
}
Graphics::g_render_ctx->GetTextureCache()->RefreshVideoOut(image);
}
VideoOut::g_video_out_context->GetFlipQueue().AllowEopPresent(ctx, index);
void DisplayBufferCompleteFlipFromGpu(uint64_t request_id) {
EXIT_IF(VideoOut::g_video_out_context == nullptr || Graphics::g_render_ctx == nullptr);
VideoOut::g_video_out_context->GetFlipQueue().Complete(request_id);
}
void DisplayBufferWaitForFlipQueueSlot() {
EXIT_IF(VideoOut::g_video_out_context == nullptr || Graphics::g_render_ctx == nullptr);
VideoOut::g_video_out_context->GetFlipQueue().WaitForSubmitSlot();
}
} // namespace Libs::Presentation
+6 -1
View File
@@ -9,7 +9,9 @@ struct VkSurfaceCapabilitiesKHR;
namespace Libs::Graphics {
struct GraphicContext;
class CommandBuffer;
struct VideoOutVulkanImage;
struct PreparedFrame;
VkSurfaceCapabilitiesKHR* VulkanGetSurfaceCapabilities();
@@ -18,7 +20,10 @@ GraphicContext* WindowGetGraphicContext();
void WindowInit(uint32_t width, uint32_t height);
void WindowRun();
void WindowWaitForGraphicInitialized();
void WindowDrawBuffer(VideoOutVulkanImage* image);
PreparedFrame* WindowPrepareFrame(CommandBuffer* buffer, VideoOutVulkanImage* image);
PreparedFrame* WindowPrepareBlankFrame(CommandBuffer* buffer, uint32_t width, uint32_t height,
bool opaque);
void WindowPresentFrame(PreparedFrame* frame);
} // namespace Libs::Graphics
+178 -26
View File
@@ -28,6 +28,7 @@
#include "common/timer.h"
#include "graphics/host_gpu/graphicContext.h"
#include "graphics/host_gpu/renderer/render.h"
#include "graphics/host_gpu/renderer/renderContext.h"
#include "graphics/host_gpu/utils.h"
#include "graphics/host_gpu/vma.h"
#include "graphics/presentation/renderDoc.h"
@@ -38,9 +39,11 @@
#include "loader/systemContent.h"
#include <algorithm>
#include <array>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fmt/format.h>
#include <memory>
#include <string>
@@ -55,6 +58,131 @@
namespace Libs::Graphics {
struct PreparedFrame {
VulkanImage image {VulkanImageType::Unknown};
std::unique_ptr<CommandBuffer> present_commands;
bool busy = false;
};
class PreparedFramePool {
public:
void EnsureCapacity(uint32_t count, VkFormat format) {
if (count == 0 || format == VK_FORMAT_UNDEFINED) {
EXIT("prepared-frame pool requires at least one frame\n");
}
Common::LockGuard lock(m_mutex);
m_format = format;
while (m_frames.size() < count) {
auto frame = std::make_unique<PreparedFrame>();
m_free.push_back(frame.get());
m_frames.push_back(std::move(frame));
}
}
VkFormat GetFormat() {
Common::LockGuard lock(m_mutex);
if (m_format == VK_FORMAT_UNDEFINED) {
EXIT("prepared-frame pool has no presentation format\n");
}
return m_format;
}
PreparedFrame* Acquire() {
m_mutex.Lock();
if (m_frames.empty()) {
EXIT("prepared-frame pool was used before swapchain initialization\n");
}
while (m_free.empty()) {
m_available.Wait(&m_mutex);
}
auto* frame = m_free.front();
m_free.pop_front();
if (frame->busy) {
EXIT("prepared-frame pool returned an invalid frame\n");
}
frame->busy = true;
m_mutex.Unlock();
// The producer only waits here. Reset stays on the presentation thread that owns the
// allocating Vulkan command pool.
if (frame->present_commands != nullptr) {
frame->present_commands->WaitForFenceOnly();
}
return frame;
}
void Release(PreparedFrame* frame) {
if (frame == nullptr) {
EXIT("cannot release a null prepared frame\n");
}
Common::LockGuard lock(m_mutex);
if (!frame->busy) {
EXIT("prepared frame was released twice\n");
}
frame->busy = false;
m_free.push_back(frame);
m_available.Signal();
}
private:
Common::Mutex m_mutex;
Common::CondVar m_available;
std::vector<std::unique_ptr<PreparedFrame>> m_frames;
std::deque<PreparedFrame*> m_free;
VkFormat m_format = VK_FORMAT_UNDEFINED;
};
static PreparedFramePool* GetPreparedFramePool() {
static auto* pool = new PreparedFramePool;
return pool;
}
static void ConfigurePreparedFrame(PreparedFrame* frame, VkExtent2D extent, VkFormat format) {
EXIT_IF(frame == nullptr);
EXIT_IF(g_window_ctx == nullptr);
if (extent.width == 0 || extent.height == 0 || format == VK_FORMAT_UNDEFINED) {
EXIT("unsupported prepared frame, extent=%ux%u format=%d\n", extent.width, extent.height,
static_cast<int>(format));
}
auto* ctx = &g_window_ctx->graphic_ctx;
auto& dst = frame->image;
const bool compatible = dst.image != nullptr && dst.extent.width == extent.width &&
dst.extent.height == extent.height && dst.format == format;
if (compatible) {
return;
}
if (dst.image != nullptr) {
VulkanDeleteImage(ctx, &dst, &dst.memory);
dst = VulkanImage(VulkanImageType::Unknown);
}
dst.extent = extent;
dst.format = format;
dst.layers = 1;
dst.mip_levels = 1;
dst.layout = VK_IMAGE_LAYOUT_UNDEFINED;
dst.memory.property = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
VkImageCreateInfo create {};
create.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
create.imageType = VK_IMAGE_TYPE_2D;
create.extent = {dst.extent.width, dst.extent.height, 1};
create.mipLevels = 1;
create.arrayLayers = 1;
create.format = dst.format;
create.tiling = VK_IMAGE_TILING_OPTIMAL;
create.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
create.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
create.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
create.samples = VK_SAMPLE_COUNT_1_BIT;
if (!VulkanCreateImage(ctx, &create, &dst, &dst.memory)) {
EXIT("failed to allocate prepared presentation image, extent=%ux%u format=%d\n",
dst.extent.width, dst.extent.height, static_cast<int>(dst.format));
}
}
VulkanSwapchain::~VulkanSwapchain() = default;
[[maybe_unused]] static VkSwapchainKHR VulkanCreateSwapchainInternal(
@@ -189,6 +317,7 @@ VulkanSwapchain* VulkanCreateSwapchain(GraphicContext* ctx, uint32_t image_count
&s->render_complete_semaphores[i]);
EXIT_NOT_IMPLEMENTED(result != VK_SUCCESS);
}
GetPreparedFramePool()->EnsureCapacity(s->swapchain_images_count, s->swapchain_format);
return swapchain_owner.release();
}
@@ -201,8 +330,6 @@ static void VulkanDeleteSwapchain(GraphicContext* ctx, VulkanSwapchain* s) {
VulkanDeviceWaitIdle(ctx);
s->present_command_buffers.clear();
if (s->image_acquired_semaphores != nullptr) {
for (uint32_t i = 0; i < s->swapchain_images_count; i++) {
if (s->image_acquired_semaphores[i] != nullptr) {
@@ -256,31 +383,56 @@ static void VulkanRecreateSwapchain() {
g_window_ctx->swapchain = VulkanCreateSwapchain(&g_window_ctx->graphic_ctx, 2);
}
static CommandBuffer* WindowGetPresentCommandBuffer(VulkanSwapchain* swapchain,
uint32_t present_frame) {
EXIT_IF(swapchain == nullptr);
EXIT_IF(swapchain->swapchain_images_count == 0);
EXIT_IF(present_frame >= swapchain->swapchain_images_count);
if (swapchain->present_command_buffers.empty()) {
swapchain->present_command_buffers.resize(swapchain->swapchain_images_count);
static void ValidatePreparedCommand(CommandBuffer* buffer) {
if (buffer == nullptr || buffer->IsInvalid() || buffer->GetQueue() != GraphicContext::QUEUE_GFX) {
EXIT("prepared frames must be recorded on the graphics queue\n");
}
EXIT_IF(swapchain->present_command_buffers.size() != swapchain->swapchain_images_count);
auto& buffer = swapchain->present_command_buffers[present_frame];
if (buffer == nullptr) {
buffer = std::make_unique<CommandBuffer>(GraphicContext::QUEUE_GFX);
}
return buffer.get();
EXIT_IF(g_render_ctx == nullptr);
}
void WindowDrawBuffer(VideoOutVulkanImage* image) {
PreparedFrame* WindowPrepareFrame(CommandBuffer* buffer, VideoOutVulkanImage* image) {
KYTY_PROFILER_FUNCTION();
ValidatePreparedCommand(buffer);
if (image->format == VK_FORMAT_UNDEFINED) {
EXIT("unsupported presentation source, image=%p\n", static_cast<const void*>(image));
}
auto* frame = GetPreparedFramePool()->Acquire();
Common::LockGuard render_lock(g_render_ctx->GetMutex());
g_render_ctx->GetTextureCache()->RefreshVideoOut(image);
ConfigurePreparedFrame(frame, image->extent, image->format);
const std::array copies {ImageImageCopy {
.src_image = image, .width = image->extent.width, .height = image->extent.height}};
UtilImageToImage(buffer, copies, &frame->image,
static_cast<uint64_t>(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL));
return frame;
}
PreparedFrame* WindowPrepareBlankFrame(CommandBuffer* buffer, uint32_t width, uint32_t height,
bool opaque) {
KYTY_PROFILER_FUNCTION();
ValidatePreparedCommand(buffer);
auto* pool = GetPreparedFramePool();
auto format = pool->GetFormat();
auto* frame = pool->Acquire();
Common::LockGuard render_lock(g_render_ctx->GetMutex());
ConfigurePreparedFrame(frame, {width, height}, format);
const VkClearColorValue clear {{0.0f, 0.0f, 0.0f, opaque ? 1.0f : 0.0f}};
UtilClearColorImage(buffer, &frame->image, clear);
return frame;
}
void WindowPresentFrame(PreparedFrame* frame) {
KYTY_PROFILER_FUNCTION();
EXIT_IF(image == nullptr);
EXIT_IF(frame == nullptr);
EXIT_IF(g_window_ctx == nullptr);
EXIT_IF(g_window_ctx->swapchain == nullptr);
struct ReleaseScope {
PreparedFrame* frame;
~ReleaseScope() { GetPreparedFramePool()->Release(frame); }
};
ReleaseScope release {frame};
if (g_window_ctx->window_hidden) {
WindowUpdateIcon();
@@ -296,9 +448,6 @@ void WindowDrawBuffer(VideoOutVulkanImage* image) {
const auto present_frame = swapchain->present_frame;
EXIT_IF(present_frame >= swapchain->swapchain_images_count);
auto& buffer = *WindowGetPresentCommandBuffer(swapchain, present_frame);
buffer.WaitForFenceAndReset();
swapchain->current_index = static_cast<uint32_t>(-1);
auto result = vkAcquireNextImageKHR(
@@ -315,14 +464,17 @@ void WindowDrawBuffer(VideoOutVulkanImage* image) {
default: EXIT("vkAcquireNextImageKHR failed: %s\n", string_VkResult(result));
}
EXIT_NOT_IMPLEMENTED(swapchain->current_index == static_cast<uint32_t>(-1));
EXIT_NOT_IMPLEMENTED(buffer.IsInvalid());
if (frame->present_commands == nullptr) {
frame->present_commands = std::make_unique<CommandBuffer>(GraphicContext::QUEUE_GFX);
}
frame->present_commands->WaitForFenceAndReset();
auto& buffer = *frame->present_commands;
auto* vk_buffer = buffer.GetPool()->buffers[buffer.GetIndex()];
buffer.Begin();
UtilBlitImage(&buffer, image, swapchain);
UtilBlitPreparedImage(&buffer, &frame->image, swapchain);
VkImageMemoryBarrier pre_present_barrier {};
pre_present_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;