Skip to content

Commit

Permalink
Revert "perf: removed snow mask, do all diffuse specular addition in …
Browse files Browse the repository at this point in the history
…linear" (#447)

This reverts commit 9b45c22.
  • Loading branch information
doodlum authored Aug 22, 2024
1 parent 9b45c22 commit 7b76f1f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 36 deletions.
29 changes: 20 additions & 9 deletions package/Shaders/DeferredCompositeCS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ Texture2D<half3> SpecularTexture : register(t0);
Texture2D<unorm half3> AlbedoTexture : register(t1);
Texture2D<unorm half3> NormalRoughnessTexture : register(t2);
Texture2D<unorm half3> MasksTexture : register(t3);
Texture2D<unorm half3> Masks2Texture : register(t4);

RWTexture2D<half3> MainRW : register(u0);
RWTexture2D<half4> NormalTAAMaskSpecularMaskRW : register(u1);
RWTexture2D<half2> SnowParametersRW : register(u2);

#if defined(DYNAMIC_CUBEMAPS)
Texture2D<float> DepthTexture : register(t4);
Texture2D<half3> ReflectanceTexture : register(t5);
TextureCube<half3> EnvTexture : register(t6);
TextureCube<half3> EnvReflectionsTexture : register(t7);
Texture2D<float> DepthTexture : register(t5);
Texture2D<half3> ReflectanceTexture : register(t6);
TextureCube<half3> EnvTexture : register(t7);
TextureCube<half3> EnvReflectionsTexture : register(t8);

SamplerState LinearSampler : register(s0);
#endif
Expand All @@ -32,11 +34,11 @@ cbuffer SkylightingCB : register(b1)
SkylightingSettings skylightingSettings;
};

Texture3D<sh2> SkylightingProbeArray : register(t8);
Texture3D<sh2> SkylightingProbeArray : register(t9);
#endif

#if defined(SSGI)
Texture2D<half4> SpecularSSGITexture : register(t9);
Texture2D<half4> SpecularSSGITexture : register(t10);
#endif

[numthreads(8, 8, 1)] void main(uint3 dispatchID
Expand All @@ -51,9 +53,14 @@ Texture2D<half4> SpecularSSGITexture : register(t9);
half3 diffuseColor = MainRW[dispatchID.xy];
half3 specularColor = SpecularTexture[dispatchID.xy];
half3 albedo = AlbedoTexture[dispatchID.xy];
half3 masks2 = Masks2Texture[dispatchID.xy];

half2 snowParameters = masks2.xy;
half pbrWeight = masks2.z;

half glossiness = normalGlossiness.z;

half3 color = sRGB2Lin(diffuseColor) + sRGB2Lin(specularColor);
half3 color = lerp(diffuseColor + specularColor, Lin2sRGB(sRGB2Lin(diffuseColor) + sRGB2Lin(specularColor)), pbrWeight);

#if defined(DYNAMIC_CUBEMAPS)

Expand All @@ -66,6 +73,8 @@ Texture2D<half4> SpecularSSGITexture : register(t9);

normalWS = lerp(normalWS, float3(0, 0, 1), wetnessMask);

color = sRGB2Lin(color);

half depth = DepthTexture[dispatchID.xy];

half4 positionCS = half4(2 * half2(uv.x, -uv.y + 1) - 1, depth, 1);
Expand All @@ -80,6 +89,7 @@ Texture2D<half4> SpecularSSGITexture : register(t9);
half roughness = 1.0 - glossiness;
half level = roughness * 7.0;

half3 directionalAmbientColor = sRGB2Lin(mul(DirectionalAmbient, half4(R, 1.0)));
half3 finalIrradiance = 0;

# if defined(INTERIOR)
Expand Down Expand Up @@ -127,12 +137,12 @@ Texture2D<half4> SpecularSSGITexture : register(t9);
# endif

color += reflectance * finalIrradiance;

color = Lin2sRGB(color);
}

#endif

color = Lin2sRGB(color);

#if defined(DEBUG)

# if defined(VR)
Expand All @@ -153,4 +163,5 @@ Texture2D<half4> SpecularSSGITexture : register(t9);

MainRW[dispatchID.xy] = min(color, 250); // Vanilla bloom fix
NormalTAAMaskSpecularMaskRW[dispatchID.xy] = half4(EncodeNormalVanilla(normalVS), 0.0, 0.0);
SnowParametersRW[dispatchID.xy] = snowParameters;
}
49 changes: 27 additions & 22 deletions package/Shaders/Lighting.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,7 @@ struct PS_OUTPUT
float4 Specular : SV_Target4;
float4 Reflectance : SV_Target5;
float4 Masks : SV_Target6;
# if defined(SNOW)
float4 Parameters : SV_Target7;
# endif
# if defined(TERRAIN_BLENDING)
float Depth : SV_Depth;
# endif
Expand Down Expand Up @@ -1729,6 +1727,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
}
# endif

float3 specularColorPBR = 0;
float3 transmissionColor = 0;

float pbrWeight = 1;
Expand Down Expand Up @@ -1979,13 +1978,13 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
lightsDiffuseColor += dirDiffuseColor;
coatLightsDiffuseColor += coatDirDiffuseColor;
transmissionColor += dirTransmissionColor;
specularColor += dirSpecularColor * !InInterior;
specularColorPBR += dirSpecularColor * !InInterior;
# if defined(LOD_LAND_BLEND)
lodLandDiffuseColor += dirLightColor * saturate(dirLightAngle) * dirLightColorMultiplier * dirDetailShadow * parallaxShadow;
# endif
# if defined(WETNESS_EFFECTS)
if (waterRoughnessSpecular < 1.0)
specularColor += PBR::GetWetnessDirectLightSpecularInput(wetnessNormal, worldSpaceViewDirection, normalizedDirLightDirectionWS, lightProperties.LinearCoatLightColor, waterRoughnessSpecular);
specularColorPBR += PBR::GetWetnessDirectLightSpecularInput(wetnessNormal, worldSpaceViewDirection, normalizedDirLightDirectionWS, lightProperties.LinearCoatLightColor, waterRoughnessSpecular);
# endif
}
# else
Expand Down Expand Up @@ -2061,7 +2060,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
lightsDiffuseColor += pointDiffuseColor;
coatLightsDiffuseColor += coatPointDiffuseColor;
transmissionColor += pointTransmissionColor;
specularColor += pointSpecularColor;
specularColorPBR += pointSpecularColor;
}
# else
lightColor *= lightShadow;
Expand Down Expand Up @@ -2184,10 +2183,10 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
lightsDiffuseColor += pointDiffuseColor;
coatLightsDiffuseColor += coatPointDiffuseColor;
transmissionColor += pointTransmissionColor;
specularColor += pointSpecularColor;
specularColorPBR += pointSpecularColor;
# if defined(WETNESS_EFFECTS)
if (waterRoughnessSpecular < 1.0)
specularColor += PBR::GetWetnessDirectLightSpecularInput(wetnessNormal, worldSpaceViewDirection, normalizedLightDirection, lightProperties.LinearCoatLightColor, waterRoughnessSpecular);
specularColorPBR += PBR::GetWetnessDirectLightSpecularInput(wetnessNormal, worldSpaceViewDirection, normalizedLightDirection, lightProperties.LinearCoatLightColor, waterRoughnessSpecular);
# endif
}
# else
Expand Down Expand Up @@ -2267,7 +2266,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
float3 reflectionDiffuseColor = diffuseColor + directionalAmbientColor;

# if defined(SKYLIGHTING)
float skylightingDiffuse = shFuncProductIntegral(skylightingSH, shEvaluateCosineLobe(skylightingSettings.DirectionalDiffuse ? worldSpaceNormal : float3(worldSpaceNormal.xy, worldSpaceNormal.z * 0.5 + 0.5))) / shPI;
float skylightingDiffuse = shFuncProductIntegral(skylightingSH, shEvaluateCosineLobe(skylightingSettings.DirectionalDiffuse ? worldSpaceNormal : float3(0, 0, 1))) / shPI;
skylightingDiffuse = Skylighting::mixDiffuse(skylightingSettings, skylightingDiffuse);
directionalAmbientColor = sRGB2Lin(directionalAmbientColor);
directionalAmbientColor *= skylightingDiffuse;
Expand Down Expand Up @@ -2388,12 +2387,12 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
# endif

# if !defined(DYNAMIC_CUBEMAPS)
specularColor += indirectSpecularLobeWeight * sRGB2Lin(directionalAmbientColor);
specularColorPBR += indirectSpecularLobeWeight * sRGB2Lin(directionalAmbientColor);
# endif

# if !defined(DEFERRED)
# if defined(DYNAMIC_CUBEMAPS)
specularColor += indirectSpecularLobeWeight * DynamicCubemaps::GetDynamicCubemapSpecularIrradiance(screenUV, worldSpaceNormal, worldSpaceVertexNormal, worldSpaceViewDirection, pbrSurfaceProperties.Roughness, viewPosition.z);
specularColorPBR += indirectSpecularLobeWeight * DynamicCubemaps::GetDynamicCubemapSpecularIrradiance(screenUV, worldSpaceNormal, worldSpaceVertexNormal, worldSpaceViewDirection, pbrSurfaceProperties.Roughness, viewPosition.z);
# endif
# else
indirectDiffuseLobeWeight *= vertexColor;
Expand Down Expand Up @@ -2475,20 +2474,22 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
specularColor *= complexSpecular;
# endif // defined (EMAT) && defined(ENVMAP)

color.xyz = sRGB2Lin(color.xyz);

# if !defined(DEFERRED) && !defined(TRUE_PBR)
specularColor = sRGB2Lin(specularColor);
# endif

# if !defined(DEFERRED)
# if !defined(TRUE_PBR)
# if !defined(DEFERRED)
color.xyz += specularColor;
# endif
# endif

color.xyz = sRGB2Lin(color.xyz);

# if defined(WETNESS_EFFECTS) && !defined(TRUE_PBR)
color.xyz += wetnessSpecular * wetnessGlossinessSpecular;
# endif

# if defined(TRUE_PBR) && !defined(DEFERRED)
color.xyz += specularColorPBR;
# endif

color.xyz = Lin2sRGB(color.xyz);

# if defined(LOD_LAND_BLEND) && defined(TRUE_PBR)
Expand All @@ -2499,7 +2500,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
color.xyz = lerp(color.xyz, litLodLandColor, lodLandBlendFactor);

# if defined(DEFERRED)
specularColor = lerp(specularColor, 0, lodLandBlendFactor);
specularColorPBR = lerp(specularColorPBR, 0, lodLandBlendFactor);
indirectDiffuseLobeWeight = lerp(indirectDiffuseLobeWeight, input.Color.xyz * lodLandColor * lodLandFadeFactor, lodLandBlendFactor);
indirectSpecularLobeWeight = lerp(indirectSpecularLobeWeight, 0, lodLandBlendFactor);
pbrGlossiness = lerp(pbrGlossiness, 0, lodLandBlendFactor);
Expand Down Expand Up @@ -2605,7 +2606,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace

# if defined(SNOW)
# if defined(TRUE_PBR)
psout.Parameters.x = RGBToLuminanceAlternative(specularColor);
psout.Parameters.x = RGBToLuminanceAlternative(specularColorPBR);
# else
psout.Parameters.x = RGBToLuminanceAlternative(lightsSpecularColor);
# endif
Expand Down Expand Up @@ -2641,7 +2642,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace

float3 outputSpecular = specularColor.xyz;
# if defined(TRUE_PBR)
outputSpecular = Lin2sRGB(specularColor.xyz);
outputSpecular = Lin2sRGB(specularColorPBR.xyz);
# endif
psout.Specular = float4(outputSpecular, psout.Diffuse.w);

Expand All @@ -2653,6 +2654,12 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace

float outGlossiness = saturate(glossiness * SSRParams.w);

# if defined(TRUE_PBR)
psout.Parameters.z = pbrWeight;
# else
psout.Parameters.z = 0;
# endif

# if defined(TRUE_PBR)
psout.Reflectance = float4(indirectSpecularLobeWeight, psout.Diffuse.w);
psout.NormalGlossiness = float4(EncodeNormal(screenSpaceNormal), pbrGlossiness, psout.Diffuse.w);
Expand All @@ -2664,9 +2671,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
psout.NormalGlossiness = float4(EncodeNormal(screenSpaceNormal), outGlossiness, psout.Diffuse.w);
# endif

# if defined(SNOW)
psout.Parameters.w = psout.Diffuse.w;
# endif

# if (defined(ENVMAP) || defined(MULTI_LAYER_PARALLAX) || defined(EYE))
# if defined(DYNAMIC_CUBEMAPS)
Expand Down
16 changes: 11 additions & 5 deletions src/Deferred.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ void Deferred::SetupResources()
SetupRenderTarget(NORMALROUGHNESS, texDesc, srvDesc, rtvDesc, uavDesc, DXGI_FORMAT_R8G8B8A8_UNORM);
// Masks
SetupRenderTarget(MASKS, texDesc, srvDesc, rtvDesc, uavDesc, DXGI_FORMAT_R8G8B8A8_UNORM);
// Additional Masks
SetupRenderTarget(MASKS2, texDesc, srvDesc, rtvDesc, uavDesc, DXGI_FORMAT_R8G8B8A8_UNORM);
}

{
Expand Down Expand Up @@ -326,7 +328,7 @@ void Deferred::StartDeferred()
SPECULAR,
REFLECTANCE,
MASKS,
forwardRenderTargets[3]
MASKS2
};

for (uint i = 2; i < 8; i++) {
Expand Down Expand Up @@ -362,9 +364,11 @@ void Deferred::DeferredPasses()
auto albedo = renderer->GetRuntimeData().renderTargets[ALBEDO];
auto normalRoughness = renderer->GetRuntimeData().renderTargets[NORMALROUGHNESS];
auto masks = renderer->GetRuntimeData().renderTargets[MASKS];
auto masks2 = renderer->GetRuntimeData().renderTargets[MASKS2];

auto main = renderer->GetRuntimeData().renderTargets[forwardRenderTargets[0]];
auto normals = renderer->GetRuntimeData().renderTargets[forwardRenderTargets[2]];
auto snow = renderer->GetRuntimeData().renderTargets[forwardRenderTargets[3]];

auto depth = renderer->GetDepthStencilData().depthStencils[RE::RENDER_TARGETS_DEPTHSTENCIL::kPOST_ZPREPASS_COPY];
auto reflectance = renderer->GetRuntimeData().renderTargets[REFLECTANCE];
Expand All @@ -389,12 +393,13 @@ void Deferred::DeferredPasses()
ID3D11Buffer* buffer = skylighting->loaded ? skylighting->skylightingCB->CB() : nullptr;
context->CSSetConstantBuffers(1, 1, &buffer);

ID3D11ShaderResourceView* srvs[5]{
ID3D11ShaderResourceView* srvs[6]{
albedo.SRV,
normalRoughness.SRV,
skylighting->loaded ? depth.depthSRV : nullptr,
skylighting->loaded ? skylighting->texProbeArray->srv.get() : nullptr,
ssgi->settings.Enabled ? ssgi->texGI[ssgi->outputGIIdx]->srv.get() : nullptr
ssgi->settings.Enabled ? ssgi->texGI[ssgi->outputGIIdx]->srv.get() : nullptr,
masks2.SRV,
};

context->CSSetShaderResources(0, ARRAYSIZE(srvs), srvs);
Expand Down Expand Up @@ -431,11 +436,12 @@ void Deferred::DeferredPasses()

bool doSSGISpecular = ssgi->loaded && ssgi->settings.Enabled && ssgi->settings.EnableGI && ssgi->settings.EnableSpecularGI;

ID3D11ShaderResourceView* srvs[10]{
ID3D11ShaderResourceView* srvs[11]{
specular.SRV,
albedo.SRV,
normalRoughness.SRV,
masks.SRV,
masks2.SRV,
dynamicCubemaps->loaded ? (terrainBlending->loaded ? terrainBlending->blendedDepthTexture16->srv.get() : depth.depthSRV) : nullptr,
dynamicCubemaps->loaded ? reflectance.SRV : nullptr,
dynamicCubemaps->loaded ? dynamicCubemaps->envTexture->srv.get() : nullptr,
Expand All @@ -449,7 +455,7 @@ void Deferred::DeferredPasses()

context->CSSetShaderResources(0, ARRAYSIZE(srvs), srvs);

ID3D11UnorderedAccessView* uavs[2]{ main.UAV, normals.UAV };
ID3D11UnorderedAccessView* uavs[3]{ main.UAV, normals.UAV, snow.UAV };
context->CSSetUnorderedAccessViews(0, ARRAYSIZE(uavs), uavs, nullptr);

auto shader = interior ? GetComputeMainCompositeInterior() : GetComputeMainComposite();
Expand Down
1 change: 1 addition & 0 deletions src/Deferred.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define REFLECTANCE RE::RENDER_TARGETS::kRAWINDIRECT
#define NORMALROUGHNESS RE::RENDER_TARGETS::kRAWINDIRECT_DOWNSCALED
#define MASKS RE::RENDER_TARGETS::kRAWINDIRECT_PREVIOUS
#define MASKS2 RE::RENDER_TARGETS::kRAWINDIRECT_PREVIOUS_DOWNSCALED

class Deferred
{
Expand Down

0 comments on commit 7b76f1f

Please sign in to comment.