0

我正在尝试挂钩 DirectX 游戏。我成功加载了我的钩子,并且可以使用以下方法将图像/后备缓冲区保存到磁盘:

HRESULT Capture(IDirect3DDevice9* Device, const char* FilePath)
{
    IDirect3DSurface9* RenderTarget = nullptr;
    HRESULT result = Device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &RenderTarget);
    //for some reason result is never S_OK but this still works.
    result = D3DXSaveSurfaceToFile(FilePath, D3DXIFF_PNG, RenderTarget, nullptr, nullptr);
    SafeRelease(RenderTarget);
    return result;
}

它成功保存,我很高兴。但是,我想保存到像素阵列而不是磁盘。我尝试使用:

#include <memory>

std::unique_ptr<std::uint8_t[]> mem(new std::uint8_t[100 * 100 * 4]);

HRESULT Direct3DDevice9Proxy::EndScene()
{
    IDirect3DSurface9* RenderTarget = nullptr;
    HRESULT result = ptr_Direct3DDevice9->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &RenderTarget);

    D3DLOCKED_RECT LR;
    RenderTarget->LockRect(&LR, nullptr, D3DLOCK_NOSYSLOCK | D3DLOCK_READONLY);
    memcpy(mem.get(), LR.pBits, LR.Pitch - 1);
    RenderTarget->UnlockRect();

    SafeRelease(RenderTarget);
    return ptr_Direct3DDevice9->EndScene();
}

但是,它永远不会锁定,如果我尝试访问 LR.pBits,它会引发访问冲突。我不知道为什么它不会锁定。有没有另一种方法可以将后缓冲区中的像素抓取到字节数组中?游戏的最大视口为 1366x768。

编辑:我试过:

void dump_buffer(LPDIRECT3DDEVICE9 Device, std::unique_ptr<std::uint8_t[]> &bits)
{
    IDirect3DSurface9* RenderTarget = nullptr;
    IDirect3DSurface9* DestTarget = nullptr;
    D3DSURFACE_DESC rtDesc = {};

    Device->GetRenderTarget(0, &RenderTarget);
    RenderTarget->GetDesc(&rtDesc);

    Device->CreateOffscreenPlainSurface(rtDesc.Width, rtDesc.Height, rtDesc.Format, D3DPOOL_DEFAULT, &DestTarget, nullptr);
    Device->GetRenderTargetData(RenderTarget, DestTarget);

    if(DestTarget != nullptr)
    {
        D3DLOCKED_RECT rect;
        DestTarget->LockRect(&rect, 0, D3DLOCK_READONLY);    
        memcpy(bits.get(), rect.pBits, rtDesc.Width * rtDesc.Height * 4);

        std::uint8_t* ptr = &bits[0];
        CG::Image(ptr, rtDesc.Width, rtDesc.Height).Save("Foo.bmp");

        DestTarget->UnlockRect();
        DestTarget->Release();
    }

    RenderTarget->Release();
}

HRESULT Direct3DDevice9Proxy::EndScene()
{
    dump_buffer(ptr_Direct3DDevice9, mem);
    return ptr_Direct3DDevice9->EndScene();
}

但我的图像是黑色的。有任何想法吗?

4

2 回答 2

1

有点晚了。但希望这对某人有所帮助。

创造

        if (SUCCEEDED(hr = _device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuf)))
        {
            D3DSURFACE_DESC sd;
            backBuf->GetDesc(&sd);

            if (SUCCEEDED(hr = _device->CreateOffscreenPlainSurface(sd.Width, sd.Height, sd.Format, D3DPOOL_SYSTEMMEM, &_surface, NULL)))

捕获

    if (SUCCEEDED(hr = _device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuf)))
    {
        if (SUCCEEDED(hr = _device->GetRenderTargetData(backBuf, _surface)))
        {
            if (SUCCEEDED(hr = D3DXSaveSurfaceToFileInMemory(&buf, D3DXIFF_DIB, _surface, NULL, NULL)))
            {
                LPVOID imgBuf = buf->GetBufferPointer();
                DWORD length = buf->GetBufferSize();

在 buf 中,您将拥有原始图像数据。不要忘记释放一切。

于 2016-03-04T00:42:20.353 回答
0

请注意,EndScene 将要渲染的场景排队 - 您不能保证在此之前(或什至,之后立即)已经渲染了任何内容。

IDirect3DDevice9::GetFrontBufferData可能对您更有用。它“生成设备前端缓冲区的副本,并将该副本放置在应用程序提供的系统内存缓冲区中。”

您要做的是调用 CreateOffscreenPlainSurface 在系统内存中创建 pSurface,然后调用 GetFrontBufferData(0, pSurface)。

于 2013-12-19T02:37:14.100 回答