Skip to content

Commit

Permalink
Minor Vulkan & DX12 stuff + graphics class and function (#23)
Browse files Browse the repository at this point in the history
* Misc

* Update main.cpp

* improvements

* Error checking

* CI fix

* Is this okay?

* Add classes and a helper function
  • Loading branch information
MarkEcza authored Oct 5, 2023
1 parent 9a2b8b2 commit 1b942a0
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 40 deletions.
84 changes: 49 additions & 35 deletions src/core/renderer/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace YimMenu
}
else if (!Pointers.IsVulkan)
{
WaitForLastFrame();
ImGui_ImplDX12_Shutdown();
}

Expand Down Expand Up @@ -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<const char*> InstanceExtensions = {"VK_KHR_surface" };

const std::vector<const char*> 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)
{
Expand All @@ -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<VkPhysicalDevice> 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);
Expand All @@ -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<void*>(vkGetDeviceProcAddr(m_VkFakeDevice, "vkQueuePresentKHR"));
Pointers.CreateSwapchainKHR = reinterpret_cast<void*>(vkGetDeviceProcAddr(m_VkFakeDevice, "vkCreateSwapchainKHR"));
Pointers.AcquireNextImageKHR = reinterpret_cast<void*>(vkGetDeviceProcAddr(m_VkFakeDevice, "vkAcquireNextImageKHR"));
Expand All @@ -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)
Expand Down Expand Up @@ -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())
{
Expand Down Expand Up @@ -735,6 +753,7 @@ namespace YimMenu
}
}


bool Renderer::InitImpl()
{
if (Pointers.IsVulkan)
Expand All @@ -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();
}

Expand Down Expand Up @@ -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()] };
Expand Down
21 changes: 21 additions & 0 deletions src/core/renderer/Renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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)
Expand Down
12 changes: 8 additions & 4 deletions src/game/hooks/GUI/Vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ namespace YimMenu::Hooks
VkResult VKAPI_CALL Vulkan::QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo)
{
Renderer::VkOnPresent(queue, pPresentInfo);

return BaseHook::Get<Vulkan::QueuePresentKHR, DetourHook<decltype(&QueuePresentKHR)>>()->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<Vulkan::CreateSwapchainKHR, DetourHook<decltype(&CreateSwapchainKHR)>>()->Original()(device, pCreateInfo, pAllocator, pSwapchain);
}

Expand Down
27 changes: 27 additions & 0 deletions src/game/pointers/Pointers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "core/memory/PatternScanner.hpp"
#include "util/Joaat.hpp"
#include "core/renderer/Renderer.hpp"
#include "util/GraphicsValue.hpp"

namespace YimMenu
{
Expand Down Expand Up @@ -35,6 +36,32 @@ namespace YimMenu
GetRendererInfo = ptr.Add(1).Rip().As<Functions::GetRendererInfo>();
});

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<GraphicsOptions*>();

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<PVOID>();
Expand Down
8 changes: 8 additions & 0 deletions src/game/pointers/Pointers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
#include <dxgi1_4.h>
#include <d3d12.h>
#include "game/rdr/RenderingInfo.hpp"
#include "game/rdr/GraphicsOptions.hpp"
#include <script/scrNativeHandler.hpp>
#include <rage/atArray.hpp>
#include <vulkan/vulkan.h>


namespace rage
{
class scrThread;
Expand Down Expand Up @@ -46,9 +48,15 @@ namespace YimMenu

//Misc Renderer Related
HWND Hwnd;

Functions::GetRendererInfo GetRendererInfo;
GraphicsOptions GameGraphicsOptions;

PVOID WndProc;
BOOL IsVulkan;

uint32_t ScreenResX;
uint32_t ScreenResY;
};

struct Pointers : PointerData
Expand Down
47 changes: 47 additions & 0 deletions src/game/rdr/GraphicsOptions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#pragma once
#include <cstdint>

class GraphicsOptions
{
public:
char m_padding_0[0x8];
int gfx_adv_shadowSoftShadows; // 8
char m_padding_1[0x4];
bool m_motion_blur;
char m_padding_12[0x3];
int gfx_adv_particleLightingQuality;
char m_padding_2[0x4];
int gfx_adv_waterRefractionQuality;
int gfx_adv_waterReflectionQuality;
char m_padding_3[0x4];
int gfx_adv_waterLightingQuality;
int gfx_adv_furDisplayQuality;
char m_padding_4[0x4];
int gfx_adv_shadowGrassShadows;
char m_padding_5[0x10];
int gfx_adv_scatteringVolumeQuality;
int gfx_adv_volumetricsRaymarchQuality;
int gfx_adv_volumetricsLightingQuality;
char m_padding_6[0x4];
int gfx_adv_terrainShadowQuality;
char m_padding_7[0x58];
int m_gfx_tessellation;
int m_gfx_shadowQuality;
int m_gfx_farShadowQuality;
int m_gfx_reflectionQuality;
int m_gfx_mirrorQuality;
int m_gfx_ssao;
int m_gfx_textureQuality;
int m_gfx_particleQuality;
int m_gfx_waterQuality;
int m_gfx_volumetricsQuality;
int m_gfx_lightingQuality;
int m_gfx_ambientLightingQuality;
char m_padding_8[0x28];
bool m_hdr;
char m_padding_9[0x2F];
uint32_t m_screen_resolution_x;
uint32_t m_screen_resolution_y;
char m_padding_10[0x4];
bool m_unk;
};
8 changes: 7 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ namespace YimMenu

while (g_Running)
{
std::this_thread::sleep_for(100ms);
//Needed incase UI is malfunctioning or for emergencies
if (GetAsyncKeyState(VK_DELETE) & 0x8000)
{
g_Running = false;
}

std::this_thread::sleep_for(500ms);
}

LOG(INFO) << "Unloading";
Expand Down
25 changes: 25 additions & 0 deletions src/util/GraphicsValue.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once
#include <cstdint>

namespace YimMenu
{
inline const char* GetGraphicsValue(int value)
{
const char* ret = "Unkown";

switch (value)
{
case 0: ret = "Low"; break;
case 1: ret = "Medium"; break;
case 2: ret = "High"; break;
case 3: ret = "Ultra"; break;
case 4: ret = "Custom"; break;
case 5: ret = "Special"; break;
default: ret = "Default"; break;
}

return ret;
}


}

0 comments on commit 1b942a0

Please sign in to comment.