diff --git a/ThirdParty_LICENSES b/ThirdParty_LICENSES index b9fcd297..00908d37 100644 --- a/ThirdParty_LICENSES +++ b/ThirdParty_LICENSES @@ -1,4 +1,4 @@ -============ imgui ============ +============ imgui ============ The MIT License (MIT) @@ -22,11 +22,11 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -============ spdlog ============ +============ spdlog ============ The MIT License (MIT) -Copyright (c) 2016 Gabi Melman. +Copyright (c) 2016 Gabi Melman. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -52,7 +52,7 @@ and users must comply to its license: https://github.com/fmtlib/fmt/blob/master/ ============ nlohmann/json ============ -MIT License +MIT License Copyright (c) 2013-2021 Niels Lohmann @@ -98,9 +98,9 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -============ MinHook ============ +============ MinHook ============ -MinHook - The Minimalistic API Hooking Library for x64/x86 +MinHook - The Minimalistic API Hooking Library for x64/x86 Copyright (C) 2009-2017 Tsuda Kageyu. All rights reserved. @@ -323,30 +323,6 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -============ kiero ============ - -MIT License - -Copyright (c) 2014-2020 Rebzzel - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - ============ lsqlite3 ============ * lsqlite3 * @@ -491,7 +467,7 @@ with others. The OFL allows the licensed fonts to be used, studied, modified and redistributed freely as long as they are not sold by themselves. The -fonts, including any derivative works, can be bundled, embedded, +fonts, including any derivative works, can be bundled, embedded, redistributed and/or sold with any software provided that any reserved names are not used by derivative works. The fonts and derivatives, however, cannot be released under any other type of license. The diff --git a/src/d3d12/D3D12.cpp b/src/d3d12/D3D12.cpp index 58a99bc7..cb8efff8 100644 --- a/src/d3d12/D3D12.cpp +++ b/src/d3d12/D3D12.cpp @@ -65,7 +65,7 @@ D3D12::D3D12(Window& aWindow, Paths& aPaths, Options& aOptions) , m_window(aWindow) , m_options(aOptions) { - HookGame(); + Hook(); // add repeated task which prepares next ImGui frame for update GameMainThread::Get().AddGenericTask( diff --git a/src/d3d12/D3D12.h b/src/d3d12/D3D12.h index 0c64197b..87035739 100644 --- a/src/d3d12/D3D12.h +++ b/src/d3d12/D3D12.h @@ -3,10 +3,6 @@ #include "common/D3D12Downlevel.h" #include "window/Window.h" -using TPresentD3D12Downlevel = HRESULT(ID3D12CommandQueueDownlevel*, ID3D12GraphicsCommandList*, ID3D12Resource*, HWND, D3D12_DOWNLEVEL_PRESENT_FLAGS); -using TCreateCommittedResource = - HRESULT(ID3D12Device*, const D3D12_HEAP_PROPERTIES*, D3D12_HEAP_FLAGS, const D3D12_RESOURCE_DESC*, D3D12_RESOURCE_STATES, const D3D12_CLEAR_VALUE*, const IID*, void**); -using TExecuteCommandLists = void(ID3D12CommandQueue*, UINT, ID3D12CommandList* const*); using TCRenderNode_Present_InternalPresent = void*(int32_t*, uint8_t, UINT); using TCRenderGlobal_Resize = void*(uint32_t a1, uint32_t a2, uint32_t a3, uint8_t a4, int32_t* a5); using TCRenderGlobal_Shutdown = void*(uint64_t a1, uint64_t a2, uint64_t a3, uint64_t a4); @@ -35,7 +31,6 @@ struct D3D12 protected: void Hook(); - void HookGame(); struct FrameContext { @@ -44,30 +39,18 @@ struct D3D12 D3D12_CPU_DESCRIPTOR_HANDLE MainRenderTargetDescriptor{0}; }; - bool ResetState(const bool acClearDownlevelBackbuffers = true, const bool acDestroyContext = false); + bool ResetState(const bool acDestroyContext = false); bool Initialize(); - bool InitializeDownlevel(ID3D12CommandQueue* apCommandQueue, ID3D12Resource* apSourceTex2D, HWND ahWindow); bool InitializeImGui(size_t aBuffersCounts); void PrepareUpdate(); void Update(); - static HRESULT PresentDownlevel( - ID3D12CommandQueueDownlevel* apCommandQueueDownlevel, ID3D12GraphicsCommandList* apOpenCommandList, ID3D12Resource* apSourceTex2D, HWND ahWindow, - D3D12_DOWNLEVEL_PRESENT_FLAGS aFlags); - static HRESULT CreateCommittedResource( - ID3D12Device* apDevice, const D3D12_HEAP_PROPERTIES* acpHeapProperties, D3D12_HEAP_FLAGS aHeapFlags, const D3D12_RESOURCE_DESC* acpDesc, - D3D12_RESOURCE_STATES aInitialResourceState, const D3D12_CLEAR_VALUE* acpOptimizedClearValue, const IID* acpRIID, void** appvResource); - static void ExecuteCommandLists(ID3D12CommandQueue* apCommandQueue, UINT aNumCommandLists, ID3D12CommandList* const* apcpCommandLists); - static void* CRenderNode_Present_InternalPresent(int32_t* apSomeInt, uint8_t aSomeSync, UINT aSyncInterval); static void* CRenderGlobal_Resize(uint32_t a1, uint32_t a2, uint32_t a3, uint8_t a4, int32_t* a5); static void* CRenderGlobal_Shutdown(uint64_t a1, uint64_t a2, uint64_t a3, uint64_t a4); private: - TPresentD3D12Downlevel* m_realPresentD3D12Downlevel{nullptr}; - TCreateCommittedResource* m_realCreateCommittedResource{nullptr}; - TExecuteCommandLists* m_realExecuteCommandLists{nullptr}; TCRenderNode_Present_InternalPresent* m_realInternalPresent{nullptr}; TCRenderGlobal_Resize* m_realInternalResize{nullptr}; TCRenderGlobal_Shutdown* m_realInternalShutdown{nullptr}; @@ -75,8 +58,6 @@ struct D3D12 bool m_initialized{false}; TiltedPhoques::Vector m_frameContexts; - TiltedPhoques::Vector> m_downlevelBackbuffers; - uint32_t m_downlevelBufferIndex{0}; Microsoft::WRL::ComPtr m_pd3d12Device{}; Microsoft::WRL::ComPtr m_pd3dRtvDescHeap{}; diff --git a/src/d3d12/D3D12_Functions.cpp b/src/d3d12/D3D12_Functions.cpp index ca23e2d6..f48dc2d4 100644 --- a/src/d3d12/D3D12_Functions.cpp +++ b/src/d3d12/D3D12_Functions.cpp @@ -9,7 +9,7 @@ #include #include -bool D3D12::ResetState(const bool acClearDownlevelBackbuffers, const bool acDestroyContext) +bool D3D12::ResetState(const bool acDestroyContext) { if (m_initialized) { @@ -32,10 +32,6 @@ bool D3D12::ResetState(const bool acClearDownlevelBackbuffers, const bool acDest m_frameContexts.clear(); m_outSize = {0, 0}; - if (acClearDownlevelBackbuffers) - m_downlevelBackbuffers.clear(); - m_downlevelBufferIndex = 0; - m_pd3d12Device.Reset(); m_pd3dRtvDescHeap.Reset(); m_pd3dSrvDescHeap.Reset(); @@ -144,137 +140,6 @@ bool D3D12::Initialize() return true; } -bool D3D12::InitializeDownlevel(ID3D12CommandQueue* apCommandQueue, ID3D12Resource* apSourceTex2D, HWND ahWindow) -{ - if (!apCommandQueue || !apSourceTex2D) - return false; - - const HWND hWnd = m_window.GetWindow(); - if (!hWnd) - { - Log::Warn("D3D12::InitializeDownlevel() - window not yet hooked!"); - return false; - } - - if (m_initialized) - { - if (hWnd != ahWindow) - Log::Warn( - "D3D12::InitializeDownlevel() - current output window does not match hooked window! Currently hooked " - "to {} while current output window is {}.", - reinterpret_cast(hWnd), reinterpret_cast(ahWindow)); - - return true; - } - - const auto cmdQueueDesc = apCommandQueue->GetDesc(); - if (cmdQueueDesc.Type != D3D12_COMMAND_LIST_TYPE_DIRECT) - { - Log::Warn("D3D12::InitializeDownlevel() - ignoring command queue - invalid type of command list!"); - return false; - } - - m_pCommandQueue = apCommandQueue; - - const auto st2DDesc = apSourceTex2D->GetDesc(); - m_outSize = {static_cast(st2DDesc.Width), static_cast(st2DDesc.Height)}; - - if (hWnd != ahWindow) - Log::Warn( - "D3D12::InitializeDownlevel() - current output window does not match hooked window! Currently hooked to {} " - "while current output window is {}.", - reinterpret_cast(hWnd), reinterpret_cast(ahWindow)); - - if (FAILED(apSourceTex2D->GetDevice(IID_PPV_ARGS(&m_pd3d12Device)))) - { - Log::Error("D3D12::InitializeDownlevel() - failed to get device!"); - return ResetState(); - } - - const size_t buffersCounts = m_downlevelBackbuffers.size(); - m_frameContexts.resize(buffersCounts); - if (buffersCounts == 0) - { - Log::Error("D3D12::InitializeDownlevel() - no backbuffers were found!"); - return ResetState(); - } - if (buffersCounts < g_numDownlevelBackbuffersRequired) - { - Log::Info("D3D12::InitializeDownlevel() - backbuffer list is not complete yet; assuming window was resized"); - return false; - } - - D3D12_DESCRIPTOR_HEAP_DESC rtvdesc; - rtvdesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV; - rtvdesc.NumDescriptors = static_cast(buffersCounts); - rtvdesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE; - rtvdesc.NodeMask = 1; - if (FAILED(m_pd3d12Device->CreateDescriptorHeap(&rtvdesc, IID_PPV_ARGS(&m_pd3dRtvDescHeap)))) - { - Log::Error("D3D12::InitializeDownlevel() - failed to create RTV descriptor heap!"); - return ResetState(); - } - - const SIZE_T rtvDescriptorSize = m_pd3d12Device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV); - D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle = m_pd3dRtvDescHeap->GetCPUDescriptorHandleForHeapStart(); - for (auto& context : m_frameContexts) - { - context.MainRenderTargetDescriptor = rtvHandle; - rtvHandle.ptr += rtvDescriptorSize; - } - - D3D12_DESCRIPTOR_HEAP_DESC srvdesc = {}; - srvdesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; - srvdesc.NumDescriptors = 2; - srvdesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE; - if (FAILED(m_pd3d12Device->CreateDescriptorHeap(&srvdesc, IID_PPV_ARGS(&m_pd3dSrvDescHeap)))) - { - Log::Error("D3D12::InitializeDownlevel() - failed to create SRV descriptor heap!"); - return ResetState(); - } - - for (auto& context : m_frameContexts) - { - if (FAILED(m_pd3d12Device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&context.CommandAllocator)))) - { - Log::Error("D3D12::InitializeDownlevel() - failed to create command allocator!"); - return ResetState(); - } - } - - if (FAILED(m_pd3d12Device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, m_frameContexts[0].CommandAllocator.Get(), nullptr, IID_PPV_ARGS(&m_pd3dCommandList)))) - { - Log::Error("D3D12::InitializeDownlevel() - failed to create command list!"); - return ResetState(); - } - - if (FAILED(m_pd3dCommandList->Close())) - { - Log::Error("D3D12::InitializeDownlevel() - failed to close command list!"); - return ResetState(); - } - - for (size_t i = 0; i < buffersCounts; i++) - { - auto& context = m_frameContexts[i]; - context.BackBuffer = m_downlevelBackbuffers[i]; - m_pd3d12Device->CreateRenderTargetView(context.BackBuffer.Get(), nullptr, context.MainRenderTargetDescriptor); - } - - if (!InitializeImGui(buffersCounts)) - { - Log::Error("D3D12::InitializeDownlevel() - failed to initialize ImGui!"); - return ResetState(); - } - - Log::Info("D3D12::InitializeDownlevel() - initialization successful!"); - m_initialized = true; - - OnInitialized.Emit(); - - return true; -} - void D3D12::ReloadFonts() { std::lock_guard _(m_imguiLock); @@ -525,8 +390,8 @@ void D3D12::Update() if (!m_imguiDrawDataBuffers[0].Valid) return; - const auto bufferIndex = m_pdxgiSwapChain != nullptr ? m_pdxgiSwapChain->GetCurrentBackBufferIndex() : m_downlevelBufferIndex; - auto& frameContext = m_frameContexts[bufferIndex]; + assert(m_pdxgiSwapChain); + auto& frameContext = m_frameContexts[m_pdxgiSwapChain->GetCurrentBackBufferIndex()]; frameContext.CommandAllocator->Reset(); D3D12_RESOURCE_BARRIER barrier; diff --git a/src/d3d12/D3D12_Hooks.cpp b/src/d3d12/D3D12_Hooks.cpp index c05b7189..a90a3886 100644 --- a/src/d3d12/D3D12_Hooks.cpp +++ b/src/d3d12/D3D12_Hooks.cpp @@ -6,103 +6,8 @@ #include "reverse/Addresses.h" #include "reverse/RenderContext.h" -#include - #include -HRESULT D3D12::PresentDownlevel( - ID3D12CommandQueueDownlevel* apCommandQueueDownlevel, ID3D12GraphicsCommandList* apOpenCommandList, ID3D12Resource* apSourceTex2D, HWND ahWindow, - D3D12_DOWNLEVEL_PRESENT_FLAGS aFlags) -{ - if (CET::Get().GetOptions().Patches.DisableWin7Vsync) - aFlags &= ~D3D12_DOWNLEVEL_PRESENT_FLAG_WAIT_FOR_VBLANK; - - auto& d3d12 = CET::Get().GetD3D12(); - - // On Windows 7 there is no swap chain to query the current backbuffer index. Instead do a reverse lookup in the - // known backbuffer list - const auto cbegin = d3d12.m_downlevelBackbuffers.size() >= g_numDownlevelBackbuffersRequired ? d3d12.m_downlevelBackbuffers.cend() - g_numDownlevelBackbuffersRequired - : d3d12.m_downlevelBackbuffers.cbegin(); - auto it = std::find_if(cbegin, d3d12.m_downlevelBackbuffers.cend(), [apSourceTex2D](const auto& downlevelBackbuffer) { return downlevelBackbuffer.Get() == apSourceTex2D; }); - if (it == d3d12.m_downlevelBackbuffers.cend()) - { - if (d3d12.m_initialized) - { - // Already initialized - assume the window was resized and reset state - d3d12.ResetState(); - } - - // Add the buffer to the list - d3d12.m_downlevelBackbuffers.emplace_back(apSourceTex2D); - it = d3d12.m_downlevelBackbuffers.cend() - 1; - } - - // Limit to at most 3 buffers - const size_t numBackbuffers = std::min(d3d12.m_downlevelBackbuffers.size(), g_numDownlevelBackbuffersRequired); - const size_t skip = d3d12.m_downlevelBackbuffers.size() - numBackbuffers; - d3d12.m_downlevelBackbuffers.erase(d3d12.m_downlevelBackbuffers.cbegin(), d3d12.m_downlevelBackbuffers.cbegin() + skip); - - // Determine the current buffer index - d3d12.m_downlevelBufferIndex = static_cast(std::distance(d3d12.m_downlevelBackbuffers.cbegin() + skip, it)); - - if (d3d12.InitializeDownlevel(d3d12.m_pCommandQueue.Get(), apSourceTex2D, ahWindow)) - d3d12.Update(); - - return d3d12.m_realPresentD3D12Downlevel(apCommandQueueDownlevel, apOpenCommandList, apSourceTex2D, ahWindow, aFlags); -} - -HRESULT D3D12::CreateCommittedResource( - ID3D12Device* apDevice, const D3D12_HEAP_PROPERTIES* acpHeapProperties, D3D12_HEAP_FLAGS aHeapFlags, const D3D12_RESOURCE_DESC* acpDesc, - D3D12_RESOURCE_STATES aInitialResourceState, const D3D12_CLEAR_VALUE* acpOptimizedClearValue, const IID* acpRIID, void** appvResource) -{ - auto& d3d12 = CET::Get().GetD3D12(); - - // Check if this is a backbuffer resource being created - bool isBackBuffer = false; - if (acpHeapProperties != nullptr && acpHeapProperties->Type == D3D12_HEAP_TYPE_DEFAULT && aHeapFlags == D3D12_HEAP_FLAG_NONE && acpDesc != nullptr && - acpDesc->Flags == D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET && aInitialResourceState == D3D12_RESOURCE_STATE_COMMON && acpOptimizedClearValue == nullptr && - acpRIID != nullptr && IsEqualGUID(*acpRIID, __uuidof(ID3D12Resource))) - { - isBackBuffer = true; - } - - HRESULT result = d3d12.m_realCreateCommittedResource(apDevice, acpHeapProperties, aHeapFlags, acpDesc, aInitialResourceState, acpOptimizedClearValue, acpRIID, appvResource); - - if (SUCCEEDED(result) && isBackBuffer) - { - // Store the returned resource - d3d12.m_downlevelBackbuffers.emplace_back(static_cast(*appvResource)); - spdlog::debug("D3D12::CreateCommittedResourceD3D12() - found valid backbuffer target at {}.", *appvResource); - - if (d3d12.m_initialized) - { - // Reset state (a resize may have happened), but don't touch the backbuffer list. The downlevel Present hook - // will take care of this - d3d12.ResetState(false); - } - } - - return result; -} - -void D3D12::ExecuteCommandLists(ID3D12CommandQueue* apCommandQueue, UINT aNumCommandLists, ID3D12CommandList* const* apcpCommandLists) -{ - auto& d3d12 = CET::Get().GetD3D12(); - if (d3d12.m_pCommandQueue == nullptr) - { - const auto desc = apCommandQueue->GetDesc(); - if (desc.Type == D3D12_COMMAND_LIST_TYPE_DIRECT) - { - auto ret = reinterpret_cast(_ReturnAddress()) - reinterpret_cast(GetModuleHandleA(nullptr)); - d3d12.m_pCommandQueue = apCommandQueue; - Log::Info("D3D12::ExecuteCommandListsD3D12() - found valid command queue. {:X}", ret); - } - else - Log::Info("D3D12::ExecuteCommandListsD3D12() - ignoring command queue - unusable command list type"); - } - d3d12.m_realExecuteCommandLists(apCommandQueue, aNumCommandLists, apcpCommandLists); -} - void* ApplyHook(void** vtable, size_t index, void* target) { DWORD oldProtect; @@ -116,8 +21,6 @@ void* ApplyHook(void** vtable, size_t index, void* target) void* D3D12::CRenderNode_Present_InternalPresent(int32_t* apDeviceIndex, uint8_t aSomeSync, UINT aSyncInterval) { - static std::once_flag s_kieroOnce; - auto& d3d12 = CET::Get().GetD3D12(); const auto* pContext = RenderContext::GetInstance(); @@ -137,20 +40,7 @@ void* D3D12::CRenderNode_Present_InternalPresent(int32_t* apDeviceIndex, uint8_t } else { - std::call_once( - s_kieroOnce, - [] - { - if (kiero::init() != kiero::Status::Success) - Log::Error("Kiero failed!"); - else - { - std::string_view d3d12type = kiero::isDownLevelDevice() ? "D3D12on7" : "D3D12"; - Log::Info("Kiero initialized for {}", d3d12type); - - CET::Get().GetD3D12().Hook(); - } - }); + Log::Error("Unsupported OS!"); } } @@ -177,7 +67,7 @@ void* D3D12::CRenderGlobal_Shutdown(uint64_t a1, uint64_t a2, uint64_t a3, uint6 { auto& d3d12 = CET::Get().GetD3D12(); - d3d12.ResetState(true, true); + d3d12.ResetState(true); return d3d12.m_realInternalShutdown(a1, a2, a3, a4); } @@ -206,55 +96,6 @@ std::tuple D3D12::Crea } void D3D12::Hook() -{ - if (kiero::isDownLevelDevice()) - { - int d3d12FailedHooksCount = 0; - int d3d12CompleteHooksCount = 0; - - if (kiero::bind(175, reinterpret_cast(&m_realPresentD3D12Downlevel), reinterpret_cast(&PresentDownlevel)) != kiero::Status::Success) - { - Log::Error("D3D12on7: Downlevel Present hook failed!"); - ++d3d12FailedHooksCount; - } - else - { - Log::Info("D3D12on7: Downlevel Present hook complete."); - ++d3d12CompleteHooksCount; - } - - if (kiero::bind(27, reinterpret_cast(&m_realCreateCommittedResource), reinterpret_cast(&CreateCommittedResource)) != kiero::Status::Success) - { - Log::Error("D3D12on7: CreateCommittedResource Hook failed!"); - ++d3d12FailedHooksCount; - } - else - { - Log::Info("D3D12on7: CreateCommittedResource hook complete."); - ++d3d12CompleteHooksCount; - } - - if (kiero::bind(54, reinterpret_cast(&m_realExecuteCommandLists), reinterpret_cast(&ExecuteCommandLists)) != kiero::Status::Success) - { - Log::Error("D3D12on7: ExecuteCommandLists hook failed!"); - ++d3d12FailedHooksCount; - } - else - { - Log::Info("D3D12on7: ExecuteCommandLists hook complete."); - ++d3d12CompleteHooksCount; - } - - if (d3d12FailedHooksCount == 0) - Log::Info("D3D12on7: hook complete. ({}/{})", d3d12CompleteHooksCount, d3d12CompleteHooksCount + d3d12FailedHooksCount); - else - Log::Error("D3D12on7: hook failed! ({}/{})", d3d12CompleteHooksCount, d3d12CompleteHooksCount + d3d12FailedHooksCount); - } - else - Log::Info("Skipping internal d3d12 hook, using game method"); -} - -void D3D12::HookGame() { const RED4ext::UniversalRelocPtr presentInternal(CyberEngineTweaks::AddressHashes::CRenderNode_Present_DoInternal); const RED4ext::UniversalRelocPtr resizeInternal(CyberEngineTweaks::AddressHashes::CRenderGlobal_Resize); diff --git a/src/kiero/LICENSE b/src/kiero/LICENSE deleted file mode 100644 index 174cd232..00000000 --- a/src/kiero/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2014-2020 Rebzzel - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/src/kiero/kiero.cpp b/src/kiero/kiero.cpp deleted file mode 100644 index 53603330..00000000 --- a/src/kiero/kiero.cpp +++ /dev/null @@ -1,304 +0,0 @@ -#include - -#include "kiero.h" -#include - -#include -#include "common/D3D12Downlevel.h" - -#include - -static bool g_kieroInitialized = false; -static uint150_t* g_methodsTable = nullptr; -static void** g_swapChainVtable = nullptr; -static void** g_commandListVtable = nullptr; -static void** g_commandQueueVtable = nullptr; -static uintptr_t g_commandQueueOffset = 0; -static bool g_isDownLevelDevice = false; - -kiero::Status::Enum kiero::init() -{ - if (g_kieroInitialized) - return Status::AlreadyInitializedError; - - WNDCLASSEX windowClass; - windowClass.cbSize = sizeof(WNDCLASSEX); - windowClass.style = CS_HREDRAW | CS_VREDRAW; - windowClass.lpfnWndProc = DefWindowProc; - windowClass.cbClsExtra = 0; - windowClass.cbWndExtra = 0; - windowClass.hInstance = GetModuleHandle(nullptr); - windowClass.hIcon = nullptr; - windowClass.hCursor = nullptr; - windowClass.hbrBackground = nullptr; - windowClass.lpszMenuName = nullptr; - windowClass.lpszClassName = TEXT("Kiero"); - windowClass.hIconSm = nullptr; - - ::RegisterClassEx(&windowClass); - - HWND window = ::CreateWindow(windowClass.lpszClassName, TEXT("Kiero DirectX Window"), WS_OVERLAPPEDWINDOW, 0, 0, 100, 100, NULL, NULL, windowClass.hInstance, NULL); - - HMODULE libDXGI; - HMODULE libD3D12; - if ((libDXGI = ::GetModuleHandle(TEXT("dxgi.dll"))) == nullptr) - { - DestroyWindow(window); - UnregisterClass(windowClass.lpszClassName, windowClass.hInstance); - return Status::ModuleNotFoundError; - } - - if ((libD3D12 = ::GetModuleHandle(TEXT("d3d12.dll"))) == nullptr) - { - const TCHAR* localD3d12on7Paths[] = {TEXT(".\\d3d12on7\\d3d12.dll"), TEXT(".\\12on7\\d3d12.dll")}; - - for (uint32_t i = 0; i < std::size(localD3d12on7Paths); i++) - { - libD3D12 = LoadLibrary(localD3d12on7Paths[i]); - if (libD3D12 != nullptr) - break; - } - - if (libD3D12 == nullptr) - { - if ((libD3D12 = ::LoadLibrary(TEXT("d3d12.dll"))) == nullptr) - { - DestroyWindow(window); - UnregisterClass(windowClass.lpszClassName, windowClass.hInstance); - return Status::ModuleNotFoundError; - } - } - } - - void* CreateDXGIFactory; - if ((CreateDXGIFactory = reinterpret_cast(GetProcAddress(libDXGI, "CreateDXGIFactory"))) == nullptr) - { - DestroyWindow(window); - UnregisterClass(windowClass.lpszClassName, windowClass.hInstance); - return Status::UnknownError; - } - - Microsoft::WRL::ComPtr factory; - if (reinterpret_cast(CreateDXGIFactory)(IID_PPV_ARGS(&factory)) < 0) - { - DestroyWindow(window); - UnregisterClass(windowClass.lpszClassName, windowClass.hInstance); - return Status::UnknownError; - } - - Microsoft::WRL::ComPtr adapter; - if (factory->EnumAdapters(0, &adapter) == DXGI_ERROR_NOT_FOUND) - { - DestroyWindow(window); - UnregisterClass(windowClass.lpszClassName, windowClass.hInstance); - return Status::UnknownError; - } - - void* D3D12CreateDevice; - if ((D3D12CreateDevice = reinterpret_cast(GetProcAddress(libD3D12, "D3D12CreateDevice"))) == nullptr) - { - DestroyWindow(window); - UnregisterClass(windowClass.lpszClassName, windowClass.hInstance); - return Status::UnknownError; - } - - Microsoft::WRL::ComPtr device; - if (reinterpret_cast(D3D12CreateDevice)(adapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&device)) < 0) - { - DestroyWindow(window); - UnregisterClass(windowClass.lpszClassName, windowClass.hInstance); - return Status::UnknownError; - } - - D3D12_COMMAND_QUEUE_DESC queueDesc; - queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT; - queueDesc.Priority = 0; - queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE; - queueDesc.NodeMask = 0; - - Microsoft::WRL::ComPtr commandQueue; - if (device->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&commandQueue)) < 0) - { - DestroyWindow(window); - UnregisterClass(windowClass.lpszClassName, windowClass.hInstance); - return Status::UnknownError; - } - - Microsoft::WRL::ComPtr commandAllocator; - if (device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&commandAllocator)) < 0) - { - DestroyWindow(window); - UnregisterClass(windowClass.lpszClassName, windowClass.hInstance); - return Status::UnknownError; - } - - Microsoft::WRL::ComPtr commandList; - if (device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, commandAllocator.Get(), nullptr, IID_PPV_ARGS(&commandList)) < 0) - { - DestroyWindow(window); - UnregisterClass(windowClass.lpszClassName, windowClass.hInstance); - return Status::UnknownError; - } - - Microsoft::WRL::ComPtr downlevelDevice; - g_isDownLevelDevice = device->QueryInterface(IID_PPV_ARGS(&downlevelDevice)) >= 0; - Microsoft::WRL::ComPtr swapChain; - Microsoft::WRL::ComPtr commandQueueDownlevel; - - if (!g_isDownLevelDevice) - { - DXGI_RATIONAL refreshRate; - refreshRate.Numerator = 60; - refreshRate.Denominator = 1; - - DXGI_MODE_DESC bufferDesc; - bufferDesc.Width = 100; - bufferDesc.Height = 100; - bufferDesc.RefreshRate = refreshRate; - bufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; - bufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED; - bufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; - - DXGI_SAMPLE_DESC sampleDesc; - sampleDesc.Count = 1; - sampleDesc.Quality = 0; - - DXGI_SWAP_CHAIN_DESC swapChainDesc = {}; - swapChainDesc.BufferDesc = bufferDesc; - swapChainDesc.SampleDesc = sampleDesc; - swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; - swapChainDesc.BufferCount = 2; - swapChainDesc.OutputWindow = window; - swapChainDesc.Windowed = 1; - swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD; - swapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; - - Microsoft::WRL::ComPtr swapChain1; - if (factory->CreateSwapChain(commandQueue.Get(), &swapChainDesc, &swapChain1) < 0) - { - DestroyWindow(window); - UnregisterClass(windowClass.lpszClassName, windowClass.hInstance); - return Status::UnknownError; - } - - if (FAILED(swapChain1->QueryInterface(IID_PPV_ARGS(&swapChain)))) - { - DestroyWindow(window); - UnregisterClass(windowClass.lpszClassName, windowClass.hInstance); - return Status::UnknownError; - } - - auto valueToFind = reinterpret_cast(static_cast(commandQueue.Get())); - auto* swapChainPtr = static_cast(static_cast(swapChain.Get())); - auto* addr = std::find(swapChainPtr, swapChainPtr + 512, valueToFind); - - g_commandQueueOffset = reinterpret_cast(addr) - reinterpret_cast(swapChainPtr); - } - else - { - if (commandQueue->QueryInterface(IID_PPV_ARGS(&commandQueueDownlevel)) < 0) - { - DestroyWindow(window); - UnregisterClass(windowClass.lpszClassName, windowClass.hInstance); - return Status::UnknownError; - } - - auto* commandQueueDownlevelPtr = static_cast(static_cast(commandQueueDownlevel.Get())); - auto* addr = std::find(commandQueueDownlevelPtr, commandQueueDownlevelPtr + 512, reinterpret_cast(static_cast(commandQueue.Get()))); - - g_commandQueueOffset = reinterpret_cast(addr) - reinterpret_cast(commandQueueDownlevelPtr); - } - - g_methodsTable = static_cast(std::calloc(176, sizeof(uint150_t))); - - if (!g_isDownLevelDevice) - g_swapChainVtable = *reinterpret_cast(swapChain.Get()); - - g_commandListVtable = *reinterpret_cast(commandList.Get()); - g_commandQueueVtable = *reinterpret_cast(commandQueue.Get()); - - std::memcpy(g_methodsTable, *static_cast(static_cast(device.Get())), 44 * sizeof(uint150_t)); - std::memcpy(g_methodsTable + 44, *static_cast(static_cast(commandQueue.Get())), 19 * sizeof(uint150_t)); - std::memcpy(g_methodsTable + 44 + 19, *static_cast(static_cast(commandAllocator.Get())), 9 * sizeof(uint150_t)); - std::memcpy(g_methodsTable + 44 + 19 + 9, *static_cast(static_cast(commandList.Get())), 60 * sizeof(uint150_t)); - if (!g_isDownLevelDevice) - std::memcpy(g_methodsTable + 44 + 19 + 9 + 60, *static_cast(static_cast(swapChain.Get())), 40 * sizeof(uint150_t)); - else - std::memcpy(g_methodsTable + 44 + 19 + 9 + 60 + 40, *static_cast(static_cast(commandQueueDownlevel.Get())), 4 * sizeof(uint150_t)); - - DestroyWindow(window); - UnregisterClass(windowClass.lpszClassName, windowClass.hInstance); - - g_kieroInitialized = true; - return Status::Success; -} - -void kiero::shutdown() -{ - if (g_kieroInitialized) - { - MH_DisableHook(nullptr); - - std::free(g_methodsTable); - g_methodsTable = nullptr; - g_kieroInitialized = false; - } -} - -kiero::Status::Enum kiero::bind(uint16_t _index, void** _original, void* _function) -{ - // TODO: Need own detour function - - assert(_original != NULL && _function != NULL); - - if (g_kieroInitialized) - { - auto* target = reinterpret_cast(g_methodsTable[_index]); - if (MH_CreateHook(target, _function, _original) != MH_OK || MH_EnableHook(target) != MH_OK) - return Status::UnknownError; - - return Status::Success; - } - - return Status::NotInitializedError; -} - -void kiero::unbind(uint16_t _index) -{ - if (g_kieroInitialized) - { - auto* target = (void*)g_methodsTable[_index]; - MH_DisableHook(target); - MH_RemoveHook(target); - } -} - -void** kiero::getSwapChainVtable() -{ - return g_swapChainVtable; -} - -void** kiero::getCommandListVtable() -{ - return g_commandListVtable; -} - -void** kiero::getCommandQueueVtable() -{ - return g_commandQueueVtable; -} - -uint150_t* kiero::getMethodsTable() -{ - return g_methodsTable; -} - -uintptr_t kiero::getCommandQueueOffset() -{ - return g_commandQueueOffset; -} - -bool kiero::isDownLevelDevice() -{ - return g_isDownLevelDevice; -} diff --git a/src/kiero/kiero.h b/src/kiero/kiero.h deleted file mode 100644 index 79587e94..00000000 --- a/src/kiero/kiero.h +++ /dev/null @@ -1,51 +0,0 @@ -#pragma once - -#include - -#define KIERO_VERSION "1.2.10-cet_1.0" - -#if defined(_M_X64) -#define KIERO_ARCH_X86 0 -#define KIERO_ARCH_X64 1 -#else -#define KIERO_ARCH_X86 1 -#define KIERO_ARCH_X64 0 -#endif - -#if KIERO_ARCH_X64 -typedef uint64_t uint150_t; -#else -typedef uint32_t uint150_t; -#endif - -namespace kiero -{ -struct Status -{ - enum Enum - { - UnknownError = -1, - NotSupportedError = -2, - ModuleNotFoundError = -3, - - AlreadyInitializedError = -4, - NotInitializedError = -5, - - Success = 0, - }; -}; - -Status::Enum init(); -void shutdown(); - -Status::Enum bind(uint16_t index, void** original, void* function); -void unbind(uint16_t index); - -void** getSwapChainVtable(); -void** getCommandListVtable(); -void** getCommandQueueVtable(); - -uint150_t* getMethodsTable(); -uintptr_t getCommandQueueOffset(); -bool isDownLevelDevice(); -} // namespace kiero \ No newline at end of file