Skip to content

Commit

Permalink
Plugins: Add various render target functions
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Feb 22, 2024
1 parent c25695a commit aab171b
Show file tree
Hide file tree
Showing 16 changed files with 250 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,9 @@ list(APPEND uevr_SOURCES
"src/mods/PluginLoader.cpp"
"src/mods/UObjectHook.cpp"
"src/mods/VR.cpp"
"src/mods/pluginloader/FFakeStereoRenderingFunctions.cpp"
"src/mods/pluginloader/FRHITexture2DFunctions.cpp"
"src/mods/pluginloader/FRenderTargetPoolHook.cpp"
"src/mods/uobjecthook/SDKDumper.cpp"
"src/mods/vr/Bindings.cpp"
"src/mods/vr/CVarManager.cpp"
Expand Down Expand Up @@ -701,6 +704,9 @@ list(APPEND uevr_SOURCES
"src/mods/PluginLoader.hpp"
"src/mods/UObjectHook.hpp"
"src/mods/VR.hpp"
"src/mods/pluginloader/FFakeStereoRenderingFunctions.hpp"
"src/mods/pluginloader/FRHITexture2DFunctions.hpp"
"src/mods/pluginloader/FRenderTargetPoolHook.hpp"
"src/mods/uobjecthook/SDKDumper.hpp"
"src/mods/vr/CVarManager.hpp"
"src/mods/vr/D3D11Component.hpp"
Expand Down
22 changes: 21 additions & 1 deletion include/uevr/API.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ SOFTWARE.
#define UEVR_OUT

#define UEVR_PLUGIN_VERSION_MAJOR 2
#define UEVR_PLUGIN_VERSION_MINOR 13
#define UEVR_PLUGIN_VERSION_MINOR 14
#define UEVR_PLUGIN_VERSION_PATCH 0

#define UEVR_RENDERER_D3D11 0
Expand Down Expand Up @@ -74,6 +74,7 @@ DECLARE_UEVR_HANDLE(UEVR_IConsoleCommandHandle);
DECLARE_UEVR_HANDLE(UEVR_IConsoleVariableHandle);
DECLARE_UEVR_HANDLE(UEVR_TArrayHandle);
DECLARE_UEVR_HANDLE(UEVR_FMallocHandle);
DECLARE_UEVR_HANDLE(UEVR_FRHITexture2DHandle);

/* OpenXR stuff */
DECLARE_UEVR_HANDLE(UEVR_XrInstance);
Expand Down Expand Up @@ -342,6 +343,22 @@ typedef struct {
void (*free)(UEVR_FMallocHandle instance, void* ptr);
} UEVR_FMallocFunctions;

DECLARE_UEVR_HANDLE(UEVR_IPooledRenderTargetHandle);

typedef struct {
void (*activate)();
UEVR_IPooledRenderTargetHandle (*get_render_target)(const wchar_t* name);
} UEVR_FRenderTargetPoolHookFunctions;

typedef struct {
UEVR_FRHITexture2DHandle (*get_scene_render_target)();
UEVR_FRHITexture2DHandle (*get_ui_render_target)();
} UEVR_FFakeStereoRenderingHookFunctions;

typedef struct {
void* (*get_native_resource)(UEVR_FRHITexture2DHandle texture);
} UEVR_FRHITexture2DFunctions;

typedef struct {
const UEVR_SDKFunctions* functions;
const UEVR_SDKCallbacks* callbacks;
Expand All @@ -357,6 +374,9 @@ typedef struct {
const UEVR_FNameFunctions* fname;
const UEVR_ConsoleFunctions* console;
const UEVR_FMallocFunctions* malloc;
const UEVR_FRenderTargetPoolHookFunctions* render_target_pool_hook;
const UEVR_FFakeStereoRenderingHookFunctions* stereo_hook;
const UEVR_FRHITexture2DFunctions* frhitexture2d;
} UEVR_SDKData;

DECLARE_UEVR_HANDLE(UEVR_IVRSystem);
Expand Down
66 changes: 66 additions & 0 deletions include/uevr/API.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ class API {
struct IConsoleCommand;
struct ConsoleObjectElement;
struct UObjectHook;
struct FRHITexture2D;
struct IPooledRenderTarget;

template<typename T>
struct TArray;
Expand Down Expand Up @@ -741,6 +743,26 @@ class API {
}
};

struct FRHITexture2D {
inline UEVR_FRHITexture2DHandle to_handle() { return (UEVR_FRHITexture2DHandle)this; }
inline UEVR_FRHITexture2DHandle to_handle() const { return (UEVR_FRHITexture2DHandle)this; }

void* get_native_resource() const {
static const auto fn = initialize()->get_native_resource;
return fn(to_handle());
}

private:
static inline const UEVR_FRHITexture2DFunctions* s_functions{nullptr};
static inline const UEVR_FRHITexture2DFunctions* initialize() {
if (s_functions == nullptr) {
s_functions = API::get()->sdk()->frhitexture2d;
}

return s_functions;
}
};

public:
// UEVR specific stuff
struct UObjectHook {
Expand Down Expand Up @@ -831,6 +853,50 @@ class API {
}
};

struct RenderTargetPoolHook {
static void activate() {
static const auto fn = initialize()->activate;
fn();
}

static IPooledRenderTarget* get_render_target(const wchar_t* name) {
static const auto fn = initialize()->get_render_target;
return (IPooledRenderTarget*)fn(name);
}

private:
static inline const UEVR_FRenderTargetPoolHookFunctions* s_functions{nullptr};
static inline const UEVR_FRenderTargetPoolHookFunctions* initialize() {
if (s_functions == nullptr) {
s_functions = API::get()->sdk()->render_target_pool_hook;
}

return s_functions;
}
};

struct StereoHook {
static FRHITexture2D* get_scene_render_target() {
static const auto fn = initialize()->get_scene_render_target;
return (FRHITexture2D*)fn();
}

static FRHITexture2D* get_ui_render_target() {
static const auto fn = initialize()->get_ui_render_target;
return (FRHITexture2D*)fn();
}

private:
static inline const UEVR_FFakeStereoRenderingHookFunctions* s_functions{nullptr};
static inline const UEVR_FFakeStereoRenderingHookFunctions* initialize() {
if (s_functions == nullptr) {
s_functions = API::get()->sdk()->stereo_hook;
}

return s_functions;
}
};

private:
const UEVR_PluginInitializeParam* m_param;
const UEVR_SDKData* m_sdk;
Expand Down
4 changes: 2 additions & 2 deletions src/CommitHash.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma once
#define UEVR_COMMIT_HASH "f48ed675e2c1f8730b2a7ebfb9a1981c9a6ebf3d"
#define UEVR_BUILD_DATE "20.02.2024"
#define UEVR_COMMIT_HASH "c25695a8c0ec35b556229046cc7011e27f4d42ee"
#define UEVR_BUILD_DATE "22.02.2024"
#define UEVR_BUILD_TIME "00:00"
9 changes: 8 additions & 1 deletion src/mods/PluginLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
#include <sdk/APlayerController.hpp>
#include <sdk/USceneComponent.hpp>

#include "pluginloader/FFakeStereoRenderingFunctions.hpp"
#include "pluginloader/FRenderTargetPoolHook.hpp"
#include "pluginloader/FRHITexture2DFunctions.hpp"

#include "UObjectHook.hpp"
#include "VR.hpp"

Expand Down Expand Up @@ -775,7 +779,10 @@ UEVR_SDKData g_sdk_data {
&g_ffield_class_functions,
&g_fname_functions,
&g_console_functions,
&g_malloc_functions
&g_malloc_functions,
&uevr::render_target_pool_hook::functions,
&uevr::stereo_hook::functions,
&uevr::frhitexture2d::functions
};

namespace uevr {
Expand Down
10 changes: 9 additions & 1 deletion src/mods/VR.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ class VR : public Mod {
return m_lowest_xinput_user_index;
}

auto& get_render_target_pool_hook() {
auto& get_render_target_pool_hook() const {
return m_render_target_pool_hook;
}

Expand Down Expand Up @@ -577,6 +577,14 @@ class VR : public Mod {
return m_extreme_compat_mode->value();
}

vrmod::D3D11Component& d3d11() {
return m_d3d11;
}

vrmod::D3D12Component& d3d12() {
return m_d3d12;
}

private:
Vector4f get_position_unsafe(uint32_t index) const;
Vector4f get_velocity_unsafe(uint32_t index) const;
Expand Down
35 changes: 35 additions & 0 deletions src/mods/pluginloader/FFakeStereoRenderingFunctions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "uevr/API.h"

#include "mods/VR.hpp"
#include "FFakeStereoRenderingFunctions.hpp"

namespace uevr {
UEVR_FRHITexture2DHandle stereo_hook::get_scene_render_target() {
const auto& vr = VR::get();
if (auto& hook = vr->get_fake_stereo_hook(); hook != nullptr) {
auto rtm = hook->get_render_target_manager();
if (auto rtm = hook->get_render_target_manager(); rtm != nullptr) {
return (UEVR_FRHITexture2DHandle)rtm->get_render_target();
}
}

return nullptr;
}

UEVR_FRHITexture2DHandle stereo_hook::get_ui_render_target() {
const auto& vr = VR::get();
if (auto& hook = vr->get_fake_stereo_hook(); hook != nullptr) {
auto rtm = hook->get_render_target_manager();
if (auto rtm = hook->get_render_target_manager(); rtm != nullptr) {
return (UEVR_FRHITexture2DHandle)rtm->get_ui_target();
}
}

return nullptr;
}

UEVR_FFakeStereoRenderingHookFunctions stereo_hook::functions {
&stereo_hook::get_scene_render_target,
&stereo_hook::get_ui_render_target
};
}
12 changes: 12 additions & 0 deletions src/mods/pluginloader/FFakeStereoRenderingFunctions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include "uevr/API.h"

namespace uevr {
namespace stereo_hook {
UEVR_FRHITexture2DHandle get_scene_render_target();
UEVR_FRHITexture2DHandle get_ui_render_target();

extern UEVR_FFakeStereoRenderingHookFunctions functions;
}
}
21 changes: 21 additions & 0 deletions src/mods/pluginloader/FRHITexture2DFunctions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <sdk/StereoStuff.hpp>

#include "FRHITexture2DFunctions.hpp"

namespace uevr {
namespace frhitexture2d {
void* get_native_resource(UEVR_FRHITexture2DHandle handle) {
const auto texture = (::FRHITexture2D*)handle;

if (texture == nullptr) {
return nullptr;
}

return texture->get_native_resource();
}

UEVR_FRHITexture2DFunctions functions {
&uevr::frhitexture2d::get_native_resource
};
}
}
11 changes: 11 additions & 0 deletions src/mods/pluginloader/FRHITexture2DFunctions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include "uevr/API.h"

namespace uevr {
namespace frhitexture2d {
void* get_native_resource(UEVR_FRHITexture2DHandle handle);

extern UEVR_FRHITexture2DFunctions functions;
}
}
25 changes: 25 additions & 0 deletions src/mods/pluginloader/FRenderTargetPoolHook.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "../mods/VR.hpp"
#include "FRenderTargetPoolHook.hpp"

namespace uevr {
void render_target_pool_hook::activate() {
const auto& vr = VR::get();
if (auto& hook = vr->get_render_target_pool_hook(); hook != nullptr) {
hook->activate();
}
}

UEVR_IPooledRenderTargetHandle render_target_pool_hook::get_render_target(const wchar_t* name) {
const auto& vr = VR::get();
if (auto& hook = vr->get_render_target_pool_hook(); hook != nullptr) {
return (UEVR_IPooledRenderTargetHandle)hook->get_render_target(name);
}

return nullptr;
}

UEVR_FRenderTargetPoolHookFunctions render_target_pool_hook::functions {
&render_target_pool_hook::activate,
&render_target_pool_hook::get_render_target
};
}
11 changes: 11 additions & 0 deletions src/mods/pluginloader/FRenderTargetPoolHook.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include "uevr/API.h"

namespace uevr {
namespace render_target_pool_hook {
void activate();
UEVR_IPooledRenderTargetHandle get_render_target(const wchar_t* name);
extern UEVR_FRenderTargetPoolHookFunctions functions;
}
}
6 changes: 6 additions & 0 deletions src/mods/vr/D3D11Component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,12 @@ bool D3D11Component::clear_tex(ID3D11Resource* tex, std::optional<DXGI_FORMAT> f
return ctx.clear_rtv(color);
}

bool D3D11Component::clear_tex(ID3D11Resource* tex, float* color, std::optional<DXGI_FORMAT> format) {
auto ctx = TextureContext{tex, format};

return ctx.clear_rtv(color);
}

void D3D11Component::copy_tex(ID3D11Resource* src, ID3D11Resource* dst) {
auto& hook = g_framework->get_d3d11_hook();
auto device = hook->get_device();
Expand Down
1 change: 1 addition & 0 deletions src/mods/vr/D3D11Component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class D3D11Component {
auto& get_ui_tex() { return m_ui_tex; }

bool clear_tex(ID3D11Resource* rsrc, std::optional<DXGI_FORMAT> format = std::nullopt);
bool clear_tex(ID3D11Resource* rsrc, float* color, std::optional<DXGI_FORMAT> format = std::nullopt);
void copy_tex(ID3D11Resource* src, ID3D11Resource* dst);

void force_reset() { m_force_reset = true; }
Expand Down
16 changes: 11 additions & 5 deletions src/mods/vr/RenderTargetPoolHook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ RenderTargetPoolHook::RenderTargetPoolHook() {

void RenderTargetPoolHook::on_pre_engine_tick(sdk::UGameEngine* engine, float delta) {
if (!m_attempted_hook && VR::get()->is_depth_enabled()) {
m_wants_activate = true;
}

if (!m_attempted_hook && m_wants_activate) {
m_attempted_hook = true;
m_hooked = hook();
}
Expand Down Expand Up @@ -66,7 +70,7 @@ bool RenderTargetPoolHook::find_free_element_hook(
// Right now we are only using this for depth
// and on some games it will crash if we mess with anything
// so, TODO: fix the games that crash with depth enabled
if (!VR::get()->is_depth_enabled()) {
if (!g_hook->m_wants_activate) {
std::scoped_lock _{g_hook->m_mutex};
g_hook->m_render_targets.clear();
return result;
Expand All @@ -77,11 +81,13 @@ bool RenderTargetPoolHook::find_free_element_hook(

if (out != nullptr) {
std::scoped_lock _{g_hook->m_mutex};
g_hook->m_render_targets[name] = out->reference;

/*if (out->reference != nullptr && out->reference->item.texture.texture != nullptr) {
const auto resource = out->reference->item.texture.texture->get_native_resource();
}*/
if (!g_hook->m_seen_names.contains(name)) {
g_hook->m_seen_names.insert(name);
SPDLOG_INFO("FRenderTargetPool::FindFreeElement called with name {}", utility::narrow(name));
}

g_hook->m_render_targets[name] = out->reference;
}
}

Expand Down
Loading

0 comments on commit aab171b

Please sign in to comment.