2022-05-29 19:26:27 -07:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
2022-05-27 23:33:23 -07:00
|
|
|
#include "video_core/vulkan_common/vulkan_wrapper.h"
|
|
|
|
|
2022-07-10 11:08:20 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <cstring> // for memset, strncpy
|
|
|
|
#include <processthreadsapi.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#elif defined(YUZU_UNIX)
|
2022-07-10 14:01:37 -07:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/wait.h>
|
2022-07-10 11:08:20 -07:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <cstdio>
|
2022-05-27 23:33:23 -07:00
|
|
|
#include "video_core/vulkan_common/vulkan_instance.h"
|
|
|
|
#include "video_core/vulkan_common/vulkan_library.h"
|
2022-07-10 11:08:20 -07:00
|
|
|
#include "yuzu/startup_checks.h"
|
2022-05-27 23:33:23 -07:00
|
|
|
|
2022-07-10 13:10:35 -07:00
|
|
|
void CheckVulkan() {
|
2022-07-10 14:18:31 -07:00
|
|
|
// Just start the Vulkan loader, this will crash if something is wrong
|
2022-05-27 23:33:23 -07:00
|
|
|
try {
|
|
|
|
Vulkan::vk::InstanceDispatch dld;
|
|
|
|
const Common::DynamicLibrary library = Vulkan::OpenLibrary();
|
|
|
|
const Vulkan::vk::Instance instance =
|
|
|
|
Vulkan::CreateInstance(library, dld, VK_API_VERSION_1_0);
|
|
|
|
|
|
|
|
} catch (const Vulkan::vk::Exception& exception) {
|
2022-07-10 14:18:31 -07:00
|
|
|
std::fprintf(stderr, "Failed to initialize Vulkan: %s\n", exception.what());
|
2022-05-27 23:33:23 -07:00
|
|
|
}
|
|
|
|
}
|
2022-07-10 11:08:20 -07:00
|
|
|
|
2022-07-10 08:29:10 -07:00
|
|
|
bool CheckEnvVars(bool* is_child) {
|
2022-07-10 11:08:20 -07:00
|
|
|
#ifdef _WIN32
|
2022-07-10 14:01:37 -07:00
|
|
|
// Check environment variable to see if we are the child
|
2022-07-10 17:24:10 -07:00
|
|
|
char variable_contents[8];
|
2022-07-10 14:01:37 -07:00
|
|
|
const DWORD startup_check_var =
|
2022-07-10 17:24:10 -07:00
|
|
|
GetEnvironmentVariableA(STARTUP_CHECK_ENV_VAR, variable_contents, 8);
|
2022-07-10 08:29:10 -07:00
|
|
|
if (startup_check_var > 0 && std::strncmp(variable_contents, ENV_VAR_ENABLED_TEXT, 8) == 0) {
|
2022-07-10 14:01:37 -07:00
|
|
|
CheckVulkan();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-10 08:29:10 -07:00
|
|
|
// Don't perform startup checks if we are a child process
|
|
|
|
char is_child_s[8];
|
|
|
|
const DWORD is_child_len = GetEnvironmentVariableA(IS_CHILD_ENV_VAR, is_child_s, 8);
|
|
|
|
if (is_child_len > 0 && std::strncmp(is_child_s, ENV_VAR_ENABLED_TEXT, 8) == 0) {
|
|
|
|
*is_child = true;
|
|
|
|
return false;
|
|
|
|
} else if (!SetEnvironmentVariableA(IS_CHILD_ENV_VAR, ENV_VAR_ENABLED_TEXT)) {
|
2022-10-20 23:34:08 -07:00
|
|
|
std::fprintf(stderr, "SetEnvironmentVariableA failed to set %s with error %lu\n",
|
2022-07-10 08:29:10 -07:00
|
|
|
IS_CHILD_ENV_VAR, GetLastError());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-19 11:46:12 -07:00
|
|
|
bool StartupChecks(const char* arg0, bool* has_broken_vulkan, bool perform_vulkan_check) {
|
2022-07-10 08:29:10 -07:00
|
|
|
#ifdef _WIN32
|
2022-07-10 14:01:37 -07:00
|
|
|
// Set the startup variable for child processes
|
2022-07-10 08:29:10 -07:00
|
|
|
const bool env_var_set = SetEnvironmentVariableA(STARTUP_CHECK_ENV_VAR, ENV_VAR_ENABLED_TEXT);
|
2022-07-10 11:08:20 -07:00
|
|
|
if (!env_var_set) {
|
2022-10-20 23:34:08 -07:00
|
|
|
std::fprintf(stderr, "SetEnvironmentVariableA failed to set %s with error %lu\n",
|
2022-07-10 13:10:35 -07:00
|
|
|
STARTUP_CHECK_ENV_VAR, GetLastError());
|
2022-07-10 11:08:20 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-19 11:46:12 -07:00
|
|
|
if (perform_vulkan_check) {
|
|
|
|
// Spawn child process that performs Vulkan check
|
|
|
|
PROCESS_INFORMATION process_info;
|
|
|
|
std::memset(&process_info, '\0', sizeof(process_info));
|
|
|
|
|
|
|
|
if (!SpawnChild(arg0, &process_info, 0)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until the processs exits and get exit code from it
|
|
|
|
WaitForSingleObject(process_info.hProcess, INFINITE);
|
|
|
|
DWORD exit_code = STILL_ACTIVE;
|
|
|
|
const int err = GetExitCodeProcess(process_info.hProcess, &exit_code);
|
|
|
|
if (err == 0) {
|
2022-10-20 23:34:08 -07:00
|
|
|
std::fprintf(stderr, "GetExitCodeProcess failed with error %lu\n", GetLastError());
|
2022-09-19 11:46:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Vulkan is broken if the child crashed (return value is not zero)
|
|
|
|
*has_broken_vulkan = (exit_code != 0);
|
|
|
|
|
|
|
|
if (CloseHandle(process_info.hProcess) == 0) {
|
2022-10-20 23:34:08 -07:00
|
|
|
std::fprintf(stderr, "CloseHandle failed with error %lu\n", GetLastError());
|
2022-09-19 11:46:12 -07:00
|
|
|
}
|
|
|
|
if (CloseHandle(process_info.hThread) == 0) {
|
2022-10-20 23:34:08 -07:00
|
|
|
std::fprintf(stderr, "CloseHandle failed with error %lu\n", GetLastError());
|
2022-09-19 11:46:12 -07:00
|
|
|
}
|
2022-07-10 14:01:37 -07:00
|
|
|
}
|
|
|
|
|
2022-07-12 11:23:50 -07:00
|
|
|
if (!SetEnvironmentVariableA(STARTUP_CHECK_ENV_VAR, nullptr)) {
|
2022-10-20 23:34:08 -07:00
|
|
|
std::fprintf(stderr, "SetEnvironmentVariableA failed to clear %s with error %lu\n",
|
2022-07-12 11:23:50 -07:00
|
|
|
STARTUP_CHECK_ENV_VAR, GetLastError());
|
|
|
|
}
|
|
|
|
|
2022-07-10 14:01:37 -07:00
|
|
|
#elif defined(YUZU_UNIX)
|
2022-09-19 11:46:12 -07:00
|
|
|
if (perform_vulkan_check) {
|
|
|
|
const pid_t pid = fork();
|
|
|
|
if (pid == 0) {
|
|
|
|
CheckVulkan();
|
|
|
|
return true;
|
|
|
|
} else if (pid == -1) {
|
|
|
|
const int err = errno;
|
|
|
|
std::fprintf(stderr, "fork failed with error %d\n", err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get exit code from child process
|
|
|
|
int status;
|
|
|
|
const int r_val = wait(&status);
|
|
|
|
if (r_val == -1) {
|
|
|
|
const int err = errno;
|
|
|
|
std::fprintf(stderr, "wait failed with error %d\n", err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Vulkan is broken if the child crashed (return value is not zero)
|
|
|
|
*has_broken_vulkan = (status != 0);
|
2022-07-10 14:01:37 -07:00
|
|
|
}
|
2022-07-10 13:10:35 -07:00
|
|
|
#endif
|
2022-07-10 14:01:37 -07:00
|
|
|
return false;
|
2022-07-10 13:10:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2022-07-10 08:29:10 -07:00
|
|
|
bool SpawnChild(const char* arg0, PROCESS_INFORMATION* pi, int flags) {
|
2022-07-10 13:10:35 -07:00
|
|
|
STARTUPINFOA startup_info;
|
2022-07-10 11:08:20 -07:00
|
|
|
|
|
|
|
std::memset(&startup_info, '\0', sizeof(startup_info));
|
|
|
|
startup_info.cb = sizeof(startup_info);
|
|
|
|
|
|
|
|
char p_name[255];
|
2022-07-10 13:10:35 -07:00
|
|
|
std::strncpy(p_name, arg0, 255);
|
2022-07-10 11:08:20 -07:00
|
|
|
|
|
|
|
const bool process_created = CreateProcessA(nullptr, // lpApplicationName
|
|
|
|
p_name, // lpCommandLine
|
|
|
|
nullptr, // lpProcessAttributes
|
|
|
|
nullptr, // lpThreadAttributes
|
|
|
|
false, // bInheritHandles
|
2022-07-10 08:29:10 -07:00
|
|
|
flags, // dwCreationFlags
|
2022-07-10 11:08:20 -07:00
|
|
|
nullptr, // lpEnvironment
|
|
|
|
nullptr, // lpCurrentDirectory
|
|
|
|
&startup_info, // lpStartupInfo
|
2022-07-10 13:10:35 -07:00
|
|
|
pi // lpProcessInformation
|
2022-07-10 11:08:20 -07:00
|
|
|
);
|
|
|
|
if (!process_created) {
|
2022-10-20 23:34:08 -07:00
|
|
|
std::fprintf(stderr, "CreateProcessA failed with error %lu\n", GetLastError());
|
2022-07-10 11:08:20 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2022-07-10 13:10:35 -07:00
|
|
|
#endif
|