From 34614cd8f05b3b30a514af8a746c67c168b639ab Mon Sep 17 00:00:00 2001 From: Krzysztof Bogacki Date: Sat, 25 Jun 2022 21:53:33 +0200 Subject: nvapi-gpu: Implement NvAPI_GPU_GetGpuCoreCount using nvmlDeviceGetNumGpuCores --- src/nvapi_gpu.cpp | 39 +++++++++++++++++++++++++++++++++++++++ src/nvapi_interface.cpp | 1 + src/sysinfo/nvapi_adapter.cpp | 4 ++++ src/sysinfo/nvapi_adapter.h | 1 + 4 files changed, 45 insertions(+) diff --git a/src/nvapi_gpu.cpp b/src/nvapi_gpu.cpp index b0b4ce6..888826f 100644 --- a/src/nvapi_gpu.cpp +++ b/src/nvapi_gpu.cpp @@ -6,6 +6,45 @@ extern "C" { using namespace dxvk; + NvAPI_Status __cdecl NvAPI_GPU_GetGpuCoreCount(NvPhysicalGpuHandle hPhysicalGpu, NvU32* pCount) { + constexpr auto n = __func__; + static bool alreadyLoggedNoNvml = false; + static bool alreadyLoggedHandleInvalidated = false; + static bool alreadyLoggedOk = false; + + if (nvapiAdapterRegistry == nullptr) + return ApiNotInitialized(n); + + if (pCount == nullptr) + return InvalidArgument(n); + + auto adapter = reinterpret_cast(hPhysicalGpu); + if (!nvapiAdapterRegistry->IsAdapter(adapter)) + return ExpectedPhysicalGpuHandle(n); + + if (!adapter->HasNvml()) + return NoImplementation(n, alreadyLoggedNoNvml); + + if (!adapter->HasNvmlDevice()) + return HandleInvalidated(str::format(n, ": NVML available but current adapter is not NVML compatible"), alreadyLoggedHandleInvalidated); + + unsigned int cores; + auto result = adapter->GetNvmlDeviceNumGpuCores(&cores); + switch (result) { + case NVML_SUCCESS: + *pCount = cores; + return Ok(n, alreadyLoggedOk); + case NVML_ERROR_FUNCTION_NOT_FOUND: + return NoImplementation(n, alreadyLoggedNoNvml); + case NVML_ERROR_NOT_SUPPORTED: + return NotSupported(n); + case NVML_ERROR_GPU_IS_LOST: + return HandleInvalidated(n); + default: + return Error(str::format(n, ": ", adapter->GetNvmlErrorString(result))); + } + } + NvAPI_Status __cdecl NvAPI_GPU_GetGPUType(NvPhysicalGpuHandle hPhysicalGpu, NV_GPU_TYPE* pGpuType) { constexpr auto n = __func__; diff --git a/src/nvapi_interface.cpp b/src/nvapi_interface.cpp index 45f035e..4dabba1 100644 --- a/src/nvapi_interface.cpp +++ b/src/nvapi_interface.cpp @@ -71,6 +71,7 @@ extern "C" { INSERT_AND_RETURN_WHEN_EQUALS(NvAPI_D3D_Sleep) INSERT_AND_RETURN_WHEN_EQUALS(NvAPI_D3D_GetLatency) INSERT_AND_RETURN_WHEN_EQUALS(NvAPI_D3D_SetLatencyMarker) + INSERT_AND_RETURN_WHEN_EQUALS(NvAPI_GPU_GetGpuCoreCount) INSERT_AND_RETURN_WHEN_EQUALS(NvAPI_GPU_GetGPUType) INSERT_AND_RETURN_WHEN_EQUALS(NvAPI_GPU_GetPCIIdentifiers) INSERT_AND_RETURN_WHEN_EQUALS(NvAPI_GPU_GetFullName) diff --git a/src/sysinfo/nvapi_adapter.cpp b/src/sysinfo/nvapi_adapter.cpp index e30d184..569d110 100644 --- a/src/sysinfo/nvapi_adapter.cpp +++ b/src/sysinfo/nvapi_adapter.cpp @@ -274,6 +274,10 @@ namespace dxvk { return m_nvml.DeviceGetVbiosVersion(m_nvmlDevice, version, length); } + nvmlReturn_t NvapiAdapter::GetNvmlDeviceNumGpuCores(unsigned int* numCores) const { + return m_nvml.DeviceGetNumGpuCores(m_nvmlDevice, numCores); + } + nvmlReturn_t NvapiAdapter::GetNvmlDeviceBusType(nvmlBusType_t* type) const { return m_nvml.DeviceGetBusType(m_nvmlDevice, type); } diff --git a/src/sysinfo/nvapi_adapter.h b/src/sysinfo/nvapi_adapter.h index 19000a6..0a3075f 100644 --- a/src/sysinfo/nvapi_adapter.h +++ b/src/sysinfo/nvapi_adapter.h @@ -37,6 +37,7 @@ namespace dxvk { [[nodiscard]] nvmlReturn_t GetNvmlDevicePerformanceState(nvmlPstates_t* pState) const; [[nodiscard]] nvmlReturn_t GetNvmlDeviceUtilizationRates(nvmlUtilization_t* utilization) const; [[nodiscard]] nvmlReturn_t GetNvmlDeviceVbiosVersion(char* version, unsigned int length) const; + [[nodiscard]] nvmlReturn_t GetNvmlDeviceNumGpuCores(unsigned int* numCores) const; [[nodiscard]] nvmlReturn_t GetNvmlDeviceBusType(nvmlBusType_t* type) const; [[nodiscard]] nvmlReturn_t GetNvmlDeviceDynamicPstatesInfo(nvmlGpuDynamicPstatesInfo_t* pDynamicPstatesInfo) const; -- cgit v1.2.3