It renders something, crashes because ImGui exhausts the display list. Need to find a better way for thread sync. Message queue?

This commit is contained in:
irisz64
2025-05-22 09:16:58 +02:00
parent 668edbcd91
commit 326b4b43cd
7 changed files with 25 additions and 43 deletions

View File

@@ -6,6 +6,7 @@
#include <resources/vert.spv.h> #include <resources/vert.spv.h>
#include <resources/frag.spv.h> #include <resources/frag.spv.h>
#include <KaizenGui.hpp> #include <KaizenGui.hpp>
#include <imgui_impl_vulkan.h>
using namespace Vulkan; using namespace Vulkan;
using namespace RDP; using namespace RDP;
@@ -157,7 +158,7 @@ void ParallelRDP::DrawFullscreenTexturedQuad(Util::IntrusivePtr<Image> image,
cmd->draw(3, 1); cmd->draw(3, 1);
} }
void ParallelRDP::UpdateScreen(Util::IntrusivePtr<Image> image, KaizenGui& kaizenGui) const { void ParallelRDP::UpdateScreen(Util::IntrusivePtr<Image> image) const {
wsi->begin_frame(); wsi->begin_frame();
if (!image) { if (!image) {
@@ -183,13 +184,15 @@ void ParallelRDP::UpdateScreen(Util::IntrusivePtr<Image> image, KaizenGui& kaize
cmd->begin_render_pass(wsi->get_device().get_swapchain_render_pass(SwapchainRenderPass::ColorOnly)); cmd->begin_render_pass(wsi->get_device().get_swapchain_render_pass(SwapchainRenderPass::ColorOnly));
DrawFullscreenTexturedQuad(image, cmd); DrawFullscreenTexturedQuad(image, cmd);
kaizenGui.RenderUI(); auto drawData = ImGui::GetDrawData();
if(drawData)
ImGui_ImplVulkan_RenderDrawData(drawData, cmd->get_command_buffer());
cmd->end_render_pass(); cmd->end_render_pass();
wsi->get_device().submit(cmd); wsi->get_device().submit(cmd);
wsi->end_frame(); wsi->end_frame();
} }
void ParallelRDP::UpdateScreen(const n64::VI &vi, KaizenGui& kaizenGui, bool playing) const { void ParallelRDP::UpdateScreen(const n64::VI &vi, bool playing) const {
if(playing) { if(playing) {
command_processor->set_vi_register(VIRegister::Control, vi.status.raw); command_processor->set_vi_register(VIRegister::Control, vi.status.raw);
command_processor->set_vi_register(VIRegister::Origin, vi.origin); command_processor->set_vi_register(VIRegister::Origin, vi.origin);
@@ -215,12 +218,12 @@ void ParallelRDP::UpdateScreen(const n64::VI &vi, KaizenGui& kaizenGui, bool pla
opts.downscale_steps = true; opts.downscale_steps = true;
opts.crop_overscan_pixels = true; opts.crop_overscan_pixels = true;
Util::IntrusivePtr<Image> image = command_processor->scanout(opts); Util::IntrusivePtr<Image> image = command_processor->scanout(opts);
UpdateScreen(image, kaizenGui); UpdateScreen(image);
command_processor->begin_frame_context(); command_processor->begin_frame_context();
return; return;
} }
UpdateScreen(static_cast<Util::IntrusivePtr<Image>>(nullptr), kaizenGui); UpdateScreen(static_cast<Util::IntrusivePtr<Image>>(nullptr));
} }
void ParallelRDP::EnqueueCommand(int command_length, const u32 *buffer) const { void ParallelRDP::EnqueueCommand(int command_length, const u32 *buffer) const {

View File

@@ -7,8 +7,6 @@ namespace n64 {
struct VI; struct VI;
} }
class KaizenGui;
class ParallelRDP { class ParallelRDP {
public: public:
class WindowInfo { class WindowInfo {
@@ -25,7 +23,7 @@ public:
const std::shared_ptr<WindowInfo> &, const u8 *); const std::shared_ptr<WindowInfo> &, const u8 *);
ParallelRDP() = default; ParallelRDP() = default;
void UpdateScreen(const n64::VI &, KaizenGui&, bool = true) const; void UpdateScreen(const n64::VI &, bool = true) const;
void EnqueueCommand(int, const u32 *) const; void EnqueueCommand(int, const u32 *) const;
void OnFullSync() const; void OnFullSync() const;
bool IsFramerateUnlocked() const; bool IsFramerateUnlocked() const;
@@ -38,5 +36,5 @@ public:
private: private:
void LoadWSIPlatform(const std::shared_ptr<Vulkan::WSIPlatform> &, const std::shared_ptr<WindowInfo> &); void LoadWSIPlatform(const std::shared_ptr<Vulkan::WSIPlatform> &, const std::shared_ptr<WindowInfo> &);
void DrawFullscreenTexturedQuad(Util::IntrusivePtr<Vulkan::Image>, Util::IntrusivePtr<Vulkan::CommandBuffer>) const; void DrawFullscreenTexturedQuad(Util::IntrusivePtr<Vulkan::Image>, Util::IntrusivePtr<Vulkan::CommandBuffer>) const;
void UpdateScreen(Util::IntrusivePtr<Vulkan::Image>, KaizenGui&) const; void UpdateScreen(Util::IntrusivePtr<Vulkan::Image>) const;
}; };

View File

@@ -3,10 +3,12 @@
#include <KaizenGui.hpp> #include <KaizenGui.hpp>
EmuThread::EmuThread(const std::shared_ptr<n64::Core> &core, double &fps, RenderWidget &renderWidget, EmuThread::EmuThread(const std::shared_ptr<n64::Core> &core, double &fps, RenderWidget &renderWidget,
SettingsWindow &settings, KaizenGui& kaizenGui) noexcept : SettingsWindow &settings) noexcept :
renderWidget(renderWidget), core(core), settings(settings), fps(fps) { renderWidget(renderWidget), core(core), settings(settings), fps(fps) {
thread = std::thread([&]() { thread = std::thread([&]() {
core->parallel.Init(renderWidget.wsiPlatform, renderWidget.windowInfo, core->cpu->GetMem().GetRDRAMPtr());
parallelRDPInitialized = true;
isRunning = true; isRunning = true;
auto lastSample = std::chrono::high_resolution_clock::now(); auto lastSample = std::chrono::high_resolution_clock::now();
@@ -18,26 +20,8 @@ EmuThread::EmuThread(const std::shared_ptr<n64::Core> &core, double &fps, Render
while (!interruptionRequested) { while (!interruptionRequested) {
if(!started) { if(!started) {
const auto startFrameTime = std::chrono::high_resolution_clock::now(); core->parallel.UpdateScreen(core->cpu->GetMem().mmio.vi, false);
core->parallel.UpdateScreen(core->cpu->GetMem().mmio.vi, kaizenGui, false); fps = -1.0;
const auto endFrameTime = std::chrono::high_resolution_clock::now();
using namespace std::chrono_literals;
const auto frameTimeMs = std::chrono::duration<double>(endFrameTime - startFrameTime) / 1ms;
avgFps += frameTimeMs;
sampledFps++;
if (const auto elapsedSinceLastSample = std::chrono::duration<double>(endFrameTime - lastSample) / 1s;
elapsedSinceLastSample >= 1.0) {
if (!oneSecondPassed) {
oneSecondPassed = true;
continue;
}
lastSample = endFrameTime;
avgFps /= sampledFps;
sampledFps = 0;
fps = 1000.0 / avgFps;
}
continue; continue;
} }
@@ -50,7 +34,7 @@ EmuThread::EmuThread(const std::shared_ptr<n64::Core> &core, double &fps, Render
} }
if (core->render) { if (core->render) {
core->parallel.UpdateScreen(core->cpu->GetMem().mmio.vi, kaizenGui); core->parallel.UpdateScreen(core->cpu->GetMem().mmio.vi);
} }
const auto endFrameTime = std::chrono::high_resolution_clock::now(); const auto endFrameTime = std::chrono::high_resolution_clock::now();

View File

@@ -8,13 +8,11 @@ namespace n64 {
struct Core; struct Core;
} }
class KaizenGui;
class EmuThread final { class EmuThread final {
RenderWidget &renderWidget; RenderWidget &renderWidget;
bool started = false; bool started = false;
public: public:
explicit EmuThread(const std::shared_ptr<n64::Core> &, double &, RenderWidget &, SettingsWindow &, KaizenGui&) noexcept; explicit EmuThread(const std::shared_ptr<n64::Core> &, double &, RenderWidget &, SettingsWindow &) noexcept;
void start() noexcept; void start() noexcept;
void TogglePause() const noexcept; void TogglePause() const noexcept;
@@ -23,7 +21,7 @@ public:
void Stop() const noexcept; void Stop() const noexcept;
void requestInterruption() { interruptionRequested = true; } void requestInterruption() { interruptionRequested = true; }
bool interruptionRequested = false, isRunning = false; bool interruptionRequested = false, isRunning = false, parallelRDPInitialized = false;
std::shared_ptr<n64::Core> core; std::shared_ptr<n64::Core> core;
SettingsWindow &settings; SettingsWindow &settings;
double& fps; double& fps;

View File

@@ -6,8 +6,6 @@
#include <utils/log.hpp> #include <utils/log.hpp>
#include <memory> #include <memory>
class KaizenGui;
namespace gui { namespace gui {
static VkAllocationCallbacks* g_Allocator = NULL; static VkAllocationCallbacks* g_Allocator = NULL;
static VkInstance g_Instance = VK_NULL_HANDLE; static VkInstance g_Instance = VK_NULL_HANDLE;
@@ -151,7 +149,5 @@ namespace gui {
inline void EndFrame() { inline void EndFrame() {
ImGui::Render(); ImGui::Render();
auto cmd = g_Wsi->get_device().request_command_buffer();
ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), cmd->get_command_buffer());
} }
} }

View File

@@ -4,8 +4,8 @@
#include <ImGuiImpl/StatusBar.hpp> #include <ImGuiImpl/StatusBar.hpp>
#include <ImGuiImpl/GUI.hpp> #include <ImGuiImpl/GUI.hpp>
KaizenGui::KaizenGui() noexcept : window("Kaizen", 1280, 720), core(std::make_shared<n64::Core>()), vulkanWidget(core, window.getHandle()), emuThread(core, fpsCounter, vulkanWidget, settingsWindow, *this) { KaizenGui::KaizenGui() noexcept : window("Kaizen", 1280, 720), core(std::make_shared<n64::Core>()), vulkanWidget(core, window.getHandle()), emuThread(core, fpsCounter, vulkanWidget, settingsWindow) {
core->parallel.Init(vulkanWidget.wsiPlatform, vulkanWidget.windowInfo, core->cpu->GetMem().GetRDRAMPtr()); while(!emuThread.parallelRDPInitialized);
gui::Initialize(core->parallel.wsi, window.getHandle()); gui::Initialize(core->parallel.wsi, window.getHandle());
emuExitFunc = [&]() { emuExitFunc = [&]() {
@@ -27,7 +27,8 @@ KaizenGui::KaizenGui() noexcept : window("Kaizen", 1280, 720), core(std::make_sh
}); });
statusBar.setFunc([&]() { statusBar.setFunc([&]() {
ImGui::Text("GUI FPS: %.2f, Emulation FPS: %.2f", ImGui::GetIO().Framerate, fpsCounter); ImGui::Text("GUI FPS: %.2f, Emulation FPS: %s", ImGui::GetIO().Framerate,
fmt::format(fpsCounter > 0 ? fmt::runtime("{:.2f{0}}") : fmt::runtime("{1}"), fpsCounter, "Not playing").c_str());
}); });
menuBar.addMenu({"File", menuBar.addMenu({"File",
@@ -102,6 +103,8 @@ void KaizenGui::handleEvents() {
int KaizenGui::run() { int KaizenGui::run() {
while(!quit) { while(!quit) {
handleEvents(); handleEvents();
RenderUI();
} }
return 0; return 0;

View File

@@ -20,7 +20,6 @@ public:
EmuThread emuThread; EmuThread emuThread;
gui::PopupWindow about{"About Kaizen"}; gui::PopupWindow about{"About Kaizen"};
gui::StatusBar statusBar{}; gui::StatusBar statusBar{};
void RenderUI();
int run(); int run();
void LoadTAS(const std::string &path) const noexcept; void LoadTAS(const std::string &path) const noexcept;
@@ -29,4 +28,5 @@ private:
bool quit = false; bool quit = false;
void handleEvents(); void handleEvents();
std::function<void()> emuExitFunc; std::function<void()> emuExitFunc;
void RenderUI();
}; };