Prevent SDL events from getting flushed outside the main thread.

This commit is contained in:
Skyth 2024-12-25 22:15:01 +03:00
parent b054e16461
commit 0b5281d652
2 changed files with 26 additions and 5 deletions

View file

@ -35,6 +35,8 @@ PPC_FUNC(sub_824EB490)
__imp__sub_824EB490(ctx, base); __imp__sub_824EB490(ctx, base);
} }
static std::thread::id g_mainThreadId = std::this_thread::get_id();
// CApplication::Update // CApplication::Update
PPC_FUNC_IMPL(__imp__sub_822C1130); PPC_FUNC_IMPL(__imp__sub_822C1130);
PPC_FUNC(sub_822C1130) PPC_FUNC(sub_822C1130)
@ -51,8 +53,15 @@ PPC_FUNC(sub_822C1130)
App::s_deltaTime = ctx.f1.f64; App::s_deltaTime = ctx.f1.f64;
SDL_PumpEvents(); // This function can also be called by the loading thread,
SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT); // which SDL does not like. To prevent the OS from thinking
// the process is unresponsive, we will flush while waiting
// for the pipelines to finish compiling in video.cpp.
if (std::this_thread::get_id() == g_mainThreadId)
{
SDL_PumpEvents();
SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
}
GameWindow::Update(); GameWindow::Update();
AudioPatches::Update(App::s_deltaTime); AudioPatches::Update(App::s_deltaTime);

View file

@ -562,7 +562,6 @@ static void DestructTempResources()
g_tempBuffers[g_frame].clear(); g_tempBuffers[g_frame].clear();
} }
static std::thread::id g_mainThreadId;
static std::thread::id g_presentThreadId = std::this_thread::get_id(); static std::thread::id g_presentThreadId = std::this_thread::get_id();
PPC_FUNC_IMPL(__imp__sub_824ECA00); PPC_FUNC_IMPL(__imp__sub_824ECA00);
@ -1482,8 +1481,6 @@ void Video::CreateHostDevice(const char *sdlVideoDriver)
for (auto& renderSemaphore : g_renderSemaphores) for (auto& renderSemaphore : g_renderSemaphores)
renderSemaphore = g_device->createCommandSemaphore(); renderSemaphore = g_device->createCommandSemaphore();
g_mainThreadId = std::this_thread::get_id();
RenderPipelineLayoutBuilder pipelineLayoutBuilder; RenderPipelineLayoutBuilder pipelineLayoutBuilder;
pipelineLayoutBuilder.begin(false, true); pipelineLayoutBuilder.begin(false, true);
@ -1742,6 +1739,8 @@ static void LockTextureRect(GuestTexture* texture, uint32_t, GuestLockedRect* lo
static void UnlockTextureRect(GuestTexture* texture) static void UnlockTextureRect(GuestTexture* texture)
{ {
assert(std::this_thread::get_id() == g_presentThreadId);
RenderCommand cmd; RenderCommand cmd;
cmd.type = RenderCommandType::UnlockTextureRect; cmd.type = RenderCommandType::UnlockTextureRect;
cmd.unlockTextureRect.texture = texture; cmd.unlockTextureRect.texture = texture;
@ -5540,14 +5539,27 @@ static void CompileParticleMaterialPipeline(const Hedgehog::Sparkle::CParticleMa
} }
} }
#ifdef _DEBUG
static std::thread::id g_mainThreadId = std::this_thread::get_id();
#endif
// SWA::CGameModeStage::ExitLoading // SWA::CGameModeStage::ExitLoading
PPC_FUNC_IMPL(__imp__sub_825369A0); PPC_FUNC_IMPL(__imp__sub_825369A0);
PPC_FUNC(sub_825369A0) PPC_FUNC(sub_825369A0)
{ {
assert(std::this_thread::get_id() == g_mainThreadId);
// Wait for pipeline compilations to finish. // Wait for pipeline compilations to finish.
uint32_t value; uint32_t value;
while ((value = g_compilingDataCount.load()) != 0) while ((value = g_compilingDataCount.load()) != 0)
{
// Pump SDL events to prevent the OS
// from thinking the process is unresponsive.
SDL_PumpEvents();
SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
g_compilingDataCount.wait(value); g_compilingDataCount.wait(value);
}
__imp__sub_825369A0(ctx, base); __imp__sub_825369A0(ctx, base);
} }