mirror of
https://github.com/orange-cpp/omath.git
synced 2026-05-06 23:43:27 +00:00
added opengl for linux
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include <optional>
|
||||
#include <shared_mutex>
|
||||
|
||||
#ifdef _WIN32
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
@@ -17,6 +18,12 @@
|
||||
#include <d3d12.h>
|
||||
#include <d3d9.h>
|
||||
#include <dxgi.h>
|
||||
#endif // _WIN32
|
||||
|
||||
#ifdef __linux__
|
||||
#include <GL/glx.h>
|
||||
#endif // __linux__
|
||||
|
||||
#include <safetyhook.hpp>
|
||||
|
||||
namespace omath::hooks
|
||||
@@ -26,6 +33,7 @@ namespace omath::hooks
|
||||
HooksManager() = default;
|
||||
|
||||
public:
|
||||
#ifdef _WIN32
|
||||
// IDXGISwapChain callbacks — shared between DX11 and DX12 (same interface, same signature).
|
||||
using present_callback = std::function<void(IDXGISwapChain*, UINT, UINT)>;
|
||||
using resize_buffers_callback = std::function<void(IDXGISwapChain*, UINT, UINT, UINT, DXGI_FORMAT, UINT)>;
|
||||
@@ -38,11 +46,17 @@ namespace omath::hooks
|
||||
using dx9_reset_callback = std::function<void(IDirect3DDevice9*, D3DPRESENT_PARAMETERS*)>;
|
||||
using dx9_end_scene_callback = std::function<void(IDirect3DDevice9*)>;
|
||||
|
||||
// OpenGL callbacks. Fires before the hooked buffer swap function calls the original.
|
||||
// OpenGL callback — Windows. Fires before the hooked buffer swap function calls the original.
|
||||
using opengl_swap_buffers_callback = std::function<void(HDC)>;
|
||||
|
||||
// Return nullopt to pass the message to the original WndProc; return a value to intercept it.
|
||||
using wnd_proc_callback = std::function<std::optional<LRESULT>(HWND, UINT, WPARAM, LPARAM)>;
|
||||
#endif // _WIN32
|
||||
|
||||
#ifdef __linux__
|
||||
// OpenGL/GLX callback — Linux. Fires before glXSwapBuffers calls the original.
|
||||
using opengl_swap_buffers_callback = std::function<void(Display*, GLXDrawable)>;
|
||||
#endif // __linux__
|
||||
|
||||
template<typename Callback>
|
||||
using callback_ptr = std::shared_ptr<const Callback>;
|
||||
@@ -52,6 +66,7 @@ namespace omath::hooks
|
||||
HooksManager& operator=(const HooksManager&) = delete;
|
||||
~HooksManager();
|
||||
|
||||
#ifdef _WIN32
|
||||
[[nodiscard]] bool hook_dx9();
|
||||
void unhook_dx9();
|
||||
void set_on_dx9_present(dx9_present_callback callback);
|
||||
@@ -63,11 +78,13 @@ namespace omath::hooks
|
||||
|
||||
[[nodiscard]] bool hook_dx12();
|
||||
void unhook_dx12();
|
||||
#endif // _WIN32
|
||||
|
||||
[[nodiscard]] bool hook_opengl();
|
||||
void unhook_opengl();
|
||||
void set_on_opengl_swap_buffers(opengl_swap_buffers_callback callback);
|
||||
|
||||
#ifdef _WIN32
|
||||
// Present and ResizeBuffers callbacks fire for whichever of DX11/DX12 is hooked.
|
||||
void set_on_present(present_callback callback);
|
||||
void set_on_resize_buffers(resize_buffers_callback callback);
|
||||
@@ -76,8 +93,10 @@ namespace omath::hooks
|
||||
[[nodiscard]] bool hook_wnd_proc(HWND hwnd);
|
||||
void unhook_wnd_proc();
|
||||
void set_on_wnd_proc(wnd_proc_callback callback);
|
||||
#endif // _WIN32
|
||||
|
||||
private:
|
||||
#ifdef _WIN32
|
||||
[[nodiscard]]
|
||||
static HRESULT __stdcall dx9_present_detour(IDirect3DDevice9* p_device, const RECT* p_source_rect,
|
||||
const RECT* p_dest_rect, HWND h_dest_window_override,
|
||||
@@ -109,9 +128,15 @@ namespace omath::hooks
|
||||
|
||||
[[nodiscard]]
|
||||
static LRESULT __stdcall wnd_proc_detour(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param);
|
||||
#endif // _WIN32
|
||||
|
||||
#ifdef __linux__
|
||||
static void opengl_glx_swap_buffers_detour(Display* display, GLXDrawable drawable);
|
||||
#endif // __linux__
|
||||
|
||||
mutable std::shared_mutex m_hook_state_mutex;
|
||||
|
||||
#ifdef _WIN32
|
||||
mutable std::shared_mutex m_dx9_present_mutex;
|
||||
mutable std::shared_mutex m_dx9_reset_mutex;
|
||||
mutable std::shared_mutex m_dx9_end_scene_mutex;
|
||||
@@ -120,13 +145,15 @@ namespace omath::hooks
|
||||
mutable std::shared_mutex m_resize_buffers_mutex;
|
||||
mutable std::shared_mutex m_execute_command_lists_mutex;
|
||||
|
||||
mutable std::shared_mutex m_opengl_swap_buffers_mutex;
|
||||
mutable std::shared_mutex m_wnd_proc_mutex;
|
||||
#endif // _WIN32
|
||||
|
||||
mutable std::shared_mutex m_opengl_swap_buffers_mutex;
|
||||
|
||||
#ifdef _WIN32
|
||||
bool m_is_dx9_hooked = false;
|
||||
bool m_is_dx11_hooked = false;
|
||||
bool m_is_dx12_hooked = false;
|
||||
bool m_is_opengl_hooked = false;
|
||||
bool m_is_wnd_proc_hooked = false;
|
||||
|
||||
HWND m_hooked_hwnd = nullptr;
|
||||
@@ -145,7 +172,15 @@ namespace omath::hooks
|
||||
|
||||
safetyhook::InlineHook m_opengl_wgl_swap_buffers_hook;
|
||||
safetyhook::InlineHook m_opengl_swap_buffers_hook;
|
||||
#endif // _WIN32
|
||||
|
||||
#ifdef __linux__
|
||||
safetyhook::InlineHook m_opengl_glx_swap_buffers_hook;
|
||||
#endif // __linux__
|
||||
|
||||
bool m_is_opengl_hooked = false;
|
||||
|
||||
#ifdef _WIN32
|
||||
callback_ptr<dx9_present_callback> m_dx9_present_cb;
|
||||
callback_ptr<dx9_reset_callback> m_dx9_reset_cb;
|
||||
callback_ptr<dx9_end_scene_callback> m_dx9_end_scene_cb;
|
||||
@@ -153,8 +188,11 @@ namespace omath::hooks
|
||||
callback_ptr<present_callback> m_present_cb;
|
||||
callback_ptr<resize_buffers_callback> m_resize_buffers_cb;
|
||||
callback_ptr<execute_command_lists_callback> m_execute_command_lists_cb;
|
||||
callback_ptr<opengl_swap_buffers_callback> m_opengl_swap_buffers_cb;
|
||||
|
||||
callback_ptr<wnd_proc_callback> m_wnd_proc_cb;
|
||||
#endif // _WIN32
|
||||
|
||||
callback_ptr<opengl_swap_buffers_callback> m_opengl_swap_buffers_cb;
|
||||
};
|
||||
} // namespace omath::hooks
|
||||
|
||||
|
||||
Reference in New Issue
Block a user