Skip to content

Batman Arkham Knight: engine motion vectors and jitter, DLSS/FSR upscaling, FSR 3 - #234

Merged
garamond13 merged 11 commits into
Filoppi:mainfrom
Gargauth:bak
Nov 16, 2026
Merged

garamond13 merged 11 commits into
Filoppi:mainfrom
Gargauth:bak

Conversation

@Gargauth

Copy link
Copy Markdown
Contributor

Summary

This makes DLSS/FSR in Batman: Arkham Knight work from the engine's own data instead of approximations, and adds upscaling (quality modes) and FSR 3.

It should address:

Note: this rewrites most of the WIP main.cpp. The commits are split by topic, so I'm happy to split the PR or drop parts if you'd rather take only some of it.

Changes (one commit each)

  1. Engine motion vectors and jitter
    • The engine only computes velocities (camera and objects) with motion blur on, and skips the camera velocity pass when the camera barely moves. Four gates are patched so velocities are always computed; the motion blur effect itself still follows the user setting.
    • The camera velocity compute shader (0x47811EB4) is replaced. The original used projection-divided units, no jitter, and an approximated camera translation. The replacement reprojects with one matrix built on the CPU in double precision: inverse current translated view projection, camera translation since the last frame, previous translated view projection.
    • Jitter goes into the main view projection inside the engine's own view jitter function, before all the derived view matrices are built, so shadows and scene captures are not jittered. DLSS gets the jitter read back from the view being rendered (same frame, no game/render thread mismatch).
    • MV scale is the render resolution: the MVs are "previous UV - current UV".
  2. FSR 3: UseLumaFSR is enabled.
  3. Upscaling (Native/DLAA, Quality, Balanced, Performance, Ultra Performance)
    • The engine has a UE3 ScreenPercentage path, but it is broken on PC:
      • the scaled view rect is centered for compute passes, while raster passes draw it at the top left;
      • the final upscale reads black, because its source texture is still bound as a UAV.
    • The fix keeps the rect at the top left and disables the engine upscale.
    • The view scaling reads a mod-owned percentage, so the game setting is never touched and can't be saved to the game config.
    • After SR, the view is switched to the output rect before the first post process (via InitRenderTargetRect), so bloom, tonemapping, DOF and motion blur run at output resolution. The two post shaders that still read render-res depth/velocity (Gather<0>, RockOn preamble) are replaced to scale their load coordinates.
    • Jitter phases scale with the upscale ratio. The mip bias follows the render scale, with a user offset.
  4. Fix: when upscaling, the camera velocity pass constants hold the full buffer size instead of the view rect, so the pixel NDC now comes from the view rect.
  5. Game exposure and camera cuts
    • SR gets the game's eye adaptation exposure: 1 / the 1x1 divisor the tonemapper uses (option, on by default).
    • The camera velocity pass also runs on engine-flagged camera cut frames (bPrevTransformsReset, bCameraCut, bInstantlySwitchPostProcessVolume). History is reset exactly once per cut instead of twice.
  6. clang-format with the repository rules.

How it was verified

Reverse engineering was done on the Steam exe, using RenderDoc captures of the game without the mod and shader cache disassembly. In-game measurements came from a temporary debug build: it read back the GPU view constant buffers and a grid of velocity buffer pixels a few frames later and exported them to an external script. That debug code is not part of this PR.

  • Motion vectors, while walking with a moving camera, comparing static pixels against the reprojected geometry (best-fit depth):
    • an earlier iteration that ignored the camera origin shift between frames: 2-9 px off near the bottom of the screen;
    • at Quality before fix 4: 0.3-1.9 px off;
    • final: ≤ 0.04 px at DLAA and ≤ 0.02 px at Quality, everywhere on static geometry. Characters keep their own motion, as expected.
    • In the unmodded game with motion blur off, static geometry has no motion vectors at all (the camera velocity pass doesn't run).
  • Jitter: the jitter in the GPU view projection matched the jitter sent to DLSS in 100% of frames, for both the current and previous frame. This was checked over 100 frames at DLAA and 4,716 frames at Quality 1280x720 -> 1920x1080, with 18 Halton phases at Quality.
  • Original camera velocity shader: a Python replica matched the engine output to fp16 precision (correlation 0.9999997) before it was replaced.
  • Exposure semantics: confirmed with the RenderDoc shader debugger in 4 captures. The tonemapper input is (scene + bloom) / tex. A Python reimplementation of the adaptation shader matches the GPU output.
  • Signatures and shaders:
    • every signature matches exactly once in .text;
    • the code patches were simulated on the exe bytes and disassembled;
    • the replaced shaders compile, and in the non-upscaling path they produce the same instruction mix as the originals.
  • Visual testing: done by me on an RTX 5070 Ti, Steam version, 1920x1080 windowed, DLSS 310.9.1 with presets K and M, plus FSR 3:
    • DLAA: the diamond plate floor no longer shimmers or smears while walking or turning, and the image is stable when standing still;
    • Quality: no shimmering after fix 4;
    • FSR 3 works at native resolution;
    • the motion blur on/off setting still works independently.

Not tested

  • GOG and EGS executables (the signatures may or may not match)
  • 4K and other output resolutions in game (the screen percentage math was checked for common resolutions)
  • AMD GPUs
  • FSR 3 upscaling modes beyond a quick check
  • A full set of cutscene camera cuts
  • DSR/DLDSR combinations

Known gap: the engine doesn't flag the switch from a cutscene camera back to the gameplay camera as a cut, so that one frame isn't reset.

Disclosure

The reverse engineering and the code were done with the help of an AI coding assistant. Everything claimed above was measured (in game or in captures) or statically checked as described, and I tested it in game myself.

- Always compute the engine's velocities (camera and objects): remove the
  motion blur setting gates and the "camera barely moved" early out of the
  camera velocity pass, by signature, while the motion blur effect itself
  still follows the user setting.
- Replace the camera velocity compute shader: reproject with a single matrix
  built on the CPU in double precision (inverse current translated view
  projection, camera translation since last frame, previous translated view
  projection). The original used different units than the object
  velocities, no jitter and an approximated camera translation, which made
  DLSS reject history (shimmering and aliasing while moving).
- Jitter the main view projection in the engine's own view jitter function,
  before all the derived view matrices, and read the jitter back from the
  rendered view for DLSS (same frame, no game/render thread mismatch).
  Views built from an already jittered view keep its jitter.
- MVs are "previous UV - current UV": scale by the render resolution.
- Reset DLSS when the camera velocity pass was skipped (camera cuts) or has
  no previous frame.
- Find all patch sites by signature (no Steam/GOG offsets).
- Render at a lower resolution through the engine's ScreenPercentage view
  scaling, read from a mod owned value (the game setting is never touched,
  so it can't be saved to the game config). Quality modes: Native (DLAA),
  Quality, Balanced, Performance, Ultra Performance.
- Fix the engine's broken ScreenPercentage path on PC: keep the scaled view
  rect at the top left (it was centered for compute passes only), and
  disable the engine upscale (it read black, the texture being still bound
  as UAV).
- After super resolution, switch the view to the output rect before the
  first post process, so bloom, tonemapping, DOF and motion blur run at the
  output resolution. Replace the two post shaders that still read the render
  resolution depth/velocity (Gather<0>, RockOn preamble) to scale their
  load coordinates.
- Pick the screen percentages so that the HUD canvas unscales back to exactly
  the output size (the engine truncates both ways).
- More jitter phases when upscaling (8 * ratio^2), mip bias from the render
  scale, plus a user mip bias offset.
…ct when upscaling

The camera velocity pass constants have the full buffer size, not the
(smaller) view rect, so camera motion vectors were scaled wrong when
upscaling (up to ~2 px off near the screen edges, making DLSS reject
history on the floor while moving). Measured in game: static pixels now
match the reprojected geometry within 0.02 px at Quality.
…era cuts

- Feed DLSS/FSR the game's eye adaptation exposure (1 / the 1x1 divisor
  written by TRockToneMap, converted by a 1x1 compute pass) instead of their
  auto exposure (option, on by default; DLSS uses it with presets J and K).
- Run the camera velocity pass on engine flagged camera cut frames too
  (bPrevTransformsReset, bCameraCut, bInstantlySwitchPostProcessVolume): it
  writes no motion and resets SR, but stores the camera so the next frame
  has history again (it reset twice before).
- Delete the game device data with its real type (its resources leaked).
@Filoppi

Filoppi commented Oct 13, 2026

Copy link
Copy Markdown
Owner

R u on discord?

@Gargauth

Copy link
Copy Markdown
Contributor Author

R u on discord?

HDR Den? Yes.

…ctors

All the game velocities (the replaced camera velocity and the materials'
object velocities) come from the jittered current and previous projections,
so they contain "previous jitter - current jitter". They were passed with
MVJittered. A small compute pass now removes that jitter change (the same for
every pixel) into a separate texture for DLSS/FSR, and mvs_jittered is false,
so the result doesn't depend on how each SR implementation cancels jitter.
The engine motion blur still reads the original velocities.

Verified with the DLSS debug overlay: the input motion vectors of a static
scene are now zero (they shook before). An in-game A/B against MVJittered
showed no visible difference in the DLSS output.
@Filoppi

Filoppi commented Nov 6, 2026

Copy link
Copy Markdown
Owner

What's your name on discord?

@Gargauth

Copy link
Copy Markdown
Contributor Author

@Filoppi panogden

…reconstruction

The decal pixel shaders (FRockDecalPixelShader, 1084 variants in the combined
shader cache) rebuild the view position as "ndc * (1 / projection scale) * depth",
which is only right for a centered projection. With the jittered projection it's
off by the jitter (up to half a pixel, different every frame).

FRockDecalVertexShader (0x52595883) is replaced: it passes the pixel shaders the
clip position with the jitter removed. The jitter is taken from the view
projection in cb1[0..3], so views without jitter are unaffected. SV_Position is
unchanged.

Checked statically only: same signatures as the original, and a float32
simulation of the vertex + pixel shader math (8-72 jitter phases, 360p-4K) gives
a position error below 0.001 px (0.44-0.49 px before) with the same depth pixel.
In game there was no visible difference before and after in the places tried.
…e patches can't be applied

- The velocity and jitter patches are now all or nothing (like the upscaling
  ones). Without them super resolution isn't used and SMAA stays, instead of
  running with unusable velocities and a reset every frame.
- The replaced camera velocity shader runs even without its hook: the
  reprojection matrix and view rect now default to "no motion" instead of
  zeroes (0 / 0 velocities).
- The projection is only jittered after a frame where super resolution ran:
  it doesn't run with the game anti aliasing off, and nothing else resolves
  the jitter.

Only compiled: the unsupported executable path can't be reached on the Steam build.
The engine lighting is checkerboarded: FTileShadingComputeShader shades every
other pixel (x = 2 * thread x + (y & 1)) and FRecomposeComputeShader fills in the
rest. The two packed outputs sit side by side, the second "int(view width * 0.5)"
columns to the right, so with an odd view width the last column of the first one
lands on the first column of the second one. Odd widths came up at e.g. 1920x1080
Balanced (1113) and 3440x1440 Quality (2293).

From the shader disassembly, the effect in game wasn't observed. The percentage
search was checked for 15 output resolutions x 4 modes: always an even width, at
most 0.53 percentage points above the nominal scale.
…ter aware in all shaders

The game shaders that rebuild a position from the scene depth turn NDC into a
view ray with the projection scale only, which is only right for a centered
projection. With the jittered one the position is off by the jitter, differently
every frame, so decals, lighting, shadows and AO shimmer.

They are now live patched when the game creates them (jitter_patch.h), 2073
shaders in the shader caches:
- 1084 deferred decal pixel shaders and 741 material pixel shaders:
  "ndc * scale" becomes "(ndc - jitter) * scale"
- 248 compute shaders (tiled lighting, reflections, AO): "ndc / projection"
  becomes "(ndc - jitter) / projection", and the opposite where a view position
  is projected back to the screen
The jitter is derived in the shader from the view constants it already has
(cb1[0..2] and cb1[17]), so it's zero for views without jitter. Nothing is
patched if the engine jitter patch couldn't be applied.

This replaces the FRockDecalVertexShader replacement (0x52595883): the decals
are now fixed in their pixel shaders, which also keeps the depth pixel
coordinates untouched.

OnBindPatchedShader() returns false so the game's own (in place patched) shader
objects stay bound instead of Luma's later clones. 74 of the patched compute
shaders use NVIDIA shader extensions (NV_EXTN_OP_VOTE_BALLOT through the fake UAV
at u7), which only work in shaders created while the game has the extension
slot set: with the clones whole lighting tiles flickered.

These are the same rewrites as the "Dlss Anti Aliasing" mod on Nexus (3530) does:
the patcher output was compared with it for the 29936 pixel and compute shaders
of the game caches, the same 2073 are patched, with identical instructions.

In game (RTX 5070 Ti, 4K): the jitter derived from the constants matches the one
we set in 900 sampled dispatches and decal draws. The tile flicker is gone on the
spot it was found on (16x16 blocks changing between frames: 0.6%, 4-12% with the
clones, 0.2% unpatched). Whether distant lighting is visibly calmer wasn't
compared, and other GPU vendors weren't tried.
@garamond13
garamond13 merged commit b881c40 into Filoppi:main Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants