1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <atomic>
#include <condition_variable>
#include <mutex>
#include <boost/container/deque.hpp>
#include "common/common_types.h"
#include "common/polyfill_thread.h"
#include "video_core/vulkan_common/vulkan_memory_allocator.h"
#include "video_core/vulkan_common/vulkan_wrapper.h"
namespace Core::Frontend {
class EmuWindow;
} // namespace Core::Frontend
namespace Vulkan {
class Device;
class Scheduler;
class Swapchain;
struct Frame {
u32 width;
u32 height;
u32 index;
vk::Image image;
vk::ImageView image_view;
vk::ImageView storage_view;
vk::Framebuffer framebuffer;
vk::CommandBuffer cmdbuf;
vk::Semaphore render_ready;
vk::Fence present_done;
bool storage_capable{};
};
class PresentManager {
public:
PresentManager(const vk::Instance& instance,
Core::Frontend::EmuWindow& render_window,
const Device& device,
MemoryAllocator& memory_allocator,
Scheduler& scheduler,
Swapchain& swapchain,
vk::SurfaceKHR& surface);
~PresentManager();
/// Returns the last used presentation frame
Frame* GetRenderFrame();
/// Pushes a frame for presentation
void Present(Frame* frame);
/// Recreates the present frame to match the provided parameters
void RecreateFrame(Frame* frame, u32 width, u32 height, VkFormat image_view_format,
VkRenderPass rd, bool storage);
[[nodiscard]] bool NeedsStorage(const Frame* frame, bool required) const;
/// Waits for the present thread to finish presenting all queued frames.
void WaitPresent();
/// How many additional frames can be queued without stalling the render thread
[[nodiscard]] size_t MaxExtraFrames() const;
[[nodiscard]] std::size_t SwapchainImageCount() const { return swapchain_image_count; }
[[nodiscard]] VkFormat SwapchainImageFormat() const { return swapchain_image_format; }
private:
void PresentThread(std::stop_token token);
void CopyToSwapchain(Frame* frame);
void CopyToSwapchainImpl(Frame* frame);
void RecreateSwapchain(Frame* frame);
void DiscardFrame(Frame* frame);
void SetImageCount();
private:
const vk::Instance& instance;
Core::Frontend::EmuWindow& render_window;
const Device& device;
MemoryAllocator& memory_allocator;
Scheduler& scheduler;
Swapchain& swapchain;
vk::SurfaceKHR& surface;
vk::CommandPool cmdpool;
std::vector<Frame> frames;
boost::container::deque<Frame*> present_queue;
boost::container::deque<Frame*> free_queue;
std::condition_variable_any frame_cv;
std::condition_variable free_cv;
std::mutex swapchain_mutex;
std::mutex queue_mutex;
std::mutex free_mutex;
std::jthread present_thread;
bool blit_supported;
bool storage_supported;
bool use_present_thread;
std::atomic<std::size_t> image_count{};
std::atomic<std::size_t> swapchain_image_count{};
std::atomic<VkFormat> swapchain_image_format{};
};
} // namespace Vulkan
|