From 1b942a03d4dfe489640a13b0ea49483c8fbec820 Mon Sep 17 00:00:00 2001 From: Marcezsa <85032105+Hydra-Source@users.noreply.github.com> Date: Thu, 5 Oct 2023 18:07:56 +0200 Subject: [PATCH] Minor Vulkan & DX12 stuff + graphics class and function (#23) * Misc * Update main.cpp * improvements * Error checking * CI fix * Is this okay? * Add classes and a helper function --- src/core/renderer/Renderer.cpp | 84 +++++++++++++++++++------------- src/core/renderer/Renderer.hpp | 21 ++++++++ src/game/hooks/GUI/Vulkan.cpp | 12 +++-- src/game/pointers/Pointers.cpp | 27 ++++++++++ src/game/pointers/Pointers.hpp | 8 +++ src/game/rdr/GraphicsOptions.hpp | 47 ++++++++++++++++++ src/main.cpp | 8 ++- src/util/GraphicsValue.hpp | 25 ++++++++++ 8 files changed, 192 insertions(+), 40 deletions(-) create mode 100644 src/game/rdr/GraphicsOptions.hpp create mode 100644 src/util/GraphicsValue.hpp diff --git a/src/core/renderer/Renderer.cpp b/src/core/renderer/Renderer.cpp index 66d341c7..74a186a7 100644 --- a/src/core/renderer/Renderer.cpp +++ b/src/core/renderer/Renderer.cpp @@ -35,6 +35,7 @@ namespace YimMenu } else if (!Pointers.IsVulkan) { + WaitForLastFrame(); ImGui_ImplDX12_Shutdown(); } @@ -191,19 +192,28 @@ namespace YimMenu bool Renderer::InitVulkan() { VkInstanceCreateInfo CreateInfo = {}; - constexpr const char* InstanceExtension = "VK_KHR_surface"; - CreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; - CreateInfo.enabledExtensionCount = 1; - CreateInfo.ppEnabledExtensionNames = &InstanceExtension; - - // Create Vulkan Instance without any debug feature + const std::vector InstanceExtensions = {"VK_KHR_surface" }; + + const std::vector ValidationLayers = { + "VK_LAYER_KHRONOS_validation" + }; + + CreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; + CreateInfo.enabledExtensionCount = (uint32_t)InstanceExtensions.size(); + CreateInfo.ppEnabledExtensionNames = InstanceExtensions.data(); + + // CreateInfo.enabledLayerCount = (uint32_t)ValidationLayers.size(); + // CreateInfo.ppEnabledLayerNames = ValidationLayers.data(); + + // Create Vulkan Instance without debug feature if (const VkResult result = vkCreateInstance(&CreateInfo, m_VkAllocator, &m_VkInstance); result != VK_SUCCESS) { LOG(WARNING) << "vkCreateInstance failed with result: [" << result << "]"; return false; } +; uint32_t GpuCount; if (const VkResult result = vkEnumeratePhysicalDevices(m_VkInstance, &GpuCount, NULL); result != VK_SUCCESS) { @@ -213,27 +223,36 @@ namespace YimMenu IM_ASSERT(GpuCount > 0); - VkPhysicalDevice* Gpus = new VkPhysicalDevice[sizeof(VkPhysicalDevice) * GpuCount]; - if (const VkResult result = vkEnumeratePhysicalDevices(m_VkInstance, &GpuCount, Gpus); result != VK_SUCCESS) + ImVector GpuArr; + GpuArr.resize(GpuCount); + + if (const VkResult result = vkEnumeratePhysicalDevices(m_VkInstance, &GpuCount, GpuArr.Data); result != VK_SUCCESS) { LOG(WARNING) << "vkEnumeratePhysicalDevices 2 failed with result: [" << result << "]"; return false; } - int UseGpu = 0; - for (int i = 0; i < (int)GpuCount; ++i) + VkPhysicalDevice MainGPU = nullptr; + for (const auto& Gpu : GpuArr) { VkPhysicalDeviceProperties Properties; - vkGetPhysicalDeviceProperties(Gpus[i], &Properties); + vkGetPhysicalDeviceProperties(Gpu, &Properties); if (Properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) { - UseGpu = i; + LOG(INFO) << "Vulkan - Using GPU: " << Properties.deviceName; + + MainGPU = Gpu; break; } } - LOG(INFO) << "Vulkan - Using GPU: " << UseGpu; - m_VkPhysicalDevice = Gpus[0]; + if (!MainGPU) + { + LOG(INFO) << "Failed to get main GPU!"; + return false; + } + + m_VkPhysicalDevice = MainGPU; uint32_t Count; vkGetPhysicalDeviceQueueFamilyProperties(m_VkPhysicalDevice, &Count, NULL); @@ -247,31 +266,31 @@ namespace YimMenu break; } } + IM_ASSERT(m_VkQueueFamily != (uint32_t)-1); constexpr const char* DeviceExtension = "VK_KHR_swapchain"; constexpr const float QueuePriority = 1.0f; - VkDeviceQueueCreateInfo queue_info = {}; - queue_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; - queue_info.queueFamilyIndex = m_VkQueueFamily; - queue_info.queueCount = 1; - queue_info.pQueuePriorities = &QueuePriority; + VkDeviceQueueCreateInfo DeviceQueueInfo = {}; + DeviceQueueInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; + DeviceQueueInfo.queueFamilyIndex = m_VkQueueFamily; + DeviceQueueInfo.queueCount = 1; + DeviceQueueInfo.pQueuePriorities = &QueuePriority; - VkDeviceCreateInfo create_info = {}; - create_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; - create_info.queueCreateInfoCount = 1; - create_info.pQueueCreateInfos = &queue_info; - create_info.enabledExtensionCount = 1; - create_info.ppEnabledExtensionNames = &DeviceExtension; + VkDeviceCreateInfo DeviceCreateInfo = {}; + DeviceCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; + DeviceCreateInfo.queueCreateInfoCount = 1; + DeviceCreateInfo.pQueueCreateInfos = &DeviceQueueInfo; + DeviceCreateInfo.enabledExtensionCount = 1; + DeviceCreateInfo.ppEnabledExtensionNames = &DeviceExtension; - if (const VkResult result = vkCreateDevice(m_VkPhysicalDevice, &create_info, m_VkAllocator, &m_VkFakeDevice); result != VK_SUCCESS) + if (const VkResult result = vkCreateDevice(m_VkPhysicalDevice, &DeviceCreateInfo, m_VkAllocator, &m_VkFakeDevice); result != VK_SUCCESS) { LOG(WARNING) << "Fake vkCreateDevice failed with result: [" << result << "]"; return false; } - //Cursed place to do it. Pointers.QueuePresentKHR = reinterpret_cast(vkGetDeviceProcAddr(m_VkFakeDevice, "vkQueuePresentKHR")); Pointers.CreateSwapchainKHR = reinterpret_cast(vkGetDeviceProcAddr(m_VkFakeDevice, "vkCreateSwapchainKHR")); Pointers.AcquireNextImageKHR = reinterpret_cast(vkGetDeviceProcAddr(m_VkFakeDevice, "vkAcquireNextImageKHR")); @@ -282,7 +301,7 @@ namespace YimMenu LOG(INFO) << "Vulkan renderer has finished initializing."; - return true; //I guess? + return true; } void Renderer::VkCreateRenderTarget(VkDevice Device, VkSwapchainKHR Swapchain) @@ -502,7 +521,6 @@ namespace YimMenu } } - //Fucked. //Reason we have to rescan is when window is resized the HWND changes and Vulkan ImGui does not like this at all. (Grabbing from IDXGISwapchain does not work, it simply doesn't update or is too slow in my testing. Feel free) if (IsResizing()) { @@ -735,6 +753,7 @@ namespace YimMenu } } + bool Renderer::InitImpl() { if (Pointers.IsVulkan) @@ -744,7 +763,7 @@ namespace YimMenu } else if (!Pointers.IsVulkan) { - LOG(INFO) << "Using DX12"; + LOG(INFO) << "Using DX12, clear shader cache if your having issues."; return InitDX12(); } @@ -863,11 +882,6 @@ namespace YimMenu void Renderer::DX12EndFrame() { - ImGui::EndFrame(); - - UINT NextFrameIndex = GetInstance().m_FrameIndex + 1; - GetInstance().m_FrameIndex = NextFrameIndex; - WaitForNextFrame(); FrameContext& CurrentFrameContext{ GetInstance().m_FrameContext[GetInstance().m_SwapChain->GetCurrentBackBufferIndex()] }; diff --git a/src/core/renderer/Renderer.hpp b/src/core/renderer/Renderer.hpp index 596defa9..78796b3a 100644 --- a/src/core/renderer/Renderer.hpp +++ b/src/core/renderer/Renderer.hpp @@ -78,6 +78,20 @@ namespace YimMenu static void VkOnPresent(VkQueue queue, const VkPresentInfoKHR* pPresentInfo) { + if (!queue) + { + LOG(FATAL) << "Invalid Vulkan Queue!"; + + return; + } + + if (!pPresentInfo) + { + LOG(FATAL) << "Invalid Vulkan Present Info!"; + + return; + } + GetInstance().VkOnPresentImpl(queue, pPresentInfo); } @@ -106,6 +120,13 @@ namespace YimMenu static void VkSetDevice(VkDevice device) { + if (!device) + { + LOG(FATAL) << "Invalid Vulkan Device!"; + + return; + } + GetInstance().m_VkDevice = device; } static void VkSetScreenSize(VkExtent2D extent) diff --git a/src/game/hooks/GUI/Vulkan.cpp b/src/game/hooks/GUI/Vulkan.cpp index 2f27e604..b03957ce 100644 --- a/src/game/hooks/GUI/Vulkan.cpp +++ b/src/game/hooks/GUI/Vulkan.cpp @@ -8,15 +8,19 @@ namespace YimMenu::Hooks VkResult VKAPI_CALL Vulkan::QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo) { Renderer::VkOnPresent(queue, pPresentInfo); + return BaseHook::Get>()->Original()(queue, pPresentInfo); } VkResult VKAPI_CALL Vulkan::CreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkSwapchainKHR* pSwapchain) { - Renderer::SetResizing(true); - Renderer::VkCleanupRenderTarget(); - Renderer::VkSetScreenSize(pCreateInfo->imageExtent); - + if (pCreateInfo) + { + Renderer::SetResizing(true); + Renderer::VkCleanupRenderTarget(); + Renderer::VkSetScreenSize(pCreateInfo->imageExtent); + } + return BaseHook::Get>()->Original()(device, pCreateInfo, pAllocator, pSwapchain); } diff --git a/src/game/pointers/Pointers.cpp b/src/game/pointers/Pointers.cpp index e80ffd04..e28e2deb 100644 --- a/src/game/pointers/Pointers.cpp +++ b/src/game/pointers/Pointers.cpp @@ -5,6 +5,7 @@ #include "core/memory/PatternScanner.hpp" #include "util/Joaat.hpp" #include "core/renderer/Renderer.hpp" +#include "util/GraphicsValue.hpp" namespace YimMenu { @@ -35,6 +36,32 @@ namespace YimMenu GetRendererInfo = ptr.Add(1).Rip().As(); }); + constexpr auto gfxInformation = Pattern<"48 8D 0D ? ? ? ? 48 8B F8 E8 ? ? ? ? 45 33 ED 45 84 FF">("GFXInformation"); + scanner.Add(gfxInformation, [this](PointerCalculator ptr) { + auto gfx = ptr.Add(3).Rip().As(); + + if (gfx->m_hdr) + { + LOG(WARNING) << "Turn HDR off!"; + + } + + if (gfx->m_motion_blur) + { + LOG(INFO) << "Ew motion blur. Seriously?"; + } + + //LOG(INFO) << GetGraphicsValue(gfx->m_gfx_lightingQuality); example + + if (gfx->m_unk) + { + ScreenResX = gfx->m_screen_resolution_x; + ScreenResY = gfx->m_screen_resolution_y; + LOG(INFO) << "Screen Resolution: " << ScreenResX << "x" << ScreenResY; + } + + }); + constexpr auto wndProc = Pattern<"48 89 5C 24 ? 4C 89 4C 24 ? 48 89 4C 24 ? 55 56 57 41 54 41 55 41 56 41 57 48 8B EC 48 83 EC 60">("WndProc"); scanner.Add(wndProc, [this](PointerCalculator ptr) { WndProc = ptr.As(); diff --git a/src/game/pointers/Pointers.hpp b/src/game/pointers/Pointers.hpp index 1e83ad3f..042953db 100644 --- a/src/game/pointers/Pointers.hpp +++ b/src/game/pointers/Pointers.hpp @@ -3,10 +3,12 @@ #include #include #include "game/rdr/RenderingInfo.hpp" +#include "game/rdr/GraphicsOptions.hpp" #include