[D3D12] Trying to fix resizebuffers

This commit is contained in:
FlightlessMango 2020-07-27 23:06:05 +02:00
parent 2273cd30a4
commit 8e67b23804
3 changed files with 75 additions and 29 deletions

View File

@ -12,18 +12,26 @@
#include "imgui_impl_win32.h"
#include "imgui_impl_dx12.h"
#include "dx_shared.h"
#include <winuser.h>
long(__stdcall* oPresent)(IDXGISwapChain3*, UINT, UINT) = nullptr;
void(*oExecuteCommandListsD3D12)(ID3D12CommandQueue*, UINT, ID3D12CommandList*) = nullptr;
void(*oExecuteCommandListsD3D12)(ID3D12CommandQueue*, UINT, ID3D12CommandList*);
HRESULT(*oSignalD3D12)(ID3D12CommandQueue*, ID3D12Fence*, UINT64) = nullptr;
PresentD3D12 oPresentD3D12;
tResizeBuffers oResizeBuffers;
ID3D12Device* d3d12Device = nullptr;
IDXGISwapChain* pSwapChain_;
ID3D12DescriptorHeap* d3d12DescriptorHeapBackBuffers = nullptr;
ID3D12DescriptorHeap* d3d12DescriptorHeapImGuiRender = nullptr;
ID3D12GraphicsCommandList* d3d12CommandList = nullptr;
ID3D12Fence* d3d12Fence = nullptr;
UINT64 d3d12FenceValue = 0;
UINT64 d3d12FenceValue = 0;
ID3D12CommandQueue* d3d12CommandQueue = nullptr;
using namespace Microsoft::WRL;
Microsoft::WRL::ComPtr<ID3D12CommandQueue> queue;
Microsoft::WRL::ComPtr<IDXGISwapChain3> swapchain_;
bool initDX12 = false;
struct FrameContext {
ID3D12CommandAllocator* commandAllocator = nullptr;
ID3D12Resource* main_render_target_resource = nullptr;
@ -33,14 +41,13 @@ struct FrameContext {
uint32_t buffersCounts = -1;
FrameContext* frameContext;
bool DX12Init(IDXGISwapChain3* pSwapChain)
bool DX12Init(IDXGISwapChain* pSwapChain)
{
ID3D12Device* d3d12Device = nullptr;
if (SUCCEEDED(pSwapChain->GetDevice(__uuidof(ID3D12Device), (void**)&d3d12Device))) {
pSwapChain_ = pSwapChain;
imgui_create(pSwapChain, d3d12Device);
ImGui::SetCurrentContext(state.imgui_ctx);
CreateEvent(nullptr, false, false, nullptr);
DXGI_SWAP_CHAIN_DESC sdesc;
@ -53,20 +60,18 @@ bool DX12Init(IDXGISwapChain3* pSwapChain)
descriptorImGuiRender.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
descriptorImGuiRender.NumDescriptors = buffersCounts;
descriptorImGuiRender.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
if (d3d12Device->CreateDescriptorHeap(&descriptorImGuiRender, IID_PPV_ARGS(&d3d12DescriptorHeapImGuiRender)) != S_OK)
if (d3d12Device->CreateDescriptorHeap(&descriptorImGuiRender, __uuidof(ID3D12DescriptorHeap), (void**)&d3d12DescriptorHeapImGuiRender))
return false;
ID3D12CommandAllocator* allocator;
if (d3d12Device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator)) != S_OK)
return false;
if (d3d12Device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, __uuidof(ID3D12CommandAllocator), (void**)&allocator) < 0)
return false;
for (size_t i = 0; i < buffersCounts; i++) {
frameContext[i].commandAllocator = allocator;
}
if (d3d12Device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator, NULL, IID_PPV_ARGS(&d3d12CommandList)) != S_OK ||
d3d12CommandList->Close() != S_OK)
if (d3d12Device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator, NULL, __uuidof(ID3D12GraphicsCommandList), (void**)&d3d12CommandList) < 0)
return false;
D3D12_DESCRIPTOR_HEAP_DESC descriptorBackBuffers;
@ -75,7 +80,7 @@ bool DX12Init(IDXGISwapChain3* pSwapChain)
descriptorBackBuffers.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
descriptorBackBuffers.NodeMask = 1;
if (d3d12Device->CreateDescriptorHeap(&descriptorBackBuffers, IID_PPV_ARGS(&d3d12DescriptorHeapBackBuffers)) != S_OK)
if (d3d12Device->CreateDescriptorHeap(&descriptorBackBuffers, __uuidof(ID3D12DescriptorHeap), (void**)&d3d12DescriptorHeapBackBuffers) != S_OK)
return false;
const auto rtvDescriptorSize = d3d12Device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
@ -90,7 +95,7 @@ bool DX12Init(IDXGISwapChain3* pSwapChain)
frameContext[i].main_render_target_resource = pBackBuffer;
rtvHandle.ptr += rtvDescriptorSize;
}
auto mainWindow = sdesc.OutputWindow;
ImGui_ImplWin32_Init(sdesc.OutputWindow);
ImGui_ImplDX12_Init(d3d12Device, buffersCounts,
DXGI_FORMAT_R8G8B8A8_UNORM, d3d12DescriptorHeapImGuiRender,
@ -104,17 +109,15 @@ bool DX12Init(IDXGISwapChain3* pSwapChain)
long __fastcall hkPresent12(IDXGISwapChain3* pSwapChain, UINT SyncInterval, UINT Flags)
{
static bool init = false;
auto prev_ctx = ImGui::GetCurrentContext();
if (!init)
if (!initDX12)
{
DX12Init(pSwapChain);
init = true;
initDX12 = true;
}
ImGui::SetCurrentContext(state.imgui_ctx);
check_keybinds(params);
update_hud_info(sw_stats, params, vendorID);
ImGui_ImplDX12_NewFrame();
@ -123,9 +126,8 @@ long __fastcall hkPresent12(IDXGISwapChain3* pSwapChain, UINT SyncInterval, UINT
position_layer(sw_stats, params, window_size);
render_imgui(sw_stats, params, window_size, "D3D12");
ImGui::EndFrame();
FrameContext& currentFrameContext = frameContext[pSwapChain->GetCurrentBackBufferIndex()];
currentFrameContext.commandAllocator->Reset();
//currentFrameContext.commandAllocator->Reset();
D3D12_RESOURCE_BARRIER barrier;
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
@ -139,7 +141,7 @@ long __fastcall hkPresent12(IDXGISwapChain3* pSwapChain, UINT SyncInterval, UINT
d3d12CommandList->ResourceBarrier(1, &barrier);
d3d12CommandList->OMSetRenderTargets(1, &currentFrameContext.main_render_target_descriptor, FALSE, nullptr);
d3d12CommandList->SetDescriptorHeaps(1, &d3d12DescriptorHeapImGuiRender);
// printf("imgui/d3d12 render\n");
ImGui::Render();
ImGui_ImplDX12_RenderDrawData(ImGui::GetDrawData(), d3d12CommandList);
@ -149,10 +151,9 @@ long __fastcall hkPresent12(IDXGISwapChain3* pSwapChain, UINT SyncInterval, UINT
d3d12CommandList->ResourceBarrier(1, &barrier);
d3d12CommandList->Close();
d3d12CommandQueue->ExecuteCommandLists(1, reinterpret_cast<ID3D12CommandList* const*>(&d3d12CommandList));
ImGui::SetCurrentContext(prev_ctx);
d3d12CommandQueue->ExecuteCommandLists(1, (ID3D12CommandList* const*)&d3d12CommandList);
//ImGui::SetCurrentContext(prev_ctx);
return oPresent(pSwapChain, SyncInterval, Flags);
}
@ -172,14 +173,41 @@ HRESULT hookSignalD3D12(ID3D12CommandQueue* queue, ID3D12Fence* fence, UINT64 va
return oSignalD3D12(queue, fence, value);
}
HRESULT __stdcall hkResizeBuffers(IDXGISwapChain* pThis, UINT BufferCount, UINT Width, UINT Height, DXGI_FORMAT NewFormat, UINT SwapChainFlags)
{
//if (pSwapChain_) { pSwapChain_->Release(); pSwapChain_ = nullptr; }
if (d3d12CommandQueue) { d3d12CommandQueue->Release(); d3d12CommandQueue = nullptr; }
if (d3d12CommandList) { d3d12CommandList->Release(); d3d12CommandList = nullptr; }
if (d3d12Fence) { d3d12Fence->Release(); d3d12Fence = nullptr; }
if (d3d12DescriptorHeapBackBuffers) { d3d12DescriptorHeapBackBuffers->Release(); d3d12DescriptorHeapBackBuffers = nullptr; }
if (d3d12DescriptorHeapImGuiRender) { d3d12DescriptorHeapImGuiRender->Release(); d3d12DescriptorHeapImGuiRender = nullptr; }
for (uint32_t i = 0; i < sizeof(FrameContext); ++i)
{
frameContext[i] = {};
}
//if (d3d12Device) { d3d12Device->Release(); d3d12Device = nullptr; }
ImGui_ImplDX12_Shutdown();
initDX12 = false;
auto hr = oResizeBuffers(pThis, BufferCount, Width, Height, NewFormat, SwapChainFlags);
if (hr != S_OK)
printf("ResizeBuffers Failed\n");
pSwapChain_ = pThis;
d3d12CommandQueue = queue.Get();
pSwapChain_->GetDevice(__uuidof(ID3D12Device), (void**)&d3d12Device);
return hr;
}
void impl::d3d12::init()
{
auto ret = kiero::bind(54, (void**)&oExecuteCommandListsD3D12, reinterpret_cast<void*>(hookExecuteCommandListsD3D12));
printf("init dx12\n");
auto ret = kiero::bind(54, (void**)&oExecuteCommandListsD3D12, hookExecuteCommandListsD3D12);
assert(ret == kiero::Status::Success);
ret = kiero::bind(58, (void**)&oSignalD3D12, reinterpret_cast<void*>(hookSignalD3D12));
assert(ret == kiero::Status::Success);
ret = kiero::bind(140, (void**)&oPresent, reinterpret_cast<void *>(hkPresent12));
ret = kiero::bind(140, (void**)&oPresent, hkPresent12);
assert(ret == kiero::Status::Success);
ret = kiero::bind(145, (void**)&oResizeBuffers, hkResizeBuffers);
assert(ret == kiero::Status::Success);
}
#endif // KIERO_INCLUDE_D3D12
#endif // KIERO_INCLUDE_D3D12

View File

@ -1,3 +1,7 @@
#include <dxgi.h>
#include <dxgi1_5.h>
#include <d3d12.h>
#include <wrl.h>
#ifndef __D3D12_IMPL_H__
#define __D3D12_IMPL_H__
@ -9,4 +13,18 @@ namespace impl
}
}
#endif // __D3D12_IMPL_H__
#endif // __D3D12_IMPL_H__
typedef long(__fastcall* PresentD3D12) (IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT Flags);
extern PresentD3D12 oPresentD3D12;
extern void(*oExecuteCommandListsD3D12)(ID3D12CommandQueue*, UINT, ID3D12CommandList*);
typedef HRESULT(__stdcall* tResizeBuffers)(IDXGISwapChain* pThis, UINT BufferCount, UINT Width,
UINT Height, DXGI_FORMAT NewFormat, UINT SwapChainFlags);
extern tResizeBuffers oResizeBuffers;
extern tResizeBuffers oResizeBuffers;
typedef void(__fastcall* DrawInstancedD3D12)(ID3D12GraphicsCommandList* dCommandList, UINT VertexCountPerInstance, UINT InstanceCount, UINT StartVertexLocation, UINT StartInstanceLocation);
extern DrawInstancedD3D12 oDrawInstancedD3D12;
typedef void(__fastcall* DrawIndexedInstancedD3D12)(ID3D12GraphicsCommandList* dCommandList, UINT IndexCount, UINT InstanceCount, UINT StartIndex, INT BaseVertex);
extern DrawIndexedInstancedD3D12 oDrawIndexedInstancedD3D12;

View File

@ -8,7 +8,7 @@
#define KIERO_INCLUDE_D3D9 1 // 1 if you need D3D9 hook
#define KIERO_INCLUDE_D3D10 0 // 1 if you need D3D10 hook
#define KIERO_INCLUDE_D3D11 1 // 1 if you need D3D11 hook
#define KIERO_INCLUDE_D3D12 0 // 1 if you need D3D12 hook
#define KIERO_INCLUDE_D3D12 1 // 1 if you need D3D12 hook
#define KIERO_INCLUDE_OPENGL 0 // 1 if you need OpenGL hook
#define KIERO_INCLUDE_VULKAN 0 // 1 if you need Vulkan hook
#define KIERO_USE_MINHOOK 1 // 1 if you will use kiero::bind function