Skip to content

Logarithmic depth buffer implementation for GTA IV to fix z-fighting

License

Notifications You must be signed in to change notification settings

Parallellines0451/GTAIV.EFLC.LogDepth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GTAIV.EFLC.LogDepth

Grand Theft Auto IV on PC suffers from severe z-fighting due to the use of a conventional depth buffer, as opposed to the console versions, which use reversed depth. This repository provides a logarithmic depth implementation for the game's shaders, which mitigates the issue similarly to reversed depth, and it's also the base for Fusion Shaders.

A showcase is available here.

Requirements

All game versions post 1.0.6.0 are supported.

There are some prerequisites for these shaders to work correctly and examples of each of them are implemented in FusionFix:

  • Depth bounds test (specifically D3DRS_ADAPTIVETESS_X) needs to be disabled to fix light pop-in on Nvidia graphics cards, and on AMD if using DXVK
  • The game's current near and far clip values need to be sent to the c227 vertex shader register for corona depth test to work (in deferred_lightingVS11)
  • Near and far clip overrides need to be removed from cutscenes and timecycmodifiers in order to prevent flickering as the game tends to update their values erratically

Notes and complementary fixes

  • The ZShift sign in gta_emissivestrong shaders was erroneously inverted by patch 1.0.6.0, which made several lights invisible; this was reverted, but it causes clipping on traffic lights because that patch also enabled D3DRS_ZWRITEENABLE for emissives, thus optional adjusted models are provided here
  • Linear2Log code is the same across all shaders except emissive ones due to ZShift; Log2Linear is the same across all but gta_rmptfx_litsprite, as a fix for soft particles was included, courtesy of robi29
  • Since the game uses the same registers for orthographic and perspective projection, Linear2Log only writes logarithmic depth in the case of the latter to prevent rain and UI issues

Conclusion

Special thanks to RaphaelK12, AssaultKifle47, robi29, ThirteenAG and Shvab. Fixing this monumental issue wouldn't have been possible without their help.

References:

Functions used:

  • Linear2Log = log2(W_clip / near) / log2(far / near)
  • Log2Linear = (far * (W_clip - near)) / (W_clip * (far - near))

About

Logarithmic depth buffer implementation for GTA IV to fix z-fighting

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published