debug_renderdoc.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright 2011-2017 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "bgfx_p.h"
  6. #if BGFX_CONFIG_DEBUG_PIX && (BX_PLATFORM_WINDOWS || BX_PLATFORM_LINUX)
  7. # if BX_PLATFORM_WINDOWS
  8. # include <psapi.h>
  9. # endif // BX_PLATFORM_WINDOWS
  10. # include <renderdoc/renderdoc_app.h>
  11. namespace bgfx
  12. {
  13. bool findModule(const char* _name)
  14. {
  15. #if BX_PLATFORM_WINDOWS
  16. HANDLE process = GetCurrentProcess();
  17. DWORD size;
  18. BOOL result = EnumProcessModules(process
  19. , NULL
  20. , 0
  21. , &size
  22. );
  23. if (0 != result)
  24. {
  25. HMODULE* modules = (HMODULE*)alloca(size);
  26. result = EnumProcessModules(process
  27. , modules
  28. , size
  29. , &size
  30. );
  31. if (0 != result)
  32. {
  33. char moduleName[MAX_PATH];
  34. for (uint32_t ii = 0, num = uint32_t(size/sizeof(HMODULE) ); ii < num; ++ii)
  35. {
  36. result = GetModuleBaseNameA(process
  37. , modules[ii]
  38. , moduleName
  39. , BX_COUNTOF(moduleName)
  40. );
  41. if (0 != result
  42. && 0 == bx::strCmpI(_name, moduleName) )
  43. {
  44. return true;
  45. }
  46. }
  47. }
  48. }
  49. #endif // BX_PLATFORM_WINDOWS
  50. BX_UNUSED(_name);
  51. return false;
  52. }
  53. pRENDERDOC_GetAPI RENDERDOC_GetAPI;
  54. static RENDERDOC_API_1_1_0* s_renderDoc = NULL;
  55. static void* s_renderDocDll = NULL;
  56. void* loadRenderDoc()
  57. {
  58. if (NULL != s_renderDoc)
  59. {
  60. return s_renderDocDll;
  61. }
  62. // Skip loading RenderDoc when IntelGPA is present to avoid RenderDoc crash.
  63. if (findModule(BX_ARCH_32BIT ? "shimloader32.dll" : "shimloader64.dll") )
  64. {
  65. return NULL;
  66. }
  67. void* renderDocDll = bx::dlopen("renderdoc.dll");
  68. if (NULL != renderDocDll)
  69. {
  70. RENDERDOC_GetAPI = (pRENDERDOC_GetAPI)bx::dlsym(renderDocDll, "RENDERDOC_GetAPI");
  71. if (NULL != RENDERDOC_GetAPI
  72. && 1 == RENDERDOC_GetAPI(eRENDERDOC_API_Version_1_1_0, (void**)&s_renderDoc) )
  73. {
  74. s_renderDoc->SetLogFilePathTemplate(BGFX_CONFIG_RENDERDOC_LOG_FILEPATH);
  75. s_renderDoc->SetFocusToggleKeys(NULL, 0);
  76. RENDERDOC_InputButton captureKeys[] = BGFX_CONFIG_RENDERDOC_CAPTURE_KEYS;
  77. s_renderDoc->SetCaptureKeys(captureKeys, BX_COUNTOF(captureKeys) );
  78. s_renderDoc->SetCaptureOptionU32(eRENDERDOC_Option_AllowVSync, 1);
  79. s_renderDoc->SetCaptureOptionU32(eRENDERDOC_Option_SaveAllInitials, 1);
  80. s_renderDoc->MaskOverlayBits(eRENDERDOC_Overlay_None, eRENDERDOC_Overlay_None);
  81. s_renderDocDll = renderDocDll;
  82. }
  83. else
  84. {
  85. bx::dlclose(renderDocDll);
  86. renderDocDll = NULL;
  87. }
  88. }
  89. return renderDocDll;
  90. }
  91. void unloadRenderDoc(void* _renderdocdll)
  92. {
  93. if (NULL != _renderdocdll)
  94. {
  95. // BK - Once RenderDoc is loaded there shouldn't be calls
  96. // to Shutdown or unload RenderDoc DLL.
  97. // https://github.com/bkaradzic/bgfx/issues/1192
  98. //
  99. // s_renderDoc->Shutdown();
  100. // bx::dlclose(_renderdocdll);
  101. }
  102. }
  103. void renderDocTriggerCapture()
  104. {
  105. if (NULL != s_renderDoc)
  106. {
  107. s_renderDoc->TriggerCapture();
  108. }
  109. }
  110. } // namespace bgfx
  111. #else
  112. namespace bgfx
  113. {
  114. void* loadRenderDoc()
  115. {
  116. return NULL;
  117. }
  118. void unloadRenderDoc(void*)
  119. {
  120. }
  121. void renderDocTriggerCapture()
  122. {
  123. }
  124. } // namespace bgfx
  125. #endif // BGFX_CONFIG_DEBUG_PIX && (BX_PLATFORM_WINDOWS || BX_PLATFORM_LINUX)