Skip to content

Commit

Permalink
Mad Max: Fix bloom/lens flare opacity being calculated from wrong spo…
Browse files Browse the repository at this point in the history
…t on depth buffer

Calculating the opacity from the wrong spot on the depth buffer leads to
the bloom & lens flare appearing & disappearing when they should not,
being too pronounced, or not visible at all when it should be.

The pixel shader has the depth of the light source in Z buffer scale,
which we can use to correct this. Scale it out to world Z and use it to
offset the location of the depth buffer read to determine the opacity of
the bloom & lens flare.

Debugging is included to show where the depth buffer is being sampled.
  • Loading branch information
DarkStarSword committed Oct 8, 2015
1 parent f2d5a89 commit a0f71fc
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 4 deletions.
45 changes: 43 additions & 2 deletions Mad Max/ShaderFixes/cfe4a25b2677c39b-ps_replace.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Lens flare
// VS e9849e745227d124

cbuffer GlobalConstants : register(b0)
{
Expand Down Expand Up @@ -33,21 +35,60 @@ void main(
float4 fDest;

r0.xy = saturate(InstanceConsts[0].xy);

float4 stereo = StereoParams.Load(0);
// Values from frame analysis of other shaders that sample depth buffer and
// scale to world Z:
float depth = 1 / (InstanceConsts[0].z * 9.9999752 + 0.000025);
float correction = stereo.x * (depth - stereo.y) / depth / 2;


//// Debugging, show original depth buffer sample location in red:
//if (abs(v0.x/1920 - r0.x) < 0.01) {
// o0 = float4(1,0,0,1);
// return;
//}

r0.x += correction;

//// Debugging, show adjusted depth buffer sample location in green:
//if (abs(v0.x/1920 - r0.x) < 0.01) {
// o0 = float4(0,1,0,1);
// return;
//}


r0.x = DepthTexture.Sample(DepthTexture_s, r0.xy).x;
r0.x = r0.x < InstanceConsts[0].z;
r0.x = r0.x ? 0.500000 : 0;
r1.xyzw = saturate(Globals[8].zwzw * float4(-3,3.5,3.5,6) + InstanceConsts[0].xyxy);

r1.x += correction;
r1.z += correction;

r0.y = DepthTexture.Sample(DepthTexture_s, r1.xy).x;
r0.z = DepthTexture.Sample(DepthTexture_s, r1.zw).x;
r0.yz = r0.yz < InstanceConsts[0].zz;
r0.yz = r0.yz ? float2(1.04018739e+009,1.04018739e+009) : 0;

// Decompiler bug misinterpreted 0x3e000000:
// r0.yz = r0.yz ? float2(1.04018739e+009,1.04018739e+009) : 0;
r0.yz = r0.yz ? float2(0.125, 0.125) : 0;

r0.x = r0.x + r0.y;
r0.x = r0.x + r0.z;
r1.xyzw = saturate(Globals[8].zwzw * float4(-5,-3.5,3,-3) + InstanceConsts[0].xyxy);

r1.x += correction;
r1.z += correction;

r0.y = DepthTexture.Sample(DepthTexture_s, r1.xy).x;
r0.z = DepthTexture.Sample(DepthTexture_s, r1.zw).x;
r0.yz = r0.yz < InstanceConsts[0].zz;
r0.yz = r0.yz ? float2(1.04018739e+009,1.04018739e+009) : 0;

// Decompiler bug misinterpreted 0x3e000000:
// r0.yz = r0.yz ? float2(1.04018739e+009,1.04018739e+009) : 0;
r0.yz = r0.yz ? float2(0.125, 0.125) : 0;

r0.x = r0.x + r0.y;
r0.x = r0.x + r0.z;
r0.y = SkyMaskB.Sample(SkyMaskB_s, InstanceConsts[1].xyz).y;
Expand Down
44 changes: 42 additions & 2 deletions Mad Max/ShaderFixes/d6160e64bc5d9be7-ps_replace.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Bloom around lights
// VS e9849e745227d124

cbuffer GlobalConstants : register(b0)
{
Expand Down Expand Up @@ -29,21 +31,59 @@ void main(
float4 fDest;

r0.xy = saturate(InstanceConsts[0].xy);

float4 stereo = StereoParams.Load(0);
// Values from frame analysis of other shaders that sample depth buffer and
// scale to world Z:
float depth = 1 / (InstanceConsts[0].z * 9.9999752 + 0.000025);
float correction = stereo.x * (depth - stereo.y) / depth / 2;


// // Debugging, show original depth buffer sample location in red:
// if (abs(v0.x/1920 - r0.x) < 0.01) {
// o0 = float4(1,0,0,1);
// return;
// }

r0.x += correction;

// // Debugging, show adjusted depth buffer sample location in green:
// if (abs(v0.x/1920 - r0.x) < 0.01) {
// o0 = float4(0,1,0,1);
// return;
// }

r0.x = DepthTexture.Sample(DepthTexture_s, r0.xy).x;
r0.x = r0.x < InstanceConsts[0].z;
r0.x = r0.x ? 0.500000 : 0;
r1.xyzw = saturate(Globals[8].zwzw * float4(-3,3.5,3.5,6) + InstanceConsts[0].xyxy);

r1.x += correction;
r1.z += correction;

r0.y = DepthTexture.Sample(DepthTexture_s, r1.xy).x;
r0.z = DepthTexture.Sample(DepthTexture_s, r1.zw).x;
r0.yz = r0.yz < InstanceConsts[0].zz;
r0.yz = r0.yz ? float2(1.04018739e+009,1.04018739e+009) : 0;

// Decompiler bug misinterpreted 0x3e000000:
//r0.yz = r0.yz ? float2(1.04018739e+009,1.04018739e+009) : 0;
r0.yz = r0.yz ? float2(0.125, 0.125) : 0;

r0.x = r0.x + r0.y;
r0.x = r0.x + r0.z;
r1.xyzw = saturate(Globals[8].zwzw * float4(-5,-3.5,3,-3) + InstanceConsts[0].xyxy);

r1.x += correction;
r1.z += correction;

r0.y = DepthTexture.Sample(DepthTexture_s, r1.xy).x;
r0.z = DepthTexture.Sample(DepthTexture_s, r1.zw).x;
r0.yz = r0.yz < InstanceConsts[0].zz;
r0.yz = r0.yz ? float2(1.04018739e+009,1.04018739e+009) : 0;

// Decompiler bug misinterpreted 0x3e000000:
// r0.yz = r0.yz ? float2(1.04018739e+009,1.04018739e+009) : 0;
r0.yz = r0.yz ? float2(0.125, 0.125) : 0;

r0.x = r0.x + r0.y;
r0.x = r0.x + r0.z;
r1.xyzw = DiffuseTexture.Sample(DiffuseTexture_s, v1.xy).xyzw;
Expand Down

0 comments on commit a0f71fc

Please sign in to comment.