Skip to content

Commit

Permalink
adapt code to dxvk
Browse files Browse the repository at this point in the history
  • Loading branch information
sd805 committed Jun 6, 2022
1 parent 669bc0c commit b785862
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 30 deletions.
5 changes: 1 addition & 4 deletions L4D2VR/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include "vr.h"
#include "sdk.h"

Game *g_pGame;

// testing random hooks
// TestMeleeSwingCollision: game.g_client + 0x30C040
typedef void(__thiscall *TestMeleeSwingCollision)(void *thisptr, Vector const &vec);
Expand Down Expand Up @@ -59,8 +57,7 @@ DWORD WINAPI InitL4D2VR(HMODULE hModule)
if (!insecureEnabled)
ExitProcess(0);

Game* game = new Game();
g_pGame = game;
g_Game = new Game();

return 0;
}
Expand Down
2 changes: 2 additions & 0 deletions L4D2VR/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Game::Game()
m_Offsets = new Offsets();
m_VR = new VR(this);
m_Hooks = new Hooks(this);

m_Initialized = true;
}

void *Game::GetInterface(const char *dllname, const char *interfacename)
Expand Down
4 changes: 4 additions & 0 deletions L4D2VR/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ class IViewRender;
class IViewRender;
class CBaseEntity;

class Game;
class Offsets;
class VR;
class Hooks;

inline Game *g_Game;

struct Player
{
Expand Down Expand Up @@ -50,6 +52,8 @@ class Game
VR *m_VR = nullptr;
Hooks *m_Hooks = nullptr;

bool m_Initialized = false;

std::array<Player, 24> m_PlayersVRInfo;
int m_CurrentUsercmdID = -1;

Expand Down
20 changes: 11 additions & 9 deletions L4D2VR/hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ Hooks::Hooks(Game *game)
m_Game = game;
m_VR = m_Game->m_VR;

initDxHooks();
//initDxHooks();
initSourceHooks();

hkBeginScene.enableHook();
hkPresent.enableHook();
//hkBeginScene.enableHook();
//hkPresent.enableHook();
hkGetRenderTarget.enableHook();
hkCalcViewModelView.enableHook();
hkServerFireTerrorBullets.enableHook();
Expand Down Expand Up @@ -195,8 +195,8 @@ void __fastcall Hooks::dRenderView(void *ecx, void *edx, CViewSetup &setup, CVie

// Left eye CViewSetup
leftEyeView.x = 0;
leftEyeView.width = width_VR;
leftEyeView.height = height_VR;
leftEyeView.width = m_VR->m_RenderWidth;
leftEyeView.height = m_VR->m_RenderHeight;
leftEyeView.fov = m_VR->m_Fov;
leftEyeView.fovViewmodel = m_VR->m_Fov;
leftEyeView.m_flAspectRatio = m_VR->m_Aspect;
Expand All @@ -212,12 +212,12 @@ void __fastcall Hooks::dRenderView(void *ecx, void *edx, CViewSetup &setup, CVie
m_Game->m_EngineClient->SetViewAngles(inGameAngle);

rndrContext->SetRenderTarget(m_VR->m_LeftEyeTexture);
hkRenderView.fOriginal(ecx, leftEyeView, hudViewSetup, clearflags, whatToDraw);
hkRenderView.fOriginal(ecx, leftEyeView, hudViewSetup, nClearFlags, whatToDraw);

// Right eye CViewSetup
rightEyeView.x = 0;
rightEyeView.width = width_VR;
rightEyeView.height = height_VR;
rightEyeView.width = m_VR->m_RenderWidth;
rightEyeView.height = m_VR->m_RenderHeight;
rightEyeView.fov = m_VR->m_Fov;
rightEyeView.fovViewmodel = m_VR->m_Fov;
rightEyeView.m_flAspectRatio = m_VR->m_Aspect;
Expand All @@ -227,7 +227,9 @@ void __fastcall Hooks::dRenderView(void *ecx, void *edx, CViewSetup &setup, CVie
rightEyeView.angles = m_VR->GetViewAngle();

rndrContext->SetRenderTarget(m_VR->m_RightEyeTexture);
hkRenderView.fOriginal(ecx, rightEyeView, hudViewSetup, clearflags, whatToDraw);
hkRenderView.fOriginal(ecx, rightEyeView, hudViewSetup, nClearFlags, whatToDraw);

m_VR->m_RenderedNewFrame = true;
}

bool __fastcall Hooks::dCreateMove(void *ecx, void *edx, float flInputSampleTime, CUserCmd *cmd)
Expand Down
12 changes: 6 additions & 6 deletions L4D2VR/l4d2vr.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<TargetName>d3d9</TargetName>
<IncludePath>$(DXSDK_DIR)Include;$(SolutionDir)thirdparty\minhook\include;$(SolutionDir)thirdparty\openvr\include;$(ProjectDir)sdk;$(SolutionDir)dxvk\include;$(SolutionDir)dxvk\src\d3d9\shaders;$(SolutionDir)dxvk\include\shaders;$(ProjectDir);$(SolutionDir);$(IncludePath)</IncludePath>
<IncludePath>$(DXSDK_DIR)Include;$(SolutionDir)thirdparty\minhook\include;$(SolutionDir)thirdparty\openvr\include;$(ProjectDir)sdk;$(SolutionDir)dxvk\include;$(SolutionDir)dxvk\src\d3d9\shaders;$(SolutionDir)dxvk\include\shaders;$(ProjectDir);$(SolutionDir);$(SolutionDir)dxvk\src\d3d9;$(IncludePath)</IncludePath>
<ExecutablePath>$(ProjectDir)include;$(ExecutablePath)</ExecutablePath>
<LibraryPath>$(DXSDK_DIR)Lib\x86;$(SolutionDir)thirdparty\minhook\lib\$(Configuration);$(SolutionDir)thirdparty\openvr\lib\win32;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<TargetName>d3d9</TargetName>
<IncludePath>$(DXSDK_DIR)Include;$(SolutionDir)thirdparty\minhook\include;$(SolutionDir)thirdparty\openvr\include;$(ProjectDir)sdk;$(SolutionDir)dxvk\include;$(SolutionDir)dxvk\src\d3d9\shaders;$(SolutionDir)dxvk\include\shaders;$(ProjectDir);$(SolutionDir);$(IncludePath)</IncludePath>
<IncludePath>$(DXSDK_DIR)Include;$(SolutionDir)thirdparty\minhook\include;$(SolutionDir)thirdparty\openvr\include;$(ProjectDir)sdk;$(SolutionDir)dxvk\include;$(SolutionDir)dxvk\src\d3d9\shaders;$(SolutionDir)dxvk\include\shaders;$(ProjectDir);$(SolutionDir);$(SolutionDir)dxvk\src\d3d9;$(IncludePath)</IncludePath>
<ExecutablePath>$(ProjectDir)include;$(ExecutablePath)</ExecutablePath>
<LibraryPath>$(DXSDK_DIR)Lib\x86;$(SolutionDir)thirdparty\minhook\lib\$(Configuration);$(SolutionDir)thirdparty\openvr\lib\win32;$(LibraryPath)</LibraryPath>
</PropertyGroup>
Expand Down Expand Up @@ -111,8 +111,8 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
<AdditionalLibraryDirectories>%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>openvr_api.lib;libMinHook.x86.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ModuleDefinitionFile>$(ProjectDir)d3d9.def</ModuleDefinitionFile>
<AdditionalDependencies>openvr_api.lib;libMinHook.x86.lib;$(SolutionDir)dxvk\lib32\vulkan-1.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ModuleDefinitionFile>$(SolutionDir)dxvk\src\d3d9\d3d9.def</ModuleDefinitionFile>
</Link>
<PostBuildEvent>
<Command>copy /Y "$(OutDir)$(TargetName)$(TargetExt)" "C:\Program Files (x86)\Steam\steamapps\common\Left 4 Dead 2\$(TargetName)$(TargetExt)"</Command>
Expand All @@ -137,9 +137,9 @@
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
<ModuleDefinitionFile>$(ProjectDir)d3d9.def</ModuleDefinitionFile>
<ModuleDefinitionFile>$(SolutionDir)dxvk\src\d3d9\d3d9.def</ModuleDefinitionFile>
<AdditionalLibraryDirectories>%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>openvr_api.lib;libMinHook.x86.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>openvr_api.lib;libMinHook.x86.lib;$(SolutionDir)dxvk\lib32\vulkan-1.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<PostBuildEvent>
<Command>copy /Y "$(OutDir)$(TargetName)$(TargetExt)" "C:\Program Files (x86)\Steam\steamapps\common\Left 4 Dead 2\$(TargetName)$(TargetExt)"</Command>
Expand Down
70 changes: 60 additions & 10 deletions L4D2VR/vr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ VR::VR(Game *game) {

float tanHalfFov[2];

tanHalfFov[0] = max(-l_left, l_right, -r_left, r_right);
tanHalfFov[1] = max(-l_top, l_bottom, -r_top, r_bottom);
tanHalfFov[0] = std::max({ -l_left, l_right, -r_left, r_right });
tanHalfFov[1] = std::max({ -l_top, l_bottom, -r_top, r_bottom });

m_TextureBounds[0].uMin = 0.5f + 0.5f * l_left / tanHalfFov[0];
m_TextureBounds[0].uMax = 0.5f + 0.5f * l_right / tanHalfFov[0];
Expand All @@ -59,9 +59,6 @@ VR::VR(Game *game) {
m_TextureBounds[1].vMin = 0.5f - 0.5f * r_bottom / tanHalfFov[1];
m_TextureBounds[1].vMax = 0.5f - 0.5f * r_top / tanHalfFov[1];

m_RenderWidth = m_RenderWidth / max(m_TextureBounds[0].uMax - m_TextureBounds[0].uMin, m_TextureBounds[1].uMax - m_TextureBounds[1].uMin);
m_RenderHeight = m_RenderHeight / max(m_TextureBounds[0].vMax - m_TextureBounds[0].vMin, m_TextureBounds[1].vMax - m_TextureBounds[1].vMin);

m_Aspect = tanHalfFov[0] / tanHalfFov[1];
m_Fov = 2.0f * atan(tanHalfFov[0]) * 360 / (3.14159265358979323846 * 2);

Expand Down Expand Up @@ -121,6 +118,63 @@ int VR::SetActionManifest(const char *fileName) {
return 0;
}

void VR::Update()
{
if (!m_IsInitialized || !m_Game->m_Initialized)
return;

if (m_IsVREnabled)
{
if (!m_CreatedVRTextures)
{
CreateVRTextures();
m_Game->m_Hooks->hkRenderView.enableHook();
}

// Prevents crashing at menu
if (!m_Game->m_EngineClient->IsInGame())
{
IMatRenderContext *rndrContext = m_Game->m_MaterialSystem->GetRenderContext();
rndrContext->SetRenderTarget(NULL);
}
SubmitVRTextures();
}

UpdatePosesAndActions();
UpdateTracking(m_SetupOrigin);
}

void VR::CreateVRTextures()
{
m_Game->m_MaterialSystem->isGameRunning = false;
m_Game->m_MaterialSystem->BeginRenderTargetAllocation();
m_Game->m_MaterialSystem->isGameRunning = true;

m_CreatingTextureID = 0;
m_LeftEyeTexture = m_Game->m_MaterialSystem->CreateNamedRenderTargetTextureEx("leftEye0", m_RenderWidth, m_RenderHeight, RT_SIZE_NO_CHANGE, m_Game->m_MaterialSystem->GetBackBufferFormat(), MATERIAL_RT_DEPTH_SHARED, TEXTUREFLAGS_NOMIP);
m_CreatingTextureID = 1;
m_RightEyeTexture = m_Game->m_MaterialSystem->CreateNamedRenderTargetTextureEx("rightEye0", m_RenderWidth, m_RenderHeight, RT_SIZE_NO_CHANGE, m_Game->m_MaterialSystem->GetBackBufferFormat(), MATERIAL_RT_DEPTH_SHARED, TEXTUREFLAGS_NOMIP);
m_CreatingTextureID = -1;

m_Game->m_MaterialSystem->isGameRunning = false;
//MaterialSystem->EndRenderTargetAllocation(); // Freezes game and leaks memory
m_Game->m_MaterialSystem->isGameRunning = true;

m_CreatedVRTextures = true;
}

void VR::SubmitVRTextures()
{
if (!m_RenderedNewFrame)
return;

vr::EVRCompositorError leftEyeError = vr::VRCompositor()->Submit(vr::Eye_Left, &m_VKLeftEye.m_VRTexture, &(m_TextureBounds)[0], vr::Submit_Default);

vr::EVRCompositorError rightEyeError = vr::VRCompositor()->Submit(vr::Eye_Right, &m_VKRightEye.m_VRTexture, &(m_TextureBounds)[1], vr::Submit_Default);

m_RenderedNewFrame = false;
}

void VR::GetPoseData(vr::TrackedDevicePose_t &poseRaw, TrackedDevicePoseData &poseOut)
{
if (poseRaw.bPoseIsValid) {
Expand Down Expand Up @@ -210,11 +264,7 @@ void VR::ProcessInput()

if (GetAsyncKeyState(VK_F6) || PressedDigitalAction(m_ActionActivateVR))
{
if (!m_IsVREnabled)
{
m_Game->m_Hooks->hkRenderView.enableHook();
m_IsVREnabled = true;
}
m_IsVREnabled = true;
}

if (!m_IsVREnabled)
Expand Down
19 changes: 19 additions & 0 deletions L4D2VR/vr.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

class Game;
class IDirect3DTexture9;
class IDirect3DSurface9;
class ITexture;


Expand All @@ -18,6 +19,11 @@ struct TrackedDevicePoseData {
QAngle TrackedDeviceAngVel;
};

struct SharedTextureHolder {
vr::VRVulkanTextureData_t m_VulkanData;
vr::Texture_t m_VRTexture;
};


class VR
{
Expand Down Expand Up @@ -84,8 +90,17 @@ class VR
IDirect3DTexture9 *m_D9LeftEyeTexture;
IDirect3DTexture9 *m_D9RightEyeTexture;

IDirect3DSurface9 *m_D9LeftEyeSurface;
IDirect3DSurface9 *m_D9RightEyeSurface;

SharedTextureHolder m_VKLeftEye;
SharedTextureHolder m_VKRightEye;

bool m_IsVREnabled = false;
bool m_IsInitialized = false;
bool m_RenderedNewFrame = false;
bool m_CreatedVRTextures = false;
int m_CreatingTextureID = -1;

bool m_PressedLeftStick = false;
bool m_ChangedItem = false;
Expand Down Expand Up @@ -123,6 +138,7 @@ class VR
float m_RotationOffset = 0;
std::chrono::steady_clock::time_point m_PrevFrameTime;


float m_TurnSpeed = 0.3;
bool m_SnapTurning = false;
float m_SnapTurnAngle = 45.0;
Expand All @@ -132,6 +148,9 @@ class VR
VR() {};
VR(Game *game);
int SetActionManifest(const char *fileName);
void Update();
void CreateVRTextures();
void SubmitVRTextures();
void GetPoses();
void UpdatePosesAndActions();
void GetViewParameters();
Expand Down
2 changes: 1 addition & 1 deletion dxvk

0 comments on commit b785862

Please sign in to comment.