mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-10-16 16:00:30 +02:00
The core and the headless tools no longer use mutexes, condition variables, spinlocks, std::atomic, std::thread or volatile flags. Threads hand off through libretro-common's retro_atomic and rthreads plus a few small pieces on top of them in Common/Thread: Thread, MpscQueue, EventCounter, ParkingLot waits and ReaderGate. Every wait blocks until it's woken; nothing polls on a timer. State that only one thread touches just loses its lock. The PSP filesystem, sceIo and the savedata dialog now belong to the emulation thread: async reads and writes run when they're queued and complete later in emulated time, and the IO and savedata threads are gone. The friend finder only talks to the adhoc server and hands what it gets to the emulation thread. GPU init always runs on the thread that starts the game. The Vulkan render and compile queues, pipeline and render pass caches, delete list and SPIR-V cache, the GL render queue, the software renderer's jit caches, the file loader caches, the web debugger, UPnP, input, savestates, logging and the GE debugger all hand off through queues and atomics. The IR jit's spinning profiler thread goes. A CPU thread with nothing to run now sleeps until work is queued instead of spinning. Headless with --debugger waits for a client while stopped, and ends the run once every client that connected has left. Web server shutdown keeps servicing the CPU queue until the server has finished.
148 lines
3.5 KiB
C++
148 lines
3.5 KiB
C++
#pragma once
|
|
|
|
#include <cstdint>
|
|
|
|
#include <retro_atomic.h>
|
|
|
|
#include "Common/GPU/Vulkan/VulkanContext.h"
|
|
#include "Common/Data/Collections/Hashmaps.h"
|
|
|
|
#if PPSSPP_PLATFORM(IOS)
|
|
// iOS doesn't support a large number of timestamp queries natively.
|
|
// We don't want MoltenVK to fall back to emulation.
|
|
enum {
|
|
MAX_TIMESTAMP_QUERIES = 32,
|
|
};
|
|
#else
|
|
enum {
|
|
MAX_TIMESTAMP_QUERIES = 128,
|
|
};
|
|
#endif
|
|
|
|
enum class VKRRunType {
|
|
SUBMIT,
|
|
PRESENT,
|
|
SYNC,
|
|
EXIT,
|
|
};
|
|
|
|
struct QueueProfileContext {
|
|
bool enabled = false;
|
|
bool timestampsEnabled = false;
|
|
VkQueryPool queryPool;
|
|
std::vector<std::string> timestampDescriptions;
|
|
std::string profileSummary;
|
|
double cpuStartTime;
|
|
double cpuEndTime;
|
|
double descWriteTime;
|
|
int descriptorsWritten;
|
|
int descriptorsDeduped;
|
|
#ifdef _DEBUG
|
|
int commandCounts[11];
|
|
#endif
|
|
};
|
|
|
|
class VKRFramebuffer;
|
|
|
|
struct ReadbackKey {
|
|
const VKRFramebuffer *framebuf;
|
|
int width;
|
|
int height;
|
|
};
|
|
|
|
struct CachedReadback {
|
|
VkBuffer buffer;
|
|
VmaAllocation allocation;
|
|
VkDeviceSize bufferSize;
|
|
bool isCoherent;
|
|
|
|
void Destroy(VulkanContext *vulkan);
|
|
};
|
|
|
|
// Swap chain management
|
|
struct SwapchainImageData {
|
|
VkImage image;
|
|
VkImageView view;
|
|
VkSemaphore renderingCompleteSemaphore = VK_NULL_HANDLE;
|
|
};
|
|
|
|
struct FrameDataShared {
|
|
// For synchronous readbacks.
|
|
VkFence readbackFence = VK_NULL_HANDLE;
|
|
bool useMultiThreading = false;
|
|
bool measurePresentTime = false;
|
|
|
|
std::vector<SwapchainImageData> swapchainImages_;
|
|
uint32_t swapchainImageCount_ = 0;
|
|
|
|
void Init(VulkanContext *vulkan, bool useMultiThreading, bool measurePresentTime);
|
|
void Destroy(VulkanContext *vulkan);
|
|
};
|
|
|
|
enum class FrameSubmitType {
|
|
Pending,
|
|
Sync,
|
|
FinishFrame,
|
|
};
|
|
|
|
// Per-frame data, round-robin so we can overlap submission with execution of the previous frame.
|
|
struct FrameData {
|
|
bool skipSwap = false;
|
|
|
|
// With the render thread: set once it has submitted this frame (its fence is then in flight),
|
|
// cleared by BeginFrame before it waits on the fence.
|
|
retro_atomic_int_t readyForFence{ 1 };
|
|
|
|
VkFence fence = VK_NULL_HANDLE;
|
|
VkSemaphore acquireSemaphore = VK_NULL_HANDLE;
|
|
|
|
// These are on different threads so need separate pools.
|
|
VkCommandPool cmdPoolInit = VK_NULL_HANDLE; // Written to from main thread
|
|
VkCommandPool cmdPoolMain = VK_NULL_HANDLE; // Written to from render thread, which also submits
|
|
|
|
VkCommandBuffer initCmd = VK_NULL_HANDLE;
|
|
VkCommandBuffer mainCmd = VK_NULL_HANDLE;
|
|
VkCommandBuffer presentCmd = VK_NULL_HANDLE;
|
|
|
|
bool hasInitCommands = false;
|
|
bool hasMainCommands = false;
|
|
bool hasPresentCommands = false;
|
|
|
|
bool hasFencePending = false;
|
|
bool hasAcquired = false;
|
|
|
|
// Set by the render thread once a sync submit has finished, cleared by FlushSync.
|
|
retro_atomic_int_t syncDone{ 0 };
|
|
|
|
// Swapchain.
|
|
uint32_t curSwapchainImage = -1;
|
|
|
|
// Frames need unique IDs to wait for present on, let's keep them here.
|
|
// Also used for indexing into the frame timing history buffer.
|
|
uint64_t frameId = 0;
|
|
|
|
// Profiling.
|
|
QueueProfileContext profile{};
|
|
|
|
// Async readback cache.
|
|
DenseHashMap<ReadbackKey, CachedReadback *> readbacks_;
|
|
|
|
FrameData() : readbacks_(8) {}
|
|
|
|
void Init(VulkanContext *vulkan, int index);
|
|
void Destroy(VulkanContext *vulkan);
|
|
|
|
void AcquireNextImage(VulkanContext *vulkan);
|
|
VkResult QueuePresent(VulkanContext *vulkan, FrameDataShared &shared);
|
|
|
|
// Generally called from the main thread, unlike most of the rest.
|
|
VkCommandBuffer GetInitCmd(VulkanContext *vulkan);
|
|
|
|
// Submits pending command buffers.
|
|
void Submit(VulkanContext *vulkan, FrameSubmitType type, FrameDataShared &shared);
|
|
|
|
private:
|
|
// Metadata for logging etc
|
|
int index = -1;
|
|
};
|