| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "gpuinfovulkan.h" |
| 5 | |
| 6 | #include "../infoproviderregistry.h" |
| 7 | #include <QProcess> |
| 8 | #include <QStringList> |
| 9 | #include <format> |
| 10 | #include <spdlog/spdlog.h> |
| 11 | #include <utility> |
| 12 | |
| 13 | class GPUInfoVulkanDataSource : public IDataSource<std::string> |
| 14 | { |
| 15 | public: |
| 16 | std::string source() const override |
| 17 | { |
| 18 | return "vulkaninfo"; |
| 19 | } |
| 20 | |
| 21 | bool read(std::string &data) override |
| 22 | { |
| 23 | auto env = QProcessEnvironment::systemEnvironment(); |
| 24 | env.insert("LC_ALL", "C"); |
| 25 | |
| 26 | QProcess cmd; |
| 27 | cmd.setProcessChannelMode(QProcess::MergedChannels); |
| 28 | cmd.setProcessEnvironment(env); |
| 29 | cmd.start(source().c_str(), QStringList()); |
| 30 | |
| 31 | if (cmd.waitForFinished()) { |
| 32 | data = cmd.readAllStandardOutput().toStdString(); |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | SPDLOG_WARN("vulkaninfo command failed"); |
| 37 | return false; |
| 38 | } |
| 39 | }; |
| 40 | |
| 41 | GPUInfoVulkan::GPUInfoVulkan( |
| 42 | std::unique_ptr<IDataSource<std::string>> &&dataSource) noexcept |
| 43 | : dataSource_(std::move(dataSource)) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | std::vector<std::pair<std::string, std::string>> |
| 48 | GPUInfoVulkan::provideInfo(Vendor, int gpuIndex, IGPUInfo::Path const &, |
| 49 | IHWIDTranslator const &) const |
| 50 | { |
| 51 | std::vector<std::pair<std::string, std::string>> info; |
| 52 | |
| 53 | static constexpr std::string_view devicePropStr("VkPhysicalDeviceProperties"); |
| 54 | |
| 55 | std::string data; |
| 56 | if (dataSource_->read(data)) { |
| 57 | int currentGPUIndex = 0; |
| 58 | auto devicePropStrPos = data.find(devicePropStr); |
| 59 | while (devicePropStrPos != std::string::npos) { |
| 60 | |
| 61 | if (currentGPUIndex == gpuIndex) { |
| 62 | auto apiVersion = parseApiVersion(data, devicePropStrPos); |
| 63 | if (!apiVersion.empty()) |
| 64 | info.emplace_back(GPUInfoVulkan::Keys::apiVersion, |
| 65 | std::move(apiVersion)); |
| 66 | break; |
| 67 | } |
| 68 | |
| 69 | devicePropStrPos = data.find(devicePropStr, |
| 70 | devicePropStrPos + devicePropStr.length()); |
| 71 | currentGPUIndex++; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return info; |
| 76 | } |
| 77 | |
| 78 | std::vector<std::string> |
| 79 | GPUInfoVulkan::provideCapabilities(Vendor, int, IGPUInfo::Path const &) const |
| 80 | { |
| 81 | return {}; |
| 82 | } |
| 83 | |
| 84 | std::string GPUInfoVulkan::parseApiVersion(std::string const &src, |
| 85 | size_t pos) const |
| 86 | { |
| 87 | static constexpr std::string_view apiVerStr("apiVersion"); |
| 88 | |
| 89 | auto apiVerPos = src.find(apiVerStr, pos); |
| 90 | if (apiVerPos != std::string::npos) { |
| 91 | |
| 92 | auto startPos = src.find_first_not_of("= ", apiVerPos + apiVerStr.length()); |
| 93 | |
| 94 | // version inside parentheses is preferred |
| 95 | auto openParenPos = src.find("(", startPos); |
| 96 | if (openParenPos != std::string::npos) { |
| 97 | auto endPos = src.find(")", openParenPos); |
| 98 | auto version = src.substr(openParenPos + 1, endPos - openParenPos - 1); |
| 99 | |
| 100 | return version; |
| 101 | } |
| 102 | |
| 103 | // fallback version |
| 104 | auto endPos = src.find("\n", startPos); |
| 105 | auto version = src.substr(startPos, endPos - startPos); |
| 106 | |
| 107 | return version; |
| 108 | } |
| 109 | else |
| 110 | SPDLOG_DEBUG("Cannot find '{}' in vulkaninfo output", apiVerStr.data()); |
| 111 | |
| 112 | return std::string{}; |
| 113 | } |
| 114 | |
| 115 | bool GPUInfoVulkan::registered_ = InfoProviderRegistry::add( |
| 116 | std::make_unique<GPUInfoVulkan>(std::make_unique<GPUInfoVulkanDataSource>())); |