Skip to content

Commit

Permalink
Merge pull request #18296 from hrydgard/startup-perf
Browse files Browse the repository at this point in the history
Startup and tex-replacement performance fixes
  • Loading branch information
hrydgard committed Oct 3, 2023
2 parents 4f43dac + d07c3c5 commit 7a2f308
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 39 deletions.
10 changes: 7 additions & 3 deletions GPU/Common/TextureReplacer.cpp
Expand Up @@ -809,9 +809,13 @@ void TextureReplacer::Decimate(ReplacerDecimateMode mode) {
const double threshold = time_now_d() - age;
size_t totalSize = 0;
for (auto &item : levelCache_) {
std::lock_guard<std::mutex> guard(item.second->lock_);
item.second->PurgeIfNotUsedSinceTime(threshold);
totalSize += item.second->GetTotalDataSize(); // TODO: Make something better.
// During decimation, it's fine to try-lock here to avoid blocking the main thread while
// the level is being loaded - in that case we don't want to decimate anyway.
if (item.second->lock_.try_lock()) {
item.second->PurgeIfNotUsedSinceTime(threshold);
totalSize += item.second->GetTotalDataSize(); // TODO: Make something better.
item.second->lock_.unlock();
}
// don't actually delete the items here, just clean out the data.
}

Expand Down
28 changes: 28 additions & 0 deletions Windows/W32Util/Misc.cpp
Expand Up @@ -168,6 +168,34 @@ namespace W32Util
ExitProcess(0);
}

bool ExecuteAndGetReturnCode(const wchar_t *executable, const wchar_t *cmdline, const wchar_t *currentDirectory, DWORD *exitCode) {
PROCESS_INFORMATION processInformation = { 0 };
STARTUPINFO startupInfo = { 0 };
startupInfo.cb = sizeof(startupInfo);

std::wstring cmdlineW;
cmdlineW += L"PPSSPP "; // could also put the executable name as first argument, but concerned about escaping.
cmdlineW += cmdline;

// Create the process
bool result = CreateProcess(executable, (LPWSTR)cmdlineW.c_str(),
NULL, NULL, FALSE,
NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW,
NULL, currentDirectory, &startupInfo, &processInformation);

if (!result) {
// We failed.
return false;
}

// Successfully created the process. Wait for it to finish.
WaitForSingleObject(processInformation.hProcess, INFINITE);
result = GetExitCodeProcess(processInformation.hProcess, exitCode);
CloseHandle(processInformation.hProcess);
CloseHandle(processInformation.hThread);
return result != 0;
}

void SpawnNewInstance(bool overrideArgs, const std::string &args) {
// This preserves arguments (for example, config file) and working directory.
std::wstring workingDirectory;
Expand Down
1 change: 1 addition & 0 deletions Windows/W32Util/Misc.h
Expand Up @@ -14,6 +14,7 @@ namespace W32Util
void MakeTopMost(HWND hwnd, bool topMost);
void ExitAndRestart(bool overrideArgs = false, const std::string &args = "");
void SpawnNewInstance(bool overrideArgs = false, const std::string &args = "");
bool ExecuteAndGetReturnCode(const wchar_t *executable, const wchar_t *cmdline, const wchar_t *currentDirectory, DWORD *exitCode);
void GetSelfExecuteParams(std::wstring &workingDirectory, std::wstring &moduleFilename);

void GetWindowRes(HWND hWnd, int *xres, int *yres);
Expand Down
58 changes: 22 additions & 36 deletions Windows/main.cpp
Expand Up @@ -694,28 +694,13 @@ static bool DetectVulkanInExternalProcess() {

const wchar_t *cmdline = L"--vulkan-available-check";

SHELLEXECUTEINFO info{ sizeof(SHELLEXECUTEINFO) };
info.fMask = SEE_MASK_NOCLOSEPROCESS;
info.lpFile = moduleFilename.c_str();
info.lpParameters = cmdline;
info.lpDirectory = workingDirectory.c_str();
info.nShow = SW_HIDE;
if (ShellExecuteEx(&info) != TRUE) {
return false;
}
if (info.hProcess == nullptr) {
return false;
}

DWORD result = WaitForSingleObject(info.hProcess, 10000);
DWORD exitCode = 0;
if (result == WAIT_FAILED || GetExitCodeProcess(info.hProcess, &exitCode) == 0) {
CloseHandle(info.hProcess);
if (W32Util::ExecuteAndGetReturnCode(moduleFilename.c_str(), cmdline, workingDirectory.c_str(), &exitCode)) {
return exitCode == EXIT_CODE_VULKAN_WORKS;
} else {
ERROR_LOG(G3D, "Failed to detect Vulkan in external process somehow");
return false;
}
CloseHandle(info.hProcess);

return exitCode == EXIT_CODE_VULKAN_WORKS;
}
#endif

Expand Down Expand Up @@ -840,6 +825,24 @@ static void WinMainCleanup() {
}

int WINAPI WinMain(HINSTANCE _hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow) {
std::vector<std::wstring> wideArgs = GetWideCmdLine();

// Check for the Vulkan workaround before any serious init.
for (size_t i = 1; i < wideArgs.size(); ++i) {
if (wideArgs[i][0] == L'-') {
// This should only be called by DetectVulkanInExternalProcess().
if (wideArgs[i] == L"--vulkan-available-check") {
// Just call it, this way it will crash here if it doesn't work.
// (this is an external process.)
bool result = VulkanMayBeAvailable();

LogManager::Shutdown();
WinMainCleanup();
return result ? EXIT_CODE_VULKAN_WORKS : EXIT_FAILURE;
}
}
}

SetCurrentThreadName("Main");

WinMainInit();
Expand All @@ -863,7 +866,6 @@ int WINAPI WinMain(HINSTANCE _hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLin
std::string controlsConfigFilename = "";
const std::wstring controlsOption = L"--controlconfig=";

std::vector<std::wstring> wideArgs = GetWideCmdLine();

for (size_t i = 1; i < wideArgs.size(); ++i) {
if (wideArgs[i][0] == L'\0')
Expand All @@ -889,22 +891,6 @@ int WINAPI WinMain(HINSTANCE _hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLin
InitMemstickDirectory();
CreateSysDirectories();

// Check for the Vulkan workaround before any serious init.
for (size_t i = 1; i < wideArgs.size(); ++i) {
if (wideArgs[i][0] == L'-') {
// This should only be called by DetectVulkanInExternalProcess().
if (wideArgs[i] == L"--vulkan-available-check") {
// Just call it, this way it will crash here if it doesn't work.
// (this is an external process.)
bool result = VulkanMayBeAvailable();

LogManager::Shutdown();
WinMainCleanup();
return result ? EXIT_CODE_VULKAN_WORKS : EXIT_FAILURE;
}
}
}


// Load config up here, because those changes below would be overwritten
// if it's not loaded here first.
Expand Down

0 comments on commit 7a2f308

Please sign in to comment.