Welcome to mirror list, hosted at ThFree Co, Russian Federation.

nvapi_adapter.cpp « sysinfo « src - github.com/jp7677/dxvk-nvapi.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a2cf1485b458ae9cb5a0fae428cb5e08c4f57793 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include "nvapi_adapter.h"
#include "../dxvk/dxvk_interfaces.h"
#include "../util/util_string.h"
#include "../util/util_env.h"
#include "../util/util_log.h"

namespace dxvk {
    NvapiAdapter::NvapiAdapter(Vulkan& vulkan, Nvml& nvml)
        : m_vulkan(vulkan), m_nvml(nvml) {}

    NvapiAdapter::~NvapiAdapter() = default;

    bool NvapiAdapter::Initialize(Com<IDXGIAdapter1>& dxgiAdapter, std::vector<NvapiOutput*>& outputs) {
        constexpr auto driverVersionEnvName = "DXVK_NVAPI_DRIVER_VERSION";
        constexpr auto allowOtherDriversEnvName = "DXVK_NVAPI_ALLOW_OTHER_DRIVERS";

        // Get the Vulkan handle from the DXGI adapter to get access to Vulkan device properties which has some information we want.
        Com<IDXGIVkInteropAdapter> dxgiVkInteropAdapter;
        if (FAILED(dxgiAdapter->QueryInterface(IID_PPV_ARGS(&dxgiVkInteropAdapter)))) {
            log::write("Querying Vulkan handle from DXGI adapter failed, please ensure that DXVK's dxgi.dll is loaded");
            return false;
        }

        VkInstance vkInstance = VK_NULL_HANDLE;
        VkPhysicalDevice vkDevice = VK_NULL_HANDLE;
        dxgiVkInteropAdapter->GetVulkanHandles(&vkInstance, &vkDevice);

        m_deviceExtensions = m_vulkan.GetDeviceExtensions(vkInstance, vkDevice);
        if (m_deviceExtensions.empty())
            return false;

        // Query Properties for this device. Per section 4.1.2. Extending Physical Device From Device Extensions of the Vulkan
        // 1.2.177 Specification, we must first query that a device extension is
        // supported before requesting information on its physical-device-level
        // functionality (ie: Properties).
        VkPhysicalDeviceProperties2 deviceProperties2;
        deviceProperties2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
        deviceProperties2.pNext = nullptr;

        if (IsVkDeviceExtensionSupported(VK_EXT_PCI_BUS_INFO_EXTENSION_NAME)) {
            m_devicePciBusProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT;
            m_devicePciBusProperties.pNext = deviceProperties2.pNext;
            deviceProperties2.pNext = &m_devicePciBusProperties;
        }

        if (IsVkDeviceExtensionSupported(VK_KHR_DRIVER_PROPERTIES_EXTENSION_NAME)) {
            m_deviceDriverProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES_KHR;
            m_deviceDriverProperties.pNext = deviceProperties2.pNext;
            deviceProperties2.pNext = &m_deviceDriverProperties;
        }

        if (IsVkDeviceExtensionSupported(VK_KHR_FRAGMENT_SHADING_RATE_EXTENSION_NAME)) {
            m_deviceFragmentShadingRateProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FRAGMENT_SHADING_RATE_PROPERTIES_KHR;
            m_deviceFragmentShadingRateProperties.pNext = deviceProperties2.pNext;
            deviceProperties2.pNext = &m_deviceFragmentShadingRateProperties;
        }

        m_deviceIdProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES;
        m_deviceIdProperties.pNext = deviceProperties2.pNext;
        deviceProperties2.pNext = &m_deviceIdProperties;

        m_vulkan.GetPhysicalDeviceProperties2(vkInstance, vkDevice, &deviceProperties2);
        m_deviceProperties = deviceProperties2.properties;

        VkPhysicalDeviceMemoryProperties2 memoryProperties2;
        memoryProperties2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_PROPERTIES_2;
        memoryProperties2.pNext = nullptr;

        m_vulkan.GetPhysicalDeviceMemoryProperties2(vkInstance, vkDevice, &memoryProperties2);
        m_memoryProperties = memoryProperties2.memoryProperties;

        auto allowOtherDrivers = env::getEnvVariable(allowOtherDriversEnvName);
        if (!allowOtherDrivers.empty())
            log::write(str::format(allowOtherDriversEnvName, " is set, reporting also GPUs with non-NVIDIA proprietary driver"));

        if (!HasNvProprietaryDriver() && allowOtherDrivers.empty())
            return false;

        if (HasNvProprietaryDriver())
            // Handle NVIDIA version notation
            m_vkDriverVersion = VK_MAKE_VERSION(
                VK_VERSION_MAJOR(m_deviceProperties.driverVersion),
                VK_VERSION_MINOR(m_deviceProperties.driverVersion >> 0) >> 2,
                VK_VERSION_PATCH(m_deviceProperties.driverVersion >> 2) >> 4);
        else
            m_vkDriverVersion = m_deviceProperties.driverVersion;

        log::write(str::format("NvAPI Device: ", m_deviceProperties.deviceName, " (",
            VK_VERSION_MAJOR(m_vkDriverVersion), ".",
            VK_VERSION_MINOR(m_vkDriverVersion), ".",
            VK_VERSION_PATCH(m_vkDriverVersion), ")"));

        // Query all outputs from DXVK
        // Mosaic setup is not supported, thus one display output refers to one GPU
        Com<IDXGIOutput> dxgiOutput;
        for (auto i = 0U; dxgiAdapter->EnumOutputs(i, &dxgiOutput) != DXGI_ERROR_NOT_FOUND; i++) {
            auto nvapiOutput = new NvapiOutput((uintptr_t)this);
            nvapiOutput->Initialize(dxgiOutput);
            outputs.push_back(nvapiOutput);
        }

        if (m_nvml.IsAvailable()) {
            char pciId[NVML_DEVICE_PCI_BUS_ID_BUFFER_SIZE];

            snprintf(pciId, NVML_DEVICE_PCI_BUS_ID_BUFFER_SIZE, NVML_DEVICE_PCI_BUS_ID_FMT,
                m_devicePciBusProperties.pciDomain,
                m_devicePciBusProperties.pciBus,
                m_devicePciBusProperties.pciDevice);

            nvmlDevice_t nvmlDevice;
            auto result = m_nvml.DeviceGetHandleByPciBusId_v2(pciId, &nvmlDevice);
            if (result == NVML_SUCCESS)
                m_nvmlDevice = nvmlDevice;
            else
                log::write(str::format("NVML failed to find device with PCI BusId [", pciId, "]: ", m_nvml.ErrorString(result)));
        }

        auto driverVersion = env::getEnvVariable(driverVersionEnvName);
        if (!driverVersion.empty()) {
            char* end;
            auto driverVersionOverride = std::strtol(driverVersion.c_str(), &end, 10);
            if (std::string(end).empty() && driverVersionOverride >= 100 && driverVersionOverride <= 99999) {
                std::stringstream stream;
                stream << (driverVersionOverride / 100) << "." << std::setfill('0') << std::setw(2) << (driverVersionOverride % 100);
                log::write(str::format(driverVersionEnvName, " is set to '", driverVersion, "', reporting driver version ", stream.str()));
                m_driverVersionOverride = driverVersionOverride;
            } else
                log::write(str::format(driverVersionEnvName, " is set to '", driverVersion, "', but this value is invalid, please set a number between 100 and 99999"));
        }

        return true;
    }

    std::string NvapiAdapter::GetDeviceName() const {
        return {m_deviceProperties.deviceName};
    }

    uint32_t NvapiAdapter::GetDriverVersion() const {
        // Windows releases can only ever have a two digit minor version
        // and does not have a patch number
        return m_driverVersionOverride > 0
            ? m_driverVersionOverride
            : VK_VERSION_MAJOR(m_vkDriverVersion) * 100 + std::min(VK_VERSION_MINOR(m_vkDriverVersion), 99U);
    }

    bool NvapiAdapter::HasNvProprietaryDriver() const {
        return m_deviceDriverProperties.driverID == VK_DRIVER_ID_NVIDIA_PROPRIETARY;
    }

    uint32_t NvapiAdapter::GetDeviceId() const {
        return (m_deviceProperties.deviceID << 16) | m_deviceProperties.vendorID;
    }

    uint32_t NvapiAdapter::GetExternalDeviceId() const {
        return m_deviceProperties.deviceID;
    }

    uint32_t NvapiAdapter::GetSubSystemId() const {
        if (!this->HasNvmlDevice())
            return 0;

        nvmlPciInfo_t pciInfo;
        auto result = this->m_nvml.DeviceGetPciInfo_v3(this->m_nvmlDevice, &pciInfo);
        return result == NVML_SUCCESS ? pciInfo.pciSubSystemId : 0;
    }

    NV_GPU_TYPE NvapiAdapter::GetGpuType() const {
        return Vulkan::ToNvGpuType(m_deviceProperties.deviceType);
    }

    uint32_t NvapiAdapter::GetPciBusId() const {
        return m_devicePciBusProperties.pciBus;
    }

    uint32_t NvapiAdapter::GetPciDeviceId() const {
        return m_devicePciBusProperties.pciDevice;
    }

    uint32_t NvapiAdapter::GetBoardId() const {
        // There is also https://docs.nvidia.com/deploy/nvml-api/group__nvmlDeviceQueries.html#group__nvmlDeviceQueries_1gbf4a80f14b6093ce98e4c2dd511275c5
        // But since we don't have NVML everywhere, we will create a board ID derived from PCI Domain/BUS/Device ID
        return m_devicePciBusProperties.pciDomain << 16
            | m_devicePciBusProperties.pciBus << 8
            | m_devicePciBusProperties.pciDevice;
    }

    uint32_t NvapiAdapter::GetVRamSize() const {
        // The total size of all device-local heaps sometimes do not match what other tools are reporting,
        // though this is best we have.
        auto size = 0U;
        for (auto i = 0U; i < m_memoryProperties.memoryHeapCount; i++) {
            auto heap = m_memoryProperties.memoryHeaps[i];
            if (heap.flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT)
                size += heap.size / 1024;
        }

        return size;
    }

    std::optional<LUID> NvapiAdapter::GetLuid() const {
        if (!m_deviceIdProperties.deviceLUIDValid)
            return {};

        LUID luid{};
        memcpy(&luid, &m_deviceIdProperties.deviceLUID, sizeof(luid));
        return std::make_optional(luid);
    }

    NV_GPU_ARCHITECTURE_ID NvapiAdapter::GetArchitectureId() const {
        // KHR_fragment_shading_rate's
        // primitiveFragmentShadingRateWithMultipleViewports is supported on
        // Ampere and newer
        if (IsVkDeviceExtensionSupported(VK_KHR_FRAGMENT_SHADING_RATE_EXTENSION_NAME)
            && m_deviceFragmentShadingRateProperties.primitiveFragmentShadingRateWithMultipleViewports)
            return NV_GPU_ARCHITECTURE_GA100;

        // Variable rate shading is supported on Turing and newer
        if (IsVkDeviceExtensionSupported(VK_NV_SHADING_RATE_IMAGE_EXTENSION_NAME))
            return NV_GPU_ARCHITECTURE_TU100;

        // VK_NVX_image_view_handle is supported on Volta and newer
        if (IsVkDeviceExtensionSupported(VK_NVX_IMAGE_VIEW_HANDLE_EXTENSION_NAME))
            return NV_GPU_ARCHITECTURE_GV100;

        // VK_NV_clip_space_w_scaling is supported on Pascal and newer
        if (IsVkDeviceExtensionSupported(VK_NV_CLIP_SPACE_W_SCALING_EXTENSION_NAME))
            return NV_GPU_ARCHITECTURE_GP100;

        // VK_NV_viewport_array2 is supported on Maxwell and newer
        if (IsVkDeviceExtensionSupported(VK_NV_VIEWPORT_ARRAY2_EXTENSION_NAME))
            return NV_GPU_ARCHITECTURE_GM200;

        // Fall back to Kepler
        return NV_GPU_ARCHITECTURE_GK100;
    }

    bool NvapiAdapter::IsVkDeviceExtensionSupported(std::string name) const { // NOLINT(performance-unnecessary-value-param)
        return m_deviceExtensions.find(name) != m_deviceExtensions.end();
    }

    bool NvapiAdapter::HasNvml() const {
        return m_nvml.IsAvailable();
    }

    bool NvapiAdapter::HasNvmlDevice() const {
        return m_nvml.IsAvailable() && m_nvmlDevice != nullptr;
    }

    std::string NvapiAdapter::GetNvmlErrorString(nvmlReturn_t result) const {
        return {m_nvml.ErrorString(result)};
    }

    nvmlReturn_t NvapiAdapter::GetNvmlDeviceTemperature(nvmlTemperatureSensors_t sensorType, unsigned int* temp) const {
        return m_nvml.DeviceGetTemperature(m_nvmlDevice, sensorType, temp);
    }

    nvmlReturn_t NvapiAdapter::GetNvmlDeviceUtilizationRates(nvmlUtilization_t* utilization) const {
        return m_nvml.DeviceGetUtilizationRates(m_nvmlDevice, utilization);
    }

    nvmlReturn_t NvapiAdapter::GetNvmlDeviceVbiosVersion(char* version, unsigned int length) const {
        return m_nvml.DeviceGetVbiosVersion(m_nvmlDevice, version, length);
    }

    nvmlReturn_t NvapiAdapter::GetNvmlDevicePerformanceState(nvmlPstates_t* pState) const {
        return m_nvml.DeviceGetPerformanceState(m_nvmlDevice, pState);
    }

    nvmlReturn_t NvapiAdapter::GetNvmlDeviceClockInfo(nvmlClockType_t type, unsigned int* clock) const {
        return m_nvml.DeviceGetClockInfo(m_nvmlDevice, type, clock);
    }

    nvmlReturn_t NvapiAdapter::GetNvmlDeviceBusType(nvmlBusType_t* type) const {
        return m_nvml.DeviceGetBusType(m_nvmlDevice, type);
    }

    nvmlReturn_t NvapiAdapter::GetNvmlDeviceDynamicPstatesInfo(nvmlGpuDynamicPstatesInfo_t* pDynamicPstatesInfo) const {
        return m_nvml.DeviceGetDynamicPstatesInfo(m_nvmlDevice, pDynamicPstatesInfo);
    }
}