Skip to content

Commit

Permalink
Revert "Use D3D11 for intermediary game capture contexts"
Browse files Browse the repository at this point in the history
This reverts commit f5c2dc0.

This was a change that was added to fix support for certain intel
devices, but ultimately I feel like it's just not needed and is trying
to fix something that isn't broken.
  • Loading branch information
jp9000 committed Jan 23, 2015
1 parent 7945b7d commit ed58b5b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 79 deletions.
69 changes: 31 additions & 38 deletions GraphicsCapture/GraphicsCaptureHook/D3D9Capture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "GraphicsCaptureHook.h"

#include <d3d9.h>
#include <D3D11.h>
#include <D3D10_1.h>

typedef HRESULT (WINAPI *PRESENTPROC)(IDirect3DDevice9*, const RECT*, const RECT*, HWND, const RGNDATA*);
typedef HRESULT (WINAPI *PRESENTEXPROC)(IDirect3DDevice9*, const RECT*, const RECT*, HWND, const RGNDATA*, DWORD);
Expand Down Expand Up @@ -76,9 +76,8 @@ BOOL bUseSharedTextures = FALSE;
IDirect3DSurface9 *copyD3D9TextureGame = NULL;
extern SharedTexData *texData;
extern DXGI_FORMAT dxgiFormat;
ID3D11Device *shareDevice = NULL;
ID3D11DeviceContext *shareContext = NULL;
ID3D11Resource *copyTextureIntermediary = NULL;
ID3D10Device1 *shareDevice = NULL;
ID3D10Resource *copyTextureIntermediary = NULL;
extern HANDLE sharedHandle;

HMODULE hD3D9Dll = NULL;
Expand Down Expand Up @@ -263,7 +262,6 @@ void ClearD3D9Data()
SafeRelease(copyD3D9TextureGame);
SafeRelease(copyTextureIntermediary);
SafeRelease(shareDevice);
SafeRelease(shareContext);

DestroySharedMemory();
texData = NULL;
Expand Down Expand Up @@ -312,14 +310,6 @@ void SetupD3D9(IDirect3DDevice9 *device);
typedef HRESULT (WINAPI *CREATEDXGIFACTORY1PROC)(REFIID riid, void **ppFactory);


const static D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
D3D_FEATURE_LEVEL_9_3,
};

void DoD3D9GPUHook(IDirect3DDevice9 *device)
{
BYTE *savedData = nullptr;
Expand All @@ -329,10 +319,10 @@ void DoD3D9GPUHook(IDirect3DDevice9 *device)

HRESULT hErr;

HMODULE hD3D11 = LoadLibrary(TEXT("d3d11.dll"));
if(!hD3D11)
HMODULE hD3D10_1 = LoadLibrary(TEXT("d3d10_1.dll"));
if(!hD3D10_1)
{
RUNEVERYRESET logOutput << CurrentTimeString() << "DoD3D9GPUHook: Could not load D3D11" << endl;
RUNEVERYRESET logOutput << CurrentTimeString() << "DoD3D9GPUHook: Could not load D3D10.1" << endl;
goto finishGPUHook;
}

Expand All @@ -350,10 +340,10 @@ void DoD3D9GPUHook(IDirect3DDevice9 *device)
goto finishGPUHook;
}

PFN_D3D11_CREATE_DEVICE d3d11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(hD3D11, "D3D11CreateDevice");
if(!d3d11CreateDevice)
PFN_D3D10_CREATE_DEVICE1 d3d10CreateDevice1 = (PFN_D3D10_CREATE_DEVICE1)GetProcAddress(hD3D10_1, "D3D10CreateDevice1");
if(!d3d10CreateDevice1)
{
RUNEVERYRESET logOutput << CurrentTimeString() << "DoD3D9GPUHook: Could not load 'D3D11CreateDevice'" << endl;
RUNEVERYRESET logOutput << CurrentTimeString() << "DoD3D9GPUHook: Could not load 'D3D10CreateDevice1'" << endl;
goto finishGPUHook;
}

Expand All @@ -372,62 +362,65 @@ void DoD3D9GPUHook(IDirect3DDevice9 *device)
goto finishGPUHook;
}

if(FAILED(hErr = (*d3d11CreateDevice)(adapter, D3D_DRIVER_TYPE_UNKNOWN, NULL, 0, featureLevels, sizeof(featureLevels) / sizeof(featureLevels[0]), D3D11_SDK_VERSION, &shareDevice, NULL, &shareContext)))
if(FAILED(hErr = (*d3d10CreateDevice1)(adapter, D3D10_DRIVER_TYPE_HARDWARE, NULL, 0, D3D10_FEATURE_LEVEL_10_1, D3D10_1_SDK_VERSION, &shareDevice)))
{
RUNEVERYRESET logOutput << CurrentTimeString() << "DoD3D9GPUHook: Could not create D3D11 device, result = " << (UINT)hErr << endl;
adapter->Release();
factory->Release();
goto finishGPUHook;
if(FAILED(hErr = (*d3d10CreateDevice1)(adapter, D3D10_DRIVER_TYPE_HARDWARE, NULL, 0, D3D10_FEATURE_LEVEL_9_3, D3D10_1_SDK_VERSION, &shareDevice)))
{
RUNEVERYRESET logOutput << CurrentTimeString() << "DoD3D9GPUHook: Could not create D3D10.1 device, result = " << (UINT)hErr << endl;
adapter->Release();
factory->Release();
goto finishGPUHook;
}
}

adapter->Release();
factory->Release();

//------------------------------------------------

D3D11_TEXTURE2D_DESC texGameDesc;
D3D10_TEXTURE2D_DESC texGameDesc;
ZeroMemory(&texGameDesc, sizeof(texGameDesc));
texGameDesc.Width = d3d9CaptureInfo.cx;
texGameDesc.Height = d3d9CaptureInfo.cy;
texGameDesc.MipLevels = 1;
texGameDesc.ArraySize = 1;
texGameDesc.Format = dxgiFormat;
texGameDesc.SampleDesc.Count = 1;
texGameDesc.BindFlags = D3D11_BIND_RENDER_TARGET|D3D11_BIND_SHADER_RESOURCE;
texGameDesc.Usage = D3D11_USAGE_DEFAULT;
texGameDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED;
texGameDesc.BindFlags = D3D10_BIND_RENDER_TARGET|D3D10_BIND_SHADER_RESOURCE;
texGameDesc.Usage = D3D10_USAGE_DEFAULT;
texGameDesc.MiscFlags = D3D10_RESOURCE_MISC_SHARED;

ID3D11Texture2D *d3d11Tex;
if(FAILED(hErr = shareDevice->CreateTexture2D(&texGameDesc, NULL, &d3d11Tex)))
ID3D10Texture2D *d3d101Tex;
if(FAILED(hErr = shareDevice->CreateTexture2D(&texGameDesc, NULL, &d3d101Tex)))
{
RUNEVERYRESET logOutput << CurrentTimeString() << "DoD3D9GPUHook: shareDevice->CreateTexture2D failed, result = " << (UINT)hErr << endl;
goto finishGPUHook;
}

if(FAILED(hErr = d3d11Tex->QueryInterface(__uuidof(ID3D11Resource), (void**)&copyTextureIntermediary)))
if(FAILED(hErr = d3d101Tex->QueryInterface(__uuidof(ID3D10Resource), (void**)&copyTextureIntermediary)))
{
RUNEVERYRESET logOutput << CurrentTimeString() << "DoD3D9GPUHook: d3d11Tex->QueryInterface(ID3D11Resource) failed, result = " << (UINT)hErr << endl;
d3d11Tex->Release();
RUNEVERYRESET logOutput << CurrentTimeString() << "DoD3D9GPUHook: d3d101Tex->QueryInterface(ID3D10Resource) failed, result = " << (UINT)hErr << endl;
d3d101Tex->Release();
goto finishGPUHook;
}

IDXGIResource *res;
if(FAILED(hErr = d3d11Tex->QueryInterface(IID_IDXGIResource, (void**)&res)))
if(FAILED(hErr = d3d101Tex->QueryInterface(IID_IDXGIResource, (void**)&res)))
{
RUNEVERYRESET logOutput << CurrentTimeString() << "DoD3D9GPUHook: d3d11Tex->QueryInterface(IDXGIResource) failed, result = " << (UINT)hErr << endl;
d3d11Tex->Release();
RUNEVERYRESET logOutput << CurrentTimeString() << "DoD3D9GPUHook: d3d101Tex->QueryInterface(IDXGIResource) failed, result = " << (UINT)hErr << endl;
d3d101Tex->Release();
goto finishGPUHook;
}

if(FAILED(res->GetSharedHandle(&sharedHandle)))
{
RUNEVERYRESET logOutput << CurrentTimeString() << "DoD3D9GPUHook: res->GetSharedHandle failed, result = " << (UINT)hErr << endl;
d3d11Tex->Release();
d3d101Tex->Release();
res->Release();
goto finishGPUHook;
}

d3d11Tex->Release();
d3d101Tex->Release();
res->Release();
res = NULL;

Expand Down
74 changes: 33 additions & 41 deletions GraphicsCapture/GraphicsCaptureHook/OpenGLCapture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#ifdef USE_D3D9_GL_INTEROP
#include <D3D9.h>
#else
#include <D3D11.h>
#include <D3D10_1.h>
#endif


Expand Down Expand Up @@ -228,9 +228,8 @@ static IDirect3DDevice9Ex *d3d9exDevice = NULL;
static IDirect3DTexture9 *d3d9exTexture = NULL;
extern bool bD3D9Hooked;
#else
extern ID3D11Device *shareDevice;
extern ID3D11DeviceContext *shareContext;
extern ID3D11Resource *copyTextureIntermediary;
extern ID3D10Device1 *shareDevice;
extern ID3D10Resource *copyTextureIntermediary;
IDXGISwapChain *swapChain = NULL;
extern bool bDXGIHooked;
#endif
Expand Down Expand Up @@ -504,18 +503,8 @@ static bool DoGLGPUHook(RECT &rc)

typedef HRESULT (WINAPI *CREATEDXGIFACTORY1PROC)(REFIID riid, void **ppFactory);

const static D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
D3D_FEATURE_LEVEL_9_3,
};

static bool DoGLGPUHook(RECT &rc)
{
D3D_FEATURE_LEVEL level;

bUseSharedTextures = true;
glcaptureInfo.cx = rc.right;
glcaptureInfo.cy = rc.bottom;
Expand All @@ -537,10 +526,10 @@ static bool DoGLGPUHook(RECT &rc)
swapDesc.Windowed = TRUE;
swapDesc.OutputWindow = hwndD3DDummyWindow;

HMODULE hD3D11 = LoadLibrary(TEXT("d3d11.dll"));
if(!hD3D11)
HMODULE hD3D10_1 = LoadLibrary(TEXT("d3d10_1.dll"));
if(!hD3D10_1)
{
RUNEVERYRESET logOutput << CurrentTimeString() << "DoGLGPUHook: Could not load D3D11" << endl;
RUNEVERYRESET logOutput << CurrentTimeString() << "DoGLGPUHook: Could not load D3D10.1" << endl;
goto finishGPUHook;
}

Expand All @@ -558,10 +547,10 @@ static bool DoGLGPUHook(RECT &rc)
goto finishGPUHook;
}

PFN_D3D11_CREATE_DEVICE_AND_SWAP_CHAIN d3d11CreateDeviceAndSwapChain = (PFN_D3D11_CREATE_DEVICE_AND_SWAP_CHAIN)GetProcAddress(hD3D11, "D3D11CreateDeviceAndSwapChain");
if(!d3d11CreateDeviceAndSwapChain)
PFN_D3D10_CREATE_DEVICE_AND_SWAP_CHAIN1 d3d10CreateDeviceAndSwapChain1 = (PFN_D3D10_CREATE_DEVICE_AND_SWAP_CHAIN1)GetProcAddress(hD3D10_1, "D3D10CreateDeviceAndSwapChain1");
if(!d3d10CreateDeviceAndSwapChain1)
{
RUNEVERYRESET logOutput << CurrentTimeString() << "DoGLGPUHook: Could not load 'D3D11CreateDeviceAndSwapChain'" << endl;
RUNEVERYRESET logOutput << CurrentTimeString() << "DoGLGPUHook: Could not load 'D3D10CreateDeviceAndSwapChain1'" << endl;
goto finishGPUHook;
}

Expand All @@ -580,57 +569,60 @@ static bool DoGLGPUHook(RECT &rc)
goto finishGPUHook;
}

if(FAILED(hErr = (*d3d11CreateDeviceAndSwapChain)(adapter, D3D_DRIVER_TYPE_UNKNOWN, NULL, 0, featureLevels, sizeof(featureLevels) / sizeof(featureLevels[0]), D3D11_SDK_VERSION, &swapDesc, &swapChain, &shareDevice, &level, &shareContext)))
if(FAILED(hErr = (*d3d10CreateDeviceAndSwapChain1)(adapter, D3D10_DRIVER_TYPE_HARDWARE, NULL, 0, D3D10_FEATURE_LEVEL_10_1, D3D10_1_SDK_VERSION, &swapDesc, &swapChain, &shareDevice)))
{
RUNEVERYRESET logOutput << CurrentTimeString() << "DoGLGPUHook: Could not create D3D10.1 device, result = " << (UINT)hErr << endl;
adapter->Release();
factory->Release();
goto finishGPUHook;
if(FAILED(hErr = (*d3d10CreateDeviceAndSwapChain1)(adapter, D3D10_DRIVER_TYPE_HARDWARE, NULL, 0, D3D10_FEATURE_LEVEL_9_3, D3D10_1_SDK_VERSION, &swapDesc, &swapChain, &shareDevice)))
{
RUNEVERYRESET logOutput << CurrentTimeString() << "DoGLGPUHook: Could not create D3D10.1 device, result = " << (UINT)hErr << endl;
adapter->Release();
factory->Release();
goto finishGPUHook;
}
}

adapter->Release();
factory->Release();

//------------------------------------------------

D3D11_TEXTURE2D_DESC texGameDesc;
D3D10_TEXTURE2D_DESC texGameDesc;
ZeroMemory(&texGameDesc, sizeof(texGameDesc));
texGameDesc.Width = glcaptureInfo.cx;
texGameDesc.Height = glcaptureInfo.cy;
texGameDesc.MipLevels = 1;
texGameDesc.ArraySize = 1;
texGameDesc.Format = DXGI_FORMAT_B8G8R8X8_UNORM;
texGameDesc.SampleDesc.Count = 1;
texGameDesc.BindFlags = D3D11_BIND_RENDER_TARGET|D3D11_BIND_SHADER_RESOURCE;
texGameDesc.Usage = D3D11_USAGE_DEFAULT;
texGameDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED;
texGameDesc.BindFlags = D3D10_BIND_RENDER_TARGET|D3D10_BIND_SHADER_RESOURCE;
texGameDesc.Usage = D3D10_USAGE_DEFAULT;
texGameDesc.MiscFlags = D3D10_RESOURCE_MISC_SHARED;

ID3D11Texture2D *d3d11Tex;
if(FAILED(hErr = shareDevice->CreateTexture2D(&texGameDesc, NULL, &d3d11Tex)))
ID3D10Texture2D *d3d101Tex;
if(FAILED(hErr = shareDevice->CreateTexture2D(&texGameDesc, NULL, &d3d101Tex)))
{
RUNEVERYRESET logOutput << CurrentTimeString() << "DoGLGPUHook: shareDevice->CreateTexture2D failed, result = " << (UINT)hErr << endl;
goto finishGPUHook;
}

if(FAILED(hErr = d3d11Tex->QueryInterface(__uuidof(ID3D10Resource), (void**)&copyTextureIntermediary)))
if(FAILED(hErr = d3d101Tex->QueryInterface(__uuidof(ID3D10Resource), (void**)&copyTextureIntermediary)))
{
RUNEVERYRESET logOutput << CurrentTimeString() << "DoGLGPUHook: d3d11Tex->QueryInterface(ID3D10Resource) failed, result = " << (UINT)hErr << endl;
d3d11Tex->Release();
RUNEVERYRESET logOutput << CurrentTimeString() << "DoGLGPUHook: d3d101Tex->QueryInterface(ID3D10Resource) failed, result = " << (UINT)hErr << endl;
d3d101Tex->Release();
goto finishGPUHook;
}

IDXGIResource *res;
if(FAILED(hErr = d3d11Tex->QueryInterface(IID_IDXGIResource, (void**)&res)))
if(FAILED(hErr = d3d101Tex->QueryInterface(IID_IDXGIResource, (void**)&res)))
{
RUNEVERYRESET logOutput << CurrentTimeString() << "DoGLGPUHook: d3d11Tex->QueryInterface(IDXGIResource) failed, result = " << (UINT)hErr << endl;
d3d11Tex->Release();
RUNEVERYRESET logOutput << CurrentTimeString() << "DoGLGPUHook: d3d101Tex->QueryInterface(IDXGIResource) failed, result = " << (UINT)hErr << endl;
d3d101Tex->Release();
goto finishGPUHook;
}

if(FAILED(res->GetSharedHandle(&sharedHandle)))
{
RUNEVERYRESET logOutput << CurrentTimeString() << "DoGLGPUHook: res->GetSharedHandle failed, result = " << (UINT)hErr << endl;
d3d11Tex->Release();
d3d101Tex->Release();
res->Release();
goto finishGPUHook;
}
Expand All @@ -639,7 +631,7 @@ static bool DoGLGPUHook(RECT &rc)
gl_dxDevice = wglDXOpenDeviceNV(shareDevice);
if (gl_dxDevice == NULL) {
RUNEVERYRESET logOutput << CurrentTimeString() << "DoGLGPUHook: wglDXOpenDeviceNV failed" << endl;
d3d11Tex->Release();
d3d101Tex->Release();
res->Release();
goto finishGPUHook;
}
Expand All @@ -649,7 +641,7 @@ static bool DoGLGPUHook(RECT &rc)

if (gl_handle == NULL) {
RUNEVERYRESET logOutput << CurrentTimeString() << "DoGLGPUHook: wglDXRegisterObjectNV failed" << endl;
d3d11Tex->Release();
d3d101Tex->Release();
res->Release();
goto finishGPUHook;
}
Expand All @@ -662,7 +654,7 @@ static bool DoGLGPUHook(RECT &rc)

glGenFramebuffers(1, &gl_fbo);

d3d11Tex->Release();
d3d101Tex->Release();
res->Release();
res = NULL;

Expand Down

0 comments on commit ed58b5b

Please sign in to comment.