renderdoc.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright 2011-2015 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "bgfx_p.h"
  6. #if BGFX_CONFIG_DEBUG_PIX && !BGFX_CONFIG_RENDERER_OPENGLES && (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::stricmp(_name, moduleName) )
  43. {
  44. return true;
  45. }
  46. }
  47. }
  48. }
  49. #endif // BX_PLATFORM_WINDOWS
  50. BX_UNUSED(_name);
  51. return false;
  52. }
  53. #define RENDERDOC_IMPORT \
  54. RENDERDOC_IMPORT_FUNC(RENDERDOC_Shutdown); \
  55. RENDERDOC_IMPORT_FUNC(RENDERDOC_SetLogFile); \
  56. RENDERDOC_IMPORT_FUNC(RENDERDOC_GetCapture); \
  57. RENDERDOC_IMPORT_FUNC(RENDERDOC_SetCaptureOptions); \
  58. RENDERDOC_IMPORT_FUNC(RENDERDOC_SetActiveWindow); \
  59. RENDERDOC_IMPORT_FUNC(RENDERDOC_TriggerCapture); \
  60. RENDERDOC_IMPORT_FUNC(RENDERDOC_StartFrameCapture); \
  61. RENDERDOC_IMPORT_FUNC(RENDERDOC_EndFrameCapture); \
  62. RENDERDOC_IMPORT_FUNC(RENDERDOC_GetOverlayBits); \
  63. RENDERDOC_IMPORT_FUNC(RENDERDOC_MaskOverlayBits); \
  64. RENDERDOC_IMPORT_FUNC(RENDERDOC_SetFocusToggleKeys); \
  65. RENDERDOC_IMPORT_FUNC(RENDERDOC_SetCaptureKeys); \
  66. RENDERDOC_IMPORT_FUNC(RENDERDOC_InitRemoteAccess); \
  67. RENDERDOC_IMPORT_FUNC(RENDERDOC_UnloadCrashHandler);
  68. #define RENDERDOC_IMPORT_FUNC(_func) p##_func _func
  69. RENDERDOC_IMPORT
  70. #undef RENDERDOC_IMPORT_FUNC
  71. pRENDERDOC_GetAPIVersion RENDERDOC_GetAPIVersion;
  72. void* loadRenderDoc()
  73. {
  74. // Skip loading RenderDoc when IntelGPA is present to avoid RenderDoc crash.
  75. if (findModule(BX_ARCH_32BIT ? "shimloader32.dll" : "shimloader64.dll") )
  76. {
  77. return NULL;
  78. }
  79. void* renderdocdll = bx::dlopen("renderdoc.dll");
  80. if (NULL != renderdocdll)
  81. {
  82. RENDERDOC_GetAPIVersion = (pRENDERDOC_GetAPIVersion)bx::dlsym(renderdocdll, "RENDERDOC_GetAPIVersion");
  83. if (NULL != RENDERDOC_GetAPIVersion
  84. && RENDERDOC_API_VERSION == RENDERDOC_GetAPIVersion() )
  85. {
  86. #define RENDERDOC_IMPORT_FUNC(_func) \
  87. _func = (p##_func)bx::dlsym(renderdocdll, #_func); \
  88. BX_TRACE("%p " #_func, _func);
  89. RENDERDOC_IMPORT
  90. #undef RENDERDOC_IMPORT_FUNC
  91. RENDERDOC_SetLogFile("temp/bgfx");
  92. RENDERDOC_SetFocusToggleKeys(NULL, 0);
  93. KeyButton captureKey = eKey_F11;
  94. RENDERDOC_SetCaptureKeys(&captureKey, 1);
  95. CaptureOptions opt;
  96. memset(&opt, 0, sizeof(opt) );
  97. opt.AllowVSync = 1;
  98. opt.SaveAllInitials = 1;
  99. RENDERDOC_SetCaptureOptions(&opt);
  100. uint32_t ident = 0;
  101. RENDERDOC_InitRemoteAccess(&ident);
  102. RENDERDOC_MaskOverlayBits(eOverlay_None, eOverlay_None);
  103. }
  104. else
  105. {
  106. bx::dlclose(renderdocdll);
  107. renderdocdll = NULL;
  108. }
  109. }
  110. return renderdocdll;
  111. }
  112. void unloadRenderDoc(void* _renderdocdll)
  113. {
  114. if (NULL != _renderdocdll)
  115. {
  116. RENDERDOC_Shutdown();
  117. bx::dlclose(_renderdocdll);
  118. }
  119. }
  120. } // namespace bgfx
  121. #else
  122. namespace bgfx
  123. {
  124. void* loadRenderDoc()
  125. {
  126. return NULL;
  127. }
  128. void unloadRenderDoc(void*)
  129. {
  130. }
  131. } // namespace bgfx
  132. #endif // BGFX_CONFIG_DEBUG_PIX && (BX_PLATFORM_WINDOWS || BX_PLATFORM_LINUX)