gl-capture.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. #ifdef _MSC_VER
  2. #pragma warning(disable : 4214) /* nonstandard extension, non-int bitfield */
  3. #pragma warning(disable : 4054) /* function pointer to data pointer */
  4. #endif
  5. #include <windows.h>
  6. #define COBJMACROS
  7. #include <dxgi.h>
  8. #include <d3d11.h>
  9. #include "gl-decs.h"
  10. #include "graphics-hook.h"
  11. #include <detours.h>
  12. #define DUMMY_WINDOW_CLASS_NAME L"graphics_hook_gl_dummy_window"
  13. /* clang-format off */
  14. static const GUID GUID_IDXGIFactory1 =
  15. {0x770aae78, 0xf26f, 0x4dba, {0xa8, 0x29, 0x25, 0x3c, 0x83, 0xd1, 0xb3, 0x87}};
  16. static const GUID GUID_IDXGIResource =
  17. {0x035f3ab4, 0x482e, 0x4e50, {0xb4, 0x1f, 0x8a, 0x7f, 0x8b, 0xd8, 0x96, 0x0b}};
  18. /* clang-format on */
  19. typedef BOOL(WINAPI *PFN_SwapBuffers)(HDC);
  20. typedef BOOL(WINAPI *PFN_WglSwapLayerBuffers)(HDC, UINT);
  21. typedef BOOL(WINAPI *PFN_WglSwapBuffers)(HDC);
  22. typedef BOOL(WINAPI *PFN_WglDeleteContext)(HGLRC);
  23. PFN_SwapBuffers RealSwapBuffers = NULL;
  24. PFN_WglSwapLayerBuffers RealWglSwapLayerBuffers = NULL;
  25. PFN_WglSwapBuffers RealWglSwapBuffers = NULL;
  26. PFN_WglDeleteContext RealWglDeleteContext = NULL;
  27. static bool darkest_dungeon_fix = false;
  28. static bool functions_initialized = false;
  29. struct gl_data {
  30. HDC hdc;
  31. uint32_t cx;
  32. uint32_t cy;
  33. DXGI_FORMAT format;
  34. GLuint fbo;
  35. bool using_shtex;
  36. bool shmem_fallback;
  37. union {
  38. /* shared texture */
  39. struct {
  40. struct shtex_data *shtex_info;
  41. ID3D11Device *d3d11_device;
  42. ID3D11DeviceContext *d3d11_context;
  43. ID3D11Texture2D *d3d11_tex;
  44. IDXGISwapChain *dxgi_swap;
  45. HANDLE gl_device;
  46. HANDLE gl_dxobj;
  47. HANDLE handle;
  48. HWND hwnd;
  49. GLuint texture;
  50. };
  51. /* shared memory */
  52. struct {
  53. struct shmem_data *shmem_info;
  54. int cur_tex;
  55. int copy_wait;
  56. GLuint pbos[NUM_BUFFERS];
  57. GLuint textures[NUM_BUFFERS];
  58. bool texture_ready[NUM_BUFFERS];
  59. bool texture_mapped[NUM_BUFFERS];
  60. };
  61. };
  62. };
  63. static HMODULE gl = NULL;
  64. static bool nv_capture_available = false;
  65. static struct gl_data data = {0};
  66. __declspec(thread) static int swap_recurse;
  67. static inline bool gl_error(const char *func, const char *str)
  68. {
  69. GLenum error = glGetError();
  70. if (error != 0) {
  71. hlog("%s: %s: %lu", func, str, error);
  72. return true;
  73. }
  74. return false;
  75. }
  76. static void gl_free(void)
  77. {
  78. capture_free();
  79. if (data.using_shtex) {
  80. if (data.gl_dxobj)
  81. jimglDXUnregisterObjectNV(data.gl_device,
  82. data.gl_dxobj);
  83. if (data.gl_device)
  84. jimglDXCloseDeviceNV(data.gl_device);
  85. if (data.texture)
  86. glDeleteTextures(1, &data.texture);
  87. if (data.d3d11_tex)
  88. ID3D11Resource_Release(data.d3d11_tex);
  89. if (data.d3d11_context)
  90. ID3D11DeviceContext_Release(data.d3d11_context);
  91. if (data.d3d11_device)
  92. ID3D11Device_Release(data.d3d11_device);
  93. if (data.dxgi_swap)
  94. IDXGISwapChain_Release(data.dxgi_swap);
  95. if (data.hwnd)
  96. DestroyWindow(data.hwnd);
  97. } else {
  98. for (size_t i = 0; i < NUM_BUFFERS; i++) {
  99. if (data.pbos[i]) {
  100. if (data.texture_mapped[i]) {
  101. glBindBuffer(GL_PIXEL_PACK_BUFFER,
  102. data.pbos[i]);
  103. glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
  104. glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
  105. }
  106. glDeleteBuffers(1, &data.pbos[i]);
  107. }
  108. if (data.textures[i])
  109. glDeleteTextures(1, &data.textures[i]);
  110. }
  111. }
  112. if (data.fbo)
  113. glDeleteFramebuffers(1, &data.fbo);
  114. gl_error("gl_free", "GL error occurred on free");
  115. memset(&data, 0, sizeof(data));
  116. hlog("------------------ gl capture freed ------------------");
  117. }
  118. static inline void *base_get_proc(const char *name)
  119. {
  120. return (void *)GetProcAddress(gl, name);
  121. }
  122. static inline void *wgl_get_proc(const char *name)
  123. {
  124. return (void *)jimglGetProcAddress(name);
  125. }
  126. static inline void *get_proc(const char *name)
  127. {
  128. void *func = wgl_get_proc(name);
  129. if (!func)
  130. func = base_get_proc(name);
  131. return func;
  132. }
  133. static void init_nv_functions(void)
  134. {
  135. jimglDXSetResourceShareHandleNV =
  136. get_proc("wglDXSetResourceShareHandleNV");
  137. jimglDXOpenDeviceNV = get_proc("wglDXOpenDeviceNV");
  138. jimglDXCloseDeviceNV = get_proc("wglDXCloseDeviceNV");
  139. jimglDXRegisterObjectNV = get_proc("wglDXRegisterObjectNV");
  140. jimglDXUnregisterObjectNV = get_proc("wglDXUnregisterObjectNV");
  141. jimglDXObjectAccessNV = get_proc("wglDXObjectAccessNV");
  142. jimglDXLockObjectsNV = get_proc("wglDXLockObjectsNV");
  143. jimglDXUnlockObjectsNV = get_proc("wglDXUnlockObjectsNV");
  144. nv_capture_available =
  145. !!jimglDXSetResourceShareHandleNV && !!jimglDXOpenDeviceNV &&
  146. !!jimglDXCloseDeviceNV && !!jimglDXRegisterObjectNV &&
  147. !!jimglDXUnregisterObjectNV && !!jimglDXObjectAccessNV &&
  148. !!jimglDXLockObjectsNV && !!jimglDXUnlockObjectsNV;
  149. if (nv_capture_available)
  150. hlog("Shared-texture OpenGL capture available");
  151. }
  152. #define GET_PROC(cur_func, ptr, func) \
  153. do { \
  154. ptr = get_proc(#func); \
  155. if (!ptr) { \
  156. hlog("%s: failed to get function '%s'", #cur_func, \
  157. #func); \
  158. success = false; \
  159. } \
  160. } while (false)
  161. static bool init_gl_functions(void)
  162. {
  163. bool success = true;
  164. jimglGetProcAddress = base_get_proc("wglGetProcAddress");
  165. if (!jimglGetProcAddress) {
  166. hlog("init_gl_functions: failed to get wglGetProcAddress");
  167. return false;
  168. }
  169. GET_PROC(init_gl_functions, jimglMakeCurrent, wglMakeCurrent);
  170. GET_PROC(init_gl_functions, jimglGetCurrentDC, wglGetCurrentDC);
  171. GET_PROC(init_gl_functions, jimglGetCurrentContext,
  172. wglGetCurrentContext);
  173. GET_PROC(init_gl_functions, glTexImage2D, glTexImage2D);
  174. GET_PROC(init_gl_functions, glReadBuffer, glReadBuffer);
  175. GET_PROC(init_gl_functions, glGetTexImage, glGetTexImage);
  176. GET_PROC(init_gl_functions, glDrawBuffer, glDrawBuffer);
  177. GET_PROC(init_gl_functions, glGetError, glGetError);
  178. GET_PROC(init_gl_functions, glBufferData, glBufferData);
  179. GET_PROC(init_gl_functions, glDeleteBuffers, glDeleteBuffers);
  180. GET_PROC(init_gl_functions, glDeleteTextures, glDeleteTextures);
  181. GET_PROC(init_gl_functions, glGenBuffers, glGenBuffers);
  182. GET_PROC(init_gl_functions, glGenTextures, glGenTextures);
  183. GET_PROC(init_gl_functions, glMapBuffer, glMapBuffer);
  184. GET_PROC(init_gl_functions, glUnmapBuffer, glUnmapBuffer);
  185. GET_PROC(init_gl_functions, glBindBuffer, glBindBuffer);
  186. GET_PROC(init_gl_functions, glGetIntegerv, glGetIntegerv);
  187. GET_PROC(init_gl_functions, glBindTexture, glBindTexture);
  188. GET_PROC(init_gl_functions, glGenFramebuffers, glGenFramebuffers);
  189. GET_PROC(init_gl_functions, glDeleteFramebuffers, glDeleteFramebuffers);
  190. GET_PROC(init_gl_functions, glBindFramebuffer, glBindFramebuffer);
  191. GET_PROC(init_gl_functions, glBlitFramebuffer, glBlitFramebuffer);
  192. GET_PROC(init_gl_functions, glFramebufferTexture2D,
  193. glFramebufferTexture2D);
  194. init_nv_functions();
  195. return success;
  196. }
  197. static void get_window_size(HDC hdc, uint32_t *cx, uint32_t *cy)
  198. {
  199. HWND hwnd = WindowFromDC(hdc);
  200. RECT rc = {0};
  201. if (darkest_dungeon_fix) {
  202. *cx = 1920;
  203. *cy = 1080;
  204. } else {
  205. GetClientRect(hwnd, &rc);
  206. *cx = rc.right;
  207. *cy = rc.bottom;
  208. }
  209. }
  210. static inline bool gl_shtex_init_window(void)
  211. {
  212. data.hwnd = CreateWindowExW(
  213. 0, DUMMY_WINDOW_CLASS_NAME, L"Dummy GL window, ignore",
  214. WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 0, 0, 2, 2, NULL,
  215. NULL, GetModuleHandle(NULL), NULL);
  216. if (!data.hwnd) {
  217. hlog("gl_shtex_init_window: failed to create window: %d",
  218. GetLastError());
  219. return false;
  220. }
  221. return true;
  222. }
  223. typedef HRESULT(WINAPI *create_dxgi_factory1_t)(REFIID, void **);
  224. static const D3D_FEATURE_LEVEL feature_levels[] = {
  225. D3D_FEATURE_LEVEL_11_0,
  226. D3D_FEATURE_LEVEL_10_1,
  227. D3D_FEATURE_LEVEL_10_0,
  228. };
  229. static inline bool gl_shtex_init_d3d11(void)
  230. {
  231. D3D_FEATURE_LEVEL level_used;
  232. IDXGIFactory1 *factory;
  233. IDXGIAdapter *adapter;
  234. HRESULT hr;
  235. HMODULE d3d11 = load_system_library("d3d11.dll");
  236. if (!d3d11) {
  237. hlog("gl_shtex_init_d3d11: failed to load D3D11.dll: %d",
  238. GetLastError());
  239. return false;
  240. }
  241. HMODULE dxgi = load_system_library("dxgi.dll");
  242. if (!dxgi) {
  243. hlog("gl_shtex_init_d3d11: failed to load DXGI.dll: %d",
  244. GetLastError());
  245. return false;
  246. }
  247. DXGI_SWAP_CHAIN_DESC desc = {0};
  248. desc.BufferCount = 2;
  249. desc.BufferDesc.Format = data.format;
  250. desc.BufferDesc.Width = 2;
  251. desc.BufferDesc.Height = 2;
  252. desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  253. desc.SampleDesc.Count = 1;
  254. desc.Windowed = true;
  255. desc.OutputWindow = data.hwnd;
  256. create_dxgi_factory1_t create_factory =
  257. (void *)GetProcAddress(dxgi, "CreateDXGIFactory1");
  258. if (!create_factory) {
  259. hlog("gl_shtex_init_d3d11: failed to load CreateDXGIFactory1 "
  260. "procedure: %d",
  261. GetLastError());
  262. return false;
  263. }
  264. PFN_D3D11_CREATE_DEVICE_AND_SWAP_CHAIN create =
  265. (void *)GetProcAddress(d3d11, "D3D11CreateDeviceAndSwapChain");
  266. if (!create) {
  267. hlog("gl_shtex_init_d3d11: failed to load "
  268. "D3D11CreateDeviceAndSwapChain procedure: %d",
  269. GetLastError());
  270. return false;
  271. }
  272. hr = create_factory(&GUID_IDXGIFactory1, (void **)&factory);
  273. if (FAILED(hr)) {
  274. hlog_hr("gl_shtex_init_d3d11: failed to create factory", hr);
  275. return false;
  276. }
  277. hr = IDXGIFactory1_EnumAdapters1(factory, 0,
  278. (IDXGIAdapter1 **)&adapter);
  279. IDXGIFactory1_Release(factory);
  280. if (FAILED(hr)) {
  281. hlog_hr("gl_shtex_init_d3d11: failed to create adapter", hr);
  282. return false;
  283. }
  284. hr = create(adapter, D3D_DRIVER_TYPE_UNKNOWN, NULL, 0, feature_levels,
  285. sizeof(feature_levels) / sizeof(D3D_FEATURE_LEVEL),
  286. D3D11_SDK_VERSION, &desc, &data.dxgi_swap,
  287. &data.d3d11_device, &level_used, &data.d3d11_context);
  288. IDXGIAdapter_Release(adapter);
  289. if (FAILED(hr)) {
  290. hlog_hr("gl_shtex_init_d3d11: failed to create device", hr);
  291. return false;
  292. }
  293. return true;
  294. }
  295. static inline bool gl_shtex_init_d3d11_tex(void)
  296. {
  297. IDXGIResource *dxgi_res;
  298. HRESULT hr;
  299. D3D11_TEXTURE2D_DESC desc = {0};
  300. desc.Width = data.cx;
  301. desc.Height = data.cy;
  302. desc.MipLevels = 1;
  303. desc.ArraySize = 1;
  304. desc.Format = data.format;
  305. desc.SampleDesc.Count = 1;
  306. desc.Usage = D3D11_USAGE_DEFAULT;
  307. desc.MiscFlags = D3D11_RESOURCE_MISC_SHARED;
  308. desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  309. hr = ID3D11Device_CreateTexture2D(data.d3d11_device, &desc, NULL,
  310. &data.d3d11_tex);
  311. if (FAILED(hr)) {
  312. hlog_hr("gl_shtex_init_d3d11_tex: failed to create texture",
  313. hr);
  314. return false;
  315. }
  316. hr = ID3D11Device_QueryInterface(data.d3d11_tex, &GUID_IDXGIResource,
  317. (void **)&dxgi_res);
  318. if (FAILED(hr)) {
  319. hlog_hr("gl_shtex_init_d3d11_tex: failed to get IDXGIResource",
  320. hr);
  321. return false;
  322. }
  323. hr = IDXGIResource_GetSharedHandle(dxgi_res, &data.handle);
  324. IDXGIResource_Release(dxgi_res);
  325. if (FAILED(hr)) {
  326. hlog_hr("gl_shtex_init_d3d11_tex: failed to get shared handle",
  327. hr);
  328. return false;
  329. }
  330. return true;
  331. }
  332. static inline bool gl_shtex_init_gl_tex(void)
  333. {
  334. data.gl_device = jimglDXOpenDeviceNV(data.d3d11_device);
  335. if (!data.gl_device) {
  336. hlog("gl_shtex_init_gl_tex: failed to open device");
  337. return false;
  338. }
  339. glGenTextures(1, &data.texture);
  340. if (gl_error("gl_shtex_init_gl_tex", "failed to generate texture")) {
  341. return false;
  342. }
  343. data.gl_dxobj = jimglDXRegisterObjectNV(data.gl_device, data.d3d11_tex,
  344. data.texture, GL_TEXTURE_2D,
  345. WGL_ACCESS_WRITE_DISCARD_NV);
  346. if (!data.gl_dxobj) {
  347. hlog("gl_shtex_init_gl_tex: failed to register object");
  348. return false;
  349. }
  350. return true;
  351. }
  352. static inline bool gl_init_fbo(void)
  353. {
  354. glGenFramebuffers(1, &data.fbo);
  355. return !gl_error("gl_init_fbo", "failed to initialize FBO");
  356. }
  357. static bool gl_shtex_init(HWND window)
  358. {
  359. if (!gl_shtex_init_window()) {
  360. return false;
  361. }
  362. if (!gl_shtex_init_d3d11()) {
  363. return false;
  364. }
  365. if (!gl_shtex_init_d3d11_tex()) {
  366. return false;
  367. }
  368. if (!gl_shtex_init_gl_tex()) {
  369. return false;
  370. }
  371. if (!gl_init_fbo()) {
  372. return false;
  373. }
  374. if (!capture_init_shtex(&data.shtex_info, window, data.cx, data.cy,
  375. data.format, true, (uintptr_t)data.handle)) {
  376. return false;
  377. }
  378. hlog("gl shared texture capture successful");
  379. return true;
  380. }
  381. static inline bool gl_shmem_init_data(size_t idx, size_t size)
  382. {
  383. glBindBuffer(GL_PIXEL_PACK_BUFFER, data.pbos[idx]);
  384. if (gl_error("gl_shmem_init_data", "failed to bind pbo")) {
  385. return false;
  386. }
  387. glBufferData(GL_PIXEL_PACK_BUFFER, size, 0, GL_STREAM_READ);
  388. if (gl_error("gl_shmem_init_data", "failed to set pbo data")) {
  389. return false;
  390. }
  391. glBindTexture(GL_TEXTURE_2D, data.textures[idx]);
  392. if (gl_error("gl_shmem_init_data", "failed to set bind texture")) {
  393. return false;
  394. }
  395. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, data.cx, data.cy, 0, GL_BGRA,
  396. GL_UNSIGNED_BYTE, NULL);
  397. if (gl_error("gl_shmem_init_data", "failed to set texture data")) {
  398. return false;
  399. }
  400. return true;
  401. }
  402. static inline bool gl_shmem_init_buffers(void)
  403. {
  404. uint32_t size = data.cx * data.cy * 4;
  405. GLint last_pbo;
  406. GLint last_tex;
  407. glGenBuffers(NUM_BUFFERS, data.pbos);
  408. if (gl_error("gl_shmem_init_buffers", "failed to generate buffers")) {
  409. return false;
  410. }
  411. glGenTextures(NUM_BUFFERS, data.textures);
  412. if (gl_error("gl_shmem_init_buffers", "failed to generate textures")) {
  413. return false;
  414. }
  415. glGetIntegerv(GL_PIXEL_PACK_BUFFER_BINDING, &last_pbo);
  416. if (gl_error("gl_shmem_init_buffers",
  417. "failed to save pixel pack buffer")) {
  418. return false;
  419. }
  420. glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_tex);
  421. if (gl_error("gl_shmem_init_buffers", "failed to save texture")) {
  422. return false;
  423. }
  424. for (size_t i = 0; i < NUM_BUFFERS; i++) {
  425. if (!gl_shmem_init_data(i, size)) {
  426. return false;
  427. }
  428. }
  429. glBindBuffer(GL_PIXEL_PACK_BUFFER, last_pbo);
  430. glBindTexture(GL_TEXTURE_2D, last_tex);
  431. return true;
  432. }
  433. static bool gl_shmem_init(HWND window)
  434. {
  435. if (!gl_shmem_init_buffers()) {
  436. return false;
  437. }
  438. if (!gl_init_fbo()) {
  439. return false;
  440. }
  441. if (!capture_init_shmem(&data.shmem_info, window, data.cx, data.cy,
  442. data.cx * 4, data.format, true)) {
  443. return false;
  444. }
  445. hlog("gl memory capture successful");
  446. return true;
  447. }
  448. #define INIT_SUCCESS 0
  449. #define INIT_FAILED -1
  450. #define INIT_SHTEX_FAILED -2
  451. static int gl_init(HDC hdc)
  452. {
  453. HWND window = WindowFromDC(hdc);
  454. int ret = INIT_FAILED;
  455. bool success = false;
  456. RECT rc = {0};
  457. if (darkest_dungeon_fix) {
  458. data.cx = 1920;
  459. data.cy = 1080;
  460. } else {
  461. GetClientRect(window, &rc);
  462. data.cx = rc.right;
  463. data.cy = rc.bottom;
  464. }
  465. data.hdc = hdc;
  466. data.format = DXGI_FORMAT_B8G8R8A8_UNORM;
  467. data.using_shtex = nv_capture_available &&
  468. !global_hook_info->force_shmem &&
  469. !data.shmem_fallback;
  470. if (data.using_shtex) {
  471. success = gl_shtex_init(window);
  472. if (!success)
  473. ret = INIT_SHTEX_FAILED;
  474. } else {
  475. success = gl_shmem_init(window);
  476. }
  477. if (!success)
  478. gl_free();
  479. else
  480. ret = INIT_SUCCESS;
  481. return ret;
  482. }
  483. static void gl_copy_backbuffer(GLuint dst)
  484. {
  485. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, data.fbo);
  486. if (gl_error("gl_copy_backbuffer", "failed to bind FBO")) {
  487. return;
  488. }
  489. glBindTexture(GL_TEXTURE_2D, dst);
  490. if (gl_error("gl_copy_backbuffer", "failed to bind texture")) {
  491. return;
  492. }
  493. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
  494. GL_TEXTURE_2D, dst, 0);
  495. if (gl_error("gl_copy_backbuffer", "failed to set frame buffer")) {
  496. return;
  497. }
  498. glReadBuffer(GL_BACK);
  499. /* darkest dungeon fix */
  500. darkest_dungeon_fix = glGetError() == GL_INVALID_OPERATION &&
  501. _strcmpi(process_name, "Darkest.exe") == 0;
  502. glDrawBuffer(GL_COLOR_ATTACHMENT0);
  503. if (gl_error("gl_copy_backbuffer", "failed to set draw buffer")) {
  504. return;
  505. }
  506. glBlitFramebuffer(0, 0, data.cx, data.cy, 0, 0, data.cx, data.cy,
  507. GL_COLOR_BUFFER_BIT, GL_LINEAR);
  508. gl_error("gl_copy_backbuffer", "failed to blit");
  509. }
  510. static void gl_shtex_capture(void)
  511. {
  512. GLint last_fbo;
  513. GLint last_tex;
  514. jimglDXLockObjectsNV(data.gl_device, 1, &data.gl_dxobj);
  515. glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &last_fbo);
  516. if (gl_error("gl_shtex_capture", "failed to get last fbo")) {
  517. return;
  518. }
  519. glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_tex);
  520. if (gl_error("gl_shtex_capture", "failed to get last texture")) {
  521. return;
  522. }
  523. gl_copy_backbuffer(data.texture);
  524. glBindTexture(GL_TEXTURE_2D, last_tex);
  525. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, last_fbo);
  526. jimglDXUnlockObjectsNV(data.gl_device, 1, &data.gl_dxobj);
  527. IDXGISwapChain_Present(data.dxgi_swap, 0, 0);
  528. }
  529. static void gl_shmem_capture_copy(int i)
  530. {
  531. if (data.texture_ready[i]) {
  532. GLvoid *buffer;
  533. data.texture_ready[i] = false;
  534. glBindBuffer(GL_PIXEL_PACK_BUFFER, data.pbos[i]);
  535. if (gl_error("gl_shmem_capture_queue_copy",
  536. "failed to bind pbo")) {
  537. return;
  538. }
  539. buffer = glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
  540. if (buffer) {
  541. data.texture_mapped[i] = true;
  542. shmem_copy_data(i, buffer);
  543. }
  544. }
  545. }
  546. static inline void gl_shmem_capture_stage(GLuint dst_pbo, GLuint src_tex)
  547. {
  548. glBindTexture(GL_TEXTURE_2D, src_tex);
  549. if (gl_error("gl_shmem_capture_stage", "failed to bind src_tex")) {
  550. return;
  551. }
  552. glBindBuffer(GL_PIXEL_PACK_BUFFER, dst_pbo);
  553. if (gl_error("gl_shmem_capture_stage", "failed to bind dst_pbo")) {
  554. return;
  555. }
  556. glGetTexImage(GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_BYTE, 0);
  557. if (gl_error("gl_shmem_capture_stage", "failed to read src_tex")) {
  558. return;
  559. }
  560. }
  561. static void gl_shmem_capture(void)
  562. {
  563. int next_tex;
  564. GLint last_fbo;
  565. GLint last_tex;
  566. GLint last_pbo;
  567. glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &last_fbo);
  568. if (gl_error("gl_shmem_capture", "failed to get last fbo")) {
  569. return;
  570. }
  571. glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_tex);
  572. if (gl_error("gl_shmem_capture", "failed to get last texture")) {
  573. return;
  574. }
  575. glGetIntegerv(GL_PIXEL_PACK_BUFFER_BINDING, &last_pbo);
  576. if (gl_error("gl_shmem_capture", "failed to get last pbo")) {
  577. return;
  578. }
  579. next_tex = (data.cur_tex + 1) % NUM_BUFFERS;
  580. gl_shmem_capture_copy(next_tex);
  581. if (data.copy_wait < NUM_BUFFERS - 1) {
  582. data.copy_wait++;
  583. } else {
  584. if (shmem_texture_data_lock(data.cur_tex)) {
  585. glBindBuffer(GL_PIXEL_PACK_BUFFER,
  586. data.pbos[data.cur_tex]);
  587. glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
  588. data.texture_mapped[data.cur_tex] = false;
  589. shmem_texture_data_unlock(data.cur_tex);
  590. }
  591. gl_copy_backbuffer(data.textures[data.cur_tex]);
  592. gl_shmem_capture_stage(data.pbos[data.cur_tex],
  593. data.textures[data.cur_tex]);
  594. data.texture_ready[data.cur_tex] = true;
  595. }
  596. glBindTexture(GL_TEXTURE_2D, last_tex);
  597. glBindBuffer(GL_PIXEL_PACK_BUFFER, last_pbo);
  598. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, last_fbo);
  599. data.cur_tex = next_tex;
  600. }
  601. static void gl_capture(HDC hdc)
  602. {
  603. static bool critical_failure = false;
  604. if (critical_failure) {
  605. return;
  606. }
  607. if (!functions_initialized) {
  608. functions_initialized = init_gl_functions();
  609. if (!functions_initialized) {
  610. critical_failure = true;
  611. return;
  612. }
  613. }
  614. /* reset error flag */
  615. glGetError();
  616. if (capture_should_stop()) {
  617. gl_free();
  618. }
  619. if (capture_should_init()) {
  620. if (gl_init(hdc) == INIT_SHTEX_FAILED) {
  621. data.shmem_fallback = true;
  622. gl_init(hdc);
  623. }
  624. }
  625. if (capture_ready() && hdc == data.hdc) {
  626. uint32_t new_cx;
  627. uint32_t new_cy;
  628. /* reset capture if resized */
  629. get_window_size(hdc, &new_cx, &new_cy);
  630. if (new_cx != data.cx || new_cy != data.cy) {
  631. if (new_cx != 0 && new_cy != 0)
  632. gl_free();
  633. return;
  634. }
  635. if (data.using_shtex)
  636. gl_shtex_capture();
  637. else
  638. gl_shmem_capture();
  639. }
  640. }
  641. static inline void gl_swap_begin(HDC hdc)
  642. {
  643. const bool first = swap_recurse == 0;
  644. ++swap_recurse;
  645. if (first && !global_hook_info->capture_overlay)
  646. gl_capture(hdc);
  647. }
  648. static inline void gl_swap_end(HDC hdc)
  649. {
  650. --swap_recurse;
  651. const bool first = swap_recurse == 0;
  652. if (first && global_hook_info->capture_overlay)
  653. gl_capture(hdc);
  654. }
  655. static BOOL WINAPI hook_swap_buffers(HDC hdc)
  656. {
  657. gl_swap_begin(hdc);
  658. const BOOL ret = RealSwapBuffers(hdc);
  659. gl_swap_end(hdc);
  660. return ret;
  661. }
  662. static BOOL WINAPI hook_wgl_swap_buffers(HDC hdc)
  663. {
  664. gl_swap_begin(hdc);
  665. const BOOL ret = RealWglSwapBuffers(hdc);
  666. gl_swap_end(hdc);
  667. return ret;
  668. }
  669. static BOOL WINAPI hook_wgl_swap_layer_buffers(HDC hdc, UINT planes)
  670. {
  671. gl_swap_begin(hdc);
  672. const BOOL ret = RealWglSwapLayerBuffers(hdc, planes);
  673. gl_swap_end(hdc);
  674. return ret;
  675. }
  676. static BOOL WINAPI hook_wgl_delete_context(HGLRC hrc)
  677. {
  678. if (capture_active() && functions_initialized) {
  679. HDC last_hdc = jimglGetCurrentDC();
  680. HGLRC last_hrc = jimglGetCurrentContext();
  681. jimglMakeCurrent(data.hdc, hrc);
  682. gl_free();
  683. jimglMakeCurrent(last_hdc, last_hrc);
  684. }
  685. return RealWglDeleteContext(hrc);
  686. }
  687. static bool gl_register_window(void)
  688. {
  689. WNDCLASSW wc = {0};
  690. wc.style = CS_OWNDC;
  691. wc.hInstance = GetModuleHandle(NULL);
  692. wc.lpfnWndProc = DefWindowProc;
  693. wc.lpszClassName = DUMMY_WINDOW_CLASS_NAME;
  694. if (!RegisterClassW(&wc)) {
  695. hlog("gl_register_window: failed to register window class: %d",
  696. GetLastError());
  697. return false;
  698. }
  699. return true;
  700. }
  701. bool hook_gl(void)
  702. {
  703. void *wgl_dc_proc;
  704. void *wgl_slb_proc;
  705. void *wgl_sb_proc;
  706. gl = get_system_module("opengl32.dll");
  707. if (!gl) {
  708. return false;
  709. }
  710. /* "life is feudal: your own" somehow uses both opengl and directx at
  711. * the same time, so blacklist it from capturing opengl */
  712. const char *process_name = get_process_name();
  713. if (_strcmpi(process_name, "yo_cm_client.exe") == 0 ||
  714. _strcmpi(process_name, "cm_client.exe") == 0) {
  715. hlog("Ignoring opengl for game: %s", process_name);
  716. return true;
  717. }
  718. if (!gl_register_window()) {
  719. return true;
  720. }
  721. wgl_dc_proc = base_get_proc("wglDeleteContext");
  722. wgl_slb_proc = base_get_proc("wglSwapLayerBuffers");
  723. wgl_sb_proc = base_get_proc("wglSwapBuffers");
  724. DetourTransactionBegin();
  725. RealSwapBuffers = SwapBuffers;
  726. DetourAttach((PVOID *)&RealSwapBuffers, hook_swap_buffers);
  727. if (wgl_dc_proc) {
  728. RealWglDeleteContext = (PFN_WglDeleteContext)wgl_dc_proc;
  729. DetourAttach((PVOID *)&RealWglDeleteContext,
  730. hook_wgl_delete_context);
  731. }
  732. if (wgl_slb_proc) {
  733. RealWglSwapLayerBuffers = (PFN_WglSwapLayerBuffers)wgl_slb_proc;
  734. DetourAttach((PVOID *)&RealWglSwapLayerBuffers,
  735. hook_wgl_swap_layer_buffers);
  736. }
  737. if (wgl_sb_proc) {
  738. RealWglSwapBuffers = (PFN_WglSwapBuffers)wgl_sb_proc;
  739. DetourAttach((PVOID *)&RealWglSwapBuffers,
  740. hook_wgl_swap_buffers);
  741. }
  742. const LONG error = DetourTransactionCommit();
  743. const bool success = error == NO_ERROR;
  744. if (success) {
  745. hlog("Hooked SwapBuffers");
  746. if (RealWglDeleteContext)
  747. hlog("Hooked wglDeleteContext");
  748. if (RealWglSwapLayerBuffers)
  749. hlog("Hooked wglSwapLayerBuffers");
  750. if (RealWglSwapBuffers)
  751. hlog("Hooked wglSwapBuffers");
  752. hlog("Hooked GL");
  753. } else {
  754. RealSwapBuffers = NULL;
  755. RealWglDeleteContext = NULL;
  756. RealWglSwapLayerBuffers = NULL;
  757. RealWglSwapBuffers = NULL;
  758. hlog("Failed to attach Detours hook: %ld", error);
  759. }
  760. return success;
  761. }