Skip to content

Commit

Permalink
System: Restore context after pumping messages
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Sep 3, 2023
1 parent 25fb695 commit 0076ab5
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 45 deletions.
10 changes: 5 additions & 5 deletions src/core/gpu.cpp
Expand Up @@ -309,7 +309,7 @@ bool GPU::DoState(StateWrapper& sw, GPUTexture** host_texture, bool update_displ
return !sw.HasError();
}

void GPU::RestoreGraphicsAPIState()
void GPU::RestoreDeviceContext()
{
}

Expand Down Expand Up @@ -1999,11 +1999,11 @@ bool GPU::WriteDisplayTextureToFile(std::string filename, bool full_resolution /
texture_data_stride))
{
Log_ErrorPrintf("Texture download failed");
RestoreGraphicsAPIState();
RestoreDeviceContext();
return false;
}

RestoreGraphicsAPIState();
RestoreDeviceContext();

auto fp = FileSystem::OpenManagedCFile(filename.c_str(), "wb");
if (!fp)
Expand Down Expand Up @@ -2055,13 +2055,13 @@ bool GPU::RenderScreenshotToBuffer(u32 width, u32 height, const Common::Rectangl
out_pixels->resize(width * height);
if (!g_gpu_device->DownloadTexture(render_texture.get(), 0, 0, width, height, out_pixels->data(), stride))
{
RestoreGraphicsAPIState();
RestoreDeviceContext();
return false;
}

*out_stride = stride;
*out_format = hdformat;
RestoreGraphicsAPIState();
RestoreDeviceContext();
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/gpu.h
Expand Up @@ -96,7 +96,7 @@ class GPU

// Graphics API state reset/restore - call when drawing the UI etc.
// TODO: replace with "invalidate cached state"
virtual void RestoreGraphicsAPIState();
virtual void RestoreDeviceContext();

// Render statistics debug window.
void DrawDebugStateWindow();
Expand Down
24 changes: 12 additions & 12 deletions src/core/gpu_hw.cpp
Expand Up @@ -215,7 +215,7 @@ bool GPU_HW::Initialize()
return false;
}

RestoreGraphicsAPIState();
RestoreDeviceContext();
return true;
}

Expand Down Expand Up @@ -290,7 +290,7 @@ bool GPU_HW::DoState(StateWrapper& sw, GPUTexture** host_texture, bool update_di
return true;
}

void GPU_HW::RestoreGraphicsAPIState()
void GPU_HW::RestoreDeviceContext()
{
g_gpu_device->SetTextureSampler(0, m_vram_read_texture.get(), g_gpu_device->GetNearestSampler());
g_gpu_device->SetFramebuffer(m_vram_framebuffer.get());
Expand Down Expand Up @@ -352,7 +352,7 @@ void GPU_HW::UpdateSettings(const Settings& old_settings)
// Back up VRAM if we're recreating the framebuffer.
if (framebuffer_changed)
{
RestoreGraphicsAPIState();
RestoreDeviceContext();
ReadVRAM(0, 0, VRAM_WIDTH, VRAM_HEIGHT);
DestroyBuffers();
}
Expand Down Expand Up @@ -397,7 +397,7 @@ void GPU_HW::UpdateSettings(const Settings& old_settings)
if (!CreateBuffers())
Panic("Failed to recreate buffers.");

RestoreGraphicsAPIState();
RestoreDeviceContext();
UpdateVRAM(0, 0, VRAM_WIDTH, VRAM_HEIGHT, m_vram_ptr, false, false);
UpdateDepthBufferFromMaskBit();
UpdateDisplay();
Expand Down Expand Up @@ -1896,7 +1896,7 @@ bool GPU_HW::BlitVRAMReplacementTexture(const TextureReplacementTexture* tex, u3
g_gpu_device->SetViewportAndScissor(dst_x, dst_y, width, height);
g_gpu_device->Draw(3, 0);

RestoreGraphicsAPIState();
RestoreDeviceContext();
return true;
}

Expand Down Expand Up @@ -2088,7 +2088,7 @@ void GPU_HW::FillVRAM(u32 x, u32 y, u32 width, u32 height, u32 color)
g_gpu_device->PushUniformBuffer(&uniforms, sizeof(uniforms));
g_gpu_device->Draw(3, 0);

RestoreGraphicsAPIState();
RestoreDeviceContext();
}

void GPU_HW::ReadVRAM(u32 x, u32 y, u32 width, u32 height)
Expand Down Expand Up @@ -2119,7 +2119,7 @@ void GPU_HW::ReadVRAM(u32 x, u32 y, u32 width, u32 height)
reinterpret_cast<u32*>(&m_vram_shadow[copy_rect.top * VRAM_WIDTH + copy_rect.left]),
VRAM_WIDTH * sizeof(u16));

RestoreGraphicsAPIState();
RestoreDeviceContext();
}

void GPU_HW::UpdateVRAM(u32 x, u32 y, u32 width, u32 height, const void* data, bool set_mask, bool check_mask)
Expand Down Expand Up @@ -2188,7 +2188,7 @@ void GPU_HW::UpdateVRAM(u32 x, u32 y, u32 width, u32 height, const void* data, b
g_gpu_device->SetTextureBuffer(0, m_vram_upload_buffer.get());
g_gpu_device->Draw(3, 0);

RestoreGraphicsAPIState();
RestoreDeviceContext();
}

void GPU_HW::CopyVRAM(u32 src_x, u32 src_y, u32 dst_x, u32 dst_y, u32 width, u32 height)
Expand Down Expand Up @@ -2254,7 +2254,7 @@ void GPU_HW::CopyVRAM(u32 src_x, u32 src_y, u32 dst_x, u32 dst_y, u32 width, u32
m_vram_copy_pipelines[BoolToUInt8(m_GPUSTAT.check_mask_before_draw && !m_pgxp_depth_buffer)].get());
g_gpu_device->PushUniformBuffer(&uniforms, sizeof(uniforms));
g_gpu_device->Draw(3, 0);
RestoreGraphicsAPIState();
RestoreDeviceContext();

if (m_GPUSTAT.check_mask_before_draw && !m_pgxp_depth_buffer)
m_current_depth++;
Expand Down Expand Up @@ -2532,7 +2532,7 @@ void GPU_HW::UpdateDisplay()
else
SetDisplayTexture(m_display_private_texture.get(), 0, 0, scaled_display_width, scaled_display_height);

RestoreGraphicsAPIState();
RestoreDeviceContext();
}
}
}
Expand Down Expand Up @@ -2635,7 +2635,7 @@ void GPU_HW::DownsampleFramebufferAdaptive(GPUTexture* source, u32 left, u32 top

GL_POP();

RestoreGraphicsAPIState();
RestoreDeviceContext();

SetDisplayTexture(m_downsample_render_texture.get(), 0, 0, width, height);
}
Expand All @@ -2656,7 +2656,7 @@ void GPU_HW::DownsampleFramebufferBoxFilter(GPUTexture* source, u32 left, u32 to
g_gpu_device->SetViewportAndScissor(ds_left, ds_top, ds_width, ds_height);
g_gpu_device->Draw(3, 0);

RestoreGraphicsAPIState();
RestoreDeviceContext();

SetDisplayTexture(m_downsample_render_texture.get(), ds_left, ds_top, ds_width, ds_height);
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/gpu_hw.h
Expand Up @@ -49,7 +49,7 @@ class GPU_HW final : public GPU
void Reset(bool clear_vram) override;
bool DoState(StateWrapper& sw, GPUTexture** host_texture, bool update_display) override;

void RestoreGraphicsAPIState() override;
void RestoreDeviceContext() override;

void UpdateSettings(const Settings& old_settings) override;
void UpdateResolutionScale() override final;
Expand Down
8 changes: 4 additions & 4 deletions src/core/hotkeys.cpp
Expand Up @@ -53,7 +53,7 @@ static void HotkeyModifyResolutionScale(s32 increment)

if (System::IsValid())
{
g_gpu->RestoreGraphicsAPIState();
g_gpu->RestoreDeviceContext();
g_gpu->UpdateSettings(old_settings);
System::ClearMemorySaveStates();
}
Expand Down Expand Up @@ -312,7 +312,7 @@ DEFINE_HOTKEY("TogglePGXP", TRANSLATE_NOOP("Hotkeys", "Graphics"), TRANSLATE_NOO
{
Settings old_settings = g_settings;
g_settings.gpu_pgxp_enable = !g_settings.gpu_pgxp_enable;
g_gpu->RestoreGraphicsAPIState();
g_gpu->RestoreDeviceContext();
g_gpu->UpdateSettings(old_settings);
System::ClearMemorySaveStates();
Host::AddKeyedOSDMessage("TogglePGXP",
Expand Down Expand Up @@ -382,7 +382,7 @@ DEFINE_HOTKEY("TogglePGXPDepth", TRANSLATE_NOOP("Hotkeys", "Graphics"),
const Settings old_settings = g_settings;
g_settings.gpu_pgxp_depth_buffer = !g_settings.gpu_pgxp_depth_buffer;

g_gpu->RestoreGraphicsAPIState();
g_gpu->RestoreDeviceContext();
g_gpu->UpdateSettings(old_settings);
System::ClearMemorySaveStates();
Host::AddKeyedOSDMessage("TogglePGXPDepth",
Expand All @@ -403,7 +403,7 @@ DEFINE_HOTKEY("TogglePGXPCPU", TRANSLATE_NOOP("Hotkeys", "Graphics"), TRANSLATE_
const Settings old_settings = g_settings;
g_settings.gpu_pgxp_cpu = !g_settings.gpu_pgxp_cpu;

g_gpu->RestoreGraphicsAPIState();
g_gpu->RestoreDeviceContext();
g_gpu->UpdateSettings(old_settings);
System::ClearMemorySaveStates();
Host::AddKeyedOSDMessage("TogglePGXPCPU",
Expand Down
23 changes: 13 additions & 10 deletions src/core/system.cpp
Expand Up @@ -876,7 +876,7 @@ std::string System::GetInputProfilePath(const std::string_view& name)
bool System::RecreateGPU(GPURenderer renderer, bool force_recreate_device, bool update_display /* = true*/)
{
ClearMemorySaveStates();
g_gpu->RestoreGraphicsAPIState();
g_gpu->RestoreDeviceContext();

// save current state
std::unique_ptr<ByteStream> state_stream = ByteStream::CreateGrowableMemoryStream();
Expand All @@ -903,7 +903,7 @@ bool System::RecreateGPU(GPURenderer renderer, bool force_recreate_device, bool
{
state_stream->SeekAbsolute(0);
sw.SetMode(StateWrapper::Mode::Read);
g_gpu->RestoreGraphicsAPIState();
g_gpu->RestoreDeviceContext();
g_gpu->DoState(sw, nullptr, update_display);
TimingEvents::DoState(sw);
}
Expand Down Expand Up @@ -1724,7 +1724,7 @@ void System::Execute()
s_system_executing = true;

// TODO: Purge reset/restore
g_gpu->RestoreGraphicsAPIState();
g_gpu->RestoreDeviceContext();

if (s_rewind_load_counter >= 0)
DoRewind();
Expand Down Expand Up @@ -1800,6 +1800,7 @@ void System::FrameDone()
// counter-acts that.
Host::PumpMessagesOnCPUThread();
InputManager::PollSources();
g_gpu->RestoreDeviceContext();

if (IsExecutionInterrupted())
{
Expand Down Expand Up @@ -1846,6 +1847,8 @@ void System::FrameDone()
}
}

g_gpu->RestoreDeviceContext();

// Update perf counters *after* throttling, we want to measure from start-of-frame
// to start-of-frame, not end-of-frame to end-of-frame (will be noisy due to different
// amounts of computation happening in each frame).
Expand Down Expand Up @@ -1911,7 +1914,7 @@ void System::SingleStepCPU()
s_frame_timer.Reset();
s_system_executing = true;

g_gpu->RestoreGraphicsAPIState();
g_gpu->RestoreDeviceContext();

CPU::SingleStep();

Expand Down Expand Up @@ -2068,7 +2071,7 @@ bool System::DoState(StateWrapper& sw, GPUTexture** host_texture, bool update_di
if (!sw.DoMarker("InterruptController") || !InterruptController::DoState(sw))
return false;

g_gpu->RestoreGraphicsAPIState();
g_gpu->RestoreDeviceContext();
if (!sw.DoMarker("GPU") || !g_gpu->DoState(sw, host_texture, update_display))
return false;

Expand Down Expand Up @@ -2464,7 +2467,7 @@ bool System::SaveStateToStream(ByteStream* state, u32 screenshot_size /* = 256 *
{
header.offset_to_data = static_cast<u32>(state->GetPosition());

g_gpu->RestoreGraphicsAPIState();
g_gpu->RestoreDeviceContext();

header.data_compression_type = compression_method;

Expand Down Expand Up @@ -3220,7 +3223,7 @@ bool System::DumpVRAM(const char* filename)
if (!IsValid())
return false;

g_gpu->RestoreGraphicsAPIState();
g_gpu->RestoreDeviceContext();
return g_gpu->DumpVRAMToFile(filename);
}

Expand Down Expand Up @@ -4707,15 +4710,15 @@ bool System::PresentDisplay(bool allow_skip_present)

ImGuiManager::NewFrame();

if (g_gpu)
g_gpu->RestoreGraphicsAPIState();

return do_present;
}

void System::InvalidateDisplay()
{
PresentDisplay(false);

if (g_gpu)
g_gpu->RestoreDeviceContext();
}

void System::SetTimerResolutionIncreased(bool enabled)
Expand Down
12 changes: 0 additions & 12 deletions src/util/vulkan_device.cpp
Expand Up @@ -1916,18 +1916,6 @@ bool VulkanDevice::UpdateWindow()
SubmitCommandBuffer(false);
WaitForGPUIdle();

// recreate surface in existing swap chain if it already exists
if (m_swap_chain)
{
if (m_swap_chain->RecreateSurface(m_window_info))
{
m_window_info = m_swap_chain->GetWindowInfo();
return true;
}

m_swap_chain.reset();
}

VkSurfaceKHR surface = VulkanSwapChain::CreateVulkanSurface(m_instance, m_physical_device, &m_window_info);
if (surface == VK_NULL_HANDLE)
{
Expand Down

0 comments on commit 0076ab5

Please sign in to comment.