Skip to content

Commit

Permalink
Pass lighting info to RTX Remix
Browse files Browse the repository at this point in the history
Also disable practically all of New Vegas Reloaded
Tiny dll go brr
  • Loading branch information
BlueAmulet committed May 23, 2023
1 parent 0aea679 commit 3f439ba
Show file tree
Hide file tree
Showing 16 changed files with 187 additions and 22 deletions.
2 changes: 1 addition & 1 deletion NewVegasReloaded/Exports.def
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
LIBRARY "NewVegasReloaded"
LIBRARY "NewVegasRTXLight"
EXPORTS
NVSEPlugin_Query
NVSEPlugin_Load
49 changes: 45 additions & 4 deletions NewVegasReloaded/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
#include "Hooks/NewVegas/Hooks.h"
#include "Device/Hook.h"

static bool GetPrivateProfileBoolW(LPCWSTR lpAppName, LPCWSTR lpKeyName, BOOLEAN bDefault, LPCWSTR lpFileName) {
wchar_t boolStr[6];
DWORD strlen = GetPrivateProfileStringW(lpAppName, lpKeyName, bDefault ? L"True" : L"False", boolStr, _countof(boolStr), lpFileName);
return (CompareStringW(LOCALE_INVARIANT, NORM_IGNORECASE, boolStr, strlen, L"True", 4) == CSTR_EQUAL);
}

extern "C" {

bool NVSEPlugin_Query(const PluginInterface* Interface, PluginInfo* Info) {

Info->InfoVersion = PluginInfo::kInfoVersion;
Info->Name = "NewVegasReloaded";
Info->Name = "NewVegasRTXLight";
Info->Version = 4;
return true;

Expand All @@ -26,13 +32,48 @@ extern "C" {
AttachDeviceHooks();
#endif

Logger::Initialize("NewVegasReloaded.log");
CommandManager::Initialize(Interface);
Logger::Initialize("NewVegasRTXLight.log");
//CommandManager::Initialize(Interface);

if (!Interface->IsEditor) {
PluginVersion::CreateVersionString();
SettingManager::Initialize();
TheSettingManager->LoadSettings();
//TheSettingManager->LoadSettings();

HMODULE hDllHandle;
GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCWSTR)&NVSEPlugin_Load, &hDllHandle);
wchar_t iniPath[MAX_PATH];
GetModuleFileNameW(hDllHandle, iniPath, _countof(iniPath));
wchar_t* slash = wcsrchr(iniPath, L'\\');
if (slash) {
slash[0] = 0;
if (SUCCEEDED(StringCchCatW(iniPath, _countof(iniPath), L"\\NewVegasRTXLight.ini"))) {
Logger::Log("Reading settings from %ls", iniPath);
SettingManager::LightRangeMode = GetPrivateProfileIntW(L"NewVegasRTXLight", L"LightRangeMode", SettingManager::LightRangeMode, iniPath);
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);

// Write settings back
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);
} else {
Logger::Log("Failed to calculate ini path, path is too long");
}
} else {
Logger::Log("Failed to calculate ini path, odd path?");
}

Logger::Log("Light mode: %u", SettingManager::LightRangeMode);
Logger::Log("Disable culling: %s", SettingManager::DisableCulling ? "true" : "false");
Logger::Log("Inaccurate sun: %s", SettingManager::SunLight ? "true" : "false");

AttachHooks();
}
return true;
Expand Down
11 changes: 5 additions & 6 deletions NewVegasReloaded/NewVegasReloaded.rc
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,12 @@ BEGIN
BEGIN
BLOCK "040904b0"
BEGIN
VALUE "CompanyName", "Alessio Tamburini"
VALUE "FileDescription", "New Vegas Reloaded"
VALUE "FileDescription", "New Vegas RTX Light"
VALUE "FileVersion", "4.0.0.0"
VALUE "InternalName", "NewVegasReloaded.dll"
VALUE "LegalCopyright", "Copyright (C) 2021"
VALUE "OriginalFilename", "NewVegasReloaded.dll"
VALUE "ProductName", "New Vegas Reloaded"
VALUE "InternalName", "NewVegasRTXLight.dll"
VALUE "LegalCopyright", "Copyright (C) 2023"
VALUE "OriginalFilename", "NewVegasRTXLight.dll"
VALUE "ProductName", "New Vegas RTX Light"
VALUE "ProductVersion", "4.0.0.0"
END
END
Expand Down
2 changes: 2 additions & 0 deletions NewVegasReloaded/NewVegasReloaded.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@
<IncludePath>$(IncludePath)</IncludePath>
<LibraryPath>$(LibraryPath)</LibraryPath>
<OutDir>$(SolutionDir)$(ProjectName)\$(Configuration)\</OutDir>
<TargetName>NewVegasRTXLight</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<EnableMicrosoftCodeAnalysis>false</EnableMicrosoftCodeAnalysis>
<IncludePath>$(IncludePath)</IncludePath>
<LibraryPath>$(LibraryPath)</LibraryPath>
<OutDir>$(SolutionDir)$(ProjectName)\$(Configuration)\</OutDir>
<TargetName>NewVegasRTXLight</TargetName>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
Expand Down
4 changes: 2 additions & 2 deletions TESReloaded/Core/Hooks/GameCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ NiDX9Renderer* __fastcall InitializeRendererHook(NiDX9Renderer* This, UInt32 edx

TheRenderManager = (RenderManager*)(*InitializeRenderer)(This);
TheRenderManager->Initialize();
InitializeManagers();
TheShaderManager->CreateEffects();
//InitializeManagers();
//TheShaderManager->CreateEffects();
return TheRenderManager;

}
Expand Down
16 changes: 16 additions & 0 deletions TESReloaded/Core/Hooks/NewVegas/Hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ void AttachHooks() {

DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
/*
DetourAttach(&(PVOID&)ReadSetting, &ReadSettingHook);
DetourAttach(&(PVOID&)WriteSetting, &WriteSettingHook);
DetourAttach(&(PVOID&)LoadGame, &LoadGameHook);
DetourAttach(&(PVOID&)NewMain, &NewMainHook);
//*/
DetourAttach(&(PVOID&)InitializeRenderer, &InitializeRendererHook);
DetourAttach(&(PVOID&)NewTES, &NewTESHook);
/*
DetourAttach(&(PVOID&)NewPlayerCharacter, &NewPlayerCharacterHook);
DetourAttach(&(PVOID&)NewSceneGraph, &NewSceneGraphHook);
DetourAttach(&(PVOID&)NewMainDataHandler, &NewMainDataHandlerHook);
//*/
DetourAttach(&(PVOID&)NewMenuInterfaceManager, &NewMenuInterfaceManagerHook);
/*
DetourAttach(&(PVOID&)NewQueuedModelLoader, &NewQueuedModelLoaderHook);
DetourAttach(&(PVOID&)CreateVertexShader, &CreateVertexShaderHook);
DetourAttach(&(PVOID&)CreatePixelShader, &CreatePixelShaderHook);
Expand All @@ -36,6 +41,7 @@ void AttachHooks() {
DetourAttach(&(PVOID&)ShowDetectorWindow, &ShowDetectorWindowHook);
DetourAttach(&(PVOID&)LoadForm, &LoadFormHook);
if (SettingsMain->FlyCam.Enabled) DetourAttach(&(PVOID&)UpdateFlyCam, &UpdateFlyCamHook);
//*/
DetourTransactionCommit();


Expand All @@ -50,26 +56,31 @@ void AttachHooks() {
SafeWrite8(0x008751C0, 0); // Stops to clear the depth buffer when rendering the 1st person node
SafeWrite16(0x0086A170, 0x9090); // Avoids to pause the game when ALT-TAB

/*
SafeWriteJump(Jumpers::DetectorWindow::CreateTreeViewHook, (UInt32)DetectorWindowCreateTreeViewHook);
SafeWriteJump(Jumpers::DetectorWindow::DumpAttributesHook, (UInt32)DetectorWindowDumpAttributesHook);
SafeWriteJump(Jumpers::DetectorWindow::ConsoleCommandHook, (UInt32)DetectorWindowConsoleCommandHook);
SafeWriteCall(Jumpers::DetectorWindow::SetNodeName, (UInt32)DetectorWindowSetNodeName);
SafeWriteJump(Jumpers::RenderInterface::Hook, (UInt32)RenderInterfaceHook);
SafeWriteJump(Jumpers::SetRegionEditorName::Hook, (UInt32)SetRegionEditorNameHook);
SafeWriteJump(Jumpers::SetWeatherEditorName::Hook, (UInt32)SetWeatherEditorNameHook);
//*/
SafeWriteJump(Jumpers::Shadows::RenderShadowMapHook, (UInt32)RenderShadowMapHook);
// SafeWriteJump(Jumpers::Shadows::RenderShadowMap1Hook, (UInt32)RenderShadowMap1Hook);
/*
SafeWriteJump(Jumpers::Shadows::AddCastShadowFlagHook, (UInt32)AddCastShadowFlagHook);
SafeWriteJump(Jumpers::Shadows::LeavesNodeNameHook, (UInt32)LeavesNodeNameHook);
SafeWriteCall(Jumpers::MainMenuMusic::Fix1, (UInt32)MainMenuMusicFix);
SafeWriteCall(Jumpers::MainMenuMusic::Fix2, (UInt32)MainMenuMusicFix);
//*/

SafeWriteJump(0x004E4C3B, 0x004E4C42); // Fixes reflections when cell water height is not like worldspace water height
SafeWriteJump(0x004E4DA4, 0x004E4DAC); // Fixes reflections on the distant water
SafeWriteCall(0x00875B86, 0x00710AB0); // Sets the world fov at the end of 1st person rendering
SafeWriteCall(0x00875B9D, 0x00710AB0); // Sets the world fov at the end of 1st person rendering
SafeWriteJump(0x00C03F49, 0x00C03F5A); // Fixes wrong rendering for image space effects

/*
SafeWriteCall(0x9BB158, (UInt32)MuzzleLightCullingFix);
if (SettingsMain->Main.ForceReflections) {
Expand All @@ -91,5 +102,10 @@ void AttachHooks() {
SafeWriteJump(Jumpers::FlyCam::UpdateRightFlyCamHook, (UInt32)UpdateRightFlyCamHook);
SafeWriteJump(Jumpers::FlyCam::UpdateLeftFlyCamHook, (UInt32)UpdateLeftFlyCamHook);
}
//*/

if (SettingManager::DisableCulling)
{
SafeWriteCall(0xC51887, (UInt32)BSCullingProcessCreateHook);
}
}
9 changes: 8 additions & 1 deletion TESReloaded/Core/Hooks/NewVegas/Render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,11 @@ void __fastcall MuzzleLightCullingFix(MuzzleFlash* This) {
}
}
ThisCall(0x9BB8A0, This);
}
}

BSCullingProcess* __fastcall BSCullingProcessCreateHook(BSCullingProcess* apThis, void*, void* apVisibleSet) {

ThisCall(0x4A0EB0, apThis, apVisibleSet);
apThis->kCullMode = 1; // ALLPASS
return apThis;
}
1 change: 1 addition & 0 deletions TESReloaded/Core/Hooks/NewVegas/Render.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ void DetectorWindowConsoleCommandHook();
void DetectorWindowDumpAttributesHook();

void __fastcall MuzzleLightCullingFix(MuzzleFlash* This);
BSCullingProcess* __fastcall BSCullingProcessCreateHook(BSCullingProcess* apThis, void*, void* apVisibleSet);

float MultiBoundWaterHeightFix();
void DetectorWindowDumpAttributes(HWND TreeView, UInt32 Msg, WPARAM wParam, LPTVINSERTSTRUCTA lParam);
Expand Down
3 changes: 1 addition & 2 deletions TESReloaded/Core/Hooks/NewVegas/Shadows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ __declspec(naked) void RenderShadowMapHook() {

__asm {
pushad
mov ecx, TheShadowManager
call ShadowManager::RenderShadowMaps
call ShadowManager::FixedFunctionLighting
popad
jmp Jumpers::Shadows::RenderShadowMapReturn
}
Expand Down
8 changes: 4 additions & 4 deletions TESReloaded/Core/RenderManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,16 @@ void RenderManager::Initialize() {
RESZ = D3D->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, (D3DFORMAT)MAKEFOURCC('R','E','S','Z')) == D3D_OK;
if (RESZ)
Logger::Log("AMD/Intel detected: RESZ supported.");
else if (NvAPI_Initialize() == NVAPI_OK)
Logger::Log("NVIDIA detected: NVAPI supported.");
//else if (NvAPI_Initialize() == NVAPI_OK)
// Logger::Log("NVIDIA detected: NVAPI supported.");
else
Logger::Log("ERROR: Cannot initialize the render manager. Graphics device not supported.");
if (TheSettingManager->SettingsMain.Main.AnisotropicFilter >= 2) device->SetSamplerState(0, D3DSAMP_MAXANISOTROPY, TheSettingManager->SettingsMain.Main.AnisotropicFilter);
BackBuffer = CreateHDRRenderTarget();
}

void RenderManager::ResolveDepthBuffer() {

/*
if (RESZ) {
IDirect3DBaseTexture9 *pCurrTX = NULL;
IDirect3DVertexShader9 *pCurrVS = NULL;
Expand Down Expand Up @@ -304,7 +304,7 @@ void RenderManager::ResolveDepthBuffer() {
else
NvAPI_D3D9_StretchRectEx(device, TheTextureManager->DepthSurface, NULL, TheTextureManager->DepthTexture, NULL, D3DTEXF_NONE);
}

//*/
}

void RenderManager::CheckAndTakeScreenShot(IDirect3DSurface9* RenderTarget){
Expand Down
4 changes: 4 additions & 0 deletions TESReloaded/Core/SettingManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1199,3 +1199,7 @@ void SettingManager::FillFromString(const char* String, const char* Delimiter, T
}

}

int SettingManager::LightRangeMode = 0;
bool SettingManager::DisableCulling = true;
bool SettingManager::SunLight = true;
5 changes: 5 additions & 0 deletions TESReloaded/Core/SettingManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,9 @@ class SettingManager : public SettingManagerBase {
SettingsColoringMap SettingsColoring;
SettingsWeatherMap SettingsWeather;
SettingsSpecularStruct SettingsSpecular;

// Shit settings
static int LightRangeMode;
static bool DisableCulling;
static bool SunLight;
};
69 changes: 68 additions & 1 deletion TESReloaded/Core/ShadowManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -664,11 +664,78 @@ void ShadowManager::RenderShadowCubeMap(NiPointLight** Lights, int LightIndex, S

//static NiDX9RenderState::NiRenderStateSetting* RenderStateSettings = nullptr;

/*
* Passes light information to RTX Remix
*/
void ShadowManager::FixedFunctionLighting() {
static int lastEnabled = 0;

IDirect3DDevice9* Device = TheRenderManager->device;
NiTList<ShadowSceneLight>::Entry* Entry = SceneNode->lights.start;
NiPoint3* CameraPosition = (NiPoint3*)0x011F8E9C;
//NiPoint3* CameraPosition = (NiPoint3*)0x011FA2A0;

// Fill in sun light
if (SettingManager::SunLight)
{
NiDirectionalLight* sunLight = Tes->directionalLight;
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);
Device->SetLight(0, &DirLight);
}
Device->LightEnable(0, SettingManager::SunLight);

// Fill in various point lights
int i = 1; // Sun is at 0
while (Entry) {
NiPointLight* Light = Entry->data->sourceLight;

D3DLIGHT9 DX9Light;
ZeroMemory(&DX9Light, sizeof(DX9Light));
DX9Light.Type = D3DLIGHT_POINT;
// TODO: Diffuse seems to be 0 to 1, Specular is not 0-1, not 0-255, maybe a radius?, Ambient is usually black
DX9Light.Diffuse = D3DXCOLOR(Light->Diff.r * Light->Dimmer, Light->Diff.g * Light->Dimmer, Light->Diff.b * Light->Dimmer, 1.0f);
//DX9Light.Specular = D3DXCOLOR(Light->Spec.r, Light->Spec.g, Light->Spec.b, 1.0f);
DX9Light.Ambient = D3DXCOLOR(Light->Amb.r, Light->Amb.g, Light->Amb.b, 1.0f);
DX9Light.Position = D3DXVECTOR3(Light->m_worldTransform.pos.x - CameraPosition->x, Light->m_worldTransform.pos.y - CameraPosition->y, Light->m_worldTransform.pos.z - CameraPosition->z);
if (SettingManager::LightRangeMode == 0) {
// Actual range value according to Wall_SoGB
DX9Light.Range = Light->Spec.r;
} else if (SettingManager::LightRangeMode == 1) {
// Calculate range such that attenuation = 1/255
if (Light->Atten2 > 0) {
DX9Light.Range = (-Light->Atten1 + std::sqrt(Light->Atten1 * Light->Atten1 + 1020.0f * Light->Atten2)) / (2.0f * Light->Atten2);
} else if (Light->Atten1 > 0) {
DX9Light.Range = 255.0f / Light->Atten1;
}
} else {
// Let Remix figure it out
DX9Light.Range = INFINITY;
}

DX9Light.Attenuation0 = Light->Atten0;
DX9Light.Attenuation1 = Light->Atten1;
DX9Light.Attenuation2 = Light->Atten2;
Device->SetLight(i, &DX9Light);
Device->LightEnable(i, TRUE);
i++;

Entry = Entry->next;
}
for (int j = i; j < lastEnabled; j++) {
Device->LightEnable(j, FALSE);
}
lastEnabled = i;
}

/*
* Renders the different shadow maps: Near, Far, Ortho.
*/
void ShadowManager::RenderShadowMaps() {

if (!TheSettingManager->GetSettingI("Main.Main.Misc", "RenderEffects")) return; // cancel out if rendering effects is disabled

SettingsMainStruct::EquipmentModeStruct* EquipmentModeSettings = &TheSettingManager->SettingsMain.EquipmentMode;
Expand Down
1 change: 1 addition & 0 deletions TESReloaded/Core/ShadowManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class ShadowManager { // Never disposed
D3DXMATRIX GetCascadeViewProj(ShadowMapTypeEnum ShadowMapType, SettingsShadowStruct::ExteriorsStruct* ShadowsExteriors, D3DXMATRIX View);
static void GetCascadeDepths();
static float lerp(float a, float b, float t);
static void FixedFunctionLighting();

ShaderRecordVertex* ShadowMapVertex;
ShaderRecordPixel* ShadowMapPixel;
Expand Down
2 changes: 1 addition & 1 deletion TESReloaded/Framework/NewVegas/Framework.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <dinput.h>
#include <dsound.h>
#include "../Common/Lib/Detours/detours.h"
#include "../Common/Lib/Nvidia/nvapi.h"
//#include "../Common/Lib/Nvidia/nvapi.h"
#include "../Common/Lib/Bink/bink.h"
#include "../Common/Base/Logger.h"
#include "../Common/Base/Types.h"
Expand Down
Loading

0 comments on commit 3f439ba

Please sign in to comment.