Repository navigation
Batman Arkham Knight: engine motion vectors and jitter, DLSS/FSR upscaling, FSR 3 - #234
Merged
Merged
Conversation
- 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).
Owner
|
R u on discord? |
Contributor
Author
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.
Owner
|
What's your name on discord? |
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)
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.UseLumaFSRis enabled.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.bPrevTransformsReset,bCameraCut,bInstantlySwitchPostProcessVolume). History is reset exactly once per cut instead of twice.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.
(scene + bloom) / tex. A Python reimplementation of the adaptation shader matches the GPU output..text;Not tested
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.