Skip to content

Commit

Permalink
Obtain the command queue directly in game memory
Browse files Browse the repository at this point in the history
  • Loading branch information
Yamashi committed Feb 10, 2022
1 parent 481f972 commit 707c8ed
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 40 deletions.
35 changes: 1 addition & 34 deletions src/d3d12/D3D12_Functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,6 @@ bool D3D12::ResetState(bool aClearDownlevelBackbuffers)

bool D3D12::Initialize(IDXGISwapChain* apSwapChain)
{
static auto checkCmdQueue = [](D3D12* d3d12)
{
if (d3d12->m_pCommandQueue == nullptr)
{
auto swapChainAddr = reinterpret_cast<uintptr_t>(*(&d3d12->m_pdxgiSwapChain));
d3d12->m_pCommandQueue = *reinterpret_cast<ID3D12CommandQueue**>(swapChainAddr + kiero::getCommandQueueOffset());
if (d3d12->m_pCommandQueue != nullptr)
{
auto desc = d3d12->m_pCommandQueue->GetDesc();
if(desc.Type != D3D12_COMMAND_LIST_TYPE_DIRECT)
{
d3d12->m_pCommandQueue = nullptr;
Log::Warn("D3D12::Initialize() - invalid type of command list!");
return false;
}
return true;
}
Log::Warn("D3D12::Initialize() - swap chain is missing command queue!");
return false;
}
return true;
};

if (!apSwapChain)
return false;

Expand Down Expand Up @@ -89,11 +66,7 @@ bool D3D12::Initialize(IDXGISwapChain* apSwapChain)
if (hWnd != sdesc.OutputWindow)
Log::Warn("D3D12::Initialize() - output window of current swap chain does not match hooked window! Currently hooked to {0} while swap chain output window is {1}.", reinterpret_cast<void*>(hWnd), reinterpret_cast<void*>(sdesc.OutputWindow));
}
if (!checkCmdQueue(this))
{
Log::Error("D3D12::Initialize() - missing command queue!");
return false;
}

return true;
}

Expand Down Expand Up @@ -168,12 +141,6 @@ bool D3D12::Initialize(IDXGISwapChain* apSwapChain)
for (auto& context : m_frameContexts)
m_pd3d12Device->CreateRenderTargetView(context.BackBuffer, nullptr, context.MainRenderTargetDescriptor);

if (!checkCmdQueue(this))
{
Log::Error("D3D12::Initialize() - missing command queue!");
return false;
}

if (!InitializeImGui(buffersCounts))
{
Log::Error("D3D12::Initialize() - failed to initialize ImGui!");
Expand Down
13 changes: 7 additions & 6 deletions src/d3d12/D3D12_Hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,22 @@ HRESULT D3D12::CreateCommittedResource(ID3D12Device* apDevice, const D3D12_HEAP_
return result;
}

void D3D12::ExecuteCommandLists(ID3D12CommandQueue* apCommandQueue, UINT aNumCommandLists, ID3D12CommandList* const* apcpCommandLists)
void D3D12::ExecuteCommandLists(ID3D12CommandQueue* apCommandQueue, UINT aNumCommandLists,
ID3D12CommandList* const* apcpCommandLists)
{
auto& d3d12 = CET::Get().GetD3D12();

if (d3d12.m_pCommandQueue == nullptr)
{
auto desc = apCommandQueue->GetDesc();
if(desc.Type == D3D12_COMMAND_LIST_TYPE_DIRECT)
if (desc.Type == D3D12_COMMAND_LIST_TYPE_DIRECT)
{
auto ret = (uintptr_t)_ReturnAddress() - (uintptr_t)GetModuleHandleA(nullptr);

d3d12.m_pCommandQueue = apCommandQueue;
Log::Info("D3D12::ExecuteCommandListsD3D12() - found valid command queue. {:X}", ret);
}
else
else
Log::Info("D3D12::ExecuteCommandListsD3D12() - ignoring command queue - unusable command list type");
}

d3d12.m_realExecuteCommandLists(apCommandQueue, aNumCommandLists, apcpCommandLists);
}

Expand Down Expand Up @@ -143,9 +141,12 @@ void* D3D12::CRenderNode_Present_InternalPresent(int32_t* apSomeInt, uint8_t aSo
void** vtbl = *reinterpret_cast<void***>(pDevice);
d3d12.m_realResizeBuffersD3D12 =
static_cast<TResizeBuffersD3D12*>(ApplyHook(vtbl, 13, &D3D12::ResizeBuffers));

Log::Info("D3D12: Applied ResizeBuffers vtable hook");
});

d3d12.m_pCommandQueue = pContext->pDirectQueue;

if (d3d12.Initialize(pDevice))
d3d12.Update();
}
Expand Down
3 changes: 3 additions & 0 deletions src/reverse/RenderContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ struct RenderContext
Device devices[0x30]; // Count unknown, it is at least 0x20
uint8_t pad[0xED65C0 - 0xC9A1B8];
void* unkED65C0; // Some pointer, might be related to d3d12on7, I have never seen it being anything other than null
uint8_t padED65C8[0x10];
ID3D12CommandQueue* pDirectQueue;
};

static_assert(sizeof(RenderContext::Device) == 0xB8);
static_assert(offsetof(RenderContext, devices) == 0xC97F38);
static_assert(offsetof(RenderContext, unkED65C0) == 0xED65C0);
static_assert(offsetof(RenderContext, pDirectQueue) == 0xED65D8);

0 comments on commit 707c8ed

Please sign in to comment.