Initial commit of existing code.

* All renderdoc code up to this point was written by me, history is available by request
This commit is contained in:
baldurk committed 2014-05-02 08:33:01 +01:00
1 parent 04b1549c0f
commit c38affcded
695 files changed
+230316

No files matched your search

+353
View File
@@ -0,0 +1,353 @@
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2014 Crytek
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
#include "driver/d3d11/d3d11_device.h"
#include "driver/dxgi/dxgi_wrapped.h"
#include "hooks.h"
#define DLL_NAME "d3d11.dll"
class D3D11Hook : LibraryHook
{
public:
D3D11Hook() { LibraryHooks::GetInstance().RegisterHook(DLL_NAME, this); m_EnabledHooks = true; m_InsideCreate = false; }
bool CreateHooks(const char *libName)
{
bool success = true;
// require dxgi.dll hooked as well for proper operation
if(GetModuleHandleA("dxgi.dll") == NULL)
{
RDCWARN("Failed to load dxgi.dll - not inserting D3D11 hooks.");
return false;
}
// also require d3dcompiler_??.dll
if(GetD3DCompiler() == NULL)
{
RDCERR("Failed to load d3dcompiler_??.dll - not inserting D3D11 hooks.");
return false;
}
success &= CreateDevice.Initialize("D3D11CreateDevice", DLL_NAME, D3D11CreateDevice_hook);
success &= CreateDeviceAndSwapChain.Initialize("D3D11CreateDeviceAndSwapChain", DLL_NAME, D3D11CreateDeviceAndSwapChain_hook);
if(!success) return false;
// FRAPS compatibility. Save out the first 16 bytes (arbitrary number) of the 'real' function code.
// this should be
// jmp D3D11CreateDeviceAndSwapChain_hook
// push r12 <- this is where our trampoline jumps back to
// push r13
//
// FRAPS stomps over this with its own hook that we detect and handle later.
void *hooked_func_ptr = GetProcAddress(GetModuleHandleA("d3d11.dll"), "D3D11CreateDeviceAndSwapChain");
if(hooked_func_ptr == NULL) return false;
memcpy(CreateDeviceAndSwapChain_ident, hooked_func_ptr, 16);
m_HasHooks = true;
m_EnabledHooks = true;
return true;
}
void EnableHooks(const char *libName, bool enable)
{
m_EnabledHooks = enable;
}
bool UseHooks()
{
return (d3d11hooks.m_HasHooks && d3d11hooks.m_EnabledHooks);
}
static HRESULT CreateWrappedDeviceAndSwapChain(
__in_opt IDXGIAdapter* pAdapter,
D3D_DRIVER_TYPE DriverType,
HMODULE Software,
UINT Flags,
__in_ecount_opt( FeatureLevels ) CONST D3D_FEATURE_LEVEL* pFeatureLevels,
UINT FeatureLevels,
UINT SDKVersion,
__in_opt CONST DXGI_SWAP_CHAIN_DESC* pSwapChainDesc,
__out_opt IDXGISwapChain** ppSwapChain,
__out_opt ID3D11Device** ppDevice,
__out_opt D3D_FEATURE_LEVEL* pFeatureLevel,
__out_opt ID3D11DeviceContext** ppImmediateContext )
{
return d3d11hooks.Create_Internal(pAdapter, DriverType, Software, Flags, pFeatureLevels, FeatureLevels,
SDKVersion, pSwapChainDesc, ppSwapChain, ppDevice, pFeatureLevel, ppImmediateContext);
}
private:
static D3D11Hook d3d11hooks;
bool m_HasHooks;
bool m_EnabledHooks;
byte CreateDeviceAndSwapChain_ident[16];
Hook<PFN_D3D11_CREATE_DEVICE_AND_SWAP_CHAIN> CreateDeviceAndSwapChain;
Hook<PFN_D3D11_CREATE_DEVICE> CreateDevice;
// re-entrancy detection (can happen in rare cases with e.g. fraps)
bool m_InsideCreate;
HRESULT Create_Internal(
__in_opt IDXGIAdapter* pAdapter,
D3D_DRIVER_TYPE DriverType,
HMODULE Software,
UINT Flags,
__in_ecount_opt( FeatureLevels ) CONST D3D_FEATURE_LEVEL* pFeatureLevels,
UINT FeatureLevels,
UINT SDKVersion,
__in_opt CONST DXGI_SWAP_CHAIN_DESC* pSwapChainDesc,
__out_opt IDXGISwapChain** ppSwapChain,
__out_opt ID3D11Device** ppDevice,
__out_opt D3D_FEATURE_LEVEL* pFeatureLevel,
__out_opt ID3D11DeviceContext** ppImmediateContext )
{
// if we're already inside a wrapped create i.e. this function, then DON'T do anything
// special. Just grab the trampolined function and call it.
if(m_InsideCreate)
{
PFN_D3D11_CREATE_DEVICE_AND_SWAP_CHAIN createFunc = NULL;
// shouldn't ever get in here if we're in the case without hooks but let's be safe.
if(m_HasHooks)
createFunc = CreateDeviceAndSwapChain();
else
createFunc = (PFN_D3D11_CREATE_DEVICE_AND_SWAP_CHAIN)GetProcAddress(GetModuleHandleA("d3d11.dll"), "D3D11CreateDeviceAndSwapChain");
return createFunc(pAdapter, DriverType, Software, Flags, pFeatureLevels, FeatureLevels,
SDKVersion, pSwapChainDesc, ppSwapChain, ppDevice, pFeatureLevel, ppImmediateContext);
}
m_InsideCreate = true;
RDCDEBUG("Call to Create_Internal Flags %x", Flags);
bool reading = RenderDoc::Inst().IsReplayApp();
if(reading)
{
RDCDEBUG("In replay app");
}
if(m_EnabledHooks)
{
if(!reading && RenderDoc::Inst().GetCaptureOptions().DebugDeviceMode)
{
Flags |= D3D11_CREATE_DEVICE_DEBUG;
}
else
{
Flags &= ~D3D11_CREATE_DEVICE_DEBUG;
}
}
DXGI_SWAP_CHAIN_DESC swapDesc;
DXGI_SWAP_CHAIN_DESC *pUsedSwapDesc = NULL;
if(pSwapChainDesc)
{
swapDesc = *pSwapChainDesc;
pUsedSwapDesc = &swapDesc;
}
if(pUsedSwapDesc && m_EnabledHooks && !RenderDoc::Inst().GetCaptureOptions().AllowFullscreen)
{
pUsedSwapDesc->Windowed = TRUE;
}
RDCDEBUG("Calling real createdevice...");
PFN_D3D11_CREATE_DEVICE_AND_SWAP_CHAIN createFunc = (PFN_D3D11_CREATE_DEVICE_AND_SWAP_CHAIN)GetProcAddress(GetModuleHandleA("d3d11.dll"), "D3D11CreateDeviceAndSwapChain");
if(createFunc)
{
byte ident[16];
memcpy(ident, createFunc, 16);
// FRAPS compatibility. If FRAPS has come in and modified the real code to something like
// mov rax, [ptrintofraps]
// jmp rax
// then we just jump straight there. If we jump to the trampoline, we'll come in half way through
// those instructions and things will go boom very quickly.
//
// Note that when we call this function FRAPS then restores the asm from before it got here, which is our
// trampolined code, so FRAPS is going to call back into this function. We detect this re-entrancy
// at the top and just go straight into the real d3d function now that FRAPS has restored our trampoline,
// and return right back here
// What a headache!
//
// in the non-fraps case no-one else has messed with this code so the idents will be the same and we
// will use our trampolines.
if(!memcmp(ident, CreateDeviceAndSwapChain_ident, 16) && m_HasHooks)
createFunc = CreateDeviceAndSwapChain();
}
// shouldn't ever get here, we should either have it from procaddress or the trampoline, but let's be
// safe.
if(createFunc == NULL)
{
RDCERR("Something went seriously wrong with the hooks!");
m_InsideCreate = false;
return E_UNEXPECTED;
}
HRESULT ret = createFunc(pAdapter, DriverType, Software, Flags, pFeatureLevels, FeatureLevels,
SDKVersion, pUsedSwapDesc, ppSwapChain, ppDevice, pFeatureLevel, ppImmediateContext);
RDCDEBUG("Called real createdevice...");
bool suppress = (Flags & D3D11_CREATE_DEVICE_PREVENT_ALTERING_LAYER_SETTINGS_FROM_REGISTRY) != 0;
if(suppress && !reading)
{
RDCDEBUG("Application requested not to be hooked.");
}
else if(SUCCEEDED(ret) && m_EnabledHooks && ppDevice)
{
RDCDEBUG("succeeded and hooking.");
if(!WrappedID3D11Device::IsAlloc(*ppDevice))
{
D3D11InitParams *params = new D3D11InitParams;
params->DriverType = DriverType;
params->Flags = Flags;
params->SDKVersion = SDKVersion;
params->NumFeatureLevels = FeatureLevels;
if(FeatureLevels > 0)
memcpy(params->FeatureLevels, pFeatureLevels, sizeof(D3D_FEATURE_LEVEL)*FeatureLevels);
WrappedID3D11Device *wrap = new WrappedID3D11Device(*ppDevice, params);
RDCDEBUG("created wrapped device.");
*ppDevice = wrap;
wrap->GetImmediateContext(ppImmediateContext);
if(ppSwapChain && *ppSwapChain)
*ppSwapChain = new WrappedIDXGISwapChain(*ppSwapChain, wrap);
}
}
else if(SUCCEEDED(ret))
{
RDCDEBUG("succeeded.");
}
else
{
RDCDEBUG("failed. 0x%08x", ret);
}
m_InsideCreate = false;
return ret;
}
static HRESULT WINAPI D3D11CreateDevice_hook(
__in_opt IDXGIAdapter* pAdapter,
D3D_DRIVER_TYPE DriverType,
HMODULE Software,
UINT Flags,
__in_ecount_opt( FeatureLevels ) CONST D3D_FEATURE_LEVEL* pFeatureLevels,
UINT FeatureLevels,
UINT SDKVersion,
__out_opt ID3D11Device** ppDevice,
__out_opt D3D_FEATURE_LEVEL* pFeatureLevel,
__out_opt ID3D11DeviceContext** ppImmediateContext )
{
HRESULT ret = d3d11hooks.Create_Internal(
pAdapter, DriverType, Software, Flags, pFeatureLevels, FeatureLevels,
SDKVersion, NULL, NULL, ppDevice, pFeatureLevel, ppImmediateContext);
return ret;
}
static HRESULT WINAPI D3D11CreateDeviceAndSwapChain_hook(
__in_opt IDXGIAdapter* pAdapter,
D3D_DRIVER_TYPE DriverType,
HMODULE Software,
UINT Flags,
__in_ecount_opt( FeatureLevels ) CONST D3D_FEATURE_LEVEL* pFeatureLevels,
UINT FeatureLevels,
UINT SDKVersion,
__in_opt CONST DXGI_SWAP_CHAIN_DESC* pSwapChainDesc,
__out_opt IDXGISwapChain** ppSwapChain,
__out_opt ID3D11Device** ppDevice,
__out_opt D3D_FEATURE_LEVEL* pFeatureLevel,
__out_opt ID3D11DeviceContext** ppImmediateContext )
{
HRESULT ret = d3d11hooks.Create_Internal(
pAdapter, DriverType, Software, Flags, pFeatureLevels, FeatureLevels,
SDKVersion, pSwapChainDesc, ppSwapChain, ppDevice, pFeatureLevel, ppImmediateContext);
return ret;
}
};
D3D11Hook D3D11Hook::d3d11hooks;
extern "C" __declspec(dllexport)
HRESULT __cdecl RENDERDOC_CreateWrappedD3D11DeviceAndSwapChain(
__in_opt IDXGIAdapter* pAdapter,
D3D_DRIVER_TYPE DriverType,
HMODULE Software,
UINT Flags,
__in_ecount_opt( FeatureLevels ) CONST D3D_FEATURE_LEVEL* pFeatureLevels,
UINT FeatureLevels,
UINT SDKVersion,
__in_opt CONST DXGI_SWAP_CHAIN_DESC* pSwapChainDesc,
__out_opt IDXGISwapChain** ppSwapChain,
__out_opt ID3D11Device** ppDevice,
__out_opt D3D_FEATURE_LEVEL* pFeatureLevel,
__out_opt ID3D11DeviceContext** ppImmediateContext )
{
return D3D11Hook::CreateWrappedDeviceAndSwapChain(pAdapter, DriverType, Software, Flags, pFeatureLevels, FeatureLevels,
SDKVersion, pSwapChainDesc, ppSwapChain, ppDevice, pFeatureLevel, ppImmediateContext);
}
extern "C" __declspec(dllexport)
HRESULT __cdecl RENDERDOC_CreateWrappedD3D11Device(
__in_opt IDXGIAdapter* pAdapter,
D3D_DRIVER_TYPE DriverType,
HMODULE Software,
UINT Flags,
__in_ecount_opt( FeatureLevels ) CONST D3D_FEATURE_LEVEL* pFeatureLevels,
UINT FeatureLevels,
UINT SDKVersion,
__out_opt ID3D11Device** ppDevice,
__out_opt D3D_FEATURE_LEVEL* pFeatureLevel,
__out_opt ID3D11DeviceContext** ppImmediateContext )
{
return D3D11Hook::CreateWrappedDeviceAndSwapChain(pAdapter, DriverType, Software, Flags, pFeatureLevels, FeatureLevels,
SDKVersion, NULL, NULL, ppDevice, pFeatureLevel, ppImmediateContext);
}
+142
View File
@@ -0,0 +1,142 @@
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2014 Crytek
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
#include <d3d9.h>
#include "driver/d3d11/d3d11_device.h"
#include "hooks.h"
#define DLL_NAME "d3d9.dll"
typedef int (WINAPI* PFN_BEGIN_EVENT)( DWORD, WCHAR* );
typedef int (WINAPI* PFN_END_EVENT)( );
typedef int (WINAPI* PFN_SET_MARKER_EVENT)( DWORD, WCHAR* );
typedef void (WINAPI* PFN_SET_OPTIONS)( DWORD );
typedef DWORD (WINAPI* PFN_GET_OPTIONS)( );
typedef IDirect3D9* (WINAPI* PFN_D3D9_CREATE)( UINT );
class D3D9Hook : LibraryHook
{
public:
D3D9Hook() { LibraryHooks::GetInstance().RegisterHook(DLL_NAME, this); m_EnabledHooks = true; }
bool CreateHooks(const char *libName)
{
bool success = true;
success &= PERF_BeginEvent.Initialize("D3DPERF_BeginEvent", DLL_NAME, PERF_BeginEvent_hook);
success &= PERF_EndEvent.Initialize("D3DPERF_EndEvent", DLL_NAME, PERF_EndEvent_hook);
success &= PERF_SetMarker.Initialize("D3DPERF_SetMarker", DLL_NAME, PERF_SetMarker_hook);
success &= PERF_SetOptions.Initialize("D3DPERF_SetOptions", DLL_NAME, PERF_SetOptions_hook);
success &= PERF_GetStatus.Initialize("D3DPERF_GetStatus", DLL_NAME, PERF_GetStatus_hook);
success &= Create9.Initialize("Direct3DCreate9", DLL_NAME, Create9_hook);
if(!success) return false;
m_HasHooks = true;
m_EnabledHooks = true;
return true;
}
void EnableHooks(const char *libName, bool enable)
{
m_EnabledHooks = enable;
}
private:
static D3D9Hook d3d9hooks;
bool m_HasHooks;
bool m_EnabledHooks;
// D3DPERF api
Hook<PFN_BEGIN_EVENT> PERF_BeginEvent;
Hook<PFN_END_EVENT> PERF_EndEvent;
Hook<PFN_SET_MARKER_EVENT> PERF_SetMarker;
Hook<PFN_SET_OPTIONS> PERF_SetOptions;
Hook<PFN_GET_OPTIONS> PERF_GetStatus;
Hook<PFN_D3D9_CREATE> Create9;
static int WINAPI PERF_BeginEvent_hook(DWORD col, WCHAR *wszName)
{
int ret = WrappedID3D11Device::BeginEvent((uint32_t)col, wszName);
d3d9hooks.PERF_BeginEvent()(col, wszName);
return ret;
}
static int WINAPI PERF_EndEvent_hook()
{
int ret = WrappedID3D11Device::EndEvent();
d3d9hooks.PERF_EndEvent()();
return ret;
}
static void WINAPI PERF_SetMarker_hook(DWORD col, WCHAR *wszName)
{
WrappedID3D11Device::SetMarker((uint32_t)col, wszName);
d3d9hooks.PERF_SetMarker()(col, wszName);
}
static void WINAPI PERF_SetOptions_hook(DWORD dwOptions)
{
if(dwOptions & 1)
{
RDCDEBUG("Application requested not to be hooked.");
LibraryHooks::GetInstance().EnableHooks(false);
}
else
{
LibraryHooks::GetInstance().EnableHooks(true);
}
}
static DWORD WINAPI PERF_GetStatus_hook()
{
if(d3d9hooks.m_HasHooks && d3d9hooks.m_EnabledHooks)
{
return 1;
}
return 0;
}
static IDirect3D9* WINAPI Create9_hook(UINT SDKVersion)
{
RDCWARN("App creating d3d9 %x device - not hooked!", SDKVersion);
return d3d9hooks.Create9()(SDKVersion);
}
};
D3D9Hook D3D9Hook::d3d9hooks;
+124
View File
@@ -0,0 +1,124 @@
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2014 Crytek
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
#include "driver/dxgi/dxgi_wrapped.h"
#include "hooks.h"
#define DLL_NAME "dxgi.dll"
typedef HRESULT (WINAPI* PFN_CREATE_DXGI_FACTORY)( __in REFIID, __out void **ppFactory);
class DXGIHook : LibraryHook
{
public:
DXGIHook() { LibraryHooks::GetInstance().RegisterHook(DLL_NAME, this); m_EnabledHooks = true; }
bool CreateHooks(const char *libName)
{
bool success = true;
// require d3d11.dll hooked as well for proper operation
if(GetModuleHandleA("d3d11.dll") == NULL) return false;
success &= CreateDXGIFactory.Initialize("CreateDXGIFactory", DLL_NAME, CreateDXGIFactory_hook);
success &= CreateDXGIFactory1.Initialize("CreateDXGIFactory1", DLL_NAME, CreateDXGIFactory1_hook);
if(!success) return false;
m_HasHooks = true;
m_EnabledHooks = true;
return true;
}
void EnableHooks(const char *libName, bool enable)
{
m_EnabledHooks = enable;
}
bool UseHooks()
{
return (dxgihooks.m_HasHooks && dxgihooks.m_EnabledHooks);
}
static HRESULT CreateWrappedFactory1(REFIID riid, void **ppFactory)
{
if(dxgihooks.m_HasHooks)
return dxgihooks.CreateDXGIFactory1_hook(riid, ppFactory);
PFN_CREATE_DXGI_FACTORY createFunc = (PFN_CREATE_DXGI_FACTORY)GetProcAddress(GetModuleHandleA("dxgi.dll"), "CreateDXGIFactory1");
if(!createFunc)
{
RDCERR("Trying to create hooked dxgi factory without dxgi loaded");
return E_INVALIDARG;
}
HRESULT ret = createFunc(riid, ppFactory);
if(SUCCEEDED(ret))
RefCountDXGIObject::HandleWrap(riid, ppFactory);
return ret;
}
private:
static DXGIHook dxgihooks;
bool m_HasHooks;
bool m_EnabledHooks;
Hook<PFN_CREATE_DXGI_FACTORY> CreateDXGIFactory;
Hook<PFN_CREATE_DXGI_FACTORY> CreateDXGIFactory1;
static HRESULT WINAPI CreateDXGIFactory_hook(__in REFIID riid, __out void **ppFactory)
{
HRESULT ret = dxgihooks.CreateDXGIFactory()(riid, ppFactory);
if(SUCCEEDED(ret) && dxgihooks.m_EnabledHooks)
RefCountDXGIObject::HandleWrap(riid, ppFactory);
return ret;
}
static HRESULT WINAPI CreateDXGIFactory1_hook(__in REFIID riid, __out void **ppFactory)
{
HRESULT ret = dxgihooks.CreateDXGIFactory1()(riid, ppFactory);
if(SUCCEEDED(ret) && dxgihooks.m_EnabledHooks)
RefCountDXGIObject::HandleWrap(riid, ppFactory);
return ret;
}
};
DXGIHook DXGIHook::dxgihooks;
extern "C" __declspec(dllexport)
HRESULT __cdecl RENDERDOC_CreateWrappedDXGIFactory1(__in REFIID riid, __out void **ppFactory)
{
return DXGIHook::CreateWrappedFactory1(riid, ppFactory);
}
+395
View File
@@ -0,0 +1,395 @@
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2014 Crytek
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
#include <dlfcn.h>
#include <stdio.h>
#include "hooks/hooks.h"
#include "driver/gl/gl_common.h"
#include "driver/gl/gl_hookset.h"
#include "driver/gl/gl_driver.h"
#include "common/string_utils.h"
// bit of a hack
namespace Keyboard { extern Display *CurrentXDisplay; }
typedef GLXContext (*PFNGLXCREATECONTEXTPROC)(Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct);
typedef const char *(*PFNGLXQUERYEXTENSIONSSTRING)(Display *dpy, int screen);
typedef Bool (*PFNGLXMAKECURRENTPROC)(Display *dpy, GLXDrawable drawable, GLXContext ctx);
typedef void (*PFNGLXSWAPBUFFERSPROC)(Display *dpy, GLXDrawable drawable);
#define HookInit(function) \
GL.function = (CONCAT(function, _hooktype))dlsym(RTLD_NEXT, STRINGIZE(function));
#define HookExtension(funcPtrType, function) \
if(!strcmp(func, STRINGIZE(function))) \
{ \
OpenGLHook::glhooks.GL.function = (funcPtrType)realFunc; \
return (__GLXextFuncPtr)&function; \
}
#define HookExtensionAlias(funcPtrType, function, alias) \
if(!strcmp(func, STRINGIZE(alias))) \
{ \
OpenGLHook::glhooks.GL.function = (funcPtrType)realFunc; \
return (__GLXextFuncPtr)&function; \
}
/*
in bash:
function HookWrapper()
{
N=$1;
echo -n "#define HookWrapper$N(ret, function";
for I in `seq 1 $N`; do echo -n ", t$I, p$I"; done;
echo ") \\";
echo -en "\ttypedef ret (*CONCAT(function, _hooktype)) (";
for I in `seq 1 $N`; do echo -n "t$I"; if [ $I -ne $N ]; then echo -n ", "; fi; done;
echo "); \\";
echo -e "\textern \"C\" __attribute__ ((visibility (\"default\"))) \\";
echo -en "\tret function(";
for I in `seq 1 $N`; do echo -n "t$I p$I"; if [ $I -ne $N ]; then echo -n ", "; fi; done;
echo ") \\";
echo -en "\t{ return OpenGLHook::glhooks.GetDriver()->function(";
for I in `seq 1 $N`; do echo -n "p$I"; if [ $I -ne $N ]; then echo -n ", "; fi; done;
echo -n "); }";
}
for I in `seq 0 ...`; do HookWrapper $I; echo; done
*/
#define HookWrapper0(ret, function) \
typedef ret (*CONCAT(function, _hooktype)) (); \
extern "C" __attribute__ ((visibility ("default"))) \
ret function() \
{ return OpenGLHook::glhooks.GetDriver()->function(); }
#define HookWrapper1(ret, function, t1, p1) \
typedef ret (*CONCAT(function, _hooktype)) (t1); \
extern "C" __attribute__ ((visibility ("default"))) \
ret function(t1 p1) \
{ return OpenGLHook::glhooks.GetDriver()->function(p1); }
#define HookWrapper2(ret, function, t1, p1, t2, p2) \
typedef ret (*CONCAT(function, _hooktype)) (t1, t2); \
extern "C" __attribute__ ((visibility ("default"))) \
ret function(t1 p1, t2 p2) \
{ return OpenGLHook::glhooks.GetDriver()->function(p1, p2); }
#define HookWrapper3(ret, function, t1, p1, t2, p2, t3, p3) \
typedef ret (*CONCAT(function, _hooktype)) (t1, t2, t3); \
extern "C" __attribute__ ((visibility ("default"))) \
ret function(t1 p1, t2 p2, t3 p3) \
{ return OpenGLHook::glhooks.GetDriver()->function(p1, p2, p3); }
#define HookWrapper4(ret, function, t1, p1, t2, p2, t3, p3, t4, p4) \
typedef ret (*CONCAT(function, _hooktype)) (t1, t2, t3, t4); \
extern "C" __attribute__ ((visibility ("default"))) \
ret function(t1 p1, t2 p2, t3 p3, t4 p4) \
{ return OpenGLHook::glhooks.GetDriver()->function(p1, p2, p3, p4); }
#define HookWrapper5(ret, function, t1, p1, t2, p2, t3, p3, t4, p4, t5, p5) \
typedef ret (*CONCAT(function, _hooktype)) (t1, t2, t3, t4, t5); \
extern "C" __attribute__ ((visibility ("default"))) \
ret function(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5) \
{ return OpenGLHook::glhooks.GetDriver()->function(p1, p2, p3, p4, p5); }
#define HookWrapper6(ret, function, t1, p1, t2, p2, t3, p3, t4, p4, t5, p5, t6, p6) \
typedef ret (*CONCAT(function, _hooktype)) (t1, t2, t3, t4, t5, t6); \
extern "C" __attribute__ ((visibility ("default"))) \
ret function(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6) \
{ return OpenGLHook::glhooks.GetDriver()->function(p1, p2, p3, p4, p5, p6); }
#define HookWrapper7(ret, function, t1, p1, t2, p2, t3, p3, t4, p4, t5, p5, t6, p6, t7, p7) \
typedef ret (*CONCAT(function, _hooktype)) (t1, t2, t3, t4, t5, t6, t7); \
extern "C" __attribute__ ((visibility ("default"))) \
ret function(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7) \
{ return OpenGLHook::glhooks.GetDriver()->function(p1, p2, p3, p4, p5, p6, p7); }
#define HookWrapper8(ret, function, t1, p1, t2, p2, t3, p3, t4, p4, t5, p5, t6, p6, t7, p7, t8, p8) \
typedef ret (*CONCAT(function, _hooktype)) (t1, t2, t3, t4, t5, t6, t7, t8); \
extern "C" __attribute__ ((visibility ("default"))) \
ret function(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8) \
{ return OpenGLHook::glhooks.GetDriver()->function(p1, p2, p3, p4, p5, p6, p7, p8); }
#define HookWrapper9(ret, function, t1, p1, t2, p2, t3, p3, t4, p4, t5, p5, t6, p6, t7, p7, t8, p8, t9, p9) \
typedef ret (*CONCAT(function, _hooktype)) (t1, t2, t3, t4, t5, t6, t7, t8, t9); \
extern "C" __attribute__ ((visibility ("default"))) \
ret function(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8, t9 p9) \
{ return OpenGLHook::glhooks.GetDriver()->function(p1, p2, p3, p4, p5, p6, p7, p8, p9); }
#define HookWrapper10(ret, function, t1, p1, t2, p2, t3, p3, t4, p4, t5, p5, t6, p6, t7, p7, t8, p8, t9, p9, t10, p10) \
typedef ret (*CONCAT(function, _hooktype)) (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10); \
extern "C" __attribute__ ((visibility ("default"))) \
ret function(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8, t9 p9, t10 p10) \
{ return OpenGLHook::glhooks.GetDriver()->function(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10); }
class OpenGLHook : LibraryHook
{
public:
OpenGLHook()
{
LibraryHooks::GetInstance().RegisterHook("libGL.so", this);
// TODO: need to check against implementation to ensure we don't claim to support
// an extension that it doesn't!
glXExts.push_back("GLX_ARB_extensions_string");
//glXExts.push_back("GLX_ARB_multisample");
glXExts.push_back("GLX_ARB_create_context");
glXExts.push_back("GLX_ARB_create_context_profile");
merge(glXExts, glXExtsString, ' ');
m_GLDriver = NULL;
m_EnabledHooks = true;
m_PopulatedHooks = false;
}
~OpenGLHook()
{
delete m_GLDriver;
}
bool CreateHooks(const char *libName)
{
RDCEraseEl(GL);
if(!m_EnabledHooks)
return false;
bool success = SetupHooks(GL);
if(!success) return false;
m_HasHooks = true;
return true;
}
void EnableHooks(const char *libName, bool enable)
{
m_EnabledHooks = enable;
}
static OpenGLHook glhooks;
const GLHookSet &GetRealFunctions()
{
if(!m_PopulatedHooks)
m_PopulatedHooks = PopulateHooks();
return GL;
}
WrappedOpenGL *GetDriver()
{
if(m_GLDriver == NULL)
m_GLDriver = new WrappedOpenGL(L"", GL);
return m_GLDriver;
}
PFNGLXCREATECONTEXTPROC glXCreateContext_real;
PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB_real;
PFNGLXGETPROCADDRESSPROC glXGetProcAddress_real;
PFNGLXMAKECURRENTPROC glXMakeCurrent_real;
PFNGLXSWAPBUFFERSPROC glXSwapBuffers_real;
WrappedOpenGL *m_GLDriver;
GLHookSet GL;
vector<string> glXExts;
string glXExtsString;
bool m_PopulatedHooks;
bool m_HasHooks;
bool m_EnabledHooks;
bool SetupHooks(GLHookSet &GL);
bool PopulateHooks();
};
DefineDLLExportHooks();
DefineGLExtensionHooks();
typedef XVisualInfo* (*PFNGLXGETVISUALFROMFBCONFIGPROC)(Display *dpy, GLXFBConfig config);
typedef int (*PFNGLXGETCONFIGPROC)(Display *dpy, XVisualInfo *vis, int attrib, int * value);
static PFNGLXGETCONFIGPROC getVisualInfoAttrib = NULL;
__attribute__ ((visibility ("default")))
GLXContext glXCreateContext(Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct)
{
GLXContext ret = OpenGLHook::glhooks.glXCreateContext_real(dpy, vis, shareList, direct);
GLInitParams init;
init.width = 0;
init.height = 0;
int value = 0;
if(Keyboard::CurrentXDisplay == NULL) Keyboard::CurrentXDisplay = dpy;
if(getVisualInfoAttrib == NULL) getVisualInfoAttrib = (PFNGLXGETCONFIGPROC)dlsym(RTLD_NEXT, "glXGetConfig");
getVisualInfoAttrib(dpy, vis, GLX_BUFFER_SIZE, &value); init.colorBits = value;
getVisualInfoAttrib(dpy, vis, GLX_DEPTH_SIZE, &value); init.depthBits = value;
getVisualInfoAttrib(dpy, vis, GLX_STENCIL_SIZE, &value); init.stencilBits = value;
OpenGLHook::glhooks.GetDriver()->CreateContext(NULL, ret, shareList, init);
return ret;
}
__attribute__ ((visibility ("default")))
GLXContext glXCreateContextAttribsARB(Display *dpy, GLXFBConfig config, GLXContext shareList, Bool direct, const int *attribList)
{
GLXContext ret = OpenGLHook::glhooks.glXCreateContextAttribsARB_real(dpy, config, shareList, direct, attribList);
PFNGLXGETVISUALFROMFBCONFIGPROC getVisual = (PFNGLXGETVISUALFROMFBCONFIGPROC)dlsym(RTLD_NEXT, "glXGetVisualFromFBConfig");
XVisualInfo *vis = getVisual(dpy, config);
GLInitParams init;
init.width = 0;
init.height = 0;
int value = 0;
if(Keyboard::CurrentXDisplay == NULL) Keyboard::CurrentXDisplay = dpy;
if(getVisualInfoAttrib == NULL) getVisualInfoAttrib = (PFNGLXGETCONFIGPROC)dlsym(RTLD_NEXT, "glXGetConfig");
getVisualInfoAttrib(dpy, vis, GLX_BUFFER_SIZE, &value); init.colorBits = value;
getVisualInfoAttrib(dpy, vis, GLX_DEPTH_SIZE, &value); init.depthBits = value;
getVisualInfoAttrib(dpy, vis, GLX_STENCIL_SIZE, &value); init.stencilBits = value;
XFree(vis);
OpenGLHook::glhooks.GetDriver()->CreateContext(NULL, ret, shareList, init);
return ret;
}
__attribute__ ((visibility ("default")))
Bool glXMakeCurrent(Display *dpy, GLXDrawable drawable, GLXContext ctx)
{
Bool ret = OpenGLHook::glhooks.glXMakeCurrent_real(dpy, drawable, ctx);
OpenGLHook::glhooks.GetDriver()->ActivateContext((void *)drawable, ctx);
return ret;
}
__attribute__ ((visibility ("default")))
void glXSwapBuffers(Display *dpy, GLXDrawable drawable)
{
Window root;
int x, y;
unsigned int width, height, border_width, depth;
XGetGeometry(dpy, drawable, &root, &x, &y, &width, &height, &border_width, &depth);
OpenGLHook::glhooks.GetDriver()->WindowSize((void *)drawable, width, height);
OpenGLHook::glhooks.GetDriver()->Present((void *)drawable);
OpenGLHook::glhooks.glXSwapBuffers_real(dpy, drawable);
}
__attribute__ ((visibility ("default")))
const char *glXQueryExtensionsString(Display *dpy, int screen)
{
#if !defined(_RELEASE)
PFNGLXQUERYEXTENSIONSSTRING glXGetExtStr = (PFNGLXQUERYEXTENSIONSSTRING)dlsym(RTLD_NEXT, "glXQueryExtensionsString");
string realExtsString = glXGetExtStr(dpy, screen);
vector<string> realExts;
split(realExtsString, realExts, ' ');
#endif
return OpenGLHook::glhooks.glXExtsString.c_str();
}
bool OpenGLHook::SetupHooks(GLHookSet &GL)
{
bool success = true;
glXGetProcAddress_real = (PFNGLXGETPROCADDRESSPROC)dlsym(RTLD_NEXT, "glXGetProcAddress");
glXCreateContext_real = (PFNGLXCREATECONTEXTPROC)dlsym(RTLD_NEXT, "glXCreateContext");
glXCreateContextAttribsARB_real = (PFNGLXCREATECONTEXTATTRIBSARBPROC)dlsym(RTLD_NEXT, "glXCreateContextAttribsARB");
glXMakeCurrent_real = (PFNGLXMAKECURRENTPROC)dlsym(RTLD_NEXT, "glXMakeCurrent");
glXSwapBuffers_real = (PFNGLXSWAPBUFFERSPROC)dlsym(RTLD_NEXT, "glXSwapBuffers");
DLLExportHooks();
return success;
}
__attribute__ ((visibility ("default")))
__GLXextFuncPtr glXGetProcAddress(const GLubyte *f)
{
__GLXextFuncPtr realFunc = OpenGLHook::glhooks.glXGetProcAddress_real(f);
const char *func = (const char *)f;
// if the real RC doesn't support this function, don't bother hooking
if(realFunc == NULL)
return realFunc;
if(!strcmp(func, "glXCreateContextAttribsARB"))
{
OpenGLHook::glhooks.glXCreateContextAttribsARB_real = (PFNGLXCREATECONTEXTATTRIBSARBPROC)realFunc;
return (__GLXextFuncPtr)&glXCreateContextAttribsARB;
}
HookCheckGLExtensions();
// claim not to know this extension!
RDCWARN("Claiming not to know extension that is available - %s", func);
return NULL;
}
bool OpenGLHook::PopulateHooks()
{
bool success = true;
if(glXGetProcAddress_real == NULL)
glXGetProcAddress_real = (PFNGLXGETPROCADDRESSPROC)dlsym(RTLD_NEXT, "glXGetProcAddress");
glXGetProcAddress_real((const GLubyte *)"glXCreateContextAttribsARB");
#undef HookInit
#define HookInit(function) if(GL.function == NULL) GL.function = (CONCAT(function, _hooktype))dlsym(RTLD_NEXT, "glXGetProcAddress");
// cheeky
#undef HookExtension
#define HookExtension(funcPtrType, function) glXGetProcAddress((const GLubyte *)STRINGIZE(function))
#undef HookExtensionAlias
#define HookExtensionAlias(funcPtrType, function, alias)
DLLExportHooks();
HookCheckGLExtensions();
return true;
}
OpenGLHook OpenGLHook::glhooks;
const GLHookSet &GetRealFunctions() { return OpenGLHook::glhooks.GetRealFunctions(); }
+411
View File
@@ -0,0 +1,411 @@
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2014 Crytek
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
#include "driver/gl/gl_common.h"
#include "driver/gl/gl_hookset.h"
#include "driver/gl/gl_driver.h"
#include "common/string_utils.h"
#include "hooks.h"
#define DLL_NAME "opengl32.dll"
#define HookInit(function) \
success &= CONCAT(function, _hook).Initialize(STRINGIZE(function), DLL_NAME, CONCAT(function, _hooked)); \
GL.function = CONCAT(function, _hook)();
#define HookExtension(funcPtrType, function) \
if(!strcmp(func, STRINGIZE(function))) \
{ \
glhooks.GL.function = (funcPtrType)realFunc; \
return (PROC)&glhooks.CONCAT(function,_hooked); \
}
#define HookExtensionAlias(funcPtrType, function, alias) \
if(!strcmp(func, STRINGIZE(alias))) \
{ \
glhooks.GL.function = (funcPtrType)realFunc; \
return (PROC)&glhooks.CONCAT(function,_hooked); \
}
/*
in bash:
function HookWrapper()
{
N=$1;
echo -n "#define HookWrapper$N(ret, function";
for I in `seq 1 $N`; do echo -n ", t$I, p$I"; done;
echo ") \\";
echo -en "\tHook<ret (WINAPI *) (";
for I in `seq 1 $N`; do echo -n "t$I"; if [ $I -ne $N ]; then echo -n ", "; fi; done;
echo ")> CONCAT(function, _hook); \\";
echo -en "\ttypedef ret (WINAPI *CONCAT(function, _hooktype)) (";
for I in `seq 1 $N`; do echo -n "t$I"; if [ $I -ne $N ]; then echo -n ", "; fi; done;
echo "); \\";
echo -en "\tstatic ret WINAPI CONCAT(function, _hooked)(";
for I in `seq 1 $N`; do echo -n "t$I p$I"; if [ $I -ne $N ]; then echo -n ", "; fi; done;
echo ") \\";
echo -en "\t{ return glhooks.GetDriver()->function(";
for I in `seq 1 $N`; do echo -n "p$I"; if [ $I -ne $N ]; then echo -n ", "; fi; done;
echo -n "); }";
}
for I in `seq 0 ...`; do HookWrapper $I; echo; done
*/
#define HookWrapper0(ret, function) \
Hook<ret (WINAPI *) ()> CONCAT(function, _hook); \
typedef ret (WINAPI *CONCAT(function, _hooktype)) (); \
static ret WINAPI CONCAT(function, _hooked)() \
{ return glhooks.GetDriver()->function(); }
#define HookWrapper1(ret, function, t1, p1) \
Hook<ret (WINAPI *) (t1)> CONCAT(function, _hook); \
typedef ret (WINAPI *CONCAT(function, _hooktype)) (t1); \
static ret WINAPI CONCAT(function, _hooked)(t1 p1) \
{ return glhooks.GetDriver()->function(p1); }
#define HookWrapper2(ret, function, t1, p1, t2, p2) \
Hook<ret (WINAPI *) (t1, t2)> CONCAT(function, _hook); \
typedef ret (WINAPI *CONCAT(function, _hooktype)) (t1, t2); \
static ret WINAPI CONCAT(function, _hooked)(t1 p1, t2 p2) \
{ return glhooks.GetDriver()->function(p1, p2); }
#define HookWrapper3(ret, function, t1, p1, t2, p2, t3, p3) \
Hook<ret (WINAPI *) (t1, t2, t3)> CONCAT(function, _hook); \
typedef ret (WINAPI *CONCAT(function, _hooktype)) (t1, t2, t3); \
static ret WINAPI CONCAT(function, _hooked)(t1 p1, t2 p2, t3 p3) \
{ return glhooks.GetDriver()->function(p1, p2, p3); }
#define HookWrapper4(ret, function, t1, p1, t2, p2, t3, p3, t4, p4) \
Hook<ret (WINAPI *) (t1, t2, t3, t4)> CONCAT(function, _hook); \
typedef ret (WINAPI *CONCAT(function, _hooktype)) (t1, t2, t3, t4); \
static ret WINAPI CONCAT(function, _hooked)(t1 p1, t2 p2, t3 p3, t4 p4) \
{ return glhooks.GetDriver()->function(p1, p2, p3, p4); }
#define HookWrapper5(ret, function, t1, p1, t2, p2, t3, p3, t4, p4, t5, p5) \
Hook<ret (WINAPI *) (t1, t2, t3, t4, t5)> CONCAT(function, _hook); \
typedef ret (WINAPI *CONCAT(function, _hooktype)) (t1, t2, t3, t4, t5); \
static ret WINAPI CONCAT(function, _hooked)(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5) \
{ return glhooks.GetDriver()->function(p1, p2, p3, p4, p5); }
#define HookWrapper6(ret, function, t1, p1, t2, p2, t3, p3, t4, p4, t5, p5, t6, p6) \
Hook<ret (WINAPI *) (t1, t2, t3, t4, t5, t6)> CONCAT(function, _hook); \
typedef ret (WINAPI *CONCAT(function, _hooktype)) (t1, t2, t3, t4, t5, t6); \
static ret WINAPI CONCAT(function, _hooked)(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6) \
{ return glhooks.GetDriver()->function(p1, p2, p3, p4, p5, p6); }
#define HookWrapper7(ret, function, t1, p1, t2, p2, t3, p3, t4, p4, t5, p5, t6, p6, t7, p7) \
Hook<ret (WINAPI *) (t1, t2, t3, t4, t5, t6, t7)> CONCAT(function, _hook); \
typedef ret (WINAPI *CONCAT(function, _hooktype)) (t1, t2, t3, t4, t5, t6, t7); \
static ret WINAPI CONCAT(function, _hooked)(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7) \
{ return glhooks.GetDriver()->function(p1, p2, p3, p4, p5, p6, p7); }
#define HookWrapper8(ret, function, t1, p1, t2, p2, t3, p3, t4, p4, t5, p5, t6, p6, t7, p7, t8, p8) \
Hook<ret (WINAPI *) (t1, t2, t3, t4, t5, t6, t7, t8)> CONCAT(function, _hook); \
typedef ret (WINAPI *CONCAT(function, _hooktype)) (t1, t2, t3, t4, t5, t6, t7, t8); \
static ret WINAPI CONCAT(function, _hooked)(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8) \
{ return glhooks.GetDriver()->function(p1, p2, p3, p4, p5, p6, p7, p8); }
#define HookWrapper9(ret, function, t1, p1, t2, p2, t3, p3, t4, p4, t5, p5, t6, p6, t7, p7, t8, p8, t9, p9) \
Hook<ret (WINAPI *) (t1, t2, t3, t4, t5, t6, t7, t8, t9)> CONCAT(function, _hook); \
typedef ret (WINAPI *CONCAT(function, _hooktype)) (t1, t2, t3, t4, t5, t6, t7, t8, t9); \
static ret WINAPI CONCAT(function, _hooked)(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8, t9 p9) \
{ return glhooks.GetDriver()->function(p1, p2, p3, p4, p5, p6, p7, p8, p9); }
#define HookWrapper10(ret, function, t1, p1, t2, p2, t3, p3, t4, p4, t5, p5, t6, p6, t7, p7, t8, p8, t9, p9, t10, p10) \
Hook<ret (WINAPI *) (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10)> CONCAT(function, _hook); \
typedef ret (WINAPI *CONCAT(function, _hooktype)) (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10); \
static ret WINAPI CONCAT(function, _hooked)(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8, t9 p9, t10 p10) \
{ return glhooks.GetDriver()->function(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10); }
class OpenGLHook : LibraryHook
{
public:
OpenGLHook()
{
LibraryHooks::GetInstance().RegisterHook(DLL_NAME, this);
// TODO: need to check against implementation to ensure we don't claim to support
// an extension that it doesn't!
wglExts.push_back("WGL_ARB_extensions_string");
//wglExts.push_back("WGL_ARB_multisample");
wglExts.push_back("WGL_ARB_create_context");
wglExts.push_back("WGL_ARB_create_context_profile");
merge(wglExts, wglExtsString, ' ');
m_GLDriver = NULL;
m_EnabledHooks = true;
m_PopulatedHooks = false;
}
~OpenGLHook()
{
delete m_GLDriver;
}
bool CreateHooks(const char *libName)
{
RDCEraseEl(GL);
if(!m_EnabledHooks)
return false;
bool success = SetupHooks(GL);
if(!success) return false;
m_HasHooks = true;
return true;
}
void EnableHooks(const char *libName, bool enable)
{
m_EnabledHooks = enable;
}
static OpenGLHook glhooks;
const GLHookSet &GetRealFunctions()
{
if(!m_PopulatedHooks)
m_PopulatedHooks = PopulateHooks();
return GL;
}
private:
WrappedOpenGL *GetDriver()
{
if(m_GLDriver == NULL)
m_GLDriver = new WrappedOpenGL(L"", GL);
return m_GLDriver;
}
Hook<HGLRC (WINAPI*)(HDC)> wglCreateContext_hook;
Hook<BOOL (WINAPI*)(HDC, HGLRC)> wglMakeCurrent_hook;
Hook<PROC (WINAPI*)(const char*)> wglGetProcAddress_hook;
Hook<BOOL (WINAPI*)(HDC)> SwapBuffers_hook;
PROC (WINAPI* wglGetProcAddress_realfunc)(const char*);
PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB_realfunc;
static private GLInitParams GetInitParamsForDC(HDC dc)
{
GLInitParams ret;
int pf = GetPixelFormat(dc);
PIXELFORMATDESCRIPTOR pfd;
DescribePixelFormat(dc, pf, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
HWND w = WindowFromDC(dc);
RECT r;
GetClientRect(w, &r);
RDCLOG("dc %p. PFD: type %d, %d color bits, %d depth bits, %d stencil bits. Win: %dx%d",
dc,
pfd.iPixelType, pfd.cColorBits, pfd.cDepthBits, pfd.cStencilBits,
r.right-r.left, r.bottom-r.top);
ret.colorBits = pfd.cColorBits;
ret.depthBits = pfd.cDepthBits;
ret.stencilBits = pfd.cStencilBits;
ret.width = (r.right-r.left);
ret.height = (r.bottom-r.top);
if(pfd.iPixelType != PFD_TYPE_RGBA)
{
RDCERR("Unsupported OpenGL pixel type");
}
return ret;
}
static HGLRC WINAPI wglCreateContext_hooked(HDC dc)
{
HGLRC ret = glhooks.wglCreateContext_hook()(dc);
glhooks.GetDriver()->CreateContext(WindowFromDC(dc), ret, NULL, GetInitParamsForDC(dc));
return ret;
}
static HGLRC WINAPI wglCreateContextAttribsARB_hooked(HDC dc, HGLRC hShareContext, const int *attribList)
{
HGLRC ret = glhooks.wglCreateContextAttribsARB_realfunc(dc, hShareContext, attribList);
glhooks.GetDriver()->CreateContext(WindowFromDC(dc), ret, hShareContext, GetInitParamsForDC(dc));
return ret;
}
// wglShareLists_hooked ?
static BOOL WINAPI wglMakeCurrent_hooked(HDC dc, HGLRC rc)
{
BOOL ret = glhooks.wglMakeCurrent_hook()(dc, rc);
glhooks.GetDriver()->ActivateContext(WindowFromDC(dc), rc);
return ret;
}
static BOOL WINAPI SwapBuffers_hooked(HDC dc)
{
HWND w = WindowFromDC(dc);
RECT r;
GetClientRect(w, &r);
glhooks.GetDriver()->WindowSize(w, r.right-r.left, r.bottom-r.top);
glhooks.GetDriver()->Present(dc);
return glhooks.SwapBuffers_hook()(dc);
}
static const char * WINAPI wglGetExtensionsStringARB_hooked(HDC dc)
{
#if !defined(_RELEASE)
PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtStrARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC)glhooks.wglGetProcAddress_realfunc("wglGetExtensionsStringARB");
string realExtsString = wglGetExtStrARB(dc);
vector<string> realExts;
split(realExtsString, realExts, ' ');
#endif
return glhooks.wglExtsString.c_str();
}
static const char * WINAPI wglGetExtensionsStringEXT_hooked()
{
#if !defined(_RELEASE)
PFNWGLGETEXTENSIONSSTRINGEXTPROC wglGetExtStrEXT = (PFNWGLGETEXTENSIONSSTRINGEXTPROC)glhooks.wglGetProcAddress_realfunc("wglGetExtensionsStringEXT");
string realExtsString = wglGetExtStrEXT();
vector<string> realExts;
split(realExtsString, realExts, ' ');
#endif
return glhooks.wglExtsString.c_str();
}
static PROC WINAPI wglGetProcAddress_hooked(const char *func)
{
PROC realFunc = glhooks.wglGetProcAddress_realfunc(func);
// if the real RC doesn't support this function, don't bother hooking
if(realFunc == NULL)
return realFunc;
if(!strcmp(func, "wglGetExtensionsStringEXT"))
{
return (PROC)&wglGetExtensionsStringEXT_hooked;
}
if(!strcmp(func, "wglGetExtensionsStringARB"))
{
return (PROC)&wglGetExtensionsStringARB_hooked;
}
if(!strcmp(func, "wglCreateContextAttribsARB"))
{
glhooks.wglCreateContextAttribsARB_realfunc = (PFNWGLCREATECONTEXTATTRIBSARBPROC)realFunc;
return (PROC)&wglCreateContextAttribsARB_hooked;
}
HookCheckWGLExtensions();
HookCheckGLExtensions();
// claim not to know this extension!
RDCWARN("Claiming not to know extension that is available - %hs", func);
return NULL;
}
WrappedOpenGL *m_GLDriver;
GLHookSet GL;
vector<string> wglExts;
string wglExtsString;
bool m_PopulatedHooks;
bool m_HasHooks;
bool m_EnabledHooks;
bool SetupHooks(GLHookSet &GL)
{
bool success = true;
success &= wglCreateContext_hook.Initialize("wglCreateContext", DLL_NAME, wglCreateContext_hooked);
success &= wglMakeCurrent_hook.Initialize("wglMakeCurrent", DLL_NAME, wglMakeCurrent_hooked);
success &= wglGetProcAddress_hook.Initialize("wglGetProcAddress", DLL_NAME, wglGetProcAddress_hooked);
success &= SwapBuffers_hook.Initialize("SwapBuffers", "gdi32.dll", SwapBuffers_hooked);
wglGetProcAddress_realfunc = wglGetProcAddress_hook();
DLLExportHooks();
return success;
}
bool PopulateHooks()
{
bool success = true;
if(wglGetProcAddress_realfunc == NULL)
wglGetProcAddress_realfunc = (PROC (WINAPI*)(const char*))Process::GetFunctionAddress(DLL_NAME, "wglGetProcAddress");
wglGetProcAddress_hooked("wglCreateContextAttribsARB");
#undef HookInit
#define HookInit(function) if(GL.function == NULL) GL.function = (CONCAT(function, _hooktype)) Process::GetFunctionAddress(DLL_NAME, STRINGIZE(function));
// cheeky
#undef HookExtension
#define HookExtension(funcPtrType, function) wglGetProcAddress_hooked(STRINGIZE(function))
#undef HookExtensionAlias
#define HookExtensionAlias(funcPtrType, function, alias)
DLLExportHooks();
HookCheckWGLExtensions();
HookCheckGLExtensions();
return true;
}
DefineDLLExportHooks();
DefineWGLExtensionHooks();
DefineGLExtensionHooks();
};
OpenGLHook OpenGLHook::glhooks;
const GLHookSet &GetRealFunctions() { return OpenGLHook::glhooks.GetRealFunctions(); }
+68
View File
@@ -0,0 +1,68 @@
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2014 Crytek
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
#include "common/common.h"
#include "hooks.h"
LibraryHooks &LibraryHooks::GetInstance()
{
static LibraryHooks instance;
return instance;
}
void LibraryHooks::RegisterHook(const char *libName, LibraryHook *hook)
{
m_Hooks[libName] = hook;
}
void LibraryHooks::CreateHooks()
{
HOOKS_BEGIN();
for(auto it=m_Hooks.begin(); it!=m_Hooks.end(); ++it)
{
RDCDEBUG("Attempting to hook %hs", it->first);
if(it->second->CreateHooks(it->first))
{
RDCLOG("Loaded and hooked into %hs, PID %d", it->first, Process::GetCurrentPID());
}
else
{
RDCWARN("Couldn't hook into %hs", it->first);
}
}
HOOKS_END();
}
void LibraryHooks::EnableHooks(bool enable)
{
RDCDEBUG("%hs hooks!", enable ? "Enabling" : "Disabling");
if(!enable)
return;
for(auto it=m_Hooks.begin(); it!=m_Hooks.end(); ++it)
it->second->EnableHooks(it->first, enable);
}
+106
View File
@@ -0,0 +1,106 @@
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2014 Crytek
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
#pragma once
#include <map>
using std::map;
#include "os/os_specific.h"
// include os-specific hooking mechanisms here
#if defined(WIN32)
#include "3rdparty/mhook/mhook-lib/mhook.h"
template<typename FuncType>
class Hook
{
public:
Hook() { trampoline = NULL; }
~Hook() { Mhook_Unhook(&trampoline); }
FuncType operator()()
{
return (FuncType)trampoline;
}
bool Initialize(const char *function, const char *module_name, void *destination_function_ptr)
{
trampoline = Process::GetFunctionAddress(module_name, function);
if(trampoline == NULL)
return false;
return Mhook_SetHook(&trampoline, destination_function_ptr);
}
private:
void* trampoline;
};
#define HOOKS_BEGIN() Mhook_SuspendOtherThreads()
#define HOOKS_END() Mhook_ResumeOtherThreads()
#elif defined(LINUX)
// just need this for dlsym
#include <dlfcn.h>
#define HOOKS_BEGIN()
#define HOOKS_END()
#else
#error "undefined platform"
#endif
// defines the interface that a library hooking class will implement.
// the libName is the name they used when registering
struct LibraryHook
{
virtual bool CreateHooks(const char *libName) = 0;
virtual void EnableHooks(const char *libName, bool enable) = 0;
};
// this singleton allows you to compile in code that defines a hook for a given library
// (and it will be registered). Then when the renderdoc library is initialised in the target
// program CreateHooks() will be called to set up the hooks.
class LibraryHooks
{
public:
static LibraryHooks &GetInstance();
void RegisterHook(const char *libName, LibraryHook *hook);
void CreateHooks();
void EnableHooks(bool enable);
private:
typedef map<const char *, LibraryHook *> HookMap;
HookMap m_Hooks;
};
+55
View File
@@ -0,0 +1,55 @@
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2014 Crytek
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
#include "core/core.h"
#include "hooks/hooks.h"
#include "os/os_specific.h"
// DllMain equivalent
__attribute__((constructor))
void library_loaded()
{
wstring curfile;
FileIO::GetExecutableFilename(curfile);
if(curfile.find(L"/renderdoccmd") != wstring::npos ||
curfile.find(L"/renderdocui") != wstring::npos)
{
RDCDEBUG("Not creating hooks - in replay app");
RenderDoc::Inst().SetReplayApp(true);
RenderDoc::Inst().Initialise();
return;
}
else
{
RenderDoc::Inst().Initialise();
RDCLOG("Loading into %S", curfile.c_str());
LibraryHooks::GetInstance().CreateHooks();
}
}
+196
View File
@@ -0,0 +1,196 @@
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2014 Crytek
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
#include "core/core.h"
#include "replay/renderdoc.h"
#include "hooks.h"
#define DLL_NAME "kernel32.dll"
typedef BOOL (WINAPI *PFN_CREATE_PROCESS_A)(
__in_opt LPCSTR lpApplicationName,
__inout_opt LPSTR lpCommandLine,
__in_opt LPSECURITY_ATTRIBUTES lpProcessAttributes,
__in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes,
__in BOOL bInheritHandles,
__in DWORD dwCreationFlags,
__in_opt LPVOID lpEnvironment,
__in_opt LPCSTR lpCurrentDirectory,
__in LPSTARTUPINFOA lpStartupInfo,
__out LPPROCESS_INFORMATION lpProcessInformation
);
typedef BOOL (WINAPI *PFN_CREATE_PROCESS_W)(
__in_opt LPCWSTR lpApplicationName,
__inout_opt LPWSTR lpCommandLine,
__in_opt LPSECURITY_ATTRIBUTES lpProcessAttributes,
__in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes,
__in BOOL bInheritHandles,
__in DWORD dwCreationFlags,
__in_opt LPVOID lpEnvironment,
__in_opt LPCWSTR lpCurrentDirectory,
__in LPSTARTUPINFOW lpStartupInfo,
__out LPPROCESS_INFORMATION lpProcessInformation
);
class SysHook : LibraryHook
{
public:
SysHook() { LibraryHooks::GetInstance().RegisterHook(DLL_NAME, this); m_EnabledHooks = true; }
bool CreateHooks(const char *libName)
{
bool success = true;
// we want to hook CreateProcess purely so that we can recursively insert our hooks (if we so wish)
success &= CreateProcessA.Initialize("CreateProcessA", DLL_NAME, CreateProcessA_hook);
success &= CreateProcessW.Initialize("CreateProcessW", DLL_NAME, CreateProcessW_hook);
if(!success) return false;
m_HasHooks = true;
m_EnabledHooks = true;
return true;
}
void EnableHooks(const char *libName, bool enable)
{
m_EnabledHooks = enable;
}
private:
static SysHook syshooks;
bool m_HasHooks;
bool m_EnabledHooks;
// D3DPERF api
Hook<PFN_CREATE_PROCESS_A> CreateProcessA;
Hook<PFN_CREATE_PROCESS_W> CreateProcessW;
static BOOL WINAPI CreateProcessA_hook(
__in_opt LPCSTR lpApplicationName,
__inout_opt LPSTR lpCommandLine,
__in_opt LPSECURITY_ATTRIBUTES lpProcessAttributes,
__in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes,
__in BOOL bInheritHandles,
__in DWORD dwCreationFlags,
__in_opt LPVOID lpEnvironment,
__in_opt LPCSTR lpCurrentDirectory,
__in LPSTARTUPINFOA lpStartupInfo,
__out LPPROCESS_INFORMATION lpProcessInformation)
{
PROCESS_INFORMATION dummy;
RDCEraseEl(dummy);
// not sure if this is valid, but I need the PID so I'll fill in my own struct to ensure that.
if(lpProcessInformation == NULL)
{
lpProcessInformation = &dummy;
}
dwCreationFlags |= CREATE_SUSPENDED;
BOOL ret = syshooks.CreateProcessA()(lpApplicationName, lpCommandLine,
lpProcessAttributes, lpThreadAttributes,
bInheritHandles, dwCreationFlags,
lpEnvironment, lpCurrentDirectory, lpStartupInfo,
lpProcessInformation);
if(RenderDoc::Inst().GetCaptureOptions().HookIntoChildren)
{
RDCDEBUG("Intercepting CreateProcessA");
// inherit logfile and capture options
RENDERDOC_InjectIntoProcess(lpProcessInformation->dwProcessId,
RenderDoc::Inst().GetLogFile(), &RenderDoc::Inst().GetCaptureOptions(), false);
}
ResumeThread(lpProcessInformation->hThread);
// ensure we clean up after ourselves
if(dummy.dwProcessId != 0)
{
CloseHandle(dummy.hProcess);
CloseHandle(dummy.hThread);
}
return ret;
}
static BOOL WINAPI CreateProcessW_hook(
__in_opt LPCWSTR lpApplicationName,
__inout_opt LPWSTR lpCommandLine,
__in_opt LPSECURITY_ATTRIBUTES lpProcessAttributes,
__in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes,
__in BOOL bInheritHandles,
__in DWORD dwCreationFlags,
__in_opt LPVOID lpEnvironment,
__in_opt LPCWSTR lpCurrentDirectory,
__in LPSTARTUPINFOW lpStartupInfo,
__out LPPROCESS_INFORMATION lpProcessInformation)
{
PROCESS_INFORMATION dummy;
RDCEraseEl(dummy);
// not sure if this is valid, but I need the PID so I'll fill in my own struct to ensure that.
if(lpProcessInformation == NULL)
{
lpProcessInformation = &dummy;
}
dwCreationFlags |= CREATE_SUSPENDED;
BOOL ret = syshooks.CreateProcessW()(lpApplicationName, lpCommandLine,
lpProcessAttributes, lpThreadAttributes,
bInheritHandles, dwCreationFlags,
lpEnvironment, lpCurrentDirectory, lpStartupInfo,
lpProcessInformation);
if(RenderDoc::Inst().GetCaptureOptions().HookIntoChildren)
{
RDCDEBUG("Intercepting CreateProcessW");
// inherit logfile and capture options
RENDERDOC_InjectIntoProcess(lpProcessInformation->dwProcessId,
RenderDoc::Inst().GetLogFile(), &RenderDoc::Inst().GetCaptureOptions(), false);
}
ResumeThread(lpProcessInformation->hThread);
// ensure we clean up after ourselves
if(dummy.dwProcessId != 0)
{
CloseHandle(dummy.hProcess);
CloseHandle(dummy.hThread);
}
return ret;
}
};
SysHook SysHook::syshooks;
+120
View File
@@ -0,0 +1,120 @@
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2014 Crytek
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
// win32_libentry.cpp : Defines the entry point for the DLL
#include <windows.h>
#include <tchar.h>
#include "common/common.h"
#include "common/string_utils.h"
#include "hooks/hooks.h"
#include "core/core.h"
void shutdown()
{
}
BOOL add_hooks()
{
wchar_t curFile[512];
GetModuleFileNameW(NULL, curFile, 512);
wstring f = strlower(wstring(curFile));
// bail immediately if we're in a system process. We don't want to hook, log, anything -
// this instance is being used for a shell extension.
if(f.find(L"dllhost.exe") != wstring::npos ||
f.find(L"explorer.exe") != wstring::npos)
{
#ifndef _RELEASE
OutputDebugStringA("Hosting renderdoc.dll in shell process\n");
#endif
return TRUE;
}
if(f.find(L"renderdoccmd.exe") != wstring::npos ||
f.find(L"renderdocui.exe") != wstring::npos)
{
RDCDEBUG("Not creating hooks - in replay app");
RenderDoc::Inst().SetReplayApp(true);
RenderDoc::Inst().Initialise();
return true;
}
RenderDoc::Inst().Initialise();
RDCLOG("Loading into %ls", curFile);
const wchar_t *appFilter = NULL; //L"MyApplication";
if(appFilter != NULL)
{
if(wcsstr(curFile, appFilter) == 0)
{
RDCDEBUG("Not app I want. Exiting");
return TRUE;
}
}
LibraryHooks::GetInstance().CreateHooks();
return TRUE;
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
return add_hooks();
break;
}
case DLL_THREAD_ATTACH:
{
int thread_attach=0;
break;
}
case DLL_THREAD_DETACH:
{
int thread_detach=0;
break;
}
case DLL_PROCESS_DETACH:
{
shutdown();
break;
}
}
return TRUE;
}