AmnesiaTheBunker.OptiPerf 1.0.0
===================================

Performance fix for Amnesia: The Bunker (64-bit Windows), made for high frame
rates.

The game runs an update (physics, scripts, AI, the player) and then draws a
frame, one after the other on the same thread. At high rates, set with FPS
Unlock, the processor cannot do both in time: the frame rate falls short of
the update rate, and the game runs two or more updates in some frames to
catch up, which makes motion stutter. OptiPerf draws each frame on a thread of
its own while the next update runs, and runs exactly one update per frame.

Measured on the PC it was developed on (GeForce RTX 4090, Core i9-13900K), on
a walk through the bunker with one update per frame: about 240 frames per
second without OptiPerf and about 415 with it, uncapped. Set to 360 updates
per second, it held 358 frames per second.


What it does
------------
- Render thread: the game's drawing (culling, render setup, the drawing
  passes and the present) runs on a thread of its own, overlapped with the
  next update.
- One update per frame: no more catch-up updates. A frame that comes late
  gets a longer update instead, so the game keeps real time.
- DXGI present: frames go to the screen through a flip-model swapchain with
  tearing allowed, for the lowest latency. G-SYNC and FreeSync follow the
  frame rate, and the RivaTuner Statistics Server overlay works.
- Stairs fix: the game counts the player as on the ground for 12 updates
  after losing contact. That is 200 ms at 60 updates per second but 34 ms at
  357, so running down stairs became a series of small falls and landings
  (the camera dips, sprinting stops). OptiPerf keeps the 200 ms at any rate.
- Thread placement: the two busy threads go to separate processor cores, the
  ones least busy with device interrupts.


Requirements
------------
- Amnesia: The Bunker for Windows from GOG (AmnesiaTheBunker.exe, GOG build
  57301760292462330). The Steam release includes
  AmnesiaTheBunker_NoSteam.exe, the same file as GOG's executable; OptiPerf
  runs with it too, untested. Steam's AmnesiaTheBunker.exe is a different
  build. Before changing anything OptiPerf checks the game code it depends
  on: on any other build, or after a game update, it changes nothing and its
  log says why.
- An x64 ASI loader (see "Choose an installation").
- Recommended: Amnesia: The Bunker FPS Unlock (BunkerHighFPS.asi), to set
  the update rate above the 120 of the game's Framerate option. With
  OptiPerf the update rate is the frame rate. Without FPS Unlock, OptiPerf
  works at the rate of the Framerate option.
- Not together with AmnesiaTheBunker.FPSFix, the frame interpolation fix: it
  draws several frames per update, OptiPerf one. Use one or the other; the
  two have not been tested together.


Choose an installation
----------------------
Plugin-only archive:
  For a game folder that already has an x64 ASI loader. Copy
  AmnesiaTheBunker.OptiPerf.asi and AmnesiaTheBunker.OptiPerf.ini beside
  AmnesiaTheBunker.exe, or into the plugin folder your loader uses. Keep
  both files together.

Archive with Ultimate ASI Loader:
  For a game folder without an ASI loader. Copy version.dll,
  AmnesiaTheBunker.OptiPerf.asi and AmnesiaTheBunker.OptiPerf.ini beside
  AmnesiaTheBunker.exe. The bundled version.dll is Ultimate ASI Loader x64
  v9.7.4 by ThirteenAG; its license is included.

  If the folder already has a version.dll, do not overwrite it without
  finding out what it is and backing it up. If it is already an ASI loader,
  for example from FPS Unlock's archive with the loader, copy only the .asi
  and the .ini.

For either installation:
1. Close the game before copying files.
2. With FPS Unlock, set iUpdatesPerSec in BunkerHighFPS.ini to the frame
   rate you want, since every frame is one update. With G-SYNC or FreeSync,
   stay a few below the display's refresh rate so it keeps to its variable
   refresh range, for example 357 on a 360 Hz display.
3. Start the game normally.


Settings (AmnesiaTheBunker.OptiPerf.ini)
----------------------------------------
The file describes every setting. Edit it while the game is closed. Set
Enabled = false under [General] to switch OptiPerf off without removing it.

[Present] Mode = dxgi presents without VSync: with G-SYNC or FreeSync the
display follows the frames, without variable refresh the picture can tear.
Mode = native keeps the game's own present, where its VSync option applies.

[LockStep] Mode = variable (the default) keeps the game in real time with one
update per frame. paced lets the game slow down instead when a frame is late;
off brings back the game's catch-up updates. MaxStepMs (16.667, the game's
own 60 updates per second) is the longest update: below 60 frames per second
the game slows down.

[RenderThread] Affinity = none lets Windows place the threads.

[Fixes] GroundGraceRate = 0 restores the game's own on-ground timing.


Known limits
------------
  - Tested on one PC: GeForce RTX 4090, Core i9-13900K, Windows 11, a 360 Hz
    G-SYNC display, the game in borderless fullscreen with FPS Unlock at 357
    updates per second. AMD and Intel graphics have not been tried. The DXGI
    present needs OpenGL-to-Direct3D sharing in the graphics driver
    (GL_EXT_memory_object_win32 or WGL_NV_DX_interop2); without it OptiPerf
    falls back to the game's own present.
  - While a frame is drawn, the next update already runs. A few things the
    drawing reads late (particles, flickering lights) can be one update ahead
    of the rest of the frame. Nothing of the kind was noticed in testing.
    Characters, the player's hands and body included, are drawn exactly as
    their update left them.
  - The game has not been played to the end with this version. Gamepads,
    mods and custom stories are untested.


Diagnostics
-----------
OptiPerf writes AmnesiaTheBunker.OptiPerf.log beside the .asi each time the
game starts, and keeps the previous one as
AmnesiaTheBunker.OptiPerf.previous.log. A working start lists each part as it
is installed (render thread, lock-step, present) and then "Hooks live".

  - No log file: the .asi was not loaded. Check that an ASI loader is in the
    game folder and that the .asi is where the loader looks for it.
  - "Not the supported game build": this is not the supported executable;
    nothing was changed.
  - A part that switches itself off writes the reason in the log; the others
    keep working.

Set Report = true under [Probe] before reproducing a problem you want to
report: the log then gets a short report every five seconds (frame rate,
game speed, where the time went).


Uninstallation
--------------
Close the game and remove AmnesiaTheBunker.OptiPerf.asi,
AmnesiaTheBunker.OptiPerf.ini, AmnesiaTheBunker.OptiPerf.log and
AmnesiaTheBunker.OptiPerf.previous.log.

Remove version.dll and ULTIMATE-ASI-LOADER-LICENSE.txt only if you installed
them for this fix and no other ASI plugin needs the loader.

OptiPerf changes nothing on disk except its own log. Saves, the game's
settings file and the game executable are not modified.

This software is provided without warranty. Back up your saves before
installing game modifications.


Licenses
--------
OptiPerf is under the MIT License. It includes MinHook by Tsuda Kageyu (BSD
2-Clause License, MINHOOK-LICENSE.txt). The archive with the loader also
includes Ultimate ASI Loader by ThirteenAG (MIT License,
ULTIMATE-ASI-LOADER-LICENSE.txt).

Misc
-------
For more game fixes, visit https://slop-blog.enkhayzomachines.net/fixes :)
