Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions core/libs/prx/libSceAgcDriver/Execution/include/GpuIndex.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef CORE_LIBS_PRX_LIBSCEAGCDRIVER_EXECUTION_INCLUDE_GPUINDEX_HPP
#define CORE_LIBS_PRX_LIBSCEAGCDRIVER_EXECUTION_INCLUDE_GPUINDEX_HPP

#include <cstddef>
#include <cstdlib>
#include <optional>
#include <stdexcept>
#include <string>
#include <string_view>

namespace AgcDriver {

inline std::optional<std::size_t> ParseGpuIndex(const char* value) {
if (value == nullptr || *value == '\0') return std::nullopt;
const std::string_view text(value);
if (text.size() > 4) throw std::runtime_error("ANYPS5_GPU_INDEX=\"" + std::string(text) + "\" is not a Vulkan device index");
std::size_t index = 0;
for (const char digit : text) {
if (digit < '0' || digit > '9') throw std::runtime_error("ANYPS5_GPU_INDEX=\"" + std::string(text) + "\" is not a Vulkan device index");
index = index * 10 + static_cast<std::size_t>(digit - '0');
}
return index;
}

inline std::optional<std::size_t> RequestedGpuIndex() {
return ParseGpuIndex(std::getenv("ANYPS5_GPU_INDEX"));
}

}

#endif
18 changes: 14 additions & 4 deletions core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "prx/libSceAgcDriver/Execution/include/CaptureTrace.hpp"
#include "prx/libSceAgcDriver/Execution/include/Mutex.hpp"
#include "prx/libSceAgcDriver/Execution/include/CompileBudget.hpp"
#include "prx/libSceAgcDriver/Execution/include/GpuIndex.hpp"
#include "BdaAbi.hpp"
#include "SplitStateAbi.hpp"
#if ANYPS5_ENABLE_SPIRV_TOOLS
Expand Down Expand Up @@ -1538,6 +1539,8 @@ VulkanDevice::VulkanDevice(const PresentationWindow* window, bool compileOnly) :
int selectedRank = -1;
const char* requestedName = std::getenv("ANYPS5_GPU");
if (requestedName != nullptr && *requestedName == '\0') requestedName = nullptr;
const auto requestedIndex = RequestedGpuIndex();
const bool explicitRequest = requestedName != nullptr || requestedIndex.has_value();
const auto matchesRequest = [&](const char* deviceName) {
const std::string_view name(deviceName);
const std::string_view request(requestedName);
Expand All @@ -1563,7 +1566,8 @@ VulkanDevice::VulkanDevice(const PresentationWindow* window, bool compileOnly) :
}
};
const std::array<const char*, 1> presentationExtensions{VK_KHR_SWAPCHAIN_EXTENSION_NAME};
for (auto physical : devices) {
for (std::size_t deviceIndex = 0; deviceIndex < devices.size(); ++deviceIndex) {
const auto physical = devices[deviceIndex];
VkPhysicalDeviceProperties properties{};
state->InstanceFunction<PFN_vkGetPhysicalDeviceProperties>("vkGetPhysicalDeviceProperties")(physical, &properties);
APS5_LOG_OUT("Physical device candidate=%p name=%s api=0x%x type=%d", reinterpret_cast<void*>(physical), properties.deviceName, properties.apiVersion, static_cast<int>(properties.deviceType));
Expand All @@ -1573,8 +1577,9 @@ VulkanDevice::VulkanDevice(const PresentationWindow* window, bool compileOnly) :
if (properties.apiVersion < VK_API_VERSION_1_1) {
continue;
}
if (requestedIndex.has_value() && deviceIndex != *requestedIndex) continue;
if (requestedName != nullptr && !matchesRequest(properties.deviceName)) continue;
const int rank = requestedName != nullptr ? 4 : rankDeviceType(properties.deviceType);
const int rank = explicitRequest ? 4 : rankDeviceType(properties.deviceType);
if (rank <= selectedRank) {
continue;
}
Expand Down Expand Up @@ -1612,12 +1617,17 @@ VulkanDevice::VulkanDevice(const PresentationWindow* window, bool compileOnly) :
break;
}
}
if (window != nullptr && requestedName == nullptr && rank >= 2 && rank > rejectedHardwareRank && graphicsCompute && !presentable) {
if (window != nullptr && !explicitRequest && rank >= 2 && rank > rejectedHardwareRank && graphicsCompute && !presentable) {
rejectedHardwareName = properties.deviceName;
rejectedHardwareReason = swapchain ? PresentationRejection::NoSurfaceQueue : PresentationRejection::MissingSwapchain;
rejectedHardwareRank = rank;
}
}
if (selected == VK_NULL_HANDLE && requestedIndex.has_value()) {
std::string request = "at ANYPS5_GPU_INDEX=" + std::to_string(*requestedIndex);
if (requestedName != nullptr) request += std::string(" whose name contains ANYPS5_GPU=\"") + requestedName + "\"";
throw std::runtime_error("Vulkan: no usable device " + request + "; devices: " + candidateNames);
}
if (selected == VK_NULL_HANDLE && requestedName != nullptr) {
throw std::runtime_error(std::string("Vulkan: no usable device whose name contains ANYPS5_GPU=\"") + requestedName + "\"; devices: " + candidateNames);
}
Expand All @@ -1631,7 +1641,7 @@ VulkanDevice::VulkanDevice(const PresentationWindow* window, bool compileOnly) :
state->properties = properties.properties;
state->physical = selected;
if (window != nullptr) {
const auto warning = SoftwareFallbackWarning(state->properties.deviceType, state->properties.deviceName, rejectedHardwareName, rejectedHardwareReason, requestedName != nullptr);
const auto warning = SoftwareFallbackWarning(state->properties.deviceType, state->properties.deviceName, rejectedHardwareName, rejectedHardwareReason, explicitRequest);
if (!warning.empty()) std::fprintf(stderr, "%s\n", warning.c_str());
}
state->srgbDecodeFormats = Graphics::SrgbDecodeFormats(state->InstanceFunction<PFN_vkGetPhysicalDeviceFormatProperties>("vkGetPhysicalDeviceFormatProperties"), selected);
Expand Down
28 changes: 22 additions & 6 deletions core/libs/prx/libSceAgcDriver/tests/BdaDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "HostImportFailure.hpp"
#include <fstream>
#include "prx/libSceAgcDriver/Execution/include/BdaFeatures.hpp"
#include "prx/libSceAgcDriver/Execution/include/GpuIndex.hpp"
#include "prx/libSceAgcDriver/Graphics/include/Recorder.hpp"
#include "prx/libSceAgcDriver/Graphics/include/Resources.hpp"
#include <SDL_loadso.h>
Expand Down Expand Up @@ -31,7 +32,7 @@ namespace {

using namespace AgcDriver::Graphics;

std::optional<std::size_t> selectDevice(std::span<const VkPhysicalDeviceProperties> devices, std::string_view request) {
std::optional<std::size_t> selectDevice(std::span<const VkPhysicalDeviceProperties> devices, std::string_view request, std::optional<std::size_t> requestedIndex = std::nullopt) {
const auto rankDeviceType = [](VkPhysicalDeviceType type) {
switch (type) {
case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU: return 3;
Expand All @@ -45,20 +46,21 @@ std::optional<std::size_t> selectDevice(std::span<const VkPhysicalDeviceProperti
for (std::size_t index = 0; index < devices.size(); ++index) {
const auto& candidate = devices[index];
if (candidate.apiVersion < VK_API_VERSION_1_1) continue;
if (requestedIndex.has_value() && index != *requestedIndex) continue;
const std::string_view name(candidate.deviceName);
if (!request.empty() && std::search(name.begin(), name.end(), request.begin(), request.end(), [](char left, char right) {
return std::tolower(static_cast<unsigned char>(left)) == std::tolower(static_cast<unsigned char>(right));
}) == name.end()) continue;
const int rank = request.empty() ? rankDeviceType(candidate.deviceType) : 4;
const int rank = request.empty() && !requestedIndex.has_value() ? rankDeviceType(candidate.deviceType) : 4;
if (rank <= selectedRank) continue;
selected = index;
selectedRank = rank;
}
return selected;
}

bool canSkipVulkanFailure(const char* requestedName, const char* requireVulkan) {
return (requestedName == nullptr || *requestedName == '\0') && requireVulkan == nullptr;
bool canSkipVulkanFailure(const char* requestedName, const char* requireVulkan, const char* requestedIndex = nullptr) {
return (requestedName == nullptr || *requestedName == '\0') && (requestedIndex == nullptr || *requestedIndex == '\0') && requireVulkan == nullptr;
}

void deviceSelectionTests() {
Expand All @@ -74,6 +76,10 @@ void deviceSelectionTests() {
properties("NVIDIA GeForce RTX 5080", VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU),
};
Require(!selectDevice({}, {}).has_value(), "an empty Vulkan device list selected a device");
Require(selectDevice(devices, {}, 0u) == 0u, "ANYPS5_GPU_INDEX=0 did not select the first device over the discrete GPU");
Require(selectDevice(devices, "rtx 5080", 1u) == 1u, "ANYPS5_GPU_INDEX with a matching name did not select the device");
Require(!selectDevice(devices, "rtx 5080", 0u).has_value(), "ANYPS5_GPU_INDEX selected a device whose name does not match ANYPS5_GPU");
Require(!selectDevice(devices, {}, devices.size()).has_value(), "an out-of-range ANYPS5_GPU_INDEX selected a device");
Require(selectDevice(devices, {}) == 1u, "default selection did not prefer the discrete GPU");
Require(selectDevice(devices, "AMD") == 0u, "an AMD request lost to an unrelated discrete GPU");
Require(selectDevice(devices, "aMd rAdEoN") == 0u, "GPU name matching was not case-insensitive");
Expand All @@ -83,6 +89,7 @@ void deviceSelectionTests() {
Require(canSkipVulkanFailure("", nullptr), "an empty GPU request prevents an optional Vulkan skip");
Require(!canSkipVulkanFailure("missing device", nullptr), "an unmatched GPU request can be reported as a skip");
Require(!canSkipVulkanFailure(nullptr, "1"), "a required Vulkan test can be reported as a skip");
Require(!canSkipVulkanFailure(nullptr, nullptr, "2"), "a GPU index request can be reported as a skip");
devices[0].apiVersion = VK_API_VERSION_1_0;
Require(!selectDevice(devices, "AMD").has_value(), "a GPU without Vulkan 1.1 matched the request");
std::cout << "BDA device selection CPU tests passed\n";
Expand Down Expand Up @@ -127,8 +134,17 @@ class Device {
}
const char* requestedName = std::getenv("ANYPS5_GPU");
const std::string_view request = requestedName != nullptr ? requestedName : "";
const auto selected = selectDevice(deviceProperties, request);
const auto requestedIndex = AgcDriver::RequestedGpuIndex();
const auto selected = selectDevice(deviceProperties, request, requestedIndex);
if (!selected.has_value()) {
if (requestedIndex.has_value()) {
std::string candidateNames;
for (const auto& candidate : deviceProperties) {
if (!candidateNames.empty()) candidateNames += ", ";
candidateNames += candidate.deviceName;
}
throw std::runtime_error("no Vulkan 1.1 device at ANYPS5_GPU_INDEX=" + std::to_string(*requestedIndex) + (request.empty() ? std::string() : " whose name contains ANYPS5_GPU=\"" + std::string(request) + "\"") + "; devices: " + candidateNames);
}
if (!request.empty()) {
std::string candidateNames;
for (const auto& candidate : deviceProperties) {
Expand Down Expand Up @@ -244,7 +260,7 @@ int main(int argc, char** argv) {
try {
device = std::make_unique<Device>(importFailures);
} catch (const std::exception& error) {
if (!canSkipVulkanFailure(std::getenv("ANYPS5_GPU"), std::getenv("ANYPS5_REQUIRE_VULKAN"))) throw;
if (!canSkipVulkanFailure(std::getenv("ANYPS5_GPU"), std::getenv("ANYPS5_REQUIRE_VULKAN"), std::getenv("ANYPS5_GPU_INDEX"))) throw;
std::cout << "skipped, no usable Vulkan device: " << error.what() << '\n';
return 77;
}
Expand Down
17 changes: 13 additions & 4 deletions core/libs/prx/libSceAgcDriver/tests/PerformanceOverlay.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "PortabilitySubset.hpp"
#include "prx/libSceAgcDriver/Execution/include/PerformanceOverlay.hpp"
#include "prx/libSceAgcDriver/Execution/include/GpuIndex.hpp"
#include "prx/libSceAgcDriver/Execution/include/KeyboardOverlay.hpp"
#include "prx/libSceAgcDriver/Execution/include/PresentationPass.hpp"
#include "prx/libSceAgcDriver/Execution/include/PresentationScaler.hpp"
Expand Down Expand Up @@ -47,7 +48,7 @@ using AgcDriver::PresentationUpscaler;
constexpr int VulkanTestSkipped = 77;
constexpr std::uint64_t Bgra8 = 0x8000000000000000ull;

std::optional<std::size_t> selectDevice(std::span<const VkPhysicalDeviceProperties> devices, std::string_view request) {
std::optional<std::size_t> selectDevice(std::span<const VkPhysicalDeviceProperties> devices, std::string_view request, std::optional<std::size_t> requestedIndex = std::nullopt) {
const auto rankDeviceType = [](VkPhysicalDeviceType type) {
switch (type) {
case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU: return 3;
Expand All @@ -61,11 +62,12 @@ std::optional<std::size_t> selectDevice(std::span<const VkPhysicalDeviceProperti
for (std::size_t index = 0; index < devices.size(); ++index) {
const auto& candidate = devices[index];
if (candidate.apiVersion < VK_API_VERSION_1_1) continue;
if (requestedIndex.has_value() && index != *requestedIndex) continue;
const std::string_view name(candidate.deviceName);
if (!request.empty() && std::search(name.begin(), name.end(), request.begin(), request.end(), [](char left, char right) {
return std::tolower(static_cast<unsigned char>(left)) == std::tolower(static_cast<unsigned char>(right));
}) == name.end()) continue;
const int rank = request.empty() ? rankDeviceType(candidate.deviceType) : 4;
const int rank = request.empty() && !requestedIndex.has_value() ? rankDeviceType(candidate.deviceType) : 4;
if (rank <= selectedRank) continue;
selected = index;
selectedRank = rank;
Expand All @@ -88,6 +90,10 @@ void deviceSelectionTests() {
properties("CPU", VK_PHYSICAL_DEVICE_TYPE_CPU),
};
Require(!selectDevice({}, {}).has_value(), "an empty device list selected a device");
Require(selectDevice(devices, {}, 0u) == 0u, "ANYPS5_GPU_INDEX=0 did not select the first device over the discrete GPU");
Require(selectDevice(devices, "rtx 5080", 1u) == 1u, "ANYPS5_GPU_INDEX with a matching name did not select the device");
Require(!selectDevice(devices, "rtx 5080", 0u).has_value(), "ANYPS5_GPU_INDEX selected a device whose name does not match ANYPS5_GPU");
Require(!selectDevice(devices, {}, devices.size()).has_value(), "an out-of-range ANYPS5_GPU_INDEX selected a device");
Require(selectDevice(devices, {}) == 1u, "default selection did not prefer the discrete GPU");
Require(selectDevice(devices, "AMD") == 0u, "the AMD request did not select the integrated GPU");
Require(selectDevice(devices, "NVIDIA") == 1u, "the NVIDIA request did not select the discrete GPU");
Expand Down Expand Up @@ -143,7 +149,9 @@ class Device {
std::vector<VkPhysicalDeviceProperties> deviceProperties(devices.size());
for (std::size_t index = 0; index < devices.size(); ++index) physicalProperties(devices[index], &deviceProperties[index]);
const char* requestedName = std::getenv("ANYPS5_GPU");
const auto selectedIndex = selectDevice(deviceProperties, requestedName != nullptr ? requestedName : "");
const auto requestedIndex = AgcDriver::RequestedGpuIndex();
const auto selectedIndex = selectDevice(deviceProperties, requestedName != nullptr ? requestedName : "", requestedIndex);
Require(selectedIndex.has_value() || !requestedIndex.has_value(), "no Vulkan 1.1 device at ANYPS5_GPU_INDEX=" + std::to_string(requestedIndex.value_or(0)) + (requestedName != nullptr && *requestedName != '\0' ? std::string(" matching ANYPS5_GPU=\"") + requestedName + "\"" : std::string()));
Require(selectedIndex.has_value(), requestedName != nullptr && *requestedName != '\0' ? std::string("no Vulkan 1.1 device matching ANYPS5_GPU=\"") + requestedName + "\"" : "no Vulkan 1.1 device");
context.physical = devices[*selectedIndex];
const auto queues = function<PFN_vkGetPhysicalDeviceQueueFamilyProperties>("vkGetPhysicalDeviceQueueFamilyProperties");
Expand Down Expand Up @@ -1160,7 +1168,8 @@ int main(int argc, char** argv) {
device = std::make_unique<Device>();
} catch (const std::exception& error) {
const char* requestedName = std::getenv("ANYPS5_GPU");
if (std::getenv("ANYPS5_REQUIRE_VULKAN") != nullptr || (requestedName != nullptr && *requestedName != '\0')) throw;
const char* requestedIndex = std::getenv("ANYPS5_GPU_INDEX");
if (std::getenv("ANYPS5_REQUIRE_VULKAN") != nullptr || (requestedName != nullptr && *requestedName != '\0') || (requestedIndex != nullptr && *requestedIndex != '\0')) throw;
std::printf("skipped, no usable Vulkan device: %s\n", error.what());
return VulkanTestSkipped;
}
Expand Down
Loading
Loading