2022-08-05 02:06:42 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) Nemirtingas
|
|
|
|
* This file is part of the ingame overlay project
|
|
|
|
*
|
|
|
|
* The ingame overlay project is free software; you can redistribute it
|
|
|
|
* and/or modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* The ingame overlay project is distributed in the hope that it will be
|
|
|
|
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with the ingame overlay project; if not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2024-02-08 02:26:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// read this:
|
|
|
|
//https://github.com/ocornut/imgui/wiki/Image-Loading-and-Displaying-Examples#example-for-directx12-users
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-07-25 23:21:03 +02:00
|
|
|
#include "DX12_Hook.h"
|
2019-08-18 16:22:07 +02:00
|
|
|
#include "Windows_Hook.h"
|
2019-07-25 23:21:03 +02:00
|
|
|
|
|
|
|
#include <imgui.h>
|
2022-08-05 02:06:42 -04:00
|
|
|
#include <backends/imgui_impl_dx12.h>
|
2019-08-19 18:56:15 +02:00
|
|
|
|
2019-08-16 19:21:30 +02:00
|
|
|
DX12_Hook* DX12_Hook::_inst = nullptr;
|
|
|
|
|
2022-08-05 02:06:42 -04:00
|
|
|
template<typename T>
|
|
|
|
inline void SafeRelease(T*& pUnk)
|
|
|
|
{
|
|
|
|
if (pUnk != nullptr)
|
|
|
|
{
|
|
|
|
pUnk->Release();
|
|
|
|
pUnk = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-13 14:32:43 -04:00
|
|
|
bool DX12_Hook::StartHook(std::function<bool(bool)> key_combination_callback, std::set<ingame_overlay::ToggleKey> toggle_keys)
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2022-08-05 02:06:42 -04:00
|
|
|
if (!_Hooked)
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2022-08-05 02:06:42 -04:00
|
|
|
if (Present == nullptr || ResizeTarget == nullptr || ResizeBuffers == nullptr || ExecuteCommandLists == nullptr)
|
|
|
|
{
|
|
|
|
SPDLOG_WARN("Failed to hook DirectX 12: Rendering functions missing.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-08-13 14:32:43 -04:00
|
|
|
if (!Windows_Hook::Inst()->StartHook(key_combination_callback, toggle_keys))
|
2019-08-19 18:56:15 +02:00
|
|
|
return false;
|
|
|
|
|
2022-08-05 02:06:42 -04:00
|
|
|
_WindowsHooked = true;
|
2019-08-26 16:46:56 +02:00
|
|
|
|
2022-08-05 02:06:42 -04:00
|
|
|
SPDLOG_INFO("Hooked DirectX 12");
|
|
|
|
_Hooked = true;
|
2019-08-26 16:46:56 +02:00
|
|
|
|
|
|
|
BeginHook();
|
|
|
|
HookFuncs(
|
2022-08-05 02:06:42 -04:00
|
|
|
std::make_pair<void**, void*>(&(PVOID&)Present , &DX12_Hook::MyPresent),
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)ResizeTarget , &DX12_Hook::MyResizeTarget),
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)ResizeBuffers , &DX12_Hook::MyResizeBuffers),
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)ExecuteCommandLists, &DX12_Hook::MyExecuteCommandLists)
|
2019-08-26 16:46:56 +02:00
|
|
|
);
|
2022-08-05 02:06:42 -04:00
|
|
|
if (Present1 != nullptr)
|
|
|
|
{
|
|
|
|
HookFuncs(
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)Present1, &DX12_Hook::MyPresent1)
|
|
|
|
);
|
|
|
|
}
|
2019-08-26 16:46:56 +02:00
|
|
|
EndHook();
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
2022-08-05 02:06:42 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DX12_Hook::IsStarted()
|
|
|
|
{
|
|
|
|
return _Hooked;
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
2024-02-08 02:26:06 +02:00
|
|
|
DX12_Hook::heap_t DX12_Hook::get_free_texture_heap()
|
|
|
|
{
|
2024-02-08 03:02:12 +02:00
|
|
|
uint32_t i;
|
2024-02-08 02:26:06 +02:00
|
|
|
for (i = 0; i < srvDescHeapBitmap.size(); ++i) {
|
|
|
|
if (!srvDescHeapBitmap[i]) {
|
|
|
|
srvDescHeapBitmap[i] = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == srvDescHeapBitmap.size())
|
|
|
|
return heap_t{ {}, {}, -1 };
|
|
|
|
|
|
|
|
UINT inc = pDevice->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
|
|
|
|
|
|
|
|
return heap_t{
|
|
|
|
// i * 2 because each 2 handles are used per 1 image,
|
|
|
|
// 1st handle for ImGui internal font, 2nd for our texture
|
|
|
|
// then +1 because the 1st handle is reserved for internal font
|
|
|
|
pSrvDescHeap->GetGPUDescriptorHandleForHeapStart().ptr + inc * ((i * 2) + 1),
|
|
|
|
pSrvDescHeap->GetCPUDescriptorHandleForHeapStart().ptr + inc * ((i * 2) + 1),
|
|
|
|
i
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DX12_Hook::release_texture_heap(int64_t heap_id)
|
|
|
|
{
|
|
|
|
srvDescHeapBitmap[heap_id] = false;
|
|
|
|
return true;
|
|
|
|
}
|
2022-08-05 02:06:42 -04:00
|
|
|
|
|
|
|
ID3D12CommandQueue* DX12_Hook::_FindCommandQueueFromSwapChain(IDXGISwapChain* pSwapChain)
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2022-08-05 02:06:42 -04:00
|
|
|
ID3D12CommandQueue* pCommandQueue = nullptr;
|
|
|
|
|
|
|
|
if (CommandQueueOffset == 0 && pCmdQueue != nullptr)
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2022-08-05 02:06:42 -04:00
|
|
|
for (size_t i = 0; i < 1024; ++i)
|
2019-10-17 18:03:19 +02:00
|
|
|
{
|
2022-08-05 02:06:42 -04:00
|
|
|
if (*reinterpret_cast<ID3D12CommandQueue**>(reinterpret_cast<uintptr_t>(pSwapChain) + i) == pCmdQueue)
|
|
|
|
{
|
|
|
|
SPDLOG_INFO("Found IDXGISwapChain::ppCommandQueue at offset {}.", i);
|
|
|
|
CommandQueueOffset = i;
|
|
|
|
break;
|
|
|
|
}
|
2019-10-17 18:03:19 +02:00
|
|
|
}
|
2022-08-05 02:06:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (CommandQueueOffset != 0)
|
|
|
|
pCommandQueue = *reinterpret_cast<ID3D12CommandQueue**>(reinterpret_cast<uintptr_t>(pSwapChain) + CommandQueueOffset);
|
|
|
|
|
|
|
|
return pCommandQueue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DX12_Hook::_ResetRenderState()
|
|
|
|
{
|
|
|
|
if (_Initialized)
|
|
|
|
{
|
|
|
|
OverlayHookReady(false);
|
2019-07-25 23:21:03 +02:00
|
|
|
|
|
|
|
ImGui_ImplDX12_Shutdown();
|
2022-08-10 03:22:23 -04:00
|
|
|
Windows_Hook::Inst()->ResetRenderState();
|
2019-07-31 22:20:27 +02:00
|
|
|
ImGui::DestroyContext();
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2024-02-09 21:58:00 +02:00
|
|
|
_ResetStatesOnly();
|
2022-08-05 02:06:42 -04:00
|
|
|
|
|
|
|
_Initialized = false;
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-09 21:58:00 +02:00
|
|
|
void DX12_Hook::_ResetStatesOnly()
|
|
|
|
{
|
|
|
|
// each image texture holds a reference to its CPU and GPU descriptors
|
|
|
|
// when the state is reset we have to reload everything again
|
|
|
|
// I don't know a better way to handle this
|
|
|
|
while (_ImageResources.size()) {
|
|
|
|
_ImageResources.erase(std::prev(_ImageResources.end()));
|
|
|
|
}
|
|
|
|
|
|
|
|
srvDescHeapBitmap.clear();
|
|
|
|
OverlayFrames.clear();
|
|
|
|
SafeRelease(pCmdList);
|
|
|
|
SafeRelease(pSrvDescHeap);
|
|
|
|
SafeRelease(pRtvDescHeap);
|
|
|
|
SafeRelease(pDevice);
|
|
|
|
}
|
|
|
|
|
2024-02-08 02:26:06 +02:00
|
|
|
// https://github.com/ocornut/imgui/blob/06ce312745e0b25bfa8412b324503393964e3812/examples/example_win32_directx12/main.cpp#L237
|
2019-08-14 14:55:31 +02:00
|
|
|
// Try to make this function and overlay's proc as short as possible or it might affect game's fps.
|
2022-08-05 02:06:42 -04:00
|
|
|
void DX12_Hook::_PrepareForOverlay(IDXGISwapChain* pSwapChain, ID3D12CommandQueue* pCommandQueue)
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2024-02-09 21:58:00 +02:00
|
|
|
if (!pCommandQueue || !pSwapChain)
|
2019-10-15 08:41:07 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
IDXGISwapChain3* pSwapChain3 = nullptr;
|
|
|
|
pSwapChain->QueryInterface(IID_PPV_ARGS(&pSwapChain3));
|
|
|
|
if (pSwapChain3 == nullptr)
|
|
|
|
return;
|
|
|
|
|
2024-02-08 02:26:06 +02:00
|
|
|
DXGI_SWAP_CHAIN_DESC sc_desc{};
|
|
|
|
SecureZeroMemory(&sc_desc, sizeof(sc_desc));
|
2019-10-15 08:41:07 +02:00
|
|
|
pSwapChain3->GetDesc(&sc_desc);
|
2019-07-31 22:20:27 +02:00
|
|
|
|
2022-08-05 02:06:42 -04:00
|
|
|
if (!_Initialized)
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2024-02-09 05:06:08 +02:00
|
|
|
// UINT bufferIndex = pSwapChain3->GetCurrentBackBufferIndex();
|
2022-08-05 02:06:42 -04:00
|
|
|
pDevice = nullptr;
|
2024-02-09 10:32:33 +02:00
|
|
|
if (pSwapChain3->GetDevice(IID_PPV_ARGS(&pDevice)) != S_OK) {
|
2024-02-09 21:58:00 +02:00
|
|
|
_ResetStatesOnly();
|
2024-02-09 10:32:33 +02:00
|
|
|
pSwapChain3->Release();
|
2019-07-25 23:21:03 +02:00
|
|
|
return;
|
2024-02-09 10:32:33 +02:00
|
|
|
}
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2024-02-08 02:26:06 +02:00
|
|
|
// https://github.com/ocornut/imgui/wiki/Image-Loading-and-Displaying-Examples#example-for-directx12-users
|
2024-02-09 21:58:00 +02:00
|
|
|
srvDescHeapBitmap.clear();
|
|
|
|
constexpr const static UINT DESCRIPTOR_COUNT = 1024;
|
2019-10-17 18:03:19 +02:00
|
|
|
|
2019-08-19 18:56:15 +02:00
|
|
|
{
|
2019-08-26 16:46:56 +02:00
|
|
|
D3D12_DESCRIPTOR_HEAP_DESC desc = {};
|
|
|
|
desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
|
2024-02-08 02:26:06 +02:00
|
|
|
// desc.NumDescriptors = 1;
|
|
|
|
// <-- Set this value to 2 (the first descriptor is used for the built-in font texture, the second for our new texture)
|
2024-02-09 21:58:00 +02:00
|
|
|
desc.NumDescriptors = DESCRIPTOR_COUNT * 2;
|
2019-08-26 16:46:56 +02:00
|
|
|
desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
|
2024-02-09 21:58:00 +02:00
|
|
|
pSrvDescHeap = nullptr;
|
2019-08-26 16:46:56 +02:00
|
|
|
if (pDevice->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&pSrvDescHeap)) != S_OK)
|
|
|
|
{
|
2024-02-09 21:58:00 +02:00
|
|
|
_ResetStatesOnly();
|
2019-10-15 08:41:07 +02:00
|
|
|
pSwapChain3->Release();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2024-02-09 21:58:00 +02:00
|
|
|
srvDescHeapBitmap.resize(DESCRIPTOR_COUNT, false);
|
2022-08-05 02:06:42 -04:00
|
|
|
|
2024-02-09 21:58:00 +02:00
|
|
|
const UINT bufferCount = sc_desc.BufferCount;
|
2019-10-15 08:41:07 +02:00
|
|
|
{
|
|
|
|
D3D12_DESCRIPTOR_HEAP_DESC desc = {};
|
|
|
|
desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
|
|
|
|
desc.NumDescriptors = bufferCount;
|
|
|
|
desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
|
|
|
|
desc.NodeMask = 1;
|
2024-02-09 21:58:00 +02:00
|
|
|
pRtvDescHeap = nullptr;
|
2019-10-15 08:41:07 +02:00
|
|
|
if (pDevice->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&pRtvDescHeap)) != S_OK)
|
|
|
|
{
|
2024-02-09 21:58:00 +02:00
|
|
|
_ResetStatesOnly();
|
2019-10-15 08:41:07 +02:00
|
|
|
pSwapChain3->Release();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SIZE_T rtvDescriptorSize = pDevice->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
|
|
|
|
D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle = pRtvDescHeap->GetCPUDescriptorHandleForHeapStart();
|
2022-08-05 02:06:42 -04:00
|
|
|
|
2024-02-09 21:58:00 +02:00
|
|
|
OverlayFrames.clear();
|
|
|
|
pCmdList = nullptr;
|
2022-08-05 02:06:42 -04:00
|
|
|
for (UINT i = 0; i < bufferCount; ++i)
|
2019-10-15 08:41:07 +02:00
|
|
|
{
|
2024-02-08 02:26:06 +02:00
|
|
|
ID3D12CommandAllocator* pCmdAlloc = nullptr;
|
2022-08-05 02:06:42 -04:00
|
|
|
if (pDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&pCmdAlloc)) != S_OK || pCmdAlloc == nullptr)
|
2019-10-15 08:41:07 +02:00
|
|
|
{
|
2024-02-09 21:58:00 +02:00
|
|
|
_ResetStatesOnly();
|
|
|
|
SafeRelease(pSwapChain3);
|
2022-08-05 02:06:42 -04:00
|
|
|
return;
|
2019-10-15 08:41:07 +02:00
|
|
|
}
|
2019-08-19 18:56:15 +02:00
|
|
|
|
2022-08-05 02:06:42 -04:00
|
|
|
if (i == 0)
|
|
|
|
{
|
|
|
|
if (pDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, pCmdAlloc, NULL, IID_PPV_ARGS(&pCmdList)) != S_OK ||
|
2024-02-09 21:58:00 +02:00
|
|
|
pCmdList == nullptr ||
|
|
|
|
pCmdList->Close() != S_OK)
|
2022-08-05 02:06:42 -04:00
|
|
|
{
|
|
|
|
pCmdAlloc->Release();
|
2024-02-09 21:58:00 +02:00
|
|
|
_ResetStatesOnly();
|
2022-08-05 02:06:42 -04:00
|
|
|
pSwapChain3->Release();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-10-15 08:41:07 +02:00
|
|
|
|
2024-02-09 21:58:00 +02:00
|
|
|
ID3D12Resource* pBackBuffer = nullptr;
|
2022-08-05 02:06:42 -04:00
|
|
|
if (pSwapChain3->GetBuffer(i, IID_PPV_ARGS(&pBackBuffer)) != S_OK || pBackBuffer == nullptr)
|
|
|
|
{
|
|
|
|
pCmdAlloc->Release();
|
2024-02-09 21:58:00 +02:00
|
|
|
_ResetStatesOnly();
|
2022-08-05 02:06:42 -04:00
|
|
|
pSwapChain3->Release();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pDevice->CreateRenderTargetView(pBackBuffer, NULL, rtvHandle);
|
|
|
|
|
|
|
|
OverlayFrames.emplace_back(rtvHandle, pCmdAlloc, pBackBuffer);
|
|
|
|
rtvHandle.ptr += rtvDescriptorSize;
|
|
|
|
}
|
2019-10-15 08:41:07 +02:00
|
|
|
}
|
|
|
|
|
2024-02-08 02:26:06 +02:00
|
|
|
// auto heaps = std::move(get_free_texture_heap());
|
2022-08-05 02:06:42 -04:00
|
|
|
|
2024-02-09 21:58:00 +02:00
|
|
|
if(ImGui::GetCurrentContext() == nullptr)
|
|
|
|
ImGui::CreateContext((ImFontAtlas *)ImGuiFontAtlas);
|
|
|
|
|
2022-08-05 02:06:42 -04:00
|
|
|
ImGui_ImplDX12_Init(pDevice, bufferCount, DXGI_FORMAT_R8G8B8A8_UNORM, pSrvDescHeap,
|
2019-08-26 16:46:56 +02:00
|
|
|
pSrvDescHeap->GetCPUDescriptorHandleForHeapStart(),
|
|
|
|
pSrvDescHeap->GetGPUDescriptorHandleForHeapStart());
|
2024-02-08 02:26:06 +02:00
|
|
|
// heaps.cpu_handle,
|
|
|
|
// heaps.gpu_handle);
|
2019-10-15 08:41:07 +02:00
|
|
|
|
2022-08-10 03:22:23 -04:00
|
|
|
Windows_Hook::Inst()->SetInitialWindowSize(sc_desc.OutputWindow);
|
|
|
|
|
2022-08-05 02:06:42 -04:00
|
|
|
_Initialized = true;
|
|
|
|
OverlayHookReady(true);
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
2019-10-15 08:41:07 +02:00
|
|
|
|
2022-08-10 03:22:23 -04:00
|
|
|
if (ImGui_ImplDX12_NewFrame() && Windows_Hook::Inst()->PrepareForOverlay(sc_desc.OutputWindow))
|
2022-08-05 02:06:42 -04:00
|
|
|
{
|
2020-01-10 08:10:33 +01:00
|
|
|
ImGui::NewFrame();
|
2019-10-17 18:03:19 +02:00
|
|
|
|
2022-08-05 02:06:42 -04:00
|
|
|
OverlayProc();
|
2019-10-17 18:03:19 +02:00
|
|
|
|
2024-02-09 21:58:00 +02:00
|
|
|
ImGui::Render();
|
|
|
|
|
|
|
|
const UINT bufferIndex = pSwapChain3->GetCurrentBackBufferIndex();
|
|
|
|
|
|
|
|
OverlayFrames[bufferIndex].pCmdAlloc->Reset();
|
2019-10-15 08:41:07 +02:00
|
|
|
|
2020-01-10 08:10:33 +01:00
|
|
|
D3D12_RESOURCE_BARRIER barrier = {};
|
|
|
|
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
|
|
|
|
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
|
2022-08-05 02:06:42 -04:00
|
|
|
barrier.Transition.pResource = OverlayFrames[bufferIndex].pBackBuffer;
|
2020-01-10 08:10:33 +01:00
|
|
|
barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
|
|
|
|
barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT;
|
|
|
|
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_RENDER_TARGET;
|
2022-08-05 02:06:42 -04:00
|
|
|
pCmdList->Reset(OverlayFrames[bufferIndex].pCmdAlloc, NULL);
|
2020-01-10 08:10:33 +01:00
|
|
|
pCmdList->ResourceBarrier(1, &barrier);
|
2024-02-09 21:58:00 +02:00
|
|
|
|
2022-08-05 02:06:42 -04:00
|
|
|
pCmdList->OMSetRenderTargets(1, &OverlayFrames[bufferIndex].RenderTarget, FALSE, NULL);
|
2020-01-10 08:10:33 +01:00
|
|
|
pCmdList->SetDescriptorHeaps(1, &pSrvDescHeap);
|
|
|
|
|
|
|
|
ImGui_ImplDX12_RenderDrawData(ImGui::GetDrawData(), pCmdList);
|
|
|
|
|
|
|
|
barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_RENDER_TARGET;
|
|
|
|
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PRESENT;
|
|
|
|
pCmdList->ResourceBarrier(1, &barrier);
|
|
|
|
pCmdList->Close();
|
|
|
|
|
2022-08-05 02:06:42 -04:00
|
|
|
pCommandQueue->ExecuteCommandLists(1, (ID3D12CommandList* const*)&pCmdList);
|
2024-02-09 21:58:00 +02:00
|
|
|
|
2020-01-10 08:10:33 +01:00
|
|
|
}
|
2022-08-05 02:06:42 -04:00
|
|
|
|
2019-10-15 08:41:07 +02:00
|
|
|
pSwapChain3->Release();
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX12_Hook::MyPresent(IDXGISwapChain *_this, UINT SyncInterval, UINT Flags)
|
|
|
|
{
|
2022-08-05 02:06:42 -04:00
|
|
|
auto inst = DX12_Hook::Inst();
|
|
|
|
|
|
|
|
ID3D12CommandQueue* pCommandQueue = inst->_FindCommandQueueFromSwapChain(_this);
|
|
|
|
if (pCommandQueue != nullptr)
|
|
|
|
{
|
|
|
|
inst->_PrepareForOverlay(_this, pCommandQueue);
|
|
|
|
}
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2022-08-05 02:06:42 -04:00
|
|
|
return (_this->*inst->Present)(SyncInterval, Flags);
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX12_Hook::MyResizeTarget(IDXGISwapChain* _this, const DXGI_MODE_DESC* pNewTargetParameters)
|
|
|
|
{
|
2022-08-05 02:06:42 -04:00
|
|
|
auto inst = DX12_Hook::Inst();
|
|
|
|
inst->_ResetRenderState();
|
|
|
|
return (_this->*inst->ResizeTarget)(pNewTargetParameters);
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX12_Hook::MyResizeBuffers(IDXGISwapChain* _this, UINT BufferCount, UINT Width, UINT Height, DXGI_FORMAT NewFormat, UINT SwapChainFlags)
|
|
|
|
{
|
2022-08-05 02:06:42 -04:00
|
|
|
auto inst = DX12_Hook::Inst();
|
|
|
|
inst->_ResetRenderState();
|
|
|
|
return (_this->*inst->ResizeBuffers)(BufferCount, Width, Height, NewFormat, SwapChainFlags);
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
2019-08-26 16:46:56 +02:00
|
|
|
void STDMETHODCALLTYPE DX12_Hook::MyExecuteCommandLists(ID3D12CommandQueue *_this, UINT NumCommandLists, ID3D12CommandList* const* ppCommandLists)
|
|
|
|
{
|
2022-08-05 02:06:42 -04:00
|
|
|
auto inst = DX12_Hook::Inst();
|
|
|
|
inst->pCmdQueue = _this;
|
|
|
|
(_this->*inst->ExecuteCommandLists)(NumCommandLists, ppCommandLists);
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE DX12_Hook::MyPresent1(IDXGISwapChain1* _this, UINT SyncInterval, UINT Flags, const DXGI_PRESENT_PARAMETERS* pPresentParameters)
|
|
|
|
{
|
|
|
|
auto inst = DX12_Hook::Inst();
|
|
|
|
|
|
|
|
ID3D12CommandQueue* pCommandQueue = inst->_FindCommandQueueFromSwapChain(_this);
|
|
|
|
if (pCommandQueue != nullptr)
|
|
|
|
{
|
|
|
|
inst->_PrepareForOverlay(_this, pCommandQueue);
|
|
|
|
}
|
2019-10-17 18:03:19 +02:00
|
|
|
|
2022-08-05 02:06:42 -04:00
|
|
|
return (_this->*inst->Present1)(SyncInterval, Flags, pPresentParameters);
|
2019-08-26 16:46:56 +02:00
|
|
|
}
|
|
|
|
|
2019-07-25 23:21:03 +02:00
|
|
|
DX12_Hook::DX12_Hook():
|
2022-08-05 02:06:42 -04:00
|
|
|
_Initialized(false),
|
|
|
|
CommandQueueOffset(0),
|
|
|
|
pDevice(nullptr),
|
2019-10-15 08:41:07 +02:00
|
|
|
pCmdQueue(nullptr),
|
2019-08-27 15:38:07 +02:00
|
|
|
pSrvDescHeap(nullptr),
|
2019-10-15 08:41:07 +02:00
|
|
|
pCmdList(nullptr),
|
|
|
|
pRtvDescHeap(nullptr),
|
2022-08-05 02:06:42 -04:00
|
|
|
_Hooked(false),
|
|
|
|
_WindowsHooked(false),
|
2019-08-27 15:38:07 +02:00
|
|
|
Present(nullptr),
|
|
|
|
ResizeBuffers(nullptr),
|
|
|
|
ResizeTarget(nullptr),
|
2022-08-05 02:06:42 -04:00
|
|
|
ExecuteCommandLists(nullptr),
|
|
|
|
Present1(nullptr)
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2022-08-05 02:06:42 -04:00
|
|
|
SPDLOG_WARN("DX12 support is experimental, don't complain if it doesn't work as expected.");
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DX12_Hook::~DX12_Hook()
|
|
|
|
{
|
2022-08-05 02:06:42 -04:00
|
|
|
SPDLOG_INFO("DX12 Hook removed");
|
|
|
|
|
|
|
|
if (_WindowsHooked)
|
|
|
|
delete Windows_Hook::Inst();
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2022-08-05 02:06:42 -04:00
|
|
|
if (_Initialized)
|
2019-08-21 21:52:08 +02:00
|
|
|
{
|
2024-02-09 21:58:00 +02:00
|
|
|
_ResetStatesOnly();
|
2019-10-15 15:01:25 +02:00
|
|
|
|
2019-08-21 21:52:08 +02:00
|
|
|
ImGui_ImplDX12_InvalidateDeviceObjects();
|
|
|
|
ImGui::DestroyContext();
|
|
|
|
|
2022-08-05 02:06:42 -04:00
|
|
|
_Initialized = false;
|
2019-08-21 21:52:08 +02:00
|
|
|
}
|
2019-08-18 16:19:28 +02:00
|
|
|
|
2019-08-16 19:21:30 +02:00
|
|
|
_inst = nullptr;
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
2019-08-16 19:21:30 +02:00
|
|
|
DX12_Hook* DX12_Hook::Inst()
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2019-08-16 19:21:30 +02:00
|
|
|
if (_inst == nullptr)
|
|
|
|
_inst = new DX12_Hook();
|
|
|
|
|
|
|
|
return _inst;
|
2019-07-25 23:21:03 +02:00
|
|
|
}
|
|
|
|
|
2022-08-05 02:06:42 -04:00
|
|
|
std::string DX12_Hook::GetLibraryName() const
|
2019-07-25 23:21:03 +02:00
|
|
|
{
|
2022-08-05 02:06:42 -04:00
|
|
|
return LibraryName;
|
2019-08-26 16:46:56 +02:00
|
|
|
}
|
2019-07-25 23:21:03 +02:00
|
|
|
|
2022-08-05 02:06:42 -04:00
|
|
|
void DX12_Hook::LoadFunctions(
|
|
|
|
decltype(Present) PresentFcn,
|
|
|
|
decltype(ResizeBuffers) ResizeBuffersFcn,
|
|
|
|
decltype(ResizeTarget) ResizeTargetFcn,
|
|
|
|
decltype(ExecuteCommandLists) ExecuteCommandListsFcn,
|
|
|
|
decltype(Present1) Present1Fcn)
|
2019-08-26 16:46:56 +02:00
|
|
|
{
|
2022-08-05 02:06:42 -04:00
|
|
|
Present = PresentFcn;
|
|
|
|
ResizeBuffers = ResizeBuffersFcn;
|
|
|
|
ResizeTarget = ResizeTargetFcn;
|
|
|
|
|
|
|
|
ExecuteCommandLists = ExecuteCommandListsFcn;
|
|
|
|
|
|
|
|
Present1 = Present1Fcn;
|
2019-08-14 14:55:31 +02:00
|
|
|
}
|
|
|
|
|
2024-02-08 02:26:06 +02:00
|
|
|
// source: https://github.com/ocornut/imgui/wiki/Image-Loading-and-Displaying-Examples#example-for-directx12-users
|
2022-08-05 02:06:42 -04:00
|
|
|
std::weak_ptr<uint64_t> DX12_Hook::CreateImageResource(const void* image_data, uint32_t width, uint32_t height)
|
|
|
|
{
|
2024-02-08 02:26:06 +02:00
|
|
|
heap_t heap = get_free_texture_heap();
|
2024-02-09 21:58:00 +02:00
|
|
|
if (heap.id == -1) return {};
|
2024-02-08 02:26:06 +02:00
|
|
|
|
2024-02-09 21:58:00 +02:00
|
|
|
D3D12_HEAP_PROPERTIES props;
|
|
|
|
SecureZeroMemory(&props, sizeof(props));
|
2024-02-08 02:26:06 +02:00
|
|
|
props.Type = D3D12_HEAP_TYPE_DEFAULT;
|
|
|
|
props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
|
|
|
|
props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
|
|
|
|
|
2024-02-09 21:58:00 +02:00
|
|
|
D3D12_RESOURCE_DESC desc;
|
2024-02-08 02:26:06 +02:00
|
|
|
SecureZeroMemory(&desc, sizeof(desc));
|
|
|
|
desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
|
|
|
|
desc.Alignment = 0;
|
2024-02-09 21:58:00 +02:00
|
|
|
desc.Width = static_cast<UINT64>(width);
|
2024-02-08 02:26:06 +02:00
|
|
|
desc.Height = static_cast<UINT>(height);
|
|
|
|
desc.DepthOrArraySize = 1;
|
|
|
|
desc.MipLevels = 1;
|
|
|
|
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
|
|
|
desc.SampleDesc.Count = 1;
|
|
|
|
desc.SampleDesc.Quality = 0;
|
|
|
|
desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
|
|
|
|
desc.Flags = D3D12_RESOURCE_FLAG_NONE;
|
|
|
|
|
|
|
|
ID3D12Resource* pTexture = NULL;
|
2024-02-09 21:58:00 +02:00
|
|
|
HRESULT hr = pDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc,
|
2024-02-08 02:26:06 +02:00
|
|
|
D3D12_RESOURCE_STATE_COPY_DEST, NULL, IID_PPV_ARGS(&pTexture));
|
2024-02-09 21:58:00 +02:00
|
|
|
if (!SUCCEEDED(hr)) {
|
|
|
|
release_texture_heap(heap.id);
|
|
|
|
return {};
|
|
|
|
}
|
2024-02-08 02:26:06 +02:00
|
|
|
|
|
|
|
UINT uploadPitch = (width * 4 + D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1u) & ~(D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1u);
|
|
|
|
UINT uploadSize = height * uploadPitch;
|
|
|
|
desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
|
|
|
|
desc.Alignment = 0;
|
|
|
|
desc.Width = uploadSize;
|
|
|
|
desc.Height = 1;
|
|
|
|
desc.DepthOrArraySize = 1;
|
|
|
|
desc.MipLevels = 1;
|
|
|
|
desc.Format = DXGI_FORMAT_UNKNOWN;
|
|
|
|
desc.SampleDesc.Count = 1;
|
|
|
|
desc.SampleDesc.Quality = 0;
|
|
|
|
desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
|
|
|
|
desc.Flags = D3D12_RESOURCE_FLAG_NONE;
|
|
|
|
|
|
|
|
props.Type = D3D12_HEAP_TYPE_UPLOAD;
|
|
|
|
props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
|
|
|
|
props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
|
|
|
|
|
|
|
|
ID3D12Resource* uploadBuffer = NULL;
|
2024-02-09 21:58:00 +02:00
|
|
|
hr = pDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc,
|
2024-02-08 02:26:06 +02:00
|
|
|
D3D12_RESOURCE_STATE_GENERIC_READ, NULL, IID_PPV_ARGS(&uploadBuffer));
|
2024-02-09 21:58:00 +02:00
|
|
|
if (!SUCCEEDED(hr)) {
|
|
|
|
pTexture->Release();
|
|
|
|
release_texture_heap(heap.id);
|
|
|
|
return {};
|
|
|
|
}
|
2024-02-08 02:26:06 +02:00
|
|
|
|
|
|
|
void* mapped = NULL;
|
|
|
|
D3D12_RANGE range = { 0, uploadSize };
|
|
|
|
hr = uploadBuffer->Map(0, &range, &mapped);
|
2024-02-09 21:58:00 +02:00
|
|
|
if (!SUCCEEDED(hr)) {
|
|
|
|
uploadBuffer->Release();
|
|
|
|
pTexture->Release();
|
|
|
|
release_texture_heap(heap.id);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int y = 0; y < height; y++) {
|
|
|
|
memcpy(
|
|
|
|
(void*)((uintptr_t)mapped + y * uploadPitch),
|
|
|
|
reinterpret_cast<const uint8_t*>(image_data) + y * width * 4,
|
|
|
|
width * 4
|
|
|
|
);
|
|
|
|
}
|
2024-02-08 02:26:06 +02:00
|
|
|
uploadBuffer->Unmap(0, &range);
|
|
|
|
|
|
|
|
D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
|
|
|
|
srcLocation.pResource = uploadBuffer;
|
|
|
|
srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
|
|
|
|
srcLocation.PlacedFootprint.Footprint.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
|
|
|
srcLocation.PlacedFootprint.Footprint.Width = width;
|
|
|
|
srcLocation.PlacedFootprint.Footprint.Height = height;
|
|
|
|
srcLocation.PlacedFootprint.Footprint.Depth = 1;
|
|
|
|
srcLocation.PlacedFootprint.Footprint.RowPitch = uploadPitch;
|
|
|
|
|
|
|
|
D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
|
|
|
|
dstLocation.pResource = pTexture;
|
|
|
|
dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
|
|
|
|
dstLocation.SubresourceIndex = 0;
|
|
|
|
|
|
|
|
D3D12_RESOURCE_BARRIER barrier = {};
|
|
|
|
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
|
|
|
|
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
|
|
|
|
barrier.Transition.pResource = pTexture;
|
|
|
|
barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
|
|
|
|
barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
|
|
|
|
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
|
|
|
|
|
|
|
|
ID3D12Fence* fence = NULL;
|
|
|
|
hr = pDevice->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence));
|
2024-02-09 21:58:00 +02:00
|
|
|
if (!SUCCEEDED(hr)) {
|
|
|
|
uploadBuffer->Unmap(0, &range);
|
|
|
|
uploadBuffer->Release();
|
|
|
|
pTexture->Release();
|
|
|
|
release_texture_heap(heap.id);
|
|
|
|
return {};
|
|
|
|
}
|
2024-02-08 02:26:06 +02:00
|
|
|
|
|
|
|
D3D12_COMMAND_QUEUE_DESC queueDesc = {};
|
|
|
|
queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
|
|
|
|
queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
|
|
|
|
queueDesc.NodeMask = 1;
|
|
|
|
|
|
|
|
ID3D12CommandQueue* cmdQueue = NULL;
|
|
|
|
hr = pDevice->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&cmdQueue));
|
2024-02-09 21:58:00 +02:00
|
|
|
if (!SUCCEEDED(hr)) {
|
|
|
|
fence->Release();
|
|
|
|
uploadBuffer->Unmap(0, &range);
|
|
|
|
uploadBuffer->Release();
|
|
|
|
pTexture->Release();
|
|
|
|
release_texture_heap(heap.id);
|
|
|
|
return {};
|
|
|
|
}
|
2024-02-08 02:26:06 +02:00
|
|
|
|
|
|
|
ID3D12CommandAllocator* cmdAlloc = NULL;
|
|
|
|
hr = pDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&cmdAlloc));
|
2024-02-09 21:58:00 +02:00
|
|
|
if (!SUCCEEDED(hr)) {
|
|
|
|
cmdQueue->Release();
|
|
|
|
fence->Release();
|
|
|
|
uploadBuffer->Unmap(0, &range);
|
|
|
|
uploadBuffer->Release();
|
|
|
|
pTexture->Release();
|
|
|
|
release_texture_heap(heap.id);
|
|
|
|
return {};
|
|
|
|
}
|
2024-02-08 02:26:06 +02:00
|
|
|
|
|
|
|
ID3D12GraphicsCommandList* cmdList = NULL;
|
|
|
|
hr = pDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, cmdAlloc, NULL, IID_PPV_ARGS(&cmdList));
|
2024-02-09 21:58:00 +02:00
|
|
|
if (!SUCCEEDED(hr)) {
|
|
|
|
cmdAlloc->Release();
|
|
|
|
cmdQueue->Release();
|
|
|
|
fence->Release();
|
|
|
|
uploadBuffer->Unmap(0, &range);
|
|
|
|
uploadBuffer->Release();
|
|
|
|
pTexture->Release();
|
|
|
|
release_texture_heap(heap.id);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE event = CreateEventW(0, 0, 0, 0);
|
|
|
|
if (!event) {
|
|
|
|
cmdList->Release();
|
|
|
|
cmdAlloc->Release();
|
|
|
|
cmdQueue->Release();
|
|
|
|
fence->Release();
|
|
|
|
uploadBuffer->Unmap(0, &range);
|
|
|
|
uploadBuffer->Release();
|
|
|
|
pTexture->Release();
|
|
|
|
release_texture_heap(heap.id);
|
|
|
|
return {};
|
|
|
|
}
|
2024-02-08 02:26:06 +02:00
|
|
|
|
|
|
|
cmdList->CopyTextureRegion(&dstLocation, 0, 0, 0, &srcLocation, NULL);
|
|
|
|
cmdList->ResourceBarrier(1, &barrier);
|
|
|
|
|
|
|
|
hr = cmdList->Close();
|
2024-02-09 21:58:00 +02:00
|
|
|
if (!SUCCEEDED(hr)) {
|
|
|
|
CloseHandle(event);
|
|
|
|
cmdList->Release();
|
|
|
|
cmdAlloc->Release();
|
|
|
|
cmdQueue->Release();
|
|
|
|
fence->Release();
|
|
|
|
uploadBuffer->Unmap(0, &range);
|
|
|
|
uploadBuffer->Release();
|
|
|
|
pTexture->Release();
|
|
|
|
release_texture_heap(heap.id);
|
|
|
|
return {};
|
|
|
|
}
|
2024-02-08 02:26:06 +02:00
|
|
|
|
|
|
|
cmdQueue->ExecuteCommandLists(1, (ID3D12CommandList* const*)&cmdList);
|
|
|
|
hr = cmdQueue->Signal(fence, 1);
|
2024-02-09 21:58:00 +02:00
|
|
|
if (!SUCCEEDED(hr)) {
|
|
|
|
CloseHandle(event);
|
|
|
|
cmdList->Close();
|
|
|
|
cmdList->Release();
|
|
|
|
cmdAlloc->Release();
|
|
|
|
cmdQueue->Release();
|
|
|
|
fence->Release();
|
|
|
|
uploadBuffer->Unmap(0, &range);
|
|
|
|
uploadBuffer->Release();
|
|
|
|
pTexture->Release();
|
|
|
|
release_texture_heap(heap.id);
|
|
|
|
return {};
|
|
|
|
}
|
2024-02-08 02:26:06 +02:00
|
|
|
|
2024-02-09 21:58:00 +02:00
|
|
|
hr = fence->SetEventOnCompletion(1, event);
|
|
|
|
if (!SUCCEEDED(hr)) {
|
|
|
|
CloseHandle(event);
|
|
|
|
cmdList->Close();
|
|
|
|
cmdList->Release();
|
|
|
|
cmdAlloc->Release();
|
|
|
|
cmdQueue->Release();
|
|
|
|
fence->Release();
|
|
|
|
uploadBuffer->Unmap(0, &range);
|
|
|
|
uploadBuffer->Release();
|
|
|
|
pTexture->Release();
|
|
|
|
release_texture_heap(heap.id);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2024-02-08 02:26:06 +02:00
|
|
|
WaitForSingleObject(event, INFINITE);
|
|
|
|
|
2024-02-09 21:58:00 +02:00
|
|
|
CloseHandle(event);
|
2024-02-08 02:26:06 +02:00
|
|
|
cmdList->Release();
|
|
|
|
cmdAlloc->Release();
|
|
|
|
cmdQueue->Release();
|
|
|
|
fence->Release();
|
|
|
|
uploadBuffer->Release();
|
|
|
|
|
|
|
|
// Create texture view
|
|
|
|
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc;
|
|
|
|
SecureZeroMemory(&srvDesc, sizeof(srvDesc));
|
|
|
|
srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
|
|
|
srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
|
|
|
|
srvDesc.Texture2D.MipLevels = desc.MipLevels;
|
|
|
|
srvDesc.Texture2D.MostDetailedMip = 0;
|
|
|
|
srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
|
|
|
|
|
|
|
|
pDevice->CreateShaderResourceView(pTexture, &srvDesc, heap.cpu_handle);
|
|
|
|
|
|
|
|
texture_t* texture_data = new texture_t;
|
|
|
|
texture_data->gpu_handle = heap.gpu_handle.ptr;
|
|
|
|
texture_data->pTexture = pTexture;
|
|
|
|
texture_data->heap_id = heap.id;
|
|
|
|
|
|
|
|
auto ptr = std::shared_ptr<uint64_t>((uint64_t*)texture_data, [this](uint64_t* handle)
|
|
|
|
{
|
|
|
|
if (handle != nullptr) {
|
|
|
|
texture_t* pTextureData = reinterpret_cast<texture_t*>(handle);
|
|
|
|
pTextureData->pTexture->Release();
|
|
|
|
release_texture_heap(pTextureData->heap_id);
|
|
|
|
|
|
|
|
delete pTextureData;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
_ImageResources.emplace(ptr);
|
|
|
|
|
|
|
|
return ptr;
|
2022-08-05 02:06:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void DX12_Hook::ReleaseImageResource(std::weak_ptr<uint64_t> resource)
|
|
|
|
{
|
2024-02-08 02:26:06 +02:00
|
|
|
auto ptr = resource.lock();
|
|
|
|
if (ptr)
|
|
|
|
{
|
|
|
|
auto it = _ImageResources.find(ptr);
|
|
|
|
if (it != _ImageResources.end())
|
|
|
|
_ImageResources.erase(it);
|
|
|
|
}
|
2024-02-09 21:58:00 +02:00
|
|
|
}
|