Skip to content

Commit

Permalink
Merge pull request #12660 from unknownbrackets/frame-latency
Browse files Browse the repository at this point in the history
GPU: Add setting to control inflight frame usage
  • Loading branch information
hrydgard authored Mar 1, 2020
2 parents c363c16 + 98df4bb commit fa8968f
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 4 deletions.
8 changes: 8 additions & 0 deletions Common/Vulkan/VulkanContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,14 @@ void VulkanContext::EndFrame() {
}
}

void VulkanContext::UpdateInflightFrames(int n) {
assert(n >= 1 && n <= MAX_INFLIGHT_FRAMES);
inflightFrames_ = n;
if (curFrame_ >= inflightFrames_) {
curFrame_ = 0;
}
}

void VulkanContext::WaitUntilQueueIdle() {
// Should almost never be used
vkQueueWaitIdle(gfx_queue_);
Expand Down
2 changes: 2 additions & 0 deletions Common/Vulkan/VulkanContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ class VulkanContext {
int GetInflightFrames() const {
return inflightFrames_;
}
// Don't call while a frame is in progress.
void UpdateInflightFrames(int n);

int GetCurFrame() const {
return curFrame_;
Expand Down
2 changes: 2 additions & 0 deletions Core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,8 @@ static ConfigSetting graphicsSettings[] = {
ConfigSetting("GfxDebugSplitSubmit", &g_Config.bGfxDebugSplitSubmit, false, false, false),
ConfigSetting("LogFrameDrops", &g_Config.bLogFrameDrops, false, true, false),

ConfigSetting("InflightFrames", &g_Config.iInflightFrames, 3, true, true),

ConfigSetting(false),
};

Expand Down
1 change: 1 addition & 0 deletions Core/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ struct Config {
std::string sPostShaderName; // Off for off.
bool bGfxDebugOutput;
bool bGfxDebugSplitSubmit;
int iInflightFrames;

// Sound
bool bEnableSound;
Expand Down
2 changes: 1 addition & 1 deletion Core/FileLoaders/RamCachingFileLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ size_t RamCachingFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data, F
size_t bytesFromCache = ReadFromCache(absolutePos + readSize, bytes - readSize, (u8 *)data + readSize);
readSize += bytesFromCache;
if (bytesFromCache == 0) {
// We can't read any more.
// We can't read any more.
break;
}
}
Expand Down
6 changes: 6 additions & 0 deletions GPU/GLES/GPU_GLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ GPU_GLES::GPU_GLES(GraphicsContext *gfxCtx, Draw::DrawContext *draw)

textureCacheGL_->NotifyConfigChanged();

GLRenderManager *rm = (GLRenderManager *)draw_->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);
rm->SetInflightFrames(g_Config.iInflightFrames);

// Load shader cache.
std::string discID = g_paramSFO.GetDiscID();
if (discID.size()) {
Expand Down Expand Up @@ -355,6 +358,9 @@ void GPU_GLES::BeginHostFrame() {
GPUCommon::BeginHostFrame();
UpdateCmdInfo();
if (resized_) {
GLRenderManager *rm = (GLRenderManager *)draw_->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);
rm->SetInflightFrames(g_Config.iInflightFrames);

CheckGPUFeatures();
framebufferManager_->Resized();
drawEngine_.Resized();
Expand Down
6 changes: 5 additions & 1 deletion GPU/Vulkan/GPU_Vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include "Core/Config.h"
#include "Core/Debugger/Breakpoints.h"
#include "Core/MemMapHelpers.h"
#include "Core/Config.h"
#include "Core/Reporting.h"
#include "Core/System.h"
#include "Core/ELF/ParamSFO.h"
Expand Down Expand Up @@ -98,6 +97,8 @@ GPU_Vulkan::GPU_Vulkan(GraphicsContext *gfxCtx, Draw::DrawContext *draw)
if (vulkan_->GetDeviceFeatures().enabled.wideLines) {
drawEngine_.SetLineWidth(PSP_CoreParameter().renderWidth / 480.0f);
}
VulkanRenderManager *rm = (VulkanRenderManager *)draw_->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);
rm->SetInflightFrames(g_Config.iInflightFrames);

// Load shader cache.
std::string discID = g_paramSFO.GetDiscID();
Expand Down Expand Up @@ -283,6 +284,9 @@ void GPU_Vulkan::BeginHostFrame() {
UpdateCmdInfo();

if (resized_) {
VulkanRenderManager *rm = (VulkanRenderManager *)draw_->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);
rm->SetInflightFrames(g_Config.iInflightFrames);

CheckGPUFeatures();
// In case the GPU changed.
BuildReportingInfo();
Expand Down
9 changes: 9 additions & 0 deletions UI/GameSettingsScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,15 @@ void GameSettingsScreen::CreateViews() {
});
#endif

if (GetGPUBackend() == GPUBackend::VULKAN || GetGPUBackend() == GPUBackend::OPENGL) {
static const char *bufferOptions[] = { "No buffer", "Up to 1", "Up to 2" };
PopupMultiChoice *inflightChoice = graphicsSettings->Add(new PopupMultiChoice(&g_Config.iInflightFrames, gr->T("Buffer graphics commands (faster, input lag)"), bufferOptions, 0, ARRAY_SIZE(bufferOptions), gr->GetName(), screenManager()));
inflightChoice->OnChoice.Add([=](EventParams &e) {
NativeMessageReceived("gpu_resized", "");
return UI::EVENT_CONTINUE;
});
}

CheckBox *hwTransform = graphicsSettings->Add(new CheckBox(&g_Config.bHardwareTransform, gr->T("Hardware Transform")));
hwTransform->OnClick.Handle(this, &GameSettingsScreen::OnHardwareTransform);
hwTransform->SetDisabledPtr(&g_Config.bSoftwareRendering);
Expand Down
8 changes: 6 additions & 2 deletions ext/native/thin3d/GLRenderManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ bool GLRenderManager::ThreadFrame() {
do {
if (nextFrame) {
threadFrame_++;
if (threadFrame_ >= MAX_INFLIGHT_FRAMES)
if (threadFrame_ >= inflightFrames_)
threadFrame_ = 0;
}
FrameData &frameData = frameData_[threadFrame_];
Expand Down Expand Up @@ -449,7 +449,11 @@ void GLRenderManager::Finish() {
frameData.pull_condVar.notify_all();

curFrame_++;
if (curFrame_ >= MAX_INFLIGHT_FRAMES)
if (newInflightFrames_ != -1) {
inflightFrames_ = newInflightFrames_;
newInflightFrames_ = -1;
}
if (curFrame_ >= inflightFrames_)
curFrame_ = 0;

insideFrame_ = false;
Expand Down
7 changes: 7 additions & 0 deletions ext/native/thin3d/GLRenderManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,10 @@ class GLRenderManager {

enum { MAX_INFLIGHT_FRAMES = 3 };

void SetInflightFrames(int f) {
newInflightFrames_ = f < 1 || f > MAX_INFLIGHT_FRAMES ? MAX_INFLIGHT_FRAMES : f;
}

int GetCurFrame() const {
return curFrame_;
}
Expand Down Expand Up @@ -988,6 +992,9 @@ class GLRenderManager {
std::function<void(int)> swapIntervalFunction_;
GLBufferStrategy bufferStrategy_ = GLBufferStrategy::SUBDATA;

int inflightFrames_ = MAX_INFLIGHT_FRAMES;
int newInflightFrames_ = -1;

int swapInterval_ = 0;
bool swapIntervalChanged_ = true;

Expand Down
4 changes: 4 additions & 0 deletions ext/native/thin3d/VulkanRenderManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,10 @@ void VulkanRenderManager::Finish() {
frameData.pull_condVar.notify_all();
}
vulkan_->EndFrame();
if (newInflightFrames_ != -1) {
vulkan_->UpdateInflightFrames(newInflightFrames_);
newInflightFrames_ = -1;
}

insideFrame_ = false;
}
Expand Down
5 changes: 5 additions & 0 deletions ext/native/thin3d/VulkanRenderManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ class VulkanRenderManager {
splitSubmit_ = split;
}

void SetInflightFrames(int f) {
newInflightFrames_ = f < 1 || f > VulkanContext::MAX_INFLIGHT_FRAMES ? VulkanContext::MAX_INFLIGHT_FRAMES : f;
}

VulkanContext *GetVulkanContext() {
return vulkan_;
}
Expand Down Expand Up @@ -302,6 +306,7 @@ class VulkanRenderManager {
};

FrameData frameData_[VulkanContext::MAX_INFLIGHT_FRAMES];
int newInflightFrames_ = -1;

// Submission time state
int curWidth_ = -1;
Expand Down

0 comments on commit fa8968f

Please sign in to comment.