Skip to content

Commit

Permalink
Improve patches
Browse files Browse the repository at this point in the history
Culling patch actually works now
Use sun's local position for daytime lighting
It's still kind of jank
  • Loading branch information
BlueAmulet committed May 24, 2023
1 parent 7346156 commit ae2b1b9
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 12 deletions.
81 changes: 76 additions & 5 deletions TESReloaded/Core/Hooks/NewVegas/Culling.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,83 @@
// Anti culling patch, thanks Wall_SoGB

BSCullingProcess* __fastcall BSCullingProcessCreateHook(BSCullingProcess* apThis, void*, void* apVisibleSet) {
class BSOcclusionPlane;

ThisCall(0x4A0EB0, apThis, apVisibleSet);
apThis->kCullMode = 1; // ALLPASS
return apThis;
void __fastcall BSCullingProcess_SetCullModeEx(BSCullingProcess* apThis, void*, UInt32 eType) {
apThis->kCullMode = 1; // CULL_ALLPASS
}

void __fastcall BSCullingProcess_PushCullModeEx(BSCullingProcess* apThis, void*, UInt32 eType) {
apThis->eTypeStack[apThis->uiStackIndex++] = 1;
apThis->kCullMode = 1;
}

typedef void* (__fastcall* MTRenderManager__AddTask)(void*, void*, NiCamera*, BSCompoundFrustum*, NiNode*, void*, void*, BSShaderAccumulator*, UInt32, UInt32, UInt32);
static MTRenderManager__AddTask MTRenderManager__AddTaskFnk = (MTRenderManager__AddTask)0xBA3390;

void __fastcall MTRenderManager__AddTaskEx(void* ecx, void* edx, NiCamera* apCamera, BSCompoundFrustum* apFrustum, NiNode* apNode, void* apGeometryList1, void* apGeometryList2, BSShaderAccumulator* apShaderAccum, UInt32 aeCullMode, UInt32, UInt32) {
MTRenderManager__AddTaskFnk(ecx, edx, apCamera, apFrustum, apNode, apGeometryList1, apGeometryList2, apShaderAccum, 1, 0, 0);
}

bool __fastcall BSOcclusionPlane__WithinFrustumEx(
BSOcclusionPlane* apThis, void*,
NiFrustumPlanes* aPlanes,
bool abViewHitPlane,
NiPoint3 akViewIntersection) {
return true;
}

bool __fastcall WithinFrustumDistFirst(void*, void*, void*, void*) {
return true;
}

bool __fastcall CompletelyWithinFrustum(void*, void*, void*) {
return true;
}

// TODO: The vtable for NiAVObject seems off, OnVisible at 0xBC instead of 0xD4
/*
void __fastcall BSCullingProcess_ProcessEx(BSCullingProcess* apThis, void*, NiAVObject* apObject) {
apThis->pCompoundFrustum = 0;
apObject->OnVisible(apThis);
}
//*/
__declspec(naked) void BSCullingProcess_ProcessEx() {
__asm {
push ebp
mov ebp, esp
mov edx, ecx
mov ecx, dword ptr ss:[ebp + 8]
mov dword ptr ds:[edx + 0xC0], 0
mov eax, dword ptr ds:[ecx]
mov dword ptr ss:[ebp + 8], edx
pop ebp
jmp dword ptr ds:[eax + 0xD4]
}
}

void AttachCullingHooks() {
SafeWriteCall(0xC51887, (UInt32)BSCullingProcessCreateHook);
for (int callAddr : {0x8014F7, 0x80182E, 0x874260, 0x8742F0, 0x874316, 0x8743A6, 0x876233, 0x8767FD, }) {
SafeWriteCall(callAddr, (UInt32)BSCullingProcess_SetCullModeEx);
}

for (int callAddr : {0x54EB4E, 0x54EB91, 0x7134DF, 0x73354D, 0x740F7D, 0x77F188, 0x78E820, 0x794939, 0x7BBD3D, 0x7C1A0D, 0x7C9D09, 0x7E480E, 0x7FBD02, 0x7FE3D1, 0x8753CA, 0x875C45, }) {
SafeWriteCall(callAddr, (UInt32)BSCullingProcess_PushCullModeEx);
}

for (int callAddr : {0x4EA815, 0x4EAF18, 0x4EB1D7, 0x4EC769, 0x713F97, 0x874037, 0x87509C, 0xB9E6CB, }) {
SafeWriteCall(callAddr, (UInt32)MTRenderManager__AddTaskEx);
}

for (int callAddr : {0x549CCF, 0xB5AD64, 0xC347B7, 0xC4965C, }) {
SafeWriteCall(callAddr, (UInt32)BSOcclusionPlane__WithinFrustumEx);
}

for (int callAddr : {0xB5F25E, 0xB5F33A, 0xC49596, }) {
SafeWriteCall(callAddr, (UInt32)WithinFrustumDistFirst);
}

SafeWriteCall(0xC49690, (UInt32)CompletelyWithinFrustum);
SafeWrite32(0x101EA44, (UInt32)CompletelyWithinFrustum);
SafeWrite32(0x101E984, (UInt32)CompletelyWithinFrustum);
SafeWrite32(0x101E330, (UInt32)BSCullingProcess_ProcessEx);
}
20 changes: 17 additions & 3 deletions TESReloaded/Core/Hooks/NewVegas/Lights.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,28 @@ void FixedFunctionLighting() {
//NiPoint3* CameraPosition = (NiPoint3*)0x011FA2A0;

// Fill in sun light
if (SettingManager::SunLight)
{
if (SettingManager::SunLight) {
TimeGlobals* GameTimeGlobals = TimeGlobals::Get();
float GameHour = GameTimeGlobals->GameHour->data;
NiDirectionalLight* sunLight = Tes->directionalLight;
Sky* WorldSky = Tes->sky;
NiNode* SunRoot = WorldSky->sun->RootNode;
float SunriseStart = WorldSky->GetSunriseBegin();
float SunsetEnd = WorldSky->GetSunsetEnd();

D3DLIGHT9 DirLight;
ZeroMemory(&DirLight, sizeof(DirLight));
DirLight.Type = D3DLIGHT_DIRECTIONAL;
DirLight.Diffuse = D3DXCOLOR(sunLight->Diff.r, sunLight->Diff.g, sunLight->Diff.b, 1);
DirLight.Direction = D3DXVECTOR3(sunLight->direction.x, sunLight->direction.y, sunLight->direction.z);
bool night = GameHour > SunsetEnd || GameHour < SunriseStart;
if (!SettingManager::VisualSun || GameHour > SunsetEnd || GameHour < SunriseStart) {
// Night time, use lighting direction
DirLight.Direction = D3DXVECTOR3(Tes->directionalLight->direction.x, Tes->directionalLight->direction.y, Tes->directionalLight->direction.z);
}
else {
// Day time, flip sun position to direction
DirLight.Direction = D3DXVECTOR3(-SunRoot->m_localTransform.pos.x, -SunRoot->m_localTransform.pos.y, -SunRoot->m_localTransform.pos.z);
}
Device->SetLight(0, &DirLight);
}
Device->LightEnable(0, SettingManager::SunLight);
Expand Down
15 changes: 11 additions & 4 deletions TESReloaded/Core/SettingManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ static bool GetPrivateProfileBoolW(LPCWSTR lpAppName, LPCWSTR lpKeyName, BOOLEAN
return (CompareStringW(LOCALE_INVARIANT, NORM_IGNORECASE, boolStr, strlen, L"True", 4) == CSTR_EQUAL);
}

#define WritePrivateProfileBoolW(lpAppName, lpKeyName, bValue, lpFileName) WritePrivateProfileStringW((lpAppName), (lpKeyName), (bValue) ? L"True" : L"False", (lpFileName))

void SettingManager::Initialize() {
Logger::Log("Starting the settings manager...");
TheSettingManager = new SettingManager();
Expand Down Expand Up @@ -35,12 +37,15 @@ void SettingManager::LoadSettings() {

Logger::Log("Reading settings from %ls", iniPath);
SettingManager::LightRangeMode = GetPrivateProfileIntW(L"NewVegasRTXLight", L"LightRangeMode", SettingManager::LightRangeMode, iniPath);
SettingManager::DisableCulling = GetPrivateProfileBoolW(L"NewVegasRTXLight", L"DisableCulling", SettingManager::DisableCulling, iniPath);
SettingManager::SunLight = GetPrivateProfileBoolW(L"NewVegasRTXLight", L"SunLight", SettingManager::SunLight, iniPath);
SettingManager::VisualSun = GetPrivateProfileBoolW(L"NewVegasRTXLight", L"VisualSun", SettingManager::VisualSun, iniPath);

// Validate settings
if (SettingManager::LightRangeMode < 0 || SettingManager::LightRangeMode > 2) {
Logger::Log("Invalid light mode %u, resetting to 1", SettingManager::LightRangeMode);
SettingManager::LightRangeMode = 1;
}
SettingManager::DisableCulling = GetPrivateProfileBoolW(L"NewVegasRTXLight", L"DisableCulling", SettingManager::DisableCulling, iniPath);
SettingManager::SunLight = GetPrivateProfileBoolW(L"NewVegasRTXLight", L"SunLight", SettingManager::SunLight, iniPath);
}

/*
Expand All @@ -55,10 +60,12 @@ void SettingManager::SaveSettings() {
wchar_t buffer[32];
_snwprintf(buffer, _countof(buffer), L"%u", SettingManager::LightRangeMode);
WritePrivateProfileStringW(L"NewVegasRTXLight", L"LightRangeMode", buffer, iniPath);
WritePrivateProfileStringW(L"NewVegasRTXLight", L"DisableCulling", SettingManager::DisableCulling ? L"True" : L"False", iniPath);
WritePrivateProfileStringW(L"NewVegasRTXLight", L"SunLight", SettingManager::SunLight ? L"True" : L"False", iniPath);
WritePrivateProfileBoolW(L"NewVegasRTXLight", L"DisableCulling", SettingManager::DisableCulling, iniPath);
WritePrivateProfileBoolW(L"NewVegasRTXLight", L"SunLight", SettingManager::SunLight, iniPath);
WritePrivateProfileBoolW(L"NewVegasRTXLight", L"VisualSun", SettingManager::VisualSun, iniPath);
}

int SettingManager::LightRangeMode = 0;
bool SettingManager::DisableCulling = true;
bool SettingManager::SunLight = true;
bool SettingManager::VisualSun = true;
1 change: 1 addition & 0 deletions TESReloaded/Core/SettingManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ class SettingManager : public SettingManagerBase {
static int LightRangeMode;
static bool DisableCulling;
static bool SunLight;
static bool VisualSun;
};

0 comments on commit ae2b1b9

Please sign in to comment.