From 3f11ae5bb42bd5a18f4d02688f47dc919f2d77ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Sun, 1 Oct 2017 19:45:48 -0700 Subject: [PATCH] Moved nvapi integration into separate file. --- src/amalgamated.cpp | 3 +- src/nvapi.cpp | 172 +++++++++++++++++++++++++++++++++++++++++ src/nvapi.h | 34 ++++++++ src/renderer_d3d11.cpp | 152 ++---------------------------------- src/renderer_d3d11.h | 1 + 5 files changed, 214 insertions(+), 148 deletions(-) create mode 100644 src/nvapi.cpp create mode 100644 src/nvapi.h diff --git a/src/amalgamated.cpp b/src/amalgamated.cpp index a2975ca01..3a940d0b1 100644 --- a/src/amalgamated.cpp +++ b/src/amalgamated.cpp @@ -4,13 +4,14 @@ */ #include "bgfx.cpp" +#include "debug_renderdoc.cpp" #include "glcontext_egl.cpp" #include "glcontext_glx.cpp" #include "glcontext_wgl.cpp" #include "hmd.cpp" #include "hmd_ovr.cpp" #include "hmd_openvr.cpp" -#include "debug_renderdoc.cpp" +#include "nvapi.cpp" #include "renderer_d3d9.cpp" #include "renderer_d3d11.cpp" #include "renderer_d3d12.cpp" diff --git a/src/nvapi.cpp b/src/nvapi.cpp new file mode 100644 index 000000000..779b0ecdb --- /dev/null +++ b/src/nvapi.cpp @@ -0,0 +1,172 @@ +/* + * Copyright 2011-2017 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause + */ + +#include "bgfx_p.h" +#include "nvapi.h" + +namespace bgfx +{ + /* + * NVAPI + * + * Reference: + * http://docs.nvidia.com/gameworks/content/gameworkslibrary/coresdk/nvapi/index.html + * https://github.com/jNizM/AHK_NVIDIA_NvAPI/blob/master/info/NvAPI_IDs.txt + */ + struct NvPhysicalGpuHandle; + +#define NVAPI_MAX_PHYSICAL_GPUS 64 + + enum NvApiStatus + { + NVAPI_OK = 0, + NVAPI_ERROR = -1, + }; + + struct NvMemoryInfoV2 + { + NvMemoryInfoV2() + : version(sizeof(NvMemoryInfoV2) | (2 << 16) ) + { + } + + uint32_t version; + uint32_t dedicatedVideoMemory; + uint32_t availableDedicatedVideoMemory; + uint32_t systemVideoMemory; + uint32_t sharedSystemMemory; + uint32_t curAvailableDedicatedVideoMemory; + }; + + typedef void* (__cdecl* PFN_NVAPI_QUERYINTERFACE)(uint32_t _functionOffset); + typedef NvApiStatus (__cdecl* PFN_NVAPI_INITIALIZE)(); + typedef NvApiStatus (__cdecl* PFN_NVAPI_UNLOAD)(); + typedef NvApiStatus (__cdecl* PFN_NVAPI_ENUMPHYSICALGPUS)(NvPhysicalGpuHandle* _handle[NVAPI_MAX_PHYSICAL_GPUS], uint32_t* _gpuCount); + typedef NvApiStatus (__cdecl* PFN_NVAPI_GPUGETMEMORYINFO)(NvPhysicalGpuHandle* _handle, NvMemoryInfoV2* _memoryInfo); + typedef NvApiStatus (__cdecl* PFN_NVAPI_GPUGETFULLNAME)(NvPhysicalGpuHandle* _physicalGpu, char _name[64]); + +#define NVAPI_INITIALIZE UINT32_C(0x0150e828) +#define NVAPI_UNLOAD UINT32_C(0xd22bdd7e) +#define NVAPI_ENUMPHYSICALGPUS UINT32_C(0xe5ac921f) +#define NVAPI_GPUGETMEMORYINFO UINT32_C(0x07f9b368) +#define NVAPI_GPUGETFULLNAME UINT32_C(0xceee8e9f) + + static PFN_NVAPI_QUERYINTERFACE nvApiQueryInterface; + static PFN_NVAPI_INITIALIZE nvApiInitialize; + static PFN_NVAPI_UNLOAD nvApiUnload; + static PFN_NVAPI_ENUMPHYSICALGPUS nvApiEnumPhysicalGPUs; + static PFN_NVAPI_GPUGETMEMORYINFO nvApiGpuGetMemoryInfo; + static PFN_NVAPI_GPUGETFULLNAME nvApiGpuGetFullName; + + NvApi::NvApi() + : m_nvApiDll(NULL) + , m_nvGpu(NULL) + { + } + + void NvApi::init() + { + m_nvGpu = NULL; + m_nvApiDll = bx::dlopen( +#if BX_ARCH_32BIT + "nvapi.dll" +#else + "nvapi64.dll" +#endif // BX_ARCH_32BIT + ); + + if (NULL != m_nvApiDll) + { + nvApiQueryInterface = (PFN_NVAPI_QUERYINTERFACE)bx::dlsym(m_nvApiDll, "nvapi_QueryInterface"); + + bool initialized = NULL != nvApiQueryInterface; + + if (initialized) + { + nvApiInitialize = (PFN_NVAPI_INITIALIZE )nvApiQueryInterface(NVAPI_INITIALIZE); + nvApiUnload = (PFN_NVAPI_UNLOAD )nvApiQueryInterface(NVAPI_UNLOAD); + nvApiEnumPhysicalGPUs = (PFN_NVAPI_ENUMPHYSICALGPUS )nvApiQueryInterface(NVAPI_ENUMPHYSICALGPUS); + nvApiGpuGetMemoryInfo = (PFN_NVAPI_GPUGETMEMORYINFO )nvApiQueryInterface(NVAPI_GPUGETMEMORYINFO); + nvApiGpuGetFullName = (PFN_NVAPI_GPUGETFULLNAME )nvApiQueryInterface(NVAPI_GPUGETFULLNAME); + + initialized = true + && NULL != nvApiInitialize + && NULL != nvApiUnload + && NULL != nvApiEnumPhysicalGPUs + && NULL != nvApiGpuGetMemoryInfo + && NULL != nvApiGpuGetFullName + && NVAPI_OK == nvApiInitialize() + ; + + if (initialized) + { + NvPhysicalGpuHandle* physicalGpus[NVAPI_MAX_PHYSICAL_GPUS]; + uint32_t numGpus = 0; + nvApiEnumPhysicalGPUs(physicalGpus, &numGpus); + + initialized = 0 < numGpus; + if (initialized) + { + m_nvGpu = physicalGpus[0]; + } + + char name[64]; + nvApiGpuGetFullName(m_nvGpu, name); + BX_TRACE("%s", name); + } + + initialized = NULL != m_nvGpu; + + if (!initialized) + { + nvApiUnload(); + } + } + + if (!initialized) + { + bx::dlclose(m_nvApiDll); + m_nvApiDll = NULL; + } + + BX_WARN(!initialized, "NVAPI supported."); + } + } + + void NvApi::shutdown() + { + if (NULL != m_nvGpu) + { + nvApiUnload(); + m_nvGpu = NULL; + } + + bx::dlclose(m_nvApiDll); + m_nvApiDll = NULL; + } + + void NvApi::getMemoryInfo(int64_t& _gpuMemoryUsed, int64_t& _gpuMemoryMax) + { + _gpuMemoryMax = -INT64_MAX; + _gpuMemoryUsed = -INT64_MAX; + + if (NULL != m_nvGpu) + { + NvMemoryInfoV2 memInfo; + NvApiStatus status = nvApiGpuGetMemoryInfo(m_nvGpu, &memInfo); + if (NVAPI_OK == status) + { + _gpuMemoryMax = 1024 * memInfo.availableDedicatedVideoMemory; + _gpuMemoryUsed = 1024 * (memInfo.availableDedicatedVideoMemory - memInfo.curAvailableDedicatedVideoMemory); +// BX_TRACE(" dedicatedVideoMemory: %d KiB", memInfo.dedicatedVideoMemory); +// BX_TRACE(" availableDedicatedVideoMemory: %d KiB", memInfo.availableDedicatedVideoMemory); +// BX_TRACE(" systemVideoMemory: %d KiB", memInfo.systemVideoMemory); +// BX_TRACE(" sharedSystemMemory: %d KiB", memInfo.sharedSystemMemory); +// BX_TRACE("curAvailableDedicatedVideoMemory: %d KiB", memInfo.curAvailableDedicatedVideoMemory); + } + } + } + +} // namespace bgfx diff --git a/src/nvapi.h b/src/nvapi.h new file mode 100644 index 000000000..ce08052b3 --- /dev/null +++ b/src/nvapi.h @@ -0,0 +1,34 @@ +/* + * Copyright 2011-2017 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause + */ + +#ifndef BGFX_NVAPI_H_HEADER_GUARD +#define BGFX_NVAPI_H_HEADER_GUARD + +namespace bgfx +{ + struct NvPhysicalGpuHandle; + + /// + struct NvApi + { + /// + NvApi(); + + /// + void init(); + + /// + void shutdown(); + + /// + void getMemoryInfo(int64_t& _gpuMemoryUsed, int64_t& _gpuMemoryMax); + + void* m_nvApiDll; + NvPhysicalGpuHandle* m_nvGpu; + }; + +} // namespace bgfx + +#endif // BGFX_NVAPI_H_HEADER_GUARD diff --git a/src/renderer_d3d11.cpp b/src/renderer_d3d11.cpp index 09d18d947..8ad0b63c6 100644 --- a/src/renderer_d3d11.cpp +++ b/src/renderer_d3d11.cpp @@ -629,56 +629,6 @@ namespace bgfx { namespace d3d11 static MultiDrawIndirectFn multiDrawInstancedIndirect; static MultiDrawIndirectFn multiDrawIndexedInstancedIndirect; - /* - * NVAPI - * - * Reference: - * http://docs.nvidia.com/gameworks/content/gameworkslibrary/coresdk/nvapi/index.html - * https://github.com/jNizM/AHK_NVIDIA_NvAPI/blob/master/info/NvAPI_IDs.txt - */ - struct NvDisplayHandle; - struct NvPhysicalGpuHandle; - -#define NVAPI_MAX_PHYSICAL_GPUS 64 - - enum NvApiStatus - { - NVAPI_OK = 0, - NVAPI_ERROR = -1, - }; - - struct NvMemoryInfoV2 - { - NvMemoryInfoV2() - : version(sizeof(NvMemoryInfoV2) | (2 << 16) ) - { - } - - uint32_t version; - uint32_t dedicatedVideoMemory; - uint32_t availableDedicatedVideoMemory; - uint32_t systemVideoMemory; - uint32_t sharedSystemMemory; - uint32_t curAvailableDedicatedVideoMemory; - }; - - typedef void* (__cdecl* PFN_NVAPI_QUERYINTERFACE)(uint32_t _functionOffset); - typedef NvApiStatus (__cdecl* PFN_NVAPI_INITIALIZE)(); - typedef NvApiStatus (__cdecl* PFN_NVAPI_UNLOAD)(); - typedef NvApiStatus (__cdecl* PFN_NVAPI_ENUMPHYSICALGPUS)(NvPhysicalGpuHandle* _handle[NVAPI_MAX_PHYSICAL_GPUS], uint32_t* _gpuCount); - typedef NvApiStatus (__cdecl* PFN_NVAPI_GPUGETMEMORYINFO)(NvPhysicalGpuHandle* _handle, NvMemoryInfoV2* _memoryInfo); - -#define NVAPI_INITIALIZE UINT32_C(0x0150e828) -#define NVAPI_UNLOAD UINT32_C(0xd22bdd7e) -#define NVAPI_ENUMPHYSICALGPUS UINT32_C(0xe5ac921f) -#define NVAPI_GPUGETMEMORYINFO UINT32_C(0x07f9b368) - - static PFN_NVAPI_QUERYINTERFACE nvApiQueryInterface; - static PFN_NVAPI_INITIALIZE nvApiInitialize; - static PFN_NVAPI_UNLOAD nvApiUnload; - static PFN_NVAPI_ENUMPHYSICALGPUS nvApiEnumPhysicalGPUs; - static PFN_NVAPI_GPUGETMEMORYINFO nvApiGpuGetMemoryInfo; - #if USE_D3D11_DYNAMIC_LIB static PFN_D3D11_CREATE_DEVICE D3D11CreateDevice; static PFN_CREATE_DXGI_FACTORY CreateDXGIFactory; @@ -722,8 +672,6 @@ namespace bgfx { namespace d3d11 , m_renderdocdll(NULL) , m_agsdll(NULL) , m_ags(NULL) - , m_nvapidll(NULL) - , m_nvGpu(NULL) , m_driverType(D3D_DRIVER_TYPE_NULL) , m_featureLevel(D3D_FEATURE_LEVEL(0) ) , m_adapter(NULL) @@ -870,65 +818,7 @@ namespace bgfx { namespace d3d11 } } - m_nvGpu = NULL; - m_nvapidll = bx::dlopen( -#if BX_ARCH_32BIT - "nvapi.dll" -#else - "nvapi64.dll" -#endif // BX_ARCH_32BIT - ); - - if (NULL != m_nvapidll) - { - nvApiQueryInterface = (PFN_NVAPI_QUERYINTERFACE)bx::dlsym(m_nvapidll, "nvapi_QueryInterface"); - - bool initialized = NULL != nvApiQueryInterface; - - if (initialized) - { - nvApiInitialize = (PFN_NVAPI_INITIALIZE )nvApiQueryInterface(NVAPI_INITIALIZE); - nvApiUnload = (PFN_NVAPI_UNLOAD )nvApiQueryInterface(NVAPI_UNLOAD); - nvApiEnumPhysicalGPUs = (PFN_NVAPI_ENUMPHYSICALGPUS )nvApiQueryInterface(NVAPI_ENUMPHYSICALGPUS); - nvApiGpuGetMemoryInfo = (PFN_NVAPI_GPUGETMEMORYINFO )nvApiQueryInterface(NVAPI_GPUGETMEMORYINFO); - - initialized = true - && NULL != nvApiInitialize - && NULL != nvApiUnload - && NULL != nvApiEnumPhysicalGPUs - && NULL != nvApiGpuGetMemoryInfo - && NVAPI_OK == nvApiInitialize() - ; - - if (initialized) - { - NvPhysicalGpuHandle* physicalGpus[NVAPI_MAX_PHYSICAL_GPUS]; - uint32_t numGpus = 0; - nvApiEnumPhysicalGPUs(physicalGpus, &numGpus); - - initialized = 0 < numGpus; - if (initialized) - { - m_nvGpu = physicalGpus[0]; - } - } - - initialized = NULL != m_nvGpu; - - if (!initialized) - { - nvApiUnload(); - } - } - - if (!initialized) - { - bx::dlclose(m_nvapidll); - m_nvapidll = NULL; - } - - BX_WARN(!initialized, "NVAPI supported."); - } + m_nvapi.init(); #if USE_D3D11_DYNAMIC_LIB m_d3d11dll = bx::dlopen("d3d11.dll"); @@ -1818,14 +1708,7 @@ BX_PRAGMA_DIAGNOSTIC_POP(); case ErrorState::Default: default: - if (NULL != m_nvGpu) - { - nvApiUnload(); - m_nvGpu = NULL; - } - - bx::dlclose(m_nvapidll); - m_nvapidll = NULL; + m_nvapi.shutdown(); if (NULL != m_ags) { @@ -1849,14 +1732,7 @@ BX_PRAGMA_DIAGNOSTIC_POP(); preReset(); m_ovr.shutdown(); - if (NULL != m_nvGpu) - { - nvApiUnload(); - m_nvGpu = NULL; - } - - bx::dlclose(m_nvapidll); - m_nvapidll = NULL; + m_nvapi.shutdown(); if (NULL != m_ags) { @@ -3789,9 +3665,8 @@ BX_PRAGMA_DIAGNOSTIC_POP(); void* m_renderdocdll; void* m_agsdll; AGSContext* m_ags; - void* m_nvapidll; - NvPhysicalGpuHandle* m_nvGpu; + NvApi m_nvapi; D3D_DRIVER_TYPE m_driverType; D3D_FEATURE_LEVEL m_featureLevel; @@ -6618,24 +6493,7 @@ BX_PRAGMA_DIAGNOSTIC_POP(); perfStats.numDraw = statsKeyType[0]; perfStats.numCompute = statsKeyType[1]; perfStats.maxGpuLatency = maxGpuLatency; - perfStats.gpuMemoryMax = -INT64_MAX; - perfStats.gpuMemoryUsed = -INT64_MAX; - - if (NULL != m_nvGpu) - { - NvMemoryInfoV2 memInfo; - NvApiStatus status = nvApiGpuGetMemoryInfo(m_nvGpu, &memInfo); - if (NVAPI_OK == status) - { - perfStats.gpuMemoryMax = 1024 * memInfo.availableDedicatedVideoMemory; - perfStats.gpuMemoryUsed = 1024 * (memInfo.availableDedicatedVideoMemory - memInfo.curAvailableDedicatedVideoMemory); -// BX_TRACE(" dedicatedVideoMemory: %d KiB", memInfo.dedicatedVideoMemory); -// BX_TRACE(" availableDedicatedVideoMemory: %d KiB", memInfo.availableDedicatedVideoMemory); -// BX_TRACE(" systemVideoMemory: %d KiB", memInfo.systemVideoMemory); -// BX_TRACE(" sharedSystemMemory: %d KiB", memInfo.sharedSystemMemory); -// BX_TRACE("curAvailableDedicatedVideoMemory: %d KiB", memInfo.curAvailableDedicatedVideoMemory); - } - } + m_nvapi.getMemoryInfo(perfStats.gpuMemoryUsed, perfStats.gpuMemoryMax); if (_render->m_debug & (BGFX_DEBUG_IFH|BGFX_DEBUG_STATS) ) { diff --git a/src/renderer_d3d11.h b/src/renderer_d3d11.h index deb821503..c7b4fd17b 100644 --- a/src/renderer_d3d11.h +++ b/src/renderer_d3d11.h @@ -36,6 +36,7 @@ BX_PRAGMA_DIAGNOSTIC_POP() #include "hmd.h" #include "hmd_openvr.h" #include "debug_renderdoc.h" +#include "nvapi.h" #ifndef D3DCOLOR_ARGB # define D3DCOLOR_ARGB(_a, _r, _g, _b) ( (DWORD)( ( ( (_a)&0xff)<<24)|( ( (_r)&0xff)<<16)|( ( (_g)&0xff)<<8)|( (_b)&0xff) ) )