GPUModel.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. Simple header for detecting Nvidia GPU models
  3. */
  4. namespace GPUModel
  5. {
  6. #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
  7. #include <windows.h>
  8. #include <string>
  9. #include <stdio.h>
  10. #pragma comment(lib, "advapi32")
  11. std::wstring ExePath() {
  12. WCHAR buffer[MAX_PATH];
  13. GetModuleFileNameW(NULL, buffer, MAX_PATH);
  14. return std::wstring(buffer);
  15. }
  16. std::wstring DirPath() {
  17. std::wstring exepath = ExePath();
  18. std::wstring::size_type pos = exepath.find_last_of(L"\\/");
  19. return exepath.substr(0, pos);
  20. }
  21. std::wstring CONFIG_FILE_STRING = DirPath() + L"\\plugins\\config.ini";
  22. LPCWSTR CONFIG_FILE = CONFIG_FILE_STRING.c_str();
  23. void*(*NvAPI_QueryInterface)(unsigned int offset) = NULL;
  24. int(*NvAPI_Initialize)() = NULL;
  25. int(*NvAPI_Unload)() = NULL;
  26. int(*NvAPI_EnumPhysicalGPUs)(int64_t** handle, int* count) = NULL;
  27. int(*NvAPI_GPU_GetShortName)(int64_t* handle, char* name) = NULL;
  28. std::string getGpuName()
  29. {
  30. // detected model can be overridden -- 0: Kepler, 1: Maxwell, 2: Turing
  31. int nGPUModel = GetPrivateProfileIntW(L"gpu", L"model", -1, CONFIG_FILE);
  32. if (nGPUModel >= 0 && nGPUModel <= 2)
  33. {
  34. std::string arch;
  35. switch (nGPUModel)
  36. {
  37. case 0:
  38. arch = "GK000";
  39. break;
  40. case 1:
  41. arch = "GM000";
  42. break;
  43. case 2:
  44. arch = "TU000";
  45. break;
  46. }
  47. printf("[GPUModel] GPU Model override: %s\n", arch.c_str());
  48. return arch;
  49. }
  50. printf("[GPUModel] Checking GPU model\n");
  51. HMODULE hdlNvapi = LoadLibraryW(L"nvapi64.dll");
  52. if (hdlNvapi == NULL) return
  53. std::string("Other");
  54. NvAPI_QueryInterface = (void* (*)(unsigned int offset))GetProcAddress(hdlNvapi, "nvapi_QueryInterface");
  55. if (NvAPI_QueryInterface == nullptr)
  56. return std::string("Other");
  57. NvAPI_Initialize = (int(*)())NvAPI_QueryInterface(0x0150E828);
  58. NvAPI_Unload = (int(*)()) NvAPI_QueryInterface(0xD22BDD7E);
  59. NvAPI_EnumPhysicalGPUs = (int(*)(int64_t * *handle, int* count))NvAPI_QueryInterface(0xE5AC921F);
  60. NvAPI_GPU_GetShortName = (int(*)(int64_t * handle, char* name))NvAPI_QueryInterface(0xD988F0F3);
  61. if (NvAPI_Initialize == nullptr ||
  62. NvAPI_Unload == nullptr ||
  63. NvAPI_EnumPhysicalGPUs == nullptr ||
  64. NvAPI_GPU_GetShortName == nullptr)
  65. return std::string("Unknown");
  66. int64_t* hdlGpu[64] = { 0 };
  67. int nGpu = 0;
  68. NvAPI_Initialize();
  69. NvAPI_EnumPhysicalGPUs(hdlGpu, &nGpu);
  70. //printf("[ShaderPatch] nGpu: %d\n", nGpu);
  71. if (nGpu < 1)
  72. {
  73. NvAPI_Unload();
  74. return std::string("Unknown");
  75. }
  76. char nameBuf[64];
  77. NvAPI_GPU_GetShortName(hdlGpu[0], nameBuf);
  78. NvAPI_Unload();
  79. return std::string(nameBuf);
  80. }
  81. }