mirror of
https://github.com/KytyPS5/KytyPS5.git
synced 2026-10-20 10:56:58 +02:00
Initial commit
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
#ifndef EMULATOR_INCLUDE_EMULATOR_GRAPHICS_PRESENTATION_DISPLAYBUFFER_H_
|
||||
#define EMULATOR_INCLUDE_EMULATOR_GRAPHICS_PRESENTATION_DISPLAYBUFFER_H_
|
||||
|
||||
#include "common/common.h"
|
||||
|
||||
namespace Libs::Graphics {
|
||||
struct VideoOutVulkanImage;
|
||||
} // namespace Libs::Graphics
|
||||
|
||||
namespace Libs::Presentation {
|
||||
|
||||
struct DisplayBufferImage {
|
||||
Graphics::VideoOutVulkanImage* image = nullptr;
|
||||
uint32_t index = static_cast<uint32_t>(-1);
|
||||
uint64_t size = 0;
|
||||
uint64_t pitch = 0;
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
} // namespace Libs::Presentation
|
||||
|
||||
#endif // EMULATOR_INCLUDE_EMULATOR_GRAPHICS_PRESENTATION_DISPLAYBUFFER_H_
|
||||
@@ -0,0 +1,293 @@
|
||||
#include "graphics/presentation/renderDoc.h"
|
||||
|
||||
#include "SDL_syswm.h"
|
||||
#include "SDL_version.h"
|
||||
#include "SDL_video.h"
|
||||
#include "common/logging/log.h"
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
|
||||
#if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS
|
||||
#ifndef NOMINMAX
|
||||
#define NOMINMAX
|
||||
#endif
|
||||
#include <windows.h>
|
||||
#undef min
|
||||
#undef max
|
||||
#endif
|
||||
|
||||
namespace Libs::Graphics {
|
||||
|
||||
#if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS
|
||||
|
||||
using RenderDocDevicePointer = void*;
|
||||
using RenderDocWindowHandle = void*;
|
||||
|
||||
enum RenderDocVersion {
|
||||
eRENDERDOC_API_Version_1_4_2 = 10402,
|
||||
};
|
||||
|
||||
enum RenderDocInputButton {
|
||||
eRENDERDOC_Key_NonPrintable = 0x100,
|
||||
eRENDERDOC_Key_Divide,
|
||||
eRENDERDOC_Key_Multiply,
|
||||
eRENDERDOC_Key_Subtract,
|
||||
eRENDERDOC_Key_Plus,
|
||||
eRENDERDOC_Key_F1,
|
||||
};
|
||||
|
||||
using pRENDERDOC_SetCaptureKeys = void(__cdecl*)(RenderDocInputButton* keys, int num);
|
||||
using pRENDERDOC_SetCaptureFilePathTemplate = void(__cdecl*)(const char* pathtemplate);
|
||||
using pRENDERDOC_GetCaptureFilePathTemplate = const char*(__cdecl*)();
|
||||
using pRENDERDOC_GetNumCaptures = uint32_t(__cdecl*)();
|
||||
using pRENDERDOC_GetCapture = uint32_t(__cdecl*)(uint32_t idx, char* filename, uint32_t* pathlength,
|
||||
uint64_t* timestamp);
|
||||
using pRENDERDOC_UnloadCrashHandler = void(__cdecl*)();
|
||||
using pRENDERDOC_SetActiveWindow = void(__cdecl*)(RenderDocDevicePointer device,
|
||||
RenderDocWindowHandle wndHandle);
|
||||
using pRENDERDOC_StartFrameCapture = void(__cdecl*)(RenderDocDevicePointer device,
|
||||
RenderDocWindowHandle wndHandle);
|
||||
using pRENDERDOC_IsFrameCapturing = uint32_t(__cdecl*)();
|
||||
using pRENDERDOC_EndFrameCapture = uint32_t(__cdecl*)(RenderDocDevicePointer device,
|
||||
RenderDocWindowHandle wndHandle);
|
||||
using pRENDERDOC_GetAPI = int(__cdecl*)(RenderDocVersion version, void** out_api_pointers);
|
||||
|
||||
struct RenderDocApi {
|
||||
void* GetAPIVersion;
|
||||
void* SetCaptureOptionU32;
|
||||
void* SetCaptureOptionF32;
|
||||
void* GetCaptureOptionU32;
|
||||
void* GetCaptureOptionF32;
|
||||
void* SetFocusToggleKeys;
|
||||
pRENDERDOC_SetCaptureKeys SetCaptureKeys;
|
||||
void* GetOverlayBits;
|
||||
void* MaskOverlayBits;
|
||||
void* RemoveHooks;
|
||||
pRENDERDOC_UnloadCrashHandler UnloadCrashHandler;
|
||||
pRENDERDOC_SetCaptureFilePathTemplate SetCaptureFilePathTemplate;
|
||||
pRENDERDOC_GetCaptureFilePathTemplate GetCaptureFilePathTemplate;
|
||||
pRENDERDOC_GetNumCaptures GetNumCaptures;
|
||||
pRENDERDOC_GetCapture GetCapture;
|
||||
void* TriggerCapture;
|
||||
void* IsTargetControlConnected;
|
||||
void* LaunchReplayUI;
|
||||
pRENDERDOC_SetActiveWindow SetActiveWindow;
|
||||
pRENDERDOC_StartFrameCapture StartFrameCapture;
|
||||
pRENDERDOC_IsFrameCapturing IsFrameCapturing;
|
||||
pRENDERDOC_EndFrameCapture EndFrameCapture;
|
||||
void* TriggerMultiFrameCapture;
|
||||
void* SetCaptureFileComments;
|
||||
void* DiscardFrameCapture;
|
||||
};
|
||||
|
||||
enum class RenderDocState : uint32_t {
|
||||
Idle,
|
||||
Requested,
|
||||
Capturing,
|
||||
};
|
||||
|
||||
static RenderDocApi* g_api = nullptr;
|
||||
static HMODULE g_module = nullptr;
|
||||
static RenderDocDevicePointer g_device = nullptr;
|
||||
static RenderDocWindowHandle g_window = nullptr;
|
||||
static std::atomic<RenderDocState> g_state = RenderDocState::Idle;
|
||||
static std::atomic_bool g_init_done = false;
|
||||
static std::atomic_bool g_unavailable_log = false;
|
||||
|
||||
static RenderDocDevicePointer GetRenderDocDevicePointer(VkInstance instance) {
|
||||
if (instance == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return reinterpret_cast<void*>(instance);
|
||||
}
|
||||
|
||||
static bool BindRenderDocApi(HMODULE module) {
|
||||
auto* get_api = reinterpret_cast<pRENDERDOC_GetAPI>(GetProcAddress(module, "RENDERDOC_GetAPI"));
|
||||
if (get_api == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
void* api = nullptr;
|
||||
if (get_api(eRENDERDOC_API_Version_1_4_2, &api) == 0 || api == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
g_module = module;
|
||||
g_api = static_cast<RenderDocApi*>(api);
|
||||
|
||||
g_api->SetCaptureFilePathTemplate("_RenderDoc/kyty");
|
||||
static RenderDocInputButton capture_keys[] = {eRENDERDOC_Key_F1};
|
||||
g_api->SetCaptureKeys(capture_keys, 1);
|
||||
g_api->UnloadCrashHandler();
|
||||
|
||||
char module_path[MAX_PATH] = {};
|
||||
GetModuleFileNameA(module, module_path, sizeof(module_path));
|
||||
LOGF("RenderDoc: bound API from %s\n", module_path);
|
||||
return true;
|
||||
}
|
||||
|
||||
static RenderDocWindowHandle GetRenderDocWindowHandle(SDL_Window* window) {
|
||||
if (window == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
SDL_SysWMinfo info {};
|
||||
SDL_VERSION(&info.version);
|
||||
|
||||
if (SDL_GetWindowWMInfo(window, &info) != SDL_TRUE || info.subsystem != SDL_SYSWM_WINDOWS) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return info.info.win.window;
|
||||
}
|
||||
|
||||
static bool IsAvailable() {
|
||||
return g_api != nullptr && g_device != nullptr && g_window != nullptr;
|
||||
}
|
||||
|
||||
void RenderDocInit() {
|
||||
bool expected = false;
|
||||
if (!g_init_done.compare_exchange_strong(expected, true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
HKEY h_reg_key;
|
||||
LONG result = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
|
||||
L"SOFTWARE\\Classes\\RenderDoc.RDCCapture.1\\DefaultIcon\\", 0,
|
||||
KEY_READ, &h_reg_key);
|
||||
if (result != ERROR_SUCCESS) {
|
||||
return;
|
||||
}
|
||||
std::array<wchar_t, MAX_PATH> key_str {};
|
||||
DWORD str_sz_out {key_str.size()};
|
||||
result = RegQueryValueExW(h_reg_key, L"", 0, NULL, (LPBYTE)key_str.data(), &str_sz_out);
|
||||
RegCloseKey(h_reg_key);
|
||||
if (result != ERROR_SUCCESS) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::filesystem::path path {key_str.cbegin(), key_str.cend()};
|
||||
path = path.parent_path().append("renderdoc.dll");
|
||||
const auto path_to_lib = path.generic_string();
|
||||
auto* module = LoadLibraryA(path_to_lib.c_str());
|
||||
if (module == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!BindRenderDocApi(module)) {
|
||||
LOGF("RenderDoc: API 1.4.2 is not available; in-app capture disabled\n");
|
||||
FreeLibrary(module);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
bool RenderDocIsLoaded() {
|
||||
return g_api != nullptr;
|
||||
}
|
||||
|
||||
void RenderDocSetActiveWindow(VkInstance instance, SDL_Window* window) {
|
||||
if (g_api == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
g_device = GetRenderDocDevicePointer(instance);
|
||||
g_window = GetRenderDocWindowHandle(window);
|
||||
|
||||
if (g_device == nullptr || g_window == nullptr) {
|
||||
LOGF("RenderDoc: active Vulkan window was not registered\n");
|
||||
return;
|
||||
}
|
||||
|
||||
g_api->SetActiveWindow(g_device, g_window);
|
||||
LOGF("RenderDoc: active Vulkan window registered\n");
|
||||
}
|
||||
|
||||
void RenderDocRequestCapture() {
|
||||
if (!IsAvailable()) {
|
||||
if (!g_unavailable_log.exchange(true)) {
|
||||
LOGF("RenderDoc: capture requested, but RenderDoc is not available\n");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
RenderDocState expected = RenderDocState::Idle;
|
||||
if (g_state.compare_exchange_strong(expected, RenderDocState::Requested)) {
|
||||
LOGF("RenderDoc: capture requested; next complete presented frame will be captured\n");
|
||||
} else {
|
||||
LOGF("RenderDoc: capture request ignored because a capture is already pending\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void LogNewestCapture() {
|
||||
const auto count = g_api->GetNumCaptures();
|
||||
if (count == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
char filename[4096] = {};
|
||||
uint32_t path_length = sizeof(filename);
|
||||
uint64_t timestamp = 0;
|
||||
|
||||
if (g_api->GetCapture(count - 1, filename, &path_length, ×tamp) != 0) {
|
||||
filename[sizeof(filename) - 1] = '\0';
|
||||
LOGF("RenderDoc: wrote capture %s\n", filename);
|
||||
}
|
||||
}
|
||||
|
||||
void RenderDocOnPresent() {
|
||||
if (!IsAvailable()) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (g_state.load()) {
|
||||
case RenderDocState::Idle: return;
|
||||
case RenderDocState::Requested:
|
||||
if (g_api->IsFrameCapturing() != 0) {
|
||||
LOGF("RenderDoc: capture request ignored because RenderDoc is already capturing\n");
|
||||
g_state.store(RenderDocState::Idle);
|
||||
return;
|
||||
}
|
||||
|
||||
g_api->StartFrameCapture(nullptr, nullptr);
|
||||
if (g_api->IsFrameCapturing() == 0) {
|
||||
LOGF("RenderDoc: StartFrameCapture returned, but RenderDoc is not capturing\n");
|
||||
g_state.store(RenderDocState::Idle);
|
||||
return;
|
||||
}
|
||||
g_state.store(RenderDocState::Capturing);
|
||||
LOGF("RenderDoc: capture started\n");
|
||||
return;
|
||||
case RenderDocState::Capturing: break;
|
||||
}
|
||||
|
||||
const auto ok = g_api->EndFrameCapture(nullptr, nullptr);
|
||||
g_state.store(RenderDocState::Idle);
|
||||
|
||||
if (ok != 0) {
|
||||
const auto count = g_api->GetNumCaptures();
|
||||
LOGF("RenderDoc: capture finished, count=%u\n", count);
|
||||
LogNewestCapture();
|
||||
} else {
|
||||
LOGF("RenderDoc: capture failed\n");
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
void RenderDocInit() {}
|
||||
bool RenderDocIsLoaded() {
|
||||
return false;
|
||||
}
|
||||
void RenderDocSetActiveWindow(VkInstance /*instance*/, SDL_Window* /*window*/) {}
|
||||
void RenderDocRequestCapture() {}
|
||||
void RenderDocOnPresent() {}
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace Libs::Graphics
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef EMULATOR_INCLUDE_EMULATOR_GRAPHICS_RENDERDOC_H_
|
||||
#define EMULATOR_INCLUDE_EMULATOR_GRAPHICS_RENDERDOC_H_
|
||||
|
||||
#include "common/common.h"
|
||||
|
||||
#include <vulkan/vulkan_core.h>
|
||||
|
||||
struct SDL_Window;
|
||||
|
||||
namespace Libs::Graphics {
|
||||
|
||||
void RenderDocInit();
|
||||
bool RenderDocIsLoaded();
|
||||
void RenderDocSetActiveWindow(VkInstance instance, SDL_Window* window);
|
||||
void RenderDocRequestCapture();
|
||||
void RenderDocOnPresent();
|
||||
|
||||
} // namespace Libs::Graphics
|
||||
|
||||
#endif /* EMULATOR_INCLUDE_EMULATOR_GRAPHICS_RENDERDOC_H_ */
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,84 @@
|
||||
#ifndef EMULATOR_INCLUDE_EMULATOR_GRAPHICS_VIDEOOUT_H_
|
||||
#define EMULATOR_INCLUDE_EMULATOR_GRAPHICS_VIDEOOUT_H_
|
||||
|
||||
#include "common/common.h"
|
||||
// #include "common/subsystems.h"
|
||||
|
||||
#include "common/abi.h"
|
||||
#include "kernel/eventQueue.h"
|
||||
|
||||
namespace Libs::Graphics {
|
||||
// struct VulkanSwapchain;
|
||||
struct VideoOutVulkanImage;
|
||||
} // namespace Libs::Graphics
|
||||
|
||||
namespace Libs::VideoOut {
|
||||
|
||||
struct VideoOutBufferAttribute2;
|
||||
struct VideoOutFlipStatus;
|
||||
struct VideoOutVblankStatus;
|
||||
struct VideoOutOutputStatus;
|
||||
struct VideoOutOutputOptions;
|
||||
struct VideoOutBuffers;
|
||||
struct VideoOutColorSettings;
|
||||
|
||||
void VideoOutInit(uint32_t width, uint32_t height);
|
||||
void VideoOutWaitFlipDone(int handle, int index);
|
||||
|
||||
KYTY_SYSV_ABI int VideoOutOpen(int user_id, int bus_type, int index, const void* param);
|
||||
KYTY_SYSV_ABI int VideoOutClose(int handle);
|
||||
KYTY_SYSV_ABI void VideoOutSetBufferAttribute2(VideoOutBufferAttribute2* attribute,
|
||||
uint64_t pixel_format, uint32_t tiling_mode,
|
||||
uint32_t width, uint32_t height, uint64_t option,
|
||||
uint32_t dcc_control,
|
||||
uint64_t dcc_cb_register_clear_color);
|
||||
KYTY_SYSV_ABI int VideoOutSetFlipRate(int handle, int rate);
|
||||
KYTY_SYSV_ABI int VideoOutAddFlipEvent(LibKernel::EventQueue::KernelEqueue eq, int handle,
|
||||
void* udata);
|
||||
KYTY_SYSV_ABI int VideoOutAddVblankEvent(LibKernel::EventQueue::KernelEqueue eq, int handle,
|
||||
void* udata);
|
||||
KYTY_SYSV_ABI int VideoOutAddPreVblankStartEvent(LibKernel::EventQueue::KernelEqueue eq, int handle,
|
||||
void* udata);
|
||||
KYTY_SYSV_ABI int VideoOutAddOutputModeEvent(LibKernel::EventQueue::KernelEqueue eq, int handle,
|
||||
void* udata);
|
||||
KYTY_SYSV_ABI int VideoOutDeleteFlipEvent(LibKernel::EventQueue::KernelEqueue eq, int handle);
|
||||
KYTY_SYSV_ABI int VideoOutDeleteVblankEvent(LibKernel::EventQueue::KernelEqueue eq, int handle);
|
||||
KYTY_SYSV_ABI int VideoOutDeletePreVblankStartEvent(LibKernel::EventQueue::KernelEqueue eq,
|
||||
int handle);
|
||||
KYTY_SYSV_ABI int VideoOutRegisterBuffers2(int handle, int set_index, int buffer_index_start,
|
||||
const VideoOutBuffers* buffers, int buffer_num,
|
||||
const VideoOutBufferAttribute2* attribute, int category,
|
||||
void* option);
|
||||
KYTY_SYSV_ABI int VideoOutSubmitChangeBufferAttribute2(int handle, int set_index,
|
||||
const VideoOutBufferAttribute2* attribute,
|
||||
void* option);
|
||||
KYTY_SYSV_ABI int VideoOutUnregisterBuffers(int handle, int set_index);
|
||||
KYTY_SYSV_ABI int VideoOutSubmitFlip(int handle, int index, int flip_mode, int64_t flip_arg);
|
||||
KYTY_SYSV_ABI int VideoOutGetFlipStatus(int handle, VideoOutFlipStatus* status);
|
||||
KYTY_SYSV_ABI int VideoOutIsFlipPending(int handle);
|
||||
KYTY_SYSV_ABI int VideoOutGetVblankStatus(int handle, VideoOutVblankStatus* status);
|
||||
KYTY_SYSV_ABI int VideoOutGetEventId(const LibKernel::EventQueue::KernelEvent* ev);
|
||||
KYTY_SYSV_ABI int VideoOutGetEventData(const LibKernel::EventQueue::KernelEvent* ev, int64_t* data);
|
||||
KYTY_SYSV_ABI int VideoOutGetEventCount(const LibKernel::EventQueue::KernelEvent* ev);
|
||||
KYTY_SYSV_ABI int VideoOutWaitVblank(int handle);
|
||||
KYTY_SYSV_ABI int VideoOutGetOutputStatus(int handle, VideoOutOutputStatus* status);
|
||||
KYTY_SYSV_ABI int VideoOutInitializeOutputOptions(VideoOutOutputOptions* options);
|
||||
KYTY_SYSV_ABI int VideoOutIsOutputSupported(int handle, uint64_t mode,
|
||||
const VideoOutOutputOptions* options,
|
||||
void* reserved_ptr, uint64_t reserved);
|
||||
KYTY_SYSV_ABI int VideoOutConfigureOutput(int handle, uint64_t mode,
|
||||
const VideoOutOutputOptions* options, void* reserved_ptr,
|
||||
uint64_t reserved);
|
||||
KYTY_SYSV_ABI int VideoOutSetWindowModeMargins(int handle, int top, int bottom);
|
||||
KYTY_SYSV_ABI int VideoOutLatencyControlWaitBeforeInput(int handle);
|
||||
KYTY_SYSV_ABI int VideoOutLatencyMeasureSetStartPoint(int handle, uint32_t point);
|
||||
KYTY_SYSV_ABI int VideoOutColorSettingsSetGamma(VideoOutColorSettings* settings, float gamma);
|
||||
KYTY_SYSV_ABI int VideoOutAdjustColor(int handle, const VideoOutColorSettings* settings);
|
||||
|
||||
void VideoOutBeginVblank();
|
||||
void VideoOutEndVblank();
|
||||
bool VideoOutFlipWindow(uint32_t micros);
|
||||
|
||||
} // namespace Libs::VideoOut
|
||||
|
||||
#endif /* EMULATOR_INCLUDE_EMULATOR_GRAPHICS_VIDEOOUT_H_ */
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef EMULATOR_INCLUDE_EMULATOR_GRAPHICS_WINDOW_H_
|
||||
#define EMULATOR_INCLUDE_EMULATOR_GRAPHICS_WINDOW_H_
|
||||
|
||||
#include "common/abi.h"
|
||||
#include "common/common.h"
|
||||
|
||||
struct VkSurfaceCapabilitiesKHR;
|
||||
|
||||
namespace Libs::Graphics {
|
||||
|
||||
struct GraphicContext;
|
||||
struct VideoOutVulkanImage;
|
||||
|
||||
VkSurfaceCapabilitiesKHR* VulkanGetSurfaceCapabilities();
|
||||
|
||||
GraphicContext* WindowGetGraphicContext();
|
||||
|
||||
void WindowInit(uint32_t width, uint32_t height);
|
||||
void WindowRun();
|
||||
void WindowWaitForGraphicInitialized();
|
||||
void WindowDrawBuffer(VideoOutVulkanImage* image);
|
||||
|
||||
} // namespace Libs::Graphics
|
||||
|
||||
#endif /* EMULATOR_INCLUDE_EMULATOR_GRAPHICS_WINDOW_H_ */
|
||||
@@ -0,0 +1,389 @@
|
||||
#include "SDL.h"
|
||||
#include "SDL_error.h"
|
||||
#include "SDL_events.h"
|
||||
#include "SDL_gamecontroller.h"
|
||||
#include "SDL_hints.h"
|
||||
#include "SDL_joystick.h"
|
||||
#include "SDL_keyboard.h"
|
||||
#include "SDL_keycode.h"
|
||||
#include "SDL_mouse.h"
|
||||
#include "SDL_pixels.h"
|
||||
#include "SDL_rwops.h"
|
||||
#include "SDL_stdinc.h"
|
||||
#include "SDL_surface.h"
|
||||
#include "SDL_thread.h"
|
||||
#include "SDL_touch.h"
|
||||
#include "SDL_video.h"
|
||||
#include "SDL_vulkan.h"
|
||||
#include "common/assert.h"
|
||||
#include "common/common.h"
|
||||
#include "common/emulatorConfig.h"
|
||||
#include "common/file.h"
|
||||
#include "common/logging/log.h"
|
||||
#include "common/profiler.h"
|
||||
#include "common/stringUtils.h"
|
||||
#include "common/subsystems.h"
|
||||
#include "common/systemInfo.h"
|
||||
#include "common/threads.h"
|
||||
#include "common/timer.h"
|
||||
#include "graphics/host_gpu/graphicContext.h"
|
||||
#include "graphics/host_gpu/renderer/render.h"
|
||||
#include "graphics/host_gpu/utils.h"
|
||||
#include "graphics/host_gpu/vma.h"
|
||||
#include "graphics/presentation/renderDoc.h"
|
||||
#include "graphics/presentation/videoOut.h"
|
||||
#include "graphics/presentation/window.h"
|
||||
#include "graphics/presentation/window/windowInternal.h"
|
||||
#include "libs/controller.h"
|
||||
#include "loader/systemContent.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <fmt/format.h>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <vulkan/vk_enum_string_helper.h>
|
||||
#include <vulkan/vk_platform.h>
|
||||
|
||||
// IWYU pragma: no_include <intrin.h>
|
||||
|
||||
#define KYTY_ENABLE_DEBUG_PRINTF
|
||||
#define KYTY_DBG_INPUT
|
||||
|
||||
namespace Libs::Graphics {
|
||||
|
||||
VulkanSwapchain::~VulkanSwapchain() = default;
|
||||
|
||||
[[maybe_unused]] static VkSwapchainKHR VulkanCreateSwapchainInternal(
|
||||
VkDevice device, VkSurfaceKHR surface, uint32_t width, uint32_t height, uint32_t image_count,
|
||||
SurfaceCapabilities* r, VkFormat* swapchain_format, VkExtent2D* swapchain_extent,
|
||||
std::unique_ptr<VkImage[]>* swapchain_images,
|
||||
std::unique_ptr<VkImageView[]>* swapchain_image_views, uint32_t* swapchain_images_count) {
|
||||
EXIT_IF(device == nullptr);
|
||||
EXIT_IF(surface == nullptr);
|
||||
EXIT_IF(r == nullptr);
|
||||
EXIT_IF(swapchain_format == nullptr);
|
||||
EXIT_IF(swapchain_extent == nullptr);
|
||||
EXIT_IF(swapchain_images == nullptr);
|
||||
EXIT_IF(swapchain_image_views == nullptr);
|
||||
EXIT_IF(swapchain_images_count == nullptr);
|
||||
|
||||
EXIT_NOT_IMPLEMENTED(r->formats.empty());
|
||||
|
||||
VkExtent2D extent {};
|
||||
extent.width = std::clamp(width, r->capabilities.minImageExtent.width,
|
||||
r->capabilities.maxImageExtent.width);
|
||||
extent.height = std::clamp(height, r->capabilities.minImageExtent.height,
|
||||
r->capabilities.maxImageExtent.height);
|
||||
|
||||
image_count =
|
||||
std::clamp(image_count, r->capabilities.minImageCount, r->capabilities.maxImageCount);
|
||||
|
||||
VkSwapchainCreateInfoKHR create_info {};
|
||||
create_info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
|
||||
create_info.pNext = nullptr;
|
||||
create_info.flags = 0;
|
||||
create_info.surface = surface;
|
||||
create_info.minImageCount = image_count;
|
||||
|
||||
if (r->format_unorm_bgra32) {
|
||||
create_info.imageFormat = VK_FORMAT_B8G8R8A8_UNORM;
|
||||
create_info.imageColorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
|
||||
} else if (r->format_srgb_bgra32) {
|
||||
create_info.imageFormat = VK_FORMAT_B8G8R8A8_SRGB;
|
||||
create_info.imageColorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
|
||||
} else {
|
||||
const auto& format = r->formats[0];
|
||||
create_info.imageFormat = format.format;
|
||||
create_info.imageColorSpace = format.colorSpace;
|
||||
}
|
||||
|
||||
create_info.imageExtent = extent;
|
||||
create_info.imageArrayLayers = 1;
|
||||
create_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
|
||||
create_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||
create_info.queueFamilyIndexCount = 0;
|
||||
create_info.pQueueFamilyIndices = nullptr;
|
||||
create_info.preTransform = r->capabilities.currentTransform;
|
||||
create_info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
|
||||
create_info.presentMode = VK_PRESENT_MODE_FIFO_KHR;
|
||||
create_info.clipped = VK_TRUE;
|
||||
create_info.oldSwapchain = nullptr;
|
||||
|
||||
*swapchain_format = create_info.imageFormat;
|
||||
*swapchain_extent = extent;
|
||||
|
||||
VkSwapchainKHR swapchain = nullptr;
|
||||
|
||||
vkCreateSwapchainKHR(device, &create_info, nullptr, &swapchain);
|
||||
|
||||
vkGetSwapchainImagesKHR(device, swapchain, swapchain_images_count, nullptr);
|
||||
EXIT_NOT_IMPLEMENTED(*swapchain_images_count == 0);
|
||||
|
||||
*swapchain_images = std::make_unique<VkImage[]>(*swapchain_images_count);
|
||||
vkGetSwapchainImagesKHR(device, swapchain, swapchain_images_count, swapchain_images->get());
|
||||
|
||||
*swapchain_image_views = std::make_unique<VkImageView[]>(*swapchain_images_count);
|
||||
for (uint32_t i = 0; i < *swapchain_images_count; i++) {
|
||||
VkImageViewCreateInfo create_info {};
|
||||
create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
||||
create_info.pNext = nullptr;
|
||||
create_info.flags = 0;
|
||||
create_info.image = (*swapchain_images)[i];
|
||||
create_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
||||
create_info.format = *swapchain_format;
|
||||
create_info.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
|
||||
create_info.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
|
||||
create_info.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
|
||||
create_info.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
|
||||
create_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
create_info.subresourceRange.baseArrayLayer = 0;
|
||||
create_info.subresourceRange.baseMipLevel = 0;
|
||||
create_info.subresourceRange.layerCount = 1;
|
||||
create_info.subresourceRange.levelCount = 1;
|
||||
|
||||
vkCreateImageView(device, &create_info, nullptr, &((*swapchain_image_views)[i]));
|
||||
}
|
||||
|
||||
return swapchain;
|
||||
}
|
||||
|
||||
VulkanSwapchain* VulkanCreateSwapchain(GraphicContext* ctx, uint32_t image_count) {
|
||||
EXIT_IF(g_window_ctx == nullptr);
|
||||
EXIT_IF(ctx == nullptr);
|
||||
EXIT_IF(ctx->screen_width == 0);
|
||||
EXIT_IF(ctx->screen_height == 0);
|
||||
|
||||
Common::LockGuard lock(g_window_ctx->mutex);
|
||||
|
||||
auto swapchain_owner = std::make_unique<VulkanSwapchain>();
|
||||
auto* s = swapchain_owner.get();
|
||||
|
||||
s->swapchain = VulkanCreateSwapchainInternal(
|
||||
ctx->device, g_window_ctx->surface, ctx->screen_width, ctx->screen_height, image_count,
|
||||
g_window_ctx->surface_capabilities, &s->swapchain_format, &s->swapchain_extent,
|
||||
&s->swapchain_images, &s->swapchain_image_views, &s->swapchain_images_count);
|
||||
if (s->swapchain == nullptr) {
|
||||
EXIT("Could not create swapchain");
|
||||
}
|
||||
|
||||
s->current_index = static_cast<uint32_t>(-1);
|
||||
s->present_frame = 0;
|
||||
|
||||
VkSemaphoreCreateInfo semaphore_info {};
|
||||
semaphore_info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
|
||||
semaphore_info.pNext = nullptr;
|
||||
semaphore_info.flags = 0;
|
||||
|
||||
s->image_acquired_semaphores = std::make_unique<VkSemaphore[]>(s->swapchain_images_count);
|
||||
s->render_complete_semaphores = std::make_unique<VkSemaphore[]>(s->swapchain_images_count);
|
||||
for (uint32_t i = 0; i < s->swapchain_images_count; i++) {
|
||||
auto result = vkCreateSemaphore(ctx->device, &semaphore_info, nullptr,
|
||||
&s->image_acquired_semaphores[i]);
|
||||
EXIT_NOT_IMPLEMENTED(result != VK_SUCCESS);
|
||||
|
||||
result = vkCreateSemaphore(ctx->device, &semaphore_info, nullptr,
|
||||
&s->render_complete_semaphores[i]);
|
||||
EXIT_NOT_IMPLEMENTED(result != VK_SUCCESS);
|
||||
}
|
||||
|
||||
return swapchain_owner.release();
|
||||
}
|
||||
|
||||
static void VulkanDeleteSwapchain(GraphicContext* ctx, VulkanSwapchain* s) {
|
||||
if (ctx == nullptr || s == nullptr) {
|
||||
return;
|
||||
}
|
||||
auto swapchain_owner = std::unique_ptr<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) {
|
||||
vkDestroySemaphore(ctx->device, s->image_acquired_semaphores[i], nullptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (s->render_complete_semaphores != nullptr) {
|
||||
for (uint32_t i = 0; i < s->swapchain_images_count; i++) {
|
||||
if (s->render_complete_semaphores[i] != nullptr) {
|
||||
vkDestroySemaphore(ctx->device, s->render_complete_semaphores[i], nullptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (s->swapchain_image_views != nullptr) {
|
||||
for (uint32_t i = 0; i < s->swapchain_images_count; i++) {
|
||||
if (s->swapchain_image_views[i] != nullptr) {
|
||||
vkDestroyImageView(ctx->device, s->swapchain_image_views[i], nullptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (s->swapchain != nullptr) {
|
||||
vkDestroySwapchainKHR(ctx->device, s->swapchain, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
static void VulkanRefreshSurfaceSize() {
|
||||
EXIT_IF(g_window_ctx == nullptr);
|
||||
EXIT_IF(g_window_ctx->window == nullptr);
|
||||
EXIT_IF(g_window_ctx->surface_capabilities == nullptr);
|
||||
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
SDL_Vulkan_GetDrawableSize(g_window_ctx->window, &width, &height);
|
||||
if (width > 0 && height > 0) {
|
||||
g_window_ctx->graphic_ctx.screen_width = static_cast<uint32_t>(width);
|
||||
g_window_ctx->graphic_ctx.screen_height = static_cast<uint32_t>(height);
|
||||
}
|
||||
|
||||
VulkanGetSurfaceCapabilities(g_window_ctx->graphic_ctx.physical_device, g_window_ctx->surface,
|
||||
g_window_ctx->surface_capabilities);
|
||||
}
|
||||
|
||||
static void VulkanRecreateSwapchain() {
|
||||
EXIT_IF(g_window_ctx == nullptr);
|
||||
|
||||
LOGF("Recreating Vulkan swapchain\n");
|
||||
VulkanRefreshSurfaceSize();
|
||||
VulkanDeleteSwapchain(&g_window_ctx->graphic_ctx, g_window_ctx->swapchain);
|
||||
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);
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
void WindowDrawBuffer(VideoOutVulkanImage* image) {
|
||||
KYTY_PROFILER_FUNCTION();
|
||||
|
||||
EXIT_IF(image == nullptr);
|
||||
EXIT_IF(g_window_ctx == nullptr);
|
||||
EXIT_IF(g_window_ctx->swapchain == nullptr);
|
||||
|
||||
if (g_window_ctx->window_hidden) {
|
||||
WindowUpdateIcon();
|
||||
|
||||
SDL_ShowWindow(g_window_ctx->window);
|
||||
|
||||
g_window_ctx->window_hidden = false;
|
||||
VulkanRecreateSwapchain();
|
||||
}
|
||||
|
||||
auto* swapchain = g_window_ctx->swapchain;
|
||||
|
||||
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(
|
||||
g_window_ctx->graphic_ctx.device, swapchain->swapchain, UINT64_MAX,
|
||||
swapchain->image_acquired_semaphores[present_frame], nullptr, &swapchain->current_index);
|
||||
|
||||
switch (result) {
|
||||
case VK_SUCCESS: break;
|
||||
case VK_SUBOPTIMAL_KHR: LOGF("vkAcquireNextImageKHR returned VK_SUBOPTIMAL_KHR\n"); break;
|
||||
case VK_ERROR_OUT_OF_DATE_KHR:
|
||||
LOGF("vkAcquireNextImageKHR returned VK_ERROR_OUT_OF_DATE_KHR\n");
|
||||
VulkanRecreateSwapchain();
|
||||
return;
|
||||
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());
|
||||
|
||||
auto* vk_buffer = buffer.GetPool()->buffers[buffer.GetIndex()];
|
||||
|
||||
buffer.Begin();
|
||||
|
||||
UtilBlitImage(&buffer, image, swapchain);
|
||||
|
||||
VkImageMemoryBarrier pre_present_barrier {};
|
||||
pre_present_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
|
||||
pre_present_barrier.pNext = nullptr;
|
||||
pre_present_barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
||||
pre_present_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
|
||||
pre_present_barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
||||
pre_present_barrier.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
|
||||
pre_present_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
||||
pre_present_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
||||
pre_present_barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
pre_present_barrier.subresourceRange.baseMipLevel = 0;
|
||||
pre_present_barrier.subresourceRange.levelCount = 1;
|
||||
pre_present_barrier.subresourceRange.baseArrayLayer = 0;
|
||||
pre_present_barrier.subresourceRange.layerCount = 1;
|
||||
pre_present_barrier.image = swapchain->swapchain_images[swapchain->current_index];
|
||||
vkCmdPipelineBarrier(vk_buffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
|
||||
VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, nullptr, 0, nullptr, 1,
|
||||
&pre_present_barrier);
|
||||
|
||||
buffer.End();
|
||||
|
||||
auto render_complete_semaphore =
|
||||
swapchain->render_complete_semaphores[swapchain->current_index];
|
||||
const VkPipelineStageFlags wait_stage = VK_PIPELINE_STAGE_TRANSFER_BIT;
|
||||
buffer.ExecuteWithSemaphore(swapchain->image_acquired_semaphores[present_frame], wait_stage,
|
||||
render_complete_semaphore);
|
||||
|
||||
VkPresentInfoKHR present;
|
||||
present.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
|
||||
present.pNext = nullptr;
|
||||
present.swapchainCount = 1;
|
||||
present.pSwapchains = &swapchain->swapchain;
|
||||
present.pImageIndices = &swapchain->current_index;
|
||||
present.pWaitSemaphores = &render_complete_semaphore;
|
||||
present.waitSemaphoreCount = 1;
|
||||
present.pResults = nullptr;
|
||||
|
||||
const auto& queue = g_window_ctx->graphic_ctx.queues[GraphicContext::QUEUE_PRESENT];
|
||||
|
||||
if (queue.mutex != nullptr) {
|
||||
queue.mutex->Lock();
|
||||
}
|
||||
result = vkQueuePresentKHR(queue.vk_queue, &present);
|
||||
if (queue.mutex != nullptr) {
|
||||
queue.mutex->Unlock();
|
||||
}
|
||||
switch (result) {
|
||||
case VK_SUCCESS: break;
|
||||
case VK_SUBOPTIMAL_KHR: LOGF("vkQueuePresentKHR returned VK_SUBOPTIMAL_KHR\n"); break;
|
||||
case VK_ERROR_OUT_OF_DATE_KHR:
|
||||
LOGF("vkQueuePresentKHR returned VK_ERROR_OUT_OF_DATE_KHR\n");
|
||||
VulkanRecreateSwapchain();
|
||||
return;
|
||||
default: EXIT("vkQueuePresentKHR failed: %s\n", string_VkResult(result));
|
||||
}
|
||||
|
||||
swapchain->present_frame = (present_frame + 1u) % swapchain->swapchain_images_count;
|
||||
|
||||
RenderDocOnPresent();
|
||||
WindowUpdateTitle();
|
||||
}
|
||||
|
||||
} // namespace Libs::Graphics
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,53 @@
|
||||
#ifndef EMULATOR_SRC_GRAPHICS_PRESENTATION_WINDOW_WINDOWINTERNAL_H_
|
||||
#define EMULATOR_SRC_GRAPHICS_PRESENTATION_WINDOW_WINDOWINTERNAL_H_
|
||||
|
||||
#include "SDL_video.h"
|
||||
#include "common/threads.h"
|
||||
#include "graphics/host_gpu/graphicContext.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include <vulkan/vulkan_core.h>
|
||||
|
||||
namespace Libs::Graphics {
|
||||
|
||||
struct WindowGame;
|
||||
|
||||
struct SurfaceCapabilities {
|
||||
VkSurfaceCapabilitiesKHR capabilities {};
|
||||
std::vector<VkSurfaceFormatKHR> formats;
|
||||
std::vector<VkPresentModeKHR> present_modes;
|
||||
bool format_srgb_bgra32 = false;
|
||||
bool format_unorm_bgra32 = false;
|
||||
};
|
||||
|
||||
struct WindowContext {
|
||||
GraphicContext graphic_ctx;
|
||||
VulkanSwapchain* swapchain = nullptr;
|
||||
SDL_Window* window = nullptr;
|
||||
bool window_hidden = true;
|
||||
VkSurfaceKHR surface = nullptr;
|
||||
SurfaceCapabilities* surface_capabilities = nullptr;
|
||||
WindowGame* game = nullptr;
|
||||
|
||||
char device_name[VK_MAX_PHYSICAL_DEVICE_NAME_SIZE] = {0};
|
||||
char processor_name[64] = {0};
|
||||
|
||||
Common::Mutex mutex;
|
||||
bool graphic_initialized = false;
|
||||
Common::CondVar graphic_initialized_condvar;
|
||||
};
|
||||
|
||||
extern WindowContext* g_window_ctx;
|
||||
|
||||
void VulkanGetSurfaceCapabilities(VkPhysicalDevice physical_device, VkSurfaceKHR surface,
|
||||
SurfaceCapabilities* capabilities);
|
||||
VulkanSwapchain* VulkanCreateSwapchain(GraphicContext* ctx, uint32_t image_count);
|
||||
void VulkanCreate(WindowContext* ctx);
|
||||
|
||||
void WindowUpdateIcon();
|
||||
void WindowUpdateTitle();
|
||||
|
||||
} // namespace Libs::Graphics
|
||||
|
||||
#endif // EMULATOR_SRC_GRAPHICS_PRESENTATION_WINDOW_WINDOWINTERNAL_H_
|
||||
Reference in New Issue
Block a user