Skip to content

Commit 7ebf418

Browse files
committed
nvngx_dlss: update preset select hook for 3.6/3.7
TODO: check 3.5.x versions
1 parent 7b58b52 commit 7ebf418

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

src/module_hooks/nvngx_dlss.cpp

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ void DlssPresetOverrideFunc_Version3_1_1(SafetyHookContext& ctx)
119119
SafetyHookMid CreateDlssInstance_PresetSelection_Hook;
120120
uint8_t CreateDlssInstance_PresetSelection_Register = 0x83; // register used to access DLSS struct in this func, unfortunately changes between dev/release
121121
uint32_t CreateDlssInstance_PresetSelection_OrigInsnOffset = 0x1F8;
122-
122+
int CreateDlssInstance_PresetSelection_Offset = 3;
123123
void CreateDlssInstance_PresetSelection(SafetyHookContext& ctx)
124124
{
125125
uint8_t* dlssStruct = 0;
@@ -144,9 +144,6 @@ void CreateDlssInstance_PresetSelection(SafetyHookContext& ctx)
144144
if (!dlssStruct)
145145
return;
146146

147-
// Code that the hook overwrote (TODO: does safetyhook midhook even require this?)
148-
*(uint32_t*)(dlssStruct + CreateDlssInstance_PresetSelection_OrigInsnOffset) = 3;
149-
150147
if (!settings.overrideQualityLevels)
151148
return;
152149

@@ -155,22 +152,27 @@ void CreateDlssInstance_PresetSelection(SafetyHookContext& ctx)
155152
int displayWidth = *(int*)(dlssStruct + offsets.DisplayResolution);
156153
int displayHeight = *(int*)(dlssStruct + offsets.DisplayResolution + 4);
157154

155+
spdlog::debug("CreateDlssInstance_PresetSelection: DLSS res {}x{}, display {}x{}", dlssWidth, dlssHeight, displayWidth, displayHeight);
156+
158157
std::optional<unsigned int> presetValue;
159158
presetValue.reset();
160159

161160
unsigned int presetDLAA = 0;
161+
if (settings.qualities.contains(NVSDK_NGX_PerfQuality_Value_DLAA))
162+
presetDLAA = settings.qualities[NVSDK_NGX_PerfQuality_Value_DLAA].preset;
163+
162164
for (const auto& [level, quality] : settings.qualities)
163165
{
164-
if (level == NVSDK_NGX_PerfQuality_Value_DLAA)
165-
presetDLAA = quality.preset; // note down DLAA value for later since we're iterating here anyway...
166-
167166
// Check that both height & width are within 1 pixel of each other either way
168167
bool widthIsClose = abs(quality.currentResolution.first - dlssWidth) <= 1;
169168
bool heightIsClose = abs(quality.currentResolution.second - dlssHeight) <= 1;
170169
if (widthIsClose && heightIsClose)
171170
{
172171
if (quality.preset != NVSDK_NGX_DLSS_Hint_Render_Preset_Default)
172+
{
173+
spdlog::debug("CreateDlssInstance_PresetSelection: using preset {} for quality {} with resolution {}x{}", char('A' + quality.preset - 1), quality.name, quality.currentResolution.first, quality.currentResolution.second);
173174
presetValue = quality.preset;
175+
}
174176

175177
break;
176178
}
@@ -184,10 +186,13 @@ void CreateDlssInstance_PresetSelection(SafetyHookContext& ctx)
184186
bool widthIsClose = abs(displayWidth - dlssWidth) <= 1;
185187
bool heightIsClose = abs(displayHeight - dlssHeight) <= 1;
186188
if (widthIsClose && heightIsClose)
189+
{
190+
spdlog::debug("CreateDlssInstance_PresetSelection: using preset {} for DLAA with resolution {}x{}", char('A' + presetDLAA - 1), displayWidth, displayHeight);
187191
presetValue = presetDLAA;
192+
}
188193
}
189194

190-
// If no value override set, return now so DLSS will use whatever it was going to originally
195+
// If still no value override set, return now so DLSS will use whatever it was going to originally
191196
if (!presetValue.has_value())
192197
return;
193198
}
@@ -196,8 +201,13 @@ void CreateDlssInstance_PresetSelection(SafetyHookContext& ctx)
196201
ctx.rdx = *presetValue;
197202
else if (CreateDlssInstance_PresetSelection_Register == 0x86) // dev DLL, preset stored in r15
198203
ctx.r15 = *presetValue;
199-
else if (CreateDlssInstance_PresetSelection_Register == 0x87) // 3.1.30, preset stored in rdx
200-
ctx.rdx = *presetValue;
204+
else if (CreateDlssInstance_PresetSelection_Register == 0x87)
205+
{
206+
if (CreateDlssInstance_PresetSelection_Offset == 0x11)
207+
ctx.rcx = *presetValue; // 3.6.0 / 3.7.0, preset stored in rcx
208+
else
209+
ctx.rdx = *presetValue; // 3.1.30, preset stored in rdx
210+
}
201211
}
202212

203213
SafetyHookMid dlssIndicatorHudHook{};
@@ -269,16 +279,23 @@ bool hook(HMODULE ngx_module)
269279
// Hook to override the preset DLSS picks based on ratio, so we can check against users customized ratios/resolutions instead
270280
bool presetSelectPatternSuccess = false;
271281
auto presetSelectPattern = hook::pattern(ngx_module, "8B ? ? C7 ? ? ? 00 00 03 00 00 00");
282+
CreateDlssInstance_PresetSelection_Offset = 3;
283+
if (!presetSelectPattern.size())
284+
{
285+
// 3.6.0 / 3.7.0
286+
presetSelectPattern = hook::pattern(ngx_module, "8B ? ? F6 47 ? 80");
287+
CreateDlssInstance_PresetSelection_Offset = 0x11;
288+
}
272289
if (presetSelectPattern.size())
273290
{
274-
uint8_t* match = presetSelectPattern.count(1).get_first<uint8_t>(3);
291+
uint8_t* match = presetSelectPattern.count(1).get_first<uint8_t>(CreateDlssInstance_PresetSelection_Offset);
275292
CreateDlssInstance_PresetSelection_Register = match[1];
276293
CreateDlssInstance_PresetSelection_OrigInsnOffset = *(uint32_t*)&match[2];
277294

278295
// TODO: better way of handling CreateDlssInstance_PresetSelection_Register
279296

280-
if (CreateDlssInstance_PresetSelection_Register != 0x83 &&
281-
CreateDlssInstance_PresetSelection_Register != 0x86 &&
297+
if (CreateDlssInstance_PresetSelection_Register != 0x83 &&
298+
CreateDlssInstance_PresetSelection_Register != 0x86 &&
282299
CreateDlssInstance_PresetSelection_Register != 0x87)
283300
{
284301
spdlog::error("nvngx_dlss: DLSS preset selection hook failed (unknown register 0x{:X})", CreateDlssInstance_PresetSelection_Register);
@@ -298,7 +315,7 @@ bool hook(HMODULE ngx_module)
298315

299316
if (!presetSelectPatternSuccess)
300317
{
301-
spdlog::warn("nvngx_dlss: failed to hook DLSS3.1.11+ preset selection code, presets may act strange when used with customized DLSSQualityLevels - recommend using DLSS 3.1.11 / 3.1.30!");
318+
spdlog::warn("nvngx_dlss: failed to locate DLSS3.1.11+ preset selection code, customized [DLSSPresets] may act strange if used with customized [DLSSQualityLevels] - recommend using DLSS 3.7!");
302319
}
303320

304321
// OverrideDlssHud hooks

src/resource.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#define TWEAKS_VER_MAJOR 0
1717
#define TWEAKS_VER_MINOR 200
1818
#define TWEAKS_VER_BUILD 9
19-
#define TWEAKS_VER_REVISION 0
19+
#define TWEAKS_VER_REVISION 1
2020

2121
#define STR(value) #value
2222
#define STRINGIZE(value) STR(value)

0 commit comments

Comments
 (0)