Skip to content

Commit

Permalink
GPUThread: Push settings through FIFO
Browse files Browse the repository at this point in the history
Saves the std::function heap allocation.
  • Loading branch information
stenzek committed Jan 22, 2025
1 parent f045a17 commit 89504b0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
31 changes: 20 additions & 11 deletions src/core/gpu_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ static bool CreateGPUBackendOnThread(GPURenderer renderer, bool upload_vram, Err
static void DestroyGPUBackendOnThread();
static void DestroyGPUPresenterOnThread();

static void UpdateSettingsOnThread(const GPUSettings& old_settings);
static void UpdateSettingsOnThread(GPUThreadUpdateSettingsCommand* cmd);

static void UpdateRunIdle();

Expand Down Expand Up @@ -507,6 +507,14 @@ void GPUThread::Internal::GPUThreadEntryPoint()
}
break;

case GPUBackendCommandType::UpdateSettings:
{
GPUThreadUpdateSettingsCommand* ccmd = static_cast<GPUThreadUpdateSettingsCommand*>(cmd);
UpdateSettingsOnThread(ccmd);
ccmd->~GPUThreadUpdateSettingsCommand();
}
break;

case GPUBackendCommandType::Shutdown:
{
// Should have consumed everything, and be shutdown.
Expand Down Expand Up @@ -553,6 +561,7 @@ bool GPUThread::Reconfigure(std::string serial, std::optional<GPURenderer> rende
cmd->upload_vram = upload_vram;
cmd->error_ptr = error;
cmd->out_result = &result;
cmd->settings = g_settings;

if (!s_state.use_gpu_thread) [[unlikely]]
ReconfigureOnThread(cmd);
Expand Down Expand Up @@ -819,7 +828,7 @@ void GPUThread::ReconfigureOnThread(GPUThreadReconfigureCommand* cmd)
s_state.requested_allow_present_throttle = cmd->allow_present_throttle;
s_state.requested_fullscreen_ui = cmd->start_fullscreen_ui.value_or(s_state.requested_fullscreen_ui);
s_state.game_serial = std::move(cmd->game_serial);
g_gpu_settings = g_settings;
g_gpu_settings = std::move(cmd->settings);

// Readback old VRAM for hardware renderers.
const bool had_renderer = static_cast<bool>(s_state.gpu_backend);
Expand Down Expand Up @@ -966,8 +975,13 @@ bool GPUThread::Internal::PresentFrameAndRestoreContext()
return true;
}

void GPUThread::UpdateSettingsOnThread(const GPUSettings& old_settings)
void GPUThread::UpdateSettingsOnThread(GPUThreadUpdateSettingsCommand* cmd)
{
VERBOSE_LOG("Updating GPU settings on thread...");

GPUSettings old_settings = std::move(g_gpu_settings);
g_gpu_settings = std::move(cmd->settings);

if (g_gpu_device)
{
if (g_gpu_settings.display_osd_scale != old_settings.display_osd_scale)
Expand Down Expand Up @@ -1067,14 +1081,9 @@ void GPUThread::UpdateSettings(bool gpu_settings_changed, bool device_settings_c
}
else if (gpu_settings_changed)
{
RunOnThread([settings = g_settings]() {
VERBOSE_LOG("Updating GPU settings on thread...");

GPUSettings old_settings = std::move(g_gpu_settings);
g_gpu_settings = std::move(settings);

UpdateSettingsOnThread(old_settings);
});
GPUThreadUpdateSettingsCommand* cmd =
AllocateCommand<GPUThreadUpdateSettingsCommand>(GPUBackendCommandType::UpdateSettings, g_settings);
PushCommandAndWakeThread(cmd);
}
else
{
Expand Down
12 changes: 11 additions & 1 deletion src/core/gpu_thread_commands.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
// SPDX-FileCopyrightText: 2019-2025 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: CC-BY-NC-ND-4.0

#pragma once

#include "gpu_types.h"
#include "settings.h"

#include "common/align.h"

Expand All @@ -29,6 +30,7 @@ enum class GPUBackendCommandType : u8
AsyncCall,
AsyncBackendCall,
Reconfigure,
UpdateSettings,
Shutdown,
ClearVRAM,
ClearDisplay,
Expand Down Expand Up @@ -78,6 +80,14 @@ struct GPUThreadReconfigureCommand : public GPUThreadCommand
bool allow_present_throttle;
bool force_recreate_device;
bool upload_vram;
GPUSettings settings;
};

struct GPUThreadUpdateSettingsCommand : public GPUThreadCommand
{
GPUThreadUpdateSettingsCommand(const GPUSettings& settings_) : settings(settings_) {}

GPUSettings settings;
};

struct GPUThreadAsyncCallCommand : public GPUThreadCommand
Expand Down

0 comments on commit 89504b0

Please sign in to comment.