forked from codecrafting-io/AdvancedPathTracingCP2077
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
590 lines (521 loc) · 19.6 KB
/
Copy pathinit.lua
File metadata and controls
590 lines (521 loc) · 19.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
--[[
Created by codecrafting-io
AdvancedPathTracing is a small mod for properly setting some advanced path tracing settings
Credits
- [Ultra Plus Control (sammilucia)](https://www.nexusmods.com/cyberpunk2077/mods/10490)
- [Weathermancer (RMK)](https://www.nexusmods.com/cyberpunk2077/mods/9805)
- [JB - TPP MOD WIP third person (Jelle Bakker)](https://www.nexusmods.com/cyberpunk2077/mods/669)
- [betterHeadlights (keanuWheeze)](https://www.nexusmods.com/cyberpunk2077/mods/5013)
- [Lua Kit for CET (Pavel Siberx)](https://github.com/psiberx/cp2077-cet-kit)
Nvidia Docs - https://github.com/NVIDIAGameWorks/RTXDI/blob/main/doc/Integration.md
Native Settings Docs - https://github.com/justarandomguyintheinternet/CP77_nativeSettings
]]--
--do return end
settings = {}
Cron = require('Modules/Cron')
GameUI = require('Modules/GameUI')
GameSettings = require('Modules/GameSettings')
_Mod = {
name = 'Advanced Path Tracing',
settings = {},
events = {
beforeRefresh = {},
afterRefresh = {}
}
}
local settingsFilename = "settings.json"
local defaults = require('defaults')
local ptSettings = require('ptSettings')
local Debug = require('Modules/Debug')
local ModSettings = require('Modules/ModSettings')
local ModOptions = require('modOptions')
local PRESET = {
VANILLA = 1,
VERY_LOW = 2,
LOW = 3,
MEDIUM = 4,
HIGH = 5,
ULTRA = 6,
PSYCHO = 7,
CUSTOM = 8
}
local previous = {
dlssSharpness = nil,
dlssPreset = nil,
dlssBackend = nil,
hasDLSSD = nil,
isRaining = nil,
isIndoors = nil
}
local runtime = {
firstLoad = true,
reGIRApplied = false,
inGame = false,
nrdTimer = nil,
particleTimer = nil,
enableReGIR = false,
enableReSTIR = true,
refreshGame = false,
refreshTimer = nil,
hasDLSSD = false,
}
local eventHandler = {
--Register events
__index = function(self, key)
return function(callback)
if self.events[key] then
table.insert(self.events[key], callback)
end
end
end,
--Call events
__call = function(self, event, ...)
if self.events[event] then
for _, callback in ipairs(self.events[event]) do
callback(...)
end
end
end
}
setmetatable(_Mod, eventHandler)
---Checks if preset has a valid range, except custom
---@param preset integer
---@return boolean
local function isPresetInRange(preset)
return preset >= PRESET.VANILLA and preset <= PRESET.PSYCHO
end
---Checks if the provided setting index of the current ptPreset is identical to value, if not set "Custom" profile
---@param setting string
---@param value any
local function checkCustomPreset(setting, value)
if isPresetInRange(settings.ptPreset) and ptSettings.preset[settings.ptPreset][setting] ~= value then
ModOptions.setOption('ptPreset', 8)
end
end
---Confirm game changes and refresh NativeSettings
local function pushChanges()
Game.GetSettingsSystem():ConfirmChanges()
Cron.After(0.25, function()
ModOptions.refresh()
end)
end
---Refresh Ray Reconstruction if preset or sharpness was changed
local function refreshDLSSD()
previous.hasDLSSD = runtime.hasDLSSD
previous.dlssSharpness = GameSettings.Get("/graphics/presets", "DLSS_NewSharpness")
previous.dlssPreset = GameSettings.Get("/graphics/presets", "DLSS")
previous.dlssBackend = GameSettings.Get("/graphics/presets", "DLSS_BackendPreset")
Debug:Info("Refreshing DLSS Ray Reconstruction - " .. previous.dlssPreset)
--Enabling NRD also disables DLSSD
GameSettings.Set("RayTracing", "EnableNRD", "true")
GameSettings.Set("/graphics/presets", "DLSS_D", false)
pushChanges()
Cron.After(0.025, function()
--This should be true, but set it to false also enables DLSSD and reduce noise while refreshing DLSS_D
GameSettings.Set("RayTracing", "EnableNRD", "false")
Cron.After(settings.fastTimeout, function()
--DLSSD is already enabled, but this will update game settings
GameSettings.Set("/graphics/presets", "DLSS_D", true)
GameSettings.Set("RayTracing", "EnableNRD", "false")
pushChanges()
end)
end)
end
---Check for Ray Reconstruction changes in preset or sharpness
---@return boolean
local function hasDLSSDChanged()
return previous.hasDLSSD ~= runtime.hasDLSSD
or previous.dlssPreset ~= GameSettings.Get("/graphics/presets", "DLSS")
or previous.dlssSharpness ~= GameSettings.Get("/graphics/presets", "DLSS_NewSharpness")
or previous.dlssBackend ~= GameSettings.Get("/graphics/presets", "DLSS_BackendPreset")
end
---Set Ray Reconstruction particles control detection
---@param dlssdParticles boolean
function setDLSSDParticlesControl(dlssdParticles)
settings.dlssdParticles = dlssdParticles
if not runtime.particleTimer and dlssdParticles then
-- enable particle PT integration unless player is outdoors AND it's raining
runtime.particleTimer = Cron.Every(settings.fastTimeout * 2.0, function()
if runtime.inGame then
local isRaining = GameSettings.IsRaining()
local isIndoors = GameSettings.IsIndoors()
--Change detection
if isRaining ~= previous.isRaining or isIndoors ~= previous.isIndoors then
previous.isRaining = isRaining
previous.isIndoors = isIndoors
if isIndoors or isRaining then
Debug:Info("It's raining or is indoors. Enabling DLSSD separate particle color")
GameSettings.Set("Rendering", "DLSSDSeparateParticleColor", "true")
else
Debug:Info("It's not raining and it's outdoors. Disabling DLSSD separate particle color")
GameSettings.Set("Rendering", "DLSSDSeparateParticleColor", "false")
end
end
end
end)
end
if dlssdParticles then
Debug:Info("Resume Particle Timer")
Cron.Resume(runtime.particleTimer)
else
if runtime.particleTimer then
Debug:Info("Pause Particle Timer")
Cron.Pause(runtime.particleTimer)
end
GameSettings.Set("Rendering", "DLSSDSeparateParticleColor", "false")
end
checkCustomPreset('dlssdParticles', dlssdParticles)
end
---Set NRD denoiser control (timer only)
---@param nrdControl boolean
function setNRDControl(nrdControl)
--Keep last setting when user disables DLSSD
if runtime.hasDLSSD then
settings.nrdControl = nrdControl
end
if not runtime.nrdTimer and nrdControl then
runtime.nrdTimer = Cron.Every(settings.slowTimeout, function()
--hasDLSSD should not be necessary but sometimes the timer dosen't stop at the right time and executes one more time
if runtime.inGame and runtime.hasDLSSD then
Debug:Info("Disabling NRD")
GameSettings.Set("RayTracing", "EnableNRD", "false")
end
end)
end
if nrdControl and runtime.hasDLSSD then
Debug:Info("Resume NRD Control")
Cron.Resume(runtime.nrdTimer)
elseif runtime.nrdTimer then
Debug:Info("Pause NRD Control")
Cron.Pause(runtime.nrdTimer)
--Just for aid user understanding. NativeSettings does not trigger if value stays the same
ModOptions.setOption('nrdControl', false)
end
end
---Set Refresh Game control (timer only)
---@param refreshGame boolean
function setRefreshControl(refreshGame)
settings.refreshGame = refreshGame
runtime.refreshGame = refreshGame
if settings.refreshInterval > 0 then
if not runtime.refreshTimer and refreshGame then
--In minutes
runtime.refreshTimer = Cron.Every(settings.refreshInterval * 60, function()
Debug:Info("Enabling Refresh Game for the next time")
runtime.refreshGame = true
end)
end
elseif refreshGame then
Debug:Info("Enabling Refresh Game every time")
end
if refreshGame then
Debug:Info("Resume Refresh Control")
Cron.Resume(runtime.refreshTimer)
elseif runtime.refreshTimer then
Debug:Info("Pause Refresh Control")
Cron.Pause(runtime.refreshTimer)
else
Debug:Info("Refresh Control is disabled")
end
end
---Set a new interval for the Refresh Game control. Will reset current timer
---@param time number
function setRefreshTime(time)
if runtime.refreshTimer then
Cron.Halt(runtime.refreshTimer)
end
runtime.refreshTimer = nil
settings.refreshInterval = time
end
function setRefreshNow()
runtime.refreshGame = true
previous.dlssPreset = nil
Debug:Info("Will refresh game and DLSSD")
end
---Set ReGIR PT mode. ReGIR will be enabled after a fast timeout * 1.5
local function setReGIR()
Debug:Info("Disabling ReGIR")
GameSettings.Set("Editor/ReGIR", "UseForDI", "false")
GameSettings.Set("Editor/ReGIR", "Enable", "false")
if runtime.enableReGIR then
--Regir requires to wait a bit before be enabled
Cron.After(settings.fastTimeout * 1.5, function()
Debug:Info("Enabling ReGIR")
runtime.reGIRApplied = true
GameSettings.Set("Editor/ReGIR", "Enable", "true")
--Only for ReGIR DI/GI
if settings.ptMode == 4 then
GameSettings.Set("Editor/ReGIR", "UseForDI", "true")
end
end)
end
end
---Set ReSTIR PT mode. Will trigger ReGIR if enabled, and also refreshes Ray Reconstruction
local function setReSTIR()
if runtime.enableReSTIR then
Debug:Info("Enabling ReSTIRGI")
GameSettings.Set("Editor/ReSTIRGI", "Enable", "true")
else
Debug:Info("Disabling ReSTIRGI")
runtime.enableReGIR = false
GameSettings.Set("Editor/ReSTIRGI", "Enable", "false")
end
local dlssdHasChanged = hasDLSSDChanged()
if dlssdHasChanged then
Debug:Info('DLSSD has changed')
runtime.reGIRApplied = false
end
if not runtime.reGIRApplied then
setReGIR()
end
if runtime.hasDLSSD and dlssdHasChanged then
Cron.After(settings.fastTimeout * 2.0, function()
refreshDLSSD()
end)
end
end
---Set the PT Ray Number. Only works in ReSTIR DI mode
---@param number integer
---@param photomode boolean
function setRayNumber(number, photomode)
if photomode then
Debug:Info("Setting Ray Number for Photo Mode")
settings.rayNumberPm = number
GameSettings.Set("RayTracing/Reference", "RayNumberScreenshot", tostring(number))
GameSettings.Set("RayTracing/ReferenceScreenshot", "SampleNumber", tostring(number))
else
Debug:Info("Setting Ray Number")
settings.rayNumber = number
GameSettings.Set("RayTracing/Reference", "RayNumber", tostring(number))
end
checkCustomPreset('rayNumber', number)
end
---Set the PT Ray Bounce Number. Only works in ReSTIR DI mode
---@param number integer
---@param photomode boolean
function setRayBounce(number, photomode)
if photomode then
Debug:Info("Setting Ray Bounce for Photo Mode")
settings.rayBouncePm = number
GameSettings.Set("RayTracing/Reference", "BounceNumberScreenshot", tostring(number))
else
Debug:Info("Setting Ray Bounce")
settings.rayBounce = number
GameSettings.Set("RayTracing/Reference", "BounceNumber", tostring(number))
end
checkCustomPreset('rayBounce', number)
end
---Set NVIDIA's SHARC
---@param sharc boolean
function setSharc(sharc)
settings.sharc = sharc
--Trigger a DLSS Refresh is better on changing SHARC
previous.hasDLSSD = nil
if not (sharc and runtime.enableReGIR) then
Debug:Info("Setting SHARC")
GameSettings.Set("Editor/SHARC", "Enable", tostring(sharc))
checkCustomPreset('sharc', sharc)
else
settings.sharc = false
Debug:Info("Skipping SHARC because ReGIR is enabled")
ModOptions.setOption('sharc', false)
end
end
---Set PT mode
---@param mode integer the index of PT mode
function setPTMode(mode)
settings.ptMode = mode
if settings.ptMode == 1 then
--ReSTIR DI
Debug:Info("Setting Path Tracing Mode: ReSTIR DI")
runtime.enableReGIR = false
runtime.enableReSTIR = false
runtime.reGIRApplied = false
GameSettings.Set("Editor/SHARC", "Enable", tostring(settings.sharc))
elseif settings.ptMode == 2 then
--ReSTIR DI/GI
Debug:Info("Setting Path Tracing Mode: ReSTIR DI/GI")
runtime.enableReGIR = false
runtime.reGIRApplied = false
runtime.enableReSTIR = true
GameSettings.Set("Editor/SHARC", "Enable", tostring(settings.sharc))
else
--ReGIR
if mode == 3 then
Debug:Info("Setting Path Tracing Mode: ReSTIR DI + ReGIR GI")
else
Debug:Info("Setting Path Tracing Mode: ReGIR DI/GI")
end
previous.hasDLSSD = nil
runtime.enableReGIR = true
runtime.enableReSTIR = true
GameSettings.Set("Editor/SHARC", "Enable", "false")
ModOptions.setOption('sharc', false)
end
checkCustomPreset('ptMode', mode)
end
---Set PT Quality
---@param quality any the PT quality index
function setPTQuality(quality)
Debug:Info("Setting Path Tracing Quality")
settings.ptQuality = quality
GameSettings.SetAll(ptSettings.quality[quality])
checkCustomPreset('ptQuality', quality)
end
---Set Light Frame Accumulation (RTXDI MaxHistoryLength)
---@param rtxdiHistory boolean
function setRTXDIHistory(rtxdiHistory)
Debug:Info("Setting Light Frame Accumulation")
settings.rtxdiHistory = rtxdiHistory
GameSettings.Set("Editor/RTXDI", "MaxHistoryLength", ptSettings.rtxdiHistory[rtxdiHistory])
checkCustomPreset('rtxdiHistory', rtxdiHistory)
end
---Set PT optimizations
---@param ptTweaks boolean
function setPTTweaks(ptTweaks)
Debug:Info("Setting Path Tracing Tweaks")
settings.ptTweaks = ptTweaks
GameSettings.SetAll(ptSettings.tweaks[ptTweaks])
checkCustomPreset('ptTweaks', ptTweaks)
end
---Set global light to enabled or disabled
---@param globalLight boolean
function setGlobalLight(globalLight)
Debug:Info("Setting Global Light")
settings.globalLight = globalLight
GameSettings.Set("Editor/RTXDI", "EnableGlobalLight", tostring(globalLight))
checkCustomPreset('globalLight', globalLight)
end
---Set self reflections to show or not
---@param selfReflection boolean
function setSelfReflection(selfReflection)
Debug:Info("Setting Self Reflection")
settings.selfReflection = selfReflection
GameSettings.Set("RayTracing", "HideFPPAvatar", tostring(not selfReflection))
checkCustomPreset('selfReflection', selfReflection)
end
---Set Path Tracing presetNumber
---@param preset integer
function setPTPreset(preset)
local ptQualityPreset = ptSettings.preset[preset]
settings.ptPreset = preset
if isPresetInRange(preset) and ptQualityPreset then
local presetOption = ModOptions.getNativeSettings('ptPreset')
Debug:Info(string.format('Setting PT Preset "%s"', presetOption.range[preset]))
--Settings only change if current value is different from ptQualityPreset value
for index, value in pairs(ptQualityPreset) do
--print(string.format('[[[ Index/Value: %s/%s ]]]', index, value))
ModOptions.setOption(index, value)
end
else
Debug:Info('Setting PT Preset "Custom"')
end
end
---Apply the PT preset settings
---@param preset any
function applyPTPreset(preset)
--The setNativeSettings only loads native settings to the preset settings, but does set engine values
--Use index 1 just for reference
for index, _ in pairs(preset) do
--print(string.format('[[[ Index/Value: %s/%s ]]]', index, settings[index]))
ModOptions.callAction(index, settings[index])
end
end
---Check for refresh game
local function checkRefreshGame()
if runtime.refreshGame then
if GameSettings.CanRefresh() then
--Apply delay for LUTSwitcher
GameSettings.RefreshGame(settings.refreshPauseTimeout, 0.45, _Mod)
--Times 0 always will refresh the game, so never false
if settings.refreshInterval > 0 then
runtime.refreshGame = false
end
else
--Should not refresh due to limited gameplay scene
Debug:Info("Can't Refresh now")
end
elseif settings.refreshGame then
--It could refresh but hasn't passed enough time or refresh is disabled
Debug:Info("Won't Refresh now")
end
end
---Update Runtime mod state and refresh settings
local function updateRuntime()
runtime.hasDLSSD = GameSettings.HasDLSSD()
GameSettings.Set("RayTracing", "EnableNRD", tostring(not runtime.hasDLSSD))
if runtime.firstLoad then
Debug:Info('First Load')
runtime.firstLoad = false
end
if GameSettings.HasPathTracing() then
setReSTIR()
end
checkRefreshGame()
end
---Set runtime mod state controls and events
local function setRuntime()
GameUI.Listen(function(state)
--GameUI.PrintState(state)
--Some events if you clear console can trigger an access memory violation when exiting to MainMenu. Game, CET or GameUI fault?
if state.event == 'SessionStart' or state.event == 'FastTravelFinish' then
runtime.inGame = true
runtime.refreshGame = settings.refreshGame
updateRuntime()
elseif state.event == 'SessionEnd' or state.event == 'FastTravelStart' then
runtime.inGame = false
runtime.reGIRApplied = false
previous.hasDLSSD = nil
end
end)
GameUI.OnMenuClose(function(state)
--Avoid update runtime if the player may use fast travel
if runtime.inGame and state.lastMenu ~= "FastTravel" then
updateRuntime()
end
end)
GameUI.OnMenuNav(function(state)
if state.lastSubmenu == "Settings" then
runtime.hasDLSSD = GameSettings.HasDLSSD()
if not runtime.hasDLSSD then
--If user disables DLSSD and don't open Mod settings NRD Control has to turn off
ModOptions.setOption('nrdControl', false)
else
ModOptions.setOption('nrdControl', settings.nrdControl)
end
--Only update refreshTime on menu close. Condition is only for the messaging
if settings.refreshGame and not runtime.refreshTimer then
Debug:Info(string.format('Refresh time updated to %ss', settings.refreshInterval))
setRefreshControl(settings.refreshGame)
end
--Keep event settings updated
_Mod.settings = Debug:Clone(settings)
ModSettings.saveSettings(settings, settingsFilename)
end
end)
runtime.inGame = not GameUI.IsDetached()
runtime.hasDLSSD = GameSettings.HasDLSSD()
end
registerForEvent('onInit', function()
settings = ModSettings.loadSettings(defaults, settingsFilename)
if ModOptions.loadNativeSettings(settings, defaults) then
setRuntime()
applyPTPreset(ptSettings.preset[1])
setNRDControl(settings.nrdControl)
setRefreshControl(settings.refreshGame)
Debug:Log(string.format('%s v%s loaded', _Mod.name, settings.version))
_Mod.settings = Debug:Clone(settings)
else
Debug:Error('Failed to load Advanced Path Tracing: NativeSettings missing')
end
end)
registerForEvent('onUpdate', function(delta)
Cron.Update(delta)
end)
registerHotkey("refresh_game", "Refresh Game", function()
if runtime.inGame then
runtime.refreshGame = true
checkRefreshGame()
end
end)
return _Mod